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.20 2004/08/13 17:51:13 dillon Exp $
38 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
49 #include <sys/fcntl.h>
50 #include <sys/disklabel.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>
65 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
67 static int ffs_sbupdate (struct ufsmount *, int);
68 static int ffs_reload (struct mount *,struct ucred *,struct thread *);
69 static int ffs_oldfscompat (struct fs *);
70 static int ffs_mount (struct mount *, char *, caddr_t,
71 struct nameidata *, struct thread *);
72 static int ffs_init (struct vfsconf *);
74 static struct vfsops ufs_vfsops = {
91 VFS_SET(ufs_vfsops, ufs, 0);
96 * Called when mounting local physical media
100 * mp mount point structure
101 * path NULL (flag for root mount!!!)
104 * p process (user credentials check [statfs])
107 * mp mount point structure
108 * path path to mount point
109 * data pointer to argument struct in user space
110 * ndp mount point namei() return (used for
111 * credentials on reload), reused to look
113 * p process (user credentials check)
116 * !0 error number (errno.h)
121 * mount point is locked
123 * mount point is locked
126 * A NULL path can be used for a flag since the mount
127 * system call will fail with EFAULT in copyinstr in
128 * namei() if it is a genuine NULL from the user.
131 ffs_mount(struct mount *mp, /* mount struct pointer */
132 char *path, /* path to mount point */
133 caddr_t data, /* arguments to FS specific mount */
134 struct nameidata *ndp, /* mount point credentials */
135 struct thread *td) /* process requesting mount */
141 struct ufs_args args;
142 struct ufsmount *ump = 0;
144 int error, flags, ronly = 0;
148 KKASSERT(td->td_proc);
149 cred = td->td_proc->p_ucred;
152 * Use NULL path to flag a root mount
157 * Mounting root filesystem
161 if ((err = bdevvp(rootdev, &rootvp))) {
162 printf("ffs_mountroot: can't find rootvp\n");
166 if( ( err = ffs_mountfs(rootvp, mp, td, M_FFSNODE)) != 0) {
167 /* fs specific cleanup (if any)*/
171 goto dostatfs; /* success*/
177 * Mounting non-root filesystem or updating a filesystem
181 /* copy in user arguments*/
182 err = 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, td);
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 err = softdep_flushfiles(mp, flags, td);
211 err = ffs_flushfiles(mp, flags, td);
215 if (!err && (mp->mnt_flag & MNT_RELOAD))
216 err = ffs_reload(mp, ndp->ni_cnd.cn_cred, td);
220 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
222 * If upgrade to read-write by non-root, then verify
223 * that user has necessary permissions on the device.
225 if (cred->cr_uid != 0) {
226 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
227 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
229 VOP_UNLOCK(devvp, NULL, 0, td);
232 VOP_UNLOCK(devvp, NULL, 0, td);
235 fs->fs_flags &= ~FS_UNCLEAN;
236 if (fs->fs_clean == 0) {
237 fs->fs_flags |= FS_UNCLEAN;
238 if (mp->mnt_flag & MNT_FORCE) {
240 "WARNING: %s was not properly dismounted\n",
244 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
251 /* check to see if we need to start softdep */
252 if (fs->fs_flags & FS_DOSOFTDEP) {
253 err = 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 err = 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.
285 NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
288 /* can't get devvp!*/
292 NDFREE(ndp, NDF_ONLY_PNBUF);
295 if (!vn_isdisk(devvp, &err))
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, NULL, LK_EXCLUSIVE | LK_RETRY, td);
307 if ((error = VOP_ACCESS(devvp, accessmode, cred, td)) != 0) {
311 VOP_UNLOCK(devvp, NULL, 0, td);
314 if (mp->mnt_flag & MNT_UPDATE) {
318 * If it's not the same vnode, or at least the same device
319 * then it's not correct. NOTE: devvp->v_rdev may be NULL
320 * since we haven't opened it, so we compare udev instead.
323 if (devvp != ump->um_devvp) {
324 if (devvp->v_udev == ump->um_devvp->v_udev) {
327 printf("cannot update mount, udev does"
328 " not match %08x vs %08x\n",
329 devvp->v_udev, ump->um_devvp->v_udev);
330 err = EINVAL; /* needs translation */
336 * Update device name only on success
339 /* Save "mounted from" info for mount point (NULL pad)*/
340 copyinstr( args.fspec,
341 mp->mnt_stat.f_mntfromname,
344 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
354 * Since this is a new mount, we want the names for
355 * the device and the mount point copied in. If an
356 * error occurs, the mountpoint is discarded by the
359 /* Save "last mounted on" info for mount point (NULL pad)*/
360 copyinstr( path, /* mount point*/
361 mp->mnt_stat.f_mntonname, /* save area*/
362 MNAMELEN - 1, /* max size*/
363 &size); /* real size*/
364 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
366 /* Save "mounted from" info for mount point (NULL pad)*/
367 copyinstr( args.fspec, /* device name*/
368 mp->mnt_stat.f_mntfromname, /* save area*/
369 MNAMELEN - 1, /* max size*/
370 &size); /* real size*/
371 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
373 err = ffs_mountfs(devvp, mp, td, M_FFSNODE);
381 * Initialize FS stat information in mount struct; uses both
382 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
384 * This code is common to root and non-root mounts
386 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
391 error_2: /* error with devvp held*/
393 /* release devvp before failing*/
396 error_1: /* no state to back out*/
399 if (!err && path && (mp->mnt_flag & MNT_UPDATE)) {
400 /* Update clean flag after changing read-onlyness. */
402 if (ronly != fs->fs_ronly) {
403 fs->fs_ronly = ronly;
404 fs->fs_clean = ronly &&
405 (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
406 ffs_sbupdate(ump, MNT_WAIT);
413 * Reload all incore data for a filesystem (used after running fsck on
414 * the root filesystem and finding things to fix). The filesystem must
415 * be mounted read-only.
417 * Things to do to update the mount:
418 * 1) invalidate all cached meta-data.
419 * 2) re-read superblock from disk.
420 * 3) re-read summary information from disk.
421 * 4) invalidate all inactive vnodes.
422 * 5) invalidate all cached file data.
423 * 6) re-read inode data for all active vnodes.
426 static int ffs_reload_scan1(struct mount *mp, struct vnode *vp, void *data);
427 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp,
428 lwkt_tokref_t vlock, void *data);
440 ffs_reload(struct mount *mp, struct ucred *cred, struct thread *td)
445 struct fs *fs, *newfs;
446 struct partinfo dpart;
448 int i, blks, size, error;
450 struct scaninfo scaninfo;
453 if ((mp->mnt_flag & MNT_RDONLY) == 0)
456 * Step 1: invalidate all cached meta-data.
458 devvp = VFSTOUFS(mp)->um_devvp;
459 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
460 error = vinvalbuf(devvp, 0, td, 0, 0);
461 VOP_UNLOCK(devvp, NULL, 0, td);
463 panic("ffs_reload: dirty1");
467 * Only VMIO the backing device if the backing device is a real
468 * block device. See ffs_mountmfs() for more details.
470 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
471 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
472 vfs_object_create(devvp, td);
473 lwkt_gettoken(&vlock, devvp->v_interlock);
474 VOP_UNLOCK(devvp, &vlock, LK_INTERLOCK, td);
478 * Step 2: re-read superblock from disk.
480 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, td) != 0)
483 size = dpart.disklab->d_secsize;
484 if ((error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, &bp)) != 0)
486 newfs = (struct fs *)bp->b_data;
487 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
488 newfs->fs_bsize < sizeof(struct fs)) {
490 return (EIO); /* XXX needs translation */
492 fs = VFSTOUFS(mp)->um_fs;
494 * Copy pointer fields back into superblock before copying in XXX
495 * new superblock. These should really be in the ufsmount. XXX
496 * Note that important parameters (eg fs_ncg) are unchanged.
498 newfs->fs_csp = fs->fs_csp;
499 newfs->fs_maxcluster = fs->fs_maxcluster;
500 newfs->fs_contigdirs = fs->fs_contigdirs;
501 /* The filesystem is still read-only. */
503 bcopy(newfs, fs, (uint)fs->fs_sbsize);
504 if (fs->fs_sbsize < SBSIZE)
505 bp->b_flags |= B_INVAL;
507 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
509 /* An old fsck may have zeroed these fields, so recheck them. */
510 if (fs->fs_avgfilesize <= 0) /* XXX */
511 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
512 if (fs->fs_avgfpdir <= 0) /* XXX */
513 fs->fs_avgfpdir = AFPDIR; /* XXX */
516 * Step 3: re-read summary information from disk.
518 blks = howmany(fs->fs_cssize, fs->fs_fsize);
520 for (i = 0; i < blks; i += fs->fs_frag) {
522 if (i + fs->fs_frag > blks)
523 size = (blks - i) * fs->fs_fsize;
524 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
527 bcopy(bp->b_data, space, (uint)size);
528 space = (char *)space + size;
532 * We no longer know anything about clusters per cylinder group.
534 if (fs->fs_contigsumsize > 0) {
535 lp = fs->fs_maxcluster;
536 for (i = 0; i < fs->fs_ncg; i++)
537 *lp++ = fs->fs_contigsumsize;
542 scaninfo.devvp = devvp;
544 while (error == 0 && scaninfo.rescan) {
546 error = vmntvnodescan(mp, ffs_reload_scan1,
547 ffs_reload_scan2, &scaninfo);
553 ffs_reload_scan1(struct mount *mp, struct vnode *vp, void *data)
555 struct scaninfo *info = data;
558 * Step 4: invalidate all inactive vnodes.
560 if (vrecycle(vp, NULL, info->td)) {
562 return(-1); /* continue loop, do not call scan2 */
568 ffs_reload_scan2(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
570 struct scaninfo *info = data;
576 * Step 5: invalidate all cached file data.
578 if (vget(vp, vlock, LK_EXCLUSIVE | LK_INTERLOCK, info->td)) {
582 if (vinvalbuf(vp, 0, info->td, 0, 0))
583 panic("ffs_reload: dirty2");
585 * Step 6: re-read inode data for all active vnodes.
588 error = bread(info->devvp,
589 fsbtodb(info->fs, ino_to_fsba(info->fs, ip->i_number)),
590 (int)info->fs->fs_bsize, &bp);
595 ip->i_din = *((struct dinode *)bp->b_data +
596 ino_to_fsbo(info->fs, ip->i_number));
597 ip->i_effnlink = ip->i_nlink;
604 * Common code for mount and mountroot
607 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
608 struct malloc_type *malloctype)
610 struct ufsmount *ump;
614 struct partinfo dpart;
616 int error, i, blks, size, ronly;
619 uint64_t maxfilesize; /* XXX */
623 * Disallow multiple mounts of the same device.
624 * Disallow mounting of a device that is currently in use
625 * (except for root, which might share swap device for miniroot).
626 * Flush out any old buffers remaining from a previous use.
628 error = vfs_mountedon(devvp);
631 if (count_udev(devvp->v_udev) > 0 && devvp != rootvp)
633 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
634 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
635 VOP_UNLOCK(devvp, NULL, 0, td);
640 * Only VMIO the backing device if the backing device is a real
641 * block device. This excludes the original MFS implementation.
642 * Note that it is optional that the backing device be VMIOed. This
643 * increases the opportunity for metadata caching.
645 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
646 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
647 vfs_object_create(devvp, td);
648 lwkt_gettoken(&vlock, devvp->v_interlock);
649 VOP_UNLOCK(devvp, &vlock, LK_INTERLOCK, td);
652 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
653 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
654 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
655 VOP_UNLOCK(devvp, NULL, 0, td);
659 if (dev->si_iosize_max != 0)
660 mp->mnt_iosize_max = dev->si_iosize_max;
661 if (mp->mnt_iosize_max > MAXPHYS)
662 mp->mnt_iosize_max = MAXPHYS;
664 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred, td) != 0)
667 size = dpart.disklab->d_secsize;
671 if ((error = bread(devvp, SBLOCK, SBSIZE, &bp)) != 0)
673 fs = (struct fs *)bp->b_data;
674 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
675 fs->fs_bsize < sizeof(struct fs)) {
676 error = EINVAL; /* XXX needs translation */
680 fs->fs_flags &= ~FS_UNCLEAN;
681 if (fs->fs_clean == 0) {
682 fs->fs_flags |= FS_UNCLEAN;
683 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
685 "WARNING: %s was not properly dismounted\n",
689 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
695 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
696 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
697 error = EROFS; /* needs translation */
700 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
701 bzero((caddr_t)ump, sizeof *ump);
702 ump->um_malloctype = malloctype;
703 ump->um_i_effnlink_valid = 1;
704 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
706 ump->um_blkatoff = ffs_blkatoff;
707 ump->um_truncate = ffs_truncate;
708 ump->um_update = ffs_update;
709 ump->um_valloc = ffs_valloc;
710 ump->um_vfree = ffs_vfree;
711 bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
712 if (fs->fs_sbsize < SBSIZE)
713 bp->b_flags |= B_INVAL;
717 fs->fs_ronly = ronly;
718 size = fs->fs_cssize;
719 blks = howmany(size, fs->fs_fsize);
720 if (fs->fs_contigsumsize > 0)
721 size += fs->fs_ncg * sizeof(int32_t);
722 size += fs->fs_ncg * sizeof(uint8_t);
723 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
725 for (i = 0; i < blks; i += fs->fs_frag) {
727 if (i + fs->fs_frag > blks)
728 size = (blks - i) * fs->fs_fsize;
729 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
731 free(fs->fs_csp, M_UFSMNT);
734 bcopy(bp->b_data, space, (uint)size);
735 space = (char *)space + size;
739 if (fs->fs_contigsumsize > 0) {
740 fs->fs_maxcluster = lp = space;
741 for (i = 0; i < fs->fs_ncg; i++)
742 *lp++ = fs->fs_contigsumsize;
745 size = fs->fs_ncg * sizeof(uint8_t);
746 fs->fs_contigdirs = (uint8_t *)space;
747 bzero(fs->fs_contigdirs, size);
748 /* Compatibility for old filesystems XXX */
749 if (fs->fs_avgfilesize <= 0) /* XXX */
750 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
751 if (fs->fs_avgfpdir <= 0) /* XXX */
752 fs->fs_avgfpdir = AFPDIR; /* XXX */
753 mp->mnt_data = (qaddr_t)ump;
754 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
755 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
756 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
757 vfs_getvfs(&mp->mnt_stat.f_fsid))
759 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
760 mp->mnt_flag |= MNT_LOCAL;
763 ump->um_devvp = devvp;
764 ump->um_nindir = fs->fs_nindir;
765 ump->um_bptrtodb = fs->fs_fsbtodb;
766 ump->um_seqinc = fs->fs_frag;
767 for (i = 0; i < MAXQUOTAS; i++)
768 ump->um_quotas[i] = NULLVP;
769 dev->si_mountpoint = mp;
773 * Set FS local "last mounted on" information (NULL pad)
775 copystr( mp->mnt_stat.f_mntonname, /* mount point*/
776 fs->fs_fsmnt, /* copy area*/
777 sizeof(fs->fs_fsmnt) - 1, /* max size*/
778 &strsize); /* real size*/
779 bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize);
781 if( mp->mnt_flag & MNT_ROOTFS) {
783 * Root mount; update timestamp in mount structure.
784 * this will be used by the common root mount code
785 * to update the system clock.
787 mp->mnt_time = fs->fs_time;
790 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
791 maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
792 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
793 if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
794 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
795 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
796 fs->fs_maxfilesize = maxfilesize; /* XXX */
798 if ((fs->fs_flags & FS_DOSOFTDEP) &&
799 (error = softdep_mount(devvp, mp, fs)) != 0) {
800 free(fs->fs_csp, M_UFSMNT);
805 (void) ffs_sbupdate(ump, MNT_WAIT);
809 dev->si_mountpoint = NULL;
812 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
814 free(ump->um_fs, M_UFSMNT);
816 mp->mnt_data = (qaddr_t)0;
822 * Sanity checks for old filesystems.
824 * XXX - goes away some day.
827 ffs_oldfscompat(struct fs *fs)
829 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
830 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
831 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
832 fs->fs_nrpos = 8; /* XXX */
833 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
836 uint64_t sizepb = fs->fs_bsize; /* XXX */
838 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
839 for (i = 0; i < NIADDR; i++) { /* XXX */
840 sizepb *= NINDIR(fs); /* XXX */
841 fs->fs_maxfilesize += sizepb; /* XXX */
844 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
845 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
846 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
852 * unmount system call
855 ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
857 struct ufsmount *ump;
862 if (mntflags & MNT_FORCE) {
865 if (mp->mnt_flag & MNT_SOFTDEP) {
866 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
869 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
874 if (fs->fs_ronly == 0) {
875 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
876 error = ffs_sbupdate(ump, MNT_WAIT);
882 ump->um_devvp->v_rdev->si_mountpoint = NULL;
884 vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
885 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, td);
887 vrele(ump->um_devvp);
889 free(fs->fs_csp, M_UFSMNT);
892 mp->mnt_data = (qaddr_t)0;
893 mp->mnt_flag &= ~MNT_LOCAL;
898 * Flush out all the files in a filesystem.
901 ffs_flushfiles(struct mount *mp, int flags, struct thread *td)
903 struct ufsmount *ump;
908 if (mp->mnt_flag & MNT_QUOTA) {
910 error = vflush(mp, 0, SKIPSYSTEM|flags);
913 for (i = 0; i < MAXQUOTAS; i++) {
914 if (ump->um_quotas[i] == NULLVP)
919 * Here we fall through to vflush again to ensure
920 * that we have gotten rid of all the system vnodes.
925 * Flush all the files.
927 if ((error = vflush(mp, 0, flags)) != 0)
930 * Flush filesystem metadata.
932 vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
933 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
934 VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
939 * Get filesystem statistics.
942 ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
944 struct ufsmount *ump;
949 if (fs->fs_magic != FS_MAGIC)
951 sbp->f_bsize = fs->fs_fsize;
952 sbp->f_iosize = fs->fs_bsize;
953 sbp->f_blocks = fs->fs_dsize;
954 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
955 fs->fs_cstotal.cs_nffree;
956 sbp->f_bavail = freespace(fs, fs->fs_minfree);
957 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
958 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
959 if (sbp != &mp->mnt_stat) {
960 sbp->f_type = mp->mnt_vfc->vfc_typenum;
961 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
962 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
963 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
964 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
970 * Go through the disk queues to initiate sandbagged IO;
971 * go through the inodes to write those that have been modified;
972 * initiate the writing of the super block if it has been modified.
974 * Note: we are always called with the filesystem marked `MPBUSY'.
978 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
979 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp,
980 lwkt_tokref_t vlock, void *data);
983 ffs_sync(struct mount *mp, int waitfor, struct thread *td)
985 struct ufsmount *ump = VFSTOUFS(mp);
988 struct scaninfo scaninfo;
991 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
992 printf("fs = %s\n", fs->fs_fsmnt);
993 panic("ffs_sync: rofs mod");
997 * Write back each (modified) inode.
999 scaninfo.allerror = 0;
1000 scaninfo.rescan = 1;
1001 scaninfo.waitfor = waitfor;
1002 while (scaninfo.rescan) {
1003 scaninfo.rescan = 0;
1004 vmntvnodescan(mp, ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
1008 * Force stale filesystem control information to be flushed.
1010 if (waitfor != MNT_LAZY) {
1011 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1012 waitfor = MNT_NOWAIT;
1013 vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1014 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
1015 scaninfo.allerror = error;
1016 VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
1022 * Write back modified superblock.
1024 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1025 scaninfo.allerror = error;
1026 return (scaninfo.allerror);
1030 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1035 * Depend on the mount list's vnode lock to keep things stable
1036 * enough for a quick test. Since there might be hundreds of
1037 * thousands of vnodes, we cannot afford even a subroutine
1038 * call unless there's a good chance that we have work to do.
1041 if (vp->v_type == VNON || ((ip->i_flag &
1042 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1043 TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1050 ffs_sync_scan2(struct mount *mp, struct vnode *vp,
1051 lwkt_tokref_t vlock, void *data)
1053 struct scaninfo *info = data;
1054 thread_t td = curthread; /* XXX */
1059 * We have to recheck after having obtained the vnode interlock.
1062 if (vp->v_type == VNON || ((ip->i_flag &
1063 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1064 TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1065 lwkt_reltoken(vlock);
1068 if (vp->v_type != VCHR) {
1069 error = vget(vp, vlock, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT, td);
1071 if (error == ENOENT)
1074 if ((error = VOP_FSYNC(vp, info->waitfor, td)) != 0)
1075 info->allerror = error;
1076 VOP_UNLOCK(vp, NULL, 0, td);
1081 * We must reference the vp to prevent it from
1082 * getting ripped out from under UFS_UPDATE, since
1083 * we are not holding a vnode lock.
1086 lwkt_reltoken(vlock);
1087 /* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1095 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1096 * in from disk. If it is in core, wait for the lock bit to clear, then
1097 * return the inode locked. Detection and handling of mount points must be
1098 * done by the calling routine.
1100 static int ffs_inode_hash_lock;
1103 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1107 struct ufsmount *ump;
1116 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1121 * Lock out the creation of new entries in the FFS hash table in
1122 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1125 if (ffs_inode_hash_lock) {
1126 while (ffs_inode_hash_lock) {
1127 ffs_inode_hash_lock = -1;
1128 tsleep(&ffs_inode_hash_lock, 0, "ffsvgt", 0);
1132 ffs_inode_hash_lock = 1;
1135 * If this MALLOC() is performed after the getnewvnode()
1136 * it might block, leaving a vnode with a NULL v_data to be
1137 * found by ffs_sync() if a sync happens to fire right then,
1138 * which will cause a panic because ffs_sync() blindly
1139 * dereferences vp->v_data (as well it should).
1141 MALLOC(ip, struct inode *, sizeof(struct inode),
1142 ump->um_malloctype, M_WAITOK);
1144 /* Allocate a new vnode/inode. */
1145 error = getnewvnode(VT_UFS, mp, ffs_vnode_vops, &vp);
1147 if (ffs_inode_hash_lock < 0)
1148 wakeup(&ffs_inode_hash_lock);
1149 ffs_inode_hash_lock = 0;
1151 FREE(ip, ump->um_malloctype);
1154 bzero((caddr_t)ip, sizeof(struct inode));
1155 lockinit(&ip->i_lock, 0, "inode", VLKTIMEOUT, LK_CANRECURSE);
1158 * FFS supports lock sharing in the stack of vnodes
1160 vp->v_vnlock = &ip->i_lock;
1162 ip->i_fs = fs = ump->um_fs;
1168 for (i = 0; i < MAXQUOTAS; i++)
1169 ip->i_dquot[i] = NODQUOT;
1173 * Put it onto its hash chain and lock it so that other requests for
1174 * this inode will block if they arrive while we are sleeping waiting
1175 * for old data structures to be purged or for the contents of the
1176 * disk portion of this inode to be read.
1180 if (ffs_inode_hash_lock < 0)
1181 wakeup(&ffs_inode_hash_lock);
1182 ffs_inode_hash_lock = 0;
1184 /* Read in the disk contents for the inode, copy into the inode. */
1185 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1186 (int)fs->fs_bsize, &bp);
1189 * The inode does not contain anything useful, so it would
1190 * be misleading to leave it on its hash chain. With mode
1191 * still zero, it will be unlinked and returned to the free
1199 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1200 if (DOINGSOFTDEP(vp))
1201 softdep_load_inodeblock(ip);
1203 ip->i_effnlink = ip->i_nlink;
1207 * Initialize the vnode from the inode, check for aliases.
1208 * Note that the underlying vnode may have changed.
1210 error = ufs_vinit(mp, ffs_spec_vops, ffs_fifo_vops, &vp);
1217 * Finish inode initialization now that aliasing has been resolved.
1219 ip->i_devvp = ump->um_devvp;
1222 * Set up a generation number for this inode if it does not
1223 * already have one. This should only happen on old filesystems.
1225 if (ip->i_gen == 0) {
1226 ip->i_gen = random() / 2 + 1;
1227 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1228 ip->i_flag |= IN_MODIFIED;
1231 * Ensure that uid and gid are correct. This is a temporary
1232 * fix until fsck has been changed to do the update.
1234 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1235 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1236 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1244 * File handle to vnode
1246 * Have to be really careful about stale file handles:
1247 * - check that the inode number is valid
1248 * - call ffs_vget() to get the locked inode
1249 * - check for an unallocated inode (i_mode == 0)
1250 * - check that the given client host has export rights and return
1251 * those rights via. exflagsp and credanonp
1254 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1259 ufhp = (struct ufid *)fhp;
1260 fs = VFSTOUFS(mp)->um_fs;
1261 if (ufhp->ufid_ino < ROOTINO ||
1262 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1264 return (ufs_fhtovp(mp, ufhp, vpp));
1268 * Vnode pointer to File handle
1272 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1278 ufhp = (struct ufid *)fhp;
1279 ufhp->ufid_len = sizeof(struct ufid);
1280 ufhp->ufid_ino = ip->i_number;
1281 ufhp->ufid_gen = ip->i_gen;
1286 * Initialize the filesystem; just use ufs_init.
1289 ffs_init(struct vfsconf *vfsp)
1291 softdep_initialize();
1292 return (ufs_init(vfsp));
1296 * Write a superblock and associated information back to disk.
1299 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1301 struct fs *dfs, *fs = mp->um_fs;
1305 int i, size, error, allerror = 0;
1308 * First write back the summary information.
1310 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1312 for (i = 0; i < blks; i += fs->fs_frag) {
1313 size = fs->fs_bsize;
1314 if (i + fs->fs_frag > blks)
1315 size = (blks - i) * fs->fs_fsize;
1316 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1318 bcopy(space, bp->b_data, (uint)size);
1319 space = (char *)space + size;
1320 if (waitfor != MNT_WAIT)
1322 else if ((error = bwrite(bp)) != 0)
1326 * Now write back the superblock itself. If any errors occurred
1327 * up to this point, then fail so that the superblock avoids
1328 * being written out as clean.
1332 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1334 fs->fs_time = time_second;
1335 bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1336 /* Restore compatibility to old filesystems. XXX */
1337 dfs = (struct fs *)bp->b_data; /* XXX */
1338 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1339 dfs->fs_nrpos = -1; /* XXX */
1340 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1341 int32_t *lp, tmp; /* XXX */
1343 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1344 tmp = lp[4]; /* XXX */
1345 for (i = 4; i > 0; i--) /* XXX */
1346 lp[i] = lp[i-1]; /* XXX */
1347 lp[0] = tmp; /* XXX */
1349 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1350 if (waitfor != MNT_WAIT)
1352 else if ((error = bwrite(bp)) != 0)