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