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