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