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