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/gsb_crc32.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
48 #include <sys/taskqueue.h>
49 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
56 #include <sys/fcntl.h>
57 #include <sys/ioccom.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/rwlock.h>
61 #include <sys/vmmeter.h>
63 #include <security/mac/mac_framework.h>
65 #include <ufs/ufs/dir.h>
66 #include <ufs/ufs/extattr.h>
67 #include <ufs/ufs/gjournal.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufs_extern.h>
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/ffs_extern.h>
78 #include <vm/vm_page.h>
80 #include <geom/geom.h>
81 #include <geom/geom_vfs.h>
85 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
87 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
88 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
90 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
91 static int ffs_sync_lazy(struct mount *mp);
92 static int ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
93 static int ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
95 static vfs_init_t ffs_init;
96 static vfs_uninit_t ffs_uninit;
97 static vfs_extattrctl_t ffs_extattrctl;
98 static vfs_cmount_t ffs_cmount;
99 static vfs_unmount_t ffs_unmount;
100 static vfs_mount_t ffs_mount;
101 static vfs_statfs_t ffs_statfs;
102 static vfs_fhtovp_t ffs_fhtovp;
103 static vfs_sync_t ffs_sync;
105 static struct vfsops ufs_vfsops = {
106 .vfs_extattrctl = ffs_extattrctl,
107 .vfs_fhtovp = ffs_fhtovp,
108 .vfs_init = ffs_init,
109 .vfs_mount = ffs_mount,
110 .vfs_cmount = ffs_cmount,
111 .vfs_quotactl = ufs_quotactl,
112 .vfs_root = vfs_cache_root,
113 .vfs_cachedroot = ufs_root,
114 .vfs_statfs = ffs_statfs,
115 .vfs_sync = ffs_sync,
116 .vfs_uninit = ffs_uninit,
117 .vfs_unmount = ffs_unmount,
118 .vfs_vget = ffs_vget,
119 .vfs_susp_clean = process_deferred_inactive,
122 VFS_SET(ufs_vfsops, ufs, 0);
123 MODULE_VERSION(ufs, 1);
125 static b_strategy_t ffs_geom_strategy;
126 static b_write_t ffs_bufwrite;
128 static struct buf_ops ffs_ops = {
130 .bop_write = ffs_bufwrite,
131 .bop_strategy = ffs_geom_strategy,
133 #ifdef NO_FFS_SNAPSHOT
134 .bop_bdflush = bufbdflush,
136 .bop_bdflush = ffs_bdflush,
141 * Note that userquota and groupquota options are not currently used
142 * by UFS/FFS code and generally mount(8) does not pass those options
143 * from userland, but they can be passed by loader(8) via
144 * vfs.root.mountfrom.options.
146 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
147 "noclusterw", "noexec", "export", "force", "from", "groupquota",
148 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
149 "nosymfollow", "sync", "union", "userquota", "untrusted", NULL };
152 ffs_mount(struct mount *mp)
156 struct ufsmount *ump = NULL;
159 int error, error1, flags;
160 uint64_t mntorflags, saved_mnt_flag;
162 struct nameidata ndp;
166 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
168 if (uma_inode == NULL) {
169 uma_inode = uma_zcreate("FFS inode",
170 sizeof(struct inode), NULL, NULL, NULL, NULL,
172 uma_ufs1 = uma_zcreate("FFS1 dinode",
173 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
175 uma_ufs2 = uma_zcreate("FFS2 dinode",
176 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
180 vfs_deleteopt(mp->mnt_optnew, "groupquota");
181 vfs_deleteopt(mp->mnt_optnew, "userquota");
183 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
188 if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
189 mntorflags |= MNT_UNTRUSTED;
191 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
192 mntorflags |= MNT_ACLS;
194 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
195 mntorflags |= MNT_SNAPSHOT;
197 * Once we have set the MNT_SNAPSHOT flag, do not
198 * persist "snapshot" in the options list.
200 vfs_deleteopt(mp->mnt_optnew, "snapshot");
201 vfs_deleteopt(mp->mnt_opt, "snapshot");
204 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
205 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
207 * Once we have set the restricted PID, do not
208 * persist "fsckpid" in the options list.
210 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
211 vfs_deleteopt(mp->mnt_opt, "fsckpid");
212 if (mp->mnt_flag & MNT_UPDATE) {
213 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
214 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
216 "Checker enable: Must be read-only");
219 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
221 "Checker enable: Must be read-only");
224 /* Set to -1 if we are done */
229 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
230 if (mntorflags & MNT_ACLS) {
232 "\"acls\" and \"nfsv4acls\" options "
233 "are mutually exclusive");
236 mntorflags |= MNT_NFS4ACLS;
240 mp->mnt_flag |= mntorflags;
243 * If updating, check whether changing from read-only to
244 * read/write; if there is no device name, that's all we do.
246 if (mp->mnt_flag & MNT_UPDATE) {
249 devvp = ump->um_devvp;
250 if (fsckpid == -1 && ump->um_fsckpid > 0) {
251 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
252 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
256 * Return to normal read-only mode.
258 error = g_access(ump->um_cp, 0, -1, 0);
262 if (fs->fs_ronly == 0 &&
263 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
265 * Flush any dirty data and suspend filesystem.
267 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
269 error = vfs_write_suspend_umnt(mp);
273 * Check for and optionally get rid of files open
277 if (mp->mnt_flag & MNT_FORCE)
279 if (MOUNTEDSOFTDEP(mp)) {
280 error = softdep_flushfiles(mp, flags, td);
282 error = ffs_flushfiles(mp, flags, td);
285 vfs_write_resume(mp, 0);
288 if (fs->fs_pendingblocks != 0 ||
289 fs->fs_pendinginodes != 0) {
290 printf("WARNING: %s Update error: blocks %jd "
291 "files %d\n", fs->fs_fsmnt,
292 (intmax_t)fs->fs_pendingblocks,
293 fs->fs_pendinginodes);
294 fs->fs_pendingblocks = 0;
295 fs->fs_pendinginodes = 0;
297 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
299 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
302 vfs_write_resume(mp, 0);
305 if (MOUNTEDSOFTDEP(mp))
309 * Drop our write and exclusive access.
311 g_access(ump->um_cp, 0, -1, -1);
315 mp->mnt_flag |= MNT_RDONLY;
318 * Allow the writers to note that filesystem
321 vfs_write_resume(mp, 0);
323 if ((mp->mnt_flag & MNT_RELOAD) &&
324 (error = ffs_reload(mp, td, 0)) != 0)
327 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
329 * If we are running a checker, do not allow upgrade.
331 if (ump->um_fsckpid > 0) {
333 "Active checker, cannot upgrade to write");
337 * If upgrade to read-write by non-root, then verify
338 * that user has necessary permissions on the device.
340 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
341 error = VOP_ACCESS(devvp, VREAD | VWRITE,
344 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
346 VOP_UNLOCK(devvp, 0);
349 VOP_UNLOCK(devvp, 0);
350 fs->fs_flags &= ~FS_UNCLEAN;
351 if (fs->fs_clean == 0) {
352 fs->fs_flags |= FS_UNCLEAN;
353 if ((mp->mnt_flag & MNT_FORCE) ||
355 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
356 (fs->fs_flags & FS_DOSOFTDEP))) {
357 printf("WARNING: %s was not properly "
358 "dismounted\n", fs->fs_fsmnt);
361 "R/W mount of %s denied. %s.%s",
363 "Filesystem is not clean - run fsck",
364 (fs->fs_flags & FS_SUJ) == 0 ? "" :
365 " Forced mount will invalidate"
366 " journal contents");
372 * Request exclusive write access.
374 error = g_access(ump->um_cp, 0, 1, 1);
378 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
380 error = vfs_write_suspend_umnt(mp);
385 saved_mnt_flag = MNT_RDONLY;
386 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
388 saved_mnt_flag |= MNT_ASYNC;
389 mp->mnt_flag &= ~saved_mnt_flag;
391 fs->fs_mtime = time_second;
392 /* check to see if we need to start softdep */
393 if ((fs->fs_flags & FS_DOSOFTDEP) &&
394 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
397 mp->mnt_flag |= saved_mnt_flag;
399 vfs_write_resume(mp, 0);
403 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
406 mp->mnt_flag |= saved_mnt_flag;
408 vfs_write_resume(mp, 0);
411 if (fs->fs_snapinum[0] != 0)
412 ffs_snapshot_mount(mp);
413 vfs_write_resume(mp, 0);
416 * Soft updates is incompatible with "async",
417 * so if we are doing softupdates stop the user
418 * from setting the async flag in an update.
419 * Softdep_mount() clears it in an initial mount
422 if (MOUNTEDSOFTDEP(mp)) {
423 /* XXX: Reset too late ? */
425 mp->mnt_flag &= ~MNT_ASYNC;
429 * Keep MNT_ACLS flag if it is stored in superblock.
431 if ((fs->fs_flags & FS_ACLS) != 0) {
432 /* XXX: Set too late ? */
434 mp->mnt_flag |= MNT_ACLS;
438 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
439 /* XXX: Set too late ? */
441 mp->mnt_flag |= MNT_NFS4ACLS;
445 * If this is a request from fsck to clean up the filesystem,
446 * then allow the specified pid to proceed.
449 if (ump->um_fsckpid != 0) {
451 "Active checker already running on %s",
455 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
456 ("soft updates enabled on read-only file system"));
459 * Request write access.
461 error = g_access(ump->um_cp, 0, 1, 0);
465 "Checker activation failed on %s",
469 ump->um_fsckpid = fsckpid;
470 if (fs->fs_snapinum[0] != 0)
471 ffs_snapshot_mount(mp);
472 fs->fs_mtime = time_second;
475 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
479 * If this is a snapshot request, take the snapshot.
481 if (mp->mnt_flag & MNT_SNAPSHOT)
482 return (ffs_snapshot(mp, fspec));
485 * Must not call namei() while owning busy ref.
491 * Not an update, or updating the name: look up the name
492 * and verify that it refers to a sensible disk device.
494 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
496 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
498 * Unmount does not start if MNT_UPDATE is set. Mount
499 * update busies mp before setting MNT_UPDATE. We
500 * must be able to retain our busy ref succesfully,
503 error1 = vfs_busy(mp, MBF_NOWAIT);
508 NDFREE(&ndp, NDF_ONLY_PNBUF);
510 if (!vn_isdisk(devvp, &error)) {
516 * If mount by non-root, then verify that user has necessary
517 * permissions on the device.
520 if ((mp->mnt_flag & MNT_RDONLY) == 0)
522 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
524 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
530 if (mp->mnt_flag & MNT_UPDATE) {
534 * If it's not the same vnode, or at least the same device
535 * then it's not correct.
538 if (devvp->v_rdev != ump->um_devvp->v_rdev)
539 error = EINVAL; /* needs translation */
547 * We need the name for the mount point (also used for
548 * "last mounted on") copied in. If an error occurs,
549 * the mount point is discarded by the upper level code.
550 * Note that vfs_mount_alloc() populates f_mntonname for us.
552 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
557 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
558 ("soft updates enabled on read-only file system"));
563 * Request write access.
565 error = g_access(ump->um_cp, 0, 1, 0);
568 printf("WARNING: %s: Checker activation "
569 "failed\n", fs->fs_fsmnt);
571 ump->um_fsckpid = fsckpid;
572 if (fs->fs_snapinum[0] != 0)
573 ffs_snapshot_mount(mp);
574 fs->fs_mtime = time_second;
576 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
580 vfs_mountedfrom(mp, fspec);
585 * Compatibility with old mount system call.
589 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
591 struct ufs_args args;
592 struct export_args exp;
597 error = copyin(data, &args, sizeof args);
600 vfs_oexport_conv(&args.export, &exp);
602 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
603 ma = mount_arg(ma, "export", &exp, sizeof(exp));
604 error = kernel_mount(ma, flags);
610 * Reload all incore data for a filesystem (used after running fsck on
611 * the root filesystem and finding things to fix). If the 'force' flag
612 * is 0, the filesystem must be mounted read-only.
614 * Things to do to update the mount:
615 * 1) invalidate all cached meta-data.
616 * 2) re-read superblock from disk.
617 * 3) re-read summary information from disk.
618 * 4) invalidate all inactive vnodes.
619 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
620 * writers, if requested.
621 * 6) invalidate all cached file data.
622 * 7) re-read inode data for all active vnodes.
625 ffs_reload(struct mount *mp, struct thread *td, int flags)
627 struct vnode *vp, *mvp, *devvp;
631 struct fs *fs, *newfs;
632 struct ufsmount *ump;
633 ufs2_daddr_t sblockloc;
641 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
648 * Step 1: invalidate all cached meta-data.
650 devvp = VFSTOUFS(mp)->um_devvp;
651 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
652 if (vinvalbuf(devvp, 0, 0, 0) != 0)
653 panic("ffs_reload: dirty1");
654 VOP_UNLOCK(devvp, 0);
657 * Step 2: re-read superblock from disk.
659 fs = VFSTOUFS(mp)->um_fs;
660 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
663 newfs = (struct fs *)bp->b_data;
664 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
665 newfs->fs_magic != FS_UFS2_MAGIC) ||
666 newfs->fs_bsize > MAXBSIZE ||
667 newfs->fs_bsize < sizeof(struct fs)) {
669 return (EIO); /* XXX needs translation */
672 * Copy pointer fields back into superblock before copying in XXX
673 * new superblock. These should really be in the ufsmount. XXX
674 * Note that important parameters (eg fs_ncg) are unchanged.
676 newfs->fs_csp = fs->fs_csp;
677 newfs->fs_maxcluster = fs->fs_maxcluster;
678 newfs->fs_contigdirs = fs->fs_contigdirs;
679 newfs->fs_active = fs->fs_active;
680 newfs->fs_ronly = fs->fs_ronly;
681 sblockloc = fs->fs_sblockloc;
682 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
684 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
685 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
687 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
688 printf("WARNING: %s: reload pending error: blocks %jd "
689 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
690 fs->fs_pendinginodes);
691 fs->fs_pendingblocks = 0;
692 fs->fs_pendinginodes = 0;
697 * Step 3: re-read summary information from disk.
699 size = fs->fs_cssize;
700 blks = howmany(size, fs->fs_fsize);
701 if (fs->fs_contigsumsize > 0)
702 size += fs->fs_ncg * sizeof(int32_t);
703 size += fs->fs_ncg * sizeof(u_int8_t);
704 free(fs->fs_csp, M_UFSMNT);
705 space = malloc(size, M_UFSMNT, M_WAITOK);
707 for (i = 0; i < blks; i += fs->fs_frag) {
709 if (i + fs->fs_frag > blks)
710 size = (blks - i) * fs->fs_fsize;
711 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
715 bcopy(bp->b_data, space, (u_int)size);
716 space = (char *)space + size;
720 * We no longer know anything about clusters per cylinder group.
722 if (fs->fs_contigsumsize > 0) {
723 fs->fs_maxcluster = lp = space;
724 for (i = 0; i < fs->fs_ncg; i++)
725 *lp++ = fs->fs_contigsumsize;
728 size = fs->fs_ncg * sizeof(u_int8_t);
729 fs->fs_contigdirs = (u_int8_t *)space;
730 bzero(fs->fs_contigdirs, size);
731 if ((flags & FFSR_UNSUSPEND) != 0) {
733 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
734 wakeup(&mp->mnt_flag);
739 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
743 if (vp->v_type == VNON) {
748 * Step 4: invalidate all cached file data.
750 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
751 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
754 if (vinvalbuf(vp, 0, 0, 0))
755 panic("ffs_reload: dirty2");
757 * Step 5: re-read inode data for all active vnodes.
761 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
762 (int)fs->fs_bsize, NOCRED, &bp);
765 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
768 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
771 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
774 ip->i_effnlink = ip->i_nlink;
782 * Common code for mount and mountroot
785 ffs_mountfs(devvp, mp, td)
790 struct ufsmount *ump;
793 int error, i, len, ronly;
795 struct g_consumer *cp;
802 cred = td ? td->td_ucred : NOCRED;
803 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
805 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
807 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
808 (uintptr_t)mp) == 0) {
809 VOP_UNLOCK(devvp, 0);
813 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
816 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
817 VOP_UNLOCK(devvp, 0);
821 devvp->v_bufobj.bo_ops = &ffs_ops;
822 VOP_UNLOCK(devvp, 0);
823 if (dev->si_iosize_max != 0)
824 mp->mnt_iosize_max = dev->si_iosize_max;
825 if (mp->mnt_iosize_max > MAXPHYS)
826 mp->mnt_iosize_max = MAXPHYS;
827 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
830 "Invalid sectorsize %d for superblock size %d",
831 cp->provider->sectorsize, SBLOCKSIZE);
834 /* fetch the superblock and summary information */
836 if ((mp->mnt_flag & MNT_ROOTFS) != 0)
837 loc = STDSB_NOHASHFAIL;
838 if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
840 /* none of these types of check-hashes are maintained by this kernel */
841 fs->fs_metackhash &= ~(CK_INDIR | CK_DIR);
842 /* no support for any undefined flags */
843 fs->fs_flags &= FS_SUPPORTED;
844 fs->fs_flags &= ~FS_UNCLEAN;
845 if (fs->fs_clean == 0) {
846 fs->fs_flags |= FS_UNCLEAN;
847 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
848 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
849 (fs->fs_flags & FS_DOSOFTDEP))) {
850 printf("WARNING: %s was not properly dismounted\n",
853 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
854 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
855 (fs->fs_flags & FS_SUJ) == 0 ? "" :
856 " Forced mount will invalidate journal contents");
860 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
861 (mp->mnt_flag & MNT_FORCE)) {
862 printf("WARNING: %s: lost blocks %jd files %d\n",
863 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
864 fs->fs_pendinginodes);
865 fs->fs_pendingblocks = 0;
866 fs->fs_pendinginodes = 0;
869 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
870 printf("WARNING: %s: mount pending error: blocks %jd "
871 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
872 fs->fs_pendinginodes);
873 fs->fs_pendingblocks = 0;
874 fs->fs_pendinginodes = 0;
876 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
879 * Get journal provider name.
882 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
883 if (g_io_getattr("GJOURNAL::provider", cp, &len,
884 mp->mnt_gjprovider) == 0) {
885 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
888 mp->mnt_flag |= MNT_GJOURNAL;
891 printf("WARNING: %s: GJOURNAL flag on fs "
892 "but no gjournal provider below\n",
893 mp->mnt_stat.f_mntonname);
894 free(mp->mnt_gjprovider, M_UFSMNT);
895 mp->mnt_gjprovider = NULL;
898 printf("WARNING: %s: GJOURNAL flag on fs but no "
899 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
902 mp->mnt_gjprovider = NULL;
904 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
906 ump->um_bo = &devvp->v_bufobj;
908 if (fs->fs_magic == FS_UFS1_MAGIC) {
909 ump->um_fstype = UFS1;
910 ump->um_balloc = ffs_balloc_ufs1;
912 ump->um_fstype = UFS2;
913 ump->um_balloc = ffs_balloc_ufs2;
915 ump->um_blkatoff = ffs_blkatoff;
916 ump->um_truncate = ffs_truncate;
917 ump->um_update = ffs_update;
918 ump->um_valloc = ffs_valloc;
919 ump->um_vfree = ffs_vfree;
920 ump->um_ifree = ffs_ifree;
921 ump->um_rdonly = ffs_rdonly;
922 ump->um_snapgone = ffs_snapgone;
923 if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
924 ump->um_check_blkno = ffs_check_blkno;
926 ump->um_check_blkno = NULL;
927 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
928 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
929 fs->fs_ronly = ronly;
930 fs->fs_active = NULL;
932 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
933 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
935 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
936 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
941 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
943 mp->mnt_flag |= MNT_LOCAL;
945 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
948 mp->mnt_flag |= MNT_MULTILABEL;
951 printf("WARNING: %s: multilabel flag on fs but "
952 "no MAC support\n", mp->mnt_stat.f_mntonname);
955 if ((fs->fs_flags & FS_ACLS) != 0) {
959 if (mp->mnt_flag & MNT_NFS4ACLS)
960 printf("WARNING: %s: ACLs flag on fs conflicts with "
961 "\"nfsv4acls\" mount option; option ignored\n",
962 mp->mnt_stat.f_mntonname);
963 mp->mnt_flag &= ~MNT_NFS4ACLS;
964 mp->mnt_flag |= MNT_ACLS;
968 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
969 mp->mnt_stat.f_mntonname);
972 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
976 if (mp->mnt_flag & MNT_ACLS)
977 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
978 "with \"acls\" mount option; option ignored\n",
979 mp->mnt_stat.f_mntonname);
980 mp->mnt_flag &= ~MNT_ACLS;
981 mp->mnt_flag |= MNT_NFS4ACLS;
985 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
986 "ACLs support\n", mp->mnt_stat.f_mntonname);
989 if ((fs->fs_flags & FS_TRIM) != 0) {
991 if (g_io_getattr("GEOM::candelete", cp, &len,
994 ump->um_flags |= UM_CANDELETE;
996 printf("WARNING: %s: TRIM flag on fs but disk "
997 "does not support TRIM\n",
998 mp->mnt_stat.f_mntonname);
1000 printf("WARNING: %s: TRIM flag on fs but disk does "
1001 "not confirm that it supports TRIM\n",
1002 mp->mnt_stat.f_mntonname);
1004 if (((ump->um_flags) & UM_CANDELETE) != 0) {
1005 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1006 taskqueue_thread_enqueue, &ump->um_trim_tq);
1007 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1008 "%s trim", mp->mnt_stat.f_mntonname);
1009 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1010 &ump->um_trimlisthashsize);
1014 ump->um_mountp = mp;
1016 ump->um_devvp = devvp;
1017 ump->um_nindir = fs->fs_nindir;
1018 ump->um_bptrtodb = fs->fs_fsbtodb;
1019 ump->um_seqinc = fs->fs_frag;
1020 for (i = 0; i < MAXQUOTAS; i++)
1021 ump->um_quotas[i] = NULLVP;
1023 ufs_extattr_uepm_init(&ump->um_extattr);
1026 * Set FS local "last mounted on" information (NULL pad)
1028 bzero(fs->fs_fsmnt, MAXMNTLEN);
1029 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1030 mp->mnt_stat.f_iosize = fs->fs_bsize;
1032 if (mp->mnt_flag & MNT_ROOTFS) {
1034 * Root mount; update timestamp in mount structure.
1035 * this will be used by the common root mount code
1036 * to update the system clock.
1038 mp->mnt_time = fs->fs_time;
1042 fs->fs_mtime = time_second;
1043 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1044 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1045 ffs_flushfiles(mp, FORCECLOSE, td);
1048 if (fs->fs_snapinum[0] != 0)
1049 ffs_snapshot_mount(mp);
1052 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1055 * Initialize filesystem state information in mount struct.
1058 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1059 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1062 #ifdef UFS_EXTATTR_AUTOSTART
1065 * Auto-starting does the following:
1066 * - check for /.attribute in the fs, and extattr_start if so
1067 * - for each file in .attribute, enable that file with
1068 * an attribute of the same name.
1069 * Not clear how to report errors -- probably eat them.
1070 * This would all happen while the filesystem was busy/not
1071 * available, so would effectively be "atomic".
1073 (void) ufs_extattr_autostart(mp, td);
1074 #endif /* !UFS_EXTATTR_AUTOSTART */
1075 #endif /* !UFS_EXTATTR */
1079 free(fs->fs_csp, M_UFSMNT);
1085 g_topology_unlock();
1088 mtx_destroy(UFS_MTX(ump));
1089 if (mp->mnt_gjprovider != NULL) {
1090 free(mp->mnt_gjprovider, M_UFSMNT);
1091 mp->mnt_gjprovider = NULL;
1093 free(ump, M_UFSMNT);
1094 mp->mnt_data = NULL;
1096 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1102 * A read function for use by filesystem-layer routines.
1105 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1110 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1111 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1112 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1115 bcopy(bp->b_data, *bufp, size);
1116 bp->b_flags |= B_INVAL | B_NOCACHE;
1121 #include <sys/sysctl.h>
1122 static int bigcgs = 0;
1123 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1126 * Sanity checks for loading old filesystem superblocks.
1127 * See ffs_oldfscompat_write below for unwound actions.
1129 * XXX - Parts get retired eventually.
1130 * Unfortunately new bits get added.
1133 ffs_oldfscompat_read(fs, ump, sblockloc)
1135 struct ufsmount *ump;
1136 ufs2_daddr_t sblockloc;
1141 * If not yet done, update fs_flags location and value of fs_sblockloc.
1143 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1144 fs->fs_flags = fs->fs_old_flags;
1145 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1146 fs->fs_sblockloc = sblockloc;
1149 * If not yet done, update UFS1 superblock with new wider fields.
1151 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1152 fs->fs_maxbsize = fs->fs_bsize;
1153 fs->fs_time = fs->fs_old_time;
1154 fs->fs_size = fs->fs_old_size;
1155 fs->fs_dsize = fs->fs_old_dsize;
1156 fs->fs_csaddr = fs->fs_old_csaddr;
1157 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1158 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1159 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1160 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1162 if (fs->fs_magic == FS_UFS1_MAGIC &&
1163 fs->fs_old_inodefmt < FS_44INODEFMT) {
1164 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1165 fs->fs_qbmask = ~fs->fs_bmask;
1166 fs->fs_qfmask = ~fs->fs_fmask;
1168 if (fs->fs_magic == FS_UFS1_MAGIC) {
1169 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1170 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1171 if (fs->fs_maxfilesize > maxfilesize)
1172 fs->fs_maxfilesize = maxfilesize;
1174 /* Compatibility for old filesystems */
1175 if (fs->fs_avgfilesize <= 0)
1176 fs->fs_avgfilesize = AVFILESIZ;
1177 if (fs->fs_avgfpdir <= 0)
1178 fs->fs_avgfpdir = AFPDIR;
1180 fs->fs_save_cgsize = fs->fs_cgsize;
1181 fs->fs_cgsize = fs->fs_bsize;
1186 * Unwinding superblock updates for old filesystems.
1187 * See ffs_oldfscompat_read above for details.
1189 * XXX - Parts get retired eventually.
1190 * Unfortunately new bits get added.
1193 ffs_oldfscompat_write(fs, ump)
1195 struct ufsmount *ump;
1199 * Copy back UFS2 updated fields that UFS1 inspects.
1201 if (fs->fs_magic == FS_UFS1_MAGIC) {
1202 fs->fs_old_time = fs->fs_time;
1203 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1204 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1205 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1206 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1207 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1210 fs->fs_cgsize = fs->fs_save_cgsize;
1211 fs->fs_save_cgsize = 0;
1216 * unmount system call
1219 ffs_unmount(mp, mntflags)
1224 struct ufsmount *ump = VFSTOUFS(mp);
1226 int error, flags, susp;
1235 if (mntflags & MNT_FORCE) {
1236 flags |= FORCECLOSE;
1237 susp = fs->fs_ronly == 0;
1240 if ((error = ufs_extattr_stop(mp, td))) {
1241 if (error != EOPNOTSUPP)
1242 printf("WARNING: unmount %s: ufs_extattr_stop "
1243 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1247 ufs_extattr_uepm_destroy(&ump->um_extattr);
1252 error = vfs_write_suspend_umnt(mp);
1256 if (MOUNTEDSOFTDEP(mp))
1257 error = softdep_flushfiles(mp, flags, td);
1259 error = ffs_flushfiles(mp, flags, td);
1260 if (error != 0 && error != ENXIO)
1264 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1265 printf("WARNING: unmount %s: pending error: blocks %jd "
1266 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1267 fs->fs_pendinginodes);
1268 fs->fs_pendingblocks = 0;
1269 fs->fs_pendinginodes = 0;
1272 if (MOUNTEDSOFTDEP(mp))
1273 softdep_unmount(mp);
1274 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1275 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1276 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1277 if (error && error != ENXIO) {
1283 vfs_write_resume(mp, VR_START_WRITE);
1284 if (ump->um_trim_tq != NULL) {
1285 while (ump->um_trim_inflight != 0)
1286 pause("ufsutr", hz);
1287 taskqueue_drain_all(ump->um_trim_tq);
1288 taskqueue_free(ump->um_trim_tq);
1289 free (ump->um_trimhash, M_TRIM);
1292 if (ump->um_fsckpid > 0) {
1294 * Return to normal read-only mode.
1296 error = g_access(ump->um_cp, 0, -1, 0);
1297 ump->um_fsckpid = 0;
1299 g_vfs_close(ump->um_cp);
1300 g_topology_unlock();
1301 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1302 vrele(ump->um_devvp);
1303 dev_rel(ump->um_dev);
1304 mtx_destroy(UFS_MTX(ump));
1305 if (mp->mnt_gjprovider != NULL) {
1306 free(mp->mnt_gjprovider, M_UFSMNT);
1307 mp->mnt_gjprovider = NULL;
1309 free(fs->fs_csp, M_UFSMNT);
1311 free(ump, M_UFSMNT);
1312 mp->mnt_data = NULL;
1314 mp->mnt_flag &= ~MNT_LOCAL;
1316 if (td->td_su == mp) {
1324 vfs_write_resume(mp, VR_START_WRITE);
1328 ufs_extattr_uepm_init(&ump->um_extattr);
1329 #ifdef UFS_EXTATTR_AUTOSTART
1330 (void) ufs_extattr_autostart(mp, td);
1339 * Flush out all the files in a filesystem.
1342 ffs_flushfiles(mp, flags, td)
1347 struct ufsmount *ump;
1353 if (mp->mnt_flag & MNT_QUOTA) {
1355 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1358 for (i = 0; i < MAXQUOTAS; i++) {
1359 error = quotaoff(td, mp, i);
1361 if ((flags & EARLYFLUSH) == 0)
1369 * Here we fall through to vflush again to ensure that
1370 * we have gotten rid of all the system vnodes, unless
1371 * quotas must not be closed.
1375 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1376 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1377 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1379 ffs_snapshot_unmount(mp);
1380 flags |= FORCECLOSE;
1382 * Here we fall through to vflush again to ensure
1383 * that we have gotten rid of all the system vnodes.
1388 * Do not close system files if quotas were not closed, to be
1389 * able to sync the remaining dquots. The freeblks softupdate
1390 * workitems might hold a reference on a dquot, preventing
1391 * quotaoff() from completing. Next round of
1392 * softdep_flushworklist() iteration should process the
1393 * blockers, allowing the next run of quotaoff() to finally
1394 * flush held dquots.
1396 * Otherwise, flush all the files.
1398 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1402 * Flush filesystem metadata.
1404 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1405 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1406 VOP_UNLOCK(ump->um_devvp, 0);
1411 * Get filesystem statistics.
1418 struct ufsmount *ump;
1423 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1424 panic("ffs_statfs");
1425 sbp->f_version = STATFS_VERSION;
1426 sbp->f_bsize = fs->fs_fsize;
1427 sbp->f_iosize = fs->fs_bsize;
1428 sbp->f_blocks = fs->fs_dsize;
1430 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1431 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1432 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1433 dbtofsb(fs, fs->fs_pendingblocks);
1434 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1435 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1437 sbp->f_namemax = UFS_MAXNAMLEN;
1442 sync_doupdate(struct inode *ip)
1445 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1450 * For a lazy sync, we only care about access times, quotas and the
1451 * superblock. Other filesystem changes are already converted to
1452 * cylinder group blocks or inode blocks updates and are written to
1459 struct vnode *mvp, *vp;
1462 int allerror, error;
1466 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1468 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1469 if (vp->v_type == VNON) {
1476 * The IN_ACCESS flag is converted to IN_MODIFIED by
1477 * ufs_close() and ufs_getattr() by the calls to
1478 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1479 * Test also all the other timestamp flags too, to pick up
1480 * any other cases that could be missed.
1482 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1486 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1489 if (sync_doupdate(ip))
1490 error = ffs_update(vp, 0);
1501 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1502 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1508 * Go through the disk queues to initiate sandbagged IO;
1509 * go through the inodes to write those that have been modified;
1510 * initiate the writing of the super block if it has been modified.
1512 * Note: we are always called with the filesystem marked busy using
1516 ffs_sync(mp, waitfor)
1520 struct vnode *mvp, *vp, *devvp;
1523 struct ufsmount *ump = VFSTOUFS(mp);
1525 int error, count, lockreq, allerror = 0;
1528 int secondary_writes;
1529 int secondary_accwrites;
1531 int softdep_accdeps;
1538 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1539 panic("%s: ffs_sync: modification on read-only filesystem",
1541 if (waitfor == MNT_LAZY) {
1543 return (ffs_sync_lazy(mp));
1544 waitfor = MNT_NOWAIT;
1548 * Write back each (modified) inode.
1550 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1551 if (waitfor == MNT_SUSPEND) {
1555 if (waitfor == MNT_WAIT)
1556 lockreq = LK_EXCLUSIVE;
1557 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1559 /* Grab snapshot of secondary write counts */
1561 secondary_writes = mp->mnt_secondary_writes;
1562 secondary_accwrites = mp->mnt_secondary_accwrites;
1565 /* Grab snapshot of softdep dependency counts */
1566 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1568 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1570 * Depend on the vnode interlock to keep things stable enough
1571 * for a quick test. Since there might be hundreds of
1572 * thousands of vnodes, we cannot afford even a subroutine
1573 * call unless there's a good chance that we have work to do.
1575 if (vp->v_type == VNON) {
1581 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1582 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1586 if ((error = vget(vp, lockreq, td)) != 0) {
1587 if (error == ENOENT || error == ENOLCK) {
1588 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1593 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1598 * Force stale filesystem control information to be flushed.
1600 if (waitfor == MNT_WAIT || rebooting) {
1601 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1603 /* Flushed work items may create new vnodes to clean */
1604 if (allerror == 0 && count)
1611 devvp = ump->um_devvp;
1612 bo = &devvp->v_bufobj;
1614 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1616 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1617 error = VOP_FSYNC(devvp, waitfor, td);
1618 VOP_UNLOCK(devvp, 0);
1619 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1620 error = ffs_sbupdate(ump, waitfor, 0);
1623 if (allerror == 0 && waitfor == MNT_WAIT)
1625 } else if (suspend != 0) {
1626 if (softdep_check_suspend(mp,
1631 secondary_accwrites) != 0) {
1633 goto loop; /* More work needed */
1635 mtx_assert(MNT_MTX(mp), MA_OWNED);
1636 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1642 * Write back modified superblock.
1644 if (fs->fs_fmod != 0 &&
1645 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1651 ffs_vget(mp, ino, flags, vpp)
1657 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1661 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1670 struct ufsmount *ump;
1675 MPASS((ffs_flags & FFSV_REPLACE) == 0 || (flags & LK_EXCLUSIVE) != 0);
1677 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1681 if ((ffs_flags & FFSV_REPLACE) == 0)
1688 * We must promote to an exclusive lock for vnode creation. This
1689 * can happen if lookup is passed LOCKSHARED.
1691 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1692 flags &= ~LK_TYPE_MASK;
1693 flags |= LK_EXCLUSIVE;
1697 * We do not lock vnode creation as it is believed to be too
1698 * expensive for such rare case as simultaneous creation of vnode
1699 * for same ino by different processes. We just allow them to race
1700 * and check later to decide who wins. Let the race begin!
1705 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1707 /* Allocate a new vnode/inode. */
1708 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1709 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1712 uma_zfree(uma_inode, ip);
1716 * FFS supports recursive locking.
1718 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1721 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1726 ip->i_nextclustercg = -1;
1727 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1728 ip->i_mode = 0; /* ensure error cases below throw away vnode */
1732 for (i = 0; i < MAXQUOTAS; i++)
1733 ip->i_dquot[i] = NODQUOT;
1737 if (ffs_flags & FFSV_FORCEINSMQ)
1738 vp->v_vflag |= VV_FORCEINSMQ;
1739 error = insmntque(vp, mp);
1741 uma_zfree(uma_inode, ip);
1745 vp->v_vflag &= ~VV_FORCEINSMQ;
1746 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1751 * Calls from ffs_valloc() (i.e. FFSV_REPLACE set)
1752 * operate on empty inode, which must not be found by
1753 * other threads until fully filled. Vnode for empty
1754 * inode must be not re-inserted on the hash by other
1755 * thread, after removal by us at the beginning.
1757 MPASS((ffs_flags & FFSV_REPLACE) == 0);
1761 /* Read in the disk contents for the inode, copy into the inode. */
1762 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1763 (int)fs->fs_bsize, NOCRED, &bp);
1766 * The inode does not contain anything useful, so it would
1767 * be misleading to leave it on its hash chain. With mode
1768 * still zero, it will be unlinked and returned to the free
1776 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1778 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1779 if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
1785 if (DOINGSOFTDEP(vp))
1786 softdep_load_inodeblock(ip);
1788 ip->i_effnlink = ip->i_nlink;
1792 * Initialize the vnode from the inode, check for aliases.
1793 * Note that the underlying vnode may have changed.
1795 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1804 * Finish inode initialization.
1806 if (vp->v_type != VFIFO) {
1807 /* FFS supports shared locking for all files except fifos. */
1812 * Set up a generation number for this inode if it does not
1813 * already have one. This should only happen on old filesystems.
1815 if (ip->i_gen == 0) {
1816 while (ip->i_gen == 0)
1817 ip->i_gen = arc4random();
1818 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1819 ip->i_flag |= IN_MODIFIED;
1820 DIP_SET(ip, i_gen, ip->i_gen);
1824 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1826 * If this vnode is already allocated, and we're running
1827 * multi-label, attempt to perform a label association
1828 * from the extended attributes on the inode.
1830 error = mac_vnode_associate_extattr(mp, vp);
1832 /* ufs_inactive will release ip->i_devvp ref. */
1845 * File handle to vnode
1847 * Have to be really careful about stale file handles:
1848 * - check that the inode number is valid
1849 * - for UFS2 check that the inode number is initialized
1850 * - call ffs_vget() to get the locked inode
1851 * - check for an unallocated inode (i_mode == 0)
1852 * - check that the given client host has export rights and return
1853 * those rights via. exflagsp and credanonp
1856 ffs_fhtovp(mp, fhp, flags, vpp)
1863 struct ufsmount *ump;
1871 ufhp = (struct ufid *)fhp;
1872 ino = ufhp->ufid_ino;
1875 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1878 * Need to check if inode is initialized because UFS2 does lazy
1879 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1881 if (fs->fs_magic != FS_UFS2_MAGIC)
1882 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1883 cg = ino_to_cg(fs, ino);
1884 if ((error = ffs_getcg(fs, ump->um_devvp, cg, 0, &bp, &cgp)) != 0)
1886 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1891 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1895 * Initialize the filesystem.
1899 struct vfsconf *vfsp;
1902 ffs_susp_initialize();
1903 softdep_initialize();
1904 return (ufs_init(vfsp));
1908 * Undo the work of ffs_init().
1912 struct vfsconf *vfsp;
1916 ret = ufs_uninit(vfsp);
1917 softdep_uninitialize();
1918 ffs_susp_uninitialize();
1923 * Structure used to pass information from ffs_sbupdate to its
1924 * helper routine ffs_use_bwrite.
1927 struct ufsmount *ump;
1935 * Write a superblock and associated information back to disk.
1938 ffs_sbupdate(ump, waitfor, suspended)
1939 struct ufsmount *ump;
1948 if (fs->fs_ronly == 1 &&
1949 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1950 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1951 panic("ffs_sbupdate: write read-only filesystem");
1953 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1955 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1956 (int)fs->fs_sbsize, 0, 0, 0);
1958 * Initialize info needed for write function.
1962 devfd.waitfor = waitfor;
1963 devfd.suspended = suspended;
1965 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
1969 * Write function for use by filesystem-layer routines.
1972 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
1974 struct devfd *devfdp;
1975 struct ufsmount *ump;
1984 * Writing the superblock summary information.
1986 if (loc != fs->fs_sblockloc) {
1987 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
1988 bcopy(buf, bp->b_data, (u_int)size);
1989 if (devfdp->suspended)
1990 bp->b_flags |= B_VALIDSUSPWRT;
1991 if (devfdp->waitfor != MNT_WAIT)
1993 else if ((error = bwrite(bp)) != 0)
1994 devfdp->error = error;
1998 * Writing the superblock itself. We need to do special checks for it.
2001 if (devfdp->error != 0) {
2003 return (devfdp->error);
2005 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
2006 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2007 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2008 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
2009 fs->fs_sblockloc = SBLOCK_UFS1;
2011 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
2012 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2013 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2014 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
2015 fs->fs_sblockloc = SBLOCK_UFS2;
2017 if (MOUNTEDSOFTDEP(ump->um_mountp))
2018 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
2019 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
2020 fs = (struct fs *)bp->b_data;
2021 ffs_oldfscompat_write(fs, ump);
2023 * Because we may have made changes to the superblock, we need to
2024 * recompute its check-hash.
2026 fs->fs_ckhash = ffs_calc_sbhash(fs);
2027 if (devfdp->suspended)
2028 bp->b_flags |= B_VALIDSUSPWRT;
2029 if (devfdp->waitfor != MNT_WAIT)
2031 else if ((error = bwrite(bp)) != 0)
2032 devfdp->error = error;
2033 return (devfdp->error);
2037 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2038 int attrnamespace, const char *attrname)
2042 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2045 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2051 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2054 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2055 uma_zfree(uma_ufs1, ip->i_din1);
2056 else if (ip->i_din2 != NULL)
2057 uma_zfree(uma_ufs2, ip->i_din2);
2058 uma_zfree(uma_inode, ip);
2061 static int dobkgrdwrite = 1;
2062 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2063 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2066 * Complete a background write started from bwrite.
2069 ffs_backgroundwritedone(struct buf *bp)
2071 struct bufobj *bufobj;
2075 * Find the original buffer that we are writing.
2077 bufobj = bp->b_bufobj;
2079 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2080 panic("backgroundwritedone: lost buffer");
2083 * We should mark the cylinder group buffer origbp as
2084 * dirty, to not loose the failed write.
2086 if ((bp->b_ioflags & BIO_ERROR) != 0)
2087 origbp->b_vflags |= BV_BKGRDERR;
2090 * Process dependencies then return any unfinished ones.
2092 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2095 if (!LIST_EMPTY(&bp->b_dep))
2096 softdep_move_dependencies(bp, origbp);
2099 * This buffer is marked B_NOCACHE so when it is released
2100 * by biodone it will be tossed.
2102 bp->b_flags |= B_NOCACHE;
2103 bp->b_flags &= ~B_CACHE;
2107 * Prevent brelse() from trying to keep and re-dirtying bp on
2108 * errors. It causes b_bufobj dereference in
2109 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2112 if ((bp->b_ioflags & BIO_ERROR) != 0)
2113 bp->b_flags |= B_INVAL;
2117 * Clear the BV_BKGRDINPROG flag in the original buffer
2118 * and awaken it if it is waiting for the write to complete.
2119 * If BV_BKGRDINPROG is not set in the original buffer it must
2120 * have been released and re-instantiated - which is not legal.
2122 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2123 ("backgroundwritedone: lost buffer2"));
2124 origbp->b_vflags &= ~BV_BKGRDINPROG;
2125 if (origbp->b_vflags & BV_BKGRDWAIT) {
2126 origbp->b_vflags &= ~BV_BKGRDWAIT;
2127 wakeup(&origbp->b_xflags);
2134 * Write, release buffer on completion. (Done by iodone
2135 * if async). Do not bother writing anything if the buffer
2138 * Note that we set B_CACHE here, indicating that buffer is
2139 * fully valid and thus cacheable. This is true even of NFS
2140 * now so we set it generally. This could be set either here
2141 * or in biodone() since the I/O is synchronous. We put it
2145 ffs_bufwrite(struct buf *bp)
2150 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2151 if (bp->b_flags & B_INVAL) {
2156 if (!BUF_ISLOCKED(bp))
2157 panic("bufwrite: buffer is not busy???");
2159 * If a background write is already in progress, delay
2160 * writing this block if it is asynchronous. Otherwise
2161 * wait for the background write to complete.
2163 BO_LOCK(bp->b_bufobj);
2164 if (bp->b_vflags & BV_BKGRDINPROG) {
2165 if (bp->b_flags & B_ASYNC) {
2166 BO_UNLOCK(bp->b_bufobj);
2170 bp->b_vflags |= BV_BKGRDWAIT;
2171 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2173 if (bp->b_vflags & BV_BKGRDINPROG)
2174 panic("bufwrite: still writing");
2176 bp->b_vflags &= ~BV_BKGRDERR;
2177 BO_UNLOCK(bp->b_bufobj);
2180 * If this buffer is marked for background writing and we
2181 * do not have to wait for it, make a copy and write the
2182 * copy so as to leave this buffer ready for further use.
2184 * This optimization eats a lot of memory. If we have a page
2185 * or buffer shortfall we can't do it.
2187 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2188 (bp->b_flags & B_ASYNC) &&
2189 !vm_page_count_severe() &&
2190 !buf_dirty_count_severe()) {
2191 KASSERT(bp->b_iodone == NULL,
2192 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2194 /* get a new block */
2195 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2199 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2200 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2201 BO_LOCK(bp->b_bufobj);
2202 bp->b_vflags |= BV_BKGRDINPROG;
2203 BO_UNLOCK(bp->b_bufobj);
2205 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2206 newbp->b_lblkno = bp->b_lblkno;
2207 newbp->b_blkno = bp->b_blkno;
2208 newbp->b_offset = bp->b_offset;
2209 newbp->b_iodone = ffs_backgroundwritedone;
2210 newbp->b_flags |= B_ASYNC;
2211 newbp->b_flags &= ~B_INVAL;
2212 pbgetvp(bp->b_vp, newbp);
2216 * Move over the dependencies. If there are rollbacks,
2217 * leave the parent buffer dirtied as it will need to
2220 if (LIST_EMPTY(&bp->b_dep) ||
2221 softdep_move_dependencies(bp, newbp) == 0)
2228 * Initiate write on the copy, release the original. The
2229 * BKGRDINPROG flag prevents it from going away until
2230 * the background write completes. We have to recalculate
2231 * its check hash in case the buffer gets freed and then
2232 * reconstituted from the buffer cache during a later read.
2234 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2235 cgp = (struct cg *)bp->b_data;
2238 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2243 /* Mark the buffer clean */
2247 /* Let the normal bufwrite do the rest for us */
2250 * If we are writing a cylinder group, update its time.
2252 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2253 cgp = (struct cg *)bp->b_data;
2254 cgp->cg_old_time = cgp->cg_time = time_second;
2256 return (bufwrite(bp));
2261 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2268 if (bp->b_iocmd == BIO_WRITE) {
2269 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2270 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2271 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2272 panic("ffs_geom_strategy: bad I/O");
2273 nocopy = bp->b_flags & B_NOCOPY;
2274 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2275 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2276 vp->v_rdev->si_snapdata != NULL) {
2277 if ((bp->b_flags & B_CLUSTER) != 0) {
2278 runningbufwakeup(bp);
2279 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2280 b_cluster.cluster_entry) {
2281 error = ffs_copyonwrite(vp, tbp);
2283 error != EOPNOTSUPP) {
2284 bp->b_error = error;
2285 bp->b_ioflags |= BIO_ERROR;
2290 bp->b_runningbufspace = bp->b_bufsize;
2291 atomic_add_long(&runningbufspace,
2292 bp->b_runningbufspace);
2294 error = ffs_copyonwrite(vp, bp);
2295 if (error != 0 && error != EOPNOTSUPP) {
2296 bp->b_error = error;
2297 bp->b_ioflags |= BIO_ERROR;
2304 if ((bp->b_flags & B_CLUSTER) != 0) {
2305 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2306 b_cluster.cluster_entry) {
2307 if (!LIST_EMPTY(&tbp->b_dep))
2311 if (!LIST_EMPTY(&bp->b_dep))
2317 * Check for metadata that needs check-hashes and update them.
2319 switch (bp->b_xflags & BX_FSPRIV) {
2321 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2322 ((struct cg *)bp->b_data)->cg_ckhash =
2323 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2330 printf("Check-hash write is unimplemented!!!\n");
2337 printf("multiple buffer types 0x%b\n",
2338 (u_int)(bp->b_xflags & BX_FSPRIV),
2339 PRINT_UFS_BUF_XFLAGS);
2343 g_vfs_strategy(bo, bp);
2347 ffs_own_mount(const struct mount *mp)
2350 if (mp->mnt_op == &ufs_vfsops)
2358 /* defined in ffs_softdep.c */
2359 extern void db_print_ffs(struct ufsmount *ump);
2361 DB_SHOW_COMMAND(ffs, db_show_ffs)
2364 struct ufsmount *ump;
2367 ump = VFSTOUFS((struct mount *)addr);
2372 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2373 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2374 db_print_ffs(VFSTOUFS(mp));
2378 #endif /* SOFTUPDATES */