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;
385 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
387 if (uma_inode == NULL) {
388 uma_inode = uma_zcreate("FFS inode",
389 sizeof(struct inode), NULL, NULL, NULL, NULL,
391 uma_ufs1 = uma_zcreate("FFS1 dinode",
392 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
394 uma_ufs2 = uma_zcreate("FFS2 dinode",
395 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
397 VFS_SMR_ZONE_SET(uma_inode);
400 vfs_deleteopt(mp->mnt_optnew, "groupquota");
401 vfs_deleteopt(mp->mnt_optnew, "userquota");
403 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
408 if (vfs_getopt(mp->mnt_optnew, "untrusted", NULL, NULL) == 0)
409 mntorflags |= MNT_UNTRUSTED;
411 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
412 mntorflags |= MNT_ACLS;
414 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
415 mntorflags |= MNT_SNAPSHOT;
417 * Once we have set the MNT_SNAPSHOT flag, do not
418 * persist "snapshot" in the options list.
420 vfs_deleteopt(mp->mnt_optnew, "snapshot");
421 vfs_deleteopt(mp->mnt_opt, "snapshot");
424 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
425 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
427 * Once we have set the restricted PID, do not
428 * persist "fsckpid" in the options list.
430 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
431 vfs_deleteopt(mp->mnt_opt, "fsckpid");
432 if (mp->mnt_flag & MNT_UPDATE) {
433 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
434 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
436 "Checker enable: Must be read-only");
439 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
441 "Checker enable: Must be read-only");
444 /* Set to -1 if we are done */
449 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
450 if (mntorflags & MNT_ACLS) {
452 "\"acls\" and \"nfsv4acls\" options "
453 "are mutually exclusive");
456 mntorflags |= MNT_NFS4ACLS;
460 mp->mnt_kern_flag &= ~MNTK_FPLOOKUP;
461 mp->mnt_flag |= mntorflags;
464 * If updating, check whether changing from read-only to
465 * read/write; if there is no device name, that's all we do.
467 if (mp->mnt_flag & MNT_UPDATE) {
470 odevvp = ump->um_odevvp;
471 devvp = ump->um_devvp;
472 if (fsckpid == -1 && ump->um_fsckpid > 0) {
473 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
474 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
478 * Return to normal read-only mode.
480 error = g_access(ump->um_cp, 0, -1, 0);
484 if (fs->fs_ronly == 0 &&
485 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
487 * Flush any dirty data and suspend filesystem.
489 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
491 error = vfs_write_suspend_umnt(mp);
495 * Check for and optionally get rid of files open
499 if (mp->mnt_flag & MNT_FORCE)
501 if (MOUNTEDSOFTDEP(mp)) {
502 error = softdep_flushfiles(mp, flags, td);
504 error = ffs_flushfiles(mp, flags, td);
507 vfs_write_resume(mp, 0);
510 if (fs->fs_pendingblocks != 0 ||
511 fs->fs_pendinginodes != 0) {
512 printf("WARNING: %s Update error: blocks %jd "
513 "files %d\n", fs->fs_fsmnt,
514 (intmax_t)fs->fs_pendingblocks,
515 fs->fs_pendinginodes);
516 fs->fs_pendingblocks = 0;
517 fs->fs_pendinginodes = 0;
519 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
521 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
524 vfs_write_resume(mp, 0);
527 if (MOUNTEDSOFTDEP(mp))
531 * Drop our write and exclusive access.
533 g_access(ump->um_cp, 0, -1, -1);
537 mp->mnt_flag |= MNT_RDONLY;
540 * Allow the writers to note that filesystem
543 vfs_write_resume(mp, 0);
545 if ((mp->mnt_flag & MNT_RELOAD) &&
546 (error = ffs_reload(mp, td, 0)) != 0)
549 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
551 * If we are running a checker, do not allow upgrade.
553 if (ump->um_fsckpid > 0) {
555 "Active checker, cannot upgrade to write");
559 * If upgrade to read-write by non-root, then verify
560 * that user has necessary permissions on the device.
562 vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
563 error = VOP_ACCESS(odevvp, VREAD | VWRITE,
566 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
571 fs->fs_flags &= ~FS_UNCLEAN;
572 if (fs->fs_clean == 0) {
573 fs->fs_flags |= FS_UNCLEAN;
574 if ((mp->mnt_flag & MNT_FORCE) ||
576 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
577 (fs->fs_flags & FS_DOSOFTDEP))) {
578 printf("WARNING: %s was not properly "
579 "dismounted\n", fs->fs_fsmnt);
582 "R/W mount of %s denied. %s.%s",
584 "Filesystem is not clean - run fsck",
585 (fs->fs_flags & FS_SUJ) == 0 ? "" :
586 " Forced mount will invalidate"
587 " journal contents");
593 * Request exclusive write access.
595 error = g_access(ump->um_cp, 0, 1, 1);
599 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
601 error = vfs_write_suspend_umnt(mp);
606 saved_mnt_flag = MNT_RDONLY;
607 if (MOUNTEDSOFTDEP(mp) && (mp->mnt_flag &
609 saved_mnt_flag |= MNT_ASYNC;
610 mp->mnt_flag &= ~saved_mnt_flag;
612 fs->fs_mtime = time_second;
613 /* check to see if we need to start softdep */
614 if ((fs->fs_flags & FS_DOSOFTDEP) &&
615 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
618 mp->mnt_flag |= saved_mnt_flag;
620 vfs_write_resume(mp, 0);
624 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
627 mp->mnt_flag |= saved_mnt_flag;
629 vfs_write_resume(mp, 0);
632 if (fs->fs_snapinum[0] != 0)
633 ffs_snapshot_mount(mp);
634 vfs_write_resume(mp, 0);
637 * Soft updates is incompatible with "async",
638 * so if we are doing softupdates stop the user
639 * from setting the async flag in an update.
640 * Softdep_mount() clears it in an initial mount
643 if (MOUNTEDSOFTDEP(mp)) {
644 /* XXX: Reset too late ? */
646 mp->mnt_flag &= ~MNT_ASYNC;
650 * Keep MNT_ACLS flag if it is stored in superblock.
652 if ((fs->fs_flags & FS_ACLS) != 0) {
653 /* XXX: Set too late ? */
655 mp->mnt_flag |= MNT_ACLS;
659 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
660 /* XXX: Set too late ? */
662 mp->mnt_flag |= MNT_NFS4ACLS;
666 * If this is a request from fsck to clean up the filesystem,
667 * then allow the specified pid to proceed.
670 if (ump->um_fsckpid != 0) {
672 "Active checker already running on %s",
676 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
677 ("soft updates enabled on read-only file system"));
680 * Request write access.
682 error = g_access(ump->um_cp, 0, 1, 0);
686 "Checker activation failed on %s",
690 ump->um_fsckpid = fsckpid;
691 if (fs->fs_snapinum[0] != 0)
692 ffs_snapshot_mount(mp);
693 fs->fs_mtime = time_second;
696 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
700 * If this is a snapshot request, take the snapshot.
702 if (mp->mnt_flag & MNT_SNAPSHOT)
703 return (ffs_snapshot(mp, fspec));
706 * Must not call namei() while owning busy ref.
712 * Not an update, or updating the name: look up the name
713 * and verify that it refers to a sensible disk device.
715 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
717 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
719 * Unmount does not start if MNT_UPDATE is set. Mount
720 * update busies mp before setting MNT_UPDATE. We
721 * must be able to retain our busy ref succesfully,
724 error1 = vfs_busy(mp, MBF_NOWAIT);
729 NDFREE(&ndp, NDF_ONLY_PNBUF);
731 if (!vn_isdisk(devvp, &error)) {
737 * If mount by non-root, then verify that user has necessary
738 * permissions on the device.
741 if ((mp->mnt_flag & MNT_RDONLY) == 0)
743 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
745 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
751 if (mp->mnt_flag & MNT_UPDATE) {
755 * If it's not the same vnode, or at least the same device
756 * then it's not correct.
759 if (devvp->v_rdev != ump->um_devvp->v_rdev)
760 error = EINVAL; /* needs translation */
768 * We need the name for the mount point (also used for
769 * "last mounted on") copied in. If an error occurs,
770 * the mount point is discarded by the upper level code.
771 * Note that vfs_mount_alloc() populates f_mntonname for us.
773 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
778 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
779 ("soft updates enabled on read-only file system"));
784 * Request write access.
786 error = g_access(ump->um_cp, 0, 1, 0);
789 printf("WARNING: %s: Checker activation "
790 "failed\n", fs->fs_fsmnt);
792 ump->um_fsckpid = fsckpid;
793 if (fs->fs_snapinum[0] != 0)
794 ffs_snapshot_mount(mp);
795 fs->fs_mtime = time_second;
797 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
804 * This is racy versus lookup, see ufs_fplookup_vexec for details.
806 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) != 0)
807 panic("MNTK_FPLOOKUP set on mount %p when it should not be", mp);
808 if ((mp->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS | MNT_UNION)) == 0)
809 mp->mnt_kern_flag |= MNTK_FPLOOKUP;
812 vfs_mountedfrom(mp, fspec);
817 * Compatibility with old mount system call.
821 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
823 struct ufs_args args;
828 error = copyin(data, &args, sizeof args);
832 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
833 ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
834 error = kernel_mount(ma, flags);
840 * Reload all incore data for a filesystem (used after running fsck on
841 * the root filesystem and finding things to fix). If the 'force' flag
842 * is 0, the filesystem must be mounted read-only.
844 * Things to do to update the mount:
845 * 1) invalidate all cached meta-data.
846 * 2) re-read superblock from disk.
847 * 3) re-read summary information from disk.
848 * 4) invalidate all inactive vnodes.
849 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
850 * writers, if requested.
851 * 6) invalidate all cached file data.
852 * 7) re-read inode data for all active vnodes.
855 ffs_reload(struct mount *mp, struct thread *td, int flags)
857 struct vnode *vp, *mvp, *devvp;
861 struct fs *fs, *newfs;
862 struct ufsmount *ump;
863 ufs2_daddr_t sblockloc;
871 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
878 * Step 1: invalidate all cached meta-data.
880 devvp = VFSTOUFS(mp)->um_devvp;
881 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
882 if (vinvalbuf(devvp, 0, 0, 0) != 0)
883 panic("ffs_reload: dirty1");
887 * Step 2: re-read superblock from disk.
889 fs = VFSTOUFS(mp)->um_fs;
890 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
893 newfs = (struct fs *)bp->b_data;
894 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
895 newfs->fs_magic != FS_UFS2_MAGIC) ||
896 newfs->fs_bsize > MAXBSIZE ||
897 newfs->fs_bsize < sizeof(struct fs)) {
899 return (EIO); /* XXX needs translation */
902 * Preserve the summary information, read-only status, and
903 * superblock location by copying these fields into our new
904 * superblock before using it to update the existing superblock.
906 newfs->fs_si = fs->fs_si;
907 newfs->fs_ronly = fs->fs_ronly;
908 sblockloc = fs->fs_sblockloc;
909 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
911 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
912 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
914 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
915 printf("WARNING: %s: reload pending error: blocks %jd "
916 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
917 fs->fs_pendinginodes);
918 fs->fs_pendingblocks = 0;
919 fs->fs_pendinginodes = 0;
924 * Step 3: re-read summary information from disk.
926 size = fs->fs_cssize;
927 blks = howmany(size, fs->fs_fsize);
928 if (fs->fs_contigsumsize > 0)
929 size += fs->fs_ncg * sizeof(int32_t);
930 size += fs->fs_ncg * sizeof(u_int8_t);
931 free(fs->fs_csp, M_UFSMNT);
932 space = malloc(size, M_UFSMNT, M_WAITOK);
934 for (i = 0; i < blks; i += fs->fs_frag) {
936 if (i + fs->fs_frag > blks)
937 size = (blks - i) * fs->fs_fsize;
938 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
942 bcopy(bp->b_data, space, (u_int)size);
943 space = (char *)space + size;
947 * We no longer know anything about clusters per cylinder group.
949 if (fs->fs_contigsumsize > 0) {
950 fs->fs_maxcluster = lp = space;
951 for (i = 0; i < fs->fs_ncg; i++)
952 *lp++ = fs->fs_contigsumsize;
955 size = fs->fs_ncg * sizeof(u_int8_t);
956 fs->fs_contigdirs = (u_int8_t *)space;
957 bzero(fs->fs_contigdirs, size);
958 if ((flags & FFSR_UNSUSPEND) != 0) {
960 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
961 wakeup(&mp->mnt_flag);
966 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
970 if (vp->v_type == VNON) {
975 * Step 4: invalidate all cached file data.
977 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
978 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
981 if (vinvalbuf(vp, 0, 0, 0))
982 panic("ffs_reload: dirty2");
984 * Step 5: re-read inode data for all active vnodes.
988 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
989 (int)fs->fs_bsize, NOCRED, &bp);
992 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
995 if ((error = ffs_load_inode(bp, ip, fs, ip->i_number)) != 0) {
998 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1001 ip->i_effnlink = ip->i_nlink;
1009 * Common code for mount and mountroot
1012 ffs_mountfs(odevvp, mp, td)
1013 struct vnode *odevvp;
1017 struct ufsmount *ump;
1020 int error, i, len, ronly;
1022 struct g_consumer *cp;
1024 struct vnode *devvp;
1025 struct fsfail_task *etp;
1026 int candelete, canspeedup;
1031 cred = td ? td->td_ucred : NOCRED;
1032 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1034 devvp = mntfs_allocvp(mp, odevvp);
1036 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
1037 dev = devvp->v_rdev;
1038 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
1039 (uintptr_t)mp) == 0) {
1040 mntfs_freevp(devvp);
1044 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
1045 g_topology_unlock();
1047 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1048 mntfs_freevp(devvp);
1052 devvp->v_bufobj.bo_ops = &ffs_ops;
1053 BO_LOCK(&odevvp->v_bufobj);
1054 odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
1055 BO_UNLOCK(&odevvp->v_bufobj);
1056 if (dev->si_iosize_max != 0)
1057 mp->mnt_iosize_max = dev->si_iosize_max;
1058 if (mp->mnt_iosize_max > MAXPHYS)
1059 mp->mnt_iosize_max = MAXPHYS;
1060 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
1063 "Invalid sectorsize %d for superblock size %d",
1064 cp->provider->sectorsize, SBLOCKSIZE);
1067 /* fetch the superblock and summary information */
1069 if ((mp->mnt_flag & MNT_ROOTFS) != 0)
1070 loc = STDSB_NOHASHFAIL;
1071 if ((error = ffs_sbget(devvp, &fs, loc, M_UFSMNT, ffs_use_bread)) != 0)
1073 /* none of these types of check-hashes are maintained by this kernel */
1074 fs->fs_metackhash &= ~(CK_INDIR | CK_DIR);
1075 /* no support for any undefined flags */
1076 fs->fs_flags &= FS_SUPPORTED;
1077 fs->fs_flags &= ~FS_UNCLEAN;
1078 if (fs->fs_clean == 0) {
1079 fs->fs_flags |= FS_UNCLEAN;
1080 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
1081 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
1082 (fs->fs_flags & FS_DOSOFTDEP))) {
1083 printf("WARNING: %s was not properly dismounted\n",
1086 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
1087 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
1088 (fs->fs_flags & FS_SUJ) == 0 ? "" :
1089 " Forced mount will invalidate journal contents");
1093 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
1094 (mp->mnt_flag & MNT_FORCE)) {
1095 printf("WARNING: %s: lost blocks %jd files %d\n",
1096 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1097 fs->fs_pendinginodes);
1098 fs->fs_pendingblocks = 0;
1099 fs->fs_pendinginodes = 0;
1102 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1103 printf("WARNING: %s: mount pending error: blocks %jd "
1104 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1105 fs->fs_pendinginodes);
1106 fs->fs_pendingblocks = 0;
1107 fs->fs_pendinginodes = 0;
1109 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
1112 * Get journal provider name.
1115 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
1116 if (g_io_getattr("GJOURNAL::provider", cp, &len,
1117 mp->mnt_gjprovider) == 0) {
1118 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
1119 M_UFSMNT, M_WAITOK);
1121 mp->mnt_flag |= MNT_GJOURNAL;
1124 printf("WARNING: %s: GJOURNAL flag on fs "
1125 "but no gjournal provider below\n",
1126 mp->mnt_stat.f_mntonname);
1127 free(mp->mnt_gjprovider, M_UFSMNT);
1128 mp->mnt_gjprovider = NULL;
1131 printf("WARNING: %s: GJOURNAL flag on fs but no "
1132 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
1135 mp->mnt_gjprovider = NULL;
1137 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
1139 ump->um_bo = &devvp->v_bufobj;
1141 if (fs->fs_magic == FS_UFS1_MAGIC) {
1142 ump->um_fstype = UFS1;
1143 ump->um_balloc = ffs_balloc_ufs1;
1145 ump->um_fstype = UFS2;
1146 ump->um_balloc = ffs_balloc_ufs2;
1148 ump->um_blkatoff = ffs_blkatoff;
1149 ump->um_truncate = ffs_truncate;
1150 ump->um_update = ffs_update;
1151 ump->um_valloc = ffs_valloc;
1152 ump->um_vfree = ffs_vfree;
1153 ump->um_ifree = ffs_ifree;
1154 ump->um_rdonly = ffs_rdonly;
1155 ump->um_snapgone = ffs_snapgone;
1156 if ((mp->mnt_flag & MNT_UNTRUSTED) != 0)
1157 ump->um_check_blkno = ffs_check_blkno;
1159 ump->um_check_blkno = NULL;
1160 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
1161 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
1162 fs->fs_ronly = ronly;
1163 fs->fs_active = NULL;
1165 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
1166 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
1168 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
1169 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
1174 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
1176 mp->mnt_flag |= MNT_LOCAL;
1178 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
1181 mp->mnt_flag |= MNT_MULTILABEL;
1184 printf("WARNING: %s: multilabel flag on fs but "
1185 "no MAC support\n", mp->mnt_stat.f_mntonname);
1188 if ((fs->fs_flags & FS_ACLS) != 0) {
1192 if (mp->mnt_flag & MNT_NFS4ACLS)
1193 printf("WARNING: %s: ACLs flag on fs conflicts with "
1194 "\"nfsv4acls\" mount option; option ignored\n",
1195 mp->mnt_stat.f_mntonname);
1196 mp->mnt_flag &= ~MNT_NFS4ACLS;
1197 mp->mnt_flag |= MNT_ACLS;
1201 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
1202 mp->mnt_stat.f_mntonname);
1205 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
1209 if (mp->mnt_flag & MNT_ACLS)
1210 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1211 "with \"acls\" mount option; option ignored\n",
1212 mp->mnt_stat.f_mntonname);
1213 mp->mnt_flag &= ~MNT_ACLS;
1214 mp->mnt_flag |= MNT_NFS4ACLS;
1218 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1219 "ACLs support\n", mp->mnt_stat.f_mntonname);
1222 if ((fs->fs_flags & FS_TRIM) != 0) {
1224 if (g_io_getattr("GEOM::candelete", cp, &len,
1227 ump->um_flags |= UM_CANDELETE;
1229 printf("WARNING: %s: TRIM flag on fs but disk "
1230 "does not support TRIM\n",
1231 mp->mnt_stat.f_mntonname);
1233 printf("WARNING: %s: TRIM flag on fs but disk does "
1234 "not confirm that it supports TRIM\n",
1235 mp->mnt_stat.f_mntonname);
1237 if (((ump->um_flags) & UM_CANDELETE) != 0) {
1238 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1239 taskqueue_thread_enqueue, &ump->um_trim_tq);
1240 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1241 "%s trim", mp->mnt_stat.f_mntonname);
1242 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
1243 &ump->um_trimlisthashsize);
1248 if (g_io_getattr("GEOM::canspeedup", cp, &len, &canspeedup) == 0) {
1250 ump->um_flags |= UM_CANSPEEDUP;
1253 ump->um_mountp = mp;
1255 ump->um_devvp = devvp;
1256 ump->um_odevvp = odevvp;
1257 ump->um_nindir = fs->fs_nindir;
1258 ump->um_bptrtodb = fs->fs_fsbtodb;
1259 ump->um_seqinc = fs->fs_frag;
1260 for (i = 0; i < MAXQUOTAS; i++)
1261 ump->um_quotas[i] = NULLVP;
1263 ufs_extattr_uepm_init(&ump->um_extattr);
1266 * Set FS local "last mounted on" information (NULL pad)
1268 bzero(fs->fs_fsmnt, MAXMNTLEN);
1269 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1270 mp->mnt_stat.f_iosize = fs->fs_bsize;
1272 if (mp->mnt_flag & MNT_ROOTFS) {
1274 * Root mount; update timestamp in mount structure.
1275 * this will be used by the common root mount code
1276 * to update the system clock.
1278 mp->mnt_time = fs->fs_time;
1282 fs->fs_mtime = time_second;
1283 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1284 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1285 ffs_flushfiles(mp, FORCECLOSE, td);
1288 if (fs->fs_snapinum[0] != 0)
1289 ffs_snapshot_mount(mp);
1292 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1295 * Initialize filesystem state information in mount struct.
1298 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1299 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1302 #ifdef UFS_EXTATTR_AUTOSTART
1305 * Auto-starting does the following:
1306 * - check for /.attribute in the fs, and extattr_start if so
1307 * - for each file in .attribute, enable that file with
1308 * an attribute of the same name.
1309 * Not clear how to report errors -- probably eat them.
1310 * This would all happen while the filesystem was busy/not
1311 * available, so would effectively be "atomic".
1313 (void) ufs_extattr_autostart(mp, td);
1314 #endif /* !UFS_EXTATTR_AUTOSTART */
1315 #endif /* !UFS_EXTATTR */
1316 etp = malloc(sizeof *ump->um_fsfail_task, M_UFSMNT, M_WAITOK | M_ZERO);
1317 etp->fsid = mp->mnt_stat.f_fsid;
1318 ump->um_fsfail_task = etp;
1322 free(fs->fs_csp, M_UFSMNT);
1323 free(fs->fs_si, M_UFSMNT);
1329 g_topology_unlock();
1332 mtx_destroy(UFS_MTX(ump));
1333 if (mp->mnt_gjprovider != NULL) {
1334 free(mp->mnt_gjprovider, M_UFSMNT);
1335 mp->mnt_gjprovider = NULL;
1337 free(ump, M_UFSMNT);
1338 mp->mnt_data = NULL;
1340 BO_LOCK(&odevvp->v_bufobj);
1341 odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1342 BO_UNLOCK(&odevvp->v_bufobj);
1343 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1344 mntfs_freevp(devvp);
1350 * A read function for use by filesystem-layer routines.
1353 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1358 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1359 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1360 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1363 bcopy(bp->b_data, *bufp, size);
1364 bp->b_flags |= B_INVAL | B_NOCACHE;
1369 static int bigcgs = 0;
1370 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1373 * Sanity checks for loading old filesystem superblocks.
1374 * See ffs_oldfscompat_write below for unwound actions.
1376 * XXX - Parts get retired eventually.
1377 * Unfortunately new bits get added.
1380 ffs_oldfscompat_read(fs, ump, sblockloc)
1382 struct ufsmount *ump;
1383 ufs2_daddr_t sblockloc;
1388 * If not yet done, update fs_flags location and value of fs_sblockloc.
1390 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1391 fs->fs_flags = fs->fs_old_flags;
1392 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1393 fs->fs_sblockloc = sblockloc;
1396 * If not yet done, update UFS1 superblock with new wider fields.
1398 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1399 fs->fs_maxbsize = fs->fs_bsize;
1400 fs->fs_time = fs->fs_old_time;
1401 fs->fs_size = fs->fs_old_size;
1402 fs->fs_dsize = fs->fs_old_dsize;
1403 fs->fs_csaddr = fs->fs_old_csaddr;
1404 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1405 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1406 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1407 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1409 if (fs->fs_magic == FS_UFS1_MAGIC &&
1410 fs->fs_old_inodefmt < FS_44INODEFMT) {
1411 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1412 fs->fs_qbmask = ~fs->fs_bmask;
1413 fs->fs_qfmask = ~fs->fs_fmask;
1415 if (fs->fs_magic == FS_UFS1_MAGIC) {
1416 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1417 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1418 if (fs->fs_maxfilesize > maxfilesize)
1419 fs->fs_maxfilesize = maxfilesize;
1421 /* Compatibility for old filesystems */
1422 if (fs->fs_avgfilesize <= 0)
1423 fs->fs_avgfilesize = AVFILESIZ;
1424 if (fs->fs_avgfpdir <= 0)
1425 fs->fs_avgfpdir = AFPDIR;
1427 fs->fs_save_cgsize = fs->fs_cgsize;
1428 fs->fs_cgsize = fs->fs_bsize;
1433 * Unwinding superblock updates for old filesystems.
1434 * See ffs_oldfscompat_read above for details.
1436 * XXX - Parts get retired eventually.
1437 * Unfortunately new bits get added.
1440 ffs_oldfscompat_write(fs, ump)
1442 struct ufsmount *ump;
1446 * Copy back UFS2 updated fields that UFS1 inspects.
1448 if (fs->fs_magic == FS_UFS1_MAGIC) {
1449 fs->fs_old_time = fs->fs_time;
1450 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1451 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1452 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1453 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1454 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1457 fs->fs_cgsize = fs->fs_save_cgsize;
1458 fs->fs_save_cgsize = 0;
1463 * unmount system call
1466 ffs_unmount(mp, mntflags)
1471 struct ufsmount *ump = VFSTOUFS(mp);
1473 int error, flags, susp;
1481 if (mntflags & MNT_FORCE)
1482 flags |= FORCECLOSE;
1483 susp = fs->fs_ronly == 0;
1485 if ((error = ufs_extattr_stop(mp, td))) {
1486 if (error != EOPNOTSUPP)
1487 printf("WARNING: unmount %s: ufs_extattr_stop "
1488 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1492 ufs_extattr_uepm_destroy(&ump->um_extattr);
1497 error = vfs_write_suspend_umnt(mp);
1501 if (MOUNTEDSOFTDEP(mp))
1502 error = softdep_flushfiles(mp, flags, td);
1504 error = ffs_flushfiles(mp, flags, td);
1505 if (error != 0 && !ffs_fsfail_cleanup(ump, error))
1509 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1510 printf("WARNING: unmount %s: pending error: blocks %jd "
1511 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1512 fs->fs_pendinginodes);
1513 fs->fs_pendingblocks = 0;
1514 fs->fs_pendinginodes = 0;
1517 if (MOUNTEDSOFTDEP(mp))
1518 softdep_unmount(mp);
1519 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1520 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1521 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1522 if (ffs_fsfail_cleanup(ump, error))
1524 if (error != 0 && !ffs_fsfail_cleanup(ump, error)) {
1530 vfs_write_resume(mp, VR_START_WRITE);
1531 if (ump->um_trim_tq != NULL) {
1532 while (ump->um_trim_inflight != 0)
1533 pause("ufsutr", hz);
1534 taskqueue_drain_all(ump->um_trim_tq);
1535 taskqueue_free(ump->um_trim_tq);
1536 free (ump->um_trimhash, M_TRIM);
1539 if (ump->um_fsckpid > 0) {
1541 * Return to normal read-only mode.
1543 error = g_access(ump->um_cp, 0, -1, 0);
1544 ump->um_fsckpid = 0;
1546 g_vfs_close(ump->um_cp);
1547 g_topology_unlock();
1548 BO_LOCK(&ump->um_odevvp->v_bufobj);
1549 ump->um_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
1550 BO_UNLOCK(&ump->um_odevvp->v_bufobj);
1551 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1552 mntfs_freevp(ump->um_devvp);
1553 vrele(ump->um_odevvp);
1554 dev_rel(ump->um_dev);
1555 mtx_destroy(UFS_MTX(ump));
1556 if (mp->mnt_gjprovider != NULL) {
1557 free(mp->mnt_gjprovider, M_UFSMNT);
1558 mp->mnt_gjprovider = NULL;
1560 free(fs->fs_csp, M_UFSMNT);
1561 free(fs->fs_si, M_UFSMNT);
1563 if (ump->um_fsfail_task != NULL)
1564 free(ump->um_fsfail_task, M_UFSMNT);
1565 free(ump, M_UFSMNT);
1566 mp->mnt_data = NULL;
1568 mp->mnt_flag &= ~MNT_LOCAL;
1570 if (td->td_su == mp) {
1578 vfs_write_resume(mp, VR_START_WRITE);
1582 ufs_extattr_uepm_init(&ump->um_extattr);
1583 #ifdef UFS_EXTATTR_AUTOSTART
1584 (void) ufs_extattr_autostart(mp, td);
1593 * Flush out all the files in a filesystem.
1596 ffs_flushfiles(mp, flags, td)
1601 struct ufsmount *ump;
1607 if (mp->mnt_flag & MNT_QUOTA) {
1609 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1612 for (i = 0; i < MAXQUOTAS; i++) {
1613 error = quotaoff(td, mp, i);
1615 if ((flags & EARLYFLUSH) == 0)
1623 * Here we fall through to vflush again to ensure that
1624 * we have gotten rid of all the system vnodes, unless
1625 * quotas must not be closed.
1629 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1630 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1631 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1633 ffs_snapshot_unmount(mp);
1634 flags |= FORCECLOSE;
1636 * Here we fall through to vflush again to ensure
1637 * that we have gotten rid of all the system vnodes.
1642 * Do not close system files if quotas were not closed, to be
1643 * able to sync the remaining dquots. The freeblks softupdate
1644 * workitems might hold a reference on a dquot, preventing
1645 * quotaoff() from completing. Next round of
1646 * softdep_flushworklist() iteration should process the
1647 * blockers, allowing the next run of quotaoff() to finally
1648 * flush held dquots.
1650 * Otherwise, flush all the files.
1652 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1656 * Flush filesystem metadata.
1658 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1659 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1660 VOP_UNLOCK(ump->um_devvp);
1665 * Get filesystem statistics.
1672 struct ufsmount *ump;
1677 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1678 panic("ffs_statfs");
1679 sbp->f_version = STATFS_VERSION;
1680 sbp->f_bsize = fs->fs_fsize;
1681 sbp->f_iosize = fs->fs_bsize;
1682 sbp->f_blocks = fs->fs_dsize;
1684 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1685 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1686 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1687 dbtofsb(fs, fs->fs_pendingblocks);
1688 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1689 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1691 sbp->f_namemax = UFS_MAXNAMLEN;
1696 sync_doupdate(struct inode *ip)
1699 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1704 ffs_sync_lazy_filter(struct vnode *vp, void *arg __unused)
1709 * Flags are safe to access because ->v_data invalidation
1710 * is held off by listmtx.
1712 if (vp->v_type == VNON)
1715 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0)
1721 * For a lazy sync, we only care about access times, quotas and the
1722 * superblock. Other filesystem changes are already converted to
1723 * cylinder group blocks or inode blocks updates and are written to
1730 struct vnode *mvp, *vp;
1733 int allerror, error;
1737 if ((mp->mnt_flag & MNT_NOATIME) != 0) {
1743 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, ffs_sync_lazy_filter, NULL) {
1744 if (vp->v_type == VNON) {
1751 * The IN_ACCESS flag is converted to IN_MODIFIED by
1752 * ufs_close() and ufs_getattr() by the calls to
1753 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1754 * Test also all the other timestamp flags too, to pick up
1755 * any other cases that could be missed.
1757 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1761 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK)) != 0)
1766 if (sync_doupdate(ip))
1767 error = ffs_update(vp, 0);
1773 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1774 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1780 * Go through the disk queues to initiate sandbagged IO;
1781 * go through the inodes to write those that have been modified;
1782 * initiate the writing of the super block if it has been modified.
1784 * Note: we are always called with the filesystem marked busy using
1788 ffs_sync(mp, waitfor)
1792 struct vnode *mvp, *vp, *devvp;
1795 struct ufsmount *ump = VFSTOUFS(mp);
1797 int error, count, lockreq, allerror = 0;
1800 int secondary_writes;
1801 int secondary_accwrites;
1803 int softdep_accdeps;
1810 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1811 panic("%s: ffs_sync: modification on read-only filesystem",
1813 if (waitfor == MNT_LAZY) {
1815 return (ffs_sync_lazy(mp));
1816 waitfor = MNT_NOWAIT;
1820 * Write back each (modified) inode.
1822 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1823 if (waitfor == MNT_SUSPEND) {
1827 if (waitfor == MNT_WAIT)
1828 lockreq = LK_EXCLUSIVE;
1829 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1831 /* Grab snapshot of secondary write counts */
1833 secondary_writes = mp->mnt_secondary_writes;
1834 secondary_accwrites = mp->mnt_secondary_accwrites;
1837 /* Grab snapshot of softdep dependency counts */
1838 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1840 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1842 * Depend on the vnode interlock to keep things stable enough
1843 * for a quick test. Since there might be hundreds of
1844 * thousands of vnodes, we cannot afford even a subroutine
1845 * call unless there's a good chance that we have work to do.
1847 if (vp->v_type == VNON) {
1853 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1854 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1858 if ((error = vget(vp, lockreq)) != 0) {
1859 if (error == ENOENT || error == ENOLCK) {
1860 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1868 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1873 * Force stale filesystem control information to be flushed.
1875 if (waitfor == MNT_WAIT || rebooting) {
1876 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1878 if (ffs_fsfail_cleanup(ump, allerror))
1880 /* Flushed work items may create new vnodes to clean */
1881 if (allerror == 0 && count)
1885 devvp = ump->um_devvp;
1886 bo = &devvp->v_bufobj;
1888 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1890 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1891 error = VOP_FSYNC(devvp, waitfor, td);
1893 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1894 error = ffs_sbupdate(ump, waitfor, 0);
1897 if (ffs_fsfail_cleanup(ump, allerror))
1899 if (allerror == 0 && waitfor == MNT_WAIT)
1901 } else if (suspend != 0) {
1902 if (softdep_check_suspend(mp,
1907 secondary_accwrites) != 0) {
1909 goto loop; /* More work needed */
1911 mtx_assert(MNT_MTX(mp), MA_OWNED);
1912 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1918 * Write back modified superblock.
1920 if (fs->fs_fmod != 0 &&
1921 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1923 if (ffs_fsfail_cleanup(ump, allerror))
1929 ffs_vget(mp, ino, flags, vpp)
1935 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1939 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1948 struct ufsmount *ump;
1954 MPASS((ffs_flags & FFSV_REPLACE) == 0 || (flags & LK_EXCLUSIVE) != 0);
1956 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1960 if ((ffs_flags & FFSV_REPLACE) == 0)
1967 * We must promote to an exclusive lock for vnode creation. This
1968 * can happen if lookup is passed LOCKSHARED.
1970 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1971 flags &= ~LK_TYPE_MASK;
1972 flags |= LK_EXCLUSIVE;
1976 * We do not lock vnode creation as it is believed to be too
1977 * expensive for such rare case as simultaneous creation of vnode
1978 * for same ino by different processes. We just allow them to race
1979 * and check later to decide who wins. Let the race begin!
1984 ip = uma_zalloc_smr(uma_inode, M_WAITOK | M_ZERO);
1986 /* Allocate a new vnode/inode. */
1987 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1988 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1991 uma_zfree_smr(uma_inode, ip);
1995 * FFS supports recursive locking.
1997 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
2000 vp->v_bufobj.bo_bsize = fs->fs_bsize;
2005 ip->i_nextclustercg = -1;
2006 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
2007 ip->i_mode = 0; /* ensure error cases below throw away vnode */
2011 for (i = 0; i < MAXQUOTAS; i++)
2012 ip->i_dquot[i] = NODQUOT;
2016 if (ffs_flags & FFSV_FORCEINSMQ)
2017 vp->v_vflag |= VV_FORCEINSMQ;
2018 error = insmntque(vp, mp);
2020 uma_zfree_smr(uma_inode, ip);
2024 vp->v_vflag &= ~VV_FORCEINSMQ;
2025 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
2030 * Calls from ffs_valloc() (i.e. FFSV_REPLACE set)
2031 * operate on empty inode, which must not be found by
2032 * other threads until fully filled. Vnode for empty
2033 * inode must be not re-inserted on the hash by other
2034 * thread, after removal by us at the beginning.
2036 MPASS((ffs_flags & FFSV_REPLACE) == 0);
2040 /* Read in the disk contents for the inode, copy into the inode. */
2041 dbn = fsbtodb(fs, ino_to_fsba(fs, ino));
2042 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
2043 NULL, NULL, 0, NOCRED, 0, NULL, &bp);
2046 * The inode does not contain anything useful, so it would
2047 * be misleading to leave it on its hash chain. With mode
2048 * still zero, it will be unlinked and returned to the free
2057 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
2059 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
2060 if ((error = ffs_load_inode(bp, ip, fs, ino)) != 0) {
2067 if (DOINGSOFTDEP(vp))
2068 softdep_load_inodeblock(ip);
2070 ip->i_effnlink = ip->i_nlink;
2074 * Initialize the vnode from the inode, check for aliases.
2075 * Note that the underlying vnode may have changed.
2077 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
2087 * Finish inode initialization.
2089 if (vp->v_type != VFIFO) {
2090 /* FFS supports shared locking for all files except fifos. */
2095 * Set up a generation number for this inode if it does not
2096 * already have one. This should only happen on old filesystems.
2098 if (ip->i_gen == 0) {
2099 while (ip->i_gen == 0)
2100 ip->i_gen = arc4random();
2101 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
2102 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
2103 DIP_SET(ip, i_gen, ip->i_gen);
2107 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
2109 * If this vnode is already allocated, and we're running
2110 * multi-label, attempt to perform a label association
2111 * from the extended attributes on the inode.
2113 error = mac_vnode_associate_extattr(mp, vp);
2115 /* ufs_inactive will release ip->i_devvp ref. */
2129 * File handle to vnode
2131 * Have to be really careful about stale file handles:
2132 * - check that the inode number is valid
2133 * - for UFS2 check that the inode number is initialized
2134 * - call ffs_vget() to get the locked inode
2135 * - check for an unallocated inode (i_mode == 0)
2136 * - check that the given client host has export rights and return
2137 * those rights via. exflagsp and credanonp
2140 ffs_fhtovp(mp, fhp, flags, vpp)
2147 struct ufsmount *ump;
2155 ufhp = (struct ufid *)fhp;
2156 ino = ufhp->ufid_ino;
2159 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
2162 * Need to check if inode is initialized because UFS2 does lazy
2163 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
2165 if (fs->fs_magic != FS_UFS2_MAGIC)
2166 return (ufs_fhtovp(mp, ufhp, flags, vpp));
2167 cg = ino_to_cg(fs, ino);
2168 if ((error = ffs_getcg(fs, ump->um_devvp, cg, 0, &bp, &cgp)) != 0)
2170 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
2175 return (ufs_fhtovp(mp, ufhp, flags, vpp));
2179 * Initialize the filesystem.
2183 struct vfsconf *vfsp;
2186 ffs_susp_initialize();
2187 softdep_initialize();
2188 return (ufs_init(vfsp));
2192 * Undo the work of ffs_init().
2196 struct vfsconf *vfsp;
2200 ret = ufs_uninit(vfsp);
2201 softdep_uninitialize();
2202 ffs_susp_uninitialize();
2203 taskqueue_drain_all(taskqueue_thread);
2208 * Structure used to pass information from ffs_sbupdate to its
2209 * helper routine ffs_use_bwrite.
2212 struct ufsmount *ump;
2220 * Write a superblock and associated information back to disk.
2223 ffs_sbupdate(ump, waitfor, suspended)
2224 struct ufsmount *ump;
2233 if (fs->fs_ronly == 1 &&
2234 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
2235 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
2236 panic("ffs_sbupdate: write read-only filesystem");
2238 * We use the superblock's buf to serialize calls to ffs_sbupdate().
2240 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
2241 (int)fs->fs_sbsize, 0, 0, 0);
2243 * Initialize info needed for write function.
2247 devfd.waitfor = waitfor;
2248 devfd.suspended = suspended;
2250 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
2254 * Write function for use by filesystem-layer routines.
2257 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
2259 struct devfd *devfdp;
2260 struct ufsmount *ump;
2269 * Writing the superblock summary information.
2271 if (loc != fs->fs_sblockloc) {
2272 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
2273 bcopy(buf, bp->b_data, (u_int)size);
2274 if (devfdp->suspended)
2275 bp->b_flags |= B_VALIDSUSPWRT;
2276 if (devfdp->waitfor != MNT_WAIT)
2278 else if ((error = bwrite(bp)) != 0)
2279 devfdp->error = error;
2283 * Writing the superblock itself. We need to do special checks for it.
2286 if (ffs_fsfail_cleanup(ump, devfdp->error))
2288 if (devfdp->error != 0) {
2290 return (devfdp->error);
2292 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
2293 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2294 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2295 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
2296 fs->fs_sblockloc = SBLOCK_UFS1;
2298 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
2299 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
2300 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
2301 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
2302 fs->fs_sblockloc = SBLOCK_UFS2;
2304 if (MOUNTEDSOFTDEP(ump->um_mountp))
2305 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
2306 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
2307 fs = (struct fs *)bp->b_data;
2308 ffs_oldfscompat_write(fs, ump);
2310 /* Recalculate the superblock hash */
2311 fs->fs_ckhash = ffs_calc_sbhash(fs);
2312 if (devfdp->suspended)
2313 bp->b_flags |= B_VALIDSUSPWRT;
2314 if (devfdp->waitfor != MNT_WAIT)
2316 else if ((error = bwrite(bp)) != 0)
2317 devfdp->error = error;
2318 return (devfdp->error);
2322 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
2323 int attrnamespace, const char *attrname)
2327 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
2330 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2336 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2339 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2340 uma_zfree(uma_ufs1, ip->i_din1);
2341 else if (ip->i_din2 != NULL)
2342 uma_zfree(uma_ufs2, ip->i_din2);
2343 uma_zfree_smr(uma_inode, ip);
2346 static int dobkgrdwrite = 1;
2347 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2348 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2351 * Complete a background write started from bwrite.
2354 ffs_backgroundwritedone(struct buf *bp)
2356 struct bufobj *bufobj;
2360 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) != 0)
2361 softdep_handle_error(bp);
2365 * Find the original buffer that we are writing.
2367 bufobj = bp->b_bufobj;
2369 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2370 panic("backgroundwritedone: lost buffer");
2373 * We should mark the cylinder group buffer origbp as
2374 * dirty, to not lose the failed write.
2376 if ((bp->b_ioflags & BIO_ERROR) != 0)
2377 origbp->b_vflags |= BV_BKGRDERR;
2380 * Process dependencies then return any unfinished ones.
2382 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2385 if (!LIST_EMPTY(&bp->b_dep))
2386 softdep_move_dependencies(bp, origbp);
2389 * This buffer is marked B_NOCACHE so when it is released
2390 * by biodone it will be tossed.
2392 bp->b_flags |= B_NOCACHE;
2393 bp->b_flags &= ~B_CACHE;
2397 * Prevent brelse() from trying to keep and re-dirtying bp on
2398 * errors. It causes b_bufobj dereference in
2399 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2402 if ((bp->b_ioflags & BIO_ERROR) != 0)
2403 bp->b_flags |= B_INVAL;
2407 * Clear the BV_BKGRDINPROG flag in the original buffer
2408 * and awaken it if it is waiting for the write to complete.
2409 * If BV_BKGRDINPROG is not set in the original buffer it must
2410 * have been released and re-instantiated - which is not legal.
2412 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2413 ("backgroundwritedone: lost buffer2"));
2414 origbp->b_vflags &= ~BV_BKGRDINPROG;
2415 if (origbp->b_vflags & BV_BKGRDWAIT) {
2416 origbp->b_vflags &= ~BV_BKGRDWAIT;
2417 wakeup(&origbp->b_xflags);
2424 * Write, release buffer on completion. (Done by iodone
2425 * if async). Do not bother writing anything if the buffer
2428 * Note that we set B_CACHE here, indicating that buffer is
2429 * fully valid and thus cacheable. This is true even of NFS
2430 * now so we set it generally. This could be set either here
2431 * or in biodone() since the I/O is synchronous. We put it
2435 ffs_bufwrite(struct buf *bp)
2440 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2441 if (bp->b_flags & B_INVAL) {
2446 if (!BUF_ISLOCKED(bp))
2447 panic("bufwrite: buffer is not busy???");
2449 * If a background write is already in progress, delay
2450 * writing this block if it is asynchronous. Otherwise
2451 * wait for the background write to complete.
2453 BO_LOCK(bp->b_bufobj);
2454 if (bp->b_vflags & BV_BKGRDINPROG) {
2455 if (bp->b_flags & B_ASYNC) {
2456 BO_UNLOCK(bp->b_bufobj);
2460 bp->b_vflags |= BV_BKGRDWAIT;
2461 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2463 if (bp->b_vflags & BV_BKGRDINPROG)
2464 panic("bufwrite: still writing");
2466 bp->b_vflags &= ~BV_BKGRDERR;
2467 BO_UNLOCK(bp->b_bufobj);
2470 * If this buffer is marked for background writing and we
2471 * do not have to wait for it, make a copy and write the
2472 * copy so as to leave this buffer ready for further use.
2474 * This optimization eats a lot of memory. If we have a page
2475 * or buffer shortfall we can't do it.
2477 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2478 (bp->b_flags & B_ASYNC) &&
2479 !vm_page_count_severe() &&
2480 !buf_dirty_count_severe()) {
2481 KASSERT(bp->b_iodone == NULL,
2482 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2484 /* get a new block */
2485 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2489 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2490 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2491 BO_LOCK(bp->b_bufobj);
2492 bp->b_vflags |= BV_BKGRDINPROG;
2493 BO_UNLOCK(bp->b_bufobj);
2495 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2496 newbp->b_lblkno = bp->b_lblkno;
2497 newbp->b_blkno = bp->b_blkno;
2498 newbp->b_offset = bp->b_offset;
2499 newbp->b_iodone = ffs_backgroundwritedone;
2500 newbp->b_flags |= B_ASYNC;
2501 newbp->b_flags &= ~B_INVAL;
2502 pbgetvp(bp->b_vp, newbp);
2506 * Move over the dependencies. If there are rollbacks,
2507 * leave the parent buffer dirtied as it will need to
2510 if (LIST_EMPTY(&bp->b_dep) ||
2511 softdep_move_dependencies(bp, newbp) == 0)
2518 * Initiate write on the copy, release the original. The
2519 * BKGRDINPROG flag prevents it from going away until
2520 * the background write completes. We have to recalculate
2521 * its check hash in case the buffer gets freed and then
2522 * reconstituted from the buffer cache during a later read.
2524 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2525 cgp = (struct cg *)bp->b_data;
2528 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2533 /* Mark the buffer clean */
2537 /* Let the normal bufwrite do the rest for us */
2540 * If we are writing a cylinder group, update its time.
2542 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2543 cgp = (struct cg *)bp->b_data;
2544 cgp->cg_old_time = cgp->cg_time = time_second;
2546 return (bufwrite(bp));
2551 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2558 * This is the bufobj strategy for the private VCHR vnodes
2559 * used by FFS to access the underlying storage device.
2560 * We override the default bufobj strategy and thus bypass
2561 * VOP_STRATEGY() for these vnodes.
2564 KASSERT(bp->b_vp == NULL || bp->b_vp->v_type != VCHR ||
2565 bp->b_vp->v_rdev == NULL ||
2566 bp->b_vp->v_rdev->si_mountpt == NULL ||
2567 VFSTOUFS(bp->b_vp->v_rdev->si_mountpt) == NULL ||
2568 vp == VFSTOUFS(bp->b_vp->v_rdev->si_mountpt)->um_devvp,
2569 ("ffs_geom_strategy() with wrong vp"));
2570 if (bp->b_iocmd == BIO_WRITE) {
2571 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2572 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2573 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2574 panic("ffs_geom_strategy: bad I/O");
2575 nocopy = bp->b_flags & B_NOCOPY;
2576 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2577 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2578 vp->v_rdev->si_snapdata != NULL) {
2579 if ((bp->b_flags & B_CLUSTER) != 0) {
2580 runningbufwakeup(bp);
2581 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2582 b_cluster.cluster_entry) {
2583 error = ffs_copyonwrite(vp, tbp);
2585 error != EOPNOTSUPP) {
2586 bp->b_error = error;
2587 bp->b_ioflags |= BIO_ERROR;
2592 bp->b_runningbufspace = bp->b_bufsize;
2593 atomic_add_long(&runningbufspace,
2594 bp->b_runningbufspace);
2596 error = ffs_copyonwrite(vp, bp);
2597 if (error != 0 && error != EOPNOTSUPP) {
2598 bp->b_error = error;
2599 bp->b_ioflags |= BIO_ERROR;
2606 if ((bp->b_flags & B_CLUSTER) != 0) {
2607 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2608 b_cluster.cluster_entry) {
2609 if (!LIST_EMPTY(&tbp->b_dep))
2613 if (!LIST_EMPTY(&bp->b_dep))
2619 * Check for metadata that needs check-hashes and update them.
2621 switch (bp->b_xflags & BX_FSPRIV) {
2623 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2624 ((struct cg *)bp->b_data)->cg_ckhash =
2625 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2632 printf("Check-hash write is unimplemented!!!\n");
2639 printf("multiple buffer types 0x%b\n",
2640 (u_int)(bp->b_xflags & BX_FSPRIV),
2641 PRINT_UFS_BUF_XFLAGS);
2645 if (bp->b_iocmd != BIO_READ && ffs_enxio_enable)
2646 bp->b_xflags |= BX_CVTENXIO;
2647 g_vfs_strategy(bo, bp);
2651 ffs_own_mount(const struct mount *mp)
2654 if (mp->mnt_op == &ufs_vfsops)
2662 /* defined in ffs_softdep.c */
2663 extern void db_print_ffs(struct ufsmount *ump);
2665 DB_SHOW_COMMAND(ffs, db_show_ffs)
2668 struct ufsmount *ump;
2671 ump = VFSTOUFS((struct mount *)addr);
2676 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2677 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2678 db_print_ffs(VFSTOUFS(mp));
2682 #endif /* SOFTUPDATES */