xref: /freebsd/sys/kern/vfs_vnops.c (revision d82e286489da73321a47e329d98a98817b0438b6)
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.18 1995/10/06 09:43:32 phk 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 	 * 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 	/*
222 	 * this code is here for VMIO support, will eventually
223 	 * be in vfs code.
224 	 */
225 	if (vp->v_flag & VVMIO) {
226 		vrele(vp);
227 		if (vp->v_object == NULL)
228 			panic("vn_close: VMIO object missing");
229 		vm_object_deallocate(vp->v_object);
230 	} else
231 		vrele(vp);
232 	return (error);
233 }
234 
235 /*
236  * Package up an I/O request on a vnode into a uio and do it.
237  */
238 int
239 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
240 	enum uio_rw rw;
241 	struct vnode *vp;
242 	caddr_t base;
243 	int len;
244 	off_t offset;
245 	enum uio_seg segflg;
246 	int ioflg;
247 	struct ucred *cred;
248 	int *aresid;
249 	struct proc *p;
250 {
251 	struct uio auio;
252 	struct iovec aiov;
253 	int error;
254 
255 	if ((ioflg & IO_NODELOCKED) == 0)
256 		VOP_LOCK(vp);
257 	auio.uio_iov = &aiov;
258 	auio.uio_iovcnt = 1;
259 	aiov.iov_base = base;
260 	aiov.iov_len = len;
261 	auio.uio_resid = len;
262 	auio.uio_offset = offset;
263 	auio.uio_segflg = segflg;
264 	auio.uio_rw = rw;
265 	auio.uio_procp = p;
266 	if (rw == UIO_READ) {
267 		error = VOP_READ(vp, &auio, ioflg, cred);
268 	} else {
269 		error = VOP_WRITE(vp, &auio, ioflg, cred);
270 	}
271 	if (aresid)
272 		*aresid = auio.uio_resid;
273 	else
274 		if (auio.uio_resid && error == 0)
275 			error = EIO;
276 	if ((ioflg & IO_NODELOCKED) == 0)
277 		VOP_UNLOCK(vp);
278 	return (error);
279 }
280 
281 /*
282  * File table vnode read routine.
283  */
284 int
285 vn_read(fp, uio, cred)
286 	struct file *fp;
287 	struct uio *uio;
288 	struct ucred *cred;
289 {
290 	register struct vnode *vp = (struct vnode *)fp->f_data;
291 	int count, error;
292 
293 	LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
294 	VOP_LOCK(vp);
295 	uio->uio_offset = fp->f_offset;
296 	count = uio->uio_resid;
297 	error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
298 		cred);
299 	fp->f_offset += count - uio->uio_resid;
300 	VOP_UNLOCK(vp);
301 	return (error);
302 }
303 
304 /*
305  * File table vnode write routine.
306  */
307 int
308 vn_write(fp, uio, cred)
309 	struct file *fp;
310 	struct uio *uio;
311 	struct ucred *cred;
312 {
313 	register struct vnode *vp = (struct vnode *)fp->f_data;
314 	int count, error, ioflag = 0;
315 
316 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
317 		ioflag |= IO_APPEND;
318 	if (fp->f_flag & FNONBLOCK)
319 		ioflag |= IO_NDELAY;
320 	LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
321 	VOP_LOCK(vp);
322 	uio->uio_offset = fp->f_offset;
323 	count = uio->uio_resid;
324 	error = VOP_WRITE(vp, uio, ioflag, cred);
325 	if (ioflag & IO_APPEND)
326 		fp->f_offset = uio->uio_offset;
327 	else
328 		fp->f_offset += count - uio->uio_resid;
329 	VOP_UNLOCK(vp);
330 	return (error);
331 }
332 
333 /*
334  * File table vnode stat routine.
335  */
336 int
337 vn_stat(vp, sb, p)
338 	struct vnode *vp;
339 	register struct stat *sb;
340 	struct proc *p;
341 {
342 	struct vattr vattr;
343 	register struct vattr *vap;
344 	int error;
345 	u_short mode;
346 
347 	vap = &vattr;
348 	error = VOP_GETATTR(vp, vap, p->p_ucred, p);
349 	if (error)
350 		return (error);
351 	/*
352 	 * Copy from vattr table
353 	 */
354 	sb->st_dev = vap->va_fsid;
355 	sb->st_ino = vap->va_fileid;
356 	mode = vap->va_mode;
357 	switch (vp->v_type) {
358 	case VREG:
359 		mode |= S_IFREG;
360 		break;
361 	case VDIR:
362 		mode |= S_IFDIR;
363 		break;
364 	case VBLK:
365 		mode |= S_IFBLK;
366 		break;
367 	case VCHR:
368 		mode |= S_IFCHR;
369 		break;
370 	case VLNK:
371 		mode |= S_IFLNK;
372 		break;
373 	case VSOCK:
374 		mode |= S_IFSOCK;
375 		break;
376 	case VFIFO:
377 		mode |= S_IFIFO;
378 		break;
379 	default:
380 		return (EBADF);
381 	};
382 	sb->st_mode = mode;
383 	sb->st_nlink = vap->va_nlink;
384 	sb->st_uid = vap->va_uid;
385 	sb->st_gid = vap->va_gid;
386 	sb->st_rdev = vap->va_rdev;
387 	sb->st_size = vap->va_size;
388 	sb->st_atimespec = vap->va_atime;
389 	sb->st_mtimespec= vap->va_mtime;
390 	sb->st_ctimespec = vap->va_ctime;
391 	sb->st_blksize = vap->va_blocksize;
392 	sb->st_flags = vap->va_flags;
393 	sb->st_gen = vap->va_gen;
394 #if (S_BLKSIZE == 512)
395 	/* Optimize this case */
396 	sb->st_blocks = vap->va_bytes >> 9;
397 #else
398 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
399 #endif
400 	return (0);
401 }
402 
403 /*
404  * File table vnode ioctl routine.
405  */
406 int
407 vn_ioctl(fp, com, data, p)
408 	struct file *fp;
409 	int com;
410 	caddr_t data;
411 	struct proc *p;
412 {
413 	register struct vnode *vp = ((struct vnode *)fp->f_data);
414 	struct vattr vattr;
415 	int error;
416 
417 	switch (vp->v_type) {
418 
419 	case VREG:
420 	case VDIR:
421 		if (com == FIONREAD) {
422 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
423 			if (error)
424 				return (error);
425 			*(int *)data = vattr.va_size - fp->f_offset;
426 			return (0);
427 		}
428 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
429 			return (0);			/* XXX */
430 		/* fall into ... */
431 
432 	default:
433 		return (ENOTTY);
434 
435 	case VFIFO:
436 	case VCHR:
437 	case VBLK:
438 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
439 		if (error == 0 && com == TIOCSCTTY) {
440 
441 			/* Do nothing if reassigning same control tty */
442 			if (p->p_session->s_ttyvp == vp)
443 				return (0);
444 
445 			/* Get rid of reference to old control tty */
446 			if (p->p_session->s_ttyvp)
447 				vrele(p->p_session->s_ttyvp);
448 
449 			p->p_session->s_ttyvp = vp;
450 			VREF(vp);
451 		}
452 		return (error);
453 	}
454 }
455 
456 /*
457  * File table vnode select routine.
458  */
459 int
460 vn_select(fp, which, p)
461 	struct file *fp;
462 	int which;
463 	struct proc *p;
464 {
465 
466 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
467 		fp->f_cred, p));
468 }
469 
470 /*
471  * File table vnode close routine.
472  */
473 int
474 vn_closefile(fp, p)
475 	struct file *fp;
476 	struct proc *p;
477 {
478 
479 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
480 		fp->f_cred, p));
481 }
482