vfs_vnops.c revision 1.55 1 1.55 chs /* $NetBSD: vfs_vnops.c,v 1.55 2002/10/05 22:34:05 chs 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.52 lukem
43 1.52 lukem #include <sys/cdefs.h>
44 1.55 chs __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.55 2002/10/05 22:34:05 chs Exp $");
45 1.26 mrg
46 1.27 thorpej #include "fs_union.h"
47 1.10 cgd
48 1.10 cgd #include <sys/param.h>
49 1.10 cgd #include <sys/systm.h>
50 1.10 cgd #include <sys/kernel.h>
51 1.10 cgd #include <sys/file.h>
52 1.10 cgd #include <sys/stat.h>
53 1.10 cgd #include <sys/buf.h>
54 1.10 cgd #include <sys/proc.h>
55 1.10 cgd #include <sys/mount.h>
56 1.10 cgd #include <sys/namei.h>
57 1.10 cgd #include <sys/vnode.h>
58 1.10 cgd #include <sys/ioctl.h>
59 1.10 cgd #include <sys/tty.h>
60 1.21 mycroft #include <sys/poll.h>
61 1.11 mycroft
62 1.25 mrg #include <uvm/uvm_extern.h>
63 1.25 mrg
64 1.28 fvdl #ifdef UNION
65 1.28 fvdl #include <miscfs/union/union.h>
66 1.28 fvdl #endif
67 1.28 fvdl
68 1.48 jdolecek static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
69 1.48 jdolecek
70 1.48 jdolecek struct fileops vnops = {
71 1.48 jdolecek vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
72 1.48 jdolecek vn_statfile, vn_closefile
73 1.48 jdolecek };
74 1.10 cgd
75 1.10 cgd /*
76 1.10 cgd * Common code for vnode open operations.
77 1.10 cgd * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
78 1.10 cgd */
79 1.20 christos int
80 1.18 mycroft vn_open(ndp, fmode, cmode)
81 1.41 augustss struct nameidata *ndp;
82 1.10 cgd int fmode, cmode;
83 1.10 cgd {
84 1.41 augustss struct vnode *vp;
85 1.41 augustss struct proc *p = ndp->ni_cnd.cn_proc;
86 1.41 augustss struct ucred *cred = p->p_ucred;
87 1.19 mycroft struct vattr va;
88 1.10 cgd int error;
89 1.10 cgd
90 1.10 cgd if (fmode & O_CREAT) {
91 1.11 mycroft ndp->ni_cnd.cn_nameiop = CREATE;
92 1.11 mycroft ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
93 1.38 bouyer if ((fmode & O_EXCL) == 0 &&
94 1.38 bouyer ((fmode & FNOSYMLINK) == 0))
95 1.11 mycroft ndp->ni_cnd.cn_flags |= FOLLOW;
96 1.20 christos if ((error = namei(ndp)) != 0)
97 1.10 cgd return (error);
98 1.10 cgd if (ndp->ni_vp == NULL) {
99 1.19 mycroft VATTR_NULL(&va);
100 1.19 mycroft va.va_type = VREG;
101 1.19 mycroft va.va_mode = cmode;
102 1.28 fvdl if (fmode & O_EXCL)
103 1.28 fvdl va.va_vaflags |= VA_EXCLUSIVE;
104 1.17 mycroft VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
105 1.20 christos error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
106 1.20 christos &ndp->ni_cnd, &va);
107 1.20 christos if (error)
108 1.10 cgd return (error);
109 1.10 cgd fmode &= ~O_TRUNC;
110 1.10 cgd vp = ndp->ni_vp;
111 1.10 cgd } else {
112 1.11 mycroft VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
113 1.10 cgd if (ndp->ni_dvp == ndp->ni_vp)
114 1.10 cgd vrele(ndp->ni_dvp);
115 1.10 cgd else
116 1.10 cgd vput(ndp->ni_dvp);
117 1.10 cgd ndp->ni_dvp = NULL;
118 1.10 cgd vp = ndp->ni_vp;
119 1.10 cgd if (fmode & O_EXCL) {
120 1.10 cgd error = EEXIST;
121 1.38 bouyer goto bad;
122 1.38 bouyer }
123 1.38 bouyer if (ndp->ni_vp->v_type == VLNK) {
124 1.38 bouyer error = EFTYPE;
125 1.10 cgd goto bad;
126 1.10 cgd }
127 1.10 cgd fmode &= ~O_CREAT;
128 1.10 cgd }
129 1.10 cgd } else {
130 1.11 mycroft ndp->ni_cnd.cn_nameiop = LOOKUP;
131 1.11 mycroft ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
132 1.20 christos if ((error = namei(ndp)) != 0)
133 1.10 cgd return (error);
134 1.10 cgd vp = ndp->ni_vp;
135 1.10 cgd }
136 1.10 cgd if (vp->v_type == VSOCK) {
137 1.10 cgd error = EOPNOTSUPP;
138 1.10 cgd goto bad;
139 1.10 cgd }
140 1.10 cgd if ((fmode & O_CREAT) == 0) {
141 1.10 cgd if (fmode & FREAD) {
142 1.20 christos if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
143 1.10 cgd goto bad;
144 1.10 cgd }
145 1.10 cgd if (fmode & (FWRITE | O_TRUNC)) {
146 1.10 cgd if (vp->v_type == VDIR) {
147 1.10 cgd error = EISDIR;
148 1.10 cgd goto bad;
149 1.10 cgd }
150 1.20 christos if ((error = vn_writechk(vp)) != 0 ||
151 1.20 christos (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
152 1.10 cgd goto bad;
153 1.10 cgd }
154 1.10 cgd }
155 1.10 cgd if (fmode & O_TRUNC) {
156 1.28 fvdl VOP_UNLOCK(vp, 0); /* XXX */
157 1.17 mycroft VOP_LEASE(vp, p, cred, LEASE_WRITE);
158 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
159 1.19 mycroft VATTR_NULL(&va);
160 1.19 mycroft va.va_size = 0;
161 1.20 christos if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
162 1.10 cgd goto bad;
163 1.10 cgd }
164 1.20 christos if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
165 1.10 cgd goto bad;
166 1.45 chs if (vp->v_type == VREG &&
167 1.45 chs uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
168 1.45 chs error = EIO;
169 1.45 chs goto bad;
170 1.45 chs }
171 1.10 cgd if (fmode & FWRITE)
172 1.10 cgd vp->v_writecount++;
173 1.45 chs
174 1.10 cgd return (0);
175 1.10 cgd bad:
176 1.10 cgd vput(vp);
177 1.10 cgd return (error);
178 1.10 cgd }
179 1.10 cgd
180 1.10 cgd /*
181 1.10 cgd * Check for write permissions on the specified vnode.
182 1.28 fvdl * Prototype text segments cannot be written.
183 1.10 cgd */
184 1.20 christos int
185 1.10 cgd vn_writechk(vp)
186 1.41 augustss struct vnode *vp;
187 1.10 cgd {
188 1.10 cgd
189 1.10 cgd /*
190 1.45 chs * If the vnode is in use as a process's text,
191 1.45 chs * we can't allow writing.
192 1.10 cgd */
193 1.45 chs if (vp->v_flag & VTEXT)
194 1.25 mrg return (ETXTBSY);
195 1.10 cgd return (0);
196 1.42 chs }
197 1.42 chs
198 1.42 chs /*
199 1.51 thorpej * Mark a vnode as having executable mappings.
200 1.42 chs */
201 1.42 chs void
202 1.51 thorpej vn_markexec(vp)
203 1.42 chs struct vnode *vp;
204 1.42 chs {
205 1.51 thorpej if ((vp->v_flag & VEXECMAP) == 0) {
206 1.53 chs uvmexp.filepages -= vp->v_uobj.uo_npages;
207 1.53 chs uvmexp.execpages += vp->v_uobj.uo_npages;
208 1.46 chs }
209 1.51 thorpej vp->v_flag |= VEXECMAP;
210 1.55 chs }
211 1.55 chs
212 1.55 chs /*
213 1.55 chs * Mark a vnode as being the text of a process.
214 1.55 chs * Fail if the vnode is currently writable.
215 1.55 chs */
216 1.55 chs int
217 1.55 chs vn_marktext(vp)
218 1.55 chs struct vnode *vp;
219 1.55 chs {
220 1.55 chs
221 1.55 chs if (vp->v_writecount != 0) {
222 1.55 chs KASSERT((vp->v_flag & VTEXT) == 0);
223 1.55 chs return (ETXTBSY);
224 1.55 chs }
225 1.55 chs vp->v_flag |= VTEXT;
226 1.55 chs vn_markexec(vp);
227 1.55 chs return (0);
228 1.10 cgd }
229 1.10 cgd
230 1.10 cgd /*
231 1.10 cgd * Vnode close call
232 1.32 wrstuden *
233 1.32 wrstuden * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
234 1.10 cgd */
235 1.20 christos int
236 1.10 cgd vn_close(vp, flags, cred, p)
237 1.41 augustss struct vnode *vp;
238 1.10 cgd int flags;
239 1.10 cgd struct ucred *cred;
240 1.10 cgd struct proc *p;
241 1.10 cgd {
242 1.10 cgd int error;
243 1.10 cgd
244 1.10 cgd if (flags & FWRITE)
245 1.10 cgd vp->v_writecount--;
246 1.32 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
247 1.10 cgd error = VOP_CLOSE(vp, flags, cred, p);
248 1.32 wrstuden vput(vp);
249 1.10 cgd return (error);
250 1.10 cgd }
251 1.10 cgd
252 1.10 cgd /*
253 1.10 cgd * Package up an I/O request on a vnode into a uio and do it.
254 1.10 cgd */
255 1.20 christos int
256 1.10 cgd vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
257 1.10 cgd enum uio_rw rw;
258 1.10 cgd struct vnode *vp;
259 1.10 cgd caddr_t base;
260 1.10 cgd int len;
261 1.10 cgd off_t offset;
262 1.10 cgd enum uio_seg segflg;
263 1.10 cgd int ioflg;
264 1.10 cgd struct ucred *cred;
265 1.30 thorpej size_t *aresid;
266 1.10 cgd struct proc *p;
267 1.10 cgd {
268 1.10 cgd struct uio auio;
269 1.10 cgd struct iovec aiov;
270 1.10 cgd int error;
271 1.10 cgd
272 1.50 chs if ((ioflg & IO_NODELOCKED) == 0) {
273 1.50 chs if (rw == UIO_READ) {
274 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
275 1.50 chs } else {
276 1.50 chs vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
277 1.50 chs }
278 1.50 chs }
279 1.10 cgd auio.uio_iov = &aiov;
280 1.10 cgd auio.uio_iovcnt = 1;
281 1.10 cgd aiov.iov_base = base;
282 1.10 cgd aiov.iov_len = len;
283 1.10 cgd auio.uio_resid = len;
284 1.10 cgd auio.uio_offset = offset;
285 1.10 cgd auio.uio_segflg = segflg;
286 1.10 cgd auio.uio_rw = rw;
287 1.10 cgd auio.uio_procp = p;
288 1.11 mycroft if (rw == UIO_READ) {
289 1.10 cgd error = VOP_READ(vp, &auio, ioflg, cred);
290 1.11 mycroft } else {
291 1.10 cgd error = VOP_WRITE(vp, &auio, ioflg, cred);
292 1.11 mycroft }
293 1.10 cgd if (aresid)
294 1.10 cgd *aresid = auio.uio_resid;
295 1.10 cgd else
296 1.10 cgd if (auio.uio_resid && error == 0)
297 1.10 cgd error = EIO;
298 1.10 cgd if ((ioflg & IO_NODELOCKED) == 0)
299 1.28 fvdl VOP_UNLOCK(vp, 0);
300 1.10 cgd return (error);
301 1.23 fvdl }
302 1.23 fvdl
303 1.23 fvdl int
304 1.23 fvdl vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
305 1.23 fvdl struct file *fp;
306 1.23 fvdl char *buf;
307 1.28 fvdl int segflg, *done, *ncookies;
308 1.23 fvdl u_int count;
309 1.23 fvdl struct proc *p;
310 1.28 fvdl off_t **cookies;
311 1.23 fvdl {
312 1.23 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
313 1.23 fvdl struct iovec aiov;
314 1.23 fvdl struct uio auio;
315 1.23 fvdl int error, eofflag;
316 1.23 fvdl
317 1.23 fvdl unionread:
318 1.23 fvdl if (vp->v_type != VDIR)
319 1.23 fvdl return (EINVAL);
320 1.23 fvdl aiov.iov_base = buf;
321 1.23 fvdl aiov.iov_len = count;
322 1.23 fvdl auio.uio_iov = &aiov;
323 1.23 fvdl auio.uio_iovcnt = 1;
324 1.23 fvdl auio.uio_rw = UIO_READ;
325 1.23 fvdl auio.uio_segflg = segflg;
326 1.23 fvdl auio.uio_procp = p;
327 1.23 fvdl auio.uio_resid = count;
328 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
329 1.23 fvdl auio.uio_offset = fp->f_offset;
330 1.28 fvdl error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
331 1.23 fvdl ncookies);
332 1.23 fvdl fp->f_offset = auio.uio_offset;
333 1.28 fvdl VOP_UNLOCK(vp, 0);
334 1.23 fvdl if (error)
335 1.23 fvdl return (error);
336 1.23 fvdl
337 1.23 fvdl #ifdef UNION
338 1.23 fvdl {
339 1.23 fvdl extern struct vnode *union_dircache __P((struct vnode *));
340 1.23 fvdl
341 1.23 fvdl if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
342 1.23 fvdl struct vnode *lvp;
343 1.23 fvdl
344 1.23 fvdl lvp = union_dircache(vp);
345 1.23 fvdl if (lvp != NULLVP) {
346 1.23 fvdl struct vattr va;
347 1.23 fvdl
348 1.23 fvdl /*
349 1.23 fvdl * If the directory is opaque,
350 1.23 fvdl * then don't show lower entries
351 1.23 fvdl */
352 1.23 fvdl error = VOP_GETATTR(vp, &va, fp->f_cred, p);
353 1.23 fvdl if (va.va_flags & OPAQUE) {
354 1.23 fvdl vput(lvp);
355 1.23 fvdl lvp = NULL;
356 1.23 fvdl }
357 1.23 fvdl }
358 1.23 fvdl
359 1.23 fvdl if (lvp != NULLVP) {
360 1.23 fvdl error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
361 1.23 fvdl if (error) {
362 1.28 fvdl vput(lvp);
363 1.23 fvdl return (error);
364 1.23 fvdl }
365 1.28 fvdl VOP_UNLOCK(lvp, 0);
366 1.23 fvdl fp->f_data = (caddr_t) lvp;
367 1.23 fvdl fp->f_offset = 0;
368 1.23 fvdl error = vn_close(vp, FREAD, fp->f_cred, p);
369 1.23 fvdl if (error)
370 1.23 fvdl return (error);
371 1.23 fvdl vp = lvp;
372 1.23 fvdl goto unionread;
373 1.23 fvdl }
374 1.23 fvdl }
375 1.23 fvdl }
376 1.23 fvdl #endif /* UNION */
377 1.23 fvdl
378 1.23 fvdl if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
379 1.23 fvdl (vp->v_mount->mnt_flag & MNT_UNION)) {
380 1.23 fvdl struct vnode *tvp = vp;
381 1.23 fvdl vp = vp->v_mount->mnt_vnodecovered;
382 1.23 fvdl VREF(vp);
383 1.23 fvdl fp->f_data = (caddr_t) vp;
384 1.23 fvdl fp->f_offset = 0;
385 1.23 fvdl vrele(tvp);
386 1.23 fvdl goto unionread;
387 1.23 fvdl }
388 1.23 fvdl *done = count - auio.uio_resid;
389 1.23 fvdl return error;
390 1.10 cgd }
391 1.10 cgd
392 1.10 cgd /*
393 1.10 cgd * File table vnode read routine.
394 1.10 cgd */
395 1.20 christos int
396 1.29 thorpej vn_read(fp, offset, uio, cred, flags)
397 1.10 cgd struct file *fp;
398 1.29 thorpej off_t *offset;
399 1.10 cgd struct uio *uio;
400 1.10 cgd struct ucred *cred;
401 1.29 thorpej int flags;
402 1.10 cgd {
403 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
404 1.31 kleink int count, error, ioflag = 0;
405 1.10 cgd
406 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
407 1.31 kleink if (fp->f_flag & FNONBLOCK)
408 1.31 kleink ioflag |= IO_NDELAY;
409 1.31 kleink if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
410 1.31 kleink ioflag |= IO_SYNC;
411 1.37 wrstuden if (fp->f_flag & FALTIO)
412 1.37 wrstuden ioflag |= IO_ALTSEMANTICS;
413 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
414 1.29 thorpej uio->uio_offset = *offset;
415 1.10 cgd count = uio->uio_resid;
416 1.31 kleink error = VOP_READ(vp, uio, ioflag, cred);
417 1.29 thorpej if (flags & FOF_UPDATE_OFFSET)
418 1.29 thorpej *offset += count - uio->uio_resid;
419 1.28 fvdl VOP_UNLOCK(vp, 0);
420 1.10 cgd return (error);
421 1.10 cgd }
422 1.10 cgd
423 1.10 cgd /*
424 1.10 cgd * File table vnode write routine.
425 1.10 cgd */
426 1.20 christos int
427 1.29 thorpej vn_write(fp, offset, uio, cred, flags)
428 1.10 cgd struct file *fp;
429 1.29 thorpej off_t *offset;
430 1.10 cgd struct uio *uio;
431 1.10 cgd struct ucred *cred;
432 1.29 thorpej int flags;
433 1.10 cgd {
434 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
435 1.17 mycroft int count, error, ioflag = IO_UNIT;
436 1.10 cgd
437 1.10 cgd if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
438 1.10 cgd ioflag |= IO_APPEND;
439 1.10 cgd if (fp->f_flag & FNONBLOCK)
440 1.10 cgd ioflag |= IO_NDELAY;
441 1.24 thorpej if (fp->f_flag & FFSYNC ||
442 1.24 thorpej (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
443 1.24 thorpej ioflag |= IO_SYNC;
444 1.31 kleink else if (fp->f_flag & FDSYNC)
445 1.31 kleink ioflag |= IO_DSYNC;
446 1.37 wrstuden if (fp->f_flag & FALTIO)
447 1.37 wrstuden ioflag |= IO_ALTSEMANTICS;
448 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
449 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
450 1.29 thorpej uio->uio_offset = *offset;
451 1.10 cgd count = uio->uio_resid;
452 1.10 cgd error = VOP_WRITE(vp, uio, ioflag, cred);
453 1.29 thorpej if (flags & FOF_UPDATE_OFFSET) {
454 1.29 thorpej if (ioflag & IO_APPEND)
455 1.29 thorpej *offset = uio->uio_offset;
456 1.29 thorpej else
457 1.29 thorpej *offset += count - uio->uio_resid;
458 1.29 thorpej }
459 1.28 fvdl VOP_UNLOCK(vp, 0);
460 1.10 cgd return (error);
461 1.10 cgd }
462 1.10 cgd
463 1.10 cgd /*
464 1.10 cgd * File table vnode stat routine.
465 1.10 cgd */
466 1.48 jdolecek static int
467 1.48 jdolecek vn_statfile(fp, sb, p)
468 1.48 jdolecek struct file *fp;
469 1.48 jdolecek struct stat *sb;
470 1.48 jdolecek struct proc *p;
471 1.48 jdolecek {
472 1.48 jdolecek struct vnode *vp = (struct vnode *)fp->f_data;
473 1.48 jdolecek
474 1.48 jdolecek return vn_stat(vp, sb, p);
475 1.48 jdolecek }
476 1.48 jdolecek
477 1.20 christos int
478 1.47 jdolecek vn_stat(fdata, sb, p)
479 1.47 jdolecek void *fdata;
480 1.41 augustss struct stat *sb;
481 1.10 cgd struct proc *p;
482 1.10 cgd {
483 1.47 jdolecek struct vnode *vp = fdata;
484 1.19 mycroft struct vattr va;
485 1.10 cgd int error;
486 1.35 wrstuden mode_t mode;
487 1.10 cgd
488 1.19 mycroft error = VOP_GETATTR(vp, &va, p->p_ucred, p);
489 1.10 cgd if (error)
490 1.10 cgd return (error);
491 1.10 cgd /*
492 1.10 cgd * Copy from vattr table
493 1.10 cgd */
494 1.19 mycroft sb->st_dev = va.va_fsid;
495 1.19 mycroft sb->st_ino = va.va_fileid;
496 1.19 mycroft mode = va.va_mode;
497 1.10 cgd switch (vp->v_type) {
498 1.10 cgd case VREG:
499 1.10 cgd mode |= S_IFREG;
500 1.10 cgd break;
501 1.10 cgd case VDIR:
502 1.10 cgd mode |= S_IFDIR;
503 1.10 cgd break;
504 1.10 cgd case VBLK:
505 1.10 cgd mode |= S_IFBLK;
506 1.10 cgd break;
507 1.10 cgd case VCHR:
508 1.10 cgd mode |= S_IFCHR;
509 1.10 cgd break;
510 1.10 cgd case VLNK:
511 1.10 cgd mode |= S_IFLNK;
512 1.10 cgd break;
513 1.10 cgd case VSOCK:
514 1.10 cgd mode |= S_IFSOCK;
515 1.10 cgd break;
516 1.10 cgd case VFIFO:
517 1.10 cgd mode |= S_IFIFO;
518 1.10 cgd break;
519 1.10 cgd default:
520 1.10 cgd return (EBADF);
521 1.10 cgd };
522 1.10 cgd sb->st_mode = mode;
523 1.19 mycroft sb->st_nlink = va.va_nlink;
524 1.19 mycroft sb->st_uid = va.va_uid;
525 1.19 mycroft sb->st_gid = va.va_gid;
526 1.19 mycroft sb->st_rdev = va.va_rdev;
527 1.19 mycroft sb->st_size = va.va_size;
528 1.19 mycroft sb->st_atimespec = va.va_atime;
529 1.19 mycroft sb->st_mtimespec = va.va_mtime;
530 1.19 mycroft sb->st_ctimespec = va.va_ctime;
531 1.19 mycroft sb->st_blksize = va.va_blocksize;
532 1.19 mycroft sb->st_flags = va.va_flags;
533 1.22 mycroft sb->st_gen = 0;
534 1.19 mycroft sb->st_blocks = va.va_bytes / S_BLKSIZE;
535 1.10 cgd return (0);
536 1.37 wrstuden }
537 1.37 wrstuden
538 1.37 wrstuden /*
539 1.37 wrstuden * File table vnode fcntl routine.
540 1.37 wrstuden */
541 1.37 wrstuden int
542 1.37 wrstuden vn_fcntl(fp, com, data, p)
543 1.37 wrstuden struct file *fp;
544 1.37 wrstuden u_int com;
545 1.37 wrstuden caddr_t data;
546 1.37 wrstuden struct proc *p;
547 1.37 wrstuden {
548 1.41 augustss struct vnode *vp = ((struct vnode *)fp->f_data);
549 1.37 wrstuden int error;
550 1.37 wrstuden
551 1.37 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
552 1.37 wrstuden error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
553 1.37 wrstuden VOP_UNLOCK(vp, 0);
554 1.37 wrstuden return (error);
555 1.10 cgd }
556 1.10 cgd
557 1.10 cgd /*
558 1.10 cgd * File table vnode ioctl routine.
559 1.10 cgd */
560 1.20 christos int
561 1.10 cgd vn_ioctl(fp, com, data, p)
562 1.10 cgd struct file *fp;
563 1.15 cgd u_long com;
564 1.10 cgd caddr_t data;
565 1.10 cgd struct proc *p;
566 1.10 cgd {
567 1.41 augustss struct vnode *vp = ((struct vnode *)fp->f_data);
568 1.10 cgd struct vattr vattr;
569 1.10 cgd int error;
570 1.10 cgd
571 1.10 cgd switch (vp->v_type) {
572 1.10 cgd
573 1.10 cgd case VREG:
574 1.10 cgd case VDIR:
575 1.10 cgd if (com == FIONREAD) {
576 1.20 christos error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
577 1.20 christos if (error)
578 1.10 cgd return (error);
579 1.10 cgd *(int *)data = vattr.va_size - fp->f_offset;
580 1.10 cgd return (0);
581 1.10 cgd }
582 1.10 cgd if (com == FIONBIO || com == FIOASYNC) /* XXX */
583 1.10 cgd return (0); /* XXX */
584 1.10 cgd /* fall into ... */
585 1.10 cgd
586 1.10 cgd default:
587 1.54 atatat return (EPASSTHROUGH);
588 1.10 cgd
589 1.10 cgd case VFIFO:
590 1.10 cgd case VCHR:
591 1.10 cgd case VBLK:
592 1.10 cgd error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
593 1.10 cgd if (error == 0 && com == TIOCSCTTY) {
594 1.13 cgd if (p->p_session->s_ttyvp)
595 1.13 cgd vrele(p->p_session->s_ttyvp);
596 1.10 cgd p->p_session->s_ttyvp = vp;
597 1.10 cgd VREF(vp);
598 1.10 cgd }
599 1.10 cgd return (error);
600 1.10 cgd }
601 1.10 cgd }
602 1.10 cgd
603 1.10 cgd /*
604 1.21 mycroft * File table vnode poll routine.
605 1.10 cgd */
606 1.20 christos int
607 1.21 mycroft vn_poll(fp, events, p)
608 1.10 cgd struct file *fp;
609 1.21 mycroft int events;
610 1.10 cgd struct proc *p;
611 1.10 cgd {
612 1.10 cgd
613 1.21 mycroft return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
614 1.28 fvdl }
615 1.28 fvdl
616 1.28 fvdl /*
617 1.28 fvdl * Check that the vnode is still valid, and if so
618 1.28 fvdl * acquire requested lock.
619 1.28 fvdl */
620 1.28 fvdl int
621 1.28 fvdl vn_lock(vp, flags)
622 1.28 fvdl struct vnode *vp;
623 1.28 fvdl int flags;
624 1.28 fvdl {
625 1.28 fvdl int error;
626 1.34 sommerfe
627 1.28 fvdl do {
628 1.36 mycroft if ((flags & LK_INTERLOCK) == 0)
629 1.28 fvdl simple_lock(&vp->v_interlock);
630 1.28 fvdl if (vp->v_flag & VXLOCK) {
631 1.49 chs if (flags & LK_NOWAIT) {
632 1.49 chs simple_unlock(&vp->v_interlock);
633 1.49 chs return EBUSY;
634 1.49 chs }
635 1.28 fvdl vp->v_flag |= VXWANT;
636 1.49 chs ltsleep(vp, PINOD | PNORELOCK,
637 1.44 sommerfe "vn_lock", 0, &vp->v_interlock);
638 1.28 fvdl error = ENOENT;
639 1.28 fvdl } else {
640 1.28 fvdl error = VOP_LOCK(vp, flags | LK_INTERLOCK);
641 1.49 chs if (error == 0 || error == EDEADLK || error == EBUSY)
642 1.28 fvdl return (error);
643 1.28 fvdl }
644 1.28 fvdl flags &= ~LK_INTERLOCK;
645 1.28 fvdl } while (flags & LK_RETRY);
646 1.28 fvdl return (error);
647 1.10 cgd }
648 1.10 cgd
649 1.10 cgd /*
650 1.10 cgd * File table vnode close routine.
651 1.10 cgd */
652 1.20 christos int
653 1.10 cgd vn_closefile(fp, p)
654 1.10 cgd struct file *fp;
655 1.10 cgd struct proc *p;
656 1.10 cgd {
657 1.10 cgd
658 1.10 cgd return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
659 1.10 cgd fp->f_cred, p));
660 1.39 fvdl }
661 1.39 fvdl
662 1.39 fvdl /*
663 1.39 fvdl * Enable LK_CANRECURSE on lock. Return prior status.
664 1.39 fvdl */
665 1.39 fvdl u_int
666 1.39 fvdl vn_setrecurse(vp)
667 1.39 fvdl struct vnode *vp;
668 1.39 fvdl {
669 1.39 fvdl struct lock *lkp = &vp->v_lock;
670 1.39 fvdl u_int retval = lkp->lk_flags & LK_CANRECURSE;
671 1.39 fvdl
672 1.39 fvdl lkp->lk_flags |= LK_CANRECURSE;
673 1.39 fvdl return retval;
674 1.39 fvdl }
675 1.39 fvdl
676 1.39 fvdl /*
677 1.39 fvdl * Called when done with locksetrecurse.
678 1.39 fvdl */
679 1.39 fvdl void
680 1.39 fvdl vn_restorerecurse(vp, flags)
681 1.39 fvdl struct vnode *vp;
682 1.39 fvdl u_int flags;
683 1.39 fvdl {
684 1.39 fvdl struct lock *lkp = &vp->v_lock;
685 1.39 fvdl
686 1.39 fvdl lkp->lk_flags &= ~LK_CANRECURSE;
687 1.39 fvdl lkp->lk_flags |= flags;
688 1.10 cgd }
689