ffs_vfsops.c revision 96755
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 96755 2002-05-16 21:28:32Z trhodes $
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_i_effnlink_valid = 1;
666	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
667	    M_WAITOK);
668	ump->um_blkatoff = ffs_blkatoff;
669	ump->um_truncate = ffs_truncate;
670	ump->um_update = ffs_update;
671	ump->um_valloc = ffs_valloc;
672	ump->um_vfree = ffs_vfree;
673	ump->um_balloc = ffs_balloc;
674	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
675	if (fs->fs_sbsize < SBSIZE)
676		bp->b_flags |= B_INVAL | B_NOCACHE;
677	brelse(bp);
678	bp = NULL;
679	fs = ump->um_fs;
680	fs->fs_ronly = ronly;
681	size = fs->fs_cssize;
682	blks = howmany(size, fs->fs_fsize);
683	if (fs->fs_contigsumsize > 0)
684		size += fs->fs_ncg * sizeof(int32_t);
685	size += fs->fs_ncg * sizeof(u_int8_t);
686	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
687	fs->fs_csp = space;
688	for (i = 0; i < blks; i += fs->fs_frag) {
689		size = fs->fs_bsize;
690		if (i + fs->fs_frag > blks)
691			size = (blks - i) * fs->fs_fsize;
692		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
693		    cred, &bp)) != 0) {
694			free(fs->fs_csp, M_UFSMNT);
695			goto out;
696		}
697		bcopy(bp->b_data, space, (u_int)size);
698		space = (char *)space + size;
699		brelse(bp);
700		bp = NULL;
701	}
702	if (fs->fs_contigsumsize > 0) {
703		fs->fs_maxcluster = lp = space;
704		for (i = 0; i < fs->fs_ncg; i++)
705			*lp++ = fs->fs_contigsumsize;
706		space = lp;
707	}
708	size = fs->fs_ncg * sizeof(u_int8_t);
709	fs->fs_contigdirs = (u_int8_t *)space;
710	bzero(fs->fs_contigdirs, size);
711	fs->fs_active = NULL;
712	/* Compatibility for old filesystems 	   XXX */
713	if (fs->fs_avgfilesize <= 0)		/* XXX */
714		fs->fs_avgfilesize = AVFILESIZ;	/* XXX */
715	if (fs->fs_avgfpdir <= 0)		/* XXX */
716		fs->fs_avgfpdir = AFPDIR;	/* XXX */
717	mp->mnt_data = (qaddr_t)ump;
718	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
719	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
720	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
721	    vfs_getvfs(&mp->mnt_stat.f_fsid))
722		vfs_getnewfsid(mp);
723	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
724	mp->mnt_flag |= MNT_LOCAL;
725	ump->um_mountp = mp;
726	ump->um_dev = dev;
727	ump->um_devvp = devvp;
728	ump->um_nindir = fs->fs_nindir;
729	ump->um_bptrtodb = fs->fs_fsbtodb;
730	ump->um_seqinc = fs->fs_frag;
731	for (i = 0; i < MAXQUOTAS; i++)
732		ump->um_quotas[i] = NULLVP;
733#ifdef UFS_EXTATTR
734	ufs_extattr_uepm_init(&ump->um_extattr);
735#endif
736	devvp->v_rdev->si_mountpoint = mp;
737	ffs_oldfscompat(fs);
738
739	/*
740	 * Set FS local "last mounted on" information (NULL pad)
741	 */
742	copystr(	mp->mnt_stat.f_mntonname,	/* mount point*/
743			fs->fs_fsmnt,			/* copy area*/
744			sizeof(fs->fs_fsmnt) - 1,	/* max size*/
745			&strsize);			/* real size*/
746	bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize);
747
748	if( mp->mnt_flag & MNT_ROOTFS) {
749		/*
750		 * Root mount; update timestamp in mount structure.
751		 * this will be used by the common root mount code
752		 * to update the system clock.
753		 */
754		mp->mnt_time = fs->fs_time;
755	}
756
757	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
758	maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;	/* XXX */
759	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
760		fs->fs_maxfilesize = maxfilesize;		/* XXX */
761	if (bigcgs) {
762		if (fs->fs_sparecon[0] <= 0)
763			fs->fs_sparecon[0] = fs->fs_cgsize;
764		fs->fs_cgsize = fs->fs_bsize;
765	}
766	if (ronly == 0) {
767		if ((fs->fs_flags & FS_DOSOFTDEP) &&
768		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
769			free(fs->fs_csp, M_UFSMNT);
770			goto out;
771		}
772		if (fs->fs_snapinum[0] != 0)
773			ffs_snapshot_mount(mp);
774		fs->fs_fmod = 1;
775		fs->fs_clean = 0;
776		(void) ffs_sbupdate(ump, MNT_WAIT);
777	}
778#ifdef UFS_EXTATTR
779#ifdef UFS_EXTATTR_AUTOSTART
780	/*
781	 *
782	 * Auto-starting does the following:
783	 *	- check for /.attribute in the fs, and extattr_start if so
784	 *	- for each file in .attribute, enable that file with
785	 * 	  an attribute of the same name.
786	 * Not clear how to report errors -- probably eat them.
787	 * This would all happen while the filesystem was busy/not
788	 * available, so would effectively be "atomic".
789	 */
790	(void) ufs_extattr_autostart(mp, td);
791#endif /* !UFS_EXTATTR_AUTOSTART */
792#endif /* !UFS_EXTATTR */
793	return (0);
794out:
795	devvp->v_rdev->si_mountpoint = NULL;
796	if (bp)
797		brelse(bp);
798	/* XXX: see comment above VOP_OPEN */
799#ifdef notyet
800	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, td);
801#else
802	(void)VOP_CLOSE(devvp, FREAD|FWRITE, cred, td);
803#endif
804	if (ump) {
805		free(ump->um_fs, M_UFSMNT);
806		free(ump, M_UFSMNT);
807		mp->mnt_data = (qaddr_t)0;
808	}
809	return (error);
810}
811
812/*
813 * Sanity checks for old filesystems.
814 *
815 * XXX - goes away some day.
816 */
817static int
818ffs_oldfscompat(fs)
819	struct fs *fs;
820{
821
822	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
823	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
824	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
825		fs->fs_nrpos = 8;				/* XXX */
826	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
827#if 0
828		int i;						/* XXX */
829		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
830								/* XXX */
831		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
832		for (i = 0; i < NIADDR; i++) {			/* XXX */
833			sizepb *= NINDIR(fs);			/* XXX */
834			fs->fs_maxfilesize += sizepb;		/* XXX */
835		}						/* XXX */
836#endif
837		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
838		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
839		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
840	}							/* XXX */
841	return (0);
842}
843
844/*
845 * unmount system call
846 */
847int
848ffs_unmount(mp, mntflags, td)
849	struct mount *mp;
850	int mntflags;
851	struct thread *td;
852{
853	struct ufsmount *ump = VFSTOUFS(mp);
854	struct fs *fs;
855	int error, flags;
856
857	flags = 0;
858	if (mntflags & MNT_FORCE) {
859		flags |= FORCECLOSE;
860	}
861#ifdef UFS_EXTATTR
862	if ((error = ufs_extattr_stop(mp, td))) {
863		if (error != EOPNOTSUPP)
864			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
865			    error);
866	} else {
867		ufs_extattr_uepm_destroy(&ump->um_extattr);
868	}
869#endif
870	if (mp->mnt_flag & MNT_SOFTDEP) {
871		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
872			return (error);
873	} else {
874		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
875			return (error);
876	}
877	fs = ump->um_fs;
878	if (bigcgs) {
879		fs->fs_cgsize = fs->fs_sparecon[0];
880		fs->fs_sparecon[0] = 0;
881	}
882	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
883		printf("%s: unmount pending error: blocks %d files %d\n",
884		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
885		fs->fs_pendingblocks = 0;
886		fs->fs_pendinginodes = 0;
887	}
888	if (fs->fs_ronly == 0) {
889		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
890		error = ffs_sbupdate(ump, MNT_WAIT);
891		if (error) {
892			fs->fs_clean = 0;
893			return (error);
894		}
895	}
896	ump->um_devvp->v_rdev->si_mountpoint = NULL;
897
898	vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, td, 0, 0);
899	/* XXX: see comment above VOP_OPEN */
900#ifdef notyet
901	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
902		NOCRED, td);
903#else
904	error = VOP_CLOSE(ump->um_devvp, FREAD|FWRITE, NOCRED, td);
905#endif
906
907	vrele(ump->um_devvp);
908
909	free(fs->fs_csp, M_UFSMNT);
910	free(fs, M_UFSMNT);
911	free(ump, M_UFSMNT);
912	mp->mnt_data = (qaddr_t)0;
913	mp->mnt_flag &= ~MNT_LOCAL;
914	return (error);
915}
916
917/*
918 * Flush out all the files in a filesystem.
919 */
920int
921ffs_flushfiles(mp, flags, td)
922	struct mount *mp;
923	int flags;
924	struct thread *td;
925{
926	struct ufsmount *ump;
927	int error;
928
929	ump = VFSTOUFS(mp);
930#ifdef QUOTA
931	if (mp->mnt_flag & MNT_QUOTA) {
932		int i;
933		error = vflush(mp, 0, SKIPSYSTEM|flags);
934		if (error)
935			return (error);
936		for (i = 0; i < MAXQUOTAS; i++) {
937			if (ump->um_quotas[i] == NULLVP)
938				continue;
939			quotaoff(td, mp, i);
940		}
941		/*
942		 * Here we fall through to vflush again to ensure
943		 * that we have gotten rid of all the system vnodes.
944		 */
945	}
946#endif
947	if (ump->um_devvp->v_flag & VCOPYONWRITE) {
948		if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
949			return (error);
950		ffs_snapshot_unmount(mp);
951		/*
952		 * Here we fall through to vflush again to ensure
953		 * that we have gotten rid of all the system vnodes.
954		 */
955	}
956        /*
957	 * Flush all the files.
958	 */
959	if ((error = vflush(mp, 0, flags)) != 0)
960		return (error);
961	/*
962	 * Flush filesystem metadata.
963	 */
964	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
965	error = VOP_FSYNC(ump->um_devvp, td->td_ucred, MNT_WAIT, td);
966	VOP_UNLOCK(ump->um_devvp, 0, td);
967	return (error);
968}
969
970/*
971 * Get filesystem statistics.
972 */
973int
974ffs_statfs(mp, sbp, td)
975	struct mount *mp;
976	struct statfs *sbp;
977	struct thread *td;
978{
979	struct ufsmount *ump;
980	struct fs *fs;
981
982	ump = VFSTOUFS(mp);
983	fs = ump->um_fs;
984	if (fs->fs_magic != FS_MAGIC)
985		panic("ffs_statfs");
986	sbp->f_bsize = fs->fs_fsize;
987	sbp->f_iosize = fs->fs_bsize;
988	sbp->f_blocks = fs->fs_dsize;
989	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
990	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
991	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
992	    dbtofsb(fs, fs->fs_pendingblocks);
993	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
994	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
995	if (sbp != &mp->mnt_stat) {
996		sbp->f_type = mp->mnt_vfc->vfc_typenum;
997		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
998			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
999		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
1000			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
1001	}
1002	return (0);
1003}
1004
1005/*
1006 * Go through the disk queues to initiate sandbagged IO;
1007 * go through the inodes to write those that have been modified;
1008 * initiate the writing of the super block if it has been modified.
1009 *
1010 * Note: we are always called with the filesystem marked `MPBUSY'.
1011 */
1012int
1013ffs_sync(mp, waitfor, cred, td)
1014	struct mount *mp;
1015	int waitfor;
1016	struct ucred *cred;
1017	struct thread *td;
1018{
1019	struct vnode *nvp, *vp, *devvp;
1020	struct inode *ip;
1021	struct ufsmount *ump = VFSTOUFS(mp);
1022	struct fs *fs;
1023	int error, count, wait, lockreq, allerror = 0;
1024
1025	fs = ump->um_fs;
1026	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1027		printf("fs = %s\n", fs->fs_fsmnt);
1028		panic("ffs_sync: rofs mod");
1029	}
1030	/*
1031	 * Write back each (modified) inode.
1032	 */
1033	wait = 0;
1034	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1035	if (waitfor == MNT_WAIT) {
1036		wait = 1;
1037		lockreq = LK_EXCLUSIVE;
1038	}
1039	mtx_lock(&mntvnode_mtx);
1040loop:
1041	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
1042		/*
1043		 * If the vnode that we are about to sync is no longer
1044		 * associated with this mount point, start over.
1045		 */
1046		if (vp->v_mount != mp)
1047			goto loop;
1048
1049		/*
1050		 * Depend on the mntvnode_slock to keep things stable enough
1051		 * for a quick test.  Since there might be hundreds of
1052		 * thousands of vnodes, we cannot afford even a subroutine
1053		 * call unless there's a good chance that we have work to do.
1054		 */
1055		nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1056		ip = VTOI(vp);
1057		if (vp->v_type == VNON || ((ip->i_flag &
1058		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1059		    TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1060			continue;
1061		}
1062		if (vp->v_type != VCHR) {
1063			mtx_unlock(&mntvnode_mtx);
1064			if ((error = vget(vp, lockreq, td)) != 0) {
1065				mtx_lock(&mntvnode_mtx);
1066				if (error == ENOENT)
1067					goto loop;
1068			} else {
1069				if ((error = VOP_FSYNC(vp, cred, waitfor, td)) != 0)
1070					allerror = error;
1071				VOP_UNLOCK(vp, 0, td);
1072				vrele(vp);
1073				mtx_lock(&mntvnode_mtx);
1074			}
1075		} else {
1076			mtx_unlock(&mntvnode_mtx);
1077			UFS_UPDATE(vp, wait);
1078			mtx_lock(&mntvnode_mtx);
1079		}
1080		if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp)
1081			goto loop;
1082	}
1083	mtx_unlock(&mntvnode_mtx);
1084	/*
1085	 * Force stale filesystem control information to be flushed.
1086	 */
1087	if (waitfor == MNT_WAIT) {
1088		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1089			allerror = error;
1090		/* Flushed work items may create new vnodes to clean */
1091		if (count) {
1092			mtx_lock(&mntvnode_mtx);
1093			goto loop;
1094		}
1095	}
1096#ifdef QUOTA
1097	qsync(mp);
1098#endif
1099	devvp = ump->um_devvp;
1100	mtx_lock(&devvp->v_interlock);
1101	if (waitfor != MNT_LAZY &&
1102	    (devvp->v_numoutput > 0 || TAILQ_FIRST(&devvp->v_dirtyblkhd))) {
1103		mtx_unlock(&devvp->v_interlock);
1104		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
1105		if ((error = VOP_FSYNC(devvp, cred, waitfor, td)) != 0)
1106			allerror = error;
1107		VOP_UNLOCK(devvp, 0, td);
1108		if (waitfor == MNT_WAIT) {
1109			mtx_lock(&mntvnode_mtx);
1110			goto loop;
1111		}
1112	} else
1113		mtx_unlock(&devvp->v_interlock);
1114	/*
1115	 * Write back modified superblock.
1116	 */
1117	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1118		allerror = error;
1119	return (allerror);
1120}
1121
1122/*
1123 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1124 * in from disk.  If it is in core, wait for the lock bit to clear, then
1125 * return the inode locked.  Detection and handling of mount points must be
1126 * done by the calling routine.
1127 */
1128static int ffs_inode_hash_lock;
1129/*
1130 * ffs_inode_hash_lock is a variable to manage mutual exclusion
1131 * of vnode allocation and intertion to the hash, especially to
1132 * avoid holding more than one vnodes for the same inode in the
1133 * hash table. ffs_inode_hash_lock must hence be tested-and-set
1134 * or cleared atomically, accomplished by ffs_inode_hash_mtx.
1135 *
1136 * As vnode allocation may block during MALLOC() and zone
1137 * allocation, we should also do msleep() to give away the CPU
1138 * if anyone else is allocating a vnode. lockmgr is not suitable
1139 * here because someone else may insert to the hash table the
1140 * vnode we are trying to allocate during our sleep, in which
1141 * case the hash table needs to be examined once again after
1142 * waking up.
1143 */
1144static struct mtx ffs_inode_hash_mtx;
1145
1146int
1147ffs_vget(mp, ino, flags, vpp)
1148	struct mount *mp;
1149	ino_t ino;
1150	int flags;
1151	struct vnode **vpp;
1152{
1153	struct fs *fs;
1154	struct inode *ip;
1155	struct ufsmount *ump;
1156	struct buf *bp;
1157	struct vnode *vp;
1158	dev_t dev;
1159	int error, want_wakeup;
1160
1161	ump = VFSTOUFS(mp);
1162	dev = ump->um_dev;
1163restart:
1164	if ((error = ufs_ihashget(dev, ino, flags, vpp)) != 0)
1165		return (error);
1166	if (*vpp != NULL)
1167		return (0);
1168
1169	/*
1170	 * Lock out the creation of new entries in the FFS hash table in
1171	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1172	 * may occur!
1173	 */
1174	mtx_lock(&ffs_inode_hash_mtx);
1175	if (ffs_inode_hash_lock) {
1176		while (ffs_inode_hash_lock) {
1177			ffs_inode_hash_lock = -1;
1178			msleep(&ffs_inode_hash_lock, &ffs_inode_hash_mtx, PVM, "ffsvgt", 0);
1179		}
1180		mtx_unlock(&ffs_inode_hash_mtx);
1181		goto restart;
1182	}
1183	ffs_inode_hash_lock = 1;
1184	mtx_unlock(&ffs_inode_hash_mtx);
1185
1186	/*
1187	 * If this MALLOC() is performed after the getnewvnode()
1188	 * it might block, leaving a vnode with a NULL v_data to be
1189	 * found by ffs_sync() if a sync happens to fire right then,
1190	 * which will cause a panic because ffs_sync() blindly
1191	 * dereferences vp->v_data (as well it should).
1192	 */
1193	MALLOC(ip, struct inode *, sizeof(struct inode),
1194	    ump->um_malloctype, M_WAITOK);
1195
1196	/* Allocate a new vnode/inode. */
1197	error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp);
1198	if (error) {
1199		/*
1200		 * Do not wake up processes while holding the mutex,
1201		 * otherwise the processes waken up immediately hit
1202		 * themselves into the mutex.
1203		 */
1204		mtx_lock(&ffs_inode_hash_mtx);
1205		want_wakeup = ffs_inode_hash_lock < 0;
1206		ffs_inode_hash_lock = 0;
1207		mtx_unlock(&ffs_inode_hash_mtx);
1208		if (want_wakeup)
1209			wakeup(&ffs_inode_hash_lock);
1210		*vpp = NULL;
1211		FREE(ip, ump->um_malloctype);
1212		return (error);
1213	}
1214	bzero((caddr_t)ip, sizeof(struct inode));
1215	/*
1216	 * FFS supports lock sharing in the stack of vnodes
1217	 */
1218	vp->v_vnlock = &vp->v_lock;
1219	lockinit(vp->v_vnlock, PINOD, "inode", VLKTIMEOUT, LK_CANRECURSE);
1220	vp->v_data = ip;
1221	ip->i_vnode = vp;
1222	ip->i_fs = fs = ump->um_fs;
1223	ip->i_dev = dev;
1224	ip->i_number = ino;
1225#ifdef QUOTA
1226	{
1227		int i;
1228		for (i = 0; i < MAXQUOTAS; i++)
1229			ip->i_dquot[i] = NODQUOT;
1230	}
1231#endif
1232	/*
1233	 * Put it onto its hash chain and lock it so that other requests for
1234	 * this inode will block if they arrive while we are sleeping waiting
1235	 * for old data structures to be purged or for the contents of the
1236	 * disk portion of this inode to be read.
1237	 */
1238	ufs_ihashins(ip);
1239
1240	/*
1241	 * Do not wake up processes while holding the mutex,
1242	 * otherwise the processes waken up immediately hit
1243	 * themselves into the mutex.
1244	 */
1245	mtx_lock(&ffs_inode_hash_mtx);
1246	want_wakeup = ffs_inode_hash_lock < 0;
1247	ffs_inode_hash_lock = 0;
1248	mtx_unlock(&ffs_inode_hash_mtx);
1249	if (want_wakeup)
1250		wakeup(&ffs_inode_hash_lock);
1251
1252	/* Read in the disk contents for the inode, copy into the inode. */
1253	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1254	    (int)fs->fs_bsize, NOCRED, &bp);
1255	if (error) {
1256		/*
1257		 * The inode does not contain anything useful, so it would
1258		 * be misleading to leave it on its hash chain. With mode
1259		 * still zero, it will be unlinked and returned to the free
1260		 * list by vput().
1261		 */
1262		brelse(bp);
1263		vput(vp);
1264		*vpp = NULL;
1265		return (error);
1266	}
1267	ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1268	if (DOINGSOFTDEP(vp))
1269		softdep_load_inodeblock(ip);
1270	else
1271		ip->i_effnlink = ip->i_nlink;
1272	bqrelse(bp);
1273
1274	/*
1275	 * Initialize the vnode from the inode, check for aliases.
1276	 * Note that the underlying vnode may have changed.
1277	 */
1278	error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1279	if (error) {
1280		vput(vp);
1281		*vpp = NULL;
1282		return (error);
1283	}
1284	/*
1285	 * Finish inode initialization now that aliasing has been resolved.
1286	 */
1287	ip->i_devvp = ump->um_devvp;
1288	VREF(ip->i_devvp);
1289	/*
1290	 * Set up a generation number for this inode if it does not
1291	 * already have one. This should only happen on old filesystems.
1292	 */
1293	if (ip->i_gen == 0) {
1294		ip->i_gen = random() / 2 + 1;
1295		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1296			ip->i_flag |= IN_MODIFIED;
1297	}
1298	/*
1299	 * Ensure that uid and gid are correct. This is a temporary
1300	 * fix until fsck has been changed to do the update.
1301	 */
1302	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
1303		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
1304		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
1305	}						/* XXX */
1306
1307	*vpp = vp;
1308	return (0);
1309}
1310
1311/*
1312 * File handle to vnode
1313 *
1314 * Have to be really careful about stale file handles:
1315 * - check that the inode number is valid
1316 * - call ffs_vget() to get the locked inode
1317 * - check for an unallocated inode (i_mode == 0)
1318 * - check that the given client host has export rights and return
1319 *   those rights via. exflagsp and credanonp
1320 */
1321int
1322ffs_fhtovp(mp, fhp, vpp)
1323	struct mount *mp;
1324	struct fid *fhp;
1325	struct vnode **vpp;
1326{
1327	struct ufid *ufhp;
1328	struct fs *fs;
1329
1330	ufhp = (struct ufid *)fhp;
1331	fs = VFSTOUFS(mp)->um_fs;
1332	if (ufhp->ufid_ino < ROOTINO ||
1333	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1334		return (ESTALE);
1335	return (ufs_fhtovp(mp, ufhp, vpp));
1336}
1337
1338/*
1339 * Vnode pointer to File handle
1340 */
1341/* ARGSUSED */
1342int
1343ffs_vptofh(vp, fhp)
1344	struct vnode *vp;
1345	struct fid *fhp;
1346{
1347	struct inode *ip;
1348	struct ufid *ufhp;
1349
1350	ip = VTOI(vp);
1351	ufhp = (struct ufid *)fhp;
1352	ufhp->ufid_len = sizeof(struct ufid);
1353	ufhp->ufid_ino = ip->i_number;
1354	ufhp->ufid_gen = ip->i_gen;
1355	return (0);
1356}
1357
1358/*
1359 * Initialize the filesystem; just use ufs_init.
1360 */
1361static int
1362ffs_init(vfsp)
1363	struct vfsconf *vfsp;
1364{
1365
1366	softdep_initialize();
1367	mtx_init(&ffs_inode_hash_mtx, "ifsvgt", NULL, MTX_DEF);
1368	return (ufs_init(vfsp));
1369}
1370
1371/*
1372 * Write a superblock and associated information back to disk.
1373 */
1374static int
1375ffs_sbupdate(mp, waitfor)
1376	struct ufsmount *mp;
1377	int waitfor;
1378{
1379	struct fs *dfs, *fs = mp->um_fs;
1380	struct buf *bp;
1381	int blks;
1382	void *space;
1383	int i, size, error, allerror = 0;
1384
1385	/*
1386	 * First write back the summary information.
1387	 */
1388	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1389	space = fs->fs_csp;
1390	for (i = 0; i < blks; i += fs->fs_frag) {
1391		size = fs->fs_bsize;
1392		if (i + fs->fs_frag > blks)
1393			size = (blks - i) * fs->fs_fsize;
1394		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1395		    size, 0, 0);
1396		bcopy(space, bp->b_data, (u_int)size);
1397		space = (char *)space + size;
1398		if (waitfor != MNT_WAIT)
1399			bawrite(bp);
1400		else if ((error = bwrite(bp)) != 0)
1401			allerror = error;
1402	}
1403	/*
1404	 * Now write back the superblock itself. If any errors occurred
1405	 * up to this point, then fail so that the superblock avoids
1406	 * being written out as clean.
1407	 */
1408	if (allerror)
1409		return (allerror);
1410	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1411	fs->fs_fmod = 0;
1412	fs->fs_time = time_second;
1413	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1414	/* Restore compatibility to old filesystems.		   XXX */
1415	dfs = (struct fs *)bp->b_data;				/* XXX */
1416	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
1417		dfs->fs_nrpos = -1;				/* XXX */
1418	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
1419		int32_t *lp, tmp;				/* XXX */
1420								/* XXX */
1421		lp = (int32_t *)&dfs->fs_qbmask;		/* XXX */
1422		tmp = lp[4];					/* XXX */
1423		for (i = 4; i > 0; i--)				/* XXX */
1424			lp[i] = lp[i-1];			/* XXX */
1425		lp[0] = tmp;					/* XXX */
1426	}							/* XXX */
1427	dfs->fs_maxfilesize = mp->um_savedmaxfilesize;		/* XXX */
1428	if (waitfor != MNT_WAIT)
1429		bawrite(bp);
1430	else if ((error = bwrite(bp)) != 0)
1431		allerror = error;
1432	return (allerror);
1433}
1434