xref: /original-bsd/sys/ufs/ffs/ffs_vfsops.c (revision 753853ba)
1 /*
2  * Copyright (c) 1989, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ffs_vfsops.c	7.64 (Berkeley) 03/22/92
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/specdev.h>
17 #include <sys/mount.h>
18 #include <sys/buf.h>
19 #include <sys/file.h>
20 #include <sys/disklabel.h>
21 #include <sys/ioctl.h>
22 #include <sys/errno.h>
23 #include <sys/malloc.h>
24 
25 #include <ufs/ufs/quota.h>
26 #include <ufs/ufs/ufsmount.h>
27 #include <ufs/ufs/inode.h>
28 #include <ufs/ufs/ufs_extern.h>
29 
30 #include <ufs/ffs/fs.h>
31 #include <ufs/ffs/ffs_extern.h>
32 
33 int ffs_sbupdate __P((struct ufsmount *, int));
34 
35 struct vfsops ufs_vfsops = {
36 	ffs_mount,
37 	ufs_start,
38 	ffs_unmount,
39 	ffs_root,
40 	ufs_quotactl,
41 	ffs_statfs,
42 	ffs_sync,
43 	ffs_fhtovp,
44 	ffs_vptofh,
45 	ffs_init,
46 };
47 
48 /*
49  * Called by main() when ufs is going to be mounted as root.
50  *
51  * Name is updated by mount(8) after booting.
52  */
53 #define ROOTNAME	"root_device"
54 
55 ffs_mountroot()
56 {
57 	extern struct vnode *rootvp;
58 	register struct fs *fs;
59 	register struct mount *mp;
60 	struct proc *p = curproc;	/* XXX */
61 	struct ufsmount *ump;
62 	u_int size;
63 	int error;
64 
65 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
66 	mp->mnt_op = &ufs_vfsops;
67 	mp->mnt_flag = MNT_RDONLY;
68 	mp->mnt_mounth = NULLVP;
69 	if (error = ffs_mountfs(rootvp, mp, p)) {
70 		free(mp, M_MOUNT);
71 		return (error);
72 	}
73 	if (error = vfs_lock(mp)) {
74 		(void)ffs_unmount(mp, 0, p);
75 		free(mp, M_MOUNT);
76 		return (error);
77 	}
78 	rootfs = mp;
79 	mp->mnt_next = mp;
80 	mp->mnt_prev = mp;
81 	mp->mnt_vnodecovered = NULLVP;
82 	ump = VFSTOUFS(mp);
83 	fs = ump->um_fs;
84 	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
85 	fs->fs_fsmnt[0] = '/';
86 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
87 	    MNAMELEN);
88 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
89 	    &size);
90 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
91 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
92 	vfs_unlock(mp);
93 	inittodr(fs->fs_time);
94 	return (0);
95 }
96 
97 /*
98  * VFS Operations.
99  *
100  * mount system call
101  */
102 int
103 ffs_mount(mp, path, data, ndp, p)
104 	register struct mount *mp;
105 	char *path;
106 	caddr_t data;
107 	struct nameidata *ndp;
108 	struct proc *p;
109 {
110 	struct vnode *devvp;
111 	struct ufs_args args;
112 	struct ufsmount *ump;
113 	register struct fs *fs;
114 	u_int size;
115 	int error;
116 
117 	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
118 		return (error);
119 	/*
120 	 * If updating, check whether changing from read-only to
121 	 * read/write; if there is no device name, that's all we do.
122 	 */
123 	if (mp->mnt_flag & MNT_UPDATE) {
124 		ump = VFSTOUFS(mp);
125 		fs = ump->um_fs;
126 		if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0)
127 			fs->fs_ronly = 0;
128 		if (args.fspec == 0) {
129 			/*
130 			 * Process export requests.
131 			 */
132 			if (args.exflags & MNT_EXPORTED) {
133 				if (error = hang_addrlist(mp, &args))
134 					return (error);
135 				mp->mnt_flag |= MNT_EXPORTED;
136 			}
137 			if (args.exflags & MNT_DELEXPORT) {
138 				free_addrlist(ump);
139 				mp->mnt_flag &=
140 				    ~(MNT_EXPORTED | MNT_DEFEXPORTED);
141 			}
142 			return (0);
143 		}
144 	}
145 	/*
146 	 * Not an update, or updating the name: look up the name
147 	 * and verify that it refers to a sensible block device.
148 	 */
149 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
150 	if (error = namei(ndp))
151 		return (error);
152 	devvp = ndp->ni_vp;
153 
154 	if (devvp->v_type != VBLK) {
155 		vrele(devvp);
156 		return (ENOTBLK);
157 	}
158 	if (major(devvp->v_rdev) >= nblkdev) {
159 		vrele(devvp);
160 		return (ENXIO);
161 	}
162 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
163 		error = ffs_mountfs(devvp, mp, p);
164 	else {
165 		if (devvp != ump->um_devvp)
166 			error = EINVAL;	/* needs translation */
167 		else
168 			vrele(devvp);
169 	}
170 	if (error) {
171 		vrele(devvp);
172 		return (error);
173 	}
174 	ump = VFSTOUFS(mp);
175 	fs = ump->um_fs;
176 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
177 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
178 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
179 	    MNAMELEN);
180 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
181 	    &size);
182 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
183 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
184 	return (0);
185 }
186 
187 /*
188  * Common code for mount and mountroot
189  */
190 int
191 ffs_mountfs(devvp, mp, p)
192 	register struct vnode *devvp;
193 	struct mount *mp;
194 	struct proc *p;
195 {
196 	register struct ufsmount *ump = (struct ufsmount *)0;
197 	struct buf *bp = NULL;
198 	register struct fs *fs;
199 	dev_t dev = devvp->v_rdev;
200 	struct partinfo dpart;
201 	caddr_t base, space;
202 	int havepart = 0, blks;
203 	int error, i, size;
204 	int needclose = 0;
205 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
206 	extern struct vnode *rootvp;
207 
208 	/*
209 	 * Disallow multiple mounts of the same device.
210 	 * Disallow mounting of a device that is currently in use
211 	 * (except for root, which might share swap device for miniroot).
212 	 * Flush out any old buffers remaining from a previous use.
213 	 */
214 	if (error = ufs_mountedon(devvp))
215 		return (error);
216 	if (vcount(devvp) > 1 && devvp != rootvp)
217 		return (EBUSY);
218 	vinvalbuf(devvp, 1);
219 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p))
220 		return (error);
221 	needclose = 1;
222 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
223 		size = DEV_BSIZE;
224 	else {
225 		havepart = 1;
226 		size = dpart.disklab->d_secsize;
227 	}
228 	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
229 		goto out;
230 	fs = bp->b_un.b_fs;
231 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
232 	    fs->fs_bsize < sizeof(struct fs)) {
233 		error = EINVAL;		/* XXX needs translation */
234 		goto out;
235 	}
236 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
237 	bzero((caddr_t)ump, sizeof *ump);
238 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
239 	    M_WAITOK);
240 	bcopy((caddr_t)bp->b_un.b_addr, (caddr_t)ump->um_fs,
241 	   (u_int)fs->fs_sbsize);
242 	if (fs->fs_sbsize < SBSIZE)
243 		bp->b_flags |= B_INVAL;
244 	brelse(bp);
245 	bp = NULL;
246 	fs = ump->um_fs;
247 	fs->fs_ronly = ronly;
248 	if (ronly == 0)
249 		fs->fs_fmod = 1;
250 	if (havepart) {
251 		dpart.part->p_fstype = FS_BSDFFS;
252 		dpart.part->p_fsize = fs->fs_fsize;
253 		dpart.part->p_frag = fs->fs_frag;
254 		dpart.part->p_cpg = fs->fs_cpg;
255 	}
256 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
257 	base = space = malloc((u_long)fs->fs_cssize, M_UFSMNT,
258 	    M_WAITOK);
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 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
264 			NOCRED, &bp);
265 		if (error) {
266 			free(base, M_UFSMNT);
267 			goto out;
268 		}
269 		bcopy((caddr_t)bp->b_un.b_addr, space, (u_int)size);
270 		fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
271 		space += size;
272 		brelse(bp);
273 		bp = NULL;
274 	}
275 	mp->mnt_data = (qaddr_t)ump;
276 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
277 	mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS;
278 	mp->mnt_flag |= MNT_LOCAL;
279 	ump->um_mountp = mp;
280 	ump->um_dev = dev;
281 	ump->um_devvp = devvp;
282 	for (i = 0; i < MAXQUOTAS; i++)
283 		ump->um_quotas[i] = NULLVP;
284 	devvp->v_specflags |= SI_MOUNTEDON;
285 
286 	/* Sanity checks for old file systems.			   XXX */
287 	fs->fs_npsect = MAX(fs->fs_npsect, fs->fs_nsect);	/* XXX */
288 	fs->fs_interleave = MAX(fs->fs_interleave, 1);		/* XXX */
289 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
290 		fs->fs_nrpos = 8;				/* XXX */
291 	return (0);
292 out:
293 	if (bp)
294 		brelse(bp);
295 	if (needclose)
296 		(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
297 	if (ump) {
298 		free(ump->um_fs, M_UFSMNT);
299 		free(ump, M_UFSMNT);
300 		mp->mnt_data = (qaddr_t)0;
301 	}
302 	return (error);
303 }
304 
305 /*
306  * unmount system call
307  */
308 int
309 ffs_unmount(mp, mntflags, p)
310 	struct mount *mp;
311 	int mntflags;
312 	struct proc *p;
313 {
314 	extern int doforce;
315 	register struct ufsmount *ump;
316 	register struct fs *fs;
317 	int i, error, ronly, flags = 0;
318 
319 	if (mntflags & MNT_FORCE) {
320 		if (!doforce || mp == rootfs)
321 			return (EINVAL);
322 		flags |= FORCECLOSE;
323 	}
324 	mntflushbuf(mp, 0);
325 	if (mntinvalbuf(mp))
326 		return (EBUSY);
327 	ump = VFSTOUFS(mp);
328 #ifdef QUOTA
329 	if (mp->mnt_flag & MNT_QUOTA) {
330 		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
331 			return (error);
332 		for (i = 0; i < MAXQUOTAS; i++) {
333 			if (ump->um_quotas[i] == NULLVP)
334 				continue;
335 			quotaoff(p, mp, i);
336 		}
337 		/*
338 		 * Here we fall through to vflush again to ensure
339 		 * that we have gotten rid of all the system vnodes.
340 		 */
341 	}
342 #endif
343 	if (error = vflush(mp, NULLVP, flags))
344 		return (error);
345 	fs = ump->um_fs;
346 	ronly = !fs->fs_ronly;
347 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
348 	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
349 		NOCRED, p);
350 	vrele(ump->um_devvp);
351 	free(fs->fs_csp[0], M_UFSMNT);
352 	free(fs, M_UFSMNT);
353 	free(ump, M_UFSMNT);
354 	mp->mnt_data = (qaddr_t)0;
355 	mp->mnt_flag &= ~MNT_LOCAL;
356 	return (error);
357 }
358 
359 /*
360  * Return root of a filesystem
361  */
362 int
363 ffs_root(mp, vpp)
364 	struct mount *mp;
365 	struct vnode **vpp;
366 {
367 	struct vnode *nvp;
368 	int error;
369 
370 	if (error = ffs_vget(mp, (ino_t)ROOTINO, &nvp))
371 		return (error);
372 	*vpp = nvp;
373 	return (0);
374 }
375 
376 /*
377  * Get file system statistics.
378  */
379 int
380 ffs_statfs(mp, sbp, p)
381 	struct mount *mp;
382 	register struct statfs *sbp;
383 	struct proc *p;
384 {
385 	register struct ufsmount *ump;
386 	register struct fs *fs;
387 
388 	ump = VFSTOUFS(mp);
389 	fs = ump->um_fs;
390 	if (fs->fs_magic != FS_MAGIC)
391 		panic("ffs_statfs");
392 	sbp->f_type = MOUNT_UFS;
393 	sbp->f_bsize = fs->fs_fsize;
394 	sbp->f_iosize = fs->fs_bsize;
395 	sbp->f_blocks = fs->fs_dsize;
396 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
397 		fs->fs_cstotal.cs_nffree;
398 	sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
399 		(fs->fs_dsize - sbp->f_bfree);
400 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
401 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
402 	if (sbp != &mp->mnt_stat) {
403 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
404 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
405 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
406 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
407 	}
408 	return (0);
409 }
410 
411 /*
412  * Go through the disk queues to initiate sandbagged IO;
413  * go through the inodes to write those that have been modified;
414  * initiate the writing of the super block if it has been modified.
415  *
416  * Note: we are always called with the filesystem marked `MPBUSY'.
417  */
418 int
419 ffs_sync(mp, waitfor)
420 	struct mount *mp;
421 	int waitfor;
422 {
423 	extern int syncprt;
424 	register struct vnode *vp;
425 	register struct inode *ip;
426 	register struct ufsmount *ump = VFSTOUFS(mp);
427 	register struct fs *fs;
428 	int error, allerror = 0;
429 
430 	if (syncprt)
431 		ufs_bufstats();
432 	fs = ump->um_fs;
433 	/*
434 	 * Write back modified superblock.
435 	 * Consistency check that the superblock
436 	 * is still in the buffer cache.
437 	 */
438 	if (fs->fs_fmod != 0) {
439 		if (fs->fs_ronly != 0) {		/* XXX */
440 			printf("fs = %s\n", fs->fs_fsmnt);
441 			panic("update: rofs mod");
442 		}
443 		fs->fs_fmod = 0;
444 		fs->fs_time = time.tv_sec;
445 		allerror = ffs_sbupdate(ump, waitfor);
446 	}
447 	/*
448 	 * Write back each (modified) inode.
449 	 */
450 loop:
451 	for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
452 		/*
453 		 * If the vnode that we are about to sync is no longer
454 		 * associated with this mount point, start over.
455 		 */
456 		if (vp->v_mount != mp)
457 			goto loop;
458 		if (VOP_ISLOCKED(vp))
459 			continue;
460 		ip = VTOI(vp);
461 		if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0 &&
462 		    vp->v_dirtyblkhd == NULL)
463 			continue;
464 		if (vget(vp))
465 			goto loop;
466 		if (vp->v_dirtyblkhd)
467 			vflushbuf(vp, 0);
468 		if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) &&
469 		    (error = ffs_update(vp, &time, &time, 0)))
470 			allerror = error;
471 		vput(vp);
472 	}
473 	/*
474 	 * Force stale file system control information to be flushed.
475 	 */
476 	vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0);
477 #ifdef QUOTA
478 	qsync(mp);
479 #endif
480 	return (allerror);
481 }
482 
483 /*
484  * File handle to vnode
485  *
486  * Have to be really careful about stale file handles:
487  * - check that the inode number is valid
488  * - call ffs_vget() to get the locked inode
489  * - check for an unallocated inode (i_mode == 0)
490  * - check that the generation number matches unless setgen true
491  */
492 int
493 ffs_fhtovp(mp, fhp, setgen, vpp)
494 	register struct mount *mp;
495 	struct fid *fhp;
496 	int setgen;
497 	struct vnode **vpp;
498 {
499 	register struct inode *ip;
500 	register struct ufid *ufhp;
501 	struct fs *fs;
502 	struct vnode *nvp;
503 	int error;
504 
505 	ufhp = (struct ufid *)fhp;
506 	fs = VFSTOUFS(mp)->um_fs;
507 	if (ufhp->ufid_ino < ROOTINO ||
508 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
509 		return (EINVAL);
510 	if (error = ffs_vget(mp, ufhp->ufid_ino, &nvp)) {
511 		*vpp = NULLVP;
512 		return (error);
513 	}
514 	ip = VTOI(nvp);
515 	if (ip->i_mode == 0) {
516 		ufs_iput(ip);
517 		*vpp = NULLVP;
518 		return (EINVAL);
519 	}
520 	if (ip->i_gen != ufhp->ufid_gen) {
521 		if (setgen)
522 			ufhp->ufid_gen = ip->i_gen;
523 		else {
524 			ufs_iput(ip);
525 			*vpp = NULLVP;
526 			return (EINVAL);
527 		}
528 	}
529 	*vpp = nvp;
530 	return (0);
531 }
532 
533 /*
534  * Vnode pointer to File handle
535  */
536 /* ARGSUSED */
537 ffs_vptofh(vp, fhp)
538 	struct vnode *vp;
539 	struct fid *fhp;
540 {
541 	register struct inode *ip;
542 	register struct ufid *ufhp;
543 
544 	ip = VTOI(vp);
545 	ufhp = (struct ufid *)fhp;
546 	ufhp->ufid_len = sizeof(struct ufid);
547 	ufhp->ufid_ino = ip->i_number;
548 	ufhp->ufid_gen = ip->i_gen;
549 	return (0);
550 }
551 
552 /*
553  * Write a superblock and associated information back to disk.
554  */
555 int
556 ffs_sbupdate(mp, waitfor)
557 	struct ufsmount *mp;
558 	int waitfor;
559 {
560 	register struct fs *fs = mp->um_fs;
561 	register struct buf *bp;
562 	int blks;
563 	caddr_t space;
564 	int i, size, error = 0;
565 
566 	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize);
567 	bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize);
568 	/* Restore compatibility to old file systems.		   XXX */
569 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
570 		bp->b_un.b_fs->fs_nrpos = -1;			/* XXX */
571 	if (waitfor == MNT_WAIT)
572 		error = bwrite(bp);
573 	else
574 		bawrite(bp);
575 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
576 	space = (caddr_t)fs->fs_csp[0];
577 	for (i = 0; i < blks; i += fs->fs_frag) {
578 		size = fs->fs_bsize;
579 		if (i + fs->fs_frag > blks)
580 			size = (blks - i) * fs->fs_fsize;
581 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size);
582 		bcopy(space, bp->b_un.b_addr, (u_int)size);
583 		space += size;
584 		if (waitfor == MNT_WAIT)
585 			error = bwrite(bp);
586 		else
587 			bawrite(bp);
588 	}
589 	return (error);
590 }
591