2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_quota.h"
42 #include <sys/param.h>
43 #include <sys/gsb_crc32.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
48 #include <sys/taskqueue.h>
49 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
56 #include <sys/fcntl.h>
57 #include <sys/ioccom.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/rwlock.h>
61 #include <sys/sysctl.h>
62 #include <sys/vmmeter.h>
64 #include <security/mac/mac_framework.h>
66 #include <ufs/ufs/dir.h>
67 #include <ufs/ufs/extattr.h>
68 #include <ufs/ufs/gjournal.h>
69 #include <ufs/ufs/quota.h>
70 #include <ufs/ufs/ufsmount.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/ufs_extern.h>
74 #include <ufs/ffs/fs.h>
75 #include <ufs/ffs/ffs_extern.h>
79 #include <vm/vm_page.h>
81 #include <geom/geom.h>
82 #include <geom/geom_vfs.h>
86 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
89 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
90 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
92 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
93 static int ffs_sync_lazy(struct mount *mp);
94 static int ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
95 static int ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
97 static vfs_init_t ffs_init;
98 static vfs_uninit_t ffs_uninit;
99 static vfs_extattrctl_t ffs_extattrctl;
100 static vfs_cmount_t ffs_cmount;
101 static vfs_unmount_t ffs_unmount;
102 static vfs_mount_t ffs_mount;
103 static vfs_statfs_t ffs_statfs;
104 static vfs_fhtovp_t ffs_fhtovp;
105 static vfs_sync_t ffs_sync;
107 static struct vfsops ufs_vfsops = {
108 .vfs_extattrctl = ffs_extattrctl,
109 .vfs_fhtovp = ffs_fhtovp,
110 .vfs_init = ffs_init,
111 .vfs_mount = ffs_mount,
112 .vfs_cmount = ffs_cmount,
113 .vfs_quotactl = ufs_quotactl,
114 .vfs_root = vfs_cache_root,
115 .vfs_cachedroot = ufs_root,
116 .vfs_statfs = ffs_statfs,
117 .vfs_sync = ffs_sync,
118 .vfs_uninit = ffs_uninit,
119 .vfs_unmount = ffs_unmount,
120 .vfs_vget = ffs_vget,
121 .vfs_susp_clean = process_deferred_inactive,
124 VFS_SET(ufs_vfsops, ufs, 0);
125 MODULE_VERSION(ufs, 1);
127 static b_strategy_t ffs_geom_strategy;
128 static b_write_t ffs_bufwrite;
130 static struct buf_ops ffs_ops = {
132 .bop_write = ffs_bufwrite,
133 .bop_strategy = ffs_geom_strategy,
135 #ifdef NO_FFS_SNAPSHOT
136 .bop_bdflush = bufbdflush,
138 .bop_bdflush = ffs_bdflush,
143 * Note that userquota and groupquota options are not currently used
144 * by UFS/FFS code and generally mount(8) does not pass those options
145 * from userland, but they can be passed by loader(8) via
146 * vfs.root.mountfrom.options.
148 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
149 "noclusterw", "noexec", "export", "force", "from", "groupquota",
150 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
151 "nosymfollow", "sync", "union", "userquota", "untrusted", NULL };
153 static int ffs_enxio_enable = 1;
154 SYSCTL_DECL(_vfs_ffs);
155 SYSCTL_INT(_vfs_ffs, OID_AUTO, enxio_enable, CTLFLAG_RWTUN,
156 &ffs_enxio_enable, 0,
157 "enable mapping of other disk I/O errors to ENXIO");
160 * Return buffer with the contents of block "offset" from the beginning of
161 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
162 * remaining space in the directory.
165 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
175 lbn = lblkno(fs, offset);
176 bsize = blksize(fs, ip, lbn);
179 error = bread(vp, lbn, bsize, NOCRED, &bp);
184 *res = (char *)bp->b_data + blkoff(fs, offset);
190 * Load up the contents of an inode and copy the appropriate pieces
191 * to the incore copy.
194 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
196 struct ufs1_dinode *dip1;
197 struct ufs2_dinode *dip2;
203 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
204 ip->i_mode = dip1->di_mode;
205 ip->i_nlink = dip1->di_nlink;
206 ip->i_effnlink = dip1->di_nlink;
207 ip->i_size = dip1->di_size;
208 ip->i_flags = dip1->di_flags;
209 ip->i_gen = dip1->di_gen;
210 ip->i_uid = dip1->di_uid;
211 ip->i_gid = dip1->di_gid;
214 dip2 = ((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
215 if ((error = ffs_verify_dinode_ckhash(fs, dip2)) != 0 &&
216 !ffs_fsfail_cleanup(ITOUMP(ip), error)) {
217 printf("%s: inode %jd: check-hash failed\n", fs->fs_fsmnt,
223 ip->i_mode = dip2->di_mode;
224 ip->i_nlink = dip2->di_nlink;
225 ip->i_effnlink = dip2->di_nlink;
226 ip->i_size = dip2->di_size;
227 ip->i_flags = dip2->di_flags;
228 ip->i_gen = dip2->di_gen;
229 ip->i_uid = dip2->di_uid;
230 ip->i_gid = dip2->di_gid;
235 * Verify that a filesystem block number is a valid data block.
236 * This routine is only called on untrusted filesystems.
239 ffs_check_blkno(struct mount *mp, ino_t inum, ufs2_daddr_t daddr, int blksize)
242 struct ufsmount *ump;
243 ufs2_daddr_t end_daddr;
246 KASSERT((mp->mnt_flag & MNT_UNTRUSTED) != 0,
247 ("ffs_check_blkno called on a trusted file system"));
250 cg = dtog(fs, daddr);
251 end_daddr = daddr + numfrags(fs, blksize);
253 * Verify that the block number is a valid data block. Also check
254 * that it does not point to an inode block or a superblock. Accept
255 * blocks that are unalloacted (0) or part of snapshot metadata
256 * (BLK_NOCOPY or BLK_SNAP).
258 * Thus, the block must be in a valid range for the filesystem and
259 * either in the space before a backup superblock (except the first
260 * cylinder group where that space is used by the bootstrap code) or
261 * after the inode blocks and before the end of the cylinder group.
263 if ((uint64_t)daddr <= BLK_SNAP ||
264 ((uint64_t)end_daddr <= fs->fs_size &&
265 ((cg > 0 && end_daddr <= cgsblock(fs, cg)) ||
266 (daddr >= cgdmin(fs, cg) &&
267 end_daddr <= cgbase(fs, cg) + fs->fs_fpg))))
269 if ((havemtx = mtx_owned(UFS_MTX(ump))) == 0)
271 if (ppsratecheck(&ump->um_last_integritymsg,
272 &ump->um_secs_integritymsg, 1)) {
274 uprintf("\n%s: inode %jd, out-of-range indirect block "
275 "number %jd\n", mp->mnt_stat.f_mntonname, inum, daddr);
284 * On first ENXIO error, initiate an asynchronous forcible unmount.
285 * Used to unmount filesystems whose underlying media has gone away.
287 * Return true if a cleanup is in progress.
290 ffs_fsfail_cleanup(struct ufsmount *ump, int error)
295 retval = ffs_fsfail_cleanup_locked(ump, error);
301 ffs_fsfail_cleanup_locked(struct ufsmount *ump, int error)
303 mtx_assert(UFS_MTX(ump), MA_OWNED);
304 if (error == ENXIO && (ump->um_flags & UM_FSFAIL_CLEANUP) == 0) {
305 ump->um_flags |= UM_FSFAIL_CLEANUP;
307 * Queue an async forced unmount.
309 vfs_ref(ump->um_mountp);
310 dounmount(ump->um_mountp,
311 MNT_FORCE | MNT_RECURSE | MNT_DEFERRED, curthread);
312 printf("UFS: forcibly unmounting %s from %s\n",
313 ump->um_mountp->mnt_stat.f_mntfromname,
314 ump->um_mountp->mnt_stat.f_mntonname);
316 return ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0);
320 * Wrapper used during ENXIO cleanup to allocate empty buffers when
321 * the kernel is unable to read the real one. They are needed so that
322 * the soft updates code can use them to unwind its dependencies.
325 ffs_breadz(struct ufsmount *ump, struct vnode *vp, daddr_t lblkno,
326 daddr_t dblkno, int size, daddr_t *rablkno, int *rabsize, int cnt,
327 struct ucred *cred, int flags, void (*ckhashfunc)(struct buf *),
332 flags |= GB_CVTENXIO;
333 error = breadn_flags(vp, lblkno, dblkno, size, rablkno, rabsize, cnt,
334 cred, flags, ckhashfunc, bpp);
335 if (error != 0 && ffs_fsfail_cleanup(ump, error)) {
336 error = getblkx(vp, lblkno, dblkno, size, 0, 0, flags, bpp);
337 KASSERT(error == 0, ("getblkx failed"));
338 vfs_bio_bzero_buf(*bpp, 0, size);
344 ffs_mount(struct mount *mp)
346 struct vnode *devvp, *odevvp;
348 struct ufsmount *ump = NULL;
351 int error, error1, flags;
352 uint64_t mntorflags, saved_mnt_flag;
354 struct nameidata ndp;
356 bool mounted_softdep;
359 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
361 if (uma_inode == NULL) {
362 uma_inode = uma_zcreate("FFS inode",
363 sizeof(struct inode), NULL, NULL, NULL, NULL,
365 uma_ufs1 = uma_zcreate("FFS1 dinode",
366 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
368 uma_ufs2 = uma_zcreate("FFS2 dinode",
369 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
371 VFS_SMR_ZONE_SET(uma_inode);
374 vfs_deleteopt(mp->mnt_optnew, "groupquota");
375 vfs_deleteopt(mp->mnt_optnew, "userquota");
377 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
382 if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
383 mntorflags |= MNT_UNTRUSTED;
385 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
386 mntorflags |= MNT_ACLS;
388 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
389 mntorflags |= MNT_SNAPSHOT;
391 * Once we have set the MNT_SNAPSHOT flag, do not
392 * persist "snapshot" in the options list.
394 vfs_deleteopt(mp->mnt_optnew, "snapshot");
395 vfs_deleteopt(mp->mnt_opt, "snapshot");
398 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
399 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
401 * Once we have set the restricted PID, do not
402 * persist "fsckpid" in the options list.
404 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
405 vfs_deleteopt(mp->mnt_opt, "fsckpid");
406 if (mp->mnt_flag & MNT_UPDATE) {
407 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
408 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
410 "Checker enable: Must be read-only");
413 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
415 "Checker enable: Must be read-only");
418 /* Set to -1 if we are done */
423 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
424 if (mntorflags & MNT_ACLS) {
426 "\"acls\" and \"nfsv4acls\" options "
427 "are mutually exclusive");
430 mntorflags |= MNT_NFS4ACLS;
434 mp->mnt_kern_flag &= ~MNTK_FPLOOKUP;
435 mp->mnt_flag |= mntorflags;
438 * If updating, check whether changing from read-only to
439 * read/write; if there is no device name, that's all we do.
441 if (mp->mnt_flag & MNT_UPDATE) {
444 odevvp = ump->um_odevvp;
445 devvp = ump->um_devvp;
446 if (fsckpid == -1 && ump->um_fsckpid > 0) {
447 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
448 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
452 * Return to normal read-only mode.
454 error = g_access(ump->um_cp, 0, -1, 0);
458 if (fs->fs_ronly == 0 &&
459 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
461 * Flush any dirty data and suspend filesystem.
463 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
465 error = vfs_write_suspend_umnt(mp);
470 if (MOUNTEDSOFTDEP(mp)) {
472 mp->mnt_flag &= ~MNT_SOFTDEP;
474 mounted_softdep = true;
476 mounted_softdep = false;
479 * Check for and optionally get rid of files open
483 if (mp->mnt_flag & MNT_FORCE)
485 if (mounted_softdep) {
486 error = softdep_flushfiles(mp, flags, td);
488 error = ffs_flushfiles(mp, flags, td);
492 if (mounted_softdep) {
494 mp->mnt_flag |= MNT_SOFTDEP;
497 vfs_write_resume(mp, 0);
501 if (fs->fs_pendingblocks != 0 ||
502 fs->fs_pendinginodes != 0) {
503 printf("WARNING: %s Update error: blocks %jd "
504 "files %d\n", fs->fs_fsmnt,
505 (intmax_t)fs->fs_pendingblocks,
506 fs->fs_pendinginodes);
507 fs->fs_pendingblocks = 0;
508 fs->fs_pendinginodes = 0;
510 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
512 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
515 if (mounted_softdep) {
517 mp->mnt_flag |= MNT_SOFTDEP;
520 vfs_write_resume(mp, 0);
527 * Drop our write and exclusive access.
529 g_access(ump->um_cp, 0, -1, -1);
532 mp->mnt_flag |= MNT_RDONLY;
535 * Allow the writers to note that filesystem
538 vfs_write_resume(mp, 0);
540 if ((mp->mnt_flag & MNT_RELOAD) &&
541 (error = ffs_reload(mp, td, 0)) != 0)
544 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
546 * If we are running a checker, do not allow upgrade.
548 if (ump->um_fsckpid > 0) {
550 "Active checker, cannot upgrade to write");
554 * If upgrade to read-write by non-root, then verify
555 * that user has necessary permissions on the device.
557 vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
558 error = VOP_ACCESS(odevvp, VREAD | VWRITE,
561 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
566 fs->fs_flags &= ~FS_UNCLEAN;
567 if (fs->fs_clean == 0) {
568 fs->fs_flags |= FS_UNCLEAN;
569 if ((mp->mnt_flag & MNT_FORCE) ||
571 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
572 (fs->fs_flags & FS_DOSOFTDEP))) {
573 printf("WARNING: %s was not properly "
574 "dismounted\n", fs->fs_fsmnt);
577 "R/W mount of %s denied. %s.%s",
579 "Filesystem is not clean - run fsck",
580 (fs->fs_flags & FS_SUJ) == 0 ? "" :
581 " Forced mount will invalidate"
582 " journal contents");
588 * Request exclusive write access.
590 error = g_access(ump->um_cp, 0, 1, 1);
594 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
596 error = vfs_write_suspend_umnt(mp);
601 saved_mnt_flag = MNT_RDONLY;
602 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
604 saved_mnt_flag |= MNT_ASYNC;
605 mp->mnt_flag &= ~saved_mnt_flag;
607 fs->fs_mtime = time_second;
608 /* check to see if we need to start softdep */
609 if ((fs->fs_flags & FS_DOSOFTDEP) &&
610 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
613 mp->mnt_flag |= saved_mnt_flag;
615 vfs_write_resume(mp, 0);
619 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
621 if ((fs->fs_flags & FS_DOSOFTDEP) != 0)
624 mp->mnt_flag |= saved_mnt_flag;
626 vfs_write_resume(mp, 0);
629 if (fs->fs_snapinum[0] != 0)
630 ffs_snapshot_mount(mp);
631 vfs_write_resume(mp, 0);
634 * Soft updates is incompatible with "async",
635 * so if we are doing softupdates stop the user
636 * from setting the async flag in an update.
637 * Softdep_mount() clears it in an initial mount
640 if (MOUNTEDSOFTDEP(mp)) {
641 /* XXX: Reset too late ? */
643 mp->mnt_flag &= ~MNT_ASYNC;
647 * Keep MNT_ACLS flag if it is stored in superblock.
649 if ((fs->fs_flags & FS_ACLS) != 0) {
650 /* XXX: Set too late ? */
652 mp->mnt_flag |= MNT_ACLS;
656 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
657 /* XXX: Set too late ? */
659 mp->mnt_flag |= MNT_NFS4ACLS;
663 * If this is a request from fsck to clean up the filesystem,
664 * then allow the specified pid to proceed.
667 if (ump->um_fsckpid != 0) {
669 "Active checker already running on %s",
673 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
674 ("soft updates enabled on read-only file system"));
677 * Request write access.
679 error = g_access(ump->um_cp, 0, 1, 0);
683 "Checker activation failed on %s",
687 ump->um_fsckpid = fsckpid;
688 if (fs->fs_snapinum[0] != 0)
689 ffs_snapshot_mount(mp);
690 fs->fs_mtime = time_second;
693 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
697 * If this is a snapshot request, take the snapshot.
699 if (mp->mnt_flag & MNT_SNAPSHOT)
700 return (ffs_snapshot(mp, fspec));
703 * Must not call namei() while owning busy ref.
709 * Not an update, or updating the name: look up the name
710 * and verify that it refers to a sensible disk device.
712 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
714 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
716 * Unmount does not start if MNT_UPDATE is set. Mount
717 * update busies mp before setting MNT_UPDATE. We
718 * must be able to retain our busy ref succesfully,
721 error1 = vfs_busy(mp, MBF_NOWAIT);
726 NDFREE(&ndp, NDF_ONLY_PNBUF);
728 if (!vn_isdisk_error(devvp, &error)) {
734 * If mount by non-root, then verify that user has necessary
735 * permissions on the device.
738 if ((mp->mnt_flag & MNT_RDONLY) == 0)
740 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
742 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
748 if (mp->mnt_flag & MNT_UPDATE) {
752 * If it's not the same vnode, or at least the same device
753 * then it's not correct.
756 if (devvp->v_rdev != ump->um_devvp->v_rdev)
757 error = EINVAL; /* needs translation */
765 * We need the name for the mount point (also used for
766 * "last mounted on") copied in. If an error occurs,
767 * the mount point is discarded by the upper level code.
768 * Note that vfs_mount_alloc() populates f_mntonname for us.
770 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
775 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
776 ("soft updates enabled on read-only file system"));
781 * Request write access.
783 error = g_access(ump->um_cp, 0, 1, 0);
786 printf("WARNING: %s: Checker activation "
787 "failed\n", fs->fs_fsmnt);
789 ump->um_fsckpid = fsckpid;
790 if (fs->fs_snapinum[0] != 0)
791 ffs_snapshot_mount(mp);
792 fs->fs_mtime = time_second;
794 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
801 * This is racy versus lookup, see ufs_fplookup_vexec for details.
803 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) != 0)
804 panic("MNTK_FPLOOKUP set on mount %p when it should not be", mp);
805 if ((mp->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS | MNT_UNION)) == 0)
806 mp->mnt_kern_flag |= MNTK_FPLOOKUP;
809 vfs_mountedfrom(mp, fspec);
814 * Compatibility with old mount system call.
818 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
820 struct ufs_args args;
825 error = copyin(data, &args, sizeof args);
829 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
830 ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
831 error = kernel_mount(ma, flags);
837 * Reload all incore data for a filesystem (used after running fsck on
838 * the root filesystem and finding things to fix). If the 'force' flag
839 * is 0, the filesystem must be mounted read-only.
841 * Things to do to update the mount:
842 * 1) invalidate all cached meta-data.
843 * 2) re-read superblock from disk.
844 * 3) re-read summary information from disk.
845 * 4) invalidate all inactive vnodes.
846 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
847 * writers, if requested.
848 * 6) invalidate all cached file data.
849 * 7) re-read inode data for all active vnodes.
852 ffs_reload(struct mount *mp, struct thread *td, int flags)
854 struct vnode *vp, *mvp, *devvp;
858 struct fs *fs, *newfs;
859 struct ufsmount *ump;
860 ufs2_daddr_t sblockloc;
868 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
875 * Step 1: invalidate all cached meta-data.
877 devvp = VFSTOUFS(mp)->um_devvp;
878 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
879 if (vinvalbuf(devvp, 0, 0, 0) != 0)
880 panic("ffs_reload: dirty1");
884 * Step 2: re-read superblock from disk.
886 fs = VFSTOUFS(mp)->um_fs;
887 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
890 newfs = (struct fs *)bp->b_data;
891 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
892 newfs->fs_magic != FS_UFS2_MAGIC) ||
893 newfs->fs_bsize > MAXBSIZE ||
894 newfs->fs_bsize < sizeof(struct fs)) {
896 return (EIO); /* XXX needs translation */
899 * Preserve the summary information, read-only status, and
900 * superblock location by copying these fields into our new
901 * superblock before using it to update the existing superblock.
903 newfs->fs_si = fs->fs_si;
904 newfs->fs_ronly = fs->fs_ronly;
905 sblockloc = fs->fs_sblockloc;
906 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
908 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
909 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
911 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
912 printf("WARNING: %s: reload pending error: blocks %jd "
913 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
914 fs->fs_pendinginodes);
915 fs->fs_pendingblocks = 0;
916 fs->fs_pendinginodes = 0;
921 * Step 3: re-read summary information from disk.
923 size = fs->fs_cssize;
924 blks = howmany(size, fs->fs_fsize);
925 if (fs->fs_contigsumsize > 0)
926 size += fs->fs_ncg * sizeof(int32_t);
927 size += fs->fs_ncg * sizeof(u_int8_t);
928 free(fs->fs_csp, M_UFSMNT);
929 space = malloc(size, M_UFSMNT, M_WAITOK);
931 for (i = 0; i < blks; i += fs->fs_frag) {
933 if (i + fs->fs_frag > blks)
934 size = (blks - i) * fs->fs_fsize;
935 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
939 bcopy(bp->b_data, space, (u_int)size);
940 space = (char *)space + size;
944 * We no longer know anything about clusters per cylinder group.
946 if (fs->fs_contigsumsize > 0) {
947 fs->fs_maxcluster = lp = space;
948 for (i = 0; i < fs->fs_ncg; i++)
949 *lp++ = fs->fs_contigsumsize;
952 size = fs->fs_ncg * sizeof(u_int8_t);
953 fs->fs_contigdirs = (u_int8_t *)space;
954 bzero(fs->fs_contigdirs, size);
955 if ((flags & FFSR_UNSUSPEND) != 0) {
957 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
958 wakeup(&mp->mnt_flag);
963 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
967 if (vp->v_type == VNON) {
972 * Step 4: invalidate all cached file data.
974 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
975 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
978 if (vinvalbuf(vp, 0, 0, 0))
979 panic("ffs_reload: dirty2");
981 * Step 5: re-read inode data for all active vnodes.
985 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
986 (int)fs->fs_bsize, NOCRED, &bp);
989 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
992 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
995 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
998 ip->i_effnlink = ip->i_nlink;
1006 * Common code for mount and mountroot
1009 ffs_mountfs(odevvp, mp, td)
1010 struct vnode *odevvp;
1014 struct ufsmount *ump;
1017 int error, i, len, ronly;
1019 struct g_consumer *cp;
1021 struct vnode *devvp;
1022 int candelete, canspeedup;
1027 cred = td ? td->td_ucred : NOCRED;
1028 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1030 devvp = mntfs_allocvp(mp, odevvp);
1032 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
1033 dev = devvp->v_rdev;
1034 KASSERT(dev->si_snapdata == NULL, ("non-NULL snapshot data"));
1035 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
1036 (uintptr_t)mp) == 0) {
1037 mntfs_freevp(devvp);
1041 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
1042 g_topology_unlock();
1044 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1045 mntfs_freevp(devvp);
1049 devvp->v_bufobj.bo_ops = &ffs_ops;
1050 BO_LOCK(&odevvp->v_bufobj);
1051 odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
1052 BO_UNLOCK(&odevvp->v_bufobj);
1053 if (dev->si_iosize_max != 0)
1054 mp->mnt_iosize_max = dev->si_iosize_max;
1055 if (mp->mnt_iosize_max > maxphys)
1056 mp->mnt_iosize_max = maxphys;
1057 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
1060 "Invalid sectorsize %d for superblock size %d",
1061 cp->provider->sectorsize, SBLOCKSIZE);
1064 /* fetch the superblock and summary information */
1066 if ((mp->mnt_flag & MNT_ROOTFS) != 0)
1067 loc = STDSB_NOHASHFAIL;
1068 if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
1070 fs->fs_flags &= ~FS_UNCLEAN;
1071 if (fs->fs_clean == 0) {
1072 fs->fs_flags |= FS_UNCLEAN;
1073 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
1074 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
1075 (fs->fs_flags & FS_DOSOFTDEP))) {
1076 printf("WARNING: %s was not properly dismounted\n",
1079 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
1080 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
1081 (fs->fs_flags & FS_SUJ) == 0 ? "" :
1082 " Forced mount will invalidate journal contents");
1086 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
1087 (mp->mnt_flag & MNT_FORCE)) {
1088 printf("WARNING: %s: lost blocks %jd files %d\n",
1089 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1090 fs->fs_pendinginodes);
1091 fs->fs_pendingblocks = 0;
1092 fs->fs_pendinginodes = 0;
1095 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1096 printf("WARNING: %s: mount pending error: blocks %jd "
1097 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1098 fs->fs_pendinginodes);
1099 fs->fs_pendingblocks = 0;
1100 fs->fs_pendinginodes = 0;
1102 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
1105 * Get journal provider name.
1108 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
1109 if (g_io_getattr("GJOURNAL::provider", cp, &len,
1110 mp->mnt_gjprovider) == 0) {
1111 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
1112 M_UFSMNT, M_WAITOK);
1114 mp->mnt_flag |= MNT_GJOURNAL;
1117 printf("WARNING: %s: GJOURNAL flag on fs "
1118 "but no gjournal provider below\n",
1119 mp->mnt_stat.f_mntonname);
1120 free(mp->mnt_gjprovider, M_UFSMNT);
1121 mp->mnt_gjprovider = NULL;
1124 printf("WARNING: %s: GJOURNAL flag on fs but no "
1125 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
1128 mp->mnt_gjprovider = NULL;
1130 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
1132 ump->um_bo = &devvp->v_bufobj;
1134 if (fs->fs_magic == FS_UFS1_MAGIC) {
1135 ump->um_fstype = UFS1;
1136 ump->um_balloc = ffs_balloc_ufs1;
1138 ump->um_fstype = UFS2;
1139 ump->um_balloc = ffs_balloc_ufs2;
1141 ump->um_blkatoff = ffs_blkatoff;
1142 ump->um_truncate = ffs_truncate;
1143 ump->um_update = ffs_update;
1144 ump->um_valloc = ffs_valloc;
1145 ump->um_vfree = ffs_vfree;
1146 ump->um_ifree = ffs_ifree;
1147 ump->um_rdonly = ffs_rdonly;
1148 ump->um_snapgone = ffs_snapgone;
1149 if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
1150 ump->um_check_blkno = ffs_check_blkno;
1152 ump->um_check_blkno = NULL;
1153 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
1154 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
1155 fs->fs_ronly = ronly;
1156 fs->fs_active = NULL;
1158 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
1159 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
1161 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
1162 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
1167 ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
1169 mp->mnt_flag |= MNT_LOCAL;
1171 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
1174 mp->mnt_flag |= MNT_MULTILABEL;
1177 printf("WARNING: %s: multilabel flag on fs but "
1178 "no MAC support\n", mp->mnt_stat.f_mntonname);
1181 if ((fs->fs_flags & FS_ACLS) != 0) {
1185 if (mp->mnt_flag & MNT_NFS4ACLS)
1186 printf("WARNING: %s: ACLs flag on fs conflicts with "
1187 "\"nfsv4acls\" mount option; option ignored\n",
1188 mp->mnt_stat.f_mntonname);
1189 mp->mnt_flag &= ~MNT_NFS4ACLS;
1190 mp->mnt_flag |= MNT_ACLS;
1194 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
1195 mp->mnt_stat.f_mntonname);
1198 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
1202 if (mp->mnt_flag & MNT_ACLS)
1203 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1204 "with \"acls\" mount option; option ignored\n",
1205 mp->mnt_stat.f_mntonname);
1206 mp->mnt_flag &= ~MNT_ACLS;
1207 mp->mnt_flag |= MNT_NFS4ACLS;
1211 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1212 "ACLs support\n", mp->mnt_stat.f_mntonname);
1215 if ((fs->fs_flags & FS_TRIM) != 0) {
1217 if (g_io_getattr("GEOM::candelete", cp, &len,
1220 ump->um_flags |= UM_CANDELETE;
1222 printf("WARNING: %s: TRIM flag on fs but disk "
1223 "does not support TRIM\n",
1224 mp->mnt_stat.f_mntonname);
1226 printf("WARNING: %s: TRIM flag on fs but disk does "
1227 "not confirm that it supports TRIM\n",
1228 mp->mnt_stat.f_mntonname);
1230 if (((ump->um_flags) & UM_CANDELETE) != 0) {
1231 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1232 taskqueue_thread_enqueue, &ump->um_trim_tq);
1233 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1234 "%s trim", mp->mnt_stat.f_mntonname);
1235 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1236 &ump->um_trimlisthashsize);
1241 if (g_io_getattr("GEOM::canspeedup", cp, &len, &canspeedup) == 0) {
1243 ump->um_flags |= UM_CANSPEEDUP;
1246 ump->um_mountp = mp;
1248 ump->um_devvp = devvp;
1249 ump->um_odevvp = odevvp;
1250 ump->um_nindir = fs->fs_nindir;
1251 ump->um_bptrtodb = fs->fs_fsbtodb;
1252 ump->um_seqinc = fs->fs_frag;
1253 for (i = 0; i < MAXQUOTAS; i++)
1254 ump->um_quotas[i] = NULLVP;
1256 ufs_extattr_uepm_init(&ump->um_extattr);
1259 * Set FS local "last mounted on" information (NULL pad)
1261 bzero(fs->fs_fsmnt, MAXMNTLEN);
1262 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1263 mp->mnt_stat.f_iosize = fs->fs_bsize;
1265 if (mp->mnt_flag & MNT_ROOTFS) {
1267 * Root mount; update timestamp in mount structure.
1268 * this will be used by the common root mount code
1269 * to update the system clock.
1271 mp->mnt_time = fs->fs_time;
1275 fs->fs_mtime = time_second;
1276 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1277 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1278 ffs_flushfiles(mp, FORCECLOSE, td);
1281 if (fs->fs_snapinum[0] != 0)
1282 ffs_snapshot_mount(mp);
1285 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1288 * Initialize filesystem state information in mount struct.
1291 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1292 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1295 #ifdef UFS_EXTATTR_AUTOSTART
1298 * Auto-starting does the following:
1299 * - check for /.attribute in the fs, and extattr_start if so
1300 * - for each file in .attribute, enable that file with
1301 * an attribute of the same name.
1302 * Not clear how to report errors -- probably eat them.
1303 * This would all happen while the filesystem was busy/not
1304 * available, so would effectively be "atomic".
1306 (void) ufs_extattr_autostart(mp, td);
1307 #endif /* !UFS_EXTATTR_AUTOSTART */
1308 #endif /* !UFS_EXTATTR */
1312 free(fs->fs_csp, M_UFSMNT);
1313 free(fs->fs_si, M_UFSMNT);
1319 g_topology_unlock();
1322 mtx_destroy(UFS_MTX(ump));
1323 if (mp->mnt_gjprovider != NULL) {
1324 free(mp->mnt_gjprovider, M_UFSMNT);
1325 mp->mnt_gjprovider = NULL;
1327 MPASS(ump->um_softdep == NULL);
1328 free(ump, M_UFSMNT);
1329 mp->mnt_data = NULL;
1331 BO_LOCK(&odevvp->v_bufobj);
1332 odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1333 BO_UNLOCK(&odevvp->v_bufobj);
1334 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1335 mntfs_freevp(devvp);
1341 * A read function for use by filesystem-layer routines.
1344 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1349 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1350 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1351 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1354 bcopy(bp->b_data, *bufp, size);
1355 bp->b_flags |= B_INVAL | B_NOCACHE;
1360 static int bigcgs = 0;
1361 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1364 * Sanity checks for loading old filesystem superblocks.
1365 * See ffs_oldfscompat_write below for unwound actions.
1367 * XXX - Parts get retired eventually.
1368 * Unfortunately new bits get added.
1371 ffs_oldfscompat_read(fs, ump, sblockloc)
1373 struct ufsmount *ump;
1374 ufs2_daddr_t sblockloc;
1379 * If not yet done, update fs_flags location and value of fs_sblockloc.
1381 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1382 fs->fs_flags = fs->fs_old_flags;
1383 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1384 fs->fs_sblockloc = sblockloc;
1387 * If not yet done, update UFS1 superblock with new wider fields.
1389 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1390 fs->fs_maxbsize = fs->fs_bsize;
1391 fs->fs_time = fs->fs_old_time;
1392 fs->fs_size = fs->fs_old_size;
1393 fs->fs_dsize = fs->fs_old_dsize;
1394 fs->fs_csaddr = fs->fs_old_csaddr;
1395 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1396 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1397 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1398 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1400 if (fs->fs_magic == FS_UFS1_MAGIC &&
1401 fs->fs_old_inodefmt < FS_44INODEFMT) {
1402 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1403 fs->fs_qbmask = ~fs->fs_bmask;
1404 fs->fs_qfmask = ~fs->fs_fmask;
1406 if (fs->fs_magic == FS_UFS1_MAGIC) {
1407 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1408 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1409 if (fs->fs_maxfilesize > maxfilesize)
1410 fs->fs_maxfilesize = maxfilesize;
1412 /* Compatibility for old filesystems */
1413 if (fs->fs_avgfilesize <= 0)
1414 fs->fs_avgfilesize = AVFILESIZ;
1415 if (fs->fs_avgfpdir <= 0)
1416 fs->fs_avgfpdir = AFPDIR;
1418 fs->fs_save_cgsize = fs->fs_cgsize;
1419 fs->fs_cgsize = fs->fs_bsize;
1424 * Unwinding superblock updates for old filesystems.
1425 * See ffs_oldfscompat_read above for details.
1427 * XXX - Parts get retired eventually.
1428 * Unfortunately new bits get added.
1431 ffs_oldfscompat_write(fs, ump)
1433 struct ufsmount *ump;
1437 * Copy back UFS2 updated fields that UFS1 inspects.
1439 if (fs->fs_magic == FS_UFS1_MAGIC) {
1440 fs->fs_old_time = fs->fs_time;
1441 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1442 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1443 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1444 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1445 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1448 fs->fs_cgsize = fs->fs_save_cgsize;
1449 fs->fs_save_cgsize = 0;
1454 * unmount system call
1457 ffs_unmount(mp, mntflags)
1462 struct ufsmount *ump = VFSTOUFS(mp);
1464 int error, flags, susp;
1472 if (mntflags & MNT_FORCE)
1473 flags |= FORCECLOSE;
1474 susp = fs->fs_ronly == 0;
1476 if ((error = ufs_extattr_stop(mp, td))) {
1477 if (error != EOPNOTSUPP)
1478 printf("WARNING: unmount %s: ufs_extattr_stop "
1479 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1483 ufs_extattr_uepm_destroy(&ump->um_extattr);
1488 error = vfs_write_suspend_umnt(mp);
1492 if (MOUNTEDSOFTDEP(mp))
1493 error = softdep_flushfiles(mp, flags, td);
1495 error = ffs_flushfiles(mp, flags, td);
1496 if (error != 0 && !ffs_fsfail_cleanup(ump, error))
1500 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1501 printf("WARNING: unmount %s: pending error: blocks %jd "
1502 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1503 fs->fs_pendinginodes);
1504 fs->fs_pendingblocks = 0;
1505 fs->fs_pendinginodes = 0;
1508 if (MOUNTEDSOFTDEP(mp))
1509 softdep_unmount(mp);
1510 MPASS(ump->um_softdep == NULL);
1511 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1512 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1513 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1514 if (ffs_fsfail_cleanup(ump, error))
1516 if (error != 0 && !ffs_fsfail_cleanup(ump, error)) {
1522 vfs_write_resume(mp, VR_START_WRITE);
1523 if (ump->um_trim_tq != NULL) {
1524 while (ump->um_trim_inflight != 0)
1525 pause("ufsutr", hz);
1526 taskqueue_drain_all(ump->um_trim_tq);
1527 taskqueue_free(ump->um_trim_tq);
1528 free (ump->um_trimhash, M_TRIM);
1531 if (ump->um_fsckpid > 0) {
1533 * Return to normal read-only mode.
1535 error = g_access(ump->um_cp, 0, -1, 0);
1536 ump->um_fsckpid = 0;
1538 g_vfs_close(ump->um_cp);
1539 g_topology_unlock();
1540 BO_LOCK(&ump->um_odevvp->v_bufobj);
1541 ump->um_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1542 BO_UNLOCK(&ump->um_odevvp->v_bufobj);
1543 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1544 mntfs_freevp(ump->um_devvp);
1545 vrele(ump->um_odevvp);
1546 dev_rel(ump->um_dev);
1547 mtx_destroy(UFS_MTX(ump));
1548 if (mp->mnt_gjprovider != NULL) {
1549 free(mp->mnt_gjprovider, M_UFSMNT);
1550 mp->mnt_gjprovider = NULL;
1552 free(fs->fs_csp, M_UFSMNT);
1553 free(fs->fs_si, M_UFSMNT);
1555 free(ump, M_UFSMNT);
1556 mp->mnt_data = NULL;
1558 mp->mnt_flag &= ~MNT_LOCAL;
1560 if (td->td_su == mp) {
1568 vfs_write_resume(mp, VR_START_WRITE);
1572 ufs_extattr_uepm_init(&ump->um_extattr);
1573 #ifdef UFS_EXTATTR_AUTOSTART
1574 (void) ufs_extattr_autostart(mp, td);
1583 * Flush out all the files in a filesystem.
1586 ffs_flushfiles(mp, flags, td)
1591 struct ufsmount *ump;
1597 if (mp->mnt_flag & MNT_QUOTA) {
1599 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1602 for (i = 0; i < MAXQUOTAS; i++) {
1603 error = quotaoff(td, mp, i);
1605 if ((flags & EARLYFLUSH) == 0)
1613 * Here we fall through to vflush again to ensure that
1614 * we have gotten rid of all the system vnodes, unless
1615 * quotas must not be closed.
1619 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1620 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1621 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1623 ffs_snapshot_unmount(mp);
1624 flags |= FORCECLOSE;
1626 * Here we fall through to vflush again to ensure
1627 * that we have gotten rid of all the system vnodes.
1632 * Do not close system files if quotas were not closed, to be
1633 * able to sync the remaining dquots. The freeblks softupdate
1634 * workitems might hold a reference on a dquot, preventing
1635 * quotaoff() from completing. Next round of
1636 * softdep_flushworklist() iteration should process the
1637 * blockers, allowing the next run of quotaoff() to finally
1638 * flush held dquots.
1640 * Otherwise, flush all the files.
1642 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1646 * Flush filesystem metadata.
1648 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1649 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1650 VOP_UNLOCK(ump->um_devvp);
1655 * Get filesystem statistics.
1662 struct ufsmount *ump;
1667 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1668 panic("ffs_statfs");
1669 sbp->f_version = STATFS_VERSION;
1670 sbp->f_bsize = fs->fs_fsize;
1671 sbp->f_iosize = fs->fs_bsize;
1672 sbp->f_blocks = fs->fs_dsize;
1674 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1675 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1676 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1677 dbtofsb(fs, fs->fs_pendingblocks);
1678 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1679 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1681 sbp->f_namemax = UFS_MAXNAMLEN;
1686 sync_doupdate(struct inode *ip)
1689 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1694 ffs_sync_lazy_filter(struct vnode *vp, void *arg __unused)
1699 * Flags are safe to access because ->v_data invalidation
1700 * is held off by listmtx.
1702 if (vp->v_type == VNON)
1705 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0)
1711 * For a lazy sync, we only care about access times, quotas and the
1712 * superblock. Other filesystem changes are already converted to
1713 * cylinder group blocks or inode blocks updates and are written to
1720 struct vnode *mvp, *vp;
1723 int allerror, error;
1727 if ((mp->mnt_flag & MNT_NOATIME) != 0) {
1733 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, ffs_sync_lazy_filter, NULL) {
1734 if (vp->v_type == VNON) {
1741 * The IN_ACCESS flag is converted to IN_MODIFIED by
1742 * ufs_close() and ufs_getattr() by the calls to
1743 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1744 * Test also all the other timestamp flags too, to pick up
1745 * any other cases that could be missed.
1747 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1751 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK)) != 0)
1756 if (sync_doupdate(ip))
1757 error = ffs_update(vp, 0);
1763 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1764 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1770 * Go through the disk queues to initiate sandbagged IO;
1771 * go through the inodes to write those that have been modified;
1772 * initiate the writing of the super block if it has been modified.
1774 * Note: we are always called with the filesystem marked busy using
1778 ffs_sync(mp, waitfor)
1782 struct vnode *mvp, *vp, *devvp;
1785 struct ufsmount *ump = VFSTOUFS(mp);
1787 int error, count, lockreq, allerror = 0;
1790 int secondary_writes;
1791 int secondary_accwrites;
1793 int softdep_accdeps;
1800 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1801 panic("%s: ffs_sync: modification on read-only filesystem",
1803 if (waitfor == MNT_LAZY) {
1805 return (ffs_sync_lazy(mp));
1806 waitfor = MNT_NOWAIT;
1810 * Write back each (modified) inode.
1812 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1813 if (waitfor == MNT_SUSPEND) {
1817 if (waitfor == MNT_WAIT)
1818 lockreq = LK_EXCLUSIVE;
1819 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1821 /* Grab snapshot of secondary write counts */
1823 secondary_writes = mp->mnt_secondary_writes;
1824 secondary_accwrites = mp->mnt_secondary_accwrites;
1827 /* Grab snapshot of softdep dependency counts */
1828 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1830 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1832 * Depend on the vnode interlock to keep things stable enough
1833 * for a quick test. Since there might be hundreds of
1834 * thousands of vnodes, we cannot afford even a subroutine
1835 * call unless there's a good chance that we have work to do.
1837 if (vp->v_type == VNON) {
1843 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1844 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1848 if ((error = vget(vp, lockreq)) != 0) {
1849 if (error == ENOENT || error == ENOLCK) {
1850 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1859 error = ffs_syncvnode(vp, waitfor, 0);
1860 if (error == ERELOOKUP)
1869 * Force stale filesystem control information to be flushed.
1871 if (waitfor == MNT_WAIT || rebooting) {
1872 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1874 if (ffs_fsfail_cleanup(ump, allerror))
1876 /* Flushed work items may create new vnodes to clean */
1877 if (allerror == 0 && count)
1881 devvp = ump->um_devvp;
1882 bo = &devvp->v_bufobj;
1884 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1886 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1887 error = VOP_FSYNC(devvp, waitfor, td);
1889 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1890 error = ffs_sbupdate(ump, waitfor, 0);
1893 if (ffs_fsfail_cleanup(ump, allerror))
1895 if (allerror == 0 && waitfor == MNT_WAIT)
1897 } else if (suspend != 0) {
1898 if (softdep_check_suspend(mp,
1903 secondary_accwrites) != 0) {
1905 goto loop; /* More work needed */
1907 mtx_assert(MNT_MTX(mp), MA_OWNED);
1908 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1914 * Write back modified superblock.
1916 if (fs->fs_fmod != 0 &&
1917 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1919 if (ffs_fsfail_cleanup(ump, allerror))
1925 ffs_vget(mp, ino, flags, vpp)
1931 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1935 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1944 struct ufsmount *ump;
1950 MPASS((ffs_flags & (FFSV_REPLACE | FFSV_REPLACE_DOOMED)) == 0 ||
1951 (flags & LK_EXCLUSIVE) != 0);
1953 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1957 if ((ffs_flags & FFSV_REPLACE) == 0 ||
1958 ((ffs_flags & FFSV_REPLACE_DOOMED) == 0 ||
1959 !VN_IS_DOOMED(*vpp)))
1966 * We must promote to an exclusive lock for vnode creation. This
1967 * can happen if lookup is passed LOCKSHARED.
1969 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1970 flags &= ~LK_TYPE_MASK;
1971 flags |= LK_EXCLUSIVE;
1975 * We do not lock vnode creation as it is believed to be too
1976 * expensive for such rare case as simultaneous creation of vnode
1977 * for same ino by different processes. We just allow them to race
1978 * and check later to decide who wins. Let the race begin!
1983 ip = uma_zalloc_smr(uma_inode, M_WAITOK | M_ZERO);
1985 /* Allocate a new vnode/inode. */
1986 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1987 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1990 uma_zfree_smr(uma_inode, ip);
1994 * FFS supports recursive locking.
1996 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1999 vp->v_bufobj.bo_bsize = fs->fs_bsize;
2004 ip->i_nextclustercg = -1;
2005 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
2006 ip->i_mode = 0; /* ensure error cases below throw away vnode */
2007 cluster_init_vn(&ip->i_clusterw);
2009 ufs_init_trackers(ip);
2014 for (i = 0; i < MAXQUOTAS; i++)
2015 ip->i_dquot[i] = NODQUOT;
2019 if (ffs_flags & FFSV_FORCEINSMQ)
2020 vp->v_vflag |= VV_FORCEINSMQ;
2021 error = insmntque(vp, mp);
2023 uma_zfree_smr(uma_inode, ip);
2027 vp->v_vflag &= ~VV_FORCEINSMQ;
2028 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
2033 * Calls from ffs_valloc() (i.e. FFSV_REPLACE set)
2034 * operate on empty inode, which must not be found by
2035 * other threads until fully filled. Vnode for empty
2036 * inode must be not re-inserted on the hash by other
2037 * thread, after removal by us at the beginning.
2039 MPASS((ffs_flags & FFSV_REPLACE) == 0);
2043 /* Read in the disk contents for the inode, copy into the inode. */
2044 dbn = fsbtodb(fs, ino_to_fsba(fs, ino));
2045 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
2046 NULL, NULL, 0, NOCRED, 0, NULL, &bp);
2049 * The inode does not contain anything useful, so it would
2050 * be misleading to leave it on its hash chain. With mode
2051 * still zero, it will be unlinked and returned to the free
2060 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
2062 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
2063 if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
2070 if (DOINGSOFTDEP(vp) && (!fs->fs_ronly ||
2071 (ffs_flags & FFSV_FORCEINODEDEP) != 0))
2072 softdep_load_inodeblock(ip);
2074 ip->i_effnlink = ip->i_nlink;
2078 * Initialize the vnode from the inode, check for aliases.
2079 * Note that the underlying vnode may have changed.
2081 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
2091 * Finish inode initialization.
2093 if (vp->v_type != VFIFO) {
2094 /* FFS supports shared locking for all files except fifos. */
2099 * Set up a generation number for this inode if it does not
2100 * already have one. This should only happen on old filesystems.
2102 if (ip->i_gen == 0) {
2103 while (ip->i_gen == 0)
2104 ip->i_gen = arc4random();
2105 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
2106 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
2107 DIP_SET(ip, i_gen, ip->i_gen);
2111 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
2113 * If this vnode is already allocated, and we're running
2114 * multi-label, attempt to perform a label association
2115 * from the extended attributes on the inode.
2117 error = mac_vnode_associate_extattr(mp, vp);
2119 /* ufs_inactive will release ip->i_devvp ref. */
2133 * File handle to vnode
2135 * Have to be really careful about stale file handles:
2136 * - check that the inode number is valid
2137 * - for UFS2 check that the inode number is initialized
2138 * - call ffs_vget() to get the locked inode
2139 * - check for an unallocated inode (i_mode == 0)
2140 * - check that the given client host has export rights and return
2141 * those rights via. exflagsp and credanonp
2144 ffs_fhtovp(mp, fhp, flags, vpp)
2152 ufhp = (struct ufid *)fhp;
2153 return (ffs_inotovp(mp, ufhp->ufid_ino, ufhp->ufid_gen, flags,
2158 ffs_inotovp(mp, ino, gen, lflags, vpp, ffs_flags)
2166 struct ufsmount *ump;
2179 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
2183 * Need to check if inode is initialized because UFS2 does lazy
2184 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
2186 if (fs->fs_magic == FS_UFS2_MAGIC) {
2187 cg = ino_to_cg(fs, ino);
2188 error = ffs_getcg(fs, ump->um_devvp, cg, 0, &bp, &cgp);
2191 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
2198 error = ffs_vgetf(mp, ino, lflags, &nvp, ffs_flags);
2203 if (ip->i_mode == 0 || ip->i_gen != gen || ip->i_effnlink <= 0) {
2204 if (ip->i_mode == 0)
2210 vnode_create_vobject(nvp, DIP(ip, i_size), curthread);
2216 * Initialize the filesystem.
2220 struct vfsconf *vfsp;
2223 ffs_susp_initialize();
2224 softdep_initialize();
2225 return (ufs_init(vfsp));
2229 * Undo the work of ffs_init().
2233 struct vfsconf *vfsp;
2237 ret = ufs_uninit(vfsp);
2238 softdep_uninitialize();
2239 ffs_susp_uninitialize();
2240 taskqueue_drain_all(taskqueue_thread);
2245 * Structure used to pass information from ffs_sbupdate to its
2246 * helper routine ffs_use_bwrite.
2249 struct ufsmount *ump;
2257 * Write a superblock and associated information back to disk.
2260 ffs_sbupdate(ump, waitfor, suspended)
2261 struct ufsmount *ump;
2270 if (fs->fs_ronly == 1 &&
2271 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
2272 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
2273 panic("ffs_sbupdate: write read-only filesystem");
2275 * We use the superblock's buf to serialize calls to ffs_sbupdate().
2277 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
2278 (int)fs->fs_sbsize, 0, 0, 0);
2280 * Initialize info needed for write function.
2284 devfd.waitfor = waitfor;
2285 devfd.suspended = suspended;
2287 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
2291 * Write function for use by filesystem-layer routines.
2294 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
2296 struct devfd *devfdp;
2297 struct ufsmount *ump;
2306 * Writing the superblock summary information.
2308 if (loc != fs->fs_sblockloc) {
2309 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
2310 bcopy(buf, bp->b_data, (u_int)size);
2311 if (devfdp->suspended)
2312 bp->b_flags |= B_VALIDSUSPWRT;
2313 if (devfdp->waitfor != MNT_WAIT)
2315 else if ((error = bwrite(bp)) != 0)
2316 devfdp->error = error;
2320 * Writing the superblock itself. We need to do special checks for it.
2323 if (ffs_fsfail_cleanup(ump, devfdp->error))
2325 if (devfdp->error != 0) {
2327 return (devfdp->error);
2329 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
2330 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2331 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2332 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
2333 fs->fs_sblockloc = SBLOCK_UFS1;
2335 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
2336 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2337 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2338 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
2339 fs->fs_sblockloc = SBLOCK_UFS2;
2341 if (MOUNTEDSOFTDEP(ump->um_mountp))
2342 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
2343 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
2344 fs = (struct fs *)bp->b_data;
2345 ffs_oldfscompat_write(fs, ump);
2347 /* Recalculate the superblock hash */
2348 fs->fs_ckhash = ffs_calc_sbhash(fs);
2349 if (devfdp->suspended)
2350 bp->b_flags |= B_VALIDSUSPWRT;
2351 if (devfdp->waitfor != MNT_WAIT)
2353 else if ((error = bwrite(bp)) != 0)
2354 devfdp->error = error;
2355 return (devfdp->error);
2359 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2360 int attrnamespace, const char *attrname)
2364 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2367 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2373 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2376 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2377 uma_zfree(uma_ufs1, ip->i_din1);
2378 else if (ip->i_din2 != NULL)
2379 uma_zfree(uma_ufs2, ip->i_din2);
2380 uma_zfree_smr(uma_inode, ip);
2383 static int dobkgrdwrite = 1;
2384 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2385 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2388 * Complete a background write started from bwrite.
2391 ffs_backgroundwritedone(struct buf *bp)
2393 struct bufobj *bufobj;
2397 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) != 0)
2398 softdep_handle_error(bp);
2402 * Find the original buffer that we are writing.
2404 bufobj = bp->b_bufobj;
2406 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2407 panic("backgroundwritedone: lost buffer");
2410 * We should mark the cylinder group buffer origbp as
2411 * dirty, to not lose the failed write.
2413 if ((bp->b_ioflags & BIO_ERROR) != 0)
2414 origbp->b_vflags |= BV_BKGRDERR;
2417 * Process dependencies then return any unfinished ones.
2419 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2422 if (!LIST_EMPTY(&bp->b_dep))
2423 softdep_move_dependencies(bp, origbp);
2426 * This buffer is marked B_NOCACHE so when it is released
2427 * by biodone it will be tossed. Clear B_IOSTARTED in case of error.
2429 bp->b_flags |= B_NOCACHE;
2430 bp->b_flags &= ~(B_CACHE | B_IOSTARTED);
2434 * Prevent brelse() from trying to keep and re-dirtying bp on
2435 * errors. It causes b_bufobj dereference in
2436 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2439 if ((bp->b_ioflags & BIO_ERROR) != 0)
2440 bp->b_flags |= B_INVAL;
2444 * Clear the BV_BKGRDINPROG flag in the original buffer
2445 * and awaken it if it is waiting for the write to complete.
2446 * If BV_BKGRDINPROG is not set in the original buffer it must
2447 * have been released and re-instantiated - which is not legal.
2449 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2450 ("backgroundwritedone: lost buffer2"));
2451 origbp->b_vflags &= ~BV_BKGRDINPROG;
2452 if (origbp->b_vflags & BV_BKGRDWAIT) {
2453 origbp->b_vflags &= ~BV_BKGRDWAIT;
2454 wakeup(&origbp->b_xflags);
2460 * Write, release buffer on completion. (Done by iodone
2461 * if async). Do not bother writing anything if the buffer
2464 * Note that we set B_CACHE here, indicating that buffer is
2465 * fully valid and thus cacheable. This is true even of NFS
2466 * now so we set it generally. This could be set either here
2467 * or in biodone() since the I/O is synchronous. We put it
2471 ffs_bufwrite(struct buf *bp)
2476 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2477 if (bp->b_flags & B_INVAL) {
2482 if (!BUF_ISLOCKED(bp))
2483 panic("bufwrite: buffer is not busy???");
2485 * If a background write is already in progress, delay
2486 * writing this block if it is asynchronous. Otherwise
2487 * wait for the background write to complete.
2489 BO_LOCK(bp->b_bufobj);
2490 if (bp->b_vflags & BV_BKGRDINPROG) {
2491 if (bp->b_flags & B_ASYNC) {
2492 BO_UNLOCK(bp->b_bufobj);
2496 bp->b_vflags |= BV_BKGRDWAIT;
2497 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2499 if (bp->b_vflags & BV_BKGRDINPROG)
2500 panic("bufwrite: still writing");
2502 bp->b_vflags &= ~BV_BKGRDERR;
2503 BO_UNLOCK(bp->b_bufobj);
2506 * If this buffer is marked for background writing and we
2507 * do not have to wait for it, make a copy and write the
2508 * copy so as to leave this buffer ready for further use.
2510 * This optimization eats a lot of memory. If we have a page
2511 * or buffer shortfall we can't do it.
2513 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2514 (bp->b_flags & B_ASYNC) &&
2515 !vm_page_count_severe() &&
2516 !buf_dirty_count_severe()) {
2517 KASSERT(bp->b_iodone == NULL,
2518 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2520 /* get a new block */
2521 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2525 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2526 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2527 BO_LOCK(bp->b_bufobj);
2528 bp->b_vflags |= BV_BKGRDINPROG;
2529 BO_UNLOCK(bp->b_bufobj);
2531 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2532 newbp->b_lblkno = bp->b_lblkno;
2533 newbp->b_blkno = bp->b_blkno;
2534 newbp->b_offset = bp->b_offset;
2535 newbp->b_iodone = ffs_backgroundwritedone;
2536 newbp->b_flags |= B_ASYNC;
2537 newbp->b_flags &= ~B_INVAL;
2538 pbgetvp(bp->b_vp, newbp);
2542 * Move over the dependencies. If there are rollbacks,
2543 * leave the parent buffer dirtied as it will need to
2546 if (LIST_EMPTY(&bp->b_dep) ||
2547 softdep_move_dependencies(bp, newbp) == 0)
2554 * Initiate write on the copy, release the original. The
2555 * BKGRDINPROG flag prevents it from going away until
2556 * the background write completes. We have to recalculate
2557 * its check hash in case the buffer gets freed and then
2558 * reconstituted from the buffer cache during a later read.
2560 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2561 cgp = (struct cg *)bp->b_data;
2564 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2569 /* Mark the buffer clean */
2572 /* Let the normal bufwrite do the rest for us */
2575 * If we are writing a cylinder group, update its time.
2577 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2578 cgp = (struct cg *)bp->b_data;
2579 cgp->cg_old_time = cgp->cg_time = time_second;
2581 return (bufwrite(bp));
2585 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2592 * This is the bufobj strategy for the private VCHR vnodes
2593 * used by FFS to access the underlying storage device.
2594 * We override the default bufobj strategy and thus bypass
2595 * VOP_STRATEGY() for these vnodes.
2598 KASSERT(bp->b_vp == NULL || bp->b_vp->v_type != VCHR ||
2599 bp->b_vp->v_rdev == NULL ||
2600 bp->b_vp->v_rdev->si_mountpt == NULL ||
2601 VFSTOUFS(bp->b_vp->v_rdev->si_mountpt) == NULL ||
2602 vp == VFSTOUFS(bp->b_vp->v_rdev->si_mountpt)->um_devvp,
2603 ("ffs_geom_strategy() with wrong vp"));
2604 if (bp->b_iocmd == BIO_WRITE) {
2605 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2606 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2607 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2608 panic("ffs_geom_strategy: bad I/O");
2609 nocopy = bp->b_flags & B_NOCOPY;
2610 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2611 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2612 vp->v_rdev->si_snapdata != NULL) {
2613 if ((bp->b_flags & B_CLUSTER) != 0) {
2614 runningbufwakeup(bp);
2615 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2616 b_cluster.cluster_entry) {
2617 error = ffs_copyonwrite(vp, tbp);
2619 error != EOPNOTSUPP) {
2620 bp->b_error = error;
2621 bp->b_ioflags |= BIO_ERROR;
2622 bp->b_flags &= ~B_BARRIER;
2627 bp->b_runningbufspace = bp->b_bufsize;
2628 atomic_add_long(&runningbufspace,
2629 bp->b_runningbufspace);
2631 error = ffs_copyonwrite(vp, bp);
2632 if (error != 0 && error != EOPNOTSUPP) {
2633 bp->b_error = error;
2634 bp->b_ioflags |= BIO_ERROR;
2635 bp->b_flags &= ~B_BARRIER;
2642 if ((bp->b_flags & B_CLUSTER) != 0) {
2643 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2644 b_cluster.cluster_entry) {
2645 if (!LIST_EMPTY(&tbp->b_dep))
2649 if (!LIST_EMPTY(&bp->b_dep))
2655 * Check for metadata that needs check-hashes and update them.
2657 switch (bp->b_xflags & BX_FSPRIV) {
2659 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2660 ((struct cg *)bp->b_data)->cg_ckhash =
2661 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2668 printf("Check-hash write is unimplemented!!!\n");
2675 printf("multiple buffer types 0x%b\n",
2676 (u_int)(bp->b_xflags & BX_FSPRIV),
2677 PRINT_UFS_BUF_XFLAGS);
2681 if (bp->b_iocmd != BIO_READ && ffs_enxio_enable)
2682 bp->b_xflags |= BX_CVTENXIO;
2683 g_vfs_strategy(bo, bp);
2687 ffs_own_mount(const struct mount *mp)
2690 if (mp->mnt_op == &ufs_vfsops)
2698 /* defined in ffs_softdep.c */
2699 extern void db_print_ffs(struct ufsmount *ump);
2701 DB_SHOW_COMMAND(ffs, db_show_ffs)
2704 struct ufsmount *ump;
2707 ump = VFSTOUFS((struct mount *)addr);
2712 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2713 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2714 db_print_ffs(VFSTOUFS(mp));
2718 #endif /* SOFTUPDATES */