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