2 * Copyright (c) 1989, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
45 #include <sys/taskqueue.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
52 #include <sys/fcntl.h>
53 #include <sys/ioccom.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/rwlock.h>
57 #include <sys/vmmeter.h>
59 #include <security/mac/mac_framework.h>
61 #include <ufs/ufs/dir.h>
62 #include <ufs/ufs/extattr.h>
63 #include <ufs/ufs/gjournal.h>
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/fs.h>
70 #include <ufs/ffs/ffs_extern.h>
74 #include <vm/vm_page.h>
76 #include <geom/geom.h>
77 #include <geom/geom_vfs.h>
81 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
83 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
84 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
86 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
87 static int ffs_sync_lazy(struct mount *mp);
89 static vfs_init_t ffs_init;
90 static vfs_uninit_t ffs_uninit;
91 static vfs_extattrctl_t ffs_extattrctl;
92 static vfs_cmount_t ffs_cmount;
93 static vfs_unmount_t ffs_unmount;
94 static vfs_mount_t ffs_mount;
95 static vfs_statfs_t ffs_statfs;
96 static vfs_fhtovp_t ffs_fhtovp;
97 static vfs_sync_t ffs_sync;
99 static struct vfsops ufs_vfsops = {
100 .vfs_extattrctl = ffs_extattrctl,
101 .vfs_fhtovp = ffs_fhtovp,
102 .vfs_init = ffs_init,
103 .vfs_mount = ffs_mount,
104 .vfs_cmount = ffs_cmount,
105 .vfs_quotactl = ufs_quotactl,
106 .vfs_root = ufs_root,
107 .vfs_statfs = ffs_statfs,
108 .vfs_sync = ffs_sync,
109 .vfs_uninit = ffs_uninit,
110 .vfs_unmount = ffs_unmount,
111 .vfs_vget = ffs_vget,
112 .vfs_susp_clean = process_deferred_inactive,
115 VFS_SET(ufs_vfsops, ufs, 0);
116 MODULE_VERSION(ufs, 1);
118 static b_strategy_t ffs_geom_strategy;
119 static b_write_t ffs_bufwrite;
121 static struct buf_ops ffs_ops = {
123 .bop_write = ffs_bufwrite,
124 .bop_strategy = ffs_geom_strategy,
126 #ifdef NO_FFS_SNAPSHOT
127 .bop_bdflush = bufbdflush,
129 .bop_bdflush = ffs_bdflush,
134 * Note that userquota and groupquota options are not currently used
135 * by UFS/FFS code and generally mount(8) does not pass those options
136 * from userland, but they can be passed by loader(8) via
137 * vfs.root.mountfrom.options.
139 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
140 "noclusterw", "noexec", "export", "force", "from", "groupquota",
141 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
142 "nosymfollow", "sync", "union", "userquota", NULL };
145 ffs_mount(struct mount *mp)
149 struct ufsmount *ump = NULL;
152 int error, error1, flags;
155 struct nameidata ndp;
159 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
161 if (uma_inode == NULL) {
162 uma_inode = uma_zcreate("FFS inode",
163 sizeof(struct inode), NULL, NULL, NULL, NULL,
165 uma_ufs1 = uma_zcreate("FFS1 dinode",
166 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
168 uma_ufs2 = uma_zcreate("FFS2 dinode",
169 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
173 vfs_deleteopt(mp->mnt_optnew, "groupquota");
174 vfs_deleteopt(mp->mnt_optnew, "userquota");
176 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
181 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
182 mntorflags |= MNT_ACLS;
184 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
185 mntorflags |= MNT_SNAPSHOT;
187 * Once we have set the MNT_SNAPSHOT flag, do not
188 * persist "snapshot" in the options list.
190 vfs_deleteopt(mp->mnt_optnew, "snapshot");
191 vfs_deleteopt(mp->mnt_opt, "snapshot");
194 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
195 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
197 * Once we have set the restricted PID, do not
198 * persist "fsckpid" in the options list.
200 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
201 vfs_deleteopt(mp->mnt_opt, "fsckpid");
202 if (mp->mnt_flag & MNT_UPDATE) {
203 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
204 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
206 "Checker enable: Must be read-only");
209 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
211 "Checker enable: Must be read-only");
214 /* Set to -1 if we are done */
219 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
220 if (mntorflags & MNT_ACLS) {
222 "\"acls\" and \"nfsv4acls\" options "
223 "are mutually exclusive");
226 mntorflags |= MNT_NFS4ACLS;
230 mp->mnt_flag |= mntorflags;
233 * If updating, check whether changing from read-only to
234 * read/write; if there is no device name, that's all we do.
236 if (mp->mnt_flag & MNT_UPDATE) {
239 devvp = ump->um_devvp;
240 if (fsckpid == -1 && ump->um_fsckpid > 0) {
241 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
242 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
246 * Return to normal read-only mode.
248 error = g_access(ump->um_cp, 0, -1, 0);
252 if (fs->fs_ronly == 0 &&
253 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
255 * Flush any dirty data and suspend filesystem.
257 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
259 error = vfs_write_suspend_umnt(mp);
263 * Check for and optionally get rid of files open
267 if (mp->mnt_flag & MNT_FORCE)
269 if (MOUNTEDSOFTDEP(mp)) {
270 error = softdep_flushfiles(mp, flags, td);
272 error = ffs_flushfiles(mp, flags, td);
275 vfs_write_resume(mp, 0);
278 if (fs->fs_pendingblocks != 0 ||
279 fs->fs_pendinginodes != 0) {
280 printf("WARNING: %s Update error: blocks %jd "
281 "files %d\n", fs->fs_fsmnt,
282 (intmax_t)fs->fs_pendingblocks,
283 fs->fs_pendinginodes);
284 fs->fs_pendingblocks = 0;
285 fs->fs_pendinginodes = 0;
287 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
289 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
292 vfs_write_resume(mp, 0);
295 if (MOUNTEDSOFTDEP(mp))
299 * Drop our write and exclusive access.
301 g_access(ump->um_cp, 0, -1, -1);
305 mp->mnt_flag |= MNT_RDONLY;
308 * Allow the writers to note that filesystem
311 vfs_write_resume(mp, 0);
313 if ((mp->mnt_flag & MNT_RELOAD) &&
314 (error = ffs_reload(mp, td, 0)) != 0)
317 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
319 * If we are running a checker, do not allow upgrade.
321 if (ump->um_fsckpid > 0) {
323 "Active checker, cannot upgrade to write");
327 * If upgrade to read-write by non-root, then verify
328 * that user has necessary permissions on the device.
330 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
331 error = VOP_ACCESS(devvp, VREAD | VWRITE,
334 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
336 VOP_UNLOCK(devvp, 0);
339 VOP_UNLOCK(devvp, 0);
340 fs->fs_flags &= ~FS_UNCLEAN;
341 if (fs->fs_clean == 0) {
342 fs->fs_flags |= FS_UNCLEAN;
343 if ((mp->mnt_flag & MNT_FORCE) ||
345 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
346 (fs->fs_flags & FS_DOSOFTDEP))) {
347 printf("WARNING: %s was not properly "
348 "dismounted\n", fs->fs_fsmnt);
351 "R/W mount of %s denied. %s.%s",
353 "Filesystem is not clean - run fsck",
354 (fs->fs_flags & FS_SUJ) == 0 ? "" :
355 " Forced mount will invalidate"
356 " journal contents");
362 * Request exclusive write access.
364 error = g_access(ump->um_cp, 0, 1, 1);
368 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
372 mp->mnt_flag &= ~MNT_RDONLY;
374 fs->fs_mtime = time_second;
375 /* check to see if we need to start softdep */
376 if ((fs->fs_flags & FS_DOSOFTDEP) &&
377 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
378 vn_finished_write(mp);
382 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
383 vn_finished_write(mp);
386 if (fs->fs_snapinum[0] != 0)
387 ffs_snapshot_mount(mp);
388 vn_finished_write(mp);
391 * Soft updates is incompatible with "async",
392 * so if we are doing softupdates stop the user
393 * from setting the async flag in an update.
394 * Softdep_mount() clears it in an initial mount
397 if (MOUNTEDSOFTDEP(mp)) {
398 /* XXX: Reset too late ? */
400 mp->mnt_flag &= ~MNT_ASYNC;
404 * Keep MNT_ACLS flag if it is stored in superblock.
406 if ((fs->fs_flags & FS_ACLS) != 0) {
407 /* XXX: Set too late ? */
409 mp->mnt_flag |= MNT_ACLS;
413 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
414 /* XXX: Set too late ? */
416 mp->mnt_flag |= MNT_NFS4ACLS;
420 * If this is a request from fsck to clean up the filesystem,
421 * then allow the specified pid to proceed.
424 if (ump->um_fsckpid != 0) {
426 "Active checker already running on %s",
430 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
431 ("soft updates enabled on read-only file system"));
434 * Request write access.
436 error = g_access(ump->um_cp, 0, 1, 0);
440 "Checker activation failed on %s",
444 ump->um_fsckpid = fsckpid;
445 if (fs->fs_snapinum[0] != 0)
446 ffs_snapshot_mount(mp);
447 fs->fs_mtime = time_second;
450 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
454 * If this is a snapshot request, take the snapshot.
456 if (mp->mnt_flag & MNT_SNAPSHOT)
457 return (ffs_snapshot(mp, fspec));
460 * Must not call namei() while owning busy ref.
466 * Not an update, or updating the name: look up the name
467 * and verify that it refers to a sensible disk device.
469 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
471 if ((mp->mnt_flag & MNT_UPDATE) != 0) {
473 * Unmount does not start if MNT_UPDATE is set. Mount
474 * update busies mp before setting MNT_UPDATE. We
475 * must be able to retain our busy ref succesfully,
478 error1 = vfs_busy(mp, MBF_NOWAIT);
483 NDFREE(&ndp, NDF_ONLY_PNBUF);
485 if (!vn_isdisk(devvp, &error)) {
491 * If mount by non-root, then verify that user has necessary
492 * permissions on the device.
495 if ((mp->mnt_flag & MNT_RDONLY) == 0)
497 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
499 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
505 if (mp->mnt_flag & MNT_UPDATE) {
509 * If it's not the same vnode, or at least the same device
510 * then it's not correct.
513 if (devvp->v_rdev != ump->um_devvp->v_rdev)
514 error = EINVAL; /* needs translation */
522 * We need the name for the mount point (also used for
523 * "last mounted on") copied in. If an error occurs,
524 * the mount point is discarded by the upper level code.
525 * Note that vfs_mount_alloc() populates f_mntonname for us.
527 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
532 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
533 ("soft updates enabled on read-only file system"));
538 * Request write access.
540 error = g_access(ump->um_cp, 0, 1, 0);
543 printf("WARNING: %s: Checker activation "
544 "failed\n", fs->fs_fsmnt);
546 ump->um_fsckpid = fsckpid;
547 if (fs->fs_snapinum[0] != 0)
548 ffs_snapshot_mount(mp);
549 fs->fs_mtime = time_second;
551 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
555 vfs_mountedfrom(mp, fspec);
560 * Compatibility with old mount system call.
564 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
566 struct ufs_args args;
567 struct export_args exp;
572 error = copyin(data, &args, sizeof args);
575 vfs_oexport_conv(&args.export, &exp);
577 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
578 ma = mount_arg(ma, "export", &exp, sizeof(exp));
579 error = kernel_mount(ma, flags);
585 * Reload all incore data for a filesystem (used after running fsck on
586 * the root filesystem and finding things to fix). If the 'force' flag
587 * is 0, the filesystem must be mounted read-only.
589 * Things to do to update the mount:
590 * 1) invalidate all cached meta-data.
591 * 2) re-read superblock from disk.
592 * 3) re-read summary information from disk.
593 * 4) invalidate all inactive vnodes.
594 * 5) clear MNTK_SUSPEND2 and MNTK_SUSPENDED flags, allowing secondary
595 * writers, if requested.
596 * 6) invalidate all cached file data.
597 * 7) re-read inode data for all active vnodes.
600 ffs_reload(struct mount *mp, struct thread *td, int flags)
602 struct vnode *vp, *mvp, *devvp;
606 struct fs *fs, *newfs;
607 struct ufsmount *ump;
608 ufs2_daddr_t sblockloc;
616 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (flags & FFSR_FORCE) == 0) {
623 * Step 1: invalidate all cached meta-data.
625 devvp = VFSTOUFS(mp)->um_devvp;
626 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
627 if (vinvalbuf(devvp, 0, 0, 0) != 0)
628 panic("ffs_reload: dirty1");
629 VOP_UNLOCK(devvp, 0);
632 * Step 2: re-read superblock from disk.
634 fs = VFSTOUFS(mp)->um_fs;
635 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
638 newfs = (struct fs *)bp->b_data;
639 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
640 newfs->fs_magic != FS_UFS2_MAGIC) ||
641 newfs->fs_bsize > MAXBSIZE ||
642 newfs->fs_bsize < sizeof(struct fs)) {
644 return (EIO); /* XXX needs translation */
647 * Copy pointer fields back into superblock before copying in XXX
648 * new superblock. These should really be in the ufsmount. XXX
649 * Note that important parameters (eg fs_ncg) are unchanged.
651 newfs->fs_csp = fs->fs_csp;
652 newfs->fs_maxcluster = fs->fs_maxcluster;
653 newfs->fs_contigdirs = fs->fs_contigdirs;
654 newfs->fs_active = fs->fs_active;
655 newfs->fs_ronly = fs->fs_ronly;
656 sblockloc = fs->fs_sblockloc;
657 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
659 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
660 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
662 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
663 printf("WARNING: %s: reload pending error: blocks %jd "
664 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
665 fs->fs_pendinginodes);
666 fs->fs_pendingblocks = 0;
667 fs->fs_pendinginodes = 0;
672 * Step 3: re-read summary information from disk.
674 size = fs->fs_cssize;
675 blks = howmany(size, fs->fs_fsize);
676 if (fs->fs_contigsumsize > 0)
677 size += fs->fs_ncg * sizeof(int32_t);
678 size += fs->fs_ncg * sizeof(u_int8_t);
679 free(fs->fs_csp, M_UFSMNT);
680 space = malloc(size, M_UFSMNT, M_WAITOK);
682 for (i = 0; i < blks; i += fs->fs_frag) {
684 if (i + fs->fs_frag > blks)
685 size = (blks - i) * fs->fs_fsize;
686 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
690 bcopy(bp->b_data, space, (u_int)size);
691 space = (char *)space + size;
695 * We no longer know anything about clusters per cylinder group.
697 if (fs->fs_contigsumsize > 0) {
698 fs->fs_maxcluster = lp = space;
699 for (i = 0; i < fs->fs_ncg; i++)
700 *lp++ = fs->fs_contigsumsize;
703 size = fs->fs_ncg * sizeof(u_int8_t);
704 fs->fs_contigdirs = (u_int8_t *)space;
705 bzero(fs->fs_contigdirs, size);
706 if ((flags & FFSR_UNSUSPEND) != 0) {
708 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2);
709 wakeup(&mp->mnt_flag);
714 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
718 if (vp->v_type == VNON) {
723 * Step 4: invalidate all cached file data.
725 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
726 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
729 if (vinvalbuf(vp, 0, 0, 0))
730 panic("ffs_reload: dirty2");
732 * Step 5: re-read inode data for all active vnodes.
736 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
737 (int)fs->fs_bsize, NOCRED, &bp);
741 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
744 ffs_load_inode(bp, ip, fs, ip->i_number);
745 ip->i_effnlink = ip->i_nlink;
754 * Possible superblock locations ordered from most to least likely.
756 static int sblock_try[] = SBLOCKSEARCH;
759 * Common code for mount and mountroot
762 ffs_mountfs(devvp, mp, td)
767 struct ufsmount *ump;
772 ufs2_daddr_t sblockloc;
773 int error, i, blks, len, ronly;
777 struct g_consumer *cp;
782 cred = td ? td->td_ucred : NOCRED;
783 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
785 KASSERT(devvp->v_type == VCHR, ("reclaimed devvp"));
787 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
788 (uintptr_t)mp) == 0) {
789 VOP_UNLOCK(devvp, 0);
793 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
796 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
797 VOP_UNLOCK(devvp, 0);
801 devvp->v_bufobj.bo_ops = &ffs_ops;
802 VOP_UNLOCK(devvp, 0);
803 if (dev->si_iosize_max != 0)
804 mp->mnt_iosize_max = dev->si_iosize_max;
805 if (mp->mnt_iosize_max > MAXPHYS)
806 mp->mnt_iosize_max = MAXPHYS;
811 * Try reading the superblock in each of its possible locations.
813 for (i = 0; sblock_try[i] != -1; i++) {
814 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
817 "Invalid sectorsize %d for superblock size %d",
818 cp->provider->sectorsize, SBLOCKSIZE);
821 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
824 fs = (struct fs *)bp->b_data;
825 sblockloc = sblock_try[i];
826 if ((fs->fs_magic == FS_UFS1_MAGIC ||
827 (fs->fs_magic == FS_UFS2_MAGIC &&
828 (fs->fs_sblockloc == sblockloc ||
829 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
830 fs->fs_bsize <= MAXBSIZE &&
831 fs->fs_bsize >= sizeof(struct fs))
836 if (sblock_try[i] == -1) {
837 error = EINVAL; /* XXX needs translation */
841 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indices */
842 fs->fs_flags &= ~FS_UNCLEAN;
843 if (fs->fs_clean == 0) {
844 fs->fs_flags |= FS_UNCLEAN;
845 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
846 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
847 (fs->fs_flags & FS_DOSOFTDEP))) {
848 printf("WARNING: %s was not properly dismounted\n",
851 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
852 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
853 (fs->fs_flags & FS_SUJ) == 0 ? "" :
854 " Forced mount will invalidate journal contents");
858 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
859 (mp->mnt_flag & MNT_FORCE)) {
860 printf("WARNING: %s: lost blocks %jd files %d\n",
861 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
862 fs->fs_pendinginodes);
863 fs->fs_pendingblocks = 0;
864 fs->fs_pendinginodes = 0;
867 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
868 printf("WARNING: %s: mount pending error: blocks %jd "
869 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
870 fs->fs_pendinginodes);
871 fs->fs_pendingblocks = 0;
872 fs->fs_pendinginodes = 0;
874 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
877 * Get journal provider name.
880 mp->mnt_gjprovider = malloc((u_long)len, M_UFSMNT, M_WAITOK);
881 if (g_io_getattr("GJOURNAL::provider", cp, &len,
882 mp->mnt_gjprovider) == 0) {
883 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, len,
886 mp->mnt_flag |= MNT_GJOURNAL;
889 printf("WARNING: %s: GJOURNAL flag on fs "
890 "but no gjournal provider below\n",
891 mp->mnt_stat.f_mntonname);
892 free(mp->mnt_gjprovider, M_UFSMNT);
893 mp->mnt_gjprovider = NULL;
896 printf("WARNING: %s: GJOURNAL flag on fs but no "
897 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
900 mp->mnt_gjprovider = NULL;
902 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
904 ump->um_bo = &devvp->v_bufobj;
905 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
906 if (fs->fs_magic == FS_UFS1_MAGIC) {
907 ump->um_fstype = UFS1;
908 ump->um_balloc = ffs_balloc_ufs1;
910 ump->um_fstype = UFS2;
911 ump->um_balloc = ffs_balloc_ufs2;
913 ump->um_blkatoff = ffs_blkatoff;
914 ump->um_truncate = ffs_truncate;
915 ump->um_update = ffs_update;
916 ump->um_valloc = ffs_valloc;
917 ump->um_vfree = ffs_vfree;
918 ump->um_ifree = ffs_ifree;
919 ump->um_rdonly = ffs_rdonly;
920 ump->um_snapgone = ffs_snapgone;
921 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
922 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
923 if (fs->fs_sbsize < SBLOCKSIZE)
924 bp->b_flags |= B_INVAL | B_NOCACHE;
928 ffs_oldfscompat_read(fs, ump, sblockloc);
929 fs->fs_ronly = ronly;
930 size = fs->fs_cssize;
931 blks = howmany(size, fs->fs_fsize);
932 if (fs->fs_contigsumsize > 0)
933 size += fs->fs_ncg * sizeof(int32_t);
934 size += fs->fs_ncg * sizeof(u_int8_t);
935 space = malloc(size, M_UFSMNT, M_WAITOK);
937 for (i = 0; i < blks; i += fs->fs_frag) {
939 if (i + fs->fs_frag > blks)
940 size = (blks - i) * fs->fs_fsize;
941 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
943 free(fs->fs_csp, M_UFSMNT);
946 bcopy(bp->b_data, space, (u_int)size);
947 space = (char *)space + size;
951 if (fs->fs_contigsumsize > 0) {
952 fs->fs_maxcluster = lp = space;
953 for (i = 0; i < fs->fs_ncg; i++)
954 *lp++ = fs->fs_contigsumsize;
957 size = fs->fs_ncg * sizeof(u_int8_t);
958 fs->fs_contigdirs = (u_int8_t *)space;
959 bzero(fs->fs_contigdirs, size);
960 fs->fs_active = NULL;
962 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
963 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
965 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
966 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
971 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
973 mp->mnt_flag |= MNT_LOCAL;
975 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
978 mp->mnt_flag |= MNT_MULTILABEL;
981 printf("WARNING: %s: multilabel flag on fs but "
982 "no MAC support\n", mp->mnt_stat.f_mntonname);
985 if ((fs->fs_flags & FS_ACLS) != 0) {
989 if (mp->mnt_flag & MNT_NFS4ACLS)
990 printf("WARNING: %s: ACLs flag on fs conflicts with "
991 "\"nfsv4acls\" mount option; option ignored\n",
992 mp->mnt_stat.f_mntonname);
993 mp->mnt_flag &= ~MNT_NFS4ACLS;
994 mp->mnt_flag |= MNT_ACLS;
998 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
999 mp->mnt_stat.f_mntonname);
1002 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
1006 if (mp->mnt_flag & MNT_ACLS)
1007 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
1008 "with \"acls\" mount option; option ignored\n",
1009 mp->mnt_stat.f_mntonname);
1010 mp->mnt_flag &= ~MNT_ACLS;
1011 mp->mnt_flag |= MNT_NFS4ACLS;
1015 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
1016 "ACLs support\n", mp->mnt_stat.f_mntonname);
1019 if ((fs->fs_flags & FS_TRIM) != 0) {
1021 if (g_io_getattr("GEOM::candelete", cp, &len,
1022 &ump->um_candelete) == 0) {
1023 if (!ump->um_candelete)
1024 printf("WARNING: %s: TRIM flag on fs but disk "
1025 "does not support TRIM\n",
1026 mp->mnt_stat.f_mntonname);
1028 printf("WARNING: %s: TRIM flag on fs but disk does "
1029 "not confirm that it supports TRIM\n",
1030 mp->mnt_stat.f_mntonname);
1031 ump->um_candelete = 0;
1033 if (ump->um_candelete) {
1034 ump->um_trim_tq = taskqueue_create("trim", M_WAITOK,
1035 taskqueue_thread_enqueue, &ump->um_trim_tq);
1036 taskqueue_start_threads(&ump->um_trim_tq, 1, PVFS,
1037 "%s trim", mp->mnt_stat.f_mntonname);
1041 ump->um_mountp = mp;
1043 ump->um_devvp = devvp;
1044 ump->um_nindir = fs->fs_nindir;
1045 ump->um_bptrtodb = fs->fs_fsbtodb;
1046 ump->um_seqinc = fs->fs_frag;
1047 for (i = 0; i < MAXQUOTAS; i++)
1048 ump->um_quotas[i] = NULLVP;
1050 ufs_extattr_uepm_init(&ump->um_extattr);
1053 * Set FS local "last mounted on" information (NULL pad)
1055 bzero(fs->fs_fsmnt, MAXMNTLEN);
1056 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1057 mp->mnt_stat.f_iosize = fs->fs_bsize;
1059 if (mp->mnt_flag & MNT_ROOTFS) {
1061 * Root mount; update timestamp in mount structure.
1062 * this will be used by the common root mount code
1063 * to update the system clock.
1065 mp->mnt_time = fs->fs_time;
1069 fs->fs_mtime = time_second;
1070 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1071 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1072 free(fs->fs_csp, M_UFSMNT);
1073 ffs_flushfiles(mp, FORCECLOSE, td);
1076 if (fs->fs_snapinum[0] != 0)
1077 ffs_snapshot_mount(mp);
1080 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1083 * Initialize filesystem state information in mount struct.
1086 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1087 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1090 #ifdef UFS_EXTATTR_AUTOSTART
1093 * Auto-starting does the following:
1094 * - check for /.attribute in the fs, and extattr_start if so
1095 * - for each file in .attribute, enable that file with
1096 * an attribute of the same name.
1097 * Not clear how to report errors -- probably eat them.
1098 * This would all happen while the filesystem was busy/not
1099 * available, so would effectively be "atomic".
1101 (void) ufs_extattr_autostart(mp, td);
1102 #endif /* !UFS_EXTATTR_AUTOSTART */
1103 #endif /* !UFS_EXTATTR */
1111 g_topology_unlock();
1114 mtx_destroy(UFS_MTX(ump));
1115 if (mp->mnt_gjprovider != NULL) {
1116 free(mp->mnt_gjprovider, M_UFSMNT);
1117 mp->mnt_gjprovider = NULL;
1119 free(ump->um_fs, M_UFSMNT);
1120 free(ump, M_UFSMNT);
1121 mp->mnt_data = NULL;
1123 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
1128 #include <sys/sysctl.h>
1129 static int bigcgs = 0;
1130 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1133 * Sanity checks for loading old filesystem superblocks.
1134 * See ffs_oldfscompat_write below for unwound actions.
1136 * XXX - Parts get retired eventually.
1137 * Unfortunately new bits get added.
1140 ffs_oldfscompat_read(fs, ump, sblockloc)
1142 struct ufsmount *ump;
1143 ufs2_daddr_t sblockloc;
1148 * If not yet done, update fs_flags location and value of fs_sblockloc.
1150 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1151 fs->fs_flags = fs->fs_old_flags;
1152 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1153 fs->fs_sblockloc = sblockloc;
1156 * If not yet done, update UFS1 superblock with new wider fields.
1158 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1159 fs->fs_maxbsize = fs->fs_bsize;
1160 fs->fs_time = fs->fs_old_time;
1161 fs->fs_size = fs->fs_old_size;
1162 fs->fs_dsize = fs->fs_old_dsize;
1163 fs->fs_csaddr = fs->fs_old_csaddr;
1164 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1165 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1166 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1167 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1169 if (fs->fs_magic == FS_UFS1_MAGIC &&
1170 fs->fs_old_inodefmt < FS_44INODEFMT) {
1171 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1172 fs->fs_qbmask = ~fs->fs_bmask;
1173 fs->fs_qfmask = ~fs->fs_fmask;
1175 if (fs->fs_magic == FS_UFS1_MAGIC) {
1176 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1177 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1178 if (fs->fs_maxfilesize > maxfilesize)
1179 fs->fs_maxfilesize = maxfilesize;
1181 /* Compatibility for old filesystems */
1182 if (fs->fs_avgfilesize <= 0)
1183 fs->fs_avgfilesize = AVFILESIZ;
1184 if (fs->fs_avgfpdir <= 0)
1185 fs->fs_avgfpdir = AFPDIR;
1187 fs->fs_save_cgsize = fs->fs_cgsize;
1188 fs->fs_cgsize = fs->fs_bsize;
1193 * Unwinding superblock updates for old filesystems.
1194 * See ffs_oldfscompat_read above for details.
1196 * XXX - Parts get retired eventually.
1197 * Unfortunately new bits get added.
1200 ffs_oldfscompat_write(fs, ump)
1202 struct ufsmount *ump;
1206 * Copy back UFS2 updated fields that UFS1 inspects.
1208 if (fs->fs_magic == FS_UFS1_MAGIC) {
1209 fs->fs_old_time = fs->fs_time;
1210 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1211 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1212 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1213 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1214 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1217 fs->fs_cgsize = fs->fs_save_cgsize;
1218 fs->fs_save_cgsize = 0;
1223 * unmount system call
1226 ffs_unmount(mp, mntflags)
1231 struct ufsmount *ump = VFSTOUFS(mp);
1233 int error, flags, susp;
1242 if (mntflags & MNT_FORCE) {
1243 flags |= FORCECLOSE;
1244 susp = fs->fs_ronly == 0;
1247 if ((error = ufs_extattr_stop(mp, td))) {
1248 if (error != EOPNOTSUPP)
1249 printf("WARNING: unmount %s: ufs_extattr_stop "
1250 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1254 ufs_extattr_uepm_destroy(&ump->um_extattr);
1259 error = vfs_write_suspend_umnt(mp);
1263 if (MOUNTEDSOFTDEP(mp))
1264 error = softdep_flushfiles(mp, flags, td);
1266 error = ffs_flushfiles(mp, flags, td);
1267 if (error != 0 && error != ENXIO)
1271 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1272 printf("WARNING: unmount %s: pending error: blocks %jd "
1273 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1274 fs->fs_pendinginodes);
1275 fs->fs_pendingblocks = 0;
1276 fs->fs_pendinginodes = 0;
1279 if (MOUNTEDSOFTDEP(mp))
1280 softdep_unmount(mp);
1281 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1282 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1283 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1284 if (error && error != ENXIO) {
1290 vfs_write_resume(mp, VR_START_WRITE);
1291 if (ump->um_trim_tq != NULL) {
1292 while (ump->um_trim_inflight != 0)
1293 pause("ufsutr", hz);
1294 taskqueue_drain_all(ump->um_trim_tq);
1295 taskqueue_free(ump->um_trim_tq);
1298 if (ump->um_fsckpid > 0) {
1300 * Return to normal read-only mode.
1302 error = g_access(ump->um_cp, 0, -1, 0);
1303 ump->um_fsckpid = 0;
1305 g_vfs_close(ump->um_cp);
1306 g_topology_unlock();
1307 atomic_store_rel_ptr((uintptr_t *)&ump->um_dev->si_mountpt, 0);
1308 vrele(ump->um_devvp);
1309 dev_rel(ump->um_dev);
1310 mtx_destroy(UFS_MTX(ump));
1311 if (mp->mnt_gjprovider != NULL) {
1312 free(mp->mnt_gjprovider, M_UFSMNT);
1313 mp->mnt_gjprovider = NULL;
1315 free(fs->fs_csp, M_UFSMNT);
1317 free(ump, M_UFSMNT);
1318 mp->mnt_data = NULL;
1320 mp->mnt_flag &= ~MNT_LOCAL;
1326 vfs_write_resume(mp, VR_START_WRITE);
1330 ufs_extattr_uepm_init(&ump->um_extattr);
1331 #ifdef UFS_EXTATTR_AUTOSTART
1332 (void) ufs_extattr_autostart(mp, td);
1341 * Flush out all the files in a filesystem.
1344 ffs_flushfiles(mp, flags, td)
1349 struct ufsmount *ump;
1355 if (mp->mnt_flag & MNT_QUOTA) {
1357 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1360 for (i = 0; i < MAXQUOTAS; i++) {
1361 error = quotaoff(td, mp, i);
1363 if ((flags & EARLYFLUSH) == 0)
1371 * Here we fall through to vflush again to ensure that
1372 * we have gotten rid of all the system vnodes, unless
1373 * quotas must not be closed.
1377 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1378 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1379 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1381 ffs_snapshot_unmount(mp);
1382 flags |= FORCECLOSE;
1384 * Here we fall through to vflush again to ensure
1385 * that we have gotten rid of all the system vnodes.
1390 * Do not close system files if quotas were not closed, to be
1391 * able to sync the remaining dquots. The freeblks softupdate
1392 * workitems might hold a reference on a dquot, preventing
1393 * quotaoff() from completing. Next round of
1394 * softdep_flushworklist() iteration should process the
1395 * blockers, allowing the next run of quotaoff() to finally
1396 * flush held dquots.
1398 * Otherwise, flush all the files.
1400 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1404 * Flush filesystem metadata.
1406 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1407 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1408 VOP_UNLOCK(ump->um_devvp, 0);
1413 * Get filesystem statistics.
1420 struct ufsmount *ump;
1425 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1426 panic("ffs_statfs");
1427 sbp->f_version = STATFS_VERSION;
1428 sbp->f_bsize = fs->fs_fsize;
1429 sbp->f_iosize = fs->fs_bsize;
1430 sbp->f_blocks = fs->fs_dsize;
1432 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1433 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1434 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1435 dbtofsb(fs, fs->fs_pendingblocks);
1436 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
1437 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1439 sbp->f_namemax = UFS_MAXNAMLEN;
1444 sync_doupdate(struct inode *ip)
1447 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1452 * For a lazy sync, we only care about access times, quotas and the
1453 * superblock. Other filesystem changes are already converted to
1454 * cylinder group blocks or inode blocks updates and are written to
1461 struct vnode *mvp, *vp;
1464 int allerror, error;
1468 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1470 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1471 if (vp->v_type == VNON) {
1478 * The IN_ACCESS flag is converted to IN_MODIFIED by
1479 * ufs_close() and ufs_getattr() by the calls to
1480 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1481 * Test also all the other timestamp flags too, to pick up
1482 * any other cases that could be missed.
1484 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1488 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1491 if (sync_doupdate(ip))
1492 error = ffs_update(vp, 0);
1503 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1504 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1510 * Go through the disk queues to initiate sandbagged IO;
1511 * go through the inodes to write those that have been modified;
1512 * initiate the writing of the super block if it has been modified.
1514 * Note: we are always called with the filesystem marked busy using
1518 ffs_sync(mp, waitfor)
1522 struct vnode *mvp, *vp, *devvp;
1525 struct ufsmount *ump = VFSTOUFS(mp);
1527 int error, count, lockreq, allerror = 0;
1530 int secondary_writes;
1531 int secondary_accwrites;
1533 int softdep_accdeps;
1540 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1541 panic("%s: ffs_sync: modification on read-only filesystem",
1543 if (waitfor == MNT_LAZY) {
1545 return (ffs_sync_lazy(mp));
1546 waitfor = MNT_NOWAIT;
1550 * Write back each (modified) inode.
1552 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1553 if (waitfor == MNT_SUSPEND) {
1557 if (waitfor == MNT_WAIT)
1558 lockreq = LK_EXCLUSIVE;
1559 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1561 /* Grab snapshot of secondary write counts */
1563 secondary_writes = mp->mnt_secondary_writes;
1564 secondary_accwrites = mp->mnt_secondary_accwrites;
1567 /* Grab snapshot of softdep dependency counts */
1568 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1570 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1572 * Depend on the vnode interlock to keep things stable enough
1573 * for a quick test. Since there might be hundreds of
1574 * thousands of vnodes, we cannot afford even a subroutine
1575 * call unless there's a good chance that we have work to do.
1577 if (vp->v_type == VNON) {
1583 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1584 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1588 if ((error = vget(vp, lockreq, td)) != 0) {
1589 if (error == ENOENT || error == ENOLCK) {
1590 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1595 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1600 * Force stale filesystem control information to be flushed.
1602 if (waitfor == MNT_WAIT || rebooting) {
1603 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1605 /* Flushed work items may create new vnodes to clean */
1606 if (allerror == 0 && count)
1613 devvp = ump->um_devvp;
1614 bo = &devvp->v_bufobj;
1616 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1618 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1619 error = VOP_FSYNC(devvp, waitfor, td);
1620 VOP_UNLOCK(devvp, 0);
1621 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1622 error = ffs_sbupdate(ump, waitfor, 0);
1625 if (allerror == 0 && waitfor == MNT_WAIT)
1627 } else if (suspend != 0) {
1628 if (softdep_check_suspend(mp,
1633 secondary_accwrites) != 0) {
1635 goto loop; /* More work needed */
1637 mtx_assert(MNT_MTX(mp), MA_OWNED);
1638 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1644 * Write back modified superblock.
1646 if (fs->fs_fmod != 0 &&
1647 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1653 ffs_vget(mp, ino, flags, vpp)
1659 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1663 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1672 struct ufsmount *ump;
1677 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1678 if (error || *vpp != NULL)
1682 * We must promote to an exclusive lock for vnode creation. This
1683 * can happen if lookup is passed LOCKSHARED.
1685 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1686 flags &= ~LK_TYPE_MASK;
1687 flags |= LK_EXCLUSIVE;
1691 * We do not lock vnode creation as it is believed to be too
1692 * expensive for such rare case as simultaneous creation of vnode
1693 * for same ino by different processes. We just allow them to race
1694 * and check later to decide who wins. Let the race begin!
1699 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1701 /* Allocate a new vnode/inode. */
1702 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1703 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1706 uma_zfree(uma_inode, ip);
1710 * FFS supports recursive locking.
1712 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1715 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1720 ip->i_nextclustercg = -1;
1721 ip->i_flag = fs->fs_magic == FS_UFS1_MAGIC ? 0 : IN_UFS2;
1725 for (i = 0; i < MAXQUOTAS; i++)
1726 ip->i_dquot[i] = NODQUOT;
1730 if (ffs_flags & FFSV_FORCEINSMQ)
1731 vp->v_vflag |= VV_FORCEINSMQ;
1732 error = insmntque(vp, mp);
1734 uma_zfree(uma_inode, ip);
1738 vp->v_vflag &= ~VV_FORCEINSMQ;
1739 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1740 if (error || *vpp != NULL)
1743 /* Read in the disk contents for the inode, copy into the inode. */
1744 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1745 (int)fs->fs_bsize, NOCRED, &bp);
1748 * The inode does not contain anything useful, so it would
1749 * be misleading to leave it on its hash chain. With mode
1750 * still zero, it will be unlinked and returned to the free
1759 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1761 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1762 ffs_load_inode(bp, ip, fs, ino);
1763 if (DOINGSOFTDEP(vp))
1764 softdep_load_inodeblock(ip);
1766 ip->i_effnlink = ip->i_nlink;
1770 * Initialize the vnode from the inode, check for aliases.
1771 * Note that the underlying vnode may have changed.
1773 error = ufs_vinit(mp, I_IS_UFS1(ip) ? &ffs_fifoops1 : &ffs_fifoops2,
1782 * Finish inode initialization.
1784 if (vp->v_type != VFIFO) {
1785 /* FFS supports shared locking for all files except fifos. */
1790 * Set up a generation number for this inode if it does not
1791 * already have one. This should only happen on old filesystems.
1793 if (ip->i_gen == 0) {
1794 while (ip->i_gen == 0)
1795 ip->i_gen = arc4random();
1796 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1797 ip->i_flag |= IN_MODIFIED;
1798 DIP_SET(ip, i_gen, ip->i_gen);
1802 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1804 * If this vnode is already allocated, and we're running
1805 * multi-label, attempt to perform a label association
1806 * from the extended attributes on the inode.
1808 error = mac_vnode_associate_extattr(mp, vp);
1810 /* ufs_inactive will release ip->i_devvp ref. */
1823 * File handle to vnode
1825 * Have to be really careful about stale file handles:
1826 * - check that the inode number is valid
1827 * - for UFS2 check that the inode number is initialized
1828 * - call ffs_vget() to get the locked inode
1829 * - check for an unallocated inode (i_mode == 0)
1830 * - check that the given client host has export rights and return
1831 * those rights via. exflagsp and credanonp
1834 ffs_fhtovp(mp, fhp, flags, vpp)
1841 struct ufsmount *ump;
1849 ufhp = (struct ufid *)fhp;
1850 ino = ufhp->ufid_ino;
1853 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
1856 * Need to check if inode is initialized because UFS2 does lazy
1857 * initialization and nfs_fhtovp can offer arbitrary inode numbers.
1859 if (fs->fs_magic != FS_UFS2_MAGIC)
1860 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1861 cg = ino_to_cg(fs, ino);
1862 error = bread(ump->um_devvp, fsbtodb(fs, cgtod(fs, cg)),
1863 (int)fs->fs_cgsize, NOCRED, &bp);
1866 cgp = (struct cg *)bp->b_data;
1867 if (!cg_chkmagic(cgp) || ino >= cg * fs->fs_ipg + cgp->cg_initediblk) {
1872 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1876 * Initialize the filesystem.
1880 struct vfsconf *vfsp;
1883 ffs_susp_initialize();
1884 softdep_initialize();
1885 return (ufs_init(vfsp));
1889 * Undo the work of ffs_init().
1893 struct vfsconf *vfsp;
1897 ret = ufs_uninit(vfsp);
1898 softdep_uninitialize();
1899 ffs_susp_uninitialize();
1904 * Write a superblock and associated information back to disk.
1907 ffs_sbupdate(ump, waitfor, suspended)
1908 struct ufsmount *ump;
1912 struct fs *fs = ump->um_fs;
1917 int i, size, error, allerror = 0;
1919 if (fs->fs_ronly == 1 &&
1920 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1921 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1922 panic("ffs_sbupdate: write read-only filesystem");
1924 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1926 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1927 (int)fs->fs_sbsize, 0, 0, 0);
1929 * First write back the summary information.
1931 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1933 for (i = 0; i < blks; i += fs->fs_frag) {
1934 size = fs->fs_bsize;
1935 if (i + fs->fs_frag > blks)
1936 size = (blks - i) * fs->fs_fsize;
1937 bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1939 bcopy(space, bp->b_data, (u_int)size);
1940 space = (char *)space + size;
1942 bp->b_flags |= B_VALIDSUSPWRT;
1943 if (waitfor != MNT_WAIT)
1945 else if ((error = bwrite(bp)) != 0)
1949 * Now write back the superblock itself. If any errors occurred
1950 * up to this point, then fail so that the superblock avoids
1951 * being written out as clean.
1958 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1959 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1960 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1961 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1962 fs->fs_sblockloc = SBLOCK_UFS1;
1964 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1965 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1966 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1967 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1968 fs->fs_sblockloc = SBLOCK_UFS2;
1971 fs->fs_time = time_second;
1972 if (MOUNTEDSOFTDEP(ump->um_mountp))
1973 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1974 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1975 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1977 bp->b_flags |= B_VALIDSUSPWRT;
1978 if (waitfor != MNT_WAIT)
1980 else if ((error = bwrite(bp)) != 0)
1986 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1987 int attrnamespace, const char *attrname)
1991 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1994 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
2000 ffs_ifree(struct ufsmount *ump, struct inode *ip)
2003 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
2004 uma_zfree(uma_ufs1, ip->i_din1);
2005 else if (ip->i_din2 != NULL)
2006 uma_zfree(uma_ufs2, ip->i_din2);
2007 uma_zfree(uma_inode, ip);
2010 static int dobkgrdwrite = 1;
2011 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
2012 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
2015 * Complete a background write started from bwrite.
2018 ffs_backgroundwritedone(struct buf *bp)
2020 struct bufobj *bufobj;
2024 * Find the original buffer that we are writing.
2026 bufobj = bp->b_bufobj;
2028 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
2029 panic("backgroundwritedone: lost buffer");
2032 * We should mark the cylinder group buffer origbp as
2033 * dirty, to not loose the failed write.
2035 if ((bp->b_ioflags & BIO_ERROR) != 0)
2036 origbp->b_vflags |= BV_BKGRDERR;
2039 * Process dependencies then return any unfinished ones.
2042 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
2045 if (!LIST_EMPTY(&bp->b_dep))
2046 softdep_move_dependencies(bp, origbp);
2049 * This buffer is marked B_NOCACHE so when it is released
2050 * by biodone it will be tossed.
2052 bp->b_flags |= B_NOCACHE;
2053 bp->b_flags &= ~B_CACHE;
2056 * Prevent brelse() from trying to keep and re-dirtying bp on
2057 * errors. It causes b_bufobj dereference in
2058 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2061 if ((bp->b_ioflags & BIO_ERROR) != 0)
2062 bp->b_flags |= B_INVAL;
2066 * Clear the BV_BKGRDINPROG flag in the original buffer
2067 * and awaken it if it is waiting for the write to complete.
2068 * If BV_BKGRDINPROG is not set in the original buffer it must
2069 * have been released and re-instantiated - which is not legal.
2071 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2072 ("backgroundwritedone: lost buffer2"));
2073 origbp->b_vflags &= ~BV_BKGRDINPROG;
2074 if (origbp->b_vflags & BV_BKGRDWAIT) {
2075 origbp->b_vflags &= ~BV_BKGRDWAIT;
2076 wakeup(&origbp->b_xflags);
2083 * Write, release buffer on completion. (Done by iodone
2084 * if async). Do not bother writing anything if the buffer
2087 * Note that we set B_CACHE here, indicating that buffer is
2088 * fully valid and thus cacheable. This is true even of NFS
2089 * now so we set it generally. This could be set either here
2090 * or in biodone() since the I/O is synchronous. We put it
2094 ffs_bufwrite(struct buf *bp)
2098 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2099 if (bp->b_flags & B_INVAL) {
2104 if (!BUF_ISLOCKED(bp))
2105 panic("bufwrite: buffer is not busy???");
2107 * If a background write is already in progress, delay
2108 * writing this block if it is asynchronous. Otherwise
2109 * wait for the background write to complete.
2111 BO_LOCK(bp->b_bufobj);
2112 if (bp->b_vflags & BV_BKGRDINPROG) {
2113 if (bp->b_flags & B_ASYNC) {
2114 BO_UNLOCK(bp->b_bufobj);
2118 bp->b_vflags |= BV_BKGRDWAIT;
2119 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2121 if (bp->b_vflags & BV_BKGRDINPROG)
2122 panic("bufwrite: still writing");
2124 bp->b_vflags &= ~BV_BKGRDERR;
2125 BO_UNLOCK(bp->b_bufobj);
2128 * If this buffer is marked for background writing and we
2129 * do not have to wait for it, make a copy and write the
2130 * copy so as to leave this buffer ready for further use.
2132 * This optimization eats a lot of memory. If we have a page
2133 * or buffer shortfall we can't do it.
2135 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2136 (bp->b_flags & B_ASYNC) &&
2137 !vm_page_count_severe() &&
2138 !buf_dirty_count_severe()) {
2139 KASSERT(bp->b_iodone == NULL,
2140 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2142 /* get a new block */
2143 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2147 KASSERT(buf_mapped(bp), ("Unmapped cg"));
2148 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2149 BO_LOCK(bp->b_bufobj);
2150 bp->b_vflags |= BV_BKGRDINPROG;
2151 BO_UNLOCK(bp->b_bufobj);
2152 newbp->b_xflags |= BX_BKGRDMARKER;
2153 newbp->b_lblkno = bp->b_lblkno;
2154 newbp->b_blkno = bp->b_blkno;
2155 newbp->b_offset = bp->b_offset;
2156 newbp->b_iodone = ffs_backgroundwritedone;
2157 newbp->b_flags |= B_ASYNC;
2158 newbp->b_flags &= ~B_INVAL;
2159 pbgetvp(bp->b_vp, newbp);
2163 * Move over the dependencies. If there are rollbacks,
2164 * leave the parent buffer dirtied as it will need to
2167 if (LIST_EMPTY(&bp->b_dep) ||
2168 softdep_move_dependencies(bp, newbp) == 0)
2175 * Initiate write on the copy, release the original. The
2176 * BKGRDINPROG flag prevents it from going away until
2177 * the background write completes.
2182 /* Mark the buffer clean */
2186 /* Let the normal bufwrite do the rest for us */
2188 return (bufwrite(bp));
2193 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2201 if (bp->b_iocmd == BIO_WRITE) {
2202 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2203 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2204 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2205 panic("ffs_geom_strategy: bad I/O");
2206 nocopy = bp->b_flags & B_NOCOPY;
2207 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2208 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2209 vp->v_rdev->si_snapdata != NULL) {
2210 if ((bp->b_flags & B_CLUSTER) != 0) {
2211 runningbufwakeup(bp);
2212 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2213 b_cluster.cluster_entry) {
2214 error = ffs_copyonwrite(vp, tbp);
2216 error != EOPNOTSUPP) {
2217 bp->b_error = error;
2218 bp->b_ioflags |= BIO_ERROR;
2223 bp->b_runningbufspace = bp->b_bufsize;
2224 atomic_add_long(&runningbufspace,
2225 bp->b_runningbufspace);
2227 error = ffs_copyonwrite(vp, bp);
2228 if (error != 0 && error != EOPNOTSUPP) {
2229 bp->b_error = error;
2230 bp->b_ioflags |= BIO_ERROR;
2237 if ((bp->b_flags & B_CLUSTER) != 0) {
2238 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2239 b_cluster.cluster_entry) {
2240 if (!LIST_EMPTY(&tbp->b_dep))
2244 if (!LIST_EMPTY(&bp->b_dep))
2250 g_vfs_strategy(bo, bp);
2254 ffs_own_mount(const struct mount *mp)
2257 if (mp->mnt_op == &ufs_vfsops)
2265 /* defined in ffs_softdep.c */
2266 extern void db_print_ffs(struct ufsmount *ump);
2268 DB_SHOW_COMMAND(ffs, db_show_ffs)
2271 struct ufsmount *ump;
2274 ump = VFSTOUFS((struct mount *)addr);
2279 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2280 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2281 db_print_ffs(VFSTOUFS(mp));
2285 #endif /* SOFTUPDATES */