vfs_vnops.c revision 1.35 1 /* $NetBSD: vfs_vnops.c,v 1.35 1999/03/30 00:16:44 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_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 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
361 uio->uio_offset = *offset;
362 count = uio->uio_resid;
363 error = VOP_READ(vp, uio, ioflag, cred);
364 if (flags & FOF_UPDATE_OFFSET)
365 *offset += count - uio->uio_resid;
366 VOP_UNLOCK(vp, 0);
367 return (error);
368 }
369
370 /*
371 * File table vnode write routine.
372 */
373 int
374 vn_write(fp, offset, uio, cred, flags)
375 struct file *fp;
376 off_t *offset;
377 struct uio *uio;
378 struct ucred *cred;
379 int flags;
380 {
381 struct vnode *vp = (struct vnode *)fp->f_data;
382 int count, error, ioflag = IO_UNIT;
383
384 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
385 ioflag |= IO_APPEND;
386 if (fp->f_flag & FNONBLOCK)
387 ioflag |= IO_NDELAY;
388 if (fp->f_flag & FFSYNC ||
389 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
390 ioflag |= IO_SYNC;
391 else if (fp->f_flag & FDSYNC)
392 ioflag |= IO_DSYNC;
393 VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
394 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
395 uio->uio_offset = *offset;
396 count = uio->uio_resid;
397 error = VOP_WRITE(vp, uio, ioflag, cred);
398 if (flags & FOF_UPDATE_OFFSET) {
399 if (ioflag & IO_APPEND)
400 *offset = uio->uio_offset;
401 else
402 *offset += count - uio->uio_resid;
403 }
404 VOP_UNLOCK(vp, 0);
405 return (error);
406 }
407
408 /*
409 * File table vnode stat routine.
410 */
411 int
412 vn_stat(vp, sb, p)
413 struct vnode *vp;
414 register struct stat *sb;
415 struct proc *p;
416 {
417 struct vattr va;
418 int error;
419 mode_t mode;
420
421 error = VOP_GETATTR(vp, &va, p->p_ucred, p);
422 if (error)
423 return (error);
424 /*
425 * Copy from vattr table
426 */
427 sb->st_dev = va.va_fsid;
428 sb->st_ino = va.va_fileid;
429 mode = va.va_mode;
430 switch (vp->v_type) {
431 case VREG:
432 mode |= S_IFREG;
433 break;
434 case VDIR:
435 mode |= S_IFDIR;
436 break;
437 case VBLK:
438 mode |= S_IFBLK;
439 break;
440 case VCHR:
441 mode |= S_IFCHR;
442 break;
443 case VLNK:
444 mode |= S_IFLNK;
445 break;
446 case VSOCK:
447 mode |= S_IFSOCK;
448 break;
449 case VFIFO:
450 mode |= S_IFIFO;
451 break;
452 default:
453 return (EBADF);
454 };
455 sb->st_mode = mode;
456 sb->st_nlink = va.va_nlink;
457 sb->st_uid = va.va_uid;
458 sb->st_gid = va.va_gid;
459 sb->st_rdev = va.va_rdev;
460 sb->st_size = va.va_size;
461 sb->st_atimespec = va.va_atime;
462 sb->st_mtimespec = va.va_mtime;
463 sb->st_ctimespec = va.va_ctime;
464 sb->st_blksize = va.va_blocksize;
465 sb->st_flags = va.va_flags;
466 sb->st_gen = 0;
467 sb->st_blocks = va.va_bytes / S_BLKSIZE;
468 return (0);
469 }
470
471 /*
472 * File table vnode ioctl routine.
473 */
474 int
475 vn_ioctl(fp, com, data, p)
476 struct file *fp;
477 u_long com;
478 caddr_t data;
479 struct proc *p;
480 {
481 register struct vnode *vp = ((struct vnode *)fp->f_data);
482 struct vattr vattr;
483 int error;
484
485 switch (vp->v_type) {
486
487 case VREG:
488 case VDIR:
489 if (com == FIONREAD) {
490 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
491 if (error)
492 return (error);
493 *(int *)data = vattr.va_size - fp->f_offset;
494 return (0);
495 }
496 if (com == FIONBIO || com == FIOASYNC) /* XXX */
497 return (0); /* XXX */
498 /* fall into ... */
499
500 default:
501 return (ENOTTY);
502
503 case VFIFO:
504 case VCHR:
505 case VBLK:
506 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
507 if (error == 0 && com == TIOCSCTTY) {
508 if (p->p_session->s_ttyvp)
509 vrele(p->p_session->s_ttyvp);
510 p->p_session->s_ttyvp = vp;
511 VREF(vp);
512 }
513 return (error);
514 }
515 }
516
517 /*
518 * File table vnode poll routine.
519 */
520 int
521 vn_poll(fp, events, p)
522 struct file *fp;
523 int events;
524 struct proc *p;
525 {
526
527 return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
528 }
529
530 /*
531 * Check that the vnode is still valid, and if so
532 * acquire requested lock.
533 */
534 int
535 vn_lock(vp, flags)
536 struct vnode *vp;
537 int flags;
538 {
539 int error;
540 int have_interlock = 0;
541
542 do {
543 if ((flags & LK_INTERLOCK) == 0) {
544 simple_lock(&vp->v_interlock);
545 have_interlock++;
546 }
547 if (vp->v_flag & VXLOCK) {
548 vp->v_flag |= VXWANT;
549 simple_unlock(&vp->v_interlock);
550 tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
551 error = ENOENT;
552 } else {
553 error = VOP_LOCK(vp, flags | LK_INTERLOCK);
554 if (error == 0)
555 return (error);
556 }
557 if (error == EDEADLK) {
558 if (have_interlock)
559 simple_unlock(&vp->v_interlock);
560 break;
561 }
562 flags &= ~LK_INTERLOCK;
563 } while (flags & LK_RETRY);
564 return (error);
565 }
566
567 /*
568 * File table vnode close routine.
569 */
570 int
571 vn_closefile(fp, p)
572 struct file *fp;
573 struct proc *p;
574 {
575
576 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
577 fp->f_cred, p));
578 }
579