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