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