2 * Copyright (c) 1989, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <security/mac/mac_framework.h>
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/gjournal.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_extern.h>
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
69 #include <vm/vm_page.h>
71 #include <geom/geom.h>
72 #include <geom/geom_vfs.h>
76 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
78 static int ffs_reload(struct mount *, struct thread *);
79 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
80 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
82 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
83 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
84 static int ffs_sync_lazy(struct mount *mp);
86 static vfs_init_t ffs_init;
87 static vfs_uninit_t ffs_uninit;
88 static vfs_extattrctl_t ffs_extattrctl;
89 static vfs_cmount_t ffs_cmount;
90 static vfs_unmount_t ffs_unmount;
91 static vfs_mount_t ffs_mount;
92 static vfs_statfs_t ffs_statfs;
93 static vfs_fhtovp_t ffs_fhtovp;
94 static vfs_sync_t ffs_sync;
96 static struct vfsops ufs_vfsops = {
97 .vfs_extattrctl = ffs_extattrctl,
98 .vfs_fhtovp = ffs_fhtovp,
100 .vfs_mount = ffs_mount,
101 .vfs_cmount = ffs_cmount,
102 .vfs_quotactl = ufs_quotactl,
103 .vfs_root = ufs_root,
104 .vfs_statfs = ffs_statfs,
105 .vfs_sync = ffs_sync,
106 .vfs_uninit = ffs_uninit,
107 .vfs_unmount = ffs_unmount,
108 .vfs_vget = ffs_vget,
109 .vfs_susp_clean = process_deferred_inactive,
112 VFS_SET(ufs_vfsops, ufs, 0);
113 MODULE_VERSION(ufs, 1);
115 static b_strategy_t ffs_geom_strategy;
116 static b_write_t ffs_bufwrite;
118 static struct buf_ops ffs_ops = {
120 .bop_write = ffs_bufwrite,
121 .bop_strategy = ffs_geom_strategy,
123 #ifdef NO_FFS_SNAPSHOT
124 .bop_bdflush = bufbdflush,
126 .bop_bdflush = ffs_bdflush,
131 * Note that userquota and groupquota options are not currently used
132 * by UFS/FFS code and generally mount(8) does not pass those options
133 * from userland, but they can be passed by loader(8) via
134 * vfs.root.mountfrom.options.
136 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
137 "noclusterw", "noexec", "export", "force", "from", "groupquota",
138 "multilabel", "nfsv4acls", "snapshot", "nosuid", "suiddir", "nosymfollow",
139 "sync", "union", "userquota", NULL };
142 ffs_mount(struct mount *mp)
146 struct ufsmount *ump = 0;
149 u_int mntorflags, mntandnotflags;
151 struct nameidata ndp;
155 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
157 if (uma_inode == NULL) {
158 uma_inode = uma_zcreate("FFS inode",
159 sizeof(struct inode), NULL, NULL, NULL, NULL,
161 uma_ufs1 = uma_zcreate("FFS1 dinode",
162 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
164 uma_ufs2 = uma_zcreate("FFS2 dinode",
165 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
169 vfs_deleteopt(mp->mnt_optnew, "groupquota");
170 vfs_deleteopt(mp->mnt_optnew, "userquota");
172 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
178 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
179 mntorflags |= MNT_ACLS;
181 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
182 mntorflags |= MNT_SNAPSHOT;
184 * Once we have set the MNT_SNAPSHOT flag, do not
185 * persist "snapshot" in the options list.
187 vfs_deleteopt(mp->mnt_optnew, "snapshot");
188 vfs_deleteopt(mp->mnt_opt, "snapshot");
191 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
192 if (mntorflags & MNT_ACLS) {
194 "\"acls\" and \"nfsv4acls\" options "
195 "are mutually exclusive");
198 mntorflags |= MNT_NFS4ACLS;
202 mp->mnt_flag = (mp->mnt_flag | mntorflags) & ~mntandnotflags;
205 * If updating, check whether changing from read-only to
206 * read/write; if there is no device name, that's all we do.
208 if (mp->mnt_flag & MNT_UPDATE) {
211 devvp = ump->um_devvp;
212 if (fs->fs_ronly == 0 &&
213 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
215 * Flush any dirty data and suspend filesystem.
217 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
220 vn_finished_write(mp);
221 if ((error = vfs_write_suspend(mp)) != 0)
224 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
226 * Allow the secondary writes
229 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
231 wakeup(&mp->mnt_flag);
234 * Allow the curthread to
235 * ignore the suspension to
236 * synchronize on-disk state.
238 td->td_pflags |= TDP_IGNSUSP;
242 vn_start_write(NULL, &mp, V_WAIT);
245 * Check for and optionally get rid of files open
249 if (mp->mnt_flag & MNT_FORCE)
251 if (mp->mnt_flag & MNT_SOFTDEP) {
252 error = softdep_flushfiles(mp, flags, td);
254 error = ffs_flushfiles(mp, flags, td);
257 vfs_write_resume(mp);
260 if (fs->fs_pendingblocks != 0 ||
261 fs->fs_pendinginodes != 0) {
262 printf("WARNING: %s Update error: blocks %jd "
263 "files %d\n", fs->fs_fsmnt,
264 (intmax_t)fs->fs_pendingblocks,
265 fs->fs_pendinginodes);
266 fs->fs_pendingblocks = 0;
267 fs->fs_pendinginodes = 0;
269 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
271 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
274 vfs_write_resume(mp);
279 g_access(ump->um_cp, 0, -1, 0);
284 mp->mnt_flag |= MNT_RDONLY;
287 * Allow the writers to note that filesystem
290 vfs_write_resume(mp);
292 if ((mp->mnt_flag & MNT_RELOAD) &&
293 (error = ffs_reload(mp, td)) != 0)
296 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
298 * If upgrade to read-write by non-root, then verify
299 * that user has necessary permissions on the device.
301 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
302 error = VOP_ACCESS(devvp, VREAD | VWRITE,
305 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
307 VOP_UNLOCK(devvp, 0);
310 VOP_UNLOCK(devvp, 0);
311 fs->fs_flags &= ~FS_UNCLEAN;
312 if (fs->fs_clean == 0) {
313 fs->fs_flags |= FS_UNCLEAN;
314 if ((mp->mnt_flag & MNT_FORCE) ||
315 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
316 (fs->fs_flags & FS_DOSOFTDEP))) {
317 printf("WARNING: %s was not properly "
318 "dismounted\n", fs->fs_fsmnt);
321 "R/W mount of %s denied. %s.",
323 "Filesystem is not clean - run fsck");
330 * If we're the root device, we may not have an E count
333 if (ump->um_cp->ace == 0)
334 error = g_access(ump->um_cp, 0, 1, 1);
336 error = g_access(ump->um_cp, 0, 1, 0);
341 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
345 mp->mnt_flag &= ~MNT_RDONLY;
348 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
349 vn_finished_write(mp);
352 /* check to see if we need to start softdep */
353 if ((fs->fs_flags & FS_DOSOFTDEP) &&
354 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
355 vn_finished_write(mp);
358 if (fs->fs_snapinum[0] != 0)
359 ffs_snapshot_mount(mp);
360 vn_finished_write(mp);
363 * Soft updates is incompatible with "async",
364 * so if we are doing softupdates stop the user
365 * from setting the async flag in an update.
366 * Softdep_mount() clears it in an initial mount
369 if (mp->mnt_flag & MNT_SOFTDEP) {
370 /* XXX: Reset too late ? */
372 mp->mnt_flag &= ~MNT_ASYNC;
376 * Keep MNT_ACLS flag if it is stored in superblock.
378 if ((fs->fs_flags & FS_ACLS) != 0) {
379 /* XXX: Set too late ? */
381 mp->mnt_flag |= MNT_ACLS;
385 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
386 /* XXX: Set too late ? */
388 mp->mnt_flag |= MNT_NFS4ACLS;
393 * If this is a snapshot request, take the snapshot.
395 if (mp->mnt_flag & MNT_SNAPSHOT)
396 return (ffs_snapshot(mp, fspec));
400 * Not an update, or updating the name: look up the name
401 * and verify that it refers to a sensible disk device.
403 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
404 if ((error = namei(&ndp)) != 0)
406 NDFREE(&ndp, NDF_ONLY_PNBUF);
408 if (!vn_isdisk(devvp, &error)) {
414 * If mount by non-root, then verify that user has necessary
415 * permissions on the device.
418 if ((mp->mnt_flag & MNT_RDONLY) == 0)
420 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
422 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
428 if (mp->mnt_flag & MNT_UPDATE) {
432 * If it's not the same vnode, or at least the same device
433 * then it's not correct.
436 if (devvp->v_rdev != ump->um_devvp->v_rdev)
437 error = EINVAL; /* needs translation */
445 * We need the name for the mount point (also used for
446 * "last mounted on") copied in. If an error occurs,
447 * the mount point is discarded by the upper level code.
448 * Note that vfs_mount() populates f_mntonname for us.
450 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
455 vfs_mountedfrom(mp, fspec);
460 * Compatibility with old mount system call.
464 ffs_cmount(struct mntarg *ma, void *data, int flags)
466 struct ufs_args args;
467 struct export_args exp;
472 error = copyin(data, &args, sizeof args);
475 vfs_oexport_conv(&args.export, &exp);
477 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
478 ma = mount_arg(ma, "export", &exp, sizeof(exp));
479 error = kernel_mount(ma, flags);
485 * Reload all incore data for a filesystem (used after running fsck on
486 * the root filesystem and finding things to fix). The filesystem must
487 * be mounted read-only.
489 * Things to do to update the mount:
490 * 1) invalidate all cached meta-data.
491 * 2) re-read superblock from disk.
492 * 3) re-read summary information from disk.
493 * 4) invalidate all inactive vnodes.
494 * 5) invalidate all cached file data.
495 * 6) re-read inode data for all active vnodes.
498 ffs_reload(struct mount *mp, struct thread *td)
500 struct vnode *vp, *mvp, *devvp;
504 struct fs *fs, *newfs;
505 struct ufsmount *ump;
506 ufs2_daddr_t sblockloc;
507 int i, blks, size, error;
510 if ((mp->mnt_flag & MNT_RDONLY) == 0)
514 * Step 1: invalidate all cached meta-data.
516 devvp = VFSTOUFS(mp)->um_devvp;
517 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
518 if (vinvalbuf(devvp, 0, 0, 0) != 0)
519 panic("ffs_reload: dirty1");
520 VOP_UNLOCK(devvp, 0);
523 * Step 2: re-read superblock from disk.
525 fs = VFSTOUFS(mp)->um_fs;
526 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
529 newfs = (struct fs *)bp->b_data;
530 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
531 newfs->fs_magic != FS_UFS2_MAGIC) ||
532 newfs->fs_bsize > MAXBSIZE ||
533 newfs->fs_bsize < sizeof(struct fs)) {
535 return (EIO); /* XXX needs translation */
538 * Copy pointer fields back into superblock before copying in XXX
539 * new superblock. These should really be in the ufsmount. XXX
540 * Note that important parameters (eg fs_ncg) are unchanged.
542 newfs->fs_csp = fs->fs_csp;
543 newfs->fs_maxcluster = fs->fs_maxcluster;
544 newfs->fs_contigdirs = fs->fs_contigdirs;
545 newfs->fs_active = fs->fs_active;
546 /* The file system is still read-only. */
548 sblockloc = fs->fs_sblockloc;
549 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
551 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
552 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
554 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
555 printf("WARNING: %s: reload pending error: blocks %jd "
556 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
557 fs->fs_pendinginodes);
558 fs->fs_pendingblocks = 0;
559 fs->fs_pendinginodes = 0;
564 * Step 3: re-read summary information from disk.
566 blks = howmany(fs->fs_cssize, fs->fs_fsize);
568 for (i = 0; i < blks; i += fs->fs_frag) {
570 if (i + fs->fs_frag > blks)
571 size = (blks - i) * fs->fs_fsize;
572 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
576 bcopy(bp->b_data, space, (u_int)size);
577 space = (char *)space + size;
581 * We no longer know anything about clusters per cylinder group.
583 if (fs->fs_contigsumsize > 0) {
584 lp = fs->fs_maxcluster;
585 for (i = 0; i < fs->fs_ncg; i++)
586 *lp++ = fs->fs_contigsumsize;
591 MNT_VNODE_FOREACH(vp, mp, mvp) {
593 if (vp->v_iflag & VI_DOOMED) {
599 * Step 4: invalidate all cached file data.
601 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
602 MNT_VNODE_FOREACH_ABORT(mp, mvp);
605 if (vinvalbuf(vp, 0, 0, 0))
606 panic("ffs_reload: dirty2");
608 * Step 5: re-read inode data for all active vnodes.
612 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
613 (int)fs->fs_bsize, NOCRED, &bp);
617 MNT_VNODE_FOREACH_ABORT(mp, mvp);
620 ffs_load_inode(bp, ip, fs, ip->i_number);
621 ip->i_effnlink = ip->i_nlink;
632 * Possible superblock locations ordered from most to least likely.
634 static int sblock_try[] = SBLOCKSEARCH;
637 * Common code for mount and mountroot
640 ffs_mountfs(devvp, mp, td)
645 struct ufsmount *ump;
650 ufs2_daddr_t sblockloc;
651 int error, i, blks, size, ronly;
654 struct g_consumer *cp;
659 cred = td ? td->td_ucred : NOCRED;
660 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
666 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
669 * If we are a root mount, drop the E flag so fsck can do its magic.
670 * We will pick it up again when we remount R/W.
672 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
673 error = g_access(cp, 0, 0, -1);
676 VOP_UNLOCK(devvp, 0);
679 if (devvp->v_rdev->si_iosize_max != 0)
680 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
681 if (mp->mnt_iosize_max > MAXPHYS)
682 mp->mnt_iosize_max = MAXPHYS;
684 devvp->v_bufobj.bo_ops = &ffs_ops;
689 * Try reading the superblock in each of its possible locations.
691 for (i = 0; sblock_try[i] != -1; i++) {
692 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
695 "Invalid sectorsize %d for superblock size %d",
696 cp->provider->sectorsize, SBLOCKSIZE);
699 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
702 fs = (struct fs *)bp->b_data;
703 sblockloc = sblock_try[i];
704 if ((fs->fs_magic == FS_UFS1_MAGIC ||
705 (fs->fs_magic == FS_UFS2_MAGIC &&
706 (fs->fs_sblockloc == sblockloc ||
707 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
708 fs->fs_bsize <= MAXBSIZE &&
709 fs->fs_bsize >= sizeof(struct fs))
714 if (sblock_try[i] == -1) {
715 error = EINVAL; /* XXX needs translation */
719 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
720 fs->fs_flags &= ~FS_UNCLEAN;
721 if (fs->fs_clean == 0) {
722 fs->fs_flags |= FS_UNCLEAN;
723 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
724 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
725 (fs->fs_flags & FS_DOSOFTDEP))) {
727 "WARNING: %s was not properly dismounted\n",
730 vfs_mount_error(mp, "R/W mount of %s denied. %s.",
731 fs->fs_fsmnt, "Filesystem is not clean - run fsck");
735 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
736 (mp->mnt_flag & MNT_FORCE)) {
737 printf("WARNING: %s: lost blocks %jd files %d\n",
738 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
739 fs->fs_pendinginodes);
740 fs->fs_pendingblocks = 0;
741 fs->fs_pendinginodes = 0;
744 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
745 printf("WARNING: %s: mount pending error: blocks %jd "
746 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
747 fs->fs_pendinginodes);
748 fs->fs_pendingblocks = 0;
749 fs->fs_pendinginodes = 0;
751 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
754 * Get journal provider name.
757 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
758 if (g_io_getattr("GJOURNAL::provider", cp, &size,
759 mp->mnt_gjprovider) == 0) {
760 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
763 mp->mnt_flag |= MNT_GJOURNAL;
766 printf("WARNING: %s: GJOURNAL flag on fs "
767 "but no gjournal provider below\n",
768 mp->mnt_stat.f_mntonname);
769 free(mp->mnt_gjprovider, M_UFSMNT);
770 mp->mnt_gjprovider = NULL;
773 printf("WARNING: %s: GJOURNAL flag on fs but no "
774 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
777 mp->mnt_gjprovider = NULL;
779 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
781 ump->um_bo = &devvp->v_bufobj;
782 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
783 if (fs->fs_magic == FS_UFS1_MAGIC) {
784 ump->um_fstype = UFS1;
785 ump->um_balloc = ffs_balloc_ufs1;
787 ump->um_fstype = UFS2;
788 ump->um_balloc = ffs_balloc_ufs2;
790 ump->um_blkatoff = ffs_blkatoff;
791 ump->um_truncate = ffs_truncate;
792 ump->um_update = ffs_update;
793 ump->um_valloc = ffs_valloc;
794 ump->um_vfree = ffs_vfree;
795 ump->um_ifree = ffs_ifree;
796 ump->um_rdonly = ffs_rdonly;
797 ump->um_snapgone = ffs_snapgone;
798 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
799 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
800 if (fs->fs_sbsize < SBLOCKSIZE)
801 bp->b_flags |= B_INVAL | B_NOCACHE;
805 ffs_oldfscompat_read(fs, ump, sblockloc);
806 fs->fs_ronly = ronly;
807 size = fs->fs_cssize;
808 blks = howmany(size, fs->fs_fsize);
809 if (fs->fs_contigsumsize > 0)
810 size += fs->fs_ncg * sizeof(int32_t);
811 size += fs->fs_ncg * sizeof(u_int8_t);
812 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
814 for (i = 0; i < blks; i += fs->fs_frag) {
816 if (i + fs->fs_frag > blks)
817 size = (blks - i) * fs->fs_fsize;
818 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
820 free(fs->fs_csp, M_UFSMNT);
823 bcopy(bp->b_data, space, (u_int)size);
824 space = (char *)space + size;
828 if (fs->fs_contigsumsize > 0) {
829 fs->fs_maxcluster = lp = space;
830 for (i = 0; i < fs->fs_ncg; i++)
831 *lp++ = fs->fs_contigsumsize;
834 size = fs->fs_ncg * sizeof(u_int8_t);
835 fs->fs_contigdirs = (u_int8_t *)space;
836 bzero(fs->fs_contigdirs, size);
837 fs->fs_active = NULL;
839 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
840 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
842 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
843 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
848 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
850 mp->mnt_flag |= MNT_LOCAL;
852 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
855 mp->mnt_flag |= MNT_MULTILABEL;
858 printf("WARNING: %s: multilabel flag on fs but "
859 "no MAC support\n", mp->mnt_stat.f_mntonname);
862 if ((fs->fs_flags & FS_ACLS) != 0) {
866 if (mp->mnt_flag & MNT_NFS4ACLS)
867 printf("WARNING: %s: ACLs flag on fs conflicts with "
868 "\"nfsv4acls\" mount option; option ignored\n",
869 mp->mnt_stat.f_mntonname);
870 mp->mnt_flag &= ~MNT_NFS4ACLS;
871 mp->mnt_flag |= MNT_ACLS;
876 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
877 mp->mnt_stat.f_mntonname);
880 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
884 if (mp->mnt_flag & MNT_ACLS)
885 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
886 "with \"acls\" mount option; option ignored\n",
887 mp->mnt_stat.f_mntonname);
888 mp->mnt_flag &= ~MNT_ACLS;
889 mp->mnt_flag |= MNT_NFS4ACLS;
893 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
894 "ACLs support\n", mp->mnt_stat.f_mntonname);
897 if ((fs->fs_flags & FS_TRIM) != 0) {
899 if (g_io_getattr("GEOM::candelete", cp, &size,
900 &ump->um_candelete) == 0) {
901 if (!ump->um_candelete)
902 printf("WARNING: %s: TRIM flag on fs but disk "
903 "does not support TRIM\n",
904 mp->mnt_stat.f_mntonname);
906 printf("WARNING: %s: TRIM flag on fs but disk does "
907 "not confirm that it supports TRIM\n",
908 mp->mnt_stat.f_mntonname);
909 ump->um_candelete = 0;
915 ump->um_devvp = devvp;
916 ump->um_nindir = fs->fs_nindir;
917 ump->um_bptrtodb = fs->fs_fsbtodb;
918 ump->um_seqinc = fs->fs_frag;
919 for (i = 0; i < MAXQUOTAS; i++)
920 ump->um_quotas[i] = NULLVP;
922 ufs_extattr_uepm_init(&ump->um_extattr);
925 * Set FS local "last mounted on" information (NULL pad)
927 bzero(fs->fs_fsmnt, MAXMNTLEN);
928 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
930 if( mp->mnt_flag & MNT_ROOTFS) {
932 * Root mount; update timestamp in mount structure.
933 * this will be used by the common root mount code
934 * to update the system clock.
936 mp->mnt_time = fs->fs_time;
940 if ((fs->fs_flags & FS_DOSOFTDEP) &&
941 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
942 free(fs->fs_csp, M_UFSMNT);
945 if (fs->fs_snapinum[0] != 0)
946 ffs_snapshot_mount(mp);
949 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
952 * Initialize filesystem stat information in mount struct.
955 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
956 MNTK_EXTENDED_SHARED;
959 #ifdef UFS_EXTATTR_AUTOSTART
962 * Auto-starting does the following:
963 * - check for /.attribute in the fs, and extattr_start if so
964 * - for each file in .attribute, enable that file with
965 * an attribute of the same name.
966 * Not clear how to report errors -- probably eat them.
967 * This would all happen while the filesystem was busy/not
968 * available, so would effectively be "atomic".
970 mp->mnt_stat.f_iosize = fs->fs_bsize;
971 (void) ufs_extattr_autostart(mp, td);
972 #endif /* !UFS_EXTATTR_AUTOSTART */
973 #endif /* !UFS_EXTATTR */
986 mtx_destroy(UFS_MTX(ump));
987 if (mp->mnt_gjprovider != NULL) {
988 free(mp->mnt_gjprovider, M_UFSMNT);
989 mp->mnt_gjprovider = NULL;
991 free(ump->um_fs, M_UFSMNT);
999 #include <sys/sysctl.h>
1000 static int bigcgs = 0;
1001 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1004 * Sanity checks for loading old filesystem superblocks.
1005 * See ffs_oldfscompat_write below for unwound actions.
1007 * XXX - Parts get retired eventually.
1008 * Unfortunately new bits get added.
1011 ffs_oldfscompat_read(fs, ump, sblockloc)
1013 struct ufsmount *ump;
1014 ufs2_daddr_t sblockloc;
1019 * If not yet done, update fs_flags location and value of fs_sblockloc.
1021 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1022 fs->fs_flags = fs->fs_old_flags;
1023 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1024 fs->fs_sblockloc = sblockloc;
1027 * If not yet done, update UFS1 superblock with new wider fields.
1029 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1030 fs->fs_maxbsize = fs->fs_bsize;
1031 fs->fs_time = fs->fs_old_time;
1032 fs->fs_size = fs->fs_old_size;
1033 fs->fs_dsize = fs->fs_old_dsize;
1034 fs->fs_csaddr = fs->fs_old_csaddr;
1035 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1036 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1037 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1038 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1040 if (fs->fs_magic == FS_UFS1_MAGIC &&
1041 fs->fs_old_inodefmt < FS_44INODEFMT) {
1042 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1043 fs->fs_qbmask = ~fs->fs_bmask;
1044 fs->fs_qfmask = ~fs->fs_fmask;
1046 if (fs->fs_magic == FS_UFS1_MAGIC) {
1047 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1048 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1049 if (fs->fs_maxfilesize > maxfilesize)
1050 fs->fs_maxfilesize = maxfilesize;
1052 /* Compatibility for old filesystems */
1053 if (fs->fs_avgfilesize <= 0)
1054 fs->fs_avgfilesize = AVFILESIZ;
1055 if (fs->fs_avgfpdir <= 0)
1056 fs->fs_avgfpdir = AFPDIR;
1058 fs->fs_save_cgsize = fs->fs_cgsize;
1059 fs->fs_cgsize = fs->fs_bsize;
1064 * Unwinding superblock updates for old filesystems.
1065 * See ffs_oldfscompat_read above for details.
1067 * XXX - Parts get retired eventually.
1068 * Unfortunately new bits get added.
1071 ffs_oldfscompat_write(fs, ump)
1073 struct ufsmount *ump;
1077 * Copy back UFS2 updated fields that UFS1 inspects.
1079 if (fs->fs_magic == FS_UFS1_MAGIC) {
1080 fs->fs_old_time = fs->fs_time;
1081 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1082 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1083 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1084 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1085 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1088 fs->fs_cgsize = fs->fs_save_cgsize;
1089 fs->fs_save_cgsize = 0;
1094 * unmount system call
1097 ffs_unmount(mp, mntflags)
1102 struct ufsmount *ump = VFSTOUFS(mp);
1104 int error, flags, susp;
1113 if (mntflags & MNT_FORCE) {
1114 flags |= FORCECLOSE;
1115 susp = fs->fs_ronly != 0;
1118 if ((error = ufs_extattr_stop(mp, td))) {
1119 if (error != EOPNOTSUPP)
1120 printf("WARNING: unmount %s: ufs_extattr_stop "
1121 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1125 ufs_extattr_uepm_destroy(&ump->um_extattr);
1131 * dounmount already called vn_start_write().
1134 vn_finished_write(mp);
1135 if ((error = vfs_write_suspend(mp)) != 0)
1138 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1139 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1141 wakeup(&mp->mnt_flag);
1143 td->td_pflags |= TDP_IGNSUSP;
1147 vn_start_write(NULL, &mp, V_WAIT);
1150 if (mp->mnt_flag & MNT_SOFTDEP)
1151 error = softdep_flushfiles(mp, flags, td);
1153 error = ffs_flushfiles(mp, flags, td);
1154 if (error != 0 && error != ENXIO)
1158 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1159 printf("WARNING: unmount %s: pending error: blocks %jd "
1160 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1161 fs->fs_pendinginodes);
1162 fs->fs_pendingblocks = 0;
1163 fs->fs_pendinginodes = 0;
1166 if (fs->fs_ronly == 0) {
1167 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1168 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1169 if (error && error != ENXIO) {
1175 vfs_write_resume(mp);
1176 vn_start_write(NULL, &mp, V_WAIT);
1180 g_vfs_close(ump->um_cp);
1181 g_topology_unlock();
1183 vrele(ump->um_devvp);
1184 dev_rel(ump->um_dev);
1185 mtx_destroy(UFS_MTX(ump));
1186 if (mp->mnt_gjprovider != NULL) {
1187 free(mp->mnt_gjprovider, M_UFSMNT);
1188 mp->mnt_gjprovider = NULL;
1190 free(fs->fs_csp, M_UFSMNT);
1192 free(ump, M_UFSMNT);
1193 mp->mnt_data = NULL;
1195 mp->mnt_flag &= ~MNT_LOCAL;
1201 vfs_write_resume(mp);
1202 vn_start_write(NULL, &mp, V_WAIT);
1206 ufs_extattr_uepm_init(&ump->um_extattr);
1207 #ifdef UFS_EXTATTR_AUTOSTART
1208 (void) ufs_extattr_autostart(mp, td);
1217 * Flush out all the files in a filesystem.
1220 ffs_flushfiles(mp, flags, td)
1225 struct ufsmount *ump;
1230 if (mp->mnt_flag & MNT_QUOTA) {
1232 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1235 for (i = 0; i < MAXQUOTAS; i++) {
1236 quotaoff(td, mp, i);
1239 * Here we fall through to vflush again to ensure
1240 * that we have gotten rid of all the system vnodes.
1244 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1245 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1246 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1248 ffs_snapshot_unmount(mp);
1249 flags |= FORCECLOSE;
1251 * Here we fall through to vflush again to ensure
1252 * that we have gotten rid of all the system vnodes.
1256 * Flush all the files.
1258 if ((error = vflush(mp, 0, flags, td)) != 0)
1261 * Flush filesystem metadata.
1263 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1264 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1265 VOP_UNLOCK(ump->um_devvp, 0);
1270 * Get filesystem statistics.
1277 struct ufsmount *ump;
1282 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1283 panic("ffs_statfs");
1284 sbp->f_version = STATFS_VERSION;
1285 sbp->f_bsize = fs->fs_fsize;
1286 sbp->f_iosize = fs->fs_bsize;
1287 sbp->f_blocks = fs->fs_dsize;
1289 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1290 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1291 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1292 dbtofsb(fs, fs->fs_pendingblocks);
1293 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1294 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1296 sbp->f_namemax = NAME_MAX;
1301 * For a lazy sync, we only care about access times, quotas and the
1302 * superblock. Other filesystem changes are already converted to
1303 * cylinder group blocks or inode blocks updates and are written to
1310 struct vnode *mvp, *vp;
1313 int allerror, error;
1317 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1320 MNT_VNODE_FOREACH(vp, mp, mvp) {
1322 if (vp->v_iflag & VI_DOOMED || vp->v_type == VNON) {
1329 * The IN_ACCESS flag is converted to IN_MODIFIED by
1330 * ufs_close() and ufs_getattr() by the calls to
1331 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1332 * Test also all the other timestamp flags too, to pick up
1333 * any other cases that could be missed.
1335 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1341 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1346 error = ffs_update(vp, 0);
1359 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1360 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1366 * Go through the disk queues to initiate sandbagged IO;
1367 * go through the inodes to write those that have been modified;
1368 * initiate the writing of the super block if it has been modified.
1370 * Note: we are always called with the filesystem marked busy using
1374 ffs_sync(mp, waitfor)
1378 struct vnode *mvp, *vp, *devvp;
1381 struct ufsmount *ump = VFSTOUFS(mp);
1383 int error, count, wait, lockreq, allerror = 0;
1386 int secondary_writes;
1387 int secondary_accwrites;
1389 int softdep_accdeps;
1397 if (fs->fs_fmod != 0 && fs->fs_ronly != 0)
1398 panic("%s: ffs_sync: modification on read-only filesystem",
1400 if (waitfor == MNT_LAZY)
1401 return (ffs_sync_lazy(mp));
1404 * Write back each (modified) inode.
1406 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1407 if (waitfor == MNT_SUSPEND) {
1411 if (waitfor == MNT_WAIT) {
1413 lockreq = LK_EXCLUSIVE;
1415 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1418 /* Grab snapshot of secondary write counts */
1419 secondary_writes = mp->mnt_secondary_writes;
1420 secondary_accwrites = mp->mnt_secondary_accwrites;
1422 /* Grab snapshot of softdep dependency counts */
1424 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1427 MNT_VNODE_FOREACH(vp, mp, mvp) {
1429 * Depend on the vnode interlock to keep things stable enough
1430 * for a quick test. Since there might be hundreds of
1431 * thousands of vnodes, we cannot afford even a subroutine
1432 * call unless there's a good chance that we have work to do.
1435 if (vp->v_iflag & VI_DOOMED) {
1440 if (vp->v_type == VNON || ((ip->i_flag &
1441 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1442 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1447 if ((error = vget(vp, lockreq, td)) != 0) {
1449 if (error == ENOENT || error == ENOLCK) {
1450 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1455 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1462 * Force stale filesystem control information to be flushed.
1464 if (waitfor == MNT_WAIT) {
1465 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1467 /* Flushed work items may create new vnodes to clean */
1468 if (allerror == 0 && count) {
1477 devvp = ump->um_devvp;
1478 bo = &devvp->v_bufobj;
1480 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1482 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1483 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1485 VOP_UNLOCK(devvp, 0);
1486 if (allerror == 0 && waitfor == MNT_WAIT) {
1490 } else if (suspend != 0) {
1491 if (softdep_check_suspend(mp,
1496 secondary_accwrites) != 0)
1497 goto loop; /* More work needed */
1498 mtx_assert(MNT_MTX(mp), MA_OWNED);
1499 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1505 * Write back modified superblock.
1507 if (fs->fs_fmod != 0 &&
1508 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1514 ffs_vget(mp, ino, flags, vpp)
1520 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1524 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1533 struct ufsmount *ump;
1539 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1540 if (error || *vpp != NULL)
1544 * We must promote to an exclusive lock for vnode creation. This
1545 * can happen if lookup is passed LOCKSHARED.
1547 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1548 flags &= ~LK_TYPE_MASK;
1549 flags |= LK_EXCLUSIVE;
1553 * We do not lock vnode creation as it is believed to be too
1554 * expensive for such rare case as simultaneous creation of vnode
1555 * for same ino by different processes. We just allow them to race
1556 * and check later to decide who wins. Let the race begin!
1564 * If this malloc() is performed after the getnewvnode()
1565 * it might block, leaving a vnode with a NULL v_data to be
1566 * found by ffs_sync() if a sync happens to fire right then,
1567 * which will cause a panic because ffs_sync() blindly
1568 * dereferences vp->v_data (as well it should).
1570 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1572 /* Allocate a new vnode/inode. */
1573 if (fs->fs_magic == FS_UFS1_MAGIC)
1574 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1576 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1579 uma_zfree(uma_inode, ip);
1583 * FFS supports recursive locking.
1587 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1597 for (i = 0; i < MAXQUOTAS; i++)
1598 ip->i_dquot[i] = NODQUOT;
1602 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1603 if (ffs_flags & FFSV_FORCEINSMQ)
1604 vp->v_vflag |= VV_FORCEINSMQ;
1605 error = insmntque(vp, mp);
1607 uma_zfree(uma_inode, ip);
1611 vp->v_vflag &= ~VV_FORCEINSMQ;
1612 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1613 if (error || *vpp != NULL)
1616 /* Read in the disk contents for the inode, copy into the inode. */
1617 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1618 (int)fs->fs_bsize, NOCRED, &bp);
1621 * The inode does not contain anything useful, so it would
1622 * be misleading to leave it on its hash chain. With mode
1623 * still zero, it will be unlinked and returned to the free
1631 if (ip->i_ump->um_fstype == UFS1)
1632 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1634 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1635 ffs_load_inode(bp, ip, fs, ino);
1636 if (DOINGSOFTDEP(vp))
1637 softdep_load_inodeblock(ip);
1639 ip->i_effnlink = ip->i_nlink;
1643 * Initialize the vnode from the inode, check for aliases.
1644 * Note that the underlying vnode may have changed.
1646 if (ip->i_ump->um_fstype == UFS1)
1647 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1649 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1657 * Finish inode initialization.
1659 if (vp->v_type != VFIFO) {
1660 /* FFS supports shared locking for all files except fifos. */
1665 * Set up a generation number for this inode if it does not
1666 * already have one. This should only happen on old filesystems.
1668 if (ip->i_gen == 0) {
1669 ip->i_gen = arc4random() / 2 + 1;
1670 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1671 ip->i_flag |= IN_MODIFIED;
1672 DIP_SET(ip, i_gen, ip->i_gen);
1676 * Ensure that uid and gid are correct. This is a temporary
1677 * fix until fsck has been changed to do the update.
1679 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
1680 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1681 ip->i_uid = ip->i_din1->di_ouid; /* XXX */
1682 ip->i_gid = ip->i_din1->di_ogid; /* XXX */
1686 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1688 * If this vnode is already allocated, and we're running
1689 * multi-label, attempt to perform a label association
1690 * from the extended attributes on the inode.
1692 error = mac_vnode_associate_extattr(mp, vp);
1694 /* ufs_inactive will release ip->i_devvp ref. */
1707 * File handle to vnode
1709 * Have to be really careful about stale file handles:
1710 * - check that the inode number is valid
1711 * - call ffs_vget() to get the locked inode
1712 * - check for an unallocated inode (i_mode == 0)
1713 * - check that the given client host has export rights and return
1714 * those rights via. exflagsp and credanonp
1717 ffs_fhtovp(mp, fhp, vpp)
1725 ufhp = (struct ufid *)fhp;
1726 fs = VFSTOUFS(mp)->um_fs;
1727 if (ufhp->ufid_ino < ROOTINO ||
1728 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1730 return (ufs_fhtovp(mp, ufhp, vpp));
1734 * Initialize the filesystem.
1738 struct vfsconf *vfsp;
1741 softdep_initialize();
1742 return (ufs_init(vfsp));
1746 * Undo the work of ffs_init().
1750 struct vfsconf *vfsp;
1754 ret = ufs_uninit(vfsp);
1755 softdep_uninitialize();
1760 * Write a superblock and associated information back to disk.
1763 ffs_sbupdate(mp, waitfor, suspended)
1764 struct ufsmount *mp;
1768 struct fs *fs = mp->um_fs;
1773 int i, size, error, allerror = 0;
1775 if (fs->fs_ronly == 1 &&
1776 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1777 (MNT_RDONLY | MNT_UPDATE))
1778 panic("ffs_sbupdate: write read-only filesystem");
1780 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1782 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1785 * First write back the summary information.
1787 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1789 for (i = 0; i < blks; i += fs->fs_frag) {
1790 size = fs->fs_bsize;
1791 if (i + fs->fs_frag > blks)
1792 size = (blks - i) * fs->fs_fsize;
1793 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1795 bcopy(space, bp->b_data, (u_int)size);
1796 space = (char *)space + size;
1798 bp->b_flags |= B_VALIDSUSPWRT;
1799 if (waitfor != MNT_WAIT)
1801 else if ((error = bwrite(bp)) != 0)
1805 * Now write back the superblock itself. If any errors occurred
1806 * up to this point, then fail so that the superblock avoids
1807 * being written out as clean.
1814 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1815 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1816 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1817 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1818 fs->fs_sblockloc = SBLOCK_UFS1;
1820 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1821 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1822 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1823 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1824 fs->fs_sblockloc = SBLOCK_UFS2;
1827 fs->fs_time = time_second;
1828 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1829 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1831 bp->b_flags |= B_VALIDSUSPWRT;
1832 if (waitfor != MNT_WAIT)
1834 else if ((error = bwrite(bp)) != 0)
1840 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1841 int attrnamespace, const char *attrname)
1845 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1848 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1854 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1857 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1858 uma_zfree(uma_ufs1, ip->i_din1);
1859 else if (ip->i_din2 != NULL)
1860 uma_zfree(uma_ufs2, ip->i_din2);
1861 uma_zfree(uma_inode, ip);
1864 static int dobkgrdwrite = 1;
1865 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1866 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1869 * Complete a background write started from bwrite.
1872 ffs_backgroundwritedone(struct buf *bp)
1874 struct bufobj *bufobj;
1878 * Find the original buffer that we are writing.
1880 bufobj = bp->b_bufobj;
1882 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1883 panic("backgroundwritedone: lost buffer");
1884 /* Grab an extra reference to be dropped by the bufdone() below. */
1885 bufobj_wrefl(bufobj);
1888 * Process dependencies then return any unfinished ones.
1890 if (!LIST_EMPTY(&bp->b_dep))
1893 if (!LIST_EMPTY(&bp->b_dep))
1894 softdep_move_dependencies(bp, origbp);
1897 * This buffer is marked B_NOCACHE so when it is released
1898 * by biodone it will be tossed.
1900 bp->b_flags |= B_NOCACHE;
1901 bp->b_flags &= ~B_CACHE;
1905 * Clear the BV_BKGRDINPROG flag in the original buffer
1906 * and awaken it if it is waiting for the write to complete.
1907 * If BV_BKGRDINPROG is not set in the original buffer it must
1908 * have been released and re-instantiated - which is not legal.
1910 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1911 ("backgroundwritedone: lost buffer2"));
1912 origbp->b_vflags &= ~BV_BKGRDINPROG;
1913 if (origbp->b_vflags & BV_BKGRDWAIT) {
1914 origbp->b_vflags &= ~BV_BKGRDWAIT;
1915 wakeup(&origbp->b_xflags);
1922 * Write, release buffer on completion. (Done by iodone
1923 * if async). Do not bother writing anything if the buffer
1926 * Note that we set B_CACHE here, indicating that buffer is
1927 * fully valid and thus cacheable. This is true even of NFS
1928 * now so we set it generally. This could be set either here
1929 * or in biodone() since the I/O is synchronous. We put it
1933 ffs_bufwrite(struct buf *bp)
1938 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1939 if (bp->b_flags & B_INVAL) {
1944 oldflags = bp->b_flags;
1946 if (!BUF_ISLOCKED(bp))
1947 panic("bufwrite: buffer is not busy???");
1950 * If a background write is already in progress, delay
1951 * writing this block if it is asynchronous. Otherwise
1952 * wait for the background write to complete.
1954 BO_LOCK(bp->b_bufobj);
1955 if (bp->b_vflags & BV_BKGRDINPROG) {
1956 if (bp->b_flags & B_ASYNC) {
1957 BO_UNLOCK(bp->b_bufobj);
1962 bp->b_vflags |= BV_BKGRDWAIT;
1963 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1964 if (bp->b_vflags & BV_BKGRDINPROG)
1965 panic("bufwrite: still writing");
1967 BO_UNLOCK(bp->b_bufobj);
1969 /* Mark the buffer clean */
1973 * If this buffer is marked for background writing and we
1974 * do not have to wait for it, make a copy and write the
1975 * copy so as to leave this buffer ready for further use.
1977 * This optimization eats a lot of memory. If we have a page
1978 * or buffer shortfall we can't do it.
1980 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1981 (bp->b_flags & B_ASYNC) &&
1982 !vm_page_count_severe() &&
1983 !buf_dirty_count_severe()) {
1984 KASSERT(bp->b_iodone == NULL,
1985 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1987 /* get a new block */
1988 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
1993 * set it to be identical to the old block. We have to
1994 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1995 * to avoid confusing the splay tree and gbincore().
1997 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1998 newbp->b_lblkno = bp->b_lblkno;
1999 newbp->b_xflags |= BX_BKGRDMARKER;
2000 BO_LOCK(bp->b_bufobj);
2001 bp->b_vflags |= BV_BKGRDINPROG;
2002 bgetvp(bp->b_vp, newbp);
2003 BO_UNLOCK(bp->b_bufobj);
2004 newbp->b_bufobj = &bp->b_vp->v_bufobj;
2005 newbp->b_blkno = bp->b_blkno;
2006 newbp->b_offset = bp->b_offset;
2007 newbp->b_iodone = ffs_backgroundwritedone;
2008 newbp->b_flags |= B_ASYNC;
2009 newbp->b_flags &= ~B_INVAL;
2012 /* move over the dependencies */
2013 if (!LIST_EMPTY(&bp->b_dep))
2014 softdep_move_dependencies(bp, newbp);
2018 * Initiate write on the copy, release the original to
2019 * the B_LOCKED queue so that it cannot go away until
2020 * the background write completes. If not locked it could go
2021 * away and then be reconstituted while it was being written.
2022 * If the reconstituted buffer were written, we could end up
2023 * with two background copies being written at the same time.
2029 /* Let the normal bufwrite do the rest for us */
2031 return (bufwrite(bp));
2036 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2042 vp = bo->__bo_vnode;
2043 if (bp->b_iocmd == BIO_WRITE) {
2044 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2045 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2046 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2047 panic("ffs_geom_strategy: bad I/O");
2048 bp->b_flags &= ~B_VALIDSUSPWRT;
2049 if ((vp->v_vflag & VV_COPYONWRITE) &&
2050 vp->v_rdev->si_snapdata != NULL) {
2051 if ((bp->b_flags & B_CLUSTER) != 0) {
2052 runningbufwakeup(bp);
2053 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2054 b_cluster.cluster_entry) {
2055 error = ffs_copyonwrite(vp, tbp);
2057 error != EOPNOTSUPP) {
2058 bp->b_error = error;
2059 bp->b_ioflags |= BIO_ERROR;
2064 bp->b_runningbufspace = bp->b_bufsize;
2065 atomic_add_long(&runningbufspace,
2066 bp->b_runningbufspace);
2068 error = ffs_copyonwrite(vp, bp);
2069 if (error != 0 && error != EOPNOTSUPP) {
2070 bp->b_error = error;
2071 bp->b_ioflags |= BIO_ERROR;
2078 if ((bp->b_flags & B_CLUSTER) != 0) {
2079 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2080 b_cluster.cluster_entry) {
2081 if (!LIST_EMPTY(&tbp->b_dep))
2085 if (!LIST_EMPTY(&bp->b_dep))
2091 g_vfs_strategy(bo, bp);
2097 db_print_ffs(struct ufsmount *ump)
2099 db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
2101 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2102 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2103 ump->softdep_on_worklist_inprogress, ump->softdep_deps,
2107 DB_SHOW_COMMAND(ffs, db_show_ffs)
2110 struct ufsmount *ump;
2113 ump = VFSTOUFS((struct mount *)addr);
2118 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2119 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2120 db_print_ffs(VFSTOUFS(mp));