ffs_vfsops.c revision 1.319 1 /* $NetBSD: ffs_vfsops.c,v 1.319 2015/02/23 13:38:54 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.319 2015/02/23 13:38:54 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 fs->fs_flags |= FS_SWAPPED;
732 } else
733 #endif
734 fs->fs_flags &= ~FS_SWAPPED;
735
736 /* We don't want the superblock size to change. */
737 if (newfs->fs_sbsize != fs_sbsize) {
738 brelse(bp, 0);
739 kmem_free(newfs, fs_sbsize);
740 return (EINVAL);
741 }
742 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
743 newfs->fs_magic != FS_UFS2_MAGIC)) {
744 brelse(bp, 0);
745 kmem_free(newfs, fs_sbsize);
746 return (EIO); /* XXX needs translation */
747 }
748 if (!ffs_superblock_validate(newfs)) {
749 brelse(bp, 0);
750 kmem_free(newfs, fs_sbsize);
751 return (EINVAL);
752 }
753
754 /* Store off old fs_sblockloc for fs_oldfscompat_read. */
755 sblockloc = fs->fs_sblockloc;
756 /*
757 * Copy pointer fields back into superblock before copying in XXX
758 * new superblock. These should really be in the ufsmount. XXX
759 * Note that important parameters (eg fs_ncg) are unchanged.
760 */
761 newfs->fs_csp = fs->fs_csp;
762 newfs->fs_maxcluster = fs->fs_maxcluster;
763 newfs->fs_contigdirs = fs->fs_contigdirs;
764 newfs->fs_ronly = fs->fs_ronly;
765 newfs->fs_active = fs->fs_active;
766 memcpy(fs, newfs, (u_int)fs_sbsize);
767 brelse(bp, 0);
768 kmem_free(newfs, fs_sbsize);
769
770 /* Recheck for apple UFS filesystem */
771 ump->um_flags &= ~UFS_ISAPPLEUFS;
772 /* First check to see if this is tagged as an Apple UFS filesystem
773 * in the disklabel
774 */
775 if (getdiskinfo(devvp, &dkw) == 0 &&
776 strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
777 ump->um_flags |= UFS_ISAPPLEUFS;
778 #ifdef APPLE_UFS
779 else {
780 /* Manually look for an apple ufs label, and if a valid one
781 * is found, then treat it like an Apple UFS filesystem anyway
782 *
783 * EINVAL is most probably a blocksize or alignment problem,
784 * it is unlikely that this is an Apple UFS filesystem then.
785 */
786 error = bread(devvp,
787 (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
788 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
789 if (error && error != EINVAL) {
790 return error;
791 }
792 if (error == 0) {
793 error = ffs_appleufs_validate(fs->fs_fsmnt,
794 (struct appleufslabel *)bp->b_data, NULL);
795 if (error == 0)
796 ump->um_flags |= UFS_ISAPPLEUFS;
797 brelse(bp, 0);
798 }
799 bp = NULL;
800 }
801 #else
802 if (ump->um_flags & UFS_ISAPPLEUFS)
803 return (EIO);
804 #endif
805
806 if (UFS_MPISAPPLEUFS(ump)) {
807 /* see comment about NeXT below */
808 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
809 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
810 mp->mnt_iflag |= IMNT_DTYPE;
811 } else {
812 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
813 ump->um_dirblksiz = UFS_DIRBLKSIZ;
814 if (ump->um_maxsymlinklen > 0)
815 mp->mnt_iflag |= IMNT_DTYPE;
816 else
817 mp->mnt_iflag &= ~IMNT_DTYPE;
818 }
819 ffs_oldfscompat_read(fs, ump, sblockloc);
820
821 mutex_enter(&ump->um_lock);
822 ump->um_maxfilesize = fs->fs_maxfilesize;
823 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
824 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
825 mp->mnt_stat.f_mntonname, fs->fs_flags,
826 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
827 if ((mp->mnt_flag & MNT_FORCE) == 0) {
828 mutex_exit(&ump->um_lock);
829 return (EINVAL);
830 }
831 }
832 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
833 fs->fs_pendingblocks = 0;
834 fs->fs_pendinginodes = 0;
835 }
836 mutex_exit(&ump->um_lock);
837
838 ffs_statvfs(mp, &mp->mnt_stat);
839 /*
840 * Step 3: re-read summary information from disk.
841 */
842 blks = howmany(fs->fs_cssize, fs->fs_fsize);
843 space = fs->fs_csp;
844 for (i = 0; i < blks; i += fs->fs_frag) {
845 bsize = fs->fs_bsize;
846 if (i + fs->fs_frag > blks)
847 bsize = (blks - i) * fs->fs_fsize;
848 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
849 NOCRED, 0, &bp);
850 if (error) {
851 return (error);
852 }
853 #ifdef FFS_EI
854 if (UFS_FSNEEDSWAP(fs))
855 ffs_csum_swap((struct csum *)bp->b_data,
856 (struct csum *)space, bsize);
857 else
858 #endif
859 memcpy(space, bp->b_data, (size_t)bsize);
860 space = (char *)space + bsize;
861 brelse(bp, 0);
862 }
863 /*
864 * We no longer know anything about clusters per cylinder group.
865 */
866 if (fs->fs_contigsumsize > 0) {
867 lp = fs->fs_maxcluster;
868 for (i = 0; i < fs->fs_ncg; i++)
869 *lp++ = fs->fs_contigsumsize;
870 }
871
872 vfs_vnode_iterator_init(mp, &marker);
873 while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
874 /*
875 * Step 4: invalidate all inactive vnodes.
876 */
877 if (vrecycle(vp))
878 continue;
879 /*
880 * Step 5: invalidate all cached file data.
881 */
882 if (vn_lock(vp, LK_EXCLUSIVE)) {
883 vrele(vp);
884 continue;
885 }
886 if (vinvalbuf(vp, 0, cred, l, 0, 0))
887 panic("ffs_reload: dirty2");
888 /*
889 * Step 6: re-read inode data for all active vnodes.
890 */
891 ip = VTOI(vp);
892 error = bread(devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
893 (int)fs->fs_bsize, NOCRED, 0, &bp);
894 if (error) {
895 vput(vp);
896 break;
897 }
898 ffs_load_inode(bp, ip, fs, ip->i_number);
899 brelse(bp, 0);
900 vput(vp);
901 }
902 vfs_vnode_iterator_destroy(marker);
903 return (error);
904 }
905
906 /*
907 * Possible superblock locations ordered from most to least likely.
908 */
909 static const int sblock_try[] = SBLOCKSEARCH;
910
911
912 static int
913 ffs_superblock_validate(struct fs *fs)
914 {
915 int32_t i, fs_bshift = 0, fs_fshift = 0, fs_fragshift = 0, fs_frag;
916
917 /* Check the superblock size */
918 if (fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < sizeof(struct fs))
919 return 0;
920
921 /* Check the file system blocksize */
922 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < MINBSIZE)
923 return 0;
924 if (!powerof2(fs->fs_bsize))
925 return 0;
926
927 /* Check the size of frag blocks */
928 if (!powerof2(fs->fs_fsize))
929 return 0;
930 if (fs->fs_fsize == 0)
931 return 0;
932
933 if (fs->fs_size == 0)
934 return 0;
935 if (fs->fs_cssize == 0)
936 return 0;
937
938 /* Block size cannot be smaller than fragment size */
939 if (fs->fs_bsize < fs->fs_fsize)
940 return 0;
941
942 /* Compute fs_bshift and ensure it is consistent */
943 for (i = fs->fs_bsize; i > 1; i >>= 1)
944 fs_bshift++;
945 if (fs->fs_bshift != fs_bshift)
946 return 0;
947
948 /* Compute fs_fshift and ensure it is consistent */
949 for (i = fs->fs_fsize; i > 1; i >>= 1)
950 fs_fshift++;
951 if (fs->fs_fshift != fs_fshift)
952 return 0;
953
954 /* Compute fs_fragshift and ensure it is consistent */
955 for (i = fs->fs_frag; i > 1; i >>= 1)
956 fs_fragshift++;
957 if (fs->fs_fragshift != fs_fragshift)
958 return 0;
959
960 /* Check the masks */
961 if (fs->fs_bmask != ~(fs->fs_bsize - 1))
962 return 0;
963 if (fs->fs_fmask != ~(fs->fs_fsize - 1))
964 return 0;
965
966 /*
967 * Now that the shifts and masks are sanitized, we can use the ffs_ API.
968 */
969
970 /* Check the number of frag blocks */
971 if ((fs_frag = ffs_numfrags(fs, fs->fs_bsize)) > MAXFRAG)
972 return 0;
973 if (fs->fs_frag != fs_frag)
974 return 0;
975
976 return 1;
977 }
978
979 /*
980 * Common code for mount and mountroot
981 */
982 int
983 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
984 {
985 struct ufsmount *ump = NULL;
986 struct buf *bp = NULL;
987 struct fs *fs = NULL;
988 dev_t dev;
989 struct dkwedge_info dkw;
990 void *space;
991 daddr_t sblockloc = 0;
992 int blks, fstype = 0;
993 int error, i, bsize, ronly, bset = 0;
994 #ifdef FFS_EI
995 int needswap = 0; /* keep gcc happy */
996 #endif
997 int32_t *lp;
998 kauth_cred_t cred;
999 u_int32_t allocsbsize, fs_sbsize = 0;
1000
1001 dev = devvp->v_rdev;
1002 cred = l ? l->l_cred : NOCRED;
1003
1004 /* Flush out any old buffers remaining from a previous use. */
1005 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1006 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
1007 VOP_UNLOCK(devvp);
1008 if (error) {
1009 DPRINTF("vinvalbuf returned %d", error);
1010 return error;
1011 }
1012
1013 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1014
1015 error = fstrans_mount(mp);
1016 if (error) {
1017 DPRINTF("fstrans_mount returned %d", error);
1018 return error;
1019 }
1020
1021 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
1022 mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
1023 error = ffs_snapshot_init(ump);
1024 if (error) {
1025 DPRINTF("ffs_snapshot_init returned %d", error);
1026 goto out;
1027 }
1028 ump->um_ops = &ffs_ufsops;
1029
1030 #ifdef WAPBL
1031 sbagain:
1032 #endif
1033 /*
1034 * Try reading the superblock in each of its possible locations.
1035 */
1036 for (i = 0; ; i++) {
1037 daddr_t fs_sblockloc;
1038
1039 if (bp != NULL) {
1040 brelse(bp, BC_NOCACHE);
1041 bp = NULL;
1042 }
1043 if (sblock_try[i] == -1) {
1044 DPRINTF("no superblock found");
1045 error = EINVAL;
1046 fs = NULL;
1047 goto out;
1048 }
1049
1050 error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
1051 cred, 0, &bp);
1052 if (error) {
1053 DPRINTF("bread@0x%x returned %d",
1054 sblock_try[i] / DEV_BSIZE, error);
1055 fs = NULL;
1056 goto out;
1057 }
1058 fs = (struct fs *)bp->b_data;
1059
1060 sblockloc = sblock_try[i];
1061 DPRINTF("fs_magic 0x%x", fs->fs_magic);
1062
1063 /*
1064 * Swap: here, we swap fs->fs_sbsize in order to get the correct
1065 * size to read the superblock. Once read, we swap the whole
1066 * superblock structure.
1067 */
1068 if (fs->fs_magic == FS_UFS1_MAGIC) {
1069 fs_sbsize = fs->fs_sbsize;
1070 fstype = UFS1;
1071 #ifdef FFS_EI
1072 needswap = 0;
1073 } else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) {
1074 fs_sbsize = bswap32(fs->fs_sbsize);
1075 fstype = UFS1;
1076 needswap = 1;
1077 #endif
1078 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
1079 fs_sbsize = fs->fs_sbsize;
1080 fstype = UFS2;
1081 #ifdef FFS_EI
1082 needswap = 0;
1083 } else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
1084 fs_sbsize = bswap32(fs->fs_sbsize);
1085 fstype = UFS2;
1086 needswap = 1;
1087 #endif
1088 } else
1089 continue;
1090
1091 /* fs->fs_sblockloc isn't defined for old filesystems */
1092 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
1093 if (sblockloc == SBLOCK_UFS2)
1094 /*
1095 * This is likely to be the first alternate
1096 * in a filesystem with 64k blocks.
1097 * Don't use it.
1098 */
1099 continue;
1100 fs_sblockloc = sblockloc;
1101 } else {
1102 fs_sblockloc = fs->fs_sblockloc;
1103 #ifdef FFS_EI
1104 if (needswap)
1105 fs_sblockloc = bswap64(fs_sblockloc);
1106 #endif
1107 }
1108
1109 /* Check we haven't found an alternate superblock */
1110 if (fs_sblockloc != sblockloc)
1111 continue;
1112
1113 /* Check the superblock size */
1114 if (fs_sbsize > SBLOCKSIZE || fs_sbsize < sizeof(struct fs))
1115 continue;
1116 fs = kmem_alloc((u_long)fs_sbsize, KM_SLEEP);
1117 memcpy(fs, bp->b_data, fs_sbsize);
1118
1119 /* Swap the whole superblock structure, if necessary. */
1120 #ifdef FFS_EI
1121 if (needswap) {
1122 ffs_sb_swap((struct fs*)bp->b_data, fs);
1123 fs->fs_flags |= FS_SWAPPED;
1124 } else
1125 #endif
1126 fs->fs_flags &= ~FS_SWAPPED;
1127
1128 /*
1129 * Now that everything is swapped, the superblock is ready to
1130 * be sanitized.
1131 */
1132 if (!ffs_superblock_validate(fs)) {
1133 kmem_free(fs, fs_sbsize);
1134 continue;
1135 }
1136
1137 /* Ok seems to be a good superblock */
1138 break;
1139 }
1140
1141 ump->um_fs = fs;
1142
1143 #ifdef WAPBL
1144 if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
1145 error = ffs_wapbl_replay_start(mp, fs, devvp);
1146 if (error && (mp->mnt_flag & MNT_FORCE) == 0) {
1147 DPRINTF("ffs_wapbl_replay_start returned %d", error);
1148 goto out;
1149 }
1150 if (!error) {
1151 if (!ronly) {
1152 /* XXX fsmnt may be stale. */
1153 printf("%s: replaying log to disk\n",
1154 fs->fs_fsmnt);
1155 error = wapbl_replay_write(mp->mnt_wapbl_replay,
1156 devvp);
1157 if (error) {
1158 DPRINTF("wapbl_replay_write returned %d",
1159 error);
1160 goto out;
1161 }
1162 wapbl_replay_stop(mp->mnt_wapbl_replay);
1163 fs->fs_clean = FS_WASCLEAN;
1164 } else {
1165 /* XXX fsmnt may be stale */
1166 printf("%s: replaying log to memory\n",
1167 fs->fs_fsmnt);
1168 }
1169
1170 /* Force a re-read of the superblock */
1171 brelse(bp, BC_INVAL);
1172 bp = NULL;
1173 kmem_free(fs, fs_sbsize);
1174 fs = NULL;
1175 goto sbagain;
1176 }
1177 }
1178 #else /* !WAPBL */
1179 if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
1180 error = EPERM;
1181 DPRINTF("no force %d", error);
1182 goto out;
1183 }
1184 #endif /* !WAPBL */
1185
1186 ffs_oldfscompat_read(fs, ump, sblockloc);
1187 ump->um_maxfilesize = fs->fs_maxfilesize;
1188
1189 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
1190 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
1191 mp->mnt_stat.f_mntonname, fs->fs_flags,
1192 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1193 if ((mp->mnt_flag & MNT_FORCE) == 0) {
1194 error = EINVAL;
1195 DPRINTF("no force %d", error);
1196 goto out;
1197 }
1198 }
1199
1200 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1201 fs->fs_pendingblocks = 0;
1202 fs->fs_pendinginodes = 0;
1203 }
1204
1205 ump->um_fstype = fstype;
1206 if (fs->fs_sbsize < SBLOCKSIZE)
1207 brelse(bp, BC_INVAL);
1208 else
1209 brelse(bp, 0);
1210 bp = NULL;
1211
1212 /*
1213 * First check to see if this is tagged as an Apple UFS filesystem
1214 * in the disklabel
1215 */
1216 if (getdiskinfo(devvp, &dkw) == 0 &&
1217 strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
1218 ump->um_flags |= UFS_ISAPPLEUFS;
1219 #ifdef APPLE_UFS
1220 else {
1221 /*
1222 * Manually look for an apple ufs label, and if a valid one
1223 * is found, then treat it like an Apple UFS filesystem anyway
1224 */
1225 error = bread(devvp,
1226 (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
1227 APPLEUFS_LABEL_SIZE, cred, 0, &bp);
1228 if (error) {
1229 DPRINTF("apple bread@0x%jx returned %d",
1230 (intmax_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
1231 error);
1232 goto out;
1233 }
1234 error = ffs_appleufs_validate(fs->fs_fsmnt,
1235 (struct appleufslabel *)bp->b_data, NULL);
1236 if (error == 0)
1237 ump->um_flags |= UFS_ISAPPLEUFS;
1238 brelse(bp, 0);
1239 bp = NULL;
1240 }
1241 #else
1242 if (ump->um_flags & UFS_ISAPPLEUFS) {
1243 DPRINTF("AppleUFS not supported");
1244 error = EINVAL;
1245 goto out;
1246 }
1247 #endif
1248
1249 #if 0
1250 /*
1251 * XXX This code changes the behaviour of mounting dirty filesystems, to
1252 * XXX require "mount -f ..." to mount them. This doesn't match what
1253 * XXX mount(8) describes and is disabled for now.
1254 */
1255 /*
1256 * If the file system is not clean, don't allow it to be mounted
1257 * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
1258 * for the root file system.)
1259 */
1260 if (fs->fs_flags & FS_DOWAPBL) {
1261 /*
1262 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
1263 * bit is set, although there's a window in unmount where it
1264 * could be FS_ISCLEAN
1265 */
1266 if ((mp->mnt_flag & MNT_FORCE) == 0 &&
1267 (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
1268 error = EPERM;
1269 goto out;
1270 }
1271 } else
1272 if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
1273 (mp->mnt_flag & MNT_FORCE) == 0) {
1274 error = EPERM;
1275 goto out;
1276 }
1277 #endif
1278
1279 /*
1280 * Verify that we can access the last block in the fs
1281 * if we're mounting read/write.
1282 */
1283 if (!ronly) {
1284 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_size - 1),
1285 fs->fs_fsize, cred, 0, &bp);
1286 if (error) {
1287 DPRINTF("bread@0x%jx returned %d",
1288 (intmax_t)FFS_FSBTODB(fs, fs->fs_size - 1),
1289 error);
1290 bset = BC_INVAL;
1291 goto out;
1292 }
1293 if (bp->b_bcount != fs->fs_fsize) {
1294 DPRINTF("bcount %x != fsize %x", bp->b_bcount,
1295 fs->fs_fsize);
1296 error = EINVAL;
1297 }
1298 brelse(bp, BC_INVAL);
1299 bp = NULL;
1300 }
1301
1302 fs->fs_ronly = ronly;
1303 /* Don't bump fs_clean if we're replaying journal */
1304 if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN))) {
1305 if (ronly == 0) {
1306 fs->fs_clean <<= 1;
1307 fs->fs_fmod = 1;
1308 }
1309 }
1310
1311 bsize = fs->fs_cssize;
1312 blks = howmany(bsize, fs->fs_fsize);
1313 if (fs->fs_contigsumsize > 0)
1314 bsize += fs->fs_ncg * sizeof(int32_t);
1315 bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1316 allocsbsize = bsize;
1317 space = kmem_alloc((u_long)allocsbsize, KM_SLEEP);
1318 fs->fs_csp = space;
1319
1320 for (i = 0; i < blks; i += fs->fs_frag) {
1321 bsize = fs->fs_bsize;
1322 if (i + fs->fs_frag > blks)
1323 bsize = (blks - i) * fs->fs_fsize;
1324 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
1325 cred, 0, &bp);
1326 if (error) {
1327 DPRINTF("bread@0x%jx %d",
1328 (intmax_t)FFS_FSBTODB(fs, fs->fs_csaddr + i),
1329 error);
1330 goto out1;
1331 }
1332 #ifdef FFS_EI
1333 if (needswap)
1334 ffs_csum_swap((struct csum *)bp->b_data,
1335 (struct csum *)space, bsize);
1336 else
1337 #endif
1338 memcpy(space, bp->b_data, (u_int)bsize);
1339
1340 space = (char *)space + bsize;
1341 brelse(bp, 0);
1342 bp = NULL;
1343 }
1344 if (fs->fs_contigsumsize > 0) {
1345 fs->fs_maxcluster = lp = space;
1346 for (i = 0; i < fs->fs_ncg; i++)
1347 *lp++ = fs->fs_contigsumsize;
1348 space = lp;
1349 }
1350 bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1351 fs->fs_contigdirs = space;
1352 space = (char *)space + bsize;
1353 memset(fs->fs_contigdirs, 0, bsize);
1354
1355 /* Compatibility for old filesystems - XXX */
1356 if (fs->fs_avgfilesize <= 0)
1357 fs->fs_avgfilesize = AVFILESIZ;
1358 if (fs->fs_avgfpdir <= 0)
1359 fs->fs_avgfpdir = AFPDIR;
1360 fs->fs_active = NULL;
1361
1362 mp->mnt_data = ump;
1363 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1364 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
1365 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1366 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
1367 if (UFS_MPISAPPLEUFS(ump)) {
1368 /* NeXT used to keep short symlinks in the inode even
1369 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
1370 * is probably -1, but we still need to be able to identify
1371 * short symlinks.
1372 */
1373 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
1374 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
1375 mp->mnt_iflag |= IMNT_DTYPE;
1376 } else {
1377 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
1378 ump->um_dirblksiz = UFS_DIRBLKSIZ;
1379 if (ump->um_maxsymlinklen > 0)
1380 mp->mnt_iflag |= IMNT_DTYPE;
1381 else
1382 mp->mnt_iflag &= ~IMNT_DTYPE;
1383 }
1384 mp->mnt_fs_bshift = fs->fs_bshift;
1385 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
1386 mp->mnt_flag |= MNT_LOCAL;
1387 mp->mnt_iflag |= IMNT_MPSAFE;
1388 #ifdef FFS_EI
1389 if (needswap)
1390 ump->um_flags |= UFS_NEEDSWAP;
1391 #endif
1392 ump->um_mountp = mp;
1393 ump->um_dev = dev;
1394 ump->um_devvp = devvp;
1395 ump->um_nindir = fs->fs_nindir;
1396 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
1397 ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
1398 ump->um_seqinc = fs->fs_frag;
1399 for (i = 0; i < MAXQUOTAS; i++)
1400 ump->um_quotas[i] = NULLVP;
1401 spec_node_setmountedfs(devvp, mp);
1402 if (ronly == 0 && fs->fs_snapinum[0] != 0)
1403 ffs_snapshot_mount(mp);
1404 #ifdef WAPBL
1405 if (!ronly) {
1406 KDASSERT(fs->fs_ronly == 0);
1407 /*
1408 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
1409 * needs to create a new log file in-filesystem.
1410 */
1411 error = ffs_statvfs(mp, &mp->mnt_stat);
1412 if (error) {
1413 DPRINTF("ffs_statvfs returned %d", error);
1414 goto out1;
1415 }
1416
1417 error = ffs_wapbl_start(mp);
1418 if (error) {
1419 DPRINTF("ffs_wapbl_start returned %d", error);
1420 goto out1;
1421 }
1422 }
1423 #endif /* WAPBL */
1424 if (ronly == 0) {
1425 #ifdef QUOTA2
1426 error = ffs_quota2_mount(mp);
1427 if (error) {
1428 DPRINTF("ffs_quota2_mount returned %d", error);
1429 goto out1;
1430 }
1431 #else
1432 if (fs->fs_flags & FS_DOQUOTA2) {
1433 ump->um_flags |= UFS_QUOTA2;
1434 uprintf("%s: options QUOTA2 not enabled%s\n",
1435 mp->mnt_stat.f_mntonname,
1436 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
1437 if ((mp->mnt_flag & MNT_FORCE) == 0) {
1438 error = EINVAL;
1439 DPRINTF("quota disabled %d", error);
1440 goto out1;
1441 }
1442 }
1443 #endif
1444 }
1445
1446 if (mp->mnt_flag & MNT_DISCARD)
1447 ump->um_discarddata = ffs_discard_init(devvp, fs);
1448
1449 return (0);
1450 out1:
1451 kmem_free(fs->fs_csp, allocsbsize);
1452 out:
1453 #ifdef WAPBL
1454 if (mp->mnt_wapbl_replay) {
1455 wapbl_replay_stop(mp->mnt_wapbl_replay);
1456 wapbl_replay_free(mp->mnt_wapbl_replay);
1457 mp->mnt_wapbl_replay = 0;
1458 }
1459 #endif
1460
1461 fstrans_unmount(mp);
1462 if (fs)
1463 kmem_free(fs, fs->fs_sbsize);
1464 spec_node_setmountedfs(devvp, NULL);
1465 if (bp)
1466 brelse(bp, bset);
1467 if (ump) {
1468 if (ump->um_oldfscompat)
1469 kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
1470 mutex_destroy(&ump->um_lock);
1471 kmem_free(ump, sizeof(*ump));
1472 mp->mnt_data = NULL;
1473 }
1474 return (error);
1475 }
1476
1477 /*
1478 * Sanity checks for loading old filesystem superblocks.
1479 * See ffs_oldfscompat_write below for unwound actions.
1480 *
1481 * XXX - Parts get retired eventually.
1482 * Unfortunately new bits get added.
1483 */
1484 static void
1485 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1486 {
1487 off_t maxfilesize;
1488 int32_t *extrasave;
1489
1490 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1491 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1492 return;
1493
1494 if (!ump->um_oldfscompat)
1495 ump->um_oldfscompat = kmem_alloc(512 + 3*sizeof(int32_t),
1496 KM_SLEEP);
1497
1498 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
1499 extrasave = ump->um_oldfscompat;
1500 extrasave += 512/sizeof(int32_t);
1501 extrasave[0] = fs->fs_old_npsect;
1502 extrasave[1] = fs->fs_old_interleave;
1503 extrasave[2] = fs->fs_old_trackskew;
1504
1505 /* These fields will be overwritten by their
1506 * original values in fs_oldfscompat_write, so it is harmless
1507 * to modify them here.
1508 */
1509 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1510 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1511 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1512 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1513
1514 fs->fs_maxbsize = fs->fs_bsize;
1515 fs->fs_time = fs->fs_old_time;
1516 fs->fs_size = fs->fs_old_size;
1517 fs->fs_dsize = fs->fs_old_dsize;
1518 fs->fs_csaddr = fs->fs_old_csaddr;
1519 fs->fs_sblockloc = sblockloc;
1520
1521 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1522
1523 if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
1524 fs->fs_old_nrpos = 8;
1525 fs->fs_old_npsect = fs->fs_old_nsect;
1526 fs->fs_old_interleave = 1;
1527 fs->fs_old_trackskew = 0;
1528 }
1529
1530 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1531 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
1532 fs->fs_qbmask = ~fs->fs_bmask;
1533 fs->fs_qfmask = ~fs->fs_fmask;
1534 }
1535
1536 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1537 if (fs->fs_maxfilesize > maxfilesize)
1538 fs->fs_maxfilesize = maxfilesize;
1539
1540 /* Compatibility for old filesystems */
1541 if (fs->fs_avgfilesize <= 0)
1542 fs->fs_avgfilesize = AVFILESIZ;
1543 if (fs->fs_avgfpdir <= 0)
1544 fs->fs_avgfpdir = AFPDIR;
1545
1546 #if 0
1547 if (bigcgs) {
1548 fs->fs_save_cgsize = fs->fs_cgsize;
1549 fs->fs_cgsize = fs->fs_bsize;
1550 }
1551 #endif
1552 }
1553
1554 /*
1555 * Unwinding superblock updates for old filesystems.
1556 * See ffs_oldfscompat_read above for details.
1557 *
1558 * XXX - Parts get retired eventually.
1559 * Unfortunately new bits get added.
1560 */
1561 static void
1562 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1563 {
1564 int32_t *extrasave;
1565
1566 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1567 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1568 return;
1569
1570 fs->fs_old_time = fs->fs_time;
1571 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1572 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1573 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1574 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1575 fs->fs_old_flags = fs->fs_flags;
1576
1577 #if 0
1578 if (bigcgs) {
1579 fs->fs_cgsize = fs->fs_save_cgsize;
1580 }
1581 #endif
1582
1583 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
1584 extrasave = ump->um_oldfscompat;
1585 extrasave += 512/sizeof(int32_t);
1586 fs->fs_old_npsect = extrasave[0];
1587 fs->fs_old_interleave = extrasave[1];
1588 fs->fs_old_trackskew = extrasave[2];
1589
1590 }
1591
1592 /*
1593 * unmount vfs operation
1594 */
1595 int
1596 ffs_unmount(struct mount *mp, int mntflags)
1597 {
1598 struct lwp *l = curlwp;
1599 struct ufsmount *ump = VFSTOUFS(mp);
1600 struct fs *fs = ump->um_fs;
1601 int error, flags;
1602 u_int32_t bsize;
1603 #ifdef WAPBL
1604 extern int doforce;
1605 #endif
1606
1607 if (ump->um_discarddata) {
1608 ffs_discard_finish(ump->um_discarddata, mntflags);
1609 ump->um_discarddata = NULL;
1610 }
1611
1612 flags = 0;
1613 if (mntflags & MNT_FORCE)
1614 flags |= FORCECLOSE;
1615 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1616 return (error);
1617 error = UFS_WAPBL_BEGIN(mp);
1618 if (error == 0)
1619 if (fs->fs_ronly == 0 &&
1620 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1621 fs->fs_clean & FS_WASCLEAN) {
1622 fs->fs_clean = FS_ISCLEAN;
1623 fs->fs_fmod = 0;
1624 (void) ffs_sbupdate(ump, MNT_WAIT);
1625 }
1626 if (error == 0)
1627 UFS_WAPBL_END(mp);
1628 #ifdef WAPBL
1629 KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
1630 if (mp->mnt_wapbl_replay) {
1631 KDASSERT(fs->fs_ronly);
1632 wapbl_replay_stop(mp->mnt_wapbl_replay);
1633 wapbl_replay_free(mp->mnt_wapbl_replay);
1634 mp->mnt_wapbl_replay = 0;
1635 }
1636 error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
1637 if (error) {
1638 return error;
1639 }
1640 #endif /* WAPBL */
1641
1642 if (ump->um_devvp->v_type != VBAD)
1643 spec_node_setmountedfs(ump->um_devvp, NULL);
1644 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1645 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
1646 NOCRED);
1647 vput(ump->um_devvp);
1648
1649 bsize = fs->fs_cssize;
1650 if (fs->fs_contigsumsize > 0)
1651 bsize += fs->fs_ncg * sizeof(int32_t);
1652 bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1653 kmem_free(fs->fs_csp, bsize);
1654
1655 kmem_free(fs, fs->fs_sbsize);
1656 if (ump->um_oldfscompat != NULL)
1657 kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
1658 mutex_destroy(&ump->um_lock);
1659 ffs_snapshot_fini(ump);
1660 kmem_free(ump, sizeof(*ump));
1661 mp->mnt_data = NULL;
1662 mp->mnt_flag &= ~MNT_LOCAL;
1663 fstrans_unmount(mp);
1664 return (0);
1665 }
1666
1667 /*
1668 * Flush out all the files in a filesystem.
1669 */
1670 int
1671 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1672 {
1673 extern int doforce;
1674 struct ufsmount *ump;
1675 int error;
1676
1677 if (!doforce)
1678 flags &= ~FORCECLOSE;
1679 ump = VFSTOUFS(mp);
1680 #ifdef QUOTA
1681 if ((error = quota1_umount(mp, flags)) != 0)
1682 return (error);
1683 #endif
1684 #ifdef QUOTA2
1685 if ((error = quota2_umount(mp, flags)) != 0)
1686 return (error);
1687 #endif
1688 #ifdef UFS_EXTATTR
1689 if (ump->um_fstype == UFS1) {
1690 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
1691 ufs_extattr_stop(mp, l);
1692 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)
1693 ufs_extattr_uepm_destroy(&ump->um_extattr);
1694 mp->mnt_flag &= ~MNT_EXTATTR;
1695 }
1696 #endif
1697 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1698 return (error);
1699 ffs_snapshot_unmount(mp);
1700 /*
1701 * Flush all the files.
1702 */
1703 error = vflush(mp, NULLVP, flags);
1704 if (error)
1705 return (error);
1706 /*
1707 * Flush filesystem metadata.
1708 */
1709 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1710 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
1711 VOP_UNLOCK(ump->um_devvp);
1712 if (flags & FORCECLOSE) /* XXXDBJ */
1713 error = 0;
1714
1715 #ifdef WAPBL
1716 if (error)
1717 return error;
1718 if (mp->mnt_wapbl) {
1719 error = wapbl_flush(mp->mnt_wapbl, 1);
1720 if (flags & FORCECLOSE)
1721 error = 0;
1722 }
1723 #endif
1724
1725 return (error);
1726 }
1727
1728 /*
1729 * Get file system statistics.
1730 */
1731 int
1732 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
1733 {
1734 struct ufsmount *ump;
1735 struct fs *fs;
1736
1737 ump = VFSTOUFS(mp);
1738 fs = ump->um_fs;
1739 mutex_enter(&ump->um_lock);
1740 sbp->f_bsize = fs->fs_bsize;
1741 sbp->f_frsize = fs->fs_fsize;
1742 sbp->f_iosize = fs->fs_bsize;
1743 sbp->f_blocks = fs->fs_dsize;
1744 sbp->f_bfree = ffs_blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1745 fs->fs_cstotal.cs_nffree + FFS_DBTOFSB(fs, fs->fs_pendingblocks);
1746 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1747 fs->fs_minfree) / (u_int64_t) 100;
1748 if (sbp->f_bfree > sbp->f_bresvd)
1749 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1750 else
1751 sbp->f_bavail = 0;
1752 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1753 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1754 sbp->f_favail = sbp->f_ffree;
1755 sbp->f_fresvd = 0;
1756 mutex_exit(&ump->um_lock);
1757 copy_statvfs_info(sbp, mp);
1758
1759 return (0);
1760 }
1761
1762 struct ffs_sync_ctx {
1763 int waitfor;
1764 bool is_suspending;
1765 };
1766
1767 static bool
1768 ffs_sync_selector(void *cl, struct vnode *vp)
1769 {
1770 struct ffs_sync_ctx *c = cl;
1771 struct inode *ip;
1772
1773 ip = VTOI(vp);
1774 /*
1775 * Skip the vnode/inode if inaccessible.
1776 */
1777 if (ip == NULL || vp->v_type == VNON)
1778 return false;
1779
1780 /*
1781 * We deliberately update inode times here. This will
1782 * prevent a massive queue of updates accumulating, only
1783 * to be handled by a call to unmount.
1784 *
1785 * XXX It would be better to have the syncer trickle these
1786 * out. Adjustment needed to allow registering vnodes for
1787 * sync when the vnode is clean, but the inode dirty. Or
1788 * have ufs itself trickle out inode updates.
1789 *
1790 * If doing a lazy sync, we don't care about metadata or
1791 * data updates, because they are handled by each vnode's
1792 * synclist entry. In this case we are only interested in
1793 * writing back modified inodes.
1794 */
1795 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
1796 IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
1797 (c->waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
1798 UVM_OBJ_IS_CLEAN(&vp->v_uobj))))
1799 return false;
1800
1801 if (vp->v_type == VBLK && c->is_suspending)
1802 return false;
1803
1804 return true;
1805 }
1806
1807 /*
1808 * Go through the disk queues to initiate sandbagged IO;
1809 * go through the inodes to write those that have been modified;
1810 * initiate the writing of the super block if it has been modified.
1811 *
1812 * Note: we are always called with the filesystem marked `MPBUSY'.
1813 */
1814 int
1815 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1816 {
1817 struct vnode *vp;
1818 struct ufsmount *ump = VFSTOUFS(mp);
1819 struct fs *fs;
1820 struct vnode_iterator *marker;
1821 int error, allerror = 0;
1822 bool is_suspending;
1823 struct ffs_sync_ctx ctx;
1824
1825 fs = ump->um_fs;
1826 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1827 printf("fs = %s\n", fs->fs_fsmnt);
1828 panic("update: rofs mod");
1829 }
1830
1831 fstrans_start(mp, FSTRANS_SHARED);
1832 is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
1833 /*
1834 * Write back each (modified) inode.
1835 */
1836 vfs_vnode_iterator_init(mp, &marker);
1837
1838 ctx.waitfor = waitfor;
1839 ctx.is_suspending = is_suspending;
1840 while ((vp = vfs_vnode_iterator_next(marker, ffs_sync_selector, &ctx)))
1841 {
1842 error = vn_lock(vp, LK_EXCLUSIVE);
1843 if (error) {
1844 vrele(vp);
1845 continue;
1846 }
1847 if (waitfor == MNT_LAZY) {
1848 error = UFS_WAPBL_BEGIN(vp->v_mount);
1849 if (!error) {
1850 error = ffs_update(vp, NULL, NULL,
1851 UPDATE_CLOSE);
1852 UFS_WAPBL_END(vp->v_mount);
1853 }
1854 } else {
1855 error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
1856 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
1857 }
1858 if (error)
1859 allerror = error;
1860 vput(vp);
1861 }
1862 vfs_vnode_iterator_destroy(marker);
1863
1864 /*
1865 * Force stale file system control information to be flushed.
1866 */
1867 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1868 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1869 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1870 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1871 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
1872 0, 0)) != 0)
1873 allerror = error;
1874 VOP_UNLOCK(ump->um_devvp);
1875 }
1876 #if defined(QUOTA) || defined(QUOTA2)
1877 qsync(mp);
1878 #endif
1879 /*
1880 * Write back modified superblock.
1881 */
1882 if (fs->fs_fmod != 0) {
1883 fs->fs_fmod = 0;
1884 fs->fs_time = time_second;
1885 error = UFS_WAPBL_BEGIN(mp);
1886 if (error)
1887 allerror = error;
1888 else {
1889 if ((error = ffs_cgupdate(ump, waitfor)))
1890 allerror = error;
1891 UFS_WAPBL_END(mp);
1892 }
1893 }
1894
1895 #ifdef WAPBL
1896 if (mp->mnt_wapbl) {
1897 error = wapbl_flush(mp->mnt_wapbl, 0);
1898 if (error)
1899 allerror = error;
1900 }
1901 #endif
1902
1903 fstrans_done(mp);
1904 return (allerror);
1905 }
1906
1907 /*
1908 * Read an inode from disk and initialize this vnode / inode pair.
1909 * Caller assures no other thread will try to load this inode.
1910 */
1911 int
1912 ffs_loadvnode(struct mount *mp, struct vnode *vp,
1913 const void *key, size_t key_len, const void **new_key)
1914 {
1915 ino_t ino;
1916 struct fs *fs;
1917 struct inode *ip;
1918 struct ufsmount *ump;
1919 struct buf *bp;
1920 dev_t dev;
1921 int error;
1922
1923 KASSERT(key_len == sizeof(ino));
1924 memcpy(&ino, key, key_len);
1925 ump = VFSTOUFS(mp);
1926 dev = ump->um_dev;
1927 fs = ump->um_fs;
1928
1929 /* Read in the disk contents for the inode. */
1930 error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)),
1931 (int)fs->fs_bsize, NOCRED, 0, &bp);
1932 if (error)
1933 return error;
1934
1935 /* Allocate and initialize inode. */
1936 ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
1937 memset(ip, 0, sizeof(struct inode));
1938 vp->v_tag = VT_UFS;
1939 vp->v_op = ffs_vnodeop_p;
1940 vp->v_vflag |= VV_LOCKSWORK;
1941 vp->v_data = ip;
1942 ip->i_vnode = vp;
1943 ip->i_ump = ump;
1944 ip->i_fs = fs;
1945 ip->i_dev = dev;
1946 ip->i_number = ino;
1947 #if defined(QUOTA) || defined(QUOTA2)
1948 ufsquota_init(ip);
1949 #endif
1950
1951 /* Initialize genfs node. */
1952 genfs_node_init(vp, &ffs_genfsops);
1953
1954 if (ip->i_ump->um_fstype == UFS1)
1955 ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
1956 PR_WAITOK);
1957 else
1958 ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
1959 PR_WAITOK);
1960 ffs_load_inode(bp, ip, fs, ino);
1961 brelse(bp, 0);
1962
1963 /* Initialize the vnode from the inode. */
1964 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1965
1966 /* Finish inode initialization. */
1967 ip->i_devvp = ump->um_devvp;
1968 vref(ip->i_devvp);
1969
1970 /*
1971 * Ensure that uid and gid are correct. This is a temporary
1972 * fix until fsck has been changed to do the update.
1973 */
1974
1975 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1976 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1977 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1978 } /* XXX */
1979 uvm_vnp_setsize(vp, ip->i_size);
1980 *new_key = &ip->i_number;
1981 return 0;
1982 }
1983
1984 /*
1985 * File handle to vnode
1986 *
1987 * Have to be really careful about stale file handles:
1988 * - check that the inode number is valid
1989 * - call ffs_vget() to get the locked inode
1990 * - check for an unallocated inode (i_mode == 0)
1991 * - check that the given client host has export rights and return
1992 * those rights via. exflagsp and credanonp
1993 */
1994 int
1995 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1996 {
1997 struct ufid ufh;
1998 struct fs *fs;
1999
2000 if (fhp->fid_len != sizeof(struct ufid))
2001 return EINVAL;
2002
2003 memcpy(&ufh, fhp, sizeof(ufh));
2004 fs = VFSTOUFS(mp)->um_fs;
2005 if (ufh.ufid_ino < UFS_ROOTINO ||
2006 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
2007 return (ESTALE);
2008 return (ufs_fhtovp(mp, &ufh, vpp));
2009 }
2010
2011 /*
2012 * Vnode pointer to File handle
2013 */
2014 /* ARGSUSED */
2015 int
2016 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
2017 {
2018 struct inode *ip;
2019 struct ufid ufh;
2020
2021 if (*fh_size < sizeof(struct ufid)) {
2022 *fh_size = sizeof(struct ufid);
2023 return E2BIG;
2024 }
2025 ip = VTOI(vp);
2026 *fh_size = sizeof(struct ufid);
2027 memset(&ufh, 0, sizeof(ufh));
2028 ufh.ufid_len = sizeof(struct ufid);
2029 ufh.ufid_ino = ip->i_number;
2030 ufh.ufid_gen = ip->i_gen;
2031 memcpy(fhp, &ufh, sizeof(ufh));
2032 return (0);
2033 }
2034
2035 void
2036 ffs_init(void)
2037 {
2038 if (ffs_initcount++ > 0)
2039 return;
2040
2041 ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
2042 "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
2043 ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
2044 "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
2045 ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
2046 "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
2047 ufs_init();
2048 }
2049
2050 void
2051 ffs_reinit(void)
2052 {
2053 ufs_reinit();
2054 }
2055
2056 void
2057 ffs_done(void)
2058 {
2059 if (--ffs_initcount > 0)
2060 return;
2061
2062 ufs_done();
2063 pool_cache_destroy(ffs_dinode2_cache);
2064 pool_cache_destroy(ffs_dinode1_cache);
2065 pool_cache_destroy(ffs_inode_cache);
2066 }
2067
2068 /*
2069 * Write a superblock and associated information back to disk.
2070 */
2071 int
2072 ffs_sbupdate(struct ufsmount *mp, int waitfor)
2073 {
2074 struct fs *fs = mp->um_fs;
2075 struct buf *bp;
2076 int error = 0;
2077 u_int32_t saveflag;
2078
2079 error = ffs_getblk(mp->um_devvp,
2080 fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
2081 fs->fs_sbsize, false, &bp);
2082 if (error)
2083 return error;
2084 saveflag = fs->fs_flags & FS_INTERNAL;
2085 fs->fs_flags &= ~FS_INTERNAL;
2086
2087 memcpy(bp->b_data, fs, fs->fs_sbsize);
2088
2089 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
2090 #ifdef FFS_EI
2091 if (mp->um_flags & UFS_NEEDSWAP)
2092 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
2093 #endif
2094 fs->fs_flags |= saveflag;
2095
2096 if (waitfor == MNT_WAIT)
2097 error = bwrite(bp);
2098 else
2099 bawrite(bp);
2100 return (error);
2101 }
2102
2103 int
2104 ffs_cgupdate(struct ufsmount *mp, int waitfor)
2105 {
2106 struct fs *fs = mp->um_fs;
2107 struct buf *bp;
2108 int blks;
2109 void *space;
2110 int i, size, error = 0, allerror = 0;
2111
2112 allerror = ffs_sbupdate(mp, waitfor);
2113 blks = howmany(fs->fs_cssize, fs->fs_fsize);
2114 space = fs->fs_csp;
2115 for (i = 0; i < blks; i += fs->fs_frag) {
2116 size = fs->fs_bsize;
2117 if (i + fs->fs_frag > blks)
2118 size = (blks - i) * fs->fs_fsize;
2119 error = ffs_getblk(mp->um_devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i),
2120 FFS_NOBLK, size, false, &bp);
2121 if (error)
2122 break;
2123 #ifdef FFS_EI
2124 if (mp->um_flags & UFS_NEEDSWAP)
2125 ffs_csum_swap((struct csum*)space,
2126 (struct csum*)bp->b_data, size);
2127 else
2128 #endif
2129 memcpy(bp->b_data, space, (u_int)size);
2130 space = (char *)space + size;
2131 if (waitfor == MNT_WAIT)
2132 error = bwrite(bp);
2133 else
2134 bawrite(bp);
2135 }
2136 if (!allerror && error)
2137 allerror = error;
2138 return (allerror);
2139 }
2140
2141 int
2142 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2143 int attrnamespace, const char *attrname)
2144 {
2145 #ifdef UFS_EXTATTR
2146 /*
2147 * File-backed extended attributes are only supported on UFS1.
2148 * UFS2 has native extended attributes.
2149 */
2150 if (VFSTOUFS(mp)->um_fstype == UFS1)
2151 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
2152 #endif
2153 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
2154 }
2155
2156 int
2157 ffs_suspendctl(struct mount *mp, int cmd)
2158 {
2159 int error;
2160 struct lwp *l = curlwp;
2161
2162 switch (cmd) {
2163 case SUSPEND_SUSPEND:
2164 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
2165 return error;
2166 error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
2167 if (error == 0)
2168 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
2169 #ifdef WAPBL
2170 if (error == 0 && mp->mnt_wapbl)
2171 error = wapbl_flush(mp->mnt_wapbl, 1);
2172 #endif
2173 if (error != 0) {
2174 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
2175 return error;
2176 }
2177 return 0;
2178
2179 case SUSPEND_RESUME:
2180 return fstrans_setstate(mp, FSTRANS_NORMAL);
2181
2182 default:
2183 return EINVAL;
2184 }
2185 }
2186
2187 /*
2188 * Synch vnode for a mounted file system.
2189 */
2190 static int
2191 ffs_vfs_fsync(vnode_t *vp, int flags)
2192 {
2193 int error, i, pflags;
2194 #ifdef WAPBL
2195 struct mount *mp;
2196 #endif
2197
2198 KASSERT(vp->v_type == VBLK);
2199 KASSERT(spec_node_getmountedfs(vp) != NULL);
2200
2201 /*
2202 * Flush all dirty data associated with the vnode.
2203 */
2204 pflags = PGO_ALLPAGES | PGO_CLEANIT;
2205 if ((flags & FSYNC_WAIT) != 0)
2206 pflags |= PGO_SYNCIO;
2207 mutex_enter(vp->v_interlock);
2208 error = VOP_PUTPAGES(vp, 0, 0, pflags);
2209 if (error)
2210 return error;
2211
2212 #ifdef WAPBL
2213 mp = spec_node_getmountedfs(vp);
2214 if (mp && mp->mnt_wapbl) {
2215 /*
2216 * Don't bother writing out metadata if the syncer is
2217 * making the request. We will let the sync vnode
2218 * write it out in a single burst through a call to
2219 * VFS_SYNC().
2220 */
2221 if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
2222 return 0;
2223
2224 /*
2225 * Don't flush the log if the vnode being flushed
2226 * contains no dirty buffers that could be in the log.
2227 */
2228 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
2229 error = wapbl_flush(mp->mnt_wapbl, 0);
2230 if (error)
2231 return error;
2232 }
2233
2234 if ((flags & FSYNC_WAIT) != 0) {
2235 mutex_enter(vp->v_interlock);
2236 while (vp->v_numoutput)
2237 cv_wait(&vp->v_cv, vp->v_interlock);
2238 mutex_exit(vp->v_interlock);
2239 }
2240
2241 return 0;
2242 }
2243 #endif /* WAPBL */
2244
2245 error = vflushbuf(vp, flags);
2246 if (error == 0 && (flags & FSYNC_CACHE) != 0) {
2247 i = 1;
2248 (void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
2249 kauth_cred_get());
2250 }
2251
2252 return error;
2253 }
2254