2 * Copyright (c) 1999-2005 Apple Computer, 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>
89 #include <kern/locks.h>
91 #include <vfs/vfs_journal.h>
93 #include <miscfs/specfs/specdev.h>
94 #include <hfs/hfs_mount.h>
97 #include "hfs_catalog.h"
98 #include "hfs_cnode.h"
100 #include "hfs_endian.h"
101 #include "hfs_hotfiles.h"
102 #include "hfs_quota.h"
104 #include "hfscommon/headers/FileMgrInternal.h"
105 #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
;
120 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
122 extern void hfs_converterinit(void);
124 extern void inittodr(time_t base
);
126 extern int hfs_write_access(struct vnode
*, kauth_cred_t
, struct proc
*, Boolean
);
129 static int hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
);
130 static int hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
);
131 static int hfs_flushfiles(struct mount
*, int, struct proc
*);
132 static int hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
133 static int hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
);
134 static int hfs_init(struct vfsconf
*vfsp
);
135 static int hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
);
136 static int hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
, vfs_context_t context
);
137 static int hfs_reload(struct mount
*mp
, kauth_cred_t cred
, struct proc
*p
);
138 static int hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, vfs_context_t context
);
139 static int hfs_quotactl(struct mount
*, int, uid_t
, caddr_t
, vfs_context_t context
);
140 static int hfs_start(struct mount
*mp
, int flags
, vfs_context_t context
);
141 static int hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, vfs_context_t context
);
142 static int hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
);
143 static int hfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
144 user_addr_t newp
, size_t newlen
, vfs_context_t context
);
145 static int hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
);
146 static int hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, vfs_context_t context
);
147 static int hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
149 static int hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
);
150 static int hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
,
151 u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
);
155 * Called by vfs_mountroot when mounting HFS Plus as root.
160 hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
)
162 struct hfsmount
*hfsmp
;
164 struct vfsstatfs
*vfsp
;
167 if ((error
= hfs_mountfs(rvp
, mp
, NULL
, context
)))
171 hfsmp
= VFSTOHFS(mp
);
173 hfsmp
->hfs_uid
= UNKNOWNUID
;
174 hfsmp
->hfs_gid
= UNKNOWNGID
;
175 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
176 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
178 /* Establish the free block reserve. */
179 vcb
= HFSTOVCB(hfsmp
);
180 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
181 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
183 vfsp
= vfs_statfs(mp
);
184 (void)hfs_statfs(mp
, vfsp
, NULL
);
197 hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
199 struct proc
*p
= vfs_context_proc(context
);
200 struct hfsmount
*hfsmp
= NULL
;
201 struct hfs_mount_args args
;
205 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
)))) {
208 cmdflags
= (uint32_t)vfs_flags(mp
) & MNT_CMDFLAGS
;
209 if (cmdflags
& MNT_UPDATE
) {
210 hfsmp
= VFSTOHFS(mp
);
212 /* Reload incore data after an fsck. */
213 if (cmdflags
& MNT_RELOAD
) {
214 if (vfs_isrdonly(mp
))
215 return hfs_reload(mp
, vfs_context_ucred(context
), p
);
220 /* Change to a read-only file system. */
221 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
225 /* use VFS_SYNC to push out System (btree) files */
226 retval
= VFS_SYNC(mp
, MNT_WAIT
, context
);
227 if (retval
&& ((cmdflags
& MNT_FORCE
) == 0))
231 if (cmdflags
& MNT_FORCE
)
234 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
236 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
237 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
239 /* also get the volume bitmap blocks */
241 if (vnode_mount(hfsmp
->hfs_devvp
) == mp
) {
242 retval
= hfs_fsync(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, p
);
244 vnode_get(hfsmp
->hfs_devvp
);
245 retval
= VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
246 vnode_put(hfsmp
->hfs_devvp
);
250 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
254 hfs_global_exclusive_lock_acquire(hfsmp
);
256 journal_close(hfsmp
->jnl
);
259 // Note: we explicitly don't want to shutdown
260 // access to the jvp because we may need
261 // it later if we go back to being read-write.
263 hfs_global_exclusive_lock_release(hfsmp
);
267 /* Change to a writable file system. */
268 if (vfs_iswriteupgrade(mp
)) {
269 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
270 if (retval
!= E_NONE
)
273 // If the journal was shut-down previously because we were
274 // asked to be read-only, let's start it back up again now
276 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
277 && hfsmp
->jnl
== NULL
278 && hfsmp
->jvp
!= NULL
) {
281 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
282 jflags
= JOURNAL_RESET
;
287 hfs_global_exclusive_lock_acquire(hfsmp
);
289 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
290 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
293 hfsmp
->hfs_phys_block_size
,
296 hfs_sync_metadata
, hfsmp
->hfs_mp
);
298 hfs_global_exclusive_lock_release(hfsmp
);
300 if (hfsmp
->jnl
== NULL
) {
304 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
309 /* Only clear HFS_READ_ONLY after a successfull write */
310 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
312 if (!(hfsmp
->hfs_flags
& (HFS_READ_ONLY
& HFS_STANDARD
))) {
313 /* setup private/hidden directory for unlinked files */
314 FindMetaDataDirectory(HFSTOVCB(hfsmp
));
315 hfs_remove_orphans(hfsmp
);
318 * Allow hot file clustering if conditions allow.
320 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
321 (void) hfs_recording_init(hfsmp
);
326 /* Update file system parameters. */
327 retval
= hfs_changefs(mp
, &args
);
329 } else /* not an update request */ {
331 /* Set the mount flag to indicate that we support volfs */
332 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_DOVOLFS
));
334 retval
= hfs_mountfs(devvp
, mp
, &args
, context
);
338 (void)hfs_statfs(mp
, vfs_statfs(mp
), context
);
344 struct hfs_changefs_cargs
{
345 struct hfsmount
*hfsmp
;
352 hfs_changefs_callback(struct vnode
*vp
, void *cargs
)
356 struct cat_desc cndesc
;
357 struct cat_attr cnattr
;
358 struct hfs_changefs_cargs
*args
;
360 args
= (struct hfs_changefs_cargs
*)cargs
;
363 vcb
= HFSTOVCB(args
->hfsmp
);
365 if (cat_lookup(args
->hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
, NULL
)) {
367 * If we couldn't find this guy skip to the next one
372 return (VNODE_RETURNED
);
375 * Get the real uid/gid and perm mask from disk.
377 if (args
->permswitch
|| args
->permfix
) {
378 cp
->c_uid
= cnattr
.ca_uid
;
379 cp
->c_gid
= cnattr
.ca_gid
;
380 cp
->c_mode
= cnattr
.ca_mode
;
383 * If we're switching name converters then...
384 * Remove the existing entry from the namei cache.
385 * Update name to one based on new encoder.
389 replace_desc(cp
, &cndesc
);
391 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
392 strncpy(vcb
->vcbVN
, cp
->c_desc
.cd_nameptr
, NAME_MAX
);
393 cp
->c_desc
.cd_encoding
= args
->hfsmp
->hfs_encoding
;
396 cat_releasedesc(&cndesc
);
398 return (VNODE_RETURNED
);
401 /* Change fs mount parameters */
403 hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
)
406 int namefix
, permfix
, permswitch
;
407 struct hfsmount
*hfsmp
;
409 hfs_to_unicode_func_t get_unicode_func
;
410 unicode_to_hfs_func_t get_hfsname_func
;
411 u_long old_encoding
= 0;
412 struct hfs_changefs_cargs cargs
;
413 uint32_t mount_flags
;
415 hfsmp
= VFSTOHFS(mp
);
416 vcb
= HFSTOVCB(hfsmp
);
417 mount_flags
= (unsigned int)vfs_flags(mp
);
419 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
420 ((mount_flags
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
421 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
422 (mount_flags
& MNT_UNKNOWNPERMISSIONS
)));
424 /* The root filesystem must operate with actual permissions: */
425 if (permswitch
&& (mount_flags
& MNT_ROOTFS
) && (mount_flags
& MNT_UNKNOWNPERMISSIONS
)) {
426 vfs_clearflags(mp
, (uint64_t)((unsigned int)MNT_UNKNOWNPERMISSIONS
)); /* Just say "No". */
429 if (mount_flags
& MNT_UNKNOWNPERMISSIONS
)
430 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
432 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
434 namefix
= permfix
= 0;
436 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
437 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
438 gTimeZone
= args
->hfs_timezone
;
441 /* Change the default uid, gid and/or mask */
442 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
443 hfsmp
->hfs_uid
= args
->hfs_uid
;
444 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
447 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
448 hfsmp
->hfs_gid
= args
->hfs_gid
;
449 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
452 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
453 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
454 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
455 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
456 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
457 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
458 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
463 /* Change the hfs encoding value (hfs only) */
464 if ((vcb
->vcbSigWord
== kHFSSigWord
) &&
465 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
466 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
468 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
473 * Connect the new hfs_get_unicode converter but leave
474 * the old hfs_get_hfsname converter in place so that
475 * we can lookup existing vnodes to get their correctly
478 * When we're all finished, we can then connect the new
479 * hfs_get_hfsname converter and release our interest
480 * in the old converters.
482 hfsmp
->hfs_get_unicode
= get_unicode_func
;
483 old_encoding
= hfsmp
->hfs_encoding
;
484 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
488 if (!(namefix
|| permfix
|| permswitch
))
491 /* XXX 3762912 hack to support HFS filesystem 'owner' */
494 hfsmp
->hfs_uid
== UNKNOWNUID
? KAUTH_UID_NONE
: hfsmp
->hfs_uid
,
495 hfsmp
->hfs_gid
== UNKNOWNGID
? KAUTH_GID_NONE
: hfsmp
->hfs_gid
);
498 * For each active vnode fix things that changed
500 * Note that we can visit a vnode more than once
501 * and we can race with fsync.
503 * hfs_changefs_callback will be called for each vnode
504 * hung off of this mount point
506 * properly referenced and unreferenced around the callback
509 cargs
.namefix
= namefix
;
510 cargs
.permfix
= permfix
;
511 cargs
.permswitch
= permswitch
;
513 vnode_iterate(mp
, 0, hfs_changefs_callback
, (void *)&cargs
);
516 * If we're switching name converters we can now
517 * connect the new hfs_get_hfsname converter and
518 * release our interest in the old converters.
521 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
522 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
523 (void) hfs_relconverter(old_encoding
);
530 struct hfs_reload_cargs
{
531 struct hfsmount
*hfsmp
;
538 hfs_reload_callback(struct vnode
*vp
, void *cargs
)
541 struct hfs_reload_cargs
*args
;
543 args
= (struct hfs_reload_cargs
*)cargs
;
545 * flush all the buffers associated with this node
547 (void) buf_invalidateblks(vp
, 0, 0, 0);
551 * Remove any directory hints
554 hfs_reldirhints(cp
, 0);
557 * Re-read cnode data for all active vnodes (non-metadata files).
559 if (!vnode_issystem(vp
) && !VNODE_IS_RSRC(vp
)) {
560 struct cat_fork
*datafork
;
561 struct cat_desc desc
;
563 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
565 /* lookup by fileID since name could have changed */
566 if ((args
->error
= cat_idlookup(args
->hfsmp
, cp
->c_fileid
, &desc
, &cp
->c_attr
, datafork
)))
567 return (VNODE_RETURNED_DONE
);
569 /* update cnode's catalog descriptor */
570 (void) replace_desc(cp
, &desc
);
572 return (VNODE_RETURNED
);
576 * Reload all incore data for a filesystem (used after running fsck on
577 * the root filesystem and finding things to fix). The filesystem must
578 * be mounted read-only.
580 * Things to do to update the mount:
581 * invalidate all cached meta-data.
582 * invalidate all inactive vnodes.
583 * invalidate all cached file data.
584 * re-read volume header from disk.
585 * re-load meta-file info (extents, file size).
586 * re-load B-tree header data.
587 * re-read cnode data for all active vnodes.
590 hfs_reload(struct mount
*mountp
, kauth_cred_t cred
, struct proc
*p
)
592 register struct vnode
*devvp
;
596 struct hfsmount
*hfsmp
;
597 struct HFSPlusVolumeHeader
*vhp
;
599 struct filefork
*forkp
;
600 struct cat_desc cndesc
;
601 struct hfs_reload_cargs args
;
603 hfsmp
= VFSTOHFS(mountp
);
604 vcb
= HFSTOVCB(hfsmp
);
606 if (vcb
->vcbSigWord
== kHFSSigWord
)
607 return (EINVAL
); /* rooting from HFS is not supported! */
610 * Invalidate all cached meta-data.
612 devvp
= hfsmp
->hfs_devvp
;
613 if (buf_invalidateblks(devvp
, 0, 0, 0))
614 panic("hfs_reload: dirty1");
621 * hfs_reload_callback will be called for each vnode
622 * hung off of this mount point that can't be recycled...
623 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
624 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
625 * properly referenced and unreferenced around the callback
627 vnode_iterate(mountp
, VNODE_RELOAD
| VNODE_WAIT
, hfs_reload_callback
, (void *)&args
);
633 * Re-read VolumeHeader from disk.
635 sectorsize
= hfsmp
->hfs_phys_block_size
;
637 error
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
638 (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
)),
639 sectorsize
, NOCRED
, &bp
);
646 vhp
= (HFSPlusVolumeHeader
*) (buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
648 /* Do a quick sanity check */
649 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
650 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
651 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
652 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
653 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
658 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
659 vcb
->vcbAtrb
= SWAP_BE32 (vhp
->attributes
);
660 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
661 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
662 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
663 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
664 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
665 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
666 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
667 vcb
->nextAllocation
= SWAP_BE32 (vhp
->nextAllocation
);
668 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
669 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
670 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
671 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
672 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
675 * Re-load meta-file vnode data (extent info, file size, etc).
677 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
678 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
679 forkp
->ff_extents
[i
].startBlock
=
680 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
681 forkp
->ff_extents
[i
].blockCount
=
682 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
684 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
685 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
686 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
689 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
690 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
691 forkp
->ff_extents
[i
].startBlock
=
692 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
693 forkp
->ff_extents
[i
].blockCount
=
694 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
696 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
697 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
698 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
700 if (hfsmp
->hfs_attribute_vp
) {
701 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
702 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
703 forkp
->ff_extents
[i
].startBlock
=
704 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
705 forkp
->ff_extents
[i
].blockCount
=
706 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
708 forkp
->ff_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
709 forkp
->ff_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
710 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
713 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
714 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
715 forkp
->ff_extents
[i
].startBlock
=
716 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
717 forkp
->ff_extents
[i
].blockCount
=
718 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
720 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
721 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
722 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
728 * Re-load B-tree header data
730 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
731 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
734 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
735 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
738 if (hfsmp
->hfs_attribute_vp
) {
739 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
740 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
744 /* Reload the volume name */
745 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, NULL
, NULL
)))
747 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
748 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
749 cat_releasedesc(&cndesc
);
751 /* Re-establish private/hidden directory for unlinked files */
752 FindMetaDataDirectory(vcb
);
754 /* In case any volume information changed to trigger a notification */
755 hfs_generate_volume_notifications(hfsmp
);
762 * Common code for mount and mountroot
765 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
,
766 vfs_context_t context
)
768 struct proc
*p
= vfs_context_proc(context
);
770 struct hfsmount
*hfsmp
;
773 HFSMasterDirectoryBlock
*mdbp
;
781 u_int32_t minblksize
;
782 u_int32_t iswritable
;
783 daddr64_t mdb_offset
;
785 ronly
= vfs_isrdonly(mp
);
786 dev
= vnode_specrdev(devvp
);
787 cred
= p
? vfs_context_ucred(context
) : NOCRED
;
793 minblksize
= kHFSBlockSize
;
795 /* Advisory locking should be handled at the VFS layer */
796 vfs_setlocklocal(mp
);
798 /* Get the real physical block size. */
799 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, context
)) {
803 /* Switch to 512 byte sectors (temporarily) */
805 u_int32_t size512
= 512;
807 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, context
)) {
812 /* Get the number of 512 byte physical blocks. */
813 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
817 /* Compute an accurate disk size (i.e. within 512 bytes) */
818 disksize
= (u_int64_t
)blkcnt
* (u_int64_t
)512;
821 * On Tiger it is not necessary to switch the device
822 * block size to be 4k if there are more than 31-bits
823 * worth of blocks but to insure compatibility with
824 * pre-Tiger systems we have to do it.
826 if (blkcnt
> (u_int64_t
)0x000000007fffffff) {
827 minblksize
= blksize
= 4096;
830 /* Now switch to our prefered physical block size. */
832 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
836 /* Get the count of physical blocks. */
837 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
844 * minblksize is the minimum physical block size
845 * blksize has our prefered physical block size
846 * blkcnt has the total number of physical blocks
849 mdb_offset
= (daddr64_t
)HFS_PRI_SECTOR(blksize
);
850 if ((retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
))) {
853 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
854 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
858 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
859 bzero(hfsmp
, sizeof(struct hfsmount
));
862 * Init the volume information structure
865 lck_mtx_init(&hfsmp
->hfs_mutex
, hfs_mutex_group
, hfs_lock_attr
);
866 lck_mtx_init(&hfsmp
->hfc_mutex
, hfs_mutex_group
, hfs_lock_attr
);
867 lck_rw_init(&hfsmp
->hfs_global_lock
, hfs_rwlock_group
, hfs_lock_attr
);
868 lck_rw_init(&hfsmp
->hfs_insync
, hfs_rwlock_group
, hfs_lock_attr
);
870 vfs_setfsprivate(mp
, hfsmp
);
871 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
872 hfsmp
->hfs_raw_dev
= vnode_specrdev(devvp
);
873 hfsmp
->hfs_devvp
= devvp
;
874 hfsmp
->hfs_phys_block_size
= blksize
;
875 hfsmp
->hfs_phys_block_count
= blkcnt
;
876 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
878 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
879 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)
880 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
881 for (i
= 0; i
< MAXQUOTAS
; i
++)
882 dqfileinit(&hfsmp
->hfs_qfiles
[i
]);
885 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
886 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
887 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
888 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
889 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
890 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
891 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
892 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
893 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
895 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
898 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
899 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
901 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
904 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
905 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
) {
906 hfsmp
->hfs_uid
= UNKNOWNUID
;
907 hfsmp
->hfs_gid
= UNKNOWNGID
;
908 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
909 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
910 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
914 /* Find out if disk media is writable. */
915 if (VNOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, context
) == 0) {
917 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
919 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
922 // record the current time at which we're mounting this volume
926 hfsmp
->hfs_mount_time
= tv
.tv_sec
;
929 /* Mount a standard HFS disk */
930 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
931 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
932 if ((vfs_flags(mp
) & MNT_ROOTFS
)) {
933 retval
= EINVAL
; /* Cannot root from HFS standard disks */
936 /* HFS disks can only use 512 byte physical blocks */
937 if (blksize
> kHFSBlockSize
) {
938 blksize
= kHFSBlockSize
;
939 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
943 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
947 hfsmp
->hfs_phys_block_size
= blksize
;
948 hfsmp
->hfs_phys_block_count
= blkcnt
;
951 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
952 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
954 /* establish the timezone */
955 gTimeZone
= args
->hfs_timezone
;
958 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
959 &hfsmp
->hfs_get_hfsname
);
963 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
965 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
967 } else /* Mount an HFS Plus disk */ {
968 HFSPlusVolumeHeader
*vhp
;
969 off_t embeddedOffset
;
972 /* Get the embedded Volume Header */
973 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
974 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
975 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
976 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
979 * If the embedded volume doesn't start on a block
980 * boundary, then switch the device to a 512-byte
981 * block size so everything will line up on a block
984 if ((embeddedOffset
% blksize
) != 0) {
985 printf("HFS Mount: embedded volume offset not"
986 " a multiple of physical block size (%d);"
987 " switching to 512\n", blksize
);
989 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
990 (caddr_t
)&blksize
, FWRITE
, context
)) {
994 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
995 (caddr_t
)&blkcnt
, 0, context
)) {
999 /* Note: relative block count adjustment */
1000 hfsmp
->hfs_phys_block_count
*=
1001 hfsmp
->hfs_phys_block_size
/ blksize
;
1002 hfsmp
->hfs_phys_block_size
= blksize
;
1005 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1006 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1008 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1010 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1011 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1014 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1017 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1019 } else /* pure HFS+ */ {
1021 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1028 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) && args
->journal_disable
) {
1033 // We only initialize the journal here if the last person
1034 // to mount this volume was journaling aware. Otherwise
1035 // we delay journal initialization until later at the end
1036 // of hfs_MountHFSPlusVolume() because the last person who
1037 // mounted it could have messed things up behind our back
1038 // (so we need to go find the .journal file, make sure it's
1039 // the right size, re-sync up if it was moved, etc).
1041 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1042 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1045 // if we're able to init the journal, mark the mount
1046 // point as journaled.
1048 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1049 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
1051 // if the journal failed to open, then set the lastMountedVersion
1052 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1053 // of just bailing out because the volume is journaled.
1055 HFSPlusVolumeHeader
*jvhp
;
1057 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1059 if (mdb_offset
== 0) {
1060 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1064 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1066 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1068 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1069 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1070 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1078 // clear this so the error exit path won't try to use it
1083 // if this isn't the root device just bail out.
1084 // If it is the root device we just continue on
1085 // in the hopes that fsck_hfs will be able to
1086 // fix any damage that exists on the volume.
1087 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1095 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1097 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1099 * If the backend didn't like our physical blocksize
1100 * then retry with physical blocksize of 512.
1102 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1103 printf("HFS Mount: could not use physical block size "
1104 "(%d) switching to 512\n", blksize
);
1106 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
1110 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
1114 devvp
->v_specsize
= blksize
;
1115 /* Note: relative block count adjustment (in case this is an embedded volume). */
1116 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1117 hfsmp
->hfs_phys_block_size
= blksize
;
1120 // close and re-open this with the new block size
1121 journal_close(hfsmp
->jnl
);
1123 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1124 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
1126 // if the journal failed to open, then set the lastMountedVersion
1127 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1128 // of just bailing out because the volume is journaled.
1130 HFSPlusVolumeHeader
*jvhp
;
1132 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1134 if (mdb_offset
== 0) {
1135 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1139 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1141 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1143 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1144 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1145 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1153 // clear this so the error exit path won't try to use it
1158 // if this isn't the root device just bail out.
1159 // If it is the root device we just continue on
1160 // in the hopes that fsck_hfs will be able to
1161 // fix any damage that exists on the volume.
1162 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1169 /* Try again with a smaller block size... */
1170 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1173 (void) hfs_relconverter(0);
1176 // save off a snapshot of the mtime from the previous mount
1178 hfsmp
->hfs_last_mounted_mtime
= hfsmp
->hfs_mtime
;
1184 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (long)dev
;
1185 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
1186 vfs_setmaxsymlen(mp
, 0);
1187 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
1188 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSNATIVEXATTR
;
1192 * Set the free space warning levels for a non-root volume:
1194 * Set the lower freespace limit (the level that will trigger a warning)
1195 * to 5% of the volume size or 250MB, whichever is less, and the desired
1196 * level (which will cancel the alert request) to 1/2 above that limit.
1197 * Start looking for free space to drop below this level and generate a
1198 * warning immediately if needed:
1200 hfsmp
->hfs_freespace_notify_warninglimit
=
1201 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1202 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1203 hfsmp
->hfs_freespace_notify_desiredlevel
=
1204 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1205 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1208 * Set the free space warning levels for the root volume:
1210 * Set the lower freespace limit (the level that will trigger a warning)
1211 * to 1% of the volume size or 50MB, whichever is less, and the desired
1212 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1214 hfsmp
->hfs_freespace_notify_warninglimit
=
1215 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1216 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1217 hfsmp
->hfs_freespace_notify_desiredlevel
=
1218 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1219 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1223 * Start looking for free space to drop below this level and generate a
1224 * warning immediately if needed:
1226 hfsmp
->hfs_notification_conditions
= 0;
1227 hfs_generate_volume_notifications(hfsmp
);
1230 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1241 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1242 (void)VNOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, context
);
1246 FREE(hfsmp
, M_HFSMNT
);
1247 vfs_setfsprivate(mp
, NULL
);
1254 * Make a filesystem operational.
1255 * Nothing to do at the moment.
1259 hfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
1266 * unmount system call
1269 hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
)
1271 struct proc
*p
= vfs_context_proc(context
);
1272 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1273 int retval
= E_NONE
;
1280 if (mntflags
& MNT_FORCE
) {
1281 flags
|= FORCECLOSE
;
1285 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1288 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1289 (void) hfs_recording_suspend(hfsmp
);
1292 * Flush out the b-trees, volume bitmap and Volume Header
1294 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1295 hfs_start_transaction(hfsmp
);
1298 if (hfsmp
->hfs_attribute_vp
) {
1299 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), HFS_EXCLUSIVE_LOCK
);
1300 retval
= hfs_fsync(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, p
);
1301 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
1302 if (retval
&& !force
)
1306 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), HFS_EXCLUSIVE_LOCK
);
1307 retval
= hfs_fsync(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, p
);
1308 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
1309 if (retval
&& !force
)
1312 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
1313 retval
= hfs_fsync(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, p
);
1314 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
1315 if (retval
&& !force
)
1318 if (hfsmp
->hfs_allocation_vp
) {
1319 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
1320 retval
= hfs_fsync(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, p
);
1321 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
1322 if (retval
&& !force
)
1326 if (hfsmp
->hfc_filevp
&& vnode_issystem(hfsmp
->hfc_filevp
)) {
1327 retval
= hfs_fsync(hfsmp
->hfc_filevp
, MNT_WAIT
, 0, p
);
1328 if (retval
&& !force
)
1332 /* See if this volume is damaged, is so do not unmount cleanly */
1333 if (HFSTOVCB(hfsmp
)->vcbFlags
& kHFS_DamagedVolume
) {
1334 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1336 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1339 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1341 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1343 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1345 goto err_exit
; /* could not flush everything */
1348 hfs_end_transaction(hfsmp
);
1353 journal_flush(hfsmp
->jnl
);
1357 * Invalidate our caches and release metadata vnodes
1359 (void) hfsUnmount(hfsmp
, p
);
1362 * Last chance to dump unreferenced system files.
1364 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1366 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1367 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1371 journal_close(hfsmp
->jnl
);
1375 VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
1377 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1378 retval
= VNOP_CLOSE(hfsmp
->jvp
,
1379 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1381 vnode_put(hfsmp
->jvp
);
1386 #ifdef HFS_SPARSE_DEV
1387 /* Drop our reference on the backing fs (if any). */
1388 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1389 struct vnode
* tmpvp
;
1391 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1392 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1393 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1396 #endif /* HFS_SPARSE_DEV */
1397 lck_mtx_destroy(&hfsmp
->hfc_mutex
, hfs_mutex_group
);
1398 FREE(hfsmp
, M_HFSMNT
);
1404 hfs_end_transaction(hfsmp
);
1411 * Return the root of a filesystem.
1414 hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1416 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)kHFSRootFolderID
, vpp
, 1);
1421 * Do operations associated with quotas
1424 hfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t context
)
1426 struct proc
*p
= vfs_context_proc(context
);
1427 int cmd
, type
, error
;
1433 uid
= vfs_context_ucred(context
)->cr_ruid
;
1434 cmd
= cmds
>> SUBCMDSHIFT
;
1441 if (uid
== vfs_context_ucred(context
)->cr_ruid
)
1445 if ( (error
= vfs_context_suser(context
)) )
1449 type
= cmds
& SUBCMDMASK
;
1450 if ((u_int
)type
>= MAXQUOTAS
)
1452 if (vfs_busy(mp
, LK_NOWAIT
))
1458 error
= hfs_quotaon(p
, mp
, type
, datap
);
1462 error
= hfs_quotaoff(p
, mp
, type
);
1466 error
= hfs_setquota(mp
, uid
, type
, datap
);
1470 error
= hfs_setuse(mp
, uid
, type
, datap
);
1474 error
= hfs_getquota(mp
, uid
, type
, datap
);
1478 error
= hfs_qsync(mp
);
1482 error
= hfs_quotastat(mp
, type
, datap
);
1495 /* Subtype is composite of bits */
1496 #define HFS_SUBTYPE_JOURNALED 0x01
1497 #define HFS_SUBTYPE_CASESENSITIVE 0x02
1498 /* bits 2 - 6 reserved */
1499 #define HFS_SUBTYPE_STANDARDHFS 0x80
1502 * Get file system statistics.
1505 hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
1507 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1508 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1510 uint16_t subtype
= 0;
1512 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1514 sbp
->f_bsize
= (uint32_t)vcb
->blockSize
;
1515 sbp
->f_iosize
= (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
);
1516 sbp
->f_blocks
= (uint64_t)((unsigned long)vcb
->totalBlocks
);
1517 sbp
->f_bfree
= (uint64_t)((unsigned long )hfs_freeblks(hfsmp
, 0));
1518 sbp
->f_bavail
= (uint64_t)((unsigned long )hfs_freeblks(hfsmp
, 1));
1519 sbp
->f_files
= (uint64_t)((unsigned long )(vcb
->totalBlocks
- 2)); /* max files is constrained by total blocks */
1520 sbp
->f_ffree
= (uint64_t)((unsigned long )(MIN(freeCNIDs
, sbp
->f_bavail
)));
1523 * Subtypes (flavors) for HFS
1524 * 0: Mac OS Extended
1525 * 1: Mac OS Extended (Journaled)
1526 * 2: Mac OS Extended (Case Sensitive)
1527 * 3: Mac OS Extended (Case Sensitive, Journaled)
1529 * 128: Mac OS Standard
1532 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1533 subtype
= HFS_SUBTYPE_STANDARDHFS
;
1534 } else /* HFS Plus */ {
1536 subtype
|= HFS_SUBTYPE_JOURNALED
;
1537 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
1538 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
1540 sbp
->f_fssubtype
= subtype
;
1547 // XXXdbg -- this is a callback to be used by the journal to
1548 // get meta data blocks flushed out to disk.
1550 // XXXdbg -- be smarter and don't flush *every* block on each
1551 // call. try to only flush some so we don't wind up
1552 // being too synchronous.
1556 hfs_sync_metadata(void *arg
)
1558 struct mount
*mp
= (struct mount
*)arg
;
1559 struct hfsmount
*hfsmp
;
1562 int sectorsize
, retval
;
1563 daddr64_t priIDSector
;
1564 hfsmp
= VFSTOHFS(mp
);
1565 vcb
= HFSTOVCB(hfsmp
);
1567 // now make sure the super block is flushed
1568 sectorsize
= hfsmp
->hfs_phys_block_size
;
1569 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1570 HFS_PRI_SECTOR(sectorsize
));
1571 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1572 if ((retval
!= 0) && (retval
!= ENXIO
)) {
1573 printf("hfs_sync_metadata: can't read volume header at %d! (retval 0x%x)\n",
1574 priIDSector
, retval
);
1577 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1583 // the alternate super block...
1584 // XXXdbg - we probably don't need to do this each and every time.
1585 // hfs_btreeio.c:FlushAlternate() should flag when it was
1587 if (hfsmp
->hfs_alt_id_sector
) {
1588 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &bp
);
1589 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1598 struct hfs_sync_cargs
{
1607 hfs_sync_callback(struct vnode
*vp
, void *cargs
)
1610 struct hfs_sync_cargs
*args
;
1613 args
= (struct hfs_sync_cargs
*)cargs
;
1615 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1616 return (VNODE_RETURNED
);
1620 if ((cp
->c_flag
& C_MODIFIED
) ||
1621 (cp
->c_touch_acctime
| cp
->c_touch_chgtime
| cp
->c_touch_modtime
) ||
1622 vnode_hasdirtyblks(vp
)) {
1623 error
= hfs_fsync(vp
, args
->waitfor
, 0, args
->p
);
1626 args
->error
= error
;
1629 return (VNODE_RETURNED
);
1635 * Go through the disk queues to initiate sandbagged IO;
1636 * go through the inodes to write those that have been modified;
1637 * initiate the writing of the super block if it has been modified.
1639 * Note: we are always called with the filesystem marked `MPBUSY'.
1642 hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
)
1644 struct proc
*p
= vfs_context_proc(context
);
1646 struct hfsmount
*hfsmp
;
1648 struct vnode
*meta_vp
[4];
1650 int error
, allerror
= 0;
1651 struct hfs_sync_cargs args
;
1654 * During MNT_UPDATE hfs_changefs might be manipulating
1655 * vnodes so back off
1657 if (((uint32_t)vfs_flags(mp
)) & MNT_UPDATE
) /* XXX MNT_UPDATE may not be visible here */
1660 hfsmp
= VFSTOHFS(mp
);
1661 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1664 /* skip over frozen volumes */
1665 if (!lck_rw_try_lock_shared(&hfsmp
->hfs_insync
))
1668 args
.cred
= vfs_context_proc(context
);
1669 args
.waitfor
= waitfor
;
1673 * hfs_sync_callback will be called for each vnode
1674 * hung off of this mount point... the vnode will be
1675 * properly referenced and unreferenced around the callback
1677 vnode_iterate(mp
, 0, hfs_sync_callback
, (void *)&args
);
1680 allerror
= args
.error
;
1682 vcb
= HFSTOVCB(hfsmp
);
1684 meta_vp
[0] = vcb
->extentsRefNum
;
1685 meta_vp
[1] = vcb
->catalogRefNum
;
1686 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1687 meta_vp
[3] = hfsmp
->hfs_attribute_vp
; /* Optional file */
1689 /* Now sync our three metadata files */
1690 for (i
= 0; i
< 4; ++i
) {
1694 if ((btvp
==0) || (vnode_mount(btvp
) != mp
))
1697 /* XXX use hfs_systemfile_lock instead ? */
1698 (void) hfs_lock(VTOC(btvp
), HFS_EXCLUSIVE_LOCK
);
1701 if (((cp
->c_flag
& C_MODIFIED
) == 0) &&
1702 (cp
->c_touch_acctime
== 0) &&
1703 (cp
->c_touch_chgtime
== 0) &&
1704 (cp
->c_touch_modtime
== 0) &&
1705 vnode_hasdirtyblks(btvp
) == 0) {
1706 hfs_unlock(VTOC(btvp
));
1709 error
= vnode_get(btvp
);
1711 hfs_unlock(VTOC(btvp
));
1714 if ((error
= hfs_fsync(btvp
, waitfor
, 0, p
)))
1722 * Force stale file system control information to be flushed.
1724 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1725 if ((error
= VNOP_FSYNC(hfsmp
->hfs_devvp
, waitfor
, context
))) {
1733 hfs_hotfilesync(hfsmp
, p
);
1735 * Write back modified superblock.
1738 if (IsVCBDirty(vcb
)) {
1739 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1745 journal_flush(hfsmp
->jnl
);
1748 lck_rw_unlock_shared(&hfsmp
->hfs_insync
);
1754 * File handle to vnode
1756 * Have to be really careful about stale file handles:
1757 * - check that the cnode id is valid
1758 * - call hfs_vget() to get the locked cnode
1759 * - check for an unallocated cnode (i_mode == 0)
1760 * - check that the given client host has export rights and return
1761 * those rights via. exflagsp and credanonp
1764 hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
)
1766 struct hfsfid
*hfsfhp
;
1771 hfsfhp
= (struct hfsfid
*)fhp
;
1773 if (fhlen
< sizeof(struct hfsfid
))
1776 result
= hfs_vget(VFSTOHFS(mp
), ntohl(hfsfhp
->hfsfid_cnid
), &nvp
, 0);
1778 if (result
== ENOENT
)
1783 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1784 * For NFS, we are assuming that only if the createtime was moved
1785 * forward would it mean the fileID got reused in that session by
1786 * wrapping. We don't have a volume ID or other unique identifier to
1787 * to use here for a generation ID across reboots, crashes where
1788 * metadata noting lastFileID didn't make it to disk but client has
1789 * it, or volume erasures where fileIDs start over again. Lastly,
1790 * with HFS allowing "wraps" of fileIDs now, this becomes more
1791 * error prone. Future, would be change the "wrap bit" to a unique
1792 * wrap number and use that for generation number. For now do this.
1794 if ((ntohl(hfsfhp
->hfsfid_gen
) < VTOC(nvp
)->c_itime
)) {
1795 hfs_unlock(VTOC(nvp
));
1801 hfs_unlock(VTOC(nvp
));
1807 * Vnode pointer to File handle
1811 hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
)
1814 struct hfsfid
*hfsfhp
;
1816 if (ISHFS(VTOVCB(vp
)))
1817 return (ENOTSUP
); /* hfs standard is not exportable */
1819 if (*fhlenp
< (int)sizeof(struct hfsfid
))
1823 hfsfhp
= (struct hfsfid
*)fhp
;
1824 hfsfhp
->hfsfid_cnid
= htonl(cp
->c_fileid
);
1825 hfsfhp
->hfsfid_gen
= htonl(cp
->c_itime
);
1826 *fhlenp
= sizeof(struct hfsfid
);
1833 * Initial HFS filesystems, done only once.
1836 hfs_init(__unused
struct vfsconf
*vfsp
)
1838 static int done
= 0;
1844 hfs_converterinit();
1852 hfs_lock_attr
= lck_attr_alloc_init();
1853 hfs_group_attr
= lck_grp_attr_alloc_init();
1854 hfs_mutex_group
= lck_grp_alloc_init("hfs-mutex", hfs_group_attr
);
1855 hfs_rwlock_group
= lck_grp_alloc_init("hfs-rwlock", hfs_group_attr
);
1862 hfs_getmountpoint(vp
, hfsmpp
)
1864 struct hfsmount
**hfsmpp
;
1866 struct hfsmount
* hfsmp
;
1867 char fstypename
[MFSNAMELEN
];
1872 if (!vnode_isvroot(vp
))
1875 vnode_vfsname(vp
, fstypename
);
1876 if (strcmp(fstypename
, "hfs") != 0)
1881 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1890 #include <sys/filedesc.h>
1893 * HFS filesystem related variables.
1896 hfs_sysctl(int *name
, __unused u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
1897 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
1899 struct proc
*p
= vfs_context_proc(context
);
1901 struct hfsmount
*hfsmp
;
1903 /* all sysctl names at this level are terminal */
1905 if (name
[0] == HFS_ENCODINGBIAS
) {
1908 bias
= hfs_getencodingbias();
1909 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
1910 if (error
== 0 && newp
)
1911 hfs_setencodingbias(bias
);
1914 } else if (name
[0] == HFS_EXTEND_FS
) {
1916 vnode_t vp
= p
->p_fd
->fd_cdir
;
1918 if (newp
== USER_ADDR_NULL
|| vp
== NULL
)
1920 if ((error
= hfs_getmountpoint(vp
, &hfsmp
)))
1922 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, &newsize
);
1926 error
= hfs_extendfs(hfsmp
, newsize
, context
);
1929 } else if (name
[0] == HFS_ENCODINGHINT
) {
1933 u_int16_t
*unicode_name
;
1936 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
1937 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
1938 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
1940 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
1942 error
= utf8_decodestr(filename
, newlen
- 1, unicode_name
,
1943 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
1945 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
1946 error
= sysctl_int(oldp
, oldlenp
, USER_ADDR_NULL
, 0, &hint
);
1949 FREE(unicode_name
, M_TEMP
);
1950 FREE(filename
, M_TEMP
);
1953 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
1954 // make the file system journaled...
1955 struct vnode
*vp
= p
->p_fd
->fd_cdir
, *jvp
;
1957 struct cat_attr jnl_attr
, jinfo_attr
;
1958 struct cat_fork jnl_fork
, jinfo_fork
;
1962 /* Only root can enable journaling */
1970 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1973 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
1974 printf("hfs: can't make a plain hfs volume journaled.\n");
1979 printf("hfs: volume @ mp 0x%x is already journaled!\n", vnode_mount(vp
));
1983 vcb
= HFSTOVCB(hfsmp
);
1984 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1985 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
1986 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
1988 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
1989 hfs_systemfile_unlock(hfsmp
, lockflags
);
1992 hfs_systemfile_unlock(hfsmp
, lockflags
);
1994 // make sure these both exist!
1995 if ( GetFileInfo(vcb
, kHFSRootFolderID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
1996 || GetFileInfo(vcb
, kHFSRootFolderID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2001 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, context
);
2003 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2004 (off_t
)name
[2], (off_t
)name
[3]);
2006 jvp
= hfsmp
->hfs_devvp
;
2007 jnl
= journal_create(jvp
,
2008 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2009 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2010 (off_t
)((unsigned)name
[3]),
2012 hfsmp
->hfs_phys_block_size
,
2015 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2018 printf("hfs: FAILED to create the journal!\n");
2019 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2020 VNOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2027 hfs_global_exclusive_lock_acquire(hfsmp
);
2030 * Flush all dirty metadata buffers.
2032 buf_flushdirtyblks(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, "hfs_sysctl");
2033 buf_flushdirtyblks(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2034 buf_flushdirtyblks(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2035 buf_flushdirtyblks(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2036 if (hfsmp
->hfs_attribute_vp
)
2037 buf_flushdirtyblks(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2039 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2040 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2044 // save this off for the hack-y check in hfs_remove()
2045 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2046 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2047 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2048 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2050 vfs_setflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
2052 hfs_global_exclusive_lock_release(hfsmp
);
2053 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2056 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2057 // clear the journaling bit
2058 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2060 /* Only root can disable journaling */
2069 printf("hfs: disabling journaling for mount @ 0x%x\n", vnode_mount(vp
));
2071 hfs_global_exclusive_lock_acquire(hfsmp
);
2073 // Lights out for you buddy!
2074 journal_close(hfsmp
->jnl
);
2077 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2078 VNOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2081 vfs_clearflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
2082 hfsmp
->jnl_start
= 0;
2083 hfsmp
->hfs_jnlinfoblkid
= 0;
2084 hfsmp
->hfs_jnlfileid
= 0;
2086 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2088 hfs_global_exclusive_lock_release(hfsmp
);
2089 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2092 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2093 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2094 off_t jnl_start
, jnl_size
;
2100 if (hfsmp
->jnl
== NULL
) {
2104 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2105 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2108 if ((error
= copyout((caddr_t
)&jnl_start
, CAST_USER_ADDR_T(name
[1]), sizeof(off_t
))) != 0) {
2111 if ((error
= copyout((caddr_t
)&jnl_size
, CAST_USER_ADDR_T(name
[2]), sizeof(off_t
))) != 0) {
2116 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2118 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2120 } else if (name
[0] == VFS_CTL_QUERY
) {
2121 struct sysctl_req
*req
;
2123 struct user_vfsidctl user_vc
;
2126 boolean_t is_64_bit
;
2128 is_64_bit
= proc_is64bit(p
);
2129 req
= CAST_DOWN(struct sysctl_req
*, oldp
); /* we're new style vfs sysctl. */
2132 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2133 if (error
) return (error
);
2135 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
2138 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2139 if (error
) return (error
);
2141 mp
= vfs_getvfs(&vc
.vc_fsid
);
2143 if (mp
== NULL
) return (ENOENT
);
2145 hfsmp
= VFSTOHFS(mp
);
2146 bzero(&vq
, sizeof(vq
));
2147 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2148 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2156 hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, __unused vfs_context_t context
)
2158 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)ino
, vpp
, 1);
2163 * Look up an HFS object by ID.
2165 * The object is returned with an iocount reference and the cnode locked.
2167 * If the object is a file then it will represent the data fork.
2171 hfs_vget(struct hfsmount
*hfsmp
, cnid_t cnid
, struct vnode
**vpp
, int skiplock
)
2173 struct vnode
*vp
= NULL
;
2174 struct cat_desc cndesc
;
2175 struct cat_attr cnattr
;
2176 struct cat_fork cnfork
;
2177 struct componentname cn
;
2178 u_int32_t linkref
= 0;
2181 /* Check for cnids that should't be exported. */
2182 if ((cnid
< kHFSFirstUserCatalogNodeID
)
2183 && (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
))
2186 /* Don't export HFS Private Data dir. */
2187 if (cnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
2191 * Check the hash first
2193 vp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, cnid
, 0, skiplock
);
2199 bzero(&cndesc
, sizeof(cndesc
));
2200 bzero(&cnattr
, sizeof(cnattr
));
2201 bzero(&cnfork
, sizeof(cnfork
));
2204 * Not in hash, lookup in catalog
2206 if (cnid
== kHFSRootParentID
) {
2207 static char hfs_rootname
[] = "/";
2209 cndesc
.cd_nameptr
= &hfs_rootname
[0];
2210 cndesc
.cd_namelen
= 1;
2211 cndesc
.cd_parentcnid
= kHFSRootParentID
;
2212 cndesc
.cd_cnid
= kHFSRootFolderID
;
2213 cndesc
.cd_flags
= CD_ISDIR
;
2215 cnattr
.ca_fileid
= kHFSRootFolderID
;
2216 cnattr
.ca_nlink
= 2;
2217 cnattr
.ca_entries
= 1;
2218 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
2222 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2223 error
= cat_idlookup(hfsmp
, cnid
, &cndesc
, &cnattr
, &cnfork
);
2224 hfs_systemfile_unlock(hfsmp
, lockflags
);
2232 * If we just looked up a raw hardlink inode,
2233 * then finish initializing it.
2235 if ((cndesc
.cd_parentcnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
) &&
2236 (bcmp(cndesc
.cd_nameptr
, HFS_INODE_PREFIX
, HFS_INODE_PREFIX_LEN
) == 0)) {
2237 linkref
= strtoul((const char*)&cndesc
.cd_nameptr
[HFS_INODE_PREFIX_LEN
], NULL
, 10);
2238 cnattr
.ca_rdev
= linkref
;
2243 * Supply hfs_getnewvnode with a component name.
2245 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2246 cn
.cn_nameiop
= LOOKUP
;
2247 cn
.cn_flags
= ISLASTCN
| HASBUF
;
2248 cn
.cn_context
= NULL
;
2249 cn
.cn_pnlen
= MAXPATHLEN
;
2250 cn
.cn_nameptr
= cn
.cn_pnbuf
;
2251 cn
.cn_namelen
= cndesc
.cd_namelen
;
2254 bcopy(cndesc
.cd_nameptr
, cn
.cn_nameptr
, cndesc
.cd_namelen
+ 1);
2256 /* XXX should we supply the parent as well... ? */
2257 error
= hfs_getnewvnode(hfsmp
, NULLVP
, &cn
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2258 if (error
== 0 && linkref
!= 0) {
2259 VTOC(vp
)->c_flag
|= C_HARDLINK
;
2262 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
2264 cat_releasedesc(&cndesc
);
2267 hfs_unlock(VTOC(vp
));
2273 * Flush out all the files in a filesystem.
2276 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2278 struct hfsmount
*hfsmp
;
2279 struct vnode
*skipvp
= NULLVP
;
2284 hfsmp
= VFSTOHFS(mp
);
2288 * The open quota files have an indirect reference on
2289 * the root directory vnode. We must account for this
2290 * extra reference when doing the intial vflush.
2293 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2295 /* Find out how many quota files we have open. */
2296 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2297 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2301 /* Obtain the root vnode so we can skip over it. */
2302 skipvp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, kHFSRootFolderID
, 0, 0);
2306 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2310 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2313 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2316 * See if there are additional references on the
2317 * root vp besides the ones obtained from the open
2318 * quota files and the hfs_chash_getvnode call above.
2321 (vnode_isinuse(skipvp
, quotafilecnt
))) {
2322 error
= EBUSY
; /* root directory is still open */
2324 hfs_unlock(VTOC(skipvp
));
2327 if (error
&& (flags
& FORCECLOSE
) == 0)
2330 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2331 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2333 hfs_quotaoff(p
, mp
, i
);
2335 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2343 * Update volume encoding bitmap (HFS Plus only)
2347 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2349 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2350 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2355 case kTextEncodingMacUkrainian
:
2356 index
= kIndexMacUkrainian
;
2358 case kTextEncodingMacFarsi
:
2359 index
= kIndexMacFarsi
;
2367 HFS_MOUNT_LOCK(hfsmp
, TRUE
)
2368 hfsmp
->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2369 hfsmp
->vcbFlags
|= 0xFF00;
2370 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2375 * Update volume stats
2377 * On journal volumes this will cause a volume header flush
2381 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2387 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2389 hfsmp
->vcbFlags
|= 0xFF00;
2390 hfsmp
->hfs_mtime
= tv
.tv_sec
;
2396 if (hfsmp
->hfs_dircount
!= 0xFFFFFFFF)
2397 ++hfsmp
->hfs_dircount
;
2398 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2399 ++hfsmp
->vcbNmRtDirs
;
2402 if (hfsmp
->hfs_dircount
!= 0)
2403 --hfsmp
->hfs_dircount
;
2404 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2405 --hfsmp
->vcbNmRtDirs
;
2408 if (hfsmp
->hfs_filecount
!= 0xFFFFFFFF)
2409 ++hfsmp
->hfs_filecount
;
2410 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2414 if (hfsmp
->hfs_filecount
!= 0)
2415 --hfsmp
->hfs_filecount
;
2416 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2421 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2424 hfs_flushvolumeheader(hfsmp
, 0, 0);
2432 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2434 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2435 struct filefork
*fp
;
2436 HFSMasterDirectoryBlock
*mdb
;
2437 struct buf
*bp
= NULL
;
2442 sectorsize
= hfsmp
->hfs_phys_block_size
;
2443 retval
= (int)buf_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2450 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2452 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2454 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2455 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2456 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2457 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2458 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2459 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2460 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2461 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2463 namelen
= strlen(vcb
->vcbVN
);
2464 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2465 /* Retry with MacRoman in case that's how it was exported. */
2467 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2469 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2470 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2471 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2472 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2473 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2475 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2477 fp
= VTOF(vcb
->extentsRefNum
);
2478 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2479 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2480 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2481 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2482 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2483 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2484 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2485 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2486 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2488 fp
= VTOF(vcb
->catalogRefNum
);
2489 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2490 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2491 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2492 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2493 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2494 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2495 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2496 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2497 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2499 MarkVCBClean( vcb
);
2501 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2503 /* If requested, flush out the alternate MDB */
2505 struct buf
*alt_bp
= NULL
;
2507 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2508 bcopy(mdb
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2510 (void) VNOP_BWRITE(alt_bp
);
2515 if (waitfor
!= MNT_WAIT
)
2518 retval
= VNOP_BWRITE(bp
);
2524 * Flush any dirty in-memory mount data to the on-disk
2527 * Note: the on-disk volume signature is intentionally
2528 * not flushed since the on-disk "H+" and "HX" signatures
2529 * are always stored in-memory as "H+".
2533 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2535 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2536 struct filefork
*fp
;
2537 HFSPlusVolumeHeader
*volumeHeader
;
2542 daddr64_t priIDSector
;
2544 u_int16_t signature
;
2545 u_int16_t hfsversion
;
2547 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2550 if (vcb
->vcbSigWord
== kHFSSigWord
)
2551 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2555 sectorsize
= hfsmp
->hfs_phys_block_size
;
2556 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2557 HFS_PRI_SECTOR(sectorsize
));
2559 if (hfs_start_transaction(hfsmp
) != 0) {
2563 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2568 hfs_end_transaction(hfsmp
);
2570 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2575 journal_modify_block_start(hfsmp
->jnl
, bp
);
2578 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2581 * Sanity check what we just read.
2583 signature
= SWAP_BE16 (volumeHeader
->signature
);
2584 hfsversion
= SWAP_BE16 (volumeHeader
->version
);
2585 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2586 (hfsversion
< kHFSPlusVersion
) || (hfsversion
> 100) ||
2587 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2589 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2590 vcb
->vcbVN
, signature
, hfsversion
,
2591 SWAP_BE32 (volumeHeader
->blockSize
));
2593 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2599 * For embedded HFS+ volumes, update create date if it changed
2600 * (ie from a setattrlist call)
2602 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2603 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2605 HFSMasterDirectoryBlock
*mdb
;
2607 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
),
2608 sectorsize
, NOCRED
, &bp2
);
2614 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp2
) +
2615 HFS_PRI_OFFSET(sectorsize
));
2617 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2620 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2623 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2626 journal_modify_block_end(hfsmp
->jnl
, bp2
);
2628 (void) VNOP_BWRITE(bp2
); /* write out the changes */
2633 buf_brelse(bp2
); /* just release it */
2638 if (1 /* hfsmp->jnl == 0 */) {
2639 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2642 /* Note: only update the lower 16 bits worth of attributes */
2643 volumeHeader
->attributes
= SWAP_BE32 (vcb
->vcbAtrb
);
2644 volumeHeader
->journalInfoBlock
= SWAP_BE32 (vcb
->vcbJinfoBlock
);
2646 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2648 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2650 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2651 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2652 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2653 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2654 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2655 volumeHeader
->totalBlocks
= SWAP_BE32 (vcb
->totalBlocks
);
2656 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2657 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2658 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2659 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2660 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2661 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2662 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2664 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0) {
2665 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2669 /* Sync Extents over-flow file meta data */
2670 fp
= VTOF(vcb
->extentsRefNum
);
2671 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2672 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2673 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2674 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2675 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2676 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2678 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2679 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2680 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2681 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2684 /* Sync Catalog file meta data */
2685 fp
= VTOF(vcb
->catalogRefNum
);
2686 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2687 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2688 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2689 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2690 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2691 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2693 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2694 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2695 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2696 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2699 /* Sync Allocation file meta data */
2700 fp
= VTOF(vcb
->allocationsRefNum
);
2701 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2702 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2703 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2704 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2705 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2706 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2708 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2709 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2710 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2711 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2714 /* Sync Attribute file meta data */
2715 if (hfsmp
->hfs_attribute_vp
) {
2716 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
2717 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2718 volumeHeader
->attributesFile
.extents
[i
].startBlock
=
2719 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2720 volumeHeader
->attributesFile
.extents
[i
].blockCount
=
2721 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2723 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2724 volumeHeader
->attributesFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2725 volumeHeader
->attributesFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2726 volumeHeader
->attributesFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2729 vcb
->vcbFlags
&= 0x00FF;
2731 if (1 /* hfsmp->jnl == 0 */) {
2732 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2735 /* If requested, flush out the alternate volume header */
2736 if (altflush
&& hfsmp
->hfs_alt_id_sector
) {
2737 struct buf
*alt_bp
= NULL
;
2739 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2741 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2744 bcopy(volumeHeader
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2747 journal_modify_block_end(hfsmp
->jnl
, alt_bp
);
2749 (void) VNOP_BWRITE(alt_bp
);
2756 journal_modify_block_end(hfsmp
->jnl
, bp
);
2758 if (waitfor
!= MNT_WAIT
)
2761 retval
= VNOP_BWRITE(bp
);
2762 /* When critical data changes, flush the device cache */
2763 if (critical
&& (retval
== 0)) {
2764 (void) VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2765 NULL
, FWRITE
, NULL
);
2769 hfs_end_transaction(hfsmp
);
2776 * Extend a file system.
2780 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
2782 struct proc
*p
= vfs_context_proc(context
);
2783 kauth_cred_t cred
= vfs_context_ucred(context
);
2785 struct vnode
*devvp
;
2787 struct filefork
*fp
= NULL
;
2789 struct cat_fork forkdata
;
2791 u_int64_t newblkcnt
;
2792 u_int64_t prev_phys_block_count
;
2794 u_int64_t sectorcnt
;
2795 u_int32_t sectorsize
;
2796 daddr64_t prev_alt_sector
;
2801 devvp
= hfsmp
->hfs_devvp
;
2802 vcb
= HFSTOVCB(hfsmp
);
2805 * - HFS Plus file systems only.
2806 * - Journaling must be enabled.
2807 * - No embedded volumes.
2809 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
2810 (hfsmp
->jnl
== NULL
) ||
2811 (vcb
->hfsPlusIOPosOffset
!= 0)) {
2815 * If extending file system by non-root, then verify
2816 * ownership and check permissions.
2818 if (suser(cred
, NULL
)) {
2819 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0);
2823 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
2825 error
= hfs_write_access(vp
, cred
, p
, false);
2827 hfs_unlock(VTOC(vp
));
2832 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
2836 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, context
)) {
2839 if (sectorsize
!= hfsmp
->hfs_phys_block_size
) {
2842 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, context
)) {
2845 if ((sectorsize
* sectorcnt
) < newsize
) {
2846 printf("hfs_extendfs: not enough space on device\n");
2849 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
2852 * Validate new size.
2854 if ((newsize
<= oldsize
) || (newsize
% sectorsize
)) {
2855 printf("hfs_extendfs: invalid size\n");
2858 newblkcnt
= newsize
/ vcb
->blockSize
;
2859 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
2862 addblks
= newblkcnt
- vcb
->totalBlocks
;
2864 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
2866 * Enclose changes inside a transaction.
2868 if (hfs_start_transaction(hfsmp
) != 0) {
2872 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2873 vp
= vcb
->allocationsRefNum
;
2875 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
2878 * Calculate additional space required (if any) by allocation bitmap.
2880 bitmapblks
= roundup(newblkcnt
/ 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
2881 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
2882 bitmapblks
-= fp
->ff_blocks
;
2886 if (bitmapblks
> 0) {
2891 * Add a new extent to the allocation bitmap file.
2893 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
2895 printf("hfs_extendfs: error %d adding extents\n", error
);
2898 blkcnt
= bitmapblks
;
2899 blkno
= (daddr64_t
)fp
->ff_blocks
;
2900 fp
->ff_blocks
+= bitmapblks
;
2901 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
2902 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
2904 * Zero out the new bitmap blocks.
2909 while (blkcnt
> 0) {
2910 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
2917 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
2919 error
= (int)buf_bwrite(bp
);
2927 printf("hfs_extendfs: error %d clearing blocks\n", error
);
2931 * Mark the new bitmap space as allocated.
2933 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
2935 printf("hfs_extendfs: error %d setting bitmap\n", error
);
2940 * Mark the new alternate VH as allocated.
2942 if (vcb
->blockSize
== 512)
2943 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
2945 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
2947 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
2951 * Mark the old alternate VH as free.
2953 if (vcb
->blockSize
== 512)
2954 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
2956 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
2958 * Adjust file system variables for new space.
2960 prev_phys_block_count
= hfsmp
->hfs_phys_block_count
;
2961 prev_alt_sector
= hfsmp
->hfs_alt_id_sector
;
2963 vcb
->totalBlocks
+= addblks
;
2964 vcb
->freeBlocks
+= addblks
- bitmapblks
;
2965 hfsmp
->hfs_phys_block_count
= newsize
/ sectorsize
;
2966 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sectorsize
) +
2967 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2969 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
2971 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
2973 * Restore to old state.
2975 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
2976 vcb
->totalBlocks
-= addblks
;
2977 vcb
->freeBlocks
-= addblks
- bitmapblks
;
2978 hfsmp
->hfs_phys_block_count
= prev_phys_block_count
;
2979 hfsmp
->hfs_alt_id_sector
= prev_alt_sector
;
2981 if (vcb
->blockSize
== 512)
2982 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
2984 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
2988 * Invalidate the old alternate volume header.
2991 if (prev_alt_sector
) {
2992 if (buf_meta_bread(hfsmp
->hfs_devvp
, prev_alt_sector
, sectorsize
,
2993 NOCRED
, &bp
) == 0) {
2994 journal_modify_block_start(hfsmp
->jnl
, bp
);
2996 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2998 journal_modify_block_end(hfsmp
->jnl
, bp
);
3005 /* Restore allocation fork. */
3006 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3007 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3010 hfs_systemfile_unlock(hfsmp
, lockflags
);
3011 hfs_end_transaction(hfsmp
);
3016 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3019 * Truncate a file system (while still mounted).
3023 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, __unused vfs_context_t context
)
3025 struct buf
*bp
= NULL
;
3027 u_int32_t newblkcnt
;
3028 u_int32_t reclaimblks
;
3030 int transaction_begun
= 0;
3034 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3035 if (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) {
3036 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3039 hfsmp
->hfs_flags
|= HFS_RESIZE_IN_PROGRESS
;
3040 hfsmp
->hfs_resize_filesmoved
= 0;
3041 hfsmp
->hfs_resize_totalfiles
= 0;
3042 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3045 * - Journaled HFS Plus volumes only.
3046 * - No embedded volumes.
3048 if ((hfsmp
->jnl
== NULL
) ||
3049 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
3053 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3054 newblkcnt
= newsize
/ hfsmp
->blockSize
;
3055 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
3057 /* Make sure new size is valid. */
3058 if ((newsize
< HFS_MIN_SIZE
) ||
3059 (newsize
>= oldsize
) ||
3060 (newsize
% hfsmp
->hfs_phys_block_size
)) {
3064 /* Make sure there's enough space to work with. */
3065 if (reclaimblks
>= hfs_freeblks(hfsmp
, 1)) {
3069 /* Start with a clean journal. */
3070 journal_flush(hfsmp
->jnl
);
3072 if (hfs_start_transaction(hfsmp
) != 0) {
3076 transaction_begun
= 1;
3079 * Look for files that have blocks beyond newblkcnt.
3081 if (hfs_isallocated(hfsmp
, newblkcnt
, reclaimblks
- 1)) {
3083 * hfs_reclaimspace will use separate transactions when
3084 * relocating files (so we don't overwhelm the journal).
3086 hfs_end_transaction(hfsmp
);
3087 transaction_begun
= 0;
3089 /* Attempt to reclaim some space. */
3090 if (hfs_reclaimspace(hfsmp
, newblkcnt
, reclaimblks
) != 0) {
3091 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp
->vcbVN
);
3095 if (hfs_start_transaction(hfsmp
) != 0) {
3099 transaction_begun
= 1;
3101 /* Check if we're clear now. */
3102 if (hfs_isallocated(hfsmp
, newblkcnt
, reclaimblks
- 1)) {
3103 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp
->vcbVN
);
3104 error
= EAGAIN
; /* tell client to try again */
3108 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3111 * Mark the old alternate volume header as free.
3112 * We don't bother shrinking allocation bitmap file.
3114 if (hfsmp
->blockSize
== 512)
3115 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
3117 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
3120 * Allocate last block for alternate volume header.
3122 if (hfsmp
->blockSize
== 512)
3123 error
= BlockMarkAllocated(hfsmp
, newblkcnt
- 2, 2);
3125 error
= BlockMarkAllocated(hfsmp
, newblkcnt
- 1, 1);
3132 * Invalidate the existing alternate volume header.
3134 * Don't do this as a transaction (don't call journal_modify_block)
3135 * since this block will be outside of the truncated file system!
3137 if (hfsmp
->hfs_alt_id_sector
) {
3138 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
,
3139 hfsmp
->hfs_phys_block_size
, NOCRED
, &bp
) == 0) {
3141 bzero((void*)((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_phys_block_size
)), kMDBSize
);
3142 (void) VNOP_BWRITE(bp
);
3149 /* Log successful shrinking. */
3150 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
3151 hfsmp
->vcbVN
, newblkcnt
, hfsmp
->totalBlocks
);
3154 * Adjust file system variables and flush them to disk.
3156 hfsmp
->freeBlocks
-= hfsmp
->totalBlocks
- newblkcnt
;
3157 hfsmp
->totalBlocks
= newblkcnt
;
3158 hfsmp
->hfs_phys_block_count
= newsize
/ hfsmp
->hfs_phys_block_size
;
3159 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
, hfsmp
->hfs_phys_block_count
);
3160 MarkVCBDirty(hfsmp
);
3161 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3163 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
3166 hfs_systemfile_unlock(hfsmp
, lockflags
);
3168 if (transaction_begun
) {
3169 hfs_end_transaction(hfsmp
);
3170 journal_flush(hfsmp
->jnl
);
3173 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3174 hfsmp
->hfs_flags
&= ~HFS_RESIZE_IN_PROGRESS
;
3175 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3182 * Reclaim space at the end of a file system.
3185 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
)
3187 struct vnode
*vp
= NULL
;
3189 struct BTreeIterator
* iterator
= NULL
;
3190 struct FSBufferDescriptor btdata
;
3191 struct HFSPlusCatalogFile filerec
;
3192 struct filefork
*fp
;
3193 u_int32_t saved_next_allocation
;
3201 u_long blkstomove
= 0;
3205 int lastprogress
= 0;
3208 /* Check if Attributes file overlaps reclaim area. */
3209 if (hfsmp
->hfs_attribute_vp
) {
3210 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
3212 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3213 if (fp
->ff_extents
[i
].blockCount
== 0) {
3216 datablks
+= fp
->ff_extents
[i
].blockCount
;
3217 block
= fp
->ff_extents
[i
].startBlock
+ fp
->ff_extents
[i
].blockCount
;
3218 if (block
>= startblk
) {
3219 printf("hfs_reclaimspace: Attributes file can't move\n");
3223 if ((i
== kHFSPlusExtentDensity
) && (fp
->ff_blocks
> datablks
)) {
3224 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, datablks
, kHFSAttributesFileID
, 0)) {
3225 printf("hfs_reclaimspace: Attributes file can't move\n");
3230 /* Check if Catalog file overlaps reclaim area. */
3231 fp
= VTOF(hfsmp
->hfs_catalog_vp
);
3233 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3234 if (fp
->ff_extents
[i
].blockCount
== 0) {
3237 datablks
+= fp
->ff_extents
[i
].blockCount
;
3238 block
= fp
->ff_extents
[i
].startBlock
+ fp
->ff_extents
[i
].blockCount
;
3239 if (block
>= startblk
) {
3240 printf("hfs_reclaimspace: Catalog file can't move\n");
3244 if ((i
== kHFSPlusExtentDensity
) && (fp
->ff_blocks
> datablks
)) {
3245 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, datablks
, kHFSCatalogFileID
, 0)) {
3246 printf("hfs_reclaimspace: Catalog file can't move\n");
3251 /* For now move a maximum of 250,000 files. */
3252 maxfilecnt
= MIN(hfsmp
->hfs_filecount
, 250000);
3253 maxfilecnt
= MIN((u_long
)maxfilecnt
, reclaimblks
);
3254 cnidbufsize
= maxfilecnt
* sizeof(cnid_t
);
3255 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&cnidbufp
, cnidbufsize
)) {
3258 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
3259 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
3263 saved_next_allocation
= hfsmp
->nextAllocation
;
3264 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_start
;
3266 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
3267 bzero(iterator
, sizeof(*iterator
));
3269 btdata
.bufferAddress
= &filerec
;
3270 btdata
.itemSize
= sizeof(filerec
);
3271 btdata
.itemCount
= 1;
3273 /* Keep the Catalog and extents files locked during iteration. */
3274 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_SHARED_LOCK
);
3276 error
= BTIterateRecord(fcb
, kBTreeFirstRecord
, iterator
, NULL
, NULL
);
3281 * Iterate over all the catalog records looking for files
3282 * that overlap into the space we're trying to free up.
3284 for (filecnt
= 0; filecnt
< maxfilecnt
; ) {
3285 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3287 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
3292 if (filerec
.recordType
!= kHFSPlusFileRecord
) {
3295 datablks
= rsrcblks
= 0;
3297 * Check if either fork overlaps target space.
3299 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3300 if (filerec
.dataFork
.extents
[i
].blockCount
!= 0) {
3301 datablks
+= filerec
.dataFork
.extents
[i
].blockCount
;
3302 block
= filerec
.dataFork
.extents
[i
].startBlock
+
3303 filerec
.dataFork
.extents
[i
].blockCount
;
3304 if (block
>= startblk
) {
3305 if ((filerec
.fileID
== hfsmp
->hfs_jnlfileid
) ||
3306 (filerec
.fileID
== hfsmp
->hfs_jnlinfoblkid
)) {
3307 printf("hfs_reclaimspace: cannot move active journal\n");
3311 cnidbufp
[filecnt
++] = filerec
.fileID
;
3312 blkstomove
+= filerec
.dataFork
.totalBlocks
;
3316 if (filerec
.resourceFork
.extents
[i
].blockCount
!= 0) {
3317 rsrcblks
+= filerec
.resourceFork
.extents
[i
].blockCount
;
3318 block
= filerec
.resourceFork
.extents
[i
].startBlock
+
3319 filerec
.resourceFork
.extents
[i
].blockCount
;
3320 if (block
>= startblk
) {
3321 cnidbufp
[filecnt
++] = filerec
.fileID
;
3322 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
3328 * Check for any overflow extents that overlap.
3330 if (i
== kHFSPlusExtentDensity
) {
3331 if (filerec
.dataFork
.totalBlocks
> datablks
) {
3332 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, datablks
, filerec
.fileID
, 0)) {
3333 cnidbufp
[filecnt
++] = filerec
.fileID
;
3334 blkstomove
+= filerec
.dataFork
.totalBlocks
;
3336 } else if (filerec
.resourceFork
.totalBlocks
> rsrcblks
) {
3337 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, rsrcblks
, filerec
.fileID
, 1)) {
3338 cnidbufp
[filecnt
++] = filerec
.fileID
;
3339 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
3349 /* All done with catalog. */
3350 hfs_systemfile_unlock(hfsmp
, lockflags
);
3355 * Double check space requirements to make sure
3356 * there is enough space to relocate any files
3357 * that reside in the reclaim area.
3359 * Blocks To Move --------------
3362 * ------------------------------------------------------------------------
3366 * ------------------------------------------------------------------------
3368 * <------------------- New Total Blocks ------------------><-- Reclaim -->
3370 * <------------------------ Original Total Blocks ----------------------->
3373 if ((reclaimblks
+ blkstomove
) >= hfs_freeblks(hfsmp
, 1)) {
3377 hfsmp
->hfs_resize_filesmoved
= 0;
3378 hfsmp
->hfs_resize_totalfiles
= filecnt
;
3380 /* Now move any files that are in the way. */
3381 for (i
= 0; i
< filecnt
; ++i
) {
3384 if (hfs_vget(hfsmp
, cnidbufp
[i
], &vp
, 0) != 0)
3387 /* Relocate any data fork blocks. */
3388 if (VTOF(vp
)->ff_blocks
> 0) {
3389 error
= hfs_relocate(vp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
3394 /* Relocate any resource fork blocks. */
3395 if ((VTOC((vp
))->c_blocks
- VTOF((vp
))->ff_blocks
) > 0) {
3396 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, current_proc());
3399 error
= hfs_relocate(rvp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
3404 hfs_unlock(VTOC(vp
));
3408 ++hfsmp
->hfs_resize_filesmoved
;
3410 /* Report intermediate progress. */
3411 if (filecnt
> 100) {
3414 progress
= (i
* 100) / filecnt
;
3415 if (progress
> (lastprogress
+ 9)) {
3416 printf("hfs_reclaimspace: %d%% done...\n", progress
);
3417 lastprogress
= progress
;
3422 hfs_unlock(VTOC(vp
));
3426 if (hfsmp
->hfs_resize_filesmoved
!= 0) {
3427 printf("hfs_reclaimspace: relocated %d files on \"%s\"\n",
3428 (int)hfsmp
->hfs_resize_filesmoved
, hfsmp
->vcbVN
);
3431 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3432 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
3435 * Restore the roving allocation pointer on errors.
3436 * (but only if we didn't move any files)
3438 if (error
&& hfsmp
->hfs_resize_filesmoved
== 0) {
3439 hfsmp
->nextAllocation
= saved_next_allocation
;
3446 * Check if there are any overflow extents that overlap.
3449 hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
, u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
)
3451 struct BTreeIterator
* iterator
= NULL
;
3452 struct FSBufferDescriptor btdata
;
3453 HFSPlusExtentRecord extrec
;
3454 HFSPlusExtentKey
*extkeyptr
;
3462 forktype
= rsrcfork
? 0xFF : 0;
3463 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
3466 bzero(iterator
, sizeof(*iterator
));
3467 extkeyptr
= (HFSPlusExtentKey
*)&iterator
->key
;
3468 extkeyptr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
3469 extkeyptr
->forkType
= forktype
;
3470 extkeyptr
->fileID
= fileID
;
3471 extkeyptr
->startBlock
= catblks
;
3473 btdata
.bufferAddress
= &extrec
;
3474 btdata
.itemSize
= sizeof(extrec
);
3475 btdata
.itemCount
= 1;
3477 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
3479 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
3480 while (error
== 0) {
3481 /* Stop when we encounter a different file. */
3482 if ((extkeyptr
->fileID
!= fileID
) ||
3483 (extkeyptr
->forkType
!= forktype
)) {
3487 * Check if the file overlaps target space.
3489 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3490 if (extrec
[i
].blockCount
== 0) {
3493 block
= extrec
[i
].startBlock
+ extrec
[i
].blockCount
;
3494 if (block
>= startblk
) {
3499 /* Look for more records. */
3500 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3503 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3504 return (overlapped
);
3509 * Calculate the progress of a file system resize operation.
3513 hfs_resize_progress(struct hfsmount
*hfsmp
, u_int32_t
*progress
)
3515 if ((hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) == 0) {
3519 if (hfsmp
->hfs_resize_totalfiles
> 0)
3520 *progress
= (hfsmp
->hfs_resize_filesmoved
* 100) / hfsmp
->hfs_resize_totalfiles
;
3529 * Get file system attributes.
3532 hfs_vfs_getattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
3534 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
3535 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
3538 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)hfsmp
->vcbNxtCNID
;
3540 VFSATTR_RETURN(fsap
, f_objcount
, (uint64_t)hfsmp
->vcbFilCnt
+ (uint64_t)hfsmp
->vcbDirCnt
);
3541 VFSATTR_RETURN(fsap
, f_filecount
, (uint64_t)hfsmp
->vcbFilCnt
);
3542 VFSATTR_RETURN(fsap
, f_dircount
, (uint64_t)hfsmp
->vcbDirCnt
);
3543 VFSATTR_RETURN(fsap
, f_maxobjcount
, (uint64_t)0xFFFFFFFF);
3544 VFSATTR_RETURN(fsap
, f_iosize
, (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
));
3545 VFSATTR_RETURN(fsap
, f_blocks
, (uint64_t)hfsmp
->totalBlocks
);
3546 VFSATTR_RETURN(fsap
, f_bfree
, (uint64_t)hfs_freeblks(hfsmp
, 0));
3547 VFSATTR_RETURN(fsap
, f_bavail
, (uint64_t)hfs_freeblks(hfsmp
, 1));
3548 VFSATTR_RETURN(fsap
, f_bsize
, (uint32_t)vcb
->blockSize
);
3549 /* XXX needs clarification */
3550 VFSATTR_RETURN(fsap
, f_bused
, hfsmp
->totalBlocks
- hfs_freeblks(hfsmp
, 1));
3551 /* Maximum files is constrained by total blocks. */
3552 VFSATTR_RETURN(fsap
, f_files
, (uint64_t)(hfsmp
->totalBlocks
- 2));
3553 VFSATTR_RETURN(fsap
, f_ffree
, MIN((uint64_t)freeCNIDs
, (uint64_t)hfs_freeblks(hfsmp
, 1)));
3555 fsap
->f_fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
3556 fsap
->f_fsid
.val
[1] = vfs_typenum(mp
);
3557 VFSATTR_SET_SUPPORTED(fsap
, f_fsid
);
3559 VFSATTR_RETURN(fsap
, f_signature
, vcb
->vcbSigWord
);
3560 VFSATTR_RETURN(fsap
, f_carbon_fsid
, 0);
3562 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
3563 vol_capabilities_attr_t
*cap
;
3565 cap
= &fsap
->f_capabilities
;
3567 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
3568 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
3569 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3570 VOL_CAP_FMT_CASE_PRESERVING
|
3571 VOL_CAP_FMT_FAST_STATFS
;
3573 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
3574 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3575 VOL_CAP_FMT_SYMBOLICLINKS
|
3576 VOL_CAP_FMT_HARDLINKS
|
3577 VOL_CAP_FMT_JOURNAL
|
3578 (hfsmp
->jnl
? VOL_CAP_FMT_JOURNAL_ACTIVE
: 0) |
3579 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
? VOL_CAP_FMT_CASE_SENSITIVE
: 0) |
3580 VOL_CAP_FMT_CASE_PRESERVING
|
3581 VOL_CAP_FMT_FAST_STATFS
|
3582 VOL_CAP_FMT_2TB_FILESIZE
;
3584 cap
->capabilities
[VOL_CAPABILITIES_INTERFACES
] =
3585 VOL_CAP_INT_SEARCHFS
|
3586 VOL_CAP_INT_ATTRLIST
|
3587 VOL_CAP_INT_NFSEXPORT
|
3588 VOL_CAP_INT_READDIRATTR
|
3589 VOL_CAP_INT_EXCHANGEDATA
|
3590 VOL_CAP_INT_ALLOCATE
|
3591 VOL_CAP_INT_VOL_RENAME
|
3592 VOL_CAP_INT_ADVLOCK
|
3594 cap
->capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
3595 cap
->capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
3597 cap
->valid
[VOL_CAPABILITIES_FORMAT
] =
3598 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3599 VOL_CAP_FMT_SYMBOLICLINKS
|
3600 VOL_CAP_FMT_HARDLINKS
|
3601 VOL_CAP_FMT_JOURNAL
|
3602 VOL_CAP_FMT_JOURNAL_ACTIVE
|
3603 VOL_CAP_FMT_NO_ROOT_TIMES
|
3604 VOL_CAP_FMT_SPARSE_FILES
|
3605 VOL_CAP_FMT_ZERO_RUNS
|
3606 VOL_CAP_FMT_CASE_SENSITIVE
|
3607 VOL_CAP_FMT_CASE_PRESERVING
|
3608 VOL_CAP_FMT_FAST_STATFS
|
3609 VOL_CAP_FMT_2TB_FILESIZE
;
3610 cap
->valid
[VOL_CAPABILITIES_INTERFACES
] =
3611 VOL_CAP_INT_SEARCHFS
|
3612 VOL_CAP_INT_ATTRLIST
|
3613 VOL_CAP_INT_NFSEXPORT
|
3614 VOL_CAP_INT_READDIRATTR
|
3615 VOL_CAP_INT_EXCHANGEDATA
|
3616 VOL_CAP_INT_COPYFILE
|
3617 VOL_CAP_INT_ALLOCATE
|
3618 VOL_CAP_INT_VOL_RENAME
|
3619 VOL_CAP_INT_ADVLOCK
|
3621 cap
->valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
3622 cap
->valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
3623 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
3625 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
3626 vol_attributes_attr_t
*attrp
= &fsap
->f_attributes
;
3628 attrp
->validattr
.commonattr
= ATTR_CMN_VALIDMASK
;
3629 attrp
->validattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
3630 attrp
->validattr
.dirattr
= ATTR_DIR_VALIDMASK
;
3631 attrp
->validattr
.fileattr
= ATTR_FILE_VALIDMASK
;
3632 attrp
->validattr
.forkattr
= 0;
3634 attrp
->nativeattr
.commonattr
= ATTR_CMN_VALIDMASK
;
3635 attrp
->nativeattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
3636 attrp
->nativeattr
.dirattr
= ATTR_DIR_VALIDMASK
;
3637 attrp
->nativeattr
.fileattr
= ATTR_FILE_VALIDMASK
;
3638 attrp
->nativeattr
.forkattr
= 0;
3639 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
3641 fsap
->f_create_time
.tv_sec
= hfsmp
->vcbCrDate
;
3642 fsap
->f_create_time
.tv_nsec
= 0;
3643 VFSATTR_SET_SUPPORTED(fsap
, f_create_time
);
3644 fsap
->f_modify_time
.tv_sec
= hfsmp
->vcbLsMod
;
3645 fsap
->f_modify_time
.tv_nsec
= 0;
3646 VFSATTR_SET_SUPPORTED(fsap
, f_modify_time
);
3648 fsap
->f_backup_time
.tv_sec
= hfsmp
->vcbVolBkUp
;
3649 fsap
->f_backup_time
.tv_nsec
= 0;
3650 VFSATTR_SET_SUPPORTED(fsap
, f_backup_time
);
3651 if (VFSATTR_IS_ACTIVE(fsap
, f_fssubtype
)) {
3652 uint16_t subtype
= 0;
3655 * Subtypes (flavors) for HFS
3656 * 0: Mac OS Extended
3657 * 1: Mac OS Extended (Journaled)
3658 * 2: Mac OS Extended (Case Sensitive)
3659 * 3: Mac OS Extended (Case Sensitive, Journaled)
3661 * 128: Mac OS Standard
3664 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
3665 subtype
= HFS_SUBTYPE_STANDARDHFS
;
3666 } else /* HFS Plus */ {
3668 subtype
|= HFS_SUBTYPE_JOURNALED
;
3669 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
3670 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
3672 fsap
->f_fssubtype
= subtype
;
3673 VFSATTR_SET_SUPPORTED(fsap
, f_fssubtype
);
3676 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
3677 strncpy(fsap
->f_vol_name
, hfsmp
->vcbVN
, MAXPATHLEN
);
3678 fsap
->f_vol_name
[MAXPATHLEN
- 1] = 0;
3679 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
3685 * Perform a volume rename. Requires the FS' root vp.
3688 hfs_rename_volume(struct vnode
*vp
, const char *name
, proc_t p
)
3690 ExtendedVCB
*vcb
= VTOVCB(vp
);
3691 struct cnode
*cp
= VTOC(vp
);
3692 struct hfsmount
*hfsmp
= VTOHFS(vp
);
3693 struct cat_desc to_desc
;
3694 struct cat_desc todir_desc
;
3695 struct cat_desc new_desc
;
3696 cat_cookie_t cookie
;
3701 * Ignore attempts to rename a volume to a zero-length name.
3706 bzero(&to_desc
, sizeof(to_desc
));
3707 bzero(&todir_desc
, sizeof(todir_desc
));
3708 bzero(&new_desc
, sizeof(new_desc
));
3709 bzero(&cookie
, sizeof(cookie
));
3711 todir_desc
.cd_parentcnid
= kHFSRootParentID
;
3712 todir_desc
.cd_cnid
= kHFSRootFolderID
;
3713 todir_desc
.cd_flags
= CD_ISDIR
;
3715 to_desc
.cd_nameptr
= name
;
3716 to_desc
.cd_namelen
= strlen(name
);
3717 to_desc
.cd_parentcnid
= kHFSRootParentID
;
3718 to_desc
.cd_cnid
= cp
->c_cnid
;
3719 to_desc
.cd_flags
= CD_ISDIR
;
3721 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)) == 0) {
3722 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
3723 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, &cookie
, p
)) == 0) {
3724 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3726 error
= cat_rename(hfsmp
, &cp
->c_desc
, &todir_desc
, &to_desc
, &new_desc
);
3729 * If successful, update the name in the VCB, ensure it's terminated.
3732 strncpy(vcb
->vcbVN
, name
, sizeof(vcb
->vcbVN
));
3733 vcb
->vcbVN
[sizeof(vcb
->vcbVN
) - 1] = 0;
3736 hfs_systemfile_unlock(hfsmp
, lockflags
);
3737 cat_postflight(hfsmp
, &cookie
, p
);
3740 vcb
->vcbFlags
|= 0xFF00;
3741 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
3743 hfs_end_transaction(hfsmp
);
3746 /* Release old allocated name buffer */
3747 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
3748 char *name
= cp
->c_desc
.cd_nameptr
;
3750 cp
->c_desc
.cd_nameptr
= 0;
3751 cp
->c_desc
.cd_namelen
= 0;
3752 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
3753 vfs_removename(name
);
3755 /* Update cnode's catalog descriptor */
3756 replace_desc(cp
, &new_desc
);
3757 vcb
->volumeNameEncodingHint
= new_desc
.cd_encoding
;
3758 cp
->c_touch_chgtime
= TRUE
;
3768 * Get file system attributes.
3771 hfs_vfs_setattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
3773 kauth_cred_t cred
= vfs_context_ucred(context
);
3777 * Must be superuser or owner of filesystem to change volume attributes
3779 if (!kauth_cred_issuser(cred
) && (kauth_cred_getuid(cred
) != vfs_statfs(mp
)->f_owner
))
3782 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
3785 error
= hfs_vfs_root(mp
, &root_vp
, context
);
3789 error
= hfs_rename_volume(root_vp
, fsap
->f_vol_name
, vfs_context_proc(context
));
3790 (void) vnode_put(root_vp
);
3794 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
3803 * hfs vfs operations.
3805 struct vfsops hfs_vfsops
= {
3811 hfs_vfs_getattr
, /* was hfs_statfs */