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