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