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