Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
600f7f07 | 33 | * from: @(#)vfs_vnops.c 7.33 (Berkeley) 6/27/91 |
317350b1 | 34 | * $Id: vfs_vnops.c,v 1.3 1993/11/25 01:33:45 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
38 | #include "systm.h" | |
39 | #include "kernel.h" | |
40 | #include "file.h" | |
41 | #include "stat.h" | |
42 | #include "buf.h" | |
43 | #include "proc.h" | |
44 | #include "mount.h" | |
45 | #include "namei.h" | |
46 | #include "vnode.h" | |
47 | #include "ioctl.h" | |
48 | #include "tty.h" | |
49 | ||
50 | struct fileops vnops = | |
51 | { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile }; | |
52 | ||
53 | /* | |
54 | * Common code for vnode open operations. | |
55 | * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. | |
56 | */ | |
4c45483e | 57 | int |
15637ed4 RG |
58 | vn_open(ndp, p, fmode, cmode) |
59 | register struct nameidata *ndp; | |
60 | struct proc *p; | |
61 | int fmode, cmode; | |
62 | { | |
63 | register struct vnode *vp; | |
64 | register struct ucred *cred = p->p_ucred; | |
65 | struct vattr vat; | |
66 | struct vattr *vap = &vat; | |
67 | int error; | |
68 | ||
69 | if (fmode & O_CREAT) { | |
70 | ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF; | |
71 | if ((fmode & O_EXCL) == 0) | |
72 | ndp->ni_nameiop |= FOLLOW; | |
73 | if (error = namei(ndp, p)) | |
74 | return (error); | |
75 | if (ndp->ni_vp == NULL) { | |
76 | VATTR_NULL(vap); | |
77 | vap->va_type = VREG; | |
78 | vap->va_mode = cmode; | |
79 | if (error = VOP_CREATE(ndp, vap, p)) | |
80 | return (error); | |
81 | fmode &= ~O_TRUNC; | |
82 | vp = ndp->ni_vp; | |
83 | } else { | |
84 | VOP_ABORTOP(ndp); | |
85 | if (ndp->ni_dvp == ndp->ni_vp) | |
86 | vrele(ndp->ni_dvp); | |
87 | else | |
88 | vput(ndp->ni_dvp); | |
89 | ndp->ni_dvp = NULL; | |
90 | vp = ndp->ni_vp; | |
91 | if (fmode & O_EXCL) { | |
92 | error = EEXIST; | |
93 | goto bad; | |
94 | } | |
95 | fmode &= ~O_CREAT; | |
96 | } | |
97 | } else { | |
98 | ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; | |
99 | if (error = namei(ndp, p)) | |
100 | return (error); | |
101 | vp = ndp->ni_vp; | |
102 | } | |
103 | if (vp->v_type == VSOCK) { | |
104 | error = EOPNOTSUPP; | |
105 | goto bad; | |
106 | } | |
107 | if ((fmode & O_CREAT) == 0) { | |
108 | if (fmode & FREAD) { | |
109 | if (error = VOP_ACCESS(vp, VREAD, cred, p)) | |
110 | goto bad; | |
111 | } | |
112 | if (fmode & (FWRITE | O_TRUNC)) { | |
113 | if (vp->v_type == VDIR) { | |
114 | error = EISDIR; | |
115 | goto bad; | |
116 | } | |
117 | if ((error = vn_writechk(vp)) || | |
118 | (error = VOP_ACCESS(vp, VWRITE, cred, p))) | |
119 | goto bad; | |
120 | } | |
121 | } | |
122 | if (fmode & O_TRUNC) { | |
123 | VATTR_NULL(vap); | |
124 | vap->va_size = 0; | |
125 | if (error = VOP_SETATTR(vp, vap, cred, p)) | |
126 | goto bad; | |
127 | } | |
128 | if (error = VOP_OPEN(vp, fmode, cred, p)) | |
129 | goto bad; | |
130 | if (fmode & FWRITE) | |
131 | vp->v_writecount++; | |
132 | return (0); | |
133 | bad: | |
134 | vput(vp); | |
135 | return (error); | |
136 | } | |
137 | ||
138 | /* | |
139 | * Check for write permissions on the specified vnode. | |
140 | * The read-only status of the file system is checked. | |
141 | * Also, prototype text segments cannot be written. | |
142 | */ | |
4c45483e | 143 | int |
15637ed4 RG |
144 | vn_writechk(vp) |
145 | register struct vnode *vp; | |
146 | { | |
147 | ||
148 | /* | |
149 | * Disallow write attempts on read-only file systems; | |
150 | * unless the file is a socket or a block or character | |
151 | * device resident on the file system. | |
152 | */ | |
153 | if (vp->v_mount->mnt_flag & MNT_RDONLY) { | |
154 | switch (vp->v_type) { | |
155 | case VREG: case VDIR: case VLNK: | |
156 | return (EROFS); | |
157 | } | |
158 | } | |
159 | /* | |
160 | * If there's shared text associated with | |
161 | * the vnode, try to free it up once. If | |
162 | * we fail, we can't allow writing. | |
163 | */ | |
164 | if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp)) | |
165 | return (ETXTBSY); | |
166 | return (0); | |
167 | } | |
168 | ||
169 | /* | |
170 | * Vnode close call | |
171 | */ | |
4c45483e | 172 | int |
15637ed4 RG |
173 | vn_close(vp, flags, cred, p) |
174 | register struct vnode *vp; | |
175 | int flags; | |
176 | struct ucred *cred; | |
177 | struct proc *p; | |
178 | { | |
179 | int error; | |
180 | ||
181 | if (flags & FWRITE) | |
182 | vp->v_writecount--; | |
183 | error = VOP_CLOSE(vp, flags, cred, p); | |
184 | vrele(vp); | |
185 | return (error); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Package up an I/O request on a vnode into a uio and do it. | |
190 | * [internal interface to file i/o for kernel only] | |
191 | */ | |
4c45483e | 192 | int |
15637ed4 RG |
193 | vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) |
194 | enum uio_rw rw; | |
195 | struct vnode *vp; | |
196 | caddr_t base; | |
197 | int len; | |
198 | off_t offset; | |
199 | enum uio_seg segflg; | |
200 | int ioflg; | |
201 | struct ucred *cred; | |
202 | int *aresid; | |
203 | struct proc *p; | |
204 | { | |
205 | struct uio auio; | |
206 | struct iovec aiov; | |
207 | int error; | |
208 | ||
209 | if ((ioflg & IO_NODELOCKED) == 0) | |
210 | VOP_LOCK(vp); | |
211 | auio.uio_iov = &aiov; | |
212 | auio.uio_iovcnt = 1; | |
213 | aiov.iov_base = base; | |
214 | aiov.iov_len = len; | |
215 | auio.uio_resid = len; | |
216 | auio.uio_offset = offset; | |
217 | auio.uio_segflg = segflg; | |
218 | auio.uio_rw = rw; | |
219 | auio.uio_procp = p; | |
220 | if (rw == UIO_READ) | |
221 | error = VOP_READ(vp, &auio, ioflg, cred); | |
222 | else | |
223 | error = VOP_WRITE(vp, &auio, ioflg, cred); | |
224 | if (aresid) | |
225 | *aresid = auio.uio_resid; | |
226 | else | |
227 | if (auio.uio_resid && error == 0) | |
228 | error = EIO; | |
229 | if ((ioflg & IO_NODELOCKED) == 0) | |
230 | VOP_UNLOCK(vp); | |
231 | return (error); | |
232 | } | |
233 | ||
234 | /* | |
235 | * File table vnode read routine. | |
236 | */ | |
4c45483e | 237 | int |
15637ed4 RG |
238 | vn_read(fp, uio, cred) |
239 | struct file *fp; | |
240 | struct uio *uio; | |
241 | struct ucred *cred; | |
242 | { | |
243 | register struct vnode *vp = (struct vnode *)fp->f_data; | |
244 | int count, error; | |
245 | ||
246 | VOP_LOCK(vp); | |
247 | uio->uio_offset = fp->f_offset; | |
248 | count = uio->uio_resid; | |
249 | error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, | |
250 | cred); | |
251 | fp->f_offset += count - uio->uio_resid; | |
252 | VOP_UNLOCK(vp); | |
253 | return (error); | |
254 | } | |
255 | ||
256 | /* | |
257 | * File table vnode write routine. | |
258 | */ | |
4c45483e | 259 | int |
15637ed4 RG |
260 | vn_write(fp, uio, cred) |
261 | struct file *fp; | |
262 | struct uio *uio; | |
263 | struct ucred *cred; | |
264 | { | |
265 | register struct vnode *vp = (struct vnode *)fp->f_data; | |
266 | int count, error, ioflag = 0; | |
267 | ||
268 | if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) | |
269 | ioflag |= IO_APPEND; | |
270 | if (fp->f_flag & FNONBLOCK) | |
271 | ioflag |= IO_NDELAY; | |
272 | VOP_LOCK(vp); | |
273 | uio->uio_offset = fp->f_offset; | |
274 | count = uio->uio_resid; | |
275 | error = VOP_WRITE(vp, uio, ioflag, cred); | |
276 | if (ioflag & IO_APPEND) | |
277 | fp->f_offset = uio->uio_offset; | |
278 | else | |
279 | fp->f_offset += count - uio->uio_resid; | |
280 | VOP_UNLOCK(vp); | |
281 | return (error); | |
282 | } | |
283 | ||
284 | /* | |
285 | * File table vnode stat routine. | |
286 | */ | |
4c45483e | 287 | int |
15637ed4 RG |
288 | vn_stat(vp, sb, p) |
289 | struct vnode *vp; | |
290 | register struct stat *sb; | |
291 | struct proc *p; | |
292 | { | |
293 | struct vattr vattr; | |
294 | register struct vattr *vap; | |
295 | int error; | |
296 | u_short mode; | |
297 | ||
298 | vap = &vattr; | |
299 | error = VOP_GETATTR(vp, vap, p->p_ucred, p); | |
300 | if (error) | |
301 | return (error); | |
302 | /* | |
303 | * Copy from vattr table | |
304 | */ | |
305 | sb->st_dev = vap->va_fsid; | |
306 | sb->st_ino = vap->va_fileid; | |
307 | mode = vap->va_mode; | |
308 | switch (vp->v_type) { | |
309 | case VREG: | |
317350b1 | 310 | case VPROC: |
15637ed4 RG |
311 | mode |= S_IFREG; |
312 | break; | |
313 | case VDIR: | |
314 | mode |= S_IFDIR; | |
315 | break; | |
316 | case VBLK: | |
317 | mode |= S_IFBLK; | |
318 | break; | |
319 | case VCHR: | |
320 | mode |= S_IFCHR; | |
321 | break; | |
322 | case VLNK: | |
323 | mode |= S_IFLNK; | |
324 | break; | |
325 | case VSOCK: | |
326 | mode |= S_IFSOCK; | |
327 | break; | |
328 | case VFIFO: | |
329 | mode |= S_IFIFO; | |
330 | break; | |
331 | default: | |
332 | return (EBADF); | |
333 | }; | |
334 | sb->st_mode = mode; | |
335 | sb->st_nlink = vap->va_nlink; | |
336 | sb->st_uid = vap->va_uid; | |
337 | sb->st_gid = vap->va_gid; | |
338 | sb->st_rdev = vap->va_rdev; | |
339 | sb->st_size = vap->va_size; | |
340 | sb->st_atime = vap->va_atime.tv_sec; | |
341 | sb->st_spare1 = 0; | |
342 | sb->st_mtime = vap->va_mtime.tv_sec; | |
343 | sb->st_spare2 = 0; | |
344 | sb->st_ctime = vap->va_ctime.tv_sec; | |
345 | sb->st_spare3 = 0; | |
346 | sb->st_blksize = vap->va_blocksize; | |
347 | sb->st_flags = vap->va_flags; | |
348 | sb->st_gen = vap->va_gen; | |
349 | sb->st_blocks = vap->va_bytes / S_BLKSIZE; | |
350 | return (0); | |
351 | } | |
352 | ||
353 | /* | |
354 | * File table vnode ioctl routine. | |
355 | */ | |
4c45483e | 356 | int |
15637ed4 RG |
357 | vn_ioctl(fp, com, data, p) |
358 | struct file *fp; | |
359 | int com; | |
360 | caddr_t data; | |
361 | struct proc *p; | |
362 | { | |
363 | register struct vnode *vp = ((struct vnode *)fp->f_data); | |
364 | struct vattr vattr; | |
365 | int error; | |
366 | ||
367 | switch (vp->v_type) { | |
368 | ||
369 | case VREG: | |
370 | case VDIR: | |
371 | if (com == FIONREAD) { | |
372 | if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) | |
373 | return (error); | |
374 | *(off_t *)data = vattr.va_size - fp->f_offset; | |
375 | return (0); | |
376 | } | |
377 | if (com == FIONBIO || com == FIOASYNC) /* XXX */ | |
378 | return (0); /* XXX */ | |
379 | /* fall into ... */ | |
380 | ||
381 | default: | |
382 | return (ENOTTY); | |
383 | ||
384 | case VFIFO: | |
385 | case VCHR: | |
386 | case VBLK: | |
387 | error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); | |
388 | if (error == 0 && com == TIOCSCTTY) { | |
389 | p->p_session->s_ttyvp = vp; | |
390 | VREF(vp); | |
391 | } | |
392 | return (error); | |
393 | } | |
394 | } | |
395 | ||
396 | /* | |
397 | * File table vnode select routine. | |
398 | */ | |
4c45483e | 399 | int |
15637ed4 RG |
400 | vn_select(fp, which, p) |
401 | struct file *fp; | |
402 | int which; | |
403 | struct proc *p; | |
404 | { | |
405 | ||
406 | return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag, | |
407 | fp->f_cred, p)); | |
408 | } | |
409 | ||
410 | /* | |
411 | * File table vnode close routine. | |
412 | */ | |
4c45483e | 413 | int |
15637ed4 RG |
414 | vn_closefile(fp, p) |
415 | struct file *fp; | |
416 | struct proc *p; | |
417 | { | |
418 | ||
419 | return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, | |
420 | fp->f_cred, p)); | |
421 | } | |
422 | ||
423 | /* | |
424 | * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked) | |
425 | * - look up fsid in mount list (if not found ret error) | |
426 | * - get vp by calling VFS_FHTOVP() macro | |
427 | * - if lockflag lock it with VOP_LOCK() | |
428 | */ | |
4c45483e | 429 | int |
15637ed4 RG |
430 | vn_fhtovp(fhp, lockflag, vpp) |
431 | fhandle_t *fhp; | |
432 | int lockflag; | |
433 | struct vnode **vpp; | |
434 | { | |
435 | register struct mount *mp; | |
436 | ||
437 | if ((mp = getvfs(&fhp->fh_fsid)) == NULL) | |
438 | return (ESTALE); | |
439 | if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) | |
440 | return (ESTALE); | |
441 | if (!lockflag) | |
442 | VOP_UNLOCK(*vpp); | |
443 | return (0); | |
444 | } |