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