2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_quota.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
47 #include <sys/taskqueue.h>
48 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
54 #include <sys/fcntl.h>
55 #include <sys/ioccom.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/rwlock.h>
59 #include <sys/vmmeter.h>
61 #include <security/mac/mac_framework.h>
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/gjournal.h>
66 #include <ufs/ufs/quota.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ffs/fs.h>
72 #include <ufs/ffs/ffs_extern.h>
76 #include <vm/vm_page.h>
78 #include <geom/geom.h>
79 #include <geom/geom_vfs.h>
83 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
85 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
86 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
88 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
89 static int ffs_sync_lazy(struct mount *mp);
91 static vfs_init_t ffs_init;
92 static vfs_uninit_t ffs_uninit;
93 static vfs_extattrctl_t ffs_extattrctl;
94 static vfs_cmount_t ffs_cmount;
95 static vfs_unmount_t ffs_unmount;
96 static vfs_mount_t ffs_mount;
97 static vfs_statfs_t ffs_statfs;
98 static vfs_fhtovp_t ffs_fhtovp;
99 static vfs_sync_t ffs_sync;
101 static struct vfsops ufs_vfsops = {
102 .vfs_extattrctl = ffs_extattrctl,
103 .vfs_fhtovp = ffs_fhtovp,
104 .vfs_init = ffs_init,
105 .vfs_mount = ffs_mount,
106 .vfs_cmount = ffs_cmount,
107 .vfs_quotactl = ufs_quotactl,
108 .vfs_root = ufs_root,
109 .vfs_statfs = ffs_statfs,
110 .vfs_sync = ffs_sync,
111 .vfs_uninit = ffs_uninit,
112 .vfs_unmount = ffs_unmount,
113 .vfs_vget = ffs_vget,
114 .vfs_susp_clean = process_deferred_inactive,
117 VFS_SET(ufs_vfsops, ufs, 0);
118 MODULE_VERSION(ufs, 1);
120 static b_strategy_t ffs_geom_strategy;
121 static b_write_t ffs_bufwrite;
123 static struct buf_ops ffs_ops = {
125 .bop_write = ffs_bufwrite,
126 .bop_strategy = ffs_geom_strategy,
128 #ifdef NO_FFS_SNAPSHOT
129 .bop_bdflush = bufbdflush,
131 .bop_bdflush = ffs_bdflush,
136 * Note that userquota and groupquota options are not currently used
137 * by UFS/FFS code and generally mount(8) does not pass those options
138 * from userland, but they can be passed by loader(8) via
139 * vfs.root.mountfrom.options.
141 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
142 "noclusterw", "noexec", "export", "force", "from", "groupquota",
143 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
144 "nosymfollow", "sync", "union", "userquota", NULL };
147 ffs_mount(struct mount *mp)
151 struct ufsmount *ump = NULL;
154 int error, error1, flags;
157 struct nameidata ndp;
161 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
163 if (uma_inode == NULL) {
164 uma_inode = uma_zcreate("FFS inode",
165 sizeof(struct inode), NULL, NULL, NULL, NULL,
167 uma_ufs1 = uma_zcreate("FFS1 dinode",
168 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
170 uma_ufs2 = uma_zcreate("FFS2 dinode",
171 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
175 vfs_deleteopt(mp->mnt_optnew, "groupquota");
176 vfs_deleteopt(mp->mnt_optnew, "userquota");
178 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
183 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
184 mntorflags |= MNT_ACLS;
186 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
187 mntorflags |= MNT_SNAPSHOT;
189 * Once we have set the MNT_SNAPSHOT flag, do not
190 * persist "snapshot" in the options list.
192 vfs_deleteopt(mp->mnt_optnew, "snapshot");
193 vfs_deleteopt(mp->mnt_opt, "snapshot");
196 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
197 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
199 * Once we have set the restricted PID, do not
200 * persist "fsckpid" in the options list.
202 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
203 vfs_deleteopt(mp->mnt_opt, "fsckpid");
204 if (mp->mnt_flag & MNT_UPDATE) {
205 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
206 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
208 "Checker enable: Must be read-only");
211 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
213 "Checker enable: Must be read-only");
216 /* Set to -1 if we are done */
221 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
222 if (mntorflags & MNT_ACLS) {
224 "\"acls\" and \"nfsv4acls\" options "
225 "are mutually exclusive");
228 mntorflags |= MNT_NFS4ACLS;
232 mp->mnt_flag |= mntorflags;
235 * If updating, check whether changing from read-only to
236 * read/write; if there is no device name, that's all we do.
238 if (mp->mnt_flag & MNT_UPDATE) {
241 devvp = ump->um_devvp;
242 if (fsckpid == -1 && ump->um_fsckpid > 0) {
243 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
244 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
248 * Return to normal read-only mode.
250 error = g_access(ump->um_cp, 0, -1, 0);
254 if (fs->fs_ronly == 0 &&
255 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
257 * Flush any dirty data and suspend filesystem.
259 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
261 error = vfs_write_suspend_umnt(mp);
265 * Check for and optionally get rid of files open
269 if (mp->mnt_flag & MNT_FORCE)
271 if (MOUNTEDSOFTDEP(mp)) {
272 error = softdep_flushfiles(mp, flags, td);
274 error = ffs_flushfiles(mp, flags, td);
277 vfs_write_resume(mp, 0);
280 if (fs->fs_pendingblocks != 0 ||
281 fs->fs_pendinginodes != 0) {
282 printf("WARNING: %s Update error: blocks %jd "
283 "files %d\n", fs->fs_fsmnt,
284 (intmax_t)fs->fs_pendingblocks,
285 fs->fs_pendinginodes);
286 fs->fs_pendingblocks = 0;
287 fs->fs_pendinginodes = 0;
289 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
291 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
294 vfs_write_resume(mp, 0);
297 if (MOUNTEDSOFTDEP(mp))
301 * Drop our write and exclusive access.
303 g_access(ump->um_cp, 0, -1, -1);
307 mp->mnt_flag |= MNT_RDONLY;
310 * Allow the writers to note that filesystem
313 vfs_write_resume(mp, 0);
315 if ((mp->mnt_flag & MNT_RELOAD) &&
316 (error = ffs_reload(mp, td, 0)) != 0)
319 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
321 * If we are running a checker, do not allow upgrade.
323 if (ump->um_fsckpid > 0) {
325 "Active checker, cannot upgrade to write");
329 * If upgrade to read-write by non-root, then verify
330 * that user has necessary permissions on the device.
332 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
333 error = VOP_ACCESS(devvp, VREAD | VWRITE,
336 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
338 VOP_UNLOCK(devvp, 0);
341 VOP_UNLOCK(devvp, 0);
342 fs->fs_flags &= ~FS_UNCLEAN;
343 if (fs->fs_clean == 0) {
344 fs->fs_flags |= FS_UNCLEAN;
345 if ((mp->mnt_flag & MNT_FORCE) ||
347 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
348 (fs->fs_flags & FS_DOSOFTDEP))) {
349 printf("WARNING: %s was not properly "
350 "dismounted\n", fs->fs_fsmnt);
353 "R/W mount of %s denied. %s.%s",
355 "Filesystem is not clean - run fsck",
356 (fs->fs_flags & FS_SUJ) == 0 ? "" :
357 " Forced mount will invalidate"
358 " journal contents");
364 * Request exclusive write access.
366 error = g_access(ump->um_cp, 0, 1, 1);
370 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
374 mp->mnt_flag &= ~MNT_RDONLY;
376 fs->fs_mtime = time_second;
377 /* check to see if we need to start softdep */
378 if ((fs->fs_flags & FS_DOSOFTDEP) &&
379 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
380 vn_finished_write(mp);
384 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
385 vn_finished_write(mp);
388 if (fs->fs_snapinum[0] != 0)
389 ffs_snapshot_mount(mp);
390 vn_finished_write(mp);
393 * Soft updates is incompatible with "async",
394 * so if we are doing softupdates stop the user
395 * from setting the async flag in an update.
396 * Softdep_mount() clears it in an initial mount
399 if (MOUNTEDSOFTDEP(mp)) {
400 /* XXX: Reset too late ? */
402 mp->mnt_flag &= ~MNT_ASYNC;
406 * Keep MNT_ACLS flag if it is stored in superblock.
408 if ((fs->fs_flags & FS_ACLS) != 0) {
409 /* XXX: Set too late ? */
411 mp->mnt_flag |= MNT_ACLS;
415 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
416 /* XXX: Set too late ? */
418 mp->mnt_flag |= MNT_NFS4ACLS;
422 * If this is a request from fsck to clean up the filesystem,
423 * then allow the specified pid to proceed.
426 if (ump->um_fsckpid != 0) {
428 "Active checker already running on %s",
432 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
433 ("soft updates enabled on read-only file system"));
436 * Request write access.
438 error = g_access(ump->um_cp, 0, 1, 0);
442 "Checker activation failed on %s",
446 ump->um_fsckpid = fsckpid;
447 if (fs->fs_snapinum[0] != 0)
448 ffs_snapshot_mount(mp);
449 fs->fs_mtime = time_second;
452 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
456 * If this is a snapshot request, take the snapshot.
458 if (mp->mnt_flag & MNT_SNAPSHOT)
459 return (ffs_snapshot(mp, fspec));
462 * Must not call namei() while owning busy ref.
468 * Not an update, or updating the name: look up the name
469 * and verify that it refers to a sensible disk device.
471 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
473 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
475 * Unmount does not start if MNT_UPDATE is set. Mount
476 * update busies mp before setting MNT_UPDATE. We
477 * must be able to retain our busy ref succesfully,
480 error1 = vfs_busy(mp, MBF_NOWAIT);
485 NDFREE(&ndp, NDF_ONLY_PNBUF);
487 if (!vn_isdisk(devvp, &error)) {
493 * If mount by non-root, then verify that user has necessary
494 * permissions on the device.
497 if ((mp->mnt_flag & MNT_RDONLY) == 0)
499 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
501 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
507 if (mp->mnt_flag & MNT_UPDATE) {
511 * If it's not the same vnode, or at least the same device
512 * then it's not correct.
515 if (devvp->v_rdev != ump->um_devvp->v_rdev)
516 error = EINVAL; /* needs translation */
524 * We need the name for the mount point (also used for
525 * "last mounted on") copied in. If an error occurs,
526 * the mount point is discarded by the upper level code.
527 * Note that vfs_mount_alloc() populates f_mntonname for us.
529 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
534 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
535 ("soft updates enabled on read-only file system"));
540 * Request write access.
542 error = g_access(ump->um_cp, 0, 1, 0);
545 printf("WARNING: %s: Checker activation "
546 "failed\n", fs->fs_fsmnt);
548 ump->um_fsckpid = fsckpid;
549 if (fs->fs_snapinum[0] != 0)
550 ffs_snapshot_mount(mp);
551 fs->fs_mtime = time_second;
553 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
557 vfs_mountedfrom(mp, fspec);
562 * Compatibility with old mount system call.
566 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
568 struct ufs_args args;
569 struct export_args exp;
574 error = copyin(data, &args, sizeof args);
577 vfs_oexport_conv(&args.export, &exp);
579 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
580 ma = mount_arg(ma, "export", &exp, sizeof(exp));
581 error = kernel_mount(ma, flags);
587 * Reload all incore data for a filesystem (used after running fsck on
588 * the root filesystem and finding things to fix). If the 'force' flag
589 * is 0, the filesystem must be mounted read-only.
591 * Things to do to update the mount:
592 * 1) invalidate all cached meta-data.
593 * 2) re-read superblock from disk.
594 * 3) re-read summary information from disk.
595 * 4) invalidate all inactive vnodes.
596 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
597 * writers, if requested.
598 * 6) invalidate all cached file data.
599 * 7) re-read inode data for all active vnodes.
602 ffs_reload(struct mount *mp, struct thread *td, int flags)
604 struct vnode *vp, *mvp, *devvp;
608 struct fs *fs, *newfs;
609 struct ufsmount *ump;
610 ufs2_daddr_t sblockloc;
618 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
625 * Step 1: invalidate all cached meta-data.
627 devvp = VFSTOUFS(mp)->um_devvp;
628 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
629 if (vinvalbuf(devvp, 0, 0, 0) != 0)
630 panic("ffs_reload: dirty1");
631 VOP_UNLOCK(devvp, 0);
634 * Step 2: re-read superblock from disk.
636 fs = VFSTOUFS(mp)->um_fs;
637 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
640 newfs = (struct fs *)bp->b_data;
641 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
642 newfs->fs_magic != FS_UFS2_MAGIC) ||
643 newfs->fs_bsize > MAXBSIZE ||
644 newfs->fs_bsize < sizeof(struct fs)) {
646 return (EIO); /* XXX needs translation */
649 * Copy pointer fields back into superblock before copying in XXX
650 * new superblock. These should really be in the ufsmount. XXX
651 * Note that important parameters (eg fs_ncg) are unchanged.
653 newfs->fs_csp = fs->fs_csp;
654 newfs->fs_maxcluster = fs->fs_maxcluster;
655 newfs->fs_contigdirs = fs->fs_contigdirs;
656 newfs->fs_active = fs->fs_active;
657 newfs->fs_ronly = fs->fs_ronly;
658 sblockloc = fs->fs_sblockloc;
659 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
661 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
662 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
664 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
665 printf("WARNING: %s: reload pending error: blocks %jd "
666 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
667 fs->fs_pendinginodes);
668 fs->fs_pendingblocks = 0;
669 fs->fs_pendinginodes = 0;
674 * Step 3: re-read summary information from disk.
676 size = fs->fs_cssize;
677 blks = howmany(size, fs->fs_fsize);
678 if (fs->fs_contigsumsize > 0)
679 size += fs->fs_ncg * sizeof(int32_t);
680 size += fs->fs_ncg * sizeof(u_int8_t);
681 free(fs->fs_csp, M_UFSMNT);
682 space = malloc(size, M_UFSMNT, M_WAITOK);
684 for (i = 0; i < blks; i += fs->fs_frag) {
686 if (i + fs->fs_frag > blks)
687 size = (blks - i) * fs->fs_fsize;
688 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
692 bcopy(bp->b_data, space, (u_int)size);
693 space = (char *)space + size;
697 * We no longer know anything about clusters per cylinder group.
699 if (fs->fs_contigsumsize > 0) {
700 fs->fs_maxcluster = lp = space;
701 for (i = 0; i < fs->fs_ncg; i++)
702 *lp++ = fs->fs_contigsumsize;
705 size = fs->fs_ncg * sizeof(u_int8_t);
706 fs->fs_contigdirs = (u_int8_t *)space;
707 bzero(fs->fs_contigdirs, size);
708 if ((flags & FFSR_UNSUSPEND) != 0) {
710 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
711 wakeup(&mp->mnt_flag);
716 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
720 if (vp->v_type == VNON) {
725 * Step 4: invalidate all cached file data.
727 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
728 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
731 if (vinvalbuf(vp, 0, 0, 0))
732 panic("ffs_reload: dirty2");
734 * Step 5: re-read inode data for all active vnodes.
738 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
739 (int)fs->fs_bsize, NOCRED, &bp);
743 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
746 ffs_load_inode(bp, ip, fs, ip->i_number);
747 ip->i_effnlink = ip->i_nlink;
756 * Possible superblock locations ordered from most to least likely.
758 static int sblock_try[] = SBLOCKSEARCH;
761 * Common code for mount and mountroot
764 ffs_mountfs(devvp, mp, td)
769 struct ufsmount *ump;
774 ufs2_daddr_t sblockloc;
775 int error, i, blks, len, ronly;
779 struct g_consumer *cp;
784 cred = td ? td->td_ucred : NOCRED;
785 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
787 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
789 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
790 (uintptr_t)mp) == 0) {
791 VOP_UNLOCK(devvp, 0);
795 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
798 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
799 VOP_UNLOCK(devvp, 0);
803 devvp->v_bufobj.bo_ops = &ffs_ops;
804 VOP_UNLOCK(devvp, 0);
805 if (dev->si_iosize_max != 0)
806 mp->mnt_iosize_max = dev->si_iosize_max;
807 if (mp->mnt_iosize_max > MAXPHYS)
808 mp->mnt_iosize_max = MAXPHYS;
813 * Try reading the superblock in each of its possible locations.
815 for (i = 0; sblock_try[i] != -1; i++) {
816 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
819 "Invalid sectorsize %d for superblock size %d",
820 cp->provider->sectorsize, SBLOCKSIZE);
823 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
826 fs = (struct fs *)bp->b_data;
827 sblockloc = sblock_try[i];
828 if ((fs->fs_magic == FS_UFS1_MAGIC ||
829 (fs->fs_magic == FS_UFS2_MAGIC &&
830 (fs->fs_sblockloc == sblockloc ||
831 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
832 fs->fs_bsize <= MAXBSIZE &&
833 fs->fs_bsize >= sizeof(struct fs))
838 if (sblock_try[i] == -1) {
839 error = EINVAL; /* XXX needs translation */
843 /* none of these types of check-hashes are maintained */
844 fs->fs_metackhash &= ~(CK_SUPERBLOCK | CK_INODE | CK_INDIR | CK_DIR);
845 /* no support for directory indices or any other undefined flags */
846 fs->fs_flags &= ~FS_INDEXDIRS;
847 fs->fs_flags &= FS_SUPPORTED;
848 fs->fs_flags &= ~FS_UNCLEAN;
849 if (fs->fs_clean == 0) {
850 fs->fs_flags |= FS_UNCLEAN;
851 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
852 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
853 (fs->fs_flags & FS_DOSOFTDEP))) {
854 printf("WARNING: %s was not properly dismounted\n",
857 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
858 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
859 (fs->fs_flags & FS_SUJ) == 0 ? "" :
860 " Forced mount will invalidate journal contents");
864 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
865 (mp->mnt_flag & MNT_FORCE)) {
866 printf("WARNING: %s: lost blocks %jd files %d\n",
867 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
868 fs->fs_pendinginodes);
869 fs->fs_pendingblocks = 0;
870 fs->fs_pendinginodes = 0;
873 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
874 printf("WARNING: %s: mount pending error: blocks %jd "
875 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
876 fs->fs_pendinginodes);
877 fs->fs_pendingblocks = 0;
878 fs->fs_pendinginodes = 0;
880 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
883 * Get journal provider name.
886 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
887 if (g_io_getattr("GJOURNAL::provider", cp, &len,
888 mp->mnt_gjprovider) == 0) {
889 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
892 mp->mnt_flag |= MNT_GJOURNAL;
895 printf("WARNING: %s: GJOURNAL flag on fs "
896 "but no gjournal provider below\n",
897 mp->mnt_stat.f_mntonname);
898 free(mp->mnt_gjprovider, M_UFSMNT);
899 mp->mnt_gjprovider = NULL;
902 printf("WARNING: %s: GJOURNAL flag on fs but no "
903 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
906 mp->mnt_gjprovider = NULL;
908 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
910 ump->um_bo = &devvp->v_bufobj;
911 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
912 if (fs->fs_magic == FS_UFS1_MAGIC) {
913 ump->um_fstype = UFS1;
914 ump->um_balloc = ffs_balloc_ufs1;
916 ump->um_fstype = UFS2;
917 ump->um_balloc = ffs_balloc_ufs2;
919 ump->um_blkatoff = ffs_blkatoff;
920 ump->um_truncate = ffs_truncate;
921 ump->um_update = ffs_update;
922 ump->um_valloc = ffs_valloc;
923 ump->um_vfree = ffs_vfree;
924 ump->um_ifree = ffs_ifree;
925 ump->um_rdonly = ffs_rdonly;
926 ump->um_snapgone = ffs_snapgone;
927 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
928 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
929 if (fs->fs_sbsize < SBLOCKSIZE)
930 bp->b_flags |= B_INVAL | B_NOCACHE;
934 ffs_oldfscompat_read(fs, ump, sblockloc);
935 fs->fs_ronly = ronly;
936 size = fs->fs_cssize;
937 blks = howmany(size, fs->fs_fsize);
938 if (fs->fs_contigsumsize > 0)
939 size += fs->fs_ncg * sizeof(int32_t);
940 size += fs->fs_ncg * sizeof(u_int8_t);
941 space = malloc(size, M_UFSMNT, M_WAITOK);
943 for (i = 0; i < blks; i += fs->fs_frag) {
945 if (i + fs->fs_frag > blks)
946 size = (blks - i) * fs->fs_fsize;
947 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
949 free(fs->fs_csp, M_UFSMNT);
952 bcopy(bp->b_data, space, (u_int)size);
953 space = (char *)space + size;
957 if (fs->fs_contigsumsize > 0) {
958 fs->fs_maxcluster = lp = space;
959 for (i = 0; i < fs->fs_ncg; i++)
960 *lp++ = fs->fs_contigsumsize;
963 size = fs->fs_ncg * sizeof(u_int8_t);
964 fs->fs_contigdirs = (u_int8_t *)space;
965 bzero(fs->fs_contigdirs, size);
966 fs->fs_active = NULL;
968 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
969 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
971 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
972 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
977 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
979 mp->mnt_flag |= MNT_LOCAL;
981 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
984 mp->mnt_flag |= MNT_MULTILABEL;
987 printf("WARNING: %s: multilabel flag on fs but "
988 "no MAC support\n", mp->mnt_stat.f_mntonname);
991 if ((fs->fs_flags & FS_ACLS) != 0) {
995 if (mp->mnt_flag & MNT_NFS4ACLS)
996 printf("WARNING: %s: ACLs flag on fs conflicts with "
997 "\"nfsv4acls\" mount option; option ignored\n",
998 mp->mnt_stat.f_mntonname);
999 mp->mnt_flag &= ~MNT_NFS4ACLS;
1000 mp->mnt_flag |= MNT_ACLS;
1004 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
1005 mp->mnt_stat.f_mntonname);
1008 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
1012 if (mp->mnt_flag & MNT_ACLS)
1013 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1014 "with \"acls\" mount option; option ignored\n",
1015 mp->mnt_stat.f_mntonname);
1016 mp->mnt_flag &= ~MNT_ACLS;
1017 mp->mnt_flag |= MNT_NFS4ACLS;
1021 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1022 "ACLs support\n", mp->mnt_stat.f_mntonname);
1025 if ((fs->fs_flags & FS_TRIM) != 0) {
1027 if (g_io_getattr("GEOM::candelete", cp, &len,
1028 &ump->um_candelete) == 0) {
1029 if (!ump->um_candelete)
1030 printf("WARNING: %s: TRIM flag on fs but disk "
1031 "does not support TRIM\n",
1032 mp->mnt_stat.f_mntonname);
1034 printf("WARNING: %s: TRIM flag on fs but disk does "
1035 "not confirm that it supports TRIM\n",
1036 mp->mnt_stat.f_mntonname);
1037 ump->um_candelete = 0;
1039 if (ump->um_candelete) {
1040 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1041 taskqueue_thread_enqueue, &ump->um_trim_tq);
1042 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1043 "%s trim", mp->mnt_stat.f_mntonname);
1047 ump->um_mountp = mp;
1049 ump->um_devvp = devvp;
1050 ump->um_nindir = fs->fs_nindir;
1051 ump->um_bptrtodb = fs->fs_fsbtodb;
1052 ump->um_seqinc = fs->fs_frag;
1053 for (i = 0; i < MAXQUOTAS; i++)
1054 ump->um_quotas[i] = NULLVP;
1056 ufs_extattr_uepm_init(&ump->um_extattr);
1059 * Set FS local "last mounted on" information (NULL pad)
1061 bzero(fs->fs_fsmnt, MAXMNTLEN);
1062 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1063 mp->mnt_stat.f_iosize = fs->fs_bsize;
1065 if (mp->mnt_flag & MNT_ROOTFS) {
1067 * Root mount; update timestamp in mount structure.
1068 * this will be used by the common root mount code
1069 * to update the system clock.
1071 mp->mnt_time = fs->fs_time;
1075 fs->fs_mtime = time_second;
1076 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1077 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1078 free(fs->fs_csp, M_UFSMNT);
1079 ffs_flushfiles(mp, FORCECLOSE, td);
1082 if (fs->fs_snapinum[0] != 0)
1083 ffs_snapshot_mount(mp);
1086 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1089 * Initialize filesystem state information in mount struct.
1092 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1093 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1096 #ifdef UFS_EXTATTR_AUTOSTART
1099 * Auto-starting does the following:
1100 * - check for /.attribute in the fs, and extattr_start if so
1101 * - for each file in .attribute, enable that file with
1102 * an attribute of the same name.
1103 * Not clear how to report errors -- probably eat them.
1104 * This would all happen while the filesystem was busy/not
1105 * available, so would effectively be "atomic".
1107 (void) ufs_extattr_autostart(mp, td);
1108 #endif /* !UFS_EXTATTR_AUTOSTART */
1109 #endif /* !UFS_EXTATTR */
1117 g_topology_unlock();
1120 mtx_destroy(UFS_MTX(ump));
1121 if (mp->mnt_gjprovider != NULL) {
1122 free(mp->mnt_gjprovider, M_UFSMNT);
1123 mp->mnt_gjprovider = NULL;
1125 free(ump->um_fs, M_UFSMNT);
1126 free(ump, M_UFSMNT);
1127 mp->mnt_data = NULL;
1129 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1134 #include <sys/sysctl.h>
1135 static int bigcgs = 0;
1136 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1139 * Sanity checks for loading old filesystem superblocks.
1140 * See ffs_oldfscompat_write below for unwound actions.
1142 * XXX - Parts get retired eventually.
1143 * Unfortunately new bits get added.
1146 ffs_oldfscompat_read(fs, ump, sblockloc)
1148 struct ufsmount *ump;
1149 ufs2_daddr_t sblockloc;
1154 * If not yet done, update fs_flags location and value of fs_sblockloc.
1156 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1157 fs->fs_flags = fs->fs_old_flags;
1158 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1159 fs->fs_sblockloc = sblockloc;
1162 * If not yet done, update UFS1 superblock with new wider fields.
1164 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1165 fs->fs_maxbsize = fs->fs_bsize;
1166 fs->fs_time = fs->fs_old_time;
1167 fs->fs_size = fs->fs_old_size;
1168 fs->fs_dsize = fs->fs_old_dsize;
1169 fs->fs_csaddr = fs->fs_old_csaddr;
1170 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1171 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1172 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1173 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1175 if (fs->fs_magic == FS_UFS1_MAGIC &&
1176 fs->fs_old_inodefmt < FS_44INODEFMT) {
1177 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1178 fs->fs_qbmask = ~fs->fs_bmask;
1179 fs->fs_qfmask = ~fs->fs_fmask;
1181 if (fs->fs_magic == FS_UFS1_MAGIC) {
1182 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1183 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1184 if (fs->fs_maxfilesize > maxfilesize)
1185 fs->fs_maxfilesize = maxfilesize;
1187 /* Compatibility for old filesystems */
1188 if (fs->fs_avgfilesize <= 0)
1189 fs->fs_avgfilesize = AVFILESIZ;
1190 if (fs->fs_avgfpdir <= 0)
1191 fs->fs_avgfpdir = AFPDIR;
1193 fs->fs_save_cgsize = fs->fs_cgsize;
1194 fs->fs_cgsize = fs->fs_bsize;
1199 * Unwinding superblock updates for old filesystems.
1200 * See ffs_oldfscompat_read above for details.
1202 * XXX - Parts get retired eventually.
1203 * Unfortunately new bits get added.
1206 ffs_oldfscompat_write(fs, ump)
1208 struct ufsmount *ump;
1212 * Copy back UFS2 updated fields that UFS1 inspects.
1214 if (fs->fs_magic == FS_UFS1_MAGIC) {
1215 fs->fs_old_time = fs->fs_time;
1216 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1217 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1218 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1219 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1220 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1223 fs->fs_cgsize = fs->fs_save_cgsize;
1224 fs->fs_save_cgsize = 0;
1229 * unmount system call
1232 ffs_unmount(mp, mntflags)
1237 struct ufsmount *ump = VFSTOUFS(mp);
1239 int error, flags, susp;
1248 if (mntflags & MNT_FORCE) {
1249 flags |= FORCECLOSE;
1250 susp = fs->fs_ronly == 0;
1253 if ((error = ufs_extattr_stop(mp, td))) {
1254 if (error != EOPNOTSUPP)
1255 printf("WARNING: unmount %s: ufs_extattr_stop "
1256 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1260 ufs_extattr_uepm_destroy(&ump->um_extattr);
1265 error = vfs_write_suspend_umnt(mp);
1269 if (MOUNTEDSOFTDEP(mp))
1270 error = softdep_flushfiles(mp, flags, td);
1272 error = ffs_flushfiles(mp, flags, td);
1273 if (error != 0 && error != ENXIO)
1277 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1278 printf("WARNING: unmount %s: pending error: blocks %jd "
1279 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1280 fs->fs_pendinginodes);
1281 fs->fs_pendingblocks = 0;
1282 fs->fs_pendinginodes = 0;
1285 if (MOUNTEDSOFTDEP(mp))
1286 softdep_unmount(mp);
1287 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1288 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1289 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1290 if (error && error != ENXIO) {
1296 vfs_write_resume(mp, VR_START_WRITE);
1297 if (ump->um_trim_tq != NULL) {
1298 while (ump->um_trim_inflight != 0)
1299 pause("ufsutr", hz);
1300 taskqueue_drain_all(ump->um_trim_tq);
1301 taskqueue_free(ump->um_trim_tq);
1304 if (ump->um_fsckpid > 0) {
1306 * Return to normal read-only mode.
1308 error = g_access(ump->um_cp, 0, -1, 0);
1309 ump->um_fsckpid = 0;
1311 g_vfs_close(ump->um_cp);
1312 g_topology_unlock();
1313 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1314 vrele(ump->um_devvp);
1315 dev_rel(ump->um_dev);
1316 mtx_destroy(UFS_MTX(ump));
1317 if (mp->mnt_gjprovider != NULL) {
1318 free(mp->mnt_gjprovider, M_UFSMNT);
1319 mp->mnt_gjprovider = NULL;
1321 free(fs->fs_csp, M_UFSMNT);
1323 free(ump, M_UFSMNT);
1324 mp->mnt_data = NULL;
1326 mp->mnt_flag &= ~MNT_LOCAL;
1328 if (td->td_su == mp) {
1336 vfs_write_resume(mp, VR_START_WRITE);
1340 ufs_extattr_uepm_init(&ump->um_extattr);
1341 #ifdef UFS_EXTATTR_AUTOSTART
1342 (void) ufs_extattr_autostart(mp, td);
1351 * Flush out all the files in a filesystem.
1354 ffs_flushfiles(mp, flags, td)
1359 struct ufsmount *ump;
1365 if (mp->mnt_flag & MNT_QUOTA) {
1367 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1370 for (i = 0; i < MAXQUOTAS; i++) {
1371 error = quotaoff(td, mp, i);
1373 if ((flags & EARLYFLUSH) == 0)
1381 * Here we fall through to vflush again to ensure that
1382 * we have gotten rid of all the system vnodes, unless
1383 * quotas must not be closed.
1387 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1388 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1389 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1391 ffs_snapshot_unmount(mp);
1392 flags |= FORCECLOSE;
1394 * Here we fall through to vflush again to ensure
1395 * that we have gotten rid of all the system vnodes.
1400 * Do not close system files if quotas were not closed, to be
1401 * able to sync the remaining dquots. The freeblks softupdate
1402 * workitems might hold a reference on a dquot, preventing
1403 * quotaoff() from completing. Next round of
1404 * softdep_flushworklist() iteration should process the
1405 * blockers, allowing the next run of quotaoff() to finally
1406 * flush held dquots.
1408 * Otherwise, flush all the files.
1410 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1414 * Flush filesystem metadata.
1416 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1417 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1418 VOP_UNLOCK(ump->um_devvp, 0);
1423 * Get filesystem statistics.
1430 struct ufsmount *ump;
1435 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1436 panic("ffs_statfs");
1437 sbp->f_version = STATFS_VERSION;
1438 sbp->f_bsize = fs->fs_fsize;
1439 sbp->f_iosize = fs->fs_bsize;
1440 sbp->f_blocks = fs->fs_dsize;
1442 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1443 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1444 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1445 dbtofsb(fs, fs->fs_pendingblocks);
1446 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1447 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1449 sbp->f_namemax = UFS_MAXNAMLEN;
1454 sync_doupdate(struct inode *ip)
1457 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1462 * For a lazy sync, we only care about access times, quotas and the
1463 * superblock. Other filesystem changes are already converted to
1464 * cylinder group blocks or inode blocks updates and are written to
1471 struct vnode *mvp, *vp;
1474 int allerror, error;
1478 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1480 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1481 if (vp->v_type == VNON) {
1488 * The IN_ACCESS flag is converted to IN_MODIFIED by
1489 * ufs_close() and ufs_getattr() by the calls to
1490 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1491 * Test also all the other timestamp flags too, to pick up
1492 * any other cases that could be missed.
1494 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1498 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1501 if (sync_doupdate(ip))
1502 error = ffs_update(vp, 0);
1513 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1514 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1520 * Go through the disk queues to initiate sandbagged IO;
1521 * go through the inodes to write those that have been modified;
1522 * initiate the writing of the super block if it has been modified.
1524 * Note: we are always called with the filesystem marked busy using
1528 ffs_sync(mp, waitfor)
1532 struct vnode *mvp, *vp, *devvp;
1535 struct ufsmount *ump = VFSTOUFS(mp);
1537 int error, count, lockreq, allerror = 0;
1540 int secondary_writes;
1541 int secondary_accwrites;
1543 int softdep_accdeps;
1550 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1551 panic("%s: ffs_sync: modification on read-only filesystem",
1553 if (waitfor == MNT_LAZY) {
1555 return (ffs_sync_lazy(mp));
1556 waitfor = MNT_NOWAIT;
1560 * Write back each (modified) inode.
1562 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1563 if (waitfor == MNT_SUSPEND) {
1567 if (waitfor == MNT_WAIT)
1568 lockreq = LK_EXCLUSIVE;
1569 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1571 /* Grab snapshot of secondary write counts */
1573 secondary_writes = mp->mnt_secondary_writes;
1574 secondary_accwrites = mp->mnt_secondary_accwrites;
1577 /* Grab snapshot of softdep dependency counts */
1578 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1580 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1582 * Depend on the vnode interlock to keep things stable enough
1583 * for a quick test. Since there might be hundreds of
1584 * thousands of vnodes, we cannot afford even a subroutine
1585 * call unless there's a good chance that we have work to do.
1587 if (vp->v_type == VNON) {
1593 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1594 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1598 if ((error = vget(vp, lockreq, td)) != 0) {
1599 if (error == ENOENT || error == ENOLCK) {
1600 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1605 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1610 * Force stale filesystem control information to be flushed.
1612 if (waitfor == MNT_WAIT || rebooting) {
1613 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1615 /* Flushed work items may create new vnodes to clean */
1616 if (allerror == 0 && count)
1623 devvp = ump->um_devvp;
1624 bo = &devvp->v_bufobj;
1626 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1628 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1629 error = VOP_FSYNC(devvp, waitfor, td);
1630 VOP_UNLOCK(devvp, 0);
1631 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1632 error = ffs_sbupdate(ump, waitfor, 0);
1635 if (allerror == 0 && waitfor == MNT_WAIT)
1637 } else if (suspend != 0) {
1638 if (softdep_check_suspend(mp,
1643 secondary_accwrites) != 0) {
1645 goto loop; /* More work needed */
1647 mtx_assert(MNT_MTX(mp), MA_OWNED);
1648 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1654 * Write back modified superblock.
1656 if (fs->fs_fmod != 0 &&
1657 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1663 ffs_vget(mp, ino, flags, vpp)
1669 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1673 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1682 struct ufsmount *ump;
1687 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1688 if (error || *vpp != NULL)
1692 * We must promote to an exclusive lock for vnode creation. This
1693 * can happen if lookup is passed LOCKSHARED.
1695 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1696 flags &= ~LK_TYPE_MASK;
1697 flags |= LK_EXCLUSIVE;
1701 * We do not lock vnode creation as it is believed to be too
1702 * expensive for such rare case as simultaneous creation of vnode
1703 * for same ino by different processes. We just allow them to race
1704 * and check later to decide who wins. Let the race begin!
1709 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1711 /* Allocate a new vnode/inode. */
1712 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1713 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1716 uma_zfree(uma_inode, ip);
1720 * FFS supports recursive locking.
1722 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1725 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1730 ip->i_nextclustercg = -1;
1731 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1735 for (i = 0; i < MAXQUOTAS; i++)
1736 ip->i_dquot[i] = NODQUOT;
1740 if (ffs_flags & FFSV_FORCEINSMQ)
1741 vp->v_vflag |= VV_FORCEINSMQ;
1742 error = insmntque(vp, mp);
1744 uma_zfree(uma_inode, ip);
1748 vp->v_vflag &= ~VV_FORCEINSMQ;
1749 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1750 if (error || *vpp != NULL)
1753 /* Read in the disk contents for the inode, copy into the inode. */
1754 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1755 (int)fs->fs_bsize, NOCRED, &bp);
1758 * The inode does not contain anything useful, so it would
1759 * be misleading to leave it on its hash chain. With mode
1760 * still zero, it will be unlinked and returned to the free
1769 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1771 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1772 ffs_load_inode(bp, ip, fs, ino);
1773 if (DOINGSOFTDEP(vp))
1774 softdep_load_inodeblock(ip);
1776 ip->i_effnlink = ip->i_nlink;
1780 * Initialize the vnode from the inode, check for aliases.
1781 * Note that the underlying vnode may have changed.
1783 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1792 * Finish inode initialization.
1794 if (vp->v_type != VFIFO) {
1795 /* FFS supports shared locking for all files except fifos. */
1800 * Set up a generation number for this inode if it does not
1801 * already have one. This should only happen on old filesystems.
1803 if (ip->i_gen == 0) {
1804 while (ip->i_gen == 0)
1805 ip->i_gen = arc4random();
1806 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1807 ip->i_flag |= IN_MODIFIED;
1808 DIP_SET(ip, i_gen, ip->i_gen);
1812 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1814 * If this vnode is already allocated, and we're running
1815 * multi-label, attempt to perform a label association
1816 * from the extended attributes on the inode.
1818 error = mac_vnode_associate_extattr(mp, vp);
1820 /* ufs_inactive will release ip->i_devvp ref. */
1833 * File handle to vnode
1835 * Have to be really careful about stale file handles:
1836 * - check that the inode number is valid
1837 * - for UFS2 check that the inode number is initialized
1838 * - call ffs_vget() to get the locked inode
1839 * - check for an unallocated inode (i_mode == 0)
1840 * - check that the given client host has export rights and return
1841 * those rights via. exflagsp and credanonp
1844 ffs_fhtovp(mp, fhp, flags, vpp)
1851 struct ufsmount *ump;
1859 ufhp = (struct ufid *)fhp;
1860 ino = ufhp->ufid_ino;
1863 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1866 * Need to check if inode is initialized because UFS2 does lazy
1867 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1869 if (fs->fs_magic != FS_UFS2_MAGIC)
1870 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1871 cg = ino_to_cg(fs, ino);
1872 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0)
1874 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1879 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1883 * Initialize the filesystem.
1887 struct vfsconf *vfsp;
1890 ffs_susp_initialize();
1891 softdep_initialize();
1892 return (ufs_init(vfsp));
1896 * Undo the work of ffs_init().
1900 struct vfsconf *vfsp;
1904 ret = ufs_uninit(vfsp);
1905 softdep_uninitialize();
1906 ffs_susp_uninitialize();
1911 * Write a superblock and associated information back to disk.
1914 ffs_sbupdate(ump, waitfor, suspended)
1915 struct ufsmount *ump;
1919 struct fs *fs = ump->um_fs;
1924 int i, size, error, allerror = 0;
1926 if (fs->fs_ronly == 1 &&
1927 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1928 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1929 panic("ffs_sbupdate: write read-only filesystem");
1931 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1933 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1934 (int)fs->fs_sbsize, 0, 0, 0);
1936 * First write back the summary information.
1938 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1940 for (i = 0; i < blks; i += fs->fs_frag) {
1941 size = fs->fs_bsize;
1942 if (i + fs->fs_frag > blks)
1943 size = (blks - i) * fs->fs_fsize;
1944 bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1946 bcopy(space, bp->b_data, (u_int)size);
1947 space = (char *)space + size;
1949 bp->b_flags |= B_VALIDSUSPWRT;
1950 if (waitfor != MNT_WAIT)
1952 else if ((error = bwrite(bp)) != 0)
1956 * Now write back the superblock itself. If any errors occurred
1957 * up to this point, then fail so that the superblock avoids
1958 * being written out as clean.
1965 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1966 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1967 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1968 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1969 fs->fs_sblockloc = SBLOCK_UFS1;
1971 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1972 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1973 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1974 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1975 fs->fs_sblockloc = SBLOCK_UFS2;
1978 fs->fs_time = time_second;
1979 if (MOUNTEDSOFTDEP(ump->um_mountp))
1980 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1981 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1982 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1984 bp->b_flags |= B_VALIDSUSPWRT;
1985 if (waitfor != MNT_WAIT)
1987 else if ((error = bwrite(bp)) != 0)
1993 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1994 int attrnamespace, const char *attrname)
1998 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2001 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2007 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2010 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2011 uma_zfree(uma_ufs1, ip->i_din1);
2012 else if (ip->i_din2 != NULL)
2013 uma_zfree(uma_ufs2, ip->i_din2);
2014 uma_zfree(uma_inode, ip);
2017 static int dobkgrdwrite = 1;
2018 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2019 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2022 * Complete a background write started from bwrite.
2025 ffs_backgroundwritedone(struct buf *bp)
2027 struct bufobj *bufobj;
2031 * Find the original buffer that we are writing.
2033 bufobj = bp->b_bufobj;
2035 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2036 panic("backgroundwritedone: lost buffer");
2039 * We should mark the cylinder group buffer origbp as
2040 * dirty, to not loose the failed write.
2042 if ((bp->b_ioflags & BIO_ERROR) != 0)
2043 origbp->b_vflags |= BV_BKGRDERR;
2046 * Process dependencies then return any unfinished ones.
2049 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2052 if (!LIST_EMPTY(&bp->b_dep))
2053 softdep_move_dependencies(bp, origbp);
2056 * This buffer is marked B_NOCACHE so when it is released
2057 * by biodone it will be tossed.
2059 bp->b_flags |= B_NOCACHE;
2060 bp->b_flags &= ~B_CACHE;
2063 * Prevent brelse() from trying to keep and re-dirtying bp on
2064 * errors. It causes b_bufobj dereference in
2065 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2068 if ((bp->b_ioflags & BIO_ERROR) != 0)
2069 bp->b_flags |= B_INVAL;
2073 * Clear the BV_BKGRDINPROG flag in the original buffer
2074 * and awaken it if it is waiting for the write to complete.
2075 * If BV_BKGRDINPROG is not set in the original buffer it must
2076 * have been released and re-instantiated - which is not legal.
2078 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2079 ("backgroundwritedone: lost buffer2"));
2080 origbp->b_vflags &= ~BV_BKGRDINPROG;
2081 if (origbp->b_vflags & BV_BKGRDWAIT) {
2082 origbp->b_vflags &= ~BV_BKGRDWAIT;
2083 wakeup(&origbp->b_xflags);
2090 * Write, release buffer on completion. (Done by iodone
2091 * if async). Do not bother writing anything if the buffer
2094 * Note that we set B_CACHE here, indicating that buffer is
2095 * fully valid and thus cacheable. This is true even of NFS
2096 * now so we set it generally. This could be set either here
2097 * or in biodone() since the I/O is synchronous. We put it
2101 ffs_bufwrite(struct buf *bp)
2105 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2106 if (bp->b_flags & B_INVAL) {
2111 if (!BUF_ISLOCKED(bp))
2112 panic("bufwrite: buffer is not busy???");
2114 * If a background write is already in progress, delay
2115 * writing this block if it is asynchronous. Otherwise
2116 * wait for the background write to complete.
2118 BO_LOCK(bp->b_bufobj);
2119 if (bp->b_vflags & BV_BKGRDINPROG) {
2120 if (bp->b_flags & B_ASYNC) {
2121 BO_UNLOCK(bp->b_bufobj);
2125 bp->b_vflags |= BV_BKGRDWAIT;
2126 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2128 if (bp->b_vflags & BV_BKGRDINPROG)
2129 panic("bufwrite: still writing");
2131 bp->b_vflags &= ~BV_BKGRDERR;
2132 BO_UNLOCK(bp->b_bufobj);
2135 * If this buffer is marked for background writing and we
2136 * do not have to wait for it, make a copy and write the
2137 * copy so as to leave this buffer ready for further use.
2139 * This optimization eats a lot of memory. If we have a page
2140 * or buffer shortfall we can't do it.
2142 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2143 (bp->b_flags & B_ASYNC) &&
2144 !vm_page_count_severe() &&
2145 !buf_dirty_count_severe()) {
2146 KASSERT(bp->b_iodone == NULL,
2147 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2149 /* get a new block */
2150 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2154 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2155 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2156 BO_LOCK(bp->b_bufobj);
2157 bp->b_vflags |= BV_BKGRDINPROG;
2158 BO_UNLOCK(bp->b_bufobj);
2160 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2161 newbp->b_lblkno = bp->b_lblkno;
2162 newbp->b_blkno = bp->b_blkno;
2163 newbp->b_offset = bp->b_offset;
2164 newbp->b_iodone = ffs_backgroundwritedone;
2165 newbp->b_flags |= B_ASYNC;
2166 newbp->b_flags &= ~B_INVAL;
2167 pbgetvp(bp->b_vp, newbp);
2171 * Move over the dependencies. If there are rollbacks,
2172 * leave the parent buffer dirtied as it will need to
2175 if (LIST_EMPTY(&bp->b_dep) ||
2176 softdep_move_dependencies(bp, newbp) == 0)
2183 * Initiate write on the copy, release the original. The
2184 * BKGRDINPROG flag prevents it from going away until
2185 * the background write completes.
2190 /* Mark the buffer clean */
2194 /* Let the normal bufwrite do the rest for us */
2196 return (bufwrite(bp));
2201 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2208 if (bp->b_iocmd == BIO_WRITE) {
2209 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2210 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2211 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2212 panic("ffs_geom_strategy: bad I/O");
2213 nocopy = bp->b_flags & B_NOCOPY;
2214 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2215 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2216 vp->v_rdev->si_snapdata != NULL) {
2217 if ((bp->b_flags & B_CLUSTER) != 0) {
2218 runningbufwakeup(bp);
2219 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2220 b_cluster.cluster_entry) {
2221 error = ffs_copyonwrite(vp, tbp);
2223 error != EOPNOTSUPP) {
2224 bp->b_error = error;
2225 bp->b_ioflags |= BIO_ERROR;
2230 bp->b_runningbufspace = bp->b_bufsize;
2231 atomic_add_long(&runningbufspace,
2232 bp->b_runningbufspace);
2234 error = ffs_copyonwrite(vp, bp);
2235 if (error != 0 && error != EOPNOTSUPP) {
2236 bp->b_error = error;
2237 bp->b_ioflags |= BIO_ERROR;
2244 if ((bp->b_flags & B_CLUSTER) != 0) {
2245 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2246 b_cluster.cluster_entry) {
2247 if (!LIST_EMPTY(&tbp->b_dep))
2251 if (!LIST_EMPTY(&bp->b_dep))
2257 * Check for metadata that needs check-hashes and update them.
2259 switch (bp->b_xflags & BX_FSPRIV) {
2261 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2262 ((struct cg *)bp->b_data)->cg_ckhash =
2263 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2270 printf("Check-hash write is unimplemented!!!\n");
2277 printf("multiple buffer types 0x%b\n",
2278 (u_int)(bp->b_xflags & BX_FSPRIV),
2279 PRINT_UFS_BUF_XFLAGS);
2283 g_vfs_strategy(bo, bp);
2287 ffs_own_mount(const struct mount *mp)
2290 if (mp->mnt_op == &ufs_vfsops)
2298 /* defined in ffs_softdep.c */
2299 extern void db_print_ffs(struct ufsmount *ump);
2301 DB_SHOW_COMMAND(ffs, db_show_ffs)
2304 struct ufsmount *ump;
2307 ump = VFSTOUFS((struct mount *)addr);
2312 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2313 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2314 db_print_ffs(VFSTOUFS(mp));
2318 #endif /* SOFTUPDATES */