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