2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1991, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
62 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
64 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
67 #include <sys/param.h>
68 #include <sys/systm.h>
71 #include <sys/namei.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/sysctl.h>
75 #include <sys/malloc.h>
78 #include <sys/quota.h>
80 #include <sys/paths.h>
81 #include <sys/utfconv.h>
84 #include <vfs/vfs_journal.h>
86 #include <miscfs/specfs/specdev.h>
87 #include <hfs/hfs_mount.h>
90 #include "hfs_catalog.h"
91 #include "hfs_cnode.h"
93 #include "hfs_endian.h"
94 #include "hfs_quota.h"
96 #include "hfscommon/headers/FileMgrInternal.h"
97 #include "hfscommon/headers/BTreesInternal.h"
106 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
108 extern void hfs_converterinit(void);
110 extern void inittodr( time_t base
);
113 static int hfs_changefs
__P((struct mount
*mp
, struct hfs_mount_args
*args
,
115 static int hfs_reload
__P((struct mount
*mp
, struct ucred
*cred
, struct proc
*p
));
117 static int hfs_mountfs
__P((struct vnode
*devvp
, struct mount
*mp
, struct proc
*p
,
118 struct hfs_mount_args
*args
));
119 static int hfs_statfs
__P((struct mount
*mp
, register struct statfs
*sbp
,
121 static int hfs_flushfiles
__P((struct mount
*, int, struct proc
*));
123 static int hfs_extendfs
__P((struct mount
*, u_int64_t
, struct proc
*));
126 * Called by vfs_mountroot when mounting HFS Plus as root.
132 extern struct vnode
*rootvp
;
134 struct proc
*p
= current_proc(); /* XXX */
135 struct hfsmount
*hfsmp
;
140 * Get vnode for rootdev.
142 if ((error
= bdevvp(rootdev
, &rootvp
))) {
143 printf("hfs_mountroot: can't setup bdevvp");
146 if ((error
= vfs_rootmountalloc("hfs", "root_device", &mp
))) {
147 vrele(rootvp
); /* release the reference from bdevvp() */
150 if ((error
= hfs_mountfs(rootvp
, mp
, p
, NULL
))) {
151 mp
->mnt_vfc
->vfc_refcount
--;
153 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
154 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
157 vrele(rootvp
); /* release the reference from bdevvp() */
158 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
161 simple_lock(&mountlist_slock
);
162 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
163 simple_unlock(&mountlist_slock
);
166 hfsmp
= VFSTOHFS(mp
);
168 hfsmp
->hfs_uid
= UNKNOWNUID
;
169 hfsmp
->hfs_gid
= UNKNOWNGID
;
170 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
171 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
173 /* Establish the free block reserve. */
174 vcb
= HFSTOVCB(hfsmp
);
175 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
176 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
178 (void)hfs_statfs(mp
, &mp
->mnt_stat
, p
);
181 inittodr(HFSTOVCB(hfsmp
)->vcbLsMod
);
193 hfs_mount(mp
, path
, data
, ndp
, p
)
194 register struct mount
*mp
;
197 struct nameidata
*ndp
;
200 struct hfsmount
*hfsmp
= NULL
;
202 struct hfs_mount_args args
;
208 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
))))
212 * If updating, check whether changing from read-only to
213 * read/write; if there is no device name, that's all we do.
215 if (mp
->mnt_flag
& MNT_UPDATE
) {
217 hfsmp
= VFSTOHFS(mp
);
218 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
219 (mp
->mnt_flag
& MNT_RDONLY
)) {
221 /* use VFS_SYNC to push out System (btree) files */
222 retval
= VFS_SYNC(mp
, MNT_WAIT
, p
->p_ucred
, p
);
223 if (retval
&& ((mp
->mnt_flag
& MNT_FORCE
) == 0))
227 if (mp
->mnt_flag
& MNT_FORCE
)
230 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
232 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
233 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
235 /* also get the volume bitmap blocks */
237 retval
= VOP_FSYNC(hfsmp
->hfs_devvp
, NOCRED
, MNT_WAIT
, p
);
240 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
245 hfs_global_exclusive_lock_acquire(hfsmp
);
247 journal_close(hfsmp
->jnl
);
250 // Note: we explicitly don't want to shutdown
251 // access to the jvp because we may need
252 // it later if we go back to being read-write.
254 hfs_global_exclusive_lock_release(hfsmp
);
258 if ((mp
->mnt_flag
& MNT_RELOAD
) &&
259 (retval
= hfs_reload(mp
, ndp
->ni_cnd
.cn_cred
, p
)))
262 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) &&
263 (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
265 * If upgrade to read-write by non-root, then verify
266 * that user has necessary permissions on the device.
268 if (p
->p_ucred
->cr_uid
!= 0) {
269 devvp
= hfsmp
->hfs_devvp
;
270 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
271 if ((retval
= VOP_ACCESS(devvp
, VREAD
| VWRITE
, p
->p_ucred
, p
))) {
272 VOP_UNLOCK(devvp
, 0, p
);
275 VOP_UNLOCK(devvp
, 0, p
);
277 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
279 if (retval
!= E_NONE
)
282 // If the journal was shut-down previously because we were
283 // asked to be read-only, let's start it back up again now
285 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
286 && hfsmp
->jnl
== NULL
287 && hfsmp
->jvp
!= NULL
) {
290 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
291 flags
= JOURNAL_RESET
;
296 hfs_global_exclusive_lock_acquire(hfsmp
);
298 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
299 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
302 hfsmp
->hfs_phys_block_size
,
305 hfs_sync_metadata
, hfsmp
->hfs_mp
);
307 hfs_global_exclusive_lock_release(hfsmp
);
309 if (hfsmp
->jnl
== NULL
) {
313 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
318 /* Only clear HFS_READ_ONLY after a successfull write */
319 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
322 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
323 (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)) {
324 /* setup private/hidden directory for unlinked files */
325 FindMetaDataDirectory(HFSTOVCB(hfsmp
));
327 hfs_remove_orphans(hfsmp
);
330 * Allow hot file clustering if conditions allow.
332 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) &&
333 (mp
->mnt_flag
& MNT_RDONLY
) &&
334 (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
335 (void) hfs_recording_init(hfsmp
, p
);
339 if (args
.fspec
== 0) {
341 * Process export requests.
343 return vfs_export(mp
, &hfsmp
->hfs_export
, &args
.export
);
348 * Not an update, or updating the name: look up the name
349 * and verify that it refers to a sensible block device.
351 NDINIT(ndp
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, args
.fspec
, p
);
353 if (retval
!= E_NONE
) {
354 DBG_ERR(("hfs_mount: CAN'T GET DEVICE: %s, %x\n", args
.fspec
, ndp
->ni_vp
->v_rdev
));
360 if (devvp
->v_type
!= VBLK
) {
365 if (major(devvp
->v_rdev
) >= nblkdev
) {
372 * If mount by non-root, then verify that user has necessary
373 * permissions on the device.
375 if (p
->p_ucred
->cr_uid
!= 0) {
377 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
378 accessmode
|= VWRITE
;
379 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
380 if ((retval
= VOP_ACCESS(devvp
, accessmode
, p
->p_ucred
, p
))) {
384 VOP_UNLOCK(devvp
, 0, p
);
387 if ((mp
->mnt_flag
& MNT_UPDATE
) == 0) {
388 retval
= hfs_mountfs(devvp
, mp
, p
, &args
);
389 if (retval
!= E_NONE
)
392 if (devvp
!= hfsmp
->hfs_devvp
)
393 retval
= EINVAL
; /* needs translation */
395 retval
= hfs_changefs(mp
, &args
, p
);
399 if (retval
!= E_NONE
) {
403 /* Set the mount flag to indicate that we support volfs */
404 mp
->mnt_flag
|= MNT_DOVOLFS
;
405 if (VFSTOVCB(mp
)->vcbSigWord
== kHFSSigWord
) {
406 /* HFS volumes only want roman-encoded names: */
407 mp
->mnt_flag
|= MNT_FIXEDSCRIPTENCODING
;
409 (void) copyinstr(path
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
-1, &size
);
411 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
412 (void) copyinstr(args
.fspec
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, &size
);
413 bzero(mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
414 (void)hfs_statfs(mp
, &mp
->mnt_stat
, p
);
423 /* Change fs mount parameters */
425 hfs_changefs(mp
, args
, p
)
427 struct hfs_mount_args
*args
;
431 int namefix
, permfix
, permswitch
;
432 struct hfsmount
*hfsmp
;
435 register struct vnode
*vp
, *nvp
;
436 hfs_to_unicode_func_t get_unicode_func
;
437 unicode_to_hfs_func_t get_hfsname_func
;
438 struct cat_desc cndesc
;
439 struct cat_attr cnattr
;
442 hfsmp
= VFSTOHFS(mp
);
443 vcb
= HFSTOVCB(hfsmp
);
444 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
445 ((mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
446 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
447 (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)));
449 /* The root filesystem must operate with actual permissions: */
450 if (permswitch
&& (mp
->mnt_flag
& MNT_ROOTFS
) && (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)) {
451 mp
->mnt_flag
&= ~MNT_UNKNOWNPERMISSIONS
; /* Just say "No". */
454 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)
455 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
457 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
459 namefix
= permfix
= 0;
461 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
462 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
463 gTimeZone
= args
->hfs_timezone
;
466 /* Change the default uid, gid and/or mask */
467 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
468 hfsmp
->hfs_uid
= args
->hfs_uid
;
469 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
472 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
473 hfsmp
->hfs_gid
= args
->hfs_gid
;
474 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
477 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
478 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
479 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
480 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
481 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
482 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
483 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
488 /* Change the hfs encoding value (hfs only) */
489 if ((HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) &&
490 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
491 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
493 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
498 * Connect the new hfs_get_unicode converter but leave
499 * the old hfs_get_hfsname converter in place so that
500 * we can lookup existing vnodes to get their correctly
503 * When we're all finished, we can then connect the new
504 * hfs_get_hfsname converter and release our interest
505 * in the old converters.
507 hfsmp
->hfs_get_unicode
= get_unicode_func
;
508 old_encoding
= hfsmp
->hfs_encoding
;
509 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
513 if (!(namefix
|| permfix
|| permswitch
))
517 * For each active vnode fix things that changed
519 * Note that we can visit a vnode more than once
520 * and we can race with fsync.
522 simple_lock(&mntvnode_slock
);
524 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
526 * If the vnode that we are about to fix is no longer
527 * associated with this mount point, start over.
529 if (vp
->v_mount
!= mp
)
532 simple_lock(&vp
->v_interlock
);
533 nvp
= vp
->v_mntvnodes
.le_next
;
534 if (vp
->v_flag
& VSYSTEM
) {
535 simple_unlock(&vp
->v_interlock
);
538 simple_unlock(&mntvnode_slock
);
539 retval
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
541 simple_lock(&mntvnode_slock
);
542 if (retval
== ENOENT
)
549 retval
= cat_lookup(hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
);
550 /* If we couldn't find this guy skip to the next one */
555 simple_lock(&mntvnode_slock
);
559 /* Get the real uid/gid and perm mask from disk. */
560 if (permswitch
|| permfix
) {
561 cp
->c_uid
= cnattr
.ca_uid
;
562 cp
->c_gid
= cnattr
.ca_gid
;
563 cp
->c_mode
= cnattr
.ca_mode
;
567 * If we're switching name converters then...
568 * Remove the existing entry from the namei cache.
569 * Update name to one based on new encoder.
573 replace_desc(cp
, &cndesc
);
575 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
576 strncpy(vcb
->vcbVN
, cp
->c_desc
.cd_nameptr
, NAME_MAX
);
577 cp
->c_desc
.cd_encoding
= hfsmp
->hfs_encoding
;
580 cat_releasedesc(&cndesc
);
583 simple_lock(&mntvnode_slock
);
585 } /* end for (vp...) */
586 simple_unlock(&mntvnode_slock
);
588 * If we're switching name converters we can now
589 * connect the new hfs_get_hfsname converter and
590 * release our interest in the old converters.
593 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
594 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
595 (void) hfs_relconverter(old_encoding
);
603 * Reload all incore data for a filesystem (used after running fsck on
604 * the root filesystem and finding things to fix). The filesystem must
605 * be mounted read-only.
607 * Things to do to update the mount:
608 * invalidate all cached meta-data.
609 * invalidate all inactive vnodes.
610 * invalidate all cached file data.
611 * re-read volume header from disk.
612 * re-load meta-file info (extents, file size).
613 * re-load B-tree header data.
614 * re-read cnode data for all active vnodes.
617 hfs_reload(mountp
, cred
, p
)
618 register struct mount
*mountp
;
622 register struct vnode
*vp
, *nvp
, *devvp
;
627 struct hfsmount
*hfsmp
;
628 struct HFSPlusVolumeHeader
*vhp
;
630 struct filefork
*forkp
;
631 struct cat_desc cndesc
;
633 if ((mountp
->mnt_flag
& MNT_RDONLY
) == 0)
636 hfsmp
= VFSTOHFS(mountp
);
637 vcb
= HFSTOVCB(hfsmp
);
639 if (vcb
->vcbSigWord
== kHFSSigWord
)
640 return (EINVAL
); /* rooting from HFS is not supported! */
643 * Invalidate all cached meta-data.
645 devvp
= hfsmp
->hfs_devvp
;
646 if (vinvalbuf(devvp
, 0, cred
, p
, 0, 0))
647 panic("hfs_reload: dirty1");
648 InvalidateCatalogCache(vcb
);
651 simple_lock(&mntvnode_slock
);
652 for (vp
= mountp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
653 if (vp
->v_mount
!= mountp
) {
654 simple_unlock(&mntvnode_slock
);
657 nvp
= vp
->v_mntvnodes
.le_next
;
660 * Invalidate all inactive vnodes.
662 if (vrecycle(vp
, &mntvnode_slock
, p
))
666 * Invalidate all cached file data.
668 simple_lock(&vp
->v_interlock
);
669 simple_unlock(&mntvnode_slock
);
670 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, p
)) {
673 if (vinvalbuf(vp
, 0, cred
, p
, 0, 0))
674 panic("hfs_reload: dirty2");
677 * Re-read cnode data for all active vnodes (non-metadata files).
680 if ((vp
->v_flag
& VSYSTEM
) == 0 && !VNODE_IS_RSRC(vp
)) {
681 struct cat_fork
*datafork
;
682 struct cat_desc desc
;
684 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
686 /* lookup by fileID since name could have changed */
687 if ((error
= cat_idlookup(hfsmp
, cp
->c_fileid
, &desc
, &cp
->c_attr
, datafork
))) {
692 /* update cnode's catalog descriptor */
693 (void) replace_desc(cp
, &desc
);
696 simple_lock(&mntvnode_slock
);
698 simple_unlock(&mntvnode_slock
);
701 * Re-read VolumeHeader from disk.
703 sectorsize
= hfsmp
->hfs_phys_block_size
;
705 error
= meta_bread(hfsmp
->hfs_devvp
,
706 (vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
),
707 sectorsize
, NOCRED
, &bp
);
714 vhp
= (HFSPlusVolumeHeader
*) (bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
716 /* Do a quick sanity check */
717 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
718 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
719 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
720 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
721 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
726 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
727 vcb
->vcbAtrb
= (UInt16
) SWAP_BE32 (vhp
->attributes
); /* VCB only uses lower 16 bits */
728 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
729 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
730 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
731 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
732 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
733 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
734 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
735 vcb
->nextAllocation
= SWAP_BE32 (vhp
->nextAllocation
);
736 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
737 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
738 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
739 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
740 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
743 * Re-load meta-file vnode data (extent info, file size, etc).
745 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
746 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
747 forkp
->ff_extents
[i
].startBlock
=
748 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
749 forkp
->ff_extents
[i
].blockCount
=
750 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
752 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
753 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
754 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
757 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
758 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
759 forkp
->ff_extents
[i
].startBlock
=
760 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
761 forkp
->ff_extents
[i
].blockCount
=
762 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
764 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
765 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
766 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
769 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
770 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
771 forkp
->ff_extents
[i
].startBlock
=
772 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
773 forkp
->ff_extents
[i
].blockCount
=
774 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
776 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
777 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
778 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
784 * Re-load B-tree header data
786 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
787 if (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) ))
790 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
791 if (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) ))
794 /* Reload the volume name */
795 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, NULL
, NULL
)))
797 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
798 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
799 cat_releasedesc(&cndesc
);
801 /* Re-establish private/hidden directory for unlinked files */
802 FindMetaDataDirectory(vcb
);
804 /* In case any volume information changed to trigger a notification */
805 hfs_generate_volume_notifications(hfsmp
);
812 get_raw_device(char *fspec
, int is_user
, int ronly
, struct vnode
**rvp
, struct ucred
*cred
, struct proc
*p
)
822 MALLOC(rawbuf
, char *, MAXPATHLEN
, M_HFSMNT
, M_WAITOK
);
823 if (rawbuf
== NULL
) {
829 retval
= copyinstr(fspec
, rawbuf
, MAXPATHLEN
- 1, &namelen
);
830 if (retval
!= E_NONE
) {
831 FREE(rawbuf
, M_HFSMNT
);
835 strcpy(rawbuf
, fspec
);
836 namelen
= strlen(rawbuf
);
839 /* make sure it's null terminated */
840 rawbuf
[MAXPATHLEN
-1] = '\0';
842 dp
= &rawbuf
[namelen
-1];
843 while(dp
>= rawbuf
&& *dp
!= '/') {
853 /* make room for and insert the 'r' for the raw device */
854 memmove(dp
+1, dp
, strlen(dp
)+1);
857 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, rawbuf
, p
);
859 if (retval
!= E_NONE
) {
860 DBG_ERR(("hfs_mountfs: can't open raw device for journal: %s, %x\n", rawbuf
, nd
.ni_vp
->v_rdev
));
861 FREE(rawbuf
, M_HFSMNT
);
866 if ((retval
= VOP_OPEN(*rvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
))) {
871 // don't need this any more
872 FREE(rawbuf
, M_HFSMNT
);
878 (void)VOP_CLOSE(*rvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
882 FREE(rawbuf
, M_HFSMNT
);
890 * Common code for mount and mountroot
893 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct proc
*p
,
894 struct hfs_mount_args
*args
)
897 struct hfsmount
*hfsmp
;
900 HFSMasterDirectoryBlock
*mdbp
;
908 u_int32_t minblksize
;
909 u_int32_t iswritable
;
913 cred
= p
? p
->p_ucred
: NOCRED
;
916 * Disallow multiple mounts of the same device.
917 * Disallow mounting of a device that is currently in use
918 * (except for root, which might share swap device for miniroot).
919 * Flush out any old buffers remaining from a previous use.
921 if ((retval
= vfs_mountedon(devvp
)))
923 if ((vcount(devvp
) > 1) && (devvp
!= rootvp
))
925 if ((retval
= vinvalbuf(devvp
, V_SAVE
, cred
, p
, 0, 0)))
928 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
929 if ((retval
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
)))
935 minblksize
= kHFSBlockSize
;
937 /* Get the real physical block size. */
938 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, cred
, p
)) {
942 /* Switch to 512 byte sectors (temporarily) */
944 u_int32_t size512
= 512;
946 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, cred
, p
)) {
951 /* Get the number of 512 byte physical blocks. */
952 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
956 /* Compute an accurate disk size (i.e. within 512 bytes) */
957 disksize
= blkcnt
* (u_int64_t
)512;
960 * There are only 31 bits worth of block count in
961 * the buffer cache. So for large volumes a 4K
962 * physical block size is needed.
964 if (blkcnt
> (u_int64_t
)0x000000007fffffff) {
965 minblksize
= blksize
= 4096;
967 /* Now switch to our prefered physical block size. */
969 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
973 /* Get the count of physical blocks. */
974 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
982 * minblksize is the minimum physical block size
983 * blksize has our prefered physical block size
984 * blkcnt has the total number of physical blocks
986 devvp
->v_specsize
= blksize
;
988 /* cache the IO attributes */
989 if ((retval
= vfs_init_io_attributes(devvp
, mp
))) {
990 printf("hfs_mountfs: vfs_init_io_attributes returned %d\n",
995 mdb_offset
= HFS_PRI_SECTOR(blksize
);
996 if ((retval
= meta_bread(devvp
, HFS_PRI_SECTOR(blksize
), blksize
, cred
, &bp
))) {
999 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
1000 bcopy(bp
->b_data
+ HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
1004 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
1005 bzero(hfsmp
, sizeof(struct hfsmount
));
1008 * Init the volume information structure
1010 mp
->mnt_data
= (qaddr_t
)hfsmp
;
1011 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
1012 hfsmp
->hfs_vcb
.vcb_hfsmp
= hfsmp
; /* Make VCBTOHFS work */
1013 hfsmp
->hfs_raw_dev
= devvp
->v_rdev
;
1014 hfsmp
->hfs_devvp
= devvp
;
1015 hfsmp
->hfs_phys_block_size
= blksize
;
1016 hfsmp
->hfs_phys_block_count
= blkcnt
;
1017 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
1019 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
1020 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)
1021 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
1022 for (i
= 0; i
< MAXQUOTAS
; i
++)
1023 hfsmp
->hfs_qfiles
[i
].qf_vp
= NULLVP
;
1026 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
1027 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
1028 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
1029 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
1030 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
1031 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
1032 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
1033 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
1035 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
1038 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
1039 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
1041 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
1044 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
1045 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) {
1046 hfsmp
->hfs_uid
= UNKNOWNUID
;
1047 hfsmp
->hfs_gid
= UNKNOWNGID
;
1048 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
1049 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
1053 /* Find out if disk media is writable. */
1054 if (VOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, cred
, p
) == 0) {
1056 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
1058 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
1061 /* Mount a standard HFS disk */
1062 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
1063 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
1064 if (devvp
== rootvp
) {
1065 retval
= EINVAL
; /* Cannot root from HFS standard disks */
1068 /* HFS disks can only use 512 byte physical blocks */
1069 if (blksize
> kHFSBlockSize
) {
1070 blksize
= kHFSBlockSize
;
1071 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1075 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1079 devvp
->v_specsize
= blksize
;
1080 hfsmp
->hfs_phys_block_size
= blksize
;
1081 hfsmp
->hfs_phys_block_count
= blkcnt
;
1084 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
1085 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
1087 /* establish the timezone */
1088 gTimeZone
= args
->hfs_timezone
;
1091 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
1092 &hfsmp
->hfs_get_hfsname
);
1096 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
1098 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1100 } else /* Mount an HFS Plus disk */ {
1101 HFSPlusVolumeHeader
*vhp
;
1102 off_t embeddedOffset
;
1103 int jnl_disable
= 0;
1105 /* Get the embedded Volume Header */
1106 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1107 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
1108 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1109 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1112 * If the embedded volume doesn't start on a block
1113 * boundary, then switch the device to a 512-byte
1114 * block size so everything will line up on a block
1117 if ((embeddedOffset
% blksize
) != 0) {
1118 printf("HFS Mount: embedded volume offset not"
1119 " a multiple of physical block size (%d);"
1120 " switching to 512\n", blksize
);
1122 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
1123 (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1127 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
1128 (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1132 /* XXX do we need to call vfs_init_io_attributes again? */
1133 devvp
->v_specsize
= blksize
;
1134 /* Note: relative block count adjustment */
1135 hfsmp
->hfs_phys_block_count
*=
1136 hfsmp
->hfs_phys_block_size
/ blksize
;
1137 hfsmp
->hfs_phys_block_size
= blksize
;
1140 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1141 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1143 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1145 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1146 retval
= meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1149 bcopy(bp
->b_data
+ HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1152 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1154 } else /* pure HFS+ */ {
1156 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1163 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) && args
->journal_disable
) {
1168 // We only initialize the journal here if the last person
1169 // to mount this volume was journaling aware. Otherwise
1170 // we delay journal initialization until later at the end
1171 // of hfs_MountHFSPlusVolume() because the last person who
1172 // mounted it could have messed things up behind our back
1173 // (so we need to go find the .journal file, make sure it's
1174 // the right size, re-sync up if it was moved, etc).
1176 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1177 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1180 // if we're able to init the journal, mark the mount
1181 // point as journaled.
1183 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1184 mp
->mnt_flag
|= MNT_JOURNALED
;
1186 // if the journal failed to open, then set the lastMountedVersion
1187 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1188 // of just bailing out because the volume is journaled.
1189 if (ronly
!= 0 || devvp
== rootvp
) {
1190 HFSPlusVolumeHeader
*vhp
;
1192 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1194 if (mdb_offset
== 0) {
1195 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1199 retval
= meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1201 vhp
= (HFSPlusVolumeHeader
*)(bp
->b_data
+ HFS_PRI_OFFSET(blksize
));
1203 if (SWAP_BE16(vhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(vhp
->signature
) == kHFSXSigWord
) {
1204 vhp
->lastMountedVersion
= SWAP_BE32('FSK!');
1215 // if this isn't the root device just bail out.
1216 // if it is the root device we just continue on
1217 // in the hopes that fsck_hfs will be able to
1218 // fix any damage that exists on the volume.
1219 if (devvp
!= rootvp
) {
1227 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1229 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
);
1231 * If the backend didn't like our physical blocksize
1232 * then retry with physical blocksize of 512.
1234 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1235 printf("HFS Mount: could not use physical block size "
1236 "(%d) switching to 512\n", blksize
);
1238 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1242 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1246 devvp
->v_specsize
= blksize
;
1247 /* Note: relative block count adjustment (in case this is an embedded volume). */
1248 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1249 hfsmp
->hfs_phys_block_size
= blksize
;
1252 // close and re-open this with the new block size
1253 journal_close(hfsmp
->jnl
);
1255 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1256 mp
->mnt_flag
|= MNT_JOURNALED
;
1260 /* Try again with a smaller block size... */
1261 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
);
1264 (void) hfs_relconverter(0);
1271 mp
->mnt_stat
.f_fsid
.val
[0] = (long)dev
;
1272 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_vfc
->vfc_typenum
;
1273 mp
->mnt_maxsymlinklen
= 0;
1274 devvp
->v_specflags
|= SI_MOUNTEDON
;
1278 * Set the free space warning levels for a non-root volume:
1280 * Set the lower freespace limit (the level that will trigger a warning)
1281 * to 5% of the volume size or 250MB, whichever is less, and the desired
1282 * level (which will cancel the alert request) to 1/2 above that limit.
1283 * Start looking for free space to drop below this level and generate a
1284 * warning immediately if needed:
1286 hfsmp
->hfs_freespace_notify_warninglimit
=
1287 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1288 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1289 hfsmp
->hfs_freespace_notify_desiredlevel
=
1290 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1291 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1294 * Set the free space warning levels for the root volume:
1296 * Set the lower freespace limit (the level that will trigger a warning)
1297 * to 1% of the volume size or 50MB, whichever is less, and the desired
1298 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1300 hfsmp
->hfs_freespace_notify_warninglimit
=
1301 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1302 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1303 hfsmp
->hfs_freespace_notify_desiredlevel
=
1304 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1305 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1309 * Start looking for free space to drop below this level and generate a
1310 * warning immediately if needed:
1312 hfsmp
->hfs_notification_conditions
= 0;
1313 hfs_generate_volume_notifications(hfsmp
);
1316 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1326 (void)VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
1327 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1328 (void)VOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
1332 FREE(hfsmp
, M_HFSMNT
);
1333 mp
->mnt_data
= (qaddr_t
)0;
1340 * Make a filesystem operational.
1341 * Nothing to do at the moment.
1345 hfs_start(mp
, flags
, p
)
1355 * unmount system call
1358 hfs_unmount(mp
, mntflags
, p
)
1363 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1364 int retval
= E_NONE
;
1367 int started_tr
= 0, grabbed_lock
= 0;
1371 if (mntflags
& MNT_FORCE
) {
1372 flags
|= FORCECLOSE
;
1376 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1379 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1380 (void) hfs_recording_suspend(hfsmp
, p
);
1383 * Flush out the b-trees, volume bitmap and Volume Header
1385 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1386 hfs_global_shared_lock_acquire(hfsmp
);
1389 journal_start_transaction(hfsmp
->jnl
);
1393 retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->catalogRefNum
, NOCRED
, MNT_WAIT
, p
);
1394 if (retval
&& !force
)
1397 retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->extentsRefNum
, NOCRED
, MNT_WAIT
, p
);
1398 if (retval
&& !force
)
1401 // if we have an allocation file, sync it too so we don't leave dirty
1403 if (HFSTOVCB(hfsmp
)->allocationsRefNum
) {
1404 if (retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->allocationsRefNum
, NOCRED
, MNT_WAIT
, p
)) {
1410 if (hfsmp
->hfc_filevp
&& (hfsmp
->hfc_filevp
->v_flag
& VSYSTEM
)) {
1411 retval
= VOP_FSYNC(hfsmp
->hfc_filevp
, NOCRED
, MNT_WAIT
, p
);
1412 if (retval
&& !force
)
1416 if (retval
= VOP_FSYNC(hfsmp
->hfs_devvp
, NOCRED
, MNT_WAIT
, p
)) {
1422 /* See if this volume is damaged, is so do not unmount cleanly */
1423 if (HFSTOVCB(hfsmp
)->vcbFlags
& kHFS_DamagedVolume
) {
1424 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1426 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1429 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1431 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
1433 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1435 goto err_exit
; /* could not flush everything */
1439 journal_end_transaction(hfsmp
->jnl
);
1443 hfs_global_shared_lock_release(hfsmp
);
1449 journal_flush(hfsmp
->jnl
);
1453 * Invalidate our caches and release metadata vnodes
1455 (void) hfsUnmount(hfsmp
, p
);
1458 * Last chance to dump unreferenced system files.
1460 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1462 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1463 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1467 journal_close(hfsmp
->jnl
);
1471 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1472 retval
= VOP_CLOSE(hfsmp
->jvp
,
1473 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1480 #ifdef HFS_SPARSE_DEV
1481 /* Drop our reference on the backing fs (if any). */
1482 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1483 struct vnode
* tmpvp
;
1485 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1486 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1487 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1490 #endif /* HFS_SPARSE_DEV */
1492 hfsmp
->hfs_devvp
->v_specflags
&= ~SI_MOUNTEDON
;
1493 retval
= VOP_CLOSE(hfsmp
->hfs_devvp
,
1494 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1496 if (retval
&& !force
)
1499 vrele(hfsmp
->hfs_devvp
);
1500 FREE(hfsmp
, M_HFSMNT
);
1501 mp
->mnt_data
= (qaddr_t
)0;
1505 if (hfsmp
->jnl
&& started_tr
) {
1506 journal_end_transaction(hfsmp
->jnl
);
1509 hfs_global_shared_lock_release(hfsmp
);
1516 * Return the root of a filesystem.
1518 * OUT - vpp, should be locked and vget()'d (to increment usecount and lock)
1527 UInt32 rootObjID
= kRootDirID
;
1529 if ((retval
= VFS_VGET(mp
, &rootObjID
, &nvp
)))
1538 * Do operations associated with quotas
1541 hfs_quotactl(mp
, cmds
, uid
, arg
, p
)
1548 int cmd
, type
, error
;
1551 return (EOPNOTSUPP
);
1554 uid
= p
->p_cred
->p_ruid
;
1555 cmd
= cmds
>> SUBCMDSHIFT
;
1562 if (uid
== p
->p_cred
->p_ruid
)
1566 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
1570 type
= cmds
& SUBCMDMASK
;
1571 if ((u_int
)type
>= MAXQUOTAS
)
1573 if (vfs_busy(mp
, LK_NOWAIT
, 0, p
))
1579 error
= hfs_quotaon(p
, mp
, type
, arg
, UIO_USERSPACE
);
1583 error
= hfs_quotaoff(p
, mp
, type
);
1587 error
= hfs_setquota(mp
, uid
, type
, arg
);
1591 error
= hfs_setuse(mp
, uid
, type
, arg
);
1595 error
= hfs_getquota(mp
, uid
, type
, arg
);
1599 error
= hfs_qsync(mp
);
1603 error
= hfs_quotastat(mp
, type
, arg
);
1619 * Get file system statistics.
1622 hfs_statfs(mp
, sbp
, p
)
1624 register struct statfs
*sbp
;
1627 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1628 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1631 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1633 sbp
->f_bsize
= vcb
->blockSize
;
1634 sbp
->f_iosize
= hfsmp
->hfs_logBlockSize
;
1635 sbp
->f_blocks
= vcb
->totalBlocks
;
1636 sbp
->f_bfree
= hfs_freeblks(hfsmp
, 0);
1637 sbp
->f_bavail
= hfs_freeblks(hfsmp
, 1);
1638 sbp
->f_files
= vcb
->totalBlocks
- 2; /* max files is constrained by total blocks */
1639 sbp
->f_ffree
= MIN(freeCNIDs
, sbp
->f_bavail
);
1642 if (sbp
!= &mp
->mnt_stat
) {
1643 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
1644 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
1645 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
1646 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
1647 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
1654 // XXXdbg -- this is a callback to be used by the journal to
1655 // get meta data blocks flushed out to disk.
1657 // XXXdbg -- be smarter and don't flush *every* block on each
1658 // call. try to only flush some so we don't wind up
1659 // being too synchronous.
1663 hfs_sync_metadata(void *arg
)
1665 struct mount
*mp
= (struct mount
*)arg
;
1667 struct hfsmount
*hfsmp
;
1669 struct vnode
*meta_vp
[3];
1671 int i
, sectorsize
, priIDSector
, altIDSector
, retval
;
1672 int error
, allerror
= 0;
1674 hfsmp
= VFSTOHFS(mp
);
1675 vcb
= HFSTOVCB(hfsmp
);
1677 bflushq(BQ_META
, mp
);
1680 #if 1 // XXXdbg - I do not believe this is necessary...
1681 // but if I pull it out, then the journal
1682 // does not seem to get flushed properly
1683 // when it is closed....
1685 // now make sure the super block is flushed
1686 sectorsize
= hfsmp
->hfs_phys_block_size
;
1687 priIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1688 HFS_PRI_SECTOR(sectorsize
);
1689 retval
= meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1691 panic("hfs: sync_metadata: can't read super-block?! (retval 0x%x, priIDSector)\n",
1692 retval
, priIDSector
);
1695 if (retval
== 0 && (bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1701 // the alternate super block...
1702 // XXXdbg - we probably don't need to do this each and every time.
1703 // hfs_btreeio.c:FlushAlternate() should flag when it was
1705 altIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1706 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
1707 retval
= meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &bp
);
1708 if (retval
== 0 && (bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1718 * Go through the disk queues to initiate sandbagged IO;
1719 * go through the inodes to write those that have been modified;
1720 * initiate the writing of the super block if it has been modified.
1722 * Note: we are always called with the filesystem marked `MPBUSY'.
1725 hfs_sync(mp
, waitfor
, cred
, p
)
1731 struct vnode
*nvp
, *vp
;
1733 struct hfsmount
*hfsmp
;
1735 struct vnode
*meta_vp
[3];
1737 int error
, allerror
= 0;
1740 * During MNT_UPDATE hfs_changefs might be manipulating
1741 * vnodes so back off
1743 if (mp
->mnt_flag
& MNT_UPDATE
)
1746 hfsmp
= VFSTOHFS(mp
);
1747 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1751 // XXXdbg first go through and flush out any modified
1752 // meta data blocks so they go out in order...
1753 bflushq(BQ_META
, mp
);
1754 bflushq(BQ_LRU
, mp
);
1755 // only flush locked blocks if we're not doing journaling
1756 if (hfsmp
->jnl
== NULL
) {
1757 bflushq(BQ_LOCKED
, mp
);
1762 * Write back each 'modified' vnode
1766 simple_lock(&mntvnode_slock
);
1767 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
1770 * If the vnode that we are about to sync is no longer
1771 * associated with this mount point, start over.
1773 if (vp
->v_mount
!= mp
) {
1774 simple_unlock(&mntvnode_slock
);
1778 simple_lock(&vp
->v_interlock
);
1779 nvp
= vp
->v_mntvnodes
.le_next
;
1783 // restart our whole search if this guy is locked
1784 // or being reclaimed.
1785 if (vp
->v_tag
!= VT_HFS
|| cp
== NULL
|| vp
->v_flag
& (VXLOCK
|VORECLAIM
)) {
1786 simple_unlock(&vp
->v_interlock
);
1790 if ((vp
->v_flag
& VSYSTEM
) || (vp
->v_type
== VNON
) ||
1791 (((cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) == 0) &&
1792 (vp
->v_dirtyblkhd
.lh_first
== NULL
) && !(vp
->v_flag
& VHASDIRTY
))) {
1793 simple_unlock(&vp
->v_interlock
);
1794 simple_unlock(&mntvnode_slock
);
1795 simple_lock(&mntvnode_slock
);
1799 simple_unlock(&mntvnode_slock
);
1800 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
1802 if (error
== ENOENT
) {
1804 * If vnode is being reclaimed, yield so
1805 * that it can be removed from our list.
1808 (void) tsleep((caddr_t
)&lbolt
, PINOD
, "hfs_sync", 0);
1811 simple_lock(&mntvnode_slock
);
1815 didhold
= ubc_hold(vp
);
1817 // mark the cnode so that fsync won't flush
1818 // the journal since we're going to do that...
1819 cp
->c_flag
|= C_FROMSYNC
;
1820 if ((error
= VOP_FSYNC(vp
, cred
, waitfor
, p
))) {
1823 cp
->c_flag
&= ~C_FROMSYNC
;
1825 VOP_UNLOCK(vp
, 0, p
);
1829 simple_lock(&mntvnode_slock
);
1832 vcb
= HFSTOVCB(hfsmp
);
1834 meta_vp
[0] = vcb
->extentsRefNum
;
1835 meta_vp
[1] = vcb
->catalogRefNum
;
1836 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1838 /* Now sync our three metadata files */
1839 for (i
= 0; i
< 3; ++i
) {
1842 btvp
= btvp
= meta_vp
[i
];;
1843 if ((btvp
==0) || (btvp
->v_type
== VNON
) || (btvp
->v_mount
!= mp
))
1846 simple_lock(&btvp
->v_interlock
);
1848 if (((cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) == 0) &&
1849 (btvp
->v_dirtyblkhd
.lh_first
== NULL
) && !(btvp
->v_flag
& VHASDIRTY
)) {
1850 simple_unlock(&btvp
->v_interlock
);
1853 simple_unlock(&mntvnode_slock
);
1854 error
= vget(btvp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
1856 simple_lock(&mntvnode_slock
);
1859 if ((error
= VOP_FSYNC(btvp
, cred
, waitfor
, p
)))
1861 VOP_UNLOCK(btvp
, 0, p
);
1863 simple_lock(&mntvnode_slock
);
1866 simple_unlock(&mntvnode_slock
);
1869 * Force stale file system control information to be flushed.
1871 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1872 if ((error
= VOP_FSYNC(hfsmp
->hfs_devvp
, cred
, waitfor
, p
)))
1879 hfs_hotfilesync(hfsmp
, p
);
1881 * Write back modified superblock.
1884 if (IsVCBDirty(vcb
)) {
1885 // XXXdbg - debugging, remove
1887 //printf("hfs: sync: strange, a journaled volume w/dirty VCB? jnl 0x%x hfsmp 0x%x\n",
1888 // hfsmp->jnl, hfsmp);
1891 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1897 journal_flush(hfsmp
->jnl
);
1906 * File handle to vnode
1908 * Have to be really careful about stale file handles:
1909 * - check that the cnode id is valid
1910 * - call hfs_vget() to get the locked cnode
1911 * - check for an unallocated cnode (i_mode == 0)
1912 * - check that the given client host has export rights and return
1913 * those rights via. exflagsp and credanonp
1916 hfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1917 register struct mount
*mp
;
1922 struct ucred
**credanonp
;
1924 struct hfsfid
*hfsfhp
;
1930 hfsfhp
= (struct hfsfid
*)fhp
;
1933 * Get the export permission structure for this <mp, client> tuple.
1935 np
= vfs_export_lookup(mp
, &VFSTOHFS(mp
)->hfs_export
, nam
);
1936 if (nam
&& (np
== NULL
)) {
1940 result
= VFS_VGET(mp
, &hfsfhp
->hfsfid_cnid
, &nvp
);
1941 if (result
) return result
;
1942 if (nvp
== NULL
) return ESTALE
;
1944 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1945 * For NFS, we are assuming that only if the createtime was moved
1946 * forward would it mean the fileID got reused in that session by
1947 * wrapping. We don't have a volume ID or other unique identifier to
1948 * to use here for a generation ID across reboots, crashes where
1949 * metadata noting lastFileID didn't make it to disk but client has
1950 * it, or volume erasures where fileIDs start over again. Lastly,
1951 * with HFS allowing "wraps" of fileIDs now, this becomes more
1952 * error prone. Future, would be change the "wrap bit" to a unique
1953 * wrap number and use that for generation number. For now do this.
1955 if ((hfsfhp
->hfsfid_gen
< VTOC(nvp
)->c_itime
)) {
1960 if (VNAME(nvp
) == NULL
) {
1961 struct cnode
*cp
= VTOC(nvp
);
1963 if (nvp
== cp
->c_rsrc_vp
) {
1964 // the +1/-2 thing is to skip the leading "/" on the rsrc fork spec
1965 // and to not count the trailing null byte at the end of the string.
1966 VNAME(nvp
) = add_name(_PATH_RSRCFORKSPEC
+1, sizeof(_PATH_RSRCFORKSPEC
)-2, 0, 0);
1968 VNAME(nvp
) = add_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
, 0, 0);
1974 *exflagsp
= np
->netc_exflags
;
1975 *credanonp
= &np
->netc_anon
;
1983 * Vnode pointer to File handle
1992 struct hfsfid
*hfsfhp
;
1994 if (ISHFS(VTOVCB(vp
)))
1995 return (EOPNOTSUPP
); /* hfs standard is not exportable */
1998 hfsfhp
= (struct hfsfid
*)fhp
;
1999 hfsfhp
->hfsfid_len
= sizeof(struct hfsfid
);
2000 hfsfhp
->hfsfid_pad
= 0;
2001 hfsfhp
->hfsfid_cnid
= cp
->c_fileid
;
2002 hfsfhp
->hfsfid_gen
= cp
->c_itime
;
2009 * Initial HFS filesystems, done only once.
2013 struct vfsconf
*vfsp
;
2015 static int done
= 0;
2021 hfs_converterinit();
2029 * Allocate Catalog Iterator cache...
2031 (void) InitCatalogCache();
2037 hfs_getmountpoint(vp
, hfsmpp
)
2039 struct hfsmount
**hfsmpp
;
2041 struct hfsmount
* hfsmp
;
2046 if ((vp
->v_flag
& VROOT
) == 0)
2049 if (strcmp(vp
->v_mount
->mnt_stat
.f_fstypename
, "hfs") != 0)
2054 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
2063 #include <sys/filedesc.h>
2067 * HFS filesystem related variables.
2070 hfs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
2079 extern u_int32_t
hfs_getencodingbias(void);
2080 extern void hfs_setencodingbias(u_int32_t
);
2083 struct sysctl_req
*req
;
2086 struct hfsmount
*hfsmp
;
2089 /* all sysctl names at this level are terminal */
2091 if (name
[0] == HFS_ENCODINGBIAS
) {
2094 bias
= hfs_getencodingbias();
2095 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
2096 if (error
== 0 && newp
)
2097 hfs_setencodingbias(bias
);
2100 } else if (name
[0] == HFS_EXTEND_FS
) {
2105 if ((error
= hfs_getmountpoint(p
->p_fd
->fd_cdir
, &hfsmp
)))
2107 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, &newsize
);
2111 error
= hfs_extendfs(HFSTOVFS(hfsmp
), newsize
, p
);
2114 } else if (name
[0] == HFS_ENCODINGHINT
) {
2118 u_int16_t
*unicode_name
;
2121 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
2122 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
2123 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
2125 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
2127 error
= utf8_decodestr(filename
, newlen
- 1, unicode_name
,
2128 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
2130 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
2131 error
= sysctl_int(oldp
, oldlenp
, NULL
, NULL
, &hint
);
2134 FREE(unicode_name
, M_TEMP
);
2135 FREE(filename
, M_TEMP
);
2138 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
2139 // make the file system journaled...
2140 struct vnode
*vp
= p
->p_fd
->fd_cdir
, *jvp
;
2143 struct cat_attr jnl_attr
, jinfo_attr
;
2144 struct cat_fork jnl_fork
, jinfo_fork
;
2147 /* Only root can enable journaling */
2148 if (current_proc()->p_ucred
->cr_uid
!= 0) {
2153 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2156 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2157 printf("hfs: can't make a plain hfs volume journaled.\n");
2162 printf("hfs: volume @ mp 0x%x is already journaled!\n", vp
->v_mount
);
2166 vcb
= HFSTOVCB(hfsmp
);
2167 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
2168 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
2170 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
2174 // make sure these both exist!
2175 if ( GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
2176 || GetFileInfo(vcb
, kRootDirID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2181 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, FSCRED
, p
);
2184 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2185 (off_t
)name
[2], (off_t
)name
[3]);
2187 jvp
= hfsmp
->hfs_devvp
;
2188 jnl
= journal_create(jvp
,
2189 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2190 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2191 (off_t
)((unsigned)name
[3]),
2193 hfsmp
->hfs_phys_block_size
,
2196 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2199 printf("hfs: FAILED to create the journal!\n");
2200 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2201 VOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
);
2208 hfs_global_exclusive_lock_acquire(hfsmp
);
2210 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2211 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2215 // save this off for the hack-y check in hfs_remove()
2216 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2217 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2218 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2219 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2221 hfsmp
->hfs_mp
->mnt_flag
|= MNT_JOURNALED
;
2223 hfs_global_exclusive_lock_release(hfsmp
);
2224 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2227 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2228 // clear the journaling bit
2229 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2233 /* Only root can disable journaling */
2234 if (current_proc()->p_ucred
->cr_uid
!= 0) {
2240 printf("hfs: disabling journaling for mount @ 0x%x\n", vp
->v_mount
);
2244 hfs_global_exclusive_lock_acquire(hfsmp
);
2246 // Lights out for you buddy!
2250 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2251 VOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
);
2255 hfsmp
->hfs_mp
->mnt_flag
&= ~MNT_JOURNALED
;
2256 hfsmp
->jnl_start
= 0;
2257 hfsmp
->hfs_jnlinfoblkid
= 0;
2258 hfsmp
->hfs_jnlfileid
= 0;
2260 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2262 hfs_global_exclusive_lock_release(hfsmp
);
2263 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2266 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2267 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2268 off_t jnl_start
, jnl_size
;
2271 if (hfsmp
->jnl
== NULL
) {
2275 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2276 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2279 if ((error
= copyout((caddr_t
)&jnl_start
, (void *)name
[1], sizeof(off_t
))) != 0) {
2282 if ((error
= copyout((caddr_t
)&jnl_size
, (void *)name
[2], sizeof(off_t
))) != 0) {
2287 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2289 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2291 } else if (name
[0] == VFS_CTL_QUERY
) {
2292 req
= oldp
; /* we're new style vfs sysctl. */
2294 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2295 if (error
) return (error
);
2297 mp
= vfs_getvfs(&vc
.vc_fsid
);
2298 if (mp
== NULL
) return (ENOENT
);
2300 hfsmp
= VFSTOHFS(mp
);
2301 bzero(&vq
, sizeof(vq
));
2302 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2303 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2306 return (EOPNOTSUPP
);
2310 /* This will return a vnode of either a directory or a data vnode based on an object id. If
2311 * it is a file id, its data fork will be returned.
2314 hfs_vget(mp
, ino
, vpp
)
2319 cnid_t cnid
= *(cnid_t
*)ino
;
2321 /* Check for cnids that should't be exported. */
2322 if ((cnid
< kHFSFirstUserCatalogNodeID
)
2323 && (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
))
2325 /* Don't export HFS Private Data dir. */
2326 if (cnid
== VFSTOHFS(mp
)->hfs_privdir_desc
.cd_cnid
)
2329 return (hfs_getcnode(VFSTOHFS(mp
), cnid
, NULL
, 0, NULL
, NULL
, vpp
));
2333 * Check to see if a given vnode is only referenced for events:
2334 * [ entered with vp->v_interlock locked ]
2337 hfs_evtonly(struct vnode
*vp
)
2341 ubc_refcount
= UBCINFOEXISTS(vp
) ? 1 : 0;
2342 return (vp
->v_usecount
== (ubc_refcount
+ EVTONLYREFS(vp
)));
2346 * Check to see if all non-system vnodes for a given mountpoint are events-only
2349 hfs_flush_evtonly(struct mount
*mp
, int flags
, int dispose
, struct proc
*p
)
2351 struct vnode
*vp
, *nvp
;
2354 simple_lock(&mntvnode_slock
);
2356 for (vp
= LIST_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= nvp
) {
2357 if (vp
->v_mount
!= mp
) goto loop
;
2358 nvp
= vp
->v_mntvnodes
.le_next
;
2360 simple_lock(&vp
->v_interlock
);
2362 * Skip over a vnodes marked VSYSTEM or VNOFLUSH.
2364 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) || (vp
->v_flag
& VNOFLUSH
))) {
2365 simple_unlock(&vp
->v_interlock
);
2369 * Skip over a vnodes marked VSWAP.
2371 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
2372 simple_unlock(&vp
->v_interlock
);
2375 if (hfs_evtonly(vp
)) {
2377 /* "dispose" implies "forcibly", a la "FORCECLOSE": */
2378 simple_unlock(&mntvnode_slock
);
2380 simple_lock(&mntvnode_slock
);
2382 simple_unlock(&vp
->v_interlock
);
2387 simple_unlock(&vp
->v_interlock
);
2389 /* If asked to dispose, keep trying. If only checking, the answer is now known. */
2396 simple_unlock(&mntvnode_slock
);
2402 * Flush out all the files in a filesystem.
2405 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2407 struct hfsmount
*hfsmp
;
2408 struct vnode
*skipvp
= NULLVP
;
2409 struct vnode
*rsrcvp
;
2414 hfsmp
= VFSTOHFS(mp
);
2418 * The open quota files have an indirect reference on
2419 * the root directory vnode. We must account for this
2420 * extra reference when doing the intial vflush.
2423 if (mp
->mnt_flag
& MNT_QUOTA
) {
2425 /* Find out how many quota files we have open. */
2426 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2427 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2431 /* Obtain the root vnode so we can skip over it. */
2432 if (hfs_chashget(hfsmp
->hfs_raw_dev
, kRootDirID
, 0,
2433 &skipvp
, &rsrcvp
) == NULL
) {
2439 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2441 * If the vflush() call failed solely because there are
2442 * some event-only vnodes in the list, then forcibly get
2443 * rid of those vnodes before the final vflush() pass.
2445 if ((error
== EBUSY
) && hfs_flush_evtonly(mp
, SKIPSYSTEM
| SKIPSWAP
, 0, p
)) {
2446 (void) hfs_flush_evtonly(mp
, SKIPSYSTEM
| SKIPSWAP
, 1, p
);
2448 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2451 if (mp
->mnt_flag
& MNT_QUOTA
) {
2454 * See if there are additional references on the
2455 * root vp besides the ones obtained from the open
2456 * quota files and the hfs_chashget call above.
2459 (skipvp
->v_usecount
> (1 + quotafilecnt
))) {
2460 error
= EBUSY
; /* root directory is still open */
2464 if (error
&& (flags
& FORCECLOSE
) == 0)
2467 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2468 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2470 hfs_quotaoff(p
, mp
, i
);
2472 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2480 * Update volume encoding bitmap (HFS Plus only)
2484 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2486 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2487 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2492 case kTextEncodingMacUkrainian
:
2493 index
= kIndexMacUkrainian
;
2495 case kTextEncodingMacFarsi
:
2496 index
= kIndexMacFarsi
;
2504 HFSTOVCB(hfsmp
)->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2505 HFSTOVCB(hfsmp
)->vcbFlags
|= 0xFF00;
2510 * Update volume stats
2514 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2518 vcb
= HFSTOVCB(hfsmp
);
2519 vcb
->vcbFlags
|= 0xFF00;
2520 vcb
->vcbLsMod
= time
.tv_sec
;
2526 if (vcb
->vcbDirCnt
!= 0xFFFFFFFF)
2528 if (inroot
&& vcb
->vcbNmRtDirs
!= 0xFFFF)
2532 if (vcb
->vcbDirCnt
!= 0)
2534 if (inroot
&& vcb
->vcbNmRtDirs
!= 0xFFFF)
2538 if (vcb
->vcbFilCnt
!= 0xFFFFFFFF)
2540 if (inroot
&& vcb
->vcbNmFls
!= 0xFFFF)
2544 if (vcb
->vcbFilCnt
!= 0)
2546 if (inroot
&& vcb
->vcbNmFls
!= 0xFFFF)
2552 hfs_flushvolumeheader(hfsmp
, 0, 0);
2560 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2562 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2563 struct filefork
*fp
;
2564 HFSMasterDirectoryBlock
*mdb
;
2565 struct buf
*bp
= NULL
;
2570 sectorsize
= hfsmp
->hfs_phys_block_size
;
2571 retval
= bread(hfsmp
->hfs_devvp
, HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2578 DBG_ASSERT(bp
!= NULL
);
2579 DBG_ASSERT(bp
->b_data
!= NULL
);
2580 DBG_ASSERT(bp
->b_bcount
== size
);
2583 panic("hfs: standard hfs volumes should not be journaled!\n");
2586 mdb
= (HFSMasterDirectoryBlock
*)(bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
2588 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2589 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2590 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2591 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2592 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2593 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2594 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2595 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2597 namelen
= strlen(vcb
->vcbVN
);
2598 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2599 /* Retry with MacRoman in case that's how it was exported. */
2601 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2603 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2604 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2605 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2606 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2607 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2609 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2611 fp
= VTOF(vcb
->extentsRefNum
);
2612 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2613 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2614 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2615 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2616 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2617 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2618 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2619 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2621 fp
= VTOF(vcb
->catalogRefNum
);
2622 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2623 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2624 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2625 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2626 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2627 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2628 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2629 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2631 /* If requested, flush out the alternate MDB */
2633 struct buf
*alt_bp
= NULL
;
2636 altIDSector
= HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2638 if (meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2639 bcopy(mdb
, alt_bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2641 (void) VOP_BWRITE(alt_bp
);
2646 if (waitfor
!= MNT_WAIT
)
2649 retval
= VOP_BWRITE(bp
);
2651 MarkVCBClean( vcb
);
2657 * Flush any dirty in-memory mount data to the on-disk
2660 * Note: the on-disk volume signature is intentionally
2661 * not flushed since the on-disk "H+" and "HX" signatures
2662 * are always stored in-memory as "H+".
2666 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2668 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2669 struct filefork
*fp
;
2670 HFSPlusVolumeHeader
*volumeHeader
;
2677 u_int16_t signature
;
2680 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2683 if (vcb
->vcbSigWord
== kHFSSigWord
)
2684 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2688 sectorsize
= hfsmp
->hfs_phys_block_size
;
2689 priIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2690 HFS_PRI_SECTOR(sectorsize
);
2693 hfs_global_shared_lock_acquire(hfsmp
);
2695 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
2696 hfs_global_shared_lock_release(hfsmp
);
2701 retval
= meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2707 journal_end_transaction(hfsmp
->jnl
);
2709 hfs_global_shared_lock_release(hfsmp
);
2711 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2716 journal_modify_block_start(hfsmp
->jnl
, bp
);
2719 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
2722 * Sanity check what we just read.
2724 signature
= SWAP_BE16 (volumeHeader
->signature
);
2725 version
= SWAP_BE16 (volumeHeader
->version
);
2726 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2727 (version
< kHFSPlusVersion
) || (version
> 100) ||
2728 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2730 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2731 vcb
->vcbVN
, signature
, version
,
2732 SWAP_BE32 (volumeHeader
->blockSize
));
2734 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2740 * For embedded HFS+ volumes, update create date if it changed
2741 * (ie from a setattrlist call)
2743 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2744 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2746 HFSMasterDirectoryBlock
*mdb
;
2748 retval
= meta_bread(hfsmp
->hfs_devvp
, HFS_PRI_SECTOR(sectorsize
),
2749 sectorsize
, NOCRED
, &bp2
);
2755 mdb
= (HFSMasterDirectoryBlock
*)(bp2
->b_data
+
2756 HFS_PRI_OFFSET(sectorsize
));
2758 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2762 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2765 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2769 journal_modify_block_end(hfsmp
->jnl
, bp2
);
2771 (void) VOP_BWRITE(bp2
); /* write out the changes */
2776 brelse(bp2
); /* just release it */
2781 /* Note: only update the lower 16 bits worth of attributes */
2782 volumeHeader
->attributes
= SWAP_BE32 ((SWAP_BE32 (volumeHeader
->attributes
) & 0xFFFF0000) + (UInt16
) vcb
->vcbAtrb
);
2783 volumeHeader
->journalInfoBlock
= SWAP_BE32(vcb
->vcbJinfoBlock
);
2785 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2787 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2789 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2790 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2791 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2792 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2793 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2794 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2795 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2796 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2797 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2798 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2799 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2800 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2802 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0)
2804 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2806 /* Sync Extents over-flow file meta data */
2807 fp
= VTOF(vcb
->extentsRefNum
);
2808 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2809 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2810 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2811 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2812 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2814 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2815 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2816 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2817 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2819 /* Sync Catalog file meta data */
2820 fp
= VTOF(vcb
->catalogRefNum
);
2821 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2822 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2823 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2824 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2825 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2827 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2828 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2829 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2830 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2832 /* Sync Allocation file meta data */
2833 fp
= VTOF(vcb
->allocationsRefNum
);
2834 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2835 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2836 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2837 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2838 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2840 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2841 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2842 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2843 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2845 /* If requested, flush out the alternate volume header */
2847 struct buf
*alt_bp
= NULL
;
2850 altIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2851 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2853 if (meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2855 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2858 bcopy(volumeHeader
, alt_bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2861 journal_modify_block_end(hfsmp
->jnl
, alt_bp
);
2863 (void) VOP_BWRITE(alt_bp
);
2871 journal_modify_block_end(hfsmp
->jnl
, bp
);
2872 journal_end_transaction(hfsmp
->jnl
);
2874 if (waitfor
!= MNT_WAIT
)
2877 retval
= VOP_BWRITE(bp
);
2878 /* When critical data changes, flush the device cache */
2879 if (critical
&& (retval
== 0)) {
2880 (void) VOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2881 NULL
, FWRITE
, NOCRED
, current_proc());
2885 hfs_global_shared_lock_release(hfsmp
);
2887 vcb
->vcbFlags
&= 0x00FF;
2893 * Extend a file system.
2896 hfs_extendfs(struct mount
*mp
, u_int64_t newsize
, struct proc
*p
)
2899 struct vnode
*devvp
;
2901 struct hfsmount
*hfsmp
;
2902 struct filefork
*fp
= NULL
;
2904 struct cat_fork forkdata
;
2906 u_int64_t newblkcnt
;
2908 u_int64_t sectorcnt
;
2909 u_int32_t sectorsize
;
2910 daddr_t prev_alt_sector
;
2914 hfsmp
= VFSTOHFS(mp
);
2915 devvp
= hfsmp
->hfs_devvp
;
2916 vcb
= HFSTOVCB(hfsmp
);
2919 * - HFS Plus file systems only.
2920 * - Journaling must be enabled.
2921 * - No embedded volumes.
2923 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
2924 (hfsmp
->jnl
== NULL
) ||
2925 (vcb
->hfsPlusIOPosOffset
!= 0)) {
2929 * If extending file system by non-root, then verify
2930 * ownership and check permissions.
2932 if (p
->p_ucred
->cr_uid
!= 0) {
2933 error
= hfs_root(mp
, &vp
);
2936 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, p
->p_ucred
, p
, 0);
2938 error
= hfs_write_access(vp
, p
->p_ucred
, p
, false);
2944 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
2945 error
= VOP_ACCESS(devvp
, VREAD
| VWRITE
, p
->p_ucred
, p
);
2946 VOP_UNLOCK(devvp
, 0, p
);
2950 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, FSCRED
, p
)) {
2953 if (sectorsize
!= hfsmp
->hfs_phys_block_size
) {
2956 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, FSCRED
, p
)) {
2959 if ((sectorsize
* sectorcnt
) < newsize
) {
2960 printf("hfs_extendfs: not enough space on device\n");
2963 oldsize
= (u_int64_t
)hfsmp
->hfs_phys_block_count
*
2964 (u_int64_t
)hfsmp
->hfs_phys_block_size
;
2967 * Validate new size.
2969 if ((newsize
<= oldsize
) || (newsize
% vcb
->blockSize
)) {
2970 printf("hfs_extendfs: invalid size\n");
2973 newblkcnt
= newsize
/ vcb
->blockSize
;
2974 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
2977 addblks
= newblkcnt
- vcb
->totalBlocks
;
2979 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
2981 * Enclose changes inside a transaction.
2983 hfs_global_shared_lock_acquire(hfsmp
);
2984 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
2985 hfs_global_shared_lock_release(hfsmp
);
2990 * Remember the location of existing alternate VH.
2992 prev_alt_sector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2993 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2995 vp
= vcb
->allocationsRefNum
;
2996 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
3001 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
3004 * Calculate additional space required (if any) by allocation bitmap.
3006 bitmapblks
= roundup(newblkcnt
/ 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
3007 if (bitmapblks
> fp
->ff_blocks
)
3008 bitmapblks
-= fp
->ff_blocks
;
3012 if (bitmapblks
> 0) {
3017 * Add a new extent to the allocation bitmap file.
3019 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
3021 printf("hfs_extendfs: error %d adding extents\n", error
);
3024 blkcnt
= bitmapblks
;
3025 blkno
= fp
->ff_blocks
;
3026 fp
->ff_blocks
+= bitmapblks
;
3027 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3028 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3030 * Zero out the new bitmap blocks.
3035 while (blkcnt
> 0) {
3036 error
= meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
3043 bzero((char *)bp
->b_data
, vcb
->blockSize
);
3044 bp
->b_flags
|= B_AGE
;
3053 printf("hfs_extendfs: error %d clearing blocks\n", error
);
3057 * Mark the new bitmap space as allocated.
3059 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
3061 printf("hfs_extendfs: error %d setting bitmap\n", error
);
3066 * Mark the new alternate VH as allocated.
3068 if (vcb
->blockSize
== 512)
3069 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
3071 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
3073 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
3077 * Mark the old alternate VH as free.
3079 if (vcb
->blockSize
== 512)
3080 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
3082 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
3085 * Adjust file system variables for new space.
3087 vcb
->totalBlocks
+= addblks
;
3088 vcb
->freeBlocks
+= addblks
- bitmapblks
;
3089 hfsmp
->hfs_phys_block_count
= newsize
/ sectorsize
;
3092 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3094 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
3096 * Restore to old state.
3098 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3099 vcb
->totalBlocks
-= addblks
;
3100 vcb
->freeBlocks
-= addblks
- bitmapblks
;
3101 hfsmp
->hfs_phys_block_count
= oldsize
/ sectorsize
;
3103 if (vcb
->blockSize
== 512)
3104 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
3106 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
3110 * Invalidate the old alternate volume header.
3113 if (meta_bread(hfsmp
->hfs_devvp
, prev_alt_sector
, sectorsize
,
3114 NOCRED
, &bp
) == 0) {
3115 journal_modify_block_start(hfsmp
->jnl
, bp
);
3116 bzero(bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
3117 journal_modify_block_end(hfsmp
->jnl
, bp
);
3123 /* Restore allocation fork. */
3124 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3125 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3128 VOP_UNLOCK(vp
, 0, p
);
3130 journal_end_transaction(hfsmp
->jnl
);
3131 hfs_global_shared_lock_release(hfsmp
);
3138 * hfs vfs operations.
3140 struct vfsops hfs_vfsops
= {