Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.67.2.7
      1 /*	$NetBSD: ffs_vfsops.c,v 1.67.2.7 2001/11/25 20:00:26 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     36  */
     37 
     38 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_ffs.h"
     40 #include "opt_quota.h"
     41 #include "opt_compat_netbsd.h"
     42 #include "opt_softdep.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/namei.h>
     48 #include <sys/proc.h>
     49 #include <sys/kernel.h>
     50 #include <sys/vnode.h>
     51 #include <sys/socket.h>
     52 #include <sys/mount.h>
     53 #include <sys/buf.h>
     54 #include <sys/device.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/file.h>
     57 #include <sys/disklabel.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/errno.h>
     60 #include <sys/malloc.h>
     61 #include <sys/pool.h>
     62 #include <sys/lock.h>
     63 #include <vm/vm.h>
     64 #include <sys/sysctl.h>
     65 
     66 #include <miscfs/specfs/specdev.h>
     67 
     68 #include <ufs/ufs/quota.h>
     69 #include <ufs/ufs/ufsmount.h>
     70 #include <ufs/ufs/inode.h>
     71 #include <ufs/ufs/dir.h>
     72 #include <ufs/ufs/ufs_extern.h>
     73 #include <ufs/ufs/ufs_bswap.h>
     74 
     75 #include <ufs/ffs/fs.h>
     76 #include <ufs/ffs/ffs_extern.h>
     77 
     78 /* how many times ffs_init() was called */
     79 int ffs_initcount = 0;
     80 
     81 extern struct lock ufs_hashlock;
     82 
     83 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
     84 extern struct vnodeopv_desc ffs_specop_opv_desc;
     85 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
     86 
     87 struct vnodeopv_desc *ffs_vnodeopv_descs[] = {
     88 	&ffs_vnodeop_opv_desc,
     89 	&ffs_specop_opv_desc,
     90 	&ffs_fifoop_opv_desc,
     91 	NULL,
     92 };
     93 
     94 struct vfsops ffs_vfsops = {
     95 	MOUNT_FFS,
     96 	ffs_mount,
     97 	ufs_start,
     98 	ffs_unmount,
     99 	ufs_root,
    100 	ufs_quotactl,
    101 	ffs_statfs,
    102 	ffs_sync,
    103 	ffs_vget,
    104 	ffs_fhtovp,
    105 	ffs_vptofh,
    106 	ffs_init,
    107 	ffs_done,
    108 	ffs_sysctl,
    109 	ffs_mountroot,
    110 	ufs_check_export,
    111 	ffs_vnodeopv_descs,
    112 };
    113 
    114 struct pool ffs_inode_pool;
    115 
    116 /*
    117  * Called by main() when ffs is going to be mounted as root.
    118  */
    119 
    120 int
    121 ffs_mountroot()
    122 {
    123 	struct fs *fs;
    124 	struct mount *mp;
    125 	struct proc *p = curproc;	/* XXX */
    126 	struct ufsmount *ump;
    127 	int error;
    128 
    129 	if (root_device->dv_class != DV_DISK)
    130 		return (ENODEV);
    131 
    132 	/*
    133 	 * Get vnodes for rootdev.
    134 	 */
    135 	if (bdevvp(rootdev, &rootvp))
    136 		panic("ffs_mountroot: can't setup bdevvp's");
    137 
    138 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    139 		vrele(rootvp);
    140 		return (error);
    141 	}
    142 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    143 		mp->mnt_op->vfs_refcount--;
    144 		vfs_unbusy(mp);
    145 		free(mp, M_MOUNT);
    146 		vrele(rootvp);
    147 		return (error);
    148 	}
    149 	simple_lock(&mountlist_slock);
    150 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    151 	simple_unlock(&mountlist_slock);
    152 	ump = VFSTOUFS(mp);
    153 	fs = ump->um_fs;
    154 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    155 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    156 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    157 	vfs_unbusy(mp);
    158 	inittodr(fs->fs_time);
    159 	return (0);
    160 }
    161 
    162 /*
    163  * VFS Operations.
    164  *
    165  * mount system call
    166  */
    167 int
    168 ffs_mount(mp, path, data, ndp, p)
    169 	struct mount *mp;
    170 	const char *path;
    171 	void *data;
    172 	struct nameidata *ndp;
    173 	struct proc *p;
    174 {
    175 	struct vnode *devvp;
    176 	struct ufs_args args;
    177 	struct ufsmount *ump = NULL;
    178 	struct fs *fs;
    179 	size_t size;
    180 	int error, flags;
    181 	mode_t accessmode;
    182 
    183 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    184 	if (error)
    185 		return (error);
    186 
    187 #if !defined(SOFTDEP)
    188 	mp->mnt_flag &= ~MNT_SOFTDEP;
    189 #endif
    190 
    191 	/*
    192 	 * If updating, check whether changing from read-only to
    193 	 * read/write; if there is no device name, that's all we do.
    194 	 */
    195 	if (mp->mnt_flag & MNT_UPDATE) {
    196 		ump = VFSTOUFS(mp);
    197 		fs = ump->um_fs;
    198 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    199 			flags = WRITECLOSE;
    200 			if (mp->mnt_flag & MNT_FORCE)
    201 				flags |= FORCECLOSE;
    202 			if (mp->mnt_flag & MNT_SOFTDEP)
    203 				error = softdep_flushfiles(mp, flags, p);
    204 			else
    205 				error = ffs_flushfiles(mp, flags, p);
    206 			if (error == 0 &&
    207 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    208 			    fs->fs_clean & FS_WASCLEAN) {
    209 				if (mp->mnt_flag & MNT_SOFTDEP)
    210 					fs->fs_flags &= ~FS_DOSOFTDEP;
    211 				fs->fs_clean = FS_ISCLEAN;
    212 				(void) ffs_sbupdate(ump, MNT_WAIT);
    213 			}
    214 			if (error)
    215 				return (error);
    216 			fs->fs_ronly = 1;
    217 			fs->fs_fmod = 0;
    218 		}
    219 
    220 		/*
    221 		 * Flush soft dependencies if disabling it via an update
    222 		 * mount. This may leave some items to be processed,
    223 		 * so don't do this yet XXX.
    224 		 */
    225 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    226 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    227 #ifdef notyet
    228 			flags = WRITECLOSE;
    229 			if (mp->mnt_flag & MNT_FORCE)
    230 				flags |= FORCECLOSE;
    231 			error = softdep_flushfiles(mp, flags, p);
    232 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    233 				fs->fs_flags &= ~FS_DOSOFTDEP;
    234 				(void) ffs_sbupdate(ump, MNT_WAIT);
    235 #elif defined(SOFTDEP)
    236 			mp->mnt_flag |= MNT_SOFTDEP;
    237 #endif
    238 		}
    239 
    240 		/*
    241 		 * When upgrading to a softdep mount, we must first flush
    242 		 * all vnodes. (not done yet -- see above)
    243 		 */
    244 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    245 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    246 #ifdef notyet
    247 			flags = WRITECLOSE;
    248 			if (mp->mnt_flag & MNT_FORCE)
    249 				flags |= FORCECLOSE;
    250 			error = ffs_flushfiles(mp, flags, p);
    251 #else
    252 			mp->mnt_flag &= ~MNT_SOFTDEP;
    253 #endif
    254 		}
    255 
    256 		if (mp->mnt_flag & MNT_RELOAD) {
    257 			error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
    258 			if (error)
    259 				return (error);
    260 		}
    261 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    262 			/*
    263 			 * If upgrade to read-write by non-root, then verify
    264 			 * that user has necessary permissions on the device.
    265 			 */
    266 			devvp = ump->um_devvp;
    267 			if (p->p_ucred->cr_uid != 0) {
    268 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    269 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
    270 						   p->p_ucred, p);
    271 				VOP_UNLOCK(devvp, 0);
    272 				if (error)
    273 					return (error);
    274 			}
    275 			fs->fs_ronly = 0;
    276 			fs->fs_clean <<= 1;
    277 			fs->fs_fmod = 1;
    278 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    279 				error = softdep_mount(devvp, mp, fs,
    280 				    p->p_ucred);
    281 				if (error)
    282 					return (error);
    283 			}
    284 		}
    285 		if (args.fspec == 0) {
    286 			/*
    287 			 * Process export requests.
    288 			 */
    289 			return (vfs_export(mp, &ump->um_export, &args.export));
    290 		}
    291 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    292 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    293 			printf("%s fs uses soft updates, ignoring async mode\n",
    294 			    fs->fs_fsmnt);
    295 			mp->mnt_flag &= ~MNT_ASYNC;
    296 		}
    297 	}
    298 	/*
    299 	 * Not an update, or updating the name: look up the name
    300 	 * and verify that it refers to a sensible block device.
    301 	 */
    302 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    303 	if ((error = namei(ndp)) != 0)
    304 		return (error);
    305 	devvp = ndp->ni_vp;
    306 
    307 	if (devvp->v_type != VBLK) {
    308 		vrele(devvp);
    309 		return (ENOTBLK);
    310 	}
    311 	if (major(devvp->v_rdev) >= nblkdev) {
    312 		vrele(devvp);
    313 		return (ENXIO);
    314 	}
    315 	/*
    316 	 * If mount by non-root, then verify that user has necessary
    317 	 * permissions on the device.
    318 	 */
    319 	if (p->p_ucred->cr_uid != 0) {
    320 		accessmode = VREAD;
    321 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    322 			accessmode |= VWRITE;
    323 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    324 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    325 		VOP_UNLOCK(devvp, 0);
    326 		if (error) {
    327 			vrele(devvp);
    328 			return (error);
    329 		}
    330 	}
    331 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
    332 		error = ffs_mountfs(devvp, mp, p);
    333 		if (!error) {
    334 			ump = VFSTOUFS(mp);
    335 			fs = ump->um_fs;
    336 			if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    337 			    (MNT_SOFTDEP | MNT_ASYNC)) {
    338 				printf("%s fs uses soft updates, "
    339 				       "ignoring async mode\n",
    340 				    fs->fs_fsmnt);
    341 				mp->mnt_flag &= ~MNT_ASYNC;
    342 			}
    343 		}
    344 	}
    345 	else {
    346 		if (devvp != ump->um_devvp)
    347 			error = EINVAL;	/* needs translation */
    348 		else
    349 			vrele(devvp);
    350 	}
    351 	if (error) {
    352 		vrele(devvp);
    353 		return (error);
    354 	}
    355 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    356 	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
    357 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
    358 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    359 	    &size);
    360 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    361 	if (mp->mnt_flag & MNT_SOFTDEP)
    362 		fs->fs_flags |= FS_DOSOFTDEP;
    363 	if (fs->fs_fmod != 0) {	/* XXX */
    364 		fs->fs_fmod = 0;
    365 		if (fs->fs_clean & FS_WASCLEAN)
    366 			fs->fs_time = time.tv_sec;
    367 		else
    368 			printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
    369 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    370 		(void) ffs_cgupdate(ump, MNT_WAIT);
    371 	}
    372 	return (0);
    373 }
    374 
    375 /*
    376  * Reload all incore data for a filesystem (used after running fsck on
    377  * the root filesystem and finding things to fix). The filesystem must
    378  * be mounted read-only.
    379  *
    380  * Things to do to update the mount:
    381  *	1) invalidate all cached meta-data.
    382  *	2) re-read superblock from disk.
    383  *	3) re-read summary information from disk.
    384  *	4) invalidate all inactive vnodes.
    385  *	5) invalidate all cached file data.
    386  *	6) re-read inode data for all active vnodes.
    387  */
    388 int
    389 ffs_reload(mountp, cred, p)
    390 	struct mount *mountp;
    391 	struct ucred *cred;
    392 	struct proc *p;
    393 {
    394 	struct vnode *vp, *nvp, *devvp;
    395 	struct inode *ip;
    396 	void *space;
    397 	struct buf *bp;
    398 	struct fs *fs, *newfs;
    399 	struct partinfo dpart;
    400 	int i, blks, size, error;
    401 	int32_t *lp;
    402 	caddr_t cp;
    403 
    404 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    405 		return (EINVAL);
    406 	/*
    407 	 * Step 1: invalidate all cached meta-data.
    408 	 */
    409 	devvp = VFSTOUFS(mountp)->um_devvp;
    410 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    411 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    412 	VOP_UNLOCK(devvp, 0);
    413 	if (error)
    414 		panic("ffs_reload: dirty1");
    415 	/*
    416 	 * Step 2: re-read superblock from disk.
    417 	 */
    418 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
    419 		size = DEV_BSIZE;
    420 	else
    421 		size = dpart.disklab->d_secsize;
    422 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    423 	if (error) {
    424 		brelse(bp);
    425 		return (error);
    426 	}
    427 	fs = VFSTOUFS(mountp)->um_fs;
    428 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    429 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    430 #ifdef FFS_EI
    431 	if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP) {
    432 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    433 		fs->fs_flags |= FS_SWAPPED;
    434 	}
    435 #endif
    436 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
    437 	    newfs->fs_bsize < sizeof(struct fs)) {
    438 		brelse(bp);
    439 		free(newfs, M_UFSMNT);
    440 		return (EIO);		/* XXX needs translation */
    441 	}
    442 	/*
    443 	 * Copy pointer fields back into superblock before copying in	XXX
    444 	 * new superblock. These should really be in the ufsmount.	XXX
    445 	 * Note that important parameters (eg fs_ncg) are unchanged.
    446 	 */
    447 	newfs->fs_csp = fs->fs_csp;
    448 	newfs->fs_maxcluster = fs->fs_maxcluster;
    449 	newfs->fs_contigdirs = fs->fs_contigdirs;
    450 	newfs->fs_ronly = fs->fs_ronly;
    451 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    452 	if (fs->fs_sbsize < SBSIZE)
    453 		bp->b_flags |= B_INVAL;
    454 	brelse(bp);
    455 	free(newfs, M_UFSMNT);
    456 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    457 	ffs_oldfscompat(fs);
    458 		/* An old fsck may have zeroed these fields, so recheck them. */
    459 	if (fs->fs_avgfilesize <= 0)
    460 		fs->fs_avgfilesize = AVFILESIZ;
    461 	if (fs->fs_avgfpdir <= 0)
    462 		fs->fs_avgfpdir = AFPDIR;
    463 
    464 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    465 	/*
    466 	 * Step 3: re-read summary information from disk.
    467 	 */
    468 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    469 	space = fs->fs_csp;
    470 	for (i = 0; i < blks; i += fs->fs_frag) {
    471 		size = fs->fs_bsize;
    472 		if (i + fs->fs_frag > blks)
    473 			size = (blks - i) * fs->fs_fsize;
    474 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    475 			      NOCRED, &bp);
    476 		if (error) {
    477 			brelse(bp);
    478 			return (error);
    479 		}
    480 #ifdef FFS_EI
    481 		if (UFS_FSNEEDSWAP(fs))
    482 			ffs_csum_swap((struct csum *)bp->b_data,
    483 			    (struct csum *)space, size);
    484 		else
    485 #endif
    486 			memcpy(space, bp->b_data, (size_t)size);
    487 		space = (char *)space + size;
    488 		brelse(bp);
    489 	}
    490 	if ((fs->fs_flags & FS_DOSOFTDEP))
    491 		softdep_mount(devvp, mountp, fs, cred);
    492 	/*
    493 	 * We no longer know anything about clusters per cylinder group.
    494 	 */
    495 	if (fs->fs_contigsumsize > 0) {
    496 		lp = fs->fs_maxcluster;
    497 		for (i = 0; i < fs->fs_ncg; i++)
    498 			*lp++ = fs->fs_contigsumsize;
    499 	}
    500 
    501 loop:
    502 	simple_lock(&mntvnode_slock);
    503 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    504 		if (vp->v_mount != mountp) {
    505 			simple_unlock(&mntvnode_slock);
    506 			goto loop;
    507 		}
    508 		nvp = vp->v_mntvnodes.le_next;
    509 		/*
    510 		 * Step 4: invalidate all inactive vnodes.
    511 		 */
    512 		if (vrecycle(vp, &mntvnode_slock, p))
    513 			goto loop;
    514 		/*
    515 		 * Step 5: invalidate all cached file data.
    516 		 */
    517 		simple_lock(&vp->v_interlock);
    518 		simple_unlock(&mntvnode_slock);
    519 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    520 			goto loop;
    521 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    522 			panic("ffs_reload: dirty2");
    523 		/*
    524 		 * Step 6: re-read inode data for all active vnodes.
    525 		 */
    526 		ip = VTOI(vp);
    527 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    528 			      (int)fs->fs_bsize, NOCRED, &bp);
    529 		if (error) {
    530 			brelse(bp);
    531 			vput(vp);
    532 			return (error);
    533 		}
    534 		cp = (caddr_t)bp->b_data +
    535 		    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    536 #ifdef FFS_EI
    537 		if (UFS_FSNEEDSWAP(fs))
    538 			ffs_dinode_swap((struct dinode *)cp,
    539 			    &ip->i_din.ffs_din);
    540 		else
    541 #endif
    542 			memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
    543 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
    544 		brelse(bp);
    545 		vput(vp);
    546 		simple_lock(&mntvnode_slock);
    547 	}
    548 	simple_unlock(&mntvnode_slock);
    549 	return (0);
    550 }
    551 
    552 /*
    553  * Common code for mount and mountroot
    554  */
    555 int
    556 ffs_mountfs(devvp, mp, p)
    557 	struct vnode *devvp;
    558 	struct mount *mp;
    559 	struct proc *p;
    560 {
    561 	struct ufsmount *ump;
    562 	struct buf *bp;
    563 	struct fs *fs;
    564 	dev_t dev;
    565 	struct partinfo dpart;
    566 	void *space;
    567 	int blks;
    568 	int error, i, size, ronly;
    569 #ifdef FFS_EI
    570 	int needswap;
    571 #endif
    572 	int32_t *lp;
    573 	struct ucred *cred;
    574 	u_int64_t maxfilesize;					/* XXX */
    575 	u_int32_t sbsize;
    576 
    577 	dev = devvp->v_rdev;
    578 	cred = p ? p->p_ucred : NOCRED;
    579 	/*
    580 	 * Disallow multiple mounts of the same device.
    581 	 * Disallow mounting of a device that is currently in use
    582 	 * (except for root, which might share swap device for miniroot).
    583 	 * Flush out any old buffers remaining from a previous use.
    584 	 */
    585 	if ((error = vfs_mountedon(devvp)) != 0)
    586 		return (error);
    587 	if (vcount(devvp) > 1 && devvp != rootvp)
    588 		return (EBUSY);
    589 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    590 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    591 	VOP_UNLOCK(devvp, 0);
    592 	if (error)
    593 		return (error);
    594 
    595 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    596 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    597 	if (error)
    598 		return (error);
    599 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    600 		size = DEV_BSIZE;
    601 	else
    602 		size = dpart.disklab->d_secsize;
    603 
    604 	bp = NULL;
    605 	ump = NULL;
    606 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
    607 	if (error)
    608 		goto out;
    609 
    610 	fs = (struct fs*)bp->b_data;
    611 	if (fs->fs_magic == FS_MAGIC) {
    612 		sbsize = fs->fs_sbsize;
    613 #ifdef FFS_EI
    614 		needswap = 0;
    615 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    616 		sbsize = bswap32(fs->fs_sbsize);
    617 		needswap = 1;
    618 #endif
    619 	} else {
    620 		error = EINVAL;
    621 		goto out;
    622 	}
    623 	if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
    624 		error = EINVAL;
    625 		goto out;
    626 	}
    627 
    628 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    629 	memcpy(fs, bp->b_data, sbsize);
    630 #ifdef FFS_EI
    631 	if (needswap) {
    632 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    633 		fs->fs_flags |= FS_SWAPPED;
    634 	}
    635 #endif
    636 	ffs_oldfscompat(fs);
    637 
    638 	if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    639 		error = EINVAL;
    640 		goto out;
    641 	}
    642 	 /* make sure cylinder group summary area is a reasonable size. */
    643 	if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
    644 	    fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
    645 	    fs->fs_cssize >
    646 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
    647 		error = EINVAL;		/* XXX needs translation */
    648 		goto out2;
    649 	}
    650 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    651 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    652 		error = EROFS;		/* XXX what should be returned? */
    653 		goto out2;
    654 	}
    655 
    656 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    657 	memset((caddr_t)ump, 0, sizeof *ump);
    658 	ump->um_fs = fs;
    659 	if (fs->fs_sbsize < SBSIZE)
    660 		bp->b_flags |= B_INVAL;
    661 	brelse(bp);
    662 	bp = NULL;
    663 	fs->fs_ronly = ronly;
    664 	if (ronly == 0) {
    665 		fs->fs_clean <<= 1;
    666 		fs->fs_fmod = 1;
    667 	}
    668 	size = fs->fs_cssize;
    669 	blks = howmany(size, fs->fs_fsize);
    670 	if (fs->fs_contigsumsize > 0)
    671 		size += fs->fs_ncg * sizeof(int32_t);
    672 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    673 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    674 	fs->fs_csp = space;
    675 	for (i = 0; i < blks; i += fs->fs_frag) {
    676 		size = fs->fs_bsize;
    677 		if (i + fs->fs_frag > blks)
    678 			size = (blks - i) * fs->fs_fsize;
    679 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    680 			      cred, &bp);
    681 		if (error) {
    682 			free(fs->fs_csp, M_UFSMNT);
    683 			goto out2;
    684 		}
    685 #ifdef FFS_EI
    686 		if (needswap)
    687 			ffs_csum_swap((struct csum *)bp->b_data,
    688 				(struct csum *)space, size);
    689 		else
    690 #endif
    691 			memcpy(space, bp->b_data, (u_int)size);
    692 
    693 		space = (char *)space + size;
    694 		brelse(bp);
    695 		bp = NULL;
    696 	}
    697 	if (fs->fs_contigsumsize > 0) {
    698 		fs->fs_maxcluster = lp = space;
    699 		for (i = 0; i < fs->fs_ncg; i++)
    700 			*lp++ = fs->fs_contigsumsize;
    701 		space = lp;
    702 	}
    703 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    704 	fs->fs_contigdirs = space;
    705 	space = (char *)space + size;
    706 	memset(fs->fs_contigdirs, 0, size);
    707 		/* Compatibility for old filesystems - XXX */
    708 	if (fs->fs_avgfilesize <= 0)
    709 		fs->fs_avgfilesize = AVFILESIZ;
    710 	if (fs->fs_avgfpdir <= 0)
    711 		fs->fs_avgfpdir = AFPDIR;
    712 	mp->mnt_data = (qaddr_t)ump;
    713 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    714 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    715 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    716 	mp->mnt_flag |= MNT_LOCAL;
    717 #ifdef FFS_EI
    718 	if (needswap)
    719 		ump->um_flags |= UFS_NEEDSWAP;
    720 #endif
    721 	ump->um_mountp = mp;
    722 	ump->um_dev = dev;
    723 	ump->um_devvp = devvp;
    724 	ump->um_nindir = fs->fs_nindir;
    725 	ump->um_bptrtodb = fs->fs_fsbtodb;
    726 	ump->um_seqinc = fs->fs_frag;
    727 	for (i = 0; i < MAXQUOTAS; i++)
    728 		ump->um_quotas[i] = NULLVP;
    729 	devvp->v_specmountpoint = mp;
    730 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    731 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;	/* XXX */
    732 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    733 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    734 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    735 		error = softdep_mount(devvp, mp, fs, cred);
    736 		if (error) {
    737 			free(fs->fs_csp, M_UFSMNT);
    738 			goto out;
    739 		}
    740 	}
    741 	return (0);
    742 out2:
    743 	free(fs, M_UFSMNT);
    744 out:
    745 	devvp->v_specmountpoint = NULL;
    746 	if (bp)
    747 		brelse(bp);
    748 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    749 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    750 	VOP_UNLOCK(devvp, 0);
    751 	if (ump) {
    752 		free(ump, M_UFSMNT);
    753 		mp->mnt_data = (qaddr_t)0;
    754 	}
    755 	return (error);
    756 }
    757 
    758 /*
    759  * Sanity checks for old file systems.
    760  *
    761  * XXX - goes away some day.
    762  */
    763 int
    764 ffs_oldfscompat(fs)
    765 	struct fs *fs;
    766 {
    767 	int i;
    768 
    769 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    770 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    771 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    772 		fs->fs_nrpos = 8;				/* XXX */
    773 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    774 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    775 								/* XXX */
    776 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    777 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    778 			sizepb *= NINDIR(fs);			/* XXX */
    779 			fs->fs_maxfilesize += sizepb;		/* XXX */
    780 		}						/* XXX */
    781 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    782 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    783 	}							/* XXX */
    784 	return (0);
    785 }
    786 
    787 /*
    788  * unmount system call
    789  */
    790 int
    791 ffs_unmount(mp, mntflags, p)
    792 	struct mount *mp;
    793 	int mntflags;
    794 	struct proc *p;
    795 {
    796 	struct ufsmount *ump;
    797 	struct fs *fs;
    798 	int error, flags;
    799 
    800 	flags = 0;
    801 	if (mntflags & MNT_FORCE)
    802 		flags |= FORCECLOSE;
    803 	if (mp->mnt_flag & MNT_SOFTDEP) {
    804 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
    805 			return (error);
    806 	} else {
    807 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
    808 			return (error);
    809 	}
    810 	ump = VFSTOUFS(mp);
    811 	fs = ump->um_fs;
    812 	if (fs->fs_ronly == 0 &&
    813 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    814 	    fs->fs_clean & FS_WASCLEAN) {
    815 		if (mp->mnt_flag & MNT_SOFTDEP)
    816 			fs->fs_flags &= ~FS_DOSOFTDEP;
    817 		fs->fs_clean = FS_ISCLEAN;
    818 		(void) ffs_sbupdate(ump, MNT_WAIT);
    819 	}
    820 	if (ump->um_devvp->v_type != VBAD)
    821 		ump->um_devvp->v_specmountpoint = NULL;
    822 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    823 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    824 		NOCRED, p);
    825 	vput(ump->um_devvp);
    826 	free(fs->fs_csp, M_UFSMNT);
    827 	free(fs, M_UFSMNT);
    828 	free(ump, M_UFSMNT);
    829 	mp->mnt_data = (qaddr_t)0;
    830 	mp->mnt_flag &= ~MNT_LOCAL;
    831 	return (error);
    832 }
    833 
    834 /*
    835  * Flush out all the files in a filesystem.
    836  */
    837 int
    838 ffs_flushfiles(mp, flags, p)
    839 	struct mount *mp;
    840 	int flags;
    841 	struct proc *p;
    842 {
    843 	extern int doforce;
    844 	struct ufsmount *ump;
    845 	int error;
    846 
    847 	if (!doforce)
    848 		flags &= ~FORCECLOSE;
    849 	ump = VFSTOUFS(mp);
    850 #ifdef QUOTA
    851 	if (mp->mnt_flag & MNT_QUOTA) {
    852 		int i;
    853 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    854 			return (error);
    855 		for (i = 0; i < MAXQUOTAS; i++) {
    856 			if (ump->um_quotas[i] == NULLVP)
    857 				continue;
    858 			quotaoff(p, mp, i);
    859 		}
    860 		/*
    861 		 * Here we fall through to vflush again to ensure
    862 		 * that we have gotten rid of all the system vnodes.
    863 		 */
    864 	}
    865 #endif
    866 	/*
    867 	 * Flush all the files.
    868 	 */
    869 	error = vflush(mp, NULLVP, flags);
    870 	if (error)
    871 		return (error);
    872 	/*
    873 	 * Flush filesystem metadata.
    874 	 */
    875 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    876 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
    877 	VOP_UNLOCK(ump->um_devvp, 0);
    878 	return (error);
    879 }
    880 
    881 /*
    882  * Get file system statistics.
    883  */
    884 int
    885 ffs_statfs(mp, sbp, p)
    886 	struct mount *mp;
    887 	struct statfs *sbp;
    888 	struct proc *p;
    889 {
    890 	struct ufsmount *ump;
    891 	struct fs *fs;
    892 
    893 	ump = VFSTOUFS(mp);
    894 	fs = ump->um_fs;
    895 	if (fs->fs_magic != FS_MAGIC)
    896 		panic("ffs_statfs");
    897 #ifdef COMPAT_09
    898 	sbp->f_type = 1;
    899 #else
    900 	sbp->f_type = 0;
    901 #endif
    902 	sbp->f_bsize = fs->fs_fsize;
    903 	sbp->f_iosize = fs->fs_bsize;
    904 	sbp->f_blocks = fs->fs_dsize;
    905 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
    906 		fs->fs_cstotal.cs_nffree;
    907 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
    908 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
    909 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
    910 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
    911 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
    912 	if (sbp != &mp->mnt_stat) {
    913 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    914 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    915 	}
    916 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    917 	return (0);
    918 }
    919 
    920 /*
    921  * Go through the disk queues to initiate sandbagged IO;
    922  * go through the inodes to write those that have been modified;
    923  * initiate the writing of the super block if it has been modified.
    924  *
    925  * Note: we are always called with the filesystem marked `MPBUSY'.
    926  */
    927 int
    928 ffs_sync(mp, waitfor, cred, p)
    929 	struct mount *mp;
    930 	int waitfor;
    931 	struct ucred *cred;
    932 	struct proc *p;
    933 {
    934 	struct vnode *vp, *nvp;
    935 	struct inode *ip;
    936 	struct ufsmount *ump = VFSTOUFS(mp);
    937 	struct fs *fs;
    938 	int error, allerror = 0;
    939 
    940 	fs = ump->um_fs;
    941 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
    942 		printf("fs = %s\n", fs->fs_fsmnt);
    943 		panic("update: rofs mod");
    944 	}
    945 	/*
    946 	 * Write back each (modified) inode.
    947 	 */
    948 	simple_lock(&mntvnode_slock);
    949 loop:
    950 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
    951 		/*
    952 		 * If the vnode that we are about to sync is no longer
    953 		 * associated with this mount point, start over.
    954 		 */
    955 		if (vp->v_mount != mp)
    956 			goto loop;
    957 		simple_lock(&vp->v_interlock);
    958 		nvp = LIST_NEXT(vp, v_mntvnodes);
    959 		ip = VTOI(vp);
    960 		if (vp->v_type == VNON ||
    961 		    ((ip->i_flag &
    962 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
    963 		     LIST_EMPTY(&vp->v_dirtyblkhd)))
    964 		{
    965 			simple_unlock(&vp->v_interlock);
    966 			continue;
    967 		}
    968 		simple_unlock(&mntvnode_slock);
    969 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    970 		if (error) {
    971 			simple_lock(&mntvnode_slock);
    972 			if (error == ENOENT)
    973 				goto loop;
    974 			continue;
    975 		}
    976 		if ((error = VOP_FSYNC(vp, cred,
    977 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    978 			allerror = error;
    979 		vput(vp);
    980 		simple_lock(&mntvnode_slock);
    981 	}
    982 	simple_unlock(&mntvnode_slock);
    983 	/*
    984 	 * Force stale file system control information to be flushed.
    985 	 */
    986 	if (waitfor != MNT_LAZY) {
    987 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
    988 			waitfor = MNT_NOWAIT;
    989 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    990 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    991 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    992 			allerror = error;
    993 		VOP_UNLOCK(ump->um_devvp, 0);
    994 	}
    995 #ifdef QUOTA
    996 	qsync(mp);
    997 #endif
    998 	/*
    999 	 * Write back modified superblock.
   1000 	 */
   1001 	if (fs->fs_fmod != 0) {
   1002 		fs->fs_fmod = 0;
   1003 		fs->fs_time = time.tv_sec;
   1004 		if ((error = ffs_cgupdate(ump, waitfor)))
   1005 			allerror = error;
   1006 	}
   1007 	return (allerror);
   1008 }
   1009 
   1010 /*
   1011  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1012  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1013  * return the inode locked.  Detection and handling of mount points must be
   1014  * done by the calling routine.
   1015  */
   1016 int
   1017 ffs_vget(mp, ino, vpp)
   1018 	struct mount *mp;
   1019 	ino_t ino;
   1020 	struct vnode **vpp;
   1021 {
   1022 	struct fs *fs;
   1023 	struct inode *ip;
   1024 	struct ufsmount *ump;
   1025 	struct buf *bp;
   1026 	struct vnode *vp;
   1027 	dev_t dev;
   1028 	int error;
   1029 	caddr_t cp;
   1030 
   1031 	ump = VFSTOUFS(mp);
   1032 	dev = ump->um_dev;
   1033 
   1034 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1035 		return (0);
   1036 
   1037 	/* Allocate a new vnode/inode. */
   1038 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1039 		*vpp = NULL;
   1040 		return (error);
   1041 	}
   1042 
   1043 	/*
   1044 	 * If someone beat us to it while sleeping in getnewvnode(),
   1045 	 * push back the freshly allocated vnode we don't need, and return.
   1046 	 */
   1047 	do {
   1048 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1049 			ungetnewvnode(vp);
   1050 			return (0);
   1051 		}
   1052 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1053 
   1054 	/*
   1055 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1056 	 * XXX create another pool for MFS inodes?
   1057 	 */
   1058 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1059 	memset((caddr_t)ip, 0, sizeof(struct inode));
   1060 	vp->v_data = ip;
   1061 	ip->i_vnode = vp;
   1062 	ip->i_fs = fs = ump->um_fs;
   1063 	ip->i_dev = dev;
   1064 	ip->i_number = ino;
   1065 #ifdef QUOTA
   1066 	{
   1067 		int i;
   1068 
   1069 		for (i = 0; i < MAXQUOTAS; i++)
   1070 			ip->i_dquot[i] = NODQUOT;
   1071 	}
   1072 #endif
   1073 	/*
   1074 	 * Put it onto its hash chain and lock it so that other requests for
   1075 	 * this inode will block if they arrive while we are sleeping waiting
   1076 	 * for old data structures to be purged or for the contents of the
   1077 	 * disk portion of this inode to be read.
   1078 	 */
   1079 	ufs_ihashins(ip);
   1080 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1081 
   1082 	/* Read in the disk contents for the inode, copy into the inode. */
   1083 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1084 		      (int)fs->fs_bsize, NOCRED, &bp);
   1085 	if (error) {
   1086 		/*
   1087 		 * The inode does not contain anything useful, so it would
   1088 		 * be misleading to leave it on its hash chain. With mode
   1089 		 * still zero, it will be unlinked and returned to the free
   1090 		 * list by vput().
   1091 		 */
   1092 		vput(vp);
   1093 		brelse(bp);
   1094 		*vpp = NULL;
   1095 		return (error);
   1096 	}
   1097 	cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
   1098 #ifdef FFS_EI
   1099 	if (UFS_FSNEEDSWAP(fs))
   1100 		ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
   1101 	else
   1102 #endif
   1103 		memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
   1104 	if (DOINGSOFTDEP(vp))
   1105 		softdep_load_inodeblock(ip);
   1106 	else
   1107 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1108 	brelse(bp);
   1109 
   1110 	/*
   1111 	 * Initialize the vnode from the inode, check for aliases.
   1112 	 * Note that the underlying vnode may have changed.
   1113 	 */
   1114 	error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1115 	if (error) {
   1116 		vput(vp);
   1117 		*vpp = NULL;
   1118 		return (error);
   1119 	}
   1120 	/*
   1121 	 * Finish inode initialization now that aliasing has been resolved.
   1122 	 */
   1123 	ip->i_devvp = ump->um_devvp;
   1124 	VREF(ip->i_devvp);
   1125 	/*
   1126 	 * Ensure that uid and gid are correct. This is a temporary
   1127 	 * fix until fsck has been changed to do the update.
   1128 	 */
   1129 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1130 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
   1131 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
   1132 	}							/* XXX */
   1133 
   1134 	*vpp = vp;
   1135 	return (0);
   1136 }
   1137 
   1138 /*
   1139  * File handle to vnode
   1140  *
   1141  * Have to be really careful about stale file handles:
   1142  * - check that the inode number is valid
   1143  * - call ffs_vget() to get the locked inode
   1144  * - check for an unallocated inode (i_mode == 0)
   1145  * - check that the given client host has export rights and return
   1146  *   those rights via. exflagsp and credanonp
   1147  */
   1148 int
   1149 ffs_fhtovp(mp, fhp, vpp)
   1150 	struct mount *mp;
   1151 	struct fid *fhp;
   1152 	struct vnode **vpp;
   1153 {
   1154 	struct ufid *ufhp;
   1155 	struct fs *fs;
   1156 
   1157 	ufhp = (struct ufid *)fhp;
   1158 	fs = VFSTOUFS(mp)->um_fs;
   1159 	if (ufhp->ufid_ino < ROOTINO ||
   1160 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1161 		return (ESTALE);
   1162 	return (ufs_fhtovp(mp, ufhp, vpp));
   1163 }
   1164 
   1165 /*
   1166  * Vnode pointer to File handle
   1167  */
   1168 /* ARGSUSED */
   1169 int
   1170 ffs_vptofh(vp, fhp)
   1171 	struct vnode *vp;
   1172 	struct fid *fhp;
   1173 {
   1174 	struct inode *ip;
   1175 	struct ufid *ufhp;
   1176 
   1177 	ip = VTOI(vp);
   1178 	ufhp = (struct ufid *)fhp;
   1179 	ufhp->ufid_len = sizeof(struct ufid);
   1180 	ufhp->ufid_ino = ip->i_number;
   1181 	ufhp->ufid_gen = ip->i_ffs_gen;
   1182 	return (0);
   1183 }
   1184 
   1185 void
   1186 ffs_init()
   1187 {
   1188 	if (ffs_initcount++ > 0)
   1189 		return;
   1190 
   1191 	softdep_initialize();
   1192 	ufs_init();
   1193 
   1194 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1195 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
   1196 }
   1197 
   1198 void
   1199 ffs_done()
   1200 {
   1201 	if (--ffs_initcount > 0)
   1202 		return;
   1203 
   1204 	/* XXX softdep cleanup ? */
   1205 	ufs_done();
   1206 	pool_destroy(&ffs_inode_pool);
   1207 }
   1208 
   1209 int
   1210 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1211 	int *name;
   1212 	u_int namelen;
   1213 	void *oldp;
   1214 	size_t *oldlenp;
   1215 	void *newp;
   1216 	size_t newlen;
   1217 	struct proc *p;
   1218 {
   1219 	extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
   1220 	extern int ffs_log_changeopt;
   1221 
   1222 	/* all sysctl names at this level are terminal */
   1223 	if (namelen != 1)
   1224 		return (ENOTDIR);		/* overloaded */
   1225 
   1226 	switch (name[0]) {
   1227 	case FFS_CLUSTERREAD:
   1228 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1229 		    &doclusterread));
   1230 	case FFS_CLUSTERWRITE:
   1231 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1232 		    &doclusterwrite));
   1233 	case FFS_REALLOCBLKS:
   1234 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1235 		    &doreallocblks));
   1236 	case FFS_ASYNCFREE:
   1237 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1238 	case FFS_LOG_CHANGEOPT:
   1239 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1240 			&ffs_log_changeopt));
   1241 	default:
   1242 		return (EOPNOTSUPP);
   1243 	}
   1244 	/* NOTREACHED */
   1245 }
   1246 
   1247 /*
   1248  * Write a superblock and associated information back to disk.
   1249  */
   1250 int
   1251 ffs_sbupdate(mp, waitfor)
   1252 	struct ufsmount *mp;
   1253 	int waitfor;
   1254 {
   1255 	struct fs *fs = mp->um_fs;
   1256 	struct buf *bp;
   1257 	int i, error = 0;
   1258 	int32_t saved_nrpos = fs->fs_nrpos;
   1259 	int64_t saved_qbmask = fs->fs_qbmask;
   1260 	int64_t saved_qfmask = fs->fs_qfmask;
   1261 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1262 	u_int8_t saveflag;
   1263 
   1264 	/* Restore compatibility to old file systems.		   XXX */
   1265 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1266 		fs->fs_nrpos = -1;		/* XXX */
   1267 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1268 		int32_t *lp, tmp;				/* XXX */
   1269 								/* XXX */
   1270 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1271 		tmp = lp[4];					/* XXX */
   1272 		for (i = 4; i > 0; i--)				/* XXX */
   1273 			lp[i] = lp[i-1];			/* XXX */
   1274 		lp[0] = tmp;					/* XXX */
   1275 	}							/* XXX */
   1276 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1277 
   1278 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1279 	    (int)fs->fs_sbsize, 0, 0);
   1280 	saveflag = fs->fs_flags & FS_INTERNAL;
   1281 	fs->fs_flags &= ~FS_INTERNAL;
   1282 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1283 #ifdef FFS_EI
   1284 	if (mp->um_flags & UFS_NEEDSWAP)
   1285 		ffs_sb_swap(fs, (struct fs*)bp->b_data);
   1286 #endif
   1287 
   1288 	fs->fs_flags |= saveflag;
   1289 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1290 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1291 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1292 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1293 
   1294 	if (waitfor == MNT_WAIT)
   1295 		error = bwrite(bp);
   1296 	else
   1297 		bawrite(bp);
   1298 	return (error);
   1299 }
   1300 
   1301 int
   1302 ffs_cgupdate(mp, waitfor)
   1303 	struct ufsmount *mp;
   1304 	int waitfor;
   1305 {
   1306 	struct fs *fs = mp->um_fs;
   1307 	struct buf *bp;
   1308 	int blks;
   1309 	void *space;
   1310 	int i, size, error = 0, allerror = 0;
   1311 
   1312 	allerror = ffs_sbupdate(mp, waitfor);
   1313 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1314 	space = fs->fs_csp;
   1315 	for (i = 0; i < blks; i += fs->fs_frag) {
   1316 		size = fs->fs_bsize;
   1317 		if (i + fs->fs_frag > blks)
   1318 			size = (blks - i) * fs->fs_fsize;
   1319 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1320 		    size, 0, 0);
   1321 #ifdef FFS_EI
   1322 		if (mp->um_flags & UFS_NEEDSWAP)
   1323 			ffs_csum_swap((struct csum*)space,
   1324 			    (struct csum*)bp->b_data, size);
   1325 		else
   1326 #endif
   1327 			memcpy(bp->b_data, space, (u_int)size);
   1328 		space = (char *)space + size;
   1329 		if (waitfor == MNT_WAIT)
   1330 			error = bwrite(bp);
   1331 		else
   1332 			bawrite(bp);
   1333 	}
   1334 	if (!allerror && error)
   1335 		allerror = error;
   1336 	return (allerror);
   1337 }
   1338