The thread/proc pointer argument in the VFS subsystem originally existed
[dragonfly.git] / sys / kern / vfs_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/vfs_vnops.c,v 1.87.2.13 2002/12/29 18:19:53 dillon Exp $
87de5057 40 * $DragonFly: src/sys/kern/vfs_vnops.c,v 1.39 2006/05/06 02:43:12 dillon Exp $
984263bc
MD
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/fcntl.h>
46#include <sys/file.h>
47#include <sys/stat.h>
48#include <sys/proc.h>
49#include <sys/mount.h>
fad57d0e 50#include <sys/nlookup.h>
984263bc
MD
51#include <sys/vnode.h>
52#include <sys/buf.h>
53#include <sys/filio.h>
54#include <sys/ttycom.h>
55#include <sys/conf.h>
56#include <sys/syslog.h>
57
87de5057
MD
58static int vn_closefile (struct file *fp);
59static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
60 struct ucred *cred);
402ed7e1 61static int vn_read (struct file *fp, struct uio *uio,
87de5057 62 struct ucred *cred, int flags);
fad57d0e 63static int svn_read (struct file *fp, struct uio *uio,
87de5057
MD
64 struct ucred *cred, int flags);
65static int vn_poll (struct file *fp, int events, struct ucred *cred);
402ed7e1 66static int vn_kqfilter (struct file *fp, struct knote *kn);
87de5057 67static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
402ed7e1 68static int vn_write (struct file *fp, struct uio *uio,
87de5057 69 struct ucred *cred, int flags);
fad57d0e 70static int svn_write (struct file *fp, struct uio *uio,
87de5057 71 struct ucred *cred, int flags);
984263bc 72
fad57d0e 73struct fileops vnode_fileops = {
f53ede20 74 NULL, /* port */
455fcd7e 75 NULL, /* clone */
984263bc 76 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
004d2de5 77 vn_statfile, vn_closefile, nofo_shutdown
984263bc
MD
78};
79
fad57d0e
MD
80struct fileops specvnode_fileops = {
81 NULL, /* port */
82 NULL, /* clone */
83 svn_read, svn_write, vn_ioctl, vn_poll, vn_kqfilter,
004d2de5 84 vn_statfile, vn_closefile, nofo_shutdown
fad57d0e
MD
85};
86
87/*
88 * Shortcut the device read/write. This avoids a lot of vnode junk.
89 * Basically the specfs vnops for read and write take the locked vnode,
90 * unlock it (because we can't hold the vnode locked while reading or writing
91 * a device which may block indefinitely), issues the device operation, then
92 * relock the vnode before returning, plus other junk. This bypasses all
93 * of that and just does the device operation.
94 */
95void
96vn_setspecops(struct file *fp)
97{
98 if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
99 fp->f_ops = &specvnode_fileops;
100 }
101}
102
984263bc 103/*
fad57d0e
MD
104 * Common code for vnode open operations. Check permissions, and call
105 * the VOP_NOPEN or VOP_NCREATE routine.
106 *
107 * The caller is responsible for setting up nd with nlookup_init() and
108 * for cleaning it up with nlookup_done(), whether we return an error
109 * or not.
110 *
111 * On success nd->nl_open_vp will hold a referenced and, if requested,
112 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
113 * is non-NULL the vnode will be installed in the file pointer.
114 *
115 * NOTE: The vnode is referenced just once on return whether or not it
116 * is also installed in the file pointer.
984263bc
MD
117 */
118int
fad57d0e 119vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
984263bc 120{
1fd87d54 121 struct vnode *vp;
fad57d0e 122 struct ucred *cred = nd->nl_cred;
984263bc
MD
123 struct vattr vat;
124 struct vattr *vap = &vat;
fad57d0e 125 struct namecache *ncp;
984263bc
MD
126 int mode, error;
127
fad57d0e
MD
128 /*
129 * Lookup the path and create or obtain the vnode. After a
130 * successful lookup a locked nd->nl_ncp will be returned.
131 *
132 * The result of this section should be a locked vnode.
133 *
134 * XXX with only a little work we should be able to avoid locking
135 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
136 */
984263bc 137 if (fmode & O_CREAT) {
fad57d0e
MD
138 /*
139 * CONDITIONAL CREATE FILE CASE
140 *
141 * Setting NLC_CREATE causes a negative hit to store
142 * the negative hit ncp and not return an error. Then
143 * nc_error or nc_vp may be checked to see if the ncp
144 * represents a negative hit. NLC_CREATE also requires
145 * write permission on the governing directory or EPERM
146 * is returned.
147 */
984263bc 148 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
fad57d0e
MD
149 nd->nl_flags |= NLC_FOLLOW;
150 nd->nl_flags |= NLC_CREATE;
984263bc 151 bwillwrite();
fad57d0e 152 error = nlookup(nd);
806dcf9a
MD
153 } else {
154 /*
155 * NORMAL OPEN FILE CASE
156 */
157 error = nlookup(nd);
158 }
fad57d0e 159
806dcf9a
MD
160 if (error)
161 return (error);
162 ncp = nd->nl_ncp;
fad57d0e 163
806dcf9a
MD
164 /*
165 * split case to allow us to re-resolve and retry the ncp in case
166 * we get ESTALE.
167 */
168again:
169 if (fmode & O_CREAT) {
fad57d0e 170 if (ncp->nc_vp == NULL) {
984263bc
MD
171 VATTR_NULL(vap);
172 vap->va_type = VREG;
173 vap->va_mode = cmode;
174 if (fmode & O_EXCL)
175 vap->va_vaflags |= VA_EXCLUSIVE;
fad57d0e
MD
176 error = VOP_NCREATE(ncp, &vp, nd->nl_cred, vap);
177 if (error)
984263bc 178 return (error);
984263bc 179 fmode &= ~O_TRUNC;
fad57d0e
MD
180 ASSERT_VOP_LOCKED(vp, "create");
181 /* locked vnode is returned */
984263bc 182 } else {
984263bc
MD
183 if (fmode & O_EXCL) {
184 error = EEXIST;
fad57d0e
MD
185 } else {
186 error = cache_vget(ncp, cred,
187 LK_EXCLUSIVE, &vp);
984263bc 188 }
fad57d0e
MD
189 if (error)
190 return (error);
984263bc
MD
191 fmode &= ~O_CREAT;
192 }
193 } else {
fad57d0e 194 error = cache_vget(ncp, cred, LK_EXCLUSIVE, &vp);
984263bc
MD
195 if (error)
196 return (error);
984263bc 197 }
fad57d0e
MD
198
199 /*
806dcf9a
MD
200 * We have a locked vnode and ncp now. Note that the ncp will
201 * be cleaned up by the caller if nd->nl_ncp is left intact.
fad57d0e 202 */
984263bc
MD
203 if (vp->v_type == VLNK) {
204 error = EMLINK;
205 goto bad;
206 }
207 if (vp->v_type == VSOCK) {
208 error = EOPNOTSUPP;
209 goto bad;
210 }
211 if ((fmode & O_CREAT) == 0) {
212 mode = 0;
213 if (fmode & (FWRITE | O_TRUNC)) {
214 if (vp->v_type == VDIR) {
215 error = EISDIR;
216 goto bad;
217 }
218 error = vn_writechk(vp);
806dcf9a
MD
219 if (error) {
220 /*
221 * Special stale handling, re-resolve the
222 * vnode.
223 */
224 if (error == ESTALE) {
225 vput(vp);
226 vp = NULL;
227 cache_setunresolved(ncp);
228 error = cache_resolve(ncp, cred);
229 if (error == 0)
230 goto again;
231 }
984263bc 232 goto bad;
806dcf9a 233 }
984263bc
MD
234 mode |= VWRITE;
235 }
236 if (fmode & FREAD)
237 mode |= VREAD;
238 if (mode) {
87de5057 239 error = VOP_ACCESS(vp, mode, cred);
806dcf9a
MD
240 if (error) {
241 /*
242 * Special stale handling, re-resolve the
243 * vnode.
244 */
245 if (error == ESTALE) {
246 vput(vp);
247 vp = NULL;
248 cache_setunresolved(ncp);
249 error = cache_resolve(ncp, cred);
250 if (error == 0)
251 goto again;
252 }
984263bc 253 goto bad;
806dcf9a 254 }
984263bc
MD
255 }
256 }
257 if (fmode & O_TRUNC) {
ca466bae
MD
258 VOP_UNLOCK(vp, 0); /* XXX */
259 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
984263bc
MD
260 VATTR_NULL(vap);
261 vap->va_size = 0;
87de5057 262 error = VOP_SETATTR(vp, vap, cred);
984263bc
MD
263 if (error)
264 goto bad;
265 }
fad57d0e
MD
266
267 /*
268 * Setup the fp so VOP_OPEN can override it. No descriptor has been
8ddc6004
MD
269 * associated with the fp yet so we own it clean. f_ncp inherits
270 * nl_ncp .
fad57d0e
MD
271 */
272 if (fp) {
fad57d0e
MD
273 if (vp->v_type == VDIR) {
274 fp->f_ncp = nd->nl_ncp;
275 nd->nl_ncp = NULL;
276 cache_unlock(fp->f_ncp);
277 }
278 }
279
280 /*
281 * Get rid of nl_ncp. vn_open does not return it (it returns the
282 * vnode or the file pointer). Note: we can't leave nl_ncp locked
283 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
284 * on /dev/ttyd0
285 */
286 if (nd->nl_ncp) {
287 cache_put(nd->nl_ncp);
288 nd->nl_ncp = NULL;
289 }
290
87de5057 291 error = VOP_OPEN(vp, fmode, cred, fp);
fad57d0e
MD
292 if (error) {
293 /*
294 * setting f_ops to &badfileops will prevent the descriptor
295 * code from trying to close and release the vnode, since
296 * the open failed we do not want to call close.
297 */
675eb4c0
MD
298 if (fp) {
299 fp->f_data = NULL;
300 fp->f_ops = &badfileops;
301 }
984263bc 302 goto bad;
fad57d0e 303 }
fad57d0e 304
7540ab49 305#if 0
984263bc 306 /*
7540ab49 307 * Assert that VREG files have been setup for vmio.
984263bc 308 */
7540ab49
MD
309 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
310 ("vn_open: regular file was not VMIO enabled!"));
311#endif
984263bc 312
fad57d0e
MD
313 /*
314 * Return the vnode. XXX needs some cleaning up. The vnode is
8ddc6004 315 * only returned in the fp == NULL case.
fad57d0e
MD
316 */
317 if (fp == NULL) {
318 nd->nl_open_vp = vp;
319 nd->nl_vp_fmode = fmode;
320 if ((nd->nl_flags & NLC_LOCKVP) == 0)
ca466bae 321 VOP_UNLOCK(vp, 0);
fad57d0e 322 } else {
8ddc6004 323 vput(vp);
fad57d0e 324 }
984263bc
MD
325 return (0);
326bad:
bb5c9c00
MD
327 if (vp)
328 vput(vp);
984263bc
MD
329 return (error);
330}
331
332/*
333 * Check for write permissions on the specified vnode.
334 * Prototype text segments cannot be written.
335 */
336int
337vn_writechk(vp)
1fd87d54 338 struct vnode *vp;
984263bc
MD
339{
340
341 /*
342 * If there's shared text associated with
343 * the vnode, try to free it up once. If
344 * we fail, we can't allow writing.
345 */
346 if (vp->v_flag & VTEXT)
347 return (ETXTBSY);
348 return (0);
349}
350
351/*
352 * Vnode close call
353 */
354int
87de5057 355vn_close(struct vnode *vp, int flags)
984263bc
MD
356{
357 int error;
358
ca466bae 359 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
87de5057 360 error = VOP_CLOSE(vp, flags);
ca466bae 361 VOP_UNLOCK(vp, 0);
5fd012e0 362 }
984263bc
MD
363 vrele(vp);
364 return (error);
365}
366
367static __inline
368int
369sequential_heuristic(struct uio *uio, struct file *fp)
370{
371 /*
372 * Sequential heuristic - detect sequential operation
373 */
374 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
375 uio->uio_offset == fp->f_nextoff) {
376 int tmpseq = fp->f_seqcount;
377 /*
378 * XXX we assume that the filesystem block size is
379 * the default. Not true, but still gives us a pretty
380 * good indicator of how sequential the read operations
381 * are.
382 */
383 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
384 if (tmpseq > IO_SEQMAX)
385 tmpseq = IO_SEQMAX;
386 fp->f_seqcount = tmpseq;
387 return(fp->f_seqcount << IO_SEQSHIFT);
388 }
389
390 /*
391 * Not sequential, quick draw-down of seqcount
392 */
393 if (fp->f_seqcount > 1)
394 fp->f_seqcount = 1;
395 else
396 fp->f_seqcount = 0;
397 return(0);
398}
399
400/*
401 * Package up an I/O request on a vnode into a uio and do it.
402 */
403int
87de5057
MD
404vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
405 off_t offset, enum uio_seg segflg, int ioflg,
406 struct ucred *cred, int *aresid)
984263bc
MD
407{
408 struct uio auio;
409 struct iovec aiov;
410 int error;
411
412 if ((ioflg & IO_NODELOCKED) == 0)
ca466bae 413 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
414 auio.uio_iov = &aiov;
415 auio.uio_iovcnt = 1;
416 aiov.iov_base = base;
417 aiov.iov_len = len;
418 auio.uio_resid = len;
419 auio.uio_offset = offset;
420 auio.uio_segflg = segflg;
421 auio.uio_rw = rw;
87de5057 422 auio.uio_td = curthread;
984263bc
MD
423 if (rw == UIO_READ) {
424 error = VOP_READ(vp, &auio, ioflg, cred);
425 } else {
426 error = VOP_WRITE(vp, &auio, ioflg, cred);
427 }
428 if (aresid)
429 *aresid = auio.uio_resid;
430 else
431 if (auio.uio_resid && error == 0)
432 error = EIO;
433 if ((ioflg & IO_NODELOCKED) == 0)
ca466bae 434 VOP_UNLOCK(vp, 0);
984263bc
MD
435 return (error);
436}
437
438/*
439 * Package up an I/O request on a vnode into a uio and do it. The I/O
440 * request is split up into smaller chunks and we try to avoid saturating
441 * the buffer cache while potentially holding a vnode locked, so we
442 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
443 * to give other processes a chance to lock the vnode (either other processes
444 * core'ing the same binary, or unrelated processes scanning the directory).
445 */
446int
87de5057
MD
447vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
448 off_t offset, enum uio_seg segflg, int ioflg,
449 struct ucred *cred, int *aresid)
984263bc
MD
450{
451 int error = 0;
452
453 do {
9a0222ac 454 int chunk;
984263bc 455
9a0222ac
DR
456 /*
457 * Force `offset' to a multiple of MAXBSIZE except possibly
458 * for the first chunk, so that filesystems only need to
459 * write full blocks except possibly for the first and last
460 * chunks.
461 */
462 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
463
464 if (chunk > len)
465 chunk = len;
984263bc
MD
466 if (rw != UIO_READ && vp->v_type == VREG)
467 bwillwrite();
468 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
87de5057 469 ioflg, cred, aresid);
984263bc
MD
470 len -= chunk; /* aresid calc already includes length */
471 if (error)
472 break;
473 offset += chunk;
474 base += chunk;
475 uio_yield();
476 } while (len);
477 if (aresid)
478 *aresid += len;
479 return (error);
480}
481
482/*
483 * File table vnode read routine.
484 */
485static int
87de5057 486vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
984263bc
MD
487{
488 struct vnode *vp;
489 int error, ioflag;
490
87de5057
MD
491 KASSERT(uio->uio_td == curthread,
492 ("uio_td %p is not td %p", uio->uio_td, curthread));
984263bc
MD
493 vp = (struct vnode *)fp->f_data;
494 ioflag = 0;
495 if (fp->f_flag & FNONBLOCK)
496 ioflag |= IO_NDELAY;
497 if (fp->f_flag & O_DIRECT)
498 ioflag |= IO_DIRECT;
ca466bae 499 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY);
984263bc
MD
500 if ((flags & FOF_OFFSET) == 0)
501 uio->uio_offset = fp->f_offset;
502
503 ioflag |= sequential_heuristic(uio, fp);
504
505 error = VOP_READ(vp, uio, ioflag, cred);
506 if ((flags & FOF_OFFSET) == 0)
507 fp->f_offset = uio->uio_offset;
508 fp->f_nextoff = uio->uio_offset;
ca466bae 509 VOP_UNLOCK(vp, 0);
984263bc
MD
510 return (error);
511}
512
fad57d0e
MD
513/*
514 * Device-optimized file table vnode read routine.
515 *
516 * This bypasses the VOP table and talks directly to the device. Most
517 * filesystems just route to specfs and can make this optimization.
518 */
519static int
87de5057 520svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
fad57d0e
MD
521{
522 struct vnode *vp;
523 int ioflag;
524 int error;
525 dev_t dev;
526
87de5057
MD
527 KASSERT(uio->uio_td == curthread,
528 ("uio_td %p is not td %p", uio->uio_td, curthread));
fad57d0e
MD
529
530 vp = (struct vnode *)fp->f_data;
531 if (vp == NULL || vp->v_type == VBAD)
532 return (EBADF);
533
534 if ((dev = vp->v_rdev) == NULL)
535 return (EBADF);
536 reference_dev(dev);
537
538 if (uio->uio_resid == 0)
539 return (0);
540 if ((flags & FOF_OFFSET) == 0)
541 uio->uio_offset = fp->f_offset;
542
543 ioflag = 0;
544 if (fp->f_flag & FNONBLOCK)
545 ioflag |= IO_NDELAY;
546 if (fp->f_flag & O_DIRECT)
547 ioflag |= IO_DIRECT;
548 ioflag |= sequential_heuristic(uio, fp);
549
550 error = dev_dread(dev, uio, ioflag);
551
552 release_dev(dev);
553 if ((flags & FOF_OFFSET) == 0)
554 fp->f_offset = uio->uio_offset;
555 fp->f_nextoff = uio->uio_offset;
556 return (error);
557}
558
984263bc
MD
559/*
560 * File table vnode write routine.
561 */
562static int
87de5057 563vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
984263bc
MD
564{
565 struct vnode *vp;
566 int error, ioflag;
567
87de5057
MD
568 KASSERT(uio->uio_td == curthread,
569 ("uio_procp %p is not p %p", uio->uio_td, curthread));
984263bc
MD
570 vp = (struct vnode *)fp->f_data;
571 if (vp->v_type == VREG)
572 bwillwrite();
573 vp = (struct vnode *)fp->f_data; /* XXX needed? */
574 ioflag = IO_UNIT;
575 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
576 ioflag |= IO_APPEND;
577 if (fp->f_flag & FNONBLOCK)
578 ioflag |= IO_NDELAY;
579 if (fp->f_flag & O_DIRECT)
580 ioflag |= IO_DIRECT;
581 if ((fp->f_flag & O_FSYNC) ||
582 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
583 ioflag |= IO_SYNC;
ca466bae 584 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
585 if ((flags & FOF_OFFSET) == 0)
586 uio->uio_offset = fp->f_offset;
587 ioflag |= sequential_heuristic(uio, fp);
588 error = VOP_WRITE(vp, uio, ioflag, cred);
589 if ((flags & FOF_OFFSET) == 0)
590 fp->f_offset = uio->uio_offset;
591 fp->f_nextoff = uio->uio_offset;
ca466bae 592 VOP_UNLOCK(vp, 0);
984263bc
MD
593 return (error);
594}
595
fad57d0e
MD
596/*
597 * Device-optimized file table vnode write routine.
598 *
599 * This bypasses the VOP table and talks directly to the device. Most
600 * filesystems just route to specfs and can make this optimization.
601 */
602static int
87de5057 603svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
fad57d0e
MD
604{
605 struct vnode *vp;
606 int ioflag;
607 int error;
608 dev_t dev;
609
87de5057
MD
610 KASSERT(uio->uio_td == curthread,
611 ("uio_procp %p is not p %p", uio->uio_td, curthread));
fad57d0e
MD
612
613 vp = (struct vnode *)fp->f_data;
614 if (vp == NULL || vp->v_type == VBAD)
615 return (EBADF);
616 if (vp->v_type == VREG)
617 bwillwrite();
618 vp = (struct vnode *)fp->f_data; /* XXX needed? */
619
620 if ((dev = vp->v_rdev) == NULL)
621 return (EBADF);
622 reference_dev(dev);
623
624 if ((flags & FOF_OFFSET) == 0)
625 uio->uio_offset = fp->f_offset;
626
627 ioflag = IO_UNIT;
628 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
629 ioflag |= IO_APPEND;
630 if (fp->f_flag & FNONBLOCK)
631 ioflag |= IO_NDELAY;
632 if (fp->f_flag & O_DIRECT)
633 ioflag |= IO_DIRECT;
634 if ((fp->f_flag & O_FSYNC) ||
635 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
636 ioflag |= IO_SYNC;
637 ioflag |= sequential_heuristic(uio, fp);
638
639 error = dev_dwrite(dev, uio, ioflag);
640
641 release_dev(dev);
642 if ((flags & FOF_OFFSET) == 0)
643 fp->f_offset = uio->uio_offset;
644 fp->f_nextoff = uio->uio_offset;
645
646 return (error);
647}
648
984263bc
MD
649/*
650 * File table vnode stat routine.
651 */
652static int
87de5057 653vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
984263bc
MD
654{
655 struct vnode *vp = (struct vnode *)fp->f_data;
656
87de5057 657 return vn_stat(vp, sb, cred);
984263bc
MD
658}
659
660int
87de5057 661vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
984263bc
MD
662{
663 struct vattr vattr;
dadab5e9 664 struct vattr *vap;
984263bc
MD
665 int error;
666 u_short mode;
d8869c1b 667 dev_t dev;
984263bc
MD
668
669 vap = &vattr;
87de5057 670 error = VOP_GETATTR(vp, vap);
984263bc
MD
671 if (error)
672 return (error);
673
674 /*
675 * Zero the spare stat fields
676 */
677 sb->st_lspare = 0;
7d15906a 678 sb->st_qspare = 0;
984263bc
MD
679
680 /*
681 * Copy from vattr table
682 */
683 if (vap->va_fsid != VNOVAL)
684 sb->st_dev = vap->va_fsid;
685 else
686 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
687 sb->st_ino = vap->va_fileid;
688 mode = vap->va_mode;
689 switch (vap->va_type) {
690 case VREG:
691 mode |= S_IFREG;
692 break;
693 case VDIR:
694 mode |= S_IFDIR;
695 break;
696 case VBLK:
697 mode |= S_IFBLK;
698 break;
699 case VCHR:
700 mode |= S_IFCHR;
701 break;
702 case VLNK:
703 mode |= S_IFLNK;
704 /* This is a cosmetic change, symlinks do not have a mode. */
705 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
706 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
707 else
708 sb->st_mode |= ACCESSPERMS; /* 0777 */
709 break;
710 case VSOCK:
711 mode |= S_IFSOCK;
712 break;
713 case VFIFO:
714 mode |= S_IFIFO;
715 break;
716 default:
717 return (EBADF);
718 };
719 sb->st_mode = mode;
720 sb->st_nlink = vap->va_nlink;
721 sb->st_uid = vap->va_uid;
722 sb->st_gid = vap->va_gid;
723 sb->st_rdev = vap->va_rdev;
724 sb->st_size = vap->va_size;
725 sb->st_atimespec = vap->va_atime;
726 sb->st_mtimespec = vap->va_mtime;
727 sb->st_ctimespec = vap->va_ctime;
728
d8869c1b
MD
729 /*
730 * A VCHR and VBLK device may track the last access and last modified
731 * time independantly of the filesystem. This is particularly true
732 * because device read and write calls may bypass the filesystem.
733 */
734 if (vp->v_type == VCHR || vp->v_type == VBLK) {
735 if ((dev = vp->v_rdev) != NULL) {
736 if (dev->si_lastread) {
737 sb->st_atimespec.tv_sec = dev->si_lastread;
738 sb->st_atimespec.tv_nsec = 0;
739 }
740 if (dev->si_lastwrite) {
741 sb->st_atimespec.tv_sec = dev->si_lastwrite;
742 sb->st_atimespec.tv_nsec = 0;
743 }
744 }
745 }
746
984263bc
MD
747 /*
748 * According to www.opengroup.org, the meaning of st_blksize is
749 * "a filesystem-specific preferred I/O block size for this
750 * object. In some filesystem types, this may vary from file
751 * to file"
752 * Default to PAGE_SIZE after much discussion.
753 */
754
755 if (vap->va_type == VREG) {
756 sb->st_blksize = vap->va_blocksize;
757 } else if (vn_isdisk(vp, NULL)) {
e4c9c0c8
MD
758 /*
759 * XXX this is broken. If the device is not yet open (aka
760 * stat() call, aka v_rdev == NULL), how are we supposed
761 * to get a valid block size out of it?
762 */
763 dev_t dev;
764
765 if ((dev = vp->v_rdev) == NULL)
766 dev = udev2dev(vp->v_udev, vp->v_type == VBLK);
767 sb->st_blksize = dev->si_bsize_best;
768 if (sb->st_blksize < dev->si_bsize_phys)
769 sb->st_blksize = dev->si_bsize_phys;
984263bc
MD
770 if (sb->st_blksize < BLKDEV_IOSIZE)
771 sb->st_blksize = BLKDEV_IOSIZE;
772 } else {
773 sb->st_blksize = PAGE_SIZE;
774 }
775
776 sb->st_flags = vap->va_flags;
87de5057 777 if (suser_cred(cred, 0))
984263bc
MD
778 sb->st_gen = 0;
779 else
780 sb->st_gen = vap->va_gen;
781
782#if (S_BLKSIZE == 512)
783 /* Optimize this case */
784 sb->st_blocks = vap->va_bytes >> 9;
785#else
786 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
787#endif
dc1be39c 788 sb->st_fsmid = vap->va_fsmid;
984263bc
MD
789 return (0);
790}
791
792/*
793 * File table vnode ioctl routine.
794 */
795static int
87de5057 796vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
984263bc 797{
dadab5e9 798 struct vnode *vp = ((struct vnode *)fp->f_data);
1fbb5fc0 799 struct vnode *ovp;
984263bc
MD
800 struct vattr vattr;
801 int error;
802
dadab5e9 803 switch (vp->v_type) {
984263bc
MD
804 case VREG:
805 case VDIR:
806 if (com == FIONREAD) {
87de5057 807 error = VOP_GETATTR(vp, &vattr);
984263bc
MD
808 if (error)
809 return (error);
810 *(int *)data = vattr.va_size - fp->f_offset;
811 return (0);
812 }
813 if (com == FIONBIO || com == FIOASYNC) /* XXX */
814 return (0); /* XXX */
815 /* fall into ... */
984263bc
MD
816 default:
817#if 0
818 return (ENOTTY);
819#endif
820 case VFIFO:
821 case VCHR:
822 case VBLK:
823 if (com == FIODTYPE) {
824 if (vp->v_type != VCHR && vp->v_type != VBLK)
825 return (ENOTTY);
335dda38 826 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
984263bc
MD
827 return (0);
828 }
87de5057 829 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
984263bc 830 if (error == 0 && com == TIOCSCTTY) {
87de5057
MD
831 struct proc *p = curthread->td_proc;
832 struct session *sess;
833
834 if (p == NULL)
835 return (ENOTTY);
984263bc 836
87de5057 837 sess = p->p_session;
984263bc 838 /* Do nothing if reassigning same control tty */
dadab5e9 839 if (sess->s_ttyvp == vp)
984263bc
MD
840 return (0);
841
842 /* Get rid of reference to old control tty */
1fbb5fc0 843 ovp = sess->s_ttyvp;
597aea93 844 vref(vp);
1fbb5fc0
MD
845 sess->s_ttyvp = vp;
846 if (ovp)
847 vrele(ovp);
984263bc
MD
848 }
849 return (error);
850 }
851}
852
853/*
854 * File table vnode poll routine.
855 */
856static int
87de5057 857vn_poll(struct file *fp, int events, struct ucred *cred)
984263bc 858{
87de5057 859 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred));
984263bc
MD
860}
861
862/*
863 * Check that the vnode is still valid, and if so
864 * acquire requested lock.
865 */
866int
867#ifndef DEBUG_LOCKS
ca466bae 868vn_lock(struct vnode *vp, int flags)
984263bc 869#else
ca466bae 870debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
984263bc
MD
871#endif
872{
873 int error;
874
875 do {
984263bc 876#ifdef DEBUG_LOCKS
5fd012e0
MD
877 vp->filename = filename;
878 vp->line = line;
984263bc 879#endif
ca466bae 880 error = VOP_LOCK(vp, flags | LK_NOPAUSE);
5fd012e0
MD
881 if (error == 0)
882 break;
984263bc 883 } while (flags & LK_RETRY);
5fd012e0
MD
884
885 /*
886 * Because we (had better!) have a ref on the vnode, once it
887 * goes to VRECLAIMED state it will not be recycled until all
888 * refs go away. So we can just check the flag.
889 */
890 if (error == 0 && (vp->v_flag & VRECLAIMED)) {
ca466bae 891 VOP_UNLOCK(vp, 0);
5fd012e0
MD
892 error = ENOENT;
893 }
984263bc
MD
894 return (error);
895}
896
897/*
898 * File table vnode close routine.
899 */
900static int
87de5057 901vn_closefile(struct file *fp)
984263bc 902{
dadab5e9 903 int err;
984263bc
MD
904
905 fp->f_ops = &badfileops;
87de5057 906 err = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
dadab5e9 907 return(err);
984263bc
MD
908}
909
910static int
911vn_kqfilter(struct file *fp, struct knote *kn)
912{
913
914 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
915}