xref: /netbsd-src/sys/ufs/lfs/lfs_vfsops.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: lfs_vfsops.c,v 1.13 1997/06/11 10:10:04 bouyer Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)lfs_vfsops.c	8.10 (Berkeley) 11/21/94
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/proc.h>
42 #include <sys/kernel.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/buf.h>
46 #include <sys/mbuf.h>
47 #include <sys/file.h>
48 #include <sys/disklabel.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 
54 #include <miscfs/specfs/specdev.h>
55 
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60 
61 #include <ufs/lfs/lfs.h>
62 #include <ufs/lfs/lfs_extern.h>
63 
64 int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
65 
66 struct vfsops lfs_vfsops = {
67 	MOUNT_LFS,
68 	lfs_mount,
69 	ufs_start,
70 	lfs_unmount,
71 	ufs_root,
72 	ufs_quotactl,
73 	lfs_statfs,
74 	lfs_sync,
75 	lfs_vget,
76 	lfs_fhtovp,
77 	lfs_vptofh,
78 	lfs_init,
79 };
80 
81 int
82 lfs_mountroot()
83 {
84 	panic("lfs_mountroot");		/* XXX -- implement */
85 	return 0;
86 }
87 
88 /*
89  * VFS Operations.
90  *
91  * mount system call
92  */
93 int
94 lfs_mount(mp, path, data, ndp, p)
95 	register struct mount *mp;
96 	const char *path;
97 	void *data;
98 	struct nameidata *ndp;
99 	struct proc *p;
100 {
101 	struct vnode *devvp;
102 	struct ufs_args args;
103 	struct ufsmount *ump = NULL;
104 	register struct lfs *fs = NULL;				/* LFS */
105 	size_t size;
106 	int error;
107 	mode_t accessmode;
108 
109 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
110 	if (error)
111 		return (error);
112 
113 	/* Until LFS can do NFS right.		XXX */
114 	if (args.export.ex_flags & MNT_EXPORTED)
115 		return (EINVAL);
116 
117 	/*
118 	 * If updating, check whether changing from read-only to
119 	 * read/write; if there is no device name, that's all we do.
120 	 */
121 	if (mp->mnt_flag & MNT_UPDATE) {
122 		ump = VFSTOUFS(mp);
123 		if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
124 			/*
125 			 * If upgrade to read-write by non-root, then verify
126 			 * that user has necessary permissions on the device.
127 			 */
128 			if (p->p_ucred->cr_uid != 0) {
129 				VOP_LOCK(ump->um_devvp);
130 				error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
131 						   p->p_ucred, p);
132 				if (error) {
133 					VOP_UNLOCK(ump->um_devvp);
134 					return (error);
135 				}
136 				VOP_UNLOCK(ump->um_devvp);
137 			}
138 			fs->lfs_ronly = 0;
139 		}
140 		if (args.fspec == 0) {
141 			/*
142 			 * Process export requests.
143 			 */
144 			return (vfs_export(mp, &ump->um_export, &args.export));
145 		}
146 	}
147 	/*
148 	 * Not an update, or updating the name: look up the name
149 	 * and verify that it refers to a sensible block device.
150 	 */
151 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
152 	if ((error = namei(ndp)) != 0)
153 		return (error);
154 	devvp = ndp->ni_vp;
155 	if (devvp->v_type != VBLK) {
156 		vrele(devvp);
157 		return (ENOTBLK);
158 	}
159 	if (major(devvp->v_rdev) >= nblkdev) {
160 		vrele(devvp);
161 		return (ENXIO);
162 	}
163 	/*
164 	 * If mount by non-root, then verify that user has necessary
165 	 * permissions on the device.
166 	 */
167 	if (p->p_ucred->cr_uid != 0) {
168 		accessmode = VREAD;
169 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
170 			accessmode |= VWRITE;
171 		VOP_LOCK(devvp);
172 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
173 		if (error) {
174 			vput(devvp);
175 			return (error);
176 		}
177 		VOP_UNLOCK(devvp);
178 	}
179 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
180 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
181 	else {
182 		if (devvp != ump->um_devvp)
183 			error = EINVAL;	/* needs translation */
184 		else
185 			vrele(devvp);
186 	}
187 	if (error) {
188 		vrele(devvp);
189 		return (error);
190 	}
191 	ump = VFSTOUFS(mp);
192 	fs = ump->um_lfs;					/* LFS */
193 #ifdef NOTLFS							/* LFS */
194 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
195 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
196 	bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
197 #else
198 	(void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
199 	bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
200 	bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
201 #endif
202 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
203 	    &size);
204 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
205 	return (0);
206 }
207 
208 /*
209  * Common code for mount and mountroot
210  * LFS specific
211  */
212 int
213 lfs_mountfs(devvp, mp, p)
214 	register struct vnode *devvp;
215 	struct mount *mp;
216 	struct proc *p;
217 {
218 	extern struct vnode *rootvp;
219 	register struct lfs *fs;
220 	register struct ufsmount *ump;
221 	struct vnode *vp;
222 	struct buf *bp;
223 	struct partinfo dpart;
224 	dev_t dev;
225 	int error, i, ronly, size;
226 	struct ucred *cred;
227 
228 	cred = p ? p->p_ucred : NOCRED;
229 	/*
230 	 * Disallow multiple mounts of the same device.
231 	 * Disallow mounting of a device that is currently in use
232 	 * (except for root, which might share swap device for miniroot).
233 	 * Flush out any old buffers remaining from a previous use.
234 	 */
235 	if ((error = vfs_mountedon(devvp)) != 0)
236 		return (error);
237 	if (vcount(devvp) > 1 && devvp != rootvp)
238 		return (EBUSY);
239 	if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
240 		return (error);
241 
242 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
243 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
244 	if (error)
245 		return (error);
246 
247 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
248 		size = DEV_BSIZE;
249 	else {
250 		size = dpart.disklab->d_secsize;
251 #ifdef NEVER_USED
252 		dpart.part->p_fstype = FS_LFS;
253 		dpart.part->p_fsize = fs->lfs_fsize;	/* frag size */
254 		dpart.part->p_frag = fs->lfs_frag;	/* frags per block */
255 		dpart.part->p_cpg = fs->lfs_segshift;	/* segment shift */
256 #endif
257 	}
258 
259 	/* Don't free random space on error. */
260 	bp = NULL;
261 	ump = NULL;
262 
263 	/* Read in the superblock. */
264 	error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp);
265 	if (error)
266 		goto out;
267 	fs = (struct lfs *)bp->b_data;
268 
269 	/* Check the basics. */
270 	if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE ||
271 	    fs->lfs_bsize < sizeof(struct lfs)) {
272 		error = EINVAL;		/* XXX needs translation */
273 		goto out;
274 	}
275 
276 	/* Allocate the mount structure, copy the superblock into it. */
277 	ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
278 	fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
279 	bcopy(bp->b_data, fs, sizeof(struct lfs));
280 	if (sizeof(struct lfs) < LFS_SBPAD)			/* XXX why? */
281 		bp->b_flags |= B_INVAL;
282 	brelse(bp);
283 	bp = NULL;
284 
285 	/* Set up the I/O information */
286 	fs->lfs_iocount = 0;
287 
288 	/* Set up the ifile and lock aflags */
289 	fs->lfs_doifile = 0;
290 	fs->lfs_writer = 0;
291 	fs->lfs_dirops = 0;
292 	fs->lfs_seglock = 0;
293 
294 	/* Set the file system readonly/modify bits. */
295 	fs->lfs_ronly = ronly;
296 	if (ronly == 0)
297 		fs->lfs_fmod = 1;
298 
299 	/* Initialize the mount structure. */
300 	dev = devvp->v_rdev;
301 	mp->mnt_data = (qaddr_t)ump;
302 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
303 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
304 	mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
305 	mp->mnt_flag |= MNT_LOCAL;
306 	ump->um_mountp = mp;
307 	ump->um_dev = dev;
308 	ump->um_devvp = devvp;
309 	ump->um_bptrtodb = 0;
310 	ump->um_seqinc = 1 << fs->lfs_fsbtodb;
311 	ump->um_nindir = fs->lfs_nindir;
312 	for (i = 0; i < MAXQUOTAS; i++)
313 		ump->um_quotas[i] = NULLVP;
314 	devvp->v_specflags |= SI_MOUNTEDON;
315 
316 	/*
317 	 * We use the ifile vnode for almost every operation.  Instead of
318 	 * retrieving it from the hash table each time we retrieve it here,
319 	 * artificially increment the reference count and keep a pointer
320 	 * to it in the incore copy of the superblock.
321 	 */
322 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0)
323 		goto out;
324 	fs->lfs_ivnode = vp;
325 	VREF(vp);
326 	vput(vp);
327 
328 	return (0);
329 out:
330 	if (bp)
331 		brelse(bp);
332 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
333 	if (ump) {
334 		free(ump->um_lfs, M_UFSMNT);
335 		free(ump, M_UFSMNT);
336 		mp->mnt_data = (qaddr_t)0;
337 	}
338 	return (error);
339 }
340 
341 /*
342  * unmount system call
343  */
344 int
345 lfs_unmount(mp, mntflags, p)
346 	struct mount *mp;
347 	int mntflags;
348 	struct proc *p;
349 {
350 	register struct ufsmount *ump;
351 	register struct lfs *fs;
352 	int error, flags, ronly;
353 
354 	flags = 0;
355 	if (mntflags & MNT_FORCE)
356 		flags |= FORCECLOSE;
357 
358 	ump = VFSTOUFS(mp);
359 	fs = ump->um_lfs;
360 #ifdef QUOTA
361 	if (mp->mnt_flag & MNT_QUOTA) {
362 		int i;
363 		error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
364 		if (error)
365 			return (error);
366 		for (i = 0; i < MAXQUOTAS; i++) {
367 			if (ump->um_quotas[i] == NULLVP)
368 				continue;
369 			quotaoff(p, mp, i);
370 		}
371 		/*
372 		 * Here we fall through to vflush again to ensure
373 		 * that we have gotten rid of all the system vnodes.
374 		 */
375 	}
376 #endif
377 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
378 		return (error);
379 	fs->lfs_clean = 1;
380 	if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
381 		return (error);
382 	if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
383 		panic("lfs_unmount: still dirty blocks on ifile vnode\n");
384 	vrele(fs->lfs_ivnode);
385 	vgone(fs->lfs_ivnode);
386 
387 	ronly = !fs->lfs_ronly;
388 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
389 	error = VOP_CLOSE(ump->um_devvp,
390 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
391 	vrele(ump->um_devvp);
392 	free(fs, M_UFSMNT);
393 	free(ump, M_UFSMNT);
394 	mp->mnt_data = (qaddr_t)0;
395 	mp->mnt_flag &= ~MNT_LOCAL;
396 	return (error);
397 }
398 
399 /*
400  * Get file system statistics.
401  */
402 int
403 lfs_statfs(mp, sbp, p)
404 	struct mount *mp;
405 	register struct statfs *sbp;
406 	struct proc *p;
407 {
408 	register struct lfs *fs;
409 	register struct ufsmount *ump;
410 
411 	ump = VFSTOUFS(mp);
412 	fs = ump->um_lfs;
413 	if (fs->lfs_magic != LFS_MAGIC)
414 		panic("lfs_statfs: magic");
415 	sbp->f_type = 0;
416 	sbp->f_bsize = fs->lfs_bsize;
417 	sbp->f_iosize = fs->lfs_bsize;
418 	sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
419 	sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
420 	sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
421 		(fs->lfs_dsize - fs->lfs_bfree);
422 	sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
423 	sbp->f_files = fs->lfs_nfiles;
424 	sbp->f_ffree = sbp->f_bfree * INOPB(fs);
425 	if (sbp != &mp->mnt_stat) {
426 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
427 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
428 	}
429 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
430 	return (0);
431 }
432 
433 /*
434  * Go through the disk queues to initiate sandbagged IO;
435  * go through the inodes to write those that have been modified;
436  * initiate the writing of the super block if it has been modified.
437  *
438  * Note: we are always called with the filesystem marked `MPBUSY'.
439  */
440 int
441 lfs_sync(mp, waitfor, cred, p)
442 	struct mount *mp;
443 	int waitfor;
444 	struct ucred *cred;
445 	struct proc *p;
446 {
447 	int error;
448 
449 	/* All syncs must be checkpoints until roll-forward is implemented. */
450 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
451 #ifdef QUOTA
452 	qsync(mp);
453 #endif
454 	return (error);
455 }
456 
457 /*
458  * Look up an LFS dinode number to find its incore vnode.  If not already
459  * in core, read it in from the specified device.  Return the inode locked.
460  * Detection and handling of mount points must be done by the calling routine.
461  */
462 int
463 lfs_vget(mp, ino, vpp)
464 	struct mount *mp;
465 	ino_t ino;
466 	struct vnode **vpp;
467 {
468 	register struct lfs *fs;
469 	register struct inode *ip;
470 	struct buf *bp;
471 	struct ifile *ifp;
472 	struct vnode *vp;
473 	struct ufsmount *ump;
474 	daddr_t daddr;
475 	dev_t dev;
476 	int error;
477 
478 	ump = VFSTOUFS(mp);
479 	dev = ump->um_dev;
480 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
481 		return (0);
482 
483 	/* Translate the inode number to a disk address. */
484 	fs = ump->um_lfs;
485 	if (ino == LFS_IFILE_INUM)
486 		daddr = fs->lfs_idaddr;
487 	else {
488 		LFS_IENTRY(ifp, fs, ino, bp);
489 		daddr = ifp->if_daddr;
490 		brelse(bp);
491 		if (daddr == LFS_UNUSED_DADDR)
492 			return (ENOENT);
493 	}
494 
495 	/* Allocate new vnode/inode. */
496 	if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
497 		*vpp = NULL;
498 		return (error);
499 	}
500 
501 	/*
502 	 * Put it onto its hash chain and lock it so that other requests for
503 	 * this inode will block if they arrive while we are sleeping waiting
504 	 * for old data structures to be purged or for the contents of the
505 	 * disk portion of this inode to be read.
506 	 */
507 	ip = VTOI(vp);
508 	ufs_ihashins(ip);
509 
510 	/*
511 	 * XXX
512 	 * This may not need to be here, logically it should go down with
513 	 * the i_devvp initialization.
514 	 * Ask Kirk.
515 	 */
516 	ip->i_lfs = ump->um_lfs;
517 
518 	/* Read in the disk contents for the inode, copy into the inode. */
519 	error = bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp);
520 	if (error) {
521 		/*
522 		 * The inode does not contain anything useful, so it would
523 		 * be misleading to leave it on its hash chain. With mode
524 		 * still zero, it will be unlinked and returned to the free
525 		 * list by vput().
526 		 */
527 		vput(vp);
528 		brelse(bp);
529 		*vpp = NULL;
530 		return (error);
531 	}
532 	ip->i_din.ffs_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
533 	brelse(bp);
534 
535 	/*
536 	 * Initialize the vnode from the inode, check for aliases.  In all
537 	 * cases re-init ip, the underlying vnode/inode may have changed.
538 	 */
539 	error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp);
540 	if (error) {
541 		vput(vp);
542 		*vpp = NULL;
543 		return (error);
544 	}
545 	/*
546 	 * Finish inode initialization now that aliasing has been resolved.
547 	 */
548 	ip->i_devvp = ump->um_devvp;
549 	VREF(ip->i_devvp);
550 	*vpp = vp;
551 	return (0);
552 }
553 
554 /*
555  * File handle to vnode
556  *
557  * Have to be really careful about stale file handles:
558  * - check that the inode number is valid
559  * - call lfs_vget() to get the locked inode
560  * - check for an unallocated inode (i_mode == 0)
561  * - check that the given client host has export rights and return
562  *   those rights via. exflagsp and credanonp
563  *
564  * XXX
565  * use ifile to see if inode is allocated instead of reading off disk
566  * what is the relationship between my generational number and the NFS
567  * generational number.
568  */
569 int
570 lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
571 	register struct mount *mp;
572 	struct fid *fhp;
573 	struct mbuf *nam;
574 	struct vnode **vpp;
575 	int *exflagsp;
576 	struct ucred **credanonp;
577 {
578 	register struct ufid *ufhp;
579 
580 	ufhp = (struct ufid *)fhp;
581 	if (ufhp->ufid_ino < ROOTINO)
582 		return (ESTALE);
583 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
584 }
585 
586 /*
587  * Vnode pointer to File handle
588  */
589 /* ARGSUSED */
590 int
591 lfs_vptofh(vp, fhp)
592 	struct vnode *vp;
593 	struct fid *fhp;
594 {
595 	register struct inode *ip;
596 	register struct ufid *ufhp;
597 
598 	ip = VTOI(vp);
599 	ufhp = (struct ufid *)fhp;
600 	ufhp->ufid_len = sizeof(struct ufid);
601 	ufhp->ufid_ino = ip->i_number;
602 	ufhp->ufid_gen = ip->i_ffs_gen;
603 	return (0);
604 }
605