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