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