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 $
35 * $DragonFly: src/sys/vfs/ufs/ffs_vfsops.c,v 1.59 2008/09/17 21:44:25 dillon Exp $
38 #include "opt_quota.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/nlookup.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
50 #include <sys/fcntl.h>
51 #include <sys/diskslice.h>
52 #include <sys/malloc.h>
57 #include "ufs_extern.h"
60 #include "ffs_extern.h"
63 #include <vm/vm_page.h>
64 #include <vm/vm_zone.h>
68 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
70 static int ffs_sbupdate (struct ufsmount *, int);
71 static int ffs_reload (struct mount *, struct ucred *);
72 static int ffs_oldfscompat (struct fs *);
73 static int ffs_mount (struct mount *, char *, caddr_t, struct ucred *);
74 static int ffs_init (struct vfsconf *);
76 static struct vfsops ufs_vfsops = {
77 .vfs_mount = ffs_mount,
78 .vfs_unmount = ffs_unmount,
80 .vfs_quotactl = ufs_quotactl,
81 .vfs_statfs = ffs_statfs,
84 .vfs_fhtovp = ffs_fhtovp,
85 .vfs_checkexp = ufs_check_export,
86 .vfs_vptofh = ffs_vptofh,
88 .vfs_uninit = ufs_uninit
91 VFS_SET(ufs_vfsops, ufs, 0);
92 MODULE_VERSION(ufs, 1);
94 extern struct vop_ops ffs_vnode_vops;
95 extern struct vop_ops ffs_spec_vops;
96 extern struct vop_ops ffs_fifo_vops;
101 * Called when mounting local physical media
105 * mp mount point structure
106 * path NULL (flag for root mount!!!)
108 * p process (user credentials check [statfs])
111 * mp mount point structure
112 * path path to mount point
113 * data pointer to argument struct in user space
114 * p process (user credentials check)
117 * !0 error number (errno.h)
122 * mount point is locked
124 * mount point is locked
127 * A NULL path can be used for a flag since the mount
128 * system call will fail with EFAULT in copyinstr in
129 * nlookup() if it is a genuine NULL from the user.
132 ffs_mount(struct mount *mp, /* mount struct pointer */
133 char *path, /* path to mount point */
134 caddr_t data, /* arguments to FS specific mount */
135 struct ucred *cred) /* process requesting mount */
141 struct ufs_args args;
142 struct ufsmount *ump = 0;
144 int flags, ronly = 0;
146 struct nlookupdata nd;
147 struct vnode *rootvp;
153 * Use NULL path to flag a root mount
158 * Mounting root filesystem
162 if ((error = bdevvp(rootdev, &rootvp))) {
163 kprintf("ffs_mountroot: can't find rootvp\n");
167 if( ( error = ffs_mountfs(rootvp, mp, M_FFSNODE)) != 0) {
168 /* fs specific cleanup (if any)*/
173 goto dostatfs; /* success*/
179 * Mounting non-root filesystem or updating a filesystem
183 /* copy in user arguments*/
184 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
186 goto error_1; /* can't get arguments*/
189 * If updating, check whether changing from read-only to
190 * read/write; if there is no device name, that's all we do.
192 if (mp->mnt_flag & MNT_UPDATE) {
195 devvp = ump->um_devvp;
197 ronly = fs->fs_ronly; /* MNT_RELOAD might change this */
198 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
200 * Flush any dirty data.
202 VFS_SYNC(mp, MNT_WAIT);
204 * Check for and optionally get rid of files open
208 if (mp->mnt_flag & MNT_FORCE)
210 if (mp->mnt_flag & MNT_SOFTDEP) {
211 error = softdep_flushfiles(mp, flags);
213 error = ffs_flushfiles(mp, flags);
217 if (!error && (mp->mnt_flag & MNT_RELOAD)) {
218 error = ffs_reload(mp, NULL);
223 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
225 * If upgrade to read-write by non-root, then verify
226 * that user has necessary permissions on the device.
228 if (cred->cr_uid != 0) {
229 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
230 if ((error = VOP_EACCESS(devvp, VREAD | VWRITE,
238 fs->fs_flags &= ~FS_UNCLEAN;
239 if (fs->fs_clean == 0) {
240 fs->fs_flags |= FS_UNCLEAN;
241 if (mp->mnt_flag & MNT_FORCE) {
243 "WARNING: %s was not properly dismounted\n",
247 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
254 /* check to see if we need to start softdep */
255 if (fs->fs_flags & FS_DOSOFTDEP) {
256 error = softdep_mount(devvp, mp, fs);
263 * Soft updates is incompatible with "async",
264 * so if we are doing softupdates stop the user
265 * from setting the async flag in an update.
266 * Softdep_mount() clears it in an initial mount
269 if (mp->mnt_flag & MNT_SOFTDEP) {
270 mp->mnt_flag &= ~MNT_ASYNC;
272 /* if not updating name...*/
273 if (args.fspec == 0) {
275 * Process export requests. Jumping to "success"
276 * will return the vfs_export() error code.
278 error = vfs_export(mp, &ump->um_export, &args.export);
284 * Not an update, or updating the name: look up the name
285 * and verify that it refers to a sensible block device.
288 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
290 error = nlookup(&nd);
292 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
297 if (!vn_isdisk(devvp, &error))
301 * If mount by non-root, then verify that user has necessary
302 * permissions on the device.
304 if (cred->cr_uid != 0) {
306 if ((mp->mnt_flag & MNT_RDONLY) == 0)
307 accessmode |= VWRITE;
308 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
309 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
316 if (mp->mnt_flag & MNT_UPDATE) {
318 * UPDATE - make sure the resolved vnode represents the same
319 * device. Note that devvp->v_rdev may be NULL since we
320 * haven't opened it, so compare udev instead.
322 * Our current open/writecount state is associated with
323 * um_devvp, so continue using um_devvp and throw away devvp.
325 if (devvp != ump->um_devvp) {
326 if (devvp->v_umajor == ump->um_devvp->v_umajor &&
327 devvp->v_uminor == ump->um_devvp->v_uminor) {
329 devvp = ump->um_devvp;
331 kprintf("cannot update mount, udev does"
332 " not match %08x:%08x vs %08x:%08x\n",
333 devvp->v_umajor, devvp->v_uminor,
334 ump->um_devvp->v_umajor,
335 ump->um_devvp->v_uminor);
336 error = EINVAL; /* needs translation */
342 * Update device name only on success
345 /* Save "mounted from" info for mount point (NULL pad)*/
346 copyinstr( args.fspec,
347 mp->mnt_stat.f_mntfromname,
350 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
359 /* Save "mounted from" info for mount point (NULL pad)*/
360 copyinstr( args.fspec, /* device name*/
361 mp->mnt_stat.f_mntfromname, /* save area*/
362 MNAMELEN - 1, /* max size*/
363 &size); /* real size*/
364 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
366 /* Save "last mounted on" info for mount point (NULL pad)*/
367 bzero(mp->mnt_stat.f_mntonname,
368 sizeof(mp->mnt_stat.f_mntonname));
370 copyinstr(path, mp->mnt_stat.f_mntonname,
371 sizeof(mp->mnt_stat.f_mntonname) - 1,
375 error = ffs_mountfs(devvp, mp, M_FFSNODE);
383 * Initialize FS stat information in mount struct; uses
384 * mp->mnt_stat.f_mntfromname.
386 * This code is common to root and non-root mounts
388 (void)VFS_STATFS(mp, &mp->mnt_stat, cred);
393 error_2: /* error with devvp held*/
395 /* release devvp before failing*/
398 error_1: /* no state to back out*/
401 if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
402 /* Update clean flag after changing read-onlyness. */
404 if (ronly != fs->fs_ronly) {
405 fs->fs_ronly = ronly;
406 fs->fs_clean = ronly &&
407 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
410 * The device must be re-opened as appropriate or
411 * the device close at unmount time will panic.
413 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
415 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
416 VOP_CLOSE(devvp, FREAD|FWRITE);
418 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
419 VOP_CLOSE(devvp, FREAD);
422 ffs_sbupdate(ump, MNT_WAIT);
429 * Reload all incore data for a filesystem (used after running fsck on
430 * the root filesystem and finding things to fix). The filesystem must
431 * be mounted read-only.
433 * Things to do to update the mount:
434 * 1) invalidate all cached meta-data.
435 * 2) re-read superblock from disk.
436 * 3) re-read summary information from disk.
437 * 4) invalidate all inactive vnodes.
438 * 5) invalidate all cached file data.
439 * 6) re-read inode data for all active vnodes.
442 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
453 ffs_reload(struct mount *mp, struct ucred *cred)
458 struct fs *fs, *newfs;
459 struct partinfo dpart;
461 int i, blks, size, error;
462 struct scaninfo scaninfo;
465 if ((mp->mnt_flag & MNT_RDONLY) == 0)
468 * Step 1: invalidate all cached meta-data.
470 devvp = VFSTOUFS(mp)->um_devvp;
471 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
472 error = vinvalbuf(devvp, 0, 0, 0);
475 panic("ffs_reload: dirty1");
480 * The backing device must be VMIO-capable because we use getblk().
481 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
483 if (devvp->v_object == NULL)
484 panic("ffs_reload: devvp has no VM object!");
487 * Step 2: re-read superblock from disk.
489 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD,
493 size = dpart.media_blksize;
495 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
499 newfs = (struct fs *)bp->b_data;
500 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
501 newfs->fs_bsize < sizeof(struct fs)) {
503 return (EIO); /* XXX needs translation */
505 fs = VFSTOUFS(mp)->um_fs;
507 * Copy pointer fields back into superblock before copying in XXX
508 * new superblock. These should really be in the ufsmount. XXX
509 * Note that important parameters (eg fs_ncg) are unchanged.
511 newfs->fs_csp = fs->fs_csp;
512 newfs->fs_maxcluster = fs->fs_maxcluster;
513 newfs->fs_contigdirs = fs->fs_contigdirs;
514 /* The filesystem is still read-only. */
516 bcopy(newfs, fs, (uint)fs->fs_sbsize);
517 if (fs->fs_sbsize < SBSIZE)
518 bp->b_flags |= B_INVAL;
520 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
522 /* An old fsck may have zeroed these fields, so recheck them. */
523 if (fs->fs_avgfilesize <= 0) /* XXX */
524 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
525 if (fs->fs_avgfpdir <= 0) /* XXX */
526 fs->fs_avgfpdir = AFPDIR; /* XXX */
529 * Step 3: re-read summary information from disk.
531 blks = howmany(fs->fs_cssize, fs->fs_fsize);
533 for (i = 0; i < blks; i += fs->fs_frag) {
535 if (i + fs->fs_frag > blks)
536 size = (blks - i) * fs->fs_fsize;
537 error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp);
542 bcopy(bp->b_data, space, (uint)size);
543 space = (char *)space + size;
547 * We no longer know anything about clusters per cylinder group.
549 if (fs->fs_contigsumsize > 0) {
550 lp = fs->fs_maxcluster;
551 for (i = 0; i < fs->fs_ncg; i++)
552 *lp++ = fs->fs_contigsumsize;
557 scaninfo.devvp = devvp;
558 while (error == 0 && scaninfo.rescan) {
560 error = vmntvnodescan(mp, VMSC_GETVX,
561 NULL, ffs_reload_scan2, &scaninfo);
567 ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
569 struct scaninfo *info = data;
580 if (vinvalbuf(vp, 0, 0, 0))
581 panic("ffs_reload: dirty2");
583 * Step 6: re-read inode data for all active vnodes.
586 error = bread(info->devvp,
587 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
588 (int)info->fs->fs_bsize, &bp);
593 ip->i_din = *((struct ufs1_dinode *)bp->b_data +
594 ino_to_fsbo(info->fs, ip->i_number));
595 ip->i_effnlink = ip->i_nlink;
601 * Common code for mount and mountroot
604 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct malloc_type *mtype)
606 struct ufsmount *ump;
610 struct partinfo dpart;
612 int error, i, blks, size, ronly;
614 uint64_t maxfilesize; /* XXX */
617 * Disallow multiple mounts of the same device.
618 * Disallow mounting of a device that is currently in use
619 * Flush out any old buffers remaining from a previous use.
621 error = vfs_mountedon(devvp);
624 if (vcount(devvp) > 0)
626 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
627 error = vinvalbuf(devvp, V_SAVE, 0, 0);
632 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
633 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
634 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
639 if (dev->si_iosize_max != 0)
640 mp->mnt_iosize_max = dev->si_iosize_max;
641 if (mp->mnt_iosize_max > MAXPHYS)
642 mp->mnt_iosize_max = MAXPHYS;
645 * Filesystem supports native FSMIDs
647 mp->mnt_kern_flag |= MNTK_FSMID;
650 * The backing device must be VMIO-capable because we use getblk().
651 * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
652 * The VOP_OPEN() call above should have associated a VM object
655 if (devvp->v_object == NULL)
656 panic("ffs_reload: devvp has no VM object!");
658 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD,
659 proc0.p_ucred, NULL) != 0) {
662 size = dpart.media_blksize;
667 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
669 fs = (struct fs *)bp->b_data;
670 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
671 fs->fs_bsize < sizeof(struct fs)) {
672 error = EINVAL; /* XXX needs translation */
676 fs->fs_flags &= ~FS_UNCLEAN;
677 if (fs->fs_clean == 0) {
678 fs->fs_flags |= FS_UNCLEAN;
679 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
681 "WARNING: %s was not properly dismounted\n",
685 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
691 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
692 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
693 error = EROFS; /* needs translation */
696 ump = kmalloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
697 ump->um_malloctype = mtype;
698 ump->um_i_effnlink_valid = 1;
699 ump->um_fs = kmalloc((u_long)fs->fs_sbsize, M_UFSMNT,
701 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
702 if (fs->fs_sbsize < SBSIZE)
703 bp->b_flags |= B_INVAL;
707 fs->fs_ronly = ronly;
708 size = fs->fs_cssize;
709 blks = howmany(size, fs->fs_fsize);
710 if (fs->fs_contigsumsize > 0)
711 size += fs->fs_ncg * sizeof(int32_t);
712 size += fs->fs_ncg * sizeof(uint8_t);
713 space = kmalloc((u_long)size, M_UFSMNT, M_WAITOK);
715 for (i = 0; i < blks; i += fs->fs_frag) {
717 if (i + fs->fs_frag > blks)
718 size = (blks - i) * fs->fs_fsize;
719 if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i),
721 kfree(fs->fs_csp, M_UFSMNT);
724 bcopy(bp->b_data, space, (uint)size);
725 space = (char *)space + size;
729 if (fs->fs_contigsumsize > 0) {
730 fs->fs_maxcluster = lp = space;
731 for (i = 0; i < fs->fs_ncg; i++)
732 *lp++ = fs->fs_contigsumsize;
735 size = fs->fs_ncg * sizeof(uint8_t);
736 fs->fs_contigdirs = (uint8_t *)space;
737 bzero(fs->fs_contigdirs, size);
738 /* Compatibility for old filesystems XXX */
739 if (fs->fs_avgfilesize <= 0) /* XXX */
740 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
741 if (fs->fs_avgfpdir <= 0) /* XXX */
742 fs->fs_avgfpdir = AFPDIR; /* XXX */
743 mp->mnt_data = (qaddr_t)ump;
744 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
745 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
746 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
747 vfs_getvfs(&mp->mnt_stat.f_fsid))
749 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
750 mp->mnt_flag |= MNT_LOCAL;
753 ump->um_devvp = devvp;
754 ump->um_nindir = fs->fs_nindir;
755 ump->um_bptrtodb = fs->fs_fsbtodb;
756 ump->um_seqinc = fs->fs_frag;
757 for (i = 0; i < MAXQUOTAS; i++)
758 ump->um_quotas[i] = NULLVP;
759 dev->si_mountpoint = mp;
762 /* restore "last mounted on" here */
763 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
764 ksnprintf(fs->fs_fsmnt, sizeof(fs->fs_fsmnt),
765 "%s", mp->mnt_stat.f_mntonname);
767 if( mp->mnt_flag & MNT_ROOTFS) {
769 * Root mount; update timestamp in mount structure.
770 * this will be used by the common root mount code
771 * to update the system clock.
773 mp->mnt_time = fs->fs_time;
776 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
777 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
778 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
779 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
780 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
781 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
782 fs->fs_maxfilesize = maxfilesize; /* XXX */
784 if ((fs->fs_flags & FS_DOSOFTDEP) &&
785 (error = softdep_mount(devvp, mp, fs)) != 0) {
786 kfree(fs->fs_csp, M_UFSMNT);
791 (void) ffs_sbupdate(ump, MNT_WAIT);
793 vfs_add_vnodeops(mp, &ffs_vnode_vops, &mp->mnt_vn_norm_ops);
794 vfs_add_vnodeops(mp, &ffs_spec_vops, &mp->mnt_vn_spec_ops);
795 vfs_add_vnodeops(mp, &ffs_fifo_vops, &mp->mnt_vn_fifo_ops);
799 dev->si_mountpoint = NULL;
802 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
804 kfree(ump->um_fs, M_UFSMNT);
805 kfree(ump, M_UFSMNT);
806 mp->mnt_data = (qaddr_t)0;
812 * Sanity checks for old filesystems.
814 * XXX - goes away some day.
817 ffs_oldfscompat(struct fs *fs)
819 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
820 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
821 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
822 fs->fs_nrpos = 8; /* XXX */
823 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
826 uint64_t sizepb = fs->fs_bsize; /* XXX */
828 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
829 for (i = 0; i < NIADDR; i++) { /* XXX */
830 sizepb *= NINDIR(fs); /* XXX */
831 fs->fs_maxfilesize += sizepb; /* XXX */
834 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
835 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
836 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
842 * unmount system call
845 ffs_unmount(struct mount *mp, int mntflags)
847 struct ufsmount *ump;
852 if (mntflags & MNT_FORCE) {
855 if (mp->mnt_flag & MNT_SOFTDEP) {
856 if ((error = softdep_flushfiles(mp, flags)) != 0)
859 if ((error = ffs_flushfiles(mp, flags)) != 0)
864 if (fs->fs_ronly == 0) {
865 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
866 error = ffs_sbupdate(ump, MNT_WAIT);
872 ump->um_devvp->v_rdev->si_mountpoint = NULL;
874 vinvalbuf(ump->um_devvp, V_SAVE, 0, 0);
875 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE);
877 vrele(ump->um_devvp);
879 kfree(fs->fs_csp, M_UFSMNT);
881 kfree(ump, M_UFSMNT);
882 mp->mnt_data = (qaddr_t)0;
883 mp->mnt_flag &= ~MNT_LOCAL;
888 * Flush out all the files in a filesystem.
891 ffs_flushfiles(struct mount *mp, int flags)
893 struct ufsmount *ump;
898 if (mp->mnt_flag & MNT_QUOTA) {
900 error = vflush(mp, 0, SKIPSYSTEM|flags);
903 /* Find out how many quota files we have open. */
904 for (i = 0; i < MAXQUOTAS; i++) {
905 if (ump->um_quotas[i] == NULLVP)
910 * Here we fall through to vflush again to ensure
911 * that we have gotten rid of all the system vnodes.
916 * Flush all the files.
918 if ((error = vflush(mp, 0, flags)) != 0)
921 * Flush filesystem metadata.
923 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
924 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, 0);
925 vn_unlock(ump->um_devvp);
930 * Get filesystem statistics.
933 ffs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
935 struct ufsmount *ump;
940 if (fs->fs_magic != FS_MAGIC)
942 sbp->f_bsize = fs->fs_fsize;
943 sbp->f_iosize = fs->fs_bsize;
944 sbp->f_blocks = fs->fs_dsize;
945 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
946 fs->fs_cstotal.cs_nffree;
947 sbp->f_bavail = freespace(fs, fs->fs_minfree);
948 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
949 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
950 if (sbp != &mp->mnt_stat) {
951 sbp->f_type = mp->mnt_vfc->vfc_typenum;
952 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
953 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
959 * Go through the disk queues to initiate sandbagged IO;
960 * go through the inodes to write those that have been modified;
961 * initiate the writing of the super block if it has been modified.
963 * Note: we are always called with the filesystem marked `MPBUSY'.
967 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
968 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
971 ffs_sync(struct mount *mp, int waitfor)
973 struct ufsmount *ump = VFSTOUFS(mp);
976 struct scaninfo scaninfo;
979 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
980 kprintf("fs = %s\n", fs->fs_fsmnt);
981 panic("ffs_sync: rofs mod");
985 * Write back each (modified) inode.
987 scaninfo.allerror = 0;
989 scaninfo.waitfor = waitfor;
990 while (scaninfo.rescan) {
992 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
993 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
997 * Force stale filesystem control information to be flushed.
999 if ((waitfor & MNT_LAZY) == 0) {
1000 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1001 waitfor = MNT_NOWAIT;
1002 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1003 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
1004 scaninfo.allerror = error;
1005 vn_unlock(ump->um_devvp);
1011 * Write back modified superblock.
1013 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1014 scaninfo.allerror = error;
1015 return (scaninfo.allerror);
1019 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1024 * Depend on the mount list's vnode lock to keep things stable
1025 * enough for a quick test. Since there might be hundreds of
1026 * thousands of vnodes, we cannot afford even a subroutine
1027 * call unless there's a good chance that we have work to do.
1030 /* Restart out whole search if this guy is locked
1031 * or is being reclaimed.
1033 if (vp->v_type == VNON || ((ip->i_flag &
1034 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1035 RB_EMPTY(&vp->v_rbdirty_tree))) {
1042 ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1044 struct scaninfo *info = data;
1049 * We have to recheck after having obtained the vnode interlock.
1052 if (vp->v_type == VNON || vp->v_type == VBAD ||
1054 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1055 RB_EMPTY(&vp->v_rbdirty_tree))) {
1058 if (vp->v_type != VCHR) {
1059 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
1060 info->allerror = error;
1063 * We must reference the vp to prevent it from
1064 * getting ripped out from under ffs_update, since
1065 * we are not holding a vnode lock.
1067 /* ffs_update(vp, waitfor == MNT_WAIT); */
1074 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1075 * in from disk. If it is in core, wait for the lock bit to clear, then
1076 * return the inode locked. Detection and handling of mount points must be
1077 * done by the calling routine.
1081 ffs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
1085 struct ufsmount *ump;
1094 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1099 * If this MALLOC() is performed after the getnewvnode()
1100 * it might block, leaving a vnode with a NULL v_data to be
1101 * found by ffs_sync() if a sync happens to fire right then,
1102 * which will cause a panic because ffs_sync() blindly
1103 * dereferences vp->v_data (as well it should).
1105 * XXX this may no longer be true since getnewvnode returns a
1106 * VX locked vnode now.
1108 MALLOC(ip, struct inode *, sizeof(struct inode),
1109 ump->um_malloctype, M_WAITOK);
1111 /* Allocate a new vnode/inode. */
1112 error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
1115 kfree(ip, ump->um_malloctype);
1118 bzero((caddr_t)ip, sizeof(struct inode));
1120 ip->i_fs = fs = ump->um_fs;
1126 for (i = 0; i < MAXQUOTAS; i++)
1127 ip->i_dquot[i] = NODQUOT;
1132 * Insert it into the inode hash table and check for a collision.
1133 * If a collision occurs, throw away the vnode and try again.
1135 if (ufs_ihashins(ip) != 0) {
1136 kprintf("debug: ufs ihashins collision, retrying inode %ld\n",
1137 (long)ip->i_number);
1140 kfree(ip, ump->um_malloctype);
1145 /* Read in the disk contents for the inode, copy into the inode. */
1146 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1147 (int)fs->fs_bsize, &bp);
1150 * The inode does not contain anything useful, so it would
1151 * be misleading to leave it on its hash chain. With mode
1152 * still zero, it will be unlinked and returned to the free
1161 ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1162 if (DOINGSOFTDEP(vp))
1163 softdep_load_inodeblock(ip);
1165 ip->i_effnlink = ip->i_nlink;
1169 * Initialize the vnode from the inode, check for aliases.
1170 * Note that the underlying vnode may have changed.
1172 error = ufs_vinit(mp, &vp);
1180 * Finish inode initialization now that aliasing has been resolved.
1182 ip->i_devvp = ump->um_devvp;
1185 * Set up a generation number for this inode if it does not
1186 * already have one. This should only happen on old filesystems.
1188 if (ip->i_gen == 0) {
1189 ip->i_gen = krandom() / 2 + 1;
1190 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1191 ip->i_flag |= IN_MODIFIED;
1194 * Ensure that uid and gid are correct. This is a temporary
1195 * fix until fsck has been changed to do the update.
1197 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1198 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1199 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1203 * return a VX locked and refd vnode (VX == same as normal vget()
1204 * vnode so we are ok)
1211 * File handle to vnode
1213 * Have to be really careful about stale file handles:
1214 * - check that the inode number is valid
1215 * - call ffs_vget() to get the locked inode
1216 * - check for an unallocated inode (i_mode == 0)
1217 * - check that the given client host has export rights and return
1218 * those rights via. exflagsp and credanonp
1221 ffs_fhtovp(struct mount *mp, struct vnode *rootvp,
1222 struct fid *fhp, struct vnode **vpp)
1227 ufhp = (struct ufid *)fhp;
1228 fs = VFSTOUFS(mp)->um_fs;
1229 if (ufhp->ufid_ino < ROOTINO ||
1230 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1232 return (ufs_fhtovp(mp, rootvp, ufhp, vpp));
1236 * Vnode pointer to File handle
1240 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1246 ufhp = (struct ufid *)fhp;
1247 ufhp->ufid_len = sizeof(struct ufid);
1248 ufhp->ufid_ino = ip->i_number;
1249 ufhp->ufid_gen = ip->i_gen;
1254 * Initialize the filesystem; just use ufs_init.
1257 ffs_init(struct vfsconf *vfsp)
1259 softdep_initialize();
1260 kmalloc_raise_limit(M_FFSNODE, 0);
1261 return (ufs_init(vfsp));
1265 * Write a superblock and associated information back to disk.
1268 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1270 struct fs *dfs, *fs = mp->um_fs;
1274 int i, size, error, allerror = 0;
1277 * First write back the summary information.
1279 * NOTE: the getblk is relative to the device vnode so bio1
1280 * contains the device block number.
1282 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1284 for (i = 0; i < blks; i += fs->fs_frag) {
1285 size = fs->fs_bsize;
1286 if (i + fs->fs_frag > blks)
1287 size = (blks - i) * fs->fs_fsize;
1288 bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i),
1290 bcopy(space, bp->b_data, (uint)size);
1291 space = (char *)space + size;
1292 if (waitfor != MNT_WAIT)
1294 else if ((error = bwrite(bp)) != 0)
1298 * Now write back the superblock itself. If any errors occurred
1299 * up to this point, then fail so that the superblock avoids
1300 * being written out as clean.
1304 bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0);
1306 fs->fs_time = time_second;
1307 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1308 /* Restore compatibility to old filesystems. XXX */
1309 dfs = (struct fs *)bp->b_data; /* XXX */
1310 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1311 dfs->fs_nrpos = -1; /* XXX */
1312 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1313 int32_t *lp, tmp; /* XXX */
1315 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1316 tmp = lp[4]; /* XXX */
1317 for (i = 4; i > 0; i--) /* XXX */
1318 lp[i] = lp[i-1]; /* XXX */
1319 lp[0] = tmp; /* XXX */
1321 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1322 if (waitfor != MNT_WAIT)
1324 else if ((error = bwrite(bp)) != 0)