xref: /netbsd/sys/kern/vfs_vnops.c (revision bf9ec67e)
1 /*	$NetBSD: vfs_vnops.c,v 1.54 2002/03/17 19:41:08 atatat Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.54 2002/03/17 19:41:08 atatat Exp $");
45 
46 #include "fs_union.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/buf.h>
54 #include <sys/proc.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/ioctl.h>
59 #include <sys/tty.h>
60 #include <sys/poll.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #ifdef UNION
65 #include <miscfs/union/union.h>
66 #endif
67 
68 static int  vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
69 
70 struct 	fileops vnops = {
71 	vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
72 	vn_statfile, vn_closefile
73 };
74 
75 /*
76  * Common code for vnode open operations.
77  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
78  */
79 int
80 vn_open(ndp, fmode, cmode)
81 	struct nameidata *ndp;
82 	int fmode, cmode;
83 {
84 	struct vnode *vp;
85 	struct proc *p = ndp->ni_cnd.cn_proc;
86 	struct ucred *cred = p->p_ucred;
87 	struct vattr va;
88 	int error;
89 
90 	if (fmode & O_CREAT) {
91 		ndp->ni_cnd.cn_nameiop = CREATE;
92 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
93 		if ((fmode & O_EXCL) == 0 &&
94 		    ((fmode & FNOSYMLINK) == 0))
95 			ndp->ni_cnd.cn_flags |= FOLLOW;
96 		if ((error = namei(ndp)) != 0)
97 			return (error);
98 		if (ndp->ni_vp == NULL) {
99 			VATTR_NULL(&va);
100 			va.va_type = VREG;
101 			va.va_mode = cmode;
102 			if (fmode & O_EXCL)
103 				 va.va_vaflags |= VA_EXCLUSIVE;
104 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
105 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
106 					   &ndp->ni_cnd, &va);
107 			if (error)
108 				return (error);
109 			fmode &= ~O_TRUNC;
110 			vp = ndp->ni_vp;
111 		} else {
112 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
113 			if (ndp->ni_dvp == ndp->ni_vp)
114 				vrele(ndp->ni_dvp);
115 			else
116 				vput(ndp->ni_dvp);
117 			ndp->ni_dvp = NULL;
118 			vp = ndp->ni_vp;
119 			if (fmode & O_EXCL) {
120 				error = EEXIST;
121 				goto bad;
122 			}
123 			if (ndp->ni_vp->v_type == VLNK) {
124 				error = EFTYPE;
125 				goto bad;
126 			}
127 			fmode &= ~O_CREAT;
128 		}
129 	} else {
130 		ndp->ni_cnd.cn_nameiop = LOOKUP;
131 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
132 		if ((error = namei(ndp)) != 0)
133 			return (error);
134 		vp = ndp->ni_vp;
135 	}
136 	if (vp->v_type == VSOCK) {
137 		error = EOPNOTSUPP;
138 		goto bad;
139 	}
140 	if ((fmode & O_CREAT) == 0) {
141 		if (fmode & FREAD) {
142 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
143 				goto bad;
144 		}
145 		if (fmode & (FWRITE | O_TRUNC)) {
146 			if (vp->v_type == VDIR) {
147 				error = EISDIR;
148 				goto bad;
149 			}
150 			if ((error = vn_writechk(vp)) != 0 ||
151 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
152 				goto bad;
153 		}
154 	}
155 	if (fmode & O_TRUNC) {
156 		VOP_UNLOCK(vp, 0);			/* XXX */
157 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
158 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
159 		VATTR_NULL(&va);
160 		va.va_size = 0;
161 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
162 			goto bad;
163 	}
164 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
165 		goto bad;
166 	if (vp->v_type == VREG &&
167 	    uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
168 		error = EIO;
169 		goto bad;
170 	}
171 	if (fmode & FWRITE)
172 		vp->v_writecount++;
173 
174 	return (0);
175 bad:
176 	vput(vp);
177 	return (error);
178 }
179 
180 /*
181  * Check for write permissions on the specified vnode.
182  * Prototype text segments cannot be written.
183  */
184 int
185 vn_writechk(vp)
186 	struct vnode *vp;
187 {
188 
189 	/*
190 	 * If the vnode is in use as a process's text,
191 	 * we can't allow writing.
192 	 */
193 	if (vp->v_flag & VTEXT)
194 		return (ETXTBSY);
195 	return (0);
196 }
197 
198 /*
199  * Mark a vnode as having executable mappings.
200  */
201 void
202 vn_markexec(vp)
203 	struct vnode *vp;
204 {
205 	if ((vp->v_flag & VEXECMAP) == 0) {
206 		uvmexp.filepages -= vp->v_uobj.uo_npages;
207 		uvmexp.execpages += vp->v_uobj.uo_npages;
208 	}
209 	vp->v_flag |= VEXECMAP;
210 }
211 
212 /*
213  * Vnode close call
214  *
215  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
216  */
217 int
218 vn_close(vp, flags, cred, p)
219 	struct vnode *vp;
220 	int flags;
221 	struct ucred *cred;
222 	struct proc *p;
223 {
224 	int error;
225 
226 	if (flags & FWRITE)
227 		vp->v_writecount--;
228 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
229 	error = VOP_CLOSE(vp, flags, cred, p);
230 	vput(vp);
231 	return (error);
232 }
233 
234 /*
235  * Package up an I/O request on a vnode into a uio and do it.
236  */
237 int
238 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
239 	enum uio_rw rw;
240 	struct vnode *vp;
241 	caddr_t base;
242 	int len;
243 	off_t offset;
244 	enum uio_seg segflg;
245 	int ioflg;
246 	struct ucred *cred;
247 	size_t *aresid;
248 	struct proc *p;
249 {
250 	struct uio auio;
251 	struct iovec aiov;
252 	int error;
253 
254 	if ((ioflg & IO_NODELOCKED) == 0) {
255 		if (rw == UIO_READ) {
256 			vn_lock(vp, LK_SHARED | LK_RETRY);
257 		} else {
258 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
259 		}
260 	}
261 	auio.uio_iov = &aiov;
262 	auio.uio_iovcnt = 1;
263 	aiov.iov_base = base;
264 	aiov.iov_len = len;
265 	auio.uio_resid = len;
266 	auio.uio_offset = offset;
267 	auio.uio_segflg = segflg;
268 	auio.uio_rw = rw;
269 	auio.uio_procp = p;
270 	if (rw == UIO_READ) {
271 		error = VOP_READ(vp, &auio, ioflg, cred);
272 	} else {
273 		error = VOP_WRITE(vp, &auio, ioflg, cred);
274 	}
275 	if (aresid)
276 		*aresid = auio.uio_resid;
277 	else
278 		if (auio.uio_resid && error == 0)
279 			error = EIO;
280 	if ((ioflg & IO_NODELOCKED) == 0)
281 		VOP_UNLOCK(vp, 0);
282 	return (error);
283 }
284 
285 int
286 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
287 	struct file *fp;
288 	char *buf;
289 	int segflg, *done, *ncookies;
290 	u_int count;
291 	struct proc *p;
292 	off_t **cookies;
293 {
294 	struct vnode *vp = (struct vnode *)fp->f_data;
295 	struct iovec aiov;
296 	struct uio auio;
297 	int error, eofflag;
298 
299 unionread:
300 	if (vp->v_type != VDIR)
301 		return (EINVAL);
302 	aiov.iov_base = buf;
303 	aiov.iov_len = count;
304 	auio.uio_iov = &aiov;
305 	auio.uio_iovcnt = 1;
306 	auio.uio_rw = UIO_READ;
307 	auio.uio_segflg = segflg;
308 	auio.uio_procp = p;
309 	auio.uio_resid = count;
310 	vn_lock(vp, LK_SHARED | LK_RETRY);
311 	auio.uio_offset = fp->f_offset;
312 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
313 		    ncookies);
314 	fp->f_offset = auio.uio_offset;
315 	VOP_UNLOCK(vp, 0);
316 	if (error)
317 		return (error);
318 
319 #ifdef UNION
320 {
321 	extern struct vnode *union_dircache __P((struct vnode *));
322 
323 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
324 		struct vnode *lvp;
325 
326 		lvp = union_dircache(vp);
327 		if (lvp != NULLVP) {
328 			struct vattr va;
329 
330 			/*
331 			 * If the directory is opaque,
332 			 * then don't show lower entries
333 			 */
334 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
335 			if (va.va_flags & OPAQUE) {
336 				vput(lvp);
337 				lvp = NULL;
338 			}
339 		}
340 
341 		if (lvp != NULLVP) {
342 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
343 			if (error) {
344 				vput(lvp);
345 				return (error);
346 			}
347 			VOP_UNLOCK(lvp, 0);
348 			fp->f_data = (caddr_t) lvp;
349 			fp->f_offset = 0;
350 			error = vn_close(vp, FREAD, fp->f_cred, p);
351 			if (error)
352 				return (error);
353 			vp = lvp;
354 			goto unionread;
355 		}
356 	}
357 }
358 #endif /* UNION */
359 
360 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
361 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
362 		struct vnode *tvp = vp;
363 		vp = vp->v_mount->mnt_vnodecovered;
364 		VREF(vp);
365 		fp->f_data = (caddr_t) vp;
366 		fp->f_offset = 0;
367 		vrele(tvp);
368 		goto unionread;
369 	}
370 	*done = count - auio.uio_resid;
371 	return error;
372 }
373 
374 /*
375  * File table vnode read routine.
376  */
377 int
378 vn_read(fp, offset, uio, cred, flags)
379 	struct file *fp;
380 	off_t *offset;
381 	struct uio *uio;
382 	struct ucred *cred;
383 	int flags;
384 {
385 	struct vnode *vp = (struct vnode *)fp->f_data;
386 	int count, error, ioflag = 0;
387 
388 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
389 	if (fp->f_flag & FNONBLOCK)
390 		ioflag |= IO_NDELAY;
391 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
392 		ioflag |= IO_SYNC;
393 	if (fp->f_flag & FALTIO)
394 		ioflag |= IO_ALTSEMANTICS;
395 	vn_lock(vp, LK_SHARED | LK_RETRY);
396 	uio->uio_offset = *offset;
397 	count = uio->uio_resid;
398 	error = VOP_READ(vp, uio, ioflag, cred);
399 	if (flags & FOF_UPDATE_OFFSET)
400 		*offset += count - uio->uio_resid;
401 	VOP_UNLOCK(vp, 0);
402 	return (error);
403 }
404 
405 /*
406  * File table vnode write routine.
407  */
408 int
409 vn_write(fp, offset, uio, cred, flags)
410 	struct file *fp;
411 	off_t *offset;
412 	struct uio *uio;
413 	struct ucred *cred;
414 	int flags;
415 {
416 	struct vnode *vp = (struct vnode *)fp->f_data;
417 	int count, error, ioflag = IO_UNIT;
418 
419 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
420 		ioflag |= IO_APPEND;
421 	if (fp->f_flag & FNONBLOCK)
422 		ioflag |= IO_NDELAY;
423 	if (fp->f_flag & FFSYNC ||
424 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
425 		ioflag |= IO_SYNC;
426 	else if (fp->f_flag & FDSYNC)
427 		ioflag |= IO_DSYNC;
428 	if (fp->f_flag & FALTIO)
429 		ioflag |= IO_ALTSEMANTICS;
430 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
431 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
432 	uio->uio_offset = *offset;
433 	count = uio->uio_resid;
434 	error = VOP_WRITE(vp, uio, ioflag, cred);
435 	if (flags & FOF_UPDATE_OFFSET) {
436 		if (ioflag & IO_APPEND)
437 			*offset = uio->uio_offset;
438 		else
439 			*offset += count - uio->uio_resid;
440 	}
441 	VOP_UNLOCK(vp, 0);
442 	return (error);
443 }
444 
445 /*
446  * File table vnode stat routine.
447  */
448 static int
449 vn_statfile(fp, sb, p)
450 	struct file *fp;
451 	struct stat *sb;
452 	struct proc *p;
453 {
454 	struct vnode *vp = (struct vnode *)fp->f_data;
455 
456 	return vn_stat(vp, sb, p);
457 }
458 
459 int
460 vn_stat(fdata, sb, p)
461 	void *fdata;
462 	struct stat *sb;
463 	struct proc *p;
464 {
465 	struct vnode *vp = fdata;
466 	struct vattr va;
467 	int error;
468 	mode_t mode;
469 
470 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
471 	if (error)
472 		return (error);
473 	/*
474 	 * Copy from vattr table
475 	 */
476 	sb->st_dev = va.va_fsid;
477 	sb->st_ino = va.va_fileid;
478 	mode = va.va_mode;
479 	switch (vp->v_type) {
480 	case VREG:
481 		mode |= S_IFREG;
482 		break;
483 	case VDIR:
484 		mode |= S_IFDIR;
485 		break;
486 	case VBLK:
487 		mode |= S_IFBLK;
488 		break;
489 	case VCHR:
490 		mode |= S_IFCHR;
491 		break;
492 	case VLNK:
493 		mode |= S_IFLNK;
494 		break;
495 	case VSOCK:
496 		mode |= S_IFSOCK;
497 		break;
498 	case VFIFO:
499 		mode |= S_IFIFO;
500 		break;
501 	default:
502 		return (EBADF);
503 	};
504 	sb->st_mode = mode;
505 	sb->st_nlink = va.va_nlink;
506 	sb->st_uid = va.va_uid;
507 	sb->st_gid = va.va_gid;
508 	sb->st_rdev = va.va_rdev;
509 	sb->st_size = va.va_size;
510 	sb->st_atimespec = va.va_atime;
511 	sb->st_mtimespec = va.va_mtime;
512 	sb->st_ctimespec = va.va_ctime;
513 	sb->st_blksize = va.va_blocksize;
514 	sb->st_flags = va.va_flags;
515 	sb->st_gen = 0;
516 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
517 	return (0);
518 }
519 
520 /*
521  * File table vnode fcntl routine.
522  */
523 int
524 vn_fcntl(fp, com, data, p)
525 	struct file *fp;
526 	u_int com;
527 	caddr_t data;
528 	struct proc *p;
529 {
530 	struct vnode *vp = ((struct vnode *)fp->f_data);
531 	int error;
532 
533 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
534 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
535 	VOP_UNLOCK(vp, 0);
536 	return (error);
537 }
538 
539 /*
540  * File table vnode ioctl routine.
541  */
542 int
543 vn_ioctl(fp, com, data, p)
544 	struct file *fp;
545 	u_long com;
546 	caddr_t data;
547 	struct proc *p;
548 {
549 	struct vnode *vp = ((struct vnode *)fp->f_data);
550 	struct vattr vattr;
551 	int error;
552 
553 	switch (vp->v_type) {
554 
555 	case VREG:
556 	case VDIR:
557 		if (com == FIONREAD) {
558 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
559 			if (error)
560 				return (error);
561 			*(int *)data = vattr.va_size - fp->f_offset;
562 			return (0);
563 		}
564 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
565 			return (0);			/* XXX */
566 		/* fall into ... */
567 
568 	default:
569 		return (EPASSTHROUGH);
570 
571 	case VFIFO:
572 	case VCHR:
573 	case VBLK:
574 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
575 		if (error == 0 && com == TIOCSCTTY) {
576 			if (p->p_session->s_ttyvp)
577 				vrele(p->p_session->s_ttyvp);
578 			p->p_session->s_ttyvp = vp;
579 			VREF(vp);
580 		}
581 		return (error);
582 	}
583 }
584 
585 /*
586  * File table vnode poll routine.
587  */
588 int
589 vn_poll(fp, events, p)
590 	struct file *fp;
591 	int events;
592 	struct proc *p;
593 {
594 
595 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
596 }
597 
598 /*
599  * Check that the vnode is still valid, and if so
600  * acquire requested lock.
601  */
602 int
603 vn_lock(vp, flags)
604 	struct vnode *vp;
605 	int flags;
606 {
607 	int error;
608 
609 	do {
610 		if ((flags & LK_INTERLOCK) == 0)
611 			simple_lock(&vp->v_interlock);
612 		if (vp->v_flag & VXLOCK) {
613 			if (flags & LK_NOWAIT) {
614 				simple_unlock(&vp->v_interlock);
615 				return EBUSY;
616 			}
617 			vp->v_flag |= VXWANT;
618 			ltsleep(vp, PINOD | PNORELOCK,
619 			    "vn_lock", 0, &vp->v_interlock);
620 			error = ENOENT;
621 		} else {
622 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
623 			if (error == 0 || error == EDEADLK || error == EBUSY)
624 				return (error);
625 		}
626 		flags &= ~LK_INTERLOCK;
627 	} while (flags & LK_RETRY);
628 	return (error);
629 }
630 
631 /*
632  * File table vnode close routine.
633  */
634 int
635 vn_closefile(fp, p)
636 	struct file *fp;
637 	struct proc *p;
638 {
639 
640 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
641 		fp->f_cred, p));
642 }
643 
644 /*
645  * Enable LK_CANRECURSE on lock. Return prior status.
646  */
647 u_int
648 vn_setrecurse(vp)
649 	struct vnode *vp;
650 {
651 	struct lock *lkp = &vp->v_lock;
652 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
653 
654 	lkp->lk_flags |= LK_CANRECURSE;
655 	return retval;
656 }
657 
658 /*
659  * Called when done with locksetrecurse.
660  */
661 void
662 vn_restorerecurse(vp, flags)
663 	struct vnode *vp;
664 	u_int flags;
665 {
666 	struct lock *lkp = &vp->v_lock;
667 
668 	lkp->lk_flags &= ~LK_CANRECURSE;
669 	lkp->lk_flags |= flags;
670 }
671