2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1991, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
62 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
64 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kauth.h>
72 #include <sys/vnode_internal.h>
73 #include <sys/mount_internal.h>
74 #include <sys/sysctl.h>
75 #include <sys/malloc.h>
77 #include <sys/quota.h>
79 #include <sys/paths.h>
80 #include <sys/utfconv.h>
81 #include <sys/kdebug.h>
83 #include <kern/locks.h>
85 #include <vfs/vfs_journal.h>
87 #include <miscfs/specfs/specdev.h>
88 #include <hfs/hfs_mount.h>
91 #include "hfs_catalog.h"
92 #include "hfs_cnode.h"
94 #include "hfs_endian.h"
95 #include "hfs_hotfiles.h"
96 #include "hfs_quota.h"
98 #include "hfscommon/headers/FileMgrInternal.h"
99 #include "hfscommon/headers/BTreesInternal.h"
108 lck_grp_attr_t
* hfs_group_attr
;
109 lck_attr_t
* hfs_lock_attr
;
110 lck_grp_t
* hfs_mutex_group
;
111 lck_grp_t
* hfs_rwlock_group
;
114 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
116 extern void hfs_converterinit(void);
118 extern void inittodr(time_t base
);
120 extern int hfs_write_access(struct vnode
*, kauth_cred_t
, struct proc
*, Boolean
);
123 static int hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
);
124 static int hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
);
125 static int hfs_flushfiles(struct mount
*, int, struct proc
*);
126 static int hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
127 static int hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
);
128 static int hfs_init(struct vfsconf
*vfsp
);
129 static int hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
);
130 static int hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
, vfs_context_t context
);
131 static int hfs_reload(struct mount
*mp
, kauth_cred_t cred
, struct proc
*p
);
132 static int hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, vfs_context_t context
);
133 static int hfs_quotactl(struct mount
*, int, uid_t
, caddr_t
, vfs_context_t context
);
134 static int hfs_start(struct mount
*mp
, int flags
, vfs_context_t context
);
135 static int hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, vfs_context_t context
);
136 static int hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
);
137 static int hfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
138 user_addr_t newp
, size_t newlen
, vfs_context_t context
);
139 static int hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
);
140 static int hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, vfs_context_t context
);
141 static int hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
143 static int hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
);
147 * Called by vfs_mountroot when mounting HFS Plus as root.
152 hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
)
154 struct hfsmount
*hfsmp
;
156 struct vfsstatfs
*vfsp
;
159 if ((error
= hfs_mountfs(rvp
, mp
, NULL
, context
)))
163 hfsmp
= VFSTOHFS(mp
);
165 hfsmp
->hfs_uid
= UNKNOWNUID
;
166 hfsmp
->hfs_gid
= UNKNOWNGID
;
167 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
168 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
170 /* Establish the free block reserve. */
171 vcb
= HFSTOVCB(hfsmp
);
172 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
173 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
175 vfsp
= vfs_statfs(mp
);
176 (void)hfs_statfs(mp
, vfsp
, NULL
);
189 hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
191 struct proc
*p
= vfs_context_proc(context
);
192 struct hfsmount
*hfsmp
= NULL
;
193 struct hfs_mount_args args
;
197 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
)))) {
200 cmdflags
= (uint32_t)vfs_flags(mp
) & MNT_CMDFLAGS
;
201 if (cmdflags
& MNT_UPDATE
) {
202 hfsmp
= VFSTOHFS(mp
);
204 /* Reload incore data after an fsck. */
205 if (cmdflags
& MNT_RELOAD
) {
206 if (vfs_isrdonly(mp
))
207 return hfs_reload(mp
, vfs_context_ucred(context
), p
);
212 /* Change to a read-only file system. */
213 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
217 /* use VFS_SYNC to push out System (btree) files */
218 retval
= VFS_SYNC(mp
, MNT_WAIT
, context
);
219 if (retval
&& ((cmdflags
& MNT_FORCE
) == 0))
223 if (cmdflags
& MNT_FORCE
)
226 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
228 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
229 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
231 /* also get the volume bitmap blocks */
233 if (vnode_mount(hfsmp
->hfs_devvp
) == mp
) {
234 retval
= hfs_fsync(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, p
);
236 vnode_get(hfsmp
->hfs_devvp
);
237 retval
= VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
238 vnode_put(hfsmp
->hfs_devvp
);
242 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
246 hfs_global_exclusive_lock_acquire(hfsmp
);
248 journal_close(hfsmp
->jnl
);
251 // Note: we explicitly don't want to shutdown
252 // access to the jvp because we may need
253 // it later if we go back to being read-write.
255 hfs_global_exclusive_lock_release(hfsmp
);
259 /* Change to a writable file system. */
260 if (vfs_iswriteupgrade(mp
)) {
261 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
262 if (retval
!= E_NONE
)
265 // If the journal was shut-down previously because we were
266 // asked to be read-only, let's start it back up again now
268 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
269 && hfsmp
->jnl
== NULL
270 && hfsmp
->jvp
!= NULL
) {
273 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
274 jflags
= JOURNAL_RESET
;
279 hfs_global_exclusive_lock_acquire(hfsmp
);
281 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
282 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
285 hfsmp
->hfs_phys_block_size
,
288 hfs_sync_metadata
, hfsmp
->hfs_mp
);
290 hfs_global_exclusive_lock_release(hfsmp
);
292 if (hfsmp
->jnl
== NULL
) {
296 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
301 /* Only clear HFS_READ_ONLY after a successfull write */
302 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
304 if (!(hfsmp
->hfs_flags
& (HFS_READ_ONLY
& HFS_STANDARD
))) {
305 /* setup private/hidden directory for unlinked files */
306 FindMetaDataDirectory(HFSTOVCB(hfsmp
));
307 hfs_remove_orphans(hfsmp
);
310 * Allow hot file clustering if conditions allow.
312 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
313 (void) hfs_recording_init(hfsmp
);
318 /* Update file system parameters. */
319 retval
= hfs_changefs(mp
, &args
);
321 } else /* not an update request */ {
323 /* Set the mount flag to indicate that we support volfs */
324 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_DOVOLFS
));
326 retval
= hfs_mountfs(devvp
, mp
, &args
, context
);
330 (void)hfs_statfs(mp
, vfs_statfs(mp
), context
);
336 struct hfs_changefs_cargs
{
337 struct hfsmount
*hfsmp
;
344 hfs_changefs_callback(struct vnode
*vp
, void *cargs
)
348 struct cat_desc cndesc
;
349 struct cat_attr cnattr
;
350 struct hfs_changefs_cargs
*args
;
352 args
= (struct hfs_changefs_cargs
*)cargs
;
355 vcb
= HFSTOVCB(args
->hfsmp
);
357 if (cat_lookup(args
->hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
, NULL
)) {
359 * If we couldn't find this guy skip to the next one
364 return (VNODE_RETURNED
);
367 * Get the real uid/gid and perm mask from disk.
369 if (args
->permswitch
|| args
->permfix
) {
370 cp
->c_uid
= cnattr
.ca_uid
;
371 cp
->c_gid
= cnattr
.ca_gid
;
372 cp
->c_mode
= cnattr
.ca_mode
;
375 * If we're switching name converters then...
376 * Remove the existing entry from the namei cache.
377 * Update name to one based on new encoder.
381 replace_desc(cp
, &cndesc
);
383 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
384 strncpy(vcb
->vcbVN
, cp
->c_desc
.cd_nameptr
, NAME_MAX
);
385 cp
->c_desc
.cd_encoding
= args
->hfsmp
->hfs_encoding
;
388 cat_releasedesc(&cndesc
);
390 return (VNODE_RETURNED
);
393 /* Change fs mount parameters */
395 hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
)
398 int namefix
, permfix
, permswitch
;
399 struct hfsmount
*hfsmp
;
401 hfs_to_unicode_func_t get_unicode_func
;
402 unicode_to_hfs_func_t get_hfsname_func
;
403 u_long old_encoding
= 0;
404 struct hfs_changefs_cargs cargs
;
405 uint32_t mount_flags
;
407 hfsmp
= VFSTOHFS(mp
);
408 vcb
= HFSTOVCB(hfsmp
);
409 mount_flags
= (unsigned int)vfs_flags(mp
);
411 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
412 ((mount_flags
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
413 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
414 (mount_flags
& MNT_UNKNOWNPERMISSIONS
)));
416 /* The root filesystem must operate with actual permissions: */
417 if (permswitch
&& (mount_flags
& MNT_ROOTFS
) && (mount_flags
& MNT_UNKNOWNPERMISSIONS
)) {
418 vfs_clearflags(mp
, (uint64_t)((unsigned int)MNT_UNKNOWNPERMISSIONS
)); /* Just say "No". */
421 if (mount_flags
& MNT_UNKNOWNPERMISSIONS
)
422 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
424 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
426 namefix
= permfix
= 0;
428 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
429 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
430 gTimeZone
= args
->hfs_timezone
;
433 /* Change the default uid, gid and/or mask */
434 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
435 hfsmp
->hfs_uid
= args
->hfs_uid
;
436 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
439 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
440 hfsmp
->hfs_gid
= args
->hfs_gid
;
441 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
444 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
445 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
446 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
447 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
448 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
449 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
450 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
455 /* Change the hfs encoding value (hfs only) */
456 if ((vcb
->vcbSigWord
== kHFSSigWord
) &&
457 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
458 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
460 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
465 * Connect the new hfs_get_unicode converter but leave
466 * the old hfs_get_hfsname converter in place so that
467 * we can lookup existing vnodes to get their correctly
470 * When we're all finished, we can then connect the new
471 * hfs_get_hfsname converter and release our interest
472 * in the old converters.
474 hfsmp
->hfs_get_unicode
= get_unicode_func
;
475 old_encoding
= hfsmp
->hfs_encoding
;
476 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
480 if (!(namefix
|| permfix
|| permswitch
))
483 /* XXX 3762912 hack to support HFS filesystem 'owner' */
486 hfsmp
->hfs_uid
== UNKNOWNUID
? KAUTH_UID_NONE
: hfsmp
->hfs_uid
,
487 hfsmp
->hfs_gid
== UNKNOWNGID
? KAUTH_GID_NONE
: hfsmp
->hfs_gid
);
490 * For each active vnode fix things that changed
492 * Note that we can visit a vnode more than once
493 * and we can race with fsync.
495 * hfs_changefs_callback will be called for each vnode
496 * hung off of this mount point
498 * properly referenced and unreferenced around the callback
501 cargs
.namefix
= namefix
;
502 cargs
.permfix
= permfix
;
503 cargs
.permswitch
= permswitch
;
505 vnode_iterate(mp
, 0, hfs_changefs_callback
, (void *)&cargs
);
508 * If we're switching name converters we can now
509 * connect the new hfs_get_hfsname converter and
510 * release our interest in the old converters.
513 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
514 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
515 (void) hfs_relconverter(old_encoding
);
522 struct hfs_reload_cargs
{
523 struct hfsmount
*hfsmp
;
530 hfs_reload_callback(struct vnode
*vp
, void *cargs
)
533 struct hfs_reload_cargs
*args
;
535 args
= (struct hfs_reload_cargs
*)cargs
;
537 * flush all the buffers associated with this node
539 (void) buf_invalidateblks(vp
, 0, 0, 0);
543 * Remove any directory hints
546 hfs_reldirhints(cp
, 0);
549 * Re-read cnode data for all active vnodes (non-metadata files).
551 if (!vnode_issystem(vp
) && !VNODE_IS_RSRC(vp
)) {
552 struct cat_fork
*datafork
;
553 struct cat_desc desc
;
555 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
557 /* lookup by fileID since name could have changed */
558 if ((args
->error
= cat_idlookup(args
->hfsmp
, cp
->c_fileid
, &desc
, &cp
->c_attr
, datafork
)))
559 return (VNODE_RETURNED_DONE
);
561 /* update cnode's catalog descriptor */
562 (void) replace_desc(cp
, &desc
);
564 return (VNODE_RETURNED
);
568 * Reload all incore data for a filesystem (used after running fsck on
569 * the root filesystem and finding things to fix). The filesystem must
570 * be mounted read-only.
572 * Things to do to update the mount:
573 * invalidate all cached meta-data.
574 * invalidate all inactive vnodes.
575 * invalidate all cached file data.
576 * re-read volume header from disk.
577 * re-load meta-file info (extents, file size).
578 * re-load B-tree header data.
579 * re-read cnode data for all active vnodes.
582 hfs_reload(struct mount
*mountp
, kauth_cred_t cred
, struct proc
*p
)
584 register struct vnode
*devvp
;
588 struct hfsmount
*hfsmp
;
589 struct HFSPlusVolumeHeader
*vhp
;
591 struct filefork
*forkp
;
592 struct cat_desc cndesc
;
593 struct hfs_reload_cargs args
;
595 hfsmp
= VFSTOHFS(mountp
);
596 vcb
= HFSTOVCB(hfsmp
);
598 if (vcb
->vcbSigWord
== kHFSSigWord
)
599 return (EINVAL
); /* rooting from HFS is not supported! */
602 * Invalidate all cached meta-data.
604 devvp
= hfsmp
->hfs_devvp
;
605 if (buf_invalidateblks(devvp
, 0, 0, 0))
606 panic("hfs_reload: dirty1");
613 * hfs_reload_callback will be called for each vnode
614 * hung off of this mount point that can't be recycled...
615 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
616 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
617 * properly referenced and unreferenced around the callback
619 vnode_iterate(mountp
, VNODE_RELOAD
| VNODE_WAIT
, hfs_reload_callback
, (void *)&args
);
625 * Re-read VolumeHeader from disk.
627 sectorsize
= hfsmp
->hfs_phys_block_size
;
629 error
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
630 (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
)),
631 sectorsize
, NOCRED
, &bp
);
638 vhp
= (HFSPlusVolumeHeader
*) (buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
640 /* Do a quick sanity check */
641 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
642 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
643 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
644 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
645 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
650 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
651 vcb
->vcbAtrb
= SWAP_BE32 (vhp
->attributes
);
652 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
653 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
654 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
655 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
656 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
657 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
658 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
659 vcb
->nextAllocation
= SWAP_BE32 (vhp
->nextAllocation
);
660 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
661 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
662 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
663 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
664 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
667 * Re-load meta-file vnode data (extent info, file size, etc).
669 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
670 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
671 forkp
->ff_extents
[i
].startBlock
=
672 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
673 forkp
->ff_extents
[i
].blockCount
=
674 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
676 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
677 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
678 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
681 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
682 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
683 forkp
->ff_extents
[i
].startBlock
=
684 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
685 forkp
->ff_extents
[i
].blockCount
=
686 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
688 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
689 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
690 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
692 if (hfsmp
->hfs_attribute_vp
) {
693 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
694 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
695 forkp
->ff_extents
[i
].startBlock
=
696 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
697 forkp
->ff_extents
[i
].blockCount
=
698 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
700 forkp
->ff_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
701 forkp
->ff_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
702 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
705 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
706 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
707 forkp
->ff_extents
[i
].startBlock
=
708 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
709 forkp
->ff_extents
[i
].blockCount
=
710 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
712 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
713 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
714 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
720 * Re-load B-tree header data
722 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
723 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
726 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
727 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
730 if (hfsmp
->hfs_attribute_vp
) {
731 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
732 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
736 /* Reload the volume name */
737 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, NULL
, NULL
)))
739 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
740 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
741 cat_releasedesc(&cndesc
);
743 /* Re-establish private/hidden directory for unlinked files */
744 FindMetaDataDirectory(vcb
);
746 /* In case any volume information changed to trigger a notification */
747 hfs_generate_volume_notifications(hfsmp
);
754 * Common code for mount and mountroot
757 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
,
758 vfs_context_t context
)
760 struct proc
*p
= vfs_context_proc(context
);
762 struct hfsmount
*hfsmp
;
765 HFSMasterDirectoryBlock
*mdbp
;
773 u_int32_t minblksize
;
774 u_int32_t iswritable
;
775 daddr64_t mdb_offset
;
777 ronly
= vfs_isrdonly(mp
);
778 dev
= vnode_specrdev(devvp
);
779 cred
= p
? vfs_context_ucred(context
) : NOCRED
;
785 minblksize
= kHFSBlockSize
;
787 /* Advisory locking should be handled at the VFS layer */
788 vfs_setlocklocal(mp
);
790 /* Get the real physical block size. */
791 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, context
)) {
795 /* Switch to 512 byte sectors (temporarily) */
797 u_int32_t size512
= 512;
799 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, context
)) {
804 /* Get the number of 512 byte physical blocks. */
805 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
809 /* Compute an accurate disk size (i.e. within 512 bytes) */
810 disksize
= (u_int64_t
)blkcnt
* (u_int64_t
)512;
813 * On Tiger it is not necessary to switch the device
814 * block size to be 4k if there are more than 31-bits
815 * worth of blocks but to insure compatibility with
816 * pre-Tiger systems we have to do it.
818 if (blkcnt
> (u_int64_t
)0x000000007fffffff) {
819 minblksize
= blksize
= 4096;
822 /* Now switch to our prefered physical block size. */
824 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
828 /* Get the count of physical blocks. */
829 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
836 * minblksize is the minimum physical block size
837 * blksize has our prefered physical block size
838 * blkcnt has the total number of physical blocks
841 mdb_offset
= (daddr64_t
)HFS_PRI_SECTOR(blksize
);
842 if ((retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
))) {
845 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
846 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
850 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
851 bzero(hfsmp
, sizeof(struct hfsmount
));
854 * Init the volume information structure
857 lck_mtx_init(&hfsmp
->hfs_mutex
, hfs_mutex_group
, hfs_lock_attr
);
858 lck_mtx_init(&hfsmp
->hfc_mutex
, hfs_mutex_group
, hfs_lock_attr
);
859 lck_rw_init(&hfsmp
->hfs_global_lock
, hfs_rwlock_group
, hfs_lock_attr
);
860 lck_rw_init(&hfsmp
->hfs_insync
, hfs_rwlock_group
, hfs_lock_attr
);
862 vfs_setfsprivate(mp
, hfsmp
);
863 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
864 hfsmp
->hfs_raw_dev
= vnode_specrdev(devvp
);
865 hfsmp
->hfs_devvp
= devvp
;
866 hfsmp
->hfs_phys_block_size
= blksize
;
867 hfsmp
->hfs_phys_block_count
= blkcnt
;
868 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
870 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
871 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)
872 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
873 for (i
= 0; i
< MAXQUOTAS
; i
++)
874 dqfileinit(&hfsmp
->hfs_qfiles
[i
]);
877 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
878 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
879 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
880 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
881 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
882 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
883 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
884 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
885 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
887 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
890 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
891 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
893 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
896 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
897 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
) {
898 hfsmp
->hfs_uid
= UNKNOWNUID
;
899 hfsmp
->hfs_gid
= UNKNOWNGID
;
900 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
901 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
902 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
906 /* Find out if disk media is writable. */
907 if (VNOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, context
) == 0) {
909 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
911 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
914 // record the current time at which we're mounting this volume
918 hfsmp
->hfs_mount_time
= tv
.tv_sec
;
921 /* Mount a standard HFS disk */
922 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
923 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
924 if ((vfs_flags(mp
) & MNT_ROOTFS
)) {
925 retval
= EINVAL
; /* Cannot root from HFS standard disks */
928 /* HFS disks can only use 512 byte physical blocks */
929 if (blksize
> kHFSBlockSize
) {
930 blksize
= kHFSBlockSize
;
931 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
935 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
939 hfsmp
->hfs_phys_block_size
= blksize
;
940 hfsmp
->hfs_phys_block_count
= blkcnt
;
943 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
944 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
946 /* establish the timezone */
947 gTimeZone
= args
->hfs_timezone
;
950 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
951 &hfsmp
->hfs_get_hfsname
);
955 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
957 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
959 } else /* Mount an HFS Plus disk */ {
960 HFSPlusVolumeHeader
*vhp
;
961 off_t embeddedOffset
;
964 /* Get the embedded Volume Header */
965 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
966 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
967 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
968 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
971 * If the embedded volume doesn't start on a block
972 * boundary, then switch the device to a 512-byte
973 * block size so everything will line up on a block
976 if ((embeddedOffset
% blksize
) != 0) {
977 printf("HFS Mount: embedded volume offset not"
978 " a multiple of physical block size (%d);"
979 " switching to 512\n", blksize
);
981 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
982 (caddr_t
)&blksize
, FWRITE
, context
)) {
986 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
987 (caddr_t
)&blkcnt
, 0, context
)) {
991 /* Note: relative block count adjustment */
992 hfsmp
->hfs_phys_block_count
*=
993 hfsmp
->hfs_phys_block_size
/ blksize
;
994 hfsmp
->hfs_phys_block_size
= blksize
;
997 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
998 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1000 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1002 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1003 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1006 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1009 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1011 } else /* pure HFS+ */ {
1013 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1020 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) && args
->journal_disable
) {
1025 // We only initialize the journal here if the last person
1026 // to mount this volume was journaling aware. Otherwise
1027 // we delay journal initialization until later at the end
1028 // of hfs_MountHFSPlusVolume() because the last person who
1029 // mounted it could have messed things up behind our back
1030 // (so we need to go find the .journal file, make sure it's
1031 // the right size, re-sync up if it was moved, etc).
1033 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1034 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1037 // if we're able to init the journal, mark the mount
1038 // point as journaled.
1040 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1041 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
1043 // if the journal failed to open, then set the lastMountedVersion
1044 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1045 // of just bailing out because the volume is journaled.
1047 HFSPlusVolumeHeader
*jvhp
;
1049 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1051 if (mdb_offset
== 0) {
1052 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1056 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1058 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1060 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1061 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1062 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1070 // clear this so the error exit path won't try to use it
1075 // if this isn't the root device just bail out.
1076 // If it is the root device we just continue on
1077 // in the hopes that fsck_hfs will be able to
1078 // fix any damage that exists on the volume.
1079 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1087 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1089 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1091 * If the backend didn't like our physical blocksize
1092 * then retry with physical blocksize of 512.
1094 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1095 printf("HFS Mount: could not use physical block size "
1096 "(%d) switching to 512\n", blksize
);
1098 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, context
)) {
1102 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, context
)) {
1106 devvp
->v_specsize
= blksize
;
1107 /* Note: relative block count adjustment (in case this is an embedded volume). */
1108 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1109 hfsmp
->hfs_phys_block_size
= blksize
;
1112 // close and re-open this with the new block size
1113 journal_close(hfsmp
->jnl
);
1115 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1116 vfs_setflags(mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
1118 // if the journal failed to open, then set the lastMountedVersion
1119 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1120 // of just bailing out because the volume is journaled.
1122 HFSPlusVolumeHeader
*jvhp
;
1124 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1126 if (mdb_offset
== 0) {
1127 mdb_offset
= (daddr64_t
)((embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
));
1131 retval
= (int)buf_meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1133 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(blksize
));
1135 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1136 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1137 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1145 // clear this so the error exit path won't try to use it
1150 // if this isn't the root device just bail out.
1151 // If it is the root device we just continue on
1152 // in the hopes that fsck_hfs will be able to
1153 // fix any damage that exists on the volume.
1154 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1161 /* Try again with a smaller block size... */
1162 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1165 (void) hfs_relconverter(0);
1168 // save off a snapshot of the mtime from the previous mount
1170 hfsmp
->hfs_last_mounted_mtime
= hfsmp
->hfs_mtime
;
1176 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (long)dev
;
1177 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
1178 vfs_setmaxsymlen(mp
, 0);
1179 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
1180 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSNATIVEXATTR
;
1184 * Set the free space warning levels for a non-root volume:
1186 * Set the lower freespace limit (the level that will trigger a warning)
1187 * to 5% of the volume size or 250MB, whichever is less, and the desired
1188 * level (which will cancel the alert request) to 1/2 above that limit.
1189 * Start looking for free space to drop below this level and generate a
1190 * warning immediately if needed:
1192 hfsmp
->hfs_freespace_notify_warninglimit
=
1193 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1194 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1195 hfsmp
->hfs_freespace_notify_desiredlevel
=
1196 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1197 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1200 * Set the free space warning levels for the root volume:
1202 * Set the lower freespace limit (the level that will trigger a warning)
1203 * to 1% of the volume size or 50MB, whichever is less, and the desired
1204 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1206 hfsmp
->hfs_freespace_notify_warninglimit
=
1207 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1208 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1209 hfsmp
->hfs_freespace_notify_desiredlevel
=
1210 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1211 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1215 * Start looking for free space to drop below this level and generate a
1216 * warning immediately if needed:
1218 hfsmp
->hfs_notification_conditions
= 0;
1219 hfs_generate_volume_notifications(hfsmp
);
1222 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1233 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1234 (void)VNOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, context
);
1238 FREE(hfsmp
, M_HFSMNT
);
1239 vfs_setfsprivate(mp
, NULL
);
1246 * Make a filesystem operational.
1247 * Nothing to do at the moment.
1251 hfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
1258 * unmount system call
1261 hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
)
1263 struct proc
*p
= vfs_context_proc(context
);
1264 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1265 int retval
= E_NONE
;
1272 if (mntflags
& MNT_FORCE
) {
1273 flags
|= FORCECLOSE
;
1277 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1280 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1281 (void) hfs_recording_suspend(hfsmp
);
1284 * Flush out the b-trees, volume bitmap and Volume Header
1286 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1287 hfs_start_transaction(hfsmp
);
1290 if (hfsmp
->hfs_attribute_vp
) {
1291 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), HFS_EXCLUSIVE_LOCK
);
1292 retval
= hfs_fsync(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, p
);
1293 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
1294 if (retval
&& !force
)
1298 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), HFS_EXCLUSIVE_LOCK
);
1299 retval
= hfs_fsync(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, p
);
1300 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
1301 if (retval
&& !force
)
1304 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
1305 retval
= hfs_fsync(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, p
);
1306 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
1307 if (retval
&& !force
)
1310 if (hfsmp
->hfs_allocation_vp
) {
1311 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
1312 retval
= hfs_fsync(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, p
);
1313 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
1314 if (retval
&& !force
)
1318 if (hfsmp
->hfc_filevp
&& vnode_issystem(hfsmp
->hfc_filevp
)) {
1319 retval
= hfs_fsync(hfsmp
->hfc_filevp
, MNT_WAIT
, 0, p
);
1320 if (retval
&& !force
)
1324 /* See if this volume is damaged, is so do not unmount cleanly */
1325 if (HFSTOVCB(hfsmp
)->vcbFlags
& kHFS_DamagedVolume
) {
1326 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1328 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1331 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1333 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1335 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1337 goto err_exit
; /* could not flush everything */
1340 hfs_end_transaction(hfsmp
);
1345 journal_flush(hfsmp
->jnl
);
1349 * Invalidate our caches and release metadata vnodes
1351 (void) hfsUnmount(hfsmp
, p
);
1354 * Last chance to dump unreferenced system files.
1356 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1358 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1359 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1363 journal_close(hfsmp
->jnl
);
1367 VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
1369 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1370 retval
= VNOP_CLOSE(hfsmp
->jvp
,
1371 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1373 vnode_put(hfsmp
->jvp
);
1378 #ifdef HFS_SPARSE_DEV
1379 /* Drop our reference on the backing fs (if any). */
1380 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1381 struct vnode
* tmpvp
;
1383 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1384 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1385 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1388 #endif /* HFS_SPARSE_DEV */
1389 lck_mtx_destroy(&hfsmp
->hfc_mutex
, hfs_mutex_group
);
1390 FREE(hfsmp
, M_HFSMNT
);
1396 hfs_end_transaction(hfsmp
);
1403 * Return the root of a filesystem.
1406 hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1408 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)kHFSRootFolderID
, vpp
, 1);
1413 * Do operations associated with quotas
1416 hfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t context
)
1418 struct proc
*p
= vfs_context_proc(context
);
1419 int cmd
, type
, error
;
1425 uid
= vfs_context_ucred(context
)->cr_ruid
;
1426 cmd
= cmds
>> SUBCMDSHIFT
;
1433 if (uid
== vfs_context_ucred(context
)->cr_ruid
)
1437 if ( (error
= vfs_context_suser(context
)) )
1441 type
= cmds
& SUBCMDMASK
;
1442 if ((u_int
)type
>= MAXQUOTAS
)
1444 if (vfs_busy(mp
, LK_NOWAIT
))
1450 error
= hfs_quotaon(p
, mp
, type
, datap
);
1454 error
= hfs_quotaoff(p
, mp
, type
);
1458 error
= hfs_setquota(mp
, uid
, type
, datap
);
1462 error
= hfs_setuse(mp
, uid
, type
, datap
);
1466 error
= hfs_getquota(mp
, uid
, type
, datap
);
1470 error
= hfs_qsync(mp
);
1474 error
= hfs_quotastat(mp
, type
, datap
);
1487 /* Subtype is composite of bits */
1488 #define HFS_SUBTYPE_JOURNALED 0x01
1489 #define HFS_SUBTYPE_CASESENSITIVE 0x02
1490 /* bits 2 - 6 reserved */
1491 #define HFS_SUBTYPE_STANDARDHFS 0x80
1494 * Get file system statistics.
1497 hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
1499 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1500 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1502 uint16_t subtype
= 0;
1504 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1506 sbp
->f_bsize
= (uint32_t)vcb
->blockSize
;
1507 sbp
->f_iosize
= (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
);
1508 sbp
->f_blocks
= (uint64_t)((unsigned long)vcb
->totalBlocks
);
1509 sbp
->f_bfree
= (uint64_t)((unsigned long )hfs_freeblks(hfsmp
, 0));
1510 sbp
->f_bavail
= (uint64_t)((unsigned long )hfs_freeblks(hfsmp
, 1));
1511 sbp
->f_files
= (uint64_t)((unsigned long )(vcb
->totalBlocks
- 2)); /* max files is constrained by total blocks */
1512 sbp
->f_ffree
= (uint64_t)((unsigned long )(MIN(freeCNIDs
, sbp
->f_bavail
)));
1515 * Subtypes (flavors) for HFS
1516 * 0: Mac OS Extended
1517 * 1: Mac OS Extended (Journaled)
1518 * 2: Mac OS Extended (Case Sensitive)
1519 * 3: Mac OS Extended (Case Sensitive, Journaled)
1521 * 128: Mac OS Standard
1524 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1525 subtype
= HFS_SUBTYPE_STANDARDHFS
;
1526 } else /* HFS Plus */ {
1528 subtype
|= HFS_SUBTYPE_JOURNALED
;
1529 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
1530 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
1532 sbp
->f_fssubtype
= subtype
;
1539 // XXXdbg -- this is a callback to be used by the journal to
1540 // get meta data blocks flushed out to disk.
1542 // XXXdbg -- be smarter and don't flush *every* block on each
1543 // call. try to only flush some so we don't wind up
1544 // being too synchronous.
1548 hfs_sync_metadata(void *arg
)
1550 struct mount
*mp
= (struct mount
*)arg
;
1551 struct hfsmount
*hfsmp
;
1554 int sectorsize
, retval
;
1555 daddr64_t priIDSector
;
1556 hfsmp
= VFSTOHFS(mp
);
1557 vcb
= HFSTOVCB(hfsmp
);
1559 // now make sure the super block is flushed
1560 sectorsize
= hfsmp
->hfs_phys_block_size
;
1561 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1562 HFS_PRI_SECTOR(sectorsize
));
1563 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1565 panic("hfs: sync_metadata: can't read super-block?! (retval 0x%x, priIDSector)\n",
1566 retval
, priIDSector
);
1569 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1575 // the alternate super block...
1576 // XXXdbg - we probably don't need to do this each and every time.
1577 // hfs_btreeio.c:FlushAlternate() should flag when it was
1579 if (hfsmp
->hfs_alt_id_sector
) {
1580 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &bp
);
1581 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1590 struct hfs_sync_cargs
{
1599 hfs_sync_callback(struct vnode
*vp
, void *cargs
)
1602 struct hfs_sync_cargs
*args
;
1605 args
= (struct hfs_sync_cargs
*)cargs
;
1607 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1608 return (VNODE_RETURNED
);
1612 if ((cp
->c_flag
& C_MODIFIED
) ||
1613 (cp
->c_touch_acctime
| cp
->c_touch_chgtime
| cp
->c_touch_modtime
) ||
1614 vnode_hasdirtyblks(vp
)) {
1615 error
= hfs_fsync(vp
, args
->waitfor
, 0, args
->p
);
1618 args
->error
= error
;
1621 return (VNODE_RETURNED
);
1627 * Go through the disk queues to initiate sandbagged IO;
1628 * go through the inodes to write those that have been modified;
1629 * initiate the writing of the super block if it has been modified.
1631 * Note: we are always called with the filesystem marked `MPBUSY'.
1634 hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
)
1636 struct proc
*p
= vfs_context_proc(context
);
1638 struct hfsmount
*hfsmp
;
1640 struct vnode
*meta_vp
[4];
1642 int error
, allerror
= 0;
1643 struct hfs_sync_cargs args
;
1646 * During MNT_UPDATE hfs_changefs might be manipulating
1647 * vnodes so back off
1649 if (((uint32_t)vfs_flags(mp
)) & MNT_UPDATE
) /* XXX MNT_UPDATE may not be visible here */
1652 hfsmp
= VFSTOHFS(mp
);
1653 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1656 /* skip over frozen volumes */
1657 if (!lck_rw_try_lock_shared(&hfsmp
->hfs_insync
))
1660 args
.cred
= vfs_context_proc(context
);
1661 args
.waitfor
= waitfor
;
1665 * hfs_sync_callback will be called for each vnode
1666 * hung off of this mount point... the vnode will be
1667 * properly referenced and unreferenced around the callback
1669 vnode_iterate(mp
, 0, hfs_sync_callback
, (void *)&args
);
1672 allerror
= args
.error
;
1674 vcb
= HFSTOVCB(hfsmp
);
1676 meta_vp
[0] = vcb
->extentsRefNum
;
1677 meta_vp
[1] = vcb
->catalogRefNum
;
1678 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1679 meta_vp
[3] = hfsmp
->hfs_attribute_vp
; /* Optional file */
1681 /* Now sync our three metadata files */
1682 for (i
= 0; i
< 4; ++i
) {
1686 if ((btvp
==0) || (vnode_mount(btvp
) != mp
))
1689 /* XXX use hfs_systemfile_lock instead ? */
1690 (void) hfs_lock(VTOC(btvp
), HFS_EXCLUSIVE_LOCK
);
1693 if (((cp
->c_flag
& C_MODIFIED
) == 0) &&
1694 (cp
->c_touch_acctime
== 0) &&
1695 (cp
->c_touch_chgtime
== 0) &&
1696 (cp
->c_touch_modtime
== 0) &&
1697 vnode_hasdirtyblks(btvp
) == 0) {
1698 hfs_unlock(VTOC(btvp
));
1701 error
= vnode_get(btvp
);
1703 hfs_unlock(VTOC(btvp
));
1706 if ((error
= hfs_fsync(btvp
, waitfor
, 0, p
)))
1714 * Force stale file system control information to be flushed.
1716 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1717 if ((error
= VNOP_FSYNC(hfsmp
->hfs_devvp
, waitfor
, context
))) {
1725 hfs_hotfilesync(hfsmp
, p
);
1727 * Write back modified superblock.
1730 if (IsVCBDirty(vcb
)) {
1731 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1737 journal_flush(hfsmp
->jnl
);
1740 lck_rw_unlock_shared(&hfsmp
->hfs_insync
);
1746 * File handle to vnode
1748 * Have to be really careful about stale file handles:
1749 * - check that the cnode id is valid
1750 * - call hfs_vget() to get the locked cnode
1751 * - check for an unallocated cnode (i_mode == 0)
1752 * - check that the given client host has export rights and return
1753 * those rights via. exflagsp and credanonp
1756 hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
)
1758 struct hfsfid
*hfsfhp
;
1763 hfsfhp
= (struct hfsfid
*)fhp
;
1765 if (fhlen
< sizeof(struct hfsfid
))
1768 result
= hfs_vget(VFSTOHFS(mp
), hfsfhp
->hfsfid_cnid
, &nvp
, 0);
1770 if (result
== ENOENT
)
1775 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1776 * For NFS, we are assuming that only if the createtime was moved
1777 * forward would it mean the fileID got reused in that session by
1778 * wrapping. We don't have a volume ID or other unique identifier to
1779 * to use here for a generation ID across reboots, crashes where
1780 * metadata noting lastFileID didn't make it to disk but client has
1781 * it, or volume erasures where fileIDs start over again. Lastly,
1782 * with HFS allowing "wraps" of fileIDs now, this becomes more
1783 * error prone. Future, would be change the "wrap bit" to a unique
1784 * wrap number and use that for generation number. For now do this.
1786 if ((hfsfhp
->hfsfid_gen
< VTOC(nvp
)->c_itime
)) {
1787 hfs_unlock(VTOC(nvp
));
1793 hfs_unlock(VTOC(nvp
));
1799 * Vnode pointer to File handle
1803 hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
)
1806 struct hfsfid
*hfsfhp
;
1808 if (ISHFS(VTOVCB(vp
)))
1809 return (ENOTSUP
); /* hfs standard is not exportable */
1811 if (*fhlenp
< (int)sizeof(struct hfsfid
))
1815 hfsfhp
= (struct hfsfid
*)fhp
;
1816 hfsfhp
->hfsfid_cnid
= cp
->c_fileid
;
1817 hfsfhp
->hfsfid_gen
= cp
->c_itime
;
1818 *fhlenp
= sizeof(struct hfsfid
);
1825 * Initial HFS filesystems, done only once.
1828 hfs_init(__unused
struct vfsconf
*vfsp
)
1830 static int done
= 0;
1836 hfs_converterinit();
1844 hfs_lock_attr
= lck_attr_alloc_init();
1845 hfs_group_attr
= lck_grp_attr_alloc_init();
1846 hfs_mutex_group
= lck_grp_alloc_init("hfs-mutex", hfs_group_attr
);
1847 hfs_rwlock_group
= lck_grp_alloc_init("hfs-rwlock", hfs_group_attr
);
1849 /* Turn on lock debugging */
1850 //lck_attr_setdebug(hfs_lock_attr);
1857 hfs_getmountpoint(vp
, hfsmpp
)
1859 struct hfsmount
**hfsmpp
;
1861 struct hfsmount
* hfsmp
;
1862 char fstypename
[MFSNAMELEN
];
1867 if (!vnode_isvroot(vp
))
1870 vnode_vfsname(vp
, fstypename
);
1871 if (strcmp(fstypename
, "hfs") != 0)
1876 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1885 #include <sys/filedesc.h>
1888 * HFS filesystem related variables.
1891 hfs_sysctl(int *name
, __unused u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
1892 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
1894 struct proc
*p
= vfs_context_proc(context
);
1896 struct hfsmount
*hfsmp
;
1898 /* all sysctl names at this level are terminal */
1900 if (name
[0] == HFS_ENCODINGBIAS
) {
1903 bias
= hfs_getencodingbias();
1904 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
1905 if (error
== 0 && newp
)
1906 hfs_setencodingbias(bias
);
1909 } else if (name
[0] == HFS_EXTEND_FS
) {
1911 vnode_t vp
= p
->p_fd
->fd_cdir
;
1913 if (newp
== USER_ADDR_NULL
|| vp
== NULL
)
1915 if ((error
= hfs_getmountpoint(vp
, &hfsmp
)))
1917 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, &newsize
);
1921 error
= hfs_extendfs(hfsmp
, newsize
, context
);
1924 } else if (name
[0] == HFS_ENCODINGHINT
) {
1928 u_int16_t
*unicode_name
;
1931 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
1932 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
1933 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
1935 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
1937 error
= utf8_decodestr(filename
, newlen
- 1, unicode_name
,
1938 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
1940 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
1941 error
= sysctl_int(oldp
, oldlenp
, USER_ADDR_NULL
, 0, &hint
);
1944 FREE(unicode_name
, M_TEMP
);
1945 FREE(filename
, M_TEMP
);
1948 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
1949 // make the file system journaled...
1950 struct vnode
*vp
= p
->p_fd
->fd_cdir
, *jvp
;
1952 struct cat_attr jnl_attr
, jinfo_attr
;
1953 struct cat_fork jnl_fork
, jinfo_fork
;
1957 /* Only root can enable journaling */
1965 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
1968 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
1969 printf("hfs: can't make a plain hfs volume journaled.\n");
1974 printf("hfs: volume @ mp 0x%x is already journaled!\n", vnode_mount(vp
));
1978 vcb
= HFSTOVCB(hfsmp
);
1979 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
1980 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
1981 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
1983 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
1984 hfs_systemfile_unlock(hfsmp
, lockflags
);
1987 hfs_systemfile_unlock(hfsmp
, lockflags
);
1989 // make sure these both exist!
1990 if ( GetFileInfo(vcb
, kHFSRootFolderID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
1991 || GetFileInfo(vcb
, kHFSRootFolderID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
1996 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, context
);
1998 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1999 (off_t
)name
[2], (off_t
)name
[3]);
2001 jvp
= hfsmp
->hfs_devvp
;
2002 jnl
= journal_create(jvp
,
2003 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2004 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2005 (off_t
)((unsigned)name
[3]),
2007 hfsmp
->hfs_phys_block_size
,
2010 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2013 printf("hfs: FAILED to create the journal!\n");
2014 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2015 VNOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2022 hfs_global_exclusive_lock_acquire(hfsmp
);
2024 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2025 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2029 // save this off for the hack-y check in hfs_remove()
2030 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2031 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2032 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2033 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2035 vfs_setflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
2037 hfs_global_exclusive_lock_release(hfsmp
);
2038 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2041 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2042 // clear the journaling bit
2043 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2045 /* Only root can disable journaling */
2054 printf("hfs: disabling journaling for mount @ 0x%x\n", vnode_mount(vp
));
2056 hfs_global_exclusive_lock_acquire(hfsmp
);
2058 // Lights out for you buddy!
2059 journal_close(hfsmp
->jnl
);
2062 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2063 VNOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2066 vfs_clearflags(hfsmp
->hfs_mp
, (uint64_t)((unsigned int)MNT_JOURNALED
));
2067 hfsmp
->jnl_start
= 0;
2068 hfsmp
->hfs_jnlinfoblkid
= 0;
2069 hfsmp
->hfs_jnlfileid
= 0;
2071 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2073 hfs_global_exclusive_lock_release(hfsmp
);
2074 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2077 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2078 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
2079 off_t jnl_start
, jnl_size
;
2085 if (hfsmp
->jnl
== NULL
) {
2089 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2090 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2093 if ((error
= copyout((caddr_t
)&jnl_start
, CAST_USER_ADDR_T(name
[1]), sizeof(off_t
))) != 0) {
2096 if ((error
= copyout((caddr_t
)&jnl_size
, CAST_USER_ADDR_T(name
[2]), sizeof(off_t
))) != 0) {
2101 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2103 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2105 } else if (name
[0] == VFS_CTL_QUERY
) {
2106 struct sysctl_req
*req
;
2108 struct user_vfsidctl user_vc
;
2111 boolean_t is_64_bit
;
2113 is_64_bit
= proc_is64bit(p
);
2114 req
= CAST_DOWN(struct sysctl_req
*, oldp
); /* we're new style vfs sysctl. */
2117 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2118 if (error
) return (error
);
2120 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
2123 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2124 if (error
) return (error
);
2126 mp
= vfs_getvfs(&vc
.vc_fsid
);
2128 if (mp
== NULL
) return (ENOENT
);
2130 hfsmp
= VFSTOHFS(mp
);
2131 bzero(&vq
, sizeof(vq
));
2132 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2133 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2141 hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, __unused vfs_context_t context
)
2143 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)ino
, vpp
, 1);
2148 * Look up an HFS object by ID.
2150 * The object is returned with an iocount reference and the cnode locked.
2152 * If the object is a file then it will represent the data fork.
2156 hfs_vget(struct hfsmount
*hfsmp
, cnid_t cnid
, struct vnode
**vpp
, int skiplock
)
2158 struct vnode
*vp
= NULL
;
2159 struct cat_desc cndesc
;
2160 struct cat_attr cnattr
;
2161 struct cat_fork cnfork
;
2162 struct componentname cn
;
2163 u_int32_t linkref
= 0;
2166 /* Check for cnids that should't be exported. */
2167 if ((cnid
< kHFSFirstUserCatalogNodeID
)
2168 && (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
))
2171 /* Don't export HFS Private Data dir. */
2172 if (cnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
)
2176 * Check the hash first
2178 vp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, cnid
, 0, skiplock
);
2184 bzero(&cndesc
, sizeof(cndesc
));
2185 bzero(&cnattr
, sizeof(cnattr
));
2186 bzero(&cnfork
, sizeof(cnfork
));
2189 * Not in hash, lookup in catalog
2191 if (cnid
== kHFSRootParentID
) {
2192 static char hfs_rootname
[] = "/";
2194 cndesc
.cd_nameptr
= &hfs_rootname
[0];
2195 cndesc
.cd_namelen
= 1;
2196 cndesc
.cd_parentcnid
= kHFSRootParentID
;
2197 cndesc
.cd_cnid
= kHFSRootFolderID
;
2198 cndesc
.cd_flags
= CD_ISDIR
;
2200 cnattr
.ca_fileid
= kHFSRootFolderID
;
2201 cnattr
.ca_nlink
= 2;
2202 cnattr
.ca_entries
= 1;
2203 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
2207 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2208 error
= cat_idlookup(hfsmp
, cnid
, &cndesc
, &cnattr
, &cnfork
);
2209 hfs_systemfile_unlock(hfsmp
, lockflags
);
2217 * If we just looked up a raw hardlink inode,
2218 * then finish initializing it.
2220 if ((cndesc
.cd_parentcnid
== hfsmp
->hfs_privdir_desc
.cd_cnid
) &&
2221 (bcmp(cndesc
.cd_nameptr
, HFS_INODE_PREFIX
, HFS_INODE_PREFIX_LEN
) == 0)) {
2222 linkref
= strtoul((const char*)&cndesc
.cd_nameptr
[HFS_INODE_PREFIX_LEN
], NULL
, 10);
2223 cnattr
.ca_rdev
= linkref
;
2225 // patch up the parentcnid
2226 if (cnattr
.ca_attrblks
!= 0) {
2227 cndesc
.cd_parentcnid
= cnattr
.ca_attrblks
;
2233 * Supply hfs_getnewvnode with a component name.
2235 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2236 cn
.cn_nameiop
= LOOKUP
;
2237 cn
.cn_flags
= ISLASTCN
| HASBUF
;
2238 cn
.cn_context
= NULL
;
2239 cn
.cn_pnlen
= MAXPATHLEN
;
2240 cn
.cn_nameptr
= cn
.cn_pnbuf
;
2241 cn
.cn_namelen
= cndesc
.cd_namelen
;
2244 bcopy(cndesc
.cd_nameptr
, cn
.cn_nameptr
, cndesc
.cd_namelen
+ 1);
2246 /* XXX should we supply the parent as well... ? */
2247 error
= hfs_getnewvnode(hfsmp
, NULLVP
, &cn
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2248 if (error
== 0 && linkref
!= 0) {
2249 VTOC(vp
)->c_flag
|= C_HARDLINK
;
2252 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
2254 cat_releasedesc(&cndesc
);
2257 hfs_unlock(VTOC(vp
));
2263 * Flush out all the files in a filesystem.
2266 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2268 struct hfsmount
*hfsmp
;
2269 struct vnode
*skipvp
= NULLVP
;
2274 hfsmp
= VFSTOHFS(mp
);
2278 * The open quota files have an indirect reference on
2279 * the root directory vnode. We must account for this
2280 * extra reference when doing the intial vflush.
2283 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2285 /* Find out how many quota files we have open. */
2286 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2287 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2291 /* Obtain the root vnode so we can skip over it. */
2292 skipvp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, kHFSRootFolderID
, 0, 0);
2296 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2300 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2303 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2306 * See if there are additional references on the
2307 * root vp besides the ones obtained from the open
2308 * quota files and the hfs_chash_getvnode call above.
2311 (vnode_isinuse(skipvp
, quotafilecnt
))) {
2312 error
= EBUSY
; /* root directory is still open */
2314 hfs_unlock(VTOC(skipvp
));
2317 if (error
&& (flags
& FORCECLOSE
) == 0)
2320 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2321 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2323 hfs_quotaoff(p
, mp
, i
);
2325 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2333 * Update volume encoding bitmap (HFS Plus only)
2337 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2339 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2340 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2345 case kTextEncodingMacUkrainian
:
2346 index
= kIndexMacUkrainian
;
2348 case kTextEncodingMacFarsi
:
2349 index
= kIndexMacFarsi
;
2357 HFS_MOUNT_LOCK(hfsmp
, TRUE
)
2358 hfsmp
->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2359 hfsmp
->vcbFlags
|= 0xFF00;
2360 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2365 * Update volume stats
2367 * On journal volumes this will cause a volume header flush
2371 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2377 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2379 hfsmp
->vcbFlags
|= 0xFF00;
2380 hfsmp
->hfs_mtime
= tv
.tv_sec
;
2386 if (hfsmp
->hfs_dircount
!= 0xFFFFFFFF)
2387 ++hfsmp
->hfs_dircount
;
2388 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2389 ++hfsmp
->vcbNmRtDirs
;
2392 if (hfsmp
->hfs_dircount
!= 0)
2393 --hfsmp
->hfs_dircount
;
2394 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2395 --hfsmp
->vcbNmRtDirs
;
2398 if (hfsmp
->hfs_filecount
!= 0xFFFFFFFF)
2399 ++hfsmp
->hfs_filecount
;
2400 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2404 if (hfsmp
->hfs_filecount
!= 0)
2405 --hfsmp
->hfs_filecount
;
2406 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2411 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2414 hfs_flushvolumeheader(hfsmp
, 0, 0);
2422 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2424 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2425 struct filefork
*fp
;
2426 HFSMasterDirectoryBlock
*mdb
;
2427 struct buf
*bp
= NULL
;
2432 sectorsize
= hfsmp
->hfs_phys_block_size
;
2433 retval
= (int)buf_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2440 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2442 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2444 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2445 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2446 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2447 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2448 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2449 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2450 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2451 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2453 namelen
= strlen(vcb
->vcbVN
);
2454 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2455 /* Retry with MacRoman in case that's how it was exported. */
2457 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2459 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2460 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2461 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2462 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2463 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2465 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2467 fp
= VTOF(vcb
->extentsRefNum
);
2468 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2469 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2470 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2471 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2472 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2473 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2474 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2475 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2476 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2478 fp
= VTOF(vcb
->catalogRefNum
);
2479 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2480 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2481 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2482 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2483 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2484 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2485 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2486 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2487 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2489 MarkVCBClean( vcb
);
2491 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2493 /* If requested, flush out the alternate MDB */
2495 struct buf
*alt_bp
= NULL
;
2497 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2498 bcopy(mdb
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2500 (void) VNOP_BWRITE(alt_bp
);
2505 if (waitfor
!= MNT_WAIT
)
2508 retval
= VNOP_BWRITE(bp
);
2514 * Flush any dirty in-memory mount data to the on-disk
2517 * Note: the on-disk volume signature is intentionally
2518 * not flushed since the on-disk "H+" and "HX" signatures
2519 * are always stored in-memory as "H+".
2523 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2525 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2526 struct filefork
*fp
;
2527 HFSPlusVolumeHeader
*volumeHeader
;
2532 daddr64_t priIDSector
;
2534 u_int16_t signature
;
2535 u_int16_t hfsversion
;
2537 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2540 if (vcb
->vcbSigWord
== kHFSSigWord
)
2541 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2545 sectorsize
= hfsmp
->hfs_phys_block_size
;
2546 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2547 HFS_PRI_SECTOR(sectorsize
));
2549 if (hfs_start_transaction(hfsmp
) != 0) {
2553 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2558 hfs_end_transaction(hfsmp
);
2560 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2565 journal_modify_block_start(hfsmp
->jnl
, bp
);
2568 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2571 * Sanity check what we just read.
2573 signature
= SWAP_BE16 (volumeHeader
->signature
);
2574 hfsversion
= SWAP_BE16 (volumeHeader
->version
);
2575 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2576 (hfsversion
< kHFSPlusVersion
) || (hfsversion
> 100) ||
2577 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2579 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2580 vcb
->vcbVN
, signature
, hfsversion
,
2581 SWAP_BE32 (volumeHeader
->blockSize
));
2583 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2589 * For embedded HFS+ volumes, update create date if it changed
2590 * (ie from a setattrlist call)
2592 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2593 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2595 HFSMasterDirectoryBlock
*mdb
;
2597 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
),
2598 sectorsize
, NOCRED
, &bp2
);
2604 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp2
) +
2605 HFS_PRI_OFFSET(sectorsize
));
2607 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2610 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2613 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2616 journal_modify_block_end(hfsmp
->jnl
, bp2
);
2618 (void) VNOP_BWRITE(bp2
); /* write out the changes */
2623 buf_brelse(bp2
); /* just release it */
2628 if (1 /* hfsmp->jnl == 0 */) {
2629 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2632 /* Note: only update the lower 16 bits worth of attributes */
2633 volumeHeader
->attributes
= SWAP_BE32 (vcb
->vcbAtrb
);
2634 volumeHeader
->journalInfoBlock
= SWAP_BE32 (vcb
->vcbJinfoBlock
);
2636 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2638 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2640 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2641 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2642 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2643 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2644 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2645 volumeHeader
->totalBlocks
= SWAP_BE32 (vcb
->totalBlocks
);
2646 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2647 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2648 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2649 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2650 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2651 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2652 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2654 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0) {
2655 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2659 /* Sync Extents over-flow file meta data */
2660 fp
= VTOF(vcb
->extentsRefNum
);
2661 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2662 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2663 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2664 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2665 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2666 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2668 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2669 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2670 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2671 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2674 /* Sync Catalog file meta data */
2675 fp
= VTOF(vcb
->catalogRefNum
);
2676 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2677 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2678 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2679 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2680 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2681 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2683 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2684 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2685 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2686 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2689 /* Sync Allocation file meta data */
2690 fp
= VTOF(vcb
->allocationsRefNum
);
2691 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2692 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2693 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2694 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2695 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2696 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2698 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2699 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2700 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2701 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2704 /* Sync Attribute file meta data */
2705 if (hfsmp
->hfs_attribute_vp
) {
2706 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
2707 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2708 volumeHeader
->attributesFile
.extents
[i
].startBlock
=
2709 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2710 volumeHeader
->attributesFile
.extents
[i
].blockCount
=
2711 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2713 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2714 volumeHeader
->attributesFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2715 volumeHeader
->attributesFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2716 volumeHeader
->attributesFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2719 vcb
->vcbFlags
&= 0x00FF;
2721 if (1 /* hfsmp->jnl == 0 */) {
2722 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2725 /* If requested, flush out the alternate volume header */
2726 if (altflush
&& hfsmp
->hfs_alt_id_sector
) {
2727 struct buf
*alt_bp
= NULL
;
2729 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2731 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2734 bcopy(volumeHeader
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2737 journal_modify_block_end(hfsmp
->jnl
, alt_bp
);
2739 (void) VNOP_BWRITE(alt_bp
);
2746 journal_modify_block_end(hfsmp
->jnl
, bp
);
2748 if (waitfor
!= MNT_WAIT
)
2751 retval
= VNOP_BWRITE(bp
);
2752 /* When critical data changes, flush the device cache */
2753 if (critical
&& (retval
== 0)) {
2754 (void) VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2755 NULL
, FWRITE
, NULL
);
2759 hfs_end_transaction(hfsmp
);
2766 * Extend a file system.
2770 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
2772 struct proc
*p
= vfs_context_proc(context
);
2773 kauth_cred_t cred
= vfs_context_ucred(context
);
2775 struct vnode
*devvp
;
2777 struct filefork
*fp
= NULL
;
2779 struct cat_fork forkdata
;
2781 u_int64_t newblkcnt
;
2782 u_int64_t prev_phys_block_count
;
2784 u_int64_t sectorcnt
;
2785 u_int32_t sectorsize
;
2786 daddr64_t prev_alt_sector
;
2791 devvp
= hfsmp
->hfs_devvp
;
2792 vcb
= HFSTOVCB(hfsmp
);
2795 * - HFS Plus file systems only.
2796 * - Journaling must be enabled.
2797 * - No embedded volumes.
2799 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
2800 (hfsmp
->jnl
== NULL
) ||
2801 (vcb
->hfsPlusIOPosOffset
!= 0)) {
2805 * If extending file system by non-root, then verify
2806 * ownership and check permissions.
2808 if (suser(cred
, NULL
)) {
2809 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0);
2813 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
2815 error
= hfs_write_access(vp
, cred
, p
, false);
2817 hfs_unlock(VTOC(vp
));
2822 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
2826 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, context
)) {
2829 if (sectorsize
!= hfsmp
->hfs_phys_block_size
) {
2832 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, context
)) {
2835 if ((sectorsize
* sectorcnt
) < newsize
) {
2836 printf("hfs_extendfs: not enough space on device\n");
2839 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
2842 * Validate new size.
2844 if ((newsize
<= oldsize
) || (newsize
% sectorsize
)) {
2845 printf("hfs_extendfs: invalid size\n");
2848 newblkcnt
= newsize
/ vcb
->blockSize
;
2849 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
2852 addblks
= newblkcnt
- vcb
->totalBlocks
;
2854 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
2856 * Enclose changes inside a transaction.
2858 if (hfs_start_transaction(hfsmp
) != 0) {
2862 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
2863 vp
= vcb
->allocationsRefNum
;
2865 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
2868 * Calculate additional space required (if any) by allocation bitmap.
2870 bitmapblks
= roundup(newblkcnt
/ 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
2871 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
2872 bitmapblks
-= fp
->ff_blocks
;
2876 if (bitmapblks
> 0) {
2881 * Add a new extent to the allocation bitmap file.
2883 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
2885 printf("hfs_extendfs: error %d adding extents\n", error
);
2888 blkcnt
= bitmapblks
;
2889 blkno
= (daddr64_t
)fp
->ff_blocks
;
2890 fp
->ff_blocks
+= bitmapblks
;
2891 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
2892 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
2894 * Zero out the new bitmap blocks.
2899 while (blkcnt
> 0) {
2900 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
2907 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
2909 error
= (int)buf_bwrite(bp
);
2917 printf("hfs_extendfs: error %d clearing blocks\n", error
);
2921 * Mark the new bitmap space as allocated.
2923 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
2925 printf("hfs_extendfs: error %d setting bitmap\n", error
);
2930 * Mark the new alternate VH as allocated.
2932 if (vcb
->blockSize
== 512)
2933 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
2935 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
2937 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
2941 * Mark the old alternate VH as free.
2943 if (vcb
->blockSize
== 512)
2944 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
2946 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
2948 * Adjust file system variables for new space.
2950 prev_phys_block_count
= hfsmp
->hfs_phys_block_count
;
2951 prev_alt_sector
= hfsmp
->hfs_alt_id_sector
;
2953 vcb
->totalBlocks
+= addblks
;
2954 vcb
->freeBlocks
+= addblks
- bitmapblks
;
2955 hfsmp
->hfs_phys_block_count
= newsize
/ sectorsize
;
2956 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sectorsize
) +
2957 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2959 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
2961 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
2963 * Restore to old state.
2965 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
2966 vcb
->totalBlocks
-= addblks
;
2967 vcb
->freeBlocks
-= addblks
- bitmapblks
;
2968 hfsmp
->hfs_phys_block_count
= prev_phys_block_count
;
2969 hfsmp
->hfs_alt_id_sector
= prev_alt_sector
;
2971 if (vcb
->blockSize
== 512)
2972 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
2974 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
2978 * Invalidate the old alternate volume header.
2981 if (prev_alt_sector
) {
2982 if (buf_meta_bread(hfsmp
->hfs_devvp
, prev_alt_sector
, sectorsize
,
2983 NOCRED
, &bp
) == 0) {
2984 journal_modify_block_start(hfsmp
->jnl
, bp
);
2986 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2988 journal_modify_block_end(hfsmp
->jnl
, bp
);
2995 /* Restore allocation fork. */
2996 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
2997 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3000 hfs_systemfile_unlock(hfsmp
, lockflags
);
3001 hfs_end_transaction(hfsmp
);
3006 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3009 * Truncate a file system (while still mounted).
3013 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, __unused vfs_context_t context
)
3015 struct vnode
* rvp
= NULL
;
3016 struct buf
*bp
= NULL
;
3018 u_int32_t newblkcnt
;
3019 u_int32_t reclaimblks
;
3021 int transaction_begun
= 0;
3025 * Grab the root vnode to serialize with another hfs_truncatefs call.
3027 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &rvp
, 0);
3032 * - HFS Plus file systems only.
3033 * - Journaling must be enabled.
3034 * - No embedded volumes.
3036 if ((hfsmp
->hfs_flags
& HFS_STANDARD
) ||
3037 (hfsmp
->jnl
== NULL
) ||
3038 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
3042 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3043 newblkcnt
= newsize
/ hfsmp
->blockSize
;
3044 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
3046 /* Make sure new size is valid. */
3047 if ((newsize
< HFS_MIN_SIZE
) ||
3048 (newsize
>= oldsize
) ||
3049 (newsize
% hfsmp
->hfs_phys_block_size
)) {
3053 /* Make sure there's enough space to work with. */
3054 if (reclaimblks
> (hfsmp
->freeBlocks
/ 4)) {
3059 printf("hfs_truncatefs: shrinking %s by %d blocks out of %d\n",
3060 hfsmp
->vcbVN
, reclaimblks
, hfsmp
->totalBlocks
);
3062 if (hfs_start_transaction(hfsmp
) != 0) {
3066 transaction_begun
= 1;
3069 * Look for files that have blocks beyond newblkcnt.
3071 if (hfs_isallocated(hfsmp
, newblkcnt
, reclaimblks
- 1)) {
3073 * hfs_reclaimspace will use separate transactions when
3074 * relocating files (so we don't overwhelm the journal).
3076 hfs_end_transaction(hfsmp
);
3077 transaction_begun
= 0;
3079 /* Attempt to reclaim some space. */
3080 if (hfs_reclaimspace(hfsmp
, newblkcnt
) != 0) {
3081 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp
->vcbVN
);
3085 if (hfs_start_transaction(hfsmp
) != 0) {
3089 transaction_begun
= 1;
3091 /* Check if we're clear now. */
3092 if (hfs_isallocated(hfsmp
, newblkcnt
, reclaimblks
- 1)) {
3093 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp
->vcbVN
);
3098 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3101 * Mark the old alternate volume header as free.
3102 * We don't bother shrinking allocation bitmap file.
3104 if (hfsmp
->blockSize
== 512)
3105 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
3107 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
3110 * Allocate last block for alternate volume header.
3112 if (hfsmp
->blockSize
== 512)
3113 error
= BlockMarkAllocated(hfsmp
, newblkcnt
- 2, 2);
3115 error
= BlockMarkAllocated(hfsmp
, newblkcnt
- 1, 1);
3122 * Invalidate the existing alternate volume header.
3124 if (hfsmp
->hfs_alt_id_sector
) {
3125 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
,
3126 hfsmp
->hfs_phys_block_size
, NOCRED
, &bp
) == 0) {
3127 journal_modify_block_start(hfsmp
->jnl
, bp
);
3129 bzero((void*)((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_phys_block_size
)), kMDBSize
);
3131 journal_modify_block_end(hfsmp
->jnl
, bp
);
3139 * Adjust file system variables and flush them to disk.
3141 hfsmp
->freeBlocks
-= hfsmp
->totalBlocks
- newblkcnt
;
3142 hfsmp
->totalBlocks
= newblkcnt
;
3143 hfsmp
->hfs_phys_block_count
= newsize
/ hfsmp
->hfs_phys_block_size
;
3144 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_phys_block_size
, hfsmp
->hfs_phys_block_count
);
3145 MarkVCBDirty(hfsmp
);
3146 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3148 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
3151 hfs_systemfile_unlock(hfsmp
, lockflags
);
3153 if (transaction_begun
) {
3154 hfs_end_transaction(hfsmp
);
3157 hfs_unlock(VTOC(rvp
));
3164 * Reclaim space at the end of a file system.
3167 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
)
3169 struct vnode
*vp
= NULL
;
3171 struct BTreeIterator
* iterator
= NULL
;
3172 struct FSBufferDescriptor btdata
;
3173 struct HFSPlusCatalogFile filerec
;
3174 u_int32_t saved_next_allocation
;
3185 * Check if Attributes file overlaps.
3187 if (hfsmp
->hfs_attribute_vp
) {
3188 struct filefork
*fp
;
3190 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
3191 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3192 block
= fp
->ff_extents
[i
].startBlock
+
3193 fp
->ff_extents
[i
].blockCount
;
3194 if (block
>= startblk
) {
3195 printf("hfs_reclaimspace: Attributes file can't move\n");
3201 /* For now we'll move a maximum of 16,384 files. */
3202 maxfilecnt
= MIN(hfsmp
->hfs_filecount
, 16384);
3203 cnidbufsize
= maxfilecnt
* sizeof(cnid_t
);
3204 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&cnidbufp
, cnidbufsize
)) {
3207 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
3208 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
3212 saved_next_allocation
= hfsmp
->nextAllocation
;
3213 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_start
;
3215 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
3216 bzero(iterator
, sizeof(*iterator
));
3218 btdata
.bufferAddress
= &filerec
;
3219 btdata
.itemSize
= sizeof(filerec
);
3220 btdata
.itemCount
= 1;
3222 /* Keep the Catalog file locked during iteration. */
3223 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
3224 error
= BTIterateRecord(fcb
, kBTreeFirstRecord
, iterator
, NULL
, NULL
);
3226 hfs_systemfile_unlock(hfsmp
, lockflags
);
3231 * Iterate over all the catalog records looking for files
3232 * that overlap into the space we're trying to free up.
3234 for (filecnt
= 0; filecnt
< maxfilecnt
; ) {
3235 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
3237 if (error
== btNotFound
)
3241 if (filerec
.recordType
!= kHFSPlusFileRecord
||
3242 filerec
.fileID
== hfsmp
->hfs_jnlfileid
)
3245 * Check if either fork overlaps target space.
3247 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3248 block
= filerec
.dataFork
.extents
[i
].startBlock
+
3249 filerec
.dataFork
.extents
[i
].blockCount
;
3250 if (block
>= startblk
) {
3251 if (filerec
.fileID
== hfsmp
->hfs_jnlfileid
) {
3252 printf("hfs_reclaimspace: cannot move active journal\n");
3256 cnidbufp
[filecnt
++] = filerec
.fileID
;
3259 block
= filerec
.resourceFork
.extents
[i
].startBlock
+
3260 filerec
.resourceFork
.extents
[i
].blockCount
;
3261 if (block
>= startblk
) {
3262 cnidbufp
[filecnt
++] = filerec
.fileID
;
3267 /* All done with catalog. */
3268 hfs_systemfile_unlock(hfsmp
, lockflags
);
3272 /* Now move any files that are in the way. */
3273 for (i
= 0; i
< filecnt
; ++i
) {
3276 if (hfs_vget(hfsmp
, cnidbufp
[i
], &vp
, 0) != 0)
3279 /* Relocate any data fork blocks. */
3280 if (VTOF(vp
)->ff_blocks
> 0) {
3281 error
= hfs_relocate(vp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
3286 /* Relocate any resource fork blocks. */
3287 if ((VTOC((vp
))->c_blocks
- VTOF((vp
))->ff_blocks
) > 0) {
3288 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, current_proc());
3291 error
= hfs_relocate(rvp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
3296 hfs_unlock(VTOC(vp
));
3301 hfs_unlock(VTOC(vp
));
3307 * Note: this implementation doesn't handle overflow extents.
3310 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3311 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
3313 /* On errors restore the roving allocation pointer. */
3315 hfsmp
->nextAllocation
= saved_next_allocation
;
3322 * Get file system attributes.
3325 hfs_vfs_getattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
3327 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
3328 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
3331 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)hfsmp
->vcbNxtCNID
;
3333 VFSATTR_RETURN(fsap
, f_objcount
, (uint64_t)hfsmp
->vcbFilCnt
+ (uint64_t)hfsmp
->vcbDirCnt
);
3334 VFSATTR_RETURN(fsap
, f_filecount
, (uint64_t)hfsmp
->vcbFilCnt
);
3335 VFSATTR_RETURN(fsap
, f_dircount
, (uint64_t)hfsmp
->vcbDirCnt
);
3336 VFSATTR_RETURN(fsap
, f_maxobjcount
, (uint64_t)0xFFFFFFFF);
3337 VFSATTR_RETURN(fsap
, f_iosize
, (size_t)(MAX_UPL_TRANSFER
* PAGE_SIZE
));
3338 VFSATTR_RETURN(fsap
, f_blocks
, (uint64_t)hfsmp
->totalBlocks
);
3339 VFSATTR_RETURN(fsap
, f_bfree
, (uint64_t)hfs_freeblks(hfsmp
, 0));
3340 VFSATTR_RETURN(fsap
, f_bavail
, (uint64_t)hfs_freeblks(hfsmp
, 1));
3341 VFSATTR_RETURN(fsap
, f_bsize
, (uint32_t)vcb
->blockSize
);
3342 /* XXX needs clarification */
3343 VFSATTR_RETURN(fsap
, f_bused
, hfsmp
->totalBlocks
- hfs_freeblks(hfsmp
, 1));
3344 /* Maximum files is constrained by total blocks. */
3345 VFSATTR_RETURN(fsap
, f_files
, (uint64_t)(hfsmp
->totalBlocks
- 2));
3346 VFSATTR_RETURN(fsap
, f_ffree
, MIN((uint64_t)freeCNIDs
, (uint64_t)hfs_freeblks(hfsmp
, 1)));
3348 fsap
->f_fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
3349 fsap
->f_fsid
.val
[1] = vfs_typenum(mp
);
3350 VFSATTR_SET_SUPPORTED(fsap
, f_fsid
);
3352 VFSATTR_RETURN(fsap
, f_signature
, vcb
->vcbSigWord
);
3353 VFSATTR_RETURN(fsap
, f_carbon_fsid
, 0);
3355 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
3356 vol_capabilities_attr_t
*cap
;
3358 cap
= &fsap
->f_capabilities
;
3360 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
3361 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
3362 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3363 VOL_CAP_FMT_CASE_PRESERVING
|
3364 VOL_CAP_FMT_FAST_STATFS
;
3366 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
3367 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3368 VOL_CAP_FMT_SYMBOLICLINKS
|
3369 VOL_CAP_FMT_HARDLINKS
|
3370 VOL_CAP_FMT_JOURNAL
|
3371 (hfsmp
->jnl
? VOL_CAP_FMT_JOURNAL_ACTIVE
: 0) |
3372 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
? VOL_CAP_FMT_CASE_SENSITIVE
: 0) |
3373 VOL_CAP_FMT_CASE_PRESERVING
|
3374 VOL_CAP_FMT_FAST_STATFS
|
3375 VOL_CAP_FMT_2TB_FILESIZE
;
3377 cap
->capabilities
[VOL_CAPABILITIES_INTERFACES
] =
3378 VOL_CAP_INT_SEARCHFS
|
3379 VOL_CAP_INT_ATTRLIST
|
3380 VOL_CAP_INT_NFSEXPORT
|
3381 VOL_CAP_INT_READDIRATTR
|
3382 VOL_CAP_INT_EXCHANGEDATA
|
3383 VOL_CAP_INT_ALLOCATE
|
3384 VOL_CAP_INT_VOL_RENAME
|
3385 VOL_CAP_INT_ADVLOCK
|
3387 cap
->capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
3388 cap
->capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
3390 cap
->valid
[VOL_CAPABILITIES_FORMAT
] =
3391 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
3392 VOL_CAP_FMT_SYMBOLICLINKS
|
3393 VOL_CAP_FMT_HARDLINKS
|
3394 VOL_CAP_FMT_JOURNAL
|
3395 VOL_CAP_FMT_JOURNAL_ACTIVE
|
3396 VOL_CAP_FMT_NO_ROOT_TIMES
|
3397 VOL_CAP_FMT_SPARSE_FILES
|
3398 VOL_CAP_FMT_ZERO_RUNS
|
3399 VOL_CAP_FMT_CASE_SENSITIVE
|
3400 VOL_CAP_FMT_CASE_PRESERVING
|
3401 VOL_CAP_FMT_FAST_STATFS
|
3402 VOL_CAP_FMT_2TB_FILESIZE
;
3403 cap
->valid
[VOL_CAPABILITIES_INTERFACES
] =
3404 VOL_CAP_INT_SEARCHFS
|
3405 VOL_CAP_INT_ATTRLIST
|
3406 VOL_CAP_INT_NFSEXPORT
|
3407 VOL_CAP_INT_READDIRATTR
|
3408 VOL_CAP_INT_EXCHANGEDATA
|
3409 VOL_CAP_INT_COPYFILE
|
3410 VOL_CAP_INT_ALLOCATE
|
3411 VOL_CAP_INT_VOL_RENAME
|
3412 VOL_CAP_INT_ADVLOCK
|
3414 cap
->valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
3415 cap
->valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
3416 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
3418 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
3419 vol_attributes_attr_t
*attrp
= &fsap
->f_attributes
;
3421 attrp
->validattr
.commonattr
= ATTR_CMN_VALIDMASK
;
3422 attrp
->validattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
3423 attrp
->validattr
.dirattr
= ATTR_DIR_VALIDMASK
;
3424 attrp
->validattr
.fileattr
= ATTR_FILE_VALIDMASK
;
3425 attrp
->validattr
.forkattr
= 0;
3427 attrp
->nativeattr
.commonattr
= ATTR_CMN_VALIDMASK
;
3428 attrp
->nativeattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
3429 attrp
->nativeattr
.dirattr
= ATTR_DIR_VALIDMASK
;
3430 attrp
->nativeattr
.fileattr
= ATTR_FILE_VALIDMASK
;
3431 attrp
->nativeattr
.forkattr
= 0;
3432 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
3434 fsap
->f_create_time
.tv_sec
= hfsmp
->vcbCrDate
;
3435 fsap
->f_create_time
.tv_nsec
= 0;
3436 VFSATTR_SET_SUPPORTED(fsap
, f_create_time
);
3437 fsap
->f_modify_time
.tv_sec
= hfsmp
->vcbLsMod
;
3438 fsap
->f_modify_time
.tv_nsec
= 0;
3439 VFSATTR_SET_SUPPORTED(fsap
, f_modify_time
);
3441 fsap
->f_backup_time
.tv_sec
= hfsmp
->vcbVolBkUp
;
3442 fsap
->f_backup_time
.tv_nsec
= 0;
3443 VFSATTR_SET_SUPPORTED(fsap
, f_backup_time
);
3444 if (VFSATTR_IS_ACTIVE(fsap
, f_fssubtype
)) {
3445 uint16_t subtype
= 0;
3448 * Subtypes (flavors) for HFS
3449 * 0: Mac OS Extended
3450 * 1: Mac OS Extended (Journaled)
3451 * 2: Mac OS Extended (Case Sensitive)
3452 * 3: Mac OS Extended (Case Sensitive, Journaled)
3454 * 128: Mac OS Standard
3457 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
3458 subtype
= HFS_SUBTYPE_STANDARDHFS
;
3459 } else /* HFS Plus */ {
3461 subtype
|= HFS_SUBTYPE_JOURNALED
;
3462 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
3463 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
3465 fsap
->f_fssubtype
= subtype
;
3466 VFSATTR_SET_SUPPORTED(fsap
, f_fssubtype
);
3469 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
3470 strncpy(fsap
->f_vol_name
, hfsmp
->vcbVN
, MAXPATHLEN
);
3471 fsap
->f_vol_name
[MAXPATHLEN
- 1] = 0;
3472 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
3478 * Perform a volume rename. Requires the FS' root vp.
3481 hfs_rename_volume(struct vnode
*vp
, const char *name
, proc_t p
)
3483 ExtendedVCB
*vcb
= VTOVCB(vp
);
3484 struct cnode
*cp
= VTOC(vp
);
3485 struct hfsmount
*hfsmp
= VTOHFS(vp
);
3486 struct cat_desc to_desc
;
3487 struct cat_desc todir_desc
;
3488 struct cat_desc new_desc
;
3489 cat_cookie_t cookie
;
3494 * Ignore attempts to rename a volume to a zero-length name.
3499 bzero(&to_desc
, sizeof(to_desc
));
3500 bzero(&todir_desc
, sizeof(todir_desc
));
3501 bzero(&new_desc
, sizeof(new_desc
));
3502 bzero(&cookie
, sizeof(cookie
));
3504 todir_desc
.cd_parentcnid
= kHFSRootParentID
;
3505 todir_desc
.cd_cnid
= kHFSRootFolderID
;
3506 todir_desc
.cd_flags
= CD_ISDIR
;
3508 to_desc
.cd_nameptr
= name
;
3509 to_desc
.cd_namelen
= strlen(name
);
3510 to_desc
.cd_parentcnid
= kHFSRootParentID
;
3511 to_desc
.cd_cnid
= cp
->c_cnid
;
3512 to_desc
.cd_flags
= CD_ISDIR
;
3514 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)) == 0) {
3515 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
3516 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, &cookie
, p
)) == 0) {
3517 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
3519 error
= cat_rename(hfsmp
, &cp
->c_desc
, &todir_desc
, &to_desc
, &new_desc
);
3522 * If successful, update the name in the VCB, ensure it's terminated.
3525 strncpy(vcb
->vcbVN
, name
, sizeof(vcb
->vcbVN
));
3526 vcb
->vcbVN
[sizeof(vcb
->vcbVN
) - 1] = 0;
3529 hfs_systemfile_unlock(hfsmp
, lockflags
);
3530 cat_postflight(hfsmp
, &cookie
, p
);
3533 vcb
->vcbFlags
|= 0xFF00;
3534 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
3536 hfs_end_transaction(hfsmp
);
3539 /* Release old allocated name buffer */
3540 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
3541 char *name
= cp
->c_desc
.cd_nameptr
;
3543 cp
->c_desc
.cd_nameptr
= 0;
3544 cp
->c_desc
.cd_namelen
= 0;
3545 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
3546 vfs_removename(name
);
3548 /* Update cnode's catalog descriptor */
3549 replace_desc(cp
, &new_desc
);
3550 vcb
->volumeNameEncodingHint
= new_desc
.cd_encoding
;
3551 cp
->c_touch_chgtime
= TRUE
;
3561 * Get file system attributes.
3564 hfs_vfs_setattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
3566 kauth_cred_t cred
= vfs_context_ucred(context
);
3570 * Must be superuser or owner of filesystem to change volume attributes
3572 if (!kauth_cred_issuser(cred
) && (kauth_cred_getuid(cred
) != vfs_statfs(mp
)->f_owner
))
3575 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
3578 error
= hfs_vfs_root(mp
, &root_vp
, context
);
3582 error
= hfs_rename_volume(root_vp
, fsap
->f_vol_name
, vfs_context_proc(context
));
3583 (void) vnode_put(root_vp
);
3587 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
3596 * hfs vfs operations.
3598 struct vfsops hfs_vfsops
= {
3604 hfs_vfs_getattr
, /* was hfs_statfs */