vfs_vnops.c revision 1.55 1 /* $NetBSD: vfs_vnops.c,v 1.55 2002/10/05 22:34:05 chs Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.55 2002/10/05 22:34:05 chs Exp $");
45
46 #include "fs_union.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/buf.h>
54 #include <sys/proc.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/ioctl.h>
59 #include <sys/tty.h>
60 #include <sys/poll.h>
61
62 #include <uvm/uvm_extern.h>
63
64 #ifdef UNION
65 #include <miscfs/union/union.h>
66 #endif
67
68 static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
69
70 struct fileops vnops = {
71 vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
72 vn_statfile, vn_closefile
73 };
74
75 /*
76 * Common code for vnode open operations.
77 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
78 */
79 int
80 vn_open(ndp, fmode, cmode)
81 struct nameidata *ndp;
82 int fmode, cmode;
83 {
84 struct vnode *vp;
85 struct proc *p = ndp->ni_cnd.cn_proc;
86 struct ucred *cred = p->p_ucred;
87 struct vattr va;
88 int error;
89
90 if (fmode & O_CREAT) {
91 ndp->ni_cnd.cn_nameiop = CREATE;
92 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
93 if ((fmode & O_EXCL) == 0 &&
94 ((fmode & FNOSYMLINK) == 0))
95 ndp->ni_cnd.cn_flags |= FOLLOW;
96 if ((error = namei(ndp)) != 0)
97 return (error);
98 if (ndp->ni_vp == NULL) {
99 VATTR_NULL(&va);
100 va.va_type = VREG;
101 va.va_mode = cmode;
102 if (fmode & O_EXCL)
103 va.va_vaflags |= VA_EXCLUSIVE;
104 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
105 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
106 &ndp->ni_cnd, &va);
107 if (error)
108 return (error);
109 fmode &= ~O_TRUNC;
110 vp = ndp->ni_vp;
111 } else {
112 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
113 if (ndp->ni_dvp == ndp->ni_vp)
114 vrele(ndp->ni_dvp);
115 else
116 vput(ndp->ni_dvp);
117 ndp->ni_dvp = NULL;
118 vp = ndp->ni_vp;
119 if (fmode & O_EXCL) {
120 error = EEXIST;
121 goto bad;
122 }
123 if (ndp->ni_vp->v_type == VLNK) {
124 error = EFTYPE;
125 goto bad;
126 }
127 fmode &= ~O_CREAT;
128 }
129 } else {
130 ndp->ni_cnd.cn_nameiop = LOOKUP;
131 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
132 if ((error = namei(ndp)) != 0)
133 return (error);
134 vp = ndp->ni_vp;
135 }
136 if (vp->v_type == VSOCK) {
137 error = EOPNOTSUPP;
138 goto bad;
139 }
140 if ((fmode & O_CREAT) == 0) {
141 if (fmode & FREAD) {
142 if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
143 goto bad;
144 }
145 if (fmode & (FWRITE | O_TRUNC)) {
146 if (vp->v_type == VDIR) {
147 error = EISDIR;
148 goto bad;
149 }
150 if ((error = vn_writechk(vp)) != 0 ||
151 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
152 goto bad;
153 }
154 }
155 if (fmode & O_TRUNC) {
156 VOP_UNLOCK(vp, 0); /* XXX */
157 VOP_LEASE(vp, p, cred, LEASE_WRITE);
158 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
159 VATTR_NULL(&va);
160 va.va_size = 0;
161 if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
162 goto bad;
163 }
164 if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
165 goto bad;
166 if (vp->v_type == VREG &&
167 uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
168 error = EIO;
169 goto bad;
170 }
171 if (fmode & FWRITE)
172 vp->v_writecount++;
173
174 return (0);
175 bad:
176 vput(vp);
177 return (error);
178 }
179
180 /*
181 * Check for write permissions on the specified vnode.
182 * Prototype text segments cannot be written.
183 */
184 int
185 vn_writechk(vp)
186 struct vnode *vp;
187 {
188
189 /*
190 * If the vnode is in use as a process's text,
191 * we can't allow writing.
192 */
193 if (vp->v_flag & VTEXT)
194 return (ETXTBSY);
195 return (0);
196 }
197
198 /*
199 * Mark a vnode as having executable mappings.
200 */
201 void
202 vn_markexec(vp)
203 struct vnode *vp;
204 {
205 if ((vp->v_flag & VEXECMAP) == 0) {
206 uvmexp.filepages -= vp->v_uobj.uo_npages;
207 uvmexp.execpages += vp->v_uobj.uo_npages;
208 }
209 vp->v_flag |= VEXECMAP;
210 }
211
212 /*
213 * Mark a vnode as being the text of a process.
214 * Fail if the vnode is currently writable.
215 */
216 int
217 vn_marktext(vp)
218 struct vnode *vp;
219 {
220
221 if (vp->v_writecount != 0) {
222 KASSERT((vp->v_flag & VTEXT) == 0);
223 return (ETXTBSY);
224 }
225 vp->v_flag |= VTEXT;
226 vn_markexec(vp);
227 return (0);
228 }
229
230 /*
231 * Vnode close call
232 *
233 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
234 */
235 int
236 vn_close(vp, flags, cred, p)
237 struct vnode *vp;
238 int flags;
239 struct ucred *cred;
240 struct proc *p;
241 {
242 int error;
243
244 if (flags & FWRITE)
245 vp->v_writecount--;
246 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
247 error = VOP_CLOSE(vp, flags, cred, p);
248 vput(vp);
249 return (error);
250 }
251
252 /*
253 * Package up an I/O request on a vnode into a uio and do it.
254 */
255 int
256 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
257 enum uio_rw rw;
258 struct vnode *vp;
259 caddr_t base;
260 int len;
261 off_t offset;
262 enum uio_seg segflg;
263 int ioflg;
264 struct ucred *cred;
265 size_t *aresid;
266 struct proc *p;
267 {
268 struct uio auio;
269 struct iovec aiov;
270 int error;
271
272 if ((ioflg & IO_NODELOCKED) == 0) {
273 if (rw == UIO_READ) {
274 vn_lock(vp, LK_SHARED | LK_RETRY);
275 } else {
276 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
277 }
278 }
279 auio.uio_iov = &aiov;
280 auio.uio_iovcnt = 1;
281 aiov.iov_base = base;
282 aiov.iov_len = len;
283 auio.uio_resid = len;
284 auio.uio_offset = offset;
285 auio.uio_segflg = segflg;
286 auio.uio_rw = rw;
287 auio.uio_procp = p;
288 if (rw == UIO_READ) {
289 error = VOP_READ(vp, &auio, ioflg, cred);
290 } else {
291 error = VOP_WRITE(vp, &auio, ioflg, cred);
292 }
293 if (aresid)
294 *aresid = auio.uio_resid;
295 else
296 if (auio.uio_resid && error == 0)
297 error = EIO;
298 if ((ioflg & IO_NODELOCKED) == 0)
299 VOP_UNLOCK(vp, 0);
300 return (error);
301 }
302
303 int
304 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
305 struct file *fp;
306 char *buf;
307 int segflg, *done, *ncookies;
308 u_int count;
309 struct proc *p;
310 off_t **cookies;
311 {
312 struct vnode *vp = (struct vnode *)fp->f_data;
313 struct iovec aiov;
314 struct uio auio;
315 int error, eofflag;
316
317 unionread:
318 if (vp->v_type != VDIR)
319 return (EINVAL);
320 aiov.iov_base = buf;
321 aiov.iov_len = count;
322 auio.uio_iov = &aiov;
323 auio.uio_iovcnt = 1;
324 auio.uio_rw = UIO_READ;
325 auio.uio_segflg = segflg;
326 auio.uio_procp = p;
327 auio.uio_resid = count;
328 vn_lock(vp, LK_SHARED | LK_RETRY);
329 auio.uio_offset = fp->f_offset;
330 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
331 ncookies);
332 fp->f_offset = auio.uio_offset;
333 VOP_UNLOCK(vp, 0);
334 if (error)
335 return (error);
336
337 #ifdef UNION
338 {
339 extern struct vnode *union_dircache __P((struct vnode *));
340
341 if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
342 struct vnode *lvp;
343
344 lvp = union_dircache(vp);
345 if (lvp != NULLVP) {
346 struct vattr va;
347
348 /*
349 * If the directory is opaque,
350 * then don't show lower entries
351 */
352 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
353 if (va.va_flags & OPAQUE) {
354 vput(lvp);
355 lvp = NULL;
356 }
357 }
358
359 if (lvp != NULLVP) {
360 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
361 if (error) {
362 vput(lvp);
363 return (error);
364 }
365 VOP_UNLOCK(lvp, 0);
366 fp->f_data = (caddr_t) lvp;
367 fp->f_offset = 0;
368 error = vn_close(vp, FREAD, fp->f_cred, p);
369 if (error)
370 return (error);
371 vp = lvp;
372 goto unionread;
373 }
374 }
375 }
376 #endif /* UNION */
377
378 if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
379 (vp->v_mount->mnt_flag & MNT_UNION)) {
380 struct vnode *tvp = vp;
381 vp = vp->v_mount->mnt_vnodecovered;
382 VREF(vp);
383 fp->f_data = (caddr_t) vp;
384 fp->f_offset = 0;
385 vrele(tvp);
386 goto unionread;
387 }
388 *done = count - auio.uio_resid;
389 return error;
390 }
391
392 /*
393 * File table vnode read routine.
394 */
395 int
396 vn_read(fp, offset, uio, cred, flags)
397 struct file *fp;
398 off_t *offset;
399 struct uio *uio;
400 struct ucred *cred;
401 int flags;
402 {
403 struct vnode *vp = (struct vnode *)fp->f_data;
404 int count, error, ioflag = 0;
405
406 VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
407 if (fp->f_flag & FNONBLOCK)
408 ioflag |= IO_NDELAY;
409 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
410 ioflag |= IO_SYNC;
411 if (fp->f_flag & FALTIO)
412 ioflag |= IO_ALTSEMANTICS;
413 vn_lock(vp, LK_SHARED | LK_RETRY);
414 uio->uio_offset = *offset;
415 count = uio->uio_resid;
416 error = VOP_READ(vp, uio, ioflag, cred);
417 if (flags & FOF_UPDATE_OFFSET)
418 *offset += count - uio->uio_resid;
419 VOP_UNLOCK(vp, 0);
420 return (error);
421 }
422
423 /*
424 * File table vnode write routine.
425 */
426 int
427 vn_write(fp, offset, uio, cred, flags)
428 struct file *fp;
429 off_t *offset;
430 struct uio *uio;
431 struct ucred *cred;
432 int flags;
433 {
434 struct vnode *vp = (struct vnode *)fp->f_data;
435 int count, error, ioflag = IO_UNIT;
436
437 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
438 ioflag |= IO_APPEND;
439 if (fp->f_flag & FNONBLOCK)
440 ioflag |= IO_NDELAY;
441 if (fp->f_flag & FFSYNC ||
442 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
443 ioflag |= IO_SYNC;
444 else if (fp->f_flag & FDSYNC)
445 ioflag |= IO_DSYNC;
446 if (fp->f_flag & FALTIO)
447 ioflag |= IO_ALTSEMANTICS;
448 VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
449 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
450 uio->uio_offset = *offset;
451 count = uio->uio_resid;
452 error = VOP_WRITE(vp, uio, ioflag, cred);
453 if (flags & FOF_UPDATE_OFFSET) {
454 if (ioflag & IO_APPEND)
455 *offset = uio->uio_offset;
456 else
457 *offset += count - uio->uio_resid;
458 }
459 VOP_UNLOCK(vp, 0);
460 return (error);
461 }
462
463 /*
464 * File table vnode stat routine.
465 */
466 static int
467 vn_statfile(fp, sb, p)
468 struct file *fp;
469 struct stat *sb;
470 struct proc *p;
471 {
472 struct vnode *vp = (struct vnode *)fp->f_data;
473
474 return vn_stat(vp, sb, p);
475 }
476
477 int
478 vn_stat(fdata, sb, p)
479 void *fdata;
480 struct stat *sb;
481 struct proc *p;
482 {
483 struct vnode *vp = fdata;
484 struct vattr va;
485 int error;
486 mode_t mode;
487
488 error = VOP_GETATTR(vp, &va, p->p_ucred, p);
489 if (error)
490 return (error);
491 /*
492 * Copy from vattr table
493 */
494 sb->st_dev = va.va_fsid;
495 sb->st_ino = va.va_fileid;
496 mode = va.va_mode;
497 switch (vp->v_type) {
498 case VREG:
499 mode |= S_IFREG;
500 break;
501 case VDIR:
502 mode |= S_IFDIR;
503 break;
504 case VBLK:
505 mode |= S_IFBLK;
506 break;
507 case VCHR:
508 mode |= S_IFCHR;
509 break;
510 case VLNK:
511 mode |= S_IFLNK;
512 break;
513 case VSOCK:
514 mode |= S_IFSOCK;
515 break;
516 case VFIFO:
517 mode |= S_IFIFO;
518 break;
519 default:
520 return (EBADF);
521 };
522 sb->st_mode = mode;
523 sb->st_nlink = va.va_nlink;
524 sb->st_uid = va.va_uid;
525 sb->st_gid = va.va_gid;
526 sb->st_rdev = va.va_rdev;
527 sb->st_size = va.va_size;
528 sb->st_atimespec = va.va_atime;
529 sb->st_mtimespec = va.va_mtime;
530 sb->st_ctimespec = va.va_ctime;
531 sb->st_blksize = va.va_blocksize;
532 sb->st_flags = va.va_flags;
533 sb->st_gen = 0;
534 sb->st_blocks = va.va_bytes / S_BLKSIZE;
535 return (0);
536 }
537
538 /*
539 * File table vnode fcntl routine.
540 */
541 int
542 vn_fcntl(fp, com, data, p)
543 struct file *fp;
544 u_int com;
545 caddr_t data;
546 struct proc *p;
547 {
548 struct vnode *vp = ((struct vnode *)fp->f_data);
549 int error;
550
551 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
552 error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
553 VOP_UNLOCK(vp, 0);
554 return (error);
555 }
556
557 /*
558 * File table vnode ioctl routine.
559 */
560 int
561 vn_ioctl(fp, com, data, p)
562 struct file *fp;
563 u_long com;
564 caddr_t data;
565 struct proc *p;
566 {
567 struct vnode *vp = ((struct vnode *)fp->f_data);
568 struct vattr vattr;
569 int error;
570
571 switch (vp->v_type) {
572
573 case VREG:
574 case VDIR:
575 if (com == FIONREAD) {
576 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
577 if (error)
578 return (error);
579 *(int *)data = vattr.va_size - fp->f_offset;
580 return (0);
581 }
582 if (com == FIONBIO || com == FIOASYNC) /* XXX */
583 return (0); /* XXX */
584 /* fall into ... */
585
586 default:
587 return (EPASSTHROUGH);
588
589 case VFIFO:
590 case VCHR:
591 case VBLK:
592 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
593 if (error == 0 && com == TIOCSCTTY) {
594 if (p->p_session->s_ttyvp)
595 vrele(p->p_session->s_ttyvp);
596 p->p_session->s_ttyvp = vp;
597 VREF(vp);
598 }
599 return (error);
600 }
601 }
602
603 /*
604 * File table vnode poll routine.
605 */
606 int
607 vn_poll(fp, events, p)
608 struct file *fp;
609 int events;
610 struct proc *p;
611 {
612
613 return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
614 }
615
616 /*
617 * Check that the vnode is still valid, and if so
618 * acquire requested lock.
619 */
620 int
621 vn_lock(vp, flags)
622 struct vnode *vp;
623 int flags;
624 {
625 int error;
626
627 do {
628 if ((flags & LK_INTERLOCK) == 0)
629 simple_lock(&vp->v_interlock);
630 if (vp->v_flag & VXLOCK) {
631 if (flags & LK_NOWAIT) {
632 simple_unlock(&vp->v_interlock);
633 return EBUSY;
634 }
635 vp->v_flag |= VXWANT;
636 ltsleep(vp, PINOD | PNORELOCK,
637 "vn_lock", 0, &vp->v_interlock);
638 error = ENOENT;
639 } else {
640 error = VOP_LOCK(vp, flags | LK_INTERLOCK);
641 if (error == 0 || error == EDEADLK || error == EBUSY)
642 return (error);
643 }
644 flags &= ~LK_INTERLOCK;
645 } while (flags & LK_RETRY);
646 return (error);
647 }
648
649 /*
650 * File table vnode close routine.
651 */
652 int
653 vn_closefile(fp, p)
654 struct file *fp;
655 struct proc *p;
656 {
657
658 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
659 fp->f_cred, p));
660 }
661
662 /*
663 * Enable LK_CANRECURSE on lock. Return prior status.
664 */
665 u_int
666 vn_setrecurse(vp)
667 struct vnode *vp;
668 {
669 struct lock *lkp = &vp->v_lock;
670 u_int retval = lkp->lk_flags & LK_CANRECURSE;
671
672 lkp->lk_flags |= LK_CANRECURSE;
673 return retval;
674 }
675
676 /*
677 * Called when done with locksetrecurse.
678 */
679 void
680 vn_restorerecurse(vp, flags)
681 struct vnode *vp;
682 u_int flags;
683 {
684 struct lock *lkp = &vp->v_lock;
685
686 lkp->lk_flags &= ~LK_CANRECURSE;
687 lkp->lk_flags |= flags;
688 }
689