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$");
36 #include "opt_quota.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
56 #include <security/mac/mac_framework.h>
58 #include <ufs/ufs/extattr.h>
59 #include <ufs/ufs/gjournal.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/ufs_extern.h>
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ffs/ffs_extern.h>
70 #include <vm/vm_page.h>
72 #include <geom/geom.h>
73 #include <geom/geom_vfs.h>
77 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
79 static int ffs_reload(struct mount *, struct thread *);
80 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
81 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
83 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
84 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
85 static vfs_init_t ffs_init;
86 static vfs_uninit_t ffs_uninit;
87 static vfs_extattrctl_t ffs_extattrctl;
88 static vfs_cmount_t ffs_cmount;
89 static vfs_unmount_t ffs_unmount;
90 static vfs_mount_t ffs_mount;
91 static vfs_statfs_t ffs_statfs;
92 static vfs_fhtovp_t ffs_fhtovp;
93 static vfs_sync_t ffs_sync;
95 static struct vfsops ufs_vfsops = {
96 .vfs_extattrctl = ffs_extattrctl,
97 .vfs_fhtovp = ffs_fhtovp,
99 .vfs_mount = ffs_mount,
100 .vfs_cmount = ffs_cmount,
101 .vfs_quotactl = ufs_quotactl,
102 .vfs_root = ufs_root,
103 .vfs_statfs = ffs_statfs,
104 .vfs_sync = ffs_sync,
105 .vfs_uninit = ffs_uninit,
106 .vfs_unmount = ffs_unmount,
107 .vfs_vget = ffs_vget,
108 .vfs_susp_clean = process_deferred_inactive,
111 VFS_SET(ufs_vfsops, ufs, 0);
112 MODULE_VERSION(ufs, 1);
114 static b_strategy_t ffs_geom_strategy;
115 static b_write_t ffs_bufwrite;
117 static struct buf_ops ffs_ops = {
119 .bop_write = ffs_bufwrite,
120 .bop_strategy = ffs_geom_strategy,
122 #ifdef NO_FFS_SNAPSHOT
123 .bop_bdflush = bufbdflush,
125 .bop_bdflush = ffs_bdflush,
129 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
130 "noclusterw", "noexec", "export", "force", "from", "multilabel",
131 "snapshot", "nosuid", "suiddir", "nosymfollow", "sync",
135 ffs_mount(struct mount *mp, struct thread *td)
138 struct ufsmount *ump = 0;
141 u_int mntorflags, mntandnotflags;
143 struct nameidata ndp;
146 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
148 if (uma_inode == NULL) {
149 uma_inode = uma_zcreate("FFS inode",
150 sizeof(struct inode), NULL, NULL, NULL, NULL,
152 uma_ufs1 = uma_zcreate("FFS1 dinode",
153 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
155 uma_ufs2 = uma_zcreate("FFS2 dinode",
156 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
160 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
166 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
167 mntorflags |= MNT_ACLS;
169 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
170 mntorflags |= MNT_SNAPSHOT;
172 * Once we have set the MNT_SNAPSHOT flag, do not
173 * persist "snapshot" in the options list.
175 vfs_deleteopt(mp->mnt_optnew, "snapshot");
176 vfs_deleteopt(mp->mnt_opt, "snapshot");
180 mp->mnt_flag = (mp->mnt_flag | mntorflags) & ~mntandnotflags;
183 * If updating, check whether changing from read-only to
184 * read/write; if there is no device name, that's all we do.
186 if (mp->mnt_flag & MNT_UPDATE) {
189 devvp = ump->um_devvp;
190 if (fs->fs_ronly == 0 &&
191 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
192 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
195 * Flush any dirty data.
197 if ((error = ffs_sync(mp, MNT_WAIT, td)) != 0) {
198 vn_finished_write(mp);
202 * Check for and optionally get rid of files open
206 if (mp->mnt_flag & MNT_FORCE)
208 if (mp->mnt_flag & MNT_SOFTDEP) {
209 error = softdep_flushfiles(mp, flags, td);
211 error = ffs_flushfiles(mp, flags, td);
214 vn_finished_write(mp);
217 if (fs->fs_pendingblocks != 0 ||
218 fs->fs_pendinginodes != 0) {
219 printf("%s: %s: blocks %jd files %d\n",
220 fs->fs_fsmnt, "update error",
221 (intmax_t)fs->fs_pendingblocks,
222 fs->fs_pendinginodes);
223 fs->fs_pendingblocks = 0;
224 fs->fs_pendinginodes = 0;
226 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
228 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
231 vn_finished_write(mp);
234 vn_finished_write(mp);
237 g_access(ump->um_cp, 0, -1, 0);
242 mp->mnt_flag |= MNT_RDONLY;
245 if ((mp->mnt_flag & MNT_RELOAD) &&
246 (error = ffs_reload(mp, td)) != 0)
249 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
251 * If upgrade to read-write by non-root, then verify
252 * that user has necessary permissions on the device.
254 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
255 error = VOP_ACCESS(devvp, VREAD | VWRITE,
258 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
260 VOP_UNLOCK(devvp, 0);
263 VOP_UNLOCK(devvp, 0);
264 fs->fs_flags &= ~FS_UNCLEAN;
265 if (fs->fs_clean == 0) {
266 fs->fs_flags |= FS_UNCLEAN;
267 if ((mp->mnt_flag & MNT_FORCE) ||
268 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
269 (fs->fs_flags & FS_DOSOFTDEP))) {
270 printf("WARNING: %s was not %s\n",
271 fs->fs_fsmnt, "properly dismounted");
274 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
282 * If we're the root device, we may not have an E count
285 if (ump->um_cp->ace == 0)
286 error = g_access(ump->um_cp, 0, 1, 1);
288 error = g_access(ump->um_cp, 0, 1, 0);
293 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
297 mp->mnt_flag &= ~MNT_RDONLY;
300 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
301 vn_finished_write(mp);
304 /* check to see if we need to start softdep */
305 if ((fs->fs_flags & FS_DOSOFTDEP) &&
306 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
307 vn_finished_write(mp);
310 if (fs->fs_snapinum[0] != 0)
311 ffs_snapshot_mount(mp);
312 vn_finished_write(mp);
315 * Soft updates is incompatible with "async",
316 * so if we are doing softupdates stop the user
317 * from setting the async flag in an update.
318 * Softdep_mount() clears it in an initial mount
321 if (mp->mnt_flag & MNT_SOFTDEP) {
322 /* XXX: Reset too late ? */
324 mp->mnt_flag &= ~MNT_ASYNC;
328 * Keep MNT_ACLS flag if it is stored in superblock.
330 if ((fs->fs_flags & FS_ACLS) != 0) {
331 /* XXX: Set too late ? */
333 mp->mnt_flag |= MNT_ACLS;
338 * If this is a snapshot request, take the snapshot.
340 if (mp->mnt_flag & MNT_SNAPSHOT)
341 return (ffs_snapshot(mp, fspec));
345 * Not an update, or updating the name: look up the name
346 * and verify that it refers to a sensible disk device.
348 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
349 if ((error = namei(&ndp)) != 0)
351 NDFREE(&ndp, NDF_ONLY_PNBUF);
353 if (!vn_isdisk(devvp, &error)) {
359 * If mount by non-root, then verify that user has necessary
360 * permissions on the device.
363 if ((mp->mnt_flag & MNT_RDONLY) == 0)
364 accessmode |= VWRITE;
365 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
367 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
373 if (mp->mnt_flag & MNT_UPDATE) {
377 * If it's not the same vnode, or at least the same device
378 * then it's not correct.
381 if (devvp->v_rdev != ump->um_devvp->v_rdev)
382 error = EINVAL; /* needs translation */
390 * We need the name for the mount point (also used for
391 * "last mounted on") copied in. If an error occurs,
392 * the mount point is discarded by the upper level code.
393 * Note that vfs_mount() populates f_mntonname for us.
395 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
400 vfs_mountedfrom(mp, fspec);
405 * Compatibility with old mount system call.
409 ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
411 struct ufs_args args;
416 error = copyin(data, &args, sizeof args);
420 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
421 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
422 error = kernel_mount(ma, flags);
428 * Reload all incore data for a filesystem (used after running fsck on
429 * the root filesystem and finding things to fix). The filesystem must
430 * be mounted read-only.
432 * Things to do to update the mount:
433 * 1) invalidate all cached meta-data.
434 * 2) re-read superblock from disk.
435 * 3) re-read summary information from disk.
436 * 4) invalidate all inactive vnodes.
437 * 5) invalidate all cached file data.
438 * 6) re-read inode data for all active vnodes.
441 ffs_reload(struct mount *mp, struct thread *td)
443 struct vnode *vp, *mvp, *devvp;
447 struct fs *fs, *newfs;
448 struct ufsmount *ump;
449 ufs2_daddr_t sblockloc;
450 int i, blks, size, error;
453 if ((mp->mnt_flag & MNT_RDONLY) == 0)
457 * Step 1: invalidate all cached meta-data.
459 devvp = VFSTOUFS(mp)->um_devvp;
460 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
461 if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
462 panic("ffs_reload: dirty1");
463 VOP_UNLOCK(devvp, 0);
466 * Step 2: re-read superblock from disk.
468 fs = VFSTOUFS(mp)->um_fs;
469 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
472 newfs = (struct fs *)bp->b_data;
473 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
474 newfs->fs_magic != FS_UFS2_MAGIC) ||
475 newfs->fs_bsize > MAXBSIZE ||
476 newfs->fs_bsize < sizeof(struct fs)) {
478 return (EIO); /* XXX needs translation */
481 * Copy pointer fields back into superblock before copying in XXX
482 * new superblock. These should really be in the ufsmount. XXX
483 * Note that important parameters (eg fs_ncg) are unchanged.
485 newfs->fs_csp = fs->fs_csp;
486 newfs->fs_maxcluster = fs->fs_maxcluster;
487 newfs->fs_contigdirs = fs->fs_contigdirs;
488 newfs->fs_active = fs->fs_active;
489 /* The file system is still read-only. */
491 sblockloc = fs->fs_sblockloc;
492 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
494 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
495 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
497 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
498 printf("%s: reload pending error: blocks %jd files %d\n",
499 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
500 fs->fs_pendinginodes);
501 fs->fs_pendingblocks = 0;
502 fs->fs_pendinginodes = 0;
507 * Step 3: re-read summary information from disk.
509 blks = howmany(fs->fs_cssize, fs->fs_fsize);
511 for (i = 0; i < blks; i += fs->fs_frag) {
513 if (i + fs->fs_frag > blks)
514 size = (blks - i) * fs->fs_fsize;
515 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
519 bcopy(bp->b_data, space, (u_int)size);
520 space = (char *)space + size;
524 * We no longer know anything about clusters per cylinder group.
526 if (fs->fs_contigsumsize > 0) {
527 lp = fs->fs_maxcluster;
528 for (i = 0; i < fs->fs_ncg; i++)
529 *lp++ = fs->fs_contigsumsize;
534 MNT_VNODE_FOREACH(vp, mp, mvp) {
536 if (vp->v_iflag & VI_DOOMED) {
542 * Step 4: invalidate all cached file data.
544 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
545 MNT_VNODE_FOREACH_ABORT(mp, mvp);
548 if (vinvalbuf(vp, 0, td, 0, 0))
549 panic("ffs_reload: dirty2");
551 * Step 5: re-read inode data for all active vnodes.
555 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
556 (int)fs->fs_bsize, NOCRED, &bp);
560 MNT_VNODE_FOREACH_ABORT(mp, mvp);
563 ffs_load_inode(bp, ip, fs, ip->i_number);
564 ip->i_effnlink = ip->i_nlink;
575 * Possible superblock locations ordered from most to least likely.
577 static int sblock_try[] = SBLOCKSEARCH;
580 * Common code for mount and mountroot
583 ffs_mountfs(devvp, mp, td)
588 struct ufsmount *ump;
593 ufs2_daddr_t sblockloc;
594 int error, i, blks, size, ronly;
597 struct g_consumer *cp;
601 cred = td ? td->td_ucred : NOCRED;
603 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
606 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
609 * If we are a root mount, drop the E flag so fsck can do its magic.
610 * We will pick it up again when we remount R/W.
612 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
613 error = g_access(cp, 0, 0, -1);
616 VOP_UNLOCK(devvp, 0);
619 if (devvp->v_rdev->si_iosize_max != 0)
620 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
621 if (mp->mnt_iosize_max > MAXPHYS)
622 mp->mnt_iosize_max = MAXPHYS;
624 devvp->v_bufobj.bo_private = cp;
625 devvp->v_bufobj.bo_ops = &ffs_ops;
632 * Try reading the superblock in each of its possible locations.
634 for (i = 0; sblock_try[i] != -1; i++) {
635 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
638 "Invalid sectorsize %d for superblock size %d",
639 cp->provider->sectorsize, SBLOCKSIZE);
642 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
645 fs = (struct fs *)bp->b_data;
646 sblockloc = sblock_try[i];
647 if ((fs->fs_magic == FS_UFS1_MAGIC ||
648 (fs->fs_magic == FS_UFS2_MAGIC &&
649 (fs->fs_sblockloc == sblockloc ||
650 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
651 fs->fs_bsize <= MAXBSIZE &&
652 fs->fs_bsize >= sizeof(struct fs))
657 if (sblock_try[i] == -1) {
658 error = EINVAL; /* XXX needs translation */
662 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
663 fs->fs_flags &= ~FS_UNCLEAN;
664 if (fs->fs_clean == 0) {
665 fs->fs_flags |= FS_UNCLEAN;
666 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
667 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
668 (fs->fs_flags & FS_DOSOFTDEP))) {
670 "WARNING: %s was not properly dismounted\n",
674 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
679 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
680 (mp->mnt_flag & MNT_FORCE)) {
681 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
682 (intmax_t)fs->fs_pendingblocks,
683 fs->fs_pendinginodes);
684 fs->fs_pendingblocks = 0;
685 fs->fs_pendinginodes = 0;
688 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
689 printf("%s: mount pending error: blocks %jd files %d\n",
690 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
691 fs->fs_pendinginodes);
692 fs->fs_pendingblocks = 0;
693 fs->fs_pendinginodes = 0;
695 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
698 * Get journal provider name.
701 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
702 if (g_io_getattr("GJOURNAL::provider", cp, &size,
703 mp->mnt_gjprovider) == 0) {
704 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
707 mp->mnt_flag |= MNT_GJOURNAL;
711 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
712 mp->mnt_stat.f_mntonname);
713 free(mp->mnt_gjprovider, M_UFSMNT);
714 mp->mnt_gjprovider = NULL;
718 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
719 mp->mnt_stat.f_mntonname);
722 mp->mnt_gjprovider = NULL;
724 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
726 ump->um_bo = &devvp->v_bufobj;
727 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
728 if (fs->fs_magic == FS_UFS1_MAGIC) {
729 ump->um_fstype = UFS1;
730 ump->um_balloc = ffs_balloc_ufs1;
732 ump->um_fstype = UFS2;
733 ump->um_balloc = ffs_balloc_ufs2;
735 ump->um_blkatoff = ffs_blkatoff;
736 ump->um_truncate = ffs_truncate;
737 ump->um_update = ffs_update;
738 ump->um_valloc = ffs_valloc;
739 ump->um_vfree = ffs_vfree;
740 ump->um_ifree = ffs_ifree;
741 ump->um_rdonly = ffs_rdonly;
742 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
743 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
744 if (fs->fs_sbsize < SBLOCKSIZE)
745 bp->b_flags |= B_INVAL | B_NOCACHE;
749 ffs_oldfscompat_read(fs, ump, sblockloc);
750 fs->fs_ronly = ronly;
751 size = fs->fs_cssize;
752 blks = howmany(size, fs->fs_fsize);
753 if (fs->fs_contigsumsize > 0)
754 size += fs->fs_ncg * sizeof(int32_t);
755 size += fs->fs_ncg * sizeof(u_int8_t);
756 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
758 for (i = 0; i < blks; i += fs->fs_frag) {
760 if (i + fs->fs_frag > blks)
761 size = (blks - i) * fs->fs_fsize;
762 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
764 free(fs->fs_csp, M_UFSMNT);
767 bcopy(bp->b_data, space, (u_int)size);
768 space = (char *)space + size;
772 if (fs->fs_contigsumsize > 0) {
773 fs->fs_maxcluster = lp = space;
774 for (i = 0; i < fs->fs_ncg; i++)
775 *lp++ = fs->fs_contigsumsize;
778 size = fs->fs_ncg * sizeof(u_int8_t);
779 fs->fs_contigdirs = (u_int8_t *)space;
780 bzero(fs->fs_contigdirs, size);
781 fs->fs_active = NULL;
783 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
784 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
786 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
787 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
792 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
794 mp->mnt_flag |= MNT_LOCAL;
796 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
799 mp->mnt_flag |= MNT_MULTILABEL;
803 "WARNING: %s: multilabel flag on fs but no MAC support\n",
804 mp->mnt_stat.f_mntonname);
807 if ((fs->fs_flags & FS_ACLS) != 0) {
810 mp->mnt_flag |= MNT_ACLS;
814 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
815 mp->mnt_stat.f_mntonname);
820 ump->um_devvp = devvp;
821 ump->um_nindir = fs->fs_nindir;
822 ump->um_bptrtodb = fs->fs_fsbtodb;
823 ump->um_seqinc = fs->fs_frag;
824 for (i = 0; i < MAXQUOTAS; i++)
825 ump->um_quotas[i] = NULLVP;
827 ufs_extattr_uepm_init(&ump->um_extattr);
830 * Set FS local "last mounted on" information (NULL pad)
832 bzero(fs->fs_fsmnt, MAXMNTLEN);
833 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
835 if( mp->mnt_flag & MNT_ROOTFS) {
837 * Root mount; update timestamp in mount structure.
838 * this will be used by the common root mount code
839 * to update the system clock.
841 mp->mnt_time = fs->fs_time;
845 if ((fs->fs_flags & FS_DOSOFTDEP) &&
846 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
847 free(fs->fs_csp, M_UFSMNT);
850 if (fs->fs_snapinum[0] != 0)
851 ffs_snapshot_mount(mp);
854 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
857 * Initialize filesystem stat information in mount struct.
860 mp->mnt_kern_flag |= MNTK_MPSAFE;
863 #ifdef UFS_EXTATTR_AUTOSTART
866 * Auto-starting does the following:
867 * - check for /.attribute in the fs, and extattr_start if so
868 * - for each file in .attribute, enable that file with
869 * an attribute of the same name.
870 * Not clear how to report errors -- probably eat them.
871 * This would all happen while the filesystem was busy/not
872 * available, so would effectively be "atomic".
874 mp->mnt_stat.f_iosize = fs->fs_bsize;
875 (void) ufs_extattr_autostart(mp, td);
876 #endif /* !UFS_EXTATTR_AUTOSTART */
877 #endif /* !UFS_EXTATTR */
890 mtx_destroy(UFS_MTX(ump));
891 if (mp->mnt_gjprovider != NULL) {
892 free(mp->mnt_gjprovider, M_UFSMNT);
893 mp->mnt_gjprovider = NULL;
895 free(ump->um_fs, M_UFSMNT);
902 #include <sys/sysctl.h>
903 static int bigcgs = 0;
904 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
907 * Sanity checks for loading old filesystem superblocks.
908 * See ffs_oldfscompat_write below for unwound actions.
910 * XXX - Parts get retired eventually.
911 * Unfortunately new bits get added.
914 ffs_oldfscompat_read(fs, ump, sblockloc)
916 struct ufsmount *ump;
917 ufs2_daddr_t sblockloc;
922 * If not yet done, update fs_flags location and value of fs_sblockloc.
924 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
925 fs->fs_flags = fs->fs_old_flags;
926 fs->fs_old_flags |= FS_FLAGS_UPDATED;
927 fs->fs_sblockloc = sblockloc;
930 * If not yet done, update UFS1 superblock with new wider fields.
932 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
933 fs->fs_maxbsize = fs->fs_bsize;
934 fs->fs_time = fs->fs_old_time;
935 fs->fs_size = fs->fs_old_size;
936 fs->fs_dsize = fs->fs_old_dsize;
937 fs->fs_csaddr = fs->fs_old_csaddr;
938 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
939 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
940 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
941 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
943 if (fs->fs_magic == FS_UFS1_MAGIC &&
944 fs->fs_old_inodefmt < FS_44INODEFMT) {
945 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
946 fs->fs_qbmask = ~fs->fs_bmask;
947 fs->fs_qfmask = ~fs->fs_fmask;
949 if (fs->fs_magic == FS_UFS1_MAGIC) {
950 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
951 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
952 if (fs->fs_maxfilesize > maxfilesize)
953 fs->fs_maxfilesize = maxfilesize;
955 /* Compatibility for old filesystems */
956 if (fs->fs_avgfilesize <= 0)
957 fs->fs_avgfilesize = AVFILESIZ;
958 if (fs->fs_avgfpdir <= 0)
959 fs->fs_avgfpdir = AFPDIR;
961 fs->fs_save_cgsize = fs->fs_cgsize;
962 fs->fs_cgsize = fs->fs_bsize;
967 * Unwinding superblock updates for old filesystems.
968 * See ffs_oldfscompat_read above for details.
970 * XXX - Parts get retired eventually.
971 * Unfortunately new bits get added.
974 ffs_oldfscompat_write(fs, ump)
976 struct ufsmount *ump;
980 * Copy back UFS2 updated fields that UFS1 inspects.
982 if (fs->fs_magic == FS_UFS1_MAGIC) {
983 fs->fs_old_time = fs->fs_time;
984 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
985 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
986 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
987 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
988 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
991 fs->fs_cgsize = fs->fs_save_cgsize;
992 fs->fs_save_cgsize = 0;
997 * unmount system call
1000 ffs_unmount(mp, mntflags, td)
1005 struct ufsmount *ump = VFSTOUFS(mp);
1010 if (mntflags & MNT_FORCE) {
1011 flags |= FORCECLOSE;
1014 if ((error = ufs_extattr_stop(mp, td))) {
1015 if (error != EOPNOTSUPP)
1016 printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1019 ufs_extattr_uepm_destroy(&ump->um_extattr);
1022 if (mp->mnt_flag & MNT_SOFTDEP) {
1023 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
1026 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
1031 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1032 printf("%s: unmount pending error: blocks %jd files %d\n",
1033 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1034 fs->fs_pendinginodes);
1035 fs->fs_pendingblocks = 0;
1036 fs->fs_pendinginodes = 0;
1039 if (fs->fs_ronly == 0) {
1040 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1041 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1049 g_vfs_close(ump->um_cp, td);
1050 g_topology_unlock();
1052 vrele(ump->um_devvp);
1053 mtx_destroy(UFS_MTX(ump));
1054 if (mp->mnt_gjprovider != NULL) {
1055 free(mp->mnt_gjprovider, M_UFSMNT);
1056 mp->mnt_gjprovider = NULL;
1058 free(fs->fs_csp, M_UFSMNT);
1060 free(ump, M_UFSMNT);
1061 mp->mnt_data = NULL;
1063 mp->mnt_flag &= ~MNT_LOCAL;
1069 * Flush out all the files in a filesystem.
1072 ffs_flushfiles(mp, flags, td)
1077 struct ufsmount *ump;
1082 if (mp->mnt_flag & MNT_QUOTA) {
1084 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1087 for (i = 0; i < MAXQUOTAS; i++) {
1088 quotaoff(td, mp, i);
1091 * Here we fall through to vflush again to ensure
1092 * that we have gotten rid of all the system vnodes.
1096 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1097 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1098 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1100 ffs_snapshot_unmount(mp);
1101 flags |= FORCECLOSE;
1103 * Here we fall through to vflush again to ensure
1104 * that we have gotten rid of all the system vnodes.
1108 * Flush all the files.
1110 if ((error = vflush(mp, 0, flags, td)) != 0)
1113 * Flush filesystem metadata.
1115 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1116 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1117 VOP_UNLOCK(ump->um_devvp, 0);
1122 * Get filesystem statistics.
1125 ffs_statfs(mp, sbp, td)
1130 struct ufsmount *ump;
1135 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1136 panic("ffs_statfs");
1137 sbp->f_version = STATFS_VERSION;
1138 sbp->f_bsize = fs->fs_fsize;
1139 sbp->f_iosize = fs->fs_bsize;
1140 sbp->f_blocks = fs->fs_dsize;
1142 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1143 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1144 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1145 dbtofsb(fs, fs->fs_pendingblocks);
1146 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1147 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1149 sbp->f_namemax = NAME_MAX;
1154 * Go through the disk queues to initiate sandbagged IO;
1155 * go through the inodes to write those that have been modified;
1156 * initiate the writing of the super block if it has been modified.
1158 * Note: we are always called with the filesystem marked `MPBUSY'.
1161 ffs_sync(mp, waitfor, td)
1166 struct vnode *mvp, *vp, *devvp;
1168 struct ufsmount *ump = VFSTOUFS(mp);
1170 int error, count, wait, lockreq, allerror = 0;
1173 int secondary_writes;
1174 int secondary_accwrites;
1176 int softdep_accdeps;
1180 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1181 printf("fs = %s\n", fs->fs_fsmnt);
1182 panic("ffs_sync: rofs mod");
1185 * Write back each (modified) inode.
1190 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1191 if (waitfor == MNT_SUSPEND) {
1195 if (waitfor == MNT_WAIT) {
1197 lockreq = LK_EXCLUSIVE;
1199 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1202 /* Grab snapshot of secondary write counts */
1203 secondary_writes = mp->mnt_secondary_writes;
1204 secondary_accwrites = mp->mnt_secondary_accwrites;
1206 /* Grab snapshot of softdep dependency counts */
1208 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1211 MNT_VNODE_FOREACH(vp, mp, mvp) {
1213 * Depend on the mntvnode_slock to keep things stable enough
1214 * for a quick test. Since there might be hundreds of
1215 * thousands of vnodes, we cannot afford even a subroutine
1216 * call unless there's a good chance that we have work to do.
1219 if (vp->v_iflag & VI_DOOMED) {
1224 if (vp->v_type == VNON || ((ip->i_flag &
1225 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1226 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1231 if ((error = vget(vp, lockreq, td)) != 0) {
1233 if (error == ENOENT || error == ENOLCK) {
1234 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1239 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1246 * Force stale filesystem control information to be flushed.
1248 if (waitfor == MNT_WAIT) {
1249 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1251 /* Flushed work items may create new vnodes to clean */
1252 if (allerror == 0 && count) {
1260 devvp = ump->um_devvp;
1261 bo = &devvp->v_bufobj;
1263 if (waitfor != MNT_LAZY &&
1264 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1266 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1267 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1269 VOP_UNLOCK(devvp, 0);
1270 if (allerror == 0 && waitfor == MNT_WAIT) {
1274 } else if (suspend != 0) {
1275 if (softdep_check_suspend(mp,
1280 secondary_accwrites) != 0)
1281 goto loop; /* More work needed */
1282 mtx_assert(MNT_MTX(mp), MA_OWNED);
1283 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1289 * Write back modified superblock.
1291 if (fs->fs_fmod != 0 &&
1292 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1298 ffs_vget(mp, ino, flags, vpp)
1304 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1308 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1317 struct ufsmount *ump;
1323 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1324 if (error || *vpp != NULL)
1328 * We must promote to an exclusive lock for vnode creation. This
1329 * can happen if lookup is passed LOCKSHARED.
1331 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1332 flags &= ~LK_TYPE_MASK;
1333 flags |= LK_EXCLUSIVE;
1337 * We do not lock vnode creation as it is believed to be too
1338 * expensive for such rare case as simultaneous creation of vnode
1339 * for same ino by different processes. We just allow them to race
1340 * and check later to decide who wins. Let the race begin!
1348 * If this MALLOC() is performed after the getnewvnode()
1349 * it might block, leaving a vnode with a NULL v_data to be
1350 * found by ffs_sync() if a sync happens to fire right then,
1351 * which will cause a panic because ffs_sync() blindly
1352 * dereferences vp->v_data (as well it should).
1354 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1356 /* Allocate a new vnode/inode. */
1357 if (fs->fs_magic == FS_UFS1_MAGIC)
1358 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1360 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1363 uma_zfree(uma_inode, ip);
1367 * FFS supports recursive and shared locking.
1372 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1381 for (i = 0; i < MAXQUOTAS; i++)
1382 ip->i_dquot[i] = NODQUOT;
1386 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1387 if (ffs_flags & FFSV_FORCEINSMQ)
1388 vp->v_vflag |= VV_FORCEINSMQ;
1389 error = insmntque(vp, mp);
1391 uma_zfree(uma_inode, ip);
1395 vp->v_vflag &= ~VV_FORCEINSMQ;
1396 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1397 if (error || *vpp != NULL)
1400 /* Read in the disk contents for the inode, copy into the inode. */
1401 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1402 (int)fs->fs_bsize, NOCRED, &bp);
1405 * The inode does not contain anything useful, so it would
1406 * be misleading to leave it on its hash chain. With mode
1407 * still zero, it will be unlinked and returned to the free
1415 if (ip->i_ump->um_fstype == UFS1)
1416 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1418 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1419 ffs_load_inode(bp, ip, fs, ino);
1420 if (DOINGSOFTDEP(vp))
1421 softdep_load_inodeblock(ip);
1423 ip->i_effnlink = ip->i_nlink;
1427 * Initialize the vnode from the inode, check for aliases.
1428 * Note that the underlying vnode may have changed.
1430 if (ip->i_ump->um_fstype == UFS1)
1431 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1433 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1441 * Finish inode initialization.
1445 * Set up a generation number for this inode if it does not
1446 * already have one. This should only happen on old filesystems.
1448 if (ip->i_gen == 0) {
1449 ip->i_gen = arc4random() / 2 + 1;
1450 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1451 ip->i_flag |= IN_MODIFIED;
1452 DIP_SET(ip, i_gen, ip->i_gen);
1456 * Ensure that uid and gid are correct. This is a temporary
1457 * fix until fsck has been changed to do the update.
1459 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
1460 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1461 ip->i_uid = ip->i_din1->di_ouid; /* XXX */
1462 ip->i_gid = ip->i_din1->di_ogid; /* XXX */
1466 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1468 * If this vnode is already allocated, and we're running
1469 * multi-label, attempt to perform a label association
1470 * from the extended attributes on the inode.
1472 error = mac_vnode_associate_extattr(mp, vp);
1474 /* ufs_inactive will release ip->i_devvp ref. */
1487 * File handle to vnode
1489 * Have to be really careful about stale file handles:
1490 * - check that the inode number is valid
1491 * - call ffs_vget() to get the locked inode
1492 * - check for an unallocated inode (i_mode == 0)
1493 * - check that the given client host has export rights and return
1494 * those rights via. exflagsp and credanonp
1497 ffs_fhtovp(mp, fhp, vpp)
1505 ufhp = (struct ufid *)fhp;
1506 fs = VFSTOUFS(mp)->um_fs;
1507 if (ufhp->ufid_ino < ROOTINO ||
1508 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1510 return (ufs_fhtovp(mp, ufhp, vpp));
1514 * Initialize the filesystem.
1518 struct vfsconf *vfsp;
1521 softdep_initialize();
1522 return (ufs_init(vfsp));
1526 * Undo the work of ffs_init().
1530 struct vfsconf *vfsp;
1534 ret = ufs_uninit(vfsp);
1535 softdep_uninitialize();
1540 * Write a superblock and associated information back to disk.
1543 ffs_sbupdate(mp, waitfor, suspended)
1544 struct ufsmount *mp;
1548 struct fs *fs = mp->um_fs;
1553 int i, size, error, allerror = 0;
1555 if (fs->fs_ronly == 1 &&
1556 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1557 (MNT_RDONLY | MNT_UPDATE))
1558 panic("ffs_sbupdate: write read-only filesystem");
1560 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1562 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1565 * First write back the summary information.
1567 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1569 for (i = 0; i < blks; i += fs->fs_frag) {
1570 size = fs->fs_bsize;
1571 if (i + fs->fs_frag > blks)
1572 size = (blks - i) * fs->fs_fsize;
1573 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1575 bcopy(space, bp->b_data, (u_int)size);
1576 space = (char *)space + size;
1578 bp->b_flags |= B_VALIDSUSPWRT;
1579 if (waitfor != MNT_WAIT)
1581 else if ((error = bwrite(bp)) != 0)
1585 * Now write back the superblock itself. If any errors occurred
1586 * up to this point, then fail so that the superblock avoids
1587 * being written out as clean.
1594 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1595 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1596 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1597 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1598 fs->fs_sblockloc = SBLOCK_UFS1;
1600 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1601 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1602 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1603 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1604 fs->fs_sblockloc = SBLOCK_UFS2;
1607 fs->fs_time = time_second;
1608 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1609 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1611 bp->b_flags |= B_VALIDSUSPWRT;
1612 if (waitfor != MNT_WAIT)
1614 else if ((error = bwrite(bp)) != 0)
1620 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1621 int attrnamespace, const char *attrname, struct thread *td)
1625 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1628 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1634 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1637 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1638 uma_zfree(uma_ufs1, ip->i_din1);
1639 else if (ip->i_din2 != NULL)
1640 uma_zfree(uma_ufs2, ip->i_din2);
1641 uma_zfree(uma_inode, ip);
1644 static int dobkgrdwrite = 1;
1645 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1646 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1649 * Complete a background write started from bwrite.
1652 ffs_backgroundwritedone(struct buf *bp)
1654 struct bufobj *bufobj;
1658 * Find the original buffer that we are writing.
1660 bufobj = bp->b_bufobj;
1662 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1663 panic("backgroundwritedone: lost buffer");
1664 /* Grab an extra reference to be dropped by the bufdone() below. */
1665 bufobj_wrefl(bufobj);
1668 * Process dependencies then return any unfinished ones.
1670 if (!LIST_EMPTY(&bp->b_dep))
1673 if (!LIST_EMPTY(&bp->b_dep))
1674 softdep_move_dependencies(bp, origbp);
1677 * This buffer is marked B_NOCACHE so when it is released
1678 * by biodone it will be tossed.
1680 bp->b_flags |= B_NOCACHE;
1681 bp->b_flags &= ~B_CACHE;
1685 * Clear the BV_BKGRDINPROG flag in the original buffer
1686 * and awaken it if it is waiting for the write to complete.
1687 * If BV_BKGRDINPROG is not set in the original buffer it must
1688 * have been released and re-instantiated - which is not legal.
1690 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1691 ("backgroundwritedone: lost buffer2"));
1692 origbp->b_vflags &= ~BV_BKGRDINPROG;
1693 if (origbp->b_vflags & BV_BKGRDWAIT) {
1694 origbp->b_vflags &= ~BV_BKGRDWAIT;
1695 wakeup(&origbp->b_xflags);
1702 * Write, release buffer on completion. (Done by iodone
1703 * if async). Do not bother writing anything if the buffer
1706 * Note that we set B_CACHE here, indicating that buffer is
1707 * fully valid and thus cacheable. This is true even of NFS
1708 * now so we set it generally. This could be set either here
1709 * or in biodone() since the I/O is synchronous. We put it
1713 ffs_bufwrite(struct buf *bp)
1718 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1719 if (bp->b_flags & B_INVAL) {
1724 oldflags = bp->b_flags;
1726 if (!BUF_ISLOCKED(bp))
1727 panic("bufwrite: buffer is not busy???");
1730 * If a background write is already in progress, delay
1731 * writing this block if it is asynchronous. Otherwise
1732 * wait for the background write to complete.
1734 BO_LOCK(bp->b_bufobj);
1735 if (bp->b_vflags & BV_BKGRDINPROG) {
1736 if (bp->b_flags & B_ASYNC) {
1737 BO_UNLOCK(bp->b_bufobj);
1742 bp->b_vflags |= BV_BKGRDWAIT;
1743 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1744 if (bp->b_vflags & BV_BKGRDINPROG)
1745 panic("bufwrite: still writing");
1747 BO_UNLOCK(bp->b_bufobj);
1749 /* Mark the buffer clean */
1753 * If this buffer is marked for background writing and we
1754 * do not have to wait for it, make a copy and write the
1755 * copy so as to leave this buffer ready for further use.
1757 * This optimization eats a lot of memory. If we have a page
1758 * or buffer shortfall we can't do it.
1760 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1761 (bp->b_flags & B_ASYNC) &&
1762 !vm_page_count_severe() &&
1763 !buf_dirty_count_severe()) {
1764 KASSERT(bp->b_iodone == NULL,
1765 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1767 /* get a new block */
1768 newbp = geteblk(bp->b_bufsize);
1771 * set it to be identical to the old block. We have to
1772 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1773 * to avoid confusing the splay tree and gbincore().
1775 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1776 newbp->b_lblkno = bp->b_lblkno;
1777 newbp->b_xflags |= BX_BKGRDMARKER;
1778 BO_LOCK(bp->b_bufobj);
1779 bp->b_vflags |= BV_BKGRDINPROG;
1780 bgetvp(bp->b_vp, newbp);
1781 BO_UNLOCK(bp->b_bufobj);
1782 newbp->b_bufobj = &bp->b_vp->v_bufobj;
1783 newbp->b_blkno = bp->b_blkno;
1784 newbp->b_offset = bp->b_offset;
1785 newbp->b_iodone = ffs_backgroundwritedone;
1786 newbp->b_flags |= B_ASYNC;
1787 newbp->b_flags &= ~B_INVAL;
1790 /* move over the dependencies */
1791 if (!LIST_EMPTY(&bp->b_dep))
1792 softdep_move_dependencies(bp, newbp);
1796 * Initiate write on the copy, release the original to
1797 * the B_LOCKED queue so that it cannot go away until
1798 * the background write completes. If not locked it could go
1799 * away and then be reconstituted while it was being written.
1800 * If the reconstituted buffer were written, we could end up
1801 * with two background copies being written at the same time.
1807 /* Let the normal bufwrite do the rest for us */
1808 return (bufwrite(bp));
1813 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1819 vp = bo->__bo_vnode;
1820 if (bp->b_iocmd == BIO_WRITE) {
1821 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1822 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1823 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1824 panic("ffs_geom_strategy: bad I/O");
1825 bp->b_flags &= ~B_VALIDSUSPWRT;
1826 if ((vp->v_vflag & VV_COPYONWRITE) &&
1827 vp->v_rdev->si_snapdata != NULL) {
1828 if ((bp->b_flags & B_CLUSTER) != 0) {
1829 runningbufwakeup(bp);
1830 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1831 b_cluster.cluster_entry) {
1832 error = ffs_copyonwrite(vp, tbp);
1834 error != EOPNOTSUPP) {
1835 bp->b_error = error;
1836 bp->b_ioflags |= BIO_ERROR;
1841 bp->b_runningbufspace = bp->b_bufsize;
1842 atomic_add_int(&runningbufspace,
1843 bp->b_runningbufspace);
1845 error = ffs_copyonwrite(vp, bp);
1846 if (error != 0 && error != EOPNOTSUPP) {
1847 bp->b_error = error;
1848 bp->b_ioflags |= BIO_ERROR;
1855 if ((bp->b_flags & B_CLUSTER) != 0) {
1856 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1857 b_cluster.cluster_entry) {
1858 if (!LIST_EMPTY(&tbp->b_dep))
1862 if (!LIST_EMPTY(&bp->b_dep))
1868 g_vfs_strategy(bo, bp);
1874 db_print_ffs(struct ufsmount *ump)
1876 db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
1878 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
1879 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
1880 ump->softdep_on_worklist_inprogress, ump->softdep_deps,
1884 DB_SHOW_COMMAND(ffs, db_show_ffs)
1887 struct ufsmount *ump;
1890 ump = VFSTOUFS((struct mount *)addr);
1895 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1896 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
1897 db_print_ffs(VFSTOUFS(mp));