xref: /original-bsd/sys/ufs/ffs/ffs_vfsops.c (revision 4ba124f7)
1 /*
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ffs_vfsops.c	8.11 (Berkeley) 07/14/94
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/namei.h>
13 #include <sys/proc.h>
14 #include <sys/kernel.h>
15 #include <sys/vnode.h>
16 #include <sys/socket.h>
17 #include <sys/mount.h>
18 #include <sys/buf.h>
19 #include <sys/mbuf.h>
20 #include <sys/file.h>
21 #include <sys/disklabel.h>
22 #include <sys/ioctl.h>
23 #include <sys/errno.h>
24 #include <sys/malloc.h>
25 
26 #include <miscfs/specfs/specdev.h>
27 
28 #include <ufs/ufs/quota.h>
29 #include <ufs/ufs/ufsmount.h>
30 #include <ufs/ufs/inode.h>
31 #include <ufs/ufs/ufs_extern.h>
32 
33 #include <ufs/ffs/fs.h>
34 #include <ufs/ffs/ffs_extern.h>
35 
36 int ffs_sbupdate __P((struct ufsmount *, int));
37 
38 struct vfsops ufs_vfsops = {
39 	ffs_mount,
40 	ufs_start,
41 	ffs_unmount,
42 	ufs_root,
43 	ufs_quotactl,
44 	ffs_statfs,
45 	ffs_sync,
46 	ffs_vget,
47 	ffs_fhtovp,
48 	ffs_vptofh,
49 	ffs_init,
50 };
51 
52 extern u_long nextgennumber;
53 
54 /*
55  * Called by main() when ufs is going to be mounted as root.
56  *
57  * Name is updated by mount(8) after booting.
58  */
59 #define ROOTNAME	"root_device"
60 
61 ffs_mountroot()
62 {
63 	extern struct vnode *rootvp;
64 	register struct fs *fs;
65 	register struct mount *mp;
66 	struct proc *p = curproc;	/* XXX */
67 	struct ufsmount *ump;
68 	u_int size;
69 	int error;
70 
71 	/*
72 	 * Get vnodes for swapdev and rootdev.
73 	 */
74 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
75 		panic("ffs_mountroot: can't setup bdevvp's");
76 
77 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
78 	bzero((char *)mp, (u_long)sizeof(struct mount));
79 	mp->mnt_op = &ufs_vfsops;
80 	mp->mnt_flag = MNT_RDONLY;
81 	if (error = ffs_mountfs(rootvp, mp, p)) {
82 		free(mp, M_MOUNT);
83 		return (error);
84 	}
85 	if (error = vfs_lock(mp)) {
86 		(void)ffs_unmount(mp, 0, p);
87 		free(mp, M_MOUNT);
88 		return (error);
89 	}
90 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
91 	mp->mnt_flag |= MNT_ROOTFS;
92 	mp->mnt_vnodecovered = NULLVP;
93 	ump = VFSTOUFS(mp);
94 	fs = ump->um_fs;
95 	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
96 	fs->fs_fsmnt[0] = '/';
97 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
98 	    MNAMELEN);
99 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
100 	    &size);
101 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
102 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
103 	vfs_unlock(mp);
104 	inittodr(fs->fs_time);
105 	return (0);
106 }
107 
108 /*
109  * VFS Operations.
110  *
111  * mount system call
112  */
113 int
114 ffs_mount(mp, path, data, ndp, p)
115 	register struct mount *mp;
116 	char *path;
117 	caddr_t data;
118 	struct nameidata *ndp;
119 	struct proc *p;
120 {
121 	struct vnode *devvp;
122 	struct ufs_args args;
123 	struct ufsmount *ump;
124 	register struct fs *fs;
125 	u_int size;
126 	int error, flags;
127 	mode_t accessmode;
128 
129 	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
130 		return (error);
131 	/*
132 	 * If updating, check whether changing from read-only to
133 	 * read/write; if there is no device name, that's all we do.
134 	 */
135 	if (mp->mnt_flag & MNT_UPDATE) {
136 		ump = VFSTOUFS(mp);
137 		fs = ump->um_fs;
138 		error = 0;
139 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
140 			flags = WRITECLOSE;
141 			if (mp->mnt_flag & MNT_FORCE)
142 				flags |= FORCECLOSE;
143 			if (vfs_busy(mp))
144 				return (EBUSY);
145 			error = ffs_flushfiles(mp, flags, p);
146 			vfs_unbusy(mp);
147 		}
148 		if (!error && (mp->mnt_flag & MNT_RELOAD))
149 			error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
150 		if (error)
151 			return (error);
152 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
153 			/*
154 			 * If upgrade to read-write by non-root, then verify
155 			 * that user has necessary permissions on the device.
156 			 */
157 			if (p->p_ucred->cr_uid != 0) {
158 				devvp = ump->um_devvp;
159 				VOP_LOCK(devvp);
160 				if (error = VOP_ACCESS(devvp, VREAD | VWRITE,
161 				    p->p_ucred, p)) {
162 					VOP_UNLOCK(devvp);
163 					return (error);
164 				}
165 				VOP_UNLOCK(devvp);
166 			}
167 			fs->fs_ronly = 0;
168 		}
169 		if (args.fspec == 0) {
170 			/*
171 			 * Process export requests.
172 			 */
173 			return (vfs_export(mp, &ump->um_export, &args.export));
174 		}
175 	}
176 	/*
177 	 * Not an update, or updating the name: look up the name
178 	 * and verify that it refers to a sensible block device.
179 	 */
180 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
181 	if (error = namei(ndp))
182 		return (error);
183 	devvp = ndp->ni_vp;
184 
185 	if (devvp->v_type != VBLK) {
186 		vrele(devvp);
187 		return (ENOTBLK);
188 	}
189 	if (major(devvp->v_rdev) >= nblkdev) {
190 		vrele(devvp);
191 		return (ENXIO);
192 	}
193 	/*
194 	 * If mount by non-root, then verify that user has necessary
195 	 * permissions on the device.
196 	 */
197 	if (p->p_ucred->cr_uid != 0) {
198 		accessmode = VREAD;
199 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
200 			accessmode |= VWRITE;
201 		VOP_LOCK(devvp);
202 		if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
203 			vput(devvp);
204 			return (error);
205 		}
206 		VOP_UNLOCK(devvp);
207 	}
208 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
209 		error = ffs_mountfs(devvp, mp, p);
210 	else {
211 		if (devvp != ump->um_devvp)
212 			error = EINVAL;	/* needs translation */
213 		else
214 			vrele(devvp);
215 	}
216 	if (error) {
217 		vrele(devvp);
218 		return (error);
219 	}
220 	ump = VFSTOUFS(mp);
221 	fs = ump->um_fs;
222 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
223 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
224 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
225 	    MNAMELEN);
226 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
227 	    &size);
228 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
229 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
230 	return (0);
231 }
232 
233 /*
234  * Reload all incore data for a filesystem (used after running fsck on
235  * the root filesystem and finding things to fix). The filesystem must
236  * be mounted read-only.
237  *
238  * Things to do to update the mount:
239  *	1) invalidate all cached meta-data.
240  *	2) re-read superblock from disk.
241  *	3) re-read summary information from disk.
242  *	4) invalidate all inactive vnodes.
243  *	5) invalidate all cached file data.
244  *	6) re-read inode data for all active vnodes.
245  */
246 ffs_reload(mountp, cred, p)
247 	register struct mount *mountp;
248 	struct ucred *cred;
249 	struct proc *p;
250 {
251 	register struct vnode *vp, *nvp, *devvp;
252 	struct inode *ip;
253 	struct csum *space;
254 	struct buf *bp;
255 	struct fs *fs;
256 	int i, blks, size, error;
257 
258 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
259 		return (EINVAL);
260 	/*
261 	 * Step 1: invalidate all cached meta-data.
262 	 */
263 	devvp = VFSTOUFS(mountp)->um_devvp;
264 	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
265 		panic("ffs_reload: dirty1");
266 	/*
267 	 * Step 2: re-read superblock from disk.
268 	 */
269 	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
270 		return (error);
271 	fs = (struct fs *)bp->b_data;
272 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
273 	    fs->fs_bsize < sizeof(struct fs)) {
274 		brelse(bp);
275 		return (EIO);		/* XXX needs translation */
276 	}
277 	fs = VFSTOUFS(mountp)->um_fs;
278 	bcopy(&fs->fs_csp[0], &((struct fs *)bp->b_data)->fs_csp[0],
279 	    sizeof(fs->fs_csp));
280 	bcopy(bp->b_data, fs, (u_int)fs->fs_sbsize);
281 	if (fs->fs_sbsize < SBSIZE)
282 		bp->b_flags |= B_INVAL;
283 	brelse(bp);
284 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
285 	ffs_oldfscompat(fs);
286 	/*
287 	 * Step 3: re-read summary information from disk.
288 	 */
289 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
290 	space = fs->fs_csp[0];
291 	for (i = 0; i < blks; i += fs->fs_frag) {
292 		size = fs->fs_bsize;
293 		if (i + fs->fs_frag > blks)
294 			size = (blks - i) * fs->fs_fsize;
295 		if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
296 		    NOCRED, &bp))
297 			return (error);
298 		bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size);
299 		brelse(bp);
300 	}
301 loop:
302 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
303 		nvp = vp->v_mntvnodes.le_next;
304 		/*
305 		 * Step 4: invalidate all inactive vnodes.
306 		 */
307 		if (vp->v_usecount == 0) {
308 			vgone(vp);
309 			continue;
310 		}
311 		/*
312 		 * Step 5: invalidate all cached file data.
313 		 */
314 		if (vget(vp, 1))
315 			goto loop;
316 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
317 			panic("ffs_reload: dirty2");
318 		/*
319 		 * Step 6: re-read inode data for all active vnodes.
320 		 */
321 		ip = VTOI(vp);
322 		if (error =
323 		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
324 		    (int)fs->fs_bsize, NOCRED, &bp)) {
325 			vput(vp);
326 			return (error);
327 		}
328 		ip->i_din = *((struct dinode *)bp->b_data +
329 		    ino_to_fsbo(fs, ip->i_number));
330 		brelse(bp);
331 		vput(vp);
332 		if (vp->v_mount != mountp)
333 			goto loop;
334 	}
335 	return (0);
336 }
337 
338 /*
339  * Common code for mount and mountroot
340  */
341 int
342 ffs_mountfs(devvp, mp, p)
343 	register struct vnode *devvp;
344 	struct mount *mp;
345 	struct proc *p;
346 {
347 	register struct ufsmount *ump;
348 	struct buf *bp;
349 	register struct fs *fs;
350 	dev_t dev = devvp->v_rdev;
351 	struct partinfo dpart;
352 	caddr_t base, space;
353 	int havepart = 0, blks;
354 	int error, i, size;
355 	int ronly;
356 	extern struct vnode *rootvp;
357 
358 	/*
359 	 * Disallow multiple mounts of the same device.
360 	 * Disallow mounting of a device that is currently in use
361 	 * (except for root, which might share swap device for miniroot).
362 	 * Flush out any old buffers remaining from a previous use.
363 	 */
364 	if (error = vfs_mountedon(devvp))
365 		return (error);
366 	if (vcount(devvp) > 1 && devvp != rootvp)
367 		return (EBUSY);
368 	if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
369 		return (error);
370 
371 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
372 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
373 		return (error);
374 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
375 		size = DEV_BSIZE;
376 	else {
377 		havepart = 1;
378 		size = dpart.disklab->d_secsize;
379 	}
380 
381 	bp = NULL;
382 	ump = NULL;
383 	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
384 		goto out;
385 	fs = (struct fs *)bp->b_data;
386 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
387 	    fs->fs_bsize < sizeof(struct fs)) {
388 		error = EINVAL;		/* XXX needs translation */
389 		goto out;
390 	}
391 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
392 	bzero((caddr_t)ump, sizeof *ump);
393 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
394 	    M_WAITOK);
395 	bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
396 	if (fs->fs_sbsize < SBSIZE)
397 		bp->b_flags |= B_INVAL;
398 	brelse(bp);
399 	bp = NULL;
400 	fs = ump->um_fs;
401 	fs->fs_ronly = ronly;
402 	if (ronly == 0)
403 		fs->fs_fmod = 1;
404 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
405 	base = space = malloc((u_long)fs->fs_cssize, M_UFSMNT,
406 	    M_WAITOK);
407 	for (i = 0; i < blks; i += fs->fs_frag) {
408 		size = fs->fs_bsize;
409 		if (i + fs->fs_frag > blks)
410 			size = (blks - i) * fs->fs_fsize;
411 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
412 			NOCRED, &bp);
413 		if (error) {
414 			free(base, M_UFSMNT);
415 			goto out;
416 		}
417 		bcopy(bp->b_data, space, (u_int)size);
418 		fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
419 		space += size;
420 		brelse(bp);
421 		bp = NULL;
422 	}
423 	mp->mnt_data = (qaddr_t)ump;
424 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
425 	mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS;
426 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
427 	mp->mnt_flag |= MNT_LOCAL;
428 	ump->um_mountp = mp;
429 	ump->um_dev = dev;
430 	ump->um_devvp = devvp;
431 	ump->um_nindir = fs->fs_nindir;
432 	ump->um_bptrtodb = fs->fs_fsbtodb;
433 	ump->um_seqinc = fs->fs_frag;
434 	for (i = 0; i < MAXQUOTAS; i++)
435 		ump->um_quotas[i] = NULLVP;
436 	devvp->v_specflags |= SI_MOUNTEDON;
437 	ffs_oldfscompat(fs);
438 	return (0);
439 out:
440 	if (bp)
441 		brelse(bp);
442 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
443 	if (ump) {
444 		free(ump->um_fs, M_UFSMNT);
445 		free(ump, M_UFSMNT);
446 		mp->mnt_data = (qaddr_t)0;
447 	}
448 	return (error);
449 }
450 
451 /*
452  * Sanity checks for old file systems.
453  *
454  * XXX - goes away some day.
455  */
456 ffs_oldfscompat(fs)
457 	struct fs *fs;
458 {
459 	int i;
460 
461 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
462 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
463 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
464 		fs->fs_nrpos = 8;				/* XXX */
465 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
466 		quad_t sizepb = fs->fs_bsize;			/* XXX */
467 								/* XXX */
468 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
469 		for (i = 0; i < NIADDR; i++) {			/* XXX */
470 			sizepb *= NINDIR(fs);			/* XXX */
471 			fs->fs_maxfilesize += sizepb;		/* XXX */
472 		}						/* XXX */
473 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
474 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
475 	}							/* XXX */
476 	return (0);
477 }
478 
479 /*
480  * unmount system call
481  */
482 int
483 ffs_unmount(mp, mntflags, p)
484 	struct mount *mp;
485 	int mntflags;
486 	struct proc *p;
487 {
488 	register struct ufsmount *ump;
489 	register struct fs *fs;
490 	int error, flags, ronly;
491 
492 	flags = 0;
493 	if (mntflags & MNT_FORCE) {
494 		if (mp->mnt_flag & MNT_ROOTFS)
495 			return (EINVAL);
496 		flags |= FORCECLOSE;
497 	}
498 	if (error = ffs_flushfiles(mp, flags, p))
499 		return (error);
500 	ump = VFSTOUFS(mp);
501 	fs = ump->um_fs;
502 	ronly = !fs->fs_ronly;
503 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
504 	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
505 		NOCRED, p);
506 	vrele(ump->um_devvp);
507 	free(fs->fs_csp[0], M_UFSMNT);
508 	free(fs, M_UFSMNT);
509 	free(ump, M_UFSMNT);
510 	mp->mnt_data = (qaddr_t)0;
511 	mp->mnt_flag &= ~MNT_LOCAL;
512 	return (error);
513 }
514 
515 /*
516  * Flush out all the files in a filesystem.
517  */
518 ffs_flushfiles(mp, flags, p)
519 	register struct mount *mp;
520 	int flags;
521 	struct proc *p;
522 {
523 	extern int doforce;
524 	register struct ufsmount *ump;
525 	int i, error;
526 
527 	if (!doforce)
528 		flags &= ~FORCECLOSE;
529 	ump = VFSTOUFS(mp);
530 #ifdef QUOTA
531 	if (mp->mnt_flag & MNT_QUOTA) {
532 		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
533 			return (error);
534 		for (i = 0; i < MAXQUOTAS; i++) {
535 			if (ump->um_quotas[i] == NULLVP)
536 				continue;
537 			quotaoff(p, mp, i);
538 		}
539 		/*
540 		 * Here we fall through to vflush again to ensure
541 		 * that we have gotten rid of all the system vnodes.
542 		 */
543 	}
544 #endif
545 	error = vflush(mp, NULLVP, flags);
546 	return (error);
547 }
548 
549 /*
550  * Get file system statistics.
551  */
552 int
553 ffs_statfs(mp, sbp, p)
554 	struct mount *mp;
555 	register struct statfs *sbp;
556 	struct proc *p;
557 {
558 	register struct ufsmount *ump;
559 	register struct fs *fs;
560 
561 	ump = VFSTOUFS(mp);
562 	fs = ump->um_fs;
563 	if (fs->fs_magic != FS_MAGIC)
564 		panic("ffs_statfs");
565 	sbp->f_type = MOUNT_UFS;
566 	sbp->f_bsize = fs->fs_fsize;
567 	sbp->f_iosize = fs->fs_bsize;
568 	sbp->f_blocks = fs->fs_dsize;
569 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
570 		fs->fs_cstotal.cs_nffree;
571 	sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
572 		(fs->fs_dsize - sbp->f_bfree);
573 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
574 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
575 	if (sbp != &mp->mnt_stat) {
576 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
577 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
578 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
579 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
580 	}
581 	return (0);
582 }
583 
584 /*
585  * Go through the disk queues to initiate sandbagged IO;
586  * go through the inodes to write those that have been modified;
587  * initiate the writing of the super block if it has been modified.
588  *
589  * Note: we are always called with the filesystem marked `MPBUSY'.
590  */
591 int
592 ffs_sync(mp, waitfor, cred, p)
593 	struct mount *mp;
594 	int waitfor;
595 	struct ucred *cred;
596 	struct proc *p;
597 {
598 	register struct vnode *vp;
599 	register struct inode *ip;
600 	register struct ufsmount *ump = VFSTOUFS(mp);
601 	register struct fs *fs;
602 	int error, allerror = 0;
603 
604 	fs = ump->um_fs;
605 	/*
606 	 * Write back modified superblock.
607 	 * Consistency check that the superblock
608 	 * is still in the buffer cache.
609 	 */
610 	if (fs->fs_fmod != 0) {
611 		if (fs->fs_ronly != 0) {		/* XXX */
612 			printf("fs = %s\n", fs->fs_fsmnt);
613 			panic("update: rofs mod");
614 		}
615 		fs->fs_fmod = 0;
616 		fs->fs_time = time.tv_sec;
617 		allerror = ffs_sbupdate(ump, waitfor);
618 	}
619 	/*
620 	 * Write back each (modified) inode.
621 	 */
622 loop:
623 	for (vp = mp->mnt_vnodelist.lh_first;
624 	     vp != NULL;
625 	     vp = vp->v_mntvnodes.le_next) {
626 		/*
627 		 * If the vnode that we are about to sync is no longer
628 		 * associated with this mount point, start over.
629 		 */
630 		if (vp->v_mount != mp)
631 			goto loop;
632 		if (VOP_ISLOCKED(vp))
633 			continue;
634 		ip = VTOI(vp);
635 		if ((ip->i_flag &
636 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
637 		    vp->v_dirtyblkhd.lh_first == NULL)
638 			continue;
639 		if (vget(vp, 1))
640 			goto loop;
641 		if (error = VOP_FSYNC(vp, cred, waitfor, p))
642 			allerror = error;
643 		vput(vp);
644 	}
645 	/*
646 	 * Force stale file system control information to be flushed.
647 	 */
648 	if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
649 		allerror = error;
650 #ifdef QUOTA
651 	qsync(mp);
652 #endif
653 	return (allerror);
654 }
655 
656 /*
657  * Look up a FFS dinode number to find its incore vnode, otherwise read it
658  * in from disk.  If it is in core, wait for the lock bit to clear, then
659  * return the inode locked.  Detection and handling of mount points must be
660  * done by the calling routine.
661  */
662 int
663 ffs_vget(mp, ino, vpp)
664 	struct mount *mp;
665 	ino_t ino;
666 	struct vnode **vpp;
667 {
668 	register struct fs *fs;
669 	register struct inode *ip;
670 	struct ufsmount *ump;
671 	struct buf *bp;
672 	struct vnode *vp;
673 	dev_t dev;
674 	int i, type, error;
675 
676 	ump = VFSTOUFS(mp);
677 	dev = ump->um_dev;
678 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
679 		return (0);
680 
681 	/* Allocate a new vnode/inode. */
682 	if (error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) {
683 		*vpp = NULL;
684 		return (error);
685 	}
686 	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
687 	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
688 	bzero((caddr_t)ip, sizeof(struct inode));
689 	vp->v_data = ip;
690 	ip->i_vnode = vp;
691 	ip->i_fs = fs = ump->um_fs;
692 	ip->i_dev = dev;
693 	ip->i_number = ino;
694 #ifdef QUOTA
695 	for (i = 0; i < MAXQUOTAS; i++)
696 		ip->i_dquot[i] = NODQUOT;
697 #endif
698 	/*
699 	 * Put it onto its hash chain and lock it so that other requests for
700 	 * this inode will block if they arrive while we are sleeping waiting
701 	 * for old data structures to be purged or for the contents of the
702 	 * disk portion of this inode to be read.
703 	 */
704 	ufs_ihashins(ip);
705 
706 	/* Read in the disk contents for the inode, copy into the inode. */
707 	if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
708 	    (int)fs->fs_bsize, NOCRED, &bp)) {
709 		/*
710 		 * The inode does not contain anything useful, so it would
711 		 * be misleading to leave it on its hash chain. With mode
712 		 * still zero, it will be unlinked and returned to the free
713 		 * list by vput().
714 		 */
715 		vput(vp);
716 		brelse(bp);
717 		*vpp = NULL;
718 		return (error);
719 	}
720 	ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
721 	brelse(bp);
722 
723 	/*
724 	 * Initialize the vnode from the inode, check for aliases.
725 	 * Note that the underlying vnode may have changed.
726 	 */
727 	if (error = ufs_vinit(mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
728 		vput(vp);
729 		*vpp = NULL;
730 		return (error);
731 	}
732 	/*
733 	 * Finish inode initialization now that aliasing has been resolved.
734 	 */
735 	ip->i_devvp = ump->um_devvp;
736 	VREF(ip->i_devvp);
737 	/*
738 	 * Set up a generation number for this inode if it does not
739 	 * already have one. This should only happen on old filesystems.
740 	 */
741 	if (ip->i_gen == 0) {
742 		if (++nextgennumber < (u_long)time.tv_sec)
743 			nextgennumber = time.tv_sec;
744 		ip->i_gen = nextgennumber;
745 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
746 			ip->i_flag |= IN_MODIFIED;
747 	}
748 	/*
749 	 * Ensure that uid and gid are correct. This is a temporary
750 	 * fix until fsck has been changed to do the update.
751 	 */
752 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
753 		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
754 		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
755 	}						/* XXX */
756 
757 	*vpp = vp;
758 	return (0);
759 }
760 
761 /*
762  * File handle to vnode
763  *
764  * Have to be really careful about stale file handles:
765  * - check that the inode number is valid
766  * - call ffs_vget() to get the locked inode
767  * - check for an unallocated inode (i_mode == 0)
768  * - check that the given client host has export rights and return
769  *   those rights via. exflagsp and credanonp
770  */
771 int
772 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
773 	register struct mount *mp;
774 	struct fid *fhp;
775 	struct mbuf *nam;
776 	struct vnode **vpp;
777 	int *exflagsp;
778 	struct ucred **credanonp;
779 {
780 	register struct ufid *ufhp;
781 	struct fs *fs;
782 
783 	ufhp = (struct ufid *)fhp;
784 	fs = VFSTOUFS(mp)->um_fs;
785 	if (ufhp->ufid_ino < ROOTINO ||
786 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
787 		return (ESTALE);
788 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
789 }
790 
791 /*
792  * Vnode pointer to File handle
793  */
794 /* ARGSUSED */
795 ffs_vptofh(vp, fhp)
796 	struct vnode *vp;
797 	struct fid *fhp;
798 {
799 	register struct inode *ip;
800 	register struct ufid *ufhp;
801 
802 	ip = VTOI(vp);
803 	ufhp = (struct ufid *)fhp;
804 	ufhp->ufid_len = sizeof(struct ufid);
805 	ufhp->ufid_ino = ip->i_number;
806 	ufhp->ufid_gen = ip->i_gen;
807 	return (0);
808 }
809 
810 /*
811  * Write a superblock and associated information back to disk.
812  */
813 int
814 ffs_sbupdate(mp, waitfor)
815 	struct ufsmount *mp;
816 	int waitfor;
817 {
818 	register struct fs *fs = mp->um_fs;
819 	register struct buf *bp;
820 	int blks;
821 	caddr_t space;
822 	int i, size, error = 0;
823 
824 	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
825 	bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
826 	/* Restore compatibility to old file systems.		   XXX */
827 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
828 		((struct fs *)bp->b_data)->fs_nrpos = -1;	/* XXX */
829 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
830 		long *lp, tmp;					/* XXX */
831 								/* XXX */
832 		lp = (long *)&((struct fs *)bp->b_data)->fs_qbmask; /* XXX */
833 		tmp = lp[4];					/* XXX */
834 		for (i = 4; i > 0; i--)				/* XXX */
835 			lp[i] = lp[i-1];			/* XXX */
836 		lp[0] = tmp;					/* XXX */
837 	}							/* XXX */
838 	if (waitfor == MNT_WAIT)
839 		error = bwrite(bp);
840 	else
841 		bawrite(bp);
842 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
843 	space = (caddr_t)fs->fs_csp[0];
844 	for (i = 0; i < blks; i += fs->fs_frag) {
845 		size = fs->fs_bsize;
846 		if (i + fs->fs_frag > blks)
847 			size = (blks - i) * fs->fs_fsize;
848 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
849 		    size, 0, 0);
850 		bcopy(space, bp->b_data, (u_int)size);
851 		space += size;
852 		if (waitfor == MNT_WAIT)
853 			error = bwrite(bp);
854 		else
855 			bawrite(bp);
856 	}
857 	return (error);
858 }
859