2 * Copyright (c) 1989, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
30 * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $
33 #include "opt_quota.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/nlookup.h>
40 #include <sys/kernel.h>
41 #include <sys/vnode.h>
42 #include <sys/mount.h>
45 #include <sys/fcntl.h>
46 #include <sys/diskslice.h>
47 #include <sys/malloc.h>
52 #include "ufs_extern.h"
55 #include "ffs_extern.h"
58 #include <vm/vm_page.h>
59 #include <vm/vm_zone.h>
63 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
65 static int ffs_sbupdate (struct ufsmount *, int);
66 static int ffs_reload (struct mount *, struct ucred *);
67 static int ffs_oldfscompat (struct fs *);
68 static int ffs_mount (struct mount *, char *, caddr_t, struct ucred *);
69 static int ffs_init (struct vfsconf *);
71 static struct vfsops ufs_vfsops = {
72 .vfs_mount = ffs_mount,
73 .vfs_unmount = ffs_unmount,
75 .vfs_quotactl = ufs_quotactl,
76 .vfs_statfs = ffs_statfs,
79 .vfs_fhtovp = ffs_fhtovp,
80 .vfs_checkexp = ufs_check_export,
81 .vfs_vptofh = ffs_vptofh,
85 VFS_SET(ufs_vfsops, ufs, 0);
86 MODULE_VERSION(ufs, 1);
88 extern struct vop_ops ffs_vnode_vops;
89 extern struct vop_ops ffs_spec_vops;
90 extern struct vop_ops ffs_fifo_vops;
95 * Called when mounting local physical media
99 * mp mount point structure
100 * path NULL (flag for root mount!!!)
102 * p process (user credentials check [statfs])
105 * mp mount point structure
106 * path path to mount point
107 * data pointer to argument struct in user space
108 * p process (user credentials check)
111 * !0 error number (errno.h)
116 * mount point is locked
118 * mount point is locked
121 * A NULL path can be used for a flag since the mount
122 * system call will fail with EFAULT in copyinstr in
123 * nlookup() if it is a genuine NULL from the user.
126 ffs_mount(struct mount *mp, /* mount struct pointer */
127 char *path, /* path to mount point */
128 caddr_t data, /* arguments to FS specific mount */
129 struct ucred *cred) /* process requesting mount */
135 struct ufs_args args;
136 struct ufsmount *ump = NULL;
138 int flags, ronly = 0;
140 struct nlookupdata nd;
141 struct vnode *rootvp;
147 * Use NULL path to flag a root mount
152 * Mounting root filesystem
156 if ((error = bdevvp(rootdev, &rootvp))) {
157 kprintf("ffs_mountroot: can't find rootvp\n");
161 if( ( error = ffs_mountfs(rootvp, mp, M_FFSNODE)) != 0) {
162 /* fs specific cleanup (if any)*/
167 goto dostatfs; /* success*/
173 * Mounting non-root filesystem or updating a filesystem
177 /* copy in user arguments*/
178 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
180 goto error_1; /* can't get arguments*/
183 * If updating, check whether changing from read-only to
184 * read/write; if there is no device name, that's all we do.
186 if (mp->mnt_flag & MNT_UPDATE) {
189 devvp = ump->um_devvp;
191 ronly = fs->fs_ronly; /* MNT_RELOAD might change this */
192 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
194 * Flush any dirty data.
196 VFS_SYNC(mp, MNT_WAIT);
198 * Check for and optionally get rid of files open
202 if (mp->mnt_flag & MNT_FORCE)
204 if (mp->mnt_flag & MNT_SOFTDEP) {
205 error = softdep_flushfiles(mp, flags);
207 error = ffs_flushfiles(mp, flags);
211 if (!error && (mp->mnt_flag & MNT_RELOAD)) {
212 error = ffs_reload(mp, NULL);
217 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
219 * If upgrade to read-write by non-root, then verify
220 * that user has necessary permissions on the device.
222 if (cred->cr_uid != 0) {
223 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
224 if ((error = VOP_EACCESS(devvp, VREAD | VWRITE,
232 fs->fs_flags &= ~FS_UNCLEAN;
233 if (fs->fs_clean == 0) {
234 fs->fs_flags |= FS_UNCLEAN;
235 if (mp->mnt_flag & MNT_FORCE) {
237 "WARNING: %s was not properly dismounted\n",
241 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
248 /* check to see if we need to start softdep */
249 if (fs->fs_flags & FS_DOSOFTDEP) {
250 error = softdep_mount(devvp, mp, fs);
257 * Soft updates is incompatible with "async",
258 * so if we are doing softupdates stop the user
259 * from setting the async flag in an update.
260 * Softdep_mount() clears it in an initial mount
263 if (mp->mnt_flag & MNT_SOFTDEP) {
264 mp->mnt_flag &= ~MNT_ASYNC;
266 /* if not updating name...*/
267 if (args.fspec == 0) {
269 * Process export requests. Jumping to "success"
270 * will return the vfs_export() error code.
272 error = vfs_export(mp, &ump->um_export, &args.export);
278 * Not an update, or updating the name: look up the name
279 * and verify that it refers to a sensible block device.
282 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
284 error = nlookup(&nd);
286 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
291 if (!vn_isdisk(devvp, &error))
295 * If mount by non-root, then verify that user has necessary
296 * permissions on the device.
298 if (cred->cr_uid != 0) {
300 if ((mp->mnt_flag & MNT_RDONLY) == 0)
301 accessmode |= VWRITE;
302 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
303 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
310 if (mp->mnt_flag & MNT_UPDATE) {
312 * UPDATE - make sure the resolved vnode represents the same
313 * device. Since devfs, devvp->v_rdev can be used directly as
314 * it is always associated as long as the vnode exists.
316 * Our current open/writecount state is associated with
317 * um_devvp, so continue using um_devvp and throw away devvp.
319 if (devvp != ump->um_devvp) {
320 if (devvp->v_rdev == ump->um_devvp->v_rdev) {
322 devvp = ump->um_devvp;
324 kprintf("cannot update mount, v_rdev does"
325 " not match (%p vs %p)\n",
326 devvp->v_rdev, ump->um_devvp->v_rdev);
327 error = EINVAL; /* needs translation */
333 * Update device name only on success
336 /* Save "mounted from" info for mount point (NULL pad)*/
337 copyinstr( args.fspec,
338 mp->mnt_stat.f_mntfromname,
341 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
350 /* Save "mounted from" info for mount point (NULL pad)*/
351 copyinstr( args.fspec, /* device name*/
352 mp->mnt_stat.f_mntfromname, /* save area*/
353 MNAMELEN - 1, /* max size*/
354 &size); /* real size*/
355 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
357 /* Save "last mounted on" info for mount point (NULL pad)*/
358 bzero(mp->mnt_stat.f_mntonname,
359 sizeof(mp->mnt_stat.f_mntonname));
361 copyinstr(path, mp->mnt_stat.f_mntonname,
362 sizeof(mp->mnt_stat.f_mntonname) - 1,
364 } else { /* Root mount */
365 mp->mnt_stat.f_mntonname[0] = '/';
368 error = ffs_mountfs(devvp, mp, M_FFSNODE);
376 * Initialize FS stat information in mount struct; uses
377 * mp->mnt_stat.f_mntfromname.
379 * This code is common to root and non-root mounts
381 (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
386 error_2: /* error with devvp held*/
388 /* release devvp before failing*/
391 error_1: /* no state to back out*/
394 if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
395 /* Update clean flag after changing read-onlyness. */
397 if (ronly != fs->fs_ronly) {
398 fs->fs_ronly = ronly;
399 fs->fs_clean = ronly &&
400 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
403 * The device must be re-opened as appropriate or
404 * the device close at unmount time will panic.
406 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
408 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
409 VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
411 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
412 VOP_CLOSE(devvp, FREAD, NULL);
415 ffs_sbupdate(ump, MNT_WAIT);
422 * Reload all incore data for a filesystem (used after running fsck on
423 * the root filesystem and finding things to fix). The filesystem must
424 * be mounted read-only.
426 * Things to do to update the mount:
427 * 1) invalidate all cached meta-data.
428 * 2) re-read superblock from disk.
429 * 3) re-read summary information from disk.
430 * 4) invalidate all inactive vnodes.
431 * 5) invalidate all cached file data.
432 * 6) re-read inode data for all active vnodes.
435 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
446 ffs_reload(struct mount *mp, struct ucred *cred)
451 struct fs *fs, *newfs;
452 int i, blks, size, error;
453 struct scaninfo scaninfo;
456 if ((mp->mnt_flag & MNT_RDONLY) == 0)
459 * Step 1: invalidate all cached meta-data.
461 devvp = VFSTOUFS(mp)->um_devvp;
462 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
463 error = vinvalbuf(devvp, 0, 0, 0);
466 panic("ffs_reload: dirty1");
469 * The backing device must be VMIO-capable because we use getblk().
470 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
472 if (devvp->v_object == NULL)
473 panic("ffs_reload: devvp has no VM object!");
476 * Step 2: re-read superblock from disk.
478 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
482 newfs = (struct fs *)bp->b_data;
483 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
484 newfs->fs_bsize < sizeof(struct fs)) {
486 return (EIO); /* XXX needs translation */
488 fs = VFSTOUFS(mp)->um_fs;
490 * Copy pointer fields back into superblock before copying in XXX
491 * new superblock. These should really be in the ufsmount. XXX
492 * Note that important parameters (eg fs_ncg) are unchanged.
494 newfs->fs_csp = fs->fs_csp;
495 newfs->fs_maxcluster = fs->fs_maxcluster;
496 newfs->fs_contigdirs = fs->fs_contigdirs;
497 /* The filesystem is still read-only. */
499 bcopy(newfs, fs, (uint)fs->fs_sbsize);
500 if (fs->fs_sbsize < SBSIZE)
501 bp->b_flags |= B_INVAL;
503 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
505 /* An old fsck may have zeroed these fields, so recheck them. */
506 if (fs->fs_avgfilesize <= 0) /* XXX */
507 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
508 if (fs->fs_avgfpdir <= 0) /* XXX */
509 fs->fs_avgfpdir = AFPDIR; /* XXX */
512 * Step 3: re-read summary information from disk.
514 blks = howmany(fs->fs_cssize, fs->fs_fsize);
516 for (i = 0; i < blks; i += fs->fs_frag) {
518 if (i + fs->fs_frag > blks)
519 size = (blks - i) * fs->fs_fsize;
520 error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp);
525 bcopy(bp->b_data, space, (uint)size);
526 space = (char *)space + size;
530 * We no longer know anything about clusters per cylinder group.
532 if (fs->fs_contigsumsize > 0) {
533 lp = fs->fs_maxcluster;
534 for (i = 0; i < fs->fs_ncg; i++)
535 *lp++ = fs->fs_contigsumsize;
540 scaninfo.devvp = devvp;
541 while (error == 0 && scaninfo.rescan) {
543 error = vmntvnodescan(mp, VMSC_GETVX,
544 NULL, ffs_reload_scan2, &scaninfo);
550 ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
552 struct scaninfo *info = data;
563 if (vinvalbuf(vp, 0, 0, 0))
564 panic("ffs_reload: dirty2");
566 * Step 6: re-read inode data for all active vnodes.
569 error = bread(info->devvp,
570 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
571 (int)info->fs->fs_bsize, &bp);
576 ip->i_din = *((struct ufs1_dinode *)bp->b_data +
577 ino_to_fsbo(info->fs, ip->i_number));
578 ip->i_effnlink = ip->i_nlink;
584 * Common code for mount and mountroot
587 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct malloc_type *mtype)
590 struct ufsmount *ump;
595 int error, i, blks, size, ronly;
597 uint64_t maxfilesize; /* XXX */
600 * Disallow multiple mounts of the same device.
601 * Disallow mounting of a device that is currently in use
602 * Flush out any old buffers remaining from a previous use.
604 error = vfs_mountedon(devvp);
607 if (vcount(devvp) > 0)
609 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
610 error = vinvalbuf(devvp, V_SAVE, 0, 0);
615 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
616 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
617 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
622 if (dev->si_iosize_max != 0)
623 mp->mnt_iosize_max = dev->si_iosize_max;
624 if (mp->mnt_iosize_max > MAXPHYS)
625 mp->mnt_iosize_max = MAXPHYS;
628 * Filesystem supports native FSMIDs
630 mp->mnt_kern_flag |= MNTK_FSMID;
633 * The backing device must be VMIO-capable because we use getblk().
634 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
635 * The VOP_OPEN() call above should have associated a VM object
638 if (devvp->v_object == NULL)
639 panic("ffs_reload: devvp has no VM object!");
643 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
645 fs = (struct fs *)bp->b_data;
646 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
647 fs->fs_bsize < sizeof(struct fs)) {
648 error = EINVAL; /* XXX needs translation */
652 fs->fs_flags &= ~FS_UNCLEAN;
653 if (fs->fs_clean == 0) {
654 fs->fs_flags |= FS_UNCLEAN;
655 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
657 "WARNING: %s was not properly dismounted\n",
661 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
667 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
668 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
669 error = EROFS; /* needs translation */
672 ump = kmalloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
673 ump->um_malloctype = mtype;
674 ump->um_i_effnlink_valid = 1;
675 ump->um_fs = kmalloc((u_long)fs->fs_sbsize, M_UFSMNT,
677 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
678 if (fs->fs_sbsize < SBSIZE)
679 bp->b_flags |= B_INVAL;
683 fs->fs_ronly = ronly;
684 size = fs->fs_cssize;
685 blks = howmany(size, fs->fs_fsize);
686 if (fs->fs_contigsumsize > 0)
687 size += fs->fs_ncg * sizeof(int32_t);
688 size += fs->fs_ncg * sizeof(uint8_t);
689 space = kmalloc((u_long)size, M_UFSMNT, M_WAITOK);
691 for (i = 0; i < blks; i += fs->fs_frag) {
693 if (i + fs->fs_frag > blks)
694 size = (blks - i) * fs->fs_fsize;
695 if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i),
697 kfree(fs->fs_csp, M_UFSMNT);
700 bcopy(bp->b_data, space, (uint)size);
701 space = (char *)space + size;
705 if (fs->fs_contigsumsize > 0) {
706 fs->fs_maxcluster = lp = space;
707 for (i = 0; i < fs->fs_ncg; i++)
708 *lp++ = fs->fs_contigsumsize;
711 size = fs->fs_ncg * sizeof(uint8_t);
712 fs->fs_contigdirs = (uint8_t *)space;
713 bzero(fs->fs_contigdirs, size);
714 /* Compatibility for old filesystems XXX */
715 if (fs->fs_avgfilesize <= 0) /* XXX */
716 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
717 if (fs->fs_avgfpdir <= 0) /* XXX */
718 fs->fs_avgfpdir = AFPDIR; /* XXX */
719 mp->mnt_data = (qaddr_t)ump;
720 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
721 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
722 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0) {
724 } else if ((mptmp = vfs_getvfs(&mp->mnt_stat.f_fsid)) != NULL) {
728 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
729 mp->mnt_flag |= MNT_LOCAL;
732 ump->um_devvp = devvp;
733 ump->um_nindir = fs->fs_nindir;
734 ump->um_bptrtodb = fs->fs_fsbtodb;
735 ump->um_seqinc = fs->fs_frag;
736 for (i = 0; i < MAXQUOTAS; i++)
737 ump->um_quotas[i] = NULLVP;
738 dev->si_mountpoint = mp;
741 /* restore "last mounted on" here */
742 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
743 ksnprintf(fs->fs_fsmnt, sizeof(fs->fs_fsmnt),
744 "%s", mp->mnt_stat.f_mntonname);
746 if( mp->mnt_flag & MNT_ROOTFS) {
748 * Root mount; update timestamp in mount structure.
749 * this will be used by the common root mount code
750 * to update the system clock.
752 mp->mnt_time = fs->fs_time;
756 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
757 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
758 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
759 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
760 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
761 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
762 fs->fs_maxfilesize = maxfilesize; /* XXX */
764 if ((fs->fs_flags & FS_DOSOFTDEP) &&
765 (error = softdep_mount(devvp, mp, fs)) != 0) {
766 kfree(fs->fs_csp, M_UFSMNT);
771 (void) ffs_sbupdate(ump, MNT_WAIT);
773 vfs_add_vnodeops(mp, &ffs_vnode_vops, &mp->mnt_vn_norm_ops);
774 vfs_add_vnodeops(mp, &ffs_spec_vops, &mp->mnt_vn_spec_ops);
775 vfs_add_vnodeops(mp, &ffs_fifo_vops, &mp->mnt_vn_fifo_ops);
779 dev->si_mountpoint = NULL;
782 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
783 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
786 ufs_ihashuninit(ump);
787 kfree(ump->um_fs, M_UFSMNT);
788 kfree(ump, M_UFSMNT);
789 mp->mnt_data = (qaddr_t)0;
795 * Sanity checks for old filesystems.
797 * XXX - goes away some day.
800 ffs_oldfscompat(struct fs *fs)
802 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
803 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
804 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
805 fs->fs_nrpos = 8; /* XXX */
806 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
809 uint64_t sizepb = fs->fs_bsize; /* XXX */
811 fs->fs_maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; /* XXX */
812 for (i = 0; i < UFS_NIADDR; i++) { /* XXX */
813 sizepb *= NINDIR(fs); /* XXX */
814 fs->fs_maxfilesize += sizepb; /* XXX */
817 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
818 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
819 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
825 * unmount system call
828 ffs_unmount(struct mount *mp, int mntflags)
830 struct ufsmount *ump;
835 if (mntflags & MNT_FORCE) {
838 if (mp->mnt_flag & MNT_SOFTDEP) {
839 if ((error = softdep_flushfiles(mp, flags)) != 0)
842 if ((error = ffs_flushfiles(mp, flags)) != 0)
847 if (fs->fs_ronly == 0) {
848 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
849 error = ffs_sbupdate(ump, MNT_WAIT);
855 ump->um_devvp->v_rdev->si_mountpoint = NULL;
857 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
858 vinvalbuf(ump->um_devvp, V_SAVE, 0, 0);
859 error = VOP_CLOSE(ump->um_devvp,
860 fs->fs_ronly ? FREAD : FREAD|FWRITE,
862 vn_unlock(ump->um_devvp);
864 vrele(ump->um_devvp);
866 ufs_ihashuninit(ump);
867 kfree(fs->fs_csp, M_UFSMNT);
869 kfree(ump, M_UFSMNT);
870 mp->mnt_data = (qaddr_t)0;
871 mp->mnt_flag &= ~MNT_LOCAL;
876 * Flush out all the files in a filesystem.
879 ffs_flushfiles(struct mount *mp, int flags)
881 struct ufsmount *ump;
886 if (mp->mnt_flag & MNT_QUOTA) {
888 error = vflush(mp, 0, SKIPSYSTEM|flags);
891 /* Find out how many quota files we have open. */
892 for (i = 0; i < MAXQUOTAS; i++) {
893 if (ump->um_quotas[i] == NULLVP)
898 * Here we fall through to vflush again to ensure
899 * that we have gotten rid of all the system vnodes.
904 * Flush all the files.
906 if ((error = vflush(mp, 0, flags)) != 0)
909 * Flush filesystem metadata.
911 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
912 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, 0);
913 vn_unlock(ump->um_devvp);
918 * Get filesystem statistics.
921 ffs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
923 struct ufsmount *ump;
928 if (fs->fs_magic != FS_MAGIC)
930 sbp->f_bsize = fs->fs_fsize;
931 sbp->f_iosize = fs->fs_bsize;
932 sbp->f_blocks = fs->fs_dsize;
933 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
934 fs->fs_cstotal.cs_nffree;
935 sbp->f_bavail = freespace(fs, fs->fs_minfree);
936 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
937 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
938 if (sbp != &mp->mnt_stat) {
939 sbp->f_type = mp->mnt_vfc->vfc_typenum;
940 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
941 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
947 * Go through the disk queues to initiate sandbagged IO;
948 * go through the inodes to write those that have been modified;
949 * initiate the writing of the super block if it has been modified.
951 * Note: we are always called with the filesystem marked `MPBUSY'.
955 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
956 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
959 ffs_sync(struct mount *mp, int waitfor)
961 struct ufsmount *ump = VFSTOUFS(mp);
964 struct scaninfo scaninfo;
967 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
968 kprintf("fs = %s\n", fs->fs_fsmnt);
969 panic("ffs_sync: rofs mod");
973 * Write back each (modified) inode.
975 scaninfo.allerror = 0;
977 scaninfo.waitfor = waitfor;
978 while (scaninfo.rescan) {
980 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
981 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
985 * Force stale filesystem control information to be flushed.
987 if ((waitfor & MNT_LAZY) == 0) {
988 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
989 waitfor = MNT_NOWAIT;
990 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
991 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
992 scaninfo.allerror = error;
993 vn_unlock(ump->um_devvp);
999 * Write back modified superblock.
1001 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1002 scaninfo.allerror = error;
1003 return (scaninfo.allerror);
1007 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1012 * Depend on the mount list's vnode lock to keep things stable
1013 * enough for a quick test. Since there might be hundreds of
1014 * thousands of vnodes, we cannot afford even a subroutine
1015 * call unless there's a good chance that we have work to do.
1018 /* Restart out whole search if this guy is locked
1019 * or is being reclaimed.
1021 if (vp->v_type == VNON || (ip == NULL) || ((ip->i_flag &
1022 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1023 RB_EMPTY(&vp->v_rbdirty_tree))) {
1030 ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1032 struct scaninfo *info = data;
1037 * We have to recheck after having obtained the vnode interlock.
1040 if (vp->v_type == VNON || vp->v_type == VBAD ||
1042 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1043 RB_EMPTY(&vp->v_rbdirty_tree))) {
1046 if (vp->v_type != VCHR) {
1047 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
1048 info->allerror = error;
1051 * We must reference the vp to prevent it from
1052 * getting ripped out from under ffs_update, since
1053 * we are not holding a vnode lock.
1055 /* ffs_update(vp, waitfor == MNT_WAIT); */
1062 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1063 * in from disk. If it is in core, wait for the lock bit to clear, then
1064 * return the inode locked. Detection and handling of mount points must be
1065 * done by the calling routine.
1069 ffs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
1073 struct ufsmount *ump;
1083 if ((*vpp = ufs_ihashget(ump, dev, ino)) != NULL) {
1088 * If this MALLOC() is performed after the getnewvnode()
1089 * it might block, leaving a vnode with a NULL v_data to be
1090 * found by ffs_sync() if a sync happens to fire right then,
1091 * which will cause a panic because ffs_sync() blindly
1092 * dereferences vp->v_data (as well it should).
1094 * XXX this may no longer be true since getnewvnode returns a
1095 * VX locked vnode now.
1097 ip = kmalloc(sizeof(struct inode), ump->um_malloctype,
1100 /* Allocate a new vnode/inode. */
1101 error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
1104 kfree(ip, ump->um_malloctype);
1108 ip->i_fs = fs = ump->um_fs;
1114 for (i = 0; i < MAXQUOTAS; i++)
1115 ip->i_dquot[i] = NODQUOT;
1120 * Insert it into the inode hash table.
1122 if (ufs_ihashins(ump, ip) != 0) {
1123 kprintf("debug: ufs ihashins collision, retrying inode %ld\n",
1124 (long) ip->i_number);
1128 kfree(ip, ump->um_malloctype);
1133 /* Read in the disk contents for the inode, copy into the inode. */
1134 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1135 (int)fs->fs_bsize, &bp);
1138 * The inode does not contain anything useful, so it would
1139 * be misleading to leave it on its hash chain. With mode
1140 * still zero, it will be unlinked and returned to the free
1149 ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1150 if (DOINGSOFTDEP(vp))
1151 softdep_load_inodeblock(ip);
1153 ip->i_effnlink = ip->i_nlink;
1157 * Initialize the vnode from the inode, check for aliases.
1158 * Note that the underlying vnode may have changed.
1160 error = ufs_vinit(mp, &vp);
1168 * Finish inode initialization now that aliasing has been resolved.
1170 ip->i_devvp = ump->um_devvp;
1173 * Set up a generation number for this inode if it does not
1174 * already have one. This should only happen on old filesystems.
1176 if (ip->i_gen == 0) {
1177 ip->i_gen = krandom() / 2 + 1;
1178 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1179 ip->i_flag |= IN_MODIFIED;
1182 * Ensure that uid and gid are correct. This is a temporary
1183 * fix until fsck has been changed to do the update.
1185 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1186 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1187 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1191 * return a VX locked and refd vnode (VX == same as normal vget()
1192 * vnode so we are ok)
1199 * File handle to vnode
1201 * Have to be really careful about stale file handles:
1202 * - check that the inode number is valid
1203 * - call ffs_vget() to get the locked inode
1204 * - check for an unallocated inode (i_mode == 0)
1205 * - check that the given client host has export rights and return
1206 * those rights via. exflagsp and credanonp
1209 ffs_fhtovp(struct mount *mp, struct vnode *rootvp,
1210 struct fid *fhp, struct vnode **vpp)
1215 ufhp = (struct ufid *)fhp;
1216 fs = VFSTOUFS(mp)->um_fs;
1217 if (ufhp->ufid_ino < UFS_ROOTINO ||
1218 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1220 return (ufs_fhtovp(mp, rootvp, ufhp, vpp));
1224 * Vnode pointer to File handle
1228 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1234 ufhp = (struct ufid *)fhp;
1235 ufhp->ufid_len = sizeof(struct ufid);
1236 ufhp->ufid_ino = ip->i_number;
1237 ufhp->ufid_gen = ip->i_gen;
1242 * Initialize the filesystem; just use ufs_init.
1245 ffs_init(struct vfsconf *vfsp)
1247 softdep_initialize();
1248 kmalloc_raise_limit(M_FFSNODE, 0);
1249 return (ufs_init(vfsp));
1253 * Write a superblock and associated information back to disk.
1256 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1258 struct fs *dfs, *fs = mp->um_fs;
1262 int i, size, error, allerror = 0;
1265 * First write back the summary information.
1267 * NOTE: the getblk is relative to the device vnode so bio1
1268 * contains the device block number.
1270 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1272 for (i = 0; i < blks; i += fs->fs_frag) {
1273 size = fs->fs_bsize;
1274 if (i + fs->fs_frag > blks)
1275 size = (blks - i) * fs->fs_fsize;
1276 bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i),
1278 bcopy(space, bp->b_data, (uint)size);
1279 space = (char *)space + size;
1280 if (waitfor != MNT_WAIT)
1282 else if ((error = bwrite(bp)) != 0)
1286 * Now write back the superblock itself. If any errors occurred
1287 * up to this point, then fail so that the superblock avoids
1288 * being written out as clean.
1292 bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0);
1294 fs->fs_time = time_second;
1295 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1296 /* Restore compatibility to old filesystems. XXX */
1297 dfs = (struct fs *)bp->b_data; /* XXX */
1298 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1299 dfs->fs_nrpos = -1; /* XXX */
1300 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1301 int32_t *lp, tmp; /* XXX */
1303 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1304 tmp = lp[4]; /* XXX */
1305 for (i = 4; i > 0; i--) /* XXX */
1306 lp[i] = lp[i-1]; /* XXX */
1307 lp[0] = tmp; /* XXX */
1309 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1310 if (waitfor != MNT_WAIT)
1312 else if ((error = bwrite(bp)) != 0)