ffs_vfsops.c revision 1.324 1 /* $NetBSD: ffs_vfsops.c,v 1.324 2015/03/15 09:21:01 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1989, 1991, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.324 2015/03/15 09:21:01 maxv Exp $");
65
66 #if defined(_KERNEL_OPT)
67 #include "opt_ffs.h"
68 #include "opt_quota.h"
69 #include "opt_wapbl.h"
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/namei.h>
75 #include <sys/proc.h>
76 #include <sys/kernel.h>
77 #include <sys/vnode.h>
78 #include <sys/socket.h>
79 #include <sys/mount.h>
80 #include <sys/buf.h>
81 #include <sys/device.h>
82 #include <sys/disk.h>
83 #include <sys/mbuf.h>
84 #include <sys/file.h>
85 #include <sys/disklabel.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/kmem.h>
89 #include <sys/pool.h>
90 #include <sys/lock.h>
91 #include <sys/sysctl.h>
92 #include <sys/conf.h>
93 #include <sys/kauth.h>
94 #include <sys/wapbl.h>
95 #include <sys/fstrans.h>
96 #include <sys/module.h>
97
98 #include <miscfs/genfs/genfs.h>
99 #include <miscfs/specfs/specdev.h>
100
101 #include <ufs/ufs/quota.h>
102 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ufs/inode.h>
104 #include <ufs/ufs/dir.h>
105 #include <ufs/ufs/ufs_extern.h>
106 #include <ufs/ufs/ufs_bswap.h>
107 #include <ufs/ufs/ufs_wapbl.h>
108
109 #include <ufs/ffs/fs.h>
110 #include <ufs/ffs/ffs_extern.h>
111
112 MODULE(MODULE_CLASS_VFS, ffs, NULL);
113
114 static int ffs_vfs_fsync(vnode_t *, int);
115 static int ffs_superblock_validate(struct fs *);
116
117 static struct sysctllog *ffs_sysctl_log;
118
119 static kauth_listener_t ffs_snapshot_listener;
120
121 /* how many times ffs_init() was called */
122 int ffs_initcount = 0;
123
124 #ifdef DEBUG_FFS_MOUNT
125 #define DPRINTF(_fmt, args...) printf("%s: " _fmt "\n", __func__, ##args)
126 #else
127 #define DPRINTF(_fmt, args...) do {} while (/*CONSTCOND*/0)
128 #endif
129
130 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
131 extern const struct vnodeopv_desc ffs_specop_opv_desc;
132 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
133
134 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
135 &ffs_vnodeop_opv_desc,
136 &ffs_specop_opv_desc,
137 &ffs_fifoop_opv_desc,
138 NULL,
139 };
140
141 struct vfsops ffs_vfsops = {
142 .vfs_name = MOUNT_FFS,
143 .vfs_min_mount_data = sizeof (struct ufs_args),
144 .vfs_mount = ffs_mount,
145 .vfs_start = ufs_start,
146 .vfs_unmount = ffs_unmount,
147 .vfs_root = ufs_root,
148 .vfs_quotactl = ufs_quotactl,
149 .vfs_statvfs = ffs_statvfs,
150 .vfs_sync = ffs_sync,
151 .vfs_vget = ufs_vget,
152 .vfs_loadvnode = ffs_loadvnode,
153 .vfs_fhtovp = ffs_fhtovp,
154 .vfs_vptofh = ffs_vptofh,
155 .vfs_init = ffs_init,
156 .vfs_reinit = ffs_reinit,
157 .vfs_done = ffs_done,
158 .vfs_mountroot = ffs_mountroot,
159 .vfs_snapshot = ffs_snapshot,
160 .vfs_extattrctl = ffs_extattrctl,
161 .vfs_suspendctl = ffs_suspendctl,
162 .vfs_renamelock_enter = genfs_renamelock_enter,
163 .vfs_renamelock_exit = genfs_renamelock_exit,
164 .vfs_fsync = ffs_vfs_fsync,
165 .vfs_opv_descs = ffs_vnodeopv_descs
166 };
167
168 static const struct genfs_ops ffs_genfsops = {
169 .gop_size = ffs_gop_size,
170 .gop_alloc = ufs_gop_alloc,
171 .gop_write = genfs_gop_write,
172 .gop_markupdate = ufs_gop_markupdate,
173 };
174
175 static const struct ufs_ops ffs_ufsops = {
176 .uo_itimes = ffs_itimes,
177 .uo_update = ffs_update,
178 .uo_truncate = ffs_truncate,
179 .uo_valloc = ffs_valloc,
180 .uo_vfree = ffs_vfree,
181 .uo_balloc = ffs_balloc,
182 .uo_snapgone = ffs_snapgone,
183 };
184
185 static int
186 ffs_snapshot_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
187 void *arg0, void *arg1, void *arg2, void *arg3)
188 {
189 vnode_t *vp = arg2;
190 int result = KAUTH_RESULT_DEFER;
191
192 if (action != KAUTH_SYSTEM_FS_SNAPSHOT)
193 return result;
194
195 if (VTOI(vp)->i_uid == kauth_cred_geteuid(cred))
196 result = KAUTH_RESULT_ALLOW;
197
198 return result;
199 }
200
201 static int
202 ffs_modcmd(modcmd_t cmd, void *arg)
203 {
204 int error;
205
206 #if 0
207 extern int doasyncfree;
208 #endif
209 #ifdef UFS_EXTATTR
210 extern int ufs_extattr_autocreate;
211 #endif
212 extern int ffs_log_changeopt;
213
214 switch (cmd) {
215 case MODULE_CMD_INIT:
216 error = vfs_attach(&ffs_vfsops);
217 if (error != 0)
218 break;
219
220 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
221 CTLFLAG_PERMANENT,
222 CTLTYPE_NODE, "ffs",
223 SYSCTL_DESCR("Berkeley Fast File System"),
224 NULL, 0, NULL, 0,
225 CTL_VFS, 1, CTL_EOL);
226 /*
227 * @@@ should we even bother with these first three?
228 */
229 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
230 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
231 CTLTYPE_INT, "doclusterread", NULL,
232 sysctl_notavail, 0, NULL, 0,
233 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
234 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
235 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
236 CTLTYPE_INT, "doclusterwrite", NULL,
237 sysctl_notavail, 0, NULL, 0,
238 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
239 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
240 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
241 CTLTYPE_INT, "doreallocblks", NULL,
242 sysctl_notavail, 0, NULL, 0,
243 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
244 #if 0
245 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
246 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
247 CTLTYPE_INT, "doasyncfree",
248 SYSCTL_DESCR("Release dirty blocks asynchronously"),
249 NULL, 0, &doasyncfree, 0,
250 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
251 #endif
252 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
253 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
254 CTLTYPE_INT, "log_changeopt",
255 SYSCTL_DESCR("Log changes in optimization strategy"),
256 NULL, 0, &ffs_log_changeopt, 0,
257 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
258 #ifdef UFS_EXTATTR
259 sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
260 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
261 CTLTYPE_INT, "extattr_autocreate",
262 SYSCTL_DESCR("Size of attribute for "
263 "backing file autocreation"),
264 NULL, 0, &ufs_extattr_autocreate, 0,
265 CTL_VFS, 1, FFS_EXTATTR_AUTOCREATE, CTL_EOL);
266
267 #endif /* UFS_EXTATTR */
268
269 ffs_snapshot_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
270 ffs_snapshot_cb, NULL);
271 if (ffs_snapshot_listener == NULL)
272 printf("ffs_modcmd: can't listen on system scope.\n");
273
274 break;
275 case MODULE_CMD_FINI:
276 error = vfs_detach(&ffs_vfsops);
277 if (error != 0)
278 break;
279 sysctl_teardown(&ffs_sysctl_log);
280 if (ffs_snapshot_listener != NULL)
281 kauth_unlisten_scope(ffs_snapshot_listener);
282 break;
283 default:
284 error = ENOTTY;
285 break;
286 }
287
288 return (error);
289 }
290
291 pool_cache_t ffs_inode_cache;
292 pool_cache_t ffs_dinode1_cache;
293 pool_cache_t ffs_dinode2_cache;
294
295 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
296 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
297
298 /*
299 * Called by main() when ffs is going to be mounted as root.
300 */
301
302 int
303 ffs_mountroot(void)
304 {
305 struct fs *fs;
306 struct mount *mp;
307 struct lwp *l = curlwp; /* XXX */
308 struct ufsmount *ump;
309 int error;
310
311 if (device_class(root_device) != DV_DISK)
312 return (ENODEV);
313
314 if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
315 vrele(rootvp);
316 return (error);
317 }
318
319 /*
320 * We always need to be able to mount the root file system.
321 */
322 mp->mnt_flag |= MNT_FORCE;
323 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
324 vfs_unbusy(mp, false, NULL);
325 vfs_destroy(mp);
326 return (error);
327 }
328 mp->mnt_flag &= ~MNT_FORCE;
329 mountlist_append(mp);
330 ump = VFSTOUFS(mp);
331 fs = ump->um_fs;
332 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
333 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
334 (void)ffs_statvfs(mp, &mp->mnt_stat);
335 vfs_unbusy(mp, false, NULL);
336 setrootfstime((time_t)fs->fs_time);
337 return (0);
338 }
339
340 /*
341 * VFS Operations.
342 *
343 * mount system call
344 */
345 int
346 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
347 {
348 struct lwp *l = curlwp;
349 struct vnode *devvp = NULL;
350 struct ufs_args *args = data;
351 struct ufsmount *ump = NULL;
352 struct fs *fs;
353 int error = 0, flags, update;
354 mode_t accessmode;
355
356 if (args == NULL) {
357 DPRINTF("NULL args");
358 return EINVAL;
359 }
360 if (*data_len < sizeof(*args)) {
361 DPRINTF("bad size args %zu != %zu", *data_len, sizeof(*args));
362 return EINVAL;
363 }
364
365 if (mp->mnt_flag & MNT_GETARGS) {
366 ump = VFSTOUFS(mp);
367 if (ump == NULL) {
368 DPRINTF("no ump");
369 return EIO;
370 }
371 args->fspec = NULL;
372 *data_len = sizeof *args;
373 return 0;
374 }
375
376 update = mp->mnt_flag & MNT_UPDATE;
377
378 /* Check arguments */
379 if (args->fspec != NULL) {
380 /*
381 * Look up the name and verify that it's sane.
382 */
383 error = namei_simple_user(args->fspec,
384 NSM_FOLLOW_NOEMULROOT, &devvp);
385 if (error != 0) {
386 DPRINTF("namei_simple_user returned %d", error);
387 return error;
388 }
389
390 if (!update) {
391 /*
392 * Be sure this is a valid block device
393 */
394 if (devvp->v_type != VBLK) {
395 DPRINTF("non block device %d", devvp->v_type);
396 error = ENOTBLK;
397 } else if (bdevsw_lookup(devvp->v_rdev) == NULL) {
398 DPRINTF("can't find block device 0x%jx",
399 devvp->v_rdev);
400 error = ENXIO;
401 }
402 } else {
403 /*
404 * Be sure we're still naming the same device
405 * used for our initial mount
406 */
407 ump = VFSTOUFS(mp);
408 if (devvp != ump->um_devvp) {
409 if (devvp->v_rdev != ump->um_devvp->v_rdev) {
410 DPRINTF("wrong device 0x%jx != 0x%jx",
411 (uintmax_t)devvp->v_rdev,
412 (uintmax_t)ump->um_devvp->v_rdev);
413 error = EINVAL;
414 } else {
415 vrele(devvp);
416 devvp = ump->um_devvp;
417 vref(devvp);
418 }
419 }
420 }
421 } else {
422 if (!update) {
423 /* New mounts must have a filename for the device */
424 DPRINTF("no filename for mount");
425 return EINVAL;
426 } else {
427 /* Use the extant mount */
428 ump = VFSTOUFS(mp);
429 devvp = ump->um_devvp;
430 vref(devvp);
431 }
432 }
433
434 /*
435 * If mount by non-root, then verify that user has necessary
436 * permissions on the device.
437 *
438 * Permission to update a mount is checked higher, so here we presume
439 * updating the mount is okay (for example, as far as securelevel goes)
440 * which leaves us with the normal check.
441 */
442 if (error == 0) {
443 accessmode = VREAD;
444 if (update ?
445 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
446 (mp->mnt_flag & MNT_RDONLY) == 0)
447 accessmode |= VWRITE;
448 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
449 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
450 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
451 KAUTH_ARG(accessmode));
452 if (error) {
453 DPRINTF("kauth returned %d", error);
454 }
455 VOP_UNLOCK(devvp);
456 }
457
458 if (error) {
459 vrele(devvp);
460 return (error);
461 }
462
463 #ifdef WAPBL
464 /* WAPBL can only be enabled on a r/w mount. */
465 if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
466 mp->mnt_flag &= ~MNT_LOG;
467 }
468 #else /* !WAPBL */
469 mp->mnt_flag &= ~MNT_LOG;
470 #endif /* !WAPBL */
471
472 if (!update) {
473 int xflags;
474
475 if (mp->mnt_flag & MNT_RDONLY)
476 xflags = FREAD;
477 else
478 xflags = FREAD | FWRITE;
479 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
480 error = VOP_OPEN(devvp, xflags, FSCRED);
481 VOP_UNLOCK(devvp);
482 if (error) {
483 DPRINTF("VOP_OPEN returned %d", error);
484 goto fail;
485 }
486 error = ffs_mountfs(devvp, mp, l);
487 if (error) {
488 DPRINTF("ffs_mountfs returned %d", error);
489 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
490 (void)VOP_CLOSE(devvp, xflags, NOCRED);
491 VOP_UNLOCK(devvp);
492 goto fail;
493 }
494
495 ump = VFSTOUFS(mp);
496 fs = ump->um_fs;
497 } else {
498 /*
499 * Update the mount.
500 */
501
502 /*
503 * The initial mount got a reference on this
504 * device, so drop the one obtained via
505 * namei(), above.
506 */
507 vrele(devvp);
508
509 ump = VFSTOUFS(mp);
510 fs = ump->um_fs;
511 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
512 /*
513 * Changing from r/w to r/o
514 */
515 flags = WRITECLOSE;
516 if (mp->mnt_flag & MNT_FORCE)
517 flags |= FORCECLOSE;
518 error = ffs_flushfiles(mp, flags, l);
519 if (error == 0)
520 error = UFS_WAPBL_BEGIN(mp);
521 if (error == 0 &&
522 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
523 fs->fs_clean & FS_WASCLEAN) {
524 if (mp->mnt_flag & MNT_SOFTDEP)
525 fs->fs_flags &= ~FS_DOSOFTDEP;
526 fs->fs_clean = FS_ISCLEAN;
527 (void) ffs_sbupdate(ump, MNT_WAIT);
528 }
529 if (error) {
530 DPRINTF("wapbl %d", error);
531 return error;
532 }
533 UFS_WAPBL_END(mp);
534 }
535
536 #ifdef WAPBL
537 if ((mp->mnt_flag & MNT_LOG) == 0) {
538 error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
539 if (error) {
540 DPRINTF("ffs_wapbl_stop returned %d", error);
541 return error;
542 }
543 }
544 #endif /* WAPBL */
545
546 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
547 /*
548 * Finish change from r/w to r/o
549 */
550 fs->fs_ronly = 1;
551 fs->fs_fmod = 0;
552 }
553
554 if (mp->mnt_flag & MNT_RELOAD) {
555 error = ffs_reload(mp, l->l_cred, l);
556 if (error) {
557 DPRINTF("ffs_reload returned %d", error);
558 return error;
559 }
560 }
561
562 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
563 /*
564 * Changing from read-only to read/write
565 */
566 #ifndef QUOTA2
567 if (fs->fs_flags & FS_DOQUOTA2) {
568 ump->um_flags |= UFS_QUOTA2;
569 uprintf("%s: options QUOTA2 not enabled%s\n",
570 mp->mnt_stat.f_mntonname,
571 (mp->mnt_flag & MNT_FORCE) ? "" :
572 ", not mounting");
573 DPRINTF("ffs_quota2 %d", EINVAL);
574 return EINVAL;
575 }
576 #endif
577 fs->fs_ronly = 0;
578 fs->fs_clean <<= 1;
579 fs->fs_fmod = 1;
580 #ifdef WAPBL
581 if (fs->fs_flags & FS_DOWAPBL) {
582 const char *nm = mp->mnt_stat.f_mntonname;
583 if (!mp->mnt_wapbl_replay) {
584 printf("%s: log corrupted;"
585 " replay cancelled\n", nm);
586 return EFTYPE;
587 }
588 printf("%s: replaying log to disk\n", nm);
589 error = wapbl_replay_write(mp->mnt_wapbl_replay,
590 devvp);
591 if (error) {
592 DPRINTF("%s: wapbl_replay_write %d",
593 nm, error);
594 return error;
595 }
596 wapbl_replay_stop(mp->mnt_wapbl_replay);
597 fs->fs_clean = FS_WASCLEAN;
598 }
599 #endif /* WAPBL */
600 if (fs->fs_snapinum[0] != 0)
601 ffs_snapshot_mount(mp);
602 }
603
604 #ifdef WAPBL
605 error = ffs_wapbl_start(mp);
606 if (error) {
607 DPRINTF("ffs_wapbl_start returned %d", error);
608 return error;
609 }
610 #endif /* WAPBL */
611
612 #ifdef QUOTA2
613 if (!fs->fs_ronly) {
614 error = ffs_quota2_mount(mp);
615 if (error) {
616 DPRINTF("ffs_quota2_mount returned %d", error);
617 return error;
618 }
619 }
620 #endif
621
622 if ((mp->mnt_flag & MNT_DISCARD) && !(ump->um_discarddata))
623 ump->um_discarddata = ffs_discard_init(devvp, fs);
624
625 if (args->fspec == NULL)
626 return 0;
627 }
628
629 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
630 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
631 if (error == 0)
632 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
633 sizeof(fs->fs_fsmnt));
634 else {
635 DPRINTF("set_statvfs_info returned %d", error);
636 }
637 fs->fs_flags &= ~FS_DOSOFTDEP;
638 if (fs->fs_fmod != 0) { /* XXX */
639 int err;
640
641 fs->fs_fmod = 0;
642 if (fs->fs_clean & FS_WASCLEAN)
643 fs->fs_time = time_second;
644 else {
645 printf("%s: file system not clean (fs_clean=%#x); "
646 "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
647 fs->fs_clean);
648 printf("%s: lost blocks %" PRId64 " files %d\n",
649 mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
650 fs->fs_pendinginodes);
651 }
652 err = UFS_WAPBL_BEGIN(mp);
653 if (err == 0) {
654 (void) ffs_cgupdate(ump, MNT_WAIT);
655 UFS_WAPBL_END(mp);
656 }
657 }
658 if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
659 printf("%s: `-o softdep' is no longer supported, "
660 "consider `-o log'\n", mp->mnt_stat.f_mntfromname);
661 mp->mnt_flag &= ~MNT_SOFTDEP;
662 }
663
664 return (error);
665
666 fail:
667 vrele(devvp);
668 return (error);
669 }
670
671 /*
672 * Reload all incore data for a filesystem (used after running fsck on
673 * the root filesystem and finding things to fix). The filesystem must
674 * be mounted read-only.
675 *
676 * Things to do to update the mount:
677 * 1) invalidate all cached meta-data.
678 * 2) re-read superblock from disk.
679 * 3) re-read summary information from disk.
680 * 4) invalidate all inactive vnodes.
681 * 5) invalidate all cached file data.
682 * 6) re-read inode data for all active vnodes.
683 */
684 int
685 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
686 {
687 struct vnode *vp, *devvp;
688 struct inode *ip;
689 void *space;
690 struct buf *bp;
691 struct fs *fs, *newfs;
692 struct dkwedge_info dkw;
693 int i, bsize, blks, error;
694 int32_t *lp, fs_sbsize;
695 struct ufsmount *ump;
696 daddr_t sblockloc;
697 struct vnode_iterator *marker;
698
699 if ((mp->mnt_flag & MNT_RDONLY) == 0)
700 return (EINVAL);
701
702 ump = VFSTOUFS(mp);
703
704 /*
705 * Step 1: invalidate all cached meta-data.
706 */
707 devvp = ump->um_devvp;
708 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
709 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
710 VOP_UNLOCK(devvp);
711 if (error)
712 panic("ffs_reload: dirty1");
713
714 /*
715 * Step 2: re-read superblock from disk. XXX: We don't handle
716 * possibility that superblock moved. Which implies that we don't
717 * want its size to change either.
718 */
719 fs = ump->um_fs;
720 fs_sbsize = fs->fs_sbsize;
721 error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, fs_sbsize,
722 NOCRED, 0, &bp);
723 if (error)
724 return (error);
725 newfs = kmem_alloc(fs_sbsize, KM_SLEEP);
726 memcpy(newfs, bp->b_data, fs_sbsize);
727
728 #ifdef FFS_EI
729 if (ump->um_flags & UFS_NEEDSWAP) {
730 ffs_sb_swap((struct fs *)bp->b_data, newfs);
731 newfs->fs_flags |= FS_SWAPPED;
732 } else
733 #endif
734 newfs->fs_flags &= ~FS_SWAPPED;
735
736 brelse(bp, 0);
737
738 if ((newfs->fs_magic != FS_UFS1_MAGIC) &&
739 (newfs->fs_magic != FS_UFS2_MAGIC)) {
740 kmem_free(newfs, fs_sbsize);
741 return (EIO); /* XXX needs translation */
742 }
743 if (!ffs_superblock_validate(newfs)) {
744 kmem_free(newfs, fs_sbsize);
745 return (EINVAL);
746 }
747
748 /*
749 * The current implementation doesn't handle the possibility that
750 * these values may have changed.
751 */
752 if ((newfs->fs_sbsize != fs_sbsize) ||
753 (newfs->fs_cssize != fs->fs_cssize) ||
754 (newfs->fs_contigsumsize != fs->fs_contigsumsize) ||
755 (newfs->fs_ncg != fs->fs_ncg)) {
756 kmem_free(newfs, fs_sbsize);
757 return (EINVAL);
758 }
759
760 /* Store off old fs_sblockloc for fs_oldfscompat_read. */
761 sblockloc = fs->fs_sblockloc;
762 /*
763 * Copy pointer fields back into superblock before copying in XXX
764 * new superblock. These should really be in the ufsmount. XXX
765 * Note that important parameters (eg fs_ncg) are unchanged.
766 */
767 newfs->fs_csp = fs->fs_csp;
768 newfs->fs_maxcluster = fs->fs_maxcluster;
769 newfs->fs_contigdirs = fs->fs_contigdirs;
770 newfs->fs_ronly = fs->fs_ronly;
771 newfs->fs_active = fs->fs_active;
772 memcpy(fs, newfs, (u_int)fs_sbsize);
773 kmem_free(newfs, fs_sbsize);
774
775 /* Recheck for apple UFS filesystem */
776 ump->um_flags &= ~UFS_ISAPPLEUFS;
777 /* First check to see if this is tagged as an Apple UFS filesystem
778 * in the disklabel
779 */
780 if (getdiskinfo(devvp, &dkw) == 0 &&
781 strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
782 ump->um_flags |= UFS_ISAPPLEUFS;
783 #ifdef APPLE_UFS
784 else {
785 /* Manually look for an apple ufs label, and if a valid one
786 * is found, then treat it like an Apple UFS filesystem anyway
787 *
788 * EINVAL is most probably a blocksize or alignment problem,
789 * it is unlikely that this is an Apple UFS filesystem then.
790 */
791 error = bread(devvp,
792 (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
793 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
794 if (error && error != EINVAL) {
795 return error;
796 }
797 if (error == 0) {
798 error = ffs_appleufs_validate(fs->fs_fsmnt,
799 (struct appleufslabel *)bp->b_data, NULL);
800 if (error == 0)
801 ump->um_flags |= UFS_ISAPPLEUFS;
802 brelse(bp, 0);
803 }
804 bp = NULL;
805 }
806 #else
807 if (ump->um_flags & UFS_ISAPPLEUFS)
808 return (EIO);
809 #endif
810
811 if (UFS_MPISAPPLEUFS(ump)) {
812 /* see comment about NeXT below */
813 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
814 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
815 mp->mnt_iflag |= IMNT_DTYPE;
816 } else {
817 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
818 ump->um_dirblksiz = UFS_DIRBLKSIZ;
819 if (ump->um_maxsymlinklen > 0)
820 mp->mnt_iflag |= IMNT_DTYPE;
821 else
822 mp->mnt_iflag &= ~IMNT_DTYPE;
823 }
824 ffs_oldfscompat_read(fs, ump, sblockloc);
825
826 mutex_enter(&ump->um_lock);
827 ump->um_maxfilesize = fs->fs_maxfilesize;
828 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
829 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
830 mp->mnt_stat.f_mntonname, fs->fs_flags,
831 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
832 if ((mp->mnt_flag & MNT_FORCE) == 0) {
833 mutex_exit(&ump->um_lock);
834 return (EINVAL);
835 }
836 }
837 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
838 fs->fs_pendingblocks = 0;
839 fs->fs_pendinginodes = 0;
840 }
841 mutex_exit(&ump->um_lock);
842
843 ffs_statvfs(mp, &mp->mnt_stat);
844 /*
845 * Step 3: re-read summary information from disk.
846 */
847 blks = howmany(fs->fs_cssize, fs->fs_fsize);
848 space = fs->fs_csp;
849 for (i = 0; i < blks; i += fs->fs_frag) {
850 bsize = fs->fs_bsize;
851 if (i + fs->fs_frag > blks)
852 bsize = (blks - i) * fs->fs_fsize;
853 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
854 NOCRED, 0, &bp);
855 if (error) {
856 return (error);
857 }
858 #ifdef FFS_EI
859 if (UFS_FSNEEDSWAP(fs))
860 ffs_csum_swap((struct csum *)bp->b_data,
861 (struct csum *)space, bsize);
862 else
863 #endif
864 memcpy(space, bp->b_data, (size_t)bsize);
865 space = (char *)space + bsize;
866 brelse(bp, 0);
867 }
868 /*
869 * We no longer know anything about clusters per cylinder group.
870 */
871 if (fs->fs_contigsumsize > 0) {
872 lp = fs->fs_maxcluster;
873 for (i = 0; i < fs->fs_ncg; i++)
874 *lp++ = fs->fs_contigsumsize;
875 }
876
877 vfs_vnode_iterator_init(mp, &marker);
878 while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
879 /*
880 * Step 4: invalidate all inactive vnodes.
881 */
882 if (vrecycle(vp))
883 continue;
884 /*
885 * Step 5: invalidate all cached file data.
886 */
887 if (vn_lock(vp, LK_EXCLUSIVE)) {
888 vrele(vp);
889 continue;
890 }
891 if (vinvalbuf(vp, 0, cred, l, 0, 0))
892 panic("ffs_reload: dirty2");
893 /*
894 * Step 6: re-read inode data for all active vnodes.
895 */
896 ip = VTOI(vp);
897 error = bread(devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
898 (int)fs->fs_bsize, NOCRED, 0, &bp);
899 if (error) {
900 vput(vp);
901 break;
902 }
903 ffs_load_inode(bp, ip, fs, ip->i_number);
904 brelse(bp, 0);
905 vput(vp);
906 }
907 vfs_vnode_iterator_destroy(marker);
908 return (error);
909 }
910
911 /*
912 * Possible superblock locations ordered from most to least likely.
913 */
914 static const int sblock_try[] = SBLOCKSEARCH;
915
916
917 static int
918 ffs_superblock_validate(struct fs *fs)
919 {
920 int32_t i, fs_bshift = 0, fs_fshift = 0, fs_fragshift = 0, fs_frag;
921 int32_t fs_inopb;
922
923 /* Check the superblock size */
924 if (fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < sizeof(struct fs))
925 return 0;
926
927 /* Check the file system blocksize */
928 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < MINBSIZE)
929 return 0;
930 if (!powerof2(fs->fs_bsize))
931 return 0;
932
933 /* Check the size of frag blocks */
934 if (!powerof2(fs->fs_fsize))
935 return 0;
936 if (fs->fs_fsize == 0)
937 return 0;
938
939 /*
940 * XXX: these values are just zero-checked to prevent obvious
941 * bugs. We need more strict checks.
942 */
943 if (fs->fs_size == 0)
944 return 0;
945 if (fs->fs_cssize == 0)
946 return 0;
947 if (fs->fs_ipg == 0)
948 return 0;
949 if (fs->fs_fpg == 0)
950 return 0;
951
952 /* Check the number of inodes per block */
953 if (fs->fs_magic == FS_UFS1_MAGIC)
954 fs_inopb = fs->fs_bsize / sizeof(struct ufs1_dinode);
955 else /* fs->fs_magic == FS_UFS2_MAGIC */
956 fs_inopb = fs->fs_bsize / sizeof(struct ufs2_dinode);
957 if (fs->fs_inopb != fs_inopb)
958 return 0;
959
960 /* Block size cannot be smaller than fragment size */
961 if (fs->fs_bsize < fs->fs_fsize)
962 return 0;
963
964 /* Compute fs_bshift and ensure it is consistent */
965 for (i = fs->fs_bsize; i > 1; i >>= 1)
966 fs_bshift++;
967 if (fs->fs_bshift != fs_bshift)
968 return 0;
969
970 /* Compute fs_fshift and ensure it is consistent */
971 for (i = fs->fs_fsize; i > 1; i >>= 1)
972 fs_fshift++;
973 if (fs->fs_fshift != fs_fshift)
974 return 0;
975
976 /* Compute fs_fragshift and ensure it is consistent */
977 for (i = fs->fs_frag; i > 1; i >>= 1)
978 fs_fragshift++;
979 if (fs->fs_fragshift != fs_fragshift)
980 return 0;
981
982 /* Check the masks */
983 if (fs->fs_bmask != ~(fs->fs_bsize - 1))
984 return 0;
985 if (fs->fs_fmask != ~(fs->fs_fsize - 1))
986 return 0;
987
988 /*
989 * Now that the shifts and masks are sanitized, we can use the ffs_ API.
990 */
991
992 /* Check the number of frag blocks */
993 if ((fs_frag = ffs_numfrags(fs, fs->fs_bsize)) > MAXFRAG)
994 return 0;
995 if (fs->fs_frag != fs_frag)
996 return 0;
997
998 return 1;
999 }
1000
1001 /*
1002 * Common code for mount and mountroot
1003 */
1004 int
1005 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
1006 {
1007 struct ufsmount *ump = NULL;
1008 struct buf *bp = NULL;
1009 struct fs *fs = NULL;
1010 dev_t dev;
1011 struct dkwedge_info dkw;
1012 void *space;
1013 daddr_t sblockloc = 0;
1014 int blks, fstype = 0;
1015 int error, i, bsize, ronly, bset = 0;
1016 #ifdef FFS_EI
1017 int needswap = 0; /* keep gcc happy */
1018 #endif
1019 int32_t *lp;
1020 kauth_cred_t cred;
1021 u_int32_t allocsbsize, fs_sbsize = 0;
1022
1023 dev = devvp->v_rdev;
1024 cred = l ? l->l_cred : NOCRED;
1025
1026 /* Flush out any old buffers remaining from a previous use. */
1027 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1028 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
1029 VOP_UNLOCK(devvp);
1030 if (error) {
1031 DPRINTF("vinvalbuf returned %d", error);
1032 return error;
1033 }
1034
1035 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1036
1037 error = fstrans_mount(mp);
1038 if (error) {
1039 DPRINTF("fstrans_mount returned %d", error);
1040 return error;
1041 }
1042
1043 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
1044 mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
1045 error = ffs_snapshot_init(ump);
1046 if (error) {
1047 DPRINTF("ffs_snapshot_init returned %d", error);
1048 goto out;
1049 }
1050 ump->um_ops = &ffs_ufsops;
1051
1052 #ifdef WAPBL
1053 sbagain:
1054 #endif
1055 /*
1056 * Try reading the superblock in each of its possible locations.
1057 */
1058 for (i = 0; ; i++) {
1059 daddr_t fs_sblockloc;
1060
1061 if (bp != NULL) {
1062 brelse(bp, BC_NOCACHE);
1063 bp = NULL;
1064 }
1065 if (sblock_try[i] == -1) {
1066 DPRINTF("no superblock found");
1067 error = EINVAL;
1068 fs = NULL;
1069 goto out;
1070 }
1071
1072 error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
1073 cred, 0, &bp);
1074 if (error) {
1075 DPRINTF("bread@0x%x returned %d",
1076 sblock_try[i] / DEV_BSIZE, error);
1077 fs = NULL;
1078 goto out;
1079 }
1080 fs = (struct fs *)bp->b_data;
1081
1082 sblockloc = sblock_try[i];
1083 DPRINTF("fs_magic 0x%x", fs->fs_magic);
1084
1085 /*
1086 * Swap: here, we swap fs->fs_sbsize in order to get the correct
1087 * size to read the superblock. Once read, we swap the whole
1088 * superblock structure.
1089 */
1090 if (fs->fs_magic == FS_UFS1_MAGIC) {
1091 fs_sbsize = fs->fs_sbsize;
1092 fstype = UFS1;
1093 #ifdef FFS_EI
1094 needswap = 0;
1095 } else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) {
1096 fs_sbsize = bswap32(fs->fs_sbsize);
1097 fstype = UFS1;
1098 needswap = 1;
1099 #endif
1100 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
1101 fs_sbsize = fs->fs_sbsize;
1102 fstype = UFS2;
1103 #ifdef FFS_EI
1104 needswap = 0;
1105 } else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
1106 fs_sbsize = bswap32(fs->fs_sbsize);
1107 fstype = UFS2;
1108 needswap = 1;
1109 #endif
1110 } else
1111 continue;
1112
1113 /* fs->fs_sblockloc isn't defined for old filesystems */
1114 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
1115 if (sblockloc == SBLOCK_UFS2)
1116 /*
1117 * This is likely to be the first alternate
1118 * in a filesystem with 64k blocks.
1119 * Don't use it.
1120 */
1121 continue;
1122 fs_sblockloc = sblockloc;
1123 } else {
1124 fs_sblockloc = fs->fs_sblockloc;
1125 #ifdef FFS_EI
1126 if (needswap)
1127 fs_sblockloc = bswap64(fs_sblockloc);
1128 #endif
1129 }
1130
1131 /* Check we haven't found an alternate superblock */
1132 if (fs_sblockloc != sblockloc)
1133 continue;
1134
1135 /* Check the superblock size */
1136 if (fs_sbsize > SBLOCKSIZE || fs_sbsize < sizeof(struct fs))
1137 continue;
1138 fs = kmem_alloc((u_long)fs_sbsize, KM_SLEEP);
1139 memcpy(fs, bp->b_data, fs_sbsize);
1140
1141 /* Swap the whole superblock structure, if necessary. */
1142 #ifdef FFS_EI
1143 if (needswap) {
1144 ffs_sb_swap((struct fs*)bp->b_data, fs);
1145 fs->fs_flags |= FS_SWAPPED;
1146 } else
1147 #endif
1148 fs->fs_flags &= ~FS_SWAPPED;
1149
1150 /*
1151 * Now that everything is swapped, the superblock is ready to
1152 * be sanitized.
1153 */
1154 if (!ffs_superblock_validate(fs)) {
1155 kmem_free(fs, fs_sbsize);
1156 continue;
1157 }
1158
1159 /* Ok seems to be a good superblock */
1160 break;
1161 }
1162
1163 ump->um_fs = fs;
1164
1165 #ifdef WAPBL
1166 if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
1167 error = ffs_wapbl_replay_start(mp, fs, devvp);
1168 if (error && (mp->mnt_flag & MNT_FORCE) == 0) {
1169 DPRINTF("ffs_wapbl_replay_start returned %d", error);
1170 goto out;
1171 }
1172 if (!error) {
1173 if (!ronly) {
1174 /* XXX fsmnt may be stale. */
1175 printf("%s: replaying log to disk\n",
1176 fs->fs_fsmnt);
1177 error = wapbl_replay_write(mp->mnt_wapbl_replay,
1178 devvp);
1179 if (error) {
1180 DPRINTF("wapbl_replay_write returned %d",
1181 error);
1182 goto out;
1183 }
1184 wapbl_replay_stop(mp->mnt_wapbl_replay);
1185 fs->fs_clean = FS_WASCLEAN;
1186 } else {
1187 /* XXX fsmnt may be stale */
1188 printf("%s: replaying log to memory\n",
1189 fs->fs_fsmnt);
1190 }
1191
1192 /* Force a re-read of the superblock */
1193 brelse(bp, BC_INVAL);
1194 bp = NULL;
1195 kmem_free(fs, fs_sbsize);
1196 fs = NULL;
1197 goto sbagain;
1198 }
1199 }
1200 #else /* !WAPBL */
1201 if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
1202 error = EPERM;
1203 DPRINTF("no force %d", error);
1204 goto out;
1205 }
1206 #endif /* !WAPBL */
1207
1208 ffs_oldfscompat_read(fs, ump, sblockloc);
1209 ump->um_maxfilesize = fs->fs_maxfilesize;
1210
1211 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
1212 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
1213 mp->mnt_stat.f_mntonname, fs->fs_flags,
1214 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1215 if ((mp->mnt_flag & MNT_FORCE) == 0) {
1216 error = EINVAL;
1217 DPRINTF("no force %d", error);
1218 goto out;
1219 }
1220 }
1221
1222 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1223 fs->fs_pendingblocks = 0;
1224 fs->fs_pendinginodes = 0;
1225 }
1226
1227 ump->um_fstype = fstype;
1228 if (fs->fs_sbsize < SBLOCKSIZE)
1229 brelse(bp, BC_INVAL);
1230 else
1231 brelse(bp, 0);
1232 bp = NULL;
1233
1234 /*
1235 * First check to see if this is tagged as an Apple UFS filesystem
1236 * in the disklabel
1237 */
1238 if (getdiskinfo(devvp, &dkw) == 0 &&
1239 strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
1240 ump->um_flags |= UFS_ISAPPLEUFS;
1241 #ifdef APPLE_UFS
1242 else {
1243 /*
1244 * Manually look for an apple ufs label, and if a valid one
1245 * is found, then treat it like an Apple UFS filesystem anyway
1246 */
1247 error = bread(devvp,
1248 (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
1249 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
1250 if (error) {
1251 DPRINTF("apple bread@0x%jx returned %d",
1252 (intmax_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
1253 error);
1254 goto out;
1255 }
1256 error = ffs_appleufs_validate(fs->fs_fsmnt,
1257 (struct appleufslabel *)bp->b_data, NULL);
1258 if (error == 0)
1259 ump->um_flags |= UFS_ISAPPLEUFS;
1260 brelse(bp, 0);
1261 bp = NULL;
1262 }
1263 #else
1264 if (ump->um_flags & UFS_ISAPPLEUFS) {
1265 DPRINTF("AppleUFS not supported");
1266 error = EINVAL;
1267 goto out;
1268 }
1269 #endif
1270
1271 #if 0
1272 /*
1273 * XXX This code changes the behaviour of mounting dirty filesystems, to
1274 * XXX require "mount -f ..." to mount them. This doesn't match what
1275 * XXX mount(8) describes and is disabled for now.
1276 */
1277 /*
1278 * If the file system is not clean, don't allow it to be mounted
1279 * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
1280 * for the root file system.)
1281 */
1282 if (fs->fs_flags & FS_DOWAPBL) {
1283 /*
1284 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
1285 * bit is set, although there's a window in unmount where it
1286 * could be FS_ISCLEAN
1287 */
1288 if ((mp->mnt_flag & MNT_FORCE) == 0 &&
1289 (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
1290 error = EPERM;
1291 goto out;
1292 }
1293 } else
1294 if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
1295 (mp->mnt_flag & MNT_FORCE) == 0) {
1296 error = EPERM;
1297 goto out;
1298 }
1299 #endif
1300
1301 /*
1302 * Verify that we can access the last block in the fs
1303 * if we're mounting read/write.
1304 */
1305 if (!ronly) {
1306 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_size - 1),
1307 fs->fs_fsize, cred, 0, &bp);
1308 if (error) {
1309 DPRINTF("bread@0x%jx returned %d",
1310 (intmax_t)FFS_FSBTODB(fs, fs->fs_size - 1),
1311 error);
1312 bset = BC_INVAL;
1313 goto out;
1314 }
1315 if (bp->b_bcount != fs->fs_fsize) {
1316 DPRINTF("bcount %x != fsize %x", bp->b_bcount,
1317 fs->fs_fsize);
1318 error = EINVAL;
1319 }
1320 brelse(bp, BC_INVAL);
1321 bp = NULL;
1322 }
1323
1324 fs->fs_ronly = ronly;
1325 /* Don't bump fs_clean if we're replaying journal */
1326 if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN))) {
1327 if (ronly == 0) {
1328 fs->fs_clean <<= 1;
1329 fs->fs_fmod = 1;
1330 }
1331 }
1332
1333 bsize = fs->fs_cssize;
1334 blks = howmany(bsize, fs->fs_fsize);
1335 if (fs->fs_contigsumsize > 0)
1336 bsize += fs->fs_ncg * sizeof(int32_t);
1337 bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1338 allocsbsize = bsize;
1339 space = kmem_alloc((u_long)allocsbsize, KM_SLEEP);
1340 fs->fs_csp = space;
1341
1342 for (i = 0; i < blks; i += fs->fs_frag) {
1343 bsize = fs->fs_bsize;
1344 if (i + fs->fs_frag > blks)
1345 bsize = (blks - i) * fs->fs_fsize;
1346 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
1347 cred, 0, &bp);
1348 if (error) {
1349 DPRINTF("bread@0x%jx %d",
1350 (intmax_t)FFS_FSBTODB(fs, fs->fs_csaddr + i),
1351 error);
1352 goto out1;
1353 }
1354 #ifdef FFS_EI
1355 if (needswap)
1356 ffs_csum_swap((struct csum *)bp->b_data,
1357 (struct csum *)space, bsize);
1358 else
1359 #endif
1360 memcpy(space, bp->b_data, (u_int)bsize);
1361
1362 space = (char *)space + bsize;
1363 brelse(bp, 0);
1364 bp = NULL;
1365 }
1366 if (fs->fs_contigsumsize > 0) {
1367 fs->fs_maxcluster = lp = space;
1368 for (i = 0; i < fs->fs_ncg; i++)
1369 *lp++ = fs->fs_contigsumsize;
1370 space = lp;
1371 }
1372 bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1373 fs->fs_contigdirs = space;
1374 space = (char *)space + bsize;
1375 memset(fs->fs_contigdirs, 0, bsize);
1376
1377 /* Compatibility for old filesystems - XXX */
1378 if (fs->fs_avgfilesize <= 0)
1379 fs->fs_avgfilesize = AVFILESIZ;
1380 if (fs->fs_avgfpdir <= 0)
1381 fs->fs_avgfpdir = AFPDIR;
1382 fs->fs_active = NULL;
1383
1384 mp->mnt_data = ump;
1385 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1386 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
1387 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1388 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
1389 if (UFS_MPISAPPLEUFS(ump)) {
1390 /* NeXT used to keep short symlinks in the inode even
1391 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
1392 * is probably -1, but we still need to be able to identify
1393 * short symlinks.
1394 */
1395 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
1396 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
1397 mp->mnt_iflag |= IMNT_DTYPE;
1398 } else {
1399 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
1400 ump->um_dirblksiz = UFS_DIRBLKSIZ;
1401 if (ump->um_maxsymlinklen > 0)
1402 mp->mnt_iflag |= IMNT_DTYPE;
1403 else
1404 mp->mnt_iflag &= ~IMNT_DTYPE;
1405 }
1406 mp->mnt_fs_bshift = fs->fs_bshift;
1407 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
1408 mp->mnt_flag |= MNT_LOCAL;
1409 mp->mnt_iflag |= IMNT_MPSAFE;
1410 #ifdef FFS_EI
1411 if (needswap)
1412 ump->um_flags |= UFS_NEEDSWAP;
1413 #endif
1414 ump->um_mountp = mp;
1415 ump->um_dev = dev;
1416 ump->um_devvp = devvp;
1417 ump->um_nindir = fs->fs_nindir;
1418 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
1419 ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
1420 ump->um_seqinc = fs->fs_frag;
1421 for (i = 0; i < MAXQUOTAS; i++)
1422 ump->um_quotas[i] = NULLVP;
1423 spec_node_setmountedfs(devvp, mp);
1424 if (ronly == 0 && fs->fs_snapinum[0] != 0)
1425 ffs_snapshot_mount(mp);
1426 #ifdef WAPBL
1427 if (!ronly) {
1428 KDASSERT(fs->fs_ronly == 0);
1429 /*
1430 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
1431 * needs to create a new log file in-filesystem.
1432 */
1433 error = ffs_statvfs(mp, &mp->mnt_stat);
1434 if (error) {
1435 DPRINTF("ffs_statvfs returned %d", error);
1436 goto out1;
1437 }
1438
1439 error = ffs_wapbl_start(mp);
1440 if (error) {
1441 DPRINTF("ffs_wapbl_start returned %d", error);
1442 goto out1;
1443 }
1444 }
1445 #endif /* WAPBL */
1446 if (ronly == 0) {
1447 #ifdef QUOTA2
1448 error = ffs_quota2_mount(mp);
1449 if (error) {
1450 DPRINTF("ffs_quota2_mount returned %d", error);
1451 goto out1;
1452 }
1453 #else
1454 if (fs->fs_flags & FS_DOQUOTA2) {
1455 ump->um_flags |= UFS_QUOTA2;
1456 uprintf("%s: options QUOTA2 not enabled%s\n",
1457 mp->mnt_stat.f_mntonname,
1458 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1459 if ((mp->mnt_flag & MNT_FORCE) == 0) {
1460 error = EINVAL;
1461 DPRINTF("quota disabled %d", error);
1462 goto out1;
1463 }
1464 }
1465 #endif
1466 }
1467
1468 if (mp->mnt_flag & MNT_DISCARD)
1469 ump->um_discarddata = ffs_discard_init(devvp, fs);
1470
1471 return (0);
1472 out1:
1473 kmem_free(fs->fs_csp, allocsbsize);
1474 out:
1475 #ifdef WAPBL
1476 if (mp->mnt_wapbl_replay) {
1477 wapbl_replay_stop(mp->mnt_wapbl_replay);
1478 wapbl_replay_free(mp->mnt_wapbl_replay);
1479 mp->mnt_wapbl_replay = 0;
1480 }
1481 #endif
1482
1483 fstrans_unmount(mp);
1484 if (fs)
1485 kmem_free(fs, fs->fs_sbsize);
1486 spec_node_setmountedfs(devvp, NULL);
1487 if (bp)
1488 brelse(bp, bset);
1489 if (ump) {
1490 if (ump->um_oldfscompat)
1491 kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
1492 mutex_destroy(&ump->um_lock);
1493 kmem_free(ump, sizeof(*ump));
1494 mp->mnt_data = NULL;
1495 }
1496 return (error);
1497 }
1498
1499 /*
1500 * Sanity checks for loading old filesystem superblocks.
1501 * See ffs_oldfscompat_write below for unwound actions.
1502 *
1503 * XXX - Parts get retired eventually.
1504 * Unfortunately new bits get added.
1505 */
1506 static void
1507 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1508 {
1509 off_t maxfilesize;
1510 int32_t *extrasave;
1511
1512 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1513 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1514 return;
1515
1516 if (!ump->um_oldfscompat)
1517 ump->um_oldfscompat = kmem_alloc(512 + 3*sizeof(int32_t),
1518 KM_SLEEP);
1519
1520 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1521 extrasave = ump->um_oldfscompat;
1522 extrasave += 512/sizeof(int32_t);
1523 extrasave[0] = fs->fs_old_npsect;
1524 extrasave[1] = fs->fs_old_interleave;
1525 extrasave[2] = fs->fs_old_trackskew;
1526
1527 /* These fields will be overwritten by their
1528 * original values in fs_oldfscompat_write, so it is harmless
1529 * to modify them here.
1530 */
1531 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1532 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1533 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1534 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1535
1536 fs->fs_maxbsize = fs->fs_bsize;
1537 fs->fs_time = fs->fs_old_time;
1538 fs->fs_size = fs->fs_old_size;
1539 fs->fs_dsize = fs->fs_old_dsize;
1540 fs->fs_csaddr = fs->fs_old_csaddr;
1541 fs->fs_sblockloc = sblockloc;
1542
1543 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1544
1545 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1546 fs->fs_old_nrpos = 8;
1547 fs->fs_old_npsect = fs->fs_old_nsect;
1548 fs->fs_old_interleave = 1;
1549 fs->fs_old_trackskew = 0;
1550 }
1551
1552 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1553 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1554 fs->fs_qbmask = ~fs->fs_bmask;
1555 fs->fs_qfmask = ~fs->fs_fmask;
1556 }
1557
1558 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1559 if (fs->fs_maxfilesize > maxfilesize)
1560 fs->fs_maxfilesize = maxfilesize;
1561
1562 /* Compatibility for old filesystems */
1563 if (fs->fs_avgfilesize <= 0)
1564 fs->fs_avgfilesize = AVFILESIZ;
1565 if (fs->fs_avgfpdir <= 0)
1566 fs->fs_avgfpdir = AFPDIR;
1567
1568 #if 0
1569 if (bigcgs) {
1570 fs->fs_save_cgsize = fs->fs_cgsize;
1571 fs->fs_cgsize = fs->fs_bsize;
1572 }
1573 #endif
1574 }
1575
1576 /*
1577 * Unwinding superblock updates for old filesystems.
1578 * See ffs_oldfscompat_read above for details.
1579 *
1580 * XXX - Parts get retired eventually.
1581 * Unfortunately new bits get added.
1582 */
1583 static void
1584 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1585 {
1586 int32_t *extrasave;
1587
1588 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1589 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1590 return;
1591
1592 fs->fs_old_time = fs->fs_time;
1593 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1594 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1595 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1596 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1597 fs->fs_old_flags = fs->fs_flags;
1598
1599 #if 0
1600 if (bigcgs) {
1601 fs->fs_cgsize = fs->fs_save_cgsize;
1602 }
1603 #endif
1604
1605 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1606 extrasave = ump->um_oldfscompat;
1607 extrasave += 512/sizeof(int32_t);
1608 fs->fs_old_npsect = extrasave[0];
1609 fs->fs_old_interleave = extrasave[1];
1610 fs->fs_old_trackskew = extrasave[2];
1611
1612 }
1613
1614 /*
1615 * unmount vfs operation
1616 */
1617 int
1618 ffs_unmount(struct mount *mp, int mntflags)
1619 {
1620 struct lwp *l = curlwp;
1621 struct ufsmount *ump = VFSTOUFS(mp);
1622 struct fs *fs = ump->um_fs;
1623 int error, flags;
1624 u_int32_t bsize;
1625 #ifdef WAPBL
1626 extern int doforce;
1627 #endif
1628
1629 if (ump->um_discarddata) {
1630 ffs_discard_finish(ump->um_discarddata, mntflags);
1631 ump->um_discarddata = NULL;
1632 }
1633
1634 flags = 0;
1635 if (mntflags & MNT_FORCE)
1636 flags |= FORCECLOSE;
1637 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1638 return (error);
1639 error = UFS_WAPBL_BEGIN(mp);
1640 if (error == 0)
1641 if (fs->fs_ronly == 0 &&
1642 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1643 fs->fs_clean & FS_WASCLEAN) {
1644 fs->fs_clean = FS_ISCLEAN;
1645 fs->fs_fmod = 0;
1646 (void) ffs_sbupdate(ump, MNT_WAIT);
1647 }
1648 if (error == 0)
1649 UFS_WAPBL_END(mp);
1650 #ifdef WAPBL
1651 KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
1652 if (mp->mnt_wapbl_replay) {
1653 KDASSERT(fs->fs_ronly);
1654 wapbl_replay_stop(mp->mnt_wapbl_replay);
1655 wapbl_replay_free(mp->mnt_wapbl_replay);
1656 mp->mnt_wapbl_replay = 0;
1657 }
1658 error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
1659 if (error) {
1660 return error;
1661 }
1662 #endif /* WAPBL */
1663
1664 if (ump->um_devvp->v_type != VBAD)
1665 spec_node_setmountedfs(ump->um_devvp, NULL);
1666 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1667 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
1668 NOCRED);
1669 vput(ump->um_devvp);
1670
1671 bsize = fs->fs_cssize;
1672 if (fs->fs_contigsumsize > 0)
1673 bsize += fs->fs_ncg * sizeof(int32_t);
1674 bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1675 kmem_free(fs->fs_csp, bsize);
1676
1677 kmem_free(fs, fs->fs_sbsize);
1678 if (ump->um_oldfscompat != NULL)
1679 kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
1680 mutex_destroy(&ump->um_lock);
1681 ffs_snapshot_fini(ump);
1682 kmem_free(ump, sizeof(*ump));
1683 mp->mnt_data = NULL;
1684 mp->mnt_flag &= ~MNT_LOCAL;
1685 fstrans_unmount(mp);
1686 return (0);
1687 }
1688
1689 /*
1690 * Flush out all the files in a filesystem.
1691 */
1692 int
1693 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1694 {
1695 extern int doforce;
1696 struct ufsmount *ump;
1697 int error;
1698
1699 if (!doforce)
1700 flags &= ~FORCECLOSE;
1701 ump = VFSTOUFS(mp);
1702 #ifdef QUOTA
1703 if ((error = quota1_umount(mp, flags)) != 0)
1704 return (error);
1705 #endif
1706 #ifdef QUOTA2
1707 if ((error = quota2_umount(mp, flags)) != 0)
1708 return (error);
1709 #endif
1710 #ifdef UFS_EXTATTR
1711 if (ump->um_fstype == UFS1) {
1712 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
1713 ufs_extattr_stop(mp, l);
1714 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)
1715 ufs_extattr_uepm_destroy(&ump->um_extattr);
1716 mp->mnt_flag &= ~MNT_EXTATTR;
1717 }
1718 #endif
1719 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1720 return (error);
1721 ffs_snapshot_unmount(mp);
1722 /*
1723 * Flush all the files.
1724 */
1725 error = vflush(mp, NULLVP, flags);
1726 if (error)
1727 return (error);
1728 /*
1729 * Flush filesystem metadata.
1730 */
1731 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1732 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
1733 VOP_UNLOCK(ump->um_devvp);
1734 if (flags & FORCECLOSE) /* XXXDBJ */
1735 error = 0;
1736
1737 #ifdef WAPBL
1738 if (error)
1739 return error;
1740 if (mp->mnt_wapbl) {
1741 error = wapbl_flush(mp->mnt_wapbl, 1);
1742 if (flags & FORCECLOSE)
1743 error = 0;
1744 }
1745 #endif
1746
1747 return (error);
1748 }
1749
1750 /*
1751 * Get file system statistics.
1752 */
1753 int
1754 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
1755 {
1756 struct ufsmount *ump;
1757 struct fs *fs;
1758
1759 ump = VFSTOUFS(mp);
1760 fs = ump->um_fs;
1761 mutex_enter(&ump->um_lock);
1762 sbp->f_bsize = fs->fs_bsize;
1763 sbp->f_frsize = fs->fs_fsize;
1764 sbp->f_iosize = fs->fs_bsize;
1765 sbp->f_blocks = fs->fs_dsize;
1766 sbp->f_bfree = ffs_blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1767 fs->fs_cstotal.cs_nffree + FFS_DBTOFSB(fs, fs->fs_pendingblocks);
1768 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1769 fs->fs_minfree) / (u_int64_t) 100;
1770 if (sbp->f_bfree > sbp->f_bresvd)
1771 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1772 else
1773 sbp->f_bavail = 0;
1774 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1775 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1776 sbp->f_favail = sbp->f_ffree;
1777 sbp->f_fresvd = 0;
1778 mutex_exit(&ump->um_lock);
1779 copy_statvfs_info(sbp, mp);
1780
1781 return (0);
1782 }
1783
1784 struct ffs_sync_ctx {
1785 int waitfor;
1786 bool is_suspending;
1787 };
1788
1789 static bool
1790 ffs_sync_selector(void *cl, struct vnode *vp)
1791 {
1792 struct ffs_sync_ctx *c = cl;
1793 struct inode *ip;
1794
1795 ip = VTOI(vp);
1796 /*
1797 * Skip the vnode/inode if inaccessible.
1798 */
1799 if (ip == NULL || vp->v_type == VNON)
1800 return false;
1801
1802 /*
1803 * We deliberately update inode times here. This will
1804 * prevent a massive queue of updates accumulating, only
1805 * to be handled by a call to unmount.
1806 *
1807 * XXX It would be better to have the syncer trickle these
1808 * out. Adjustment needed to allow registering vnodes for
1809 * sync when the vnode is clean, but the inode dirty. Or
1810 * have ufs itself trickle out inode updates.
1811 *
1812 * If doing a lazy sync, we don't care about metadata or
1813 * data updates, because they are handled by each vnode's
1814 * synclist entry. In this case we are only interested in
1815 * writing back modified inodes.
1816 */
1817 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
1818 IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
1819 (c->waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
1820 UVM_OBJ_IS_CLEAN(&vp->v_uobj))))
1821 return false;
1822
1823 if (vp->v_type == VBLK && c->is_suspending)
1824 return false;
1825
1826 return true;
1827 }
1828
1829 /*
1830 * Go through the disk queues to initiate sandbagged IO;
1831 * go through the inodes to write those that have been modified;
1832 * initiate the writing of the super block if it has been modified.
1833 *
1834 * Note: we are always called with the filesystem marked `MPBUSY'.
1835 */
1836 int
1837 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1838 {
1839 struct vnode *vp;
1840 struct ufsmount *ump = VFSTOUFS(mp);
1841 struct fs *fs;
1842 struct vnode_iterator *marker;
1843 int error, allerror = 0;
1844 bool is_suspending;
1845 struct ffs_sync_ctx ctx;
1846
1847 fs = ump->um_fs;
1848 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1849 printf("fs = %s\n", fs->fs_fsmnt);
1850 panic("update: rofs mod");
1851 }
1852
1853 fstrans_start(mp, FSTRANS_SHARED);
1854 is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
1855 /*
1856 * Write back each (modified) inode.
1857 */
1858 vfs_vnode_iterator_init(mp, &marker);
1859
1860 ctx.waitfor = waitfor;
1861 ctx.is_suspending = is_suspending;
1862 while ((vp = vfs_vnode_iterator_next(marker, ffs_sync_selector, &ctx)))
1863 {
1864 error = vn_lock(vp, LK_EXCLUSIVE);
1865 if (error) {
1866 vrele(vp);
1867 continue;
1868 }
1869 if (waitfor == MNT_LAZY) {
1870 error = UFS_WAPBL_BEGIN(vp->v_mount);
1871 if (!error) {
1872 error = ffs_update(vp, NULL, NULL,
1873 UPDATE_CLOSE);
1874 UFS_WAPBL_END(vp->v_mount);
1875 }
1876 } else {
1877 error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
1878 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
1879 }
1880 if (error)
1881 allerror = error;
1882 vput(vp);
1883 }
1884 vfs_vnode_iterator_destroy(marker);
1885
1886 /*
1887 * Force stale file system control information to be flushed.
1888 */
1889 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1890 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1891 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1892 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1893 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
1894 0, 0)) != 0)
1895 allerror = error;
1896 VOP_UNLOCK(ump->um_devvp);
1897 }
1898 #if defined(QUOTA) || defined(QUOTA2)
1899 qsync(mp);
1900 #endif
1901 /*
1902 * Write back modified superblock.
1903 */
1904 if (fs->fs_fmod != 0) {
1905 fs->fs_fmod = 0;
1906 fs->fs_time = time_second;
1907 error = UFS_WAPBL_BEGIN(mp);
1908 if (error)
1909 allerror = error;
1910 else {
1911 if ((error = ffs_cgupdate(ump, waitfor)))
1912 allerror = error;
1913 UFS_WAPBL_END(mp);
1914 }
1915 }
1916
1917 #ifdef WAPBL
1918 if (mp->mnt_wapbl) {
1919 error = wapbl_flush(mp->mnt_wapbl, 0);
1920 if (error)
1921 allerror = error;
1922 }
1923 #endif
1924
1925 fstrans_done(mp);
1926 return (allerror);
1927 }
1928
1929 /*
1930 * Read an inode from disk and initialize this vnode / inode pair.
1931 * Caller assures no other thread will try to load this inode.
1932 */
1933 int
1934 ffs_loadvnode(struct mount *mp, struct vnode *vp,
1935 const void *key, size_t key_len, const void **new_key)
1936 {
1937 ino_t ino;
1938 struct fs *fs;
1939 struct inode *ip;
1940 struct ufsmount *ump;
1941 struct buf *bp;
1942 dev_t dev;
1943 int error;
1944
1945 KASSERT(key_len == sizeof(ino));
1946 memcpy(&ino, key, key_len);
1947 ump = VFSTOUFS(mp);
1948 dev = ump->um_dev;
1949 fs = ump->um_fs;
1950
1951 /* Read in the disk contents for the inode. */
1952 error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)),
1953 (int)fs->fs_bsize, NOCRED, 0, &bp);
1954 if (error)
1955 return error;
1956
1957 /* Allocate and initialize inode. */
1958 ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
1959 memset(ip, 0, sizeof(struct inode));
1960 vp->v_tag = VT_UFS;
1961 vp->v_op = ffs_vnodeop_p;
1962 vp->v_vflag |= VV_LOCKSWORK;
1963 vp->v_data = ip;
1964 ip->i_vnode = vp;
1965 ip->i_ump = ump;
1966 ip->i_fs = fs;
1967 ip->i_dev = dev;
1968 ip->i_number = ino;
1969 #if defined(QUOTA) || defined(QUOTA2)
1970 ufsquota_init(ip);
1971 #endif
1972
1973 /* Initialize genfs node. */
1974 genfs_node_init(vp, &ffs_genfsops);
1975
1976 if (ip->i_ump->um_fstype == UFS1)
1977 ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
1978 PR_WAITOK);
1979 else
1980 ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
1981 PR_WAITOK);
1982 ffs_load_inode(bp, ip, fs, ino);
1983 brelse(bp, 0);
1984
1985 /* Initialize the vnode from the inode. */
1986 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1987
1988 /* Finish inode initialization. */
1989 ip->i_devvp = ump->um_devvp;
1990 vref(ip->i_devvp);
1991
1992 /*
1993 * Ensure that uid and gid are correct. This is a temporary
1994 * fix until fsck has been changed to do the update.
1995 */
1996
1997 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1998 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1999 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
2000 } /* XXX */
2001 uvm_vnp_setsize(vp, ip->i_size);
2002 *new_key = &ip->i_number;
2003 return 0;
2004 }
2005
2006 /*
2007 * File handle to vnode
2008 *
2009 * Have to be really careful about stale file handles:
2010 * - check that the inode number is valid
2011 * - call ffs_vget() to get the locked inode
2012 * - check for an unallocated inode (i_mode == 0)
2013 * - check that the given client host has export rights and return
2014 * those rights via. exflagsp and credanonp
2015 */
2016 int
2017 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
2018 {
2019 struct ufid ufh;
2020 struct fs *fs;
2021
2022 if (fhp->fid_len != sizeof(struct ufid))
2023 return EINVAL;
2024
2025 memcpy(&ufh, fhp, sizeof(ufh));
2026 fs = VFSTOUFS(mp)->um_fs;
2027 if (ufh.ufid_ino < UFS_ROOTINO ||
2028 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
2029 return (ESTALE);
2030 return (ufs_fhtovp(mp, &ufh, vpp));
2031 }
2032
2033 /*
2034 * Vnode pointer to File handle
2035 */
2036 /* ARGSUSED */
2037 int
2038 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
2039 {
2040 struct inode *ip;
2041 struct ufid ufh;
2042
2043 if (*fh_size < sizeof(struct ufid)) {
2044 *fh_size = sizeof(struct ufid);
2045 return E2BIG;
2046 }
2047 ip = VTOI(vp);
2048 *fh_size = sizeof(struct ufid);
2049 memset(&ufh, 0, sizeof(ufh));
2050 ufh.ufid_len = sizeof(struct ufid);
2051 ufh.ufid_ino = ip->i_number;
2052 ufh.ufid_gen = ip->i_gen;
2053 memcpy(fhp, &ufh, sizeof(ufh));
2054 return (0);
2055 }
2056
2057 void
2058 ffs_init(void)
2059 {
2060 if (ffs_initcount++ > 0)
2061 return;
2062
2063 ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
2064 "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
2065 ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
2066 "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
2067 ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
2068 "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
2069 ufs_init();
2070 }
2071
2072 void
2073 ffs_reinit(void)
2074 {
2075 ufs_reinit();
2076 }
2077
2078 void
2079 ffs_done(void)
2080 {
2081 if (--ffs_initcount > 0)
2082 return;
2083
2084 ufs_done();
2085 pool_cache_destroy(ffs_dinode2_cache);
2086 pool_cache_destroy(ffs_dinode1_cache);
2087 pool_cache_destroy(ffs_inode_cache);
2088 }
2089
2090 /*
2091 * Write a superblock and associated information back to disk.
2092 */
2093 int
2094 ffs_sbupdate(struct ufsmount *mp, int waitfor)
2095 {
2096 struct fs *fs = mp->um_fs;
2097 struct buf *bp;
2098 int error = 0;
2099 u_int32_t saveflag;
2100
2101 error = ffs_getblk(mp->um_devvp,
2102 fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
2103 fs->fs_sbsize, false, &bp);
2104 if (error)
2105 return error;
2106 saveflag = fs->fs_flags & FS_INTERNAL;
2107 fs->fs_flags &= ~FS_INTERNAL;
2108
2109 memcpy(bp->b_data, fs, fs->fs_sbsize);
2110
2111 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
2112 #ifdef FFS_EI
2113 if (mp->um_flags & UFS_NEEDSWAP)
2114 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
2115 #endif
2116 fs->fs_flags |= saveflag;
2117
2118 if (waitfor == MNT_WAIT)
2119 error = bwrite(bp);
2120 else
2121 bawrite(bp);
2122 return (error);
2123 }
2124
2125 int
2126 ffs_cgupdate(struct ufsmount *mp, int waitfor)
2127 {
2128 struct fs *fs = mp->um_fs;
2129 struct buf *bp;
2130 int blks;
2131 void *space;
2132 int i, size, error = 0, allerror = 0;
2133
2134 allerror = ffs_sbupdate(mp, waitfor);
2135 blks = howmany(fs->fs_cssize, fs->fs_fsize);
2136 space = fs->fs_csp;
2137 for (i = 0; i < blks; i += fs->fs_frag) {
2138 size = fs->fs_bsize;
2139 if (i + fs->fs_frag > blks)
2140 size = (blks - i) * fs->fs_fsize;
2141 error = ffs_getblk(mp->um_devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i),
2142 FFS_NOBLK, size, false, &bp);
2143 if (error)
2144 break;
2145 #ifdef FFS_EI
2146 if (mp->um_flags & UFS_NEEDSWAP)
2147 ffs_csum_swap((struct csum*)space,
2148 (struct csum*)bp->b_data, size);
2149 else
2150 #endif
2151 memcpy(bp->b_data, space, (u_int)size);
2152 space = (char *)space + size;
2153 if (waitfor == MNT_WAIT)
2154 error = bwrite(bp);
2155 else
2156 bawrite(bp);
2157 }
2158 if (!allerror && error)
2159 allerror = error;
2160 return (allerror);
2161 }
2162
2163 int
2164 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2165 int attrnamespace, const char *attrname)
2166 {
2167 #ifdef UFS_EXTATTR
2168 /*
2169 * File-backed extended attributes are only supported on UFS1.
2170 * UFS2 has native extended attributes.
2171 */
2172 if (VFSTOUFS(mp)->um_fstype == UFS1)
2173 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
2174 #endif
2175 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
2176 }
2177
2178 int
2179 ffs_suspendctl(struct mount *mp, int cmd)
2180 {
2181 int error;
2182 struct lwp *l = curlwp;
2183
2184 switch (cmd) {
2185 case SUSPEND_SUSPEND:
2186 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
2187 return error;
2188 error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
2189 if (error == 0)
2190 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
2191 #ifdef WAPBL
2192 if (error == 0 && mp->mnt_wapbl)
2193 error = wapbl_flush(mp->mnt_wapbl, 1);
2194 #endif
2195 if (error != 0) {
2196 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
2197 return error;
2198 }
2199 return 0;
2200
2201 case SUSPEND_RESUME:
2202 return fstrans_setstate(mp, FSTRANS_NORMAL);
2203
2204 default:
2205 return EINVAL;
2206 }
2207 }
2208
2209 /*
2210 * Synch vnode for a mounted file system.
2211 */
2212 static int
2213 ffs_vfs_fsync(vnode_t *vp, int flags)
2214 {
2215 int error, i, pflags;
2216 #ifdef WAPBL
2217 struct mount *mp;
2218 #endif
2219
2220 KASSERT(vp->v_type == VBLK);
2221 KASSERT(spec_node_getmountedfs(vp) != NULL);
2222
2223 /*
2224 * Flush all dirty data associated with the vnode.
2225 */
2226 pflags = PGO_ALLPAGES | PGO_CLEANIT;
2227 if ((flags & FSYNC_WAIT) != 0)
2228 pflags |= PGO_SYNCIO;
2229 mutex_enter(vp->v_interlock);
2230 error = VOP_PUTPAGES(vp, 0, 0, pflags);
2231 if (error)
2232 return error;
2233
2234 #ifdef WAPBL
2235 mp = spec_node_getmountedfs(vp);
2236 if (mp && mp->mnt_wapbl) {
2237 /*
2238 * Don't bother writing out metadata if the syncer is
2239 * making the request. We will let the sync vnode
2240 * write it out in a single burst through a call to
2241 * VFS_SYNC().
2242 */
2243 if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
2244 return 0;
2245
2246 /*
2247 * Don't flush the log if the vnode being flushed
2248 * contains no dirty buffers that could be in the log.
2249 */
2250 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
2251 error = wapbl_flush(mp->mnt_wapbl, 0);
2252 if (error)
2253 return error;
2254 }
2255
2256 if ((flags & FSYNC_WAIT) != 0) {
2257 mutex_enter(vp->v_interlock);
2258 while (vp->v_numoutput)
2259 cv_wait(&vp->v_cv, vp->v_interlock);
2260 mutex_exit(vp->v_interlock);
2261 }
2262
2263 return 0;
2264 }
2265 #endif /* WAPBL */
2266
2267 error = vflushbuf(vp, flags);
2268 if (error == 0 && (flags & FSYNC_CACHE) != 0) {
2269 i = 1;
2270 (void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
2271 kauth_cred_get());
2272 }
2273
2274 return error;
2275 }
2276