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