2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1991, 1993, 1994
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
68 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
70 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kauth.h>
78 #include <sys/ubc_internal.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/mount_internal.h>
81 #include <sys/sysctl.h>
82 #include <sys/malloc.h>
84 #include <sys/quota.h>
86 #include <sys/paths.h>
87 #include <sys/utfconv.h>
88 #include <sys/kdebug.h>
89 #include <sys/fslog.h>
91 #include <kern/locks.h>
93 #include <vfs/vfs_journal.h>
95 #include <miscfs/specfs/specdev.h>
96 #include <hfs/hfs_mount.h>
99 #include "hfs_catalog.h"
100 #include "hfs_cnode.h"
102 #include "hfs_endian.h"
103 #include "hfs_hotfiles.h"
104 #include "hfs_quota.h"
106 #include "hfscommon/headers/FileMgrInternal.h"
107 #include "hfscommon/headers/BTreesInternal.h"
115 lck_grp_attr_t
* hfs_group_attr
;
116 lck_attr_t
* hfs_lock_attr
;
117 lck_grp_t
* hfs_mutex_group
;
118 lck_grp_t
* hfs_rwlock_group
;
120 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
121 /* not static so we can re-use in hfs_readwrite.c for build_path */
122 int hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, vfs_context_t context
);
125 static int hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
);
126 static int hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
);
127 static int hfs_flushfiles(struct mount
*, int, struct proc
*);
128 static int hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
129 static int hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
);
130 static int hfs_init(struct vfsconf
*vfsp
);
131 static int hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
);
132 static int hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
, int journal_replay_only
, vfs_context_t context
);
133 static int hfs_reload(struct mount
*mp
);
134 static int hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, vfs_context_t context
);
135 static int hfs_quotactl(struct mount
*, int, uid_t
, caddr_t
, vfs_context_t context
);
136 static int hfs_start(struct mount
*mp
, int flags
, vfs_context_t context
);
137 static int hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, vfs_context_t context
);
138 static int hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
);
139 static int hfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
140 user_addr_t newp
, size_t newlen
, vfs_context_t context
);
141 static int hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
);
142 static int hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
144 static int hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
, vfs_context_t context
);
145 static int hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
,
146 u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
);
147 static int hfs_journal_replay(const char *devnode
, vfs_context_t context
);
151 * Called by vfs_mountroot when mounting HFS Plus as root.
156 hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
)
158 struct hfsmount
*hfsmp
;
160 struct vfsstatfs
*vfsp
;
163 hfs_chashinit_finish();
165 if ((error
= hfs_mountfs(rvp
, mp
, NULL
, 0, context
)))
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 vfsp
= vfs_statfs(mp
);
182 (void)hfs_statfs(mp
, vfsp
, NULL
);
195 hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
197 struct proc
*p
= vfs_context_proc(context
);
198 struct hfsmount
*hfsmp
= NULL
;
199 struct hfs_mount_args args
;
203 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
)))) {
206 cmdflags
= (u_int32_t
)vfs_flags(mp
) & MNT_CMDFLAGS
;
207 if (cmdflags
& MNT_UPDATE
) {
208 hfsmp
= VFSTOHFS(mp
);
210 /* Reload incore data after an fsck. */
211 if (cmdflags
& MNT_RELOAD
) {
212 if (vfs_isrdonly(mp
))
213 return hfs_reload(mp
);
218 /* Change to a read-only file system. */
219 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
223 /* use VFS_SYNC to push out System (btree) files */
224 retval
= VFS_SYNC(mp
, MNT_WAIT
, context
);
225 if (retval
&& ((cmdflags
& MNT_FORCE
) == 0))
229 if (cmdflags
& MNT_FORCE
)
232 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
235 /* mark the volume cleanly unmounted */
236 hfsmp
->vcbAtrb
|= kHFSVolumeUnmountedMask
;
237 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
238 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
240 /* also get the volume bitmap blocks */
242 if (vnode_mount(hfsmp
->hfs_devvp
) == mp
) {
243 retval
= hfs_fsync(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, p
);
245 vnode_get(hfsmp
->hfs_devvp
);
246 retval
= VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
247 vnode_put(hfsmp
->hfs_devvp
);
251 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
255 hfs_global_exclusive_lock_acquire(hfsmp
);
257 journal_close(hfsmp
->jnl
);
260 // Note: we explicitly don't want to shutdown
261 // access to the jvp because we may need
262 // it later if we go back to being read-write.
264 hfs_global_exclusive_lock_release(hfsmp
);
268 /* Change to a writable file system. */
269 if (vfs_iswriteupgrade(mp
)) {
272 * On inconsistent disks, do not allow read-write mount
273 * unless it is the boot volume being mounted.
275 if (!(vfs_flags(mp
) & MNT_ROOTFS
) &&
276 (hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
)) {
281 // If the journal was shut-down previously because we were
282 // asked to be read-only, let's start it back up again now
284 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
285 && hfsmp
->jnl
== NULL
286 && hfsmp
->jvp
!= NULL
) {
289 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
290 jflags
= JOURNAL_RESET
;
295 hfs_global_exclusive_lock_acquire(hfsmp
);
297 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
298 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
301 hfsmp
->hfs_logical_block_size
,
304 hfs_sync_metadata
, hfsmp
->hfs_mp
);
306 hfs_global_exclusive_lock_release(hfsmp
);
308 if (hfsmp
->jnl
== NULL
) {
312 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
317 /* Only clear HFS_READ_ONLY after a successfull write */
318 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
320 /* mark the volume dirty (clear clean unmount bit) */
321 hfsmp
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
323 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
324 if (retval
!= E_NONE
)
327 if (!(hfsmp
->hfs_flags
& (HFS_READ_ONLY
| HFS_STANDARD
))) {
328 /* Setup private/hidden directories for hardlinks. */
329 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
330 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
332 hfs_remove_orphans(hfsmp
);
335 * Allow hot file clustering if conditions allow.
337 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
338 (void) hfs_recording_init(hfsmp
);
340 /* Force ACLs on HFS+ file systems. */
341 if (vfs_extendedsecurity(HFSTOVFS(hfsmp
)) == 0) {
342 vfs_setextendedsecurity(HFSTOVFS(hfsmp
));
347 /* Update file system parameters. */
348 retval
= hfs_changefs(mp
, &args
);
350 } else /* not an update request */ {
352 /* Set the mount flag to indicate that we support volfs */
353 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_DOVOLFS
));
355 hfs_chashinit_finish();
357 retval
= hfs_mountfs(devvp
, mp
, &args
, 0, context
);
361 (void)hfs_statfs(mp
, vfs_statfs(mp
), context
);
367 struct hfs_changefs_cargs
{
368 struct hfsmount
*hfsmp
;
375 hfs_changefs_callback(struct vnode
*vp
, void *cargs
)
379 struct cat_desc cndesc
;
380 struct cat_attr cnattr
;
381 struct hfs_changefs_cargs
*args
;
385 args
= (struct hfs_changefs_cargs
*)cargs
;
388 vcb
= HFSTOVCB(args
->hfsmp
);
390 lockflags
= hfs_systemfile_lock(args
->hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
391 error
= cat_lookup(args
->hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
, NULL
);
392 hfs_systemfile_unlock(args
->hfsmp
, lockflags
);
395 * If we couldn't find this guy skip to the next one
400 return (VNODE_RETURNED
);
403 * Get the real uid/gid and perm mask from disk.
405 if (args
->permswitch
|| args
->permfix
) {
406 cp
->c_uid
= cnattr
.ca_uid
;
407 cp
->c_gid
= cnattr
.ca_gid
;
408 cp
->c_mode
= cnattr
.ca_mode
;
411 * If we're switching name converters then...
412 * Remove the existing entry from the namei cache.
413 * Update name to one based on new encoder.
417 replace_desc(cp
, &cndesc
);
419 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
420 strlcpy((char *)vcb
->vcbVN
, (const char *)cp
->c_desc
.cd_nameptr
, NAME_MAX
+1);
421 cp
->c_desc
.cd_encoding
= args
->hfsmp
->hfs_encoding
;
424 cat_releasedesc(&cndesc
);
426 return (VNODE_RETURNED
);
429 /* Change fs mount parameters */
431 hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
)
434 int namefix
, permfix
, permswitch
;
435 struct hfsmount
*hfsmp
;
437 hfs_to_unicode_func_t get_unicode_func
;
438 unicode_to_hfs_func_t get_hfsname_func
;
439 u_long old_encoding
= 0;
440 struct hfs_changefs_cargs cargs
;
441 u_int32_t mount_flags
;
443 hfsmp
= VFSTOHFS(mp
);
444 vcb
= HFSTOVCB(hfsmp
);
445 mount_flags
= (unsigned int)vfs_flags(mp
);
447 hfsmp
->hfs_flags
|= HFS_IN_CHANGEFS
;
449 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
450 ((mount_flags
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
451 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
452 (mount_flags
& MNT_UNKNOWNPERMISSIONS
)));
454 /* The root filesystem must operate with actual permissions: */
455 if (permswitch
&& (mount_flags
& MNT_ROOTFS
) && (mount_flags
& MNT_UNKNOWNPERMISSIONS
)) {
456 vfs_clearflags(mp
, (u_int64_t
)((unsigned int)MNT_UNKNOWNPERMISSIONS
)); /* Just say "No". */
460 if (mount_flags
& MNT_UNKNOWNPERMISSIONS
)
461 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
463 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
465 namefix
= permfix
= 0;
468 * Tracking of hot files requires up-to-date access times. So if
469 * access time updates are disabled, we must also disable hot files.
471 if (mount_flags
& MNT_NOATIME
) {
472 (void) hfs_recording_suspend(hfsmp
);
475 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
476 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
477 gTimeZone
= args
->hfs_timezone
;
480 /* Change the default uid, gid and/or mask */
481 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
482 hfsmp
->hfs_uid
= args
->hfs_uid
;
483 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
486 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
487 hfsmp
->hfs_gid
= args
->hfs_gid
;
488 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
491 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
492 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
493 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
494 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
495 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
496 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
497 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
502 /* Change the hfs encoding value (hfs only) */
503 if ((vcb
->vcbSigWord
== kHFSSigWord
) &&
504 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
505 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
507 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
512 * Connect the new hfs_get_unicode converter but leave
513 * the old hfs_get_hfsname converter in place so that
514 * we can lookup existing vnodes to get their correctly
517 * When we're all finished, we can then connect the new
518 * hfs_get_hfsname converter and release our interest
519 * in the old converters.
521 hfsmp
->hfs_get_unicode
= get_unicode_func
;
522 old_encoding
= hfsmp
->hfs_encoding
;
523 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
527 if (!(namefix
|| permfix
|| permswitch
))
530 /* XXX 3762912 hack to support HFS filesystem 'owner' */
533 hfsmp
->hfs_uid
== UNKNOWNUID
? KAUTH_UID_NONE
: hfsmp
->hfs_uid
,
534 hfsmp
->hfs_gid
== UNKNOWNGID
? KAUTH_GID_NONE
: hfsmp
->hfs_gid
);
537 * For each active vnode fix things that changed
539 * Note that we can visit a vnode more than once
540 * and we can race with fsync.
542 * hfs_changefs_callback will be called for each vnode
543 * hung off of this mount point
545 * The vnode will be properly referenced and unreferenced
546 * around the callback
549 cargs
.namefix
= namefix
;
550 cargs
.permfix
= permfix
;
551 cargs
.permswitch
= permswitch
;
553 vnode_iterate(mp
, 0, hfs_changefs_callback
, (void *)&cargs
);
556 * If we're switching name converters we can now
557 * connect the new hfs_get_hfsname converter and
558 * release our interest in the old converters.
561 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
562 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
563 (void) hfs_relconverter(old_encoding
);
566 hfsmp
->hfs_flags
&= ~HFS_IN_CHANGEFS
;
571 struct hfs_reload_cargs
{
572 struct hfsmount
*hfsmp
;
577 hfs_reload_callback(struct vnode
*vp
, void *cargs
)
580 struct hfs_reload_cargs
*args
;
583 args
= (struct hfs_reload_cargs
*)cargs
;
585 * flush all the buffers associated with this node
587 (void) buf_invalidateblks(vp
, 0, 0, 0);
591 * Remove any directory hints
594 hfs_reldirhints(cp
, 0);
597 * Re-read cnode data for all active vnodes (non-metadata files).
599 if (!vnode_issystem(vp
) && !VNODE_IS_RSRC(vp
)) {
600 struct cat_fork
*datafork
;
601 struct cat_desc desc
;
603 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
605 /* lookup by fileID since name could have changed */
606 lockflags
= hfs_systemfile_lock(args
->hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
607 args
->error
= cat_idlookup(args
->hfsmp
, cp
->c_fileid
, 0, &desc
, &cp
->c_attr
, datafork
);
608 hfs_systemfile_unlock(args
->hfsmp
, lockflags
);
610 return (VNODE_RETURNED_DONE
);
613 /* update cnode's catalog descriptor */
614 (void) replace_desc(cp
, &desc
);
616 return (VNODE_RETURNED
);
620 * Reload all incore data for a filesystem (used after running fsck on
621 * the root filesystem and finding things to fix). The filesystem must
622 * be mounted read-only.
624 * Things to do to update the mount:
625 * invalidate all cached meta-data.
626 * invalidate all inactive vnodes.
627 * invalidate all cached file data.
628 * re-read volume header from disk.
629 * re-load meta-file info (extents, file size).
630 * re-load B-tree header data.
631 * re-read cnode data for all active vnodes.
634 hfs_reload(struct mount
*mountp
)
636 register struct vnode
*devvp
;
639 struct hfsmount
*hfsmp
;
640 struct HFSPlusVolumeHeader
*vhp
;
642 struct filefork
*forkp
;
643 struct cat_desc cndesc
;
644 struct hfs_reload_cargs args
;
645 daddr64_t priIDSector
;
647 hfsmp
= VFSTOHFS(mountp
);
648 vcb
= HFSTOVCB(hfsmp
);
650 if (vcb
->vcbSigWord
== kHFSSigWord
)
651 return (EINVAL
); /* rooting from HFS is not supported! */
654 * Invalidate all cached meta-data.
656 devvp
= hfsmp
->hfs_devvp
;
657 if (buf_invalidateblks(devvp
, 0, 0, 0))
658 panic("hfs_reload: dirty1");
663 * hfs_reload_callback will be called for each vnode
664 * hung off of this mount point that can't be recycled...
665 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
666 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
667 * properly referenced and unreferenced around the callback
669 vnode_iterate(mountp
, VNODE_RELOAD
| VNODE_WAIT
, hfs_reload_callback
, (void *)&args
);
675 * Re-read VolumeHeader from disk.
677 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
678 HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
));
680 error
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
681 HFS_PHYSBLK_ROUNDDOWN(priIDSector
, hfsmp
->hfs_log_per_phys
),
682 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
689 vhp
= (HFSPlusVolumeHeader
*) (buf_dataptr(bp
) + HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
691 /* Do a quick sanity check */
692 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
693 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
694 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
695 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
696 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
701 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
702 vcb
->vcbAtrb
= SWAP_BE32 (vhp
->attributes
);
703 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
704 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
705 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
706 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
707 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
708 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
709 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
710 HFS_UPDATE_NEXT_ALLOCATION(vcb
, SWAP_BE32 (vhp
->nextAllocation
));
711 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
712 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
713 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
714 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
715 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
718 * Re-load meta-file vnode data (extent info, file size, etc).
720 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
721 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
722 forkp
->ff_extents
[i
].startBlock
=
723 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
724 forkp
->ff_extents
[i
].blockCount
=
725 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
727 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
728 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
729 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
732 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
733 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
734 forkp
->ff_extents
[i
].startBlock
=
735 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
736 forkp
->ff_extents
[i
].blockCount
=
737 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
739 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
740 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
741 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
743 if (hfsmp
->hfs_attribute_vp
) {
744 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
745 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
746 forkp
->ff_extents
[i
].startBlock
=
747 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
748 forkp
->ff_extents
[i
].blockCount
=
749 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
751 forkp
->ff_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
752 forkp
->ff_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
753 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
756 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
757 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
758 forkp
->ff_extents
[i
].startBlock
=
759 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
760 forkp
->ff_extents
[i
].blockCount
=
761 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
763 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
764 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
765 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
771 * Re-load B-tree header data
773 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
774 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
777 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
778 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
781 if (hfsmp
->hfs_attribute_vp
) {
782 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
783 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
787 /* Reload the volume name */
788 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, 0, &cndesc
, NULL
, NULL
)))
790 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
791 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
792 cat_releasedesc(&cndesc
);
794 /* Re-establish private/hidden directories. */
795 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
796 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
798 /* In case any volume information changed to trigger a notification */
799 hfs_generate_volume_notifications(hfsmp
);
806 * Common code for mount and mountroot
809 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
,
810 int journal_replay_only
, vfs_context_t context
)
812 struct proc
*p
= vfs_context_proc(context
);
814 struct hfsmount
*hfsmp
;
817 HFSMasterDirectoryBlock
*mdbp
;
825 daddr64_t log_blkcnt
;
826 u_int32_t log_blksize
;
827 u_int32_t phys_blksize
;
828 u_int32_t minblksize
;
829 u_int32_t iswritable
;
830 daddr64_t mdb_offset
;
833 ronly
= vfs_isrdonly(mp
);
834 dev
= vnode_specrdev(devvp
);
835 cred
= p
? vfs_context_ucred(context
) : NOCRED
;
841 minblksize
= kHFSBlockSize
;
843 /* Advisory locking should be handled at the VFS layer */
844 vfs_setlocklocal(mp
);
846 /* Get the logical block size (treated as physical block size everywhere) */
847 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&log_blksize
, 0, context
)) {
851 /* Get the physical block size. */
852 retval
= VNOP_IOCTL(devvp
, DKIOCGETPHYSICALBLOCKSIZE
, (caddr_t
)&phys_blksize
, 0, context
);
854 if ((retval
!= ENOTSUP
) && (retval
!= ENOTTY
)) {
858 /* If device does not support this ioctl, assume that physical
859 * block size is same as logical block size
861 phys_blksize
= log_blksize
;
863 /* Switch to 512 byte sectors (temporarily) */
864 if (log_blksize
> 512) {
865 u_int32_t size512
= 512;
867 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, context
)) {
872 /* Get the number of 512 byte physical blocks. */
873 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
874 /* resetting block size may fail if getting block count did */
875 (void)VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
);
880 /* Compute an accurate disk size (i.e. within 512 bytes) */
881 disksize
= (u_int64_t
)log_blkcnt
* (u_int64_t
)512;
884 * On Tiger it is not necessary to switch the device
885 * block size to be 4k if there are more than 31-bits
886 * worth of blocks but to insure compatibility with
887 * pre-Tiger systems we have to do it.
889 if (log_blkcnt
> 0x000000007fffffff) {
890 minblksize
= log_blksize
= 4096;
891 if (phys_blksize
< log_blksize
)
892 phys_blksize
= log_blksize
;
895 /* Now switch to our preferred physical block size. */
896 if (log_blksize
> 512) {
897 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
)) {
901 /* Get the count of physical blocks. */
902 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
909 * minblksize is the minimum physical block size
910 * log_blksize has our preferred physical block size
911 * log_blkcnt has the total number of physical blocks
914 mdb_offset
= (daddr64_t
)HFS_PRI_SECTOR(log_blksize
);
915 if ((retval
= (int)buf_meta_bread(devvp
,
916 HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, (phys_blksize
/log_blksize
)),
917 phys_blksize
, cred
, &bp
))) {
920 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
921 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
), mdbp
, kMDBSize
);
925 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
926 bzero(hfsmp
, sizeof(struct hfsmount
));
929 * Init the volume information structure
932 lck_mtx_init(&hfsmp
->hfs_mutex
, hfs_mutex_group
, hfs_lock_attr
);
933 lck_mtx_init(&hfsmp
->hfc_mutex
, hfs_mutex_group
, hfs_lock_attr
);
934 lck_rw_init(&hfsmp
->hfs_global_lock
, hfs_rwlock_group
, hfs_lock_attr
);
935 lck_rw_init(&hfsmp
->hfs_insync
, hfs_rwlock_group
, hfs_lock_attr
);
937 vfs_setfsprivate(mp
, hfsmp
);
938 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
939 hfsmp
->hfs_raw_dev
= vnode_specrdev(devvp
);
940 hfsmp
->hfs_devvp
= devvp
;
941 vnode_ref(devvp
); /* Hold a ref on the device, dropped when hfsmp is freed. */
942 hfsmp
->hfs_logical_block_size
= log_blksize
;
943 hfsmp
->hfs_logical_block_count
= log_blkcnt
;
944 hfsmp
->hfs_physical_block_size
= phys_blksize
;
945 hfsmp
->hfs_log_per_phys
= (phys_blksize
/ log_blksize
);
946 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
948 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
949 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)
950 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
953 for (i
= 0; i
< MAXQUOTAS
; i
++)
954 dqfileinit(&hfsmp
->hfs_qfiles
[i
]);
958 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
959 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
960 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
961 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
962 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
963 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
964 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
965 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
966 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
968 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
971 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
972 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
974 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
977 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
978 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
) {
979 hfsmp
->hfs_uid
= UNKNOWNUID
;
980 hfsmp
->hfs_gid
= UNKNOWNGID
;
981 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
982 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
983 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
987 /* Find out if disk media is writable. */
988 if (VNOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, context
) == 0) {
990 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
992 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
995 // record the current time at which we're mounting this volume
998 hfsmp
->hfs_mount_time
= tv
.tv_sec
;
1000 /* Mount a standard HFS disk */
1001 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
1002 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
1004 /* If only journal replay is requested, exit immediately */
1005 if (journal_replay_only
) {
1010 if ((vfs_flags(mp
) & MNT_ROOTFS
)) {
1011 retval
= EINVAL
; /* Cannot root from HFS standard disks */
1014 /* HFS disks can only use 512 byte physical blocks */
1015 if (log_blksize
> kHFSBlockSize
) {
1016 log_blksize
= kHFSBlockSize
;
1017 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
)) {
1021 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
1025 hfsmp
->hfs_logical_block_size
= log_blksize
;
1026 hfsmp
->hfs_logical_block_count
= log_blkcnt
;
1029 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
1030 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
1032 /* establish the timezone */
1033 gTimeZone
= args
->hfs_timezone
;
1036 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
1037 &hfsmp
->hfs_get_hfsname
);
1041 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
1043 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1045 } else /* Mount an HFS Plus disk */ {
1046 HFSPlusVolumeHeader
*vhp
;
1047 off_t embeddedOffset
;
1048 int jnl_disable
= 0;
1050 /* Get the embedded Volume Header */
1051 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1052 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
1053 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1054 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1057 * If the embedded volume doesn't start on a block
1058 * boundary, then switch the device to a 512-byte
1059 * block size so everything will line up on a block
1062 if ((embeddedOffset
% log_blksize
) != 0) {
1063 printf("HFS Mount: embedded volume offset not"
1064 " a multiple of physical block size (%d);"
1065 " switching to 512\n", log_blksize
);
1067 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
1068 (caddr_t
)&log_blksize
, FWRITE
, context
)) {
1072 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
1073 (caddr_t
)&log_blkcnt
, 0, context
)) {
1077 /* Note: relative block count adjustment */
1078 hfsmp
->hfs_logical_block_count
*=
1079 hfsmp
->hfs_logical_block_size
/ log_blksize
;
1080 hfsmp
->hfs_logical_block_size
= log_blksize
;
1083 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1084 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1086 hfsmp
->hfs_logical_block_count
= disksize
/ log_blksize
;
1088 mdb_offset
= (daddr64_t
)((embeddedOffset
/ log_blksize
) + HFS_PRI_SECTOR(log_blksize
));
1089 retval
= (int)buf_meta_bread(devvp
, HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
1090 phys_blksize
, cred
, &bp
);
1093 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
), mdbp
, 512);
1096 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1098 } else /* pure HFS+ */ {
1100 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1104 * On inconsistent disks, do not allow read-write mount
1105 * unless it is the boot volume being mounted.
1107 if (!(vfs_flags(mp
) & MNT_ROOTFS
) &&
1108 (SWAP_BE32(vhp
->attributes
) & kHFSVolumeInconsistentMask
) &&
1109 !(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
1119 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) &&
1120 args
->journal_disable
) {
1125 // We only initialize the journal here if the last person
1126 // to mount this volume was journaling aware. Otherwise
1127 // we delay journal initialization until later at the end
1128 // of hfs_MountHFSPlusVolume() because the last person who
1129 // mounted it could have messed things up behind our back
1130 // (so we need to go find the .journal file, make sure it's
1131 // the right size, re-sync up if it was moved, etc).
1133 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1134 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1137 // if we're able to init the journal, mark the mount
1138 // point as journaled.
1140 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1141 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
1143 // if the journal failed to open, then set the lastMountedVersion
1144 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1145 // of just bailing out because the volume is journaled.
1147 HFSPlusVolumeHeader
*jvhp
;
1149 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1151 if (mdb_offset
== 0) {
1152 mdb_offset
= (daddr64_t
)((embeddedOffset
/ log_blksize
) + HFS_PRI_SECTOR(log_blksize
));
1156 retval
= (int)buf_meta_bread(devvp
,
1157 HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
1158 phys_blksize
, cred
, &bp
);
1160 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
));
1162 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1163 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1164 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1172 // clear this so the error exit path won't try to use it
1177 // if this isn't the root device just bail out.
1178 // If it is the root device we just continue on
1179 // in the hopes that fsck_hfs will be able to
1180 // fix any damage that exists on the volume.
1181 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1189 /* Either the journal is replayed successfully, or there
1190 * was nothing to replay, or no journal exists. In any case,
1193 if (journal_replay_only
) {
1198 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1200 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1202 * If the backend didn't like our physical blocksize
1203 * then retry with physical blocksize of 512.
1205 if ((retval
== ENXIO
) && (log_blksize
> 512) && (log_blksize
!= minblksize
)) {
1206 printf("HFS Mount: could not use physical block size "
1207 "(%d) switching to 512\n", log_blksize
);
1209 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
)) {
1213 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
1217 devvp
->v_specsize
= log_blksize
;
1218 /* Note: relative block count adjustment (in case this is an embedded volume). */
1219 hfsmp
->hfs_logical_block_count
*= hfsmp
->hfs_logical_block_size
/ log_blksize
;
1220 hfsmp
->hfs_logical_block_size
= log_blksize
;
1223 // close and re-open this with the new block size
1224 journal_close(hfsmp
->jnl
);
1226 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1227 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
1229 // if the journal failed to open, then set the lastMountedVersion
1230 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1231 // of just bailing out because the volume is journaled.
1233 HFSPlusVolumeHeader
*jvhp
;
1235 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1237 if (mdb_offset
== 0) {
1238 mdb_offset
= (daddr64_t
)((embeddedOffset
/ log_blksize
) + HFS_PRI_SECTOR(log_blksize
));
1242 retval
= (int)buf_meta_bread(devvp
, HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
1243 phys_blksize
, cred
, &bp
);
1245 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
));
1247 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1248 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1249 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1257 // clear this so the error exit path won't try to use it
1262 // if this isn't the root device just bail out.
1263 // If it is the root device we just continue on
1264 // in the hopes that fsck_hfs will be able to
1265 // fix any damage that exists on the volume.
1266 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1273 /* Try again with a smaller block size... */
1274 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1277 (void) hfs_relconverter(0);
1280 // save off a snapshot of the mtime from the previous mount
1282 hfsmp
->hfs_last_mounted_mtime
= hfsmp
->hfs_mtime
;
1288 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (long)dev
;
1289 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
1290 vfs_setmaxsymlen(mp
, 0);
1291 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
1292 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSNATIVEXATTR
;
1294 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1296 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
1297 /* Tell VFS that we support directory hard links. */
1298 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSDIRLINKS
;
1300 /* HFS standard doesn't support extended readdir! */
1301 mp
->mnt_vtable
->vfc_vfsflags
&= ~VFC_VFSREADDIR_EXTENDED
;
1306 * Set the free space warning levels for a non-root volume:
1308 * Set the lower freespace limit (the level that will trigger a warning)
1309 * to 5% of the volume size or 250MB, whichever is less, and the desired
1310 * level (which will cancel the alert request) to 1/2 above that limit.
1311 * Start looking for free space to drop below this level and generate a
1312 * warning immediately if needed:
1314 hfsmp
->hfs_freespace_notify_warninglimit
=
1315 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1316 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1317 hfsmp
->hfs_freespace_notify_desiredlevel
=
1318 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1319 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1322 * Set the free space warning levels for the root volume:
1324 * Set the lower freespace limit (the level that will trigger a warning)
1325 * to 1% of the volume size or 50MB, whichever is less, and the desired
1326 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1328 hfsmp
->hfs_freespace_notify_warninglimit
=
1329 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1330 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1331 hfsmp
->hfs_freespace_notify_desiredlevel
=
1332 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1333 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1336 /* Check if the file system exists on virtual device, like disk image */
1337 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, context
) == 0) {
1339 hfsmp
->hfs_flags
|= HFS_VIRTUAL_DEVICE
;
1344 * Start looking for free space to drop below this level and generate a
1345 * warning immediately if needed:
1347 hfsmp
->hfs_notification_conditions
= 0;
1348 hfs_generate_volume_notifications(hfsmp
);
1351 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1362 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1363 (void)VNOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, context
);
1367 if (hfsmp
->hfs_devvp
) {
1368 vnode_rele(hfsmp
->hfs_devvp
);
1370 FREE(hfsmp
, M_HFSMNT
);
1371 vfs_setfsprivate(mp
, NULL
);
1378 * Make a filesystem operational.
1379 * Nothing to do at the moment.
1383 hfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
1390 * unmount system call
1393 hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
)
1395 struct proc
*p
= vfs_context_proc(context
);
1396 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1397 int retval
= E_NONE
;
1404 if (mntflags
& MNT_FORCE
) {
1405 flags
|= FORCECLOSE
;
1409 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1412 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1413 (void) hfs_recording_suspend(hfsmp
);
1416 * Flush out the b-trees, volume bitmap and Volume Header
1418 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1419 retval
= hfs_start_transaction(hfsmp
);
1422 } else if (!force
) {
1426 if (hfsmp
->hfs_startup_vp
) {
1427 (void) hfs_lock(VTOC(hfsmp
->hfs_startup_vp
), HFS_EXCLUSIVE_LOCK
);
1428 retval
= hfs_fsync(hfsmp
->hfs_startup_vp
, MNT_WAIT
, 0, p
);
1429 hfs_unlock(VTOC(hfsmp
->hfs_startup_vp
));
1430 if (retval
&& !force
)
1434 if (hfsmp
->hfs_attribute_vp
) {
1435 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), HFS_EXCLUSIVE_LOCK
);
1436 retval
= hfs_fsync(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, p
);
1437 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
1438 if (retval
&& !force
)
1442 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), HFS_EXCLUSIVE_LOCK
);
1443 retval
= hfs_fsync(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, p
);
1444 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
1445 if (retval
&& !force
)
1448 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
1449 retval
= hfs_fsync(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, p
);
1450 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
1451 if (retval
&& !force
)
1454 if (hfsmp
->hfs_allocation_vp
) {
1455 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
1456 retval
= hfs_fsync(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, p
);
1457 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
1458 if (retval
&& !force
)
1462 if (hfsmp
->hfc_filevp
&& vnode_issystem(hfsmp
->hfc_filevp
)) {
1463 retval
= hfs_fsync(hfsmp
->hfc_filevp
, MNT_WAIT
, 0, p
);
1464 if (retval
&& !force
)
1468 /* If runtime corruption was detected, indicate that the volume
1469 * was not unmounted cleanly.
1471 if (hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
) {
1472 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1474 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1477 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1479 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1481 goto err_exit
; /* could not flush everything */
1485 hfs_end_transaction(hfsmp
);
1491 journal_flush(hfsmp
->jnl
);
1495 * Invalidate our caches and release metadata vnodes
1497 (void) hfsUnmount(hfsmp
, p
);
1500 * Last chance to dump unreferenced system files.
1502 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1504 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1505 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1509 journal_close(hfsmp
->jnl
);
1513 VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
1515 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1516 retval
= VNOP_CLOSE(hfsmp
->jvp
,
1517 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1519 vnode_put(hfsmp
->jvp
);
1524 #ifdef HFS_SPARSE_DEV
1525 /* Drop our reference on the backing fs (if any). */
1526 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1527 struct vnode
* tmpvp
;
1529 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1530 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1531 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1534 #endif /* HFS_SPARSE_DEV */
1535 lck_mtx_destroy(&hfsmp
->hfc_mutex
, hfs_mutex_group
);
1536 vnode_rele(hfsmp
->hfs_devvp
);
1537 FREE(hfsmp
, M_HFSMNT
);
1543 hfs_end_transaction(hfsmp
);
1550 * Return the root of a filesystem.
1553 hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1555 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)kHFSRootFolderID
, vpp
, 1);
1560 * Do operations associated with quotas
1564 hfs_quotactl(__unused
struct mount
*mp
, __unused
int cmds
, __unused uid_t uid
, __unused caddr_t datap
, __unused vfs_context_t context
)
1570 hfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t context
)
1572 struct proc
*p
= vfs_context_proc(context
);
1573 int cmd
, type
, error
;
1576 uid
= vfs_context_ucred(context
)->cr_ruid
;
1577 cmd
= cmds
>> SUBCMDSHIFT
;
1584 if (uid
== vfs_context_ucred(context
)->cr_ruid
)
1588 if ( (error
= vfs_context_suser(context
)) )
1592 type
= cmds
& SUBCMDMASK
;
1593 if ((u_int
)type
>= MAXQUOTAS
)
1595 if (vfs_busy(mp
, LK_NOWAIT
))
1601 error
= hfs_quotaon(p
, mp
, type
, datap
);
1605 error
= hfs_quotaoff(p
, mp
, type
);
1609 error
= hfs_setquota(mp
, uid
, type
, datap
);
1613 error
= hfs_setuse(mp
, uid
, type
, datap
);
1617 error
= hfs_getquota(mp
, uid
, type
, datap
);
1621 error
= hfs_qsync(mp
);
1625 error
= hfs_quotastat(mp
, type
, datap
);
1638 /* Subtype is composite of bits */
1639 #define HFS_SUBTYPE_JOURNALED 0x01
1640 #define HFS_SUBTYPE_CASESENSITIVE 0x02
1641 /* bits 2 - 6 reserved */
1642 #define HFS_SUBTYPE_STANDARDHFS 0x80
1645 * Get file system statistics.
1648 hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
1650 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1651 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1653 u_int16_t subtype
= 0;
1655 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1657 sbp
->f_bsize
= (u_int32_t
)vcb
->blockSize
;
1658 sbp
->f_iosize
= (size_t)cluster_max_io_size(mp
, 0);
1659 sbp
->f_blocks
= (u_int64_t
)((unsigned long)vcb
->totalBlocks
);
1660 sbp
->f_bfree
= (u_int64_t
)((unsigned long )hfs_freeblks(hfsmp
, 0));
1661 sbp
->f_bavail
= (u_int64_t
)((unsigned long )hfs_freeblks(hfsmp
, 1));
1662 sbp
->f_files
= (u_int64_t
)((unsigned long )(vcb
->totalBlocks
- 2)); /* max files is constrained by total blocks */
1663 sbp
->f_ffree
= (u_int64_t
)((unsigned long )(MIN(freeCNIDs
, sbp
->f_bavail
)));
1666 * Subtypes (flavors) for HFS
1667 * 0: Mac OS Extended
1668 * 1: Mac OS Extended (Journaled)
1669 * 2: Mac OS Extended (Case Sensitive)
1670 * 3: Mac OS Extended (Case Sensitive, Journaled)
1672 * 128: Mac OS Standard
1675 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1676 subtype
= HFS_SUBTYPE_STANDARDHFS
;
1677 } else /* HFS Plus */ {
1679 subtype
|= HFS_SUBTYPE_JOURNALED
;
1680 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
1681 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
1683 sbp
->f_fssubtype
= subtype
;
1690 // XXXdbg -- this is a callback to be used by the journal to
1691 // get meta data blocks flushed out to disk.
1693 // XXXdbg -- be smarter and don't flush *every* block on each
1694 // call. try to only flush some so we don't wind up
1695 // being too synchronous.
1699 hfs_sync_metadata(void *arg
)
1701 struct mount
*mp
= (struct mount
*)arg
;
1702 struct hfsmount
*hfsmp
;
1706 daddr64_t priIDSector
;
1707 hfsmp
= VFSTOHFS(mp
);
1708 vcb
= HFSTOVCB(hfsmp
);
1710 // now make sure the super block is flushed
1711 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
1712 HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
));
1714 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
1715 HFS_PHYSBLK_ROUNDDOWN(priIDSector
, hfsmp
->hfs_log_per_phys
),
1716 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
1717 if ((retval
!= 0 ) && (retval
!= ENXIO
)) {
1718 printf("hfs_sync_metadata: can't read volume header at %d! (retval 0x%x)\n",
1719 (int)priIDSector
, retval
);
1722 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1728 // the alternate super block...
1729 // XXXdbg - we probably don't need to do this each and every time.
1730 // hfs_btreeio.c:FlushAlternate() should flag when it was
1732 if (hfsmp
->hfs_alt_id_sector
) {
1733 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
1734 HFS_PHYSBLK_ROUNDDOWN(hfsmp
->hfs_alt_id_sector
, hfsmp
->hfs_log_per_phys
),
1735 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
1736 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1745 struct hfs_sync_cargs
{
1754 hfs_sync_callback(struct vnode
*vp
, void *cargs
)
1757 struct hfs_sync_cargs
*args
;
1760 args
= (struct hfs_sync_cargs
*)cargs
;
1762 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1763 return (VNODE_RETURNED
);
1767 if ((cp
->c_flag
& C_MODIFIED
) ||
1768 (cp
->c_touch_acctime
| cp
->c_touch_chgtime
| cp
->c_touch_modtime
) ||
1769 vnode_hasdirtyblks(vp
)) {
1770 error
= hfs_fsync(vp
, args
->waitfor
, 0, args
->p
);
1773 args
->error
= error
;
1776 return (VNODE_RETURNED
);
1782 * Go through the disk queues to initiate sandbagged IO;
1783 * go through the inodes to write those that have been modified;
1784 * initiate the writing of the super block if it has been modified.
1786 * Note: we are always called with the filesystem marked `MPBUSY'.
1789 hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
)
1791 struct proc
*p
= vfs_context_proc(context
);
1793 struct hfsmount
*hfsmp
;
1795 struct vnode
*meta_vp
[4];
1797 int error
, allerror
= 0;
1798 struct hfs_sync_cargs args
;
1800 hfsmp
= VFSTOHFS(mp
);
1803 * hfs_changefs might be manipulating vnodes so back off
1805 if (hfsmp
->hfs_flags
& HFS_IN_CHANGEFS
)
1808 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1811 /* skip over frozen volumes */
1812 if (!lck_rw_try_lock_shared(&hfsmp
->hfs_insync
))
1815 args
.cred
= kauth_cred_get();
1816 args
.waitfor
= waitfor
;
1820 * hfs_sync_callback will be called for each vnode
1821 * hung off of this mount point... the vnode will be
1822 * properly referenced and unreferenced around the callback
1824 vnode_iterate(mp
, 0, hfs_sync_callback
, (void *)&args
);
1827 allerror
= args
.error
;
1829 vcb
= HFSTOVCB(hfsmp
);
1831 meta_vp
[0] = vcb
->extentsRefNum
;
1832 meta_vp
[1] = vcb
->catalogRefNum
;
1833 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1834 meta_vp
[3] = hfsmp
->hfs_attribute_vp
; /* Optional file */
1836 /* Now sync our three metadata files */
1837 for (i
= 0; i
< 4; ++i
) {
1841 if ((btvp
==0) || (vnode_mount(btvp
) != mp
))
1844 /* XXX use hfs_systemfile_lock instead ? */
1845 (void) hfs_lock(VTOC(btvp
), HFS_EXCLUSIVE_LOCK
);
1848 if (((cp
->c_flag
& C_MODIFIED
) == 0) &&
1849 (cp
->c_touch_acctime
== 0) &&
1850 (cp
->c_touch_chgtime
== 0) &&
1851 (cp
->c_touch_modtime
== 0) &&
1852 vnode_hasdirtyblks(btvp
) == 0) {
1853 hfs_unlock(VTOC(btvp
));
1856 error
= vnode_get(btvp
);
1858 hfs_unlock(VTOC(btvp
));
1861 if ((error
= hfs_fsync(btvp
, waitfor
, 0, p
)))
1869 * Force stale file system control information to be flushed.
1871 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1872 if ((error
= VNOP_FSYNC(hfsmp
->hfs_devvp
, waitfor
, context
))) {
1880 hfs_hotfilesync(hfsmp
, vfs_context_kernel());
1883 * Write back modified superblock.
1885 if (IsVCBDirty(vcb
)) {
1886 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1892 journal_flush(hfsmp
->jnl
);
1895 lck_rw_unlock_shared(&hfsmp
->hfs_insync
);
1901 * File handle to vnode
1903 * Have to be really careful about stale file handles:
1904 * - check that the cnode id is valid
1905 * - call hfs_vget() to get the locked cnode
1906 * - check for an unallocated cnode (i_mode == 0)
1907 * - check that the given client host has export rights and return
1908 * those rights via. exflagsp and credanonp
1911 hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1913 struct hfsfid
*hfsfhp
;
1918 hfsfhp
= (struct hfsfid
*)fhp
;
1920 if (fhlen
< (int)sizeof(struct hfsfid
))
1923 result
= hfs_vget(VFSTOHFS(mp
), ntohl(hfsfhp
->hfsfid_cnid
), &nvp
, 0);
1925 if (result
== ENOENT
)
1930 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1931 * For NFS, we are assuming that only if the createtime was moved
1932 * forward would it mean the fileID got reused in that session by
1933 * wrapping. We don't have a volume ID or other unique identifier to
1934 * to use here for a generation ID across reboots, crashes where
1935 * metadata noting lastFileID didn't make it to disk but client has
1936 * it, or volume erasures where fileIDs start over again. Lastly,
1937 * with HFS allowing "wraps" of fileIDs now, this becomes more
1938 * error prone. Future, would be change the "wrap bit" to a unique
1939 * wrap number and use that for generation number. For now do this.
1941 if (((time_t)(ntohl(hfsfhp
->hfsfid_gen
)) < VTOC(nvp
)->c_itime
)) {
1942 hfs_unlock(VTOC(nvp
));
1948 hfs_unlock(VTOC(nvp
));
1954 * Vnode pointer to File handle
1958 hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, __unused vfs_context_t context
)
1961 struct hfsfid
*hfsfhp
;
1963 if (ISHFS(VTOVCB(vp
)))
1964 return (ENOTSUP
); /* hfs standard is not exportable */
1966 if (*fhlenp
< (int)sizeof(struct hfsfid
))
1970 hfsfhp
= (struct hfsfid
*)fhp
;
1971 hfsfhp
->hfsfid_cnid
= htonl(cp
->c_fileid
);
1972 hfsfhp
->hfsfid_gen
= htonl(cp
->c_itime
);
1973 *fhlenp
= sizeof(struct hfsfid
);
1980 * Initial HFS filesystems, done only once.
1983 hfs_init(__unused
struct vfsconf
*vfsp
)
1985 static int done
= 0;
1991 hfs_converterinit();
1996 hfs_lock_attr
= lck_attr_alloc_init();
1997 hfs_group_attr
= lck_grp_attr_alloc_init();
1998 hfs_mutex_group
= lck_grp_alloc_init("hfs-mutex", hfs_group_attr
);
1999 hfs_rwlock_group
= lck_grp_alloc_init("hfs-rwlock", hfs_group_attr
);
2006 hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
)
2008 struct hfsmount
* hfsmp
;
2009 char fstypename
[MFSNAMELEN
];
2014 if (!vnode_isvroot(vp
))
2017 vnode_vfsname(vp
, fstypename
);
2018 if (strncmp(fstypename
, "hfs", sizeof(fstypename
)) != 0)
2023 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
2032 #include <sys/filedesc.h>
2035 * HFS filesystem related variables.
2038 hfs_sysctl(int *name
, __unused u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2039 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
2041 struct proc
*p
= vfs_context_proc(context
);
2043 struct hfsmount
*hfsmp
;
2045 /* all sysctl names at this level are terminal */
2047 if (name
[0] == HFS_ENCODINGBIAS
) {
2050 bias
= hfs_getencodingbias();
2051 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
2052 if (error
== 0 && newp
)
2053 hfs_setencodingbias(bias
);
2056 } else if (name
[0] == HFS_EXTEND_FS
) {
2058 vnode_t vp
= vfs_context_cwd(context
);
2060 if (newp
== USER_ADDR_NULL
|| vp
== NULLVP
)
2062 if ((error
= hfs_getmountpoint(vp
, &hfsmp
)))
2064 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, (quad_t
*)&newsize
);
2068 error
= hfs_extendfs(hfsmp
, newsize
, context
);
2071 } else if (name
[0] == HFS_ENCODINGHINT
) {
2075 u_int16_t
*unicode_name
;
2078 if ((newlen
<= 0) || (newlen
> MAXPATHLEN
))
2081 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
2082 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
2083 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
2085 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
2087 error
= utf8_decodestr((u_int8_t
*)filename
, newlen
- 1, unicode_name
,
2088 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
2090 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
2091 error
= sysctl_int(oldp
, oldlenp
, USER_ADDR_NULL
, 0, (int32_t *)&hint
);
2094 FREE(unicode_name
, M_TEMP
);
2095 FREE(filename
, M_TEMP
);
2098 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
2099 // make the file system journaled...
2100 vnode_t vp
= vfs_context_cwd(context
);
2103 struct cat_attr jnl_attr
, jinfo_attr
;
2104 struct cat_fork jnl_fork
, jinfo_fork
;
2108 /* Only root can enable journaling */
2116 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2119 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2120 printf("hfs: can't make a plain hfs volume journaled.\n");
2125 printf("hfs: volume @ mp %p is already journaled!\n", vnode_mount(vp
));
2129 vcb
= HFSTOVCB(hfsmp
);
2130 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
2131 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
2132 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
2134 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
2135 hfs_systemfile_unlock(hfsmp
, lockflags
);
2138 hfs_systemfile_unlock(hfsmp
, lockflags
);
2140 // make sure these both exist!
2141 if ( GetFileInfo(vcb
, kHFSRootFolderID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
2142 || GetFileInfo(vcb
, kHFSRootFolderID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2147 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, context
);
2149 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2150 (off_t
)name
[2], (off_t
)name
[3]);
2152 jvp
= hfsmp
->hfs_devvp
;
2153 jnl
= journal_create(jvp
,
2154 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2155 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2156 (off_t
)((unsigned)name
[3]),
2158 hfsmp
->hfs_logical_block_size
,
2161 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2164 printf("hfs: FAILED to create the journal!\n");
2165 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2166 VNOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2173 hfs_global_exclusive_lock_acquire(hfsmp
);
2176 * Flush all dirty metadata buffers.
2178 buf_flushdirtyblks(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, "hfs_sysctl");
2179 buf_flushdirtyblks(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2180 buf_flushdirtyblks(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2181 buf_flushdirtyblks(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2182 if (hfsmp
->hfs_attribute_vp
)
2183 buf_flushdirtyblks(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2185 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2186 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2190 // save this off for the hack-y check in hfs_remove()
2191 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2192 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2193 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2194 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2196 vfs_setflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
2198 hfs_global_exclusive_lock_release(hfsmp
);
2199 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2202 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2203 // clear the journaling bit
2204 vnode_t vp
= vfs_context_cwd(context
);
2206 /* Only root can disable journaling */
2216 * Disabling journaling is disallowed on volumes with directory hard links
2217 * because we have not tested the relevant code path.
2219 if (hfsmp
->hfs_private_attr
[DIR_HARDLINKS
].ca_entries
!= 0){
2220 printf("hfs: cannot disable journaling on volumes with directory hardlinks\n");
2224 printf("hfs: disabling journaling for mount @ %p\n", vnode_mount(vp
));
2226 hfs_global_exclusive_lock_acquire(hfsmp
);
2228 // Lights out for you buddy!
2229 journal_close(hfsmp
->jnl
);
2232 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2233 VNOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2236 vfs_clearflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
2237 hfsmp
->jnl_start
= 0;
2238 hfsmp
->hfs_jnlinfoblkid
= 0;
2239 hfsmp
->hfs_jnlfileid
= 0;
2241 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2243 hfs_global_exclusive_lock_release(hfsmp
);
2244 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2247 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2248 vnode_t vp
= vfs_context_cwd(context
);
2249 off_t jnl_start
, jnl_size
;
2255 if (hfsmp
->jnl
== NULL
) {
2259 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2260 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2263 if ((error
= copyout((caddr_t
)&jnl_start
, CAST_USER_ADDR_T(name
[1]), sizeof(off_t
))) != 0) {
2266 if ((error
= copyout((caddr_t
)&jnl_size
, CAST_USER_ADDR_T(name
[2]), sizeof(off_t
))) != 0) {
2271 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2273 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2275 } else if (name
[0] == VFS_CTL_QUERY
) {
2276 struct sysctl_req
*req
;
2278 struct user_vfsidctl user_vc
;
2281 boolean_t is_64_bit
;
2283 is_64_bit
= proc_is64bit(p
);
2284 req
= CAST_DOWN(struct sysctl_req
*, oldp
); /* we're new style vfs sysctl. */
2287 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2288 if (error
) return (error
);
2290 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
2293 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2294 if (error
) return (error
);
2296 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
));;
2304 } else if (name
[0] == HFS_REPLAY_JOURNAL
) {
2305 char *devnode
= NULL
;
2308 devnode_len
= *oldlenp
;
2309 MALLOC(devnode
, char *, devnode_len
+ 1, M_TEMP
, M_WAITOK
);
2310 if (devnode
== NULL
) {
2314 error
= copyin(oldp
, (caddr_t
)devnode
, devnode_len
);
2316 FREE(devnode
, M_TEMP
);
2319 devnode
[devnode_len
] = 0;
2321 error
= hfs_journal_replay(devnode
, context
);
2322 FREE(devnode
, M_TEMP
);
2329 /* hfs_vfs_vget is not static since it is used in hfs_readwrite.c to support the
2330 * build_path ioctl. We use it to leverage the code below that updates the origin
2331 * cache if necessary.
2334 hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, __unused vfs_context_t context
)
2338 struct hfsmount
*hfsmp
;
2340 hfsmp
= VFSTOHFS(mp
);
2342 error
= hfs_vget(hfsmp
, (cnid_t
)ino
, vpp
, 1);
2347 * ADLs may need to have their origin state updated
2348 * since build_path needs a valid parent. The same is true
2349 * for hardlinked files as well. There isn't a race window here in re-acquiring
2350 * the cnode lock since we aren't pulling any data out of the cnode; instead, we're
2351 * going back to the catalog.
2353 if ((VTOC(*vpp
)->c_flag
& C_HARDLINK
) &&
2354 (hfs_lock(VTOC(*vpp
), HFS_EXCLUSIVE_LOCK
) == 0)) {
2355 cnode_t
*cp
= VTOC(*vpp
);
2356 struct cat_desc cdesc
;
2358 if (!hfs_haslinkorigin(cp
)) {
2359 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2360 error
= cat_findname(hfsmp
, (cnid_t
)ino
, &cdesc
);
2361 hfs_systemfile_unlock(hfsmp
, lockflags
);
2363 if ((cdesc
.cd_parentcnid
!=
2364 hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) &&
2365 (cdesc
.cd_parentcnid
!=
2366 hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
)) {
2367 hfs_savelinkorigin(cp
, cdesc
.cd_parentcnid
);
2369 cat_releasedesc(&cdesc
);
2379 * Look up an HFS object by ID.
2381 * The object is returned with an iocount reference and the cnode locked.
2383 * If the object is a file then it will represent the data fork.
2387 hfs_vget(struct hfsmount
*hfsmp
, cnid_t cnid
, struct vnode
**vpp
, int skiplock
)
2389 struct vnode
*vp
= NULLVP
;
2390 struct cat_desc cndesc
;
2391 struct cat_attr cnattr
;
2392 struct cat_fork cnfork
;
2393 u_int32_t linkref
= 0;
2396 /* Check for cnids that should't be exported. */
2397 if ((cnid
< kHFSFirstUserCatalogNodeID
) &&
2398 (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
)) {
2401 /* Don't export our private directories. */
2402 if (cnid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
||
2403 cnid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
2407 * Check the hash first
2409 vp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, cnid
, 0, skiplock
);
2415 bzero(&cndesc
, sizeof(cndesc
));
2416 bzero(&cnattr
, sizeof(cnattr
));
2417 bzero(&cnfork
, sizeof(cnfork
));
2420 * Not in hash, lookup in catalog
2422 if (cnid
== kHFSRootParentID
) {
2423 static char hfs_rootname
[] = "/";
2425 cndesc
.cd_nameptr
= (const u_int8_t
*)&hfs_rootname
[0];
2426 cndesc
.cd_namelen
= 1;
2427 cndesc
.cd_parentcnid
= kHFSRootParentID
;
2428 cndesc
.cd_cnid
= kHFSRootFolderID
;
2429 cndesc
.cd_flags
= CD_ISDIR
;
2431 cnattr
.ca_fileid
= kHFSRootFolderID
;
2432 cnattr
.ca_linkcount
= 1;
2433 cnattr
.ca_entries
= 1;
2434 cnattr
.ca_dircount
= 1;
2435 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
2439 const char *nameptr
;
2441 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2442 error
= cat_idlookup(hfsmp
, cnid
, 0, &cndesc
, &cnattr
, &cnfork
);
2443 hfs_systemfile_unlock(hfsmp
, lockflags
);
2451 * Check for a raw hardlink inode and save its linkref.
2453 pid
= cndesc
.cd_parentcnid
;
2454 nameptr
= (const char *)cndesc
.cd_nameptr
;
2456 if ((pid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2457 (bcmp(nameptr
, HFS_INODE_PREFIX
, HFS_INODE_PREFIX_LEN
) == 0)) {
2458 linkref
= strtoul(&nameptr
[HFS_INODE_PREFIX_LEN
], NULL
, 10);
2460 } else if ((pid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) &&
2461 (bcmp(nameptr
, HFS_DIRINODE_PREFIX
, HFS_DIRINODE_PREFIX_LEN
) == 0)) {
2462 linkref
= strtoul(&nameptr
[HFS_DIRINODE_PREFIX_LEN
], NULL
, 10);
2464 } else if ((pid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2465 (bcmp(nameptr
, HFS_DELETE_PREFIX
, HFS_DELETE_PREFIX_LEN
) == 0)) {
2467 cat_releasedesc(&cndesc
);
2468 return (ENOENT
); /* open unlinked file */
2473 * Finish initializing cnode descriptor for hardlinks.
2475 * We need a valid name and parent for reverse lookups.
2480 struct cat_desc linkdesc
;
2483 cnattr
.ca_linkref
= linkref
;
2486 * Pick up the first link in the chain and get a descriptor for it.
2487 * This allows blind volfs paths to work for hardlinks.
2489 if ((hfs_lookuplink(hfsmp
, linkref
, &prevlinkid
, &nextlinkid
) == 0) &&
2490 (nextlinkid
!= 0)) {
2491 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2492 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
2493 hfs_systemfile_unlock(hfsmp
, lockflags
);
2495 cat_releasedesc(&cndesc
);
2496 bcopy(&linkdesc
, &cndesc
, sizeof(linkdesc
));
2502 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2504 VTOC(vp
)->c_flag
|= C_HARDLINK
;
2505 vnode_setmultipath(vp
);
2508 struct componentname cn
;
2510 /* Supply hfs_getnewvnode with a component name. */
2511 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2512 cn
.cn_nameiop
= LOOKUP
;
2513 cn
.cn_flags
= ISLASTCN
| HASBUF
;
2514 cn
.cn_context
= NULL
;
2515 cn
.cn_pnlen
= MAXPATHLEN
;
2516 cn
.cn_nameptr
= cn
.cn_pnbuf
;
2517 cn
.cn_namelen
= cndesc
.cd_namelen
;
2520 bcopy(cndesc
.cd_nameptr
, cn
.cn_nameptr
, cndesc
.cd_namelen
+ 1);
2522 error
= hfs_getnewvnode(hfsmp
, NULLVP
, &cn
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2524 if ((error
== 0) && (VTOC(vp
)->c_flag
& C_HARDLINK
)) {
2525 hfs_savelinkorigin(VTOC(vp
), cndesc
.cd_parentcnid
);
2527 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
2529 cat_releasedesc(&cndesc
);
2532 if (vp
&& skiplock
) {
2533 hfs_unlock(VTOC(vp
));
2540 * Flush out all the files in a filesystem.
2544 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2546 hfs_flushfiles(struct mount
*mp
, int flags
, __unused
struct proc
*p
)
2549 struct hfsmount
*hfsmp
;
2550 struct vnode
*skipvp
= NULLVP
;
2557 hfsmp
= VFSTOHFS(mp
);
2561 * The open quota files have an indirect reference on
2562 * the root directory vnode. We must account for this
2563 * extra reference when doing the intial vflush.
2566 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2568 /* Find out how many quota files we have open. */
2569 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2570 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2574 /* Obtain the root vnode so we can skip over it. */
2575 skipvp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, kHFSRootFolderID
, 0, 0);
2579 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2583 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2586 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2589 * See if there are additional references on the
2590 * root vp besides the ones obtained from the open
2591 * quota files and the hfs_chash_getvnode call above.
2594 (vnode_isinuse(skipvp
, quotafilecnt
))) {
2595 error
= EBUSY
; /* root directory is still open */
2597 hfs_unlock(VTOC(skipvp
));
2600 if (error
&& (flags
& FORCECLOSE
) == 0)
2603 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2604 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2606 hfs_quotaoff(p
, mp
, i
);
2608 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2616 * Update volume encoding bitmap (HFS Plus only)
2620 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2622 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2623 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2628 case kTextEncodingMacUkrainian
:
2629 index
= kIndexMacUkrainian
;
2631 case kTextEncodingMacFarsi
:
2632 index
= kIndexMacFarsi
;
2639 if (index
< 64 && (hfsmp
->encodingsBitmap
& (u_int64_t
)(1ULL << index
)) == 0) {
2640 HFS_MOUNT_LOCK(hfsmp
, TRUE
)
2641 hfsmp
->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2642 MarkVCBDirty(hfsmp
);
2643 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2648 * Update volume stats
2650 * On journal volumes this will cause a volume header flush
2654 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2660 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2662 MarkVCBDirty(hfsmp
);
2663 hfsmp
->hfs_mtime
= tv
.tv_sec
;
2669 if (hfsmp
->hfs_dircount
!= 0xFFFFFFFF)
2670 ++hfsmp
->hfs_dircount
;
2671 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2672 ++hfsmp
->vcbNmRtDirs
;
2675 if (hfsmp
->hfs_dircount
!= 0)
2676 --hfsmp
->hfs_dircount
;
2677 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2678 --hfsmp
->vcbNmRtDirs
;
2681 if (hfsmp
->hfs_filecount
!= 0xFFFFFFFF)
2682 ++hfsmp
->hfs_filecount
;
2683 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2687 if (hfsmp
->hfs_filecount
!= 0)
2688 --hfsmp
->hfs_filecount
;
2689 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2694 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2697 hfs_flushvolumeheader(hfsmp
, 0, 0);
2705 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2707 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2708 struct filefork
*fp
;
2709 HFSMasterDirectoryBlock
*mdb
;
2710 struct buf
*bp
= NULL
;
2715 sectorsize
= hfsmp
->hfs_logical_block_size
;
2716 retval
= (int)buf_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2723 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2725 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2727 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2728 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2729 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2730 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2731 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2732 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2733 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2734 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2736 namelen
= strlen((char *)vcb
->vcbVN
);
2737 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2738 /* Retry with MacRoman in case that's how it was exported. */
2740 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2742 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2743 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2744 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2745 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2746 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2748 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2750 fp
= VTOF(vcb
->extentsRefNum
);
2751 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2752 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2753 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2754 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2755 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2756 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2757 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2758 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2759 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2761 fp
= VTOF(vcb
->catalogRefNum
);
2762 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2763 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2764 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2765 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2766 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2767 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2768 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2769 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2770 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2772 MarkVCBClean( vcb
);
2774 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2776 /* If requested, flush out the alternate MDB */
2778 struct buf
*alt_bp
= NULL
;
2780 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2781 bcopy(mdb
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2783 (void) VNOP_BWRITE(alt_bp
);
2788 if (waitfor
!= MNT_WAIT
)
2791 retval
= VNOP_BWRITE(bp
);
2797 * Flush any dirty in-memory mount data to the on-disk
2800 * Note: the on-disk volume signature is intentionally
2801 * not flushed since the on-disk "H+" and "HX" signatures
2802 * are always stored in-memory as "H+".
2806 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2808 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2809 struct filefork
*fp
;
2810 HFSPlusVolumeHeader
*volumeHeader
;
2814 daddr64_t priIDSector
;
2816 u_int16_t signature
;
2817 u_int16_t hfsversion
;
2819 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2822 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
2823 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2825 critical
= altflush
;
2826 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
2827 HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
));
2829 if (hfs_start_transaction(hfsmp
) != 0) {
2833 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
2834 HFS_PHYSBLK_ROUNDDOWN(priIDSector
, hfsmp
->hfs_log_per_phys
),
2835 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
2840 hfs_end_transaction(hfsmp
);
2842 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2847 journal_modify_block_start(hfsmp
->jnl
, bp
);
2850 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)buf_dataptr(bp
) +
2851 HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
2854 * Sanity check what we just read.
2856 signature
= SWAP_BE16 (volumeHeader
->signature
);
2857 hfsversion
= SWAP_BE16 (volumeHeader
->version
);
2858 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2859 (hfsversion
< kHFSPlusVersion
) || (hfsversion
> 100) ||
2860 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2862 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2863 vcb
->vcbVN
, signature
, hfsversion
,
2864 SWAP_BE32 (volumeHeader
->blockSize
));
2866 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2872 * For embedded HFS+ volumes, update create date if it changed
2873 * (ie from a setattrlist call)
2875 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2876 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2878 HFSMasterDirectoryBlock
*mdb
;
2880 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
2881 HFS_PHYSBLK_ROUNDDOWN(HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
), hfsmp
->hfs_log_per_phys
),
2882 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp2
);
2888 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp2
) +
2889 HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
2891 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2894 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2897 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2900 journal_modify_block_end(hfsmp
->jnl
, bp2
, NULL
, NULL
);
2902 (void) VNOP_BWRITE(bp2
); /* write out the changes */
2907 buf_brelse(bp2
); /* just release it */
2912 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2914 /* Note: only update the lower 16 bits worth of attributes */
2915 volumeHeader
->attributes
= SWAP_BE32 (vcb
->vcbAtrb
);
2916 volumeHeader
->journalInfoBlock
= SWAP_BE32 (vcb
->vcbJinfoBlock
);
2918 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2920 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2922 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2923 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2924 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2925 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2926 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2927 volumeHeader
->totalBlocks
= SWAP_BE32 (vcb
->totalBlocks
);
2928 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2929 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2930 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2931 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2932 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2933 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2934 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2936 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0) {
2937 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2942 * System files are only dirty when altflush is set.
2944 if (altflush
== 0) {
2948 /* Sync Extents over-flow file meta data */
2949 fp
= VTOF(vcb
->extentsRefNum
);
2950 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2951 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2952 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2953 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2954 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2955 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2957 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2958 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2959 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2960 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2963 /* Sync Catalog file meta data */
2964 fp
= VTOF(vcb
->catalogRefNum
);
2965 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2966 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2967 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2968 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2969 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2970 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2972 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2973 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2974 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2975 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2978 /* Sync Allocation file meta data */
2979 fp
= VTOF(vcb
->allocationsRefNum
);
2980 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2981 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2982 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2983 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2984 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2985 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2987 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2988 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2989 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2990 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2993 /* Sync Attribute file meta data */
2994 if (hfsmp
->hfs_attribute_vp
) {
2995 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
2996 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2997 volumeHeader
->attributesFile
.extents
[i
].startBlock
=
2998 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2999 volumeHeader
->attributesFile
.extents
[i
].blockCount
=
3000 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
3002 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3003 volumeHeader
->attributesFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
3004 volumeHeader
->attributesFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
3005 volumeHeader
->attributesFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
3008 /* Sync Startup file meta data */
3009 if (hfsmp
->hfs_startup_vp
) {
3010 fp
= VTOF(hfsmp
->hfs_startup_vp
);
3011 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
3012 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
3013 volumeHeader
->startupFile
.extents
[i
].startBlock
=
3014 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
3015 volumeHeader
->startupFile
.extents
[i
].blockCount
=
3016 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
3018 volumeHeader
->startupFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
3019 volumeHeader
->startupFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
3020 volumeHeader
->startupFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
3021 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3026 MarkVCBClean(hfsmp
);
3027 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3029 /* If requested, flush out the alternate volume header */
3030 if (altflush
&& hfsmp
->hfs_alt_id_sector
) {
3031 struct buf
*alt_bp
= NULL
;
3033 if (buf_meta_bread(hfsmp
->hfs_devvp
,
3034 HFS_PHYSBLK_ROUNDDOWN(hfsmp
->hfs_alt_id_sector
, hfsmp
->hfs_log_per_phys
),
3035 hfsmp
->hfs_physical_block_size
, NOCRED
, &alt_bp
) == 0) {
3037 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
3040 bcopy(volumeHeader
, (char *)buf_dataptr(alt_bp
) +
3041 HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
),
3045 journal_modify_block_end(hfsmp
->jnl
, alt_bp
, NULL
, NULL
);
3047 (void) VNOP_BWRITE(alt_bp
);
3054 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
3056 if (waitfor
!= MNT_WAIT
)
3059 retval
= VNOP_BWRITE(bp
);
3060 /* When critical data changes, flush the device cache */
3061 if (critical
&& (retval
== 0)) {
3062 (void) VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
3063 NULL
, FWRITE
, NULL
);
3067 hfs_end_transaction(hfsmp
);
3074 * Extend a file system.
3078 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
3080 struct proc
*p
= vfs_context_proc(context
);
3081 kauth_cred_t cred
= vfs_context_ucred(context
);
3083 struct vnode
*devvp
;
3085 struct filefork
*fp
= NULL
;
3087 struct cat_fork forkdata
;
3089 u_int64_t newblkcnt
;
3090 u_int64_t prev_phys_block_count
;
3092 u_int64_t sectorcnt
;
3093 u_int32_t sectorsize
;
3094 u_int32_t phys_sectorsize
;
3095 daddr64_t prev_alt_sector
;
3099 int64_t oldBitmapSize
;
3100 Boolean usedExtendFileC
= false;
3102 devvp
= hfsmp
->hfs_devvp
;
3103 vcb
= HFSTOVCB(hfsmp
);
3106 * - HFS Plus file systems only.
3107 * - Journaling must be enabled.
3108 * - No embedded volumes.
3110 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
3111 (hfsmp
->jnl
== NULL
) ||
3112 (vcb
->hfsPlusIOPosOffset
!= 0)) {
3116 * If extending file system by non-root, then verify
3117 * ownership and check permissions.
3119 if (suser(cred
, NULL
)) {
3120 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0);
3124 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
3126 error
= hfs_write_access(vp
, cred
, p
, false);
3128 hfs_unlock(VTOC(vp
));
3133 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
3137 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, context
)) {
3140 if (sectorsize
!= hfsmp
->hfs_logical_block_size
) {
3143 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, context
)) {
3146 if ((sectorsize
* sectorcnt
) < newsize
) {
3147 printf("hfs_extendfs: not enough space on device\n");
3150 error
= VNOP_IOCTL(devvp
, DKIOCGETPHYSICALBLOCKSIZE
, (caddr_t
)&phys_sectorsize
, 0, context
);
3152 if ((error
!= ENOTSUP
) && (error
!= ENOTTY
)) {
3155 /* If ioctl is not supported, force physical and logical sector size to be same */
3156 phys_sectorsize
= sectorsize
;
3158 if (phys_sectorsize
!= hfsmp
->hfs_physical_block_size
) {
3161 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3164 * Validate new size.
3166 if ((newsize
<= oldsize
) || (newsize
% sectorsize
) || (newsize
% phys_sectorsize
)) {
3167 printf("hfs_extendfs: invalid size\n");
3170 newblkcnt
= newsize
/ vcb
->blockSize
;
3171 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
3174 addblks
= newblkcnt
- vcb
->totalBlocks
;
3176 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
3178 * Enclose changes inside a transaction.
3180 if (hfs_start_transaction(hfsmp
) != 0) {
3185 * Note: we take the attributes lock in case we have an attribute data vnode
3186 * which needs to change size.
3188 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3189 vp
= vcb
->allocationsRefNum
;
3191 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
3194 * Calculate additional space required (if any) by allocation bitmap.
3196 oldBitmapSize
= fp
->ff_size
;
3197 bitmapblks
= roundup((newblkcnt
+7) / 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
3198 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
3199 bitmapblks
-= fp
->ff_blocks
;
3203 if (bitmapblks
> 0) {
3209 * Get the bitmap's current size (in allocation blocks) so we know
3210 * where to start zero filling once the new space is added. We've
3211 * got to do this before the bitmap is grown.
3213 blkno
= (daddr64_t
)fp
->ff_blocks
;
3216 * Try to grow the allocation file in the normal way, using allocation
3217 * blocks already existing in the file system. This way, we might be
3218 * able to grow the bitmap contiguously, or at least in the metadata
3221 error
= ExtendFileC(vcb
, fp
, bitmapblks
* vcb
->blockSize
, 0,
3222 kEFAllMask
| kEFNoClumpMask
| kEFReserveMask
| kEFMetadataMask
,
3226 usedExtendFileC
= true;
3229 * If the above allocation failed, fall back to allocating the new
3230 * extent of the bitmap from the space we're going to add. Since those
3231 * blocks don't yet belong to the file system, we have to update the
3232 * extent list directly, and manually adjust the file size.
3235 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
3237 printf("hfs_extendfs: error %d adding extents\n", error
);
3240 fp
->ff_blocks
+= bitmapblks
;
3241 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3242 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3246 * Update the allocation file's size to include the newly allocated
3247 * blocks. Note that ExtendFileC doesn't do this, which is why this
3248 * statement is outside the above "if" statement.
3250 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3253 * Zero out the new bitmap blocks.
3258 blkcnt
= bitmapblks
;
3259 while (blkcnt
> 0) {
3260 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
3267 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
3269 error
= (int)buf_bwrite(bp
);
3277 printf("hfs_extendfs: error %d clearing blocks\n", error
);
3281 * Mark the new bitmap space as allocated.
3283 * Note that ExtendFileC will have marked any blocks it allocated, so
3284 * this is only needed if we used AddFileExtent. Also note that this
3285 * has to come *after* the zero filling of new blocks in the case where
3286 * we used AddFileExtent (since the part of the bitmap we're touching
3287 * is in those newly allocated blocks).
3289 if (!usedExtendFileC
) {
3290 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
3292 printf("hfs_extendfs: error %d setting bitmap\n", error
);
3295 vcb
->freeBlocks
-= bitmapblks
;
3299 * Mark the new alternate VH as allocated.
3301 if (vcb
->blockSize
== 512)
3302 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
3304 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
3306 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
3310 * Mark the old alternate VH as free.
3312 if (vcb
->blockSize
== 512)
3313 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
3315 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
3317 * Adjust file system variables for new space.
3319 prev_phys_block_count
= hfsmp
->hfs_logical_block_count
;
3320 prev_alt_sector
= hfsmp
->hfs_alt_id_sector
;
3322 vcb
->totalBlocks
+= addblks
;
3323 vcb
->freeBlocks
+= addblks
;
3324 hfsmp
->hfs_logical_block_count
= newsize
/ sectorsize
;
3325 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sectorsize
) +
3326 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_logical_block_count
);
3328 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3330 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
3332 * Restore to old state.
3334 if (usedExtendFileC
) {
3335 (void) TruncateFileC(vcb
, fp
, oldBitmapSize
, false);
3337 fp
->ff_blocks
-= bitmapblks
;
3338 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3340 * No need to mark the excess blocks free since those bitmap blocks
3341 * are no longer part of the bitmap. But we do need to undo the
3342 * effect of the "vcb->freeBlocks -= bitmapblks" above.
3344 vcb
->freeBlocks
+= bitmapblks
;
3346 vcb
->totalBlocks
-= addblks
;
3347 vcb
->freeBlocks
-= addblks
;
3348 hfsmp
->hfs_logical_block_count
= prev_phys_block_count
;
3349 hfsmp
->hfs_alt_id_sector
= prev_alt_sector
;
3351 if (vcb
->blockSize
== 512)
3352 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
3354 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
3358 * Invalidate the old alternate volume header.
3361 if (prev_alt_sector
) {
3362 if (buf_meta_bread(hfsmp
->hfs_devvp
,
3363 HFS_PHYSBLK_ROUNDDOWN(prev_alt_sector
, hfsmp
->hfs_log_per_phys
),
3364 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
) == 0) {
3365 journal_modify_block_start(hfsmp
->jnl
, bp
);
3367 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
), kMDBSize
);
3369 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
3376 * TODO: Adjust the size of the metadata zone based on new volume size?
3380 * Adjust the size of hfsmp->hfs_attrdata_vp
3382 if (hfsmp
->hfs_attrdata_vp
) {
3383 struct cnode
*attr_cp
;
3384 struct filefork
*attr_fp
;
3386 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
3387 attr_cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
3388 attr_fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
3390 attr_cp
->c_blocks
= newblkcnt
;
3391 attr_fp
->ff_blocks
= newblkcnt
;
3392 attr_fp
->ff_extents
[0].blockCount
= newblkcnt
;
3393 attr_fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
3394 ubc_setsize(hfsmp
->hfs_attrdata_vp
, attr_fp
->ff_size
);
3395 vnode_put(hfsmp
->hfs_attrdata_vp
);
3401 /* Restore allocation fork. */
3402 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3403 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3407 Regardless of whether or not the totalblocks actually increased,
3408 we should reset the allocLimit field. If it changed, it will
3409 get updated; if not, it will remain the same.
3411 hfsmp
->allocLimit
= vcb
->totalBlocks
;
3412 hfs_systemfile_unlock(hfsmp
, lockflags
);
3413 hfs_end_transaction(hfsmp
);
3418 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3421 * Truncate a file system (while still mounted).
3425 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
3427 struct buf
*bp
= NULL
;
3429 u_int32_t newblkcnt
;
3430 u_int32_t reclaimblks
= 0;
3432 int transaction_begun
= 0;
3435 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3436 if (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) {
3437 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3440 hfsmp
->hfs_flags
|= HFS_RESIZE_IN_PROGRESS
;
3441 hfsmp
->hfs_resize_filesmoved
= 0;
3442 hfsmp
->hfs_resize_totalfiles
= 0;
3443 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3446 * - Journaled HFS Plus volumes only.
3447 * - No embedded volumes.
3449 if ((hfsmp
->jnl
== NULL
) ||
3450 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
3454 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3455 newblkcnt
= newsize
/ hfsmp
->blockSize
;
3456 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
3458 /* Make sure new size is valid. */
3459 if ((newsize
< HFS_MIN_SIZE
) ||
3460 (newsize
>= oldsize
) ||
3461 (newsize
% hfsmp
->hfs_logical_block_size
) ||
3462 (newsize
% hfsmp
->hfs_physical_block_size
)) {
3463 printf ("hfs_truncatefs: invalid size\n");
3467 /* Make sure there's enough space to work with. */
3468 if (reclaimblks
>= hfs_freeblks(hfsmp
, 1)) {
3469 printf("hfs_truncatefs: insufficient space (need %u blocks; have %u blocks)\n", reclaimblks
, hfs_freeblks(hfsmp
, 1));
3474 /* Start with a clean journal. */
3475 journal_flush(hfsmp
->jnl
);
3477 if (hfs_start_transaction(hfsmp
) != 0) {
3481 transaction_begun
= 1;
3484 * Prevent new allocations from using the part we're trying to truncate.
3486 * NOTE: allocLimit is set to the allocation block number where the new
3487 * alternate volume header will be. That way there will be no files to
3488 * interfere with allocating the new alternate volume header, and no files
3489 * in the allocation blocks beyond (i.e. the blocks we're trying to
3492 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3493 if (hfsmp
->blockSize
== 512)
3494 hfsmp
->allocLimit
= newblkcnt
- 2;
3496 hfsmp
->allocLimit
= newblkcnt
- 1;
3497 hfsmp
->freeBlocks
-= reclaimblks
;
3498 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3501 * Look for files that have blocks at or beyond the location of the
3502 * new alternate volume header.
3504 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
3506 * hfs_reclaimspace will use separate transactions when
3507 * relocating files (so we don't overwhelm the journal).
3509 hfs_end_transaction(hfsmp
);
3510 transaction_begun
= 0;
3512 /* Attempt to reclaim some space. */
3513 if (hfs_reclaimspace(hfsmp
, hfsmp
->allocLimit
, reclaimblks
, context
) != 0) {
3514 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp
->vcbVN
);
3518 if (hfs_start_transaction(hfsmp
) != 0) {
3522 transaction_begun
= 1;
3524 /* Check if we're clear now. */
3525 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
3526 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp
->vcbVN
);
3527 error
= EAGAIN
; /* tell client to try again */
3533 * Note: we take the attributes lock in case we have an attribute data vnode
3534 * which needs to change size.
3536 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3539 * Mark the old alternate volume header as free.
3540 * We don't bother shrinking allocation bitmap file.
3542 if (hfsmp
->blockSize
== 512)
3543 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
3545 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
3548 * Allocate last 1KB for alternate volume header.
3550 error
= BlockMarkAllocated(hfsmp
, hfsmp
->allocLimit
, (hfsmp
->blockSize
== 512) ? 2 : 1);
3552 printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error
);
3557 * Invalidate the existing alternate volume header.
3559 * Don't include this in a transaction (don't call journal_modify_block)
3560 * since this block will be outside of the truncated file system!
3562 if (hfsmp
->hfs_alt_id_sector
) {
3563 if (buf_meta_bread(hfsmp
->hfs_devvp
,
3564 HFS_PHYSBLK_ROUNDDOWN(hfsmp
->hfs_alt_id_sector
, hfsmp
->hfs_log_per_phys
),
3565 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
) == 0) {
3567 bzero((void*)((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
)), kMDBSize
);
3568 (void) VNOP_BWRITE(bp
);
3575 /* Log successful shrinking. */
3576 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
3577 hfsmp
->vcbVN
, newblkcnt
, hfsmp
->totalBlocks
);
3580 * Adjust file system variables and flush them to disk.
3582 hfsmp
->totalBlocks
= newblkcnt
;
3583 hfsmp
->hfs_logical_block_count
= newsize
/ hfsmp
->hfs_logical_block_size
;
3584 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
, hfsmp
->hfs_logical_block_count
);
3585 MarkVCBDirty(hfsmp
);
3586 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3588 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
3591 * TODO: Adjust the size of the metadata zone based on new volume size?
3595 * Adjust the size of hfsmp->hfs_attrdata_vp
3597 if (hfsmp
->hfs_attrdata_vp
) {
3599 struct filefork
*fp
;
3601 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
3602 cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
3603 fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
3605 cp
->c_blocks
= newblkcnt
;
3606 fp
->ff_blocks
= newblkcnt
;
3607 fp
->ff_extents
[0].blockCount
= newblkcnt
;
3608 fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
3609 ubc_setsize(hfsmp
->hfs_attrdata_vp
, fp
->ff_size
);
3610 vnode_put(hfsmp
->hfs_attrdata_vp
);
3616 hfsmp
->freeBlocks
+= reclaimblks
;
3618 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3619 hfsmp
->allocLimit
= hfsmp
->totalBlocks
;
3620 if (hfsmp
->nextAllocation
>= hfsmp
->allocLimit
)
3621 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_end
+ 1;
3622 hfsmp
->hfs_flags
&= ~HFS_RESIZE_IN_PROGRESS
;
3623 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3626 hfs_systemfile_unlock(hfsmp
, lockflags
);
3628 if (transaction_begun
) {
3629 hfs_end_transaction(hfsmp
);
3630 journal_flush(hfsmp
->jnl
);
3638 * Invalidate the physical block numbers associated with buffer cache blocks
3639 * in the given extent of the given vnode.
3641 struct hfs_inval_blk_no
{
3642 daddr64_t sectorStart
;
3643 daddr64_t sectorCount
;
3646 hfs_invalidate_block_numbers_callback(buf_t bp
, void *args_in
)
3649 struct hfs_inval_blk_no
*args
;
3651 blkno
= buf_blkno(bp
);
3654 if (blkno
>= args
->sectorStart
&& blkno
< args
->sectorStart
+args
->sectorCount
)
3655 buf_setblkno(bp
, buf_lblkno(bp
));
3657 return BUF_RETURNED
;
3660 hfs_invalidate_sectors(struct vnode
*vp
, daddr64_t sectorStart
, daddr64_t sectorCount
)
3662 struct hfs_inval_blk_no args
;
3663 args
.sectorStart
= sectorStart
;
3664 args
.sectorCount
= sectorCount
;
3666 buf_iterate(vp
, hfs_invalidate_block_numbers_callback
, BUF_SCAN_DIRTY
|BUF_SCAN_CLEAN
, &args
);
3671 * Copy the contents of an extent to a new location. Also invalidates the
3672 * physical block number of any buffer cache block in the copied extent
3673 * (so that if the block is written, it will go through VNOP_BLOCKMAP to
3674 * determine the new physical block number).
3678 struct hfsmount
*hfsmp
,
3679 struct vnode
*vp
, /* The file whose extent is being copied. */
3680 u_int32_t oldStart
, /* The start of the source extent. */
3681 u_int32_t newStart
, /* The start of the destination extent. */
3682 u_int32_t blockCount
, /* The number of allocation blocks to copy. */
3683 vfs_context_t context
)
3687 void *buffer
= NULL
;
3688 struct vfsioattr ioattr
;
3692 u_int32_t ioSizeSectors
; /* Device sectors in this I/O */
3693 daddr64_t srcSector
, destSector
;
3694 u_int32_t sectorsPerBlock
= hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
3697 * Sanity check that we have locked the vnode of the file we're copying.
3699 * But since hfs_systemfile_lock() doesn't actually take the lock on
3700 * the allocation file if a journal is active, ignore the check if the
3701 * file being copied is the allocation file.
3703 struct cnode
*cp
= VTOC(vp
);
3704 if (cp
!= hfsmp
->hfs_allocation_cp
&& cp
->c_lockowner
!= current_thread())
3705 panic("hfs_copy_extent: vp=%p (cp=%p) not owned?\n", vp
, cp
);
3708 * Wait for any in-progress writes to this vnode to complete, so that we'll
3709 * be copying consistent bits. (Otherwise, it's possible that an async
3710 * write will complete to the old extent after we read from it. That
3711 * could lead to corruption.)
3713 err
= vnode_waitforwrites(vp
, 0, 0, 0, "hfs_copy_extent");
3715 printf("hfs_copy_extent: Error %d from vnode_waitforwrites\n", err
);
3720 * Determine the I/O size to use
3722 * NOTE: Many external drives will result in an ioSize of 128KB.
3723 * TODO: Should we use a larger buffer, doing several consecutive
3724 * reads, then several consecutive writes?
3726 vfs_ioattr(hfsmp
->hfs_mp
, &ioattr
);
3727 bufferSize
= MIN(ioattr
.io_maxreadcnt
, ioattr
.io_maxwritecnt
);
3728 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &buffer
, bufferSize
))
3731 /* Get a buffer for doing the I/O */
3732 bp
= buf_alloc(hfsmp
->hfs_devvp
);
3733 buf_setdataptr(bp
, (uintptr_t)buffer
);
3735 resid
= (off_t
) blockCount
* (off_t
) hfsmp
->blockSize
;
3736 srcSector
= (daddr64_t
) oldStart
* hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
3737 destSector
= (daddr64_t
) newStart
* hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
3739 ioSize
= MIN(bufferSize
, resid
);
3740 ioSizeSectors
= ioSize
/ hfsmp
->hfs_logical_block_size
;
3742 /* Prepare the buffer for reading */
3743 buf_reset(bp
, B_READ
);
3744 buf_setsize(bp
, ioSize
);
3745 buf_setcount(bp
, ioSize
);
3746 buf_setblkno(bp
, srcSector
);
3747 buf_setlblkno(bp
, srcSector
);
3750 err
= VNOP_STRATEGY(bp
);
3752 err
= buf_biowait(bp
);
3754 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err
);
3758 /* Prepare the buffer for writing */
3759 buf_reset(bp
, B_WRITE
);
3760 buf_setsize(bp
, ioSize
);
3761 buf_setcount(bp
, ioSize
);
3762 buf_setblkno(bp
, destSector
);
3763 buf_setlblkno(bp
, destSector
);
3764 if (journal_uses_fua(hfsmp
->jnl
))
3768 vnode_startwrite(hfsmp
->hfs_devvp
);
3769 err
= VNOP_STRATEGY(bp
);
3771 err
= buf_biowait(bp
);
3773 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err
);
3778 srcSector
+= ioSizeSectors
;
3779 destSector
+= ioSizeSectors
;
3784 kmem_free(kernel_map
, (vm_offset_t
)buffer
, bufferSize
);
3786 /* Make sure all writes have been flushed to disk. */
3787 if (!journal_uses_fua(hfsmp
->jnl
)) {
3788 err
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, context
);
3790 printf("hfs_copy_extent: DKIOCSYNCHRONIZECACHE failed (%d)\n", err
);
3791 err
= 0; /* Don't fail the copy. */
3796 hfs_invalidate_sectors(vp
, (daddr64_t
)oldStart
*sectorsPerBlock
, (daddr64_t
)blockCount
*sectorsPerBlock
);
3803 * Reclaim space at the end of a volume, used by a given system file.
3805 * This routine attempts to move any extent which contains allocation blocks
3806 * at or after "startblk." A separate transaction is used to do the move.
3807 * The contents of any moved extents are read and written via the volume's
3808 * device vnode -- NOT via "vp." During the move, moved blocks which are part
3809 * of a transaction have their physical block numbers invalidated so they will
3810 * eventually be written to their new locations.
3812 * This routine can be used to move overflow extents for the allocation file.
3815 * hfsmp The volume being resized.
3816 * startblk Blocks >= this allocation block need to be moved.
3817 * locks Which locks need to be taken for the given system file.
3818 * vp The vnode for the system file.
3821 * moved Set to true if any extents were moved.
3824 hfs_relocate_callback(__unused HFSPlusExtentKey
*key
, HFSPlusExtentRecord
*record
, HFSPlusExtentRecord
*state
)
3826 bcopy(state
, record
, sizeof(HFSPlusExtentRecord
));
3830 hfs_reclaim_sys_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, u_long startblk
, int locks
, Boolean
*moved
, vfs_context_t context
)
3837 u_int32_t oldStartBlock
;
3838 u_int32_t newStartBlock
;
3839 u_int32_t blockCount
;
3840 struct filefork
*fp
;
3842 /* If there is no vnode for this file, then there's nothing to do. */
3846 /* printf("hfs_reclaim_sys_file: %.*s\n", VTOC(vp)->c_desc.cd_namelen, VTOC(vp)->c_desc.cd_nameptr); */
3848 /* We always need the allocation bitmap and extents B-tree */
3849 locks
|= SFL_BITMAP
| SFL_EXTENTS
;
3851 error
= hfs_start_transaction(hfsmp
);
3853 printf("hfs_reclaim_sys_file: hfs_start_transaction returned %d\n", error
);
3856 lockflags
= hfs_systemfile_lock(hfsmp
, locks
, HFS_EXCLUSIVE_LOCK
);
3860 /* Relocate non-overflow extents */
3861 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3862 if (fp
->ff_extents
[i
].blockCount
== 0)
3864 oldStartBlock
= fp
->ff_extents
[i
].startBlock
;
3865 blockCount
= fp
->ff_extents
[i
].blockCount
;
3866 datablks
+= blockCount
;
3867 block
= oldStartBlock
+ blockCount
;
3868 if (block
> startblk
) {
3869 error
= BlockAllocate(hfsmp
, 1, blockCount
, blockCount
, true, true, &newStartBlock
, &blockCount
);
3871 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error
);
3874 if (blockCount
!= fp
->ff_extents
[i
].blockCount
) {
3875 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount
, fp
->ff_extents
[i
].blockCount
);
3878 error
= hfs_copy_extent(hfsmp
, vp
, oldStartBlock
, newStartBlock
, blockCount
, context
);
3880 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error
);
3883 fp
->ff_extents
[i
].startBlock
= newStartBlock
;
3884 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3886 error
= BlockDeallocate(hfsmp
, oldStartBlock
, blockCount
);
3888 /* TODO: Mark volume inconsistent? */
3889 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error
);
3892 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3894 /* TODO: Mark volume inconsistent? */
3895 printf("hfs_reclaim_sys_file: hfs_flushvolumeheader returned %d\n", error
);
3901 /* Relocate overflow extents (if any) */
3902 if (i
== kHFSPlusExtentDensity
&& fp
->ff_blocks
> datablks
) {
3903 struct BTreeIterator
*iterator
= NULL
;
3904 struct FSBufferDescriptor btdata
;
3905 HFSPlusExtentRecord record
;
3906 HFSPlusExtentKey
*key
;
3911 forktype
= VNODE_IS_RSRC(vp
) ? 0xFF : 0;
3912 fileID
= VTOC(vp
)->c_cnid
;
3913 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &iterator
, sizeof(*iterator
))) {
3914 printf("hfs_reclaim_sys_file: kmem_alloc failed!\n");
3919 bzero(iterator
, sizeof(*iterator
));
3920 key
= (HFSPlusExtentKey
*) &iterator
->key
;
3921 key
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
3922 key
->forkType
= forktype
;
3923 key
->fileID
= fileID
;
3924 key
->startBlock
= datablks
;
3926 btdata
.bufferAddress
= &record
;
3927 btdata
.itemSize
= sizeof(record
);
3928 btdata
.itemCount
= 1;
3930 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
3932 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
3933 while (error
== 0) {
3934 /* Stop when we encounter a different file or fork. */
3935 if ((key
->fileID
!= fileID
) ||
3936 (key
->forkType
!= forktype
)) {
3940 * Check if the file overlaps target space.
3942 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3943 if (record
[i
].blockCount
== 0) {
3946 oldStartBlock
= record
[i
].startBlock
;
3947 blockCount
= record
[i
].blockCount
;
3948 block
= oldStartBlock
+ blockCount
;
3949 if (block
> startblk
) {
3950 error
= BlockAllocate(hfsmp
, 1, blockCount
, blockCount
, true, true, &newStartBlock
, &blockCount
);
3952 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error
);
3955 if (blockCount
!= record
[i
].blockCount
) {
3956 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount
, fp
->ff_extents
[i
].blockCount
);
3957 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3960 error
= hfs_copy_extent(hfsmp
, vp
, oldStartBlock
, newStartBlock
, blockCount
, context
);
3962 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error
);
3963 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3966 record
[i
].startBlock
= newStartBlock
;
3967 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3970 * NOTE: To support relocating overflow extents of the
3971 * allocation file, we must update the BTree record BEFORE
3972 * deallocating the old extent so that BlockDeallocate will
3973 * use the extent's new location to calculate physical block
3974 * numbers. (This is for the case where the old extent's
3975 * bitmap bits actually reside in the extent being moved.)
3977 error
= BTUpdateRecord(fcb
, iterator
, (IterateCallBackProcPtr
) hfs_relocate_callback
, &record
);
3979 /* TODO: Mark volume inconsistent? */
3980 printf("hfs_reclaim_sys_file: BTUpdateRecord returned %d\n", error
);
3983 error
= BlockDeallocate(hfsmp
, oldStartBlock
, blockCount
);
3985 /* TODO: Mark volume inconsistent? */
3986 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error
);
3991 /* Look for more records. */
3992 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3993 if (error
== btNotFound
) {
3999 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4005 hfs_systemfile_unlock(hfsmp
, lockflags
);
4006 error
= hfs_end_transaction(hfsmp
);
4008 printf("hfs_reclaim_sys_file: hfs_end_transaction returned %d\n", error
);
4014 (void) BlockDeallocate(hfsmp
, newStartBlock
, blockCount
);
4016 (void) hfs_systemfile_unlock(hfsmp
, lockflags
);
4017 (void) hfs_end_transaction(hfsmp
);
4023 * This journal_relocate callback updates the journal info block to point
4024 * at the new journal location. This write must NOT be done using the
4025 * transaction. We must write the block immediately. We must also force
4026 * it to get to the media so that the new journal location will be seen by
4027 * the replay code before we can safely let journaled blocks be written
4028 * to their normal locations.
4030 * The tests for journal_uses_fua below are mildly hacky. Since the journal
4031 * and the file system are both on the same device, I'm leveraging what
4032 * the journal has decided about FUA.
4034 struct hfs_journal_relocate_args
{
4035 struct hfsmount
*hfsmp
;
4036 vfs_context_t context
;
4037 u_int32_t newStartBlock
;
4041 hfs_journal_relocate_callback(void *_args
)
4044 struct hfs_journal_relocate_args
*args
= _args
;
4045 struct hfsmount
*hfsmp
= args
->hfsmp
;
4047 JournalInfoBlock
*jibp
;
4049 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
4050 hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
4051 hfsmp
->blockSize
, vfs_context_ucred(args
->context
), &bp
);
4053 printf("hfs_reclaim_journal_file: failed to read JIB (%d)\n", error
);
4056 jibp
= (JournalInfoBlock
*) buf_dataptr(bp
);
4057 jibp
->offset
= SWAP_BE64((u_int64_t
)args
->newStartBlock
* hfsmp
->blockSize
);
4058 jibp
->size
= SWAP_BE64(hfsmp
->jnl_size
);
4059 if (journal_uses_fua(hfsmp
->jnl
))
4061 error
= buf_bwrite(bp
);
4063 printf("hfs_reclaim_journal_file: failed to write JIB (%d)\n", error
);
4066 if (!journal_uses_fua(hfsmp
->jnl
)) {
4067 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, args
->context
);
4069 printf("hfs_reclaim_journal_file: DKIOCSYNCHRONIZECACHE failed (%d)\n", error
);
4070 error
= 0; /* Don't fail the operation. */
4079 hfs_reclaim_journal_file(struct hfsmount
*hfsmp
, vfs_context_t context
)
4083 u_int32_t newStartBlock
;
4084 u_int32_t oldBlockCount
;
4085 u_int32_t newBlockCount
;
4086 struct cat_desc journal_desc
;
4087 struct cat_attr journal_attr
;
4088 struct cat_fork journal_fork
;
4089 struct hfs_journal_relocate_args callback_args
;
4091 error
= hfs_start_transaction(hfsmp
);
4093 printf("hfs_reclaim_journal_file: hfs_start_transaction returned %d\n", error
);
4096 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4098 oldBlockCount
= hfsmp
->jnl_size
/ hfsmp
->blockSize
;
4100 /* TODO: Allow the journal to change size based on the new volume size. */
4101 error
= BlockAllocate(hfsmp
, 1, oldBlockCount
, oldBlockCount
, true, true, &newStartBlock
, &newBlockCount
);
4103 printf("hfs_reclaim_journal_file: BlockAllocate returned %d\n", error
);
4106 if (newBlockCount
!= oldBlockCount
) {
4107 printf("hfs_reclaim_journal_file: newBlockCount != oldBlockCount (%u, %u)\n", newBlockCount
, oldBlockCount
);
4111 error
= BlockDeallocate(hfsmp
, hfsmp
->jnl_start
, oldBlockCount
);
4113 printf("hfs_reclaim_journal_file: BlockDeallocate returned %d\n", error
);
4117 /* Update the catalog record for .journal */
4118 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlfileid
, 1, &journal_desc
, &journal_attr
, &journal_fork
);
4120 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error
);
4123 journal_fork
.cf_size
= newBlockCount
* hfsmp
->blockSize
;
4124 journal_fork
.cf_extents
[0].startBlock
= newStartBlock
;
4125 journal_fork
.cf_extents
[0].blockCount
= newBlockCount
;
4126 journal_fork
.cf_blocks
= newBlockCount
;
4127 error
= cat_update(hfsmp
, &journal_desc
, &journal_attr
, &journal_fork
, NULL
);
4128 cat_releasedesc(&journal_desc
); /* all done with cat descriptor */
4130 printf("hfs_reclaim_journal_file: cat_update returned %d\n", error
);
4133 callback_args
.hfsmp
= hfsmp
;
4134 callback_args
.context
= context
;
4135 callback_args
.newStartBlock
= newStartBlock
;
4137 error
= journal_relocate(hfsmp
->jnl
, (off_t
)newStartBlock
*hfsmp
->blockSize
,
4138 (off_t
)newBlockCount
*hfsmp
->blockSize
, 0,
4139 hfs_journal_relocate_callback
, &callback_args
);
4141 /* NOTE: journal_relocate will mark the journal invalid. */
4142 printf("hfs_reclaim_journal_file: journal_relocate returned %d\n", error
);
4145 hfsmp
->jnl_start
= newStartBlock
;
4146 hfsmp
->jnl_size
= (off_t
)newBlockCount
* hfsmp
->blockSize
;
4148 hfs_systemfile_unlock(hfsmp
, lockflags
);
4149 error
= hfs_end_transaction(hfsmp
);
4151 printf("hfs_reclaim_journal_file: hfs_end_transaction returned %d\n", error
);
4157 (void) BlockDeallocate(hfsmp
, newStartBlock
, newBlockCount
);
4159 hfs_systemfile_unlock(hfsmp
, lockflags
);
4160 (void) hfs_end_transaction(hfsmp
);
4166 * Move the journal info block to a new location. We have to make sure the
4167 * new copy of the journal info block gets to the media first, then change
4168 * the field in the volume header and the catalog record.
4171 hfs_reclaim_journal_info_block(struct hfsmount
*hfsmp
, vfs_context_t context
)
4176 u_int32_t blockCount
;
4177 struct cat_desc jib_desc
;
4178 struct cat_attr jib_attr
;
4179 struct cat_fork jib_fork
;
4180 buf_t old_bp
, new_bp
;
4182 error
= hfs_start_transaction(hfsmp
);
4184 printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error
);
4187 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4189 error
= BlockAllocate(hfsmp
, 1, 1, 1, true, true, &newBlock
, &blockCount
);
4191 printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error
);
4194 if (blockCount
!= 1) {
4195 printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount
);
4198 error
= BlockDeallocate(hfsmp
, hfsmp
->vcbJinfoBlock
, 1);
4200 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error
);
4204 /* Copy the old journal info block content to the new location */
4205 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
4206 hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
4207 hfsmp
->blockSize
, vfs_context_ucred(context
), &old_bp
);
4209 printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error
);
4212 new_bp
= buf_getblk(hfsmp
->hfs_devvp
,
4213 newBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
4214 hfsmp
->blockSize
, 0, 0, BLK_META
);
4215 bcopy((char*)buf_dataptr(old_bp
), (char*)buf_dataptr(new_bp
), hfsmp
->blockSize
);
4217 if (journal_uses_fua(hfsmp
->jnl
))
4218 buf_markfua(new_bp
);
4219 error
= buf_bwrite(new_bp
);
4221 printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error
);
4224 if (!journal_uses_fua(hfsmp
->jnl
)) {
4225 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, context
);
4227 printf("hfs_reclaim_journal_info_block: DKIOCSYNCHRONIZECACHE failed (%d)\n", error
);
4228 /* Don't fail the operation. */
4232 /* Update the catalog record for .journal_info_block */
4233 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlinfoblkid
, 1, &jib_desc
, &jib_attr
, &jib_fork
);
4235 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error
);
4238 jib_fork
.cf_size
= hfsmp
->blockSize
;
4239 jib_fork
.cf_extents
[0].startBlock
= newBlock
;
4240 jib_fork
.cf_extents
[0].blockCount
= 1;
4241 jib_fork
.cf_blocks
= 1;
4242 error
= cat_update(hfsmp
, &jib_desc
, &jib_attr
, &jib_fork
, NULL
);
4243 cat_releasedesc(&jib_desc
); /* all done with cat descriptor */
4245 printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error
);
4249 /* Update the pointer to the journal info block in the volume header. */
4250 hfsmp
->vcbJinfoBlock
= newBlock
;
4251 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
4253 printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error
);
4256 hfs_systemfile_unlock(hfsmp
, lockflags
);
4257 error
= hfs_end_transaction(hfsmp
);
4259 printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error
);
4261 error
= journal_flush(hfsmp
->jnl
);
4263 printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error
);
4268 (void) BlockDeallocate(hfsmp
, newBlock
, blockCount
);
4270 hfs_systemfile_unlock(hfsmp
, lockflags
);
4271 (void) hfs_end_transaction(hfsmp
);
4277 * Reclaim space at the end of a file system.
4280 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
, vfs_context_t context
)
4282 struct vnode
*vp
= NULL
;
4284 struct BTreeIterator
* iterator
= NULL
;
4285 struct FSBufferDescriptor btdata
;
4286 struct HFSPlusCatalogFile filerec
;
4287 u_int32_t saved_next_allocation
;
4295 u_long blkstomove
= 0;
4299 int lastprogress
= 0;
4300 Boolean system_file_moved
= false;
4302 /* Relocate extents of the Allocation file if they're in the way. */
4303 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_allocation_vp
, startblk
, SFL_BITMAP
, &system_file_moved
, context
);
4305 printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error
);
4308 /* Relocate extents of the Extents B-tree if they're in the way. */
4309 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_extents_vp
, startblk
, SFL_EXTENTS
, &system_file_moved
, context
);
4311 printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error
);
4314 /* Relocate extents of the Catalog B-tree if they're in the way. */
4315 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_catalog_vp
, startblk
, SFL_CATALOG
, &system_file_moved
, context
);
4317 printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error
);
4320 /* Relocate extents of the Attributes B-tree if they're in the way. */
4321 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_attribute_vp
, startblk
, SFL_ATTRIBUTE
, &system_file_moved
, context
);
4323 printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error
);
4326 /* Relocate extents of the Startup File if there is one and they're in the way. */
4327 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_startup_vp
, startblk
, SFL_STARTUP
, &system_file_moved
, context
);
4329 printf("hfs_reclaimspace: reclaim startup file returned %d\n", error
);
4334 * We need to make sure the alternate volume header gets flushed if we moved
4335 * any extents in the volume header. But we need to do that before
4336 * shrinking the size of the volume, or else the journal code will panic
4337 * with an invalid (too large) block number.
4339 * Note that system_file_moved will be set if ANY extent was moved, even
4340 * if it was just an overflow extent. In this case, the journal_flush isn't
4341 * strictly required, but shouldn't hurt.
4343 if (system_file_moved
)
4344 journal_flush(hfsmp
->jnl
);
4346 if (hfsmp
->jnl_start
+ (hfsmp
->jnl_size
/ hfsmp
->blockSize
) > startblk
) {
4347 error
= hfs_reclaim_journal_file(hfsmp
, context
);
4349 printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error
);
4354 if (hfsmp
->vcbJinfoBlock
>= startblk
) {
4355 error
= hfs_reclaim_journal_info_block(hfsmp
, context
);
4357 printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error
);
4362 /* For now move a maximum of 250,000 files. */
4363 maxfilecnt
= MIN(hfsmp
->hfs_filecount
, 250000);
4364 maxfilecnt
= MIN((u_long
)maxfilecnt
, reclaimblks
);
4365 cnidbufsize
= maxfilecnt
* sizeof(cnid_t
);
4366 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&cnidbufp
, cnidbufsize
)) {
4369 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
4370 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
4374 saved_next_allocation
= hfsmp
->nextAllocation
;
4375 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, hfsmp
->hfs_metazone_start
);
4377 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
4378 bzero(iterator
, sizeof(*iterator
));
4380 btdata
.bufferAddress
= &filerec
;
4381 btdata
.itemSize
= sizeof(filerec
);
4382 btdata
.itemCount
= 1;
4384 /* Keep the Catalog and extents files locked during iteration. */
4385 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_SHARED_LOCK
);
4387 error
= BTIterateRecord(fcb
, kBTreeFirstRecord
, iterator
, NULL
, NULL
);
4392 * Iterate over all the catalog records looking for files
4393 * that overlap into the space we're trying to free up.
4395 for (filecnt
= 0; filecnt
< maxfilecnt
; ) {
4396 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4398 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
4403 if (filerec
.recordType
!= kHFSPlusFileRecord
) {
4406 datablks
= rsrcblks
= 0;
4408 * Check if either fork overlaps target space.
4410 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
4411 if (filerec
.dataFork
.extents
[i
].blockCount
!= 0) {
4412 datablks
+= filerec
.dataFork
.extents
[i
].blockCount
;
4413 block
= filerec
.dataFork
.extents
[i
].startBlock
+
4414 filerec
.dataFork
.extents
[i
].blockCount
;
4415 if (block
>= startblk
) {
4416 if ((filerec
.fileID
== hfsmp
->hfs_jnlfileid
) ||
4417 (filerec
.fileID
== hfsmp
->hfs_jnlinfoblkid
)) {
4418 printf("hfs_reclaimspace: cannot move active journal\n");
4422 cnidbufp
[filecnt
++] = filerec
.fileID
;
4423 blkstomove
+= filerec
.dataFork
.totalBlocks
;
4427 if (filerec
.resourceFork
.extents
[i
].blockCount
!= 0) {
4428 rsrcblks
+= filerec
.resourceFork
.extents
[i
].blockCount
;
4429 block
= filerec
.resourceFork
.extents
[i
].startBlock
+
4430 filerec
.resourceFork
.extents
[i
].blockCount
;
4431 if (block
>= startblk
) {
4432 cnidbufp
[filecnt
++] = filerec
.fileID
;
4433 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
4439 * Check for any overflow extents that overlap.
4441 if (i
== kHFSPlusExtentDensity
) {
4442 if (filerec
.dataFork
.totalBlocks
> datablks
) {
4443 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, datablks
, filerec
.fileID
, 0)) {
4444 cnidbufp
[filecnt
++] = filerec
.fileID
;
4445 blkstomove
+= filerec
.dataFork
.totalBlocks
;
4447 } else if (filerec
.resourceFork
.totalBlocks
> rsrcblks
) {
4448 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, rsrcblks
, filerec
.fileID
, 1)) {
4449 cnidbufp
[filecnt
++] = filerec
.fileID
;
4450 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
4457 if (filecnt
== 0 && !system_file_moved
) {
4458 printf("hfs_reclaimspace: no files moved\n");
4461 /* All done with catalog. */
4462 hfs_systemfile_unlock(hfsmp
, lockflags
);
4463 if (error
|| filecnt
== 0)
4467 * Double check space requirements to make sure
4468 * there is enough space to relocate any files
4469 * that reside in the reclaim area.
4471 * Blocks To Move --------------
4474 * ------------------------------------------------------------------------
4478 * ------------------------------------------------------------------------
4480 * <------------------- New Total Blocks ------------------><-- Reclaim -->
4482 * <------------------------ Original Total Blocks ----------------------->
4485 if (blkstomove
>= hfs_freeblks(hfsmp
, 1)) {
4486 printf("hfs_truncatefs: insufficient space (need %lu blocks; have %u blocks)\n", blkstomove
, hfs_freeblks(hfsmp
, 1));
4490 hfsmp
->hfs_resize_filesmoved
= 0;
4491 hfsmp
->hfs_resize_totalfiles
= filecnt
;
4493 /* Now move any files that are in the way. */
4494 for (i
= 0; i
< filecnt
; ++i
) {
4497 if (hfs_vget(hfsmp
, cnidbufp
[i
], &vp
, 0) != 0)
4500 /* Relocate any data fork blocks. */
4501 if (VTOF(vp
)->ff_blocks
> 0) {
4502 error
= hfs_relocate(vp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
4507 /* Relocate any resource fork blocks. */
4508 if ((VTOC((vp
))->c_blocks
- VTOF((vp
))->ff_blocks
) > 0) {
4509 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
);
4512 error
= hfs_relocate(rvp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
4513 VTOC(rvp
)->c_flag
|= C_NEED_RVNODE_PUT
;
4517 hfs_unlock(VTOC(vp
));
4521 ++hfsmp
->hfs_resize_filesmoved
;
4523 /* Report intermediate progress. */
4524 if (filecnt
> 100) {
4527 progress
= (i
* 100) / filecnt
;
4528 if (progress
> (lastprogress
+ 9)) {
4529 printf("hfs_reclaimspace: %d%% done...\n", progress
);
4530 lastprogress
= progress
;
4535 hfs_unlock(VTOC(vp
));
4539 if (hfsmp
->hfs_resize_filesmoved
!= 0) {
4540 printf("hfs_reclaimspace: relocated %d files on \"%s\"\n",
4541 (int)hfsmp
->hfs_resize_filesmoved
, hfsmp
->vcbVN
);
4544 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4545 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
4548 * Restore the roving allocation pointer on errors.
4549 * (but only if we didn't move any files)
4551 if (error
&& hfsmp
->hfs_resize_filesmoved
== 0) {
4552 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, saved_next_allocation
);
4559 * Check if there are any overflow extents that overlap.
4562 hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
, u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
)
4564 struct BTreeIterator
* iterator
= NULL
;
4565 struct FSBufferDescriptor btdata
;
4566 HFSPlusExtentRecord extrec
;
4567 HFSPlusExtentKey
*extkeyptr
;
4575 forktype
= rsrcfork
? 0xFF : 0;
4576 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
4579 bzero(iterator
, sizeof(*iterator
));
4580 extkeyptr
= (HFSPlusExtentKey
*)&iterator
->key
;
4581 extkeyptr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
4582 extkeyptr
->forkType
= forktype
;
4583 extkeyptr
->fileID
= fileID
;
4584 extkeyptr
->startBlock
= catblks
;
4586 btdata
.bufferAddress
= &extrec
;
4587 btdata
.itemSize
= sizeof(extrec
);
4588 btdata
.itemCount
= 1;
4590 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
4592 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
4593 while (error
== 0) {
4594 /* Stop when we encounter a different file. */
4595 if ((extkeyptr
->fileID
!= fileID
) ||
4596 (extkeyptr
->forkType
!= forktype
)) {
4600 * Check if the file overlaps target space.
4602 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
4603 if (extrec
[i
].blockCount
== 0) {
4606 block
= extrec
[i
].startBlock
+ extrec
[i
].blockCount
;
4607 if (block
>= startblk
) {
4612 /* Look for more records. */
4613 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4616 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4617 return (overlapped
);
4622 * Calculate the progress of a file system resize operation.
4626 hfs_resize_progress(struct hfsmount
*hfsmp
, u_int32_t
*progress
)
4628 if ((hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) == 0) {
4632 if (hfsmp
->hfs_resize_totalfiles
> 0)
4633 *progress
= (hfsmp
->hfs_resize_filesmoved
* 100) / hfsmp
->hfs_resize_totalfiles
;
4642 * Get file system attributes.
4645 hfs_vfs_getattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
4647 #define HFS_ATTR_CMN_VALIDMASK (ATTR_CMN_VALIDMASK & ~(ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST))
4648 #define HFS_ATTR_FILE_VALIDMASK (ATTR_FILE_VALIDMASK & ~(ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST))
4650 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
4651 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
4654 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)hfsmp
->vcbNxtCNID
;
4656 VFSATTR_RETURN(fsap
, f_objcount
, (u_int64_t
)hfsmp
->vcbFilCnt
+ (u_int64_t
)hfsmp
->vcbDirCnt
);
4657 VFSATTR_RETURN(fsap
, f_filecount
, (u_int64_t
)hfsmp
->vcbFilCnt
);
4658 VFSATTR_RETURN(fsap
, f_dircount
, (u_int64_t
)hfsmp
->vcbDirCnt
);
4659 VFSATTR_RETURN(fsap
, f_maxobjcount
, (u_int64_t
)0xFFFFFFFF);
4660 VFSATTR_RETURN(fsap
, f_iosize
, (size_t)cluster_max_io_size(mp
, 0));
4661 VFSATTR_RETURN(fsap
, f_blocks
, (u_int64_t
)hfsmp
->totalBlocks
);
4662 VFSATTR_RETURN(fsap
, f_bfree
, (u_int64_t
)hfs_freeblks(hfsmp
, 0));
4663 VFSATTR_RETURN(fsap
, f_bavail
, (u_int64_t
)hfs_freeblks(hfsmp
, 1));
4664 VFSATTR_RETURN(fsap
, f_bsize
, (u_int32_t
)vcb
->blockSize
);
4665 /* XXX needs clarification */
4666 VFSATTR_RETURN(fsap
, f_bused
, hfsmp
->totalBlocks
- hfs_freeblks(hfsmp
, 1));
4667 /* Maximum files is constrained by total blocks. */
4668 VFSATTR_RETURN(fsap
, f_files
, (u_int64_t
)(hfsmp
->totalBlocks
- 2));
4669 VFSATTR_RETURN(fsap
, f_ffree
, MIN((u_int64_t
)freeCNIDs
, (u_int64_t
)hfs_freeblks(hfsmp
, 1)));
4671 fsap
->f_fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
4672 fsap
->f_fsid
.val
[1] = vfs_typenum(mp
);
4673 VFSATTR_SET_SUPPORTED(fsap
, f_fsid
);
4675 VFSATTR_RETURN(fsap
, f_signature
, vcb
->vcbSigWord
);
4676 VFSATTR_RETURN(fsap
, f_carbon_fsid
, 0);
4678 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
4679 vol_capabilities_attr_t
*cap
;
4681 cap
= &fsap
->f_capabilities
;
4683 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4684 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
4685 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4686 VOL_CAP_FMT_CASE_PRESERVING
|
4687 VOL_CAP_FMT_FAST_STATFS
|
4688 VOL_CAP_FMT_HIDDEN_FILES
|
4689 VOL_CAP_FMT_PATH_FROM_ID
;
4691 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
4692 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4693 VOL_CAP_FMT_SYMBOLICLINKS
|
4694 VOL_CAP_FMT_HARDLINKS
|
4695 VOL_CAP_FMT_JOURNAL
|
4696 VOL_CAP_FMT_ZERO_RUNS
|
4697 (hfsmp
->jnl
? VOL_CAP_FMT_JOURNAL_ACTIVE
: 0) |
4698 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
? VOL_CAP_FMT_CASE_SENSITIVE
: 0) |
4699 VOL_CAP_FMT_CASE_PRESERVING
|
4700 VOL_CAP_FMT_FAST_STATFS
|
4701 VOL_CAP_FMT_2TB_FILESIZE
|
4702 VOL_CAP_FMT_HIDDEN_FILES
|
4703 VOL_CAP_FMT_PATH_FROM_ID
;
4705 cap
->capabilities
[VOL_CAPABILITIES_INTERFACES
] =
4706 VOL_CAP_INT_SEARCHFS
|
4707 VOL_CAP_INT_ATTRLIST
|
4708 VOL_CAP_INT_NFSEXPORT
|
4709 VOL_CAP_INT_READDIRATTR
|
4710 VOL_CAP_INT_EXCHANGEDATA
|
4711 VOL_CAP_INT_ALLOCATE
|
4712 VOL_CAP_INT_VOL_RENAME
|
4713 VOL_CAP_INT_ADVLOCK
|
4716 VOL_CAP_INT_EXTENDED_ATTR
|
4717 VOL_CAP_INT_NAMEDSTREAMS
;
4719 VOL_CAP_INT_EXTENDED_ATTR
;
4721 cap
->capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
4722 cap
->capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
4724 cap
->valid
[VOL_CAPABILITIES_FORMAT
] =
4725 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4726 VOL_CAP_FMT_SYMBOLICLINKS
|
4727 VOL_CAP_FMT_HARDLINKS
|
4728 VOL_CAP_FMT_JOURNAL
|
4729 VOL_CAP_FMT_JOURNAL_ACTIVE
|
4730 VOL_CAP_FMT_NO_ROOT_TIMES
|
4731 VOL_CAP_FMT_SPARSE_FILES
|
4732 VOL_CAP_FMT_ZERO_RUNS
|
4733 VOL_CAP_FMT_CASE_SENSITIVE
|
4734 VOL_CAP_FMT_CASE_PRESERVING
|
4735 VOL_CAP_FMT_FAST_STATFS
|
4736 VOL_CAP_FMT_2TB_FILESIZE
|
4737 VOL_CAP_FMT_OPENDENYMODES
|
4738 VOL_CAP_FMT_HIDDEN_FILES
|
4739 VOL_CAP_FMT_PATH_FROM_ID
;
4740 cap
->valid
[VOL_CAPABILITIES_INTERFACES
] =
4741 VOL_CAP_INT_SEARCHFS
|
4742 VOL_CAP_INT_ATTRLIST
|
4743 VOL_CAP_INT_NFSEXPORT
|
4744 VOL_CAP_INT_READDIRATTR
|
4745 VOL_CAP_INT_EXCHANGEDATA
|
4746 VOL_CAP_INT_COPYFILE
|
4747 VOL_CAP_INT_ALLOCATE
|
4748 VOL_CAP_INT_VOL_RENAME
|
4749 VOL_CAP_INT_ADVLOCK
|
4751 VOL_CAP_INT_MANLOCK
|
4753 VOL_CAP_INT_EXTENDED_ATTR
|
4754 VOL_CAP_INT_NAMEDSTREAMS
;
4756 VOL_CAP_INT_EXTENDED_ATTR
;
4758 cap
->valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
4759 cap
->valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
4760 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
4762 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
4763 vol_attributes_attr_t
*attrp
= &fsap
->f_attributes
;
4765 attrp
->validattr
.commonattr
= HFS_ATTR_CMN_VALIDMASK
;
4766 attrp
->validattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
4767 attrp
->validattr
.dirattr
= ATTR_DIR_VALIDMASK
;
4768 attrp
->validattr
.fileattr
= HFS_ATTR_FILE_VALIDMASK
;
4769 attrp
->validattr
.forkattr
= 0;
4771 attrp
->nativeattr
.commonattr
= HFS_ATTR_CMN_VALIDMASK
;
4772 attrp
->nativeattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
4773 attrp
->nativeattr
.dirattr
= ATTR_DIR_VALIDMASK
;
4774 attrp
->nativeattr
.fileattr
= HFS_ATTR_FILE_VALIDMASK
;
4775 attrp
->nativeattr
.forkattr
= 0;
4776 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
4778 fsap
->f_create_time
.tv_sec
= hfsmp
->vcbCrDate
;
4779 fsap
->f_create_time
.tv_nsec
= 0;
4780 VFSATTR_SET_SUPPORTED(fsap
, f_create_time
);
4781 fsap
->f_modify_time
.tv_sec
= hfsmp
->vcbLsMod
;
4782 fsap
->f_modify_time
.tv_nsec
= 0;
4783 VFSATTR_SET_SUPPORTED(fsap
, f_modify_time
);
4785 fsap
->f_backup_time
.tv_sec
= hfsmp
->vcbVolBkUp
;
4786 fsap
->f_backup_time
.tv_nsec
= 0;
4787 VFSATTR_SET_SUPPORTED(fsap
, f_backup_time
);
4788 if (VFSATTR_IS_ACTIVE(fsap
, f_fssubtype
)) {
4789 u_int16_t subtype
= 0;
4792 * Subtypes (flavors) for HFS
4793 * 0: Mac OS Extended
4794 * 1: Mac OS Extended (Journaled)
4795 * 2: Mac OS Extended (Case Sensitive)
4796 * 3: Mac OS Extended (Case Sensitive, Journaled)
4798 * 128: Mac OS Standard
4801 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4802 subtype
= HFS_SUBTYPE_STANDARDHFS
;
4803 } else /* HFS Plus */ {
4805 subtype
|= HFS_SUBTYPE_JOURNALED
;
4806 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
4807 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
4809 fsap
->f_fssubtype
= subtype
;
4810 VFSATTR_SET_SUPPORTED(fsap
, f_fssubtype
);
4813 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
4814 strlcpy(fsap
->f_vol_name
, (char *) hfsmp
->vcbVN
, MAXPATHLEN
);
4815 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
4821 * Perform a volume rename. Requires the FS' root vp.
4824 hfs_rename_volume(struct vnode
*vp
, const char *name
, proc_t p
)
4826 ExtendedVCB
*vcb
= VTOVCB(vp
);
4827 struct cnode
*cp
= VTOC(vp
);
4828 struct hfsmount
*hfsmp
= VTOHFS(vp
);
4829 struct cat_desc to_desc
;
4830 struct cat_desc todir_desc
;
4831 struct cat_desc new_desc
;
4832 cat_cookie_t cookie
;
4837 * Ignore attempts to rename a volume to a zero-length name.
4842 bzero(&to_desc
, sizeof(to_desc
));
4843 bzero(&todir_desc
, sizeof(todir_desc
));
4844 bzero(&new_desc
, sizeof(new_desc
));
4845 bzero(&cookie
, sizeof(cookie
));
4847 todir_desc
.cd_parentcnid
= kHFSRootParentID
;
4848 todir_desc
.cd_cnid
= kHFSRootFolderID
;
4849 todir_desc
.cd_flags
= CD_ISDIR
;
4851 to_desc
.cd_nameptr
= (const u_int8_t
*)name
;
4852 to_desc
.cd_namelen
= strlen(name
);
4853 to_desc
.cd_parentcnid
= kHFSRootParentID
;
4854 to_desc
.cd_cnid
= cp
->c_cnid
;
4855 to_desc
.cd_flags
= CD_ISDIR
;
4857 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)) == 0) {
4858 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
4859 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, &cookie
, p
)) == 0) {
4860 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4862 error
= cat_rename(hfsmp
, &cp
->c_desc
, &todir_desc
, &to_desc
, &new_desc
);
4865 * If successful, update the name in the VCB, ensure it's terminated.
4868 strlcpy((char *)vcb
->vcbVN
, name
, sizeof(vcb
->vcbVN
));
4871 hfs_systemfile_unlock(hfsmp
, lockflags
);
4872 cat_postflight(hfsmp
, &cookie
, p
);
4876 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
4878 hfs_end_transaction(hfsmp
);
4881 /* Release old allocated name buffer */
4882 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
4883 const char *tmp_name
= (const char *)cp
->c_desc
.cd_nameptr
;
4885 cp
->c_desc
.cd_nameptr
= 0;
4886 cp
->c_desc
.cd_namelen
= 0;
4887 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
4888 vfs_removename(tmp_name
);
4890 /* Update cnode's catalog descriptor */
4891 replace_desc(cp
, &new_desc
);
4892 vcb
->volumeNameEncodingHint
= new_desc
.cd_encoding
;
4893 cp
->c_touch_chgtime
= TRUE
;
4903 * Get file system attributes.
4906 hfs_vfs_setattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
4908 kauth_cred_t cred
= vfs_context_ucred(context
);
4912 * Must be superuser or owner of filesystem to change volume attributes
4914 if (!kauth_cred_issuser(cred
) && (kauth_cred_getuid(cred
) != vfs_statfs(mp
)->f_owner
))
4917 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
4920 error
= hfs_vfs_root(mp
, &root_vp
, context
);
4924 error
= hfs_rename_volume(root_vp
, fsap
->f_vol_name
, vfs_context_proc(context
));
4925 (void) vnode_put(root_vp
);
4929 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
4936 /* If a runtime corruption is detected, set the volume inconsistent
4937 * bit in the volume attributes. The volume inconsistent bit is a persistent
4938 * bit which represents that the volume is corrupt and needs repair.
4939 * The volume inconsistent bit can be set from the kernel when it detects
4940 * runtime corruption or from file system repair utilities like fsck_hfs when
4941 * a repair operation fails. The bit should be cleared only from file system
4942 * verify/repair utility like fsck_hfs when a verify/repair succeeds.
4944 void hfs_mark_volume_inconsistent(struct hfsmount
*hfsmp
)
4946 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
4947 if ((hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
) == 0) {
4948 hfsmp
->vcbAtrb
|= kHFSVolumeInconsistentMask
;
4949 MarkVCBDirty(hfsmp
);
4951 /* Log information to ASL log */
4952 fslog_fs_corrupt(hfsmp
->hfs_mp
);
4953 printf("HFS: Runtime corruption detected on %s, fsck will be forced on next mount.\n", hfsmp
->vcbVN
);
4954 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
4957 /* Replay the journal on the device node provided. Returns zero if
4958 * journal replay succeeded or no journal was supposed to be replayed.
4960 static int hfs_journal_replay(const char *devnode
, vfs_context_t context
)
4963 struct vnode
*devvp
= NULL
;
4964 struct mount
*mp
= NULL
;
4965 struct hfs_mount_args
*args
= NULL
;
4967 /* Lookup vnode for given raw device path */
4968 retval
= vnode_open(devnode
, FREAD
|FWRITE
, 0, 0, &devvp
, NULL
);
4973 /* Replay allowed only on raw devices */
4974 if (!vnode_ischr(devvp
)) {
4979 /* Create dummy mount structures */
4980 MALLOC(mp
, struct mount
*, sizeof(struct mount
), M_TEMP
, M_WAITOK
);
4981 bzero(mp
, sizeof(struct mount
));
4982 mount_lock_init(mp
);
4984 MALLOC(args
, struct hfs_mount_args
*, sizeof(struct hfs_mount_args
), M_TEMP
, M_WAITOK
);
4985 bzero(args
, sizeof(struct hfs_mount_args
));
4987 retval
= hfs_mountfs(devvp
, mp
, args
, 1, context
);
4988 buf_flushdirtyblks(devvp
, MNT_WAIT
, 0, "hfs_journal_replay");
4992 mount_lock_destroy(mp
);
4999 vnode_close(devvp
, FREAD
|FWRITE
, NULL
);
5005 * hfs vfs operations.
5007 struct vfsops hfs_vfsops
= {
5013 hfs_vfs_getattr
, /* was hfs_statfs */