xref: /freebsd/sys/kern/vfs_vnops.c (revision 61afd5bb22d787b0641523e7b9b95c964d669bd5)
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.26 1996/08/21 21:55:23 dyson Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/ioctl.h>
53 
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vnode_pager.h>
58 
59 static int vn_closefile __P((struct file *fp, struct proc *p));
60 static int vn_ioctl __P((struct file *fp, int com, caddr_t data,
61 		struct proc *p));
62 static int vn_read __P((struct file *fp, struct uio *uio,
63 		struct ucred *cred));
64 static int vn_select __P((struct file *fp, int which, struct proc *p));
65 static int vn_write __P((struct file *fp, struct uio *uio,
66 		struct ucred *cred));
67 
68 struct 	fileops vnops =
69 	{ vn_read, vn_write, vn_ioctl, vn_select, 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
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 vat;
84 	struct vattr *vap = &vat;
85 	int error;
86 
87 	if (fmode & O_CREAT) {
88 		ndp->ni_cnd.cn_nameiop = CREATE;
89 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
90 		if ((fmode & O_EXCL) == 0)
91 			ndp->ni_cnd.cn_flags |= FOLLOW;
92 		error = namei(ndp);
93 		if (error)
94 			return (error);
95 		if (ndp->ni_vp == NULL) {
96 			VATTR_NULL(vap);
97 			vap->va_type = VREG;
98 			vap->va_mode = cmode;
99 			LEASE_CHECK(ndp->ni_dvp, p, cred, LEASE_WRITE);
100 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
101 			    &ndp->ni_cnd, vap);
102 			if (error)
103 				return (error);
104 			fmode &= ~O_TRUNC;
105 			vp = ndp->ni_vp;
106 		} else {
107 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
108 			if (ndp->ni_dvp == ndp->ni_vp)
109 				vrele(ndp->ni_dvp);
110 			else
111 				vput(ndp->ni_dvp);
112 			ndp->ni_dvp = NULL;
113 			vp = ndp->ni_vp;
114 			if (fmode & O_EXCL) {
115 				error = EEXIST;
116 				goto bad;
117 			}
118 			fmode &= ~O_CREAT;
119 		}
120 	} else {
121 		ndp->ni_cnd.cn_nameiop = LOOKUP;
122 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
123 		error = namei(ndp);
124 		if (error)
125 			return (error);
126 		vp = ndp->ni_vp;
127 	}
128 	if (vp->v_type == VSOCK) {
129 		error = EOPNOTSUPP;
130 		goto bad;
131 	}
132 	if ((fmode & O_CREAT) == 0) {
133 		if (fmode & FREAD) {
134 			error = VOP_ACCESS(vp, VREAD, cred, p);
135 			if (error)
136 				goto bad;
137 		}
138 		if (fmode & (FWRITE | O_TRUNC)) {
139 			if (vp->v_type == VDIR) {
140 				error = EISDIR;
141 				goto bad;
142 			}
143 			error = vn_writechk(vp);
144 			if (error)
145 				goto bad;
146 		        error = VOP_ACCESS(vp, VWRITE, cred, p);
147 			if (error)
148 				goto bad;
149 		}
150 	}
151 	if (fmode & O_TRUNC) {
152 		VOP_UNLOCK(vp);				/* XXX */
153 		LEASE_CHECK(vp, p, cred, LEASE_WRITE);
154 		VOP_LOCK(vp);				/* XXX */
155 		VATTR_NULL(vap);
156 		vap->va_size = 0;
157 		error = VOP_SETATTR(vp, vap, cred, p);
158 		if (error)
159 			goto bad;
160 	}
161 	error = VOP_OPEN(vp, fmode, cred, p);
162 	if (error)
163 		goto bad;
164 	/*
165 	 * Make sure that a VM object is created for VMIO support.
166 	 */
167 	if (vp->v_type == VREG) {
168 		if ((error = vfs_object_create(vp, p, cred, 1)) != 0)
169 			goto bad;
170 	}
171 
172 	if (fmode & FWRITE)
173 		vp->v_writecount++;
174 	return (0);
175 bad:
176 	vput(vp);
177 	return (error);
178 }
179 
180 /*
181  * Check for write permissions on the specified vnode.
182  * The read-only status of the file system is checked.
183  * Also, prototype text segments cannot be written.
184  */
185 int
186 vn_writechk(vp)
187 	register struct vnode *vp;
188 {
189 
190 	/*
191 	 * If there's shared text associated with
192 	 * the vnode, try to free it up once.  If
193 	 * we fail, we can't allow writing.
194 	 */
195 	if (vp->v_flag & VTEXT)
196 		return (ETXTBSY);
197 	return (0);
198 }
199 
200 /*
201  * Vnode close call
202  */
203 int
204 vn_close(vp, flags, cred, p)
205 	register struct vnode *vp;
206 	int flags;
207 	struct ucred *cred;
208 	struct proc *p;
209 {
210 	int error;
211 
212 	if (flags & FWRITE)
213 		vp->v_writecount--;
214 	error = VOP_CLOSE(vp, flags, cred, p);
215 	vrele(vp);
216 	return (error);
217 }
218 
219 /*
220  * Package up an I/O request on a vnode into a uio and do it.
221  */
222 int
223 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
224 	enum uio_rw rw;
225 	struct vnode *vp;
226 	caddr_t base;
227 	int len;
228 	off_t offset;
229 	enum uio_seg segflg;
230 	int ioflg;
231 	struct ucred *cred;
232 	int *aresid;
233 	struct proc *p;
234 {
235 	struct uio auio;
236 	struct iovec aiov;
237 	int error;
238 
239 	if ((ioflg & IO_NODELOCKED) == 0)
240 		VOP_LOCK(vp);
241 	auio.uio_iov = &aiov;
242 	auio.uio_iovcnt = 1;
243 	aiov.iov_base = base;
244 	aiov.iov_len = len;
245 	auio.uio_resid = len;
246 	auio.uio_offset = offset;
247 	auio.uio_segflg = segflg;
248 	auio.uio_rw = rw;
249 	auio.uio_procp = p;
250 	if (rw == UIO_READ) {
251 		error = VOP_READ(vp, &auio, ioflg, cred);
252 	} else {
253 		error = VOP_WRITE(vp, &auio, ioflg, cred);
254 	}
255 	if (aresid)
256 		*aresid = auio.uio_resid;
257 	else
258 		if (auio.uio_resid && error == 0)
259 			error = EIO;
260 	if ((ioflg & IO_NODELOCKED) == 0)
261 		VOP_UNLOCK(vp);
262 	return (error);
263 }
264 
265 /*
266  * File table vnode read routine.
267  */
268 static int
269 vn_read(fp, uio, cred)
270 	struct file *fp;
271 	struct uio *uio;
272 	struct ucred *cred;
273 {
274 	register struct vnode *vp = (struct vnode *)fp->f_data;
275 	int count, error;
276 	int flag, seq;
277 
278 	LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
279 	VOP_LOCK(vp);
280 	uio->uio_offset = fp->f_offset;
281 	count = uio->uio_resid;
282 	flag = 0;
283 	if (fp->f_flag & FNONBLOCK)
284 		flag |= IO_NDELAY;
285 
286 	/*
287 	 * Sequential read heuristic.
288 	 * If we have been doing sequential input,
289 	 * a rewind operation doesn't turn off
290 	 * sequential input mode.
291 	 */
292 	if (((fp->f_offset == 0) && (fp->f_seqcount > 0)) ||
293 		(fp->f_offset == fp->f_nextread)) {
294 		int tmpseq = fp->f_seqcount;
295 		/*
296 		 * XXX we assume that the filesystem block size is
297 		 * the default.  Not true, but still gives us a pretty
298 		 * good indicator of how sequential the read operations
299 		 * are.
300 		 */
301 		tmpseq += ((count + BKVASIZE - 1) / BKVASIZE);
302 		if (tmpseq >= CHAR_MAX)
303 			tmpseq = CHAR_MAX;
304 		fp->f_seqcount = tmpseq;
305 		flag |= (fp->f_seqcount << 16);
306 	} else {
307 		if (fp->f_seqcount > 1)
308 			fp->f_seqcount = 1;
309 		else
310 			fp->f_seqcount = 0;
311 	}
312 
313 	error = VOP_READ(vp, uio, flag, cred);
314 	fp->f_offset += count - uio->uio_resid;
315 	fp->f_nextread = fp->f_offset;
316 	VOP_UNLOCK(vp);
317 	return (error);
318 }
319 
320 /*
321  * File table vnode write routine.
322  */
323 static int
324 vn_write(fp, uio, cred)
325 	struct file *fp;
326 	struct uio *uio;
327 	struct ucred *cred;
328 {
329 	register struct vnode *vp = (struct vnode *)fp->f_data;
330 	int count, error, ioflag = 0;
331 
332 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
333 		ioflag |= IO_APPEND;
334 	if (fp->f_flag & FNONBLOCK)
335 		ioflag |= IO_NDELAY;
336 	LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
337 	VOP_LOCK(vp);
338 	uio->uio_offset = fp->f_offset;
339 	count = uio->uio_resid;
340 	error = VOP_WRITE(vp, uio, ioflag, cred);
341 	if (ioflag & IO_APPEND)
342 		fp->f_offset = uio->uio_offset;
343 	else
344 		fp->f_offset += count - uio->uio_resid;
345 	VOP_UNLOCK(vp);
346 	return (error);
347 }
348 
349 /*
350  * File table vnode stat routine.
351  */
352 int
353 vn_stat(vp, sb, p)
354 	struct vnode *vp;
355 	register struct stat *sb;
356 	struct proc *p;
357 {
358 	struct vattr vattr;
359 	register struct vattr *vap;
360 	int error;
361 	u_short mode;
362 
363 	vap = &vattr;
364 	error = VOP_GETATTR(vp, vap, p->p_ucred, p);
365 	if (error)
366 		return (error);
367 	/*
368 	 * Copy from vattr table
369 	 */
370 	sb->st_dev = vap->va_fsid;
371 	sb->st_ino = vap->va_fileid;
372 	mode = vap->va_mode;
373 	switch (vp->v_type) {
374 	case VREG:
375 		mode |= S_IFREG;
376 		break;
377 	case VDIR:
378 		mode |= S_IFDIR;
379 		break;
380 	case VBLK:
381 		mode |= S_IFBLK;
382 		break;
383 	case VCHR:
384 		mode |= S_IFCHR;
385 		break;
386 	case VLNK:
387 		mode |= S_IFLNK;
388 		break;
389 	case VSOCK:
390 		mode |= S_IFSOCK;
391 		break;
392 	case VFIFO:
393 		mode |= S_IFIFO;
394 		break;
395 	default:
396 		return (EBADF);
397 	};
398 	sb->st_mode = mode;
399 	sb->st_nlink = vap->va_nlink;
400 	sb->st_uid = vap->va_uid;
401 	sb->st_gid = vap->va_gid;
402 	sb->st_rdev = vap->va_rdev;
403 	sb->st_size = vap->va_size;
404 	sb->st_atimespec = vap->va_atime;
405 	sb->st_mtimespec= vap->va_mtime;
406 	sb->st_ctimespec = vap->va_ctime;
407 	sb->st_blksize = vap->va_blocksize;
408 	sb->st_flags = vap->va_flags;
409 	sb->st_gen = vap->va_gen;
410 #if (S_BLKSIZE == 512)
411 	/* Optimize this case */
412 	sb->st_blocks = vap->va_bytes >> 9;
413 #else
414 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
415 #endif
416 	return (0);
417 }
418 
419 /*
420  * File table vnode ioctl routine.
421  */
422 static int
423 vn_ioctl(fp, com, data, p)
424 	struct file *fp;
425 	int com;
426 	caddr_t data;
427 	struct proc *p;
428 {
429 	register struct vnode *vp = ((struct vnode *)fp->f_data);
430 	struct vattr vattr;
431 	int error;
432 
433 	switch (vp->v_type) {
434 
435 	case VREG:
436 	case VDIR:
437 		if (com == FIONREAD) {
438 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
439 			if (error)
440 				return (error);
441 			*(int *)data = vattr.va_size - fp->f_offset;
442 			return (0);
443 		}
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 
457 			/* Do nothing if reassigning same control tty */
458 			if (p->p_session->s_ttyvp == vp)
459 				return (0);
460 
461 			/* Get rid of reference to old control tty */
462 			if (p->p_session->s_ttyvp)
463 				vrele(p->p_session->s_ttyvp);
464 
465 			p->p_session->s_ttyvp = vp;
466 			VREF(vp);
467 		}
468 		return (error);
469 	}
470 }
471 
472 /*
473  * File table vnode select routine.
474  */
475 static int
476 vn_select(fp, which, p)
477 	struct file *fp;
478 	int which;
479 	struct proc *p;
480 {
481 
482 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
483 		fp->f_cred, p));
484 }
485 
486 /*
487  * File table vnode close routine.
488  */
489 static int
490 vn_closefile(fp, p)
491 	struct file *fp;
492 	struct proc *p;
493 {
494 
495 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
496 		fp->f_cred, p));
497 }
498