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