Merge from vendor branch ZLIB:
[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.23 2004/10/12 19:20:46 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/namei.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 vn_poll (struct file *fp, int events, struct ucred *cred,
64                 struct thread *td);
65 static int vn_kqfilter (struct file *fp, struct knote *kn);
66 static int vn_statfile (struct file *fp, struct stat *sb, struct thread *td);
67 static int vn_write (struct file *fp, struct uio *uio, 
68                 struct ucred *cred, int flags, struct thread *td);
69
70 struct  fileops vnops = {
71         NULL,   /* port */
72         NULL,   /* clone */
73         vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
74         vn_statfile, vn_closefile
75 };
76
77 /*
78  * Common code for vnode open operations.
79  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
80  * 
81  * Note that this does NOT free nameidata for the successful case,
82  * due to the NDINIT being done elsewhere.
83  */
84 int
85 vn_open(ndp, fmode, cmode)
86         struct nameidata *ndp;
87         int fmode, cmode;
88 {
89         struct vnode *vp;
90         struct thread *td = ndp->ni_cnd.cn_td;
91         struct ucred *cred = ndp->ni_cnd.cn_cred;
92         struct vattr vat;
93         struct vattr *vap = &vat;
94         int mode, error;
95
96         KKASSERT(cred == td->td_proc->p_ucred);
97
98         if (fmode & O_CREAT) {
99                 ndp->ni_cnd.cn_nameiop = NAMEI_CREATE;
100                 ndp->ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
101                 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
102                         ndp->ni_cnd.cn_flags |= CNP_FOLLOW;
103                 bwillwrite();
104                 error = namei(ndp);
105                 if (error)
106                         return (error);
107                 if (ndp->ni_vp == NULL) {
108                         VATTR_NULL(vap);
109                         vap->va_type = VREG;
110                         vap->va_mode = cmode;
111                         if (fmode & O_EXCL)
112                                 vap->va_vaflags |= VA_EXCLUSIVE;
113                         VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
114                         error = VOP_CREATE(ndp->ni_dvp, NCPNULL, &ndp->ni_vp,
115                                            &ndp->ni_cnd, vap);
116                         if (error) {
117                                 NDFREE(ndp, NDF_ONLY_PNBUF);
118                                 vput(ndp->ni_dvp);
119                                 return (error);
120                         }
121                         vput(ndp->ni_dvp);
122                         ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
123                         ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
124                         fmode &= ~O_TRUNC;
125                         vp = ndp->ni_vp;
126                 } else {
127                         if (ndp->ni_dvp == ndp->ni_vp)
128                                 vrele(ndp->ni_dvp);
129                         else
130                                 vput(ndp->ni_dvp);
131                         ndp->ni_dvp = NULL;
132                         vp = ndp->ni_vp;
133                         if (fmode & O_EXCL) {
134                                 error = EEXIST;
135                                 goto bad;
136                         }
137                         fmode &= ~O_CREAT;
138                 }
139         } else {
140                 ndp->ni_cnd.cn_nameiop = NAMEI_LOOKUP;
141                 ndp->ni_cnd.cn_flags = CNP_LOCKLEAF |
142                     ((fmode & O_NOFOLLOW) ? 0 : CNP_FOLLOW);
143                 error = namei(ndp);
144                 if (error)
145                         return (error);
146                 vp = ndp->ni_vp;
147         }
148         if (vp->v_type == VLNK) {
149                 error = EMLINK;
150                 goto bad;
151         }
152         if (vp->v_type == VSOCK) {
153                 error = EOPNOTSUPP;
154                 goto bad;
155         }
156         if ((fmode & O_CREAT) == 0) {
157                 mode = 0;
158                 if (fmode & (FWRITE | O_TRUNC)) {
159                         if (vp->v_type == VDIR) {
160                                 error = EISDIR;
161                                 goto bad;
162                         }
163                         error = vn_writechk(vp);
164                         if (error)
165                                 goto bad;
166                         mode |= VWRITE;
167                 }
168                 if (fmode & FREAD)
169                         mode |= VREAD;
170                 if (mode) {
171                         error = VOP_ACCESS(vp, mode, cred, td);
172                         if (error)
173                                 goto bad;
174                 }
175         }
176         if (fmode & O_TRUNC) {
177                 VOP_UNLOCK(vp, 0, td);                  /* XXX */
178                 VOP_LEASE(vp, td, cred, LEASE_WRITE);
179                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);       /* XXX */
180                 VATTR_NULL(vap);
181                 vap->va_size = 0;
182                 error = VOP_SETATTR(vp, vap, cred, td);
183                 if (error)
184                         goto bad;
185         }
186         error = VOP_OPEN(vp, fmode, cred, td);
187         if (error)
188                 goto bad;
189         /*
190          * Make sure that a VM object is created for VMIO support.
191          */
192         if (vn_canvmio(vp) == TRUE) {
193                 if ((error = vfs_object_create(vp, td)) != 0)
194                         goto bad;
195         }
196
197         if (fmode & FWRITE)
198                 vp->v_writecount++;
199         return (0);
200 bad:
201         NDFREE(ndp, NDF_ONLY_PNBUF);
202         vput(vp);
203         return (error);
204 }
205
206 /*
207  * Check for write permissions on the specified vnode.
208  * Prototype text segments cannot be written.
209  */
210 int
211 vn_writechk(vp)
212         struct vnode *vp;
213 {
214
215         /*
216          * If there's shared text associated with
217          * the vnode, try to free it up once.  If
218          * we fail, we can't allow writing.
219          */
220         if (vp->v_flag & VTEXT)
221                 return (ETXTBSY);
222         return (0);
223 }
224
225 /*
226  * Vnode close call
227  */
228 int
229 vn_close(struct vnode *vp, int flags, struct thread *td)
230 {
231         int error;
232
233         if (flags & FWRITE)
234                 vp->v_writecount--;
235         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td)) == 0) {
236                 error = VOP_CLOSE(vp, flags, td);
237                 VOP_UNLOCK(vp, 0, td);
238         }
239         vrele(vp);
240         return (error);
241 }
242
243 static __inline
244 int
245 sequential_heuristic(struct uio *uio, struct file *fp)
246 {
247         /*
248          * Sequential heuristic - detect sequential operation
249          */
250         if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
251             uio->uio_offset == fp->f_nextoff) {
252                 int tmpseq = fp->f_seqcount;
253                 /*
254                  * XXX we assume that the filesystem block size is
255                  * the default.  Not true, but still gives us a pretty
256                  * good indicator of how sequential the read operations
257                  * are.
258                  */
259                 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
260                 if (tmpseq > IO_SEQMAX)
261                         tmpseq = IO_SEQMAX;
262                 fp->f_seqcount = tmpseq;
263                 return(fp->f_seqcount << IO_SEQSHIFT);
264         }
265
266         /*
267          * Not sequential, quick draw-down of seqcount
268          */
269         if (fp->f_seqcount > 1)
270                 fp->f_seqcount = 1;
271         else
272                 fp->f_seqcount = 0;
273         return(0);
274 }
275
276 /*
277  * Package up an I/O request on a vnode into a uio and do it.
278  *
279  * We are going to assume the caller has done the appropriate
280  * VOP_LEASE() call before calling vn_rdwr()
281  */
282 int
283 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
284         enum uio_rw rw;
285         struct vnode *vp;
286         caddr_t base;
287         int len;
288         off_t offset;
289         enum uio_seg segflg;
290         int ioflg;
291         struct ucred *cred;
292         int *aresid;
293         struct thread *td;
294 {
295         struct uio auio;
296         struct iovec aiov;
297         int error;
298
299         if ((ioflg & IO_NODELOCKED) == 0)
300                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
301         auio.uio_iov = &aiov;
302         auio.uio_iovcnt = 1;
303         aiov.iov_base = base;
304         aiov.iov_len = len;
305         auio.uio_resid = len;
306         auio.uio_offset = offset;
307         auio.uio_segflg = segflg;
308         auio.uio_rw = rw;
309         auio.uio_td = td;
310         if (rw == UIO_READ) {
311                 error = VOP_READ(vp, &auio, ioflg, cred);
312         } else {
313                 error = VOP_WRITE(vp, &auio, ioflg, cred);
314         }
315         if (aresid)
316                 *aresid = auio.uio_resid;
317         else
318                 if (auio.uio_resid && error == 0)
319                         error = EIO;
320         if ((ioflg & IO_NODELOCKED) == 0)
321                 VOP_UNLOCK(vp, 0, td);
322         return (error);
323 }
324
325 /*
326  * Package up an I/O request on a vnode into a uio and do it.  The I/O
327  * request is split up into smaller chunks and we try to avoid saturating
328  * the buffer cache while potentially holding a vnode locked, so we 
329  * check bwillwrite() before calling vn_rdwr().  We also call uio_yield()
330  * to give other processes a chance to lock the vnode (either other processes
331  * core'ing the same binary, or unrelated processes scanning the directory).
332  */
333 int
334 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
335         enum uio_rw rw;
336         struct vnode *vp;
337         caddr_t base;
338         int len;
339         off_t offset;
340         enum uio_seg segflg;
341         int ioflg;
342         struct ucred *cred;
343         int *aresid;
344         struct thread *td;
345 {
346         int error = 0;
347
348         do {
349                 int chunk;
350
351                 /*
352                  * Force `offset' to a multiple of MAXBSIZE except possibly
353                  * for the first chunk, so that filesystems only need to
354                  * write full blocks except possibly for the first and last
355                  * chunks.
356                  */
357                 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
358
359                 if (chunk > len)
360                         chunk = len;
361                 if (rw != UIO_READ && vp->v_type == VREG)
362                         bwillwrite();
363                 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
364                             ioflg, cred, aresid, td);
365                 len -= chunk;   /* aresid calc already includes length */
366                 if (error)
367                         break;
368                 offset += chunk;
369                 base += chunk;
370                 uio_yield();
371         } while (len);
372         if (aresid)
373                 *aresid += len;
374         return (error);
375 }
376
377 /*
378  * File table vnode read routine.
379  */
380 static int
381 vn_read(fp, uio, cred, flags, td)
382         struct file *fp;
383         struct uio *uio;
384         struct ucred *cred;
385         struct thread *td;
386         int flags;
387 {
388         struct vnode *vp;
389         int error, ioflag;
390
391         KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
392         vp = (struct vnode *)fp->f_data;
393         ioflag = 0;
394         if (fp->f_flag & FNONBLOCK)
395                 ioflag |= IO_NDELAY;
396         if (fp->f_flag & O_DIRECT)
397                 ioflag |= IO_DIRECT;
398         VOP_LEASE(vp, td, cred, LEASE_READ);
399         vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
400         if ((flags & FOF_OFFSET) == 0)
401                 uio->uio_offset = fp->f_offset;
402
403         ioflag |= sequential_heuristic(uio, fp);
404
405         error = VOP_READ(vp, uio, ioflag, cred);
406         if ((flags & FOF_OFFSET) == 0)
407                 fp->f_offset = uio->uio_offset;
408         fp->f_nextoff = uio->uio_offset;
409         VOP_UNLOCK(vp, 0, td);
410         return (error);
411 }
412
413 /*
414  * File table vnode write routine.
415  */
416 static int
417 vn_write(fp, uio, cred, flags, td)
418         struct file *fp;
419         struct uio *uio;
420         struct ucred *cred;
421         struct thread *td;
422         int flags;
423 {
424         struct vnode *vp;
425         int error, ioflag;
426
427         KASSERT(uio->uio_td == td, ("uio_procp %p is not p %p", 
428             uio->uio_td, td));
429         vp = (struct vnode *)fp->f_data;
430         if (vp->v_type == VREG)
431                 bwillwrite();
432         vp = (struct vnode *)fp->f_data;        /* XXX needed? */
433         ioflag = IO_UNIT;
434         if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
435                 ioflag |= IO_APPEND;
436         if (fp->f_flag & FNONBLOCK)
437                 ioflag |= IO_NDELAY;
438         if (fp->f_flag & O_DIRECT)
439                 ioflag |= IO_DIRECT;
440         if ((fp->f_flag & O_FSYNC) ||
441             (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
442                 ioflag |= IO_SYNC;
443         VOP_LEASE(vp, td, cred, LEASE_WRITE);
444         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
445         if ((flags & FOF_OFFSET) == 0)
446                 uio->uio_offset = fp->f_offset;
447         ioflag |= sequential_heuristic(uio, fp);
448         error = VOP_WRITE(vp, uio, ioflag, cred);
449         if ((flags & FOF_OFFSET) == 0)
450                 fp->f_offset = uio->uio_offset;
451         fp->f_nextoff = uio->uio_offset;
452         VOP_UNLOCK(vp, 0, td);
453         return (error);
454 }
455
456 /*
457  * File table vnode stat routine.
458  */
459 static int
460 vn_statfile(struct file *fp, struct stat *sb, struct thread *td)
461 {
462         struct vnode *vp = (struct vnode *)fp->f_data;
463
464         return vn_stat(vp, sb, td);
465 }
466
467 int
468 vn_stat(struct vnode *vp, struct stat *sb, struct thread *td)
469 {
470         struct vattr vattr;
471         struct vattr *vap;
472         int error;
473         u_short mode;
474
475         vap = &vattr;
476         error = VOP_GETATTR(vp, vap, td);
477         if (error)
478                 return (error);
479
480         /*
481          * Zero the spare stat fields
482          */
483         sb->st_lspare = 0;
484         sb->st_qspare[0] = 0;
485         sb->st_qspare[1] = 0;
486
487         /*
488          * Copy from vattr table
489          */
490         if (vap->va_fsid != VNOVAL)
491                 sb->st_dev = vap->va_fsid;
492         else
493                 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
494         sb->st_ino = vap->va_fileid;
495         mode = vap->va_mode;
496         switch (vap->va_type) {
497         case VREG:
498                 mode |= S_IFREG;
499                 break;
500         case VDIR:
501                 mode |= S_IFDIR;
502                 break;
503         case VBLK:
504                 mode |= S_IFBLK;
505                 break;
506         case VCHR:
507                 mode |= S_IFCHR;
508                 break;
509         case VLNK:
510                 mode |= S_IFLNK;
511                 /* This is a cosmetic change, symlinks do not have a mode. */
512                 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
513                         sb->st_mode &= ~ACCESSPERMS;    /* 0000 */
514                 else
515                         sb->st_mode |= ACCESSPERMS;     /* 0777 */
516                 break;
517         case VSOCK:
518                 mode |= S_IFSOCK;
519                 break;
520         case VFIFO:
521                 mode |= S_IFIFO;
522                 break;
523         default:
524                 return (EBADF);
525         };
526         sb->st_mode = mode;
527         sb->st_nlink = vap->va_nlink;
528         sb->st_uid = vap->va_uid;
529         sb->st_gid = vap->va_gid;
530         sb->st_rdev = vap->va_rdev;
531         sb->st_size = vap->va_size;
532         sb->st_atimespec = vap->va_atime;
533         sb->st_mtimespec = vap->va_mtime;
534         sb->st_ctimespec = vap->va_ctime;
535
536         /*
537          * According to www.opengroup.org, the meaning of st_blksize is 
538          *   "a filesystem-specific preferred I/O block size for this 
539          *    object.  In some filesystem types, this may vary from file
540          *    to file"
541          * Default to PAGE_SIZE after much discussion.
542          */
543
544         if (vap->va_type == VREG) {
545                 sb->st_blksize = vap->va_blocksize;
546         } else if (vn_isdisk(vp, NULL)) {
547                 /*
548                  * XXX this is broken.  If the device is not yet open (aka
549                  * stat() call, aka v_rdev == NULL), how are we supposed
550                  * to get a valid block size out of it?
551                  */
552                 dev_t dev;
553
554                 if ((dev = vp->v_rdev) == NULL)
555                         dev = udev2dev(vp->v_udev, vp->v_type == VBLK);
556                 sb->st_blksize = dev->si_bsize_best;
557                 if (sb->st_blksize < dev->si_bsize_phys)
558                         sb->st_blksize = dev->si_bsize_phys;
559                 if (sb->st_blksize < BLKDEV_IOSIZE)
560                         sb->st_blksize = BLKDEV_IOSIZE;
561         } else {
562                 sb->st_blksize = PAGE_SIZE;
563         }
564         
565         sb->st_flags = vap->va_flags;
566         if (suser(td))
567                 sb->st_gen = 0;
568         else
569                 sb->st_gen = vap->va_gen;
570
571 #if (S_BLKSIZE == 512)
572         /* Optimize this case */
573         sb->st_blocks = vap->va_bytes >> 9;
574 #else
575         sb->st_blocks = vap->va_bytes / S_BLKSIZE;
576 #endif
577         return (0);
578 }
579
580 /*
581  * File table vnode ioctl routine.
582  */
583 static int
584 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
585 {
586         struct vnode *vp = ((struct vnode *)fp->f_data);
587         struct vnode *ovp;
588         struct ucred *ucred;
589         struct vattr vattr;
590         int error;
591
592         KKASSERT(td->td_proc != NULL);
593         ucred = td->td_proc->p_ucred;
594
595         switch (vp->v_type) {
596         case VREG:
597         case VDIR:
598                 if (com == FIONREAD) {
599                         error = VOP_GETATTR(vp, &vattr, td);
600                         if (error)
601                                 return (error);
602                         *(int *)data = vattr.va_size - fp->f_offset;
603                         return (0);
604                 }
605                 if (com == FIONBIO || com == FIOASYNC)  /* XXX */
606                         return (0);                     /* XXX */
607                 /* fall into ... */
608         default:
609 #if 0
610                 return (ENOTTY);
611 #endif
612         case VFIFO:
613         case VCHR:
614         case VBLK:
615                 if (com == FIODTYPE) {
616                         if (vp->v_type != VCHR && vp->v_type != VBLK)
617                                 return (ENOTTY);
618                         *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
619                         return (0);
620                 }
621                 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, td);
622                 if (error == 0 && com == TIOCSCTTY) {
623                         struct session *sess = td->td_proc->p_session;
624
625                         /* Do nothing if reassigning same control tty */
626                         if (sess->s_ttyvp == vp)
627                                 return (0);
628
629                         /* Get rid of reference to old control tty */
630                         ovp = sess->s_ttyvp;
631                         vref(vp);
632                         sess->s_ttyvp = vp;
633                         if (ovp)
634                                 vrele(ovp);
635                 }
636                 return (error);
637         }
638 }
639
640 /*
641  * File table vnode poll routine.
642  */
643 static int
644 vn_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
645 {
646         return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, td));
647 }
648
649 /*
650  * Check that the vnode is still valid, and if so
651  * acquire requested lock.
652  */
653 int
654 #ifndef DEBUG_LOCKS
655 vn_lock(struct vnode *vp, int flags, struct thread *td)
656 #else
657 debug_vn_lock(struct vnode *vp, int flags, struct thread *td,
658                 const char *filename, int line)
659 #endif
660 {
661         int error;
662         
663         do {
664 #ifdef  DEBUG_LOCKS
665                 vp->filename = filename;
666                 vp->line = line;
667 #endif
668                 error = VOP_LOCK(vp, flags | LK_NOPAUSE, td);
669                 if (error == 0)
670                         break;
671         } while (flags & LK_RETRY);
672
673         /*
674          * Because we (had better!) have a ref on the vnode, once it
675          * goes to VRECLAIMED state it will not be recycled until all
676          * refs go away.  So we can just check the flag.
677          */
678         if (error == 0 && (vp->v_flag & VRECLAIMED)) {
679                 VOP_UNLOCK(vp, 0, td);
680                 error = ENOENT;
681         }
682         return (error);
683 }
684
685 /*
686  * File table vnode close routine.
687  */
688 static int
689 vn_closefile(struct file *fp, struct thread *td)
690 {
691         int err;
692
693         fp->f_ops = &badfileops;
694         err = vn_close(((struct vnode *)fp->f_data), fp->f_flag, td);
695         return(err);
696 }
697
698 static int
699 vn_kqfilter(struct file *fp, struct knote *kn)
700 {
701
702         return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
703 }