ffs_vfsops.c revision 146802
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 146802 2005-05-30 07:04:15Z jeff $");
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 | LK_SLEEPFAIL;
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 || error == ENOLCK)
1112				goto loop;
1113			continue;
1114		}
1115		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1116			allerror = error;
1117		vput(vp);
1118		MNT_ILOCK(mp);
1119	}
1120	MNT_IUNLOCK(mp);
1121	/*
1122	 * Force stale filesystem control information to be flushed.
1123	 */
1124	if (waitfor == MNT_WAIT) {
1125		if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1126			allerror = error;
1127		/* Flushed work items may create new vnodes to clean */
1128		if (allerror == 0 && count) {
1129			MNT_ILOCK(mp);
1130			goto loop;
1131		}
1132	}
1133#ifdef QUOTA
1134	qsync(mp);
1135#endif
1136	devvp = ump->um_devvp;
1137	VI_LOCK(devvp);
1138	bo = &devvp->v_bufobj;
1139	if (waitfor != MNT_LAZY &&
1140	    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
1141		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
1142		if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1143			allerror = error;
1144		VOP_UNLOCK(devvp, 0, td);
1145		if (allerror == 0 && waitfor == MNT_WAIT) {
1146			MNT_ILOCK(mp);
1147			goto loop;
1148		}
1149	} else
1150		VI_UNLOCK(devvp);
1151	/*
1152	 * Write back modified superblock.
1153	 */
1154	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1155		allerror = error;
1156	return (allerror);
1157}
1158
1159int
1160ffs_vget(mp, ino, flags, vpp)
1161	struct mount *mp;
1162	ino_t ino;
1163	int flags;
1164	struct vnode **vpp;
1165{
1166	struct fs *fs;
1167	struct inode *ip;
1168	struct ufsmount *ump;
1169	struct buf *bp;
1170	struct vnode *vp;
1171	struct cdev *dev;
1172	int error;
1173
1174	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1175	if (error || *vpp != NULL)
1176		return (error);
1177
1178	/*
1179	 * We must promote to an exclusive lock for vnode creation.  This
1180	 * can happen if lookup is passed LOCKSHARED.
1181 	 */
1182	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1183		flags &= ~LK_TYPE_MASK;
1184		flags |= LK_EXCLUSIVE;
1185	}
1186
1187	/*
1188	 * We do not lock vnode creation as it is believed to be too
1189	 * expensive for such rare case as simultaneous creation of vnode
1190	 * for same ino by different processes. We just allow them to race
1191	 * and check later to decide who wins. Let the race begin!
1192	 */
1193
1194	ump = VFSTOUFS(mp);
1195	dev = ump->um_dev;
1196	fs = ump->um_fs;
1197
1198	/*
1199	 * If this MALLOC() is performed after the getnewvnode()
1200	 * it might block, leaving a vnode with a NULL v_data to be
1201	 * found by ffs_sync() if a sync happens to fire right then,
1202	 * which will cause a panic because ffs_sync() blindly
1203	 * dereferences vp->v_data (as well it should).
1204	 */
1205	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1206
1207	/* Allocate a new vnode/inode. */
1208	if (fs->fs_magic == FS_UFS1_MAGIC)
1209		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1210	else
1211		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1212	if (error) {
1213		*vpp = NULL;
1214		uma_zfree(uma_inode, ip);
1215		return (error);
1216	}
1217	/*
1218	 * FFS supports recursive and shared locking.
1219	 */
1220	vp->v_vnlock->lk_flags |= LK_CANRECURSE;
1221	vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
1222	vp->v_data = ip;
1223	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1224	ip->i_vnode = vp;
1225	ip->i_ump = ump;
1226	ip->i_fs = fs;
1227	ip->i_dev = dev;
1228	ip->i_number = ino;
1229#ifdef QUOTA
1230	{
1231		int i;
1232		for (i = 0; i < MAXQUOTAS; i++)
1233			ip->i_dquot[i] = NODQUOT;
1234	}
1235#endif
1236
1237	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1238	if (error || *vpp != NULL)
1239		return (error);
1240
1241	/* Read in the disk contents for the inode, copy into the inode. */
1242	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1243	    (int)fs->fs_bsize, NOCRED, &bp);
1244	if (error) {
1245		/*
1246		 * The inode does not contain anything useful, so it would
1247		 * be misleading to leave it on its hash chain. With mode
1248		 * still zero, it will be unlinked and returned to the free
1249		 * list by vput().
1250		 */
1251		brelse(bp);
1252		vput(vp);
1253		*vpp = NULL;
1254		return (error);
1255	}
1256	if (ip->i_ump->um_fstype == UFS1)
1257		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1258	else
1259		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1260	ffs_load_inode(bp, ip, fs, ino);
1261	if (DOINGSOFTDEP(vp))
1262		softdep_load_inodeblock(ip);
1263	else
1264		ip->i_effnlink = ip->i_nlink;
1265	bqrelse(bp);
1266
1267	/*
1268	 * Initialize the vnode from the inode, check for aliases.
1269	 * Note that the underlying vnode may have changed.
1270	 */
1271	if (ip->i_ump->um_fstype == UFS1)
1272		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1273	else
1274		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1275	if (error) {
1276		vput(vp);
1277		*vpp = NULL;
1278		return (error);
1279	}
1280
1281	/*
1282	 * Finish inode initialization.
1283	 */
1284
1285	/*
1286	 * Set up a generation number for this inode if it does not
1287	 * already have one. This should only happen on old filesystems.
1288	 */
1289	if (ip->i_gen == 0) {
1290		ip->i_gen = arc4random() / 2 + 1;
1291		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1292			ip->i_flag |= IN_MODIFIED;
1293			DIP_SET(ip, i_gen, ip->i_gen);
1294		}
1295	}
1296	/*
1297	 * Ensure that uid and gid are correct. This is a temporary
1298	 * fix until fsck has been changed to do the update.
1299	 */
1300	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
1301	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
1302		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
1303		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1304	}						/* XXX */
1305
1306#ifdef MAC
1307	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1308		/*
1309		 * If this vnode is already allocated, and we're running
1310		 * multi-label, attempt to perform a label association
1311		 * from the extended attributes on the inode.
1312		 */
1313		error = mac_associate_vnode_extattr(mp, vp);
1314		if (error) {
1315			/* ufs_inactive will release ip->i_devvp ref. */
1316			vput(vp);
1317			*vpp = NULL;
1318			return (error);
1319		}
1320	}
1321#endif
1322
1323	*vpp = vp;
1324	return (0);
1325}
1326
1327/*
1328 * File handle to vnode
1329 *
1330 * Have to be really careful about stale file handles:
1331 * - check that the inode number is valid
1332 * - call ffs_vget() to get the locked inode
1333 * - check for an unallocated inode (i_mode == 0)
1334 * - check that the given client host has export rights and return
1335 *   those rights via. exflagsp and credanonp
1336 */
1337static int
1338ffs_fhtovp(mp, fhp, vpp)
1339	struct mount *mp;
1340	struct fid *fhp;
1341	struct vnode **vpp;
1342{
1343	struct ufid *ufhp;
1344	struct fs *fs;
1345
1346	ufhp = (struct ufid *)fhp;
1347	fs = VFSTOUFS(mp)->um_fs;
1348	if (ufhp->ufid_ino < ROOTINO ||
1349	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1350		return (ESTALE);
1351	return (ufs_fhtovp(mp, ufhp, vpp));
1352}
1353
1354/*
1355 * Vnode pointer to File handle
1356 */
1357/* ARGSUSED */
1358static int
1359ffs_vptofh(vp, fhp)
1360	struct vnode *vp;
1361	struct fid *fhp;
1362{
1363	struct inode *ip;
1364	struct ufid *ufhp;
1365
1366	ip = VTOI(vp);
1367	ufhp = (struct ufid *)fhp;
1368	ufhp->ufid_len = sizeof(struct ufid);
1369	ufhp->ufid_ino = ip->i_number;
1370	ufhp->ufid_gen = ip->i_gen;
1371	return (0);
1372}
1373
1374/*
1375 * Initialize the filesystem.
1376 */
1377static int
1378ffs_init(vfsp)
1379	struct vfsconf *vfsp;
1380{
1381
1382	softdep_initialize();
1383	return (ufs_init(vfsp));
1384}
1385
1386/*
1387 * Undo the work of ffs_init().
1388 */
1389static int
1390ffs_uninit(vfsp)
1391	struct vfsconf *vfsp;
1392{
1393	int ret;
1394
1395	ret = ufs_uninit(vfsp);
1396	softdep_uninitialize();
1397	return (ret);
1398}
1399
1400/*
1401 * Write a superblock and associated information back to disk.
1402 */
1403static int
1404ffs_sbupdate(mp, waitfor)
1405	struct ufsmount *mp;
1406	int waitfor;
1407{
1408	struct fs *fs = mp->um_fs;
1409	struct buf *sbbp;
1410	struct buf *bp;
1411	int blks;
1412	void *space;
1413	int i, size, error, allerror = 0;
1414
1415	if (fs->fs_ronly == 1 &&
1416	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1417	    (MNT_RDONLY | MNT_UPDATE))
1418		panic("ffs_sbupdate: write read-only filesystem");
1419	/*
1420	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1421	 */
1422	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1423	    0, 0, 0);
1424	/*
1425	 * First write back the summary information.
1426	 */
1427	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1428	space = fs->fs_csp;
1429	for (i = 0; i < blks; i += fs->fs_frag) {
1430		size = fs->fs_bsize;
1431		if (i + fs->fs_frag > blks)
1432			size = (blks - i) * fs->fs_fsize;
1433		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1434		    size, 0, 0, 0);
1435		bcopy(space, bp->b_data, (u_int)size);
1436		space = (char *)space + size;
1437		if (waitfor != MNT_WAIT)
1438			bawrite(bp);
1439		else if ((error = bwrite(bp)) != 0)
1440			allerror = error;
1441	}
1442	/*
1443	 * Now write back the superblock itself. If any errors occurred
1444	 * up to this point, then fail so that the superblock avoids
1445	 * being written out as clean.
1446	 */
1447	if (allerror) {
1448		brelse(sbbp);
1449		return (allerror);
1450	}
1451	bp = sbbp;
1452	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1453	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1454		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1455		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1456		fs->fs_sblockloc = SBLOCK_UFS1;
1457	}
1458	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1459	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1460		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1461		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1462		fs->fs_sblockloc = SBLOCK_UFS2;
1463	}
1464	fs->fs_fmod = 0;
1465	fs->fs_time = time_second;
1466	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1467	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1468	if (waitfor != MNT_WAIT)
1469		bawrite(bp);
1470	else if ((error = bwrite(bp)) != 0)
1471		allerror = error;
1472	return (allerror);
1473}
1474
1475static int
1476ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1477	int attrnamespace, const char *attrname, struct thread *td)
1478{
1479
1480#ifdef UFS_EXTATTR
1481	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1482	    attrname, td));
1483#else
1484	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1485	    attrname, td));
1486#endif
1487}
1488
1489static void
1490ffs_ifree(struct ufsmount *ump, struct inode *ip)
1491{
1492
1493	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1494		uma_zfree(uma_ufs1, ip->i_din1);
1495	else if (ip->i_din2 != NULL)
1496		uma_zfree(uma_ufs2, ip->i_din2);
1497	uma_zfree(uma_inode, ip);
1498}
1499
1500static int dobkgrdwrite = 1;
1501SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1502    "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1503
1504/*
1505 * Complete a background write started from bwrite.
1506 */
1507static void
1508ffs_backgroundwritedone(struct buf *bp)
1509{
1510	struct bufobj *bufobj;
1511	struct buf *origbp;
1512
1513	/*
1514	 * Find the original buffer that we are writing.
1515	 */
1516	bufobj = bp->b_bufobj;
1517	BO_LOCK(bufobj);
1518	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1519		panic("backgroundwritedone: lost buffer");
1520	/* Grab an extra reference to be dropped by the bufdone() below. */
1521	bufobj_wrefl(bufobj);
1522	BO_UNLOCK(bufobj);
1523	/*
1524	 * Process dependencies then return any unfinished ones.
1525	 */
1526	if (LIST_FIRST(&bp->b_dep) != NULL)
1527		buf_complete(bp);
1528#ifdef SOFTUPDATES
1529	if (LIST_FIRST(&bp->b_dep) != NULL)
1530		softdep_move_dependencies(bp, origbp);
1531#endif
1532	/*
1533	 * This buffer is marked B_NOCACHE so when it is released
1534	 * by biodone it will be tossed.
1535	 */
1536	bp->b_flags |= B_NOCACHE;
1537	bp->b_flags &= ~(B_CACHE | B_DONE);
1538	bufdone(bp);
1539	BO_LOCK(bufobj);
1540	/*
1541	 * Clear the BV_BKGRDINPROG flag in the original buffer
1542	 * and awaken it if it is waiting for the write to complete.
1543	 * If BV_BKGRDINPROG is not set in the original buffer it must
1544	 * have been released and re-instantiated - which is not legal.
1545	 */
1546	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1547	    ("backgroundwritedone: lost buffer2"));
1548	origbp->b_vflags &= ~BV_BKGRDINPROG;
1549	if (origbp->b_vflags & BV_BKGRDWAIT) {
1550		origbp->b_vflags &= ~BV_BKGRDWAIT;
1551		wakeup(&origbp->b_xflags);
1552	}
1553	BO_UNLOCK(bufobj);
1554}
1555
1556
1557/*
1558 * Write, release buffer on completion.  (Done by iodone
1559 * if async).  Do not bother writing anything if the buffer
1560 * is invalid.
1561 *
1562 * Note that we set B_CACHE here, indicating that buffer is
1563 * fully valid and thus cacheable.  This is true even of NFS
1564 * now so we set it generally.  This could be set either here
1565 * or in biodone() since the I/O is synchronous.  We put it
1566 * here.
1567 */
1568static int
1569ffs_bufwrite(struct buf *bp)
1570{
1571	int oldflags, s;
1572	struct buf *newbp;
1573
1574	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1575	if (bp->b_flags & B_INVAL) {
1576		brelse(bp);
1577		return (0);
1578	}
1579
1580	oldflags = bp->b_flags;
1581
1582	if (BUF_REFCNT(bp) == 0)
1583		panic("bufwrite: buffer is not busy???");
1584	s = splbio();
1585	/*
1586	 * If a background write is already in progress, delay
1587	 * writing this block if it is asynchronous. Otherwise
1588	 * wait for the background write to complete.
1589	 */
1590	BO_LOCK(bp->b_bufobj);
1591	if (bp->b_vflags & BV_BKGRDINPROG) {
1592		if (bp->b_flags & B_ASYNC) {
1593			BO_UNLOCK(bp->b_bufobj);
1594			splx(s);
1595			bdwrite(bp);
1596			return (0);
1597		}
1598		bp->b_vflags |= BV_BKGRDWAIT;
1599		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1600		if (bp->b_vflags & BV_BKGRDINPROG)
1601			panic("bufwrite: still writing");
1602	}
1603	BO_UNLOCK(bp->b_bufobj);
1604
1605	/* Mark the buffer clean */
1606	bundirty(bp);
1607
1608	/*
1609	 * If this buffer is marked for background writing and we
1610	 * do not have to wait for it, make a copy and write the
1611	 * copy so as to leave this buffer ready for further use.
1612	 *
1613	 * This optimization eats a lot of memory.  If we have a page
1614	 * or buffer shortfall we can't do it.
1615	 */
1616	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1617	    (bp->b_flags & B_ASYNC) &&
1618	    !vm_page_count_severe() &&
1619	    !buf_dirty_count_severe()) {
1620		KASSERT(bp->b_iodone == NULL,
1621		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1622
1623		/* get a new block */
1624		newbp = geteblk(bp->b_bufsize);
1625
1626		/*
1627		 * set it to be identical to the old block.  We have to
1628		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1629		 * to avoid confusing the splay tree and gbincore().
1630		 */
1631		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1632		newbp->b_lblkno = bp->b_lblkno;
1633		newbp->b_xflags |= BX_BKGRDMARKER;
1634		BO_LOCK(bp->b_bufobj);
1635		bp->b_vflags |= BV_BKGRDINPROG;
1636		bgetvp(bp->b_vp, newbp);
1637		BO_UNLOCK(bp->b_bufobj);
1638		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1639		newbp->b_blkno = bp->b_blkno;
1640		newbp->b_offset = bp->b_offset;
1641		newbp->b_iodone = ffs_backgroundwritedone;
1642		newbp->b_flags |= B_ASYNC;
1643		newbp->b_flags &= ~B_INVAL;
1644
1645#ifdef SOFTUPDATES
1646		/* move over the dependencies */
1647		if (LIST_FIRST(&bp->b_dep) != NULL)
1648			softdep_move_dependencies(bp, newbp);
1649#endif
1650
1651		/*
1652		 * Initiate write on the copy, release the original to
1653		 * the B_LOCKED queue so that it cannot go away until
1654		 * the background write completes. If not locked it could go
1655		 * away and then be reconstituted while it was being written.
1656		 * If the reconstituted buffer were written, we could end up
1657		 * with two background copies being written at the same time.
1658		 */
1659		bqrelse(bp);
1660		bp = newbp;
1661	}
1662
1663	/* Let the normal bufwrite do the rest for us */
1664	bufwrite(bp);
1665
1666	return (0);
1667}
1668
1669
1670static void
1671ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1672{
1673	struct vnode *vp;
1674	int error;
1675
1676	vp = bo->__bo_vnode;
1677	if (bp->b_iocmd == BIO_WRITE) {
1678#ifdef SOFTUPDATES
1679		if (LIST_FIRST(&bp->b_dep) != NULL)
1680			buf_start(bp);
1681#endif
1682		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1683		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1684		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1685			panic("ffs_geom_strategy: bad I/O");
1686		bp->b_flags &= ~B_VALIDSUSPWRT;
1687		if ((vp->v_vflag & VV_COPYONWRITE) &&
1688		    vp->v_rdev->si_snapdata != NULL &&
1689		    (error = (ffs_copyonwrite)(vp, bp)) != 0 &&
1690		    error != EOPNOTSUPP) {
1691			bp->b_error = error;
1692			bp->b_ioflags |= BIO_ERROR;
1693			bufdone(bp);
1694			return;
1695		}
1696	}
1697	g_vfs_strategy(bo, bp);
1698}
1699