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 * Initiate a forcible unmount.
285 * Used to unmount filesystems whose underlying media has gone away.
288 ffs_fsfail_unmount(void *v, int pending)
290 struct fsfail_task *etp;
296 * Find our mount and get a ref on it, then try to unmount.
298 mp = vfs_getvfs(&etp->fsid);
300 dounmount(mp, MNT_FORCE, curthread);
305 * On first ENXIO error, start a task that forcibly unmounts the filesystem.
307 * Return true if a cleanup is in progress.
310 ffs_fsfail_cleanup(struct ufsmount *ump, int error)
315 retval = ffs_fsfail_cleanup_locked(ump, error);
321 ffs_fsfail_cleanup_locked(struct ufsmount *ump, int error)
323 struct fsfail_task *etp;
326 mtx_assert(UFS_MTX(ump), MA_OWNED);
327 if (error == ENXIO && (ump->um_flags & UM_FSFAIL_CLEANUP) == 0) {
328 ump->um_flags |= UM_FSFAIL_CLEANUP;
330 * Queue an async forced unmount.
332 etp = ump->um_fsfail_task;
333 ump->um_fsfail_task = NULL;
336 TASK_INIT(tp, 0, ffs_fsfail_unmount, etp);
337 taskqueue_enqueue(taskqueue_thread, tp);
338 printf("UFS: forcibly unmounting %s from %s\n",
339 ump->um_mountp->mnt_stat.f_mntfromname,
340 ump->um_mountp->mnt_stat.f_mntonname);
343 return ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0);
347 * Wrapper used during ENXIO cleanup to allocate empty buffers when
348 * the kernel is unable to read the real one. They are needed so that
349 * the soft updates code can use them to unwind its dependencies.
352 ffs_breadz(struct ufsmount *ump, struct vnode *vp, daddr_t lblkno,
353 daddr_t dblkno, int size, daddr_t *rablkno, int *rabsize, int cnt,
354 struct ucred *cred, int flags, void (*ckhashfunc)(struct buf *),
359 flags |= GB_CVTENXIO;
360 error = breadn_flags(vp, lblkno, dblkno, size, rablkno, rabsize, cnt,
361 cred, flags, ckhashfunc, bpp);
362 if (error != 0 && ffs_fsfail_cleanup(ump, error)) {
363 error = getblkx(vp, lblkno, dblkno, size, 0, 0, flags, bpp);
364 KASSERT(error == 0, ("getblkx failed"));
365 vfs_bio_bzero_buf(*bpp, 0, size);
371 ffs_mount(struct mount *mp)
373 struct vnode *devvp, *odevvp;
375 struct ufsmount *ump = NULL;
378 int error, error1, flags;
379 uint64_t mntorflags, saved_mnt_flag;
381 struct nameidata ndp;
383 bool mounted_softdep;
386 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
388 if (uma_inode == NULL) {
389 uma_inode = uma_zcreate("FFS inode",
390 sizeof(struct inode), NULL, NULL, NULL, NULL,
392 uma_ufs1 = uma_zcreate("FFS1 dinode",
393 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
395 uma_ufs2 = uma_zcreate("FFS2 dinode",
396 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
398 VFS_SMR_ZONE_SET(uma_inode);
401 vfs_deleteopt(mp->mnt_optnew, "groupquota");
402 vfs_deleteopt(mp->mnt_optnew, "userquota");
404 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
409 if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
410 mntorflags |= MNT_UNTRUSTED;
412 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
413 mntorflags |= MNT_ACLS;
415 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
416 mntorflags |= MNT_SNAPSHOT;
418 * Once we have set the MNT_SNAPSHOT flag, do not
419 * persist "snapshot" in the options list.
421 vfs_deleteopt(mp->mnt_optnew, "snapshot");
422 vfs_deleteopt(mp->mnt_opt, "snapshot");
425 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
426 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
428 * Once we have set the restricted PID, do not
429 * persist "fsckpid" in the options list.
431 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
432 vfs_deleteopt(mp->mnt_opt, "fsckpid");
433 if (mp->mnt_flag & MNT_UPDATE) {
434 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
435 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
437 "Checker enable: Must be read-only");
440 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
442 "Checker enable: Must be read-only");
445 /* Set to -1 if we are done */
450 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
451 if (mntorflags & MNT_ACLS) {
453 "\"acls\" and \"nfsv4acls\" options "
454 "are mutually exclusive");
457 mntorflags |= MNT_NFS4ACLS;
461 mp->mnt_kern_flag &= ~MNTK_FPLOOKUP;
462 mp->mnt_flag |= mntorflags;
465 * If updating, check whether changing from read-only to
466 * read/write; if there is no device name, that's all we do.
468 if (mp->mnt_flag & MNT_UPDATE) {
471 odevvp = ump->um_odevvp;
472 devvp = ump->um_devvp;
473 if (fsckpid == -1 && ump->um_fsckpid > 0) {
474 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
475 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
479 * Return to normal read-only mode.
481 error = g_access(ump->um_cp, 0, -1, 0);
485 if (fs->fs_ronly == 0 &&
486 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
488 * Flush any dirty data and suspend filesystem.
490 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
492 error = vfs_write_suspend_umnt(mp);
497 if (MOUNTEDSOFTDEP(mp)) {
499 mp->mnt_flag &= ~MNT_SOFTDEP;
501 mounted_softdep = true;
503 mounted_softdep = false;
506 * Check for and optionally get rid of files open
510 if (mp->mnt_flag & MNT_FORCE)
512 if (mounted_softdep) {
513 error = softdep_flushfiles(mp, flags, td);
515 error = ffs_flushfiles(mp, flags, td);
519 if (mounted_softdep) {
521 mp->mnt_flag |= MNT_SOFTDEP;
524 vfs_write_resume(mp, 0);
528 if (fs->fs_pendingblocks != 0 ||
529 fs->fs_pendinginodes != 0) {
530 printf("WARNING: %s Update error: blocks %jd "
531 "files %d\n", fs->fs_fsmnt,
532 (intmax_t)fs->fs_pendingblocks,
533 fs->fs_pendinginodes);
534 fs->fs_pendingblocks = 0;
535 fs->fs_pendinginodes = 0;
537 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
539 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
542 if (mounted_softdep) {
544 mp->mnt_flag |= MNT_SOFTDEP;
547 vfs_write_resume(mp, 0);
554 * Drop our write and exclusive access.
556 g_access(ump->um_cp, 0, -1, -1);
559 mp->mnt_flag |= MNT_RDONLY;
562 * Allow the writers to note that filesystem
565 vfs_write_resume(mp, 0);
567 if ((mp->mnt_flag & MNT_RELOAD) &&
568 (error = ffs_reload(mp, td, 0)) != 0)
571 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
573 * If we are running a checker, do not allow upgrade.
575 if (ump->um_fsckpid > 0) {
577 "Active checker, cannot upgrade to write");
581 * If upgrade to read-write by non-root, then verify
582 * that user has necessary permissions on the device.
584 vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
585 error = VOP_ACCESS(odevvp, VREAD | VWRITE,
588 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
593 fs->fs_flags &= ~FS_UNCLEAN;
594 if (fs->fs_clean == 0) {
595 fs->fs_flags |= FS_UNCLEAN;
596 if ((mp->mnt_flag & MNT_FORCE) ||
598 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
599 (fs->fs_flags & FS_DOSOFTDEP))) {
600 printf("WARNING: %s was not properly "
601 "dismounted\n", fs->fs_fsmnt);
604 "R/W mount of %s denied. %s.%s",
606 "Filesystem is not clean - run fsck",
607 (fs->fs_flags & FS_SUJ) == 0 ? "" :
608 " Forced mount will invalidate"
609 " journal contents");
615 * Request exclusive write access.
617 error = g_access(ump->um_cp, 0, 1, 1);
621 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
623 error = vfs_write_suspend_umnt(mp);
628 saved_mnt_flag = MNT_RDONLY;
629 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
631 saved_mnt_flag |= MNT_ASYNC;
632 mp->mnt_flag &= ~saved_mnt_flag;
634 fs->fs_mtime = time_second;
635 /* check to see if we need to start softdep */
636 if ((fs->fs_flags & FS_DOSOFTDEP) &&
637 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
640 mp->mnt_flag |= saved_mnt_flag;
642 vfs_write_resume(mp, 0);
646 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
649 mp->mnt_flag |= saved_mnt_flag;
651 vfs_write_resume(mp, 0);
654 if (fs->fs_snapinum[0] != 0)
655 ffs_snapshot_mount(mp);
656 vfs_write_resume(mp, 0);
659 * Soft updates is incompatible with "async",
660 * so if we are doing softupdates stop the user
661 * from setting the async flag in an update.
662 * Softdep_mount() clears it in an initial mount
665 if (MOUNTEDSOFTDEP(mp)) {
666 /* XXX: Reset too late ? */
668 mp->mnt_flag &= ~MNT_ASYNC;
672 * Keep MNT_ACLS flag if it is stored in superblock.
674 if ((fs->fs_flags & FS_ACLS) != 0) {
675 /* XXX: Set too late ? */
677 mp->mnt_flag |= MNT_ACLS;
681 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
682 /* XXX: Set too late ? */
684 mp->mnt_flag |= MNT_NFS4ACLS;
688 * If this is a request from fsck to clean up the filesystem,
689 * then allow the specified pid to proceed.
692 if (ump->um_fsckpid != 0) {
694 "Active checker already running on %s",
698 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
699 ("soft updates enabled on read-only file system"));
702 * Request write access.
704 error = g_access(ump->um_cp, 0, 1, 0);
708 "Checker activation failed on %s",
712 ump->um_fsckpid = fsckpid;
713 if (fs->fs_snapinum[0] != 0)
714 ffs_snapshot_mount(mp);
715 fs->fs_mtime = time_second;
718 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
722 * If this is a snapshot request, take the snapshot.
724 if (mp->mnt_flag & MNT_SNAPSHOT)
725 return (ffs_snapshot(mp, fspec));
728 * Must not call namei() while owning busy ref.
734 * Not an update, or updating the name: look up the name
735 * and verify that it refers to a sensible disk device.
737 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
739 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
741 * Unmount does not start if MNT_UPDATE is set. Mount
742 * update busies mp before setting MNT_UPDATE. We
743 * must be able to retain our busy ref succesfully,
746 error1 = vfs_busy(mp, MBF_NOWAIT);
751 NDFREE(&ndp, NDF_ONLY_PNBUF);
753 if (!vn_isdisk_error(devvp, &error)) {
759 * If mount by non-root, then verify that user has necessary
760 * permissions on the device.
763 if ((mp->mnt_flag & MNT_RDONLY) == 0)
765 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
767 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
773 if (mp->mnt_flag & MNT_UPDATE) {
777 * If it's not the same vnode, or at least the same device
778 * then it's not correct.
781 if (devvp->v_rdev != ump->um_devvp->v_rdev)
782 error = EINVAL; /* needs translation */
790 * We need the name for the mount point (also used for
791 * "last mounted on") copied in. If an error occurs,
792 * the mount point is discarded by the upper level code.
793 * Note that vfs_mount_alloc() populates f_mntonname for us.
795 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
800 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
801 ("soft updates enabled on read-only file system"));
806 * Request write access.
808 error = g_access(ump->um_cp, 0, 1, 0);
811 printf("WARNING: %s: Checker activation "
812 "failed\n", fs->fs_fsmnt);
814 ump->um_fsckpid = fsckpid;
815 if (fs->fs_snapinum[0] != 0)
816 ffs_snapshot_mount(mp);
817 fs->fs_mtime = time_second;
819 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
826 * This is racy versus lookup, see ufs_fplookup_vexec for details.
828 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) != 0)
829 panic("MNTK_FPLOOKUP set on mount %p when it should not be", mp);
830 if ((mp->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS | MNT_UNION)) == 0)
831 mp->mnt_kern_flag |= MNTK_FPLOOKUP;
834 vfs_mountedfrom(mp, fspec);
839 * Compatibility with old mount system call.
843 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
845 struct ufs_args args;
850 error = copyin(data, &args, sizeof args);
854 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
855 ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
856 error = kernel_mount(ma, flags);
862 * Reload all incore data for a filesystem (used after running fsck on
863 * the root filesystem and finding things to fix). If the 'force' flag
864 * is 0, the filesystem must be mounted read-only.
866 * Things to do to update the mount:
867 * 1) invalidate all cached meta-data.
868 * 2) re-read superblock from disk.
869 * 3) re-read summary information from disk.
870 * 4) invalidate all inactive vnodes.
871 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
872 * writers, if requested.
873 * 6) invalidate all cached file data.
874 * 7) re-read inode data for all active vnodes.
877 ffs_reload(struct mount *mp, struct thread *td, int flags)
879 struct vnode *vp, *mvp, *devvp;
883 struct fs *fs, *newfs;
884 struct ufsmount *ump;
885 ufs2_daddr_t sblockloc;
893 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
900 * Step 1: invalidate all cached meta-data.
902 devvp = VFSTOUFS(mp)->um_devvp;
903 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
904 if (vinvalbuf(devvp, 0, 0, 0) != 0)
905 panic("ffs_reload: dirty1");
909 * Step 2: re-read superblock from disk.
911 fs = VFSTOUFS(mp)->um_fs;
912 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
915 newfs = (struct fs *)bp->b_data;
916 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
917 newfs->fs_magic != FS_UFS2_MAGIC) ||
918 newfs->fs_bsize > MAXBSIZE ||
919 newfs->fs_bsize < sizeof(struct fs)) {
921 return (EIO); /* XXX needs translation */
924 * Preserve the summary information, read-only status, and
925 * superblock location by copying these fields into our new
926 * superblock before using it to update the existing superblock.
928 newfs->fs_si = fs->fs_si;
929 newfs->fs_ronly = fs->fs_ronly;
930 sblockloc = fs->fs_sblockloc;
931 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
933 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
934 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
936 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
937 printf("WARNING: %s: reload pending error: blocks %jd "
938 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
939 fs->fs_pendinginodes);
940 fs->fs_pendingblocks = 0;
941 fs->fs_pendinginodes = 0;
946 * Step 3: re-read summary information from disk.
948 size = fs->fs_cssize;
949 blks = howmany(size, fs->fs_fsize);
950 if (fs->fs_contigsumsize > 0)
951 size += fs->fs_ncg * sizeof(int32_t);
952 size += fs->fs_ncg * sizeof(u_int8_t);
953 free(fs->fs_csp, M_UFSMNT);
954 space = malloc(size, M_UFSMNT, M_WAITOK);
956 for (i = 0; i < blks; i += fs->fs_frag) {
958 if (i + fs->fs_frag > blks)
959 size = (blks - i) * fs->fs_fsize;
960 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
964 bcopy(bp->b_data, space, (u_int)size);
965 space = (char *)space + size;
969 * We no longer know anything about clusters per cylinder group.
971 if (fs->fs_contigsumsize > 0) {
972 fs->fs_maxcluster = lp = space;
973 for (i = 0; i < fs->fs_ncg; i++)
974 *lp++ = fs->fs_contigsumsize;
977 size = fs->fs_ncg * sizeof(u_int8_t);
978 fs->fs_contigdirs = (u_int8_t *)space;
979 bzero(fs->fs_contigdirs, size);
980 if ((flags & FFSR_UNSUSPEND) != 0) {
982 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
983 wakeup(&mp->mnt_flag);
988 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
992 if (vp->v_type == VNON) {
997 * Step 4: invalidate all cached file data.
999 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
1000 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1003 if (vinvalbuf(vp, 0, 0, 0))
1004 panic("ffs_reload: dirty2");
1006 * Step 5: re-read inode data for all active vnodes.
1010 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
1011 (int)fs->fs_bsize, NOCRED, &bp);
1014 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1017 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
1020 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1023 ip->i_effnlink = ip->i_nlink;
1031 * Common code for mount and mountroot
1034 ffs_mountfs(odevvp, mp, td)
1035 struct vnode *odevvp;
1039 struct ufsmount *ump;
1042 int error, i, len, ronly;
1044 struct g_consumer *cp;
1046 struct vnode *devvp;
1047 struct fsfail_task *etp;
1048 int candelete, canspeedup;
1053 cred = td ? td->td_ucred : NOCRED;
1054 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1056 devvp = mntfs_allocvp(mp, odevvp);
1058 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
1059 dev = devvp->v_rdev;
1060 KASSERT(dev->si_snapdata == NULL, ("non-NULL snapshot data"));
1061 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
1062 (uintptr_t)mp) == 0) {
1063 mntfs_freevp(devvp);
1067 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
1068 g_topology_unlock();
1070 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1071 mntfs_freevp(devvp);
1075 devvp->v_bufobj.bo_ops = &ffs_ops;
1076 BO_LOCK(&odevvp->v_bufobj);
1077 odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
1078 BO_UNLOCK(&odevvp->v_bufobj);
1079 if (dev->si_iosize_max != 0)
1080 mp->mnt_iosize_max = dev->si_iosize_max;
1081 if (mp->mnt_iosize_max > maxphys)
1082 mp->mnt_iosize_max = maxphys;
1083 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
1086 "Invalid sectorsize %d for superblock size %d",
1087 cp->provider->sectorsize, SBLOCKSIZE);
1090 /* fetch the superblock and summary information */
1092 if ((mp->mnt_flag & MNT_ROOTFS) != 0)
1093 loc = STDSB_NOHASHFAIL;
1094 if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
1096 fs->fs_flags &= ~FS_UNCLEAN;
1097 if (fs->fs_clean == 0) {
1098 fs->fs_flags |= FS_UNCLEAN;
1099 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
1100 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
1101 (fs->fs_flags & FS_DOSOFTDEP))) {
1102 printf("WARNING: %s was not properly dismounted\n",
1105 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
1106 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
1107 (fs->fs_flags & FS_SUJ) == 0 ? "" :
1108 " Forced mount will invalidate journal contents");
1112 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
1113 (mp->mnt_flag & MNT_FORCE)) {
1114 printf("WARNING: %s: lost blocks %jd files %d\n",
1115 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1116 fs->fs_pendinginodes);
1117 fs->fs_pendingblocks = 0;
1118 fs->fs_pendinginodes = 0;
1121 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1122 printf("WARNING: %s: mount pending error: blocks %jd "
1123 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1124 fs->fs_pendinginodes);
1125 fs->fs_pendingblocks = 0;
1126 fs->fs_pendinginodes = 0;
1128 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
1131 * Get journal provider name.
1134 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
1135 if (g_io_getattr("GJOURNAL::provider", cp, &len,
1136 mp->mnt_gjprovider) == 0) {
1137 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
1138 M_UFSMNT, M_WAITOK);
1140 mp->mnt_flag |= MNT_GJOURNAL;
1143 printf("WARNING: %s: GJOURNAL flag on fs "
1144 "but no gjournal provider below\n",
1145 mp->mnt_stat.f_mntonname);
1146 free(mp->mnt_gjprovider, M_UFSMNT);
1147 mp->mnt_gjprovider = NULL;
1150 printf("WARNING: %s: GJOURNAL flag on fs but no "
1151 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
1154 mp->mnt_gjprovider = NULL;
1156 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
1158 ump->um_bo = &devvp->v_bufobj;
1160 if (fs->fs_magic == FS_UFS1_MAGIC) {
1161 ump->um_fstype = UFS1;
1162 ump->um_balloc = ffs_balloc_ufs1;
1164 ump->um_fstype = UFS2;
1165 ump->um_balloc = ffs_balloc_ufs2;
1167 ump->um_blkatoff = ffs_blkatoff;
1168 ump->um_truncate = ffs_truncate;
1169 ump->um_update = ffs_update;
1170 ump->um_valloc = ffs_valloc;
1171 ump->um_vfree = ffs_vfree;
1172 ump->um_ifree = ffs_ifree;
1173 ump->um_rdonly = ffs_rdonly;
1174 ump->um_snapgone = ffs_snapgone;
1175 if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
1176 ump->um_check_blkno = ffs_check_blkno;
1178 ump->um_check_blkno = NULL;
1179 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
1180 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
1181 fs->fs_ronly = ronly;
1182 fs->fs_active = NULL;
1184 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
1185 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
1187 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
1188 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
1193 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
1195 mp->mnt_flag |= MNT_LOCAL;
1197 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
1200 mp->mnt_flag |= MNT_MULTILABEL;
1203 printf("WARNING: %s: multilabel flag on fs but "
1204 "no MAC support\n", mp->mnt_stat.f_mntonname);
1207 if ((fs->fs_flags & FS_ACLS) != 0) {
1211 if (mp->mnt_flag & MNT_NFS4ACLS)
1212 printf("WARNING: %s: ACLs flag on fs conflicts with "
1213 "\"nfsv4acls\" mount option; option ignored\n",
1214 mp->mnt_stat.f_mntonname);
1215 mp->mnt_flag &= ~MNT_NFS4ACLS;
1216 mp->mnt_flag |= MNT_ACLS;
1220 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
1221 mp->mnt_stat.f_mntonname);
1224 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
1228 if (mp->mnt_flag & MNT_ACLS)
1229 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1230 "with \"acls\" mount option; option ignored\n",
1231 mp->mnt_stat.f_mntonname);
1232 mp->mnt_flag &= ~MNT_ACLS;
1233 mp->mnt_flag |= MNT_NFS4ACLS;
1237 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1238 "ACLs support\n", mp->mnt_stat.f_mntonname);
1241 if ((fs->fs_flags & FS_TRIM) != 0) {
1243 if (g_io_getattr("GEOM::candelete", cp, &len,
1246 ump->um_flags |= UM_CANDELETE;
1248 printf("WARNING: %s: TRIM flag on fs but disk "
1249 "does not support TRIM\n",
1250 mp->mnt_stat.f_mntonname);
1252 printf("WARNING: %s: TRIM flag on fs but disk does "
1253 "not confirm that it supports TRIM\n",
1254 mp->mnt_stat.f_mntonname);
1256 if (((ump->um_flags) & UM_CANDELETE) != 0) {
1257 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1258 taskqueue_thread_enqueue, &ump->um_trim_tq);
1259 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1260 "%s trim", mp->mnt_stat.f_mntonname);
1261 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1262 &ump->um_trimlisthashsize);
1267 if (g_io_getattr("GEOM::canspeedup", cp, &len, &canspeedup) == 0) {
1269 ump->um_flags |= UM_CANSPEEDUP;
1272 ump->um_mountp = mp;
1274 ump->um_devvp = devvp;
1275 ump->um_odevvp = odevvp;
1276 ump->um_nindir = fs->fs_nindir;
1277 ump->um_bptrtodb = fs->fs_fsbtodb;
1278 ump->um_seqinc = fs->fs_frag;
1279 for (i = 0; i < MAXQUOTAS; i++)
1280 ump->um_quotas[i] = NULLVP;
1282 ufs_extattr_uepm_init(&ump->um_extattr);
1285 * Set FS local "last mounted on" information (NULL pad)
1287 bzero(fs->fs_fsmnt, MAXMNTLEN);
1288 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1289 mp->mnt_stat.f_iosize = fs->fs_bsize;
1291 if (mp->mnt_flag & MNT_ROOTFS) {
1293 * Root mount; update timestamp in mount structure.
1294 * this will be used by the common root mount code
1295 * to update the system clock.
1297 mp->mnt_time = fs->fs_time;
1301 fs->fs_mtime = time_second;
1302 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1303 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1304 ffs_flushfiles(mp, FORCECLOSE, td);
1307 if (fs->fs_snapinum[0] != 0)
1308 ffs_snapshot_mount(mp);
1311 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1314 * Initialize filesystem state information in mount struct.
1317 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1318 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1321 #ifdef UFS_EXTATTR_AUTOSTART
1324 * Auto-starting does the following:
1325 * - check for /.attribute in the fs, and extattr_start if so
1326 * - for each file in .attribute, enable that file with
1327 * an attribute of the same name.
1328 * Not clear how to report errors -- probably eat them.
1329 * This would all happen while the filesystem was busy/not
1330 * available, so would effectively be "atomic".
1332 (void) ufs_extattr_autostart(mp, td);
1333 #endif /* !UFS_EXTATTR_AUTOSTART */
1334 #endif /* !UFS_EXTATTR */
1335 etp = malloc(sizeof *ump->um_fsfail_task, M_UFSMNT, M_WAITOK | M_ZERO);
1336 etp->fsid = mp->mnt_stat.f_fsid;
1337 ump->um_fsfail_task = etp;
1341 free(fs->fs_csp, M_UFSMNT);
1342 free(fs->fs_si, M_UFSMNT);
1348 g_topology_unlock();
1351 mtx_destroy(UFS_MTX(ump));
1352 if (mp->mnt_gjprovider != NULL) {
1353 free(mp->mnt_gjprovider, M_UFSMNT);
1354 mp->mnt_gjprovider = NULL;
1356 free(ump, M_UFSMNT);
1357 mp->mnt_data = NULL;
1359 BO_LOCK(&odevvp->v_bufobj);
1360 odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1361 BO_UNLOCK(&odevvp->v_bufobj);
1362 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1363 mntfs_freevp(devvp);
1369 * A read function for use by filesystem-layer routines.
1372 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1377 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1378 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1379 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1382 bcopy(bp->b_data, *bufp, size);
1383 bp->b_flags |= B_INVAL | B_NOCACHE;
1388 static int bigcgs = 0;
1389 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1392 * Sanity checks for loading old filesystem superblocks.
1393 * See ffs_oldfscompat_write below for unwound actions.
1395 * XXX - Parts get retired eventually.
1396 * Unfortunately new bits get added.
1399 ffs_oldfscompat_read(fs, ump, sblockloc)
1401 struct ufsmount *ump;
1402 ufs2_daddr_t sblockloc;
1407 * If not yet done, update fs_flags location and value of fs_sblockloc.
1409 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1410 fs->fs_flags = fs->fs_old_flags;
1411 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1412 fs->fs_sblockloc = sblockloc;
1415 * If not yet done, update UFS1 superblock with new wider fields.
1417 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1418 fs->fs_maxbsize = fs->fs_bsize;
1419 fs->fs_time = fs->fs_old_time;
1420 fs->fs_size = fs->fs_old_size;
1421 fs->fs_dsize = fs->fs_old_dsize;
1422 fs->fs_csaddr = fs->fs_old_csaddr;
1423 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1424 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1425 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1426 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1428 if (fs->fs_magic == FS_UFS1_MAGIC &&
1429 fs->fs_old_inodefmt < FS_44INODEFMT) {
1430 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1431 fs->fs_qbmask = ~fs->fs_bmask;
1432 fs->fs_qfmask = ~fs->fs_fmask;
1434 if (fs->fs_magic == FS_UFS1_MAGIC) {
1435 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1436 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1437 if (fs->fs_maxfilesize > maxfilesize)
1438 fs->fs_maxfilesize = maxfilesize;
1440 /* Compatibility for old filesystems */
1441 if (fs->fs_avgfilesize <= 0)
1442 fs->fs_avgfilesize = AVFILESIZ;
1443 if (fs->fs_avgfpdir <= 0)
1444 fs->fs_avgfpdir = AFPDIR;
1446 fs->fs_save_cgsize = fs->fs_cgsize;
1447 fs->fs_cgsize = fs->fs_bsize;
1452 * Unwinding superblock updates for old filesystems.
1453 * See ffs_oldfscompat_read above for details.
1455 * XXX - Parts get retired eventually.
1456 * Unfortunately new bits get added.
1459 ffs_oldfscompat_write(fs, ump)
1461 struct ufsmount *ump;
1465 * Copy back UFS2 updated fields that UFS1 inspects.
1467 if (fs->fs_magic == FS_UFS1_MAGIC) {
1468 fs->fs_old_time = fs->fs_time;
1469 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1470 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1471 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1472 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1473 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1476 fs->fs_cgsize = fs->fs_save_cgsize;
1477 fs->fs_save_cgsize = 0;
1482 * unmount system call
1485 ffs_unmount(mp, mntflags)
1490 struct ufsmount *ump = VFSTOUFS(mp);
1492 int error, flags, susp;
1500 if (mntflags & MNT_FORCE)
1501 flags |= FORCECLOSE;
1502 susp = fs->fs_ronly == 0;
1504 if ((error = ufs_extattr_stop(mp, td))) {
1505 if (error != EOPNOTSUPP)
1506 printf("WARNING: unmount %s: ufs_extattr_stop "
1507 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1511 ufs_extattr_uepm_destroy(&ump->um_extattr);
1516 error = vfs_write_suspend_umnt(mp);
1520 if (MOUNTEDSOFTDEP(mp))
1521 error = softdep_flushfiles(mp, flags, td);
1523 error = ffs_flushfiles(mp, flags, td);
1524 if (error != 0 && !ffs_fsfail_cleanup(ump, error))
1528 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1529 printf("WARNING: unmount %s: pending error: blocks %jd "
1530 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1531 fs->fs_pendinginodes);
1532 fs->fs_pendingblocks = 0;
1533 fs->fs_pendinginodes = 0;
1536 if (MOUNTEDSOFTDEP(mp))
1537 softdep_unmount(mp);
1538 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1539 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1540 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1541 if (ffs_fsfail_cleanup(ump, error))
1543 if (error != 0 && !ffs_fsfail_cleanup(ump, error)) {
1549 vfs_write_resume(mp, VR_START_WRITE);
1550 if (ump->um_trim_tq != NULL) {
1551 while (ump->um_trim_inflight != 0)
1552 pause("ufsutr", hz);
1553 taskqueue_drain_all(ump->um_trim_tq);
1554 taskqueue_free(ump->um_trim_tq);
1555 free (ump->um_trimhash, M_TRIM);
1558 if (ump->um_fsckpid > 0) {
1560 * Return to normal read-only mode.
1562 error = g_access(ump->um_cp, 0, -1, 0);
1563 ump->um_fsckpid = 0;
1565 g_vfs_close(ump->um_cp);
1566 g_topology_unlock();
1567 BO_LOCK(&ump->um_odevvp->v_bufobj);
1568 ump->um_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1569 BO_UNLOCK(&ump->um_odevvp->v_bufobj);
1570 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1571 mntfs_freevp(ump->um_devvp);
1572 vrele(ump->um_odevvp);
1573 dev_rel(ump->um_dev);
1574 mtx_destroy(UFS_MTX(ump));
1575 if (mp->mnt_gjprovider != NULL) {
1576 free(mp->mnt_gjprovider, M_UFSMNT);
1577 mp->mnt_gjprovider = NULL;
1579 free(fs->fs_csp, M_UFSMNT);
1580 free(fs->fs_si, M_UFSMNT);
1582 if (ump->um_fsfail_task != NULL)
1583 free(ump->um_fsfail_task, M_UFSMNT);
1584 free(ump, M_UFSMNT);
1585 mp->mnt_data = NULL;
1587 mp->mnt_flag &= ~MNT_LOCAL;
1589 if (td->td_su == mp) {
1597 vfs_write_resume(mp, VR_START_WRITE);
1601 ufs_extattr_uepm_init(&ump->um_extattr);
1602 #ifdef UFS_EXTATTR_AUTOSTART
1603 (void) ufs_extattr_autostart(mp, td);
1612 * Flush out all the files in a filesystem.
1615 ffs_flushfiles(mp, flags, td)
1620 struct ufsmount *ump;
1626 if (mp->mnt_flag & MNT_QUOTA) {
1628 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1631 for (i = 0; i < MAXQUOTAS; i++) {
1632 error = quotaoff(td, mp, i);
1634 if ((flags & EARLYFLUSH) == 0)
1642 * Here we fall through to vflush again to ensure that
1643 * we have gotten rid of all the system vnodes, unless
1644 * quotas must not be closed.
1648 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1649 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1650 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1652 ffs_snapshot_unmount(mp);
1653 flags |= FORCECLOSE;
1655 * Here we fall through to vflush again to ensure
1656 * that we have gotten rid of all the system vnodes.
1661 * Do not close system files if quotas were not closed, to be
1662 * able to sync the remaining dquots. The freeblks softupdate
1663 * workitems might hold a reference on a dquot, preventing
1664 * quotaoff() from completing. Next round of
1665 * softdep_flushworklist() iteration should process the
1666 * blockers, allowing the next run of quotaoff() to finally
1667 * flush held dquots.
1669 * Otherwise, flush all the files.
1671 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1675 * Flush filesystem metadata.
1677 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1678 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1679 VOP_UNLOCK(ump->um_devvp);
1684 * Get filesystem statistics.
1691 struct ufsmount *ump;
1696 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1697 panic("ffs_statfs");
1698 sbp->f_version = STATFS_VERSION;
1699 sbp->f_bsize = fs->fs_fsize;
1700 sbp->f_iosize = fs->fs_bsize;
1701 sbp->f_blocks = fs->fs_dsize;
1703 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1704 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1705 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1706 dbtofsb(fs, fs->fs_pendingblocks);
1707 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1708 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1710 sbp->f_namemax = UFS_MAXNAMLEN;
1715 sync_doupdate(struct inode *ip)
1718 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1723 ffs_sync_lazy_filter(struct vnode *vp, void *arg __unused)
1728 * Flags are safe to access because ->v_data invalidation
1729 * is held off by listmtx.
1731 if (vp->v_type == VNON)
1734 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0)
1740 * For a lazy sync, we only care about access times, quotas and the
1741 * superblock. Other filesystem changes are already converted to
1742 * cylinder group blocks or inode blocks updates and are written to
1749 struct vnode *mvp, *vp;
1752 int allerror, error;
1756 if ((mp->mnt_flag & MNT_NOATIME) != 0) {
1762 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, ffs_sync_lazy_filter, NULL) {
1763 if (vp->v_type == VNON) {
1770 * The IN_ACCESS flag is converted to IN_MODIFIED by
1771 * ufs_close() and ufs_getattr() by the calls to
1772 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1773 * Test also all the other timestamp flags too, to pick up
1774 * any other cases that could be missed.
1776 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1780 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK)) != 0)
1785 if (sync_doupdate(ip))
1786 error = ffs_update(vp, 0);
1792 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1793 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1799 * Go through the disk queues to initiate sandbagged IO;
1800 * go through the inodes to write those that have been modified;
1801 * initiate the writing of the super block if it has been modified.
1803 * Note: we are always called with the filesystem marked busy using
1807 ffs_sync(mp, waitfor)
1811 struct vnode *mvp, *vp, *devvp;
1814 struct ufsmount *ump = VFSTOUFS(mp);
1816 int error, count, lockreq, allerror = 0;
1819 int secondary_writes;
1820 int secondary_accwrites;
1822 int softdep_accdeps;
1829 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1830 panic("%s: ffs_sync: modification on read-only filesystem",
1832 if (waitfor == MNT_LAZY) {
1834 return (ffs_sync_lazy(mp));
1835 waitfor = MNT_NOWAIT;
1839 * Write back each (modified) inode.
1841 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1842 if (waitfor == MNT_SUSPEND) {
1846 if (waitfor == MNT_WAIT)
1847 lockreq = LK_EXCLUSIVE;
1848 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1850 /* Grab snapshot of secondary write counts */
1852 secondary_writes = mp->mnt_secondary_writes;
1853 secondary_accwrites = mp->mnt_secondary_accwrites;
1856 /* Grab snapshot of softdep dependency counts */
1857 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1859 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1861 * Depend on the vnode interlock to keep things stable enough
1862 * for a quick test. Since there might be hundreds of
1863 * thousands of vnodes, we cannot afford even a subroutine
1864 * call unless there's a good chance that we have work to do.
1866 if (vp->v_type == VNON) {
1872 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1873 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1877 if ((error = vget(vp, lockreq)) != 0) {
1878 if (error == ENOENT || error == ENOLCK) {
1879 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1888 error = ffs_syncvnode(vp, waitfor, 0);
1889 if (error == ERELOOKUP)
1898 * Force stale filesystem control information to be flushed.
1900 if (waitfor == MNT_WAIT || rebooting) {
1901 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1903 if (ffs_fsfail_cleanup(ump, allerror))
1905 /* Flushed work items may create new vnodes to clean */
1906 if (allerror == 0 && count)
1910 devvp = ump->um_devvp;
1911 bo = &devvp->v_bufobj;
1913 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1915 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1916 error = VOP_FSYNC(devvp, waitfor, td);
1918 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1919 error = ffs_sbupdate(ump, waitfor, 0);
1922 if (ffs_fsfail_cleanup(ump, allerror))
1924 if (allerror == 0 && waitfor == MNT_WAIT)
1926 } else if (suspend != 0) {
1927 if (softdep_check_suspend(mp,
1932 secondary_accwrites) != 0) {
1934 goto loop; /* More work needed */
1936 mtx_assert(MNT_MTX(mp), MA_OWNED);
1937 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1943 * Write back modified superblock.
1945 if (fs->fs_fmod != 0 &&
1946 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1948 if (ffs_fsfail_cleanup(ump, allerror))
1954 ffs_vget(mp, ino, flags, vpp)
1960 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1964 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1973 struct ufsmount *ump;
1979 MPASS((ffs_flags & (FFSV_REPLACE | FFSV_REPLACE_DOOMED)) == 0 ||
1980 (flags & LK_EXCLUSIVE) != 0);
1982 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1986 if ((ffs_flags & FFSV_REPLACE) == 0 ||
1987 ((ffs_flags & FFSV_REPLACE_DOOMED) == 0 ||
1988 !VN_IS_DOOMED(*vpp)))
1995 * We must promote to an exclusive lock for vnode creation. This
1996 * can happen if lookup is passed LOCKSHARED.
1998 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1999 flags &= ~LK_TYPE_MASK;
2000 flags |= LK_EXCLUSIVE;
2004 * We do not lock vnode creation as it is believed to be too
2005 * expensive for such rare case as simultaneous creation of vnode
2006 * for same ino by different processes. We just allow them to race
2007 * and check later to decide who wins. Let the race begin!
2012 ip = uma_zalloc_smr(uma_inode, M_WAITOK | M_ZERO);
2014 /* Allocate a new vnode/inode. */
2015 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
2016 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
2019 uma_zfree_smr(uma_inode, ip);
2023 * FFS supports recursive locking.
2025 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
2028 vp->v_bufobj.bo_bsize = fs->fs_bsize;
2033 ip->i_nextclustercg = -1;
2034 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
2035 ip->i_mode = 0; /* ensure error cases below throw away vnode */
2036 cluster_init_vn(&ip->i_clusterw);
2038 ufs_init_trackers(ip);
2043 for (i = 0; i < MAXQUOTAS; i++)
2044 ip->i_dquot[i] = NODQUOT;
2048 if (ffs_flags & FFSV_FORCEINSMQ)
2049 vp->v_vflag |= VV_FORCEINSMQ;
2050 error = insmntque(vp, mp);
2052 uma_zfree_smr(uma_inode, ip);
2056 vp->v_vflag &= ~VV_FORCEINSMQ;
2057 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
2062 * Calls from ffs_valloc() (i.e. FFSV_REPLACE set)
2063 * operate on empty inode, which must not be found by
2064 * other threads until fully filled. Vnode for empty
2065 * inode must be not re-inserted on the hash by other
2066 * thread, after removal by us at the beginning.
2068 MPASS((ffs_flags & FFSV_REPLACE) == 0);
2072 /* Read in the disk contents for the inode, copy into the inode. */
2073 dbn = fsbtodb(fs, ino_to_fsba(fs, ino));
2074 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
2075 NULL, NULL, 0, NOCRED, 0, NULL, &bp);
2078 * The inode does not contain anything useful, so it would
2079 * be misleading to leave it on its hash chain. With mode
2080 * still zero, it will be unlinked and returned to the free
2089 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
2091 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
2092 if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
2099 if (DOINGSOFTDEP(vp) && (!fs->fs_ronly ||
2100 (ffs_flags & FFSV_FORCEINODEDEP) != 0))
2101 softdep_load_inodeblock(ip);
2103 ip->i_effnlink = ip->i_nlink;
2107 * Initialize the vnode from the inode, check for aliases.
2108 * Note that the underlying vnode may have changed.
2110 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
2120 * Finish inode initialization.
2122 if (vp->v_type != VFIFO) {
2123 /* FFS supports shared locking for all files except fifos. */
2128 * Set up a generation number for this inode if it does not
2129 * already have one. This should only happen on old filesystems.
2131 if (ip->i_gen == 0) {
2132 while (ip->i_gen == 0)
2133 ip->i_gen = arc4random();
2134 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
2135 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
2136 DIP_SET(ip, i_gen, ip->i_gen);
2140 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
2142 * If this vnode is already allocated, and we're running
2143 * multi-label, attempt to perform a label association
2144 * from the extended attributes on the inode.
2146 error = mac_vnode_associate_extattr(mp, vp);
2148 /* ufs_inactive will release ip->i_devvp ref. */
2162 * File handle to vnode
2164 * Have to be really careful about stale file handles:
2165 * - check that the inode number is valid
2166 * - for UFS2 check that the inode number is initialized
2167 * - call ffs_vget() to get the locked inode
2168 * - check for an unallocated inode (i_mode == 0)
2169 * - check that the given client host has export rights and return
2170 * those rights via. exflagsp and credanonp
2173 ffs_fhtovp(mp, fhp, flags, vpp)
2181 ufhp = (struct ufid *)fhp;
2182 return (ffs_inotovp(mp, ufhp->ufid_ino, ufhp->ufid_gen, flags,
2187 ffs_inotovp(mp, ino, gen, lflags, vpp, ffs_flags)
2195 struct ufsmount *ump;
2208 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
2212 * Need to check if inode is initialized because UFS2 does lazy
2213 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
2215 if (fs->fs_magic == FS_UFS2_MAGIC) {
2216 cg = ino_to_cg(fs, ino);
2217 error = ffs_getcg(fs, ump->um_devvp, cg, 0, &bp, &cgp);
2220 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
2227 error = ffs_vgetf(mp, ino, lflags, &nvp, ffs_flags);
2232 if (ip->i_mode == 0 || ip->i_gen != gen || ip->i_effnlink <= 0) {
2233 if (ip->i_mode == 0)
2239 vnode_create_vobject(nvp, DIP(ip, i_size), curthread);
2245 * Initialize the filesystem.
2249 struct vfsconf *vfsp;
2252 ffs_susp_initialize();
2253 softdep_initialize();
2254 return (ufs_init(vfsp));
2258 * Undo the work of ffs_init().
2262 struct vfsconf *vfsp;
2266 ret = ufs_uninit(vfsp);
2267 softdep_uninitialize();
2268 ffs_susp_uninitialize();
2269 taskqueue_drain_all(taskqueue_thread);
2274 * Structure used to pass information from ffs_sbupdate to its
2275 * helper routine ffs_use_bwrite.
2278 struct ufsmount *ump;
2286 * Write a superblock and associated information back to disk.
2289 ffs_sbupdate(ump, waitfor, suspended)
2290 struct ufsmount *ump;
2299 if (fs->fs_ronly == 1 &&
2300 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
2301 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
2302 panic("ffs_sbupdate: write read-only filesystem");
2304 * We use the superblock's buf to serialize calls to ffs_sbupdate().
2306 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
2307 (int)fs->fs_sbsize, 0, 0, 0);
2309 * Initialize info needed for write function.
2313 devfd.waitfor = waitfor;
2314 devfd.suspended = suspended;
2316 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
2320 * Write function for use by filesystem-layer routines.
2323 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
2325 struct devfd *devfdp;
2326 struct ufsmount *ump;
2335 * Writing the superblock summary information.
2337 if (loc != fs->fs_sblockloc) {
2338 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
2339 bcopy(buf, bp->b_data, (u_int)size);
2340 if (devfdp->suspended)
2341 bp->b_flags |= B_VALIDSUSPWRT;
2342 if (devfdp->waitfor != MNT_WAIT)
2344 else if ((error = bwrite(bp)) != 0)
2345 devfdp->error = error;
2349 * Writing the superblock itself. We need to do special checks for it.
2352 if (ffs_fsfail_cleanup(ump, devfdp->error))
2354 if (devfdp->error != 0) {
2356 return (devfdp->error);
2358 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
2359 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2360 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2361 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
2362 fs->fs_sblockloc = SBLOCK_UFS1;
2364 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
2365 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2366 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2367 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
2368 fs->fs_sblockloc = SBLOCK_UFS2;
2370 if (MOUNTEDSOFTDEP(ump->um_mountp))
2371 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
2372 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
2373 fs = (struct fs *)bp->b_data;
2374 ffs_oldfscompat_write(fs, ump);
2376 /* Recalculate the superblock hash */
2377 fs->fs_ckhash = ffs_calc_sbhash(fs);
2378 if (devfdp->suspended)
2379 bp->b_flags |= B_VALIDSUSPWRT;
2380 if (devfdp->waitfor != MNT_WAIT)
2382 else if ((error = bwrite(bp)) != 0)
2383 devfdp->error = error;
2384 return (devfdp->error);
2388 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2389 int attrnamespace, const char *attrname)
2393 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2396 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2402 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2405 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2406 uma_zfree(uma_ufs1, ip->i_din1);
2407 else if (ip->i_din2 != NULL)
2408 uma_zfree(uma_ufs2, ip->i_din2);
2409 uma_zfree_smr(uma_inode, ip);
2412 static int dobkgrdwrite = 1;
2413 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2414 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2417 * Complete a background write started from bwrite.
2420 ffs_backgroundwritedone(struct buf *bp)
2422 struct bufobj *bufobj;
2426 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) != 0)
2427 softdep_handle_error(bp);
2431 * Find the original buffer that we are writing.
2433 bufobj = bp->b_bufobj;
2435 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2436 panic("backgroundwritedone: lost buffer");
2439 * We should mark the cylinder group buffer origbp as
2440 * dirty, to not lose the failed write.
2442 if ((bp->b_ioflags & BIO_ERROR) != 0)
2443 origbp->b_vflags |= BV_BKGRDERR;
2446 * Process dependencies then return any unfinished ones.
2448 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2451 if (!LIST_EMPTY(&bp->b_dep))
2452 softdep_move_dependencies(bp, origbp);
2455 * This buffer is marked B_NOCACHE so when it is released
2456 * by biodone it will be tossed. Clear B_IOSTARTED in case of error.
2458 bp->b_flags |= B_NOCACHE;
2459 bp->b_flags &= ~(B_CACHE | B_IOSTARTED);
2463 * Prevent brelse() from trying to keep and re-dirtying bp on
2464 * errors. It causes b_bufobj dereference in
2465 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2468 if ((bp->b_ioflags & BIO_ERROR) != 0)
2469 bp->b_flags |= B_INVAL;
2473 * Clear the BV_BKGRDINPROG flag in the original buffer
2474 * and awaken it if it is waiting for the write to complete.
2475 * If BV_BKGRDINPROG is not set in the original buffer it must
2476 * have been released and re-instantiated - which is not legal.
2478 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2479 ("backgroundwritedone: lost buffer2"));
2480 origbp->b_vflags &= ~BV_BKGRDINPROG;
2481 if (origbp->b_vflags & BV_BKGRDWAIT) {
2482 origbp->b_vflags &= ~BV_BKGRDWAIT;
2483 wakeup(&origbp->b_xflags);
2489 * Write, release buffer on completion. (Done by iodone
2490 * if async). Do not bother writing anything if the buffer
2493 * Note that we set B_CACHE here, indicating that buffer is
2494 * fully valid and thus cacheable. This is true even of NFS
2495 * now so we set it generally. This could be set either here
2496 * or in biodone() since the I/O is synchronous. We put it
2500 ffs_bufwrite(struct buf *bp)
2505 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2506 if (bp->b_flags & B_INVAL) {
2511 if (!BUF_ISLOCKED(bp))
2512 panic("bufwrite: buffer is not busy???");
2514 * If a background write is already in progress, delay
2515 * writing this block if it is asynchronous. Otherwise
2516 * wait for the background write to complete.
2518 BO_LOCK(bp->b_bufobj);
2519 if (bp->b_vflags & BV_BKGRDINPROG) {
2520 if (bp->b_flags & B_ASYNC) {
2521 BO_UNLOCK(bp->b_bufobj);
2525 bp->b_vflags |= BV_BKGRDWAIT;
2526 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2528 if (bp->b_vflags & BV_BKGRDINPROG)
2529 panic("bufwrite: still writing");
2531 bp->b_vflags &= ~BV_BKGRDERR;
2532 BO_UNLOCK(bp->b_bufobj);
2535 * If this buffer is marked for background writing and we
2536 * do not have to wait for it, make a copy and write the
2537 * copy so as to leave this buffer ready for further use.
2539 * This optimization eats a lot of memory. If we have a page
2540 * or buffer shortfall we can't do it.
2542 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2543 (bp->b_flags & B_ASYNC) &&
2544 !vm_page_count_severe() &&
2545 !buf_dirty_count_severe()) {
2546 KASSERT(bp->b_iodone == NULL,
2547 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2549 /* get a new block */
2550 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2554 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2555 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2556 BO_LOCK(bp->b_bufobj);
2557 bp->b_vflags |= BV_BKGRDINPROG;
2558 BO_UNLOCK(bp->b_bufobj);
2560 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2561 newbp->b_lblkno = bp->b_lblkno;
2562 newbp->b_blkno = bp->b_blkno;
2563 newbp->b_offset = bp->b_offset;
2564 newbp->b_iodone = ffs_backgroundwritedone;
2565 newbp->b_flags |= B_ASYNC;
2566 newbp->b_flags &= ~B_INVAL;
2567 pbgetvp(bp->b_vp, newbp);
2571 * Move over the dependencies. If there are rollbacks,
2572 * leave the parent buffer dirtied as it will need to
2575 if (LIST_EMPTY(&bp->b_dep) ||
2576 softdep_move_dependencies(bp, newbp) == 0)
2583 * Initiate write on the copy, release the original. The
2584 * BKGRDINPROG flag prevents it from going away until
2585 * the background write completes. We have to recalculate
2586 * its check hash in case the buffer gets freed and then
2587 * reconstituted from the buffer cache during a later read.
2589 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2590 cgp = (struct cg *)bp->b_data;
2593 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2598 /* Mark the buffer clean */
2601 /* Let the normal bufwrite do the rest for us */
2604 * If we are writing a cylinder group, update its time.
2606 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2607 cgp = (struct cg *)bp->b_data;
2608 cgp->cg_old_time = cgp->cg_time = time_second;
2610 return (bufwrite(bp));
2614 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2621 * This is the bufobj strategy for the private VCHR vnodes
2622 * used by FFS to access the underlying storage device.
2623 * We override the default bufobj strategy and thus bypass
2624 * VOP_STRATEGY() for these vnodes.
2627 KASSERT(bp->b_vp == NULL || bp->b_vp->v_type != VCHR ||
2628 bp->b_vp->v_rdev == NULL ||
2629 bp->b_vp->v_rdev->si_mountpt == NULL ||
2630 VFSTOUFS(bp->b_vp->v_rdev->si_mountpt) == NULL ||
2631 vp == VFSTOUFS(bp->b_vp->v_rdev->si_mountpt)->um_devvp,
2632 ("ffs_geom_strategy() with wrong vp"));
2633 if (bp->b_iocmd == BIO_WRITE) {
2634 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2635 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2636 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2637 panic("ffs_geom_strategy: bad I/O");
2638 nocopy = bp->b_flags & B_NOCOPY;
2639 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2640 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2641 vp->v_rdev->si_snapdata != NULL) {
2642 if ((bp->b_flags & B_CLUSTER) != 0) {
2643 runningbufwakeup(bp);
2644 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2645 b_cluster.cluster_entry) {
2646 error = ffs_copyonwrite(vp, tbp);
2648 error != EOPNOTSUPP) {
2649 bp->b_error = error;
2650 bp->b_ioflags |= BIO_ERROR;
2651 bp->b_flags &= ~B_BARRIER;
2656 bp->b_runningbufspace = bp->b_bufsize;
2657 atomic_add_long(&runningbufspace,
2658 bp->b_runningbufspace);
2660 error = ffs_copyonwrite(vp, bp);
2661 if (error != 0 && error != EOPNOTSUPP) {
2662 bp->b_error = error;
2663 bp->b_ioflags |= BIO_ERROR;
2664 bp->b_flags &= ~B_BARRIER;
2671 if ((bp->b_flags & B_CLUSTER) != 0) {
2672 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2673 b_cluster.cluster_entry) {
2674 if (!LIST_EMPTY(&tbp->b_dep))
2678 if (!LIST_EMPTY(&bp->b_dep))
2684 * Check for metadata that needs check-hashes and update them.
2686 switch (bp->b_xflags & BX_FSPRIV) {
2688 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2689 ((struct cg *)bp->b_data)->cg_ckhash =
2690 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2697 printf("Check-hash write is unimplemented!!!\n");
2704 printf("multiple buffer types 0x%b\n",
2705 (u_int)(bp->b_xflags & BX_FSPRIV),
2706 PRINT_UFS_BUF_XFLAGS);
2710 if (bp->b_iocmd != BIO_READ && ffs_enxio_enable)
2711 bp->b_xflags |= BX_CVTENXIO;
2712 g_vfs_strategy(bo, bp);
2716 ffs_own_mount(const struct mount *mp)
2719 if (mp->mnt_op == &ufs_vfsops)
2727 /* defined in ffs_softdep.c */
2728 extern void db_print_ffs(struct ufsmount *ump);
2730 DB_SHOW_COMMAND(ffs, db_show_ffs)
2733 struct ufsmount *ump;
2736 ump = VFSTOUFS((struct mount *)addr);
2741 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2742 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2743 db_print_ffs(VFSTOUFS(mp));
2747 #endif /* SOFTUPDATES */