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