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