xref: /original-bsd/sys/ufs/lfs/lfs_vfsops.c (revision 333da485)
1 /*
2  * Copyright (c) 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_vfsops.c	8.5 (Berkeley) 01/12/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/mount.h>
17 #include <sys/buf.h>
18 #include <sys/mbuf.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 #include <sys/socket.h>
25 
26 #include <miscfs/specfs/specdev.h>
27 
28 #include <ufs/ufs/quota.h>
29 #include <ufs/ufs/inode.h>
30 #include <ufs/ufs/ufsmount.h>
31 #include <ufs/ufs/ufs_extern.h>
32 
33 #include <ufs/lfs/lfs.h>
34 #include <ufs/lfs/lfs_extern.h>
35 
36 int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
37 
38 struct vfsops lfs_vfsops = {
39 	lfs_mount,
40 	ufs_start,
41 	lfs_unmount,
42 	lfs_root,
43 	ufs_quotactl,
44 	lfs_statfs,
45 	lfs_sync,
46 	lfs_vget,
47 	lfs_fhtovp,
48 	lfs_vptofh,
49 	lfs_init,
50 };
51 
52 int
53 lfs_mountroot()
54 {
55 	panic("lfs_mountroot");		/* XXX -- implement */
56 }
57 
58 /*
59  * VFS Operations.
60  *
61  * mount system call
62  */
63 lfs_mount(mp, path, data, ndp, p)
64 	register struct mount *mp;
65 	char *path;
66 	caddr_t data;
67 	struct nameidata *ndp;
68 	struct proc *p;
69 {
70 	struct vnode *devvp;
71 	struct ufs_args args;
72 	struct ufsmount *ump;
73 	register struct lfs *fs;				/* LFS */
74 	u_int size;
75 	int error;
76 
77 	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
78 		return (error);
79 
80 	/* Until LFS can do NFS right.		XXX */
81 	if (args.export.ex_flags & MNT_EXPORTED)
82 		return (EINVAL);
83 
84 	/*
85 	 * If updating, check whether changing from read-only to
86 	 * read/write; if there is no device name, that's all we do.
87 	 */
88 	if (mp->mnt_flag & MNT_UPDATE) {
89 		ump = VFSTOUFS(mp);
90 #ifdef NOTLFS							/* LFS */
91 		fs = ump->um_fs;
92 		if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0)
93 			fs->fs_ronly = 0;
94 #else
95 		fs = ump->um_lfs;
96 		if (fs->lfs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0)
97 			fs->lfs_ronly = 0;
98 #endif
99 		if (args.fspec == 0) {
100 			/*
101 			 * Process export requests.
102 			 */
103 			return (vfs_export(mp, &ump->um_export, &args.export));
104 		}
105 	}
106 	/*
107 	 * Not an update, or updating the name: look up the name
108 	 * and verify that it refers to a sensible block device.
109 	 */
110 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
111 	if (error = namei(ndp))
112 		return (error);
113 	devvp = ndp->ni_vp;
114 	if (devvp->v_type != VBLK) {
115 		vrele(devvp);
116 		return (ENOTBLK);
117 	}
118 	if (major(devvp->v_rdev) >= nblkdev) {
119 		vrele(devvp);
120 		return (ENXIO);
121 	}
122 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
123 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
124 	else {
125 		if (devvp != ump->um_devvp)
126 			error = EINVAL;	/* needs translation */
127 		else
128 			vrele(devvp);
129 	}
130 	if (error) {
131 		vrele(devvp);
132 		return (error);
133 	}
134 	ump = VFSTOUFS(mp);
135 	fs = ump->um_lfs;					/* LFS */
136 #ifdef NOTLFS							/* LFS */
137 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
138 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
139 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
140 	    MNAMELEN);
141 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
142 	    &size);
143 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
144 	(void) ufs_statfs(mp, &mp->mnt_stat, p);
145 #else
146 	(void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
147 	bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
148 	bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
149 	    MNAMELEN);
150 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
151 	    &size);
152 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
153 	(void) lfs_statfs(mp, &mp->mnt_stat, p);
154 #endif
155 	return (0);
156 }
157 
158 /*
159  * Common code for mount and mountroot
160  * LFS specific
161  */
162 int
163 lfs_mountfs(devvp, mp, p)
164 	register struct vnode *devvp;
165 	struct mount *mp;
166 	struct proc *p;
167 {
168 	extern struct vnode *rootvp;
169 	register struct lfs *fs;
170 	register struct ufsmount *ump;
171 	struct vnode *vp;
172 	struct buf *bp;
173 	struct partinfo dpart;
174 	dev_t dev;
175 	int error, i, ronly, size;
176 
177 	/*
178 	 * Disallow multiple mounts of the same device.
179 	 * Disallow mounting of a device that is currently in use
180 	 * (except for root, which might share swap device for miniroot).
181 	 * Flush out any old buffers remaining from a previous use.
182 	 */
183 	if (error = vfs_mountedon(devvp))
184 		return (error);
185 	if (vcount(devvp) > 1 && devvp != rootvp)
186 		return (EBUSY);
187 	if (error = vinvalbuf(devvp, 1, p->p_ucred, p, 0, 0))
188 		return (error);
189 
190 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
191 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
192 		return (error);
193 
194 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
195 		size = DEV_BSIZE;
196 	else {
197 		size = dpart.disklab->d_secsize;
198 #ifdef NEVER_USED
199 		dpart.part->p_fstype = FS_LFS;
200 		dpart.part->p_fsize = fs->lfs_fsize;	/* frag size */
201 		dpart.part->p_frag = fs->lfs_frag;	/* frags per block */
202 		dpart.part->p_cpg = fs->lfs_segshift;	/* segment shift */
203 #endif
204 	}
205 
206 	/* Don't free random space on error. */
207 	bp = NULL;
208 	ump = NULL;
209 
210 	/* Read in the superblock. */
211 	if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, NOCRED, &bp))
212 		goto out;
213 	fs = (struct lfs *)bp->b_data;
214 
215 	/* Check the basics. */
216 	if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE ||
217 	    fs->lfs_bsize < sizeof(struct lfs)) {
218 		error = EINVAL;		/* XXX needs translation */
219 		goto out;
220 	}
221 
222 	/* Allocate the mount structure, copy the superblock into it. */
223 	ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
224 	fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
225 	bcopy(bp->b_data, fs, sizeof(struct lfs));
226 	if (sizeof(struct lfs) < LFS_SBPAD)			/* XXX why? */
227 		bp->b_flags |= B_INVAL;
228 	brelse(bp);
229 	bp = NULL;
230 
231 	/* Set up the I/O information */
232 	fs->lfs_iocount = 0;
233 
234 	/* Set up the ifile and lock aflags */
235 	fs->lfs_doifile = 0;
236 	fs->lfs_writer = 0;
237 	fs->lfs_dirops = 0;
238 	fs->lfs_seglock = 0;
239 
240 	/* Set the file system readonly/modify bits. */
241 	fs->lfs_ronly = ronly;
242 	if (ronly == 0)
243 		fs->lfs_fmod = 1;
244 
245 	/* Initialize the mount structure. */
246 	dev = devvp->v_rdev;
247 	mp->mnt_data = (qaddr_t)ump;
248 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
249 	mp->mnt_stat.f_fsid.val[1] = MOUNT_LFS;
250 	mp->mnt_flag |= MNT_LOCAL;
251 	ump->um_mountp = mp;
252 	ump->um_dev = dev;
253 	ump->um_devvp = devvp;
254 	ump->um_bptrtodb = 0;
255 	ump->um_seqinc = 1 << fs->lfs_fsbtodb;
256 	ump->um_nindir = fs->lfs_nindir;
257 	for (i = 0; i < MAXQUOTAS; i++)
258 		ump->um_quotas[i] = NULLVP;
259 	devvp->v_specflags |= SI_MOUNTEDON;
260 
261 	/*
262 	 * We use the ifile vnode for almost every operation.  Instead of
263 	 * retrieving it from the hash table each time we retrieve it here,
264 	 * artificially increment the reference count and keep a pointer
265 	 * to it in the incore copy of the superblock.
266 	 */
267 	if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp))
268 		goto out;
269 	fs->lfs_ivnode = vp;
270 	VREF(vp);
271 	vput(vp);
272 
273 	return (0);
274 out:
275 	if (bp)
276 		brelse(bp);
277 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
278 	if (ump) {
279 		free(ump->um_lfs, M_UFSMNT);
280 		free(ump, M_UFSMNT);
281 		mp->mnt_data = (qaddr_t)0;
282 	}
283 	return (error);
284 }
285 
286 /*
287  * unmount system call
288  */
289 lfs_unmount(mp, mntflags, p)
290 	struct mount *mp;
291 	int mntflags;
292 	struct proc *p;
293 {
294 	extern int doforce;
295 	register struct ufsmount *ump;
296 	register struct lfs *fs;
297 	int i, error, flags, ronly;
298 
299 	flags = 0;
300 	if (mntflags & MNT_FORCE) {
301 		if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
302 			return (EINVAL);
303 		flags |= FORCECLOSE;
304 	}
305 
306 	ump = VFSTOUFS(mp);
307 	fs = ump->um_lfs;
308 #ifdef QUOTA
309 	if (mp->mnt_flag & MNT_QUOTA) {
310 		if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags))
311 			return (error);
312 		for (i = 0; i < MAXQUOTAS; i++) {
313 			if (ump->um_quotas[i] == NULLVP)
314 				continue;
315 			quotaoff(p, mp, i);
316 		}
317 		/*
318 		 * Here we fall through to vflush again to ensure
319 		 * that we have gotten rid of all the system vnodes.
320 		 */
321 	}
322 #endif
323 	if (error = vflush(mp, fs->lfs_ivnode, flags))
324 		return (error);
325 	fs->lfs_clean = 1;
326 	if (error = VFS_SYNC(mp, 1, p->p_ucred, p))
327 		return (error);
328 	if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
329 		panic("lfs_unmount: still dirty blocks on ifile vnode\n");
330 	vrele(fs->lfs_ivnode);
331 	vgone(fs->lfs_ivnode);
332 
333 	ronly = !fs->lfs_ronly;
334 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
335 	error = VOP_CLOSE(ump->um_devvp,
336 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
337 	vrele(ump->um_devvp);
338 	free(fs, M_UFSMNT);
339 	free(ump, M_UFSMNT);
340 	mp->mnt_data = (qaddr_t)0;
341 	mp->mnt_flag &= ~MNT_LOCAL;
342 	return (error);
343 }
344 
345 /*
346  * Return root of a filesystem
347  */
348 int
349 lfs_root(mp, vpp)
350 	struct mount *mp;
351 	struct vnode **vpp;
352 {
353 	struct vnode *nvp;
354 	int error;
355 
356 	if (error = VFS_VGET(mp, (ino_t)ROOTINO, &nvp))
357 		return (error);
358 	*vpp = nvp;
359 	return (0);
360 }
361 
362 /*
363  * Get file system statistics.
364  */
365 lfs_statfs(mp, sbp, p)
366 	struct mount *mp;
367 	register struct statfs *sbp;
368 	struct proc *p;
369 {
370 	register struct lfs *fs;
371 	register struct ufsmount *ump;
372 
373 	ump = VFSTOUFS(mp);
374 	fs = ump->um_lfs;
375 	if (fs->lfs_magic != LFS_MAGIC)
376 		panic("lfs_statfs: magic");
377 	sbp->f_type = MOUNT_LFS;
378 	sbp->f_bsize = fs->lfs_bsize;
379 	sbp->f_iosize = fs->lfs_bsize;
380 	sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
381 	sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
382 	sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
383 		(fs->lfs_dsize - fs->lfs_bfree);
384 	sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
385 	sbp->f_files = fs->lfs_nfiles;
386 	sbp->f_ffree = sbp->f_bfree * INOPB(fs);
387 	if (sbp != &mp->mnt_stat) {
388 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
389 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
390 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
391 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
392 	}
393 	return (0);
394 }
395 
396 /*
397  * Go through the disk queues to initiate sandbagged IO;
398  * go through the inodes to write those that have been modified;
399  * initiate the writing of the super block if it has been modified.
400  *
401  * Note: we are always called with the filesystem marked `MPBUSY'.
402  */
403 lfs_sync(mp, waitfor, cred, p)
404 	struct mount *mp;
405 	int waitfor;
406 	struct ucred *cred;
407 	struct proc *p;
408 {
409 	int error;
410 
411 	/* All syncs must be checkpoints until roll-forward is implemented. */
412 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
413 #ifdef QUOTA
414 	qsync(mp);
415 #endif
416 	return (error);
417 }
418 
419 /*
420  * Look up an LFS dinode number to find its incore vnode.  If not already
421  * in core, read it in from the specified device.  Return the inode locked.
422  * Detection and handling of mount points must be done by the calling routine.
423  */
424 int
425 lfs_vget(mp, ino, vpp)
426 	struct mount *mp;
427 	ino_t ino;
428 	struct vnode **vpp;
429 {
430 	register struct lfs *fs;
431 	register struct inode *ip;
432 	struct buf *bp;
433 	struct ifile *ifp;
434 	struct vnode *vp;
435 	struct ufsmount *ump;
436 	daddr_t daddr;
437 	dev_t dev;
438 	int error;
439 
440 	ump = VFSTOUFS(mp);
441 	dev = ump->um_dev;
442 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
443 		return (0);
444 
445 	/* Translate the inode number to a disk address. */
446 	fs = ump->um_lfs;
447 	if (ino == LFS_IFILE_INUM)
448 		daddr = fs->lfs_idaddr;
449 	else {
450 		LFS_IENTRY(ifp, fs, ino, bp);
451 		daddr = ifp->if_daddr;
452 		brelse(bp);
453 		if (daddr == LFS_UNUSED_DADDR)
454 			return (ENOENT);
455 	}
456 
457 	/* Allocate new vnode/inode. */
458 	if (error = lfs_vcreate(mp, ino, &vp)) {
459 		*vpp = NULL;
460 		return (error);
461 	}
462 
463 	/*
464 	 * Put it onto its hash chain and lock it so that other requests for
465 	 * this inode will block if they arrive while we are sleeping waiting
466 	 * for old data structures to be purged or for the contents of the
467 	 * disk portion of this inode to be read.
468 	 */
469 	ip = VTOI(vp);
470 	ufs_ihashins(ip);
471 
472 	/*
473 	 * XXX
474 	 * This may not need to be here, logically it should go down with
475 	 * the i_devvp initialization.
476 	 * Ask Kirk.
477 	 */
478 	ip->i_lfs = ump->um_lfs;
479 
480 	/* Read in the disk contents for the inode, copy into the inode. */
481 	if (error =
482 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
483 		/*
484 		 * The inode does not contain anything useful, so it would
485 		 * be misleading to leave it on its hash chain. With mode
486 		 * still zero, it will be unlinked and returned to the free
487 		 * list by vput().
488 		 */
489 		vput(vp);
490 		brelse(bp);
491 		*vpp = NULL;
492 		return (error);
493 	}
494 	ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
495 	brelse(bp);
496 
497 	/*
498 	 * Initialize the vnode from the inode, check for aliases.  In all
499 	 * cases re-init ip, the underlying vnode/inode may have changed.
500 	 */
501 	if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
502 		vput(vp);
503 		*vpp = NULL;
504 		return (error);
505 	}
506 	/*
507 	 * Finish inode initialization now that aliasing has been resolved.
508 	 */
509 	ip->i_devvp = ump->um_devvp;
510 	VREF(ip->i_devvp);
511 	*vpp = vp;
512 	return (0);
513 }
514 
515 /*
516  * File handle to vnode
517  *
518  * Have to be really careful about stale file handles:
519  * - check that the inode number is valid
520  * - call lfs_vget() to get the locked inode
521  * - check for an unallocated inode (i_mode == 0)
522  * - check that the given client host has export rights and return
523  *   those rights via. exflagsp and credanonp
524  *
525  * XXX
526  * use ifile to see if inode is allocated instead of reading off disk
527  * what is the relationship between my generational number and the NFS
528  * generational number.
529  */
530 int
531 lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
532 	register struct mount *mp;
533 	struct fid *fhp;
534 	struct mbuf *nam;
535 	struct vnode **vpp;
536 	int *exflagsp;
537 	struct ucred **credanonp;
538 {
539 	register struct ufid *ufhp;
540 
541 	ufhp = (struct ufid *)fhp;
542 	if (ufhp->ufid_ino < ROOTINO)
543 		return (ESTALE);
544 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
545 }
546 
547 /*
548  * Vnode pointer to File handle
549  */
550 /* ARGSUSED */
551 lfs_vptofh(vp, fhp)
552 	struct vnode *vp;
553 	struct fid *fhp;
554 {
555 	register struct inode *ip;
556 	register struct ufid *ufhp;
557 
558 	ip = VTOI(vp);
559 	ufhp = (struct ufid *)fhp;
560 	ufhp->ufid_len = sizeof(struct ufid);
561 	ufhp->ufid_ino = ip->i_number;
562 	ufhp->ufid_gen = ip->i_gen;
563 	return (0);
564 }
565