ffs_vfsops.c revision 143666
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/ufs/ffs/ffs_vfsops.c 143666 2005-03-15 20:50:58Z phk $");
34
35#include "opt_mac.h"
36#include "opt_quota.h"
37#include "opt_ufs.h"
38#include "opt_ffs.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/mac.h>
46#include <sys/vnode.h>
47#include <sys/mount.h>
48#include <sys/bio.h>
49#include <sys/buf.h>
50#include <sys/conf.h>
51#include <sys/fcntl.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/uma.h>
66#include <vm/vm_page.h>
67
68#include <geom/geom.h>
69#include <geom/geom_vfs.h>
70
71static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
72
73static int	ffs_sbupdate(struct ufsmount *, int);
74static int	ffs_reload(struct mount *, struct thread *);
75static int	ffs_mountfs(struct vnode *, struct mount *, struct thread *);
76static void	ffs_oldfscompat_read(struct fs *, struct ufsmount *,
77		    ufs2_daddr_t);
78static void	ffs_oldfscompat_write(struct fs *, struct ufsmount *);
79static void	ffs_ifree(struct ufsmount *ump, struct inode *ip);
80static vfs_init_t ffs_init;
81static vfs_uninit_t ffs_uninit;
82static vfs_extattrctl_t ffs_extattrctl;
83static vfs_cmount_t ffs_cmount;
84static vfs_unmount_t ffs_unmount;
85static vfs_mount_t ffs_mount;
86static vfs_statfs_t ffs_statfs;
87static vfs_fhtovp_t ffs_fhtovp;
88static vfs_vptofh_t ffs_vptofh;
89static vfs_sync_t ffs_sync;
90
91static struct vfsops ufs_vfsops = {
92	.vfs_extattrctl =	ffs_extattrctl,
93	.vfs_fhtovp =		ffs_fhtovp,
94	.vfs_init =		ffs_init,
95	.vfs_mount =		ffs_mount,
96	.vfs_cmount =		ffs_cmount,
97	.vfs_quotactl =		ufs_quotactl,
98	.vfs_root =		ufs_root,
99	.vfs_statfs =		ffs_statfs,
100	.vfs_sync =		ffs_sync,
101	.vfs_uninit =		ffs_uninit,
102	.vfs_unmount =		ffs_unmount,
103	.vfs_vget =		ffs_vget,
104	.vfs_vptofh =		ffs_vptofh,
105};
106
107VFS_SET(ufs_vfsops, ufs, 0);
108
109static b_strategy_t ffs_geom_strategy;
110static b_write_t ffs_bufwrite;
111
112static struct buf_ops ffs_ops = {
113	.bop_name =	"FFS",
114	.bop_write =	ffs_bufwrite,
115	.bop_strategy =	ffs_geom_strategy,
116	.bop_sync =	bufsync,
117};
118
119static const char *ffs_opts[] = { "from", "export", NULL };
120
121static int
122ffs_mount(struct mount *mp, struct thread *td)
123{
124	struct vnode *devvp;
125	struct ufsmount *ump = 0;
126	struct fs *fs;
127	int error, flags;
128	mode_t accessmode;
129	struct nameidata ndp;
130	struct export_args export;
131	char *fspec;
132
133	if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
134		return (EINVAL);
135	if (uma_inode == NULL) {
136		uma_inode = uma_zcreate("FFS inode",
137		    sizeof(struct inode), NULL, NULL, NULL, NULL,
138		    UMA_ALIGN_PTR, 0);
139		uma_ufs1 = uma_zcreate("FFS1 dinode",
140		    sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
141		    UMA_ALIGN_PTR, 0);
142		uma_ufs2 = uma_zcreate("FFS2 dinode",
143		    sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
144		    UMA_ALIGN_PTR, 0);
145	}
146
147	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
148	if (error)
149		return (error);
150
151	/*
152	 * If updating, check whether changing from read-only to
153	 * read/write; if there is no device name, that's all we do.
154	 */
155	if (mp->mnt_flag & MNT_UPDATE) {
156		ump = VFSTOUFS(mp);
157		fs = ump->um_fs;
158		devvp = ump->um_devvp;
159		if (fs->fs_ronly == 0 &&
160		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
161			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
162				return (error);
163			/*
164			 * Flush any dirty data.
165			 */
166			if ((error = ffs_sync(mp, MNT_WAIT, td)) != 0) {
167				vn_finished_write(mp);
168				return (error);
169			}
170			/*
171			 * Check for and optionally get rid of files open
172			 * for writing.
173			 */
174			flags = WRITECLOSE;
175			if (mp->mnt_flag & MNT_FORCE)
176				flags |= FORCECLOSE;
177			if (mp->mnt_flag & MNT_SOFTDEP) {
178				error = softdep_flushfiles(mp, flags, td);
179			} else {
180				error = ffs_flushfiles(mp, flags, td);
181			}
182			if (error) {
183				vn_finished_write(mp);
184				return (error);
185			}
186			if (fs->fs_pendingblocks != 0 ||
187			    fs->fs_pendinginodes != 0) {
188				printf("%s: %s: blocks %jd files %d\n",
189				    fs->fs_fsmnt, "update error",
190				    (intmax_t)fs->fs_pendingblocks,
191				    fs->fs_pendinginodes);
192				fs->fs_pendingblocks = 0;
193				fs->fs_pendinginodes = 0;
194			}
195			if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
196				fs->fs_clean = 1;
197			if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) {
198				fs->fs_ronly = 0;
199				fs->fs_clean = 0;
200				vn_finished_write(mp);
201				return (error);
202			}
203			vn_finished_write(mp);
204			DROP_GIANT();
205			g_topology_lock();
206			g_access(ump->um_cp, 0, -1, 0);
207			g_topology_unlock();
208			PICKUP_GIANT();
209			fs->fs_ronly = 1;
210			mp->mnt_flag |= MNT_RDONLY;
211		}
212		if ((mp->mnt_flag & MNT_RELOAD) &&
213		    (error = ffs_reload(mp, td)) != 0)
214			return (error);
215		if (fs->fs_ronly &&
216		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
217			/*
218			 * If upgrade to read-write by non-root, then verify
219			 * that user has necessary permissions on the device.
220			 */
221			if (suser(td)) {
222				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
223				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
224				    td->td_ucred, td)) != 0) {
225					VOP_UNLOCK(devvp, 0, td);
226					return (error);
227				}
228				VOP_UNLOCK(devvp, 0, td);
229			}
230			fs->fs_flags &= ~FS_UNCLEAN;
231			if (fs->fs_clean == 0) {
232				fs->fs_flags |= FS_UNCLEAN;
233				if ((mp->mnt_flag & MNT_FORCE) ||
234				    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
235				     (fs->fs_flags & FS_DOSOFTDEP))) {
236					printf("WARNING: %s was not %s\n",
237					   fs->fs_fsmnt, "properly dismounted");
238				} else {
239					printf(
240"WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
241					    fs->fs_fsmnt);
242					return (EPERM);
243				}
244			}
245			DROP_GIANT();
246			g_topology_lock();
247			/*
248			 * If we're the root device, we may not have an E count
249			 * yet, get it now.
250			 */
251			if (ump->um_cp->ace == 0)
252				error = g_access(ump->um_cp, 0, 1, 1);
253			else
254				error = g_access(ump->um_cp, 0, 1, 0);
255			g_topology_unlock();
256			PICKUP_GIANT();
257			if (error)
258				return (error);
259			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
260				return (error);
261			fs->fs_ronly = 0;
262			mp->mnt_flag &= ~MNT_RDONLY;
263			fs->fs_clean = 0;
264			if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) {
265				vn_finished_write(mp);
266				return (error);
267			}
268			/* check to see if we need to start softdep */
269			if ((fs->fs_flags & FS_DOSOFTDEP) &&
270			    (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
271				vn_finished_write(mp);
272				return (error);
273			}
274			if (fs->fs_snapinum[0] != 0)
275				ffs_snapshot_mount(mp);
276			vn_finished_write(mp);
277		}
278		/*
279		 * Soft updates is incompatible with "async",
280		 * so if we are doing softupdates stop the user
281		 * from setting the async flag in an update.
282		 * Softdep_mount() clears it in an initial mount
283		 * or ro->rw remount.
284		 */
285		if (mp->mnt_flag & MNT_SOFTDEP)
286			mp->mnt_flag &= ~MNT_ASYNC;
287		/*
288		 * Keep MNT_ACLS flag if it is stored in superblock.
289		 */
290		if ((fs->fs_flags & FS_ACLS) != 0)
291			mp->mnt_flag |= MNT_ACLS;
292		/*
293		 * If not updating name, process export requests.
294		 */
295		error = vfs_copyopt(mp->mnt_optnew, "export", &export, sizeof export);
296		if (error == 0 && export.ex_flags != 0)
297			return (vfs_export(mp, &export));
298		/*
299		 * If this is a snapshot request, take the snapshot.
300		 */
301		if (mp->mnt_flag & MNT_SNAPSHOT)
302			return (ffs_snapshot(mp, fspec));
303	}
304
305	/*
306	 * Not an update, or updating the name: look up the name
307	 * and verify that it refers to a sensible disk device.
308	 */
309	NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
310	if ((error = namei(&ndp)) != 0)
311		return (error);
312	NDFREE(&ndp, NDF_ONLY_PNBUF);
313	devvp = ndp.ni_vp;
314	if (!vn_isdisk(devvp, &error)) {
315		vrele(devvp);
316		return (error);
317	}
318
319	/*
320	 * If mount by non-root, then verify that user has necessary
321	 * permissions on the device.
322	 */
323	if (suser(td)) {
324		accessmode = VREAD;
325		if ((mp->mnt_flag & MNT_RDONLY) == 0)
326			accessmode |= VWRITE;
327		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
328		if ((error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td))!= 0){
329			vput(devvp);
330			return (error);
331		}
332		VOP_UNLOCK(devvp, 0, td);
333	}
334
335	if (mp->mnt_flag & MNT_UPDATE) {
336		/*
337		 * Update only
338		 *
339		 * If it's not the same vnode, or at least the same device
340		 * then it's not correct.
341		 */
342
343		if (devvp->v_rdev != ump->um_devvp->v_rdev)
344			error = EINVAL;	/* needs translation */
345		vrele(devvp);
346		if (error)
347			return (error);
348	} else {
349		/*
350		 * New mount
351		 *
352		 * We need the name for the mount point (also used for
353		 * "last mounted on") copied in. If an error occurs,
354		 * the mount point is discarded by the upper level code.
355		 * Note that vfs_mount() populates f_mntonname for us.
356		 */
357		if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
358			vrele(devvp);
359			return (error);
360		}
361	}
362	vfs_mountedfrom(mp, fspec);
363	return (0);
364}
365
366/*
367 * Compatibility with old mount system call.
368 */
369
370static int
371ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
372{
373	struct ufs_args args;
374	int error;
375
376	if (data == NULL)
377		return (EINVAL);
378	error = copyin(data, &args, sizeof args);
379	if (error)
380		return (error);
381
382	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
383	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
384	error = kernel_mount(ma, flags);
385
386	return (error);
387}
388
389/*
390 * Reload all incore data for a filesystem (used after running fsck on
391 * the root filesystem and finding things to fix). The filesystem must
392 * be mounted read-only.
393 *
394 * Things to do to update the mount:
395 *	1) invalidate all cached meta-data.
396 *	2) re-read superblock from disk.
397 *	3) re-read summary information from disk.
398 *	4) invalidate all inactive vnodes.
399 *	5) invalidate all cached file data.
400 *	6) re-read inode data for all active vnodes.
401 */
402static int
403ffs_reload(struct mount *mp, struct thread *td)
404{
405	struct vnode *vp, *nvp, *devvp;
406	struct inode *ip;
407	void *space;
408	struct buf *bp;
409	struct fs *fs, *newfs;
410	struct ufsmount *ump;
411	ufs2_daddr_t sblockloc;
412	int i, blks, size, error;
413	int32_t *lp;
414
415	if ((mp->mnt_flag & MNT_RDONLY) == 0)
416		return (EINVAL);
417	ump = VFSTOUFS(mp);
418	/*
419	 * Step 1: invalidate all cached meta-data.
420	 */
421	devvp = VFSTOUFS(mp)->um_devvp;
422	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
423	if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
424		panic("ffs_reload: dirty1");
425	VOP_UNLOCK(devvp, 0, td);
426
427	/*
428	 * Step 2: re-read superblock from disk.
429	 */
430	fs = VFSTOUFS(mp)->um_fs;
431	if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
432	    NOCRED, &bp)) != 0)
433		return (error);
434	newfs = (struct fs *)bp->b_data;
435	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
436	     newfs->fs_magic != FS_UFS2_MAGIC) ||
437	    newfs->fs_bsize > MAXBSIZE ||
438	    newfs->fs_bsize < sizeof(struct fs)) {
439			brelse(bp);
440			return (EIO);		/* XXX needs translation */
441	}
442	/*
443	 * Copy pointer fields back into superblock before copying in	XXX
444	 * new superblock. These should really be in the ufsmount.	XXX
445	 * Note that important parameters (eg fs_ncg) are unchanged.
446	 */
447	newfs->fs_csp = fs->fs_csp;
448	newfs->fs_maxcluster = fs->fs_maxcluster;
449	newfs->fs_contigdirs = fs->fs_contigdirs;
450	newfs->fs_active = fs->fs_active;
451	/* The file system is still read-only. */
452	newfs->fs_ronly = 1;
453	sblockloc = fs->fs_sblockloc;
454	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
455	brelse(bp);
456	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
457	ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
458	UFS_LOCK(ump);
459	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
460		printf("%s: reload pending error: blocks %jd files %d\n",
461		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
462		    fs->fs_pendinginodes);
463		fs->fs_pendingblocks = 0;
464		fs->fs_pendinginodes = 0;
465	}
466	UFS_UNLOCK(ump);
467
468	/*
469	 * Step 3: re-read summary information from disk.
470	 */
471	blks = howmany(fs->fs_cssize, fs->fs_fsize);
472	space = fs->fs_csp;
473	for (i = 0; i < blks; i += fs->fs_frag) {
474		size = fs->fs_bsize;
475		if (i + fs->fs_frag > blks)
476			size = (blks - i) * fs->fs_fsize;
477		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
478		    NOCRED, &bp);
479		if (error)
480			return (error);
481		bcopy(bp->b_data, space, (u_int)size);
482		space = (char *)space + size;
483		brelse(bp);
484	}
485	/*
486	 * We no longer know anything about clusters per cylinder group.
487	 */
488	if (fs->fs_contigsumsize > 0) {
489		lp = fs->fs_maxcluster;
490		for (i = 0; i < fs->fs_ncg; i++)
491			*lp++ = fs->fs_contigsumsize;
492	}
493
494loop:
495	MNT_ILOCK(mp);
496	MNT_VNODE_FOREACH(vp, mp, nvp) {
497		VI_LOCK(vp);
498		if (vp->v_iflag & VI_DOOMED) {
499			VI_UNLOCK(vp);
500			continue;
501		}
502		MNT_IUNLOCK(mp);
503		/*
504		 * Step 4: invalidate all cached file data.
505		 */
506		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
507			goto loop;
508		}
509		if (vinvalbuf(vp, 0, td, 0, 0))
510			panic("ffs_reload: dirty2");
511		/*
512		 * Step 5: re-read inode data for all active vnodes.
513		 */
514		ip = VTOI(vp);
515		error =
516		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
517		    (int)fs->fs_bsize, NOCRED, &bp);
518		if (error) {
519			VOP_UNLOCK(vp, 0, td);
520			vrele(vp);
521			return (error);
522		}
523		ffs_load_inode(bp, ip, fs, ip->i_number);
524		ip->i_effnlink = ip->i_nlink;
525		brelse(bp);
526		VOP_UNLOCK(vp, 0, td);
527		vrele(vp);
528		MNT_ILOCK(mp);
529	}
530	MNT_IUNLOCK(mp);
531	return (0);
532}
533
534/*
535 * Possible superblock locations ordered from most to least likely.
536 */
537static int sblock_try[] = SBLOCKSEARCH;
538
539/*
540 * Common code for mount and mountroot
541 */
542static int
543ffs_mountfs(devvp, mp, td)
544	struct vnode *devvp;
545	struct mount *mp;
546	struct thread *td;
547{
548	struct ufsmount *ump;
549	struct buf *bp;
550	struct fs *fs;
551	struct cdev *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	struct g_consumer *cp;
558
559	dev = devvp->v_rdev;
560	cred = td ? td->td_ucred : NOCRED;
561
562	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
563	DROP_GIANT();
564	g_topology_lock();
565	error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
566
567	/*
568	 * If we are a root mount, drop the E flag so fsck can do its magic.
569	 * We will pick it up again when we remount R/W.
570	 */
571	if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
572		error = g_access(cp, 0, 0, -1);
573	g_topology_unlock();
574	PICKUP_GIANT();
575	VOP_UNLOCK(devvp, 0, td);
576	if (error)
577		return (error);
578	if (devvp->v_rdev->si_iosize_max != 0)
579		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
580	if (mp->mnt_iosize_max > MAXPHYS)
581		mp->mnt_iosize_max = MAXPHYS;
582
583	devvp->v_bufobj.bo_private = cp;
584	devvp->v_bufobj.bo_ops = &ffs_ops;
585
586	bp = NULL;
587	ump = NULL;
588	fs = NULL;
589	sblockloc = 0;
590	/*
591	 * Try reading the superblock in each of its possible locations.
592	 */
593	for (i = 0; sblock_try[i] != -1; i++) {
594		if ((error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
595		    cred, &bp)) != 0)
596			goto out;
597		fs = (struct fs *)bp->b_data;
598		sblockloc = sblock_try[i];
599		if ((fs->fs_magic == FS_UFS1_MAGIC ||
600		     (fs->fs_magic == FS_UFS2_MAGIC &&
601		      (fs->fs_sblockloc == sblockloc ||
602		       (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
603		    fs->fs_bsize <= MAXBSIZE &&
604		    fs->fs_bsize >= sizeof(struct fs))
605			break;
606		brelse(bp);
607		bp = NULL;
608	}
609	if (sblock_try[i] == -1) {
610		error = EINVAL;		/* XXX needs translation */
611		goto out;
612	}
613	fs->fs_fmod = 0;
614	fs->fs_flags &= ~FS_INDEXDIRS;	/* no support for directory indicies */
615	fs->fs_flags &= ~FS_UNCLEAN;
616	if (fs->fs_clean == 0) {
617		fs->fs_flags |= FS_UNCLEAN;
618		if (ronly || (mp->mnt_flag & MNT_FORCE) ||
619		    ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
620		     (fs->fs_flags & FS_DOSOFTDEP))) {
621			printf(
622"WARNING: %s was not properly dismounted\n",
623			    fs->fs_fsmnt);
624		} else {
625			printf(
626"WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
627			    fs->fs_fsmnt);
628			error = EPERM;
629			goto out;
630		}
631		if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
632		    (mp->mnt_flag & MNT_FORCE)) {
633			printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
634			    (intmax_t)fs->fs_pendingblocks,
635			    fs->fs_pendinginodes);
636			fs->fs_pendingblocks = 0;
637			fs->fs_pendinginodes = 0;
638		}
639	}
640	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
641		printf("%s: mount pending error: blocks %jd files %d\n",
642		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
643		    fs->fs_pendinginodes);
644		fs->fs_pendingblocks = 0;
645		fs->fs_pendinginodes = 0;
646	}
647	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
648	ump->um_cp = cp;
649	ump->um_bo = &devvp->v_bufobj;
650	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
651	if (fs->fs_magic == FS_UFS1_MAGIC) {
652		ump->um_fstype = UFS1;
653		ump->um_balloc = ffs_balloc_ufs1;
654	} else {
655		ump->um_fstype = UFS2;
656		ump->um_balloc = ffs_balloc_ufs2;
657	}
658	ump->um_blkatoff = ffs_blkatoff;
659	ump->um_truncate = ffs_truncate;
660	ump->um_update = ffs_update;
661	ump->um_valloc = ffs_valloc;
662	ump->um_vfree = ffs_vfree;
663	ump->um_ifree = ffs_ifree;
664	mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
665	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
666	if (fs->fs_sbsize < SBLOCKSIZE)
667		bp->b_flags |= B_INVAL | B_NOCACHE;
668	brelse(bp);
669	bp = NULL;
670	fs = ump->um_fs;
671	ffs_oldfscompat_read(fs, ump, sblockloc);
672	fs->fs_ronly = ronly;
673	size = fs->fs_cssize;
674	blks = howmany(size, fs->fs_fsize);
675	if (fs->fs_contigsumsize > 0)
676		size += fs->fs_ncg * sizeof(int32_t);
677	size += fs->fs_ncg * sizeof(u_int8_t);
678	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
679	fs->fs_csp = space;
680	for (i = 0; i < blks; i += fs->fs_frag) {
681		size = fs->fs_bsize;
682		if (i + fs->fs_frag > blks)
683			size = (blks - i) * fs->fs_fsize;
684		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
685		    cred, &bp)) != 0) {
686			free(fs->fs_csp, M_UFSMNT);
687			goto out;
688		}
689		bcopy(bp->b_data, space, (u_int)size);
690		space = (char *)space + size;
691		brelse(bp);
692		bp = NULL;
693	}
694	if (fs->fs_contigsumsize > 0) {
695		fs->fs_maxcluster = lp = space;
696		for (i = 0; i < fs->fs_ncg; i++)
697			*lp++ = fs->fs_contigsumsize;
698		space = lp;
699	}
700	size = fs->fs_ncg * sizeof(u_int8_t);
701	fs->fs_contigdirs = (u_int8_t *)space;
702	bzero(fs->fs_contigdirs, size);
703	fs->fs_active = NULL;
704	mp->mnt_data = (qaddr_t)ump;
705	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
706	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
707	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
708	    vfs_getvfs(&mp->mnt_stat.f_fsid))
709		vfs_getnewfsid(mp);
710	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
711	mp->mnt_flag |= MNT_LOCAL;
712	if ((fs->fs_flags & FS_MULTILABEL) != 0)
713#ifdef MAC
714		mp->mnt_flag |= MNT_MULTILABEL;
715#else
716		printf(
717"WARNING: %s: multilabel flag on fs but no MAC support\n",
718		    fs->fs_fsmnt);
719#endif
720	if ((fs->fs_flags & FS_ACLS) != 0)
721#ifdef UFS_ACL
722		mp->mnt_flag |= MNT_ACLS;
723#else
724		printf(
725"WARNING: %s: ACLs flag on fs but no ACLs support\n",
726		    fs->fs_fsmnt);
727#endif
728	ump->um_mountp = mp;
729	ump->um_dev = dev;
730	ump->um_devvp = devvp;
731	ump->um_nindir = fs->fs_nindir;
732	ump->um_bptrtodb = fs->fs_fsbtodb;
733	ump->um_seqinc = fs->fs_frag;
734	for (i = 0; i < MAXQUOTAS; i++)
735		ump->um_quotas[i] = NULLVP;
736#ifdef UFS_EXTATTR
737	ufs_extattr_uepm_init(&ump->um_extattr);
738#endif
739	/*
740	 * Set FS local "last mounted on" information (NULL pad)
741	 */
742	vfs_mountedfrom(mp, fs->fs_fsmnt);
743
744	if( mp->mnt_flag & MNT_ROOTFS) {
745		/*
746		 * Root mount; update timestamp in mount structure.
747		 * this will be used by the common root mount code
748		 * to update the system clock.
749		 */
750		mp->mnt_time = fs->fs_time;
751	}
752
753	if (ronly == 0) {
754		if ((fs->fs_flags & FS_DOSOFTDEP) &&
755		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
756			free(fs->fs_csp, M_UFSMNT);
757			goto out;
758		}
759		if (fs->fs_snapinum[0] != 0)
760			ffs_snapshot_mount(mp);
761		fs->fs_fmod = 1;
762		fs->fs_clean = 0;
763		(void) ffs_sbupdate(ump, MNT_WAIT);
764	}
765	/*
766	 * Initialize filesystem stat information in mount struct.
767	 */
768#ifdef UFS_EXTATTR
769#ifdef UFS_EXTATTR_AUTOSTART
770	/*
771	 *
772	 * Auto-starting does the following:
773	 *	- check for /.attribute in the fs, and extattr_start if so
774	 *	- for each file in .attribute, enable that file with
775	 * 	  an attribute of the same name.
776	 * Not clear how to report errors -- probably eat them.
777	 * This would all happen while the filesystem was busy/not
778	 * available, so would effectively be "atomic".
779	 */
780	(void) ufs_extattr_autostart(mp, td);
781#endif /* !UFS_EXTATTR_AUTOSTART */
782#endif /* !UFS_EXTATTR */
783#ifndef QUOTA
784	mp->mnt_kern_flag |= MNTK_MPSAFE;
785#endif
786	return (0);
787out:
788	if (bp)
789		brelse(bp);
790	if (cp != NULL) {
791		DROP_GIANT();
792		g_topology_lock();
793		g_vfs_close(cp, td);
794		g_topology_unlock();
795		PICKUP_GIANT();
796	}
797	if (ump) {
798		mtx_destroy(UFS_MTX(ump));
799		free(ump->um_fs, M_UFSMNT);
800		free(ump, M_UFSMNT);
801		mp->mnt_data = (qaddr_t)0;
802	}
803	return (error);
804}
805
806#include <sys/sysctl.h>
807static int bigcgs = 0;
808SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
809
810/*
811 * Sanity checks for loading old filesystem superblocks.
812 * See ffs_oldfscompat_write below for unwound actions.
813 *
814 * XXX - Parts get retired eventually.
815 * Unfortunately new bits get added.
816 */
817static void
818ffs_oldfscompat_read(fs, ump, sblockloc)
819	struct fs *fs;
820	struct ufsmount *ump;
821	ufs2_daddr_t sblockloc;
822{
823	off_t maxfilesize;
824
825	/*
826	 * If not yet done, update fs_flags location and value of fs_sblockloc.
827	 */
828	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
829		fs->fs_flags = fs->fs_old_flags;
830		fs->fs_old_flags |= FS_FLAGS_UPDATED;
831		fs->fs_sblockloc = sblockloc;
832	}
833	/*
834	 * If not yet done, update UFS1 superblock with new wider fields.
835	 */
836	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
837		fs->fs_maxbsize = fs->fs_bsize;
838		fs->fs_time = fs->fs_old_time;
839		fs->fs_size = fs->fs_old_size;
840		fs->fs_dsize = fs->fs_old_dsize;
841		fs->fs_csaddr = fs->fs_old_csaddr;
842		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
843		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
844		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
845		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
846	}
847	if (fs->fs_magic == FS_UFS1_MAGIC &&
848	    fs->fs_old_inodefmt < FS_44INODEFMT) {
849		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
850		fs->fs_qbmask = ~fs->fs_bmask;
851		fs->fs_qfmask = ~fs->fs_fmask;
852	}
853	if (fs->fs_magic == FS_UFS1_MAGIC) {
854		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
855		maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;
856		if (fs->fs_maxfilesize > maxfilesize)
857			fs->fs_maxfilesize = maxfilesize;
858	}
859	/* Compatibility for old filesystems */
860	if (fs->fs_avgfilesize <= 0)
861		fs->fs_avgfilesize = AVFILESIZ;
862	if (fs->fs_avgfpdir <= 0)
863		fs->fs_avgfpdir = AFPDIR;
864	if (bigcgs) {
865		fs->fs_save_cgsize = fs->fs_cgsize;
866		fs->fs_cgsize = fs->fs_bsize;
867	}
868}
869
870/*
871 * Unwinding superblock updates for old filesystems.
872 * See ffs_oldfscompat_read above for details.
873 *
874 * XXX - Parts get retired eventually.
875 * Unfortunately new bits get added.
876 */
877static void
878ffs_oldfscompat_write(fs, ump)
879	struct fs *fs;
880	struct ufsmount *ump;
881{
882
883	/*
884	 * Copy back UFS2 updated fields that UFS1 inspects.
885	 */
886	if (fs->fs_magic == FS_UFS1_MAGIC) {
887		fs->fs_old_time = fs->fs_time;
888		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
889		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
890		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
891		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
892		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
893	}
894	if (bigcgs) {
895		fs->fs_cgsize = fs->fs_save_cgsize;
896		fs->fs_save_cgsize = 0;
897	}
898}
899
900/*
901 * unmount system call
902 */
903static int
904ffs_unmount(mp, mntflags, td)
905	struct mount *mp;
906	int mntflags;
907	struct thread *td;
908{
909	struct ufsmount *ump = VFSTOUFS(mp);
910	struct fs *fs;
911	int error, flags;
912
913	flags = 0;
914	if (mntflags & MNT_FORCE) {
915		flags |= FORCECLOSE;
916	}
917#ifdef UFS_EXTATTR
918	if ((error = ufs_extattr_stop(mp, td))) {
919		if (error != EOPNOTSUPP)
920			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
921			    error);
922	} else {
923		ufs_extattr_uepm_destroy(&ump->um_extattr);
924	}
925#endif
926	if (mp->mnt_flag & MNT_SOFTDEP) {
927		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
928			return (error);
929	} else {
930		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
931			return (error);
932	}
933	fs = ump->um_fs;
934	UFS_LOCK(ump);
935	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
936		printf("%s: unmount pending error: blocks %jd files %d\n",
937		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
938		    fs->fs_pendinginodes);
939		fs->fs_pendingblocks = 0;
940		fs->fs_pendinginodes = 0;
941	}
942	UFS_UNLOCK(ump);
943	if (fs->fs_ronly == 0) {
944		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
945		error = ffs_sbupdate(ump, MNT_WAIT);
946		if (error) {
947			fs->fs_clean = 0;
948			return (error);
949		}
950	}
951	DROP_GIANT();
952	g_topology_lock();
953	g_vfs_close(ump->um_cp, td);
954	g_topology_unlock();
955	PICKUP_GIANT();
956	vrele(ump->um_devvp);
957	mtx_destroy(UFS_MTX(ump));
958	free(fs->fs_csp, M_UFSMNT);
959	free(fs, M_UFSMNT);
960	free(ump, M_UFSMNT);
961	mp->mnt_data = (qaddr_t)0;
962	mp->mnt_flag &= ~MNT_LOCAL;
963	return (error);
964}
965
966/*
967 * Flush out all the files in a filesystem.
968 */
969int
970ffs_flushfiles(mp, flags, td)
971	struct mount *mp;
972	int flags;
973	struct thread *td;
974{
975	struct ufsmount *ump;
976	int error;
977
978	ump = VFSTOUFS(mp);
979#ifdef QUOTA
980	if (mp->mnt_flag & MNT_QUOTA) {
981		int i;
982		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
983		if (error)
984			return (error);
985		for (i = 0; i < MAXQUOTAS; i++) {
986			if (ump->um_quotas[i] == NULLVP)
987				continue;
988			quotaoff(td, mp, i);
989		}
990		/*
991		 * Here we fall through to vflush again to ensure
992		 * that we have gotten rid of all the system vnodes.
993		 */
994	}
995#endif
996	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
997	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
998		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
999			return (error);
1000		ffs_snapshot_unmount(mp);
1001		/*
1002		 * Here we fall through to vflush again to ensure
1003		 * that we have gotten rid of all the system vnodes.
1004		 */
1005	}
1006        /*
1007	 * Flush all the files.
1008	 */
1009	if ((error = vflush(mp, 0, flags, td)) != 0)
1010		return (error);
1011	/*
1012	 * Flush filesystem metadata.
1013	 */
1014	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
1015	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1016	VOP_UNLOCK(ump->um_devvp, 0, td);
1017	return (error);
1018}
1019
1020/*
1021 * Get filesystem statistics.
1022 */
1023static int
1024ffs_statfs(mp, sbp, td)
1025	struct mount *mp;
1026	struct statfs *sbp;
1027	struct thread *td;
1028{
1029	struct ufsmount *ump;
1030	struct fs *fs;
1031
1032	ump = VFSTOUFS(mp);
1033	fs = ump->um_fs;
1034	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1035		panic("ffs_statfs");
1036	sbp->f_version = STATFS_VERSION;
1037	sbp->f_bsize = fs->fs_fsize;
1038	sbp->f_iosize = fs->fs_bsize;
1039	sbp->f_blocks = fs->fs_dsize;
1040	UFS_LOCK(ump);
1041	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1042	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1043	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1044	    dbtofsb(fs, fs->fs_pendingblocks);
1045	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1046	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1047	UFS_UNLOCK(ump);
1048	sbp->f_namemax = NAME_MAX;
1049	return (0);
1050}
1051
1052/*
1053 * Go through the disk queues to initiate sandbagged IO;
1054 * go through the inodes to write those that have been modified;
1055 * initiate the writing of the super block if it has been modified.
1056 *
1057 * Note: we are always called with the filesystem marked `MPBUSY'.
1058 */
1059static int
1060ffs_sync(mp, waitfor, td)
1061	struct mount *mp;
1062	int waitfor;
1063	struct thread *td;
1064{
1065	struct vnode *nvp, *vp, *devvp;
1066	struct inode *ip;
1067	struct ufsmount *ump = VFSTOUFS(mp);
1068	struct fs *fs;
1069	int error, count, wait, lockreq, allerror = 0;
1070	struct bufobj *bo;
1071
1072	fs = ump->um_fs;
1073	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1074		printf("fs = %s\n", fs->fs_fsmnt);
1075		panic("ffs_sync: rofs mod");
1076	}
1077	/*
1078	 * Write back each (modified) inode.
1079	 */
1080	wait = 0;
1081	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1082	if (waitfor == MNT_WAIT) {
1083		wait = 1;
1084		lockreq = LK_EXCLUSIVE;
1085	}
1086	lockreq |= LK_INTERLOCK;
1087	MNT_ILOCK(mp);
1088loop:
1089	MNT_VNODE_FOREACH(vp, mp, nvp) {
1090		/*
1091		 * Depend on the mntvnode_slock to keep things stable enough
1092		 * for a quick test.  Since there might be hundreds of
1093		 * thousands of vnodes, we cannot afford even a subroutine
1094		 * call unless there's a good chance that we have work to do.
1095		 */
1096		VI_LOCK(vp);
1097		if (vp->v_iflag & VI_DOOMED) {
1098			VI_UNLOCK(vp);
1099			continue;
1100		}
1101		ip = VTOI(vp);
1102		if (vp->v_type == VNON || ((ip->i_flag &
1103		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1104		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1105			VI_UNLOCK(vp);
1106			continue;
1107		}
1108		MNT_IUNLOCK(mp);
1109		if ((error = vget(vp, lockreq, td)) != 0) {
1110			MNT_ILOCK(mp);
1111			if (error == ENOENT)
1112				goto loop;
1113			continue;
1114		}
1115		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1116			allerror = error;
1117		VOP_UNLOCK(vp, 0, td);
1118		vrele(vp);
1119		MNT_ILOCK(mp);
1120	}
1121	MNT_IUNLOCK(mp);
1122	/*
1123	 * Force stale filesystem control information to be flushed.
1124	 */
1125	if (waitfor == MNT_WAIT) {
1126		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1127			allerror = error;
1128		/* Flushed work items may create new vnodes to clean */
1129		if (allerror == 0 && count) {
1130			MNT_ILOCK(mp);
1131			goto loop;
1132		}
1133	}
1134#ifdef QUOTA
1135	qsync(mp);
1136#endif
1137	devvp = ump->um_devvp;
1138	VI_LOCK(devvp);
1139	bo = &devvp->v_bufobj;
1140	if (waitfor != MNT_LAZY &&
1141	    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1142		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
1143		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1144			allerror = error;
1145		VOP_UNLOCK(devvp, 0, td);
1146		if (allerror == 0 && waitfor == MNT_WAIT) {
1147			MNT_ILOCK(mp);
1148			goto loop;
1149		}
1150	} else
1151		VI_UNLOCK(devvp);
1152	/*
1153	 * Write back modified superblock.
1154	 */
1155	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1156		allerror = error;
1157	return (allerror);
1158}
1159
1160int
1161ffs_vget(mp, ino, flags, vpp)
1162	struct mount *mp;
1163	ino_t ino;
1164	int flags;
1165	struct vnode **vpp;
1166{
1167	struct fs *fs;
1168	struct inode *ip;
1169	struct ufsmount *ump;
1170	struct buf *bp;
1171	struct vnode *vp;
1172	struct cdev *dev;
1173	int error;
1174
1175	error = vfs_hash_get(mp, ino, flags, curthread, vpp);
1176	if (error || *vpp != NULL)
1177		return (error);
1178
1179	/*
1180	 * We do not lock vnode creation as it is believed to be too
1181	 * expensive for such rare case as simultaneous creation of vnode
1182	 * for same ino by different processes. We just allow them to race
1183	 * and check later to decide who wins. Let the race begin!
1184	 */
1185
1186	ump = VFSTOUFS(mp);
1187	dev = ump->um_dev;
1188	fs = ump->um_fs;
1189
1190	/*
1191	 * If this MALLOC() is performed after the getnewvnode()
1192	 * it might block, leaving a vnode with a NULL v_data to be
1193	 * found by ffs_sync() if a sync happens to fire right then,
1194	 * which will cause a panic because ffs_sync() blindly
1195	 * dereferences vp->v_data (as well it should).
1196	 */
1197	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1198
1199	/* Allocate a new vnode/inode. */
1200	if (fs->fs_magic == FS_UFS1_MAGIC)
1201		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1202	else
1203		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1204	if (error) {
1205		*vpp = NULL;
1206		uma_zfree(uma_inode, ip);
1207		return (error);
1208	}
1209	/*
1210	 * FFS supports recursive locking.
1211	 */
1212	vp->v_vnlock->lk_flags |= LK_CANRECURSE;
1213	vp->v_data = ip;
1214	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1215	ip->i_vnode = vp;
1216	ip->i_ump = ump;
1217	ip->i_fs = fs;
1218	ip->i_dev = dev;
1219	ip->i_number = ino;
1220#ifdef QUOTA
1221	{
1222		int i;
1223		for (i = 0; i < MAXQUOTAS; i++)
1224			ip->i_dquot[i] = NODQUOT;
1225	}
1226#endif
1227
1228	error = vfs_hash_insert(vp, ino, flags, curthread, vpp);
1229	if (error || *vpp != NULL)
1230		return (error);
1231
1232	/* Read in the disk contents for the inode, copy into the inode. */
1233	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1234	    (int)fs->fs_bsize, NOCRED, &bp);
1235	if (error) {
1236		/*
1237		 * The inode does not contain anything useful, so it would
1238		 * be misleading to leave it on its hash chain. With mode
1239		 * still zero, it will be unlinked and returned to the free
1240		 * list by vput().
1241		 */
1242		brelse(bp);
1243		vput(vp);
1244		*vpp = NULL;
1245		return (error);
1246	}
1247	if (ip->i_ump->um_fstype == UFS1)
1248		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1249	else
1250		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1251	ffs_load_inode(bp, ip, fs, ino);
1252	if (DOINGSOFTDEP(vp))
1253		softdep_load_inodeblock(ip);
1254	else
1255		ip->i_effnlink = ip->i_nlink;
1256	bqrelse(bp);
1257
1258	/*
1259	 * Initialize the vnode from the inode, check for aliases.
1260	 * Note that the underlying vnode may have changed.
1261	 */
1262	if (ip->i_ump->um_fstype == UFS1)
1263		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1264	else
1265		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1266	if (error) {
1267		vput(vp);
1268		*vpp = NULL;
1269		return (error);
1270	}
1271
1272	/*
1273	 * Finish inode initialization.
1274	 */
1275
1276	/*
1277	 * Set up a generation number for this inode if it does not
1278	 * already have one. This should only happen on old filesystems.
1279	 */
1280	if (ip->i_gen == 0) {
1281		ip->i_gen = arc4random() / 2 + 1;
1282		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1283			ip->i_flag |= IN_MODIFIED;
1284			DIP_SET(ip, i_gen, ip->i_gen);
1285		}
1286	}
1287	/*
1288	 * Ensure that uid and gid are correct. This is a temporary
1289	 * fix until fsck has been changed to do the update.
1290	 */
1291	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
1292	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
1293		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
1294		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1295	}						/* XXX */
1296
1297#ifdef MAC
1298	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1299		/*
1300		 * If this vnode is already allocated, and we're running
1301		 * multi-label, attempt to perform a label association
1302		 * from the extended attributes on the inode.
1303		 */
1304		error = mac_associate_vnode_extattr(mp, vp);
1305		if (error) {
1306			/* ufs_inactive will release ip->i_devvp ref. */
1307			vput(vp);
1308			*vpp = NULL;
1309			return (error);
1310		}
1311	}
1312#endif
1313
1314	*vpp = vp;
1315	return (0);
1316}
1317
1318/*
1319 * File handle to vnode
1320 *
1321 * Have to be really careful about stale file handles:
1322 * - check that the inode number is valid
1323 * - call ffs_vget() to get the locked inode
1324 * - check for an unallocated inode (i_mode == 0)
1325 * - check that the given client host has export rights and return
1326 *   those rights via. exflagsp and credanonp
1327 */
1328static int
1329ffs_fhtovp(mp, fhp, vpp)
1330	struct mount *mp;
1331	struct fid *fhp;
1332	struct vnode **vpp;
1333{
1334	struct ufid *ufhp;
1335	struct fs *fs;
1336
1337	ufhp = (struct ufid *)fhp;
1338	fs = VFSTOUFS(mp)->um_fs;
1339	if (ufhp->ufid_ino < ROOTINO ||
1340	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1341		return (ESTALE);
1342	return (ufs_fhtovp(mp, ufhp, vpp));
1343}
1344
1345/*
1346 * Vnode pointer to File handle
1347 */
1348/* ARGSUSED */
1349static int
1350ffs_vptofh(vp, fhp)
1351	struct vnode *vp;
1352	struct fid *fhp;
1353{
1354	struct inode *ip;
1355	struct ufid *ufhp;
1356
1357	ip = VTOI(vp);
1358	ufhp = (struct ufid *)fhp;
1359	ufhp->ufid_len = sizeof(struct ufid);
1360	ufhp->ufid_ino = ip->i_number;
1361	ufhp->ufid_gen = ip->i_gen;
1362	return (0);
1363}
1364
1365/*
1366 * Initialize the filesystem.
1367 */
1368static int
1369ffs_init(vfsp)
1370	struct vfsconf *vfsp;
1371{
1372
1373	softdep_initialize();
1374	return (ufs_init(vfsp));
1375}
1376
1377/*
1378 * Undo the work of ffs_init().
1379 */
1380static int
1381ffs_uninit(vfsp)
1382	struct vfsconf *vfsp;
1383{
1384	int ret;
1385
1386	ret = ufs_uninit(vfsp);
1387	softdep_uninitialize();
1388	return (ret);
1389}
1390
1391/*
1392 * Write a superblock and associated information back to disk.
1393 */
1394static int
1395ffs_sbupdate(mp, waitfor)
1396	struct ufsmount *mp;
1397	int waitfor;
1398{
1399	struct fs *fs = mp->um_fs;
1400	struct buf *sbbp;
1401	struct buf *bp;
1402	int blks;
1403	void *space;
1404	int i, size, error, allerror = 0;
1405
1406	if (fs->fs_ronly == 1 &&
1407	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1408	    (MNT_RDONLY | MNT_UPDATE))
1409		panic("ffs_sbupdate: write read-only filesystem");
1410	/*
1411	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1412	 */
1413	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1414	    0, 0, 0);
1415	/*
1416	 * First write back the summary information.
1417	 */
1418	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1419	space = fs->fs_csp;
1420	for (i = 0; i < blks; i += fs->fs_frag) {
1421		size = fs->fs_bsize;
1422		if (i + fs->fs_frag > blks)
1423			size = (blks - i) * fs->fs_fsize;
1424		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1425		    size, 0, 0, 0);
1426		bcopy(space, bp->b_data, (u_int)size);
1427		space = (char *)space + size;
1428		if (waitfor != MNT_WAIT)
1429			bawrite(bp);
1430		else if ((error = bwrite(bp)) != 0)
1431			allerror = error;
1432	}
1433	/*
1434	 * Now write back the superblock itself. If any errors occurred
1435	 * up to this point, then fail so that the superblock avoids
1436	 * being written out as clean.
1437	 */
1438	if (allerror) {
1439		brelse(sbbp);
1440		return (allerror);
1441	}
1442	bp = sbbp;
1443	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1444	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1445		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1446		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1447		fs->fs_sblockloc = SBLOCK_UFS1;
1448	}
1449	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1450	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1451		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1452		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1453		fs->fs_sblockloc = SBLOCK_UFS2;
1454	}
1455	fs->fs_fmod = 0;
1456	fs->fs_time = time_second;
1457	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1458	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1459	if (waitfor != MNT_WAIT)
1460		bawrite(bp);
1461	else if ((error = bwrite(bp)) != 0)
1462		allerror = error;
1463	return (allerror);
1464}
1465
1466static int
1467ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1468	int attrnamespace, const char *attrname, struct thread *td)
1469{
1470
1471#ifdef UFS_EXTATTR
1472	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1473	    attrname, td));
1474#else
1475	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1476	    attrname, td));
1477#endif
1478}
1479
1480static void
1481ffs_ifree(struct ufsmount *ump, struct inode *ip)
1482{
1483
1484	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1485		uma_zfree(uma_ufs1, ip->i_din1);
1486	else if (ip->i_din2 != NULL)
1487		uma_zfree(uma_ufs2, ip->i_din2);
1488	uma_zfree(uma_inode, ip);
1489}
1490
1491static int dobkgrdwrite = 1;
1492SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1493    "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1494
1495/*
1496 * Complete a background write started from bwrite.
1497 */
1498static void
1499ffs_backgroundwritedone(struct buf *bp)
1500{
1501	struct buf *origbp;
1502
1503	/*
1504	 * Find the original buffer that we are writing.
1505	 */
1506	BO_LOCK(bp->b_bufobj);
1507	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1508		panic("backgroundwritedone: lost buffer");
1509	BO_UNLOCK(bp->b_bufobj);
1510	/*
1511	 * Process dependencies then return any unfinished ones.
1512	 */
1513	if (LIST_FIRST(&bp->b_dep) != NULL)
1514		buf_complete(bp);
1515#ifdef SOFTUPDATES
1516	if (LIST_FIRST(&bp->b_dep) != NULL)
1517		softdep_move_dependencies(bp, origbp);
1518#endif
1519
1520	/*
1521	 * This buffer is marked B_NOCACHE, so when it is released
1522	 * by biodone, it will be tossed. We mark it with BIO_READ
1523	 * to avoid biodone doing a second bufobj_wdrop.
1524	 */
1525	bp->b_flags |= B_NOCACHE;
1526	bp->b_iocmd = BIO_READ;
1527	bp->b_flags &= ~(B_CACHE | B_DONE);
1528	bp->b_iodone = 0;
1529	bufdone(bp);
1530	BO_LOCK(origbp->b_bufobj);
1531	/*
1532	 * Clear the BV_BKGRDINPROG flag in the original buffer
1533	 * and awaken it if it is waiting for the write to complete.
1534	 * If BV_BKGRDINPROG is not set in the original buffer it must
1535	 * have been released and re-instantiated - which is not legal.
1536	 */
1537	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1538	    ("backgroundwritedone: lost buffer2"));
1539	origbp->b_vflags &= ~BV_BKGRDINPROG;
1540	if (origbp->b_vflags & BV_BKGRDWAIT) {
1541		origbp->b_vflags &= ~BV_BKGRDWAIT;
1542		wakeup(&origbp->b_xflags);
1543	}
1544	BO_UNLOCK(origbp->b_bufobj);
1545}
1546
1547
1548/*
1549 * Write, release buffer on completion.  (Done by iodone
1550 * if async).  Do not bother writing anything if the buffer
1551 * is invalid.
1552 *
1553 * Note that we set B_CACHE here, indicating that buffer is
1554 * fully valid and thus cacheable.  This is true even of NFS
1555 * now so we set it generally.  This could be set either here
1556 * or in biodone() since the I/O is synchronous.  We put it
1557 * here.
1558 */
1559static int
1560ffs_bufwrite(struct buf *bp)
1561{
1562	int oldflags, s;
1563	struct buf *newbp;
1564
1565	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1566	if (bp->b_flags & B_INVAL) {
1567		brelse(bp);
1568		return (0);
1569	}
1570
1571	oldflags = bp->b_flags;
1572
1573	if (BUF_REFCNT(bp) == 0)
1574		panic("bufwrite: buffer is not busy???");
1575	s = splbio();
1576	/*
1577	 * If a background write is already in progress, delay
1578	 * writing this block if it is asynchronous. Otherwise
1579	 * wait for the background write to complete.
1580	 */
1581	BO_LOCK(bp->b_bufobj);
1582	if (bp->b_vflags & BV_BKGRDINPROG) {
1583		if (bp->b_flags & B_ASYNC) {
1584			BO_UNLOCK(bp->b_bufobj);
1585			splx(s);
1586			bdwrite(bp);
1587			return (0);
1588		}
1589		bp->b_vflags |= BV_BKGRDWAIT;
1590		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1591		if (bp->b_vflags & BV_BKGRDINPROG)
1592			panic("bufwrite: still writing");
1593	}
1594	BO_UNLOCK(bp->b_bufobj);
1595
1596	/* Mark the buffer clean */
1597	bundirty(bp);
1598
1599	/*
1600	 * If this buffer is marked for background writing and we
1601	 * do not have to wait for it, make a copy and write the
1602	 * copy so as to leave this buffer ready for further use.
1603	 *
1604	 * This optimization eats a lot of memory.  If we have a page
1605	 * or buffer shortfall we can't do it.
1606	 */
1607	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1608	    (bp->b_flags & B_ASYNC) &&
1609	    !vm_page_count_severe() &&
1610	    !buf_dirty_count_severe()) {
1611		KASSERT(bp->b_iodone == NULL,
1612		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1613
1614		/* get a new block */
1615		newbp = geteblk(bp->b_bufsize);
1616
1617		/*
1618		 * set it to be identical to the old block.  We have to
1619		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1620		 * to avoid confusing the splay tree and gbincore().
1621		 */
1622		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1623		newbp->b_lblkno = bp->b_lblkno;
1624		newbp->b_xflags |= BX_BKGRDMARKER;
1625		BO_LOCK(bp->b_bufobj);
1626		bp->b_vflags |= BV_BKGRDINPROG;
1627		bgetvp(bp->b_vp, newbp);
1628		BO_UNLOCK(bp->b_bufobj);
1629		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1630		newbp->b_blkno = bp->b_blkno;
1631		newbp->b_offset = bp->b_offset;
1632		newbp->b_iodone = ffs_backgroundwritedone;
1633		newbp->b_flags |= B_ASYNC;
1634		newbp->b_flags &= ~B_INVAL;
1635
1636#ifdef SOFTUPDATES
1637		/* move over the dependencies */
1638		if (LIST_FIRST(&bp->b_dep) != NULL)
1639			softdep_move_dependencies(bp, newbp);
1640#endif
1641
1642		/*
1643		 * Initiate write on the copy, release the original to
1644		 * the B_LOCKED queue so that it cannot go away until
1645		 * the background write completes. If not locked it could go
1646		 * away and then be reconstituted while it was being written.
1647		 * If the reconstituted buffer were written, we could end up
1648		 * with two background copies being written at the same time.
1649		 */
1650		bqrelse(bp);
1651		bp = newbp;
1652	}
1653
1654	/* Let the normal bufwrite do the rest for us */
1655	bufwrite(bp);
1656
1657	return (0);
1658}
1659
1660
1661static void
1662ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1663{
1664
1665#ifdef SOFTUPDATES
1666	if (bp->b_iocmd == BIO_WRITE && softdep_disk_prewrite(bp))
1667		return;
1668#endif
1669	g_vfs_strategy(bo, bp);
1670}
1671