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 vfs_init_t ffs_init;
84 static vfs_uninit_t ffs_uninit;
85 static vfs_extattrctl_t ffs_extattrctl;
86 static vfs_cmount_t ffs_cmount;
87 static vfs_unmount_t ffs_unmount;
88 static vfs_mount_t ffs_mount;
89 static vfs_statfs_t ffs_statfs;
90 static vfs_fhtovp_t ffs_fhtovp;
91 static vfs_sync_t ffs_sync;
93 static struct vfsops ufs_vfsops = {
94 .vfs_extattrctl = ffs_extattrctl,
95 .vfs_fhtovp = ffs_fhtovp,
97 .vfs_mount = ffs_mount,
98 .vfs_cmount = ffs_cmount,
99 .vfs_quotactl = ufs_quotactl,
100 .vfs_root = ufs_root,
101 .vfs_statfs = ffs_statfs,
102 .vfs_sync = ffs_sync,
103 .vfs_uninit = ffs_uninit,
104 .vfs_unmount = ffs_unmount,
105 .vfs_vget = ffs_vget,
106 .vfs_susp_clean = process_deferred_inactive,
109 VFS_SET(ufs_vfsops, ufs, 0);
110 MODULE_VERSION(ufs, 1);
112 static b_strategy_t ffs_geom_strategy;
113 static b_write_t ffs_bufwrite;
115 static struct buf_ops ffs_ops = {
117 .bop_write = ffs_bufwrite,
118 .bop_strategy = ffs_geom_strategy,
120 #ifdef NO_FFS_SNAPSHOT
121 .bop_bdflush = bufbdflush,
123 .bop_bdflush = ffs_bdflush,
128 * Note that userquota and groupquota options are not currently used
129 * by UFS/FFS code and generally mount(8) does not pass those options
130 * from userland, but they can be passed by loader(8) via
131 * vfs.root.mountfrom.options.
133 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
134 "noclusterw", "noexec", "export", "force", "from", "groupquota",
135 "multilabel", "nfsv4acls", "fsckpid", "snapshot", "nosuid", "suiddir",
136 "nosymfollow", "sync", "union", "userquota", NULL };
139 ffs_mount(struct mount *mp)
143 struct ufsmount *ump = 0;
149 struct nameidata ndp;
153 if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
155 if (uma_inode == NULL) {
156 uma_inode = uma_zcreate("FFS inode",
157 sizeof(struct inode), NULL, NULL, NULL, NULL,
159 uma_ufs1 = uma_zcreate("FFS1 dinode",
160 sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
162 uma_ufs2 = uma_zcreate("FFS2 dinode",
163 sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
167 vfs_deleteopt(mp->mnt_optnew, "groupquota");
168 vfs_deleteopt(mp->mnt_optnew, "userquota");
170 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
175 if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
176 mntorflags |= MNT_ACLS;
178 if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
179 mntorflags |= MNT_SNAPSHOT;
181 * Once we have set the MNT_SNAPSHOT flag, do not
182 * persist "snapshot" in the options list.
184 vfs_deleteopt(mp->mnt_optnew, "snapshot");
185 vfs_deleteopt(mp->mnt_opt, "snapshot");
188 if (vfs_getopt(mp->mnt_optnew, "fsckpid", NULL, NULL) == 0 &&
189 vfs_scanopt(mp->mnt_optnew, "fsckpid", "%d", &fsckpid) == 1) {
191 * Once we have set the restricted PID, do not
192 * persist "fsckpid" in the options list.
194 vfs_deleteopt(mp->mnt_optnew, "fsckpid");
195 vfs_deleteopt(mp->mnt_opt, "fsckpid");
196 if (mp->mnt_flag & MNT_UPDATE) {
197 if (VFSTOUFS(mp)->um_fs->fs_ronly == 0 &&
198 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
199 printf("Checker enable: Must be read-only\n");
202 } else if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) == 0) {
203 printf("Checker enable: Must be read-only\n");
206 /* Set to -1 if we are done */
211 if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
212 if (mntorflags & MNT_ACLS) {
213 printf("WARNING: \"acls\" and \"nfsv4acls\" "
214 "options are mutually exclusive\n");
217 mntorflags |= MNT_NFS4ACLS;
221 mp->mnt_flag |= mntorflags;
224 * If updating, check whether changing from read-only to
225 * read/write; if there is no device name, that's all we do.
227 if (mp->mnt_flag & MNT_UPDATE) {
230 devvp = ump->um_devvp;
231 if (fsckpid == -1 && ump->um_fsckpid > 0) {
232 if ((error = ffs_flushfiles(mp, WRITECLOSE, td)) != 0 ||
233 (error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0)
238 * Return to normal read-only mode.
240 error = g_access(ump->um_cp, 0, -1, 0);
245 if (fs->fs_ronly == 0 &&
246 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
248 * Flush any dirty data and suspend filesystem.
250 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
253 vn_finished_write(mp);
254 if ((error = vfs_write_suspend(mp)) != 0)
257 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
259 * Allow the secondary writes
262 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
264 wakeup(&mp->mnt_flag);
267 * Allow the curthread to
268 * ignore the suspension to
269 * synchronize on-disk state.
271 td->td_pflags |= TDP_IGNSUSP;
275 vn_start_write(NULL, &mp, V_WAIT);
278 * Check for and optionally get rid of files open
282 if (mp->mnt_flag & MNT_FORCE)
284 if (MOUNTEDSOFTDEP(mp)) {
285 error = softdep_flushfiles(mp, flags, td);
287 error = ffs_flushfiles(mp, flags, td);
290 vfs_write_resume(mp);
293 if (fs->fs_pendingblocks != 0 ||
294 fs->fs_pendinginodes != 0) {
295 printf("%s: %s: blocks %jd files %d\n",
296 fs->fs_fsmnt, "update error",
297 (intmax_t)fs->fs_pendingblocks,
298 fs->fs_pendinginodes);
299 fs->fs_pendingblocks = 0;
300 fs->fs_pendinginodes = 0;
302 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
304 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
307 vfs_write_resume(mp);
310 if (MOUNTEDSOFTDEP(mp))
315 * Drop our write and exclusive access.
317 g_access(ump->um_cp, 0, -1, -1);
322 mp->mnt_flag |= MNT_RDONLY;
325 * Allow the writers to note that filesystem
328 vfs_write_resume(mp);
330 if ((mp->mnt_flag & MNT_RELOAD) &&
331 (error = ffs_reload(mp, td)) != 0)
334 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
336 * If we are running a checker, do not allow upgrade.
338 if (ump->um_fsckpid > 0) {
339 printf("Active checker, cannot rw upgrade\n");
343 * If upgrade to read-write by non-root, then verify
344 * that user has necessary permissions on the device.
346 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
347 error = VOP_ACCESS(devvp, VREAD | VWRITE,
350 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
352 VOP_UNLOCK(devvp, 0);
355 VOP_UNLOCK(devvp, 0);
356 fs->fs_flags &= ~FS_UNCLEAN;
357 if (fs->fs_clean == 0) {
358 fs->fs_flags |= FS_UNCLEAN;
359 if ((mp->mnt_flag & MNT_FORCE) ||
361 (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
362 (fs->fs_flags & FS_DOSOFTDEP))) {
363 printf("WARNING: %s was not %s\n",
364 fs->fs_fsmnt, "properly dismounted");
367 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
369 if (fs->fs_flags & FS_SUJ)
371 "WARNING: Forced mount will invalidate journal contents\n");
378 * Request exclusive write access.
380 error = g_access(ump->um_cp, 0, 1, 1);
385 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
389 mp->mnt_flag &= ~MNT_RDONLY;
391 fs->fs_mtime = time_second;
392 /* check to see if we need to start softdep */
393 if ((fs->fs_flags & FS_DOSOFTDEP) &&
394 (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
395 vn_finished_write(mp);
399 if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
400 vn_finished_write(mp);
403 if (fs->fs_snapinum[0] != 0)
404 ffs_snapshot_mount(mp);
405 vn_finished_write(mp);
408 * Soft updates is incompatible with "async",
409 * so if we are doing softupdates stop the user
410 * from setting the async flag in an update.
411 * Softdep_mount() clears it in an initial mount
414 if (MOUNTEDSOFTDEP(mp)) {
415 /* XXX: Reset too late ? */
417 mp->mnt_flag &= ~MNT_ASYNC;
421 * Keep MNT_ACLS flag if it is stored in superblock.
423 if ((fs->fs_flags & FS_ACLS) != 0) {
424 /* XXX: Set too late ? */
426 mp->mnt_flag |= MNT_ACLS;
430 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
431 /* XXX: Set too late ? */
433 mp->mnt_flag |= MNT_NFS4ACLS;
437 * If this is a request from fsck to clean up the filesystem,
438 * then allow the specified pid to proceed.
441 if (ump->um_fsckpid != 0) {
442 printf("Active checker already running on %s\n",
446 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
447 ("soft updates enabled on read-only file system"));
451 * Request write access.
453 error = g_access(ump->um_cp, 0, 1, 0);
457 printf("Checker activation failed on %s\n",
461 ump->um_fsckpid = fsckpid;
462 if (fs->fs_snapinum[0] != 0)
463 ffs_snapshot_mount(mp);
464 fs->fs_mtime = time_second;
467 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
471 * If this is a snapshot request, take the snapshot.
473 if (mp->mnt_flag & MNT_SNAPSHOT)
474 return (ffs_snapshot(mp, fspec));
478 * Not an update, or updating the name: look up the name
479 * and verify that it refers to a sensible disk device.
481 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
482 if ((error = namei(&ndp)) != 0)
484 NDFREE(&ndp, NDF_ONLY_PNBUF);
486 if (!vn_isdisk(devvp, &error)) {
492 * If mount by non-root, then verify that user has necessary
493 * permissions on the device.
496 if ((mp->mnt_flag & MNT_RDONLY) == 0)
498 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
500 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
506 if (mp->mnt_flag & MNT_UPDATE) {
510 * If it's not the same vnode, or at least the same device
511 * then it's not correct.
514 if (devvp->v_rdev != ump->um_devvp->v_rdev)
515 error = EINVAL; /* needs translation */
523 * We need the name for the mount point (also used for
524 * "last mounted on") copied in. If an error occurs,
525 * the mount point is discarded by the upper level code.
526 * Note that vfs_mount() populates f_mntonname for us.
528 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
533 KASSERT(MOUNTEDSOFTDEP(mp) == 0,
534 ("soft updates enabled on read-only file system"));
540 * Request write access.
542 error = g_access(ump->um_cp, 0, 1, 0);
546 printf("Checker activation failed on %s\n",
549 ump->um_fsckpid = fsckpid;
550 if (fs->fs_snapinum[0] != 0)
551 ffs_snapshot_mount(mp);
552 fs->fs_mtime = time_second;
554 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
558 vfs_mountedfrom(mp, fspec);
563 * Compatibility with old mount system call.
567 ffs_cmount(struct mntarg *ma, void *data, int flags)
569 struct ufs_args args;
570 struct export_args exp;
575 error = copyin(data, &args, sizeof args);
578 vfs_oexport_conv(&args.export, &exp);
580 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
581 ma = mount_arg(ma, "export", &exp, sizeof(exp));
582 error = kernel_mount(ma, flags);
588 * Reload all incore data for a filesystem (used after running fsck on
589 * the root filesystem and finding things to fix). The filesystem must
590 * be mounted read-only.
592 * Things to do to update the mount:
593 * 1) invalidate all cached meta-data.
594 * 2) re-read superblock from disk.
595 * 3) re-read summary information from disk.
596 * 4) invalidate all inactive vnodes.
597 * 5) invalidate all cached file data.
598 * 6) re-read inode data for all active vnodes.
601 ffs_reload(struct mount *mp, struct thread *td)
603 struct vnode *vp, *mvp, *devvp;
607 struct fs *fs, *newfs;
608 struct ufsmount *ump;
609 ufs2_daddr_t sblockloc;
610 int i, blks, size, error;
613 if ((mp->mnt_flag & MNT_RDONLY) == 0)
617 * Step 1: invalidate all cached meta-data.
619 devvp = VFSTOUFS(mp)->um_devvp;
620 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
621 if (vinvalbuf(devvp, 0, 0, 0) != 0)
622 panic("ffs_reload: dirty1");
623 VOP_UNLOCK(devvp, 0);
626 * Step 2: re-read superblock from disk.
628 fs = VFSTOUFS(mp)->um_fs;
629 if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
632 newfs = (struct fs *)bp->b_data;
633 if ((newfs->fs_magic != FS_UFS1_MAGIC &&
634 newfs->fs_magic != FS_UFS2_MAGIC) ||
635 newfs->fs_bsize > MAXBSIZE ||
636 newfs->fs_bsize < sizeof(struct fs)) {
638 return (EIO); /* XXX needs translation */
641 * Copy pointer fields back into superblock before copying in XXX
642 * new superblock. These should really be in the ufsmount. XXX
643 * Note that important parameters (eg fs_ncg) are unchanged.
645 newfs->fs_csp = fs->fs_csp;
646 newfs->fs_maxcluster = fs->fs_maxcluster;
647 newfs->fs_contigdirs = fs->fs_contigdirs;
648 newfs->fs_active = fs->fs_active;
649 /* The file system is still read-only. */
651 sblockloc = fs->fs_sblockloc;
652 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
654 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
655 ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
657 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
658 printf("%s: reload pending error: blocks %jd files %d\n",
659 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
660 fs->fs_pendinginodes);
661 fs->fs_pendingblocks = 0;
662 fs->fs_pendinginodes = 0;
667 * Step 3: re-read summary information from disk.
669 blks = howmany(fs->fs_cssize, fs->fs_fsize);
671 for (i = 0; i < blks; i += fs->fs_frag) {
673 if (i + fs->fs_frag > blks)
674 size = (blks - i) * fs->fs_fsize;
675 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
679 bcopy(bp->b_data, space, (u_int)size);
680 space = (char *)space + size;
684 * We no longer know anything about clusters per cylinder group.
686 if (fs->fs_contigsumsize > 0) {
687 lp = fs->fs_maxcluster;
688 for (i = 0; i < fs->fs_ncg; i++)
689 *lp++ = fs->fs_contigsumsize;
694 MNT_VNODE_FOREACH(vp, mp, mvp) {
696 if (vp->v_iflag & VI_DOOMED) {
702 * Step 4: invalidate all cached file data.
704 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
705 MNT_VNODE_FOREACH_ABORT(mp, mvp);
708 if (vinvalbuf(vp, 0, 0, 0))
709 panic("ffs_reload: dirty2");
711 * Step 5: re-read inode data for all active vnodes.
715 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
716 (int)fs->fs_bsize, NOCRED, &bp);
720 MNT_VNODE_FOREACH_ABORT(mp, mvp);
723 ffs_load_inode(bp, ip, fs, ip->i_number);
724 ip->i_effnlink = ip->i_nlink;
735 * Possible superblock locations ordered from most to least likely.
737 static int sblock_try[] = SBLOCKSEARCH;
740 * Common code for mount and mountroot
743 ffs_mountfs(devvp, mp, td)
748 struct ufsmount *ump;
753 ufs2_daddr_t sblockloc;
754 int error, i, blks, size, ronly;
757 struct g_consumer *cp;
762 cred = td ? td->td_ucred : NOCRED;
763 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
769 error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
772 VOP_UNLOCK(devvp, 0);
775 if (devvp->v_rdev->si_iosize_max != 0)
776 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
777 if (mp->mnt_iosize_max > MAXPHYS)
778 mp->mnt_iosize_max = MAXPHYS;
780 devvp->v_bufobj.bo_ops = &ffs_ops;
785 * Try reading the superblock in each of its possible locations.
787 for (i = 0; sblock_try[i] != -1; i++) {
788 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
791 "Invalid sectorsize %d for superblock size %d",
792 cp->provider->sectorsize, SBLOCKSIZE);
795 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
798 fs = (struct fs *)bp->b_data;
799 sblockloc = sblock_try[i];
800 if ((fs->fs_magic == FS_UFS1_MAGIC ||
801 (fs->fs_magic == FS_UFS2_MAGIC &&
802 (fs->fs_sblockloc == sblockloc ||
803 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
804 fs->fs_bsize <= MAXBSIZE &&
805 fs->fs_bsize >= sizeof(struct fs))
810 if (sblock_try[i] == -1) {
811 error = EINVAL; /* XXX needs translation */
815 fs->fs_flags &= ~FS_INDEXDIRS; /* no support for directory indicies */
816 fs->fs_flags &= ~FS_UNCLEAN;
817 if (fs->fs_clean == 0) {
818 fs->fs_flags |= FS_UNCLEAN;
819 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
820 ((fs->fs_flags & (FS_SUJ | FS_NEEDSFSCK)) == 0 &&
821 (fs->fs_flags & FS_DOSOFTDEP))) {
822 printf("WARNING: %s was not properly dismounted\n",
826 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
828 if (fs->fs_flags & FS_SUJ)
830 "WARNING: Forced mount will invalidate journal contents\n");
834 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
835 (mp->mnt_flag & MNT_FORCE)) {
836 printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
837 (intmax_t)fs->fs_pendingblocks,
838 fs->fs_pendinginodes);
839 fs->fs_pendingblocks = 0;
840 fs->fs_pendinginodes = 0;
843 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
844 printf("%s: mount pending error: blocks %jd files %d\n",
845 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
846 fs->fs_pendinginodes);
847 fs->fs_pendingblocks = 0;
848 fs->fs_pendinginodes = 0;
850 if ((fs->fs_flags & FS_GJOURNAL) != 0) {
853 * Get journal provider name.
856 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
857 if (g_io_getattr("GJOURNAL::provider", cp, &size,
858 mp->mnt_gjprovider) == 0) {
859 mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
862 mp->mnt_flag |= MNT_GJOURNAL;
866 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
867 mp->mnt_stat.f_mntonname);
868 free(mp->mnt_gjprovider, M_UFSMNT);
869 mp->mnt_gjprovider = NULL;
873 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
874 mp->mnt_stat.f_mntonname);
877 mp->mnt_gjprovider = NULL;
879 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
881 ump->um_bo = &devvp->v_bufobj;
882 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
883 if (fs->fs_magic == FS_UFS1_MAGIC) {
884 ump->um_fstype = UFS1;
885 ump->um_balloc = ffs_balloc_ufs1;
887 ump->um_fstype = UFS2;
888 ump->um_balloc = ffs_balloc_ufs2;
890 ump->um_blkatoff = ffs_blkatoff;
891 ump->um_truncate = ffs_truncate;
892 ump->um_update = ffs_update;
893 ump->um_valloc = ffs_valloc;
894 ump->um_vfree = ffs_vfree;
895 ump->um_ifree = ffs_ifree;
896 ump->um_rdonly = ffs_rdonly;
897 ump->um_snapgone = ffs_snapgone;
898 mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
899 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
900 if (fs->fs_sbsize < SBLOCKSIZE)
901 bp->b_flags |= B_INVAL | B_NOCACHE;
905 ffs_oldfscompat_read(fs, ump, sblockloc);
906 fs->fs_ronly = ronly;
907 size = fs->fs_cssize;
908 blks = howmany(size, fs->fs_fsize);
909 if (fs->fs_contigsumsize > 0)
910 size += fs->fs_ncg * sizeof(int32_t);
911 size += fs->fs_ncg * sizeof(u_int8_t);
912 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
914 for (i = 0; i < blks; i += fs->fs_frag) {
916 if (i + fs->fs_frag > blks)
917 size = (blks - i) * fs->fs_fsize;
918 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
920 free(fs->fs_csp, M_UFSMNT);
923 bcopy(bp->b_data, space, (u_int)size);
924 space = (char *)space + size;
928 if (fs->fs_contigsumsize > 0) {
929 fs->fs_maxcluster = lp = space;
930 for (i = 0; i < fs->fs_ncg; i++)
931 *lp++ = fs->fs_contigsumsize;
934 size = fs->fs_ncg * sizeof(u_int8_t);
935 fs->fs_contigdirs = (u_int8_t *)space;
936 bzero(fs->fs_contigdirs, size);
937 fs->fs_active = NULL;
939 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
940 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
942 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
943 (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
948 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
950 mp->mnt_flag |= MNT_LOCAL;
952 if ((fs->fs_flags & FS_MULTILABEL) != 0) {
955 mp->mnt_flag |= MNT_MULTILABEL;
959 "WARNING: %s: multilabel flag on fs but no MAC support\n",
960 mp->mnt_stat.f_mntonname);
963 if ((fs->fs_flags & FS_ACLS) != 0) {
967 if (mp->mnt_flag & MNT_NFS4ACLS)
968 printf("WARNING: ACLs flag on fs conflicts with "
969 "\"nfsv4acls\" mount option; option ignored\n");
970 mp->mnt_flag &= ~MNT_NFS4ACLS;
971 mp->mnt_flag |= MNT_ACLS;
975 printf("WARNING: %s: ACLs flag on fs but no ACLs support\n",
976 mp->mnt_stat.f_mntonname);
979 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
983 if (mp->mnt_flag & MNT_ACLS)
984 printf("WARNING: NFSv4 ACLs flag on fs conflicts with "
985 "\"acls\" mount option; option ignored\n");
986 mp->mnt_flag &= ~MNT_ACLS;
987 mp->mnt_flag |= MNT_NFS4ACLS;
992 "WARNING: %s: NFSv4 ACLs flag on fs but no ACLs support\n",
993 mp->mnt_stat.f_mntonname);
996 if ((fs->fs_flags & FS_TRIM) != 0) {
998 if (g_io_getattr("GEOM::candelete", cp, &size,
999 &ump->um_candelete) == 0) {
1000 if (!ump->um_candelete)
1002 "WARNING: %s: TRIM flag on fs but disk does not support TRIM\n",
1003 mp->mnt_stat.f_mntonname);
1006 "WARNING: %s: TRIM flag on fs but cannot get whether disk supports TRIM\n",
1007 mp->mnt_stat.f_mntonname);
1008 ump->um_candelete = 0;
1012 ump->um_mountp = mp;
1014 ump->um_devvp = devvp;
1015 ump->um_nindir = fs->fs_nindir;
1016 ump->um_bptrtodb = fs->fs_fsbtodb;
1017 ump->um_seqinc = fs->fs_frag;
1018 for (i = 0; i < MAXQUOTAS; i++)
1019 ump->um_quotas[i] = NULLVP;
1021 ufs_extattr_uepm_init(&ump->um_extattr);
1024 * Set FS local "last mounted on" information (NULL pad)
1026 bzero(fs->fs_fsmnt, MAXMNTLEN);
1027 strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
1028 mp->mnt_stat.f_iosize = fs->fs_bsize;
1030 if (mp->mnt_flag & MNT_ROOTFS) {
1032 * Root mount; update timestamp in mount structure.
1033 * this will be used by the common root mount code
1034 * to update the system clock.
1036 mp->mnt_time = fs->fs_time;
1040 fs->fs_mtime = time_second;
1041 if ((fs->fs_flags & FS_DOSOFTDEP) &&
1042 (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
1043 free(fs->fs_csp, M_UFSMNT);
1044 ffs_flushfiles(mp, FORCECLOSE, td);
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_MPSAFE | MNTK_LOOKUP_SHARED |
1058 MNTK_EXTENDED_SHARED;
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;
1213 if (mntflags & MNT_FORCE) {
1214 flags |= FORCECLOSE;
1215 susp = fs->fs_ronly != 0;
1219 if ((error = ufs_extattr_stop(mp, td))) {
1220 if (error != EOPNOTSUPP)
1221 printf("ffs_unmount: ufs_extattr_stop returned %d\n",
1225 ufs_extattr_uepm_destroy(&ump->um_extattr);
1231 * dounmount already called vn_start_write().
1234 vn_finished_write(mp);
1235 if ((error = vfs_write_suspend(mp)) != 0)
1238 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1239 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1241 wakeup(&mp->mnt_flag);
1243 td->td_pflags |= TDP_IGNSUSP;
1247 vn_start_write(NULL, &mp, V_WAIT);
1250 if (MOUNTEDSOFTDEP(mp))
1251 error = softdep_flushfiles(mp, flags, td);
1253 error = ffs_flushfiles(mp, flags, td);
1254 if (error != 0 && error != ENXIO)
1258 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1259 printf("%s: unmount pending error: blocks %jd files %d\n",
1260 fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1261 fs->fs_pendinginodes);
1262 fs->fs_pendingblocks = 0;
1263 fs->fs_pendinginodes = 0;
1266 softdep_unmount(mp);
1267 if (fs->fs_ronly == 0 || ump->um_fsckpid > 0) {
1268 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1269 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1270 if (error && error != ENXIO) {
1276 vfs_write_resume(mp);
1277 vn_start_write(NULL, &mp, V_WAIT);
1281 if (ump->um_fsckpid > 0) {
1283 * Return to normal read-only mode.
1285 error = g_access(ump->um_cp, 0, -1, 0);
1286 ump->um_fsckpid = 0;
1288 g_vfs_close(ump->um_cp);
1289 g_topology_unlock();
1291 vrele(ump->um_devvp);
1292 dev_rel(ump->um_dev);
1293 mtx_destroy(UFS_MTX(ump));
1294 if (mp->mnt_gjprovider != NULL) {
1295 free(mp->mnt_gjprovider, M_UFSMNT);
1296 mp->mnt_gjprovider = NULL;
1298 free(fs->fs_csp, M_UFSMNT);
1300 free(ump, M_UFSMNT);
1301 mp->mnt_data = NULL;
1303 mp->mnt_flag &= ~MNT_LOCAL;
1309 vfs_write_resume(mp);
1310 vn_start_write(NULL, &mp, V_WAIT);
1314 ufs_extattr_uepm_init(&ump->um_extattr);
1315 #ifdef UFS_EXTATTR_AUTOSTART
1316 (void) ufs_extattr_autostart(mp, td);
1325 * Flush out all the files in a filesystem.
1328 ffs_flushfiles(mp, flags, td)
1333 struct ufsmount *ump;
1338 if (mp->mnt_flag & MNT_QUOTA) {
1340 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1343 for (i = 0; i < MAXQUOTAS; i++) {
1344 quotaoff(td, mp, i);
1347 * Here we fall through to vflush again to ensure
1348 * that we have gotten rid of all the system vnodes.
1352 ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1353 if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1354 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1356 ffs_snapshot_unmount(mp);
1357 flags |= FORCECLOSE;
1359 * Here we fall through to vflush again to ensure
1360 * that we have gotten rid of all the system vnodes.
1364 * Flush all the files.
1366 if ((error = vflush(mp, 0, flags, td)) != 0)
1369 * Flush filesystem metadata.
1371 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1372 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1373 VOP_UNLOCK(ump->um_devvp, 0);
1378 * Get filesystem statistics.
1385 struct ufsmount *ump;
1390 if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1391 panic("ffs_statfs");
1392 sbp->f_version = STATFS_VERSION;
1393 sbp->f_bsize = fs->fs_fsize;
1394 sbp->f_iosize = fs->fs_bsize;
1395 sbp->f_blocks = fs->fs_dsize;
1397 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1398 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1399 sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1400 dbtofsb(fs, fs->fs_pendingblocks);
1401 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1402 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1404 sbp->f_namemax = NAME_MAX;
1409 * Go through the disk queues to initiate sandbagged IO;
1410 * go through the inodes to write those that have been modified;
1411 * initiate the writing of the super block if it has been modified.
1413 * Note: we are always called with the filesystem marked `MPBUSY'.
1416 ffs_sync(mp, waitfor)
1420 struct vnode *mvp, *vp, *devvp;
1423 struct ufsmount *ump = VFSTOUFS(mp);
1425 int error, count, wait, lockreq, allerror = 0;
1428 int secondary_writes;
1429 int secondary_accwrites;
1431 int softdep_accdeps;
1436 if (fs->fs_fmod != 0 && fs->fs_ronly != 0 && ump->um_fsckpid == 0) {
1437 printf("fs = %s\n", fs->fs_fsmnt);
1438 panic("ffs_sync: rofs mod");
1441 * Write back each (modified) inode.
1446 lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1447 if (waitfor == MNT_SUSPEND) {
1451 if (waitfor == MNT_WAIT) {
1453 lockreq = LK_EXCLUSIVE;
1455 lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1458 /* Grab snapshot of secondary write counts */
1459 secondary_writes = mp->mnt_secondary_writes;
1460 secondary_accwrites = mp->mnt_secondary_accwrites;
1462 /* Grab snapshot of softdep dependency counts */
1464 softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1467 MNT_VNODE_FOREACH(vp, mp, mvp) {
1469 * Depend on the mntvnode_slock to keep things stable enough
1470 * for a quick test. Since there might be hundreds of
1471 * thousands of vnodes, we cannot afford even a subroutine
1472 * call unless there's a good chance that we have work to do.
1475 if (vp->v_iflag & VI_DOOMED) {
1480 if (vp->v_type == VNON || ((ip->i_flag &
1481 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1482 vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1487 if ((error = vget(vp, lockreq, td)) != 0) {
1489 if (error == ENOENT || error == ENOLCK) {
1490 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1495 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1502 * Force stale filesystem control information to be flushed.
1504 if (waitfor == MNT_WAIT) {
1505 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1507 /* Flushed work items may create new vnodes to clean */
1508 if (allerror == 0 && count) {
1516 devvp = ump->um_devvp;
1517 bo = &devvp->v_bufobj;
1519 if (waitfor != MNT_LAZY &&
1520 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1522 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1523 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1525 VOP_UNLOCK(devvp, 0);
1526 if (allerror == 0 && waitfor == MNT_WAIT) {
1530 } else if (suspend != 0) {
1531 if (softdep_check_suspend(mp,
1536 secondary_accwrites) != 0)
1537 goto loop; /* More work needed */
1538 mtx_assert(MNT_MTX(mp), MA_OWNED);
1539 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1545 * Write back modified superblock.
1547 if (fs->fs_fmod != 0 &&
1548 (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1554 ffs_vget(mp, ino, flags, vpp)
1560 return (ffs_vgetf(mp, ino, flags, vpp, 0));
1564 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1573 struct ufsmount *ump;
1579 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1580 if (error || *vpp != NULL)
1584 * We must promote to an exclusive lock for vnode creation. This
1585 * can happen if lookup is passed LOCKSHARED.
1587 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1588 flags &= ~LK_TYPE_MASK;
1589 flags |= LK_EXCLUSIVE;
1593 * We do not lock vnode creation as it is believed to be too
1594 * expensive for such rare case as simultaneous creation of vnode
1595 * for same ino by different processes. We just allow them to race
1596 * and check later to decide who wins. Let the race begin!
1604 * If this malloc() is performed after the getnewvnode()
1605 * it might block, leaving a vnode with a NULL v_data to be
1606 * found by ffs_sync() if a sync happens to fire right then,
1607 * which will cause a panic because ffs_sync() blindly
1608 * dereferences vp->v_data (as well it should).
1610 ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1612 /* Allocate a new vnode/inode. */
1613 if (fs->fs_magic == FS_UFS1_MAGIC)
1614 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1616 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1619 uma_zfree(uma_inode, ip);
1623 * FFS supports recursive locking.
1625 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1628 vp->v_bufobj.bo_bsize = fs->fs_bsize;
1638 for (i = 0; i < MAXQUOTAS; i++)
1639 ip->i_dquot[i] = NODQUOT;
1643 if (ffs_flags & FFSV_FORCEINSMQ)
1644 vp->v_vflag |= VV_FORCEINSMQ;
1645 error = insmntque(vp, mp);
1647 uma_zfree(uma_inode, ip);
1651 vp->v_vflag &= ~VV_FORCEINSMQ;
1652 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1653 if (error || *vpp != NULL)
1656 /* Read in the disk contents for the inode, copy into the inode. */
1657 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1658 (int)fs->fs_bsize, NOCRED, &bp);
1661 * The inode does not contain anything useful, so it would
1662 * be misleading to leave it on its hash chain. With mode
1663 * still zero, it will be unlinked and returned to the free
1671 if (ip->i_ump->um_fstype == UFS1)
1672 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1674 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1675 ffs_load_inode(bp, ip, fs, ino);
1676 if (DOINGSOFTDEP(vp))
1677 softdep_load_inodeblock(ip);
1679 ip->i_effnlink = ip->i_nlink;
1683 * Initialize the vnode from the inode, check for aliases.
1684 * Note that the underlying vnode may have changed.
1686 if (ip->i_ump->um_fstype == UFS1)
1687 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1689 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1697 * Finish inode initialization.
1699 if (vp->v_type != VFIFO) {
1700 /* FFS supports shared locking for all files except fifos. */
1705 * Set up a generation number for this inode if it does not
1706 * already have one. This should only happen on old filesystems.
1708 if (ip->i_gen == 0) {
1709 ip->i_gen = arc4random() / 2 + 1;
1710 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1711 ip->i_flag |= IN_MODIFIED;
1712 DIP_SET(ip, i_gen, ip->i_gen);
1716 if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1718 * If this vnode is already allocated, and we're running
1719 * multi-label, attempt to perform a label association
1720 * from the extended attributes on the inode.
1722 error = mac_vnode_associate_extattr(mp, vp);
1724 /* ufs_inactive will release ip->i_devvp ref. */
1737 * File handle to vnode
1739 * Have to be really careful about stale file handles:
1740 * - check that the inode number is valid
1741 * - call ffs_vget() to get the locked inode
1742 * - check for an unallocated inode (i_mode == 0)
1743 * - check that the given client host has export rights and return
1744 * those rights via. exflagsp and credanonp
1747 ffs_fhtovp(mp, fhp, flags, vpp)
1756 ufhp = (struct ufid *)fhp;
1757 fs = VFSTOUFS(mp)->um_fs;
1758 if (ufhp->ufid_ino < ROOTINO ||
1759 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1761 return (ufs_fhtovp(mp, ufhp, flags, vpp));
1765 * Initialize the filesystem.
1769 struct vfsconf *vfsp;
1772 softdep_initialize();
1773 return (ufs_init(vfsp));
1777 * Undo the work of ffs_init().
1781 struct vfsconf *vfsp;
1785 ret = ufs_uninit(vfsp);
1786 softdep_uninitialize();
1791 * Write a superblock and associated information back to disk.
1794 ffs_sbupdate(ump, waitfor, suspended)
1795 struct ufsmount *ump;
1799 struct fs *fs = ump->um_fs;
1804 int i, size, error, allerror = 0;
1806 if (fs->fs_ronly == 1 &&
1807 (ump->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1808 (MNT_RDONLY | MNT_UPDATE) && ump->um_fsckpid == 0)
1809 panic("ffs_sbupdate: write read-only filesystem");
1811 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1813 sbbp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
1814 (int)fs->fs_sbsize, 0, 0, 0);
1816 * First write back the summary information.
1818 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1820 for (i = 0; i < blks; i += fs->fs_frag) {
1821 size = fs->fs_bsize;
1822 if (i + fs->fs_frag > blks)
1823 size = (blks - i) * fs->fs_fsize;
1824 bp = getblk(ump->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1826 bcopy(space, bp->b_data, (u_int)size);
1827 space = (char *)space + size;
1829 bp->b_flags |= B_VALIDSUSPWRT;
1830 if (waitfor != MNT_WAIT)
1832 else if ((error = bwrite(bp)) != 0)
1836 * Now write back the superblock itself. If any errors occurred
1837 * up to this point, then fail so that the superblock avoids
1838 * being written out as clean.
1845 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1846 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1847 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1848 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1849 fs->fs_sblockloc = SBLOCK_UFS1;
1851 if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1852 (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1853 printf("%s: correcting fs_sblockloc from %jd to %d\n",
1854 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1855 fs->fs_sblockloc = SBLOCK_UFS2;
1858 fs->fs_time = time_second;
1859 if (fs->fs_flags & FS_DOSOFTDEP)
1860 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, bp);
1861 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1862 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
1864 bp->b_flags |= B_VALIDSUSPWRT;
1865 if (waitfor != MNT_WAIT)
1867 else if ((error = bwrite(bp)) != 0)
1873 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1874 int attrnamespace, const char *attrname)
1878 return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1881 return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1887 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1890 if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1891 uma_zfree(uma_ufs1, ip->i_din1);
1892 else if (ip->i_din2 != NULL)
1893 uma_zfree(uma_ufs2, ip->i_din2);
1894 uma_zfree(uma_inode, ip);
1897 static int dobkgrdwrite = 1;
1898 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1899 "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1902 * Complete a background write started from bwrite.
1905 ffs_backgroundwritedone(struct buf *bp)
1907 struct bufobj *bufobj;
1911 * Find the original buffer that we are writing.
1913 bufobj = bp->b_bufobj;
1915 if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1916 panic("backgroundwritedone: lost buffer");
1917 /* Grab an extra reference to be dropped by the bufdone() below. */
1918 bufobj_wrefl(bufobj);
1921 * Process dependencies then return any unfinished ones.
1923 if (!LIST_EMPTY(&bp->b_dep))
1926 if (!LIST_EMPTY(&bp->b_dep))
1927 softdep_move_dependencies(bp, origbp);
1930 * This buffer is marked B_NOCACHE so when it is released
1931 * by biodone it will be tossed.
1933 bp->b_flags |= B_NOCACHE;
1934 bp->b_flags &= ~B_CACHE;
1938 * Clear the BV_BKGRDINPROG flag in the original buffer
1939 * and awaken it if it is waiting for the write to complete.
1940 * If BV_BKGRDINPROG is not set in the original buffer it must
1941 * have been released and re-instantiated - which is not legal.
1943 KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1944 ("backgroundwritedone: lost buffer2"));
1945 origbp->b_vflags &= ~BV_BKGRDINPROG;
1946 if (origbp->b_vflags & BV_BKGRDWAIT) {
1947 origbp->b_vflags &= ~BV_BKGRDWAIT;
1948 wakeup(&origbp->b_xflags);
1955 * Write, release buffer on completion. (Done by iodone
1956 * if async). Do not bother writing anything if the buffer
1959 * Note that we set B_CACHE here, indicating that buffer is
1960 * fully valid and thus cacheable. This is true even of NFS
1961 * now so we set it generally. This could be set either here
1962 * or in biodone() since the I/O is synchronous. We put it
1966 ffs_bufwrite(struct buf *bp)
1971 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1972 if (bp->b_flags & B_INVAL) {
1977 oldflags = bp->b_flags;
1979 if (!BUF_ISLOCKED(bp))
1980 panic("bufwrite: buffer is not busy???");
1983 * If a background write is already in progress, delay
1984 * writing this block if it is asynchronous. Otherwise
1985 * wait for the background write to complete.
1987 BO_LOCK(bp->b_bufobj);
1988 if (bp->b_vflags & BV_BKGRDINPROG) {
1989 if (bp->b_flags & B_ASYNC) {
1990 BO_UNLOCK(bp->b_bufobj);
1995 bp->b_vflags |= BV_BKGRDWAIT;
1996 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1997 if (bp->b_vflags & BV_BKGRDINPROG)
1998 panic("bufwrite: still writing");
2000 BO_UNLOCK(bp->b_bufobj);
2003 * If this buffer is marked for background writing and we
2004 * do not have to wait for it, make a copy and write the
2005 * copy so as to leave this buffer ready for further use.
2007 * This optimization eats a lot of memory. If we have a page
2008 * or buffer shortfall we can't do it.
2010 if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
2011 (bp->b_flags & B_ASYNC) &&
2012 !vm_page_count_severe() &&
2013 !buf_dirty_count_severe()) {
2014 KASSERT(bp->b_iodone == NULL,
2015 ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
2017 /* get a new block */
2018 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
2023 * set it to be identical to the old block. We have to
2024 * set b_lblkno and BKGRDMARKER before calling bgetvp()
2025 * to avoid confusing the splay tree and gbincore().
2027 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
2028 newbp->b_lblkno = bp->b_lblkno;
2029 newbp->b_xflags |= BX_BKGRDMARKER;
2030 BO_LOCK(bp->b_bufobj);
2031 bp->b_vflags |= BV_BKGRDINPROG;
2032 bgetvp(bp->b_vp, newbp);
2033 BO_UNLOCK(bp->b_bufobj);
2034 newbp->b_bufobj = &bp->b_vp->v_bufobj;
2035 newbp->b_blkno = bp->b_blkno;
2036 newbp->b_offset = bp->b_offset;
2037 newbp->b_iodone = ffs_backgroundwritedone;
2038 newbp->b_flags |= B_ASYNC;
2039 newbp->b_flags &= ~B_INVAL;
2043 * Move over the dependencies. If there are rollbacks,
2044 * leave the parent buffer dirtied as it will need to
2047 if (LIST_EMPTY(&bp->b_dep) ||
2048 softdep_move_dependencies(bp, newbp) == 0)
2055 * Initiate write on the copy, release the original to
2056 * the B_LOCKED queue so that it cannot go away until
2057 * the background write completes. If not locked it could go
2058 * away and then be reconstituted while it was being written.
2059 * If the reconstituted buffer were written, we could end up
2060 * with two background copies being written at the same time.
2065 /* Mark the buffer clean */
2069 /* Let the normal bufwrite do the rest for us */
2071 return (bufwrite(bp));
2076 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2083 vp = bo->__bo_vnode;
2084 if (bp->b_iocmd == BIO_WRITE) {
2085 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2086 bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2087 (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2088 panic("ffs_geom_strategy: bad I/O");
2089 nocopy = bp->b_flags & B_NOCOPY;
2090 bp->b_flags &= ~(B_VALIDSUSPWRT | B_NOCOPY);
2091 if ((vp->v_vflag & VV_COPYONWRITE) && nocopy == 0 &&
2092 vp->v_rdev->si_snapdata != NULL) {
2093 if ((bp->b_flags & B_CLUSTER) != 0) {
2094 runningbufwakeup(bp);
2095 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2096 b_cluster.cluster_entry) {
2097 error = ffs_copyonwrite(vp, tbp);
2099 error != EOPNOTSUPP) {
2100 bp->b_error = error;
2101 bp->b_ioflags |= BIO_ERROR;
2106 bp->b_runningbufspace = bp->b_bufsize;
2107 atomic_add_long(&runningbufspace,
2108 bp->b_runningbufspace);
2110 error = ffs_copyonwrite(vp, bp);
2111 if (error != 0 && error != EOPNOTSUPP) {
2112 bp->b_error = error;
2113 bp->b_ioflags |= BIO_ERROR;
2120 if ((bp->b_flags & B_CLUSTER) != 0) {
2121 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2122 b_cluster.cluster_entry) {
2123 if (!LIST_EMPTY(&tbp->b_dep))
2127 if (!LIST_EMPTY(&bp->b_dep))
2133 g_vfs_strategy(bo, bp);
2139 db_print_ffs(struct ufsmount *ump)
2141 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n",
2142 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2143 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2144 ump->softdep_deps, ump->softdep_req);
2147 DB_SHOW_COMMAND(ffs, db_show_ffs)
2150 struct ufsmount *ump;
2153 ump = VFSTOUFS((struct mount *)addr);
2158 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2159 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2160 db_print_ffs(VFSTOUFS(mp));