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