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>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
55 #include <sys/fcntl.h>
56 #include <sys/ioccom.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/rwlock.h>
60 #include <sys/vmmeter.h>
62 #include <security/mac/mac_framework.h>
64 #include <ufs/ufs/dir.h>
65 #include <ufs/ufs/extattr.h>
66 #include <ufs/ufs/gjournal.h>
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/inode.h>
70 #include <ufs/ufs/ufs_extern.h>
72 #include <ufs/ffs/fs.h>
73 #include <ufs/ffs/ffs_extern.h>
77 #include <vm/vm_page.h>
79 #include <geom/geom.h>
80 #include <geom/geom_vfs.h>
84 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
86 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
87 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
89 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
90 static int ffs_sync_lazy(struct mount *mp);
91 static int ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
92 static int ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
94 static vfs_init_t ffs_init;
95 static vfs_uninit_t ffs_uninit;
96 static vfs_extattrctl_t ffs_extattrctl;
97 static vfs_cmount_t ffs_cmount;
98 static vfs_unmount_t ffs_unmount;
99 static vfs_mount_t ffs_mount;
100 static vfs_statfs_t ffs_statfs;
101 static vfs_fhtovp_t ffs_fhtovp;
102 static vfs_sync_t ffs_sync;
104 static struct vfsops ufs_vfsops = {
105 .vfs_extattrctl = ffs_extattrctl,
106 .vfs_fhtovp = ffs_fhtovp,
107 .vfs_init = ffs_init,
108 .vfs_mount = ffs_mount,
109 .vfs_cmount = ffs_cmount,
110 .vfs_quotactl = ufs_quotactl,
111 .vfs_root = ufs_root,
112 .vfs_statfs = ffs_statfs,
113 .vfs_sync = ffs_sync,
114 .vfs_uninit = ffs_uninit,
115 .vfs_unmount = ffs_unmount,
116 .vfs_vget = ffs_vget,
117 .vfs_susp_clean = process_deferred_inactive,
120 VFS_SET(ufs_vfsops, ufs, 0);
121 MODULE_VERSION(ufs, 1);
123 static b_strategy_t ffs_geom_strategy;
124 static b_write_t ffs_bufwrite;
126 static struct buf_ops ffs_ops = {
128 .bop_write = ffs_bufwrite,
129 .bop_strategy = ffs_geom_strategy,
131 #ifdef NO_FFS_SNAPSHOT
132 .bop_bdflush = bufbdflush,
134 .bop_bdflush = ffs_bdflush,
139 * Note that userquota and groupquota options are not currently used
140 * by UFS/FFS code and generally mount(8) does not pass those options
141 * from userland, but they can be passed by loader(8) via
142 * vfs.root.mountfrom.options.
144 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
145 "noclusterw", "noexec", "export", "force", "from", "groupquota",
146 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
147 "nosymfollow", "sync", "union", "userquota", NULL };
150 ffs_mount(struct mount *mp)
154 struct ufsmount *ump = NULL;
157 int error, error1, flags;
158 uint64_t mntorflags, saved_mnt_flag;
160 struct nameidata ndp;
164 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
166 if (uma_inode == NULL) {
167 uma_inode = uma_zcreate("FFS inode",
168 sizeof(struct inode), NULL, NULL, NULL, NULL,
170 uma_ufs1 = uma_zcreate("FFS1 dinode",
171 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
173 uma_ufs2 = uma_zcreate("FFS2 dinode",
174 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
178 vfs_deleteopt(mp->mnt_optnew, "groupquota");
179 vfs_deleteopt(mp->mnt_optnew, "userquota");
181 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
186 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
187 mntorflags |= MNT_ACLS;
189 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
190 mntorflags |= MNT_SNAPSHOT;
192 * Once we have set the MNT_SNAPSHOT flag, do not
193 * persist "snapshot" in the options list.
195 vfs_deleteopt(mp->mnt_optnew, "snapshot");
196 vfs_deleteopt(mp->mnt_opt, "snapshot");
199 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
200 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
202 * Once we have set the restricted PID, do not
203 * persist "fsckpid" in the options list.
205 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
206 vfs_deleteopt(mp->mnt_opt, "fsckpid");
207 if (mp->mnt_flag & MNT_UPDATE) {
208 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
209 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
211 "Checker enable: Must be read-only");
214 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
216 "Checker enable: Must be read-only");
219 /* Set to -1 if we are done */
224 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
225 if (mntorflags & MNT_ACLS) {
227 "\"acls\" and \"nfsv4acls\" options "
228 "are mutually exclusive");
231 mntorflags |= MNT_NFS4ACLS;
235 mp->mnt_flag |= mntorflags;
238 * If updating, check whether changing from read-only to
239 * read/write; if there is no device name, that's all we do.
241 if (mp->mnt_flag & MNT_UPDATE) {
244 devvp = ump->um_devvp;
245 if (fsckpid == -1 && ump->um_fsckpid > 0) {
246 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
247 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
251 * Return to normal read-only mode.
253 error = g_access(ump->um_cp, 0, -1, 0);
257 if (fs->fs_ronly == 0 &&
258 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
260 * Flush any dirty data and suspend filesystem.
262 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
264 error = vfs_write_suspend_umnt(mp);
268 * Check for and optionally get rid of files open
272 if (mp->mnt_flag & MNT_FORCE)
274 if (MOUNTEDSOFTDEP(mp)) {
275 error = softdep_flushfiles(mp, flags, td);
277 error = ffs_flushfiles(mp, flags, td);
280 vfs_write_resume(mp, 0);
283 if (fs->fs_pendingblocks != 0 ||
284 fs->fs_pendinginodes != 0) {
285 printf("WARNING: %s Update error: blocks %jd "
286 "files %d\n", fs->fs_fsmnt,
287 (intmax_t)fs->fs_pendingblocks,
288 fs->fs_pendinginodes);
289 fs->fs_pendingblocks = 0;
290 fs->fs_pendinginodes = 0;
292 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
294 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
297 vfs_write_resume(mp, 0);
300 if (MOUNTEDSOFTDEP(mp))
304 * Drop our write and exclusive access.
306 g_access(ump->um_cp, 0, -1, -1);
310 mp->mnt_flag |= MNT_RDONLY;
313 * Allow the writers to note that filesystem
316 vfs_write_resume(mp, 0);
318 if ((mp->mnt_flag & MNT_RELOAD) &&
319 (error = ffs_reload(mp, td, 0)) != 0)
322 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
324 * If we are running a checker, do not allow upgrade.
326 if (ump->um_fsckpid > 0) {
328 "Active checker, cannot upgrade to write");
332 * If upgrade to read-write by non-root, then verify
333 * that user has necessary permissions on the device.
335 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
336 error = VOP_ACCESS(devvp, VREAD | VWRITE,
339 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
341 VOP_UNLOCK(devvp, 0);
344 VOP_UNLOCK(devvp, 0);
345 fs->fs_flags &= ~FS_UNCLEAN;
346 if (fs->fs_clean == 0) {
347 fs->fs_flags |= FS_UNCLEAN;
348 if ((mp->mnt_flag & MNT_FORCE) ||
350 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
351 (fs->fs_flags & FS_DOSOFTDEP))) {
352 printf("WARNING: %s was not properly "
353 "dismounted\n", fs->fs_fsmnt);
356 "R/W mount of %s denied. %s.%s",
358 "Filesystem is not clean - run fsck",
359 (fs->fs_flags & FS_SUJ) == 0 ? "" :
360 " Forced mount will invalidate"
361 " journal contents");
367 * Request exclusive write access.
369 error = g_access(ump->um_cp, 0, 1, 1);
373 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
375 error = vfs_write_suspend_umnt(mp);
380 saved_mnt_flag = MNT_RDONLY;
381 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
383 saved_mnt_flag |= MNT_ASYNC;
384 mp->mnt_flag &= ~saved_mnt_flag;
386 fs->fs_mtime = time_second;
387 /* check to see if we need to start softdep */
388 if ((fs->fs_flags & FS_DOSOFTDEP) &&
389 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
392 mp->mnt_flag |= saved_mnt_flag;
394 vfs_write_resume(mp, 0);
398 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
401 mp->mnt_flag |= saved_mnt_flag;
403 vfs_write_resume(mp, 0);
406 if (fs->fs_snapinum[0] != 0)
407 ffs_snapshot_mount(mp);
408 vfs_write_resume(mp, 0);
411 * Soft updates is incompatible with "async",
412 * so if we are doing softupdates stop the user
413 * from setting the async flag in an update.
414 * Softdep_mount() clears it in an initial mount
417 if (MOUNTEDSOFTDEP(mp)) {
418 /* XXX: Reset too late ? */
420 mp->mnt_flag &= ~MNT_ASYNC;
424 * Keep MNT_ACLS flag if it is stored in superblock.
426 if ((fs->fs_flags & FS_ACLS) != 0) {
427 /* XXX: Set too late ? */
429 mp->mnt_flag |= MNT_ACLS;
433 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
434 /* XXX: Set too late ? */
436 mp->mnt_flag |= MNT_NFS4ACLS;
440 * If this is a request from fsck to clean up the filesystem,
441 * then allow the specified pid to proceed.
444 if (ump->um_fsckpid != 0) {
446 "Active checker already running on %s",
450 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
451 ("soft updates enabled on read-only file system"));
454 * Request write access.
456 error = g_access(ump->um_cp, 0, 1, 0);
460 "Checker activation failed on %s",
464 ump->um_fsckpid = fsckpid;
465 if (fs->fs_snapinum[0] != 0)
466 ffs_snapshot_mount(mp);
467 fs->fs_mtime = time_second;
470 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
474 * If this is a snapshot request, take the snapshot.
476 if (mp->mnt_flag & MNT_SNAPSHOT)
477 return (ffs_snapshot(mp, fspec));
480 * Must not call namei() while owning busy ref.
486 * Not an update, or updating the name: look up the name
487 * and verify that it refers to a sensible disk device.
489 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
491 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
493 * Unmount does not start if MNT_UPDATE is set. Mount
494 * update busies mp before setting MNT_UPDATE. We
495 * must be able to retain our busy ref succesfully,
498 error1 = vfs_busy(mp, MBF_NOWAIT);
503 NDFREE(&ndp, NDF_ONLY_PNBUF);
505 if (!vn_isdisk(devvp, &error)) {
511 * If mount by non-root, then verify that user has necessary
512 * permissions on the device.
515 if ((mp->mnt_flag & MNT_RDONLY) == 0)
517 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
519 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
525 if (mp->mnt_flag & MNT_UPDATE) {
529 * If it's not the same vnode, or at least the same device
530 * then it's not correct.
533 if (devvp->v_rdev != ump->um_devvp->v_rdev)
534 error = EINVAL; /* needs translation */
542 * We need the name for the mount point (also used for
543 * "last mounted on") copied in. If an error occurs,
544 * the mount point is discarded by the upper level code.
545 * Note that vfs_mount_alloc() populates f_mntonname for us.
547 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
552 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
553 ("soft updates enabled on read-only file system"));
558 * Request write access.
560 error = g_access(ump->um_cp, 0, 1, 0);
563 printf("WARNING: %s: Checker activation "
564 "failed\n", fs->fs_fsmnt);
566 ump->um_fsckpid = fsckpid;
567 if (fs->fs_snapinum[0] != 0)
568 ffs_snapshot_mount(mp);
569 fs->fs_mtime = time_second;
571 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
575 vfs_mountedfrom(mp, fspec);
580 * Compatibility with old mount system call.
584 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
586 struct ufs_args args;
587 struct export_args exp;
592 error = copyin(data, &args, sizeof args);
595 vfs_oexport_conv(&args.export, &exp);
597 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
598 ma = mount_arg(ma, "export", &exp, sizeof(exp));
599 error = kernel_mount(ma, flags);
605 * Reload all incore data for a filesystem (used after running fsck on
606 * the root filesystem and finding things to fix). If the 'force' flag
607 * is 0, the filesystem must be mounted read-only.
609 * Things to do to update the mount:
610 * 1) invalidate all cached meta-data.
611 * 2) re-read superblock from disk.
612 * 3) re-read summary information from disk.
613 * 4) invalidate all inactive vnodes.
614 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
615 * writers, if requested.
616 * 6) invalidate all cached file data.
617 * 7) re-read inode data for all active vnodes.
620 ffs_reload(struct mount *mp, struct thread *td, int flags)
622 struct vnode *vp, *mvp, *devvp;
626 struct fs *fs, *newfs;
627 struct ufsmount *ump;
628 ufs2_daddr_t sblockloc;
636 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
643 * Step 1: invalidate all cached meta-data.
645 devvp = VFSTOUFS(mp)->um_devvp;
646 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
647 if (vinvalbuf(devvp, 0, 0, 0) != 0)
648 panic("ffs_reload: dirty1");
649 VOP_UNLOCK(devvp, 0);
652 * Step 2: re-read superblock from disk.
654 fs = VFSTOUFS(mp)->um_fs;
655 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
658 newfs = (struct fs *)bp->b_data;
659 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
660 newfs->fs_magic != FS_UFS2_MAGIC) ||
661 newfs->fs_bsize > MAXBSIZE ||
662 newfs->fs_bsize < sizeof(struct fs)) {
664 return (EIO); /* XXX needs translation */
667 * Copy pointer fields back into superblock before copying in XXX
668 * new superblock. These should really be in the ufsmount. XXX
669 * Note that important parameters (eg fs_ncg) are unchanged.
671 newfs->fs_csp = fs->fs_csp;
672 newfs->fs_maxcluster = fs->fs_maxcluster;
673 newfs->fs_contigdirs = fs->fs_contigdirs;
674 newfs->fs_active = fs->fs_active;
675 newfs->fs_ronly = fs->fs_ronly;
676 sblockloc = fs->fs_sblockloc;
677 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
679 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
680 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
682 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
683 printf("WARNING: %s: reload pending error: blocks %jd "
684 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
685 fs->fs_pendinginodes);
686 fs->fs_pendingblocks = 0;
687 fs->fs_pendinginodes = 0;
692 * Step 3: re-read summary information from disk.
694 size = fs->fs_cssize;
695 blks = howmany(size, fs->fs_fsize);
696 if (fs->fs_contigsumsize > 0)
697 size += fs->fs_ncg * sizeof(int32_t);
698 size += fs->fs_ncg * sizeof(u_int8_t);
699 free(fs->fs_csp, M_UFSMNT);
700 space = malloc(size, M_UFSMNT, M_WAITOK);
702 for (i = 0; i < blks; i += fs->fs_frag) {
704 if (i + fs->fs_frag > blks)
705 size = (blks - i) * fs->fs_fsize;
706 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
710 bcopy(bp->b_data, space, (u_int)size);
711 space = (char *)space + size;
715 * We no longer know anything about clusters per cylinder group.
717 if (fs->fs_contigsumsize > 0) {
718 fs->fs_maxcluster = lp = space;
719 for (i = 0; i < fs->fs_ncg; i++)
720 *lp++ = fs->fs_contigsumsize;
723 size = fs->fs_ncg * sizeof(u_int8_t);
724 fs->fs_contigdirs = (u_int8_t *)space;
725 bzero(fs->fs_contigdirs, size);
726 if ((flags & FFSR_UNSUSPEND) != 0) {
728 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
729 wakeup(&mp->mnt_flag);
734 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
738 if (vp->v_type == VNON) {
743 * Step 4: invalidate all cached file data.
745 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
746 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
749 if (vinvalbuf(vp, 0, 0, 0))
750 panic("ffs_reload: dirty2");
752 * Step 5: re-read inode data for all active vnodes.
756 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
757 (int)fs->fs_bsize, NOCRED, &bp);
760 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
763 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
766 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
769 ip->i_effnlink = ip->i_nlink;
777 * Common code for mount and mountroot
780 ffs_mountfs(devvp, mp, td)
785 struct ufsmount *ump;
788 int error, i, len, ronly;
790 struct g_consumer *cp;
797 cred = td ? td->td_ucred : NOCRED;
798 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
800 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
802 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
803 (uintptr_t)mp) == 0) {
804 VOP_UNLOCK(devvp, 0);
808 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
811 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
812 VOP_UNLOCK(devvp, 0);
816 devvp->v_bufobj.bo_ops = &ffs_ops;
817 VOP_UNLOCK(devvp, 0);
818 if (dev->si_iosize_max != 0)
819 mp->mnt_iosize_max = dev->si_iosize_max;
820 if (mp->mnt_iosize_max > MAXPHYS)
821 mp->mnt_iosize_max = MAXPHYS;
822 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
825 "Invalid sectorsize %d for superblock size %d",
826 cp->provider->sectorsize, SBLOCKSIZE);
829 /* fetch the superblock and summary information */
831 if ((mp->mnt_flag & MNT_ROOTFS) != 0)
832 loc = STDSB_NOHASHFAIL;
833 if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
835 /* none of these types of check-hashes are maintained by this kernel */
836 fs->fs_metackhash &= ~(CK_INDIR | CK_DIR);
837 /* no support for any undefined flags */
838 fs->fs_flags &= FS_SUPPORTED;
839 fs->fs_flags &= ~FS_UNCLEAN;
840 if (fs->fs_clean == 0) {
841 fs->fs_flags |= FS_UNCLEAN;
842 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
843 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
844 (fs->fs_flags & FS_DOSOFTDEP))) {
845 printf("WARNING: %s was not properly dismounted\n",
848 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
849 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
850 (fs->fs_flags & FS_SUJ) == 0 ? "" :
851 " Forced mount will invalidate journal contents");
855 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
856 (mp->mnt_flag & MNT_FORCE)) {
857 printf("WARNING: %s: lost blocks %jd files %d\n",
858 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
859 fs->fs_pendinginodes);
860 fs->fs_pendingblocks = 0;
861 fs->fs_pendinginodes = 0;
864 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
865 printf("WARNING: %s: mount pending error: blocks %jd "
866 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
867 fs->fs_pendinginodes);
868 fs->fs_pendingblocks = 0;
869 fs->fs_pendinginodes = 0;
871 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
874 * Get journal provider name.
877 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
878 if (g_io_getattr("GJOURNAL::provider", cp, &len,
879 mp->mnt_gjprovider) == 0) {
880 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
883 mp->mnt_flag |= MNT_GJOURNAL;
886 printf("WARNING: %s: GJOURNAL flag on fs "
887 "but no gjournal provider below\n",
888 mp->mnt_stat.f_mntonname);
889 free(mp->mnt_gjprovider, M_UFSMNT);
890 mp->mnt_gjprovider = NULL;
893 printf("WARNING: %s: GJOURNAL flag on fs but no "
894 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
897 mp->mnt_gjprovider = NULL;
899 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
901 ump->um_bo = &devvp->v_bufobj;
903 if (fs->fs_magic == FS_UFS1_MAGIC) {
904 ump->um_fstype = UFS1;
905 ump->um_balloc = ffs_balloc_ufs1;
907 ump->um_fstype = UFS2;
908 ump->um_balloc = ffs_balloc_ufs2;
910 ump->um_blkatoff = ffs_blkatoff;
911 ump->um_truncate = ffs_truncate;
912 ump->um_update = ffs_update;
913 ump->um_valloc = ffs_valloc;
914 ump->um_vfree = ffs_vfree;
915 ump->um_ifree = ffs_ifree;
916 ump->um_rdonly = ffs_rdonly;
917 ump->um_snapgone = ffs_snapgone;
918 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
919 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
920 fs->fs_ronly = ronly;
921 fs->fs_active = NULL;
923 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
924 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
926 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
927 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
932 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
934 mp->mnt_flag |= MNT_LOCAL;
936 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
939 mp->mnt_flag |= MNT_MULTILABEL;
942 printf("WARNING: %s: multilabel flag on fs but "
943 "no MAC support\n", mp->mnt_stat.f_mntonname);
946 if ((fs->fs_flags & FS_ACLS) != 0) {
950 if (mp->mnt_flag & MNT_NFS4ACLS)
951 printf("WARNING: %s: ACLs flag on fs conflicts with "
952 "\"nfsv4acls\" mount option; option ignored\n",
953 mp->mnt_stat.f_mntonname);
954 mp->mnt_flag &= ~MNT_NFS4ACLS;
955 mp->mnt_flag |= MNT_ACLS;
959 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
960 mp->mnt_stat.f_mntonname);
963 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
967 if (mp->mnt_flag & MNT_ACLS)
968 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
969 "with \"acls\" mount option; option ignored\n",
970 mp->mnt_stat.f_mntonname);
971 mp->mnt_flag &= ~MNT_ACLS;
972 mp->mnt_flag |= MNT_NFS4ACLS;
976 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
977 "ACLs support\n", mp->mnt_stat.f_mntonname);
980 if ((fs->fs_flags & FS_TRIM) != 0) {
982 if (g_io_getattr("GEOM::candelete", cp, &len,
985 ump->um_flags |= UM_CANDELETE;
987 printf("WARNING: %s: TRIM flag on fs but disk "
988 "does not support TRIM\n",
989 mp->mnt_stat.f_mntonname);
991 printf("WARNING: %s: TRIM flag on fs but disk does "
992 "not confirm that it supports TRIM\n",
993 mp->mnt_stat.f_mntonname);
995 if (((ump->um_flags) & UM_CANDELETE) != 0) {
996 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
997 taskqueue_thread_enqueue, &ump->um_trim_tq);
998 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
999 "%s trim", mp->mnt_stat.f_mntonname);
1000 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1001 &ump->um_trimlisthashsize);
1005 ump->um_mountp = mp;
1007 ump->um_devvp = devvp;
1008 ump->um_nindir = fs->fs_nindir;
1009 ump->um_bptrtodb = fs->fs_fsbtodb;
1010 ump->um_seqinc = fs->fs_frag;
1011 for (i = 0; i < MAXQUOTAS; i++)
1012 ump->um_quotas[i] = NULLVP;
1014 ufs_extattr_uepm_init(&ump->um_extattr);
1017 * Set FS local "last mounted on" information (NULL pad)
1019 bzero(fs->fs_fsmnt, MAXMNTLEN);
1020 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1021 mp->mnt_stat.f_iosize = fs->fs_bsize;
1023 if (mp->mnt_flag & MNT_ROOTFS) {
1025 * Root mount; update timestamp in mount structure.
1026 * this will be used by the common root mount code
1027 * to update the system clock.
1029 mp->mnt_time = fs->fs_time;
1033 fs->fs_mtime = time_second;
1034 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1035 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1036 ffs_flushfiles(mp, FORCECLOSE, td);
1039 if (fs->fs_snapinum[0] != 0)
1040 ffs_snapshot_mount(mp);
1043 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1046 * Initialize filesystem state information in mount struct.
1049 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1050 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1053 #ifdef UFS_EXTATTR_AUTOSTART
1056 * Auto-starting does the following:
1057 * - check for /.attribute in the fs, and extattr_start if so
1058 * - for each file in .attribute, enable that file with
1059 * an attribute of the same name.
1060 * Not clear how to report errors -- probably eat them.
1061 * This would all happen while the filesystem was busy/not
1062 * available, so would effectively be "atomic".
1064 (void) ufs_extattr_autostart(mp, td);
1065 #endif /* !UFS_EXTATTR_AUTOSTART */
1066 #endif /* !UFS_EXTATTR */
1070 free(fs->fs_csp, M_UFSMNT);
1076 g_topology_unlock();
1079 mtx_destroy(UFS_MTX(ump));
1080 if (mp->mnt_gjprovider != NULL) {
1081 free(mp->mnt_gjprovider, M_UFSMNT);
1082 mp->mnt_gjprovider = NULL;
1084 free(ump, M_UFSMNT);
1085 mp->mnt_data = NULL;
1087 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1093 * A read function for use by filesystem-layer routines.
1096 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1101 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1102 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1103 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1106 bcopy(bp->b_data, *bufp, size);
1107 bp->b_flags |= B_INVAL | B_NOCACHE;
1112 #include <sys/sysctl.h>
1113 static int bigcgs = 0;
1114 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1117 * Sanity checks for loading old filesystem superblocks.
1118 * See ffs_oldfscompat_write below for unwound actions.
1120 * XXX - Parts get retired eventually.
1121 * Unfortunately new bits get added.
1124 ffs_oldfscompat_read(fs, ump, sblockloc)
1126 struct ufsmount *ump;
1127 ufs2_daddr_t sblockloc;
1132 * If not yet done, update fs_flags location and value of fs_sblockloc.
1134 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1135 fs->fs_flags = fs->fs_old_flags;
1136 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1137 fs->fs_sblockloc = sblockloc;
1140 * If not yet done, update UFS1 superblock with new wider fields.
1142 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1143 fs->fs_maxbsize = fs->fs_bsize;
1144 fs->fs_time = fs->fs_old_time;
1145 fs->fs_size = fs->fs_old_size;
1146 fs->fs_dsize = fs->fs_old_dsize;
1147 fs->fs_csaddr = fs->fs_old_csaddr;
1148 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1149 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1150 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1151 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1153 if (fs->fs_magic == FS_UFS1_MAGIC &&
1154 fs->fs_old_inodefmt < FS_44INODEFMT) {
1155 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1156 fs->fs_qbmask = ~fs->fs_bmask;
1157 fs->fs_qfmask = ~fs->fs_fmask;
1159 if (fs->fs_magic == FS_UFS1_MAGIC) {
1160 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1161 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1162 if (fs->fs_maxfilesize > maxfilesize)
1163 fs->fs_maxfilesize = maxfilesize;
1165 /* Compatibility for old filesystems */
1166 if (fs->fs_avgfilesize <= 0)
1167 fs->fs_avgfilesize = AVFILESIZ;
1168 if (fs->fs_avgfpdir <= 0)
1169 fs->fs_avgfpdir = AFPDIR;
1171 fs->fs_save_cgsize = fs->fs_cgsize;
1172 fs->fs_cgsize = fs->fs_bsize;
1177 * Unwinding superblock updates for old filesystems.
1178 * See ffs_oldfscompat_read above for details.
1180 * XXX - Parts get retired eventually.
1181 * Unfortunately new bits get added.
1184 ffs_oldfscompat_write(fs, ump)
1186 struct ufsmount *ump;
1190 * Copy back UFS2 updated fields that UFS1 inspects.
1192 if (fs->fs_magic == FS_UFS1_MAGIC) {
1193 fs->fs_old_time = fs->fs_time;
1194 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1195 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1196 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1197 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1198 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1201 fs->fs_cgsize = fs->fs_save_cgsize;
1202 fs->fs_save_cgsize = 0;
1207 * unmount system call
1210 ffs_unmount(mp, mntflags)
1215 struct ufsmount *ump = VFSTOUFS(mp);
1217 int error, flags, susp;
1226 if (mntflags & MNT_FORCE) {
1227 flags |= FORCECLOSE;
1228 susp = fs->fs_ronly == 0;
1231 if ((error = ufs_extattr_stop(mp, td))) {
1232 if (error != EOPNOTSUPP)
1233 printf("WARNING: unmount %s: ufs_extattr_stop "
1234 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1238 ufs_extattr_uepm_destroy(&ump->um_extattr);
1243 error = vfs_write_suspend_umnt(mp);
1247 if (MOUNTEDSOFTDEP(mp))
1248 error = softdep_flushfiles(mp, flags, td);
1250 error = ffs_flushfiles(mp, flags, td);
1251 if (error != 0 && error != ENXIO)
1255 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1256 printf("WARNING: unmount %s: pending error: blocks %jd "
1257 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1258 fs->fs_pendinginodes);
1259 fs->fs_pendingblocks = 0;
1260 fs->fs_pendinginodes = 0;
1263 if (MOUNTEDSOFTDEP(mp))
1264 softdep_unmount(mp);
1265 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1266 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1267 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1268 if (error && error != ENXIO) {
1274 vfs_write_resume(mp, VR_START_WRITE);
1275 if (ump->um_trim_tq != NULL) {
1276 while (ump->um_trim_inflight != 0)
1277 pause("ufsutr", hz);
1278 taskqueue_drain_all(ump->um_trim_tq);
1279 taskqueue_free(ump->um_trim_tq);
1280 free (ump->um_trimhash, M_TRIM);
1283 if (ump->um_fsckpid > 0) {
1285 * Return to normal read-only mode.
1287 error = g_access(ump->um_cp, 0, -1, 0);
1288 ump->um_fsckpid = 0;
1290 g_vfs_close(ump->um_cp);
1291 g_topology_unlock();
1292 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1293 vrele(ump->um_devvp);
1294 dev_rel(ump->um_dev);
1295 mtx_destroy(UFS_MTX(ump));
1296 if (mp->mnt_gjprovider != NULL) {
1297 free(mp->mnt_gjprovider, M_UFSMNT);
1298 mp->mnt_gjprovider = NULL;
1300 free(fs->fs_csp, M_UFSMNT);
1302 free(ump, M_UFSMNT);
1303 mp->mnt_data = NULL;
1305 mp->mnt_flag &= ~MNT_LOCAL;
1307 if (td->td_su == mp) {
1315 vfs_write_resume(mp, VR_START_WRITE);
1319 ufs_extattr_uepm_init(&ump->um_extattr);
1320 #ifdef UFS_EXTATTR_AUTOSTART
1321 (void) ufs_extattr_autostart(mp, td);
1330 * Flush out all the files in a filesystem.
1333 ffs_flushfiles(mp, flags, td)
1338 struct ufsmount *ump;
1344 if (mp->mnt_flag & MNT_QUOTA) {
1346 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1349 for (i = 0; i < MAXQUOTAS; i++) {
1350 error = quotaoff(td, mp, i);
1352 if ((flags & EARLYFLUSH) == 0)
1360 * Here we fall through to vflush again to ensure that
1361 * we have gotten rid of all the system vnodes, unless
1362 * quotas must not be closed.
1366 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1367 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1368 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1370 ffs_snapshot_unmount(mp);
1371 flags |= FORCECLOSE;
1373 * Here we fall through to vflush again to ensure
1374 * that we have gotten rid of all the system vnodes.
1379 * Do not close system files if quotas were not closed, to be
1380 * able to sync the remaining dquots. The freeblks softupdate
1381 * workitems might hold a reference on a dquot, preventing
1382 * quotaoff() from completing. Next round of
1383 * softdep_flushworklist() iteration should process the
1384 * blockers, allowing the next run of quotaoff() to finally
1385 * flush held dquots.
1387 * Otherwise, flush all the files.
1389 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1393 * Flush filesystem metadata.
1395 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1396 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1397 VOP_UNLOCK(ump->um_devvp, 0);
1402 * Get filesystem statistics.
1409 struct ufsmount *ump;
1414 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1415 panic("ffs_statfs");
1416 sbp->f_version = STATFS_VERSION;
1417 sbp->f_bsize = fs->fs_fsize;
1418 sbp->f_iosize = fs->fs_bsize;
1419 sbp->f_blocks = fs->fs_dsize;
1421 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1422 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1423 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1424 dbtofsb(fs, fs->fs_pendingblocks);
1425 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1426 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1428 sbp->f_namemax = UFS_MAXNAMLEN;
1433 sync_doupdate(struct inode *ip)
1436 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1441 * For a lazy sync, we only care about access times, quotas and the
1442 * superblock. Other filesystem changes are already converted to
1443 * cylinder group blocks or inode blocks updates and are written to
1450 struct vnode *mvp, *vp;
1453 int allerror, error;
1457 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1459 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1460 if (vp->v_type == VNON) {
1467 * The IN_ACCESS flag is converted to IN_MODIFIED by
1468 * ufs_close() and ufs_getattr() by the calls to
1469 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1470 * Test also all the other timestamp flags too, to pick up
1471 * any other cases that could be missed.
1473 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1477 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1480 if (sync_doupdate(ip))
1481 error = ffs_update(vp, 0);
1492 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1493 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1499 * Go through the disk queues to initiate sandbagged IO;
1500 * go through the inodes to write those that have been modified;
1501 * initiate the writing of the super block if it has been modified.
1503 * Note: we are always called with the filesystem marked busy using
1507 ffs_sync(mp, waitfor)
1511 struct vnode *mvp, *vp, *devvp;
1514 struct ufsmount *ump = VFSTOUFS(mp);
1516 int error, count, lockreq, allerror = 0;
1519 int secondary_writes;
1520 int secondary_accwrites;
1522 int softdep_accdeps;
1529 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1530 panic("%s: ffs_sync: modification on read-only filesystem",
1532 if (waitfor == MNT_LAZY) {
1534 return (ffs_sync_lazy(mp));
1535 waitfor = MNT_NOWAIT;
1539 * Write back each (modified) inode.
1541 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1542 if (waitfor == MNT_SUSPEND) {
1546 if (waitfor == MNT_WAIT)
1547 lockreq = LK_EXCLUSIVE;
1548 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1550 /* Grab snapshot of secondary write counts */
1552 secondary_writes = mp->mnt_secondary_writes;
1553 secondary_accwrites = mp->mnt_secondary_accwrites;
1556 /* Grab snapshot of softdep dependency counts */
1557 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1559 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1561 * Depend on the vnode interlock to keep things stable enough
1562 * for a quick test. Since there might be hundreds of
1563 * thousands of vnodes, we cannot afford even a subroutine
1564 * call unless there's a good chance that we have work to do.
1566 if (vp->v_type == VNON) {
1572 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1573 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1577 if ((error = vget(vp, lockreq, td)) != 0) {
1578 if (error == ENOENT || error == ENOLCK) {
1579 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1584 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1589 * Force stale filesystem control information to be flushed.
1591 if (waitfor == MNT_WAIT || rebooting) {
1592 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1594 /* Flushed work items may create new vnodes to clean */
1595 if (allerror == 0 && count)
1602 devvp = ump->um_devvp;
1603 bo = &devvp->v_bufobj;
1605 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1607 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1608 error = VOP_FSYNC(devvp, waitfor, td);
1609 VOP_UNLOCK(devvp, 0);
1610 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1611 error = ffs_sbupdate(ump, waitfor, 0);
1614 if (allerror == 0 && waitfor == MNT_WAIT)
1616 } else if (suspend != 0) {
1617 if (softdep_check_suspend(mp,
1622 secondary_accwrites) != 0) {
1624 goto loop; /* More work needed */
1626 mtx_assert(MNT_MTX(mp), MA_OWNED);
1627 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1633 * Write back modified superblock.
1635 if (fs->fs_fmod != 0 &&
1636 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1642 ffs_vget(mp, ino, flags, vpp)
1648 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1652 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1661 struct ufsmount *ump;
1666 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1667 if (error || *vpp != NULL)
1671 * We must promote to an exclusive lock for vnode creation. This
1672 * can happen if lookup is passed LOCKSHARED.
1674 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1675 flags &= ~LK_TYPE_MASK;
1676 flags |= LK_EXCLUSIVE;
1680 * We do not lock vnode creation as it is believed to be too
1681 * expensive for such rare case as simultaneous creation of vnode
1682 * for same ino by different processes. We just allow them to race
1683 * and check later to decide who wins. Let the race begin!
1688 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1690 /* Allocate a new vnode/inode. */
1691 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1692 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1695 uma_zfree(uma_inode, ip);
1699 * FFS supports recursive locking.
1701 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1704 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1709 ip->i_nextclustercg = -1;
1710 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1711 ip->i_mode = 0; /* ensure error cases below throw away vnode */
1715 for (i = 0; i < MAXQUOTAS; i++)
1716 ip->i_dquot[i] = NODQUOT;
1720 if (ffs_flags & FFSV_FORCEINSMQ)
1721 vp->v_vflag |= VV_FORCEINSMQ;
1722 error = insmntque(vp, mp);
1724 uma_zfree(uma_inode, ip);
1728 vp->v_vflag &= ~VV_FORCEINSMQ;
1729 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1730 if (error || *vpp != NULL)
1733 /* Read in the disk contents for the inode, copy into the inode. */
1734 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1735 (int)fs->fs_bsize, NOCRED, &bp);
1738 * The inode does not contain anything useful, so it would
1739 * be misleading to leave it on its hash chain. With mode
1740 * still zero, it will be unlinked and returned to the free
1749 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1751 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1752 if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
1758 if (DOINGSOFTDEP(vp))
1759 softdep_load_inodeblock(ip);
1761 ip->i_effnlink = ip->i_nlink;
1765 * Initialize the vnode from the inode, check for aliases.
1766 * Note that the underlying vnode may have changed.
1768 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1777 * Finish inode initialization.
1779 if (vp->v_type != VFIFO) {
1780 /* FFS supports shared locking for all files except fifos. */
1785 * Set up a generation number for this inode if it does not
1786 * already have one. This should only happen on old filesystems.
1788 if (ip->i_gen == 0) {
1789 while (ip->i_gen == 0)
1790 ip->i_gen = arc4random();
1791 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1792 ip->i_flag |= IN_MODIFIED;
1793 DIP_SET(ip, i_gen, ip->i_gen);
1797 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1799 * If this vnode is already allocated, and we're running
1800 * multi-label, attempt to perform a label association
1801 * from the extended attributes on the inode.
1803 error = mac_vnode_associate_extattr(mp, vp);
1805 /* ufs_inactive will release ip->i_devvp ref. */
1818 * File handle to vnode
1820 * Have to be really careful about stale file handles:
1821 * - check that the inode number is valid
1822 * - for UFS2 check that the inode number is initialized
1823 * - call ffs_vget() to get the locked inode
1824 * - check for an unallocated inode (i_mode == 0)
1825 * - check that the given client host has export rights and return
1826 * those rights via. exflagsp and credanonp
1829 ffs_fhtovp(mp, fhp, flags, vpp)
1836 struct ufsmount *ump;
1844 ufhp = (struct ufid *)fhp;
1845 ino = ufhp->ufid_ino;
1848 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1851 * Need to check if inode is initialized because UFS2 does lazy
1852 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1854 if (fs->fs_magic != FS_UFS2_MAGIC)
1855 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1856 cg = ino_to_cg(fs, ino);
1857 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0)
1859 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1864 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1868 * Initialize the filesystem.
1872 struct vfsconf *vfsp;
1875 ffs_susp_initialize();
1876 softdep_initialize();
1877 return (ufs_init(vfsp));
1881 * Undo the work of ffs_init().
1885 struct vfsconf *vfsp;
1889 ret = ufs_uninit(vfsp);
1890 softdep_uninitialize();
1891 ffs_susp_uninitialize();
1896 * Structure used to pass information from ffs_sbupdate to its
1897 * helper routine ffs_use_bwrite.
1900 struct ufsmount *ump;
1908 * Write a superblock and associated information back to disk.
1911 ffs_sbupdate(ump, waitfor, suspended)
1912 struct ufsmount *ump;
1921 if (fs->fs_ronly == 1 &&
1922 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1923 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1924 panic("ffs_sbupdate: write read-only filesystem");
1926 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1928 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1929 (int)fs->fs_sbsize, 0, 0, 0);
1931 * Initialize info needed for write function.
1935 devfd.waitfor = waitfor;
1936 devfd.suspended = suspended;
1938 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
1942 * Write function for use by filesystem-layer routines.
1945 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
1947 struct devfd *devfdp;
1948 struct ufsmount *ump;
1957 * Writing the superblock summary information.
1959 if (loc != fs->fs_sblockloc) {
1960 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
1961 bcopy(buf, bp->b_data, (u_int)size);
1962 if (devfdp->suspended)
1963 bp->b_flags |= B_VALIDSUSPWRT;
1964 if (devfdp->waitfor != MNT_WAIT)
1966 else if ((error = bwrite(bp)) != 0)
1967 devfdp->error = error;
1971 * Writing the superblock itself. We need to do special checks for it.
1974 if (devfdp->error != 0) {
1976 return (devfdp->error);
1978 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1979 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1980 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1981 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1982 fs->fs_sblockloc = SBLOCK_UFS1;
1984 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1985 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1986 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1987 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1988 fs->fs_sblockloc = SBLOCK_UFS2;
1990 if (MOUNTEDSOFTDEP(ump->um_mountp))
1991 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1992 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1993 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1994 if (devfdp->suspended)
1995 bp->b_flags |= B_VALIDSUSPWRT;
1996 if (devfdp->waitfor != MNT_WAIT)
1998 else if ((error = bwrite(bp)) != 0)
1999 devfdp->error = error;
2000 return (devfdp->error);
2004 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2005 int attrnamespace, const char *attrname)
2009 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2012 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2018 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2021 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2022 uma_zfree(uma_ufs1, ip->i_din1);
2023 else if (ip->i_din2 != NULL)
2024 uma_zfree(uma_ufs2, ip->i_din2);
2025 uma_zfree(uma_inode, ip);
2028 static int dobkgrdwrite = 1;
2029 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2030 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2033 * Complete a background write started from bwrite.
2036 ffs_backgroundwritedone(struct buf *bp)
2038 struct bufobj *bufobj;
2042 * Find the original buffer that we are writing.
2044 bufobj = bp->b_bufobj;
2046 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2047 panic("backgroundwritedone: lost buffer");
2050 * We should mark the cylinder group buffer origbp as
2051 * dirty, to not loose the failed write.
2053 if ((bp->b_ioflags & BIO_ERROR) != 0)
2054 origbp->b_vflags |= BV_BKGRDERR;
2057 * Process dependencies then return any unfinished ones.
2059 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2062 if (!LIST_EMPTY(&bp->b_dep))
2063 softdep_move_dependencies(bp, origbp);
2066 * This buffer is marked B_NOCACHE so when it is released
2067 * by biodone it will be tossed.
2069 bp->b_flags |= B_NOCACHE;
2070 bp->b_flags &= ~B_CACHE;
2074 * Prevent brelse() from trying to keep and re-dirtying bp on
2075 * errors. It causes b_bufobj dereference in
2076 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2079 if ((bp->b_ioflags & BIO_ERROR) != 0)
2080 bp->b_flags |= B_INVAL;
2084 * Clear the BV_BKGRDINPROG flag in the original buffer
2085 * and awaken it if it is waiting for the write to complete.
2086 * If BV_BKGRDINPROG is not set in the original buffer it must
2087 * have been released and re-instantiated - which is not legal.
2089 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2090 ("backgroundwritedone: lost buffer2"));
2091 origbp->b_vflags &= ~BV_BKGRDINPROG;
2092 if (origbp->b_vflags & BV_BKGRDWAIT) {
2093 origbp->b_vflags &= ~BV_BKGRDWAIT;
2094 wakeup(&origbp->b_xflags);
2101 * Write, release buffer on completion. (Done by iodone
2102 * if async). Do not bother writing anything if the buffer
2105 * Note that we set B_CACHE here, indicating that buffer is
2106 * fully valid and thus cacheable. This is true even of NFS
2107 * now so we set it generally. This could be set either here
2108 * or in biodone() since the I/O is synchronous. We put it
2112 ffs_bufwrite(struct buf *bp)
2117 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2118 if (bp->b_flags & B_INVAL) {
2123 if (!BUF_ISLOCKED(bp))
2124 panic("bufwrite: buffer is not busy???");
2126 * If a background write is already in progress, delay
2127 * writing this block if it is asynchronous. Otherwise
2128 * wait for the background write to complete.
2130 BO_LOCK(bp->b_bufobj);
2131 if (bp->b_vflags & BV_BKGRDINPROG) {
2132 if (bp->b_flags & B_ASYNC) {
2133 BO_UNLOCK(bp->b_bufobj);
2137 bp->b_vflags |= BV_BKGRDWAIT;
2138 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2140 if (bp->b_vflags & BV_BKGRDINPROG)
2141 panic("bufwrite: still writing");
2143 bp->b_vflags &= ~BV_BKGRDERR;
2144 BO_UNLOCK(bp->b_bufobj);
2147 * If this buffer is marked for background writing and we
2148 * do not have to wait for it, make a copy and write the
2149 * copy so as to leave this buffer ready for further use.
2151 * This optimization eats a lot of memory. If we have a page
2152 * or buffer shortfall we can't do it.
2154 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2155 (bp->b_flags & B_ASYNC) &&
2156 !vm_page_count_severe() &&
2157 !buf_dirty_count_severe()) {
2158 KASSERT(bp->b_iodone == NULL,
2159 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2161 /* get a new block */
2162 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2166 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2167 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2168 BO_LOCK(bp->b_bufobj);
2169 bp->b_vflags |= BV_BKGRDINPROG;
2170 BO_UNLOCK(bp->b_bufobj);
2172 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2173 newbp->b_lblkno = bp->b_lblkno;
2174 newbp->b_blkno = bp->b_blkno;
2175 newbp->b_offset = bp->b_offset;
2176 newbp->b_iodone = ffs_backgroundwritedone;
2177 newbp->b_flags |= B_ASYNC;
2178 newbp->b_flags &= ~B_INVAL;
2179 pbgetvp(bp->b_vp, newbp);
2183 * Move over the dependencies. If there are rollbacks,
2184 * leave the parent buffer dirtied as it will need to
2187 if (LIST_EMPTY(&bp->b_dep) ||
2188 softdep_move_dependencies(bp, newbp) == 0)
2195 * Initiate write on the copy, release the original. The
2196 * BKGRDINPROG flag prevents it from going away until
2197 * the background write completes. We have to recalculate
2198 * its check hash in case the buffer gets freed and then
2199 * reconstituted from the buffer cache during a later read.
2201 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2202 cgp = (struct cg *)bp->b_data;
2205 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2210 /* Mark the buffer clean */
2214 /* Let the normal bufwrite do the rest for us */
2217 * If we are writing a cylinder group, update its time.
2219 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2220 cgp = (struct cg *)bp->b_data;
2221 cgp->cg_old_time = cgp->cg_time = time_second;
2223 return (bufwrite(bp));
2228 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2235 if (bp->b_iocmd == BIO_WRITE) {
2236 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2237 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2238 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2239 panic("ffs_geom_strategy: bad I/O");
2240 nocopy = bp->b_flags & B_NOCOPY;
2241 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2242 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2243 vp->v_rdev->si_snapdata != NULL) {
2244 if ((bp->b_flags & B_CLUSTER) != 0) {
2245 runningbufwakeup(bp);
2246 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2247 b_cluster.cluster_entry) {
2248 error = ffs_copyonwrite(vp, tbp);
2250 error != EOPNOTSUPP) {
2251 bp->b_error = error;
2252 bp->b_ioflags |= BIO_ERROR;
2257 bp->b_runningbufspace = bp->b_bufsize;
2258 atomic_add_long(&runningbufspace,
2259 bp->b_runningbufspace);
2261 error = ffs_copyonwrite(vp, bp);
2262 if (error != 0 && error != EOPNOTSUPP) {
2263 bp->b_error = error;
2264 bp->b_ioflags |= BIO_ERROR;
2271 if ((bp->b_flags & B_CLUSTER) != 0) {
2272 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2273 b_cluster.cluster_entry) {
2274 if (!LIST_EMPTY(&tbp->b_dep))
2278 if (!LIST_EMPTY(&bp->b_dep))
2284 * Check for metadata that needs check-hashes and update them.
2286 switch (bp->b_xflags & BX_FSPRIV) {
2288 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2289 ((struct cg *)bp->b_data)->cg_ckhash =
2290 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2297 printf("Check-hash write is unimplemented!!!\n");
2304 printf("multiple buffer types 0x%b\n",
2305 (u_int)(bp->b_xflags & BX_FSPRIV),
2306 PRINT_UFS_BUF_XFLAGS);
2310 g_vfs_strategy(bo, bp);
2314 ffs_own_mount(const struct mount *mp)
2317 if (mp->mnt_op == &ufs_vfsops)
2325 /* defined in ffs_softdep.c */
2326 extern void db_print_ffs(struct ufsmount *ump);
2328 DB_SHOW_COMMAND(ffs, db_show_ffs)
2331 struct ufsmount *ump;
2334 ump = VFSTOUFS((struct mount *)addr);
2339 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2340 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2341 db_print_ffs(VFSTOUFS(mp));
2345 #endif /* SOFTUPDATES */