Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.1.1.2
      1      1.1  mycroft /*
      2      1.1  mycroft  * Copyright (c) 1989, 1991, 1993, 1994
      3      1.1  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1  mycroft  *
      5      1.1  mycroft  * Redistribution and use in source and binary forms, with or without
      6      1.1  mycroft  * modification, are permitted provided that the following conditions
      7      1.1  mycroft  * are met:
      8      1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
      9      1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     10      1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     13      1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     14      1.1  mycroft  *    must display the following acknowledgement:
     15      1.1  mycroft  *	This product includes software developed by the University of
     16      1.1  mycroft  *	California, Berkeley and its contributors.
     17      1.1  mycroft  * 4. Neither the name of the University nor the names of its contributors
     18      1.1  mycroft  *    may be used to endorse or promote products derived from this software
     19      1.1  mycroft  *    without specific prior written permission.
     20      1.1  mycroft  *
     21      1.1  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1  mycroft  * SUCH DAMAGE.
     32      1.1  mycroft  *
     33  1.1.1.2     fvdl  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     34      1.1  mycroft  */
     35      1.1  mycroft 
     36      1.1  mycroft #include <sys/param.h>
     37      1.1  mycroft #include <sys/systm.h>
     38      1.1  mycroft #include <sys/namei.h>
     39      1.1  mycroft #include <sys/proc.h>
     40      1.1  mycroft #include <sys/kernel.h>
     41      1.1  mycroft #include <sys/vnode.h>
     42      1.1  mycroft #include <sys/socket.h>
     43      1.1  mycroft #include <sys/mount.h>
     44      1.1  mycroft #include <sys/buf.h>
     45      1.1  mycroft #include <sys/mbuf.h>
     46      1.1  mycroft #include <sys/file.h>
     47      1.1  mycroft #include <sys/disklabel.h>
     48      1.1  mycroft #include <sys/ioctl.h>
     49      1.1  mycroft #include <sys/errno.h>
     50      1.1  mycroft #include <sys/malloc.h>
     51      1.1  mycroft 
     52      1.1  mycroft #include <miscfs/specfs/specdev.h>
     53      1.1  mycroft 
     54      1.1  mycroft #include <ufs/ufs/quota.h>
     55      1.1  mycroft #include <ufs/ufs/ufsmount.h>
     56      1.1  mycroft #include <ufs/ufs/inode.h>
     57      1.1  mycroft #include <ufs/ufs/ufs_extern.h>
     58      1.1  mycroft 
     59      1.1  mycroft #include <ufs/ffs/fs.h>
     60      1.1  mycroft #include <ufs/ffs/ffs_extern.h>
     61      1.1  mycroft 
     62      1.1  mycroft int ffs_sbupdate __P((struct ufsmount *, int));
     63      1.1  mycroft 
     64      1.1  mycroft struct vfsops ufs_vfsops = {
     65      1.1  mycroft 	ffs_mount,
     66      1.1  mycroft 	ufs_start,
     67      1.1  mycroft 	ffs_unmount,
     68      1.1  mycroft 	ufs_root,
     69      1.1  mycroft 	ufs_quotactl,
     70      1.1  mycroft 	ffs_statfs,
     71      1.1  mycroft 	ffs_sync,
     72      1.1  mycroft 	ffs_vget,
     73      1.1  mycroft 	ffs_fhtovp,
     74      1.1  mycroft 	ffs_vptofh,
     75      1.1  mycroft 	ffs_init,
     76  1.1.1.2     fvdl 	ffs_sysctl,
     77      1.1  mycroft };
     78      1.1  mycroft 
     79      1.1  mycroft extern u_long nextgennumber;
     80      1.1  mycroft 
     81      1.1  mycroft /*
     82      1.1  mycroft  * Called by main() when ufs is going to be mounted as root.
     83      1.1  mycroft  */
     84      1.1  mycroft ffs_mountroot()
     85      1.1  mycroft {
     86      1.1  mycroft 	extern struct vnode *rootvp;
     87  1.1.1.2     fvdl 	struct fs *fs;
     88  1.1.1.2     fvdl 	struct mount *mp;
     89      1.1  mycroft 	struct proc *p = curproc;	/* XXX */
     90      1.1  mycroft 	struct ufsmount *ump;
     91      1.1  mycroft 	u_int size;
     92      1.1  mycroft 	int error;
     93      1.1  mycroft 
     94      1.1  mycroft 	/*
     95      1.1  mycroft 	 * Get vnodes for swapdev and rootdev.
     96      1.1  mycroft 	 */
     97  1.1.1.2     fvdl 	if ((error = bdevvp(swapdev, &swapdev_vp)) ||
     98  1.1.1.2     fvdl 	    (error = bdevvp(rootdev, &rootvp))) {
     99  1.1.1.2     fvdl 		printf("ffs_mountroot: can't setup bdevvp's");
    100      1.1  mycroft 		return (error);
    101      1.1  mycroft 	}
    102  1.1.1.2     fvdl 	if (error = vfs_rootmountalloc("ufs", "root_device", &mp))
    103  1.1.1.2     fvdl 		return (error);
    104  1.1.1.2     fvdl 	if (error = ffs_mountfs(rootvp, mp, p)) {
    105  1.1.1.2     fvdl 		mp->mnt_vfc->vfc_refcount--;
    106  1.1.1.2     fvdl 		vfs_unbusy(mp, p);
    107      1.1  mycroft 		free(mp, M_MOUNT);
    108      1.1  mycroft 		return (error);
    109      1.1  mycroft 	}
    110  1.1.1.2     fvdl 	simple_lock(&mountlist_slock);
    111  1.1.1.2     fvdl 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    112  1.1.1.2     fvdl 	simple_unlock(&mountlist_slock);
    113      1.1  mycroft 	ump = VFSTOUFS(mp);
    114      1.1  mycroft 	fs = ump->um_fs;
    115  1.1.1.2     fvdl 	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    116      1.1  mycroft 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    117  1.1.1.2     fvdl 	vfs_unbusy(mp, p);
    118      1.1  mycroft 	inittodr(fs->fs_time);
    119      1.1  mycroft 	return (0);
    120      1.1  mycroft }
    121      1.1  mycroft 
    122      1.1  mycroft /*
    123      1.1  mycroft  * VFS Operations.
    124      1.1  mycroft  *
    125      1.1  mycroft  * mount system call
    126      1.1  mycroft  */
    127      1.1  mycroft int
    128      1.1  mycroft ffs_mount(mp, path, data, ndp, p)
    129      1.1  mycroft 	register struct mount *mp;
    130      1.1  mycroft 	char *path;
    131      1.1  mycroft 	caddr_t data;
    132      1.1  mycroft 	struct nameidata *ndp;
    133      1.1  mycroft 	struct proc *p;
    134      1.1  mycroft {
    135      1.1  mycroft 	struct vnode *devvp;
    136      1.1  mycroft 	struct ufs_args args;
    137      1.1  mycroft 	struct ufsmount *ump;
    138      1.1  mycroft 	register struct fs *fs;
    139      1.1  mycroft 	u_int size;
    140      1.1  mycroft 	int error, flags;
    141  1.1.1.2     fvdl 	mode_t accessmode;
    142      1.1  mycroft 
    143      1.1  mycroft 	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
    144      1.1  mycroft 		return (error);
    145      1.1  mycroft 	/*
    146      1.1  mycroft 	 * If updating, check whether changing from read-only to
    147      1.1  mycroft 	 * read/write; if there is no device name, that's all we do.
    148      1.1  mycroft 	 */
    149      1.1  mycroft 	if (mp->mnt_flag & MNT_UPDATE) {
    150      1.1  mycroft 		ump = VFSTOUFS(mp);
    151      1.1  mycroft 		fs = ump->um_fs;
    152      1.1  mycroft 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    153      1.1  mycroft 			flags = WRITECLOSE;
    154      1.1  mycroft 			if (mp->mnt_flag & MNT_FORCE)
    155      1.1  mycroft 				flags |= FORCECLOSE;
    156  1.1.1.2     fvdl 			if (error = ffs_flushfiles(mp, flags, p))
    157  1.1.1.2     fvdl 				return (error);
    158  1.1.1.2     fvdl 			fs->fs_clean = 1;
    159  1.1.1.2     fvdl 			fs->fs_ronly = 1;
    160  1.1.1.2     fvdl 			if (error = ffs_sbupdate(ump, MNT_WAIT)) {
    161  1.1.1.2     fvdl 				fs->fs_clean = 0;
    162  1.1.1.2     fvdl 				fs->fs_ronly = 0;
    163  1.1.1.2     fvdl 				return (error);
    164  1.1.1.2     fvdl 			}
    165  1.1.1.2     fvdl 		}
    166  1.1.1.2     fvdl 		if ((mp->mnt_flag & MNT_RELOAD) &&
    167  1.1.1.2     fvdl 		    (error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p)))
    168      1.1  mycroft 			return (error);
    169  1.1.1.2     fvdl 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    170  1.1.1.2     fvdl 			/*
    171  1.1.1.2     fvdl 			 * If upgrade to read-write by non-root, then verify
    172  1.1.1.2     fvdl 			 * that user has necessary permissions on the device.
    173  1.1.1.2     fvdl 			 */
    174  1.1.1.2     fvdl 			if (p->p_ucred->cr_uid != 0) {
    175  1.1.1.2     fvdl 				devvp = ump->um_devvp;
    176  1.1.1.2     fvdl 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
    177  1.1.1.2     fvdl 				if (error = VOP_ACCESS(devvp, VREAD | VWRITE,
    178  1.1.1.2     fvdl 				    p->p_ucred, p)) {
    179  1.1.1.2     fvdl 					VOP_UNLOCK(devvp, 0, p);
    180  1.1.1.2     fvdl 					return (error);
    181  1.1.1.2     fvdl 				}
    182  1.1.1.2     fvdl 				VOP_UNLOCK(devvp, 0, p);
    183  1.1.1.2     fvdl 			}
    184      1.1  mycroft 			fs->fs_ronly = 0;
    185  1.1.1.2     fvdl 			fs->fs_clean = 0;
    186  1.1.1.2     fvdl 			(void) ffs_sbupdate(ump, MNT_WAIT);
    187  1.1.1.2     fvdl 		}
    188      1.1  mycroft 		if (args.fspec == 0) {
    189      1.1  mycroft 			/*
    190      1.1  mycroft 			 * Process export requests.
    191      1.1  mycroft 			 */
    192      1.1  mycroft 			return (vfs_export(mp, &ump->um_export, &args.export));
    193      1.1  mycroft 		}
    194      1.1  mycroft 	}
    195      1.1  mycroft 	/*
    196      1.1  mycroft 	 * Not an update, or updating the name: look up the name
    197      1.1  mycroft 	 * and verify that it refers to a sensible block device.
    198      1.1  mycroft 	 */
    199      1.1  mycroft 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    200      1.1  mycroft 	if (error = namei(ndp))
    201      1.1  mycroft 		return (error);
    202      1.1  mycroft 	devvp = ndp->ni_vp;
    203      1.1  mycroft 
    204      1.1  mycroft 	if (devvp->v_type != VBLK) {
    205      1.1  mycroft 		vrele(devvp);
    206      1.1  mycroft 		return (ENOTBLK);
    207      1.1  mycroft 	}
    208      1.1  mycroft 	if (major(devvp->v_rdev) >= nblkdev) {
    209      1.1  mycroft 		vrele(devvp);
    210      1.1  mycroft 		return (ENXIO);
    211      1.1  mycroft 	}
    212  1.1.1.2     fvdl 	/*
    213  1.1.1.2     fvdl 	 * If mount by non-root, then verify that user has necessary
    214  1.1.1.2     fvdl 	 * permissions on the device.
    215  1.1.1.2     fvdl 	 */
    216  1.1.1.2     fvdl 	if (p->p_ucred->cr_uid != 0) {
    217  1.1.1.2     fvdl 		accessmode = VREAD;
    218  1.1.1.2     fvdl 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    219  1.1.1.2     fvdl 			accessmode |= VWRITE;
    220  1.1.1.2     fvdl 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
    221  1.1.1.2     fvdl 		if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
    222  1.1.1.2     fvdl 			vput(devvp);
    223  1.1.1.2     fvdl 			return (error);
    224  1.1.1.2     fvdl 		}
    225  1.1.1.2     fvdl 		VOP_UNLOCK(devvp, 0, p);
    226  1.1.1.2     fvdl 	}
    227      1.1  mycroft 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    228      1.1  mycroft 		error = ffs_mountfs(devvp, mp, p);
    229      1.1  mycroft 	else {
    230      1.1  mycroft 		if (devvp != ump->um_devvp)
    231      1.1  mycroft 			error = EINVAL;	/* needs translation */
    232      1.1  mycroft 		else
    233      1.1  mycroft 			vrele(devvp);
    234      1.1  mycroft 	}
    235      1.1  mycroft 	if (error) {
    236      1.1  mycroft 		vrele(devvp);
    237      1.1  mycroft 		return (error);
    238      1.1  mycroft 	}
    239      1.1  mycroft 	ump = VFSTOUFS(mp);
    240      1.1  mycroft 	fs = ump->um_fs;
    241      1.1  mycroft 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    242      1.1  mycroft 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
    243      1.1  mycroft 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
    244      1.1  mycroft 	    MNAMELEN);
    245      1.1  mycroft 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    246      1.1  mycroft 	    &size);
    247      1.1  mycroft 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    248      1.1  mycroft 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    249      1.1  mycroft 	return (0);
    250      1.1  mycroft }
    251      1.1  mycroft 
    252      1.1  mycroft /*
    253      1.1  mycroft  * Reload all incore data for a filesystem (used after running fsck on
    254      1.1  mycroft  * the root filesystem and finding things to fix). The filesystem must
    255      1.1  mycroft  * be mounted read-only.
    256      1.1  mycroft  *
    257      1.1  mycroft  * Things to do to update the mount:
    258      1.1  mycroft  *	1) invalidate all cached meta-data.
    259      1.1  mycroft  *	2) re-read superblock from disk.
    260      1.1  mycroft  *	3) re-read summary information from disk.
    261      1.1  mycroft  *	4) invalidate all inactive vnodes.
    262      1.1  mycroft  *	5) invalidate all cached file data.
    263      1.1  mycroft  *	6) re-read inode data for all active vnodes.
    264      1.1  mycroft  */
    265      1.1  mycroft ffs_reload(mountp, cred, p)
    266      1.1  mycroft 	register struct mount *mountp;
    267      1.1  mycroft 	struct ucred *cred;
    268      1.1  mycroft 	struct proc *p;
    269      1.1  mycroft {
    270      1.1  mycroft 	register struct vnode *vp, *nvp, *devvp;
    271      1.1  mycroft 	struct inode *ip;
    272      1.1  mycroft 	struct csum *space;
    273      1.1  mycroft 	struct buf *bp;
    274  1.1.1.2     fvdl 	struct fs *fs, *newfs;
    275  1.1.1.2     fvdl 	struct partinfo dpart;
    276      1.1  mycroft 	int i, blks, size, error;
    277  1.1.1.2     fvdl 	int32_t *lp;
    278      1.1  mycroft 
    279      1.1  mycroft 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    280      1.1  mycroft 		return (EINVAL);
    281      1.1  mycroft 	/*
    282      1.1  mycroft 	 * Step 1: invalidate all cached meta-data.
    283      1.1  mycroft 	 */
    284      1.1  mycroft 	devvp = VFSTOUFS(mountp)->um_devvp;
    285      1.1  mycroft 	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
    286      1.1  mycroft 		panic("ffs_reload: dirty1");
    287      1.1  mycroft 	/*
    288      1.1  mycroft 	 * Step 2: re-read superblock from disk.
    289      1.1  mycroft 	 */
    290  1.1.1.2     fvdl 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
    291  1.1.1.2     fvdl 		size = DEV_BSIZE;
    292  1.1.1.2     fvdl 	else
    293  1.1.1.2     fvdl 		size = dpart.disklab->d_secsize;
    294  1.1.1.2     fvdl 	if (error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, NOCRED,&bp))
    295      1.1  mycroft 		return (error);
    296  1.1.1.2     fvdl 	newfs = (struct fs *)bp->b_data;
    297  1.1.1.2     fvdl 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
    298  1.1.1.2     fvdl 	    newfs->fs_bsize < sizeof(struct fs)) {
    299      1.1  mycroft 		brelse(bp);
    300      1.1  mycroft 		return (EIO);		/* XXX needs translation */
    301      1.1  mycroft 	}
    302      1.1  mycroft 	fs = VFSTOUFS(mountp)->um_fs;
    303  1.1.1.2     fvdl 	/*
    304  1.1.1.2     fvdl 	 * Copy pointer fields back into superblock before copying in	XXX
    305  1.1.1.2     fvdl 	 * new superblock. These should really be in the ufsmount.	XXX
    306  1.1.1.2     fvdl 	 * Note that important parameters (eg fs_ncg) are unchanged.
    307  1.1.1.2     fvdl 	 */
    308  1.1.1.2     fvdl 	bcopy(&fs->fs_csp[0], &newfs->fs_csp[0], sizeof(fs->fs_csp));
    309  1.1.1.2     fvdl 	newfs->fs_maxcluster = fs->fs_maxcluster;
    310  1.1.1.2     fvdl 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
    311      1.1  mycroft 	if (fs->fs_sbsize < SBSIZE)
    312      1.1  mycroft 		bp->b_flags |= B_INVAL;
    313      1.1  mycroft 	brelse(bp);
    314  1.1.1.2     fvdl 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    315      1.1  mycroft 	ffs_oldfscompat(fs);
    316      1.1  mycroft 	/*
    317      1.1  mycroft 	 * Step 3: re-read summary information from disk.
    318      1.1  mycroft 	 */
    319      1.1  mycroft 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    320      1.1  mycroft 	space = fs->fs_csp[0];
    321      1.1  mycroft 	for (i = 0; i < blks; i += fs->fs_frag) {
    322      1.1  mycroft 		size = fs->fs_bsize;
    323      1.1  mycroft 		if (i + fs->fs_frag > blks)
    324      1.1  mycroft 			size = (blks - i) * fs->fs_fsize;
    325      1.1  mycroft 		if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    326      1.1  mycroft 		    NOCRED, &bp))
    327      1.1  mycroft 			return (error);
    328      1.1  mycroft 		bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size);
    329      1.1  mycroft 		brelse(bp);
    330      1.1  mycroft 	}
    331  1.1.1.2     fvdl 	/*
    332  1.1.1.2     fvdl 	 * We no longer know anything about clusters per cylinder group.
    333  1.1.1.2     fvdl 	 */
    334  1.1.1.2     fvdl 	if (fs->fs_contigsumsize > 0) {
    335  1.1.1.2     fvdl 		lp = fs->fs_maxcluster;
    336  1.1.1.2     fvdl 		for (i = 0; i < fs->fs_ncg; i++)
    337  1.1.1.2     fvdl 			*lp++ = fs->fs_contigsumsize;
    338  1.1.1.2     fvdl 	}
    339  1.1.1.2     fvdl 
    340      1.1  mycroft loop:
    341  1.1.1.2     fvdl 	simple_lock(&mntvnode_slock);
    342      1.1  mycroft 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    343  1.1.1.2     fvdl 		if (vp->v_mount != mountp) {
    344  1.1.1.2     fvdl 			simple_unlock(&mntvnode_slock);
    345  1.1.1.2     fvdl 			goto loop;
    346  1.1.1.2     fvdl 		}
    347      1.1  mycroft 		nvp = vp->v_mntvnodes.le_next;
    348      1.1  mycroft 		/*
    349      1.1  mycroft 		 * Step 4: invalidate all inactive vnodes.
    350      1.1  mycroft 		 */
    351  1.1.1.2     fvdl 		if (vrecycle(vp, &mntvnode_slock, p))
    352  1.1.1.2     fvdl 			goto loop;
    353      1.1  mycroft 		/*
    354      1.1  mycroft 		 * Step 5: invalidate all cached file data.
    355      1.1  mycroft 		 */
    356  1.1.1.2     fvdl 		simple_lock(&vp->v_interlock);
    357  1.1.1.2     fvdl 		simple_unlock(&mntvnode_slock);
    358  1.1.1.2     fvdl 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
    359      1.1  mycroft 			goto loop;
    360  1.1.1.2     fvdl 		}
    361      1.1  mycroft 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    362      1.1  mycroft 			panic("ffs_reload: dirty2");
    363      1.1  mycroft 		/*
    364      1.1  mycroft 		 * Step 6: re-read inode data for all active vnodes.
    365      1.1  mycroft 		 */
    366      1.1  mycroft 		ip = VTOI(vp);
    367      1.1  mycroft 		if (error =
    368      1.1  mycroft 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    369      1.1  mycroft 		    (int)fs->fs_bsize, NOCRED, &bp)) {
    370      1.1  mycroft 			vput(vp);
    371      1.1  mycroft 			return (error);
    372      1.1  mycroft 		}
    373      1.1  mycroft 		ip->i_din = *((struct dinode *)bp->b_data +
    374      1.1  mycroft 		    ino_to_fsbo(fs, ip->i_number));
    375      1.1  mycroft 		brelse(bp);
    376      1.1  mycroft 		vput(vp);
    377  1.1.1.2     fvdl 		simple_lock(&mntvnode_slock);
    378      1.1  mycroft 	}
    379  1.1.1.2     fvdl 	simple_unlock(&mntvnode_slock);
    380      1.1  mycroft 	return (0);
    381      1.1  mycroft }
    382      1.1  mycroft 
    383      1.1  mycroft /*
    384      1.1  mycroft  * Common code for mount and mountroot
    385      1.1  mycroft  */
    386      1.1  mycroft int
    387      1.1  mycroft ffs_mountfs(devvp, mp, p)
    388      1.1  mycroft 	register struct vnode *devvp;
    389      1.1  mycroft 	struct mount *mp;
    390      1.1  mycroft 	struct proc *p;
    391      1.1  mycroft {
    392      1.1  mycroft 	register struct ufsmount *ump;
    393      1.1  mycroft 	struct buf *bp;
    394      1.1  mycroft 	register struct fs *fs;
    395  1.1.1.2     fvdl 	dev_t dev;
    396      1.1  mycroft 	struct partinfo dpart;
    397      1.1  mycroft 	caddr_t base, space;
    398  1.1.1.2     fvdl 	int error, i, blks, size, ronly;
    399  1.1.1.2     fvdl 	int32_t *lp;
    400  1.1.1.2     fvdl 	struct ucred *cred;
    401      1.1  mycroft 	extern struct vnode *rootvp;
    402  1.1.1.2     fvdl 	u_int64_t maxfilesize;					/* XXX */
    403      1.1  mycroft 
    404  1.1.1.2     fvdl 	dev = devvp->v_rdev;
    405  1.1.1.2     fvdl 	cred = p ? p->p_ucred : NOCRED;
    406      1.1  mycroft 	/*
    407      1.1  mycroft 	 * Disallow multiple mounts of the same device.
    408      1.1  mycroft 	 * Disallow mounting of a device that is currently in use
    409      1.1  mycroft 	 * (except for root, which might share swap device for miniroot).
    410      1.1  mycroft 	 * Flush out any old buffers remaining from a previous use.
    411      1.1  mycroft 	 */
    412      1.1  mycroft 	if (error = vfs_mountedon(devvp))
    413      1.1  mycroft 		return (error);
    414      1.1  mycroft 	if (vcount(devvp) > 1 && devvp != rootvp)
    415      1.1  mycroft 		return (EBUSY);
    416  1.1.1.2     fvdl 	if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0))
    417      1.1  mycroft 		return (error);
    418      1.1  mycroft 
    419      1.1  mycroft 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    420      1.1  mycroft 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
    421      1.1  mycroft 		return (error);
    422  1.1.1.2     fvdl 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    423      1.1  mycroft 		size = DEV_BSIZE;
    424  1.1.1.2     fvdl 	else
    425      1.1  mycroft 		size = dpart.disklab->d_secsize;
    426      1.1  mycroft 
    427      1.1  mycroft 	bp = NULL;
    428      1.1  mycroft 	ump = NULL;
    429  1.1.1.2     fvdl 	if (error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, cred, &bp))
    430      1.1  mycroft 		goto out;
    431      1.1  mycroft 	fs = (struct fs *)bp->b_data;
    432      1.1  mycroft 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
    433      1.1  mycroft 	    fs->fs_bsize < sizeof(struct fs)) {
    434      1.1  mycroft 		error = EINVAL;		/* XXX needs translation */
    435      1.1  mycroft 		goto out;
    436      1.1  mycroft 	}
    437  1.1.1.2     fvdl 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    438  1.1.1.2     fvdl 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    439  1.1.1.2     fvdl 		error = EROFS;          /* needs translation */
    440  1.1.1.2     fvdl 		goto out;
    441  1.1.1.2     fvdl 	}
    442      1.1  mycroft 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    443      1.1  mycroft 	bzero((caddr_t)ump, sizeof *ump);
    444      1.1  mycroft 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
    445      1.1  mycroft 	    M_WAITOK);
    446      1.1  mycroft 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
    447      1.1  mycroft 	if (fs->fs_sbsize < SBSIZE)
    448      1.1  mycroft 		bp->b_flags |= B_INVAL;
    449      1.1  mycroft 	brelse(bp);
    450      1.1  mycroft 	bp = NULL;
    451      1.1  mycroft 	fs = ump->um_fs;
    452      1.1  mycroft 	fs->fs_ronly = ronly;
    453  1.1.1.2     fvdl 	size = fs->fs_cssize;
    454  1.1.1.2     fvdl 	blks = howmany(size, fs->fs_fsize);
    455  1.1.1.2     fvdl 	if (fs->fs_contigsumsize > 0)
    456  1.1.1.2     fvdl 		size += fs->fs_ncg * sizeof(int32_t);
    457  1.1.1.2     fvdl 	base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    458      1.1  mycroft 	for (i = 0; i < blks; i += fs->fs_frag) {
    459      1.1  mycroft 		size = fs->fs_bsize;
    460      1.1  mycroft 		if (i + fs->fs_frag > blks)
    461      1.1  mycroft 			size = (blks - i) * fs->fs_fsize;
    462  1.1.1.2     fvdl 		if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    463  1.1.1.2     fvdl 		    cred, &bp)) {
    464      1.1  mycroft 			free(base, M_UFSMNT);
    465      1.1  mycroft 			goto out;
    466      1.1  mycroft 		}
    467      1.1  mycroft 		bcopy(bp->b_data, space, (u_int)size);
    468      1.1  mycroft 		fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
    469      1.1  mycroft 		space += size;
    470      1.1  mycroft 		brelse(bp);
    471      1.1  mycroft 		bp = NULL;
    472      1.1  mycroft 	}
    473  1.1.1.2     fvdl 	if (fs->fs_contigsumsize > 0) {
    474  1.1.1.2     fvdl 		fs->fs_maxcluster = lp = (int32_t *)space;
    475  1.1.1.2     fvdl 		for (i = 0; i < fs->fs_ncg; i++)
    476  1.1.1.2     fvdl 			*lp++ = fs->fs_contigsumsize;
    477  1.1.1.2     fvdl 	}
    478      1.1  mycroft 	mp->mnt_data = (qaddr_t)ump;
    479      1.1  mycroft 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    480  1.1.1.2     fvdl 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
    481      1.1  mycroft 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    482      1.1  mycroft 	ump->um_mountp = mp;
    483      1.1  mycroft 	ump->um_dev = dev;
    484      1.1  mycroft 	ump->um_devvp = devvp;
    485      1.1  mycroft 	ump->um_nindir = fs->fs_nindir;
    486      1.1  mycroft 	ump->um_bptrtodb = fs->fs_fsbtodb;
    487      1.1  mycroft 	ump->um_seqinc = fs->fs_frag;
    488      1.1  mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    489      1.1  mycroft 		ump->um_quotas[i] = NULLVP;
    490      1.1  mycroft 	devvp->v_specflags |= SI_MOUNTEDON;
    491      1.1  mycroft 	ffs_oldfscompat(fs);
    492  1.1.1.2     fvdl 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    493  1.1.1.2     fvdl 	maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;	/* XXX */
    494  1.1.1.2     fvdl 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    495  1.1.1.2     fvdl 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    496  1.1.1.2     fvdl 	if (ronly == 0) {
    497  1.1.1.2     fvdl 		fs->fs_clean = 0;
    498  1.1.1.2     fvdl 		(void) ffs_sbupdate(ump, MNT_WAIT);
    499  1.1.1.2     fvdl 	}
    500      1.1  mycroft 	return (0);
    501      1.1  mycroft out:
    502      1.1  mycroft 	if (bp)
    503      1.1  mycroft 		brelse(bp);
    504  1.1.1.2     fvdl 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    505      1.1  mycroft 	if (ump) {
    506      1.1  mycroft 		free(ump->um_fs, M_UFSMNT);
    507      1.1  mycroft 		free(ump, M_UFSMNT);
    508      1.1  mycroft 		mp->mnt_data = (qaddr_t)0;
    509      1.1  mycroft 	}
    510      1.1  mycroft 	return (error);
    511      1.1  mycroft }
    512      1.1  mycroft 
    513      1.1  mycroft /*
    514      1.1  mycroft  * Sanity checks for old file systems.
    515      1.1  mycroft  *
    516      1.1  mycroft  * XXX - goes away some day.
    517      1.1  mycroft  */
    518      1.1  mycroft ffs_oldfscompat(fs)
    519      1.1  mycroft 	struct fs *fs;
    520      1.1  mycroft {
    521      1.1  mycroft 	int i;
    522      1.1  mycroft 
    523      1.1  mycroft 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    524      1.1  mycroft 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    525      1.1  mycroft 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    526      1.1  mycroft 		fs->fs_nrpos = 8;				/* XXX */
    527      1.1  mycroft 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    528  1.1.1.2     fvdl 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    529      1.1  mycroft 								/* XXX */
    530      1.1  mycroft 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    531      1.1  mycroft 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    532      1.1  mycroft 			sizepb *= NINDIR(fs);			/* XXX */
    533      1.1  mycroft 			fs->fs_maxfilesize += sizepb;		/* XXX */
    534      1.1  mycroft 		}						/* XXX */
    535      1.1  mycroft 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    536      1.1  mycroft 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    537      1.1  mycroft 	}							/* XXX */
    538      1.1  mycroft 	return (0);
    539      1.1  mycroft }
    540      1.1  mycroft 
    541      1.1  mycroft /*
    542      1.1  mycroft  * unmount system call
    543      1.1  mycroft  */
    544      1.1  mycroft int
    545      1.1  mycroft ffs_unmount(mp, mntflags, p)
    546      1.1  mycroft 	struct mount *mp;
    547      1.1  mycroft 	int mntflags;
    548      1.1  mycroft 	struct proc *p;
    549      1.1  mycroft {
    550      1.1  mycroft 	register struct ufsmount *ump;
    551      1.1  mycroft 	register struct fs *fs;
    552  1.1.1.2     fvdl 	int error, flags;
    553      1.1  mycroft 
    554      1.1  mycroft 	flags = 0;
    555  1.1.1.2     fvdl 	if (mntflags & MNT_FORCE)
    556      1.1  mycroft 		flags |= FORCECLOSE;
    557      1.1  mycroft 	if (error = ffs_flushfiles(mp, flags, p))
    558      1.1  mycroft 		return (error);
    559      1.1  mycroft 	ump = VFSTOUFS(mp);
    560      1.1  mycroft 	fs = ump->um_fs;
    561  1.1.1.2     fvdl 	if (fs->fs_ronly == 0) {
    562  1.1.1.2     fvdl 		fs->fs_clean = 1;
    563  1.1.1.2     fvdl 		if (error = ffs_sbupdate(ump, MNT_WAIT)) {
    564  1.1.1.2     fvdl 			fs->fs_clean = 0;
    565  1.1.1.2     fvdl 			return (error);
    566  1.1.1.2     fvdl 		}
    567  1.1.1.2     fvdl 	}
    568      1.1  mycroft 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
    569  1.1.1.2     fvdl 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    570      1.1  mycroft 		NOCRED, p);
    571      1.1  mycroft 	vrele(ump->um_devvp);
    572      1.1  mycroft 	free(fs->fs_csp[0], M_UFSMNT);
    573      1.1  mycroft 	free(fs, M_UFSMNT);
    574      1.1  mycroft 	free(ump, M_UFSMNT);
    575      1.1  mycroft 	mp->mnt_data = (qaddr_t)0;
    576      1.1  mycroft 	return (error);
    577      1.1  mycroft }
    578      1.1  mycroft 
    579      1.1  mycroft /*
    580      1.1  mycroft  * Flush out all the files in a filesystem.
    581      1.1  mycroft  */
    582      1.1  mycroft ffs_flushfiles(mp, flags, p)
    583      1.1  mycroft 	register struct mount *mp;
    584      1.1  mycroft 	int flags;
    585      1.1  mycroft 	struct proc *p;
    586      1.1  mycroft {
    587      1.1  mycroft 	register struct ufsmount *ump;
    588      1.1  mycroft 	int i, error;
    589      1.1  mycroft 
    590      1.1  mycroft 	ump = VFSTOUFS(mp);
    591      1.1  mycroft #ifdef QUOTA
    592      1.1  mycroft 	if (mp->mnt_flag & MNT_QUOTA) {
    593      1.1  mycroft 		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
    594      1.1  mycroft 			return (error);
    595      1.1  mycroft 		for (i = 0; i < MAXQUOTAS; i++) {
    596      1.1  mycroft 			if (ump->um_quotas[i] == NULLVP)
    597      1.1  mycroft 				continue;
    598      1.1  mycroft 			quotaoff(p, mp, i);
    599      1.1  mycroft 		}
    600      1.1  mycroft 		/*
    601      1.1  mycroft 		 * Here we fall through to vflush again to ensure
    602      1.1  mycroft 		 * that we have gotten rid of all the system vnodes.
    603      1.1  mycroft 		 */
    604      1.1  mycroft 	}
    605      1.1  mycroft #endif
    606      1.1  mycroft 	error = vflush(mp, NULLVP, flags);
    607      1.1  mycroft 	return (error);
    608      1.1  mycroft }
    609      1.1  mycroft 
    610      1.1  mycroft /*
    611      1.1  mycroft  * Get file system statistics.
    612      1.1  mycroft  */
    613      1.1  mycroft int
    614      1.1  mycroft ffs_statfs(mp, sbp, p)
    615      1.1  mycroft 	struct mount *mp;
    616      1.1  mycroft 	register struct statfs *sbp;
    617      1.1  mycroft 	struct proc *p;
    618      1.1  mycroft {
    619      1.1  mycroft 	register struct ufsmount *ump;
    620      1.1  mycroft 	register struct fs *fs;
    621      1.1  mycroft 
    622      1.1  mycroft 	ump = VFSTOUFS(mp);
    623      1.1  mycroft 	fs = ump->um_fs;
    624      1.1  mycroft 	if (fs->fs_magic != FS_MAGIC)
    625      1.1  mycroft 		panic("ffs_statfs");
    626      1.1  mycroft 	sbp->f_bsize = fs->fs_fsize;
    627      1.1  mycroft 	sbp->f_iosize = fs->fs_bsize;
    628      1.1  mycroft 	sbp->f_blocks = fs->fs_dsize;
    629      1.1  mycroft 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
    630      1.1  mycroft 		fs->fs_cstotal.cs_nffree;
    631      1.1  mycroft 	sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
    632      1.1  mycroft 		(fs->fs_dsize - sbp->f_bfree);
    633      1.1  mycroft 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
    634      1.1  mycroft 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
    635      1.1  mycroft 	if (sbp != &mp->mnt_stat) {
    636  1.1.1.2     fvdl 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
    637      1.1  mycroft 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
    638      1.1  mycroft 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
    639      1.1  mycroft 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
    640      1.1  mycroft 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
    641      1.1  mycroft 	}
    642      1.1  mycroft 	return (0);
    643      1.1  mycroft }
    644      1.1  mycroft 
    645      1.1  mycroft /*
    646      1.1  mycroft  * Go through the disk queues to initiate sandbagged IO;
    647      1.1  mycroft  * go through the inodes to write those that have been modified;
    648      1.1  mycroft  * initiate the writing of the super block if it has been modified.
    649      1.1  mycroft  *
    650      1.1  mycroft  * Note: we are always called with the filesystem marked `MPBUSY'.
    651      1.1  mycroft  */
    652      1.1  mycroft int
    653      1.1  mycroft ffs_sync(mp, waitfor, cred, p)
    654      1.1  mycroft 	struct mount *mp;
    655      1.1  mycroft 	int waitfor;
    656      1.1  mycroft 	struct ucred *cred;
    657      1.1  mycroft 	struct proc *p;
    658      1.1  mycroft {
    659  1.1.1.2     fvdl 	struct vnode *nvp, *vp;
    660  1.1.1.2     fvdl 	struct inode *ip;
    661  1.1.1.2     fvdl 	struct ufsmount *ump = VFSTOUFS(mp);
    662  1.1.1.2     fvdl 	struct fs *fs;
    663      1.1  mycroft 	int error, allerror = 0;
    664      1.1  mycroft 
    665      1.1  mycroft 	fs = ump->um_fs;
    666  1.1.1.2     fvdl 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
    667  1.1.1.2     fvdl 		printf("fs = %s\n", fs->fs_fsmnt);
    668  1.1.1.2     fvdl 		panic("update: rofs mod");
    669      1.1  mycroft 	}
    670      1.1  mycroft 	/*
    671      1.1  mycroft 	 * Write back each (modified) inode.
    672      1.1  mycroft 	 */
    673  1.1.1.2     fvdl 	simple_lock(&mntvnode_slock);
    674      1.1  mycroft loop:
    675      1.1  mycroft 	for (vp = mp->mnt_vnodelist.lh_first;
    676      1.1  mycroft 	     vp != NULL;
    677  1.1.1.2     fvdl 	     vp = nvp) {
    678      1.1  mycroft 		/*
    679      1.1  mycroft 		 * If the vnode that we are about to sync is no longer
    680      1.1  mycroft 		 * associated with this mount point, start over.
    681      1.1  mycroft 		 */
    682      1.1  mycroft 		if (vp->v_mount != mp)
    683      1.1  mycroft 			goto loop;
    684  1.1.1.2     fvdl 		simple_lock(&vp->v_interlock);
    685  1.1.1.2     fvdl 		nvp = vp->v_mntvnodes.le_next;
    686      1.1  mycroft 		ip = VTOI(vp);
    687      1.1  mycroft 		if ((ip->i_flag &
    688      1.1  mycroft 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
    689  1.1.1.2     fvdl 		    vp->v_dirtyblkhd.lh_first == NULL) {
    690  1.1.1.2     fvdl 			simple_unlock(&vp->v_interlock);
    691      1.1  mycroft 			continue;
    692  1.1.1.2     fvdl 		}
    693  1.1.1.2     fvdl 		simple_unlock(&mntvnode_slock);
    694  1.1.1.2     fvdl 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
    695  1.1.1.2     fvdl 		if (error) {
    696  1.1.1.2     fvdl 			simple_lock(&mntvnode_slock);
    697  1.1.1.2     fvdl 			if (error == ENOENT)
    698  1.1.1.2     fvdl 				goto loop;
    699  1.1.1.2     fvdl 			continue;
    700  1.1.1.2     fvdl 		}
    701      1.1  mycroft 		if (error = VOP_FSYNC(vp, cred, waitfor, p))
    702      1.1  mycroft 			allerror = error;
    703  1.1.1.2     fvdl 		VOP_UNLOCK(vp, 0, p);
    704  1.1.1.2     fvdl 		vrele(vp);
    705  1.1.1.2     fvdl 		simple_lock(&mntvnode_slock);
    706      1.1  mycroft 	}
    707  1.1.1.2     fvdl 	simple_unlock(&mntvnode_slock);
    708      1.1  mycroft 	/*
    709      1.1  mycroft 	 * Force stale file system control information to be flushed.
    710      1.1  mycroft 	 */
    711      1.1  mycroft 	if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
    712      1.1  mycroft 		allerror = error;
    713      1.1  mycroft #ifdef QUOTA
    714      1.1  mycroft 	qsync(mp);
    715      1.1  mycroft #endif
    716  1.1.1.2     fvdl 	/*
    717  1.1.1.2     fvdl 	 * Write back modified superblock.
    718  1.1.1.2     fvdl 	 */
    719  1.1.1.2     fvdl 	if (fs->fs_fmod != 0) {
    720  1.1.1.2     fvdl 		fs->fs_fmod = 0;
    721  1.1.1.2     fvdl 		fs->fs_time = time.tv_sec;
    722  1.1.1.2     fvdl 		if (error = ffs_sbupdate(ump, waitfor))
    723  1.1.1.2     fvdl 			allerror = error;
    724  1.1.1.2     fvdl 	}
    725      1.1  mycroft 	return (allerror);
    726      1.1  mycroft }
    727      1.1  mycroft 
    728      1.1  mycroft /*
    729      1.1  mycroft  * Look up a FFS dinode number to find its incore vnode, otherwise read it
    730      1.1  mycroft  * in from disk.  If it is in core, wait for the lock bit to clear, then
    731      1.1  mycroft  * return the inode locked.  Detection and handling of mount points must be
    732      1.1  mycroft  * done by the calling routine.
    733      1.1  mycroft  */
    734      1.1  mycroft int
    735      1.1  mycroft ffs_vget(mp, ino, vpp)
    736      1.1  mycroft 	struct mount *mp;
    737      1.1  mycroft 	ino_t ino;
    738      1.1  mycroft 	struct vnode **vpp;
    739      1.1  mycroft {
    740  1.1.1.2     fvdl 	struct proc *p = curproc;		/* XXX */
    741  1.1.1.2     fvdl 	struct fs *fs;
    742  1.1.1.2     fvdl 	struct inode *ip;
    743      1.1  mycroft 	struct ufsmount *ump;
    744      1.1  mycroft 	struct buf *bp;
    745      1.1  mycroft 	struct vnode *vp;
    746      1.1  mycroft 	dev_t dev;
    747      1.1  mycroft 	int i, type, error;
    748      1.1  mycroft 
    749      1.1  mycroft 	ump = VFSTOUFS(mp);
    750      1.1  mycroft 	dev = ump->um_dev;
    751      1.1  mycroft 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
    752      1.1  mycroft 		return (0);
    753      1.1  mycroft 
    754      1.1  mycroft 	/* Allocate a new vnode/inode. */
    755      1.1  mycroft 	if (error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) {
    756      1.1  mycroft 		*vpp = NULL;
    757      1.1  mycroft 		return (error);
    758      1.1  mycroft 	}
    759      1.1  mycroft 	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
    760      1.1  mycroft 	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
    761      1.1  mycroft 	bzero((caddr_t)ip, sizeof(struct inode));
    762  1.1.1.2     fvdl 	lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
    763      1.1  mycroft 	vp->v_data = ip;
    764      1.1  mycroft 	ip->i_vnode = vp;
    765      1.1  mycroft 	ip->i_fs = fs = ump->um_fs;
    766      1.1  mycroft 	ip->i_dev = dev;
    767      1.1  mycroft 	ip->i_number = ino;
    768      1.1  mycroft #ifdef QUOTA
    769      1.1  mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    770      1.1  mycroft 		ip->i_dquot[i] = NODQUOT;
    771      1.1  mycroft #endif
    772      1.1  mycroft 	/*
    773      1.1  mycroft 	 * Put it onto its hash chain and lock it so that other requests for
    774      1.1  mycroft 	 * this inode will block if they arrive while we are sleeping waiting
    775      1.1  mycroft 	 * for old data structures to be purged or for the contents of the
    776      1.1  mycroft 	 * disk portion of this inode to be read.
    777      1.1  mycroft 	 */
    778      1.1  mycroft 	ufs_ihashins(ip);
    779      1.1  mycroft 
    780      1.1  mycroft 	/* Read in the disk contents for the inode, copy into the inode. */
    781      1.1  mycroft 	if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
    782      1.1  mycroft 	    (int)fs->fs_bsize, NOCRED, &bp)) {
    783      1.1  mycroft 		/*
    784      1.1  mycroft 		 * The inode does not contain anything useful, so it would
    785      1.1  mycroft 		 * be misleading to leave it on its hash chain. With mode
    786      1.1  mycroft 		 * still zero, it will be unlinked and returned to the free
    787      1.1  mycroft 		 * list by vput().
    788      1.1  mycroft 		 */
    789      1.1  mycroft 		vput(vp);
    790      1.1  mycroft 		brelse(bp);
    791      1.1  mycroft 		*vpp = NULL;
    792      1.1  mycroft 		return (error);
    793      1.1  mycroft 	}
    794      1.1  mycroft 	ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
    795      1.1  mycroft 	brelse(bp);
    796      1.1  mycroft 
    797      1.1  mycroft 	/*
    798      1.1  mycroft 	 * Initialize the vnode from the inode, check for aliases.
    799      1.1  mycroft 	 * Note that the underlying vnode may have changed.
    800      1.1  mycroft 	 */
    801      1.1  mycroft 	if (error = ufs_vinit(mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
    802      1.1  mycroft 		vput(vp);
    803      1.1  mycroft 		*vpp = NULL;
    804      1.1  mycroft 		return (error);
    805      1.1  mycroft 	}
    806      1.1  mycroft 	/*
    807      1.1  mycroft 	 * Finish inode initialization now that aliasing has been resolved.
    808      1.1  mycroft 	 */
    809      1.1  mycroft 	ip->i_devvp = ump->um_devvp;
    810      1.1  mycroft 	VREF(ip->i_devvp);
    811      1.1  mycroft 	/*
    812      1.1  mycroft 	 * Set up a generation number for this inode if it does not
    813      1.1  mycroft 	 * already have one. This should only happen on old filesystems.
    814      1.1  mycroft 	 */
    815      1.1  mycroft 	if (ip->i_gen == 0) {
    816      1.1  mycroft 		if (++nextgennumber < (u_long)time.tv_sec)
    817      1.1  mycroft 			nextgennumber = time.tv_sec;
    818      1.1  mycroft 		ip->i_gen = nextgennumber;
    819      1.1  mycroft 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    820      1.1  mycroft 			ip->i_flag |= IN_MODIFIED;
    821      1.1  mycroft 	}
    822      1.1  mycroft 	/*
    823      1.1  mycroft 	 * Ensure that uid and gid are correct. This is a temporary
    824      1.1  mycroft 	 * fix until fsck has been changed to do the update.
    825      1.1  mycroft 	 */
    826      1.1  mycroft 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
    827      1.1  mycroft 		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
    828      1.1  mycroft 		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
    829      1.1  mycroft 	}						/* XXX */
    830      1.1  mycroft 
    831      1.1  mycroft 	*vpp = vp;
    832      1.1  mycroft 	return (0);
    833      1.1  mycroft }
    834      1.1  mycroft 
    835      1.1  mycroft /*
    836      1.1  mycroft  * File handle to vnode
    837      1.1  mycroft  *
    838      1.1  mycroft  * Have to be really careful about stale file handles:
    839      1.1  mycroft  * - check that the inode number is valid
    840      1.1  mycroft  * - call ffs_vget() to get the locked inode
    841      1.1  mycroft  * - check for an unallocated inode (i_mode == 0)
    842      1.1  mycroft  * - check that the given client host has export rights and return
    843      1.1  mycroft  *   those rights via. exflagsp and credanonp
    844      1.1  mycroft  */
    845      1.1  mycroft int
    846      1.1  mycroft ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
    847      1.1  mycroft 	register struct mount *mp;
    848      1.1  mycroft 	struct fid *fhp;
    849      1.1  mycroft 	struct mbuf *nam;
    850      1.1  mycroft 	struct vnode **vpp;
    851      1.1  mycroft 	int *exflagsp;
    852      1.1  mycroft 	struct ucred **credanonp;
    853      1.1  mycroft {
    854      1.1  mycroft 	register struct ufid *ufhp;
    855      1.1  mycroft 	struct fs *fs;
    856      1.1  mycroft 
    857      1.1  mycroft 	ufhp = (struct ufid *)fhp;
    858      1.1  mycroft 	fs = VFSTOUFS(mp)->um_fs;
    859      1.1  mycroft 	if (ufhp->ufid_ino < ROOTINO ||
    860      1.1  mycroft 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
    861      1.1  mycroft 		return (ESTALE);
    862      1.1  mycroft 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
    863      1.1  mycroft }
    864      1.1  mycroft 
    865      1.1  mycroft /*
    866      1.1  mycroft  * Vnode pointer to File handle
    867      1.1  mycroft  */
    868      1.1  mycroft /* ARGSUSED */
    869      1.1  mycroft ffs_vptofh(vp, fhp)
    870      1.1  mycroft 	struct vnode *vp;
    871      1.1  mycroft 	struct fid *fhp;
    872      1.1  mycroft {
    873      1.1  mycroft 	register struct inode *ip;
    874      1.1  mycroft 	register struct ufid *ufhp;
    875      1.1  mycroft 
    876      1.1  mycroft 	ip = VTOI(vp);
    877      1.1  mycroft 	ufhp = (struct ufid *)fhp;
    878      1.1  mycroft 	ufhp->ufid_len = sizeof(struct ufid);
    879      1.1  mycroft 	ufhp->ufid_ino = ip->i_number;
    880      1.1  mycroft 	ufhp->ufid_gen = ip->i_gen;
    881      1.1  mycroft 	return (0);
    882      1.1  mycroft }
    883      1.1  mycroft 
    884      1.1  mycroft /*
    885  1.1.1.2     fvdl  * Initialize the filesystem; just use ufs_init.
    886  1.1.1.2     fvdl  */
    887  1.1.1.2     fvdl int
    888  1.1.1.2     fvdl ffs_init(vfsp)
    889  1.1.1.2     fvdl 	struct vfsconf *vfsp;
    890  1.1.1.2     fvdl {
    891  1.1.1.2     fvdl 
    892  1.1.1.2     fvdl 	return (ufs_init(vfsp));
    893  1.1.1.2     fvdl }
    894  1.1.1.2     fvdl 
    895  1.1.1.2     fvdl /*
    896  1.1.1.2     fvdl  * fast filesystem related variables.
    897  1.1.1.2     fvdl  */
    898  1.1.1.2     fvdl ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    899  1.1.1.2     fvdl 	int *name;
    900  1.1.1.2     fvdl 	u_int namelen;
    901  1.1.1.2     fvdl 	void *oldp;
    902  1.1.1.2     fvdl 	size_t *oldlenp;
    903  1.1.1.2     fvdl 	void *newp;
    904  1.1.1.2     fvdl 	size_t newlen;
    905  1.1.1.2     fvdl 	struct proc *p;
    906  1.1.1.2     fvdl {
    907  1.1.1.2     fvdl 	extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
    908  1.1.1.2     fvdl 
    909  1.1.1.2     fvdl 	/* all sysctl names at this level are terminal */
    910  1.1.1.2     fvdl 	if (namelen != 1)
    911  1.1.1.2     fvdl 		return (ENOTDIR);		/* overloaded */
    912  1.1.1.2     fvdl 
    913  1.1.1.2     fvdl 	switch (name[0]) {
    914  1.1.1.2     fvdl 	case FFS_CLUSTERREAD:
    915  1.1.1.2     fvdl 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    916  1.1.1.2     fvdl 		    &doclusterread));
    917  1.1.1.2     fvdl 	case FFS_CLUSTERWRITE:
    918  1.1.1.2     fvdl 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    919  1.1.1.2     fvdl 		    &doclusterwrite));
    920  1.1.1.2     fvdl 	case FFS_REALLOCBLKS:
    921  1.1.1.2     fvdl 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    922  1.1.1.2     fvdl 		    &doreallocblks));
    923  1.1.1.2     fvdl 	case FFS_ASYNCFREE:
    924  1.1.1.2     fvdl 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
    925  1.1.1.2     fvdl 	default:
    926  1.1.1.2     fvdl 		return (EOPNOTSUPP);
    927  1.1.1.2     fvdl 	}
    928  1.1.1.2     fvdl 	/* NOTREACHED */
    929  1.1.1.2     fvdl }
    930  1.1.1.2     fvdl 
    931  1.1.1.2     fvdl /*
    932      1.1  mycroft  * Write a superblock and associated information back to disk.
    933      1.1  mycroft  */
    934      1.1  mycroft int
    935      1.1  mycroft ffs_sbupdate(mp, waitfor)
    936      1.1  mycroft 	struct ufsmount *mp;
    937      1.1  mycroft 	int waitfor;
    938      1.1  mycroft {
    939  1.1.1.2     fvdl 	register struct fs *dfs, *fs = mp->um_fs;
    940      1.1  mycroft 	register struct buf *bp;
    941      1.1  mycroft 	int blks;
    942      1.1  mycroft 	caddr_t space;
    943  1.1.1.2     fvdl 	int i, size, error, allerror = 0;
    944      1.1  mycroft 
    945  1.1.1.2     fvdl 	/*
    946  1.1.1.2     fvdl 	 * First write back the summary information.
    947  1.1.1.2     fvdl 	 */
    948      1.1  mycroft 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    949      1.1  mycroft 	space = (caddr_t)fs->fs_csp[0];
    950      1.1  mycroft 	for (i = 0; i < blks; i += fs->fs_frag) {
    951      1.1  mycroft 		size = fs->fs_bsize;
    952      1.1  mycroft 		if (i + fs->fs_frag > blks)
    953      1.1  mycroft 			size = (blks - i) * fs->fs_fsize;
    954      1.1  mycroft 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
    955      1.1  mycroft 		    size, 0, 0);
    956      1.1  mycroft 		bcopy(space, bp->b_data, (u_int)size);
    957      1.1  mycroft 		space += size;
    958  1.1.1.2     fvdl 		if (waitfor != MNT_WAIT)
    959      1.1  mycroft 			bawrite(bp);
    960  1.1.1.2     fvdl 		else if (error = bwrite(bp))
    961  1.1.1.2     fvdl 			allerror = error;
    962      1.1  mycroft 	}
    963  1.1.1.2     fvdl 	/*
    964  1.1.1.2     fvdl 	 * Now write back the superblock itself. If any errors occurred
    965  1.1.1.2     fvdl 	 * up to this point, then fail so that the superblock avoids
    966  1.1.1.2     fvdl 	 * being written out as clean.
    967  1.1.1.2     fvdl 	 */
    968  1.1.1.2     fvdl 	if (allerror)
    969  1.1.1.2     fvdl 		return (allerror);
    970  1.1.1.2     fvdl 	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
    971  1.1.1.2     fvdl 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
    972  1.1.1.2     fvdl 	/* Restore compatibility to old file systems.		   XXX */
    973  1.1.1.2     fvdl 	dfs = (struct fs *)bp->b_data;				/* XXX */
    974  1.1.1.2     fvdl 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    975  1.1.1.2     fvdl 		dfs->fs_nrpos = -1;				/* XXX */
    976  1.1.1.2     fvdl 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    977  1.1.1.2     fvdl 		int32_t *lp, tmp;				/* XXX */
    978  1.1.1.2     fvdl 								/* XXX */
    979  1.1.1.2     fvdl 		lp = (int32_t *)&dfs->fs_qbmask;		/* XXX */
    980  1.1.1.2     fvdl 		tmp = lp[4];					/* XXX */
    981  1.1.1.2     fvdl 		for (i = 4; i > 0; i--)				/* XXX */
    982  1.1.1.2     fvdl 			lp[i] = lp[i-1];			/* XXX */
    983  1.1.1.2     fvdl 		lp[0] = tmp;					/* XXX */
    984  1.1.1.2     fvdl 	}							/* XXX */
    985  1.1.1.2     fvdl 	dfs->fs_maxfilesize = mp->um_savedmaxfilesize;		/* XXX */
    986  1.1.1.2     fvdl 	if (waitfor != MNT_WAIT)
    987  1.1.1.2     fvdl 		bawrite(bp);
    988  1.1.1.2     fvdl 	else if (error = bwrite(bp))
    989  1.1.1.2     fvdl 		allerror = error;
    990  1.1.1.2     fvdl 	return (allerror);
    991      1.1  mycroft }
    992