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