Fix a race between the namecache and the vnode recycler. A vnode cannot be
[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.56 2008/05/08 01:41:06 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);
59 static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
60                 struct ucred *cred);
61 static int vn_read (struct file *fp, struct uio *uio, 
62                 struct ucred *cred, int flags);
63 static int svn_read (struct file *fp, struct uio *uio, 
64                 struct ucred *cred, int flags);
65 static int vn_poll (struct file *fp, int events, struct ucred *cred);
66 static int vn_kqfilter (struct file *fp, struct knote *kn);
67 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
68 static int vn_write (struct file *fp, struct uio *uio, 
69                 struct ucred *cred, int flags);
70 static int svn_write (struct file *fp, struct uio *uio, 
71                 struct ucred *cred, int flags);
72
73 struct fileops vnode_fileops = {
74         .fo_read = vn_read,
75         .fo_write = vn_write,
76         .fo_ioctl = vn_ioctl,
77         .fo_poll = vn_poll,
78         .fo_kqfilter = vn_kqfilter,
79         .fo_stat = vn_statfile,
80         .fo_close = vn_closefile,
81         .fo_shutdown = nofo_shutdown
82 };
83
84 struct fileops specvnode_fileops = {
85         .fo_read = svn_read,
86         .fo_write = svn_write,
87         .fo_ioctl = vn_ioctl,
88         .fo_poll = vn_poll,
89         .fo_kqfilter = vn_kqfilter,
90         .fo_stat = vn_statfile,
91         .fo_close = vn_closefile,
92         .fo_shutdown = nofo_shutdown
93 };
94
95 /*
96  * Shortcut the device read/write.  This avoids a lot of vnode junk.
97  * Basically the specfs vnops for read and write take the locked vnode,
98  * unlock it (because we can't hold the vnode locked while reading or writing
99  * a device which may block indefinitely), issues the device operation, then
100  * relock the vnode before returning, plus other junk.  This bypasses all
101  * of that and just does the device operation.
102  */
103 void
104 vn_setspecops(struct file *fp)
105 {
106         if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
107                 fp->f_ops = &specvnode_fileops;
108         }
109 }
110
111 /*
112  * Common code for vnode open operations.  Check permissions, and call
113  * the VOP_NOPEN or VOP_NCREATE routine.
114  *
115  * The caller is responsible for setting up nd with nlookup_init() and
116  * for cleaning it up with nlookup_done(), whether we return an error
117  * or not.
118  *
119  * On success nd->nl_open_vp will hold a referenced and, if requested,
120  * locked vnode.  A locked vnode is requested via NLC_LOCKVP.  If fp
121  * is non-NULL the vnode will be installed in the file pointer.
122  *
123  * NOTE: The vnode is referenced just once on return whether or not it
124  * is also installed in the file pointer.
125  */
126 int
127 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
128 {
129         struct vnode *vp;
130         struct vnode *dvp;
131         struct ucred *cred = nd->nl_cred;
132         struct vattr vat;
133         struct vattr *vap = &vat;
134         int mode, error;
135
136         /*
137          * Lookup the path and create or obtain the vnode.  After a
138          * successful lookup a locked nd->nl_nch will be returned.
139          *
140          * The result of this section should be a locked vnode.
141          *
142          * XXX with only a little work we should be able to avoid locking
143          * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
144          */
145         if (fmode & O_CREAT) {
146                 /*
147                  * CONDITIONAL CREATE FILE CASE
148                  *
149                  * Setting NLC_CREATE causes a negative hit to store
150                  * the negative hit ncp and not return an error.  Then
151                  * nc_error or nc_vp may be checked to see if the ncp 
152                  * represents a negative hit.  NLC_CREATE also requires
153                  * write permission on the governing directory or EPERM
154                  * is returned.
155                  */
156                 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
157                         nd->nl_flags |= NLC_FOLLOW;
158                 nd->nl_flags |= NLC_CREATE;
159                 bwillwrite();
160                 error = nlookup(nd);
161         } else {
162                 /*
163                  * NORMAL OPEN FILE CASE
164                  */
165                 error = nlookup(nd);
166         }
167
168         if (error)
169                 return (error);
170
171         /*
172          * split case to allow us to re-resolve and retry the ncp in case
173          * we get ESTALE.
174          */
175 again:
176         if (fmode & O_CREAT) {
177                 if (nd->nl_nch.ncp->nc_vp == NULL) {
178                         if ((error = ncp_writechk(&nd->nl_nch)) != 0)
179                                 return (error);
180                         if ((dvp = cache_dvpref(nd->nl_nch.ncp)) == NULL)
181                                 return (EPERM);
182                         VATTR_NULL(vap);
183                         vap->va_type = VREG;
184                         vap->va_mode = cmode;
185                         if (fmode & O_EXCL)
186                                 vap->va_vaflags |= VA_EXCLUSIVE;
187                         error = VOP_NCREATE(&nd->nl_nch, dvp, &vp,
188                                             nd->nl_cred, vap);
189                         cache_dvprel(dvp);
190                         if (error)
191                                 return (error);
192                         fmode &= ~O_TRUNC;
193                         /* locked vnode is returned */
194                 } else {
195                         if (fmode & O_EXCL) {
196                                 error = EEXIST;
197                         } else {
198                                 error = cache_vget(&nd->nl_nch, cred, 
199                                                     LK_EXCLUSIVE, &vp);
200                         }
201                         if (error)
202                                 return (error);
203                         fmode &= ~O_CREAT;
204                 }
205         } else {
206                 error = cache_vget(&nd->nl_nch, cred, LK_EXCLUSIVE, &vp);
207                 if (error)
208                         return (error);
209         }
210
211         /*
212          * We have a locked vnode and ncp now.  Note that the ncp will
213          * be cleaned up by the caller if nd->nl_nch is left intact.
214          */
215         if (vp->v_type == VLNK) {
216                 error = EMLINK;
217                 goto bad;
218         }
219         if (vp->v_type == VSOCK) {
220                 error = EOPNOTSUPP;
221                 goto bad;
222         }
223         if ((fmode & O_CREAT) == 0) {
224                 mode = 0;
225                 if (fmode & (FWRITE | O_TRUNC)) {
226                         if (vp->v_type == VDIR) {
227                                 error = EISDIR;
228                                 goto bad;
229                         }
230                         error = vn_writechk(vp, &nd->nl_nch);
231                         if (error) {
232                                 /*
233                                  * Special stale handling, re-resolve the
234                                  * vnode.
235                                  */
236                                 if (error == ESTALE) {
237                                         vput(vp);
238                                         vp = NULL;
239                                         cache_setunresolved(&nd->nl_nch);
240                                         error = cache_resolve(&nd->nl_nch, cred);
241                                         if (error == 0)
242                                                 goto again;
243                                 }
244                                 goto bad;
245                         }
246                         mode |= VWRITE;
247                 }
248                 if (fmode & FREAD)
249                         mode |= VREAD;
250                 if (mode) {
251                         error = VOP_ACCESS(vp, mode, cred);
252                         if (error) {
253                                 /*
254                                  * Special stale handling, re-resolve the
255                                  * vnode.
256                                  */
257                                 if (error == ESTALE) {
258                                         vput(vp);
259                                         vp = NULL;
260                                         cache_setunresolved(&nd->nl_nch);
261                                         error = cache_resolve(&nd->nl_nch, cred);
262                                         if (error == 0)
263                                                 goto again;
264                                 }
265                                 goto bad;
266                         }
267                 }
268         }
269         if (fmode & O_TRUNC) {
270                 vn_unlock(vp);                          /* XXX */
271                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
272                 VATTR_NULL(vap);
273                 vap->va_size = 0;
274                 error = VOP_SETATTR(vp, vap, cred);
275                 if (error)
276                         goto bad;
277         }
278
279         /*
280          * Setup the fp so VOP_OPEN can override it.  No descriptor has been
281          * associated with the fp yet so we own it clean.  
282          *
283          * f_nchandle inherits nl_nch.  This used to be necessary only for
284          * directories but now we do it unconditionally so f*() ops
285          * such as fchmod() can access the actual namespace that was
286          * used to open the file.
287          */
288         if (fp) {
289                 fp->f_nchandle = nd->nl_nch;
290                 cache_zero(&nd->nl_nch);
291                 cache_unlock(&fp->f_nchandle);
292         }
293
294         /*
295          * Get rid of nl_nch.  vn_open does not return it (it returns the
296          * vnode or the file pointer).  Note: we can't leave nl_nch locked
297          * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
298          * on /dev/ttyd0
299          */
300         if (nd->nl_nch.ncp)
301                 cache_put(&nd->nl_nch);
302
303         error = VOP_OPEN(vp, fmode, cred, fp);
304         if (error) {
305                 /*
306                  * setting f_ops to &badfileops will prevent the descriptor
307                  * code from trying to close and release the vnode, since
308                  * the open failed we do not want to call close.
309                  */
310                 if (fp) {
311                         fp->f_data = NULL;
312                         fp->f_ops = &badfileops;
313                 }
314                 goto bad;
315         }
316
317 #if 0
318         /*
319          * Assert that VREG files have been setup for vmio.
320          */
321         KASSERT(vp->v_type != VREG || vp->v_object != NULL,
322                 ("vn_open: regular file was not VMIO enabled!"));
323 #endif
324
325         /*
326          * Return the vnode.  XXX needs some cleaning up.  The vnode is
327          * only returned in the fp == NULL case.
328          */
329         if (fp == NULL) {
330                 nd->nl_open_vp = vp;
331                 nd->nl_vp_fmode = fmode;
332                 if ((nd->nl_flags & NLC_LOCKVP) == 0)
333                         vn_unlock(vp);
334         } else {
335                 vput(vp);
336         }
337         return (0);
338 bad:
339         if (vp)
340                 vput(vp);
341         return (error);
342 }
343
344 int
345 vn_opendisk(const char *devname, int fmode, struct vnode **vpp)
346 {
347         struct vnode *vp;
348         int error;
349
350         if (strncmp(devname, "/dev/", 5) == 0)
351                 devname += 5;
352         if ((vp = getsynthvnode(devname)) == NULL) {
353                 error = ENODEV;
354         } else {
355                 error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL);
356                 vn_unlock(vp);
357                 if (error) {
358                         vrele(vp);
359                         vp = NULL;
360                 }
361         }
362         *vpp = vp;
363         return (error);
364 }
365
366 /*
367  * Check for write permissions on the specified vnode.  nch may be NULL.
368  */
369 int
370 vn_writechk(struct vnode *vp, struct nchandle *nch)
371 {
372         /*
373          * If there's shared text associated with
374          * the vnode, try to free it up once.  If
375          * we fail, we can't allow writing.
376          */
377         if (vp->v_flag & VTEXT)
378                 return (ETXTBSY);
379
380         /*
381          * If the vnode represents a regular file, check the mount
382          * point via the nch.  This may be a different mount point
383          * then the one embedded in the vnode (e.g. nullfs).
384          *
385          * We can still write to non-regular files (e.g. devices)
386          * via read-only mounts.
387          */
388         if (nch && nch->ncp && vp->v_type == VREG)
389                 return (ncp_writechk(nch));
390         return (0);
391 }
392
393 /*
394  * Check whether the underlying mount is read-only.  The mount point 
395  * referenced by the namecache may be different from the mount point
396  * used by the underlying vnode in the case of NULLFS, so a separate
397  * check is needed.
398  */
399 int
400 ncp_writechk(struct nchandle *nch)
401 {
402         if (nch->mount && (nch->mount->mnt_flag & MNT_RDONLY))
403                 return (EROFS);
404         return(0);
405 }
406
407 /*
408  * Vnode close call
409  */
410 int
411 vn_close(struct vnode *vp, int flags)
412 {
413         int error;
414
415         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
416                 error = VOP_CLOSE(vp, flags);
417                 vn_unlock(vp);
418         }
419         vrele(vp);
420         return (error);
421 }
422
423 static __inline
424 int
425 sequential_heuristic(struct uio *uio, struct file *fp)
426 {
427         /*
428          * Sequential heuristic - detect sequential operation
429          */
430         if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
431             uio->uio_offset == fp->f_nextoff) {
432                 int tmpseq = fp->f_seqcount;
433                 /*
434                  * XXX we assume that the filesystem block size is
435                  * the default.  Not true, but still gives us a pretty
436                  * good indicator of how sequential the read operations
437                  * are.
438                  */
439                 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
440                 if (tmpseq > IO_SEQMAX)
441                         tmpseq = IO_SEQMAX;
442                 fp->f_seqcount = tmpseq;
443                 return(fp->f_seqcount << IO_SEQSHIFT);
444         }
445
446         /*
447          * Not sequential, quick draw-down of seqcount
448          */
449         if (fp->f_seqcount > 1)
450                 fp->f_seqcount = 1;
451         else
452                 fp->f_seqcount = 0;
453         return(0);
454 }
455
456 /*
457  * Package up an I/O request on a vnode into a uio and do it.
458  */
459 int
460 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
461         off_t offset, enum uio_seg segflg, int ioflg, 
462         struct ucred *cred, int *aresid)
463 {
464         struct uio auio;
465         struct iovec aiov;
466         struct ccms_lock ccms_lock;
467         int error;
468
469         if ((ioflg & IO_NODELOCKED) == 0)
470                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
471         auio.uio_iov = &aiov;
472         auio.uio_iovcnt = 1;
473         aiov.iov_base = base;
474         aiov.iov_len = len;
475         auio.uio_resid = len;
476         auio.uio_offset = offset;
477         auio.uio_segflg = segflg;
478         auio.uio_rw = rw;
479         auio.uio_td = curthread;
480         ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, &auio);
481         if (rw == UIO_READ) {
482                 error = VOP_READ(vp, &auio, ioflg, cred);
483         } else {
484                 error = VOP_WRITE(vp, &auio, ioflg, cred);
485         }
486         ccms_lock_put(&vp->v_ccms, &ccms_lock);
487         if (aresid)
488                 *aresid = auio.uio_resid;
489         else
490                 if (auio.uio_resid && error == 0)
491                         error = EIO;
492         if ((ioflg & IO_NODELOCKED) == 0)
493                 vn_unlock(vp);
494         return (error);
495 }
496
497 /*
498  * Package up an I/O request on a vnode into a uio and do it.  The I/O
499  * request is split up into smaller chunks and we try to avoid saturating
500  * the buffer cache while potentially holding a vnode locked, so we 
501  * check bwillwrite() before calling vn_rdwr().  We also call uio_yield()
502  * to give other processes a chance to lock the vnode (either other processes
503  * core'ing the same binary, or unrelated processes scanning the directory).
504  */
505 int
506 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
507                  off_t offset, enum uio_seg segflg, int ioflg,
508                  struct ucred *cred, int *aresid)
509 {
510         int error = 0;
511
512         do {
513                 int chunk;
514
515                 /*
516                  * Force `offset' to a multiple of MAXBSIZE except possibly
517                  * for the first chunk, so that filesystems only need to
518                  * write full blocks except possibly for the first and last
519                  * chunks.
520                  */
521                 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
522
523                 if (chunk > len)
524                         chunk = len;
525                 if (rw != UIO_READ && vp->v_type == VREG)
526                         bwillwrite();
527                 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
528                             ioflg, cred, aresid);
529                 len -= chunk;   /* aresid calc already includes length */
530                 if (error)
531                         break;
532                 offset += chunk;
533                 base += chunk;
534                 uio_yield();
535         } while (len);
536         if (aresid)
537                 *aresid += len;
538         return (error);
539 }
540
541 /*
542  * MPALMOSTSAFE - acquires mplock
543  */
544 static int
545 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
546 {
547         struct ccms_lock ccms_lock;
548         struct vnode *vp;
549         int error, ioflag;
550
551         get_mplock();
552         KASSERT(uio->uio_td == curthread,
553                 ("uio_td %p is not td %p", uio->uio_td, curthread));
554         vp = (struct vnode *)fp->f_data;
555
556         ioflag = 0;
557         if (flags & O_FBLOCKING) {
558                 /* ioflag &= ~IO_NDELAY; */
559         } else if (flags & O_FNONBLOCKING) {
560                 ioflag |= IO_NDELAY;
561         } else if (fp->f_flag & FNONBLOCK) {
562                 ioflag |= IO_NDELAY;
563         }
564         if (flags & O_FBUFFERED) {
565                 /* ioflag &= ~IO_DIRECT; */
566         } else if (flags & O_FUNBUFFERED) {
567                 ioflag |= IO_DIRECT;
568         } else if (fp->f_flag & O_DIRECT) {
569                 ioflag |= IO_DIRECT;
570         }
571         vn_lock(vp, LK_SHARED | LK_RETRY);
572         if ((flags & O_FOFFSET) == 0)
573                 uio->uio_offset = fp->f_offset;
574         ioflag |= sequential_heuristic(uio, fp);
575
576         ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
577         error = VOP_READ(vp, uio, ioflag, cred);
578         ccms_lock_put(&vp->v_ccms, &ccms_lock);
579         if ((flags & O_FOFFSET) == 0)
580                 fp->f_offset = uio->uio_offset;
581         fp->f_nextoff = uio->uio_offset;
582         vn_unlock(vp);
583         rel_mplock();
584         return (error);
585 }
586
587 /*
588  * Device-optimized file table vnode read routine.
589  *
590  * This bypasses the VOP table and talks directly to the device.  Most
591  * filesystems just route to specfs and can make this optimization.
592  *
593  * MPALMOSTSAFE - acquires mplock
594  */
595 static int
596 svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
597 {
598         struct vnode *vp;
599         int ioflag;
600         int error;
601         cdev_t dev;
602
603         get_mplock();
604         KASSERT(uio->uio_td == curthread,
605                 ("uio_td %p is not td %p", uio->uio_td, curthread));
606
607         vp = (struct vnode *)fp->f_data;
608         if (vp == NULL || vp->v_type == VBAD) {
609                 error = EBADF;
610                 goto done;
611         }
612
613         if ((dev = vp->v_rdev) == NULL) {
614                 error = EBADF;
615                 goto done;
616         }
617         reference_dev(dev);
618
619         if (uio->uio_resid == 0) {
620                 error = 0;
621                 goto done;
622         }
623         if ((flags & O_FOFFSET) == 0)
624                 uio->uio_offset = fp->f_offset;
625
626         ioflag = 0;
627         if (flags & O_FBLOCKING) {
628                 /* ioflag &= ~IO_NDELAY; */
629         } else if (flags & O_FNONBLOCKING) {
630                 ioflag |= IO_NDELAY;
631         } else if (fp->f_flag & FNONBLOCK) {
632                 ioflag |= IO_NDELAY;
633         }
634         if (flags & O_FBUFFERED) {
635                 /* ioflag &= ~IO_DIRECT; */
636         } else if (flags & O_FUNBUFFERED) {
637                 ioflag |= IO_DIRECT;
638         } else if (fp->f_flag & O_DIRECT) {
639                 ioflag |= IO_DIRECT;
640         }
641         ioflag |= sequential_heuristic(uio, fp);
642
643         error = dev_dread(dev, uio, ioflag);
644
645         release_dev(dev);
646         if ((flags & O_FOFFSET) == 0)
647                 fp->f_offset = uio->uio_offset;
648         fp->f_nextoff = uio->uio_offset;
649 done:
650         rel_mplock();
651         return (error);
652 }
653
654 /*
655  * MPALMOSTSAFE - acquires mplock
656  */
657 static int
658 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
659 {
660         struct ccms_lock ccms_lock;
661         struct vnode *vp;
662         int error, ioflag;
663
664         get_mplock();
665         KASSERT(uio->uio_td == curthread,
666                 ("uio_td %p is not p %p", uio->uio_td, curthread));
667         vp = (struct vnode *)fp->f_data;
668         if (vp->v_type == VREG || vp->v_type == VDATABASE)
669                 bwillwrite();
670         vp = (struct vnode *)fp->f_data;        /* XXX needed? */
671
672         ioflag = IO_UNIT;
673         if (vp->v_type == VREG &&
674            ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
675                 ioflag |= IO_APPEND;
676         }
677
678         if (flags & O_FBLOCKING) {
679                 /* ioflag &= ~IO_NDELAY; */
680         } else if (flags & O_FNONBLOCKING) {
681                 ioflag |= IO_NDELAY;
682         } else if (fp->f_flag & FNONBLOCK) {
683                 ioflag |= IO_NDELAY;
684         }
685         if (flags & O_FBUFFERED) {
686                 /* ioflag &= ~IO_DIRECT; */
687         } else if (flags & O_FUNBUFFERED) {
688                 ioflag |= IO_DIRECT;
689         } else if (fp->f_flag & O_DIRECT) {
690                 ioflag |= IO_DIRECT;
691         }
692         if (flags & O_FASYNCWRITE) {
693                 /* ioflag &= ~IO_SYNC; */
694         } else if (flags & O_FSYNCWRITE) {
695                 ioflag |= IO_SYNC;
696         } else if (fp->f_flag & O_FSYNC) {
697                 ioflag |= IO_SYNC;
698         }
699
700         if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
701                 ioflag |= IO_SYNC;
702         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
703         if ((flags & O_FOFFSET) == 0)
704                 uio->uio_offset = fp->f_offset;
705         ioflag |= sequential_heuristic(uio, fp);
706         ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
707         error = VOP_WRITE(vp, uio, ioflag, cred);
708         ccms_lock_put(&vp->v_ccms, &ccms_lock);
709         if ((flags & O_FOFFSET) == 0)
710                 fp->f_offset = uio->uio_offset;
711         fp->f_nextoff = uio->uio_offset;
712         vn_unlock(vp);
713         rel_mplock();
714         return (error);
715 }
716
717 /*
718  * Device-optimized file table vnode write routine.
719  *
720  * This bypasses the VOP table and talks directly to the device.  Most
721  * filesystems just route to specfs and can make this optimization.
722  *
723  * MPALMOSTSAFE - acquires mplock
724  */
725 static int
726 svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
727 {
728         struct vnode *vp;
729         int ioflag;
730         int error;
731         cdev_t dev;
732
733         get_mplock();
734         KASSERT(uio->uio_td == curthread,
735                 ("uio_td %p is not p %p", uio->uio_td, curthread));
736
737         vp = (struct vnode *)fp->f_data;
738         if (vp == NULL || vp->v_type == VBAD) {
739                 error = EBADF;
740                 goto done;
741         }
742         if (vp->v_type == VREG)
743                 bwillwrite();
744         vp = (struct vnode *)fp->f_data;        /* XXX needed? */
745
746         if ((dev = vp->v_rdev) == NULL) {
747                 error = EBADF;
748                 goto done;
749         }
750         reference_dev(dev);
751
752         if ((flags & O_FOFFSET) == 0)
753                 uio->uio_offset = fp->f_offset;
754
755         ioflag = IO_UNIT;
756         if (vp->v_type == VREG && 
757            ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
758                 ioflag |= IO_APPEND;
759         }
760
761         if (flags & O_FBLOCKING) {
762                 /* ioflag &= ~IO_NDELAY; */
763         } else if (flags & O_FNONBLOCKING) {
764                 ioflag |= IO_NDELAY;
765         } else if (fp->f_flag & FNONBLOCK) {
766                 ioflag |= IO_NDELAY;
767         }
768         if (flags & O_FBUFFERED) {
769                 /* ioflag &= ~IO_DIRECT; */
770         } else if (flags & O_FUNBUFFERED) {
771                 ioflag |= IO_DIRECT;
772         } else if (fp->f_flag & O_DIRECT) {
773                 ioflag |= IO_DIRECT;
774         }
775         if (flags & O_FASYNCWRITE) {
776                 /* ioflag &= ~IO_SYNC; */
777         } else if (flags & O_FSYNCWRITE) {
778                 ioflag |= IO_SYNC;
779         } else if (fp->f_flag & O_FSYNC) {
780                 ioflag |= IO_SYNC;
781         }
782
783         if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
784                 ioflag |= IO_SYNC;
785         ioflag |= sequential_heuristic(uio, fp);
786
787         error = dev_dwrite(dev, uio, ioflag);
788
789         release_dev(dev);
790         if ((flags & O_FOFFSET) == 0)
791                 fp->f_offset = uio->uio_offset;
792         fp->f_nextoff = uio->uio_offset;
793 done:
794         rel_mplock();
795         return (error);
796 }
797
798 /*
799  * MPALMOSTSAFE - acquires mplock
800  */
801 static int
802 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
803 {
804         struct vnode *vp;
805         int error;
806
807         get_mplock();
808         vp = (struct vnode *)fp->f_data;
809         error = vn_stat(vp, sb, cred);
810         rel_mplock();
811         return (error);
812 }
813
814 int
815 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
816 {
817         struct vattr vattr;
818         struct vattr *vap;
819         int error;
820         u_short mode;
821         cdev_t dev;
822
823         vap = &vattr;
824         error = VOP_GETATTR(vp, vap);
825         if (error)
826                 return (error);
827
828         /*
829          * Zero the spare stat fields
830          */
831         sb->st_lspare = 0;
832         sb->st_qspare = 0;
833
834         /*
835          * Copy from vattr table
836          */
837         if (vap->va_fsid != VNOVAL)
838                 sb->st_dev = vap->va_fsid;
839         else
840                 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
841         sb->st_ino = vap->va_fileid;
842         mode = vap->va_mode;
843         switch (vap->va_type) {
844         case VREG:
845                 mode |= S_IFREG;
846                 break;
847         case VDATABASE:
848                 mode |= S_IFDB;
849                 break;
850         case VDIR:
851                 mode |= S_IFDIR;
852                 break;
853         case VBLK:
854                 mode |= S_IFBLK;
855                 break;
856         case VCHR:
857                 mode |= S_IFCHR;
858                 break;
859         case VLNK:
860                 mode |= S_IFLNK;
861                 /* This is a cosmetic change, symlinks do not have a mode. */
862                 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
863                         sb->st_mode &= ~ACCESSPERMS;    /* 0000 */
864                 else
865                         sb->st_mode |= ACCESSPERMS;     /* 0777 */
866                 break;
867         case VSOCK:
868                 mode |= S_IFSOCK;
869                 break;
870         case VFIFO:
871                 mode |= S_IFIFO;
872                 break;
873         default:
874                 return (EBADF);
875         };
876         sb->st_mode = mode;
877         if (vap->va_nlink > (nlink_t)-1)
878                 sb->st_nlink = (nlink_t)-1;
879         else
880                 sb->st_nlink = vap->va_nlink;
881         sb->st_uid = vap->va_uid;
882         sb->st_gid = vap->va_gid;
883         sb->st_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
884         sb->st_size = vap->va_size;
885         sb->st_atimespec = vap->va_atime;
886         sb->st_mtimespec = vap->va_mtime;
887         sb->st_ctimespec = vap->va_ctime;
888
889         /*
890          * A VCHR and VBLK device may track the last access and last modified
891          * time independantly of the filesystem.  This is particularly true
892          * because device read and write calls may bypass the filesystem.
893          */
894         if (vp->v_type == VCHR || vp->v_type == VBLK) {
895                 if ((dev = vp->v_rdev) != NULL) {
896                         if (dev->si_lastread) {
897                                 sb->st_atimespec.tv_sec = dev->si_lastread;
898                                 sb->st_atimespec.tv_nsec = 0;
899                         }
900                         if (dev->si_lastwrite) {
901                                 sb->st_atimespec.tv_sec = dev->si_lastwrite;
902                                 sb->st_atimespec.tv_nsec = 0;
903                         }
904                 }
905         }
906
907         /*
908          * According to www.opengroup.org, the meaning of st_blksize is 
909          *   "a filesystem-specific preferred I/O block size for this 
910          *    object.  In some filesystem types, this may vary from file
911          *    to file"
912          * Default to PAGE_SIZE after much discussion.
913          */
914
915         if (vap->va_type == VREG) {
916                 sb->st_blksize = vap->va_blocksize;
917         } else if (vn_isdisk(vp, NULL)) {
918                 /*
919                  * XXX this is broken.  If the device is not yet open (aka
920                  * stat() call, aka v_rdev == NULL), how are we supposed
921                  * to get a valid block size out of it?
922                  */
923                 cdev_t dev;
924
925                 if ((dev = vp->v_rdev) == NULL) {
926                         if (vp->v_type == VCHR)
927                                 dev = get_dev(vp->v_umajor, vp->v_uminor);
928                 }
929                 sb->st_blksize = dev->si_bsize_best;
930                 if (sb->st_blksize < dev->si_bsize_phys)
931                         sb->st_blksize = dev->si_bsize_phys;
932                 if (sb->st_blksize < BLKDEV_IOSIZE)
933                         sb->st_blksize = BLKDEV_IOSIZE;
934         } else {
935                 sb->st_blksize = PAGE_SIZE;
936         }
937         
938         sb->st_flags = vap->va_flags;
939         if (suser_cred(cred, 0))
940                 sb->st_gen = 0;
941         else
942                 sb->st_gen = (u_int32_t)vap->va_gen;
943
944 #if (S_BLKSIZE == 512)
945         /* Optimize this case */
946         sb->st_blocks = vap->va_bytes >> 9;
947 #else
948         sb->st_blocks = vap->va_bytes / S_BLKSIZE;
949 #endif
950         sb->st_fsmid = vap->va_fsmid;
951         return (0);
952 }
953
954 /*
955  * MPALMOSTSAFE - acquires mplock
956  */
957 static int
958 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
959 {
960         struct vnode *vp = ((struct vnode *)fp->f_data);
961         struct vnode *ovp;
962         struct vattr vattr;
963         int error;
964
965         get_mplock();
966
967         switch (vp->v_type) {
968         case VREG:
969         case VDIR:
970                 if (com == FIONREAD) {
971                         if ((error = VOP_GETATTR(vp, &vattr)) != 0)
972                                 break;
973                         *(int *)data = vattr.va_size - fp->f_offset;
974                         error = 0;
975                         break;
976                 }
977                 if (com == FIOASYNC) {                          /* XXX */
978                         error = 0;                              /* XXX */
979                         break;
980                 }
981                 /* fall into ... */
982         default:
983 #if 0
984                 return (ENOTTY);
985 #endif
986         case VFIFO:
987         case VCHR:
988         case VBLK:
989                 if (com == FIODTYPE) {
990                         if (vp->v_type != VCHR && vp->v_type != VBLK) {
991                                 error = ENOTTY;
992                                 break;
993                         }
994                         *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
995                         error = 0;
996                         break;
997                 }
998                 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
999                 if (error == 0 && com == TIOCSCTTY) {
1000                         struct proc *p = curthread->td_proc;
1001                         struct session *sess;
1002
1003                         if (p == NULL) {
1004                                 error = ENOTTY;
1005                                 break;
1006                         }
1007
1008                         sess = p->p_session;
1009                         /* Do nothing if reassigning same control tty */
1010                         if (sess->s_ttyvp == vp) {
1011                                 error = 0;
1012                                 break;
1013                         }
1014
1015                         /* Get rid of reference to old control tty */
1016                         ovp = sess->s_ttyvp;
1017                         vref(vp);
1018                         sess->s_ttyvp = vp;
1019                         if (ovp)
1020                                 vrele(ovp);
1021                 }
1022                 break;
1023         }
1024         rel_mplock();
1025         return (error);
1026 }
1027
1028 /*
1029  * MPALMOSTSAFE - acquires mplock
1030  */
1031 static int
1032 vn_poll(struct file *fp, int events, struct ucred *cred)
1033 {
1034         int error;
1035
1036         get_mplock();
1037         error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
1038         rel_mplock();
1039         return (error);
1040 }
1041
1042 /*
1043  * Check that the vnode is still valid, and if so
1044  * acquire requested lock.
1045  */
1046 int
1047 #ifndef DEBUG_LOCKS
1048 vn_lock(struct vnode *vp, int flags)
1049 #else
1050 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
1051 #endif
1052 {
1053         int error;
1054         
1055         do {
1056 #ifdef  DEBUG_LOCKS
1057                 vp->filename = filename;
1058                 vp->line = line;
1059                 error = debuglockmgr(&vp->v_lock, flags,
1060                                      "vn_lock", filename, line);
1061 #else
1062                 error = lockmgr(&vp->v_lock, flags);
1063 #endif
1064                 if (error == 0)
1065                         break;
1066         } while (flags & LK_RETRY);
1067
1068         /*
1069          * Because we (had better!) have a ref on the vnode, once it
1070          * goes to VRECLAIMED state it will not be recycled until all
1071          * refs go away.  So we can just check the flag.
1072          */
1073         if (error == 0 && (vp->v_flag & VRECLAIMED)) {
1074                 lockmgr(&vp->v_lock, LK_RELEASE);
1075                 error = ENOENT;
1076         }
1077         return (error);
1078 }
1079
1080 void
1081 vn_unlock(struct vnode *vp)
1082 {
1083         lockmgr(&vp->v_lock, LK_RELEASE);
1084 }
1085
1086 int
1087 vn_islocked(struct vnode *vp)
1088 {
1089         return (lockstatus(&vp->v_lock, curthread));
1090 }
1091
1092 /*
1093  * MPALMOSTSAFE - acquires mplock
1094  */
1095 static int
1096 vn_closefile(struct file *fp)
1097 {
1098         int error;
1099
1100         get_mplock();
1101         fp->f_ops = &badfileops;
1102         error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
1103         rel_mplock();
1104         return(error);
1105 }
1106
1107 /*
1108  * MPALMOSTSAFE - acquires mplock
1109  */
1110 static int
1111 vn_kqfilter(struct file *fp, struct knote *kn)
1112 {
1113         int error;
1114
1115         get_mplock();
1116         error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);
1117         rel_mplock();
1118         return (error);
1119 }