xref: /original-bsd/sys/ufs/lfs/lfs_vfsops.c (revision 7e21a27b)
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.12 (Berkeley) 03/30/95
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 	lfs_sysctl,
51 };
52 
53 int
54 lfs_mountroot()
55 {
56 	panic("lfs_mountroot");		/* XXX -- implement */
57 }
58 
59 /*
60  * VFS Operations.
61  *
62  * mount system call
63  */
64 lfs_mount(mp, path, data, ndp, p)
65 	register struct mount *mp;
66 	char *path;
67 	caddr_t data;
68 	struct nameidata *ndp;
69 	struct proc *p;
70 {
71 	struct vnode *devvp;
72 	struct ufs_args args;
73 	struct ufsmount *ump;
74 	register struct lfs *fs;				/* LFS */
75 	u_int size;
76 	int error;
77 	mode_t accessmode;
78 
79 	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
80 		return (error);
81 
82 	/* Until LFS can do NFS right.		XXX */
83 	if (args.export.ex_flags & MNT_EXPORTED)
84 		return (EINVAL);
85 
86 	/*
87 	 * If updating, check whether changing from read-only to
88 	 * read/write; if there is no device name, that's all we do.
89 	 */
90 	if (mp->mnt_flag & MNT_UPDATE) {
91 		ump = VFSTOUFS(mp);
92 		if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
93 			/*
94 			 * If upgrade to read-write by non-root, then verify
95 			 * that user has necessary permissions on the device.
96 			 */
97 			if (p->p_ucred->cr_uid != 0) {
98 				VOP_LOCK(ump->um_devvp);
99 				if (error = VOP_ACCESS(ump->um_devvp,
100 				    VREAD | VWRITE, p->p_ucred, p)) {
101 					VOP_UNLOCK(ump->um_devvp);
102 					return (error);
103 				}
104 				VOP_UNLOCK(ump->um_devvp);
105 			}
106 			fs->lfs_ronly = 0;
107 		}
108 		if (args.fspec == 0) {
109 			/*
110 			 * Process export requests.
111 			 */
112 			return (vfs_export(mp, &ump->um_export, &args.export));
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 	/*
132 	 * If mount by non-root, then verify that user has necessary
133 	 * permissions on the device.
134 	 */
135 	if (p->p_ucred->cr_uid != 0) {
136 		accessmode = VREAD;
137 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
138 			accessmode |= VWRITE;
139 		VOP_LOCK(devvp);
140 		if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
141 			vput(devvp);
142 			return (error);
143 		}
144 		VOP_UNLOCK(devvp);
145 	}
146 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
147 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
148 	else {
149 		if (devvp != ump->um_devvp)
150 			error = EINVAL;	/* needs translation */
151 		else
152 			vrele(devvp);
153 	}
154 	if (error) {
155 		vrele(devvp);
156 		return (error);
157 	}
158 	ump = VFSTOUFS(mp);
159 	fs = ump->um_lfs;					/* LFS */
160 #ifdef NOTLFS							/* LFS */
161 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
162 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
163 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
164 	    MNAMELEN);
165 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
166 	    &size);
167 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
168 	(void) ufs_statfs(mp, &mp->mnt_stat, p);
169 #else
170 	(void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
171 	bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
172 	bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
173 	    MNAMELEN);
174 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
175 	    &size);
176 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
177 	(void) lfs_statfs(mp, &mp->mnt_stat, p);
178 #endif
179 	return (0);
180 }
181 
182 /*
183  * Common code for mount and mountroot
184  * LFS specific
185  */
186 int
187 lfs_mountfs(devvp, mp, p)
188 	register struct vnode *devvp;
189 	struct mount *mp;
190 	struct proc *p;
191 {
192 	extern struct vnode *rootvp;
193 	register struct lfs *fs;
194 	register struct ufsmount *ump;
195 	struct vnode *vp;
196 	struct buf *bp;
197 	struct partinfo dpart;
198 	dev_t dev;
199 	int error, i, ronly, size;
200 	struct ucred *cred;
201 
202 	cred = p ? p->p_ucred : NOCRED;
203 	/*
204 	 * Disallow multiple mounts of the same device.
205 	 * Disallow mounting of a device that is currently in use
206 	 * (except for root, which might share swap device for miniroot).
207 	 * Flush out any old buffers remaining from a previous use.
208 	 */
209 	if (error = vfs_mountedon(devvp))
210 		return (error);
211 	if (vcount(devvp) > 1 && devvp != rootvp)
212 		return (EBUSY);
213 	if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0))
214 		return (error);
215 
216 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
217 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
218 		return (error);
219 
220 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
221 		size = DEV_BSIZE;
222 	else {
223 		size = dpart.disklab->d_secsize;
224 #ifdef NEVER_USED
225 		dpart.part->p_fstype = FS_LFS;
226 		dpart.part->p_fsize = fs->lfs_fsize;	/* frag size */
227 		dpart.part->p_frag = fs->lfs_frag;	/* frags per block */
228 		dpart.part->p_cpg = fs->lfs_segshift;	/* segment shift */
229 #endif
230 	}
231 
232 	/* Don't free random space on error. */
233 	bp = NULL;
234 	ump = NULL;
235 
236 	/* Read in the superblock. */
237 	if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp))
238 		goto out;
239 	fs = (struct lfs *)bp->b_data;
240 
241 	/* Check the basics. */
242 	if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE ||
243 	    fs->lfs_bsize < sizeof(struct lfs)) {
244 		error = EINVAL;		/* XXX needs translation */
245 		goto out;
246 	}
247 
248 	/* Allocate the mount structure, copy the superblock into it. */
249 	ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
250 	fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
251 	bcopy(bp->b_data, fs, sizeof(struct lfs));
252 	if (sizeof(struct lfs) < LFS_SBPAD)			/* XXX why? */
253 		bp->b_flags |= B_INVAL;
254 	brelse(bp);
255 	bp = NULL;
256 
257 	/* Set up the I/O information */
258 	fs->lfs_iocount = 0;
259 
260 	/* Set up the ifile and lock aflags */
261 	fs->lfs_doifile = 0;
262 	fs->lfs_writer = 0;
263 	fs->lfs_dirops = 0;
264 	fs->lfs_seglock = 0;
265 
266 	/* Set the file system readonly/modify bits. */
267 	fs->lfs_ronly = ronly;
268 	if (ronly == 0)
269 		fs->lfs_fmod = 1;
270 
271 	/* Initialize the mount structure. */
272 	dev = devvp->v_rdev;
273 	mp->mnt_data = (qaddr_t)ump;
274 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
275 	mp->mnt_stat.f_fsid.val[1] = lfs_mount_type;
276 	mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
277 	mp->mnt_flag |= MNT_LOCAL;
278 	ump->um_mountp = mp;
279 	ump->um_dev = dev;
280 	ump->um_devvp = devvp;
281 	ump->um_bptrtodb = 0;
282 	ump->um_seqinc = 1 << fs->lfs_fsbtodb;
283 	ump->um_nindir = fs->lfs_nindir;
284 	for (i = 0; i < MAXQUOTAS; i++)
285 		ump->um_quotas[i] = NULLVP;
286 	devvp->v_specflags |= SI_MOUNTEDON;
287 
288 	/*
289 	 * We use the ifile vnode for almost every operation.  Instead of
290 	 * retrieving it from the hash table each time we retrieve it here,
291 	 * artificially increment the reference count and keep a pointer
292 	 * to it in the incore copy of the superblock.
293 	 */
294 	if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp))
295 		goto out;
296 	fs->lfs_ivnode = vp;
297 	VREF(vp);
298 	vput(vp);
299 
300 	return (0);
301 out:
302 	if (bp)
303 		brelse(bp);
304 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
305 	if (ump) {
306 		free(ump->um_lfs, M_UFSMNT);
307 		free(ump, M_UFSMNT);
308 		mp->mnt_data = (qaddr_t)0;
309 	}
310 	return (error);
311 }
312 
313 /*
314  * unmount system call
315  */
316 lfs_unmount(mp, mntflags, p)
317 	struct mount *mp;
318 	int mntflags;
319 	struct proc *p;
320 {
321 	extern int doforce;
322 	register struct ufsmount *ump;
323 	register struct lfs *fs;
324 	int i, error, flags, ronly;
325 
326 	flags = 0;
327 	if (mntflags & MNT_FORCE) {
328 		if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
329 			return (EINVAL);
330 		flags |= FORCECLOSE;
331 	}
332 
333 	ump = VFSTOUFS(mp);
334 	fs = ump->um_lfs;
335 #ifdef QUOTA
336 	if (mp->mnt_flag & MNT_QUOTA) {
337 		if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags))
338 			return (error);
339 		for (i = 0; i < MAXQUOTAS; i++) {
340 			if (ump->um_quotas[i] == NULLVP)
341 				continue;
342 			quotaoff(p, mp, i);
343 		}
344 		/*
345 		 * Here we fall through to vflush again to ensure
346 		 * that we have gotten rid of all the system vnodes.
347 		 */
348 	}
349 #endif
350 	if (error = vflush(mp, fs->lfs_ivnode, flags))
351 		return (error);
352 	fs->lfs_clean = 1;
353 	if (error = VFS_SYNC(mp, 1, p->p_ucred, p))
354 		return (error);
355 	if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
356 		panic("lfs_unmount: still dirty blocks on ifile vnode\n");
357 	vrele(fs->lfs_ivnode);
358 	vgone(fs->lfs_ivnode);
359 
360 	ronly = !fs->lfs_ronly;
361 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
362 	error = VOP_CLOSE(ump->um_devvp,
363 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
364 	vrele(ump->um_devvp);
365 	free(fs, M_UFSMNT);
366 	free(ump, M_UFSMNT);
367 	mp->mnt_data = (qaddr_t)0;
368 	mp->mnt_flag &= ~MNT_LOCAL;
369 	return (error);
370 }
371 
372 /*
373  * Get file system statistics.
374  */
375 lfs_statfs(mp, sbp, p)
376 	struct mount *mp;
377 	register struct statfs *sbp;
378 	struct proc *p;
379 {
380 	register struct lfs *fs;
381 	register struct ufsmount *ump;
382 
383 	ump = VFSTOUFS(mp);
384 	fs = ump->um_lfs;
385 	if (fs->lfs_magic != LFS_MAGIC)
386 		panic("lfs_statfs: magic");
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 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
398 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
399 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
400 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
401 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
402 	}
403 	return (0);
404 }
405 
406 /*
407  * Go through the disk queues to initiate sandbagged IO;
408  * go through the inodes to write those that have been modified;
409  * initiate the writing of the super block if it has been modified.
410  *
411  * Note: we are always called with the filesystem marked `MPBUSY'.
412  */
413 lfs_sync(mp, waitfor, cred, p)
414 	struct mount *mp;
415 	int waitfor;
416 	struct ucred *cred;
417 	struct proc *p;
418 {
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 	ufs_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 
576 /*
577  * Initialize the filesystem, most work done by ufs_init.
578  */
579 int lfs_mount_type;
580 
581 int
582 lfs_init(vfsp)
583 	struct vfsconf *vfsp;
584 {
585 
586 	lfs_mount_type = vfsp->vfc_typenum;
587 	return (ufs_init(vfsp));
588 }
589