vfs_vnops.c revision 1.54.6.2 1 1.54.6.2 simonb /* $NetBSD: vfs_vnops.c,v 1.54.6.2 2006/06/01 05:57:19 simonb 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.54.6.2 simonb __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.54.6.2 2006/06/01 05:57:19 simonb 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.54.6.1 tron }
211 1.54.6.1 tron
212 1.54.6.1 tron /*
213 1.54.6.1 tron * Mark a vnode as being the text of a process.
214 1.54.6.1 tron * Fail if the vnode is currently writable.
215 1.54.6.1 tron */
216 1.54.6.1 tron int
217 1.54.6.1 tron vn_marktext(vp)
218 1.54.6.1 tron struct vnode *vp;
219 1.54.6.1 tron {
220 1.54.6.1 tron
221 1.54.6.1 tron if (vp->v_writecount != 0) {
222 1.54.6.1 tron KASSERT((vp->v_flag & VTEXT) == 0);
223 1.54.6.1 tron return (ETXTBSY);
224 1.54.6.1 tron }
225 1.54.6.1 tron vp->v_flag |= VTEXT;
226 1.54.6.1 tron vn_markexec(vp);
227 1.54.6.1 tron 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.54.6.2 simonb /* Limit the size on any kernel buffers used by VOP_READDIR */
318 1.54.6.2 simonb count = min(MAXBSIZE, count);
319 1.54.6.2 simonb
320 1.23 fvdl unionread:
321 1.23 fvdl if (vp->v_type != VDIR)
322 1.23 fvdl return (EINVAL);
323 1.23 fvdl aiov.iov_base = buf;
324 1.23 fvdl aiov.iov_len = count;
325 1.23 fvdl auio.uio_iov = &aiov;
326 1.23 fvdl auio.uio_iovcnt = 1;
327 1.23 fvdl auio.uio_rw = UIO_READ;
328 1.23 fvdl auio.uio_segflg = segflg;
329 1.23 fvdl auio.uio_procp = p;
330 1.23 fvdl auio.uio_resid = count;
331 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
332 1.23 fvdl auio.uio_offset = fp->f_offset;
333 1.28 fvdl error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
334 1.23 fvdl ncookies);
335 1.23 fvdl fp->f_offset = auio.uio_offset;
336 1.28 fvdl VOP_UNLOCK(vp, 0);
337 1.23 fvdl if (error)
338 1.23 fvdl return (error);
339 1.23 fvdl
340 1.23 fvdl #ifdef UNION
341 1.23 fvdl {
342 1.23 fvdl extern struct vnode *union_dircache __P((struct vnode *));
343 1.23 fvdl
344 1.23 fvdl if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
345 1.23 fvdl struct vnode *lvp;
346 1.23 fvdl
347 1.23 fvdl lvp = union_dircache(vp);
348 1.23 fvdl if (lvp != NULLVP) {
349 1.23 fvdl struct vattr va;
350 1.23 fvdl
351 1.23 fvdl /*
352 1.23 fvdl * If the directory is opaque,
353 1.23 fvdl * then don't show lower entries
354 1.23 fvdl */
355 1.23 fvdl error = VOP_GETATTR(vp, &va, fp->f_cred, p);
356 1.23 fvdl if (va.va_flags & OPAQUE) {
357 1.23 fvdl vput(lvp);
358 1.23 fvdl lvp = NULL;
359 1.23 fvdl }
360 1.23 fvdl }
361 1.23 fvdl
362 1.23 fvdl if (lvp != NULLVP) {
363 1.23 fvdl error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
364 1.23 fvdl if (error) {
365 1.28 fvdl vput(lvp);
366 1.23 fvdl return (error);
367 1.23 fvdl }
368 1.28 fvdl VOP_UNLOCK(lvp, 0);
369 1.23 fvdl fp->f_data = (caddr_t) lvp;
370 1.23 fvdl fp->f_offset = 0;
371 1.23 fvdl error = vn_close(vp, FREAD, fp->f_cred, p);
372 1.23 fvdl if (error)
373 1.23 fvdl return (error);
374 1.23 fvdl vp = lvp;
375 1.23 fvdl goto unionread;
376 1.23 fvdl }
377 1.23 fvdl }
378 1.23 fvdl }
379 1.23 fvdl #endif /* UNION */
380 1.23 fvdl
381 1.23 fvdl if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
382 1.23 fvdl (vp->v_mount->mnt_flag & MNT_UNION)) {
383 1.23 fvdl struct vnode *tvp = vp;
384 1.23 fvdl vp = vp->v_mount->mnt_vnodecovered;
385 1.23 fvdl VREF(vp);
386 1.23 fvdl fp->f_data = (caddr_t) vp;
387 1.23 fvdl fp->f_offset = 0;
388 1.23 fvdl vrele(tvp);
389 1.23 fvdl goto unionread;
390 1.23 fvdl }
391 1.23 fvdl *done = count - auio.uio_resid;
392 1.23 fvdl return error;
393 1.10 cgd }
394 1.10 cgd
395 1.10 cgd /*
396 1.10 cgd * File table vnode read routine.
397 1.10 cgd */
398 1.20 christos int
399 1.29 thorpej vn_read(fp, offset, uio, cred, flags)
400 1.10 cgd struct file *fp;
401 1.29 thorpej off_t *offset;
402 1.10 cgd struct uio *uio;
403 1.10 cgd struct ucred *cred;
404 1.29 thorpej int flags;
405 1.10 cgd {
406 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
407 1.31 kleink int count, error, ioflag = 0;
408 1.10 cgd
409 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
410 1.31 kleink if (fp->f_flag & FNONBLOCK)
411 1.31 kleink ioflag |= IO_NDELAY;
412 1.31 kleink if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
413 1.31 kleink ioflag |= IO_SYNC;
414 1.37 wrstuden if (fp->f_flag & FALTIO)
415 1.37 wrstuden ioflag |= IO_ALTSEMANTICS;
416 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
417 1.29 thorpej uio->uio_offset = *offset;
418 1.10 cgd count = uio->uio_resid;
419 1.31 kleink error = VOP_READ(vp, uio, ioflag, cred);
420 1.29 thorpej if (flags & FOF_UPDATE_OFFSET)
421 1.29 thorpej *offset += count - uio->uio_resid;
422 1.28 fvdl VOP_UNLOCK(vp, 0);
423 1.10 cgd return (error);
424 1.10 cgd }
425 1.10 cgd
426 1.10 cgd /*
427 1.10 cgd * File table vnode write routine.
428 1.10 cgd */
429 1.20 christos int
430 1.29 thorpej vn_write(fp, offset, uio, cred, flags)
431 1.10 cgd struct file *fp;
432 1.29 thorpej off_t *offset;
433 1.10 cgd struct uio *uio;
434 1.10 cgd struct ucred *cred;
435 1.29 thorpej int flags;
436 1.10 cgd {
437 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
438 1.17 mycroft int count, error, ioflag = IO_UNIT;
439 1.10 cgd
440 1.10 cgd if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
441 1.10 cgd ioflag |= IO_APPEND;
442 1.10 cgd if (fp->f_flag & FNONBLOCK)
443 1.10 cgd ioflag |= IO_NDELAY;
444 1.24 thorpej if (fp->f_flag & FFSYNC ||
445 1.24 thorpej (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
446 1.24 thorpej ioflag |= IO_SYNC;
447 1.31 kleink else if (fp->f_flag & FDSYNC)
448 1.31 kleink ioflag |= IO_DSYNC;
449 1.37 wrstuden if (fp->f_flag & FALTIO)
450 1.37 wrstuden ioflag |= IO_ALTSEMANTICS;
451 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
452 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
453 1.29 thorpej uio->uio_offset = *offset;
454 1.10 cgd count = uio->uio_resid;
455 1.10 cgd error = VOP_WRITE(vp, uio, ioflag, cred);
456 1.29 thorpej if (flags & FOF_UPDATE_OFFSET) {
457 1.29 thorpej if (ioflag & IO_APPEND)
458 1.29 thorpej *offset = uio->uio_offset;
459 1.29 thorpej else
460 1.29 thorpej *offset += count - uio->uio_resid;
461 1.29 thorpej }
462 1.28 fvdl VOP_UNLOCK(vp, 0);
463 1.10 cgd return (error);
464 1.10 cgd }
465 1.10 cgd
466 1.10 cgd /*
467 1.10 cgd * File table vnode stat routine.
468 1.10 cgd */
469 1.48 jdolecek static int
470 1.48 jdolecek vn_statfile(fp, sb, p)
471 1.48 jdolecek struct file *fp;
472 1.48 jdolecek struct stat *sb;
473 1.48 jdolecek struct proc *p;
474 1.48 jdolecek {
475 1.48 jdolecek struct vnode *vp = (struct vnode *)fp->f_data;
476 1.48 jdolecek
477 1.48 jdolecek return vn_stat(vp, sb, p);
478 1.48 jdolecek }
479 1.48 jdolecek
480 1.20 christos int
481 1.47 jdolecek vn_stat(fdata, sb, p)
482 1.47 jdolecek void *fdata;
483 1.41 augustss struct stat *sb;
484 1.10 cgd struct proc *p;
485 1.10 cgd {
486 1.47 jdolecek struct vnode *vp = fdata;
487 1.19 mycroft struct vattr va;
488 1.10 cgd int error;
489 1.35 wrstuden mode_t mode;
490 1.10 cgd
491 1.19 mycroft error = VOP_GETATTR(vp, &va, p->p_ucred, p);
492 1.10 cgd if (error)
493 1.10 cgd return (error);
494 1.10 cgd /*
495 1.10 cgd * Copy from vattr table
496 1.10 cgd */
497 1.19 mycroft sb->st_dev = va.va_fsid;
498 1.19 mycroft sb->st_ino = va.va_fileid;
499 1.19 mycroft mode = va.va_mode;
500 1.10 cgd switch (vp->v_type) {
501 1.10 cgd case VREG:
502 1.10 cgd mode |= S_IFREG;
503 1.10 cgd break;
504 1.10 cgd case VDIR:
505 1.10 cgd mode |= S_IFDIR;
506 1.10 cgd break;
507 1.10 cgd case VBLK:
508 1.10 cgd mode |= S_IFBLK;
509 1.10 cgd break;
510 1.10 cgd case VCHR:
511 1.10 cgd mode |= S_IFCHR;
512 1.10 cgd break;
513 1.10 cgd case VLNK:
514 1.10 cgd mode |= S_IFLNK;
515 1.10 cgd break;
516 1.10 cgd case VSOCK:
517 1.10 cgd mode |= S_IFSOCK;
518 1.10 cgd break;
519 1.10 cgd case VFIFO:
520 1.10 cgd mode |= S_IFIFO;
521 1.10 cgd break;
522 1.10 cgd default:
523 1.10 cgd return (EBADF);
524 1.10 cgd };
525 1.10 cgd sb->st_mode = mode;
526 1.19 mycroft sb->st_nlink = va.va_nlink;
527 1.19 mycroft sb->st_uid = va.va_uid;
528 1.19 mycroft sb->st_gid = va.va_gid;
529 1.19 mycroft sb->st_rdev = va.va_rdev;
530 1.19 mycroft sb->st_size = va.va_size;
531 1.19 mycroft sb->st_atimespec = va.va_atime;
532 1.19 mycroft sb->st_mtimespec = va.va_mtime;
533 1.19 mycroft sb->st_ctimespec = va.va_ctime;
534 1.19 mycroft sb->st_blksize = va.va_blocksize;
535 1.19 mycroft sb->st_flags = va.va_flags;
536 1.22 mycroft sb->st_gen = 0;
537 1.19 mycroft sb->st_blocks = va.va_bytes / S_BLKSIZE;
538 1.10 cgd return (0);
539 1.37 wrstuden }
540 1.37 wrstuden
541 1.37 wrstuden /*
542 1.37 wrstuden * File table vnode fcntl routine.
543 1.37 wrstuden */
544 1.37 wrstuden int
545 1.37 wrstuden vn_fcntl(fp, com, data, p)
546 1.37 wrstuden struct file *fp;
547 1.37 wrstuden u_int com;
548 1.37 wrstuden caddr_t data;
549 1.37 wrstuden struct proc *p;
550 1.37 wrstuden {
551 1.41 augustss struct vnode *vp = ((struct vnode *)fp->f_data);
552 1.37 wrstuden int error;
553 1.37 wrstuden
554 1.37 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
555 1.37 wrstuden error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
556 1.37 wrstuden VOP_UNLOCK(vp, 0);
557 1.37 wrstuden return (error);
558 1.10 cgd }
559 1.10 cgd
560 1.10 cgd /*
561 1.10 cgd * File table vnode ioctl routine.
562 1.10 cgd */
563 1.20 christos int
564 1.10 cgd vn_ioctl(fp, com, data, p)
565 1.10 cgd struct file *fp;
566 1.15 cgd u_long com;
567 1.10 cgd caddr_t data;
568 1.10 cgd struct proc *p;
569 1.10 cgd {
570 1.41 augustss struct vnode *vp = ((struct vnode *)fp->f_data);
571 1.10 cgd struct vattr vattr;
572 1.10 cgd int error;
573 1.10 cgd
574 1.10 cgd switch (vp->v_type) {
575 1.10 cgd
576 1.10 cgd case VREG:
577 1.10 cgd case VDIR:
578 1.10 cgd if (com == FIONREAD) {
579 1.20 christos error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
580 1.20 christos if (error)
581 1.10 cgd return (error);
582 1.10 cgd *(int *)data = vattr.va_size - fp->f_offset;
583 1.10 cgd return (0);
584 1.10 cgd }
585 1.10 cgd if (com == FIONBIO || com == FIOASYNC) /* XXX */
586 1.10 cgd return (0); /* XXX */
587 1.10 cgd /* fall into ... */
588 1.10 cgd
589 1.10 cgd default:
590 1.54 atatat return (EPASSTHROUGH);
591 1.10 cgd
592 1.10 cgd case VFIFO:
593 1.10 cgd case VCHR:
594 1.10 cgd case VBLK:
595 1.10 cgd error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
596 1.10 cgd if (error == 0 && com == TIOCSCTTY) {
597 1.13 cgd if (p->p_session->s_ttyvp)
598 1.13 cgd vrele(p->p_session->s_ttyvp);
599 1.10 cgd p->p_session->s_ttyvp = vp;
600 1.10 cgd VREF(vp);
601 1.10 cgd }
602 1.10 cgd return (error);
603 1.10 cgd }
604 1.10 cgd }
605 1.10 cgd
606 1.10 cgd /*
607 1.21 mycroft * File table vnode poll routine.
608 1.10 cgd */
609 1.20 christos int
610 1.21 mycroft vn_poll(fp, events, p)
611 1.10 cgd struct file *fp;
612 1.21 mycroft int events;
613 1.10 cgd struct proc *p;
614 1.10 cgd {
615 1.10 cgd
616 1.21 mycroft return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
617 1.28 fvdl }
618 1.28 fvdl
619 1.28 fvdl /*
620 1.28 fvdl * Check that the vnode is still valid, and if so
621 1.28 fvdl * acquire requested lock.
622 1.28 fvdl */
623 1.28 fvdl int
624 1.28 fvdl vn_lock(vp, flags)
625 1.28 fvdl struct vnode *vp;
626 1.28 fvdl int flags;
627 1.28 fvdl {
628 1.28 fvdl int error;
629 1.34 sommerfe
630 1.28 fvdl do {
631 1.36 mycroft if ((flags & LK_INTERLOCK) == 0)
632 1.28 fvdl simple_lock(&vp->v_interlock);
633 1.28 fvdl if (vp->v_flag & VXLOCK) {
634 1.49 chs if (flags & LK_NOWAIT) {
635 1.49 chs simple_unlock(&vp->v_interlock);
636 1.49 chs return EBUSY;
637 1.49 chs }
638 1.28 fvdl vp->v_flag |= VXWANT;
639 1.49 chs ltsleep(vp, PINOD | PNORELOCK,
640 1.44 sommerfe "vn_lock", 0, &vp->v_interlock);
641 1.28 fvdl error = ENOENT;
642 1.28 fvdl } else {
643 1.28 fvdl error = VOP_LOCK(vp, flags | LK_INTERLOCK);
644 1.49 chs if (error == 0 || error == EDEADLK || error == EBUSY)
645 1.28 fvdl return (error);
646 1.28 fvdl }
647 1.28 fvdl flags &= ~LK_INTERLOCK;
648 1.28 fvdl } while (flags & LK_RETRY);
649 1.28 fvdl return (error);
650 1.10 cgd }
651 1.10 cgd
652 1.10 cgd /*
653 1.10 cgd * File table vnode close routine.
654 1.10 cgd */
655 1.20 christos int
656 1.10 cgd vn_closefile(fp, p)
657 1.10 cgd struct file *fp;
658 1.10 cgd struct proc *p;
659 1.10 cgd {
660 1.10 cgd
661 1.10 cgd return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
662 1.10 cgd fp->f_cred, p));
663 1.39 fvdl }
664 1.39 fvdl
665 1.39 fvdl /*
666 1.39 fvdl * Enable LK_CANRECURSE on lock. Return prior status.
667 1.39 fvdl */
668 1.39 fvdl u_int
669 1.39 fvdl vn_setrecurse(vp)
670 1.39 fvdl struct vnode *vp;
671 1.39 fvdl {
672 1.39 fvdl struct lock *lkp = &vp->v_lock;
673 1.39 fvdl u_int retval = lkp->lk_flags & LK_CANRECURSE;
674 1.39 fvdl
675 1.39 fvdl lkp->lk_flags |= LK_CANRECURSE;
676 1.39 fvdl return retval;
677 1.39 fvdl }
678 1.39 fvdl
679 1.39 fvdl /*
680 1.39 fvdl * Called when done with locksetrecurse.
681 1.39 fvdl */
682 1.39 fvdl void
683 1.39 fvdl vn_restorerecurse(vp, flags)
684 1.39 fvdl struct vnode *vp;
685 1.39 fvdl u_int flags;
686 1.39 fvdl {
687 1.39 fvdl struct lock *lkp = &vp->v_lock;
688 1.39 fvdl
689 1.39 fvdl lkp->lk_flags &= ~LK_CANRECURSE;
690 1.39 fvdl lkp->lk_flags |= flags;
691 1.10 cgd }
692