xref: /original-bsd/sys/ufs/ffs/ffs_vfsops.c (revision 68d9582f)
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.66 (Berkeley) 06/04/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 	USES_VOP_CLOSE;
197 	USES_VOP_IOCTL;
198 	USES_VOP_OPEN;
199 	register struct ufsmount *ump = (struct ufsmount *)0;
200 	struct buf *bp = NULL;
201 	register struct fs *fs;
202 	dev_t dev = devvp->v_rdev;
203 	struct partinfo dpart;
204 	caddr_t base, space;
205 	int havepart = 0, blks;
206 	int error, i, size;
207 	int needclose = 0;
208 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
209 	extern struct vnode *rootvp;
210 
211 	/*
212 	 * Disallow multiple mounts of the same device.
213 	 * Disallow mounting of a device that is currently in use
214 	 * (except for root, which might share swap device for miniroot).
215 	 * Flush out any old buffers remaining from a previous use.
216 	 */
217 	if (error = ufs_mountedon(devvp))
218 		return (error);
219 	if (vcount(devvp) > 1 && devvp != rootvp)
220 		return (EBUSY);
221 	vinvalbuf(devvp, 1);
222 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p))
223 		return (error);
224 	needclose = 1;
225 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
226 		size = DEV_BSIZE;
227 	else {
228 		havepart = 1;
229 		size = dpart.disklab->d_secsize;
230 	}
231 	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
232 		goto out;
233 	fs = bp->b_un.b_fs;
234 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
235 	    fs->fs_bsize < sizeof(struct fs)) {
236 		error = EINVAL;		/* XXX needs translation */
237 		goto out;
238 	}
239 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
240 	bzero((caddr_t)ump, sizeof *ump);
241 	ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
242 	    M_WAITOK);
243 	bcopy((caddr_t)bp->b_un.b_addr, (caddr_t)ump->um_fs,
244 	   (u_int)fs->fs_sbsize);
245 	if (fs->fs_sbsize < SBSIZE)
246 		bp->b_flags |= B_INVAL;
247 	brelse(bp);
248 	bp = NULL;
249 	fs = ump->um_fs;
250 	fs->fs_ronly = ronly;
251 	if (ronly == 0)
252 		fs->fs_fmod = 1;
253 	if (havepart) {
254 		dpart.part->p_fstype = FS_BSDFFS;
255 		dpart.part->p_fsize = fs->fs_fsize;
256 		dpart.part->p_frag = fs->fs_frag;
257 		dpart.part->p_cpg = fs->fs_cpg;
258 	}
259 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
260 	base = space = malloc((u_long)fs->fs_cssize, M_UFSMNT,
261 	    M_WAITOK);
262 	for (i = 0; i < blks; i += fs->fs_frag) {
263 		size = fs->fs_bsize;
264 		if (i + fs->fs_frag > blks)
265 			size = (blks - i) * fs->fs_fsize;
266 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
267 			NOCRED, &bp);
268 		if (error) {
269 			free(base, M_UFSMNT);
270 			goto out;
271 		}
272 		bcopy((caddr_t)bp->b_un.b_addr, space, (u_int)size);
273 		fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
274 		space += size;
275 		brelse(bp);
276 		bp = NULL;
277 	}
278 	mp->mnt_data = (qaddr_t)ump;
279 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
280 	mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS;
281 	mp->mnt_flag |= MNT_LOCAL;
282 	ump->um_mountp = mp;
283 	ump->um_dev = dev;
284 	ump->um_devvp = devvp;
285 	for (i = 0; i < MAXQUOTAS; i++)
286 		ump->um_quotas[i] = NULLVP;
287 	devvp->v_specflags |= SI_MOUNTEDON;
288 
289 	/* Sanity checks for old file systems.			   XXX */
290 	fs->fs_npsect = MAX(fs->fs_npsect, fs->fs_nsect);	/* XXX */
291 	fs->fs_interleave = MAX(fs->fs_interleave, 1);		/* XXX */
292 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
293 		fs->fs_nrpos = 8;				/* XXX */
294 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
295 		quad_t sizepb = fs->fs_bsize;			/* XXX */
296 								/* XXX */
297 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
298 		for (i = 0; i < NIADDR; i++) {			/* XXX */
299 			sizepb *= NINDIR(fs);			/* XXX */
300 			fs->fs_maxfilesize += sizepb;		/* XXX */
301 		}						/* XXX */
302 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
303 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
304 	}							/* XXX */
305 	return (0);
306 out:
307 	if (bp)
308 		brelse(bp);
309 	if (needclose)
310 		(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
311 	if (ump) {
312 		free(ump->um_fs, M_UFSMNT);
313 		free(ump, M_UFSMNT);
314 		mp->mnt_data = (qaddr_t)0;
315 	}
316 	return (error);
317 }
318 
319 /*
320  * unmount system call
321  */
322 int
323 ffs_unmount(mp, mntflags, p)
324 	struct mount *mp;
325 	int mntflags;
326 	struct proc *p;
327 {
328 	USES_VOP_CLOSE;
329 	extern int doforce;
330 	register struct ufsmount *ump;
331 	register struct fs *fs;
332 	int i, error, ronly, flags = 0;
333 
334 	if (mntflags & MNT_FORCE) {
335 		if (!doforce || mp == rootfs)
336 			return (EINVAL);
337 		flags |= FORCECLOSE;
338 	}
339 	mntflushbuf(mp, 0);
340 	if (mntinvalbuf(mp))
341 		return (EBUSY);
342 	ump = VFSTOUFS(mp);
343 #ifdef QUOTA
344 	if (mp->mnt_flag & MNT_QUOTA) {
345 		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
346 			return (error);
347 		for (i = 0; i < MAXQUOTAS; i++) {
348 			if (ump->um_quotas[i] == NULLVP)
349 				continue;
350 			quotaoff(p, mp, i);
351 		}
352 		/*
353 		 * Here we fall through to vflush again to ensure
354 		 * that we have gotten rid of all the system vnodes.
355 		 */
356 	}
357 #endif
358 	if (error = vflush(mp, NULLVP, flags))
359 		return (error);
360 	fs = ump->um_fs;
361 	ronly = !fs->fs_ronly;
362 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
363 	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
364 		NOCRED, p);
365 	vrele(ump->um_devvp);
366 	free(fs->fs_csp[0], M_UFSMNT);
367 	free(fs, M_UFSMNT);
368 	free(ump, M_UFSMNT);
369 	mp->mnt_data = (qaddr_t)0;
370 	mp->mnt_flag &= ~MNT_LOCAL;
371 	return (error);
372 }
373 
374 /*
375  * Return root of a filesystem
376  */
377 int
378 ffs_root(mp, vpp)
379 	struct mount *mp;
380 	struct vnode **vpp;
381 {
382 	USES_VOP_VGET;
383 	struct vnode *nvp;
384 	int error;
385 
386 	if (error = FFS_VGET(mp, (ino_t)ROOTINO, &nvp))
387 		return (error);
388 	*vpp = nvp;
389 	return (0);
390 }
391 
392 /*
393  * Get file system statistics.
394  */
395 int
396 ffs_statfs(mp, sbp, p)
397 	struct mount *mp;
398 	register struct statfs *sbp;
399 	struct proc *p;
400 {
401 	register struct ufsmount *ump;
402 	register struct fs *fs;
403 
404 	ump = VFSTOUFS(mp);
405 	fs = ump->um_fs;
406 	if (fs->fs_magic != FS_MAGIC)
407 		panic("ffs_statfs");
408 	sbp->f_type = MOUNT_UFS;
409 	sbp->f_bsize = fs->fs_fsize;
410 	sbp->f_iosize = fs->fs_bsize;
411 	sbp->f_blocks = fs->fs_dsize;
412 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
413 		fs->fs_cstotal.cs_nffree;
414 	sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
415 		(fs->fs_dsize - sbp->f_bfree);
416 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
417 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
418 	if (sbp != &mp->mnt_stat) {
419 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
420 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
421 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
422 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
423 	}
424 	return (0);
425 }
426 
427 /*
428  * Go through the disk queues to initiate sandbagged IO;
429  * go through the inodes to write those that have been modified;
430  * initiate the writing of the super block if it has been modified.
431  *
432  * Note: we are always called with the filesystem marked `MPBUSY'.
433  */
434 int
435 ffs_sync(mp, waitfor)
436 	struct mount *mp;
437 	int waitfor;
438 {
439 	USES_VOP_ISLOCKED;
440 	USES_VOP_UPDATE;
441 	extern int syncprt;
442 	register struct vnode *vp;
443 	register struct inode *ip;
444 	register struct ufsmount *ump = VFSTOUFS(mp);
445 	register struct fs *fs;
446 	int error, allerror = 0;
447 
448 	if (syncprt)
449 		ufs_bufstats();
450 	fs = ump->um_fs;
451 	/*
452 	 * Write back modified superblock.
453 	 * Consistency check that the superblock
454 	 * is still in the buffer cache.
455 	 */
456 	if (fs->fs_fmod != 0) {
457 		if (fs->fs_ronly != 0) {		/* XXX */
458 			printf("fs = %s\n", fs->fs_fsmnt);
459 			panic("update: rofs mod");
460 		}
461 		fs->fs_fmod = 0;
462 		fs->fs_time = time.tv_sec;
463 		allerror = ffs_sbupdate(ump, waitfor);
464 	}
465 	/*
466 	 * Write back each (modified) inode.
467 	 */
468 loop:
469 	for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
470 		/*
471 		 * If the vnode that we are about to sync is no longer
472 		 * associated with this mount point, start over.
473 		 */
474 		if (vp->v_mount != mp)
475 			goto loop;
476 		if (VOP_ISLOCKED(vp))
477 			continue;
478 		ip = VTOI(vp);
479 		if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0 &&
480 		    vp->v_dirtyblkhd == NULL)
481 			continue;
482 		if (vget(vp))
483 			goto loop;
484 		if (vp->v_dirtyblkhd)
485 			vflushbuf(vp, 0);
486 		if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) &&
487 		    (error = VOP_UPDATE(vp, &time, &time, 0)))
488 			allerror = error;
489 		vput(vp);
490 	}
491 	/*
492 	 * Force stale file system control information to be flushed.
493 	 */
494 	vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0);
495 #ifdef QUOTA
496 	qsync(mp);
497 #endif
498 	return (allerror);
499 }
500 
501 /*
502  * File handle to vnode
503  *
504  * Have to be really careful about stale file handles:
505  * - check that the inode number is valid
506  * - call ffs_vget() to get the locked inode
507  * - check for an unallocated inode (i_mode == 0)
508  * - check that the generation number matches unless setgen true
509  */
510 int
511 ffs_fhtovp(mp, fhp, setgen, vpp)
512 	register struct mount *mp;
513 	struct fid *fhp;
514 	int setgen;
515 	struct vnode **vpp;
516 {
517 	USES_VOP_VGET;
518 	register struct inode *ip;
519 	register struct ufid *ufhp;
520 	struct fs *fs;
521 	struct vnode *nvp;
522 	int error;
523 
524 	ufhp = (struct ufid *)fhp;
525 	fs = VFSTOUFS(mp)->um_fs;
526 	if (ufhp->ufid_ino < ROOTINO ||
527 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
528 		return (EINVAL);
529 	if (error = FFS_VGET(mp, ufhp->ufid_ino, &nvp)) {
530 		*vpp = NULLVP;
531 		return (error);
532 	}
533 	ip = VTOI(nvp);
534 	if (ip->i_mode == 0) {
535 		ufs_iput(ip);
536 		*vpp = NULLVP;
537 		return (EINVAL);
538 	}
539 	if (ip->i_gen != ufhp->ufid_gen) {
540 		if (setgen)
541 			ufhp->ufid_gen = ip->i_gen;
542 		else {
543 			ufs_iput(ip);
544 			*vpp = NULLVP;
545 			return (EINVAL);
546 		}
547 	}
548 	*vpp = nvp;
549 	return (0);
550 }
551 
552 /*
553  * Vnode pointer to File handle
554  */
555 /* ARGSUSED */
556 ffs_vptofh(vp, fhp)
557 	struct vnode *vp;
558 	struct fid *fhp;
559 {
560 	register struct inode *ip;
561 	register struct ufid *ufhp;
562 
563 	ip = VTOI(vp);
564 	ufhp = (struct ufid *)fhp;
565 	ufhp->ufid_len = sizeof(struct ufid);
566 	ufhp->ufid_ino = ip->i_number;
567 	ufhp->ufid_gen = ip->i_gen;
568 	return (0);
569 }
570 
571 /*
572  * Write a superblock and associated information back to disk.
573  */
574 int
575 ffs_sbupdate(mp, waitfor)
576 	struct ufsmount *mp;
577 	int waitfor;
578 {
579 	register struct fs *fs = mp->um_fs;
580 	register struct buf *bp;
581 	int blks;
582 	caddr_t space;
583 	int i, size, error = 0;
584 
585 	bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize);
586 	bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize);
587 	/* Restore compatibility to old file systems.		   XXX */
588 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
589 		bp->b_un.b_fs->fs_nrpos = -1;			/* XXX */
590 	if (waitfor == MNT_WAIT)
591 		error = bwrite(bp);
592 	else
593 		bawrite(bp);
594 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
595 	space = (caddr_t)fs->fs_csp[0];
596 	for (i = 0; i < blks; i += fs->fs_frag) {
597 		size = fs->fs_bsize;
598 		if (i + fs->fs_frag > blks)
599 			size = (blks - i) * fs->fs_fsize;
600 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size);
601 		bcopy(space, bp->b_un.b_addr, (u_int)size);
602 		space += size;
603 		if (waitfor == MNT_WAIT)
604 			error = bwrite(bp);
605 		else
606 			bawrite(bp);
607 	}
608 	return (error);
609 }
610