vfs_vnops.c revision 1.54.6.2 1 /* $NetBSD: vfs_vnops.c,v 1.54.6.2 2006/06/01 05:57:19 simonb 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.54.6.2 2006/06/01 05:57:19 simonb 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 /* Limit the size on any kernel buffers used by VOP_READDIR */
318 count = min(MAXBSIZE, count);
319
320 unionread:
321 if (vp->v_type != VDIR)
322 return (EINVAL);
323 aiov.iov_base = buf;
324 aiov.iov_len = count;
325 auio.uio_iov = &aiov;
326 auio.uio_iovcnt = 1;
327 auio.uio_rw = UIO_READ;
328 auio.uio_segflg = segflg;
329 auio.uio_procp = p;
330 auio.uio_resid = count;
331 vn_lock(vp, LK_SHARED | LK_RETRY);
332 auio.uio_offset = fp->f_offset;
333 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
334 ncookies);
335 fp->f_offset = auio.uio_offset;
336 VOP_UNLOCK(vp, 0);
337 if (error)
338 return (error);
339
340 #ifdef UNION
341 {
342 extern struct vnode *union_dircache __P((struct vnode *));
343
344 if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
345 struct vnode *lvp;
346
347 lvp = union_dircache(vp);
348 if (lvp != NULLVP) {
349 struct vattr va;
350
351 /*
352 * If the directory is opaque,
353 * then don't show lower entries
354 */
355 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
356 if (va.va_flags & OPAQUE) {
357 vput(lvp);
358 lvp = NULL;
359 }
360 }
361
362 if (lvp != NULLVP) {
363 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
364 if (error) {
365 vput(lvp);
366 return (error);
367 }
368 VOP_UNLOCK(lvp, 0);
369 fp->f_data = (caddr_t) lvp;
370 fp->f_offset = 0;
371 error = vn_close(vp, FREAD, fp->f_cred, p);
372 if (error)
373 return (error);
374 vp = lvp;
375 goto unionread;
376 }
377 }
378 }
379 #endif /* UNION */
380
381 if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
382 (vp->v_mount->mnt_flag & MNT_UNION)) {
383 struct vnode *tvp = vp;
384 vp = vp->v_mount->mnt_vnodecovered;
385 VREF(vp);
386 fp->f_data = (caddr_t) vp;
387 fp->f_offset = 0;
388 vrele(tvp);
389 goto unionread;
390 }
391 *done = count - auio.uio_resid;
392 return error;
393 }
394
395 /*
396 * File table vnode read routine.
397 */
398 int
399 vn_read(fp, offset, uio, cred, flags)
400 struct file *fp;
401 off_t *offset;
402 struct uio *uio;
403 struct ucred *cred;
404 int flags;
405 {
406 struct vnode *vp = (struct vnode *)fp->f_data;
407 int count, error, ioflag = 0;
408
409 VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
410 if (fp->f_flag & FNONBLOCK)
411 ioflag |= IO_NDELAY;
412 if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
413 ioflag |= IO_SYNC;
414 if (fp->f_flag & FALTIO)
415 ioflag |= IO_ALTSEMANTICS;
416 vn_lock(vp, LK_SHARED | LK_RETRY);
417 uio->uio_offset = *offset;
418 count = uio->uio_resid;
419 error = VOP_READ(vp, uio, ioflag, cred);
420 if (flags & FOF_UPDATE_OFFSET)
421 *offset += count - uio->uio_resid;
422 VOP_UNLOCK(vp, 0);
423 return (error);
424 }
425
426 /*
427 * File table vnode write routine.
428 */
429 int
430 vn_write(fp, offset, uio, cred, flags)
431 struct file *fp;
432 off_t *offset;
433 struct uio *uio;
434 struct ucred *cred;
435 int flags;
436 {
437 struct vnode *vp = (struct vnode *)fp->f_data;
438 int count, error, ioflag = IO_UNIT;
439
440 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
441 ioflag |= IO_APPEND;
442 if (fp->f_flag & FNONBLOCK)
443 ioflag |= IO_NDELAY;
444 if (fp->f_flag & FFSYNC ||
445 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
446 ioflag |= IO_SYNC;
447 else if (fp->f_flag & FDSYNC)
448 ioflag |= IO_DSYNC;
449 if (fp->f_flag & FALTIO)
450 ioflag |= IO_ALTSEMANTICS;
451 VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
452 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
453 uio->uio_offset = *offset;
454 count = uio->uio_resid;
455 error = VOP_WRITE(vp, uio, ioflag, cred);
456 if (flags & FOF_UPDATE_OFFSET) {
457 if (ioflag & IO_APPEND)
458 *offset = uio->uio_offset;
459 else
460 *offset += count - uio->uio_resid;
461 }
462 VOP_UNLOCK(vp, 0);
463 return (error);
464 }
465
466 /*
467 * File table vnode stat routine.
468 */
469 static int
470 vn_statfile(fp, sb, p)
471 struct file *fp;
472 struct stat *sb;
473 struct proc *p;
474 {
475 struct vnode *vp = (struct vnode *)fp->f_data;
476
477 return vn_stat(vp, sb, p);
478 }
479
480 int
481 vn_stat(fdata, sb, p)
482 void *fdata;
483 struct stat *sb;
484 struct proc *p;
485 {
486 struct vnode *vp = fdata;
487 struct vattr va;
488 int error;
489 mode_t mode;
490
491 error = VOP_GETATTR(vp, &va, p->p_ucred, p);
492 if (error)
493 return (error);
494 /*
495 * Copy from vattr table
496 */
497 sb->st_dev = va.va_fsid;
498 sb->st_ino = va.va_fileid;
499 mode = va.va_mode;
500 switch (vp->v_type) {
501 case VREG:
502 mode |= S_IFREG;
503 break;
504 case VDIR:
505 mode |= S_IFDIR;
506 break;
507 case VBLK:
508 mode |= S_IFBLK;
509 break;
510 case VCHR:
511 mode |= S_IFCHR;
512 break;
513 case VLNK:
514 mode |= S_IFLNK;
515 break;
516 case VSOCK:
517 mode |= S_IFSOCK;
518 break;
519 case VFIFO:
520 mode |= S_IFIFO;
521 break;
522 default:
523 return (EBADF);
524 };
525 sb->st_mode = mode;
526 sb->st_nlink = va.va_nlink;
527 sb->st_uid = va.va_uid;
528 sb->st_gid = va.va_gid;
529 sb->st_rdev = va.va_rdev;
530 sb->st_size = va.va_size;
531 sb->st_atimespec = va.va_atime;
532 sb->st_mtimespec = va.va_mtime;
533 sb->st_ctimespec = va.va_ctime;
534 sb->st_blksize = va.va_blocksize;
535 sb->st_flags = va.va_flags;
536 sb->st_gen = 0;
537 sb->st_blocks = va.va_bytes / S_BLKSIZE;
538 return (0);
539 }
540
541 /*
542 * File table vnode fcntl routine.
543 */
544 int
545 vn_fcntl(fp, com, data, p)
546 struct file *fp;
547 u_int com;
548 caddr_t data;
549 struct proc *p;
550 {
551 struct vnode *vp = ((struct vnode *)fp->f_data);
552 int error;
553
554 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
555 error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
556 VOP_UNLOCK(vp, 0);
557 return (error);
558 }
559
560 /*
561 * File table vnode ioctl routine.
562 */
563 int
564 vn_ioctl(fp, com, data, p)
565 struct file *fp;
566 u_long com;
567 caddr_t data;
568 struct proc *p;
569 {
570 struct vnode *vp = ((struct vnode *)fp->f_data);
571 struct vattr vattr;
572 int error;
573
574 switch (vp->v_type) {
575
576 case VREG:
577 case VDIR:
578 if (com == FIONREAD) {
579 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
580 if (error)
581 return (error);
582 *(int *)data = vattr.va_size - fp->f_offset;
583 return (0);
584 }
585 if (com == FIONBIO || com == FIOASYNC) /* XXX */
586 return (0); /* XXX */
587 /* fall into ... */
588
589 default:
590 return (EPASSTHROUGH);
591
592 case VFIFO:
593 case VCHR:
594 case VBLK:
595 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
596 if (error == 0 && com == TIOCSCTTY) {
597 if (p->p_session->s_ttyvp)
598 vrele(p->p_session->s_ttyvp);
599 p->p_session->s_ttyvp = vp;
600 VREF(vp);
601 }
602 return (error);
603 }
604 }
605
606 /*
607 * File table vnode poll routine.
608 */
609 int
610 vn_poll(fp, events, p)
611 struct file *fp;
612 int events;
613 struct proc *p;
614 {
615
616 return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
617 }
618
619 /*
620 * Check that the vnode is still valid, and if so
621 * acquire requested lock.
622 */
623 int
624 vn_lock(vp, flags)
625 struct vnode *vp;
626 int flags;
627 {
628 int error;
629
630 do {
631 if ((flags & LK_INTERLOCK) == 0)
632 simple_lock(&vp->v_interlock);
633 if (vp->v_flag & VXLOCK) {
634 if (flags & LK_NOWAIT) {
635 simple_unlock(&vp->v_interlock);
636 return EBUSY;
637 }
638 vp->v_flag |= VXWANT;
639 ltsleep(vp, PINOD | PNORELOCK,
640 "vn_lock", 0, &vp->v_interlock);
641 error = ENOENT;
642 } else {
643 error = VOP_LOCK(vp, flags | LK_INTERLOCK);
644 if (error == 0 || error == EDEADLK || error == EBUSY)
645 return (error);
646 }
647 flags &= ~LK_INTERLOCK;
648 } while (flags & LK_RETRY);
649 return (error);
650 }
651
652 /*
653 * File table vnode close routine.
654 */
655 int
656 vn_closefile(fp, p)
657 struct file *fp;
658 struct proc *p;
659 {
660
661 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
662 fp->f_cred, p));
663 }
664
665 /*
666 * Enable LK_CANRECURSE on lock. Return prior status.
667 */
668 u_int
669 vn_setrecurse(vp)
670 struct vnode *vp;
671 {
672 struct lock *lkp = &vp->v_lock;
673 u_int retval = lkp->lk_flags & LK_CANRECURSE;
674
675 lkp->lk_flags |= LK_CANRECURSE;
676 return retval;
677 }
678
679 /*
680 * Called when done with locksetrecurse.
681 */
682 void
683 vn_restorerecurse(vp, flags)
684 struct vnode *vp;
685 u_int flags;
686 {
687 struct lock *lkp = &vp->v_lock;
688
689 lkp->lk_flags &= ~LK_CANRECURSE;
690 lkp->lk_flags |= flags;
691 }
692