xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 38257)
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.6 (Berkeley) 06/07/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 	return (VOP_OPEN(vp, fmode, ndp->ni_cred));
118 
119 bad:
120 	vput(vp);
121 	return(error);
122 }
123 
124 /*
125  * Check mode permission on vnode pointer. Mode is READ, WRITE or EXEC.
126  * In the case of WRITE, the read-only status of the file system is
127  * checked. Also in WRITE, prototype text segments cannot be written.
128  */
129 vn_access(vp, mode, cred)
130 	register struct vnode *vp;
131 	int mode;
132 	struct ucred *cred;
133 {
134 
135 	if (mode & VWRITE) {
136 		/*
137 		 * Disallow write attempts on read-only file systems;
138 		 * unless the file is a socket or a block or character
139 		 * device resident on the file system.
140 		 */
141 		if ((vp->v_mount->m_flag & M_RDONLY) &&
142 			vp->v_type != VCHR &&
143 			vp->v_type != VBLK &&
144 			vp->v_type != VSOCK)
145 				return (EROFS);
146 		/*
147 		 * If there's shared text associated with
148 		 * the inode, try to free it up once.  If
149 		 * we fail, we can't allow writing.
150 		 */
151 		if (vp->v_flag & VTEXT)
152 			xrele(vp);
153 		if (vp->v_flag & VTEXT)
154 			return (ETXTBSY);
155 	}
156 	return (VOP_ACCESS(vp, mode, cred));
157 }
158 
159 /*
160  * Vnode version of rdwri() for calls on file systems.
161  */
162 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid)
163 	enum uio_rw rw;
164 	struct vnode *vp;
165 	caddr_t base;
166 	int len;
167 	off_t offset;
168 	enum uio_seg segflg;
169 	int ioflg;
170 	struct ucred *cred;
171 	int *aresid;
172 {
173 	struct uio auio;
174 	struct iovec aiov;
175 	int error;
176 
177 	auio.uio_iov = &aiov;
178 	auio.uio_iovcnt = 1;
179 	aiov.iov_base = base;
180 	aiov.iov_len = len;
181 	auio.uio_resid = len;
182 	auio.uio_offset = offset;
183 	auio.uio_segflg = segflg;
184 	auio.uio_rw = rw;
185 	if (rw == UIO_READ)
186 		error = VOP_READ(vp, &auio, &offset, ioflg, cred);
187 	else
188 		error = VOP_WRITE(vp, &auio, &offset, ioflg, cred);
189 	if (aresid)
190 		*aresid = auio.uio_resid;
191 	else
192 		if (auio.uio_resid && error == 0)
193 			error = EIO;
194 	return (error);
195 }
196 
197 vn_read(fp, uio, cred)
198 	struct file *fp;
199 	struct uio *uio;
200 	struct ucred *cred;
201 {
202 
203 	return (VOP_READ((struct vnode *)fp->f_data, uio, &(fp->f_offset),
204 		(fp->f_flag & FNDELAY) ? IO_NDELAY : 0, cred));
205 }
206 
207 vn_write(fp, uio, cred)
208 	struct file *fp;
209 	struct uio *uio;
210 	struct ucred *cred;
211 {
212 	register struct vnode *vp = (struct vnode *)fp->f_data;
213 	int ioflag = 0;
214 
215 	if (vp->v_type == VREG && (fp->f_flag & FAPPEND))
216 		ioflag |= IO_APPEND;
217 	if (fp->f_flag & FNDELAY)
218 		ioflag |= IO_NDELAY;
219 	return (VOP_WRITE(vp, uio, &(fp->f_offset), ioflag, cred));
220 }
221 
222 /*
223  * Get stat info for a vnode.
224  */
225 vn_stat(vp, sb)
226 	struct vnode *vp;
227 	register struct stat *sb;
228 {
229 	struct vattr vattr;
230 	register struct vattr *vap;
231 	int error;
232 	u_short mode;
233 
234 	vap = &vattr;
235 	error = VOP_GETATTR(vp, vap, u.u_cred);
236 	if (error)
237 		return (error);
238 	/*
239 	 * Copy from vattr table
240 	 */
241 	sb->st_dev = vap->va_fsid;
242 	sb->st_ino = vap->va_fileid;
243 	mode = vap->va_mode;
244 	switch (vp->v_type) {
245 	case VREG:
246 		mode |= IFREG;
247 		break;
248 	case VDIR:
249 		mode |= IFDIR;
250 		break;
251 	case VBLK:
252 		mode |= IFBLK;
253 		break;
254 	case VCHR:
255 		mode |= IFCHR;
256 		break;
257 	case VLNK:
258 		mode |= IFLNK;
259 		break;
260 	case VSOCK:
261 		mode |= IFSOCK;
262 		break;
263 	default:
264 		return (EBADF);
265 	};
266 	sb->st_mode = mode;
267 	sb->st_nlink = vap->va_nlink;
268 	sb->st_uid = vap->va_uid;
269 	sb->st_gid = vap->va_gid;
270 	sb->st_rdev = vap->va_rdev;
271 	sb->st_size = vap->va_size;
272 	sb->st_atime = vap->va_atime.tv_sec;
273 	sb->st_spare1 = 0;
274 	sb->st_mtime = vap->va_mtime.tv_sec;
275 	sb->st_spare2 = 0;
276 	sb->st_ctime = vap->va_ctime.tv_sec;
277 	sb->st_spare3 = 0;
278 	sb->st_blksize = vap->va_blocksize;
279 	sb->st_flags = vap->va_flags;
280 	sb->st_gen = vap->va_gen;
281 	/*
282 	 * XXX THIS IS NOT CORRECT!!, but be sure to change ufs_getattr()
283 	 * if you change it.
284 	 */
285 	sb->st_blocks = vap->va_bytes;
286 	return (0);
287 }
288 
289 /*
290  * Vnode ioctl call
291  */
292 vn_ioctl(fp, com, data)
293 	struct file *fp;
294 	int com;
295 	caddr_t data;
296 {
297 	register struct vnode *vp = ((struct vnode *)fp->f_data);
298 	struct vattr vattr;
299 	int error;
300 
301 	switch (vp->v_type) {
302 
303 	case VREG:
304 	case VDIR:
305 		if (com == FIONREAD) {
306 			if (error = VOP_GETATTR(vp, &vattr, u.u_cred))
307 				return (error);
308 			*(off_t *)data = vattr.va_size - fp->f_offset;
309 			return (0);
310 		}
311 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
312 			return (0);			/* XXX */
313 		/* fall into ... */
314 
315 	default:
316 		return (ENOTTY);
317 
318 	case VCHR:
319 	case VBLK:
320 		u.u_r.r_val1 = 0;
321 		if (setjmp(&u.u_qsave)) {
322 			if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
323 				return(EINTR);
324 			u.u_eosys = RESTARTSYS;
325 			return (0);
326 		}
327 		return (VOP_IOCTL(vp, com, data, fp->f_flag, u.u_cred));
328 	}
329 }
330 
331 /*
332  * Vnode select call
333  */
334 vn_select(fp, which)
335 	struct file *fp;
336 	int which;
337 {
338 	return(VOP_SELECT(((struct vnode *)fp->f_data), which, u.u_cred));
339 }
340 
341 /*
342  * Vnode close call
343  */
344 vn_close(fp)
345 	register struct file *fp;
346 {
347 	struct vnode *vp = ((struct vnode *)fp->f_data);
348 	int error;
349 
350 	if (fp->f_flag & (FSHLOCK|FEXLOCK))
351 		vn_unlock(fp, FSHLOCK|FEXLOCK);
352 	/*
353 	 * Must delete vnode reference from this file entry
354 	 * before VOP_CLOSE, so that only other references
355 	 * will prevent close.
356 	 */
357 	fp->f_data = (caddr_t) 0;
358 	error = VOP_CLOSE(vp, fp->f_flag, u.u_cred);
359 	vrele(vp);
360 	return (error);
361 }
362 
363 /*
364  * Place an advisory lock on a vnode.
365  * !! THIS IMPLIES THAT ALL STATEFUL FILE SERVERS WILL USE file table entries
366  */
367 vn_lock(fp, cmd)
368 	register struct file *fp;
369 	int cmd;
370 {
371 	register int priority = PLOCK;
372 	register struct vnode *vp = (struct vnode *)fp->f_data;
373 
374 	if ((cmd & LOCK_EX) == 0)
375 		priority += 4;
376 	if (setjmp(&u.u_qsave)) {
377 		if ((u.u_sigintr & sigmask(u.u_procp->p_cursig)) != 0)
378 			return(EINTR);
379 		u.u_eosys = RESTARTSYS;
380 		return (0);
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 		sleep((caddr_t)&vp->v_exlockc, priority);
401 	}
402 	if ((cmd & LOCK_EX) && (vp->v_flag & VSHLOCK)) {
403 		/*
404 		 * Must wait for any shared locks to finish
405 		 * before we try to apply a exclusive lock.
406 		 *
407 		 * If we're holding a shared
408 		 * lock, then release it.
409 		 */
410 		if (fp->f_flag & FSHLOCK) {
411 			vn_unlock(fp, FSHLOCK);
412 			goto again;
413 		}
414 		if (cmd & LOCK_NB)
415 			return (EWOULDBLOCK);
416 		vp->v_flag |= VLWAIT;
417 		sleep((caddr_t)&vp->v_shlockc, PLOCK);
418 		goto again;
419 	}
420 	if (fp->f_flag & FEXLOCK)
421 		panic("vn_lock");
422 	if (cmd & LOCK_EX) {
423 		cmd &= ~LOCK_SH;
424 		vp->v_exlockc++;
425 		vp->v_flag |= VEXLOCK;
426 		fp->f_flag |= FEXLOCK;
427 	}
428 	if ((cmd & LOCK_SH) && (fp->f_flag & FSHLOCK) == 0) {
429 		vp->v_shlockc++;
430 		vp->v_flag |= VSHLOCK;
431 		fp->f_flag |= FSHLOCK;
432 	}
433 	return (0);
434 }
435 
436 /*
437  * Unlock a file.
438  */
439 vn_unlock(fp, kind)
440 	register struct file *fp;
441 	int kind;
442 {
443 	register struct vnode *vp = (struct vnode *)fp->f_data;
444 	int flags;
445 
446 	kind &= fp->f_flag;
447 	if (vp == NULL || kind == 0)
448 		return;
449 	flags = vp->v_flag;
450 	if (kind & FSHLOCK) {
451 		if ((flags & VSHLOCK) == 0)
452 			panic("vn_unlock: SHLOCK");
453 		if (--vp->v_shlockc == 0) {
454 			vp->v_flag &= ~VSHLOCK;
455 			if (flags & VLWAIT)
456 				wakeup((caddr_t)&vp->v_shlockc);
457 		}
458 		fp->f_flag &= ~FSHLOCK;
459 	}
460 	if (kind & FEXLOCK) {
461 		if ((flags & VEXLOCK) == 0)
462 			panic("vn_unlock: EXLOCK");
463 		if (--vp->v_exlockc == 0) {
464 			vp->v_flag &= ~(VEXLOCK|VLWAIT);
465 			if (flags & VLWAIT)
466 				wakeup((caddr_t)&vp->v_exlockc);
467 		}
468 		fp->f_flag &= ~FEXLOCK;
469 	}
470 }
471 
472 /*
473  * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked)
474  * 	- look up fsid in mount list (if not found ret error)
475  *	- get vp by calling VFS_FHTOVP() macro
476  *	- if lockflag lock it with VOP_LOCK()
477  */
478 vn_fhtovp(fhp, lockflag, vpp)
479 	fhandle_t *fhp;
480 	int lockflag;
481 	struct vnode **vpp;
482 {
483 	register struct mount *mp;
484 	int error;
485 
486 	if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
487 		return (ESTALE);
488 	if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
489 		return (ESTALE);
490 	if (!lockflag)
491 		VOP_UNLOCK(*vpp);
492 	return (0);
493 }
494 
495 /*
496  * Revoke access the current tty by all processes.
497  * Used only by the super-user in init
498  * to give ``clean'' terminals at login.
499  */
500 vhangup()
501 {
502 
503 	if (u.u_error = suser(u.u_cred, &u.u_acflag))
504 		return;
505 	if (u.u_ttyp == NULL)
506 		return;
507 	forceclose(u.u_ttyd);
508 	if ((u.u_ttyp->t_state) & TS_ISOPEN)
509 		gsignal(u.u_ttyp->t_pgid, SIGHUP);
510 	u.u_ttyp->t_session = 0;
511 	u.u_ttyp->t_pgid = 0;
512 }
513 
514 forceclose(dev)
515 	dev_t dev;
516 {
517 	register struct file *fp;
518 	register struct vnode *vp;
519 
520 	for (fp = file; fp < fileNFILE; fp++) {
521 		if (fp->f_count == 0)
522 			continue;
523 		if (fp->f_type != DTYPE_VNODE)
524 			continue;
525 		vp = (struct vnode *)fp->f_data;
526 		if (vp == 0)
527 			continue;
528 		if (vp->v_type != VCHR)
529 			continue;
530 		if (vp->v_rdev != dev)
531 			continue;
532 		fp->f_flag &= ~(FREAD|FWRITE);
533 	}
534 }
535 
536 /*
537  * Vnode release, just decrement the count and call VOP_INACTIVE()
538  */
539 void vrele(vp)
540 	register struct vnode *vp;
541 {
542 
543 	if (vp == NULL)
544 		return;
545 	vp->v_count--;
546 	if (vp->v_count < 0)
547 		printf("inode %d, bad ref count %d\n",
548 			VTOI(vp)->i_number, vp->v_count);
549 	if (vp->v_count == 0)
550 		VOP_INACTIVE(vp);
551 }
552 
553 /*
554  * vput(), just unlock and vrele()
555  */
556 vput(vp)
557 	register struct vnode *vp;
558 {
559 	VOP_UNLOCK(vp);
560 	vrele(vp);
561 }
562 
563 /*
564  * Noop
565  */
566 vfs_noop()
567 {
568 
569 	return (ENXIO);
570 }
571 
572 /*
573  * Null op
574  */
575 vfs_nullop()
576 {
577 
578 	return (0);
579 }
580