vfs_vnops.c revision 1.52 1 1.52 lukem /* $NetBSD: vfs_vnops.c,v 1.52 2001/11/12 15:25:42 lukem 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.52 lukem __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.52 2001/11/12 15:25:42 lukem 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.49 chs uvmexp.vnodepages -= vp->v_uobj.uo_npages;
207 1.49 chs uvmexp.vtextpages += vp->v_uobj.uo_npages;
208 1.46 chs }
209 1.51 thorpej vp->v_flag |= VEXECMAP;
210 1.10 cgd }
211 1.10 cgd
212 1.10 cgd /*
213 1.10 cgd * Vnode close call
214 1.32 wrstuden *
215 1.32 wrstuden * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
216 1.10 cgd */
217 1.20 christos int
218 1.10 cgd vn_close(vp, flags, cred, p)
219 1.41 augustss struct vnode *vp;
220 1.10 cgd int flags;
221 1.10 cgd struct ucred *cred;
222 1.10 cgd struct proc *p;
223 1.10 cgd {
224 1.10 cgd int error;
225 1.10 cgd
226 1.10 cgd if (flags & FWRITE)
227 1.10 cgd vp->v_writecount--;
228 1.32 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
229 1.10 cgd error = VOP_CLOSE(vp, flags, cred, p);
230 1.32 wrstuden vput(vp);
231 1.10 cgd return (error);
232 1.10 cgd }
233 1.10 cgd
234 1.10 cgd /*
235 1.10 cgd * Package up an I/O request on a vnode into a uio and do it.
236 1.10 cgd */
237 1.20 christos int
238 1.10 cgd vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
239 1.10 cgd enum uio_rw rw;
240 1.10 cgd struct vnode *vp;
241 1.10 cgd caddr_t base;
242 1.10 cgd int len;
243 1.10 cgd off_t offset;
244 1.10 cgd enum uio_seg segflg;
245 1.10 cgd int ioflg;
246 1.10 cgd struct ucred *cred;
247 1.30 thorpej size_t *aresid;
248 1.10 cgd struct proc *p;
249 1.10 cgd {
250 1.10 cgd struct uio auio;
251 1.10 cgd struct iovec aiov;
252 1.10 cgd int error;
253 1.10 cgd
254 1.50 chs if ((ioflg & IO_NODELOCKED) == 0) {
255 1.50 chs if (rw == UIO_READ) {
256 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
257 1.50 chs } else {
258 1.50 chs vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
259 1.50 chs }
260 1.50 chs }
261 1.10 cgd auio.uio_iov = &aiov;
262 1.10 cgd auio.uio_iovcnt = 1;
263 1.10 cgd aiov.iov_base = base;
264 1.10 cgd aiov.iov_len = len;
265 1.10 cgd auio.uio_resid = len;
266 1.10 cgd auio.uio_offset = offset;
267 1.10 cgd auio.uio_segflg = segflg;
268 1.10 cgd auio.uio_rw = rw;
269 1.10 cgd auio.uio_procp = p;
270 1.11 mycroft if (rw == UIO_READ) {
271 1.10 cgd error = VOP_READ(vp, &auio, ioflg, cred);
272 1.11 mycroft } else {
273 1.10 cgd error = VOP_WRITE(vp, &auio, ioflg, cred);
274 1.11 mycroft }
275 1.10 cgd if (aresid)
276 1.10 cgd *aresid = auio.uio_resid;
277 1.10 cgd else
278 1.10 cgd if (auio.uio_resid && error == 0)
279 1.10 cgd error = EIO;
280 1.10 cgd if ((ioflg & IO_NODELOCKED) == 0)
281 1.28 fvdl VOP_UNLOCK(vp, 0);
282 1.10 cgd return (error);
283 1.23 fvdl }
284 1.23 fvdl
285 1.23 fvdl int
286 1.23 fvdl vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
287 1.23 fvdl struct file *fp;
288 1.23 fvdl char *buf;
289 1.28 fvdl int segflg, *done, *ncookies;
290 1.23 fvdl u_int count;
291 1.23 fvdl struct proc *p;
292 1.28 fvdl off_t **cookies;
293 1.23 fvdl {
294 1.23 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
295 1.23 fvdl struct iovec aiov;
296 1.23 fvdl struct uio auio;
297 1.23 fvdl int error, eofflag;
298 1.23 fvdl
299 1.23 fvdl unionread:
300 1.23 fvdl if (vp->v_type != VDIR)
301 1.23 fvdl return (EINVAL);
302 1.23 fvdl aiov.iov_base = buf;
303 1.23 fvdl aiov.iov_len = count;
304 1.23 fvdl auio.uio_iov = &aiov;
305 1.23 fvdl auio.uio_iovcnt = 1;
306 1.23 fvdl auio.uio_rw = UIO_READ;
307 1.23 fvdl auio.uio_segflg = segflg;
308 1.23 fvdl auio.uio_procp = p;
309 1.23 fvdl auio.uio_resid = count;
310 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
311 1.23 fvdl auio.uio_offset = fp->f_offset;
312 1.28 fvdl error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
313 1.23 fvdl ncookies);
314 1.23 fvdl fp->f_offset = auio.uio_offset;
315 1.28 fvdl VOP_UNLOCK(vp, 0);
316 1.23 fvdl if (error)
317 1.23 fvdl return (error);
318 1.23 fvdl
319 1.23 fvdl #ifdef UNION
320 1.23 fvdl {
321 1.23 fvdl extern struct vnode *union_dircache __P((struct vnode *));
322 1.23 fvdl
323 1.23 fvdl if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
324 1.23 fvdl struct vnode *lvp;
325 1.23 fvdl
326 1.23 fvdl lvp = union_dircache(vp);
327 1.23 fvdl if (lvp != NULLVP) {
328 1.23 fvdl struct vattr va;
329 1.23 fvdl
330 1.23 fvdl /*
331 1.23 fvdl * If the directory is opaque,
332 1.23 fvdl * then don't show lower entries
333 1.23 fvdl */
334 1.23 fvdl error = VOP_GETATTR(vp, &va, fp->f_cred, p);
335 1.23 fvdl if (va.va_flags & OPAQUE) {
336 1.23 fvdl vput(lvp);
337 1.23 fvdl lvp = NULL;
338 1.23 fvdl }
339 1.23 fvdl }
340 1.23 fvdl
341 1.23 fvdl if (lvp != NULLVP) {
342 1.23 fvdl error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
343 1.23 fvdl if (error) {
344 1.28 fvdl vput(lvp);
345 1.23 fvdl return (error);
346 1.23 fvdl }
347 1.28 fvdl VOP_UNLOCK(lvp, 0);
348 1.23 fvdl fp->f_data = (caddr_t) lvp;
349 1.23 fvdl fp->f_offset = 0;
350 1.23 fvdl error = vn_close(vp, FREAD, fp->f_cred, p);
351 1.23 fvdl if (error)
352 1.23 fvdl return (error);
353 1.23 fvdl vp = lvp;
354 1.23 fvdl goto unionread;
355 1.23 fvdl }
356 1.23 fvdl }
357 1.23 fvdl }
358 1.23 fvdl #endif /* UNION */
359 1.23 fvdl
360 1.23 fvdl if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
361 1.23 fvdl (vp->v_mount->mnt_flag & MNT_UNION)) {
362 1.23 fvdl struct vnode *tvp = vp;
363 1.23 fvdl vp = vp->v_mount->mnt_vnodecovered;
364 1.23 fvdl VREF(vp);
365 1.23 fvdl fp->f_data = (caddr_t) vp;
366 1.23 fvdl fp->f_offset = 0;
367 1.23 fvdl vrele(tvp);
368 1.23 fvdl goto unionread;
369 1.23 fvdl }
370 1.23 fvdl *done = count - auio.uio_resid;
371 1.23 fvdl return error;
372 1.10 cgd }
373 1.10 cgd
374 1.10 cgd /*
375 1.10 cgd * File table vnode read routine.
376 1.10 cgd */
377 1.20 christos int
378 1.29 thorpej vn_read(fp, offset, uio, cred, flags)
379 1.10 cgd struct file *fp;
380 1.29 thorpej off_t *offset;
381 1.10 cgd struct uio *uio;
382 1.10 cgd struct ucred *cred;
383 1.29 thorpej int flags;
384 1.10 cgd {
385 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
386 1.31 kleink int count, error, ioflag = 0;
387 1.10 cgd
388 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
389 1.31 kleink if (fp->f_flag & FNONBLOCK)
390 1.31 kleink ioflag |= IO_NDELAY;
391 1.31 kleink if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
392 1.31 kleink ioflag |= IO_SYNC;
393 1.37 wrstuden if (fp->f_flag & FALTIO)
394 1.37 wrstuden ioflag |= IO_ALTSEMANTICS;
395 1.50 chs vn_lock(vp, LK_SHARED | LK_RETRY);
396 1.29 thorpej uio->uio_offset = *offset;
397 1.10 cgd count = uio->uio_resid;
398 1.31 kleink error = VOP_READ(vp, uio, ioflag, cred);
399 1.29 thorpej if (flags & FOF_UPDATE_OFFSET)
400 1.29 thorpej *offset += count - uio->uio_resid;
401 1.28 fvdl VOP_UNLOCK(vp, 0);
402 1.10 cgd return (error);
403 1.10 cgd }
404 1.10 cgd
405 1.10 cgd /*
406 1.10 cgd * File table vnode write routine.
407 1.10 cgd */
408 1.20 christos int
409 1.29 thorpej vn_write(fp, offset, uio, cred, flags)
410 1.10 cgd struct file *fp;
411 1.29 thorpej off_t *offset;
412 1.10 cgd struct uio *uio;
413 1.10 cgd struct ucred *cred;
414 1.29 thorpej int flags;
415 1.10 cgd {
416 1.28 fvdl struct vnode *vp = (struct vnode *)fp->f_data;
417 1.17 mycroft int count, error, ioflag = IO_UNIT;
418 1.10 cgd
419 1.10 cgd if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
420 1.10 cgd ioflag |= IO_APPEND;
421 1.10 cgd if (fp->f_flag & FNONBLOCK)
422 1.10 cgd ioflag |= IO_NDELAY;
423 1.24 thorpej if (fp->f_flag & FFSYNC ||
424 1.24 thorpej (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
425 1.24 thorpej ioflag |= IO_SYNC;
426 1.31 kleink else if (fp->f_flag & FDSYNC)
427 1.31 kleink ioflag |= IO_DSYNC;
428 1.37 wrstuden if (fp->f_flag & FALTIO)
429 1.37 wrstuden ioflag |= IO_ALTSEMANTICS;
430 1.17 mycroft VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
431 1.28 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
432 1.29 thorpej uio->uio_offset = *offset;
433 1.10 cgd count = uio->uio_resid;
434 1.10 cgd error = VOP_WRITE(vp, uio, ioflag, cred);
435 1.29 thorpej if (flags & FOF_UPDATE_OFFSET) {
436 1.29 thorpej if (ioflag & IO_APPEND)
437 1.29 thorpej *offset = uio->uio_offset;
438 1.29 thorpej else
439 1.29 thorpej *offset += count - uio->uio_resid;
440 1.29 thorpej }
441 1.28 fvdl VOP_UNLOCK(vp, 0);
442 1.10 cgd return (error);
443 1.10 cgd }
444 1.10 cgd
445 1.10 cgd /*
446 1.10 cgd * File table vnode stat routine.
447 1.10 cgd */
448 1.48 jdolecek static int
449 1.48 jdolecek vn_statfile(fp, sb, p)
450 1.48 jdolecek struct file *fp;
451 1.48 jdolecek struct stat *sb;
452 1.48 jdolecek struct proc *p;
453 1.48 jdolecek {
454 1.48 jdolecek struct vnode *vp = (struct vnode *)fp->f_data;
455 1.48 jdolecek
456 1.48 jdolecek return vn_stat(vp, sb, p);
457 1.48 jdolecek }
458 1.48 jdolecek
459 1.20 christos int
460 1.47 jdolecek vn_stat(fdata, sb, p)
461 1.47 jdolecek void *fdata;
462 1.41 augustss struct stat *sb;
463 1.10 cgd struct proc *p;
464 1.10 cgd {
465 1.47 jdolecek struct vnode *vp = fdata;
466 1.19 mycroft struct vattr va;
467 1.10 cgd int error;
468 1.35 wrstuden mode_t mode;
469 1.10 cgd
470 1.19 mycroft error = VOP_GETATTR(vp, &va, p->p_ucred, p);
471 1.10 cgd if (error)
472 1.10 cgd return (error);
473 1.10 cgd /*
474 1.10 cgd * Copy from vattr table
475 1.10 cgd */
476 1.19 mycroft sb->st_dev = va.va_fsid;
477 1.19 mycroft sb->st_ino = va.va_fileid;
478 1.19 mycroft mode = va.va_mode;
479 1.10 cgd switch (vp->v_type) {
480 1.10 cgd case VREG:
481 1.10 cgd mode |= S_IFREG;
482 1.10 cgd break;
483 1.10 cgd case VDIR:
484 1.10 cgd mode |= S_IFDIR;
485 1.10 cgd break;
486 1.10 cgd case VBLK:
487 1.10 cgd mode |= S_IFBLK;
488 1.10 cgd break;
489 1.10 cgd case VCHR:
490 1.10 cgd mode |= S_IFCHR;
491 1.10 cgd break;
492 1.10 cgd case VLNK:
493 1.10 cgd mode |= S_IFLNK;
494 1.10 cgd break;
495 1.10 cgd case VSOCK:
496 1.10 cgd mode |= S_IFSOCK;
497 1.10 cgd break;
498 1.10 cgd case VFIFO:
499 1.10 cgd mode |= S_IFIFO;
500 1.10 cgd break;
501 1.10 cgd default:
502 1.10 cgd return (EBADF);
503 1.10 cgd };
504 1.10 cgd sb->st_mode = mode;
505 1.19 mycroft sb->st_nlink = va.va_nlink;
506 1.19 mycroft sb->st_uid = va.va_uid;
507 1.19 mycroft sb->st_gid = va.va_gid;
508 1.19 mycroft sb->st_rdev = va.va_rdev;
509 1.19 mycroft sb->st_size = va.va_size;
510 1.19 mycroft sb->st_atimespec = va.va_atime;
511 1.19 mycroft sb->st_mtimespec = va.va_mtime;
512 1.19 mycroft sb->st_ctimespec = va.va_ctime;
513 1.19 mycroft sb->st_blksize = va.va_blocksize;
514 1.19 mycroft sb->st_flags = va.va_flags;
515 1.22 mycroft sb->st_gen = 0;
516 1.19 mycroft sb->st_blocks = va.va_bytes / S_BLKSIZE;
517 1.10 cgd return (0);
518 1.37 wrstuden }
519 1.37 wrstuden
520 1.37 wrstuden /*
521 1.37 wrstuden * File table vnode fcntl routine.
522 1.37 wrstuden */
523 1.37 wrstuden int
524 1.37 wrstuden vn_fcntl(fp, com, data, p)
525 1.37 wrstuden struct file *fp;
526 1.37 wrstuden u_int com;
527 1.37 wrstuden caddr_t data;
528 1.37 wrstuden struct proc *p;
529 1.37 wrstuden {
530 1.41 augustss struct vnode *vp = ((struct vnode *)fp->f_data);
531 1.37 wrstuden int error;
532 1.37 wrstuden
533 1.37 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
534 1.37 wrstuden error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
535 1.37 wrstuden VOP_UNLOCK(vp, 0);
536 1.37 wrstuden return (error);
537 1.10 cgd }
538 1.10 cgd
539 1.10 cgd /*
540 1.10 cgd * File table vnode ioctl routine.
541 1.10 cgd */
542 1.20 christos int
543 1.10 cgd vn_ioctl(fp, com, data, p)
544 1.10 cgd struct file *fp;
545 1.15 cgd u_long com;
546 1.10 cgd caddr_t data;
547 1.10 cgd struct proc *p;
548 1.10 cgd {
549 1.41 augustss struct vnode *vp = ((struct vnode *)fp->f_data);
550 1.10 cgd struct vattr vattr;
551 1.10 cgd int error;
552 1.10 cgd
553 1.10 cgd switch (vp->v_type) {
554 1.10 cgd
555 1.10 cgd case VREG:
556 1.10 cgd case VDIR:
557 1.10 cgd if (com == FIONREAD) {
558 1.20 christos error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
559 1.20 christos if (error)
560 1.10 cgd return (error);
561 1.10 cgd *(int *)data = vattr.va_size - fp->f_offset;
562 1.10 cgd return (0);
563 1.10 cgd }
564 1.10 cgd if (com == FIONBIO || com == FIOASYNC) /* XXX */
565 1.10 cgd return (0); /* XXX */
566 1.10 cgd /* fall into ... */
567 1.10 cgd
568 1.10 cgd default:
569 1.10 cgd return (ENOTTY);
570 1.10 cgd
571 1.10 cgd case VFIFO:
572 1.10 cgd case VCHR:
573 1.10 cgd case VBLK:
574 1.10 cgd error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
575 1.10 cgd if (error == 0 && com == TIOCSCTTY) {
576 1.13 cgd if (p->p_session->s_ttyvp)
577 1.13 cgd vrele(p->p_session->s_ttyvp);
578 1.10 cgd p->p_session->s_ttyvp = vp;
579 1.10 cgd VREF(vp);
580 1.10 cgd }
581 1.10 cgd return (error);
582 1.10 cgd }
583 1.10 cgd }
584 1.10 cgd
585 1.10 cgd /*
586 1.21 mycroft * File table vnode poll routine.
587 1.10 cgd */
588 1.20 christos int
589 1.21 mycroft vn_poll(fp, events, p)
590 1.10 cgd struct file *fp;
591 1.21 mycroft int events;
592 1.10 cgd struct proc *p;
593 1.10 cgd {
594 1.10 cgd
595 1.21 mycroft return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
596 1.28 fvdl }
597 1.28 fvdl
598 1.28 fvdl /*
599 1.28 fvdl * Check that the vnode is still valid, and if so
600 1.28 fvdl * acquire requested lock.
601 1.28 fvdl */
602 1.28 fvdl int
603 1.28 fvdl vn_lock(vp, flags)
604 1.28 fvdl struct vnode *vp;
605 1.28 fvdl int flags;
606 1.28 fvdl {
607 1.28 fvdl int error;
608 1.34 sommerfe
609 1.28 fvdl do {
610 1.36 mycroft if ((flags & LK_INTERLOCK) == 0)
611 1.28 fvdl simple_lock(&vp->v_interlock);
612 1.28 fvdl if (vp->v_flag & VXLOCK) {
613 1.49 chs if (flags & LK_NOWAIT) {
614 1.49 chs simple_unlock(&vp->v_interlock);
615 1.49 chs return EBUSY;
616 1.49 chs }
617 1.28 fvdl vp->v_flag |= VXWANT;
618 1.49 chs ltsleep(vp, PINOD | PNORELOCK,
619 1.44 sommerfe "vn_lock", 0, &vp->v_interlock);
620 1.28 fvdl error = ENOENT;
621 1.28 fvdl } else {
622 1.28 fvdl error = VOP_LOCK(vp, flags | LK_INTERLOCK);
623 1.49 chs if (error == 0 || error == EDEADLK || error == EBUSY)
624 1.28 fvdl return (error);
625 1.28 fvdl }
626 1.28 fvdl flags &= ~LK_INTERLOCK;
627 1.28 fvdl } while (flags & LK_RETRY);
628 1.28 fvdl return (error);
629 1.10 cgd }
630 1.10 cgd
631 1.10 cgd /*
632 1.10 cgd * File table vnode close routine.
633 1.10 cgd */
634 1.20 christos int
635 1.10 cgd vn_closefile(fp, p)
636 1.10 cgd struct file *fp;
637 1.10 cgd struct proc *p;
638 1.10 cgd {
639 1.10 cgd
640 1.10 cgd return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
641 1.10 cgd fp->f_cred, p));
642 1.39 fvdl }
643 1.39 fvdl
644 1.39 fvdl /*
645 1.39 fvdl * Enable LK_CANRECURSE on lock. Return prior status.
646 1.39 fvdl */
647 1.39 fvdl u_int
648 1.39 fvdl vn_setrecurse(vp)
649 1.39 fvdl struct vnode *vp;
650 1.39 fvdl {
651 1.39 fvdl struct lock *lkp = &vp->v_lock;
652 1.39 fvdl u_int retval = lkp->lk_flags & LK_CANRECURSE;
653 1.39 fvdl
654 1.39 fvdl lkp->lk_flags |= LK_CANRECURSE;
655 1.39 fvdl return retval;
656 1.39 fvdl }
657 1.39 fvdl
658 1.39 fvdl /*
659 1.39 fvdl * Called when done with locksetrecurse.
660 1.39 fvdl */
661 1.39 fvdl void
662 1.39 fvdl vn_restorerecurse(vp, flags)
663 1.39 fvdl struct vnode *vp;
664 1.39 fvdl u_int flags;
665 1.39 fvdl {
666 1.39 fvdl struct lock *lkp = &vp->v_lock;
667 1.39 fvdl
668 1.39 fvdl lkp->lk_flags &= ~LK_CANRECURSE;
669 1.39 fvdl lkp->lk_flags |= flags;
670 1.10 cgd }
671