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 ump->um_snapgone = ffs_snapgone;
796 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
797 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
798 if (fs->fs_sbsize < SBLOCKSIZE)
799 bp->b_flags |= B_INVAL | B_NOCACHE;
803 ffs_oldfscompat_read(fs, ump, sblockloc);
804 fs->fs_ronly = ronly;
805 size = fs->fs_cssize;
806 blks = howmany(size, fs->fs_fsize);
807 if (fs->fs_contigsumsize > 0)
808 size += fs->fs_ncg * sizeof(int32_t);
809 size += fs->fs_ncg * sizeof(u_int8_t);
810 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
812 for (i = 0; i < blks; i += fs->fs_frag) {
814 if (i + fs->fs_frag > blks)
815 size = (blks - i) * fs->fs_fsize;
816 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
818 free(fs->fs_csp, M_UFSMNT);
821 bcopy(bp->b_data, space, (u_int)size);
822 space = (char *)space + size;
826 if (fs->fs_contigsumsize > 0) {
827 fs->fs_maxcluster = lp = space;
828 for (i = 0; i < fs->fs_ncg; i++)
829 *lp++ = fs->fs_contigsumsize;
832 size = fs->fs_ncg * sizeof(u_int8_t);
833 fs->fs_contigdirs = (u_int8_t *)space;
834 bzero(fs->fs_contigdirs, size);
835 fs->fs_active = NULL;
837 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
838 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
840 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
841 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
846 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
848 mp->mnt_flag |= MNT_LOCAL;
850 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
853 mp->mnt_flag |= MNT_MULTILABEL;
857 "WARNING: %s: multilabel flag on fs but no MAC support\n",
858 mp->mnt_stat.f_mntonname);
861 if ((fs->fs_flags & FS_ACLS) != 0) {
865 if (mp->mnt_flag & MNT_NFS4ACLS)
866 printf("WARNING: ACLs flag on fs conflicts with "
867 "\"nfsv4acls\" mount option; option ignored\n");
868 mp->mnt_flag &= ~MNT_NFS4ACLS;
869 mp->mnt_flag |= MNT_ACLS;
874 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
875 mp->mnt_stat.f_mntonname);
878 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
882 if (mp->mnt_flag & MNT_ACLS)
883 printf("WARNING: NFSv4 ACLs flag on fs conflicts with "
884 "\"acls\" mount option; option ignored\n");
885 mp->mnt_flag &= ~MNT_ACLS;
886 mp->mnt_flag |= MNT_NFS4ACLS;
891 "WARNING: %s: NFSv4 ACLs flag on fs but no ACLs support\n",
892 mp->mnt_stat.f_mntonname);
895 if ((fs->fs_flags & FS_TRIM) != 0) {
897 if (g_io_getattr("GEOM::candelete", cp, &size,
898 &ump->um_candelete) == 0) {
899 if (!ump->um_candelete)
901 "WARNING: %s: TRIM flag on fs but disk does not support TRIM\n",
902 mp->mnt_stat.f_mntonname);
905 "WARNING: %s: TRIM flag on fs but cannot get whether disk supports TRIM\n",
906 mp->mnt_stat.f_mntonname);
907 ump->um_candelete = 0;
913 ump->um_devvp = devvp;
914 ump->um_nindir = fs->fs_nindir;
915 ump->um_bptrtodb = fs->fs_fsbtodb;
916 ump->um_seqinc = fs->fs_frag;
917 for (i = 0; i < MAXQUOTAS; i++)
918 ump->um_quotas[i] = NULLVP;
920 ufs_extattr_uepm_init(&ump->um_extattr);
923 * Set FS local "last mounted on" information (NULL pad)
925 bzero(fs->fs_fsmnt, MAXMNTLEN);
926 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
928 if( mp->mnt_flag & MNT_ROOTFS) {
930 * Root mount; update timestamp in mount structure.
931 * this will be used by the common root mount code
932 * to update the system clock.
934 mp->mnt_time = fs->fs_time;
938 if ((fs->fs_flags & FS_DOSOFTDEP) &&
939 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
940 free(fs->fs_csp, M_UFSMNT);
943 if (fs->fs_snapinum[0] != 0)
944 ffs_snapshot_mount(mp);
947 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
950 * Initialize filesystem stat information in mount struct.
953 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
954 MNTK_EXTENDED_SHARED;
957 #ifdef UFS_EXTATTR_AUTOSTART
960 * Auto-starting does the following:
961 * - check for /.attribute in the fs, and extattr_start if so
962 * - for each file in .attribute, enable that file with
963 * an attribute of the same name.
964 * Not clear how to report errors -- probably eat them.
965 * This would all happen while the filesystem was busy/not
966 * available, so would effectively be "atomic".
968 mp->mnt_stat.f_iosize = fs->fs_bsize;
969 (void) ufs_extattr_autostart(mp, td);
970 #endif /* !UFS_EXTATTR_AUTOSTART */
971 #endif /* !UFS_EXTATTR */
984 mtx_destroy(UFS_MTX(ump));
985 if (mp->mnt_gjprovider != NULL) {
986 free(mp->mnt_gjprovider, M_UFSMNT);
987 mp->mnt_gjprovider = NULL;
989 free(ump->um_fs, M_UFSMNT);
997 #include <sys/sysctl.h>
998 static int bigcgs = 0;
999 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1002 * Sanity checks for loading old filesystem superblocks.
1003 * See ffs_oldfscompat_write below for unwound actions.
1005 * XXX - Parts get retired eventually.
1006 * Unfortunately new bits get added.
1009 ffs_oldfscompat_read(fs, ump, sblockloc)
1011 struct ufsmount *ump;
1012 ufs2_daddr_t sblockloc;
1017 * If not yet done, update fs_flags location and value of fs_sblockloc.
1019 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1020 fs->fs_flags = fs->fs_old_flags;
1021 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1022 fs->fs_sblockloc = sblockloc;
1025 * If not yet done, update UFS1 superblock with new wider fields.
1027 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1028 fs->fs_maxbsize = fs->fs_bsize;
1029 fs->fs_time = fs->fs_old_time;
1030 fs->fs_size = fs->fs_old_size;
1031 fs->fs_dsize = fs->fs_old_dsize;
1032 fs->fs_csaddr = fs->fs_old_csaddr;
1033 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1034 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1035 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1036 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1038 if (fs->fs_magic == FS_UFS1_MAGIC &&
1039 fs->fs_old_inodefmt < FS_44INODEFMT) {
1040 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1041 fs->fs_qbmask = ~fs->fs_bmask;
1042 fs->fs_qfmask = ~fs->fs_fmask;
1044 if (fs->fs_magic == FS_UFS1_MAGIC) {
1045 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1046 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1047 if (fs->fs_maxfilesize > maxfilesize)
1048 fs->fs_maxfilesize = maxfilesize;
1050 /* Compatibility for old filesystems */
1051 if (fs->fs_avgfilesize <= 0)
1052 fs->fs_avgfilesize = AVFILESIZ;
1053 if (fs->fs_avgfpdir <= 0)
1054 fs->fs_avgfpdir = AFPDIR;
1056 fs->fs_save_cgsize = fs->fs_cgsize;
1057 fs->fs_cgsize = fs->fs_bsize;
1062 * Unwinding superblock updates for old filesystems.
1063 * See ffs_oldfscompat_read above for details.
1065 * XXX - Parts get retired eventually.
1066 * Unfortunately new bits get added.
1069 ffs_oldfscompat_write(fs, ump)
1071 struct ufsmount *ump;
1075 * Copy back UFS2 updated fields that UFS1 inspects.
1077 if (fs->fs_magic == FS_UFS1_MAGIC) {
1078 fs->fs_old_time = fs->fs_time;
1079 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1080 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1081 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1082 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1083 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1086 fs->fs_cgsize = fs->fs_save_cgsize;
1087 fs->fs_save_cgsize = 0;
1092 * unmount system call
1095 ffs_unmount(mp, mntflags)
1100 struct ufsmount *ump = VFSTOUFS(mp);
1102 int error, flags, susp;
1110 if (mntflags & MNT_FORCE) {
1111 flags |= FORCECLOSE;
1112 susp = fs->fs_ronly != 0;
1116 if ((error = ufs_extattr_stop(mp, td))) {
1117 if (error != EOPNOTSUPP)
1118 printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1122 ufs_extattr_uepm_destroy(&ump->um_extattr);
1128 * dounmount already called vn_start_write().
1131 vn_finished_write(mp);
1132 if ((error = vfs_write_suspend(mp)) != 0)
1135 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1136 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1138 wakeup(&mp->mnt_flag);
1140 td->td_pflags |= TDP_IGNSUSP;
1144 vn_start_write(NULL, &mp, V_WAIT);
1147 if (mp->mnt_flag & MNT_SOFTDEP)
1148 error = softdep_flushfiles(mp, flags, td);
1150 error = ffs_flushfiles(mp, flags, td);
1151 if (error != 0 && error != ENXIO)
1155 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1156 printf("%s: unmount pending error: blocks %jd files %d\n",
1157 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1158 fs->fs_pendinginodes);
1159 fs->fs_pendingblocks = 0;
1160 fs->fs_pendinginodes = 0;
1163 if (fs->fs_ronly == 0) {
1164 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1165 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1166 if (error && error != ENXIO) {
1172 vfs_write_resume(mp);
1173 vn_start_write(NULL, &mp, V_WAIT);
1177 g_vfs_close(ump->um_cp);
1178 g_topology_unlock();
1180 vrele(ump->um_devvp);
1181 dev_rel(ump->um_dev);
1182 mtx_destroy(UFS_MTX(ump));
1183 if (mp->mnt_gjprovider != NULL) {
1184 free(mp->mnt_gjprovider, M_UFSMNT);
1185 mp->mnt_gjprovider = NULL;
1187 free(fs->fs_csp, M_UFSMNT);
1189 free(ump, M_UFSMNT);
1190 mp->mnt_data = NULL;
1192 mp->mnt_flag &= ~MNT_LOCAL;
1198 vfs_write_resume(mp);
1199 vn_start_write(NULL, &mp, V_WAIT);
1203 ufs_extattr_uepm_init(&ump->um_extattr);
1204 #ifdef UFS_EXTATTR_AUTOSTART
1205 (void) ufs_extattr_autostart(mp, td);
1214 * Flush out all the files in a filesystem.
1217 ffs_flushfiles(mp, flags, td)
1222 struct ufsmount *ump;
1227 if (mp->mnt_flag & MNT_QUOTA) {
1229 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1232 for (i = 0; i < MAXQUOTAS; i++) {
1233 quotaoff(td, mp, i);
1236 * Here we fall through to vflush again to ensure
1237 * that we have gotten rid of all the system vnodes.
1241 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1242 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1243 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1245 ffs_snapshot_unmount(mp);
1246 flags |= FORCECLOSE;
1248 * Here we fall through to vflush again to ensure
1249 * that we have gotten rid of all the system vnodes.
1253 * Flush all the files.
1255 if ((error = vflush(mp, 0, flags, td)) != 0)
1258 * Flush filesystem metadata.
1260 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1261 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1262 VOP_UNLOCK(ump->um_devvp, 0);
1267 * Get filesystem statistics.
1274 struct ufsmount *ump;
1279 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1280 panic("ffs_statfs");
1281 sbp->f_version = STATFS_VERSION;
1282 sbp->f_bsize = fs->fs_fsize;
1283 sbp->f_iosize = fs->fs_bsize;
1284 sbp->f_blocks = fs->fs_dsize;
1286 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1287 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1288 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1289 dbtofsb(fs, fs->fs_pendingblocks);
1290 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1291 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1293 sbp->f_namemax = NAME_MAX;
1298 * Go through the disk queues to initiate sandbagged IO;
1299 * go through the inodes to write those that have been modified;
1300 * initiate the writing of the super block if it has been modified.
1302 * Note: we are always called with the filesystem marked `MPBUSY'.
1305 ffs_sync(mp, waitfor)
1309 struct vnode *mvp, *vp, *devvp;
1312 struct ufsmount *ump = VFSTOUFS(mp);
1314 int error, count, wait, lockreq, allerror = 0;
1317 int secondary_writes;
1318 int secondary_accwrites;
1320 int softdep_accdeps;
1325 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1326 printf("fs = %s\n", fs->fs_fsmnt);
1327 panic("ffs_sync: rofs mod");
1330 * Write back each (modified) inode.
1335 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1336 if (waitfor == MNT_SUSPEND) {
1340 if (waitfor == MNT_WAIT) {
1342 lockreq = LK_EXCLUSIVE;
1344 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1347 /* Grab snapshot of secondary write counts */
1348 secondary_writes = mp->mnt_secondary_writes;
1349 secondary_accwrites = mp->mnt_secondary_accwrites;
1351 /* Grab snapshot of softdep dependency counts */
1353 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1356 MNT_VNODE_FOREACH(vp, mp, mvp) {
1358 * Depend on the mntvnode_slock to keep things stable enough
1359 * for a quick test. Since there might be hundreds of
1360 * thousands of vnodes, we cannot afford even a subroutine
1361 * call unless there's a good chance that we have work to do.
1364 if (vp->v_iflag & VI_DOOMED) {
1369 if (vp->v_type == VNON || ((ip->i_flag &
1370 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1371 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1376 if ((error = vget(vp, lockreq, td)) != 0) {
1378 if (error == ENOENT || error == ENOLCK) {
1379 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1384 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1391 * Force stale filesystem control information to be flushed.
1393 if (waitfor == MNT_WAIT) {
1394 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1396 /* Flushed work items may create new vnodes to clean */
1397 if (allerror == 0 && count) {
1405 devvp = ump->um_devvp;
1406 bo = &devvp->v_bufobj;
1408 if (waitfor != MNT_LAZY &&
1409 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1411 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1412 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1414 VOP_UNLOCK(devvp, 0);
1415 if (allerror == 0 && waitfor == MNT_WAIT) {
1419 } else if (suspend != 0) {
1420 if (softdep_check_suspend(mp,
1425 secondary_accwrites) != 0)
1426 goto loop; /* More work needed */
1427 mtx_assert(MNT_MTX(mp), MA_OWNED);
1428 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1434 * Write back modified superblock.
1436 if (fs->fs_fmod != 0 &&
1437 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1443 ffs_vget(mp, ino, flags, vpp)
1449 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1453 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1462 struct ufsmount *ump;
1468 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1469 if (error || *vpp != NULL)
1473 * We must promote to an exclusive lock for vnode creation. This
1474 * can happen if lookup is passed LOCKSHARED.
1476 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1477 flags &= ~LK_TYPE_MASK;
1478 flags |= LK_EXCLUSIVE;
1482 * We do not lock vnode creation as it is believed to be too
1483 * expensive for such rare case as simultaneous creation of vnode
1484 * for same ino by different processes. We just allow them to race
1485 * and check later to decide who wins. Let the race begin!
1493 * If this malloc() is performed after the getnewvnode()
1494 * it might block, leaving a vnode with a NULL v_data to be
1495 * found by ffs_sync() if a sync happens to fire right then,
1496 * which will cause a panic because ffs_sync() blindly
1497 * dereferences vp->v_data (as well it should).
1499 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1501 /* Allocate a new vnode/inode. */
1502 if (fs->fs_magic == FS_UFS1_MAGIC)
1503 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1505 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1508 uma_zfree(uma_inode, ip);
1512 * FFS supports recursive locking.
1516 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1526 for (i = 0; i < MAXQUOTAS; i++)
1527 ip->i_dquot[i] = NODQUOT;
1531 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1532 if (ffs_flags & FFSV_FORCEINSMQ)
1533 vp->v_vflag |= VV_FORCEINSMQ;
1534 error = insmntque(vp, mp);
1536 uma_zfree(uma_inode, ip);
1540 vp->v_vflag &= ~VV_FORCEINSMQ;
1541 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1542 if (error || *vpp != NULL)
1545 /* Read in the disk contents for the inode, copy into the inode. */
1546 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1547 (int)fs->fs_bsize, NOCRED, &bp);
1550 * The inode does not contain anything useful, so it would
1551 * be misleading to leave it on its hash chain. With mode
1552 * still zero, it will be unlinked and returned to the free
1560 if (ip->i_ump->um_fstype == UFS1)
1561 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1563 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1564 ffs_load_inode(bp, ip, fs, ino);
1565 if (DOINGSOFTDEP(vp))
1566 softdep_load_inodeblock(ip);
1568 ip->i_effnlink = ip->i_nlink;
1572 * Initialize the vnode from the inode, check for aliases.
1573 * Note that the underlying vnode may have changed.
1575 if (ip->i_ump->um_fstype == UFS1)
1576 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1578 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1586 * Finish inode initialization.
1588 if (vp->v_type != VFIFO) {
1589 /* FFS supports shared locking for all files except fifos. */
1594 * Set up a generation number for this inode if it does not
1595 * already have one. This should only happen on old filesystems.
1597 if (ip->i_gen == 0) {
1598 ip->i_gen = arc4random() / 2 + 1;
1599 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1600 ip->i_flag |= IN_MODIFIED;
1601 DIP_SET(ip, i_gen, ip->i_gen);
1605 * Ensure that uid and gid are correct. This is a temporary
1606 * fix until fsck has been changed to do the update.
1608 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
1609 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1610 ip->i_uid = ip->i_din1->di_ouid; /* XXX */
1611 ip->i_gid = ip->i_din1->di_ogid; /* XXX */
1615 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1617 * If this vnode is already allocated, and we're running
1618 * multi-label, attempt to perform a label association
1619 * from the extended attributes on the inode.
1621 error = mac_vnode_associate_extattr(mp, vp);
1623 /* ufs_inactive will release ip->i_devvp ref. */
1636 * File handle to vnode
1638 * Have to be really careful about stale file handles:
1639 * - check that the inode number is valid
1640 * - call ffs_vget() to get the locked inode
1641 * - check for an unallocated inode (i_mode == 0)
1642 * - check that the given client host has export rights and return
1643 * those rights via. exflagsp and credanonp
1646 ffs_fhtovp(mp, fhp, vpp)
1654 ufhp = (struct ufid *)fhp;
1655 fs = VFSTOUFS(mp)->um_fs;
1656 if (ufhp->ufid_ino < ROOTINO ||
1657 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1659 return (ufs_fhtovp(mp, ufhp, vpp));
1663 * Initialize the filesystem.
1667 struct vfsconf *vfsp;
1670 softdep_initialize();
1671 return (ufs_init(vfsp));
1675 * Undo the work of ffs_init().
1679 struct vfsconf *vfsp;
1683 ret = ufs_uninit(vfsp);
1684 softdep_uninitialize();
1689 * Write a superblock and associated information back to disk.
1692 ffs_sbupdate(mp, waitfor, suspended)
1693 struct ufsmount *mp;
1697 struct fs *fs = mp->um_fs;
1702 int i, size, error, allerror = 0;
1704 if (fs->fs_ronly == 1 &&
1705 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1706 (MNT_RDONLY | MNT_UPDATE))
1707 panic("ffs_sbupdate: write read-only filesystem");
1709 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1711 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1714 * First write back the summary information.
1716 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1718 for (i = 0; i < blks; i += fs->fs_frag) {
1719 size = fs->fs_bsize;
1720 if (i + fs->fs_frag > blks)
1721 size = (blks - i) * fs->fs_fsize;
1722 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1724 bcopy(space, bp->b_data, (u_int)size);
1725 space = (char *)space + size;
1727 bp->b_flags |= B_VALIDSUSPWRT;
1728 if (waitfor != MNT_WAIT)
1730 else if ((error = bwrite(bp)) != 0)
1734 * Now write back the superblock itself. If any errors occurred
1735 * up to this point, then fail so that the superblock avoids
1736 * being written out as clean.
1743 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1744 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1745 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1746 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1747 fs->fs_sblockloc = SBLOCK_UFS1;
1749 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1750 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1751 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1752 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1753 fs->fs_sblockloc = SBLOCK_UFS2;
1756 fs->fs_time = time_second;
1757 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1758 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1760 bp->b_flags |= B_VALIDSUSPWRT;
1761 if (waitfor != MNT_WAIT)
1763 else if ((error = bwrite(bp)) != 0)
1769 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1770 int attrnamespace, const char *attrname)
1774 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1777 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1783 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1786 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1787 uma_zfree(uma_ufs1, ip->i_din1);
1788 else if (ip->i_din2 != NULL)
1789 uma_zfree(uma_ufs2, ip->i_din2);
1790 uma_zfree(uma_inode, ip);
1793 static int dobkgrdwrite = 1;
1794 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1795 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1798 * Complete a background write started from bwrite.
1801 ffs_backgroundwritedone(struct buf *bp)
1803 struct bufobj *bufobj;
1807 * Find the original buffer that we are writing.
1809 bufobj = bp->b_bufobj;
1811 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1812 panic("backgroundwritedone: lost buffer");
1813 /* Grab an extra reference to be dropped by the bufdone() below. */
1814 bufobj_wrefl(bufobj);
1817 * Process dependencies then return any unfinished ones.
1819 if (!LIST_EMPTY(&bp->b_dep))
1822 if (!LIST_EMPTY(&bp->b_dep))
1823 softdep_move_dependencies(bp, origbp);
1826 * This buffer is marked B_NOCACHE so when it is released
1827 * by biodone it will be tossed.
1829 bp->b_flags |= B_NOCACHE;
1830 bp->b_flags &= ~B_CACHE;
1834 * Clear the BV_BKGRDINPROG flag in the original buffer
1835 * and awaken it if it is waiting for the write to complete.
1836 * If BV_BKGRDINPROG is not set in the original buffer it must
1837 * have been released and re-instantiated - which is not legal.
1839 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1840 ("backgroundwritedone: lost buffer2"));
1841 origbp->b_vflags &= ~BV_BKGRDINPROG;
1842 if (origbp->b_vflags & BV_BKGRDWAIT) {
1843 origbp->b_vflags &= ~BV_BKGRDWAIT;
1844 wakeup(&origbp->b_xflags);
1851 * Write, release buffer on completion. (Done by iodone
1852 * if async). Do not bother writing anything if the buffer
1855 * Note that we set B_CACHE here, indicating that buffer is
1856 * fully valid and thus cacheable. This is true even of NFS
1857 * now so we set it generally. This could be set either here
1858 * or in biodone() since the I/O is synchronous. We put it
1862 ffs_bufwrite(struct buf *bp)
1867 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1868 if (bp->b_flags & B_INVAL) {
1873 oldflags = bp->b_flags;
1875 if (!BUF_ISLOCKED(bp))
1876 panic("bufwrite: buffer is not busy???");
1879 * If a background write is already in progress, delay
1880 * writing this block if it is asynchronous. Otherwise
1881 * wait for the background write to complete.
1883 BO_LOCK(bp->b_bufobj);
1884 if (bp->b_vflags & BV_BKGRDINPROG) {
1885 if (bp->b_flags & B_ASYNC) {
1886 BO_UNLOCK(bp->b_bufobj);
1891 bp->b_vflags |= BV_BKGRDWAIT;
1892 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1893 if (bp->b_vflags & BV_BKGRDINPROG)
1894 panic("bufwrite: still writing");
1896 BO_UNLOCK(bp->b_bufobj);
1898 /* Mark the buffer clean */
1902 * If this buffer is marked for background writing and we
1903 * do not have to wait for it, make a copy and write the
1904 * copy so as to leave this buffer ready for further use.
1906 * This optimization eats a lot of memory. If we have a page
1907 * or buffer shortfall we can't do it.
1909 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1910 (bp->b_flags & B_ASYNC) &&
1911 !vm_page_count_severe() &&
1912 !buf_dirty_count_severe()) {
1913 KASSERT(bp->b_iodone == NULL,
1914 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1916 /* get a new block */
1917 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
1922 * set it to be identical to the old block. We have to
1923 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1924 * to avoid confusing the splay tree and gbincore().
1926 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1927 newbp->b_lblkno = bp->b_lblkno;
1928 newbp->b_xflags |= BX_BKGRDMARKER;
1929 BO_LOCK(bp->b_bufobj);
1930 bp->b_vflags |= BV_BKGRDINPROG;
1931 bgetvp(bp->b_vp, newbp);
1932 BO_UNLOCK(bp->b_bufobj);
1933 newbp->b_bufobj = &bp->b_vp->v_bufobj;
1934 newbp->b_blkno = bp->b_blkno;
1935 newbp->b_offset = bp->b_offset;
1936 newbp->b_iodone = ffs_backgroundwritedone;
1937 newbp->b_flags |= B_ASYNC;
1938 newbp->b_flags &= ~B_INVAL;
1941 /* move over the dependencies */
1942 if (!LIST_EMPTY(&bp->b_dep))
1943 softdep_move_dependencies(bp, newbp);
1947 * Initiate write on the copy, release the original to
1948 * the B_LOCKED queue so that it cannot go away until
1949 * the background write completes. If not locked it could go
1950 * away and then be reconstituted while it was being written.
1951 * If the reconstituted buffer were written, we could end up
1952 * with two background copies being written at the same time.
1958 /* Let the normal bufwrite do the rest for us */
1960 return (bufwrite(bp));
1965 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1971 vp = bo->__bo_vnode;
1972 if (bp->b_iocmd == BIO_WRITE) {
1973 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1974 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1975 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1976 panic("ffs_geom_strategy: bad I/O");
1977 bp->b_flags &= ~B_VALIDSUSPWRT;
1978 if ((vp->v_vflag & VV_COPYONWRITE) &&
1979 vp->v_rdev->si_snapdata != NULL) {
1980 if ((bp->b_flags & B_CLUSTER) != 0) {
1981 runningbufwakeup(bp);
1982 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1983 b_cluster.cluster_entry) {
1984 error = ffs_copyonwrite(vp, tbp);
1986 error != EOPNOTSUPP) {
1987 bp->b_error = error;
1988 bp->b_ioflags |= BIO_ERROR;
1993 bp->b_runningbufspace = bp->b_bufsize;
1994 atomic_add_long(&runningbufspace,
1995 bp->b_runningbufspace);
1997 error = ffs_copyonwrite(vp, bp);
1998 if (error != 0 && error != EOPNOTSUPP) {
1999 bp->b_error = error;
2000 bp->b_ioflags |= BIO_ERROR;
2007 if ((bp->b_flags & B_CLUSTER) != 0) {
2008 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2009 b_cluster.cluster_entry) {
2010 if (!LIST_EMPTY(&tbp->b_dep))
2014 if (!LIST_EMPTY(&bp->b_dep))
2020 g_vfs_strategy(bo, bp);
2026 db_print_ffs(struct ufsmount *ump)
2028 db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
2030 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2031 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2032 ump->softdep_on_worklist_inprogress, ump->softdep_deps,
2036 DB_SHOW_COMMAND(ffs, db_show_ffs)
2039 struct ufsmount *ump;
2042 ump = VFSTOUFS((struct mount *)addr);
2047 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2048 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2049 db_print_ffs(VFSTOUFS(mp));