vfs_vnops.c revision 1.11 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 * from: @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
39 * $Id: vfs_vnops.c,v 1.11 1994/06/08 11:29:00 mycroft 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 #include <sys/tty.h>
54
55 #include <vm/vm.h>
56
57 struct fileops vnops =
58 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
59
60 /*
61 * Common code for vnode open operations.
62 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
63 */
64 vn_open(ndp, fmode, cmode)
65 register struct nameidata *ndp;
66 int fmode, cmode;
67 {
68 register struct vnode *vp;
69 register struct proc *p = ndp->ni_cnd.cn_proc;
70 register struct ucred *cred = p->p_ucred;
71 struct vattr vat;
72 struct vattr *vap = &vat;
73 int error;
74
75 if (fmode & O_CREAT) {
76 ndp->ni_cnd.cn_nameiop = CREATE;
77 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
78 if ((fmode & O_EXCL) == 0)
79 ndp->ni_cnd.cn_flags |= FOLLOW;
80 if (error = namei(ndp))
81 return (error);
82 if (ndp->ni_vp == NULL) {
83 VATTR_NULL(vap);
84 vap->va_type = VREG;
85 vap->va_mode = cmode;
86 LEASE_CHECK(ndp->ni_dvp, p, cred, LEASE_WRITE);
87 if (error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
88 &ndp->ni_cnd, vap))
89 return (error);
90 fmode &= ~O_TRUNC;
91 vp = ndp->ni_vp;
92 } else {
93 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
94 if (ndp->ni_dvp == ndp->ni_vp)
95 vrele(ndp->ni_dvp);
96 else
97 vput(ndp->ni_dvp);
98 ndp->ni_dvp = NULL;
99 vp = ndp->ni_vp;
100 if (fmode & O_EXCL) {
101 error = EEXIST;
102 goto bad;
103 }
104 fmode &= ~O_CREAT;
105 }
106 } else {
107 ndp->ni_cnd.cn_nameiop = LOOKUP;
108 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
109 if (error = namei(ndp))
110 return (error);
111 vp = ndp->ni_vp;
112 }
113 if (vp->v_type == VSOCK) {
114 error = EOPNOTSUPP;
115 goto bad;
116 }
117 if ((fmode & O_CREAT) == 0) {
118 if (fmode & FREAD) {
119 if (error = VOP_ACCESS(vp, VREAD, cred, p))
120 goto bad;
121 }
122 if (fmode & (FWRITE | O_TRUNC)) {
123 if (vp->v_type == VDIR) {
124 error = EISDIR;
125 goto bad;
126 }
127 if ((error = vn_writechk(vp)) ||
128 (error = VOP_ACCESS(vp, VWRITE, cred, p)))
129 goto bad;
130 }
131 }
132 if (fmode & O_TRUNC) {
133 VOP_UNLOCK(vp); /* XXX */
134 LEASE_CHECK(vp, p, cred, LEASE_WRITE);
135 VOP_LOCK(vp); /* XXX */
136 VATTR_NULL(vap);
137 vap->va_size = 0;
138 if (error = VOP_SETATTR(vp, vap, cred, p))
139 goto bad;
140 }
141 if (error = VOP_OPEN(vp, fmode, cred, p))
142 goto bad;
143 if (fmode & FWRITE)
144 vp->v_writecount++;
145 return (0);
146 bad:
147 vput(vp);
148 return (error);
149 }
150
151 /*
152 * Check for write permissions on the specified vnode.
153 * The read-only status of the file system is checked.
154 * Also, prototype text segments cannot be written.
155 */
156 vn_writechk(vp)
157 register struct vnode *vp;
158 {
159
160 /*
161 * Disallow write attempts on read-only file systems;
162 * unless the file is a socket or a block or character
163 * device resident on the file system.
164 */
165 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
166 switch (vp->v_type) {
167 case VREG: case VDIR: case VLNK:
168 return (EROFS);
169 }
170 }
171 /*
172 * If there's shared text associated with
173 * the vnode, try to free it up once. If
174 * we fail, we can't allow writing.
175 */
176 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
177 return (ETXTBSY);
178 return (0);
179 }
180
181 /*
182 * Vnode close call
183 */
184 vn_close(vp, flags, cred, p)
185 register struct vnode *vp;
186 int flags;
187 struct ucred *cred;
188 struct proc *p;
189 {
190 int error;
191
192 if (flags & FWRITE)
193 vp->v_writecount--;
194 error = VOP_CLOSE(vp, flags, cred, p);
195 vrele(vp);
196 return (error);
197 }
198
199 /*
200 * Package up an I/O request on a vnode into a uio and do it.
201 */
202 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
203 enum uio_rw rw;
204 struct vnode *vp;
205 caddr_t base;
206 int len;
207 off_t offset;
208 enum uio_seg segflg;
209 int ioflg;
210 struct ucred *cred;
211 int *aresid;
212 struct proc *p;
213 {
214 struct uio auio;
215 struct iovec aiov;
216 int error;
217
218 if ((ioflg & IO_NODELOCKED) == 0)
219 VOP_LOCK(vp);
220 auio.uio_iov = &aiov;
221 auio.uio_iovcnt = 1;
222 aiov.iov_base = base;
223 aiov.iov_len = len;
224 auio.uio_resid = len;
225 auio.uio_offset = offset;
226 auio.uio_segflg = segflg;
227 auio.uio_rw = rw;
228 auio.uio_procp = p;
229 if (rw == UIO_READ) {
230 error = VOP_READ(vp, &auio, ioflg, cred);
231 } else {
232 error = VOP_WRITE(vp, &auio, ioflg, cred);
233 }
234 if (aresid)
235 *aresid = auio.uio_resid;
236 else
237 if (auio.uio_resid && error == 0)
238 error = EIO;
239 if ((ioflg & IO_NODELOCKED) == 0)
240 VOP_UNLOCK(vp);
241 return (error);
242 }
243
244 /*
245 * File table vnode read routine.
246 */
247 vn_read(fp, uio, cred)
248 struct file *fp;
249 struct uio *uio;
250 struct ucred *cred;
251 {
252 register struct vnode *vp = (struct vnode *)fp->f_data;
253 int count, error;
254
255 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
256 VOP_LOCK(vp);
257 uio->uio_offset = fp->f_offset;
258 count = uio->uio_resid;
259 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
260 cred);
261 fp->f_offset += count - uio->uio_resid;
262 VOP_UNLOCK(vp);
263 return (error);
264 }
265
266 /*
267 * File table vnode write routine.
268 */
269 vn_write(fp, uio, cred)
270 struct file *fp;
271 struct uio *uio;
272 struct ucred *cred;
273 {
274 register struct vnode *vp = (struct vnode *)fp->f_data;
275 int count, error, ioflag = 0;
276
277 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
278 ioflag |= IO_APPEND;
279 if (fp->f_flag & FNONBLOCK)
280 ioflag |= IO_NDELAY;
281 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
282 VOP_LOCK(vp);
283 uio->uio_offset = fp->f_offset;
284 count = uio->uio_resid;
285 error = VOP_WRITE(vp, uio, ioflag, cred);
286 if (ioflag & IO_APPEND)
287 fp->f_offset = uio->uio_offset;
288 else
289 fp->f_offset += count - uio->uio_resid;
290 VOP_UNLOCK(vp);
291 return (error);
292 }
293
294 /*
295 * File table vnode stat routine.
296 */
297 vn_stat(vp, sb, p)
298 struct vnode *vp;
299 register struct stat *sb;
300 struct proc *p;
301 {
302 struct vattr vattr;
303 register struct vattr *vap;
304 int error;
305 u_short mode;
306
307 vap = &vattr;
308 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
309 if (error)
310 return (error);
311 /*
312 * Copy from vattr table
313 */
314 sb->st_dev = vap->va_fsid;
315 sb->st_ino = vap->va_fileid;
316 mode = vap->va_mode;
317 switch (vp->v_type) {
318 case VREG:
319 mode |= S_IFREG;
320 break;
321 case VDIR:
322 mode |= S_IFDIR;
323 break;
324 case VBLK:
325 mode |= S_IFBLK;
326 break;
327 case VCHR:
328 mode |= S_IFCHR;
329 break;
330 case VLNK:
331 mode |= S_IFLNK;
332 break;
333 case VSOCK:
334 mode |= S_IFSOCK;
335 break;
336 case VFIFO:
337 mode |= S_IFIFO;
338 break;
339 default:
340 return (EBADF);
341 };
342 sb->st_mode = mode;
343 sb->st_nlink = vap->va_nlink;
344 sb->st_uid = vap->va_uid;
345 sb->st_gid = vap->va_gid;
346 sb->st_rdev = vap->va_rdev;
347 sb->st_size = vap->va_size;
348 sb->st_atimespec = vap->va_atime;
349 sb->st_mtimespec = vap->va_mtime;
350 sb->st_ctimespec = vap->va_ctime;
351 sb->st_blksize = vap->va_blocksize;
352 sb->st_flags = vap->va_flags;
353 sb->st_gen = vap->va_gen;
354 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
355 return (0);
356 }
357
358 /*
359 * File table vnode ioctl routine.
360 */
361 vn_ioctl(fp, com, data, p)
362 struct file *fp;
363 int com;
364 caddr_t data;
365 struct proc *p;
366 {
367 register struct vnode *vp = ((struct vnode *)fp->f_data);
368 struct vattr vattr;
369 int error;
370
371 switch (vp->v_type) {
372
373 case VREG:
374 case VDIR:
375 if (com == FIONREAD) {
376 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
377 return (error);
378 *(int *)data = vattr.va_size - fp->f_offset;
379 return (0);
380 }
381 if (com == FIONBIO || com == FIOASYNC) /* XXX */
382 return (0); /* XXX */
383 /* fall into ... */
384
385 default:
386 return (ENOTTY);
387
388 case VFIFO:
389 case VCHR:
390 case VBLK:
391 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
392 if (error == 0 && com == TIOCSCTTY) {
393 p->p_session->s_ttyvp = vp;
394 VREF(vp);
395 }
396 return (error);
397 }
398 }
399
400 /*
401 * File table vnode select routine.
402 */
403 vn_select(fp, which, p)
404 struct file *fp;
405 int which;
406 struct proc *p;
407 {
408
409 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
410 fp->f_cred, p));
411 }
412
413 /*
414 * File table vnode close routine.
415 */
416 vn_closefile(fp, p)
417 struct file *fp;
418 struct proc *p;
419 {
420
421 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
422 fp->f_cred, p));
423 }
424