xref: /netbsd-src/sys/kern/vfs_vnops.c (revision 705ee976cfe4e85b18732519865b1f7b6cbb2322)
1 /*	$NetBSD: vfs_vnops.c,v 1.16 1994/11/14 06:01:24 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/ioctl.h>
54 #include <sys/tty.h>
55 
56 #include <vm/vm.h>
57 
58 struct 	fileops vnops =
59 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
60 
61 /*
62  * Common code for vnode open operations.
63  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
64  */
65 vn_open(ndp, fmode, cmode, fp)
66 	register struct nameidata *ndp;
67 	int fmode, cmode;
68 	struct file *fp;
69 {
70 	register struct vnode *vp;
71 	register struct proc *p = ndp->ni_cnd.cn_proc;
72 	register struct ucred *cred = p->p_ucred;
73 	struct vattr vat;
74 	struct vattr *vap = &vat;
75 	int error;
76 
77 	if (fmode & O_CREAT) {
78 		ndp->ni_cnd.cn_nameiop = CREATE;
79 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
80 		if ((fmode & O_EXCL) == 0)
81 			ndp->ni_cnd.cn_flags |= FOLLOW;
82 		if (error = namei(ndp))
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 			if (error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
90 			    &ndp->ni_cnd, vap))
91 				return (error);
92 			fmode &= ~O_TRUNC;
93 			vp = ndp->ni_vp;
94 		} else {
95 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
96 			if (ndp->ni_dvp == ndp->ni_vp)
97 				vrele(ndp->ni_dvp);
98 			else
99 				vput(ndp->ni_dvp);
100 			ndp->ni_dvp = NULL;
101 			vp = ndp->ni_vp;
102 			if (fmode & O_EXCL) {
103 				error = EEXIST;
104 				goto bad;
105 			}
106 			fmode &= ~O_CREAT;
107 		}
108 	} else {
109 		ndp->ni_cnd.cn_nameiop = LOOKUP;
110 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
111 		if (error = namei(ndp))
112 			return (error);
113 		vp = ndp->ni_vp;
114 	}
115 	if (vp->v_type == VSOCK) {
116 		error = EOPNOTSUPP;
117 		goto bad;
118 	}
119 	if ((fmode & O_CREAT) == 0) {
120 		if (fmode & FREAD) {
121 			if (error = VOP_ACCESS(vp, VREAD, cred, p))
122 				goto bad;
123 		}
124 		if (fmode & (FWRITE | O_TRUNC)) {
125 			if (vp->v_type == VDIR) {
126 				error = EISDIR;
127 				goto bad;
128 			}
129 			if ((error = vn_writechk(vp)) ||
130 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)))
131 				goto bad;
132 		}
133 	}
134 	if (fmode & O_TRUNC) {
135 		VOP_UNLOCK(vp);				/* XXX */
136 		LEASE_CHECK(vp, p, cred, LEASE_WRITE);
137 		VOP_LOCK(vp);				/* XXX */
138 		VATTR_NULL(vap);
139 		vap->va_size = 0;
140 		if (error = VOP_SETATTR(vp, vap, cred, p))
141 			goto bad;
142 	}
143 	if (error = VOP_OPEN(vp, fmode, cred, p, fp))
144 		goto bad;
145 	if (fmode & FWRITE)
146 		vp->v_writecount++;
147 	return (0);
148 bad:
149 	vput(vp);
150 	return (error);
151 }
152 
153 /*
154  * Check for write permissions on the specified vnode.
155  * The read-only status of the file system is checked.
156  * Also, prototype text segments cannot be written.
157  */
158 vn_writechk(vp)
159 	register struct vnode *vp;
160 {
161 
162 	/*
163 	 * Disallow write attempts on read-only file systems;
164 	 * unless the file is a socket or a block or character
165 	 * device resident on the file system.
166 	 */
167 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
168 		switch (vp->v_type) {
169 		case VREG: case VDIR: case VLNK:
170 			return (EROFS);
171 		}
172 	}
173 	/*
174 	 * If there's shared text associated with
175 	 * the vnode, try to free it up once.  If
176 	 * we fail, we can't allow writing.
177 	 */
178 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
179 		return (ETXTBSY);
180 	return (0);
181 }
182 
183 /*
184  * Vnode close call
185  */
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 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
205 	enum uio_rw rw;
206 	struct vnode *vp;
207 	caddr_t base;
208 	int len;
209 	off_t offset;
210 	enum uio_seg segflg;
211 	int ioflg;
212 	struct ucred *cred;
213 	int *aresid;
214 	struct proc *p;
215 {
216 	struct uio auio;
217 	struct iovec aiov;
218 	int error;
219 
220 	if ((ioflg & IO_NODELOCKED) == 0)
221 		VOP_LOCK(vp);
222 	auio.uio_iov = &aiov;
223 	auio.uio_iovcnt = 1;
224 	aiov.iov_base = base;
225 	aiov.iov_len = len;
226 	auio.uio_resid = len;
227 	auio.uio_offset = offset;
228 	auio.uio_segflg = segflg;
229 	auio.uio_rw = rw;
230 	auio.uio_procp = p;
231 	if (rw == UIO_READ) {
232 		error = VOP_READ(vp, &auio, ioflg, cred);
233 	} else {
234 		error = VOP_WRITE(vp, &auio, ioflg, cred);
235 	}
236 	if (aresid)
237 		*aresid = auio.uio_resid;
238 	else
239 		if (auio.uio_resid && error == 0)
240 			error = EIO;
241 	if ((ioflg & IO_NODELOCKED) == 0)
242 		VOP_UNLOCK(vp);
243 	return (error);
244 }
245 
246 /*
247  * File table vnode read routine.
248  */
249 vn_read(fp, uio, cred)
250 	struct file *fp;
251 	struct uio *uio;
252 	struct ucred *cred;
253 {
254 	register struct vnode *vp = (struct vnode *)fp->f_data;
255 	int count, error;
256 
257 	LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
258 	VOP_LOCK(vp);
259 	uio->uio_offset = fp->f_offset;
260 	count = uio->uio_resid;
261 	error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
262 		cred);
263 	fp->f_offset += count - uio->uio_resid;
264 	VOP_UNLOCK(vp);
265 	return (error);
266 }
267 
268 /*
269  * File table vnode write routine.
270  */
271 vn_write(fp, uio, cred)
272 	struct file *fp;
273 	struct uio *uio;
274 	struct ucred *cred;
275 {
276 	register struct vnode *vp = (struct vnode *)fp->f_data;
277 	int count, error, ioflag = 0;
278 
279 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
280 		ioflag |= IO_APPEND;
281 	if (fp->f_flag & FNONBLOCK)
282 		ioflag |= IO_NDELAY;
283 	LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
284 	VOP_LOCK(vp);
285 	uio->uio_offset = fp->f_offset;
286 	count = uio->uio_resid;
287 	error = VOP_WRITE(vp, uio, ioflag, cred);
288 	if (ioflag & IO_APPEND)
289 		fp->f_offset = uio->uio_offset;
290 	else
291 		fp->f_offset += count - uio->uio_resid;
292 	VOP_UNLOCK(vp);
293 	return (error);
294 }
295 
296 /*
297  * File table vnode stat routine.
298  */
299 vn_stat(vp, sb, p)
300 	struct vnode *vp;
301 	register struct stat *sb;
302 	struct proc *p;
303 {
304 	struct vattr vattr;
305 	register struct vattr *vap;
306 	int error;
307 	u_short mode;
308 
309 	vap = &vattr;
310 	error = VOP_GETATTR(vp, vap, p->p_ucred, p);
311 	if (error)
312 		return (error);
313 	/*
314 	 * Copy from vattr table
315 	 */
316 	sb->st_dev = vap->va_fsid;
317 	sb->st_ino = vap->va_fileid;
318 	mode = vap->va_mode;
319 	switch (vp->v_type) {
320 	case VREG:
321 		mode |= S_IFREG;
322 		break;
323 	case VDIR:
324 		mode |= S_IFDIR;
325 		break;
326 	case VBLK:
327 		mode |= S_IFBLK;
328 		break;
329 	case VCHR:
330 		mode |= S_IFCHR;
331 		break;
332 	case VLNK:
333 		mode |= S_IFLNK;
334 		break;
335 	case VSOCK:
336 		mode |= S_IFSOCK;
337 		break;
338 	case VFIFO:
339 		mode |= S_IFIFO;
340 		break;
341 	default:
342 		return (EBADF);
343 	};
344 	sb->st_mode = mode;
345 	sb->st_nlink = vap->va_nlink;
346 	sb->st_uid = vap->va_uid;
347 	sb->st_gid = vap->va_gid;
348 	sb->st_rdev = vap->va_rdev;
349 	sb->st_size = vap->va_size;
350 	sb->st_atimespec = vap->va_atime;
351 	sb->st_mtimespec = vap->va_mtime;
352 	sb->st_ctimespec = vap->va_ctime;
353 	sb->st_blksize = vap->va_blocksize;
354 	sb->st_flags = vap->va_flags;
355 	sb->st_gen = vap->va_gen;
356 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
357 	return (0);
358 }
359 
360 /*
361  * File table vnode ioctl routine.
362  */
363 vn_ioctl(fp, com, data, p)
364 	struct file *fp;
365 	u_long com;
366 	caddr_t data;
367 	struct proc *p;
368 {
369 	register struct vnode *vp = ((struct vnode *)fp->f_data);
370 	struct vattr vattr;
371 	int error;
372 
373 	switch (vp->v_type) {
374 
375 	case VREG:
376 	case VDIR:
377 		if (com == FIONREAD) {
378 			if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
379 				return (error);
380 			*(int *)data = vattr.va_size - fp->f_offset;
381 			return (0);
382 		}
383 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
384 			return (0);			/* XXX */
385 		/* fall into ... */
386 
387 	default:
388 		return (ENOTTY);
389 
390 	case VFIFO:
391 	case VCHR:
392 	case VBLK:
393 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
394 		if (error == 0 && com == TIOCSCTTY) {
395 			if (p->p_session->s_ttyvp)
396 				vrele(p->p_session->s_ttyvp);
397 			p->p_session->s_ttyvp = vp;
398 			VREF(vp);
399 		}
400 		return (error);
401 	}
402 }
403 
404 /*
405  * File table vnode select routine.
406  */
407 vn_select(fp, which, p)
408 	struct file *fp;
409 	int which;
410 	struct proc *p;
411 {
412 
413 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
414 		fp->f_cred, p));
415 }
416 
417 /*
418  * File table vnode close routine.
419  */
420 vn_closefile(fp, p)
421 	struct file *fp;
422 	struct proc *p;
423 {
424 
425 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
426 		fp->f_cred, p));
427 }
428