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