2 * Copyright (c) 1989, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
45 #include <sys/taskqueue.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
52 #include <sys/fcntl.h>
53 #include <sys/ioccom.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/rwlock.h>
58 #include <security/mac/mac_framework.h>
60 #include <ufs/ufs/extattr.h>
61 #include <ufs/ufs/gjournal.h>
62 #include <ufs/ufs/quota.h>
63 #include <ufs/ufs/ufsmount.h>
64 #include <ufs/ufs/inode.h>
65 #include <ufs/ufs/ufs_extern.h>
67 #include <ufs/ffs/fs.h>
68 #include <ufs/ffs/ffs_extern.h>
72 #include <vm/vm_page.h>
74 #include <geom/geom.h>
75 #include <geom/geom_vfs.h>
79 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
81 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
82 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
84 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
85 static int ffs_sync_lazy(struct mount *mp);
87 static vfs_init_t ffs_init;
88 static vfs_uninit_t ffs_uninit;
89 static vfs_extattrctl_t ffs_extattrctl;
90 static vfs_cmount_t ffs_cmount;
91 static vfs_unmount_t ffs_unmount;
92 static vfs_mount_t ffs_mount;
93 static vfs_statfs_t ffs_statfs;
94 static vfs_fhtovp_t ffs_fhtovp;
95 static vfs_sync_t ffs_sync;
97 static struct vfsops ufs_vfsops = {
98 .vfs_extattrctl = ffs_extattrctl,
99 .vfs_fhtovp = ffs_fhtovp,
100 .vfs_init = ffs_init,
101 .vfs_mount = ffs_mount,
102 .vfs_cmount = ffs_cmount,
103 .vfs_quotactl = ufs_quotactl,
104 .vfs_root = ufs_root,
105 .vfs_statfs = ffs_statfs,
106 .vfs_sync = ffs_sync,
107 .vfs_uninit = ffs_uninit,
108 .vfs_unmount = ffs_unmount,
109 .vfs_vget = ffs_vget,
110 .vfs_susp_clean = process_deferred_inactive,
113 VFS_SET(ufs_vfsops, ufs, 0);
114 MODULE_VERSION(ufs, 1);
116 static b_strategy_t ffs_geom_strategy;
117 static b_write_t ffs_bufwrite;
119 static struct buf_ops ffs_ops = {
121 .bop_write = ffs_bufwrite,
122 .bop_strategy = ffs_geom_strategy,
124 #ifdef NO_FFS_SNAPSHOT
125 .bop_bdflush = bufbdflush,
127 .bop_bdflush = ffs_bdflush,
132 * Note that userquota and groupquota options are not currently used
133 * by UFS/FFS code and generally mount(8) does not pass those options
134 * from userland, but they can be passed by loader(8) via
135 * vfs.root.mountfrom.options.
137 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
138 "noclusterw", "noexec", "export", "force", "from", "groupquota",
139 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
140 "nosymfollow", "sync", "union", "userquota", NULL };
143 ffs_mount(struct mount *mp)
147 struct ufsmount *ump = NULL;
153 struct nameidata ndp;
157 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
159 if (uma_inode == NULL) {
160 uma_inode = uma_zcreate("FFS inode",
161 sizeof(struct inode), NULL, NULL, NULL, NULL,
163 uma_ufs1 = uma_zcreate("FFS1 dinode",
164 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
166 uma_ufs2 = uma_zcreate("FFS2 dinode",
167 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
171 vfs_deleteopt(mp->mnt_optnew, "groupquota");
172 vfs_deleteopt(mp->mnt_optnew, "userquota");
174 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
179 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
180 mntorflags |= MNT_ACLS;
182 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
183 mntorflags |= MNT_SNAPSHOT;
185 * Once we have set the MNT_SNAPSHOT flag, do not
186 * persist "snapshot" in the options list.
188 vfs_deleteopt(mp->mnt_optnew, "snapshot");
189 vfs_deleteopt(mp->mnt_opt, "snapshot");
192 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
193 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
195 * Once we have set the restricted PID, do not
196 * persist "fsckpid" in the options list.
198 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
199 vfs_deleteopt(mp->mnt_opt, "fsckpid");
200 if (mp->mnt_flag & MNT_UPDATE) {
201 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
202 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
204 "Checker enable: Must be read-only");
207 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
209 "Checker enable: Must be read-only");
212 /* Set to -1 if we are done */
217 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
218 if (mntorflags & MNT_ACLS) {
220 "\"acls\" and \"nfsv4acls\" options "
221 "are mutually exclusive");
224 mntorflags |= MNT_NFS4ACLS;
228 mp->mnt_flag |= mntorflags;
231 * If updating, check whether changing from read-only to
232 * read/write; if there is no device name, that's all we do.
234 if (mp->mnt_flag & MNT_UPDATE) {
237 devvp = ump->um_devvp;
238 if (fsckpid == -1 && ump->um_fsckpid > 0) {
239 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
240 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
245 * Return to normal read-only mode.
247 error = g_access(ump->um_cp, 0, -1, 0);
252 if (fs->fs_ronly == 0 &&
253 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
255 * Flush any dirty data and suspend filesystem.
257 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
259 error = vfs_write_suspend_umnt(mp);
263 * Check for and optionally get rid of files open
267 if (mp->mnt_flag & MNT_FORCE)
269 if (MOUNTEDSOFTDEP(mp)) {
270 error = softdep_flushfiles(mp, flags, td);
272 error = ffs_flushfiles(mp, flags, td);
275 vfs_write_resume(mp, 0);
278 if (fs->fs_pendingblocks != 0 ||
279 fs->fs_pendinginodes != 0) {
280 printf("WARNING: %s Update error: blocks %jd "
281 "files %d\n", fs->fs_fsmnt,
282 (intmax_t)fs->fs_pendingblocks,
283 fs->fs_pendinginodes);
284 fs->fs_pendingblocks = 0;
285 fs->fs_pendinginodes = 0;
287 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
289 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
292 vfs_write_resume(mp, 0);
295 if (MOUNTEDSOFTDEP(mp))
300 * Drop our write and exclusive access.
302 g_access(ump->um_cp, 0, -1, -1);
307 mp->mnt_flag |= MNT_RDONLY;
310 * Allow the writers to note that filesystem
313 vfs_write_resume(mp, 0);
315 if ((mp->mnt_flag & MNT_RELOAD) &&
316 (error = ffs_reload(mp, td, 0)) != 0)
319 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
321 * If we are running a checker, do not allow upgrade.
323 if (ump->um_fsckpid > 0) {
325 "Active checker, cannot upgrade to write");
329 * If upgrade to read-write by non-root, then verify
330 * that user has necessary permissions on the device.
332 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
333 error = VOP_ACCESS(devvp, VREAD | VWRITE,
336 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
338 VOP_UNLOCK(devvp, 0);
341 VOP_UNLOCK(devvp, 0);
342 fs->fs_flags &= ~FS_UNCLEAN;
343 if (fs->fs_clean == 0) {
344 fs->fs_flags |= FS_UNCLEAN;
345 if ((mp->mnt_flag & MNT_FORCE) ||
347 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
348 (fs->fs_flags & FS_DOSOFTDEP))) {
349 printf("WARNING: %s was not properly "
350 "dismounted\n", fs->fs_fsmnt);
353 "R/W mount of %s denied. %s.%s",
355 "Filesystem is not clean - run fsck",
356 (fs->fs_flags & FS_SUJ) == 0 ? "" :
357 " Forced mount will invalidate"
358 " journal contents");
365 * Request exclusive write access.
367 error = g_access(ump->um_cp, 0, 1, 1);
372 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
376 mp->mnt_flag &= ~MNT_RDONLY;
378 fs->fs_mtime = time_second;
379 /* check to see if we need to start softdep */
380 if ((fs->fs_flags & FS_DOSOFTDEP) &&
381 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
382 vn_finished_write(mp);
386 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
387 vn_finished_write(mp);
390 if (fs->fs_snapinum[0] != 0)
391 ffs_snapshot_mount(mp);
392 vn_finished_write(mp);
395 * Soft updates is incompatible with "async",
396 * so if we are doing softupdates stop the user
397 * from setting the async flag in an update.
398 * Softdep_mount() clears it in an initial mount
401 if (MOUNTEDSOFTDEP(mp)) {
402 /* XXX: Reset too late ? */
404 mp->mnt_flag &= ~MNT_ASYNC;
408 * Keep MNT_ACLS flag if it is stored in superblock.
410 if ((fs->fs_flags & FS_ACLS) != 0) {
411 /* XXX: Set too late ? */
413 mp->mnt_flag |= MNT_ACLS;
417 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
418 /* XXX: Set too late ? */
420 mp->mnt_flag |= MNT_NFS4ACLS;
424 * If this is a request from fsck to clean up the filesystem,
425 * then allow the specified pid to proceed.
428 if (ump->um_fsckpid != 0) {
430 "Active checker already running on %s",
434 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
435 ("soft updates enabled on read-only file system"));
439 * Request write access.
441 error = g_access(ump->um_cp, 0, 1, 0);
446 "Checker activation failed on %s",
450 ump->um_fsckpid = fsckpid;
451 if (fs->fs_snapinum[0] != 0)
452 ffs_snapshot_mount(mp);
453 fs->fs_mtime = time_second;
456 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
460 * If this is a snapshot request, take the snapshot.
462 if (mp->mnt_flag & MNT_SNAPSHOT)
463 return (ffs_snapshot(mp, fspec));
467 * Not an update, or updating the name: look up the name
468 * and verify that it refers to a sensible disk device.
470 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
471 if ((error = namei(&ndp)) != 0)
473 NDFREE(&ndp, NDF_ONLY_PNBUF);
475 if (!vn_isdisk(devvp, &error)) {
481 * If mount by non-root, then verify that user has necessary
482 * permissions on the device.
485 if ((mp->mnt_flag & MNT_RDONLY) == 0)
487 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
489 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
495 if (mp->mnt_flag & MNT_UPDATE) {
499 * If it's not the same vnode, or at least the same device
500 * then it's not correct.
503 if (devvp->v_rdev != ump->um_devvp->v_rdev)
504 error = EINVAL; /* needs translation */
512 * We need the name for the mount point (also used for
513 * "last mounted on") copied in. If an error occurs,
514 * the mount point is discarded by the upper level code.
515 * Note that vfs_mount() populates f_mntonname for us.
517 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
522 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
523 ("soft updates enabled on read-only file system"));
529 * Request write access.
531 error = g_access(ump->um_cp, 0, 1, 0);
535 printf("WARNING: %s: Checker activation "
536 "failed\n", fs->fs_fsmnt);
538 ump->um_fsckpid = fsckpid;
539 if (fs->fs_snapinum[0] != 0)
540 ffs_snapshot_mount(mp);
541 fs->fs_mtime = time_second;
543 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
547 vfs_mountedfrom(mp, fspec);
552 * Compatibility with old mount system call.
556 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
558 struct ufs_args args;
559 struct export_args exp;
564 error = copyin(data, &args, sizeof args);
567 vfs_oexport_conv(&args.export, &exp);
569 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
570 ma = mount_arg(ma, "export", &exp, sizeof(exp));
571 error = kernel_mount(ma, flags);
577 * Reload all incore data for a filesystem (used after running fsck on
578 * the root filesystem and finding things to fix). If the 'force' flag
579 * is 0, the filesystem must be mounted read-only.
581 * Things to do to update the mount:
582 * 1) invalidate all cached meta-data.
583 * 2) re-read superblock from disk.
584 * 3) re-read summary information from disk.
585 * 4) invalidate all inactive vnodes.
586 * 5) invalidate all cached file data.
587 * 6) re-read inode data for all active vnodes.
590 ffs_reload(struct mount *mp, struct thread *td, int force)
592 struct vnode *vp, *mvp, *devvp;
596 struct fs *fs, *newfs;
597 struct ufsmount *ump;
598 ufs2_daddr_t sblockloc;
599 int i, blks, size, error;
605 if ((mp->mnt_flag & MNT_RDONLY) == 0 && force == 0) {
612 * Step 1: invalidate all cached meta-data.
614 devvp = VFSTOUFS(mp)->um_devvp;
615 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
616 if (vinvalbuf(devvp, 0, 0, 0) != 0)
617 panic("ffs_reload: dirty1");
618 VOP_UNLOCK(devvp, 0);
621 * Step 2: re-read superblock from disk.
623 fs = VFSTOUFS(mp)->um_fs;
624 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
627 newfs = (struct fs *)bp->b_data;
628 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
629 newfs->fs_magic != FS_UFS2_MAGIC) ||
630 newfs->fs_bsize > MAXBSIZE ||
631 newfs->fs_bsize < sizeof(struct fs)) {
633 return (EIO); /* XXX needs translation */
636 * Copy pointer fields back into superblock before copying in XXX
637 * new superblock. These should really be in the ufsmount. XXX
638 * Note that important parameters (eg fs_ncg) are unchanged.
640 newfs->fs_csp = fs->fs_csp;
641 newfs->fs_maxcluster = fs->fs_maxcluster;
642 newfs->fs_contigdirs = fs->fs_contigdirs;
643 newfs->fs_active = fs->fs_active;
644 newfs->fs_ronly = fs->fs_ronly;
645 sblockloc = fs->fs_sblockloc;
646 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
648 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
649 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
651 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
652 printf("WARNING: %s: reload pending error: blocks %jd "
653 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
654 fs->fs_pendinginodes);
655 fs->fs_pendingblocks = 0;
656 fs->fs_pendinginodes = 0;
661 * Step 3: re-read summary information from disk.
663 size = fs->fs_cssize;
664 blks = howmany(size, fs->fs_fsize);
665 if (fs->fs_contigsumsize > 0)
666 size += fs->fs_ncg * sizeof(int32_t);
667 size += fs->fs_ncg * sizeof(u_int8_t);
668 free(fs->fs_csp, M_UFSMNT);
669 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
671 for (i = 0; i < blks; i += fs->fs_frag) {
673 if (i + fs->fs_frag > blks)
674 size = (blks - i) * fs->fs_fsize;
675 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
679 bcopy(bp->b_data, space, (u_int)size);
680 space = (char *)space + size;
684 * We no longer know anything about clusters per cylinder group.
686 if (fs->fs_contigsumsize > 0) {
687 fs->fs_maxcluster = lp = space;
688 for (i = 0; i < fs->fs_ncg; i++)
689 *lp++ = fs->fs_contigsumsize;
692 size = fs->fs_ncg * sizeof(u_int8_t);
693 fs->fs_contigdirs = (u_int8_t *)space;
694 bzero(fs->fs_contigdirs, size);
697 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
701 if (vp->v_type == VNON) {
706 * Step 4: invalidate all cached file data.
708 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
709 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
712 if (vinvalbuf(vp, 0, 0, 0))
713 panic("ffs_reload: dirty2");
715 * Step 5: re-read inode data for all active vnodes.
719 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
720 (int)fs->fs_bsize, NOCRED, &bp);
724 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
727 ffs_load_inode(bp, ip, fs, ip->i_number);
728 ip->i_effnlink = ip->i_nlink;
737 * Possible superblock locations ordered from most to least likely.
739 static int sblock_try[] = SBLOCKSEARCH;
742 * Common code for mount and mountroot
745 ffs_mountfs(devvp, mp, td)
750 struct ufsmount *ump;
755 ufs2_daddr_t sblockloc;
756 int error, i, blks, size, ronly;
759 struct g_consumer *cp;
764 cred = td ? td->td_ucred : NOCRED;
765 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
771 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
774 VOP_UNLOCK(devvp, 0);
777 if (devvp->v_rdev->si_iosize_max != 0)
778 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
779 if (mp->mnt_iosize_max > MAXPHYS)
780 mp->mnt_iosize_max = MAXPHYS;
782 devvp->v_bufobj.bo_ops = &ffs_ops;
787 * Try reading the superblock in each of its possible locations.
789 for (i = 0; sblock_try[i] != -1; i++) {
790 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
793 "Invalid sectorsize %d for superblock size %d",
794 cp->provider->sectorsize, SBLOCKSIZE);
797 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
800 fs = (struct fs *)bp->b_data;
801 sblockloc = sblock_try[i];
802 if ((fs->fs_magic == FS_UFS1_MAGIC ||
803 (fs->fs_magic == FS_UFS2_MAGIC &&
804 (fs->fs_sblockloc == sblockloc ||
805 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
806 fs->fs_bsize <= MAXBSIZE &&
807 fs->fs_bsize >= sizeof(struct fs))
812 if (sblock_try[i] == -1) {
813 error = EINVAL; /* XXX needs translation */
817 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
818 fs->fs_flags &= ~FS_UNCLEAN;
819 if (fs->fs_clean == 0) {
820 fs->fs_flags |= FS_UNCLEAN;
821 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
822 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
823 (fs->fs_flags & FS_DOSOFTDEP))) {
824 printf("WARNING: %s was not properly dismounted\n",
827 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
828 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
829 (fs->fs_flags & FS_SUJ) == 0 ? "" :
830 " Forced mount will invalidate journal contents");
834 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
835 (mp->mnt_flag & MNT_FORCE)) {
836 printf("WARNING: %s: lost blocks %jd files %d\n",
837 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
838 fs->fs_pendinginodes);
839 fs->fs_pendingblocks = 0;
840 fs->fs_pendinginodes = 0;
843 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
844 printf("WARNING: %s: mount pending error: blocks %jd "
845 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
846 fs->fs_pendinginodes);
847 fs->fs_pendingblocks = 0;
848 fs->fs_pendinginodes = 0;
850 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
853 * Get journal provider name.
856 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
857 if (g_io_getattr("GJOURNAL::provider", cp, &size,
858 mp->mnt_gjprovider) == 0) {
859 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
862 mp->mnt_flag |= MNT_GJOURNAL;
865 printf("WARNING: %s: GJOURNAL flag on fs "
866 "but no gjournal provider below\n",
867 mp->mnt_stat.f_mntonname);
868 free(mp->mnt_gjprovider, M_UFSMNT);
869 mp->mnt_gjprovider = NULL;
872 printf("WARNING: %s: GJOURNAL flag on fs but no "
873 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
876 mp->mnt_gjprovider = NULL;
878 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
880 ump->um_bo = &devvp->v_bufobj;
881 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
882 if (fs->fs_magic == FS_UFS1_MAGIC) {
883 ump->um_fstype = UFS1;
884 ump->um_balloc = ffs_balloc_ufs1;
886 ump->um_fstype = UFS2;
887 ump->um_balloc = ffs_balloc_ufs2;
889 ump->um_blkatoff = ffs_blkatoff;
890 ump->um_truncate = ffs_truncate;
891 ump->um_update = ffs_update;
892 ump->um_valloc = ffs_valloc;
893 ump->um_vfree = ffs_vfree;
894 ump->um_ifree = ffs_ifree;
895 ump->um_rdonly = ffs_rdonly;
896 ump->um_snapgone = ffs_snapgone;
897 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
898 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
899 if (fs->fs_sbsize < SBLOCKSIZE)
900 bp->b_flags |= B_INVAL | B_NOCACHE;
904 ffs_oldfscompat_read(fs, ump, sblockloc);
905 fs->fs_ronly = ronly;
906 size = fs->fs_cssize;
907 blks = howmany(size, fs->fs_fsize);
908 if (fs->fs_contigsumsize > 0)
909 size += fs->fs_ncg * sizeof(int32_t);
910 size += fs->fs_ncg * sizeof(u_int8_t);
911 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
913 for (i = 0; i < blks; i += fs->fs_frag) {
915 if (i + fs->fs_frag > blks)
916 size = (blks - i) * fs->fs_fsize;
917 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
919 free(fs->fs_csp, M_UFSMNT);
922 bcopy(bp->b_data, space, (u_int)size);
923 space = (char *)space + size;
927 if (fs->fs_contigsumsize > 0) {
928 fs->fs_maxcluster = lp = space;
929 for (i = 0; i < fs->fs_ncg; i++)
930 *lp++ = fs->fs_contigsumsize;
933 size = fs->fs_ncg * sizeof(u_int8_t);
934 fs->fs_contigdirs = (u_int8_t *)space;
935 bzero(fs->fs_contigdirs, size);
936 fs->fs_active = NULL;
938 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
939 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
941 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
942 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
947 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
949 mp->mnt_flag |= MNT_LOCAL;
951 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
954 mp->mnt_flag |= MNT_MULTILABEL;
957 printf("WARNING: %s: multilabel flag on fs but "
958 "no MAC support\n", mp->mnt_stat.f_mntonname);
961 if ((fs->fs_flags & FS_ACLS) != 0) {
965 if (mp->mnt_flag & MNT_NFS4ACLS)
966 printf("WARNING: %s: ACLs flag on fs conflicts with "
967 "\"nfsv4acls\" mount option; option ignored\n",
968 mp->mnt_stat.f_mntonname);
969 mp->mnt_flag &= ~MNT_NFS4ACLS;
970 mp->mnt_flag |= MNT_ACLS;
974 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
975 mp->mnt_stat.f_mntonname);
978 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
982 if (mp->mnt_flag & MNT_ACLS)
983 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
984 "with \"acls\" mount option; option ignored\n",
985 mp->mnt_stat.f_mntonname);
986 mp->mnt_flag &= ~MNT_ACLS;
987 mp->mnt_flag |= MNT_NFS4ACLS;
991 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
992 "ACLs support\n", mp->mnt_stat.f_mntonname);
995 if ((fs->fs_flags & FS_TRIM) != 0) {
997 if (g_io_getattr("GEOM::candelete", cp, &size,
998 &ump->um_candelete) == 0) {
999 if (!ump->um_candelete)
1000 printf("WARNING: %s: TRIM flag on fs but disk "
1001 "does not support TRIM\n",
1002 mp->mnt_stat.f_mntonname);
1004 printf("WARNING: %s: TRIM flag on fs but disk does "
1005 "not confirm that it supports TRIM\n",
1006 mp->mnt_stat.f_mntonname);
1007 ump->um_candelete = 0;
1009 if (ump->um_candelete) {
1010 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1011 taskqueue_thread_enqueue, &ump->um_trim_tq);
1012 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1013 "%s trim", mp->mnt_stat.f_mntonname);
1017 ump->um_mountp = mp;
1019 ump->um_devvp = devvp;
1020 ump->um_nindir = fs->fs_nindir;
1021 ump->um_bptrtodb = fs->fs_fsbtodb;
1022 ump->um_seqinc = fs->fs_frag;
1023 for (i = 0; i < MAXQUOTAS; i++)
1024 ump->um_quotas[i] = NULLVP;
1026 ufs_extattr_uepm_init(&ump->um_extattr);
1029 * Set FS local "last mounted on" information (NULL pad)
1031 bzero(fs->fs_fsmnt, MAXMNTLEN);
1032 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1033 mp->mnt_stat.f_iosize = fs->fs_bsize;
1035 if (mp->mnt_flag & MNT_ROOTFS) {
1037 * Root mount; update timestamp in mount structure.
1038 * this will be used by the common root mount code
1039 * to update the system clock.
1041 mp->mnt_time = fs->fs_time;
1045 fs->fs_mtime = time_second;
1046 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1047 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1048 free(fs->fs_csp, M_UFSMNT);
1049 ffs_flushfiles(mp, FORCECLOSE, td);
1052 if (devvp->v_type == VCHR && devvp->v_rdev != NULL)
1053 devvp->v_rdev->si_mountpt = mp;
1054 if (fs->fs_snapinum[0] != 0)
1055 ffs_snapshot_mount(mp);
1058 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1061 * Initialize filesystem stat information in mount struct.
1064 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1065 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1068 #ifdef UFS_EXTATTR_AUTOSTART
1071 * Auto-starting does the following:
1072 * - check for /.attribute in the fs, and extattr_start if so
1073 * - for each file in .attribute, enable that file with
1074 * an attribute of the same name.
1075 * Not clear how to report errors -- probably eat them.
1076 * This would all happen while the filesystem was busy/not
1077 * available, so would effectively be "atomic".
1079 (void) ufs_extattr_autostart(mp, td);
1080 #endif /* !UFS_EXTATTR_AUTOSTART */
1081 #endif /* !UFS_EXTATTR */
1090 g_topology_unlock();
1094 mtx_destroy(UFS_MTX(ump));
1095 if (mp->mnt_gjprovider != NULL) {
1096 free(mp->mnt_gjprovider, M_UFSMNT);
1097 mp->mnt_gjprovider = NULL;
1099 free(ump->um_fs, M_UFSMNT);
1100 free(ump, M_UFSMNT);
1101 mp->mnt_data = NULL;
1107 #include <sys/sysctl.h>
1108 static int bigcgs = 0;
1109 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1112 * Sanity checks for loading old filesystem superblocks.
1113 * See ffs_oldfscompat_write below for unwound actions.
1115 * XXX - Parts get retired eventually.
1116 * Unfortunately new bits get added.
1119 ffs_oldfscompat_read(fs, ump, sblockloc)
1121 struct ufsmount *ump;
1122 ufs2_daddr_t sblockloc;
1127 * If not yet done, update fs_flags location and value of fs_sblockloc.
1129 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1130 fs->fs_flags = fs->fs_old_flags;
1131 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1132 fs->fs_sblockloc = sblockloc;
1135 * If not yet done, update UFS1 superblock with new wider fields.
1137 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1138 fs->fs_maxbsize = fs->fs_bsize;
1139 fs->fs_time = fs->fs_old_time;
1140 fs->fs_size = fs->fs_old_size;
1141 fs->fs_dsize = fs->fs_old_dsize;
1142 fs->fs_csaddr = fs->fs_old_csaddr;
1143 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1144 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1145 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1146 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1148 if (fs->fs_magic == FS_UFS1_MAGIC &&
1149 fs->fs_old_inodefmt < FS_44INODEFMT) {
1150 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1151 fs->fs_qbmask = ~fs->fs_bmask;
1152 fs->fs_qfmask = ~fs->fs_fmask;
1154 if (fs->fs_magic == FS_UFS1_MAGIC) {
1155 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1156 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1157 if (fs->fs_maxfilesize > maxfilesize)
1158 fs->fs_maxfilesize = maxfilesize;
1160 /* Compatibility for old filesystems */
1161 if (fs->fs_avgfilesize <= 0)
1162 fs->fs_avgfilesize = AVFILESIZ;
1163 if (fs->fs_avgfpdir <= 0)
1164 fs->fs_avgfpdir = AFPDIR;
1166 fs->fs_save_cgsize = fs->fs_cgsize;
1167 fs->fs_cgsize = fs->fs_bsize;
1172 * Unwinding superblock updates for old filesystems.
1173 * See ffs_oldfscompat_read above for details.
1175 * XXX - Parts get retired eventually.
1176 * Unfortunately new bits get added.
1179 ffs_oldfscompat_write(fs, ump)
1181 struct ufsmount *ump;
1185 * Copy back UFS2 updated fields that UFS1 inspects.
1187 if (fs->fs_magic == FS_UFS1_MAGIC) {
1188 fs->fs_old_time = fs->fs_time;
1189 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1190 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1191 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1192 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1193 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1196 fs->fs_cgsize = fs->fs_save_cgsize;
1197 fs->fs_save_cgsize = 0;
1202 * unmount system call
1205 ffs_unmount(mp, mntflags)
1210 struct ufsmount *ump = VFSTOUFS(mp);
1212 int error, flags, susp;
1221 if (mntflags & MNT_FORCE) {
1222 flags |= FORCECLOSE;
1223 susp = fs->fs_ronly == 0;
1226 if ((error = ufs_extattr_stop(mp, td))) {
1227 if (error != EOPNOTSUPP)
1228 printf("WARNING: unmount %s: ufs_extattr_stop "
1229 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1233 ufs_extattr_uepm_destroy(&ump->um_extattr);
1238 error = vfs_write_suspend_umnt(mp);
1242 if (MOUNTEDSOFTDEP(mp))
1243 error = softdep_flushfiles(mp, flags, td);
1245 error = ffs_flushfiles(mp, flags, td);
1246 if (error != 0 && error != ENXIO)
1250 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1251 printf("WARNING: unmount %s: pending error: blocks %jd "
1252 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1253 fs->fs_pendinginodes);
1254 fs->fs_pendingblocks = 0;
1255 fs->fs_pendinginodes = 0;
1258 if (MOUNTEDSOFTDEP(mp))
1259 softdep_unmount(mp);
1260 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1261 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1262 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1263 if (error && error != ENXIO) {
1269 vfs_write_resume(mp, VR_START_WRITE);
1270 if (ump->um_trim_tq != NULL) {
1271 while (ump->um_trim_inflight != 0)
1272 pause("ufsutr", hz);
1273 taskqueue_drain_all(ump->um_trim_tq);
1274 taskqueue_free(ump->um_trim_tq);
1278 if (ump->um_fsckpid > 0) {
1280 * Return to normal read-only mode.
1282 error = g_access(ump->um_cp, 0, -1, 0);
1283 ump->um_fsckpid = 0;
1285 g_vfs_close(ump->um_cp);
1286 g_topology_unlock();
1288 if (ump->um_devvp->v_type == VCHR && ump->um_devvp->v_rdev != NULL)
1289 ump->um_devvp->v_rdev->si_mountpt = NULL;
1290 vrele(ump->um_devvp);
1291 dev_rel(ump->um_dev);
1292 mtx_destroy(UFS_MTX(ump));
1293 if (mp->mnt_gjprovider != NULL) {
1294 free(mp->mnt_gjprovider, M_UFSMNT);
1295 mp->mnt_gjprovider = NULL;
1297 free(fs->fs_csp, M_UFSMNT);
1299 free(ump, M_UFSMNT);
1300 mp->mnt_data = NULL;
1302 mp->mnt_flag &= ~MNT_LOCAL;
1308 vfs_write_resume(mp, VR_START_WRITE);
1312 ufs_extattr_uepm_init(&ump->um_extattr);
1313 #ifdef UFS_EXTATTR_AUTOSTART
1314 (void) ufs_extattr_autostart(mp, td);
1323 * Flush out all the files in a filesystem.
1326 ffs_flushfiles(mp, flags, td)
1331 struct ufsmount *ump;
1337 if (mp->mnt_flag & MNT_QUOTA) {
1339 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1342 for (i = 0; i < MAXQUOTAS; i++) {
1343 error = quotaoff(td, mp, i);
1345 if ((flags & EARLYFLUSH) == 0)
1353 * Here we fall through to vflush again to ensure that
1354 * we have gotten rid of all the system vnodes, unless
1355 * quotas must not be closed.
1359 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1360 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1361 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1363 ffs_snapshot_unmount(mp);
1364 flags |= FORCECLOSE;
1366 * Here we fall through to vflush again to ensure
1367 * that we have gotten rid of all the system vnodes.
1372 * Do not close system files if quotas were not closed, to be
1373 * able to sync the remaining dquots. The freeblks softupdate
1374 * workitems might hold a reference on a dquot, preventing
1375 * quotaoff() from completing. Next round of
1376 * softdep_flushworklist() iteration should process the
1377 * blockers, allowing the next run of quotaoff() to finally
1378 * flush held dquots.
1380 * Otherwise, flush all the files.
1382 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1386 * Flush filesystem metadata.
1388 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1389 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1390 VOP_UNLOCK(ump->um_devvp, 0);
1395 * Get filesystem statistics.
1402 struct ufsmount *ump;
1407 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1408 panic("ffs_statfs");
1409 sbp->f_version = STATFS_VERSION;
1410 sbp->f_bsize = fs->fs_fsize;
1411 sbp->f_iosize = fs->fs_bsize;
1412 sbp->f_blocks = fs->fs_dsize;
1414 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1415 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1416 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1417 dbtofsb(fs, fs->fs_pendingblocks);
1418 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1419 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1421 sbp->f_namemax = NAME_MAX;
1426 sync_doupdate(struct inode *ip)
1429 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1434 * For a lazy sync, we only care about access times, quotas and the
1435 * superblock. Other filesystem changes are already converted to
1436 * cylinder group blocks or inode blocks updates and are written to
1443 struct vnode *mvp, *vp;
1446 int allerror, error;
1450 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1452 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1453 if (vp->v_type == VNON) {
1460 * The IN_ACCESS flag is converted to IN_MODIFIED by
1461 * ufs_close() and ufs_getattr() by the calls to
1462 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1463 * Test also all the other timestamp flags too, to pick up
1464 * any other cases that could be missed.
1466 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1470 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1473 if (sync_doupdate(ip))
1474 error = ffs_update(vp, 0);
1485 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1486 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1492 * Go through the disk queues to initiate sandbagged IO;
1493 * go through the inodes to write those that have been modified;
1494 * initiate the writing of the super block if it has been modified.
1496 * Note: we are always called with the filesystem marked busy using
1500 ffs_sync(mp, waitfor)
1504 struct vnode *mvp, *vp, *devvp;
1507 struct ufsmount *ump = VFSTOUFS(mp);
1509 int error, count, lockreq, allerror = 0;
1512 int secondary_writes;
1513 int secondary_accwrites;
1515 int softdep_accdeps;
1522 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1523 panic("%s: ffs_sync: modification on read-only filesystem",
1525 if (waitfor == MNT_LAZY) {
1527 return (ffs_sync_lazy(mp));
1528 waitfor = MNT_NOWAIT;
1532 * Write back each (modified) inode.
1534 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1535 if (waitfor == MNT_SUSPEND) {
1539 if (waitfor == MNT_WAIT)
1540 lockreq = LK_EXCLUSIVE;
1541 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1543 /* Grab snapshot of secondary write counts */
1545 secondary_writes = mp->mnt_secondary_writes;
1546 secondary_accwrites = mp->mnt_secondary_accwrites;
1549 /* Grab snapshot of softdep dependency counts */
1550 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1552 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1554 * Depend on the vnode interlock to keep things stable enough
1555 * for a quick test. Since there might be hundreds of
1556 * thousands of vnodes, we cannot afford even a subroutine
1557 * call unless there's a good chance that we have work to do.
1559 if (vp->v_type == VNON) {
1565 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1566 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1570 if ((error = vget(vp, lockreq, td)) != 0) {
1571 if (error == ENOENT || error == ENOLCK) {
1572 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1577 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1582 * Force stale filesystem control information to be flushed.
1584 if (waitfor == MNT_WAIT || rebooting) {
1585 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1587 /* Flushed work items may create new vnodes to clean */
1588 if (allerror == 0 && count)
1595 devvp = ump->um_devvp;
1596 bo = &devvp->v_bufobj;
1598 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1600 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1601 error = VOP_FSYNC(devvp, waitfor, td);
1602 VOP_UNLOCK(devvp, 0);
1603 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1604 error = ffs_sbupdate(ump, waitfor, 0);
1607 if (allerror == 0 && waitfor == MNT_WAIT)
1609 } else if (suspend != 0) {
1610 if (softdep_check_suspend(mp,
1615 secondary_accwrites) != 0) {
1617 goto loop; /* More work needed */
1619 mtx_assert(MNT_MTX(mp), MA_OWNED);
1620 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1626 * Write back modified superblock.
1628 if (fs->fs_fmod != 0 &&
1629 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1635 ffs_vget(mp, ino, flags, vpp)
1641 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1645 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1654 struct ufsmount *ump;
1660 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1661 if (error || *vpp != NULL)
1665 * We must promote to an exclusive lock for vnode creation. This
1666 * can happen if lookup is passed LOCKSHARED.
1668 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1669 flags &= ~LK_TYPE_MASK;
1670 flags |= LK_EXCLUSIVE;
1674 * We do not lock vnode creation as it is believed to be too
1675 * expensive for such rare case as simultaneous creation of vnode
1676 * for same ino by different processes. We just allow them to race
1677 * and check later to decide who wins. Let the race begin!
1683 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1685 /* Allocate a new vnode/inode. */
1686 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1687 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1690 uma_zfree(uma_inode, ip);
1694 * FFS supports recursive locking.
1696 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1699 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1706 ip->i_nextclustercg = -1;
1710 for (i = 0; i < MAXQUOTAS; i++)
1711 ip->i_dquot[i] = NODQUOT;
1715 if (ffs_flags & FFSV_FORCEINSMQ)
1716 vp->v_vflag |= VV_FORCEINSMQ;
1717 error = insmntque(vp, mp);
1719 uma_zfree(uma_inode, ip);
1723 vp->v_vflag &= ~VV_FORCEINSMQ;
1724 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1725 if (error || *vpp != NULL)
1728 /* Read in the disk contents for the inode, copy into the inode. */
1729 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1730 (int)fs->fs_bsize, NOCRED, &bp);
1733 * The inode does not contain anything useful, so it would
1734 * be misleading to leave it on its hash chain. With mode
1735 * still zero, it will be unlinked and returned to the free
1743 if (ip->i_ump->um_fstype == UFS1)
1744 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1746 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1747 ffs_load_inode(bp, ip, fs, ino);
1748 if (DOINGSOFTDEP(vp))
1749 softdep_load_inodeblock(ip);
1751 ip->i_effnlink = ip->i_nlink;
1755 * Initialize the vnode from the inode, check for aliases.
1756 * Note that the underlying vnode may have changed.
1758 if (ip->i_ump->um_fstype == UFS1)
1759 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1761 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1769 * Finish inode initialization.
1771 if (vp->v_type != VFIFO) {
1772 /* FFS supports shared locking for all files except fifos. */
1777 * Set up a generation number for this inode if it does not
1778 * already have one. This should only happen on old filesystems.
1780 if (ip->i_gen == 0) {
1781 ip->i_gen = arc4random() / 2 + 1;
1782 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1783 ip->i_flag |= IN_MODIFIED;
1784 DIP_SET(ip, i_gen, ip->i_gen);
1788 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1790 * If this vnode is already allocated, and we're running
1791 * multi-label, attempt to perform a label association
1792 * from the extended attributes on the inode.
1794 error = mac_vnode_associate_extattr(mp, vp);
1796 /* ufs_inactive will release ip->i_devvp ref. */
1809 * File handle to vnode
1811 * Have to be really careful about stale file handles:
1812 * - check that the inode number is valid
1813 * - call ffs_vget() to get the locked inode
1814 * - check for an unallocated inode (i_mode == 0)
1815 * - check that the given client host has export rights and return
1816 * those rights via. exflagsp and credanonp
1819 ffs_fhtovp(mp, fhp, flags, vpp)
1828 ufhp = (struct ufid *)fhp;
1829 fs = VFSTOUFS(mp)->um_fs;
1830 if (ufhp->ufid_ino < ROOTINO ||
1831 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1833 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1837 * Initialize the filesystem.
1841 struct vfsconf *vfsp;
1844 ffs_susp_initialize();
1845 softdep_initialize();
1846 return (ufs_init(vfsp));
1850 * Undo the work of ffs_init().
1854 struct vfsconf *vfsp;
1858 ret = ufs_uninit(vfsp);
1859 softdep_uninitialize();
1860 ffs_susp_uninitialize();
1865 * Write a superblock and associated information back to disk.
1868 ffs_sbupdate(ump, waitfor, suspended)
1869 struct ufsmount *ump;
1873 struct fs *fs = ump->um_fs;
1878 int i, size, error, allerror = 0;
1880 if (fs->fs_ronly == 1 &&
1881 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1882 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1883 panic("ffs_sbupdate: write read-only filesystem");
1885 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1887 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1888 (int)fs->fs_sbsize, 0, 0, 0);
1890 * First write back the summary information.
1892 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1894 for (i = 0; i < blks; i += fs->fs_frag) {
1895 size = fs->fs_bsize;
1896 if (i + fs->fs_frag > blks)
1897 size = (blks - i) * fs->fs_fsize;
1898 bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1900 bcopy(space, bp->b_data, (u_int)size);
1901 space = (char *)space + size;
1903 bp->b_flags |= B_VALIDSUSPWRT;
1904 if (waitfor != MNT_WAIT)
1906 else if ((error = bwrite(bp)) != 0)
1910 * Now write back the superblock itself. If any errors occurred
1911 * up to this point, then fail so that the superblock avoids
1912 * being written out as clean.
1919 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1920 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1921 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1922 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1923 fs->fs_sblockloc = SBLOCK_UFS1;
1925 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1926 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1927 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1928 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1929 fs->fs_sblockloc = SBLOCK_UFS2;
1932 fs->fs_time = time_second;
1933 if (MOUNTEDSOFTDEP(ump->um_mountp))
1934 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1935 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1936 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1938 bp->b_flags |= B_VALIDSUSPWRT;
1939 if (waitfor != MNT_WAIT)
1941 else if ((error = bwrite(bp)) != 0)
1947 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1948 int attrnamespace, const char *attrname)
1952 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1955 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1961 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1964 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1965 uma_zfree(uma_ufs1, ip->i_din1);
1966 else if (ip->i_din2 != NULL)
1967 uma_zfree(uma_ufs2, ip->i_din2);
1968 uma_zfree(uma_inode, ip);
1971 static int dobkgrdwrite = 1;
1972 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1973 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1976 * Complete a background write started from bwrite.
1979 ffs_backgroundwritedone(struct buf *bp)
1981 struct bufobj *bufobj;
1985 * Find the original buffer that we are writing.
1987 bufobj = bp->b_bufobj;
1989 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1990 panic("backgroundwritedone: lost buffer");
1993 * We should mark the cylinder group buffer origbp as
1994 * dirty, to not loose the failed write.
1996 if ((bp->b_ioflags & BIO_ERROR) != 0)
1997 origbp->b_vflags |= BV_BKGRDERR;
2000 * Process dependencies then return any unfinished ones.
2003 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2006 if (!LIST_EMPTY(&bp->b_dep))
2007 softdep_move_dependencies(bp, origbp);
2010 * This buffer is marked B_NOCACHE so when it is released
2011 * by biodone it will be tossed.
2013 bp->b_flags |= B_NOCACHE;
2014 bp->b_flags &= ~B_CACHE;
2017 * Prevent brelse() from trying to keep and re-dirtying bp on
2018 * errors. It causes b_bufobj dereference in
2019 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2022 if ((bp->b_ioflags & BIO_ERROR) != 0)
2023 bp->b_flags |= B_INVAL;
2027 * Clear the BV_BKGRDINPROG flag in the original buffer
2028 * and awaken it if it is waiting for the write to complete.
2029 * If BV_BKGRDINPROG is not set in the original buffer it must
2030 * have been released and re-instantiated - which is not legal.
2032 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2033 ("backgroundwritedone: lost buffer2"));
2034 origbp->b_vflags &= ~BV_BKGRDINPROG;
2035 if (origbp->b_vflags & BV_BKGRDWAIT) {
2036 origbp->b_vflags &= ~BV_BKGRDWAIT;
2037 wakeup(&origbp->b_xflags);
2044 * Write, release buffer on completion. (Done by iodone
2045 * if async). Do not bother writing anything if the buffer
2048 * Note that we set B_CACHE here, indicating that buffer is
2049 * fully valid and thus cacheable. This is true even of NFS
2050 * now so we set it generally. This could be set either here
2051 * or in biodone() since the I/O is synchronous. We put it
2055 ffs_bufwrite(struct buf *bp)
2059 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2060 if (bp->b_flags & B_INVAL) {
2065 if (!BUF_ISLOCKED(bp))
2066 panic("bufwrite: buffer is not busy???");
2068 * If a background write is already in progress, delay
2069 * writing this block if it is asynchronous. Otherwise
2070 * wait for the background write to complete.
2072 BO_LOCK(bp->b_bufobj);
2073 if (bp->b_vflags & BV_BKGRDINPROG) {
2074 if (bp->b_flags & B_ASYNC) {
2075 BO_UNLOCK(bp->b_bufobj);
2079 bp->b_vflags |= BV_BKGRDWAIT;
2080 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2082 if (bp->b_vflags & BV_BKGRDINPROG)
2083 panic("bufwrite: still writing");
2085 bp->b_vflags &= ~BV_BKGRDERR;
2086 BO_UNLOCK(bp->b_bufobj);
2089 * If this buffer is marked for background writing and we
2090 * do not have to wait for it, make a copy and write the
2091 * copy so as to leave this buffer ready for further use.
2093 * This optimization eats a lot of memory. If we have a page
2094 * or buffer shortfall we can't do it.
2096 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2097 (bp->b_flags & B_ASYNC) &&
2098 !vm_page_count_severe() &&
2099 !buf_dirty_count_severe()) {
2100 KASSERT(bp->b_iodone == NULL,
2101 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2103 /* get a new block */
2104 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2108 KASSERT((bp->b_flags & B_UNMAPPED) == 0, ("Unmapped cg"));
2109 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2110 BO_LOCK(bp->b_bufobj);
2111 bp->b_vflags |= BV_BKGRDINPROG;
2112 BO_UNLOCK(bp->b_bufobj);
2113 newbp->b_xflags |= BX_BKGRDMARKER;
2114 newbp->b_lblkno = bp->b_lblkno;
2115 newbp->b_blkno = bp->b_blkno;
2116 newbp->b_offset = bp->b_offset;
2117 newbp->b_iodone = ffs_backgroundwritedone;
2118 newbp->b_flags |= B_ASYNC;
2119 newbp->b_flags &= ~B_INVAL;
2120 pbgetvp(bp->b_vp, newbp);
2124 * Move over the dependencies. If there are rollbacks,
2125 * leave the parent buffer dirtied as it will need to
2128 if (LIST_EMPTY(&bp->b_dep) ||
2129 softdep_move_dependencies(bp, newbp) == 0)
2136 * Initiate write on the copy, release the original. The
2137 * BKGRDINPROG flag prevents it from going away until
2138 * the background write completes.
2143 /* Mark the buffer clean */
2147 /* Let the normal bufwrite do the rest for us */
2149 return (bufwrite(bp));
2154 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2161 vp = bo->__bo_vnode;
2162 if (bp->b_iocmd == BIO_WRITE) {
2163 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2164 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2165 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2166 panic("ffs_geom_strategy: bad I/O");
2167 nocopy = bp->b_flags & B_NOCOPY;
2168 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2169 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2170 vp->v_rdev->si_snapdata != NULL) {
2171 if ((bp->b_flags & B_CLUSTER) != 0) {
2172 runningbufwakeup(bp);
2173 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2174 b_cluster.cluster_entry) {
2175 error = ffs_copyonwrite(vp, tbp);
2177 error != EOPNOTSUPP) {
2178 bp->b_error = error;
2179 bp->b_ioflags |= BIO_ERROR;
2184 bp->b_runningbufspace = bp->b_bufsize;
2185 atomic_add_long(&runningbufspace,
2186 bp->b_runningbufspace);
2188 error = ffs_copyonwrite(vp, bp);
2189 if (error != 0 && error != EOPNOTSUPP) {
2190 bp->b_error = error;
2191 bp->b_ioflags |= BIO_ERROR;
2198 if ((bp->b_flags & B_CLUSTER) != 0) {
2199 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2200 b_cluster.cluster_entry) {
2201 if (!LIST_EMPTY(&tbp->b_dep))
2205 if (!LIST_EMPTY(&bp->b_dep))
2211 g_vfs_strategy(bo, bp);
2215 ffs_own_mount(const struct mount *mp)
2218 if (mp->mnt_op == &ufs_vfsops)
2226 /* defined in ffs_softdep.c */
2227 extern void db_print_ffs(struct ufsmount *ump);
2229 DB_SHOW_COMMAND(ffs, db_show_ffs)
2232 struct ufsmount *ump;
2235 ump = VFSTOUFS((struct mount *)addr);
2240 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2241 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2242 db_print_ffs(VFSTOUFS(mp));
2246 #endif /* SOFTUPDATES */