]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
55e303ae | 2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1991, 1993, 1994 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * (c) UNIX System Laboratories, Inc. | |
26 | * All or some portions of this file are derived from material licensed | |
27 | * to the University of California by American Telephone and Telegraph | |
28 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
29 | * the permission of UNIX System Laboratories, Inc. | |
30 | * | |
31 | * Redistribution and use in source and binary forms, with or without | |
32 | * modification, are permitted provided that the following conditions | |
33 | * are met: | |
34 | * 1. Redistributions of source code must retain the above copyright | |
35 | * notice, this list of conditions and the following disclaimer. | |
36 | * 2. Redistributions in binary form must reproduce the above copyright | |
37 | * notice, this list of conditions and the following disclaimer in the | |
38 | * documentation and/or other materials provided with the distribution. | |
39 | * 3. All advertising materials mentioning features or use of this software | |
40 | * must display the following acknowledgement: | |
41 | * This product includes software developed by the University of | |
42 | * California, Berkeley and its contributors. | |
43 | * 4. Neither the name of the University nor the names of its contributors | |
44 | * may be used to endorse or promote products derived from this software | |
45 | * without specific prior written permission. | |
46 | * | |
47 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
57 | * SUCH DAMAGE. | |
58 | * | |
59 | * hfs_vfsops.c | |
60 | * derived from @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95 | |
61 | * | |
9bccf70c | 62 | * (c) Copyright 1997-2002 Apple Computer, Inc. All rights reserved. |
1c79356b A |
63 | * |
64 | * hfs_vfsops.c -- VFS layer for loadable HFS file system. | |
65 | * | |
1c79356b A |
66 | */ |
67 | #include <sys/param.h> | |
68 | #include <sys/systm.h> | |
69 | ||
70 | #include <sys/ubc.h> | |
71 | #include <sys/namei.h> | |
72 | #include <sys/vnode.h> | |
73 | #include <sys/mount.h> | |
55e303ae | 74 | #include <sys/sysctl.h> |
1c79356b A |
75 | #include <sys/malloc.h> |
76 | #include <sys/stat.h> | |
1c79356b | 77 | #include <sys/lock.h> |
9bccf70c A |
78 | #include <sys/quota.h> |
79 | #include <sys/disk.h> | |
55e303ae A |
80 | #include <sys/paths.h> |
81 | #include <sys/utfconv.h> | |
9bccf70c | 82 | |
b4c24cb9 A |
83 | // XXXdbg |
84 | #include <vfs/vfs_journal.h> | |
85 | ||
1c79356b A |
86 | #include <miscfs/specfs/specdev.h> |
87 | #include <hfs/hfs_mount.h> | |
88 | ||
89 | #include "hfs.h" | |
9bccf70c A |
90 | #include "hfs_catalog.h" |
91 | #include "hfs_cnode.h" | |
1c79356b A |
92 | #include "hfs_dbg.h" |
93 | #include "hfs_endian.h" | |
9bccf70c | 94 | #include "hfs_quota.h" |
1c79356b A |
95 | |
96 | #include "hfscommon/headers/FileMgrInternal.h" | |
97 | #include "hfscommon/headers/BTreesInternal.h" | |
98 | ||
9bccf70c | 99 | |
1c79356b A |
100 | #if HFS_DIAGNOSTIC |
101 | int hfs_dbg_all = 0; | |
1c79356b | 102 | int hfs_dbg_err = 0; |
1c79356b A |
103 | #endif |
104 | ||
d52fe63f | 105 | |
1c79356b A |
106 | extern struct vnodeopv_desc hfs_vnodeop_opv_desc; |
107 | ||
9bccf70c | 108 | extern void hfs_converterinit(void); |
1c79356b A |
109 | |
110 | extern void inittodr( time_t base); | |
1c79356b | 111 | |
1c79356b | 112 | |
9bccf70c A |
113 | static int hfs_changefs __P((struct mount *mp, struct hfs_mount_args *args, |
114 | struct proc *p)); | |
115 | static int hfs_reload __P((struct mount *mp, struct ucred *cred, struct proc *p)); | |
1c79356b | 116 | |
9bccf70c A |
117 | static int hfs_mountfs __P((struct vnode *devvp, struct mount *mp, struct proc *p, |
118 | struct hfs_mount_args *args)); | |
119 | static int hfs_statfs __P((struct mount *mp, register struct statfs *sbp, | |
120 | struct proc *p)); | |
55e303ae | 121 | static int hfs_flushfiles __P((struct mount *, int, struct proc *)); |
1c79356b | 122 | |
55e303ae | 123 | static int hfs_extendfs __P((struct mount *, u_int64_t, struct proc *)); |
1c79356b A |
124 | |
125 | /* | |
126 | * Called by vfs_mountroot when mounting HFS Plus as root. | |
127 | */ | |
55e303ae | 128 | __private_extern__ |
1c79356b A |
129 | int |
130 | hfs_mountroot() | |
131 | { | |
132 | extern struct vnode *rootvp; | |
133 | struct mount *mp; | |
134 | struct proc *p = current_proc(); /* XXX */ | |
135 | struct hfsmount *hfsmp; | |
9bccf70c | 136 | ExtendedVCB *vcb; |
1c79356b A |
137 | int error; |
138 | ||
139 | /* | |
140 | * Get vnode for rootdev. | |
141 | */ | |
142 | if ((error = bdevvp(rootdev, &rootvp))) { | |
143 | printf("hfs_mountroot: can't setup bdevvp"); | |
144 | return (error); | |
145 | } | |
9bccf70c A |
146 | if ((error = vfs_rootmountalloc("hfs", "root_device", &mp))) { |
147 | vrele(rootvp); /* release the reference from bdevvp() */ | |
1c79356b | 148 | return (error); |
9bccf70c | 149 | } |
1c79356b A |
150 | if ((error = hfs_mountfs(rootvp, mp, p, NULL))) { |
151 | mp->mnt_vfc->vfc_refcount--; | |
55e303ae A |
152 | |
153 | if (mp->mnt_kern_flag & MNTK_IO_XINFO) | |
154 | FREE(mp->mnt_xinfo_ptr, M_TEMP); | |
1c79356b | 155 | vfs_unbusy(mp, p); |
55e303ae | 156 | |
9bccf70c | 157 | vrele(rootvp); /* release the reference from bdevvp() */ |
55e303ae | 158 | FREE_ZONE(mp, sizeof (struct mount), M_MOUNT); |
1c79356b A |
159 | return (error); |
160 | } | |
161 | simple_lock(&mountlist_slock); | |
162 | CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); | |
163 | simple_unlock(&mountlist_slock); | |
164 | ||
165 | /* Init hfsmp */ | |
166 | hfsmp = VFSTOHFS(mp); | |
167 | ||
0b4e3aa0 A |
168 | hfsmp->hfs_uid = UNKNOWNUID; |
169 | hfsmp->hfs_gid = UNKNOWNGID; | |
170 | hfsmp->hfs_dir_mask = (S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH); /* 0755 */ | |
171 | hfsmp->hfs_file_mask = (S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH); /* 0755 */ | |
1c79356b | 172 | |
9bccf70c A |
173 | /* Establish the free block reserve. */ |
174 | vcb = HFSTOVCB(hfsmp); | |
175 | vcb->reserveBlocks = ((u_int64_t)vcb->totalBlocks * HFS_MINFREE) / 100; | |
176 | vcb->reserveBlocks = MIN(vcb->reserveBlocks, HFS_MAXRESERVE / vcb->blockSize); | |
177 | ||
1c79356b A |
178 | (void)hfs_statfs(mp, &mp->mnt_stat, p); |
179 | ||
180 | vfs_unbusy(mp, p); | |
9bccf70c | 181 | inittodr(HFSTOVCB(hfsmp)->vcbLsMod); |
1c79356b A |
182 | return (0); |
183 | } | |
184 | ||
185 | ||
186 | /* | |
187 | * VFS Operations. | |
188 | * | |
189 | * mount system call | |
190 | */ | |
191 | ||
9bccf70c A |
192 | static int |
193 | hfs_mount(mp, path, data, ndp, p) | |
1c79356b A |
194 | register struct mount *mp; |
195 | char *path; | |
196 | caddr_t data; | |
197 | struct nameidata *ndp; | |
198 | struct proc *p; | |
199 | { | |
200 | struct hfsmount *hfsmp = NULL; | |
201 | struct vnode *devvp; | |
202 | struct hfs_mount_args args; | |
203 | size_t size; | |
204 | int retval = E_NONE; | |
205 | int flags; | |
206 | mode_t accessmode; | |
1c79356b A |
207 | |
208 | if ((retval = copyin(data, (caddr_t)&args, sizeof(args)))) | |
209 | goto error_exit; | |
210 | ||
211 | /* | |
212 | * If updating, check whether changing from read-only to | |
213 | * read/write; if there is no device name, that's all we do. | |
214 | */ | |
215 | if (mp->mnt_flag & MNT_UPDATE) { | |
0b4e3aa0 | 216 | |
1c79356b | 217 | hfsmp = VFSTOHFS(mp); |
55e303ae A |
218 | if (((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) && |
219 | (mp->mnt_flag & MNT_RDONLY)) { | |
1c79356b A |
220 | |
221 | /* use VFS_SYNC to push out System (btree) files */ | |
222 | retval = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p); | |
223 | if (retval && ((mp->mnt_flag & MNT_FORCE) == 0)) | |
224 | goto error_exit; | |
225 | ||
226 | flags = WRITECLOSE; | |
227 | if (mp->mnt_flag & MNT_FORCE) | |
228 | flags |= FORCECLOSE; | |
229 | ||
9bccf70c | 230 | if ((retval = hfs_flushfiles(mp, flags, p))) |
1c79356b | 231 | goto error_exit; |
55e303ae | 232 | hfsmp->hfs_flags |= HFS_READ_ONLY; |
9bccf70c | 233 | retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); |
1c79356b A |
234 | |
235 | /* also get the volume bitmap blocks */ | |
236 | if (!retval) | |
237 | retval = VOP_FSYNC(hfsmp->hfs_devvp, NOCRED, MNT_WAIT, p); | |
238 | ||
239 | if (retval) { | |
55e303ae | 240 | hfsmp->hfs_flags &= ~HFS_READ_ONLY; |
1c79356b A |
241 | goto error_exit; |
242 | } | |
55e303ae A |
243 | |
244 | if (hfsmp->jnl) { | |
245 | hfs_global_exclusive_lock_acquire(hfsmp); | |
246 | ||
247 | journal_close(hfsmp->jnl); | |
248 | hfsmp->jnl = NULL; | |
249 | ||
250 | // Note: we explicitly don't want to shutdown | |
251 | // access to the jvp because we may need | |
252 | // it later if we go back to being read-write. | |
253 | ||
254 | hfs_global_exclusive_lock_release(hfsmp); | |
255 | } | |
1c79356b A |
256 | } |
257 | ||
258 | if ((mp->mnt_flag & MNT_RELOAD) && | |
259 | (retval = hfs_reload(mp, ndp->ni_cnd.cn_cred, p))) | |
260 | goto error_exit; | |
261 | ||
55e303ae A |
262 | if ((hfsmp->hfs_flags & HFS_READ_ONLY) && |
263 | (mp->mnt_kern_flag & MNTK_WANTRDWR)) { | |
1c79356b A |
264 | /* |
265 | * If upgrade to read-write by non-root, then verify | |
266 | * that user has necessary permissions on the device. | |
267 | */ | |
268 | if (p->p_ucred->cr_uid != 0) { | |
269 | devvp = hfsmp->hfs_devvp; | |
270 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); | |
271 | if ((retval = VOP_ACCESS(devvp, VREAD | VWRITE, p->p_ucred, p))) { | |
272 | VOP_UNLOCK(devvp, 0, p); | |
273 | goto error_exit; | |
274 | } | |
275 | VOP_UNLOCK(devvp, 0, p); | |
276 | } | |
9bccf70c | 277 | retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); |
1c79356b A |
278 | |
279 | if (retval != E_NONE) | |
280 | goto error_exit; | |
281 | ||
55e303ae A |
282 | // If the journal was shut-down previously because we were |
283 | // asked to be read-only, let's start it back up again now | |
284 | ||
285 | if ( (HFSTOVCB(hfsmp)->vcbAtrb & kHFSVolumeJournaledMask) | |
286 | && hfsmp->jnl == NULL | |
287 | && hfsmp->jvp != NULL) { | |
288 | int flags; | |
289 | ||
290 | if (hfsmp->hfs_flags & HFS_NEED_JNL_RESET) { | |
291 | flags = JOURNAL_RESET; | |
292 | } else { | |
293 | flags = 0; | |
294 | } | |
295 | ||
296 | hfs_global_exclusive_lock_acquire(hfsmp); | |
297 | ||
298 | hfsmp->jnl = journal_open(hfsmp->jvp, | |
299 | (hfsmp->jnl_start * HFSTOVCB(hfsmp)->blockSize) + (off_t)HFSTOVCB(hfsmp)->hfsPlusIOPosOffset, | |
300 | hfsmp->jnl_size, | |
301 | hfsmp->hfs_devvp, | |
302 | hfsmp->hfs_phys_block_size, | |
303 | flags, | |
304 | 0, | |
305 | hfs_sync_metadata, hfsmp->hfs_mp); | |
306 | ||
307 | hfs_global_exclusive_lock_release(hfsmp); | |
308 | ||
309 | if (hfsmp->jnl == NULL) { | |
310 | retval = EINVAL; | |
311 | goto error_exit; | |
312 | } else { | |
313 | hfsmp->hfs_flags &= ~HFS_NEED_JNL_RESET; | |
314 | } | |
315 | ||
316 | } | |
317 | ||
318 | /* Only clear HFS_READ_ONLY after a successfull write */ | |
319 | hfsmp->hfs_flags &= ~HFS_READ_ONLY; | |
1c79356b A |
320 | } |
321 | ||
55e303ae | 322 | if (((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) && |
1c79356b A |
323 | (HFSTOVCB(hfsmp)->vcbSigWord == kHFSPlusSigWord)) { |
324 | /* setup private/hidden directory for unlinked files */ | |
55e303ae | 325 | FindMetaDataDirectory(HFSTOVCB(hfsmp)); |
b4c24cb9 A |
326 | if (hfsmp->jnl) |
327 | hfs_remove_orphans(hfsmp); | |
55e303ae A |
328 | |
329 | /* | |
330 | * Allow hot file clustering if conditions allow. | |
331 | */ | |
332 | if ((hfsmp->hfs_flags & HFS_METADATA_ZONE) && | |
333 | (mp->mnt_flag & MNT_RDONLY) && | |
334 | (mp->mnt_kern_flag & MNTK_WANTRDWR)) { | |
335 | (void) hfs_recording_init(hfsmp, p); | |
336 | } | |
1c79356b A |
337 | } |
338 | ||
339 | if (args.fspec == 0) { | |
340 | /* | |
341 | * Process export requests. | |
342 | */ | |
343 | return vfs_export(mp, &hfsmp->hfs_export, &args.export); | |
344 | } | |
345 | } | |
346 | ||
347 | /* | |
348 | * Not an update, or updating the name: look up the name | |
349 | * and verify that it refers to a sensible block device. | |
350 | */ | |
351 | NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); | |
352 | retval = namei(ndp); | |
353 | if (retval != E_NONE) { | |
354 | DBG_ERR(("hfs_mount: CAN'T GET DEVICE: %s, %x\n", args.fspec, ndp->ni_vp->v_rdev)); | |
355 | goto error_exit; | |
356 | } | |
357 | ||
358 | devvp = ndp->ni_vp; | |
359 | ||
360 | if (devvp->v_type != VBLK) { | |
361 | vrele(devvp); | |
362 | retval = ENOTBLK; | |
363 | goto error_exit; | |
364 | } | |
365 | if (major(devvp->v_rdev) >= nblkdev) { | |
366 | vrele(devvp); | |
367 | retval = ENXIO; | |
368 | goto error_exit; | |
369 | } | |
370 | ||
371 | /* | |
372 | * If mount by non-root, then verify that user has necessary | |
373 | * permissions on the device. | |
374 | */ | |
375 | if (p->p_ucred->cr_uid != 0) { | |
376 | accessmode = VREAD; | |
377 | if ((mp->mnt_flag & MNT_RDONLY) == 0) | |
378 | accessmode |= VWRITE; | |
379 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); | |
380 | if ((retval = VOP_ACCESS(devvp, accessmode, p->p_ucred, p))) { | |
381 | vput(devvp); | |
382 | goto error_exit; | |
383 | } | |
384 | VOP_UNLOCK(devvp, 0, p); | |
385 | } | |
386 | ||
387 | if ((mp->mnt_flag & MNT_UPDATE) == 0) { | |
388 | retval = hfs_mountfs(devvp, mp, p, &args); | |
389 | if (retval != E_NONE) | |
390 | vrele(devvp); | |
391 | } else { | |
392 | if (devvp != hfsmp->hfs_devvp) | |
393 | retval = EINVAL; /* needs translation */ | |
394 | else | |
395 | retval = hfs_changefs(mp, &args, p); | |
396 | vrele(devvp); | |
397 | } | |
398 | ||
399 | if (retval != E_NONE) { | |
400 | goto error_exit; | |
401 | } | |
402 | ||
1c79356b A |
403 | /* Set the mount flag to indicate that we support volfs */ |
404 | mp->mnt_flag |= MNT_DOVOLFS; | |
405 | if (VFSTOVCB(mp)->vcbSigWord == kHFSSigWord) { | |
406 | /* HFS volumes only want roman-encoded names: */ | |
407 | mp->mnt_flag |= MNT_FIXEDSCRIPTENCODING; | |
408 | } | |
409 | (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN-1, &size); | |
b4c24cb9 | 410 | |
1c79356b A |
411 | bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); |
412 | (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); | |
413 | bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); | |
414 | (void)hfs_statfs(mp, &mp->mnt_stat, p); | |
415 | return (E_NONE); | |
416 | ||
417 | error_exit: | |
418 | ||
419 | return (retval); | |
420 | } | |
421 | ||
422 | ||
9bccf70c A |
423 | /* Change fs mount parameters */ |
424 | static int | |
1c79356b A |
425 | hfs_changefs(mp, args, p) |
426 | struct mount *mp; | |
427 | struct hfs_mount_args *args; | |
428 | struct proc *p; | |
429 | { | |
9bccf70c | 430 | int retval = 0; |
1c79356b A |
431 | int namefix, permfix, permswitch; |
432 | struct hfsmount *hfsmp; | |
9bccf70c | 433 | struct cnode *cp; |
1c79356b | 434 | ExtendedVCB *vcb; |
1c79356b A |
435 | register struct vnode *vp, *nvp; |
436 | hfs_to_unicode_func_t get_unicode_func; | |
437 | unicode_to_hfs_func_t get_hfsname_func; | |
9bccf70c A |
438 | struct cat_desc cndesc; |
439 | struct cat_attr cnattr; | |
440 | u_long old_encoding; | |
1c79356b A |
441 | |
442 | hfsmp = VFSTOHFS(mp); | |
443 | vcb = HFSTOVCB(hfsmp); | |
55e303ae A |
444 | permswitch = (((hfsmp->hfs_flags & HFS_UNKNOWN_PERMS) && |
445 | ((mp->mnt_flag & MNT_UNKNOWNPERMISSIONS) == 0)) || | |
446 | (((hfsmp->hfs_flags & HFS_UNKNOWN_PERMS) == 0) && | |
447 | (mp->mnt_flag & MNT_UNKNOWNPERMISSIONS))); | |
448 | ||
0b4e3aa0 A |
449 | /* The root filesystem must operate with actual permissions: */ |
450 | if (permswitch && (mp->mnt_flag & MNT_ROOTFS) && (mp->mnt_flag & MNT_UNKNOWNPERMISSIONS)) { | |
451 | mp->mnt_flag &= ~MNT_UNKNOWNPERMISSIONS; /* Just say "No". */ | |
452 | return EINVAL; | |
55e303ae A |
453 | } |
454 | if (mp->mnt_flag & MNT_UNKNOWNPERMISSIONS) | |
455 | hfsmp->hfs_flags |= HFS_UNKNOWN_PERMS; | |
456 | else | |
457 | hfsmp->hfs_flags &= ~HFS_UNKNOWN_PERMS; | |
458 | ||
459 | namefix = permfix = 0; | |
1c79356b | 460 | |
9bccf70c | 461 | /* Change the timezone (Note: this affects all hfs volumes and hfs+ volume create dates) */ |
1c79356b A |
462 | if (args->hfs_timezone.tz_minuteswest != VNOVAL) { |
463 | gTimeZone = args->hfs_timezone; | |
464 | } | |
465 | ||
9bccf70c | 466 | /* Change the default uid, gid and/or mask */ |
1c79356b A |
467 | if ((args->hfs_uid != (uid_t)VNOVAL) && (hfsmp->hfs_uid != args->hfs_uid)) { |
468 | hfsmp->hfs_uid = args->hfs_uid; | |
9bccf70c A |
469 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSPlusSigWord) |
470 | ++permfix; | |
1c79356b A |
471 | } |
472 | if ((args->hfs_gid != (gid_t)VNOVAL) && (hfsmp->hfs_gid != args->hfs_gid)) { | |
473 | hfsmp->hfs_gid = args->hfs_gid; | |
9bccf70c A |
474 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSPlusSigWord) |
475 | ++permfix; | |
1c79356b A |
476 | } |
477 | if (args->hfs_mask != (mode_t)VNOVAL) { | |
478 | if (hfsmp->hfs_dir_mask != (args->hfs_mask & ALLPERMS)) { | |
479 | hfsmp->hfs_dir_mask = args->hfs_mask & ALLPERMS; | |
480 | hfsmp->hfs_file_mask = args->hfs_mask & ALLPERMS; | |
481 | if ((args->flags != VNOVAL) && (args->flags & HFSFSMNT_NOXONFILES)) | |
482 | hfsmp->hfs_file_mask = (args->hfs_mask & DEFFILEMODE); | |
9bccf70c A |
483 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSPlusSigWord) |
484 | ++permfix; | |
1c79356b A |
485 | } |
486 | } | |
487 | ||
9bccf70c | 488 | /* Change the hfs encoding value (hfs only) */ |
1c79356b | 489 | if ((HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) && |
55e303ae | 490 | (args->hfs_encoding != (u_long)VNOVAL) && |
1c79356b A |
491 | (hfsmp->hfs_encoding != args->hfs_encoding)) { |
492 | ||
493 | retval = hfs_getconverter(args->hfs_encoding, &get_unicode_func, &get_hfsname_func); | |
9bccf70c A |
494 | if (retval) |
495 | goto exit; | |
1c79356b A |
496 | |
497 | /* | |
498 | * Connect the new hfs_get_unicode converter but leave | |
499 | * the old hfs_get_hfsname converter in place so that | |
500 | * we can lookup existing vnodes to get their correctly | |
501 | * encoded names. | |
502 | * | |
503 | * When we're all finished, we can then connect the new | |
504 | * hfs_get_hfsname converter and release our interest | |
505 | * in the old converters. | |
506 | */ | |
507 | hfsmp->hfs_get_unicode = get_unicode_func; | |
9bccf70c A |
508 | old_encoding = hfsmp->hfs_encoding; |
509 | hfsmp->hfs_encoding = args->hfs_encoding; | |
1c79356b A |
510 | ++namefix; |
511 | } | |
512 | ||
9bccf70c A |
513 | if (!(namefix || permfix || permswitch)) |
514 | goto exit; | |
1c79356b A |
515 | |
516 | /* | |
517 | * For each active vnode fix things that changed | |
518 | * | |
519 | * Note that we can visit a vnode more than once | |
520 | * and we can race with fsync. | |
521 | */ | |
522 | simple_lock(&mntvnode_slock); | |
523 | loop: | |
524 | for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { | |
525 | /* | |
526 | * If the vnode that we are about to fix is no longer | |
527 | * associated with this mount point, start over. | |
528 | */ | |
529 | if (vp->v_mount != mp) | |
530 | goto loop; | |
531 | ||
532 | simple_lock(&vp->v_interlock); | |
533 | nvp = vp->v_mntvnodes.le_next; | |
534 | if (vp->v_flag & VSYSTEM) { | |
535 | simple_unlock(&vp->v_interlock); | |
536 | continue; | |
537 | } | |
538 | simple_unlock(&mntvnode_slock); | |
539 | retval = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); | |
540 | if (retval) { | |
541 | simple_lock(&mntvnode_slock); | |
542 | if (retval == ENOENT) | |
543 | goto loop; | |
544 | continue; | |
545 | } | |
546 | ||
9bccf70c | 547 | cp = VTOC(vp); |
1c79356b | 548 | |
9bccf70c | 549 | retval = cat_lookup(hfsmp, &cp->c_desc, 0, &cndesc, &cnattr, NULL); |
1c79356b A |
550 | /* If we couldn't find this guy skip to the next one */ |
551 | if (retval) { | |
552 | if (namefix) | |
553 | cache_purge(vp); | |
554 | vput(vp); | |
555 | simple_lock(&mntvnode_slock); | |
556 | continue; | |
557 | } | |
558 | ||
55e303ae | 559 | /* Get the real uid/gid and perm mask from disk. */ |
9bccf70c A |
560 | if (permswitch || permfix) { |
561 | cp->c_uid = cnattr.ca_uid; | |
562 | cp->c_gid = cnattr.ca_gid; | |
563 | cp->c_mode = cnattr.ca_mode; | |
564 | } | |
1c79356b A |
565 | |
566 | /* | |
567 | * If we're switching name converters then... | |
568 | * Remove the existing entry from the namei cache. | |
569 | * Update name to one based on new encoder. | |
570 | */ | |
571 | if (namefix) { | |
572 | cache_purge(vp); | |
9bccf70c | 573 | replace_desc(cp, &cndesc); |
1c79356b | 574 | |
9bccf70c A |
575 | if (cndesc.cd_cnid == kHFSRootFolderID) { |
576 | strncpy(vcb->vcbVN, cp->c_desc.cd_nameptr, NAME_MAX); | |
577 | cp->c_desc.cd_encoding = hfsmp->hfs_encoding; | |
578 | } | |
579 | } else { | |
580 | cat_releasedesc(&cndesc); | |
1c79356b | 581 | } |
1c79356b A |
582 | vput(vp); |
583 | simple_lock(&mntvnode_slock); | |
584 | ||
9bccf70c A |
585 | } /* end for (vp...) */ |
586 | simple_unlock(&mntvnode_slock); | |
1c79356b A |
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) { | |
1c79356b | 593 | hfsmp->hfs_get_hfsname = get_hfsname_func; |
1c79356b | 594 | vcb->volumeNameEncodingHint = args->hfs_encoding; |
1c79356b A |
595 | (void) hfs_relconverter(old_encoding); |
596 | } | |
9bccf70c | 597 | exit: |
1c79356b A |
598 | return (retval); |
599 | } | |
600 | ||
601 | ||
602 | /* | |
603 | * Reload all incore data for a filesystem (used after running fsck on | |
604 | * the root filesystem and finding things to fix). The filesystem must | |
605 | * be mounted read-only. | |
606 | * | |
607 | * Things to do to update the mount: | |
9bccf70c A |
608 | * invalidate all cached meta-data. |
609 | * invalidate all inactive vnodes. | |
610 | * invalidate all cached file data. | |
611 | * re-read volume header from disk. | |
612 | * re-load meta-file info (extents, file size). | |
613 | * re-load B-tree header data. | |
614 | * re-read cnode data for all active vnodes. | |
1c79356b | 615 | */ |
9bccf70c | 616 | static int |
1c79356b A |
617 | hfs_reload(mountp, cred, p) |
618 | register struct mount *mountp; | |
619 | struct ucred *cred; | |
620 | struct proc *p; | |
621 | { | |
622 | register struct vnode *vp, *nvp, *devvp; | |
9bccf70c | 623 | struct cnode *cp; |
1c79356b | 624 | struct buf *bp; |
d52fe63f A |
625 | int sectorsize; |
626 | int error, i; | |
1c79356b A |
627 | struct hfsmount *hfsmp; |
628 | struct HFSPlusVolumeHeader *vhp; | |
629 | ExtendedVCB *vcb; | |
9bccf70c A |
630 | struct filefork *forkp; |
631 | struct cat_desc cndesc; | |
1c79356b A |
632 | |
633 | if ((mountp->mnt_flag & MNT_RDONLY) == 0) | |
634 | return (EINVAL); | |
635 | ||
636 | hfsmp = VFSTOHFS(mountp); | |
637 | vcb = HFSTOVCB(hfsmp); | |
638 | ||
639 | if (vcb->vcbSigWord == kHFSSigWord) | |
640 | return (EINVAL); /* rooting from HFS is not supported! */ | |
641 | ||
642 | /* | |
643 | * Invalidate all cached meta-data. | |
644 | */ | |
645 | devvp = hfsmp->hfs_devvp; | |
646 | if (vinvalbuf(devvp, 0, cred, p, 0, 0)) | |
647 | panic("hfs_reload: dirty1"); | |
648 | InvalidateCatalogCache(vcb); | |
649 | ||
9bccf70c A |
650 | loop: |
651 | simple_lock(&mntvnode_slock); | |
652 | for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { | |
653 | if (vp->v_mount != mountp) { | |
654 | simple_unlock(&mntvnode_slock); | |
655 | goto loop; | |
656 | } | |
657 | nvp = vp->v_mntvnodes.le_next; | |
658 | ||
659 | /* | |
660 | * Invalidate all inactive vnodes. | |
661 | */ | |
662 | if (vrecycle(vp, &mntvnode_slock, p)) | |
663 | goto loop; | |
664 | ||
665 | /* | |
666 | * Invalidate all cached file data. | |
667 | */ | |
668 | simple_lock(&vp->v_interlock); | |
669 | simple_unlock(&mntvnode_slock); | |
670 | if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { | |
671 | goto loop; | |
672 | } | |
673 | if (vinvalbuf(vp, 0, cred, p, 0, 0)) | |
674 | panic("hfs_reload: dirty2"); | |
675 | ||
676 | /* | |
677 | * Re-read cnode data for all active vnodes (non-metadata files). | |
678 | */ | |
679 | cp = VTOC(vp); | |
680 | if ((vp->v_flag & VSYSTEM) == 0 && !VNODE_IS_RSRC(vp)) { | |
681 | struct cat_fork *datafork; | |
682 | struct cat_desc desc; | |
683 | ||
684 | datafork = cp->c_datafork ? &cp->c_datafork->ff_data : NULL; | |
685 | ||
686 | /* lookup by fileID since name could have changed */ | |
687 | if ((error = cat_idlookup(hfsmp, cp->c_fileid, &desc, &cp->c_attr, datafork))) { | |
688 | vput(vp); | |
689 | return (error); | |
690 | } | |
691 | ||
9bccf70c A |
692 | /* update cnode's catalog descriptor */ |
693 | (void) replace_desc(cp, &desc); | |
694 | } | |
695 | vput(vp); | |
696 | simple_lock(&mntvnode_slock); | |
697 | } | |
698 | simple_unlock(&mntvnode_slock); | |
699 | ||
1c79356b A |
700 | /* |
701 | * Re-read VolumeHeader from disk. | |
702 | */ | |
d52fe63f A |
703 | sectorsize = hfsmp->hfs_phys_block_size; |
704 | ||
705 | error = meta_bread(hfsmp->hfs_devvp, | |
706 | (vcb->hfsPlusIOPosOffset / sectorsize) + HFS_PRI_SECTOR(sectorsize), | |
707 | sectorsize, NOCRED, &bp); | |
1c79356b A |
708 | if (error) { |
709 | if (bp != NULL) | |
710 | brelse(bp); | |
711 | return (error); | |
712 | } | |
713 | ||
d52fe63f | 714 | vhp = (HFSPlusVolumeHeader *) (bp->b_data + HFS_PRI_OFFSET(sectorsize)); |
1c79356b | 715 | |
9bccf70c | 716 | /* Do a quick sanity check */ |
55e303ae A |
717 | if ((SWAP_BE16(vhp->signature) != kHFSPlusSigWord && |
718 | SWAP_BE16(vhp->signature) != kHFSXSigWord) || | |
719 | (SWAP_BE16(vhp->version) != kHFSPlusVersion && | |
720 | SWAP_BE16(vhp->version) != kHFSXVersion) || | |
9bccf70c | 721 | SWAP_BE32(vhp->blockSize) != vcb->blockSize) { |
1c79356b | 722 | brelse(bp); |
9bccf70c | 723 | return (EIO); |
1c79356b A |
724 | } |
725 | ||
9bccf70c A |
726 | vcb->vcbLsMod = to_bsd_time(SWAP_BE32(vhp->modifyDate)); |
727 | vcb->vcbAtrb = (UInt16) SWAP_BE32 (vhp->attributes); /* VCB only uses lower 16 bits */ | |
b4c24cb9 | 728 | vcb->vcbJinfoBlock = SWAP_BE32(vhp->journalInfoBlock); |
9bccf70c A |
729 | vcb->vcbClpSiz = SWAP_BE32 (vhp->rsrcClumpSize); |
730 | vcb->vcbNxtCNID = SWAP_BE32 (vhp->nextCatalogID); | |
731 | vcb->vcbVolBkUp = to_bsd_time(SWAP_BE32(vhp->backupDate)); | |
732 | vcb->vcbWrCnt = SWAP_BE32 (vhp->writeCount); | |
733 | vcb->vcbFilCnt = SWAP_BE32 (vhp->fileCount); | |
734 | vcb->vcbDirCnt = SWAP_BE32 (vhp->folderCount); | |
1c79356b | 735 | vcb->nextAllocation = SWAP_BE32 (vhp->nextAllocation); |
9bccf70c A |
736 | vcb->totalBlocks = SWAP_BE32 (vhp->totalBlocks); |
737 | vcb->freeBlocks = SWAP_BE32 (vhp->freeBlocks); | |
1c79356b A |
738 | vcb->encodingsBitmap = SWAP_BE64 (vhp->encodingsBitmap); |
739 | bcopy(vhp->finderInfo, vcb->vcbFndrInfo, sizeof(vhp->finderInfo)); | |
740 | vcb->localCreateDate = SWAP_BE32 (vhp->createDate); /* hfs+ create date is in local time */ | |
741 | ||
742 | /* | |
743 | * Re-load meta-file vnode data (extent info, file size, etc). | |
744 | */ | |
9bccf70c A |
745 | forkp = VTOF((struct vnode *)vcb->extentsRefNum); |
746 | for (i = 0; i < kHFSPlusExtentDensity; i++) { | |
747 | forkp->ff_extents[i].startBlock = | |
748 | SWAP_BE32 (vhp->extentsFile.extents[i].startBlock); | |
749 | forkp->ff_extents[i].blockCount = | |
750 | SWAP_BE32 (vhp->extentsFile.extents[i].blockCount); | |
751 | } | |
752 | forkp->ff_size = SWAP_BE64 (vhp->extentsFile.logicalSize); | |
753 | forkp->ff_blocks = SWAP_BE32 (vhp->extentsFile.totalBlocks); | |
754 | forkp->ff_clumpsize = SWAP_BE32 (vhp->extentsFile.clumpSize); | |
755 | ||
756 | ||
757 | forkp = VTOF((struct vnode *)vcb->catalogRefNum); | |
758 | for (i = 0; i < kHFSPlusExtentDensity; i++) { | |
759 | forkp->ff_extents[i].startBlock = | |
760 | SWAP_BE32 (vhp->catalogFile.extents[i].startBlock); | |
761 | forkp->ff_extents[i].blockCount = | |
762 | SWAP_BE32 (vhp->catalogFile.extents[i].blockCount); | |
763 | } | |
764 | forkp->ff_size = SWAP_BE64 (vhp->catalogFile.logicalSize); | |
765 | forkp->ff_blocks = SWAP_BE32 (vhp->catalogFile.totalBlocks); | |
766 | forkp->ff_clumpsize = SWAP_BE32 (vhp->catalogFile.clumpSize); | |
767 | ||
768 | ||
769 | forkp = VTOF((struct vnode *)vcb->allocationsRefNum); | |
770 | for (i = 0; i < kHFSPlusExtentDensity; i++) { | |
771 | forkp->ff_extents[i].startBlock = | |
772 | SWAP_BE32 (vhp->allocationFile.extents[i].startBlock); | |
773 | forkp->ff_extents[i].blockCount = | |
774 | SWAP_BE32 (vhp->allocationFile.extents[i].blockCount); | |
775 | } | |
776 | forkp->ff_size = SWAP_BE64 (vhp->allocationFile.logicalSize); | |
777 | forkp->ff_blocks = SWAP_BE32 (vhp->allocationFile.totalBlocks); | |
778 | forkp->ff_clumpsize = SWAP_BE32 (vhp->allocationFile.clumpSize); | |
1c79356b A |
779 | |
780 | brelse(bp); | |
781 | vhp = NULL; | |
782 | ||
783 | /* | |
784 | * Re-load B-tree header data | |
785 | */ | |
9bccf70c A |
786 | forkp = VTOF((struct vnode *)vcb->extentsRefNum); |
787 | if (error = MacToVFSError( BTReloadData((FCB*)forkp) )) | |
1c79356b A |
788 | return (error); |
789 | ||
9bccf70c A |
790 | forkp = VTOF((struct vnode *)vcb->catalogRefNum); |
791 | if (error = MacToVFSError( BTReloadData((FCB*)forkp) )) | |
1c79356b A |
792 | return (error); |
793 | ||
9bccf70c A |
794 | /* Reload the volume name */ |
795 | if ((error = cat_idlookup(hfsmp, kHFSRootFolderID, &cndesc, NULL, NULL))) | |
1c79356b | 796 | return (error); |
9bccf70c A |
797 | vcb->volumeNameEncodingHint = cndesc.cd_encoding; |
798 | bcopy(cndesc.cd_nameptr, vcb->vcbVN, min(255, cndesc.cd_namelen)); | |
799 | cat_releasedesc(&cndesc); | |
1c79356b A |
800 | |
801 | /* Re-establish private/hidden directory for unlinked files */ | |
55e303ae | 802 | FindMetaDataDirectory(vcb); |
1c79356b | 803 | |
55e303ae A |
804 | /* In case any volume information changed to trigger a notification */ |
805 | hfs_generate_volume_notifications(hfsmp); | |
806 | ||
1c79356b A |
807 | return (0); |
808 | } | |
809 | ||
810 | ||
b4c24cb9 A |
811 | static int |
812 | get_raw_device(char *fspec, int is_user, int ronly, struct vnode **rvp, struct ucred *cred, struct proc *p) | |
813 | { | |
814 | char *rawbuf; | |
815 | char *dp; | |
816 | size_t namelen; | |
817 | struct nameidata nd; | |
818 | int retval; | |
819 | ||
820 | *rvp = NULL; | |
821 | ||
822 | MALLOC(rawbuf, char *, MAXPATHLEN, M_HFSMNT, M_WAITOK); | |
823 | if (rawbuf == NULL) { | |
824 | retval = ENOMEM; | |
825 | goto error_exit; | |
826 | } | |
827 | ||
828 | if (is_user) { | |
829 | retval = copyinstr(fspec, rawbuf, MAXPATHLEN - 1, &namelen); | |
830 | if (retval != E_NONE) { | |
831 | FREE(rawbuf, M_HFSMNT); | |
832 | goto error_exit; | |
833 | } | |
834 | } else { | |
835 | strcpy(rawbuf, fspec); | |
836 | namelen = strlen(rawbuf); | |
837 | } | |
838 | ||
839 | /* make sure it's null terminated */ | |
840 | rawbuf[MAXPATHLEN-1] = '\0'; | |
841 | ||
842 | dp = &rawbuf[namelen-1]; | |
843 | while(dp >= rawbuf && *dp != '/') { | |
844 | dp--; | |
845 | } | |
846 | ||
847 | if (dp != NULL) { | |
848 | dp++; | |
849 | } else { | |
850 | dp = rawbuf; | |
851 | } | |
852 | ||
853 | /* make room for and insert the 'r' for the raw device */ | |
854 | memmove(dp+1, dp, strlen(dp)+1); | |
855 | *dp = 'r'; | |
856 | ||
857 | NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, rawbuf, p); | |
858 | retval = namei(&nd); | |
859 | if (retval != E_NONE) { | |
860 | DBG_ERR(("hfs_mountfs: can't open raw device for journal: %s, %x\n", rawbuf, nd.ni_vp->v_rdev)); | |
861 | FREE(rawbuf, M_HFSMNT); | |
862 | goto error_exit; | |
863 | } | |
864 | ||
865 | *rvp = nd.ni_vp; | |
866 | if ((retval = VOP_OPEN(*rvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))) { | |
867 | *rvp = NULL; | |
868 | goto error_exit; | |
869 | } | |
870 | ||
871 | // don't need this any more | |
872 | FREE(rawbuf, M_HFSMNT); | |
873 | ||
874 | return 0; | |
875 | ||
876 | error_exit: | |
877 | if (*rvp) { | |
878 | (void)VOP_CLOSE(*rvp, ronly ? FREAD : FREAD|FWRITE, cred, p); | |
879 | } | |
880 | ||
881 | if (rawbuf) { | |
882 | FREE(rawbuf, M_HFSMNT); | |
883 | } | |
884 | return retval; | |
885 | } | |
886 | ||
887 | ||
888 | ||
1c79356b A |
889 | /* |
890 | * Common code for mount and mountroot | |
891 | */ | |
9bccf70c A |
892 | static int |
893 | hfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p, | |
894 | struct hfs_mount_args *args) | |
1c79356b | 895 | { |
9bccf70c A |
896 | int retval = E_NONE; |
897 | struct hfsmount *hfsmp; | |
898 | struct buf *bp; | |
899 | dev_t dev; | |
900 | HFSMasterDirectoryBlock *mdbp; | |
901 | int ronly; | |
902 | int i; | |
903 | int mntwrapper; | |
904 | struct ucred *cred; | |
d52fe63f A |
905 | u_int64_t disksize; |
906 | u_int64_t blkcnt; | |
907 | u_int32_t blksize; | |
908 | u_int32_t minblksize; | |
9bccf70c | 909 | u_int32_t iswritable; |
b4c24cb9 | 910 | daddr_t mdb_offset; |
1c79356b | 911 | |
9bccf70c A |
912 | dev = devvp->v_rdev; |
913 | cred = p ? p->p_ucred : NOCRED; | |
914 | mntwrapper = 0; | |
1c79356b A |
915 | /* |
916 | * Disallow multiple mounts of the same device. | |
917 | * Disallow mounting of a device that is currently in use | |
918 | * (except for root, which might share swap device for miniroot). | |
919 | * Flush out any old buffers remaining from a previous use. | |
920 | */ | |
921 | if ((retval = vfs_mountedon(devvp))) | |
922 | return (retval); | |
923 | if ((vcount(devvp) > 1) && (devvp != rootvp)) | |
924 | return (EBUSY); | |
925 | if ((retval = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0))) | |
926 | return (retval); | |
927 | ||
928 | ronly = (mp->mnt_flag & MNT_RDONLY) != 0; | |
1c79356b A |
929 | if ((retval = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))) |
930 | return (retval); | |
931 | ||
d52fe63f A |
932 | bp = NULL; |
933 | hfsmp = NULL; | |
9bccf70c | 934 | mdbp = NULL; |
d52fe63f | 935 | minblksize = kHFSBlockSize; |
1c79356b | 936 | |
d52fe63f A |
937 | /* Get the real physical block size. */ |
938 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blksize, 0, cred, p)) { | |
939 | retval = ENXIO; | |
940 | goto error_exit; | |
941 | } | |
942 | /* Switch to 512 byte sectors (temporarily) */ | |
943 | if (blksize > 512) { | |
944 | u_int32_t size512 = 512; | |
945 | ||
946 | if (VOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, cred, p)) { | |
947 | retval = ENXIO; | |
948 | goto error_exit; | |
949 | } | |
950 | } | |
951 | /* Get the number of 512 byte physical blocks. */ | |
952 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, cred, p)) { | |
953 | retval = ENXIO; | |
954 | goto error_exit; | |
955 | } | |
956 | /* Compute an accurate disk size (i.e. within 512 bytes) */ | |
957 | disksize = blkcnt * (u_int64_t)512; | |
1c79356b | 958 | |
d52fe63f | 959 | /* |
9bccf70c A |
960 | * There are only 31 bits worth of block count in |
961 | * the buffer cache. So for large volumes a 4K | |
962 | * physical block size is needed. | |
1c79356b | 963 | */ |
d52fe63f A |
964 | if (blkcnt > (u_int64_t)0x000000007fffffff) { |
965 | minblksize = blksize = 4096; | |
966 | } | |
d52fe63f A |
967 | /* Now switch to our prefered physical block size. */ |
968 | if (blksize > 512) { | |
969 | if (VOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, cred, p)) { | |
970 | retval = ENXIO; | |
971 | goto error_exit; | |
972 | } | |
973 | /* Get the count of physical blocks. */ | |
974 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, cred, p)) { | |
975 | retval = ENXIO; | |
976 | goto error_exit; | |
977 | } | |
978 | } | |
979 | ||
980 | /* | |
981 | * At this point: | |
982 | * minblksize is the minimum physical block size | |
983 | * blksize has our prefered physical block size | |
984 | * blkcnt has the total number of physical blocks | |
1c79356b | 985 | */ |
1c79356b A |
986 | devvp->v_specsize = blksize; |
987 | ||
0b4e3aa0 A |
988 | /* cache the IO attributes */ |
989 | if ((retval = vfs_init_io_attributes(devvp, mp))) { | |
990 | printf("hfs_mountfs: vfs_init_io_attributes returned %d\n", | |
991 | retval); | |
992 | return (retval); | |
993 | } | |
994 | ||
b4c24cb9 | 995 | mdb_offset = HFS_PRI_SECTOR(blksize); |
d52fe63f A |
996 | if ((retval = meta_bread(devvp, HFS_PRI_SECTOR(blksize), blksize, cred, &bp))) { |
997 | goto error_exit; | |
998 | } | |
9bccf70c A |
999 | MALLOC(mdbp, HFSMasterDirectoryBlock *, kMDBSize, M_TEMP, M_WAITOK); |
1000 | bcopy(bp->b_data + HFS_PRI_OFFSET(blksize), mdbp, kMDBSize); | |
1001 | brelse(bp); | |
1002 | bp = NULL; | |
1c79356b | 1003 | |
d52fe63f A |
1004 | MALLOC(hfsmp, struct hfsmount *, sizeof(struct hfsmount), M_HFSMNT, M_WAITOK); |
1005 | bzero(hfsmp, sizeof(struct hfsmount)); | |
b4c24cb9 | 1006 | |
9bccf70c A |
1007 | /* |
1008 | * Init the volume information structure | |
1009 | */ | |
1010 | mp->mnt_data = (qaddr_t)hfsmp; | |
1011 | hfsmp->hfs_mp = mp; /* Make VFSTOHFS work */ | |
1012 | hfsmp->hfs_vcb.vcb_hfsmp = hfsmp; /* Make VCBTOHFS work */ | |
1013 | hfsmp->hfs_raw_dev = devvp->v_rdev; | |
1014 | hfsmp->hfs_devvp = devvp; | |
1015 | hfsmp->hfs_phys_block_size = blksize; | |
1016 | hfsmp->hfs_phys_block_count = blkcnt; | |
55e303ae A |
1017 | hfsmp->hfs_flags |= HFS_WRITEABLE_MEDIA; |
1018 | if (ronly) | |
1019 | hfsmp->hfs_flags |= HFS_READ_ONLY; | |
1020 | if (mp->mnt_flag & MNT_UNKNOWNPERMISSIONS) | |
1021 | hfsmp->hfs_flags |= HFS_UNKNOWN_PERMS; | |
9bccf70c A |
1022 | for (i = 0; i < MAXQUOTAS; i++) |
1023 | hfsmp->hfs_qfiles[i].qf_vp = NULLVP; | |
1024 | ||
1c79356b A |
1025 | if (args) { |
1026 | hfsmp->hfs_uid = (args->hfs_uid == (uid_t)VNOVAL) ? UNKNOWNUID : args->hfs_uid; | |
1027 | if (hfsmp->hfs_uid == 0xfffffffd) hfsmp->hfs_uid = UNKNOWNUID; | |
1028 | hfsmp->hfs_gid = (args->hfs_gid == (gid_t)VNOVAL) ? UNKNOWNGID : args->hfs_gid; | |
1029 | if (hfsmp->hfs_gid == 0xfffffffd) hfsmp->hfs_gid = UNKNOWNGID; | |
1030 | if (args->hfs_mask != (mode_t)VNOVAL) { | |
1031 | hfsmp->hfs_dir_mask = args->hfs_mask & ALLPERMS; | |
1032 | if (args->flags & HFSFSMNT_NOXONFILES) { | |
1033 | hfsmp->hfs_file_mask = (args->hfs_mask & DEFFILEMODE); | |
1034 | } else { | |
1035 | hfsmp->hfs_file_mask = args->hfs_mask & ALLPERMS; | |
1036 | } | |
1037 | } else { | |
1038 | hfsmp->hfs_dir_mask = UNKNOWNPERMISSIONS & ALLPERMS; /* 0777: rwx---rwx */ | |
1039 | hfsmp->hfs_file_mask = UNKNOWNPERMISSIONS & DEFFILEMODE; /* 0666: no --x by default? */ | |
9bccf70c A |
1040 | } |
1041 | if ((args->flags != (int)VNOVAL) && (args->flags & HFSFSMNT_WRAPPER)) | |
1042 | mntwrapper = 1; | |
1c79356b A |
1043 | } else { |
1044 | /* Even w/o explicit mount arguments, MNT_UNKNOWNPERMISSIONS requires setting up uid, gid, and mask: */ | |
1045 | if (mp->mnt_flag & MNT_UNKNOWNPERMISSIONS) { | |
1046 | hfsmp->hfs_uid = UNKNOWNUID; | |
1047 | hfsmp->hfs_gid = UNKNOWNGID; | |
1048 | hfsmp->hfs_dir_mask = UNKNOWNPERMISSIONS & ALLPERMS; /* 0777: rwx---rwx */ | |
1049 | hfsmp->hfs_file_mask = UNKNOWNPERMISSIONS & DEFFILEMODE; /* 0666: no --x by default? */ | |
9bccf70c A |
1050 | } |
1051 | } | |
1052 | ||
1053 | /* Find out if disk media is writable. */ | |
1054 | if (VOP_IOCTL(devvp, DKIOCISWRITABLE, (caddr_t)&iswritable, 0, cred, p) == 0) { | |
1055 | if (iswritable) | |
55e303ae | 1056 | hfsmp->hfs_flags |= HFS_WRITEABLE_MEDIA; |
9bccf70c | 1057 | else |
55e303ae | 1058 | hfsmp->hfs_flags &= ~HFS_WRITEABLE_MEDIA; |
9bccf70c | 1059 | } |
1c79356b | 1060 | |
d52fe63f A |
1061 | /* Mount a standard HFS disk */ |
1062 | if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && | |
9bccf70c | 1063 | (mntwrapper || (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord))) { |
d52fe63f A |
1064 | if (devvp == rootvp) { |
1065 | retval = EINVAL; /* Cannot root from HFS standard disks */ | |
1c79356b | 1066 | goto error_exit; |
d52fe63f A |
1067 | } |
1068 | /* HFS disks can only use 512 byte physical blocks */ | |
1069 | if (blksize > kHFSBlockSize) { | |
1070 | blksize = kHFSBlockSize; | |
1071 | if (VOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, cred, p)) { | |
1072 | retval = ENXIO; | |
1073 | goto error_exit; | |
1074 | } | |
1075 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, cred, p)) { | |
1076 | retval = ENXIO; | |
1077 | goto error_exit; | |
1078 | } | |
d52fe63f A |
1079 | devvp->v_specsize = blksize; |
1080 | hfsmp->hfs_phys_block_size = blksize; | |
1081 | hfsmp->hfs_phys_block_count = blkcnt; | |
1082 | } | |
1c79356b A |
1083 | if (args) { |
1084 | hfsmp->hfs_encoding = args->hfs_encoding; | |
1085 | HFSTOVCB(hfsmp)->volumeNameEncodingHint = args->hfs_encoding; | |
1086 | ||
1c79356b A |
1087 | /* establish the timezone */ |
1088 | gTimeZone = args->hfs_timezone; | |
1089 | } | |
1090 | ||
9bccf70c A |
1091 | retval = hfs_getconverter(hfsmp->hfs_encoding, &hfsmp->hfs_get_unicode, |
1092 | &hfsmp->hfs_get_hfsname); | |
d52fe63f A |
1093 | if (retval) |
1094 | goto error_exit; | |
1c79356b | 1095 | |
d52fe63f | 1096 | retval = hfs_MountHFSVolume(hfsmp, mdbp, p); |
1c79356b A |
1097 | if (retval) |
1098 | (void) hfs_relconverter(hfsmp->hfs_encoding); | |
1099 | ||
d52fe63f A |
1100 | } else /* Mount an HFS Plus disk */ { |
1101 | HFSPlusVolumeHeader *vhp; | |
1102 | off_t embeddedOffset; | |
b4c24cb9 | 1103 | int jnl_disable = 0; |
d52fe63f A |
1104 | |
1105 | /* Get the embedded Volume Header */ | |
1106 | if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) { | |
1107 | embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * kHFSBlockSize; | |
1108 | embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) * | |
1109 | (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz); | |
1110 | ||
d52fe63f A |
1111 | /* |
1112 | * If the embedded volume doesn't start on a block | |
1113 | * boundary, then switch the device to a 512-byte | |
1114 | * block size so everything will line up on a block | |
1115 | * boundary. | |
1116 | */ | |
1117 | if ((embeddedOffset % blksize) != 0) { | |
1118 | printf("HFS Mount: embedded volume offset not" | |
1119 | " a multiple of physical block size (%d);" | |
1120 | " switching to 512\n", blksize); | |
1121 | blksize = 512; | |
1122 | if (VOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, | |
1123 | (caddr_t)&blksize, FWRITE, cred, p)) { | |
1124 | retval = ENXIO; | |
1125 | goto error_exit; | |
1126 | } | |
1127 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, | |
1128 | (caddr_t)&blkcnt, 0, cred, p)) { | |
1129 | retval = ENXIO; | |
1130 | goto error_exit; | |
1131 | } | |
1132 | /* XXX do we need to call vfs_init_io_attributes again? */ | |
1133 | devvp->v_specsize = blksize; | |
1134 | /* Note: relative block count adjustment */ | |
1135 | hfsmp->hfs_phys_block_count *= | |
1136 | hfsmp->hfs_phys_block_size / blksize; | |
1137 | hfsmp->hfs_phys_block_size = blksize; | |
1138 | } | |
1139 | ||
9bccf70c A |
1140 | disksize = (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.blockCount) * |
1141 | (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz); | |
1142 | ||
1143 | hfsmp->hfs_phys_block_count = disksize / blksize; | |
1144 | ||
b4c24cb9 A |
1145 | mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize); |
1146 | retval = meta_bread(devvp, mdb_offset, blksize, cred, &bp); | |
d52fe63f A |
1147 | if (retval) |
1148 | goto error_exit; | |
9bccf70c A |
1149 | bcopy(bp->b_data + HFS_PRI_OFFSET(blksize), mdbp, 512); |
1150 | brelse(bp); | |
1151 | bp = NULL; | |
1152 | vhp = (HFSPlusVolumeHeader*) mdbp; | |
d52fe63f A |
1153 | |
1154 | } else /* pure HFS+ */ { | |
1155 | embeddedOffset = 0; | |
1156 | vhp = (HFSPlusVolumeHeader*) mdbp; | |
1157 | } | |
1158 | ||
b4c24cb9 A |
1159 | // XXXdbg |
1160 | // | |
1161 | hfsmp->jnl = NULL; | |
1162 | hfsmp->jvp = NULL; | |
1163 | if (args != NULL && (args->flags & HFSFSMNT_EXTENDED_ARGS) && args->journal_disable) { | |
1164 | jnl_disable = 1; | |
1165 | } | |
1166 | ||
1167 | // | |
1168 | // We only initialize the journal here if the last person | |
1169 | // to mount this volume was journaling aware. Otherwise | |
1170 | // we delay journal initialization until later at the end | |
1171 | // of hfs_MountHFSPlusVolume() because the last person who | |
1172 | // mounted it could have messed things up behind our back | |
1173 | // (so we need to go find the .journal file, make sure it's | |
1174 | // the right size, re-sync up if it was moved, etc). | |
1175 | // | |
1176 | if ( (SWAP_BE32(vhp->lastMountedVersion) == kHFSJMountVersion) | |
1177 | && (SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) | |
1178 | && !jnl_disable) { | |
1179 | ||
1180 | // if we're able to init the journal, mark the mount | |
1181 | // point as journaled. | |
1182 | // | |
1183 | if (hfs_early_journal_init(hfsmp, vhp, args, embeddedOffset, mdb_offset, mdbp, cred) == 0) { | |
1184 | mp->mnt_flag |= MNT_JOURNALED; | |
1185 | } else { | |
55e303ae A |
1186 | // if the journal failed to open, then set the lastMountedVersion |
1187 | // to be "FSK!" which fsck_hfs will see and force the fsck instead | |
1188 | // of just bailing out because the volume is journaled. | |
1189 | if (ronly != 0 || devvp == rootvp) { | |
1190 | HFSPlusVolumeHeader *vhp; | |
1191 | ||
1192 | hfsmp->hfs_flags |= HFS_NEED_JNL_RESET; | |
1193 | ||
1194 | if (mdb_offset == 0) { | |
1195 | mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize); | |
1196 | } | |
1197 | ||
1198 | bp = NULL; | |
1199 | retval = meta_bread(devvp, mdb_offset, blksize, cred, &bp); | |
1200 | if (retval == 0) { | |
1201 | vhp = (HFSPlusVolumeHeader *)(bp->b_data + HFS_PRI_OFFSET(blksize)); | |
1202 | ||
1203 | if (SWAP_BE16(vhp->signature) == kHFSPlusSigWord || SWAP_BE16(vhp->signature) == kHFSXSigWord) { | |
1204 | vhp->lastMountedVersion = SWAP_BE32('FSK!'); | |
1205 | bwrite(bp); | |
1206 | } else { | |
1207 | brelse(bp); | |
1208 | } | |
1209 | bp = NULL; | |
1210 | } else if (bp) { | |
1211 | brelse(bp); | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | // if this isn't the root device just bail out. | |
1216 | // if it is the root device we just continue on | |
1217 | // in the hopes that fsck_hfs will be able to | |
1218 | // fix any damage that exists on the volume. | |
1219 | if (devvp != rootvp) { | |
1220 | retval = EINVAL; | |
1221 | goto error_exit; | |
1222 | } | |
b4c24cb9 A |
1223 | } |
1224 | } | |
1225 | // XXXdbg | |
1226 | ||
d52fe63f A |
1227 | (void) hfs_getconverter(0, &hfsmp->hfs_get_unicode, &hfsmp->hfs_get_hfsname); |
1228 | ||
b4c24cb9 | 1229 | retval = hfs_MountHFSPlusVolume(hfsmp, vhp, embeddedOffset, disksize, p, args); |
d52fe63f A |
1230 | /* |
1231 | * If the backend didn't like our physical blocksize | |
1232 | * then retry with physical blocksize of 512. | |
1233 | */ | |
1234 | if ((retval == ENXIO) && (blksize > 512) && (blksize != minblksize)) { | |
1235 | printf("HFS Mount: could not use physical block size " | |
1236 | "(%d) switching to 512\n", blksize); | |
1237 | blksize = 512; | |
1238 | if (VOP_IOCTL(devvp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, cred, p)) { | |
1239 | retval = ENXIO; | |
1240 | goto error_exit; | |
1241 | } | |
1242 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, cred, p)) { | |
1243 | retval = ENXIO; | |
1244 | goto error_exit; | |
1245 | } | |
d52fe63f A |
1246 | devvp->v_specsize = blksize; |
1247 | /* Note: relative block count adjustment (in case this is an embedded volume). */ | |
1248 | hfsmp->hfs_phys_block_count *= hfsmp->hfs_phys_block_size / blksize; | |
1249 | hfsmp->hfs_phys_block_size = blksize; | |
1250 | ||
55e303ae A |
1251 | if (hfsmp->jnl) { |
1252 | // close and re-open this with the new block size | |
1253 | journal_close(hfsmp->jnl); | |
1254 | hfsmp->jnl = NULL; | |
1255 | if (hfs_early_journal_init(hfsmp, vhp, args, embeddedOffset, mdb_offset, mdbp, cred) == 0) { | |
1256 | mp->mnt_flag |= MNT_JOURNALED; | |
1257 | } | |
1258 | } | |
1259 | ||
d52fe63f | 1260 | /* Try again with a smaller block size... */ |
b4c24cb9 | 1261 | retval = hfs_MountHFSPlusVolume(hfsmp, vhp, embeddedOffset, disksize, p, args); |
d52fe63f A |
1262 | } |
1263 | if (retval) | |
1264 | (void) hfs_relconverter(0); | |
1265 | } | |
1c79356b A |
1266 | |
1267 | if ( retval ) { | |
1268 | goto error_exit; | |
1269 | } | |
1270 | ||
9bccf70c A |
1271 | mp->mnt_stat.f_fsid.val[0] = (long)dev; |
1272 | mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; | |
1273 | mp->mnt_maxsymlinklen = 0; | |
1274 | devvp->v_specflags |= SI_MOUNTEDON; | |
1c79356b | 1275 | |
55e303ae A |
1276 | if (args) { |
1277 | /* | |
1278 | * Set the free space warning levels for a non-root volume: | |
1279 | * | |
1280 | * Set the lower freespace limit (the level that will trigger a warning) | |
1281 | * to 5% of the volume size or 250MB, whichever is less, and the desired | |
1282 | * level (which will cancel the alert request) to 1/2 above that limit. | |
1283 | * Start looking for free space to drop below this level and generate a | |
1284 | * warning immediately if needed: | |
1285 | */ | |
1286 | hfsmp->hfs_freespace_notify_warninglimit = | |
1287 | MIN(HFS_LOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize, | |
1288 | (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_LOWDISKTRIGGERFRACTION); | |
1289 | hfsmp->hfs_freespace_notify_desiredlevel = | |
1290 | MIN(HFS_LOWDISKSHUTOFFLEVEL / HFSTOVCB(hfsmp)->blockSize, | |
1291 | (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_LOWDISKSHUTOFFFRACTION); | |
1292 | } else { | |
1293 | /* | |
1294 | * Set the free space warning levels for the root volume: | |
1295 | * | |
1296 | * Set the lower freespace limit (the level that will trigger a warning) | |
1297 | * to 1% of the volume size or 50MB, whichever is less, and the desired | |
1298 | * level (which will cancel the alert request) to 2% or 75MB, whichever is less. | |
1299 | */ | |
1300 | hfsmp->hfs_freespace_notify_warninglimit = | |
1301 | MIN(HFS_ROOTLOWDISKTRIGGERLEVEL / HFSTOVCB(hfsmp)->blockSize, | |
1302 | (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTLOWDISKTRIGGERFRACTION); | |
1303 | hfsmp->hfs_freespace_notify_desiredlevel = | |
1304 | MIN(HFS_ROOTLOWDISKSHUTOFFLEVEL / HFSTOVCB(hfsmp)->blockSize, | |
1305 | (HFSTOVCB(hfsmp)->totalBlocks / 100) * HFS_ROOTLOWDISKSHUTOFFFRACTION); | |
1306 | }; | |
1307 | ||
1308 | /* | |
1309 | * Start looking for free space to drop below this level and generate a | |
1310 | * warning immediately if needed: | |
1311 | */ | |
1312 | hfsmp->hfs_notification_conditions = 0; | |
1313 | hfs_generate_volume_notifications(hfsmp); | |
1314 | ||
9bccf70c A |
1315 | if (ronly == 0) { |
1316 | (void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0); | |
1317 | } | |
1318 | FREE(mdbp, M_TEMP); | |
1319 | return (0); | |
1c79356b | 1320 | |
9bccf70c A |
1321 | error_exit: |
1322 | if (bp) | |
1323 | brelse(bp); | |
1324 | if (mdbp) | |
1325 | FREE(mdbp, M_TEMP); | |
1326 | (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p); | |
b4c24cb9 A |
1327 | if (hfsmp && hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) { |
1328 | (void)VOP_CLOSE(hfsmp->jvp, ronly ? FREAD : FREAD|FWRITE, cred, p); | |
1329 | hfsmp->jvp = NULL; | |
1330 | } | |
9bccf70c A |
1331 | if (hfsmp) { |
1332 | FREE(hfsmp, M_HFSMNT); | |
1333 | mp->mnt_data = (qaddr_t)0; | |
1334 | } | |
1c79356b A |
1335 | return (retval); |
1336 | } | |
1337 | ||
1338 | ||
1339 | /* | |
1340 | * Make a filesystem operational. | |
1341 | * Nothing to do at the moment. | |
1342 | */ | |
1343 | /* ARGSUSED */ | |
9bccf70c A |
1344 | static int |
1345 | hfs_start(mp, flags, p) | |
1346 | struct mount *mp; | |
1347 | int flags; | |
1348 | struct proc *p; | |
1c79356b | 1349 | { |
9bccf70c | 1350 | return (0); |
1c79356b A |
1351 | } |
1352 | ||
1353 | ||
1354 | /* | |
1355 | * unmount system call | |
1356 | */ | |
9bccf70c | 1357 | static int |
1c79356b A |
1358 | hfs_unmount(mp, mntflags, p) |
1359 | struct mount *mp; | |
1360 | int mntflags; | |
1361 | struct proc *p; | |
1362 | { | |
1363 | struct hfsmount *hfsmp = VFSTOHFS(mp); | |
1364 | int retval = E_NONE; | |
1365 | int flags; | |
9bccf70c | 1366 | int force; |
b4c24cb9 | 1367 | int started_tr = 0, grabbed_lock = 0; |
1c79356b A |
1368 | |
1369 | flags = 0; | |
9bccf70c A |
1370 | force = 0; |
1371 | if (mntflags & MNT_FORCE) { | |
1c79356b | 1372 | flags |= FORCECLOSE; |
9bccf70c A |
1373 | force = 1; |
1374 | } | |
1c79356b | 1375 | |
9bccf70c | 1376 | if ((retval = hfs_flushfiles(mp, flags, p)) && !force) |
1c79356b A |
1377 | return (retval); |
1378 | ||
55e303ae A |
1379 | if (hfsmp->hfs_flags & HFS_METADATA_ZONE) |
1380 | (void) hfs_recording_suspend(hfsmp, p); | |
1381 | ||
1c79356b A |
1382 | /* |
1383 | * Flush out the b-trees, volume bitmap and Volume Header | |
1384 | */ | |
55e303ae | 1385 | if ((hfsmp->hfs_flags & HFS_READ_ONLY) == 0) { |
b4c24cb9 A |
1386 | hfs_global_shared_lock_acquire(hfsmp); |
1387 | grabbed_lock = 1; | |
55e303ae | 1388 | if (hfsmp->jnl) { |
b4c24cb9 A |
1389 | journal_start_transaction(hfsmp->jnl); |
1390 | started_tr = 1; | |
1391 | } | |
1392 | ||
1c79356b | 1393 | retval = VOP_FSYNC(HFSTOVCB(hfsmp)->catalogRefNum, NOCRED, MNT_WAIT, p); |
9bccf70c | 1394 | if (retval && !force) |
b4c24cb9 A |
1395 | goto err_exit; |
1396 | ||
1c79356b | 1397 | retval = VOP_FSYNC(HFSTOVCB(hfsmp)->extentsRefNum, NOCRED, MNT_WAIT, p); |
9bccf70c | 1398 | if (retval && !force) |
b4c24cb9 A |
1399 | goto err_exit; |
1400 | ||
1401 | // if we have an allocation file, sync it too so we don't leave dirty | |
1402 | // blocks around | |
1403 | if (HFSTOVCB(hfsmp)->allocationsRefNum) { | |
1404 | if (retval = VOP_FSYNC(HFSTOVCB(hfsmp)->allocationsRefNum, NOCRED, MNT_WAIT, p)) { | |
1405 | if (!force) | |
1406 | goto err_exit; | |
1407 | } | |
1408 | } | |
1c79356b | 1409 | |
55e303ae A |
1410 | if (hfsmp->hfc_filevp && (hfsmp->hfc_filevp->v_flag & VSYSTEM)) { |
1411 | retval = VOP_FSYNC(hfsmp->hfc_filevp, NOCRED, MNT_WAIT, p); | |
1412 | if (retval && !force) | |
1413 | goto err_exit; | |
1414 | } | |
1415 | ||
1c79356b | 1416 | if (retval = VOP_FSYNC(hfsmp->hfs_devvp, NOCRED, MNT_WAIT, p)) { |
9bccf70c | 1417 | if (!force) |
b4c24cb9 | 1418 | goto err_exit; |
1c79356b | 1419 | } |
55e303ae A |
1420 | |
1421 | #if 0 | |
1c79356b A |
1422 | /* See if this volume is damaged, is so do not unmount cleanly */ |
1423 | if (HFSTOVCB(hfsmp)->vcbFlags & kHFS_DamagedVolume) { | |
1c79356b A |
1424 | HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeUnmountedMask; |
1425 | } else { | |
9bccf70c | 1426 | HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeUnmountedMask; |
1c79356b | 1427 | } |
55e303ae A |
1428 | #else |
1429 | HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeUnmountedMask; | |
1430 | #endif | |
b4c24cb9 | 1431 | retval = hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1); |
1c79356b | 1432 | if (retval) { |
1c79356b | 1433 | HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeUnmountedMask; |
9bccf70c | 1434 | if (!force) |
b4c24cb9 A |
1435 | goto err_exit; /* could not flush everything */ |
1436 | } | |
1437 | ||
1438 | if (hfsmp->jnl) { | |
1439 | journal_end_transaction(hfsmp->jnl); | |
1440 | started_tr = 0; | |
1441 | } | |
1442 | if (grabbed_lock) { | |
1443 | hfs_global_shared_lock_release(hfsmp); | |
1444 | grabbed_lock = 0; | |
1c79356b A |
1445 | } |
1446 | } | |
1447 | ||
b4c24cb9 A |
1448 | if (hfsmp->jnl) { |
1449 | journal_flush(hfsmp->jnl); | |
1450 | } | |
1451 | ||
1c79356b A |
1452 | /* |
1453 | * Invalidate our caches and release metadata vnodes | |
1454 | */ | |
1455 | (void) hfsUnmount(hfsmp, p); | |
1456 | ||
55e303ae A |
1457 | /* |
1458 | * Last chance to dump unreferenced system files. | |
1459 | */ | |
1460 | (void) vflush(mp, NULLVP, FORCECLOSE); | |
1461 | ||
1c79356b A |
1462 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) |
1463 | (void) hfs_relconverter(hfsmp->hfs_encoding); | |
1464 | ||
b4c24cb9 A |
1465 | // XXXdbg |
1466 | if (hfsmp->jnl) { | |
1467 | journal_close(hfsmp->jnl); | |
55e303ae | 1468 | hfsmp->jnl = NULL; |
b4c24cb9 A |
1469 | } |
1470 | ||
1471 | if (hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) { | |
55e303ae A |
1472 | retval = VOP_CLOSE(hfsmp->jvp, |
1473 | hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, | |
b4c24cb9 A |
1474 | NOCRED, p); |
1475 | vrele(hfsmp->jvp); | |
55e303ae | 1476 | hfsmp->jvp = NULL; |
b4c24cb9 A |
1477 | } |
1478 | // XXXdbg | |
1479 | ||
55e303ae A |
1480 | #ifdef HFS_SPARSE_DEV |
1481 | /* Drop our reference on the backing fs (if any). */ | |
1482 | if ((hfsmp->hfs_flags & HFS_HAS_SPARSE_DEVICE) && hfsmp->hfs_backingfs_rootvp) { | |
1483 | struct vnode * tmpvp; | |
1484 | ||
1485 | hfsmp->hfs_flags &= ~HFS_HAS_SPARSE_DEVICE; | |
1486 | tmpvp = hfsmp->hfs_backingfs_rootvp; | |
1487 | hfsmp->hfs_backingfs_rootvp = NULLVP; | |
1488 | vrele(tmpvp); | |
1489 | } | |
1490 | #endif /* HFS_SPARSE_DEV */ | |
1491 | ||
1c79356b | 1492 | hfsmp->hfs_devvp->v_specflags &= ~SI_MOUNTEDON; |
9bccf70c | 1493 | retval = VOP_CLOSE(hfsmp->hfs_devvp, |
55e303ae A |
1494 | hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, |
1495 | NOCRED, p); | |
9bccf70c A |
1496 | if (retval && !force) |
1497 | return(retval); | |
1c79356b | 1498 | |
9bccf70c | 1499 | vrele(hfsmp->hfs_devvp); |
1c79356b A |
1500 | FREE(hfsmp, M_HFSMNT); |
1501 | mp->mnt_data = (qaddr_t)0; | |
9bccf70c | 1502 | return (0); |
b4c24cb9 A |
1503 | |
1504 | err_exit: | |
1505 | if (hfsmp->jnl && started_tr) { | |
1506 | journal_end_transaction(hfsmp->jnl); | |
1507 | } | |
1508 | if (grabbed_lock) { | |
1509 | hfs_global_shared_lock_release(hfsmp); | |
1510 | } | |
1511 | return retval; | |
1c79356b A |
1512 | } |
1513 | ||
1514 | ||
1515 | /* | |
1516 | * Return the root of a filesystem. | |
1517 | * | |
1518 | * OUT - vpp, should be locked and vget()'d (to increment usecount and lock) | |
1519 | */ | |
9bccf70c A |
1520 | static int |
1521 | hfs_root(mp, vpp) | |
1522 | struct mount *mp; | |
1523 | struct vnode **vpp; | |
1c79356b | 1524 | { |
9bccf70c A |
1525 | struct vnode *nvp; |
1526 | int retval; | |
1527 | UInt32 rootObjID = kRootDirID; | |
1c79356b | 1528 | |
9bccf70c A |
1529 | if ((retval = VFS_VGET(mp, &rootObjID, &nvp))) |
1530 | return (retval); | |
1c79356b | 1531 | |
9bccf70c A |
1532 | *vpp = nvp; |
1533 | return (0); | |
1c79356b A |
1534 | } |
1535 | ||
1536 | ||
1537 | /* | |
1538 | * Do operations associated with quotas | |
1539 | */ | |
9bccf70c A |
1540 | int |
1541 | hfs_quotactl(mp, cmds, uid, arg, p) | |
1542 | struct mount *mp; | |
1543 | int cmds; | |
1544 | uid_t uid; | |
1545 | caddr_t arg; | |
1546 | struct proc *p; | |
1c79356b | 1547 | { |
9bccf70c A |
1548 | int cmd, type, error; |
1549 | ||
1550 | #if !QUOTA | |
1551 | return (EOPNOTSUPP); | |
1552 | #else | |
1553 | if (uid == -1) | |
1554 | uid = p->p_cred->p_ruid; | |
1555 | cmd = cmds >> SUBCMDSHIFT; | |
1556 | ||
1557 | switch (cmd) { | |
1558 | case Q_SYNC: | |
1559 | case Q_QUOTASTAT: | |
1560 | break; | |
1561 | case Q_GETQUOTA: | |
1562 | if (uid == p->p_cred->p_ruid) | |
1563 | break; | |
1564 | /* fall through */ | |
1565 | default: | |
1566 | if (error = suser(p->p_ucred, &p->p_acflag)) | |
1567 | return (error); | |
1568 | } | |
1569 | ||
1570 | type = cmds & SUBCMDMASK; | |
1571 | if ((u_int)type >= MAXQUOTAS) | |
1572 | return (EINVAL); | |
1573 | if (vfs_busy(mp, LK_NOWAIT, 0, p)) | |
1574 | return (0); | |
1575 | ||
1576 | switch (cmd) { | |
1c79356b | 1577 | |
9bccf70c A |
1578 | case Q_QUOTAON: |
1579 | error = hfs_quotaon(p, mp, type, arg, UIO_USERSPACE); | |
1580 | break; | |
1581 | ||
1582 | case Q_QUOTAOFF: | |
1583 | error = hfs_quotaoff(p, mp, type); | |
1584 | break; | |
1585 | ||
1586 | case Q_SETQUOTA: | |
1587 | error = hfs_setquota(mp, uid, type, arg); | |
1588 | break; | |
1589 | ||
1590 | case Q_SETUSE: | |
1591 | error = hfs_setuse(mp, uid, type, arg); | |
1592 | break; | |
1593 | ||
1594 | case Q_GETQUOTA: | |
1595 | error = hfs_getquota(mp, uid, type, arg); | |
1596 | break; | |
1597 | ||
1598 | case Q_SYNC: | |
1599 | error = hfs_qsync(mp); | |
1600 | break; | |
1601 | ||
1602 | case Q_QUOTASTAT: | |
1603 | error = hfs_quotastat(mp, type, arg); | |
1604 | break; | |
1605 | ||
1606 | default: | |
1607 | error = EINVAL; | |
1608 | break; | |
1609 | } | |
1610 | vfs_unbusy(mp, p); | |
1611 | return (error); | |
1612 | #endif /* QUOTA */ | |
1c79356b A |
1613 | } |
1614 | ||
1615 | ||
b4c24cb9 A |
1616 | |
1617 | ||
1c79356b A |
1618 | /* |
1619 | * Get file system statistics. | |
1620 | */ | |
1621 | static int | |
1622 | hfs_statfs(mp, sbp, p) | |
1623 | struct mount *mp; | |
1624 | register struct statfs *sbp; | |
1625 | struct proc *p; | |
1626 | { | |
1627 | ExtendedVCB *vcb = VFSTOVCB(mp); | |
1628 | struct hfsmount *hfsmp = VFSTOHFS(mp); | |
1629 | u_long freeCNIDs; | |
1630 | ||
1c79356b A |
1631 | freeCNIDs = (u_long)0xFFFFFFFF - (u_long)vcb->vcbNxtCNID; |
1632 | ||
1633 | sbp->f_bsize = vcb->blockSize; | |
1634 | sbp->f_iosize = hfsmp->hfs_logBlockSize; | |
1635 | sbp->f_blocks = vcb->totalBlocks; | |
9bccf70c A |
1636 | sbp->f_bfree = hfs_freeblks(hfsmp, 0); |
1637 | sbp->f_bavail = hfs_freeblks(hfsmp, 1); | |
1c79356b | 1638 | sbp->f_files = vcb->totalBlocks - 2; /* max files is constrained by total blocks */ |
9bccf70c | 1639 | sbp->f_ffree = MIN(freeCNIDs, sbp->f_bavail); |
1c79356b A |
1640 | |
1641 | sbp->f_type = 0; | |
1642 | if (sbp != &mp->mnt_stat) { | |
1643 | sbp->f_type = mp->mnt_vfc->vfc_typenum; | |
1644 | bcopy((caddr_t)mp->mnt_stat.f_mntonname, | |
1645 | (caddr_t)&sbp->f_mntonname[0], MNAMELEN); | |
1646 | bcopy((caddr_t)mp->mnt_stat.f_mntfromname, | |
1647 | (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); | |
1648 | } | |
1649 | return (0); | |
1650 | } | |
1651 | ||
1652 | ||
b4c24cb9 A |
1653 | // |
1654 | // XXXdbg -- this is a callback to be used by the journal to | |
1655 | // get meta data blocks flushed out to disk. | |
1656 | // | |
1657 | // XXXdbg -- be smarter and don't flush *every* block on each | |
1658 | // call. try to only flush some so we don't wind up | |
1659 | // being too synchronous. | |
1660 | // | |
1661 | __private_extern__ | |
1662 | void | |
1663 | hfs_sync_metadata(void *arg) | |
1664 | { | |
1665 | struct mount *mp = (struct mount *)arg; | |
1666 | struct cnode *cp; | |
1667 | struct hfsmount *hfsmp; | |
1668 | ExtendedVCB *vcb; | |
1669 | struct vnode *meta_vp[3]; | |
1670 | struct buf *bp; | |
1671 | int i, sectorsize, priIDSector, altIDSector, retval; | |
1672 | int error, allerror = 0; | |
1673 | ||
1674 | hfsmp = VFSTOHFS(mp); | |
1675 | vcb = HFSTOVCB(hfsmp); | |
1676 | ||
1677 | bflushq(BQ_META, mp); | |
1678 | ||
1679 | ||
1680 | #if 1 // XXXdbg - I do not believe this is necessary... | |
1681 | // but if I pull it out, then the journal | |
1682 | // does not seem to get flushed properly | |
1683 | // when it is closed.... | |
1684 | ||
1685 | // now make sure the super block is flushed | |
1686 | sectorsize = hfsmp->hfs_phys_block_size; | |
1687 | priIDSector = (vcb->hfsPlusIOPosOffset / sectorsize) + | |
1688 | HFS_PRI_SECTOR(sectorsize); | |
1689 | retval = meta_bread(hfsmp->hfs_devvp, priIDSector, sectorsize, NOCRED, &bp); | |
1690 | if (retval != 0) { | |
1691 | panic("hfs: sync_metadata: can't read super-block?! (retval 0x%x, priIDSector)\n", | |
1692 | retval, priIDSector); | |
1693 | } | |
1694 | ||
1695 | if (retval == 0 && (bp->b_flags & B_DELWRI) && (bp->b_flags & B_LOCKED) == 0) { | |
1696 | bwrite(bp); | |
1697 | } else if (bp) { | |
1698 | brelse(bp); | |
1699 | } | |
1700 | ||
1701 | // the alternate super block... | |
1702 | // XXXdbg - we probably don't need to do this each and every time. | |
1703 | // hfs_btreeio.c:FlushAlternate() should flag when it was | |
1704 | // written... | |
1705 | altIDSector = (vcb->hfsPlusIOPosOffset / sectorsize) + | |
1706 | HFS_ALT_SECTOR(sectorsize, hfsmp->hfs_phys_block_count); | |
1707 | retval = meta_bread(hfsmp->hfs_devvp, altIDSector, sectorsize, NOCRED, &bp); | |
1708 | if (retval == 0 && (bp->b_flags & B_DELWRI) && (bp->b_flags & B_LOCKED) == 0) { | |
1709 | bwrite(bp); | |
1710 | } else if (bp) { | |
1711 | brelse(bp); | |
1712 | } | |
1713 | #endif | |
1714 | ||
1715 | } | |
1716 | ||
1c79356b A |
1717 | /* |
1718 | * Go through the disk queues to initiate sandbagged IO; | |
1719 | * go through the inodes to write those that have been modified; | |
1720 | * initiate the writing of the super block if it has been modified. | |
1721 | * | |
1722 | * Note: we are always called with the filesystem marked `MPBUSY'. | |
1723 | */ | |
9bccf70c A |
1724 | static int |
1725 | hfs_sync(mp, waitfor, cred, p) | |
1726 | struct mount *mp; | |
1727 | int waitfor; | |
1728 | struct ucred *cred; | |
1729 | struct proc *p; | |
1c79356b | 1730 | { |
9bccf70c A |
1731 | struct vnode *nvp, *vp; |
1732 | struct cnode *cp; | |
1733 | struct hfsmount *hfsmp; | |
1734 | ExtendedVCB *vcb; | |
1735 | struct vnode *meta_vp[3]; | |
1736 | int i; | |
1737 | int error, allerror = 0; | |
1c79356b A |
1738 | |
1739 | /* | |
1740 | * During MNT_UPDATE hfs_changefs might be manipulating | |
1741 | * vnodes so back off | |
1742 | */ | |
1743 | if (mp->mnt_flag & MNT_UPDATE) | |
1744 | return (0); | |
1745 | ||
9bccf70c | 1746 | hfsmp = VFSTOHFS(mp); |
55e303ae A |
1747 | if (hfsmp->hfs_flags & HFS_READ_ONLY) |
1748 | return (EROFS); | |
1c79356b | 1749 | |
b4c24cb9 A |
1750 | #if 0 |
1751 | // XXXdbg first go through and flush out any modified | |
1752 | // meta data blocks so they go out in order... | |
1753 | bflushq(BQ_META, mp); | |
1754 | bflushq(BQ_LRU, mp); | |
1755 | // only flush locked blocks if we're not doing journaling | |
1756 | if (hfsmp->jnl == NULL) { | |
1757 | bflushq(BQ_LOCKED, mp); | |
1758 | } | |
1759 | #endif | |
1760 | ||
9bccf70c A |
1761 | /* |
1762 | * Write back each 'modified' vnode | |
1763 | */ | |
1c79356b | 1764 | |
9bccf70c A |
1765 | loop: |
1766 | simple_lock(&mntvnode_slock); | |
1767 | for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { | |
0b4e3aa0 | 1768 | int didhold; |
9bccf70c A |
1769 | /* |
1770 | * If the vnode that we are about to sync is no longer | |
1771 | * associated with this mount point, start over. | |
1772 | */ | |
1773 | if (vp->v_mount != mp) { | |
1774 | simple_unlock(&mntvnode_slock); | |
1775 | goto loop; | |
1776 | } | |
b4c24cb9 | 1777 | |
9bccf70c A |
1778 | simple_lock(&vp->v_interlock); |
1779 | nvp = vp->v_mntvnodes.le_next; | |
b4c24cb9 | 1780 | |
9bccf70c A |
1781 | cp = VTOC(vp); |
1782 | ||
b4c24cb9 A |
1783 | // restart our whole search if this guy is locked |
1784 | // or being reclaimed. | |
55e303ae | 1785 | if (vp->v_tag != VT_HFS || cp == NULL || vp->v_flag & (VXLOCK|VORECLAIM)) { |
b4c24cb9 A |
1786 | simple_unlock(&vp->v_interlock); |
1787 | continue; | |
1788 | } | |
1789 | ||
9bccf70c A |
1790 | if ((vp->v_flag & VSYSTEM) || (vp->v_type == VNON) || |
1791 | (((cp->c_flag & (C_ACCESS | C_CHANGE | C_MODIFIED | C_UPDATE)) == 0) && | |
1792 | (vp->v_dirtyblkhd.lh_first == NULL) && !(vp->v_flag & VHASDIRTY))) { | |
1793 | simple_unlock(&vp->v_interlock); | |
1794 | simple_unlock(&mntvnode_slock); | |
1795 | simple_lock(&mntvnode_slock); | |
1796 | continue; | |
1797 | } | |
1798 | ||
1799 | simple_unlock(&mntvnode_slock); | |
1800 | error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); | |
1801 | if (error) { | |
ab86ba33 A |
1802 | if (error == ENOENT) { |
1803 | /* | |
1804 | * If vnode is being reclaimed, yield so | |
1805 | * that it can be removed from our list. | |
1806 | */ | |
1807 | if (UBCISVALID(vp)) | |
1808 | (void) tsleep((caddr_t)&lbolt, PINOD, "hfs_sync", 0); | |
9bccf70c | 1809 | goto loop; |
ab86ba33 | 1810 | } |
9bccf70c A |
1811 | simple_lock(&mntvnode_slock); |
1812 | continue; | |
1813 | } | |
1c79356b | 1814 | |
0b4e3aa0 | 1815 | didhold = ubc_hold(vp); |
55e303ae A |
1816 | |
1817 | // mark the cnode so that fsync won't flush | |
1818 | // the journal since we're going to do that... | |
1819 | cp->c_flag |= C_FROMSYNC; | |
9bccf70c A |
1820 | if ((error = VOP_FSYNC(vp, cred, waitfor, p))) { |
1821 | allerror = error; | |
1822 | }; | |
55e303ae A |
1823 | cp->c_flag &= ~C_FROMSYNC; |
1824 | ||
9bccf70c | 1825 | VOP_UNLOCK(vp, 0, p); |
0b4e3aa0 A |
1826 | if (didhold) |
1827 | ubc_rele(vp); | |
9bccf70c A |
1828 | vrele(vp); |
1829 | simple_lock(&mntvnode_slock); | |
1830 | }; | |
1c79356b | 1831 | |
9bccf70c A |
1832 | vcb = HFSTOVCB(hfsmp); |
1833 | ||
1834 | meta_vp[0] = vcb->extentsRefNum; | |
1835 | meta_vp[1] = vcb->catalogRefNum; | |
1836 | meta_vp[2] = vcb->allocationsRefNum; /* This is NULL for standard HFS */ | |
1837 | ||
1838 | /* Now sync our three metadata files */ | |
1839 | for (i = 0; i < 3; ++i) { | |
1840 | struct vnode *btvp; | |
1841 | ||
1842 | btvp = btvp = meta_vp[i];; | |
1843 | if ((btvp==0) || (btvp->v_type == VNON) || (btvp->v_mount != mp)) | |
1844 | continue; | |
b4c24cb9 | 1845 | |
9bccf70c A |
1846 | simple_lock(&btvp->v_interlock); |
1847 | cp = VTOC(btvp); | |
1848 | if (((cp->c_flag & (C_ACCESS | C_CHANGE | C_MODIFIED | C_UPDATE)) == 0) && | |
1849 | (btvp->v_dirtyblkhd.lh_first == NULL) && !(btvp->v_flag & VHASDIRTY)) { | |
1850 | simple_unlock(&btvp->v_interlock); | |
1851 | continue; | |
1852 | } | |
1853 | simple_unlock(&mntvnode_slock); | |
1854 | error = vget(btvp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); | |
1855 | if (error) { | |
1856 | simple_lock(&mntvnode_slock); | |
1857 | continue; | |
1858 | } | |
1859 | if ((error = VOP_FSYNC(btvp, cred, waitfor, p))) | |
1860 | allerror = error; | |
1861 | VOP_UNLOCK(btvp, 0, p); | |
1862 | vrele(btvp); | |
1863 | simple_lock(&mntvnode_slock); | |
1864 | }; | |
1865 | ||
1866 | simple_unlock(&mntvnode_slock); | |
1867 | ||
1868 | /* | |
1869 | * Force stale file system control information to be flushed. | |
1870 | */ | |
1871 | if (vcb->vcbSigWord == kHFSSigWord) { | |
1872 | if ((error = VOP_FSYNC(hfsmp->hfs_devvp, cred, waitfor, p))) | |
1873 | allerror = error; | |
1874 | } | |
1875 | #if QUOTA | |
1876 | hfs_qsync(mp); | |
1877 | #endif /* QUOTA */ | |
55e303ae A |
1878 | |
1879 | hfs_hotfilesync(hfsmp, p); | |
9bccf70c A |
1880 | /* |
1881 | * Write back modified superblock. | |
1882 | */ | |
1883 | ||
1884 | if (IsVCBDirty(vcb)) { | |
b4c24cb9 A |
1885 | // XXXdbg - debugging, remove |
1886 | if (hfsmp->jnl) { | |
1887 | //printf("hfs: sync: strange, a journaled volume w/dirty VCB? jnl 0x%x hfsmp 0x%x\n", | |
1888 | // hfsmp->jnl, hfsmp); | |
1889 | } | |
1890 | ||
9bccf70c | 1891 | error = hfs_flushvolumeheader(hfsmp, waitfor, 0); |
b4c24cb9 A |
1892 | if (error) |
1893 | allerror = error; | |
9bccf70c | 1894 | } |
1c79356b | 1895 | |
b4c24cb9 A |
1896 | if (hfsmp->jnl) { |
1897 | journal_flush(hfsmp->jnl); | |
1898 | } | |
1899 | ||
1900 | err_exit: | |
9bccf70c | 1901 | return (allerror); |
1c79356b A |
1902 | } |
1903 | ||
1904 | ||
1905 | /* | |
1906 | * File handle to vnode | |
1907 | * | |
1908 | * Have to be really careful about stale file handles: | |
9bccf70c A |
1909 | * - check that the cnode id is valid |
1910 | * - call hfs_vget() to get the locked cnode | |
1911 | * - check for an unallocated cnode (i_mode == 0) | |
1c79356b A |
1912 | * - check that the given client host has export rights and return |
1913 | * those rights via. exflagsp and credanonp | |
1914 | */ | |
9bccf70c | 1915 | static int |
1c79356b | 1916 | hfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) |
9bccf70c A |
1917 | register struct mount *mp; |
1918 | struct fid *fhp; | |
1919 | struct mbuf *nam; | |
1920 | struct vnode **vpp; | |
1921 | int *exflagsp; | |
1922 | struct ucred **credanonp; | |
1c79356b A |
1923 | { |
1924 | struct hfsfid *hfsfhp; | |
1925 | struct vnode *nvp; | |
1926 | int result; | |
1927 | struct netcred *np; | |
1c79356b A |
1928 | |
1929 | *vpp = NULL; | |
1930 | hfsfhp = (struct hfsfid *)fhp; | |
1931 | ||
1932 | /* | |
1933 | * Get the export permission structure for this <mp, client> tuple. | |
1934 | */ | |
1935 | np = vfs_export_lookup(mp, &VFSTOHFS(mp)->hfs_export, nam); | |
55e303ae | 1936 | if (nam && (np == NULL)) { |
1c79356b A |
1937 | return EACCES; |
1938 | }; | |
1939 | ||
1940 | result = VFS_VGET(mp, &hfsfhp->hfsfid_cnid, &nvp); | |
1941 | if (result) return result; | |
1942 | if (nvp == NULL) return ESTALE; | |
1943 | ||
0b4e3aa0 A |
1944 | /* The createtime can be changed by hfs_setattr or hfs_setattrlist. |
1945 | * For NFS, we are assuming that only if the createtime was moved | |
1946 | * forward would it mean the fileID got reused in that session by | |
1947 | * wrapping. We don't have a volume ID or other unique identifier to | |
1948 | * to use here for a generation ID across reboots, crashes where | |
1949 | * metadata noting lastFileID didn't make it to disk but client has | |
1950 | * it, or volume erasures where fileIDs start over again. Lastly, | |
1951 | * with HFS allowing "wraps" of fileIDs now, this becomes more | |
1952 | * error prone. Future, would be change the "wrap bit" to a unique | |
1953 | * wrap number and use that for generation number. For now do this. | |
1954 | */ | |
9bccf70c | 1955 | if ((hfsfhp->hfsfid_gen < VTOC(nvp)->c_itime)) { |
1c79356b | 1956 | vput(nvp); |
9bccf70c | 1957 | return (ESTALE); |
1c79356b A |
1958 | }; |
1959 | ||
55e303ae A |
1960 | if (VNAME(nvp) == NULL) { |
1961 | struct cnode *cp = VTOC(nvp); | |
1962 | ||
1963 | if (nvp == cp->c_rsrc_vp) { | |
1964 | // the +1/-2 thing is to skip the leading "/" on the rsrc fork spec | |
1965 | // and to not count the trailing null byte at the end of the string. | |
1966 | VNAME(nvp) = add_name(_PATH_RSRCFORKSPEC+1, sizeof(_PATH_RSRCFORKSPEC)-2, 0, 0); | |
1967 | } else { | |
1968 | VNAME(nvp) = add_name(cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen, 0, 0); | |
1969 | } | |
1970 | } | |
1971 | ||
1c79356b | 1972 | *vpp = nvp; |
55e303ae A |
1973 | if (np) { |
1974 | *exflagsp = np->netc_exflags; | |
1975 | *credanonp = &np->netc_anon; | |
1976 | } | |
1c79356b | 1977 | |
9bccf70c | 1978 | return (0); |
1c79356b A |
1979 | } |
1980 | ||
1981 | ||
1982 | /* | |
1983 | * Vnode pointer to File handle | |
1984 | */ | |
1985 | /* ARGSUSED */ | |
9bccf70c A |
1986 | static int |
1987 | hfs_vptofh(vp, fhp) | |
1988 | struct vnode *vp; | |
1989 | struct fid *fhp; | |
1c79356b | 1990 | { |
9bccf70c | 1991 | struct cnode *cp; |
1c79356b | 1992 | struct hfsfid *hfsfhp; |
1c79356b | 1993 | |
9bccf70c A |
1994 | if (ISHFS(VTOVCB(vp))) |
1995 | return (EOPNOTSUPP); /* hfs standard is not exportable */ | |
1c79356b | 1996 | |
9bccf70c A |
1997 | cp = VTOC(vp); |
1998 | hfsfhp = (struct hfsfid *)fhp; | |
1c79356b A |
1999 | hfsfhp->hfsfid_len = sizeof(struct hfsfid); |
2000 | hfsfhp->hfsfid_pad = 0; | |
55e303ae | 2001 | hfsfhp->hfsfid_cnid = cp->c_fileid; |
9bccf70c | 2002 | hfsfhp->hfsfid_gen = cp->c_itime; |
1c79356b | 2003 | |
9bccf70c | 2004 | return (0); |
1c79356b A |
2005 | } |
2006 | ||
2007 | ||
2008 | /* | |
2009 | * Initial HFS filesystems, done only once. | |
2010 | */ | |
9bccf70c | 2011 | static int |
1c79356b | 2012 | hfs_init(vfsp) |
9bccf70c | 2013 | struct vfsconf *vfsp; |
1c79356b | 2014 | { |
9bccf70c | 2015 | static int done = 0; |
1c79356b | 2016 | |
9bccf70c A |
2017 | if (done) |
2018 | return (0); | |
2019 | done = 1; | |
2020 | hfs_chashinit(); | |
2021 | hfs_converterinit(); | |
2022 | #if QUOTA | |
2023 | dqinit(); | |
2024 | #endif /* QUOTA */ | |
1c79356b | 2025 | |
55e303ae A |
2026 | BTReserveSetup(); |
2027 | ||
1c79356b A |
2028 | /* |
2029 | * Allocate Catalog Iterator cache... | |
2030 | */ | |
9bccf70c | 2031 | (void) InitCatalogCache(); |
1c79356b | 2032 | |
9bccf70c | 2033 | return (0); |
1c79356b A |
2034 | } |
2035 | ||
55e303ae A |
2036 | static int |
2037 | hfs_getmountpoint(vp, hfsmpp) | |
2038 | struct vnode *vp; | |
2039 | struct hfsmount **hfsmpp; | |
2040 | { | |
2041 | struct hfsmount * hfsmp; | |
2042 | ||
2043 | if (vp == NULL) | |
2044 | return (EINVAL); | |
2045 | ||
2046 | if ((vp->v_flag & VROOT) == 0) | |
2047 | return (EINVAL); | |
2048 | ||
2049 | if (strcmp(vp->v_mount->mnt_stat.f_fstypename, "hfs") != 0) | |
2050 | return (EINVAL); | |
2051 | ||
2052 | hfsmp = VTOHFS(vp); | |
2053 | ||
2054 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) | |
2055 | return (EINVAL); | |
2056 | ||
2057 | *hfsmpp = hfsmp; | |
2058 | ||
2059 | return (0); | |
2060 | } | |
1c79356b | 2061 | |
b4c24cb9 A |
2062 | // XXXdbg |
2063 | #include <sys/filedesc.h> | |
2064 | ||
2065 | ||
1c79356b | 2066 | /* |
9bccf70c | 2067 | * HFS filesystem related variables. |
1c79356b | 2068 | */ |
9bccf70c A |
2069 | static int |
2070 | hfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) | |
2071 | int *name; | |
2072 | u_int namelen; | |
2073 | void *oldp; | |
2074 | size_t *oldlenp; | |
2075 | void *newp; | |
2076 | size_t newlen; | |
2077 | struct proc *p; | |
1c79356b | 2078 | { |
55e303ae A |
2079 | extern u_int32_t hfs_getencodingbias(void); |
2080 | extern void hfs_setencodingbias(u_int32_t); | |
2081 | ||
2082 | int error; | |
2083 | struct sysctl_req *req; | |
2084 | struct vfsidctl vc; | |
2085 | struct mount *mp; | |
2086 | struct hfsmount *hfsmp; | |
2087 | struct vfsquery vq; | |
9bccf70c A |
2088 | |
2089 | /* all sysctl names at this level are terminal */ | |
1c79356b | 2090 | |
55e303ae A |
2091 | if (name[0] == HFS_ENCODINGBIAS) { |
2092 | u_int32_t bias; | |
2093 | ||
2094 | bias = hfs_getencodingbias(); | |
2095 | error = sysctl_int(oldp, oldlenp, newp, newlen, &bias); | |
2096 | if (error == 0 && newp) | |
2097 | hfs_setencodingbias(bias); | |
2098 | return (error); | |
2099 | ||
2100 | } else if (name[0] == HFS_EXTEND_FS) { | |
2101 | u_int64_t newsize; | |
2102 | ||
2103 | if (newp == NULL) | |
2104 | return (EINVAL); | |
2105 | if ((error = hfs_getmountpoint(p->p_fd->fd_cdir, &hfsmp))) | |
2106 | return (error); | |
2107 | error = sysctl_quad(oldp, oldlenp, newp, newlen, &newsize); | |
2108 | if (error) | |
2109 | return (error); | |
2110 | ||
2111 | error = hfs_extendfs(HFSTOVFS(hfsmp), newsize, p); | |
2112 | return (error); | |
2113 | ||
2114 | } else if (name[0] == HFS_ENCODINGHINT) { | |
2115 | size_t bufsize; | |
2116 | size_t bytes; | |
2117 | u_int32_t hint; | |
2118 | u_int16_t *unicode_name; | |
2119 | char *filename; | |
2120 | ||
2121 | bufsize = MAX(newlen * 3, MAXPATHLEN); | |
2122 | MALLOC(filename, char *, newlen, M_TEMP, M_WAITOK); | |
2123 | MALLOC(unicode_name, u_int16_t *, bufsize, M_TEMP, M_WAITOK); | |
2124 | ||
2125 | error = copyin(newp, (caddr_t)filename, newlen); | |
2126 | if (error == 0) { | |
2127 | error = utf8_decodestr(filename, newlen - 1, unicode_name, | |
2128 | &bytes, bufsize, 0, UTF_DECOMPOSED); | |
2129 | if (error == 0) { | |
2130 | hint = hfs_pickencoding(unicode_name, bytes / 2); | |
2131 | error = sysctl_int(oldp, oldlenp, NULL, NULL, &hint); | |
2132 | } | |
2133 | } | |
2134 | FREE(unicode_name, M_TEMP); | |
2135 | FREE(filename, M_TEMP); | |
2136 | return (error); | |
2137 | ||
2138 | } else if (name[0] == HFS_ENABLE_JOURNALING) { | |
b4c24cb9 A |
2139 | // make the file system journaled... |
2140 | struct vnode *vp = p->p_fd->fd_cdir, *jvp; | |
b4c24cb9 A |
2141 | ExtendedVCB *vcb; |
2142 | int retval; | |
2143 | struct cat_attr jnl_attr, jinfo_attr; | |
2144 | struct cat_fork jnl_fork, jinfo_fork; | |
2145 | void *jnl = NULL; | |
d7e50217 A |
2146 | |
2147 | /* Only root can enable journaling */ | |
55e303ae | 2148 | if (current_proc()->p_ucred->cr_uid != 0) { |
d7e50217 A |
2149 | return (EPERM); |
2150 | } | |
55e303ae | 2151 | |
b4c24cb9 | 2152 | hfsmp = VTOHFS(vp); |
55e303ae | 2153 | if (hfsmp->hfs_flags & HFS_READ_ONLY) { |
b4c24cb9 A |
2154 | return EROFS; |
2155 | } | |
2156 | if (HFSTOVCB(hfsmp)->vcbSigWord == kHFSSigWord) { | |
2157 | printf("hfs: can't make a plain hfs volume journaled.\n"); | |
2158 | return EINVAL; | |
2159 | } | |
2160 | ||
2161 | if (hfsmp->jnl) { | |
2162 | printf("hfs: volume @ mp 0x%x is already journaled!\n", vp->v_mount); | |
2163 | return EAGAIN; | |
2164 | } | |
2165 | ||
2166 | vcb = HFSTOVCB(hfsmp); | |
2167 | if (BTHasContiguousNodes(VTOF(vcb->catalogRefNum)) == 0 || | |
2168 | BTHasContiguousNodes(VTOF(vcb->extentsRefNum)) == 0) { | |
2169 | ||
2170 | printf("hfs: volume has a btree w/non-contiguous nodes. can not enable journaling.\n"); | |
2171 | return EINVAL; | |
2172 | } | |
2173 | ||
2174 | // make sure these both exist! | |
2175 | if ( GetFileInfo(vcb, kRootDirID, ".journal_info_block", &jinfo_attr, &jinfo_fork) == 0 | |
2176 | || GetFileInfo(vcb, kRootDirID, ".journal", &jnl_attr, &jnl_fork) == 0) { | |
2177 | ||
2178 | return EINVAL; | |
2179 | } | |
2180 | ||
2181 | hfs_sync(hfsmp->hfs_mp, MNT_WAIT, FSCRED, p); | |
2182 | bflushq(BQ_META); | |
2183 | ||
2184 | printf("hfs: Initializing the journal (joffset 0x%llx sz 0x%llx)...\n", | |
2185 | (off_t)name[2], (off_t)name[3]); | |
2186 | ||
2187 | jvp = hfsmp->hfs_devvp; | |
2188 | jnl = journal_create(jvp, | |
2189 | (off_t)name[2] * (off_t)HFSTOVCB(hfsmp)->blockSize | |
2190 | + HFSTOVCB(hfsmp)->hfsPlusIOPosOffset, | |
55e303ae | 2191 | (off_t)((unsigned)name[3]), |
b4c24cb9 A |
2192 | hfsmp->hfs_devvp, |
2193 | hfsmp->hfs_phys_block_size, | |
2194 | 0, | |
2195 | 0, | |
2196 | hfs_sync_metadata, hfsmp->hfs_mp); | |
2197 | ||
2198 | if (jnl == NULL) { | |
2199 | printf("hfs: FAILED to create the journal!\n"); | |
2200 | if (jvp && jvp != hfsmp->hfs_devvp) { | |
55e303ae | 2201 | VOP_CLOSE(jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, FSCRED, p); |
b4c24cb9 A |
2202 | } |
2203 | jvp = NULL; | |
2204 | ||
2205 | return EINVAL; | |
2206 | } | |
2207 | ||
2208 | hfs_global_exclusive_lock_acquire(hfsmp); | |
2209 | ||
2210 | HFSTOVCB(hfsmp)->vcbJinfoBlock = name[1]; | |
2211 | HFSTOVCB(hfsmp)->vcbAtrb |= kHFSVolumeJournaledMask; | |
2212 | hfsmp->jvp = jvp; | |
2213 | hfsmp->jnl = jnl; | |
2214 | ||
2215 | // save this off for the hack-y check in hfs_remove() | |
2216 | hfsmp->jnl_start = (u_int32_t)name[2]; | |
55e303ae | 2217 | hfsmp->jnl_size = (off_t)((unsigned)name[3]); |
b4c24cb9 A |
2218 | hfsmp->hfs_jnlinfoblkid = jinfo_attr.ca_fileid; |
2219 | hfsmp->hfs_jnlfileid = jnl_attr.ca_fileid; | |
2220 | ||
2221 | hfsmp->hfs_mp->mnt_flag |= MNT_JOURNALED; | |
2222 | ||
2223 | hfs_global_exclusive_lock_release(hfsmp); | |
2224 | hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1); | |
2225 | ||
2226 | return 0; | |
55e303ae | 2227 | } else if (name[0] == HFS_DISABLE_JOURNALING) { |
b4c24cb9 A |
2228 | // clear the journaling bit |
2229 | struct vnode *vp = p->p_fd->fd_cdir; | |
b4c24cb9 A |
2230 | void *jnl; |
2231 | int retval; | |
2232 | ||
d7e50217 | 2233 | /* Only root can disable journaling */ |
55e303ae | 2234 | if (current_proc()->p_ucred->cr_uid != 0) { |
d7e50217 A |
2235 | return (EPERM); |
2236 | } | |
55e303ae | 2237 | |
b4c24cb9 | 2238 | hfsmp = VTOHFS(vp); |
b4c24cb9 A |
2239 | |
2240 | printf("hfs: disabling journaling for mount @ 0x%x\n", vp->v_mount); | |
2241 | ||
2242 | jnl = hfsmp->jnl; | |
2243 | ||
2244 | hfs_global_exclusive_lock_acquire(hfsmp); | |
2245 | ||
2246 | // Lights out for you buddy! | |
2247 | hfsmp->jnl = NULL; | |
2248 | journal_close(jnl); | |
2249 | ||
2250 | if (hfsmp->jvp && hfsmp->jvp != hfsmp->hfs_devvp) { | |
55e303ae | 2251 | VOP_CLOSE(hfsmp->jvp, hfsmp->hfs_flags & HFS_READ_ONLY ? FREAD : FREAD|FWRITE, FSCRED, p); |
b4c24cb9 A |
2252 | } |
2253 | hfsmp->jnl = NULL; | |
2254 | hfsmp->jvp = NULL; | |
2255 | hfsmp->hfs_mp->mnt_flag &= ~MNT_JOURNALED; | |
2256 | hfsmp->jnl_start = 0; | |
2257 | hfsmp->hfs_jnlinfoblkid = 0; | |
2258 | hfsmp->hfs_jnlfileid = 0; | |
2259 | ||
2260 | HFSTOVCB(hfsmp)->vcbAtrb &= ~kHFSVolumeJournaledMask; | |
2261 | ||
2262 | hfs_global_exclusive_lock_release(hfsmp); | |
2263 | hfs_flushvolumeheader(hfsmp, MNT_WAIT, 1); | |
2264 | ||
2265 | return 0; | |
55e303ae A |
2266 | } else if (name[0] == HFS_GET_JOURNAL_INFO) { |
2267 | struct vnode *vp = p->p_fd->fd_cdir; | |
2268 | off_t jnl_start, jnl_size; | |
2269 | ||
2270 | hfsmp = VTOHFS(vp); | |
2271 | if (hfsmp->jnl == NULL) { | |
2272 | jnl_start = 0; | |
2273 | jnl_size = 0; | |
2274 | } else { | |
2275 | jnl_start = (off_t)(hfsmp->jnl_start * HFSTOVCB(hfsmp)->blockSize) + (off_t)HFSTOVCB(hfsmp)->hfsPlusIOPosOffset; | |
2276 | jnl_size = (off_t)hfsmp->jnl_size; | |
2277 | } | |
2278 | ||
2279 | if ((error = copyout((caddr_t)&jnl_start, (void *)name[1], sizeof(off_t))) != 0) { | |
2280 | return error; | |
2281 | } | |
2282 | if ((error = copyout((caddr_t)&jnl_size, (void *)name[2], sizeof(off_t))) != 0) { | |
2283 | return error; | |
2284 | } | |
2285 | ||
2286 | return 0; | |
2287 | } else if (name[0] == HFS_SET_PKG_EXTENSIONS) { | |
2288 | ||
2289 | return set_package_extensions_table((void *)name[1], name[2], name[3]); | |
2290 | ||
2291 | } else if (name[0] == VFS_CTL_QUERY) { | |
2292 | req = oldp; /* we're new style vfs sysctl. */ | |
2293 | ||
2294 | error = SYSCTL_IN(req, &vc, sizeof(vc)); | |
2295 | if (error) return (error); | |
2296 | ||
2297 | mp = vfs_getvfs(&vc.vc_fsid); | |
2298 | if (mp == NULL) return (ENOENT); | |
2299 | ||
2300 | hfsmp = VFSTOHFS(mp); | |
2301 | bzero(&vq, sizeof(vq)); | |
2302 | vq.vq_flags = hfsmp->hfs_notification_conditions; | |
2303 | return SYSCTL_OUT(req, &vq, sizeof(vq));; | |
2304 | }; | |
9bccf70c A |
2305 | |
2306 | return (EOPNOTSUPP); | |
1c79356b A |
2307 | } |
2308 | ||
2309 | ||
2310 | /* This will return a vnode of either a directory or a data vnode based on an object id. If | |
2311 | * it is a file id, its data fork will be returned. | |
2312 | */ | |
9bccf70c A |
2313 | static int |
2314 | hfs_vget(mp, ino, vpp) | |
2315 | struct mount *mp; | |
2316 | void *ino; | |
2317 | struct vnode **vpp; | |
2318 | { | |
2319 | cnid_t cnid = *(cnid_t *)ino; | |
2320 | ||
2321 | /* Check for cnids that should't be exported. */ | |
2322 | if ((cnid < kHFSFirstUserCatalogNodeID) | |
2323 | && (cnid != kHFSRootFolderID && cnid != kHFSRootParentID)) | |
2324 | return (ENOENT); | |
2325 | /* Don't export HFS Private Data dir. */ | |
2326 | if (cnid == VFSTOHFS(mp)->hfs_privdir_desc.cd_cnid) | |
2327 | return (ENOENT); | |
2328 | ||
2329 | return (hfs_getcnode(VFSTOHFS(mp), cnid, NULL, 0, NULL, NULL, vpp)); | |
2330 | } | |
2331 | ||
55e303ae A |
2332 | /* |
2333 | * Check to see if a given vnode is only referenced for events: | |
2334 | * [ entered with vp->v_interlock locked ] | |
2335 | */ | |
2336 | static int | |
2337 | hfs_evtonly(struct vnode *vp) | |
2338 | { | |
2339 | int ubc_refcount; | |
2340 | ||
2341 | ubc_refcount = UBCINFOEXISTS(vp) ? 1 : 0; | |
2342 | return (vp->v_usecount == (ubc_refcount + EVTONLYREFS(vp))); | |
2343 | } | |
2344 | ||
2345 | /* | |
2346 | * Check to see if all non-system vnodes for a given mountpoint are events-only | |
2347 | */ | |
2348 | static int | |
2349 | hfs_flush_evtonly(struct mount *mp, int flags, int dispose, struct proc *p) | |
2350 | { | |
2351 | struct vnode *vp, *nvp; | |
2352 | int busy = 0; | |
2353 | ||
2354 | simple_lock(&mntvnode_slock); | |
2355 | loop: | |
2356 | for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) { | |
2357 | if (vp->v_mount != mp) goto loop; | |
2358 | nvp = vp->v_mntvnodes.le_next; | |
2359 | ||
2360 | simple_lock(&vp->v_interlock); | |
2361 | /* | |
2362 | * Skip over a vnodes marked VSYSTEM or VNOFLUSH. | |
2363 | */ | |
2364 | if ((flags & SKIPSYSTEM) && ((vp->v_flag & VSYSTEM) || (vp->v_flag & VNOFLUSH))) { | |
2365 | simple_unlock(&vp->v_interlock); | |
2366 | continue; | |
2367 | }; | |
2368 | /* | |
2369 | * Skip over a vnodes marked VSWAP. | |
2370 | */ | |
2371 | if ((flags & SKIPSWAP) && (vp->v_flag & VSWAP)) { | |
2372 | simple_unlock(&vp->v_interlock); | |
2373 | continue; | |
2374 | } | |
2375 | if (hfs_evtonly(vp)) { | |
2376 | if (dispose) { | |
2377 | /* "dispose" implies "forcibly", a la "FORCECLOSE": */ | |
2378 | simple_unlock(&mntvnode_slock); | |
2379 | vgonel(vp, p); | |
2380 | simple_lock(&mntvnode_slock); | |
2381 | } else { | |
2382 | simple_unlock(&vp->v_interlock); | |
2383 | }; | |
2384 | continue; | |
2385 | }; | |
2386 | ||
2387 | simple_unlock(&vp->v_interlock); | |
2388 | ++busy; | |
2389 | /* If asked to dispose, keep trying. If only checking, the answer is now known. */ | |
2390 | if (dispose) { | |
2391 | continue; | |
2392 | } else { | |
2393 | break; | |
2394 | }; | |
2395 | } | |
2396 | simple_unlock(&mntvnode_slock); | |
2397 | ||
2398 | return (busy == 0); | |
2399 | } | |
2400 | ||
9bccf70c A |
2401 | /* |
2402 | * Flush out all the files in a filesystem. | |
2403 | */ | |
55e303ae | 2404 | static int |
9bccf70c | 2405 | hfs_flushfiles(struct mount *mp, int flags, struct proc *p) |
1c79356b | 2406 | { |
55e303ae A |
2407 | struct hfsmount *hfsmp; |
2408 | struct vnode *skipvp = NULLVP; | |
2409 | struct vnode *rsrcvp; | |
2410 | int quotafilecnt; | |
9bccf70c A |
2411 | int i; |
2412 | int error; | |
1c79356b | 2413 | |
9bccf70c | 2414 | hfsmp = VFSTOHFS(mp); |
1c79356b | 2415 | |
55e303ae A |
2416 | #if QUOTA |
2417 | /* | |
2418 | * The open quota files have an indirect reference on | |
2419 | * the root directory vnode. We must account for this | |
2420 | * extra reference when doing the intial vflush. | |
2421 | */ | |
2422 | quotafilecnt = 0; | |
2423 | if (mp->mnt_flag & MNT_QUOTA) { | |
2424 | ||
2425 | /* Find out how many quota files we have open. */ | |
2426 | for (i = 0; i < MAXQUOTAS; i++) { | |
2427 | if (hfsmp->hfs_qfiles[i].qf_vp != NULLVP) | |
2428 | ++quotafilecnt; | |
2429 | } | |
2430 | ||
2431 | /* Obtain the root vnode so we can skip over it. */ | |
2432 | if (hfs_chashget(hfsmp->hfs_raw_dev, kRootDirID, 0, | |
2433 | &skipvp, &rsrcvp) == NULL) { | |
2434 | skipvp = NULLVP; | |
2435 | } | |
2436 | } | |
2437 | #endif /* QUOTA */ | |
2438 | ||
2439 | error = vflush(mp, skipvp, SKIPSYSTEM | SKIPSWAP | flags); | |
2440 | /* | |
2441 | * If the vflush() call failed solely because there are | |
2442 | * some event-only vnodes in the list, then forcibly get | |
2443 | * rid of those vnodes before the final vflush() pass. | |
2444 | */ | |
2445 | if ((error == EBUSY) && hfs_flush_evtonly(mp, SKIPSYSTEM | SKIPSWAP, 0, p)) { | |
2446 | (void) hfs_flush_evtonly(mp, SKIPSYSTEM | SKIPSWAP, 1, p); | |
2447 | }; | |
2448 | error = vflush(mp, skipvp, SKIPSYSTEM | flags); | |
2449 | ||
2450 | #if QUOTA | |
9bccf70c | 2451 | if (mp->mnt_flag & MNT_QUOTA) { |
55e303ae A |
2452 | if (skipvp) { |
2453 | /* | |
2454 | * See if there are additional references on the | |
2455 | * root vp besides the ones obtained from the open | |
2456 | * quota files and the hfs_chashget call above. | |
2457 | */ | |
2458 | if ((error == 0) && | |
2459 | (skipvp->v_usecount > (1 + quotafilecnt))) { | |
2460 | error = EBUSY; /* root directory is still open */ | |
2461 | } | |
2462 | vput(skipvp); | |
2463 | } | |
2464 | if (error && (flags & FORCECLOSE) == 0) | |
9bccf70c | 2465 | return (error); |
55e303ae | 2466 | |
9bccf70c A |
2467 | for (i = 0; i < MAXQUOTAS; i++) { |
2468 | if (hfsmp->hfs_qfiles[i].qf_vp == NULLVP) | |
2469 | continue; | |
2470 | hfs_quotaoff(p, mp, i); | |
2471 | } | |
55e303ae | 2472 | error = vflush(mp, NULLVP, SKIPSYSTEM | flags); |
1c79356b | 2473 | } |
9bccf70c | 2474 | #endif /* QUOTA */ |
1c79356b | 2475 | |
9bccf70c A |
2476 | return (error); |
2477 | } | |
1c79356b | 2478 | |
9bccf70c A |
2479 | /* |
2480 | * Update volume encoding bitmap (HFS Plus only) | |
2481 | */ | |
2482 | __private_extern__ | |
2483 | void | |
2484 | hfs_setencodingbits(struct hfsmount *hfsmp, u_int32_t encoding) | |
2485 | { | |
2486 | #define kIndexMacUkrainian 48 /* MacUkrainian encoding is 152 */ | |
2487 | #define kIndexMacFarsi 49 /* MacFarsi encoding is 140 */ | |
2488 | ||
2489 | UInt32 index; | |
2490 | ||
2491 | switch (encoding) { | |
2492 | case kTextEncodingMacUkrainian: | |
2493 | index = kIndexMacUkrainian; | |
2494 | break; | |
2495 | case kTextEncodingMacFarsi: | |
2496 | index = kIndexMacFarsi; | |
2497 | break; | |
2498 | default: | |
2499 | index = encoding; | |
2500 | break; | |
2501 | } | |
1c79356b | 2502 | |
55e303ae A |
2503 | if (index < 64) { |
2504 | HFSTOVCB(hfsmp)->encodingsBitmap |= (u_int64_t)(1ULL << index); | |
9bccf70c A |
2505 | HFSTOVCB(hfsmp)->vcbFlags |= 0xFF00; |
2506 | } | |
1c79356b A |
2507 | } |
2508 | ||
2509 | /* | |
9bccf70c | 2510 | * Update volume stats |
1c79356b | 2511 | */ |
9bccf70c | 2512 | __private_extern__ |
1c79356b | 2513 | int |
9bccf70c | 2514 | hfs_volupdate(struct hfsmount *hfsmp, enum volop op, int inroot) |
1c79356b | 2515 | { |
9bccf70c | 2516 | ExtendedVCB *vcb; |
1c79356b | 2517 | |
9bccf70c A |
2518 | vcb = HFSTOVCB(hfsmp); |
2519 | vcb->vcbFlags |= 0xFF00; | |
2520 | vcb->vcbLsMod = time.tv_sec; | |
2521 | ||
2522 | switch (op) { | |
2523 | case VOL_UPDATE: | |
2524 | break; | |
2525 | case VOL_MKDIR: | |
2526 | if (vcb->vcbDirCnt != 0xFFFFFFFF) | |
2527 | ++vcb->vcbDirCnt; | |
2528 | if (inroot && vcb->vcbNmRtDirs != 0xFFFF) | |
2529 | ++vcb->vcbNmRtDirs; | |
2530 | break; | |
2531 | case VOL_RMDIR: | |
2532 | if (vcb->vcbDirCnt != 0) | |
2533 | --vcb->vcbDirCnt; | |
2534 | if (inroot && vcb->vcbNmRtDirs != 0xFFFF) | |
2535 | --vcb->vcbNmRtDirs; | |
2536 | break; | |
2537 | case VOL_MKFILE: | |
2538 | if (vcb->vcbFilCnt != 0xFFFFFFFF) | |
2539 | ++vcb->vcbFilCnt; | |
2540 | if (inroot && vcb->vcbNmFls != 0xFFFF) | |
2541 | ++vcb->vcbNmFls; | |
2542 | break; | |
2543 | case VOL_RMFILE: | |
2544 | if (vcb->vcbFilCnt != 0) | |
2545 | --vcb->vcbFilCnt; | |
2546 | if (inroot && vcb->vcbNmFls != 0xFFFF) | |
2547 | --vcb->vcbNmFls; | |
2548 | break; | |
2549 | } | |
b4c24cb9 A |
2550 | |
2551 | if (hfsmp->jnl) { | |
2552 | hfs_flushvolumeheader(hfsmp, 0, 0); | |
2553 | } | |
2554 | ||
9bccf70c | 2555 | return (0); |
1c79356b A |
2556 | } |
2557 | ||
9bccf70c A |
2558 | |
2559 | static int | |
2560 | hfs_flushMDB(struct hfsmount *hfsmp, int waitfor, int altflush) | |
1c79356b | 2561 | { |
9bccf70c A |
2562 | ExtendedVCB *vcb = HFSTOVCB(hfsmp); |
2563 | struct filefork *fp; | |
1c79356b | 2564 | HFSMasterDirectoryBlock *mdb; |
9bccf70c A |
2565 | struct buf *bp = NULL; |
2566 | int retval; | |
2567 | int sectorsize; | |
2568 | ByteCount namelen; | |
1c79356b | 2569 | |
9bccf70c | 2570 | sectorsize = hfsmp->hfs_phys_block_size; |
9bccf70c | 2571 | retval = bread(hfsmp->hfs_devvp, HFS_PRI_SECTOR(sectorsize), sectorsize, NOCRED, &bp); |
1c79356b | 2572 | if (retval) { |
9bccf70c A |
2573 | if (bp) |
2574 | brelse(bp); | |
1c79356b A |
2575 | return retval; |
2576 | } | |
2577 | ||
9bccf70c A |
2578 | DBG_ASSERT(bp != NULL); |
2579 | DBG_ASSERT(bp->b_data != NULL); | |
2580 | DBG_ASSERT(bp->b_bcount == size); | |
1c79356b | 2581 | |
b4c24cb9 A |
2582 | if (hfsmp->jnl) { |
2583 | panic("hfs: standard hfs volumes should not be journaled!\n"); | |
2584 | } | |
2585 | ||
9bccf70c | 2586 | mdb = (HFSMasterDirectoryBlock *)(bp->b_data + HFS_PRI_OFFSET(sectorsize)); |
1c79356b | 2587 | |
9bccf70c A |
2588 | mdb->drCrDate = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbCrDate))); |
2589 | mdb->drLsMod = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbLsMod))); | |
2590 | mdb->drAtrb = SWAP_BE16 (vcb->vcbAtrb); | |
1c79356b A |
2591 | mdb->drNmFls = SWAP_BE16 (vcb->vcbNmFls); |
2592 | mdb->drAllocPtr = SWAP_BE16 (vcb->nextAllocation); | |
2593 | mdb->drClpSiz = SWAP_BE32 (vcb->vcbClpSiz); | |
2594 | mdb->drNxtCNID = SWAP_BE32 (vcb->vcbNxtCNID); | |
2595 | mdb->drFreeBks = SWAP_BE16 (vcb->freeBlocks); | |
2596 | ||
2597 | namelen = strlen(vcb->vcbVN); | |
2598 | retval = utf8_to_hfs(vcb, namelen, vcb->vcbVN, mdb->drVN); | |
2599 | /* Retry with MacRoman in case that's how it was exported. */ | |
2600 | if (retval) | |
2601 | retval = utf8_to_mac_roman(namelen, vcb->vcbVN, mdb->drVN); | |
2602 | ||
9bccf70c | 2603 | mdb->drVolBkUp = SWAP_BE32 (UTCToLocal(to_hfs_time(vcb->vcbVolBkUp))); |
1c79356b A |
2604 | mdb->drWrCnt = SWAP_BE32 (vcb->vcbWrCnt); |
2605 | mdb->drNmRtDirs = SWAP_BE16 (vcb->vcbNmRtDirs); | |
2606 | mdb->drFilCnt = SWAP_BE32 (vcb->vcbFilCnt); | |
2607 | mdb->drDirCnt = SWAP_BE32 (vcb->vcbDirCnt); | |
2608 | ||
2609 | bcopy(vcb->vcbFndrInfo, mdb->drFndrInfo, sizeof(mdb->drFndrInfo)); | |
2610 | ||
9bccf70c A |
2611 | fp = VTOF(vcb->extentsRefNum); |
2612 | mdb->drXTExtRec[0].startBlock = SWAP_BE16 (fp->ff_extents[0].startBlock); | |
2613 | mdb->drXTExtRec[0].blockCount = SWAP_BE16 (fp->ff_extents[0].blockCount); | |
2614 | mdb->drXTExtRec[1].startBlock = SWAP_BE16 (fp->ff_extents[1].startBlock); | |
2615 | mdb->drXTExtRec[1].blockCount = SWAP_BE16 (fp->ff_extents[1].blockCount); | |
2616 | mdb->drXTExtRec[2].startBlock = SWAP_BE16 (fp->ff_extents[2].startBlock); | |
2617 | mdb->drXTExtRec[2].blockCount = SWAP_BE16 (fp->ff_extents[2].blockCount); | |
2618 | mdb->drXTFlSize = SWAP_BE32 (fp->ff_blocks * vcb->blockSize); | |
2619 | mdb->drXTClpSiz = SWAP_BE32 (fp->ff_clumpsize); | |
1c79356b | 2620 | |
9bccf70c A |
2621 | fp = VTOF(vcb->catalogRefNum); |
2622 | mdb->drCTExtRec[0].startBlock = SWAP_BE16 (fp->ff_extents[0].startBlock); | |
2623 | mdb->drCTExtRec[0].blockCount = SWAP_BE16 (fp->ff_extents[0].blockCount); | |
2624 | mdb->drCTExtRec[1].startBlock = SWAP_BE16 (fp->ff_extents[1].startBlock); | |
2625 | mdb->drCTExtRec[1].blockCount = SWAP_BE16 (fp->ff_extents[1].blockCount); | |
2626 | mdb->drCTExtRec[2].startBlock = SWAP_BE16 (fp->ff_extents[2].startBlock); | |
2627 | mdb->drCTExtRec[2].blockCount = SWAP_BE16 (fp->ff_extents[2].blockCount); | |
2628 | mdb->drCTFlSize = SWAP_BE32 (fp->ff_blocks * vcb->blockSize); | |
2629 | mdb->drCTClpSiz = SWAP_BE32 (fp->ff_clumpsize); | |
2630 | ||
2631 | /* If requested, flush out the alternate MDB */ | |
2632 | if (altflush) { | |
2633 | struct buf *alt_bp = NULL; | |
2634 | u_long altIDSector; | |
2635 | ||
2636 | altIDSector = HFS_ALT_SECTOR(sectorsize, hfsmp->hfs_phys_block_count); | |
2637 | ||
2638 | if (meta_bread(hfsmp->hfs_devvp, altIDSector, sectorsize, NOCRED, &alt_bp) == 0) { | |
2639 | bcopy(mdb, alt_bp->b_data + HFS_ALT_OFFSET(sectorsize), kMDBSize); | |
b4c24cb9 | 2640 | |
9bccf70c A |
2641 | (void) VOP_BWRITE(alt_bp); |
2642 | } else if (alt_bp) | |
2643 | brelse(alt_bp); | |
2644 | } | |
1c79356b | 2645 | |
9bccf70c | 2646 | if (waitfor != MNT_WAIT) |
1c79356b | 2647 | bawrite(bp); |
b4c24cb9 | 2648 | else |
1c79356b A |
2649 | retval = VOP_BWRITE(bp); |
2650 | ||
2651 | MarkVCBClean( vcb ); | |
2652 | ||
2653 | return (retval); | |
2654 | } | |
2655 | ||
55e303ae A |
2656 | /* |
2657 | * Flush any dirty in-memory mount data to the on-disk | |
2658 | * volume header. | |
2659 | * | |
2660 | * Note: the on-disk volume signature is intentionally | |
2661 | * not flushed since the on-disk "H+" and "HX" signatures | |
2662 | * are always stored in-memory as "H+". | |
2663 | */ | |
9bccf70c A |
2664 | __private_extern__ |
2665 | int | |
2666 | hfs_flushvolumeheader(struct hfsmount *hfsmp, int waitfor, int altflush) | |
1c79356b | 2667 | { |
9bccf70c A |
2668 | ExtendedVCB *vcb = HFSTOVCB(hfsmp); |
2669 | struct filefork *fp; | |
2670 | HFSPlusVolumeHeader *volumeHeader; | |
2671 | int retval; | |
2672 | struct buf *bp; | |
2673 | int i; | |
d52fe63f A |
2674 | int sectorsize; |
2675 | int priIDSector; | |
9bccf70c | 2676 | int critical = 0; |
55e303ae A |
2677 | u_int16_t signature; |
2678 | u_int16_t version; | |
1c79356b | 2679 | |
55e303ae A |
2680 | if (hfsmp->hfs_flags & HFS_READ_ONLY) { |
2681 | return(0); | |
2682 | } | |
9bccf70c A |
2683 | if (vcb->vcbSigWord == kHFSSigWord) |
2684 | return hfs_flushMDB(hfsmp, waitfor, altflush); | |
1c79356b | 2685 | |
9bccf70c A |
2686 | if (altflush) |
2687 | critical = 1; | |
d52fe63f A |
2688 | sectorsize = hfsmp->hfs_phys_block_size; |
2689 | priIDSector = (vcb->hfsPlusIOPosOffset / sectorsize) + | |
2690 | HFS_PRI_SECTOR(sectorsize); | |
2691 | ||
b4c24cb9 A |
2692 | // XXXdbg |
2693 | hfs_global_shared_lock_acquire(hfsmp); | |
2694 | if (hfsmp->jnl) { | |
2695 | if (journal_start_transaction(hfsmp->jnl) != 0) { | |
2696 | hfs_global_shared_lock_release(hfsmp); | |
2697 | return EINVAL; | |
2698 | } | |
2699 | } | |
2700 | ||
d52fe63f | 2701 | retval = meta_bread(hfsmp->hfs_devvp, priIDSector, sectorsize, NOCRED, &bp); |
1c79356b | 2702 | if (retval) { |
9bccf70c A |
2703 | if (bp) |
2704 | brelse(bp); | |
b4c24cb9 A |
2705 | |
2706 | if (hfsmp->jnl) { | |
2707 | journal_end_transaction(hfsmp->jnl); | |
2708 | } | |
2709 | hfs_global_shared_lock_release(hfsmp); | |
2710 | ||
55e303ae | 2711 | printf("HFS: err %d reading VH blk (%s)\n", retval, vcb->vcbVN); |
9bccf70c | 2712 | return (retval); |
1c79356b A |
2713 | } |
2714 | ||
b4c24cb9 A |
2715 | if (hfsmp->jnl) { |
2716 | journal_modify_block_start(hfsmp->jnl, bp); | |
2717 | } | |
2718 | ||
d52fe63f | 2719 | volumeHeader = (HFSPlusVolumeHeader *)((char *)bp->b_data + HFS_PRI_OFFSET(sectorsize)); |
1c79356b | 2720 | |
55e303ae A |
2721 | /* |
2722 | * Sanity check what we just read. | |
2723 | */ | |
2724 | signature = SWAP_BE16 (volumeHeader->signature); | |
2725 | version = SWAP_BE16 (volumeHeader->version); | |
2726 | if ((signature != kHFSPlusSigWord && signature != kHFSXSigWord) || | |
2727 | (version < kHFSPlusVersion) || (version > 100) || | |
2728 | (SWAP_BE32 (volumeHeader->blockSize) != vcb->blockSize)) { | |
2729 | #if 1 | |
2730 | panic("HFS: corrupt VH on %s, sig 0x%04x, ver %d, blksize %d", | |
2731 | vcb->vcbVN, signature, version, | |
2732 | SWAP_BE32 (volumeHeader->blockSize)); | |
2733 | #endif | |
2734 | printf("HFS: corrupt VH blk (%s)\n", vcb->vcbVN); | |
2735 | brelse(bp); | |
2736 | return (EIO); | |
2737 | } | |
2738 | ||
1c79356b A |
2739 | /* |
2740 | * For embedded HFS+ volumes, update create date if it changed | |
2741 | * (ie from a setattrlist call) | |
2742 | */ | |
9bccf70c A |
2743 | if ((vcb->hfsPlusIOPosOffset != 0) && |
2744 | (SWAP_BE32 (volumeHeader->createDate) != vcb->localCreateDate)) { | |
2745 | struct buf *bp2; | |
1c79356b A |
2746 | HFSMasterDirectoryBlock *mdb; |
2747 | ||
9bccf70c A |
2748 | retval = meta_bread(hfsmp->hfs_devvp, HFS_PRI_SECTOR(sectorsize), |
2749 | sectorsize, NOCRED, &bp2); | |
2750 | if (retval) { | |
2751 | if (bp2) | |
2752 | brelse(bp2); | |
2753 | retval = 0; | |
1c79356b | 2754 | } else { |
9bccf70c A |
2755 | mdb = (HFSMasterDirectoryBlock *)(bp2->b_data + |
2756 | HFS_PRI_OFFSET(sectorsize)); | |
1c79356b A |
2757 | |
2758 | if ( SWAP_BE32 (mdb->drCrDate) != vcb->localCreateDate ) | |
2759 | { | |
b4c24cb9 A |
2760 | // XXXdbg |
2761 | if (hfsmp->jnl) { | |
2762 | journal_modify_block_start(hfsmp->jnl, bp2); | |
2763 | } | |
2764 | ||
1c79356b A |
2765 | mdb->drCrDate = SWAP_BE32 (vcb->localCreateDate); /* pick up the new create date */ |
2766 | ||
b4c24cb9 A |
2767 | // XXXdbg |
2768 | if (hfsmp->jnl) { | |
2769 | journal_modify_block_end(hfsmp->jnl, bp2); | |
2770 | } else { | |
2771 | (void) VOP_BWRITE(bp2); /* write out the changes */ | |
2772 | } | |
1c79356b A |
2773 | } |
2774 | else | |
2775 | { | |
2776 | brelse(bp2); /* just release it */ | |
2777 | } | |
2778 | } | |
9bccf70c | 2779 | } |
1c79356b | 2780 | |
1c79356b | 2781 | /* Note: only update the lower 16 bits worth of attributes */ |
9bccf70c | 2782 | volumeHeader->attributes = SWAP_BE32 ((SWAP_BE32 (volumeHeader->attributes) & 0xFFFF0000) + (UInt16) vcb->vcbAtrb); |
b4c24cb9 A |
2783 | volumeHeader->journalInfoBlock = SWAP_BE32(vcb->vcbJinfoBlock); |
2784 | if (hfsmp->jnl) { | |
2785 | volumeHeader->lastMountedVersion = SWAP_BE32 (kHFSJMountVersion); | |
2786 | } else { | |
2787 | volumeHeader->lastMountedVersion = SWAP_BE32 (kHFSPlusMountVersion); | |
2788 | } | |
9bccf70c A |
2789 | volumeHeader->createDate = SWAP_BE32 (vcb->localCreateDate); /* volume create date is in local time */ |
2790 | volumeHeader->modifyDate = SWAP_BE32 (to_hfs_time(vcb->vcbLsMod)); | |
2791 | volumeHeader->backupDate = SWAP_BE32 (to_hfs_time(vcb->vcbVolBkUp)); | |
2792 | volumeHeader->fileCount = SWAP_BE32 (vcb->vcbFilCnt); | |
2793 | volumeHeader->folderCount = SWAP_BE32 (vcb->vcbDirCnt); | |
2794 | volumeHeader->freeBlocks = SWAP_BE32 (vcb->freeBlocks); | |
2795 | volumeHeader->nextAllocation = SWAP_BE32 (vcb->nextAllocation); | |
2796 | volumeHeader->rsrcClumpSize = SWAP_BE32 (vcb->vcbClpSiz); | |
2797 | volumeHeader->dataClumpSize = SWAP_BE32 (vcb->vcbClpSiz); | |
2798 | volumeHeader->nextCatalogID = SWAP_BE32 (vcb->vcbNxtCNID); | |
2799 | volumeHeader->writeCount = SWAP_BE32 (vcb->vcbWrCnt); | |
2800 | volumeHeader->encodingsBitmap = SWAP_BE64 (vcb->encodingsBitmap); | |
2801 | ||
2802 | if (bcmp(vcb->vcbFndrInfo, volumeHeader->finderInfo, sizeof(volumeHeader->finderInfo)) != 0) | |
2803 | critical = 1; | |
2804 | bcopy(vcb->vcbFndrInfo, volumeHeader->finderInfo, sizeof(volumeHeader->finderInfo)); | |
2805 | ||
2806 | /* Sync Extents over-flow file meta data */ | |
2807 | fp = VTOF(vcb->extentsRefNum); | |
2808 | for (i = 0; i < kHFSPlusExtentDensity; i++) { | |
2809 | volumeHeader->extentsFile.extents[i].startBlock = | |
2810 | SWAP_BE32 (fp->ff_extents[i].startBlock); | |
2811 | volumeHeader->extentsFile.extents[i].blockCount = | |
2812 | SWAP_BE32 (fp->ff_extents[i].blockCount); | |
2813 | } | |
2814 | FTOC(fp)->c_flag &= ~C_MODIFIED; | |
2815 | volumeHeader->extentsFile.logicalSize = SWAP_BE64 (fp->ff_size); | |
2816 | volumeHeader->extentsFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); | |
2817 | volumeHeader->extentsFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); | |
2818 | ||
2819 | /* Sync Catalog file meta data */ | |
2820 | fp = VTOF(vcb->catalogRefNum); | |
2821 | for (i = 0; i < kHFSPlusExtentDensity; i++) { | |
2822 | volumeHeader->catalogFile.extents[i].startBlock = | |
2823 | SWAP_BE32 (fp->ff_extents[i].startBlock); | |
2824 | volumeHeader->catalogFile.extents[i].blockCount = | |
2825 | SWAP_BE32 (fp->ff_extents[i].blockCount); | |
2826 | } | |
2827 | FTOC(fp)->c_flag &= ~C_MODIFIED; | |
2828 | volumeHeader->catalogFile.logicalSize = SWAP_BE64 (fp->ff_size); | |
2829 | volumeHeader->catalogFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); | |
2830 | volumeHeader->catalogFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); | |
2831 | ||
2832 | /* Sync Allocation file meta data */ | |
2833 | fp = VTOF(vcb->allocationsRefNum); | |
2834 | for (i = 0; i < kHFSPlusExtentDensity; i++) { | |
2835 | volumeHeader->allocationFile.extents[i].startBlock = | |
2836 | SWAP_BE32 (fp->ff_extents[i].startBlock); | |
2837 | volumeHeader->allocationFile.extents[i].blockCount = | |
2838 | SWAP_BE32 (fp->ff_extents[i].blockCount); | |
2839 | } | |
2840 | FTOC(fp)->c_flag &= ~C_MODIFIED; | |
2841 | volumeHeader->allocationFile.logicalSize = SWAP_BE64 (fp->ff_size); | |
2842 | volumeHeader->allocationFile.totalBlocks = SWAP_BE32 (fp->ff_blocks); | |
2843 | volumeHeader->allocationFile.clumpSize = SWAP_BE32 (fp->ff_clumpsize); | |
2844 | ||
2845 | /* If requested, flush out the alternate volume header */ | |
2846 | if (altflush) { | |
2847 | struct buf *alt_bp = NULL; | |
2848 | u_long altIDSector; | |
2849 | ||
2850 | altIDSector = (vcb->hfsPlusIOPosOffset / sectorsize) + | |
2851 | HFS_ALT_SECTOR(sectorsize, hfsmp->hfs_phys_block_count); | |
2852 | ||
2853 | if (meta_bread(hfsmp->hfs_devvp, altIDSector, sectorsize, NOCRED, &alt_bp) == 0) { | |
b4c24cb9 A |
2854 | if (hfsmp->jnl) { |
2855 | journal_modify_block_start(hfsmp->jnl, alt_bp); | |
2856 | } | |
2857 | ||
9bccf70c | 2858 | bcopy(volumeHeader, alt_bp->b_data + HFS_ALT_OFFSET(sectorsize), kMDBSize); |
b4c24cb9 A |
2859 | |
2860 | if (hfsmp->jnl) { | |
2861 | journal_modify_block_end(hfsmp->jnl, alt_bp); | |
2862 | } else { | |
2863 | (void) VOP_BWRITE(alt_bp); | |
2864 | } | |
9bccf70c A |
2865 | } else if (alt_bp) |
2866 | brelse(alt_bp); | |
2867 | } | |
2868 | ||
b4c24cb9 A |
2869 | // XXXdbg |
2870 | if (hfsmp->jnl) { | |
2871 | journal_modify_block_end(hfsmp->jnl, bp); | |
2872 | journal_end_transaction(hfsmp->jnl); | |
2873 | } else { | |
2874 | if (waitfor != MNT_WAIT) | |
2875 | bawrite(bp); | |
2876 | else { | |
2877 | retval = VOP_BWRITE(bp); | |
2878 | /* When critical data changes, flush the device cache */ | |
2879 | if (critical && (retval == 0)) { | |
9bccf70c | 2880 | (void) VOP_IOCTL(hfsmp->hfs_devvp, DKIOCSYNCHRONIZECACHE, |
b4c24cb9 A |
2881 | NULL, FWRITE, NOCRED, current_proc()); |
2882 | } | |
9bccf70c A |
2883 | } |
2884 | } | |
b4c24cb9 | 2885 | hfs_global_shared_lock_release(hfsmp); |
1c79356b | 2886 | |
9bccf70c | 2887 | vcb->vcbFlags &= 0x00FF; |
1c79356b A |
2888 | return (retval); |
2889 | } | |
2890 | ||
2891 | ||
55e303ae A |
2892 | /* |
2893 | * Extend a file system. | |
2894 | */ | |
2895 | static int | |
2896 | hfs_extendfs(struct mount *mp, u_int64_t newsize, struct proc *p) | |
2897 | { | |
2898 | struct vnode *vp; | |
2899 | struct vnode *devvp; | |
2900 | struct buf *bp; | |
2901 | struct hfsmount *hfsmp; | |
2902 | struct filefork *fp = NULL; | |
2903 | ExtendedVCB *vcb; | |
2904 | struct cat_fork forkdata; | |
2905 | u_int64_t oldsize; | |
2906 | u_int64_t newblkcnt; | |
2907 | u_int32_t addblks; | |
2908 | u_int64_t sectorcnt; | |
2909 | u_int32_t sectorsize; | |
2910 | daddr_t prev_alt_sector; | |
2911 | daddr_t bitmapblks; | |
2912 | int error; | |
2913 | ||
2914 | hfsmp = VFSTOHFS(mp); | |
2915 | devvp = hfsmp->hfs_devvp; | |
2916 | vcb = HFSTOVCB(hfsmp); | |
2917 | ||
2918 | /* | |
2919 | * - HFS Plus file systems only. | |
2920 | * - Journaling must be enabled. | |
2921 | * - No embedded volumes. | |
2922 | */ | |
2923 | if ((vcb->vcbSigWord == kHFSSigWord) || | |
2924 | (hfsmp->jnl == NULL) || | |
2925 | (vcb->hfsPlusIOPosOffset != 0)) { | |
2926 | return (EPERM); | |
2927 | } | |
2928 | /* | |
2929 | * If extending file system by non-root, then verify | |
2930 | * ownership and check permissions. | |
2931 | */ | |
2932 | if (p->p_ucred->cr_uid != 0) { | |
2933 | error = hfs_root(mp, &vp); | |
2934 | if (error) | |
2935 | return (error); | |
2936 | error = hfs_owner_rights(hfsmp, VTOC(vp)->c_uid, p->p_ucred, p, 0); | |
2937 | if (error == 0) { | |
2938 | error = hfs_write_access(vp, p->p_ucred, p, false); | |
2939 | } | |
2940 | vput(vp); | |
2941 | if (error) | |
2942 | return (error); | |
2943 | ||
2944 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); | |
2945 | error = VOP_ACCESS(devvp, VREAD | VWRITE, p->p_ucred, p); | |
2946 | VOP_UNLOCK(devvp, 0, p); | |
2947 | if (error) | |
2948 | return (error); | |
2949 | } | |
2950 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)§orsize, 0, FSCRED, p)) { | |
2951 | return (ENXIO); | |
2952 | } | |
2953 | if (sectorsize != hfsmp->hfs_phys_block_size) { | |
2954 | return (ENXIO); | |
2955 | } | |
2956 | if (VOP_IOCTL(devvp, DKIOCGETBLOCKCOUNT, (caddr_t)§orcnt, 0, FSCRED, p)) { | |
2957 | return (ENXIO); | |
2958 | } | |
2959 | if ((sectorsize * sectorcnt) < newsize) { | |
2960 | printf("hfs_extendfs: not enough space on device\n"); | |
2961 | return (ENOSPC); | |
2962 | } | |
2963 | oldsize = (u_int64_t)hfsmp->hfs_phys_block_count * | |
2964 | (u_int64_t)hfsmp->hfs_phys_block_size; | |
2965 | ||
2966 | /* | |
2967 | * Validate new size. | |
2968 | */ | |
2969 | if ((newsize <= oldsize) || (newsize % vcb->blockSize)) { | |
2970 | printf("hfs_extendfs: invalid size\n"); | |
2971 | return (EINVAL); | |
2972 | } | |
2973 | newblkcnt = newsize / vcb->blockSize; | |
2974 | if (newblkcnt > (u_int64_t)0xFFFFFFFF) | |
2975 | return (EOVERFLOW); | |
2976 | ||
2977 | addblks = newblkcnt - vcb->totalBlocks; | |
2978 | ||
2979 | printf("hfs_extendfs: growing %s by %d blocks\n", vcb->vcbVN, addblks); | |
2980 | /* | |
2981 | * Enclose changes inside a transaction. | |
2982 | */ | |
2983 | hfs_global_shared_lock_acquire(hfsmp); | |
2984 | if (journal_start_transaction(hfsmp->jnl) != 0) { | |
2985 | hfs_global_shared_lock_release(hfsmp); | |
2986 | return (EINVAL); | |
2987 | } | |
2988 | ||
2989 | /* | |
2990 | * Remember the location of existing alternate VH. | |
2991 | */ | |
2992 | prev_alt_sector = (vcb->hfsPlusIOPosOffset / sectorsize) + | |
2993 | HFS_ALT_SECTOR(sectorsize, hfsmp->hfs_phys_block_count); | |
2994 | ||
2995 | vp = vcb->allocationsRefNum; | |
2996 | error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); | |
2997 | if (error) { | |
2998 | goto out2; | |
2999 | } | |
3000 | fp = VTOF(vp); | |
3001 | bcopy(&fp->ff_data, &forkdata, sizeof(forkdata)); | |
3002 | ||
3003 | /* | |
3004 | * Calculate additional space required (if any) by allocation bitmap. | |
3005 | */ | |
3006 | bitmapblks = roundup(newblkcnt / 8, vcb->vcbVBMIOSize) / vcb->blockSize; | |
3007 | if (bitmapblks > fp->ff_blocks) | |
3008 | bitmapblks -= fp->ff_blocks; | |
3009 | else | |
3010 | bitmapblks = 0; | |
3011 | ||
3012 | if (bitmapblks > 0) { | |
3013 | daddr_t blkno; | |
3014 | daddr_t blkcnt; | |
3015 | ||
3016 | /* | |
3017 | * Add a new extent to the allocation bitmap file. | |
3018 | */ | |
3019 | error = AddFileExtent(vcb, fp, vcb->totalBlocks, bitmapblks); | |
3020 | if (error) { | |
3021 | printf("hfs_extendfs: error %d adding extents\n", error); | |
3022 | goto out; | |
3023 | } | |
3024 | blkcnt = bitmapblks; | |
3025 | blkno = fp->ff_blocks; | |
3026 | fp->ff_blocks += bitmapblks; | |
3027 | fp->ff_size += (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize; | |
3028 | VTOC(vp)->c_blocks = fp->ff_blocks; | |
3029 | /* | |
3030 | * Zero out the new bitmap blocks. | |
3031 | */ | |
3032 | { | |
3033 | ||
3034 | bp = NULL; | |
3035 | while (blkcnt > 0) { | |
3036 | error = meta_bread(vp, blkno, vcb->blockSize, NOCRED, &bp); | |
3037 | if (error) { | |
3038 | if (bp) { | |
3039 | brelse(bp); | |
3040 | } | |
3041 | break; | |
3042 | } | |
3043 | bzero((char *)bp->b_data, vcb->blockSize); | |
3044 | bp->b_flags |= B_AGE; | |
3045 | error = bwrite(bp); | |
3046 | if (error) | |
3047 | break; | |
3048 | --blkcnt; | |
3049 | ++blkno; | |
3050 | } | |
3051 | } | |
3052 | if (error) { | |
3053 | printf("hfs_extendfs: error %d clearing blocks\n", error); | |
3054 | goto out; | |
3055 | } | |
3056 | /* | |
3057 | * Mark the new bitmap space as allocated. | |
3058 | */ | |
3059 | error = BlockMarkAllocated(vcb, vcb->totalBlocks, bitmapblks); | |
3060 | if (error) { | |
3061 | printf("hfs_extendfs: error %d setting bitmap\n", error); | |
3062 | goto out; | |
3063 | } | |
3064 | } | |
3065 | /* | |
3066 | * Mark the new alternate VH as allocated. | |
3067 | */ | |
3068 | if (vcb->blockSize == 512) | |
3069 | error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 2, 2); | |
3070 | else | |
3071 | error = BlockMarkAllocated(vcb, vcb->totalBlocks + addblks - 1, 1); | |
3072 | if (error) { | |
3073 | printf("hfs_extendfs: error %d setting bitmap (VH)\n", error); | |
3074 | goto out; | |
3075 | } | |
3076 | /* | |
3077 | * Mark the old alternate VH as free. | |
3078 | */ | |
3079 | if (vcb->blockSize == 512) | |
3080 | (void) BlockMarkFree(vcb, vcb->totalBlocks - 2, 2); | |
3081 | else | |
3082 | (void) BlockMarkFree(vcb, vcb->totalBlocks - 1, 1); | |
3083 | ||
3084 | /* | |
3085 | * Adjust file system variables for new space. | |
3086 | */ | |
3087 | vcb->totalBlocks += addblks; | |
3088 | vcb->freeBlocks += addblks - bitmapblks; | |
3089 | hfsmp->hfs_phys_block_count = newsize / sectorsize; | |
3090 | ||
3091 | MarkVCBDirty(vcb); | |
3092 | error = hfs_flushvolumeheader(hfsmp, MNT_WAIT, HFS_ALTFLUSH); | |
3093 | if (error) { | |
3094 | printf("hfs_extendfs: couldn't flush volume headers (%d)", error); | |
3095 | /* | |
3096 | * Restore to old state. | |
3097 | */ | |
3098 | fp->ff_size -= (u_int64_t)bitmapblks * (u_int64_t)vcb->blockSize; | |
3099 | vcb->totalBlocks -= addblks; | |
3100 | vcb->freeBlocks -= addblks - bitmapblks; | |
3101 | hfsmp->hfs_phys_block_count = oldsize / sectorsize; | |
3102 | MarkVCBDirty(vcb); | |
3103 | if (vcb->blockSize == 512) | |
3104 | (void) BlockMarkAllocated(vcb, vcb->totalBlocks - 2, 2); | |
3105 | else | |
3106 | (void) BlockMarkAllocated(vcb, vcb->totalBlocks - 1, 1); | |
3107 | goto out; | |
3108 | } | |
3109 | /* | |
3110 | * Invalidate the old alternate volume header. | |
3111 | */ | |
3112 | bp = NULL; | |
3113 | if (meta_bread(hfsmp->hfs_devvp, prev_alt_sector, sectorsize, | |
3114 | NOCRED, &bp) == 0) { | |
3115 | journal_modify_block_start(hfsmp->jnl, bp); | |
3116 | bzero(bp->b_data + HFS_ALT_OFFSET(sectorsize), kMDBSize); | |
3117 | journal_modify_block_end(hfsmp->jnl, bp); | |
3118 | } else if (bp) { | |
3119 | brelse(bp); | |
3120 | } | |
3121 | out: | |
3122 | if (error && fp) { | |
3123 | /* Restore allocation fork. */ | |
3124 | bcopy(&forkdata, &fp->ff_data, sizeof(forkdata)); | |
3125 | VTOC(vp)->c_blocks = fp->ff_blocks; | |
3126 | ||
3127 | } | |
3128 | VOP_UNLOCK(vp, 0, p); | |
3129 | out2: | |
3130 | journal_end_transaction(hfsmp->jnl); | |
3131 | hfs_global_shared_lock_release(hfsmp); | |
3132 | ||
3133 | return (error); | |
3134 | } | |
3135 | ||
3136 | ||
1c79356b A |
3137 | /* |
3138 | * hfs vfs operations. | |
3139 | */ | |
3140 | struct vfsops hfs_vfsops = { | |
9bccf70c A |
3141 | hfs_mount, |
3142 | hfs_start, | |
3143 | hfs_unmount, | |
3144 | hfs_root, | |
3145 | hfs_quotactl, | |
3146 | hfs_statfs, | |
3147 | hfs_sync, | |
3148 | hfs_vget, | |
3149 | hfs_fhtovp, | |
3150 | hfs_vptofh, | |
3151 | hfs_init, | |
3152 | hfs_sysctl | |
1c79356b | 3153 | }; |