xref: /freebsd/sys/kern/vfs_vnops.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
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.59 1998/06/27 06:43:09 phk Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/filio.h>
52 #include <sys/ttycom.h>
53 
54 static int vn_closefile __P((struct file *fp, struct proc *p));
55 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
56 		struct proc *p));
57 static int vn_read __P((struct file *fp, struct uio *uio,
58 		struct ucred *cred));
59 static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
60 		struct proc *p));
61 static int vn_write __P((struct file *fp, struct uio *uio,
62 		struct ucred *cred));
63 
64 struct 	fileops vnops =
65 	{ vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile };
66 
67 /*
68  * Common code for vnode open operations.
69  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
70  */
71 int
72 vn_open(ndp, fmode, cmode)
73 	register struct nameidata *ndp;
74 	int fmode, cmode;
75 {
76 	register struct vnode *vp;
77 	register struct proc *p = ndp->ni_cnd.cn_proc;
78 	register struct ucred *cred = p->p_ucred;
79 	struct vattr vat;
80 	struct vattr *vap = &vat;
81 	int mode, error;
82 
83 	if (fmode & O_CREAT) {
84 		ndp->ni_cnd.cn_nameiop = CREATE;
85 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
86 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
87 			ndp->ni_cnd.cn_flags |= FOLLOW;
88 		error = namei(ndp);
89 		if (error)
90 			return (error);
91 		if (ndp->ni_vp == NULL) {
92 			VATTR_NULL(vap);
93 			vap->va_type = VREG;
94 			vap->va_mode = cmode;
95 			if (fmode & O_EXCL)
96 				vap->va_vaflags |= VA_EXCLUSIVE;
97 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
98 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
99 					   &ndp->ni_cnd, vap);
100 			vput(ndp->ni_dvp);
101 			if (error)
102 				return (error);
103 			ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
104 			ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
105 			fmode &= ~O_TRUNC;
106 			vp = ndp->ni_vp;
107 		} else {
108 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
109 			if (ndp->ni_dvp == ndp->ni_vp)
110 				vrele(ndp->ni_dvp);
111 			else
112 				vput(ndp->ni_dvp);
113 			ndp->ni_dvp = NULL;
114 			vp = ndp->ni_vp;
115 			if (fmode & O_EXCL) {
116 				error = EEXIST;
117 				goto bad;
118 			}
119 			fmode &= ~O_CREAT;
120 		}
121 	} else {
122 		ndp->ni_cnd.cn_nameiop = LOOKUP;
123 		ndp->ni_cnd.cn_flags =
124 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
125 		error = namei(ndp);
126 		if (error)
127 			return (error);
128 		vp = ndp->ni_vp;
129 	}
130 	if (vp->v_type == VLNK) {
131 		error = EMLINK;
132 		goto bad;
133 	}
134 	if (vp->v_type == VSOCK) {
135 		error = EOPNOTSUPP;
136 		goto bad;
137 	}
138 	if ((fmode & O_CREAT) == 0) {
139 		mode = 0;
140 		if (fmode & (FWRITE | O_TRUNC)) {
141 			if (vp->v_type == VDIR) {
142 				error = EISDIR;
143 				goto bad;
144 			}
145 			error = vn_writechk(vp);
146 			if (error)
147 				goto bad;
148 			mode |= VWRITE;
149 		}
150 		if (fmode & FREAD)
151 			mode |= VREAD;
152 		if (mode) {
153 		        error = VOP_ACCESS(vp, mode, cred, p);
154 			if (error)
155 				goto bad;
156 		}
157 	}
158 	if (fmode & O_TRUNC) {
159 		VOP_UNLOCK(vp, 0, p);				/* XXX */
160 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
161 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);	/* XXX */
162 		VATTR_NULL(vap);
163 		vap->va_size = 0;
164 		error = VOP_SETATTR(vp, vap, cred, p);
165 		if (error)
166 			goto bad;
167 	}
168 	error = VOP_OPEN(vp, fmode, cred, p);
169 	if (error)
170 		goto bad;
171 	/*
172 	 * Make sure that a VM object is created for VMIO support.
173 	 */
174 	if (vp->v_type == VREG) {
175 		if ((error = vfs_object_create(vp, p, cred, 1)) != 0)
176 			goto bad;
177 	}
178 
179 	if (fmode & FWRITE)
180 		vp->v_writecount++;
181 	return (0);
182 bad:
183 	vput(vp);
184 	return (error);
185 }
186 
187 /*
188  * Check for write permissions on the specified vnode.
189  * Prototype text segments cannot be written.
190  */
191 int
192 vn_writechk(vp)
193 	register struct vnode *vp;
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)
202 		return (ETXTBSY);
203 	return (0);
204 }
205 
206 /*
207  * Vnode close call
208  */
209 int
210 vn_close(vp, flags, cred, p)
211 	register struct vnode *vp;
212 	int flags;
213 	struct ucred *cred;
214 	struct proc *p;
215 {
216 	int error;
217 
218 	if (flags & FWRITE)
219 		vp->v_writecount--;
220 	error = VOP_CLOSE(vp, flags, cred, p);
221 	vrele(vp);
222 	return (error);
223 }
224 
225 /*
226  * Package up an I/O request on a vnode into a uio and do it.
227  */
228 int
229 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
230 	enum uio_rw rw;
231 	struct vnode *vp;
232 	caddr_t base;
233 	int len;
234 	off_t offset;
235 	enum uio_seg segflg;
236 	int ioflg;
237 	struct ucred *cred;
238 	int *aresid;
239 	struct proc *p;
240 {
241 	struct uio auio;
242 	struct iovec aiov;
243 	int error;
244 
245 	if ((ioflg & IO_NODELOCKED) == 0)
246 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
247 	auio.uio_iov = &aiov;
248 	auio.uio_iovcnt = 1;
249 	aiov.iov_base = base;
250 	aiov.iov_len = len;
251 	auio.uio_resid = len;
252 	auio.uio_offset = offset;
253 	auio.uio_segflg = segflg;
254 	auio.uio_rw = rw;
255 	auio.uio_procp = p;
256 	if (rw == UIO_READ) {
257 		error = VOP_READ(vp, &auio, ioflg, cred);
258 	} else {
259 		error = VOP_WRITE(vp, &auio, ioflg, cred);
260 	}
261 	if (aresid)
262 		*aresid = auio.uio_resid;
263 	else
264 		if (auio.uio_resid && error == 0)
265 			error = EIO;
266 	if ((ioflg & IO_NODELOCKED) == 0)
267 		VOP_UNLOCK(vp, 0, p);
268 	return (error);
269 }
270 
271 /*
272  * File table vnode read routine.
273  */
274 static int
275 vn_read(fp, uio, cred)
276 	struct file *fp;
277 	struct uio *uio;
278 	struct ucred *cred;
279 {
280 	struct vnode *vp = (struct vnode *)fp->f_data;
281 	struct proc *p = uio->uio_procp;
282 	int count, error;
283 	int flag;
284 
285 	VOP_LEASE(vp, p, cred, LEASE_READ);
286 	vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
287 	if (uio->uio_offset == -1)
288 		uio->uio_offset = fp->f_offset;
289 	count = uio->uio_resid;
290 	flag = 0;
291 	if (fp->f_flag & FNONBLOCK)
292 		flag |= IO_NDELAY;
293 
294 	/*
295 	 * Sequential read heuristic.
296 	 * If we have been doing sequential input,
297 	 * a rewind operation doesn't turn off
298 	 * sequential input mode.
299 	 */
300 	if (((fp->f_offset == 0) && (fp->f_seqcount > 0)) ||
301 		(fp->f_offset == fp->f_nextread)) {
302 		int tmpseq = fp->f_seqcount;
303 		/*
304 		 * XXX we assume that the filesystem block size is
305 		 * the default.  Not true, but still gives us a pretty
306 		 * good indicator of how sequential the read operations
307 		 * are.
308 		 */
309 		tmpseq += ((count + BKVASIZE - 1) / BKVASIZE);
310 		if (tmpseq >= 127)
311 			tmpseq = 127;
312 		fp->f_seqcount = tmpseq;
313 		flag |= (fp->f_seqcount << 16);
314 	} else {
315 		if (fp->f_seqcount > 1)
316 			fp->f_seqcount = 1;
317 		else
318 			fp->f_seqcount = 0;
319 	}
320 
321 	error = VOP_READ(vp, uio, flag, cred);
322 	fp->f_offset += count - uio->uio_resid;
323 	fp->f_nextread = fp->f_offset;
324 	VOP_UNLOCK(vp, 0, p);
325 	return (error);
326 }
327 
328 /*
329  * File table vnode write routine.
330  */
331 static int
332 vn_write(fp, uio, cred)
333 	struct file *fp;
334 	struct uio *uio;
335 	struct ucred *cred;
336 {
337 	struct vnode *vp = (struct vnode *)fp->f_data;
338 	struct proc *p = uio->uio_procp;
339 	int count, error, ioflag = IO_UNIT;
340 
341 	if (uio->uio_offset == -1 && vp->v_type == VREG && (fp->f_flag & O_APPEND))
342 		ioflag |= IO_APPEND;
343 	if (fp->f_flag & FNONBLOCK)
344 		ioflag |= IO_NDELAY;
345 	if ((fp->f_flag & O_FSYNC) ||
346 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
347 		ioflag |= IO_SYNC;
348 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
349 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
350 	uio->uio_offset = fp->f_offset;
351 	count = uio->uio_resid;
352 	error = VOP_WRITE(vp, uio, ioflag, cred);
353 	if (ioflag & IO_APPEND)
354 		fp->f_offset = uio->uio_offset;
355 	else
356 		fp->f_offset += count - uio->uio_resid;
357 	VOP_UNLOCK(vp, 0, p);
358 	return (error);
359 }
360 
361 /*
362  * File table vnode stat routine.
363  */
364 int
365 vn_stat(vp, sb, p)
366 	struct vnode *vp;
367 	register struct stat *sb;
368 	struct proc *p;
369 {
370 	struct vattr vattr;
371 	register struct vattr *vap;
372 	int error;
373 	u_short mode;
374 
375 	vap = &vattr;
376 	error = VOP_GETATTR(vp, vap, p->p_ucred, p);
377 	if (error)
378 		return (error);
379 	/*
380 	 * Copy from vattr table
381 	 */
382 	sb->st_dev = vap->va_fsid;
383 	sb->st_ino = vap->va_fileid;
384 	mode = vap->va_mode;
385 	switch (vap->va_type) {
386 	case VREG:
387 		mode |= S_IFREG;
388 		break;
389 	case VDIR:
390 		mode |= S_IFDIR;
391 		break;
392 	case VBLK:
393 		mode |= S_IFBLK;
394 		break;
395 	case VCHR:
396 		mode |= S_IFCHR;
397 		break;
398 	case VLNK:
399 		mode |= S_IFLNK;
400 		/* This is a cosmetic change, symlinks do not have a mode. */
401 		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
402 			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
403 		else
404 			sb->st_mode |= ACCESSPERMS;	/* 0777 */
405 		break;
406 	case VSOCK:
407 		mode |= S_IFSOCK;
408 		break;
409 	case VFIFO:
410 		mode |= S_IFIFO;
411 		break;
412 	default:
413 		return (EBADF);
414 	};
415 	sb->st_mode = mode;
416 	sb->st_nlink = vap->va_nlink;
417 	sb->st_uid = vap->va_uid;
418 	sb->st_gid = vap->va_gid;
419 	sb->st_rdev = vap->va_rdev;
420 	sb->st_size = vap->va_size;
421 	sb->st_atimespec = vap->va_atime;
422 	sb->st_mtimespec = vap->va_mtime;
423 	sb->st_ctimespec = vap->va_ctime;
424 	sb->st_blksize = vap->va_blocksize;
425 	sb->st_flags = vap->va_flags;
426 	if (p->p_ucred->cr_uid != 0)
427 		sb->st_gen = 0;
428 	else
429 		sb->st_gen = vap->va_gen;
430 
431 #if (S_BLKSIZE == 512)
432 	/* Optimize this case */
433 	sb->st_blocks = vap->va_bytes >> 9;
434 #else
435 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
436 #endif
437 	return (0);
438 }
439 
440 /*
441  * File table vnode ioctl routine.
442  */
443 static int
444 vn_ioctl(fp, com, data, p)
445 	struct file *fp;
446 	u_long com;
447 	caddr_t data;
448 	struct proc *p;
449 {
450 	register struct vnode *vp = ((struct vnode *)fp->f_data);
451 	struct vattr vattr;
452 	int error;
453 
454 	switch (vp->v_type) {
455 
456 	case VREG:
457 	case VDIR:
458 		if (com == FIONREAD) {
459 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
460 			if (error)
461 				return (error);
462 			*(int *)data = vattr.va_size - fp->f_offset;
463 			return (0);
464 		}
465 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
466 			return (0);			/* XXX */
467 		/* fall into ... */
468 
469 	default:
470 #if 0
471 		return (ENOTTY);
472 #endif
473 	case VFIFO:
474 	case VCHR:
475 	case VBLK:
476 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
477 		if (error == 0 && com == TIOCSCTTY) {
478 
479 			/* Do nothing if reassigning same control tty */
480 			if (p->p_session->s_ttyvp == vp)
481 				return (0);
482 
483 			/* Get rid of reference to old control tty */
484 			if (p->p_session->s_ttyvp)
485 				vrele(p->p_session->s_ttyvp);
486 
487 			p->p_session->s_ttyvp = vp;
488 			VREF(vp);
489 		}
490 		return (error);
491 	}
492 }
493 
494 /*
495  * File table vnode poll routine.
496  */
497 static int
498 vn_poll(fp, events, cred, p)
499 	struct file *fp;
500 	int events;
501 	struct ucred *cred;
502 	struct proc *p;
503 {
504 
505 	return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p));
506 }
507 
508 /*
509  * Check that the vnode is still valid, and if so
510  * acquire requested lock.
511  */
512 int
513 vn_lock(vp, flags, p)
514 	struct vnode *vp;
515 	int flags;
516 	struct proc *p;
517 {
518 	int error;
519 
520 	do {
521 		if ((flags & LK_INTERLOCK) == 0)
522 			simple_lock(&vp->v_interlock);
523 		if (vp->v_flag & VXLOCK) {
524 			vp->v_flag |= VXWANT;
525 			simple_unlock(&vp->v_interlock);
526 			tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
527 			error = ENOENT;
528 		} else {
529 			error = VOP_LOCK(vp, flags | LK_NOPAUSE | LK_INTERLOCK, p);
530 			if (error == 0)
531 				return (error);
532 		}
533 		flags &= ~LK_INTERLOCK;
534 	} while (flags & LK_RETRY);
535 	return (error);
536 }
537 
538 /*
539  * File table vnode close routine.
540  */
541 static int
542 vn_closefile(fp, p)
543 	struct file *fp;
544 	struct proc *p;
545 {
546 
547 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
548 		fp->f_cred, p));
549 }
550