xref: /original-bsd/sys/ufs/lfs/lfs_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  *	@(#)lfs_vfsops.c	8.7 (Berkeley) 04/16/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 	ufs_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, V_SAVE, 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  * Get file system statistics.
347  */
348 lfs_statfs(mp, sbp, p)
349 	struct mount *mp;
350 	register struct statfs *sbp;
351 	struct proc *p;
352 {
353 	register struct lfs *fs;
354 	register struct ufsmount *ump;
355 
356 	ump = VFSTOUFS(mp);
357 	fs = ump->um_lfs;
358 	if (fs->lfs_magic != LFS_MAGIC)
359 		panic("lfs_statfs: magic");
360 	sbp->f_type = MOUNT_LFS;
361 	sbp->f_bsize = fs->lfs_bsize;
362 	sbp->f_iosize = fs->lfs_bsize;
363 	sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
364 	sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
365 	sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
366 		(fs->lfs_dsize - fs->lfs_bfree);
367 	sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
368 	sbp->f_files = fs->lfs_nfiles;
369 	sbp->f_ffree = sbp->f_bfree * INOPB(fs);
370 	if (sbp != &mp->mnt_stat) {
371 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
372 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
373 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
374 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
375 	}
376 	return (0);
377 }
378 
379 /*
380  * Go through the disk queues to initiate sandbagged IO;
381  * go through the inodes to write those that have been modified;
382  * initiate the writing of the super block if it has been modified.
383  *
384  * Note: we are always called with the filesystem marked `MPBUSY'.
385  */
386 lfs_sync(mp, waitfor, cred, p)
387 	struct mount *mp;
388 	int waitfor;
389 	struct ucred *cred;
390 	struct proc *p;
391 {
392 	int error;
393 
394 	/* All syncs must be checkpoints until roll-forward is implemented. */
395 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
396 #ifdef QUOTA
397 	qsync(mp);
398 #endif
399 	return (error);
400 }
401 
402 /*
403  * Look up an LFS dinode number to find its incore vnode.  If not already
404  * in core, read it in from the specified device.  Return the inode locked.
405  * Detection and handling of mount points must be done by the calling routine.
406  */
407 int
408 lfs_vget(mp, ino, vpp)
409 	struct mount *mp;
410 	ino_t ino;
411 	struct vnode **vpp;
412 {
413 	register struct lfs *fs;
414 	register struct inode *ip;
415 	struct buf *bp;
416 	struct ifile *ifp;
417 	struct vnode *vp;
418 	struct ufsmount *ump;
419 	daddr_t daddr;
420 	dev_t dev;
421 	int error;
422 
423 	ump = VFSTOUFS(mp);
424 	dev = ump->um_dev;
425 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
426 		return (0);
427 
428 	/* Translate the inode number to a disk address. */
429 	fs = ump->um_lfs;
430 	if (ino == LFS_IFILE_INUM)
431 		daddr = fs->lfs_idaddr;
432 	else {
433 		LFS_IENTRY(ifp, fs, ino, bp);
434 		daddr = ifp->if_daddr;
435 		brelse(bp);
436 		if (daddr == LFS_UNUSED_DADDR)
437 			return (ENOENT);
438 	}
439 
440 	/* Allocate new vnode/inode. */
441 	if (error = lfs_vcreate(mp, ino, &vp)) {
442 		*vpp = NULL;
443 		return (error);
444 	}
445 
446 	/*
447 	 * Put it onto its hash chain and lock it so that other requests for
448 	 * this inode will block if they arrive while we are sleeping waiting
449 	 * for old data structures to be purged or for the contents of the
450 	 * disk portion of this inode to be read.
451 	 */
452 	ip = VTOI(vp);
453 	ufs_ihashins(ip);
454 
455 	/*
456 	 * XXX
457 	 * This may not need to be here, logically it should go down with
458 	 * the i_devvp initialization.
459 	 * Ask Kirk.
460 	 */
461 	ip->i_lfs = ump->um_lfs;
462 
463 	/* Read in the disk contents for the inode, copy into the inode. */
464 	if (error =
465 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
466 		/*
467 		 * The inode does not contain anything useful, so it would
468 		 * be misleading to leave it on its hash chain. With mode
469 		 * still zero, it will be unlinked and returned to the free
470 		 * list by vput().
471 		 */
472 		vput(vp);
473 		brelse(bp);
474 		*vpp = NULL;
475 		return (error);
476 	}
477 	ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
478 	brelse(bp);
479 
480 	/*
481 	 * Initialize the vnode from the inode, check for aliases.  In all
482 	 * cases re-init ip, the underlying vnode/inode may have changed.
483 	 */
484 	if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
485 		vput(vp);
486 		*vpp = NULL;
487 		return (error);
488 	}
489 	/*
490 	 * Finish inode initialization now that aliasing has been resolved.
491 	 */
492 	ip->i_devvp = ump->um_devvp;
493 	VREF(ip->i_devvp);
494 	*vpp = vp;
495 	return (0);
496 }
497 
498 /*
499  * File handle to vnode
500  *
501  * Have to be really careful about stale file handles:
502  * - check that the inode number is valid
503  * - call lfs_vget() to get the locked inode
504  * - check for an unallocated inode (i_mode == 0)
505  * - check that the given client host has export rights and return
506  *   those rights via. exflagsp and credanonp
507  *
508  * XXX
509  * use ifile to see if inode is allocated instead of reading off disk
510  * what is the relationship between my generational number and the NFS
511  * generational number.
512  */
513 int
514 lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
515 	register struct mount *mp;
516 	struct fid *fhp;
517 	struct mbuf *nam;
518 	struct vnode **vpp;
519 	int *exflagsp;
520 	struct ucred **credanonp;
521 {
522 	register struct ufid *ufhp;
523 
524 	ufhp = (struct ufid *)fhp;
525 	if (ufhp->ufid_ino < ROOTINO)
526 		return (ESTALE);
527 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
528 }
529 
530 /*
531  * Vnode pointer to File handle
532  */
533 /* ARGSUSED */
534 lfs_vptofh(vp, fhp)
535 	struct vnode *vp;
536 	struct fid *fhp;
537 {
538 	register struct inode *ip;
539 	register struct ufid *ufhp;
540 
541 	ip = VTOI(vp);
542 	ufhp = (struct ufid *)fhp;
543 	ufhp->ufid_len = sizeof(struct ufid);
544 	ufhp->ufid_ino = ip->i_number;
545 	ufhp->ufid_gen = ip->i_gen;
546 	return (0);
547 }
548