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