2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1991, 1993, 1994
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
68 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
70 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kauth.h>
78 #include <sys/ubc_internal.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/mount_internal.h>
81 #include <sys/sysctl.h>
82 #include <sys/malloc.h>
84 #include <sys/quota.h>
86 #include <sys/paths.h>
87 #include <sys/utfconv.h>
88 #include <sys/kdebug.h>
89 #include <sys/fslog.h>
91 #include <kern/locks.h>
93 #include <vfs/vfs_journal.h>
95 #include <miscfs/specfs/specdev.h>
96 #include <hfs/hfs_mount.h>
99 #include "hfs_catalog.h"
100 #include "hfs_cnode.h"
102 #include "hfs_endian.h"
103 #include "hfs_hotfiles.h"
104 #include "hfs_quota.h"
106 #include "hfscommon/headers/FileMgrInternal.h"
107 #include "hfscommon/headers/BTreesInternal.h"
115 lck_grp_attr_t
* hfs_group_attr
;
116 lck_attr_t
* hfs_lock_attr
;
117 lck_grp_t
* hfs_mutex_group
;
118 lck_grp_t
* hfs_rwlock_group
;
120 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
121 /* not static so we can re-use in hfs_readwrite.c for build_path */
122 int hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, vfs_context_t context
);
125 static int hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
);
126 static int hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, vfs_context_t context
);
127 static int hfs_flushfiles(struct mount
*, int, struct proc
*);
128 static int hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
);
129 static int hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
);
130 static int hfs_init(struct vfsconf
*vfsp
);
131 static int hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
);
132 static int hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
, int journal_replay_only
, vfs_context_t context
);
133 static int hfs_reload(struct mount
*mp
);
134 static int hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, vfs_context_t context
);
135 static int hfs_quotactl(struct mount
*, int, uid_t
, caddr_t
, vfs_context_t context
);
136 static int hfs_start(struct mount
*mp
, int flags
, vfs_context_t context
);
137 static int hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, vfs_context_t context
);
138 static int hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
);
139 static int hfs_sysctl(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
140 user_addr_t newp
, size_t newlen
, vfs_context_t context
);
141 static int hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
);
142 static int hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, vfs_context_t context
);
144 static int hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
, vfs_context_t context
);
145 static int hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
,
146 u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
);
147 static int hfs_journal_replay(const char *devnode
, vfs_context_t context
);
151 * Called by vfs_mountroot when mounting HFS Plus as root.
156 hfs_mountroot(mount_t mp
, vnode_t rvp
, vfs_context_t context
)
158 struct hfsmount
*hfsmp
;
160 struct vfsstatfs
*vfsp
;
163 hfs_chashinit_finish();
165 if ((error
= hfs_mountfs(rvp
, mp
, NULL
, 0, context
)))
169 hfsmp
= VFSTOHFS(mp
);
171 hfsmp
->hfs_uid
= UNKNOWNUID
;
172 hfsmp
->hfs_gid
= UNKNOWNGID
;
173 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
174 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
176 /* Establish the free block reserve. */
177 vcb
= HFSTOVCB(hfsmp
);
178 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
179 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
181 vfsp
= vfs_statfs(mp
);
182 (void)hfs_statfs(mp
, vfsp
, NULL
);
195 hfs_mount(struct mount
*mp
, vnode_t devvp
, user_addr_t data
, vfs_context_t context
)
197 struct proc
*p
= vfs_context_proc(context
);
198 struct hfsmount
*hfsmp
= NULL
;
199 struct hfs_mount_args args
;
203 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
)))) {
206 cmdflags
= (u_int32_t
)vfs_flags(mp
) & MNT_CMDFLAGS
;
207 if (cmdflags
& MNT_UPDATE
) {
208 hfsmp
= VFSTOHFS(mp
);
210 /* Reload incore data after an fsck. */
211 if (cmdflags
& MNT_RELOAD
) {
212 if (vfs_isrdonly(mp
))
213 return hfs_reload(mp
);
218 /* Change to a read-only file system. */
219 if (((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) &&
223 /* Set flag to indicate that a downgrade to read-only
224 * is in progress and therefore block any further
225 * modifications to the file system.
227 hfs_global_exclusive_lock_acquire(hfsmp
);
228 hfsmp
->hfs_flags
|= HFS_RDONLY_DOWNGRADE
;
229 hfsmp
->hfs_downgrading_proc
= current_thread();
230 hfs_global_exclusive_lock_release(hfsmp
);
232 /* use VFS_SYNC to push out System (btree) files */
233 retval
= VFS_SYNC(mp
, MNT_WAIT
, context
);
234 if (retval
&& ((cmdflags
& MNT_FORCE
) == 0)) {
235 hfsmp
->hfs_flags
&= ~HFS_RDONLY_DOWNGRADE
;
236 hfsmp
->hfs_downgrading_proc
= NULL
;
241 if (cmdflags
& MNT_FORCE
)
244 if ((retval
= hfs_flushfiles(mp
, flags
, p
))) {
245 hfsmp
->hfs_flags
&= ~HFS_RDONLY_DOWNGRADE
;
246 hfsmp
->hfs_downgrading_proc
= NULL
;
250 /* mark the volume cleanly unmounted */
251 hfsmp
->vcbAtrb
|= kHFSVolumeUnmountedMask
;
252 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
253 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
255 /* also get the volume bitmap blocks */
257 if (vnode_mount(hfsmp
->hfs_devvp
) == mp
) {
258 retval
= hfs_fsync(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, p
);
260 vnode_get(hfsmp
->hfs_devvp
);
261 retval
= VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
262 vnode_put(hfsmp
->hfs_devvp
);
266 hfsmp
->hfs_flags
&= ~HFS_RDONLY_DOWNGRADE
;
267 hfsmp
->hfs_downgrading_proc
= NULL
;
268 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
272 hfs_global_exclusive_lock_acquire(hfsmp
);
274 journal_close(hfsmp
->jnl
);
277 // Note: we explicitly don't want to shutdown
278 // access to the jvp because we may need
279 // it later if we go back to being read-write.
281 hfs_global_exclusive_lock_release(hfsmp
);
284 hfsmp
->hfs_downgrading_proc
= NULL
;
287 /* Change to a writable file system. */
288 if (vfs_iswriteupgrade(mp
)) {
291 * On inconsistent disks, do not allow read-write mount
292 * unless it is the boot volume being mounted.
294 if (!(vfs_flags(mp
) & MNT_ROOTFS
) &&
295 (hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
)) {
300 // If the journal was shut-down previously because we were
301 // asked to be read-only, let's start it back up again now
303 if ( (HFSTOVCB(hfsmp
)->vcbAtrb
& kHFSVolumeJournaledMask
)
304 && hfsmp
->jnl
== NULL
305 && hfsmp
->jvp
!= NULL
) {
308 if (hfsmp
->hfs_flags
& HFS_NEED_JNL_RESET
) {
309 jflags
= JOURNAL_RESET
;
314 hfs_global_exclusive_lock_acquire(hfsmp
);
316 hfsmp
->jnl
= journal_open(hfsmp
->jvp
,
317 (hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
320 hfsmp
->hfs_logical_block_size
,
323 hfs_sync_metadata
, hfsmp
->hfs_mp
);
325 hfs_global_exclusive_lock_release(hfsmp
);
327 if (hfsmp
->jnl
== NULL
) {
331 hfsmp
->hfs_flags
&= ~HFS_NEED_JNL_RESET
;
336 /* Only clear HFS_READ_ONLY after a successfull write */
337 hfsmp
->hfs_flags
&= ~HFS_READ_ONLY
;
339 /* If this mount point was downgraded from read-write
340 * to read-only, clear that information as we are now
341 * moving back to read-write.
343 hfsmp
->hfs_flags
&= ~HFS_RDONLY_DOWNGRADE
;
344 hfsmp
->hfs_downgrading_proc
= NULL
;
346 /* mark the volume dirty (clear clean unmount bit) */
347 hfsmp
->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
349 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
350 if (retval
!= E_NONE
)
353 if (!(hfsmp
->hfs_flags
& (HFS_READ_ONLY
| HFS_STANDARD
))) {
354 /* Setup private/hidden directories for hardlinks. */
355 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
356 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
358 hfs_remove_orphans(hfsmp
);
361 * Allow hot file clustering if conditions allow.
363 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) {
364 (void) hfs_recording_init(hfsmp
);
366 /* Force ACLs on HFS+ file systems. */
367 if (vfs_extendedsecurity(HFSTOVFS(hfsmp
)) == 0) {
368 vfs_setextendedsecurity(HFSTOVFS(hfsmp
));
373 /* Update file system parameters. */
374 retval
= hfs_changefs(mp
, &args
);
376 } else /* not an update request */ {
378 /* Set the mount flag to indicate that we support volfs */
379 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_DOVOLFS
));
381 hfs_chashinit_finish();
383 retval
= hfs_mountfs(devvp
, mp
, &args
, 0, context
);
387 (void)hfs_statfs(mp
, vfs_statfs(mp
), context
);
393 struct hfs_changefs_cargs
{
394 struct hfsmount
*hfsmp
;
401 hfs_changefs_callback(struct vnode
*vp
, void *cargs
)
405 struct cat_desc cndesc
;
406 struct cat_attr cnattr
;
407 struct hfs_changefs_cargs
*args
;
411 args
= (struct hfs_changefs_cargs
*)cargs
;
414 vcb
= HFSTOVCB(args
->hfsmp
);
416 lockflags
= hfs_systemfile_lock(args
->hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
417 error
= cat_lookup(args
->hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
, NULL
);
418 hfs_systemfile_unlock(args
->hfsmp
, lockflags
);
421 * If we couldn't find this guy skip to the next one
426 return (VNODE_RETURNED
);
429 * Get the real uid/gid and perm mask from disk.
431 if (args
->permswitch
|| args
->permfix
) {
432 cp
->c_uid
= cnattr
.ca_uid
;
433 cp
->c_gid
= cnattr
.ca_gid
;
434 cp
->c_mode
= cnattr
.ca_mode
;
437 * If we're switching name converters then...
438 * Remove the existing entry from the namei cache.
439 * Update name to one based on new encoder.
443 replace_desc(cp
, &cndesc
);
445 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
446 strlcpy((char *)vcb
->vcbVN
, (const char *)cp
->c_desc
.cd_nameptr
, NAME_MAX
+1);
447 cp
->c_desc
.cd_encoding
= args
->hfsmp
->hfs_encoding
;
450 cat_releasedesc(&cndesc
);
452 return (VNODE_RETURNED
);
455 /* Change fs mount parameters */
457 hfs_changefs(struct mount
*mp
, struct hfs_mount_args
*args
)
460 int namefix
, permfix
, permswitch
;
461 struct hfsmount
*hfsmp
;
463 hfs_to_unicode_func_t get_unicode_func
;
464 unicode_to_hfs_func_t get_hfsname_func
;
465 u_long old_encoding
= 0;
466 struct hfs_changefs_cargs cargs
;
467 u_int32_t mount_flags
;
469 hfsmp
= VFSTOHFS(mp
);
470 vcb
= HFSTOVCB(hfsmp
);
471 mount_flags
= (unsigned int)vfs_flags(mp
);
473 hfsmp
->hfs_flags
|= HFS_IN_CHANGEFS
;
475 permswitch
= (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) &&
476 ((mount_flags
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
477 (((hfsmp
->hfs_flags
& HFS_UNKNOWN_PERMS
) == 0) &&
478 (mount_flags
& MNT_UNKNOWNPERMISSIONS
)));
480 /* The root filesystem must operate with actual permissions: */
481 if (permswitch
&& (mount_flags
& MNT_ROOTFS
) && (mount_flags
& MNT_UNKNOWNPERMISSIONS
)) {
482 vfs_clearflags(mp
, (u_int64_t
)((unsigned int)MNT_UNKNOWNPERMISSIONS
)); /* Just say "No". */
486 if (mount_flags
& MNT_UNKNOWNPERMISSIONS
)
487 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
489 hfsmp
->hfs_flags
&= ~HFS_UNKNOWN_PERMS
;
491 namefix
= permfix
= 0;
494 * Tracking of hot files requires up-to-date access times. So if
495 * access time updates are disabled, we must also disable hot files.
497 if (mount_flags
& MNT_NOATIME
) {
498 (void) hfs_recording_suspend(hfsmp
);
501 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
502 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
503 gTimeZone
= args
->hfs_timezone
;
506 /* Change the default uid, gid and/or mask */
507 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
508 hfsmp
->hfs_uid
= args
->hfs_uid
;
509 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
512 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
513 hfsmp
->hfs_gid
= args
->hfs_gid
;
514 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
517 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
518 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
519 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
520 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
521 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
522 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
523 if (vcb
->vcbSigWord
== kHFSPlusSigWord
)
528 /* Change the hfs encoding value (hfs only) */
529 if ((vcb
->vcbSigWord
== kHFSSigWord
) &&
530 (args
->hfs_encoding
!= (u_long
)VNOVAL
) &&
531 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
533 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
538 * Connect the new hfs_get_unicode converter but leave
539 * the old hfs_get_hfsname converter in place so that
540 * we can lookup existing vnodes to get their correctly
543 * When we're all finished, we can then connect the new
544 * hfs_get_hfsname converter and release our interest
545 * in the old converters.
547 hfsmp
->hfs_get_unicode
= get_unicode_func
;
548 old_encoding
= hfsmp
->hfs_encoding
;
549 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
553 if (!(namefix
|| permfix
|| permswitch
))
556 /* XXX 3762912 hack to support HFS filesystem 'owner' */
559 hfsmp
->hfs_uid
== UNKNOWNUID
? KAUTH_UID_NONE
: hfsmp
->hfs_uid
,
560 hfsmp
->hfs_gid
== UNKNOWNGID
? KAUTH_GID_NONE
: hfsmp
->hfs_gid
);
563 * For each active vnode fix things that changed
565 * Note that we can visit a vnode more than once
566 * and we can race with fsync.
568 * hfs_changefs_callback will be called for each vnode
569 * hung off of this mount point
571 * The vnode will be properly referenced and unreferenced
572 * around the callback
575 cargs
.namefix
= namefix
;
576 cargs
.permfix
= permfix
;
577 cargs
.permswitch
= permswitch
;
579 vnode_iterate(mp
, 0, hfs_changefs_callback
, (void *)&cargs
);
582 * If we're switching name converters we can now
583 * connect the new hfs_get_hfsname converter and
584 * release our interest in the old converters.
587 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
588 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
589 (void) hfs_relconverter(old_encoding
);
592 hfsmp
->hfs_flags
&= ~HFS_IN_CHANGEFS
;
597 struct hfs_reload_cargs
{
598 struct hfsmount
*hfsmp
;
603 hfs_reload_callback(struct vnode
*vp
, void *cargs
)
606 struct hfs_reload_cargs
*args
;
609 args
= (struct hfs_reload_cargs
*)cargs
;
611 * flush all the buffers associated with this node
613 (void) buf_invalidateblks(vp
, 0, 0, 0);
617 * Remove any directory hints
620 hfs_reldirhints(cp
, 0);
623 * Re-read cnode data for all active vnodes (non-metadata files).
625 if (!vnode_issystem(vp
) && !VNODE_IS_RSRC(vp
)) {
626 struct cat_fork
*datafork
;
627 struct cat_desc desc
;
629 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
631 /* lookup by fileID since name could have changed */
632 lockflags
= hfs_systemfile_lock(args
->hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
633 args
->error
= cat_idlookup(args
->hfsmp
, cp
->c_fileid
, 0, &desc
, &cp
->c_attr
, datafork
);
634 hfs_systemfile_unlock(args
->hfsmp
, lockflags
);
636 return (VNODE_RETURNED_DONE
);
639 /* update cnode's catalog descriptor */
640 (void) replace_desc(cp
, &desc
);
642 return (VNODE_RETURNED
);
646 * Reload all incore data for a filesystem (used after running fsck on
647 * the root filesystem and finding things to fix). The filesystem must
648 * be mounted read-only.
650 * Things to do to update the mount:
651 * invalidate all cached meta-data.
652 * invalidate all inactive vnodes.
653 * invalidate all cached file data.
654 * re-read volume header from disk.
655 * re-load meta-file info (extents, file size).
656 * re-load B-tree header data.
657 * re-read cnode data for all active vnodes.
660 hfs_reload(struct mount
*mountp
)
662 register struct vnode
*devvp
;
665 struct hfsmount
*hfsmp
;
666 struct HFSPlusVolumeHeader
*vhp
;
668 struct filefork
*forkp
;
669 struct cat_desc cndesc
;
670 struct hfs_reload_cargs args
;
671 daddr64_t priIDSector
;
673 hfsmp
= VFSTOHFS(mountp
);
674 vcb
= HFSTOVCB(hfsmp
);
676 if (vcb
->vcbSigWord
== kHFSSigWord
)
677 return (EINVAL
); /* rooting from HFS is not supported! */
680 * Invalidate all cached meta-data.
682 devvp
= hfsmp
->hfs_devvp
;
683 if (buf_invalidateblks(devvp
, 0, 0, 0))
684 panic("hfs_reload: dirty1");
689 * hfs_reload_callback will be called for each vnode
690 * hung off of this mount point that can't be recycled...
691 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
692 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
693 * properly referenced and unreferenced around the callback
695 vnode_iterate(mountp
, VNODE_RELOAD
| VNODE_WAIT
, hfs_reload_callback
, (void *)&args
);
701 * Re-read VolumeHeader from disk.
703 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
704 HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
));
706 error
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
707 HFS_PHYSBLK_ROUNDDOWN(priIDSector
, hfsmp
->hfs_log_per_phys
),
708 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
715 vhp
= (HFSPlusVolumeHeader
*) (buf_dataptr(bp
) + HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
717 /* Do a quick sanity check */
718 if ((SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
&&
719 SWAP_BE16(vhp
->signature
) != kHFSXSigWord
) ||
720 (SWAP_BE16(vhp
->version
) != kHFSPlusVersion
&&
721 SWAP_BE16(vhp
->version
) != kHFSXVersion
) ||
722 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
727 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
728 vcb
->vcbAtrb
= SWAP_BE32 (vhp
->attributes
);
729 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
730 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
731 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
732 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
733 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
734 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
735 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
736 HFS_UPDATE_NEXT_ALLOCATION(vcb
, SWAP_BE32 (vhp
->nextAllocation
));
737 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
738 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
739 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
740 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
741 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
744 * Re-load meta-file vnode data (extent info, file size, etc).
746 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
747 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
748 forkp
->ff_extents
[i
].startBlock
=
749 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
750 forkp
->ff_extents
[i
].blockCount
=
751 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
753 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
754 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
755 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
758 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
759 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
760 forkp
->ff_extents
[i
].startBlock
=
761 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
762 forkp
->ff_extents
[i
].blockCount
=
763 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
765 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
766 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
767 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
769 if (hfsmp
->hfs_attribute_vp
) {
770 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
771 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
772 forkp
->ff_extents
[i
].startBlock
=
773 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].startBlock
);
774 forkp
->ff_extents
[i
].blockCount
=
775 SWAP_BE32 (vhp
->attributesFile
.extents
[i
].blockCount
);
777 forkp
->ff_size
= SWAP_BE64 (vhp
->attributesFile
.logicalSize
);
778 forkp
->ff_blocks
= SWAP_BE32 (vhp
->attributesFile
.totalBlocks
);
779 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->attributesFile
.clumpSize
);
782 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
783 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
784 forkp
->ff_extents
[i
].startBlock
=
785 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
786 forkp
->ff_extents
[i
].blockCount
=
787 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
789 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
790 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
791 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
797 * Re-load B-tree header data
799 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
800 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
803 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
804 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
807 if (hfsmp
->hfs_attribute_vp
) {
808 forkp
= VTOF(hfsmp
->hfs_attribute_vp
);
809 if ( (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) )) )
813 /* Reload the volume name */
814 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, 0, &cndesc
, NULL
, NULL
)))
816 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
817 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
818 cat_releasedesc(&cndesc
);
820 /* Re-establish private/hidden directories. */
821 hfs_privatedir_init(hfsmp
, FILE_HARDLINKS
);
822 hfs_privatedir_init(hfsmp
, DIR_HARDLINKS
);
824 /* In case any volume information changed to trigger a notification */
825 hfs_generate_volume_notifications(hfsmp
);
832 * Common code for mount and mountroot
835 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct hfs_mount_args
*args
,
836 int journal_replay_only
, vfs_context_t context
)
838 struct proc
*p
= vfs_context_proc(context
);
840 struct hfsmount
*hfsmp
;
843 HFSMasterDirectoryBlock
*mdbp
;
851 daddr64_t log_blkcnt
;
852 u_int32_t log_blksize
;
853 u_int32_t phys_blksize
;
854 u_int32_t minblksize
;
855 u_int32_t iswritable
;
856 daddr64_t mdb_offset
;
859 ronly
= vfs_isrdonly(mp
);
860 dev
= vnode_specrdev(devvp
);
861 cred
= p
? vfs_context_ucred(context
) : NOCRED
;
867 minblksize
= kHFSBlockSize
;
869 /* Advisory locking should be handled at the VFS layer */
870 vfs_setlocklocal(mp
);
872 /* Get the logical block size (treated as physical block size everywhere) */
873 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&log_blksize
, 0, context
)) {
877 /* Get the physical block size. */
878 retval
= VNOP_IOCTL(devvp
, DKIOCGETPHYSICALBLOCKSIZE
, (caddr_t
)&phys_blksize
, 0, context
);
880 if ((retval
!= ENOTSUP
) && (retval
!= ENOTTY
)) {
884 /* If device does not support this ioctl, assume that physical
885 * block size is same as logical block size
887 phys_blksize
= log_blksize
;
889 /* Switch to 512 byte sectors (temporarily) */
890 if (log_blksize
> 512) {
891 u_int32_t size512
= 512;
893 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, context
)) {
898 /* Get the number of 512 byte physical blocks. */
899 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
900 /* resetting block size may fail if getting block count did */
901 (void)VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
);
906 /* Compute an accurate disk size (i.e. within 512 bytes) */
907 disksize
= (u_int64_t
)log_blkcnt
* (u_int64_t
)512;
910 * On Tiger it is not necessary to switch the device
911 * block size to be 4k if there are more than 31-bits
912 * worth of blocks but to insure compatibility with
913 * pre-Tiger systems we have to do it.
915 * If the device size is not a multiple of 4K (8 * 512), then
916 * switching the logical block size isn't going to help because
917 * we will be unable to write the alternate volume header.
918 * In this case, just leave the logical block size unchanged.
920 if (log_blkcnt
> 0x000000007fffffff && (log_blkcnt
& 7) == 0) {
921 minblksize
= log_blksize
= 4096;
922 if (phys_blksize
< log_blksize
)
923 phys_blksize
= log_blksize
;
926 /* Now switch to our preferred physical block size. */
927 if (log_blksize
> 512) {
928 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
)) {
932 /* Get the count of physical blocks. */
933 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
940 * minblksize is the minimum physical block size
941 * log_blksize has our preferred physical block size
942 * log_blkcnt has the total number of physical blocks
945 mdb_offset
= (daddr64_t
)HFS_PRI_SECTOR(log_blksize
);
946 if ((retval
= (int)buf_meta_bread(devvp
,
947 HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, (phys_blksize
/log_blksize
)),
948 phys_blksize
, cred
, &bp
))) {
951 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
952 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
), mdbp
, kMDBSize
);
956 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
957 bzero(hfsmp
, sizeof(struct hfsmount
));
960 * Init the volume information structure
963 lck_mtx_init(&hfsmp
->hfs_mutex
, hfs_mutex_group
, hfs_lock_attr
);
964 lck_mtx_init(&hfsmp
->hfc_mutex
, hfs_mutex_group
, hfs_lock_attr
);
965 lck_rw_init(&hfsmp
->hfs_global_lock
, hfs_rwlock_group
, hfs_lock_attr
);
966 lck_rw_init(&hfsmp
->hfs_insync
, hfs_rwlock_group
, hfs_lock_attr
);
968 vfs_setfsprivate(mp
, hfsmp
);
969 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
970 hfsmp
->hfs_raw_dev
= vnode_specrdev(devvp
);
971 hfsmp
->hfs_devvp
= devvp
;
972 vnode_ref(devvp
); /* Hold a ref on the device, dropped when hfsmp is freed. */
973 hfsmp
->hfs_logical_block_size
= log_blksize
;
974 hfsmp
->hfs_logical_block_count
= log_blkcnt
;
975 hfsmp
->hfs_physical_block_size
= phys_blksize
;
976 hfsmp
->hfs_log_per_phys
= (phys_blksize
/ log_blksize
);
977 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
979 hfsmp
->hfs_flags
|= HFS_READ_ONLY
;
980 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
)
981 hfsmp
->hfs_flags
|= HFS_UNKNOWN_PERMS
;
984 for (i
= 0; i
< MAXQUOTAS
; i
++)
985 dqfileinit(&hfsmp
->hfs_qfiles
[i
]);
989 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
990 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
991 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
992 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
993 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
994 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
995 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
996 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
997 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
999 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
1002 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
1003 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
1005 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
1008 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
1009 if (((unsigned int)vfs_flags(mp
)) & MNT_UNKNOWNPERMISSIONS
) {
1010 hfsmp
->hfs_uid
= UNKNOWNUID
;
1011 hfsmp
->hfs_gid
= UNKNOWNGID
;
1012 vfs_setowner(mp
, hfsmp
->hfs_uid
, hfsmp
->hfs_gid
); /* tell the VFS */
1013 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
1014 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
1018 /* Find out if disk media is writable. */
1019 if (VNOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, context
) == 0) {
1021 hfsmp
->hfs_flags
|= HFS_WRITEABLE_MEDIA
;
1023 hfsmp
->hfs_flags
&= ~HFS_WRITEABLE_MEDIA
;
1026 // record the current time at which we're mounting this volume
1029 hfsmp
->hfs_mount_time
= tv
.tv_sec
;
1031 /* Mount a standard HFS disk */
1032 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
1033 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
1035 /* If only journal replay is requested, exit immediately */
1036 if (journal_replay_only
) {
1041 if ((vfs_flags(mp
) & MNT_ROOTFS
)) {
1042 retval
= EINVAL
; /* Cannot root from HFS standard disks */
1045 /* HFS disks can only use 512 byte physical blocks */
1046 if (log_blksize
> kHFSBlockSize
) {
1047 log_blksize
= kHFSBlockSize
;
1048 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
)) {
1052 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
1056 hfsmp
->hfs_logical_block_size
= log_blksize
;
1057 hfsmp
->hfs_logical_block_count
= log_blkcnt
;
1058 hfsmp
->hfs_physical_block_size
= log_blksize
;
1059 hfsmp
->hfs_log_per_phys
= 1;
1062 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
1063 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
1065 /* establish the timezone */
1066 gTimeZone
= args
->hfs_timezone
;
1069 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
1070 &hfsmp
->hfs_get_hfsname
);
1074 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
1076 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1078 } else /* Mount an HFS Plus disk */ {
1079 HFSPlusVolumeHeader
*vhp
;
1080 off_t embeddedOffset
;
1081 int jnl_disable
= 0;
1083 /* Get the embedded Volume Header */
1084 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1085 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
1086 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1087 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1090 * If the embedded volume doesn't start on a block
1091 * boundary, then switch the device to a 512-byte
1092 * block size so everything will line up on a block
1095 if ((embeddedOffset
% log_blksize
) != 0) {
1096 printf("HFS Mount: embedded volume offset not"
1097 " a multiple of physical block size (%d);"
1098 " switching to 512\n", log_blksize
);
1100 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
1101 (caddr_t
)&log_blksize
, FWRITE
, context
)) {
1105 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
1106 (caddr_t
)&log_blkcnt
, 0, context
)) {
1110 /* Note: relative block count adjustment */
1111 hfsmp
->hfs_logical_block_count
*=
1112 hfsmp
->hfs_logical_block_size
/ log_blksize
;
1113 hfsmp
->hfs_logical_block_size
= log_blksize
;
1115 /* Update logical/physical block size */
1116 hfsmp
->hfs_physical_block_size
= log_blksize
;
1117 phys_blksize
= log_blksize
;
1118 hfsmp
->hfs_log_per_phys
= 1;
1121 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1122 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1124 hfsmp
->hfs_logical_block_count
= disksize
/ log_blksize
;
1126 mdb_offset
= (daddr64_t
)((embeddedOffset
/ log_blksize
) + HFS_PRI_SECTOR(log_blksize
));
1127 retval
= (int)buf_meta_bread(devvp
, HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
1128 phys_blksize
, cred
, &bp
);
1131 bcopy((char *)buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
), mdbp
, 512);
1134 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1136 } else /* pure HFS+ */ {
1138 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1142 * On inconsistent disks, do not allow read-write mount
1143 * unless it is the boot volume being mounted.
1145 if (!(vfs_flags(mp
) & MNT_ROOTFS
) &&
1146 (SWAP_BE32(vhp
->attributes
) & kHFSVolumeInconsistentMask
) &&
1147 !(hfsmp
->hfs_flags
& HFS_READ_ONLY
)) {
1157 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) &&
1158 args
->journal_disable
) {
1163 // We only initialize the journal here if the last person
1164 // to mount this volume was journaling aware. Otherwise
1165 // we delay journal initialization until later at the end
1166 // of hfs_MountHFSPlusVolume() because the last person who
1167 // mounted it could have messed things up behind our back
1168 // (so we need to go find the .journal file, make sure it's
1169 // the right size, re-sync up if it was moved, etc).
1171 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1172 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1175 // if we're able to init the journal, mark the mount
1176 // point as journaled.
1178 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1179 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
1181 // if the journal failed to open, then set the lastMountedVersion
1182 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1183 // of just bailing out because the volume is journaled.
1185 HFSPlusVolumeHeader
*jvhp
;
1187 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1189 if (mdb_offset
== 0) {
1190 mdb_offset
= (daddr64_t
)((embeddedOffset
/ log_blksize
) + HFS_PRI_SECTOR(log_blksize
));
1194 retval
= (int)buf_meta_bread(devvp
,
1195 HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
1196 phys_blksize
, cred
, &bp
);
1198 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
));
1200 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1201 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1202 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1210 // clear this so the error exit path won't try to use it
1215 // if this isn't the root device just bail out.
1216 // If it is the root device we just continue on
1217 // in the hopes that fsck_hfs will be able to
1218 // fix any damage that exists on the volume.
1219 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1227 /* Either the journal is replayed successfully, or there
1228 * was nothing to replay, or no journal exists. In any case,
1231 if (journal_replay_only
) {
1236 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1238 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1240 * If the backend didn't like our physical blocksize
1241 * then retry with physical blocksize of 512.
1243 if ((retval
== ENXIO
) && (log_blksize
> 512) && (log_blksize
!= minblksize
)) {
1244 printf("HFS Mount: could not use physical block size "
1245 "(%d) switching to 512\n", log_blksize
);
1247 if (VNOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&log_blksize
, FWRITE
, context
)) {
1251 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&log_blkcnt
, 0, context
)) {
1255 devvp
->v_specsize
= log_blksize
;
1256 /* Note: relative block count adjustment (in case this is an embedded volume). */
1257 hfsmp
->hfs_logical_block_count
*= hfsmp
->hfs_logical_block_size
/ log_blksize
;
1258 hfsmp
->hfs_logical_block_size
= log_blksize
;
1259 hfsmp
->hfs_log_per_phys
= hfsmp
->hfs_physical_block_size
/ log_blksize
;
1262 // close and re-open this with the new block size
1263 journal_close(hfsmp
->jnl
);
1265 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1266 vfs_setflags(mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
1268 // if the journal failed to open, then set the lastMountedVersion
1269 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1270 // of just bailing out because the volume is journaled.
1272 HFSPlusVolumeHeader
*jvhp
;
1274 hfsmp
->hfs_flags
|= HFS_NEED_JNL_RESET
;
1276 if (mdb_offset
== 0) {
1277 mdb_offset
= (daddr64_t
)((embeddedOffset
/ log_blksize
) + HFS_PRI_SECTOR(log_blksize
));
1281 retval
= (int)buf_meta_bread(devvp
, HFS_PHYSBLK_ROUNDDOWN(mdb_offset
, hfsmp
->hfs_log_per_phys
),
1282 phys_blksize
, cred
, &bp
);
1284 jvhp
= (HFSPlusVolumeHeader
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(phys_blksize
));
1286 if (SWAP_BE16(jvhp
->signature
) == kHFSPlusSigWord
|| SWAP_BE16(jvhp
->signature
) == kHFSXSigWord
) {
1287 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1288 jvhp
->lastMountedVersion
= SWAP_BE32(kFSKMountVersion
);
1296 // clear this so the error exit path won't try to use it
1301 // if this isn't the root device just bail out.
1302 // If it is the root device we just continue on
1303 // in the hopes that fsck_hfs will be able to
1304 // fix any damage that exists on the volume.
1305 if ( !(vfs_flags(mp
) & MNT_ROOTFS
)) {
1312 /* Try again with a smaller block size... */
1313 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
, cred
);
1316 (void) hfs_relconverter(0);
1319 // save off a snapshot of the mtime from the previous mount
1321 hfsmp
->hfs_last_mounted_mtime
= hfsmp
->hfs_mtime
;
1327 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (long)dev
;
1328 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
1329 vfs_setmaxsymlen(mp
, 0);
1330 mp
->mnt_vtable
->vfc_threadsafe
= TRUE
;
1331 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSNATIVEXATTR
;
1333 mp
->mnt_kern_flag
|= MNTK_NAMED_STREAMS
;
1335 if (!(hfsmp
->hfs_flags
& HFS_STANDARD
)) {
1336 /* Tell VFS that we support directory hard links. */
1337 mp
->mnt_vtable
->vfc_vfsflags
|= VFC_VFSDIRLINKS
;
1339 /* HFS standard doesn't support extended readdir! */
1340 mp
->mnt_vtable
->vfc_vfsflags
&= ~VFC_VFSREADDIR_EXTENDED
;
1345 * Set the free space warning levels for a non-root volume:
1347 * Set the lower freespace limit (the level that will trigger a warning)
1348 * to 5% of the volume size or 250MB, whichever is less, and the desired
1349 * level (which will cancel the alert request) to 1/2 above that limit.
1350 * Start looking for free space to drop below this level and generate a
1351 * warning immediately if needed:
1353 hfsmp
->hfs_freespace_notify_warninglimit
=
1354 MIN(HFS_LOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1355 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKTRIGGERFRACTION
);
1356 hfsmp
->hfs_freespace_notify_desiredlevel
=
1357 MIN(HFS_LOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1358 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_LOWDISKSHUTOFFFRACTION
);
1361 * Set the free space warning levels for the root volume:
1363 * Set the lower freespace limit (the level that will trigger a warning)
1364 * to 1% of the volume size or 50MB, whichever is less, and the desired
1365 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1367 hfsmp
->hfs_freespace_notify_warninglimit
=
1368 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1369 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKTRIGGERFRACTION
);
1370 hfsmp
->hfs_freespace_notify_desiredlevel
=
1371 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL
/ HFSTOVCB(hfsmp
)->blockSize
,
1372 (HFSTOVCB(hfsmp
)->totalBlocks
/ 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION
);
1375 /* Check if the file system exists on virtual device, like disk image */
1376 if (VNOP_IOCTL(devvp
, DKIOCISVIRTUAL
, (caddr_t
)&isvirtual
, 0, context
) == 0) {
1378 hfsmp
->hfs_flags
|= HFS_VIRTUAL_DEVICE
;
1383 * Start looking for free space to drop below this level and generate a
1384 * warning immediately if needed:
1386 hfsmp
->hfs_notification_conditions
= 0;
1387 hfs_generate_volume_notifications(hfsmp
);
1390 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1401 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1402 (void)VNOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, context
);
1406 if (hfsmp
->hfs_devvp
) {
1407 vnode_rele(hfsmp
->hfs_devvp
);
1409 FREE(hfsmp
, M_HFSMNT
);
1410 vfs_setfsprivate(mp
, NULL
);
1417 * Make a filesystem operational.
1418 * Nothing to do at the moment.
1422 hfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
1429 * unmount system call
1432 hfs_unmount(struct mount
*mp
, int mntflags
, vfs_context_t context
)
1434 struct proc
*p
= vfs_context_proc(context
);
1435 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1436 int retval
= E_NONE
;
1443 if (mntflags
& MNT_FORCE
) {
1444 flags
|= FORCECLOSE
;
1448 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1451 if (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
)
1452 (void) hfs_recording_suspend(hfsmp
);
1455 * Flush out the b-trees, volume bitmap and Volume Header
1457 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) == 0) {
1458 retval
= hfs_start_transaction(hfsmp
);
1461 } else if (!force
) {
1465 if (hfsmp
->hfs_startup_vp
) {
1466 (void) hfs_lock(VTOC(hfsmp
->hfs_startup_vp
), HFS_EXCLUSIVE_LOCK
);
1467 retval
= hfs_fsync(hfsmp
->hfs_startup_vp
, MNT_WAIT
, 0, p
);
1468 hfs_unlock(VTOC(hfsmp
->hfs_startup_vp
));
1469 if (retval
&& !force
)
1473 if (hfsmp
->hfs_attribute_vp
) {
1474 (void) hfs_lock(VTOC(hfsmp
->hfs_attribute_vp
), HFS_EXCLUSIVE_LOCK
);
1475 retval
= hfs_fsync(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, p
);
1476 hfs_unlock(VTOC(hfsmp
->hfs_attribute_vp
));
1477 if (retval
&& !force
)
1481 (void) hfs_lock(VTOC(hfsmp
->hfs_catalog_vp
), HFS_EXCLUSIVE_LOCK
);
1482 retval
= hfs_fsync(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, p
);
1483 hfs_unlock(VTOC(hfsmp
->hfs_catalog_vp
));
1484 if (retval
&& !force
)
1487 (void) hfs_lock(VTOC(hfsmp
->hfs_extents_vp
), HFS_EXCLUSIVE_LOCK
);
1488 retval
= hfs_fsync(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, p
);
1489 hfs_unlock(VTOC(hfsmp
->hfs_extents_vp
));
1490 if (retval
&& !force
)
1493 if (hfsmp
->hfs_allocation_vp
) {
1494 (void) hfs_lock(VTOC(hfsmp
->hfs_allocation_vp
), HFS_EXCLUSIVE_LOCK
);
1495 retval
= hfs_fsync(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, p
);
1496 hfs_unlock(VTOC(hfsmp
->hfs_allocation_vp
));
1497 if (retval
&& !force
)
1501 if (hfsmp
->hfc_filevp
&& vnode_issystem(hfsmp
->hfc_filevp
)) {
1502 retval
= hfs_fsync(hfsmp
->hfc_filevp
, MNT_WAIT
, 0, p
);
1503 if (retval
&& !force
)
1507 /* If runtime corruption was detected, indicate that the volume
1508 * was not unmounted cleanly.
1510 if (hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
) {
1511 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1513 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1516 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1518 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1520 goto err_exit
; /* could not flush everything */
1524 hfs_end_transaction(hfsmp
);
1530 journal_flush(hfsmp
->jnl
);
1534 * Invalidate our caches and release metadata vnodes
1536 (void) hfsUnmount(hfsmp
, p
);
1539 * Last chance to dump unreferenced system files.
1541 (void) vflush(mp
, NULLVP
, FORCECLOSE
);
1543 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1544 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1548 journal_close(hfsmp
->jnl
);
1552 VNOP_FSYNC(hfsmp
->hfs_devvp
, MNT_WAIT
, context
);
1554 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1555 retval
= VNOP_CLOSE(hfsmp
->jvp
,
1556 hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
,
1558 vnode_put(hfsmp
->jvp
);
1563 #ifdef HFS_SPARSE_DEV
1564 /* Drop our reference on the backing fs (if any). */
1565 if ((hfsmp
->hfs_flags
& HFS_HAS_SPARSE_DEVICE
) && hfsmp
->hfs_backingfs_rootvp
) {
1566 struct vnode
* tmpvp
;
1568 hfsmp
->hfs_flags
&= ~HFS_HAS_SPARSE_DEVICE
;
1569 tmpvp
= hfsmp
->hfs_backingfs_rootvp
;
1570 hfsmp
->hfs_backingfs_rootvp
= NULLVP
;
1573 #endif /* HFS_SPARSE_DEV */
1574 lck_mtx_destroy(&hfsmp
->hfc_mutex
, hfs_mutex_group
);
1575 vnode_rele(hfsmp
->hfs_devvp
);
1576 FREE(hfsmp
, M_HFSMNT
);
1582 hfs_end_transaction(hfsmp
);
1589 * Return the root of a filesystem.
1592 hfs_vfs_root(struct mount
*mp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1594 return hfs_vget(VFSTOHFS(mp
), (cnid_t
)kHFSRootFolderID
, vpp
, 1);
1599 * Do operations associated with quotas
1603 hfs_quotactl(__unused
struct mount
*mp
, __unused
int cmds
, __unused uid_t uid
, __unused caddr_t datap
, __unused vfs_context_t context
)
1609 hfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t datap
, vfs_context_t context
)
1611 struct proc
*p
= vfs_context_proc(context
);
1612 int cmd
, type
, error
;
1615 uid
= vfs_context_ucred(context
)->cr_ruid
;
1616 cmd
= cmds
>> SUBCMDSHIFT
;
1623 if (uid
== vfs_context_ucred(context
)->cr_ruid
)
1627 if ( (error
= vfs_context_suser(context
)) )
1631 type
= cmds
& SUBCMDMASK
;
1632 if ((u_int
)type
>= MAXQUOTAS
)
1634 if (vfs_busy(mp
, LK_NOWAIT
))
1640 error
= hfs_quotaon(p
, mp
, type
, datap
);
1644 error
= hfs_quotaoff(p
, mp
, type
);
1648 error
= hfs_setquota(mp
, uid
, type
, datap
);
1652 error
= hfs_setuse(mp
, uid
, type
, datap
);
1656 error
= hfs_getquota(mp
, uid
, type
, datap
);
1660 error
= hfs_qsync(mp
);
1664 error
= hfs_quotastat(mp
, type
, datap
);
1677 /* Subtype is composite of bits */
1678 #define HFS_SUBTYPE_JOURNALED 0x01
1679 #define HFS_SUBTYPE_CASESENSITIVE 0x02
1680 /* bits 2 - 6 reserved */
1681 #define HFS_SUBTYPE_STANDARDHFS 0x80
1684 * Get file system statistics.
1687 hfs_statfs(struct mount
*mp
, register struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
1689 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1690 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1692 u_int16_t subtype
= 0;
1694 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1696 sbp
->f_bsize
= (u_int32_t
)vcb
->blockSize
;
1697 sbp
->f_iosize
= (size_t)cluster_max_io_size(mp
, 0);
1698 sbp
->f_blocks
= (u_int64_t
)((unsigned long)vcb
->totalBlocks
);
1699 sbp
->f_bfree
= (u_int64_t
)((unsigned long )hfs_freeblks(hfsmp
, 0));
1700 sbp
->f_bavail
= (u_int64_t
)((unsigned long )hfs_freeblks(hfsmp
, 1));
1701 sbp
->f_files
= (u_int64_t
)((unsigned long )(vcb
->totalBlocks
- 2)); /* max files is constrained by total blocks */
1702 sbp
->f_ffree
= (u_int64_t
)((unsigned long )(MIN(freeCNIDs
, sbp
->f_bavail
)));
1705 * Subtypes (flavors) for HFS
1706 * 0: Mac OS Extended
1707 * 1: Mac OS Extended (Journaled)
1708 * 2: Mac OS Extended (Case Sensitive)
1709 * 3: Mac OS Extended (Case Sensitive, Journaled)
1711 * 128: Mac OS Standard
1714 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
1715 subtype
= HFS_SUBTYPE_STANDARDHFS
;
1716 } else /* HFS Plus */ {
1718 subtype
|= HFS_SUBTYPE_JOURNALED
;
1719 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
1720 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
1722 sbp
->f_fssubtype
= subtype
;
1729 // XXXdbg -- this is a callback to be used by the journal to
1730 // get meta data blocks flushed out to disk.
1732 // XXXdbg -- be smarter and don't flush *every* block on each
1733 // call. try to only flush some so we don't wind up
1734 // being too synchronous.
1738 hfs_sync_metadata(void *arg
)
1740 struct mount
*mp
= (struct mount
*)arg
;
1741 struct hfsmount
*hfsmp
;
1745 daddr64_t priIDSector
;
1746 hfsmp
= VFSTOHFS(mp
);
1747 vcb
= HFSTOVCB(hfsmp
);
1749 // now make sure the super block is flushed
1750 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
1751 HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
));
1753 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
1754 HFS_PHYSBLK_ROUNDDOWN(priIDSector
, hfsmp
->hfs_log_per_phys
),
1755 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
1756 if ((retval
!= 0 ) && (retval
!= ENXIO
)) {
1757 printf("hfs_sync_metadata: can't read volume header at %d! (retval 0x%x)\n",
1758 (int)priIDSector
, retval
);
1761 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1767 // the alternate super block...
1768 // XXXdbg - we probably don't need to do this each and every time.
1769 // hfs_btreeio.c:FlushAlternate() should flag when it was
1771 if (hfsmp
->hfs_alt_id_sector
) {
1772 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
1773 HFS_PHYSBLK_ROUNDDOWN(hfsmp
->hfs_alt_id_sector
, hfsmp
->hfs_log_per_phys
),
1774 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
1775 if (retval
== 0 && ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
)) {
1784 struct hfs_sync_cargs
{
1793 hfs_sync_callback(struct vnode
*vp
, void *cargs
)
1796 struct hfs_sync_cargs
*args
;
1799 args
= (struct hfs_sync_cargs
*)cargs
;
1801 if (hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1802 return (VNODE_RETURNED
);
1806 if ((cp
->c_flag
& C_MODIFIED
) ||
1807 (cp
->c_touch_acctime
| cp
->c_touch_chgtime
| cp
->c_touch_modtime
) ||
1808 vnode_hasdirtyblks(vp
)) {
1809 error
= hfs_fsync(vp
, args
->waitfor
, 0, args
->p
);
1812 args
->error
= error
;
1815 return (VNODE_RETURNED
);
1821 * Go through the disk queues to initiate sandbagged IO;
1822 * go through the inodes to write those that have been modified;
1823 * initiate the writing of the super block if it has been modified.
1825 * Note: we are always called with the filesystem marked `MPBUSY'.
1828 hfs_sync(struct mount
*mp
, int waitfor
, vfs_context_t context
)
1830 struct proc
*p
= vfs_context_proc(context
);
1832 struct hfsmount
*hfsmp
;
1834 struct vnode
*meta_vp
[4];
1836 int error
, allerror
= 0;
1837 struct hfs_sync_cargs args
;
1839 hfsmp
= VFSTOHFS(mp
);
1842 * hfs_changefs might be manipulating vnodes so back off
1844 if (hfsmp
->hfs_flags
& HFS_IN_CHANGEFS
)
1847 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
)
1850 /* skip over frozen volumes */
1851 if (!lck_rw_try_lock_shared(&hfsmp
->hfs_insync
))
1854 args
.cred
= kauth_cred_get();
1855 args
.waitfor
= waitfor
;
1859 * hfs_sync_callback will be called for each vnode
1860 * hung off of this mount point... the vnode will be
1861 * properly referenced and unreferenced around the callback
1863 vnode_iterate(mp
, 0, hfs_sync_callback
, (void *)&args
);
1866 allerror
= args
.error
;
1868 vcb
= HFSTOVCB(hfsmp
);
1870 meta_vp
[0] = vcb
->extentsRefNum
;
1871 meta_vp
[1] = vcb
->catalogRefNum
;
1872 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1873 meta_vp
[3] = hfsmp
->hfs_attribute_vp
; /* Optional file */
1875 /* Now sync our three metadata files */
1876 for (i
= 0; i
< 4; ++i
) {
1880 if ((btvp
==0) || (vnode_mount(btvp
) != mp
))
1883 /* XXX use hfs_systemfile_lock instead ? */
1884 (void) hfs_lock(VTOC(btvp
), HFS_EXCLUSIVE_LOCK
);
1887 if (((cp
->c_flag
& C_MODIFIED
) == 0) &&
1888 (cp
->c_touch_acctime
== 0) &&
1889 (cp
->c_touch_chgtime
== 0) &&
1890 (cp
->c_touch_modtime
== 0) &&
1891 vnode_hasdirtyblks(btvp
) == 0) {
1892 hfs_unlock(VTOC(btvp
));
1895 error
= vnode_get(btvp
);
1897 hfs_unlock(VTOC(btvp
));
1900 if ((error
= hfs_fsync(btvp
, waitfor
, 0, p
)))
1908 * Force stale file system control information to be flushed.
1910 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1911 if ((error
= VNOP_FSYNC(hfsmp
->hfs_devvp
, waitfor
, context
))) {
1919 hfs_hotfilesync(hfsmp
, vfs_context_kernel());
1922 * Write back modified superblock.
1924 if (IsVCBDirty(vcb
)) {
1925 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1931 journal_flush(hfsmp
->jnl
);
1934 lck_rw_unlock_shared(&hfsmp
->hfs_insync
);
1940 * File handle to vnode
1942 * Have to be really careful about stale file handles:
1943 * - check that the cnode id is valid
1944 * - call hfs_vget() to get the locked cnode
1945 * - check for an unallocated cnode (i_mode == 0)
1946 * - check that the given client host has export rights and return
1947 * those rights via. exflagsp and credanonp
1950 hfs_fhtovp(struct mount
*mp
, int fhlen
, unsigned char *fhp
, struct vnode
**vpp
, __unused vfs_context_t context
)
1952 struct hfsfid
*hfsfhp
;
1957 hfsfhp
= (struct hfsfid
*)fhp
;
1959 if (fhlen
< (int)sizeof(struct hfsfid
))
1962 result
= hfs_vget(VFSTOHFS(mp
), ntohl(hfsfhp
->hfsfid_cnid
), &nvp
, 0);
1964 if (result
== ENOENT
)
1969 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1970 * For NFS, we are assuming that only if the createtime was moved
1971 * forward would it mean the fileID got reused in that session by
1972 * wrapping. We don't have a volume ID or other unique identifier to
1973 * to use here for a generation ID across reboots, crashes where
1974 * metadata noting lastFileID didn't make it to disk but client has
1975 * it, or volume erasures where fileIDs start over again. Lastly,
1976 * with HFS allowing "wraps" of fileIDs now, this becomes more
1977 * error prone. Future, would be change the "wrap bit" to a unique
1978 * wrap number and use that for generation number. For now do this.
1980 if (((time_t)(ntohl(hfsfhp
->hfsfid_gen
)) < VTOC(nvp
)->c_itime
)) {
1981 hfs_unlock(VTOC(nvp
));
1987 hfs_unlock(VTOC(nvp
));
1993 * Vnode pointer to File handle
1997 hfs_vptofh(struct vnode
*vp
, int *fhlenp
, unsigned char *fhp
, __unused vfs_context_t context
)
2000 struct hfsfid
*hfsfhp
;
2002 if (ISHFS(VTOVCB(vp
)))
2003 return (ENOTSUP
); /* hfs standard is not exportable */
2005 if (*fhlenp
< (int)sizeof(struct hfsfid
))
2009 hfsfhp
= (struct hfsfid
*)fhp
;
2010 hfsfhp
->hfsfid_cnid
= htonl(cp
->c_fileid
);
2011 hfsfhp
->hfsfid_gen
= htonl(cp
->c_itime
);
2012 *fhlenp
= sizeof(struct hfsfid
);
2019 * Initial HFS filesystems, done only once.
2022 hfs_init(__unused
struct vfsconf
*vfsp
)
2024 static int done
= 0;
2030 hfs_converterinit();
2035 hfs_lock_attr
= lck_attr_alloc_init();
2036 hfs_group_attr
= lck_grp_attr_alloc_init();
2037 hfs_mutex_group
= lck_grp_alloc_init("hfs-mutex", hfs_group_attr
);
2038 hfs_rwlock_group
= lck_grp_alloc_init("hfs-rwlock", hfs_group_attr
);
2045 hfs_getmountpoint(struct vnode
*vp
, struct hfsmount
**hfsmpp
)
2047 struct hfsmount
* hfsmp
;
2048 char fstypename
[MFSNAMELEN
];
2053 if (!vnode_isvroot(vp
))
2056 vnode_vfsname(vp
, fstypename
);
2057 if (strncmp(fstypename
, "hfs", sizeof(fstypename
)) != 0)
2062 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
2071 #include <sys/filedesc.h>
2074 * HFS filesystem related variables.
2077 hfs_sysctl(int *name
, __unused u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
2078 user_addr_t newp
, size_t newlen
, vfs_context_t context
)
2080 struct proc
*p
= vfs_context_proc(context
);
2082 struct hfsmount
*hfsmp
;
2084 /* all sysctl names at this level are terminal */
2086 if (name
[0] == HFS_ENCODINGBIAS
) {
2089 bias
= hfs_getencodingbias();
2090 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &bias
);
2091 if (error
== 0 && newp
)
2092 hfs_setencodingbias(bias
);
2095 } else if (name
[0] == HFS_EXTEND_FS
) {
2097 vnode_t vp
= vfs_context_cwd(context
);
2099 if (newp
== USER_ADDR_NULL
|| vp
== NULLVP
)
2101 if ((error
= hfs_getmountpoint(vp
, &hfsmp
)))
2103 error
= sysctl_quad(oldp
, oldlenp
, newp
, newlen
, (quad_t
*)&newsize
);
2107 error
= hfs_extendfs(hfsmp
, newsize
, context
);
2110 } else if (name
[0] == HFS_ENCODINGHINT
) {
2114 u_int16_t
*unicode_name
;
2117 if ((newlen
<= 0) || (newlen
> MAXPATHLEN
))
2120 bufsize
= MAX(newlen
* 3, MAXPATHLEN
);
2121 MALLOC(filename
, char *, newlen
, M_TEMP
, M_WAITOK
);
2122 MALLOC(unicode_name
, u_int16_t
*, bufsize
, M_TEMP
, M_WAITOK
);
2124 error
= copyin(newp
, (caddr_t
)filename
, newlen
);
2126 error
= utf8_decodestr((u_int8_t
*)filename
, newlen
- 1, unicode_name
,
2127 &bytes
, bufsize
, 0, UTF_DECOMPOSED
);
2129 hint
= hfs_pickencoding(unicode_name
, bytes
/ 2);
2130 error
= sysctl_int(oldp
, oldlenp
, USER_ADDR_NULL
, 0, (int32_t *)&hint
);
2133 FREE(unicode_name
, M_TEMP
);
2134 FREE(filename
, M_TEMP
);
2137 } else if (name
[0] == HFS_ENABLE_JOURNALING
) {
2138 // make the file system journaled...
2139 vnode_t vp
= vfs_context_cwd(context
);
2142 struct cat_attr jnl_attr
, jinfo_attr
;
2143 struct cat_fork jnl_fork
, jinfo_fork
;
2147 /* Only root can enable journaling */
2155 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2158 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
2159 printf("hfs: can't make a plain hfs volume journaled.\n");
2164 printf("hfs: volume @ mp %p is already journaled!\n", vnode_mount(vp
));
2168 vcb
= HFSTOVCB(hfsmp
);
2169 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_EXCLUSIVE_LOCK
);
2170 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
2171 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
2173 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
2174 hfs_systemfile_unlock(hfsmp
, lockflags
);
2177 hfs_systemfile_unlock(hfsmp
, lockflags
);
2179 // make sure these both exist!
2180 if ( GetFileInfo(vcb
, kHFSRootFolderID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
2181 || GetFileInfo(vcb
, kHFSRootFolderID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
2186 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, context
);
2188 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2189 (off_t
)name
[2], (off_t
)name
[3]);
2191 jvp
= hfsmp
->hfs_devvp
;
2192 jnl
= journal_create(jvp
,
2193 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
2194 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
2195 (off_t
)((unsigned)name
[3]),
2197 hfsmp
->hfs_logical_block_size
,
2200 hfs_sync_metadata
, hfsmp
->hfs_mp
);
2203 printf("hfs: FAILED to create the journal!\n");
2204 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
2205 VNOP_CLOSE(jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2212 hfs_global_exclusive_lock_acquire(hfsmp
);
2215 * Flush all dirty metadata buffers.
2217 buf_flushdirtyblks(hfsmp
->hfs_devvp
, MNT_WAIT
, 0, "hfs_sysctl");
2218 buf_flushdirtyblks(hfsmp
->hfs_extents_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2219 buf_flushdirtyblks(hfsmp
->hfs_catalog_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2220 buf_flushdirtyblks(hfsmp
->hfs_allocation_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2221 if (hfsmp
->hfs_attribute_vp
)
2222 buf_flushdirtyblks(hfsmp
->hfs_attribute_vp
, MNT_WAIT
, 0, "hfs_sysctl");
2224 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
2225 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
2229 // save this off for the hack-y check in hfs_remove()
2230 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
2231 hfsmp
->jnl_size
= (off_t
)((unsigned)name
[3]);
2232 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
2233 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
2235 vfs_setflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
2237 hfs_global_exclusive_lock_release(hfsmp
);
2238 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2241 } else if (name
[0] == HFS_DISABLE_JOURNALING
) {
2242 // clear the journaling bit
2243 vnode_t vp
= vfs_context_cwd(context
);
2245 /* Only root can disable journaling */
2255 * Disabling journaling is disallowed on volumes with directory hard links
2256 * because we have not tested the relevant code path.
2258 if (hfsmp
->hfs_private_attr
[DIR_HARDLINKS
].ca_entries
!= 0){
2259 printf("hfs: cannot disable journaling on volumes with directory hardlinks\n");
2263 printf("hfs: disabling journaling for mount @ %p\n", vnode_mount(vp
));
2265 hfs_global_exclusive_lock_acquire(hfsmp
);
2267 // Lights out for you buddy!
2268 journal_close(hfsmp
->jnl
);
2271 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
2272 VNOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_flags
& HFS_READ_ONLY
? FREAD
: FREAD
|FWRITE
, context
);
2275 vfs_clearflags(hfsmp
->hfs_mp
, (u_int64_t
)((unsigned int)MNT_JOURNALED
));
2276 hfsmp
->jnl_start
= 0;
2277 hfsmp
->hfs_jnlinfoblkid
= 0;
2278 hfsmp
->hfs_jnlfileid
= 0;
2280 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
2282 hfs_global_exclusive_lock_release(hfsmp
);
2283 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
2286 } else if (name
[0] == HFS_GET_JOURNAL_INFO
) {
2287 vnode_t vp
= vfs_context_cwd(context
);
2288 off_t jnl_start
, jnl_size
;
2294 if (hfsmp
->jnl
== NULL
) {
2298 jnl_start
= (off_t
)(hfsmp
->jnl_start
* HFSTOVCB(hfsmp
)->blockSize
) + (off_t
)HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
;
2299 jnl_size
= (off_t
)hfsmp
->jnl_size
;
2302 if ((error
= copyout((caddr_t
)&jnl_start
, CAST_USER_ADDR_T(name
[1]), sizeof(off_t
))) != 0) {
2305 if ((error
= copyout((caddr_t
)&jnl_size
, CAST_USER_ADDR_T(name
[2]), sizeof(off_t
))) != 0) {
2310 } else if (name
[0] == HFS_SET_PKG_EXTENSIONS
) {
2312 return set_package_extensions_table((void *)name
[1], name
[2], name
[3]);
2314 } else if (name
[0] == VFS_CTL_QUERY
) {
2315 struct sysctl_req
*req
;
2317 struct user_vfsidctl user_vc
;
2320 boolean_t is_64_bit
;
2322 is_64_bit
= proc_is64bit(p
);
2323 req
= CAST_DOWN(struct sysctl_req
*, oldp
); /* we're new style vfs sysctl. */
2326 error
= SYSCTL_IN(req
, &user_vc
, sizeof(user_vc
));
2327 if (error
) return (error
);
2329 mp
= vfs_getvfs(&user_vc
.vc_fsid
);
2332 error
= SYSCTL_IN(req
, &vc
, sizeof(vc
));
2333 if (error
) return (error
);
2335 mp
= vfs_getvfs(&vc
.vc_fsid
);
2337 if (mp
== NULL
) return (ENOENT
);
2339 hfsmp
= VFSTOHFS(mp
);
2340 bzero(&vq
, sizeof(vq
));
2341 vq
.vq_flags
= hfsmp
->hfs_notification_conditions
;
2342 return SYSCTL_OUT(req
, &vq
, sizeof(vq
));;
2343 } else if (name
[0] == HFS_REPLAY_JOURNAL
) {
2344 char *devnode
= NULL
;
2347 devnode_len
= *oldlenp
;
2348 MALLOC(devnode
, char *, devnode_len
+ 1, M_TEMP
, M_WAITOK
);
2349 if (devnode
== NULL
) {
2353 error
= copyin(oldp
, (caddr_t
)devnode
, devnode_len
);
2355 FREE(devnode
, M_TEMP
);
2358 devnode
[devnode_len
] = 0;
2360 error
= hfs_journal_replay(devnode
, context
);
2361 FREE(devnode
, M_TEMP
);
2368 /* hfs_vfs_vget is not static since it is used in hfs_readwrite.c to support the
2369 * build_path ioctl. We use it to leverage the code below that updates the origin
2370 * cache if necessary.
2373 hfs_vfs_vget(struct mount
*mp
, ino64_t ino
, struct vnode
**vpp
, __unused vfs_context_t context
)
2377 struct hfsmount
*hfsmp
;
2379 hfsmp
= VFSTOHFS(mp
);
2381 error
= hfs_vget(hfsmp
, (cnid_t
)ino
, vpp
, 1);
2386 * ADLs may need to have their origin state updated
2387 * since build_path needs a valid parent. The same is true
2388 * for hardlinked files as well. There isn't a race window here in re-acquiring
2389 * the cnode lock since we aren't pulling any data out of the cnode; instead, we're
2390 * going back to the catalog.
2392 if ((VTOC(*vpp
)->c_flag
& C_HARDLINK
) &&
2393 (hfs_lock(VTOC(*vpp
), HFS_EXCLUSIVE_LOCK
) == 0)) {
2394 cnode_t
*cp
= VTOC(*vpp
);
2395 struct cat_desc cdesc
;
2397 if (!hfs_haslinkorigin(cp
)) {
2398 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2399 error
= cat_findname(hfsmp
, (cnid_t
)ino
, &cdesc
);
2400 hfs_systemfile_unlock(hfsmp
, lockflags
);
2402 if ((cdesc
.cd_parentcnid
!=
2403 hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) &&
2404 (cdesc
.cd_parentcnid
!=
2405 hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
)) {
2406 hfs_savelinkorigin(cp
, cdesc
.cd_parentcnid
);
2408 cat_releasedesc(&cdesc
);
2418 * Look up an HFS object by ID.
2420 * The object is returned with an iocount reference and the cnode locked.
2422 * If the object is a file then it will represent the data fork.
2426 hfs_vget(struct hfsmount
*hfsmp
, cnid_t cnid
, struct vnode
**vpp
, int skiplock
)
2428 struct vnode
*vp
= NULLVP
;
2429 struct cat_desc cndesc
;
2430 struct cat_attr cnattr
;
2431 struct cat_fork cnfork
;
2432 u_int32_t linkref
= 0;
2435 /* Check for cnids that should't be exported. */
2436 if ((cnid
< kHFSFirstUserCatalogNodeID
) &&
2437 (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
)) {
2440 /* Don't export our private directories. */
2441 if (cnid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
||
2442 cnid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) {
2446 * Check the hash first
2448 vp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, cnid
, 0, skiplock
);
2454 bzero(&cndesc
, sizeof(cndesc
));
2455 bzero(&cnattr
, sizeof(cnattr
));
2456 bzero(&cnfork
, sizeof(cnfork
));
2459 * Not in hash, lookup in catalog
2461 if (cnid
== kHFSRootParentID
) {
2462 static char hfs_rootname
[] = "/";
2464 cndesc
.cd_nameptr
= (const u_int8_t
*)&hfs_rootname
[0];
2465 cndesc
.cd_namelen
= 1;
2466 cndesc
.cd_parentcnid
= kHFSRootParentID
;
2467 cndesc
.cd_cnid
= kHFSRootFolderID
;
2468 cndesc
.cd_flags
= CD_ISDIR
;
2470 cnattr
.ca_fileid
= kHFSRootFolderID
;
2471 cnattr
.ca_linkcount
= 1;
2472 cnattr
.ca_entries
= 1;
2473 cnattr
.ca_dircount
= 1;
2474 cnattr
.ca_mode
= (S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
2478 const char *nameptr
;
2480 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2481 error
= cat_idlookup(hfsmp
, cnid
, 0, &cndesc
, &cnattr
, &cnfork
);
2482 hfs_systemfile_unlock(hfsmp
, lockflags
);
2490 * Check for a raw hardlink inode and save its linkref.
2492 pid
= cndesc
.cd_parentcnid
;
2493 nameptr
= (const char *)cndesc
.cd_nameptr
;
2495 if ((pid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2496 (bcmp(nameptr
, HFS_INODE_PREFIX
, HFS_INODE_PREFIX_LEN
) == 0)) {
2497 linkref
= strtoul(&nameptr
[HFS_INODE_PREFIX_LEN
], NULL
, 10);
2499 } else if ((pid
== hfsmp
->hfs_private_desc
[DIR_HARDLINKS
].cd_cnid
) &&
2500 (bcmp(nameptr
, HFS_DIRINODE_PREFIX
, HFS_DIRINODE_PREFIX_LEN
) == 0)) {
2501 linkref
= strtoul(&nameptr
[HFS_DIRINODE_PREFIX_LEN
], NULL
, 10);
2503 } else if ((pid
== hfsmp
->hfs_private_desc
[FILE_HARDLINKS
].cd_cnid
) &&
2504 (bcmp(nameptr
, HFS_DELETE_PREFIX
, HFS_DELETE_PREFIX_LEN
) == 0)) {
2506 cat_releasedesc(&cndesc
);
2507 return (ENOENT
); /* open unlinked file */
2512 * Finish initializing cnode descriptor for hardlinks.
2514 * We need a valid name and parent for reverse lookups.
2519 struct cat_desc linkdesc
;
2522 cnattr
.ca_linkref
= linkref
;
2525 * Pick up the first link in the chain and get a descriptor for it.
2526 * This allows blind volfs paths to work for hardlinks.
2528 if ((hfs_lookuplink(hfsmp
, linkref
, &prevlinkid
, &nextlinkid
) == 0) &&
2529 (nextlinkid
!= 0)) {
2530 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
2531 error
= cat_findname(hfsmp
, nextlinkid
, &linkdesc
);
2532 hfs_systemfile_unlock(hfsmp
, lockflags
);
2534 cat_releasedesc(&cndesc
);
2535 bcopy(&linkdesc
, &cndesc
, sizeof(linkdesc
));
2541 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2543 VTOC(vp
)->c_flag
|= C_HARDLINK
;
2544 vnode_setmultipath(vp
);
2547 struct componentname cn
;
2549 /* Supply hfs_getnewvnode with a component name. */
2550 MALLOC_ZONE(cn
.cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2551 cn
.cn_nameiop
= LOOKUP
;
2552 cn
.cn_flags
= ISLASTCN
| HASBUF
;
2553 cn
.cn_context
= NULL
;
2554 cn
.cn_pnlen
= MAXPATHLEN
;
2555 cn
.cn_nameptr
= cn
.cn_pnbuf
;
2556 cn
.cn_namelen
= cndesc
.cd_namelen
;
2559 bcopy(cndesc
.cd_nameptr
, cn
.cn_nameptr
, cndesc
.cd_namelen
+ 1);
2561 error
= hfs_getnewvnode(hfsmp
, NULLVP
, &cn
, &cndesc
, 0, &cnattr
, &cnfork
, &vp
);
2563 if ((error
== 0) && (VTOC(vp
)->c_flag
& C_HARDLINK
)) {
2564 hfs_savelinkorigin(VTOC(vp
), cndesc
.cd_parentcnid
);
2566 FREE_ZONE(cn
.cn_pnbuf
, cn
.cn_pnlen
, M_NAMEI
);
2568 cat_releasedesc(&cndesc
);
2571 if (vp
&& skiplock
) {
2572 hfs_unlock(VTOC(vp
));
2579 * Flush out all the files in a filesystem.
2583 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2585 hfs_flushfiles(struct mount
*mp
, int flags
, __unused
struct proc
*p
)
2588 struct hfsmount
*hfsmp
;
2589 struct vnode
*skipvp
= NULLVP
;
2596 hfsmp
= VFSTOHFS(mp
);
2600 * The open quota files have an indirect reference on
2601 * the root directory vnode. We must account for this
2602 * extra reference when doing the intial vflush.
2605 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2607 /* Find out how many quota files we have open. */
2608 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2609 if (hfsmp
->hfs_qfiles
[i
].qf_vp
!= NULLVP
)
2613 /* Obtain the root vnode so we can skip over it. */
2614 skipvp
= hfs_chash_getvnode(hfsmp
->hfs_raw_dev
, kHFSRootFolderID
, 0, 0);
2618 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| SKIPSWAP
| flags
);
2622 error
= vflush(mp
, skipvp
, SKIPSYSTEM
| flags
);
2625 if (((unsigned int)vfs_flags(mp
)) & MNT_QUOTA
) {
2628 * See if there are additional references on the
2629 * root vp besides the ones obtained from the open
2630 * quota files and the hfs_chash_getvnode call above.
2633 (vnode_isinuse(skipvp
, quotafilecnt
))) {
2634 error
= EBUSY
; /* root directory is still open */
2636 hfs_unlock(VTOC(skipvp
));
2639 if (error
&& (flags
& FORCECLOSE
) == 0)
2642 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2643 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2645 hfs_quotaoff(p
, mp
, i
);
2647 error
= vflush(mp
, NULLVP
, SKIPSYSTEM
| flags
);
2655 * Update volume encoding bitmap (HFS Plus only)
2659 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2661 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2662 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2667 case kTextEncodingMacUkrainian
:
2668 index
= kIndexMacUkrainian
;
2670 case kTextEncodingMacFarsi
:
2671 index
= kIndexMacFarsi
;
2678 if (index
< 64 && (hfsmp
->encodingsBitmap
& (u_int64_t
)(1ULL << index
)) == 0) {
2679 HFS_MOUNT_LOCK(hfsmp
, TRUE
)
2680 hfsmp
->encodingsBitmap
|= (u_int64_t
)(1ULL << index
);
2681 MarkVCBDirty(hfsmp
);
2682 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
2687 * Update volume stats
2689 * On journal volumes this will cause a volume header flush
2693 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2699 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2701 MarkVCBDirty(hfsmp
);
2702 hfsmp
->hfs_mtime
= tv
.tv_sec
;
2708 if (hfsmp
->hfs_dircount
!= 0xFFFFFFFF)
2709 ++hfsmp
->hfs_dircount
;
2710 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2711 ++hfsmp
->vcbNmRtDirs
;
2714 if (hfsmp
->hfs_dircount
!= 0)
2715 --hfsmp
->hfs_dircount
;
2716 if (inroot
&& hfsmp
->vcbNmRtDirs
!= 0xFFFF)
2717 --hfsmp
->vcbNmRtDirs
;
2720 if (hfsmp
->hfs_filecount
!= 0xFFFFFFFF)
2721 ++hfsmp
->hfs_filecount
;
2722 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2726 if (hfsmp
->hfs_filecount
!= 0)
2727 --hfsmp
->hfs_filecount
;
2728 if (inroot
&& hfsmp
->vcbNmFls
!= 0xFFFF)
2733 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2736 hfs_flushvolumeheader(hfsmp
, 0, 0);
2744 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2746 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2747 struct filefork
*fp
;
2748 HFSMasterDirectoryBlock
*mdb
;
2749 struct buf
*bp
= NULL
;
2754 sectorsize
= hfsmp
->hfs_logical_block_size
;
2755 retval
= (int)buf_bread(hfsmp
->hfs_devvp
, (daddr64_t
)HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2762 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2764 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp
) + HFS_PRI_OFFSET(sectorsize
));
2766 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2767 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2768 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2769 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2770 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2771 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2772 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2773 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2775 namelen
= strlen((char *)vcb
->vcbVN
);
2776 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2777 /* Retry with MacRoman in case that's how it was exported. */
2779 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2781 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2782 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2783 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2784 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2785 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2787 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2789 fp
= VTOF(vcb
->extentsRefNum
);
2790 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2791 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2792 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2793 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2794 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2795 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2796 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2797 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2798 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2800 fp
= VTOF(vcb
->catalogRefNum
);
2801 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2802 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2803 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2804 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2805 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2806 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2807 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2808 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2809 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2811 MarkVCBClean( vcb
);
2813 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
2815 /* If requested, flush out the alternate MDB */
2817 struct buf
*alt_bp
= NULL
;
2819 if (buf_meta_bread(hfsmp
->hfs_devvp
, hfsmp
->hfs_alt_id_sector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2820 bcopy(mdb
, (char *)buf_dataptr(alt_bp
) + HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2822 (void) VNOP_BWRITE(alt_bp
);
2827 if (waitfor
!= MNT_WAIT
)
2830 retval
= VNOP_BWRITE(bp
);
2836 * Flush any dirty in-memory mount data to the on-disk
2839 * Note: the on-disk volume signature is intentionally
2840 * not flushed since the on-disk "H+" and "HX" signatures
2841 * are always stored in-memory as "H+".
2845 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2847 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2848 struct filefork
*fp
;
2849 HFSPlusVolumeHeader
*volumeHeader
;
2853 daddr64_t priIDSector
;
2855 u_int16_t signature
;
2856 u_int16_t hfsversion
;
2858 if (hfsmp
->hfs_flags
& HFS_READ_ONLY
) {
2861 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
2862 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2864 critical
= altflush
;
2865 priIDSector
= (daddr64_t
)((vcb
->hfsPlusIOPosOffset
/ hfsmp
->hfs_logical_block_size
) +
2866 HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
));
2868 if (hfs_start_transaction(hfsmp
) != 0) {
2872 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
2873 HFS_PHYSBLK_ROUNDDOWN(priIDSector
, hfsmp
->hfs_log_per_phys
),
2874 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
);
2879 hfs_end_transaction(hfsmp
);
2881 printf("HFS: err %d reading VH blk (%s)\n", retval
, vcb
->vcbVN
);
2886 journal_modify_block_start(hfsmp
->jnl
, bp
);
2889 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)buf_dataptr(bp
) +
2890 HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
2893 * Sanity check what we just read.
2895 signature
= SWAP_BE16 (volumeHeader
->signature
);
2896 hfsversion
= SWAP_BE16 (volumeHeader
->version
);
2897 if ((signature
!= kHFSPlusSigWord
&& signature
!= kHFSXSigWord
) ||
2898 (hfsversion
< kHFSPlusVersion
) || (hfsversion
> 100) ||
2899 (SWAP_BE32 (volumeHeader
->blockSize
) != vcb
->blockSize
)) {
2901 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
2902 vcb
->vcbVN
, signature
, hfsversion
,
2903 SWAP_BE32 (volumeHeader
->blockSize
));
2905 printf("HFS: corrupt VH blk (%s)\n", vcb
->vcbVN
);
2911 * For embedded HFS+ volumes, update create date if it changed
2912 * (ie from a setattrlist call)
2914 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2915 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2917 HFSMasterDirectoryBlock
*mdb
;
2919 retval
= (int)buf_meta_bread(hfsmp
->hfs_devvp
,
2920 HFS_PHYSBLK_ROUNDDOWN(HFS_PRI_SECTOR(hfsmp
->hfs_logical_block_size
), hfsmp
->hfs_log_per_phys
),
2921 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp2
);
2927 mdb
= (HFSMasterDirectoryBlock
*)(buf_dataptr(bp2
) +
2928 HFS_PRI_OFFSET(hfsmp
->hfs_physical_block_size
));
2930 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2933 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2936 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2939 journal_modify_block_end(hfsmp
->jnl
, bp2
, NULL
, NULL
);
2941 (void) VNOP_BWRITE(bp2
); /* write out the changes */
2946 buf_brelse(bp2
); /* just release it */
2951 lck_mtx_lock(&hfsmp
->hfs_mutex
);
2953 /* Note: only update the lower 16 bits worth of attributes */
2954 volumeHeader
->attributes
= SWAP_BE32 (vcb
->vcbAtrb
);
2955 volumeHeader
->journalInfoBlock
= SWAP_BE32 (vcb
->vcbJinfoBlock
);
2957 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2959 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2961 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2962 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2963 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2964 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2965 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2966 volumeHeader
->totalBlocks
= SWAP_BE32 (vcb
->totalBlocks
);
2967 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2968 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2969 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2970 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2971 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2972 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2973 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2975 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0) {
2976 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2981 * System files are only dirty when altflush is set.
2983 if (altflush
== 0) {
2987 /* Sync Extents over-flow file meta data */
2988 fp
= VTOF(vcb
->extentsRefNum
);
2989 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
2990 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2991 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2992 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2993 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2994 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2996 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2997 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2998 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2999 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3002 /* Sync Catalog file meta data */
3003 fp
= VTOF(vcb
->catalogRefNum
);
3004 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
3005 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
3006 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
3007 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
3008 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
3009 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
3011 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
3012 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
3013 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
3014 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3017 /* Sync Allocation file meta data */
3018 fp
= VTOF(vcb
->allocationsRefNum
);
3019 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
3020 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
3021 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
3022 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
3023 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
3024 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
3026 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
3027 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
3028 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
3029 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3032 /* Sync Attribute file meta data */
3033 if (hfsmp
->hfs_attribute_vp
) {
3034 fp
= VTOF(hfsmp
->hfs_attribute_vp
);
3035 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
3036 volumeHeader
->attributesFile
.extents
[i
].startBlock
=
3037 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
3038 volumeHeader
->attributesFile
.extents
[i
].blockCount
=
3039 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
3041 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3042 volumeHeader
->attributesFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
3043 volumeHeader
->attributesFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
3044 volumeHeader
->attributesFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
3047 /* Sync Startup file meta data */
3048 if (hfsmp
->hfs_startup_vp
) {
3049 fp
= VTOF(hfsmp
->hfs_startup_vp
);
3050 if (FTOC(fp
)->c_flag
& C_MODIFIED
) {
3051 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
3052 volumeHeader
->startupFile
.extents
[i
].startBlock
=
3053 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
3054 volumeHeader
->startupFile
.extents
[i
].blockCount
=
3055 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
3057 volumeHeader
->startupFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
3058 volumeHeader
->startupFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
3059 volumeHeader
->startupFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
3060 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
3065 MarkVCBClean(hfsmp
);
3066 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3068 /* If requested, flush out the alternate volume header */
3069 if (altflush
&& hfsmp
->hfs_alt_id_sector
) {
3070 struct buf
*alt_bp
= NULL
;
3072 if (buf_meta_bread(hfsmp
->hfs_devvp
,
3073 HFS_PHYSBLK_ROUNDDOWN(hfsmp
->hfs_alt_id_sector
, hfsmp
->hfs_log_per_phys
),
3074 hfsmp
->hfs_physical_block_size
, NOCRED
, &alt_bp
) == 0) {
3076 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
3079 bcopy(volumeHeader
, (char *)buf_dataptr(alt_bp
) +
3080 HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
),
3084 journal_modify_block_end(hfsmp
->jnl
, alt_bp
, NULL
, NULL
);
3086 (void) VNOP_BWRITE(alt_bp
);
3093 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
3095 if (waitfor
!= MNT_WAIT
)
3098 retval
= VNOP_BWRITE(bp
);
3099 /* When critical data changes, flush the device cache */
3100 if (critical
&& (retval
== 0)) {
3101 (void) VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
3102 NULL
, FWRITE
, NULL
);
3106 hfs_end_transaction(hfsmp
);
3113 * Extend a file system.
3117 hfs_extendfs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
3119 struct proc
*p
= vfs_context_proc(context
);
3120 kauth_cred_t cred
= vfs_context_ucred(context
);
3122 struct vnode
*devvp
;
3124 struct filefork
*fp
= NULL
;
3126 struct cat_fork forkdata
;
3128 u_int64_t newblkcnt
;
3129 u_int64_t prev_phys_block_count
;
3131 u_int64_t sectorcnt
;
3132 u_int32_t sectorsize
;
3133 u_int32_t phys_sectorsize
;
3134 daddr64_t prev_alt_sector
;
3138 int64_t oldBitmapSize
;
3139 Boolean usedExtendFileC
= false;
3141 devvp
= hfsmp
->hfs_devvp
;
3142 vcb
= HFSTOVCB(hfsmp
);
3145 * - HFS Plus file systems only.
3146 * - Journaling must be enabled.
3147 * - No embedded volumes.
3149 if ((vcb
->vcbSigWord
== kHFSSigWord
) ||
3150 (hfsmp
->jnl
== NULL
) ||
3151 (vcb
->hfsPlusIOPosOffset
!= 0)) {
3155 * If extending file system by non-root, then verify
3156 * ownership and check permissions.
3158 if (suser(cred
, NULL
)) {
3159 error
= hfs_vget(hfsmp
, kHFSRootFolderID
, &vp
, 0);
3163 error
= hfs_owner_rights(hfsmp
, VTOC(vp
)->c_uid
, cred
, p
, 0);
3165 error
= hfs_write_access(vp
, cred
, p
, false);
3167 hfs_unlock(VTOC(vp
));
3172 error
= vnode_authorize(devvp
, NULL
, KAUTH_VNODE_READ_DATA
| KAUTH_VNODE_WRITE_DATA
, context
);
3176 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)§orsize
, 0, context
)) {
3179 if (sectorsize
!= hfsmp
->hfs_logical_block_size
) {
3182 if (VNOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)§orcnt
, 0, context
)) {
3185 if ((sectorsize
* sectorcnt
) < newsize
) {
3186 printf("hfs_extendfs: not enough space on device\n");
3189 error
= VNOP_IOCTL(devvp
, DKIOCGETPHYSICALBLOCKSIZE
, (caddr_t
)&phys_sectorsize
, 0, context
);
3191 if ((error
!= ENOTSUP
) && (error
!= ENOTTY
)) {
3194 /* If ioctl is not supported, force physical and logical sector size to be same */
3195 phys_sectorsize
= sectorsize
;
3197 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3200 * Validate new size.
3202 if ((newsize
<= oldsize
) || (newsize
% sectorsize
) || (newsize
% phys_sectorsize
)) {
3203 printf("hfs_extendfs: invalid size\n");
3206 newblkcnt
= newsize
/ vcb
->blockSize
;
3207 if (newblkcnt
> (u_int64_t
)0xFFFFFFFF)
3210 addblks
= newblkcnt
- vcb
->totalBlocks
;
3212 printf("hfs_extendfs: growing %s by %d blocks\n", vcb
->vcbVN
, addblks
);
3214 * Enclose changes inside a transaction.
3216 if (hfs_start_transaction(hfsmp
) != 0) {
3221 * Note: we take the attributes lock in case we have an attribute data vnode
3222 * which needs to change size.
3224 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3225 vp
= vcb
->allocationsRefNum
;
3227 bcopy(&fp
->ff_data
, &forkdata
, sizeof(forkdata
));
3230 * Calculate additional space required (if any) by allocation bitmap.
3232 oldBitmapSize
= fp
->ff_size
;
3233 bitmapblks
= roundup((newblkcnt
+7) / 8, vcb
->vcbVBMIOSize
) / vcb
->blockSize
;
3234 if (bitmapblks
> (daddr_t
)fp
->ff_blocks
)
3235 bitmapblks
-= fp
->ff_blocks
;
3239 if (bitmapblks
> 0) {
3245 * Get the bitmap's current size (in allocation blocks) so we know
3246 * where to start zero filling once the new space is added. We've
3247 * got to do this before the bitmap is grown.
3249 blkno
= (daddr64_t
)fp
->ff_blocks
;
3252 * Try to grow the allocation file in the normal way, using allocation
3253 * blocks already existing in the file system. This way, we might be
3254 * able to grow the bitmap contiguously, or at least in the metadata
3257 error
= ExtendFileC(vcb
, fp
, bitmapblks
* vcb
->blockSize
, 0,
3258 kEFAllMask
| kEFNoClumpMask
| kEFReserveMask
| kEFMetadataMask
,
3262 usedExtendFileC
= true;
3265 * If the above allocation failed, fall back to allocating the new
3266 * extent of the bitmap from the space we're going to add. Since those
3267 * blocks don't yet belong to the file system, we have to update the
3268 * extent list directly, and manually adjust the file size.
3271 error
= AddFileExtent(vcb
, fp
, vcb
->totalBlocks
, bitmapblks
);
3273 printf("hfs_extendfs: error %d adding extents\n", error
);
3276 fp
->ff_blocks
+= bitmapblks
;
3277 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3278 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3282 * Update the allocation file's size to include the newly allocated
3283 * blocks. Note that ExtendFileC doesn't do this, which is why this
3284 * statement is outside the above "if" statement.
3286 fp
->ff_size
+= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3289 * Zero out the new bitmap blocks.
3294 blkcnt
= bitmapblks
;
3295 while (blkcnt
> 0) {
3296 error
= (int)buf_meta_bread(vp
, blkno
, vcb
->blockSize
, NOCRED
, &bp
);
3303 bzero((char *)buf_dataptr(bp
), vcb
->blockSize
);
3305 error
= (int)buf_bwrite(bp
);
3313 printf("hfs_extendfs: error %d clearing blocks\n", error
);
3317 * Mark the new bitmap space as allocated.
3319 * Note that ExtendFileC will have marked any blocks it allocated, so
3320 * this is only needed if we used AddFileExtent. Also note that this
3321 * has to come *after* the zero filling of new blocks in the case where
3322 * we used AddFileExtent (since the part of the bitmap we're touching
3323 * is in those newly allocated blocks).
3325 if (!usedExtendFileC
) {
3326 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
, bitmapblks
);
3328 printf("hfs_extendfs: error %d setting bitmap\n", error
);
3331 vcb
->freeBlocks
-= bitmapblks
;
3335 * Mark the new alternate VH as allocated.
3337 if (vcb
->blockSize
== 512)
3338 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 2, 2);
3340 error
= BlockMarkAllocated(vcb
, vcb
->totalBlocks
+ addblks
- 1, 1);
3342 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error
);
3346 * Mark the old alternate VH as free.
3348 if (vcb
->blockSize
== 512)
3349 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 2, 2);
3351 (void) BlockMarkFree(vcb
, vcb
->totalBlocks
- 1, 1);
3353 * Adjust file system variables for new space.
3355 prev_phys_block_count
= hfsmp
->hfs_logical_block_count
;
3356 prev_alt_sector
= hfsmp
->hfs_alt_id_sector
;
3358 vcb
->totalBlocks
+= addblks
;
3359 vcb
->freeBlocks
+= addblks
;
3360 hfsmp
->hfs_logical_block_count
= newsize
/ sectorsize
;
3361 hfsmp
->hfs_alt_id_sector
= (hfsmp
->hfsPlusIOPosOffset
/ sectorsize
) +
3362 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_logical_block_count
);
3364 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3366 printf("hfs_extendfs: couldn't flush volume headers (%d)", error
);
3368 * Restore to old state.
3370 if (usedExtendFileC
) {
3371 (void) TruncateFileC(vcb
, fp
, oldBitmapSize
, false);
3373 fp
->ff_blocks
-= bitmapblks
;
3374 fp
->ff_size
-= (u_int64_t
)bitmapblks
* (u_int64_t
)vcb
->blockSize
;
3376 * No need to mark the excess blocks free since those bitmap blocks
3377 * are no longer part of the bitmap. But we do need to undo the
3378 * effect of the "vcb->freeBlocks -= bitmapblks" above.
3380 vcb
->freeBlocks
+= bitmapblks
;
3382 vcb
->totalBlocks
-= addblks
;
3383 vcb
->freeBlocks
-= addblks
;
3384 hfsmp
->hfs_logical_block_count
= prev_phys_block_count
;
3385 hfsmp
->hfs_alt_id_sector
= prev_alt_sector
;
3387 if (vcb
->blockSize
== 512)
3388 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 2, 2);
3390 (void) BlockMarkAllocated(vcb
, vcb
->totalBlocks
- 1, 1);
3394 * Invalidate the old alternate volume header.
3397 if (prev_alt_sector
) {
3398 if (buf_meta_bread(hfsmp
->hfs_devvp
,
3399 HFS_PHYSBLK_ROUNDDOWN(prev_alt_sector
, hfsmp
->hfs_log_per_phys
),
3400 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
) == 0) {
3401 journal_modify_block_start(hfsmp
->jnl
, bp
);
3403 bzero((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
), kMDBSize
);
3405 journal_modify_block_end(hfsmp
->jnl
, bp
, NULL
, NULL
);
3412 * TODO: Adjust the size of the metadata zone based on new volume size?
3416 * Adjust the size of hfsmp->hfs_attrdata_vp
3418 if (hfsmp
->hfs_attrdata_vp
) {
3419 struct cnode
*attr_cp
;
3420 struct filefork
*attr_fp
;
3422 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
3423 attr_cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
3424 attr_fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
3426 attr_cp
->c_blocks
= newblkcnt
;
3427 attr_fp
->ff_blocks
= newblkcnt
;
3428 attr_fp
->ff_extents
[0].blockCount
= newblkcnt
;
3429 attr_fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
3430 ubc_setsize(hfsmp
->hfs_attrdata_vp
, attr_fp
->ff_size
);
3431 vnode_put(hfsmp
->hfs_attrdata_vp
);
3437 /* Restore allocation fork. */
3438 bcopy(&forkdata
, &fp
->ff_data
, sizeof(forkdata
));
3439 VTOC(vp
)->c_blocks
= fp
->ff_blocks
;
3443 Regardless of whether or not the totalblocks actually increased,
3444 we should reset the allocLimit field. If it changed, it will
3445 get updated; if not, it will remain the same.
3447 hfsmp
->allocLimit
= vcb
->totalBlocks
;
3448 hfs_systemfile_unlock(hfsmp
, lockflags
);
3449 hfs_end_transaction(hfsmp
);
3454 #define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3457 * Truncate a file system (while still mounted).
3461 hfs_truncatefs(struct hfsmount
*hfsmp
, u_int64_t newsize
, vfs_context_t context
)
3463 struct buf
*bp
= NULL
;
3465 u_int32_t newblkcnt
;
3466 u_int32_t reclaimblks
= 0;
3468 int transaction_begun
= 0;
3471 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3472 if (hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) {
3473 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3476 hfsmp
->hfs_flags
|= HFS_RESIZE_IN_PROGRESS
;
3477 hfsmp
->hfs_resize_filesmoved
= 0;
3478 hfsmp
->hfs_resize_totalfiles
= 0;
3479 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3482 * - Journaled HFS Plus volumes only.
3483 * - No embedded volumes.
3485 if ((hfsmp
->jnl
== NULL
) ||
3486 (hfsmp
->hfsPlusIOPosOffset
!= 0)) {
3490 oldsize
= (u_int64_t
)hfsmp
->totalBlocks
* (u_int64_t
)hfsmp
->blockSize
;
3491 newblkcnt
= newsize
/ hfsmp
->blockSize
;
3492 reclaimblks
= hfsmp
->totalBlocks
- newblkcnt
;
3494 /* Make sure new size is valid. */
3495 if ((newsize
< HFS_MIN_SIZE
) ||
3496 (newsize
>= oldsize
) ||
3497 (newsize
% hfsmp
->hfs_logical_block_size
) ||
3498 (newsize
% hfsmp
->hfs_physical_block_size
)) {
3499 printf ("hfs_truncatefs: invalid size\n");
3503 /* Make sure there's enough space to work with. */
3504 if (reclaimblks
>= hfs_freeblks(hfsmp
, 1)) {
3505 printf("hfs_truncatefs: insufficient space (need %u blocks; have %u blocks)\n", reclaimblks
, hfs_freeblks(hfsmp
, 1));
3510 /* Start with a clean journal. */
3511 journal_flush(hfsmp
->jnl
);
3513 if (hfs_start_transaction(hfsmp
) != 0) {
3517 transaction_begun
= 1;
3520 * Prevent new allocations from using the part we're trying to truncate.
3522 * NOTE: allocLimit is set to the allocation block number where the new
3523 * alternate volume header will be. That way there will be no files to
3524 * interfere with allocating the new alternate volume header, and no files
3525 * in the allocation blocks beyond (i.e. the blocks we're trying to
3528 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3529 if (hfsmp
->blockSize
== 512)
3530 hfsmp
->allocLimit
= newblkcnt
- 2;
3532 hfsmp
->allocLimit
= newblkcnt
- 1;
3533 hfsmp
->freeBlocks
-= reclaimblks
;
3534 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3537 * Look for files that have blocks at or beyond the location of the
3538 * new alternate volume header.
3540 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
3542 * hfs_reclaimspace will use separate transactions when
3543 * relocating files (so we don't overwhelm the journal).
3545 hfs_end_transaction(hfsmp
);
3546 transaction_begun
= 0;
3548 /* Attempt to reclaim some space. */
3549 if (hfs_reclaimspace(hfsmp
, hfsmp
->allocLimit
, reclaimblks
, context
) != 0) {
3550 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp
->vcbVN
);
3554 if (hfs_start_transaction(hfsmp
) != 0) {
3558 transaction_begun
= 1;
3560 /* Check if we're clear now. */
3561 if (hfs_isallocated(hfsmp
, hfsmp
->allocLimit
, reclaimblks
)) {
3562 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp
->vcbVN
);
3563 error
= EAGAIN
; /* tell client to try again */
3569 * Note: we take the attributes lock in case we have an attribute data vnode
3570 * which needs to change size.
3572 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_ATTRIBUTE
| SFL_EXTENTS
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
3575 * Mark the old alternate volume header as free.
3576 * We don't bother shrinking allocation bitmap file.
3578 if (hfsmp
->blockSize
== 512)
3579 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 2, 2);
3581 (void) BlockMarkFree(hfsmp
, hfsmp
->totalBlocks
- 1, 1);
3584 * Allocate last 1KB for alternate volume header.
3586 error
= BlockMarkAllocated(hfsmp
, hfsmp
->allocLimit
, (hfsmp
->blockSize
== 512) ? 2 : 1);
3588 printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error
);
3593 * Invalidate the existing alternate volume header.
3595 * Don't include this in a transaction (don't call journal_modify_block)
3596 * since this block will be outside of the truncated file system!
3598 if (hfsmp
->hfs_alt_id_sector
) {
3599 if (buf_meta_bread(hfsmp
->hfs_devvp
,
3600 HFS_PHYSBLK_ROUNDDOWN(hfsmp
->hfs_alt_id_sector
, hfsmp
->hfs_log_per_phys
),
3601 hfsmp
->hfs_physical_block_size
, NOCRED
, &bp
) == 0) {
3603 bzero((void*)((char *)buf_dataptr(bp
) + HFS_ALT_OFFSET(hfsmp
->hfs_physical_block_size
)), kMDBSize
);
3604 (void) VNOP_BWRITE(bp
);
3611 /* Log successful shrinking. */
3612 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
3613 hfsmp
->vcbVN
, newblkcnt
, hfsmp
->totalBlocks
);
3616 * Adjust file system variables and flush them to disk.
3618 hfsmp
->totalBlocks
= newblkcnt
;
3619 hfsmp
->hfs_logical_block_count
= newsize
/ hfsmp
->hfs_logical_block_size
;
3620 hfsmp
->hfs_alt_id_sector
= HFS_ALT_SECTOR(hfsmp
->hfs_logical_block_size
, hfsmp
->hfs_logical_block_count
);
3621 MarkVCBDirty(hfsmp
);
3622 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3624 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error
);
3627 * TODO: Adjust the size of the metadata zone based on new volume size?
3631 * Adjust the size of hfsmp->hfs_attrdata_vp
3633 if (hfsmp
->hfs_attrdata_vp
) {
3635 struct filefork
*fp
;
3637 if (vnode_get(hfsmp
->hfs_attrdata_vp
) == 0) {
3638 cp
= VTOC(hfsmp
->hfs_attrdata_vp
);
3639 fp
= VTOF(hfsmp
->hfs_attrdata_vp
);
3641 cp
->c_blocks
= newblkcnt
;
3642 fp
->ff_blocks
= newblkcnt
;
3643 fp
->ff_extents
[0].blockCount
= newblkcnt
;
3644 fp
->ff_size
= (off_t
) newblkcnt
* hfsmp
->blockSize
;
3645 ubc_setsize(hfsmp
->hfs_attrdata_vp
, fp
->ff_size
);
3646 vnode_put(hfsmp
->hfs_attrdata_vp
);
3652 hfsmp
->freeBlocks
+= reclaimblks
;
3654 lck_mtx_lock(&hfsmp
->hfs_mutex
);
3655 hfsmp
->allocLimit
= hfsmp
->totalBlocks
;
3656 if (hfsmp
->nextAllocation
>= hfsmp
->allocLimit
)
3657 hfsmp
->nextAllocation
= hfsmp
->hfs_metazone_end
+ 1;
3658 hfsmp
->hfs_flags
&= ~HFS_RESIZE_IN_PROGRESS
;
3659 lck_mtx_unlock(&hfsmp
->hfs_mutex
);
3662 hfs_systemfile_unlock(hfsmp
, lockflags
);
3664 if (transaction_begun
) {
3665 hfs_end_transaction(hfsmp
);
3666 journal_flush(hfsmp
->jnl
);
3674 * Invalidate the physical block numbers associated with buffer cache blocks
3675 * in the given extent of the given vnode.
3677 struct hfs_inval_blk_no
{
3678 daddr64_t sectorStart
;
3679 daddr64_t sectorCount
;
3682 hfs_invalidate_block_numbers_callback(buf_t bp
, void *args_in
)
3685 struct hfs_inval_blk_no
*args
;
3687 blkno
= buf_blkno(bp
);
3690 if (blkno
>= args
->sectorStart
&& blkno
< args
->sectorStart
+args
->sectorCount
)
3691 buf_setblkno(bp
, buf_lblkno(bp
));
3693 return BUF_RETURNED
;
3696 hfs_invalidate_sectors(struct vnode
*vp
, daddr64_t sectorStart
, daddr64_t sectorCount
)
3698 struct hfs_inval_blk_no args
;
3699 args
.sectorStart
= sectorStart
;
3700 args
.sectorCount
= sectorCount
;
3702 buf_iterate(vp
, hfs_invalidate_block_numbers_callback
, BUF_SCAN_DIRTY
|BUF_SCAN_CLEAN
, &args
);
3707 * Copy the contents of an extent to a new location. Also invalidates the
3708 * physical block number of any buffer cache block in the copied extent
3709 * (so that if the block is written, it will go through VNOP_BLOCKMAP to
3710 * determine the new physical block number).
3714 struct hfsmount
*hfsmp
,
3715 struct vnode
*vp
, /* The file whose extent is being copied. */
3716 u_int32_t oldStart
, /* The start of the source extent. */
3717 u_int32_t newStart
, /* The start of the destination extent. */
3718 u_int32_t blockCount
, /* The number of allocation blocks to copy. */
3719 vfs_context_t context
)
3723 void *buffer
= NULL
;
3724 struct vfsioattr ioattr
;
3728 u_int32_t ioSizeSectors
; /* Device sectors in this I/O */
3729 daddr64_t srcSector
, destSector
;
3730 u_int32_t sectorsPerBlock
= hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
3733 * Sanity check that we have locked the vnode of the file we're copying.
3735 * But since hfs_systemfile_lock() doesn't actually take the lock on
3736 * the allocation file if a journal is active, ignore the check if the
3737 * file being copied is the allocation file.
3739 struct cnode
*cp
= VTOC(vp
);
3740 if (cp
!= hfsmp
->hfs_allocation_cp
&& cp
->c_lockowner
!= current_thread())
3741 panic("hfs_copy_extent: vp=%p (cp=%p) not owned?\n", vp
, cp
);
3744 * Wait for any in-progress writes to this vnode to complete, so that we'll
3745 * be copying consistent bits. (Otherwise, it's possible that an async
3746 * write will complete to the old extent after we read from it. That
3747 * could lead to corruption.)
3749 err
= vnode_waitforwrites(vp
, 0, 0, 0, "hfs_copy_extent");
3751 printf("hfs_copy_extent: Error %d from vnode_waitforwrites\n", err
);
3756 * Determine the I/O size to use
3758 * NOTE: Many external drives will result in an ioSize of 128KB.
3759 * TODO: Should we use a larger buffer, doing several consecutive
3760 * reads, then several consecutive writes?
3762 vfs_ioattr(hfsmp
->hfs_mp
, &ioattr
);
3763 bufferSize
= MIN(ioattr
.io_maxreadcnt
, ioattr
.io_maxwritecnt
);
3764 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &buffer
, bufferSize
))
3767 /* Get a buffer for doing the I/O */
3768 bp
= buf_alloc(hfsmp
->hfs_devvp
);
3769 buf_setdataptr(bp
, (uintptr_t)buffer
);
3771 resid
= (off_t
) blockCount
* (off_t
) hfsmp
->blockSize
;
3772 srcSector
= (daddr64_t
) oldStart
* hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
3773 destSector
= (daddr64_t
) newStart
* hfsmp
->blockSize
/ hfsmp
->hfs_logical_block_size
;
3775 ioSize
= MIN(bufferSize
, resid
);
3776 ioSizeSectors
= ioSize
/ hfsmp
->hfs_logical_block_size
;
3778 /* Prepare the buffer for reading */
3779 buf_reset(bp
, B_READ
);
3780 buf_setsize(bp
, ioSize
);
3781 buf_setcount(bp
, ioSize
);
3782 buf_setblkno(bp
, srcSector
);
3783 buf_setlblkno(bp
, srcSector
);
3786 err
= VNOP_STRATEGY(bp
);
3788 err
= buf_biowait(bp
);
3790 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err
);
3794 /* Prepare the buffer for writing */
3795 buf_reset(bp
, B_WRITE
);
3796 buf_setsize(bp
, ioSize
);
3797 buf_setcount(bp
, ioSize
);
3798 buf_setblkno(bp
, destSector
);
3799 buf_setlblkno(bp
, destSector
);
3800 if (journal_uses_fua(hfsmp
->jnl
))
3804 vnode_startwrite(hfsmp
->hfs_devvp
);
3805 err
= VNOP_STRATEGY(bp
);
3807 err
= buf_biowait(bp
);
3809 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err
);
3814 srcSector
+= ioSizeSectors
;
3815 destSector
+= ioSizeSectors
;
3820 kmem_free(kernel_map
, (vm_offset_t
)buffer
, bufferSize
);
3822 /* Make sure all writes have been flushed to disk. */
3823 if (!journal_uses_fua(hfsmp
->jnl
)) {
3824 err
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, context
);
3826 printf("hfs_copy_extent: DKIOCSYNCHRONIZECACHE failed (%d)\n", err
);
3827 err
= 0; /* Don't fail the copy. */
3832 hfs_invalidate_sectors(vp
, (daddr64_t
)oldStart
*sectorsPerBlock
, (daddr64_t
)blockCount
*sectorsPerBlock
);
3839 * Reclaim space at the end of a volume, used by a given system file.
3841 * This routine attempts to move any extent which contains allocation blocks
3842 * at or after "startblk." A separate transaction is used to do the move.
3843 * The contents of any moved extents are read and written via the volume's
3844 * device vnode -- NOT via "vp." During the move, moved blocks which are part
3845 * of a transaction have their physical block numbers invalidated so they will
3846 * eventually be written to their new locations.
3848 * This routine can be used to move overflow extents for the allocation file.
3851 * hfsmp The volume being resized.
3852 * startblk Blocks >= this allocation block need to be moved.
3853 * locks Which locks need to be taken for the given system file.
3854 * vp The vnode for the system file.
3857 * moved Set to true if any extents were moved.
3860 hfs_relocate_callback(__unused HFSPlusExtentKey
*key
, HFSPlusExtentRecord
*record
, HFSPlusExtentRecord
*state
)
3862 bcopy(state
, record
, sizeof(HFSPlusExtentRecord
));
3866 hfs_reclaim_sys_file(struct hfsmount
*hfsmp
, struct vnode
*vp
, u_long startblk
, int locks
, Boolean
*moved
, vfs_context_t context
)
3873 u_int32_t oldStartBlock
;
3874 u_int32_t newStartBlock
;
3875 u_int32_t blockCount
;
3876 struct filefork
*fp
;
3878 /* If there is no vnode for this file, then there's nothing to do. */
3882 /* printf("hfs_reclaim_sys_file: %.*s\n", VTOC(vp)->c_desc.cd_namelen, VTOC(vp)->c_desc.cd_nameptr); */
3884 /* We always need the allocation bitmap and extents B-tree */
3885 locks
|= SFL_BITMAP
| SFL_EXTENTS
;
3887 error
= hfs_start_transaction(hfsmp
);
3889 printf("hfs_reclaim_sys_file: hfs_start_transaction returned %d\n", error
);
3892 lockflags
= hfs_systemfile_lock(hfsmp
, locks
, HFS_EXCLUSIVE_LOCK
);
3896 /* Relocate non-overflow extents */
3897 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3898 if (fp
->ff_extents
[i
].blockCount
== 0)
3900 oldStartBlock
= fp
->ff_extents
[i
].startBlock
;
3901 blockCount
= fp
->ff_extents
[i
].blockCount
;
3902 datablks
+= blockCount
;
3903 block
= oldStartBlock
+ blockCount
;
3904 if (block
> startblk
) {
3905 error
= BlockAllocate(hfsmp
, 1, blockCount
, blockCount
, true, true, &newStartBlock
, &blockCount
);
3907 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error
);
3910 if (blockCount
!= fp
->ff_extents
[i
].blockCount
) {
3911 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount
, fp
->ff_extents
[i
].blockCount
);
3914 error
= hfs_copy_extent(hfsmp
, vp
, oldStartBlock
, newStartBlock
, blockCount
, context
);
3916 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error
);
3919 fp
->ff_extents
[i
].startBlock
= newStartBlock
;
3920 VTOC(vp
)->c_flag
|= C_MODIFIED
;
3922 error
= BlockDeallocate(hfsmp
, oldStartBlock
, blockCount
);
3924 /* TODO: Mark volume inconsistent? */
3925 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error
);
3928 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
3930 /* TODO: Mark volume inconsistent? */
3931 printf("hfs_reclaim_sys_file: hfs_flushvolumeheader returned %d\n", error
);
3937 /* Relocate overflow extents (if any) */
3938 if (i
== kHFSPlusExtentDensity
&& fp
->ff_blocks
> datablks
) {
3939 struct BTreeIterator
*iterator
= NULL
;
3940 struct FSBufferDescriptor btdata
;
3941 HFSPlusExtentRecord record
;
3942 HFSPlusExtentKey
*key
;
3947 forktype
= VNODE_IS_RSRC(vp
) ? 0xFF : 0;
3948 fileID
= VTOC(vp
)->c_cnid
;
3949 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &iterator
, sizeof(*iterator
))) {
3950 printf("hfs_reclaim_sys_file: kmem_alloc failed!\n");
3955 bzero(iterator
, sizeof(*iterator
));
3956 key
= (HFSPlusExtentKey
*) &iterator
->key
;
3957 key
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
3958 key
->forkType
= forktype
;
3959 key
->fileID
= fileID
;
3960 key
->startBlock
= datablks
;
3962 btdata
.bufferAddress
= &record
;
3963 btdata
.itemSize
= sizeof(record
);
3964 btdata
.itemCount
= 1;
3966 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
3968 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
3969 while (error
== 0) {
3970 /* Stop when we encounter a different file or fork. */
3971 if ((key
->fileID
!= fileID
) ||
3972 (key
->forkType
!= forktype
)) {
3976 * Check if the file overlaps target space.
3978 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
3979 if (record
[i
].blockCount
== 0) {
3982 oldStartBlock
= record
[i
].startBlock
;
3983 blockCount
= record
[i
].blockCount
;
3984 block
= oldStartBlock
+ blockCount
;
3985 if (block
> startblk
) {
3986 error
= BlockAllocate(hfsmp
, 1, blockCount
, blockCount
, true, true, &newStartBlock
, &blockCount
);
3988 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error
);
3991 if (blockCount
!= record
[i
].blockCount
) {
3992 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount
, fp
->ff_extents
[i
].blockCount
);
3993 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
3996 error
= hfs_copy_extent(hfsmp
, vp
, oldStartBlock
, newStartBlock
, blockCount
, context
);
3998 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error
);
3999 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4002 record
[i
].startBlock
= newStartBlock
;
4003 VTOC(vp
)->c_flag
|= C_MODIFIED
;
4006 * NOTE: To support relocating overflow extents of the
4007 * allocation file, we must update the BTree record BEFORE
4008 * deallocating the old extent so that BlockDeallocate will
4009 * use the extent's new location to calculate physical block
4010 * numbers. (This is for the case where the old extent's
4011 * bitmap bits actually reside in the extent being moved.)
4013 error
= BTUpdateRecord(fcb
, iterator
, (IterateCallBackProcPtr
) hfs_relocate_callback
, &record
);
4015 /* TODO: Mark volume inconsistent? */
4016 printf("hfs_reclaim_sys_file: BTUpdateRecord returned %d\n", error
);
4019 error
= BlockDeallocate(hfsmp
, oldStartBlock
, blockCount
);
4021 /* TODO: Mark volume inconsistent? */
4022 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error
);
4027 /* Look for more records. */
4028 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4029 if (error
== btNotFound
) {
4035 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4041 hfs_systemfile_unlock(hfsmp
, lockflags
);
4042 error
= hfs_end_transaction(hfsmp
);
4044 printf("hfs_reclaim_sys_file: hfs_end_transaction returned %d\n", error
);
4050 (void) BlockDeallocate(hfsmp
, newStartBlock
, blockCount
);
4052 (void) hfs_systemfile_unlock(hfsmp
, lockflags
);
4053 (void) hfs_end_transaction(hfsmp
);
4059 * This journal_relocate callback updates the journal info block to point
4060 * at the new journal location. This write must NOT be done using the
4061 * transaction. We must write the block immediately. We must also force
4062 * it to get to the media so that the new journal location will be seen by
4063 * the replay code before we can safely let journaled blocks be written
4064 * to their normal locations.
4066 * The tests for journal_uses_fua below are mildly hacky. Since the journal
4067 * and the file system are both on the same device, I'm leveraging what
4068 * the journal has decided about FUA.
4070 struct hfs_journal_relocate_args
{
4071 struct hfsmount
*hfsmp
;
4072 vfs_context_t context
;
4073 u_int32_t newStartBlock
;
4077 hfs_journal_relocate_callback(void *_args
)
4080 struct hfs_journal_relocate_args
*args
= _args
;
4081 struct hfsmount
*hfsmp
= args
->hfsmp
;
4083 JournalInfoBlock
*jibp
;
4085 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
4086 hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
4087 hfsmp
->blockSize
, vfs_context_ucred(args
->context
), &bp
);
4089 printf("hfs_reclaim_journal_file: failed to read JIB (%d)\n", error
);
4092 jibp
= (JournalInfoBlock
*) buf_dataptr(bp
);
4093 jibp
->offset
= SWAP_BE64((u_int64_t
)args
->newStartBlock
* hfsmp
->blockSize
);
4094 jibp
->size
= SWAP_BE64(hfsmp
->jnl_size
);
4095 if (journal_uses_fua(hfsmp
->jnl
))
4097 error
= buf_bwrite(bp
);
4099 printf("hfs_reclaim_journal_file: failed to write JIB (%d)\n", error
);
4102 if (!journal_uses_fua(hfsmp
->jnl
)) {
4103 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, args
->context
);
4105 printf("hfs_reclaim_journal_file: DKIOCSYNCHRONIZECACHE failed (%d)\n", error
);
4106 error
= 0; /* Don't fail the operation. */
4115 hfs_reclaim_journal_file(struct hfsmount
*hfsmp
, vfs_context_t context
)
4119 u_int32_t newStartBlock
;
4120 u_int32_t oldBlockCount
;
4121 u_int32_t newBlockCount
;
4122 struct cat_desc journal_desc
;
4123 struct cat_attr journal_attr
;
4124 struct cat_fork journal_fork
;
4125 struct hfs_journal_relocate_args callback_args
;
4127 error
= hfs_start_transaction(hfsmp
);
4129 printf("hfs_reclaim_journal_file: hfs_start_transaction returned %d\n", error
);
4132 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4134 oldBlockCount
= hfsmp
->jnl_size
/ hfsmp
->blockSize
;
4136 /* TODO: Allow the journal to change size based on the new volume size. */
4137 error
= BlockAllocate(hfsmp
, 1, oldBlockCount
, oldBlockCount
, true, true, &newStartBlock
, &newBlockCount
);
4139 printf("hfs_reclaim_journal_file: BlockAllocate returned %d\n", error
);
4142 if (newBlockCount
!= oldBlockCount
) {
4143 printf("hfs_reclaim_journal_file: newBlockCount != oldBlockCount (%u, %u)\n", newBlockCount
, oldBlockCount
);
4147 error
= BlockDeallocate(hfsmp
, hfsmp
->jnl_start
, oldBlockCount
);
4149 printf("hfs_reclaim_journal_file: BlockDeallocate returned %d\n", error
);
4153 /* Update the catalog record for .journal */
4154 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlfileid
, 1, &journal_desc
, &journal_attr
, &journal_fork
);
4156 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error
);
4159 journal_fork
.cf_size
= newBlockCount
* hfsmp
->blockSize
;
4160 journal_fork
.cf_extents
[0].startBlock
= newStartBlock
;
4161 journal_fork
.cf_extents
[0].blockCount
= newBlockCount
;
4162 journal_fork
.cf_blocks
= newBlockCount
;
4163 error
= cat_update(hfsmp
, &journal_desc
, &journal_attr
, &journal_fork
, NULL
);
4164 cat_releasedesc(&journal_desc
); /* all done with cat descriptor */
4166 printf("hfs_reclaim_journal_file: cat_update returned %d\n", error
);
4169 callback_args
.hfsmp
= hfsmp
;
4170 callback_args
.context
= context
;
4171 callback_args
.newStartBlock
= newStartBlock
;
4173 error
= journal_relocate(hfsmp
->jnl
, (off_t
)newStartBlock
*hfsmp
->blockSize
,
4174 (off_t
)newBlockCount
*hfsmp
->blockSize
, 0,
4175 hfs_journal_relocate_callback
, &callback_args
);
4177 /* NOTE: journal_relocate will mark the journal invalid. */
4178 printf("hfs_reclaim_journal_file: journal_relocate returned %d\n", error
);
4181 hfsmp
->jnl_start
= newStartBlock
;
4182 hfsmp
->jnl_size
= (off_t
)newBlockCount
* hfsmp
->blockSize
;
4184 hfs_systemfile_unlock(hfsmp
, lockflags
);
4185 error
= hfs_end_transaction(hfsmp
);
4187 printf("hfs_reclaim_journal_file: hfs_end_transaction returned %d\n", error
);
4193 (void) BlockDeallocate(hfsmp
, newStartBlock
, newBlockCount
);
4195 hfs_systemfile_unlock(hfsmp
, lockflags
);
4196 (void) hfs_end_transaction(hfsmp
);
4202 * Move the journal info block to a new location. We have to make sure the
4203 * new copy of the journal info block gets to the media first, then change
4204 * the field in the volume header and the catalog record.
4207 hfs_reclaim_journal_info_block(struct hfsmount
*hfsmp
, vfs_context_t context
)
4212 u_int32_t blockCount
;
4213 struct cat_desc jib_desc
;
4214 struct cat_attr jib_attr
;
4215 struct cat_fork jib_fork
;
4216 buf_t old_bp
, new_bp
;
4218 error
= hfs_start_transaction(hfsmp
);
4220 printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error
);
4223 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_BITMAP
, HFS_EXCLUSIVE_LOCK
);
4225 error
= BlockAllocate(hfsmp
, 1, 1, 1, true, true, &newBlock
, &blockCount
);
4227 printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error
);
4230 if (blockCount
!= 1) {
4231 printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount
);
4234 error
= BlockDeallocate(hfsmp
, hfsmp
->vcbJinfoBlock
, 1);
4236 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error
);
4240 /* Copy the old journal info block content to the new location */
4241 error
= buf_meta_bread(hfsmp
->hfs_devvp
,
4242 hfsmp
->vcbJinfoBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
4243 hfsmp
->blockSize
, vfs_context_ucred(context
), &old_bp
);
4245 printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error
);
4248 new_bp
= buf_getblk(hfsmp
->hfs_devvp
,
4249 newBlock
* (hfsmp
->blockSize
/hfsmp
->hfs_logical_block_size
),
4250 hfsmp
->blockSize
, 0, 0, BLK_META
);
4251 bcopy((char*)buf_dataptr(old_bp
), (char*)buf_dataptr(new_bp
), hfsmp
->blockSize
);
4253 if (journal_uses_fua(hfsmp
->jnl
))
4254 buf_markfua(new_bp
);
4255 error
= buf_bwrite(new_bp
);
4257 printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error
);
4260 if (!journal_uses_fua(hfsmp
->jnl
)) {
4261 error
= VNOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, context
);
4263 printf("hfs_reclaim_journal_info_block: DKIOCSYNCHRONIZECACHE failed (%d)\n", error
);
4264 /* Don't fail the operation. */
4268 /* Update the catalog record for .journal_info_block */
4269 error
= cat_idlookup(hfsmp
, hfsmp
->hfs_jnlinfoblkid
, 1, &jib_desc
, &jib_attr
, &jib_fork
);
4271 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error
);
4274 jib_fork
.cf_size
= hfsmp
->blockSize
;
4275 jib_fork
.cf_extents
[0].startBlock
= newBlock
;
4276 jib_fork
.cf_extents
[0].blockCount
= 1;
4277 jib_fork
.cf_blocks
= 1;
4278 error
= cat_update(hfsmp
, &jib_desc
, &jib_attr
, &jib_fork
, NULL
);
4279 cat_releasedesc(&jib_desc
); /* all done with cat descriptor */
4281 printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error
);
4285 /* Update the pointer to the journal info block in the volume header. */
4286 hfsmp
->vcbJinfoBlock
= newBlock
;
4287 error
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, HFS_ALTFLUSH
);
4289 printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error
);
4292 hfs_systemfile_unlock(hfsmp
, lockflags
);
4293 error
= hfs_end_transaction(hfsmp
);
4295 printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error
);
4297 error
= journal_flush(hfsmp
->jnl
);
4299 printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error
);
4304 (void) BlockDeallocate(hfsmp
, newBlock
, blockCount
);
4306 hfs_systemfile_unlock(hfsmp
, lockflags
);
4307 (void) hfs_end_transaction(hfsmp
);
4313 * Reclaim space at the end of a file system.
4316 hfs_reclaimspace(struct hfsmount
*hfsmp
, u_long startblk
, u_long reclaimblks
, vfs_context_t context
)
4318 struct vnode
*vp
= NULL
;
4320 struct BTreeIterator
* iterator
= NULL
;
4321 struct FSBufferDescriptor btdata
;
4322 struct HFSPlusCatalogFile filerec
;
4323 u_int32_t saved_next_allocation
;
4331 u_long blkstomove
= 0;
4335 int lastprogress
= 0;
4336 Boolean system_file_moved
= false;
4338 /* Relocate extents of the Allocation file if they're in the way. */
4339 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_allocation_vp
, startblk
, SFL_BITMAP
, &system_file_moved
, context
);
4341 printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error
);
4344 /* Relocate extents of the Extents B-tree if they're in the way. */
4345 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_extents_vp
, startblk
, SFL_EXTENTS
, &system_file_moved
, context
);
4347 printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error
);
4350 /* Relocate extents of the Catalog B-tree if they're in the way. */
4351 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_catalog_vp
, startblk
, SFL_CATALOG
, &system_file_moved
, context
);
4353 printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error
);
4356 /* Relocate extents of the Attributes B-tree if they're in the way. */
4357 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_attribute_vp
, startblk
, SFL_ATTRIBUTE
, &system_file_moved
, context
);
4359 printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error
);
4362 /* Relocate extents of the Startup File if there is one and they're in the way. */
4363 error
= hfs_reclaim_sys_file(hfsmp
, hfsmp
->hfs_startup_vp
, startblk
, SFL_STARTUP
, &system_file_moved
, context
);
4365 printf("hfs_reclaimspace: reclaim startup file returned %d\n", error
);
4370 * We need to make sure the alternate volume header gets flushed if we moved
4371 * any extents in the volume header. But we need to do that before
4372 * shrinking the size of the volume, or else the journal code will panic
4373 * with an invalid (too large) block number.
4375 * Note that system_file_moved will be set if ANY extent was moved, even
4376 * if it was just an overflow extent. In this case, the journal_flush isn't
4377 * strictly required, but shouldn't hurt.
4379 if (system_file_moved
)
4380 journal_flush(hfsmp
->jnl
);
4382 if (hfsmp
->jnl_start
+ (hfsmp
->jnl_size
/ hfsmp
->blockSize
) > startblk
) {
4383 error
= hfs_reclaim_journal_file(hfsmp
, context
);
4385 printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error
);
4390 if (hfsmp
->vcbJinfoBlock
>= startblk
) {
4391 error
= hfs_reclaim_journal_info_block(hfsmp
, context
);
4393 printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error
);
4398 /* For now move a maximum of 250,000 files. */
4399 maxfilecnt
= MIN(hfsmp
->hfs_filecount
, 250000);
4400 maxfilecnt
= MIN((u_long
)maxfilecnt
, reclaimblks
);
4401 cnidbufsize
= maxfilecnt
* sizeof(cnid_t
);
4402 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&cnidbufp
, cnidbufsize
)) {
4405 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
4406 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
4410 saved_next_allocation
= hfsmp
->nextAllocation
;
4411 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, hfsmp
->hfs_metazone_start
);
4413 fcb
= VTOF(hfsmp
->hfs_catalog_vp
);
4414 bzero(iterator
, sizeof(*iterator
));
4416 btdata
.bufferAddress
= &filerec
;
4417 btdata
.itemSize
= sizeof(filerec
);
4418 btdata
.itemCount
= 1;
4420 /* Keep the Catalog and extents files locked during iteration. */
4421 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
| SFL_EXTENTS
, HFS_SHARED_LOCK
);
4423 error
= BTIterateRecord(fcb
, kBTreeFirstRecord
, iterator
, NULL
, NULL
);
4428 * Iterate over all the catalog records looking for files
4429 * that overlap into the space we're trying to free up.
4431 for (filecnt
= 0; filecnt
< maxfilecnt
; ) {
4432 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4434 if (error
== fsBTRecordNotFoundErr
|| error
== fsBTEndOfIterationErr
) {
4439 if (filerec
.recordType
!= kHFSPlusFileRecord
) {
4442 datablks
= rsrcblks
= 0;
4444 * Check if either fork overlaps target space.
4446 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
4447 if (filerec
.dataFork
.extents
[i
].blockCount
!= 0) {
4448 datablks
+= filerec
.dataFork
.extents
[i
].blockCount
;
4449 block
= filerec
.dataFork
.extents
[i
].startBlock
+
4450 filerec
.dataFork
.extents
[i
].blockCount
;
4451 if (block
>= startblk
) {
4452 if ((filerec
.fileID
== hfsmp
->hfs_jnlfileid
) ||
4453 (filerec
.fileID
== hfsmp
->hfs_jnlinfoblkid
)) {
4454 printf("hfs_reclaimspace: cannot move active journal\n");
4458 cnidbufp
[filecnt
++] = filerec
.fileID
;
4459 blkstomove
+= filerec
.dataFork
.totalBlocks
;
4463 if (filerec
.resourceFork
.extents
[i
].blockCount
!= 0) {
4464 rsrcblks
+= filerec
.resourceFork
.extents
[i
].blockCount
;
4465 block
= filerec
.resourceFork
.extents
[i
].startBlock
+
4466 filerec
.resourceFork
.extents
[i
].blockCount
;
4467 if (block
>= startblk
) {
4468 cnidbufp
[filecnt
++] = filerec
.fileID
;
4469 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
4475 * Check for any overflow extents that overlap.
4477 if (i
== kHFSPlusExtentDensity
) {
4478 if (filerec
.dataFork
.totalBlocks
> datablks
) {
4479 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, datablks
, filerec
.fileID
, 0)) {
4480 cnidbufp
[filecnt
++] = filerec
.fileID
;
4481 blkstomove
+= filerec
.dataFork
.totalBlocks
;
4483 } else if (filerec
.resourceFork
.totalBlocks
> rsrcblks
) {
4484 if (hfs_overlapped_overflow_extents(hfsmp
, startblk
, rsrcblks
, filerec
.fileID
, 1)) {
4485 cnidbufp
[filecnt
++] = filerec
.fileID
;
4486 blkstomove
+= filerec
.resourceFork
.totalBlocks
;
4493 if (filecnt
== 0 && !system_file_moved
) {
4494 printf("hfs_reclaimspace: no files moved\n");
4497 /* All done with catalog. */
4498 hfs_systemfile_unlock(hfsmp
, lockflags
);
4499 if (error
|| filecnt
== 0)
4503 * Double check space requirements to make sure
4504 * there is enough space to relocate any files
4505 * that reside in the reclaim area.
4507 * Blocks To Move --------------
4510 * ------------------------------------------------------------------------
4514 * ------------------------------------------------------------------------
4516 * <------------------- New Total Blocks ------------------><-- Reclaim -->
4518 * <------------------------ Original Total Blocks ----------------------->
4521 if (blkstomove
>= hfs_freeblks(hfsmp
, 1)) {
4522 printf("hfs_truncatefs: insufficient space (need %lu blocks; have %u blocks)\n", blkstomove
, hfs_freeblks(hfsmp
, 1));
4526 hfsmp
->hfs_resize_filesmoved
= 0;
4527 hfsmp
->hfs_resize_totalfiles
= filecnt
;
4529 /* Now move any files that are in the way. */
4530 for (i
= 0; i
< filecnt
; ++i
) {
4534 if (hfs_vget(hfsmp
, cnidbufp
[i
], &vp
, 0) != 0)
4537 /* Relocating directory hard links is not supported, so we
4538 * punt (see radar 6217026). */
4540 if ((cp
->c_flag
& C_HARDLINK
) && vnode_isdir(vp
)) {
4541 printf("hfs_reclaimspace: unable to relocate directory hard link %d\n", cp
->c_cnid
);
4546 /* Relocate any data fork blocks. */
4547 if (VTOF(vp
) && VTOF(vp
)->ff_blocks
> 0) {
4548 error
= hfs_relocate(vp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
4553 /* Relocate any resource fork blocks. */
4554 if ((cp
->c_blocks
- (VTOF(vp
) ? VTOF((vp
))->ff_blocks
: 0)) > 0) {
4555 error
= hfs_vgetrsrc(hfsmp
, vp
, &rvp
, TRUE
);
4558 error
= hfs_relocate(rvp
, hfsmp
->hfs_metazone_end
+ 1, kauth_cred_get(), current_proc());
4559 VTOC(rvp
)->c_flag
|= C_NEED_RVNODE_PUT
;
4567 ++hfsmp
->hfs_resize_filesmoved
;
4569 /* Report intermediate progress. */
4570 if (filecnt
> 100) {
4573 progress
= (i
* 100) / filecnt
;
4574 if (progress
> (lastprogress
+ 9)) {
4575 printf("hfs_reclaimspace: %d%% done...\n", progress
);
4576 lastprogress
= progress
;
4581 hfs_unlock(VTOC(vp
));
4585 if (hfsmp
->hfs_resize_filesmoved
!= 0) {
4586 printf("hfs_reclaimspace: relocated %d files on \"%s\"\n",
4587 (int)hfsmp
->hfs_resize_filesmoved
, hfsmp
->vcbVN
);
4590 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4591 kmem_free(kernel_map
, (vm_offset_t
)cnidbufp
, cnidbufsize
);
4594 * Restore the roving allocation pointer on errors.
4595 * (but only if we didn't move any files)
4597 if (error
&& hfsmp
->hfs_resize_filesmoved
== 0) {
4598 HFS_UPDATE_NEXT_ALLOCATION(hfsmp
, saved_next_allocation
);
4605 * Check if there are any overflow extents that overlap.
4608 hfs_overlapped_overflow_extents(struct hfsmount
*hfsmp
, u_int32_t startblk
, u_int32_t catblks
, u_int32_t fileID
, int rsrcfork
)
4610 struct BTreeIterator
* iterator
= NULL
;
4611 struct FSBufferDescriptor btdata
;
4612 HFSPlusExtentRecord extrec
;
4613 HFSPlusExtentKey
*extkeyptr
;
4621 forktype
= rsrcfork
? 0xFF : 0;
4622 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&iterator
, sizeof(*iterator
))) {
4625 bzero(iterator
, sizeof(*iterator
));
4626 extkeyptr
= (HFSPlusExtentKey
*)&iterator
->key
;
4627 extkeyptr
->keyLength
= kHFSPlusExtentKeyMaximumLength
;
4628 extkeyptr
->forkType
= forktype
;
4629 extkeyptr
->fileID
= fileID
;
4630 extkeyptr
->startBlock
= catblks
;
4632 btdata
.bufferAddress
= &extrec
;
4633 btdata
.itemSize
= sizeof(extrec
);
4634 btdata
.itemCount
= 1;
4636 fcb
= VTOF(hfsmp
->hfs_extents_vp
);
4638 error
= BTSearchRecord(fcb
, iterator
, &btdata
, NULL
, iterator
);
4639 while (error
== 0) {
4640 /* Stop when we encounter a different file. */
4641 if ((extkeyptr
->fileID
!= fileID
) ||
4642 (extkeyptr
->forkType
!= forktype
)) {
4646 * Check if the file overlaps target space.
4648 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
4649 if (extrec
[i
].blockCount
== 0) {
4652 block
= extrec
[i
].startBlock
+ extrec
[i
].blockCount
;
4653 if (block
>= startblk
) {
4658 /* Look for more records. */
4659 error
= BTIterateRecord(fcb
, kBTreeNextRecord
, iterator
, &btdata
, NULL
);
4662 kmem_free(kernel_map
, (vm_offset_t
)iterator
, sizeof(*iterator
));
4663 return (overlapped
);
4668 * Calculate the progress of a file system resize operation.
4672 hfs_resize_progress(struct hfsmount
*hfsmp
, u_int32_t
*progress
)
4674 if ((hfsmp
->hfs_flags
& HFS_RESIZE_IN_PROGRESS
) == 0) {
4678 if (hfsmp
->hfs_resize_totalfiles
> 0)
4679 *progress
= (hfsmp
->hfs_resize_filesmoved
* 100) / hfsmp
->hfs_resize_totalfiles
;
4688 * Get file system attributes.
4691 hfs_vfs_getattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
4693 #define HFS_ATTR_CMN_VALIDMASK (ATTR_CMN_VALIDMASK & ~(ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST))
4694 #define HFS_ATTR_FILE_VALIDMASK (ATTR_FILE_VALIDMASK & ~(ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST))
4696 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
4697 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
4700 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)hfsmp
->vcbNxtCNID
;
4702 VFSATTR_RETURN(fsap
, f_objcount
, (u_int64_t
)hfsmp
->vcbFilCnt
+ (u_int64_t
)hfsmp
->vcbDirCnt
);
4703 VFSATTR_RETURN(fsap
, f_filecount
, (u_int64_t
)hfsmp
->vcbFilCnt
);
4704 VFSATTR_RETURN(fsap
, f_dircount
, (u_int64_t
)hfsmp
->vcbDirCnt
);
4705 VFSATTR_RETURN(fsap
, f_maxobjcount
, (u_int64_t
)0xFFFFFFFF);
4706 VFSATTR_RETURN(fsap
, f_iosize
, (size_t)cluster_max_io_size(mp
, 0));
4707 VFSATTR_RETURN(fsap
, f_blocks
, (u_int64_t
)hfsmp
->totalBlocks
);
4708 VFSATTR_RETURN(fsap
, f_bfree
, (u_int64_t
)hfs_freeblks(hfsmp
, 0));
4709 VFSATTR_RETURN(fsap
, f_bavail
, (u_int64_t
)hfs_freeblks(hfsmp
, 1));
4710 VFSATTR_RETURN(fsap
, f_bsize
, (u_int32_t
)vcb
->blockSize
);
4711 /* XXX needs clarification */
4712 VFSATTR_RETURN(fsap
, f_bused
, hfsmp
->totalBlocks
- hfs_freeblks(hfsmp
, 1));
4713 /* Maximum files is constrained by total blocks. */
4714 VFSATTR_RETURN(fsap
, f_files
, (u_int64_t
)(hfsmp
->totalBlocks
- 2));
4715 VFSATTR_RETURN(fsap
, f_ffree
, MIN((u_int64_t
)freeCNIDs
, (u_int64_t
)hfs_freeblks(hfsmp
, 1)));
4717 fsap
->f_fsid
.val
[0] = hfsmp
->hfs_raw_dev
;
4718 fsap
->f_fsid
.val
[1] = vfs_typenum(mp
);
4719 VFSATTR_SET_SUPPORTED(fsap
, f_fsid
);
4721 VFSATTR_RETURN(fsap
, f_signature
, vcb
->vcbSigWord
);
4722 VFSATTR_RETURN(fsap
, f_carbon_fsid
, 0);
4724 if (VFSATTR_IS_ACTIVE(fsap
, f_capabilities
)) {
4725 vol_capabilities_attr_t
*cap
;
4727 cap
= &fsap
->f_capabilities
;
4729 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4730 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
4731 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4732 VOL_CAP_FMT_CASE_PRESERVING
|
4733 VOL_CAP_FMT_FAST_STATFS
|
4734 VOL_CAP_FMT_HIDDEN_FILES
|
4735 VOL_CAP_FMT_PATH_FROM_ID
;
4737 cap
->capabilities
[VOL_CAPABILITIES_FORMAT
] =
4738 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4739 VOL_CAP_FMT_SYMBOLICLINKS
|
4740 VOL_CAP_FMT_HARDLINKS
|
4741 VOL_CAP_FMT_JOURNAL
|
4742 VOL_CAP_FMT_ZERO_RUNS
|
4743 (hfsmp
->jnl
? VOL_CAP_FMT_JOURNAL_ACTIVE
: 0) |
4744 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
? VOL_CAP_FMT_CASE_SENSITIVE
: 0) |
4745 VOL_CAP_FMT_CASE_PRESERVING
|
4746 VOL_CAP_FMT_FAST_STATFS
|
4747 VOL_CAP_FMT_2TB_FILESIZE
|
4748 VOL_CAP_FMT_HIDDEN_FILES
|
4749 VOL_CAP_FMT_PATH_FROM_ID
;
4751 cap
->capabilities
[VOL_CAPABILITIES_INTERFACES
] =
4752 VOL_CAP_INT_SEARCHFS
|
4753 VOL_CAP_INT_ATTRLIST
|
4754 VOL_CAP_INT_NFSEXPORT
|
4755 VOL_CAP_INT_READDIRATTR
|
4756 VOL_CAP_INT_EXCHANGEDATA
|
4757 VOL_CAP_INT_ALLOCATE
|
4758 VOL_CAP_INT_VOL_RENAME
|
4759 VOL_CAP_INT_ADVLOCK
|
4762 VOL_CAP_INT_EXTENDED_ATTR
|
4763 VOL_CAP_INT_NAMEDSTREAMS
;
4765 VOL_CAP_INT_EXTENDED_ATTR
;
4767 cap
->capabilities
[VOL_CAPABILITIES_RESERVED1
] = 0;
4768 cap
->capabilities
[VOL_CAPABILITIES_RESERVED2
] = 0;
4770 cap
->valid
[VOL_CAPABILITIES_FORMAT
] =
4771 VOL_CAP_FMT_PERSISTENTOBJECTIDS
|
4772 VOL_CAP_FMT_SYMBOLICLINKS
|
4773 VOL_CAP_FMT_HARDLINKS
|
4774 VOL_CAP_FMT_JOURNAL
|
4775 VOL_CAP_FMT_JOURNAL_ACTIVE
|
4776 VOL_CAP_FMT_NO_ROOT_TIMES
|
4777 VOL_CAP_FMT_SPARSE_FILES
|
4778 VOL_CAP_FMT_ZERO_RUNS
|
4779 VOL_CAP_FMT_CASE_SENSITIVE
|
4780 VOL_CAP_FMT_CASE_PRESERVING
|
4781 VOL_CAP_FMT_FAST_STATFS
|
4782 VOL_CAP_FMT_2TB_FILESIZE
|
4783 VOL_CAP_FMT_OPENDENYMODES
|
4784 VOL_CAP_FMT_HIDDEN_FILES
|
4785 VOL_CAP_FMT_PATH_FROM_ID
;
4786 cap
->valid
[VOL_CAPABILITIES_INTERFACES
] =
4787 VOL_CAP_INT_SEARCHFS
|
4788 VOL_CAP_INT_ATTRLIST
|
4789 VOL_CAP_INT_NFSEXPORT
|
4790 VOL_CAP_INT_READDIRATTR
|
4791 VOL_CAP_INT_EXCHANGEDATA
|
4792 VOL_CAP_INT_COPYFILE
|
4793 VOL_CAP_INT_ALLOCATE
|
4794 VOL_CAP_INT_VOL_RENAME
|
4795 VOL_CAP_INT_ADVLOCK
|
4797 VOL_CAP_INT_MANLOCK
|
4799 VOL_CAP_INT_EXTENDED_ATTR
|
4800 VOL_CAP_INT_NAMEDSTREAMS
;
4802 VOL_CAP_INT_EXTENDED_ATTR
;
4804 cap
->valid
[VOL_CAPABILITIES_RESERVED1
] = 0;
4805 cap
->valid
[VOL_CAPABILITIES_RESERVED2
] = 0;
4806 VFSATTR_SET_SUPPORTED(fsap
, f_capabilities
);
4808 if (VFSATTR_IS_ACTIVE(fsap
, f_attributes
)) {
4809 vol_attributes_attr_t
*attrp
= &fsap
->f_attributes
;
4811 attrp
->validattr
.commonattr
= HFS_ATTR_CMN_VALIDMASK
;
4812 attrp
->validattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
4813 attrp
->validattr
.dirattr
= ATTR_DIR_VALIDMASK
;
4814 attrp
->validattr
.fileattr
= HFS_ATTR_FILE_VALIDMASK
;
4815 attrp
->validattr
.forkattr
= 0;
4817 attrp
->nativeattr
.commonattr
= HFS_ATTR_CMN_VALIDMASK
;
4818 attrp
->nativeattr
.volattr
= ATTR_VOL_VALIDMASK
& ~ATTR_VOL_INFO
;
4819 attrp
->nativeattr
.dirattr
= ATTR_DIR_VALIDMASK
;
4820 attrp
->nativeattr
.fileattr
= HFS_ATTR_FILE_VALIDMASK
;
4821 attrp
->nativeattr
.forkattr
= 0;
4822 VFSATTR_SET_SUPPORTED(fsap
, f_attributes
);
4824 fsap
->f_create_time
.tv_sec
= hfsmp
->vcbCrDate
;
4825 fsap
->f_create_time
.tv_nsec
= 0;
4826 VFSATTR_SET_SUPPORTED(fsap
, f_create_time
);
4827 fsap
->f_modify_time
.tv_sec
= hfsmp
->vcbLsMod
;
4828 fsap
->f_modify_time
.tv_nsec
= 0;
4829 VFSATTR_SET_SUPPORTED(fsap
, f_modify_time
);
4831 fsap
->f_backup_time
.tv_sec
= hfsmp
->vcbVolBkUp
;
4832 fsap
->f_backup_time
.tv_nsec
= 0;
4833 VFSATTR_SET_SUPPORTED(fsap
, f_backup_time
);
4834 if (VFSATTR_IS_ACTIVE(fsap
, f_fssubtype
)) {
4835 u_int16_t subtype
= 0;
4838 * Subtypes (flavors) for HFS
4839 * 0: Mac OS Extended
4840 * 1: Mac OS Extended (Journaled)
4841 * 2: Mac OS Extended (Case Sensitive)
4842 * 3: Mac OS Extended (Case Sensitive, Journaled)
4844 * 128: Mac OS Standard
4847 if (hfsmp
->hfs_flags
& HFS_STANDARD
) {
4848 subtype
= HFS_SUBTYPE_STANDARDHFS
;
4849 } else /* HFS Plus */ {
4851 subtype
|= HFS_SUBTYPE_JOURNALED
;
4852 if (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
)
4853 subtype
|= HFS_SUBTYPE_CASESENSITIVE
;
4855 fsap
->f_fssubtype
= subtype
;
4856 VFSATTR_SET_SUPPORTED(fsap
, f_fssubtype
);
4859 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
4860 strlcpy(fsap
->f_vol_name
, (char *) hfsmp
->vcbVN
, MAXPATHLEN
);
4861 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
4867 * Perform a volume rename. Requires the FS' root vp.
4870 hfs_rename_volume(struct vnode
*vp
, const char *name
, proc_t p
)
4872 ExtendedVCB
*vcb
= VTOVCB(vp
);
4873 struct cnode
*cp
= VTOC(vp
);
4874 struct hfsmount
*hfsmp
= VTOHFS(vp
);
4875 struct cat_desc to_desc
;
4876 struct cat_desc todir_desc
;
4877 struct cat_desc new_desc
;
4878 cat_cookie_t cookie
;
4883 * Ignore attempts to rename a volume to a zero-length name.
4888 bzero(&to_desc
, sizeof(to_desc
));
4889 bzero(&todir_desc
, sizeof(todir_desc
));
4890 bzero(&new_desc
, sizeof(new_desc
));
4891 bzero(&cookie
, sizeof(cookie
));
4893 todir_desc
.cd_parentcnid
= kHFSRootParentID
;
4894 todir_desc
.cd_cnid
= kHFSRootFolderID
;
4895 todir_desc
.cd_flags
= CD_ISDIR
;
4897 to_desc
.cd_nameptr
= (const u_int8_t
*)name
;
4898 to_desc
.cd_namelen
= strlen(name
);
4899 to_desc
.cd_parentcnid
= kHFSRootParentID
;
4900 to_desc
.cd_cnid
= cp
->c_cnid
;
4901 to_desc
.cd_flags
= CD_ISDIR
;
4903 if ((error
= hfs_lock(cp
, HFS_EXCLUSIVE_LOCK
)) == 0) {
4904 if ((error
= hfs_start_transaction(hfsmp
)) == 0) {
4905 if ((error
= cat_preflight(hfsmp
, CAT_RENAME
, &cookie
, p
)) == 0) {
4906 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_EXCLUSIVE_LOCK
);
4908 error
= cat_rename(hfsmp
, &cp
->c_desc
, &todir_desc
, &to_desc
, &new_desc
);
4911 * If successful, update the name in the VCB, ensure it's terminated.
4914 strlcpy((char *)vcb
->vcbVN
, name
, sizeof(vcb
->vcbVN
));
4917 hfs_systemfile_unlock(hfsmp
, lockflags
);
4918 cat_postflight(hfsmp
, &cookie
, p
);
4922 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
4924 hfs_end_transaction(hfsmp
);
4927 /* Release old allocated name buffer */
4928 if (cp
->c_desc
.cd_flags
& CD_HASBUF
) {
4929 const char *tmp_name
= (const char *)cp
->c_desc
.cd_nameptr
;
4931 cp
->c_desc
.cd_nameptr
= 0;
4932 cp
->c_desc
.cd_namelen
= 0;
4933 cp
->c_desc
.cd_flags
&= ~CD_HASBUF
;
4934 vfs_removename(tmp_name
);
4936 /* Update cnode's catalog descriptor */
4937 replace_desc(cp
, &new_desc
);
4938 vcb
->volumeNameEncodingHint
= new_desc
.cd_encoding
;
4939 cp
->c_touch_chgtime
= TRUE
;
4949 * Get file system attributes.
4952 hfs_vfs_setattr(struct mount
*mp
, struct vfs_attr
*fsap
, __unused vfs_context_t context
)
4954 kauth_cred_t cred
= vfs_context_ucred(context
);
4958 * Must be superuser or owner of filesystem to change volume attributes
4960 if (!kauth_cred_issuser(cred
) && (kauth_cred_getuid(cred
) != vfs_statfs(mp
)->f_owner
))
4963 if (VFSATTR_IS_ACTIVE(fsap
, f_vol_name
)) {
4966 error
= hfs_vfs_root(mp
, &root_vp
, context
);
4970 error
= hfs_rename_volume(root_vp
, fsap
->f_vol_name
, vfs_context_proc(context
));
4971 (void) vnode_put(root_vp
);
4975 VFSATTR_SET_SUPPORTED(fsap
, f_vol_name
);
4982 /* If a runtime corruption is detected, set the volume inconsistent
4983 * bit in the volume attributes. The volume inconsistent bit is a persistent
4984 * bit which represents that the volume is corrupt and needs repair.
4985 * The volume inconsistent bit can be set from the kernel when it detects
4986 * runtime corruption or from file system repair utilities like fsck_hfs when
4987 * a repair operation fails. The bit should be cleared only from file system
4988 * verify/repair utility like fsck_hfs when a verify/repair succeeds.
4990 void hfs_mark_volume_inconsistent(struct hfsmount
*hfsmp
)
4992 HFS_MOUNT_LOCK(hfsmp
, TRUE
);
4993 if ((hfsmp
->vcbAtrb
& kHFSVolumeInconsistentMask
) == 0) {
4994 hfsmp
->vcbAtrb
|= kHFSVolumeInconsistentMask
;
4995 MarkVCBDirty(hfsmp
);
4997 /* Log information to ASL log */
4998 fslog_fs_corrupt(hfsmp
->hfs_mp
);
4999 printf("HFS: Runtime corruption detected on %s, fsck will be forced on next mount.\n", hfsmp
->vcbVN
);
5000 HFS_MOUNT_UNLOCK(hfsmp
, TRUE
);
5003 /* Replay the journal on the device node provided. Returns zero if
5004 * journal replay succeeded or no journal was supposed to be replayed.
5006 static int hfs_journal_replay(const char *devnode
, vfs_context_t context
)
5009 struct vnode
*devvp
= NULL
;
5010 struct mount
*mp
= NULL
;
5011 struct hfs_mount_args
*args
= NULL
;
5013 /* Lookup vnode for given raw device path */
5014 retval
= vnode_open(devnode
, FREAD
|FWRITE
, 0, 0, &devvp
, NULL
);
5019 /* Replay allowed only on raw devices */
5020 if (!vnode_ischr(devvp
)) {
5025 /* Create dummy mount structures */
5026 MALLOC(mp
, struct mount
*, sizeof(struct mount
), M_TEMP
, M_WAITOK
);
5027 bzero(mp
, sizeof(struct mount
));
5028 mount_lock_init(mp
);
5030 MALLOC(args
, struct hfs_mount_args
*, sizeof(struct hfs_mount_args
), M_TEMP
, M_WAITOK
);
5031 bzero(args
, sizeof(struct hfs_mount_args
));
5033 retval
= hfs_mountfs(devvp
, mp
, args
, 1, context
);
5034 buf_flushdirtyblks(devvp
, MNT_WAIT
, 0, "hfs_journal_replay");
5038 mount_lock_destroy(mp
);
5045 vnode_close(devvp
, FREAD
|FWRITE
, NULL
);
5051 * hfs vfs operations.
5053 struct vfsops hfs_vfsops
= {
5059 hfs_vfs_getattr
, /* was hfs_statfs */