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