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