ffs_vfsops.c revision 149358
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 149358 2005-08-21 22:06:41Z ssouhlal $");
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	bzero(fs->fs_fsmnt, MAXMNTLEN);
743	strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
744
745	if( mp->mnt_flag & MNT_ROOTFS) {
746		/*
747		 * Root mount; update timestamp in mount structure.
748		 * this will be used by the common root mount code
749		 * to update the system clock.
750		 */
751		mp->mnt_time = fs->fs_time;
752	}
753
754	if (ronly == 0) {
755		if ((fs->fs_flags & FS_DOSOFTDEP) &&
756		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
757			free(fs->fs_csp, M_UFSMNT);
758			goto out;
759		}
760		if (fs->fs_snapinum[0] != 0)
761			ffs_snapshot_mount(mp);
762		fs->fs_fmod = 1;
763		fs->fs_clean = 0;
764		(void) ffs_sbupdate(ump, MNT_WAIT);
765	}
766	/*
767	 * Initialize filesystem stat information in mount struct.
768	 */
769#ifdef UFS_EXTATTR
770#ifdef UFS_EXTATTR_AUTOSTART
771	/*
772	 *
773	 * Auto-starting does the following:
774	 *	- check for /.attribute in the fs, and extattr_start if so
775	 *	- for each file in .attribute, enable that file with
776	 * 	  an attribute of the same name.
777	 * Not clear how to report errors -- probably eat them.
778	 * This would all happen while the filesystem was busy/not
779	 * available, so would effectively be "atomic".
780	 */
781	(void) ufs_extattr_autostart(mp, td);
782#endif /* !UFS_EXTATTR_AUTOSTART */
783#endif /* !UFS_EXTATTR */
784#ifndef QUOTA
785	mp->mnt_kern_flag |= MNTK_MPSAFE;
786#endif
787	return (0);
788out:
789	if (bp)
790		brelse(bp);
791	if (cp != NULL) {
792		DROP_GIANT();
793		g_topology_lock();
794		g_vfs_close(cp, td);
795		g_topology_unlock();
796		PICKUP_GIANT();
797	}
798	if (ump) {
799		mtx_destroy(UFS_MTX(ump));
800		free(ump->um_fs, M_UFSMNT);
801		free(ump, M_UFSMNT);
802		mp->mnt_data = (qaddr_t)0;
803	}
804	return (error);
805}
806
807#include <sys/sysctl.h>
808static int bigcgs = 0;
809SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
810
811/*
812 * Sanity checks for loading old filesystem superblocks.
813 * See ffs_oldfscompat_write below for unwound actions.
814 *
815 * XXX - Parts get retired eventually.
816 * Unfortunately new bits get added.
817 */
818static void
819ffs_oldfscompat_read(fs, ump, sblockloc)
820	struct fs *fs;
821	struct ufsmount *ump;
822	ufs2_daddr_t sblockloc;
823{
824	off_t maxfilesize;
825
826	/*
827	 * If not yet done, update fs_flags location and value of fs_sblockloc.
828	 */
829	if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
830		fs->fs_flags = fs->fs_old_flags;
831		fs->fs_old_flags |= FS_FLAGS_UPDATED;
832		fs->fs_sblockloc = sblockloc;
833	}
834	/*
835	 * If not yet done, update UFS1 superblock with new wider fields.
836	 */
837	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
838		fs->fs_maxbsize = fs->fs_bsize;
839		fs->fs_time = fs->fs_old_time;
840		fs->fs_size = fs->fs_old_size;
841		fs->fs_dsize = fs->fs_old_dsize;
842		fs->fs_csaddr = fs->fs_old_csaddr;
843		fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
844		fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
845		fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
846		fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
847	}
848	if (fs->fs_magic == FS_UFS1_MAGIC &&
849	    fs->fs_old_inodefmt < FS_44INODEFMT) {
850		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
851		fs->fs_qbmask = ~fs->fs_bmask;
852		fs->fs_qfmask = ~fs->fs_fmask;
853	}
854	if (fs->fs_magic == FS_UFS1_MAGIC) {
855		ump->um_savedmaxfilesize = fs->fs_maxfilesize;
856		maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;
857		if (fs->fs_maxfilesize > maxfilesize)
858			fs->fs_maxfilesize = maxfilesize;
859	}
860	/* Compatibility for old filesystems */
861	if (fs->fs_avgfilesize <= 0)
862		fs->fs_avgfilesize = AVFILESIZ;
863	if (fs->fs_avgfpdir <= 0)
864		fs->fs_avgfpdir = AFPDIR;
865	if (bigcgs) {
866		fs->fs_save_cgsize = fs->fs_cgsize;
867		fs->fs_cgsize = fs->fs_bsize;
868	}
869}
870
871/*
872 * Unwinding superblock updates for old filesystems.
873 * See ffs_oldfscompat_read above for details.
874 *
875 * XXX - Parts get retired eventually.
876 * Unfortunately new bits get added.
877 */
878static void
879ffs_oldfscompat_write(fs, ump)
880	struct fs *fs;
881	struct ufsmount *ump;
882{
883
884	/*
885	 * Copy back UFS2 updated fields that UFS1 inspects.
886	 */
887	if (fs->fs_magic == FS_UFS1_MAGIC) {
888		fs->fs_old_time = fs->fs_time;
889		fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
890		fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
891		fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
892		fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
893		fs->fs_maxfilesize = ump->um_savedmaxfilesize;
894	}
895	if (bigcgs) {
896		fs->fs_cgsize = fs->fs_save_cgsize;
897		fs->fs_save_cgsize = 0;
898	}
899}
900
901/*
902 * unmount system call
903 */
904static int
905ffs_unmount(mp, mntflags, td)
906	struct mount *mp;
907	int mntflags;
908	struct thread *td;
909{
910	struct ufsmount *ump = VFSTOUFS(mp);
911	struct fs *fs;
912	int error, flags;
913
914	flags = 0;
915	if (mntflags & MNT_FORCE) {
916		flags |= FORCECLOSE;
917	}
918#ifdef UFS_EXTATTR
919	if ((error = ufs_extattr_stop(mp, td))) {
920		if (error != EOPNOTSUPP)
921			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
922			    error);
923	} else {
924		ufs_extattr_uepm_destroy(&ump->um_extattr);
925	}
926#endif
927	if (mp->mnt_flag & MNT_SOFTDEP) {
928		if ((error = softdep_flushfiles(mp, flags, td)) != 0)
929			return (error);
930	} else {
931		if ((error = ffs_flushfiles(mp, flags, td)) != 0)
932			return (error);
933	}
934	fs = ump->um_fs;
935	UFS_LOCK(ump);
936	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
937		printf("%s: unmount pending error: blocks %jd files %d\n",
938		    fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
939		    fs->fs_pendinginodes);
940		fs->fs_pendingblocks = 0;
941		fs->fs_pendinginodes = 0;
942	}
943	UFS_UNLOCK(ump);
944	if (fs->fs_ronly == 0) {
945		fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
946		error = ffs_sbupdate(ump, MNT_WAIT);
947		if (error) {
948			fs->fs_clean = 0;
949			return (error);
950		}
951	}
952	DROP_GIANT();
953	g_topology_lock();
954	g_vfs_close(ump->um_cp, td);
955	g_topology_unlock();
956	PICKUP_GIANT();
957	vrele(ump->um_devvp);
958	mtx_destroy(UFS_MTX(ump));
959	free(fs->fs_csp, M_UFSMNT);
960	free(fs, M_UFSMNT);
961	free(ump, M_UFSMNT);
962	mp->mnt_data = (qaddr_t)0;
963	mp->mnt_flag &= ~MNT_LOCAL;
964	return (error);
965}
966
967/*
968 * Flush out all the files in a filesystem.
969 */
970int
971ffs_flushfiles(mp, flags, td)
972	struct mount *mp;
973	int flags;
974	struct thread *td;
975{
976	struct ufsmount *ump;
977	int error;
978
979	ump = VFSTOUFS(mp);
980#ifdef QUOTA
981	if (mp->mnt_flag & MNT_QUOTA) {
982		int i;
983		error = vflush(mp, 0, SKIPSYSTEM|flags, td);
984		if (error)
985			return (error);
986		for (i = 0; i < MAXQUOTAS; i++) {
987			if (ump->um_quotas[i] == NULLVP)
988				continue;
989			quotaoff(td, mp, i);
990		}
991		/*
992		 * Here we fall through to vflush again to ensure
993		 * that we have gotten rid of all the system vnodes.
994		 */
995	}
996#endif
997	ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
998	if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
999		if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1000			return (error);
1001		ffs_snapshot_unmount(mp);
1002		/*
1003		 * Here we fall through to vflush again to ensure
1004		 * that we have gotten rid of all the system vnodes.
1005		 */
1006	}
1007        /*
1008	 * Flush all the files.
1009	 */
1010	if ((error = vflush(mp, 0, flags, td)) != 0)
1011		return (error);
1012	/*
1013	 * Flush filesystem metadata.
1014	 */
1015	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
1016	error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1017	VOP_UNLOCK(ump->um_devvp, 0, td);
1018	return (error);
1019}
1020
1021/*
1022 * Get filesystem statistics.
1023 */
1024static int
1025ffs_statfs(mp, sbp, td)
1026	struct mount *mp;
1027	struct statfs *sbp;
1028	struct thread *td;
1029{
1030	struct ufsmount *ump;
1031	struct fs *fs;
1032
1033	ump = VFSTOUFS(mp);
1034	fs = ump->um_fs;
1035	if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1036		panic("ffs_statfs");
1037	sbp->f_version = STATFS_VERSION;
1038	sbp->f_bsize = fs->fs_fsize;
1039	sbp->f_iosize = fs->fs_bsize;
1040	sbp->f_blocks = fs->fs_dsize;
1041	UFS_LOCK(ump);
1042	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1043	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1044	sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1045	    dbtofsb(fs, fs->fs_pendingblocks);
1046	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1047	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1048	UFS_UNLOCK(ump);
1049	sbp->f_namemax = NAME_MAX;
1050	return (0);
1051}
1052
1053/*
1054 * Go through the disk queues to initiate sandbagged IO;
1055 * go through the inodes to write those that have been modified;
1056 * initiate the writing of the super block if it has been modified.
1057 *
1058 * Note: we are always called with the filesystem marked `MPBUSY'.
1059 */
1060static int
1061ffs_sync(mp, waitfor, td)
1062	struct mount *mp;
1063	int waitfor;
1064	struct thread *td;
1065{
1066	struct vnode *nvp, *vp, *devvp;
1067	struct inode *ip;
1068	struct ufsmount *ump = VFSTOUFS(mp);
1069	struct fs *fs;
1070	int error, count, wait, lockreq, allerror = 0;
1071	struct bufobj *bo;
1072
1073	fs = ump->um_fs;
1074	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
1075		printf("fs = %s\n", fs->fs_fsmnt);
1076		panic("ffs_sync: rofs mod");
1077	}
1078	/*
1079	 * Write back each (modified) inode.
1080	 */
1081	wait = 0;
1082	lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1083	if (waitfor == MNT_WAIT) {
1084		wait = 1;
1085		lockreq = LK_EXCLUSIVE;
1086	}
1087	lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1088	MNT_ILOCK(mp);
1089loop:
1090	MNT_VNODE_FOREACH(vp, mp, nvp) {
1091		/*
1092		 * Depend on the mntvnode_slock to keep things stable enough
1093		 * for a quick test.  Since there might be hundreds of
1094		 * thousands of vnodes, we cannot afford even a subroutine
1095		 * call unless there's a good chance that we have work to do.
1096		 */
1097		VI_LOCK(vp);
1098		if (vp->v_iflag & VI_DOOMED) {
1099			VI_UNLOCK(vp);
1100			continue;
1101		}
1102		ip = VTOI(vp);
1103		if (vp->v_type == VNON || ((ip->i_flag &
1104		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1105		    vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1106			VI_UNLOCK(vp);
1107			continue;
1108		}
1109		MNT_IUNLOCK(mp);
1110		if ((error = vget(vp, lockreq, td)) != 0) {
1111			MNT_ILOCK(mp);
1112			if (error == ENOENT || error == ENOLCK)
1113				goto loop;
1114			continue;
1115		}
1116		if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1117			allerror = error;
1118		vput(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, NULL, NULL);
1176	if (error || *vpp != NULL)
1177		return (error);
1178
1179	/*
1180	 * We must promote to an exclusive lock for vnode creation.  This
1181	 * can happen if lookup is passed LOCKSHARED.
1182 	 */
1183	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1184		flags &= ~LK_TYPE_MASK;
1185		flags |= LK_EXCLUSIVE;
1186	}
1187
1188	/*
1189	 * We do not lock vnode creation as it is believed to be too
1190	 * expensive for such rare case as simultaneous creation of vnode
1191	 * for same ino by different processes. We just allow them to race
1192	 * and check later to decide who wins. Let the race begin!
1193	 */
1194
1195	ump = VFSTOUFS(mp);
1196	dev = ump->um_dev;
1197	fs = ump->um_fs;
1198
1199	/*
1200	 * If this MALLOC() is performed after the getnewvnode()
1201	 * it might block, leaving a vnode with a NULL v_data to be
1202	 * found by ffs_sync() if a sync happens to fire right then,
1203	 * which will cause a panic because ffs_sync() blindly
1204	 * dereferences vp->v_data (as well it should).
1205	 */
1206	ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1207
1208	/* Allocate a new vnode/inode. */
1209	if (fs->fs_magic == FS_UFS1_MAGIC)
1210		error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1211	else
1212		error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1213	if (error) {
1214		*vpp = NULL;
1215		uma_zfree(uma_inode, ip);
1216		return (error);
1217	}
1218	/*
1219	 * FFS supports recursive and shared locking.
1220	 */
1221	vp->v_vnlock->lk_flags |= LK_CANRECURSE;
1222	vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
1223	vp->v_data = ip;
1224	vp->v_bufobj.bo_bsize = fs->fs_bsize;
1225	ip->i_vnode = vp;
1226	ip->i_ump = ump;
1227	ip->i_fs = fs;
1228	ip->i_dev = dev;
1229	ip->i_number = ino;
1230#ifdef QUOTA
1231	{
1232		int i;
1233		for (i = 0; i < MAXQUOTAS; i++)
1234			ip->i_dquot[i] = NODQUOT;
1235	}
1236#endif
1237
1238	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1239	if (error || *vpp != NULL)
1240		return (error);
1241
1242	/* Read in the disk contents for the inode, copy into the inode. */
1243	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1244	    (int)fs->fs_bsize, NOCRED, &bp);
1245	if (error) {
1246		/*
1247		 * The inode does not contain anything useful, so it would
1248		 * be misleading to leave it on its hash chain. With mode
1249		 * still zero, it will be unlinked and returned to the free
1250		 * list by vput().
1251		 */
1252		brelse(bp);
1253		vput(vp);
1254		*vpp = NULL;
1255		return (error);
1256	}
1257	if (ip->i_ump->um_fstype == UFS1)
1258		ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1259	else
1260		ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1261	ffs_load_inode(bp, ip, fs, ino);
1262	if (DOINGSOFTDEP(vp))
1263		softdep_load_inodeblock(ip);
1264	else
1265		ip->i_effnlink = ip->i_nlink;
1266	bqrelse(bp);
1267
1268	/*
1269	 * Initialize the vnode from the inode, check for aliases.
1270	 * Note that the underlying vnode may have changed.
1271	 */
1272	if (ip->i_ump->um_fstype == UFS1)
1273		error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1274	else
1275		error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1276	if (error) {
1277		vput(vp);
1278		*vpp = NULL;
1279		return (error);
1280	}
1281
1282	/*
1283	 * Finish inode initialization.
1284	 */
1285
1286	/*
1287	 * Set up a generation number for this inode if it does not
1288	 * already have one. This should only happen on old filesystems.
1289	 */
1290	if (ip->i_gen == 0) {
1291		ip->i_gen = arc4random() / 2 + 1;
1292		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1293			ip->i_flag |= IN_MODIFIED;
1294			DIP_SET(ip, i_gen, ip->i_gen);
1295		}
1296	}
1297	/*
1298	 * Ensure that uid and gid are correct. This is a temporary
1299	 * fix until fsck has been changed to do the update.
1300	 */
1301	if (fs->fs_magic == FS_UFS1_MAGIC &&		/* XXX */
1302	    fs->fs_old_inodefmt < FS_44INODEFMT) {	/* XXX */
1303		ip->i_uid = ip->i_din1->di_ouid;	/* XXX */
1304		ip->i_gid = ip->i_din1->di_ogid;	/* XXX */
1305	}						/* XXX */
1306
1307#ifdef MAC
1308	if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1309		/*
1310		 * If this vnode is already allocated, and we're running
1311		 * multi-label, attempt to perform a label association
1312		 * from the extended attributes on the inode.
1313		 */
1314		error = mac_associate_vnode_extattr(mp, vp);
1315		if (error) {
1316			/* ufs_inactive will release ip->i_devvp ref. */
1317			vput(vp);
1318			*vpp = NULL;
1319			return (error);
1320		}
1321	}
1322#endif
1323
1324	*vpp = vp;
1325	return (0);
1326}
1327
1328/*
1329 * File handle to vnode
1330 *
1331 * Have to be really careful about stale file handles:
1332 * - check that the inode number is valid
1333 * - call ffs_vget() to get the locked inode
1334 * - check for an unallocated inode (i_mode == 0)
1335 * - check that the given client host has export rights and return
1336 *   those rights via. exflagsp and credanonp
1337 */
1338static int
1339ffs_fhtovp(mp, fhp, vpp)
1340	struct mount *mp;
1341	struct fid *fhp;
1342	struct vnode **vpp;
1343{
1344	struct ufid *ufhp;
1345	struct fs *fs;
1346
1347	ufhp = (struct ufid *)fhp;
1348	fs = VFSTOUFS(mp)->um_fs;
1349	if (ufhp->ufid_ino < ROOTINO ||
1350	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1351		return (ESTALE);
1352	return (ufs_fhtovp(mp, ufhp, vpp));
1353}
1354
1355/*
1356 * Vnode pointer to File handle
1357 */
1358/* ARGSUSED */
1359static int
1360ffs_vptofh(vp, fhp)
1361	struct vnode *vp;
1362	struct fid *fhp;
1363{
1364	struct inode *ip;
1365	struct ufid *ufhp;
1366
1367	ip = VTOI(vp);
1368	ufhp = (struct ufid *)fhp;
1369	ufhp->ufid_len = sizeof(struct ufid);
1370	ufhp->ufid_ino = ip->i_number;
1371	ufhp->ufid_gen = ip->i_gen;
1372	return (0);
1373}
1374
1375/*
1376 * Initialize the filesystem.
1377 */
1378static int
1379ffs_init(vfsp)
1380	struct vfsconf *vfsp;
1381{
1382
1383	softdep_initialize();
1384	return (ufs_init(vfsp));
1385}
1386
1387/*
1388 * Undo the work of ffs_init().
1389 */
1390static int
1391ffs_uninit(vfsp)
1392	struct vfsconf *vfsp;
1393{
1394	int ret;
1395
1396	ret = ufs_uninit(vfsp);
1397	softdep_uninitialize();
1398	return (ret);
1399}
1400
1401/*
1402 * Write a superblock and associated information back to disk.
1403 */
1404static int
1405ffs_sbupdate(mp, waitfor)
1406	struct ufsmount *mp;
1407	int waitfor;
1408{
1409	struct fs *fs = mp->um_fs;
1410	struct buf *sbbp;
1411	struct buf *bp;
1412	int blks;
1413	void *space;
1414	int i, size, error, allerror = 0;
1415
1416	if (fs->fs_ronly == 1 &&
1417	    (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) !=
1418	    (MNT_RDONLY | MNT_UPDATE))
1419		panic("ffs_sbupdate: write read-only filesystem");
1420	/*
1421	 * We use the superblock's buf to serialize calls to ffs_sbupdate().
1422	 */
1423	sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1424	    0, 0, 0);
1425	/*
1426	 * First write back the summary information.
1427	 */
1428	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1429	space = fs->fs_csp;
1430	for (i = 0; i < blks; i += fs->fs_frag) {
1431		size = fs->fs_bsize;
1432		if (i + fs->fs_frag > blks)
1433			size = (blks - i) * fs->fs_fsize;
1434		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1435		    size, 0, 0, 0);
1436		bcopy(space, bp->b_data, (u_int)size);
1437		space = (char *)space + size;
1438		if (waitfor != MNT_WAIT)
1439			bawrite(bp);
1440		else if ((error = bwrite(bp)) != 0)
1441			allerror = error;
1442	}
1443	/*
1444	 * Now write back the superblock itself. If any errors occurred
1445	 * up to this point, then fail so that the superblock avoids
1446	 * being written out as clean.
1447	 */
1448	if (allerror) {
1449		brelse(sbbp);
1450		return (allerror);
1451	}
1452	bp = sbbp;
1453	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1454	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1455		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1456		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1457		fs->fs_sblockloc = SBLOCK_UFS1;
1458	}
1459	if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1460	    (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1461		printf("%s: correcting fs_sblockloc from %jd to %d\n",
1462		    fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1463		fs->fs_sblockloc = SBLOCK_UFS2;
1464	}
1465	fs->fs_fmod = 0;
1466	fs->fs_time = time_second;
1467	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1468	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1469	if (waitfor != MNT_WAIT)
1470		bawrite(bp);
1471	else if ((error = bwrite(bp)) != 0)
1472		allerror = error;
1473	return (allerror);
1474}
1475
1476static int
1477ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1478	int attrnamespace, const char *attrname, struct thread *td)
1479{
1480
1481#ifdef UFS_EXTATTR
1482	return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1483	    attrname, td));
1484#else
1485	return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1486	    attrname, td));
1487#endif
1488}
1489
1490static void
1491ffs_ifree(struct ufsmount *ump, struct inode *ip)
1492{
1493
1494	if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1495		uma_zfree(uma_ufs1, ip->i_din1);
1496	else if (ip->i_din2 != NULL)
1497		uma_zfree(uma_ufs2, ip->i_din2);
1498	uma_zfree(uma_inode, ip);
1499}
1500
1501static int dobkgrdwrite = 1;
1502SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1503    "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1504
1505/*
1506 * Complete a background write started from bwrite.
1507 */
1508static void
1509ffs_backgroundwritedone(struct buf *bp)
1510{
1511	struct bufobj *bufobj;
1512	struct buf *origbp;
1513
1514	/*
1515	 * Find the original buffer that we are writing.
1516	 */
1517	bufobj = bp->b_bufobj;
1518	BO_LOCK(bufobj);
1519	if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1520		panic("backgroundwritedone: lost buffer");
1521	/* Grab an extra reference to be dropped by the bufdone() below. */
1522	bufobj_wrefl(bufobj);
1523	BO_UNLOCK(bufobj);
1524	/*
1525	 * Process dependencies then return any unfinished ones.
1526	 */
1527	if (LIST_FIRST(&bp->b_dep) != NULL)
1528		buf_complete(bp);
1529#ifdef SOFTUPDATES
1530	if (LIST_FIRST(&bp->b_dep) != NULL)
1531		softdep_move_dependencies(bp, origbp);
1532#endif
1533	/*
1534	 * This buffer is marked B_NOCACHE so when it is released
1535	 * by biodone it will be tossed.
1536	 */
1537	bp->b_flags |= B_NOCACHE;
1538	bp->b_flags &= ~B_CACHE;
1539	bufdone(bp);
1540	BO_LOCK(bufobj);
1541	/*
1542	 * Clear the BV_BKGRDINPROG flag in the original buffer
1543	 * and awaken it if it is waiting for the write to complete.
1544	 * If BV_BKGRDINPROG is not set in the original buffer it must
1545	 * have been released and re-instantiated - which is not legal.
1546	 */
1547	KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1548	    ("backgroundwritedone: lost buffer2"));
1549	origbp->b_vflags &= ~BV_BKGRDINPROG;
1550	if (origbp->b_vflags & BV_BKGRDWAIT) {
1551		origbp->b_vflags &= ~BV_BKGRDWAIT;
1552		wakeup(&origbp->b_xflags);
1553	}
1554	BO_UNLOCK(bufobj);
1555}
1556
1557
1558/*
1559 * Write, release buffer on completion.  (Done by iodone
1560 * if async).  Do not bother writing anything if the buffer
1561 * is invalid.
1562 *
1563 * Note that we set B_CACHE here, indicating that buffer is
1564 * fully valid and thus cacheable.  This is true even of NFS
1565 * now so we set it generally.  This could be set either here
1566 * or in biodone() since the I/O is synchronous.  We put it
1567 * here.
1568 */
1569static int
1570ffs_bufwrite(struct buf *bp)
1571{
1572	int oldflags, s;
1573	struct buf *newbp;
1574
1575	CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1576	if (bp->b_flags & B_INVAL) {
1577		brelse(bp);
1578		return (0);
1579	}
1580
1581	oldflags = bp->b_flags;
1582
1583	if (BUF_REFCNT(bp) == 0)
1584		panic("bufwrite: buffer is not busy???");
1585	s = splbio();
1586	/*
1587	 * If a background write is already in progress, delay
1588	 * writing this block if it is asynchronous. Otherwise
1589	 * wait for the background write to complete.
1590	 */
1591	BO_LOCK(bp->b_bufobj);
1592	if (bp->b_vflags & BV_BKGRDINPROG) {
1593		if (bp->b_flags & B_ASYNC) {
1594			BO_UNLOCK(bp->b_bufobj);
1595			splx(s);
1596			bdwrite(bp);
1597			return (0);
1598		}
1599		bp->b_vflags |= BV_BKGRDWAIT;
1600		msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1601		if (bp->b_vflags & BV_BKGRDINPROG)
1602			panic("bufwrite: still writing");
1603	}
1604	BO_UNLOCK(bp->b_bufobj);
1605
1606	/* Mark the buffer clean */
1607	bundirty(bp);
1608
1609	/*
1610	 * If this buffer is marked for background writing and we
1611	 * do not have to wait for it, make a copy and write the
1612	 * copy so as to leave this buffer ready for further use.
1613	 *
1614	 * This optimization eats a lot of memory.  If we have a page
1615	 * or buffer shortfall we can't do it.
1616	 */
1617	if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) &&
1618	    (bp->b_flags & B_ASYNC) &&
1619	    !vm_page_count_severe() &&
1620	    !buf_dirty_count_severe()) {
1621		KASSERT(bp->b_iodone == NULL,
1622		    ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1623
1624		/* get a new block */
1625		newbp = geteblk(bp->b_bufsize);
1626
1627		/*
1628		 * set it to be identical to the old block.  We have to
1629		 * set b_lblkno and BKGRDMARKER before calling bgetvp()
1630		 * to avoid confusing the splay tree and gbincore().
1631		 */
1632		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1633		newbp->b_lblkno = bp->b_lblkno;
1634		newbp->b_xflags |= BX_BKGRDMARKER;
1635		BO_LOCK(bp->b_bufobj);
1636		bp->b_vflags |= BV_BKGRDINPROG;
1637		bgetvp(bp->b_vp, newbp);
1638		BO_UNLOCK(bp->b_bufobj);
1639		newbp->b_bufobj = &bp->b_vp->v_bufobj;
1640		newbp->b_blkno = bp->b_blkno;
1641		newbp->b_offset = bp->b_offset;
1642		newbp->b_iodone = ffs_backgroundwritedone;
1643		newbp->b_flags |= B_ASYNC;
1644		newbp->b_flags &= ~B_INVAL;
1645
1646#ifdef SOFTUPDATES
1647		/* move over the dependencies */
1648		if (LIST_FIRST(&bp->b_dep) != NULL)
1649			softdep_move_dependencies(bp, newbp);
1650#endif
1651
1652		/*
1653		 * Initiate write on the copy, release the original to
1654		 * the B_LOCKED queue so that it cannot go away until
1655		 * the background write completes. If not locked it could go
1656		 * away and then be reconstituted while it was being written.
1657		 * If the reconstituted buffer were written, we could end up
1658		 * with two background copies being written at the same time.
1659		 */
1660		bqrelse(bp);
1661		bp = newbp;
1662	}
1663
1664	/* Let the normal bufwrite do the rest for us */
1665	bufwrite(bp);
1666
1667	return (0);
1668}
1669
1670
1671static void
1672ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
1673{
1674	struct vnode *vp;
1675	int error;
1676
1677	vp = bo->__bo_vnode;
1678	if (bp->b_iocmd == BIO_WRITE) {
1679#ifdef SOFTUPDATES
1680		if (LIST_FIRST(&bp->b_dep) != NULL)
1681			buf_start(bp);
1682#endif
1683		if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
1684		    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
1685		    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
1686			panic("ffs_geom_strategy: bad I/O");
1687		bp->b_flags &= ~B_VALIDSUSPWRT;
1688		if ((vp->v_vflag & VV_COPYONWRITE) &&
1689		    vp->v_rdev->si_snapdata != NULL &&
1690		    (error = (ffs_copyonwrite)(vp, bp)) != 0 &&
1691		    error != EOPNOTSUPP) {
1692			bp->b_error = error;
1693			bp->b_ioflags |= BIO_ERROR;
1694			bufdone(bp);
1695			return;
1696		}
1697	}
1698	g_vfs_strategy(bo, bp);
1699}
1700