xref: /openbsd-src/sys/kern/vfs_vnops.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: vfs_vnops.c,v 1.39 2001/12/19 08:58:06 art Exp $	*/
2 /*	$NetBSD: vfs_vnops.c,v 1.20 1996/02/04 02:18:41 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)vfs_vnops.c	8.5 (Berkeley) 12/8/94
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/ioctl.h>
55 #include <sys/tty.h>
56 #include <sys/cdio.h>
57 
58 #include <uvm/uvm_extern.h>
59 
60 int	vn_read __P((struct file *fp, off_t *off, struct uio *uio,
61 	    struct ucred *cred));
62 int	vn_write __P((struct file *fp, off_t *off, struct uio *uio,
63 	    struct ucred *cred));
64 int	vn_select __P((struct file *fp, int which, struct proc *p));
65 int	vn_kqfilter __P((struct file *fp, struct knote *kn));
66 int 	vn_closefile __P((struct file *fp, struct proc *p));
67 int	vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
68 	    struct proc *p));
69 
70 struct 	fileops vnops =
71 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_kqfilter, vn_statfile,
72 	  vn_closefile };
73 
74 /*
75  * Common code for vnode open operations.
76  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
77  */
78 int
79 vn_open(ndp, fmode, cmode)
80 	register struct nameidata *ndp;
81 	int fmode, cmode;
82 {
83 	register struct vnode *vp;
84 	register struct proc *p = ndp->ni_cnd.cn_proc;
85 	register struct ucred *cred = p->p_ucred;
86 	struct vattr va;
87 	int error;
88 
89 	if ((fmode & (FREAD|FWRITE)) == 0)
90 		return (EINVAL);
91 	if ((fmode & (O_TRUNC | FWRITE)) == O_TRUNC)
92 		return (EINVAL);
93 	if (fmode & O_CREAT) {
94 		ndp->ni_cnd.cn_nameiop = CREATE;
95 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
96 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
97 			ndp->ni_cnd.cn_flags |= FOLLOW;
98 		if ((error = namei(ndp)) != 0)
99 			return (error);
100 
101 		if (ndp->ni_vp == NULL) {
102 			VATTR_NULL(&va);
103 			va.va_type = VREG;
104 			va.va_mode = cmode;
105 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
106 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
107 					   &ndp->ni_cnd, &va);
108 			if (error)
109 				return (error);
110 			fmode &= ~O_TRUNC;
111 			vp = ndp->ni_vp;
112 		} else {
113 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
114 			if (ndp->ni_dvp == ndp->ni_vp)
115 				vrele(ndp->ni_dvp);
116 			else
117 				vput(ndp->ni_dvp);
118 			ndp->ni_dvp = NULL;
119 			vp = ndp->ni_vp;
120 			if (fmode & O_EXCL) {
121 				error = EEXIST;
122 				goto bad;
123 			}
124 			fmode &= ~O_CREAT;
125 		}
126 	} else {
127 		ndp->ni_cnd.cn_nameiop = LOOKUP;
128 		ndp->ni_cnd.cn_flags =
129 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
130 		if ((error = namei(ndp)) != 0)
131 			return (error);
132 		vp = ndp->ni_vp;
133 	}
134 	if (vp->v_type == VSOCK) {
135 		error = EOPNOTSUPP;
136 		goto bad;
137 	}
138 	if (vp->v_type == VLNK) {
139 		error = EMLINK;
140 		goto bad;
141 	}
142 	if ((fmode & O_CREAT) == 0) {
143 		if (fmode & FREAD) {
144 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
145 				goto bad;
146 		}
147 		if (fmode & FWRITE) {
148 			if (vp->v_type == VDIR) {
149 				error = EISDIR;
150 				goto bad;
151 			}
152 			if ((error = vn_writechk(vp)) != 0 ||
153 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
154 				goto bad;
155 		}
156 	}
157 	if ((fmode & O_TRUNC) && vp->v_type == VREG) {
158 		VOP_UNLOCK(vp, 0, p);				/* XXX */
159 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
160 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);	/* XXX */
161 		VATTR_NULL(&va);
162 		va.va_size = 0;
163 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
164 			goto bad;
165 	}
166 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
167 		goto bad;
168 	if (fmode & FWRITE)
169 		vp->v_writecount++;
170 	return (0);
171 bad:
172 	vput(vp);
173 	return (error);
174 }
175 
176 /*
177  * Check for write permissions on the specified vnode.
178  * Prototype text segments cannot be written.
179  */
180 int
181 vn_writechk(vp)
182 	register struct vnode *vp;
183 {
184 
185 	/*
186 	 * Disallow write attempts on read-only file systems;
187 	 * unless the file is a socket or a block or character
188 	 * device resident on the file system.
189 	 */
190 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
191 		switch (vp->v_type) {
192 		case VREG: case VDIR: case VLNK:
193 			return (EROFS);
194 		case VNON: case VCHR: case VSOCK:
195 		case VFIFO: case VBAD: case VBLK:
196 			break;
197 		}
198 	}
199 	/*
200 	 * If there's shared text associated with
201 	 * the vnode, try to free it up once.  If
202 	 * we fail, we can't allow writing.
203 	 */
204 	if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
205 		return (ETXTBSY);
206 
207 	return (0);
208 }
209 
210 /*
211  * Mark a vnode as being the text image of a running process.
212  */
213 void
214 vn_marktext(vp)
215 	struct vnode *vp;
216 {
217 	vp->v_flag |= VTEXT;
218 }
219 
220 /*
221  * Vnode close call
222  */
223 int
224 vn_close(vp, flags, cred, p)
225 	register struct vnode *vp;
226 	int flags;
227 	struct ucred *cred;
228 	struct proc *p;
229 {
230 	int error;
231 
232 	if (flags & FWRITE)
233 		vp->v_writecount--;
234 	error = VOP_CLOSE(vp, flags, cred, p);
235 	vrele(vp);
236 	return (error);
237 }
238 
239 /*
240  * Package up an I/O request on a vnode into a uio and do it.
241  */
242 int
243 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
244 	enum uio_rw rw;
245 	struct vnode *vp;
246 	caddr_t base;
247 	int len;
248 	off_t offset;
249 	enum uio_seg segflg;
250 	int ioflg;
251 	struct ucred *cred;
252 	size_t *aresid;
253 	struct proc *p;
254 {
255 	struct uio auio;
256 	struct iovec aiov;
257 	int error;
258 
259 	if ((ioflg & IO_NODELOCKED) == 0)
260 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
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, p);
282 	return (error);
283 }
284 
285 /*
286  * File table vnode read routine.
287  */
288 int
289 vn_read(fp, poff, uio, cred)
290 	struct file *fp;
291 	off_t *poff;
292 	struct uio *uio;
293 	struct ucred *cred;
294 {
295 	register struct vnode *vp = (struct vnode *)fp->f_data;
296 	int error = 0;
297 	size_t count;
298 	struct proc *p = uio->uio_procp;
299 
300 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
301 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
302 	uio->uio_offset = *poff;
303 	count = uio->uio_resid;
304 	if (vp->v_type != VDIR)
305 		error = VOP_READ(vp, uio,
306 		    (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, cred);
307 	*poff += count - uio->uio_resid;
308 	VOP_UNLOCK(vp, 0, p);
309 	return (error);
310 }
311 
312 /*
313  * File table vnode write routine.
314  */
315 int
316 vn_write(fp, poff, uio, cred)
317 	struct file *fp;
318 	off_t *poff;
319 	struct uio *uio;
320 	struct ucred *cred;
321 {
322 	register struct vnode *vp = (struct vnode *)fp->f_data;
323 	struct proc *p = uio->uio_procp;
324 	int error, ioflag = IO_UNIT;
325 	size_t count;
326 
327 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
328 		ioflag |= IO_APPEND;
329 	if (fp->f_flag & FNONBLOCK)
330 		ioflag |= IO_NDELAY;
331 	if ((fp->f_flag & FFSYNC) ||
332 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
333 		ioflag |= IO_SYNC;
334 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
335 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
336 	uio->uio_offset = *poff;
337 	count = uio->uio_resid;
338 	error = VOP_WRITE(vp, uio, ioflag, cred);
339 	if (ioflag & IO_APPEND)
340 		*poff = uio->uio_offset;
341 	else
342 		*poff += count - uio->uio_resid;
343 	VOP_UNLOCK(vp, 0, p);
344 	return (error);
345 }
346 
347 /*
348  * File table wrapper for vn_stat
349  */
350 int
351 vn_statfile(fp, sb, p)
352 	struct file *fp;
353 	struct stat *sb;
354 	struct proc *p;
355 {
356 	struct vnode *vp = (struct vnode *)fp->f_data;
357 
358 	return vn_stat(vp, sb, p);
359 }
360 
361 /*
362  * vnode stat routine.
363  */
364 int
365 vn_stat(vp, sb, p)
366 	struct vnode *vp;
367 	register struct stat *sb;
368 	struct proc *p;
369 {
370 	struct vattr va;
371 	int error;
372 	u_short mode;
373 
374 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
375 	if (error)
376 		return (error);
377 	/*
378 	 * Copy from vattr table
379 	 */
380 	sb->st_dev = va.va_fsid;
381 	sb->st_ino = va.va_fileid;
382 	mode = va.va_mode;
383 	switch (vp->v_type) {
384 	case VREG:
385 		mode |= S_IFREG;
386 		break;
387 	case VDIR:
388 		mode |= S_IFDIR;
389 		break;
390 	case VBLK:
391 		mode |= S_IFBLK;
392 		break;
393 	case VCHR:
394 		mode |= S_IFCHR;
395 		break;
396 	case VLNK:
397 		mode |= S_IFLNK;
398 		break;
399 	case VSOCK:
400 		mode |= S_IFSOCK;
401 		break;
402 	case VFIFO:
403 		mode |= S_IFIFO;
404 		break;
405 	default:
406 		return (EBADF);
407 	}
408 	sb->st_mode = mode;
409 	sb->st_nlink = va.va_nlink;
410 	sb->st_uid = va.va_uid;
411 	sb->st_gid = va.va_gid;
412 	sb->st_rdev = va.va_rdev;
413 	sb->st_size = va.va_size;
414 	sb->st_atimespec = va.va_atime;
415 	sb->st_mtimespec = va.va_mtime;
416 	sb->st_ctimespec = va.va_ctime;
417 	sb->st_blksize = va.va_blocksize;
418 	sb->st_flags = va.va_flags;
419 	sb->st_gen = va.va_gen;
420 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
421 	return (0);
422 }
423 
424 /*
425  * File table vnode ioctl routine.
426  */
427 int
428 vn_ioctl(fp, com, data, p)
429 	struct file *fp;
430 	u_long com;
431 	caddr_t data;
432 	struct proc *p;
433 {
434 	register struct vnode *vp = ((struct vnode *)fp->f_data);
435 	struct vattr vattr;
436 	int error;
437 
438 	switch (vp->v_type) {
439 
440 	case VREG:
441 	case VDIR:
442 		if (com == FIONREAD) {
443 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
444 			if (error)
445 				return (error);
446 			*(int *)data = vattr.va_size - fp->f_offset;
447 			return (0);
448 		}
449 		if (com == FIBMAP)
450 			return VOP_IOCTL(vp, com, data, fp->f_flag,
451 					 p->p_ucred, p);
452 		if (com == FIONBIO || com == FIOASYNC)  /* XXX */
453 			return (0);			/* XXX */
454 		/* fall into... */
455 
456 	default:
457 		return (ENOTTY);
458 
459 	case VFIFO:
460 	case VCHR:
461 	case VBLK:
462 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
463 		if (error == 0 && com == TIOCSCTTY) {
464 			if (p->p_session->s_ttyvp)
465 				vrele(p->p_session->s_ttyvp);
466 			p->p_session->s_ttyvp = vp;
467 			VREF(vp);
468 		}
469 		return (error);
470 	}
471 }
472 
473 /*
474  * File table vnode select routine.
475  */
476 int
477 vn_select(fp, which, p)
478 	struct file *fp;
479 	int which;
480 	struct proc *p;
481 {
482 
483 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
484 			   fp->f_cred, p));
485 }
486 
487 /*
488  * Check that the vnode is still valid, and if so
489  * acquire requested lock.
490  */
491 int
492 vn_lock(vp, flags, p)
493 	struct vnode *vp;
494 	int flags;
495 	struct proc *p;
496 {
497 	int error;
498 
499 	do {
500 		if ((flags & LK_INTERLOCK) == 0)
501 			simple_lock(&vp->v_interlock);
502 		if (vp->v_flag & VXLOCK) {
503 			vp->v_flag |= VXWANT;
504 			simple_unlock(&vp->v_interlock);
505 			tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
506 			error = ENOENT;
507 		} else {
508 			error = VOP_LOCK(vp, flags | LK_INTERLOCK | LK_CANRECURSE, p);
509 			if (error == 0)
510 				return (error);
511 		}
512 		flags &= ~LK_INTERLOCK;
513 	} while (flags & LK_RETRY);
514 	return (error);
515 }
516 
517 /*
518  * File table vnode close routine.
519  */
520 int
521 vn_closefile(fp, p)
522 	struct file *fp;
523 	struct proc *p;
524 {
525 
526 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
527 		fp->f_cred, p));
528 }
529 
530 /*ARGSUSED*/
531 int
532 vn_kqfilter(struct file *fp, struct knote *kn)
533 {
534 	return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
535 }
536