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