Deleted Added
sdiff udiff text old ( 12767 ) new ( 12913 )
full compact
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
39 * $Id: vfs_vnops.c,v 1.21 1995/12/11 04:56:13 dyson Exp $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/file.h>
46#include <sys/stat.h>
47#include <sys/buf.h>
48#include <sys/proc.h>
49#include <sys/mount.h>
50#include <sys/namei.h>
51#include <sys/vnode.h>
52#include <sys/ioctl.h>
53
54#include <vm/vm.h>
55#include <vm/vm_param.h>
56#include <vm/vm_object.h>
57#include <vm/vnode_pager.h>
58
59static int vn_closefile __P((struct file *fp, struct proc *p));
60static int vn_ioctl __P((struct file *fp, int com, caddr_t data,
61 struct proc *p));
62static int vn_read __P((struct file *fp, struct uio *uio,
63 struct ucred *cred));
64static int vn_select __P((struct file *fp, int which, struct proc *p));
65static int vn_vmio_open __P((struct vnode *vp, struct proc *p,
66 struct ucred *cred));
67static int vn_write __P((struct file *fp, struct uio *uio,
68 struct ucred *cred));
69
70struct fileops vnops =
71 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
72
73/*
74 * Common code for vnode open operations.
75 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
76 */
77int
78vn_open(ndp, fmode, cmode)
79 register struct nameidata *ndp;
80 int fmode, cmode;
81{
82 register struct vnode *vp;
83 register struct proc *p = ndp->ni_cnd.cn_proc;
84 register struct ucred *cred = p->p_ucred;
85 struct vattr vat;
86 struct vattr *vap = &vat;
87 int error;
88
89 if (fmode & O_CREAT) {
90 ndp->ni_cnd.cn_nameiop = CREATE;
91 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
92 if ((fmode & O_EXCL) == 0)
93 ndp->ni_cnd.cn_flags |= FOLLOW;
94 error = namei(ndp);
95 if (error)
96 return (error);
97 if (ndp->ni_vp == NULL) {
98 VATTR_NULL(vap);
99 vap->va_type = VREG;
100 vap->va_mode = cmode;
101 LEASE_CHECK(ndp->ni_dvp, p, cred, LEASE_WRITE);
102 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
103 &ndp->ni_cnd, vap);
104 if (error)
105 return (error);
106 fmode &= ~O_TRUNC;
107 vp = ndp->ni_vp;
108 } else {
109 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
110 if (ndp->ni_dvp == ndp->ni_vp)
111 vrele(ndp->ni_dvp);
112 else
113 vput(ndp->ni_dvp);
114 ndp->ni_dvp = NULL;
115 vp = ndp->ni_vp;
116 if (fmode & O_EXCL) {
117 error = EEXIST;
118 goto bad;
119 }
120 fmode &= ~O_CREAT;
121 }
122 } else {
123 ndp->ni_cnd.cn_nameiop = LOOKUP;
124 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
125 error = namei(ndp);
126 if (error)
127 return (error);
128 vp = ndp->ni_vp;
129 }
130 if (vp->v_type == VSOCK) {
131 error = EOPNOTSUPP;
132 goto bad;
133 }
134 if ((fmode & O_CREAT) == 0) {
135 if (fmode & FREAD) {
136 error = VOP_ACCESS(vp, VREAD, cred, p);
137 if (error)
138 goto bad;
139 }
140 if (fmode & (FWRITE | O_TRUNC)) {
141 if (vp->v_type == VDIR) {
142 error = EISDIR;
143 goto bad;
144 }
145 error = vn_writechk(vp);
146 if (error)
147 goto bad;
148 error = VOP_ACCESS(vp, VWRITE, cred, p);
149 if (error)
150 goto bad;
151 }
152 }
153 if (fmode & O_TRUNC) {
154 VOP_UNLOCK(vp); /* XXX */
155 LEASE_CHECK(vp, p, cred, LEASE_WRITE);
156 VOP_LOCK(vp); /* XXX */
157 VATTR_NULL(vap);
158 vap->va_size = 0;
159 error = VOP_SETATTR(vp, vap, cred, p);
160 if (error)
161 goto bad;
162 }
163 error = VOP_OPEN(vp, fmode, cred, p);
164 if (error)
165 goto bad;
166 /*
167 * this is here for VMIO support
168 */
169 if (vp->v_type == VREG) {
170 if ((error = vn_vmio_open(vp, p, cred)) != 0)
171 goto bad;
172 }
173 if (fmode & FWRITE)
174 vp->v_writecount++;
175 return (0);
176bad:
177 vput(vp);
178 return (error);
179}
180
181/*
182 * Check for write permissions on the specified vnode.
183 * The read-only status of the file system is checked.
184 * Also, prototype text segments cannot be written.
185 */
186int
187vn_writechk(vp)
188 register struct vnode *vp;
189{
190
191 /*
192 * If there's shared text associated with
193 * the vnode, try to free it up once. If
194 * we fail, we can't allow writing.
195 */
196 if (vp->v_flag & VTEXT)
197 return (ETXTBSY);
198 return (0);
199}
200
201/*
202 * Vnode close call
203 */
204int
205vn_close(vp, flags, cred, p)
206 register struct vnode *vp;
207 int flags;
208 struct ucred *cred;
209 struct proc *p;
210{
211 int error;
212
213 if (flags & FWRITE)
214 vp->v_writecount--;
215 error = VOP_CLOSE(vp, flags, cred, p);
216 vn_vmio_close(vp);
217 return (error);
218}
219
220/*
221 * Package up an I/O request on a vnode into a uio and do it.
222 */
223int
224vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
225 enum uio_rw rw;
226 struct vnode *vp;
227 caddr_t base;
228 int len;
229 off_t offset;
230 enum uio_seg segflg;
231 int ioflg;
232 struct ucred *cred;
233 int *aresid;
234 struct proc *p;
235{
236 struct uio auio;
237 struct iovec aiov;
238 int error;
239
240 if ((ioflg & IO_NODELOCKED) == 0)
241 VOP_LOCK(vp);
242 auio.uio_iov = &aiov;
243 auio.uio_iovcnt = 1;
244 aiov.iov_base = base;
245 aiov.iov_len = len;
246 auio.uio_resid = len;
247 auio.uio_offset = offset;
248 auio.uio_segflg = segflg;
249 auio.uio_rw = rw;
250 auio.uio_procp = p;
251 if (rw == UIO_READ) {
252 error = VOP_READ(vp, &auio, ioflg, cred);
253 } else {
254 error = VOP_WRITE(vp, &auio, ioflg, cred);
255 }
256 if (aresid)
257 *aresid = auio.uio_resid;
258 else
259 if (auio.uio_resid && error == 0)
260 error = EIO;
261 if ((ioflg & IO_NODELOCKED) == 0)
262 VOP_UNLOCK(vp);
263 return (error);
264}
265
266/*
267 * File table vnode read routine.
268 */
269static int
270vn_read(fp, uio, cred)
271 struct file *fp;
272 struct uio *uio;
273 struct ucred *cred;
274{
275 register struct vnode *vp = (struct vnode *)fp->f_data;
276 int count, error;
277
278 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
279 VOP_LOCK(vp);
280 uio->uio_offset = fp->f_offset;
281 count = uio->uio_resid;
282 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
283 cred);
284 fp->f_offset += count - uio->uio_resid;
285 VOP_UNLOCK(vp);
286 return (error);
287}
288
289/*
290 * File table vnode write routine.
291 */
292static int
293vn_write(fp, uio, cred)
294 struct file *fp;
295 struct uio *uio;
296 struct ucred *cred;
297{
298 register struct vnode *vp = (struct vnode *)fp->f_data;
299 int count, error, ioflag = 0;
300
301 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
302 ioflag |= IO_APPEND;
303 if (fp->f_flag & FNONBLOCK)
304 ioflag |= IO_NDELAY;
305 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
306 VOP_LOCK(vp);
307 uio->uio_offset = fp->f_offset;
308 count = uio->uio_resid;
309 error = VOP_WRITE(vp, uio, ioflag, cred);
310 if (ioflag & IO_APPEND)
311 fp->f_offset = uio->uio_offset;
312 else
313 fp->f_offset += count - uio->uio_resid;
314 VOP_UNLOCK(vp);
315 return (error);
316}
317
318/*
319 * File table vnode stat routine.
320 */
321int
322vn_stat(vp, sb, p)
323 struct vnode *vp;
324 register struct stat *sb;
325 struct proc *p;
326{
327 struct vattr vattr;
328 register struct vattr *vap;
329 int error;
330 u_short mode;
331
332 vap = &vattr;
333 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
334 if (error)
335 return (error);
336 /*
337 * Copy from vattr table
338 */
339 sb->st_dev = vap->va_fsid;
340 sb->st_ino = vap->va_fileid;
341 mode = vap->va_mode;
342 switch (vp->v_type) {
343 case VREG:
344 mode |= S_IFREG;
345 break;
346 case VDIR:
347 mode |= S_IFDIR;
348 break;
349 case VBLK:
350 mode |= S_IFBLK;
351 break;
352 case VCHR:
353 mode |= S_IFCHR;
354 break;
355 case VLNK:
356 mode |= S_IFLNK;
357 break;
358 case VSOCK:
359 mode |= S_IFSOCK;
360 break;
361 case VFIFO:
362 mode |= S_IFIFO;
363 break;
364 default:
365 return (EBADF);
366 };
367 sb->st_mode = mode;
368 sb->st_nlink = vap->va_nlink;
369 sb->st_uid = vap->va_uid;
370 sb->st_gid = vap->va_gid;
371 sb->st_rdev = vap->va_rdev;
372 sb->st_size = vap->va_size;
373 sb->st_atimespec = vap->va_atime;
374 sb->st_mtimespec= vap->va_mtime;
375 sb->st_ctimespec = vap->va_ctime;
376 sb->st_blksize = vap->va_blocksize;
377 sb->st_flags = vap->va_flags;
378 sb->st_gen = vap->va_gen;
379#if (S_BLKSIZE == 512)
380 /* Optimize this case */
381 sb->st_blocks = vap->va_bytes >> 9;
382#else
383 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
384#endif
385 return (0);
386}
387
388/*
389 * File table vnode ioctl routine.
390 */
391static int
392vn_ioctl(fp, com, data, p)
393 struct file *fp;
394 int com;
395 caddr_t data;
396 struct proc *p;
397{
398 register struct vnode *vp = ((struct vnode *)fp->f_data);
399 struct vattr vattr;
400 int error;
401
402 switch (vp->v_type) {
403
404 case VREG:
405 case VDIR:
406 if (com == FIONREAD) {
407 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
408 if (error)
409 return (error);
410 *(int *)data = vattr.va_size - fp->f_offset;
411 return (0);
412 }
413 if (com == FIONBIO || com == FIOASYNC) /* XXX */
414 return (0); /* XXX */
415 /* fall into ... */
416
417 default:
418 return (ENOTTY);
419
420 case VFIFO:
421 case VCHR:
422 case VBLK:
423 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
424 if (error == 0 && com == TIOCSCTTY) {
425
426 /* Do nothing if reassigning same control tty */
427 if (p->p_session->s_ttyvp == vp)
428 return (0);
429
430 /* Get rid of reference to old control tty */
431 if (p->p_session->s_ttyvp)
432 vrele(p->p_session->s_ttyvp);
433
434 p->p_session->s_ttyvp = vp;
435 VREF(vp);
436 }
437 return (error);
438 }
439}
440
441/*
442 * File table vnode select routine.
443 */
444static int
445vn_select(fp, which, p)
446 struct file *fp;
447 int which;
448 struct proc *p;
449{
450
451 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
452 fp->f_cred, p));
453}
454
455/*
456 * File table vnode close routine.
457 */
458static int
459vn_closefile(fp, p)
460 struct file *fp;
461 struct proc *p;
462{
463
464 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
465 fp->f_cred, p));
466}
467
468static int
469vn_vmio_open(vp, p, cred)
470 struct vnode *vp;
471 struct proc *p;
472 struct ucred *cred;
473{
474 struct vattr vat;
475 int error;
476 /*
477 * this is here for VMIO support
478 */
479 if (vp->v_type == VREG || vp->v_type == VBLK) {
480retry:
481 if ((vp->v_flag & VVMIO) == 0) {
482 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
483 return error;
484 (void) vnode_pager_alloc(vp, vat.va_size, 0, 0);
485 vp->v_flag |= VVMIO;
486 } else {
487 vm_object_t object;
488 if ((object = vp->v_object) &&
489 (object->flags & OBJ_DEAD)) {
490 VOP_UNLOCK(vp);
491 tsleep(object, PVM, "vodead", 0);
492 VOP_LOCK(vp);
493 goto retry;
494 }
495 if (!object)
496 panic("vn_open: VMIO object missing");
497 vm_object_reference(object);
498 }
499 }
500 return 0;
501}
502
503void
504vn_vmio_close(vp)
505 struct vnode *vp;
506{
507 /*
508 * this code is here for VMIO support, will eventually
509 * be in vfs code.
510 */
511 if (vp->v_flag & VVMIO) {
512 vrele(vp);
513 if (vp->v_object == NULL)
514 panic("vn_close: VMIO object missing");
515 vm_object_deallocate(vp->v_object);
516 } else
517 vrele(vp);
518}