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