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/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <security/mac/mac_framework.h>
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/gjournal.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_extern.h>
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
69 #include <vm/vm_page.h>
71 #include <geom/geom.h>
72 #include <geom/geom_vfs.h>
76 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
78 static int ffs_reload(struct mount *, struct thread *);
79 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
80 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
82 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
83 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
84 static vfs_init_t ffs_init;
85 static vfs_uninit_t ffs_uninit;
86 static vfs_extattrctl_t ffs_extattrctl;
87 static vfs_cmount_t ffs_cmount;
88 static vfs_unmount_t ffs_unmount;
89 static vfs_mount_t ffs_mount;
90 static vfs_statfs_t ffs_statfs;
91 static vfs_fhtovp_t ffs_fhtovp;
92 static vfs_sync_t ffs_sync;
94 static struct vfsops ufs_vfsops = {
95 .vfs_extattrctl = ffs_extattrctl,
96 .vfs_fhtovp = ffs_fhtovp,
98 .vfs_mount = ffs_mount,
99 .vfs_cmount = ffs_cmount,
100 .vfs_quotactl = ufs_quotactl,
101 .vfs_root = ufs_root,
102 .vfs_statfs = ffs_statfs,
103 .vfs_sync = ffs_sync,
104 .vfs_uninit = ffs_uninit,
105 .vfs_unmount = ffs_unmount,
106 .vfs_vget = ffs_vget,
107 .vfs_susp_clean = process_deferred_inactive,
110 VFS_SET(ufs_vfsops, ufs, 0);
111 MODULE_VERSION(ufs, 1);
113 static b_strategy_t ffs_geom_strategy;
114 static b_write_t ffs_bufwrite;
116 static struct buf_ops ffs_ops = {
118 .bop_write = ffs_bufwrite,
119 .bop_strategy = ffs_geom_strategy,
121 #ifdef NO_FFS_SNAPSHOT
122 .bop_bdflush = bufbdflush,
124 .bop_bdflush = ffs_bdflush,
129 * Note that userquota and groupquota options are not currently used
130 * by UFS/FFS code and generally mount(8) does not pass those options
131 * from userland, but they can be passed by loader(8) via
132 * vfs.root.mountfrom.options.
134 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
135 "noclusterw", "noexec", "export", "force", "from", "groupquota",
136 "multilabel", "nfsv4acls", "snapshot", "nosuid", "suiddir", "nosymfollow",
137 "sync", "union", "userquota", NULL };
140 ffs_mount(struct mount *mp)
144 struct ufsmount *ump = 0;
147 u_int mntorflags, mntandnotflags;
149 struct nameidata ndp;
153 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
155 if (uma_inode == NULL) {
156 uma_inode = uma_zcreate("FFS inode",
157 sizeof(struct inode), NULL, NULL, NULL, NULL,
159 uma_ufs1 = uma_zcreate("FFS1 dinode",
160 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
162 uma_ufs2 = uma_zcreate("FFS2 dinode",
163 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
167 vfs_deleteopt(mp->mnt_optnew, "groupquota");
168 vfs_deleteopt(mp->mnt_optnew, "userquota");
170 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
176 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
177 mntorflags |= MNT_ACLS;
179 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
180 mntorflags |= MNT_SNAPSHOT;
182 * Once we have set the MNT_SNAPSHOT flag, do not
183 * persist "snapshot" in the options list.
185 vfs_deleteopt(mp->mnt_optnew, "snapshot");
186 vfs_deleteopt(mp->mnt_opt, "snapshot");
189 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
190 if (mntorflags & MNT_ACLS) {
191 printf("WARNING: \"acls\" and \"nfsv4acls\" "
192 "options are mutually exclusive\n");
195 mntorflags |= MNT_NFS4ACLS;
199 mp->mnt_flag = (mp->mnt_flag | mntorflags) & ~mntandnotflags;
202 * If updating, check whether changing from read-only to
203 * read/write; if there is no device name, that's all we do.
205 if (mp->mnt_flag & MNT_UPDATE) {
208 devvp = ump->um_devvp;
209 if (fs->fs_ronly == 0 &&
210 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
212 * Flush any dirty data and suspend filesystem.
214 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
217 vn_finished_write(mp);
218 if ((error = vfs_write_suspend(mp)) != 0)
221 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
223 * Allow the secondary writes
226 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
228 wakeup(&mp->mnt_flag);
231 * Allow the curthread to
232 * ignore the suspension to
233 * synchronize on-disk state.
235 td->td_pflags |= TDP_IGNSUSP;
239 vn_start_write(NULL, &mp, V_WAIT);
242 * Check for and optionally get rid of files open
246 if (mp->mnt_flag & MNT_FORCE)
248 if (mp->mnt_flag & MNT_SOFTDEP) {
249 error = softdep_flushfiles(mp, flags, td);
251 error = ffs_flushfiles(mp, flags, td);
254 vfs_write_resume(mp);
257 if (fs->fs_pendingblocks != 0 ||
258 fs->fs_pendinginodes != 0) {
259 printf("%s: %s: blocks %jd files %d\n",
260 fs->fs_fsmnt, "update error",
261 (intmax_t)fs->fs_pendingblocks,
262 fs->fs_pendinginodes);
263 fs->fs_pendingblocks = 0;
264 fs->fs_pendinginodes = 0;
266 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
268 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
271 vfs_write_resume(mp);
276 g_access(ump->um_cp, 0, -1, 0);
281 mp->mnt_flag |= MNT_RDONLY;
284 * Allow the writers to note that filesystem
287 vfs_write_resume(mp);
289 if ((mp->mnt_flag & MNT_RELOAD) &&
290 (error = ffs_reload(mp, td)) != 0)
293 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
295 * If upgrade to read-write by non-root, then verify
296 * that user has necessary permissions on the device.
298 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
299 error = VOP_ACCESS(devvp, VREAD | VWRITE,
302 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
304 VOP_UNLOCK(devvp, 0);
307 VOP_UNLOCK(devvp, 0);
308 fs->fs_flags &= ~FS_UNCLEAN;
309 if (fs->fs_clean == 0) {
310 fs->fs_flags |= FS_UNCLEAN;
311 if ((mp->mnt_flag & MNT_FORCE) ||
312 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
313 (fs->fs_flags & FS_DOSOFTDEP))) {
314 printf("WARNING: %s was not %s\n",
315 fs->fs_fsmnt, "properly dismounted");
318 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
326 * If we're the root device, we may not have an E count
329 if (ump->um_cp->ace == 0)
330 error = g_access(ump->um_cp, 0, 1, 1);
332 error = g_access(ump->um_cp, 0, 1, 0);
337 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
341 mp->mnt_flag &= ~MNT_RDONLY;
344 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
345 vn_finished_write(mp);
348 /* check to see if we need to start softdep */
349 if ((fs->fs_flags & FS_DOSOFTDEP) &&
350 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
351 vn_finished_write(mp);
354 if (fs->fs_snapinum[0] != 0)
355 ffs_snapshot_mount(mp);
356 vn_finished_write(mp);
359 * Soft updates is incompatible with "async",
360 * so if we are doing softupdates stop the user
361 * from setting the async flag in an update.
362 * Softdep_mount() clears it in an initial mount
365 if (mp->mnt_flag & MNT_SOFTDEP) {
366 /* XXX: Reset too late ? */
368 mp->mnt_flag &= ~MNT_ASYNC;
372 * Keep MNT_ACLS flag if it is stored in superblock.
374 if ((fs->fs_flags & FS_ACLS) != 0) {
375 /* XXX: Set too late ? */
377 mp->mnt_flag |= MNT_ACLS;
381 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
382 /* XXX: Set too late ? */
384 mp->mnt_flag |= MNT_NFS4ACLS;
389 * If this is a snapshot request, take the snapshot.
391 if (mp->mnt_flag & MNT_SNAPSHOT)
392 return (ffs_snapshot(mp, fspec));
396 * Not an update, or updating the name: look up the name
397 * and verify that it refers to a sensible disk device.
399 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
400 if ((error = namei(&ndp)) != 0)
402 NDFREE(&ndp, NDF_ONLY_PNBUF);
404 if (!vn_isdisk(devvp, &error)) {
410 * If mount by non-root, then verify that user has necessary
411 * permissions on the device.
414 if ((mp->mnt_flag & MNT_RDONLY) == 0)
416 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
418 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
424 if (mp->mnt_flag & MNT_UPDATE) {
428 * If it's not the same vnode, or at least the same device
429 * then it's not correct.
432 if (devvp->v_rdev != ump->um_devvp->v_rdev)
433 error = EINVAL; /* needs translation */
441 * We need the name for the mount point (also used for
442 * "last mounted on") copied in. If an error occurs,
443 * the mount point is discarded by the upper level code.
444 * Note that vfs_mount() populates f_mntonname for us.
446 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
451 vfs_mountedfrom(mp, fspec);
456 * Compatibility with old mount system call.
460 ffs_cmount(struct mntarg *ma, void *data, int flags)
462 struct ufs_args args;
463 struct export_args exp;
468 error = copyin(data, &args, sizeof args);
471 vfs_oexport_conv(&args.export, &exp);
473 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
474 ma = mount_arg(ma, "export", &exp, sizeof(exp));
475 error = kernel_mount(ma, flags);
481 * Reload all incore data for a filesystem (used after running fsck on
482 * the root filesystem and finding things to fix). The filesystem must
483 * be mounted read-only.
485 * Things to do to update the mount:
486 * 1) invalidate all cached meta-data.
487 * 2) re-read superblock from disk.
488 * 3) re-read summary information from disk.
489 * 4) invalidate all inactive vnodes.
490 * 5) invalidate all cached file data.
491 * 6) re-read inode data for all active vnodes.
494 ffs_reload(struct mount *mp, struct thread *td)
496 struct vnode *vp, *mvp, *devvp;
500 struct fs *fs, *newfs;
501 struct ufsmount *ump;
502 ufs2_daddr_t sblockloc;
503 int i, blks, size, error;
506 if ((mp->mnt_flag & MNT_RDONLY) == 0)
510 * Step 1: invalidate all cached meta-data.
512 devvp = VFSTOUFS(mp)->um_devvp;
513 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
514 if (vinvalbuf(devvp, 0, 0, 0) != 0)
515 panic("ffs_reload: dirty1");
516 VOP_UNLOCK(devvp, 0);
519 * Step 2: re-read superblock from disk.
521 fs = VFSTOUFS(mp)->um_fs;
522 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
525 newfs = (struct fs *)bp->b_data;
526 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
527 newfs->fs_magic != FS_UFS2_MAGIC) ||
528 newfs->fs_bsize > MAXBSIZE ||
529 newfs->fs_bsize < sizeof(struct fs)) {
531 return (EIO); /* XXX needs translation */
534 * Copy pointer fields back into superblock before copying in XXX
535 * new superblock. These should really be in the ufsmount. XXX
536 * Note that important parameters (eg fs_ncg) are unchanged.
538 newfs->fs_csp = fs->fs_csp;
539 newfs->fs_maxcluster = fs->fs_maxcluster;
540 newfs->fs_contigdirs = fs->fs_contigdirs;
541 newfs->fs_active = fs->fs_active;
542 /* The file system is still read-only. */
544 sblockloc = fs->fs_sblockloc;
545 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
547 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
548 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
550 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
551 printf("%s: reload pending error: blocks %jd files %d\n",
552 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
553 fs->fs_pendinginodes);
554 fs->fs_pendingblocks = 0;
555 fs->fs_pendinginodes = 0;
560 * Step 3: re-read summary information from disk.
562 blks = howmany(fs->fs_cssize, fs->fs_fsize);
564 for (i = 0; i < blks; i += fs->fs_frag) {
566 if (i + fs->fs_frag > blks)
567 size = (blks - i) * fs->fs_fsize;
568 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
572 bcopy(bp->b_data, space, (u_int)size);
573 space = (char *)space + size;
577 * We no longer know anything about clusters per cylinder group.
579 if (fs->fs_contigsumsize > 0) {
580 lp = fs->fs_maxcluster;
581 for (i = 0; i < fs->fs_ncg; i++)
582 *lp++ = fs->fs_contigsumsize;
587 MNT_VNODE_FOREACH(vp, mp, mvp) {
589 if (vp->v_iflag & VI_DOOMED) {
595 * Step 4: invalidate all cached file data.
597 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
598 MNT_VNODE_FOREACH_ABORT(mp, mvp);
601 if (vinvalbuf(vp, 0, 0, 0))
602 panic("ffs_reload: dirty2");
604 * Step 5: re-read inode data for all active vnodes.
608 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
609 (int)fs->fs_bsize, NOCRED, &bp);
613 MNT_VNODE_FOREACH_ABORT(mp, mvp);
616 ffs_load_inode(bp, ip, fs, ip->i_number);
617 ip->i_effnlink = ip->i_nlink;
628 * Possible superblock locations ordered from most to least likely.
630 static int sblock_try[] = SBLOCKSEARCH;
633 * Common code for mount and mountroot
636 ffs_mountfs(devvp, mp, td)
641 struct ufsmount *ump;
646 ufs2_daddr_t sblockloc;
647 int error, i, blks, size, ronly;
650 struct g_consumer *cp;
655 cred = td ? td->td_ucred : NOCRED;
656 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
662 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
665 * If we are a root mount, drop the E flag so fsck can do its magic.
666 * We will pick it up again when we remount R/W.
668 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
669 error = g_access(cp, 0, 0, -1);
672 VOP_UNLOCK(devvp, 0);
675 if (devvp->v_rdev->si_iosize_max != 0)
676 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
677 if (mp->mnt_iosize_max > MAXPHYS)
678 mp->mnt_iosize_max = MAXPHYS;
680 devvp->v_bufobj.bo_ops = &ffs_ops;
685 * Try reading the superblock in each of its possible locations.
687 for (i = 0; sblock_try[i] != -1; i++) {
688 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
691 "Invalid sectorsize %d for superblock size %d",
692 cp->provider->sectorsize, SBLOCKSIZE);
695 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
698 fs = (struct fs *)bp->b_data;
699 sblockloc = sblock_try[i];
700 if ((fs->fs_magic == FS_UFS1_MAGIC ||
701 (fs->fs_magic == FS_UFS2_MAGIC &&
702 (fs->fs_sblockloc == sblockloc ||
703 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
704 fs->fs_bsize <= MAXBSIZE &&
705 fs->fs_bsize >= sizeof(struct fs))
710 if (sblock_try[i] == -1) {
711 error = EINVAL; /* XXX needs translation */
715 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
716 fs->fs_flags &= ~FS_UNCLEAN;
717 if (fs->fs_clean == 0) {
718 fs->fs_flags |= FS_UNCLEAN;
719 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
720 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
721 (fs->fs_flags & FS_DOSOFTDEP))) {
723 "WARNING: %s was not properly dismounted\n",
727 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
732 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
733 (mp->mnt_flag & MNT_FORCE)) {
734 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
735 (intmax_t)fs->fs_pendingblocks,
736 fs->fs_pendinginodes);
737 fs->fs_pendingblocks = 0;
738 fs->fs_pendinginodes = 0;
741 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
742 printf("%s: mount pending error: blocks %jd files %d\n",
743 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
744 fs->fs_pendinginodes);
745 fs->fs_pendingblocks = 0;
746 fs->fs_pendinginodes = 0;
748 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
751 * Get journal provider name.
754 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
755 if (g_io_getattr("GJOURNAL::provider", cp, &size,
756 mp->mnt_gjprovider) == 0) {
757 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
760 mp->mnt_flag |= MNT_GJOURNAL;
764 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
765 mp->mnt_stat.f_mntonname);
766 free(mp->mnt_gjprovider, M_UFSMNT);
767 mp->mnt_gjprovider = NULL;
771 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
772 mp->mnt_stat.f_mntonname);
775 mp->mnt_gjprovider = NULL;
777 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
779 ump->um_bo = &devvp->v_bufobj;
780 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
781 if (fs->fs_magic == FS_UFS1_MAGIC) {
782 ump->um_fstype = UFS1;
783 ump->um_balloc = ffs_balloc_ufs1;
785 ump->um_fstype = UFS2;
786 ump->um_balloc = ffs_balloc_ufs2;
788 ump->um_blkatoff = ffs_blkatoff;
789 ump->um_truncate = ffs_truncate;
790 ump->um_update = ffs_update;
791 ump->um_valloc = ffs_valloc;
792 ump->um_vfree = ffs_vfree;
793 ump->um_ifree = ffs_ifree;
794 ump->um_rdonly = ffs_rdonly;
795 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
796 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
797 if (fs->fs_sbsize < SBLOCKSIZE)
798 bp->b_flags |= B_INVAL | B_NOCACHE;
802 ffs_oldfscompat_read(fs, ump, sblockloc);
803 fs->fs_ronly = ronly;
804 size = fs->fs_cssize;
805 blks = howmany(size, fs->fs_fsize);
806 if (fs->fs_contigsumsize > 0)
807 size += fs->fs_ncg * sizeof(int32_t);
808 size += fs->fs_ncg * sizeof(u_int8_t);
809 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
811 for (i = 0; i < blks; i += fs->fs_frag) {
813 if (i + fs->fs_frag > blks)
814 size = (blks - i) * fs->fs_fsize;
815 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
817 free(fs->fs_csp, M_UFSMNT);
820 bcopy(bp->b_data, space, (u_int)size);
821 space = (char *)space + size;
825 if (fs->fs_contigsumsize > 0) {
826 fs->fs_maxcluster = lp = space;
827 for (i = 0; i < fs->fs_ncg; i++)
828 *lp++ = fs->fs_contigsumsize;
831 size = fs->fs_ncg * sizeof(u_int8_t);
832 fs->fs_contigdirs = (u_int8_t *)space;
833 bzero(fs->fs_contigdirs, size);
834 fs->fs_active = NULL;
836 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
837 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
839 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
840 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
845 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
847 mp->mnt_flag |= MNT_LOCAL;
849 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
852 mp->mnt_flag |= MNT_MULTILABEL;
856 "WARNING: %s: multilabel flag on fs but no MAC support\n",
857 mp->mnt_stat.f_mntonname);
860 if ((fs->fs_flags & FS_ACLS) != 0) {
864 if (mp->mnt_flag & MNT_NFS4ACLS)
865 printf("WARNING: ACLs flag on fs conflicts with "
866 "\"nfsv4acls\" mount option; option ignored\n");
867 mp->mnt_flag &= ~MNT_NFS4ACLS;
868 mp->mnt_flag |= MNT_ACLS;
873 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
874 mp->mnt_stat.f_mntonname);
877 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
881 if (mp->mnt_flag & MNT_ACLS)
882 printf("WARNING: NFSv4 ACLs flag on fs conflicts with "
883 "\"acls\" mount option; option ignored\n");
884 mp->mnt_flag &= ~MNT_ACLS;
885 mp->mnt_flag |= MNT_NFS4ACLS;
890 "WARNING: %s: NFSv4 ACLs flag on fs but no ACLs support\n",
891 mp->mnt_stat.f_mntonname);
897 ump->um_devvp = devvp;
898 ump->um_nindir = fs->fs_nindir;
899 ump->um_bptrtodb = fs->fs_fsbtodb;
900 ump->um_seqinc = fs->fs_frag;
901 for (i = 0; i < MAXQUOTAS; i++)
902 ump->um_quotas[i] = NULLVP;
904 ufs_extattr_uepm_init(&ump->um_extattr);
907 * Set FS local "last mounted on" information (NULL pad)
909 bzero(fs->fs_fsmnt, MAXMNTLEN);
910 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
912 if( mp->mnt_flag & MNT_ROOTFS) {
914 * Root mount; update timestamp in mount structure.
915 * this will be used by the common root mount code
916 * to update the system clock.
918 mp->mnt_time = fs->fs_time;
922 if ((fs->fs_flags & FS_DOSOFTDEP) &&
923 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
924 free(fs->fs_csp, M_UFSMNT);
927 if (fs->fs_snapinum[0] != 0)
928 ffs_snapshot_mount(mp);
931 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
934 * Initialize filesystem stat information in mount struct.
937 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
938 MNTK_EXTENDED_SHARED;
941 #ifdef UFS_EXTATTR_AUTOSTART
944 * Auto-starting does the following:
945 * - check for /.attribute in the fs, and extattr_start if so
946 * - for each file in .attribute, enable that file with
947 * an attribute of the same name.
948 * Not clear how to report errors -- probably eat them.
949 * This would all happen while the filesystem was busy/not
950 * available, so would effectively be "atomic".
952 mp->mnt_stat.f_iosize = fs->fs_bsize;
953 (void) ufs_extattr_autostart(mp, td);
954 #endif /* !UFS_EXTATTR_AUTOSTART */
955 #endif /* !UFS_EXTATTR */
968 mtx_destroy(UFS_MTX(ump));
969 if (mp->mnt_gjprovider != NULL) {
970 free(mp->mnt_gjprovider, M_UFSMNT);
971 mp->mnt_gjprovider = NULL;
973 free(ump->um_fs, M_UFSMNT);
981 #include <sys/sysctl.h>
982 static int bigcgs = 0;
983 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
986 * Sanity checks for loading old filesystem superblocks.
987 * See ffs_oldfscompat_write below for unwound actions.
989 * XXX - Parts get retired eventually.
990 * Unfortunately new bits get added.
993 ffs_oldfscompat_read(fs, ump, sblockloc)
995 struct ufsmount *ump;
996 ufs2_daddr_t sblockloc;
1001 * If not yet done, update fs_flags location and value of fs_sblockloc.
1003 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1004 fs->fs_flags = fs->fs_old_flags;
1005 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1006 fs->fs_sblockloc = sblockloc;
1009 * If not yet done, update UFS1 superblock with new wider fields.
1011 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1012 fs->fs_maxbsize = fs->fs_bsize;
1013 fs->fs_time = fs->fs_old_time;
1014 fs->fs_size = fs->fs_old_size;
1015 fs->fs_dsize = fs->fs_old_dsize;
1016 fs->fs_csaddr = fs->fs_old_csaddr;
1017 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1018 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1019 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1020 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1022 if (fs->fs_magic == FS_UFS1_MAGIC &&
1023 fs->fs_old_inodefmt < FS_44INODEFMT) {
1024 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1025 fs->fs_qbmask = ~fs->fs_bmask;
1026 fs->fs_qfmask = ~fs->fs_fmask;
1028 if (fs->fs_magic == FS_UFS1_MAGIC) {
1029 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1030 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1031 if (fs->fs_maxfilesize > maxfilesize)
1032 fs->fs_maxfilesize = maxfilesize;
1034 /* Compatibility for old filesystems */
1035 if (fs->fs_avgfilesize <= 0)
1036 fs->fs_avgfilesize = AVFILESIZ;
1037 if (fs->fs_avgfpdir <= 0)
1038 fs->fs_avgfpdir = AFPDIR;
1040 fs->fs_save_cgsize = fs->fs_cgsize;
1041 fs->fs_cgsize = fs->fs_bsize;
1046 * Unwinding superblock updates for old filesystems.
1047 * See ffs_oldfscompat_read above for details.
1049 * XXX - Parts get retired eventually.
1050 * Unfortunately new bits get added.
1053 ffs_oldfscompat_write(fs, ump)
1055 struct ufsmount *ump;
1059 * Copy back UFS2 updated fields that UFS1 inspects.
1061 if (fs->fs_magic == FS_UFS1_MAGIC) {
1062 fs->fs_old_time = fs->fs_time;
1063 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1064 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1065 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1066 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1067 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1070 fs->fs_cgsize = fs->fs_save_cgsize;
1071 fs->fs_save_cgsize = 0;
1076 * unmount system call
1079 ffs_unmount(mp, mntflags)
1084 struct ufsmount *ump = VFSTOUFS(mp);
1086 int error, flags, susp;
1094 if (mntflags & MNT_FORCE) {
1095 flags |= FORCECLOSE;
1096 susp = fs->fs_ronly != 0;
1100 if ((error = ufs_extattr_stop(mp, td))) {
1101 if (error != EOPNOTSUPP)
1102 printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1106 ufs_extattr_uepm_destroy(&ump->um_extattr);
1112 * dounmount already called vn_start_write().
1115 vn_finished_write(mp);
1116 if ((error = vfs_write_suspend(mp)) != 0)
1119 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1120 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1122 wakeup(&mp->mnt_flag);
1124 td->td_pflags |= TDP_IGNSUSP;
1128 vn_start_write(NULL, &mp, V_WAIT);
1131 if (mp->mnt_flag & MNT_SOFTDEP)
1132 error = softdep_flushfiles(mp, flags, td);
1134 error = ffs_flushfiles(mp, flags, td);
1135 if (error != 0 && error != ENXIO)
1139 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1140 printf("%s: unmount pending error: blocks %jd files %d\n",
1141 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1142 fs->fs_pendinginodes);
1143 fs->fs_pendingblocks = 0;
1144 fs->fs_pendinginodes = 0;
1147 if (fs->fs_ronly == 0) {
1148 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1149 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1150 if (error && error != ENXIO) {
1156 vfs_write_resume(mp);
1157 vn_start_write(NULL, &mp, V_WAIT);
1161 g_vfs_close(ump->um_cp);
1162 g_topology_unlock();
1164 vrele(ump->um_devvp);
1165 dev_rel(ump->um_dev);
1166 mtx_destroy(UFS_MTX(ump));
1167 if (mp->mnt_gjprovider != NULL) {
1168 free(mp->mnt_gjprovider, M_UFSMNT);
1169 mp->mnt_gjprovider = NULL;
1171 free(fs->fs_csp, M_UFSMNT);
1173 free(ump, M_UFSMNT);
1174 mp->mnt_data = NULL;
1176 mp->mnt_flag &= ~MNT_LOCAL;
1182 vfs_write_resume(mp);
1183 vn_start_write(NULL, &mp, V_WAIT);
1187 ufs_extattr_uepm_init(&ump->um_extattr);
1188 #ifdef UFS_EXTATTR_AUTOSTART
1189 (void) ufs_extattr_autostart(mp, td);
1198 * Flush out all the files in a filesystem.
1201 ffs_flushfiles(mp, flags, td)
1206 struct ufsmount *ump;
1211 if (mp->mnt_flag & MNT_QUOTA) {
1213 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1216 for (i = 0; i < MAXQUOTAS; i++) {
1217 quotaoff(td, mp, i);
1220 * Here we fall through to vflush again to ensure
1221 * that we have gotten rid of all the system vnodes.
1225 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1226 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1227 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1229 ffs_snapshot_unmount(mp);
1230 flags |= FORCECLOSE;
1232 * Here we fall through to vflush again to ensure
1233 * that we have gotten rid of all the system vnodes.
1237 * Flush all the files.
1239 if ((error = vflush(mp, 0, flags, td)) != 0)
1242 * Flush filesystem metadata.
1244 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1245 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1246 VOP_UNLOCK(ump->um_devvp, 0);
1251 * Get filesystem statistics.
1258 struct ufsmount *ump;
1263 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1264 panic("ffs_statfs");
1265 sbp->f_version = STATFS_VERSION;
1266 sbp->f_bsize = fs->fs_fsize;
1267 sbp->f_iosize = fs->fs_bsize;
1268 sbp->f_blocks = fs->fs_dsize;
1270 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1271 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1272 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1273 dbtofsb(fs, fs->fs_pendingblocks);
1274 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1275 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1277 sbp->f_namemax = NAME_MAX;
1282 * Go through the disk queues to initiate sandbagged IO;
1283 * go through the inodes to write those that have been modified;
1284 * initiate the writing of the super block if it has been modified.
1286 * Note: we are always called with the filesystem marked `MPBUSY'.
1289 ffs_sync(mp, waitfor)
1293 struct vnode *mvp, *vp, *devvp;
1296 struct ufsmount *ump = VFSTOUFS(mp);
1298 int error, count, wait, lockreq, allerror = 0;
1301 int secondary_writes;
1302 int secondary_accwrites;
1304 int softdep_accdeps;
1309 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1310 printf("fs = %s\n", fs->fs_fsmnt);
1311 panic("ffs_sync: rofs mod");
1314 * Write back each (modified) inode.
1319 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1320 if (waitfor == MNT_SUSPEND) {
1324 if (waitfor == MNT_WAIT) {
1326 lockreq = LK_EXCLUSIVE;
1328 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1331 /* Grab snapshot of secondary write counts */
1332 secondary_writes = mp->mnt_secondary_writes;
1333 secondary_accwrites = mp->mnt_secondary_accwrites;
1335 /* Grab snapshot of softdep dependency counts */
1337 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1340 MNT_VNODE_FOREACH(vp, mp, mvp) {
1342 * Depend on the mntvnode_slock to keep things stable enough
1343 * for a quick test. Since there might be hundreds of
1344 * thousands of vnodes, we cannot afford even a subroutine
1345 * call unless there's a good chance that we have work to do.
1348 if (vp->v_iflag & VI_DOOMED) {
1353 if (vp->v_type == VNON || ((ip->i_flag &
1354 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1355 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1360 if ((error = vget(vp, lockreq, td)) != 0) {
1362 if (error == ENOENT || error == ENOLCK) {
1363 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1368 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1375 * Force stale filesystem control information to be flushed.
1377 if (waitfor == MNT_WAIT) {
1378 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1380 /* Flushed work items may create new vnodes to clean */
1381 if (allerror == 0 && count) {
1389 devvp = ump->um_devvp;
1390 bo = &devvp->v_bufobj;
1392 if (waitfor != MNT_LAZY &&
1393 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1395 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1396 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1398 VOP_UNLOCK(devvp, 0);
1399 if (allerror == 0 && waitfor == MNT_WAIT) {
1403 } else if (suspend != 0) {
1404 if (softdep_check_suspend(mp,
1409 secondary_accwrites) != 0)
1410 goto loop; /* More work needed */
1411 mtx_assert(MNT_MTX(mp), MA_OWNED);
1412 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1418 * Write back modified superblock.
1420 if (fs->fs_fmod != 0 &&
1421 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1427 ffs_vget(mp, ino, flags, vpp)
1433 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1437 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1446 struct ufsmount *ump;
1452 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1453 if (error || *vpp != NULL)
1457 * We must promote to an exclusive lock for vnode creation. This
1458 * can happen if lookup is passed LOCKSHARED.
1460 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1461 flags &= ~LK_TYPE_MASK;
1462 flags |= LK_EXCLUSIVE;
1466 * We do not lock vnode creation as it is believed to be too
1467 * expensive for such rare case as simultaneous creation of vnode
1468 * for same ino by different processes. We just allow them to race
1469 * and check later to decide who wins. Let the race begin!
1477 * If this malloc() is performed after the getnewvnode()
1478 * it might block, leaving a vnode with a NULL v_data to be
1479 * found by ffs_sync() if a sync happens to fire right then,
1480 * which will cause a panic because ffs_sync() blindly
1481 * dereferences vp->v_data (as well it should).
1483 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1485 /* Allocate a new vnode/inode. */
1486 if (fs->fs_magic == FS_UFS1_MAGIC)
1487 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1489 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1492 uma_zfree(uma_inode, ip);
1496 * FFS supports recursive locking.
1500 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1510 for (i = 0; i < MAXQUOTAS; i++)
1511 ip->i_dquot[i] = NODQUOT;
1515 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1516 if (ffs_flags & FFSV_FORCEINSMQ)
1517 vp->v_vflag |= VV_FORCEINSMQ;
1518 error = insmntque(vp, mp);
1520 uma_zfree(uma_inode, ip);
1524 vp->v_vflag &= ~VV_FORCEINSMQ;
1525 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1526 if (error || *vpp != NULL)
1529 /* Read in the disk contents for the inode, copy into the inode. */
1530 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1531 (int)fs->fs_bsize, NOCRED, &bp);
1534 * The inode does not contain anything useful, so it would
1535 * be misleading to leave it on its hash chain. With mode
1536 * still zero, it will be unlinked and returned to the free
1544 if (ip->i_ump->um_fstype == UFS1)
1545 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1547 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1548 ffs_load_inode(bp, ip, fs, ino);
1549 if (DOINGSOFTDEP(vp))
1550 softdep_load_inodeblock(ip);
1552 ip->i_effnlink = ip->i_nlink;
1556 * Initialize the vnode from the inode, check for aliases.
1557 * Note that the underlying vnode may have changed.
1559 if (ip->i_ump->um_fstype == UFS1)
1560 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1562 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1570 * Finish inode initialization.
1572 if (vp->v_type != VFIFO) {
1573 /* FFS supports shared locking for all files except fifos. */
1578 * Set up a generation number for this inode if it does not
1579 * already have one. This should only happen on old filesystems.
1581 if (ip->i_gen == 0) {
1582 ip->i_gen = arc4random() / 2 + 1;
1583 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1584 ip->i_flag |= IN_MODIFIED;
1585 DIP_SET(ip, i_gen, ip->i_gen);
1589 * Ensure that uid and gid are correct. This is a temporary
1590 * fix until fsck has been changed to do the update.
1592 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
1593 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1594 ip->i_uid = ip->i_din1->di_ouid; /* XXX */
1595 ip->i_gid = ip->i_din1->di_ogid; /* XXX */
1599 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1601 * If this vnode is already allocated, and we're running
1602 * multi-label, attempt to perform a label association
1603 * from the extended attributes on the inode.
1605 error = mac_vnode_associate_extattr(mp, vp);
1607 /* ufs_inactive will release ip->i_devvp ref. */
1620 * File handle to vnode
1622 * Have to be really careful about stale file handles:
1623 * - check that the inode number is valid
1624 * - call ffs_vget() to get the locked inode
1625 * - check for an unallocated inode (i_mode == 0)
1626 * - check that the given client host has export rights and return
1627 * those rights via. exflagsp and credanonp
1630 ffs_fhtovp(mp, fhp, vpp)
1638 ufhp = (struct ufid *)fhp;
1639 fs = VFSTOUFS(mp)->um_fs;
1640 if (ufhp->ufid_ino < ROOTINO ||
1641 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1643 return (ufs_fhtovp(mp, ufhp, vpp));
1647 * Initialize the filesystem.
1651 struct vfsconf *vfsp;
1654 softdep_initialize();
1655 return (ufs_init(vfsp));
1659 * Undo the work of ffs_init().
1663 struct vfsconf *vfsp;
1667 ret = ufs_uninit(vfsp);
1668 softdep_uninitialize();
1673 * Write a superblock and associated information back to disk.
1676 ffs_sbupdate(mp, waitfor, suspended)
1677 struct ufsmount *mp;
1681 struct fs *fs = mp->um_fs;
1686 int i, size, error, allerror = 0;
1688 if (fs->fs_ronly == 1 &&
1689 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1690 (MNT_RDONLY | MNT_UPDATE))
1691 panic("ffs_sbupdate: write read-only filesystem");
1693 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1695 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1698 * First write back the summary information.
1700 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1702 for (i = 0; i < blks; i += fs->fs_frag) {
1703 size = fs->fs_bsize;
1704 if (i + fs->fs_frag > blks)
1705 size = (blks - i) * fs->fs_fsize;
1706 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1708 bcopy(space, bp->b_data, (u_int)size);
1709 space = (char *)space + size;
1711 bp->b_flags |= B_VALIDSUSPWRT;
1712 if (waitfor != MNT_WAIT)
1714 else if ((error = bwrite(bp)) != 0)
1718 * Now write back the superblock itself. If any errors occurred
1719 * up to this point, then fail so that the superblock avoids
1720 * being written out as clean.
1727 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1728 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1729 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1730 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1731 fs->fs_sblockloc = SBLOCK_UFS1;
1733 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1734 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1735 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1736 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1737 fs->fs_sblockloc = SBLOCK_UFS2;
1740 fs->fs_time = time_second;
1741 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1742 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1744 bp->b_flags |= B_VALIDSUSPWRT;
1745 if (waitfor != MNT_WAIT)
1747 else if ((error = bwrite(bp)) != 0)
1753 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1754 int attrnamespace, const char *attrname)
1758 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1761 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1767 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1770 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1771 uma_zfree(uma_ufs1, ip->i_din1);
1772 else if (ip->i_din2 != NULL)
1773 uma_zfree(uma_ufs2, ip->i_din2);
1774 uma_zfree(uma_inode, ip);
1777 static int dobkgrdwrite = 1;
1778 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1779 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1782 * Complete a background write started from bwrite.
1785 ffs_backgroundwritedone(struct buf *bp)
1787 struct bufobj *bufobj;
1791 * Find the original buffer that we are writing.
1793 bufobj = bp->b_bufobj;
1795 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1796 panic("backgroundwritedone: lost buffer");
1797 /* Grab an extra reference to be dropped by the bufdone() below. */
1798 bufobj_wrefl(bufobj);
1801 * Process dependencies then return any unfinished ones.
1803 if (!LIST_EMPTY(&bp->b_dep))
1806 if (!LIST_EMPTY(&bp->b_dep))
1807 softdep_move_dependencies(bp, origbp);
1810 * This buffer is marked B_NOCACHE so when it is released
1811 * by biodone it will be tossed.
1813 bp->b_flags |= B_NOCACHE;
1814 bp->b_flags &= ~B_CACHE;
1818 * Clear the BV_BKGRDINPROG flag in the original buffer
1819 * and awaken it if it is waiting for the write to complete.
1820 * If BV_BKGRDINPROG is not set in the original buffer it must
1821 * have been released and re-instantiated - which is not legal.
1823 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1824 ("backgroundwritedone: lost buffer2"));
1825 origbp->b_vflags &= ~BV_BKGRDINPROG;
1826 if (origbp->b_vflags & BV_BKGRDWAIT) {
1827 origbp->b_vflags &= ~BV_BKGRDWAIT;
1828 wakeup(&origbp->b_xflags);
1835 * Write, release buffer on completion. (Done by iodone
1836 * if async). Do not bother writing anything if the buffer
1839 * Note that we set B_CACHE here, indicating that buffer is
1840 * fully valid and thus cacheable. This is true even of NFS
1841 * now so we set it generally. This could be set either here
1842 * or in biodone() since the I/O is synchronous. We put it
1846 ffs_bufwrite(struct buf *bp)
1851 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1852 if (bp->b_flags & B_INVAL) {
1857 oldflags = bp->b_flags;
1859 if (!BUF_ISLOCKED(bp))
1860 panic("bufwrite: buffer is not busy???");
1863 * If a background write is already in progress, delay
1864 * writing this block if it is asynchronous. Otherwise
1865 * wait for the background write to complete.
1867 BO_LOCK(bp->b_bufobj);
1868 if (bp->b_vflags & BV_BKGRDINPROG) {
1869 if (bp->b_flags & B_ASYNC) {
1870 BO_UNLOCK(bp->b_bufobj);
1875 bp->b_vflags |= BV_BKGRDWAIT;
1876 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1877 if (bp->b_vflags & BV_BKGRDINPROG)
1878 panic("bufwrite: still writing");
1880 BO_UNLOCK(bp->b_bufobj);
1882 /* Mark the buffer clean */
1886 * If this buffer is marked for background writing and we
1887 * do not have to wait for it, make a copy and write the
1888 * copy so as to leave this buffer ready for further use.
1890 * This optimization eats a lot of memory. If we have a page
1891 * or buffer shortfall we can't do it.
1893 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1894 (bp->b_flags & B_ASYNC) &&
1895 !vm_page_count_severe() &&
1896 !buf_dirty_count_severe()) {
1897 KASSERT(bp->b_iodone == NULL,
1898 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1900 /* get a new block */
1901 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
1906 * set it to be identical to the old block. We have to
1907 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1908 * to avoid confusing the splay tree and gbincore().
1910 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1911 newbp->b_lblkno = bp->b_lblkno;
1912 newbp->b_xflags |= BX_BKGRDMARKER;
1913 BO_LOCK(bp->b_bufobj);
1914 bp->b_vflags |= BV_BKGRDINPROG;
1915 bgetvp(bp->b_vp, newbp);
1916 BO_UNLOCK(bp->b_bufobj);
1917 newbp->b_bufobj = &bp->b_vp->v_bufobj;
1918 newbp->b_blkno = bp->b_blkno;
1919 newbp->b_offset = bp->b_offset;
1920 newbp->b_iodone = ffs_backgroundwritedone;
1921 newbp->b_flags |= B_ASYNC;
1922 newbp->b_flags &= ~B_INVAL;
1925 /* move over the dependencies */
1926 if (!LIST_EMPTY(&bp->b_dep))
1927 softdep_move_dependencies(bp, newbp);
1931 * Initiate write on the copy, release the original to
1932 * the B_LOCKED queue so that it cannot go away until
1933 * the background write completes. If not locked it could go
1934 * away and then be reconstituted while it was being written.
1935 * If the reconstituted buffer were written, we could end up
1936 * with two background copies being written at the same time.
1942 /* Let the normal bufwrite do the rest for us */
1944 return (bufwrite(bp));
1949 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1955 vp = bo->__bo_vnode;
1956 if (bp->b_iocmd == BIO_WRITE) {
1957 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1958 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1959 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1960 panic("ffs_geom_strategy: bad I/O");
1961 bp->b_flags &= ~B_VALIDSUSPWRT;
1962 if ((vp->v_vflag & VV_COPYONWRITE) &&
1963 vp->v_rdev->si_snapdata != NULL) {
1964 if ((bp->b_flags & B_CLUSTER) != 0) {
1965 runningbufwakeup(bp);
1966 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1967 b_cluster.cluster_entry) {
1968 error = ffs_copyonwrite(vp, tbp);
1970 error != EOPNOTSUPP) {
1971 bp->b_error = error;
1972 bp->b_ioflags |= BIO_ERROR;
1977 bp->b_runningbufspace = bp->b_bufsize;
1978 atomic_add_long(&runningbufspace,
1979 bp->b_runningbufspace);
1981 error = ffs_copyonwrite(vp, bp);
1982 if (error != 0 && error != EOPNOTSUPP) {
1983 bp->b_error = error;
1984 bp->b_ioflags |= BIO_ERROR;
1991 if ((bp->b_flags & B_CLUSTER) != 0) {
1992 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1993 b_cluster.cluster_entry) {
1994 if (!LIST_EMPTY(&tbp->b_dep))
1998 if (!LIST_EMPTY(&bp->b_dep))
2004 g_vfs_strategy(bo, bp);
2010 db_print_ffs(struct ufsmount *ump)
2012 db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
2014 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2015 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2016 ump->softdep_on_worklist_inprogress, ump->softdep_deps,
2020 DB_SHOW_COMMAND(ffs, db_show_ffs)
2023 struct ufsmount *ump;
2026 ump = VFSTOUFS((struct mount *)addr);
2031 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2032 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2033 db_print_ffs(VFSTOUFS(mp));