xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 38351)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)vfs_vnops.c	7.7 (Berkeley) 06/27/89
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "user.h"
23 #include "kernel.h"
24 #include "file.h"
25 #include "stat.h"
26 #include "buf.h"
27 #include "proc.h"
28 #include "uio.h"
29 #include "socket.h"
30 #include "socketvar.h"
31 #include "mount.h"
32 #include "vnode.h"
33 #include "../ufs/inode.h"
34 #include "../ufs/fs.h"
35 #include "../ufs/quota.h"
36 #include "ioctl.h"
37 #include "tty.h"
38 
39 int	vn_read(), vn_write(), vn_ioctl(), vn_select(), vn_close();
40 struct 	fileops vnops =
41 	{ vn_read, vn_write, vn_ioctl, vn_select, vn_close };
42 
43 /*
44  * Common code for vnode open operations.
45  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
46  */
47 vn_open(ndp, fmode, cmode)
48 	register struct nameidata *ndp;
49 	int fmode, cmode;
50 {
51 	register struct vnode *vp;
52 	struct vattr vat;
53 	struct vattr *vap = &vat;
54 	int error;
55 
56 	if (fmode & FCREAT) {
57 		ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF;
58 		if ((fmode & FEXCL) == 0)
59 			ndp->ni_nameiop |= FOLLOW;
60 		if (error = namei(ndp))
61 			return (error);
62 		if (ndp->ni_vp == NULL) {
63 			vattr_null(vap);
64 			vap->va_type = VREG;
65 			vap->va_mode = cmode;
66 			if (error = VOP_CREATE(ndp, vap))
67 				return (error);
68 			fmode &= ~FTRUNC;
69 			vp = ndp->ni_vp;
70 		} else {
71 			vp = ndp->ni_vp;
72 			ndp->ni_vp = 0;
73 			VOP_ABORTOP(ndp);
74 			ndp->ni_vp = vp;
75 			if (fmode & FEXCL) {
76 				error = EEXIST;
77 				goto bad;
78 			}
79 			fmode &= ~FCREAT;
80 		}
81 	} else {
82 		ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
83 		if (error = namei(ndp))
84 			return (error);
85 		vp = ndp->ni_vp;
86 	}
87 	if (vp->v_type == VSOCK) {
88 		error = EOPNOTSUPP;
89 		goto bad;
90 	}
91 	if ((fmode & FCREAT) == 0) {
92 		if (fmode & FREAD) {
93 			if (error = vn_access(vp, VREAD, ndp->ni_cred))
94 				goto bad;
95 		}
96 		if (fmode & (FWRITE|FTRUNC)) {
97 			if (error = vn_access(vp, VWRITE, ndp->ni_cred))
98 				goto bad;
99 			if (vp->v_type == VDIR) {
100 				error = EISDIR;
101 				goto bad;
102 			}
103 		}
104 	}
105 	if (fmode & FTRUNC) {
106 		vattr_null(vap);
107 		vap->va_size = 0;
108 		if (error = VOP_SETATTR(vp, vap, ndp->ni_cred))
109 			goto bad;
110 	}
111 	VOP_UNLOCK(vp);
112 	if (setjmp(&u.u_qsave)) {
113 		if (error == 0)
114 			error = EINTR;
115 		return (error);
116 	}
117 	error = VOP_OPEN(vp, fmode, ndp->ni_cred);
118 	if (error)
119 		vrele(vp);
120 	return (error);
121 
122 bad:
123 	vput(vp);
124 	return(error);
125 }
126 
127 /*
128  * Check mode permission on vnode pointer. Mode is READ, WRITE or EXEC.
129  * In the case of WRITE, the read-only status of the file system is
130  * checked. Also in WRITE, prototype text segments cannot be written.
131  */
132 vn_access(vp, mode, cred)
133 	register struct vnode *vp;
134 	int mode;
135 	struct ucred *cred;
136 {
137 
138 	if (mode & VWRITE) {
139 		/*
140 		 * Disallow write attempts on read-only file systems;
141 		 * unless the file is a socket or a block or character
142 		 * device resident on the file system.
143 		 */
144 		if ((vp->v_mount->m_flag & M_RDONLY) &&
145 			vp->v_type != VCHR &&
146 			vp->v_type != VBLK &&
147 			vp->v_type != VSOCK)
148 				return (EROFS);
149 		/*
150 		 * If there's shared text associated with
151 		 * the inode, try to free it up once.  If
152 		 * we fail, we can't allow writing.
153 		 */
154 		if (vp->v_flag & VTEXT)
155 			xrele(vp);
156 		if (vp->v_flag & VTEXT)
157 			return (ETXTBSY);
158 	}
159 	return (VOP_ACCESS(vp, mode, cred));
160 }
161 
162 /*
163  * Vnode version of rdwri() for calls on file systems.
164  */
165 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid)
166 	enum uio_rw rw;
167 	struct vnode *vp;
168 	caddr_t base;
169 	int len;
170 	off_t offset;
171 	enum uio_seg segflg;
172 	int ioflg;
173 	struct ucred *cred;
174 	int *aresid;
175 {
176 	struct uio auio;
177 	struct iovec aiov;
178 	int error;
179 
180 	auio.uio_iov = &aiov;
181 	auio.uio_iovcnt = 1;
182 	aiov.iov_base = base;
183 	aiov.iov_len = len;
184 	auio.uio_resid = len;
185 	auio.uio_offset = offset;
186 	auio.uio_segflg = segflg;
187 	auio.uio_rw = rw;
188 	if (rw == UIO_READ)
189 		error = VOP_READ(vp, &auio, &offset, ioflg, cred);
190 	else
191 		error = VOP_WRITE(vp, &auio, &offset, ioflg, cred);
192 	if (aresid)
193 		*aresid = auio.uio_resid;
194 	else
195 		if (auio.uio_resid && error == 0)
196 			error = EIO;
197 	return (error);
198 }
199 
200 vn_read(fp, uio, cred)
201 	struct file *fp;
202 	struct uio *uio;
203 	struct ucred *cred;
204 {
205 
206 	return (VOP_READ((struct vnode *)fp->f_data, uio, &(fp->f_offset),
207 		(fp->f_flag & FNDELAY) ? IO_NDELAY : 0, cred));
208 }
209 
210 vn_write(fp, uio, cred)
211 	struct file *fp;
212 	struct uio *uio;
213 	struct ucred *cred;
214 {
215 	register struct vnode *vp = (struct vnode *)fp->f_data;
216 	int ioflag = 0;
217 
218 	if (vp->v_type == VREG && (fp->f_flag & FAPPEND))
219 		ioflag |= IO_APPEND;
220 	if (fp->f_flag & FNDELAY)
221 		ioflag |= IO_NDELAY;
222 	return (VOP_WRITE(vp, uio, &(fp->f_offset), ioflag, cred));
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 |= IFREG;
250 		break;
251 	case VDIR:
252 		mode |= IFDIR;
253 		break;
254 	case VBLK:
255 		mode |= IFBLK;
256 		break;
257 	case VCHR:
258 		mode |= IFCHR;
259 		break;
260 	case VLNK:
261 		mode |= IFLNK;
262 		break;
263 	case VSOCK:
264 		mode |= IFSOCK;
265 		break;
266 	default:
267 		return (EBADF);
268 	};
269 	sb->st_mode = mode;
270 	sb->st_nlink = vap->va_nlink;
271 	sb->st_uid = vap->va_uid;
272 	sb->st_gid = vap->va_gid;
273 	sb->st_rdev = vap->va_rdev;
274 	sb->st_size = vap->va_size;
275 	sb->st_atime = vap->va_atime.tv_sec;
276 	sb->st_spare1 = 0;
277 	sb->st_mtime = vap->va_mtime.tv_sec;
278 	sb->st_spare2 = 0;
279 	sb->st_ctime = vap->va_ctime.tv_sec;
280 	sb->st_spare3 = 0;
281 	sb->st_blksize = vap->va_blocksize;
282 	sb->st_flags = vap->va_flags;
283 	sb->st_gen = vap->va_gen;
284 	/*
285 	 * XXX THIS IS NOT CORRECT!!, but be sure to change ufs_getattr()
286 	 * if you change it.
287 	 */
288 	sb->st_blocks = vap->va_bytes;
289 	return (0);
290 }
291 
292 /*
293  * Vnode ioctl call
294  */
295 vn_ioctl(fp, com, data)
296 	struct file *fp;
297 	int com;
298 	caddr_t data;
299 {
300 	register struct vnode *vp = ((struct vnode *)fp->f_data);
301 	struct vattr vattr;
302 	int error;
303 
304 	switch (vp->v_type) {
305 
306 	case VREG:
307 	case VDIR:
308 		if (com == FIONREAD) {
309 			if (error = VOP_GETATTR(vp, &vattr, u.u_cred))
310 				return (error);
311 			*(off_t *)data = vattr.va_size - fp->f_offset;
312 			return (0);
313 		}
314 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
315 			return (0);			/* XXX */
316 		/* fall into ... */
317 
318 	default:
319 		return (ENOTTY);
320 
321 	case VCHR:
322 	case VBLK:
323 		u.u_r.r_val1 = 0;
324 		if (setjmp(&u.u_qsave)) {
325 			if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
326 				return(EINTR);
327 			u.u_eosys = RESTARTSYS;
328 			return (0);
329 		}
330 		return (VOP_IOCTL(vp, com, data, fp->f_flag, u.u_cred));
331 	}
332 }
333 
334 /*
335  * Vnode select call
336  */
337 vn_select(fp, which)
338 	struct file *fp;
339 	int which;
340 {
341 	return(VOP_SELECT(((struct vnode *)fp->f_data), which, u.u_cred));
342 }
343 
344 /*
345  * Vnode close call
346  */
347 vn_close(fp)
348 	register struct file *fp;
349 {
350 	struct vnode *vp = ((struct vnode *)fp->f_data);
351 	int error;
352 
353 	if (fp->f_flag & (FSHLOCK|FEXLOCK))
354 		vn_unlock(fp, FSHLOCK|FEXLOCK);
355 	/*
356 	 * Must delete vnode reference from this file entry
357 	 * before VOP_CLOSE, so that only other references
358 	 * will prevent close.
359 	 */
360 	fp->f_data = (caddr_t) 0;
361 	error = VOP_CLOSE(vp, fp->f_flag, u.u_cred);
362 	vrele(vp);
363 	return (error);
364 }
365 
366 /*
367  * Place an advisory lock on a vnode.
368  * !! THIS IMPLIES THAT ALL STATEFUL FILE SERVERS WILL USE file table entries
369  */
370 vn_lock(fp, cmd)
371 	register struct file *fp;
372 	int cmd;
373 {
374 	register int priority = PLOCK;
375 	register struct vnode *vp = (struct vnode *)fp->f_data;
376 
377 	if ((cmd & LOCK_EX) == 0)
378 		priority += 4;
379 	if (setjmp(&u.u_qsave)) {
380 		if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
381 			return(EINTR);
382 		u.u_eosys = RESTARTSYS;
383 		return (0);
384 	}
385 	/*
386 	 * If there's a exclusive lock currently applied
387 	 * to the file, then we've gotta wait for the
388 	 * lock with everyone else.
389 	 */
390 again:
391 	while (vp->v_flag & VEXLOCK) {
392 		/*
393 		 * If we're holding an exclusive
394 		 * lock, then release it.
395 		 */
396 		if (fp->f_flag & FEXLOCK) {
397 			vn_unlock(fp, FEXLOCK);
398 			continue;
399 		}
400 		if (cmd & LOCK_NB)
401 			return (EWOULDBLOCK);
402 		vp->v_flag |= VLWAIT;
403 		sleep((caddr_t)&vp->v_exlockc, priority);
404 	}
405 	if ((cmd & LOCK_EX) && (vp->v_flag & VSHLOCK)) {
406 		/*
407 		 * Must wait for any shared locks to finish
408 		 * before we try to apply a exclusive lock.
409 		 *
410 		 * If we're holding a shared
411 		 * lock, then release it.
412 		 */
413 		if (fp->f_flag & FSHLOCK) {
414 			vn_unlock(fp, FSHLOCK);
415 			goto again;
416 		}
417 		if (cmd & LOCK_NB)
418 			return (EWOULDBLOCK);
419 		vp->v_flag |= VLWAIT;
420 		sleep((caddr_t)&vp->v_shlockc, PLOCK);
421 		goto again;
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 	int error;
488 
489 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
490 		return (ESTALE);
491 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
492 		return (ESTALE);
493 	if (!lockflag)
494 		VOP_UNLOCK(*vpp);
495 	return (0);
496 }
497 
498 /*
499  * Revoke access the current tty by all processes.
500  * Used only by the super-user in init
501  * to give ``clean'' terminals at login.
502  */
503 vhangup()
504 {
505 
506 	if (u.u_error = suser(u.u_cred, &u.u_acflag))
507 		return;
508 	if (u.u_ttyp == NULL)
509 		return;
510 	forceclose(u.u_ttyd);
511 	if ((u.u_ttyp->t_state) & TS_ISOPEN)
512 		gsignal(u.u_ttyp->t_pgid, SIGHUP);
513 	u.u_ttyp->t_session = 0;
514 	u.u_ttyp->t_pgid = 0;
515 }
516 
517 forceclose(dev)
518 	dev_t dev;
519 {
520 	register struct file *fp;
521 	register struct vnode *vp;
522 
523 	for (fp = file; fp < fileNFILE; fp++) {
524 		if (fp->f_count == 0)
525 			continue;
526 		if (fp->f_type != DTYPE_VNODE)
527 			continue;
528 		vp = (struct vnode *)fp->f_data;
529 		if (vp == 0)
530 			continue;
531 		if (vp->v_type != VCHR)
532 			continue;
533 		if (vp->v_rdev != dev)
534 			continue;
535 		fp->f_flag &= ~(FREAD|FWRITE);
536 	}
537 }
538 
539 /*
540  * Vnode reference, just increment the count
541  */
542 void vref(vp)
543 	struct vnode *vp;
544 {
545 
546 	vp->v_count++;
547 }
548 
549 /*
550  * Vnode release, just decrement the count and call VOP_INACTIVE()
551  */
552 void vrele(vp)
553 	register struct vnode *vp;
554 {
555 
556 	if (vp == NULL)
557 		return;
558 	vp->v_count--;
559 	if (vp->v_count < 0)
560 		printf("inode %d, bad ref count %d\n",
561 			VTOI(vp)->i_number, vp->v_count);
562 	if (vp->v_count == 0)
563 		VOP_INACTIVE(vp);
564 }
565 
566 /*
567  * vput(), just unlock and vrele()
568  */
569 vput(vp)
570 	register struct vnode *vp;
571 {
572 	VOP_UNLOCK(vp);
573 	vrele(vp);
574 }
575 
576 /*
577  * Noop
578  */
579 vfs_noop()
580 {
581 
582 	return (ENXIO);
583 }
584 
585 /*
586  * Null op
587  */
588 vfs_nullop()
589 {
590 
591 	return (0);
592 }
593