2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1991, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 * (c) UNIX System Laboratories, Inc.
29 * All or some portions of this file are derived from material licensed
30 * to the University of California by American Telephone and Telegraph
31 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
32 * the permission of UNIX System Laboratories, Inc.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
65 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
67 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
70 #include <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/namei.h>
75 #include <sys/vnode.h>
76 #include <sys/mount.h>
77 #include <sys/malloc.h>
80 #include <sys/quota.h>
84 #include <vfs/vfs_journal.h>
86 #include <miscfs/specfs/specdev.h>
87 #include <hfs/hfs_mount.h>
90 #include "hfs_catalog.h"
91 #include "hfs_cnode.h"
93 #include "hfs_endian.h"
94 #include "hfs_quota.h"
96 #include "hfscommon/headers/FileMgrInternal.h"
97 #include "hfscommon/headers/BTreesInternal.h"
106 extern struct vnodeopv_desc hfs_vnodeop_opv_desc
;
108 extern void hfs_converterinit(void);
110 extern void inittodr( time_t base
);
113 static int hfs_changefs
__P((struct mount
*mp
, struct hfs_mount_args
*args
,
115 static int hfs_reload
__P((struct mount
*mp
, struct ucred
*cred
, struct proc
*p
));
117 static int hfs_mountfs
__P((struct vnode
*devvp
, struct mount
*mp
, struct proc
*p
,
118 struct hfs_mount_args
*args
));
119 static int hfs_statfs
__P((struct mount
*mp
, register struct statfs
*sbp
,
124 * Called by vfs_mountroot when mounting HFS Plus as root.
129 extern struct vnode
*rootvp
;
131 struct proc
*p
= current_proc(); /* XXX */
132 struct hfsmount
*hfsmp
;
137 * Get vnode for rootdev.
139 if ((error
= bdevvp(rootdev
, &rootvp
))) {
140 printf("hfs_mountroot: can't setup bdevvp");
143 if ((error
= vfs_rootmountalloc("hfs", "root_device", &mp
))) {
144 vrele(rootvp
); /* release the reference from bdevvp() */
147 if ((error
= hfs_mountfs(rootvp
, mp
, p
, NULL
))) {
148 mp
->mnt_vfc
->vfc_refcount
--;
150 vrele(rootvp
); /* release the reference from bdevvp() */
151 _FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
154 simple_lock(&mountlist_slock
);
155 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
156 simple_unlock(&mountlist_slock
);
159 hfsmp
= VFSTOHFS(mp
);
161 hfsmp
->hfs_uid
= UNKNOWNUID
;
162 hfsmp
->hfs_gid
= UNKNOWNGID
;
163 hfsmp
->hfs_dir_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
164 hfsmp
->hfs_file_mask
= (S_IRWXU
| S_IRGRP
|S_IXGRP
| S_IROTH
|S_IXOTH
); /* 0755 */
166 /* Establish the free block reserve. */
167 vcb
= HFSTOVCB(hfsmp
);
168 vcb
->reserveBlocks
= ((u_int64_t
)vcb
->totalBlocks
* HFS_MINFREE
) / 100;
169 vcb
->reserveBlocks
= MIN(vcb
->reserveBlocks
, HFS_MAXRESERVE
/ vcb
->blockSize
);
171 (void)hfs_statfs(mp
, &mp
->mnt_stat
, p
);
174 inittodr(HFSTOVCB(hfsmp
)->vcbLsMod
);
186 hfs_mount(mp
, path
, data
, ndp
, p
)
187 register struct mount
*mp
;
190 struct nameidata
*ndp
;
193 struct hfsmount
*hfsmp
= NULL
;
195 struct hfs_mount_args args
;
201 if ((retval
= copyin(data
, (caddr_t
)&args
, sizeof(args
))))
205 * If updating, check whether changing from read-only to
206 * read/write; if there is no device name, that's all we do.
208 if (mp
->mnt_flag
& MNT_UPDATE
) {
210 hfsmp
= VFSTOHFS(mp
);
211 if ((hfsmp
->hfs_fs_ronly
== 0) && (mp
->mnt_flag
& MNT_RDONLY
)) {
213 /* use VFS_SYNC to push out System (btree) files */
214 retval
= VFS_SYNC(mp
, MNT_WAIT
, p
->p_ucred
, p
);
215 if (retval
&& ((mp
->mnt_flag
& MNT_FORCE
) == 0))
219 if (mp
->mnt_flag
& MNT_FORCE
)
222 if ((retval
= hfs_flushfiles(mp
, flags
, p
)))
224 hfsmp
->hfs_fs_ronly
= 1;
225 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
227 /* also get the volume bitmap blocks */
229 retval
= VOP_FSYNC(hfsmp
->hfs_devvp
, NOCRED
, MNT_WAIT
, p
);
232 hfsmp
->hfs_fs_ronly
= 0;
237 if ((mp
->mnt_flag
& MNT_RELOAD
) &&
238 (retval
= hfs_reload(mp
, ndp
->ni_cnd
.cn_cred
, p
)))
241 if (hfsmp
->hfs_fs_ronly
&& (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
243 * If upgrade to read-write by non-root, then verify
244 * that user has necessary permissions on the device.
246 if (p
->p_ucred
->cr_uid
!= 0) {
247 devvp
= hfsmp
->hfs_devvp
;
248 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
249 if ((retval
= VOP_ACCESS(devvp
, VREAD
| VWRITE
, p
->p_ucred
, p
))) {
250 VOP_UNLOCK(devvp
, 0, p
);
253 VOP_UNLOCK(devvp
, 0, p
);
255 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
257 if (retval
!= E_NONE
)
260 /* only change hfs_fs_ronly after a successfull write */
261 hfsmp
->hfs_fs_ronly
= 0;
264 if ((hfsmp
->hfs_fs_ronly
== 0) &&
265 (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)) {
266 /* setup private/hidden directory for unlinked files */
267 hfsmp
->hfs_private_metadata_dir
= FindMetaDataDirectory(HFSTOVCB(hfsmp
));
269 hfs_remove_orphans(hfsmp
);
272 if (args
.fspec
== 0) {
274 * Process export requests.
276 return vfs_export(mp
, &hfsmp
->hfs_export
, &args
.export
);
281 * Not an update, or updating the name: look up the name
282 * and verify that it refers to a sensible block device.
284 NDINIT(ndp
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, args
.fspec
, p
);
286 if (retval
!= E_NONE
) {
287 DBG_ERR(("hfs_mount: CAN'T GET DEVICE: %s, %x\n", args
.fspec
, ndp
->ni_vp
->v_rdev
));
293 if (devvp
->v_type
!= VBLK
) {
298 if (major(devvp
->v_rdev
) >= nblkdev
) {
305 * If mount by non-root, then verify that user has necessary
306 * permissions on the device.
308 if (p
->p_ucred
->cr_uid
!= 0) {
310 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
311 accessmode
|= VWRITE
;
312 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
313 if ((retval
= VOP_ACCESS(devvp
, accessmode
, p
->p_ucred
, p
))) {
317 VOP_UNLOCK(devvp
, 0, p
);
320 if ((mp
->mnt_flag
& MNT_UPDATE
) == 0) {
321 retval
= hfs_mountfs(devvp
, mp
, p
, &args
);
322 if (retval
!= E_NONE
)
325 if (devvp
!= hfsmp
->hfs_devvp
)
326 retval
= EINVAL
; /* needs translation */
328 retval
= hfs_changefs(mp
, &args
, p
);
332 if (retval
!= E_NONE
) {
336 /* Set the mount flag to indicate that we support volfs */
337 mp
->mnt_flag
|= MNT_DOVOLFS
;
338 if (VFSTOVCB(mp
)->vcbSigWord
== kHFSSigWord
) {
339 /* HFS volumes only want roman-encoded names: */
340 mp
->mnt_flag
|= MNT_FIXEDSCRIPTENCODING
;
342 (void) copyinstr(path
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
-1, &size
);
344 bzero(mp
->mnt_stat
.f_mntonname
+ size
, MNAMELEN
- size
);
345 (void) copyinstr(args
.fspec
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, &size
);
346 bzero(mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
347 (void)hfs_statfs(mp
, &mp
->mnt_stat
, p
);
356 /* Change fs mount parameters */
358 hfs_changefs(mp
, args
, p
)
360 struct hfs_mount_args
*args
;
364 int namefix
, permfix
, permswitch
;
365 struct hfsmount
*hfsmp
;
368 register struct vnode
*vp
, *nvp
;
369 hfs_to_unicode_func_t get_unicode_func
;
370 unicode_to_hfs_func_t get_hfsname_func
;
371 struct cat_desc cndesc
;
372 struct cat_attr cnattr
;
375 hfsmp
= VFSTOHFS(mp
);
376 vcb
= HFSTOVCB(hfsmp
);
377 permswitch
= (((hfsmp
->hfs_unknownpermissions
!= 0) && ((mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) == 0)) ||
378 ((hfsmp
->hfs_unknownpermissions
== 0) && ((mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) != 0)));
379 /* The root filesystem must operate with actual permissions: */
380 if (permswitch
&& (mp
->mnt_flag
& MNT_ROOTFS
) && (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
)) {
381 mp
->mnt_flag
&= ~MNT_UNKNOWNPERMISSIONS
; /* Just say "No". */
384 hfsmp
->hfs_unknownpermissions
= ((mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) != 0);
385 namefix
= permfix
= 0;
387 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
388 if (args
->hfs_timezone
.tz_minuteswest
!= VNOVAL
) {
389 gTimeZone
= args
->hfs_timezone
;
392 /* Change the default uid, gid and/or mask */
393 if ((args
->hfs_uid
!= (uid_t
)VNOVAL
) && (hfsmp
->hfs_uid
!= args
->hfs_uid
)) {
394 hfsmp
->hfs_uid
= args
->hfs_uid
;
395 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
398 if ((args
->hfs_gid
!= (gid_t
)VNOVAL
) && (hfsmp
->hfs_gid
!= args
->hfs_gid
)) {
399 hfsmp
->hfs_gid
= args
->hfs_gid
;
400 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
403 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
404 if (hfsmp
->hfs_dir_mask
!= (args
->hfs_mask
& ALLPERMS
)) {
405 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
406 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
407 if ((args
->flags
!= VNOVAL
) && (args
->flags
& HFSFSMNT_NOXONFILES
))
408 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
409 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSPlusSigWord
)
414 /* Change the hfs encoding value (hfs only) */
415 if ((HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) &&
416 (hfsmp
->hfs_encoding
!= (u_long
)VNOVAL
) &&
417 (hfsmp
->hfs_encoding
!= args
->hfs_encoding
)) {
419 retval
= hfs_getconverter(args
->hfs_encoding
, &get_unicode_func
, &get_hfsname_func
);
424 * Connect the new hfs_get_unicode converter but leave
425 * the old hfs_get_hfsname converter in place so that
426 * we can lookup existing vnodes to get their correctly
429 * When we're all finished, we can then connect the new
430 * hfs_get_hfsname converter and release our interest
431 * in the old converters.
433 hfsmp
->hfs_get_unicode
= get_unicode_func
;
434 old_encoding
= hfsmp
->hfs_encoding
;
435 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
439 if (!(namefix
|| permfix
|| permswitch
))
443 * For each active vnode fix things that changed
445 * Note that we can visit a vnode more than once
446 * and we can race with fsync.
448 simple_lock(&mntvnode_slock
);
450 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
452 * If the vnode that we are about to fix is no longer
453 * associated with this mount point, start over.
455 if (vp
->v_mount
!= mp
)
458 simple_lock(&vp
->v_interlock
);
459 nvp
= vp
->v_mntvnodes
.le_next
;
460 if (vp
->v_flag
& VSYSTEM
) {
461 simple_unlock(&vp
->v_interlock
);
464 simple_unlock(&mntvnode_slock
);
465 retval
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
467 simple_lock(&mntvnode_slock
);
468 if (retval
== ENOENT
)
475 retval
= cat_lookup(hfsmp
, &cp
->c_desc
, 0, &cndesc
, &cnattr
, NULL
);
476 /* If we couldn't find this guy skip to the next one */
481 simple_lock(&mntvnode_slock
);
485 if (permswitch
|| permfix
) {
486 cp
->c_uid
= cnattr
.ca_uid
;
487 cp
->c_gid
= cnattr
.ca_gid
;
488 cp
->c_mode
= cnattr
.ca_mode
;
492 * If we're switching name converters then...
493 * Remove the existing entry from the namei cache.
494 * Update name to one based on new encoder.
498 replace_desc(cp
, &cndesc
);
500 if (cndesc
.cd_cnid
== kHFSRootFolderID
) {
501 strncpy(vcb
->vcbVN
, cp
->c_desc
.cd_nameptr
, NAME_MAX
);
502 cp
->c_desc
.cd_encoding
= hfsmp
->hfs_encoding
;
505 cat_releasedesc(&cndesc
);
508 simple_lock(&mntvnode_slock
);
510 } /* end for (vp...) */
511 simple_unlock(&mntvnode_slock
);
513 * If we're switching name converters we can now
514 * connect the new hfs_get_hfsname converter and
515 * release our interest in the old converters.
518 hfsmp
->hfs_get_hfsname
= get_hfsname_func
;
519 vcb
->volumeNameEncodingHint
= args
->hfs_encoding
;
520 (void) hfs_relconverter(old_encoding
);
528 * Reload all incore data for a filesystem (used after running fsck on
529 * the root filesystem and finding things to fix). The filesystem must
530 * be mounted read-only.
532 * Things to do to update the mount:
533 * invalidate all cached meta-data.
534 * invalidate all inactive vnodes.
535 * invalidate all cached file data.
536 * re-read volume header from disk.
537 * re-load meta-file info (extents, file size).
538 * re-load B-tree header data.
539 * re-read cnode data for all active vnodes.
542 hfs_reload(mountp
, cred
, p
)
543 register struct mount
*mountp
;
547 register struct vnode
*vp
, *nvp
, *devvp
;
552 struct hfsmount
*hfsmp
;
553 struct HFSPlusVolumeHeader
*vhp
;
555 struct filefork
*forkp
;
556 struct cat_desc cndesc
;
558 if ((mountp
->mnt_flag
& MNT_RDONLY
) == 0)
561 hfsmp
= VFSTOHFS(mountp
);
562 vcb
= HFSTOVCB(hfsmp
);
564 if (vcb
->vcbSigWord
== kHFSSigWord
)
565 return (EINVAL
); /* rooting from HFS is not supported! */
568 * Invalidate all cached meta-data.
570 devvp
= hfsmp
->hfs_devvp
;
571 if (vinvalbuf(devvp
, 0, cred
, p
, 0, 0))
572 panic("hfs_reload: dirty1");
573 InvalidateCatalogCache(vcb
);
576 simple_lock(&mntvnode_slock
);
577 for (vp
= mountp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
578 if (vp
->v_mount
!= mountp
) {
579 simple_unlock(&mntvnode_slock
);
582 nvp
= vp
->v_mntvnodes
.le_next
;
585 * Invalidate all inactive vnodes.
587 if (vrecycle(vp
, &mntvnode_slock
, p
))
591 * Invalidate all cached file data.
593 simple_lock(&vp
->v_interlock
);
594 simple_unlock(&mntvnode_slock
);
595 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, p
)) {
598 if (vinvalbuf(vp
, 0, cred
, p
, 0, 0))
599 panic("hfs_reload: dirty2");
602 * Re-read cnode data for all active vnodes (non-metadata files).
605 if ((vp
->v_flag
& VSYSTEM
) == 0 && !VNODE_IS_RSRC(vp
)) {
606 struct cat_fork
*datafork
;
607 struct cat_desc desc
;
609 datafork
= cp
->c_datafork
? &cp
->c_datafork
->ff_data
: NULL
;
611 /* lookup by fileID since name could have changed */
612 if ((error
= cat_idlookup(hfsmp
, cp
->c_fileid
, &desc
, &cp
->c_attr
, datafork
))) {
618 /* update cnode's catalog descriptor */
619 (void) replace_desc(cp
, &desc
);
622 simple_lock(&mntvnode_slock
);
624 simple_unlock(&mntvnode_slock
);
627 * Re-read VolumeHeader from disk.
629 sectorsize
= hfsmp
->hfs_phys_block_size
;
631 error
= meta_bread(hfsmp
->hfs_devvp
,
632 (vcb
->hfsPlusIOPosOffset
/ sectorsize
) + HFS_PRI_SECTOR(sectorsize
),
633 sectorsize
, NOCRED
, &bp
);
640 vhp
= (HFSPlusVolumeHeader
*) (bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
642 /* Do a quick sanity check */
643 if (SWAP_BE16(vhp
->signature
) != kHFSPlusSigWord
||
644 SWAP_BE16(vhp
->version
) != kHFSPlusVersion
||
645 SWAP_BE32(vhp
->blockSize
) != vcb
->blockSize
) {
650 vcb
->vcbLsMod
= to_bsd_time(SWAP_BE32(vhp
->modifyDate
));
651 vcb
->vcbAtrb
= (UInt16
) SWAP_BE32 (vhp
->attributes
); /* VCB only uses lower 16 bits */
652 vcb
->vcbJinfoBlock
= SWAP_BE32(vhp
->journalInfoBlock
);
653 vcb
->vcbClpSiz
= SWAP_BE32 (vhp
->rsrcClumpSize
);
654 vcb
->vcbNxtCNID
= SWAP_BE32 (vhp
->nextCatalogID
);
655 vcb
->vcbVolBkUp
= to_bsd_time(SWAP_BE32(vhp
->backupDate
));
656 vcb
->vcbWrCnt
= SWAP_BE32 (vhp
->writeCount
);
657 vcb
->vcbFilCnt
= SWAP_BE32 (vhp
->fileCount
);
658 vcb
->vcbDirCnt
= SWAP_BE32 (vhp
->folderCount
);
659 vcb
->nextAllocation
= SWAP_BE32 (vhp
->nextAllocation
);
660 vcb
->totalBlocks
= SWAP_BE32 (vhp
->totalBlocks
);
661 vcb
->freeBlocks
= SWAP_BE32 (vhp
->freeBlocks
);
662 vcb
->encodingsBitmap
= SWAP_BE64 (vhp
->encodingsBitmap
);
663 bcopy(vhp
->finderInfo
, vcb
->vcbFndrInfo
, sizeof(vhp
->finderInfo
));
664 vcb
->localCreateDate
= SWAP_BE32 (vhp
->createDate
); /* hfs+ create date is in local time */
667 * Re-load meta-file vnode data (extent info, file size, etc).
669 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
670 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
671 forkp
->ff_extents
[i
].startBlock
=
672 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].startBlock
);
673 forkp
->ff_extents
[i
].blockCount
=
674 SWAP_BE32 (vhp
->extentsFile
.extents
[i
].blockCount
);
676 forkp
->ff_size
= SWAP_BE64 (vhp
->extentsFile
.logicalSize
);
677 forkp
->ff_blocks
= SWAP_BE32 (vhp
->extentsFile
.totalBlocks
);
678 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->extentsFile
.clumpSize
);
681 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
682 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
683 forkp
->ff_extents
[i
].startBlock
=
684 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].startBlock
);
685 forkp
->ff_extents
[i
].blockCount
=
686 SWAP_BE32 (vhp
->catalogFile
.extents
[i
].blockCount
);
688 forkp
->ff_size
= SWAP_BE64 (vhp
->catalogFile
.logicalSize
);
689 forkp
->ff_blocks
= SWAP_BE32 (vhp
->catalogFile
.totalBlocks
);
690 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->catalogFile
.clumpSize
);
693 forkp
= VTOF((struct vnode
*)vcb
->allocationsRefNum
);
694 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
695 forkp
->ff_extents
[i
].startBlock
=
696 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].startBlock
);
697 forkp
->ff_extents
[i
].blockCount
=
698 SWAP_BE32 (vhp
->allocationFile
.extents
[i
].blockCount
);
700 forkp
->ff_size
= SWAP_BE64 (vhp
->allocationFile
.logicalSize
);
701 forkp
->ff_blocks
= SWAP_BE32 (vhp
->allocationFile
.totalBlocks
);
702 forkp
->ff_clumpsize
= SWAP_BE32 (vhp
->allocationFile
.clumpSize
);
708 * Re-load B-tree header data
710 forkp
= VTOF((struct vnode
*)vcb
->extentsRefNum
);
711 if (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) ))
714 forkp
= VTOF((struct vnode
*)vcb
->catalogRefNum
);
715 if (error
= MacToVFSError( BTReloadData((FCB
*)forkp
) ))
718 /* Reload the volume name */
719 if ((error
= cat_idlookup(hfsmp
, kHFSRootFolderID
, &cndesc
, NULL
, NULL
)))
721 vcb
->volumeNameEncodingHint
= cndesc
.cd_encoding
;
722 bcopy(cndesc
.cd_nameptr
, vcb
->vcbVN
, min(255, cndesc
.cd_namelen
));
723 cat_releasedesc(&cndesc
);
725 /* Re-establish private/hidden directory for unlinked files */
726 hfsmp
->hfs_private_metadata_dir
= FindMetaDataDirectory(vcb
);
733 get_raw_device(char *fspec
, int is_user
, int ronly
, struct vnode
**rvp
, struct ucred
*cred
, struct proc
*p
)
743 MALLOC(rawbuf
, char *, MAXPATHLEN
, M_HFSMNT
, M_WAITOK
);
744 if (rawbuf
== NULL
) {
750 retval
= copyinstr(fspec
, rawbuf
, MAXPATHLEN
- 1, &namelen
);
751 if (retval
!= E_NONE
) {
752 FREE(rawbuf
, M_HFSMNT
);
756 strcpy(rawbuf
, fspec
);
757 namelen
= strlen(rawbuf
);
760 /* make sure it's null terminated */
761 rawbuf
[MAXPATHLEN
-1] = '\0';
763 dp
= &rawbuf
[namelen
-1];
764 while(dp
>= rawbuf
&& *dp
!= '/') {
774 /* make room for and insert the 'r' for the raw device */
775 memmove(dp
+1, dp
, strlen(dp
)+1);
778 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, rawbuf
, p
);
780 if (retval
!= E_NONE
) {
781 DBG_ERR(("hfs_mountfs: can't open raw device for journal: %s, %x\n", rawbuf
, nd
.ni_vp
->v_rdev
));
782 FREE(rawbuf
, M_HFSMNT
);
787 if ((retval
= VOP_OPEN(*rvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
))) {
792 // don't need this any more
793 FREE(rawbuf
, M_HFSMNT
);
799 (void)VOP_CLOSE(*rvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
803 FREE(rawbuf
, M_HFSMNT
);
811 * Common code for mount and mountroot
814 hfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct proc
*p
,
815 struct hfs_mount_args
*args
)
818 struct hfsmount
*hfsmp
;
821 HFSMasterDirectoryBlock
*mdbp
;
829 u_int32_t minblksize
;
830 u_int32_t iswritable
;
834 cred
= p
? p
->p_ucred
: NOCRED
;
837 * Disallow multiple mounts of the same device.
838 * Disallow mounting of a device that is currently in use
839 * (except for root, which might share swap device for miniroot).
840 * Flush out any old buffers remaining from a previous use.
842 if ((retval
= vfs_mountedon(devvp
)))
844 if ((vcount(devvp
) > 1) && (devvp
!= rootvp
))
846 if ((retval
= vinvalbuf(devvp
, V_SAVE
, cred
, p
, 0, 0)))
849 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
850 if ((retval
= VOP_OPEN(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
)))
856 minblksize
= kHFSBlockSize
;
858 /* Get the real physical block size. */
859 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, cred
, p
)) {
863 /* Switch to 512 byte sectors (temporarily) */
865 u_int32_t size512
= 512;
867 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, cred
, p
)) {
872 /* Get the number of 512 byte physical blocks. */
873 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
877 /* Compute an accurate disk size (i.e. within 512 bytes) */
878 disksize
= blkcnt
* (u_int64_t
)512;
881 * There are only 31 bits worth of block count in
882 * the buffer cache. So for large volumes a 4K
883 * physical block size is needed.
885 if (blkcnt
> (u_int64_t
)0x000000007fffffff) {
886 minblksize
= blksize
= 4096;
888 /* Now switch to our prefered physical block size. */
890 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
894 /* Get the count of physical blocks. */
895 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
903 * minblksize is the minimum physical block size
904 * blksize has our prefered physical block size
905 * blkcnt has the total number of physical blocks
907 devvp
->v_specsize
= blksize
;
909 /* cache the IO attributes */
910 if ((retval
= vfs_init_io_attributes(devvp
, mp
))) {
911 printf("hfs_mountfs: vfs_init_io_attributes returned %d\n",
916 mdb_offset
= HFS_PRI_SECTOR(blksize
);
917 if ((retval
= meta_bread(devvp
, HFS_PRI_SECTOR(blksize
), blksize
, cred
, &bp
))) {
920 MALLOC(mdbp
, HFSMasterDirectoryBlock
*, kMDBSize
, M_TEMP
, M_WAITOK
);
921 bcopy(bp
->b_data
+ HFS_PRI_OFFSET(blksize
), mdbp
, kMDBSize
);
925 MALLOC(hfsmp
, struct hfsmount
*, sizeof(struct hfsmount
), M_HFSMNT
, M_WAITOK
);
926 bzero(hfsmp
, sizeof(struct hfsmount
));
928 simple_lock_init(&hfsmp
->hfs_renamelock
);
931 * Init the volume information structure
933 mp
->mnt_data
= (qaddr_t
)hfsmp
;
934 hfsmp
->hfs_mp
= mp
; /* Make VFSTOHFS work */
935 hfsmp
->hfs_vcb
.vcb_hfsmp
= hfsmp
; /* Make VCBTOHFS work */
936 hfsmp
->hfs_raw_dev
= devvp
->v_rdev
;
937 hfsmp
->hfs_devvp
= devvp
;
938 hfsmp
->hfs_phys_block_size
= blksize
;
939 hfsmp
->hfs_phys_block_count
= blkcnt
;
940 hfsmp
->hfs_media_writeable
= 1;
941 hfsmp
->hfs_fs_ronly
= ronly
;
942 hfsmp
->hfs_unknownpermissions
= ((mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) != 0);
943 for (i
= 0; i
< MAXQUOTAS
; i
++)
944 hfsmp
->hfs_qfiles
[i
].qf_vp
= NULLVP
;
947 hfsmp
->hfs_uid
= (args
->hfs_uid
== (uid_t
)VNOVAL
) ? UNKNOWNUID
: args
->hfs_uid
;
948 if (hfsmp
->hfs_uid
== 0xfffffffd) hfsmp
->hfs_uid
= UNKNOWNUID
;
949 hfsmp
->hfs_gid
= (args
->hfs_gid
== (gid_t
)VNOVAL
) ? UNKNOWNGID
: args
->hfs_gid
;
950 if (hfsmp
->hfs_gid
== 0xfffffffd) hfsmp
->hfs_gid
= UNKNOWNGID
;
951 if (args
->hfs_mask
!= (mode_t
)VNOVAL
) {
952 hfsmp
->hfs_dir_mask
= args
->hfs_mask
& ALLPERMS
;
953 if (args
->flags
& HFSFSMNT_NOXONFILES
) {
954 hfsmp
->hfs_file_mask
= (args
->hfs_mask
& DEFFILEMODE
);
956 hfsmp
->hfs_file_mask
= args
->hfs_mask
& ALLPERMS
;
959 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
960 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
962 if ((args
->flags
!= (int)VNOVAL
) && (args
->flags
& HFSFSMNT_WRAPPER
))
965 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
966 if (mp
->mnt_flag
& MNT_UNKNOWNPERMISSIONS
) {
967 hfsmp
->hfs_uid
= UNKNOWNUID
;
968 hfsmp
->hfs_gid
= UNKNOWNGID
;
969 hfsmp
->hfs_dir_mask
= UNKNOWNPERMISSIONS
& ALLPERMS
; /* 0777: rwx---rwx */
970 hfsmp
->hfs_file_mask
= UNKNOWNPERMISSIONS
& DEFFILEMODE
; /* 0666: no --x by default? */
974 /* Find out if disk media is writable. */
975 if (VOP_IOCTL(devvp
, DKIOCISWRITABLE
, (caddr_t
)&iswritable
, 0, cred
, p
) == 0) {
977 hfsmp
->hfs_media_writeable
= 1;
979 hfsmp
->hfs_media_writeable
= 0;
982 /* Mount a standard HFS disk */
983 if ((SWAP_BE16(mdbp
->drSigWord
) == kHFSSigWord
) &&
984 (mntwrapper
|| (SWAP_BE16(mdbp
->drEmbedSigWord
) != kHFSPlusSigWord
))) {
985 if (devvp
== rootvp
) {
986 retval
= EINVAL
; /* Cannot root from HFS standard disks */
989 /* HFS disks can only use 512 byte physical blocks */
990 if (blksize
> kHFSBlockSize
) {
991 blksize
= kHFSBlockSize
;
992 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
996 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1000 devvp
->v_specsize
= blksize
;
1001 hfsmp
->hfs_phys_block_size
= blksize
;
1002 hfsmp
->hfs_phys_block_count
= blkcnt
;
1005 hfsmp
->hfs_encoding
= args
->hfs_encoding
;
1006 HFSTOVCB(hfsmp
)->volumeNameEncodingHint
= args
->hfs_encoding
;
1008 /* establish the timezone */
1009 gTimeZone
= args
->hfs_timezone
;
1012 retval
= hfs_getconverter(hfsmp
->hfs_encoding
, &hfsmp
->hfs_get_unicode
,
1013 &hfsmp
->hfs_get_hfsname
);
1017 retval
= hfs_MountHFSVolume(hfsmp
, mdbp
, p
);
1019 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1021 } else /* Mount an HFS Plus disk */ {
1022 HFSPlusVolumeHeader
*vhp
;
1023 off_t embeddedOffset
;
1024 int jnl_disable
= 0;
1026 /* Get the embedded Volume Header */
1027 if (SWAP_BE16(mdbp
->drEmbedSigWord
) == kHFSPlusSigWord
) {
1028 embeddedOffset
= SWAP_BE16(mdbp
->drAlBlSt
) * kHFSBlockSize
;
1029 embeddedOffset
+= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.startBlock
) *
1030 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1033 * If the embedded volume doesn't start on a block
1034 * boundary, then switch the device to a 512-byte
1035 * block size so everything will line up on a block
1038 if ((embeddedOffset
% blksize
) != 0) {
1039 printf("HFS Mount: embedded volume offset not"
1040 " a multiple of physical block size (%d);"
1041 " switching to 512\n", blksize
);
1043 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
,
1044 (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1048 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
,
1049 (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1053 /* XXX do we need to call vfs_init_io_attributes again? */
1054 devvp
->v_specsize
= blksize
;
1055 /* Note: relative block count adjustment */
1056 hfsmp
->hfs_phys_block_count
*=
1057 hfsmp
->hfs_phys_block_size
/ blksize
;
1058 hfsmp
->hfs_phys_block_size
= blksize
;
1061 disksize
= (u_int64_t
)SWAP_BE16(mdbp
->drEmbedExtent
.blockCount
) *
1062 (u_int64_t
)SWAP_BE32(mdbp
->drAlBlkSiz
);
1064 hfsmp
->hfs_phys_block_count
= disksize
/ blksize
;
1066 mdb_offset
= (embeddedOffset
/ blksize
) + HFS_PRI_SECTOR(blksize
);
1067 retval
= meta_bread(devvp
, mdb_offset
, blksize
, cred
, &bp
);
1070 bcopy(bp
->b_data
+ HFS_PRI_OFFSET(blksize
), mdbp
, 512);
1073 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1075 } else /* pure HFS+ */ {
1077 vhp
= (HFSPlusVolumeHeader
*) mdbp
;
1084 if (args
!= NULL
&& (args
->flags
& HFSFSMNT_EXTENDED_ARGS
) && args
->journal_disable
) {
1089 // We only initialize the journal here if the last person
1090 // to mount this volume was journaling aware. Otherwise
1091 // we delay journal initialization until later at the end
1092 // of hfs_MountHFSPlusVolume() because the last person who
1093 // mounted it could have messed things up behind our back
1094 // (so we need to go find the .journal file, make sure it's
1095 // the right size, re-sync up if it was moved, etc).
1097 if ( (SWAP_BE32(vhp
->lastMountedVersion
) == kHFSJMountVersion
)
1098 && (SWAP_BE32(vhp
->attributes
) & kHFSVolumeJournaledMask
)
1101 // if we're able to init the journal, mark the mount
1102 // point as journaled.
1104 if (hfs_early_journal_init(hfsmp
, vhp
, args
, embeddedOffset
, mdb_offset
, mdbp
, cred
) == 0) {
1105 mp
->mnt_flag
|= MNT_JOURNALED
;
1113 (void) hfs_getconverter(0, &hfsmp
->hfs_get_unicode
, &hfsmp
->hfs_get_hfsname
);
1115 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
);
1117 * If the backend didn't like our physical blocksize
1118 * then retry with physical blocksize of 512.
1120 if ((retval
== ENXIO
) && (blksize
> 512) && (blksize
!= minblksize
)) {
1121 printf("HFS Mount: could not use physical block size "
1122 "(%d) switching to 512\n", blksize
);
1124 if (VOP_IOCTL(devvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, cred
, p
)) {
1128 if (VOP_IOCTL(devvp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, cred
, p
)) {
1132 devvp
->v_specsize
= blksize
;
1133 /* Note: relative block count adjustment (in case this is an embedded volume). */
1134 hfsmp
->hfs_phys_block_count
*= hfsmp
->hfs_phys_block_size
/ blksize
;
1135 hfsmp
->hfs_phys_block_size
= blksize
;
1137 /* Try again with a smaller block size... */
1138 retval
= hfs_MountHFSPlusVolume(hfsmp
, vhp
, embeddedOffset
, disksize
, p
, args
);
1141 (void) hfs_relconverter(0);
1148 mp
->mnt_stat
.f_fsid
.val
[0] = (long)dev
;
1149 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_vfc
->vfc_typenum
;
1150 mp
->mnt_maxsymlinklen
= 0;
1151 devvp
->v_specflags
|= SI_MOUNTEDON
;
1154 (void) hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 0);
1164 (void)VOP_CLOSE(devvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
1165 if (hfsmp
&& hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1166 (void)VOP_CLOSE(hfsmp
->jvp
, ronly
? FREAD
: FREAD
|FWRITE
, cred
, p
);
1170 FREE(hfsmp
, M_HFSMNT
);
1171 mp
->mnt_data
= (qaddr_t
)0;
1178 * Make a filesystem operational.
1179 * Nothing to do at the moment.
1183 hfs_start(mp
, flags
, p
)
1193 * unmount system call
1196 hfs_unmount(mp
, mntflags
, p
)
1201 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1202 int retval
= E_NONE
;
1205 int started_tr
= 0, grabbed_lock
= 0;
1209 if (mntflags
& MNT_FORCE
) {
1210 flags
|= FORCECLOSE
;
1214 if ((retval
= hfs_flushfiles(mp
, flags
, p
)) && !force
)
1218 * Flush out the b-trees, volume bitmap and Volume Header
1220 if (hfsmp
->hfs_fs_ronly
== 0) {
1221 hfs_global_shared_lock_acquire(hfsmp
);
1224 journal_start_transaction(hfsmp
->jnl
);
1228 retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->catalogRefNum
, NOCRED
, MNT_WAIT
, p
);
1229 if (retval
&& !force
)
1232 retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->extentsRefNum
, NOCRED
, MNT_WAIT
, p
);
1233 if (retval
&& !force
)
1236 // if we have an allocation file, sync it too so we don't leave dirty
1238 if (HFSTOVCB(hfsmp
)->allocationsRefNum
) {
1239 if (retval
= VOP_FSYNC(HFSTOVCB(hfsmp
)->allocationsRefNum
, NOCRED
, MNT_WAIT
, p
)) {
1245 if (retval
= VOP_FSYNC(hfsmp
->hfs_devvp
, NOCRED
, MNT_WAIT
, p
)) {
1250 /* See if this volume is damaged, is so do not unmount cleanly */
1251 if (HFSTOVCB(hfsmp
)->vcbFlags
& kHFS_DamagedVolume
) {
1252 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1254 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeUnmountedMask
;
1257 retval
= hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
1259 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeUnmountedMask
;
1261 goto err_exit
; /* could not flush everything */
1265 journal_end_transaction(hfsmp
->jnl
);
1269 hfs_global_shared_lock_release(hfsmp
);
1275 journal_flush(hfsmp
->jnl
);
1279 * Invalidate our caches and release metadata vnodes
1281 (void) hfsUnmount(hfsmp
, p
);
1283 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
)
1284 (void) hfs_relconverter(hfsmp
->hfs_encoding
);
1288 journal_close(hfsmp
->jnl
);
1291 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1292 retval
= VOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_fs_ronly
? FREAD
: FREAD
|FWRITE
,
1299 hfsmp
->hfs_devvp
->v_specflags
&= ~SI_MOUNTEDON
;
1300 retval
= VOP_CLOSE(hfsmp
->hfs_devvp
,
1301 hfsmp
->hfs_fs_ronly
? FREAD
: FREAD
|FWRITE
,
1303 if (retval
&& !force
)
1306 vrele(hfsmp
->hfs_devvp
);
1307 FREE(hfsmp
, M_HFSMNT
);
1308 mp
->mnt_data
= (qaddr_t
)0;
1312 if (hfsmp
->jnl
&& started_tr
) {
1313 journal_end_transaction(hfsmp
->jnl
);
1316 hfs_global_shared_lock_release(hfsmp
);
1323 * Return the root of a filesystem.
1325 * OUT - vpp, should be locked and vget()'d (to increment usecount and lock)
1334 UInt32 rootObjID
= kRootDirID
;
1336 if ((retval
= VFS_VGET(mp
, &rootObjID
, &nvp
)))
1345 * Do operations associated with quotas
1348 hfs_quotactl(mp
, cmds
, uid
, arg
, p
)
1355 int cmd
, type
, error
;
1358 return (EOPNOTSUPP
);
1361 uid
= p
->p_cred
->p_ruid
;
1362 cmd
= cmds
>> SUBCMDSHIFT
;
1369 if (uid
== p
->p_cred
->p_ruid
)
1373 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
1377 type
= cmds
& SUBCMDMASK
;
1378 if ((u_int
)type
>= MAXQUOTAS
)
1380 if (vfs_busy(mp
, LK_NOWAIT
, 0, p
))
1386 error
= hfs_quotaon(p
, mp
, type
, arg
, UIO_USERSPACE
);
1390 error
= hfs_quotaoff(p
, mp
, type
);
1394 error
= hfs_setquota(mp
, uid
, type
, arg
);
1398 error
= hfs_setuse(mp
, uid
, type
, arg
);
1402 error
= hfs_getquota(mp
, uid
, type
, arg
);
1406 error
= hfs_qsync(mp
);
1410 error
= hfs_quotastat(mp
, type
, arg
);
1426 * Get file system statistics.
1429 hfs_statfs(mp
, sbp
, p
)
1431 register struct statfs
*sbp
;
1434 ExtendedVCB
*vcb
= VFSTOVCB(mp
);
1435 struct hfsmount
*hfsmp
= VFSTOHFS(mp
);
1438 freeCNIDs
= (u_long
)0xFFFFFFFF - (u_long
)vcb
->vcbNxtCNID
;
1440 sbp
->f_bsize
= vcb
->blockSize
;
1441 sbp
->f_iosize
= hfsmp
->hfs_logBlockSize
;
1442 sbp
->f_blocks
= vcb
->totalBlocks
;
1443 sbp
->f_bfree
= hfs_freeblks(hfsmp
, 0);
1444 sbp
->f_bavail
= hfs_freeblks(hfsmp
, 1);
1445 sbp
->f_files
= vcb
->totalBlocks
- 2; /* max files is constrained by total blocks */
1446 sbp
->f_ffree
= MIN(freeCNIDs
, sbp
->f_bavail
);
1449 if (sbp
!= &mp
->mnt_stat
) {
1450 sbp
->f_type
= mp
->mnt_vfc
->vfc_typenum
;
1451 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
1452 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
1453 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
1454 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
1461 // XXXdbg -- this is a callback to be used by the journal to
1462 // get meta data blocks flushed out to disk.
1464 // XXXdbg -- be smarter and don't flush *every* block on each
1465 // call. try to only flush some so we don't wind up
1466 // being too synchronous.
1470 hfs_sync_metadata(void *arg
)
1472 struct mount
*mp
= (struct mount
*)arg
;
1474 struct hfsmount
*hfsmp
;
1476 struct vnode
*meta_vp
[3];
1478 int i
, sectorsize
, priIDSector
, altIDSector
, retval
;
1479 int error
, allerror
= 0;
1481 hfsmp
= VFSTOHFS(mp
);
1482 vcb
= HFSTOVCB(hfsmp
);
1484 bflushq(BQ_META
, mp
);
1487 #if 1 // XXXdbg - I do not believe this is necessary...
1488 // but if I pull it out, then the journal
1489 // does not seem to get flushed properly
1490 // when it is closed....
1492 // now make sure the super block is flushed
1493 sectorsize
= hfsmp
->hfs_phys_block_size
;
1494 priIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1495 HFS_PRI_SECTOR(sectorsize
);
1496 retval
= meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
1498 panic("hfs: sync_metadata: can't read super-block?! (retval 0x%x, priIDSector)\n",
1499 retval
, priIDSector
);
1502 if (retval
== 0 && (bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1508 // the alternate super block...
1509 // XXXdbg - we probably don't need to do this each and every time.
1510 // hfs_btreeio.c:FlushAlternate() should flag when it was
1512 altIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
1513 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
1514 retval
= meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &bp
);
1515 if (retval
== 0 && (bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1525 * Go through the disk queues to initiate sandbagged IO;
1526 * go through the inodes to write those that have been modified;
1527 * initiate the writing of the super block if it has been modified.
1529 * Note: we are always called with the filesystem marked `MPBUSY'.
1532 hfs_sync(mp
, waitfor
, cred
, p
)
1538 struct vnode
*nvp
, *vp
;
1540 struct hfsmount
*hfsmp
;
1542 struct vnode
*meta_vp
[3];
1544 int error
, allerror
= 0;
1547 * During MNT_UPDATE hfs_changefs might be manipulating
1548 * vnodes so back off
1550 if (mp
->mnt_flag
& MNT_UPDATE
)
1553 hfsmp
= VFSTOHFS(mp
);
1554 if (hfsmp
->hfs_fs_ronly
!= 0) {
1555 panic("update: rofs mod");
1559 // XXXdbg first go through and flush out any modified
1560 // meta data blocks so they go out in order...
1561 bflushq(BQ_META
, mp
);
1562 bflushq(BQ_LRU
, mp
);
1563 // only flush locked blocks if we're not doing journaling
1564 if (hfsmp
->jnl
== NULL
) {
1565 bflushq(BQ_LOCKED
, mp
);
1570 * Write back each 'modified' vnode
1574 simple_lock(&mntvnode_slock
);
1575 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
!= NULL
; vp
= nvp
) {
1578 * If the vnode that we are about to sync is no longer
1579 * associated with this mount point, start over.
1581 if (vp
->v_mount
!= mp
) {
1582 simple_unlock(&mntvnode_slock
);
1586 simple_lock(&vp
->v_interlock
);
1587 nvp
= vp
->v_mntvnodes
.le_next
;
1591 // restart our whole search if this guy is locked
1592 // or being reclaimed.
1593 // XXXdbg - at some point this should go away or we
1594 // need to change all file systems to have
1595 // this same code. vget() should never return
1596 // success if either of these conditions is
1598 if (vp
->v_tag
!= VT_HFS
|| cp
== NULL
) {
1599 simple_unlock(&vp
->v_interlock
);
1603 if ((vp
->v_flag
& VSYSTEM
) || (vp
->v_type
== VNON
) ||
1604 (((cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) == 0) &&
1605 (vp
->v_dirtyblkhd
.lh_first
== NULL
) && !(vp
->v_flag
& VHASDIRTY
))) {
1606 simple_unlock(&vp
->v_interlock
);
1607 simple_unlock(&mntvnode_slock
);
1608 simple_lock(&mntvnode_slock
);
1612 simple_unlock(&mntvnode_slock
);
1613 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
1615 if (error
== ENOENT
)
1617 simple_lock(&mntvnode_slock
);
1621 didhold
= ubc_hold(vp
);
1622 if ((error
= VOP_FSYNC(vp
, cred
, waitfor
, p
))) {
1625 VOP_UNLOCK(vp
, 0, p
);
1629 simple_lock(&mntvnode_slock
);
1632 vcb
= HFSTOVCB(hfsmp
);
1634 meta_vp
[0] = vcb
->extentsRefNum
;
1635 meta_vp
[1] = vcb
->catalogRefNum
;
1636 meta_vp
[2] = vcb
->allocationsRefNum
; /* This is NULL for standard HFS */
1638 /* Now sync our three metadata files */
1639 for (i
= 0; i
< 3; ++i
) {
1642 btvp
= btvp
= meta_vp
[i
];;
1643 if ((btvp
==0) || (btvp
->v_type
== VNON
) || (btvp
->v_mount
!= mp
))
1646 simple_lock(&btvp
->v_interlock
);
1648 if (((cp
->c_flag
& (C_ACCESS
| C_CHANGE
| C_MODIFIED
| C_UPDATE
)) == 0) &&
1649 (btvp
->v_dirtyblkhd
.lh_first
== NULL
) && !(btvp
->v_flag
& VHASDIRTY
)) {
1650 simple_unlock(&btvp
->v_interlock
);
1653 simple_unlock(&mntvnode_slock
);
1654 error
= vget(btvp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
, p
);
1656 simple_lock(&mntvnode_slock
);
1659 if ((error
= VOP_FSYNC(btvp
, cred
, waitfor
, p
)))
1661 VOP_UNLOCK(btvp
, 0, p
);
1663 simple_lock(&mntvnode_slock
);
1666 simple_unlock(&mntvnode_slock
);
1669 * Force stale file system control information to be flushed.
1671 if (vcb
->vcbSigWord
== kHFSSigWord
) {
1672 if ((error
= VOP_FSYNC(hfsmp
->hfs_devvp
, cred
, waitfor
, p
)))
1679 * Write back modified superblock.
1682 if (IsVCBDirty(vcb
)) {
1683 // XXXdbg - debugging, remove
1685 //printf("hfs: sync: strange, a journaled volume w/dirty VCB? jnl 0x%x hfsmp 0x%x\n",
1686 // hfsmp->jnl, hfsmp);
1689 error
= hfs_flushvolumeheader(hfsmp
, waitfor
, 0);
1695 journal_flush(hfsmp
->jnl
);
1704 * File handle to vnode
1706 * Have to be really careful about stale file handles:
1707 * - check that the cnode id is valid
1708 * - call hfs_vget() to get the locked cnode
1709 * - check for an unallocated cnode (i_mode == 0)
1710 * - check that the given client host has export rights and return
1711 * those rights via. exflagsp and credanonp
1714 hfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
1715 register struct mount
*mp
;
1720 struct ucred
**credanonp
;
1722 struct hfsfid
*hfsfhp
;
1728 hfsfhp
= (struct hfsfid
*)fhp
;
1731 * Get the export permission structure for this <mp, client> tuple.
1733 np
= vfs_export_lookup(mp
, &VFSTOHFS(mp
)->hfs_export
, nam
);
1738 result
= VFS_VGET(mp
, &hfsfhp
->hfsfid_cnid
, &nvp
);
1739 if (result
) return result
;
1740 if (nvp
== NULL
) return ESTALE
;
1742 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1743 * For NFS, we are assuming that only if the createtime was moved
1744 * forward would it mean the fileID got reused in that session by
1745 * wrapping. We don't have a volume ID or other unique identifier to
1746 * to use here for a generation ID across reboots, crashes where
1747 * metadata noting lastFileID didn't make it to disk but client has
1748 * it, or volume erasures where fileIDs start over again. Lastly,
1749 * with HFS allowing "wraps" of fileIDs now, this becomes more
1750 * error prone. Future, would be change the "wrap bit" to a unique
1751 * wrap number and use that for generation number. For now do this.
1753 if ((hfsfhp
->hfsfid_gen
< VTOC(nvp
)->c_itime
)) {
1759 *exflagsp
= np
->netc_exflags
;
1760 *credanonp
= &np
->netc_anon
;
1767 * Vnode pointer to File handle
1776 struct hfsfid
*hfsfhp
;
1778 if (ISHFS(VTOVCB(vp
)))
1779 return (EOPNOTSUPP
); /* hfs standard is not exportable */
1782 hfsfhp
= (struct hfsfid
*)fhp
;
1783 hfsfhp
->hfsfid_len
= sizeof(struct hfsfid
);
1784 hfsfhp
->hfsfid_pad
= 0;
1785 hfsfhp
->hfsfid_cnid
= cp
->c_cnid
;
1786 hfsfhp
->hfsfid_gen
= cp
->c_itime
;
1793 * Initial HFS filesystems, done only once.
1797 struct vfsconf
*vfsp
;
1799 static int done
= 0;
1805 hfs_converterinit();
1811 * Allocate Catalog Iterator cache...
1813 (void) InitCatalogCache();
1820 #include <sys/filedesc.h>
1824 * HFS filesystem related variables.
1827 hfs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
1836 extern u_int32_t hfs_encodingbias
;
1838 /* all sysctl names at this level are terminal */
1840 if (name
[0] == HFS_ENCODINGBIAS
)
1841 return (sysctl_int(oldp
, oldlenp
, newp
, newlen
,
1842 &hfs_encodingbias
));
1843 else if (name
[0] == 0x082969) {
1844 // make the file system journaled...
1845 struct vnode
*vp
= p
->p_fd
->fd_cdir
, *jvp
;
1846 struct hfsmount
*hfsmp
;
1849 struct cat_attr jnl_attr
, jinfo_attr
;
1850 struct cat_fork jnl_fork
, jinfo_fork
;
1853 /* Only root can enable journaling */
1854 if (current_proc()->p_ucred
->cr_uid
!= 0) {
1858 if (hfsmp
->hfs_fs_ronly
) {
1861 if (HFSTOVCB(hfsmp
)->vcbSigWord
== kHFSSigWord
) {
1862 printf("hfs: can't make a plain hfs volume journaled.\n");
1867 printf("hfs: volume @ mp 0x%x is already journaled!\n", vp
->v_mount
);
1871 vcb
= HFSTOVCB(hfsmp
);
1872 if (BTHasContiguousNodes(VTOF(vcb
->catalogRefNum
)) == 0 ||
1873 BTHasContiguousNodes(VTOF(vcb
->extentsRefNum
)) == 0) {
1875 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
1879 // make sure these both exist!
1880 if ( GetFileInfo(vcb
, kRootDirID
, ".journal_info_block", &jinfo_attr
, &jinfo_fork
) == 0
1881 || GetFileInfo(vcb
, kRootDirID
, ".journal", &jnl_attr
, &jnl_fork
) == 0) {
1886 hfs_sync(hfsmp
->hfs_mp
, MNT_WAIT
, FSCRED
, p
);
1889 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
1890 (off_t
)name
[2], (off_t
)name
[3]);
1892 jvp
= hfsmp
->hfs_devvp
;
1893 jnl
= journal_create(jvp
,
1894 (off_t
)name
[2] * (off_t
)HFSTOVCB(hfsmp
)->blockSize
1895 + HFSTOVCB(hfsmp
)->hfsPlusIOPosOffset
,
1898 hfsmp
->hfs_phys_block_size
,
1901 hfs_sync_metadata
, hfsmp
->hfs_mp
);
1904 printf("hfs: FAILED to create the journal!\n");
1905 if (jvp
&& jvp
!= hfsmp
->hfs_devvp
) {
1906 VOP_CLOSE(jvp
, hfsmp
->hfs_fs_ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
);
1913 hfs_global_exclusive_lock_acquire(hfsmp
);
1915 HFSTOVCB(hfsmp
)->vcbJinfoBlock
= name
[1];
1916 HFSTOVCB(hfsmp
)->vcbAtrb
|= kHFSVolumeJournaledMask
;
1920 // save this off for the hack-y check in hfs_remove()
1921 hfsmp
->jnl_start
= (u_int32_t
)name
[2];
1922 hfsmp
->hfs_jnlinfoblkid
= jinfo_attr
.ca_fileid
;
1923 hfsmp
->hfs_jnlfileid
= jnl_attr
.ca_fileid
;
1925 hfsmp
->hfs_mp
->mnt_flag
|= MNT_JOURNALED
;
1927 hfs_global_exclusive_lock_release(hfsmp
);
1928 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
1931 } else if (name
[0] == 0x031272) {
1932 // clear the journaling bit
1933 struct vnode
*vp
= p
->p_fd
->fd_cdir
;
1934 struct hfsmount
*hfsmp
;
1938 /* Only root can disable journaling */
1939 if (current_proc()->p_ucred
->cr_uid
!= 0) {
1943 if (hfsmp
->jnl
== NULL
) {
1947 printf("hfs: disabling journaling for mount @ 0x%x\n", vp
->v_mount
);
1951 hfs_global_exclusive_lock_acquire(hfsmp
);
1953 // Lights out for you buddy!
1957 if (hfsmp
->jvp
&& hfsmp
->jvp
!= hfsmp
->hfs_devvp
) {
1958 VOP_CLOSE(hfsmp
->jvp
, hfsmp
->hfs_fs_ronly
? FREAD
: FREAD
|FWRITE
, FSCRED
, p
);
1962 hfsmp
->hfs_mp
->mnt_flag
&= ~MNT_JOURNALED
;
1963 hfsmp
->jnl_start
= 0;
1964 hfsmp
->hfs_jnlinfoblkid
= 0;
1965 hfsmp
->hfs_jnlfileid
= 0;
1967 HFSTOVCB(hfsmp
)->vcbAtrb
&= ~kHFSVolumeJournaledMask
;
1969 hfs_global_exclusive_lock_release(hfsmp
);
1970 hfs_flushvolumeheader(hfsmp
, MNT_WAIT
, 1);
1975 return (EOPNOTSUPP
);
1979 /* This will return a vnode of either a directory or a data vnode based on an object id. If
1980 * it is a file id, its data fork will be returned.
1983 hfs_vget(mp
, ino
, vpp
)
1988 cnid_t cnid
= *(cnid_t
*)ino
;
1990 /* Check for cnids that should't be exported. */
1991 if ((cnid
< kHFSFirstUserCatalogNodeID
)
1992 && (cnid
!= kHFSRootFolderID
&& cnid
!= kHFSRootParentID
))
1994 /* Don't export HFS Private Data dir. */
1995 if (cnid
== VFSTOHFS(mp
)->hfs_privdir_desc
.cd_cnid
)
1998 return (hfs_getcnode(VFSTOHFS(mp
), cnid
, NULL
, 0, NULL
, NULL
, vpp
));
2002 * Flush out all the files in a filesystem.
2005 hfs_flushfiles(struct mount
*mp
, int flags
, struct proc
*p
)
2007 register struct hfsmount
*hfsmp
;
2012 hfsmp
= VFSTOHFS(mp
);
2014 if (mp
->mnt_flag
& MNT_QUOTA
) {
2015 if (error
= vflush(mp
, NULLVP
, SKIPSYSTEM
|flags
))
2017 for (i
= 0; i
< MAXQUOTAS
; i
++) {
2018 if (hfsmp
->hfs_qfiles
[i
].qf_vp
== NULLVP
)
2020 hfs_quotaoff(p
, mp
, i
);
2023 * Here we fall through to vflush again to ensure
2024 * that we have gotten rid of all the system vnodes.
2029 error
= vflush(mp
, NULLVP
, (SKIPSYSTEM
| SKIPSWAP
| flags
));
2030 error
= vflush(mp
, NULLVP
, (SKIPSYSTEM
| flags
));
2036 * Update volume encoding bitmap (HFS Plus only)
2040 hfs_setencodingbits(struct hfsmount
*hfsmp
, u_int32_t encoding
)
2042 #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2043 #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2048 case kTextEncodingMacUkrainian
:
2049 index
= kIndexMacUkrainian
;
2051 case kTextEncodingMacFarsi
:
2052 index
= kIndexMacFarsi
;
2060 HFSTOVCB(hfsmp
)->encodingsBitmap
|= (1 << index
);
2061 HFSTOVCB(hfsmp
)->vcbFlags
|= 0xFF00;
2066 * Update volume stats
2070 hfs_volupdate(struct hfsmount
*hfsmp
, enum volop op
, int inroot
)
2074 vcb
= HFSTOVCB(hfsmp
);
2075 vcb
->vcbFlags
|= 0xFF00;
2076 vcb
->vcbLsMod
= time
.tv_sec
;
2082 if (vcb
->vcbDirCnt
!= 0xFFFFFFFF)
2084 if (inroot
&& vcb
->vcbNmRtDirs
!= 0xFFFF)
2088 if (vcb
->vcbDirCnt
!= 0)
2090 if (inroot
&& vcb
->vcbNmRtDirs
!= 0xFFFF)
2094 if (vcb
->vcbFilCnt
!= 0xFFFFFFFF)
2096 if (inroot
&& vcb
->vcbNmFls
!= 0xFFFF)
2100 if (vcb
->vcbFilCnt
!= 0)
2102 if (inroot
&& vcb
->vcbNmFls
!= 0xFFFF)
2108 hfs_flushvolumeheader(hfsmp
, 0, 0);
2116 hfs_flushMDB(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2118 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2119 struct filefork
*fp
;
2120 HFSMasterDirectoryBlock
*mdb
;
2121 struct buf
*bp
= NULL
;
2126 sectorsize
= hfsmp
->hfs_phys_block_size
;
2127 retval
= bread(hfsmp
->hfs_devvp
, HFS_PRI_SECTOR(sectorsize
), sectorsize
, NOCRED
, &bp
);
2134 DBG_ASSERT(bp
!= NULL
);
2135 DBG_ASSERT(bp
->b_data
!= NULL
);
2136 DBG_ASSERT(bp
->b_bcount
== size
);
2139 panic("hfs: standard hfs volumes should not be journaled!\n");
2142 mdb
= (HFSMasterDirectoryBlock
*)(bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
2144 mdb
->drCrDate
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbCrDate
)));
2145 mdb
->drLsMod
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbLsMod
)));
2146 mdb
->drAtrb
= SWAP_BE16 (vcb
->vcbAtrb
);
2147 mdb
->drNmFls
= SWAP_BE16 (vcb
->vcbNmFls
);
2148 mdb
->drAllocPtr
= SWAP_BE16 (vcb
->nextAllocation
);
2149 mdb
->drClpSiz
= SWAP_BE32 (vcb
->vcbClpSiz
);
2150 mdb
->drNxtCNID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2151 mdb
->drFreeBks
= SWAP_BE16 (vcb
->freeBlocks
);
2153 namelen
= strlen(vcb
->vcbVN
);
2154 retval
= utf8_to_hfs(vcb
, namelen
, vcb
->vcbVN
, mdb
->drVN
);
2155 /* Retry with MacRoman in case that's how it was exported. */
2157 retval
= utf8_to_mac_roman(namelen
, vcb
->vcbVN
, mdb
->drVN
);
2159 mdb
->drVolBkUp
= SWAP_BE32 (UTCToLocal(to_hfs_time(vcb
->vcbVolBkUp
)));
2160 mdb
->drWrCnt
= SWAP_BE32 (vcb
->vcbWrCnt
);
2161 mdb
->drNmRtDirs
= SWAP_BE16 (vcb
->vcbNmRtDirs
);
2162 mdb
->drFilCnt
= SWAP_BE32 (vcb
->vcbFilCnt
);
2163 mdb
->drDirCnt
= SWAP_BE32 (vcb
->vcbDirCnt
);
2165 bcopy(vcb
->vcbFndrInfo
, mdb
->drFndrInfo
, sizeof(mdb
->drFndrInfo
));
2167 fp
= VTOF(vcb
->extentsRefNum
);
2168 mdb
->drXTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2169 mdb
->drXTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2170 mdb
->drXTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2171 mdb
->drXTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2172 mdb
->drXTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2173 mdb
->drXTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2174 mdb
->drXTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2175 mdb
->drXTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2177 fp
= VTOF(vcb
->catalogRefNum
);
2178 mdb
->drCTExtRec
[0].startBlock
= SWAP_BE16 (fp
->ff_extents
[0].startBlock
);
2179 mdb
->drCTExtRec
[0].blockCount
= SWAP_BE16 (fp
->ff_extents
[0].blockCount
);
2180 mdb
->drCTExtRec
[1].startBlock
= SWAP_BE16 (fp
->ff_extents
[1].startBlock
);
2181 mdb
->drCTExtRec
[1].blockCount
= SWAP_BE16 (fp
->ff_extents
[1].blockCount
);
2182 mdb
->drCTExtRec
[2].startBlock
= SWAP_BE16 (fp
->ff_extents
[2].startBlock
);
2183 mdb
->drCTExtRec
[2].blockCount
= SWAP_BE16 (fp
->ff_extents
[2].blockCount
);
2184 mdb
->drCTFlSize
= SWAP_BE32 (fp
->ff_blocks
* vcb
->blockSize
);
2185 mdb
->drCTClpSiz
= SWAP_BE32 (fp
->ff_clumpsize
);
2187 /* If requested, flush out the alternate MDB */
2189 struct buf
*alt_bp
= NULL
;
2192 altIDSector
= HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2194 if (meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2195 bcopy(mdb
, alt_bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2197 (void) VOP_BWRITE(alt_bp
);
2202 if (waitfor
!= MNT_WAIT
)
2205 retval
= VOP_BWRITE(bp
);
2207 MarkVCBClean( vcb
);
2215 hfs_flushvolumeheader(struct hfsmount
*hfsmp
, int waitfor
, int altflush
)
2217 ExtendedVCB
*vcb
= HFSTOVCB(hfsmp
);
2218 struct filefork
*fp
;
2219 HFSPlusVolumeHeader
*volumeHeader
;
2227 if (vcb
->vcbSigWord
== kHFSSigWord
)
2228 return hfs_flushMDB(hfsmp
, waitfor
, altflush
);
2232 sectorsize
= hfsmp
->hfs_phys_block_size
;
2233 priIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2234 HFS_PRI_SECTOR(sectorsize
);
2237 hfs_global_shared_lock_acquire(hfsmp
);
2239 if (journal_start_transaction(hfsmp
->jnl
) != 0) {
2240 hfs_global_shared_lock_release(hfsmp
);
2245 retval
= meta_bread(hfsmp
->hfs_devvp
, priIDSector
, sectorsize
, NOCRED
, &bp
);
2251 journal_end_transaction(hfsmp
->jnl
);
2253 hfs_global_shared_lock_release(hfsmp
);
2259 journal_modify_block_start(hfsmp
->jnl
, bp
);
2262 volumeHeader
= (HFSPlusVolumeHeader
*)((char *)bp
->b_data
+ HFS_PRI_OFFSET(sectorsize
));
2265 * For embedded HFS+ volumes, update create date if it changed
2266 * (ie from a setattrlist call)
2268 if ((vcb
->hfsPlusIOPosOffset
!= 0) &&
2269 (SWAP_BE32 (volumeHeader
->createDate
) != vcb
->localCreateDate
)) {
2271 HFSMasterDirectoryBlock
*mdb
;
2273 retval
= meta_bread(hfsmp
->hfs_devvp
, HFS_PRI_SECTOR(sectorsize
),
2274 sectorsize
, NOCRED
, &bp2
);
2280 mdb
= (HFSMasterDirectoryBlock
*)(bp2
->b_data
+
2281 HFS_PRI_OFFSET(sectorsize
));
2283 if ( SWAP_BE32 (mdb
->drCrDate
) != vcb
->localCreateDate
)
2287 journal_modify_block_start(hfsmp
->jnl
, bp2
);
2290 mdb
->drCrDate
= SWAP_BE32 (vcb
->localCreateDate
); /* pick up the new create date */
2294 journal_modify_block_end(hfsmp
->jnl
, bp2
);
2296 (void) VOP_BWRITE(bp2
); /* write out the changes */
2301 brelse(bp2
); /* just release it */
2306 // XXXdbg - only monkey around with the volume signature on non-root volumes
2310 hfsmp
->hfs_fs_ronly
== 0 &&
2311 (HFSTOVFS(hfsmp
)->mnt_flag
& MNT_ROOTFS
) == 0) {
2313 int old_sig
= volumeHeader
->signature
;
2315 if (vcb
->vcbAtrb
& kHFSVolumeUnmountedMask
) {
2316 volumeHeader
->signature
= kHFSPlusSigWord
;
2318 volumeHeader
->signature
= kHFSJSigWord
;
2321 if (old_sig
!= volumeHeader
->signature
) {
2328 /* Note: only update the lower 16 bits worth of attributes */
2329 volumeHeader
->attributes
= SWAP_BE32 ((SWAP_BE32 (volumeHeader
->attributes
) & 0xFFFF0000) + (UInt16
) vcb
->vcbAtrb
);
2330 volumeHeader
->journalInfoBlock
= SWAP_BE32(vcb
->vcbJinfoBlock
);
2332 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSJMountVersion
);
2334 volumeHeader
->lastMountedVersion
= SWAP_BE32 (kHFSPlusMountVersion
);
2336 volumeHeader
->createDate
= SWAP_BE32 (vcb
->localCreateDate
); /* volume create date is in local time */
2337 volumeHeader
->modifyDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbLsMod
));
2338 volumeHeader
->backupDate
= SWAP_BE32 (to_hfs_time(vcb
->vcbVolBkUp
));
2339 volumeHeader
->fileCount
= SWAP_BE32 (vcb
->vcbFilCnt
);
2340 volumeHeader
->folderCount
= SWAP_BE32 (vcb
->vcbDirCnt
);
2341 volumeHeader
->freeBlocks
= SWAP_BE32 (vcb
->freeBlocks
);
2342 volumeHeader
->nextAllocation
= SWAP_BE32 (vcb
->nextAllocation
);
2343 volumeHeader
->rsrcClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2344 volumeHeader
->dataClumpSize
= SWAP_BE32 (vcb
->vcbClpSiz
);
2345 volumeHeader
->nextCatalogID
= SWAP_BE32 (vcb
->vcbNxtCNID
);
2346 volumeHeader
->writeCount
= SWAP_BE32 (vcb
->vcbWrCnt
);
2347 volumeHeader
->encodingsBitmap
= SWAP_BE64 (vcb
->encodingsBitmap
);
2349 if (bcmp(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
)) != 0)
2351 bcopy(vcb
->vcbFndrInfo
, volumeHeader
->finderInfo
, sizeof(volumeHeader
->finderInfo
));
2353 /* Sync Extents over-flow file meta data */
2354 fp
= VTOF(vcb
->extentsRefNum
);
2355 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2356 volumeHeader
->extentsFile
.extents
[i
].startBlock
=
2357 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2358 volumeHeader
->extentsFile
.extents
[i
].blockCount
=
2359 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2361 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2362 volumeHeader
->extentsFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2363 volumeHeader
->extentsFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2364 volumeHeader
->extentsFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2366 /* Sync Catalog file meta data */
2367 fp
= VTOF(vcb
->catalogRefNum
);
2368 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2369 volumeHeader
->catalogFile
.extents
[i
].startBlock
=
2370 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2371 volumeHeader
->catalogFile
.extents
[i
].blockCount
=
2372 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2374 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2375 volumeHeader
->catalogFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2376 volumeHeader
->catalogFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2377 volumeHeader
->catalogFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2379 /* Sync Allocation file meta data */
2380 fp
= VTOF(vcb
->allocationsRefNum
);
2381 for (i
= 0; i
< kHFSPlusExtentDensity
; i
++) {
2382 volumeHeader
->allocationFile
.extents
[i
].startBlock
=
2383 SWAP_BE32 (fp
->ff_extents
[i
].startBlock
);
2384 volumeHeader
->allocationFile
.extents
[i
].blockCount
=
2385 SWAP_BE32 (fp
->ff_extents
[i
].blockCount
);
2387 FTOC(fp
)->c_flag
&= ~C_MODIFIED
;
2388 volumeHeader
->allocationFile
.logicalSize
= SWAP_BE64 (fp
->ff_size
);
2389 volumeHeader
->allocationFile
.totalBlocks
= SWAP_BE32 (fp
->ff_blocks
);
2390 volumeHeader
->allocationFile
.clumpSize
= SWAP_BE32 (fp
->ff_clumpsize
);
2392 /* If requested, flush out the alternate volume header */
2394 struct buf
*alt_bp
= NULL
;
2397 altIDSector
= (vcb
->hfsPlusIOPosOffset
/ sectorsize
) +
2398 HFS_ALT_SECTOR(sectorsize
, hfsmp
->hfs_phys_block_count
);
2400 if (meta_bread(hfsmp
->hfs_devvp
, altIDSector
, sectorsize
, NOCRED
, &alt_bp
) == 0) {
2402 journal_modify_block_start(hfsmp
->jnl
, alt_bp
);
2405 bcopy(volumeHeader
, alt_bp
->b_data
+ HFS_ALT_OFFSET(sectorsize
), kMDBSize
);
2408 journal_modify_block_end(hfsmp
->jnl
, alt_bp
);
2410 (void) VOP_BWRITE(alt_bp
);
2418 journal_modify_block_end(hfsmp
->jnl
, bp
);
2419 journal_end_transaction(hfsmp
->jnl
);
2421 if (waitfor
!= MNT_WAIT
)
2424 retval
= VOP_BWRITE(bp
);
2425 /* When critical data changes, flush the device cache */
2426 if (critical
&& (retval
== 0)) {
2427 (void) VOP_IOCTL(hfsmp
->hfs_devvp
, DKIOCSYNCHRONIZECACHE
,
2428 NULL
, FWRITE
, NOCRED
, current_proc());
2432 hfs_global_shared_lock_release(hfsmp
);
2434 vcb
->vcbFlags
&= 0x00FF;
2440 * hfs vfs operations.
2442 struct vfsops hfs_vfsops
= {