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