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;
467 error = copyin(data, &args, sizeof args);
471 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
472 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
473 error = kernel_mount(ma, flags);
479 * Reload all incore data for a filesystem (used after running fsck on
480 * the root filesystem and finding things to fix). The filesystem must
481 * be mounted read-only.
483 * Things to do to update the mount:
484 * 1) invalidate all cached meta-data.
485 * 2) re-read superblock from disk.
486 * 3) re-read summary information from disk.
487 * 4) invalidate all inactive vnodes.
488 * 5) invalidate all cached file data.
489 * 6) re-read inode data for all active vnodes.
492 ffs_reload(struct mount *mp, struct thread *td)
494 struct vnode *vp, *mvp, *devvp;
498 struct fs *fs, *newfs;
499 struct ufsmount *ump;
500 ufs2_daddr_t sblockloc;
501 int i, blks, size, error;
504 if ((mp->mnt_flag & MNT_RDONLY) == 0)
508 * Step 1: invalidate all cached meta-data.
510 devvp = VFSTOUFS(mp)->um_devvp;
511 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
512 if (vinvalbuf(devvp, 0, 0, 0) != 0)
513 panic("ffs_reload: dirty1");
514 VOP_UNLOCK(devvp, 0);
517 * Step 2: re-read superblock from disk.
519 fs = VFSTOUFS(mp)->um_fs;
520 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
523 newfs = (struct fs *)bp->b_data;
524 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
525 newfs->fs_magic != FS_UFS2_MAGIC) ||
526 newfs->fs_bsize > MAXBSIZE ||
527 newfs->fs_bsize < sizeof(struct fs)) {
529 return (EIO); /* XXX needs translation */
532 * Copy pointer fields back into superblock before copying in XXX
533 * new superblock. These should really be in the ufsmount. XXX
534 * Note that important parameters (eg fs_ncg) are unchanged.
536 newfs->fs_csp = fs->fs_csp;
537 newfs->fs_maxcluster = fs->fs_maxcluster;
538 newfs->fs_contigdirs = fs->fs_contigdirs;
539 newfs->fs_active = fs->fs_active;
540 /* The file system is still read-only. */
542 sblockloc = fs->fs_sblockloc;
543 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
545 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
546 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
548 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
549 printf("%s: reload pending error: blocks %jd files %d\n",
550 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
551 fs->fs_pendinginodes);
552 fs->fs_pendingblocks = 0;
553 fs->fs_pendinginodes = 0;
558 * Step 3: re-read summary information from disk.
560 blks = howmany(fs->fs_cssize, fs->fs_fsize);
562 for (i = 0; i < blks; i += fs->fs_frag) {
564 if (i + fs->fs_frag > blks)
565 size = (blks - i) * fs->fs_fsize;
566 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
570 bcopy(bp->b_data, space, (u_int)size);
571 space = (char *)space + size;
575 * We no longer know anything about clusters per cylinder group.
577 if (fs->fs_contigsumsize > 0) {
578 lp = fs->fs_maxcluster;
579 for (i = 0; i < fs->fs_ncg; i++)
580 *lp++ = fs->fs_contigsumsize;
585 MNT_VNODE_FOREACH(vp, mp, mvp) {
587 if (vp->v_iflag & VI_DOOMED) {
593 * Step 4: invalidate all cached file data.
595 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
596 MNT_VNODE_FOREACH_ABORT(mp, mvp);
599 if (vinvalbuf(vp, 0, 0, 0))
600 panic("ffs_reload: dirty2");
602 * Step 5: re-read inode data for all active vnodes.
606 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
607 (int)fs->fs_bsize, NOCRED, &bp);
611 MNT_VNODE_FOREACH_ABORT(mp, mvp);
614 ffs_load_inode(bp, ip, fs, ip->i_number);
615 ip->i_effnlink = ip->i_nlink;
626 * Possible superblock locations ordered from most to least likely.
628 static int sblock_try[] = SBLOCKSEARCH;
631 * Common code for mount and mountroot
634 ffs_mountfs(devvp, mp, td)
639 struct ufsmount *ump;
644 ufs2_daddr_t sblockloc;
645 int error, i, blks, size, ronly;
648 struct g_consumer *cp;
653 cred = td ? td->td_ucred : NOCRED;
654 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
660 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
663 * If we are a root mount, drop the E flag so fsck can do its magic.
664 * We will pick it up again when we remount R/W.
666 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
667 error = g_access(cp, 0, 0, -1);
670 VOP_UNLOCK(devvp, 0);
673 if (devvp->v_rdev->si_iosize_max != 0)
674 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
675 if (mp->mnt_iosize_max > MAXPHYS)
676 mp->mnt_iosize_max = MAXPHYS;
678 devvp->v_bufobj.bo_ops = &ffs_ops;
683 * Try reading the superblock in each of its possible locations.
685 for (i = 0; sblock_try[i] != -1; i++) {
686 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
689 "Invalid sectorsize %d for superblock size %d",
690 cp->provider->sectorsize, SBLOCKSIZE);
693 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
696 fs = (struct fs *)bp->b_data;
697 sblockloc = sblock_try[i];
698 if ((fs->fs_magic == FS_UFS1_MAGIC ||
699 (fs->fs_magic == FS_UFS2_MAGIC &&
700 (fs->fs_sblockloc == sblockloc ||
701 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
702 fs->fs_bsize <= MAXBSIZE &&
703 fs->fs_bsize >= sizeof(struct fs))
708 if (sblock_try[i] == -1) {
709 error = EINVAL; /* XXX needs translation */
713 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
714 fs->fs_flags &= ~FS_UNCLEAN;
715 if (fs->fs_clean == 0) {
716 fs->fs_flags |= FS_UNCLEAN;
717 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
718 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
719 (fs->fs_flags & FS_DOSOFTDEP))) {
721 "WARNING: %s was not properly dismounted\n",
725 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
730 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
731 (mp->mnt_flag & MNT_FORCE)) {
732 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
733 (intmax_t)fs->fs_pendingblocks,
734 fs->fs_pendinginodes);
735 fs->fs_pendingblocks = 0;
736 fs->fs_pendinginodes = 0;
739 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
740 printf("%s: mount pending error: blocks %jd files %d\n",
741 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
742 fs->fs_pendinginodes);
743 fs->fs_pendingblocks = 0;
744 fs->fs_pendinginodes = 0;
746 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
749 * Get journal provider name.
752 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
753 if (g_io_getattr("GJOURNAL::provider", cp, &size,
754 mp->mnt_gjprovider) == 0) {
755 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
758 mp->mnt_flag |= MNT_GJOURNAL;
762 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
763 mp->mnt_stat.f_mntonname);
764 free(mp->mnt_gjprovider, M_UFSMNT);
765 mp->mnt_gjprovider = NULL;
769 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
770 mp->mnt_stat.f_mntonname);
773 mp->mnt_gjprovider = NULL;
775 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
777 ump->um_bo = &devvp->v_bufobj;
778 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
779 if (fs->fs_magic == FS_UFS1_MAGIC) {
780 ump->um_fstype = UFS1;
781 ump->um_balloc = ffs_balloc_ufs1;
783 ump->um_fstype = UFS2;
784 ump->um_balloc = ffs_balloc_ufs2;
786 ump->um_blkatoff = ffs_blkatoff;
787 ump->um_truncate = ffs_truncate;
788 ump->um_update = ffs_update;
789 ump->um_valloc = ffs_valloc;
790 ump->um_vfree = ffs_vfree;
791 ump->um_ifree = ffs_ifree;
792 ump->um_rdonly = ffs_rdonly;
793 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
794 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
795 if (fs->fs_sbsize < SBLOCKSIZE)
796 bp->b_flags |= B_INVAL | B_NOCACHE;
800 ffs_oldfscompat_read(fs, ump, sblockloc);
801 fs->fs_ronly = ronly;
802 size = fs->fs_cssize;
803 blks = howmany(size, fs->fs_fsize);
804 if (fs->fs_contigsumsize > 0)
805 size += fs->fs_ncg * sizeof(int32_t);
806 size += fs->fs_ncg * sizeof(u_int8_t);
807 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
809 for (i = 0; i < blks; i += fs->fs_frag) {
811 if (i + fs->fs_frag > blks)
812 size = (blks - i) * fs->fs_fsize;
813 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
815 free(fs->fs_csp, M_UFSMNT);
818 bcopy(bp->b_data, space, (u_int)size);
819 space = (char *)space + size;
823 if (fs->fs_contigsumsize > 0) {
824 fs->fs_maxcluster = lp = space;
825 for (i = 0; i < fs->fs_ncg; i++)
826 *lp++ = fs->fs_contigsumsize;
829 size = fs->fs_ncg * sizeof(u_int8_t);
830 fs->fs_contigdirs = (u_int8_t *)space;
831 bzero(fs->fs_contigdirs, size);
832 fs->fs_active = NULL;
834 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
835 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
837 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
838 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
843 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
845 mp->mnt_flag |= MNT_LOCAL;
847 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
850 mp->mnt_flag |= MNT_MULTILABEL;
854 "WARNING: %s: multilabel flag on fs but no MAC support\n",
855 mp->mnt_stat.f_mntonname);
858 if ((fs->fs_flags & FS_ACLS) != 0) {
862 if (mp->mnt_flag & MNT_NFS4ACLS)
863 printf("WARNING: ACLs flag on fs conflicts with "
864 "\"nfsv4acls\" mount option; option ignored\n");
865 mp->mnt_flag &= ~MNT_NFS4ACLS;
866 mp->mnt_flag |= MNT_ACLS;
871 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
872 mp->mnt_stat.f_mntonname);
875 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
879 if (mp->mnt_flag & MNT_ACLS)
880 printf("WARNING: NFSv4 ACLs flag on fs conflicts with "
881 "\"acls\" mount option; option ignored\n");
882 mp->mnt_flag &= ~MNT_ACLS;
883 mp->mnt_flag |= MNT_NFS4ACLS;
888 "WARNING: %s: NFSv4 ACLs flag on fs but no ACLs support\n",
889 mp->mnt_stat.f_mntonname);
895 ump->um_devvp = devvp;
896 ump->um_nindir = fs->fs_nindir;
897 ump->um_bptrtodb = fs->fs_fsbtodb;
898 ump->um_seqinc = fs->fs_frag;
899 for (i = 0; i < MAXQUOTAS; i++)
900 ump->um_quotas[i] = NULLVP;
902 ufs_extattr_uepm_init(&ump->um_extattr);
905 * Set FS local "last mounted on" information (NULL pad)
907 bzero(fs->fs_fsmnt, MAXMNTLEN);
908 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
910 if( mp->mnt_flag & MNT_ROOTFS) {
912 * Root mount; update timestamp in mount structure.
913 * this will be used by the common root mount code
914 * to update the system clock.
916 mp->mnt_time = fs->fs_time;
920 if ((fs->fs_flags & FS_DOSOFTDEP) &&
921 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
922 free(fs->fs_csp, M_UFSMNT);
925 if (fs->fs_snapinum[0] != 0)
926 ffs_snapshot_mount(mp);
929 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
932 * Initialize filesystem stat information in mount struct.
935 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
936 MNTK_EXTENDED_SHARED;
939 #ifdef UFS_EXTATTR_AUTOSTART
942 * Auto-starting does the following:
943 * - check for /.attribute in the fs, and extattr_start if so
944 * - for each file in .attribute, enable that file with
945 * an attribute of the same name.
946 * Not clear how to report errors -- probably eat them.
947 * This would all happen while the filesystem was busy/not
948 * available, so would effectively be "atomic".
950 mp->mnt_stat.f_iosize = fs->fs_bsize;
951 (void) ufs_extattr_autostart(mp, td);
952 #endif /* !UFS_EXTATTR_AUTOSTART */
953 #endif /* !UFS_EXTATTR */
966 mtx_destroy(UFS_MTX(ump));
967 if (mp->mnt_gjprovider != NULL) {
968 free(mp->mnt_gjprovider, M_UFSMNT);
969 mp->mnt_gjprovider = NULL;
971 free(ump->um_fs, M_UFSMNT);
979 #include <sys/sysctl.h>
980 static int bigcgs = 0;
981 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
984 * Sanity checks for loading old filesystem superblocks.
985 * See ffs_oldfscompat_write below for unwound actions.
987 * XXX - Parts get retired eventually.
988 * Unfortunately new bits get added.
991 ffs_oldfscompat_read(fs, ump, sblockloc)
993 struct ufsmount *ump;
994 ufs2_daddr_t sblockloc;
999 * If not yet done, update fs_flags location and value of fs_sblockloc.
1001 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1002 fs->fs_flags = fs->fs_old_flags;
1003 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1004 fs->fs_sblockloc = sblockloc;
1007 * If not yet done, update UFS1 superblock with new wider fields.
1009 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1010 fs->fs_maxbsize = fs->fs_bsize;
1011 fs->fs_time = fs->fs_old_time;
1012 fs->fs_size = fs->fs_old_size;
1013 fs->fs_dsize = fs->fs_old_dsize;
1014 fs->fs_csaddr = fs->fs_old_csaddr;
1015 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1016 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1017 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1018 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1020 if (fs->fs_magic == FS_UFS1_MAGIC &&
1021 fs->fs_old_inodefmt < FS_44INODEFMT) {
1022 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1023 fs->fs_qbmask = ~fs->fs_bmask;
1024 fs->fs_qfmask = ~fs->fs_fmask;
1026 if (fs->fs_magic == FS_UFS1_MAGIC) {
1027 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1028 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1029 if (fs->fs_maxfilesize > maxfilesize)
1030 fs->fs_maxfilesize = maxfilesize;
1032 /* Compatibility for old filesystems */
1033 if (fs->fs_avgfilesize <= 0)
1034 fs->fs_avgfilesize = AVFILESIZ;
1035 if (fs->fs_avgfpdir <= 0)
1036 fs->fs_avgfpdir = AFPDIR;
1038 fs->fs_save_cgsize = fs->fs_cgsize;
1039 fs->fs_cgsize = fs->fs_bsize;
1044 * Unwinding superblock updates for old filesystems.
1045 * See ffs_oldfscompat_read above for details.
1047 * XXX - Parts get retired eventually.
1048 * Unfortunately new bits get added.
1051 ffs_oldfscompat_write(fs, ump)
1053 struct ufsmount *ump;
1057 * Copy back UFS2 updated fields that UFS1 inspects.
1059 if (fs->fs_magic == FS_UFS1_MAGIC) {
1060 fs->fs_old_time = fs->fs_time;
1061 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1062 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1063 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1064 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1065 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1068 fs->fs_cgsize = fs->fs_save_cgsize;
1069 fs->fs_save_cgsize = 0;
1074 * unmount system call
1077 ffs_unmount(mp, mntflags)
1082 struct ufsmount *ump = VFSTOUFS(mp);
1084 int error, flags, susp;
1092 if (mntflags & MNT_FORCE) {
1093 flags |= FORCECLOSE;
1094 susp = fs->fs_ronly != 0;
1098 if ((error = ufs_extattr_stop(mp, td))) {
1099 if (error != EOPNOTSUPP)
1100 printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1104 ufs_extattr_uepm_destroy(&ump->um_extattr);
1110 * dounmount already called vn_start_write().
1113 vn_finished_write(mp);
1114 if ((error = vfs_write_suspend(mp)) != 0)
1117 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1118 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1120 wakeup(&mp->mnt_flag);
1122 td->td_pflags |= TDP_IGNSUSP;
1126 vn_start_write(NULL, &mp, V_WAIT);
1129 if (mp->mnt_flag & MNT_SOFTDEP)
1130 error = softdep_flushfiles(mp, flags, td);
1132 error = ffs_flushfiles(mp, flags, td);
1133 if (error != 0 && error != ENXIO)
1137 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1138 printf("%s: unmount pending error: blocks %jd files %d\n",
1139 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1140 fs->fs_pendinginodes);
1141 fs->fs_pendingblocks = 0;
1142 fs->fs_pendinginodes = 0;
1145 if (fs->fs_ronly == 0) {
1146 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1147 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1148 if (error && error != ENXIO) {
1154 vfs_write_resume(mp);
1155 vn_start_write(NULL, &mp, V_WAIT);
1159 g_vfs_close(ump->um_cp);
1160 g_topology_unlock();
1162 vrele(ump->um_devvp);
1163 dev_rel(ump->um_dev);
1164 mtx_destroy(UFS_MTX(ump));
1165 if (mp->mnt_gjprovider != NULL) {
1166 free(mp->mnt_gjprovider, M_UFSMNT);
1167 mp->mnt_gjprovider = NULL;
1169 free(fs->fs_csp, M_UFSMNT);
1171 free(ump, M_UFSMNT);
1172 mp->mnt_data = NULL;
1174 mp->mnt_flag &= ~MNT_LOCAL;
1180 vfs_write_resume(mp);
1181 vn_start_write(NULL, &mp, V_WAIT);
1185 ufs_extattr_uepm_init(&ump->um_extattr);
1186 #ifdef UFS_EXTATTR_AUTOSTART
1187 (void) ufs_extattr_autostart(mp, td);
1196 * Flush out all the files in a filesystem.
1199 ffs_flushfiles(mp, flags, td)
1204 struct ufsmount *ump;
1209 if (mp->mnt_flag & MNT_QUOTA) {
1211 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1214 for (i = 0; i < MAXQUOTAS; i++) {
1215 quotaoff(td, mp, i);
1218 * Here we fall through to vflush again to ensure
1219 * that we have gotten rid of all the system vnodes.
1223 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1224 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1225 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1227 ffs_snapshot_unmount(mp);
1228 flags |= FORCECLOSE;
1230 * Here we fall through to vflush again to ensure
1231 * that we have gotten rid of all the system vnodes.
1235 * Flush all the files.
1237 if ((error = vflush(mp, 0, flags, td)) != 0)
1240 * Flush filesystem metadata.
1242 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1243 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1244 VOP_UNLOCK(ump->um_devvp, 0);
1249 * Get filesystem statistics.
1256 struct ufsmount *ump;
1261 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1262 panic("ffs_statfs");
1263 sbp->f_version = STATFS_VERSION;
1264 sbp->f_bsize = fs->fs_fsize;
1265 sbp->f_iosize = fs->fs_bsize;
1266 sbp->f_blocks = fs->fs_dsize;
1268 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1269 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1270 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1271 dbtofsb(fs, fs->fs_pendingblocks);
1272 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1273 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1275 sbp->f_namemax = NAME_MAX;
1280 * Go through the disk queues to initiate sandbagged IO;
1281 * go through the inodes to write those that have been modified;
1282 * initiate the writing of the super block if it has been modified.
1284 * Note: we are always called with the filesystem marked `MPBUSY'.
1287 ffs_sync(mp, waitfor)
1291 struct vnode *mvp, *vp, *devvp;
1294 struct ufsmount *ump = VFSTOUFS(mp);
1296 int error, count, wait, lockreq, allerror = 0;
1299 int secondary_writes;
1300 int secondary_accwrites;
1302 int softdep_accdeps;
1307 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1308 printf("fs = %s\n", fs->fs_fsmnt);
1309 panic("ffs_sync: rofs mod");
1312 * Write back each (modified) inode.
1317 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1318 if (waitfor == MNT_SUSPEND) {
1322 if (waitfor == MNT_WAIT) {
1324 lockreq = LK_EXCLUSIVE;
1326 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1329 /* Grab snapshot of secondary write counts */
1330 secondary_writes = mp->mnt_secondary_writes;
1331 secondary_accwrites = mp->mnt_secondary_accwrites;
1333 /* Grab snapshot of softdep dependency counts */
1335 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1338 MNT_VNODE_FOREACH(vp, mp, mvp) {
1340 * Depend on the mntvnode_slock to keep things stable enough
1341 * for a quick test. Since there might be hundreds of
1342 * thousands of vnodes, we cannot afford even a subroutine
1343 * call unless there's a good chance that we have work to do.
1346 if (vp->v_iflag & VI_DOOMED) {
1351 if (vp->v_type == VNON || ((ip->i_flag &
1352 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1353 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1358 if ((error = vget(vp, lockreq, td)) != 0) {
1360 if (error == ENOENT || error == ENOLCK) {
1361 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1366 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1373 * Force stale filesystem control information to be flushed.
1375 if (waitfor == MNT_WAIT) {
1376 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1378 /* Flushed work items may create new vnodes to clean */
1379 if (allerror == 0 && count) {
1387 devvp = ump->um_devvp;
1388 bo = &devvp->v_bufobj;
1390 if (waitfor != MNT_LAZY &&
1391 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1393 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1394 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1396 VOP_UNLOCK(devvp, 0);
1397 if (allerror == 0 && waitfor == MNT_WAIT) {
1401 } else if (suspend != 0) {
1402 if (softdep_check_suspend(mp,
1407 secondary_accwrites) != 0)
1408 goto loop; /* More work needed */
1409 mtx_assert(MNT_MTX(mp), MA_OWNED);
1410 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1416 * Write back modified superblock.
1418 if (fs->fs_fmod != 0 &&
1419 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1425 ffs_vget(mp, ino, flags, vpp)
1431 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1435 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1444 struct ufsmount *ump;
1450 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1451 if (error || *vpp != NULL)
1455 * We must promote to an exclusive lock for vnode creation. This
1456 * can happen if lookup is passed LOCKSHARED.
1458 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1459 flags &= ~LK_TYPE_MASK;
1460 flags |= LK_EXCLUSIVE;
1464 * We do not lock vnode creation as it is believed to be too
1465 * expensive for such rare case as simultaneous creation of vnode
1466 * for same ino by different processes. We just allow them to race
1467 * and check later to decide who wins. Let the race begin!
1475 * If this malloc() is performed after the getnewvnode()
1476 * it might block, leaving a vnode with a NULL v_data to be
1477 * found by ffs_sync() if a sync happens to fire right then,
1478 * which will cause a panic because ffs_sync() blindly
1479 * dereferences vp->v_data (as well it should).
1481 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1483 /* Allocate a new vnode/inode. */
1484 if (fs->fs_magic == FS_UFS1_MAGIC)
1485 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1487 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1490 uma_zfree(uma_inode, ip);
1494 * FFS supports recursive locking.
1498 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1508 for (i = 0; i < MAXQUOTAS; i++)
1509 ip->i_dquot[i] = NODQUOT;
1513 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1514 if (ffs_flags & FFSV_FORCEINSMQ)
1515 vp->v_vflag |= VV_FORCEINSMQ;
1516 error = insmntque(vp, mp);
1518 uma_zfree(uma_inode, ip);
1522 vp->v_vflag &= ~VV_FORCEINSMQ;
1523 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1524 if (error || *vpp != NULL)
1527 /* Read in the disk contents for the inode, copy into the inode. */
1528 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1529 (int)fs->fs_bsize, NOCRED, &bp);
1532 * The inode does not contain anything useful, so it would
1533 * be misleading to leave it on its hash chain. With mode
1534 * still zero, it will be unlinked and returned to the free
1542 if (ip->i_ump->um_fstype == UFS1)
1543 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1545 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1546 ffs_load_inode(bp, ip, fs, ino);
1547 if (DOINGSOFTDEP(vp))
1548 softdep_load_inodeblock(ip);
1550 ip->i_effnlink = ip->i_nlink;
1554 * Initialize the vnode from the inode, check for aliases.
1555 * Note that the underlying vnode may have changed.
1557 if (ip->i_ump->um_fstype == UFS1)
1558 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1560 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1568 * Finish inode initialization.
1570 if (vp->v_type != VFIFO) {
1571 /* FFS supports shared locking for all files except fifos. */
1576 * Set up a generation number for this inode if it does not
1577 * already have one. This should only happen on old filesystems.
1579 if (ip->i_gen == 0) {
1580 ip->i_gen = arc4random() / 2 + 1;
1581 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1582 ip->i_flag |= IN_MODIFIED;
1583 DIP_SET(ip, i_gen, ip->i_gen);
1587 * Ensure that uid and gid are correct. This is a temporary
1588 * fix until fsck has been changed to do the update.
1590 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
1591 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1592 ip->i_uid = ip->i_din1->di_ouid; /* XXX */
1593 ip->i_gid = ip->i_din1->di_ogid; /* XXX */
1597 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1599 * If this vnode is already allocated, and we're running
1600 * multi-label, attempt to perform a label association
1601 * from the extended attributes on the inode.
1603 error = mac_vnode_associate_extattr(mp, vp);
1605 /* ufs_inactive will release ip->i_devvp ref. */
1618 * File handle to vnode
1620 * Have to be really careful about stale file handles:
1621 * - check that the inode number is valid
1622 * - call ffs_vget() to get the locked inode
1623 * - check for an unallocated inode (i_mode == 0)
1624 * - check that the given client host has export rights and return
1625 * those rights via. exflagsp and credanonp
1628 ffs_fhtovp(mp, fhp, vpp)
1636 ufhp = (struct ufid *)fhp;
1637 fs = VFSTOUFS(mp)->um_fs;
1638 if (ufhp->ufid_ino < ROOTINO ||
1639 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1641 return (ufs_fhtovp(mp, ufhp, vpp));
1645 * Initialize the filesystem.
1649 struct vfsconf *vfsp;
1652 softdep_initialize();
1653 return (ufs_init(vfsp));
1657 * Undo the work of ffs_init().
1661 struct vfsconf *vfsp;
1665 ret = ufs_uninit(vfsp);
1666 softdep_uninitialize();
1671 * Write a superblock and associated information back to disk.
1674 ffs_sbupdate(mp, waitfor, suspended)
1675 struct ufsmount *mp;
1679 struct fs *fs = mp->um_fs;
1684 int i, size, error, allerror = 0;
1686 if (fs->fs_ronly == 1 &&
1687 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1688 (MNT_RDONLY | MNT_UPDATE))
1689 panic("ffs_sbupdate: write read-only filesystem");
1691 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1693 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1696 * First write back the summary information.
1698 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1700 for (i = 0; i < blks; i += fs->fs_frag) {
1701 size = fs->fs_bsize;
1702 if (i + fs->fs_frag > blks)
1703 size = (blks - i) * fs->fs_fsize;
1704 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1706 bcopy(space, bp->b_data, (u_int)size);
1707 space = (char *)space + size;
1709 bp->b_flags |= B_VALIDSUSPWRT;
1710 if (waitfor != MNT_WAIT)
1712 else if ((error = bwrite(bp)) != 0)
1716 * Now write back the superblock itself. If any errors occurred
1717 * up to this point, then fail so that the superblock avoids
1718 * being written out as clean.
1725 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1726 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1727 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1728 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1729 fs->fs_sblockloc = SBLOCK_UFS1;
1731 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1732 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1733 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1734 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1735 fs->fs_sblockloc = SBLOCK_UFS2;
1738 fs->fs_time = time_second;
1739 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1740 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1742 bp->b_flags |= B_VALIDSUSPWRT;
1743 if (waitfor != MNT_WAIT)
1745 else if ((error = bwrite(bp)) != 0)
1751 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1752 int attrnamespace, const char *attrname)
1756 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1759 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1765 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1768 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1769 uma_zfree(uma_ufs1, ip->i_din1);
1770 else if (ip->i_din2 != NULL)
1771 uma_zfree(uma_ufs2, ip->i_din2);
1772 uma_zfree(uma_inode, ip);
1775 static int dobkgrdwrite = 1;
1776 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1777 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1780 * Complete a background write started from bwrite.
1783 ffs_backgroundwritedone(struct buf *bp)
1785 struct bufobj *bufobj;
1789 * Find the original buffer that we are writing.
1791 bufobj = bp->b_bufobj;
1793 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1794 panic("backgroundwritedone: lost buffer");
1795 /* Grab an extra reference to be dropped by the bufdone() below. */
1796 bufobj_wrefl(bufobj);
1799 * Process dependencies then return any unfinished ones.
1801 if (!LIST_EMPTY(&bp->b_dep))
1804 if (!LIST_EMPTY(&bp->b_dep))
1805 softdep_move_dependencies(bp, origbp);
1808 * This buffer is marked B_NOCACHE so when it is released
1809 * by biodone it will be tossed.
1811 bp->b_flags |= B_NOCACHE;
1812 bp->b_flags &= ~B_CACHE;
1816 * Clear the BV_BKGRDINPROG flag in the original buffer
1817 * and awaken it if it is waiting for the write to complete.
1818 * If BV_BKGRDINPROG is not set in the original buffer it must
1819 * have been released and re-instantiated - which is not legal.
1821 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1822 ("backgroundwritedone: lost buffer2"));
1823 origbp->b_vflags &= ~BV_BKGRDINPROG;
1824 if (origbp->b_vflags & BV_BKGRDWAIT) {
1825 origbp->b_vflags &= ~BV_BKGRDWAIT;
1826 wakeup(&origbp->b_xflags);
1833 * Write, release buffer on completion. (Done by iodone
1834 * if async). Do not bother writing anything if the buffer
1837 * Note that we set B_CACHE here, indicating that buffer is
1838 * fully valid and thus cacheable. This is true even of NFS
1839 * now so we set it generally. This could be set either here
1840 * or in biodone() since the I/O is synchronous. We put it
1844 ffs_bufwrite(struct buf *bp)
1849 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1850 if (bp->b_flags & B_INVAL) {
1855 oldflags = bp->b_flags;
1857 if (!BUF_ISLOCKED(bp))
1858 panic("bufwrite: buffer is not busy???");
1861 * If a background write is already in progress, delay
1862 * writing this block if it is asynchronous. Otherwise
1863 * wait for the background write to complete.
1865 BO_LOCK(bp->b_bufobj);
1866 if (bp->b_vflags & BV_BKGRDINPROG) {
1867 if (bp->b_flags & B_ASYNC) {
1868 BO_UNLOCK(bp->b_bufobj);
1873 bp->b_vflags |= BV_BKGRDWAIT;
1874 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1875 if (bp->b_vflags & BV_BKGRDINPROG)
1876 panic("bufwrite: still writing");
1878 BO_UNLOCK(bp->b_bufobj);
1880 /* Mark the buffer clean */
1884 * If this buffer is marked for background writing and we
1885 * do not have to wait for it, make a copy and write the
1886 * copy so as to leave this buffer ready for further use.
1888 * This optimization eats a lot of memory. If we have a page
1889 * or buffer shortfall we can't do it.
1891 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1892 (bp->b_flags & B_ASYNC) &&
1893 !vm_page_count_severe() &&
1894 !buf_dirty_count_severe()) {
1895 KASSERT(bp->b_iodone == NULL,
1896 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1898 /* get a new block */
1899 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
1904 * set it to be identical to the old block. We have to
1905 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1906 * to avoid confusing the splay tree and gbincore().
1908 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1909 newbp->b_lblkno = bp->b_lblkno;
1910 newbp->b_xflags |= BX_BKGRDMARKER;
1911 BO_LOCK(bp->b_bufobj);
1912 bp->b_vflags |= BV_BKGRDINPROG;
1913 bgetvp(bp->b_vp, newbp);
1914 BO_UNLOCK(bp->b_bufobj);
1915 newbp->b_bufobj = &bp->b_vp->v_bufobj;
1916 newbp->b_blkno = bp->b_blkno;
1917 newbp->b_offset = bp->b_offset;
1918 newbp->b_iodone = ffs_backgroundwritedone;
1919 newbp->b_flags |= B_ASYNC;
1920 newbp->b_flags &= ~B_INVAL;
1923 /* move over the dependencies */
1924 if (!LIST_EMPTY(&bp->b_dep))
1925 softdep_move_dependencies(bp, newbp);
1929 * Initiate write on the copy, release the original to
1930 * the B_LOCKED queue so that it cannot go away until
1931 * the background write completes. If not locked it could go
1932 * away and then be reconstituted while it was being written.
1933 * If the reconstituted buffer were written, we could end up
1934 * with two background copies being written at the same time.
1940 /* Let the normal bufwrite do the rest for us */
1942 return (bufwrite(bp));
1947 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1953 vp = bo->__bo_vnode;
1954 if (bp->b_iocmd == BIO_WRITE) {
1955 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1956 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1957 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1958 panic("ffs_geom_strategy: bad I/O");
1959 bp->b_flags &= ~B_VALIDSUSPWRT;
1960 if ((vp->v_vflag & VV_COPYONWRITE) &&
1961 vp->v_rdev->si_snapdata != NULL) {
1962 if ((bp->b_flags & B_CLUSTER) != 0) {
1963 runningbufwakeup(bp);
1964 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1965 b_cluster.cluster_entry) {
1966 error = ffs_copyonwrite(vp, tbp);
1968 error != EOPNOTSUPP) {
1969 bp->b_error = error;
1970 bp->b_ioflags |= BIO_ERROR;
1975 bp->b_runningbufspace = bp->b_bufsize;
1976 atomic_add_long(&runningbufspace,
1977 bp->b_runningbufspace);
1979 error = ffs_copyonwrite(vp, bp);
1980 if (error != 0 && error != EOPNOTSUPP) {
1981 bp->b_error = error;
1982 bp->b_ioflags |= BIO_ERROR;
1989 if ((bp->b_flags & B_CLUSTER) != 0) {
1990 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1991 b_cluster.cluster_entry) {
1992 if (!LIST_EMPTY(&tbp->b_dep))
1996 if (!LIST_EMPTY(&bp->b_dep))
2002 g_vfs_strategy(bo, bp);
2008 db_print_ffs(struct ufsmount *ump)
2010 db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
2012 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2013 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2014 ump->softdep_on_worklist_inprogress, ump->softdep_deps,
2018 DB_SHOW_COMMAND(ffs, db_show_ffs)
2021 struct ufsmount *ump;
2024 ump = VFSTOUFS((struct mount *)addr);
2029 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2030 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2031 db_print_ffs(VFSTOUFS(mp));