2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_quota.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
47 #include <sys/taskqueue.h>
48 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
54 #include <sys/fcntl.h>
55 #include <sys/ioccom.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/rwlock.h>
59 #include <sys/vmmeter.h>
61 #include <security/mac/mac_framework.h>
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/gjournal.h>
66 #include <ufs/ufs/quota.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ffs/fs.h>
72 #include <ufs/ffs/ffs_extern.h>
76 #include <vm/vm_page.h>
78 #include <geom/geom.h>
79 #include <geom/geom_vfs.h>
83 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
85 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
86 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
88 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
89 static int ffs_sync_lazy(struct mount *mp);
90 static int ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
91 static int ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
93 static vfs_init_t ffs_init;
94 static vfs_uninit_t ffs_uninit;
95 static vfs_extattrctl_t ffs_extattrctl;
96 static vfs_cmount_t ffs_cmount;
97 static vfs_unmount_t ffs_unmount;
98 static vfs_mount_t ffs_mount;
99 static vfs_statfs_t ffs_statfs;
100 static vfs_fhtovp_t ffs_fhtovp;
101 static vfs_sync_t ffs_sync;
103 static struct vfsops ufs_vfsops = {
104 .vfs_extattrctl = ffs_extattrctl,
105 .vfs_fhtovp = ffs_fhtovp,
106 .vfs_init = ffs_init,
107 .vfs_mount = ffs_mount,
108 .vfs_cmount = ffs_cmount,
109 .vfs_quotactl = ufs_quotactl,
110 .vfs_root = ufs_root,
111 .vfs_statfs = ffs_statfs,
112 .vfs_sync = ffs_sync,
113 .vfs_uninit = ffs_uninit,
114 .vfs_unmount = ffs_unmount,
115 .vfs_vget = ffs_vget,
116 .vfs_susp_clean = process_deferred_inactive,
119 VFS_SET(ufs_vfsops, ufs, 0);
120 MODULE_VERSION(ufs, 1);
122 static b_strategy_t ffs_geom_strategy;
123 static b_write_t ffs_bufwrite;
125 static struct buf_ops ffs_ops = {
127 .bop_write = ffs_bufwrite,
128 .bop_strategy = ffs_geom_strategy,
130 #ifdef NO_FFS_SNAPSHOT
131 .bop_bdflush = bufbdflush,
133 .bop_bdflush = ffs_bdflush,
138 * Note that userquota and groupquota options are not currently used
139 * by UFS/FFS code and generally mount(8) does not pass those options
140 * from userland, but they can be passed by loader(8) via
141 * vfs.root.mountfrom.options.
143 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
144 "noclusterw", "noexec", "export", "force", "from", "groupquota",
145 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
146 "nosymfollow", "sync", "union", "userquota", "untrusted", NULL };
149 ffs_mount(struct mount *mp)
153 struct ufsmount *ump = NULL;
156 int error, error1, flags;
157 uint64_t mntorflags, saved_mnt_flag;
159 struct nameidata ndp;
163 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
165 if (uma_inode == NULL) {
166 uma_inode = uma_zcreate("FFS inode",
167 sizeof(struct inode), NULL, NULL, NULL, NULL,
169 uma_ufs1 = uma_zcreate("FFS1 dinode",
170 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
172 uma_ufs2 = uma_zcreate("FFS2 dinode",
173 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
177 vfs_deleteopt(mp->mnt_optnew, "groupquota");
178 vfs_deleteopt(mp->mnt_optnew, "userquota");
180 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
185 if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
186 mntorflags |= MNT_UNTRUSTED;
188 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
189 mntorflags |= MNT_ACLS;
191 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
192 mntorflags |= MNT_SNAPSHOT;
194 * Once we have set the MNT_SNAPSHOT flag, do not
195 * persist "snapshot" in the options list.
197 vfs_deleteopt(mp->mnt_optnew, "snapshot");
198 vfs_deleteopt(mp->mnt_opt, "snapshot");
201 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
202 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
204 * Once we have set the restricted PID, do not
205 * persist "fsckpid" in the options list.
207 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
208 vfs_deleteopt(mp->mnt_opt, "fsckpid");
209 if (mp->mnt_flag & MNT_UPDATE) {
210 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
211 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
213 "Checker enable: Must be read-only");
216 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
218 "Checker enable: Must be read-only");
221 /* Set to -1 if we are done */
226 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
227 if (mntorflags & MNT_ACLS) {
229 "\"acls\" and \"nfsv4acls\" options "
230 "are mutually exclusive");
233 mntorflags |= MNT_NFS4ACLS;
237 mp->mnt_flag |= mntorflags;
240 * If updating, check whether changing from read-only to
241 * read/write; if there is no device name, that's all we do.
243 if (mp->mnt_flag & MNT_UPDATE) {
246 devvp = ump->um_devvp;
247 if (fsckpid == -1 && ump->um_fsckpid > 0) {
248 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
249 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
253 * Return to normal read-only mode.
255 error = g_access(ump->um_cp, 0, -1, 0);
259 if (fs->fs_ronly == 0 &&
260 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
262 * Flush any dirty data and suspend filesystem.
264 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
266 error = vfs_write_suspend_umnt(mp);
270 * Check for and optionally get rid of files open
274 if (mp->mnt_flag & MNT_FORCE)
276 if (MOUNTEDSOFTDEP(mp)) {
277 error = softdep_flushfiles(mp, flags, td);
279 error = ffs_flushfiles(mp, flags, td);
282 vfs_write_resume(mp, 0);
285 if (fs->fs_pendingblocks != 0 ||
286 fs->fs_pendinginodes != 0) {
287 printf("WARNING: %s Update error: blocks %jd "
288 "files %d\n", fs->fs_fsmnt,
289 (intmax_t)fs->fs_pendingblocks,
290 fs->fs_pendinginodes);
291 fs->fs_pendingblocks = 0;
292 fs->fs_pendinginodes = 0;
294 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
296 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
299 vfs_write_resume(mp, 0);
302 if (MOUNTEDSOFTDEP(mp))
306 * Drop our write and exclusive access.
308 g_access(ump->um_cp, 0, -1, -1);
312 mp->mnt_flag |= MNT_RDONLY;
315 * Allow the writers to note that filesystem
318 vfs_write_resume(mp, 0);
320 if ((mp->mnt_flag & MNT_RELOAD) &&
321 (error = ffs_reload(mp, td, 0)) != 0)
324 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
326 * If we are running a checker, do not allow upgrade.
328 if (ump->um_fsckpid > 0) {
330 "Active checker, cannot upgrade to write");
334 * If upgrade to read-write by non-root, then verify
335 * that user has necessary permissions on the device.
337 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
338 error = VOP_ACCESS(devvp, VREAD | VWRITE,
341 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
343 VOP_UNLOCK(devvp, 0);
346 VOP_UNLOCK(devvp, 0);
347 fs->fs_flags &= ~FS_UNCLEAN;
348 if (fs->fs_clean == 0) {
349 fs->fs_flags |= FS_UNCLEAN;
350 if ((mp->mnt_flag & MNT_FORCE) ||
352 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
353 (fs->fs_flags & FS_DOSOFTDEP))) {
354 printf("WARNING: %s was not properly "
355 "dismounted\n", fs->fs_fsmnt);
358 "R/W mount of %s denied. %s.%s",
360 "Filesystem is not clean - run fsck",
361 (fs->fs_flags & FS_SUJ) == 0 ? "" :
362 " Forced mount will invalidate"
363 " journal contents");
369 * Request exclusive write access.
371 error = g_access(ump->um_cp, 0, 1, 1);
375 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
377 error = vfs_write_suspend_umnt(mp);
382 saved_mnt_flag = MNT_RDONLY;
383 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
385 saved_mnt_flag |= MNT_ASYNC;
386 mp->mnt_flag &= ~saved_mnt_flag;
388 fs->fs_mtime = time_second;
389 /* check to see if we need to start softdep */
390 if ((fs->fs_flags & FS_DOSOFTDEP) &&
391 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
394 mp->mnt_flag |= saved_mnt_flag;
396 vfs_write_resume(mp, 0);
400 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
403 mp->mnt_flag |= saved_mnt_flag;
405 vfs_write_resume(mp, 0);
408 if (fs->fs_snapinum[0] != 0)
409 ffs_snapshot_mount(mp);
410 vfs_write_resume(mp, 0);
413 * Soft updates is incompatible with "async",
414 * so if we are doing softupdates stop the user
415 * from setting the async flag in an update.
416 * Softdep_mount() clears it in an initial mount
419 if (MOUNTEDSOFTDEP(mp)) {
420 /* XXX: Reset too late ? */
422 mp->mnt_flag &= ~MNT_ASYNC;
426 * Keep MNT_ACLS flag if it is stored in superblock.
428 if ((fs->fs_flags & FS_ACLS) != 0) {
429 /* XXX: Set too late ? */
431 mp->mnt_flag |= MNT_ACLS;
435 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
436 /* XXX: Set too late ? */
438 mp->mnt_flag |= MNT_NFS4ACLS;
442 * If this is a request from fsck to clean up the filesystem,
443 * then allow the specified pid to proceed.
446 if (ump->um_fsckpid != 0) {
448 "Active checker already running on %s",
452 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
453 ("soft updates enabled on read-only file system"));
456 * Request write access.
458 error = g_access(ump->um_cp, 0, 1, 0);
462 "Checker activation failed on %s",
466 ump->um_fsckpid = fsckpid;
467 if (fs->fs_snapinum[0] != 0)
468 ffs_snapshot_mount(mp);
469 fs->fs_mtime = time_second;
472 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
476 * If this is a snapshot request, take the snapshot.
478 if (mp->mnt_flag & MNT_SNAPSHOT)
479 return (ffs_snapshot(mp, fspec));
482 * Must not call namei() while owning busy ref.
488 * Not an update, or updating the name: look up the name
489 * and verify that it refers to a sensible disk device.
491 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
493 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
495 * Unmount does not start if MNT_UPDATE is set. Mount
496 * update busies mp before setting MNT_UPDATE. We
497 * must be able to retain our busy ref succesfully,
500 error1 = vfs_busy(mp, MBF_NOWAIT);
505 NDFREE(&ndp, NDF_ONLY_PNBUF);
507 if (!vn_isdisk(devvp, &error)) {
513 * If mount by non-root, then verify that user has necessary
514 * permissions on the device.
517 if ((mp->mnt_flag & MNT_RDONLY) == 0)
519 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
521 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
527 if (mp->mnt_flag & MNT_UPDATE) {
531 * If it's not the same vnode, or at least the same device
532 * then it's not correct.
535 if (devvp->v_rdev != ump->um_devvp->v_rdev)
536 error = EINVAL; /* needs translation */
544 * We need the name for the mount point (also used for
545 * "last mounted on") copied in. If an error occurs,
546 * the mount point is discarded by the upper level code.
547 * Note that vfs_mount_alloc() populates f_mntonname for us.
549 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
554 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
555 ("soft updates enabled on read-only file system"));
560 * Request write access.
562 error = g_access(ump->um_cp, 0, 1, 0);
565 printf("WARNING: %s: Checker activation "
566 "failed\n", fs->fs_fsmnt);
568 ump->um_fsckpid = fsckpid;
569 if (fs->fs_snapinum[0] != 0)
570 ffs_snapshot_mount(mp);
571 fs->fs_mtime = time_second;
573 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
577 vfs_mountedfrom(mp, fspec);
582 * Compatibility with old mount system call.
586 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
588 struct ufs_args args;
589 struct export_args exp;
594 error = copyin(data, &args, sizeof args);
597 vfs_oexport_conv(&args.export, &exp);
599 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
600 ma = mount_arg(ma, "export", &exp, sizeof(exp));
601 error = kernel_mount(ma, flags);
607 * Reload all incore data for a filesystem (used after running fsck on
608 * the root filesystem and finding things to fix). If the 'force' flag
609 * is 0, the filesystem must be mounted read-only.
611 * Things to do to update the mount:
612 * 1) invalidate all cached meta-data.
613 * 2) re-read superblock from disk.
614 * 3) re-read summary information from disk.
615 * 4) invalidate all inactive vnodes.
616 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
617 * writers, if requested.
618 * 6) invalidate all cached file data.
619 * 7) re-read inode data for all active vnodes.
622 ffs_reload(struct mount *mp, struct thread *td, int flags)
624 struct vnode *vp, *mvp, *devvp;
628 struct fs *fs, *newfs;
629 struct ufsmount *ump;
630 ufs2_daddr_t sblockloc;
638 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
645 * Step 1: invalidate all cached meta-data.
647 devvp = VFSTOUFS(mp)->um_devvp;
648 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
649 if (vinvalbuf(devvp, 0, 0, 0) != 0)
650 panic("ffs_reload: dirty1");
651 VOP_UNLOCK(devvp, 0);
654 * Step 2: re-read superblock from disk.
656 fs = VFSTOUFS(mp)->um_fs;
657 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
660 newfs = (struct fs *)bp->b_data;
661 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
662 newfs->fs_magic != FS_UFS2_MAGIC) ||
663 newfs->fs_bsize > MAXBSIZE ||
664 newfs->fs_bsize < sizeof(struct fs)) {
666 return (EIO); /* XXX needs translation */
669 * Copy pointer fields back into superblock before copying in XXX
670 * new superblock. These should really be in the ufsmount. XXX
671 * Note that important parameters (eg fs_ncg) are unchanged.
673 newfs->fs_csp = fs->fs_csp;
674 newfs->fs_maxcluster = fs->fs_maxcluster;
675 newfs->fs_contigdirs = fs->fs_contigdirs;
676 newfs->fs_active = fs->fs_active;
677 newfs->fs_ronly = fs->fs_ronly;
678 sblockloc = fs->fs_sblockloc;
679 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
681 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
682 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
684 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
685 printf("WARNING: %s: reload pending error: blocks %jd "
686 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
687 fs->fs_pendinginodes);
688 fs->fs_pendingblocks = 0;
689 fs->fs_pendinginodes = 0;
694 * Step 3: re-read summary information from disk.
696 size = fs->fs_cssize;
697 blks = howmany(size, fs->fs_fsize);
698 if (fs->fs_contigsumsize > 0)
699 size += fs->fs_ncg * sizeof(int32_t);
700 size += fs->fs_ncg * sizeof(u_int8_t);
701 free(fs->fs_csp, M_UFSMNT);
702 space = malloc(size, M_UFSMNT, M_WAITOK);
704 for (i = 0; i < blks; i += fs->fs_frag) {
706 if (i + fs->fs_frag > blks)
707 size = (blks - i) * fs->fs_fsize;
708 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
712 bcopy(bp->b_data, space, (u_int)size);
713 space = (char *)space + size;
717 * We no longer know anything about clusters per cylinder group.
719 if (fs->fs_contigsumsize > 0) {
720 fs->fs_maxcluster = lp = space;
721 for (i = 0; i < fs->fs_ncg; i++)
722 *lp++ = fs->fs_contigsumsize;
725 size = fs->fs_ncg * sizeof(u_int8_t);
726 fs->fs_contigdirs = (u_int8_t *)space;
727 bzero(fs->fs_contigdirs, size);
728 if ((flags & FFSR_UNSUSPEND) != 0) {
730 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
731 wakeup(&mp->mnt_flag);
736 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
740 if (vp->v_type == VNON) {
745 * Step 4: invalidate all cached file data.
747 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
748 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
751 if (vinvalbuf(vp, 0, 0, 0))
752 panic("ffs_reload: dirty2");
754 * Step 5: re-read inode data for all active vnodes.
758 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
759 (int)fs->fs_bsize, NOCRED, &bp);
763 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
766 ffs_load_inode(bp, ip, fs, ip->i_number);
767 ip->i_effnlink = ip->i_nlink;
776 * Common code for mount and mountroot
779 ffs_mountfs(devvp, mp, td)
784 struct ufsmount *ump;
787 int error, i, len, ronly;
789 struct g_consumer *cp;
795 cred = td ? td->td_ucred : NOCRED;
796 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
798 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
800 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
801 (uintptr_t)mp) == 0) {
802 VOP_UNLOCK(devvp, 0);
806 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
809 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
810 VOP_UNLOCK(devvp, 0);
814 devvp->v_bufobj.bo_ops = &ffs_ops;
815 VOP_UNLOCK(devvp, 0);
816 if (dev->si_iosize_max != 0)
817 mp->mnt_iosize_max = dev->si_iosize_max;
818 if (mp->mnt_iosize_max > MAXPHYS)
819 mp->mnt_iosize_max = MAXPHYS;
820 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
823 "Invalid sectorsize %d for superblock size %d",
824 cp->provider->sectorsize, SBLOCKSIZE);
827 /* fetch the superblock and summary information */
828 if ((error = ffs_sbget(devvp, &fs, -1, M_UFSMNT, ffs_use_bread)) != 0)
831 /* if we ran on a kernel without metadata check hashes, disable them */
832 if ((fs->fs_flags & FS_METACKHASH) == 0)
833 fs->fs_metackhash = 0;
834 /* none of these types of check-hashes are maintained by this kernel */
835 fs->fs_metackhash &= ~(CK_SUPERBLOCK | CK_INODE | CK_INDIR | CK_DIR);
836 /* no support for any undefined flags */
837 fs->fs_flags &= FS_SUPPORTED;
838 fs->fs_flags &= ~FS_UNCLEAN;
839 if (fs->fs_clean == 0) {
840 fs->fs_flags |= FS_UNCLEAN;
841 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
842 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
843 (fs->fs_flags & FS_DOSOFTDEP))) {
844 printf("WARNING: %s was not properly dismounted\n",
847 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
848 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
849 (fs->fs_flags & FS_SUJ) == 0 ? "" :
850 " Forced mount will invalidate journal contents");
854 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
855 (mp->mnt_flag & MNT_FORCE)) {
856 printf("WARNING: %s: lost blocks %jd files %d\n",
857 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
858 fs->fs_pendinginodes);
859 fs->fs_pendingblocks = 0;
860 fs->fs_pendinginodes = 0;
863 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
864 printf("WARNING: %s: mount pending error: blocks %jd "
865 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
866 fs->fs_pendinginodes);
867 fs->fs_pendingblocks = 0;
868 fs->fs_pendinginodes = 0;
870 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
873 * Get journal provider name.
876 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
877 if (g_io_getattr("GJOURNAL::provider", cp, &len,
878 mp->mnt_gjprovider) == 0) {
879 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
882 mp->mnt_flag |= MNT_GJOURNAL;
885 printf("WARNING: %s: GJOURNAL flag on fs "
886 "but no gjournal provider below\n",
887 mp->mnt_stat.f_mntonname);
888 free(mp->mnt_gjprovider, M_UFSMNT);
889 mp->mnt_gjprovider = NULL;
892 printf("WARNING: %s: GJOURNAL flag on fs but no "
893 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
896 mp->mnt_gjprovider = NULL;
898 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
900 ump->um_bo = &devvp->v_bufobj;
902 if (fs->fs_magic == FS_UFS1_MAGIC) {
903 ump->um_fstype = UFS1;
904 ump->um_balloc = ffs_balloc_ufs1;
906 ump->um_fstype = UFS2;
907 ump->um_balloc = ffs_balloc_ufs2;
909 ump->um_blkatoff = ffs_blkatoff;
910 ump->um_truncate = ffs_truncate;
911 ump->um_update = ffs_update;
912 ump->um_valloc = ffs_valloc;
913 ump->um_vfree = ffs_vfree;
914 ump->um_ifree = ffs_ifree;
915 ump->um_rdonly = ffs_rdonly;
916 ump->um_snapgone = ffs_snapgone;
917 if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
918 ump->um_check_blkno = ffs_check_blkno;
920 ump->um_check_blkno = NULL;
921 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
922 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
923 fs->fs_ronly = ronly;
924 fs->fs_active = NULL;
926 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
927 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
929 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
930 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
935 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
937 mp->mnt_flag |= MNT_LOCAL;
939 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
942 mp->mnt_flag |= MNT_MULTILABEL;
945 printf("WARNING: %s: multilabel flag on fs but "
946 "no MAC support\n", mp->mnt_stat.f_mntonname);
949 if ((fs->fs_flags & FS_ACLS) != 0) {
953 if (mp->mnt_flag & MNT_NFS4ACLS)
954 printf("WARNING: %s: ACLs flag on fs conflicts with "
955 "\"nfsv4acls\" mount option; option ignored\n",
956 mp->mnt_stat.f_mntonname);
957 mp->mnt_flag &= ~MNT_NFS4ACLS;
958 mp->mnt_flag |= MNT_ACLS;
962 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
963 mp->mnt_stat.f_mntonname);
966 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
970 if (mp->mnt_flag & MNT_ACLS)
971 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
972 "with \"acls\" mount option; option ignored\n",
973 mp->mnt_stat.f_mntonname);
974 mp->mnt_flag &= ~MNT_ACLS;
975 mp->mnt_flag |= MNT_NFS4ACLS;
979 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
980 "ACLs support\n", mp->mnt_stat.f_mntonname);
983 if ((fs->fs_flags & FS_TRIM) != 0) {
985 if (g_io_getattr("GEOM::candelete", cp, &len,
988 ump->um_flags |= UM_CANDELETE;
990 printf("WARNING: %s: TRIM flag on fs but disk "
991 "does not support TRIM\n",
992 mp->mnt_stat.f_mntonname);
994 printf("WARNING: %s: TRIM flag on fs but disk does "
995 "not confirm that it supports TRIM\n",
996 mp->mnt_stat.f_mntonname);
998 if (((ump->um_flags) & UM_CANDELETE) != 0) {
999 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1000 taskqueue_thread_enqueue, &ump->um_trim_tq);
1001 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1002 "%s trim", mp->mnt_stat.f_mntonname);
1003 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1004 &ump->um_trimlisthashsize);
1008 ump->um_mountp = mp;
1010 ump->um_devvp = devvp;
1011 ump->um_nindir = fs->fs_nindir;
1012 ump->um_bptrtodb = fs->fs_fsbtodb;
1013 ump->um_seqinc = fs->fs_frag;
1014 for (i = 0; i < MAXQUOTAS; i++)
1015 ump->um_quotas[i] = NULLVP;
1017 ufs_extattr_uepm_init(&ump->um_extattr);
1020 * Set FS local "last mounted on" information (NULL pad)
1022 bzero(fs->fs_fsmnt, MAXMNTLEN);
1023 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1024 mp->mnt_stat.f_iosize = fs->fs_bsize;
1026 if (mp->mnt_flag & MNT_ROOTFS) {
1028 * Root mount; update timestamp in mount structure.
1029 * this will be used by the common root mount code
1030 * to update the system clock.
1032 mp->mnt_time = fs->fs_time;
1036 fs->fs_mtime = time_second;
1037 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1038 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1039 ffs_flushfiles(mp, FORCECLOSE, td);
1042 if (fs->fs_snapinum[0] != 0)
1043 ffs_snapshot_mount(mp);
1046 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1049 * Initialize filesystem state information in mount struct.
1052 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1053 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1056 #ifdef UFS_EXTATTR_AUTOSTART
1059 * Auto-starting does the following:
1060 * - check for /.attribute in the fs, and extattr_start if so
1061 * - for each file in .attribute, enable that file with
1062 * an attribute of the same name.
1063 * Not clear how to report errors -- probably eat them.
1064 * This would all happen while the filesystem was busy/not
1065 * available, so would effectively be "atomic".
1067 (void) ufs_extattr_autostart(mp, td);
1068 #endif /* !UFS_EXTATTR_AUTOSTART */
1069 #endif /* !UFS_EXTATTR */
1073 free(fs->fs_csp, M_UFSMNT);
1079 g_topology_unlock();
1082 mtx_destroy(UFS_MTX(ump));
1083 if (mp->mnt_gjprovider != NULL) {
1084 free(mp->mnt_gjprovider, M_UFSMNT);
1085 mp->mnt_gjprovider = NULL;
1087 free(ump, M_UFSMNT);
1088 mp->mnt_data = NULL;
1090 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1096 * A read function for use by filesystem-layer routines.
1099 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1104 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1105 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1106 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1109 bcopy(bp->b_data, *bufp, size);
1110 bp->b_flags |= B_INVAL | B_NOCACHE;
1115 #include <sys/sysctl.h>
1116 static int bigcgs = 0;
1117 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1120 * Sanity checks for loading old filesystem superblocks.
1121 * See ffs_oldfscompat_write below for unwound actions.
1123 * XXX - Parts get retired eventually.
1124 * Unfortunately new bits get added.
1127 ffs_oldfscompat_read(fs, ump, sblockloc)
1129 struct ufsmount *ump;
1130 ufs2_daddr_t sblockloc;
1135 * If not yet done, update fs_flags location and value of fs_sblockloc.
1137 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1138 fs->fs_flags = fs->fs_old_flags;
1139 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1140 fs->fs_sblockloc = sblockloc;
1143 * If not yet done, update UFS1 superblock with new wider fields.
1145 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1146 fs->fs_maxbsize = fs->fs_bsize;
1147 fs->fs_time = fs->fs_old_time;
1148 fs->fs_size = fs->fs_old_size;
1149 fs->fs_dsize = fs->fs_old_dsize;
1150 fs->fs_csaddr = fs->fs_old_csaddr;
1151 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1152 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1153 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1154 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1156 if (fs->fs_magic == FS_UFS1_MAGIC &&
1157 fs->fs_old_inodefmt < FS_44INODEFMT) {
1158 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1159 fs->fs_qbmask = ~fs->fs_bmask;
1160 fs->fs_qfmask = ~fs->fs_fmask;
1162 if (fs->fs_magic == FS_UFS1_MAGIC) {
1163 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1164 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1165 if (fs->fs_maxfilesize > maxfilesize)
1166 fs->fs_maxfilesize = maxfilesize;
1168 /* Compatibility for old filesystems */
1169 if (fs->fs_avgfilesize <= 0)
1170 fs->fs_avgfilesize = AVFILESIZ;
1171 if (fs->fs_avgfpdir <= 0)
1172 fs->fs_avgfpdir = AFPDIR;
1174 fs->fs_save_cgsize = fs->fs_cgsize;
1175 fs->fs_cgsize = fs->fs_bsize;
1180 * Unwinding superblock updates for old filesystems.
1181 * See ffs_oldfscompat_read above for details.
1183 * XXX - Parts get retired eventually.
1184 * Unfortunately new bits get added.
1187 ffs_oldfscompat_write(fs, ump)
1189 struct ufsmount *ump;
1193 * Copy back UFS2 updated fields that UFS1 inspects.
1195 if (fs->fs_magic == FS_UFS1_MAGIC) {
1196 fs->fs_old_time = fs->fs_time;
1197 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1198 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1199 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1200 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1201 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1204 fs->fs_cgsize = fs->fs_save_cgsize;
1205 fs->fs_save_cgsize = 0;
1210 * unmount system call
1213 ffs_unmount(mp, mntflags)
1218 struct ufsmount *ump = VFSTOUFS(mp);
1220 int error, flags, susp;
1229 if (mntflags & MNT_FORCE) {
1230 flags |= FORCECLOSE;
1231 susp = fs->fs_ronly == 0;
1234 if ((error = ufs_extattr_stop(mp, td))) {
1235 if (error != EOPNOTSUPP)
1236 printf("WARNING: unmount %s: ufs_extattr_stop "
1237 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1241 ufs_extattr_uepm_destroy(&ump->um_extattr);
1246 error = vfs_write_suspend_umnt(mp);
1250 if (MOUNTEDSOFTDEP(mp))
1251 error = softdep_flushfiles(mp, flags, td);
1253 error = ffs_flushfiles(mp, flags, td);
1254 if (error != 0 && error != ENXIO)
1258 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1259 printf("WARNING: unmount %s: pending error: blocks %jd "
1260 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1261 fs->fs_pendinginodes);
1262 fs->fs_pendingblocks = 0;
1263 fs->fs_pendinginodes = 0;
1266 if (MOUNTEDSOFTDEP(mp))
1267 softdep_unmount(mp);
1268 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1269 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1270 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1271 if (error && error != ENXIO) {
1277 vfs_write_resume(mp, VR_START_WRITE);
1278 if (ump->um_trim_tq != NULL) {
1279 while (ump->um_trim_inflight != 0)
1280 pause("ufsutr", hz);
1281 taskqueue_drain_all(ump->um_trim_tq);
1282 taskqueue_free(ump->um_trim_tq);
1283 free (ump->um_trimhash, M_TRIM);
1286 if (ump->um_fsckpid > 0) {
1288 * Return to normal read-only mode.
1290 error = g_access(ump->um_cp, 0, -1, 0);
1291 ump->um_fsckpid = 0;
1293 g_vfs_close(ump->um_cp);
1294 g_topology_unlock();
1295 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1296 vrele(ump->um_devvp);
1297 dev_rel(ump->um_dev);
1298 mtx_destroy(UFS_MTX(ump));
1299 if (mp->mnt_gjprovider != NULL) {
1300 free(mp->mnt_gjprovider, M_UFSMNT);
1301 mp->mnt_gjprovider = NULL;
1303 free(fs->fs_csp, M_UFSMNT);
1305 free(ump, M_UFSMNT);
1306 mp->mnt_data = NULL;
1308 mp->mnt_flag &= ~MNT_LOCAL;
1310 if (td->td_su == mp) {
1318 vfs_write_resume(mp, VR_START_WRITE);
1322 ufs_extattr_uepm_init(&ump->um_extattr);
1323 #ifdef UFS_EXTATTR_AUTOSTART
1324 (void) ufs_extattr_autostart(mp, td);
1333 * Flush out all the files in a filesystem.
1336 ffs_flushfiles(mp, flags, td)
1341 struct ufsmount *ump;
1347 if (mp->mnt_flag & MNT_QUOTA) {
1349 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1352 for (i = 0; i < MAXQUOTAS; i++) {
1353 error = quotaoff(td, mp, i);
1355 if ((flags & EARLYFLUSH) == 0)
1363 * Here we fall through to vflush again to ensure that
1364 * we have gotten rid of all the system vnodes, unless
1365 * quotas must not be closed.
1369 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1370 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1371 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1373 ffs_snapshot_unmount(mp);
1374 flags |= FORCECLOSE;
1376 * Here we fall through to vflush again to ensure
1377 * that we have gotten rid of all the system vnodes.
1382 * Do not close system files if quotas were not closed, to be
1383 * able to sync the remaining dquots. The freeblks softupdate
1384 * workitems might hold a reference on a dquot, preventing
1385 * quotaoff() from completing. Next round of
1386 * softdep_flushworklist() iteration should process the
1387 * blockers, allowing the next run of quotaoff() to finally
1388 * flush held dquots.
1390 * Otherwise, flush all the files.
1392 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1396 * Flush filesystem metadata.
1398 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1399 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1400 VOP_UNLOCK(ump->um_devvp, 0);
1405 * Get filesystem statistics.
1412 struct ufsmount *ump;
1417 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1418 panic("ffs_statfs");
1419 sbp->f_version = STATFS_VERSION;
1420 sbp->f_bsize = fs->fs_fsize;
1421 sbp->f_iosize = fs->fs_bsize;
1422 sbp->f_blocks = fs->fs_dsize;
1424 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1425 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1426 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1427 dbtofsb(fs, fs->fs_pendingblocks);
1428 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1429 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1431 sbp->f_namemax = UFS_MAXNAMLEN;
1436 sync_doupdate(struct inode *ip)
1439 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1444 * For a lazy sync, we only care about access times, quotas and the
1445 * superblock. Other filesystem changes are already converted to
1446 * cylinder group blocks or inode blocks updates and are written to
1453 struct vnode *mvp, *vp;
1456 int allerror, error;
1460 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1462 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1463 if (vp->v_type == VNON) {
1470 * The IN_ACCESS flag is converted to IN_MODIFIED by
1471 * ufs_close() and ufs_getattr() by the calls to
1472 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1473 * Test also all the other timestamp flags too, to pick up
1474 * any other cases that could be missed.
1476 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1480 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1483 if (sync_doupdate(ip))
1484 error = ffs_update(vp, 0);
1495 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1496 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1502 * Go through the disk queues to initiate sandbagged IO;
1503 * go through the inodes to write those that have been modified;
1504 * initiate the writing of the super block if it has been modified.
1506 * Note: we are always called with the filesystem marked busy using
1510 ffs_sync(mp, waitfor)
1514 struct vnode *mvp, *vp, *devvp;
1517 struct ufsmount *ump = VFSTOUFS(mp);
1519 int error, count, lockreq, allerror = 0;
1522 int secondary_writes;
1523 int secondary_accwrites;
1525 int softdep_accdeps;
1532 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1533 panic("%s: ffs_sync: modification on read-only filesystem",
1535 if (waitfor == MNT_LAZY) {
1537 return (ffs_sync_lazy(mp));
1538 waitfor = MNT_NOWAIT;
1542 * Write back each (modified) inode.
1544 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1545 if (waitfor == MNT_SUSPEND) {
1549 if (waitfor == MNT_WAIT)
1550 lockreq = LK_EXCLUSIVE;
1551 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1553 /* Grab snapshot of secondary write counts */
1555 secondary_writes = mp->mnt_secondary_writes;
1556 secondary_accwrites = mp->mnt_secondary_accwrites;
1559 /* Grab snapshot of softdep dependency counts */
1560 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1562 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1564 * Depend on the vnode interlock to keep things stable enough
1565 * for a quick test. Since there might be hundreds of
1566 * thousands of vnodes, we cannot afford even a subroutine
1567 * call unless there's a good chance that we have work to do.
1569 if (vp->v_type == VNON) {
1575 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1576 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1580 if ((error = vget(vp, lockreq, td)) != 0) {
1581 if (error == ENOENT || error == ENOLCK) {
1582 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1587 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1592 * Force stale filesystem control information to be flushed.
1594 if (waitfor == MNT_WAIT || rebooting) {
1595 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1597 /* Flushed work items may create new vnodes to clean */
1598 if (allerror == 0 && count)
1605 devvp = ump->um_devvp;
1606 bo = &devvp->v_bufobj;
1608 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1610 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1611 error = VOP_FSYNC(devvp, waitfor, td);
1612 VOP_UNLOCK(devvp, 0);
1613 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1614 error = ffs_sbupdate(ump, waitfor, 0);
1617 if (allerror == 0 && waitfor == MNT_WAIT)
1619 } else if (suspend != 0) {
1620 if (softdep_check_suspend(mp,
1625 secondary_accwrites) != 0) {
1627 goto loop; /* More work needed */
1629 mtx_assert(MNT_MTX(mp), MA_OWNED);
1630 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1636 * Write back modified superblock.
1638 if (fs->fs_fmod != 0 &&
1639 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1645 ffs_vget(mp, ino, flags, vpp)
1651 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1655 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1664 struct ufsmount *ump;
1669 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1670 if (error || *vpp != NULL)
1674 * We must promote to an exclusive lock for vnode creation. This
1675 * can happen if lookup is passed LOCKSHARED.
1677 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1678 flags &= ~LK_TYPE_MASK;
1679 flags |= LK_EXCLUSIVE;
1683 * We do not lock vnode creation as it is believed to be too
1684 * expensive for such rare case as simultaneous creation of vnode
1685 * for same ino by different processes. We just allow them to race
1686 * and check later to decide who wins. Let the race begin!
1691 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1693 /* Allocate a new vnode/inode. */
1694 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1695 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1698 uma_zfree(uma_inode, ip);
1702 * FFS supports recursive locking.
1704 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1707 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1712 ip->i_nextclustercg = -1;
1713 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1717 for (i = 0; i < MAXQUOTAS; i++)
1718 ip->i_dquot[i] = NODQUOT;
1722 if (ffs_flags & FFSV_FORCEINSMQ)
1723 vp->v_vflag |= VV_FORCEINSMQ;
1724 error = insmntque(vp, mp);
1726 uma_zfree(uma_inode, ip);
1730 vp->v_vflag &= ~VV_FORCEINSMQ;
1731 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1732 if (error || *vpp != NULL)
1735 /* Read in the disk contents for the inode, copy into the inode. */
1736 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1737 (int)fs->fs_bsize, NOCRED, &bp);
1740 * The inode does not contain anything useful, so it would
1741 * be misleading to leave it on its hash chain. With mode
1742 * still zero, it will be unlinked and returned to the free
1751 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1753 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1754 ffs_load_inode(bp, ip, fs, ino);
1755 if (DOINGSOFTDEP(vp))
1756 softdep_load_inodeblock(ip);
1758 ip->i_effnlink = ip->i_nlink;
1762 * Initialize the vnode from the inode, check for aliases.
1763 * Note that the underlying vnode may have changed.
1765 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1774 * Finish inode initialization.
1776 if (vp->v_type != VFIFO) {
1777 /* FFS supports shared locking for all files except fifos. */
1782 * Set up a generation number for this inode if it does not
1783 * already have one. This should only happen on old filesystems.
1785 if (ip->i_gen == 0) {
1786 while (ip->i_gen == 0)
1787 ip->i_gen = arc4random();
1788 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1789 ip->i_flag |= IN_MODIFIED;
1790 DIP_SET(ip, i_gen, ip->i_gen);
1794 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1796 * If this vnode is already allocated, and we're running
1797 * multi-label, attempt to perform a label association
1798 * from the extended attributes on the inode.
1800 error = mac_vnode_associate_extattr(mp, vp);
1802 /* ufs_inactive will release ip->i_devvp ref. */
1815 * File handle to vnode
1817 * Have to be really careful about stale file handles:
1818 * - check that the inode number is valid
1819 * - for UFS2 check that the inode number is initialized
1820 * - call ffs_vget() to get the locked inode
1821 * - check for an unallocated inode (i_mode == 0)
1822 * - check that the given client host has export rights and return
1823 * those rights via. exflagsp and credanonp
1826 ffs_fhtovp(mp, fhp, flags, vpp)
1833 struct ufsmount *ump;
1841 ufhp = (struct ufid *)fhp;
1842 ino = ufhp->ufid_ino;
1845 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1848 * Need to check if inode is initialized because UFS2 does lazy
1849 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1851 if (fs->fs_magic != FS_UFS2_MAGIC)
1852 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1853 cg = ino_to_cg(fs, ino);
1854 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0)
1856 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1861 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1865 * Initialize the filesystem.
1869 struct vfsconf *vfsp;
1872 ffs_susp_initialize();
1873 softdep_initialize();
1874 return (ufs_init(vfsp));
1878 * Undo the work of ffs_init().
1882 struct vfsconf *vfsp;
1886 ret = ufs_uninit(vfsp);
1887 softdep_uninitialize();
1888 ffs_susp_uninitialize();
1893 * Structure used to pass information from ffs_sbupdate to its
1894 * helper routine ffs_use_bwrite.
1897 struct ufsmount *ump;
1905 * Write a superblock and associated information back to disk.
1908 ffs_sbupdate(ump, waitfor, suspended)
1909 struct ufsmount *ump;
1918 if (fs->fs_ronly == 1 &&
1919 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1920 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1921 panic("ffs_sbupdate: write read-only filesystem");
1923 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1925 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1926 (int)fs->fs_sbsize, 0, 0, 0);
1928 * Initialize info needed for write function.
1932 devfd.waitfor = waitfor;
1933 devfd.suspended = suspended;
1935 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
1939 * Write function for use by filesystem-layer routines.
1942 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
1944 struct devfd *devfdp;
1945 struct ufsmount *ump;
1954 * Writing the superblock summary information.
1956 if (loc != fs->fs_sblockloc) {
1957 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
1958 bcopy(buf, bp->b_data, (u_int)size);
1959 if (devfdp->suspended)
1960 bp->b_flags |= B_VALIDSUSPWRT;
1961 if (devfdp->waitfor != MNT_WAIT)
1963 else if ((error = bwrite(bp)) != 0)
1964 devfdp->error = error;
1968 * Writing the superblock itself. We need to do special checks for it.
1971 if (devfdp->error != 0) {
1973 return (devfdp->error);
1975 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1976 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1977 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1978 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1979 fs->fs_sblockloc = SBLOCK_UFS1;
1981 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1982 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1983 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1984 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1985 fs->fs_sblockloc = SBLOCK_UFS2;
1987 if (MOUNTEDSOFTDEP(ump->um_mountp))
1988 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1989 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1990 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1991 if (devfdp->suspended)
1992 bp->b_flags |= B_VALIDSUSPWRT;
1993 if (devfdp->waitfor != MNT_WAIT)
1995 else if ((error = bwrite(bp)) != 0)
1996 devfdp->error = error;
1997 return (devfdp->error);
2001 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2002 int attrnamespace, const char *attrname)
2006 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2009 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2015 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2018 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2019 uma_zfree(uma_ufs1, ip->i_din1);
2020 else if (ip->i_din2 != NULL)
2021 uma_zfree(uma_ufs2, ip->i_din2);
2022 uma_zfree(uma_inode, ip);
2025 static int dobkgrdwrite = 1;
2026 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2027 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2030 * Complete a background write started from bwrite.
2033 ffs_backgroundwritedone(struct buf *bp)
2035 struct bufobj *bufobj;
2039 * Find the original buffer that we are writing.
2041 bufobj = bp->b_bufobj;
2043 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2044 panic("backgroundwritedone: lost buffer");
2047 * We should mark the cylinder group buffer origbp as
2048 * dirty, to not loose the failed write.
2050 if ((bp->b_ioflags & BIO_ERROR) != 0)
2051 origbp->b_vflags |= BV_BKGRDERR;
2054 * Process dependencies then return any unfinished ones.
2056 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2059 if (!LIST_EMPTY(&bp->b_dep))
2060 softdep_move_dependencies(bp, origbp);
2063 * This buffer is marked B_NOCACHE so when it is released
2064 * by biodone it will be tossed.
2066 bp->b_flags |= B_NOCACHE;
2067 bp->b_flags &= ~B_CACHE;
2071 * Prevent brelse() from trying to keep and re-dirtying bp on
2072 * errors. It causes b_bufobj dereference in
2073 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2076 if ((bp->b_ioflags & BIO_ERROR) != 0)
2077 bp->b_flags |= B_INVAL;
2081 * Clear the BV_BKGRDINPROG flag in the original buffer
2082 * and awaken it if it is waiting for the write to complete.
2083 * If BV_BKGRDINPROG is not set in the original buffer it must
2084 * have been released and re-instantiated - which is not legal.
2086 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2087 ("backgroundwritedone: lost buffer2"));
2088 origbp->b_vflags &= ~BV_BKGRDINPROG;
2089 if (origbp->b_vflags & BV_BKGRDWAIT) {
2090 origbp->b_vflags &= ~BV_BKGRDWAIT;
2091 wakeup(&origbp->b_xflags);
2098 * Write, release buffer on completion. (Done by iodone
2099 * if async). Do not bother writing anything if the buffer
2102 * Note that we set B_CACHE here, indicating that buffer is
2103 * fully valid and thus cacheable. This is true even of NFS
2104 * now so we set it generally. This could be set either here
2105 * or in biodone() since the I/O is synchronous. We put it
2109 ffs_bufwrite(struct buf *bp)
2114 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2115 if (bp->b_flags & B_INVAL) {
2120 if (!BUF_ISLOCKED(bp))
2121 panic("bufwrite: buffer is not busy???");
2123 * If a background write is already in progress, delay
2124 * writing this block if it is asynchronous. Otherwise
2125 * wait for the background write to complete.
2127 BO_LOCK(bp->b_bufobj);
2128 if (bp->b_vflags & BV_BKGRDINPROG) {
2129 if (bp->b_flags & B_ASYNC) {
2130 BO_UNLOCK(bp->b_bufobj);
2134 bp->b_vflags |= BV_BKGRDWAIT;
2135 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2137 if (bp->b_vflags & BV_BKGRDINPROG)
2138 panic("bufwrite: still writing");
2140 bp->b_vflags &= ~BV_BKGRDERR;
2141 BO_UNLOCK(bp->b_bufobj);
2144 * If this buffer is marked for background writing and we
2145 * do not have to wait for it, make a copy and write the
2146 * copy so as to leave this buffer ready for further use.
2148 * This optimization eats a lot of memory. If we have a page
2149 * or buffer shortfall we can't do it.
2151 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2152 (bp->b_flags & B_ASYNC) &&
2153 !vm_page_count_severe() &&
2154 !buf_dirty_count_severe()) {
2155 KASSERT(bp->b_iodone == NULL,
2156 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2158 /* get a new block */
2159 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2163 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2164 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2165 BO_LOCK(bp->b_bufobj);
2166 bp->b_vflags |= BV_BKGRDINPROG;
2167 BO_UNLOCK(bp->b_bufobj);
2169 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2170 newbp->b_lblkno = bp->b_lblkno;
2171 newbp->b_blkno = bp->b_blkno;
2172 newbp->b_offset = bp->b_offset;
2173 newbp->b_iodone = ffs_backgroundwritedone;
2174 newbp->b_flags |= B_ASYNC;
2175 newbp->b_flags &= ~B_INVAL;
2176 pbgetvp(bp->b_vp, newbp);
2180 * Move over the dependencies. If there are rollbacks,
2181 * leave the parent buffer dirtied as it will need to
2184 if (LIST_EMPTY(&bp->b_dep) ||
2185 softdep_move_dependencies(bp, newbp) == 0)
2192 * Initiate write on the copy, release the original. The
2193 * BKGRDINPROG flag prevents it from going away until
2194 * the background write completes. We have to recalculate
2195 * its check hash in case the buffer gets freed and then
2196 * reconstituted from the buffer cache during a later read.
2198 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2199 cgp = (struct cg *)bp->b_data;
2202 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2207 /* Mark the buffer clean */
2211 /* Let the normal bufwrite do the rest for us */
2214 * If we are writing a cylinder group, update its time.
2216 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2217 cgp = (struct cg *)bp->b_data;
2218 cgp->cg_old_time = cgp->cg_time = time_second;
2220 return (bufwrite(bp));
2225 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2232 if (bp->b_iocmd == BIO_WRITE) {
2233 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2234 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2235 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2236 panic("ffs_geom_strategy: bad I/O");
2237 nocopy = bp->b_flags & B_NOCOPY;
2238 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2239 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2240 vp->v_rdev->si_snapdata != NULL) {
2241 if ((bp->b_flags & B_CLUSTER) != 0) {
2242 runningbufwakeup(bp);
2243 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2244 b_cluster.cluster_entry) {
2245 error = ffs_copyonwrite(vp, tbp);
2247 error != EOPNOTSUPP) {
2248 bp->b_error = error;
2249 bp->b_ioflags |= BIO_ERROR;
2254 bp->b_runningbufspace = bp->b_bufsize;
2255 atomic_add_long(&runningbufspace,
2256 bp->b_runningbufspace);
2258 error = ffs_copyonwrite(vp, bp);
2259 if (error != 0 && error != EOPNOTSUPP) {
2260 bp->b_error = error;
2261 bp->b_ioflags |= BIO_ERROR;
2268 if ((bp->b_flags & B_CLUSTER) != 0) {
2269 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2270 b_cluster.cluster_entry) {
2271 if (!LIST_EMPTY(&tbp->b_dep))
2275 if (!LIST_EMPTY(&bp->b_dep))
2281 * Check for metadata that needs check-hashes and update them.
2283 switch (bp->b_xflags & BX_FSPRIV) {
2285 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2286 ((struct cg *)bp->b_data)->cg_ckhash =
2287 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2294 printf("Check-hash write is unimplemented!!!\n");
2301 printf("multiple buffer types 0x%b\n",
2302 (u_int)(bp->b_xflags & BX_FSPRIV),
2303 PRINT_UFS_BUF_XFLAGS);
2307 g_vfs_strategy(bo, bp);
2311 ffs_own_mount(const struct mount *mp)
2314 if (mp->mnt_op == &ufs_vfsops)
2322 /* defined in ffs_softdep.c */
2323 extern void db_print_ffs(struct ufsmount *ump);
2325 DB_SHOW_COMMAND(ffs, db_show_ffs)
2328 struct ufsmount *ump;
2331 ump = VFSTOUFS((struct mount *)addr);
2336 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2337 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2338 db_print_ffs(VFSTOUFS(mp));
2342 #endif /* SOFTUPDATES */