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/vnode_internal.h>
79 #include <sys/mount_internal.h>
80 #include <sys/sysctl.h>
81 #include <sys/malloc.h>
83 #include <sys/quota.h>
85 #include <sys/paths.h>
86 #include <sys/utfconv.h>
87 #include <sys/kdebug.h>
88 #include <sys/fslog.h>
90 #include <kern/locks.h>
92 #include <vfs/vfs_journal.h>
94 #include <miscfs/specfs/specdev.h>
95 #include <hfs/hfs_mount.h>
98 #include "hfs_catalog.h"
99 #include "hfs_cnode.h"
101 #include "hfs_endian.h"
102 #include "hfs_hotfiles.h"
103 #include "hfs_quota.h"
105 #include "hfscommon/headers/FileMgrInternal.h"
106 #include "hfscommon/headers/BTreesInternal.h"
114 lck_grp_attr_t
* hfs_group_attr
;
115 lck_attr_t
* hfs_lock_attr
;
116 lck_grp_t
* hfs_mutex_group
;
117 lck_grp_t
* hfs_rwlock_group
;
119 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
121 static int hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
);
122 static int hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
);
123 static int hfs_flushfiles(struct mount
*, int, struct proc
*);
124 static int hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
125 static int hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
);
126 static int hfs_init(struct vfsconf
*vfsp
);
127 static int hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
);
128 static int hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
, int journal_replay_only
, vfs_context_t context
);
129 static int hfs_reload(struct mount
*mp
);
130 static int hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, vfs_context_t context
);
131 static int hfs_quotactl(struct mount
*, int, uid_t
, caddr_t
, vfs_context_t context
);
132 static int hfs_start(struct mount
*mp
, int flags
, vfs_context_t context
);
133 static int hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, vfs_context_t context
);
134 static int hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
);
135 static int hfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
136 user_addr_t newp
, size_t newlen
, vfs_context_t context
);
137 static int hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
);
138 static int hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, vfs_context_t context
);
139 static int hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
141 static int hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
, vfs_context_t context
);
142 static int hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
,
143 u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
);
144 static int hfs_journal_replay(const char *devnode
, vfs_context_t context
);
148 * Called by vfs_mountroot when mounting HFS Plus as root.
153 hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
)
155 struct hfsmount
*hfsmp
;
157 struct vfsstatfs
*vfsp
;
160 hfs_chashinit_finish();
162 if ((error
= hfs_mountfs(rvp
, mp
, NULL
, 0, context
)))
166 hfsmp
= VFSTOHFS(mp
);
168 hfsmp
->hfs_uid
= UNKNOWNUID
;
169 hfsmp
->hfs_gid
= UNKNOWNGID
;
170 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
171 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
173 /* Establish the free block reserve. */
174 vcb
= HFSTOVCB(hfsmp
);
175 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
176 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
178 vfsp
= vfs_statfs(mp
);
179 (void)hfs_statfs(mp
, vfsp
, NULL
);
192 hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
194 struct proc
*p
= vfs_context_proc(context
);
195 struct hfsmount
*hfsmp
= NULL
;
196 struct hfs_mount_args args
;
200 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
)))) {
203 cmdflags
= (u_int32_t
)vfs_flags(mp
) & MNT_CMDFLAGS
;
204 if (cmdflags
& MNT_UPDATE
) {
205 hfsmp
= VFSTOHFS(mp
);
207 /* Reload incore data after an fsck. */
208 if (cmdflags
& MNT_RELOAD
) {
209 if (vfs_isrdonly(mp
))
210 return hfs_reload(mp
);
215 /* Change to a read-only file system. */
216 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
220 /* use VFS_SYNC to push out System (btree) files */
221 retval
= VFS_SYNC(mp
, MNT_WAIT
, context
);
222 if (retval
&& ((cmdflags
& MNT_FORCE
) == 0))
226 if (cmdflags
& MNT_FORCE
)
229 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
231 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
232 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
234 /* also get the volume bitmap blocks */
236 if (vnode_mount(hfsmp
->hfs_devvp
) == mp
) {
237 retval
= hfs_fsync(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, p
);
239 vnode_get(hfsmp
->hfs_devvp
);
240 retval
= VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
241 vnode_put(hfsmp
->hfs_devvp
);
245 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
249 hfs_global_exclusive_lock_acquire(hfsmp
);
251 journal_close(hfsmp
->jnl
);
254 // Note: we explicitly don't want to shutdown
255 // access to the jvp because we may need
256 // it later if we go back to being read-write.
258 hfs_global_exclusive_lock_release(hfsmp
);
262 /* Change to a writable file system. */
263 if (vfs_iswriteupgrade(mp
)) {
266 * On inconsistent disks, do not allow read-write mount
267 * unless it is the boot volume being mounted.
269 if (!(vfs_flags(mp
) & MNT_ROOTFS
) &&
270 (hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
)) {
276 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
277 if (retval
!= E_NONE
)
280 // If the journal was shut-down previously because we were
281 // asked to be read-only, let's start it back up again now
283 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
284 && hfsmp
->jnl
== NULL
285 && hfsmp
->jvp
!= NULL
) {
288 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
289 jflags
= JOURNAL_RESET
;
294 hfs_global_exclusive_lock_acquire(hfsmp
);
296 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
297 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
300 hfsmp
->hfs_phys_block_size
,
303 hfs_sync_metadata
, hfsmp
->hfs_mp
);
305 hfs_global_exclusive_lock_release(hfsmp
);
307 if (hfsmp
->jnl
== NULL
) {
311 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
316 /* Only clear HFS_READ_ONLY after a successfull write */
317 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
319 if (!(hfsmp
->hfs_flags
& (HFS_READ_ONLY
& HFS_STANDARD
))) {
320 /* Setup private/hidden directories for hardlinks. */
321 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
322 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
324 hfs_remove_orphans(hfsmp
);
327 * Allow hot file clustering if conditions allow.
329 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
330 (void) hfs_recording_init(hfsmp
);
332 /* Force ACLs on HFS+ file systems. */
333 if (vfs_extendedsecurity(HFSTOVFS(hfsmp
)) == 0) {
334 vfs_setextendedsecurity(HFSTOVFS(hfsmp
));
339 /* Update file system parameters. */
340 retval
= hfs_changefs(mp
, &args
);
342 } else /* not an update request */ {
344 /* Set the mount flag to indicate that we support volfs */
345 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_DOVOLFS
));
347 hfs_chashinit_finish();
349 retval
= hfs_mountfs(devvp
, mp
, &args
, 0, context
);
353 (void)hfs_statfs(mp
, vfs_statfs(mp
), context
);
359 struct hfs_changefs_cargs
{
360 struct hfsmount
*hfsmp
;
367 hfs_changefs_callback(struct vnode
*vp
, void *cargs
)
371 struct cat_desc cndesc
;
372 struct cat_attr cnattr
;
373 struct hfs_changefs_cargs
*args
;
375 args
= (struct hfs_changefs_cargs
*)cargs
;
378 vcb
= HFSTOVCB(args
->hfsmp
);
380 if (cat_lookup(args
->hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
, NULL
)) {
382 * If we couldn't find this guy skip to the next one
387 return (VNODE_RETURNED
);
390 * Get the real uid/gid and perm mask from disk.
392 if (args
->permswitch
|| args
->permfix
) {
393 cp
->c_uid
= cnattr
.ca_uid
;
394 cp
->c_gid
= cnattr
.ca_gid
;
395 cp
->c_mode
= cnattr
.ca_mode
;
398 * If we're switching name converters then...
399 * Remove the existing entry from the namei cache.
400 * Update name to one based on new encoder.
404 replace_desc(cp
, &cndesc
);
406 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
407 strlcpy((char *)vcb
->vcbVN
, (const char *)cp
->c_desc
.cd_nameptr
, NAME_MAX
+1);
408 cp
->c_desc
.cd_encoding
= args
->hfsmp
->hfs_encoding
;
411 cat_releasedesc(&cndesc
);
413 return (VNODE_RETURNED
);
416 /* Change fs mount parameters */
418 hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
)
421 int namefix
, permfix
, permswitch
;
422 struct hfsmount
*hfsmp
;
424 hfs_to_unicode_func_t get_unicode_func
;
425 unicode_to_hfs_func_t get_hfsname_func
;
426 u_long old_encoding
= 0;
427 struct hfs_changefs_cargs cargs
;
428 u_int32_t mount_flags
;
430 hfsmp
= VFSTOHFS(mp
);
431 vcb
= HFSTOVCB(hfsmp
);
432 mount_flags
= (unsigned int)vfs_flags(mp
);
434 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
435 ((mount_flags
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
436 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
437 (mount_flags
& MNT_UNKNOWNPERMISSIONS
)));
439 /* The root filesystem must operate with actual permissions: */
440 if (permswitch
&& (mount_flags
& MNT_ROOTFS
) && (mount_flags
& MNT_UNKNOWNPERMISSIONS
)) {
441 vfs_clearflags(mp
, (u_int64_t
)((unsigned int)MNT_UNKNOWNPERMISSIONS
)); /* Just say "No". */
444 if (mount_flags
& MNT_UNKNOWNPERMISSIONS
)
445 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
447 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
449 namefix
= permfix
= 0;
452 * Tracking of hot files requires up-to-date access times. So if
453 * access time updates are disabled, we must also disable hot files.
455 if (mount_flags
& MNT_NOATIME
) {
456 (void) hfs_recording_suspend(hfsmp
);
459 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
460 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
461 gTimeZone
= args
->hfs_timezone
;
464 /* Change the default uid, gid and/or mask */
465 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
466 hfsmp
->hfs_uid
= args
->hfs_uid
;
467 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
470 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
471 hfsmp
->hfs_gid
= args
->hfs_gid
;
472 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
475 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
476 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
477 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
478 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
479 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
480 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
481 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
486 /* Change the hfs encoding value (hfs only) */
487 if ((vcb
->vcbSigWord
== kHFSSigWord
) &&
488 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
489 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
491 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
496 * Connect the new hfs_get_unicode converter but leave
497 * the old hfs_get_hfsname converter in place so that
498 * we can lookup existing vnodes to get their correctly
501 * When we're all finished, we can then connect the new
502 * hfs_get_hfsname converter and release our interest
503 * in the old converters.
505 hfsmp
->hfs_get_unicode
= get_unicode_func
;
506 old_encoding
= hfsmp
->hfs_encoding
;
507 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
511 if (!(namefix
|| permfix
|| permswitch
))
514 /* XXX 3762912 hack to support HFS filesystem 'owner' */
517 hfsmp
->hfs_uid
== UNKNOWNUID
? KAUTH_UID_NONE
: hfsmp
->hfs_uid
,
518 hfsmp
->hfs_gid
== UNKNOWNGID
? KAUTH_GID_NONE
: hfsmp
->hfs_gid
);
521 * For each active vnode fix things that changed
523 * Note that we can visit a vnode more than once
524 * and we can race with fsync.
526 * hfs_changefs_callback will be called for each vnode
527 * hung off of this mount point
529 * properly referenced and unreferenced around the callback
532 cargs
.namefix
= namefix
;
533 cargs
.permfix
= permfix
;
534 cargs
.permswitch
= permswitch
;
536 vnode_iterate(mp
, 0, hfs_changefs_callback
, (void *)&cargs
);
539 * If we're switching name converters we can now
540 * connect the new hfs_get_hfsname converter and
541 * release our interest in the old converters.
544 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
545 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
546 (void) hfs_relconverter(old_encoding
);
553 struct hfs_reload_cargs
{
554 struct hfsmount
*hfsmp
;
559 hfs_reload_callback(struct vnode
*vp
, void *cargs
)
562 struct hfs_reload_cargs
*args
;
564 args
= (struct hfs_reload_cargs
*)cargs
;
566 * flush all the buffers associated with this node
568 (void) buf_invalidateblks(vp
, 0, 0, 0);
572 * Remove any directory hints
575 hfs_reldirhints(cp
, 0);
578 * Re-read cnode data for all active vnodes (non-metadata files).
580 if (!vnode_issystem(vp
) && !VNODE_IS_RSRC(vp
)) {
581 struct cat_fork
*datafork
;
582 struct cat_desc desc
;
584 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
586 /* lookup by fileID since name could have changed */
587 if ((args
->error
= cat_idlookup(args
->hfsmp
, cp
->c_fileid
, 0, &desc
, &cp
->c_attr
, datafork
)))
588 return (VNODE_RETURNED_DONE
);
590 /* update cnode's catalog descriptor */
591 (void) replace_desc(cp
, &desc
);
593 return (VNODE_RETURNED
);
597 * Reload all incore data for a filesystem (used after running fsck on
598 * the root filesystem and finding things to fix). The filesystem must
599 * be mounted read-only.
601 * Things to do to update the mount:
602 * invalidate all cached meta-data.
603 * invalidate all inactive vnodes.
604 * invalidate all cached file data.
605 * re-read volume header from disk.
606 * re-load meta-file info (extents, file size).
607 * re-load B-tree header data.
608 * re-read cnode data for all active vnodes.
611 hfs_reload(struct mount
*mountp
)
613 register struct vnode
*devvp
;
617 struct hfsmount
*hfsmp
;
618 struct HFSPlusVolumeHeader
*vhp
;
620 struct filefork
*forkp
;
621 struct cat_desc cndesc
;
622 struct hfs_reload_cargs args
;
624 hfsmp
= VFSTOHFS(mountp
);
625 vcb
= HFSTOVCB(hfsmp
);
627 if (vcb
->vcbSigWord
== kHFSSigWord
)
628 return (EINVAL
); /* rooting from HFS is not supported! */
631 * Invalidate all cached meta-data.
633 devvp
= hfsmp
->hfs_devvp
;
634 if (buf_invalidateblks(devvp
, 0, 0, 0))
635 panic("hfs_reload: dirty1");
640 * hfs_reload_callback will be called for each vnode
641 * hung off of this mount point that can't be recycled...
642 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
643 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
644 * properly referenced and unreferenced around the callback
646 vnode_iterate(mountp
, VNODE_RELOAD
| VNODE_WAIT
, hfs_reload_callback
, (void *)&args
);
652 * Re-read VolumeHeader from disk.
654 sectorsize
= hfsmp
->hfs_phys_block_size
;
656 error
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
657 (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
)),
658 sectorsize
, NOCRED
, &bp
);
665 vhp
= (HFSPlusVolumeHeader
*) (buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
667 /* Do a quick sanity check */
668 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
669 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
670 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
671 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
672 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
677 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
678 vcb
->vcbAtrb
= SWAP_BE32 (vhp
->attributes
);
679 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
680 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
681 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
682 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
683 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
684 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
685 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
686 HFS_UPDATE_NEXT_ALLOCATION(vcb
, SWAP_BE32 (vhp
->nextAllocation
));
687 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
688 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
689 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
690 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
691 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
694 * Re-load meta-file vnode data (extent info, file size, etc).
696 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
697 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
698 forkp
->ff_extents
[i
].startBlock
=
699 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
700 forkp
->ff_extents
[i
].blockCount
=
701 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
703 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
704 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
705 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
708 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
709 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
710 forkp
->ff_extents
[i
].startBlock
=
711 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
712 forkp
->ff_extents
[i
].blockCount
=
713 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
715 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
716 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
717 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
719 if (hfsmp
->hfs_attribute_vp
) {
720 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
721 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
722 forkp
->ff_extents
[i
].startBlock
=
723 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
724 forkp
->ff_extents
[i
].blockCount
=
725 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
727 forkp
->ff_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
728 forkp
->ff_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
729 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
732 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
733 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
734 forkp
->ff_extents
[i
].startBlock
=
735 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
736 forkp
->ff_extents
[i
].blockCount
=
737 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
739 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
740 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
741 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
747 * Re-load B-tree header data
749 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
750 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
753 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
754 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
757 if (hfsmp
->hfs_attribute_vp
) {
758 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
759 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
763 /* Reload the volume name */
764 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, 0, &cndesc
, NULL
, NULL
)))
766 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
767 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
768 cat_releasedesc(&cndesc
);
770 /* Re-establish private/hidden directories. */
771 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
772 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
774 /* In case any volume information changed to trigger a notification */
775 hfs_generate_volume_notifications(hfsmp
);
782 * Common code for mount and mountroot
785 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
,
786 int journal_replay_only
, vfs_context_t context
)
788 struct proc
*p
= vfs_context_proc(context
);
790 struct hfsmount
*hfsmp
;
793 HFSMasterDirectoryBlock
*mdbp
;
803 u_int32_t minblksize
;
804 u_int32_t iswritable
;
805 daddr64_t mdb_offset
;
808 ronly
= vfs_isrdonly(mp
);
809 dev
= vnode_specrdev(devvp
);
810 cred
= p
? vfs_context_ucred(context
) : NOCRED
;
816 minblksize
= kHFSBlockSize
;
818 /* Advisory locking should be handled at the VFS layer */
819 vfs_setlocklocal(mp
);
821 /* Get the real physical block size. */
822 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, context
)) {
826 /* Switch to 512 byte sectors (temporarily) */
828 u_int32_t size512
= 512;
830 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, context
)) {
835 /* Get the number of 512 byte physical blocks. */
836 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
837 /* resetting block size may fail if getting block count did */
838 (void)VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
);
843 /* Compute an accurate disk size (i.e. within 512 bytes) */
844 disksize
= (u_int64_t
)blkcnt
* (u_int64_t
)512;
847 * On Tiger it is not necessary to switch the device
848 * block size to be 4k if there are more than 31-bits
849 * worth of blocks but to insure compatibility with
850 * pre-Tiger systems we have to do it.
852 if (blkcnt
> 0x000000007fffffff) {
853 minblksize
= blksize
= 4096;
856 /* Now switch to our preferred physical block size. */
858 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
862 /* Get the count of physical blocks. */
863 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
870 * minblksize is the minimum physical block size
871 * blksize has our preferred physical block size
872 * blkcnt has the total number of physical blocks
875 mdb_offset
= (daddr64_t
)HFS_PRI_SECTOR(blksize
);
876 if ((retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
))) {
879 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
880 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
884 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
885 bzero(hfsmp
, sizeof(struct hfsmount
));
888 * Init the volume information structure
891 lck_mtx_init(&hfsmp
->hfs_mutex
, hfs_mutex_group
, hfs_lock_attr
);
892 lck_mtx_init(&hfsmp
->hfc_mutex
, hfs_mutex_group
, hfs_lock_attr
);
893 lck_rw_init(&hfsmp
->hfs_global_lock
, hfs_rwlock_group
, hfs_lock_attr
);
894 lck_rw_init(&hfsmp
->hfs_insync
, hfs_rwlock_group
, hfs_lock_attr
);
896 vfs_setfsprivate(mp
, hfsmp
);
897 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
898 hfsmp
->hfs_raw_dev
= vnode_specrdev(devvp
);
899 hfsmp
->hfs_devvp
= devvp
;
900 vnode_ref(devvp
); /* Hold a ref on the device, dropped when hfsmp is freed. */
901 hfsmp
->hfs_phys_block_size
= blksize
;
902 hfsmp
->hfs_phys_block_count
= blkcnt
;
903 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
905 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
906 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)
907 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
910 for (i
= 0; i
< MAXQUOTAS
; i
++)
911 dqfileinit(&hfsmp
->hfs_qfiles
[i
]);
915 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
916 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
917 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
918 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
919 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
920 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
921 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
922 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
923 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
925 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
928 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
929 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
931 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
934 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
935 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
) {
936 hfsmp
->hfs_uid
= UNKNOWNUID
;
937 hfsmp
->hfs_gid
= UNKNOWNGID
;
938 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
939 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
940 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
944 /* Find out if disk media is writable. */
945 if (VNOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, context
) == 0) {
947 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
949 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
952 // record the current time at which we're mounting this volume
955 hfsmp
->hfs_mount_time
= tv
.tv_sec
;
957 /* Mount a standard HFS disk */
958 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
959 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
961 /* If only journal replay is requested, exit immediately */
962 if (journal_replay_only
) {
967 if ((vfs_flags(mp
) & MNT_ROOTFS
)) {
968 retval
= EINVAL
; /* Cannot root from HFS standard disks */
971 /* HFS disks can only use 512 byte physical blocks */
972 if (blksize
> kHFSBlockSize
) {
973 blksize
= kHFSBlockSize
;
974 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
978 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
982 hfsmp
->hfs_phys_block_size
= blksize
;
983 hfsmp
->hfs_phys_block_count
= blkcnt
;
986 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
987 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
989 /* establish the timezone */
990 gTimeZone
= args
->hfs_timezone
;
993 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
994 &hfsmp
->hfs_get_hfsname
);
998 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
1000 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1002 } else /* Mount an HFS Plus disk */ {
1003 HFSPlusVolumeHeader
*vhp
;
1004 off_t embeddedOffset
;
1005 int jnl_disable
= 0;
1007 /* Get the embedded Volume Header */
1008 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1009 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
1010 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1011 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1014 * If the embedded volume doesn't start on a block
1015 * boundary, then switch the device to a 512-byte
1016 * block size so everything will line up on a block
1019 if ((embeddedOffset
% blksize
) != 0) {
1020 printf("HFS Mount: embedded volume offset not"
1021 " a multiple of physical block size (%d);"
1022 " switching to 512\n", blksize
);
1024 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
1025 (caddr_t
)&blksize
, FWRITE
, context
)) {
1029 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
1030 (caddr_t
)&blkcnt
, 0, context
)) {
1034 /* Note: relative block count adjustment */
1035 hfsmp
->hfs_phys_block_count
*=
1036 hfsmp
->hfs_phys_block_size
/ blksize
;
1037 hfsmp
->hfs_phys_block_size
= blksize
;
1040 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1041 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1043 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1045 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1046 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1049 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1052 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1054 } else /* pure HFS+ */ {
1056 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1060 * On inconsistent disks, do not allow read-write mount
1061 * unless it is the boot volume being mounted.
1063 if (!(vfs_flags(mp
) & MNT_ROOTFS
) &&
1064 (SWAP_BE32(vhp
->attributes
) & kHFSVolumeInconsistentMask
) &&
1065 !(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
1075 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) &&
1076 args
->journal_disable
) {
1081 // We only initialize the journal here if the last person
1082 // to mount this volume was journaling aware. Otherwise
1083 // we delay journal initialization until later at the end
1084 // of hfs_MountHFSPlusVolume() because the last person who
1085 // mounted it could have messed things up behind our back
1086 // (so we need to go find the .journal file, make sure it's
1087 // the right size, re-sync up if it was moved, etc).
1089 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1090 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1093 // if we're able to init the journal, mark the mount
1094 // point as journaled.
1096 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1097 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
1099 // if the journal failed to open, then set the lastMountedVersion
1100 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1101 // of just bailing out because the volume is journaled.
1103 HFSPlusVolumeHeader
*jvhp
;
1105 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1107 if (mdb_offset
== 0) {
1108 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1112 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1114 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1116 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1117 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1118 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1126 // clear this so the error exit path won't try to use it
1131 // if this isn't the root device just bail out.
1132 // If it is the root device we just continue on
1133 // in the hopes that fsck_hfs will be able to
1134 // fix any damage that exists on the volume.
1135 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1143 /* Either the journal is replayed successfully, or there
1144 * was nothing to replay, or no journal exists. In any case,
1147 if (journal_replay_only
) {
1152 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1154 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1156 * If the backend didn't like our physical blocksize
1157 * then retry with physical blocksize of 512.
1159 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1160 printf("HFS Mount: could not use physical block size "
1161 "(%d) switching to 512\n", blksize
);
1163 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
1167 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
1171 devvp
->v_specsize
= blksize
;
1172 /* Note: relative block count adjustment (in case this is an embedded volume). */
1173 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1174 hfsmp
->hfs_phys_block_size
= blksize
;
1177 // close and re-open this with the new block size
1178 journal_close(hfsmp
->jnl
);
1180 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1181 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
1183 // if the journal failed to open, then set the lastMountedVersion
1184 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1185 // of just bailing out because the volume is journaled.
1187 HFSPlusVolumeHeader
*jvhp
;
1189 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1191 if (mdb_offset
== 0) {
1192 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1196 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1198 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1200 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1201 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1202 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1210 // clear this so the error exit path won't try to use it
1215 // if this isn't the root device just bail out.
1216 // If it is the root device we just continue on
1217 // in the hopes that fsck_hfs will be able to
1218 // fix any damage that exists on the volume.
1219 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1226 /* Try again with a smaller block size... */
1227 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1230 (void) hfs_relconverter(0);
1233 // save off a snapshot of the mtime from the previous mount
1235 hfsmp
->hfs_last_mounted_mtime
= hfsmp
->hfs_mtime
;
1241 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (long)dev
;
1242 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
1243 vfs_setmaxsymlen(mp
, 0);
1244 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
1245 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSNATIVEXATTR
;
1247 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1249 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
1250 /* Tell VFS that we support directory hard links. */
1251 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSDIRLINKS
;
1253 /* HFS standard doesn't support extended readdir! */
1254 mp
->mnt_vtable
->vfc_vfsflags
&= ~VFC_VFSREADDIR_EXTENDED
;
1259 * Set the free space warning levels for a non-root volume:
1261 * Set the lower freespace limit (the level that will trigger a warning)
1262 * to 5% of the volume size or 250MB, whichever is less, and the desired
1263 * level (which will cancel the alert request) to 1/2 above that limit.
1264 * Start looking for free space to drop below this level and generate a
1265 * warning immediately if needed:
1267 hfsmp
->hfs_freespace_notify_warninglimit
=
1268 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1269 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1270 hfsmp
->hfs_freespace_notify_desiredlevel
=
1271 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1272 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1275 * Set the free space warning levels for the root volume:
1277 * Set the lower freespace limit (the level that will trigger a warning)
1278 * to 1% of the volume size or 50MB, whichever is less, and the desired
1279 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1281 hfsmp
->hfs_freespace_notify_warninglimit
=
1282 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1283 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1284 hfsmp
->hfs_freespace_notify_desiredlevel
=
1285 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1286 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1289 /* Check if the file system exists on virtual device, like disk image */
1290 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, context
) == 0) {
1292 hfsmp
->hfs_flags
|= HFS_VIRTUAL_DEVICE
;
1297 * Start looking for free space to drop below this level and generate a
1298 * warning immediately if needed:
1300 hfsmp
->hfs_notification_conditions
= 0;
1301 hfs_generate_volume_notifications(hfsmp
);
1304 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1315 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1316 (void)VNOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, context
);
1320 if (hfsmp
->hfs_devvp
) {
1321 vnode_rele(hfsmp
->hfs_devvp
);
1323 FREE(hfsmp
, M_HFSMNT
);
1324 vfs_setfsprivate(mp
, NULL
);
1331 * Make a filesystem operational.
1332 * Nothing to do at the moment.
1336 hfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
1343 * unmount system call
1346 hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
)
1348 struct proc
*p
= vfs_context_proc(context
);
1349 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1350 int retval
= E_NONE
;
1357 if (mntflags
& MNT_FORCE
) {
1358 flags
|= FORCECLOSE
;
1362 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1365 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1366 (void) hfs_recording_suspend(hfsmp
);
1369 * Flush out the b-trees, volume bitmap and Volume Header
1371 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1372 retval
= hfs_start_transaction(hfsmp
);
1375 } else if (!force
) {
1379 if (hfsmp
->hfs_startup_vp
) {
1380 (void) hfs_lock(VTOC(hfsmp
->hfs_startup_vp
), HFS_EXCLUSIVE_LOCK
);
1381 retval
= hfs_fsync(hfsmp
->hfs_startup_vp
, MNT_WAIT
, 0, p
);
1382 hfs_unlock(VTOC(hfsmp
->hfs_startup_vp
));
1383 if (retval
&& !force
)
1387 if (hfsmp
->hfs_attribute_vp
) {
1388 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), HFS_EXCLUSIVE_LOCK
);
1389 retval
= hfs_fsync(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, p
);
1390 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
1391 if (retval
&& !force
)
1395 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), HFS_EXCLUSIVE_LOCK
);
1396 retval
= hfs_fsync(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, p
);
1397 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
1398 if (retval
&& !force
)
1401 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
1402 retval
= hfs_fsync(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, p
);
1403 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
1404 if (retval
&& !force
)
1407 if (hfsmp
->hfs_allocation_vp
) {
1408 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
1409 retval
= hfs_fsync(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, p
);
1410 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
1411 if (retval
&& !force
)
1415 if (hfsmp
->hfc_filevp
&& vnode_issystem(hfsmp
->hfc_filevp
)) {
1416 retval
= hfs_fsync(hfsmp
->hfc_filevp
, MNT_WAIT
, 0, p
);
1417 if (retval
&& !force
)
1421 /* If runtime corruption was detected, indicate that the volume
1422 * was not unmounted cleanly.
1424 if (hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
) {
1425 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1427 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1430 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1432 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1434 goto err_exit
; /* could not flush everything */
1438 hfs_end_transaction(hfsmp
);
1444 journal_flush(hfsmp
->jnl
);
1448 * Invalidate our caches and release metadata vnodes
1450 (void) hfsUnmount(hfsmp
, p
);
1453 * Last chance to dump unreferenced system files.
1455 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1457 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1458 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1462 journal_close(hfsmp
->jnl
);
1466 VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
1468 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1469 retval
= VNOP_CLOSE(hfsmp
->jvp
,
1470 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1472 vnode_put(hfsmp
->jvp
);
1477 #ifdef HFS_SPARSE_DEV
1478 /* Drop our reference on the backing fs (if any). */
1479 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1480 struct vnode
* tmpvp
;
1482 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1483 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1484 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1487 #endif /* HFS_SPARSE_DEV */
1488 lck_mtx_destroy(&hfsmp
->hfc_mutex
, hfs_mutex_group
);
1489 vnode_rele(hfsmp
->hfs_devvp
);
1490 FREE(hfsmp
, M_HFSMNT
);
1496 hfs_end_transaction(hfsmp
);
1503 * Return the root of a filesystem.
1506 hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1508 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)kHFSRootFolderID
, vpp
, 1);
1513 * Do operations associated with quotas
1517 hfs_quotactl(__unused
struct mount
*mp
, __unused
int cmds
, __unused uid_t uid
, __unused caddr_t datap
, __unused vfs_context_t context
)
1523 hfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t context
)
1525 struct proc
*p
= vfs_context_proc(context
);
1526 int cmd
, type
, error
;
1529 uid
= vfs_context_ucred(context
)->cr_ruid
;
1530 cmd
= cmds
>> SUBCMDSHIFT
;
1537 if (uid
== vfs_context_ucred(context
)->cr_ruid
)
1541 if ( (error
= vfs_context_suser(context
)) )
1545 type
= cmds
& SUBCMDMASK
;
1546 if ((u_int
)type
>= MAXQUOTAS
)
1548 if (vfs_busy(mp
, LK_NOWAIT
))
1554 error
= hfs_quotaon(p
, mp
, type
, datap
);
1558 error
= hfs_quotaoff(p
, mp
, type
);
1562 error
= hfs_setquota(mp
, uid
, type
, datap
);
1566 error
= hfs_setuse(mp
, uid
, type
, datap
);
1570 error
= hfs_getquota(mp
, uid
, type
, datap
);
1574 error
= hfs_qsync(mp
);
1578 error
= hfs_quotastat(mp
, type
, datap
);
1591 /* Subtype is composite of bits */
1592 #define HFS_SUBTYPE_JOURNALED 0x01
1593 #define HFS_SUBTYPE_CASESENSITIVE 0x02
1594 /* bits 2 - 6 reserved */
1595 #define HFS_SUBTYPE_STANDARDHFS 0x80
1598 * Get file system statistics.
1601 hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
1603 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1604 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1606 u_int16_t subtype
= 0;
1608 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1610 sbp
->f_bsize
= (u_int32_t
)vcb
->blockSize
;
1611 sbp
->f_iosize
= (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
);
1612 sbp
->f_blocks
= (u_int64_t
)((unsigned long)vcb
->totalBlocks
);
1613 sbp
->f_bfree
= (u_int64_t
)((unsigned long )hfs_freeblks(hfsmp
, 0));
1614 sbp
->f_bavail
= (u_int64_t
)((unsigned long )hfs_freeblks(hfsmp
, 1));
1615 sbp
->f_files
= (u_int64_t
)((unsigned long )(vcb
->totalBlocks
- 2)); /* max files is constrained by total blocks */
1616 sbp
->f_ffree
= (u_int64_t
)((unsigned long )(MIN(freeCNIDs
, sbp
->f_bavail
)));
1619 * Subtypes (flavors) for HFS
1620 * 0: Mac OS Extended
1621 * 1: Mac OS Extended (Journaled)
1622 * 2: Mac OS Extended (Case Sensitive)
1623 * 3: Mac OS Extended (Case Sensitive, Journaled)
1625 * 128: Mac OS Standard
1628 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1629 subtype
= HFS_SUBTYPE_STANDARDHFS
;
1630 } else /* HFS Plus */ {
1632 subtype
|= HFS_SUBTYPE_JOURNALED
;
1633 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
1634 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
1636 sbp
->f_fssubtype
= subtype
;
1643 // XXXdbg -- this is a callback to be used by the journal to
1644 // get meta data blocks flushed out to disk.
1646 // XXXdbg -- be smarter and don't flush *every* block on each
1647 // call. try to only flush some so we don't wind up
1648 // being too synchronous.
1652 hfs_sync_metadata(void *arg
)
1654 struct mount
*mp
= (struct mount
*)arg
;
1655 struct hfsmount
*hfsmp
;
1658 int sectorsize
, retval
;
1659 daddr64_t priIDSector
;
1660 hfsmp
= VFSTOHFS(mp
);
1661 vcb
= HFSTOVCB(hfsmp
);
1663 // now make sure the super block is flushed
1664 sectorsize
= hfsmp
->hfs_phys_block_size
;
1665 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1666 HFS_PRI_SECTOR(sectorsize
));
1667 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1668 if ((retval
!= 0 ) && (retval
!= ENXIO
)) {
1669 printf("hfs_sync_metadata: can't read volume header at %d! (retval 0x%x)\n",
1670 (int)priIDSector
, retval
);
1673 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1679 // the alternate super block...
1680 // XXXdbg - we probably don't need to do this each and every time.
1681 // hfs_btreeio.c:FlushAlternate() should flag when it was
1683 if (hfsmp
->hfs_alt_id_sector
) {
1684 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &bp
);
1685 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1694 struct hfs_sync_cargs
{
1703 hfs_sync_callback(struct vnode
*vp
, void *cargs
)
1706 struct hfs_sync_cargs
*args
;
1709 args
= (struct hfs_sync_cargs
*)cargs
;
1711 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1712 return (VNODE_RETURNED
);
1716 if ((cp
->c_flag
& C_MODIFIED
) ||
1717 (cp
->c_touch_acctime
| cp
->c_touch_chgtime
| cp
->c_touch_modtime
) ||
1718 vnode_hasdirtyblks(vp
)) {
1719 error
= hfs_fsync(vp
, args
->waitfor
, 0, args
->p
);
1722 args
->error
= error
;
1725 return (VNODE_RETURNED
);
1731 * Go through the disk queues to initiate sandbagged IO;
1732 * go through the inodes to write those that have been modified;
1733 * initiate the writing of the super block if it has been modified.
1735 * Note: we are always called with the filesystem marked `MPBUSY'.
1738 hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
)
1740 struct proc
*p
= vfs_context_proc(context
);
1742 struct hfsmount
*hfsmp
;
1744 struct vnode
*meta_vp
[4];
1746 int error
, allerror
= 0;
1747 struct hfs_sync_cargs args
;
1750 * During MNT_UPDATE hfs_changefs might be manipulating
1751 * vnodes so back off
1753 if (((u_int32_t
)vfs_flags(mp
)) & MNT_UPDATE
) /* XXX MNT_UPDATE may not be visible here */
1756 hfsmp
= VFSTOHFS(mp
);
1757 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1760 /* skip over frozen volumes */
1761 if (!lck_rw_try_lock_shared(&hfsmp
->hfs_insync
))
1764 args
.cred
= kauth_cred_get();
1765 args
.waitfor
= waitfor
;
1769 * hfs_sync_callback will be called for each vnode
1770 * hung off of this mount point... the vnode will be
1771 * properly referenced and unreferenced around the callback
1773 vnode_iterate(mp
, 0, hfs_sync_callback
, (void *)&args
);
1776 allerror
= args
.error
;
1778 vcb
= HFSTOVCB(hfsmp
);
1780 meta_vp
[0] = vcb
->extentsRefNum
;
1781 meta_vp
[1] = vcb
->catalogRefNum
;
1782 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1783 meta_vp
[3] = hfsmp
->hfs_attribute_vp
; /* Optional file */
1785 /* Now sync our three metadata files */
1786 for (i
= 0; i
< 4; ++i
) {
1790 if ((btvp
==0) || (vnode_mount(btvp
) != mp
))
1793 /* XXX use hfs_systemfile_lock instead ? */
1794 (void) hfs_lock(VTOC(btvp
), HFS_EXCLUSIVE_LOCK
);
1797 if (((cp
->c_flag
& C_MODIFIED
) == 0) &&
1798 (cp
->c_touch_acctime
== 0) &&
1799 (cp
->c_touch_chgtime
== 0) &&
1800 (cp
->c_touch_modtime
== 0) &&
1801 vnode_hasdirtyblks(btvp
) == 0) {
1802 hfs_unlock(VTOC(btvp
));
1805 error
= vnode_get(btvp
);
1807 hfs_unlock(VTOC(btvp
));
1810 if ((error
= hfs_fsync(btvp
, waitfor
, 0, p
)))
1818 * Force stale file system control information to be flushed.
1820 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1821 if ((error
= VNOP_FSYNC(hfsmp
->hfs_devvp
, waitfor
, context
))) {
1829 hfs_hotfilesync(hfsmp
, vfs_context_kernel());
1832 * Write back modified superblock.
1834 if (IsVCBDirty(vcb
)) {
1835 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1841 journal_flush(hfsmp
->jnl
);
1844 lck_rw_unlock_shared(&hfsmp
->hfs_insync
);
1850 * File handle to vnode
1852 * Have to be really careful about stale file handles:
1853 * - check that the cnode id is valid
1854 * - call hfs_vget() to get the locked cnode
1855 * - check for an unallocated cnode (i_mode == 0)
1856 * - check that the given client host has export rights and return
1857 * those rights via. exflagsp and credanonp
1860 hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1862 struct hfsfid
*hfsfhp
;
1867 hfsfhp
= (struct hfsfid
*)fhp
;
1869 if (fhlen
< (int)sizeof(struct hfsfid
))
1872 result
= hfs_vget(VFSTOHFS(mp
), ntohl(hfsfhp
->hfsfid_cnid
), &nvp
, 0);
1874 if (result
== ENOENT
)
1879 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1880 * For NFS, we are assuming that only if the createtime was moved
1881 * forward would it mean the fileID got reused in that session by
1882 * wrapping. We don't have a volume ID or other unique identifier to
1883 * to use here for a generation ID across reboots, crashes where
1884 * metadata noting lastFileID didn't make it to disk but client has
1885 * it, or volume erasures where fileIDs start over again. Lastly,
1886 * with HFS allowing "wraps" of fileIDs now, this becomes more
1887 * error prone. Future, would be change the "wrap bit" to a unique
1888 * wrap number and use that for generation number. For now do this.
1890 if (((time_t)(ntohl(hfsfhp
->hfsfid_gen
)) < VTOC(nvp
)->c_itime
)) {
1891 hfs_unlock(VTOC(nvp
));
1897 hfs_unlock(VTOC(nvp
));
1903 * Vnode pointer to File handle
1907 hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, __unused vfs_context_t context
)
1910 struct hfsfid
*hfsfhp
;
1912 if (ISHFS(VTOVCB(vp
)))
1913 return (ENOTSUP
); /* hfs standard is not exportable */
1915 if (*fhlenp
< (int)sizeof(struct hfsfid
))
1919 hfsfhp
= (struct hfsfid
*)fhp
;
1920 hfsfhp
->hfsfid_cnid
= htonl(cp
->c_fileid
);
1921 hfsfhp
->hfsfid_gen
= htonl(cp
->c_itime
);
1922 *fhlenp
= sizeof(struct hfsfid
);
1929 * Initial HFS filesystems, done only once.
1932 hfs_init(__unused
struct vfsconf
*vfsp
)
1934 static int done
= 0;
1940 hfs_converterinit();
1945 hfs_lock_attr
= lck_attr_alloc_init();
1946 hfs_group_attr
= lck_grp_attr_alloc_init();
1947 hfs_mutex_group
= lck_grp_alloc_init("hfs-mutex", hfs_group_attr
);
1948 hfs_rwlock_group
= lck_grp_alloc_init("hfs-rwlock", hfs_group_attr
);
1955 hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
)
1957 struct hfsmount
* hfsmp
;
1958 char fstypename
[MFSNAMELEN
];
1963 if (!vnode_isvroot(vp
))
1966 vnode_vfsname(vp
, fstypename
);
1967 if (strncmp(fstypename
, "hfs", sizeof(fstypename
)) != 0)
1972 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1981 #include <sys/filedesc.h>
1984 * HFS filesystem related variables.
1987 hfs_sysctl(int *name
, __unused u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
1988 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
1990 struct proc
*p
= vfs_context_proc(context
);
1992 struct hfsmount
*hfsmp
;
1994 /* all sysctl names at this level are terminal */
1996 if (name
[0] == HFS_ENCODINGBIAS
) {
1999 bias
= hfs_getencodingbias();
2000 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
2001 if (error
== 0 && newp
)
2002 hfs_setencodingbias(bias
);
2005 } else if (name
[0] == HFS_EXTEND_FS
) {
2007 vnode_t vp
= vfs_context_cwd(context
);
2009 if (newp
== USER_ADDR_NULL
|| vp
== NULLVP
)
2011 if ((error
= hfs_getmountpoint(vp
, &hfsmp
)))
2013 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, (quad_t
*)&newsize
);
2017 error
= hfs_extendfs(hfsmp
, newsize
, context
);
2020 } else if (name
[0] == HFS_ENCODINGHINT
) {
2024 u_int16_t
*unicode_name
;
2027 if ((newlen
<= 0) || (newlen
> MAXPATHLEN
))
2030 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
2031 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
2032 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
2034 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
2036 error
= utf8_decodestr((u_int8_t
*)filename
, newlen
- 1, unicode_name
,
2037 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
2039 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
2040 error
= sysctl_int(oldp
, oldlenp
, USER_ADDR_NULL
, 0, (int32_t *)&hint
);
2043 FREE(unicode_name
, M_TEMP
);
2044 FREE(filename
, M_TEMP
);
2047 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
2048 // make the file system journaled...
2049 vnode_t vp
= vfs_context_cwd(context
);
2052 struct cat_attr jnl_attr
, jinfo_attr
;
2053 struct cat_fork jnl_fork
, jinfo_fork
;
2057 /* Only root can enable journaling */
2065 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2068 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2069 printf("hfs: can't make a plain hfs volume journaled.\n");
2074 printf("hfs: volume @ mp %p is already journaled!\n", vnode_mount(vp
));
2078 vcb
= HFSTOVCB(hfsmp
);
2079 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
2080 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
2081 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
2083 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
2084 hfs_systemfile_unlock(hfsmp
, lockflags
);
2087 hfs_systemfile_unlock(hfsmp
, lockflags
);
2089 // make sure these both exist!
2090 if ( GetFileInfo(vcb
, kHFSRootFolderID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
2091 || GetFileInfo(vcb
, kHFSRootFolderID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2096 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, context
);
2098 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2099 (off_t
)name
[2], (off_t
)name
[3]);
2101 jvp
= hfsmp
->hfs_devvp
;
2102 jnl
= journal_create(jvp
,
2103 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2104 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2105 (off_t
)((unsigned)name
[3]),
2107 hfsmp
->hfs_phys_block_size
,
2110 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2113 printf("hfs: FAILED to create the journal!\n");
2114 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2115 VNOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2122 hfs_global_exclusive_lock_acquire(hfsmp
);
2125 * Flush all dirty metadata buffers.
2127 buf_flushdirtyblks(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, "hfs_sysctl");
2128 buf_flushdirtyblks(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2129 buf_flushdirtyblks(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2130 buf_flushdirtyblks(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2131 if (hfsmp
->hfs_attribute_vp
)
2132 buf_flushdirtyblks(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2134 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2135 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2139 // save this off for the hack-y check in hfs_remove()
2140 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2141 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2142 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2143 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2145 vfs_setflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
2147 hfs_global_exclusive_lock_release(hfsmp
);
2148 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2151 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2152 // clear the journaling bit
2153 vnode_t vp
= vfs_context_cwd(context
);
2155 /* Only root can disable journaling */
2165 * Disabling journaling is disallowed on volumes with directory hard links
2166 * because we have not tested the relevant code path.
2168 if (hfsmp
->hfs_private_attr
[DIR_HARDLINKS
].ca_entries
!= 0){
2169 printf("hfs: cannot disable journaling on volumes with directory hardlinks\n");
2173 printf("hfs: disabling journaling for mount @ %p\n", vnode_mount(vp
));
2175 hfs_global_exclusive_lock_acquire(hfsmp
);
2177 // Lights out for you buddy!
2178 journal_close(hfsmp
->jnl
);
2181 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2182 VNOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2185 vfs_clearflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
2186 hfsmp
->jnl_start
= 0;
2187 hfsmp
->hfs_jnlinfoblkid
= 0;
2188 hfsmp
->hfs_jnlfileid
= 0;
2190 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2192 hfs_global_exclusive_lock_release(hfsmp
);
2193 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2196 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2197 vnode_t vp
= vfs_context_cwd(context
);
2198 off_t jnl_start
, jnl_size
;
2204 if (hfsmp
->jnl
== NULL
) {
2208 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2209 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2212 if ((error
= copyout((caddr_t
)&jnl_start
, CAST_USER_ADDR_T(name
[1]), sizeof(off_t
))) != 0) {
2215 if ((error
= copyout((caddr_t
)&jnl_size
, CAST_USER_ADDR_T(name
[2]), sizeof(off_t
))) != 0) {
2220 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2222 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2224 } else if (name
[0] == VFS_CTL_QUERY
) {
2225 struct sysctl_req
*req
;
2227 struct user_vfsidctl user_vc
;
2230 boolean_t is_64_bit
;
2232 is_64_bit
= proc_is64bit(p
);
2233 req
= CAST_DOWN(struct sysctl_req
*, oldp
); /* we're new style vfs sysctl. */
2236 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2237 if (error
) return (error
);
2239 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
2242 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2243 if (error
) return (error
);
2245 mp
= vfs_getvfs(&vc
.vc_fsid
);
2247 if (mp
== NULL
) return (ENOENT
);
2249 hfsmp
= VFSTOHFS(mp
);
2250 bzero(&vq
, sizeof(vq
));
2251 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2252 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2253 } else if (name
[0] == HFS_REPLAY_JOURNAL
) {
2254 char *devnode
= NULL
;
2257 devnode_len
= *oldlenp
;
2258 MALLOC(devnode
, char *, devnode_len
+ 1, M_TEMP
, M_WAITOK
);
2259 if (devnode
== NULL
) {
2263 error
= copyin(oldp
, (caddr_t
)devnode
, devnode_len
);
2265 FREE(devnode
, M_TEMP
);
2268 devnode
[devnode_len
] = 0;
2270 error
= hfs_journal_replay(devnode
, context
);
2271 FREE(devnode
, M_TEMP
);
2280 hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, __unused vfs_context_t context
)
2284 error
= hfs_vget(VFSTOHFS(mp
), (cnid_t
)ino
, vpp
, 1);
2289 * ADLs may need to have their origin state updated
2290 * since build_path needs a valid parent.
2292 if (vnode_isdir(*vpp
) &&
2293 (VTOC(*vpp
)->c_flag
& C_HARDLINK
) &&
2294 (hfs_lock(VTOC(*vpp
), HFS_EXCLUSIVE_LOCK
) == 0)) {
2295 cnode_t
*cp
= VTOC(*vpp
);
2296 struct cat_desc cdesc
;
2298 if (!hfs_haslinkorigin(cp
) &&
2299 (cat_findname(VFSTOHFS(mp
), (cnid_t
)ino
, &cdesc
) == 0)) {
2300 if (cdesc
.cd_parentcnid
!=
2301 VFSTOHFS(mp
)->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
2302 hfs_savelinkorigin(cp
, cdesc
.cd_parentcnid
);
2304 cat_releasedesc(&cdesc
);
2313 * Look up an HFS object by ID.
2315 * The object is returned with an iocount reference and the cnode locked.
2317 * If the object is a file then it will represent the data fork.
2321 hfs_vget(struct hfsmount
*hfsmp
, cnid_t cnid
, struct vnode
**vpp
, int skiplock
)
2323 struct vnode
*vp
= NULLVP
;
2324 struct cat_desc cndesc
;
2325 struct cat_attr cnattr
;
2326 struct cat_fork cnfork
;
2327 u_int32_t linkref
= 0;
2330 /* Check for cnids that should't be exported. */
2331 if ((cnid
< kHFSFirstUserCatalogNodeID
) &&
2332 (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
)) {
2335 /* Don't export our private directories. */
2336 if (cnid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
||
2337 cnid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
2341 * Check the hash first
2343 vp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, cnid
, 0, skiplock
);
2349 bzero(&cndesc
, sizeof(cndesc
));
2350 bzero(&cnattr
, sizeof(cnattr
));
2351 bzero(&cnfork
, sizeof(cnfork
));
2354 * Not in hash, lookup in catalog
2356 if (cnid
== kHFSRootParentID
) {
2357 static char hfs_rootname
[] = "/";
2359 cndesc
.cd_nameptr
= (const u_int8_t
*)&hfs_rootname
[0];
2360 cndesc
.cd_namelen
= 1;
2361 cndesc
.cd_parentcnid
= kHFSRootParentID
;
2362 cndesc
.cd_cnid
= kHFSRootFolderID
;
2363 cndesc
.cd_flags
= CD_ISDIR
;
2365 cnattr
.ca_fileid
= kHFSRootFolderID
;
2366 cnattr
.ca_linkcount
= 1;
2367 cnattr
.ca_entries
= 1;
2368 cnattr
.ca_dircount
= 1;
2369 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
2373 const char *nameptr
;
2375 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2376 error
= cat_idlookup(hfsmp
, cnid
, 0, &cndesc
, &cnattr
, &cnfork
);
2377 hfs_systemfile_unlock(hfsmp
, lockflags
);
2385 * Check for a raw hardlink inode and save its linkref.
2387 pid
= cndesc
.cd_parentcnid
;
2388 nameptr
= (const char *)cndesc
.cd_nameptr
;
2390 if ((pid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2391 (bcmp(nameptr
, HFS_INODE_PREFIX
, HFS_INODE_PREFIX_LEN
) == 0)) {
2392 linkref
= strtoul(&nameptr
[HFS_INODE_PREFIX_LEN
], NULL
, 10);
2394 } else if ((pid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) &&
2395 (bcmp(nameptr
, HFS_DIRINODE_PREFIX
, HFS_DIRINODE_PREFIX_LEN
) == 0)) {
2396 linkref
= strtoul(&nameptr
[HFS_DIRINODE_PREFIX_LEN
], NULL
, 10);
2398 } else if ((pid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2399 (bcmp(nameptr
, HFS_DELETE_PREFIX
, HFS_DELETE_PREFIX_LEN
) == 0)) {
2401 cat_releasedesc(&cndesc
);
2402 return (ENOENT
); /* open unlinked file */
2407 * Finish initializing cnode descriptor for hardlinks.
2409 * We need a valid name and parent for reverse lookups.
2414 struct cat_desc linkdesc
;
2416 cnattr
.ca_linkref
= linkref
;
2419 * Pick up the first link in the chain and get a descriptor for it.
2420 * This allows blind volfs paths to work for hardlinks.
2422 if ((hfs_lookuplink(hfsmp
, linkref
, &prevlinkid
, &nextlinkid
) == 0) &&
2423 (nextlinkid
!= 0)) {
2424 if (cat_findname(hfsmp
, nextlinkid
, &linkdesc
) == 0) {
2425 cat_releasedesc(&cndesc
);
2426 bcopy(&linkdesc
, &cndesc
, sizeof(linkdesc
));
2432 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2434 VTOC(vp
)->c_flag
|= C_HARDLINK
;
2435 vnode_setmultipath(vp
);
2438 struct componentname cn
;
2440 /* Supply hfs_getnewvnode with a component name. */
2441 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2442 cn
.cn_nameiop
= LOOKUP
;
2443 cn
.cn_flags
= ISLASTCN
| HASBUF
;
2444 cn
.cn_context
= NULL
;
2445 cn
.cn_pnlen
= MAXPATHLEN
;
2446 cn
.cn_nameptr
= cn
.cn_pnbuf
;
2447 cn
.cn_namelen
= cndesc
.cd_namelen
;
2450 bcopy(cndesc
.cd_nameptr
, cn
.cn_nameptr
, cndesc
.cd_namelen
+ 1);
2452 error
= hfs_getnewvnode(hfsmp
, NULLVP
, &cn
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2454 if (error
== 0 && (VTOC(vp
)->c_flag
& C_HARDLINK
) && vnode_isdir(vp
)) {
2455 hfs_savelinkorigin(VTOC(vp
), cndesc
.cd_parentcnid
);
2457 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
2459 cat_releasedesc(&cndesc
);
2462 if (vp
&& skiplock
) {
2463 hfs_unlock(VTOC(vp
));
2470 * Flush out all the files in a filesystem.
2474 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2476 hfs_flushfiles(struct mount
*mp
, int flags
, __unused
struct proc
*p
)
2479 struct hfsmount
*hfsmp
;
2480 struct vnode
*skipvp
= NULLVP
;
2487 hfsmp
= VFSTOHFS(mp
);
2491 * The open quota files have an indirect reference on
2492 * the root directory vnode. We must account for this
2493 * extra reference when doing the intial vflush.
2496 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2498 /* Find out how many quota files we have open. */
2499 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2500 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2504 /* Obtain the root vnode so we can skip over it. */
2505 skipvp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, kHFSRootFolderID
, 0, 0);
2509 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2513 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2516 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2519 * See if there are additional references on the
2520 * root vp besides the ones obtained from the open
2521 * quota files and the hfs_chash_getvnode call above.
2524 (vnode_isinuse(skipvp
, quotafilecnt
))) {
2525 error
= EBUSY
; /* root directory is still open */
2527 hfs_unlock(VTOC(skipvp
));
2530 if (error
&& (flags
& FORCECLOSE
) == 0)
2533 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2534 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2536 hfs_quotaoff(p
, mp
, i
);
2538 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2546 * Update volume encoding bitmap (HFS Plus only)
2550 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2552 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2553 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2558 case kTextEncodingMacUkrainian
:
2559 index
= kIndexMacUkrainian
;
2561 case kTextEncodingMacFarsi
:
2562 index
= kIndexMacFarsi
;
2569 if (index
< 64 && (hfsmp
->encodingsBitmap
& (u_int64_t
)(1ULL << index
)) == 0) {
2570 HFS_MOUNT_LOCK(hfsmp
, TRUE
)
2571 hfsmp
->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2572 MarkVCBDirty(hfsmp
);
2573 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2578 * Update volume stats
2580 * On journal volumes this will cause a volume header flush
2584 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2590 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2592 MarkVCBDirty(hfsmp
);
2593 hfsmp
->hfs_mtime
= tv
.tv_sec
;
2599 if (hfsmp
->hfs_dircount
!= 0xFFFFFFFF)
2600 ++hfsmp
->hfs_dircount
;
2601 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2602 ++hfsmp
->vcbNmRtDirs
;
2605 if (hfsmp
->hfs_dircount
!= 0)
2606 --hfsmp
->hfs_dircount
;
2607 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2608 --hfsmp
->vcbNmRtDirs
;
2611 if (hfsmp
->hfs_filecount
!= 0xFFFFFFFF)
2612 ++hfsmp
->hfs_filecount
;
2613 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2617 if (hfsmp
->hfs_filecount
!= 0)
2618 --hfsmp
->hfs_filecount
;
2619 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2624 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2627 hfs_flushvolumeheader(hfsmp
, 0, 0);
2635 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2637 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2638 struct filefork
*fp
;
2639 HFSMasterDirectoryBlock
*mdb
;
2640 struct buf
*bp
= NULL
;
2645 sectorsize
= hfsmp
->hfs_phys_block_size
;
2646 retval
= (int)buf_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2653 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2655 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2657 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2658 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2659 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2660 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2661 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2662 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2663 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2664 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2666 namelen
= strlen((char *)vcb
->vcbVN
);
2667 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2668 /* Retry with MacRoman in case that's how it was exported. */
2670 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2672 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2673 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2674 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2675 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2676 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2678 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2680 fp
= VTOF(vcb
->extentsRefNum
);
2681 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2682 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2683 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2684 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2685 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2686 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2687 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2688 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2689 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2691 fp
= VTOF(vcb
->catalogRefNum
);
2692 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2693 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2694 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2695 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2696 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2697 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2698 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2699 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2700 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2702 MarkVCBClean( vcb
);
2704 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2706 /* If requested, flush out the alternate MDB */
2708 struct buf
*alt_bp
= NULL
;
2710 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2711 bcopy(mdb
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2713 (void) VNOP_BWRITE(alt_bp
);
2718 if (waitfor
!= MNT_WAIT
)
2721 retval
= VNOP_BWRITE(bp
);
2727 * Flush any dirty in-memory mount data to the on-disk
2730 * Note: the on-disk volume signature is intentionally
2731 * not flushed since the on-disk "H+" and "HX" signatures
2732 * are always stored in-memory as "H+".
2736 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2738 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2739 struct filefork
*fp
;
2740 HFSPlusVolumeHeader
*volumeHeader
;
2745 daddr64_t priIDSector
;
2747 u_int16_t signature
;
2748 u_int16_t hfsversion
;
2750 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2753 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
2754 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2756 critical
= altflush
;
2757 sectorsize
= hfsmp
->hfs_phys_block_size
;
2758 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2759 HFS_PRI_SECTOR(sectorsize
));
2761 if (hfs_start_transaction(hfsmp
) != 0) {
2765 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2770 hfs_end_transaction(hfsmp
);
2772 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2777 journal_modify_block_start(hfsmp
->jnl
, bp
);
2780 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2783 * Sanity check what we just read.
2785 signature
= SWAP_BE16 (volumeHeader
->signature
);
2786 hfsversion
= SWAP_BE16 (volumeHeader
->version
);
2787 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2788 (hfsversion
< kHFSPlusVersion
) || (hfsversion
> 100) ||
2789 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2791 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2792 vcb
->vcbVN
, signature
, hfsversion
,
2793 SWAP_BE32 (volumeHeader
->blockSize
));
2795 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2801 * For embedded HFS+ volumes, update create date if it changed
2802 * (ie from a setattrlist call)
2804 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2805 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2807 HFSMasterDirectoryBlock
*mdb
;
2809 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
),
2810 sectorsize
, NOCRED
, &bp2
);
2816 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp2
) +
2817 HFS_PRI_OFFSET(sectorsize
));
2819 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2822 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2825 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2828 journal_modify_block_end(hfsmp
->jnl
, bp2
, NULL
, NULL
);
2830 (void) VNOP_BWRITE(bp2
); /* write out the changes */
2835 buf_brelse(bp2
); /* just release it */
2840 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2842 /* Note: only update the lower 16 bits worth of attributes */
2843 volumeHeader
->attributes
= SWAP_BE32 (vcb
->vcbAtrb
);
2844 volumeHeader
->journalInfoBlock
= SWAP_BE32 (vcb
->vcbJinfoBlock
);
2846 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2848 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2850 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2851 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2852 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2853 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2854 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2855 volumeHeader
->totalBlocks
= SWAP_BE32 (vcb
->totalBlocks
);
2856 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2857 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2858 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2859 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2860 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2861 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2862 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2864 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0) {
2865 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2870 * System files are only dirty when altflush is set.
2872 if (altflush
== 0) {
2876 /* Sync Extents over-flow file meta data */
2877 fp
= VTOF(vcb
->extentsRefNum
);
2878 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2879 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2880 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2881 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2882 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2883 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2885 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2886 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2887 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2888 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2891 /* Sync Catalog file meta data */
2892 fp
= VTOF(vcb
->catalogRefNum
);
2893 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2894 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2895 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2896 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2897 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2898 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2900 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2901 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2902 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2903 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2906 /* Sync Allocation file meta data */
2907 fp
= VTOF(vcb
->allocationsRefNum
);
2908 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2909 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2910 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2911 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2912 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2913 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2915 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2916 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2917 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2918 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2921 /* Sync Attribute file meta data */
2922 if (hfsmp
->hfs_attribute_vp
) {
2923 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
2924 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2925 volumeHeader
->attributesFile
.extents
[i
].startBlock
=
2926 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2927 volumeHeader
->attributesFile
.extents
[i
].blockCount
=
2928 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2930 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2931 volumeHeader
->attributesFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2932 volumeHeader
->attributesFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2933 volumeHeader
->attributesFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2936 /* Sync Startup file meta data */
2937 if (hfsmp
->hfs_startup_vp
) {
2938 fp
= VTOF(hfsmp
->hfs_startup_vp
);
2939 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2940 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2941 volumeHeader
->startupFile
.extents
[i
].startBlock
=
2942 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2943 volumeHeader
->startupFile
.extents
[i
].blockCount
=
2944 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2946 volumeHeader
->startupFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2947 volumeHeader
->startupFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2948 volumeHeader
->startupFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2949 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2954 MarkVCBClean(hfsmp
);
2955 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2957 /* If requested, flush out the alternate volume header */
2958 if (altflush
&& hfsmp
->hfs_alt_id_sector
) {
2959 struct buf
*alt_bp
= NULL
;
2961 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2963 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2966 bcopy(volumeHeader
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2969 journal_modify_block_end(hfsmp
->jnl
, alt_bp
, NULL
, NULL
);
2971 (void) VNOP_BWRITE(alt_bp
);
2978 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
2980 if (waitfor
!= MNT_WAIT
)
2983 retval
= VNOP_BWRITE(bp
);
2984 /* When critical data changes, flush the device cache */
2985 if (critical
&& (retval
== 0)) {
2986 (void) VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2987 NULL
, FWRITE
, NULL
);
2991 hfs_end_transaction(hfsmp
);
2998 * Extend a file system.
3002 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
3004 struct proc
*p
= vfs_context_proc(context
);
3005 kauth_cred_t cred
= vfs_context_ucred(context
);
3007 struct vnode
*devvp
;
3009 struct filefork
*fp
= NULL
;
3011 struct cat_fork forkdata
;
3013 u_int64_t newblkcnt
;
3014 u_int64_t prev_phys_block_count
;
3016 u_int64_t sectorcnt
;
3017 u_int32_t sectorsize
;
3018 daddr64_t prev_alt_sector
;
3022 int64_t oldBitmapSize
;
3023 Boolean usedExtendFileC
= false;
3025 devvp
= hfsmp
->hfs_devvp
;
3026 vcb
= HFSTOVCB(hfsmp
);
3029 * - HFS Plus file systems only.
3030 * - Journaling must be enabled.
3031 * - No embedded volumes.
3033 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
3034 (hfsmp
->jnl
== NULL
) ||
3035 (vcb
->hfsPlusIOPosOffset
!= 0)) {
3039 * If extending file system by non-root, then verify
3040 * ownership and check permissions.
3042 if (suser(cred
, NULL
)) {
3043 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0);
3047 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
3049 error
= hfs_write_access(vp
, cred
, p
, false);
3051 hfs_unlock(VTOC(vp
));
3056 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
3060 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, context
)) {
3063 if (sectorsize
!= hfsmp
->hfs_phys_block_size
) {
3066 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, context
)) {
3069 if ((sectorsize
* sectorcnt
) < newsize
) {
3070 printf("hfs_extendfs: not enough space on device\n");
3073 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3076 * Validate new size.
3078 if ((newsize
<= oldsize
) || (newsize
% sectorsize
)) {
3079 printf("hfs_extendfs: invalid size\n");
3082 newblkcnt
= newsize
/ vcb
->blockSize
;
3083 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
3086 addblks
= newblkcnt
- vcb
->totalBlocks
;
3088 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
3090 * Enclose changes inside a transaction.
3092 if (hfs_start_transaction(hfsmp
) != 0) {
3097 * Note: we take the attributes lock in case we have an attribute data vnode
3098 * which needs to change size.
3100 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3101 vp
= vcb
->allocationsRefNum
;
3103 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
3106 * Calculate additional space required (if any) by allocation bitmap.
3108 oldBitmapSize
= fp
->ff_size
;
3109 bitmapblks
= roundup((newblkcnt
+7) / 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
3110 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
3111 bitmapblks
-= fp
->ff_blocks
;
3115 if (bitmapblks
> 0) {
3121 * Get the bitmap's current size (in allocation blocks) so we know
3122 * where to start zero filling once the new space is added. We've
3123 * got to do this before the bitmap is grown.
3125 blkno
= (daddr64_t
)fp
->ff_blocks
;
3128 * Try to grow the allocation file in the normal way, using allocation
3129 * blocks already existing in the file system. This way, we might be
3130 * able to grow the bitmap contiguously, or at least in the metadata
3133 error
= ExtendFileC(vcb
, fp
, bitmapblks
* vcb
->blockSize
, 0,
3134 kEFAllMask
| kEFNoClumpMask
| kEFReserveMask
| kEFMetadataMask
,
3138 usedExtendFileC
= true;
3141 * If the above allocation failed, fall back to allocating the new
3142 * extent of the bitmap from the space we're going to add. Since those
3143 * blocks don't yet belong to the file system, we have to update the
3144 * extent list directly, and manually adjust the file size.
3147 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
3149 printf("hfs_extendfs: error %d adding extents\n", error
);
3152 fp
->ff_blocks
+= bitmapblks
;
3153 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3154 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3158 * Update the allocation file's size to include the newly allocated
3159 * blocks. Note that ExtendFileC doesn't do this, which is why this
3160 * statement is outside the above "if" statement.
3162 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3165 * Zero out the new bitmap blocks.
3170 blkcnt
= bitmapblks
;
3171 while (blkcnt
> 0) {
3172 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
3179 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
3181 error
= (int)buf_bwrite(bp
);
3189 printf("hfs_extendfs: error %d clearing blocks\n", error
);
3193 * Mark the new bitmap space as allocated.
3195 * Note that ExtendFileC will have marked any blocks it allocated, so
3196 * this is only needed if we used AddFileExtent. Also note that this
3197 * has to come *after* the zero filling of new blocks in the case where
3198 * we used AddFileExtent (since the part of the bitmap we're touching
3199 * is in those newly allocated blocks).
3201 if (!usedExtendFileC
) {
3202 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
3204 printf("hfs_extendfs: error %d setting bitmap\n", error
);
3207 vcb
->freeBlocks
-= bitmapblks
;
3211 * Mark the new alternate VH as allocated.
3213 if (vcb
->blockSize
== 512)
3214 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
3216 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
3218 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
3222 * Mark the old alternate VH as free.
3224 if (vcb
->blockSize
== 512)
3225 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
3227 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
3229 * Adjust file system variables for new space.
3231 prev_phys_block_count
= hfsmp
->hfs_phys_block_count
;
3232 prev_alt_sector
= hfsmp
->hfs_alt_id_sector
;
3234 vcb
->totalBlocks
+= addblks
;
3235 vcb
->freeBlocks
+= addblks
;
3236 hfsmp
->hfs_phys_block_count
= newsize
/ sectorsize
;
3237 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sectorsize
) +
3238 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
3240 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3242 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
3244 * Restore to old state.
3246 if (usedExtendFileC
) {
3247 (void) TruncateFileC(vcb
, fp
, oldBitmapSize
, false);
3249 fp
->ff_blocks
-= bitmapblks
;
3250 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3252 * No need to mark the excess blocks free since those bitmap blocks
3253 * are no longer part of the bitmap. But we do need to undo the
3254 * effect of the "vcb->freeBlocks -= bitmapblks" above.
3256 vcb
->freeBlocks
+= bitmapblks
;
3258 vcb
->totalBlocks
-= addblks
;
3259 vcb
->freeBlocks
-= addblks
;
3260 hfsmp
->hfs_phys_block_count
= prev_phys_block_count
;
3261 hfsmp
->hfs_alt_id_sector
= prev_alt_sector
;
3263 if (vcb
->blockSize
== 512)
3264 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
3266 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
3270 * Invalidate the old alternate volume header.
3273 if (prev_alt_sector
) {
3274 if (buf_meta_bread(hfsmp
->hfs_devvp
, prev_alt_sector
, sectorsize
,
3275 NOCRED
, &bp
) == 0) {
3276 journal_modify_block_start(hfsmp
->jnl
, bp
);
3278 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
3280 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
3287 * TODO: Adjust the size of the metadata zone based on new volume size?
3291 * Adjust the size of hfsmp->hfs_attrdata_vp
3293 if (hfsmp
->hfs_attrdata_vp
) {
3294 struct cnode
*attr_cp
;
3295 struct filefork
*attr_fp
;
3297 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
3298 attr_cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
3299 attr_fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
3301 attr_cp
->c_blocks
= newblkcnt
;
3302 attr_fp
->ff_blocks
= newblkcnt
;
3303 attr_fp
->ff_extents
[0].blockCount
= newblkcnt
;
3304 attr_fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
3305 ubc_setsize(hfsmp
->hfs_attrdata_vp
, attr_fp
->ff_size
);
3306 vnode_put(hfsmp
->hfs_attrdata_vp
);
3312 /* Restore allocation fork. */
3313 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3314 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3318 Regardless of whether or not the totalblocks actually increased,
3319 we should reset the allocLimit field. If it changed, it will
3320 get updated; if not, it will remain the same.
3322 hfsmp
->allocLimit
= vcb
->totalBlocks
;
3323 hfs_systemfile_unlock(hfsmp
, lockflags
);
3324 hfs_end_transaction(hfsmp
);
3329 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3332 * Truncate a file system (while still mounted).
3336 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
3338 struct buf
*bp
= NULL
;
3340 u_int32_t newblkcnt
;
3341 u_int32_t reclaimblks
= 0;
3343 int transaction_begun
= 0;
3346 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3347 if (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) {
3348 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3351 hfsmp
->hfs_flags
|= HFS_RESIZE_IN_PROGRESS
;
3352 hfsmp
->hfs_resize_filesmoved
= 0;
3353 hfsmp
->hfs_resize_totalfiles
= 0;
3354 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3357 * - Journaled HFS Plus volumes only.
3358 * - No embedded volumes.
3360 if ((hfsmp
->jnl
== NULL
) ||
3361 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
3365 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3366 newblkcnt
= newsize
/ hfsmp
->blockSize
;
3367 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
3369 /* Make sure new size is valid. */
3370 if ((newsize
< HFS_MIN_SIZE
) ||
3371 (newsize
>= oldsize
) ||
3372 (newsize
% hfsmp
->hfs_phys_block_size
)) {
3376 /* Make sure there's enough space to work with. */
3377 if (reclaimblks
>= hfs_freeblks(hfsmp
, 1)) {
3378 printf("hfs_truncatefs: insufficient space (need %u blocks; have %u blocks)\n", reclaimblks
, hfs_freeblks(hfsmp
, 1));
3383 /* Start with a clean journal. */
3384 journal_flush(hfsmp
->jnl
);
3386 if (hfs_start_transaction(hfsmp
) != 0) {
3390 transaction_begun
= 1;
3393 * Prevent new allocations from using the part we're trying to truncate.
3395 * NOTE: allocLimit is set to the allocation block number where the new
3396 * alternate volume header will be. That way there will be no files to
3397 * interfere with allocating the new alternate volume header, and no files
3398 * in the allocation blocks beyond (i.e. the blocks we're trying to
3401 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3402 if (hfsmp
->blockSize
== 512)
3403 hfsmp
->allocLimit
= newblkcnt
- 2;
3405 hfsmp
->allocLimit
= newblkcnt
- 1;
3406 hfsmp
->freeBlocks
-= reclaimblks
;
3407 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3410 * Look for files that have blocks at or beyond the location of the
3411 * new alternate volume header.
3413 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
3415 * hfs_reclaimspace will use separate transactions when
3416 * relocating files (so we don't overwhelm the journal).
3418 hfs_end_transaction(hfsmp
);
3419 transaction_begun
= 0;
3421 /* Attempt to reclaim some space. */
3422 if (hfs_reclaimspace(hfsmp
, hfsmp
->allocLimit
, reclaimblks
, context
) != 0) {
3423 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp
->vcbVN
);
3427 if (hfs_start_transaction(hfsmp
) != 0) {
3431 transaction_begun
= 1;
3433 /* Check if we're clear now. */
3434 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
3435 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp
->vcbVN
);
3436 error
= EAGAIN
; /* tell client to try again */
3442 * Note: we take the attributes lock in case we have an attribute data vnode
3443 * which needs to change size.
3445 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3448 * Mark the old alternate volume header as free.
3449 * We don't bother shrinking allocation bitmap file.
3451 if (hfsmp
->blockSize
== 512)
3452 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
3454 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
3457 * Allocate last 1KB for alternate volume header.
3459 error
= BlockMarkAllocated(hfsmp
, hfsmp
->allocLimit
, (hfsmp
->blockSize
== 512) ? 2 : 1);
3461 printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error
);
3466 * Invalidate the existing alternate volume header.
3468 * Don't include this in a transaction (don't call journal_modify_block)
3469 * since this block will be outside of the truncated file system!
3471 if (hfsmp
->hfs_alt_id_sector
) {
3472 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
,
3473 hfsmp
->hfs_phys_block_size
, NOCRED
, &bp
) == 0) {
3475 bzero((void*)((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_phys_block_size
)), kMDBSize
);
3476 (void) VNOP_BWRITE(bp
);
3483 /* Log successful shrinking. */
3484 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
3485 hfsmp
->vcbVN
, newblkcnt
, hfsmp
->totalBlocks
);
3488 * Adjust file system variables and flush them to disk.
3490 hfsmp
->totalBlocks
= newblkcnt
;
3491 hfsmp
->hfs_phys_block_count
= newsize
/ hfsmp
->hfs_phys_block_size
;
3492 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
, hfsmp
->hfs_phys_block_count
);
3493 MarkVCBDirty(hfsmp
);
3494 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3496 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
3499 * TODO: Adjust the size of the metadata zone based on new volume size?
3503 * Adjust the size of hfsmp->hfs_attrdata_vp
3505 if (hfsmp
->hfs_attrdata_vp
) {
3507 struct filefork
*fp
;
3509 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
3510 cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
3511 fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
3513 cp
->c_blocks
= newblkcnt
;
3514 fp
->ff_blocks
= newblkcnt
;
3515 fp
->ff_extents
[0].blockCount
= newblkcnt
;
3516 fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
3517 ubc_setsize(hfsmp
->hfs_attrdata_vp
, fp
->ff_size
);
3518 vnode_put(hfsmp
->hfs_attrdata_vp
);
3524 hfsmp
->freeBlocks
+= reclaimblks
;
3526 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3527 hfsmp
->allocLimit
= hfsmp
->totalBlocks
;
3528 if (hfsmp
->nextAllocation
>= hfsmp
->allocLimit
)
3529 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_end
+ 1;
3530 hfsmp
->hfs_flags
&= ~HFS_RESIZE_IN_PROGRESS
;
3531 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3534 hfs_systemfile_unlock(hfsmp
, lockflags
);
3536 if (transaction_begun
) {
3537 hfs_end_transaction(hfsmp
);
3538 journal_flush(hfsmp
->jnl
);
3546 * Invalidate the physical block numbers associated with buffer cache blocks
3547 * in the given extent of the given vnode.
3549 struct hfs_inval_blk_no
{
3550 daddr64_t sectorStart
;
3551 daddr64_t sectorCount
;
3554 hfs_invalidate_block_numbers_callback(buf_t bp
, void *args_in
)
3557 struct hfs_inval_blk_no
*args
;
3559 blkno
= buf_blkno(bp
);
3562 if (blkno
>= args
->sectorStart
&& blkno
< args
->sectorStart
+args
->sectorCount
)
3563 buf_setblkno(bp
, buf_lblkno(bp
));
3565 return BUF_RETURNED
;
3568 hfs_invalidate_sectors(struct vnode
*vp
, daddr64_t sectorStart
, daddr64_t sectorCount
)
3570 struct hfs_inval_blk_no args
;
3571 args
.sectorStart
= sectorStart
;
3572 args
.sectorCount
= sectorCount
;
3574 buf_iterate(vp
, hfs_invalidate_block_numbers_callback
, BUF_SCAN_DIRTY
|BUF_SCAN_CLEAN
, &args
);
3579 * Copy the contents of an extent to a new location. Also invalidates the
3580 * physical block number of any buffer cache block in the copied extent
3581 * (so that if the block is written, it will go through VNOP_BLOCKMAP to
3582 * determine the new physical block number).
3586 struct hfsmount
*hfsmp
,
3587 struct vnode
*vp
, /* The file whose extent is being copied. */
3588 u_int32_t oldStart
, /* The start of the source extent. */
3589 u_int32_t newStart
, /* The start of the destination extent. */
3590 u_int32_t blockCount
, /* The number of allocation blocks to copy. */
3591 vfs_context_t context
)
3595 void *buffer
= NULL
;
3596 struct vfsioattr ioattr
;
3600 u_int32_t ioSizeSectors
; /* Device sectors in this I/O */
3601 daddr64_t srcSector
, destSector
;
3602 u_int32_t sectorsPerBlock
= hfsmp
->blockSize
/ hfsmp
->hfs_phys_block_size
;
3605 * Sanity check that we have locked the vnode of the file we're copying.
3607 * But since hfs_systemfile_lock() doesn't actually take the lock on
3608 * the allocation file if a journal is active, ignore the check if the
3609 * file being copied is the allocation file.
3611 struct cnode
*cp
= VTOC(vp
);
3612 if (cp
!= hfsmp
->hfs_allocation_cp
&& cp
->c_lockowner
!= current_thread())
3613 panic("hfs_copy_extent: vp=%p (cp=%p) not owned?\n", vp
, cp
);
3616 * Wait for any in-progress writes to this vnode to complete, so that we'll
3617 * be copying consistent bits. (Otherwise, it's possible that an async
3618 * write will complete to the old extent after we read from it. That
3619 * could lead to corruption.)
3621 err
= vnode_waitforwrites(vp
, 0, 0, 0, "hfs_copy_extent");
3623 printf("hfs_copy_extent: Error %d from vnode_waitforwrites\n", err
);
3628 * Determine the I/O size to use
3630 * NOTE: Many external drives will result in an ioSize of 128KB.
3631 * TODO: Should we use a larger buffer, doing several consecutive
3632 * reads, then several consecutive writes?
3634 vfs_ioattr(hfsmp
->hfs_mp
, &ioattr
);
3635 bufferSize
= MIN(ioattr
.io_maxreadcnt
, ioattr
.io_maxwritecnt
);
3636 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &buffer
, bufferSize
))
3639 /* Get a buffer for doing the I/O */
3640 bp
= buf_alloc(hfsmp
->hfs_devvp
);
3641 buf_setdataptr(bp
, (uintptr_t)buffer
);
3643 resid
= (off_t
) blockCount
* (off_t
) hfsmp
->blockSize
;
3644 srcSector
= (daddr64_t
) oldStart
* hfsmp
->blockSize
/ hfsmp
->hfs_phys_block_size
;
3645 destSector
= (daddr64_t
) newStart
* hfsmp
->blockSize
/ hfsmp
->hfs_phys_block_size
;
3647 ioSize
= MIN(bufferSize
, resid
);
3648 ioSizeSectors
= ioSize
/ hfsmp
->hfs_phys_block_size
;
3650 /* Prepare the buffer for reading */
3651 buf_reset(bp
, B_READ
);
3652 buf_setsize(bp
, ioSize
);
3653 buf_setcount(bp
, ioSize
);
3654 buf_setblkno(bp
, srcSector
);
3655 buf_setlblkno(bp
, srcSector
);
3658 err
= VNOP_STRATEGY(bp
);
3660 err
= buf_biowait(bp
);
3662 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err
);
3666 /* Prepare the buffer for writing */
3667 buf_reset(bp
, B_WRITE
);
3668 buf_setsize(bp
, ioSize
);
3669 buf_setcount(bp
, ioSize
);
3670 buf_setblkno(bp
, destSector
);
3671 buf_setlblkno(bp
, destSector
);
3672 if (journal_uses_fua(hfsmp
->jnl
))
3676 vnode_startwrite(hfsmp
->hfs_devvp
);
3677 err
= VNOP_STRATEGY(bp
);
3679 err
= buf_biowait(bp
);
3681 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err
);
3686 srcSector
+= ioSizeSectors
;
3687 destSector
+= ioSizeSectors
;
3692 kmem_free(kernel_map
, (vm_offset_t
)buffer
, bufferSize
);
3694 /* Make sure all writes have been flushed to disk. */
3695 if (!journal_uses_fua(hfsmp
->jnl
)) {
3696 err
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, context
);
3698 printf("hfs_copy_extent: DKIOCSYNCHRONIZECACHE failed (%d)\n", err
);
3699 err
= 0; /* Don't fail the copy. */
3704 hfs_invalidate_sectors(vp
, (daddr64_t
)oldStart
*sectorsPerBlock
, (daddr64_t
)blockCount
*sectorsPerBlock
);
3711 * Reclaim space at the end of a volume, used by a given system file.
3713 * This routine attempts to move any extent which contains allocation blocks
3714 * at or after "startblk." A separate transaction is used to do the move.
3715 * The contents of any moved extents are read and written via the volume's
3716 * device vnode -- NOT via "vp." During the move, moved blocks which are part
3717 * of a transaction have their physical block numbers invalidated so they will
3718 * eventually be written to their new locations.
3720 * This routine can be used to move overflow extents for the allocation file.
3723 * hfsmp The volume being resized.
3724 * startblk Blocks >= this allocation block need to be moved.
3725 * locks Which locks need to be taken for the given system file.
3726 * vp The vnode for the system file.
3729 * moved Set to true if any extents were moved.
3732 hfs_relocate_callback(__unused HFSPlusExtentKey
*key
, HFSPlusExtentRecord
*record
, HFSPlusExtentRecord
*state
)
3734 bcopy(state
, record
, sizeof(HFSPlusExtentRecord
));
3738 hfs_reclaim_sys_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, u_long startblk
, int locks
, Boolean
*moved
, vfs_context_t context
)
3745 u_int32_t oldStartBlock
;
3746 u_int32_t newStartBlock
;
3747 u_int32_t blockCount
;
3748 struct filefork
*fp
;
3750 /* If there is no vnode for this file, then there's nothing to do. */
3754 /* printf("hfs_reclaim_sys_file: %.*s\n", VTOC(vp)->c_desc.cd_namelen, VTOC(vp)->c_desc.cd_nameptr); */
3756 /* We always need the allocation bitmap and extents B-tree */
3757 locks
|= SFL_BITMAP
| SFL_EXTENTS
;
3759 error
= hfs_start_transaction(hfsmp
);
3761 printf("hfs_reclaim_sys_file: hfs_start_transaction returned %d\n", error
);
3764 lockflags
= hfs_systemfile_lock(hfsmp
, locks
, HFS_EXCLUSIVE_LOCK
);
3768 /* Relocate non-overflow extents */
3769 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3770 if (fp
->ff_extents
[i
].blockCount
== 0)
3772 oldStartBlock
= fp
->ff_extents
[i
].startBlock
;
3773 blockCount
= fp
->ff_extents
[i
].blockCount
;
3774 datablks
+= blockCount
;
3775 block
= oldStartBlock
+ blockCount
;
3776 if (block
> startblk
) {
3777 error
= BlockAllocate(hfsmp
, 1, blockCount
, blockCount
, true, true, &newStartBlock
, &blockCount
);
3779 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error
);
3782 if (blockCount
!= fp
->ff_extents
[i
].blockCount
) {
3783 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount
, fp
->ff_extents
[i
].blockCount
);
3786 error
= hfs_copy_extent(hfsmp
, vp
, oldStartBlock
, newStartBlock
, blockCount
, context
);
3788 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error
);
3791 fp
->ff_extents
[i
].startBlock
= newStartBlock
;
3792 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3794 error
= BlockDeallocate(hfsmp
, oldStartBlock
, blockCount
);
3796 /* TODO: Mark volume inconsistent? */
3797 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error
);
3800 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3802 /* TODO: Mark volume inconsistent? */
3803 printf("hfs_reclaim_sys_file: hfs_flushvolumeheader returned %d\n", error
);
3809 /* Relocate overflow extents (if any) */
3810 if (i
== kHFSPlusExtentDensity
&& fp
->ff_blocks
> datablks
) {
3811 struct BTreeIterator
*iterator
= NULL
;
3812 struct FSBufferDescriptor btdata
;
3813 HFSPlusExtentRecord record
;
3814 HFSPlusExtentKey
*key
;
3819 forktype
= VNODE_IS_RSRC(vp
) ? 0xFF : 0;
3820 fileID
= VTOC(vp
)->c_cnid
;
3821 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &iterator
, sizeof(*iterator
))) {
3822 printf("hfs_reclaim_sys_file: kmem_alloc failed!\n");
3827 bzero(iterator
, sizeof(*iterator
));
3828 key
= (HFSPlusExtentKey
*) &iterator
->key
;
3829 key
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
3830 key
->forkType
= forktype
;
3831 key
->fileID
= fileID
;
3832 key
->startBlock
= datablks
;
3834 btdata
.bufferAddress
= &record
;
3835 btdata
.itemSize
= sizeof(record
);
3836 btdata
.itemCount
= 1;
3838 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
3840 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
3841 while (error
== 0) {
3842 /* Stop when we encounter a different file or fork. */
3843 if ((key
->fileID
!= fileID
) ||
3844 (key
->forkType
!= forktype
)) {
3848 * Check if the file overlaps target space.
3850 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3851 if (record
[i
].blockCount
== 0) {
3854 oldStartBlock
= record
[i
].startBlock
;
3855 blockCount
= record
[i
].blockCount
;
3856 block
= oldStartBlock
+ blockCount
;
3857 if (block
> startblk
) {
3858 error
= BlockAllocate(hfsmp
, 1, blockCount
, blockCount
, true, true, &newStartBlock
, &blockCount
);
3860 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error
);
3863 if (blockCount
!= record
[i
].blockCount
) {
3864 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount
, fp
->ff_extents
[i
].blockCount
);
3865 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3868 error
= hfs_copy_extent(hfsmp
, vp
, oldStartBlock
, newStartBlock
, blockCount
, context
);
3870 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error
);
3871 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3874 record
[i
].startBlock
= newStartBlock
;
3875 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3878 * NOTE: To support relocating overflow extents of the
3879 * allocation file, we must update the BTree record BEFORE
3880 * deallocating the old extent so that BlockDeallocate will
3881 * use the extent's new location to calculate physical block
3882 * numbers. (This is for the case where the old extent's
3883 * bitmap bits actually reside in the extent being moved.)
3885 error
= BTUpdateRecord(fcb
, iterator
, (IterateCallBackProcPtr
) hfs_relocate_callback
, &record
);
3887 /* TODO: Mark volume inconsistent? */
3888 printf("hfs_reclaim_sys_file: BTUpdateRecord returned %d\n", error
);
3891 error
= BlockDeallocate(hfsmp
, oldStartBlock
, blockCount
);
3893 /* TODO: Mark volume inconsistent? */
3894 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error
);
3899 /* Look for more records. */
3900 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3901 if (error
== btNotFound
) {
3907 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3913 hfs_systemfile_unlock(hfsmp
, lockflags
);
3914 error
= hfs_end_transaction(hfsmp
);
3916 printf("hfs_reclaim_sys_file: hfs_end_transaction returned %d\n", error
);
3922 (void) BlockDeallocate(hfsmp
, newStartBlock
, blockCount
);
3924 (void) hfs_systemfile_unlock(hfsmp
, lockflags
);
3925 (void) hfs_end_transaction(hfsmp
);
3931 * This journal_relocate callback updates the journal info block to point
3932 * at the new journal location. This write must NOT be done using the
3933 * transaction. We must write the block immediately. We must also force
3934 * it to get to the media so that the new journal location will be seen by
3935 * the replay code before we can safely let journaled blocks be written
3936 * to their normal locations.
3938 * The tests for journal_uses_fua below are mildly hacky. Since the journal
3939 * and the file system are both on the same device, I'm leveraging what
3940 * the journal has decided about FUA.
3942 struct hfs_journal_relocate_args
{
3943 struct hfsmount
*hfsmp
;
3944 vfs_context_t context
;
3945 u_int32_t newStartBlock
;
3949 hfs_journal_relocate_callback(void *_args
)
3952 struct hfs_journal_relocate_args
*args
= _args
;
3953 struct hfsmount
*hfsmp
= args
->hfsmp
;
3955 JournalInfoBlock
*jibp
;
3957 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
3958 hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_phys_block_size
),
3959 hfsmp
->blockSize
, vfs_context_ucred(args
->context
), &bp
);
3961 printf("hfs_reclaim_journal_file: failed to read JIB (%d)\n", error
);
3964 jibp
= (JournalInfoBlock
*) buf_dataptr(bp
);
3965 jibp
->offset
= SWAP_BE64((u_int64_t
)args
->newStartBlock
* hfsmp
->blockSize
);
3966 jibp
->size
= SWAP_BE64(hfsmp
->jnl_size
);
3967 if (journal_uses_fua(hfsmp
->jnl
))
3969 error
= buf_bwrite(bp
);
3971 printf("hfs_reclaim_journal_file: failed to write JIB (%d)\n", error
);
3974 if (!journal_uses_fua(hfsmp
->jnl
)) {
3975 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, args
->context
);
3977 printf("hfs_reclaim_journal_file: DKIOCSYNCHRONIZECACHE failed (%d)\n", error
);
3978 error
= 0; /* Don't fail the operation. */
3987 hfs_reclaim_journal_file(struct hfsmount
*hfsmp
, vfs_context_t context
)
3991 u_int32_t newStartBlock
;
3992 u_int32_t oldBlockCount
;
3993 u_int32_t newBlockCount
;
3994 struct cat_desc journal_desc
;
3995 struct cat_attr journal_attr
;
3996 struct cat_fork journal_fork
;
3997 struct hfs_journal_relocate_args callback_args
;
3999 error
= hfs_start_transaction(hfsmp
);
4001 printf("hfs_reclaim_journal_file: hfs_start_transaction returned %d\n", error
);
4004 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4006 oldBlockCount
= hfsmp
->jnl_size
/ hfsmp
->blockSize
;
4008 /* TODO: Allow the journal to change size based on the new volume size. */
4009 error
= BlockAllocate(hfsmp
, 1, oldBlockCount
, oldBlockCount
, true, true, &newStartBlock
, &newBlockCount
);
4011 printf("hfs_reclaim_journal_file: BlockAllocate returned %d\n", error
);
4014 if (newBlockCount
!= oldBlockCount
) {
4015 printf("hfs_reclaim_journal_file: newBlockCount != oldBlockCount (%u, %u)\n", newBlockCount
, oldBlockCount
);
4019 error
= BlockDeallocate(hfsmp
, hfsmp
->jnl_start
, oldBlockCount
);
4021 printf("hfs_reclaim_journal_file: BlockDeallocate returned %d\n", error
);
4025 /* Update the catalog record for .journal */
4026 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlfileid
, 1, &journal_desc
, &journal_attr
, &journal_fork
);
4028 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error
);
4031 journal_fork
.cf_size
= newBlockCount
* hfsmp
->blockSize
;
4032 journal_fork
.cf_extents
[0].startBlock
= newStartBlock
;
4033 journal_fork
.cf_extents
[0].blockCount
= newBlockCount
;
4034 journal_fork
.cf_blocks
= newBlockCount
;
4035 error
= cat_update(hfsmp
, &journal_desc
, &journal_attr
, &journal_fork
, NULL
);
4036 cat_releasedesc(&journal_desc
); /* all done with cat descriptor */
4038 printf("hfs_reclaim_journal_file: cat_update returned %d\n", error
);
4041 callback_args
.hfsmp
= hfsmp
;
4042 callback_args
.context
= context
;
4043 callback_args
.newStartBlock
= newStartBlock
;
4045 error
= journal_relocate(hfsmp
->jnl
, (off_t
)newStartBlock
*hfsmp
->blockSize
,
4046 (off_t
)newBlockCount
*hfsmp
->blockSize
, 0,
4047 hfs_journal_relocate_callback
, &callback_args
);
4049 /* NOTE: journal_relocate will mark the journal invalid. */
4050 printf("hfs_reclaim_journal_file: journal_relocate returned %d\n", error
);
4053 hfsmp
->jnl_start
= newStartBlock
;
4054 hfsmp
->jnl_size
= (off_t
)newBlockCount
* hfsmp
->blockSize
;
4056 hfs_systemfile_unlock(hfsmp
, lockflags
);
4057 error
= hfs_end_transaction(hfsmp
);
4059 printf("hfs_reclaim_journal_file: hfs_end_transaction returned %d\n", error
);
4065 (void) BlockDeallocate(hfsmp
, newStartBlock
, newBlockCount
);
4067 hfs_systemfile_unlock(hfsmp
, lockflags
);
4068 (void) hfs_end_transaction(hfsmp
);
4074 * Move the journal info block to a new location. We have to make sure the
4075 * new copy of the journal info block gets to the media first, then change
4076 * the field in the volume header and the catalog record.
4079 hfs_reclaim_journal_info_block(struct hfsmount
*hfsmp
, vfs_context_t context
)
4084 u_int32_t blockCount
;
4085 struct cat_desc jib_desc
;
4086 struct cat_attr jib_attr
;
4087 struct cat_fork jib_fork
;
4088 buf_t old_bp
, new_bp
;
4090 error
= hfs_start_transaction(hfsmp
);
4092 printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error
);
4095 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4097 error
= BlockAllocate(hfsmp
, 1, 1, 1, true, true, &newBlock
, &blockCount
);
4099 printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error
);
4102 if (blockCount
!= 1) {
4103 printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount
);
4106 error
= BlockDeallocate(hfsmp
, hfsmp
->vcbJinfoBlock
, 1);
4108 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error
);
4112 /* Copy the old journal info block content to the new location */
4113 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
4114 hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_phys_block_size
),
4115 hfsmp
->blockSize
, vfs_context_ucred(context
), &old_bp
);
4117 printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error
);
4120 new_bp
= buf_getblk(hfsmp
->hfs_devvp
,
4121 newBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_phys_block_size
),
4122 hfsmp
->blockSize
, 0, 0, BLK_META
);
4123 bcopy((char*)buf_dataptr(old_bp
), (char*)buf_dataptr(new_bp
), hfsmp
->blockSize
);
4125 if (journal_uses_fua(hfsmp
->jnl
))
4126 buf_markfua(new_bp
);
4127 error
= buf_bwrite(new_bp
);
4129 printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error
);
4132 if (!journal_uses_fua(hfsmp
->jnl
)) {
4133 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, context
);
4135 printf("hfs_reclaim_journal_info_block: DKIOCSYNCHRONIZECACHE failed (%d)\n", error
);
4136 /* Don't fail the operation. */
4140 /* Update the catalog record for .journal_info_block */
4141 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlinfoblkid
, 1, &jib_desc
, &jib_attr
, &jib_fork
);
4143 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error
);
4146 jib_fork
.cf_size
= hfsmp
->blockSize
;
4147 jib_fork
.cf_extents
[0].startBlock
= newBlock
;
4148 jib_fork
.cf_extents
[0].blockCount
= 1;
4149 jib_fork
.cf_blocks
= 1;
4150 error
= cat_update(hfsmp
, &jib_desc
, &jib_attr
, &jib_fork
, NULL
);
4151 cat_releasedesc(&jib_desc
); /* all done with cat descriptor */
4153 printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error
);
4157 /* Update the pointer to the journal info block in the volume header. */
4158 hfsmp
->vcbJinfoBlock
= newBlock
;
4159 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
4161 printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error
);
4164 hfs_systemfile_unlock(hfsmp
, lockflags
);
4165 error
= hfs_end_transaction(hfsmp
);
4167 printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error
);
4169 error
= journal_flush(hfsmp
->jnl
);
4171 printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error
);
4176 (void) BlockDeallocate(hfsmp
, newBlock
, blockCount
);
4178 hfs_systemfile_unlock(hfsmp
, lockflags
);
4179 (void) hfs_end_transaction(hfsmp
);
4185 * Reclaim space at the end of a file system.
4188 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
, vfs_context_t context
)
4190 struct vnode
*vp
= NULL
;
4192 struct BTreeIterator
* iterator
= NULL
;
4193 struct FSBufferDescriptor btdata
;
4194 struct HFSPlusCatalogFile filerec
;
4195 u_int32_t saved_next_allocation
;
4203 u_long blkstomove
= 0;
4207 int lastprogress
= 0;
4208 Boolean system_file_moved
= false;
4210 /* Relocate extents of the Allocation file if they're in the way. */
4211 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_allocation_vp
, startblk
, SFL_BITMAP
, &system_file_moved
, context
);
4213 printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error
);
4216 /* Relocate extents of the Extents B-tree if they're in the way. */
4217 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_extents_vp
, startblk
, SFL_EXTENTS
, &system_file_moved
, context
);
4219 printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error
);
4222 /* Relocate extents of the Catalog B-tree if they're in the way. */
4223 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_catalog_vp
, startblk
, SFL_CATALOG
, &system_file_moved
, context
);
4225 printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error
);
4228 /* Relocate extents of the Attributes B-tree if they're in the way. */
4229 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_attribute_vp
, startblk
, SFL_ATTRIBUTE
, &system_file_moved
, context
);
4231 printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error
);
4234 /* Relocate extents of the Startup File if there is one and they're in the way. */
4235 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_startup_vp
, startblk
, SFL_STARTUP
, &system_file_moved
, context
);
4237 printf("hfs_reclaimspace: reclaim startup file returned %d\n", error
);
4242 * We need to make sure the alternate volume header gets flushed if we moved
4243 * any extents in the volume header. But we need to do that before
4244 * shrinking the size of the volume, or else the journal code will panic
4245 * with an invalid (too large) block number.
4247 * Note that system_file_moved will be set if ANY extent was moved, even
4248 * if it was just an overflow extent. In this case, the journal_flush isn't
4249 * strictly required, but shouldn't hurt.
4251 if (system_file_moved
)
4252 journal_flush(hfsmp
->jnl
);
4254 if (hfsmp
->jnl_start
+ (hfsmp
->jnl_size
/ hfsmp
->blockSize
) > startblk
) {
4255 error
= hfs_reclaim_journal_file(hfsmp
, context
);
4257 printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error
);
4262 if (hfsmp
->vcbJinfoBlock
>= startblk
) {
4263 error
= hfs_reclaim_journal_info_block(hfsmp
, context
);
4265 printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error
);
4270 /* For now move a maximum of 250,000 files. */
4271 maxfilecnt
= MIN(hfsmp
->hfs_filecount
, 250000);
4272 maxfilecnt
= MIN((u_long
)maxfilecnt
, reclaimblks
);
4273 cnidbufsize
= maxfilecnt
* sizeof(cnid_t
);
4274 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&cnidbufp
, cnidbufsize
)) {
4277 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
4278 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
4282 saved_next_allocation
= hfsmp
->nextAllocation
;
4283 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, hfsmp
->hfs_metazone_start
);
4285 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
4286 bzero(iterator
, sizeof(*iterator
));
4288 btdata
.bufferAddress
= &filerec
;
4289 btdata
.itemSize
= sizeof(filerec
);
4290 btdata
.itemCount
= 1;
4292 /* Keep the Catalog and extents files locked during iteration. */
4293 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_SHARED_LOCK
);
4295 error
= BTIterateRecord(fcb
, kBTreeFirstRecord
, iterator
, NULL
, NULL
);
4300 * Iterate over all the catalog records looking for files
4301 * that overlap into the space we're trying to free up.
4303 for (filecnt
= 0; filecnt
< maxfilecnt
; ) {
4304 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4306 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
4311 if (filerec
.recordType
!= kHFSPlusFileRecord
) {
4314 datablks
= rsrcblks
= 0;
4316 * Check if either fork overlaps target space.
4318 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
4319 if (filerec
.dataFork
.extents
[i
].blockCount
!= 0) {
4320 datablks
+= filerec
.dataFork
.extents
[i
].blockCount
;
4321 block
= filerec
.dataFork
.extents
[i
].startBlock
+
4322 filerec
.dataFork
.extents
[i
].blockCount
;
4323 if (block
>= startblk
) {
4324 if ((filerec
.fileID
== hfsmp
->hfs_jnlfileid
) ||
4325 (filerec
.fileID
== hfsmp
->hfs_jnlinfoblkid
)) {
4326 printf("hfs_reclaimspace: cannot move active journal\n");
4330 cnidbufp
[filecnt
++] = filerec
.fileID
;
4331 blkstomove
+= filerec
.dataFork
.totalBlocks
;
4335 if (filerec
.resourceFork
.extents
[i
].blockCount
!= 0) {
4336 rsrcblks
+= filerec
.resourceFork
.extents
[i
].blockCount
;
4337 block
= filerec
.resourceFork
.extents
[i
].startBlock
+
4338 filerec
.resourceFork
.extents
[i
].blockCount
;
4339 if (block
>= startblk
) {
4340 cnidbufp
[filecnt
++] = filerec
.fileID
;
4341 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
4347 * Check for any overflow extents that overlap.
4349 if (i
== kHFSPlusExtentDensity
) {
4350 if (filerec
.dataFork
.totalBlocks
> datablks
) {
4351 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, datablks
, filerec
.fileID
, 0)) {
4352 cnidbufp
[filecnt
++] = filerec
.fileID
;
4353 blkstomove
+= filerec
.dataFork
.totalBlocks
;
4355 } else if (filerec
.resourceFork
.totalBlocks
> rsrcblks
) {
4356 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, rsrcblks
, filerec
.fileID
, 1)) {
4357 cnidbufp
[filecnt
++] = filerec
.fileID
;
4358 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
4365 if (filecnt
== 0 && !system_file_moved
) {
4366 printf("hfs_reclaimspace: no files moved\n");
4369 /* All done with catalog. */
4370 hfs_systemfile_unlock(hfsmp
, lockflags
);
4371 if (error
|| filecnt
== 0)
4375 * Double check space requirements to make sure
4376 * there is enough space to relocate any files
4377 * that reside in the reclaim area.
4379 * Blocks To Move --------------
4382 * ------------------------------------------------------------------------
4386 * ------------------------------------------------------------------------
4388 * <------------------- New Total Blocks ------------------><-- Reclaim -->
4390 * <------------------------ Original Total Blocks ----------------------->
4393 if (blkstomove
>= hfs_freeblks(hfsmp
, 1)) {
4394 printf("hfs_truncatefs: insufficient space (need %lu blocks; have %u blocks)\n", blkstomove
, hfs_freeblks(hfsmp
, 1));
4398 hfsmp
->hfs_resize_filesmoved
= 0;
4399 hfsmp
->hfs_resize_totalfiles
= filecnt
;
4401 /* Now move any files that are in the way. */
4402 for (i
= 0; i
< filecnt
; ++i
) {
4405 if (hfs_vget(hfsmp
, cnidbufp
[i
], &vp
, 0) != 0)
4408 /* Relocate any data fork blocks. */
4409 if (VTOF(vp
)->ff_blocks
> 0) {
4410 error
= hfs_relocate(vp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
4415 /* Relocate any resource fork blocks. */
4416 if ((VTOC((vp
))->c_blocks
- VTOF((vp
))->ff_blocks
) > 0) {
4417 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
);
4420 error
= hfs_relocate(rvp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
4425 hfs_unlock(VTOC(vp
));
4429 ++hfsmp
->hfs_resize_filesmoved
;
4431 /* Report intermediate progress. */
4432 if (filecnt
> 100) {
4435 progress
= (i
* 100) / filecnt
;
4436 if (progress
> (lastprogress
+ 9)) {
4437 printf("hfs_reclaimspace: %d%% done...\n", progress
);
4438 lastprogress
= progress
;
4443 hfs_unlock(VTOC(vp
));
4447 if (hfsmp
->hfs_resize_filesmoved
!= 0) {
4448 printf("hfs_reclaimspace: relocated %d files on \"%s\"\n",
4449 (int)hfsmp
->hfs_resize_filesmoved
, hfsmp
->vcbVN
);
4452 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4453 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
4456 * Restore the roving allocation pointer on errors.
4457 * (but only if we didn't move any files)
4459 if (error
&& hfsmp
->hfs_resize_filesmoved
== 0) {
4460 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, saved_next_allocation
);
4467 * Check if there are any overflow extents that overlap.
4470 hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
, u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
)
4472 struct BTreeIterator
* iterator
= NULL
;
4473 struct FSBufferDescriptor btdata
;
4474 HFSPlusExtentRecord extrec
;
4475 HFSPlusExtentKey
*extkeyptr
;
4483 forktype
= rsrcfork
? 0xFF : 0;
4484 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
4487 bzero(iterator
, sizeof(*iterator
));
4488 extkeyptr
= (HFSPlusExtentKey
*)&iterator
->key
;
4489 extkeyptr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
4490 extkeyptr
->forkType
= forktype
;
4491 extkeyptr
->fileID
= fileID
;
4492 extkeyptr
->startBlock
= catblks
;
4494 btdata
.bufferAddress
= &extrec
;
4495 btdata
.itemSize
= sizeof(extrec
);
4496 btdata
.itemCount
= 1;
4498 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
4500 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
4501 while (error
== 0) {
4502 /* Stop when we encounter a different file. */
4503 if ((extkeyptr
->fileID
!= fileID
) ||
4504 (extkeyptr
->forkType
!= forktype
)) {
4508 * Check if the file overlaps target space.
4510 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
4511 if (extrec
[i
].blockCount
== 0) {
4514 block
= extrec
[i
].startBlock
+ extrec
[i
].blockCount
;
4515 if (block
>= startblk
) {
4520 /* Look for more records. */
4521 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4524 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4525 return (overlapped
);
4530 * Calculate the progress of a file system resize operation.
4534 hfs_resize_progress(struct hfsmount
*hfsmp
, u_int32_t
*progress
)
4536 if ((hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) == 0) {
4540 if (hfsmp
->hfs_resize_totalfiles
> 0)
4541 *progress
= (hfsmp
->hfs_resize_filesmoved
* 100) / hfsmp
->hfs_resize_totalfiles
;
4550 * Get file system attributes.
4553 hfs_vfs_getattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
4555 #define HFS_ATTR_CMN_VALIDMASK (ATTR_CMN_VALIDMASK & ~(ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST))
4556 #define HFS_ATTR_FILE_VALIDMASK (ATTR_FILE_VALIDMASK & ~(ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST))
4558 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
4559 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
4562 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)hfsmp
->vcbNxtCNID
;
4564 VFSATTR_RETURN(fsap
, f_objcount
, (u_int64_t
)hfsmp
->vcbFilCnt
+ (u_int64_t
)hfsmp
->vcbDirCnt
);
4565 VFSATTR_RETURN(fsap
, f_filecount
, (u_int64_t
)hfsmp
->vcbFilCnt
);
4566 VFSATTR_RETURN(fsap
, f_dircount
, (u_int64_t
)hfsmp
->vcbDirCnt
);
4567 VFSATTR_RETURN(fsap
, f_maxobjcount
, (u_int64_t
)0xFFFFFFFF);
4568 VFSATTR_RETURN(fsap
, f_iosize
, (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
));
4569 VFSATTR_RETURN(fsap
, f_blocks
, (u_int64_t
)hfsmp
->totalBlocks
);
4570 VFSATTR_RETURN(fsap
, f_bfree
, (u_int64_t
)hfs_freeblks(hfsmp
, 0));
4571 VFSATTR_RETURN(fsap
, f_bavail
, (u_int64_t
)hfs_freeblks(hfsmp
, 1));
4572 VFSATTR_RETURN(fsap
, f_bsize
, (u_int32_t
)vcb
->blockSize
);
4573 /* XXX needs clarification */
4574 VFSATTR_RETURN(fsap
, f_bused
, hfsmp
->totalBlocks
- hfs_freeblks(hfsmp
, 1));
4575 /* Maximum files is constrained by total blocks. */
4576 VFSATTR_RETURN(fsap
, f_files
, (u_int64_t
)(hfsmp
->totalBlocks
- 2));
4577 VFSATTR_RETURN(fsap
, f_ffree
, MIN((u_int64_t
)freeCNIDs
, (u_int64_t
)hfs_freeblks(hfsmp
, 1)));
4579 fsap
->f_fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
4580 fsap
->f_fsid
.val
[1] = vfs_typenum(mp
);
4581 VFSATTR_SET_SUPPORTED(fsap
, f_fsid
);
4583 VFSATTR_RETURN(fsap
, f_signature
, vcb
->vcbSigWord
);
4584 VFSATTR_RETURN(fsap
, f_carbon_fsid
, 0);
4586 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
4587 vol_capabilities_attr_t
*cap
;
4589 cap
= &fsap
->f_capabilities
;
4591 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4592 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
4593 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4594 VOL_CAP_FMT_CASE_PRESERVING
|
4595 VOL_CAP_FMT_FAST_STATFS
|
4596 VOL_CAP_FMT_HIDDEN_FILES
|
4597 VOL_CAP_FMT_PATH_FROM_ID
;
4599 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
4600 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4601 VOL_CAP_FMT_SYMBOLICLINKS
|
4602 VOL_CAP_FMT_HARDLINKS
|
4603 VOL_CAP_FMT_JOURNAL
|
4604 VOL_CAP_FMT_ZERO_RUNS
|
4605 (hfsmp
->jnl
? VOL_CAP_FMT_JOURNAL_ACTIVE
: 0) |
4606 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
? VOL_CAP_FMT_CASE_SENSITIVE
: 0) |
4607 VOL_CAP_FMT_CASE_PRESERVING
|
4608 VOL_CAP_FMT_FAST_STATFS
|
4609 VOL_CAP_FMT_2TB_FILESIZE
|
4610 VOL_CAP_FMT_HIDDEN_FILES
|
4611 VOL_CAP_FMT_PATH_FROM_ID
;
4613 cap
->capabilities
[VOL_CAPABILITIES_INTERFACES
] =
4614 VOL_CAP_INT_SEARCHFS
|
4615 VOL_CAP_INT_ATTRLIST
|
4616 VOL_CAP_INT_NFSEXPORT
|
4617 VOL_CAP_INT_READDIRATTR
|
4618 VOL_CAP_INT_EXCHANGEDATA
|
4619 VOL_CAP_INT_ALLOCATE
|
4620 VOL_CAP_INT_VOL_RENAME
|
4621 VOL_CAP_INT_ADVLOCK
|
4624 VOL_CAP_INT_EXTENDED_ATTR
|
4625 VOL_CAP_INT_NAMEDSTREAMS
;
4627 VOL_CAP_INT_EXTENDED_ATTR
;
4629 cap
->capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
4630 cap
->capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
4632 cap
->valid
[VOL_CAPABILITIES_FORMAT
] =
4633 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4634 VOL_CAP_FMT_SYMBOLICLINKS
|
4635 VOL_CAP_FMT_HARDLINKS
|
4636 VOL_CAP_FMT_JOURNAL
|
4637 VOL_CAP_FMT_JOURNAL_ACTIVE
|
4638 VOL_CAP_FMT_NO_ROOT_TIMES
|
4639 VOL_CAP_FMT_SPARSE_FILES
|
4640 VOL_CAP_FMT_ZERO_RUNS
|
4641 VOL_CAP_FMT_CASE_SENSITIVE
|
4642 VOL_CAP_FMT_CASE_PRESERVING
|
4643 VOL_CAP_FMT_FAST_STATFS
|
4644 VOL_CAP_FMT_2TB_FILESIZE
|
4645 VOL_CAP_FMT_OPENDENYMODES
|
4646 VOL_CAP_FMT_HIDDEN_FILES
|
4647 VOL_CAP_FMT_PATH_FROM_ID
;
4648 cap
->valid
[VOL_CAPABILITIES_INTERFACES
] =
4649 VOL_CAP_INT_SEARCHFS
|
4650 VOL_CAP_INT_ATTRLIST
|
4651 VOL_CAP_INT_NFSEXPORT
|
4652 VOL_CAP_INT_READDIRATTR
|
4653 VOL_CAP_INT_EXCHANGEDATA
|
4654 VOL_CAP_INT_COPYFILE
|
4655 VOL_CAP_INT_ALLOCATE
|
4656 VOL_CAP_INT_VOL_RENAME
|
4657 VOL_CAP_INT_ADVLOCK
|
4659 VOL_CAP_INT_MANLOCK
|
4661 VOL_CAP_INT_EXTENDED_ATTR
|
4662 VOL_CAP_INT_NAMEDSTREAMS
;
4664 VOL_CAP_INT_EXTENDED_ATTR
;
4666 cap
->valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
4667 cap
->valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
4668 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
4670 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
4671 vol_attributes_attr_t
*attrp
= &fsap
->f_attributes
;
4673 attrp
->validattr
.commonattr
= HFS_ATTR_CMN_VALIDMASK
;
4674 attrp
->validattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
4675 attrp
->validattr
.dirattr
= ATTR_DIR_VALIDMASK
;
4676 attrp
->validattr
.fileattr
= HFS_ATTR_FILE_VALIDMASK
;
4677 attrp
->validattr
.forkattr
= 0;
4679 attrp
->nativeattr
.commonattr
= HFS_ATTR_CMN_VALIDMASK
;
4680 attrp
->nativeattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
4681 attrp
->nativeattr
.dirattr
= ATTR_DIR_VALIDMASK
;
4682 attrp
->nativeattr
.fileattr
= HFS_ATTR_FILE_VALIDMASK
;
4683 attrp
->nativeattr
.forkattr
= 0;
4684 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
4686 fsap
->f_create_time
.tv_sec
= hfsmp
->vcbCrDate
;
4687 fsap
->f_create_time
.tv_nsec
= 0;
4688 VFSATTR_SET_SUPPORTED(fsap
, f_create_time
);
4689 fsap
->f_modify_time
.tv_sec
= hfsmp
->vcbLsMod
;
4690 fsap
->f_modify_time
.tv_nsec
= 0;
4691 VFSATTR_SET_SUPPORTED(fsap
, f_modify_time
);
4693 fsap
->f_backup_time
.tv_sec
= hfsmp
->vcbVolBkUp
;
4694 fsap
->f_backup_time
.tv_nsec
= 0;
4695 VFSATTR_SET_SUPPORTED(fsap
, f_backup_time
);
4696 if (VFSATTR_IS_ACTIVE(fsap
, f_fssubtype
)) {
4697 u_int16_t subtype
= 0;
4700 * Subtypes (flavors) for HFS
4701 * 0: Mac OS Extended
4702 * 1: Mac OS Extended (Journaled)
4703 * 2: Mac OS Extended (Case Sensitive)
4704 * 3: Mac OS Extended (Case Sensitive, Journaled)
4706 * 128: Mac OS Standard
4709 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4710 subtype
= HFS_SUBTYPE_STANDARDHFS
;
4711 } else /* HFS Plus */ {
4713 subtype
|= HFS_SUBTYPE_JOURNALED
;
4714 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
4715 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
4717 fsap
->f_fssubtype
= subtype
;
4718 VFSATTR_SET_SUPPORTED(fsap
, f_fssubtype
);
4721 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
4722 strlcpy(fsap
->f_vol_name
, (char *) hfsmp
->vcbVN
, MAXPATHLEN
);
4723 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
4729 * Perform a volume rename. Requires the FS' root vp.
4732 hfs_rename_volume(struct vnode
*vp
, const char *name
, proc_t p
)
4734 ExtendedVCB
*vcb
= VTOVCB(vp
);
4735 struct cnode
*cp
= VTOC(vp
);
4736 struct hfsmount
*hfsmp
= VTOHFS(vp
);
4737 struct cat_desc to_desc
;
4738 struct cat_desc todir_desc
;
4739 struct cat_desc new_desc
;
4740 cat_cookie_t cookie
;
4745 * Ignore attempts to rename a volume to a zero-length name.
4750 bzero(&to_desc
, sizeof(to_desc
));
4751 bzero(&todir_desc
, sizeof(todir_desc
));
4752 bzero(&new_desc
, sizeof(new_desc
));
4753 bzero(&cookie
, sizeof(cookie
));
4755 todir_desc
.cd_parentcnid
= kHFSRootParentID
;
4756 todir_desc
.cd_cnid
= kHFSRootFolderID
;
4757 todir_desc
.cd_flags
= CD_ISDIR
;
4759 to_desc
.cd_nameptr
= (const u_int8_t
*)name
;
4760 to_desc
.cd_namelen
= strlen(name
);
4761 to_desc
.cd_parentcnid
= kHFSRootParentID
;
4762 to_desc
.cd_cnid
= cp
->c_cnid
;
4763 to_desc
.cd_flags
= CD_ISDIR
;
4765 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)) == 0) {
4766 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
4767 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, &cookie
, p
)) == 0) {
4768 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4770 error
= cat_rename(hfsmp
, &cp
->c_desc
, &todir_desc
, &to_desc
, &new_desc
);
4773 * If successful, update the name in the VCB, ensure it's terminated.
4776 strlcpy((char *)vcb
->vcbVN
, name
, sizeof(vcb
->vcbVN
));
4779 hfs_systemfile_unlock(hfsmp
, lockflags
);
4780 cat_postflight(hfsmp
, &cookie
, p
);
4784 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
4786 hfs_end_transaction(hfsmp
);
4789 /* Release old allocated name buffer */
4790 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
4791 const char *tmp_name
= (const char *)cp
->c_desc
.cd_nameptr
;
4793 cp
->c_desc
.cd_nameptr
= 0;
4794 cp
->c_desc
.cd_namelen
= 0;
4795 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
4796 vfs_removename(tmp_name
);
4798 /* Update cnode's catalog descriptor */
4799 replace_desc(cp
, &new_desc
);
4800 vcb
->volumeNameEncodingHint
= new_desc
.cd_encoding
;
4801 cp
->c_touch_chgtime
= TRUE
;
4811 * Get file system attributes.
4814 hfs_vfs_setattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
4816 kauth_cred_t cred
= vfs_context_ucred(context
);
4820 * Must be superuser or owner of filesystem to change volume attributes
4822 if (!kauth_cred_issuser(cred
) && (kauth_cred_getuid(cred
) != vfs_statfs(mp
)->f_owner
))
4825 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
4828 error
= hfs_vfs_root(mp
, &root_vp
, context
);
4832 error
= hfs_rename_volume(root_vp
, fsap
->f_vol_name
, vfs_context_proc(context
));
4833 (void) vnode_put(root_vp
);
4837 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
4844 /* If a runtime corruption is detected, set the volume inconsistent
4845 * bit in the volume attributes. The volume inconsistent bit is a persistent
4846 * bit which represents that the volume is corrupt and needs repair.
4847 * The volume inconsistent bit can be set from the kernel when it detects
4848 * runtime corruption or from file system repair utilities like fsck_hfs when
4849 * a repair operation fails. The bit should be cleared only from file system
4850 * verify/repair utility like fsck_hfs when a verify/repair succeeds.
4852 void hfs_mark_volume_inconsistent(struct hfsmount
*hfsmp
)
4854 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
4855 if ((hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
) == 0) {
4856 hfsmp
->vcbAtrb
|= kHFSVolumeInconsistentMask
;
4857 MarkVCBDirty(hfsmp
);
4859 /* Log information to ASL log */
4860 fslog_fs_corrupt(hfsmp
->hfs_mp
);
4861 printf("HFS: Runtime corruption detected on %s, fsck will be forced on next mount.\n", hfsmp
->vcbVN
);
4862 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
4865 /* Replay the journal on the device node provided. Returns zero if
4866 * journal replay succeeded or no journal was supposed to be replayed.
4868 static int hfs_journal_replay(const char *devnode
, vfs_context_t context
)
4871 struct vnode
*devvp
= NULL
;
4872 struct mount
*mp
= NULL
;
4873 struct hfs_mount_args
*args
= NULL
;
4875 /* Lookup vnode for given raw device path */
4876 retval
= vnode_open(devnode
, FREAD
|FWRITE
, 0, 0, &devvp
, NULL
);
4881 /* Replay allowed only on raw devices */
4882 if (!vnode_ischr(devvp
)) {
4887 /* Create dummy mount structures */
4888 MALLOC(mp
, struct mount
*, sizeof(struct mount
), M_TEMP
, M_WAITOK
);
4889 bzero(mp
, sizeof(struct mount
));
4890 mount_lock_init(mp
);
4892 MALLOC(args
, struct hfs_mount_args
*, sizeof(struct hfs_mount_args
), M_TEMP
, M_WAITOK
);
4893 bzero(args
, sizeof(struct hfs_mount_args
));
4895 retval
= hfs_mountfs(devvp
, mp
, args
, 1, context
);
4896 buf_flushdirtyblks(devvp
, MNT_WAIT
, 0, "hfs_journal_replay");
4900 mount_lock_destroy(mp
);
4907 vnode_close(devvp
, FREAD
|FWRITE
, NULL
);
4913 * hfs vfs operations.
4915 struct vfsops hfs_vfsops
= {
4921 hfs_vfs_getattr
, /* was hfs_statfs */