vfs_vnops.c revision 1.35 1 1.35 wrstuden /* $NetBSD: vfs_vnops.c,v 1.35 1999/03/30 00:16:44 wrstuden 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.10 cgd
45 1.10 cgd #include <sys/param.h>
46 1.10 cgd #include <sys/systm.h>
47 1.10 cgd #include <sys/kernel.h>
48 1.10 cgd #include <sys/file.h>
49 1.10 cgd #include <sys/stat.h>
50 1.10 cgd #include <sys/buf.h>
51 1.10 cgd #include <sys/proc.h>
52 1.10 cgd #include <sys/mount.h>
53 1.10 cgd #include <sys/namei.h>
54 1.10 cgd #include <sys/vnode.h>
55 1.10 cgd #include <sys/ioctl.h>
56 1.10 cgd #include <sys/tty.h>
57 1.21 mycroft #include <sys/poll.h>
58 1.10 cgd
59 1.11 mycroft #include <vm/vm.h>
60 1.11 mycroft
61 1.25 mrg #include <uvm/uvm_extern.h>
62 1.25 mrg
63 1.28 fvdl #ifdef UNION
64 1.28 fvdl #include <miscfs/union/union.h>
65 1.28 fvdl #endif
66 1.28 fvdl
67 1.10 cgd struct fileops vnops =
68 1.21 mycroft { vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile };
69 1.10 cgd
70 1.10 cgd /*
71 1.10 cgd * Common code for vnode open operations.
72 1.10 cgd * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
73 1.10 cgd */
74 1.20 christos int
75 1.18 mycroft vn_open(ndp, fmode, cmode)
76 1.10 cgd register struct nameidata *ndp;
77 1.10 cgd int fmode, cmode;
78 1.10 cgd {
79 1.10 cgd register struct vnode *vp;
80 1.11 mycroft register struct proc *p = ndp->ni_cnd.cn_proc;
81 1.10 cgd register struct ucred *cred = p->p_ucred;
82 1.19 mycroft struct vattr va;
83 1.10 cgd int error;
84 1.10 cgd
85 1.10 cgd if (fmode & O_CREAT) {
86 1.11 mycroft ndp->ni_cnd.cn_nameiop = CREATE;
87 1.11 mycroft ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
88 1.10 cgd if ((fmode & O_EXCL) == 0)
89 1.11 mycroft ndp->ni_cnd.cn_flags |= FOLLOW;
90 1.20 christos if ((error = namei(ndp)) != 0)
91 1.10 cgd return (error);
92 1.10 cgd if (ndp->ni_vp == NULL) {
93 1.19 mycroft VATTR_NULL(&va);
94 1.19 mycroft va.va_type = VREG;
95 1.19 mycroft va.va_mode = cmode;
96 1.28 fvdl if (fmode & O_EXCL)
97 1.28 fvdl va.va_vaflags |= VA_EXCLUSIVE;
98 1.17 mycroft VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
99 1.20 christos error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
100 1.20 christos &ndp->ni_cnd, &va);
101 1.20 christos if (error)
102 1.10 cgd return (error);
103 1.10 cgd fmode &= ~O_TRUNC;
104 1.10 cgd vp = ndp->ni_vp;
105 1.10 cgd } else {
106 1.11 mycroft VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
107 1.10 cgd if (ndp->ni_dvp == ndp->ni_vp)
108 1.10 cgd vrele(ndp->ni_dvp);
109 1.10 cgd else
110 1.10 cgd vput(ndp->ni_dvp);
111 1.10 cgd ndp->ni_dvp = NULL;
112 1.10 cgd vp = ndp->ni_vp;
113 1.10 cgd if (fmode & O_EXCL) {
114 1.10 cgd error = EEXIST;
115 1.10 cgd goto bad;
116 1.10 cgd }
117 1.10 cgd fmode &= ~O_CREAT;
118 1.10 cgd }
119 1.10 cgd } else {
120 1.11 mycroft ndp->ni_cnd.cn_nameiop = LOOKUP;
121 1.11 mycroft ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
122 1.20 christos if ((error = namei(ndp)) != 0)
123 1.10 cgd return (error);
124 1.10 cgd vp = ndp->ni_vp;
125 1.10 cgd }
126 1.10 cgd if (vp->v_type == VSOCK) {
127 1.10 cgd error = EOPNOTSUPP;
128 1.10 cgd goto bad;
129 1.10 cgd }
130 1.10 cgd if ((fmode & O_CREAT) == 0) {
131 1.10 cgd if (fmode & FREAD) {
132 1.20 christos if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
133 1.10 cgd goto bad;
134 1.10 cgd }
135 1.10 cgd if (fmode & (FWRITE | O_TRUNC)) {
136 1.10 cgd if (vp->v_type == VDIR) {
137 1.10 cgd error = EISDIR;
138 1.10 cgd goto bad;
139 1.10 cgd }
140 1.20 christos if ((error = vn_writechk(vp)) != 0 ||
141 1.20 christos (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
142 1.10 cgd goto bad;
143 1.10 cgd }
144 1.10 cgd }
145 1.10 cgd if (fmode & O_TRUNC) {
146 1.28 fvdl VOP_UNLOCK(vp, 0); /* XXX */
147 1.17 mycroft VOP_LEASE(vp, p, cred, LEASE_WRITE);
148 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
149 1.19 mycroft VATTR_NULL(&va);
150 1.19 mycroft va.va_size = 0;
151 1.20 christos if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
152 1.10 cgd goto bad;
153 1.10 cgd }
154 1.20 christos if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
155 1.10 cgd goto bad;
156 1.10 cgd if (fmode & FWRITE)
157 1.10 cgd vp->v_writecount++;
158 1.10 cgd return (0);
159 1.10 cgd bad:
160 1.10 cgd vput(vp);
161 1.10 cgd return (error);
162 1.10 cgd }
163 1.10 cgd
164 1.10 cgd /*
165 1.10 cgd * Check for write permissions on the specified vnode.
166 1.28 fvdl * Prototype text segments cannot be written.
167 1.10 cgd */
168 1.20 christos int
169 1.10 cgd vn_writechk(vp)
170 1.10 cgd register struct vnode *vp;
171 1.10 cgd {
172 1.10 cgd
173 1.10 cgd /*
174 1.10 cgd * If there's shared text associated with
175 1.10 cgd * the vnode, try to free it up once. If
176 1.10 cgd * we fail, we can't allow writing.
177 1.10 cgd */
178 1.25 mrg if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
179 1.25 mrg return (ETXTBSY);
180 1.10 cgd return (0);
181 1.10 cgd }
182 1.10 cgd
183 1.10 cgd /*
184 1.10 cgd * Vnode close call
185 1.32 wrstuden *
186 1.32 wrstuden * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
187 1.10 cgd */
188 1.20 christos int
189 1.10 cgd vn_close(vp, flags, cred, p)
190 1.10 cgd register struct vnode *vp;
191 1.10 cgd int flags;
192 1.10 cgd struct ucred *cred;
193 1.10 cgd struct proc *p;
194 1.10 cgd {
195 1.10 cgd int error;
196 1.10 cgd
197 1.10 cgd if (flags & FWRITE)
198 1.10 cgd vp->v_writecount--;
199 1.32 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
200 1.10 cgd error = VOP_CLOSE(vp, flags, cred, p);
201 1.32 wrstuden vput(vp);
202 1.10 cgd return (error);
203 1.10 cgd }
204 1.10 cgd
205 1.10 cgd /*
206 1.10 cgd * Package up an I/O request on a vnode into a uio and do it.
207 1.10 cgd */
208 1.20 christos int
209 1.10 cgd vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
210 1.10 cgd enum uio_rw rw;
211 1.10 cgd struct vnode *vp;
212 1.10 cgd caddr_t base;
213 1.10 cgd int len;
214 1.10 cgd off_t offset;
215 1.10 cgd enum uio_seg segflg;
216 1.10 cgd int ioflg;
217 1.10 cgd struct ucred *cred;
218 1.30 thorpej size_t *aresid;
219 1.10 cgd struct proc *p;
220 1.10 cgd {
221 1.10 cgd struct uio auio;
222 1.10 cgd struct iovec aiov;
223 1.10 cgd int error;
224 1.10 cgd
225 1.10 cgd if ((ioflg & IO_NODELOCKED) == 0)
226 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
227 1.10 cgd auio.uio_iov = &aiov;
228 1.10 cgd auio.uio_iovcnt = 1;
229 1.10 cgd aiov.iov_base = base;
230 1.10 cgd aiov.iov_len = len;
231 1.10 cgd auio.uio_resid = len;
232 1.10 cgd auio.uio_offset = offset;
233 1.10 cgd auio.uio_segflg = segflg;
234 1.10 cgd auio.uio_rw = rw;
235 1.10 cgd auio.uio_procp = p;
236 1.11 mycroft if (rw == UIO_READ) {
237 1.10 cgd error = VOP_READ(vp, &auio, ioflg, cred);
238 1.11 mycroft } else {
239 1.10 cgd error = VOP_WRITE(vp, &auio, ioflg, cred);
240 1.11 mycroft }
241 1.10 cgd if (aresid)
242 1.10 cgd *aresid = auio.uio_resid;
243 1.10 cgd else
244 1.10 cgd if (auio.uio_resid && error == 0)
245 1.10 cgd error = EIO;
246 1.10 cgd if ((ioflg & IO_NODELOCKED) == 0)
247 1.28 fvdl VOP_UNLOCK(vp, 0);
248 1.10 cgd return (error);
249 1.23 fvdl }
250 1.23 fvdl
251 1.23 fvdl int
252 1.23 fvdl vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
253 1.23 fvdl struct file *fp;
254 1.23 fvdl char *buf;
255 1.28 fvdl int segflg, *done, *ncookies;
256 1.23 fvdl u_int count;
257 1.23 fvdl struct proc *p;
258 1.28 fvdl off_t **cookies;
259 1.23 fvdl {
260 1.23 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
261 1.23 fvdl struct iovec aiov;
262 1.23 fvdl struct uio auio;
263 1.23 fvdl int error, eofflag;
264 1.23 fvdl
265 1.23 fvdl unionread:
266 1.23 fvdl if (vp->v_type != VDIR)
267 1.23 fvdl return (EINVAL);
268 1.23 fvdl aiov.iov_base = buf;
269 1.23 fvdl aiov.iov_len = count;
270 1.23 fvdl auio.uio_iov = &aiov;
271 1.23 fvdl auio.uio_iovcnt = 1;
272 1.23 fvdl auio.uio_rw = UIO_READ;
273 1.23 fvdl auio.uio_segflg = segflg;
274 1.23 fvdl auio.uio_procp = p;
275 1.23 fvdl auio.uio_resid = count;
276 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
277 1.23 fvdl auio.uio_offset = fp->f_offset;
278 1.28 fvdl error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
279 1.23 fvdl ncookies);
280 1.23 fvdl fp->f_offset = auio.uio_offset;
281 1.28 fvdl VOP_UNLOCK(vp, 0);
282 1.23 fvdl if (error)
283 1.23 fvdl return (error);
284 1.23 fvdl
285 1.23 fvdl #ifdef UNION
286 1.23 fvdl {
287 1.23 fvdl extern int (**union_vnodeop_p) __P((void *));
288 1.23 fvdl extern struct vnode *union_dircache __P((struct vnode *));
289 1.23 fvdl
290 1.23 fvdl if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
291 1.23 fvdl struct vnode *lvp;
292 1.23 fvdl
293 1.23 fvdl lvp = union_dircache(vp);
294 1.23 fvdl if (lvp != NULLVP) {
295 1.23 fvdl struct vattr va;
296 1.23 fvdl
297 1.23 fvdl /*
298 1.23 fvdl * If the directory is opaque,
299 1.23 fvdl * then don't show lower entries
300 1.23 fvdl */
301 1.23 fvdl error = VOP_GETATTR(vp, &va, fp->f_cred, p);
302 1.23 fvdl if (va.va_flags & OPAQUE) {
303 1.23 fvdl vput(lvp);
304 1.23 fvdl lvp = NULL;
305 1.23 fvdl }
306 1.23 fvdl }
307 1.23 fvdl
308 1.23 fvdl if (lvp != NULLVP) {
309 1.23 fvdl error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
310 1.23 fvdl if (error) {
311 1.28 fvdl vput(lvp);
312 1.23 fvdl return (error);
313 1.23 fvdl }
314 1.28 fvdl VOP_UNLOCK(lvp, 0);
315 1.23 fvdl fp->f_data = (caddr_t) lvp;
316 1.23 fvdl fp->f_offset = 0;
317 1.23 fvdl error = vn_close(vp, FREAD, fp->f_cred, p);
318 1.23 fvdl if (error)
319 1.23 fvdl return (error);
320 1.23 fvdl vp = lvp;
321 1.23 fvdl goto unionread;
322 1.23 fvdl }
323 1.23 fvdl }
324 1.23 fvdl }
325 1.23 fvdl #endif /* UNION */
326 1.23 fvdl
327 1.23 fvdl if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
328 1.23 fvdl (vp->v_mount->mnt_flag & MNT_UNION)) {
329 1.23 fvdl struct vnode *tvp = vp;
330 1.23 fvdl vp = vp->v_mount->mnt_vnodecovered;
331 1.23 fvdl VREF(vp);
332 1.23 fvdl fp->f_data = (caddr_t) vp;
333 1.23 fvdl fp->f_offset = 0;
334 1.23 fvdl vrele(tvp);
335 1.23 fvdl goto unionread;
336 1.23 fvdl }
337 1.23 fvdl *done = count - auio.uio_resid;
338 1.23 fvdl return error;
339 1.10 cgd }
340 1.10 cgd
341 1.10 cgd /*
342 1.10 cgd * File table vnode read routine.
343 1.10 cgd */
344 1.20 christos int
345 1.29 thorpej vn_read(fp, offset, uio, cred, flags)
346 1.10 cgd struct file *fp;
347 1.29 thorpej off_t *offset;
348 1.10 cgd struct uio *uio;
349 1.10 cgd struct ucred *cred;
350 1.29 thorpej int flags;
351 1.10 cgd {
352 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
353 1.31 kleink int count, error, ioflag = 0;
354 1.10 cgd
355 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
356 1.31 kleink if (fp->f_flag & FNONBLOCK)
357 1.31 kleink ioflag |= IO_NDELAY;
358 1.31 kleink if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
359 1.31 kleink ioflag |= IO_SYNC;
360 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
361 1.29 thorpej uio->uio_offset = *offset;
362 1.10 cgd count = uio->uio_resid;
363 1.31 kleink error = VOP_READ(vp, uio, ioflag, cred);
364 1.29 thorpej if (flags & FOF_UPDATE_OFFSET)
365 1.29 thorpej *offset += count - uio->uio_resid;
366 1.28 fvdl VOP_UNLOCK(vp, 0);
367 1.10 cgd return (error);
368 1.10 cgd }
369 1.10 cgd
370 1.10 cgd /*
371 1.10 cgd * File table vnode write routine.
372 1.10 cgd */
373 1.20 christos int
374 1.29 thorpej vn_write(fp, offset, uio, cred, flags)
375 1.10 cgd struct file *fp;
376 1.29 thorpej off_t *offset;
377 1.10 cgd struct uio *uio;
378 1.10 cgd struct ucred *cred;
379 1.29 thorpej int flags;
380 1.10 cgd {
381 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
382 1.17 mycroft int count, error, ioflag = IO_UNIT;
383 1.10 cgd
384 1.10 cgd if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
385 1.10 cgd ioflag |= IO_APPEND;
386 1.10 cgd if (fp->f_flag & FNONBLOCK)
387 1.10 cgd ioflag |= IO_NDELAY;
388 1.24 thorpej if (fp->f_flag & FFSYNC ||
389 1.24 thorpej (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
390 1.24 thorpej ioflag |= IO_SYNC;
391 1.31 kleink else if (fp->f_flag & FDSYNC)
392 1.31 kleink ioflag |= IO_DSYNC;
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.35 wrstuden mode_t 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.34 sommerfe int have_interlock = 0;
541 1.34 sommerfe
542 1.28 fvdl do {
543 1.34 sommerfe if ((flags & LK_INTERLOCK) == 0) {
544 1.28 fvdl simple_lock(&vp->v_interlock);
545 1.34 sommerfe have_interlock++;
546 1.34 sommerfe }
547 1.28 fvdl if (vp->v_flag & VXLOCK) {
548 1.28 fvdl vp->v_flag |= VXWANT;
549 1.28 fvdl simple_unlock(&vp->v_interlock);
550 1.28 fvdl tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
551 1.28 fvdl error = ENOENT;
552 1.28 fvdl } else {
553 1.28 fvdl error = VOP_LOCK(vp, flags | LK_INTERLOCK);
554 1.28 fvdl if (error == 0)
555 1.28 fvdl return (error);
556 1.34 sommerfe }
557 1.34 sommerfe if (error == EDEADLK) {
558 1.34 sommerfe if (have_interlock)
559 1.34 sommerfe simple_unlock(&vp->v_interlock);
560 1.34 sommerfe break;
561 1.28 fvdl }
562 1.28 fvdl flags &= ~LK_INTERLOCK;
563 1.28 fvdl } while (flags & LK_RETRY);
564 1.28 fvdl return (error);
565 1.10 cgd }
566 1.10 cgd
567 1.10 cgd /*
568 1.10 cgd * File table vnode close routine.
569 1.10 cgd */
570 1.20 christos int
571 1.10 cgd vn_closefile(fp, p)
572 1.10 cgd struct file *fp;
573 1.10 cgd struct proc *p;
574 1.10 cgd {
575 1.10 cgd
576 1.10 cgd return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
577 1.10 cgd fp->f_cred, p));
578 1.10 cgd }
579