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