Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.144
      1 /*	$NetBSD: ffs_vfsops.c,v 1.144 2004/04/25 16:42:44 simonb 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.144 2004/04/25 16:42:44 simonb Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_ffs.h"
     39 #include "opt_quota.h"
     40 #include "opt_compat_netbsd.h"
     41 #include "opt_softdep.h"
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/namei.h>
     47 #include <sys/proc.h>
     48 #include <sys/kernel.h>
     49 #include <sys/vnode.h>
     50 #include <sys/socket.h>
     51 #include <sys/mount.h>
     52 #include <sys/buf.h>
     53 #include <sys/device.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/file.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/errno.h>
     59 #include <sys/malloc.h>
     60 #include <sys/pool.h>
     61 #include <sys/lock.h>
     62 #include <sys/sysctl.h>
     63 #include <sys/conf.h>
     64 
     65 #include <miscfs/specfs/specdev.h>
     66 
     67 #include <ufs/ufs/quota.h>
     68 #include <ufs/ufs/ufsmount.h>
     69 #include <ufs/ufs/inode.h>
     70 #include <ufs/ufs/dir.h>
     71 #include <ufs/ufs/ufs_extern.h>
     72 #include <ufs/ufs/ufs_bswap.h>
     73 
     74 #include <ufs/ffs/fs.h>
     75 #include <ufs/ffs/ffs_extern.h>
     76 
     77 /* how many times ffs_init() was called */
     78 int ffs_initcount = 0;
     79 
     80 extern struct lock ufs_hashlock;
     81 
     82 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
     83 extern const struct vnodeopv_desc ffs_specop_opv_desc;
     84 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
     85 
     86 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
     87 	&ffs_vnodeop_opv_desc,
     88 	&ffs_specop_opv_desc,
     89 	&ffs_fifoop_opv_desc,
     90 	NULL,
     91 };
     92 
     93 struct vfsops ffs_vfsops = {
     94 	MOUNT_FFS,
     95 	ffs_mount,
     96 	ufs_start,
     97 	ffs_unmount,
     98 	ufs_root,
     99 	ufs_quotactl,
    100 	ffs_statvfs,
    101 	ffs_sync,
    102 	ffs_vget,
    103 	ffs_fhtovp,
    104 	ffs_vptofh,
    105 	ffs_init,
    106 	ffs_reinit,
    107 	ffs_done,
    108 	NULL,
    109 	ffs_mountroot,
    110 	ufs_check_export,
    111 	ffs_vnodeopv_descs,
    112 };
    113 
    114 struct genfs_ops ffs_genfsops = {
    115 	ffs_gop_size,
    116 	ufs_gop_alloc,
    117 	genfs_gop_write,
    118 };
    119 
    120 POOL_INIT(ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
    121     &pool_allocator_nointr);
    122 POOL_INIT(ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "dino1pl",
    123     &pool_allocator_nointr);
    124 POOL_INIT(ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0, "dino2pl",
    125     &pool_allocator_nointr);
    126 
    127 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *,
    128 				   daddr_t);
    129 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    130 
    131 /*
    132  * Called by main() when ffs is going to be mounted as root.
    133  */
    134 
    135 int
    136 ffs_mountroot()
    137 {
    138 	struct fs *fs;
    139 	struct mount *mp;
    140 	struct proc *p = curproc;	/* XXX */
    141 	struct ufsmount *ump;
    142 	int error;
    143 
    144 	if (root_device->dv_class != DV_DISK)
    145 		return (ENODEV);
    146 
    147 	/*
    148 	 * Get vnodes for rootdev.
    149 	 */
    150 	if (bdevvp(rootdev, &rootvp))
    151 		panic("ffs_mountroot: can't setup bdevvp's");
    152 
    153 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    154 		vrele(rootvp);
    155 		return (error);
    156 	}
    157 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    158 		mp->mnt_op->vfs_refcount--;
    159 		vfs_unbusy(mp);
    160 		free(mp, M_MOUNT);
    161 		vrele(rootvp);
    162 		return (error);
    163 	}
    164 	simple_lock(&mountlist_slock);
    165 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    166 	simple_unlock(&mountlist_slock);
    167 	ump = VFSTOUFS(mp);
    168 	fs = ump->um_fs;
    169 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    170 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    171 	(void)ffs_statvfs(mp, &mp->mnt_stat, p);
    172 	vfs_unbusy(mp);
    173 	inittodr(fs->fs_time);
    174 	return (0);
    175 }
    176 
    177 /*
    178  * VFS Operations.
    179  *
    180  * mount system call
    181  */
    182 int
    183 ffs_mount(mp, path, data, ndp, p)
    184 	struct mount *mp;
    185 	const char *path;
    186 	void *data;
    187 	struct nameidata *ndp;
    188 	struct proc *p;
    189 {
    190 	struct vnode *devvp = NULL;
    191 	struct ufs_args args;
    192 	struct ufsmount *ump = NULL;
    193 	struct fs *fs;
    194 	int error, flags, update;
    195 	mode_t accessmode;
    196 
    197 	if (mp->mnt_flag & MNT_GETARGS) {
    198 		ump = VFSTOUFS(mp);
    199 		if (ump == NULL)
    200 			return EIO;
    201 		args.fspec = NULL;
    202 		vfs_showexport(mp, &args.export, &ump->um_export);
    203 		return copyout(&args, data, sizeof(args));
    204 	}
    205 	error = copyin(data, &args, sizeof (struct ufs_args));
    206 	if (error)
    207 		return (error);
    208 
    209 #if !defined(SOFTDEP)
    210 	mp->mnt_flag &= ~MNT_SOFTDEP;
    211 #endif
    212 
    213 	update = mp->mnt_flag & MNT_UPDATE;
    214 
    215 	/* Check arguments */
    216 	if (update) {
    217 		/* Use the extant mount */
    218 		ump = VFSTOUFS(mp);
    219 		devvp = ump->um_devvp;
    220 		if (args.fspec == NULL)
    221 			vref(devvp);
    222 	} else {
    223 		/* New mounts must have a filename for the device */
    224 		if (args.fspec == NULL)
    225 			return (EINVAL);
    226 	}
    227 
    228 	if (args.fspec != NULL) {
    229 		/*
    230 		 * Look up the name and verify that it's sane.
    231 		 */
    232 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    233 		if ((error = namei(ndp)) != 0)
    234 			return (error);
    235 		devvp = ndp->ni_vp;
    236 
    237 		if (!update) {
    238 			/*
    239 			 * Be sure this is a valid block device
    240 			 */
    241 			if (devvp->v_type != VBLK)
    242 				error = ENOTBLK;
    243 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    244 				error = ENXIO;
    245 		} else {
    246 			/*
    247 			 * Be sure we're still naming the same device
    248 			 * used for our initial mount
    249 			 */
    250 			if (devvp != ump->um_devvp)
    251 				error = EINVAL;
    252 		}
    253 	}
    254 
    255 	/*
    256 	 * If mount by non-root, then verify that user has necessary
    257 	 * permissions on the device.
    258 	 */
    259 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    260 		accessmode = VREAD;
    261 		if (update ?
    262 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    263 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    264 			accessmode |= VWRITE;
    265 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    266 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    267 		VOP_UNLOCK(devvp, 0);
    268 	}
    269 
    270 	if (error) {
    271 		vrele(devvp);
    272 		return (error);
    273 	}
    274 
    275 	if (!update) {
    276 		error = ffs_mountfs(devvp, mp, p);
    277 		if (error) {
    278 			vrele(devvp);
    279 			return (error);
    280 		}
    281 
    282 		ump = VFSTOUFS(mp);
    283 		fs = ump->um_fs;
    284 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    285 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    286 			printf("%s fs uses soft updates, "
    287 			    "ignoring async mode\n",
    288 			    fs->fs_fsmnt);
    289 			mp->mnt_flag &= ~MNT_ASYNC;
    290 		}
    291 	} else {
    292 		/*
    293 		 * Update the mount.
    294 		 */
    295 
    296 		/*
    297 		 * The initial mount got a reference on this
    298 		 * device, so drop the one obtained via
    299 		 * namei(), above.
    300 		 */
    301 		vrele(devvp);
    302 
    303 		fs = ump->um_fs;
    304 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    305 			/*
    306 			 * Changing from r/w to r/o
    307 			 */
    308 			vn_start_write(NULL, &mp, V_WAIT);
    309 			flags = WRITECLOSE;
    310 			if (mp->mnt_flag & MNT_FORCE)
    311 				flags |= FORCECLOSE;
    312 			if (mp->mnt_flag & MNT_SOFTDEP)
    313 				error = softdep_flushfiles(mp, flags, p);
    314 			else
    315 				error = ffs_flushfiles(mp, flags, p);
    316 			if (fs->fs_pendingblocks != 0 ||
    317 			    fs->fs_pendinginodes != 0) {
    318 				printf("%s: update error: blocks %" PRId64
    319 				       " files %d\n",
    320 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    321 				    fs->fs_pendinginodes);
    322 				fs->fs_pendingblocks = 0;
    323 				fs->fs_pendinginodes = 0;
    324 			}
    325 			if (error == 0 &&
    326 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    327 			    fs->fs_clean & FS_WASCLEAN) {
    328 				if (mp->mnt_flag & MNT_SOFTDEP)
    329 					fs->fs_flags &= ~FS_DOSOFTDEP;
    330 				fs->fs_clean = FS_ISCLEAN;
    331 				(void) ffs_sbupdate(ump, MNT_WAIT);
    332 			}
    333 			vn_finished_write(mp, 0);
    334 			if (error)
    335 				return (error);
    336 			fs->fs_ronly = 1;
    337 			fs->fs_fmod = 0;
    338 		}
    339 
    340 		/*
    341 		 * Flush soft dependencies if disabling it via an update
    342 		 * mount. This may leave some items to be processed,
    343 		 * so don't do this yet XXX.
    344 		 */
    345 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    346 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    347 #ifdef notyet
    348 			vn_start_write(NULL, &mp, V_WAIT);
    349 			flags = WRITECLOSE;
    350 			if (mp->mnt_flag & MNT_FORCE)
    351 				flags |= FORCECLOSE;
    352 			error = softdep_flushfiles(mp, flags, p);
    353 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    354 				fs->fs_flags &= ~FS_DOSOFTDEP;
    355 				(void) ffs_sbupdate(ump, MNT_WAIT);
    356 			vn_finished_write(mp);
    357 #elif defined(SOFTDEP)
    358 			mp->mnt_flag |= MNT_SOFTDEP;
    359 #endif
    360 		}
    361 
    362 		/*
    363 		 * When upgrading to a softdep mount, we must first flush
    364 		 * all vnodes. (not done yet -- see above)
    365 		 */
    366 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    367 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    368 #ifdef notyet
    369 			vn_start_write(NULL, &mp, V_WAIT);
    370 			flags = WRITECLOSE;
    371 			if (mp->mnt_flag & MNT_FORCE)
    372 				flags |= FORCECLOSE;
    373 			error = ffs_flushfiles(mp, flags, p);
    374 			vn_finished_write(mp);
    375 #else
    376 			mp->mnt_flag &= ~MNT_SOFTDEP;
    377 #endif
    378 		}
    379 
    380 		if (mp->mnt_flag & MNT_RELOAD) {
    381 			error = ffs_reload(mp, p->p_ucred, p);
    382 			if (error)
    383 				return (error);
    384 		}
    385 
    386 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    387 			/*
    388 			 * Changing from read-only to read/write
    389 			 */
    390 			fs->fs_ronly = 0;
    391 			fs->fs_clean <<= 1;
    392 			fs->fs_fmod = 1;
    393 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    394 				error = softdep_mount(devvp, mp, fs,
    395 				    p->p_ucred);
    396 				if (error)
    397 					return (error);
    398 			}
    399 		}
    400 		if (args.fspec == 0) {
    401 			/*
    402 			 * Process export requests.
    403 			 */
    404 			return (vfs_export(mp, &ump->um_export, &args.export));
    405 		}
    406 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    407 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    408 			printf("%s fs uses soft updates, ignoring async mode\n",
    409 			    fs->fs_fsmnt);
    410 			mp->mnt_flag &= ~MNT_ASYNC;
    411 		}
    412 	}
    413 
    414 	error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
    415 	    UIO_USERSPACE, mp, p);
    416 	if (error == 0)
    417 		(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    418 		    sizeof(fs->fs_fsmnt));
    419 	if (mp->mnt_flag & MNT_SOFTDEP)
    420 		fs->fs_flags |= FS_DOSOFTDEP;
    421 	else
    422 		fs->fs_flags &= ~FS_DOSOFTDEP;
    423 	if (fs->fs_fmod != 0) {	/* XXX */
    424 		fs->fs_fmod = 0;
    425 		if (fs->fs_clean & FS_WASCLEAN)
    426 			fs->fs_time = time.tv_sec;
    427 		else {
    428 			printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
    429 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    430 			printf("%s: lost blocks %" PRId64 " files %d\n",
    431 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    432 			    fs->fs_pendinginodes);
    433 		}
    434 		(void) ffs_cgupdate(ump, MNT_WAIT);
    435 	}
    436 	return error;
    437 }
    438 
    439 /*
    440  * Reload all incore data for a filesystem (used after running fsck on
    441  * the root filesystem and finding things to fix). The filesystem must
    442  * be mounted read-only.
    443  *
    444  * Things to do to update the mount:
    445  *	1) invalidate all cached meta-data.
    446  *	2) re-read superblock from disk.
    447  *	3) re-read summary information from disk.
    448  *	4) invalidate all inactive vnodes.
    449  *	5) invalidate all cached file data.
    450  *	6) re-read inode data for all active vnodes.
    451  */
    452 int
    453 ffs_reload(mountp, cred, p)
    454 	struct mount *mountp;
    455 	struct ucred *cred;
    456 	struct proc *p;
    457 {
    458 	struct vnode *vp, *nvp, *devvp;
    459 	struct inode *ip;
    460 	void *space;
    461 	struct buf *bp;
    462 	struct fs *fs, *newfs;
    463 	struct partinfo dpart;
    464 	int i, blks, size, error;
    465 	int32_t *lp;
    466 	struct ufsmount *ump;
    467 	daddr_t sblockloc;
    468 
    469 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    470 		return (EINVAL);
    471 
    472 	ump = VFSTOUFS(mountp);
    473 	/*
    474 	 * Step 1: invalidate all cached meta-data.
    475 	 */
    476 	devvp = ump->um_devvp;
    477 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    478 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    479 	VOP_UNLOCK(devvp, 0);
    480 	if (error)
    481 		panic("ffs_reload: dirty1");
    482 	/*
    483 	 * Step 2: re-read superblock from disk.
    484 	 */
    485 	fs = ump->um_fs;
    486 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
    487 		size = DEV_BSIZE;
    488 	else
    489 		size = dpart.disklab->d_secsize;
    490 	/* XXX we don't handle possibility that superblock moved. */
    491 	error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
    492 		      NOCRED, &bp);
    493 	if (error) {
    494 		brelse(bp);
    495 		return (error);
    496 	}
    497 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    498 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    499 #ifdef FFS_EI
    500 	if (ump->um_flags & UFS_NEEDSWAP) {
    501 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    502 		fs->fs_flags |= FS_SWAPPED;
    503 	} else
    504 #endif
    505 		fs->fs_flags &= ~FS_SWAPPED;
    506 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
    507 	     newfs->fs_magic != FS_UFS2_MAGIC)||
    508 	     newfs->fs_bsize > MAXBSIZE ||
    509 	     newfs->fs_bsize < sizeof(struct fs)) {
    510 		brelse(bp);
    511 		free(newfs, M_UFSMNT);
    512 		return (EIO);		/* XXX needs translation */
    513 	}
    514 	/* Store off old fs_sblockloc for fs_oldfscompat_read. */
    515 	sblockloc = fs->fs_sblockloc;
    516 	/*
    517 	 * Copy pointer fields back into superblock before copying in	XXX
    518 	 * new superblock. These should really be in the ufsmount.	XXX
    519 	 * Note that important parameters (eg fs_ncg) are unchanged.
    520 	 */
    521 	newfs->fs_csp = fs->fs_csp;
    522 	newfs->fs_maxcluster = fs->fs_maxcluster;
    523 	newfs->fs_contigdirs = fs->fs_contigdirs;
    524 	newfs->fs_ronly = fs->fs_ronly;
    525 	newfs->fs_active = fs->fs_active;
    526 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    527 	brelse(bp);
    528 	free(newfs, M_UFSMNT);
    529 
    530 	/* Recheck for apple UFS filesystem */
    531 	VFSTOUFS(mountp)->um_flags &= ~UFS_ISAPPLEUFS;
    532 	/* First check to see if this is tagged as an Apple UFS filesystem
    533 	 * in the disklabel
    534 	 */
    535 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    536 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    537 		VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
    538 	}
    539 #ifdef APPLE_UFS
    540 	else {
    541 		/* Manually look for an apple ufs label, and if a valid one
    542 		 * is found, then treat it like an Apple UFS filesystem anyway
    543 		 */
    544 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    545 			APPLEUFS_LABEL_SIZE, cred, &bp);
    546 		if (error) {
    547 			brelse(bp);
    548 			return (error);
    549 		}
    550 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    551 			(struct appleufslabel *)bp->b_data,NULL);
    552 		if (error == 0) {
    553 			VFSTOUFS(mountp)->um_flags |= UFS_ISAPPLEUFS;
    554 		}
    555 		brelse(bp);
    556 		bp = NULL;
    557 	}
    558 #else
    559 	if (VFSTOUFS(mountp)->um_flags & UFS_ISAPPLEUFS)
    560 		return (EIO);
    561 #endif
    562 
    563 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    564 	if (UFS_MPISAPPLEUFS(mountp)) {
    565 		/* see comment about NeXT below */
    566 		mountp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    567 	}
    568 	ffs_oldfscompat_read(fs, VFSTOUFS(mountp), sblockloc);
    569 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    570 		fs->fs_pendingblocks = 0;
    571 		fs->fs_pendinginodes = 0;
    572 	}
    573 
    574 	ffs_statvfs(mountp, &mountp->mnt_stat, p);
    575 	/*
    576 	 * Step 3: re-read summary information from disk.
    577 	 */
    578 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    579 	space = fs->fs_csp;
    580 	for (i = 0; i < blks; i += fs->fs_frag) {
    581 		size = fs->fs_bsize;
    582 		if (i + fs->fs_frag > blks)
    583 			size = (blks - i) * fs->fs_fsize;
    584 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    585 			      NOCRED, &bp);
    586 		if (error) {
    587 			brelse(bp);
    588 			return (error);
    589 		}
    590 #ifdef FFS_EI
    591 		if (UFS_FSNEEDSWAP(fs))
    592 			ffs_csum_swap((struct csum *)bp->b_data,
    593 			    (struct csum *)space, size);
    594 		else
    595 #endif
    596 			memcpy(space, bp->b_data, (size_t)size);
    597 		space = (char *)space + size;
    598 		brelse(bp);
    599 	}
    600 	if ((fs->fs_flags & FS_DOSOFTDEP))
    601 		softdep_mount(devvp, mountp, fs, cred);
    602 	/*
    603 	 * We no longer know anything about clusters per cylinder group.
    604 	 */
    605 	if (fs->fs_contigsumsize > 0) {
    606 		lp = fs->fs_maxcluster;
    607 		for (i = 0; i < fs->fs_ncg; i++)
    608 			*lp++ = fs->fs_contigsumsize;
    609 	}
    610 
    611 loop:
    612 	simple_lock(&mntvnode_slock);
    613 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    614 		if (vp->v_mount != mountp) {
    615 			simple_unlock(&mntvnode_slock);
    616 			goto loop;
    617 		}
    618 		nvp = vp->v_mntvnodes.le_next;
    619 		/*
    620 		 * Step 4: invalidate all inactive vnodes.
    621 		 */
    622 		if (vrecycle(vp, &mntvnode_slock, p))
    623 			goto loop;
    624 		/*
    625 		 * Step 5: invalidate all cached file data.
    626 		 */
    627 		simple_lock(&vp->v_interlock);
    628 		simple_unlock(&mntvnode_slock);
    629 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    630 			goto loop;
    631 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    632 			panic("ffs_reload: dirty2");
    633 		/*
    634 		 * Step 6: re-read inode data for all active vnodes.
    635 		 */
    636 		ip = VTOI(vp);
    637 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    638 			      (int)fs->fs_bsize, NOCRED, &bp);
    639 		if (error) {
    640 			brelse(bp);
    641 			vput(vp);
    642 			return (error);
    643 		}
    644 		ffs_load_inode(bp, ip, fs, ip->i_number);
    645 		ip->i_ffs_effnlink = ip->i_nlink;
    646 		brelse(bp);
    647 		vput(vp);
    648 		simple_lock(&mntvnode_slock);
    649 	}
    650 	simple_unlock(&mntvnode_slock);
    651 	return (0);
    652 }
    653 
    654 /*
    655  * Possible superblock locations ordered from most to least likely.
    656  */
    657 static const int sblock_try[] = SBLOCKSEARCH;
    658 
    659 /*
    660  * Common code for mount and mountroot
    661  */
    662 int
    663 ffs_mountfs(devvp, mp, p)
    664 	struct vnode *devvp;
    665 	struct mount *mp;
    666 	struct proc *p;
    667 {
    668 	struct ufsmount *ump;
    669 	struct buf *bp;
    670 	struct fs *fs;
    671 	dev_t dev;
    672 	struct partinfo dpart;
    673 	void *space;
    674 	daddr_t sblockloc, fsblockloc;
    675 	int blks, fstype;
    676 	int error, i, size, ronly;
    677 #ifdef FFS_EI
    678 	int needswap = 0;		/* keep gcc happy */
    679 #endif
    680 	int32_t *lp;
    681 	struct ucred *cred;
    682 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
    683 
    684 	dev = devvp->v_rdev;
    685 	cred = p ? p->p_ucred : NOCRED;
    686 	/*
    687 	 * Disallow multiple mounts of the same device.
    688 	 * Disallow mounting of a device that is currently in use
    689 	 * (except for root, which might share swap device for miniroot).
    690 	 * Flush out any old buffers remaining from a previous use.
    691 	 */
    692 	if ((error = vfs_mountedon(devvp)) != 0)
    693 		return (error);
    694 	if (vcount(devvp) > 1 && devvp != rootvp)
    695 		return (EBUSY);
    696 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    697 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    698 	VOP_UNLOCK(devvp, 0);
    699 	if (error)
    700 		return (error);
    701 
    702 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    703 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    704 	if (error)
    705 		return (error);
    706 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
    707 		size = DEV_BSIZE;
    708 	else
    709 		size = dpart.disklab->d_secsize;
    710 
    711 	bp = NULL;
    712 	ump = NULL;
    713 	fs = NULL;
    714 	sblockloc = 0;
    715 	fstype = 0;
    716 
    717 	/*
    718 	 * Try reading the superblock in each of its possible locations.		 */
    719 	for (i = 0; ; i++) {
    720 		if (bp != NULL) {
    721 			bp->b_flags |= B_NOCACHE;
    722 			brelse(bp);
    723 			bp = NULL;
    724 		}
    725 		if (sblock_try[i] == -1) {
    726 			error = EINVAL;
    727 			fs = NULL;
    728 			goto out;
    729 		}
    730 		error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
    731 			      &bp);
    732 		if (error)
    733 			goto out;
    734 		fs = (struct fs*)bp->b_data;
    735 		fsblockloc = sblockloc = sblock_try[i];
    736 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    737 			sbsize = fs->fs_sbsize;
    738 			fstype = UFS1;
    739 #ifdef FFS_EI
    740 			needswap = 0;
    741 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    742 			sbsize = bswap32(fs->fs_sbsize);
    743 			fstype = UFS1;
    744 			needswap = 1;
    745 #endif
    746 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    747 			sbsize = fs->fs_sbsize;
    748 			fstype = UFS2;
    749 #ifdef FFS_EI
    750 			needswap = 0;
    751 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    752 			sbsize = bswap32(fs->fs_sbsize);
    753 			fstype = UFS2;
    754 			needswap = 1;
    755 #endif
    756 		} else
    757 			continue;
    758 
    759 
    760 		/* fs->fs_sblockloc isn't defined for old filesystems */
    761 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
    762 			if (sblockloc == SBLOCK_UFS2)
    763 				/*
    764 				 * This is likely to be the first alternate
    765 				 * in a filesystem with 64k blocks.
    766 				 * Don't use it.
    767 				 */
    768 				continue;
    769 			fsblockloc = sblockloc;
    770 		} else {
    771 			fsblockloc = fs->fs_sblockloc;
    772 #ifdef FFS_EI
    773 			if (needswap)
    774 				fsblockloc = bswap64(fsblockloc);
    775 #endif
    776 		}
    777 
    778 		/* Check we haven't found an alternate superblock */
    779 		if (fsblockloc != sblockloc)
    780 			continue;
    781 
    782 		/* Validate size of superblock */
    783 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
    784 			continue;
    785 
    786 		/* Ok seems to be a good superblock */
    787 		break;
    788 	}
    789 
    790 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    791 	memcpy(fs, bp->b_data, sbsize);
    792 
    793 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    794 	memset(ump, 0, sizeof *ump);
    795 	ump->um_fs = fs;
    796 
    797 #ifdef FFS_EI
    798 	if (needswap) {
    799 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    800 		fs->fs_flags |= FS_SWAPPED;
    801 	} else
    802 #endif
    803 		fs->fs_flags &= ~FS_SWAPPED;
    804 
    805 	ffs_oldfscompat_read(fs, ump, sblockloc);
    806 
    807 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    808 		fs->fs_pendingblocks = 0;
    809 		fs->fs_pendinginodes = 0;
    810 	}
    811 
    812 	ump->um_fstype = fstype;
    813 	if (fs->fs_sbsize < SBLOCKSIZE)
    814 		bp->b_flags |= B_INVAL;
    815 	brelse(bp);
    816 	bp = NULL;
    817 
    818 	/* First check to see if this is tagged as an Apple UFS filesystem
    819 	 * in the disklabel
    820 	 */
    821 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    822 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    823 		ump->um_flags |= UFS_ISAPPLEUFS;
    824 	}
    825 #ifdef APPLE_UFS
    826 	else {
    827 		/* Manually look for an apple ufs label, and if a valid one
    828 		 * is found, then treat it like an Apple UFS filesystem anyway
    829 		 */
    830 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    831 			APPLEUFS_LABEL_SIZE, cred, &bp);
    832 		if (error)
    833 			goto out;
    834 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    835 			(struct appleufslabel *)bp->b_data,NULL);
    836 		if (error == 0) {
    837 			ump->um_flags |= UFS_ISAPPLEUFS;
    838 		}
    839 		brelse(bp);
    840 		bp = NULL;
    841 	}
    842 #else
    843 	if (ump->um_flags & UFS_ISAPPLEUFS) {
    844 		error = EINVAL;
    845 		goto out;
    846 	}
    847 #endif
    848 
    849 	/*
    850 	 * verify that we can access the last block in the fs
    851 	 * if we're mounting read/write.
    852 	 */
    853 
    854 	if (!ronly) {
    855 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    856 		    cred, &bp);
    857 		if (bp->b_bcount != fs->fs_fsize)
    858 			error = EINVAL;
    859 		bp->b_flags |= B_INVAL;
    860 		if (error)
    861 			goto out;
    862 		brelse(bp);
    863 		bp = NULL;
    864 	}
    865 
    866 	fs->fs_ronly = ronly;
    867 	if (ronly == 0) {
    868 		fs->fs_clean <<= 1;
    869 		fs->fs_fmod = 1;
    870 	}
    871 	size = fs->fs_cssize;
    872 	blks = howmany(size, fs->fs_fsize);
    873 	if (fs->fs_contigsumsize > 0)
    874 		size += fs->fs_ncg * sizeof(int32_t);
    875 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    876 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    877 	fs->fs_csp = space;
    878 	for (i = 0; i < blks; i += fs->fs_frag) {
    879 		size = fs->fs_bsize;
    880 		if (i + fs->fs_frag > blks)
    881 			size = (blks - i) * fs->fs_fsize;
    882 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    883 			      cred, &bp);
    884 		if (error) {
    885 			free(fs->fs_csp, M_UFSMNT);
    886 			goto out;
    887 		}
    888 #ifdef FFS_EI
    889 		if (needswap)
    890 			ffs_csum_swap((struct csum *)bp->b_data,
    891 				(struct csum *)space, size);
    892 		else
    893 #endif
    894 			memcpy(space, bp->b_data, (u_int)size);
    895 
    896 		space = (char *)space + size;
    897 		brelse(bp);
    898 		bp = NULL;
    899 	}
    900 	if (fs->fs_contigsumsize > 0) {
    901 		fs->fs_maxcluster = lp = space;
    902 		for (i = 0; i < fs->fs_ncg; i++)
    903 			*lp++ = fs->fs_contigsumsize;
    904 		space = lp;
    905 	}
    906 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    907 	fs->fs_contigdirs = space;
    908 	space = (char *)space + size;
    909 	memset(fs->fs_contigdirs, 0, size);
    910 		/* Compatibility for old filesystems - XXX */
    911 	if (fs->fs_avgfilesize <= 0)
    912 		fs->fs_avgfilesize = AVFILESIZ;
    913 	if (fs->fs_avgfpdir <= 0)
    914 		fs->fs_avgfpdir = AFPDIR;
    915 	mp->mnt_data = ump;
    916 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    917 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
    918 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    919 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    920 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    921 	if (UFS_MPISAPPLEUFS(mp)) {
    922 		/* NeXT used to keep short symlinks in the inode even
    923 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
    924 		 * is probably -1, but we still need to be able to identify
    925 		 * short symlinks.
    926 		 */
    927 		mp->mnt_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    928 	}
    929 	mp->mnt_fs_bshift = fs->fs_bshift;
    930 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    931 	mp->mnt_flag |= MNT_LOCAL;
    932 #ifdef FFS_EI
    933 	if (needswap)
    934 		ump->um_flags |= UFS_NEEDSWAP;
    935 #endif
    936 	ump->um_mountp = mp;
    937 	ump->um_dev = dev;
    938 	ump->um_devvp = devvp;
    939 	ump->um_nindir = fs->fs_nindir;
    940 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    941 	ump->um_bptrtodb = fs->fs_fsbtodb;
    942 	ump->um_seqinc = fs->fs_frag;
    943 	for (i = 0; i < MAXQUOTAS; i++)
    944 		ump->um_quotas[i] = NULLVP;
    945 	devvp->v_specmountpoint = mp;
    946 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    947 		error = softdep_mount(devvp, mp, fs, cred);
    948 		if (error) {
    949 			free(fs->fs_csp, M_UFSMNT);
    950 			goto out;
    951 		}
    952 	}
    953 	return (0);
    954 out:
    955 	if (fs)
    956 		free(fs, M_UFSMNT);
    957 	devvp->v_specmountpoint = NULL;
    958 	if (bp)
    959 		brelse(bp);
    960 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    961 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    962 	VOP_UNLOCK(devvp, 0);
    963 	if (ump) {
    964 		if (ump->um_oldfscompat)
    965 			free(ump->um_oldfscompat, M_UFSMNT);
    966 		free(ump, M_UFSMNT);
    967 		mp->mnt_data = NULL;
    968 	}
    969 	return (error);
    970 }
    971 
    972 /*
    973  * Sanity checks for loading old filesystem superblocks.
    974  * See ffs_oldfscompat_write below for unwound actions.
    975  *
    976  * XXX - Parts get retired eventually.
    977  * Unfortunately new bits get added.
    978  */
    979 static void
    980 ffs_oldfscompat_read(fs, ump, sblockloc)
    981 	struct fs *fs;
    982 	struct ufsmount *ump;
    983 	daddr_t sblockloc;
    984 {
    985 	off_t maxfilesize;
    986 	int32_t *extrasave;
    987 
    988 	/* XXX This warning should be removed before the next release. -- dbj */
    989 	if (fs->fs_flags & 0x40000000) {
    990 		printf("WARNING: possible botched superblock upgrade detected\n"
    991 		    "on filesystem previously mounted on %s\n"
    992 		    "Extra bits discovered in fs_flags on filesystem (0x%08x)\n"
    993 		    "Consider running the program mentioned in\n"
    994 		    "http://mail-index.NetBSD.org/tech-kern/2003/10/07/0005.html\n",
    995 		    fs->fs_fsmnt, fs->fs_flags);
    996 	}
    997 
    998 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
    999 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1000 		return;
   1001 
   1002 	/* XXX This warning should be removed before the next release. -- dbj */
   1003 	if (fs->fs_maxbsize == fs->fs_bsize) {
   1004 		printf("WARNING: possible botched superblock upgrade detected\n"
   1005 		    "on filesystem previously mounted on %s\n"
   1006 		    "fs_bsize == fs_maxbsize (0x%08x) but FS_FLAGS_UPDATED is not set\n"
   1007 		    "Test your filesystem by running fsck_ffs -n -f on it.\n"
   1008 		    "If it reports:\n"
   1009 		    "``VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE''\n"
   1010 		    "you should be able to recover with fsck_ffs -b 16 -c 4\n"
   1011 		    "See the file src/UPDATING or\n"
   1012 		    "http://mail-index.NetBSD.org/current-users/2004/01/11/0022.html\n"
   1013 		    "for more details\n",
   1014 		    fs->fs_fsmnt, fs->fs_maxbsize);
   1015 	}
   1016 
   1017 	if (!ump->um_oldfscompat)
   1018 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
   1019 		    M_UFSMNT, M_WAITOK);
   1020 
   1021 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1022 	extrasave = ump->um_oldfscompat;
   1023 	extrasave += 512/sizeof(int32_t);
   1024 	extrasave[0] = fs->fs_old_npsect;
   1025 	extrasave[1] = fs->fs_old_interleave;
   1026 	extrasave[2] = fs->fs_old_trackskew;
   1027 
   1028 	/* These fields will be overwritten by their
   1029 	 * original values in fs_oldfscompat_write, so it is harmless
   1030 	 * to modify them here.
   1031 	 */
   1032 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1033 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1034 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1035 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1036 
   1037 	fs->fs_maxbsize = fs->fs_bsize;
   1038 	fs->fs_time = fs->fs_old_time;
   1039 	fs->fs_size = fs->fs_old_size;
   1040 	fs->fs_dsize = fs->fs_old_dsize;
   1041 	fs->fs_csaddr = fs->fs_old_csaddr;
   1042 	fs->fs_sblockloc = sblockloc;
   1043 
   1044 	fs->fs_flags = fs->fs_old_flags;
   1045 
   1046 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1047 		fs->fs_old_nrpos = 8;
   1048 		fs->fs_old_npsect = fs->fs_old_nsect;
   1049 		fs->fs_old_interleave = 1;
   1050 		fs->fs_old_trackskew = 0;
   1051 	}
   1052 
   1053 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1054 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1055 		fs->fs_qbmask = ~fs->fs_bmask;
   1056 		fs->fs_qfmask = ~fs->fs_fmask;
   1057 	}
   1058 
   1059 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1060 	if (fs->fs_maxfilesize > maxfilesize)
   1061 		fs->fs_maxfilesize = maxfilesize;
   1062 
   1063 	/* Compatibility for old filesystems */
   1064 	if (fs->fs_avgfilesize <= 0)
   1065 		fs->fs_avgfilesize = AVFILESIZ;
   1066 	if (fs->fs_avgfpdir <= 0)
   1067 		fs->fs_avgfpdir = AFPDIR;
   1068 
   1069 #if 0
   1070 	if (bigcgs) {
   1071 		fs->fs_save_cgsize = fs->fs_cgsize;
   1072 		fs->fs_cgsize = fs->fs_bsize;
   1073 	}
   1074 #endif
   1075 }
   1076 
   1077 /*
   1078  * Unwinding superblock updates for old filesystems.
   1079  * See ffs_oldfscompat_read above for details.
   1080  *
   1081  * XXX - Parts get retired eventually.
   1082  * Unfortunately new bits get added.
   1083  */
   1084 static void
   1085 ffs_oldfscompat_write(fs, ump)
   1086 	struct fs *fs;
   1087 	struct ufsmount *ump;
   1088 {
   1089 	int32_t *extrasave;
   1090 
   1091 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1092 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1093 		return;
   1094 
   1095 	fs->fs_old_time = fs->fs_time;
   1096 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1097 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1098 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1099 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1100 	fs->fs_old_flags = fs->fs_flags;
   1101 
   1102 #if 0
   1103 	if (bigcgs) {
   1104 		fs->fs_cgsize = fs->fs_save_cgsize;
   1105 	}
   1106 #endif
   1107 
   1108 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1109 	extrasave = ump->um_oldfscompat;
   1110 	extrasave += 512/sizeof(int32_t);
   1111 	fs->fs_old_npsect = extrasave[0];
   1112 	fs->fs_old_interleave = extrasave[1];
   1113 	fs->fs_old_trackskew = extrasave[2];
   1114 
   1115 }
   1116 
   1117 /*
   1118  * unmount system call
   1119  */
   1120 int
   1121 ffs_unmount(mp, mntflags, p)
   1122 	struct mount *mp;
   1123 	int mntflags;
   1124 	struct proc *p;
   1125 {
   1126 	struct ufsmount *ump;
   1127 	struct fs *fs;
   1128 	int error, flags, penderr;
   1129 
   1130 	penderr = 0;
   1131 	flags = 0;
   1132 	if (mntflags & MNT_FORCE)
   1133 		flags |= FORCECLOSE;
   1134 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1135 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
   1136 			return (error);
   1137 	} else {
   1138 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
   1139 			return (error);
   1140 	}
   1141 	ump = VFSTOUFS(mp);
   1142 	fs = ump->um_fs;
   1143 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1144 		printf("%s: unmount pending error: blocks %" PRId64
   1145 		       " files %d\n",
   1146 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1147 		fs->fs_pendingblocks = 0;
   1148 		fs->fs_pendinginodes = 0;
   1149 		penderr = 1;
   1150 	}
   1151 	if (fs->fs_ronly == 0 &&
   1152 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1153 	    fs->fs_clean & FS_WASCLEAN) {
   1154 		/*
   1155 		 * XXXX don't mark fs clean in the case of softdep
   1156 		 * pending block errors, until they are fixed.
   1157 		 */
   1158 		if (penderr == 0) {
   1159 			if (mp->mnt_flag & MNT_SOFTDEP)
   1160 				fs->fs_flags &= ~FS_DOSOFTDEP;
   1161 			fs->fs_clean = FS_ISCLEAN;
   1162 		}
   1163 		fs->fs_fmod = 0;
   1164 		(void) ffs_sbupdate(ump, MNT_WAIT);
   1165 	}
   1166 	if (ump->um_devvp->v_type != VBAD)
   1167 		ump->um_devvp->v_specmountpoint = NULL;
   1168 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1169 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
   1170 		NOCRED, p);
   1171 	vput(ump->um_devvp);
   1172 	free(fs->fs_csp, M_UFSMNT);
   1173 	free(fs, M_UFSMNT);
   1174 	if (ump->um_oldfscompat != NULL)
   1175 		free(ump->um_oldfscompat, M_UFSMNT);
   1176 	free(ump, M_UFSMNT);
   1177 	mp->mnt_data = NULL;
   1178 	mp->mnt_flag &= ~MNT_LOCAL;
   1179 	return (0);
   1180 }
   1181 
   1182 /*
   1183  * Flush out all the files in a filesystem.
   1184  */
   1185 int
   1186 ffs_flushfiles(mp, flags, p)
   1187 	struct mount *mp;
   1188 	int flags;
   1189 	struct proc *p;
   1190 {
   1191 	extern int doforce;
   1192 	struct ufsmount *ump;
   1193 	int error;
   1194 
   1195 	if (!doforce)
   1196 		flags &= ~FORCECLOSE;
   1197 	ump = VFSTOUFS(mp);
   1198 #ifdef QUOTA
   1199 	if (mp->mnt_flag & MNT_QUOTA) {
   1200 		int i;
   1201 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
   1202 			return (error);
   1203 		for (i = 0; i < MAXQUOTAS; i++) {
   1204 			if (ump->um_quotas[i] == NULLVP)
   1205 				continue;
   1206 			quotaoff(p, mp, i);
   1207 		}
   1208 		/*
   1209 		 * Here we fall through to vflush again to ensure
   1210 		 * that we have gotten rid of all the system vnodes.
   1211 		 */
   1212 	}
   1213 #endif
   1214 	/*
   1215 	 * Flush all the files.
   1216 	 */
   1217 	error = vflush(mp, NULLVP, flags);
   1218 	if (error)
   1219 		return (error);
   1220 	/*
   1221 	 * Flush filesystem metadata.
   1222 	 */
   1223 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1224 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
   1225 	VOP_UNLOCK(ump->um_devvp, 0);
   1226 	return (error);
   1227 }
   1228 
   1229 /*
   1230  * Get file system statistics.
   1231  */
   1232 int
   1233 ffs_statvfs(mp, sbp, p)
   1234 	struct mount *mp;
   1235 	struct statvfs *sbp;
   1236 	struct proc *p;
   1237 {
   1238 	struct ufsmount *ump;
   1239 	struct fs *fs;
   1240 
   1241 	ump = VFSTOUFS(mp);
   1242 	fs = ump->um_fs;
   1243 	sbp->f_bsize = fs->fs_bsize;
   1244 	sbp->f_frsize = fs->fs_fsize;
   1245 	sbp->f_iosize = fs->fs_bsize;
   1246 	sbp->f_blocks = fs->fs_dsize;
   1247 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1248 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1249 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1250 	    fs->fs_minfree) / (u_int64_t) 100;
   1251 	if (sbp->f_bfree > sbp->f_bresvd)
   1252 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1253 	else
   1254 		sbp->f_bavail = 0;
   1255 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1256 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1257 	sbp->f_favail = sbp->f_ffree;
   1258 	sbp->f_fresvd = 0;
   1259 	copy_statvfs_info(sbp, mp);
   1260 	return (0);
   1261 }
   1262 
   1263 /*
   1264  * Go through the disk queues to initiate sandbagged IO;
   1265  * go through the inodes to write those that have been modified;
   1266  * initiate the writing of the super block if it has been modified.
   1267  *
   1268  * Note: we are always called with the filesystem marked `MPBUSY'.
   1269  */
   1270 int
   1271 ffs_sync(mp, waitfor, cred, p)
   1272 	struct mount *mp;
   1273 	int waitfor;
   1274 	struct ucred *cred;
   1275 	struct proc *p;
   1276 {
   1277 	struct vnode *vp, *nvp;
   1278 	struct inode *ip;
   1279 	struct ufsmount *ump = VFSTOUFS(mp);
   1280 	struct fs *fs;
   1281 	int error, count, allerror = 0;
   1282 
   1283 	fs = ump->um_fs;
   1284 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1285 		printf("fs = %s\n", fs->fs_fsmnt);
   1286 		panic("update: rofs mod");
   1287 	}
   1288 	/*
   1289 	 * Write back each (modified) inode.
   1290 	 */
   1291 	simple_lock(&mntvnode_slock);
   1292 loop:
   1293 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1294 		/*
   1295 		 * If the vnode that we are about to sync is no longer
   1296 		 * associated with this mount point, start over.
   1297 		 */
   1298 		if (vp->v_mount != mp)
   1299 			goto loop;
   1300 		simple_lock(&vp->v_interlock);
   1301 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1302 		ip = VTOI(vp);
   1303 		if (vp->v_type == VNON ||
   1304 		    ((ip->i_flag &
   1305 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1306 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1307 		     vp->v_uobj.uo_npages == 0))
   1308 		{
   1309 			simple_unlock(&vp->v_interlock);
   1310 			continue;
   1311 		}
   1312 		simple_unlock(&mntvnode_slock);
   1313 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1314 		if (error) {
   1315 			simple_lock(&mntvnode_slock);
   1316 			if (error == ENOENT)
   1317 				goto loop;
   1318 			continue;
   1319 		}
   1320 		if ((error = VOP_FSYNC(vp, cred,
   1321 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1322 			allerror = error;
   1323 		vput(vp);
   1324 		simple_lock(&mntvnode_slock);
   1325 	}
   1326 	simple_unlock(&mntvnode_slock);
   1327 	/*
   1328 	 * Force stale file system control information to be flushed.
   1329 	 */
   1330 	if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
   1331 		if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
   1332 			allerror = error;
   1333 		/* Flushed work items may create new vnodes to clean */
   1334 		if (allerror == 0 && count) {
   1335 			simple_lock(&mntvnode_slock);
   1336 			goto loop;
   1337 		}
   1338 	}
   1339 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1340 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1341 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1342 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1343 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1344 			allerror = error;
   1345 		VOP_UNLOCK(ump->um_devvp, 0);
   1346 		if (allerror == 0 && waitfor == MNT_WAIT) {
   1347 			simple_lock(&mntvnode_slock);
   1348 			goto loop;
   1349 		}
   1350 	}
   1351 #ifdef QUOTA
   1352 	qsync(mp);
   1353 #endif
   1354 	/*
   1355 	 * Write back modified superblock.
   1356 	 */
   1357 	if (fs->fs_fmod != 0) {
   1358 		fs->fs_fmod = 0;
   1359 		fs->fs_time = time.tv_sec;
   1360 		if ((error = ffs_cgupdate(ump, waitfor)))
   1361 			allerror = error;
   1362 	}
   1363 	return (allerror);
   1364 }
   1365 
   1366 /*
   1367  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1368  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1369  * return the inode locked.  Detection and handling of mount points must be
   1370  * done by the calling routine.
   1371  */
   1372 int
   1373 ffs_vget(mp, ino, vpp)
   1374 	struct mount *mp;
   1375 	ino_t ino;
   1376 	struct vnode **vpp;
   1377 {
   1378 	struct fs *fs;
   1379 	struct inode *ip;
   1380 	struct ufsmount *ump;
   1381 	struct buf *bp;
   1382 	struct vnode *vp;
   1383 	dev_t dev;
   1384 	int error;
   1385 
   1386 	ump = VFSTOUFS(mp);
   1387 	dev = ump->um_dev;
   1388 
   1389 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1390 		return (0);
   1391 
   1392 	/* Allocate a new vnode/inode. */
   1393 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1394 		*vpp = NULL;
   1395 		return (error);
   1396 	}
   1397 
   1398 	/*
   1399 	 * If someone beat us to it while sleeping in getnewvnode(),
   1400 	 * push back the freshly allocated vnode we don't need, and return.
   1401 	 */
   1402 
   1403 	do {
   1404 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1405 			ungetnewvnode(vp);
   1406 			return (0);
   1407 		}
   1408 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1409 
   1410 	/*
   1411 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1412 	 * XXX create another pool for MFS inodes?
   1413 	 */
   1414 
   1415 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1416 	memset(ip, 0, sizeof(struct inode));
   1417 	vp->v_data = ip;
   1418 	ip->i_vnode = vp;
   1419 	ip->i_ump = ump;
   1420 	ip->i_fs = fs = ump->um_fs;
   1421 	ip->i_dev = dev;
   1422 	ip->i_number = ino;
   1423 	LIST_INIT(&ip->i_pcbufhd);
   1424 #ifdef QUOTA
   1425 	{
   1426 		int i;
   1427 
   1428 		for (i = 0; i < MAXQUOTAS; i++)
   1429 			ip->i_dquot[i] = NODQUOT;
   1430 	}
   1431 #endif
   1432 
   1433 	/*
   1434 	 * Put it onto its hash chain and lock it so that other requests for
   1435 	 * this inode will block if they arrive while we are sleeping waiting
   1436 	 * for old data structures to be purged or for the contents of the
   1437 	 * disk portion of this inode to be read.
   1438 	 */
   1439 
   1440 	ufs_ihashins(ip);
   1441 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1442 
   1443 	/* Read in the disk contents for the inode, copy into the inode. */
   1444 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1445 		      (int)fs->fs_bsize, NOCRED, &bp);
   1446 	if (error) {
   1447 
   1448 		/*
   1449 		 * The inode does not contain anything useful, so it would
   1450 		 * be misleading to leave it on its hash chain. With mode
   1451 		 * still zero, it will be unlinked and returned to the free
   1452 		 * list by vput().
   1453 		 */
   1454 
   1455 		vput(vp);
   1456 		brelse(bp);
   1457 		*vpp = NULL;
   1458 		return (error);
   1459 	}
   1460 	if (ip->i_ump->um_fstype == UFS1)
   1461 		ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
   1462 	else
   1463 		ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
   1464 	ffs_load_inode(bp, ip, fs, ino);
   1465 	if (DOINGSOFTDEP(vp))
   1466 		softdep_load_inodeblock(ip);
   1467 	else
   1468 		ip->i_ffs_effnlink = ip->i_nlink;
   1469 	brelse(bp);
   1470 
   1471 	/*
   1472 	 * Initialize the vnode from the inode, check for aliases.
   1473 	 * Note that the underlying vnode may have changed.
   1474 	 */
   1475 
   1476 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1477 
   1478 	/*
   1479 	 * Finish inode initialization now that aliasing has been resolved.
   1480 	 */
   1481 
   1482 	genfs_node_init(vp, &ffs_genfsops);
   1483 	ip->i_devvp = ump->um_devvp;
   1484 	VREF(ip->i_devvp);
   1485 
   1486 	/*
   1487 	 * Ensure that uid and gid are correct. This is a temporary
   1488 	 * fix until fsck has been changed to do the update.
   1489 	 */
   1490 
   1491 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1492 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1493 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1494 	}							/* XXX */
   1495 	uvm_vnp_setsize(vp, ip->i_size);
   1496 	*vpp = vp;
   1497 	return (0);
   1498 }
   1499 
   1500 /*
   1501  * File handle to vnode
   1502  *
   1503  * Have to be really careful about stale file handles:
   1504  * - check that the inode number is valid
   1505  * - call ffs_vget() to get the locked inode
   1506  * - check for an unallocated inode (i_mode == 0)
   1507  * - check that the given client host has export rights and return
   1508  *   those rights via. exflagsp and credanonp
   1509  */
   1510 int
   1511 ffs_fhtovp(mp, fhp, vpp)
   1512 	struct mount *mp;
   1513 	struct fid *fhp;
   1514 	struct vnode **vpp;
   1515 {
   1516 	struct ufid *ufhp;
   1517 	struct fs *fs;
   1518 
   1519 	ufhp = (struct ufid *)fhp;
   1520 	fs = VFSTOUFS(mp)->um_fs;
   1521 	if (ufhp->ufid_ino < ROOTINO ||
   1522 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1523 		return (ESTALE);
   1524 	return (ufs_fhtovp(mp, ufhp, vpp));
   1525 }
   1526 
   1527 /*
   1528  * Vnode pointer to File handle
   1529  */
   1530 /* ARGSUSED */
   1531 int
   1532 ffs_vptofh(vp, fhp)
   1533 	struct vnode *vp;
   1534 	struct fid *fhp;
   1535 {
   1536 	struct inode *ip;
   1537 	struct ufid *ufhp;
   1538 
   1539 	ip = VTOI(vp);
   1540 	ufhp = (struct ufid *)fhp;
   1541 	ufhp->ufid_len = sizeof(struct ufid);
   1542 	ufhp->ufid_ino = ip->i_number;
   1543 	ufhp->ufid_gen = ip->i_gen;
   1544 	return (0);
   1545 }
   1546 
   1547 void
   1548 ffs_init()
   1549 {
   1550 	if (ffs_initcount++ > 0)
   1551 		return;
   1552 
   1553 	softdep_initialize();
   1554 	ufs_init();
   1555 }
   1556 
   1557 void
   1558 ffs_reinit()
   1559 {
   1560 	softdep_reinitialize();
   1561 	ufs_reinit();
   1562 }
   1563 
   1564 void
   1565 ffs_done()
   1566 {
   1567 	if (--ffs_initcount > 0)
   1568 		return;
   1569 
   1570 	/* XXX softdep cleanup ? */
   1571 	ufs_done();
   1572 	pool_destroy(&ffs_inode_pool);
   1573 }
   1574 
   1575 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
   1576 {
   1577 	extern int doasyncfree;
   1578 	extern int ffs_log_changeopt;
   1579 
   1580 	sysctl_createv(clog, 0, NULL, NULL,
   1581 		       CTLFLAG_PERMANENT,
   1582 		       CTLTYPE_NODE, "vfs", NULL,
   1583 		       NULL, 0, NULL, 0,
   1584 		       CTL_VFS, CTL_EOL);
   1585 	sysctl_createv(clog, 0, NULL, NULL,
   1586 		       CTLFLAG_PERMANENT,
   1587 		       CTLTYPE_NODE, "ffs", NULL,
   1588 		       NULL, 0, NULL, 0,
   1589 		       CTL_VFS, 1, CTL_EOL);
   1590 
   1591 	/*
   1592 	 * @@@ should we even bother with these first three?
   1593 	 */
   1594 	sysctl_createv(clog, 0, NULL, NULL,
   1595 		       CTLFLAG_PERMANENT,
   1596 		       CTLTYPE_INT, "doclusterread", NULL,
   1597 		       sysctl_notavail, 0, NULL, 0,
   1598 		       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
   1599 	sysctl_createv(clog, 0, NULL, NULL,
   1600 		       CTLFLAG_PERMANENT,
   1601 		       CTLTYPE_INT, "doclusterwrite", NULL,
   1602 		       sysctl_notavail, 0, NULL, 0,
   1603 		       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
   1604 	sysctl_createv(clog, 0, NULL, NULL,
   1605 		       CTLFLAG_PERMANENT,
   1606 		       CTLTYPE_INT, "doreallocblks", NULL,
   1607 		       sysctl_notavail, 0, NULL, 0,
   1608 		       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
   1609 	sysctl_createv(clog, 0, NULL, NULL,
   1610 		       CTLFLAG_PERMANENT,
   1611 		       CTLTYPE_INT, "doasyncfree", NULL,
   1612 		       NULL, 0, &doasyncfree, 0,
   1613 		       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
   1614 	sysctl_createv(clog, 0, NULL, NULL,
   1615 		       CTLFLAG_PERMANENT,
   1616 		       CTLTYPE_INT, "log_changeopt", NULL,
   1617 		       NULL, 0, &ffs_log_changeopt, 0,
   1618 		       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
   1619 }
   1620 
   1621 /*
   1622  * Write a superblock and associated information back to disk.
   1623  */
   1624 int
   1625 ffs_sbupdate(mp, waitfor)
   1626 	struct ufsmount *mp;
   1627 	int waitfor;
   1628 {
   1629 	struct fs *fs = mp->um_fs;
   1630 	struct buf *bp;
   1631 	int error = 0;
   1632 	u_int32_t saveflag;
   1633 
   1634 	bp = getblk(mp->um_devvp,
   1635 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
   1636 	    (int)fs->fs_sbsize, 0, 0);
   1637 	saveflag = fs->fs_flags & FS_INTERNAL;
   1638 	fs->fs_flags &= ~FS_INTERNAL;
   1639 
   1640 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1641 
   1642 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1643 #ifdef FFS_EI
   1644 	if (mp->um_flags & UFS_NEEDSWAP)
   1645 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   1646 #endif
   1647 	fs->fs_flags |= saveflag;
   1648 
   1649 	if (waitfor == MNT_WAIT)
   1650 		error = bwrite(bp);
   1651 	else
   1652 		bawrite(bp);
   1653 	return (error);
   1654 }
   1655 
   1656 int
   1657 ffs_cgupdate(mp, waitfor)
   1658 	struct ufsmount *mp;
   1659 	int waitfor;
   1660 {
   1661 	struct fs *fs = mp->um_fs;
   1662 	struct buf *bp;
   1663 	int blks;
   1664 	void *space;
   1665 	int i, size, error = 0, allerror = 0;
   1666 
   1667 	allerror = ffs_sbupdate(mp, waitfor);
   1668 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1669 	space = fs->fs_csp;
   1670 	for (i = 0; i < blks; i += fs->fs_frag) {
   1671 		size = fs->fs_bsize;
   1672 		if (i + fs->fs_frag > blks)
   1673 			size = (blks - i) * fs->fs_fsize;
   1674 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1675 		    size, 0, 0);
   1676 #ifdef FFS_EI
   1677 		if (mp->um_flags & UFS_NEEDSWAP)
   1678 			ffs_csum_swap((struct csum*)space,
   1679 			    (struct csum*)bp->b_data, size);
   1680 		else
   1681 #endif
   1682 			memcpy(bp->b_data, space, (u_int)size);
   1683 		space = (char *)space + size;
   1684 		if (waitfor == MNT_WAIT)
   1685 			error = bwrite(bp);
   1686 		else
   1687 			bawrite(bp);
   1688 	}
   1689 	if (!allerror && error)
   1690 		allerror = error;
   1691 	return (allerror);
   1692 }
   1693