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