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