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
);
153 * Called by vfs_mountroot when mounting HFS Plus as root.
158 hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
)
160 struct hfsmount
*hfsmp
;
162 struct vfsstatfs
*vfsp
;
165 if ((error
= hfs_mountfs(rvp
, mp
, NULL
, context
)))
169 hfsmp
= VFSTOHFS(mp
);
171 hfsmp
->hfs_uid
= UNKNOWNUID
;
172 hfsmp
->hfs_gid
= UNKNOWNGID
;
173 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
174 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
176 /* Establish the free block reserve. */
177 vcb
= HFSTOVCB(hfsmp
);
178 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
179 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
181 vfsp
= vfs_statfs(mp
);
182 (void)hfs_statfs(mp
, vfsp
, NULL
);
195 hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
197 struct proc
*p
= vfs_context_proc(context
);
198 struct hfsmount
*hfsmp
= NULL
;
199 struct hfs_mount_args args
;
203 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
)))) {
206 cmdflags
= (uint32_t)vfs_flags(mp
) & MNT_CMDFLAGS
;
207 if (cmdflags
& MNT_UPDATE
) {
208 hfsmp
= VFSTOHFS(mp
);
210 /* Reload incore data after an fsck. */
211 if (cmdflags
& MNT_RELOAD
) {
212 if (vfs_isrdonly(mp
))
213 return hfs_reload(mp
, vfs_context_ucred(context
), p
);
218 /* Change to a read-only file system. */
219 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
223 /* use VFS_SYNC to push out System (btree) files */
224 retval
= VFS_SYNC(mp
, MNT_WAIT
, context
);
225 if (retval
&& ((cmdflags
& MNT_FORCE
) == 0))
229 if (cmdflags
& MNT_FORCE
)
232 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
234 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
235 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
237 /* also get the volume bitmap blocks */
239 if (vnode_mount(hfsmp
->hfs_devvp
) == mp
) {
240 retval
= hfs_fsync(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, p
);
242 vnode_get(hfsmp
->hfs_devvp
);
243 retval
= VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
244 vnode_put(hfsmp
->hfs_devvp
);
248 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
252 hfs_global_exclusive_lock_acquire(hfsmp
);
254 journal_close(hfsmp
->jnl
);
257 // Note: we explicitly don't want to shutdown
258 // access to the jvp because we may need
259 // it later if we go back to being read-write.
261 hfs_global_exclusive_lock_release(hfsmp
);
265 /* Change to a writable file system. */
266 if (vfs_iswriteupgrade(mp
)) {
267 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
268 if (retval
!= E_NONE
)
271 // If the journal was shut-down previously because we were
272 // asked to be read-only, let's start it back up again now
274 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
275 && hfsmp
->jnl
== NULL
276 && hfsmp
->jvp
!= NULL
) {
279 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
280 jflags
= JOURNAL_RESET
;
285 hfs_global_exclusive_lock_acquire(hfsmp
);
287 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
288 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
291 hfsmp
->hfs_phys_block_size
,
294 hfs_sync_metadata
, hfsmp
->hfs_mp
);
296 hfs_global_exclusive_lock_release(hfsmp
);
298 if (hfsmp
->jnl
== NULL
) {
302 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
307 /* Only clear HFS_READ_ONLY after a successfull write */
308 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
310 if (!(hfsmp
->hfs_flags
& (HFS_READ_ONLY
& HFS_STANDARD
))) {
311 /* setup private/hidden directory for unlinked files */
312 FindMetaDataDirectory(HFSTOVCB(hfsmp
));
313 hfs_remove_orphans(hfsmp
);
316 * Allow hot file clustering if conditions allow.
318 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
319 (void) hfs_recording_init(hfsmp
);
324 /* Update file system parameters. */
325 retval
= hfs_changefs(mp
, &args
);
327 } else /* not an update request */ {
329 /* Set the mount flag to indicate that we support volfs */
330 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_DOVOLFS
));
332 retval
= hfs_mountfs(devvp
, mp
, &args
, context
);
336 (void)hfs_statfs(mp
, vfs_statfs(mp
), context
);
342 struct hfs_changefs_cargs
{
343 struct hfsmount
*hfsmp
;
350 hfs_changefs_callback(struct vnode
*vp
, void *cargs
)
354 struct cat_desc cndesc
;
355 struct cat_attr cnattr
;
356 struct hfs_changefs_cargs
*args
;
358 args
= (struct hfs_changefs_cargs
*)cargs
;
361 vcb
= HFSTOVCB(args
->hfsmp
);
363 if (cat_lookup(args
->hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
, NULL
)) {
365 * If we couldn't find this guy skip to the next one
370 return (VNODE_RETURNED
);
373 * Get the real uid/gid and perm mask from disk.
375 if (args
->permswitch
|| args
->permfix
) {
376 cp
->c_uid
= cnattr
.ca_uid
;
377 cp
->c_gid
= cnattr
.ca_gid
;
378 cp
->c_mode
= cnattr
.ca_mode
;
381 * If we're switching name converters then...
382 * Remove the existing entry from the namei cache.
383 * Update name to one based on new encoder.
387 replace_desc(cp
, &cndesc
);
389 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
390 strncpy(vcb
->vcbVN
, cp
->c_desc
.cd_nameptr
, NAME_MAX
);
391 cp
->c_desc
.cd_encoding
= args
->hfsmp
->hfs_encoding
;
394 cat_releasedesc(&cndesc
);
396 return (VNODE_RETURNED
);
399 /* Change fs mount parameters */
401 hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
)
404 int namefix
, permfix
, permswitch
;
405 struct hfsmount
*hfsmp
;
407 hfs_to_unicode_func_t get_unicode_func
;
408 unicode_to_hfs_func_t get_hfsname_func
;
409 u_long old_encoding
= 0;
410 struct hfs_changefs_cargs cargs
;
411 uint32_t mount_flags
;
413 hfsmp
= VFSTOHFS(mp
);
414 vcb
= HFSTOVCB(hfsmp
);
415 mount_flags
= (unsigned int)vfs_flags(mp
);
417 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
418 ((mount_flags
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
419 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
420 (mount_flags
& MNT_UNKNOWNPERMISSIONS
)));
422 /* The root filesystem must operate with actual permissions: */
423 if (permswitch
&& (mount_flags
& MNT_ROOTFS
) && (mount_flags
& MNT_UNKNOWNPERMISSIONS
)) {
424 vfs_clearflags(mp
, (uint64_t)((unsigned int)MNT_UNKNOWNPERMISSIONS
)); /* Just say "No". */
427 if (mount_flags
& MNT_UNKNOWNPERMISSIONS
)
428 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
430 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
432 namefix
= permfix
= 0;
434 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
435 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
436 gTimeZone
= args
->hfs_timezone
;
439 /* Change the default uid, gid and/or mask */
440 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
441 hfsmp
->hfs_uid
= args
->hfs_uid
;
442 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
445 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
446 hfsmp
->hfs_gid
= args
->hfs_gid
;
447 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
450 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
451 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
452 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
453 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
454 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
455 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
456 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
461 /* Change the hfs encoding value (hfs only) */
462 if ((vcb
->vcbSigWord
== kHFSSigWord
) &&
463 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
464 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
466 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
471 * Connect the new hfs_get_unicode converter but leave
472 * the old hfs_get_hfsname converter in place so that
473 * we can lookup existing vnodes to get their correctly
476 * When we're all finished, we can then connect the new
477 * hfs_get_hfsname converter and release our interest
478 * in the old converters.
480 hfsmp
->hfs_get_unicode
= get_unicode_func
;
481 old_encoding
= hfsmp
->hfs_encoding
;
482 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
486 if (!(namefix
|| permfix
|| permswitch
))
489 /* XXX 3762912 hack to support HFS filesystem 'owner' */
492 hfsmp
->hfs_uid
== UNKNOWNUID
? KAUTH_UID_NONE
: hfsmp
->hfs_uid
,
493 hfsmp
->hfs_gid
== UNKNOWNGID
? KAUTH_GID_NONE
: hfsmp
->hfs_gid
);
496 * For each active vnode fix things that changed
498 * Note that we can visit a vnode more than once
499 * and we can race with fsync.
501 * hfs_changefs_callback will be called for each vnode
502 * hung off of this mount point
504 * properly referenced and unreferenced around the callback
507 cargs
.namefix
= namefix
;
508 cargs
.permfix
= permfix
;
509 cargs
.permswitch
= permswitch
;
511 vnode_iterate(mp
, 0, hfs_changefs_callback
, (void *)&cargs
);
514 * If we're switching name converters we can now
515 * connect the new hfs_get_hfsname converter and
516 * release our interest in the old converters.
519 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
520 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
521 (void) hfs_relconverter(old_encoding
);
528 struct hfs_reload_cargs
{
529 struct hfsmount
*hfsmp
;
536 hfs_reload_callback(struct vnode
*vp
, void *cargs
)
539 struct hfs_reload_cargs
*args
;
541 args
= (struct hfs_reload_cargs
*)cargs
;
543 * flush all the buffers associated with this node
545 (void) buf_invalidateblks(vp
, 0, 0, 0);
549 * Remove any directory hints
552 hfs_reldirhints(cp
, 0);
555 * Re-read cnode data for all active vnodes (non-metadata files).
557 if (!vnode_issystem(vp
) && !VNODE_IS_RSRC(vp
)) {
558 struct cat_fork
*datafork
;
559 struct cat_desc desc
;
561 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
563 /* lookup by fileID since name could have changed */
564 if ((args
->error
= cat_idlookup(args
->hfsmp
, cp
->c_fileid
, &desc
, &cp
->c_attr
, datafork
)))
565 return (VNODE_RETURNED_DONE
);
567 /* update cnode's catalog descriptor */
568 (void) replace_desc(cp
, &desc
);
570 return (VNODE_RETURNED
);
574 * Reload all incore data for a filesystem (used after running fsck on
575 * the root filesystem and finding things to fix). The filesystem must
576 * be mounted read-only.
578 * Things to do to update the mount:
579 * invalidate all cached meta-data.
580 * invalidate all inactive vnodes.
581 * invalidate all cached file data.
582 * re-read volume header from disk.
583 * re-load meta-file info (extents, file size).
584 * re-load B-tree header data.
585 * re-read cnode data for all active vnodes.
588 hfs_reload(struct mount
*mountp
, kauth_cred_t cred
, struct proc
*p
)
590 register struct vnode
*devvp
;
594 struct hfsmount
*hfsmp
;
595 struct HFSPlusVolumeHeader
*vhp
;
597 struct filefork
*forkp
;
598 struct cat_desc cndesc
;
599 struct hfs_reload_cargs args
;
601 hfsmp
= VFSTOHFS(mountp
);
602 vcb
= HFSTOVCB(hfsmp
);
604 if (vcb
->vcbSigWord
== kHFSSigWord
)
605 return (EINVAL
); /* rooting from HFS is not supported! */
608 * Invalidate all cached meta-data.
610 devvp
= hfsmp
->hfs_devvp
;
611 if (buf_invalidateblks(devvp
, 0, 0, 0))
612 panic("hfs_reload: dirty1");
619 * hfs_reload_callback will be called for each vnode
620 * hung off of this mount point that can't be recycled...
621 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
622 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
623 * properly referenced and unreferenced around the callback
625 vnode_iterate(mountp
, VNODE_RELOAD
| VNODE_WAIT
, hfs_reload_callback
, (void *)&args
);
631 * Re-read VolumeHeader from disk.
633 sectorsize
= hfsmp
->hfs_phys_block_size
;
635 error
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
636 (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
)),
637 sectorsize
, NOCRED
, &bp
);
644 vhp
= (HFSPlusVolumeHeader
*) (buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
646 /* Do a quick sanity check */
647 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
648 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
649 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
650 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
651 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
656 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
657 vcb
->vcbAtrb
= SWAP_BE32 (vhp
->attributes
);
658 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
659 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
660 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
661 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
662 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
663 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
664 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
665 vcb
->nextAllocation
= SWAP_BE32 (vhp
->nextAllocation
);
666 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
667 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
668 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
669 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
670 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
673 * Re-load meta-file vnode data (extent info, file size, etc).
675 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
676 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
677 forkp
->ff_extents
[i
].startBlock
=
678 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
679 forkp
->ff_extents
[i
].blockCount
=
680 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
682 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
683 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
684 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
687 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
688 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
689 forkp
->ff_extents
[i
].startBlock
=
690 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
691 forkp
->ff_extents
[i
].blockCount
=
692 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
694 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
695 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
696 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
698 if (hfsmp
->hfs_attribute_vp
) {
699 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
700 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
701 forkp
->ff_extents
[i
].startBlock
=
702 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
703 forkp
->ff_extents
[i
].blockCount
=
704 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
706 forkp
->ff_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
707 forkp
->ff_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
708 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
711 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
712 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
713 forkp
->ff_extents
[i
].startBlock
=
714 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
715 forkp
->ff_extents
[i
].blockCount
=
716 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
718 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
719 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
720 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
726 * Re-load B-tree header data
728 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
729 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
732 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
733 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
736 if (hfsmp
->hfs_attribute_vp
) {
737 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
738 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
742 /* Reload the volume name */
743 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, NULL
, NULL
)))
745 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
746 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
747 cat_releasedesc(&cndesc
);
749 /* Re-establish private/hidden directory for unlinked files */
750 FindMetaDataDirectory(vcb
);
752 /* In case any volume information changed to trigger a notification */
753 hfs_generate_volume_notifications(hfsmp
);
760 * Common code for mount and mountroot
763 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
,
764 vfs_context_t context
)
766 struct proc
*p
= vfs_context_proc(context
);
768 struct hfsmount
*hfsmp
;
771 HFSMasterDirectoryBlock
*mdbp
;
779 u_int32_t minblksize
;
780 u_int32_t iswritable
;
781 daddr64_t mdb_offset
;
783 ronly
= vfs_isrdonly(mp
);
784 dev
= vnode_specrdev(devvp
);
785 cred
= p
? vfs_context_ucred(context
) : NOCRED
;
791 minblksize
= kHFSBlockSize
;
793 /* Advisory locking should be handled at the VFS layer */
794 vfs_setlocklocal(mp
);
796 /* Get the real physical block size. */
797 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, context
)) {
801 /* Switch to 512 byte sectors (temporarily) */
803 u_int32_t size512
= 512;
805 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, context
)) {
810 /* Get the number of 512 byte physical blocks. */
811 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
815 /* Compute an accurate disk size (i.e. within 512 bytes) */
816 disksize
= (u_int64_t
)blkcnt
* (u_int64_t
)512;
819 * On Tiger it is not necessary to switch the device
820 * block size to be 4k if there are more than 31-bits
821 * worth of blocks but to insure compatibility with
822 * pre-Tiger systems we have to do it.
824 if (blkcnt
> (u_int64_t
)0x000000007fffffff) {
825 minblksize
= blksize
= 4096;
828 /* Now switch to our prefered physical block size. */
830 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
834 /* Get the count of physical blocks. */
835 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
842 * minblksize is the minimum physical block size
843 * blksize has our prefered physical block size
844 * blkcnt has the total number of physical blocks
847 mdb_offset
= (daddr64_t
)HFS_PRI_SECTOR(blksize
);
848 if ((retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
))) {
851 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
852 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
856 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
857 bzero(hfsmp
, sizeof(struct hfsmount
));
860 * Init the volume information structure
863 lck_mtx_init(&hfsmp
->hfs_mutex
, hfs_mutex_group
, hfs_lock_attr
);
864 lck_mtx_init(&hfsmp
->hfc_mutex
, hfs_mutex_group
, hfs_lock_attr
);
865 lck_rw_init(&hfsmp
->hfs_global_lock
, hfs_rwlock_group
, hfs_lock_attr
);
866 lck_rw_init(&hfsmp
->hfs_insync
, hfs_rwlock_group
, hfs_lock_attr
);
868 vfs_setfsprivate(mp
, hfsmp
);
869 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
870 hfsmp
->hfs_raw_dev
= vnode_specrdev(devvp
);
871 hfsmp
->hfs_devvp
= devvp
;
872 hfsmp
->hfs_phys_block_size
= blksize
;
873 hfsmp
->hfs_phys_block_count
= blkcnt
;
874 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
876 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
877 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)
878 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
879 for (i
= 0; i
< MAXQUOTAS
; i
++)
880 dqfileinit(&hfsmp
->hfs_qfiles
[i
]);
883 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
884 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
885 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
886 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
887 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
888 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
889 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
890 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
891 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
893 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
896 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
897 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
899 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
902 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
903 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
) {
904 hfsmp
->hfs_uid
= UNKNOWNUID
;
905 hfsmp
->hfs_gid
= UNKNOWNGID
;
906 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
907 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
908 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
912 /* Find out if disk media is writable. */
913 if (VNOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, context
) == 0) {
915 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
917 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
920 // record the current time at which we're mounting this volume
924 hfsmp
->hfs_mount_time
= tv
.tv_sec
;
927 /* Mount a standard HFS disk */
928 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
929 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
930 if ((vfs_flags(mp
) & MNT_ROOTFS
)) {
931 retval
= EINVAL
; /* Cannot root from HFS standard disks */
934 /* HFS disks can only use 512 byte physical blocks */
935 if (blksize
> kHFSBlockSize
) {
936 blksize
= kHFSBlockSize
;
937 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
941 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
945 hfsmp
->hfs_phys_block_size
= blksize
;
946 hfsmp
->hfs_phys_block_count
= blkcnt
;
949 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
950 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
952 /* establish the timezone */
953 gTimeZone
= args
->hfs_timezone
;
956 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
957 &hfsmp
->hfs_get_hfsname
);
961 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
963 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
965 } else /* Mount an HFS Plus disk */ {
966 HFSPlusVolumeHeader
*vhp
;
967 off_t embeddedOffset
;
970 /* Get the embedded Volume Header */
971 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
972 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
973 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
974 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
977 * If the embedded volume doesn't start on a block
978 * boundary, then switch the device to a 512-byte
979 * block size so everything will line up on a block
982 if ((embeddedOffset
% blksize
) != 0) {
983 printf("HFS Mount: embedded volume offset not"
984 " a multiple of physical block size (%d);"
985 " switching to 512\n", blksize
);
987 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
988 (caddr_t
)&blksize
, FWRITE
, context
)) {
992 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
993 (caddr_t
)&blkcnt
, 0, context
)) {
997 /* Note: relative block count adjustment */
998 hfsmp
->hfs_phys_block_count
*=
999 hfsmp
->hfs_phys_block_size
/ blksize
;
1000 hfsmp
->hfs_phys_block_size
= blksize
;
1003 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1004 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1006 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1008 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1009 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1012 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1015 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1017 } else /* pure HFS+ */ {
1019 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1026 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) && args
->journal_disable
) {
1031 // We only initialize the journal here if the last person
1032 // to mount this volume was journaling aware. Otherwise
1033 // we delay journal initialization until later at the end
1034 // of hfs_MountHFSPlusVolume() because the last person who
1035 // mounted it could have messed things up behind our back
1036 // (so we need to go find the .journal file, make sure it's
1037 // the right size, re-sync up if it was moved, etc).
1039 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1040 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1043 // if we're able to init the journal, mark the mount
1044 // point as journaled.
1046 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1047 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
1049 // if the journal failed to open, then set the lastMountedVersion
1050 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1051 // of just bailing out because the volume is journaled.
1053 HFSPlusVolumeHeader
*jvhp
;
1055 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1057 if (mdb_offset
== 0) {
1058 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1062 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1064 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1066 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1067 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1068 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1076 // clear this so the error exit path won't try to use it
1081 // if this isn't the root device just bail out.
1082 // If it is the root device we just continue on
1083 // in the hopes that fsck_hfs will be able to
1084 // fix any damage that exists on the volume.
1085 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1093 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1095 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1097 * If the backend didn't like our physical blocksize
1098 * then retry with physical blocksize of 512.
1100 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1101 printf("HFS Mount: could not use physical block size "
1102 "(%d) switching to 512\n", blksize
);
1104 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
1108 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
1112 devvp
->v_specsize
= blksize
;
1113 /* Note: relative block count adjustment (in case this is an embedded volume). */
1114 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1115 hfsmp
->hfs_phys_block_size
= blksize
;
1118 // close and re-open this with the new block size
1119 journal_close(hfsmp
->jnl
);
1121 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1122 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
1124 // if the journal failed to open, then set the lastMountedVersion
1125 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1126 // of just bailing out because the volume is journaled.
1128 HFSPlusVolumeHeader
*jvhp
;
1130 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1132 if (mdb_offset
== 0) {
1133 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1137 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1139 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1141 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1142 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1143 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1151 // clear this so the error exit path won't try to use it
1156 // if this isn't the root device just bail out.
1157 // If it is the root device we just continue on
1158 // in the hopes that fsck_hfs will be able to
1159 // fix any damage that exists on the volume.
1160 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1167 /* Try again with a smaller block size... */
1168 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1171 (void) hfs_relconverter(0);
1174 // save off a snapshot of the mtime from the previous mount
1176 hfsmp
->hfs_last_mounted_mtime
= hfsmp
->hfs_mtime
;
1182 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (long)dev
;
1183 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
1184 vfs_setmaxsymlen(mp
, 0);
1185 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
1186 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSNATIVEXATTR
;
1190 * Set the free space warning levels for a non-root volume:
1192 * Set the lower freespace limit (the level that will trigger a warning)
1193 * to 5% of the volume size or 250MB, whichever is less, and the desired
1194 * level (which will cancel the alert request) to 1/2 above that limit.
1195 * Start looking for free space to drop below this level and generate a
1196 * warning immediately if needed:
1198 hfsmp
->hfs_freespace_notify_warninglimit
=
1199 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1200 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1201 hfsmp
->hfs_freespace_notify_desiredlevel
=
1202 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1203 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1206 * Set the free space warning levels for the root volume:
1208 * Set the lower freespace limit (the level that will trigger a warning)
1209 * to 1% of the volume size or 50MB, whichever is less, and the desired
1210 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1212 hfsmp
->hfs_freespace_notify_warninglimit
=
1213 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1214 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1215 hfsmp
->hfs_freespace_notify_desiredlevel
=
1216 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1217 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1221 * Start looking for free space to drop below this level and generate a
1222 * warning immediately if needed:
1224 hfsmp
->hfs_notification_conditions
= 0;
1225 hfs_generate_volume_notifications(hfsmp
);
1228 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1239 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1240 (void)VNOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, context
);
1244 FREE(hfsmp
, M_HFSMNT
);
1245 vfs_setfsprivate(mp
, NULL
);
1252 * Make a filesystem operational.
1253 * Nothing to do at the moment.
1257 hfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
1264 * unmount system call
1267 hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
)
1269 struct proc
*p
= vfs_context_proc(context
);
1270 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1271 int retval
= E_NONE
;
1278 if (mntflags
& MNT_FORCE
) {
1279 flags
|= FORCECLOSE
;
1283 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1286 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1287 (void) hfs_recording_suspend(hfsmp
);
1290 * Flush out the b-trees, volume bitmap and Volume Header
1292 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1293 hfs_start_transaction(hfsmp
);
1296 if (hfsmp
->hfs_attribute_vp
) {
1297 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), HFS_EXCLUSIVE_LOCK
);
1298 retval
= hfs_fsync(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, p
);
1299 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
1300 if (retval
&& !force
)
1304 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), HFS_EXCLUSIVE_LOCK
);
1305 retval
= hfs_fsync(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, p
);
1306 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
1307 if (retval
&& !force
)
1310 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
1311 retval
= hfs_fsync(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, p
);
1312 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
1313 if (retval
&& !force
)
1316 if (hfsmp
->hfs_allocation_vp
) {
1317 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
1318 retval
= hfs_fsync(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, p
);
1319 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
1320 if (retval
&& !force
)
1324 if (hfsmp
->hfc_filevp
&& vnode_issystem(hfsmp
->hfc_filevp
)) {
1325 retval
= hfs_fsync(hfsmp
->hfc_filevp
, MNT_WAIT
, 0, p
);
1326 if (retval
&& !force
)
1330 /* See if this volume is damaged, is so do not unmount cleanly */
1331 if (HFSTOVCB(hfsmp
)->vcbFlags
& kHFS_DamagedVolume
) {
1332 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1334 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1337 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1339 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1341 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1343 goto err_exit
; /* could not flush everything */
1346 hfs_end_transaction(hfsmp
);
1351 journal_flush(hfsmp
->jnl
);
1355 * Invalidate our caches and release metadata vnodes
1357 (void) hfsUnmount(hfsmp
, p
);
1360 * Last chance to dump unreferenced system files.
1362 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1364 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1365 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1369 journal_close(hfsmp
->jnl
);
1373 VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
1375 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1376 retval
= VNOP_CLOSE(hfsmp
->jvp
,
1377 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1379 vnode_put(hfsmp
->jvp
);
1384 #ifdef HFS_SPARSE_DEV
1385 /* Drop our reference on the backing fs (if any). */
1386 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1387 struct vnode
* tmpvp
;
1389 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1390 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1391 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1394 #endif /* HFS_SPARSE_DEV */
1395 lck_mtx_destroy(&hfsmp
->hfc_mutex
, hfs_mutex_group
);
1396 FREE(hfsmp
, M_HFSMNT
);
1402 hfs_end_transaction(hfsmp
);
1409 * Return the root of a filesystem.
1412 hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1414 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)kHFSRootFolderID
, vpp
, 1);
1419 * Do operations associated with quotas
1422 hfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t context
)
1424 struct proc
*p
= vfs_context_proc(context
);
1425 int cmd
, type
, error
;
1431 uid
= vfs_context_ucred(context
)->cr_ruid
;
1432 cmd
= cmds
>> SUBCMDSHIFT
;
1439 if (uid
== vfs_context_ucred(context
)->cr_ruid
)
1443 if ( (error
= vfs_context_suser(context
)) )
1447 type
= cmds
& SUBCMDMASK
;
1448 if ((u_int
)type
>= MAXQUOTAS
)
1450 if (vfs_busy(mp
, LK_NOWAIT
))
1456 error
= hfs_quotaon(p
, mp
, type
, datap
);
1460 error
= hfs_quotaoff(p
, mp
, type
);
1464 error
= hfs_setquota(mp
, uid
, type
, datap
);
1468 error
= hfs_setuse(mp
, uid
, type
, datap
);
1472 error
= hfs_getquota(mp
, uid
, type
, datap
);
1476 error
= hfs_qsync(mp
);
1480 error
= hfs_quotastat(mp
, type
, datap
);
1493 /* Subtype is composite of bits */
1494 #define HFS_SUBTYPE_JOURNALED 0x01
1495 #define HFS_SUBTYPE_CASESENSITIVE 0x02
1496 /* bits 2 - 6 reserved */
1497 #define HFS_SUBTYPE_STANDARDHFS 0x80
1500 * Get file system statistics.
1503 hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
1505 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1506 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1508 uint16_t subtype
= 0;
1510 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1512 sbp
->f_bsize
= (uint32_t)vcb
->blockSize
;
1513 sbp
->f_iosize
= (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
);
1514 sbp
->f_blocks
= (uint64_t)((unsigned long)vcb
->totalBlocks
);
1515 sbp
->f_bfree
= (uint64_t)((unsigned long )hfs_freeblks(hfsmp
, 0));
1516 sbp
->f_bavail
= (uint64_t)((unsigned long )hfs_freeblks(hfsmp
, 1));
1517 sbp
->f_files
= (uint64_t)((unsigned long )(vcb
->totalBlocks
- 2)); /* max files is constrained by total blocks */
1518 sbp
->f_ffree
= (uint64_t)((unsigned long )(MIN(freeCNIDs
, sbp
->f_bavail
)));
1521 * Subtypes (flavors) for HFS
1522 * 0: Mac OS Extended
1523 * 1: Mac OS Extended (Journaled)
1524 * 2: Mac OS Extended (Case Sensitive)
1525 * 3: Mac OS Extended (Case Sensitive, Journaled)
1527 * 128: Mac OS Standard
1530 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1531 subtype
= HFS_SUBTYPE_STANDARDHFS
;
1532 } else /* HFS Plus */ {
1534 subtype
|= HFS_SUBTYPE_JOURNALED
;
1535 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
1536 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
1538 sbp
->f_fssubtype
= subtype
;
1545 // XXXdbg -- this is a callback to be used by the journal to
1546 // get meta data blocks flushed out to disk.
1548 // XXXdbg -- be smarter and don't flush *every* block on each
1549 // call. try to only flush some so we don't wind up
1550 // being too synchronous.
1554 hfs_sync_metadata(void *arg
)
1556 struct mount
*mp
= (struct mount
*)arg
;
1557 struct hfsmount
*hfsmp
;
1560 int sectorsize
, retval
;
1561 daddr64_t priIDSector
;
1562 hfsmp
= VFSTOHFS(mp
);
1563 vcb
= HFSTOVCB(hfsmp
);
1565 // now make sure the super block is flushed
1566 sectorsize
= hfsmp
->hfs_phys_block_size
;
1567 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1568 HFS_PRI_SECTOR(sectorsize
));
1569 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1571 panic("hfs: sync_metadata: can't read super-block?! (retval 0x%x, priIDSector)\n",
1572 retval
, priIDSector
);
1575 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1581 // the alternate super block...
1582 // XXXdbg - we probably don't need to do this each and every time.
1583 // hfs_btreeio.c:FlushAlternate() should flag when it was
1585 if (hfsmp
->hfs_alt_id_sector
) {
1586 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &bp
);
1587 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1596 struct hfs_sync_cargs
{
1605 hfs_sync_callback(struct vnode
*vp
, void *cargs
)
1608 struct hfs_sync_cargs
*args
;
1611 args
= (struct hfs_sync_cargs
*)cargs
;
1613 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1614 return (VNODE_RETURNED
);
1618 if ((cp
->c_flag
& C_MODIFIED
) ||
1619 (cp
->c_touch_acctime
| cp
->c_touch_chgtime
| cp
->c_touch_modtime
) ||
1620 vnode_hasdirtyblks(vp
)) {
1621 error
= hfs_fsync(vp
, args
->waitfor
, 0, args
->p
);
1624 args
->error
= error
;
1627 return (VNODE_RETURNED
);
1633 * Go through the disk queues to initiate sandbagged IO;
1634 * go through the inodes to write those that have been modified;
1635 * initiate the writing of the super block if it has been modified.
1637 * Note: we are always called with the filesystem marked `MPBUSY'.
1640 hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
)
1642 struct proc
*p
= vfs_context_proc(context
);
1644 struct hfsmount
*hfsmp
;
1646 struct vnode
*meta_vp
[4];
1648 int error
, allerror
= 0;
1649 struct hfs_sync_cargs args
;
1652 * During MNT_UPDATE hfs_changefs might be manipulating
1653 * vnodes so back off
1655 if (((uint32_t)vfs_flags(mp
)) & MNT_UPDATE
) /* XXX MNT_UPDATE may not be visible here */
1658 hfsmp
= VFSTOHFS(mp
);
1659 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1662 /* skip over frozen volumes */
1663 if (!lck_rw_try_lock_shared(&hfsmp
->hfs_insync
))
1666 args
.cred
= vfs_context_proc(context
);
1667 args
.waitfor
= waitfor
;
1671 * hfs_sync_callback will be called for each vnode
1672 * hung off of this mount point... the vnode will be
1673 * properly referenced and unreferenced around the callback
1675 vnode_iterate(mp
, 0, hfs_sync_callback
, (void *)&args
);
1678 allerror
= args
.error
;
1680 vcb
= HFSTOVCB(hfsmp
);
1682 meta_vp
[0] = vcb
->extentsRefNum
;
1683 meta_vp
[1] = vcb
->catalogRefNum
;
1684 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1685 meta_vp
[3] = hfsmp
->hfs_attribute_vp
; /* Optional file */
1687 /* Now sync our three metadata files */
1688 for (i
= 0; i
< 4; ++i
) {
1692 if ((btvp
==0) || (vnode_mount(btvp
) != mp
))
1695 /* XXX use hfs_systemfile_lock instead ? */
1696 (void) hfs_lock(VTOC(btvp
), HFS_EXCLUSIVE_LOCK
);
1699 if (((cp
->c_flag
& C_MODIFIED
) == 0) &&
1700 (cp
->c_touch_acctime
== 0) &&
1701 (cp
->c_touch_chgtime
== 0) &&
1702 (cp
->c_touch_modtime
== 0) &&
1703 vnode_hasdirtyblks(btvp
) == 0) {
1704 hfs_unlock(VTOC(btvp
));
1707 error
= vnode_get(btvp
);
1709 hfs_unlock(VTOC(btvp
));
1712 if ((error
= hfs_fsync(btvp
, waitfor
, 0, p
)))
1720 * Force stale file system control information to be flushed.
1722 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1723 if ((error
= VNOP_FSYNC(hfsmp
->hfs_devvp
, waitfor
, context
))) {
1731 hfs_hotfilesync(hfsmp
, p
);
1733 * Write back modified superblock.
1736 if (IsVCBDirty(vcb
)) {
1737 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1743 journal_flush(hfsmp
->jnl
);
1746 lck_rw_unlock_shared(&hfsmp
->hfs_insync
);
1752 * File handle to vnode
1754 * Have to be really careful about stale file handles:
1755 * - check that the cnode id is valid
1756 * - call hfs_vget() to get the locked cnode
1757 * - check for an unallocated cnode (i_mode == 0)
1758 * - check that the given client host has export rights and return
1759 * those rights via. exflagsp and credanonp
1762 hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
)
1764 struct hfsfid
*hfsfhp
;
1769 hfsfhp
= (struct hfsfid
*)fhp
;
1771 if (fhlen
< sizeof(struct hfsfid
))
1774 result
= hfs_vget(VFSTOHFS(mp
), hfsfhp
->hfsfid_cnid
, &nvp
, 0);
1776 if (result
== ENOENT
)
1781 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1782 * For NFS, we are assuming that only if the createtime was moved
1783 * forward would it mean the fileID got reused in that session by
1784 * wrapping. We don't have a volume ID or other unique identifier to
1785 * to use here for a generation ID across reboots, crashes where
1786 * metadata noting lastFileID didn't make it to disk but client has
1787 * it, or volume erasures where fileIDs start over again. Lastly,
1788 * with HFS allowing "wraps" of fileIDs now, this becomes more
1789 * error prone. Future, would be change the "wrap bit" to a unique
1790 * wrap number and use that for generation number. For now do this.
1792 if ((hfsfhp
->hfsfid_gen
< VTOC(nvp
)->c_itime
)) {
1793 hfs_unlock(VTOC(nvp
));
1799 hfs_unlock(VTOC(nvp
));
1805 * Vnode pointer to File handle
1809 hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
)
1812 struct hfsfid
*hfsfhp
;
1814 if (ISHFS(VTOVCB(vp
)))
1815 return (ENOTSUP
); /* hfs standard is not exportable */
1817 if (*fhlenp
< (int)sizeof(struct hfsfid
))
1821 hfsfhp
= (struct hfsfid
*)fhp
;
1822 hfsfhp
->hfsfid_cnid
= cp
->c_fileid
;
1823 hfsfhp
->hfsfid_gen
= cp
->c_itime
;
1824 *fhlenp
= sizeof(struct hfsfid
);
1831 * Initial HFS filesystems, done only once.
1834 hfs_init(__unused
struct vfsconf
*vfsp
)
1836 static int done
= 0;
1842 hfs_converterinit();
1850 hfs_lock_attr
= lck_attr_alloc_init();
1851 hfs_group_attr
= lck_grp_attr_alloc_init();
1852 hfs_mutex_group
= lck_grp_alloc_init("hfs-mutex", hfs_group_attr
);
1853 hfs_rwlock_group
= lck_grp_alloc_init("hfs-rwlock", hfs_group_attr
);
1855 /* Turn on lock debugging */
1856 //lck_attr_setdebug(hfs_lock_attr);
1863 hfs_getmountpoint(vp
, hfsmpp
)
1865 struct hfsmount
**hfsmpp
;
1867 struct hfsmount
* hfsmp
;
1868 char fstypename
[MFSNAMELEN
];
1873 if (!vnode_isvroot(vp
))
1876 vnode_vfsname(vp
, fstypename
);
1877 if (strcmp(fstypename
, "hfs") != 0)
1882 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1891 #include <sys/filedesc.h>
1894 * HFS filesystem related variables.
1897 hfs_sysctl(int *name
, __unused u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
1898 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
1900 struct proc
*p
= vfs_context_proc(context
);
1902 struct hfsmount
*hfsmp
;
1904 /* all sysctl names at this level are terminal */
1906 if (name
[0] == HFS_ENCODINGBIAS
) {
1909 bias
= hfs_getencodingbias();
1910 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
1911 if (error
== 0 && newp
)
1912 hfs_setencodingbias(bias
);
1915 } else if (name
[0] == HFS_EXTEND_FS
) {
1917 vnode_t vp
= p
->p_fd
->fd_cdir
;
1919 if (newp
== USER_ADDR_NULL
|| vp
== NULL
)
1921 if ((error
= hfs_getmountpoint(vp
, &hfsmp
)))
1923 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, &newsize
);
1927 error
= hfs_extendfs(hfsmp
, newsize
, context
);
1930 } else if (name
[0] == HFS_ENCODINGHINT
) {
1934 u_int16_t
*unicode_name
;
1937 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
1938 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
1939 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
1941 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
1943 error
= utf8_decodestr(filename
, newlen
- 1, unicode_name
,
1944 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
1946 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
1947 error
= sysctl_int(oldp
, oldlenp
, USER_ADDR_NULL
, 0, &hint
);
1950 FREE(unicode_name
, M_TEMP
);
1951 FREE(filename
, M_TEMP
);
1954 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
1955 // make the file system journaled...
1956 struct vnode
*vp
= p
->p_fd
->fd_cdir
, *jvp
;
1958 struct cat_attr jnl_attr
, jinfo_attr
;
1959 struct cat_fork jnl_fork
, jinfo_fork
;
1963 /* Only root can enable journaling */
1971 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1974 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
1975 printf("hfs: can't make a plain hfs volume journaled.\n");
1980 printf("hfs: volume @ mp 0x%x is already journaled!\n", vnode_mount(vp
));
1984 vcb
= HFSTOVCB(hfsmp
);
1985 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1986 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
1987 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
1989 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
1990 hfs_systemfile_unlock(hfsmp
, lockflags
);
1993 hfs_systemfile_unlock(hfsmp
, lockflags
);
1995 // make sure these both exist!
1996 if ( GetFileInfo(vcb
, kHFSRootFolderID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
1997 || GetFileInfo(vcb
, kHFSRootFolderID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2002 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, context
);
2004 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2005 (off_t
)name
[2], (off_t
)name
[3]);
2007 jvp
= hfsmp
->hfs_devvp
;
2008 jnl
= journal_create(jvp
,
2009 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2010 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2011 (off_t
)((unsigned)name
[3]),
2013 hfsmp
->hfs_phys_block_size
,
2016 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2019 printf("hfs: FAILED to create the journal!\n");
2020 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2021 VNOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2028 hfs_global_exclusive_lock_acquire(hfsmp
);
2030 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2031 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2035 // save this off for the hack-y check in hfs_remove()
2036 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2037 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2038 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2039 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2041 vfs_setflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
2043 hfs_global_exclusive_lock_release(hfsmp
);
2044 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2047 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2048 // clear the journaling bit
2049 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2051 /* Only root can disable journaling */
2060 printf("hfs: disabling journaling for mount @ 0x%x\n", vnode_mount(vp
));
2062 hfs_global_exclusive_lock_acquire(hfsmp
);
2064 // Lights out for you buddy!
2065 journal_close(hfsmp
->jnl
);
2068 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2069 VNOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2072 vfs_clearflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
2073 hfsmp
->jnl_start
= 0;
2074 hfsmp
->hfs_jnlinfoblkid
= 0;
2075 hfsmp
->hfs_jnlfileid
= 0;
2077 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2079 hfs_global_exclusive_lock_release(hfsmp
);
2080 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2083 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2084 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2085 off_t jnl_start
, jnl_size
;
2091 if (hfsmp
->jnl
== NULL
) {
2095 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2096 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2099 if ((error
= copyout((caddr_t
)&jnl_start
, CAST_USER_ADDR_T(name
[1]), sizeof(off_t
))) != 0) {
2102 if ((error
= copyout((caddr_t
)&jnl_size
, CAST_USER_ADDR_T(name
[2]), sizeof(off_t
))) != 0) {
2107 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2109 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2111 } else if (name
[0] == VFS_CTL_QUERY
) {
2112 struct sysctl_req
*req
;
2114 struct user_vfsidctl user_vc
;
2117 boolean_t is_64_bit
;
2119 is_64_bit
= proc_is64bit(p
);
2120 req
= CAST_DOWN(struct sysctl_req
*, oldp
); /* we're new style vfs sysctl. */
2123 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2124 if (error
) return (error
);
2126 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
2129 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2130 if (error
) return (error
);
2132 mp
= vfs_getvfs(&vc
.vc_fsid
);
2134 if (mp
== NULL
) return (ENOENT
);
2136 hfsmp
= VFSTOHFS(mp
);
2137 bzero(&vq
, sizeof(vq
));
2138 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2139 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2147 hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, __unused vfs_context_t context
)
2149 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)ino
, vpp
, 1);
2154 * Look up an HFS object by ID.
2156 * The object is returned with an iocount reference and the cnode locked.
2158 * If the object is a file then it will represent the data fork.
2162 hfs_vget(struct hfsmount
*hfsmp
, cnid_t cnid
, struct vnode
**vpp
, int skiplock
)
2164 struct vnode
*vp
= NULL
;
2165 struct cat_desc cndesc
;
2166 struct cat_attr cnattr
;
2167 struct cat_fork cnfork
;
2168 struct componentname cn
;
2169 u_int32_t linkref
= 0;
2172 /* Check for cnids that should't be exported. */
2173 if ((cnid
< kHFSFirstUserCatalogNodeID
)
2174 && (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
))
2177 /* Don't export HFS Private Data dir. */
2178 if (cnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
2182 * Check the hash first
2184 vp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, cnid
, 0, skiplock
);
2190 bzero(&cndesc
, sizeof(cndesc
));
2191 bzero(&cnattr
, sizeof(cnattr
));
2192 bzero(&cnfork
, sizeof(cnfork
));
2195 * Not in hash, lookup in catalog
2197 if (cnid
== kHFSRootParentID
) {
2198 static char hfs_rootname
[] = "/";
2200 cndesc
.cd_nameptr
= &hfs_rootname
[0];
2201 cndesc
.cd_namelen
= 1;
2202 cndesc
.cd_parentcnid
= kHFSRootParentID
;
2203 cndesc
.cd_cnid
= kHFSRootFolderID
;
2204 cndesc
.cd_flags
= CD_ISDIR
;
2206 cnattr
.ca_fileid
= kHFSRootFolderID
;
2207 cnattr
.ca_nlink
= 2;
2208 cnattr
.ca_entries
= 1;
2209 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
2213 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2214 error
= cat_idlookup(hfsmp
, cnid
, &cndesc
, &cnattr
, &cnfork
);
2215 hfs_systemfile_unlock(hfsmp
, lockflags
);
2223 * If we just looked up a raw hardlink inode,
2224 * then finish initializing it.
2226 if ((cndesc
.cd_parentcnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
) &&
2227 (bcmp(cndesc
.cd_nameptr
, HFS_INODE_PREFIX
, HFS_INODE_PREFIX_LEN
) == 0)) {
2228 linkref
= strtoul((const char*)&cndesc
.cd_nameptr
[HFS_INODE_PREFIX_LEN
], NULL
, 10);
2229 cnattr
.ca_rdev
= linkref
;
2231 // patch up the parentcnid
2232 if (cnattr
.ca_attrblks
!= 0) {
2233 cndesc
.cd_parentcnid
= cnattr
.ca_attrblks
;
2239 * Supply hfs_getnewvnode with a component name.
2241 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2242 cn
.cn_nameiop
= LOOKUP
;
2243 cn
.cn_flags
= ISLASTCN
| HASBUF
;
2244 cn
.cn_context
= NULL
;
2245 cn
.cn_pnlen
= MAXPATHLEN
;
2246 cn
.cn_nameptr
= cn
.cn_pnbuf
;
2247 cn
.cn_namelen
= cndesc
.cd_namelen
;
2250 bcopy(cndesc
.cd_nameptr
, cn
.cn_nameptr
, cndesc
.cd_namelen
+ 1);
2252 /* XXX should we supply the parent as well... ? */
2253 error
= hfs_getnewvnode(hfsmp
, NULLVP
, &cn
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2254 if (error
== 0 && linkref
!= 0) {
2255 VTOC(vp
)->c_flag
|= C_HARDLINK
;
2258 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
2260 cat_releasedesc(&cndesc
);
2263 hfs_unlock(VTOC(vp
));
2269 * Flush out all the files in a filesystem.
2272 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2274 struct hfsmount
*hfsmp
;
2275 struct vnode
*skipvp
= NULLVP
;
2280 hfsmp
= VFSTOHFS(mp
);
2284 * The open quota files have an indirect reference on
2285 * the root directory vnode. We must account for this
2286 * extra reference when doing the intial vflush.
2289 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2291 /* Find out how many quota files we have open. */
2292 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2293 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2297 /* Obtain the root vnode so we can skip over it. */
2298 skipvp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, kHFSRootFolderID
, 0, 0);
2302 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2306 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2309 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2312 * See if there are additional references on the
2313 * root vp besides the ones obtained from the open
2314 * quota files and the hfs_chash_getvnode call above.
2317 (vnode_isinuse(skipvp
, quotafilecnt
))) {
2318 error
= EBUSY
; /* root directory is still open */
2320 hfs_unlock(VTOC(skipvp
));
2323 if (error
&& (flags
& FORCECLOSE
) == 0)
2326 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2327 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2329 hfs_quotaoff(p
, mp
, i
);
2331 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2339 * Update volume encoding bitmap (HFS Plus only)
2343 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2345 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2346 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2351 case kTextEncodingMacUkrainian
:
2352 index
= kIndexMacUkrainian
;
2354 case kTextEncodingMacFarsi
:
2355 index
= kIndexMacFarsi
;
2363 HFS_MOUNT_LOCK(hfsmp
, TRUE
)
2364 hfsmp
->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2365 hfsmp
->vcbFlags
|= 0xFF00;
2366 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2371 * Update volume stats
2373 * On journal volumes this will cause a volume header flush
2377 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2383 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2385 hfsmp
->vcbFlags
|= 0xFF00;
2386 hfsmp
->hfs_mtime
= tv
.tv_sec
;
2392 if (hfsmp
->hfs_dircount
!= 0xFFFFFFFF)
2393 ++hfsmp
->hfs_dircount
;
2394 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2395 ++hfsmp
->vcbNmRtDirs
;
2398 if (hfsmp
->hfs_dircount
!= 0)
2399 --hfsmp
->hfs_dircount
;
2400 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2401 --hfsmp
->vcbNmRtDirs
;
2404 if (hfsmp
->hfs_filecount
!= 0xFFFFFFFF)
2405 ++hfsmp
->hfs_filecount
;
2406 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2410 if (hfsmp
->hfs_filecount
!= 0)
2411 --hfsmp
->hfs_filecount
;
2412 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2417 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2420 hfs_flushvolumeheader(hfsmp
, 0, 0);
2428 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2430 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2431 struct filefork
*fp
;
2432 HFSMasterDirectoryBlock
*mdb
;
2433 struct buf
*bp
= NULL
;
2438 sectorsize
= hfsmp
->hfs_phys_block_size
;
2439 retval
= (int)buf_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2446 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2448 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2450 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2451 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2452 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2453 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2454 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2455 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2456 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2457 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2459 namelen
= strlen(vcb
->vcbVN
);
2460 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2461 /* Retry with MacRoman in case that's how it was exported. */
2463 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2465 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2466 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2467 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2468 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2469 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2471 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2473 fp
= VTOF(vcb
->extentsRefNum
);
2474 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2475 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2476 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2477 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2478 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2479 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2480 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2481 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2482 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2484 fp
= VTOF(vcb
->catalogRefNum
);
2485 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2486 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2487 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2488 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2489 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2490 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2491 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2492 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2493 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2495 MarkVCBClean( vcb
);
2497 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2499 /* If requested, flush out the alternate MDB */
2501 struct buf
*alt_bp
= NULL
;
2503 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2504 bcopy(mdb
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2506 (void) VNOP_BWRITE(alt_bp
);
2511 if (waitfor
!= MNT_WAIT
)
2514 retval
= VNOP_BWRITE(bp
);
2520 * Flush any dirty in-memory mount data to the on-disk
2523 * Note: the on-disk volume signature is intentionally
2524 * not flushed since the on-disk "H+" and "HX" signatures
2525 * are always stored in-memory as "H+".
2529 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2531 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2532 struct filefork
*fp
;
2533 HFSPlusVolumeHeader
*volumeHeader
;
2538 daddr64_t priIDSector
;
2540 u_int16_t signature
;
2541 u_int16_t hfsversion
;
2543 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2546 if (vcb
->vcbSigWord
== kHFSSigWord
)
2547 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2551 sectorsize
= hfsmp
->hfs_phys_block_size
;
2552 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2553 HFS_PRI_SECTOR(sectorsize
));
2555 if (hfs_start_transaction(hfsmp
) != 0) {
2559 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2564 hfs_end_transaction(hfsmp
);
2566 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2571 journal_modify_block_start(hfsmp
->jnl
, bp
);
2574 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2577 * Sanity check what we just read.
2579 signature
= SWAP_BE16 (volumeHeader
->signature
);
2580 hfsversion
= SWAP_BE16 (volumeHeader
->version
);
2581 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2582 (hfsversion
< kHFSPlusVersion
) || (hfsversion
> 100) ||
2583 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2585 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2586 vcb
->vcbVN
, signature
, hfsversion
,
2587 SWAP_BE32 (volumeHeader
->blockSize
));
2589 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2595 * For embedded HFS+ volumes, update create date if it changed
2596 * (ie from a setattrlist call)
2598 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2599 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2601 HFSMasterDirectoryBlock
*mdb
;
2603 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
),
2604 sectorsize
, NOCRED
, &bp2
);
2610 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp2
) +
2611 HFS_PRI_OFFSET(sectorsize
));
2613 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2616 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2619 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2622 journal_modify_block_end(hfsmp
->jnl
, bp2
);
2624 (void) VNOP_BWRITE(bp2
); /* write out the changes */
2629 buf_brelse(bp2
); /* just release it */
2634 if (1 /* hfsmp->jnl == 0 */) {
2635 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2638 /* Note: only update the lower 16 bits worth of attributes */
2639 volumeHeader
->attributes
= SWAP_BE32 (vcb
->vcbAtrb
);
2640 volumeHeader
->journalInfoBlock
= SWAP_BE32 (vcb
->vcbJinfoBlock
);
2642 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2644 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2646 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2647 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2648 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2649 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2650 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2651 volumeHeader
->totalBlocks
= SWAP_BE32 (vcb
->totalBlocks
);
2652 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2653 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2654 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2655 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2656 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2657 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2658 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2660 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0) {
2661 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2665 /* Sync Extents over-flow file meta data */
2666 fp
= VTOF(vcb
->extentsRefNum
);
2667 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2668 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2669 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2670 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2671 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2672 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2674 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2675 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2676 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2677 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2680 /* Sync Catalog file meta data */
2681 fp
= VTOF(vcb
->catalogRefNum
);
2682 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2683 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2684 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2685 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2686 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2687 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2689 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2690 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2691 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2692 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2695 /* Sync Allocation file meta data */
2696 fp
= VTOF(vcb
->allocationsRefNum
);
2697 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2698 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2699 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2700 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2701 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2702 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2704 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2705 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2706 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2707 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2710 /* Sync Attribute file meta data */
2711 if (hfsmp
->hfs_attribute_vp
) {
2712 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
2713 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2714 volumeHeader
->attributesFile
.extents
[i
].startBlock
=
2715 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2716 volumeHeader
->attributesFile
.extents
[i
].blockCount
=
2717 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2719 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2720 volumeHeader
->attributesFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2721 volumeHeader
->attributesFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2722 volumeHeader
->attributesFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2725 vcb
->vcbFlags
&= 0x00FF;
2727 if (1 /* hfsmp->jnl == 0 */) {
2728 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2731 /* If requested, flush out the alternate volume header */
2732 if (altflush
&& hfsmp
->hfs_alt_id_sector
) {
2733 struct buf
*alt_bp
= NULL
;
2735 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2737 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2740 bcopy(volumeHeader
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2743 journal_modify_block_end(hfsmp
->jnl
, alt_bp
);
2745 (void) VNOP_BWRITE(alt_bp
);
2752 journal_modify_block_end(hfsmp
->jnl
, bp
);
2754 if (waitfor
!= MNT_WAIT
)
2757 retval
= VNOP_BWRITE(bp
);
2758 /* When critical data changes, flush the device cache */
2759 if (critical
&& (retval
== 0)) {
2760 (void) VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2761 NULL
, FWRITE
, NULL
);
2765 hfs_end_transaction(hfsmp
);
2772 * Extend a file system.
2776 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
2778 struct proc
*p
= vfs_context_proc(context
);
2779 kauth_cred_t cred
= vfs_context_ucred(context
);
2781 struct vnode
*devvp
;
2783 struct filefork
*fp
= NULL
;
2785 struct cat_fork forkdata
;
2787 u_int64_t newblkcnt
;
2788 u_int64_t prev_phys_block_count
;
2790 u_int64_t sectorcnt
;
2791 u_int32_t sectorsize
;
2792 daddr64_t prev_alt_sector
;
2797 devvp
= hfsmp
->hfs_devvp
;
2798 vcb
= HFSTOVCB(hfsmp
);
2801 * - HFS Plus file systems only.
2802 * - Journaling must be enabled.
2803 * - No embedded volumes.
2805 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
2806 (hfsmp
->jnl
== NULL
) ||
2807 (vcb
->hfsPlusIOPosOffset
!= 0)) {
2811 * If extending file system by non-root, then verify
2812 * ownership and check permissions.
2814 if (suser(cred
, NULL
)) {
2815 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0);
2819 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
2821 error
= hfs_write_access(vp
, cred
, p
, false);
2823 hfs_unlock(VTOC(vp
));
2828 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
2832 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, context
)) {
2835 if (sectorsize
!= hfsmp
->hfs_phys_block_size
) {
2838 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, context
)) {
2841 if ((sectorsize
* sectorcnt
) < newsize
) {
2842 printf("hfs_extendfs: not enough space on device\n");
2845 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
2848 * Validate new size.
2850 if ((newsize
<= oldsize
) || (newsize
% sectorsize
)) {
2851 printf("hfs_extendfs: invalid size\n");
2854 newblkcnt
= newsize
/ vcb
->blockSize
;
2855 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
2858 addblks
= newblkcnt
- vcb
->totalBlocks
;
2860 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
2862 * Enclose changes inside a transaction.
2864 if (hfs_start_transaction(hfsmp
) != 0) {
2868 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2869 vp
= vcb
->allocationsRefNum
;
2871 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
2874 * Calculate additional space required (if any) by allocation bitmap.
2876 bitmapblks
= roundup(newblkcnt
/ 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
2877 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
2878 bitmapblks
-= fp
->ff_blocks
;
2882 if (bitmapblks
> 0) {
2887 * Add a new extent to the allocation bitmap file.
2889 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
2891 printf("hfs_extendfs: error %d adding extents\n", error
);
2894 blkcnt
= bitmapblks
;
2895 blkno
= (daddr64_t
)fp
->ff_blocks
;
2896 fp
->ff_blocks
+= bitmapblks
;
2897 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
2898 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
2900 * Zero out the new bitmap blocks.
2905 while (blkcnt
> 0) {
2906 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
2913 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
2915 error
= (int)buf_bwrite(bp
);
2923 printf("hfs_extendfs: error %d clearing blocks\n", error
);
2927 * Mark the new bitmap space as allocated.
2929 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
2931 printf("hfs_extendfs: error %d setting bitmap\n", error
);
2936 * Mark the new alternate VH as allocated.
2938 if (vcb
->blockSize
== 512)
2939 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
2941 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
2943 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
2947 * Mark the old alternate VH as free.
2949 if (vcb
->blockSize
== 512)
2950 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
2952 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
2954 * Adjust file system variables for new space.
2956 prev_phys_block_count
= hfsmp
->hfs_phys_block_count
;
2957 prev_alt_sector
= hfsmp
->hfs_alt_id_sector
;
2959 vcb
->totalBlocks
+= addblks
;
2960 vcb
->freeBlocks
+= addblks
- bitmapblks
;
2961 hfsmp
->hfs_phys_block_count
= newsize
/ sectorsize
;
2962 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sectorsize
) +
2963 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2965 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
2967 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
2969 * Restore to old state.
2971 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
2972 vcb
->totalBlocks
-= addblks
;
2973 vcb
->freeBlocks
-= addblks
- bitmapblks
;
2974 hfsmp
->hfs_phys_block_count
= prev_phys_block_count
;
2975 hfsmp
->hfs_alt_id_sector
= prev_alt_sector
;
2977 if (vcb
->blockSize
== 512)
2978 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
2980 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
2984 * Invalidate the old alternate volume header.
2987 if (prev_alt_sector
) {
2988 if (buf_meta_bread(hfsmp
->hfs_devvp
, prev_alt_sector
, sectorsize
,
2989 NOCRED
, &bp
) == 0) {
2990 journal_modify_block_start(hfsmp
->jnl
, bp
);
2992 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2994 journal_modify_block_end(hfsmp
->jnl
, bp
);
3001 /* Restore allocation fork. */
3002 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3003 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3006 hfs_systemfile_unlock(hfsmp
, lockflags
);
3007 hfs_end_transaction(hfsmp
);
3012 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3015 * Truncate a file system (while still mounted).
3019 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, __unused vfs_context_t context
)
3021 struct vnode
* rvp
= NULL
;
3022 struct buf
*bp
= NULL
;
3024 u_int32_t newblkcnt
;
3025 u_int32_t reclaimblks
;
3027 int transaction_begun
= 0;
3031 * Grab the root vnode to serialize with another hfs_truncatefs call.
3033 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &rvp
, 0);
3038 * - HFS Plus file systems only.
3039 * - Journaling must be enabled.
3040 * - No embedded volumes.
3042 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) ||
3043 (hfsmp
->jnl
== NULL
) ||
3044 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
3048 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3049 newblkcnt
= newsize
/ hfsmp
->blockSize
;
3050 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
3052 /* Make sure new size is valid. */
3053 if ((newsize
< HFS_MIN_SIZE
) ||
3054 (newsize
>= oldsize
) ||
3055 (newsize
% hfsmp
->hfs_phys_block_size
)) {
3059 /* Make sure there's enough space to work with. */
3060 if (reclaimblks
> (hfsmp
->freeBlocks
/ 4)) {
3065 printf("hfs_truncatefs: shrinking %s by %d blocks out of %d\n",
3066 hfsmp
->vcbVN
, reclaimblks
, hfsmp
->totalBlocks
);
3068 if (hfs_start_transaction(hfsmp
) != 0) {
3072 transaction_begun
= 1;
3075 * Look for files that have blocks beyond newblkcnt.
3077 if (hfs_isallocated(hfsmp
, newblkcnt
, reclaimblks
- 1)) {
3079 * hfs_reclaimspace will use separate transactions when
3080 * relocating files (so we don't overwhelm the journal).
3082 hfs_end_transaction(hfsmp
);
3083 transaction_begun
= 0;
3085 /* Attempt to reclaim some space. */
3086 if (hfs_reclaimspace(hfsmp
, newblkcnt
) != 0) {
3087 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp
->vcbVN
);
3091 if (hfs_start_transaction(hfsmp
) != 0) {
3095 transaction_begun
= 1;
3097 /* Check if we're clear now. */
3098 if (hfs_isallocated(hfsmp
, newblkcnt
, reclaimblks
- 1)) {
3099 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp
->vcbVN
);
3104 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3107 * Mark the old alternate volume header as free.
3108 * We don't bother shrinking allocation bitmap file.
3110 if (hfsmp
->blockSize
== 512)
3111 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
3113 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
3116 * Allocate last block for alternate volume header.
3118 if (hfsmp
->blockSize
== 512)
3119 error
= BlockMarkAllocated(hfsmp
, newblkcnt
- 2, 2);
3121 error
= BlockMarkAllocated(hfsmp
, newblkcnt
- 1, 1);
3128 * Invalidate the existing alternate volume header.
3130 if (hfsmp
->hfs_alt_id_sector
) {
3131 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
,
3132 hfsmp
->hfs_phys_block_size
, NOCRED
, &bp
) == 0) {
3133 journal_modify_block_start(hfsmp
->jnl
, bp
);
3135 bzero((void*)((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_phys_block_size
)), kMDBSize
);
3137 journal_modify_block_end(hfsmp
->jnl
, bp
);
3145 * Adjust file system variables and flush them to disk.
3147 hfsmp
->freeBlocks
-= hfsmp
->totalBlocks
- newblkcnt
;
3148 hfsmp
->totalBlocks
= newblkcnt
;
3149 hfsmp
->hfs_phys_block_count
= newsize
/ hfsmp
->hfs_phys_block_size
;
3150 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
, hfsmp
->hfs_phys_block_count
);
3151 MarkVCBDirty(hfsmp
);
3152 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3154 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
3157 hfs_systemfile_unlock(hfsmp
, lockflags
);
3159 if (transaction_begun
) {
3160 hfs_end_transaction(hfsmp
);
3163 hfs_unlock(VTOC(rvp
));
3170 * Reclaim space at the end of a file system.
3173 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
)
3175 struct vnode
*vp
= NULL
;
3177 struct BTreeIterator
* iterator
= NULL
;
3178 struct FSBufferDescriptor btdata
;
3179 struct HFSPlusCatalogFile filerec
;
3180 u_int32_t saved_next_allocation
;
3191 * Check if Attributes file overlaps.
3193 if (hfsmp
->hfs_attribute_vp
) {
3194 struct filefork
*fp
;
3196 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
3197 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3198 block
= fp
->ff_extents
[i
].startBlock
+
3199 fp
->ff_extents
[i
].blockCount
;
3200 if (block
>= startblk
) {
3201 printf("hfs_reclaimspace: Attributes file can't move\n");
3207 /* For now we'll move a maximum of 16,384 files. */
3208 maxfilecnt
= MIN(hfsmp
->hfs_filecount
, 16384);
3209 cnidbufsize
= maxfilecnt
* sizeof(cnid_t
);
3210 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&cnidbufp
, cnidbufsize
)) {
3213 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
3214 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
3218 saved_next_allocation
= hfsmp
->nextAllocation
;
3219 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_start
;
3221 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
3222 bzero(iterator
, sizeof(*iterator
));
3224 btdata
.bufferAddress
= &filerec
;
3225 btdata
.itemSize
= sizeof(filerec
);
3226 btdata
.itemCount
= 1;
3228 /* Keep the Catalog file locked during iteration. */
3229 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3230 error
= BTIterateRecord(fcb
, kBTreeFirstRecord
, iterator
, NULL
, NULL
);
3232 hfs_systemfile_unlock(hfsmp
, lockflags
);
3237 * Iterate over all the catalog records looking for files
3238 * that overlap into the space we're trying to free up.
3240 for (filecnt
= 0; filecnt
< maxfilecnt
; ) {
3241 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3243 if (error
== btNotFound
)
3247 if (filerec
.recordType
!= kHFSPlusFileRecord
||
3248 filerec
.fileID
== hfsmp
->hfs_jnlfileid
)
3251 * Check if either fork overlaps target space.
3253 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3254 block
= filerec
.dataFork
.extents
[i
].startBlock
+
3255 filerec
.dataFork
.extents
[i
].blockCount
;
3256 if (block
>= startblk
) {
3257 if (filerec
.fileID
== hfsmp
->hfs_jnlfileid
) {
3258 printf("hfs_reclaimspace: cannot move active journal\n");
3262 cnidbufp
[filecnt
++] = filerec
.fileID
;
3265 block
= filerec
.resourceFork
.extents
[i
].startBlock
+
3266 filerec
.resourceFork
.extents
[i
].blockCount
;
3267 if (block
>= startblk
) {
3268 cnidbufp
[filecnt
++] = filerec
.fileID
;
3273 /* All done with catalog. */
3274 hfs_systemfile_unlock(hfsmp
, lockflags
);
3278 /* Now move any files that are in the way. */
3279 for (i
= 0; i
< filecnt
; ++i
) {
3282 if (hfs_vget(hfsmp
, cnidbufp
[i
], &vp
, 0) != 0)
3285 /* Relocate any data fork blocks. */
3286 if (VTOF(vp
)->ff_blocks
> 0) {
3287 error
= hfs_relocate(vp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
3292 /* Relocate any resource fork blocks. */
3293 if ((VTOC((vp
))->c_blocks
- VTOF((vp
))->ff_blocks
) > 0) {
3294 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, current_proc());
3297 error
= hfs_relocate(rvp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
3302 hfs_unlock(VTOC(vp
));
3307 hfs_unlock(VTOC(vp
));
3313 * Note: this implementation doesn't handle overflow extents.
3316 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3317 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
3319 /* On errors restore the roving allocation pointer. */
3321 hfsmp
->nextAllocation
= saved_next_allocation
;
3328 * Get file system attributes.
3331 hfs_vfs_getattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
3333 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
3334 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
3337 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)hfsmp
->vcbNxtCNID
;
3339 VFSATTR_RETURN(fsap
, f_objcount
, (uint64_t)hfsmp
->vcbFilCnt
+ (uint64_t)hfsmp
->vcbDirCnt
);
3340 VFSATTR_RETURN(fsap
, f_filecount
, (uint64_t)hfsmp
->vcbFilCnt
);
3341 VFSATTR_RETURN(fsap
, f_dircount
, (uint64_t)hfsmp
->vcbDirCnt
);
3342 VFSATTR_RETURN(fsap
, f_maxobjcount
, (uint64_t)0xFFFFFFFF);
3343 VFSATTR_RETURN(fsap
, f_iosize
, (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
));
3344 VFSATTR_RETURN(fsap
, f_blocks
, (uint64_t)hfsmp
->totalBlocks
);
3345 VFSATTR_RETURN(fsap
, f_bfree
, (uint64_t)hfs_freeblks(hfsmp
, 0));
3346 VFSATTR_RETURN(fsap
, f_bavail
, (uint64_t)hfs_freeblks(hfsmp
, 1));
3347 VFSATTR_RETURN(fsap
, f_bsize
, (uint32_t)vcb
->blockSize
);
3348 /* XXX needs clarification */
3349 VFSATTR_RETURN(fsap
, f_bused
, hfsmp
->totalBlocks
- hfs_freeblks(hfsmp
, 1));
3350 /* Maximum files is constrained by total blocks. */
3351 VFSATTR_RETURN(fsap
, f_files
, (uint64_t)(hfsmp
->totalBlocks
- 2));
3352 VFSATTR_RETURN(fsap
, f_ffree
, MIN((uint64_t)freeCNIDs
, (uint64_t)hfs_freeblks(hfsmp
, 1)));
3354 fsap
->f_fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
3355 fsap
->f_fsid
.val
[1] = vfs_typenum(mp
);
3356 VFSATTR_SET_SUPPORTED(fsap
, f_fsid
);
3358 VFSATTR_RETURN(fsap
, f_signature
, vcb
->vcbSigWord
);
3359 VFSATTR_RETURN(fsap
, f_carbon_fsid
, 0);
3361 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
3362 vol_capabilities_attr_t
*cap
;
3364 cap
= &fsap
->f_capabilities
;
3366 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
3367 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
3368 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3369 VOL_CAP_FMT_CASE_PRESERVING
|
3370 VOL_CAP_FMT_FAST_STATFS
;
3372 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
3373 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3374 VOL_CAP_FMT_SYMBOLICLINKS
|
3375 VOL_CAP_FMT_HARDLINKS
|
3376 VOL_CAP_FMT_JOURNAL
|
3377 (hfsmp
->jnl
? VOL_CAP_FMT_JOURNAL_ACTIVE
: 0) |
3378 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
? VOL_CAP_FMT_CASE_SENSITIVE
: 0) |
3379 VOL_CAP_FMT_CASE_PRESERVING
|
3380 VOL_CAP_FMT_FAST_STATFS
|
3381 VOL_CAP_FMT_2TB_FILESIZE
;
3383 cap
->capabilities
[VOL_CAPABILITIES_INTERFACES
] =
3384 VOL_CAP_INT_SEARCHFS
|
3385 VOL_CAP_INT_ATTRLIST
|
3386 VOL_CAP_INT_NFSEXPORT
|
3387 VOL_CAP_INT_READDIRATTR
|
3388 VOL_CAP_INT_EXCHANGEDATA
|
3389 VOL_CAP_INT_ALLOCATE
|
3390 VOL_CAP_INT_VOL_RENAME
|
3391 VOL_CAP_INT_ADVLOCK
|
3393 cap
->capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
3394 cap
->capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
3396 cap
->valid
[VOL_CAPABILITIES_FORMAT
] =
3397 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3398 VOL_CAP_FMT_SYMBOLICLINKS
|
3399 VOL_CAP_FMT_HARDLINKS
|
3400 VOL_CAP_FMT_JOURNAL
|
3401 VOL_CAP_FMT_JOURNAL_ACTIVE
|
3402 VOL_CAP_FMT_NO_ROOT_TIMES
|
3403 VOL_CAP_FMT_SPARSE_FILES
|
3404 VOL_CAP_FMT_ZERO_RUNS
|
3405 VOL_CAP_FMT_CASE_SENSITIVE
|
3406 VOL_CAP_FMT_CASE_PRESERVING
|
3407 VOL_CAP_FMT_FAST_STATFS
|
3408 VOL_CAP_FMT_2TB_FILESIZE
;
3409 cap
->valid
[VOL_CAPABILITIES_INTERFACES
] =
3410 VOL_CAP_INT_SEARCHFS
|
3411 VOL_CAP_INT_ATTRLIST
|
3412 VOL_CAP_INT_NFSEXPORT
|
3413 VOL_CAP_INT_READDIRATTR
|
3414 VOL_CAP_INT_EXCHANGEDATA
|
3415 VOL_CAP_INT_COPYFILE
|
3416 VOL_CAP_INT_ALLOCATE
|
3417 VOL_CAP_INT_VOL_RENAME
|
3418 VOL_CAP_INT_ADVLOCK
|
3420 cap
->valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
3421 cap
->valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
3422 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
3424 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
3425 vol_attributes_attr_t
*attrp
= &fsap
->f_attributes
;
3427 attrp
->validattr
.commonattr
= ATTR_CMN_VALIDMASK
;
3428 attrp
->validattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
3429 attrp
->validattr
.dirattr
= ATTR_DIR_VALIDMASK
;
3430 attrp
->validattr
.fileattr
= ATTR_FILE_VALIDMASK
;
3431 attrp
->validattr
.forkattr
= 0;
3433 attrp
->nativeattr
.commonattr
= ATTR_CMN_VALIDMASK
;
3434 attrp
->nativeattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
3435 attrp
->nativeattr
.dirattr
= ATTR_DIR_VALIDMASK
;
3436 attrp
->nativeattr
.fileattr
= ATTR_FILE_VALIDMASK
;
3437 attrp
->nativeattr
.forkattr
= 0;
3438 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
3440 fsap
->f_create_time
.tv_sec
= hfsmp
->vcbCrDate
;
3441 fsap
->f_create_time
.tv_nsec
= 0;
3442 VFSATTR_SET_SUPPORTED(fsap
, f_create_time
);
3443 fsap
->f_modify_time
.tv_sec
= hfsmp
->vcbLsMod
;
3444 fsap
->f_modify_time
.tv_nsec
= 0;
3445 VFSATTR_SET_SUPPORTED(fsap
, f_modify_time
);
3447 fsap
->f_backup_time
.tv_sec
= hfsmp
->vcbVolBkUp
;
3448 fsap
->f_backup_time
.tv_nsec
= 0;
3449 VFSATTR_SET_SUPPORTED(fsap
, f_backup_time
);
3450 if (VFSATTR_IS_ACTIVE(fsap
, f_fssubtype
)) {
3451 uint16_t subtype
= 0;
3454 * Subtypes (flavors) for HFS
3455 * 0: Mac OS Extended
3456 * 1: Mac OS Extended (Journaled)
3457 * 2: Mac OS Extended (Case Sensitive)
3458 * 3: Mac OS Extended (Case Sensitive, Journaled)
3460 * 128: Mac OS Standard
3463 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
3464 subtype
= HFS_SUBTYPE_STANDARDHFS
;
3465 } else /* HFS Plus */ {
3467 subtype
|= HFS_SUBTYPE_JOURNALED
;
3468 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
3469 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
3471 fsap
->f_fssubtype
= subtype
;
3472 VFSATTR_SET_SUPPORTED(fsap
, f_fssubtype
);
3475 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
3476 strncpy(fsap
->f_vol_name
, hfsmp
->vcbVN
, MAXPATHLEN
);
3477 fsap
->f_vol_name
[MAXPATHLEN
- 1] = 0;
3478 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
3484 * Perform a volume rename. Requires the FS' root vp.
3487 hfs_rename_volume(struct vnode
*vp
, const char *name
, proc_t p
)
3489 ExtendedVCB
*vcb
= VTOVCB(vp
);
3490 struct cnode
*cp
= VTOC(vp
);
3491 struct hfsmount
*hfsmp
= VTOHFS(vp
);
3492 struct cat_desc to_desc
;
3493 struct cat_desc todir_desc
;
3494 struct cat_desc new_desc
;
3495 cat_cookie_t cookie
;
3500 * Ignore attempts to rename a volume to a zero-length name.
3505 bzero(&to_desc
, sizeof(to_desc
));
3506 bzero(&todir_desc
, sizeof(todir_desc
));
3507 bzero(&new_desc
, sizeof(new_desc
));
3508 bzero(&cookie
, sizeof(cookie
));
3510 todir_desc
.cd_parentcnid
= kHFSRootParentID
;
3511 todir_desc
.cd_cnid
= kHFSRootFolderID
;
3512 todir_desc
.cd_flags
= CD_ISDIR
;
3514 to_desc
.cd_nameptr
= name
;
3515 to_desc
.cd_namelen
= strlen(name
);
3516 to_desc
.cd_parentcnid
= kHFSRootParentID
;
3517 to_desc
.cd_cnid
= cp
->c_cnid
;
3518 to_desc
.cd_flags
= CD_ISDIR
;
3520 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)) == 0) {
3521 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
3522 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, &cookie
, p
)) == 0) {
3523 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3525 error
= cat_rename(hfsmp
, &cp
->c_desc
, &todir_desc
, &to_desc
, &new_desc
);
3528 * If successful, update the name in the VCB, ensure it's terminated.
3531 strncpy(vcb
->vcbVN
, name
, sizeof(vcb
->vcbVN
));
3532 vcb
->vcbVN
[sizeof(vcb
->vcbVN
) - 1] = 0;
3535 hfs_systemfile_unlock(hfsmp
, lockflags
);
3536 cat_postflight(hfsmp
, &cookie
, p
);
3539 vcb
->vcbFlags
|= 0xFF00;
3540 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
3542 hfs_end_transaction(hfsmp
);
3545 /* Release old allocated name buffer */
3546 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
3547 char *name
= cp
->c_desc
.cd_nameptr
;
3549 cp
->c_desc
.cd_nameptr
= 0;
3550 cp
->c_desc
.cd_namelen
= 0;
3551 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
3552 vfs_removename(name
);
3554 /* Update cnode's catalog descriptor */
3555 replace_desc(cp
, &new_desc
);
3556 vcb
->volumeNameEncodingHint
= new_desc
.cd_encoding
;
3557 cp
->c_touch_chgtime
= TRUE
;
3567 * Get file system attributes.
3570 hfs_vfs_setattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
3572 kauth_cred_t cred
= vfs_context_ucred(context
);
3576 * Must be superuser or owner of filesystem to change volume attributes
3578 if (!kauth_cred_issuser(cred
) && (kauth_cred_getuid(cred
) != vfs_statfs(mp
)->f_owner
))
3581 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
3584 error
= hfs_vfs_root(mp
, &root_vp
, context
);
3588 error
= hfs_rename_volume(root_vp
, fsap
->f_vol_name
, vfs_context_proc(context
));
3589 (void) vnode_put(root_vp
);
3593 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
3602 * hfs vfs operations.
3604 struct vfsops hfs_vfsops
= {
3610 hfs_vfs_getattr
, /* was hfs_statfs */