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 * 4. 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/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <security/mac/mac_framework.h>
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/gjournal.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_extern.h>
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
69 #include <vm/vm_page.h>
71 #include <geom/geom.h>
72 #include <geom/geom_vfs.h>
76 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
78 static int ffs_reload(struct mount *, struct thread *);
79 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
80 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
82 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
83 static int ffs_sync_lazy(struct mount *mp);
85 static vfs_init_t ffs_init;
86 static vfs_uninit_t ffs_uninit;
87 static vfs_extattrctl_t ffs_extattrctl;
88 static vfs_cmount_t ffs_cmount;
89 static vfs_unmount_t ffs_unmount;
90 static vfs_mount_t ffs_mount;
91 static vfs_statfs_t ffs_statfs;
92 static vfs_fhtovp_t ffs_fhtovp;
93 static vfs_sync_t ffs_sync;
95 static struct vfsops ufs_vfsops = {
96 .vfs_extattrctl = ffs_extattrctl,
97 .vfs_fhtovp = ffs_fhtovp,
99 .vfs_mount = ffs_mount,
100 .vfs_cmount = ffs_cmount,
101 .vfs_quotactl = ufs_quotactl,
102 .vfs_root = ufs_root,
103 .vfs_statfs = ffs_statfs,
104 .vfs_sync = ffs_sync,
105 .vfs_uninit = ffs_uninit,
106 .vfs_unmount = ffs_unmount,
107 .vfs_vget = ffs_vget,
108 .vfs_susp_clean = process_deferred_inactive,
111 VFS_SET(ufs_vfsops, ufs, 0);
112 MODULE_VERSION(ufs, 1);
114 static b_strategy_t ffs_geom_strategy;
115 static b_write_t ffs_bufwrite;
117 static struct buf_ops ffs_ops = {
119 .bop_write = ffs_bufwrite,
120 .bop_strategy = ffs_geom_strategy,
122 #ifdef NO_FFS_SNAPSHOT
123 .bop_bdflush = bufbdflush,
125 .bop_bdflush = ffs_bdflush,
130 * Note that userquota and groupquota options are not currently used
131 * by UFS/FFS code and generally mount(8) does not pass those options
132 * from userland, but they can be passed by loader(8) via
133 * vfs.root.mountfrom.options.
135 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
136 "noclusterw", "noexec", "export", "force", "from", "groupquota",
137 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
138 "nosymfollow", "sync", "union", "userquota", NULL };
141 ffs_mount(struct mount *mp)
145 struct ufsmount *ump = 0;
151 struct nameidata ndp;
155 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
157 if (uma_inode == NULL) {
158 uma_inode = uma_zcreate("FFS inode",
159 sizeof(struct inode), NULL, NULL, NULL, NULL,
161 uma_ufs1 = uma_zcreate("FFS1 dinode",
162 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
164 uma_ufs2 = uma_zcreate("FFS2 dinode",
165 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
169 vfs_deleteopt(mp->mnt_optnew, "groupquota");
170 vfs_deleteopt(mp->mnt_optnew, "userquota");
172 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
177 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
178 mntorflags |= MNT_ACLS;
180 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
181 mntorflags |= MNT_SNAPSHOT;
183 * Once we have set the MNT_SNAPSHOT flag, do not
184 * persist "snapshot" in the options list.
186 vfs_deleteopt(mp->mnt_optnew, "snapshot");
187 vfs_deleteopt(mp->mnt_opt, "snapshot");
190 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
191 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
193 * Once we have set the restricted PID, do not
194 * persist "fsckpid" in the options list.
196 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
197 vfs_deleteopt(mp->mnt_opt, "fsckpid");
198 if (mp->mnt_flag & MNT_UPDATE) {
199 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
200 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
202 "Checker enable: Must be read-only");
205 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
207 "Checker enable: Must be read-only");
210 /* Set to -1 if we are done */
215 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
216 if (mntorflags & MNT_ACLS) {
218 "\"acls\" and \"nfsv4acls\" options "
219 "are mutually exclusive");
222 mntorflags |= MNT_NFS4ACLS;
226 mp->mnt_flag |= mntorflags;
229 * If updating, check whether changing from read-only to
230 * read/write; if there is no device name, that's all we do.
232 if (mp->mnt_flag & MNT_UPDATE) {
235 devvp = ump->um_devvp;
236 if (fsckpid == -1 && ump->um_fsckpid > 0) {
237 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
238 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
243 * Return to normal read-only mode.
245 error = g_access(ump->um_cp, 0, -1, 0);
250 if (fs->fs_ronly == 0 &&
251 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
253 * Flush any dirty data and suspend filesystem.
255 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
258 vn_finished_write(mp);
259 if ((error = vfs_write_suspend(mp)) != 0)
262 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
264 * Allow the secondary writes
267 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
269 wakeup(&mp->mnt_flag);
272 * Allow the curthread to
273 * ignore the suspension to
274 * synchronize on-disk state.
276 td->td_pflags |= TDP_IGNSUSP;
280 vn_start_write(NULL, &mp, V_WAIT);
283 * Check for and optionally get rid of files open
287 if (mp->mnt_flag & MNT_FORCE)
289 if (MOUNTEDSOFTDEP(mp)) {
290 error = softdep_flushfiles(mp, flags, td);
292 error = ffs_flushfiles(mp, flags, td);
295 vfs_write_resume(mp);
298 if (fs->fs_pendingblocks != 0 ||
299 fs->fs_pendinginodes != 0) {
300 printf("WARNING: %s Update error: blocks %jd "
301 "files %d\n", fs->fs_fsmnt,
302 (intmax_t)fs->fs_pendingblocks,
303 fs->fs_pendinginodes);
304 fs->fs_pendingblocks = 0;
305 fs->fs_pendinginodes = 0;
307 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
309 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
312 vfs_write_resume(mp);
315 if (MOUNTEDSOFTDEP(mp))
320 * Drop our write and exclusive access.
322 g_access(ump->um_cp, 0, -1, -1);
327 mp->mnt_flag |= MNT_RDONLY;
330 * Allow the writers to note that filesystem
333 vfs_write_resume(mp);
335 if ((mp->mnt_flag & MNT_RELOAD) &&
336 (error = ffs_reload(mp, td)) != 0)
339 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
341 * If we are running a checker, do not allow upgrade.
343 if (ump->um_fsckpid > 0) {
345 "Active checker, cannot upgrade to write");
349 * If upgrade to read-write by non-root, then verify
350 * that user has necessary permissions on the device.
352 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
353 error = VOP_ACCESS(devvp, VREAD | VWRITE,
356 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
358 VOP_UNLOCK(devvp, 0);
361 VOP_UNLOCK(devvp, 0);
362 fs->fs_flags &= ~FS_UNCLEAN;
363 if (fs->fs_clean == 0) {
364 fs->fs_flags |= FS_UNCLEAN;
365 if ((mp->mnt_flag & MNT_FORCE) ||
367 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
368 (fs->fs_flags & FS_DOSOFTDEP))) {
369 printf("WARNING: %s was not properly "
370 "dismounted\n", fs->fs_fsmnt);
373 "R/W mount of %s denied. %s.%s",
375 "Filesystem is not clean - run fsck",
376 (fs->fs_flags & FS_SUJ) == 0 ? "" :
377 " Forced mount will invalidate"
378 " journal contents");
385 * Request exclusive write access.
387 error = g_access(ump->um_cp, 0, 1, 1);
392 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
396 mp->mnt_flag &= ~MNT_RDONLY;
398 fs->fs_mtime = time_second;
399 /* check to see if we need to start softdep */
400 if ((fs->fs_flags & FS_DOSOFTDEP) &&
401 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
402 vn_finished_write(mp);
406 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
407 vn_finished_write(mp);
410 if (fs->fs_snapinum[0] != 0)
411 ffs_snapshot_mount(mp);
412 vn_finished_write(mp);
415 * Soft updates is incompatible with "async",
416 * so if we are doing softupdates stop the user
417 * from setting the async flag in an update.
418 * Softdep_mount() clears it in an initial mount
421 if (MOUNTEDSOFTDEP(mp)) {
422 /* XXX: Reset too late ? */
424 mp->mnt_flag &= ~MNT_ASYNC;
428 * Keep MNT_ACLS flag if it is stored in superblock.
430 if ((fs->fs_flags & FS_ACLS) != 0) {
431 /* XXX: Set too late ? */
433 mp->mnt_flag |= MNT_ACLS;
437 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
438 /* XXX: Set too late ? */
440 mp->mnt_flag |= MNT_NFS4ACLS;
444 * If this is a request from fsck to clean up the filesystem,
445 * then allow the specified pid to proceed.
448 if (ump->um_fsckpid != 0) {
450 "Active checker already running on %s",
454 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
455 ("soft updates enabled on read-only file system"));
459 * Request write access.
461 error = g_access(ump->um_cp, 0, 1, 0);
466 "Checker activation failed on %s",
470 ump->um_fsckpid = fsckpid;
471 if (fs->fs_snapinum[0] != 0)
472 ffs_snapshot_mount(mp);
473 fs->fs_mtime = time_second;
476 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
480 * If this is a snapshot request, take the snapshot.
482 if (mp->mnt_flag & MNT_SNAPSHOT)
483 return (ffs_snapshot(mp, fspec));
487 * Not an update, or updating the name: look up the name
488 * and verify that it refers to a sensible disk device.
490 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
491 if ((error = namei(&ndp)) != 0)
493 NDFREE(&ndp, NDF_ONLY_PNBUF);
495 if (!vn_isdisk(devvp, &error)) {
501 * If mount by non-root, then verify that user has necessary
502 * permissions on the device.
505 if ((mp->mnt_flag & MNT_RDONLY) == 0)
507 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
509 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
515 if (mp->mnt_flag & MNT_UPDATE) {
519 * If it's not the same vnode, or at least the same device
520 * then it's not correct.
523 if (devvp->v_rdev != ump->um_devvp->v_rdev)
524 error = EINVAL; /* needs translation */
532 * We need the name for the mount point (also used for
533 * "last mounted on") copied in. If an error occurs,
534 * the mount point is discarded by the upper level code.
535 * Note that vfs_mount() populates f_mntonname for us.
537 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
542 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
543 ("soft updates enabled on read-only file system"));
549 * Request write access.
551 error = g_access(ump->um_cp, 0, 1, 0);
555 printf("WARNING: %s: Checker activation "
556 "failed\n", fs->fs_fsmnt);
558 ump->um_fsckpid = fsckpid;
559 if (fs->fs_snapinum[0] != 0)
560 ffs_snapshot_mount(mp);
561 fs->fs_mtime = time_second;
563 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
567 vfs_mountedfrom(mp, fspec);
572 * Compatibility with old mount system call.
576 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
578 struct ufs_args args;
579 struct export_args exp;
584 error = copyin(data, &args, sizeof args);
587 vfs_oexport_conv(&args.export, &exp);
589 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
590 ma = mount_arg(ma, "export", &exp, sizeof(exp));
591 error = kernel_mount(ma, flags);
597 * Reload all incore data for a filesystem (used after running fsck on
598 * the root filesystem and finding things to fix). The filesystem must
599 * be mounted read-only.
601 * Things to do to update the mount:
602 * 1) invalidate all cached meta-data.
603 * 2) re-read superblock from disk.
604 * 3) re-read summary information from disk.
605 * 4) invalidate all inactive vnodes.
606 * 5) invalidate all cached file data.
607 * 6) re-read inode data for all active vnodes.
610 ffs_reload(struct mount *mp, struct thread *td)
612 struct vnode *vp, *mvp, *devvp;
616 struct fs *fs, *newfs;
617 struct ufsmount *ump;
618 ufs2_daddr_t sblockloc;
619 int i, blks, size, error;
622 if ((mp->mnt_flag & MNT_RDONLY) == 0)
626 * Step 1: invalidate all cached meta-data.
628 devvp = VFSTOUFS(mp)->um_devvp;
629 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
630 if (vinvalbuf(devvp, 0, 0, 0) != 0)
631 panic("ffs_reload: dirty1");
632 VOP_UNLOCK(devvp, 0);
635 * Step 2: re-read superblock from disk.
637 fs = VFSTOUFS(mp)->um_fs;
638 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
641 newfs = (struct fs *)bp->b_data;
642 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
643 newfs->fs_magic != FS_UFS2_MAGIC) ||
644 newfs->fs_bsize > MAXBSIZE ||
645 newfs->fs_bsize < sizeof(struct fs)) {
647 return (EIO); /* XXX needs translation */
650 * Copy pointer fields back into superblock before copying in XXX
651 * new superblock. These should really be in the ufsmount. XXX
652 * Note that important parameters (eg fs_ncg) are unchanged.
654 newfs->fs_csp = fs->fs_csp;
655 newfs->fs_maxcluster = fs->fs_maxcluster;
656 newfs->fs_contigdirs = fs->fs_contigdirs;
657 newfs->fs_active = fs->fs_active;
658 /* The file system is still read-only. */
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 blks = howmany(fs->fs_cssize, fs->fs_fsize);
680 for (i = 0; i < blks; i += fs->fs_frag) {
682 if (i + fs->fs_frag > blks)
683 size = (blks - i) * fs->fs_fsize;
684 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
688 bcopy(bp->b_data, space, (u_int)size);
689 space = (char *)space + size;
693 * We no longer know anything about clusters per cylinder group.
695 if (fs->fs_contigsumsize > 0) {
696 lp = fs->fs_maxcluster;
697 for (i = 0; i < fs->fs_ncg; i++)
698 *lp++ = fs->fs_contigsumsize;
703 MNT_VNODE_FOREACH(vp, mp, mvp) {
705 if (vp->v_iflag & VI_DOOMED) {
711 * Step 4: invalidate all cached file data.
713 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
714 MNT_VNODE_FOREACH_ABORT(mp, mvp);
717 if (vinvalbuf(vp, 0, 0, 0))
718 panic("ffs_reload: dirty2");
720 * Step 5: re-read inode data for all active vnodes.
724 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
725 (int)fs->fs_bsize, NOCRED, &bp);
729 MNT_VNODE_FOREACH_ABORT(mp, mvp);
732 ffs_load_inode(bp, ip, fs, ip->i_number);
733 ip->i_effnlink = ip->i_nlink;
744 * Possible superblock locations ordered from most to least likely.
746 static int sblock_try[] = SBLOCKSEARCH;
749 * Common code for mount and mountroot
752 ffs_mountfs(devvp, mp, td)
757 struct ufsmount *ump;
762 ufs2_daddr_t sblockloc;
763 int error, i, blks, size, ronly;
766 struct g_consumer *cp;
771 cred = td ? td->td_ucred : NOCRED;
772 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
778 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
781 VOP_UNLOCK(devvp, 0);
784 if (devvp->v_rdev->si_iosize_max != 0)
785 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
786 if (mp->mnt_iosize_max > MAXPHYS)
787 mp->mnt_iosize_max = MAXPHYS;
789 devvp->v_bufobj.bo_ops = &ffs_ops;
794 * Try reading the superblock in each of its possible locations.
796 for (i = 0; sblock_try[i] != -1; i++) {
797 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
800 "Invalid sectorsize %d for superblock size %d",
801 cp->provider->sectorsize, SBLOCKSIZE);
804 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
807 fs = (struct fs *)bp->b_data;
808 sblockloc = sblock_try[i];
809 if ((fs->fs_magic == FS_UFS1_MAGIC ||
810 (fs->fs_magic == FS_UFS2_MAGIC &&
811 (fs->fs_sblockloc == sblockloc ||
812 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
813 fs->fs_bsize <= MAXBSIZE &&
814 fs->fs_bsize >= sizeof(struct fs))
819 if (sblock_try[i] == -1) {
820 error = EINVAL; /* XXX needs translation */
824 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
825 fs->fs_flags &= ~FS_UNCLEAN;
826 if (fs->fs_clean == 0) {
827 fs->fs_flags |= FS_UNCLEAN;
828 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
829 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
830 (fs->fs_flags & FS_DOSOFTDEP))) {
831 printf("WARNING: %s was not properly dismounted\n",
834 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
835 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
836 (fs->fs_flags & FS_SUJ) == 0 ? "" :
837 " Forced mount will invalidate journal contents");
841 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
842 (mp->mnt_flag & MNT_FORCE)) {
843 printf("WARNING: %s: lost blocks %jd files %d\n",
844 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
845 fs->fs_pendinginodes);
846 fs->fs_pendingblocks = 0;
847 fs->fs_pendinginodes = 0;
850 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
851 printf("WARNING: %s: mount pending error: blocks %jd "
852 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
853 fs->fs_pendinginodes);
854 fs->fs_pendingblocks = 0;
855 fs->fs_pendinginodes = 0;
857 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
860 * Get journal provider name.
863 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
864 if (g_io_getattr("GJOURNAL::provider", cp, &size,
865 mp->mnt_gjprovider) == 0) {
866 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
869 mp->mnt_flag |= MNT_GJOURNAL;
872 printf("WARNING: %s: GJOURNAL flag on fs "
873 "but no gjournal provider below\n",
874 mp->mnt_stat.f_mntonname);
875 free(mp->mnt_gjprovider, M_UFSMNT);
876 mp->mnt_gjprovider = NULL;
879 printf("WARNING: %s: GJOURNAL flag on fs but no "
880 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
883 mp->mnt_gjprovider = NULL;
885 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
887 ump->um_bo = &devvp->v_bufobj;
888 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
889 if (fs->fs_magic == FS_UFS1_MAGIC) {
890 ump->um_fstype = UFS1;
891 ump->um_balloc = ffs_balloc_ufs1;
893 ump->um_fstype = UFS2;
894 ump->um_balloc = ffs_balloc_ufs2;
896 ump->um_blkatoff = ffs_blkatoff;
897 ump->um_truncate = ffs_truncate;
898 ump->um_update = ffs_update;
899 ump->um_valloc = ffs_valloc;
900 ump->um_vfree = ffs_vfree;
901 ump->um_ifree = ffs_ifree;
902 ump->um_rdonly = ffs_rdonly;
903 ump->um_snapgone = ffs_snapgone;
904 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
905 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
906 if (fs->fs_sbsize < SBLOCKSIZE)
907 bp->b_flags |= B_INVAL | B_NOCACHE;
911 ffs_oldfscompat_read(fs, ump, sblockloc);
912 fs->fs_ronly = ronly;
913 size = fs->fs_cssize;
914 blks = howmany(size, fs->fs_fsize);
915 if (fs->fs_contigsumsize > 0)
916 size += fs->fs_ncg * sizeof(int32_t);
917 size += fs->fs_ncg * sizeof(u_int8_t);
918 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
920 for (i = 0; i < blks; i += fs->fs_frag) {
922 if (i + fs->fs_frag > blks)
923 size = (blks - i) * fs->fs_fsize;
924 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
926 free(fs->fs_csp, M_UFSMNT);
929 bcopy(bp->b_data, space, (u_int)size);
930 space = (char *)space + size;
934 if (fs->fs_contigsumsize > 0) {
935 fs->fs_maxcluster = lp = space;
936 for (i = 0; i < fs->fs_ncg; i++)
937 *lp++ = fs->fs_contigsumsize;
940 size = fs->fs_ncg * sizeof(u_int8_t);
941 fs->fs_contigdirs = (u_int8_t *)space;
942 bzero(fs->fs_contigdirs, size);
943 fs->fs_active = NULL;
945 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
946 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
948 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
949 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
954 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
956 mp->mnt_flag |= MNT_LOCAL;
958 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
961 mp->mnt_flag |= MNT_MULTILABEL;
964 printf("WARNING: %s: multilabel flag on fs but "
965 "no MAC support\n", mp->mnt_stat.f_mntonname);
968 if ((fs->fs_flags & FS_ACLS) != 0) {
972 if (mp->mnt_flag & MNT_NFS4ACLS)
973 printf("WARNING: %s: ACLs flag on fs conflicts with "
974 "\"nfsv4acls\" mount option; option ignored\n",
975 mp->mnt_stat.f_mntonname);
976 mp->mnt_flag &= ~MNT_NFS4ACLS;
977 mp->mnt_flag |= MNT_ACLS;
981 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
982 mp->mnt_stat.f_mntonname);
985 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
989 if (mp->mnt_flag & MNT_ACLS)
990 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
991 "with \"acls\" mount option; option ignored\n",
992 mp->mnt_stat.f_mntonname);
993 mp->mnt_flag &= ~MNT_ACLS;
994 mp->mnt_flag |= MNT_NFS4ACLS;
998 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
999 "ACLs support\n", mp->mnt_stat.f_mntonname);
1002 if ((fs->fs_flags & FS_TRIM) != 0) {
1004 if (g_io_getattr("GEOM::candelete", cp, &size,
1005 &ump->um_candelete) == 0) {
1006 if (!ump->um_candelete)
1007 printf("WARNING: %s: TRIM flag on fs but disk "
1008 "does not support TRIM\n",
1009 mp->mnt_stat.f_mntonname);
1011 printf("WARNING: %s: TRIM flag on fs but disk does "
1012 "not confirm that it supports TRIM\n",
1013 mp->mnt_stat.f_mntonname);
1014 ump->um_candelete = 0;
1018 ump->um_mountp = mp;
1020 ump->um_devvp = devvp;
1021 ump->um_nindir = fs->fs_nindir;
1022 ump->um_bptrtodb = fs->fs_fsbtodb;
1023 ump->um_seqinc = fs->fs_frag;
1024 for (i = 0; i < MAXQUOTAS; i++)
1025 ump->um_quotas[i] = NULLVP;
1027 ufs_extattr_uepm_init(&ump->um_extattr);
1030 * Set FS local "last mounted on" information (NULL pad)
1032 bzero(fs->fs_fsmnt, MAXMNTLEN);
1033 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1034 mp->mnt_stat.f_iosize = fs->fs_bsize;
1036 if (mp->mnt_flag & MNT_ROOTFS) {
1038 * Root mount; update timestamp in mount structure.
1039 * this will be used by the common root mount code
1040 * to update the system clock.
1042 mp->mnt_time = fs->fs_time;
1046 fs->fs_mtime = time_second;
1047 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1048 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1049 free(fs->fs_csp, M_UFSMNT);
1050 ffs_flushfiles(mp, FORCECLOSE, td);
1053 if (devvp->v_type == VCHR && devvp->v_rdev != NULL)
1054 devvp->v_rdev->si_mountpt = mp;
1055 if (fs->fs_snapinum[0] != 0)
1056 ffs_snapshot_mount(mp);
1059 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1062 * Initialize filesystem stat information in mount struct.
1065 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
1066 MNTK_EXTENDED_SHARED;
1069 #ifdef UFS_EXTATTR_AUTOSTART
1072 * Auto-starting does the following:
1073 * - check for /.attribute in the fs, and extattr_start if so
1074 * - for each file in .attribute, enable that file with
1075 * an attribute of the same name.
1076 * Not clear how to report errors -- probably eat them.
1077 * This would all happen while the filesystem was busy/not
1078 * available, so would effectively be "atomic".
1080 (void) ufs_extattr_autostart(mp, td);
1081 #endif /* !UFS_EXTATTR_AUTOSTART */
1082 #endif /* !UFS_EXTATTR */
1091 g_topology_unlock();
1095 mtx_destroy(UFS_MTX(ump));
1096 if (mp->mnt_gjprovider != NULL) {
1097 free(mp->mnt_gjprovider, M_UFSMNT);
1098 mp->mnt_gjprovider = NULL;
1100 free(ump->um_fs, M_UFSMNT);
1101 free(ump, M_UFSMNT);
1102 mp->mnt_data = NULL;
1108 #include <sys/sysctl.h>
1109 static int bigcgs = 0;
1110 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1113 * Sanity checks for loading old filesystem superblocks.
1114 * See ffs_oldfscompat_write below for unwound actions.
1116 * XXX - Parts get retired eventually.
1117 * Unfortunately new bits get added.
1120 ffs_oldfscompat_read(fs, ump, sblockloc)
1122 struct ufsmount *ump;
1123 ufs2_daddr_t sblockloc;
1128 * If not yet done, update fs_flags location and value of fs_sblockloc.
1130 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1131 fs->fs_flags = fs->fs_old_flags;
1132 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1133 fs->fs_sblockloc = sblockloc;
1136 * If not yet done, update UFS1 superblock with new wider fields.
1138 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1139 fs->fs_maxbsize = fs->fs_bsize;
1140 fs->fs_time = fs->fs_old_time;
1141 fs->fs_size = fs->fs_old_size;
1142 fs->fs_dsize = fs->fs_old_dsize;
1143 fs->fs_csaddr = fs->fs_old_csaddr;
1144 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1145 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1146 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1147 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1149 if (fs->fs_magic == FS_UFS1_MAGIC &&
1150 fs->fs_old_inodefmt < FS_44INODEFMT) {
1151 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1152 fs->fs_qbmask = ~fs->fs_bmask;
1153 fs->fs_qfmask = ~fs->fs_fmask;
1155 if (fs->fs_magic == FS_UFS1_MAGIC) {
1156 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1157 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1158 if (fs->fs_maxfilesize > maxfilesize)
1159 fs->fs_maxfilesize = maxfilesize;
1161 /* Compatibility for old filesystems */
1162 if (fs->fs_avgfilesize <= 0)
1163 fs->fs_avgfilesize = AVFILESIZ;
1164 if (fs->fs_avgfpdir <= 0)
1165 fs->fs_avgfpdir = AFPDIR;
1167 fs->fs_save_cgsize = fs->fs_cgsize;
1168 fs->fs_cgsize = fs->fs_bsize;
1173 * Unwinding superblock updates for old filesystems.
1174 * See ffs_oldfscompat_read above for details.
1176 * XXX - Parts get retired eventually.
1177 * Unfortunately new bits get added.
1180 ffs_oldfscompat_write(fs, ump)
1182 struct ufsmount *ump;
1186 * Copy back UFS2 updated fields that UFS1 inspects.
1188 if (fs->fs_magic == FS_UFS1_MAGIC) {
1189 fs->fs_old_time = fs->fs_time;
1190 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1191 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1192 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1193 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1194 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1197 fs->fs_cgsize = fs->fs_save_cgsize;
1198 fs->fs_save_cgsize = 0;
1203 * unmount system call
1206 ffs_unmount(mp, mntflags)
1211 struct ufsmount *ump = VFSTOUFS(mp);
1213 int error, flags, susp;
1222 if (mntflags & MNT_FORCE) {
1223 flags |= FORCECLOSE;
1224 susp = fs->fs_ronly != 0;
1227 if ((error = ufs_extattr_stop(mp, td))) {
1228 if (error != EOPNOTSUPP)
1229 printf("WARNING: unmount %s: ufs_extattr_stop "
1230 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1234 ufs_extattr_uepm_destroy(&ump->um_extattr);
1240 * dounmount already called vn_start_write().
1243 vn_finished_write(mp);
1244 if ((error = vfs_write_suspend(mp)) != 0)
1247 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1248 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1250 wakeup(&mp->mnt_flag);
1252 td->td_pflags |= TDP_IGNSUSP;
1256 vn_start_write(NULL, &mp, V_WAIT);
1259 if (MOUNTEDSOFTDEP(mp))
1260 error = softdep_flushfiles(mp, flags, td);
1262 error = ffs_flushfiles(mp, flags, td);
1263 if (error != 0 && error != ENXIO)
1267 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1268 printf("WARNING: unmount %s: pending error: blocks %jd "
1269 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1270 fs->fs_pendinginodes);
1271 fs->fs_pendingblocks = 0;
1272 fs->fs_pendinginodes = 0;
1275 softdep_unmount(mp);
1276 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1277 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1278 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1279 if (error && error != ENXIO) {
1285 vfs_write_resume(mp);
1286 vn_start_write(NULL, &mp, V_WAIT);
1290 if (ump->um_fsckpid > 0) {
1292 * Return to normal read-only mode.
1294 error = g_access(ump->um_cp, 0, -1, 0);
1295 ump->um_fsckpid = 0;
1297 g_vfs_close(ump->um_cp);
1298 g_topology_unlock();
1300 if (ump->um_devvp->v_type == VCHR && ump->um_devvp->v_rdev != NULL)
1301 ump->um_devvp->v_rdev->si_mountpt = NULL;
1302 vrele(ump->um_devvp);
1303 dev_rel(ump->um_dev);
1304 mtx_destroy(UFS_MTX(ump));
1305 if (mp->mnt_gjprovider != NULL) {
1306 free(mp->mnt_gjprovider, M_UFSMNT);
1307 mp->mnt_gjprovider = NULL;
1309 free(fs->fs_csp, M_UFSMNT);
1311 free(ump, M_UFSMNT);
1312 mp->mnt_data = NULL;
1314 mp->mnt_flag &= ~MNT_LOCAL;
1320 vfs_write_resume(mp);
1321 vn_start_write(NULL, &mp, V_WAIT);
1325 ufs_extattr_uepm_init(&ump->um_extattr);
1326 #ifdef UFS_EXTATTR_AUTOSTART
1327 (void) ufs_extattr_autostart(mp, td);
1336 * Flush out all the files in a filesystem.
1339 ffs_flushfiles(mp, flags, td)
1344 struct ufsmount *ump;
1349 if (mp->mnt_flag & MNT_QUOTA) {
1351 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1354 for (i = 0; i < MAXQUOTAS; i++) {
1355 quotaoff(td, mp, i);
1358 * Here we fall through to vflush again to ensure
1359 * that we have gotten rid of all the system vnodes.
1363 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1364 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1365 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1367 ffs_snapshot_unmount(mp);
1368 flags |= FORCECLOSE;
1370 * Here we fall through to vflush again to ensure
1371 * that we have gotten rid of all the system vnodes.
1375 * Flush all the files.
1377 if ((error = vflush(mp, 0, flags, td)) != 0)
1380 * Flush filesystem metadata.
1382 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1383 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1384 VOP_UNLOCK(ump->um_devvp, 0);
1389 * Get filesystem statistics.
1396 struct ufsmount *ump;
1401 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1402 panic("ffs_statfs");
1403 sbp->f_version = STATFS_VERSION;
1404 sbp->f_bsize = fs->fs_fsize;
1405 sbp->f_iosize = fs->fs_bsize;
1406 sbp->f_blocks = fs->fs_dsize;
1408 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1409 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1410 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1411 dbtofsb(fs, fs->fs_pendingblocks);
1412 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1413 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1415 sbp->f_namemax = NAME_MAX;
1420 * For a lazy sync, we only care about access times, quotas and the
1421 * superblock. Other filesystem changes are already converted to
1422 * cylinder group blocks or inode blocks updates and are written to
1429 struct vnode *mvp, *vp;
1432 int allerror, error;
1436 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1439 MNT_VNODE_FOREACH(vp, mp, mvp) {
1441 if (vp->v_iflag & VI_DOOMED || 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 ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1460 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1465 error = ffs_update(vp, 0);
1478 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1479 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1485 * Go through the disk queues to initiate sandbagged IO;
1486 * go through the inodes to write those that have been modified;
1487 * initiate the writing of the super block if it has been modified.
1489 * Note: we are always called with the filesystem marked busy using
1493 ffs_sync(mp, waitfor)
1497 struct vnode *mvp, *vp, *devvp;
1500 struct ufsmount *ump = VFSTOUFS(mp);
1502 int error, count, wait, lockreq, allerror = 0;
1505 int secondary_writes;
1506 int secondary_accwrites;
1508 int softdep_accdeps;
1516 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1517 panic("%s: ffs_sync: modification on read-only filesystem",
1519 if (waitfor == MNT_LAZY)
1520 return (ffs_sync_lazy(mp));
1523 * Write back each (modified) inode.
1525 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1526 if (waitfor == MNT_SUSPEND) {
1530 if (waitfor == MNT_WAIT) {
1532 lockreq = LK_EXCLUSIVE;
1534 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1537 /* Grab snapshot of secondary write counts */
1538 secondary_writes = mp->mnt_secondary_writes;
1539 secondary_accwrites = mp->mnt_secondary_accwrites;
1541 /* Grab snapshot of softdep dependency counts */
1543 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1546 MNT_VNODE_FOREACH(vp, mp, mvp) {
1548 * Depend on the vnode interlock to keep things stable enough
1549 * for a quick test. Since there might be hundreds of
1550 * thousands of vnodes, we cannot afford even a subroutine
1551 * call unless there's a good chance that we have work to do.
1554 if (vp->v_iflag & VI_DOOMED) {
1559 if (vp->v_type == VNON || ((ip->i_flag &
1560 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1561 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1566 if ((error = vget(vp, lockreq, td)) != 0) {
1568 if (error == ENOENT || error == ENOLCK) {
1569 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1574 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1581 * Force stale filesystem control information to be flushed.
1583 if (waitfor == MNT_WAIT) {
1584 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1586 /* Flushed work items may create new vnodes to clean */
1587 if (allerror == 0 && count) {
1596 devvp = ump->um_devvp;
1597 bo = &devvp->v_bufobj;
1599 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1601 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1602 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1604 VOP_UNLOCK(devvp, 0);
1605 if (allerror == 0 && waitfor == MNT_WAIT) {
1609 } else if (suspend != 0) {
1610 if (softdep_check_suspend(mp,
1615 secondary_accwrites) != 0)
1616 goto loop; /* More work needed */
1617 mtx_assert(MNT_MTX(mp), MA_OWNED);
1618 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1624 * Write back modified superblock.
1626 if (fs->fs_fmod != 0 &&
1627 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1633 ffs_vget(mp, ino, flags, vpp)
1639 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1643 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1652 struct ufsmount *ump;
1658 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1659 if (error || *vpp != NULL)
1663 * We must promote to an exclusive lock for vnode creation. This
1664 * can happen if lookup is passed LOCKSHARED.
1666 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1667 flags &= ~LK_TYPE_MASK;
1668 flags |= LK_EXCLUSIVE;
1672 * We do not lock vnode creation as it is believed to be too
1673 * expensive for such rare case as simultaneous creation of vnode
1674 * for same ino by different processes. We just allow them to race
1675 * and check later to decide who wins. Let the race begin!
1683 * If this malloc() is performed after the getnewvnode()
1684 * it might block, leaving a vnode with a NULL v_data to be
1685 * found by ffs_sync() if a sync happens to fire right then,
1686 * which will cause a panic because ffs_sync() blindly
1687 * dereferences vp->v_data (as well it should).
1689 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1691 /* Allocate a new vnode/inode. */
1692 if (fs->fs_magic == FS_UFS1_MAGIC)
1693 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1695 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1698 uma_zfree(uma_inode, ip);
1702 * FFS supports recursive locking.
1704 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1707 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1717 for (i = 0; i < MAXQUOTAS; i++)
1718 ip->i_dquot[i] = NODQUOT;
1722 if (ffs_flags & FFSV_FORCEINSMQ)
1723 vp->v_vflag |= VV_FORCEINSMQ;
1724 error = insmntque(vp, mp);
1726 uma_zfree(uma_inode, ip);
1730 vp->v_vflag &= ~VV_FORCEINSMQ;
1731 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1732 if (error || *vpp != NULL)
1735 /* Read in the disk contents for the inode, copy into the inode. */
1736 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1737 (int)fs->fs_bsize, NOCRED, &bp);
1740 * The inode does not contain anything useful, so it would
1741 * be misleading to leave it on its hash chain. With mode
1742 * still zero, it will be unlinked and returned to the free
1750 if (ip->i_ump->um_fstype == UFS1)
1751 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1753 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1754 ffs_load_inode(bp, ip, fs, ino);
1755 if (DOINGSOFTDEP(vp))
1756 softdep_load_inodeblock(ip);
1758 ip->i_effnlink = ip->i_nlink;
1762 * Initialize the vnode from the inode, check for aliases.
1763 * Note that the underlying vnode may have changed.
1765 if (ip->i_ump->um_fstype == UFS1)
1766 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1768 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1776 * Finish inode initialization.
1778 if (vp->v_type != VFIFO) {
1779 /* FFS supports shared locking for all files except fifos. */
1784 * Set up a generation number for this inode if it does not
1785 * already have one. This should only happen on old filesystems.
1787 if (ip->i_gen == 0) {
1788 ip->i_gen = arc4random() / 2 + 1;
1789 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1790 ip->i_flag |= IN_MODIFIED;
1791 DIP_SET(ip, i_gen, ip->i_gen);
1795 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1797 * If this vnode is already allocated, and we're running
1798 * multi-label, attempt to perform a label association
1799 * from the extended attributes on the inode.
1801 error = mac_vnode_associate_extattr(mp, vp);
1803 /* ufs_inactive will release ip->i_devvp ref. */
1816 * File handle to vnode
1818 * Have to be really careful about stale file handles:
1819 * - check that the inode number is valid
1820 * - call ffs_vget() to get the locked inode
1821 * - check for an unallocated inode (i_mode == 0)
1822 * - check that the given client host has export rights and return
1823 * those rights via. exflagsp and credanonp
1826 ffs_fhtovp(mp, fhp, flags, vpp)
1835 ufhp = (struct ufid *)fhp;
1836 fs = VFSTOUFS(mp)->um_fs;
1837 if (ufhp->ufid_ino < ROOTINO ||
1838 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1840 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1844 * Initialize the filesystem.
1848 struct vfsconf *vfsp;
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();
1870 * Write a superblock and associated information back to disk.
1873 ffs_sbupdate(ump, waitfor, suspended)
1874 struct ufsmount *ump;
1878 struct fs *fs = ump->um_fs;
1883 int i, size, error, allerror = 0;
1885 if (fs->fs_ronly == 1 &&
1886 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1887 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1888 panic("ffs_sbupdate: write read-only filesystem");
1890 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1892 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1893 (int)fs->fs_sbsize, 0, 0, 0);
1895 * First write back the summary information.
1897 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1899 for (i = 0; i < blks; i += fs->fs_frag) {
1900 size = fs->fs_bsize;
1901 if (i + fs->fs_frag > blks)
1902 size = (blks - i) * fs->fs_fsize;
1903 bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1905 bcopy(space, bp->b_data, (u_int)size);
1906 space = (char *)space + size;
1908 bp->b_flags |= B_VALIDSUSPWRT;
1909 if (waitfor != MNT_WAIT)
1911 else if ((error = bwrite(bp)) != 0)
1915 * Now write back the superblock itself. If any errors occurred
1916 * up to this point, then fail so that the superblock avoids
1917 * being written out as clean.
1924 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1925 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1926 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1927 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1928 fs->fs_sblockloc = SBLOCK_UFS1;
1930 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1931 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1932 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1933 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1934 fs->fs_sblockloc = SBLOCK_UFS2;
1937 fs->fs_time = time_second;
1938 if (fs->fs_flags & FS_DOSOFTDEP)
1939 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1940 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1941 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1943 bp->b_flags |= B_VALIDSUSPWRT;
1944 if (waitfor != MNT_WAIT)
1946 else if ((error = bwrite(bp)) != 0)
1952 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1953 int attrnamespace, const char *attrname)
1957 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1960 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1966 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1969 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1970 uma_zfree(uma_ufs1, ip->i_din1);
1971 else if (ip->i_din2 != NULL)
1972 uma_zfree(uma_ufs2, ip->i_din2);
1973 uma_zfree(uma_inode, ip);
1976 static int dobkgrdwrite = 1;
1977 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1978 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1981 * Complete a background write started from bwrite.
1984 ffs_backgroundwritedone(struct buf *bp)
1986 struct bufobj *bufobj;
1990 * Find the original buffer that we are writing.
1992 bufobj = bp->b_bufobj;
1994 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1995 panic("backgroundwritedone: lost buffer");
1996 /* Grab an extra reference to be dropped by the bufdone() below. */
1997 bufobj_wrefl(bufobj);
2000 * Process dependencies then return any unfinished ones.
2002 if (!LIST_EMPTY(&bp->b_dep))
2005 if (!LIST_EMPTY(&bp->b_dep))
2006 softdep_move_dependencies(bp, origbp);
2009 * This buffer is marked B_NOCACHE so when it is released
2010 * by biodone it will be tossed.
2012 bp->b_flags |= B_NOCACHE;
2013 bp->b_flags &= ~B_CACHE;
2017 * Clear the BV_BKGRDINPROG flag in the original buffer
2018 * and awaken it if it is waiting for the write to complete.
2019 * If BV_BKGRDINPROG is not set in the original buffer it must
2020 * have been released and re-instantiated - which is not legal.
2022 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2023 ("backgroundwritedone: lost buffer2"));
2024 origbp->b_vflags &= ~BV_BKGRDINPROG;
2025 if (origbp->b_vflags & BV_BKGRDWAIT) {
2026 origbp->b_vflags &= ~BV_BKGRDWAIT;
2027 wakeup(&origbp->b_xflags);
2034 * Write, release buffer on completion. (Done by iodone
2035 * if async). Do not bother writing anything if the buffer
2038 * Note that we set B_CACHE here, indicating that buffer is
2039 * fully valid and thus cacheable. This is true even of NFS
2040 * now so we set it generally. This could be set either here
2041 * or in biodone() since the I/O is synchronous. We put it
2045 ffs_bufwrite(struct buf *bp)
2050 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2051 if (bp->b_flags & B_INVAL) {
2056 oldflags = bp->b_flags;
2058 if (!BUF_ISLOCKED(bp))
2059 panic("bufwrite: buffer is not busy???");
2062 * If a background write is already in progress, delay
2063 * writing this block if it is asynchronous. Otherwise
2064 * wait for the background write to complete.
2066 BO_LOCK(bp->b_bufobj);
2067 if (bp->b_vflags & BV_BKGRDINPROG) {
2068 if (bp->b_flags & B_ASYNC) {
2069 BO_UNLOCK(bp->b_bufobj);
2074 bp->b_vflags |= BV_BKGRDWAIT;
2075 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
2076 if (bp->b_vflags & BV_BKGRDINPROG)
2077 panic("bufwrite: still writing");
2079 BO_UNLOCK(bp->b_bufobj);
2082 * If this buffer is marked for background writing and we
2083 * do not have to wait for it, make a copy and write the
2084 * copy so as to leave this buffer ready for further use.
2086 * This optimization eats a lot of memory. If we have a page
2087 * or buffer shortfall we can't do it.
2089 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2090 (bp->b_flags & B_ASYNC) &&
2091 !vm_page_count_severe() &&
2092 !buf_dirty_count_severe()) {
2093 KASSERT(bp->b_iodone == NULL,
2094 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2096 /* get a new block */
2097 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2102 * set it to be identical to the old block. We have to
2103 * set b_lblkno and BKGRDMARKER before calling bgetvp()
2104 * to avoid confusing the splay tree and gbincore().
2106 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2107 newbp->b_lblkno = bp->b_lblkno;
2108 newbp->b_xflags |= BX_BKGRDMARKER;
2109 BO_LOCK(bp->b_bufobj);
2110 bp->b_vflags |= BV_BKGRDINPROG;
2111 bgetvp(bp->b_vp, newbp);
2112 BO_UNLOCK(bp->b_bufobj);
2113 newbp->b_bufobj = &bp->b_vp->v_bufobj;
2114 newbp->b_blkno = bp->b_blkno;
2115 newbp->b_offset = bp->b_offset;
2116 newbp->b_iodone = ffs_backgroundwritedone;
2117 newbp->b_flags |= B_ASYNC;
2118 newbp->b_flags &= ~B_INVAL;
2122 * Move over the dependencies. If there are rollbacks,
2123 * leave the parent buffer dirtied as it will need to
2126 if (LIST_EMPTY(&bp->b_dep) ||
2127 softdep_move_dependencies(bp, newbp) == 0)
2134 * Initiate write on the copy, release the original to
2135 * the B_LOCKED queue so that it cannot go away until
2136 * the background write completes. If not locked it could go
2137 * away and then be reconstituted while it was being written.
2138 * If the reconstituted buffer were written, we could end up
2139 * with two background copies being written at the same time.
2144 /* Mark the buffer clean */
2148 /* Let the normal bufwrite do the rest for us */
2150 return (bufwrite(bp));
2155 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2162 vp = bo->__bo_vnode;
2163 if (bp->b_iocmd == BIO_WRITE) {
2164 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2165 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2166 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2167 panic("ffs_geom_strategy: bad I/O");
2168 nocopy = bp->b_flags & B_NOCOPY;
2169 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2170 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2171 vp->v_rdev->si_snapdata != NULL) {
2172 if ((bp->b_flags & B_CLUSTER) != 0) {
2173 runningbufwakeup(bp);
2174 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2175 b_cluster.cluster_entry) {
2176 error = ffs_copyonwrite(vp, tbp);
2178 error != EOPNOTSUPP) {
2179 bp->b_error = error;
2180 bp->b_ioflags |= BIO_ERROR;
2185 bp->b_runningbufspace = bp->b_bufsize;
2186 atomic_add_long(&runningbufspace,
2187 bp->b_runningbufspace);
2189 error = ffs_copyonwrite(vp, bp);
2190 if (error != 0 && error != EOPNOTSUPP) {
2191 bp->b_error = error;
2192 bp->b_ioflags |= BIO_ERROR;
2199 if ((bp->b_flags & B_CLUSTER) != 0) {
2200 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2201 b_cluster.cluster_entry) {
2202 if (!LIST_EMPTY(&tbp->b_dep))
2206 if (!LIST_EMPTY(&bp->b_dep))
2212 g_vfs_strategy(bo, bp);
2218 db_print_ffs(struct ufsmount *ump)
2220 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n",
2221 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2222 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2223 ump->softdep_deps, ump->softdep_req);
2226 DB_SHOW_COMMAND(ffs, db_show_ffs)
2229 struct ufsmount *ump;
2232 ump = VFSTOUFS((struct mount *)addr);
2237 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2238 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2239 db_print_ffs(VFSTOUFS(mp));