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