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/ioccom.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
55 #include <sys/rwlock.h>
57 #include <security/mac/mac_framework.h>
59 #include <ufs/ufs/extattr.h>
60 #include <ufs/ufs/gjournal.h>
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/ufsmount.h>
63 #include <ufs/ufs/inode.h>
64 #include <ufs/ufs/ufs_extern.h>
66 #include <ufs/ffs/fs.h>
67 #include <ufs/ffs/ffs_extern.h>
71 #include <vm/vm_page.h>
73 #include <geom/geom.h>
74 #include <geom/geom_vfs.h>
78 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
80 static int ffs_mountfs(struct vnode *, struct mount *, struct thread *);
81 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
83 static void ffs_ifree(struct ufsmount *ump, struct inode *ip);
84 static int ffs_sync_lazy(struct mount *mp);
86 static vfs_init_t ffs_init;
87 static vfs_uninit_t ffs_uninit;
88 static vfs_extattrctl_t ffs_extattrctl;
89 static vfs_cmount_t ffs_cmount;
90 static vfs_unmount_t ffs_unmount;
91 static vfs_mount_t ffs_mount;
92 static vfs_statfs_t ffs_statfs;
93 static vfs_fhtovp_t ffs_fhtovp;
94 static vfs_sync_t ffs_sync;
96 static struct vfsops ufs_vfsops = {
97 .vfs_extattrctl = ffs_extattrctl,
98 .vfs_fhtovp = ffs_fhtovp,
100 .vfs_mount = ffs_mount,
101 .vfs_cmount = ffs_cmount,
102 .vfs_quotactl = ufs_quotactl,
103 .vfs_root = ufs_root,
104 .vfs_statfs = ffs_statfs,
105 .vfs_sync = ffs_sync,
106 .vfs_uninit = ffs_uninit,
107 .vfs_unmount = ffs_unmount,
108 .vfs_vget = ffs_vget,
109 .vfs_susp_clean = process_deferred_inactive,
112 VFS_SET(ufs_vfsops, ufs, 0);
113 MODULE_VERSION(ufs, 1);
115 static b_strategy_t ffs_geom_strategy;
116 static b_write_t ffs_bufwrite;
118 static struct buf_ops ffs_ops = {
120 .bop_write = ffs_bufwrite,
121 .bop_strategy = ffs_geom_strategy,
123 #ifdef NO_FFS_SNAPSHOT
124 .bop_bdflush = bufbdflush,
126 .bop_bdflush = ffs_bdflush,
131 * Note that userquota and groupquota options are not currently used
132 * by UFS/FFS code and generally mount(8) does not pass those options
133 * from userland, but they can be passed by loader(8) via
134 * vfs.root.mountfrom.options.
136 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
137 "noclusterw", "noexec", "export", "force", "from", "groupquota",
138 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
139 "nosymfollow", "sync", "union", "userquota", NULL };
142 ffs_mount(struct mount *mp)
146 struct ufsmount *ump = NULL;
152 struct nameidata ndp;
156 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
158 if (uma_inode == NULL) {
159 uma_inode = uma_zcreate("FFS inode",
160 sizeof(struct inode), NULL, NULL, NULL, NULL,
162 uma_ufs1 = uma_zcreate("FFS1 dinode",
163 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
165 uma_ufs2 = uma_zcreate("FFS2 dinode",
166 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
170 vfs_deleteopt(mp->mnt_optnew, "groupquota");
171 vfs_deleteopt(mp->mnt_optnew, "userquota");
173 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
178 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
179 mntorflags |= MNT_ACLS;
181 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
182 mntorflags |= MNT_SNAPSHOT;
184 * Once we have set the MNT_SNAPSHOT flag, do not
185 * persist "snapshot" in the options list.
187 vfs_deleteopt(mp->mnt_optnew, "snapshot");
188 vfs_deleteopt(mp->mnt_opt, "snapshot");
191 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
192 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
194 * Once we have set the restricted PID, do not
195 * persist "fsckpid" in the options list.
197 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
198 vfs_deleteopt(mp->mnt_opt, "fsckpid");
199 if (mp->mnt_flag & MNT_UPDATE) {
200 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
201 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
203 "Checker enable: Must be read-only");
206 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
208 "Checker enable: Must be read-only");
211 /* Set to -1 if we are done */
216 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
217 if (mntorflags & MNT_ACLS) {
219 "\"acls\" and \"nfsv4acls\" options "
220 "are mutually exclusive");
223 mntorflags |= MNT_NFS4ACLS;
227 mp->mnt_flag |= mntorflags;
230 * If updating, check whether changing from read-only to
231 * read/write; if there is no device name, that's all we do.
233 if (mp->mnt_flag & MNT_UPDATE) {
236 devvp = ump->um_devvp;
237 if (fsckpid == -1 && ump->um_fsckpid > 0) {
238 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
239 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
244 * Return to normal read-only mode.
246 error = g_access(ump->um_cp, 0, -1, 0);
251 if (fs->fs_ronly == 0 &&
252 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
254 * Flush any dirty data and suspend filesystem.
256 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
258 error = vfs_write_suspend_umnt(mp);
262 * Check for and optionally get rid of files open
266 if (mp->mnt_flag & MNT_FORCE)
268 if (MOUNTEDSOFTDEP(mp)) {
269 error = softdep_flushfiles(mp, flags, td);
271 error = ffs_flushfiles(mp, flags, td);
274 vfs_write_resume(mp, 0);
277 if (fs->fs_pendingblocks != 0 ||
278 fs->fs_pendinginodes != 0) {
279 printf("WARNING: %s Update error: blocks %jd "
280 "files %d\n", fs->fs_fsmnt,
281 (intmax_t)fs->fs_pendingblocks,
282 fs->fs_pendinginodes);
283 fs->fs_pendingblocks = 0;
284 fs->fs_pendinginodes = 0;
286 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
288 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
291 vfs_write_resume(mp, 0);
294 if (MOUNTEDSOFTDEP(mp))
299 * Drop our write and exclusive access.
301 g_access(ump->um_cp, 0, -1, -1);
306 mp->mnt_flag |= MNT_RDONLY;
309 * Allow the writers to note that filesystem
312 vfs_write_resume(mp, 0);
314 if ((mp->mnt_flag & MNT_RELOAD) &&
315 (error = ffs_reload(mp, td, 0)) != 0)
318 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
320 * If we are running a checker, do not allow upgrade.
322 if (ump->um_fsckpid > 0) {
324 "Active checker, cannot upgrade to write");
328 * If upgrade to read-write by non-root, then verify
329 * that user has necessary permissions on the device.
331 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
332 error = VOP_ACCESS(devvp, VREAD | VWRITE,
335 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
337 VOP_UNLOCK(devvp, 0);
340 VOP_UNLOCK(devvp, 0);
341 fs->fs_flags &= ~FS_UNCLEAN;
342 if (fs->fs_clean == 0) {
343 fs->fs_flags |= FS_UNCLEAN;
344 if ((mp->mnt_flag & MNT_FORCE) ||
346 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
347 (fs->fs_flags & FS_DOSOFTDEP))) {
348 printf("WARNING: %s was not properly "
349 "dismounted\n", fs->fs_fsmnt);
352 "R/W mount of %s denied. %s.%s",
354 "Filesystem is not clean - run fsck",
355 (fs->fs_flags & FS_SUJ) == 0 ? "" :
356 " Forced mount will invalidate"
357 " journal contents");
364 * Request exclusive write access.
366 error = g_access(ump->um_cp, 0, 1, 1);
371 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
375 mp->mnt_flag &= ~MNT_RDONLY;
377 fs->fs_mtime = time_second;
378 /* check to see if we need to start softdep */
379 if ((fs->fs_flags & FS_DOSOFTDEP) &&
380 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
381 vn_finished_write(mp);
385 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
386 vn_finished_write(mp);
389 if (fs->fs_snapinum[0] != 0)
390 ffs_snapshot_mount(mp);
391 vn_finished_write(mp);
394 * Soft updates is incompatible with "async",
395 * so if we are doing softupdates stop the user
396 * from setting the async flag in an update.
397 * Softdep_mount() clears it in an initial mount
400 if (MOUNTEDSOFTDEP(mp)) {
401 /* XXX: Reset too late ? */
403 mp->mnt_flag &= ~MNT_ASYNC;
407 * Keep MNT_ACLS flag if it is stored in superblock.
409 if ((fs->fs_flags & FS_ACLS) != 0) {
410 /* XXX: Set too late ? */
412 mp->mnt_flag |= MNT_ACLS;
416 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
417 /* XXX: Set too late ? */
419 mp->mnt_flag |= MNT_NFS4ACLS;
423 * If this is a request from fsck to clean up the filesystem,
424 * then allow the specified pid to proceed.
427 if (ump->um_fsckpid != 0) {
429 "Active checker already running on %s",
433 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
434 ("soft updates enabled on read-only file system"));
438 * Request write access.
440 error = g_access(ump->um_cp, 0, 1, 0);
445 "Checker activation failed on %s",
449 ump->um_fsckpid = fsckpid;
450 if (fs->fs_snapinum[0] != 0)
451 ffs_snapshot_mount(mp);
452 fs->fs_mtime = time_second;
455 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
459 * If this is a snapshot request, take the snapshot.
461 if (mp->mnt_flag & MNT_SNAPSHOT)
462 return (ffs_snapshot(mp, fspec));
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);
470 if ((error = namei(&ndp)) != 0)
472 NDFREE(&ndp, NDF_ONLY_PNBUF);
474 if (!vn_isdisk(devvp, &error)) {
480 * If mount by non-root, then verify that user has necessary
481 * permissions on the device.
484 if ((mp->mnt_flag & MNT_RDONLY) == 0)
486 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
488 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
494 if (mp->mnt_flag & MNT_UPDATE) {
498 * If it's not the same vnode, or at least the same device
499 * then it's not correct.
502 if (devvp->v_rdev != ump->um_devvp->v_rdev)
503 error = EINVAL; /* needs translation */
511 * We need the name for the mount point (also used for
512 * "last mounted on") copied in. If an error occurs,
513 * the mount point is discarded by the upper level code.
514 * Note that vfs_mount() populates f_mntonname for us.
516 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
521 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
522 ("soft updates enabled on read-only file system"));
528 * Request write access.
530 error = g_access(ump->um_cp, 0, 1, 0);
534 printf("WARNING: %s: Checker activation "
535 "failed\n", fs->fs_fsmnt);
537 ump->um_fsckpid = fsckpid;
538 if (fs->fs_snapinum[0] != 0)
539 ffs_snapshot_mount(mp);
540 fs->fs_mtime = time_second;
542 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
546 vfs_mountedfrom(mp, fspec);
551 * Compatibility with old mount system call.
555 ffs_cmount(struct mntarg *ma, void *data, uint64_t flags)
557 struct ufs_args args;
558 struct export_args exp;
563 error = copyin(data, &args, sizeof args);
566 vfs_oexport_conv(&args.export, &exp);
568 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
569 ma = mount_arg(ma, "export", &exp, sizeof(exp));
570 error = kernel_mount(ma, flags);
576 * Reload all incore data for a filesystem (used after running fsck on
577 * the root filesystem and finding things to fix). If the 'force' flag
578 * is 0, the filesystem must be mounted read-only.
580 * Things to do to update the mount:
581 * 1) invalidate all cached meta-data.
582 * 2) re-read superblock from disk.
583 * 3) re-read summary information from disk.
584 * 4) invalidate all inactive vnodes.
585 * 5) invalidate all cached file data.
586 * 6) re-read inode data for all active vnodes.
589 ffs_reload(struct mount *mp, struct thread *td, int force)
591 struct vnode *vp, *mvp, *devvp;
595 struct fs *fs, *newfs;
596 struct ufsmount *ump;
597 ufs2_daddr_t sblockloc;
598 int i, blks, size, error;
604 if ((mp->mnt_flag & MNT_RDONLY) == 0 && force == 0) {
611 * Step 1: invalidate all cached meta-data.
613 devvp = VFSTOUFS(mp)->um_devvp;
614 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
615 if (vinvalbuf(devvp, 0, 0, 0) != 0)
616 panic("ffs_reload: dirty1");
617 VOP_UNLOCK(devvp, 0);
620 * Step 2: re-read superblock from disk.
622 fs = VFSTOUFS(mp)->um_fs;
623 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
626 newfs = (struct fs *)bp->b_data;
627 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
628 newfs->fs_magic != FS_UFS2_MAGIC) ||
629 newfs->fs_bsize > MAXBSIZE ||
630 newfs->fs_bsize < sizeof(struct fs)) {
632 return (EIO); /* XXX needs translation */
635 * Copy pointer fields back into superblock before copying in XXX
636 * new superblock. These should really be in the ufsmount. XXX
637 * Note that important parameters (eg fs_ncg) are unchanged.
639 newfs->fs_csp = fs->fs_csp;
640 newfs->fs_maxcluster = fs->fs_maxcluster;
641 newfs->fs_contigdirs = fs->fs_contigdirs;
642 newfs->fs_active = fs->fs_active;
643 newfs->fs_ronly = fs->fs_ronly;
644 sblockloc = fs->fs_sblockloc;
645 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
647 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
648 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
650 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
651 printf("WARNING: %s: reload pending error: blocks %jd "
652 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
653 fs->fs_pendinginodes);
654 fs->fs_pendingblocks = 0;
655 fs->fs_pendinginodes = 0;
660 * Step 3: re-read summary information from disk.
662 size = fs->fs_cssize;
663 blks = howmany(size, fs->fs_fsize);
664 if (fs->fs_contigsumsize > 0)
665 size += fs->fs_ncg * sizeof(int32_t);
666 size += fs->fs_ncg * sizeof(u_int8_t);
667 free(fs->fs_csp, M_UFSMNT);
668 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
670 for (i = 0; i < blks; i += fs->fs_frag) {
672 if (i + fs->fs_frag > blks)
673 size = (blks - i) * fs->fs_fsize;
674 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
678 bcopy(bp->b_data, space, (u_int)size);
679 space = (char *)space + size;
683 * We no longer know anything about clusters per cylinder group.
685 if (fs->fs_contigsumsize > 0) {
686 fs->fs_maxcluster = lp = space;
687 for (i = 0; i < fs->fs_ncg; i++)
688 *lp++ = fs->fs_contigsumsize;
691 size = fs->fs_ncg * sizeof(u_int8_t);
692 fs->fs_contigdirs = (u_int8_t *)space;
693 bzero(fs->fs_contigdirs, size);
696 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
700 if (vp->v_type == VNON) {
705 * Step 4: invalidate all cached file data.
707 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
708 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
711 if (vinvalbuf(vp, 0, 0, 0))
712 panic("ffs_reload: dirty2");
714 * Step 5: re-read inode data for all active vnodes.
718 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
719 (int)fs->fs_bsize, NOCRED, &bp);
723 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
726 ffs_load_inode(bp, ip, fs, ip->i_number);
727 ip->i_effnlink = ip->i_nlink;
736 * Possible superblock locations ordered from most to least likely.
738 static int sblock_try[] = SBLOCKSEARCH;
741 * Common code for mount and mountroot
744 ffs_mountfs(devvp, mp, td)
749 struct ufsmount *ump;
754 ufs2_daddr_t sblockloc;
755 int error, i, blks, size, ronly;
758 struct g_consumer *cp;
763 cred = td ? td->td_ucred : NOCRED;
764 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
770 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
773 VOP_UNLOCK(devvp, 0);
776 if (devvp->v_rdev->si_iosize_max != 0)
777 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
778 if (mp->mnt_iosize_max > MAXPHYS)
779 mp->mnt_iosize_max = MAXPHYS;
781 devvp->v_bufobj.bo_ops = &ffs_ops;
786 * Try reading the superblock in each of its possible locations.
788 for (i = 0; sblock_try[i] != -1; i++) {
789 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
792 "Invalid sectorsize %d for superblock size %d",
793 cp->provider->sectorsize, SBLOCKSIZE);
796 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
799 fs = (struct fs *)bp->b_data;
800 sblockloc = sblock_try[i];
801 if ((fs->fs_magic == FS_UFS1_MAGIC ||
802 (fs->fs_magic == FS_UFS2_MAGIC &&
803 (fs->fs_sblockloc == sblockloc ||
804 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
805 fs->fs_bsize <= MAXBSIZE &&
806 fs->fs_bsize >= sizeof(struct fs))
811 if (sblock_try[i] == -1) {
812 error = EINVAL; /* XXX needs translation */
816 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
817 fs->fs_flags &= ~FS_UNCLEAN;
818 if (fs->fs_clean == 0) {
819 fs->fs_flags |= FS_UNCLEAN;
820 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
821 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
822 (fs->fs_flags & FS_DOSOFTDEP))) {
823 printf("WARNING: %s was not properly dismounted\n",
826 vfs_mount_error(mp, "R/W mount of %s denied. %s%s",
827 fs->fs_fsmnt, "Filesystem is not clean - run fsck.",
828 (fs->fs_flags & FS_SUJ) == 0 ? "" :
829 " Forced mount will invalidate journal contents");
833 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
834 (mp->mnt_flag & MNT_FORCE)) {
835 printf("WARNING: %s: lost blocks %jd files %d\n",
836 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
837 fs->fs_pendinginodes);
838 fs->fs_pendingblocks = 0;
839 fs->fs_pendinginodes = 0;
842 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
843 printf("WARNING: %s: mount pending error: blocks %jd "
844 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
845 fs->fs_pendinginodes);
846 fs->fs_pendingblocks = 0;
847 fs->fs_pendinginodes = 0;
849 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
852 * Get journal provider name.
855 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
856 if (g_io_getattr("GJOURNAL::provider", cp, &size,
857 mp->mnt_gjprovider) == 0) {
858 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
861 mp->mnt_flag |= MNT_GJOURNAL;
864 printf("WARNING: %s: GJOURNAL flag on fs "
865 "but no gjournal provider below\n",
866 mp->mnt_stat.f_mntonname);
867 free(mp->mnt_gjprovider, M_UFSMNT);
868 mp->mnt_gjprovider = NULL;
871 printf("WARNING: %s: GJOURNAL flag on fs but no "
872 "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
875 mp->mnt_gjprovider = NULL;
877 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
879 ump->um_bo = &devvp->v_bufobj;
880 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
881 if (fs->fs_magic == FS_UFS1_MAGIC) {
882 ump->um_fstype = UFS1;
883 ump->um_balloc = ffs_balloc_ufs1;
885 ump->um_fstype = UFS2;
886 ump->um_balloc = ffs_balloc_ufs2;
888 ump->um_blkatoff = ffs_blkatoff;
889 ump->um_truncate = ffs_truncate;
890 ump->um_update = ffs_update;
891 ump->um_valloc = ffs_valloc;
892 ump->um_vfree = ffs_vfree;
893 ump->um_ifree = ffs_ifree;
894 ump->um_rdonly = ffs_rdonly;
895 ump->um_snapgone = ffs_snapgone;
896 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
897 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
898 if (fs->fs_sbsize < SBLOCKSIZE)
899 bp->b_flags |= B_INVAL | B_NOCACHE;
903 ffs_oldfscompat_read(fs, ump, sblockloc);
904 fs->fs_ronly = ronly;
905 size = fs->fs_cssize;
906 blks = howmany(size, fs->fs_fsize);
907 if (fs->fs_contigsumsize > 0)
908 size += fs->fs_ncg * sizeof(int32_t);
909 size += fs->fs_ncg * sizeof(u_int8_t);
910 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
912 for (i = 0; i < blks; i += fs->fs_frag) {
914 if (i + fs->fs_frag > blks)
915 size = (blks - i) * fs->fs_fsize;
916 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
918 free(fs->fs_csp, M_UFSMNT);
921 bcopy(bp->b_data, space, (u_int)size);
922 space = (char *)space + size;
926 if (fs->fs_contigsumsize > 0) {
927 fs->fs_maxcluster = lp = space;
928 for (i = 0; i < fs->fs_ncg; i++)
929 *lp++ = fs->fs_contigsumsize;
932 size = fs->fs_ncg * sizeof(u_int8_t);
933 fs->fs_contigdirs = (u_int8_t *)space;
934 bzero(fs->fs_contigdirs, size);
935 fs->fs_active = NULL;
937 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
938 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
940 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
941 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
946 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
948 mp->mnt_flag |= MNT_LOCAL;
950 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
953 mp->mnt_flag |= MNT_MULTILABEL;
956 printf("WARNING: %s: multilabel flag on fs but "
957 "no MAC support\n", mp->mnt_stat.f_mntonname);
960 if ((fs->fs_flags & FS_ACLS) != 0) {
964 if (mp->mnt_flag & MNT_NFS4ACLS)
965 printf("WARNING: %s: ACLs flag on fs conflicts with "
966 "\"nfsv4acls\" mount option; option ignored\n",
967 mp->mnt_stat.f_mntonname);
968 mp->mnt_flag &= ~MNT_NFS4ACLS;
969 mp->mnt_flag |= MNT_ACLS;
973 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
974 mp->mnt_stat.f_mntonname);
977 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
981 if (mp->mnt_flag & MNT_ACLS)
982 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
983 "with \"acls\" mount option; option ignored\n",
984 mp->mnt_stat.f_mntonname);
985 mp->mnt_flag &= ~MNT_ACLS;
986 mp->mnt_flag |= MNT_NFS4ACLS;
990 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
991 "ACLs support\n", mp->mnt_stat.f_mntonname);
994 if ((fs->fs_flags & FS_TRIM) != 0) {
996 if (g_io_getattr("GEOM::candelete", cp, &size,
997 &ump->um_candelete) == 0) {
998 if (!ump->um_candelete)
999 printf("WARNING: %s: TRIM flag on fs but disk "
1000 "does not support TRIM\n",
1001 mp->mnt_stat.f_mntonname);
1003 printf("WARNING: %s: TRIM flag on fs but disk does "
1004 "not confirm that it supports TRIM\n",
1005 mp->mnt_stat.f_mntonname);
1006 ump->um_candelete = 0;
1010 ump->um_mountp = mp;
1012 ump->um_devvp = devvp;
1013 ump->um_nindir = fs->fs_nindir;
1014 ump->um_bptrtodb = fs->fs_fsbtodb;
1015 ump->um_seqinc = fs->fs_frag;
1016 for (i = 0; i < MAXQUOTAS; i++)
1017 ump->um_quotas[i] = NULLVP;
1019 ufs_extattr_uepm_init(&ump->um_extattr);
1022 * Set FS local "last mounted on" information (NULL pad)
1024 bzero(fs->fs_fsmnt, MAXMNTLEN);
1025 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1026 mp->mnt_stat.f_iosize = fs->fs_bsize;
1028 if (mp->mnt_flag & MNT_ROOTFS) {
1030 * Root mount; update timestamp in mount structure.
1031 * this will be used by the common root mount code
1032 * to update the system clock.
1034 mp->mnt_time = fs->fs_time;
1038 fs->fs_mtime = time_second;
1039 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1040 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1041 free(fs->fs_csp, M_UFSMNT);
1042 ffs_flushfiles(mp, FORCECLOSE, td);
1045 if (devvp->v_type == VCHR && devvp->v_rdev != NULL)
1046 devvp->v_rdev->si_mountpt = mp;
1047 if (fs->fs_snapinum[0] != 0)
1048 ffs_snapshot_mount(mp);
1051 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
1054 * Initialize filesystem stat information in mount struct.
1057 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
1058 MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS | MNTK_USES_BCACHE;
1061 #ifdef UFS_EXTATTR_AUTOSTART
1064 * Auto-starting does the following:
1065 * - check for /.attribute in the fs, and extattr_start if so
1066 * - for each file in .attribute, enable that file with
1067 * an attribute of the same name.
1068 * Not clear how to report errors -- probably eat them.
1069 * This would all happen while the filesystem was busy/not
1070 * available, so would effectively be "atomic".
1072 (void) ufs_extattr_autostart(mp, td);
1073 #endif /* !UFS_EXTATTR_AUTOSTART */
1074 #endif /* !UFS_EXTATTR */
1083 g_topology_unlock();
1087 mtx_destroy(UFS_MTX(ump));
1088 if (mp->mnt_gjprovider != NULL) {
1089 free(mp->mnt_gjprovider, M_UFSMNT);
1090 mp->mnt_gjprovider = NULL;
1092 free(ump->um_fs, M_UFSMNT);
1093 free(ump, M_UFSMNT);
1094 mp->mnt_data = NULL;
1100 #include <sys/sysctl.h>
1101 static int bigcgs = 0;
1102 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1105 * Sanity checks for loading old filesystem superblocks.
1106 * See ffs_oldfscompat_write below for unwound actions.
1108 * XXX - Parts get retired eventually.
1109 * Unfortunately new bits get added.
1112 ffs_oldfscompat_read(fs, ump, sblockloc)
1114 struct ufsmount *ump;
1115 ufs2_daddr_t sblockloc;
1120 * If not yet done, update fs_flags location and value of fs_sblockloc.
1122 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1123 fs->fs_flags = fs->fs_old_flags;
1124 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1125 fs->fs_sblockloc = sblockloc;
1128 * If not yet done, update UFS1 superblock with new wider fields.
1130 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1131 fs->fs_maxbsize = fs->fs_bsize;
1132 fs->fs_time = fs->fs_old_time;
1133 fs->fs_size = fs->fs_old_size;
1134 fs->fs_dsize = fs->fs_old_dsize;
1135 fs->fs_csaddr = fs->fs_old_csaddr;
1136 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1137 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1138 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1139 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1141 if (fs->fs_magic == FS_UFS1_MAGIC &&
1142 fs->fs_old_inodefmt < FS_44INODEFMT) {
1143 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1144 fs->fs_qbmask = ~fs->fs_bmask;
1145 fs->fs_qfmask = ~fs->fs_fmask;
1147 if (fs->fs_magic == FS_UFS1_MAGIC) {
1148 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1149 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1150 if (fs->fs_maxfilesize > maxfilesize)
1151 fs->fs_maxfilesize = maxfilesize;
1153 /* Compatibility for old filesystems */
1154 if (fs->fs_avgfilesize <= 0)
1155 fs->fs_avgfilesize = AVFILESIZ;
1156 if (fs->fs_avgfpdir <= 0)
1157 fs->fs_avgfpdir = AFPDIR;
1159 fs->fs_save_cgsize = fs->fs_cgsize;
1160 fs->fs_cgsize = fs->fs_bsize;
1165 * Unwinding superblock updates for old filesystems.
1166 * See ffs_oldfscompat_read above for details.
1168 * XXX - Parts get retired eventually.
1169 * Unfortunately new bits get added.
1172 ffs_oldfscompat_write(fs, ump)
1174 struct ufsmount *ump;
1178 * Copy back UFS2 updated fields that UFS1 inspects.
1180 if (fs->fs_magic == FS_UFS1_MAGIC) {
1181 fs->fs_old_time = fs->fs_time;
1182 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1183 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1184 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1185 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1186 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1189 fs->fs_cgsize = fs->fs_save_cgsize;
1190 fs->fs_save_cgsize = 0;
1195 * unmount system call
1198 ffs_unmount(mp, mntflags)
1203 struct ufsmount *ump = VFSTOUFS(mp);
1205 int error, flags, susp;
1214 if (mntflags & MNT_FORCE) {
1215 flags |= FORCECLOSE;
1216 susp = fs->fs_ronly == 0;
1219 if ((error = ufs_extattr_stop(mp, td))) {
1220 if (error != EOPNOTSUPP)
1221 printf("WARNING: unmount %s: ufs_extattr_stop "
1222 "returned errno %d\n", mp->mnt_stat.f_mntonname,
1226 ufs_extattr_uepm_destroy(&ump->um_extattr);
1231 error = vfs_write_suspend_umnt(mp);
1235 if (MOUNTEDSOFTDEP(mp))
1236 error = softdep_flushfiles(mp, flags, td);
1238 error = ffs_flushfiles(mp, flags, td);
1239 if (error != 0 && error != ENXIO)
1243 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1244 printf("WARNING: unmount %s: pending error: blocks %jd "
1245 "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1246 fs->fs_pendinginodes);
1247 fs->fs_pendingblocks = 0;
1248 fs->fs_pendinginodes = 0;
1251 if (MOUNTEDSOFTDEP(mp))
1252 softdep_unmount(mp);
1253 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1254 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1255 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1256 if (error && error != ENXIO) {
1262 vfs_write_resume(mp, VR_START_WRITE);
1265 if (ump->um_fsckpid > 0) {
1267 * Return to normal read-only mode.
1269 error = g_access(ump->um_cp, 0, -1, 0);
1270 ump->um_fsckpid = 0;
1272 g_vfs_close(ump->um_cp);
1273 g_topology_unlock();
1275 if (ump->um_devvp->v_type == VCHR && ump->um_devvp->v_rdev != NULL)
1276 ump->um_devvp->v_rdev->si_mountpt = NULL;
1277 vrele(ump->um_devvp);
1278 dev_rel(ump->um_dev);
1279 mtx_destroy(UFS_MTX(ump));
1280 if (mp->mnt_gjprovider != NULL) {
1281 free(mp->mnt_gjprovider, M_UFSMNT);
1282 mp->mnt_gjprovider = NULL;
1284 free(fs->fs_csp, M_UFSMNT);
1286 free(ump, M_UFSMNT);
1287 mp->mnt_data = NULL;
1289 mp->mnt_flag &= ~MNT_LOCAL;
1295 vfs_write_resume(mp, VR_START_WRITE);
1299 ufs_extattr_uepm_init(&ump->um_extattr);
1300 #ifdef UFS_EXTATTR_AUTOSTART
1301 (void) ufs_extattr_autostart(mp, td);
1310 * Flush out all the files in a filesystem.
1313 ffs_flushfiles(mp, flags, td)
1318 struct ufsmount *ump;
1324 if (mp->mnt_flag & MNT_QUOTA) {
1326 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1329 for (i = 0; i < MAXQUOTAS; i++) {
1330 error = quotaoff(td, mp, i);
1332 if ((flags & EARLYFLUSH) == 0)
1340 * Here we fall through to vflush again to ensure that
1341 * we have gotten rid of all the system vnodes, unless
1342 * quotas must not be closed.
1346 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1347 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1348 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1350 ffs_snapshot_unmount(mp);
1351 flags |= FORCECLOSE;
1353 * Here we fall through to vflush again to ensure
1354 * that we have gotten rid of all the system vnodes.
1359 * Do not close system files if quotas were not closed, to be
1360 * able to sync the remaining dquots. The freeblks softupdate
1361 * workitems might hold a reference on a dquot, preventing
1362 * quotaoff() from completing. Next round of
1363 * softdep_flushworklist() iteration should process the
1364 * blockers, allowing the next run of quotaoff() to finally
1365 * flush held dquots.
1367 * Otherwise, flush all the files.
1369 if (qerror == 0 && (error = vflush(mp, 0, flags, td)) != 0)
1373 * Flush filesystem metadata.
1375 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1376 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1377 VOP_UNLOCK(ump->um_devvp, 0);
1382 * Get filesystem statistics.
1389 struct ufsmount *ump;
1394 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1395 panic("ffs_statfs");
1396 sbp->f_version = STATFS_VERSION;
1397 sbp->f_bsize = fs->fs_fsize;
1398 sbp->f_iosize = fs->fs_bsize;
1399 sbp->f_blocks = fs->fs_dsize;
1401 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1402 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1403 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1404 dbtofsb(fs, fs->fs_pendingblocks);
1405 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1406 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1408 sbp->f_namemax = NAME_MAX;
1413 sync_doupdate(struct inode *ip)
1416 return ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1421 * For a lazy sync, we only care about access times, quotas and the
1422 * superblock. Other filesystem changes are already converted to
1423 * cylinder group blocks or inode blocks updates and are written to
1430 struct vnode *mvp, *vp;
1433 int allerror, error;
1437 if ((mp->mnt_flag & MNT_NOATIME) != 0)
1439 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1440 if (vp->v_type == VNON) {
1447 * The IN_ACCESS flag is converted to IN_MODIFIED by
1448 * ufs_close() and ufs_getattr() by the calls to
1449 * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1450 * Test also all the other timestamp flags too, to pick up
1451 * any other cases that could be missed.
1453 if (!sync_doupdate(ip) && (vp->v_iflag & VI_OWEINACT) == 0) {
1457 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1460 if (sync_doupdate(ip))
1461 error = ffs_update(vp, 0);
1472 if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1473 (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1479 * Go through the disk queues to initiate sandbagged IO;
1480 * go through the inodes to write those that have been modified;
1481 * initiate the writing of the super block if it has been modified.
1483 * Note: we are always called with the filesystem marked busy using
1487 ffs_sync(mp, waitfor)
1491 struct vnode *mvp, *vp, *devvp;
1494 struct ufsmount *ump = VFSTOUFS(mp);
1496 int error, count, lockreq, allerror = 0;
1499 int secondary_writes;
1500 int secondary_accwrites;
1502 int softdep_accdeps;
1509 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0)
1510 panic("%s: ffs_sync: modification on read-only filesystem",
1512 if (waitfor == MNT_LAZY) {
1514 return (ffs_sync_lazy(mp));
1515 waitfor = MNT_NOWAIT;
1519 * Write back each (modified) inode.
1521 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1522 if (waitfor == MNT_SUSPEND) {
1526 if (waitfor == MNT_WAIT)
1527 lockreq = LK_EXCLUSIVE;
1528 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1530 /* Grab snapshot of secondary write counts */
1532 secondary_writes = mp->mnt_secondary_writes;
1533 secondary_accwrites = mp->mnt_secondary_accwrites;
1536 /* Grab snapshot of softdep dependency counts */
1537 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1539 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1541 * Depend on the vnode interlock to keep things stable enough
1542 * for a quick test. Since there might be hundreds of
1543 * thousands of vnodes, we cannot afford even a subroutine
1544 * call unless there's a good chance that we have work to do.
1546 if (vp->v_type == VNON) {
1552 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1553 vp->v_bufobj.bo_dirty.bv_cnt == 0) {
1557 if ((error = vget(vp, lockreq, td)) != 0) {
1558 if (error == ENOENT || error == ENOLCK) {
1559 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1564 if ((error = ffs_syncvnode(vp, waitfor, 0)) != 0)
1569 * Force stale filesystem control information to be flushed.
1571 if (waitfor == MNT_WAIT || rebooting) {
1572 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1574 /* Flushed work items may create new vnodes to clean */
1575 if (allerror == 0 && count)
1582 devvp = ump->um_devvp;
1583 bo = &devvp->v_bufobj;
1585 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1587 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1588 error = VOP_FSYNC(devvp, waitfor, td);
1589 VOP_UNLOCK(devvp, 0);
1590 if (MOUNTEDSOFTDEP(mp) && (error == 0 || error == EAGAIN))
1591 error = ffs_sbupdate(ump, waitfor, 0);
1594 if (allerror == 0 && waitfor == MNT_WAIT)
1596 } else if (suspend != 0) {
1597 if (softdep_check_suspend(mp,
1602 secondary_accwrites) != 0) {
1604 goto loop; /* More work needed */
1606 mtx_assert(MNT_MTX(mp), MA_OWNED);
1607 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1613 * Write back modified superblock.
1615 if (fs->fs_fmod != 0 &&
1616 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1622 ffs_vget(mp, ino, flags, vpp)
1628 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1632 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1641 struct ufsmount *ump;
1647 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1648 if (error || *vpp != NULL)
1652 * We must promote to an exclusive lock for vnode creation. This
1653 * can happen if lookup is passed LOCKSHARED.
1655 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1656 flags &= ~LK_TYPE_MASK;
1657 flags |= LK_EXCLUSIVE;
1661 * We do not lock vnode creation as it is believed to be too
1662 * expensive for such rare case as simultaneous creation of vnode
1663 * for same ino by different processes. We just allow them to race
1664 * and check later to decide who wins. Let the race begin!
1670 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1672 /* Allocate a new vnode/inode. */
1673 error = getnewvnode("ufs", mp, fs->fs_magic == FS_UFS1_MAGIC ?
1674 &ffs_vnodeops1 : &ffs_vnodeops2, &vp);
1677 uma_zfree(uma_inode, ip);
1681 * FFS supports recursive locking.
1683 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1686 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1693 ip->i_nextclustercg = -1;
1697 for (i = 0; i < MAXQUOTAS; i++)
1698 ip->i_dquot[i] = NODQUOT;
1702 if (ffs_flags & FFSV_FORCEINSMQ)
1703 vp->v_vflag |= VV_FORCEINSMQ;
1704 error = insmntque(vp, mp);
1706 uma_zfree(uma_inode, ip);
1710 vp->v_vflag &= ~VV_FORCEINSMQ;
1711 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1712 if (error || *vpp != NULL)
1715 /* Read in the disk contents for the inode, copy into the inode. */
1716 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1717 (int)fs->fs_bsize, NOCRED, &bp);
1720 * The inode does not contain anything useful, so it would
1721 * be misleading to leave it on its hash chain. With mode
1722 * still zero, it will be unlinked and returned to the free
1730 if (ip->i_ump->um_fstype == UFS1)
1731 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1733 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1734 ffs_load_inode(bp, ip, fs, ino);
1735 if (DOINGSOFTDEP(vp))
1736 softdep_load_inodeblock(ip);
1738 ip->i_effnlink = ip->i_nlink;
1742 * Initialize the vnode from the inode, check for aliases.
1743 * Note that the underlying vnode may have changed.
1745 if (ip->i_ump->um_fstype == UFS1)
1746 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1748 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1756 * Finish inode initialization.
1758 if (vp->v_type != VFIFO) {
1759 /* FFS supports shared locking for all files except fifos. */
1764 * Set up a generation number for this inode if it does not
1765 * already have one. This should only happen on old filesystems.
1767 if (ip->i_gen == 0) {
1768 ip->i_gen = arc4random() / 2 + 1;
1769 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1770 ip->i_flag |= IN_MODIFIED;
1771 DIP_SET(ip, i_gen, ip->i_gen);
1775 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1777 * If this vnode is already allocated, and we're running
1778 * multi-label, attempt to perform a label association
1779 * from the extended attributes on the inode.
1781 error = mac_vnode_associate_extattr(mp, vp);
1783 /* ufs_inactive will release ip->i_devvp ref. */
1796 * File handle to vnode
1798 * Have to be really careful about stale file handles:
1799 * - check that the inode number is valid
1800 * - call ffs_vget() to get the locked inode
1801 * - check for an unallocated inode (i_mode == 0)
1802 * - check that the given client host has export rights and return
1803 * those rights via. exflagsp and credanonp
1806 ffs_fhtovp(mp, fhp, flags, vpp)
1815 ufhp = (struct ufid *)fhp;
1816 fs = VFSTOUFS(mp)->um_fs;
1817 if (ufhp->ufid_ino < ROOTINO ||
1818 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1820 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1824 * Initialize the filesystem.
1828 struct vfsconf *vfsp;
1831 ffs_susp_initialize();
1832 softdep_initialize();
1833 return (ufs_init(vfsp));
1837 * Undo the work of ffs_init().
1841 struct vfsconf *vfsp;
1845 ret = ufs_uninit(vfsp);
1846 softdep_uninitialize();
1847 ffs_susp_uninitialize();
1852 * Write a superblock and associated information back to disk.
1855 ffs_sbupdate(ump, waitfor, suspended)
1856 struct ufsmount *ump;
1860 struct fs *fs = ump->um_fs;
1865 int i, size, error, allerror = 0;
1867 if (fs->fs_ronly == 1 &&
1868 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1869 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1870 panic("ffs_sbupdate: write read-only filesystem");
1872 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1874 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1875 (int)fs->fs_sbsize, 0, 0, 0);
1877 * First write back the summary information.
1879 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1881 for (i = 0; i < blks; i += fs->fs_frag) {
1882 size = fs->fs_bsize;
1883 if (i + fs->fs_frag > blks)
1884 size = (blks - i) * fs->fs_fsize;
1885 bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1887 bcopy(space, bp->b_data, (u_int)size);
1888 space = (char *)space + size;
1890 bp->b_flags |= B_VALIDSUSPWRT;
1891 if (waitfor != MNT_WAIT)
1893 else if ((error = bwrite(bp)) != 0)
1897 * Now write back the superblock itself. If any errors occurred
1898 * up to this point, then fail so that the superblock avoids
1899 * being written out as clean.
1906 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1907 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1908 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1909 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1910 fs->fs_sblockloc = SBLOCK_UFS1;
1912 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1913 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1914 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1915 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1916 fs->fs_sblockloc = SBLOCK_UFS2;
1919 fs->fs_time = time_second;
1920 if (MOUNTEDSOFTDEP(ump->um_mountp))
1921 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1922 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1923 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1925 bp->b_flags |= B_VALIDSUSPWRT;
1926 if (waitfor != MNT_WAIT)
1928 else if ((error = bwrite(bp)) != 0)
1934 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1935 int attrnamespace, const char *attrname)
1939 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1942 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1948 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1951 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1952 uma_zfree(uma_ufs1, ip->i_din1);
1953 else if (ip->i_din2 != NULL)
1954 uma_zfree(uma_ufs2, ip->i_din2);
1955 uma_zfree(uma_inode, ip);
1958 static int dobkgrdwrite = 1;
1959 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1960 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1963 * Complete a background write started from bwrite.
1966 ffs_backgroundwritedone(struct buf *bp)
1968 struct bufobj *bufobj;
1972 * Find the original buffer that we are writing.
1974 bufobj = bp->b_bufobj;
1976 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1977 panic("backgroundwritedone: lost buffer");
1980 * We should mark the cylinder group buffer origbp as
1981 * dirty, to not loose the failed write.
1983 if ((bp->b_ioflags & BIO_ERROR) != 0)
1984 origbp->b_vflags |= BV_BKGRDERR;
1987 * Process dependencies then return any unfinished ones.
1990 if (!LIST_EMPTY(&bp->b_dep) && (bp->b_ioflags & BIO_ERROR) == 0)
1993 if (!LIST_EMPTY(&bp->b_dep))
1994 softdep_move_dependencies(bp, origbp);
1997 * This buffer is marked B_NOCACHE so when it is released
1998 * by biodone it will be tossed.
2000 bp->b_flags |= B_NOCACHE;
2001 bp->b_flags &= ~B_CACHE;
2004 * Prevent brelse() from trying to keep and re-dirtying bp on
2005 * errors. It causes b_bufobj dereference in
2006 * bdirty()/reassignbuf(), and b_bufobj was cleared in
2009 if ((bp->b_ioflags & BIO_ERROR) != 0)
2010 bp->b_flags |= B_INVAL;
2014 * Clear the BV_BKGRDINPROG flag in the original buffer
2015 * and awaken it if it is waiting for the write to complete.
2016 * If BV_BKGRDINPROG is not set in the original buffer it must
2017 * have been released and re-instantiated - which is not legal.
2019 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
2020 ("backgroundwritedone: lost buffer2"));
2021 origbp->b_vflags &= ~BV_BKGRDINPROG;
2022 if (origbp->b_vflags & BV_BKGRDWAIT) {
2023 origbp->b_vflags &= ~BV_BKGRDWAIT;
2024 wakeup(&origbp->b_xflags);
2031 * Write, release buffer on completion. (Done by iodone
2032 * if async). Do not bother writing anything if the buffer
2035 * Note that we set B_CACHE here, indicating that buffer is
2036 * fully valid and thus cacheable. This is true even of NFS
2037 * now so we set it generally. This could be set either here
2038 * or in biodone() since the I/O is synchronous. We put it
2042 ffs_bufwrite(struct buf *bp)
2046 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2047 if (bp->b_flags & B_INVAL) {
2052 if (!BUF_ISLOCKED(bp))
2053 panic("bufwrite: buffer is not busy???");
2055 * If a background write is already in progress, delay
2056 * writing this block if it is asynchronous. Otherwise
2057 * wait for the background write to complete.
2059 BO_LOCK(bp->b_bufobj);
2060 if (bp->b_vflags & BV_BKGRDINPROG) {
2061 if (bp->b_flags & B_ASYNC) {
2062 BO_UNLOCK(bp->b_bufobj);
2066 bp->b_vflags |= BV_BKGRDWAIT;
2067 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), PRIBIO,
2069 if (bp->b_vflags & BV_BKGRDINPROG)
2070 panic("bufwrite: still writing");
2072 bp->b_vflags &= ~BV_BKGRDERR;
2073 BO_UNLOCK(bp->b_bufobj);
2076 * If this buffer is marked for background writing and we
2077 * do not have to wait for it, make a copy and write the
2078 * copy so as to leave this buffer ready for further use.
2080 * This optimization eats a lot of memory. If we have a page
2081 * or buffer shortfall we can't do it.
2083 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2084 (bp->b_flags & B_ASYNC) &&
2085 !vm_page_count_severe() &&
2086 !buf_dirty_count_severe()) {
2087 KASSERT(bp->b_iodone == NULL,
2088 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2090 /* get a new block */
2091 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2095 KASSERT((bp->b_flags & B_UNMAPPED) == 0, ("Unmapped cg"));
2096 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2097 BO_LOCK(bp->b_bufobj);
2098 bp->b_vflags |= BV_BKGRDINPROG;
2099 BO_UNLOCK(bp->b_bufobj);
2100 newbp->b_xflags |= BX_BKGRDMARKER;
2101 newbp->b_lblkno = bp->b_lblkno;
2102 newbp->b_blkno = bp->b_blkno;
2103 newbp->b_offset = bp->b_offset;
2104 newbp->b_iodone = ffs_backgroundwritedone;
2105 newbp->b_flags |= B_ASYNC;
2106 newbp->b_flags &= ~B_INVAL;
2107 pbgetvp(bp->b_vp, newbp);
2111 * Move over the dependencies. If there are rollbacks,
2112 * leave the parent buffer dirtied as it will need to
2115 if (LIST_EMPTY(&bp->b_dep) ||
2116 softdep_move_dependencies(bp, newbp) == 0)
2123 * Initiate write on the copy, release the original. The
2124 * BKGRDINPROG flag prevents it from going away until
2125 * the background write completes.
2130 /* Mark the buffer clean */
2134 /* Let the normal bufwrite do the rest for us */
2136 return (bufwrite(bp));
2141 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2148 vp = bo->__bo_vnode;
2149 if (bp->b_iocmd == BIO_WRITE) {
2150 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2151 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2152 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2153 panic("ffs_geom_strategy: bad I/O");
2154 nocopy = bp->b_flags & B_NOCOPY;
2155 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2156 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2157 vp->v_rdev->si_snapdata != NULL) {
2158 if ((bp->b_flags & B_CLUSTER) != 0) {
2159 runningbufwakeup(bp);
2160 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2161 b_cluster.cluster_entry) {
2162 error = ffs_copyonwrite(vp, tbp);
2164 error != EOPNOTSUPP) {
2165 bp->b_error = error;
2166 bp->b_ioflags |= BIO_ERROR;
2171 bp->b_runningbufspace = bp->b_bufsize;
2172 atomic_add_long(&runningbufspace,
2173 bp->b_runningbufspace);
2175 error = ffs_copyonwrite(vp, bp);
2176 if (error != 0 && error != EOPNOTSUPP) {
2177 bp->b_error = error;
2178 bp->b_ioflags |= BIO_ERROR;
2185 if ((bp->b_flags & B_CLUSTER) != 0) {
2186 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2187 b_cluster.cluster_entry) {
2188 if (!LIST_EMPTY(&tbp->b_dep))
2192 if (!LIST_EMPTY(&bp->b_dep))
2198 g_vfs_strategy(bo, bp);
2202 ffs_own_mount(const struct mount *mp)
2205 if (mp->mnt_op == &ufs_vfsops)
2213 /* defined in ffs_softdep.c */
2214 extern void db_print_ffs(struct ufsmount *ump);
2216 DB_SHOW_COMMAND(ffs, db_show_ffs)
2219 struct ufsmount *ump;
2222 ump = VFSTOUFS((struct mount *)addr);
2227 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2228 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2229 db_print_ffs(VFSTOUFS(mp));
2233 #endif /* SOFTUPDATES */