2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1991, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 * (c) UNIX System Laboratories, Inc.
29 * All or some portions of this file are derived from material licensed
30 * to the University of California by American Telephone and Telegraph
31 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
32 * the permission of UNIX System Laboratories, Inc.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
65 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
67 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
70 #include <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/namei.h>
75 #include <sys/vnode.h>
76 #include <sys/mount.h>
77 #include <sys/sysctl.h>
78 #include <sys/malloc.h>
81 #include <sys/quota.h>
83 #include <sys/paths.h>
84 #include <sys/utfconv.h>
87 #include <vfs/vfs_journal.h>
89 #include <miscfs/specfs/specdev.h>
90 #include <hfs/hfs_mount.h>
93 #include "hfs_catalog.h"
94 #include "hfs_cnode.h"
96 #include "hfs_endian.h"
97 #include "hfs_quota.h"
99 #include "hfscommon/headers/FileMgrInternal.h"
100 #include "hfscommon/headers/BTreesInternal.h"
109 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
111 extern void hfs_converterinit(void);
113 extern void inittodr( time_t base
);
116 static int hfs_changefs
__P((struct mount
*mp
, struct hfs_mount_args
*args
,
118 static int hfs_reload
__P((struct mount
*mp
, struct ucred
*cred
, struct proc
*p
));
120 static int hfs_mountfs
__P((struct vnode
*devvp
, struct mount
*mp
, struct proc
*p
,
121 struct hfs_mount_args
*args
));
122 static int hfs_statfs
__P((struct mount
*mp
, register struct statfs
*sbp
,
124 static int hfs_flushfiles
__P((struct mount
*, int, struct proc
*));
126 static int hfs_extendfs
__P((struct mount
*, u_int64_t
, struct proc
*));
129 * Called by vfs_mountroot when mounting HFS Plus as root.
135 extern struct vnode
*rootvp
;
137 struct proc
*p
= current_proc(); /* XXX */
138 struct hfsmount
*hfsmp
;
143 * Get vnode for rootdev.
145 if ((error
= bdevvp(rootdev
, &rootvp
))) {
146 printf("hfs_mountroot: can't setup bdevvp");
149 if ((error
= vfs_rootmountalloc("hfs", "root_device", &mp
))) {
150 vrele(rootvp
); /* release the reference from bdevvp() */
153 if ((error
= hfs_mountfs(rootvp
, mp
, p
, NULL
))) {
154 mp
->mnt_vfc
->vfc_refcount
--;
156 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
157 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
160 vrele(rootvp
); /* release the reference from bdevvp() */
161 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
164 simple_lock(&mountlist_slock
);
165 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
166 simple_unlock(&mountlist_slock
);
169 hfsmp
= VFSTOHFS(mp
);
171 hfsmp
->hfs_uid
= UNKNOWNUID
;
172 hfsmp
->hfs_gid
= UNKNOWNGID
;
173 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
174 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
176 /* Establish the free block reserve. */
177 vcb
= HFSTOVCB(hfsmp
);
178 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
179 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
181 (void)hfs_statfs(mp
, &mp
->mnt_stat
, p
);
184 inittodr(HFSTOVCB(hfsmp
)->vcbLsMod
);
196 hfs_mount(mp
, path
, data
, ndp
, p
)
197 register struct mount
*mp
;
200 struct nameidata
*ndp
;
203 struct hfsmount
*hfsmp
= NULL
;
205 struct hfs_mount_args args
;
211 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
))))
215 * If updating, check whether changing from read-only to
216 * read/write; if there is no device name, that's all we do.
218 if (mp
->mnt_flag
& MNT_UPDATE
) {
220 hfsmp
= VFSTOHFS(mp
);
221 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
222 (mp
->mnt_flag
& MNT_RDONLY
)) {
224 /* use VFS_SYNC to push out System (btree) files */
225 retval
= VFS_SYNC(mp
, MNT_WAIT
, p
->p_ucred
, p
);
226 if (retval
&& ((mp
->mnt_flag
& MNT_FORCE
) == 0))
230 if (mp
->mnt_flag
& MNT_FORCE
)
233 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
235 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
236 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
238 /* also get the volume bitmap blocks */
240 retval
= VOP_FSYNC(hfsmp
->hfs_devvp
, NOCRED
, MNT_WAIT
, p
);
243 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
248 hfs_global_exclusive_lock_acquire(hfsmp
);
250 journal_close(hfsmp
->jnl
);
253 // Note: we explicitly don't want to shutdown
254 // access to the jvp because we may need
255 // it later if we go back to being read-write.
257 hfs_global_exclusive_lock_release(hfsmp
);
261 if ((mp
->mnt_flag
& MNT_RELOAD
) &&
262 (retval
= hfs_reload(mp
, ndp
->ni_cnd
.cn_cred
, p
)))
265 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) &&
266 (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
268 * If upgrade to read-write by non-root, then verify
269 * that user has necessary permissions on the device.
271 if (p
->p_ucred
->cr_uid
!= 0) {
272 devvp
= hfsmp
->hfs_devvp
;
273 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
274 if ((retval
= VOP_ACCESS(devvp
, VREAD
| VWRITE
, p
->p_ucred
, p
))) {
275 VOP_UNLOCK(devvp
, 0, p
);
278 VOP_UNLOCK(devvp
, 0, p
);
280 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
282 if (retval
!= E_NONE
)
285 // If the journal was shut-down previously because we were
286 // asked to be read-only, let's start it back up again now
288 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
289 && hfsmp
->jnl
== NULL
290 && hfsmp
->jvp
!= NULL
) {
293 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
294 flags
= JOURNAL_RESET
;
299 hfs_global_exclusive_lock_acquire(hfsmp
);
301 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
302 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
305 hfsmp
->hfs_phys_block_size
,
308 hfs_sync_metadata
, hfsmp
->hfs_mp
);
310 hfs_global_exclusive_lock_release(hfsmp
);
312 if (hfsmp
->jnl
== NULL
) {
316 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
321 /* Only clear HFS_READ_ONLY after a successfull write */
322 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
325 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
326 (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)) {
327 /* setup private/hidden directory for unlinked files */
328 FindMetaDataDirectory(HFSTOVCB(hfsmp
));
330 hfs_remove_orphans(hfsmp
);
333 * Allow hot file clustering if conditions allow.
335 if ((hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) &&
336 (mp
->mnt_flag
& MNT_RDONLY
) &&
337 (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
338 (void) hfs_recording_init(hfsmp
, p
);
342 if (args
.fspec
== 0) {
344 * Process export requests.
346 return vfs_export(mp
, &hfsmp
->hfs_export
, &args
.export
);
351 * Not an update, or updating the name: look up the name
352 * and verify that it refers to a sensible block device.
354 NDINIT(ndp
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, args
.fspec
, p
);
356 if (retval
!= E_NONE
) {
357 DBG_ERR(("hfs_mount: CAN'T GET DEVICE: %s, %x\n", args
.fspec
, ndp
->ni_vp
->v_rdev
));
363 if (devvp
->v_type
!= VBLK
) {
368 if (major(devvp
->v_rdev
) >= nblkdev
) {
375 * If mount by non-root, then verify that user has necessary
376 * permissions on the device.
378 if (p
->p_ucred
->cr_uid
!= 0) {
380 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
381 accessmode
|= VWRITE
;
382 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
383 if ((retval
= VOP_ACCESS(devvp
, accessmode
, p
->p_ucred
, p
))) {
387 VOP_UNLOCK(devvp
, 0, p
);
390 if ((mp
->mnt_flag
& MNT_UPDATE
) == 0) {
391 retval
= hfs_mountfs(devvp
, mp
, p
, &args
);
392 if (retval
!= E_NONE
)
395 if (devvp
!= hfsmp
->hfs_devvp
)
396 retval
= EINVAL
; /* needs translation */
398 retval
= hfs_changefs(mp
, &args
, p
);
402 if (retval
!= E_NONE
) {
406 /* Set the mount flag to indicate that we support volfs */
407 mp
->mnt_flag
|= MNT_DOVOLFS
;
408 if (VFSTOVCB(mp
)->vcbSigWord
== kHFSSigWord
) {
409 /* HFS volumes only want roman-encoded names: */
410 mp
->mnt_flag
|= MNT_FIXEDSCRIPTENCODING
;
412 (void) copyinstr(path
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
-1, &size
);
414 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
415 (void) copyinstr(args
.fspec
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, &size
);
416 bzero(mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
417 (void)hfs_statfs(mp
, &mp
->mnt_stat
, p
);
426 /* Change fs mount parameters */
428 hfs_changefs(mp
, args
, p
)
430 struct hfs_mount_args
*args
;
434 int namefix
, permfix
, permswitch
;
435 struct hfsmount
*hfsmp
;
438 register struct vnode
*vp
, *nvp
;
439 hfs_to_unicode_func_t get_unicode_func
;
440 unicode_to_hfs_func_t get_hfsname_func
;
441 struct cat_desc cndesc
;
442 struct cat_attr cnattr
;
445 hfsmp
= VFSTOHFS(mp
);
446 vcb
= HFSTOVCB(hfsmp
);
447 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
448 ((mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
449 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
450 (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)));
452 /* The root filesystem must operate with actual permissions: */
453 if (permswitch
&& (mp
->mnt_flag
& MNT_ROOTFS
) && (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)) {
454 mp
->mnt_flag
&= ~MNT_UNKNOWNPERMISSIONS
; /* Just say "No". */
457 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)
458 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
460 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
462 namefix
= permfix
= 0;
464 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
465 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
466 gTimeZone
= args
->hfs_timezone
;
469 /* Change the default uid, gid and/or mask */
470 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
471 hfsmp
->hfs_uid
= args
->hfs_uid
;
472 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
475 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
476 hfsmp
->hfs_gid
= args
->hfs_gid
;
477 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
480 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
481 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
482 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
483 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
484 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
485 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
486 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
491 /* Change the hfs encoding value (hfs only) */
492 if ((HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) &&
493 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
494 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
496 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
501 * Connect the new hfs_get_unicode converter but leave
502 * the old hfs_get_hfsname converter in place so that
503 * we can lookup existing vnodes to get their correctly
506 * When we're all finished, we can then connect the new
507 * hfs_get_hfsname converter and release our interest
508 * in the old converters.
510 hfsmp
->hfs_get_unicode
= get_unicode_func
;
511 old_encoding
= hfsmp
->hfs_encoding
;
512 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
516 if (!(namefix
|| permfix
|| permswitch
))
520 * For each active vnode fix things that changed
522 * Note that we can visit a vnode more than once
523 * and we can race with fsync.
525 simple_lock(&mntvnode_slock
);
527 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
529 * If the vnode that we are about to fix is no longer
530 * associated with this mount point, start over.
532 if (vp
->v_mount
!= mp
)
535 simple_lock(&vp
->v_interlock
);
536 nvp
= vp
->v_mntvnodes
.le_next
;
537 if (vp
->v_flag
& VSYSTEM
) {
538 simple_unlock(&vp
->v_interlock
);
541 simple_unlock(&mntvnode_slock
);
542 retval
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
544 simple_lock(&mntvnode_slock
);
545 if (retval
== ENOENT
)
552 retval
= cat_lookup(hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
);
553 /* If we couldn't find this guy skip to the next one */
558 simple_lock(&mntvnode_slock
);
562 /* Get the real uid/gid and perm mask from disk. */
563 if (permswitch
|| permfix
) {
564 cp
->c_uid
= cnattr
.ca_uid
;
565 cp
->c_gid
= cnattr
.ca_gid
;
566 cp
->c_mode
= cnattr
.ca_mode
;
570 * If we're switching name converters then...
571 * Remove the existing entry from the namei cache.
572 * Update name to one based on new encoder.
576 replace_desc(cp
, &cndesc
);
578 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
579 strncpy(vcb
->vcbVN
, cp
->c_desc
.cd_nameptr
, NAME_MAX
);
580 cp
->c_desc
.cd_encoding
= hfsmp
->hfs_encoding
;
583 cat_releasedesc(&cndesc
);
586 simple_lock(&mntvnode_slock
);
588 } /* end for (vp...) */
589 simple_unlock(&mntvnode_slock
);
591 * If we're switching name converters we can now
592 * connect the new hfs_get_hfsname converter and
593 * release our interest in the old converters.
596 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
597 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
598 (void) hfs_relconverter(old_encoding
);
606 * Reload all incore data for a filesystem (used after running fsck on
607 * the root filesystem and finding things to fix). The filesystem must
608 * be mounted read-only.
610 * Things to do to update the mount:
611 * invalidate all cached meta-data.
612 * invalidate all inactive vnodes.
613 * invalidate all cached file data.
614 * re-read volume header from disk.
615 * re-load meta-file info (extents, file size).
616 * re-load B-tree header data.
617 * re-read cnode data for all active vnodes.
620 hfs_reload(mountp
, cred
, p
)
621 register struct mount
*mountp
;
625 register struct vnode
*vp
, *nvp
, *devvp
;
630 struct hfsmount
*hfsmp
;
631 struct HFSPlusVolumeHeader
*vhp
;
633 struct filefork
*forkp
;
634 struct cat_desc cndesc
;
636 if ((mountp
->mnt_flag
& MNT_RDONLY
) == 0)
639 hfsmp
= VFSTOHFS(mountp
);
640 vcb
= HFSTOVCB(hfsmp
);
642 if (vcb
->vcbSigWord
== kHFSSigWord
)
643 return (EINVAL
); /* rooting from HFS is not supported! */
646 * Invalidate all cached meta-data.
648 devvp
= hfsmp
->hfs_devvp
;
649 if (vinvalbuf(devvp
, 0, cred
, p
, 0, 0))
650 panic("hfs_reload: dirty1");
651 InvalidateCatalogCache(vcb
);
654 simple_lock(&mntvnode_slock
);
655 for (vp
= mountp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
656 if (vp
->v_mount
!= mountp
) {
657 simple_unlock(&mntvnode_slock
);
660 nvp
= vp
->v_mntvnodes
.le_next
;
663 * Invalidate all inactive vnodes.
665 if (vrecycle(vp
, &mntvnode_slock
, p
))
669 * Invalidate all cached file data.
671 simple_lock(&vp
->v_interlock
);
672 simple_unlock(&mntvnode_slock
);
673 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, p
)) {
676 if (vinvalbuf(vp
, 0, cred
, p
, 0, 0))
677 panic("hfs_reload: dirty2");
680 * Re-read cnode data for all active vnodes (non-metadata files).
683 if ((vp
->v_flag
& VSYSTEM
) == 0 && !VNODE_IS_RSRC(vp
)) {
684 struct cat_fork
*datafork
;
685 struct cat_desc desc
;
687 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
689 /* lookup by fileID since name could have changed */
690 if ((error
= cat_idlookup(hfsmp
, cp
->c_fileid
, &desc
, &cp
->c_attr
, datafork
))) {
695 /* update cnode's catalog descriptor */
696 (void) replace_desc(cp
, &desc
);
699 simple_lock(&mntvnode_slock
);
701 simple_unlock(&mntvnode_slock
);
704 * Re-read VolumeHeader from disk.
706 sectorsize
= hfsmp
->hfs_phys_block_size
;
708 error
= meta_bread(hfsmp
->hfs_devvp
,
709 (vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
),
710 sectorsize
, NOCRED
, &bp
);
717 vhp
= (HFSPlusVolumeHeader
*) (bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
719 /* Do a quick sanity check */
720 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
721 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
722 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
723 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
724 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
729 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
730 vcb
->vcbAtrb
= (UInt16
) SWAP_BE32 (vhp
->attributes
); /* VCB only uses lower 16 bits */
731 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
732 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
733 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
734 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
735 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
736 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
737 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
738 vcb
->nextAllocation
= SWAP_BE32 (vhp
->nextAllocation
);
739 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
740 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
741 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
742 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
743 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
746 * Re-load meta-file vnode data (extent info, file size, etc).
748 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
749 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
750 forkp
->ff_extents
[i
].startBlock
=
751 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
752 forkp
->ff_extents
[i
].blockCount
=
753 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
755 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
756 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
757 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
760 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
761 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
762 forkp
->ff_extents
[i
].startBlock
=
763 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
764 forkp
->ff_extents
[i
].blockCount
=
765 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
767 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
768 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
769 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
772 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
773 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
774 forkp
->ff_extents
[i
].startBlock
=
775 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
776 forkp
->ff_extents
[i
].blockCount
=
777 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
779 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
780 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
781 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
787 * Re-load B-tree header data
789 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
790 if (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) ))
793 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
794 if (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) ))
797 /* Reload the volume name */
798 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, NULL
, NULL
)))
800 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
801 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
802 cat_releasedesc(&cndesc
);
804 /* Re-establish private/hidden directory for unlinked files */
805 FindMetaDataDirectory(vcb
);
807 /* In case any volume information changed to trigger a notification */
808 hfs_generate_volume_notifications(hfsmp
);
815 get_raw_device(char *fspec
, int is_user
, int ronly
, struct vnode
**rvp
, struct ucred
*cred
, struct proc
*p
)
825 MALLOC(rawbuf
, char *, MAXPATHLEN
, M_HFSMNT
, M_WAITOK
);
826 if (rawbuf
== NULL
) {
832 retval
= copyinstr(fspec
, rawbuf
, MAXPATHLEN
- 1, &namelen
);
833 if (retval
!= E_NONE
) {
834 FREE(rawbuf
, M_HFSMNT
);
838 strcpy(rawbuf
, fspec
);
839 namelen
= strlen(rawbuf
);
842 /* make sure it's null terminated */
843 rawbuf
[MAXPATHLEN
-1] = '\0';
845 dp
= &rawbuf
[namelen
-1];
846 while(dp
>= rawbuf
&& *dp
!= '/') {
856 /* make room for and insert the 'r' for the raw device */
857 memmove(dp
+1, dp
, strlen(dp
)+1);
860 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, rawbuf
, p
);
862 if (retval
!= E_NONE
) {
863 DBG_ERR(("hfs_mountfs: can't open raw device for journal: %s, %x\n", rawbuf
, nd
.ni_vp
->v_rdev
));
864 FREE(rawbuf
, M_HFSMNT
);
869 if ((retval
= VOP_OPEN(*rvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
))) {
874 // don't need this any more
875 FREE(rawbuf
, M_HFSMNT
);
881 (void)VOP_CLOSE(*rvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
885 FREE(rawbuf
, M_HFSMNT
);
893 * Common code for mount and mountroot
896 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct proc
*p
,
897 struct hfs_mount_args
*args
)
900 struct hfsmount
*hfsmp
;
903 HFSMasterDirectoryBlock
*mdbp
;
911 u_int32_t minblksize
;
912 u_int32_t iswritable
;
916 cred
= p
? p
->p_ucred
: NOCRED
;
919 * Disallow multiple mounts of the same device.
920 * Disallow mounting of a device that is currently in use
921 * (except for root, which might share swap device for miniroot).
922 * Flush out any old buffers remaining from a previous use.
924 if ((retval
= vfs_mountedon(devvp
)))
926 if ((vcount(devvp
) > 1) && (devvp
!= rootvp
))
928 if ((retval
= vinvalbuf(devvp
, V_SAVE
, cred
, p
, 0, 0)))
931 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
932 if ((retval
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
)))
938 minblksize
= kHFSBlockSize
;
940 /* Get the real physical block size. */
941 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, cred
, p
)) {
945 /* Switch to 512 byte sectors (temporarily) */
947 u_int32_t size512
= 512;
949 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, cred
, p
)) {
954 /* Get the number of 512 byte physical blocks. */
955 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
959 /* Compute an accurate disk size (i.e. within 512 bytes) */
960 disksize
= blkcnt
* (u_int64_t
)512;
963 * There are only 31 bits worth of block count in
964 * the buffer cache. So for large volumes a 4K
965 * physical block size is needed.
967 if (blkcnt
> (u_int64_t
)0x000000007fffffff) {
968 minblksize
= blksize
= 4096;
970 /* Now switch to our prefered physical block size. */
972 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
976 /* Get the count of physical blocks. */
977 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
985 * minblksize is the minimum physical block size
986 * blksize has our prefered physical block size
987 * blkcnt has the total number of physical blocks
989 devvp
->v_specsize
= blksize
;
991 /* cache the IO attributes */
992 if ((retval
= vfs_init_io_attributes(devvp
, mp
))) {
993 printf("hfs_mountfs: vfs_init_io_attributes returned %d\n",
998 mdb_offset
= HFS_PRI_SECTOR(blksize
);
999 if ((retval
= meta_bread(devvp
, HFS_PRI_SECTOR(blksize
), blksize
, cred
, &bp
))) {
1002 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
1003 bcopy(bp
->b_data
+ HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
1007 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
1008 bzero(hfsmp
, sizeof(struct hfsmount
));
1011 * Init the volume information structure
1013 mp
->mnt_data
= (qaddr_t
)hfsmp
;
1014 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
1015 hfsmp
->hfs_vcb
.vcb_hfsmp
= hfsmp
; /* Make VCBTOHFS work */
1016 hfsmp
->hfs_raw_dev
= devvp
->v_rdev
;
1017 hfsmp
->hfs_devvp
= devvp
;
1018 hfsmp
->hfs_phys_block_size
= blksize
;
1019 hfsmp
->hfs_phys_block_count
= blkcnt
;
1020 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
1022 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
1023 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)
1024 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
1025 for (i
= 0; i
< MAXQUOTAS
; i
++)
1026 hfsmp
->hfs_qfiles
[i
].qf_vp
= NULLVP
;
1029 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
1030 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
1031 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
1032 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
1033 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
1034 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
1035 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
1036 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
1038 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
1041 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
1042 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
1044 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
1047 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
1048 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) {
1049 hfsmp
->hfs_uid
= UNKNOWNUID
;
1050 hfsmp
->hfs_gid
= UNKNOWNGID
;
1051 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
1052 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
1056 /* Find out if disk media is writable. */
1057 if (VOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, cred
, p
) == 0) {
1059 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
1061 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
1064 /* Mount a standard HFS disk */
1065 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
1066 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
1067 if (devvp
== rootvp
) {
1068 retval
= EINVAL
; /* Cannot root from HFS standard disks */
1071 /* HFS disks can only use 512 byte physical blocks */
1072 if (blksize
> kHFSBlockSize
) {
1073 blksize
= kHFSBlockSize
;
1074 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1078 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1082 devvp
->v_specsize
= blksize
;
1083 hfsmp
->hfs_phys_block_size
= blksize
;
1084 hfsmp
->hfs_phys_block_count
= blkcnt
;
1087 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
1088 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
1090 /* establish the timezone */
1091 gTimeZone
= args
->hfs_timezone
;
1094 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
1095 &hfsmp
->hfs_get_hfsname
);
1099 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
1101 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1103 } else /* Mount an HFS Plus disk */ {
1104 HFSPlusVolumeHeader
*vhp
;
1105 off_t embeddedOffset
;
1106 int jnl_disable
= 0;
1108 /* Get the embedded Volume Header */
1109 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1110 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
1111 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1112 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1115 * If the embedded volume doesn't start on a block
1116 * boundary, then switch the device to a 512-byte
1117 * block size so everything will line up on a block
1120 if ((embeddedOffset
% blksize
) != 0) {
1121 printf("HFS Mount: embedded volume offset not"
1122 " a multiple of physical block size (%d);"
1123 " switching to 512\n", blksize
);
1125 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
1126 (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1130 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
1131 (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1135 /* XXX do we need to call vfs_init_io_attributes again? */
1136 devvp
->v_specsize
= blksize
;
1137 /* Note: relative block count adjustment */
1138 hfsmp
->hfs_phys_block_count
*=
1139 hfsmp
->hfs_phys_block_size
/ blksize
;
1140 hfsmp
->hfs_phys_block_size
= blksize
;
1143 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1144 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1146 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1148 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1149 retval
= meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1152 bcopy(bp
->b_data
+ HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1155 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1157 } else /* pure HFS+ */ {
1159 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1166 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) && args
->journal_disable
) {
1171 // We only initialize the journal here if the last person
1172 // to mount this volume was journaling aware. Otherwise
1173 // we delay journal initialization until later at the end
1174 // of hfs_MountHFSPlusVolume() because the last person who
1175 // mounted it could have messed things up behind our back
1176 // (so we need to go find the .journal file, make sure it's
1177 // the right size, re-sync up if it was moved, etc).
1179 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1180 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1183 // if we're able to init the journal, mark the mount
1184 // point as journaled.
1186 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1187 mp
->mnt_flag
|= MNT_JOURNALED
;
1189 // if the journal failed to open, then set the lastMountedVersion
1190 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1191 // of just bailing out because the volume is journaled.
1192 if (ronly
!= 0 || devvp
== rootvp
) {
1193 HFSPlusVolumeHeader
*vhp
;
1195 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1197 if (mdb_offset
== 0) {
1198 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1202 retval
= meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1204 vhp
= (HFSPlusVolumeHeader
*)(bp
->b_data
+ HFS_PRI_OFFSET(blksize
));
1206 if (SWAP_BE16(vhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(vhp
->signature
) == kHFSXSigWord
) {
1207 vhp
->lastMountedVersion
= SWAP_BE32('FSK!');
1218 // if this isn't the root device just bail out.
1219 // if it is the root device we just continue on
1220 // in the hopes that fsck_hfs will be able to
1221 // fix any damage that exists on the volume.
1222 if (devvp
!= rootvp
) {
1230 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1232 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
);
1234 * If the backend didn't like our physical blocksize
1235 * then retry with physical blocksize of 512.
1237 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1238 printf("HFS Mount: could not use physical block size "
1239 "(%d) switching to 512\n", blksize
);
1241 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1245 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1249 devvp
->v_specsize
= blksize
;
1250 /* Note: relative block count adjustment (in case this is an embedded volume). */
1251 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1252 hfsmp
->hfs_phys_block_size
= blksize
;
1255 // close and re-open this with the new block size
1256 journal_close(hfsmp
->jnl
);
1258 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1259 mp
->mnt_flag
|= MNT_JOURNALED
;
1263 /* Try again with a smaller block size... */
1264 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
);
1267 (void) hfs_relconverter(0);
1274 mp
->mnt_stat
.f_fsid
.val
[0] = (long)dev
;
1275 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_vfc
->vfc_typenum
;
1276 mp
->mnt_maxsymlinklen
= 0;
1277 devvp
->v_specflags
|= SI_MOUNTEDON
;
1281 * Set the free space warning levels for a non-root volume:
1283 * Set the lower freespace limit (the level that will trigger a warning)
1284 * to 5% of the volume size or 250MB, whichever is less, and the desired
1285 * level (which will cancel the alert request) to 1/2 above that limit.
1286 * Start looking for free space to drop below this level and generate a
1287 * warning immediately if needed:
1289 hfsmp
->hfs_freespace_notify_warninglimit
=
1290 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1291 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1292 hfsmp
->hfs_freespace_notify_desiredlevel
=
1293 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1294 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1297 * Set the free space warning levels for the root volume:
1299 * Set the lower freespace limit (the level that will trigger a warning)
1300 * to 1% of the volume size or 50MB, whichever is less, and the desired
1301 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1303 hfsmp
->hfs_freespace_notify_warninglimit
=
1304 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1305 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1306 hfsmp
->hfs_freespace_notify_desiredlevel
=
1307 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1308 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1312 * Start looking for free space to drop below this level and generate a
1313 * warning immediately if needed:
1315 hfsmp
->hfs_notification_conditions
= 0;
1316 hfs_generate_volume_notifications(hfsmp
);
1319 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1329 (void)VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
1330 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1331 (void)VOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
1335 FREE(hfsmp
, M_HFSMNT
);
1336 mp
->mnt_data
= (qaddr_t
)0;
1343 * Make a filesystem operational.
1344 * Nothing to do at the moment.
1348 hfs_start(mp
, flags
, p
)
1358 * unmount system call
1361 hfs_unmount(mp
, mntflags
, p
)
1366 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1367 int retval
= E_NONE
;
1370 int started_tr
= 0, grabbed_lock
= 0;
1374 if (mntflags
& MNT_FORCE
) {
1375 flags
|= FORCECLOSE
;
1379 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1382 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1383 (void) hfs_recording_suspend(hfsmp
, p
);
1386 * Flush out the b-trees, volume bitmap and Volume Header
1388 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1389 hfs_global_shared_lock_acquire(hfsmp
);
1392 journal_start_transaction(hfsmp
->jnl
);
1396 retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->catalogRefNum
, NOCRED
, MNT_WAIT
, p
);
1397 if (retval
&& !force
)
1400 retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->extentsRefNum
, NOCRED
, MNT_WAIT
, p
);
1401 if (retval
&& !force
)
1404 // if we have an allocation file, sync it too so we don't leave dirty
1406 if (HFSTOVCB(hfsmp
)->allocationsRefNum
) {
1407 if (retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->allocationsRefNum
, NOCRED
, MNT_WAIT
, p
)) {
1413 if (hfsmp
->hfc_filevp
&& (hfsmp
->hfc_filevp
->v_flag
& VSYSTEM
)) {
1414 retval
= VOP_FSYNC(hfsmp
->hfc_filevp
, NOCRED
, MNT_WAIT
, p
);
1415 if (retval
&& !force
)
1419 if (retval
= VOP_FSYNC(hfsmp
->hfs_devvp
, NOCRED
, MNT_WAIT
, p
)) {
1425 /* See if this volume is damaged, is so do not unmount cleanly */
1426 if (HFSTOVCB(hfsmp
)->vcbFlags
& kHFS_DamagedVolume
) {
1427 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1429 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1432 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1434 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
1436 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1438 goto err_exit
; /* could not flush everything */
1442 journal_end_transaction(hfsmp
->jnl
);
1446 hfs_global_shared_lock_release(hfsmp
);
1452 journal_flush(hfsmp
->jnl
);
1456 * Invalidate our caches and release metadata vnodes
1458 (void) hfsUnmount(hfsmp
, p
);
1461 * Last chance to dump unreferenced system files.
1463 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1465 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1466 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1470 journal_close(hfsmp
->jnl
);
1474 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1475 retval
= VOP_CLOSE(hfsmp
->jvp
,
1476 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1483 #ifdef HFS_SPARSE_DEV
1484 /* Drop our reference on the backing fs (if any). */
1485 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1486 struct vnode
* tmpvp
;
1488 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1489 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1490 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1493 #endif /* HFS_SPARSE_DEV */
1495 hfsmp
->hfs_devvp
->v_specflags
&= ~SI_MOUNTEDON
;
1496 retval
= VOP_CLOSE(hfsmp
->hfs_devvp
,
1497 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1499 if (retval
&& !force
)
1502 vrele(hfsmp
->hfs_devvp
);
1503 FREE(hfsmp
, M_HFSMNT
);
1504 mp
->mnt_data
= (qaddr_t
)0;
1508 if (hfsmp
->jnl
&& started_tr
) {
1509 journal_end_transaction(hfsmp
->jnl
);
1512 hfs_global_shared_lock_release(hfsmp
);
1519 * Return the root of a filesystem.
1521 * OUT - vpp, should be locked and vget()'d (to increment usecount and lock)
1530 UInt32 rootObjID
= kRootDirID
;
1532 if ((retval
= VFS_VGET(mp
, &rootObjID
, &nvp
)))
1541 * Do operations associated with quotas
1544 hfs_quotactl(mp
, cmds
, uid
, arg
, p
)
1551 int cmd
, type
, error
;
1554 return (EOPNOTSUPP
);
1557 uid
= p
->p_cred
->p_ruid
;
1558 cmd
= cmds
>> SUBCMDSHIFT
;
1565 if (uid
== p
->p_cred
->p_ruid
)
1569 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
1573 type
= cmds
& SUBCMDMASK
;
1574 if ((u_int
)type
>= MAXQUOTAS
)
1576 if (vfs_busy(mp
, LK_NOWAIT
, 0, p
))
1582 error
= hfs_quotaon(p
, mp
, type
, arg
, UIO_USERSPACE
);
1586 error
= hfs_quotaoff(p
, mp
, type
);
1590 error
= hfs_setquota(mp
, uid
, type
, arg
);
1594 error
= hfs_setuse(mp
, uid
, type
, arg
);
1598 error
= hfs_getquota(mp
, uid
, type
, arg
);
1602 error
= hfs_qsync(mp
);
1606 error
= hfs_quotastat(mp
, type
, arg
);
1622 * Get file system statistics.
1625 hfs_statfs(mp
, sbp
, p
)
1627 register struct statfs
*sbp
;
1630 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1631 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1634 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1636 sbp
->f_bsize
= vcb
->blockSize
;
1637 sbp
->f_iosize
= hfsmp
->hfs_logBlockSize
;
1638 sbp
->f_blocks
= vcb
->totalBlocks
;
1639 sbp
->f_bfree
= hfs_freeblks(hfsmp
, 0);
1640 sbp
->f_bavail
= hfs_freeblks(hfsmp
, 1);
1641 sbp
->f_files
= vcb
->totalBlocks
- 2; /* max files is constrained by total blocks */
1642 sbp
->f_ffree
= MIN(freeCNIDs
, sbp
->f_bavail
);
1645 if (sbp
!= &mp
->mnt_stat
) {
1646 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
1647 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
1648 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
1649 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
1650 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
1657 // XXXdbg -- this is a callback to be used by the journal to
1658 // get meta data blocks flushed out to disk.
1660 // XXXdbg -- be smarter and don't flush *every* block on each
1661 // call. try to only flush some so we don't wind up
1662 // being too synchronous.
1666 hfs_sync_metadata(void *arg
)
1668 struct mount
*mp
= (struct mount
*)arg
;
1670 struct hfsmount
*hfsmp
;
1672 struct vnode
*meta_vp
[3];
1674 int i
, sectorsize
, priIDSector
, altIDSector
, retval
;
1675 int error
, allerror
= 0;
1677 hfsmp
= VFSTOHFS(mp
);
1678 vcb
= HFSTOVCB(hfsmp
);
1680 bflushq(BQ_META
, mp
);
1683 #if 1 // XXXdbg - I do not believe this is necessary...
1684 // but if I pull it out, then the journal
1685 // does not seem to get flushed properly
1686 // when it is closed....
1688 // now make sure the super block is flushed
1689 sectorsize
= hfsmp
->hfs_phys_block_size
;
1690 priIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1691 HFS_PRI_SECTOR(sectorsize
);
1692 retval
= meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1694 panic("hfs: sync_metadata: can't read super-block?! (retval 0x%x, priIDSector)\n",
1695 retval
, priIDSector
);
1698 if (retval
== 0 && (bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1704 // the alternate super block...
1705 // XXXdbg - we probably don't need to do this each and every time.
1706 // hfs_btreeio.c:FlushAlternate() should flag when it was
1708 altIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1709 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
1710 retval
= meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &bp
);
1711 if (retval
== 0 && (bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1721 * Go through the disk queues to initiate sandbagged IO;
1722 * go through the inodes to write those that have been modified;
1723 * initiate the writing of the super block if it has been modified.
1725 * Note: we are always called with the filesystem marked `MPBUSY'.
1728 hfs_sync(mp
, waitfor
, cred
, p
)
1734 struct vnode
*nvp
, *vp
;
1736 struct hfsmount
*hfsmp
;
1738 struct vnode
*meta_vp
[3];
1740 int error
, allerror
= 0;
1743 * During MNT_UPDATE hfs_changefs might be manipulating
1744 * vnodes so back off
1746 if (mp
->mnt_flag
& MNT_UPDATE
)
1749 hfsmp
= VFSTOHFS(mp
);
1750 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1754 // XXXdbg first go through and flush out any modified
1755 // meta data blocks so they go out in order...
1756 bflushq(BQ_META
, mp
);
1757 bflushq(BQ_LRU
, mp
);
1758 // only flush locked blocks if we're not doing journaling
1759 if (hfsmp
->jnl
== NULL
) {
1760 bflushq(BQ_LOCKED
, mp
);
1765 * Write back each 'modified' vnode
1769 simple_lock(&mntvnode_slock
);
1770 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
1773 * If the vnode that we are about to sync is no longer
1774 * associated with this mount point, start over.
1776 if (vp
->v_mount
!= mp
) {
1777 simple_unlock(&mntvnode_slock
);
1781 simple_lock(&vp
->v_interlock
);
1782 nvp
= vp
->v_mntvnodes
.le_next
;
1786 // restart our whole search if this guy is locked
1787 // or being reclaimed.
1788 if (vp
->v_tag
!= VT_HFS
|| cp
== NULL
|| vp
->v_flag
& (VXLOCK
|VORECLAIM
)) {
1789 simple_unlock(&vp
->v_interlock
);
1793 if ((vp
->v_flag
& VSYSTEM
) || (vp
->v_type
== VNON
) ||
1794 (((cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) == 0) &&
1795 (vp
->v_dirtyblkhd
.lh_first
== NULL
) && !(vp
->v_flag
& VHASDIRTY
))) {
1796 simple_unlock(&vp
->v_interlock
);
1797 simple_unlock(&mntvnode_slock
);
1798 simple_lock(&mntvnode_slock
);
1802 simple_unlock(&mntvnode_slock
);
1803 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
1805 if (error
== ENOENT
) {
1807 * If vnode is being reclaimed, yield so
1808 * that it can be removed from our list.
1811 (void) tsleep((caddr_t
)&lbolt
, PINOD
, "hfs_sync", 0);
1814 simple_lock(&mntvnode_slock
);
1818 didhold
= ubc_hold(vp
);
1820 // mark the cnode so that fsync won't flush
1821 // the journal since we're going to do that...
1822 cp
->c_flag
|= C_FROMSYNC
;
1823 if ((error
= VOP_FSYNC(vp
, cred
, waitfor
, p
))) {
1826 cp
->c_flag
&= ~C_FROMSYNC
;
1828 VOP_UNLOCK(vp
, 0, p
);
1832 simple_lock(&mntvnode_slock
);
1835 vcb
= HFSTOVCB(hfsmp
);
1837 meta_vp
[0] = vcb
->extentsRefNum
;
1838 meta_vp
[1] = vcb
->catalogRefNum
;
1839 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1841 /* Now sync our three metadata files */
1842 for (i
= 0; i
< 3; ++i
) {
1845 btvp
= btvp
= meta_vp
[i
];;
1846 if ((btvp
==0) || (btvp
->v_type
== VNON
) || (btvp
->v_mount
!= mp
))
1849 simple_lock(&btvp
->v_interlock
);
1851 if (((cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) == 0) &&
1852 (btvp
->v_dirtyblkhd
.lh_first
== NULL
) && !(btvp
->v_flag
& VHASDIRTY
)) {
1853 simple_unlock(&btvp
->v_interlock
);
1856 simple_unlock(&mntvnode_slock
);
1857 error
= vget(btvp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
1859 simple_lock(&mntvnode_slock
);
1862 if ((error
= VOP_FSYNC(btvp
, cred
, waitfor
, p
)))
1864 VOP_UNLOCK(btvp
, 0, p
);
1866 simple_lock(&mntvnode_slock
);
1869 simple_unlock(&mntvnode_slock
);
1872 * Force stale file system control information to be flushed.
1874 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1875 if ((error
= VOP_FSYNC(hfsmp
->hfs_devvp
, cred
, waitfor
, p
)))
1882 hfs_hotfilesync(hfsmp
, p
);
1884 * Write back modified superblock.
1887 if (IsVCBDirty(vcb
)) {
1888 // XXXdbg - debugging, remove
1890 //printf("hfs: sync: strange, a journaled volume w/dirty VCB? jnl 0x%x hfsmp 0x%x\n",
1891 // hfsmp->jnl, hfsmp);
1894 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1900 journal_flush(hfsmp
->jnl
);
1909 * File handle to vnode
1911 * Have to be really careful about stale file handles:
1912 * - check that the cnode id is valid
1913 * - call hfs_vget() to get the locked cnode
1914 * - check for an unallocated cnode (i_mode == 0)
1915 * - check that the given client host has export rights and return
1916 * those rights via. exflagsp and credanonp
1919 hfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1920 register struct mount
*mp
;
1925 struct ucred
**credanonp
;
1927 struct hfsfid
*hfsfhp
;
1933 hfsfhp
= (struct hfsfid
*)fhp
;
1936 * Get the export permission structure for this <mp, client> tuple.
1938 np
= vfs_export_lookup(mp
, &VFSTOHFS(mp
)->hfs_export
, nam
);
1939 if (nam
&& (np
== NULL
)) {
1943 result
= VFS_VGET(mp
, &hfsfhp
->hfsfid_cnid
, &nvp
);
1944 if (result
) return result
;
1945 if (nvp
== NULL
) return ESTALE
;
1947 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1948 * For NFS, we are assuming that only if the createtime was moved
1949 * forward would it mean the fileID got reused in that session by
1950 * wrapping. We don't have a volume ID or other unique identifier to
1951 * to use here for a generation ID across reboots, crashes where
1952 * metadata noting lastFileID didn't make it to disk but client has
1953 * it, or volume erasures where fileIDs start over again. Lastly,
1954 * with HFS allowing "wraps" of fileIDs now, this becomes more
1955 * error prone. Future, would be change the "wrap bit" to a unique
1956 * wrap number and use that for generation number. For now do this.
1958 if ((hfsfhp
->hfsfid_gen
< VTOC(nvp
)->c_itime
)) {
1963 if (VNAME(nvp
) == NULL
) {
1964 struct cnode
*cp
= VTOC(nvp
);
1966 if (nvp
== cp
->c_rsrc_vp
) {
1967 // the +1/-2 thing is to skip the leading "/" on the rsrc fork spec
1968 // and to not count the trailing null byte at the end of the string.
1969 VNAME(nvp
) = add_name(_PATH_RSRCFORKSPEC
+1, sizeof(_PATH_RSRCFORKSPEC
)-2, 0, 0);
1971 VNAME(nvp
) = add_name(cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
, 0, 0);
1977 *exflagsp
= np
->netc_exflags
;
1978 *credanonp
= &np
->netc_anon
;
1986 * Vnode pointer to File handle
1995 struct hfsfid
*hfsfhp
;
1997 if (ISHFS(VTOVCB(vp
)))
1998 return (EOPNOTSUPP
); /* hfs standard is not exportable */
2001 hfsfhp
= (struct hfsfid
*)fhp
;
2002 hfsfhp
->hfsfid_len
= sizeof(struct hfsfid
);
2003 hfsfhp
->hfsfid_pad
= 0;
2004 hfsfhp
->hfsfid_cnid
= cp
->c_fileid
;
2005 hfsfhp
->hfsfid_gen
= cp
->c_itime
;
2012 * Initial HFS filesystems, done only once.
2016 struct vfsconf
*vfsp
;
2018 static int done
= 0;
2024 hfs_converterinit();
2032 * Allocate Catalog Iterator cache...
2034 (void) InitCatalogCache();
2040 hfs_getmountpoint(vp
, hfsmpp
)
2042 struct hfsmount
**hfsmpp
;
2044 struct hfsmount
* hfsmp
;
2049 if ((vp
->v_flag
& VROOT
) == 0)
2052 if (strcmp(vp
->v_mount
->mnt_stat
.f_fstypename
, "hfs") != 0)
2057 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
2066 #include <sys/filedesc.h>
2070 * HFS filesystem related variables.
2073 hfs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
2082 extern u_int32_t
hfs_getencodingbias(void);
2083 extern void hfs_setencodingbias(u_int32_t
);
2086 struct sysctl_req
*req
;
2089 struct hfsmount
*hfsmp
;
2092 /* all sysctl names at this level are terminal */
2094 if (name
[0] == HFS_ENCODINGBIAS
) {
2097 bias
= hfs_getencodingbias();
2098 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
2099 if (error
== 0 && newp
)
2100 hfs_setencodingbias(bias
);
2103 } else if (name
[0] == HFS_EXTEND_FS
) {
2108 if ((error
= hfs_getmountpoint(p
->p_fd
->fd_cdir
, &hfsmp
)))
2110 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, &newsize
);
2114 error
= hfs_extendfs(HFSTOVFS(hfsmp
), newsize
, p
);
2117 } else if (name
[0] == HFS_ENCODINGHINT
) {
2121 u_int16_t
*unicode_name
;
2124 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
2125 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
2126 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
2128 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
2130 error
= utf8_decodestr(filename
, newlen
- 1, unicode_name
,
2131 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
2133 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
2134 error
= sysctl_int(oldp
, oldlenp
, NULL
, NULL
, &hint
);
2137 FREE(unicode_name
, M_TEMP
);
2138 FREE(filename
, M_TEMP
);
2141 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
2142 // make the file system journaled...
2143 struct vnode
*vp
= p
->p_fd
->fd_cdir
, *jvp
;
2146 struct cat_attr jnl_attr
, jinfo_attr
;
2147 struct cat_fork jnl_fork
, jinfo_fork
;
2150 /* Only root can enable journaling */
2151 if (current_proc()->p_ucred
->cr_uid
!= 0) {
2156 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2159 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2160 printf("hfs: can't make a plain hfs volume journaled.\n");
2165 printf("hfs: volume @ mp 0x%x is already journaled!\n", vp
->v_mount
);
2169 vcb
= HFSTOVCB(hfsmp
);
2170 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
2171 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
2173 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
2177 // make sure these both exist!
2178 if ( GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
2179 || GetFileInfo(vcb
, kRootDirID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2184 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, FSCRED
, p
);
2187 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2188 (off_t
)name
[2], (off_t
)name
[3]);
2190 jvp
= hfsmp
->hfs_devvp
;
2191 jnl
= journal_create(jvp
,
2192 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2193 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2194 (off_t
)((unsigned)name
[3]),
2196 hfsmp
->hfs_phys_block_size
,
2199 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2202 printf("hfs: FAILED to create the journal!\n");
2203 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2204 VOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
);
2211 hfs_global_exclusive_lock_acquire(hfsmp
);
2213 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2214 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2218 // save this off for the hack-y check in hfs_remove()
2219 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2220 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2221 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2222 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2224 hfsmp
->hfs_mp
->mnt_flag
|= MNT_JOURNALED
;
2226 hfs_global_exclusive_lock_release(hfsmp
);
2227 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2230 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2231 // clear the journaling bit
2232 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2236 /* Only root can disable journaling */
2237 if (current_proc()->p_ucred
->cr_uid
!= 0) {
2243 printf("hfs: disabling journaling for mount @ 0x%x\n", vp
->v_mount
);
2247 hfs_global_exclusive_lock_acquire(hfsmp
);
2249 // Lights out for you buddy!
2253 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2254 VOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
);
2258 hfsmp
->hfs_mp
->mnt_flag
&= ~MNT_JOURNALED
;
2259 hfsmp
->jnl_start
= 0;
2260 hfsmp
->hfs_jnlinfoblkid
= 0;
2261 hfsmp
->hfs_jnlfileid
= 0;
2263 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2265 hfs_global_exclusive_lock_release(hfsmp
);
2266 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2269 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2270 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2271 off_t jnl_start
, jnl_size
;
2274 if (hfsmp
->jnl
== NULL
) {
2278 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2279 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2282 if ((error
= copyout((caddr_t
)&jnl_start
, (void *)name
[1], sizeof(off_t
))) != 0) {
2285 if ((error
= copyout((caddr_t
)&jnl_size
, (void *)name
[2], sizeof(off_t
))) != 0) {
2290 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2292 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2294 } else if (name
[0] == VFS_CTL_QUERY
) {
2295 req
= oldp
; /* we're new style vfs sysctl. */
2297 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2298 if (error
) return (error
);
2300 mp
= vfs_getvfs(&vc
.vc_fsid
);
2301 if (mp
== NULL
) return (ENOENT
);
2303 hfsmp
= VFSTOHFS(mp
);
2304 bzero(&vq
, sizeof(vq
));
2305 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2306 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2309 return (EOPNOTSUPP
);
2313 /* This will return a vnode of either a directory or a data vnode based on an object id. If
2314 * it is a file id, its data fork will be returned.
2317 hfs_vget(mp
, ino
, vpp
)
2322 cnid_t cnid
= *(cnid_t
*)ino
;
2324 /* Check for cnids that should't be exported. */
2325 if ((cnid
< kHFSFirstUserCatalogNodeID
)
2326 && (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
))
2328 /* Don't export HFS Private Data dir. */
2329 if (cnid
== VFSTOHFS(mp
)->hfs_privdir_desc
.cd_cnid
)
2332 return (hfs_getcnode(VFSTOHFS(mp
), cnid
, NULL
, 0, NULL
, NULL
, vpp
));
2336 * Check to see if a given vnode is only referenced for events:
2337 * [ entered with vp->v_interlock locked ]
2340 hfs_evtonly(struct vnode
*vp
)
2344 ubc_refcount
= UBCINFOEXISTS(vp
) ? 1 : 0;
2345 return (vp
->v_usecount
== (ubc_refcount
+ EVTONLYREFS(vp
)));
2349 * Check to see if all non-system vnodes for a given mountpoint are events-only
2352 hfs_flush_evtonly(struct mount
*mp
, int flags
, int dispose
, struct proc
*p
)
2354 struct vnode
*vp
, *nvp
;
2357 simple_lock(&mntvnode_slock
);
2359 for (vp
= LIST_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= nvp
) {
2360 if (vp
->v_mount
!= mp
) goto loop
;
2361 nvp
= vp
->v_mntvnodes
.le_next
;
2363 simple_lock(&vp
->v_interlock
);
2365 * Skip over a vnodes marked VSYSTEM or VNOFLUSH.
2367 if ((flags
& SKIPSYSTEM
) && ((vp
->v_flag
& VSYSTEM
) || (vp
->v_flag
& VNOFLUSH
))) {
2368 simple_unlock(&vp
->v_interlock
);
2372 * Skip over a vnodes marked VSWAP.
2374 if ((flags
& SKIPSWAP
) && (vp
->v_flag
& VSWAP
)) {
2375 simple_unlock(&vp
->v_interlock
);
2378 if (hfs_evtonly(vp
)) {
2380 /* "dispose" implies "forcibly", a la "FORCECLOSE": */
2381 simple_unlock(&mntvnode_slock
);
2383 simple_lock(&mntvnode_slock
);
2385 simple_unlock(&vp
->v_interlock
);
2390 simple_unlock(&vp
->v_interlock
);
2392 /* If asked to dispose, keep trying. If only checking, the answer is now known. */
2399 simple_unlock(&mntvnode_slock
);
2405 * Flush out all the files in a filesystem.
2408 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2410 struct hfsmount
*hfsmp
;
2411 struct vnode
*skipvp
= NULLVP
;
2412 struct vnode
*rsrcvp
;
2417 hfsmp
= VFSTOHFS(mp
);
2421 * The open quota files have an indirect reference on
2422 * the root directory vnode. We must account for this
2423 * extra reference when doing the intial vflush.
2426 if (mp
->mnt_flag
& MNT_QUOTA
) {
2428 /* Find out how many quota files we have open. */
2429 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2430 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2434 /* Obtain the root vnode so we can skip over it. */
2435 if (hfs_chashget(hfsmp
->hfs_raw_dev
, kRootDirID
, 0,
2436 &skipvp
, &rsrcvp
) == NULL
) {
2442 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2444 * If the vflush() call failed solely because there are
2445 * some event-only vnodes in the list, then forcibly get
2446 * rid of those vnodes before the final vflush() pass.
2448 if ((error
== EBUSY
) && hfs_flush_evtonly(mp
, SKIPSYSTEM
| SKIPSWAP
, 0, p
)) {
2449 (void) hfs_flush_evtonly(mp
, SKIPSYSTEM
| SKIPSWAP
, 1, p
);
2451 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2454 if (mp
->mnt_flag
& MNT_QUOTA
) {
2457 * See if there are additional references on the
2458 * root vp besides the ones obtained from the open
2459 * quota files and the hfs_chashget call above.
2462 (skipvp
->v_usecount
> (1 + quotafilecnt
))) {
2463 error
= EBUSY
; /* root directory is still open */
2467 if (error
&& (flags
& FORCECLOSE
) == 0)
2470 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2471 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2473 hfs_quotaoff(p
, mp
, i
);
2475 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2483 * Update volume encoding bitmap (HFS Plus only)
2487 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2489 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2490 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2495 case kTextEncodingMacUkrainian
:
2496 index
= kIndexMacUkrainian
;
2498 case kTextEncodingMacFarsi
:
2499 index
= kIndexMacFarsi
;
2507 HFSTOVCB(hfsmp
)->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2508 HFSTOVCB(hfsmp
)->vcbFlags
|= 0xFF00;
2513 * Update volume stats
2517 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2521 vcb
= HFSTOVCB(hfsmp
);
2522 vcb
->vcbFlags
|= 0xFF00;
2523 vcb
->vcbLsMod
= time
.tv_sec
;
2529 if (vcb
->vcbDirCnt
!= 0xFFFFFFFF)
2531 if (inroot
&& vcb
->vcbNmRtDirs
!= 0xFFFF)
2535 if (vcb
->vcbDirCnt
!= 0)
2537 if (inroot
&& vcb
->vcbNmRtDirs
!= 0xFFFF)
2541 if (vcb
->vcbFilCnt
!= 0xFFFFFFFF)
2543 if (inroot
&& vcb
->vcbNmFls
!= 0xFFFF)
2547 if (vcb
->vcbFilCnt
!= 0)
2549 if (inroot
&& vcb
->vcbNmFls
!= 0xFFFF)
2555 hfs_flushvolumeheader(hfsmp
, 0, 0);
2563 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2565 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2566 struct filefork
*fp
;
2567 HFSMasterDirectoryBlock
*mdb
;
2568 struct buf
*bp
= NULL
;
2573 sectorsize
= hfsmp
->hfs_phys_block_size
;
2574 retval
= bread(hfsmp
->hfs_devvp
, HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2581 DBG_ASSERT(bp
!= NULL
);
2582 DBG_ASSERT(bp
->b_data
!= NULL
);
2583 DBG_ASSERT(bp
->b_bcount
== size
);
2586 panic("hfs: standard hfs volumes should not be journaled!\n");
2589 mdb
= (HFSMasterDirectoryBlock
*)(bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
2591 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2592 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2593 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2594 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2595 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2596 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2597 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2598 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2600 namelen
= strlen(vcb
->vcbVN
);
2601 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2602 /* Retry with MacRoman in case that's how it was exported. */
2604 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2606 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2607 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2608 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2609 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2610 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2612 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2614 fp
= VTOF(vcb
->extentsRefNum
);
2615 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2616 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2617 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2618 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2619 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2620 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2621 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2622 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2624 fp
= VTOF(vcb
->catalogRefNum
);
2625 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2626 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2627 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2628 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2629 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2630 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2631 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2632 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2634 /* If requested, flush out the alternate MDB */
2636 struct buf
*alt_bp
= NULL
;
2639 altIDSector
= HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2641 if (meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2642 bcopy(mdb
, alt_bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2644 (void) VOP_BWRITE(alt_bp
);
2649 if (waitfor
!= MNT_WAIT
)
2652 retval
= VOP_BWRITE(bp
);
2654 MarkVCBClean( vcb
);
2660 * Flush any dirty in-memory mount data to the on-disk
2663 * Note: the on-disk volume signature is intentionally
2664 * not flushed since the on-disk "H+" and "HX" signatures
2665 * are always stored in-memory as "H+".
2669 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2671 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2672 struct filefork
*fp
;
2673 HFSPlusVolumeHeader
*volumeHeader
;
2680 u_int16_t signature
;
2683 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2686 if (vcb
->vcbSigWord
== kHFSSigWord
)
2687 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2691 sectorsize
= hfsmp
->hfs_phys_block_size
;
2692 priIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2693 HFS_PRI_SECTOR(sectorsize
);
2696 hfs_global_shared_lock_acquire(hfsmp
);
2698 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
2699 hfs_global_shared_lock_release(hfsmp
);
2704 retval
= meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2710 journal_end_transaction(hfsmp
->jnl
);
2712 hfs_global_shared_lock_release(hfsmp
);
2714 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2719 journal_modify_block_start(hfsmp
->jnl
, bp
);
2722 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
2725 * Sanity check what we just read.
2727 signature
= SWAP_BE16 (volumeHeader
->signature
);
2728 version
= SWAP_BE16 (volumeHeader
->version
);
2729 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2730 (version
< kHFSPlusVersion
) || (version
> 100) ||
2731 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2733 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2734 vcb
->vcbVN
, signature
, version
,
2735 SWAP_BE32 (volumeHeader
->blockSize
));
2737 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2743 * For embedded HFS+ volumes, update create date if it changed
2744 * (ie from a setattrlist call)
2746 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2747 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2749 HFSMasterDirectoryBlock
*mdb
;
2751 retval
= meta_bread(hfsmp
->hfs_devvp
, HFS_PRI_SECTOR(sectorsize
),
2752 sectorsize
, NOCRED
, &bp2
);
2758 mdb
= (HFSMasterDirectoryBlock
*)(bp2
->b_data
+
2759 HFS_PRI_OFFSET(sectorsize
));
2761 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2765 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2768 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2772 journal_modify_block_end(hfsmp
->jnl
, bp2
);
2774 (void) VOP_BWRITE(bp2
); /* write out the changes */
2779 brelse(bp2
); /* just release it */
2784 /* Note: only update the lower 16 bits worth of attributes */
2785 volumeHeader
->attributes
= SWAP_BE32 ((SWAP_BE32 (volumeHeader
->attributes
) & 0xFFFF0000) + (UInt16
) vcb
->vcbAtrb
);
2786 volumeHeader
->journalInfoBlock
= SWAP_BE32(vcb
->vcbJinfoBlock
);
2788 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2790 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2792 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2793 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2794 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2795 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2796 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2797 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2798 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2799 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2800 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2801 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2802 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2803 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2805 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0)
2807 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2809 /* Sync Extents over-flow file meta data */
2810 fp
= VTOF(vcb
->extentsRefNum
);
2811 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2812 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2813 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2814 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2815 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2817 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2818 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2819 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2820 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2822 /* Sync Catalog file meta data */
2823 fp
= VTOF(vcb
->catalogRefNum
);
2824 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2825 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2826 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2827 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2828 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2830 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2831 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2832 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2833 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2835 /* Sync Allocation file meta data */
2836 fp
= VTOF(vcb
->allocationsRefNum
);
2837 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2838 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2839 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2840 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2841 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2843 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2844 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2845 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2846 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2848 /* If requested, flush out the alternate volume header */
2850 struct buf
*alt_bp
= NULL
;
2853 altIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2854 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2856 if (meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2858 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2861 bcopy(volumeHeader
, alt_bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2864 journal_modify_block_end(hfsmp
->jnl
, alt_bp
);
2866 (void) VOP_BWRITE(alt_bp
);
2874 journal_modify_block_end(hfsmp
->jnl
, bp
);
2875 journal_end_transaction(hfsmp
->jnl
);
2877 if (waitfor
!= MNT_WAIT
)
2880 retval
= VOP_BWRITE(bp
);
2881 /* When critical data changes, flush the device cache */
2882 if (critical
&& (retval
== 0)) {
2883 (void) VOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2884 NULL
, FWRITE
, NOCRED
, current_proc());
2888 hfs_global_shared_lock_release(hfsmp
);
2890 vcb
->vcbFlags
&= 0x00FF;
2896 * Extend a file system.
2899 hfs_extendfs(struct mount
*mp
, u_int64_t newsize
, struct proc
*p
)
2902 struct vnode
*devvp
;
2904 struct hfsmount
*hfsmp
;
2905 struct filefork
*fp
= NULL
;
2907 struct cat_fork forkdata
;
2909 u_int64_t newblkcnt
;
2911 u_int64_t sectorcnt
;
2912 u_int32_t sectorsize
;
2913 daddr_t prev_alt_sector
;
2917 hfsmp
= VFSTOHFS(mp
);
2918 devvp
= hfsmp
->hfs_devvp
;
2919 vcb
= HFSTOVCB(hfsmp
);
2922 * - HFS Plus file systems only.
2923 * - Journaling must be enabled.
2924 * - No embedded volumes.
2926 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
2927 (hfsmp
->jnl
== NULL
) ||
2928 (vcb
->hfsPlusIOPosOffset
!= 0)) {
2932 * If extending file system by non-root, then verify
2933 * ownership and check permissions.
2935 if (p
->p_ucred
->cr_uid
!= 0) {
2936 error
= hfs_root(mp
, &vp
);
2939 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, p
->p_ucred
, p
, 0);
2941 error
= hfs_write_access(vp
, p
->p_ucred
, p
, false);
2947 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
2948 error
= VOP_ACCESS(devvp
, VREAD
| VWRITE
, p
->p_ucred
, p
);
2949 VOP_UNLOCK(devvp
, 0, p
);
2953 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, FSCRED
, p
)) {
2956 if (sectorsize
!= hfsmp
->hfs_phys_block_size
) {
2959 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, FSCRED
, p
)) {
2962 if ((sectorsize
* sectorcnt
) < newsize
) {
2963 printf("hfs_extendfs: not enough space on device\n");
2966 oldsize
= (u_int64_t
)hfsmp
->hfs_phys_block_count
*
2967 (u_int64_t
)hfsmp
->hfs_phys_block_size
;
2970 * Validate new size.
2972 if ((newsize
<= oldsize
) || (newsize
% vcb
->blockSize
)) {
2973 printf("hfs_extendfs: invalid size\n");
2976 newblkcnt
= newsize
/ vcb
->blockSize
;
2977 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
2980 addblks
= newblkcnt
- vcb
->totalBlocks
;
2982 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
2984 * Enclose changes inside a transaction.
2986 hfs_global_shared_lock_acquire(hfsmp
);
2987 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
2988 hfs_global_shared_lock_release(hfsmp
);
2993 * Remember the location of existing alternate VH.
2995 prev_alt_sector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2996 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2998 vp
= vcb
->allocationsRefNum
;
2999 error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
3004 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
3007 * Calculate additional space required (if any) by allocation bitmap.
3009 bitmapblks
= roundup(newblkcnt
/ 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
3010 if (bitmapblks
> fp
->ff_blocks
)
3011 bitmapblks
-= fp
->ff_blocks
;
3015 if (bitmapblks
> 0) {
3020 * Add a new extent to the allocation bitmap file.
3022 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
3024 printf("hfs_extendfs: error %d adding extents\n", error
);
3027 blkcnt
= bitmapblks
;
3028 blkno
= fp
->ff_blocks
;
3029 fp
->ff_blocks
+= bitmapblks
;
3030 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3031 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3033 * Zero out the new bitmap blocks.
3038 while (blkcnt
> 0) {
3039 error
= meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
3046 bzero((char *)bp
->b_data
, vcb
->blockSize
);
3047 bp
->b_flags
|= B_AGE
;
3056 printf("hfs_extendfs: error %d clearing blocks\n", error
);
3060 * Mark the new bitmap space as allocated.
3062 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
3064 printf("hfs_extendfs: error %d setting bitmap\n", error
);
3069 * Mark the new alternate VH as allocated.
3071 if (vcb
->blockSize
== 512)
3072 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
3074 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
3076 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
3080 * Mark the old alternate VH as free.
3082 if (vcb
->blockSize
== 512)
3083 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
3085 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
3088 * Adjust file system variables for new space.
3090 vcb
->totalBlocks
+= addblks
;
3091 vcb
->freeBlocks
+= addblks
- bitmapblks
;
3092 hfsmp
->hfs_phys_block_count
= newsize
/ sectorsize
;
3095 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3097 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
3099 * Restore to old state.
3101 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3102 vcb
->totalBlocks
-= addblks
;
3103 vcb
->freeBlocks
-= addblks
- bitmapblks
;
3104 hfsmp
->hfs_phys_block_count
= oldsize
/ sectorsize
;
3106 if (vcb
->blockSize
== 512)
3107 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
3109 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
3113 * Invalidate the old alternate volume header.
3116 if (meta_bread(hfsmp
->hfs_devvp
, prev_alt_sector
, sectorsize
,
3117 NOCRED
, &bp
) == 0) {
3118 journal_modify_block_start(hfsmp
->jnl
, bp
);
3119 bzero(bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
3120 journal_modify_block_end(hfsmp
->jnl
, bp
);
3126 /* Restore allocation fork. */
3127 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3128 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3131 VOP_UNLOCK(vp
, 0, p
);
3133 journal_end_transaction(hfsmp
->jnl
);
3134 hfs_global_shared_lock_release(hfsmp
);
3141 * hfs vfs operations.
3143 struct vfsops hfs_vfsops
= {