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)) {
193 * Flush any dirty data and suspend filesystem.
195 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
198 vn_finished_write(mp);
199 if ((error = vfs_write_suspend(mp)) != 0)
202 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
204 * Allow the secondary writes
207 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
209 wakeup(&mp->mnt_flag);
212 * Allow the curthread to
213 * ignore the suspension to
214 * synchronize on-disk state.
216 curthread->td_pflags |= TDP_IGNSUSP;
220 vn_start_write(NULL, &mp, V_WAIT);
223 * Check for and optionally get rid of files open
227 if (mp->mnt_flag & MNT_FORCE)
229 if (mp->mnt_flag & MNT_SOFTDEP) {
230 error = softdep_flushfiles(mp, flags, td);
232 error = ffs_flushfiles(mp, flags, td);
235 vfs_write_resume(mp);
238 if (fs->fs_pendingblocks != 0 ||
239 fs->fs_pendinginodes != 0) {
240 printf("%s: %s: blocks %jd files %d\n",
241 fs->fs_fsmnt, "update error",
242 (intmax_t)fs->fs_pendingblocks,
243 fs->fs_pendinginodes);
244 fs->fs_pendingblocks = 0;
245 fs->fs_pendinginodes = 0;
247 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
249 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
252 vfs_write_resume(mp);
257 g_access(ump->um_cp, 0, -1, 0);
262 mp->mnt_flag |= MNT_RDONLY;
265 * Allow the writers to note that filesystem
268 vfs_write_resume(mp);
270 if ((mp->mnt_flag & MNT_RELOAD) &&
271 (error = ffs_reload(mp, td)) != 0)
274 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
276 * If upgrade to read-write by non-root, then verify
277 * that user has necessary permissions on the device.
279 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
280 error = VOP_ACCESS(devvp, VREAD | VWRITE,
283 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
285 VOP_UNLOCK(devvp, 0, td);
288 VOP_UNLOCK(devvp, 0, td);
289 fs->fs_flags &= ~FS_UNCLEAN;
290 if (fs->fs_clean == 0) {
291 fs->fs_flags |= FS_UNCLEAN;
292 if ((mp->mnt_flag & MNT_FORCE) ||
293 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
294 (fs->fs_flags & FS_DOSOFTDEP))) {
295 printf("WARNING: %s was not %s\n",
296 fs->fs_fsmnt, "properly dismounted");
299 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
307 * If we're the root device, we may not have an E count
310 if (ump->um_cp->ace == 0)
311 error = g_access(ump->um_cp, 0, 1, 1);
313 error = g_access(ump->um_cp, 0, 1, 0);
318 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
322 mp->mnt_flag &= ~MNT_RDONLY;
325 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
326 vn_finished_write(mp);
329 /* check to see if we need to start softdep */
330 if ((fs->fs_flags & FS_DOSOFTDEP) &&
331 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
332 vn_finished_write(mp);
335 if (fs->fs_snapinum[0] != 0)
336 ffs_snapshot_mount(mp);
337 vn_finished_write(mp);
340 * Soft updates is incompatible with "async",
341 * so if we are doing softupdates stop the user
342 * from setting the async flag in an update.
343 * Softdep_mount() clears it in an initial mount
346 if (mp->mnt_flag & MNT_SOFTDEP) {
347 /* XXX: Reset too late ? */
349 mp->mnt_flag &= ~MNT_ASYNC;
353 * Keep MNT_ACLS flag if it is stored in superblock.
355 if ((fs->fs_flags & FS_ACLS) != 0) {
356 /* XXX: Set too late ? */
358 mp->mnt_flag |= MNT_ACLS;
363 * If this is a snapshot request, take the snapshot.
365 if (mp->mnt_flag & MNT_SNAPSHOT)
366 return (ffs_snapshot(mp, fspec));
370 * Not an update, or updating the name: look up the name
371 * and verify that it refers to a sensible disk device.
373 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
374 if ((error = namei(&ndp)) != 0)
376 NDFREE(&ndp, NDF_ONLY_PNBUF);
378 if (!vn_isdisk(devvp, &error)) {
384 * If mount by non-root, then verify that user has necessary
385 * permissions on the device.
388 if ((mp->mnt_flag & MNT_RDONLY) == 0)
389 accessmode |= VWRITE;
390 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
392 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
398 if (mp->mnt_flag & MNT_UPDATE) {
402 * If it's not the same vnode, or at least the same device
403 * then it's not correct.
406 if (devvp->v_rdev != ump->um_devvp->v_rdev)
407 error = EINVAL; /* needs translation */
415 * We need the name for the mount point (also used for
416 * "last mounted on") copied in. If an error occurs,
417 * the mount point is discarded by the upper level code.
418 * Note that vfs_mount() populates f_mntonname for us.
420 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
425 vfs_mountedfrom(mp, fspec);
430 * Compatibility with old mount system call.
434 ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
436 struct ufs_args args;
441 error = copyin(data, &args, sizeof args);
445 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
446 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
447 error = kernel_mount(ma, flags);
453 * Reload all incore data for a filesystem (used after running fsck on
454 * the root filesystem and finding things to fix). The filesystem must
455 * be mounted read-only.
457 * Things to do to update the mount:
458 * 1) invalidate all cached meta-data.
459 * 2) re-read superblock from disk.
460 * 3) re-read summary information from disk.
461 * 4) invalidate all inactive vnodes.
462 * 5) invalidate all cached file data.
463 * 6) re-read inode data for all active vnodes.
466 ffs_reload(struct mount *mp, struct thread *td)
468 struct vnode *vp, *mvp, *devvp;
472 struct fs *fs, *newfs;
473 struct ufsmount *ump;
474 ufs2_daddr_t sblockloc;
475 int i, blks, size, error;
478 if ((mp->mnt_flag & MNT_RDONLY) == 0)
482 * Step 1: invalidate all cached meta-data.
484 devvp = VFSTOUFS(mp)->um_devvp;
485 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
486 if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
487 panic("ffs_reload: dirty1");
488 VOP_UNLOCK(devvp, 0, td);
491 * Step 2: re-read superblock from disk.
493 fs = VFSTOUFS(mp)->um_fs;
494 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
497 newfs = (struct fs *)bp->b_data;
498 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
499 newfs->fs_magic != FS_UFS2_MAGIC) ||
500 newfs->fs_bsize > MAXBSIZE ||
501 newfs->fs_bsize < sizeof(struct fs)) {
503 return (EIO); /* XXX needs translation */
506 * Copy pointer fields back into superblock before copying in XXX
507 * new superblock. These should really be in the ufsmount. XXX
508 * Note that important parameters (eg fs_ncg) are unchanged.
510 newfs->fs_csp = fs->fs_csp;
511 newfs->fs_maxcluster = fs->fs_maxcluster;
512 newfs->fs_contigdirs = fs->fs_contigdirs;
513 newfs->fs_active = fs->fs_active;
514 /* The file system is still read-only. */
516 sblockloc = fs->fs_sblockloc;
517 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
519 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
520 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
522 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
523 printf("%s: reload pending error: blocks %jd files %d\n",
524 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
525 fs->fs_pendinginodes);
526 fs->fs_pendingblocks = 0;
527 fs->fs_pendinginodes = 0;
532 * Step 3: re-read summary information from disk.
534 blks = howmany(fs->fs_cssize, fs->fs_fsize);
536 for (i = 0; i < blks; i += fs->fs_frag) {
538 if (i + fs->fs_frag > blks)
539 size = (blks - i) * fs->fs_fsize;
540 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
544 bcopy(bp->b_data, space, (u_int)size);
545 space = (char *)space + size;
549 * We no longer know anything about clusters per cylinder group.
551 if (fs->fs_contigsumsize > 0) {
552 lp = fs->fs_maxcluster;
553 for (i = 0; i < fs->fs_ncg; i++)
554 *lp++ = fs->fs_contigsumsize;
559 MNT_VNODE_FOREACH(vp, mp, mvp) {
561 if (vp->v_iflag & VI_DOOMED) {
567 * Step 4: invalidate all cached file data.
569 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
570 MNT_VNODE_FOREACH_ABORT(mp, mvp);
573 if (vinvalbuf(vp, 0, td, 0, 0))
574 panic("ffs_reload: dirty2");
576 * Step 5: re-read inode data for all active vnodes.
580 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
581 (int)fs->fs_bsize, NOCRED, &bp);
583 VOP_UNLOCK(vp, 0, td);
585 MNT_VNODE_FOREACH_ABORT(mp, mvp);
588 ffs_load_inode(bp, ip, fs, ip->i_number);
589 ip->i_effnlink = ip->i_nlink;
591 VOP_UNLOCK(vp, 0, td);
600 * Possible superblock locations ordered from most to least likely.
602 static int sblock_try[] = SBLOCKSEARCH;
605 * Common code for mount and mountroot
608 ffs_mountfs(devvp, mp, td)
613 struct ufsmount *ump;
618 ufs2_daddr_t sblockloc;
619 int error, i, blks, size, ronly;
622 struct g_consumer *cp;
627 cred = td ? td->td_ucred : NOCRED;
628 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
634 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
637 * If we are a root mount, drop the E flag so fsck can do its magic.
638 * We will pick it up again when we remount R/W.
640 if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
641 error = g_access(cp, 0, 0, -1);
644 VOP_UNLOCK(devvp, 0, td);
647 if (devvp->v_rdev->si_iosize_max != 0)
648 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
649 if (mp->mnt_iosize_max > MAXPHYS)
650 mp->mnt_iosize_max = MAXPHYS;
652 devvp->v_bufobj.bo_private = cp;
653 devvp->v_bufobj.bo_ops = &ffs_ops;
658 * Try reading the superblock in each of its possible locations.
660 for (i = 0; sblock_try[i] != -1; i++) {
661 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
664 "Invalid sectorsize %d for superblock size %d",
665 cp->provider->sectorsize, SBLOCKSIZE);
668 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
671 fs = (struct fs *)bp->b_data;
672 sblockloc = sblock_try[i];
673 if ((fs->fs_magic == FS_UFS1_MAGIC ||
674 (fs->fs_magic == FS_UFS2_MAGIC &&
675 (fs->fs_sblockloc == sblockloc ||
676 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
677 fs->fs_bsize <= MAXBSIZE &&
678 fs->fs_bsize >= sizeof(struct fs))
683 if (sblock_try[i] == -1) {
684 error = EINVAL; /* XXX needs translation */
688 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
689 fs->fs_flags &= ~FS_UNCLEAN;
690 if (fs->fs_clean == 0) {
691 fs->fs_flags |= FS_UNCLEAN;
692 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
693 ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
694 (fs->fs_flags & FS_DOSOFTDEP))) {
696 "WARNING: %s was not properly dismounted\n",
700 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
705 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
706 (mp->mnt_flag & MNT_FORCE)) {
707 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
708 (intmax_t)fs->fs_pendingblocks,
709 fs->fs_pendinginodes);
710 fs->fs_pendingblocks = 0;
711 fs->fs_pendinginodes = 0;
714 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
715 printf("%s: mount pending error: blocks %jd files %d\n",
716 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
717 fs->fs_pendinginodes);
718 fs->fs_pendingblocks = 0;
719 fs->fs_pendinginodes = 0;
721 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
724 * Get journal provider name.
727 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
728 if (g_io_getattr("GJOURNAL::provider", cp, &size,
729 mp->mnt_gjprovider) == 0) {
730 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
733 mp->mnt_flag |= MNT_GJOURNAL;
737 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
738 mp->mnt_stat.f_mntonname);
739 free(mp->mnt_gjprovider, M_UFSMNT);
740 mp->mnt_gjprovider = NULL;
744 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
745 mp->mnt_stat.f_mntonname);
748 mp->mnt_gjprovider = NULL;
750 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
752 ump->um_bo = &devvp->v_bufobj;
753 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
754 if (fs->fs_magic == FS_UFS1_MAGIC) {
755 ump->um_fstype = UFS1;
756 ump->um_balloc = ffs_balloc_ufs1;
758 ump->um_fstype = UFS2;
759 ump->um_balloc = ffs_balloc_ufs2;
761 ump->um_blkatoff = ffs_blkatoff;
762 ump->um_truncate = ffs_truncate;
763 ump->um_update = ffs_update;
764 ump->um_valloc = ffs_valloc;
765 ump->um_vfree = ffs_vfree;
766 ump->um_ifree = ffs_ifree;
767 ump->um_rdonly = ffs_rdonly;
768 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
769 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
770 if (fs->fs_sbsize < SBLOCKSIZE)
771 bp->b_flags |= B_INVAL | B_NOCACHE;
775 ffs_oldfscompat_read(fs, ump, sblockloc);
776 fs->fs_ronly = ronly;
777 size = fs->fs_cssize;
778 blks = howmany(size, fs->fs_fsize);
779 if (fs->fs_contigsumsize > 0)
780 size += fs->fs_ncg * sizeof(int32_t);
781 size += fs->fs_ncg * sizeof(u_int8_t);
782 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
784 for (i = 0; i < blks; i += fs->fs_frag) {
786 if (i + fs->fs_frag > blks)
787 size = (blks - i) * fs->fs_fsize;
788 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
790 free(fs->fs_csp, M_UFSMNT);
793 bcopy(bp->b_data, space, (u_int)size);
794 space = (char *)space + size;
798 if (fs->fs_contigsumsize > 0) {
799 fs->fs_maxcluster = lp = space;
800 for (i = 0; i < fs->fs_ncg; i++)
801 *lp++ = fs->fs_contigsumsize;
804 size = fs->fs_ncg * sizeof(u_int8_t);
805 fs->fs_contigdirs = (u_int8_t *)space;
806 bzero(fs->fs_contigdirs, size);
807 fs->fs_active = NULL;
808 mp->mnt_data = (qaddr_t)ump;
809 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
810 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
812 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
813 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
818 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
820 mp->mnt_flag |= MNT_LOCAL;
822 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
825 mp->mnt_flag |= MNT_MULTILABEL;
829 "WARNING: %s: multilabel flag on fs but no MAC support\n",
830 mp->mnt_stat.f_mntonname);
833 if ((fs->fs_flags & FS_ACLS) != 0) {
836 mp->mnt_flag |= MNT_ACLS;
840 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
841 mp->mnt_stat.f_mntonname);
846 ump->um_devvp = devvp;
847 ump->um_nindir = fs->fs_nindir;
848 ump->um_bptrtodb = fs->fs_fsbtodb;
849 ump->um_seqinc = fs->fs_frag;
850 for (i = 0; i < MAXQUOTAS; i++)
851 ump->um_quotas[i] = NULLVP;
853 ufs_extattr_uepm_init(&ump->um_extattr);
856 * Set FS local "last mounted on" information (NULL pad)
858 bzero(fs->fs_fsmnt, MAXMNTLEN);
859 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
861 if( mp->mnt_flag & MNT_ROOTFS) {
863 * Root mount; update timestamp in mount structure.
864 * this will be used by the common root mount code
865 * to update the system clock.
867 mp->mnt_time = fs->fs_time;
871 if ((fs->fs_flags & FS_DOSOFTDEP) &&
872 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
873 free(fs->fs_csp, M_UFSMNT);
876 if (fs->fs_snapinum[0] != 0)
877 ffs_snapshot_mount(mp);
880 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
883 * Initialize filesystem stat information in mount struct.
886 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED;
889 #ifdef UFS_EXTATTR_AUTOSTART
892 * Auto-starting does the following:
893 * - check for /.attribute in the fs, and extattr_start if so
894 * - for each file in .attribute, enable that file with
895 * an attribute of the same name.
896 * Not clear how to report errors -- probably eat them.
897 * This would all happen while the filesystem was busy/not
898 * available, so would effectively be "atomic".
900 mp->mnt_stat.f_iosize = fs->fs_bsize;
901 (void) ufs_extattr_autostart(mp, td);
902 #endif /* !UFS_EXTATTR_AUTOSTART */
903 #endif /* !UFS_EXTATTR */
916 mtx_destroy(UFS_MTX(ump));
917 if (mp->mnt_gjprovider != NULL) {
918 free(mp->mnt_gjprovider, M_UFSMNT);
919 mp->mnt_gjprovider = NULL;
921 free(ump->um_fs, M_UFSMNT);
923 mp->mnt_data = (qaddr_t)0;
929 #include <sys/sysctl.h>
930 static int bigcgs = 0;
931 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
934 * Sanity checks for loading old filesystem superblocks.
935 * See ffs_oldfscompat_write below for unwound actions.
937 * XXX - Parts get retired eventually.
938 * Unfortunately new bits get added.
941 ffs_oldfscompat_read(fs, ump, sblockloc)
943 struct ufsmount *ump;
944 ufs2_daddr_t sblockloc;
949 * If not yet done, update fs_flags location and value of fs_sblockloc.
951 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
952 fs->fs_flags = fs->fs_old_flags;
953 fs->fs_old_flags |= FS_FLAGS_UPDATED;
954 fs->fs_sblockloc = sblockloc;
957 * If not yet done, update UFS1 superblock with new wider fields.
959 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
960 fs->fs_maxbsize = fs->fs_bsize;
961 fs->fs_time = fs->fs_old_time;
962 fs->fs_size = fs->fs_old_size;
963 fs->fs_dsize = fs->fs_old_dsize;
964 fs->fs_csaddr = fs->fs_old_csaddr;
965 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
966 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
967 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
968 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
970 if (fs->fs_magic == FS_UFS1_MAGIC &&
971 fs->fs_old_inodefmt < FS_44INODEFMT) {
972 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
973 fs->fs_qbmask = ~fs->fs_bmask;
974 fs->fs_qfmask = ~fs->fs_fmask;
976 if (fs->fs_magic == FS_UFS1_MAGIC) {
977 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
978 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
979 if (fs->fs_maxfilesize > maxfilesize)
980 fs->fs_maxfilesize = maxfilesize;
982 /* Compatibility for old filesystems */
983 if (fs->fs_avgfilesize <= 0)
984 fs->fs_avgfilesize = AVFILESIZ;
985 if (fs->fs_avgfpdir <= 0)
986 fs->fs_avgfpdir = AFPDIR;
988 fs->fs_save_cgsize = fs->fs_cgsize;
989 fs->fs_cgsize = fs->fs_bsize;
994 * Unwinding superblock updates for old filesystems.
995 * See ffs_oldfscompat_read above for details.
997 * XXX - Parts get retired eventually.
998 * Unfortunately new bits get added.
1001 ffs_oldfscompat_write(fs, ump)
1003 struct ufsmount *ump;
1007 * Copy back UFS2 updated fields that UFS1 inspects.
1009 if (fs->fs_magic == FS_UFS1_MAGIC) {
1010 fs->fs_old_time = fs->fs_time;
1011 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1012 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1013 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1014 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1015 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1018 fs->fs_cgsize = fs->fs_save_cgsize;
1019 fs->fs_save_cgsize = 0;
1024 * unmount system call
1027 ffs_unmount(mp, mntflags, td)
1032 struct ufsmount *ump = VFSTOUFS(mp);
1034 int error, flags, susp;
1041 if (mntflags & MNT_FORCE) {
1042 flags |= FORCECLOSE;
1043 susp = fs->fs_ronly != 0;
1047 if ((error = ufs_extattr_stop(mp, td))) {
1048 if (error != EOPNOTSUPP)
1049 printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1053 ufs_extattr_uepm_destroy(&ump->um_extattr);
1059 * dounmount already called vn_start_write().
1062 vn_finished_write(mp);
1063 if ((error = vfs_write_suspend(mp)) != 0)
1066 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1067 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1069 wakeup(&mp->mnt_flag);
1071 curthread->td_pflags |= TDP_IGNSUSP;
1075 vn_start_write(NULL, &mp, V_WAIT);
1078 if (mp->mnt_flag & MNT_SOFTDEP) {
1079 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
1082 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
1086 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1087 printf("%s: unmount pending error: blocks %jd files %d\n",
1088 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1089 fs->fs_pendinginodes);
1090 fs->fs_pendingblocks = 0;
1091 fs->fs_pendinginodes = 0;
1094 if (fs->fs_ronly == 0) {
1095 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1096 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1103 vfs_write_resume(mp);
1104 vn_start_write(NULL, &mp, V_WAIT);
1108 g_vfs_close(ump->um_cp, td);
1109 g_topology_unlock();
1111 vrele(ump->um_devvp);
1112 dev_rel(ump->um_dev);
1113 mtx_destroy(UFS_MTX(ump));
1114 if (mp->mnt_gjprovider != NULL) {
1115 free(mp->mnt_gjprovider, M_UFSMNT);
1116 mp->mnt_gjprovider = NULL;
1118 free(fs->fs_csp, M_UFSMNT);
1120 free(ump, M_UFSMNT);
1121 mp->mnt_data = (qaddr_t)0;
1123 mp->mnt_flag &= ~MNT_LOCAL;
1129 vfs_write_resume(mp);
1130 vn_start_write(NULL, &mp, V_WAIT);
1134 ufs_extattr_uepm_init(&ump->um_extattr);
1135 #ifdef UFS_EXTATTR_AUTOSTART
1136 (void) ufs_extattr_autostart(mp, td);
1145 * Flush out all the files in a filesystem.
1148 ffs_flushfiles(mp, flags, td)
1153 struct ufsmount *ump;
1158 if (mp->mnt_flag & MNT_QUOTA) {
1160 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1163 for (i = 0; i < MAXQUOTAS; i++) {
1164 quotaoff(td, mp, i);
1167 * Here we fall through to vflush again to ensure
1168 * that we have gotten rid of all the system vnodes.
1172 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1173 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1174 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1176 ffs_snapshot_unmount(mp);
1177 flags |= FORCECLOSE;
1179 * Here we fall through to vflush again to ensure
1180 * that we have gotten rid of all the system vnodes.
1184 * Flush all the files.
1186 if ((error = vflush(mp, 0, flags, td)) != 0)
1189 * Flush filesystem metadata.
1191 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
1192 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1193 VOP_UNLOCK(ump->um_devvp, 0, td);
1198 * Get filesystem statistics.
1201 ffs_statfs(mp, sbp, td)
1206 struct ufsmount *ump;
1211 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1212 panic("ffs_statfs");
1213 sbp->f_version = STATFS_VERSION;
1214 sbp->f_bsize = fs->fs_fsize;
1215 sbp->f_iosize = fs->fs_bsize;
1216 sbp->f_blocks = fs->fs_dsize;
1218 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1219 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1220 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1221 dbtofsb(fs, fs->fs_pendingblocks);
1222 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1223 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1225 sbp->f_namemax = NAME_MAX;
1230 * Go through the disk queues to initiate sandbagged IO;
1231 * go through the inodes to write those that have been modified;
1232 * initiate the writing of the super block if it has been modified.
1234 * Note: we are always called with the filesystem marked `MPBUSY'.
1237 ffs_sync(mp, waitfor, td)
1242 struct vnode *mvp, *vp, *devvp;
1244 struct ufsmount *ump = VFSTOUFS(mp);
1246 int error, count, wait, lockreq, allerror = 0;
1249 int secondary_writes;
1250 int secondary_accwrites;
1252 int softdep_accdeps;
1256 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1257 printf("fs = %s\n", fs->fs_fsmnt);
1258 panic("ffs_sync: rofs mod");
1261 * Write back each (modified) inode.
1266 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1267 if (waitfor == MNT_SUSPEND) {
1271 if (waitfor == MNT_WAIT) {
1273 lockreq = LK_EXCLUSIVE;
1275 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1278 /* Grab snapshot of secondary write counts */
1279 secondary_writes = mp->mnt_secondary_writes;
1280 secondary_accwrites = mp->mnt_secondary_accwrites;
1282 /* Grab snapshot of softdep dependency counts */
1284 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1287 MNT_VNODE_FOREACH(vp, mp, mvp) {
1289 * Depend on the mntvnode_slock to keep things stable enough
1290 * for a quick test. Since there might be hundreds of
1291 * thousands of vnodes, we cannot afford even a subroutine
1292 * call unless there's a good chance that we have work to do.
1295 if (vp->v_iflag & VI_DOOMED) {
1300 if (vp->v_type == VNON || ((ip->i_flag &
1301 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1302 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1307 if ((error = vget(vp, lockreq, td)) != 0) {
1309 if (error == ENOENT || error == ENOLCK) {
1310 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1315 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1322 * Force stale filesystem control information to be flushed.
1324 if (waitfor == MNT_WAIT) {
1325 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1327 /* Flushed work items may create new vnodes to clean */
1328 if (allerror == 0 && count) {
1336 devvp = ump->um_devvp;
1338 bo = &devvp->v_bufobj;
1339 if (waitfor != MNT_LAZY &&
1340 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1341 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
1342 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1344 VOP_UNLOCK(devvp, 0, td);
1345 if (allerror == 0 && waitfor == MNT_WAIT) {
1349 } else if (suspend != 0) {
1350 if (softdep_check_suspend(mp,
1355 secondary_accwrites) != 0)
1356 goto loop; /* More work needed */
1357 mtx_assert(MNT_MTX(mp), MA_OWNED);
1358 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1364 * Write back modified superblock.
1366 if (fs->fs_fmod != 0 &&
1367 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1373 ffs_vget(mp, ino, flags, vpp)
1379 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1383 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1392 struct ufsmount *ump;
1399 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1400 if (error || *vpp != NULL)
1404 * We must promote to an exclusive lock for vnode creation. This
1405 * can happen if lookup is passed LOCKSHARED.
1407 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1408 flags &= ~LK_TYPE_MASK;
1409 flags |= LK_EXCLUSIVE;
1413 * We do not lock vnode creation as it is believed to be too
1414 * expensive for such rare case as simultaneous creation of vnode
1415 * for same ino by different processes. We just allow them to race
1416 * and check later to decide who wins. Let the race begin!
1424 * If this MALLOC() is performed after the getnewvnode()
1425 * it might block, leaving a vnode with a NULL v_data to be
1426 * found by ffs_sync() if a sync happens to fire right then,
1427 * which will cause a panic because ffs_sync() blindly
1428 * dereferences vp->v_data (as well it should).
1430 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1432 /* Allocate a new vnode/inode. */
1433 if (fs->fs_magic == FS_UFS1_MAGIC)
1434 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1436 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1439 uma_zfree(uma_inode, ip);
1443 * FFS supports recursive and shared locking.
1445 vp->v_vnlock->lk_flags |= LK_CANRECURSE;
1446 vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
1448 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1458 for (i = 0; i < MAXQUOTAS; i++)
1459 ip->i_dquot[i] = NODQUOT;
1464 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td);
1465 if (ffs_flags & FFSV_FORCEINSMQ)
1466 vp->v_vflag |= VV_FORCEINSMQ;
1467 error = insmntque(vp, mp);
1472 vp->v_vflag &= ~VV_FORCEINSMQ;
1473 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1474 if (error || *vpp != NULL)
1477 /* Read in the disk contents for the inode, copy into the inode. */
1478 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1479 (int)fs->fs_bsize, NOCRED, &bp);
1482 * The inode does not contain anything useful, so it would
1483 * be misleading to leave it on its hash chain. With mode
1484 * still zero, it will be unlinked and returned to the free
1492 if (ip->i_ump->um_fstype == UFS1)
1493 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1495 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1496 ffs_load_inode(bp, ip, fs, ino);
1497 if (DOINGSOFTDEP(vp))
1498 softdep_load_inodeblock(ip);
1500 ip->i_effnlink = ip->i_nlink;
1504 * Initialize the vnode from the inode, check for aliases.
1505 * Note that the underlying vnode may have changed.
1507 if (ip->i_ump->um_fstype == UFS1)
1508 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1510 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1518 * Finish inode initialization.
1522 * Set up a generation number for this inode if it does not
1523 * already have one. This should only happen on old filesystems.
1525 if (ip->i_gen == 0) {
1526 ip->i_gen = arc4random() / 2 + 1;
1527 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1528 ip->i_flag |= IN_MODIFIED;
1529 DIP_SET(ip, i_gen, ip->i_gen);
1533 * Ensure that uid and gid are correct. This is a temporary
1534 * fix until fsck has been changed to do the update.
1536 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
1537 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1538 ip->i_uid = ip->i_din1->di_ouid; /* XXX */
1539 ip->i_gid = ip->i_din1->di_ogid; /* XXX */
1543 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1545 * If this vnode is already allocated, and we're running
1546 * multi-label, attempt to perform a label association
1547 * from the extended attributes on the inode.
1549 error = mac_associate_vnode_extattr(mp, vp);
1551 /* ufs_inactive will release ip->i_devvp ref. */
1564 * File handle to vnode
1566 * Have to be really careful about stale file handles:
1567 * - check that the inode number is valid
1568 * - call ffs_vget() to get the locked inode
1569 * - check for an unallocated inode (i_mode == 0)
1570 * - check that the given client host has export rights and return
1571 * those rights via. exflagsp and credanonp
1574 ffs_fhtovp(mp, fhp, vpp)
1582 ufhp = (struct ufid *)fhp;
1583 fs = VFSTOUFS(mp)->um_fs;
1584 if (ufhp->ufid_ino < ROOTINO ||
1585 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1587 return (ufs_fhtovp(mp, ufhp, vpp));
1591 * Initialize the filesystem.
1595 struct vfsconf *vfsp;
1598 softdep_initialize();
1599 return (ufs_init(vfsp));
1603 * Undo the work of ffs_init().
1607 struct vfsconf *vfsp;
1611 ret = ufs_uninit(vfsp);
1612 softdep_uninitialize();
1617 * Write a superblock and associated information back to disk.
1620 ffs_sbupdate(mp, waitfor, suspended)
1621 struct ufsmount *mp;
1625 struct fs *fs = mp->um_fs;
1630 int i, size, error, allerror = 0;
1632 if (fs->fs_ronly == 1 &&
1633 (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1634 (MNT_RDONLY | MNT_UPDATE))
1635 panic("ffs_sbupdate: write read-only filesystem");
1637 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1639 sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1642 * First write back the summary information.
1644 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1646 for (i = 0; i < blks; i += fs->fs_frag) {
1647 size = fs->fs_bsize;
1648 if (i + fs->fs_frag > blks)
1649 size = (blks - i) * fs->fs_fsize;
1650 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1652 bcopy(space, bp->b_data, (u_int)size);
1653 space = (char *)space + size;
1655 bp->b_flags |= B_VALIDSUSPWRT;
1656 if (waitfor != MNT_WAIT)
1658 else if ((error = bwrite(bp)) != 0)
1662 * Now write back the superblock itself. If any errors occurred
1663 * up to this point, then fail so that the superblock avoids
1664 * being written out as clean.
1671 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1672 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1673 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1674 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1675 fs->fs_sblockloc = SBLOCK_UFS1;
1677 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1678 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1679 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1680 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1681 fs->fs_sblockloc = SBLOCK_UFS2;
1684 fs->fs_time = time_second;
1685 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1686 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1688 bp->b_flags |= B_VALIDSUSPWRT;
1689 if (waitfor != MNT_WAIT)
1691 else if ((error = bwrite(bp)) != 0)
1697 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1698 int attrnamespace, const char *attrname, struct thread *td)
1702 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1705 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1711 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1714 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1715 uma_zfree(uma_ufs1, ip->i_din1);
1716 else if (ip->i_din2 != NULL)
1717 uma_zfree(uma_ufs2, ip->i_din2);
1718 uma_zfree(uma_inode, ip);
1721 static int dobkgrdwrite = 1;
1722 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1723 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1726 * Complete a background write started from bwrite.
1729 ffs_backgroundwritedone(struct buf *bp)
1731 struct bufobj *bufobj;
1735 * Find the original buffer that we are writing.
1737 bufobj = bp->b_bufobj;
1739 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1740 panic("backgroundwritedone: lost buffer");
1741 /* Grab an extra reference to be dropped by the bufdone() below. */
1742 bufobj_wrefl(bufobj);
1745 * Process dependencies then return any unfinished ones.
1747 if (!LIST_EMPTY(&bp->b_dep))
1750 if (!LIST_EMPTY(&bp->b_dep))
1751 softdep_move_dependencies(bp, origbp);
1754 * This buffer is marked B_NOCACHE so when it is released
1755 * by biodone it will be tossed.
1757 bp->b_flags |= B_NOCACHE;
1758 bp->b_flags &= ~B_CACHE;
1762 * Clear the BV_BKGRDINPROG flag in the original buffer
1763 * and awaken it if it is waiting for the write to complete.
1764 * If BV_BKGRDINPROG is not set in the original buffer it must
1765 * have been released and re-instantiated - which is not legal.
1767 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1768 ("backgroundwritedone: lost buffer2"));
1769 origbp->b_vflags &= ~BV_BKGRDINPROG;
1770 if (origbp->b_vflags & BV_BKGRDWAIT) {
1771 origbp->b_vflags &= ~BV_BKGRDWAIT;
1772 wakeup(&origbp->b_xflags);
1779 * Write, release buffer on completion. (Done by iodone
1780 * if async). Do not bother writing anything if the buffer
1783 * Note that we set B_CACHE here, indicating that buffer is
1784 * fully valid and thus cacheable. This is true even of NFS
1785 * now so we set it generally. This could be set either here
1786 * or in biodone() since the I/O is synchronous. We put it
1790 ffs_bufwrite(struct buf *bp)
1795 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1796 if (bp->b_flags & B_INVAL) {
1801 oldflags = bp->b_flags;
1803 if (BUF_REFCNT(bp) == 0)
1804 panic("bufwrite: buffer is not busy???");
1807 * If a background write is already in progress, delay
1808 * writing this block if it is asynchronous. Otherwise
1809 * wait for the background write to complete.
1811 BO_LOCK(bp->b_bufobj);
1812 if (bp->b_vflags & BV_BKGRDINPROG) {
1813 if (bp->b_flags & B_ASYNC) {
1814 BO_UNLOCK(bp->b_bufobj);
1819 bp->b_vflags |= BV_BKGRDWAIT;
1820 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1821 if (bp->b_vflags & BV_BKGRDINPROG)
1822 panic("bufwrite: still writing");
1824 BO_UNLOCK(bp->b_bufobj);
1826 /* Mark the buffer clean */
1830 * If this buffer is marked for background writing and we
1831 * do not have to wait for it, make a copy and write the
1832 * copy so as to leave this buffer ready for further use.
1834 * This optimization eats a lot of memory. If we have a page
1835 * or buffer shortfall we can't do it.
1837 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1838 (bp->b_flags & B_ASYNC) &&
1839 !vm_page_count_severe() &&
1840 !buf_dirty_count_severe()) {
1841 KASSERT(bp->b_iodone == NULL,
1842 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1844 /* get a new block */
1845 newbp = geteblk(bp->b_bufsize);
1848 * set it to be identical to the old block. We have to
1849 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1850 * to avoid confusing the splay tree and gbincore().
1852 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1853 newbp->b_lblkno = bp->b_lblkno;
1854 newbp->b_xflags |= BX_BKGRDMARKER;
1855 BO_LOCK(bp->b_bufobj);
1856 bp->b_vflags |= BV_BKGRDINPROG;
1857 bgetvp(bp->b_vp, newbp);
1858 BO_UNLOCK(bp->b_bufobj);
1859 newbp->b_bufobj = &bp->b_vp->v_bufobj;
1860 newbp->b_blkno = bp->b_blkno;
1861 newbp->b_offset = bp->b_offset;
1862 newbp->b_iodone = ffs_backgroundwritedone;
1863 newbp->b_flags |= B_ASYNC;
1864 newbp->b_flags &= ~B_INVAL;
1867 /* move over the dependencies */
1868 if (!LIST_EMPTY(&bp->b_dep))
1869 softdep_move_dependencies(bp, newbp);
1873 * Initiate write on the copy, release the original to
1874 * the B_LOCKED queue so that it cannot go away until
1875 * the background write completes. If not locked it could go
1876 * away and then be reconstituted while it was being written.
1877 * If the reconstituted buffer were written, we could end up
1878 * with two background copies being written at the same time.
1884 /* Let the normal bufwrite do the rest for us */
1885 return (bufwrite(bp));
1890 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1896 vp = bo->__bo_vnode;
1897 if (bp->b_iocmd == BIO_WRITE) {
1898 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1899 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1900 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1901 panic("ffs_geom_strategy: bad I/O");
1902 bp->b_flags &= ~B_VALIDSUSPWRT;
1903 if ((vp->v_vflag & VV_COPYONWRITE) &&
1904 vp->v_rdev->si_snapdata != NULL) {
1905 if ((bp->b_flags & B_CLUSTER) != 0) {
1906 runningbufwakeup(bp);
1907 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1908 b_cluster.cluster_entry) {
1909 error = ffs_copyonwrite(vp, tbp);
1911 error != EOPNOTSUPP) {
1912 bp->b_error = error;
1913 bp->b_ioflags |= BIO_ERROR;
1918 bp->b_runningbufspace = bp->b_bufsize;
1919 atomic_add_int(&runningbufspace,
1920 bp->b_runningbufspace);
1922 error = ffs_copyonwrite(vp, bp);
1923 if (error != 0 && error != EOPNOTSUPP) {
1924 bp->b_error = error;
1925 bp->b_ioflags |= BIO_ERROR;
1932 if ((bp->b_flags & B_CLUSTER) != 0) {
1933 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
1934 b_cluster.cluster_entry) {
1935 if (!LIST_EMPTY(&tbp->b_dep))
1939 if (!LIST_EMPTY(&bp->b_dep))
1945 g_vfs_strategy(bo, bp);
1951 db_print_ffs(struct ufsmount *ump)
1953 db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
1955 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
1956 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
1957 ump->softdep_on_worklist_inprogress, ump->softdep_deps,
1961 DB_SHOW_COMMAND(ffs, db_show_ffs)
1964 struct ufsmount *ump;
1967 ump = VFSTOUFS((struct mount *)addr);
1972 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1973 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
1974 db_print_ffs(VFSTOUFS(mp));