xref: /freebsd/sys/ufs/ffs/ffs_vfsops.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
1 /*
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
34  * $FreeBSD$
35  */
36 
37 #include "opt_ffs.h"
38 #include "opt_quota.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51 #include <sys/disklabel.h>
52 #include <sys/malloc.h>
53 
54 #include <machine/mutex.h>
55 
56 #include <ufs/ufs/extattr.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufs_extern.h>
61 
62 #include <ufs/ffs/fs.h>
63 #include <ufs/ffs/ffs_extern.h>
64 
65 #include <vm/vm.h>
66 #include <vm/vm_page.h>
67 
68 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
69 
70 static int	ffs_sbupdate __P((struct ufsmount *, int));
71 int	ffs_reload __P((struct mount *,struct ucred *,struct proc *));
72 static int	ffs_oldfscompat __P((struct fs *));
73 static int	ffs_init __P((struct vfsconf *));
74 
75 static struct vfsops ufs_vfsops = {
76 	ffs_mount,
77 	ufs_start,
78 	ffs_unmount,
79 	ufs_root,
80 	ufs_quotactl,
81 	ffs_statfs,
82 	ffs_sync,
83 	ffs_vget,
84 	ffs_fhtovp,
85 	ufs_check_export,
86 	ffs_vptofh,
87 	ffs_init,
88 	vfs_stduninit,
89 #ifdef FFS_EXTATTR
90 	ufs_extattrctl,
91 #else
92 	vfs_stdextattrctl,
93 #endif
94 };
95 
96 VFS_SET(ufs_vfsops, ufs, 0);
97 
98 /*
99  * ffs_mount
100  *
101  * Called when mounting local physical media
102  *
103  * PARAMETERS:
104  *		mountroot
105  *			mp	mount point structure
106  *			path	NULL (flag for root mount!!!)
107  *			data	<unused>
108  *			ndp	<unused>
109  *			p	process (user credentials check [statfs])
110  *
111  *		mount
112  *			mp	mount point structure
113  *			path	path to mount point
114  *			data	pointer to argument struct in user space
115  *			ndp	mount point namei() return (used for
116  *				credentials on reload), reused to look
117  *				up block device.
118  *			p	process (user credentials check)
119  *
120  * RETURNS:	0	Success
121  *		!0	error number (errno.h)
122  *
123  * LOCK STATE:
124  *
125  *		ENTRY
126  *			mount point is locked
127  *		EXIT
128  *			mount point is locked
129  *
130  * NOTES:
131  *		A NULL path can be used for a flag since the mount
132  *		system call will fail with EFAULT in copyinstr in
133  *		namei() if it is a genuine NULL from the user.
134  */
135 int
136 ffs_mount(mp, path, data, ndp, p)
137         struct mount		*mp;	/* mount struct pointer*/
138         char			*path;	/* path to mount point*/
139         caddr_t			data;	/* arguments to FS specific mount*/
140         struct nameidata	*ndp;	/* mount point credentials*/
141         struct proc		*p;	/* process requesting mount*/
142 {
143 	size_t		size;
144 	struct vnode	*devvp;
145 	struct ufs_args args;
146 	struct ufsmount *ump = 0;
147 	register struct fs *fs;
148 	int error, flags;
149 	mode_t accessmode;
150 
151 	/*
152 	 * Use NULL path to indicate we are mounting the root file system.
153 	 */
154 	if (path == NULL) {
155 		if ((error = bdevvp(rootdev, &rootvp))) {
156 			printf("ffs_mountroot: can't find rootvp\n");
157 			return (error);
158 		}
159 
160 		if ((error = ffs_mountfs(rootvp, mp, p, M_FFSNODE)) != 0)
161 			return (error);
162 
163 		(void)VFS_STATFS(mp, &mp->mnt_stat, p);
164 		return (0);
165 	}
166 
167 	/*
168 	 * Mounting non-root file system or updating a file system
169 	 */
170 	if ((error = copyin(data, (caddr_t)&args, sizeof(struct ufs_args)))!= 0)
171 		return (error);
172 
173 	/*
174 	 * If updating, check whether changing from read-only to
175 	 * read/write; if there is no device name, that's all we do.
176 	 */
177 	if (mp->mnt_flag & MNT_UPDATE) {
178 		ump = VFSTOUFS(mp);
179 		fs = ump->um_fs;
180 		devvp = ump->um_devvp;
181 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
182 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
183 				return (error);
184 			flags = WRITECLOSE;
185 			if (mp->mnt_flag & MNT_FORCE)
186 				flags |= FORCECLOSE;
187 			if (mp->mnt_flag & MNT_SOFTDEP) {
188 				error = softdep_flushfiles(mp, flags, p);
189 			} else {
190 				error = ffs_flushfiles(mp, flags, p);
191 			}
192 			if (error) {
193 				vn_finished_write(mp);
194 				return (error);
195 			}
196 			fs->fs_ronly = 1;
197 			if ((fs->fs_flags & FS_UNCLEAN) == 0)
198 				fs->fs_clean = 1;
199 			if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) {
200 				fs->fs_ronly = 0;
201 				fs->fs_clean = 0;
202 				vn_finished_write(mp);
203 				return (error);
204 			}
205 			vn_finished_write(mp);
206 		}
207 		if ((mp->mnt_flag & MNT_RELOAD) &&
208 		    (error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p)) != 0)
209 			return (error);
210 		if (fs->fs_ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
211 			/*
212 			 * If upgrade to read-write by non-root, then verify
213 			 * that user has necessary permissions on the device.
214 			 */
215 			if (p->p_ucred->cr_uid != 0) {
216 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
217 				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
218 				    p->p_ucred, p)) != 0) {
219 					VOP_UNLOCK(devvp, 0, p);
220 					return (error);
221 				}
222 				VOP_UNLOCK(devvp, 0, p);
223 			}
224 			fs->fs_flags &= ~FS_UNCLEAN;
225 			if (fs->fs_clean == 0) {
226 				fs->fs_flags |= FS_UNCLEAN;
227 				if (mp->mnt_flag & MNT_FORCE) {
228 					printf("WARNING: %s was not %s\n",
229 					   fs->fs_fsmnt, "properly dismounted");
230 				} else {
231 					printf(
232 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
233 					    fs->fs_fsmnt);
234 					return (EPERM);
235 				}
236 			}
237 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
238 				return (error);
239 			fs->fs_ronly = 0;
240 			fs->fs_clean = 0;
241 			if ((error = ffs_sbupdate(ump, MNT_WAIT)) != 0) {
242 				vn_finished_write(mp);
243 				return (error);
244 			}
245 			/* check to see if we need to start softdep */
246 			if ((fs->fs_flags & FS_DOSOFTDEP) &&
247 			    (error = softdep_mount(devvp, mp, fs, p->p_ucred))){
248 				vn_finished_write(mp);
249 				return (error);
250 			}
251 			if (fs->fs_snapinum[0] != 0)
252 				ffs_snapshot_mount(mp);
253 			vn_finished_write(mp);
254 		}
255 		/*
256 		 * Soft updates is incompatible with "async",
257 		 * so if we are doing softupdates stop the user
258 		 * from setting the async flag in an update.
259 		 * Softdep_mount() clears it in an initial mount
260 		 * or ro->rw remount.
261 		 */
262 		if (mp->mnt_flag & MNT_SOFTDEP)
263 			mp->mnt_flag &= ~MNT_ASYNC;
264 		/*
265 		 * If not updating name, process export requests.
266 		 */
267 		if (args.fspec == 0)
268 			return (vfs_export(mp, &ump->um_export, &args.export));
269 		/*
270 		 * If this is a snapshot request, take the snapshot.
271 		 */
272 		if (mp->mnt_flag & MNT_SNAPSHOT)
273 			return (ffs_snapshot(mp, args.fspec));
274 	}
275 
276 	/*
277 	 * Not an update, or updating the name: look up the name
278 	 * and verify that it refers to a sensible block device.
279 	 */
280 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
281 	if ((error = namei(ndp)) != 0)
282 		return (error);
283 	NDFREE(ndp, NDF_ONLY_PNBUF);
284 	devvp = ndp->ni_vp;
285 	if (!vn_isdisk(devvp, &error)) {
286 		vrele(devvp);
287 		return (error);
288 	}
289 
290 	/*
291 	 * If mount by non-root, then verify that user has necessary
292 	 * permissions on the device.
293 	 */
294 	if (p->p_ucred->cr_uid != 0) {
295 		accessmode = VREAD;
296 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
297 			accessmode |= VWRITE;
298 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
299 		if ((error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p))!= 0){
300 			vput(devvp);
301 			return (error);
302 		}
303 		VOP_UNLOCK(devvp, 0, p);
304 	}
305 
306 	if (mp->mnt_flag & MNT_UPDATE) {
307 		/*
308 		 * Update only
309 		 *
310 		 * If it's not the same vnode, or at least the same device
311 		 * then it's not correct.
312 		 */
313 
314 		if (devvp != ump->um_devvp &&
315 		    devvp->v_rdev != ump->um_devvp->v_rdev)
316 			error = EINVAL;	/* needs translation */
317 		vrele(devvp);
318 		if (error)
319 			return (error);
320 	} else {
321 		/*
322 		 * New mount
323 		 *
324 		 * We need the name for the mount point (also used for
325 		 * "last mounted on") copied in. If an error occurs,
326 		 * the mount point is discarded by the upper level code.
327 		 */
328 		copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
329 		bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
330 		if ((error = ffs_mountfs(devvp, mp, p, M_FFSNODE)) != 0) {
331 			vrele(devvp);
332 			return (error);
333 		}
334 	}
335 	/*
336 	 * Save "mounted from" device name info for mount point (NULL pad).
337 	 */
338 	copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
339 	bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
340 	/*
341 	 * Initialize filesystem stat information in mount struct.
342 	 */
343 	(void)VFS_STATFS(mp, &mp->mnt_stat, p);
344 	return (0);
345 }
346 
347 /*
348  * Reload all incore data for a filesystem (used after running fsck on
349  * the root filesystem and finding things to fix). The filesystem must
350  * be mounted read-only.
351  *
352  * Things to do to update the mount:
353  *	1) invalidate all cached meta-data.
354  *	2) re-read superblock from disk.
355  *	3) re-read summary information from disk.
356  *	4) invalidate all inactive vnodes.
357  *	5) invalidate all cached file data.
358  *	6) re-read inode data for all active vnodes.
359  */
360 int
361 ffs_reload(mp, cred, p)
362 	register struct mount *mp;
363 	struct ucred *cred;
364 	struct proc *p;
365 {
366 	register struct vnode *vp, *nvp, *devvp;
367 	struct inode *ip;
368 	struct csum *space;
369 	struct buf *bp;
370 	struct fs *fs, *newfs;
371 	struct partinfo dpart;
372 	dev_t dev;
373 	int i, blks, size, error;
374 	int32_t *lp;
375 
376 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
377 		return (EINVAL);
378 	/*
379 	 * Step 1: invalidate all cached meta-data.
380 	 */
381 	devvp = VFSTOUFS(mp)->um_devvp;
382 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
383 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
384 	VOP_UNLOCK(devvp, 0, p);
385 	if (error)
386 		panic("ffs_reload: dirty1");
387 
388 	dev = devvp->v_rdev;
389 
390 	/*
391 	 * Only VMIO the backing device if the backing device is a real
392 	 * block device.  See ffs_mountmfs() for more details.
393 	 */
394 	if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
395 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
396 		vfs_object_create(devvp, p, p->p_ucred);
397 		mtx_enter(&devvp->v_interlock, MTX_DEF);
398 		VOP_UNLOCK(devvp, LK_INTERLOCK, p);
399 	}
400 
401 	/*
402 	 * Step 2: re-read superblock from disk.
403 	 */
404 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
405 		size = DEV_BSIZE;
406 	else
407 		size = dpart.disklab->d_secsize;
408 	if ((error = bread(devvp, (ufs_daddr_t)(SBOFF/size), SBSIZE, NOCRED,&bp)) != 0)
409 		return (error);
410 	newfs = (struct fs *)bp->b_data;
411 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
412 		newfs->fs_bsize < sizeof(struct fs)) {
413 			brelse(bp);
414 			return (EIO);		/* XXX needs translation */
415 	}
416 	fs = VFSTOUFS(mp)->um_fs;
417 	/*
418 	 * Copy pointer fields back into superblock before copying in	XXX
419 	 * new superblock. These should really be in the ufsmount.	XXX
420 	 * Note that important parameters (eg fs_ncg) are unchanged.
421 	 */
422 	bcopy(&fs->fs_csp[0], &newfs->fs_csp[0], sizeof(fs->fs_csp));
423 	newfs->fs_maxcluster = fs->fs_maxcluster;
424 	bcopy(newfs, fs, (u_int)fs->fs_sbsize);
425 	if (fs->fs_sbsize < SBSIZE)
426 		bp->b_flags |= B_INVAL | B_NOCACHE;
427 	brelse(bp);
428 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
429 	ffs_oldfscompat(fs);
430 
431 	/*
432 	 * Step 3: re-read summary information from disk.
433 	 */
434 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
435 	space = fs->fs_csp[0];
436 	for (i = 0; i < blks; i += fs->fs_frag) {
437 		size = fs->fs_bsize;
438 		if (i + fs->fs_frag > blks)
439 			size = (blks - i) * fs->fs_fsize;
440 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
441 		    NOCRED, &bp);
442 		if (error)
443 			return (error);
444 		bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size);
445 		brelse(bp);
446 	}
447 	/*
448 	 * We no longer know anything about clusters per cylinder group.
449 	 */
450 	if (fs->fs_contigsumsize > 0) {
451 		lp = fs->fs_maxcluster;
452 		for (i = 0; i < fs->fs_ncg; i++)
453 			*lp++ = fs->fs_contigsumsize;
454 	}
455 
456 loop:
457 	simple_lock(&mntvnode_slock);
458 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
459 		if (vp->v_mount != mp) {
460 			simple_unlock(&mntvnode_slock);
461 			goto loop;
462 		}
463 		nvp = vp->v_mntvnodes.le_next;
464 		/*
465 		 * Step 4: invalidate all inactive vnodes.
466 		 */
467 		if (vrecycle(vp, &mntvnode_slock, p))
468 			goto loop;
469 		/*
470 		 * Step 5: invalidate all cached file data.
471 		 */
472 		mtx_enter(&vp->v_interlock, MTX_DEF);
473 		simple_unlock(&mntvnode_slock);
474 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
475 			goto loop;
476 		}
477 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
478 			panic("ffs_reload: dirty2");
479 		/*
480 		 * Step 6: re-read inode data for all active vnodes.
481 		 */
482 		ip = VTOI(vp);
483 		error =
484 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
485 		    (int)fs->fs_bsize, NOCRED, &bp);
486 		if (error) {
487 			vput(vp);
488 			return (error);
489 		}
490 		ip->i_din = *((struct dinode *)bp->b_data +
491 		    ino_to_fsbo(fs, ip->i_number));
492 		ip->i_effnlink = ip->i_nlink;
493 		brelse(bp);
494 		vput(vp);
495 		simple_lock(&mntvnode_slock);
496 	}
497 	simple_unlock(&mntvnode_slock);
498 	return (0);
499 }
500 
501 /*
502  * Common code for mount and mountroot
503  */
504 int
505 ffs_mountfs(devvp, mp, p, malloctype)
506 	register struct vnode *devvp;
507 	struct mount *mp;
508 	struct proc *p;
509 	struct malloc_type *malloctype;
510 {
511 	register struct ufsmount *ump;
512 	struct buf *bp;
513 	register struct fs *fs;
514 	dev_t dev;
515 	struct partinfo dpart;
516 	caddr_t base, space;
517 	int error, i, blks, size, ronly;
518 	int32_t *lp;
519 	struct ucred *cred;
520 	u_int64_t maxfilesize;					/* XXX */
521 	size_t strsize;
522 	int ncount;
523 
524 	dev = devvp->v_rdev;
525 	cred = p ? p->p_ucred : NOCRED;
526 	/*
527 	 * Disallow multiple mounts of the same device.
528 	 * Disallow mounting of a device that is currently in use
529 	 * (except for root, which might share swap device for miniroot).
530 	 * Flush out any old buffers remaining from a previous use.
531 	 */
532 	error = vfs_mountedon(devvp);
533 	if (error)
534 		return (error);
535 	ncount = vcount(devvp);
536 
537 	if (ncount > 1 && devvp != rootvp)
538 		return (EBUSY);
539 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
540 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
541 	VOP_UNLOCK(devvp, 0, p);
542 	if (error)
543 		return (error);
544 
545 	/*
546 	 * Only VMIO the backing device if the backing device is a real
547 	 * block device.  This excludes the original MFS implementation.
548 	 * Note that it is optional that the backing device be VMIOed.  This
549 	 * increases the opportunity for metadata caching.
550 	 */
551 	if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
552 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
553 		vfs_object_create(devvp, p, cred);
554 		mtx_enter(&devvp->v_interlock, MTX_DEF);
555 		VOP_UNLOCK(devvp, LK_INTERLOCK, p);
556 	}
557 
558 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
559 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
560 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
561 	VOP_UNLOCK(devvp, 0, p);
562 	if (error)
563 		return (error);
564 	if (devvp->v_rdev->si_iosize_max > mp->mnt_iosize_max)
565 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
566 	if (mp->mnt_iosize_max > MAXPHYS)
567 		mp->mnt_iosize_max = MAXPHYS;
568 
569 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
570 		size = DEV_BSIZE;
571 	else
572 		size = dpart.disklab->d_secsize;
573 
574 	bp = NULL;
575 	ump = NULL;
576 	if ((error = bread(devvp, SBLOCK, SBSIZE, cred, &bp)) != 0)
577 		goto out;
578 	fs = (struct fs *)bp->b_data;
579 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
580 	    fs->fs_bsize < sizeof(struct fs)) {
581 		error = EINVAL;		/* XXX needs translation */
582 		goto out;
583 	}
584 	fs->fs_fmod = 0;
585 	fs->fs_flags &= ~FS_UNCLEAN;
586 	if (fs->fs_clean == 0) {
587 		fs->fs_flags |= FS_UNCLEAN;
588 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
589 			printf(
590 "WARNING: %s was not properly dismounted\n",
591 			    fs->fs_fsmnt);
592 		} else {
593 			printf(
594 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
595 			    fs->fs_fsmnt);
596 			error = EPERM;
597 			goto out;
598 		}
599 	}
600 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
601 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
602 		error = EROFS;          /* needs translation */
603 		goto out;
604 	}
605 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
606 	bzero((caddr_t)ump, sizeof *ump);
607 	ump->um_malloctype = malloctype;
608 	ump->um_i_effnlink_valid = 1;
609 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
610 	    M_WAITOK);
611 	ump->um_blkatoff = ffs_blkatoff;
612 	ump->um_truncate = ffs_truncate;
613 	ump->um_update = ffs_update;
614 	ump->um_valloc = ffs_valloc;
615 	ump->um_vfree = ffs_vfree;
616 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
617 	if (fs->fs_sbsize < SBSIZE)
618 		bp->b_flags |= B_INVAL | B_NOCACHE;
619 	brelse(bp);
620 	bp = NULL;
621 	fs = ump->um_fs;
622 	fs->fs_ronly = ronly;
623 	size = fs->fs_cssize;
624 	blks = howmany(size, fs->fs_fsize);
625 	if (fs->fs_contigsumsize > 0)
626 		size += fs->fs_ncg * sizeof(int32_t);
627 	base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
628 	for (i = 0; i < blks; i += fs->fs_frag) {
629 		size = fs->fs_bsize;
630 		if (i + fs->fs_frag > blks)
631 			size = (blks - i) * fs->fs_fsize;
632 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
633 		    cred, &bp)) != 0) {
634 			free(base, M_UFSMNT);
635 			goto out;
636 		}
637 		bcopy(bp->b_data, space, (u_int)size);
638 		fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
639 		space += size;
640 		brelse(bp);
641 		bp = NULL;
642 	}
643 	if (fs->fs_contigsumsize > 0) {
644 		fs->fs_maxcluster = lp = (int32_t *)space;
645 		for (i = 0; i < fs->fs_ncg; i++)
646 			*lp++ = fs->fs_contigsumsize;
647 	}
648 	mp->mnt_data = (qaddr_t)ump;
649 	mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
650 	mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
651 	if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 ||
652 	    vfs_getvfs(&mp->mnt_stat.f_fsid))
653 		vfs_getnewfsid(mp);
654 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
655 	mp->mnt_flag |= MNT_LOCAL;
656 	ump->um_mountp = mp;
657 	ump->um_dev = dev;
658 	ump->um_devvp = devvp;
659 	ump->um_nindir = fs->fs_nindir;
660 	ump->um_bptrtodb = fs->fs_fsbtodb;
661 	ump->um_seqinc = fs->fs_frag;
662 	for (i = 0; i < MAXQUOTAS; i++)
663 		ump->um_quotas[i] = NULLVP;
664 #ifdef FFS_EXTATTR
665 	ufs_extattr_uepm_init(&ump->um_extattr);
666 #endif
667 	devvp->v_rdev->si_mountpoint = mp;
668 	ffs_oldfscompat(fs);
669 
670 	/*
671 	 * Set FS local "last mounted on" information (NULL pad)
672 	 */
673 	copystr(	mp->mnt_stat.f_mntonname,	/* mount point*/
674 			fs->fs_fsmnt,			/* copy area*/
675 			sizeof(fs->fs_fsmnt) - 1,	/* max size*/
676 			&strsize);			/* real size*/
677 	bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize);
678 
679 	if( mp->mnt_flag & MNT_ROOTFS) {
680 		/*
681 		 * Root mount; update timestamp in mount structure.
682 		 * this will be used by the common root mount code
683 		 * to update the system clock.
684 		 */
685 		mp->mnt_time = fs->fs_time;
686 	}
687 
688 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
689 	maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1;	/* XXX */
690 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
691 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
692 	if (ronly == 0) {
693 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
694 		    (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
695 			free(base, M_UFSMNT);
696 			goto out;
697 		}
698 		if (fs->fs_snapinum[0] != 0)
699 			ffs_snapshot_mount(mp);
700 		fs->fs_fmod = 1;
701 		fs->fs_clean = 0;
702 		(void) ffs_sbupdate(ump, MNT_WAIT);
703 	}
704 #ifdef FFS_EXTATTR
705 	/*
706 	 * XXX Auto-starting of EAs would go here.
707 	 *
708 	 * Auto-starting would:
709 	 *	- check for /.attribute in the fs, and extattr_start if so
710 	 *	- for each file in .attribute, enable that file with
711 	 * 	  an attribute of the same name.
712 	 * Not clear how to report errors -- probably eat them.
713 	 * This would all happen while the file system was busy/not
714 	 * available, so would effectively be "atomic".
715 	 */
716 	/* ufs_extattr_autostart(mp, ump); */
717 #endif
718 	return (0);
719 out:
720 	devvp->v_rdev->si_mountpoint = NULL;
721 	if (bp)
722 		brelse(bp);
723 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
724 	if (ump) {
725 		free(ump->um_fs, M_UFSMNT);
726 		free(ump, M_UFSMNT);
727 		mp->mnt_data = (qaddr_t)0;
728 	}
729 	return (error);
730 }
731 
732 /*
733  * Sanity checks for old file systems.
734  *
735  * XXX - goes away some day.
736  */
737 static int
738 ffs_oldfscompat(fs)
739 	struct fs *fs;
740 {
741 
742 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
743 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
744 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
745 		fs->fs_nrpos = 8;				/* XXX */
746 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
747 #if 0
748 		int i;						/* XXX */
749 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
750 								/* XXX */
751 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
752 		for (i = 0; i < NIADDR; i++) {			/* XXX */
753 			sizepb *= NINDIR(fs);			/* XXX */
754 			fs->fs_maxfilesize += sizepb;		/* XXX */
755 		}						/* XXX */
756 #endif
757 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
758 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
759 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
760 	}							/* XXX */
761 	return (0);
762 }
763 
764 /*
765  * unmount system call
766  */
767 int
768 ffs_unmount(mp, mntflags, p)
769 	struct mount *mp;
770 	int mntflags;
771 	struct proc *p;
772 {
773 	register struct ufsmount *ump = VFSTOUFS(mp);
774 	register struct fs *fs;
775 	int error, flags;
776 
777 	flags = 0;
778 	if (mntflags & MNT_FORCE) {
779 		flags |= FORCECLOSE;
780 	}
781 #ifdef FFS_EXTATTR
782 	if ((error = ufs_extattr_stop(mp, p)))
783 		if (error != EOPNOTSUPP)
784 			printf("ffs_unmount: ufs_extattr_stop returned %d\n",
785 			    error);
786 	ufs_extattr_uepm_destroy(&ump->um_extattr);
787 #endif
788 	if (mp->mnt_flag & MNT_SOFTDEP) {
789 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
790 			return (error);
791 	} else {
792 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
793 			return (error);
794 	}
795 	fs = ump->um_fs;
796 	if (fs->fs_ronly == 0) {
797 		fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
798 		error = ffs_sbupdate(ump, MNT_WAIT);
799 		if (error) {
800 			fs->fs_clean = 0;
801 			return (error);
802 		}
803 	}
804 	ump->um_devvp->v_rdev->si_mountpoint = NULL;
805 
806 	vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, p, 0, 0);
807 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
808 		NOCRED, p);
809 
810 	vrele(ump->um_devvp);
811 
812 	free(fs->fs_csp[0], M_UFSMNT);
813 	free(fs, M_UFSMNT);
814 	free(ump, M_UFSMNT);
815 	mp->mnt_data = (qaddr_t)0;
816 	mp->mnt_flag &= ~MNT_LOCAL;
817 	return (error);
818 }
819 
820 /*
821  * Flush out all the files in a filesystem.
822  */
823 int
824 ffs_flushfiles(mp, flags, p)
825 	register struct mount *mp;
826 	int flags;
827 	struct proc *p;
828 {
829 	register struct ufsmount *ump;
830 	int error;
831 
832 	ump = VFSTOUFS(mp);
833 #ifdef QUOTA
834 	if (mp->mnt_flag & MNT_QUOTA) {
835 		int i;
836 		error = vflush(mp, NULLVP, SKIPSYSTEM|flags);
837 		if (error)
838 			return (error);
839 		for (i = 0; i < MAXQUOTAS; i++) {
840 			if (ump->um_quotas[i] == NULLVP)
841 				continue;
842 			quotaoff(p, mp, i);
843 		}
844 		/*
845 		 * Here we fall through to vflush again to ensure
846 		 * that we have gotten rid of all the system vnodes.
847 		 */
848 	}
849 #endif
850 	if (ump->um_devvp->v_flag & VCOPYONWRITE) {
851 		if ((error = vflush(mp, NULL, SKIPSYSTEM | flags)) != 0)
852 			return (error);
853 		ffs_snapshot_unmount(mp);
854 		/*
855 		 * Here we fall through to vflush again to ensure
856 		 * that we have gotten rid of all the system vnodes.
857 		 */
858 	}
859         /*
860 	 * Flush all the files.
861 	 */
862 	if ((error = vflush(mp, NULL, flags)) != 0)
863 		return (error);
864 	/*
865 	 * Flush filesystem metadata.
866 	 */
867 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
868 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p);
869 	VOP_UNLOCK(ump->um_devvp, 0, p);
870 	return (error);
871 }
872 
873 /*
874  * Get file system statistics.
875  */
876 int
877 ffs_statfs(mp, sbp, p)
878 	struct mount *mp;
879 	register struct statfs *sbp;
880 	struct proc *p;
881 {
882 	register struct ufsmount *ump;
883 	register struct fs *fs;
884 
885 	ump = VFSTOUFS(mp);
886 	fs = ump->um_fs;
887 	if (fs->fs_magic != FS_MAGIC)
888 		panic("ffs_statfs");
889 	sbp->f_bsize = fs->fs_fsize;
890 	sbp->f_iosize = fs->fs_bsize;
891 	sbp->f_blocks = fs->fs_dsize;
892 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
893 		fs->fs_cstotal.cs_nffree;
894 	sbp->f_bavail = freespace(fs, fs->fs_minfree);
895 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
896 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
897 	if (sbp != &mp->mnt_stat) {
898 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
899 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
900 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
901 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
902 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
903 	}
904 	return (0);
905 }
906 
907 /*
908  * Go through the disk queues to initiate sandbagged IO;
909  * go through the inodes to write those that have been modified;
910  * initiate the writing of the super block if it has been modified.
911  *
912  * Note: we are always called with the filesystem marked `MPBUSY'.
913  */
914 int
915 ffs_sync(mp, waitfor, cred, p)
916 	struct mount *mp;
917 	int waitfor;
918 	struct ucred *cred;
919 	struct proc *p;
920 {
921 	struct vnode *nvp, *vp;
922 	struct inode *ip;
923 	struct ufsmount *ump = VFSTOUFS(mp);
924 	struct fs *fs;
925 	int error, count, wait, lockreq, allerror = 0;
926 
927 	fs = ump->um_fs;
928 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
929 		printf("fs = %s\n", fs->fs_fsmnt);
930 		panic("ffs_sync: rofs mod");
931 	}
932 	/*
933 	 * Write back each (modified) inode.
934 	 */
935 	wait = 0;
936 	lockreq = LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK;
937 	if (waitfor == MNT_WAIT) {
938 		wait = 1;
939 		lockreq = LK_EXCLUSIVE | LK_INTERLOCK;
940 	}
941 	simple_lock(&mntvnode_slock);
942 loop:
943 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
944 		/*
945 		 * If the vnode that we are about to sync is no longer
946 		 * associated with this mount point, start over.
947 		 */
948 		if (vp->v_mount != mp)
949 			goto loop;
950 		mtx_enter(&vp->v_interlock, MTX_DEF);
951 		nvp = vp->v_mntvnodes.le_next;
952 		ip = VTOI(vp);
953 		if (vp->v_type == VNON || ((ip->i_flag &
954 		     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
955 		     TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
956 			mtx_exit(&vp->v_interlock, MTX_DEF);
957 			continue;
958 		}
959 		if (vp->v_type != VCHR) {
960 			simple_unlock(&mntvnode_slock);
961 			if ((error = vget(vp, lockreq, p)) != 0) {
962 				simple_lock(&mntvnode_slock);
963 				if (error == ENOENT)
964 					goto loop;
965 				continue;
966 			}
967 			if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
968 				allerror = error;
969 			VOP_UNLOCK(vp, 0, p);
970 			vrele(vp);
971 			simple_lock(&mntvnode_slock);
972 		} else {
973 			simple_unlock(&mntvnode_slock);
974 			mtx_exit(&vp->v_interlock, MTX_DEF);
975 			UFS_UPDATE(vp, wait);
976 			simple_lock(&mntvnode_slock);
977 		}
978 	}
979 	simple_unlock(&mntvnode_slock);
980 	/*
981 	 * Force stale file system control information to be flushed.
982 	 */
983 	if (waitfor == MNT_WAIT) {
984 		if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
985 			allerror = error;
986 		/* Flushed work items may create new vnodes to clean */
987 		if (count) {
988 			simple_lock(&mntvnode_slock);
989 			goto loop;
990 		}
991 	}
992 	if (waitfor == MNT_NOWAIT) {
993 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
994 		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
995 			allerror = error;
996 		VOP_UNLOCK(ump->um_devvp, 0, p);
997 	}
998 #ifdef QUOTA
999 	qsync(mp);
1000 #endif
1001 	/*
1002 	 * Write back modified superblock.
1003 	 */
1004 	if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1005 		allerror = error;
1006 	return (allerror);
1007 }
1008 
1009 /*
1010  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1011  * in from disk.  If it is in core, wait for the lock bit to clear, then
1012  * return the inode locked.  Detection and handling of mount points must be
1013  * done by the calling routine.
1014  */
1015 static int ffs_inode_hash_lock;
1016 
1017 int
1018 ffs_vget(mp, ino, vpp)
1019 	struct mount *mp;
1020 	ino_t ino;
1021 	struct vnode **vpp;
1022 {
1023 	struct fs *fs;
1024 	struct inode *ip;
1025 	struct ufsmount *ump;
1026 	struct buf *bp;
1027 	struct vnode *vp;
1028 	dev_t dev;
1029 	int error;
1030 
1031 	ump = VFSTOUFS(mp);
1032 	dev = ump->um_dev;
1033 restart:
1034 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1035 		return (0);
1036 	}
1037 
1038 	/*
1039 	 * Lock out the creation of new entries in the FFS hash table in
1040 	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1041 	 * may occur!
1042 	 */
1043 	if (ffs_inode_hash_lock) {
1044 		while (ffs_inode_hash_lock) {
1045 			ffs_inode_hash_lock = -1;
1046 			tsleep(&ffs_inode_hash_lock, PVM, "ffsvgt", 0);
1047 		}
1048 		goto restart;
1049 	}
1050 	ffs_inode_hash_lock = 1;
1051 
1052 	/*
1053 	 * If this MALLOC() is performed after the getnewvnode()
1054 	 * it might block, leaving a vnode with a NULL v_data to be
1055 	 * found by ffs_sync() if a sync happens to fire right then,
1056 	 * which will cause a panic because ffs_sync() blindly
1057 	 * dereferences vp->v_data (as well it should).
1058 	 */
1059 	MALLOC(ip, struct inode *, sizeof(struct inode),
1060 	    ump->um_malloctype, M_WAITOK);
1061 
1062 	/* Allocate a new vnode/inode. */
1063 	error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp);
1064 	if (error) {
1065 		if (ffs_inode_hash_lock < 0)
1066 			wakeup(&ffs_inode_hash_lock);
1067 		ffs_inode_hash_lock = 0;
1068 		*vpp = NULL;
1069 		FREE(ip, ump->um_malloctype);
1070 		return (error);
1071 	}
1072 	bzero((caddr_t)ip, sizeof(struct inode));
1073 	/*
1074 	 * FFS supports lock sharing in the stack of vnodes
1075 	 */
1076 	vp->v_vnlock = &vp->v_lock;
1077 	lockinit(vp->v_vnlock, PINOD, "inode", 0, LK_CANRECURSE);
1078 	vp->v_data = ip;
1079 	ip->i_vnode = vp;
1080 	ip->i_fs = fs = ump->um_fs;
1081 	ip->i_dev = dev;
1082 	ip->i_number = ino;
1083 #ifdef QUOTA
1084 	{
1085 		int i;
1086 		for (i = 0; i < MAXQUOTAS; i++)
1087 			ip->i_dquot[i] = NODQUOT;
1088 	}
1089 #endif
1090 	/*
1091 	 * Put it onto its hash chain and lock it so that other requests for
1092 	 * this inode will block if they arrive while we are sleeping waiting
1093 	 * for old data structures to be purged or for the contents of the
1094 	 * disk portion of this inode to be read.
1095 	 */
1096 	ufs_ihashins(ip);
1097 
1098 	if (ffs_inode_hash_lock < 0)
1099 		wakeup(&ffs_inode_hash_lock);
1100 	ffs_inode_hash_lock = 0;
1101 
1102 	/* Read in the disk contents for the inode, copy into the inode. */
1103 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1104 	    (int)fs->fs_bsize, NOCRED, &bp);
1105 	if (error) {
1106 		/*
1107 		 * The inode does not contain anything useful, so it would
1108 		 * be misleading to leave it on its hash chain. With mode
1109 		 * still zero, it will be unlinked and returned to the free
1110 		 * list by vput().
1111 		 */
1112 		brelse(bp);
1113 		vput(vp);
1114 		*vpp = NULL;
1115 		return (error);
1116 	}
1117 	ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1118 	if (DOINGSOFTDEP(vp))
1119 		softdep_load_inodeblock(ip);
1120 	else
1121 		ip->i_effnlink = ip->i_nlink;
1122 	bqrelse(bp);
1123 
1124 	/*
1125 	 * Initialize the vnode from the inode, check for aliases.
1126 	 * Note that the underlying vnode may have changed.
1127 	 */
1128 	error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1129 	if (error) {
1130 		vput(vp);
1131 		*vpp = NULL;
1132 		return (error);
1133 	}
1134 	/*
1135 	 * Finish inode initialization now that aliasing has been resolved.
1136 	 */
1137 	ip->i_devvp = ump->um_devvp;
1138 	VREF(ip->i_devvp);
1139 	/*
1140 	 * Set up a generation number for this inode if it does not
1141 	 * already have one. This should only happen on old filesystems.
1142 	 */
1143 	if (ip->i_gen == 0) {
1144 		ip->i_gen = random() / 2 + 1;
1145 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1146 			ip->i_flag |= IN_MODIFIED;
1147 	}
1148 	/*
1149 	 * Ensure that uid and gid are correct. This is a temporary
1150 	 * fix until fsck has been changed to do the update.
1151 	 */
1152 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
1153 		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
1154 		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
1155 	}						/* XXX */
1156 
1157 	*vpp = vp;
1158 	return (0);
1159 }
1160 
1161 /*
1162  * File handle to vnode
1163  *
1164  * Have to be really careful about stale file handles:
1165  * - check that the inode number is valid
1166  * - call ffs_vget() to get the locked inode
1167  * - check for an unallocated inode (i_mode == 0)
1168  * - check that the given client host has export rights and return
1169  *   those rights via. exflagsp and credanonp
1170  */
1171 int
1172 ffs_fhtovp(mp, fhp, vpp)
1173 	register struct mount *mp;
1174 	struct fid *fhp;
1175 	struct vnode **vpp;
1176 {
1177 	register struct ufid *ufhp;
1178 	struct fs *fs;
1179 
1180 	ufhp = (struct ufid *)fhp;
1181 	fs = VFSTOUFS(mp)->um_fs;
1182 	if (ufhp->ufid_ino < ROOTINO ||
1183 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1184 		return (ESTALE);
1185 	return (ufs_fhtovp(mp, ufhp, vpp));
1186 }
1187 
1188 /*
1189  * Vnode pointer to File handle
1190  */
1191 /* ARGSUSED */
1192 int
1193 ffs_vptofh(vp, fhp)
1194 	struct vnode *vp;
1195 	struct fid *fhp;
1196 {
1197 	register struct inode *ip;
1198 	register struct ufid *ufhp;
1199 
1200 	ip = VTOI(vp);
1201 	ufhp = (struct ufid *)fhp;
1202 	ufhp->ufid_len = sizeof(struct ufid);
1203 	ufhp->ufid_ino = ip->i_number;
1204 	ufhp->ufid_gen = ip->i_gen;
1205 	return (0);
1206 }
1207 
1208 /*
1209  * Initialize the filesystem; just use ufs_init.
1210  */
1211 static int
1212 ffs_init(vfsp)
1213 	struct vfsconf *vfsp;
1214 {
1215 
1216 	softdep_initialize();
1217 	return (ufs_init(vfsp));
1218 }
1219 
1220 /*
1221  * Write a superblock and associated information back to disk.
1222  */
1223 static int
1224 ffs_sbupdate(mp, waitfor)
1225 	struct ufsmount *mp;
1226 	int waitfor;
1227 {
1228 	register struct fs *dfs, *fs = mp->um_fs;
1229 	register struct buf *bp;
1230 	int blks;
1231 	caddr_t space;
1232 	int i, size, error, allerror = 0;
1233 
1234 	/*
1235 	 * First write back the summary information.
1236 	 */
1237 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
1238 	space = (caddr_t)fs->fs_csp[0];
1239 	for (i = 0; i < blks; i += fs->fs_frag) {
1240 		size = fs->fs_bsize;
1241 		if (i + fs->fs_frag > blks)
1242 			size = (blks - i) * fs->fs_fsize;
1243 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1244 		    size, 0, 0);
1245 		bcopy(space, bp->b_data, (u_int)size);
1246 		space += size;
1247 		if (waitfor != MNT_WAIT)
1248 			bawrite(bp);
1249 		else if ((error = bwrite(bp)) != 0)
1250 			allerror = error;
1251 	}
1252 	/*
1253 	 * Now write back the superblock itself. If any errors occurred
1254 	 * up to this point, then fail so that the superblock avoids
1255 	 * being written out as clean.
1256 	 */
1257 	if (allerror)
1258 		return (allerror);
1259 	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1260 	fs->fs_fmod = 0;
1261 	fs->fs_time = time_second;
1262 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1263 	/* Restore compatibility to old file systems.		   XXX */
1264 	dfs = (struct fs *)bp->b_data;				/* XXX */
1265 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
1266 		dfs->fs_nrpos = -1;				/* XXX */
1267 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
1268 		int32_t *lp, tmp;				/* XXX */
1269 								/* XXX */
1270 		lp = (int32_t *)&dfs->fs_qbmask;		/* XXX */
1271 		tmp = lp[4];					/* XXX */
1272 		for (i = 4; i > 0; i--)				/* XXX */
1273 			lp[i] = lp[i-1];			/* XXX */
1274 		lp[0] = tmp;					/* XXX */
1275 	}							/* XXX */
1276 	dfs->fs_maxfilesize = mp->um_savedmaxfilesize;		/* XXX */
1277 	if (waitfor != MNT_WAIT)
1278 		bawrite(bp);
1279 	else if ((error = bwrite(bp)) != 0)
1280 		allerror = error;
1281 	return (allerror);
1282 }
1283