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