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