xref: /original-bsd/sys/ufs/lfs/lfs_vfsops.c (revision b3c06cab)
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.14 (Berkeley) 05/08/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 	/*
392 	 * To compute the available space.  Subtract the minimum free
393 	 * from the total number of blocks in the file system.  Set avail
394 	 * to the smaller of this number and fs->lfs_bfree.
395 	 */
396 	sbp->f_bavail = fs->lfs_dsize * (100 - fs->lfs_minfree) / 100;
397 	sbp->f_bavail =
398 	    sbp->f_bavail > fs->lfs_bfree ? fs->lfs_bfree : sbp->f_bavail;
399 	sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
400 	sbp->f_files = fs->lfs_nfiles;
401 	sbp->f_ffree = sbp->f_bfree * INOPB(fs);
402 	if (sbp != &mp->mnt_stat) {
403 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
404 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
405 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
406 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
407 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
408 	}
409 	return (0);
410 }
411 
412 /*
413  * Go through the disk queues to initiate sandbagged IO;
414  * go through the inodes to write those that have been modified;
415  * initiate the writing of the super block if it has been modified.
416  *
417  * Note: we are always called with the filesystem marked `MPBUSY'.
418  */
419 lfs_sync(mp, waitfor, cred, p)
420 	struct mount *mp;
421 	int waitfor;
422 	struct ucred *cred;
423 	struct proc *p;
424 {
425 	int error;
426 
427 	/* All syncs must be checkpoints until roll-forward is implemented. */
428 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
429 #ifdef QUOTA
430 	qsync(mp);
431 #endif
432 	return (error);
433 }
434 
435 /*
436  * Look up an LFS dinode number to find its incore vnode.  If not already
437  * in core, read it in from the specified device.  Return the inode locked.
438  * Detection and handling of mount points must be done by the calling routine.
439  */
440 int
441 lfs_vget(mp, ino, vpp)
442 	struct mount *mp;
443 	ino_t ino;
444 	struct vnode **vpp;
445 {
446 	register struct lfs *fs;
447 	register struct inode *ip;
448 	struct buf *bp;
449 	struct ifile *ifp;
450 	struct vnode *vp;
451 	struct ufsmount *ump;
452 	ufs_daddr_t daddr;
453 	dev_t dev;
454 	int error;
455 
456 	ump = VFSTOUFS(mp);
457 	dev = ump->um_dev;
458 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
459 		return (0);
460 
461 	/* Translate the inode number to a disk address. */
462 	fs = ump->um_lfs;
463 	if (ino == LFS_IFILE_INUM)
464 		daddr = fs->lfs_idaddr;
465 	else {
466 		LFS_IENTRY(ifp, fs, ino, bp);
467 		daddr = ifp->if_daddr;
468 		brelse(bp);
469 		if (daddr == LFS_UNUSED_DADDR)
470 			return (ENOENT);
471 	}
472 
473 	/* Allocate new vnode/inode. */
474 	if (error = lfs_vcreate(mp, ino, &vp)) {
475 		*vpp = NULL;
476 		return (error);
477 	}
478 
479 	/*
480 	 * Put it onto its hash chain and lock it so that other requests for
481 	 * this inode will block if they arrive while we are sleeping waiting
482 	 * for old data structures to be purged or for the contents of the
483 	 * disk portion of this inode to be read.
484 	 */
485 	ip = VTOI(vp);
486 	ufs_ihashins(ip);
487 
488 	/*
489 	 * XXX
490 	 * This may not need to be here, logically it should go down with
491 	 * the i_devvp initialization.
492 	 * Ask Kirk.
493 	 */
494 	ip->i_lfs = ump->um_lfs;
495 
496 	/* Read in the disk contents for the inode, copy into the inode. */
497 	if (error =
498 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
499 		/*
500 		 * The inode does not contain anything useful, so it would
501 		 * be misleading to leave it on its hash chain. With mode
502 		 * still zero, it will be unlinked and returned to the free
503 		 * list by vput().
504 		 */
505 		vput(vp);
506 		brelse(bp);
507 		*vpp = NULL;
508 		return (error);
509 	}
510 	ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
511 	brelse(bp);
512 
513 	/*
514 	 * Initialize the vnode from the inode, check for aliases.  In all
515 	 * cases re-init ip, the underlying vnode/inode may have changed.
516 	 */
517 	if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
518 		vput(vp);
519 		*vpp = NULL;
520 		return (error);
521 	}
522 	/*
523 	 * Finish inode initialization now that aliasing has been resolved.
524 	 */
525 	ip->i_devvp = ump->um_devvp;
526 	VREF(ip->i_devvp);
527 	*vpp = vp;
528 	return (0);
529 }
530 
531 /*
532  * File handle to vnode
533  *
534  * Have to be really careful about stale file handles:
535  * - check that the inode number is valid
536  * - call lfs_vget() to get the locked inode
537  * - check for an unallocated inode (i_mode == 0)
538  * - check that the given client host has export rights and return
539  *   those rights via. exflagsp and credanonp
540  *
541  * XXX
542  * use ifile to see if inode is allocated instead of reading off disk
543  * what is the relationship between my generational number and the NFS
544  * generational number.
545  */
546 int
547 lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
548 	register struct mount *mp;
549 	struct fid *fhp;
550 	struct mbuf *nam;
551 	struct vnode **vpp;
552 	int *exflagsp;
553 	struct ucred **credanonp;
554 {
555 	register struct ufid *ufhp;
556 
557 	ufhp = (struct ufid *)fhp;
558 	if (ufhp->ufid_ino < ROOTINO)
559 		return (ESTALE);
560 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
561 }
562 
563 /*
564  * Vnode pointer to File handle
565  */
566 /* ARGSUSED */
567 lfs_vptofh(vp, fhp)
568 	struct vnode *vp;
569 	struct fid *fhp;
570 {
571 	register struct inode *ip;
572 	register struct ufid *ufhp;
573 
574 	ip = VTOI(vp);
575 	ufhp = (struct ufid *)fhp;
576 	ufhp->ufid_len = sizeof(struct ufid);
577 	ufhp->ufid_ino = ip->i_number;
578 	ufhp->ufid_gen = ip->i_gen;
579 	return (0);
580 }
581 
582 /*
583  * Initialize the filesystem, most work done by ufs_init.
584  */
585 int lfs_mount_type;
586 
587 int
588 lfs_init(vfsp)
589 	struct vfsconf *vfsp;
590 {
591 
592 	lfs_mount_type = vfsp->vfc_typenum;
593 	return (ufs_init(vfsp));
594 }
595