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.18 2004/05/19 22:53:06 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 file system
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 file system or updating a file system
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 file system is still read-only. */
503 bcopy(newfs, fs, (u_int)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, (u_int)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);
554 ffs_reload_scan1(struct mount *mp, struct vnode *vp, void *data)
556 struct scaninfo *info = data;
559 * Step 4: invalidate all inactive vnodes.
561 if (vrecycle(vp, NULL, info->td)) {
563 return(-1); /* continue loop, do not call scan2 */
570 ffs_reload_scan2(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
572 struct scaninfo *info = data;
578 * Step 5: invalidate all cached file data.
580 if (vget(vp, vlock, LK_EXCLUSIVE | LK_INTERLOCK, info->td)) {
584 if (vinvalbuf(vp, 0, info->td, 0, 0))
585 panic("ffs_reload: dirty2");
587 * Step 6: re-read inode data for all active vnodes.
590 error = bread(info->devvp,
591 fsbtodb(info->fs, ino_to_fsba(info->fs, ip->i_number)),
592 (int)info->fs->fs_bsize, &bp);
597 ip->i_din = *((struct dinode *)bp->b_data +
598 ino_to_fsbo(info->fs, ip->i_number));
599 ip->i_effnlink = ip->i_nlink;
606 * Common code for mount and mountroot
609 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
610 struct malloc_type *malloctype)
612 struct ufsmount *ump;
616 struct partinfo dpart;
618 int error, i, blks, size, ronly;
621 u_int64_t maxfilesize; /* XXX */
625 * Disallow multiple mounts of the same device.
626 * Disallow mounting of a device that is currently in use
627 * (except for root, which might share swap device for miniroot).
628 * Flush out any old buffers remaining from a previous use.
630 error = vfs_mountedon(devvp);
633 if (count_udev(devvp->v_udev) > 0 && devvp != rootvp)
635 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
636 error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
637 VOP_UNLOCK(devvp, NULL, 0, td);
642 * Only VMIO the backing device if the backing device is a real
643 * block device. This excludes the original MFS implementation.
644 * Note that it is optional that the backing device be VMIOed. This
645 * increases the opportunity for metadata caching.
647 if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
648 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
649 vfs_object_create(devvp, td);
650 lwkt_gettoken(&vlock, devvp->v_interlock);
651 VOP_UNLOCK(devvp, &vlock, LK_INTERLOCK, td);
654 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
655 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
656 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
657 VOP_UNLOCK(devvp, NULL, 0, td);
661 if (dev->si_iosize_max != 0)
662 mp->mnt_iosize_max = dev->si_iosize_max;
663 if (mp->mnt_iosize_max > MAXPHYS)
664 mp->mnt_iosize_max = MAXPHYS;
666 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred, td) != 0)
669 size = dpart.disklab->d_secsize;
673 if ((error = bread(devvp, SBLOCK, SBSIZE, &bp)) != 0)
675 fs = (struct fs *)bp->b_data;
676 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
677 fs->fs_bsize < sizeof(struct fs)) {
678 error = EINVAL; /* XXX needs translation */
682 fs->fs_flags &= ~FS_UNCLEAN;
683 if (fs->fs_clean == 0) {
684 fs->fs_flags |= FS_UNCLEAN;
685 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
687 "WARNING: %s was not properly dismounted\n",
691 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
697 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
698 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
699 error = EROFS; /* needs translation */
702 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
703 bzero((caddr_t)ump, sizeof *ump);
704 ump->um_malloctype = malloctype;
705 ump->um_i_effnlink_valid = 1;
706 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
708 ump->um_blkatoff = ffs_blkatoff;
709 ump->um_truncate = ffs_truncate;
710 ump->um_update = ffs_update;
711 ump->um_valloc = ffs_valloc;
712 ump->um_vfree = ffs_vfree;
713 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
714 if (fs->fs_sbsize < SBSIZE)
715 bp->b_flags |= B_INVAL;
719 fs->fs_ronly = ronly;
720 size = fs->fs_cssize;
721 blks = howmany(size, fs->fs_fsize);
722 if (fs->fs_contigsumsize > 0)
723 size += fs->fs_ncg * sizeof(int32_t);
724 size += fs->fs_ncg * sizeof(u_int8_t);
725 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
727 for (i = 0; i < blks; i += fs->fs_frag) {
729 if (i + fs->fs_frag > blks)
730 size = (blks - i) * fs->fs_fsize;
731 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
733 free(fs->fs_csp, M_UFSMNT);
736 bcopy(bp->b_data, space, (u_int)size);
737 space = (char *)space + size;
741 if (fs->fs_contigsumsize > 0) {
742 fs->fs_maxcluster = lp = space;
743 for (i = 0; i < fs->fs_ncg; i++)
744 *lp++ = fs->fs_contigsumsize;
747 size = fs->fs_ncg * sizeof(u_int8_t);
748 fs->fs_contigdirs = (u_int8_t *)space;
749 bzero(fs->fs_contigdirs, size);
750 /* Compatibility for old filesystems XXX */
751 if (fs->fs_avgfilesize <= 0) /* XXX */
752 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
753 if (fs->fs_avgfpdir <= 0) /* XXX */
754 fs->fs_avgfpdir = AFPDIR; /* XXX */
755 mp->mnt_data = (qaddr_t)ump;
756 mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
757 mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
758 if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
759 vfs_getvfs(&mp->mnt_stat.f_fsid))
761 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
762 mp->mnt_flag |= MNT_LOCAL;
765 ump->um_devvp = devvp;
766 ump->um_nindir = fs->fs_nindir;
767 ump->um_bptrtodb = fs->fs_fsbtodb;
768 ump->um_seqinc = fs->fs_frag;
769 for (i = 0; i < MAXQUOTAS; i++)
770 ump->um_quotas[i] = NULLVP;
771 dev->si_mountpoint = mp;
775 * Set FS local "last mounted on" information (NULL pad)
777 copystr( mp->mnt_stat.f_mntonname, /* mount point*/
778 fs->fs_fsmnt, /* copy area*/
779 sizeof(fs->fs_fsmnt) - 1, /* max size*/
780 &strsize); /* real size*/
781 bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize);
783 if( mp->mnt_flag & MNT_ROOTFS) {
785 * Root mount; update timestamp in mount structure.
786 * this will be used by the common root mount code
787 * to update the system clock.
789 mp->mnt_time = fs->fs_time;
792 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
793 maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
794 /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
795 if (maxfilesize > (u_int64_t)0x80000000u * PAGE_SIZE - 1)
796 maxfilesize = (u_int64_t)0x80000000u * PAGE_SIZE - 1;
797 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
798 fs->fs_maxfilesize = maxfilesize; /* XXX */
800 if ((fs->fs_flags & FS_DOSOFTDEP) &&
801 (error = softdep_mount(devvp, mp, fs)) != 0) {
802 free(fs->fs_csp, M_UFSMNT);
807 (void) ffs_sbupdate(ump, MNT_WAIT);
811 dev->si_mountpoint = NULL;
814 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
816 free(ump->um_fs, M_UFSMNT);
818 mp->mnt_data = (qaddr_t)0;
824 * Sanity checks for old file systems.
826 * XXX - goes away some day.
829 ffs_oldfscompat(struct fs *fs)
831 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
832 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
833 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
834 fs->fs_nrpos = 8; /* XXX */
835 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
838 u_int64_t sizepb = fs->fs_bsize; /* XXX */
840 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
841 for (i = 0; i < NIADDR; i++) { /* XXX */
842 sizepb *= NINDIR(fs); /* XXX */
843 fs->fs_maxfilesize += sizepb; /* XXX */
846 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
847 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
848 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
854 * unmount system call
857 ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
859 struct ufsmount *ump;
864 if (mntflags & MNT_FORCE) {
867 if (mp->mnt_flag & MNT_SOFTDEP) {
868 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
871 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
876 if (fs->fs_ronly == 0) {
877 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
878 error = ffs_sbupdate(ump, MNT_WAIT);
884 ump->um_devvp->v_rdev->si_mountpoint = NULL;
886 vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
887 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, td);
889 vrele(ump->um_devvp);
891 free(fs->fs_csp, M_UFSMNT);
894 mp->mnt_data = (qaddr_t)0;
895 mp->mnt_flag &= ~MNT_LOCAL;
900 * Flush out all the files in a filesystem.
903 ffs_flushfiles(struct mount *mp, int flags, struct thread *td)
905 struct ufsmount *ump;
910 if (mp->mnt_flag & MNT_QUOTA) {
912 error = vflush(mp, 0, SKIPSYSTEM|flags);
915 for (i = 0; i < MAXQUOTAS; i++) {
916 if (ump->um_quotas[i] == NULLVP)
921 * Here we fall through to vflush again to ensure
922 * that we have gotten rid of all the system vnodes.
927 * Flush all the files.
929 if ((error = vflush(mp, 0, flags)) != 0)
932 * Flush filesystem metadata.
934 vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
935 error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
936 VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
941 * Get file system statistics.
944 ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
946 struct ufsmount *ump;
951 if (fs->fs_magic != FS_MAGIC)
953 sbp->f_bsize = fs->fs_fsize;
954 sbp->f_iosize = fs->fs_bsize;
955 sbp->f_blocks = fs->fs_dsize;
956 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
957 fs->fs_cstotal.cs_nffree;
958 sbp->f_bavail = freespace(fs, fs->fs_minfree);
959 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
960 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
961 if (sbp != &mp->mnt_stat) {
962 sbp->f_type = mp->mnt_vfc->vfc_typenum;
963 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
964 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
965 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
966 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
972 * Go through the disk queues to initiate sandbagged IO;
973 * go through the inodes to write those that have been modified;
974 * initiate the writing of the super block if it has been modified.
976 * Note: we are always called with the filesystem marked `MPBUSY'.
980 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
981 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp,
982 lwkt_tokref_t vlock, void *data);
985 ffs_sync(struct mount *mp, int waitfor, struct thread *td)
987 struct ufsmount *ump = VFSTOUFS(mp);
990 struct scaninfo scaninfo;
993 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
994 printf("fs = %s\n", fs->fs_fsmnt);
995 panic("ffs_sync: rofs mod");
999 * Write back each (modified) inode.
1001 scaninfo.allerror = 0;
1002 scaninfo.rescan = 1;
1003 scaninfo.waitfor = waitfor;
1004 while (scaninfo.rescan) {
1005 scaninfo.rescan = 0;
1006 vmntvnodescan(mp, ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
1010 * Force stale file system control information to be flushed.
1012 if (waitfor != MNT_LAZY) {
1013 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1014 waitfor = MNT_NOWAIT;
1015 vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1016 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
1017 scaninfo.allerror = error;
1018 VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
1024 * Write back modified superblock.
1026 if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1027 scaninfo.allerror = error;
1028 return (scaninfo.allerror);
1033 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1038 * Depend on the mount list's vnode lock to keep things stable
1039 * enough for a quick test. Since there might be hundreds of
1040 * thousands of vnodes, we cannot afford even a subroutine
1041 * call unless there's a good chance that we have work to do.
1044 if (vp->v_type == VNON || ((ip->i_flag &
1045 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1046 TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1054 ffs_sync_scan2(struct mount *mp, struct vnode *vp,
1055 lwkt_tokref_t vlock, void *data)
1057 struct scaninfo *info = data;
1058 thread_t td = curthread; /* XXX */
1063 * We have to recheck after having obtained the vnode interlock.
1066 if (vp->v_type == VNON || ((ip->i_flag &
1067 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1068 TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1069 lwkt_reltoken(vlock);
1072 if (vp->v_type != VCHR) {
1073 error = vget(vp, vlock, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT, td);
1075 if (error == ENOENT)
1078 if ((error = VOP_FSYNC(vp, info->waitfor, td)) != 0)
1079 info->allerror = error;
1080 VOP_UNLOCK(vp, NULL, 0, td);
1085 * We must reference the vp to prevent it from
1086 * getting ripped out from under UFS_UPDATE, since
1087 * we are not holding a vnode lock.
1090 lwkt_reltoken(vlock);
1091 /* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1099 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1100 * in from disk. If it is in core, wait for the lock bit to clear, then
1101 * return the inode locked. Detection and handling of mount points must be
1102 * done by the calling routine.
1104 static int ffs_inode_hash_lock;
1107 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1111 struct ufsmount *ump;
1120 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1125 * Lock out the creation of new entries in the FFS hash table in
1126 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1129 if (ffs_inode_hash_lock) {
1130 while (ffs_inode_hash_lock) {
1131 ffs_inode_hash_lock = -1;
1132 tsleep(&ffs_inode_hash_lock, 0, "ffsvgt", 0);
1136 ffs_inode_hash_lock = 1;
1139 * If this MALLOC() is performed after the getnewvnode()
1140 * it might block, leaving a vnode with a NULL v_data to be
1141 * found by ffs_sync() if a sync happens to fire right then,
1142 * which will cause a panic because ffs_sync() blindly
1143 * dereferences vp->v_data (as well it should).
1145 MALLOC(ip, struct inode *, sizeof(struct inode),
1146 ump->um_malloctype, M_WAITOK);
1148 /* Allocate a new vnode/inode. */
1149 error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp);
1151 if (ffs_inode_hash_lock < 0)
1152 wakeup(&ffs_inode_hash_lock);
1153 ffs_inode_hash_lock = 0;
1155 FREE(ip, ump->um_malloctype);
1158 bzero((caddr_t)ip, sizeof(struct inode));
1159 lockinit(&ip->i_lock, 0, "inode", VLKTIMEOUT, LK_CANRECURSE);
1162 * FFS supports lock sharing in the stack of vnodes
1164 vp->v_vnlock = &ip->i_lock;
1166 ip->i_fs = fs = ump->um_fs;
1172 for (i = 0; i < MAXQUOTAS; i++)
1173 ip->i_dquot[i] = NODQUOT;
1177 * Put it onto its hash chain and lock it so that other requests for
1178 * this inode will block if they arrive while we are sleeping waiting
1179 * for old data structures to be purged or for the contents of the
1180 * disk portion of this inode to be read.
1184 if (ffs_inode_hash_lock < 0)
1185 wakeup(&ffs_inode_hash_lock);
1186 ffs_inode_hash_lock = 0;
1188 /* Read in the disk contents for the inode, copy into the inode. */
1189 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1190 (int)fs->fs_bsize, &bp);
1193 * The inode does not contain anything useful, so it would
1194 * be misleading to leave it on its hash chain. With mode
1195 * still zero, it will be unlinked and returned to the free
1203 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1204 if (DOINGSOFTDEP(vp))
1205 softdep_load_inodeblock(ip);
1207 ip->i_effnlink = ip->i_nlink;
1211 * Initialize the vnode from the inode, check for aliases.
1212 * Note that the underlying vnode may have changed.
1214 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1221 * Finish inode initialization now that aliasing has been resolved.
1223 ip->i_devvp = ump->um_devvp;
1226 * Set up a generation number for this inode if it does not
1227 * already have one. This should only happen on old filesystems.
1229 if (ip->i_gen == 0) {
1230 ip->i_gen = random() / 2 + 1;
1231 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1232 ip->i_flag |= IN_MODIFIED;
1235 * Ensure that uid and gid are correct. This is a temporary
1236 * fix until fsck has been changed to do the update.
1238 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1239 ip->i_uid = ip->i_din.di_ouid; /* XXX */
1240 ip->i_gid = ip->i_din.di_ogid; /* XXX */
1248 * File handle to vnode
1250 * Have to be really careful about stale file handles:
1251 * - check that the inode number is valid
1252 * - call ffs_vget() to get the locked inode
1253 * - check for an unallocated inode (i_mode == 0)
1254 * - check that the given client host has export rights and return
1255 * those rights via. exflagsp and credanonp
1258 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1263 ufhp = (struct ufid *)fhp;
1264 fs = VFSTOUFS(mp)->um_fs;
1265 if (ufhp->ufid_ino < ROOTINO ||
1266 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1268 return (ufs_fhtovp(mp, ufhp, vpp));
1272 * Vnode pointer to File handle
1276 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1282 ufhp = (struct ufid *)fhp;
1283 ufhp->ufid_len = sizeof(struct ufid);
1284 ufhp->ufid_ino = ip->i_number;
1285 ufhp->ufid_gen = ip->i_gen;
1290 * Initialize the filesystem; just use ufs_init.
1293 ffs_init(struct vfsconf *vfsp)
1295 softdep_initialize();
1296 return (ufs_init(vfsp));
1300 * Write a superblock and associated information back to disk.
1303 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1305 struct fs *dfs, *fs = mp->um_fs;
1309 int i, size, error, allerror = 0;
1312 * First write back the summary information.
1314 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1316 for (i = 0; i < blks; i += fs->fs_frag) {
1317 size = fs->fs_bsize;
1318 if (i + fs->fs_frag > blks)
1319 size = (blks - i) * fs->fs_fsize;
1320 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1322 bcopy(space, bp->b_data, (u_int)size);
1323 space = (char *)space + size;
1324 if (waitfor != MNT_WAIT)
1326 else if ((error = bwrite(bp)) != 0)
1330 * Now write back the superblock itself. If any errors occurred
1331 * up to this point, then fail so that the superblock avoids
1332 * being written out as clean.
1336 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1338 fs->fs_time = time_second;
1339 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1340 /* Restore compatibility to old file systems. XXX */
1341 dfs = (struct fs *)bp->b_data; /* XXX */
1342 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1343 dfs->fs_nrpos = -1; /* XXX */
1344 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1345 int32_t *lp, tmp; /* XXX */
1347 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
1348 tmp = lp[4]; /* XXX */
1349 for (i = 4; i > 0; i--) /* XXX */
1350 lp[i] = lp[i-1]; /* XXX */
1351 lp[0] = tmp; /* XXX */
1353 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1354 if (waitfor != MNT_WAIT)
1356 else if ((error = bwrite(bp)) != 0)