]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_vfsops.c
xnu-1228.9.59.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_vfsops.c
CommitLineData
1c79356b 1/*
4a3eedf9 2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
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.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
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.
52 *
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
63 * SUCH DAMAGE.
64 *
65 * hfs_vfsops.c
66 * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
67 *
9bccf70c 68 * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
69 *
70 * hfs_vfsops.c -- VFS layer for loadable HFS file system.
71 *
1c79356b
A
72 */
73#include <sys/param.h>
74#include <sys/systm.h>
91447636 75#include <sys/kauth.h>
1c79356b
A
76
77#include <sys/ubc.h>
cf7d32b8 78#include <sys/ubc_internal.h>
91447636
A
79#include <sys/vnode_internal.h>
80#include <sys/mount_internal.h>
55e303ae 81#include <sys/sysctl.h>
1c79356b
A
82#include <sys/malloc.h>
83#include <sys/stat.h>
9bccf70c
A
84#include <sys/quota.h>
85#include <sys/disk.h>
55e303ae
A
86#include <sys/paths.h>
87#include <sys/utfconv.h>
91447636 88#include <sys/kdebug.h>
2d21ac55 89#include <sys/fslog.h>
91447636
A
90
91#include <kern/locks.h>
9bccf70c 92
b4c24cb9
A
93#include <vfs/vfs_journal.h>
94
1c79356b
A
95#include <miscfs/specfs/specdev.h>
96#include <hfs/hfs_mount.h>
97
98#include "hfs.h"
9bccf70c
A
99#include "hfs_catalog.h"
100#include "hfs_cnode.h"
1c79356b
A
101#include "hfs_dbg.h"
102#include "hfs_endian.h"
91447636 103#include "hfs_hotfiles.h"
9bccf70c 104#include "hfs_quota.h"
1c79356b
A
105
106#include "hfscommon/headers/FileMgrInternal.h"
107#include "hfscommon/headers/BTreesInternal.h"
108
109#if HFS_DIAGNOSTIC
110int hfs_dbg_all = 0;
1c79356b 111int hfs_dbg_err = 0;
1c79356b
A
112#endif
113
d52fe63f 114
91447636
A
115lck_grp_attr_t * hfs_group_attr;
116lck_attr_t * hfs_lock_attr;
117lck_grp_t * hfs_mutex_group;
118lck_grp_t * hfs_rwlock_group;
119
1c79356b 120extern struct vnodeopv_desc hfs_vnodeop_opv_desc;
935ed37a
A
121/* not static so we can re-use in hfs_readwrite.c for build_path */
122int hfs_vfs_vget(struct mount *mp, ino64_t ino, struct vnode **vpp, vfs_context_t context);
123
1c79356b 124
91447636
A
125static int hfs_changefs(struct mount *mp, struct hfs_mount_args *args);
126static int hfs_fhtovp(struct mount *mp, int fhlen, unsigned char *fhp, struct vnode **vpp, vfs_context_t context);
127static int hfs_flushfiles(struct mount *, int, struct proc *);
128static int hfs_flushMDB(struct hfsmount *hfsmp, int waitfor, int altflush);
129static int hfs_getmountpoint(struct vnode *vp, struct hfsmount **hfsmpp);
130static int hfs_init(struct vfsconf *vfsp);
131static int hfs_mount(struct mount *mp, vnode_t devvp, user_addr_t data, vfs_context_t context);
2d21ac55
A
132static int hfs_mountfs(struct vnode *devvp, struct mount *mp, struct hfs_mount_args *args, int journal_replay_only, vfs_context_t context);
133static int hfs_reload(struct mount *mp);
91447636
A
134static int hfs_vfs_root(struct mount *mp, struct vnode **vpp, vfs_context_t context);
135static int hfs_quotactl(struct mount *, int, uid_t, caddr_t, vfs_context_t context);
136static int hfs_start(struct mount *mp, int flags, vfs_context_t context);
137static int hfs_statfs(struct mount *mp, register struct vfsstatfs *sbp, vfs_context_t context);
138static int hfs_sync(struct mount *mp, int waitfor, vfs_context_t context);
139static 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);
141static int hfs_unmount(struct mount *mp, int mntflags, vfs_context_t context);
91447636
A
142static int hfs_vptofh(struct vnode *vp, int *fhlenp, unsigned char *fhp, vfs_context_t context);
143
2d21ac55 144static int hfs_reclaimspace(struct hfsmount *hfsmp, u_long startblk, u_long reclaimblks, vfs_context_t context);
0c530ab8
A
145static int hfs_overlapped_overflow_extents(struct hfsmount *hfsmp, u_int32_t startblk,
146 u_int32_t catblks, u_int32_t fileID, int rsrcfork);
2d21ac55 147static int hfs_journal_replay(const char *devnode, vfs_context_t context);
1c79356b
A
148
149
150/*
151 * Called by vfs_mountroot when mounting HFS Plus as root.
152 */
91447636 153
55e303ae 154__private_extern__
1c79356b 155int
91447636 156hfs_mountroot(mount_t mp, vnode_t rvp, vfs_context_t context)
1c79356b 157{
1c79356b 158 struct hfsmount *hfsmp;
9bccf70c 159 ExtendedVCB *vcb;
91447636 160 struct vfsstatfs *vfsp;
1c79356b 161 int error;
2d21ac55
A
162
163 hfs_chashinit_finish();
1c79356b 164
2d21ac55 165 if ((error = hfs_mountfs(rvp, mp, NULL, 0, context)))
1c79356b 166 return (error);
55e303ae 167
1c79356b
A
168 /* Init hfsmp */
169 hfsmp = VFSTOHFS(mp);
170
0b4e3aa0
A
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 */
1c79356b 175
9bccf70c
A
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);
180
91447636
A
181 vfsp = vfs_statfs(mp);
182 (void)hfs_statfs(mp, vfsp, NULL);
183
1c79356b
A
184 return (0);
185}
186
187
188/*
189 * VFS Operations.
190 *
191 * mount system call
192 */
193
9bccf70c 194static int
91447636 195hfs_mount(struct mount *mp, vnode_t devvp, user_addr_t data, vfs_context_t context)
1c79356b 196{
91447636 197 struct proc *p = vfs_context_proc(context);
1c79356b 198 struct hfsmount *hfsmp = NULL;
1c79356b 199 struct hfs_mount_args args;
1c79356b 200 int retval = E_NONE;
2d21ac55 201 u_int32_t cmdflags;
1c79356b 202
91447636
A
203 if ((retval = copyin(data, (caddr_t)&args, sizeof(args)))) {
204 return (retval);
205 }
2d21ac55 206 cmdflags = (u_int32_t)vfs_flags(mp) & MNT_CMDFLAGS;
91447636 207 if (cmdflags & MNT_UPDATE) {
1c79356b 208 hfsmp = VFSTOHFS(mp);
91447636
A
209
210 /* Reload incore data after an fsck. */
211 if (cmdflags & MNT_RELOAD) {
212 if (vfs_isrdonly(mp))
2d21ac55 213 return hfs_reload(mp);
91447636
A
214 else
215 return (EINVAL);
216 }
217
218 /* Change to a read-only file system. */
55e303ae 219 if (((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) &&
91447636
A
220 vfs_isrdonly(mp)) {
221 int flags;
222
1c79356b 223 /* use VFS_SYNC to push out System (btree) files */
91447636
A
224 retval = VFS_SYNC(mp, MNT_WAIT, context);
225 if (retval && ((cmdflags & MNT_FORCE) == 0))
226 goto out;
1c79356b
A
227
228 flags = WRITECLOSE;
91447636 229 if (cmdflags & MNT_FORCE)
1c79356b
A
230 flags |= FORCECLOSE;
231
9bccf70c 232 if ((retval = hfs_flushfiles(mp, flags, p)))
91447636 233 goto out;
593a1d5f
A
234
235 /* mark the volume cleanly unmounted */
236 hfsmp->vcbAtrb |= kHFSVolumeUnmountedMask;
9bccf70c 237 retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0);
593a1d5f 238 hfsmp->hfs_flags |= HFS_READ_ONLY;
1c79356b
A
239
240 /* also get the volume bitmap blocks */
91447636
A
241 if (!retval) {
242 if (vnode_mount(hfsmp->hfs_devvp) == mp) {
243 retval = hfs_fsync(hfsmp->hfs_devvp, MNT_WAIT, 0, p);
244 } else {
245 vnode_get(hfsmp->hfs_devvp);
246 retval = VNOP_FSYNC(hfsmp->hfs_devvp, MNT_WAIT, context);
247 vnode_put(hfsmp->hfs_devvp);
248 }
249 }
1c79356b 250 if (retval) {
55e303ae 251 hfsmp->hfs_flags &= ~HFS_READ_ONLY;
91447636 252 goto out;
1c79356b 253 }
55e303ae
A
254 if (hfsmp->jnl) {
255 hfs_global_exclusive_lock_acquire(hfsmp);
256
257 journal_close(hfsmp->jnl);
258 hfsmp->jnl = NULL;
259
260 // Note: we explicitly don't want to shutdown
261 // access to the jvp because we may need
262 // it later if we go back to being read-write.
263
264 hfs_global_exclusive_lock_release(hfsmp);
265 }
1c79356b
A
266 }
267
91447636
A
268 /* Change to a writable file system. */
269 if (vfs_iswriteupgrade(mp)) {
2d21ac55
A
270
271 /*
272 * On inconsistent disks, do not allow read-write mount
273 * unless it is the boot volume being mounted.
274 */
275 if (!(vfs_flags(mp) & MNT_ROOTFS) &&
276 (hfsmp->vcbAtrb & kHFSVolumeInconsistentMask)) {
277 retval = EINVAL;
278 goto out;
279 }
280
55e303ae
A
281 // If the journal was shut-down previously because we were
282 // asked to be read-only, let's start it back up again now
283
284 if ( (HFSTOVCB(hfsmp)->vcbAtrb & kHFSVolumeJournaledMask)
285 && hfsmp->jnl == NULL
286 && hfsmp->jvp != NULL) {
91447636 287 int jflags;
55e303ae
A
288
289 if (hfsmp->hfs_flags & HFS_NEED_JNL_RESET) {
91447636 290 jflags = JOURNAL_RESET;
55e303ae 291 } else {
91447636 292 jflags = 0;
55e303ae
A
293 }
294
295 hfs_global_exclusive_lock_acquire(hfsmp);
296
297 hfsmp->jnl = journal_open(hfsmp->jvp,
298 (hfsmp->jnl_start * HFSTOVCB(hfsmp)->blockSize) + (off_t)HFSTOVCB(hfsmp)->hfsPlusIOPosOffset,
299 hfsmp->jnl_size,
300 hfsmp->hfs_devvp,
593a1d5f 301 hfsmp->hfs_logical_block_size,
91447636 302 jflags,
55e303ae
A
303 0,
304 hfs_sync_metadata, hfsmp->hfs_mp);
305
306 hfs_global_exclusive_lock_release(hfsmp);
307
308 if (hfsmp->jnl == NULL) {
309 retval = EINVAL;
91447636 310 goto out;
55e303ae
A
311 } else {
312 hfsmp->hfs_flags &= ~HFS_NEED_JNL_RESET;
313 }
314
315 }
316
317 /* Only clear HFS_READ_ONLY after a successfull write */
318 hfsmp->hfs_flags &= ~HFS_READ_ONLY;
1c79356b 319
593a1d5f
A
320 /* mark the volume dirty (clear clean unmount bit) */
321 hfsmp->vcbAtrb &= ~kHFSVolumeUnmountedMask;
322
323 retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0);
324 if (retval != E_NONE)
325 goto out;
326
327 if (!(hfsmp->hfs_flags & (HFS_READ_ONLY | HFS_STANDARD))) {
2d21ac55
A
328 /* Setup private/hidden directories for hardlinks. */
329 hfs_privatedir_init(hfsmp, FILE_HARDLINKS);
330 hfs_privatedir_init(hfsmp, DIR_HARDLINKS);
331
b4c24cb9 332 hfs_remove_orphans(hfsmp);
91447636
A
333
334 /*
335 * Allow hot file clustering if conditions allow.
336 */
337 if (hfsmp->hfs_flags & HFS_METADATA_ZONE) {
338 (void) hfs_recording_init(hfsmp);
339 }
2d21ac55
A
340 /* Force ACLs on HFS+ file systems. */
341 if (vfs_extendedsecurity(HFSTOVFS(hfsmp)) == 0) {
342 vfs_setextendedsecurity(HFSTOVFS(hfsmp));
343 }
55e303ae 344 }
1c79356b
A
345 }
346
91447636
A
347 /* Update file system parameters. */
348 retval = hfs_changefs(mp, &args);
1c79356b 349
91447636 350 } else /* not an update request */ {
1c79356b 351
91447636 352 /* Set the mount flag to indicate that we support volfs */
2d21ac55 353 vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_DOVOLFS));
1c79356b 354
2d21ac55
A
355 hfs_chashinit_finish();
356
357 retval = hfs_mountfs(devvp, mp, &args, 0, context);
1c79356b 358 }
91447636
A
359out:
360 if (retval == 0) {
361 (void)hfs_statfs(mp, vfs_statfs(mp), context);
1c79356b 362 }
91447636
A
363 return (retval);
364}
1c79356b 365
1c79356b 366
91447636
A
367struct hfs_changefs_cargs {
368 struct hfsmount *hfsmp;
369 int namefix;
370 int permfix;
371 int permswitch;
372};
1c79356b 373
91447636
A
374static int
375hfs_changefs_callback(struct vnode *vp, void *cargs)
376{
377 ExtendedVCB *vcb;
378 struct cnode *cp;
379 struct cat_desc cndesc;
380 struct cat_attr cnattr;
381 struct hfs_changefs_cargs *args;
935ed37a
A
382 int lockflags;
383 int error;
1c79356b 384
91447636 385 args = (struct hfs_changefs_cargs *)cargs;
b4c24cb9 386
91447636
A
387 cp = VTOC(vp);
388 vcb = HFSTOVCB(args->hfsmp);
1c79356b 389
935ed37a
A
390 lockflags = hfs_systemfile_lock(args->hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
391 error = cat_lookup(args->hfsmp, &cp->c_desc, 0, &cndesc, &cnattr, NULL, NULL);
392 hfs_systemfile_unlock(args->hfsmp, lockflags);
393 if (error) {
91447636
A
394 /*
395 * If we couldn't find this guy skip to the next one
396 */
397 if (args->namefix)
398 cache_purge(vp);
1c79356b 399
91447636
A
400 return (VNODE_RETURNED);
401 }
402 /*
403 * Get the real uid/gid and perm mask from disk.
404 */
405 if (args->permswitch || args->permfix) {
406 cp->c_uid = cnattr.ca_uid;
407 cp->c_gid = cnattr.ca_gid;
408 cp->c_mode = cnattr.ca_mode;
409 }
410 /*
411 * If we're switching name converters then...
412 * Remove the existing entry from the namei cache.
413 * Update name to one based on new encoder.
414 */
415 if (args->namefix) {
416 cache_purge(vp);
417 replace_desc(cp, &cndesc);
1c79356b 418
91447636 419 if (cndesc.cd_cnid == kHFSRootFolderID) {
2d21ac55 420 strlcpy((char *)vcb->vcbVN, (const char *)cp->c_desc.cd_nameptr, NAME_MAX+1);
91447636
A
421 cp->c_desc.cd_encoding = args->hfsmp->hfs_encoding;
422 }
423 } else {
424 cat_releasedesc(&cndesc);
425 }
426 return (VNODE_RETURNED);
427}
1c79356b 428
9bccf70c
A
429/* Change fs mount parameters */
430static int
91447636 431hfs_changefs(struct mount *mp, struct hfs_mount_args *args)
1c79356b 432{
9bccf70c 433 int retval = 0;
1c79356b
A
434 int namefix, permfix, permswitch;
435 struct hfsmount *hfsmp;
1c79356b 436 ExtendedVCB *vcb;
1c79356b
A
437 hfs_to_unicode_func_t get_unicode_func;
438 unicode_to_hfs_func_t get_hfsname_func;
91447636
A
439 u_long old_encoding = 0;
440 struct hfs_changefs_cargs cargs;
2d21ac55 441 u_int32_t mount_flags;
1c79356b
A
442
443 hfsmp = VFSTOHFS(mp);
444 vcb = HFSTOVCB(hfsmp);
91447636
A
445 mount_flags = (unsigned int)vfs_flags(mp);
446
593a1d5f
A
447 hfsmp->hfs_flags |= HFS_IN_CHANGEFS;
448
55e303ae 449 permswitch = (((hfsmp->hfs_flags & HFS_UNKNOWN_PERMS) &&
91447636 450 ((mount_flags & MNT_UNKNOWNPERMISSIONS) == 0)) ||
55e303ae 451 (((hfsmp->hfs_flags & HFS_UNKNOWN_PERMS) == 0) &&
91447636 452 (mount_flags & MNT_UNKNOWNPERMISSIONS)));
55e303ae 453
0b4e3aa0 454 /* The root filesystem must operate with actual permissions: */
91447636 455 if (permswitch && (mount_flags & MNT_ROOTFS) && (mount_flags & MNT_UNKNOWNPERMISSIONS)) {
2d21ac55 456 vfs_clearflags(mp, (u_int64_t)((unsigned int)MNT_UNKNOWNPERMISSIONS)); /* Just say "No". */
593a1d5f
A
457 retval = EINVAL;
458 goto exit;
55e303ae 459 }
91447636 460 if (mount_flags & MNT_UNKNOWNPERMISSIONS)
55e303ae
A
461 hfsmp->hfs_flags |= HFS_UNKNOWN_PERMS;
462 else
463 hfsmp->hfs_flags &= ~HFS_UNKNOWN_PERMS;
464
465 namefix = permfix = 0;
1c79356b 466
2d21ac55
A
467 /*
468 * Tracking of hot files requires up-to-date access times. So if
469 * access time updates are disabled, we must also disable hot files.
470 */
471 if (mount_flags & MNT_NOATIME) {
472 (void) hfs_recording_suspend(hfsmp);
473 }
474
9bccf70c 475 /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */
1c79356b
A
476 if (args->hfs_timezone.tz_minuteswest != VNOVAL) {
477 gTimeZone = args->hfs_timezone;
478 }
479
9bccf70c 480 /* Change the default uid, gid and/or mask */
1c79356b
A
481 if ((args->hfs_uid != (uid_t)VNOVAL) && (hfsmp->hfs_uid != args->hfs_uid)) {
482 hfsmp->hfs_uid = args->hfs_uid;
91447636 483 if (vcb->vcbSigWord == kHFSPlusSigWord)
9bccf70c 484 ++permfix;
1c79356b
A
485 }
486 if ((args->hfs_gid != (gid_t)VNOVAL) && (hfsmp->hfs_gid != args->hfs_gid)) {
487 hfsmp->hfs_gid = args->hfs_gid;
91447636 488 if (vcb->vcbSigWord == kHFSPlusSigWord)
9bccf70c 489 ++permfix;
1c79356b
A
490 }
491 if (args->hfs_mask != (mode_t)VNOVAL) {
492 if (hfsmp->hfs_dir_mask != (args->hfs_mask & ALLPERMS)) {
493 hfsmp->hfs_dir_mask = args->hfs_mask & ALLPERMS;
494 hfsmp->hfs_file_mask = args->hfs_mask & ALLPERMS;
495 if ((args->flags != VNOVAL) && (args->flags & HFSFSMNT_NOXONFILES))
496 hfsmp->hfs_file_mask = (args->hfs_mask & DEFFILEMODE);
91447636 497 if (vcb->vcbSigWord == kHFSPlusSigWord)
9bccf70c 498 ++permfix;
1c79356b
A
499 }
500 }
501
9bccf70c 502 /* Change the hfs encoding value (hfs only) */
91447636 503 if ((vcb->vcbSigWord == kHFSSigWord) &&
55e303ae 504 (args->hfs_encoding != (u_long)VNOVAL) &&
1c79356b
A
505 (hfsmp->hfs_encoding != args->hfs_encoding)) {
506
507 retval = hfs_getconverter(args->hfs_encoding, &get_unicode_func, &get_hfsname_func);
9bccf70c
A
508 if (retval)
509 goto exit;
1c79356b
A
510
511 /*
512 * Connect the new hfs_get_unicode converter but leave
513 * the old hfs_get_hfsname converter in place so that
514 * we can lookup existing vnodes to get their correctly
515 * encoded names.
516 *
517 * When we're all finished, we can then connect the new
518 * hfs_get_hfsname converter and release our interest
519 * in the old converters.
520 */
521 hfsmp->hfs_get_unicode = get_unicode_func;
9bccf70c
A
522 old_encoding = hfsmp->hfs_encoding;
523 hfsmp->hfs_encoding = args->hfs_encoding;
1c79356b
A
524 ++namefix;
525 }
526
9bccf70c
A
527 if (!(namefix || permfix || permswitch))
528 goto exit;
1c79356b 529
91447636
A
530 /* XXX 3762912 hack to support HFS filesystem 'owner' */
531 if (permfix)
532 vfs_setowner(mp,
533 hfsmp->hfs_uid == UNKNOWNUID ? KAUTH_UID_NONE : hfsmp->hfs_uid,
534 hfsmp->hfs_gid == UNKNOWNGID ? KAUTH_GID_NONE : hfsmp->hfs_gid);
535
1c79356b
A
536 /*
537 * For each active vnode fix things that changed
538 *
539 * Note that we can visit a vnode more than once
540 * and we can race with fsync.
91447636
A
541 *
542 * hfs_changefs_callback will be called for each vnode
543 * hung off of this mount point
935ed37a
A
544 *
545 * The vnode will be properly referenced and unreferenced
546 * around the callback
1c79356b 547 */
91447636
A
548 cargs.hfsmp = hfsmp;
549 cargs.namefix = namefix;
550 cargs.permfix = permfix;
551 cargs.permswitch = permswitch;
1c79356b 552
91447636 553 vnode_iterate(mp, 0, hfs_changefs_callback, (void *)&cargs);
1c79356b 554
1c79356b
A
555 /*
556 * If we're switching name converters we can now
557 * connect the new hfs_get_hfsname converter and
558 * release our interest in the old converters.
559 */
560 if (namefix) {
1c79356b 561 hfsmp->hfs_get_hfsname = get_hfsname_func;
1c79356b 562 vcb->volumeNameEncodingHint = args->hfs_encoding;
1c79356b
A
563 (void) hfs_relconverter(old_encoding);
564 }
9bccf70c 565exit:
593a1d5f 566 hfsmp->hfs_flags &= ~HFS_IN_CHANGEFS;
1c79356b
A
567 return (retval);
568}
569
570
91447636
A
571struct hfs_reload_cargs {
572 struct hfsmount *hfsmp;
91447636
A
573 int error;
574};
575
576static int
577hfs_reload_callback(struct vnode *vp, void *cargs)
578{
579 struct cnode *cp;
580 struct hfs_reload_cargs *args;
935ed37a 581 int lockflags;
91447636
A
582
583 args = (struct hfs_reload_cargs *)cargs;
584 /*
585 * flush all the buffers associated with this node
586 */
587 (void) buf_invalidateblks(vp, 0, 0, 0);
588
589 cp = VTOC(vp);
590 /*
591 * Remove any directory hints
592 */
593 if (vnode_isdir(vp))
594 hfs_reldirhints(cp, 0);
595
596 /*
597 * Re-read cnode data for all active vnodes (non-metadata files).
598 */
743b1565 599 if (!vnode_issystem(vp) && !VNODE_IS_RSRC(vp)) {
91447636
A
600 struct cat_fork *datafork;
601 struct cat_desc desc;
602
603 datafork = cp->c_datafork ? &cp->c_datafork->ff_data : NULL;
604
605 /* lookup by fileID since name could have changed */
935ed37a
A
606 lockflags = hfs_systemfile_lock(args->hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
607 args->error = cat_idlookup(args->hfsmp, cp->c_fileid, 0, &desc, &cp->c_attr, datafork);
608 hfs_systemfile_unlock(args->hfsmp, lockflags);
609 if (args->error) {
91447636 610 return (VNODE_RETURNED_DONE);
935ed37a 611 }
91447636
A
612
613 /* update cnode's catalog descriptor */
614 (void) replace_desc(cp, &desc);
615 }
616 return (VNODE_RETURNED);
617}
618
1c79356b
A
619/*
620 * Reload all incore data for a filesystem (used after running fsck on
621 * the root filesystem and finding things to fix). The filesystem must
622 * be mounted read-only.
623 *
624 * Things to do to update the mount:
9bccf70c
A
625 * invalidate all cached meta-data.
626 * invalidate all inactive vnodes.
627 * invalidate all cached file data.
628 * re-read volume header from disk.
629 * re-load meta-file info (extents, file size).
630 * re-load B-tree header data.
631 * re-read cnode data for all active vnodes.
1c79356b 632 */
9bccf70c 633static int
2d21ac55 634hfs_reload(struct mount *mountp)
1c79356b 635{
91447636 636 register struct vnode *devvp;
1c79356b 637 struct buf *bp;
d52fe63f 638 int error, i;
1c79356b
A
639 struct hfsmount *hfsmp;
640 struct HFSPlusVolumeHeader *vhp;
641 ExtendedVCB *vcb;
9bccf70c
A
642 struct filefork *forkp;
643 struct cat_desc cndesc;
91447636 644 struct hfs_reload_cargs args;
593a1d5f 645 daddr64_t priIDSector;
1c79356b
A
646
647 hfsmp = VFSTOHFS(mountp);
648 vcb = HFSTOVCB(hfsmp);
649
650 if (vcb->vcbSigWord == kHFSSigWord)
651 return (EINVAL); /* rooting from HFS is not supported! */
652
653 /*
654 * Invalidate all cached meta-data.
655 */
656 devvp = hfsmp->hfs_devvp;
91447636 657 if (buf_invalidateblks(devvp, 0, 0, 0))
1c79356b 658 panic("hfs_reload: dirty1");
9bccf70c 659
91447636 660 args.hfsmp = hfsmp;
91447636
A
661 args.error = 0;
662 /*
663 * hfs_reload_callback will be called for each vnode
664 * hung off of this mount point that can't be recycled...
665 * vnode_iterate will recycle those that it can (the VNODE_RELOAD option)
666 * the vnode will be in an 'unbusy' state (VNODE_WAIT) and
667 * properly referenced and unreferenced around the callback
668 */
91447636 669 vnode_iterate(mountp, VNODE_RELOAD | VNODE_WAIT, hfs_reload_callback, (void *)&args);
9bccf70c 670
91447636
A
671 if (args.error)
672 return (args.error);
9bccf70c 673
1c79356b
A
674 /*
675 * Re-read VolumeHeader from disk.
676 */
593a1d5f
A
677 priIDSector = (daddr64_t)((vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) +
678 HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size));
d52fe63f 679
91447636 680 error = (int)buf_meta_bread(hfsmp->hfs_devvp,
593a1d5f
A
681 HFS_PHYSBLK_ROUNDDOWN(priIDSector, hfsmp->hfs_log_per_phys),
682 hfsmp->hfs_physical_block_size, NOCRED, &bp);
1c79356b
A
683 if (error) {
684 if (bp != NULL)
91447636 685 buf_brelse(bp);
1c79356b
A
686 return (error);
687 }
688
593a1d5f 689 vhp = (HFSPlusVolumeHeader *) (buf_dataptr(bp) + HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size));
1c79356b 690
9bccf70c 691 /* Do a quick sanity check */
55e303ae
A
692 if ((SWAP_BE16(vhp->signature) != kHFSPlusSigWord &&
693 SWAP_BE16(vhp->signature) != kHFSXSigWord) ||
694 (SWAP_BE16(vhp->version) != kHFSPlusVersion &&
695 SWAP_BE16(vhp->version) != kHFSXVersion) ||
9bccf70c 696 SWAP_BE32(vhp->blockSize) != vcb->blockSize) {
91447636 697 buf_brelse(bp);
9bccf70c 698 return (EIO);
1c79356b
A
699 }
700
9bccf70c 701 vcb->vcbLsMod = to_bsd_time(SWAP_BE32(vhp->modifyDate));
91447636 702 vcb->vcbAtrb = SWAP_BE32 (vhp->attributes);
b4c24cb9 703 vcb->vcbJinfoBlock = SWAP_BE32(vhp->journalInfoBlock);
9bccf70c
A
704 vcb->vcbClpSiz = SWAP_BE32 (vhp->rsrcClumpSize);
705 vcb->vcbNxtCNID = SWAP_BE32 (vhp->nextCatalogID);
706 vcb->vcbVolBkUp = to_bsd_time(SWAP_BE32(vhp->backupDate));
707 vcb->vcbWrCnt = SWAP_BE32 (vhp->writeCount);
708 vcb->vcbFilCnt = SWAP_BE32 (vhp->fileCount);
709 vcb->vcbDirCnt = SWAP_BE32 (vhp->folderCount);
2d21ac55 710 HFS_UPDATE_NEXT_ALLOCATION(vcb, SWAP_BE32 (vhp->nextAllocation));
9bccf70c
A
711 vcb->totalBlocks = SWAP_BE32 (vhp->totalBlocks);
712 vcb->freeBlocks = SWAP_BE32 (vhp->freeBlocks);
1c79356b
A
713 vcb->encodingsBitmap = SWAP_BE64 (vhp->encodingsBitmap);
714 bcopy(vhp->finderInfo, vcb->vcbFndrInfo, sizeof(vhp->finderInfo));
715 vcb->localCreateDate = SWAP_BE32 (vhp->createDate); /* hfs+ create date is in local time */
716
717 /*
718 * Re-load meta-file vnode data (extent info, file size, etc).
719 */
9bccf70c
A
720 forkp = VTOF((struct vnode *)vcb->extentsRefNum);
721 for (i = 0; i < kHFSPlusExtentDensity; i++) {
722 forkp->ff_extents[i].startBlock =
723 SWAP_BE32 (vhp->extentsFile.extents[i].startBlock);
724 forkp->ff_extents[i].blockCount =
725 SWAP_BE32 (vhp->extentsFile.extents[i].blockCount);
726 }
727 forkp->ff_size = SWAP_BE64 (vhp->extentsFile.logicalSize);
728 forkp->ff_blocks = SWAP_BE32 (vhp->extentsFile.totalBlocks);
729 forkp->ff_clumpsize = SWAP_BE32 (vhp->extentsFile.clumpSize);
730
731
732 forkp = VTOF((struct vnode *)vcb->catalogRefNum);
733 for (i = 0; i < kHFSPlusExtentDensity; i++) {
734 forkp->ff_extents[i].startBlock =
735 SWAP_BE32 (vhp->catalogFile.extents[i].startBlock);
736 forkp->ff_extents[i].blockCount =
737 SWAP_BE32 (vhp->catalogFile.extents[i].blockCount);
738 }
739 forkp->ff_size = SWAP_BE64 (vhp->catalogFile.logicalSize);
740 forkp->ff_blocks = SWAP_BE32 (vhp->catalogFile.totalBlocks);
741 forkp->ff_clumpsize = SWAP_BE32 (vhp->catalogFile.clumpSize);
742
91447636
A
743 if (hfsmp->hfs_attribute_vp) {
744 forkp = VTOF(hfsmp->hfs_attribute_vp);
745 for (i = 0; i < kHFSPlusExtentDensity; i++) {
746 forkp->ff_extents[i].startBlock =
747 SWAP_BE32 (vhp->attributesFile.extents[i].startBlock);
748 forkp->ff_extents[i].blockCount =
749 SWAP_BE32 (vhp->attributesFile.extents[i].blockCount);
750 }
751 forkp->ff_size = SWAP_BE64 (vhp->attributesFile.logicalSize);
752 forkp->ff_blocks = SWAP_BE32 (vhp->attributesFile.totalBlocks);
753 forkp->ff_clumpsize = SWAP_BE32 (vhp->attributesFile.clumpSize);
754 }
9bccf70c
A
755
756 forkp = VTOF((struct vnode *)vcb->allocationsRefNum);
757 for (i = 0; i < kHFSPlusExtentDensity; i++) {
758 forkp->ff_extents[i].startBlock =
759 SWAP_BE32 (vhp->allocationFile.extents[i].startBlock);
760 forkp->ff_extents[i].blockCount =
761 SWAP_BE32 (vhp->allocationFile.extents[i].blockCount);
762 }
763 forkp->ff_size = SWAP_BE64 (vhp->allocationFile.logicalSize);
764 forkp->ff_blocks = SWAP_BE32 (vhp->allocationFile.totalBlocks);
765 forkp->ff_clumpsize = SWAP_BE32 (vhp->allocationFile.clumpSize);
1c79356b 766
91447636 767 buf_brelse(bp);
1c79356b
A
768 vhp = NULL;
769
770 /*
771 * Re-load B-tree header data
772 */
9bccf70c 773 forkp = VTOF((struct vnode *)vcb->extentsRefNum);
91447636 774 if ( (error = MacToVFSError( BTReloadData((FCB*)forkp) )) )
1c79356b
A
775 return (error);
776
9bccf70c 777 forkp = VTOF((struct vnode *)vcb->catalogRefNum);
91447636 778 if ( (error = MacToVFSError( BTReloadData((FCB*)forkp) )) )
1c79356b
A
779 return (error);
780
91447636
A
781 if (hfsmp->hfs_attribute_vp) {
782 forkp = VTOF(hfsmp->hfs_attribute_vp);
783 if ( (error = MacToVFSError( BTReloadData((FCB*)forkp) )) )
784 return (error);
785 }
786
9bccf70c 787 /* Reload the volume name */
2d21ac55 788 if ((error = cat_idlookup(hfsmp, kHFSRootFolderID, 0, &cndesc, NULL, NULL)))
1c79356b 789 return (error);
9bccf70c
A
790 vcb->volumeNameEncodingHint = cndesc.cd_encoding;
791 bcopy(cndesc.cd_nameptr, vcb->vcbVN, min(255, cndesc.cd_namelen));
792 cat_releasedesc(&cndesc);
1c79356b 793
2d21ac55
A
794 /* Re-establish private/hidden directories. */
795 hfs_privatedir_init(hfsmp, FILE_HARDLINKS);
796 hfs_privatedir_init(hfsmp, DIR_HARDLINKS);
1c79356b 797
55e303ae
A
798 /* In case any volume information changed to trigger a notification */
799 hfs_generate_volume_notifications(hfsmp);
800
1c79356b
A
801 return (0);
802}
803
804
805/*
806 * Common code for mount and mountroot
807 */
9bccf70c 808static int
91447636 809hfs_mountfs(struct vnode *devvp, struct mount *mp, struct hfs_mount_args *args,
2d21ac55 810 int journal_replay_only, vfs_context_t context)
1c79356b 811{
91447636 812 struct proc *p = vfs_context_proc(context);
9bccf70c
A
813 int retval = E_NONE;
814 struct hfsmount *hfsmp;
815 struct buf *bp;
816 dev_t dev;
817 HFSMasterDirectoryBlock *mdbp;
818 int ronly;
2d21ac55 819#if QUOTA
9bccf70c 820 int i;
2d21ac55 821#endif
9bccf70c 822 int mntwrapper;
91447636 823 kauth_cred_t cred;
d52fe63f 824 u_int64_t disksize;
593a1d5f
A
825 daddr64_t log_blkcnt;
826 u_int32_t log_blksize;
827 u_int32_t phys_blksize;
d52fe63f 828 u_int32_t minblksize;
9bccf70c 829 u_int32_t iswritable;
91447636 830 daddr64_t mdb_offset;
2d21ac55 831 int isvirtual = 0;
1c79356b 832
91447636
A
833 ronly = vfs_isrdonly(mp);
834 dev = vnode_specrdev(devvp);
835 cred = p ? vfs_context_ucred(context) : NOCRED;
9bccf70c 836 mntwrapper = 0;
1c79356b 837
d52fe63f
A
838 bp = NULL;
839 hfsmp = NULL;
9bccf70c 840 mdbp = NULL;
d52fe63f 841 minblksize = kHFSBlockSize;
1c79356b 842
91447636
A
843 /* Advisory locking should be handled at the VFS layer */
844 vfs_setlocklocal(mp);
845
593a1d5f
A
846 /* Get the logical block size (treated as physical block size everywhere) */
847 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&log_blksize, 0, context)) {
d52fe63f
A
848 retval = ENXIO;
849 goto error_exit;
850 }
593a1d5f
A
851 /* Get the physical block size. */
852 retval = VNOP_IOCTL(devvp, DKIOCGETPHYSICALBLOCKSIZE, (caddr_t)&phys_blksize, 0, context);
853 if (retval) {
854 if ((retval != ENOTSUP) && (retval != ENOTTY)) {
855 retval = ENXIO;
856 goto error_exit;
857 }
858 /* If device does not support this ioctl, assume that physical
859 * block size is same as logical block size
860 */
861 phys_blksize = log_blksize;
862 }
d52fe63f 863 /* Switch to 512 byte sectors (temporarily) */
593a1d5f 864 if (log_blksize > 512) {
d52fe63f
A
865 u_int32_t size512 = 512;
866
91447636 867 if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, context)) {
d52fe63f
A
868 retval = ENXIO;
869 goto error_exit;
870 }
871 }
872 /* Get the number of 512 byte physical blocks. */
593a1d5f 873 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) {
2d21ac55 874 /* resetting block size may fail if getting block count did */
593a1d5f 875 (void)VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context);
2d21ac55 876
d52fe63f
A
877 retval = ENXIO;
878 goto error_exit;
879 }
880 /* Compute an accurate disk size (i.e. within 512 bytes) */
593a1d5f 881 disksize = (u_int64_t)log_blkcnt * (u_int64_t)512;
1c79356b 882
d52fe63f 883 /*
91447636
A
884 * On Tiger it is not necessary to switch the device
885 * block size to be 4k if there are more than 31-bits
886 * worth of blocks but to insure compatibility with
887 * pre-Tiger systems we have to do it.
1c79356b 888 */
593a1d5f
A
889 if (log_blkcnt > 0x000000007fffffff) {
890 minblksize = log_blksize = 4096;
891 if (phys_blksize < log_blksize)
892 phys_blksize = log_blksize;
d52fe63f 893 }
91447636 894
2d21ac55 895 /* Now switch to our preferred physical block size. */
593a1d5f
A
896 if (log_blksize > 512) {
897 if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) {
d52fe63f
A
898 retval = ENXIO;
899 goto error_exit;
900 }
901 /* Get the count of physical blocks. */
593a1d5f 902 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) {
d52fe63f
A
903 retval = ENXIO;
904 goto error_exit;
905 }
906 }
d52fe63f
A
907 /*
908 * At this point:
909 * minblksize is the minimum physical block size
593a1d5f
A
910 * log_blksize has our preferred physical block size
911 * log_blkcnt has the total number of physical blocks
1c79356b 912 */
0b4e3aa0 913
593a1d5f
A
914 mdb_offset = (daddr64_t)HFS_PRI_SECTOR(log_blksize);
915 if ((retval = (int)buf_meta_bread(devvp,
916 HFS_PHYSBLK_ROUNDDOWN(mdb_offset, (phys_blksize/log_blksize)),
917 phys_blksize, cred, &bp))) {
d52fe63f
A
918 goto error_exit;
919 }
9bccf70c 920 MALLOC(mdbp, HFSMasterDirectoryBlock *, kMDBSize, M_TEMP, M_WAITOK);
593a1d5f 921 bcopy((char *)buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize), mdbp, kMDBSize);
91447636 922 buf_brelse(bp);
9bccf70c 923 bp = NULL;
1c79356b 924
d52fe63f
A
925 MALLOC(hfsmp, struct hfsmount *, sizeof(struct hfsmount), M_HFSMNT, M_WAITOK);
926 bzero(hfsmp, sizeof(struct hfsmount));
b4c24cb9 927
9bccf70c 928 /*
91447636
A
929 * Init the volume information structure
930 */
931
932 lck_mtx_init(&hfsmp->hfs_mutex, hfs_mutex_group, hfs_lock_attr);
933 lck_mtx_init(&hfsmp->hfc_mutex, hfs_mutex_group, hfs_lock_attr);
934 lck_rw_init(&hfsmp->hfs_global_lock, hfs_rwlock_group, hfs_lock_attr);
3a60a9f5 935 lck_rw_init(&hfsmp->hfs_insync, hfs_rwlock_group, hfs_lock_attr);
91447636
A
936
937 vfs_setfsprivate(mp, hfsmp);
9bccf70c 938 hfsmp->hfs_mp = mp; /* Make VFSTOHFS work */
91447636 939 hfsmp->hfs_raw_dev = vnode_specrdev(devvp);
9bccf70c 940 hfsmp->hfs_devvp = devvp;
2d21ac55 941 vnode_ref(devvp); /* Hold a ref on the device, dropped when hfsmp is freed. */
593a1d5f
A
942 hfsmp->hfs_logical_block_size = log_blksize;
943 hfsmp->hfs_logical_block_count = log_blkcnt;
944 hfsmp->hfs_physical_block_size = phys_blksize;
945 hfsmp->hfs_log_per_phys = (phys_blksize / log_blksize);
55e303ae
A
946 hfsmp->hfs_flags |= HFS_WRITEABLE_MEDIA;
947 if (ronly)
948 hfsmp->hfs_flags |= HFS_READ_ONLY;
91447636 949 if (((unsigned int)vfs_flags(mp)) & MNT_UNKNOWNPERMISSIONS)
55e303ae 950 hfsmp->hfs_flags |= HFS_UNKNOWN_PERMS;
2d21ac55
A
951
952#if QUOTA
9bccf70c 953 for (i = 0; i < MAXQUOTAS; i++)
91447636 954 dqfileinit(&hfsmp->hfs_qfiles[i]);
2d21ac55 955#endif
9bccf70c 956
1c79356b
A
957 if (args) {
958 hfsmp->hfs_uid = (args->hfs_uid == (uid_t)VNOVAL) ? UNKNOWNUID : args->hfs_uid;
959 if (hfsmp->hfs_uid == 0xfffffffd) hfsmp->hfs_uid = UNKNOWNUID;
960 hfsmp->hfs_gid = (args->hfs_gid == (gid_t)VNOVAL) ? UNKNOWNGID : args->hfs_gid;
961 if (hfsmp->hfs_gid == 0xfffffffd) hfsmp->hfs_gid = UNKNOWNGID;
91447636 962 vfs_setowner(mp, hfsmp->hfs_uid, hfsmp->hfs_gid); /* tell the VFS */
1c79356b
A
963 if (args->hfs_mask != (mode_t)VNOVAL) {
964 hfsmp->hfs_dir_mask = args->hfs_mask & ALLPERMS;
965 if (args->flags & HFSFSMNT_NOXONFILES) {
966 hfsmp->hfs_file_mask = (args->hfs_mask & DEFFILEMODE);
967 } else {
968 hfsmp->hfs_file_mask = args->hfs_mask & ALLPERMS;
969 }
970 } else {
971 hfsmp->hfs_dir_mask = UNKNOWNPERMISSIONS & ALLPERMS; /* 0777: rwx---rwx */
972 hfsmp->hfs_file_mask = UNKNOWNPERMISSIONS & DEFFILEMODE; /* 0666: no --x by default? */
9bccf70c
A
973 }
974 if ((args->flags != (int)VNOVAL) && (args->flags & HFSFSMNT_WRAPPER))
975 mntwrapper = 1;
1c79356b
A
976 } else {
977 /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */
91447636 978 if (((unsigned int)vfs_flags(mp)) & MNT_UNKNOWNPERMISSIONS) {
1c79356b
A
979 hfsmp->hfs_uid = UNKNOWNUID;
980 hfsmp->hfs_gid = UNKNOWNGID;
91447636 981 vfs_setowner(mp, hfsmp->hfs_uid, hfsmp->hfs_gid); /* tell the VFS */
1c79356b
A
982 hfsmp->hfs_dir_mask = UNKNOWNPERMISSIONS & ALLPERMS; /* 0777: rwx---rwx */
983 hfsmp->hfs_file_mask = UNKNOWNPERMISSIONS & DEFFILEMODE; /* 0666: no --x by default? */
9bccf70c
A
984 }
985 }
986
987 /* Find out if disk media is writable. */
91447636 988 if (VNOP_IOCTL(devvp, DKIOCISWRITABLE, (caddr_t)&iswritable, 0, context) == 0) {
9bccf70c 989 if (iswritable)
55e303ae 990 hfsmp->hfs_flags |= HFS_WRITEABLE_MEDIA;
9bccf70c 991 else
55e303ae 992 hfsmp->hfs_flags &= ~HFS_WRITEABLE_MEDIA;
9bccf70c 993 }
1c79356b 994
91447636 995 // record the current time at which we're mounting this volume
2d21ac55
A
996 struct timeval tv;
997 microtime(&tv);
998 hfsmp->hfs_mount_time = tv.tv_sec;
91447636 999
d52fe63f
A
1000 /* Mount a standard HFS disk */
1001 if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) &&
9bccf70c 1002 (mntwrapper || (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord))) {
2d21ac55
A
1003
1004 /* If only journal replay is requested, exit immediately */
1005 if (journal_replay_only) {
1006 retval = 0;
1007 goto error_exit;
1008 }
1009
91447636 1010 if ((vfs_flags(mp) & MNT_ROOTFS)) {
d52fe63f 1011 retval = EINVAL; /* Cannot root from HFS standard disks */
1c79356b 1012 goto error_exit;
d52fe63f
A
1013 }
1014 /* HFS disks can only use 512 byte physical blocks */
593a1d5f
A
1015 if (log_blksize > kHFSBlockSize) {
1016 log_blksize = kHFSBlockSize;
1017 if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) {
d52fe63f
A
1018 retval = ENXIO;
1019 goto error_exit;
1020 }
593a1d5f 1021 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) {
d52fe63f
A
1022 retval = ENXIO;
1023 goto error_exit;
1024 }
593a1d5f
A
1025 hfsmp->hfs_logical_block_size = log_blksize;
1026 hfsmp->hfs_logical_block_count = log_blkcnt;
d52fe63f 1027 }
1c79356b
A
1028 if (args) {
1029 hfsmp->hfs_encoding = args->hfs_encoding;
1030 HFSTOVCB(hfsmp)->volumeNameEncodingHint = args->hfs_encoding;
1031
1c79356b
A
1032 /* establish the timezone */
1033 gTimeZone = args->hfs_timezone;
1034 }
1035
9bccf70c
A
1036 retval = hfs_getconverter(hfsmp->hfs_encoding, &hfsmp->hfs_get_unicode,
1037 &hfsmp->hfs_get_hfsname);
d52fe63f
A
1038 if (retval)
1039 goto error_exit;
1c79356b 1040
d52fe63f 1041 retval = hfs_MountHFSVolume(hfsmp, mdbp, p);
1c79356b
A
1042 if (retval)
1043 (void) hfs_relconverter(hfsmp->hfs_encoding);
1044
d52fe63f
A
1045 } else /* Mount an HFS Plus disk */ {
1046 HFSPlusVolumeHeader *vhp;
1047 off_t embeddedOffset;
b4c24cb9 1048 int jnl_disable = 0;
d52fe63f
A
1049
1050 /* Get the embedded Volume Header */
1051 if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) {
1052 embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * kHFSBlockSize;
1053 embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) *
1054 (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
1055
d52fe63f
A
1056 /*
1057 * If the embedded volume doesn't start on a block
1058 * boundary, then switch the device to a 512-byte
1059 * block size so everything will line up on a block
1060 * boundary.
1061 */
593a1d5f 1062 if ((embeddedOffset % log_blksize) != 0) {
d52fe63f
A
1063 printf("HFS Mount: embedded volume offset not"
1064 " a multiple of physical block size (%d);"
593a1d5f
A
1065 " switching to 512\n", log_blksize);
1066 log_blksize = 512;
91447636 1067 if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE,
593a1d5f 1068 (caddr_t)&log_blksize, FWRITE, context)) {
d52fe63f
A
1069 retval = ENXIO;
1070 goto error_exit;
1071 }
91447636 1072 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT,
593a1d5f 1073 (caddr_t)&log_blkcnt, 0, context)) {
d52fe63f
A
1074 retval = ENXIO;
1075 goto error_exit;
1076 }
d52fe63f 1077 /* Note: relative block count adjustment */
593a1d5f
A
1078 hfsmp->hfs_logical_block_count *=
1079 hfsmp->hfs_logical_block_size / log_blksize;
1080 hfsmp->hfs_logical_block_size = log_blksize;
d52fe63f
A
1081 }
1082
9bccf70c
A
1083 disksize = (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.blockCount) *
1084 (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz);
1085
593a1d5f 1086 hfsmp->hfs_logical_block_count = disksize / log_blksize;
9bccf70c 1087
593a1d5f
A
1088 mdb_offset = (daddr64_t)((embeddedOffset / log_blksize) + HFS_PRI_SECTOR(log_blksize));
1089 retval = (int)buf_meta_bread(devvp, HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys),
1090 phys_blksize, cred, &bp);
d52fe63f
A
1091 if (retval)
1092 goto error_exit;
593a1d5f 1093 bcopy((char *)buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize), mdbp, 512);
91447636 1094 buf_brelse(bp);
9bccf70c
A
1095 bp = NULL;
1096 vhp = (HFSPlusVolumeHeader*) mdbp;
d52fe63f
A
1097
1098 } else /* pure HFS+ */ {
1099 embeddedOffset = 0;
1100 vhp = (HFSPlusVolumeHeader*) mdbp;
1101 }
1102
2d21ac55
A
1103 /*
1104 * On inconsistent disks, do not allow read-write mount
1105 * unless it is the boot volume being mounted.
1106 */
1107 if (!(vfs_flags(mp) & MNT_ROOTFS) &&
1108 (SWAP_BE32(vhp->attributes) & kHFSVolumeInconsistentMask) &&
1109 !(hfsmp->hfs_flags & HFS_READ_ONLY)) {
1110 retval = EINVAL;
1111 goto error_exit;
1112 }
1113
1114
b4c24cb9
A
1115 // XXXdbg
1116 //
1117 hfsmp->jnl = NULL;
1118 hfsmp->jvp = NULL;
2d21ac55
A
1119 if (args != NULL && (args->flags & HFSFSMNT_EXTENDED_ARGS) &&
1120 args->journal_disable) {
b4c24cb9
A
1121 jnl_disable = 1;
1122 }
1123
1124 //
1125 // We only initialize the journal here if the last person
1126 // to mount this volume was journaling aware. Otherwise
1127 // we delay journal initialization until later at the end
1128 // of hfs_MountHFSPlusVolume() because the last person who
1129 // mounted it could have messed things up behind our back
1130 // (so we need to go find the .journal file, make sure it's
1131 // the right size, re-sync up if it was moved, etc).
1132 //
1133 if ( (SWAP_BE32(vhp->lastMountedVersion) == kHFSJMountVersion)
1134 && (SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask)
1135 && !jnl_disable) {
1136
1137 // if we're able to init the journal, mark the mount
1138 // point as journaled.
1139 //
1140 if (hfs_early_journal_init(hfsmp, vhp, args, embeddedOffset, mdb_offset, mdbp, cred) == 0) {
2d21ac55 1141 vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_JOURNALED));
b4c24cb9 1142 } else {
55e303ae
A
1143 // if the journal failed to open, then set the lastMountedVersion
1144 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1145 // of just bailing out because the volume is journaled.
91447636
A
1146 if (!ronly) {
1147 HFSPlusVolumeHeader *jvhp;
55e303ae
A
1148
1149 hfsmp->hfs_flags |= HFS_NEED_JNL_RESET;
1150
1151 if (mdb_offset == 0) {
593a1d5f 1152 mdb_offset = (daddr64_t)((embeddedOffset / log_blksize) + HFS_PRI_SECTOR(log_blksize));
55e303ae
A
1153 }
1154
1155 bp = NULL;
593a1d5f
A
1156 retval = (int)buf_meta_bread(devvp,
1157 HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys),
1158 phys_blksize, cred, &bp);
55e303ae 1159 if (retval == 0) {
593a1d5f 1160 jvhp = (HFSPlusVolumeHeader *)(buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize));
55e303ae 1161
91447636
A
1162 if (SWAP_BE16(jvhp->signature) == kHFSPlusSigWord || SWAP_BE16(jvhp->signature) == kHFSXSigWord) {
1163 printf ("hfs(1): Journal replay fail. Writing lastMountVersion as FSK!\n");
1164 jvhp->lastMountedVersion = SWAP_BE32(kFSKMountVersion);
1165 buf_bwrite(bp);
55e303ae 1166 } else {
91447636 1167 buf_brelse(bp);
55e303ae
A
1168 }
1169 bp = NULL;
1170 } else if (bp) {
91447636
A
1171 buf_brelse(bp);
1172 // clear this so the error exit path won't try to use it
1173 bp = NULL;
55e303ae
A
1174 }
1175 }
1176
1177 // if this isn't the root device just bail out.
91447636 1178 // If it is the root device we just continue on
55e303ae
A
1179 // in the hopes that fsck_hfs will be able to
1180 // fix any damage that exists on the volume.
91447636 1181 if ( !(vfs_flags(mp) & MNT_ROOTFS)) {
55e303ae
A
1182 retval = EINVAL;
1183 goto error_exit;
1184 }
b4c24cb9
A
1185 }
1186 }
1187 // XXXdbg
1188
2d21ac55
A
1189 /* Either the journal is replayed successfully, or there
1190 * was nothing to replay, or no journal exists. In any case,
1191 * return success.
1192 */
1193 if (journal_replay_only) {
1194 retval = 0;
1195 goto error_exit;
1196 }
1197
d52fe63f
A
1198 (void) hfs_getconverter(0, &hfsmp->hfs_get_unicode, &hfsmp->hfs_get_hfsname);
1199
91447636 1200 retval = hfs_MountHFSPlusVolume(hfsmp, vhp, embeddedOffset, disksize, p, args, cred);
d52fe63f
A
1201 /*
1202 * If the backend didn't like our physical blocksize
1203 * then retry with physical blocksize of 512.
1204 */
593a1d5f 1205 if ((retval == ENXIO) && (log_blksize > 512) && (log_blksize != minblksize)) {
d52fe63f 1206 printf("HFS Mount: could not use physical block size "
593a1d5f
A
1207 "(%d) switching to 512\n", log_blksize);
1208 log_blksize = 512;
1209 if (VNOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&log_blksize, FWRITE, context)) {
d52fe63f
A
1210 retval = ENXIO;
1211 goto error_exit;
1212 }
593a1d5f 1213 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&log_blkcnt, 0, context)) {
d52fe63f
A
1214 retval = ENXIO;
1215 goto error_exit;
1216 }
593a1d5f 1217 devvp->v_specsize = log_blksize;
d52fe63f 1218 /* Note: relative block count adjustment (in case this is an embedded volume). */
593a1d5f
A
1219 hfsmp->hfs_logical_block_count *= hfsmp->hfs_logical_block_size / log_blksize;
1220 hfsmp->hfs_logical_block_size = log_blksize;
d52fe63f 1221
55e303ae
A
1222 if (hfsmp->jnl) {
1223 // close and re-open this with the new block size
1224 journal_close(hfsmp->jnl);
1225 hfsmp->jnl = NULL;
1226 if (hfs_early_journal_init(hfsmp, vhp, args, embeddedOffset, mdb_offset, mdbp, cred) == 0) {
2d21ac55 1227 vfs_setflags(mp, (u_int64_t)((unsigned int)MNT_JOURNALED));
91447636
A
1228 } else {
1229 // if the journal failed to open, then set the lastMountedVersion
1230 // to be "FSK!" which fsck_hfs will see and force the fsck instead
1231 // of just bailing out because the volume is journaled.
1232 if (!ronly) {
1233 HFSPlusVolumeHeader *jvhp;
1234
1235 hfsmp->hfs_flags |= HFS_NEED_JNL_RESET;
1236
1237 if (mdb_offset == 0) {
593a1d5f 1238 mdb_offset = (daddr64_t)((embeddedOffset / log_blksize) + HFS_PRI_SECTOR(log_blksize));
91447636
A
1239 }
1240
1241 bp = NULL;
593a1d5f
A
1242 retval = (int)buf_meta_bread(devvp, HFS_PHYSBLK_ROUNDDOWN(mdb_offset, hfsmp->hfs_log_per_phys),
1243 phys_blksize, cred, &bp);
91447636 1244 if (retval == 0) {
593a1d5f 1245 jvhp = (HFSPlusVolumeHeader *)(buf_dataptr(bp) + HFS_PRI_OFFSET(phys_blksize));
91447636
A
1246
1247 if (SWAP_BE16(jvhp->signature) == kHFSPlusSigWord || SWAP_BE16(jvhp->signature) == kHFSXSigWord) {
1248 printf ("hfs(2): Journal replay fail. Writing lastMountVersion as FSK!\n");
1249 jvhp->lastMountedVersion = SWAP_BE32(kFSKMountVersion);
1250 buf_bwrite(bp);
1251 } else {
1252 buf_brelse(bp);
1253 }
1254 bp = NULL;
1255 } else if (bp) {
1256 buf_brelse(bp);
1257 // clear this so the error exit path won't try to use it
1258 bp = NULL;
1259 }
1260 }
1261
1262 // if this isn't the root device just bail out.
1263 // If it is the root device we just continue on
1264 // in the hopes that fsck_hfs will be able to
1265 // fix any damage that exists on the volume.
1266 if ( !(vfs_flags(mp) & MNT_ROOTFS)) {
1267 retval = EINVAL;
1268 goto error_exit;
1269 }
1270 }
55e303ae
A
1271 }
1272
d52fe63f 1273 /* Try again with a smaller block size... */
91447636 1274 retval = hfs_MountHFSPlusVolume(hfsmp, vhp, embeddedOffset, disksize, p, args, cred);
d52fe63f
A
1275 }
1276 if (retval)
1277 (void) hfs_relconverter(0);
1278 }
1c79356b 1279
91447636
A
1280 // save off a snapshot of the mtime from the previous mount
1281 // (for matador).
1282 hfsmp->hfs_last_mounted_mtime = hfsmp->hfs_mtime;
1283
1c79356b
A
1284 if ( retval ) {
1285 goto error_exit;
1286 }
1287
91447636
A
1288 mp->mnt_vfsstat.f_fsid.val[0] = (long)dev;
1289 mp->mnt_vfsstat.f_fsid.val[1] = vfs_typenum(mp);
1290 vfs_setmaxsymlen(mp, 0);
1291 mp->mnt_vtable->vfc_threadsafe = TRUE;
1292 mp->mnt_vtable->vfc_vfsflags |= VFC_VFSNATIVEXATTR;
2d21ac55
A
1293#if NAMEDSTREAMS
1294 mp->mnt_kern_flag |= MNTK_NAMED_STREAMS;
1295#endif
1296 if (!(hfsmp->hfs_flags & HFS_STANDARD)) {
1297 /* Tell VFS that we support directory hard links. */
1298 mp->mnt_vtable->vfc_vfsflags |= VFC_VFSDIRLINKS;
1299 } else {
1300 /* HFS standard doesn't support extended readdir! */
1301 mp->mnt_vtable->vfc_vfsflags &= ~VFC_VFSREADDIR_EXTENDED;
1302 }
1c79356b 1303
55e303ae
A
1304 if (args) {
1305 /*
1306 * Set the free space warning levels for a non-root volume:
1307 *
1308 * Set the lower freespace limit (the level that will trigger a warning)
1309 * to 5% of the volume size or 250MB, whichever is less, and the desired
1310 * level (which will cancel the alert request) to 1/2 above that limit.
1311 * Start looking for free space to drop below this level and generate a
1312 * warning immediately if needed:
1313 */
1314 hfsmp->hfs_freespace_notify_warninglimit =
1315 MIN(HFS_LOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize,
1316 (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_LOWDISKTRIGGERFRACTION);
1317 hfsmp->hfs_freespace_notify_desiredlevel =
1318 MIN(HFS_LOWDISKSHUTOFFLEVEL / HFSTOVCB(hfsmp)->blockSize,
1319 (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_LOWDISKSHUTOFFFRACTION);
1320 } else {
1321 /*
1322 * Set the free space warning levels for the root volume:
1323 *
1324 * Set the lower freespace limit (the level that will trigger a warning)
1325 * to 1% of the volume size or 50MB, whichever is less, and the desired
1326 * level (which will cancel the alert request) to 2% or 75MB, whichever is less.
1327 */
1328 hfsmp->hfs_freespace_notify_warninglimit =
1329 MIN(HFS_ROOTLOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize,
1330 (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTLOWDISKTRIGGERFRACTION);
1331 hfsmp->hfs_freespace_notify_desiredlevel =
1332 MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL / HFSTOVCB(hfsmp)->blockSize,
1333 (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION);
1334 };
1335
2d21ac55
A
1336 /* Check if the file system exists on virtual device, like disk image */
1337 if (VNOP_IOCTL(devvp, DKIOCISVIRTUAL, (caddr_t)&isvirtual, 0, context) == 0) {
1338 if (isvirtual) {
1339 hfsmp->hfs_flags |= HFS_VIRTUAL_DEVICE;
1340 }
1341 }
1342
55e303ae
A
1343 /*
1344 * Start looking for free space to drop below this level and generate a
1345 * warning immediately if needed:
1346 */
1347 hfsmp->hfs_notification_conditions = 0;
1348 hfs_generate_volume_notifications(hfsmp);
2d21ac55 1349
9bccf70c
A
1350 if (ronly == 0) {
1351 (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0);
1352 }
1353 FREE(mdbp, M_TEMP);
1354 return (0);
1c79356b 1355
9bccf70c
A
1356error_exit:
1357 if (bp)
91447636 1358 buf_brelse(bp);
9bccf70c
A
1359 if (mdbp)
1360 FREE(mdbp, M_TEMP);
91447636 1361
b4c24cb9 1362 if (hfsmp && hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) {
91447636 1363 (void)VNOP_CLOSE(hfsmp->jvp, ronly ? FREAD : FREAD|FWRITE, context);
b4c24cb9
A
1364 hfsmp->jvp = NULL;
1365 }
9bccf70c 1366 if (hfsmp) {
2d21ac55
A
1367 if (hfsmp->hfs_devvp) {
1368 vnode_rele(hfsmp->hfs_devvp);
1369 }
9bccf70c 1370 FREE(hfsmp, M_HFSMNT);
91447636 1371 vfs_setfsprivate(mp, NULL);
9bccf70c 1372 }
1c79356b
A
1373 return (retval);
1374}
1375
1376
1377/*
1378 * Make a filesystem operational.
1379 * Nothing to do at the moment.
1380 */
1381/* ARGSUSED */
9bccf70c 1382static int
91447636 1383hfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t context)
1c79356b 1384{
9bccf70c 1385 return (0);
1c79356b
A
1386}
1387
1388
1389/*
1390 * unmount system call
1391 */
9bccf70c 1392static int
91447636 1393hfs_unmount(struct mount *mp, int mntflags, vfs_context_t context)
1c79356b 1394{
91447636 1395 struct proc *p = vfs_context_proc(context);
1c79356b
A
1396 struct hfsmount *hfsmp = VFSTOHFS(mp);
1397 int retval = E_NONE;
1398 int flags;
9bccf70c 1399 int force;
91447636 1400 int started_tr = 0;
1c79356b
A
1401
1402 flags = 0;
9bccf70c
A
1403 force = 0;
1404 if (mntflags & MNT_FORCE) {
1c79356b 1405 flags |= FORCECLOSE;
9bccf70c
A
1406 force = 1;
1407 }
1c79356b 1408
9bccf70c 1409 if ((retval = hfs_flushfiles(mp, flags, p)) && !force)
1c79356b
A
1410 return (retval);
1411
55e303ae 1412 if (hfsmp->hfs_flags & HFS_METADATA_ZONE)
91447636 1413 (void) hfs_recording_suspend(hfsmp);
55e303ae 1414
1c79356b
A
1415 /*
1416 * Flush out the b-trees, volume bitmap and Volume Header
1417 */
55e303ae 1418 if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) {
2d21ac55
A
1419 retval = hfs_start_transaction(hfsmp);
1420 if (retval == 0) {
1421 started_tr = 1;
1422 } else if (!force) {
1423 goto err_exit;
1424 }
1425
1426 if (hfsmp->hfs_startup_vp) {
1427 (void) hfs_lock(VTOC(hfsmp->hfs_startup_vp), HFS_EXCLUSIVE_LOCK);
1428 retval = hfs_fsync(hfsmp->hfs_startup_vp, MNT_WAIT, 0, p);
1429 hfs_unlock(VTOC(hfsmp->hfs_startup_vp));
1430 if (retval && !force)
1431 goto err_exit;
1432 }
91447636
A
1433
1434 if (hfsmp->hfs_attribute_vp) {
1435 (void) hfs_lock(VTOC(hfsmp->hfs_attribute_vp), HFS_EXCLUSIVE_LOCK);
1436 retval = hfs_fsync(hfsmp->hfs_attribute_vp, MNT_WAIT, 0, p);
1437 hfs_unlock(VTOC(hfsmp->hfs_attribute_vp));
1438 if (retval && !force)
1439 goto err_exit;
b4c24cb9 1440 }
91447636
A
1441
1442 (void) hfs_lock(VTOC(hfsmp->hfs_catalog_vp), HFS_EXCLUSIVE_LOCK);
1443 retval = hfs_fsync(hfsmp->hfs_catalog_vp, MNT_WAIT, 0, p);
1444 hfs_unlock(VTOC(hfsmp->hfs_catalog_vp));
9bccf70c 1445 if (retval && !force)
b4c24cb9
A
1446 goto err_exit;
1447
91447636
A
1448 (void) hfs_lock(VTOC(hfsmp->hfs_extents_vp), HFS_EXCLUSIVE_LOCK);
1449 retval = hfs_fsync(hfsmp->hfs_extents_vp, MNT_WAIT, 0, p);
1450 hfs_unlock(VTOC(hfsmp->hfs_extents_vp));
9bccf70c 1451 if (retval && !force)
b4c24cb9
A
1452 goto err_exit;
1453
91447636
A
1454 if (hfsmp->hfs_allocation_vp) {
1455 (void) hfs_lock(VTOC(hfsmp->hfs_allocation_vp), HFS_EXCLUSIVE_LOCK);
1456 retval = hfs_fsync(hfsmp->hfs_allocation_vp, MNT_WAIT, 0, p);
1457 hfs_unlock(VTOC(hfsmp->hfs_allocation_vp));
55e303ae
A
1458 if (retval && !force)
1459 goto err_exit;
1460 }
1461
91447636
A
1462 if (hfsmp->hfc_filevp && vnode_issystem(hfsmp->hfc_filevp)) {
1463 retval = hfs_fsync(hfsmp->hfc_filevp, MNT_WAIT, 0, p);
1464 if (retval && !force)
b4c24cb9 1465 goto err_exit;
1c79356b 1466 }
2d21ac55
A
1467
1468 /* If runtime corruption was detected, indicate that the volume
1469 * was not unmounted cleanly.
1470 */
1471 if (hfsmp->vcbAtrb & kHFSVolumeInconsistentMask) {
1c79356b
A
1472 HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeUnmountedMask;
1473 } else {
9bccf70c 1474 HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeUnmountedMask;
1c79356b 1475 }
2d21ac55 1476
91447636 1477 retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0);
1c79356b 1478 if (retval) {
1c79356b 1479 HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeUnmountedMask;
9bccf70c 1480 if (!force)
b4c24cb9
A
1481 goto err_exit; /* could not flush everything */
1482 }
1483
2d21ac55
A
1484 if (started_tr) {
1485 hfs_end_transaction(hfsmp);
1486 started_tr = 0;
1487 }
1c79356b
A
1488 }
1489
b4c24cb9
A
1490 if (hfsmp->jnl) {
1491 journal_flush(hfsmp->jnl);
1492 }
1493
1c79356b
A
1494 /*
1495 * Invalidate our caches and release metadata vnodes
1496 */
1497 (void) hfsUnmount(hfsmp, p);
1498
55e303ae
A
1499 /*
1500 * Last chance to dump unreferenced system files.
1501 */
1502 (void) vflush(mp, NULLVP, FORCECLOSE);
1503
1c79356b
A
1504 if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord)
1505 (void) hfs_relconverter(hfsmp->hfs_encoding);
1506
b4c24cb9
A
1507 // XXXdbg
1508 if (hfsmp->jnl) {
1509 journal_close(hfsmp->jnl);
55e303ae 1510 hfsmp->jnl = NULL;
b4c24cb9
A
1511 }
1512
91447636
A
1513 VNOP_FSYNC(hfsmp->hfs_devvp, MNT_WAIT, context);
1514
b4c24cb9 1515 if (hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) {
91447636 1516 retval = VNOP_CLOSE(hfsmp->jvp,
55e303ae 1517 hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE,
91447636
A
1518 context);
1519 vnode_put(hfsmp->jvp);
55e303ae 1520 hfsmp->jvp = NULL;
b4c24cb9
A
1521 }
1522 // XXXdbg
1523
55e303ae
A
1524#ifdef HFS_SPARSE_DEV
1525 /* Drop our reference on the backing fs (if any). */
1526 if ((hfsmp->hfs_flags & HFS_HAS_SPARSE_DEVICE) && hfsmp->hfs_backingfs_rootvp) {
1527 struct vnode * tmpvp;
1528
1529 hfsmp->hfs_flags &= ~HFS_HAS_SPARSE_DEVICE;
1530 tmpvp = hfsmp->hfs_backingfs_rootvp;
1531 hfsmp->hfs_backingfs_rootvp = NULLVP;
91447636 1532 vnode_rele(tmpvp);
55e303ae
A
1533 }
1534#endif /* HFS_SPARSE_DEV */
91447636 1535 lck_mtx_destroy(&hfsmp->hfc_mutex, hfs_mutex_group);
2d21ac55 1536 vnode_rele(hfsmp->hfs_devvp);
1c79356b 1537 FREE(hfsmp, M_HFSMNT);
91447636 1538
9bccf70c 1539 return (0);
b4c24cb9
A
1540
1541 err_exit:
91447636
A
1542 if (started_tr) {
1543 hfs_end_transaction(hfsmp);
b4c24cb9
A
1544 }
1545 return retval;
1c79356b
A
1546}
1547
1548
1549/*
1550 * Return the root of a filesystem.
1c79356b 1551 */
9bccf70c 1552static int
91447636 1553hfs_vfs_root(struct mount *mp, struct vnode **vpp, __unused vfs_context_t context)
1c79356b 1554{
91447636 1555 return hfs_vget(VFSTOHFS(mp), (cnid_t)kHFSRootFolderID, vpp, 1);
1c79356b
A
1556}
1557
1558
1559/*
1560 * Do operations associated with quotas
1561 */
2d21ac55
A
1562#if !QUOTA
1563static int
1564hfs_quotactl(__unused struct mount *mp, __unused int cmds, __unused uid_t uid, __unused caddr_t datap, __unused vfs_context_t context)
1565{
1566 return (ENOTSUP);
1567}
1568#else
91447636
A
1569static int
1570hfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t datap, vfs_context_t context)
1c79356b 1571{
91447636 1572 struct proc *p = vfs_context_proc(context);
9bccf70c
A
1573 int cmd, type, error;
1574
2d21ac55 1575 if (uid == ~0U)
91447636 1576 uid = vfs_context_ucred(context)->cr_ruid;
9bccf70c
A
1577 cmd = cmds >> SUBCMDSHIFT;
1578
1579 switch (cmd) {
1580 case Q_SYNC:
1581 case Q_QUOTASTAT:
1582 break;
1583 case Q_GETQUOTA:
91447636 1584 if (uid == vfs_context_ucred(context)->cr_ruid)
9bccf70c
A
1585 break;
1586 /* fall through */
1587 default:
91447636 1588 if ( (error = vfs_context_suser(context)) )
9bccf70c
A
1589 return (error);
1590 }
1591
1592 type = cmds & SUBCMDMASK;
1593 if ((u_int)type >= MAXQUOTAS)
1594 return (EINVAL);
91447636 1595 if (vfs_busy(mp, LK_NOWAIT))
9bccf70c
A
1596 return (0);
1597
1598 switch (cmd) {
1c79356b 1599
9bccf70c 1600 case Q_QUOTAON:
91447636 1601 error = hfs_quotaon(p, mp, type, datap);
9bccf70c
A
1602 break;
1603
1604 case Q_QUOTAOFF:
1605 error = hfs_quotaoff(p, mp, type);
1606 break;
1607
1608 case Q_SETQUOTA:
91447636 1609 error = hfs_setquota(mp, uid, type, datap);
9bccf70c
A
1610 break;
1611
1612 case Q_SETUSE:
91447636 1613 error = hfs_setuse(mp, uid, type, datap);
9bccf70c
A
1614 break;
1615
1616 case Q_GETQUOTA:
91447636 1617 error = hfs_getquota(mp, uid, type, datap);
9bccf70c
A
1618 break;
1619
1620 case Q_SYNC:
1621 error = hfs_qsync(mp);
1622 break;
1623
1624 case Q_QUOTASTAT:
91447636 1625 error = hfs_quotastat(mp, type, datap);
9bccf70c
A
1626 break;
1627
1628 default:
1629 error = EINVAL;
1630 break;
1631 }
91447636
A
1632 vfs_unbusy(mp);
1633
9bccf70c 1634 return (error);
1c79356b 1635}
2d21ac55 1636#endif /* QUOTA */
1c79356b 1637
91447636
A
1638/* Subtype is composite of bits */
1639#define HFS_SUBTYPE_JOURNALED 0x01
1640#define HFS_SUBTYPE_CASESENSITIVE 0x02
1641/* bits 2 - 6 reserved */
1642#define HFS_SUBTYPE_STANDARDHFS 0x80
b4c24cb9 1643
1c79356b
A
1644/*
1645 * Get file system statistics.
1646 */
1647static int
91447636 1648hfs_statfs(struct mount *mp, register struct vfsstatfs *sbp, __unused vfs_context_t context)
1c79356b
A
1649{
1650 ExtendedVCB *vcb = VFSTOVCB(mp);
1651 struct hfsmount *hfsmp = VFSTOHFS(mp);
1652 u_long freeCNIDs;
2d21ac55 1653 u_int16_t subtype = 0;
1c79356b 1654
1c79356b
A
1655 freeCNIDs = (u_long)0xFFFFFFFF - (u_long)vcb->vcbNxtCNID;
1656
2d21ac55 1657 sbp->f_bsize = (u_int32_t)vcb->blockSize;
cf7d32b8 1658 sbp->f_iosize = (size_t)cluster_max_io_size(mp, 0);
2d21ac55
A
1659 sbp->f_blocks = (u_int64_t)((unsigned long)vcb->totalBlocks);
1660 sbp->f_bfree = (u_int64_t)((unsigned long )hfs_freeblks(hfsmp, 0));
1661 sbp->f_bavail = (u_int64_t)((unsigned long )hfs_freeblks(hfsmp, 1));
1662 sbp->f_files = (u_int64_t)((unsigned long )(vcb->totalBlocks - 2)); /* max files is constrained by total blocks */
1663 sbp->f_ffree = (u_int64_t)((unsigned long )(MIN(freeCNIDs, sbp->f_bavail)));
91447636
A
1664
1665 /*
1666 * Subtypes (flavors) for HFS
1667 * 0: Mac OS Extended
1668 * 1: Mac OS Extended (Journaled)
1669 * 2: Mac OS Extended (Case Sensitive)
1670 * 3: Mac OS Extended (Case Sensitive, Journaled)
1671 * 4 - 127: Reserved
1672 * 128: Mac OS Standard
1673 *
1674 */
1675 if (hfsmp->hfs_flags & HFS_STANDARD) {
1676 subtype = HFS_SUBTYPE_STANDARDHFS;
1677 } else /* HFS Plus */ {
1678 if (hfsmp->jnl)
1679 subtype |= HFS_SUBTYPE_JOURNALED;
1680 if (hfsmp->hfs_flags & HFS_CASE_SENSITIVE)
1681 subtype |= HFS_SUBTYPE_CASESENSITIVE;
1c79356b 1682 }
91447636
A
1683 sbp->f_fssubtype = subtype;
1684
1c79356b
A
1685 return (0);
1686}
1687
1688
b4c24cb9
A
1689//
1690// XXXdbg -- this is a callback to be used by the journal to
1691// get meta data blocks flushed out to disk.
1692//
1693// XXXdbg -- be smarter and don't flush *every* block on each
1694// call. try to only flush some so we don't wind up
1695// being too synchronous.
1696//
1697__private_extern__
1698void
1699hfs_sync_metadata(void *arg)
1700{
1701 struct mount *mp = (struct mount *)arg;
b4c24cb9
A
1702 struct hfsmount *hfsmp;
1703 ExtendedVCB *vcb;
91447636 1704 buf_t bp;
593a1d5f 1705 int retval;
91447636 1706 daddr64_t priIDSector;
b4c24cb9
A
1707 hfsmp = VFSTOHFS(mp);
1708 vcb = HFSTOVCB(hfsmp);
1709
b4c24cb9 1710 // now make sure the super block is flushed
593a1d5f
A
1711 priIDSector = (daddr64_t)((vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) +
1712 HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size));
1713
1714 retval = (int)buf_meta_bread(hfsmp->hfs_devvp,
1715 HFS_PHYSBLK_ROUNDDOWN(priIDSector, hfsmp->hfs_log_per_phys),
1716 hfsmp->hfs_physical_block_size, NOCRED, &bp);
2d21ac55 1717 if ((retval != 0 ) && (retval != ENXIO)) {
0c530ab8 1718 printf("hfs_sync_metadata: can't read volume header at %d! (retval 0x%x)\n",
2d21ac55 1719 (int)priIDSector, retval);
b4c24cb9
A
1720 }
1721
91447636
A
1722 if (retval == 0 && ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI)) {
1723 buf_bwrite(bp);
b4c24cb9 1724 } else if (bp) {
91447636 1725 buf_brelse(bp);
b4c24cb9
A
1726 }
1727
1728 // the alternate super block...
1729 // XXXdbg - we probably don't need to do this each and every time.
1730 // hfs_btreeio.c:FlushAlternate() should flag when it was
1731 // written...
91447636 1732 if (hfsmp->hfs_alt_id_sector) {
593a1d5f
A
1733 retval = (int)buf_meta_bread(hfsmp->hfs_devvp,
1734 HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys),
1735 hfsmp->hfs_physical_block_size, NOCRED, &bp);
91447636
A
1736 if (retval == 0 && ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI)) {
1737 buf_bwrite(bp);
1738 } else if (bp) {
1739 buf_brelse(bp);
1740 }
b4c24cb9 1741 }
b4c24cb9
A
1742}
1743
91447636
A
1744
1745struct hfs_sync_cargs {
1746 kauth_cred_t cred;
1747 struct proc *p;
1748 int waitfor;
1749 int error;
1750};
1751
1752
1753static int
1754hfs_sync_callback(struct vnode *vp, void *cargs)
1755{
1756 struct cnode *cp;
1757 struct hfs_sync_cargs *args;
1758 int error;
1759
1760 args = (struct hfs_sync_cargs *)cargs;
1761
1762 if (hfs_lock(VTOC(vp), HFS_EXCLUSIVE_LOCK) != 0) {
1763 return (VNODE_RETURNED);
1764 }
1765 cp = VTOC(vp);
1766
1767 if ((cp->c_flag & C_MODIFIED) ||
1768 (cp->c_touch_acctime | cp->c_touch_chgtime | cp->c_touch_modtime) ||
1769 vnode_hasdirtyblks(vp)) {
1770 error = hfs_fsync(vp, args->waitfor, 0, args->p);
1771
1772 if (error)
1773 args->error = error;
1774 }
1775 hfs_unlock(cp);
1776 return (VNODE_RETURNED);
1777}
1778
1779
1780
1c79356b
A
1781/*
1782 * Go through the disk queues to initiate sandbagged IO;
1783 * go through the inodes to write those that have been modified;
1784 * initiate the writing of the super block if it has been modified.
1785 *
1786 * Note: we are always called with the filesystem marked `MPBUSY'.
1787 */
9bccf70c 1788static int
91447636 1789hfs_sync(struct mount *mp, int waitfor, vfs_context_t context)
1c79356b 1790{
91447636 1791 struct proc *p = vfs_context_proc(context);
9bccf70c
A
1792 struct cnode *cp;
1793 struct hfsmount *hfsmp;
1794 ExtendedVCB *vcb;
91447636 1795 struct vnode *meta_vp[4];
9bccf70c
A
1796 int i;
1797 int error, allerror = 0;
91447636 1798 struct hfs_sync_cargs args;
1c79356b 1799
593a1d5f
A
1800 hfsmp = VFSTOHFS(mp);
1801
1c79356b 1802 /*
593a1d5f 1803 * hfs_changefs might be manipulating vnodes so back off
1c79356b 1804 */
593a1d5f 1805 if (hfsmp->hfs_flags & HFS_IN_CHANGEFS)
1c79356b
A
1806 return (0);
1807
55e303ae
A
1808 if (hfsmp->hfs_flags & HFS_READ_ONLY)
1809 return (EROFS);
1c79356b 1810
3a60a9f5
A
1811 /* skip over frozen volumes */
1812 if (!lck_rw_try_lock_shared(&hfsmp->hfs_insync))
1813 return 0;
1814
2d21ac55 1815 args.cred = kauth_cred_get();
91447636
A
1816 args.waitfor = waitfor;
1817 args.p = p;
1818 args.error = 0;
9bccf70c 1819 /*
91447636
A
1820 * hfs_sync_callback will be called for each vnode
1821 * hung off of this mount point... the vnode will be
1822 * properly referenced and unreferenced around the callback
9bccf70c 1823 */
91447636 1824 vnode_iterate(mp, 0, hfs_sync_callback, (void *)&args);
1c79356b 1825
91447636
A
1826 if (args.error)
1827 allerror = args.error;
1c79356b 1828
9bccf70c
A
1829 vcb = HFSTOVCB(hfsmp);
1830
1831 meta_vp[0] = vcb->extentsRefNum;
1832 meta_vp[1] = vcb->catalogRefNum;
1833 meta_vp[2] = vcb->allocationsRefNum; /* This is NULL for standard HFS */
91447636 1834 meta_vp[3] = hfsmp->hfs_attribute_vp; /* Optional file */
9bccf70c
A
1835
1836 /* Now sync our three metadata files */
91447636 1837 for (i = 0; i < 4; ++i) {
9bccf70c
A
1838 struct vnode *btvp;
1839
91447636
A
1840 btvp = meta_vp[i];;
1841 if ((btvp==0) || (vnode_mount(btvp) != mp))
9bccf70c 1842 continue;
b4c24cb9 1843
91447636
A
1844 /* XXX use hfs_systemfile_lock instead ? */
1845 (void) hfs_lock(VTOC(btvp), HFS_EXCLUSIVE_LOCK);
9bccf70c 1846 cp = VTOC(btvp);
91447636
A
1847
1848 if (((cp->c_flag & C_MODIFIED) == 0) &&
1849 (cp->c_touch_acctime == 0) &&
1850 (cp->c_touch_chgtime == 0) &&
1851 (cp->c_touch_modtime == 0) &&
1852 vnode_hasdirtyblks(btvp) == 0) {
1853 hfs_unlock(VTOC(btvp));
9bccf70c
A
1854 continue;
1855 }
91447636 1856 error = vnode_get(btvp);
9bccf70c 1857 if (error) {
91447636 1858 hfs_unlock(VTOC(btvp));
9bccf70c
A
1859 continue;
1860 }
91447636 1861 if ((error = hfs_fsync(btvp, waitfor, 0, p)))
9bccf70c 1862 allerror = error;
9bccf70c 1863
91447636
A
1864 hfs_unlock(cp);
1865 vnode_put(btvp);
1866 };
9bccf70c
A
1867
1868 /*
1869 * Force stale file system control information to be flushed.
1870 */
1871 if (vcb->vcbSigWord == kHFSSigWord) {
91447636 1872 if ((error = VNOP_FSYNC(hfsmp->hfs_devvp, waitfor, context))) {
9bccf70c 1873 allerror = error;
91447636 1874 }
9bccf70c
A
1875 }
1876#if QUOTA
1877 hfs_qsync(mp);
1878#endif /* QUOTA */
55e303ae 1879
2d21ac55
A
1880 hfs_hotfilesync(hfsmp, vfs_context_kernel());
1881
9bccf70c
A
1882 /*
1883 * Write back modified superblock.
1884 */
9bccf70c
A
1885 if (IsVCBDirty(vcb)) {
1886 error = hfs_flushvolumeheader(hfsmp, waitfor, 0);
b4c24cb9
A
1887 if (error)
1888 allerror = error;
9bccf70c 1889 }
1c79356b 1890
b4c24cb9
A
1891 if (hfsmp->jnl) {
1892 journal_flush(hfsmp->jnl);
1893 }
3a60a9f5
A
1894
1895 lck_rw_unlock_shared(&hfsmp->hfs_insync);
9bccf70c 1896 return (allerror);
1c79356b
A
1897}
1898
1899
1900/*
1901 * File handle to vnode
1902 *
1903 * Have to be really careful about stale file handles:
9bccf70c
A
1904 * - check that the cnode id is valid
1905 * - call hfs_vget() to get the locked cnode
1906 * - check for an unallocated cnode (i_mode == 0)
1c79356b
A
1907 * - check that the given client host has export rights and return
1908 * those rights via. exflagsp and credanonp
1909 */
9bccf70c 1910static int
2d21ac55 1911hfs_fhtovp(struct mount *mp, int fhlen, unsigned char *fhp, struct vnode **vpp, __unused vfs_context_t context)
1c79356b
A
1912{
1913 struct hfsfid *hfsfhp;
1914 struct vnode *nvp;
1915 int result;
1c79356b
A
1916
1917 *vpp = NULL;
1918 hfsfhp = (struct hfsfid *)fhp;
1919
2d21ac55 1920 if (fhlen < (int)sizeof(struct hfsfid))
91447636 1921 return (EINVAL);
1c79356b 1922
0c530ab8 1923 result = hfs_vget(VFSTOHFS(mp), ntohl(hfsfhp->hfsfid_cnid), &nvp, 0);
91447636
A
1924 if (result) {
1925 if (result == ENOENT)
1926 result = ESTALE;
1927 return result;
1928 }
1c79356b 1929
0b4e3aa0
A
1930 /* The createtime can be changed by hfs_setattr or hfs_setattrlist.
1931 * For NFS, we are assuming that only if the createtime was moved
1932 * forward would it mean the fileID got reused in that session by
1933 * wrapping. We don't have a volume ID or other unique identifier to
1934 * to use here for a generation ID across reboots, crashes where
1935 * metadata noting lastFileID didn't make it to disk but client has
1936 * it, or volume erasures where fileIDs start over again. Lastly,
1937 * with HFS allowing "wraps" of fileIDs now, this becomes more
1938 * error prone. Future, would be change the "wrap bit" to a unique
1939 * wrap number and use that for generation number. For now do this.
1940 */
2d21ac55 1941 if (((time_t)(ntohl(hfsfhp->hfsfid_gen)) < VTOC(nvp)->c_itime)) {
91447636
A
1942 hfs_unlock(VTOC(nvp));
1943 vnode_put(nvp);
9bccf70c 1944 return (ESTALE);
55e303ae 1945 }
1c79356b 1946 *vpp = nvp;
91447636
A
1947
1948 hfs_unlock(VTOC(nvp));
9bccf70c 1949 return (0);
1c79356b
A
1950}
1951
1952
1953/*
1954 * Vnode pointer to File handle
1955 */
1956/* ARGSUSED */
9bccf70c 1957static int
2d21ac55 1958hfs_vptofh(struct vnode *vp, int *fhlenp, unsigned char *fhp, __unused vfs_context_t context)
1c79356b 1959{
9bccf70c 1960 struct cnode *cp;
1c79356b 1961 struct hfsfid *hfsfhp;
1c79356b 1962
9bccf70c 1963 if (ISHFS(VTOVCB(vp)))
91447636
A
1964 return (ENOTSUP); /* hfs standard is not exportable */
1965
1966 if (*fhlenp < (int)sizeof(struct hfsfid))
1967 return (EOVERFLOW);
1c79356b 1968
9bccf70c
A
1969 cp = VTOC(vp);
1970 hfsfhp = (struct hfsfid *)fhp;
0c530ab8
A
1971 hfsfhp->hfsfid_cnid = htonl(cp->c_fileid);
1972 hfsfhp->hfsfid_gen = htonl(cp->c_itime);
91447636 1973 *fhlenp = sizeof(struct hfsfid);
1c79356b 1974
9bccf70c 1975 return (0);
1c79356b
A
1976}
1977
1978
1979/*
1980 * Initial HFS filesystems, done only once.
1981 */
9bccf70c 1982static int
91447636 1983hfs_init(__unused struct vfsconf *vfsp)
1c79356b 1984{
9bccf70c 1985 static int done = 0;
1c79356b 1986
9bccf70c
A
1987 if (done)
1988 return (0);
1989 done = 1;
1990 hfs_chashinit();
1991 hfs_converterinit();
1c79356b 1992
55e303ae 1993 BTReserveSetup();
91447636
A
1994
1995
1996 hfs_lock_attr = lck_attr_alloc_init();
1997 hfs_group_attr = lck_grp_attr_alloc_init();
1998 hfs_mutex_group = lck_grp_alloc_init("hfs-mutex", hfs_group_attr);
1999 hfs_rwlock_group = lck_grp_alloc_init("hfs-rwlock", hfs_group_attr);
0c530ab8 2000
1c79356b 2001
9bccf70c 2002 return (0);
1c79356b
A
2003}
2004
55e303ae 2005static int
2d21ac55 2006hfs_getmountpoint(struct vnode *vp, struct hfsmount **hfsmpp)
55e303ae
A
2007{
2008 struct hfsmount * hfsmp;
91447636 2009 char fstypename[MFSNAMELEN];
55e303ae
A
2010
2011 if (vp == NULL)
2012 return (EINVAL);
2013
91447636 2014 if (!vnode_isvroot(vp))
55e303ae
A
2015 return (EINVAL);
2016
91447636 2017 vnode_vfsname(vp, fstypename);
2d21ac55 2018 if (strncmp(fstypename, "hfs", sizeof(fstypename)) != 0)
55e303ae
A
2019 return (EINVAL);
2020
2021 hfsmp = VTOHFS(vp);
2022
2023 if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord)
2024 return (EINVAL);
2025
2026 *hfsmpp = hfsmp;
2027
2028 return (0);
2029}
1c79356b 2030
b4c24cb9
A
2031// XXXdbg
2032#include <sys/filedesc.h>
2033
1c79356b 2034/*
9bccf70c 2035 * HFS filesystem related variables.
1c79356b 2036 */
9bccf70c 2037static int
91447636
A
2038hfs_sysctl(int *name, __unused u_int namelen, user_addr_t oldp, size_t *oldlenp,
2039 user_addr_t newp, size_t newlen, vfs_context_t context)
1c79356b 2040{
91447636 2041 struct proc *p = vfs_context_proc(context);
55e303ae 2042 int error;
55e303ae 2043 struct hfsmount *hfsmp;
9bccf70c
A
2044
2045 /* all sysctl names at this level are terminal */
1c79356b 2046
55e303ae 2047 if (name[0] == HFS_ENCODINGBIAS) {
2d21ac55 2048 int bias;
55e303ae
A
2049
2050 bias = hfs_getencodingbias();
2051 error = sysctl_int(oldp, oldlenp, newp, newlen, &bias);
2052 if (error == 0 && newp)
2053 hfs_setencodingbias(bias);
2054 return (error);
2055
2056 } else if (name[0] == HFS_EXTEND_FS) {
91447636 2057 u_int64_t newsize;
2d21ac55 2058 vnode_t vp = vfs_context_cwd(context);
91447636 2059
2d21ac55 2060 if (newp == USER_ADDR_NULL || vp == NULLVP)
55e303ae 2061 return (EINVAL);
91447636 2062 if ((error = hfs_getmountpoint(vp, &hfsmp)))
55e303ae 2063 return (error);
2d21ac55 2064 error = sysctl_quad(oldp, oldlenp, newp, newlen, (quad_t *)&newsize);
55e303ae
A
2065 if (error)
2066 return (error);
2067
91447636 2068 error = hfs_extendfs(hfsmp, newsize, context);
55e303ae
A
2069 return (error);
2070
2071 } else if (name[0] == HFS_ENCODINGHINT) {
2072 size_t bufsize;
2073 size_t bytes;
2074 u_int32_t hint;
2075 u_int16_t *unicode_name;
2076 char *filename;
2077
2d21ac55
A
2078 if ((newlen <= 0) || (newlen > MAXPATHLEN))
2079 return (EINVAL);
2080
55e303ae
A
2081 bufsize = MAX(newlen * 3, MAXPATHLEN);
2082 MALLOC(filename, char *, newlen, M_TEMP, M_WAITOK);
2083 MALLOC(unicode_name, u_int16_t *, bufsize, M_TEMP, M_WAITOK);
2084
2085 error = copyin(newp, (caddr_t)filename, newlen);
2086 if (error == 0) {
2d21ac55 2087 error = utf8_decodestr((u_int8_t *)filename, newlen - 1, unicode_name,
55e303ae
A
2088 &bytes, bufsize, 0, UTF_DECOMPOSED);
2089 if (error == 0) {
2090 hint = hfs_pickencoding(unicode_name, bytes / 2);
2d21ac55 2091 error = sysctl_int(oldp, oldlenp, USER_ADDR_NULL, 0, (int32_t *)&hint);
55e303ae
A
2092 }
2093 }
2094 FREE(unicode_name, M_TEMP);
2095 FREE(filename, M_TEMP);
2096 return (error);
2097
2098 } else if (name[0] == HFS_ENABLE_JOURNALING) {
b4c24cb9 2099 // make the file system journaled...
2d21ac55
A
2100 vnode_t vp = vfs_context_cwd(context);
2101 vnode_t jvp;
b4c24cb9 2102 ExtendedVCB *vcb;
b4c24cb9
A
2103 struct cat_attr jnl_attr, jinfo_attr;
2104 struct cat_fork jnl_fork, jinfo_fork;
2105 void *jnl = NULL;
91447636 2106 int lockflags;
d7e50217
A
2107
2108 /* Only root can enable journaling */
91447636 2109 if (!is_suser()) {
d7e50217
A
2110 return (EPERM);
2111 }
2d21ac55 2112 if (vp == NULLVP)
91447636 2113 return EINVAL;
55e303ae 2114
b4c24cb9 2115 hfsmp = VTOHFS(vp);
55e303ae 2116 if (hfsmp->hfs_flags & HFS_READ_ONLY) {
b4c24cb9
A
2117 return EROFS;
2118 }
2119 if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) {
2120 printf("hfs: can't make a plain hfs volume journaled.\n");
2121 return EINVAL;
2122 }
2123
2124 if (hfsmp->jnl) {
2d21ac55 2125 printf("hfs: volume @ mp %p is already journaled!\n", vnode_mount(vp));
b4c24cb9
A
2126 return EAGAIN;
2127 }
2128
2129 vcb = HFSTOVCB(hfsmp);
91447636 2130 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_EXTENTS, HFS_EXCLUSIVE_LOCK);
b4c24cb9
A
2131 if (BTHasContiguousNodes(VTOF(vcb->catalogRefNum)) == 0 ||
2132 BTHasContiguousNodes(VTOF(vcb->extentsRefNum)) == 0) {
2133
2134 printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n");
91447636 2135 hfs_systemfile_unlock(hfsmp, lockflags);
b4c24cb9
A
2136 return EINVAL;
2137 }
91447636 2138 hfs_systemfile_unlock(hfsmp, lockflags);
b4c24cb9
A
2139
2140 // make sure these both exist!
91447636
A
2141 if ( GetFileInfo(vcb, kHFSRootFolderID, ".journal_info_block", &jinfo_attr, &jinfo_fork) == 0
2142 || GetFileInfo(vcb, kHFSRootFolderID, ".journal", &jnl_attr, &jnl_fork) == 0) {
b4c24cb9
A
2143
2144 return EINVAL;
2145 }
2146
91447636 2147 hfs_sync(hfsmp->hfs_mp, MNT_WAIT, context);
b4c24cb9
A
2148
2149 printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n",
2150 (off_t)name[2], (off_t)name[3]);
2151
2152 jvp = hfsmp->hfs_devvp;
2153 jnl = journal_create(jvp,
2154 (off_t)name[2] * (off_t)HFSTOVCB(hfsmp)->blockSize
2155 + HFSTOVCB(hfsmp)->hfsPlusIOPosOffset,
55e303ae 2156 (off_t)((unsigned)name[3]),
b4c24cb9 2157 hfsmp->hfs_devvp,
593a1d5f 2158 hfsmp->hfs_logical_block_size,
b4c24cb9
A
2159 0,
2160 0,
2161 hfs_sync_metadata, hfsmp->hfs_mp);
2162
2163 if (jnl == NULL) {
2164 printf("hfs: FAILED to create the journal!\n");
2165 if (jvp && jvp != hfsmp->hfs_devvp) {
91447636 2166 VNOP_CLOSE(jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, context);
b4c24cb9
A
2167 }
2168 jvp = NULL;
2169
2170 return EINVAL;
2171 }
2172
2173 hfs_global_exclusive_lock_acquire(hfsmp);
2174
4452a7af
A
2175 /*
2176 * Flush all dirty metadata buffers.
2177 */
2178 buf_flushdirtyblks(hfsmp->hfs_devvp, MNT_WAIT, 0, "hfs_sysctl");
2179 buf_flushdirtyblks(hfsmp->hfs_extents_vp, MNT_WAIT, 0, "hfs_sysctl");
2180 buf_flushdirtyblks(hfsmp->hfs_catalog_vp, MNT_WAIT, 0, "hfs_sysctl");
2181 buf_flushdirtyblks(hfsmp->hfs_allocation_vp, MNT_WAIT, 0, "hfs_sysctl");
2182 if (hfsmp->hfs_attribute_vp)
2183 buf_flushdirtyblks(hfsmp->hfs_attribute_vp, MNT_WAIT, 0, "hfs_sysctl");
2184
b4c24cb9
A
2185 HFSTOVCB(hfsmp)->vcbJinfoBlock = name[1];
2186 HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeJournaledMask;
2187 hfsmp->jvp = jvp;
2188 hfsmp->jnl = jnl;
2189
2190 // save this off for the hack-y check in hfs_remove()
2191 hfsmp->jnl_start = (u_int32_t)name[2];
55e303ae 2192 hfsmp->jnl_size = (off_t)((unsigned)name[3]);
b4c24cb9
A
2193 hfsmp->hfs_jnlinfoblkid = jinfo_attr.ca_fileid;
2194 hfsmp->hfs_jnlfileid = jnl_attr.ca_fileid;
2195
2d21ac55 2196 vfs_setflags(hfsmp->hfs_mp, (u_int64_t)((unsigned int)MNT_JOURNALED));
b4c24cb9
A
2197
2198 hfs_global_exclusive_lock_release(hfsmp);
2199 hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1);
2200
2201 return 0;
55e303ae 2202 } else if (name[0] == HFS_DISABLE_JOURNALING) {
b4c24cb9 2203 // clear the journaling bit
2d21ac55 2204 vnode_t vp = vfs_context_cwd(context);
b4c24cb9 2205
d7e50217 2206 /* Only root can disable journaling */
91447636 2207 if (!is_suser()) {
d7e50217
A
2208 return (EPERM);
2209 }
2d21ac55 2210 if (vp == NULLVP)
91447636 2211 return EINVAL;
55e303ae 2212
b4c24cb9 2213 hfsmp = VTOHFS(vp);
b4c24cb9 2214
2d21ac55
A
2215 /*
2216 * Disabling journaling is disallowed on volumes with directory hard links
2217 * because we have not tested the relevant code path.
2218 */
2219 if (hfsmp->hfs_private_attr[DIR_HARDLINKS].ca_entries != 0){
2220 printf("hfs: cannot disable journaling on volumes with directory hardlinks\n");
2221 return EPERM;
2222 }
2223
2224 printf("hfs: disabling journaling for mount @ %p\n", vnode_mount(vp));
b4c24cb9 2225
b4c24cb9
A
2226 hfs_global_exclusive_lock_acquire(hfsmp);
2227
2228 // Lights out for you buddy!
91447636 2229 journal_close(hfsmp->jnl);
b4c24cb9 2230 hfsmp->jnl = NULL;
b4c24cb9
A
2231
2232 if (hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) {
91447636 2233 VNOP_CLOSE(hfsmp->jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, context);
b4c24cb9 2234 }
b4c24cb9 2235 hfsmp->jvp = NULL;
2d21ac55 2236 vfs_clearflags(hfsmp->hfs_mp, (u_int64_t)((unsigned int)MNT_JOURNALED));
b4c24cb9
A
2237 hfsmp->jnl_start = 0;
2238 hfsmp->hfs_jnlinfoblkid = 0;
2239 hfsmp->hfs_jnlfileid = 0;
2240
2241 HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeJournaledMask;
2242
2243 hfs_global_exclusive_lock_release(hfsmp);
2244 hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1);
2245
2246 return 0;
55e303ae 2247 } else if (name[0] == HFS_GET_JOURNAL_INFO) {
2d21ac55 2248 vnode_t vp = vfs_context_cwd(context);
55e303ae
A
2249 off_t jnl_start, jnl_size;
2250
2d21ac55 2251 if (vp == NULLVP)
91447636
A
2252 return EINVAL;
2253
55e303ae
A
2254 hfsmp = VTOHFS(vp);
2255 if (hfsmp->jnl == NULL) {
2256 jnl_start = 0;
2257 jnl_size = 0;
2258 } else {
2259 jnl_start = (off_t)(hfsmp->jnl_start * HFSTOVCB(hfsmp)->blockSize) + (off_t)HFSTOVCB(hfsmp)->hfsPlusIOPosOffset;
2260 jnl_size = (off_t)hfsmp->jnl_size;
2261 }
2262
91447636 2263 if ((error = copyout((caddr_t)&jnl_start, CAST_USER_ADDR_T(name[1]), sizeof(off_t))) != 0) {
55e303ae
A
2264 return error;
2265 }
91447636 2266 if ((error = copyout((caddr_t)&jnl_size, CAST_USER_ADDR_T(name[2]), sizeof(off_t))) != 0) {
55e303ae
A
2267 return error;
2268 }
2269
2270 return 0;
2271 } else if (name[0] == HFS_SET_PKG_EXTENSIONS) {
2272
2273 return set_package_extensions_table((void *)name[1], name[2], name[3]);
2274
2275 } else if (name[0] == VFS_CTL_QUERY) {
91447636
A
2276 struct sysctl_req *req;
2277 struct vfsidctl vc;
2278 struct user_vfsidctl user_vc;
2279 struct mount *mp;
2280 struct vfsquery vq;
2281 boolean_t is_64_bit;
2282
2283 is_64_bit = proc_is64bit(p);
2284 req = CAST_DOWN(struct sysctl_req *, oldp); /* we're new style vfs sysctl. */
55e303ae 2285
91447636
A
2286 if (is_64_bit) {
2287 error = SYSCTL_IN(req, &user_vc, sizeof(user_vc));
2288 if (error) return (error);
2289
2290 mp = vfs_getvfs(&user_vc.vc_fsid);
2291 }
2292 else {
2293 error = SYSCTL_IN(req, &vc, sizeof(vc));
2294 if (error) return (error);
2295
2296 mp = vfs_getvfs(&vc.vc_fsid);
2297 }
2298 if (mp == NULL) return (ENOENT);
55e303ae
A
2299
2300 hfsmp = VFSTOHFS(mp);
2301 bzero(&vq, sizeof(vq));
2302 vq.vq_flags = hfsmp->hfs_notification_conditions;
2303 return SYSCTL_OUT(req, &vq, sizeof(vq));;
2d21ac55
A
2304 } else if (name[0] == HFS_REPLAY_JOURNAL) {
2305 char *devnode = NULL;
2306 size_t devnode_len;
2307
2308 devnode_len = *oldlenp;
2309 MALLOC(devnode, char *, devnode_len + 1, M_TEMP, M_WAITOK);
2310 if (devnode == NULL) {
2311 return ENOMEM;
2312 }
2313
2314 error = copyin(oldp, (caddr_t)devnode, devnode_len);
2315 if (error) {
2316 FREE(devnode, M_TEMP);
2317 return error;
2318 }
2319 devnode[devnode_len] = 0;
2320
2321 error = hfs_journal_replay(devnode, context);
2322 FREE(devnode, M_TEMP);
2323 return error;
2324 }
9bccf70c 2325
91447636 2326 return (ENOTSUP);
1c79356b
A
2327}
2328
935ed37a
A
2329/* hfs_vfs_vget is not static since it is used in hfs_readwrite.c to support the
2330 * build_path ioctl. We use it to leverage the code below that updates the origin
2331 * cache if necessary.
2332 */
2333int
91447636
A
2334hfs_vfs_vget(struct mount *mp, ino64_t ino, struct vnode **vpp, __unused vfs_context_t context)
2335{
2d21ac55 2336 int error;
935ed37a
A
2337 int lockflags;
2338 struct hfsmount *hfsmp;
2339
2340 hfsmp = VFSTOHFS(mp);
2d21ac55 2341
935ed37a 2342 error = hfs_vget(hfsmp, (cnid_t)ino, vpp, 1);
2d21ac55
A
2343 if (error)
2344 return (error);
2345
2346 /*
2347 * ADLs may need to have their origin state updated
935ed37a
A
2348 * since build_path needs a valid parent. The same is true
2349 * for hardlinked files as well. There isn't a race window here in re-acquiring
2350 * the cnode lock since we aren't pulling any data out of the cnode; instead, we're
2351 * going back to the catalog.
2d21ac55 2352 */
935ed37a 2353 if ((VTOC(*vpp)->c_flag & C_HARDLINK) &&
2d21ac55
A
2354 (hfs_lock(VTOC(*vpp), HFS_EXCLUSIVE_LOCK) == 0)) {
2355 cnode_t *cp = VTOC(*vpp);
2356 struct cat_desc cdesc;
2357
935ed37a
A
2358 if (!hfs_haslinkorigin(cp)) {
2359 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
2360 error = cat_findname(hfsmp, (cnid_t)ino, &cdesc);
2361 hfs_systemfile_unlock(hfsmp, lockflags);
2362 if (error == 0) {
2363 if ((cdesc.cd_parentcnid !=
2364 hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) &&
2365 (cdesc.cd_parentcnid !=
2366 hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid)) {
2367 hfs_savelinkorigin(cp, cdesc.cd_parentcnid);
2368 }
2369 cat_releasedesc(&cdesc);
2d21ac55 2370 }
2d21ac55
A
2371 }
2372 hfs_unlock(cp);
2373 }
2374 return (0);
91447636
A
2375}
2376
2377
2378/*
2379 * Look up an HFS object by ID.
2380 *
2381 * The object is returned with an iocount reference and the cnode locked.
2382 *
2383 * If the object is a file then it will represent the data fork.
2384 */
2385__private_extern__
2386int
2387hfs_vget(struct hfsmount *hfsmp, cnid_t cnid, struct vnode **vpp, int skiplock)
9bccf70c 2388{
2d21ac55 2389 struct vnode *vp = NULLVP;
91447636
A
2390 struct cat_desc cndesc;
2391 struct cat_attr cnattr;
2392 struct cat_fork cnfork;
743b1565 2393 u_int32_t linkref = 0;
91447636 2394 int error;
9bccf70c
A
2395
2396 /* Check for cnids that should't be exported. */
2d21ac55
A
2397 if ((cnid < kHFSFirstUserCatalogNodeID) &&
2398 (cnid != kHFSRootFolderID && cnid != kHFSRootParentID)) {
9bccf70c 2399 return (ENOENT);
2d21ac55
A
2400 }
2401 /* Don't export our private directories. */
2402 if (cnid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid ||
2403 cnid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) {
9bccf70c 2404 return (ENOENT);
2d21ac55 2405 }
91447636
A
2406 /*
2407 * Check the hash first
2408 */
2409 vp = hfs_chash_getvnode(hfsmp->hfs_raw_dev, cnid, 0, skiplock);
2410 if (vp) {
2411 *vpp = vp;
2412 return(0);
2413 }
9bccf70c 2414
91447636
A
2415 bzero(&cndesc, sizeof(cndesc));
2416 bzero(&cnattr, sizeof(cnattr));
2417 bzero(&cnfork, sizeof(cnfork));
55e303ae 2418
91447636
A
2419 /*
2420 * Not in hash, lookup in catalog
2421 */
2422 if (cnid == kHFSRootParentID) {
2423 static char hfs_rootname[] = "/";
2424
2d21ac55 2425 cndesc.cd_nameptr = (const u_int8_t *)&hfs_rootname[0];
91447636
A
2426 cndesc.cd_namelen = 1;
2427 cndesc.cd_parentcnid = kHFSRootParentID;
2428 cndesc.cd_cnid = kHFSRootFolderID;
2429 cndesc.cd_flags = CD_ISDIR;
2430
2431 cnattr.ca_fileid = kHFSRootFolderID;
2d21ac55 2432 cnattr.ca_linkcount = 1;
91447636 2433 cnattr.ca_entries = 1;
2d21ac55 2434 cnattr.ca_dircount = 1;
91447636
A
2435 cnattr.ca_mode = (S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
2436 } else {
2437 int lockflags;
2d21ac55
A
2438 cnid_t pid;
2439 const char *nameptr;
55e303ae 2440
91447636 2441 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
2d21ac55 2442 error = cat_idlookup(hfsmp, cnid, 0, &cndesc, &cnattr, &cnfork);
91447636
A
2443 hfs_systemfile_unlock(hfsmp, lockflags);
2444
2445 if (error) {
2446 *vpp = NULL;
2447 return (error);
55e303ae 2448 }
91447636 2449
743b1565 2450 /*
2d21ac55 2451 * Check for a raw hardlink inode and save its linkref.
743b1565 2452 */
2d21ac55
A
2453 pid = cndesc.cd_parentcnid;
2454 nameptr = (const char *)cndesc.cd_nameptr;
2455
2456 if ((pid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid) &&
2457 (bcmp(nameptr, HFS_INODE_PREFIX, HFS_INODE_PREFIX_LEN) == 0)) {
2458 linkref = strtoul(&nameptr[HFS_INODE_PREFIX_LEN], NULL, 10);
2459
2460 } else if ((pid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) &&
2461 (bcmp(nameptr, HFS_DIRINODE_PREFIX, HFS_DIRINODE_PREFIX_LEN) == 0)) {
2462 linkref = strtoul(&nameptr[HFS_DIRINODE_PREFIX_LEN], NULL, 10);
2463
2464 } else if ((pid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid) &&
2465 (bcmp(nameptr, HFS_DELETE_PREFIX, HFS_DELETE_PREFIX_LEN) == 0)) {
2466 *vpp = NULL;
4a3eedf9 2467 cat_releasedesc(&cndesc);
2d21ac55 2468 return (ENOENT); /* open unlinked file */
91447636
A
2469 }
2470 }
2471
2472 /*
2d21ac55
A
2473 * Finish initializing cnode descriptor for hardlinks.
2474 *
2475 * We need a valid name and parent for reverse lookups.
91447636 2476 */
2d21ac55
A
2477 if (linkref) {
2478 cnid_t nextlinkid;
2479 cnid_t prevlinkid;
2480 struct cat_desc linkdesc;
935ed37a 2481 int lockflags;
2d21ac55
A
2482
2483 cnattr.ca_linkref = linkref;
91447636 2484
2d21ac55
A
2485 /*
2486 * Pick up the first link in the chain and get a descriptor for it.
2487 * This allows blind volfs paths to work for hardlinks.
2488 */
2489 if ((hfs_lookuplink(hfsmp, linkref, &prevlinkid, &nextlinkid) == 0) &&
2490 (nextlinkid != 0)) {
935ed37a
A
2491 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
2492 error = cat_findname(hfsmp, nextlinkid, &linkdesc);
2493 hfs_systemfile_unlock(hfsmp, lockflags);
2494 if (error == 0) {
2d21ac55
A
2495 cat_releasedesc(&cndesc);
2496 bcopy(&linkdesc, &cndesc, sizeof(linkdesc));
2497 }
2498 }
743b1565
A
2499 }
2500
2d21ac55
A
2501 if (linkref) {
2502 error = hfs_getnewvnode(hfsmp, NULL, NULL, &cndesc, 0, &cnattr, &cnfork, &vp);
2503 if (error == 0) {
2504 VTOC(vp)->c_flag |= C_HARDLINK;
2505 vnode_setmultipath(vp);
2506 }
2507 } else {
2508 struct componentname cn;
2509
2510 /* Supply hfs_getnewvnode with a component name. */
2511 MALLOC_ZONE(cn.cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
2512 cn.cn_nameiop = LOOKUP;
2513 cn.cn_flags = ISLASTCN | HASBUF;
2514 cn.cn_context = NULL;
2515 cn.cn_pnlen = MAXPATHLEN;
2516 cn.cn_nameptr = cn.cn_pnbuf;
2517 cn.cn_namelen = cndesc.cd_namelen;
2518 cn.cn_hash = 0;
2519 cn.cn_consume = 0;
2520 bcopy(cndesc.cd_nameptr, cn.cn_nameptr, cndesc.cd_namelen + 1);
2521
2522 error = hfs_getnewvnode(hfsmp, NULLVP, &cn, &cndesc, 0, &cnattr, &cnfork, &vp);
91447636 2523
935ed37a 2524 if ((error == 0) && (VTOC(vp)->c_flag & C_HARDLINK)) {
2d21ac55
A
2525 hfs_savelinkorigin(VTOC(vp), cndesc.cd_parentcnid);
2526 }
2527 FREE_ZONE(cn.cn_pnbuf, cn.cn_pnlen, M_NAMEI);
2528 }
91447636 2529 cat_releasedesc(&cndesc);
2d21ac55 2530
91447636 2531 *vpp = vp;
2d21ac55 2532 if (vp && skiplock) {
91447636 2533 hfs_unlock(VTOC(vp));
2d21ac55 2534 }
91447636 2535 return (error);
55e303ae
A
2536}
2537
91447636 2538
9bccf70c
A
2539/*
2540 * Flush out all the files in a filesystem.
2541 */
55e303ae 2542static int
2d21ac55 2543#if QUOTA
9bccf70c 2544hfs_flushfiles(struct mount *mp, int flags, struct proc *p)
2d21ac55
A
2545#else
2546hfs_flushfiles(struct mount *mp, int flags, __unused struct proc *p)
2547#endif /* QUOTA */
1c79356b 2548{
55e303ae
A
2549 struct hfsmount *hfsmp;
2550 struct vnode *skipvp = NULLVP;
2d21ac55
A
2551 int error;
2552#if QUOTA
55e303ae 2553 int quotafilecnt;
9bccf70c 2554 int i;
2d21ac55 2555#endif
1c79356b 2556
9bccf70c 2557 hfsmp = VFSTOHFS(mp);
1c79356b 2558
55e303ae
A
2559#if QUOTA
2560 /*
2561 * The open quota files have an indirect reference on
2562 * the root directory vnode. We must account for this
2563 * extra reference when doing the intial vflush.
2564 */
2565 quotafilecnt = 0;
91447636 2566 if (((unsigned int)vfs_flags(mp)) & MNT_QUOTA) {
55e303ae
A
2567
2568 /* Find out how many quota files we have open. */
2569 for (i = 0; i < MAXQUOTAS; i++) {
2570 if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP)
2571 ++quotafilecnt;
2572 }
2573
2574 /* Obtain the root vnode so we can skip over it. */
91447636 2575 skipvp = hfs_chash_getvnode(hfsmp->hfs_raw_dev, kHFSRootFolderID, 0, 0);
55e303ae
A
2576 }
2577#endif /* QUOTA */
2578
2579 error = vflush(mp, skipvp, SKIPSYSTEM | SKIPSWAP | flags);
91447636
A
2580 if (error != 0)
2581 return(error);
2582
55e303ae
A
2583 error = vflush(mp, skipvp, SKIPSYSTEM | flags);
2584
2585#if QUOTA
91447636 2586 if (((unsigned int)vfs_flags(mp)) & MNT_QUOTA) {
55e303ae
A
2587 if (skipvp) {
2588 /*
2589 * See if there are additional references on the
2590 * root vp besides the ones obtained from the open
91447636 2591 * quota files and the hfs_chash_getvnode call above.
55e303ae
A
2592 */
2593 if ((error == 0) &&
91447636 2594 (vnode_isinuse(skipvp, quotafilecnt))) {
55e303ae
A
2595 error = EBUSY; /* root directory is still open */
2596 }
91447636
A
2597 hfs_unlock(VTOC(skipvp));
2598 vnode_put(skipvp);
55e303ae
A
2599 }
2600 if (error && (flags & FORCECLOSE) == 0)
9bccf70c 2601 return (error);
55e303ae 2602
9bccf70c
A
2603 for (i = 0; i < MAXQUOTAS; i++) {
2604 if (hfsmp->hfs_qfiles[i].qf_vp == NULLVP)
2605 continue;
2606 hfs_quotaoff(p, mp, i);
2607 }
55e303ae 2608 error = vflush(mp, NULLVP, SKIPSYSTEM | flags);
1c79356b 2609 }
9bccf70c 2610#endif /* QUOTA */
1c79356b 2611
9bccf70c
A
2612 return (error);
2613}
1c79356b 2614
9bccf70c
A
2615/*
2616 * Update volume encoding bitmap (HFS Plus only)
2617 */
2618__private_extern__
2619void
2620hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding)
2621{
2622#define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */
2623#define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */
2624
2d21ac55 2625 u_int32_t index;
9bccf70c
A
2626
2627 switch (encoding) {
2628 case kTextEncodingMacUkrainian:
2629 index = kIndexMacUkrainian;
2630 break;
2631 case kTextEncodingMacFarsi:
2632 index = kIndexMacFarsi;
2633 break;
2634 default:
2635 index = encoding;
2636 break;
2637 }
1c79356b 2638
2d21ac55 2639 if (index < 64 && (hfsmp->encodingsBitmap & (u_int64_t)(1ULL << index)) == 0) {
91447636
A
2640 HFS_MOUNT_LOCK(hfsmp, TRUE)
2641 hfsmp->encodingsBitmap |= (u_int64_t)(1ULL << index);
2d21ac55 2642 MarkVCBDirty(hfsmp);
91447636 2643 HFS_MOUNT_UNLOCK(hfsmp, TRUE);
9bccf70c 2644 }
1c79356b
A
2645}
2646
2647/*
9bccf70c 2648 * Update volume stats
91447636
A
2649 *
2650 * On journal volumes this will cause a volume header flush
1c79356b 2651 */
9bccf70c 2652__private_extern__
1c79356b 2653int
9bccf70c 2654hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot)
1c79356b 2655{
91447636 2656 struct timeval tv;
1c79356b 2657
91447636
A
2658 microtime(&tv);
2659
2660 lck_mtx_lock(&hfsmp->hfs_mutex);
2661
2d21ac55 2662 MarkVCBDirty(hfsmp);
91447636 2663 hfsmp->hfs_mtime = tv.tv_sec;
9bccf70c
A
2664
2665 switch (op) {
2666 case VOL_UPDATE:
2667 break;
2668 case VOL_MKDIR:
91447636
A
2669 if (hfsmp->hfs_dircount != 0xFFFFFFFF)
2670 ++hfsmp->hfs_dircount;
2671 if (inroot && hfsmp->vcbNmRtDirs != 0xFFFF)
2672 ++hfsmp->vcbNmRtDirs;
9bccf70c
A
2673 break;
2674 case VOL_RMDIR:
91447636
A
2675 if (hfsmp->hfs_dircount != 0)
2676 --hfsmp->hfs_dircount;
2677 if (inroot && hfsmp->vcbNmRtDirs != 0xFFFF)
2678 --hfsmp->vcbNmRtDirs;
9bccf70c
A
2679 break;
2680 case VOL_MKFILE:
91447636
A
2681 if (hfsmp->hfs_filecount != 0xFFFFFFFF)
2682 ++hfsmp->hfs_filecount;
2683 if (inroot && hfsmp->vcbNmFls != 0xFFFF)
2684 ++hfsmp->vcbNmFls;
9bccf70c
A
2685 break;
2686 case VOL_RMFILE:
91447636
A
2687 if (hfsmp->hfs_filecount != 0)
2688 --hfsmp->hfs_filecount;
2689 if (inroot && hfsmp->vcbNmFls != 0xFFFF)
2690 --hfsmp->vcbNmFls;
9bccf70c
A
2691 break;
2692 }
b4c24cb9 2693
91447636
A
2694 lck_mtx_unlock(&hfsmp->hfs_mutex);
2695
b4c24cb9
A
2696 if (hfsmp->jnl) {
2697 hfs_flushvolumeheader(hfsmp, 0, 0);
2698 }
2699
9bccf70c 2700 return (0);
1c79356b
A
2701}
2702
9bccf70c
A
2703
2704static int
2705hfs_flushMDB(struct hfsmount *hfsmp, int waitfor, int altflush)
1c79356b 2706{
9bccf70c
A
2707 ExtendedVCB *vcb = HFSTOVCB(hfsmp);
2708 struct filefork *fp;
1c79356b 2709 HFSMasterDirectoryBlock *mdb;
9bccf70c
A
2710 struct buf *bp = NULL;
2711 int retval;
2712 int sectorsize;
2713 ByteCount namelen;
1c79356b 2714
593a1d5f 2715 sectorsize = hfsmp->hfs_logical_block_size;
91447636 2716 retval = (int)buf_bread(hfsmp->hfs_devvp, (daddr64_t)HFS_PRI_SECTOR(sectorsize), sectorsize, NOCRED, &bp);
1c79356b 2717 if (retval) {
9bccf70c 2718 if (bp)
91447636 2719 buf_brelse(bp);
1c79356b
A
2720 return retval;
2721 }
2722
91447636 2723 lck_mtx_lock(&hfsmp->hfs_mutex);
b4c24cb9 2724
91447636 2725 mdb = (HFSMasterDirectoryBlock *)(buf_dataptr(bp) + HFS_PRI_OFFSET(sectorsize));
1c79356b 2726
9bccf70c
A
2727 mdb->drCrDate = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbCrDate)));
2728 mdb->drLsMod = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbLsMod)));
2729 mdb->drAtrb = SWAP_BE16 (vcb->vcbAtrb);
1c79356b
A
2730 mdb->drNmFls = SWAP_BE16 (vcb->vcbNmFls);
2731 mdb->drAllocPtr = SWAP_BE16 (vcb->nextAllocation);
2732 mdb->drClpSiz = SWAP_BE32 (vcb->vcbClpSiz);
2733 mdb->drNxtCNID = SWAP_BE32 (vcb->vcbNxtCNID);
2734 mdb->drFreeBks = SWAP_BE16 (vcb->freeBlocks);
2735
2d21ac55 2736 namelen = strlen((char *)vcb->vcbVN);
1c79356b
A
2737 retval = utf8_to_hfs(vcb, namelen, vcb->vcbVN, mdb->drVN);
2738 /* Retry with MacRoman in case that's how it was exported. */
2739 if (retval)
2740 retval = utf8_to_mac_roman(namelen, vcb->vcbVN, mdb->drVN);
2741
9bccf70c 2742 mdb->drVolBkUp = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbVolBkUp)));
1c79356b
A
2743 mdb->drWrCnt = SWAP_BE32 (vcb->vcbWrCnt);
2744 mdb->drNmRtDirs = SWAP_BE16 (vcb->vcbNmRtDirs);
2745 mdb->drFilCnt = SWAP_BE32 (vcb->vcbFilCnt);
2746 mdb->drDirCnt = SWAP_BE32 (vcb->vcbDirCnt);
2747
2748 bcopy(vcb->vcbFndrInfo, mdb->drFndrInfo, sizeof(mdb->drFndrInfo));
2749
9bccf70c
A
2750 fp = VTOF(vcb->extentsRefNum);
2751 mdb->drXTExtRec[0].startBlock = SWAP_BE16 (fp->ff_extents[0].startBlock);
2752 mdb->drXTExtRec[0].blockCount = SWAP_BE16 (fp->ff_extents[0].blockCount);
2753 mdb->drXTExtRec[1].startBlock = SWAP_BE16 (fp->ff_extents[1].startBlock);
2754 mdb->drXTExtRec[1].blockCount = SWAP_BE16 (fp->ff_extents[1].blockCount);
2755 mdb->drXTExtRec[2].startBlock = SWAP_BE16 (fp->ff_extents[2].startBlock);
2756 mdb->drXTExtRec[2].blockCount = SWAP_BE16 (fp->ff_extents[2].blockCount);
2757 mdb->drXTFlSize = SWAP_BE32 (fp->ff_blocks * vcb->blockSize);
2758 mdb->drXTClpSiz = SWAP_BE32 (fp->ff_clumpsize);
91447636 2759 FTOC(fp)->c_flag &= ~C_MODIFIED;
1c79356b 2760
9bccf70c
A
2761 fp = VTOF(vcb->catalogRefNum);
2762 mdb->drCTExtRec[0].startBlock = SWAP_BE16 (fp->ff_extents[0].startBlock);
2763 mdb->drCTExtRec[0].blockCount = SWAP_BE16 (fp->ff_extents[0].blockCount);
2764 mdb->drCTExtRec[1].startBlock = SWAP_BE16 (fp->ff_extents[1].startBlock);
2765 mdb->drCTExtRec[1].blockCount = SWAP_BE16 (fp->ff_extents[1].blockCount);
2766 mdb->drCTExtRec[2].startBlock = SWAP_BE16 (fp->ff_extents[2].startBlock);
2767 mdb->drCTExtRec[2].blockCount = SWAP_BE16 (fp->ff_extents[2].blockCount);
2768 mdb->drCTFlSize = SWAP_BE32 (fp->ff_blocks * vcb->blockSize);
2769 mdb->drCTClpSiz = SWAP_BE32 (fp->ff_clumpsize);
91447636
A
2770 FTOC(fp)->c_flag &= ~C_MODIFIED;
2771
2772 MarkVCBClean( vcb );
2773
2774 lck_mtx_unlock(&hfsmp->hfs_mutex);
9bccf70c
A
2775
2776 /* If requested, flush out the alternate MDB */
2777 if (altflush) {
2778 struct buf *alt_bp = NULL;
9bccf70c 2779
91447636
A
2780 if (buf_meta_bread(hfsmp->hfs_devvp, hfsmp->hfs_alt_id_sector, sectorsize, NOCRED, &alt_bp) == 0) {
2781 bcopy(mdb, (char *)buf_dataptr(alt_bp) + HFS_ALT_OFFSET(sectorsize), kMDBSize);
b4c24cb9 2782
91447636 2783 (void) VNOP_BWRITE(alt_bp);
9bccf70c 2784 } else if (alt_bp)
91447636 2785 buf_brelse(alt_bp);
9bccf70c 2786 }
1c79356b 2787
9bccf70c 2788 if (waitfor != MNT_WAIT)
91447636 2789 buf_bawrite(bp);
b4c24cb9 2790 else
91447636 2791 retval = VNOP_BWRITE(bp);
1c79356b
A
2792
2793 return (retval);
2794}
2795
55e303ae
A
2796/*
2797 * Flush any dirty in-memory mount data to the on-disk
2798 * volume header.
2799 *
2800 * Note: the on-disk volume signature is intentionally
2801 * not flushed since the on-disk "H+" and "HX" signatures
2802 * are always stored in-memory as "H+".
2803 */
9bccf70c
A
2804__private_extern__
2805int
2806hfs_flushvolumeheader(struct hfsmount *hfsmp, int waitfor, int altflush)
1c79356b 2807{
9bccf70c
A
2808 ExtendedVCB *vcb = HFSTOVCB(hfsmp);
2809 struct filefork *fp;
2810 HFSPlusVolumeHeader *volumeHeader;
2811 int retval;
2812 struct buf *bp;
2813 int i;
91447636 2814 daddr64_t priIDSector;
2d21ac55 2815 int critical;
55e303ae 2816 u_int16_t signature;
91447636 2817 u_int16_t hfsversion;
1c79356b 2818
55e303ae
A
2819 if (hfsmp->hfs_flags & HFS_READ_ONLY) {
2820 return(0);
2821 }
2d21ac55 2822 if (hfsmp->hfs_flags & HFS_STANDARD) {
9bccf70c 2823 return hfs_flushMDB(hfsmp, waitfor, altflush);
2d21ac55
A
2824 }
2825 critical = altflush;
593a1d5f
A
2826 priIDSector = (daddr64_t)((vcb->hfsPlusIOPosOffset / hfsmp->hfs_logical_block_size) +
2827 HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size));
d52fe63f 2828
91447636
A
2829 if (hfs_start_transaction(hfsmp) != 0) {
2830 return EINVAL;
b4c24cb9
A
2831 }
2832
593a1d5f
A
2833 retval = (int)buf_meta_bread(hfsmp->hfs_devvp,
2834 HFS_PHYSBLK_ROUNDDOWN(priIDSector, hfsmp->hfs_log_per_phys),
2835 hfsmp->hfs_physical_block_size, NOCRED, &bp);
1c79356b 2836 if (retval) {
9bccf70c 2837 if (bp)
91447636 2838 buf_brelse(bp);
b4c24cb9 2839
91447636 2840 hfs_end_transaction(hfsmp);
b4c24cb9 2841
55e303ae 2842 printf("HFS: err %d reading VH blk (%s)\n", retval, vcb->vcbVN);
9bccf70c 2843 return (retval);
1c79356b
A
2844 }
2845
b4c24cb9
A
2846 if (hfsmp->jnl) {
2847 journal_modify_block_start(hfsmp->jnl, bp);
2848 }
2849
593a1d5f
A
2850 volumeHeader = (HFSPlusVolumeHeader *)((char *)buf_dataptr(bp) +
2851 HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size));
1c79356b 2852
55e303ae
A
2853 /*
2854 * Sanity check what we just read.
2855 */
2856 signature = SWAP_BE16 (volumeHeader->signature);
91447636 2857 hfsversion = SWAP_BE16 (volumeHeader->version);
55e303ae 2858 if ((signature != kHFSPlusSigWord && signature != kHFSXSigWord) ||
91447636 2859 (hfsversion < kHFSPlusVersion) || (hfsversion > 100) ||
55e303ae
A
2860 (SWAP_BE32 (volumeHeader->blockSize) != vcb->blockSize)) {
2861#if 1
2862 panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d",
91447636 2863 vcb->vcbVN, signature, hfsversion,
55e303ae
A
2864 SWAP_BE32 (volumeHeader->blockSize));
2865#endif
2866 printf("HFS: corrupt VH blk (%s)\n", vcb->vcbVN);
91447636 2867 buf_brelse(bp);
55e303ae
A
2868 return (EIO);
2869 }
2870
1c79356b
A
2871 /*
2872 * For embedded HFS+ volumes, update create date if it changed
2873 * (ie from a setattrlist call)
2874 */
9bccf70c
A
2875 if ((vcb->hfsPlusIOPosOffset != 0) &&
2876 (SWAP_BE32 (volumeHeader->createDate) != vcb->localCreateDate)) {
2877 struct buf *bp2;
1c79356b
A
2878 HFSMasterDirectoryBlock *mdb;
2879
593a1d5f
A
2880 retval = (int)buf_meta_bread(hfsmp->hfs_devvp,
2881 HFS_PHYSBLK_ROUNDDOWN(HFS_PRI_SECTOR(hfsmp->hfs_logical_block_size), hfsmp->hfs_log_per_phys),
2882 hfsmp->hfs_physical_block_size, NOCRED, &bp2);
9bccf70c
A
2883 if (retval) {
2884 if (bp2)
91447636 2885 buf_brelse(bp2);
9bccf70c 2886 retval = 0;
1c79356b 2887 } else {
91447636 2888 mdb = (HFSMasterDirectoryBlock *)(buf_dataptr(bp2) +
593a1d5f 2889 HFS_PRI_OFFSET(hfsmp->hfs_physical_block_size));
1c79356b
A
2890
2891 if ( SWAP_BE32 (mdb->drCrDate) != vcb->localCreateDate )
2892 {
b4c24cb9
A
2893 if (hfsmp->jnl) {
2894 journal_modify_block_start(hfsmp->jnl, bp2);
2895 }
2896
1c79356b
A
2897 mdb->drCrDate = SWAP_BE32 (vcb->localCreateDate); /* pick up the new create date */
2898
b4c24cb9 2899 if (hfsmp->jnl) {
2d21ac55 2900 journal_modify_block_end(hfsmp->jnl, bp2, NULL, NULL);
b4c24cb9 2901 } else {
91447636 2902 (void) VNOP_BWRITE(bp2); /* write out the changes */
b4c24cb9 2903 }
1c79356b
A
2904 }
2905 else
2906 {
91447636 2907 buf_brelse(bp2); /* just release it */
1c79356b
A
2908 }
2909 }
9bccf70c 2910 }
1c79356b 2911
2d21ac55 2912 lck_mtx_lock(&hfsmp->hfs_mutex);
91447636 2913
1c79356b 2914 /* Note: only update the lower 16 bits worth of attributes */
91447636
A
2915 volumeHeader->attributes = SWAP_BE32 (vcb->vcbAtrb);
2916 volumeHeader->journalInfoBlock = SWAP_BE32 (vcb->vcbJinfoBlock);
b4c24cb9
A
2917 if (hfsmp->jnl) {
2918 volumeHeader->lastMountedVersion = SWAP_BE32 (kHFSJMountVersion);
2919 } else {
2920 volumeHeader->lastMountedVersion = SWAP_BE32 (kHFSPlusMountVersion);
2921 }
9bccf70c
A
2922 volumeHeader->createDate = SWAP_BE32 (vcb->localCreateDate); /* volume create date is in local time */
2923 volumeHeader->modifyDate = SWAP_BE32 (to_hfs_time(vcb->vcbLsMod));
2924 volumeHeader->backupDate = SWAP_BE32 (to_hfs_time(vcb->vcbVolBkUp));
2925 volumeHeader->fileCount = SWAP_BE32 (vcb->vcbFilCnt);
2926 volumeHeader->folderCount = SWAP_BE32 (vcb->vcbDirCnt);
91447636 2927 volumeHeader->totalBlocks = SWAP_BE32 (vcb->totalBlocks);
9bccf70c
A
2928 volumeHeader->freeBlocks = SWAP_BE32 (vcb->freeBlocks);
2929 volumeHeader->nextAllocation = SWAP_BE32 (vcb->nextAllocation);
2930 volumeHeader->rsrcClumpSize = SWAP_BE32 (vcb->vcbClpSiz);
2931 volumeHeader->dataClumpSize = SWAP_BE32 (vcb->vcbClpSiz);
2932 volumeHeader->nextCatalogID = SWAP_BE32 (vcb->vcbNxtCNID);
2933 volumeHeader->writeCount = SWAP_BE32 (vcb->vcbWrCnt);
2934 volumeHeader->encodingsBitmap = SWAP_BE64 (vcb->encodingsBitmap);
2935
91447636
A
2936 if (bcmp(vcb->vcbFndrInfo, volumeHeader->finderInfo, sizeof(volumeHeader->finderInfo)) != 0) {
2937 bcopy(vcb->vcbFndrInfo, volumeHeader->finderInfo, sizeof(volumeHeader->finderInfo));
9bccf70c 2938 critical = 1;
91447636 2939 }
9bccf70c 2940
2d21ac55
A
2941 /*
2942 * System files are only dirty when altflush is set.
2943 */
2944 if (altflush == 0) {
2945 goto done;
2946 }
2947
9bccf70c
A
2948 /* Sync Extents over-flow file meta data */
2949 fp = VTOF(vcb->extentsRefNum);
91447636
A
2950 if (FTOC(fp)->c_flag & C_MODIFIED) {
2951 for (i = 0; i < kHFSPlusExtentDensity; i++) {
2952 volumeHeader->extentsFile.extents[i].startBlock =
2953 SWAP_BE32 (fp->ff_extents[i].startBlock);
2954 volumeHeader->extentsFile.extents[i].blockCount =
2955 SWAP_BE32 (fp->ff_extents[i].blockCount);
2956 }
2957 volumeHeader->extentsFile.logicalSize = SWAP_BE64 (fp->ff_size);
2958 volumeHeader->extentsFile.totalBlocks = SWAP_BE32 (fp->ff_blocks);
2959 volumeHeader->extentsFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize);
2960 FTOC(fp)->c_flag &= ~C_MODIFIED;
9bccf70c 2961 }
9bccf70c
A
2962
2963 /* Sync Catalog file meta data */
2964 fp = VTOF(vcb->catalogRefNum);
91447636
A
2965 if (FTOC(fp)->c_flag & C_MODIFIED) {
2966 for (i = 0; i < kHFSPlusExtentDensity; i++) {
2967 volumeHeader->catalogFile.extents[i].startBlock =
2968 SWAP_BE32 (fp->ff_extents[i].startBlock);
2969 volumeHeader->catalogFile.extents[i].blockCount =
2970 SWAP_BE32 (fp->ff_extents[i].blockCount);
2971 }
2972 volumeHeader->catalogFile.logicalSize = SWAP_BE64 (fp->ff_size);
2973 volumeHeader->catalogFile.totalBlocks = SWAP_BE32 (fp->ff_blocks);
2974 volumeHeader->catalogFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize);
2975 FTOC(fp)->c_flag &= ~C_MODIFIED;
9bccf70c 2976 }
9bccf70c
A
2977
2978 /* Sync Allocation file meta data */
2979 fp = VTOF(vcb->allocationsRefNum);
91447636
A
2980 if (FTOC(fp)->c_flag & C_MODIFIED) {
2981 for (i = 0; i < kHFSPlusExtentDensity; i++) {
2982 volumeHeader->allocationFile.extents[i].startBlock =
2983 SWAP_BE32 (fp->ff_extents[i].startBlock);
2984 volumeHeader->allocationFile.extents[i].blockCount =
2985 SWAP_BE32 (fp->ff_extents[i].blockCount);
2986 }
2987 volumeHeader->allocationFile.logicalSize = SWAP_BE64 (fp->ff_size);
2988 volumeHeader->allocationFile.totalBlocks = SWAP_BE32 (fp->ff_blocks);
2989 volumeHeader->allocationFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize);
2990 FTOC(fp)->c_flag &= ~C_MODIFIED;
2991 }
2992
2993 /* Sync Attribute file meta data */
2994 if (hfsmp->hfs_attribute_vp) {
2995 fp = VTOF(hfsmp->hfs_attribute_vp);
2996 for (i = 0; i < kHFSPlusExtentDensity; i++) {
2997 volumeHeader->attributesFile.extents[i].startBlock =
2998 SWAP_BE32 (fp->ff_extents[i].startBlock);
2999 volumeHeader->attributesFile.extents[i].blockCount =
3000 SWAP_BE32 (fp->ff_extents[i].blockCount);
3001 }
3002 FTOC(fp)->c_flag &= ~C_MODIFIED;
3003 volumeHeader->attributesFile.logicalSize = SWAP_BE64 (fp->ff_size);
3004 volumeHeader->attributesFile.totalBlocks = SWAP_BE32 (fp->ff_blocks);
3005 volumeHeader->attributesFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize);
3006 }
3007
2d21ac55
A
3008 /* Sync Startup file meta data */
3009 if (hfsmp->hfs_startup_vp) {
3010 fp = VTOF(hfsmp->hfs_startup_vp);
3011 if (FTOC(fp)->c_flag & C_MODIFIED) {
3012 for (i = 0; i < kHFSPlusExtentDensity; i++) {
3013 volumeHeader->startupFile.extents[i].startBlock =
3014 SWAP_BE32 (fp->ff_extents[i].startBlock);
3015 volumeHeader->startupFile.extents[i].blockCount =
3016 SWAP_BE32 (fp->ff_extents[i].blockCount);
3017 }
3018 volumeHeader->startupFile.logicalSize = SWAP_BE64 (fp->ff_size);
3019 volumeHeader->startupFile.totalBlocks = SWAP_BE32 (fp->ff_blocks);
3020 volumeHeader->startupFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize);
3021 FTOC(fp)->c_flag &= ~C_MODIFIED;
3022 }
9bccf70c 3023 }
9bccf70c 3024
2d21ac55
A
3025done:
3026 MarkVCBClean(hfsmp);
3027 lck_mtx_unlock(&hfsmp->hfs_mutex);
3028
9bccf70c 3029 /* If requested, flush out the alternate volume header */
91447636 3030 if (altflush && hfsmp->hfs_alt_id_sector) {
9bccf70c 3031 struct buf *alt_bp = NULL;
9bccf70c 3032
593a1d5f
A
3033 if (buf_meta_bread(hfsmp->hfs_devvp,
3034 HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys),
3035 hfsmp->hfs_physical_block_size, NOCRED, &alt_bp) == 0) {
b4c24cb9
A
3036 if (hfsmp->jnl) {
3037 journal_modify_block_start(hfsmp->jnl, alt_bp);
3038 }
3039
593a1d5f
A
3040 bcopy(volumeHeader, (char *)buf_dataptr(alt_bp) +
3041 HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size),
3042 kMDBSize);
b4c24cb9
A
3043
3044 if (hfsmp->jnl) {
2d21ac55 3045 journal_modify_block_end(hfsmp->jnl, alt_bp, NULL, NULL);
b4c24cb9 3046 } else {
91447636 3047 (void) VNOP_BWRITE(alt_bp);
b4c24cb9 3048 }
9bccf70c 3049 } else if (alt_bp)
91447636 3050 buf_brelse(alt_bp);
9bccf70c
A
3051 }
3052
b4c24cb9 3053 if (hfsmp->jnl) {
2d21ac55 3054 journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL);
b4c24cb9
A
3055 } else {
3056 if (waitfor != MNT_WAIT)
91447636 3057 buf_bawrite(bp);
b4c24cb9 3058 else {
91447636 3059 retval = VNOP_BWRITE(bp);
b4c24cb9
A
3060 /* When critical data changes, flush the device cache */
3061 if (critical && (retval == 0)) {
91447636
A
3062 (void) VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE,
3063 NULL, FWRITE, NULL);
b4c24cb9 3064 }
9bccf70c
A
3065 }
3066 }
91447636 3067 hfs_end_transaction(hfsmp);
1c79356b 3068
1c79356b
A
3069 return (retval);
3070}
3071
3072
55e303ae
A
3073/*
3074 * Extend a file system.
3075 */
91447636
A
3076__private_extern__
3077int
3078hfs_extendfs(struct hfsmount *hfsmp, u_int64_t newsize, vfs_context_t context)
55e303ae 3079{
91447636
A
3080 struct proc *p = vfs_context_proc(context);
3081 kauth_cred_t cred = vfs_context_ucred(context);
55e303ae
A
3082 struct vnode *vp;
3083 struct vnode *devvp;
3084 struct buf *bp;
55e303ae
A
3085 struct filefork *fp = NULL;
3086 ExtendedVCB *vcb;
3087 struct cat_fork forkdata;
3088 u_int64_t oldsize;
3089 u_int64_t newblkcnt;
91447636 3090 u_int64_t prev_phys_block_count;
55e303ae
A
3091 u_int32_t addblks;
3092 u_int64_t sectorcnt;
3093 u_int32_t sectorsize;
593a1d5f 3094 u_int32_t phys_sectorsize;
91447636
A
3095 daddr64_t prev_alt_sector;
3096 daddr_t bitmapblks;
3097 int lockflags;
55e303ae 3098 int error;
2d21ac55
A
3099 int64_t oldBitmapSize;
3100 Boolean usedExtendFileC = false;
3101
55e303ae
A
3102 devvp = hfsmp->hfs_devvp;
3103 vcb = HFSTOVCB(hfsmp);
3104
3105 /*
3106 * - HFS Plus file systems only.
3107 * - Journaling must be enabled.
3108 * - No embedded volumes.
3109 */
3110 if ((vcb->vcbSigWord == kHFSSigWord) ||
3111 (hfsmp->jnl == NULL) ||
3112 (vcb->hfsPlusIOPosOffset != 0)) {
3113 return (EPERM);
3114 }
3115 /*
3116 * If extending file system by non-root, then verify
3117 * ownership and check permissions.
3118 */
91447636
A
3119 if (suser(cred, NULL)) {
3120 error = hfs_vget(hfsmp, kHFSRootFolderID, &vp, 0);
3121
55e303ae
A
3122 if (error)
3123 return (error);
91447636 3124 error = hfs_owner_rights(hfsmp, VTOC(vp)->c_uid, cred, p, 0);
55e303ae 3125 if (error == 0) {
91447636 3126 error = hfs_write_access(vp, cred, p, false);
55e303ae 3127 }
91447636
A
3128 hfs_unlock(VTOC(vp));
3129 vnode_put(vp);
55e303ae
A
3130 if (error)
3131 return (error);
3132
91447636 3133 error = vnode_authorize(devvp, NULL, KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA, context);
55e303ae
A
3134 if (error)
3135 return (error);
3136 }
91447636 3137 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&sectorsize, 0, context)) {
55e303ae
A
3138 return (ENXIO);
3139 }
593a1d5f 3140 if (sectorsize != hfsmp->hfs_logical_block_size) {
55e303ae
A
3141 return (ENXIO);
3142 }
91447636 3143 if (VNOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&sectorcnt, 0, context)) {
55e303ae
A
3144 return (ENXIO);
3145 }
3146 if ((sectorsize * sectorcnt) < newsize) {
3147 printf("hfs_extendfs: not enough space on device\n");
3148 return (ENOSPC);
3149 }
593a1d5f
A
3150 error = VNOP_IOCTL(devvp, DKIOCGETPHYSICALBLOCKSIZE, (caddr_t)&phys_sectorsize, 0, context);
3151 if (error) {
3152 if ((error != ENOTSUP) && (error != ENOTTY)) {
3153 return (ENXIO);
3154 }
3155 /* If ioctl is not supported, force physical and logical sector size to be same */
3156 phys_sectorsize = sectorsize;
3157 }
3158 if (phys_sectorsize != hfsmp->hfs_physical_block_size) {
3159 return (ENXIO);
3160 }
91447636 3161 oldsize = (u_int64_t)hfsmp->totalBlocks * (u_int64_t)hfsmp->blockSize;
55e303ae
A
3162
3163 /*
3164 * Validate new size.
3165 */
593a1d5f 3166 if ((newsize <= oldsize) || (newsize % sectorsize) || (newsize % phys_sectorsize)) {
55e303ae
A
3167 printf("hfs_extendfs: invalid size\n");
3168 return (EINVAL);
3169 }
3170 newblkcnt = newsize / vcb->blockSize;
3171 if (newblkcnt > (u_int64_t)0xFFFFFFFF)
3172 return (EOVERFLOW);
3173
3174 addblks = newblkcnt - vcb->totalBlocks;
3175
3176 printf("hfs_extendfs: growing %s by %d blocks\n", vcb->vcbVN, addblks);
3177 /*
3178 * Enclose changes inside a transaction.
3179 */
91447636 3180 if (hfs_start_transaction(hfsmp) != 0) {
55e303ae
A
3181 return (EINVAL);
3182 }
3183
2d21ac55
A
3184 /*
3185 * Note: we take the attributes lock in case we have an attribute data vnode
3186 * which needs to change size.
3187 */
3188 lockflags = hfs_systemfile_lock(hfsmp, SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
55e303ae 3189 vp = vcb->allocationsRefNum;
55e303ae
A
3190 fp = VTOF(vp);
3191 bcopy(&fp->ff_data, &forkdata, sizeof(forkdata));
3192
3193 /*
3194 * Calculate additional space required (if any) by allocation bitmap.
3195 */
2d21ac55
A
3196 oldBitmapSize = fp->ff_size;
3197 bitmapblks = roundup((newblkcnt+7) / 8, vcb->vcbVBMIOSize) / vcb->blockSize;
91447636 3198 if (bitmapblks > (daddr_t)fp->ff_blocks)
55e303ae
A
3199 bitmapblks -= fp->ff_blocks;
3200 else
3201 bitmapblks = 0;
3202
3203 if (bitmapblks > 0) {
91447636 3204 daddr64_t blkno;
55e303ae 3205 daddr_t blkcnt;
2d21ac55 3206 off_t bytesAdded;
55e303ae
A
3207
3208 /*
2d21ac55
A
3209 * Get the bitmap's current size (in allocation blocks) so we know
3210 * where to start zero filling once the new space is added. We've
3211 * got to do this before the bitmap is grown.
55e303ae 3212 */
91447636 3213 blkno = (daddr64_t)fp->ff_blocks;
2d21ac55
A
3214
3215 /*
3216 * Try to grow the allocation file in the normal way, using allocation
3217 * blocks already existing in the file system. This way, we might be
3218 * able to grow the bitmap contiguously, or at least in the metadata
3219 * zone.
3220 */
3221 error = ExtendFileC(vcb, fp, bitmapblks * vcb->blockSize, 0,
3222 kEFAllMask | kEFNoClumpMask | kEFReserveMask | kEFMetadataMask,
3223 &bytesAdded);
3224
3225 if (error == 0) {
3226 usedExtendFileC = true;
3227 } else {
3228 /*
3229 * If the above allocation failed, fall back to allocating the new
3230 * extent of the bitmap from the space we're going to add. Since those
3231 * blocks don't yet belong to the file system, we have to update the
3232 * extent list directly, and manually adjust the file size.
3233 */
3234 bytesAdded = 0;
3235 error = AddFileExtent(vcb, fp, vcb->totalBlocks, bitmapblks);
3236 if (error) {
3237 printf("hfs_extendfs: error %d adding extents\n", error);
3238 goto out;
3239 }
3240 fp->ff_blocks += bitmapblks;
3241 VTOC(vp)->c_blocks = fp->ff_blocks;
3242 VTOC(vp)->c_flag |= C_MODIFIED;
3243 }
3244
3245 /*
3246 * Update the allocation file's size to include the newly allocated
3247 * blocks. Note that ExtendFileC doesn't do this, which is why this
3248 * statement is outside the above "if" statement.
3249 */
55e303ae 3250 fp->ff_size += (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize;
2d21ac55 3251
55e303ae
A
3252 /*
3253 * Zero out the new bitmap blocks.
3254 */
3255 {
3256
3257 bp = NULL;
2d21ac55 3258 blkcnt = bitmapblks;
55e303ae 3259 while (blkcnt > 0) {
91447636 3260 error = (int)buf_meta_bread(vp, blkno, vcb->blockSize, NOCRED, &bp);
55e303ae
A
3261 if (error) {
3262 if (bp) {
91447636 3263 buf_brelse(bp);
55e303ae
A
3264 }
3265 break;
3266 }
91447636
A
3267 bzero((char *)buf_dataptr(bp), vcb->blockSize);
3268 buf_markaged(bp);
3269 error = (int)buf_bwrite(bp);
55e303ae
A
3270 if (error)
3271 break;
3272 --blkcnt;
3273 ++blkno;
3274 }
3275 }
3276 if (error) {
3277 printf("hfs_extendfs: error %d clearing blocks\n", error);
3278 goto out;
3279 }
3280 /*
3281 * Mark the new bitmap space as allocated.
2d21ac55
A
3282 *
3283 * Note that ExtendFileC will have marked any blocks it allocated, so
3284 * this is only needed if we used AddFileExtent. Also note that this
3285 * has to come *after* the zero filling of new blocks in the case where
3286 * we used AddFileExtent (since the part of the bitmap we're touching
3287 * is in those newly allocated blocks).
55e303ae 3288 */
2d21ac55
A
3289 if (!usedExtendFileC) {
3290 error = BlockMarkAllocated(vcb, vcb->totalBlocks, bitmapblks);
3291 if (error) {
3292 printf("hfs_extendfs: error %d setting bitmap\n", error);
3293 goto out;
3294 }
3295 vcb->freeBlocks -= bitmapblks;
55e303ae
A
3296 }
3297 }
3298 /*
3299 * Mark the new alternate VH as allocated.
3300 */
3301 if (vcb->blockSize == 512)
3302 error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 2, 2);
3303 else
3304 error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 1, 1);
3305 if (error) {
3306 printf("hfs_extendfs: error %d setting bitmap (VH)\n", error);
3307 goto out;
3308 }
3309 /*
3310 * Mark the old alternate VH as free.
3311 */
3312 if (vcb->blockSize == 512)
3313 (void) BlockMarkFree(vcb, vcb->totalBlocks - 2, 2);
3314 else
3315 (void) BlockMarkFree(vcb, vcb->totalBlocks - 1, 1);
55e303ae
A
3316 /*
3317 * Adjust file system variables for new space.
3318 */
593a1d5f 3319 prev_phys_block_count = hfsmp->hfs_logical_block_count;
91447636
A
3320 prev_alt_sector = hfsmp->hfs_alt_id_sector;
3321
55e303ae 3322 vcb->totalBlocks += addblks;
2d21ac55 3323 vcb->freeBlocks += addblks;
593a1d5f 3324 hfsmp->hfs_logical_block_count = newsize / sectorsize;
91447636 3325 hfsmp->hfs_alt_id_sector = (hfsmp->hfsPlusIOPosOffset / sectorsize) +
593a1d5f 3326 HFS_ALT_SECTOR(sectorsize, hfsmp->hfs_logical_block_count);
55e303ae
A
3327 MarkVCBDirty(vcb);
3328 error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH);
3329 if (error) {
3330 printf("hfs_extendfs: couldn't flush volume headers (%d)", error);
3331 /*
3332 * Restore to old state.
3333 */
2d21ac55
A
3334 if (usedExtendFileC) {
3335 (void) TruncateFileC(vcb, fp, oldBitmapSize, false);
3336 } else {
3337 fp->ff_blocks -= bitmapblks;
3338 fp->ff_size -= (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize;
3339 /*
3340 * No need to mark the excess blocks free since those bitmap blocks
3341 * are no longer part of the bitmap. But we do need to undo the
3342 * effect of the "vcb->freeBlocks -= bitmapblks" above.
3343 */
3344 vcb->freeBlocks += bitmapblks;
3345 }
55e303ae 3346 vcb->totalBlocks -= addblks;
2d21ac55 3347 vcb->freeBlocks -= addblks;
593a1d5f 3348 hfsmp->hfs_logical_block_count = prev_phys_block_count;
91447636 3349 hfsmp->hfs_alt_id_sector = prev_alt_sector;
55e303ae
A
3350 MarkVCBDirty(vcb);
3351 if (vcb->blockSize == 512)
3352 (void) BlockMarkAllocated(vcb, vcb->totalBlocks - 2, 2);
3353 else
3354 (void) BlockMarkAllocated(vcb, vcb->totalBlocks - 1, 1);
3355 goto out;
3356 }
3357 /*
3358 * Invalidate the old alternate volume header.
3359 */
3360 bp = NULL;
91447636 3361 if (prev_alt_sector) {
593a1d5f
A
3362 if (buf_meta_bread(hfsmp->hfs_devvp,
3363 HFS_PHYSBLK_ROUNDDOWN(prev_alt_sector, hfsmp->hfs_log_per_phys),
3364 hfsmp->hfs_physical_block_size, NOCRED, &bp) == 0) {
91447636
A
3365 journal_modify_block_start(hfsmp->jnl, bp);
3366
593a1d5f 3367 bzero((char *)buf_dataptr(bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size), kMDBSize);
91447636 3368
2d21ac55 3369 journal_modify_block_end(hfsmp->jnl, bp, NULL, NULL);
91447636
A
3370 } else if (bp) {
3371 buf_brelse(bp);
3372 }
55e303ae 3373 }
2d21ac55
A
3374
3375 /*
3376 * TODO: Adjust the size of the metadata zone based on new volume size?
3377 */
3378
3379 /*
3380 * Adjust the size of hfsmp->hfs_attrdata_vp
3381 */
3382 if (hfsmp->hfs_attrdata_vp) {
3383 struct cnode *attr_cp;
3384 struct filefork *attr_fp;
3385
3386 if (vnode_get(hfsmp->hfs_attrdata_vp) == 0) {
3387 attr_cp = VTOC(hfsmp->hfs_attrdata_vp);
3388 attr_fp = VTOF(hfsmp->hfs_attrdata_vp);
3389
3390 attr_cp->c_blocks = newblkcnt;
3391 attr_fp->ff_blocks = newblkcnt;
3392 attr_fp->ff_extents[0].blockCount = newblkcnt;
3393 attr_fp->ff_size = (off_t) newblkcnt * hfsmp->blockSize;
3394 ubc_setsize(hfsmp->hfs_attrdata_vp, attr_fp->ff_size);
3395 vnode_put(hfsmp->hfs_attrdata_vp);
3396 }
3397 }
3398
55e303ae
A
3399out:
3400 if (error && fp) {
3401 /* Restore allocation fork. */
3402 bcopy(&forkdata, &fp->ff_data, sizeof(forkdata));
3403 VTOC(vp)->c_blocks = fp->ff_blocks;
3404
3405 }
4a3eedf9
A
3406 /*
3407 Regardless of whether or not the totalblocks actually increased,
3408 we should reset the allocLimit field. If it changed, it will
3409 get updated; if not, it will remain the same.
3410 */
3411 hfsmp->allocLimit = vcb->totalBlocks;
91447636
A
3412 hfs_systemfile_unlock(hfsmp, lockflags);
3413 hfs_end_transaction(hfsmp);
3414
3415 return (error);
3416}
3417
3418#define HFS_MIN_SIZE (32LL * 1024LL * 1024LL)
3419
3420/*
3421 * Truncate a file system (while still mounted).
3422 */
3423__private_extern__
3424int
2d21ac55 3425hfs_truncatefs(struct hfsmount *hfsmp, u_int64_t newsize, vfs_context_t context)
91447636 3426{
91447636
A
3427 struct buf *bp = NULL;
3428 u_int64_t oldsize;
3429 u_int32_t newblkcnt;
2d21ac55 3430 u_int32_t reclaimblks = 0;
91447636
A
3431 int lockflags = 0;
3432 int transaction_begun = 0;
3433 int error;
3434
0c530ab8
A
3435 lck_mtx_lock(&hfsmp->hfs_mutex);
3436 if (hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) {
3437 lck_mtx_unlock(&hfsmp->hfs_mutex);
3438 return (EALREADY);
91447636 3439 }
0c530ab8
A
3440 hfsmp->hfs_flags |= HFS_RESIZE_IN_PROGRESS;
3441 hfsmp->hfs_resize_filesmoved = 0;
3442 hfsmp->hfs_resize_totalfiles = 0;
3443 lck_mtx_unlock(&hfsmp->hfs_mutex);
3444
91447636 3445 /*
0c530ab8 3446 * - Journaled HFS Plus volumes only.
91447636
A
3447 * - No embedded volumes.
3448 */
0c530ab8 3449 if ((hfsmp->jnl == NULL) ||
91447636
A
3450 (hfsmp->hfsPlusIOPosOffset != 0)) {
3451 error = EPERM;
3452 goto out;
3453 }
3454 oldsize = (u_int64_t)hfsmp->totalBlocks * (u_int64_t)hfsmp->blockSize;
3455 newblkcnt = newsize / hfsmp->blockSize;
3456 reclaimblks = hfsmp->totalBlocks - newblkcnt;
3457
3458 /* Make sure new size is valid. */
3459 if ((newsize < HFS_MIN_SIZE) ||
3460 (newsize >= oldsize) ||
593a1d5f
A
3461 (newsize % hfsmp->hfs_logical_block_size) ||
3462 (newsize % hfsmp->hfs_physical_block_size)) {
3463 printf ("hfs_truncatefs: invalid size\n");
91447636
A
3464 error = EINVAL;
3465 goto out;
3466 }
3467 /* Make sure there's enough space to work with. */
0c530ab8 3468 if (reclaimblks >= hfs_freeblks(hfsmp, 1)) {
2d21ac55 3469 printf("hfs_truncatefs: insufficient space (need %u blocks; have %u blocks)\n", reclaimblks, hfs_freeblks(hfsmp, 1));
91447636
A
3470 error = ENOSPC;
3471 goto out;
3472 }
2d21ac55 3473
0c530ab8
A
3474 /* Start with a clean journal. */
3475 journal_flush(hfsmp->jnl);
2d21ac55 3476
91447636
A
3477 if (hfs_start_transaction(hfsmp) != 0) {
3478 error = EINVAL;
3479 goto out;
3480 }
3481 transaction_begun = 1;
3482
3483 /*
2d21ac55
A
3484 * Prevent new allocations from using the part we're trying to truncate.
3485 *
3486 * NOTE: allocLimit is set to the allocation block number where the new
3487 * alternate volume header will be. That way there will be no files to
3488 * interfere with allocating the new alternate volume header, and no files
3489 * in the allocation blocks beyond (i.e. the blocks we're trying to
3490 * truncate away.
3491 */
3492 lck_mtx_lock(&hfsmp->hfs_mutex);
3493 if (hfsmp->blockSize == 512)
3494 hfsmp->allocLimit = newblkcnt - 2;
3495 else
3496 hfsmp->allocLimit = newblkcnt - 1;
3497 hfsmp->freeBlocks -= reclaimblks;
3498 lck_mtx_unlock(&hfsmp->hfs_mutex);
3499
3500 /*
3501 * Look for files that have blocks at or beyond the location of the
3502 * new alternate volume header.
91447636 3503 */
2d21ac55 3504 if (hfs_isallocated(hfsmp, hfsmp->allocLimit, reclaimblks)) {
91447636
A
3505 /*
3506 * hfs_reclaimspace will use separate transactions when
3507 * relocating files (so we don't overwhelm the journal).
3508 */
3509 hfs_end_transaction(hfsmp);
3510 transaction_begun = 0;
3511
3512 /* Attempt to reclaim some space. */
2d21ac55 3513 if (hfs_reclaimspace(hfsmp, hfsmp->allocLimit, reclaimblks, context) != 0) {
91447636
A
3514 printf("hfs_truncatefs: couldn't reclaim space on %s\n", hfsmp->vcbVN);
3515 error = ENOSPC;
3516 goto out;
3517 }
3518 if (hfs_start_transaction(hfsmp) != 0) {
3519 error = EINVAL;
3520 goto out;
3521 }
3522 transaction_begun = 1;
3523
3524 /* Check if we're clear now. */
2d21ac55 3525 if (hfs_isallocated(hfsmp, hfsmp->allocLimit, reclaimblks)) {
91447636 3526 printf("hfs_truncatefs: didn't reclaim enough space on %s\n", hfsmp->vcbVN);
0c530ab8 3527 error = EAGAIN; /* tell client to try again */
91447636
A
3528 goto out;
3529 }
3530 }
2d21ac55
A
3531
3532 /*
3533 * Note: we take the attributes lock in case we have an attribute data vnode
3534 * which needs to change size.
3535 */
3536 lockflags = hfs_systemfile_lock(hfsmp, SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
91447636
A
3537
3538 /*
3539 * Mark the old alternate volume header as free.
3540 * We don't bother shrinking allocation bitmap file.
3541 */
2d21ac55 3542 if (hfsmp->blockSize == 512)
91447636
A
3543 (void) BlockMarkFree(hfsmp, hfsmp->totalBlocks - 2, 2);
3544 else
3545 (void) BlockMarkFree(hfsmp, hfsmp->totalBlocks - 1, 1);
3546
3547 /*
2d21ac55 3548 * Allocate last 1KB for alternate volume header.
91447636 3549 */
2d21ac55 3550 error = BlockMarkAllocated(hfsmp, hfsmp->allocLimit, (hfsmp->blockSize == 512) ? 2 : 1);
91447636 3551 if (error) {
2d21ac55 3552 printf("hfs_truncatefs: Error %d allocating new alternate volume header\n", error);
91447636
A
3553 goto out;
3554 }
55e303ae 3555
91447636
A
3556 /*
3557 * Invalidate the existing alternate volume header.
0c530ab8 3558 *
2d21ac55 3559 * Don't include this in a transaction (don't call journal_modify_block)
0c530ab8 3560 * since this block will be outside of the truncated file system!
91447636
A
3561 */
3562 if (hfsmp->hfs_alt_id_sector) {
593a1d5f
A
3563 if (buf_meta_bread(hfsmp->hfs_devvp,
3564 HFS_PHYSBLK_ROUNDDOWN(hfsmp->hfs_alt_id_sector, hfsmp->hfs_log_per_phys),
3565 hfsmp->hfs_physical_block_size, NOCRED, &bp) == 0) {
91447636 3566
593a1d5f 3567 bzero((void*)((char *)buf_dataptr(bp) + HFS_ALT_OFFSET(hfsmp->hfs_physical_block_size)), kMDBSize);
0c530ab8 3568 (void) VNOP_BWRITE(bp);
91447636
A
3569 } else if (bp) {
3570 buf_brelse(bp);
3571 }
3572 bp = NULL;
3573 }
3574
0c530ab8
A
3575 /* Log successful shrinking. */
3576 printf("hfs_truncatefs: shrank \"%s\" to %d blocks (was %d blocks)\n",
3577 hfsmp->vcbVN, newblkcnt, hfsmp->totalBlocks);
3578
91447636
A
3579 /*
3580 * Adjust file system variables and flush them to disk.
3581 */
91447636 3582 hfsmp->totalBlocks = newblkcnt;
593a1d5f
A
3583 hfsmp->hfs_logical_block_count = newsize / hfsmp->hfs_logical_block_size;
3584 hfsmp->hfs_alt_id_sector = HFS_ALT_SECTOR(hfsmp->hfs_logical_block_size, hfsmp->hfs_logical_block_count);
91447636
A
3585 MarkVCBDirty(hfsmp);
3586 error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH);
3587 if (error)
3588 panic("hfs_truncatefs: unexpected error flushing volume header (%d)\n", error);
2d21ac55
A
3589
3590 /*
3591 * TODO: Adjust the size of the metadata zone based on new volume size?
3592 */
3593
3594 /*
3595 * Adjust the size of hfsmp->hfs_attrdata_vp
3596 */
3597 if (hfsmp->hfs_attrdata_vp) {
3598 struct cnode *cp;
3599 struct filefork *fp;
3600
3601 if (vnode_get(hfsmp->hfs_attrdata_vp) == 0) {
3602 cp = VTOC(hfsmp->hfs_attrdata_vp);
3603 fp = VTOF(hfsmp->hfs_attrdata_vp);
3604
3605 cp->c_blocks = newblkcnt;
3606 fp->ff_blocks = newblkcnt;
3607 fp->ff_extents[0].blockCount = newblkcnt;
3608 fp->ff_size = (off_t) newblkcnt * hfsmp->blockSize;
3609 ubc_setsize(hfsmp->hfs_attrdata_vp, fp->ff_size);
3610 vnode_put(hfsmp->hfs_attrdata_vp);
3611 }
3612 }
3613
91447636 3614out:
2d21ac55
A
3615 if (error)
3616 hfsmp->freeBlocks += reclaimblks;
3617
3618 lck_mtx_lock(&hfsmp->hfs_mutex);
3619 hfsmp->allocLimit = hfsmp->totalBlocks;
3620 if (hfsmp->nextAllocation >= hfsmp->allocLimit)
3621 hfsmp->nextAllocation = hfsmp->hfs_metazone_end + 1;
3622 hfsmp->hfs_flags &= ~HFS_RESIZE_IN_PROGRESS;
3623 lck_mtx_unlock(&hfsmp->hfs_mutex);
3624
91447636
A
3625 if (lockflags) {
3626 hfs_systemfile_unlock(hfsmp, lockflags);
3627 }
3628 if (transaction_begun) {
3629 hfs_end_transaction(hfsmp);
0c530ab8 3630 journal_flush(hfsmp->jnl);
91447636 3631 }
0c530ab8 3632
55e303ae
A
3633 return (error);
3634}
3635
0c530ab8 3636
91447636 3637/*
2d21ac55
A
3638 * Invalidate the physical block numbers associated with buffer cache blocks
3639 * in the given extent of the given vnode.
91447636 3640 */
2d21ac55
A
3641struct hfs_inval_blk_no {
3642 daddr64_t sectorStart;
3643 daddr64_t sectorCount;
3644};
91447636 3645static int
2d21ac55 3646hfs_invalidate_block_numbers_callback(buf_t bp, void *args_in)
91447636 3647{
2d21ac55
A
3648 daddr64_t blkno;
3649 struct hfs_inval_blk_no *args;
3650
3651 blkno = buf_blkno(bp);
3652 args = args_in;
3653
3654 if (blkno >= args->sectorStart && blkno < args->sectorStart+args->sectorCount)
3655 buf_setblkno(bp, buf_lblkno(bp));
89b3af67 3656
2d21ac55
A
3657 return BUF_RETURNED;
3658}
3659static void
3660hfs_invalidate_sectors(struct vnode *vp, daddr64_t sectorStart, daddr64_t sectorCount)
3661{
3662 struct hfs_inval_blk_no args;
3663 args.sectorStart = sectorStart;
3664 args.sectorCount = sectorCount;
3665
3666 buf_iterate(vp, hfs_invalidate_block_numbers_callback, BUF_SCAN_DIRTY|BUF_SCAN_CLEAN, &args);
3667}
0c530ab8 3668
2d21ac55
A
3669
3670/*
3671 * Copy the contents of an extent to a new location. Also invalidates the
3672 * physical block number of any buffer cache block in the copied extent
3673 * (so that if the block is written, it will go through VNOP_BLOCKMAP to
3674 * determine the new physical block number).
3675 */
3676static int
3677hfs_copy_extent(
3678 struct hfsmount *hfsmp,
3679 struct vnode *vp, /* The file whose extent is being copied. */
3680 u_int32_t oldStart, /* The start of the source extent. */
3681 u_int32_t newStart, /* The start of the destination extent. */
3682 u_int32_t blockCount, /* The number of allocation blocks to copy. */
3683 vfs_context_t context)
3684{
3685 int err = 0;
3686 size_t bufferSize;
3687 void *buffer = NULL;
3688 struct vfsioattr ioattr;
3689 buf_t bp = NULL;
3690 off_t resid;
3691 size_t ioSize;
3692 u_int32_t ioSizeSectors; /* Device sectors in this I/O */
3693 daddr64_t srcSector, destSector;
593a1d5f 3694 u_int32_t sectorsPerBlock = hfsmp->blockSize / hfsmp->hfs_logical_block_size;
2d21ac55
A
3695
3696 /*
3697 * Sanity check that we have locked the vnode of the file we're copying.
3698 *
3699 * But since hfs_systemfile_lock() doesn't actually take the lock on
3700 * the allocation file if a journal is active, ignore the check if the
3701 * file being copied is the allocation file.
3702 */
3703 struct cnode *cp = VTOC(vp);
3704 if (cp != hfsmp->hfs_allocation_cp && cp->c_lockowner != current_thread())
3705 panic("hfs_copy_extent: vp=%p (cp=%p) not owned?\n", vp, cp);
3706
3707 /*
3708 * Wait for any in-progress writes to this vnode to complete, so that we'll
3709 * be copying consistent bits. (Otherwise, it's possible that an async
3710 * write will complete to the old extent after we read from it. That
3711 * could lead to corruption.)
3712 */
3713 err = vnode_waitforwrites(vp, 0, 0, 0, "hfs_copy_extent");
3714 if (err) {
3715 printf("hfs_copy_extent: Error %d from vnode_waitforwrites\n", err);
3716 return err;
3717 }
3718
3719 /*
3720 * Determine the I/O size to use
3721 *
3722 * NOTE: Many external drives will result in an ioSize of 128KB.
3723 * TODO: Should we use a larger buffer, doing several consecutive
3724 * reads, then several consecutive writes?
3725 */
3726 vfs_ioattr(hfsmp->hfs_mp, &ioattr);
3727 bufferSize = MIN(ioattr.io_maxreadcnt, ioattr.io_maxwritecnt);
3728 if (kmem_alloc(kernel_map, (vm_offset_t*) &buffer, bufferSize))
3729 return ENOMEM;
3730
3731 /* Get a buffer for doing the I/O */
3732 bp = buf_alloc(hfsmp->hfs_devvp);
3733 buf_setdataptr(bp, (uintptr_t)buffer);
3734
3735 resid = (off_t) blockCount * (off_t) hfsmp->blockSize;
593a1d5f
A
3736 srcSector = (daddr64_t) oldStart * hfsmp->blockSize / hfsmp->hfs_logical_block_size;
3737 destSector = (daddr64_t) newStart * hfsmp->blockSize / hfsmp->hfs_logical_block_size;
2d21ac55
A
3738 while (resid > 0) {
3739 ioSize = MIN(bufferSize, resid);
593a1d5f 3740 ioSizeSectors = ioSize / hfsmp->hfs_logical_block_size;
2d21ac55
A
3741
3742 /* Prepare the buffer for reading */
3743 buf_reset(bp, B_READ);
3744 buf_setsize(bp, ioSize);
3745 buf_setcount(bp, ioSize);
3746 buf_setblkno(bp, srcSector);
3747 buf_setlblkno(bp, srcSector);
3748
3749 /* Do the read */
3750 err = VNOP_STRATEGY(bp);
3751 if (!err)
3752 err = buf_biowait(bp);
3753 if (err) {
3754 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (read)\n", err);
3755 break;
91447636 3756 }
2d21ac55
A
3757
3758 /* Prepare the buffer for writing */
3759 buf_reset(bp, B_WRITE);
3760 buf_setsize(bp, ioSize);
3761 buf_setcount(bp, ioSize);
3762 buf_setblkno(bp, destSector);
3763 buf_setlblkno(bp, destSector);
3764 if (journal_uses_fua(hfsmp->jnl))
3765 buf_markfua(bp);
3766
3767 /* Do the write */
3768 vnode_startwrite(hfsmp->hfs_devvp);
3769 err = VNOP_STRATEGY(bp);
3770 if (!err)
3771 err = buf_biowait(bp);
3772 if (err) {
3773 printf("hfs_copy_extent: Error %d from VNOP_STRATEGY (write)\n", err);
3774 break;
0c530ab8 3775 }
2d21ac55
A
3776
3777 resid -= ioSize;
3778 srcSector += ioSizeSectors;
3779 destSector += ioSizeSectors;
3780 }
3781 if (bp)
3782 buf_free(bp);
3783 if (buffer)
3784 kmem_free(kernel_map, (vm_offset_t)buffer, bufferSize);
3785
3786 /* Make sure all writes have been flushed to disk. */
3787 if (!journal_uses_fua(hfsmp->jnl)) {
3788 err = VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, context);
3789 if (err) {
3790 printf("hfs_copy_extent: DKIOCSYNCHRONIZECACHE failed (%d)\n", err);
3791 err = 0; /* Don't fail the copy. */
3792 }
3793 }
3794
3795 if (!err)
3796 hfs_invalidate_sectors(vp, (daddr64_t)oldStart*sectorsPerBlock, (daddr64_t)blockCount*sectorsPerBlock);
3797
3798 return err;
3799}
3800
3801
3802/*
3803 * Reclaim space at the end of a volume, used by a given system file.
3804 *
3805 * This routine attempts to move any extent which contains allocation blocks
3806 * at or after "startblk." A separate transaction is used to do the move.
3807 * The contents of any moved extents are read and written via the volume's
3808 * device vnode -- NOT via "vp." During the move, moved blocks which are part
3809 * of a transaction have their physical block numbers invalidated so they will
3810 * eventually be written to their new locations.
3811 *
3812 * This routine can be used to move overflow extents for the allocation file.
3813 *
3814 * Inputs:
3815 * hfsmp The volume being resized.
3816 * startblk Blocks >= this allocation block need to be moved.
3817 * locks Which locks need to be taken for the given system file.
3818 * vp The vnode for the system file.
3819 *
3820 * Outputs:
3821 * moved Set to true if any extents were moved.
3822 */
3823static int
3824hfs_relocate_callback(__unused HFSPlusExtentKey *key, HFSPlusExtentRecord *record, HFSPlusExtentRecord *state)
3825{
3826 bcopy(state, record, sizeof(HFSPlusExtentRecord));
3827 return 0;
3828}
3829static int
3830hfs_reclaim_sys_file(struct hfsmount *hfsmp, struct vnode *vp, u_long startblk, int locks, Boolean *moved, vfs_context_t context)
3831{
3832 int error;
3833 int lockflags;
3834 int i;
3835 u_long datablks;
3836 u_long block;
3837 u_int32_t oldStartBlock;
3838 u_int32_t newStartBlock;
3839 u_int32_t blockCount;
3840 struct filefork *fp;
3841
3842 /* If there is no vnode for this file, then there's nothing to do. */
3843 if (vp == NULL)
3844 return 0;
3845
3846 /* printf("hfs_reclaim_sys_file: %.*s\n", VTOC(vp)->c_desc.cd_namelen, VTOC(vp)->c_desc.cd_nameptr); */
3847
3848 /* We always need the allocation bitmap and extents B-tree */
3849 locks |= SFL_BITMAP | SFL_EXTENTS;
3850
3851 error = hfs_start_transaction(hfsmp);
3852 if (error) {
3853 printf("hfs_reclaim_sys_file: hfs_start_transaction returned %d\n", error);
3854 return error;
0c530ab8 3855 }
2d21ac55
A
3856 lockflags = hfs_systemfile_lock(hfsmp, locks, HFS_EXCLUSIVE_LOCK);
3857 fp = VTOF(vp);
0c530ab8 3858 datablks = 0;
2d21ac55
A
3859
3860 /* Relocate non-overflow extents */
0c530ab8 3861 for (i = 0; i < kHFSPlusExtentDensity; ++i) {
2d21ac55 3862 if (fp->ff_extents[i].blockCount == 0)
0c530ab8 3863 break;
2d21ac55
A
3864 oldStartBlock = fp->ff_extents[i].startBlock;
3865 blockCount = fp->ff_extents[i].blockCount;
3866 datablks += blockCount;
3867 block = oldStartBlock + blockCount;
3868 if (block > startblk) {
3869 error = BlockAllocate(hfsmp, 1, blockCount, blockCount, true, true, &newStartBlock, &blockCount);
3870 if (error) {
3871 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error);
3872 goto fail;
3873 }
3874 if (blockCount != fp->ff_extents[i].blockCount) {
3875 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount, fp->ff_extents[i].blockCount);
3876 goto free_fail;
3877 }
3878 error = hfs_copy_extent(hfsmp, vp, oldStartBlock, newStartBlock, blockCount, context);
3879 if (error) {
3880 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error);
3881 goto free_fail;
3882 }
3883 fp->ff_extents[i].startBlock = newStartBlock;
3884 VTOC(vp)->c_flag |= C_MODIFIED;
3885 *moved = true;
3886 error = BlockDeallocate(hfsmp, oldStartBlock, blockCount);
3887 if (error) {
3888 /* TODO: Mark volume inconsistent? */
3889 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error);
3890 goto fail;
3891 }
3892 error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH);
3893 if (error) {
3894 /* TODO: Mark volume inconsistent? */
3895 printf("hfs_reclaim_sys_file: hfs_flushvolumeheader returned %d\n", error);
3896 goto fail;
3897 }
0c530ab8 3898 }
2d21ac55
A
3899 }
3900
3901 /* Relocate overflow extents (if any) */
3902 if (i == kHFSPlusExtentDensity && fp->ff_blocks > datablks) {
3903 struct BTreeIterator *iterator = NULL;
3904 struct FSBufferDescriptor btdata;
3905 HFSPlusExtentRecord record;
3906 HFSPlusExtentKey *key;
3907 FCB *fcb;
3908 u_int32_t fileID;
3909 u_int8_t forktype;
3910
3911 forktype = VNODE_IS_RSRC(vp) ? 0xFF : 0;
3912 fileID = VTOC(vp)->c_cnid;
3913 if (kmem_alloc(kernel_map, (vm_offset_t*) &iterator, sizeof(*iterator))) {
3914 printf("hfs_reclaim_sys_file: kmem_alloc failed!\n");
3915 error = ENOMEM;
3916 goto fail;
3917 }
3918
3919 bzero(iterator, sizeof(*iterator));
3920 key = (HFSPlusExtentKey *) &iterator->key;
3921 key->keyLength = kHFSPlusExtentKeyMaximumLength;
3922 key->forkType = forktype;
3923 key->fileID = fileID;
3924 key->startBlock = datablks;
3925
3926 btdata.bufferAddress = &record;
3927 btdata.itemSize = sizeof(record);
3928 btdata.itemCount = 1;
3929
3930 fcb = VTOF(hfsmp->hfs_extents_vp);
3931
3932 error = BTSearchRecord(fcb, iterator, &btdata, NULL, iterator);
3933 while (error == 0) {
3934 /* Stop when we encounter a different file or fork. */
3935 if ((key->fileID != fileID) ||
3936 (key->forkType != forktype)) {
3937 break;
3938 }
3939 /*
3940 * Check if the file overlaps target space.
3941 */
3942 for (i = 0; i < kHFSPlusExtentDensity; ++i) {
3943 if (record[i].blockCount == 0) {
3944 goto overflow_done;
3945 }
3946 oldStartBlock = record[i].startBlock;
3947 blockCount = record[i].blockCount;
3948 block = oldStartBlock + blockCount;
3949 if (block > startblk) {
3950 error = BlockAllocate(hfsmp, 1, blockCount, blockCount, true, true, &newStartBlock, &blockCount);
3951 if (error) {
3952 printf("hfs_reclaim_sys_file: BlockAllocate returned %d\n", error);
3953 goto overflow_done;
3954 }
3955 if (blockCount != record[i].blockCount) {
3956 printf("hfs_reclaim_sys_file: new blockCount=%u, original blockCount=%u", blockCount, fp->ff_extents[i].blockCount);
3957 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
3958 goto free_fail;
3959 }
3960 error = hfs_copy_extent(hfsmp, vp, oldStartBlock, newStartBlock, blockCount, context);
3961 if (error) {
3962 printf("hfs_reclaim_sys_file: hfs_copy_extent returned %d\n", error);
3963 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
3964 goto free_fail;
3965 }
3966 record[i].startBlock = newStartBlock;
3967 VTOC(vp)->c_flag |= C_MODIFIED;
3968 *moved = true;
3969 /*
3970 * NOTE: To support relocating overflow extents of the
3971 * allocation file, we must update the BTree record BEFORE
3972 * deallocating the old extent so that BlockDeallocate will
3973 * use the extent's new location to calculate physical block
3974 * numbers. (This is for the case where the old extent's
3975 * bitmap bits actually reside in the extent being moved.)
3976 */
3977 error = BTUpdateRecord(fcb, iterator, (IterateCallBackProcPtr) hfs_relocate_callback, &record);
3978 if (error) {
3979 /* TODO: Mark volume inconsistent? */
3980 printf("hfs_reclaim_sys_file: BTUpdateRecord returned %d\n", error);
3981 goto overflow_done;
3982 }
3983 error = BlockDeallocate(hfsmp, oldStartBlock, blockCount);
3984 if (error) {
3985 /* TODO: Mark volume inconsistent? */
3986 printf("hfs_reclaim_sys_file: BlockDeallocate returned %d\n", error);
3987 goto overflow_done;
3988 }
3989 }
3990 }
3991 /* Look for more records. */
3992 error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL);
3993 if (error == btNotFound) {
3994 error = 0;
3995 break;
3996 }
3997 }
3998overflow_done:
3999 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
4000 if (error) {
4001 goto fail;
0c530ab8
A
4002 }
4003 }
2d21ac55
A
4004
4005 hfs_systemfile_unlock(hfsmp, lockflags);
4006 error = hfs_end_transaction(hfsmp);
4007 if (error) {
4008 printf("hfs_reclaim_sys_file: hfs_end_transaction returned %d\n", error);
4009 }
4010
4011 return error;
4012
4013free_fail:
4014 (void) BlockDeallocate(hfsmp, newStartBlock, blockCount);
4015fail:
4016 (void) hfs_systemfile_unlock(hfsmp, lockflags);
4017 (void) hfs_end_transaction(hfsmp);
4018 return error;
4019}
4020
4021
4022/*
4023 * This journal_relocate callback updates the journal info block to point
4024 * at the new journal location. This write must NOT be done using the
4025 * transaction. We must write the block immediately. We must also force
4026 * it to get to the media so that the new journal location will be seen by
4027 * the replay code before we can safely let journaled blocks be written
4028 * to their normal locations.
4029 *
4030 * The tests for journal_uses_fua below are mildly hacky. Since the journal
4031 * and the file system are both on the same device, I'm leveraging what
4032 * the journal has decided about FUA.
4033 */
4034struct hfs_journal_relocate_args {
4035 struct hfsmount *hfsmp;
4036 vfs_context_t context;
4037 u_int32_t newStartBlock;
4038};
4039
4040static errno_t
4041hfs_journal_relocate_callback(void *_args)
4042{
4043 int error;
4044 struct hfs_journal_relocate_args *args = _args;
4045 struct hfsmount *hfsmp = args->hfsmp;
4046 buf_t bp;
4047 JournalInfoBlock *jibp;
4048
4049 error = buf_meta_bread(hfsmp->hfs_devvp,
593a1d5f 4050 hfsmp->vcbJinfoBlock * (hfsmp->blockSize/hfsmp->hfs_logical_block_size),
2d21ac55
A
4051 hfsmp->blockSize, vfs_context_ucred(args->context), &bp);
4052 if (error) {
4053 printf("hfs_reclaim_journal_file: failed to read JIB (%d)\n", error);
4054 return error;
4055 }
4056 jibp = (JournalInfoBlock*) buf_dataptr(bp);
4057 jibp->offset = SWAP_BE64((u_int64_t)args->newStartBlock * hfsmp->blockSize);
4058 jibp->size = SWAP_BE64(hfsmp->jnl_size);
4059 if (journal_uses_fua(hfsmp->jnl))
4060 buf_markfua(bp);
4061 error = buf_bwrite(bp);
4062 if (error) {
4063 printf("hfs_reclaim_journal_file: failed to write JIB (%d)\n", error);
4064 return error;
4065 }
4066 if (!journal_uses_fua(hfsmp->jnl)) {
4067 error = VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, args->context);
4068 if (error) {
4069 printf("hfs_reclaim_journal_file: DKIOCSYNCHRONIZECACHE failed (%d)\n", error);
4070 error = 0; /* Don't fail the operation. */
0c530ab8 4071 }
91447636
A
4072 }
4073
2d21ac55
A
4074 return error;
4075}
4076
4077
4078static int
4079hfs_reclaim_journal_file(struct hfsmount *hfsmp, vfs_context_t context)
4080{
4081 int error;
4082 int lockflags;
4083 u_int32_t newStartBlock;
4084 u_int32_t oldBlockCount;
4085 u_int32_t newBlockCount;
4086 struct cat_desc journal_desc;
4087 struct cat_attr journal_attr;
4088 struct cat_fork journal_fork;
4089 struct hfs_journal_relocate_args callback_args;
4090
4091 error = hfs_start_transaction(hfsmp);
4092 if (error) {
4093 printf("hfs_reclaim_journal_file: hfs_start_transaction returned %d\n", error);
4094 return error;
4095 }
4096 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
4097
4098 oldBlockCount = hfsmp->jnl_size / hfsmp->blockSize;
4099
4100 /* TODO: Allow the journal to change size based on the new volume size. */
4101 error = BlockAllocate(hfsmp, 1, oldBlockCount, oldBlockCount, true, true, &newStartBlock, &newBlockCount);
4102 if (error) {
4103 printf("hfs_reclaim_journal_file: BlockAllocate returned %d\n", error);
4104 goto fail;
4105 }
4106 if (newBlockCount != oldBlockCount) {
4107 printf("hfs_reclaim_journal_file: newBlockCount != oldBlockCount (%u, %u)\n", newBlockCount, oldBlockCount);
4108 goto free_fail;
4109 }
4110
4111 error = BlockDeallocate(hfsmp, hfsmp->jnl_start, oldBlockCount);
4112 if (error) {
4113 printf("hfs_reclaim_journal_file: BlockDeallocate returned %d\n", error);
4114 goto free_fail;
4115 }
4116
4117 /* Update the catalog record for .journal */
4118 error = cat_idlookup(hfsmp, hfsmp->hfs_jnlfileid, 1, &journal_desc, &journal_attr, &journal_fork);
4119 if (error) {
4120 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error);
4121 goto free_fail;
4122 }
4123 journal_fork.cf_size = newBlockCount * hfsmp->blockSize;
4124 journal_fork.cf_extents[0].startBlock = newStartBlock;
4125 journal_fork.cf_extents[0].blockCount = newBlockCount;
4126 journal_fork.cf_blocks = newBlockCount;
4127 error = cat_update(hfsmp, &journal_desc, &journal_attr, &journal_fork, NULL);
4a3eedf9 4128 cat_releasedesc(&journal_desc); /* all done with cat descriptor */
2d21ac55
A
4129 if (error) {
4130 printf("hfs_reclaim_journal_file: cat_update returned %d\n", error);
4131 goto free_fail;
4132 }
4133 callback_args.hfsmp = hfsmp;
4134 callback_args.context = context;
4135 callback_args.newStartBlock = newStartBlock;
4136
4137 error = journal_relocate(hfsmp->jnl, (off_t)newStartBlock*hfsmp->blockSize,
4138 (off_t)newBlockCount*hfsmp->blockSize, 0,
4139 hfs_journal_relocate_callback, &callback_args);
4140 if (error) {
4141 /* NOTE: journal_relocate will mark the journal invalid. */
4142 printf("hfs_reclaim_journal_file: journal_relocate returned %d\n", error);
4143 goto fail;
4144 }
4145 hfsmp->jnl_start = newStartBlock;
4146 hfsmp->jnl_size = (off_t)newBlockCount * hfsmp->blockSize;
4147
4148 hfs_systemfile_unlock(hfsmp, lockflags);
4149 error = hfs_end_transaction(hfsmp);
4150 if (error) {
4151 printf("hfs_reclaim_journal_file: hfs_end_transaction returned %d\n", error);
4152 }
4153
4154 return error;
4155
4156free_fail:
4157 (void) BlockDeallocate(hfsmp, newStartBlock, newBlockCount);
4158fail:
4159 hfs_systemfile_unlock(hfsmp, lockflags);
4160 (void) hfs_end_transaction(hfsmp);
4161 return error;
4162}
4163
4164
4165/*
4166 * Move the journal info block to a new location. We have to make sure the
4167 * new copy of the journal info block gets to the media first, then change
4168 * the field in the volume header and the catalog record.
4169 */
4170static int
4171hfs_reclaim_journal_info_block(struct hfsmount *hfsmp, vfs_context_t context)
4172{
4173 int error;
4174 int lockflags;
4175 u_int32_t newBlock;
4176 u_int32_t blockCount;
4177 struct cat_desc jib_desc;
4178 struct cat_attr jib_attr;
4179 struct cat_fork jib_fork;
4180 buf_t old_bp, new_bp;
4181
4182 error = hfs_start_transaction(hfsmp);
4183 if (error) {
4184 printf("hfs_reclaim_journal_info_block: hfs_start_transaction returned %d\n", error);
4185 return error;
4186 }
4187 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
4188
4189 error = BlockAllocate(hfsmp, 1, 1, 1, true, true, &newBlock, &blockCount);
4190 if (error) {
4191 printf("hfs_reclaim_journal_info_block: BlockAllocate returned %d\n", error);
4192 goto fail;
4193 }
4194 if (blockCount != 1) {
4195 printf("hfs_reclaim_journal_info_block: blockCount != 1 (%u)\n", blockCount);
4196 goto free_fail;
4197 }
4198 error = BlockDeallocate(hfsmp, hfsmp->vcbJinfoBlock, 1);
4199 if (error) {
4200 printf("hfs_reclaim_journal_info_block: BlockDeallocate returned %d\n", error);
4201 goto free_fail;
4202 }
4203
4204 /* Copy the old journal info block content to the new location */
4205 error = buf_meta_bread(hfsmp->hfs_devvp,
593a1d5f 4206 hfsmp->vcbJinfoBlock * (hfsmp->blockSize/hfsmp->hfs_logical_block_size),
2d21ac55
A
4207 hfsmp->blockSize, vfs_context_ucred(context), &old_bp);
4208 if (error) {
4209 printf("hfs_reclaim_journal_info_block: failed to read JIB (%d)\n", error);
4210 goto free_fail;
4211 }
4212 new_bp = buf_getblk(hfsmp->hfs_devvp,
593a1d5f 4213 newBlock * (hfsmp->blockSize/hfsmp->hfs_logical_block_size),
2d21ac55
A
4214 hfsmp->blockSize, 0, 0, BLK_META);
4215 bcopy((char*)buf_dataptr(old_bp), (char*)buf_dataptr(new_bp), hfsmp->blockSize);
4216 buf_brelse(old_bp);
4217 if (journal_uses_fua(hfsmp->jnl))
4218 buf_markfua(new_bp);
4219 error = buf_bwrite(new_bp);
4220 if (error) {
4221 printf("hfs_reclaim_journal_info_block: failed to write new JIB (%d)\n", error);
4222 goto free_fail;
4223 }
4224 if (!journal_uses_fua(hfsmp->jnl)) {
4225 error = VNOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, context);
4226 if (error) {
4227 printf("hfs_reclaim_journal_info_block: DKIOCSYNCHRONIZECACHE failed (%d)\n", error);
4228 /* Don't fail the operation. */
4229 }
4230 }
4231
4232 /* Update the catalog record for .journal_info_block */
4233 error = cat_idlookup(hfsmp, hfsmp->hfs_jnlinfoblkid, 1, &jib_desc, &jib_attr, &jib_fork);
4234 if (error) {
4235 printf("hfs_reclaim_journal_file: cat_idlookup returned %d\n", error);
4236 goto fail;
4237 }
4238 jib_fork.cf_size = hfsmp->blockSize;
4239 jib_fork.cf_extents[0].startBlock = newBlock;
4240 jib_fork.cf_extents[0].blockCount = 1;
4241 jib_fork.cf_blocks = 1;
4242 error = cat_update(hfsmp, &jib_desc, &jib_attr, &jib_fork, NULL);
4a3eedf9 4243 cat_releasedesc(&jib_desc); /* all done with cat descriptor */
2d21ac55
A
4244 if (error) {
4245 printf("hfs_reclaim_journal_info_block: cat_update returned %d\n", error);
4246 goto fail;
4247 }
4248
4249 /* Update the pointer to the journal info block in the volume header. */
4250 hfsmp->vcbJinfoBlock = newBlock;
4251 error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH);
4252 if (error) {
4253 printf("hfs_reclaim_journal_info_block: hfs_flushvolumeheader returned %d\n", error);
4254 goto fail;
4255 }
4256 hfs_systemfile_unlock(hfsmp, lockflags);
4257 error = hfs_end_transaction(hfsmp);
4258 if (error) {
4259 printf("hfs_reclaim_journal_info_block: hfs_end_transaction returned %d\n", error);
4260 }
4261 error = journal_flush(hfsmp->jnl);
4262 if (error) {
4263 printf("hfs_reclaim_journal_info_block: journal_flush returned %d\n", error);
4264 }
4265 return error;
4266
4267free_fail:
4268 (void) BlockDeallocate(hfsmp, newBlock, blockCount);
4269fail:
4270 hfs_systemfile_unlock(hfsmp, lockflags);
4271 (void) hfs_end_transaction(hfsmp);
4272 return error;
4273}
4274
4275
4276/*
4277 * Reclaim space at the end of a file system.
4278 */
4279static int
4280hfs_reclaimspace(struct hfsmount *hfsmp, u_long startblk, u_long reclaimblks, vfs_context_t context)
4281{
4282 struct vnode *vp = NULL;
4283 FCB *fcb;
4284 struct BTreeIterator * iterator = NULL;
4285 struct FSBufferDescriptor btdata;
4286 struct HFSPlusCatalogFile filerec;
4287 u_int32_t saved_next_allocation;
4288 cnid_t * cnidbufp;
4289 size_t cnidbufsize;
4290 int filecnt = 0;
4291 int maxfilecnt;
4292 u_long block;
4293 u_long datablks;
4294 u_long rsrcblks;
4295 u_long blkstomove = 0;
4296 int lockflags;
4297 int i;
4298 int error;
4299 int lastprogress = 0;
4300 Boolean system_file_moved = false;
4301
4302 /* Relocate extents of the Allocation file if they're in the way. */
4303 error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_allocation_vp, startblk, SFL_BITMAP, &system_file_moved, context);
4304 if (error) {
4305 printf("hfs_reclaimspace: reclaim allocation file returned %d\n", error);
4306 return error;
4307 }
4308 /* Relocate extents of the Extents B-tree if they're in the way. */
4309 error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_extents_vp, startblk, SFL_EXTENTS, &system_file_moved, context);
4310 if (error) {
4311 printf("hfs_reclaimspace: reclaim extents b-tree returned %d\n", error);
4312 return error;
4313 }
4314 /* Relocate extents of the Catalog B-tree if they're in the way. */
4315 error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_catalog_vp, startblk, SFL_CATALOG, &system_file_moved, context);
4316 if (error) {
4317 printf("hfs_reclaimspace: reclaim catalog b-tree returned %d\n", error);
4318 return error;
4319 }
4320 /* Relocate extents of the Attributes B-tree if they're in the way. */
4321 error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_attribute_vp, startblk, SFL_ATTRIBUTE, &system_file_moved, context);
4322 if (error) {
4323 printf("hfs_reclaimspace: reclaim attribute b-tree returned %d\n", error);
4324 return error;
4325 }
4326 /* Relocate extents of the Startup File if there is one and they're in the way. */
4327 error = hfs_reclaim_sys_file(hfsmp, hfsmp->hfs_startup_vp, startblk, SFL_STARTUP, &system_file_moved, context);
4328 if (error) {
4329 printf("hfs_reclaimspace: reclaim startup file returned %d\n", error);
4330 return error;
4331 }
4332
4333 /*
4334 * We need to make sure the alternate volume header gets flushed if we moved
4335 * any extents in the volume header. But we need to do that before
4336 * shrinking the size of the volume, or else the journal code will panic
4337 * with an invalid (too large) block number.
4338 *
4339 * Note that system_file_moved will be set if ANY extent was moved, even
4340 * if it was just an overflow extent. In this case, the journal_flush isn't
4341 * strictly required, but shouldn't hurt.
4342 */
4343 if (system_file_moved)
4344 journal_flush(hfsmp->jnl);
4345
4346 if (hfsmp->jnl_start + (hfsmp->jnl_size / hfsmp->blockSize) > startblk) {
4347 error = hfs_reclaim_journal_file(hfsmp, context);
4348 if (error) {
4349 printf("hfs_reclaimspace: hfs_reclaim_journal_file failed (%d)\n", error);
4350 return error;
4351 }
4352 }
4353
4354 if (hfsmp->vcbJinfoBlock >= startblk) {
4355 error = hfs_reclaim_journal_info_block(hfsmp, context);
4356 if (error) {
4357 printf("hfs_reclaimspace: hfs_reclaim_journal_info_block failed (%d)\n", error);
4358 return error;
4359 }
4360 }
4361
0c530ab8
A
4362 /* For now move a maximum of 250,000 files. */
4363 maxfilecnt = MIN(hfsmp->hfs_filecount, 250000);
4364 maxfilecnt = MIN((u_long)maxfilecnt, reclaimblks);
91447636
A
4365 cnidbufsize = maxfilecnt * sizeof(cnid_t);
4366 if (kmem_alloc(kernel_map, (vm_offset_t *)&cnidbufp, cnidbufsize)) {
4367 return (ENOMEM);
4368 }
4369 if (kmem_alloc(kernel_map, (vm_offset_t *)&iterator, sizeof(*iterator))) {
4370 kmem_free(kernel_map, (vm_offset_t)cnidbufp, cnidbufsize);
4371 return (ENOMEM);
4372 }
4373
4374 saved_next_allocation = hfsmp->nextAllocation;
2d21ac55 4375 HFS_UPDATE_NEXT_ALLOCATION(hfsmp, hfsmp->hfs_metazone_start);
91447636
A
4376
4377 fcb = VTOF(hfsmp->hfs_catalog_vp);
4378 bzero(iterator, sizeof(*iterator));
4379
4380 btdata.bufferAddress = &filerec;
4381 btdata.itemSize = sizeof(filerec);
4382 btdata.itemCount = 1;
4383
0c530ab8
A
4384 /* Keep the Catalog and extents files locked during iteration. */
4385 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG | SFL_EXTENTS, HFS_SHARED_LOCK);
4386
91447636
A
4387 error = BTIterateRecord(fcb, kBTreeFirstRecord, iterator, NULL, NULL);
4388 if (error) {
0c530ab8 4389 goto end_iteration;
91447636 4390 }
91447636
A
4391 /*
4392 * Iterate over all the catalog records looking for files
4393 * that overlap into the space we're trying to free up.
4394 */
4395 for (filecnt = 0; filecnt < maxfilecnt; ) {
4396 error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL);
4397 if (error) {
0c530ab8
A
4398 if (error == fsBTRecordNotFoundErr || error == fsBTEndOfIterationErr) {
4399 error = 0;
4400 }
91447636
A
4401 break;
4402 }
0c530ab8 4403 if (filerec.recordType != kHFSPlusFileRecord) {
91447636 4404 continue;
0c530ab8
A
4405 }
4406 datablks = rsrcblks = 0;
91447636
A
4407 /*
4408 * Check if either fork overlaps target space.
4409 */
4410 for (i = 0; i < kHFSPlusExtentDensity; ++i) {
0c530ab8
A
4411 if (filerec.dataFork.extents[i].blockCount != 0) {
4412 datablks += filerec.dataFork.extents[i].blockCount;
4413 block = filerec.dataFork.extents[i].startBlock +
4414 filerec.dataFork.extents[i].blockCount;
4415 if (block >= startblk) {
4416 if ((filerec.fileID == hfsmp->hfs_jnlfileid) ||
4417 (filerec.fileID == hfsmp->hfs_jnlinfoblkid)) {
4418 printf("hfs_reclaimspace: cannot move active journal\n");
4419 error = EPERM;
4420 goto end_iteration;
4421 }
4422 cnidbufp[filecnt++] = filerec.fileID;
4423 blkstomove += filerec.dataFork.totalBlocks;
4452a7af
A
4424 break;
4425 }
4426 }
0c530ab8
A
4427 if (filerec.resourceFork.extents[i].blockCount != 0) {
4428 rsrcblks += filerec.resourceFork.extents[i].blockCount;
4429 block = filerec.resourceFork.extents[i].startBlock +
4430 filerec.resourceFork.extents[i].blockCount;
4431 if (block >= startblk) {
4432 cnidbufp[filecnt++] = filerec.fileID;
4433 blkstomove += filerec.resourceFork.totalBlocks;
4434 break;
4435 }
91447636
A
4436 }
4437 }
0c530ab8
A
4438 /*
4439 * Check for any overflow extents that overlap.
4440 */
4441 if (i == kHFSPlusExtentDensity) {
4442 if (filerec.dataFork.totalBlocks > datablks) {
4443 if (hfs_overlapped_overflow_extents(hfsmp, startblk, datablks, filerec.fileID, 0)) {
4444 cnidbufp[filecnt++] = filerec.fileID;
4445 blkstomove += filerec.dataFork.totalBlocks;
4446 }
4447 } else if (filerec.resourceFork.totalBlocks > rsrcblks) {
4448 if (hfs_overlapped_overflow_extents(hfsmp, startblk, rsrcblks, filerec.fileID, 1)) {
4449 cnidbufp[filecnt++] = filerec.fileID;
4450 blkstomove += filerec.resourceFork.totalBlocks;
4451 }
4452 }
4453 }
4454 }
4455
4456end_iteration:
2d21ac55
A
4457 if (filecnt == 0 && !system_file_moved) {
4458 printf("hfs_reclaimspace: no files moved\n");
0c530ab8 4459 error = ENOSPC;
91447636
A
4460 }
4461 /* All done with catalog. */
4462 hfs_systemfile_unlock(hfsmp, lockflags);
2d21ac55 4463 if (error || filecnt == 0)
91447636
A
4464 goto out;
4465
0c530ab8
A
4466 /*
4467 * Double check space requirements to make sure
4468 * there is enough space to relocate any files
4469 * that reside in the reclaim area.
4470 *
4471 * Blocks To Move --------------
4472 * | | |
4473 * V V V
4474 * ------------------------------------------------------------------------
4475 * | | / /// // |
4476 * | | / /// // |
4477 * | | / /// // |
4478 * ------------------------------------------------------------------------
4479 *
4480 * <------------------- New Total Blocks ------------------><-- Reclaim -->
4481 *
4482 * <------------------------ Original Total Blocks ----------------------->
4483 *
4484 */
2d21ac55
A
4485 if (blkstomove >= hfs_freeblks(hfsmp, 1)) {
4486 printf("hfs_truncatefs: insufficient space (need %lu blocks; have %u blocks)\n", blkstomove, hfs_freeblks(hfsmp, 1));
0c530ab8
A
4487 error = ENOSPC;
4488 goto out;
4489 }
4490 hfsmp->hfs_resize_filesmoved = 0;
4491 hfsmp->hfs_resize_totalfiles = filecnt;
4492
91447636
A
4493 /* Now move any files that are in the way. */
4494 for (i = 0; i < filecnt; ++i) {
4495 struct vnode * rvp;
4496
4497 if (hfs_vget(hfsmp, cnidbufp[i], &vp, 0) != 0)
4498 continue;
4499
4500 /* Relocate any data fork blocks. */
4501 if (VTOF(vp)->ff_blocks > 0) {
4502 error = hfs_relocate(vp, hfsmp->hfs_metazone_end + 1, kauth_cred_get(), current_proc());
4503 }
91447636
A
4504 if (error)
4505 break;
4506
4507 /* Relocate any resource fork blocks. */
4508 if ((VTOC((vp))->c_blocks - VTOF((vp))->ff_blocks) > 0) {
2d21ac55 4509 error = hfs_vgetrsrc(hfsmp, vp, &rvp, TRUE);
91447636
A
4510 if (error)
4511 break;
91447636 4512 error = hfs_relocate(rvp, hfsmp->hfs_metazone_end + 1, kauth_cred_get(), current_proc());
cf7d32b8 4513 VTOC(rvp)->c_flag |= C_NEED_RVNODE_PUT;
91447636
A
4514 if (error)
4515 break;
4516 }
743b1565 4517 hfs_unlock(VTOC(vp));
91447636
A
4518 vnode_put(vp);
4519 vp = NULL;
0c530ab8
A
4520
4521 ++hfsmp->hfs_resize_filesmoved;
4522
4523 /* Report intermediate progress. */
4524 if (filecnt > 100) {
4525 int progress;
4526
4527 progress = (i * 100) / filecnt;
4528 if (progress > (lastprogress + 9)) {
4529 printf("hfs_reclaimspace: %d%% done...\n", progress);
4530 lastprogress = progress;
4531 }
4532 }
91447636
A
4533 }
4534 if (vp) {
743b1565 4535 hfs_unlock(VTOC(vp));
91447636
A
4536 vnode_put(vp);
4537 vp = NULL;
4538 }
0c530ab8
A
4539 if (hfsmp->hfs_resize_filesmoved != 0) {
4540 printf("hfs_reclaimspace: relocated %d files on \"%s\"\n",
4541 (int)hfsmp->hfs_resize_filesmoved, hfsmp->vcbVN);
4542 }
91447636
A
4543out:
4544 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
4545 kmem_free(kernel_map, (vm_offset_t)cnidbufp, cnidbufsize);
4546
0c530ab8
A
4547 /*
4548 * Restore the roving allocation pointer on errors.
4549 * (but only if we didn't move any files)
4550 */
4551 if (error && hfsmp->hfs_resize_filesmoved == 0) {
2d21ac55 4552 HFS_UPDATE_NEXT_ALLOCATION(hfsmp, saved_next_allocation);
91447636
A
4553 }
4554 return (error);
4555}
4556
4557
0c530ab8
A
4558/*
4559 * Check if there are any overflow extents that overlap.
4560 */
4561static int
4562hfs_overlapped_overflow_extents(struct hfsmount *hfsmp, u_int32_t startblk, u_int32_t catblks, u_int32_t fileID, int rsrcfork)
4563{
4564 struct BTreeIterator * iterator = NULL;
4565 struct FSBufferDescriptor btdata;
4566 HFSPlusExtentRecord extrec;
4567 HFSPlusExtentKey *extkeyptr;
4568 FCB *fcb;
4569 u_int32_t block;
4570 u_int8_t forktype;
4571 int overlapped = 0;
4572 int i;
4573 int error;
4574
4575 forktype = rsrcfork ? 0xFF : 0;
4576 if (kmem_alloc(kernel_map, (vm_offset_t *)&iterator, sizeof(*iterator))) {
4577 return (0);
4578 }
4579 bzero(iterator, sizeof(*iterator));
4580 extkeyptr = (HFSPlusExtentKey *)&iterator->key;
4581 extkeyptr->keyLength = kHFSPlusExtentKeyMaximumLength;
4582 extkeyptr->forkType = forktype;
4583 extkeyptr->fileID = fileID;
4584 extkeyptr->startBlock = catblks;
4585
4586 btdata.bufferAddress = &extrec;
4587 btdata.itemSize = sizeof(extrec);
4588 btdata.itemCount = 1;
4589
4590 fcb = VTOF(hfsmp->hfs_extents_vp);
4591
4592 error = BTSearchRecord(fcb, iterator, &btdata, NULL, iterator);
4593 while (error == 0) {
4594 /* Stop when we encounter a different file. */
4595 if ((extkeyptr->fileID != fileID) ||
4596 (extkeyptr->forkType != forktype)) {
4597 break;
4598 }
4599 /*
4600 * Check if the file overlaps target space.
4601 */
4602 for (i = 0; i < kHFSPlusExtentDensity; ++i) {
4603 if (extrec[i].blockCount == 0) {
4604 break;
4605 }
4606 block = extrec[i].startBlock + extrec[i].blockCount;
4607 if (block >= startblk) {
4608 overlapped = 1;
4609 break;
4610 }
4611 }
4612 /* Look for more records. */
4613 error = BTIterateRecord(fcb, kBTreeNextRecord, iterator, &btdata, NULL);
4614 }
4615
4616 kmem_free(kernel_map, (vm_offset_t)iterator, sizeof(*iterator));
4617 return (overlapped);
4618}
4619
4620
4621/*
4622 * Calculate the progress of a file system resize operation.
4623 */
4624__private_extern__
4625int
4626hfs_resize_progress(struct hfsmount *hfsmp, u_int32_t *progress)
4627{
4628 if ((hfsmp->hfs_flags & HFS_RESIZE_IN_PROGRESS) == 0) {
4629 return (ENXIO);
4630 }
4631
4632 if (hfsmp->hfs_resize_totalfiles > 0)
4633 *progress = (hfsmp->hfs_resize_filesmoved * 100) / hfsmp->hfs_resize_totalfiles;
4634 else
4635 *progress = 0;
4636
4637 return (0);
4638}
4639
4640
91447636
A
4641/*
4642 * Get file system attributes.
4643 */
4644static int
4645hfs_vfs_getattr(struct mount *mp, struct vfs_attr *fsap, __unused vfs_context_t context)
4646{
2d21ac55
A
4647#define HFS_ATTR_CMN_VALIDMASK (ATTR_CMN_VALIDMASK & ~(ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST))
4648#define HFS_ATTR_FILE_VALIDMASK (ATTR_FILE_VALIDMASK & ~(ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST))
4649
91447636
A
4650 ExtendedVCB *vcb = VFSTOVCB(mp);
4651 struct hfsmount *hfsmp = VFSTOHFS(mp);
4652 u_long freeCNIDs;
4653
4654 freeCNIDs = (u_long)0xFFFFFFFF - (u_long)hfsmp->vcbNxtCNID;
4655
2d21ac55
A
4656 VFSATTR_RETURN(fsap, f_objcount, (u_int64_t)hfsmp->vcbFilCnt + (u_int64_t)hfsmp->vcbDirCnt);
4657 VFSATTR_RETURN(fsap, f_filecount, (u_int64_t)hfsmp->vcbFilCnt);
4658 VFSATTR_RETURN(fsap, f_dircount, (u_int64_t)hfsmp->vcbDirCnt);
4659 VFSATTR_RETURN(fsap, f_maxobjcount, (u_int64_t)0xFFFFFFFF);
cf7d32b8 4660 VFSATTR_RETURN(fsap, f_iosize, (size_t)cluster_max_io_size(mp, 0));
2d21ac55
A
4661 VFSATTR_RETURN(fsap, f_blocks, (u_int64_t)hfsmp->totalBlocks);
4662 VFSATTR_RETURN(fsap, f_bfree, (u_int64_t)hfs_freeblks(hfsmp, 0));
4663 VFSATTR_RETURN(fsap, f_bavail, (u_int64_t)hfs_freeblks(hfsmp, 1));
4664 VFSATTR_RETURN(fsap, f_bsize, (u_int32_t)vcb->blockSize);
91447636
A
4665 /* XXX needs clarification */
4666 VFSATTR_RETURN(fsap, f_bused, hfsmp->totalBlocks - hfs_freeblks(hfsmp, 1));
4667 /* Maximum files is constrained by total blocks. */
2d21ac55
A
4668 VFSATTR_RETURN(fsap, f_files, (u_int64_t)(hfsmp->totalBlocks - 2));
4669 VFSATTR_RETURN(fsap, f_ffree, MIN((u_int64_t)freeCNIDs, (u_int64_t)hfs_freeblks(hfsmp, 1)));
91447636
A
4670
4671 fsap->f_fsid.val[0] = hfsmp->hfs_raw_dev;
4672 fsap->f_fsid.val[1] = vfs_typenum(mp);
4673 VFSATTR_SET_SUPPORTED(fsap, f_fsid);
4674
4675 VFSATTR_RETURN(fsap, f_signature, vcb->vcbSigWord);
4676 VFSATTR_RETURN(fsap, f_carbon_fsid, 0);
4677
4678 if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) {
4679 vol_capabilities_attr_t *cap;
4680
4681 cap = &fsap->f_capabilities;
4682
4683 if (hfsmp->hfs_flags & HFS_STANDARD) {
4684 cap->capabilities[VOL_CAPABILITIES_FORMAT] =
4685 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
4686 VOL_CAP_FMT_CASE_PRESERVING |
2d21ac55
A
4687 VOL_CAP_FMT_FAST_STATFS |
4688 VOL_CAP_FMT_HIDDEN_FILES |
4689 VOL_CAP_FMT_PATH_FROM_ID;
91447636
A
4690 } else {
4691 cap->capabilities[VOL_CAPABILITIES_FORMAT] =
4692 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
4693 VOL_CAP_FMT_SYMBOLICLINKS |
4694 VOL_CAP_FMT_HARDLINKS |
4695 VOL_CAP_FMT_JOURNAL |
2d21ac55 4696 VOL_CAP_FMT_ZERO_RUNS |
91447636
A
4697 (hfsmp->jnl ? VOL_CAP_FMT_JOURNAL_ACTIVE : 0) |
4698 (hfsmp->hfs_flags & HFS_CASE_SENSITIVE ? VOL_CAP_FMT_CASE_SENSITIVE : 0) |
4699 VOL_CAP_FMT_CASE_PRESERVING |
4700 VOL_CAP_FMT_FAST_STATFS |
2d21ac55
A
4701 VOL_CAP_FMT_2TB_FILESIZE |
4702 VOL_CAP_FMT_HIDDEN_FILES |
4703 VOL_CAP_FMT_PATH_FROM_ID;
91447636
A
4704 }
4705 cap->capabilities[VOL_CAPABILITIES_INTERFACES] =
4706 VOL_CAP_INT_SEARCHFS |
4707 VOL_CAP_INT_ATTRLIST |
4708 VOL_CAP_INT_NFSEXPORT |
4709 VOL_CAP_INT_READDIRATTR |
4710 VOL_CAP_INT_EXCHANGEDATA |
4711 VOL_CAP_INT_ALLOCATE |
4712 VOL_CAP_INT_VOL_RENAME |
4713 VOL_CAP_INT_ADVLOCK |
2d21ac55
A
4714 VOL_CAP_INT_FLOCK |
4715#if NAMEDSTREAMS
4716 VOL_CAP_INT_EXTENDED_ATTR |
4717 VOL_CAP_INT_NAMEDSTREAMS;
4718#else
4719 VOL_CAP_INT_EXTENDED_ATTR;
4720#endif
91447636
A
4721 cap->capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
4722 cap->capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
4723
4724 cap->valid[VOL_CAPABILITIES_FORMAT] =
4725 VOL_CAP_FMT_PERSISTENTOBJECTIDS |
4726 VOL_CAP_FMT_SYMBOLICLINKS |
4727 VOL_CAP_FMT_HARDLINKS |
4728 VOL_CAP_FMT_JOURNAL |
4729 VOL_CAP_FMT_JOURNAL_ACTIVE |
4730 VOL_CAP_FMT_NO_ROOT_TIMES |
4731 VOL_CAP_FMT_SPARSE_FILES |
4732 VOL_CAP_FMT_ZERO_RUNS |
4733 VOL_CAP_FMT_CASE_SENSITIVE |
4734 VOL_CAP_FMT_CASE_PRESERVING |
4735 VOL_CAP_FMT_FAST_STATFS |
2d21ac55
A
4736 VOL_CAP_FMT_2TB_FILESIZE |
4737 VOL_CAP_FMT_OPENDENYMODES |
4738 VOL_CAP_FMT_HIDDEN_FILES |
4739 VOL_CAP_FMT_PATH_FROM_ID;
91447636
A
4740 cap->valid[VOL_CAPABILITIES_INTERFACES] =
4741 VOL_CAP_INT_SEARCHFS |
4742 VOL_CAP_INT_ATTRLIST |
4743 VOL_CAP_INT_NFSEXPORT |
4744 VOL_CAP_INT_READDIRATTR |
4745 VOL_CAP_INT_EXCHANGEDATA |
4746 VOL_CAP_INT_COPYFILE |
4747 VOL_CAP_INT_ALLOCATE |
4748 VOL_CAP_INT_VOL_RENAME |
4749 VOL_CAP_INT_ADVLOCK |
2d21ac55
A
4750 VOL_CAP_INT_FLOCK |
4751 VOL_CAP_INT_MANLOCK |
4752#if NAMEDSTREAMS
4753 VOL_CAP_INT_EXTENDED_ATTR |
4754 VOL_CAP_INT_NAMEDSTREAMS;
4755#else
4756 VOL_CAP_INT_EXTENDED_ATTR;
4757#endif
91447636
A
4758 cap->valid[VOL_CAPABILITIES_RESERVED1] = 0;
4759 cap->valid[VOL_CAPABILITIES_RESERVED2] = 0;
4760 VFSATTR_SET_SUPPORTED(fsap, f_capabilities);
4761 }
4762 if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) {
4763 vol_attributes_attr_t *attrp = &fsap->f_attributes;
4764
2d21ac55 4765 attrp->validattr.commonattr = HFS_ATTR_CMN_VALIDMASK;
91447636
A
4766 attrp->validattr.volattr = ATTR_VOL_VALIDMASK & ~ATTR_VOL_INFO;
4767 attrp->validattr.dirattr = ATTR_DIR_VALIDMASK;
2d21ac55 4768 attrp->validattr.fileattr = HFS_ATTR_FILE_VALIDMASK;
91447636
A
4769 attrp->validattr.forkattr = 0;
4770
2d21ac55 4771 attrp->nativeattr.commonattr = HFS_ATTR_CMN_VALIDMASK;
91447636
A
4772 attrp->nativeattr.volattr = ATTR_VOL_VALIDMASK & ~ATTR_VOL_INFO;
4773 attrp->nativeattr.dirattr = ATTR_DIR_VALIDMASK;
2d21ac55 4774 attrp->nativeattr.fileattr = HFS_ATTR_FILE_VALIDMASK;
91447636
A
4775 attrp->nativeattr.forkattr = 0;
4776 VFSATTR_SET_SUPPORTED(fsap, f_attributes);
4777 }
4778 fsap->f_create_time.tv_sec = hfsmp->vcbCrDate;
4779 fsap->f_create_time.tv_nsec = 0;
4780 VFSATTR_SET_SUPPORTED(fsap, f_create_time);
4781 fsap->f_modify_time.tv_sec = hfsmp->vcbLsMod;
4782 fsap->f_modify_time.tv_nsec = 0;
4783 VFSATTR_SET_SUPPORTED(fsap, f_modify_time);
4784
4785 fsap->f_backup_time.tv_sec = hfsmp->vcbVolBkUp;
4786 fsap->f_backup_time.tv_nsec = 0;
4787 VFSATTR_SET_SUPPORTED(fsap, f_backup_time);
4788 if (VFSATTR_IS_ACTIVE(fsap, f_fssubtype)) {
2d21ac55 4789 u_int16_t subtype = 0;
91447636
A
4790
4791 /*
4792 * Subtypes (flavors) for HFS
4793 * 0: Mac OS Extended
4794 * 1: Mac OS Extended (Journaled)
4795 * 2: Mac OS Extended (Case Sensitive)
4796 * 3: Mac OS Extended (Case Sensitive, Journaled)
4797 * 4 - 127: Reserved
4798 * 128: Mac OS Standard
4799 *
4800 */
4801 if (hfsmp->hfs_flags & HFS_STANDARD) {
4802 subtype = HFS_SUBTYPE_STANDARDHFS;
4803 } else /* HFS Plus */ {
4804 if (hfsmp->jnl)
4805 subtype |= HFS_SUBTYPE_JOURNALED;
4806 if (hfsmp->hfs_flags & HFS_CASE_SENSITIVE)
4807 subtype |= HFS_SUBTYPE_CASESENSITIVE;
4808 }
4809 fsap->f_fssubtype = subtype;
4810 VFSATTR_SET_SUPPORTED(fsap, f_fssubtype);
4811 }
4812
4813 if (VFSATTR_IS_ACTIVE(fsap, f_vol_name)) {
2d21ac55 4814 strlcpy(fsap->f_vol_name, (char *) hfsmp->vcbVN, MAXPATHLEN);
91447636
A
4815 VFSATTR_SET_SUPPORTED(fsap, f_vol_name);
4816 }
4817 return (0);
4818}
4819
4820/*
4821 * Perform a volume rename. Requires the FS' root vp.
4822 */
4823static int
4824hfs_rename_volume(struct vnode *vp, const char *name, proc_t p)
4825{
4826 ExtendedVCB *vcb = VTOVCB(vp);
4827 struct cnode *cp = VTOC(vp);
4828 struct hfsmount *hfsmp = VTOHFS(vp);
4829 struct cat_desc to_desc;
4830 struct cat_desc todir_desc;
4831 struct cat_desc new_desc;
4832 cat_cookie_t cookie;
4833 int lockflags;
4834 int error = 0;
4835
4836 /*
4837 * Ignore attempts to rename a volume to a zero-length name.
4838 */
4839 if (name[0] == 0)
4840 return(0);
4841
4842 bzero(&to_desc, sizeof(to_desc));
4843 bzero(&todir_desc, sizeof(todir_desc));
4844 bzero(&new_desc, sizeof(new_desc));
4845 bzero(&cookie, sizeof(cookie));
4846
4847 todir_desc.cd_parentcnid = kHFSRootParentID;
4848 todir_desc.cd_cnid = kHFSRootFolderID;
4849 todir_desc.cd_flags = CD_ISDIR;
4850
2d21ac55 4851 to_desc.cd_nameptr = (const u_int8_t *)name;
91447636
A
4852 to_desc.cd_namelen = strlen(name);
4853 to_desc.cd_parentcnid = kHFSRootParentID;
4854 to_desc.cd_cnid = cp->c_cnid;
4855 to_desc.cd_flags = CD_ISDIR;
4856
4857 if ((error = hfs_lock(cp, HFS_EXCLUSIVE_LOCK)) == 0) {
4858 if ((error = hfs_start_transaction(hfsmp)) == 0) {
4859 if ((error = cat_preflight(hfsmp, CAT_RENAME, &cookie, p)) == 0) {
4860 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_EXCLUSIVE_LOCK);
4861
4862 error = cat_rename(hfsmp, &cp->c_desc, &todir_desc, &to_desc, &new_desc);
4863
4864 /*
4865 * If successful, update the name in the VCB, ensure it's terminated.
4866 */
4867 if (!error) {
2d21ac55 4868 strlcpy((char *)vcb->vcbVN, name, sizeof(vcb->vcbVN));
91447636
A
4869 }
4870
4871 hfs_systemfile_unlock(hfsmp, lockflags);
4872 cat_postflight(hfsmp, &cookie, p);
4873
4874 if (error)
2d21ac55 4875 MarkVCBDirty(vcb);
91447636
A
4876 (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0);
4877 }
4878 hfs_end_transaction(hfsmp);
4879 }
4880 if (!error) {
4881 /* Release old allocated name buffer */
4882 if (cp->c_desc.cd_flags & CD_HASBUF) {
2d21ac55 4883 const char *tmp_name = (const char *)cp->c_desc.cd_nameptr;
91447636
A
4884
4885 cp->c_desc.cd_nameptr = 0;
4886 cp->c_desc.cd_namelen = 0;
4887 cp->c_desc.cd_flags &= ~CD_HASBUF;
2d21ac55 4888 vfs_removename(tmp_name);
91447636
A
4889 }
4890 /* Update cnode's catalog descriptor */
4891 replace_desc(cp, &new_desc);
4892 vcb->volumeNameEncodingHint = new_desc.cd_encoding;
4893 cp->c_touch_chgtime = TRUE;
4894 }
4895
4896 hfs_unlock(cp);
4897 }
4898
4899 return(error);
4900}
4901
4902/*
4903 * Get file system attributes.
4904 */
4905static int
4906hfs_vfs_setattr(struct mount *mp, struct vfs_attr *fsap, __unused vfs_context_t context)
4907{
4908 kauth_cred_t cred = vfs_context_ucred(context);
4909 int error = 0;
4910
4911 /*
4912 * Must be superuser or owner of filesystem to change volume attributes
4913 */
4914 if (!kauth_cred_issuser(cred) && (kauth_cred_getuid(cred) != vfs_statfs(mp)->f_owner))
4915 return(EACCES);
4916
4917 if (VFSATTR_IS_ACTIVE(fsap, f_vol_name)) {
4918 vnode_t root_vp;
4919
4920 error = hfs_vfs_root(mp, &root_vp, context);
4921 if (error)
4922 goto out;
4923
4924 error = hfs_rename_volume(root_vp, fsap->f_vol_name, vfs_context_proc(context));
4925 (void) vnode_put(root_vp);
4926 if (error)
4927 goto out;
4928
4929 VFSATTR_SET_SUPPORTED(fsap, f_vol_name);
4930 }
4931
4932out:
4933 return error;
4934}
4935
2d21ac55
A
4936/* If a runtime corruption is detected, set the volume inconsistent
4937 * bit in the volume attributes. The volume inconsistent bit is a persistent
4938 * bit which represents that the volume is corrupt and needs repair.
4939 * The volume inconsistent bit can be set from the kernel when it detects
4940 * runtime corruption or from file system repair utilities like fsck_hfs when
4941 * a repair operation fails. The bit should be cleared only from file system
4942 * verify/repair utility like fsck_hfs when a verify/repair succeeds.
4943 */
4944void hfs_mark_volume_inconsistent(struct hfsmount *hfsmp)
4945{
4946 HFS_MOUNT_LOCK(hfsmp, TRUE);
4947 if ((hfsmp->vcbAtrb & kHFSVolumeInconsistentMask) == 0) {
4948 hfsmp->vcbAtrb |= kHFSVolumeInconsistentMask;
4949 MarkVCBDirty(hfsmp);
4950 }
4951 /* Log information to ASL log */
4952 fslog_fs_corrupt(hfsmp->hfs_mp);
4953 printf("HFS: Runtime corruption detected on %s, fsck will be forced on next mount.\n", hfsmp->vcbVN);
4954 HFS_MOUNT_UNLOCK(hfsmp, TRUE);
4955}
4956
4957/* Replay the journal on the device node provided. Returns zero if
4958 * journal replay succeeded or no journal was supposed to be replayed.
4959 */
4960static int hfs_journal_replay(const char *devnode, vfs_context_t context)
4961{
4962 int retval = 0;
4963 struct vnode *devvp = NULL;
4964 struct mount *mp = NULL;
4965 struct hfs_mount_args *args = NULL;
4966
4967 /* Lookup vnode for given raw device path */
4968 retval = vnode_open(devnode, FREAD|FWRITE, 0, 0, &devvp, NULL);
4969 if (retval) {
4970 goto out;
4971 }
4972
4973 /* Replay allowed only on raw devices */
4974 if (!vnode_ischr(devvp)) {
4975 retval = EINVAL;
4976 goto out;
4977 }
4978
4979 /* Create dummy mount structures */
4980 MALLOC(mp, struct mount *, sizeof(struct mount), M_TEMP, M_WAITOK);
4981 bzero(mp, sizeof(struct mount));
4982 mount_lock_init(mp);
4983
4984 MALLOC(args, struct hfs_mount_args *, sizeof(struct hfs_mount_args), M_TEMP, M_WAITOK);
4985 bzero(args, sizeof(struct hfs_mount_args));
4986
4987 retval = hfs_mountfs(devvp, mp, args, 1, context);
4988 buf_flushdirtyblks(devvp, MNT_WAIT, 0, "hfs_journal_replay");
4989
4990out:
4991 if (mp) {
4992 mount_lock_destroy(mp);
4993 FREE(mp, M_TEMP);
4994 }
4995 if (args) {
4996 FREE(args, M_TEMP);
4997 }
4998 if (devvp) {
4999 vnode_close(devvp, FREAD|FWRITE, NULL);
5000 }
5001 return retval;
5002}
55e303ae 5003
1c79356b
A
5004/*
5005 * hfs vfs operations.
5006 */
5007struct vfsops hfs_vfsops = {
9bccf70c
A
5008 hfs_mount,
5009 hfs_start,
5010 hfs_unmount,
91447636 5011 hfs_vfs_root,
9bccf70c 5012 hfs_quotactl,
91447636 5013 hfs_vfs_getattr, /* was hfs_statfs */
9bccf70c 5014 hfs_sync,
91447636 5015 hfs_vfs_vget,
9bccf70c
A
5016 hfs_fhtovp,
5017 hfs_vptofh,
5018 hfs_init,
91447636 5019 hfs_sysctl,
2d21ac55
A
5020 hfs_vfs_setattr,
5021 {NULL}
1c79356b 5022};