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