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