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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
34 * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $
37 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/nlookup.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
49 #include <sys/fcntl.h>
50 #include <sys/diskslice.h>
51 #include <sys/malloc.h>
56 #include "ufs_extern.h"
59 #include "ffs_extern.h"
62 #include <vm/vm_page.h>
63 #include <vm/vm_zone.h>
67 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
69 static int ffs_sbupdate (struct ufsmount *, int);
70 static int ffs_reload (struct mount *, struct ucred *);
71 static int ffs_oldfscompat (struct fs *);
72 static int ffs_mount (struct mount *, char *, caddr_t, struct ucred *);
73 static int ffs_init (struct vfsconf *);
75 static struct vfsops ufs_vfsops = {
76 .vfs_mount = ffs_mount,
77 .vfs_unmount = ffs_unmount,
79 .vfs_quotactl = ufs_quotactl,
80 .vfs_statfs = ffs_statfs,
83 .vfs_fhtovp = ffs_fhtovp,
84 .vfs_checkexp = ufs_check_export,
85 .vfs_vptofh = ffs_vptofh,
89 VFS_SET(ufs_vfsops, ufs, 0);
90 MODULE_VERSION(ufs, 1);
92 extern struct vop_ops ffs_vnode_vops;
93 extern struct vop_ops ffs_spec_vops;
94 extern struct vop_ops ffs_fifo_vops;
99 * Called when mounting local physical media
103 * mp mount point structure
104 * path NULL (flag for root mount!!!)
106 * p process (user credentials check [statfs])
109 * mp mount point structure
110 * path path to mount point
111 * data pointer to argument struct in user space
112 * p process (user credentials check)
115 * !0 error number (errno.h)
120 * mount point is locked
122 * mount point is locked
125 * A NULL path can be used for a flag since the mount
126 * system call will fail with EFAULT in copyinstr in
127 * nlookup() if it is a genuine NULL from the user.
130 ffs_mount(struct mount *mp, /* mount struct pointer */
131 char *path, /* path to mount point */
132 caddr_t data, /* arguments to FS specific mount */
133 struct ucred *cred) /* process requesting mount */
139 struct ufs_args args;
140 struct ufsmount *ump = NULL;
142 int flags, ronly = 0;
144 struct nlookupdata nd;
145 struct vnode *rootvp;
151 * Use NULL path to flag a root mount
156 * Mounting root filesystem
160 if ((error = bdevvp(rootdev, &rootvp))) {
161 kprintf("ffs_mountroot: can't find rootvp\n");
165 if( ( error = ffs_mountfs(rootvp, mp, M_FFSNODE)) != 0) {
166 /* fs specific cleanup (if any)*/
171 goto dostatfs; /* success*/
177 * Mounting non-root filesystem or updating a filesystem
181 /* copy in user arguments*/
182 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
184 goto error_1; /* can't get arguments*/
187 * If updating, check whether changing from read-only to
188 * read/write; if there is no device name, that's all we do.
190 if (mp->mnt_flag & MNT_UPDATE) {
193 devvp = ump->um_devvp;
195 ronly = fs->fs_ronly; /* MNT_RELOAD might change this */
196 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
198 * Flush any dirty data.
200 VFS_SYNC(mp, MNT_WAIT);
202 * Check for and optionally get rid of files open
206 if (mp->mnt_flag & MNT_FORCE)
208 if (mp->mnt_flag & MNT_SOFTDEP) {
209 error = softdep_flushfiles(mp, flags);
211 error = ffs_flushfiles(mp, flags);
215 if (!error && (mp->mnt_flag & MNT_RELOAD)) {
216 error = ffs_reload(mp, NULL);
221 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
223 * If upgrade to read-write by non-root, then verify
224 * that user has necessary permissions on the device.
226 if (cred->cr_uid != 0) {
227 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
228 if ((error = VOP_EACCESS(devvp, VREAD | VWRITE,
236 fs->fs_flags &= ~FS_UNCLEAN;
237 if (fs->fs_clean == 0) {
238 fs->fs_flags |= FS_UNCLEAN;
239 if (mp->mnt_flag & MNT_FORCE) {
241 "WARNING: %s was not properly dismounted\n",
245 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
252 /* check to see if we need to start softdep */
253 if (fs->fs_flags & FS_DOSOFTDEP) {
254 error = softdep_mount(devvp, mp, fs);
261 * Soft updates is incompatible with "async",
262 * so if we are doing softupdates stop the user
263 * from setting the async flag in an update.
264 * Softdep_mount() clears it in an initial mount
267 if (mp->mnt_flag & MNT_SOFTDEP) {
268 mp->mnt_flag &= ~MNT_ASYNC;
270 /* if not updating name...*/
271 if (args.fspec == 0) {
273 * Process export requests. Jumping to "success"
274 * will return the vfs_export() error code.
276 error = vfs_export(mp, &ump->um_export, &args.export);
282 * Not an update, or updating the name: look up the name
283 * and verify that it refers to a sensible block device.
286 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
288 error = nlookup(&nd);
290 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
295 if (!vn_isdisk(devvp, &error))
299 * If mount by non-root, then verify that user has necessary
300 * permissions on the device.
302 if (cred->cr_uid != 0) {
304 if ((mp->mnt_flag & MNT_RDONLY) == 0)
305 accessmode |= VWRITE;
306 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
307 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
314 if (mp->mnt_flag & MNT_UPDATE) {
316 * UPDATE - make sure the resolved vnode represents the same
317 * device. Since devfs, devvp->v_rdev can be used directly as
318 * it is always associated as long as the vnode exists.
320 * Our current open/writecount state is associated with
321 * um_devvp, so continue using um_devvp and throw away devvp.
323 if (devvp != ump->um_devvp) {
324 if (devvp->v_rdev == ump->um_devvp->v_rdev) {
326 devvp = ump->um_devvp;
328 kprintf("cannot update mount, v_rdev does"
329 " not match (%p vs %p)\n",
330 devvp->v_rdev, ump->um_devvp->v_rdev);
331 error = EINVAL; /* needs translation */
337 * Update device name only on success
340 /* Save "mounted from" info for mount point (NULL pad)*/
341 copyinstr( args.fspec,
342 mp->mnt_stat.f_mntfromname,
345 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
354 /* Save "mounted from" info for mount point (NULL pad)*/
355 copyinstr( args.fspec, /* device name*/
356 mp->mnt_stat.f_mntfromname, /* save area*/
357 MNAMELEN - 1, /* max size*/
358 &size); /* real size*/
359 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
361 /* Save "last mounted on" info for mount point (NULL pad)*/
362 bzero(mp->mnt_stat.f_mntonname,
363 sizeof(mp->mnt_stat.f_mntonname));
365 copyinstr(path, mp->mnt_stat.f_mntonname,
366 sizeof(mp->mnt_stat.f_mntonname) - 1,
368 } else { /* Root mount */
369 mp->mnt_stat.f_mntonname[0] = '/';
372 error = ffs_mountfs(devvp, mp, M_FFSNODE);
380 * Initialize FS stat information in mount struct; uses
381 * mp->mnt_stat.f_mntfromname.
383 * This code is common to root and non-root mounts
385 (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
390 error_2: /* error with devvp held*/
392 /* release devvp before failing*/
395 error_1: /* no state to back out*/
398 if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
399 /* Update clean flag after changing read-onlyness. */
401 if (ronly != fs->fs_ronly) {
402 fs->fs_ronly = ronly;
403 fs->fs_clean = ronly &&
404 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
407 * The device must be re-opened as appropriate or
408 * the device close at unmount time will panic.
410 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
412 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
413 VOP_CLOSE(devvp, FREAD|FWRITE);
415 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
416 VOP_CLOSE(devvp, FREAD);
419 ffs_sbupdate(ump, MNT_WAIT);
426 * Reload all incore data for a filesystem (used after running fsck on
427 * the root filesystem and finding things to fix). The filesystem must
428 * be mounted read-only.
430 * Things to do to update the mount:
431 * 1) invalidate all cached meta-data.
432 * 2) re-read superblock from disk.
433 * 3) re-read summary information from disk.
434 * 4) invalidate all inactive vnodes.
435 * 5) invalidate all cached file data.
436 * 6) re-read inode data for all active vnodes.
439 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
450 ffs_reload(struct mount *mp, struct ucred *cred)
455 struct fs *fs, *newfs;
456 struct partinfo dpart;
457 int i, blks, size, error;
458 struct scaninfo scaninfo;
461 if ((mp->mnt_flag & MNT_RDONLY) == 0)
464 * Step 1: invalidate all cached meta-data.
466 devvp = VFSTOUFS(mp)->um_devvp;
467 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
468 error = vinvalbuf(devvp, 0, 0, 0);
471 panic("ffs_reload: dirty1");
474 * The backing device must be VMIO-capable because we use getblk().
475 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
477 if (devvp->v_object == NULL)
478 panic("ffs_reload: devvp has no VM object!");
481 * Step 2: re-read superblock from disk.
483 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD,
487 size = dpart.media_blksize;
489 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
493 newfs = (struct fs *)bp->b_data;
494 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
495 newfs->fs_bsize < sizeof(struct fs)) {
497 return (EIO); /* XXX needs translation */
499 fs = VFSTOUFS(mp)->um_fs;
501 * Copy pointer fields back into superblock before copying in XXX
502 * new superblock. These should really be in the ufsmount. XXX
503 * Note that important parameters (eg fs_ncg) are unchanged.
505 newfs->fs_csp = fs->fs_csp;
506 newfs->fs_maxcluster = fs->fs_maxcluster;
507 newfs->fs_contigdirs = fs->fs_contigdirs;
508 /* The filesystem is still read-only. */
510 bcopy(newfs, fs, (uint)fs->fs_sbsize);
511 if (fs->fs_sbsize < SBSIZE)
512 bp->b_flags |= B_INVAL;
514 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
516 /* An old fsck may have zeroed these fields, so recheck them. */
517 if (fs->fs_avgfilesize <= 0) /* XXX */
518 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
519 if (fs->fs_avgfpdir <= 0) /* XXX */
520 fs->fs_avgfpdir = AFPDIR; /* XXX */
523 * Step 3: re-read summary information from disk.
525 blks = howmany(fs->fs_cssize, fs->fs_fsize);
527 for (i = 0; i < blks; i += fs->fs_frag) {
529 if (i + fs->fs_frag > blks)
530 size = (blks - i) * fs->fs_fsize;
531 error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp);
536 bcopy(bp->b_data, space, (uint)size);
537 space = (char *)space + size;
541 * We no longer know anything about clusters per cylinder group.
543 if (fs->fs_contigsumsize > 0) {
544 lp = fs->fs_maxcluster;
545 for (i = 0; i < fs->fs_ncg; i++)
546 *lp++ = fs->fs_contigsumsize;
551 scaninfo.devvp = devvp;
552 while (error == 0 && scaninfo.rescan) {
554 error = vmntvnodescan(mp, VMSC_GETVX,
555 NULL, ffs_reload_scan2, &scaninfo);
561 ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
563 struct scaninfo *info = data;
574 if (vinvalbuf(vp, 0, 0, 0))
575 panic("ffs_reload: dirty2");
577 * Step 6: re-read inode data for all active vnodes.
580 error = bread(info->devvp,
581 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
582 (int)info->fs->fs_bsize, &bp);
587 ip->i_din = *((struct ufs1_dinode *)bp->b_data +
588 ino_to_fsbo(info->fs, ip->i_number));
589 ip->i_effnlink = ip->i_nlink;
595 * Common code for mount and mountroot
598 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct malloc_type *mtype)
600 struct ufsmount *ump;
604 struct partinfo dpart;
606 int error, i, blks, size, ronly;
608 uint64_t maxfilesize; /* XXX */
611 * Disallow multiple mounts of the same device.
612 * Disallow mounting of a device that is currently in use
613 * Flush out any old buffers remaining from a previous use.
615 error = vfs_mountedon(devvp);
618 if (vcount(devvp) > 0)
620 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
621 error = vinvalbuf(devvp, V_SAVE, 0, 0);
626 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
627 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
628 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
633 if (dev->si_iosize_max != 0)
634 mp->mnt_iosize_max = dev->si_iosize_max;
635 if (mp->mnt_iosize_max > MAXPHYS)
636 mp->mnt_iosize_max = MAXPHYS;
639 * Filesystem supports native FSMIDs
641 mp->mnt_kern_flag |= MNTK_FSMID;
644 * The backing device must be VMIO-capable because we use getblk().
645 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
646 * The VOP_OPEN() call above should have associated a VM object
649 if (devvp->v_object == NULL)
650 panic("ffs_reload: devvp has no VM object!");
652 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD,
653 proc0.p_ucred, NULL) != 0) {
656 size = dpart.media_blksize;
661 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
663 fs = (struct fs *)bp->b_data;
664 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
665 fs->fs_bsize < sizeof(struct fs)) {
666 error = EINVAL; /* XXX needs translation */
670 fs->fs_flags &= ~FS_UNCLEAN;
671 if (fs->fs_clean == 0) {
672 fs->fs_flags |= FS_UNCLEAN;
673 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
675 "WARNING: %s was not properly dismounted\n",
679 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
685 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
686 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
687 error = EROFS; /* needs translation */
690 ump = kmalloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
691 ump->um_malloctype = mtype;
692 ump->um_i_effnlink_valid = 1;
693 ump->um_fs = kmalloc((u_long)fs->fs_sbsize, M_UFSMNT,
695 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
696 if (fs->fs_sbsize < SBSIZE)
697 bp->b_flags |= B_INVAL;
701 fs->fs_ronly = ronly;
702 size = fs->fs_cssize;
703 blks = howmany(size, fs->fs_fsize);
704 if (fs->fs_contigsumsize > 0)
705 size += fs->fs_ncg * sizeof(int32_t);
706 size += fs->fs_ncg * sizeof(uint8_t);
707 space = kmalloc((u_long)size, M_UFSMNT, M_WAITOK);
709 for (i = 0; i < blks; i += fs->fs_frag) {
711 if (i + fs->fs_frag > blks)
712 size = (blks - i) * fs->fs_fsize;
713 if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i),
715 kfree(fs->fs_csp, M_UFSMNT);
718 bcopy(bp->b_data, space, (uint)size);
719 space = (char *)space + size;
723 if (fs->fs_contigsumsize > 0) {
724 fs->fs_maxcluster = lp = space;
725 for (i = 0; i < fs->fs_ncg; i++)
726 *lp++ = fs->fs_contigsumsize;
729 size = fs->fs_ncg * sizeof(uint8_t);
730 fs->fs_contigdirs = (uint8_t *)space;
731 bzero(fs->fs_contigdirs, size);
732 /* Compatibility for old filesystems XXX */
733 if (fs->fs_avgfilesize <= 0) /* XXX */
734 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
735 if (fs->fs_avgfpdir <= 0) /* XXX */
736 fs->fs_avgfpdir = AFPDIR; /* XXX */
737 mp->mnt_data = (qaddr_t)ump;
738 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
739 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
740 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
741 vfs_getvfs(&mp->mnt_stat.f_fsid))
743 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
744 mp->mnt_flag |= MNT_LOCAL;
747 ump->um_devvp = devvp;
748 ump->um_nindir = fs->fs_nindir;
749 ump->um_bptrtodb = fs->fs_fsbtodb;
750 ump->um_seqinc = fs->fs_frag;
751 for (i = 0; i < MAXQUOTAS; i++)
752 ump->um_quotas[i] = NULLVP;
753 dev->si_mountpoint = mp;
756 /* restore "last mounted on" here */
757 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
758 ksnprintf(fs->fs_fsmnt, sizeof(fs->fs_fsmnt),
759 "%s", mp->mnt_stat.f_mntonname);
761 if( mp->mnt_flag & MNT_ROOTFS) {
763 * Root mount; update timestamp in mount structure.
764 * this will be used by the common root mount code
765 * to update the system clock.
767 mp->mnt_time = fs->fs_time;
771 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
772 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
773 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
774 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
775 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
776 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
777 fs->fs_maxfilesize = maxfilesize; /* XXX */
779 if ((fs->fs_flags & FS_DOSOFTDEP) &&
780 (error = softdep_mount(devvp, mp, fs)) != 0) {
781 kfree(fs->fs_csp, M_UFSMNT);
786 (void) ffs_sbupdate(ump, MNT_WAIT);
788 vfs_add_vnodeops(mp, &ffs_vnode_vops, &mp->mnt_vn_norm_ops);
789 vfs_add_vnodeops(mp, &ffs_spec_vops, &mp->mnt_vn_spec_ops);
790 vfs_add_vnodeops(mp, &ffs_fifo_vops, &mp->mnt_vn_fifo_ops);
794 dev->si_mountpoint = NULL;
797 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
799 ufs_ihashuninit(ump);
800 kfree(ump->um_fs, M_UFSMNT);
801 kfree(ump, M_UFSMNT);
802 mp->mnt_data = (qaddr_t)0;
808 * Sanity checks for old filesystems.
810 * XXX - goes away some day.
813 ffs_oldfscompat(struct fs *fs)
815 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
816 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
817 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
818 fs->fs_nrpos = 8; /* XXX */
819 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
822 uint64_t sizepb = fs->fs_bsize; /* XXX */
824 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
825 for (i = 0; i < NIADDR; i++) { /* XXX */
826 sizepb *= NINDIR(fs); /* XXX */
827 fs->fs_maxfilesize += sizepb; /* XXX */
830 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
831 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
832 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
838 * unmount system call
841 ffs_unmount(struct mount *mp, int mntflags)
843 struct ufsmount *ump;
848 if (mntflags & MNT_FORCE) {
851 if (mp->mnt_flag & MNT_SOFTDEP) {
852 if ((error = softdep_flushfiles(mp, flags)) != 0)
855 if ((error = ffs_flushfiles(mp, flags)) != 0)
860 if (fs->fs_ronly == 0) {
861 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
862 error = ffs_sbupdate(ump, MNT_WAIT);
868 ump->um_devvp->v_rdev->si_mountpoint = NULL;
870 vinvalbuf(ump->um_devvp, V_SAVE, 0, 0);
871 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE);
873 vrele(ump->um_devvp);
875 ufs_ihashuninit(ump);
876 kfree(fs->fs_csp, M_UFSMNT);
878 kfree(ump, M_UFSMNT);
879 mp->mnt_data = (qaddr_t)0;
880 mp->mnt_flag &= ~MNT_LOCAL;
885 * Flush out all the files in a filesystem.
888 ffs_flushfiles(struct mount *mp, int flags)
890 struct ufsmount *ump;
895 if (mp->mnt_flag & MNT_QUOTA) {
897 error = vflush(mp, 0, SKIPSYSTEM|flags);
900 /* Find out how many quota files we have open. */
901 for (i = 0; i < MAXQUOTAS; i++) {
902 if (ump->um_quotas[i] == NULLVP)
907 * Here we fall through to vflush again to ensure
908 * that we have gotten rid of all the system vnodes.
913 * Flush all the files.
915 if ((error = vflush(mp, 0, flags)) != 0)
918 * Flush filesystem metadata.
920 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
921 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, 0);
922 vn_unlock(ump->um_devvp);
927 * Get filesystem statistics.
930 ffs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
932 struct ufsmount *ump;
937 if (fs->fs_magic != FS_MAGIC)
939 sbp->f_bsize = fs->fs_fsize;
940 sbp->f_iosize = fs->fs_bsize;
941 sbp->f_blocks = fs->fs_dsize;
942 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
943 fs->fs_cstotal.cs_nffree;
944 sbp->f_bavail = freespace(fs, fs->fs_minfree);
945 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
946 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
947 if (sbp != &mp->mnt_stat) {
948 sbp->f_type = mp->mnt_vfc->vfc_typenum;
949 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
950 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
956 * Go through the disk queues to initiate sandbagged IO;
957 * go through the inodes to write those that have been modified;
958 * initiate the writing of the super block if it has been modified.
960 * Note: we are always called with the filesystem marked `MPBUSY'.
964 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
965 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
968 ffs_sync(struct mount *mp, int waitfor)
970 struct ufsmount *ump = VFSTOUFS(mp);
973 struct scaninfo scaninfo;
976 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
977 kprintf("fs = %s\n", fs->fs_fsmnt);
978 panic("ffs_sync: rofs mod");
982 * Write back each (modified) inode.
984 scaninfo.allerror = 0;
986 scaninfo.waitfor = waitfor;
987 while (scaninfo.rescan) {
989 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
990 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
994 * Force stale filesystem control information to be flushed.
996 if ((waitfor & MNT_LAZY) == 0) {
997 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
998 waitfor = MNT_NOWAIT;
999 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1000 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
1001 scaninfo.allerror = error;
1002 vn_unlock(ump->um_devvp);
1008 * Write back modified superblock.
1010 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1011 scaninfo.allerror = error;
1012 return (scaninfo.allerror);
1016 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1021 * Depend on the mount list's vnode lock to keep things stable
1022 * enough for a quick test. Since there might be hundreds of
1023 * thousands of vnodes, we cannot afford even a subroutine
1024 * call unless there's a good chance that we have work to do.
1027 /* Restart out whole search if this guy is locked
1028 * or is being reclaimed.
1030 if (vp->v_type == VNON || (ip == NULL) || ((ip->i_flag &
1031 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1032 RB_EMPTY(&vp->v_rbdirty_tree))) {
1039 ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1041 struct scaninfo *info = data;
1046 * We have to recheck after having obtained the vnode interlock.
1049 if (vp->v_type == VNON || vp->v_type == VBAD ||
1051 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1052 RB_EMPTY(&vp->v_rbdirty_tree))) {
1055 if (vp->v_type != VCHR) {
1056 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
1057 info->allerror = error;
1060 * We must reference the vp to prevent it from
1061 * getting ripped out from under ffs_update, since
1062 * we are not holding a vnode lock.
1064 /* ffs_update(vp, waitfor == MNT_WAIT); */
1071 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1072 * in from disk. If it is in core, wait for the lock bit to clear, then
1073 * return the inode locked. Detection and handling of mount points must be
1074 * done by the calling routine.
1078 ffs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
1082 struct ufsmount *ump;
1092 if ((*vpp = ufs_ihashget(ump, dev, ino)) != NULL) {
1097 * If this MALLOC() is performed after the getnewvnode()
1098 * it might block, leaving a vnode with a NULL v_data to be
1099 * found by ffs_sync() if a sync happens to fire right then,
1100 * which will cause a panic because ffs_sync() blindly
1101 * dereferences vp->v_data (as well it should).
1103 * XXX this may no longer be true since getnewvnode returns a
1104 * VX locked vnode now.
1106 ip = kmalloc(sizeof(struct inode), ump->um_malloctype,
1109 /* Allocate a new vnode/inode. */
1110 error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
1113 kfree(ip, ump->um_malloctype);
1117 ip->i_fs = fs = ump->um_fs;
1123 for (i = 0; i < MAXQUOTAS; i++)
1124 ip->i_dquot[i] = NODQUOT;
1129 * Insert it into the inode hash table.
1131 if (ufs_ihashins(ump, ip) != 0) {
1132 kprintf("debug: ufs ihashins collision, retrying inode %ld\n",
1133 (long) ip->i_number);
1137 kfree(ip, ump->um_malloctype);
1142 /* Read in the disk contents for the inode, copy into the inode. */
1143 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1144 (int)fs->fs_bsize, &bp);
1147 * The inode does not contain anything useful, so it would
1148 * be misleading to leave it on its hash chain. With mode
1149 * still zero, it will be unlinked and returned to the free
1158 ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1159 if (DOINGSOFTDEP(vp))
1160 softdep_load_inodeblock(ip);
1162 ip->i_effnlink = ip->i_nlink;
1166 * Initialize the vnode from the inode, check for aliases.
1167 * Note that the underlying vnode may have changed.
1169 error = ufs_vinit(mp, &vp);
1177 * Finish inode initialization now that aliasing has been resolved.
1179 ip->i_devvp = ump->um_devvp;
1182 * Set up a generation number for this inode if it does not
1183 * already have one. This should only happen on old filesystems.
1185 if (ip->i_gen == 0) {
1186 ip->i_gen = krandom() / 2 + 1;
1187 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1188 ip->i_flag |= IN_MODIFIED;
1191 * Ensure that uid and gid are correct. This is a temporary
1192 * fix until fsck has been changed to do the update.
1194 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1195 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1196 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1200 * return a VX locked and refd vnode (VX == same as normal vget()
1201 * vnode so we are ok)
1208 * File handle to vnode
1210 * Have to be really careful about stale file handles:
1211 * - check that the inode number is valid
1212 * - call ffs_vget() to get the locked inode
1213 * - check for an unallocated inode (i_mode == 0)
1214 * - check that the given client host has export rights and return
1215 * those rights via. exflagsp and credanonp
1218 ffs_fhtovp(struct mount *mp, struct vnode *rootvp,
1219 struct fid *fhp, struct vnode **vpp)
1224 ufhp = (struct ufid *)fhp;
1225 fs = VFSTOUFS(mp)->um_fs;
1226 if (ufhp->ufid_ino < ROOTINO ||
1227 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1229 return (ufs_fhtovp(mp, rootvp, ufhp, vpp));
1233 * Vnode pointer to File handle
1237 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1243 ufhp = (struct ufid *)fhp;
1244 ufhp->ufid_len = sizeof(struct ufid);
1245 ufhp->ufid_ino = ip->i_number;
1246 ufhp->ufid_gen = ip->i_gen;
1251 * Initialize the filesystem; just use ufs_init.
1254 ffs_init(struct vfsconf *vfsp)
1256 softdep_initialize();
1257 kmalloc_raise_limit(M_FFSNODE, 0);
1258 return (ufs_init(vfsp));
1262 * Write a superblock and associated information back to disk.
1265 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1267 struct fs *dfs, *fs = mp->um_fs;
1271 int i, size, error, allerror = 0;
1274 * First write back the summary information.
1276 * NOTE: the getblk is relative to the device vnode so bio1
1277 * contains the device block number.
1279 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1281 for (i = 0; i < blks; i += fs->fs_frag) {
1282 size = fs->fs_bsize;
1283 if (i + fs->fs_frag > blks)
1284 size = (blks - i) * fs->fs_fsize;
1285 bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i),
1287 bcopy(space, bp->b_data, (uint)size);
1288 space = (char *)space + size;
1289 if (waitfor != MNT_WAIT)
1291 else if ((error = bwrite(bp)) != 0)
1295 * Now write back the superblock itself. If any errors occurred
1296 * up to this point, then fail so that the superblock avoids
1297 * being written out as clean.
1301 bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0);
1303 fs->fs_time = time_second;
1304 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1305 /* Restore compatibility to old filesystems. XXX */
1306 dfs = (struct fs *)bp->b_data; /* XXX */
1307 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1308 dfs->fs_nrpos = -1; /* XXX */
1309 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1310 int32_t *lp, tmp; /* XXX */
1312 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1313 tmp = lp[4]; /* XXX */
1314 for (i = 4; i > 0; i--) /* XXX */
1315 lp[i] = lp[i-1]; /* XXX */
1316 lp[0] = tmp; /* XXX */
1318 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1319 if (waitfor != MNT_WAIT)
1321 else if ((error = bwrite(bp)) != 0)