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