xref: /openbsd-src/sys/kern/vfs_vnops.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: vfs_vnops.c,v 1.33 2001/07/16 18:25:43 millert 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 <vm/vm.h>
59 
60 #include <uvm/uvm_extern.h>
61 
62 int	vn_read __P((struct file *fp, off_t *off, struct uio *uio,
63 	    struct ucred *cred));
64 int	vn_write __P((struct file *fp, off_t *off, struct uio *uio,
65 	    struct ucred *cred));
66 int	vn_select __P((struct file *fp, int which, struct proc *p));
67 int	vn_kqfilter __P((struct file *fp, struct knote *kn));
68 int 	vn_closefile __P((struct file *fp, struct proc *p));
69 int	vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
70 	    struct proc *p));
71 
72 struct 	fileops vnops =
73 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_kqfilter, vn_statfile,
74 	  vn_closefile };
75 
76 /*
77  * Common code for vnode open operations.
78  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
79  */
80 int
81 vn_open(ndp, fmode, cmode)
82 	register struct nameidata *ndp;
83 	int fmode, cmode;
84 {
85 	register struct vnode *vp;
86 	register struct proc *p = ndp->ni_cnd.cn_proc;
87 	register struct ucred *cred = p->p_ucred;
88 	struct vattr va;
89 	int error;
90 
91 	if ((fmode & (FREAD|FWRITE)) == 0)
92 		return (EINVAL);
93 	if ((fmode & (O_TRUNC | FWRITE)) == O_TRUNC)
94 		return (EINVAL);
95 	if (fmode & O_CREAT) {
96 		ndp->ni_cnd.cn_nameiop = CREATE;
97 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
98 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
99 			ndp->ni_cnd.cn_flags |= FOLLOW;
100 		if ((error = namei(ndp)) != 0)
101 			return (error);
102 
103 		if (ndp->ni_vp == NULL) {
104 			VATTR_NULL(&va);
105 			va.va_type = VREG;
106 			va.va_mode = cmode;
107 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
108 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
109 					   &ndp->ni_cnd, &va);
110 			if (error)
111 				return (error);
112 			fmode &= ~O_TRUNC;
113 			vp = ndp->ni_vp;
114 		} else {
115 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
116 			if (ndp->ni_dvp == ndp->ni_vp)
117 				vrele(ndp->ni_dvp);
118 			else
119 				vput(ndp->ni_dvp);
120 			ndp->ni_dvp = NULL;
121 			vp = ndp->ni_vp;
122 			if (fmode & O_EXCL) {
123 				error = EEXIST;
124 				goto bad;
125 			}
126 			fmode &= ~O_CREAT;
127 		}
128 	} else {
129 		ndp->ni_cnd.cn_nameiop = LOOKUP;
130 		ndp->ni_cnd.cn_flags =
131 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : 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 (vp->v_type == VLNK) {
141 		error = EMLINK;
142 		goto bad;
143 	}
144 	if ((fmode & O_CREAT) == 0) {
145 		if (fmode & FREAD) {
146 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
147 				goto bad;
148 		}
149 		if (fmode & FWRITE) {
150 			if (vp->v_type == VDIR) {
151 				error = EISDIR;
152 				goto bad;
153 			}
154 			if ((error = vn_writechk(vp)) != 0 ||
155 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
156 				goto bad;
157 		}
158 	}
159 	if ((fmode & O_TRUNC) && vp->v_type == VREG) {
160 		VOP_UNLOCK(vp, 0, p);				/* XXX */
161 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
162 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);	/* XXX */
163 		VATTR_NULL(&va);
164 		va.va_size = 0;
165 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
166 			goto bad;
167 	}
168 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
169 		goto bad;
170 	if (fmode & FWRITE)
171 		vp->v_writecount++;
172 	return (0);
173 bad:
174 	vput(vp);
175 	return (error);
176 }
177 
178 /*
179  * Check for write permissions on the specified vnode.
180  * Prototype text segments cannot be written.
181  */
182 int
183 vn_writechk(vp)
184 	register struct vnode *vp;
185 {
186 
187 	/*
188 	 * Disallow write attempts on read-only file systems;
189 	 * unless the file is a socket or a block or character
190 	 * device resident on the file system.
191 	 */
192 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
193 		switch (vp->v_type) {
194 		case VREG: case VDIR: case VLNK:
195 			return (EROFS);
196 		case VNON: case VCHR: case VSOCK:
197 		case VFIFO: case VBAD: case VBLK:
198 			break;
199 		}
200 	}
201 	/*
202 	 * If there's shared text associated with
203 	 * the vnode, try to free it up once.  If
204 	 * we fail, we can't allow writing.
205 	 */
206 	if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
207 		return (ETXTBSY);
208 
209 	return (0);
210 }
211 
212 /*
213  * Vnode close call
214  */
215 int
216 vn_close(vp, flags, cred, p)
217 	register struct vnode *vp;
218 	int flags;
219 	struct ucred *cred;
220 	struct proc *p;
221 {
222 	int error;
223 
224 	if (flags & FWRITE)
225 		vp->v_writecount--;
226 	error = VOP_CLOSE(vp, flags, cred, p);
227 	vrele(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, p);
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, p);
274 	return (error);
275 }
276 
277 /*
278  * File table vnode read routine.
279  */
280 int
281 vn_read(fp, poff, uio, cred)
282 	struct file *fp;
283 	off_t *poff;
284 	struct uio *uio;
285 	struct ucred *cred;
286 {
287 	register struct vnode *vp = (struct vnode *)fp->f_data;
288 	int error = 0;
289 	size_t count;
290 	struct proc *p = uio->uio_procp;
291 
292 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
293 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
294 	uio->uio_offset = *poff;
295 	count = uio->uio_resid;
296 	if (vp->v_type != VDIR)
297 		error = VOP_READ(vp, uio,
298 		    (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, cred);
299 	*poff += count - uio->uio_resid;
300 	VOP_UNLOCK(vp, 0, p);
301 	return (error);
302 }
303 
304 /*
305  * File table vnode write routine.
306  */
307 int
308 vn_write(fp, poff, uio, cred)
309 	struct file *fp;
310 	off_t *poff;
311 	struct uio *uio;
312 	struct ucred *cred;
313 {
314 	register struct vnode *vp = (struct vnode *)fp->f_data;
315 	struct proc *p = uio->uio_procp;
316 	int error, ioflag = IO_UNIT;
317 	size_t count;
318 
319 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
320 		ioflag |= IO_APPEND;
321 	if (fp->f_flag & FNONBLOCK)
322 		ioflag |= IO_NDELAY;
323 	if ((fp->f_flag & FFSYNC) ||
324 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
325 		ioflag |= IO_SYNC;
326 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
327 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
328 	uio->uio_offset = *poff;
329 	count = uio->uio_resid;
330 	error = VOP_WRITE(vp, uio, ioflag, cred);
331 	if (ioflag & IO_APPEND)
332 		*poff = uio->uio_offset;
333 	else
334 		*poff += count - uio->uio_resid;
335 	VOP_UNLOCK(vp, 0, p);
336 	return (error);
337 }
338 
339 /*
340  * File table wrapper for vn_stat
341  */
342 int
343 vn_statfile(fp, sb, p)
344 	struct file *fp;
345 	struct stat *sb;
346 	struct proc *p;
347 {
348 	struct vnode *vp = (struct vnode *)fp->f_data;
349 
350 	return vn_stat(vp, sb, p);
351 }
352 
353 /*
354  * vnode stat routine.
355  */
356 int
357 vn_stat(vp, sb, p)
358 	struct vnode *vp;
359 	register struct stat *sb;
360 	struct proc *p;
361 {
362 	struct vattr va;
363 	int error;
364 	u_short mode;
365 
366 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
367 	if (error)
368 		return (error);
369 	/*
370 	 * Copy from vattr table
371 	 */
372 	sb->st_dev = va.va_fsid;
373 	sb->st_ino = va.va_fileid;
374 	mode = va.va_mode;
375 	switch (vp->v_type) {
376 	case VREG:
377 		mode |= S_IFREG;
378 		break;
379 	case VDIR:
380 		mode |= S_IFDIR;
381 		break;
382 	case VBLK:
383 		mode |= S_IFBLK;
384 		break;
385 	case VCHR:
386 		mode |= S_IFCHR;
387 		break;
388 	case VLNK:
389 		mode |= S_IFLNK;
390 		break;
391 	case VSOCK:
392 		mode |= S_IFSOCK;
393 		break;
394 	case VFIFO:
395 		mode |= S_IFIFO;
396 		break;
397 	default:
398 		return (EBADF);
399 	}
400 	sb->st_mode = mode;
401 	sb->st_nlink = va.va_nlink;
402 	sb->st_uid = va.va_uid;
403 	sb->st_gid = va.va_gid;
404 	sb->st_rdev = va.va_rdev;
405 	sb->st_size = va.va_size;
406 	sb->st_atimespec = va.va_atime;
407 	sb->st_mtimespec = va.va_mtime;
408 	sb->st_ctimespec = va.va_ctime;
409 	sb->st_blksize = va.va_blocksize;
410 	sb->st_flags = va.va_flags;
411 	sb->st_gen = va.va_gen;
412 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
413 	return (0);
414 }
415 
416 /*
417  * File table vnode ioctl routine.
418  */
419 int
420 vn_ioctl(fp, com, data, p)
421 	struct file *fp;
422 	u_long com;
423 	caddr_t data;
424 	struct proc *p;
425 {
426 	register struct vnode *vp = ((struct vnode *)fp->f_data);
427 	struct vattr vattr;
428 	int error;
429 
430 	switch (vp->v_type) {
431 
432 	case VREG:
433 	case VDIR:
434 		if (com == FIONREAD) {
435 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
436 			if (error)
437 				return (error);
438 			*(int *)data = vattr.va_size - fp->f_offset;
439 			return (0);
440 		}
441 		if (com == FIBMAP)
442 			return VOP_IOCTL(vp, com, data, fp->f_flag,
443 					 p->p_ucred, p);
444 		if (com == FIONBIO || com == FIOASYNC)  /* XXX */
445 			return (0);			/* XXX */
446 		/* fall into... */
447 
448 	default:
449 		return (ENOTTY);
450 
451 	case VFIFO:
452 	case VCHR:
453 	case VBLK:
454 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
455 		if (error == 0 && com == TIOCSCTTY) {
456 			if (p->p_session->s_ttyvp)
457 				vrele(p->p_session->s_ttyvp);
458 			p->p_session->s_ttyvp = vp;
459 			VREF(vp);
460 		}
461 		return (error);
462 	}
463 }
464 
465 /*
466  * File table vnode select routine.
467  */
468 int
469 vn_select(fp, which, p)
470 	struct file *fp;
471 	int which;
472 	struct proc *p;
473 {
474 
475 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
476 			   fp->f_cred, p));
477 }
478 
479 /*
480  * Check that the vnode is still valid, and if so
481  * acquire requested lock.
482  */
483 int
484 vn_lock(vp, flags, p)
485 	struct vnode *vp;
486 	int flags;
487 	struct proc *p;
488 {
489 	int error;
490 
491 	do {
492 		if ((flags & LK_INTERLOCK) == 0)
493 			simple_lock(&vp->v_interlock);
494 		if (vp->v_flag & VXLOCK) {
495 			vp->v_flag |= VXWANT;
496 			simple_unlock(&vp->v_interlock);
497 			tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
498 			error = ENOENT;
499 		} else {
500 			error = VOP_LOCK(vp, flags | LK_INTERLOCK | LK_CANRECURSE, p);
501 			if (error == 0)
502 				return (error);
503 		}
504 		flags &= ~LK_INTERLOCK;
505 	} while (flags & LK_RETRY);
506 	return (error);
507 }
508 
509 /*
510  * File table vnode close routine.
511  */
512 int
513 vn_closefile(fp, p)
514 	struct file *fp;
515 	struct proc *p;
516 {
517 
518 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
519 		fp->f_cred, p));
520 }
521 
522 /*ARGSUSED*/
523 int
524 vn_kqfilter(struct file *fp, struct knote *kn)
525 {
526 	return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
527 }
528