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