xref: /original-bsd/sys/kern/vfs_vnops.c (revision dd262573)
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.25 (Berkeley) 12/05/90
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "user.h"
13 #include "kernel.h"
14 #include "file.h"
15 #include "stat.h"
16 #include "buf.h"
17 #include "proc.h"
18 #include "uio.h"
19 #include "socket.h"
20 #include "socketvar.h"
21 #include "mount.h"
22 #include "vnode.h"
23 #include "ioctl.h"
24 #include "tty.h"
25 
26 int	vn_read(), vn_write(), vn_ioctl(), vn_select(), vn_close();
27 struct 	fileops vnops =
28 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_close };
29 
30 /*
31  * Common code for vnode open operations.
32  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
33  */
34 vn_open(ndp, fmode, cmode)
35 	register struct nameidata *ndp;
36 	int fmode, cmode;
37 {
38 	register struct vnode *vp;
39 	struct vattr vat;
40 	struct vattr *vap = &vat;
41 	int error;
42 
43 	if (fmode & FCREAT) {
44 		ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF;
45 		if ((fmode & FEXCL) == 0)
46 			ndp->ni_nameiop |= FOLLOW;
47 		if (error = namei(ndp))
48 			return (error);
49 		if (ndp->ni_vp == NULL) {
50 			VATTR_NULL(vap);
51 			vap->va_type = VREG;
52 			vap->va_mode = cmode;
53 			if (error = VOP_CREATE(ndp, vap))
54 				return (error);
55 			fmode &= ~FTRUNC;
56 			vp = ndp->ni_vp;
57 		} else {
58 			if (ndp->ni_dvp == ndp->ni_vp)
59 				vrele(ndp->ni_dvp);
60 			else
61 				vput(ndp->ni_dvp);
62 			ndp->ni_dvp = NULL;
63 			vp = ndp->ni_vp;
64 			if (fmode & FEXCL) {
65 				error = EEXIST;
66 				goto bad;
67 			}
68 			fmode &= ~FCREAT;
69 		}
70 	} else {
71 		ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
72 		if (error = namei(ndp))
73 			return (error);
74 		vp = ndp->ni_vp;
75 	}
76 	if (vp->v_type == VSOCK) {
77 		error = EOPNOTSUPP;
78 		goto bad;
79 	}
80 	if ((fmode & FCREAT) == 0) {
81 		if (fmode & FREAD) {
82 			if (error = VOP_ACCESS(vp, VREAD, ndp->ni_cred))
83 				goto bad;
84 		}
85 		if (fmode & (FWRITE|FTRUNC)) {
86 			if (vp->v_type == VDIR) {
87 				error = EISDIR;
88 				goto bad;
89 			}
90 			if ((error = vn_writechk(vp)) ||
91 			    (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred)))
92 				goto bad;
93 		}
94 	}
95 	if (fmode & FTRUNC) {
96 		VATTR_NULL(vap);
97 		vap->va_size = 0;
98 		if (error = VOP_SETATTR(vp, vap, ndp->ni_cred))
99 			goto bad;
100 	}
101 	VOP_UNLOCK(vp);
102 	error = VOP_OPEN(vp, fmode, ndp->ni_cred);
103 	if (error)
104 		vrele(vp);
105 	return (error);
106 
107 bad:
108 	vput(vp);
109 	return (error);
110 }
111 
112 /*
113  * Check for write permissions on the specified vnode.
114  * The read-only status of the file system is checked.
115  * Also, prototype text segments cannot be written.
116  */
117 vn_writechk(vp)
118 	register struct vnode *vp;
119 {
120 
121 	/*
122 	 * Disallow write attempts on read-only file systems;
123 	 * unless the file is a socket or a block or character
124 	 * device resident on the file system.
125 	 */
126 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
127 		switch (vp->v_type) {
128 		case VREG: case VDIR: case VLNK:
129 			return (EROFS);
130 		}
131 	}
132 	/*
133 	 * If there's shared text associated with
134 	 * the vnode, try to free it up once.  If
135 	 * we fail, we can't allow writing.
136 	 */
137 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
138 		return (ETXTBSY);
139 	return (0);
140 }
141 
142 /*
143  * Vnode version of rdwri() for calls on file systems.
144  */
145 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid)
146 	enum uio_rw rw;
147 	struct vnode *vp;
148 	caddr_t base;
149 	int len;
150 	off_t offset;
151 	enum uio_seg segflg;
152 	int ioflg;
153 	struct ucred *cred;
154 	int *aresid;
155 {
156 	struct uio auio;
157 	struct iovec aiov;
158 	int error;
159 
160 	if ((ioflg & IO_NODELOCKED) == 0)
161 		VOP_LOCK(vp);
162 	auio.uio_iov = &aiov;
163 	auio.uio_iovcnt = 1;
164 	aiov.iov_base = base;
165 	aiov.iov_len = len;
166 	auio.uio_resid = len;
167 	auio.uio_offset = offset;
168 	auio.uio_segflg = segflg;
169 	auio.uio_rw = rw;
170 	if (rw == UIO_READ)
171 		error = VOP_READ(vp, &auio, ioflg, cred);
172 	else
173 		error = VOP_WRITE(vp, &auio, ioflg, cred);
174 	if (aresid)
175 		*aresid = auio.uio_resid;
176 	else
177 		if (auio.uio_resid && error == 0)
178 			error = EIO;
179 	if ((ioflg & IO_NODELOCKED) == 0)
180 		VOP_UNLOCK(vp);
181 	return (error);
182 }
183 
184 vn_read(fp, uio, cred)
185 	struct file *fp;
186 	struct uio *uio;
187 	struct ucred *cred;
188 {
189 	register struct vnode *vp = (struct vnode *)fp->f_data;
190 	int count, error;
191 
192 	VOP_LOCK(vp);
193 	uio->uio_offset = fp->f_offset;
194 	count = uio->uio_resid;
195 	error = VOP_READ(vp, uio, (fp->f_flag & FNDELAY) ? IO_NDELAY : 0, cred);
196 	fp->f_offset += count - uio->uio_resid;
197 	VOP_UNLOCK(vp);
198 	return (error);
199 }
200 
201 vn_write(fp, uio, cred)
202 	struct file *fp;
203 	struct uio *uio;
204 	struct ucred *cred;
205 {
206 	register struct vnode *vp = (struct vnode *)fp->f_data;
207 	int count, error, ioflag = 0;
208 
209 	if (vp->v_type == VREG && (fp->f_flag & FAPPEND))
210 		ioflag |= IO_APPEND;
211 	if (fp->f_flag & FNDELAY)
212 		ioflag |= IO_NDELAY;
213 	VOP_LOCK(vp);
214 	uio->uio_offset = fp->f_offset;
215 	count = uio->uio_resid;
216 	error = VOP_WRITE(vp, uio, ioflag, cred);
217 	if (ioflag & IO_APPEND)
218 		fp->f_offset = uio->uio_offset;
219 	else
220 		fp->f_offset += count - uio->uio_resid;
221 	VOP_UNLOCK(vp);
222 	return (error);
223 }
224 
225 /*
226  * Get stat info for a vnode.
227  */
228 vn_stat(vp, sb)
229 	struct vnode *vp;
230 	register struct stat *sb;
231 {
232 	struct vattr vattr;
233 	register struct vattr *vap;
234 	int error;
235 	u_short mode;
236 
237 	vap = &vattr;
238 	error = VOP_GETATTR(vp, vap, u.u_cred);
239 	if (error)
240 		return (error);
241 	/*
242 	 * Copy from vattr table
243 	 */
244 	sb->st_dev = vap->va_fsid;
245 	sb->st_ino = vap->va_fileid;
246 	mode = vap->va_mode;
247 	switch (vp->v_type) {
248 	case VREG:
249 		mode |= S_IFREG;
250 		break;
251 	case VDIR:
252 		mode |= S_IFDIR;
253 		break;
254 	case VBLK:
255 		mode |= S_IFBLK;
256 		break;
257 	case VCHR:
258 		mode |= S_IFCHR;
259 		break;
260 	case VLNK:
261 		mode |= S_IFLNK;
262 		break;
263 	case VSOCK:
264 		mode |= S_IFSOCK;
265 		break;
266 	case VFIFO:
267 		mode |= S_IFIFO;
268 		break;
269 	default:
270 		return (EBADF);
271 	};
272 	sb->st_mode = mode;
273 	sb->st_nlink = vap->va_nlink;
274 	sb->st_uid = vap->va_uid;
275 	sb->st_gid = vap->va_gid;
276 	sb->st_rdev = vap->va_rdev;
277 	sb->st_size = vap->va_size;
278 	sb->st_atime = vap->va_atime.tv_sec;
279 	sb->st_spare1 = 0;
280 	sb->st_mtime = vap->va_mtime.tv_sec;
281 	sb->st_spare2 = 0;
282 	sb->st_ctime = vap->va_ctime.tv_sec;
283 	sb->st_spare3 = 0;
284 	sb->st_blksize = vap->va_blocksize;
285 	sb->st_flags = vap->va_flags;
286 	sb->st_gen = vap->va_gen;
287 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
288 	return (0);
289 }
290 
291 /*
292  * Vnode ioctl call
293  */
294 vn_ioctl(fp, com, data)
295 	struct file *fp;
296 	int com;
297 	caddr_t data;
298 {
299 	register struct vnode *vp = ((struct vnode *)fp->f_data);
300 	struct vattr vattr;
301 	int error;
302 
303 	switch (vp->v_type) {
304 
305 	case VREG:
306 	case VDIR:
307 		if (com == FIONREAD) {
308 			if (error = VOP_GETATTR(vp, &vattr, u.u_cred))
309 				return (error);
310 			*(off_t *)data = vattr.va_size - fp->f_offset;
311 			return (0);
312 		}
313 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
314 			return (0);			/* XXX */
315 		/* fall into ... */
316 
317 	default:
318 		return (ENOTTY);
319 
320 	case VFIFO:
321 	case VCHR:
322 	case VBLK:
323 		error = VOP_IOCTL(vp, com, data, fp->f_flag, u.u_cred);
324 		if (error == 0 && com == TIOCSCTTY) {
325 			u.u_procp->p_session->s_ttyvp = vp;
326 			VREF(vp);
327 		}
328 		return (error);
329 	}
330 }
331 
332 /*
333  * Vnode select call
334  */
335 vn_select(fp, which)
336 	struct file *fp;
337 	int which;
338 {
339 	return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
340 		u.u_cred));
341 }
342 
343 /*
344  * Vnode close call
345  */
346 vn_close(fp)
347 	register struct file *fp;
348 {
349 	struct vnode *vp = ((struct vnode *)fp->f_data);
350 	int error;
351 
352 	if (fp->f_flag & (FSHLOCK|FEXLOCK))
353 		vn_unlock(fp, FSHLOCK|FEXLOCK);
354 	/*
355 	 * Must delete vnode reference from this file entry
356 	 * before VOP_CLOSE, so that only other references
357 	 * will prevent close.
358 	 */
359 	fp->f_data = (caddr_t) 0;
360 	error = VOP_CLOSE(vp, fp->f_flag, u.u_cred);
361 	vrele(vp);
362 	return (error);
363 }
364 
365 /*
366  * Place an advisory lock on a vnode.
367  * !! THIS IMPLIES THAT ALL STATEFUL FILE SERVERS WILL USE file table entries
368  */
369 vn_lock(fp, cmd)
370 	register struct file *fp;
371 	int cmd;
372 {
373 	register int priority = PLOCK;
374 	register struct vnode *vp = (struct vnode *)fp->f_data;
375 	int error = 0;
376 	static char lockstr[] = "flock";
377 
378 	if ((cmd & LOCK_EX) == 0)
379 		priority += 4;
380 	priority |= PCATCH;
381 
382 	/*
383 	 * If there's a exclusive lock currently applied
384 	 * to the file, then we've gotta wait for the
385 	 * lock with everyone else.
386 	 */
387 again:
388 	while (vp->v_flag & VEXLOCK) {
389 		/*
390 		 * If we're holding an exclusive
391 		 * lock, then release it.
392 		 */
393 		if (fp->f_flag & FEXLOCK) {
394 			vn_unlock(fp, FEXLOCK);
395 			continue;
396 		}
397 		if (cmd & LOCK_NB)
398 			return (EWOULDBLOCK);
399 		vp->v_flag |= VLWAIT;
400 		if (error = tsleep((caddr_t)&vp->v_exlockc, priority,
401 		    lockstr, 0))
402 			return (error);
403 	}
404 	if (error == 0 && (cmd & LOCK_EX) && (vp->v_flag & VSHLOCK)) {
405 		/*
406 		 * Must wait for any shared locks to finish
407 		 * before we try to apply a exclusive lock.
408 		 *
409 		 * If we're holding a shared
410 		 * lock, then release it.
411 		 */
412 		if (fp->f_flag & FSHLOCK) {
413 			vn_unlock(fp, FSHLOCK);
414 			goto again;
415 		}
416 		if (cmd & LOCK_NB)
417 			return (EWOULDBLOCK);
418 		vp->v_flag |= VLWAIT;
419 		if (error = tsleep((caddr_t)&vp->v_shlockc, PLOCK | PCATCH,
420 		    lockstr, 0))
421 			return (error);
422 	}
423 	if (fp->f_flag & FEXLOCK)
424 		panic("vn_lock");
425 	if (cmd & LOCK_EX) {
426 		cmd &= ~LOCK_SH;
427 		vp->v_exlockc++;
428 		vp->v_flag |= VEXLOCK;
429 		fp->f_flag |= FEXLOCK;
430 	}
431 	if ((cmd & LOCK_SH) && (fp->f_flag & FSHLOCK) == 0) {
432 		vp->v_shlockc++;
433 		vp->v_flag |= VSHLOCK;
434 		fp->f_flag |= FSHLOCK;
435 	}
436 	return (0);
437 }
438 
439 /*
440  * Unlock a file.
441  */
442 vn_unlock(fp, kind)
443 	register struct file *fp;
444 	int kind;
445 {
446 	register struct vnode *vp = (struct vnode *)fp->f_data;
447 	int flags;
448 
449 	kind &= fp->f_flag;
450 	if (vp == NULL || kind == 0)
451 		return;
452 	flags = vp->v_flag;
453 	if (kind & FSHLOCK) {
454 		if ((flags & VSHLOCK) == 0)
455 			panic("vn_unlock: SHLOCK");
456 		if (--vp->v_shlockc == 0) {
457 			vp->v_flag &= ~VSHLOCK;
458 			if (flags & VLWAIT)
459 				wakeup((caddr_t)&vp->v_shlockc);
460 		}
461 		fp->f_flag &= ~FSHLOCK;
462 	}
463 	if (kind & FEXLOCK) {
464 		if ((flags & VEXLOCK) == 0)
465 			panic("vn_unlock: EXLOCK");
466 		if (--vp->v_exlockc == 0) {
467 			vp->v_flag &= ~(VEXLOCK|VLWAIT);
468 			if (flags & VLWAIT)
469 				wakeup((caddr_t)&vp->v_exlockc);
470 		}
471 		fp->f_flag &= ~FEXLOCK;
472 	}
473 }
474 
475 /*
476  * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked)
477  * 	- look up fsid in mount list (if not found ret error)
478  *	- get vp by calling VFS_FHTOVP() macro
479  *	- if lockflag lock it with VOP_LOCK()
480  */
481 vn_fhtovp(fhp, lockflag, vpp)
482 	fhandle_t *fhp;
483 	int lockflag;
484 	struct vnode **vpp;
485 {
486 	register struct mount *mp;
487 
488 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
489 		return (ESTALE);
490 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
491 		return (ESTALE);
492 	if (!lockflag)
493 		VOP_UNLOCK(*vpp);
494 	return (0);
495 }
496 
497 /*
498  * Noop
499  */
500 vfs_noop()
501 {
502 
503 	return (ENXIO);
504 }
505 
506 /*
507  * Null op
508  */
509 vfs_nullop()
510 {
511 
512 	return (0);
513 }
514