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/systm.h>
44 #include <sys/namei.h>
47 #include <sys/taskqueue.h>
48 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
54 #include <sys/fcntl.h>
55 #include <sys/ioccom.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/rwlock.h>
59 #include <sys/vmmeter.h>
61 #include <security/mac/mac_framework.h>
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/gjournal.h>
66 #include <ufs/ufs/quota.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ffs/fs.h>
72 #include <ufs/ffs/ffs_extern.h>
76 #include <vm/vm_page.h>
78 #include <geom/geom.h>
79 #include <geom/geom_vfs.h>
83 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
85 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
86 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
88 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
89 static int ffs_sync_lazy(struct mount *mp);
90 static int ffs_use_bread(void *devfd, off_t loc, void **bufp, int size);
91 static int ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size);
93 static vfs_init_t ffs_init;
94 static vfs_uninit_t ffs_uninit;
95 static vfs_extattrctl_t ffs_extattrctl;
96 static vfs_cmount_t ffs_cmount;
97 static vfs_unmount_t ffs_unmount;
98 static vfs_mount_t ffs_mount;
99 static vfs_statfs_t ffs_statfs;
100 static vfs_fhtovp_t ffs_fhtovp;
101 static vfs_sync_t ffs_sync;
103 static struct vfsops ufs_vfsops = {
104 .vfs_extattrctl = ffs_extattrctl,
105 .vfs_fhtovp = ffs_fhtovp,
106 .vfs_init = ffs_init,
107 .vfs_mount = ffs_mount,
108 .vfs_cmount = ffs_cmount,
109 .vfs_quotactl = ufs_quotactl,
110 .vfs_root = ufs_root,
111 .vfs_statfs = ffs_statfs,
112 .vfs_sync = ffs_sync,
113 .vfs_uninit = ffs_uninit,
114 .vfs_unmount = ffs_unmount,
115 .vfs_vget = ffs_vget,
116 .vfs_susp_clean = process_deferred_inactive,
119 VFS_SET(ufs_vfsops, ufs, 0);
120 MODULE_VERSION(ufs, 1);
122 static b_strategy_t ffs_geom_strategy;
123 static b_write_t ffs_bufwrite;
125 static struct buf_ops ffs_ops = {
127 .bop_write = ffs_bufwrite,
128 .bop_strategy = ffs_geom_strategy,
130 #ifdef NO_FFS_SNAPSHOT
131 .bop_bdflush = bufbdflush,
133 .bop_bdflush = ffs_bdflush,
138 * Note that userquota and groupquota options are not currently used
139 * by UFS/FFS code and generally mount(8) does not pass those options
140 * from userland, but they can be passed by loader(8) via
141 * vfs.root.mountfrom.options.
143 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
144 "noclusterw", "noexec", "export", "force", "from", "groupquota",
145 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
146 "nosymfollow", "sync", "union", "userquota", NULL };
149 ffs_mount(struct mount *mp)
153 struct ufsmount *ump = NULL;
156 int error, error1, flags;
159 struct nameidata ndp;
163 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
165 if (uma_inode == NULL) {
166 uma_inode = uma_zcreate("FFS inode",
167 sizeof(struct inode), NULL, NULL, NULL, NULL,
169 uma_ufs1 = uma_zcreate("FFS1 dinode",
170 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
172 uma_ufs2 = uma_zcreate("FFS2 dinode",
173 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
177 vfs_deleteopt(mp->mnt_optnew, "groupquota");
178 vfs_deleteopt(mp->mnt_optnew, "userquota");
180 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
185 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
186 mntorflags |= MNT_ACLS;
188 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
189 mntorflags |= MNT_SNAPSHOT;
191 * Once we have set the MNT_SNAPSHOT flag, do not
192 * persist "snapshot" in the options list.
194 vfs_deleteopt(mp->mnt_optnew, "snapshot");
195 vfs_deleteopt(mp->mnt_opt, "snapshot");
198 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
199 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
201 * Once we have set the restricted PID, do not
202 * persist "fsckpid" in the options list.
204 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
205 vfs_deleteopt(mp->mnt_opt, "fsckpid");
206 if (mp->mnt_flag & MNT_UPDATE) {
207 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
208 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
210 "Checker enable: Must be read-only");
213 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
215 "Checker enable: Must be read-only");
218 /* Set to -1 if we are done */
223 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
224 if (mntorflags & MNT_ACLS) {
226 "\"acls\" and \"nfsv4acls\" options "
227 "are mutually exclusive");
230 mntorflags |= MNT_NFS4ACLS;
234 mp->mnt_flag |= mntorflags;
237 * If updating, check whether changing from read-only to
238 * read/write; if there is no device name, that's all we do.
240 if (mp->mnt_flag & MNT_UPDATE) {
243 devvp = ump->um_devvp;
244 if (fsckpid == -1 && ump->um_fsckpid > 0) {
245 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
246 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
250 * Return to normal read-only mode.
252 error = g_access(ump->um_cp, 0, -1, 0);
256 if (fs->fs_ronly == 0 &&
257 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
259 * Flush any dirty data and suspend filesystem.
261 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
263 error = vfs_write_suspend_umnt(mp);
267 * Check for and optionally get rid of files open
271 if (mp->mnt_flag & MNT_FORCE)
273 if (MOUNTEDSOFTDEP(mp)) {
274 error = softdep_flushfiles(mp, flags, td);
276 error = ffs_flushfiles(mp, flags, td);
279 vfs_write_resume(mp, 0);
282 if (fs->fs_pendingblocks != 0 ||
283 fs->fs_pendinginodes != 0) {
284 printf("WARNING: %s Update error: blocks %jd "
285 "files %d\n", fs->fs_fsmnt,
286 (intmax_t)fs->fs_pendingblocks,
287 fs->fs_pendinginodes);
288 fs->fs_pendingblocks = 0;
289 fs->fs_pendinginodes = 0;
291 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
293 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
296 vfs_write_resume(mp, 0);
299 if (MOUNTEDSOFTDEP(mp))
303 * Drop our write and exclusive access.
305 g_access(ump->um_cp, 0, -1, -1);
309 mp->mnt_flag |= MNT_RDONLY;
312 * Allow the writers to note that filesystem
315 vfs_write_resume(mp, 0);
317 if ((mp->mnt_flag & MNT_RELOAD) &&
318 (error = ffs_reload(mp, td, 0)) != 0)
321 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
323 * If we are running a checker, do not allow upgrade.
325 if (ump->um_fsckpid > 0) {
327 "Active checker, cannot upgrade to write");
331 * If upgrade to read-write by non-root, then verify
332 * that user has necessary permissions on the device.
334 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
335 error = VOP_ACCESS(devvp, VREAD | VWRITE,
338 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
340 VOP_UNLOCK(devvp, 0);
343 VOP_UNLOCK(devvp, 0);
344 fs->fs_flags &= ~FS_UNCLEAN;
345 if (fs->fs_clean == 0) {
346 fs->fs_flags |= FS_UNCLEAN;
347 if ((mp->mnt_flag & MNT_FORCE) ||
349 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
350 (fs->fs_flags & FS_DOSOFTDEP))) {
351 printf("WARNING: %s was not properly "
352 "dismounted\n", fs->fs_fsmnt);
355 "R/W mount of %s denied. %s.%s",
357 "Filesystem is not clean - run fsck",
358 (fs->fs_flags & FS_SUJ) == 0 ? "" :
359 " Forced mount will invalidate"
360 " journal contents");
366 * Request exclusive write access.
368 error = g_access(ump->um_cp, 0, 1, 1);
372 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
376 mp->mnt_flag &= ~MNT_RDONLY;
378 fs->fs_mtime = time_second;
379 /* check to see if we need to start softdep */
380 if ((fs->fs_flags & FS_DOSOFTDEP) &&
381 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
382 vn_finished_write(mp);
386 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
387 vn_finished_write(mp);
390 if (fs->fs_snapinum[0] != 0)
391 ffs_snapshot_mount(mp);
392 vn_finished_write(mp);
395 * Soft updates is incompatible with "async",
396 * so if we are doing softupdates stop the user
397 * from setting the async flag in an update.
398 * Softdep_mount() clears it in an initial mount
401 if (MOUNTEDSOFTDEP(mp)) {
402 /* XXX: Reset too late ? */
404 mp->mnt_flag &= ~MNT_ASYNC;
408 * Keep MNT_ACLS flag if it is stored in superblock.
410 if ((fs->fs_flags & FS_ACLS) != 0) {
411 /* XXX: Set too late ? */
413 mp->mnt_flag |= MNT_ACLS;
417 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
418 /* XXX: Set too late ? */
420 mp->mnt_flag |= MNT_NFS4ACLS;
424 * If this is a request from fsck to clean up the filesystem,
425 * then allow the specified pid to proceed.
428 if (ump->um_fsckpid != 0) {
430 "Active checker already running on %s",
434 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
435 ("soft updates enabled on read-only file system"));
438 * Request write access.
440 error = g_access(ump->um_cp, 0, 1, 0);
444 "Checker activation failed on %s",
448 ump->um_fsckpid = fsckpid;
449 if (fs->fs_snapinum[0] != 0)
450 ffs_snapshot_mount(mp);
451 fs->fs_mtime = time_second;
454 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
458 * If this is a snapshot request, take the snapshot.
460 if (mp->mnt_flag & MNT_SNAPSHOT)
461 return (ffs_snapshot(mp, fspec));
464 * Must not call namei() while owning busy ref.
470 * Not an update, or updating the name: look up the name
471 * and verify that it refers to a sensible disk device.
473 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
475 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
477 * Unmount does not start if MNT_UPDATE is set. Mount
478 * update busies mp before setting MNT_UPDATE. We
479 * must be able to retain our busy ref succesfully,
482 error1 = vfs_busy(mp, MBF_NOWAIT);
487 NDFREE(&ndp, NDF_ONLY_PNBUF);
489 if (!vn_isdisk(devvp, &error)) {
495 * If mount by non-root, then verify that user has necessary
496 * permissions on the device.
499 if ((mp->mnt_flag & MNT_RDONLY) == 0)
501 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
503 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
509 if (mp->mnt_flag & MNT_UPDATE) {
513 * If it's not the same vnode, or at least the same device
514 * then it's not correct.
517 if (devvp->v_rdev != ump->um_devvp->v_rdev)
518 error = EINVAL; /* needs translation */
526 * We need the name for the mount point (also used for
527 * "last mounted on") copied in. If an error occurs,
528 * the mount point is discarded by the upper level code.
529 * Note that vfs_mount_alloc() populates f_mntonname for us.
531 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
536 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
537 ("soft updates enabled on read-only file system"));
542 * Request write access.
544 error = g_access(ump->um_cp, 0, 1, 0);
547 printf("WARNING: %s: Checker activation "
548 "failed\n", fs->fs_fsmnt);
550 ump->um_fsckpid = fsckpid;
551 if (fs->fs_snapinum[0] != 0)
552 ffs_snapshot_mount(mp);
553 fs->fs_mtime = time_second;
555 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
559 vfs_mountedfrom(mp, fspec);
564 * Compatibility with old mount system call.
568 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
570 struct ufs_args args;
571 struct export_args exp;
576 error = copyin(data, &args, sizeof args);
579 vfs_oexport_conv(&args.export, &exp);
581 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
582 ma = mount_arg(ma, "export", &exp, sizeof(exp));
583 error = kernel_mount(ma, flags);
589 * Reload all incore data for a filesystem (used after running fsck on
590 * the root filesystem and finding things to fix). If the 'force' flag
591 * is 0, the filesystem must be mounted read-only.
593 * Things to do to update the mount:
594 * 1) invalidate all cached meta-data.
595 * 2) re-read superblock from disk.
596 * 3) re-read summary information from disk.
597 * 4) invalidate all inactive vnodes.
598 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
599 * writers, if requested.
600 * 6) invalidate all cached file data.
601 * 7) re-read inode data for all active vnodes.
604 ffs_reload(struct mount *mp, struct thread *td, int flags)
606 struct vnode *vp, *mvp, *devvp;
610 struct fs *fs, *newfs;
611 struct ufsmount *ump;
612 ufs2_daddr_t sblockloc;
620 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
627 * Step 1: invalidate all cached meta-data.
629 devvp = VFSTOUFS(mp)->um_devvp;
630 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
631 if (vinvalbuf(devvp, 0, 0, 0) != 0)
632 panic("ffs_reload: dirty1");
633 VOP_UNLOCK(devvp, 0);
636 * Step 2: re-read superblock from disk.
638 fs = VFSTOUFS(mp)->um_fs;
639 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
642 newfs = (struct fs *)bp->b_data;
643 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
644 newfs->fs_magic != FS_UFS2_MAGIC) ||
645 newfs->fs_bsize > MAXBSIZE ||
646 newfs->fs_bsize < sizeof(struct fs)) {
648 return (EIO); /* XXX needs translation */
651 * Copy pointer fields back into superblock before copying in XXX
652 * new superblock. These should really be in the ufsmount. XXX
653 * Note that important parameters (eg fs_ncg) are unchanged.
655 newfs->fs_csp = fs->fs_csp;
656 newfs->fs_maxcluster = fs->fs_maxcluster;
657 newfs->fs_contigdirs = fs->fs_contigdirs;
658 newfs->fs_active = fs->fs_active;
659 newfs->fs_ronly = fs->fs_ronly;
660 sblockloc = fs->fs_sblockloc;
661 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
663 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
664 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
666 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
667 printf("WARNING: %s: reload pending error: blocks %jd "
668 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
669 fs->fs_pendinginodes);
670 fs->fs_pendingblocks = 0;
671 fs->fs_pendinginodes = 0;
676 * Step 3: re-read summary information from disk.
678 size = fs->fs_cssize;
679 blks = howmany(size, fs->fs_fsize);
680 if (fs->fs_contigsumsize > 0)
681 size += fs->fs_ncg * sizeof(int32_t);
682 size += fs->fs_ncg * sizeof(u_int8_t);
683 free(fs->fs_csp, M_UFSMNT);
684 space = malloc(size, M_UFSMNT, M_WAITOK);
686 for (i = 0; i < blks; i += fs->fs_frag) {
688 if (i + fs->fs_frag > blks)
689 size = (blks - i) * fs->fs_fsize;
690 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
694 bcopy(bp->b_data, space, (u_int)size);
695 space = (char *)space + size;
699 * We no longer know anything about clusters per cylinder group.
701 if (fs->fs_contigsumsize > 0) {
702 fs->fs_maxcluster = lp = space;
703 for (i = 0; i < fs->fs_ncg; i++)
704 *lp++ = fs->fs_contigsumsize;
707 size = fs->fs_ncg * sizeof(u_int8_t);
708 fs->fs_contigdirs = (u_int8_t *)space;
709 bzero(fs->fs_contigdirs, size);
710 if ((flags & FFSR_UNSUSPEND) != 0) {
712 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
713 wakeup(&mp->mnt_flag);
718 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
722 if (vp->v_type == VNON) {
727 * Step 4: invalidate all cached file data.
729 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
730 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
733 if (vinvalbuf(vp, 0, 0, 0))
734 panic("ffs_reload: dirty2");
736 * Step 5: re-read inode data for all active vnodes.
740 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
741 (int)fs->fs_bsize, NOCRED, &bp);
745 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
748 ffs_load_inode(bp, ip, fs, ip->i_number);
749 ip->i_effnlink = ip->i_nlink;
758 * Common code for mount and mountroot
761 ffs_mountfs(devvp, mp, td)
766 struct ufsmount *ump;
769 int error, i, len, ronly;
771 struct g_consumer *cp;
777 cred = td ? td->td_ucred : NOCRED;
778 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
780 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
782 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
783 (uintptr_t)mp) == 0) {
784 VOP_UNLOCK(devvp, 0);
788 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
791 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
792 VOP_UNLOCK(devvp, 0);
796 devvp->v_bufobj.bo_ops = &ffs_ops;
797 VOP_UNLOCK(devvp, 0);
798 if (dev->si_iosize_max != 0)
799 mp->mnt_iosize_max = dev->si_iosize_max;
800 if (mp->mnt_iosize_max > MAXPHYS)
801 mp->mnt_iosize_max = MAXPHYS;
802 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
805 "Invalid sectorsize %d for superblock size %d",
806 cp->provider->sectorsize, SBLOCKSIZE);
809 /* fetch the superblock and summary information */
810 if ((error = ffs_sbget(devvp, &fs, -1, M_UFSMNT, ffs_use_bread)) != 0)
813 /* if we ran on a kernel without metadata check hashes, disable them */
814 if ((fs->fs_flags & FS_METACKHASH) == 0)
815 fs->fs_metackhash = 0;
816 /* none of these types of check-hashes are maintained by this kernel */
817 fs->fs_metackhash &= ~(CK_INODE | CK_INDIR | CK_DIR);
818 /* no support for any undefined flags */
819 fs->fs_flags &= FS_SUPPORTED;
820 fs->fs_flags &= ~FS_UNCLEAN;
821 if (fs->fs_clean == 0) {
822 fs->fs_flags |= FS_UNCLEAN;
823 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
824 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
825 (fs->fs_flags & FS_DOSOFTDEP))) {
826 printf("WARNING: %s was not properly dismounted\n",
829 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
830 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
831 (fs->fs_flags & FS_SUJ) == 0 ? "" :
832 " Forced mount will invalidate journal contents");
836 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
837 (mp->mnt_flag & MNT_FORCE)) {
838 printf("WARNING: %s: lost blocks %jd files %d\n",
839 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
840 fs->fs_pendinginodes);
841 fs->fs_pendingblocks = 0;
842 fs->fs_pendinginodes = 0;
845 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
846 printf("WARNING: %s: mount pending error: blocks %jd "
847 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
848 fs->fs_pendinginodes);
849 fs->fs_pendingblocks = 0;
850 fs->fs_pendinginodes = 0;
852 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
855 * Get journal provider name.
858 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
859 if (g_io_getattr("GJOURNAL::provider", cp, &len,
860 mp->mnt_gjprovider) == 0) {
861 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
864 mp->mnt_flag |= MNT_GJOURNAL;
867 printf("WARNING: %s: GJOURNAL flag on fs "
868 "but no gjournal provider below\n",
869 mp->mnt_stat.f_mntonname);
870 free(mp->mnt_gjprovider, M_UFSMNT);
871 mp->mnt_gjprovider = NULL;
874 printf("WARNING: %s: GJOURNAL flag on fs but no "
875 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
878 mp->mnt_gjprovider = NULL;
880 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
882 ump->um_bo = &devvp->v_bufobj;
884 if (fs->fs_magic == FS_UFS1_MAGIC) {
885 ump->um_fstype = UFS1;
886 ump->um_balloc = ffs_balloc_ufs1;
888 ump->um_fstype = UFS2;
889 ump->um_balloc = ffs_balloc_ufs2;
891 ump->um_blkatoff = ffs_blkatoff;
892 ump->um_truncate = ffs_truncate;
893 ump->um_update = ffs_update;
894 ump->um_valloc = ffs_valloc;
895 ump->um_vfree = ffs_vfree;
896 ump->um_ifree = ffs_ifree;
897 ump->um_rdonly = ffs_rdonly;
898 ump->um_snapgone = ffs_snapgone;
899 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
900 ffs_oldfscompat_read(fs, ump, fs->fs_sblockloc);
901 fs->fs_ronly = ronly;
902 fs->fs_active = NULL;
904 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
905 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
907 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
908 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
913 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
915 mp->mnt_flag |= MNT_LOCAL;
917 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
920 mp->mnt_flag |= MNT_MULTILABEL;
923 printf("WARNING: %s: multilabel flag on fs but "
924 "no MAC support\n", mp->mnt_stat.f_mntonname);
927 if ((fs->fs_flags & FS_ACLS) != 0) {
931 if (mp->mnt_flag & MNT_NFS4ACLS)
932 printf("WARNING: %s: ACLs flag on fs conflicts with "
933 "\"nfsv4acls\" mount option; option ignored\n",
934 mp->mnt_stat.f_mntonname);
935 mp->mnt_flag &= ~MNT_NFS4ACLS;
936 mp->mnt_flag |= MNT_ACLS;
940 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
941 mp->mnt_stat.f_mntonname);
944 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
948 if (mp->mnt_flag & MNT_ACLS)
949 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
950 "with \"acls\" mount option; option ignored\n",
951 mp->mnt_stat.f_mntonname);
952 mp->mnt_flag &= ~MNT_ACLS;
953 mp->mnt_flag |= MNT_NFS4ACLS;
957 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
958 "ACLs support\n", mp->mnt_stat.f_mntonname);
961 if ((fs->fs_flags & FS_TRIM) != 0) {
963 if (g_io_getattr("GEOM::candelete", cp, &len,
966 ump->um_flags |= UM_CANDELETE;
968 printf("WARNING: %s: TRIM flag on fs but disk "
969 "does not support TRIM\n",
970 mp->mnt_stat.f_mntonname);
972 printf("WARNING: %s: TRIM flag on fs but disk does "
973 "not confirm that it supports TRIM\n",
974 mp->mnt_stat.f_mntonname);
976 if (((ump->um_flags) & UM_CANDELETE) != 0) {
977 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
978 taskqueue_thread_enqueue, &ump->um_trim_tq);
979 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
980 "%s trim", mp->mnt_stat.f_mntonname);
981 ump->um_trimhash = hashinit(MAXTRIMIO, M_TRIM,
982 &ump->um_trimlisthashsize);
988 ump->um_devvp = devvp;
989 ump->um_nindir = fs->fs_nindir;
990 ump->um_bptrtodb = fs->fs_fsbtodb;
991 ump->um_seqinc = fs->fs_frag;
992 for (i = 0; i < MAXQUOTAS; i++)
993 ump->um_quotas[i] = NULLVP;
995 ufs_extattr_uepm_init(&ump->um_extattr);
998 * Set FS local "last mounted on" information (NULL pad)
1000 bzero(fs->fs_fsmnt, MAXMNTLEN);
1001 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1002 mp->mnt_stat.f_iosize = fs->fs_bsize;
1004 if (mp->mnt_flag & MNT_ROOTFS) {
1006 * Root mount; update timestamp in mount structure.
1007 * this will be used by the common root mount code
1008 * to update the system clock.
1010 mp->mnt_time = fs->fs_time;
1014 fs->fs_mtime = time_second;
1015 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1016 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1017 ffs_flushfiles(mp, FORCECLOSE, td);
1020 if (fs->fs_snapinum[0] != 0)
1021 ffs_snapshot_mount(mp);
1024 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1027 * Initialize filesystem state information in mount struct.
1030 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1031 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1034 #ifdef UFS_EXTATTR_AUTOSTART
1037 * Auto-starting does the following:
1038 * - check for /.attribute in the fs, and extattr_start if so
1039 * - for each file in .attribute, enable that file with
1040 * an attribute of the same name.
1041 * Not clear how to report errors -- probably eat them.
1042 * This would all happen while the filesystem was busy/not
1043 * available, so would effectively be "atomic".
1045 (void) ufs_extattr_autostart(mp, td);
1046 #endif /* !UFS_EXTATTR_AUTOSTART */
1047 #endif /* !UFS_EXTATTR */
1051 free(fs->fs_csp, M_UFSMNT);
1057 g_topology_unlock();
1060 mtx_destroy(UFS_MTX(ump));
1061 if (mp->mnt_gjprovider != NULL) {
1062 free(mp->mnt_gjprovider, M_UFSMNT);
1063 mp->mnt_gjprovider = NULL;
1065 free(ump, M_UFSMNT);
1066 mp->mnt_data = NULL;
1068 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1074 * A read function for use by filesystem-layer routines.
1077 ffs_use_bread(void *devfd, off_t loc, void **bufp, int size)
1082 KASSERT(*bufp == NULL, ("ffs_use_bread: non-NULL *bufp %p\n", *bufp));
1083 *bufp = malloc(size, M_UFSMNT, M_WAITOK);
1084 if ((error = bread((struct vnode *)devfd, btodb(loc), size, NOCRED,
1087 bcopy(bp->b_data, *bufp, size);
1088 bp->b_flags |= B_INVAL | B_NOCACHE;
1093 #include <sys/sysctl.h>
1094 static int bigcgs = 0;
1095 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1098 * Sanity checks for loading old filesystem superblocks.
1099 * See ffs_oldfscompat_write below for unwound actions.
1101 * XXX - Parts get retired eventually.
1102 * Unfortunately new bits get added.
1105 ffs_oldfscompat_read(fs, ump, sblockloc)
1107 struct ufsmount *ump;
1108 ufs2_daddr_t sblockloc;
1113 * If not yet done, update fs_flags location and value of fs_sblockloc.
1115 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1116 fs->fs_flags = fs->fs_old_flags;
1117 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1118 fs->fs_sblockloc = sblockloc;
1121 * If not yet done, update UFS1 superblock with new wider fields.
1123 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1124 fs->fs_maxbsize = fs->fs_bsize;
1125 fs->fs_time = fs->fs_old_time;
1126 fs->fs_size = fs->fs_old_size;
1127 fs->fs_dsize = fs->fs_old_dsize;
1128 fs->fs_csaddr = fs->fs_old_csaddr;
1129 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1130 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1131 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1132 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1134 if (fs->fs_magic == FS_UFS1_MAGIC &&
1135 fs->fs_old_inodefmt < FS_44INODEFMT) {
1136 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1137 fs->fs_qbmask = ~fs->fs_bmask;
1138 fs->fs_qfmask = ~fs->fs_fmask;
1140 if (fs->fs_magic == FS_UFS1_MAGIC) {
1141 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1142 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1143 if (fs->fs_maxfilesize > maxfilesize)
1144 fs->fs_maxfilesize = maxfilesize;
1146 /* Compatibility for old filesystems */
1147 if (fs->fs_avgfilesize <= 0)
1148 fs->fs_avgfilesize = AVFILESIZ;
1149 if (fs->fs_avgfpdir <= 0)
1150 fs->fs_avgfpdir = AFPDIR;
1152 fs->fs_save_cgsize = fs->fs_cgsize;
1153 fs->fs_cgsize = fs->fs_bsize;
1158 * Unwinding superblock updates for old filesystems.
1159 * See ffs_oldfscompat_read above for details.
1161 * XXX - Parts get retired eventually.
1162 * Unfortunately new bits get added.
1165 ffs_oldfscompat_write(fs, ump)
1167 struct ufsmount *ump;
1171 * Copy back UFS2 updated fields that UFS1 inspects.
1173 if (fs->fs_magic == FS_UFS1_MAGIC) {
1174 fs->fs_old_time = fs->fs_time;
1175 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1176 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1177 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1178 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1179 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1182 fs->fs_cgsize = fs->fs_save_cgsize;
1183 fs->fs_save_cgsize = 0;
1188 * unmount system call
1191 ffs_unmount(mp, mntflags)
1196 struct ufsmount *ump = VFSTOUFS(mp);
1198 int error, flags, susp;
1207 if (mntflags & MNT_FORCE) {
1208 flags |= FORCECLOSE;
1209 susp = fs->fs_ronly == 0;
1212 if ((error = ufs_extattr_stop(mp, td))) {
1213 if (error != EOPNOTSUPP)
1214 printf("WARNING: unmount %s: ufs_extattr_stop "
1215 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1219 ufs_extattr_uepm_destroy(&ump->um_extattr);
1224 error = vfs_write_suspend_umnt(mp);
1228 if (MOUNTEDSOFTDEP(mp))
1229 error = softdep_flushfiles(mp, flags, td);
1231 error = ffs_flushfiles(mp, flags, td);
1232 if (error != 0 && error != ENXIO)
1236 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1237 printf("WARNING: unmount %s: pending error: blocks %jd "
1238 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1239 fs->fs_pendinginodes);
1240 fs->fs_pendingblocks = 0;
1241 fs->fs_pendinginodes = 0;
1244 if (MOUNTEDSOFTDEP(mp))
1245 softdep_unmount(mp);
1246 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1247 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1248 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1249 if (error && error != ENXIO) {
1255 vfs_write_resume(mp, VR_START_WRITE);
1256 if (ump->um_trim_tq != NULL) {
1257 while (ump->um_trim_inflight != 0)
1258 pause("ufsutr", hz);
1259 taskqueue_drain_all(ump->um_trim_tq);
1260 taskqueue_free(ump->um_trim_tq);
1261 free (ump->um_trimhash, M_TRIM);
1264 if (ump->um_fsckpid > 0) {
1266 * Return to normal read-only mode.
1268 error = g_access(ump->um_cp, 0, -1, 0);
1269 ump->um_fsckpid = 0;
1271 g_vfs_close(ump->um_cp);
1272 g_topology_unlock();
1273 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1274 vrele(ump->um_devvp);
1275 dev_rel(ump->um_dev);
1276 mtx_destroy(UFS_MTX(ump));
1277 if (mp->mnt_gjprovider != NULL) {
1278 free(mp->mnt_gjprovider, M_UFSMNT);
1279 mp->mnt_gjprovider = NULL;
1281 free(fs->fs_csp, M_UFSMNT);
1283 free(ump, M_UFSMNT);
1284 mp->mnt_data = NULL;
1286 mp->mnt_flag &= ~MNT_LOCAL;
1288 if (td->td_su == mp) {
1296 vfs_write_resume(mp, VR_START_WRITE);
1300 ufs_extattr_uepm_init(&ump->um_extattr);
1301 #ifdef UFS_EXTATTR_AUTOSTART
1302 (void) ufs_extattr_autostart(mp, td);
1311 * Flush out all the files in a filesystem.
1314 ffs_flushfiles(mp, flags, td)
1319 struct ufsmount *ump;
1325 if (mp->mnt_flag & MNT_QUOTA) {
1327 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1330 for (i = 0; i < MAXQUOTAS; i++) {
1331 error = quotaoff(td, mp, i);
1333 if ((flags & EARLYFLUSH) == 0)
1341 * Here we fall through to vflush again to ensure that
1342 * we have gotten rid of all the system vnodes, unless
1343 * quotas must not be closed.
1347 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1348 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1349 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1351 ffs_snapshot_unmount(mp);
1352 flags |= FORCECLOSE;
1354 * Here we fall through to vflush again to ensure
1355 * that we have gotten rid of all the system vnodes.
1360 * Do not close system files if quotas were not closed, to be
1361 * able to sync the remaining dquots. The freeblks softupdate
1362 * workitems might hold a reference on a dquot, preventing
1363 * quotaoff() from completing. Next round of
1364 * softdep_flushworklist() iteration should process the
1365 * blockers, allowing the next run of quotaoff() to finally
1366 * flush held dquots.
1368 * Otherwise, flush all the files.
1370 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1374 * Flush filesystem metadata.
1376 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1377 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1378 VOP_UNLOCK(ump->um_devvp, 0);
1383 * Get filesystem statistics.
1390 struct ufsmount *ump;
1395 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1396 panic("ffs_statfs");
1397 sbp->f_version = STATFS_VERSION;
1398 sbp->f_bsize = fs->fs_fsize;
1399 sbp->f_iosize = fs->fs_bsize;
1400 sbp->f_blocks = fs->fs_dsize;
1402 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1403 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1404 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1405 dbtofsb(fs, fs->fs_pendingblocks);
1406 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1407 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1409 sbp->f_namemax = UFS_MAXNAMLEN;
1414 sync_doupdate(struct inode *ip)
1417 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1422 * For a lazy sync, we only care about access times, quotas and the
1423 * superblock. Other filesystem changes are already converted to
1424 * cylinder group blocks or inode blocks updates and are written to
1431 struct vnode *mvp, *vp;
1434 int allerror, error;
1438 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1440 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1441 if (vp->v_type == VNON) {
1448 * The IN_ACCESS flag is converted to IN_MODIFIED by
1449 * ufs_close() and ufs_getattr() by the calls to
1450 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1451 * Test also all the other timestamp flags too, to pick up
1452 * any other cases that could be missed.
1454 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1458 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1461 if (sync_doupdate(ip))
1462 error = ffs_update(vp, 0);
1473 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1474 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1480 * Go through the disk queues to initiate sandbagged IO;
1481 * go through the inodes to write those that have been modified;
1482 * initiate the writing of the super block if it has been modified.
1484 * Note: we are always called with the filesystem marked busy using
1488 ffs_sync(mp, waitfor)
1492 struct vnode *mvp, *vp, *devvp;
1495 struct ufsmount *ump = VFSTOUFS(mp);
1497 int error, count, lockreq, allerror = 0;
1500 int secondary_writes;
1501 int secondary_accwrites;
1503 int softdep_accdeps;
1510 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1511 panic("%s: ffs_sync: modification on read-only filesystem",
1513 if (waitfor == MNT_LAZY) {
1515 return (ffs_sync_lazy(mp));
1516 waitfor = MNT_NOWAIT;
1520 * Write back each (modified) inode.
1522 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1523 if (waitfor == MNT_SUSPEND) {
1527 if (waitfor == MNT_WAIT)
1528 lockreq = LK_EXCLUSIVE;
1529 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1531 /* Grab snapshot of secondary write counts */
1533 secondary_writes = mp->mnt_secondary_writes;
1534 secondary_accwrites = mp->mnt_secondary_accwrites;
1537 /* Grab snapshot of softdep dependency counts */
1538 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1540 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1542 * Depend on the vnode interlock to keep things stable enough
1543 * for a quick test. Since there might be hundreds of
1544 * thousands of vnodes, we cannot afford even a subroutine
1545 * call unless there's a good chance that we have work to do.
1547 if (vp->v_type == VNON) {
1553 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1554 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1558 if ((error = vget(vp, lockreq, td)) != 0) {
1559 if (error == ENOENT || error == ENOLCK) {
1560 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1565 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1570 * Force stale filesystem control information to be flushed.
1572 if (waitfor == MNT_WAIT || rebooting) {
1573 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1575 /* Flushed work items may create new vnodes to clean */
1576 if (allerror == 0 && count)
1583 devvp = ump->um_devvp;
1584 bo = &devvp->v_bufobj;
1586 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1588 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1589 error = VOP_FSYNC(devvp, waitfor, td);
1590 VOP_UNLOCK(devvp, 0);
1591 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1592 error = ffs_sbupdate(ump, waitfor, 0);
1595 if (allerror == 0 && waitfor == MNT_WAIT)
1597 } else if (suspend != 0) {
1598 if (softdep_check_suspend(mp,
1603 secondary_accwrites) != 0) {
1605 goto loop; /* More work needed */
1607 mtx_assert(MNT_MTX(mp), MA_OWNED);
1608 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1614 * Write back modified superblock.
1616 if (fs->fs_fmod != 0 &&
1617 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1623 ffs_vget(mp, ino, flags, vpp)
1629 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1633 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1642 struct ufsmount *ump;
1647 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1648 if (error || *vpp != NULL)
1652 * We must promote to an exclusive lock for vnode creation. This
1653 * can happen if lookup is passed LOCKSHARED.
1655 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1656 flags &= ~LK_TYPE_MASK;
1657 flags |= LK_EXCLUSIVE;
1661 * We do not lock vnode creation as it is believed to be too
1662 * expensive for such rare case as simultaneous creation of vnode
1663 * for same ino by different processes. We just allow them to race
1664 * and check later to decide who wins. Let the race begin!
1669 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1671 /* Allocate a new vnode/inode. */
1672 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1673 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1676 uma_zfree(uma_inode, ip);
1680 * FFS supports recursive locking.
1682 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1685 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1690 ip->i_nextclustercg = -1;
1691 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1695 for (i = 0; i < MAXQUOTAS; i++)
1696 ip->i_dquot[i] = NODQUOT;
1700 if (ffs_flags & FFSV_FORCEINSMQ)
1701 vp->v_vflag |= VV_FORCEINSMQ;
1702 error = insmntque(vp, mp);
1704 uma_zfree(uma_inode, ip);
1708 vp->v_vflag &= ~VV_FORCEINSMQ;
1709 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1710 if (error || *vpp != NULL)
1713 /* Read in the disk contents for the inode, copy into the inode. */
1714 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1715 (int)fs->fs_bsize, NOCRED, &bp);
1718 * The inode does not contain anything useful, so it would
1719 * be misleading to leave it on its hash chain. With mode
1720 * still zero, it will be unlinked and returned to the free
1729 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1731 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1732 ffs_load_inode(bp, ip, fs, ino);
1733 if (DOINGSOFTDEP(vp))
1734 softdep_load_inodeblock(ip);
1736 ip->i_effnlink = ip->i_nlink;
1740 * Initialize the vnode from the inode, check for aliases.
1741 * Note that the underlying vnode may have changed.
1743 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1752 * Finish inode initialization.
1754 if (vp->v_type != VFIFO) {
1755 /* FFS supports shared locking for all files except fifos. */
1760 * Set up a generation number for this inode if it does not
1761 * already have one. This should only happen on old filesystems.
1763 if (ip->i_gen == 0) {
1764 while (ip->i_gen == 0)
1765 ip->i_gen = arc4random();
1766 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1767 ip->i_flag |= IN_MODIFIED;
1768 DIP_SET(ip, i_gen, ip->i_gen);
1772 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1774 * If this vnode is already allocated, and we're running
1775 * multi-label, attempt to perform a label association
1776 * from the extended attributes on the inode.
1778 error = mac_vnode_associate_extattr(mp, vp);
1780 /* ufs_inactive will release ip->i_devvp ref. */
1793 * File handle to vnode
1795 * Have to be really careful about stale file handles:
1796 * - check that the inode number is valid
1797 * - for UFS2 check that the inode number is initialized
1798 * - call ffs_vget() to get the locked inode
1799 * - check for an unallocated inode (i_mode == 0)
1800 * - check that the given client host has export rights and return
1801 * those rights via. exflagsp and credanonp
1804 ffs_fhtovp(mp, fhp, flags, vpp)
1811 struct ufsmount *ump;
1819 ufhp = (struct ufid *)fhp;
1820 ino = ufhp->ufid_ino;
1823 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1826 * Need to check if inode is initialized because UFS2 does lazy
1827 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1829 if (fs->fs_magic != FS_UFS2_MAGIC)
1830 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1831 cg = ino_to_cg(fs, ino);
1832 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0)
1834 if (ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1839 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1843 * Initialize the filesystem.
1847 struct vfsconf *vfsp;
1850 ffs_susp_initialize();
1851 softdep_initialize();
1852 return (ufs_init(vfsp));
1856 * Undo the work of ffs_init().
1860 struct vfsconf *vfsp;
1864 ret = ufs_uninit(vfsp);
1865 softdep_uninitialize();
1866 ffs_susp_uninitialize();
1871 * Structure used to pass information from ffs_sbupdate to its
1872 * helper routine ffs_use_bwrite.
1875 struct ufsmount *ump;
1883 * Write a superblock and associated information back to disk.
1886 ffs_sbupdate(ump, waitfor, suspended)
1887 struct ufsmount *ump;
1896 if (fs->fs_ronly == 1 &&
1897 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1898 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1899 panic("ffs_sbupdate: write read-only filesystem");
1901 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1903 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1904 (int)fs->fs_sbsize, 0, 0, 0);
1906 * Initialize info needed for write function.
1910 devfd.waitfor = waitfor;
1911 devfd.suspended = suspended;
1913 return (ffs_sbput(&devfd, fs, fs->fs_sblockloc, ffs_use_bwrite));
1917 * Write function for use by filesystem-layer routines.
1920 ffs_use_bwrite(void *devfd, off_t loc, void *buf, int size)
1922 struct devfd *devfdp;
1923 struct ufsmount *ump;
1932 * Writing the superblock summary information.
1934 if (loc != fs->fs_sblockloc) {
1935 bp = getblk(ump->um_devvp, btodb(loc), size, 0, 0, 0);
1936 bcopy(buf, bp->b_data, (u_int)size);
1937 if (devfdp->suspended)
1938 bp->b_flags |= B_VALIDSUSPWRT;
1939 if (devfdp->waitfor != MNT_WAIT)
1941 else if ((error = bwrite(bp)) != 0)
1942 devfdp->error = error;
1946 * Writing the superblock itself. We need to do special checks for it.
1949 if (devfdp->error != 0) {
1951 return (devfdp->error);
1953 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1954 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1955 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1956 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1957 fs->fs_sblockloc = SBLOCK_UFS1;
1959 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1960 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1961 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1962 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1963 fs->fs_sblockloc = SBLOCK_UFS2;
1965 if (MOUNTEDSOFTDEP(ump->um_mountp))
1966 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1967 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1968 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1969 if (devfdp->suspended)
1970 bp->b_flags |= B_VALIDSUSPWRT;
1971 if (devfdp->waitfor != MNT_WAIT)
1973 else if ((error = bwrite(bp)) != 0)
1974 devfdp->error = error;
1975 return (devfdp->error);
1979 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1980 int attrnamespace, const char *attrname)
1984 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1987 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1993 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1996 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1997 uma_zfree(uma_ufs1, ip->i_din1);
1998 else if (ip->i_din2 != NULL)
1999 uma_zfree(uma_ufs2, ip->i_din2);
2000 uma_zfree(uma_inode, ip);
2003 static int dobkgrdwrite = 1;
2004 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2005 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2008 * Complete a background write started from bwrite.
2011 ffs_backgroundwritedone(struct buf *bp)
2013 struct bufobj *bufobj;
2017 * Find the original buffer that we are writing.
2019 bufobj = bp->b_bufobj;
2021 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2022 panic("backgroundwritedone: lost buffer");
2025 * We should mark the cylinder group buffer origbp as
2026 * dirty, to not loose the failed write.
2028 if ((bp->b_ioflags & BIO_ERROR) != 0)
2029 origbp->b_vflags |= BV_BKGRDERR;
2032 * Process dependencies then return any unfinished ones.
2034 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2037 if (!LIST_EMPTY(&bp->b_dep))
2038 softdep_move_dependencies(bp, origbp);
2041 * This buffer is marked B_NOCACHE so when it is released
2042 * by biodone it will be tossed.
2044 bp->b_flags |= B_NOCACHE;
2045 bp->b_flags &= ~B_CACHE;
2049 * Prevent brelse() from trying to keep and re-dirtying bp on
2050 * errors. It causes b_bufobj dereference in
2051 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2054 if ((bp->b_ioflags & BIO_ERROR) != 0)
2055 bp->b_flags |= B_INVAL;
2059 * Clear the BV_BKGRDINPROG flag in the original buffer
2060 * and awaken it if it is waiting for the write to complete.
2061 * If BV_BKGRDINPROG is not set in the original buffer it must
2062 * have been released and re-instantiated - which is not legal.
2064 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2065 ("backgroundwritedone: lost buffer2"));
2066 origbp->b_vflags &= ~BV_BKGRDINPROG;
2067 if (origbp->b_vflags & BV_BKGRDWAIT) {
2068 origbp->b_vflags &= ~BV_BKGRDWAIT;
2069 wakeup(&origbp->b_xflags);
2076 * Write, release buffer on completion. (Done by iodone
2077 * if async). Do not bother writing anything if the buffer
2080 * Note that we set B_CACHE here, indicating that buffer is
2081 * fully valid and thus cacheable. This is true even of NFS
2082 * now so we set it generally. This could be set either here
2083 * or in biodone() since the I/O is synchronous. We put it
2087 ffs_bufwrite(struct buf *bp)
2092 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2093 if (bp->b_flags & B_INVAL) {
2098 if (!BUF_ISLOCKED(bp))
2099 panic("bufwrite: buffer is not busy???");
2101 * If a background write is already in progress, delay
2102 * writing this block if it is asynchronous. Otherwise
2103 * wait for the background write to complete.
2105 BO_LOCK(bp->b_bufobj);
2106 if (bp->b_vflags & BV_BKGRDINPROG) {
2107 if (bp->b_flags & B_ASYNC) {
2108 BO_UNLOCK(bp->b_bufobj);
2112 bp->b_vflags |= BV_BKGRDWAIT;
2113 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2115 if (bp->b_vflags & BV_BKGRDINPROG)
2116 panic("bufwrite: still writing");
2118 bp->b_vflags &= ~BV_BKGRDERR;
2119 BO_UNLOCK(bp->b_bufobj);
2122 * If this buffer is marked for background writing and we
2123 * do not have to wait for it, make a copy and write the
2124 * copy so as to leave this buffer ready for further use.
2126 * This optimization eats a lot of memory. If we have a page
2127 * or buffer shortfall we can't do it.
2129 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2130 (bp->b_flags & B_ASYNC) &&
2131 !vm_page_count_severe() &&
2132 !buf_dirty_count_severe()) {
2133 KASSERT(bp->b_iodone == NULL,
2134 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2136 /* get a new block */
2137 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2141 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2142 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2143 BO_LOCK(bp->b_bufobj);
2144 bp->b_vflags |= BV_BKGRDINPROG;
2145 BO_UNLOCK(bp->b_bufobj);
2147 (bp->b_xflags & BX_FSPRIV) | BX_BKGRDMARKER;
2148 newbp->b_lblkno = bp->b_lblkno;
2149 newbp->b_blkno = bp->b_blkno;
2150 newbp->b_offset = bp->b_offset;
2151 newbp->b_iodone = ffs_backgroundwritedone;
2152 newbp->b_flags |= B_ASYNC;
2153 newbp->b_flags &= ~B_INVAL;
2154 pbgetvp(bp->b_vp, newbp);
2158 * Move over the dependencies. If there are rollbacks,
2159 * leave the parent buffer dirtied as it will need to
2162 if (LIST_EMPTY(&bp->b_dep) ||
2163 softdep_move_dependencies(bp, newbp) == 0)
2170 * Initiate write on the copy, release the original. The
2171 * BKGRDINPROG flag prevents it from going away until
2172 * the background write completes. We have to recalculate
2173 * its check hash in case the buffer gets freed and then
2174 * reconstituted from the buffer cache during a later read.
2176 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2177 cgp = (struct cg *)bp->b_data;
2180 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2185 /* Mark the buffer clean */
2189 /* Let the normal bufwrite do the rest for us */
2192 * If we are writing a cylinder group, update its time.
2194 if ((bp->b_xflags & BX_CYLGRP) != 0) {
2195 cgp = (struct cg *)bp->b_data;
2196 cgp->cg_old_time = cgp->cg_time = time_second;
2198 return (bufwrite(bp));
2203 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2210 if (bp->b_iocmd == BIO_WRITE) {
2211 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2212 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2213 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2214 panic("ffs_geom_strategy: bad I/O");
2215 nocopy = bp->b_flags & B_NOCOPY;
2216 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2217 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2218 vp->v_rdev->si_snapdata != NULL) {
2219 if ((bp->b_flags & B_CLUSTER) != 0) {
2220 runningbufwakeup(bp);
2221 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2222 b_cluster.cluster_entry) {
2223 error = ffs_copyonwrite(vp, tbp);
2225 error != EOPNOTSUPP) {
2226 bp->b_error = error;
2227 bp->b_ioflags |= BIO_ERROR;
2232 bp->b_runningbufspace = bp->b_bufsize;
2233 atomic_add_long(&runningbufspace,
2234 bp->b_runningbufspace);
2236 error = ffs_copyonwrite(vp, bp);
2237 if (error != 0 && error != EOPNOTSUPP) {
2238 bp->b_error = error;
2239 bp->b_ioflags |= BIO_ERROR;
2246 if ((bp->b_flags & B_CLUSTER) != 0) {
2247 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2248 b_cluster.cluster_entry) {
2249 if (!LIST_EMPTY(&tbp->b_dep))
2253 if (!LIST_EMPTY(&bp->b_dep))
2259 * Check for metadata that needs check-hashes and update them.
2261 switch (bp->b_xflags & BX_FSPRIV) {
2263 ((struct cg *)bp->b_data)->cg_ckhash = 0;
2264 ((struct cg *)bp->b_data)->cg_ckhash =
2265 calculate_crc32c(~0L, bp->b_data, bp->b_bcount);
2272 printf("Check-hash write is unimplemented!!!\n");
2279 printf("multiple buffer types 0x%b\n",
2280 (u_int)(bp->b_xflags & BX_FSPRIV),
2281 PRINT_UFS_BUF_XFLAGS);
2285 g_vfs_strategy(bo, bp);
2289 ffs_own_mount(const struct mount *mp)
2292 if (mp->mnt_op == &ufs_vfsops)
2300 /* defined in ffs_softdep.c */
2301 extern void db_print_ffs(struct ufsmount *ump);
2303 DB_SHOW_COMMAND(ffs, db_show_ffs)
2306 struct ufsmount *ump;
2309 ump = VFSTOUFS((struct mount *)addr);
2314 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2315 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2316 db_print_ffs(VFSTOUFS(mp));
2320 #endif /* SOFTUPDATES */