proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / vfs_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/vfs_vnops.c,v 1.87.2.13 2002/12/29 18:19:53 dillon Exp $
dadab5e9 40 * $DragonFly: src/sys/kern/vfs_vnops.c,v 1.4 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/fcntl.h>
46#include <sys/file.h>
47#include <sys/stat.h>
48#include <sys/proc.h>
49#include <sys/mount.h>
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
dadab5e9 58static int vn_closefile __P((struct file *fp, struct thread *td));
984263bc 59static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
dadab5e9 60 struct thread *td));
984263bc 61static int vn_read __P((struct file *fp, struct uio *uio,
dadab5e9 62 struct ucred *cred, int flags, struct thread *td));
984263bc 63static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
dadab5e9 64 struct thread *td));
984263bc 65static int vn_kqfilter __P((struct file *fp, struct knote *kn));
dadab5e9 66static int vn_statfile __P((struct file *fp, struct stat *sb, struct thread *td));
984263bc 67static int vn_write __P((struct file *fp, struct uio *uio,
dadab5e9 68 struct ucred *cred, int flags, struct thread *td));
984263bc
MD
69
70struct fileops vnops = {
71 vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
72 vn_statfile, vn_closefile
73};
74
75/*
76 * Common code for vnode open operations.
77 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
78 *
79 * Note that this does NOT free nameidata for the successful case,
80 * due to the NDINIT being done elsewhere.
81 */
82int
83vn_open(ndp, fmode, cmode)
84 register struct nameidata *ndp;
85 int fmode, cmode;
86{
87 register struct vnode *vp;
dadab5e9
MD
88 register struct thread *td = ndp->ni_cnd.cn_td;
89 register struct ucred *cred = ndp->ni_cnd.cn_cred;
984263bc
MD
90 struct vattr vat;
91 struct vattr *vap = &vat;
92 int mode, error;
93
dadab5e9
MD
94 KKASSERT(cred == td->td_proc->p_ucred);
95
984263bc
MD
96 if (fmode & O_CREAT) {
97 ndp->ni_cnd.cn_nameiop = CREATE;
98 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
99 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
100 ndp->ni_cnd.cn_flags |= FOLLOW;
101 bwillwrite();
102 error = namei(ndp);
103 if (error)
104 return (error);
105 if (ndp->ni_vp == NULL) {
106 VATTR_NULL(vap);
107 vap->va_type = VREG;
108 vap->va_mode = cmode;
109 if (fmode & O_EXCL)
110 vap->va_vaflags |= VA_EXCLUSIVE;
dadab5e9 111 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
984263bc
MD
112 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
113 &ndp->ni_cnd, vap);
114 if (error) {
115 NDFREE(ndp, NDF_ONLY_PNBUF);
116 vput(ndp->ni_dvp);
117 return (error);
118 }
119 vput(ndp->ni_dvp);
120 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
121 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
122 fmode &= ~O_TRUNC;
123 vp = ndp->ni_vp;
124 } else {
125 if (ndp->ni_dvp == ndp->ni_vp)
126 vrele(ndp->ni_dvp);
127 else
128 vput(ndp->ni_dvp);
129 ndp->ni_dvp = NULL;
130 vp = ndp->ni_vp;
131 if (fmode & O_EXCL) {
132 error = EEXIST;
133 goto bad;
134 }
135 fmode &= ~O_CREAT;
136 }
137 } else {
138 ndp->ni_cnd.cn_nameiop = LOOKUP;
139 ndp->ni_cnd.cn_flags =
140 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
141 error = namei(ndp);
142 if (error)
143 return (error);
144 vp = ndp->ni_vp;
145 }
146 if (vp->v_type == VLNK) {
147 error = EMLINK;
148 goto bad;
149 }
150 if (vp->v_type == VSOCK) {
151 error = EOPNOTSUPP;
152 goto bad;
153 }
154 if ((fmode & O_CREAT) == 0) {
155 mode = 0;
156 if (fmode & (FWRITE | O_TRUNC)) {
157 if (vp->v_type == VDIR) {
158 error = EISDIR;
159 goto bad;
160 }
161 error = vn_writechk(vp);
162 if (error)
163 goto bad;
164 mode |= VWRITE;
165 }
166 if (fmode & FREAD)
167 mode |= VREAD;
168 if (mode) {
dadab5e9 169 error = VOP_ACCESS(vp, mode, cred, td);
984263bc
MD
170 if (error)
171 goto bad;
172 }
173 }
174 if (fmode & O_TRUNC) {
dadab5e9
MD
175 VOP_UNLOCK(vp, 0, td); /* XXX */
176 VOP_LEASE(vp, td, cred, LEASE_WRITE);
177 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
984263bc
MD
178 VATTR_NULL(vap);
179 vap->va_size = 0;
dadab5e9 180 error = VOP_SETATTR(vp, vap, cred, td);
984263bc
MD
181 if (error)
182 goto bad;
183 }
dadab5e9 184 error = VOP_OPEN(vp, fmode, cred, td);
984263bc
MD
185 if (error)
186 goto bad;
187 /*
188 * Make sure that a VM object is created for VMIO support.
189 */
190 if (vn_canvmio(vp) == TRUE) {
dadab5e9 191 if ((error = vfs_object_create(vp, td, cred)) != 0)
984263bc
MD
192 goto bad;
193 }
194
195 if (fmode & FWRITE)
196 vp->v_writecount++;
197 return (0);
198bad:
199 NDFREE(ndp, NDF_ONLY_PNBUF);
200 vput(vp);
201 return (error);
202}
203
204/*
205 * Check for write permissions on the specified vnode.
206 * Prototype text segments cannot be written.
207 */
208int
209vn_writechk(vp)
210 register struct vnode *vp;
211{
212
213 /*
214 * If there's shared text associated with
215 * the vnode, try to free it up once. If
216 * we fail, we can't allow writing.
217 */
218 if (vp->v_flag & VTEXT)
219 return (ETXTBSY);
220 return (0);
221}
222
223/*
224 * Vnode close call
225 */
226int
dadab5e9 227vn_close(struct vnode *vp, int flags, struct ucred *cred, struct thread *td)
984263bc
MD
228{
229 int error;
230
231 if (flags & FWRITE)
232 vp->v_writecount--;
dadab5e9 233 error = VOP_CLOSE(vp, flags, cred, td);
984263bc
MD
234 vrele(vp);
235 return (error);
236}
237
238static __inline
239int
240sequential_heuristic(struct uio *uio, struct file *fp)
241{
242 /*
243 * Sequential heuristic - detect sequential operation
244 */
245 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
246 uio->uio_offset == fp->f_nextoff) {
247 int tmpseq = fp->f_seqcount;
248 /*
249 * XXX we assume that the filesystem block size is
250 * the default. Not true, but still gives us a pretty
251 * good indicator of how sequential the read operations
252 * are.
253 */
254 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
255 if (tmpseq > IO_SEQMAX)
256 tmpseq = IO_SEQMAX;
257 fp->f_seqcount = tmpseq;
258 return(fp->f_seqcount << IO_SEQSHIFT);
259 }
260
261 /*
262 * Not sequential, quick draw-down of seqcount
263 */
264 if (fp->f_seqcount > 1)
265 fp->f_seqcount = 1;
266 else
267 fp->f_seqcount = 0;
268 return(0);
269}
270
271/*
272 * Package up an I/O request on a vnode into a uio and do it.
273 */
274int
dadab5e9 275vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
984263bc
MD
276 enum uio_rw rw;
277 struct vnode *vp;
278 caddr_t base;
279 int len;
280 off_t offset;
281 enum uio_seg segflg;
282 int ioflg;
283 struct ucred *cred;
284 int *aresid;
dadab5e9 285 struct thread *td;
984263bc
MD
286{
287 struct uio auio;
288 struct iovec aiov;
289 int error;
290
291 if ((ioflg & IO_NODELOCKED) == 0)
dadab5e9 292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
293 auio.uio_iov = &aiov;
294 auio.uio_iovcnt = 1;
295 aiov.iov_base = base;
296 aiov.iov_len = len;
297 auio.uio_resid = len;
298 auio.uio_offset = offset;
299 auio.uio_segflg = segflg;
300 auio.uio_rw = rw;
dadab5e9 301 auio.uio_td = td;
984263bc
MD
302 if (rw == UIO_READ) {
303 error = VOP_READ(vp, &auio, ioflg, cred);
304 } else {
305 error = VOP_WRITE(vp, &auio, ioflg, cred);
306 }
307 if (aresid)
308 *aresid = auio.uio_resid;
309 else
310 if (auio.uio_resid && error == 0)
311 error = EIO;
312 if ((ioflg & IO_NODELOCKED) == 0)
dadab5e9 313 VOP_UNLOCK(vp, 0, td);
984263bc
MD
314 return (error);
315}
316
317/*
318 * Package up an I/O request on a vnode into a uio and do it. The I/O
319 * request is split up into smaller chunks and we try to avoid saturating
320 * the buffer cache while potentially holding a vnode locked, so we
321 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
322 * to give other processes a chance to lock the vnode (either other processes
323 * core'ing the same binary, or unrelated processes scanning the directory).
324 */
325int
dadab5e9 326vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
984263bc
MD
327 enum uio_rw rw;
328 struct vnode *vp;
329 caddr_t base;
330 int len;
331 off_t offset;
332 enum uio_seg segflg;
333 int ioflg;
334 struct ucred *cred;
335 int *aresid;
dadab5e9 336 struct thread *td;
984263bc
MD
337{
338 int error = 0;
339
340 do {
341 int chunk = (len > MAXBSIZE) ? MAXBSIZE : len;
342
343 if (rw != UIO_READ && vp->v_type == VREG)
344 bwillwrite();
345 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
dadab5e9 346 ioflg, cred, aresid, td);
984263bc
MD
347 len -= chunk; /* aresid calc already includes length */
348 if (error)
349 break;
350 offset += chunk;
351 base += chunk;
352 uio_yield();
353 } while (len);
354 if (aresid)
355 *aresid += len;
356 return (error);
357}
358
359/*
360 * File table vnode read routine.
361 */
362static int
dadab5e9 363vn_read(fp, uio, cred, flags, td)
984263bc
MD
364 struct file *fp;
365 struct uio *uio;
366 struct ucred *cred;
dadab5e9 367 struct thread *td;
984263bc
MD
368 int flags;
369{
370 struct vnode *vp;
371 int error, ioflag;
372
dadab5e9 373 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
984263bc
MD
374 vp = (struct vnode *)fp->f_data;
375 ioflag = 0;
376 if (fp->f_flag & FNONBLOCK)
377 ioflag |= IO_NDELAY;
378 if (fp->f_flag & O_DIRECT)
379 ioflag |= IO_DIRECT;
dadab5e9
MD
380 VOP_LEASE(vp, td, cred, LEASE_READ);
381 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
984263bc
MD
382 if ((flags & FOF_OFFSET) == 0)
383 uio->uio_offset = fp->f_offset;
384
385 ioflag |= sequential_heuristic(uio, fp);
386
387 error = VOP_READ(vp, uio, ioflag, cred);
388 if ((flags & FOF_OFFSET) == 0)
389 fp->f_offset = uio->uio_offset;
390 fp->f_nextoff = uio->uio_offset;
dadab5e9 391 VOP_UNLOCK(vp, 0, td);
984263bc
MD
392 return (error);
393}
394
395/*
396 * File table vnode write routine.
397 */
398static int
dadab5e9 399vn_write(fp, uio, cred, flags, td)
984263bc
MD
400 struct file *fp;
401 struct uio *uio;
402 struct ucred *cred;
dadab5e9 403 struct thread *td;
984263bc
MD
404 int flags;
405{
406 struct vnode *vp;
407 int error, ioflag;
408
dadab5e9
MD
409 KASSERT(uio->uio_td == td, ("uio_procp %p is not p %p",
410 uio->uio_td, td));
984263bc
MD
411 vp = (struct vnode *)fp->f_data;
412 if (vp->v_type == VREG)
413 bwillwrite();
414 vp = (struct vnode *)fp->f_data; /* XXX needed? */
415 ioflag = IO_UNIT;
416 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
417 ioflag |= IO_APPEND;
418 if (fp->f_flag & FNONBLOCK)
419 ioflag |= IO_NDELAY;
420 if (fp->f_flag & O_DIRECT)
421 ioflag |= IO_DIRECT;
422 if ((fp->f_flag & O_FSYNC) ||
423 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
424 ioflag |= IO_SYNC;
dadab5e9
MD
425 VOP_LEASE(vp, td, cred, LEASE_WRITE);
426 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
427 if ((flags & FOF_OFFSET) == 0)
428 uio->uio_offset = fp->f_offset;
429 ioflag |= sequential_heuristic(uio, fp);
430 error = VOP_WRITE(vp, uio, ioflag, cred);
431 if ((flags & FOF_OFFSET) == 0)
432 fp->f_offset = uio->uio_offset;
433 fp->f_nextoff = uio->uio_offset;
dadab5e9 434 VOP_UNLOCK(vp, 0, td);
984263bc
MD
435 return (error);
436}
437
438/*
439 * File table vnode stat routine.
440 */
441static int
dadab5e9 442vn_statfile(struct file *fp, struct stat *sb, struct thread *td)
984263bc
MD
443{
444 struct vnode *vp = (struct vnode *)fp->f_data;
445
dadab5e9 446 return vn_stat(vp, sb, td);
984263bc
MD
447}
448
449int
dadab5e9 450vn_stat(struct vnode *vp, struct stat *sb, struct thread *td)
984263bc 451{
dadab5e9 452 struct proc *p = td->td_proc;
984263bc 453 struct vattr vattr;
dadab5e9 454 struct vattr *vap;
984263bc
MD
455 int error;
456 u_short mode;
457
dadab5e9 458 KKASSERT(p);
984263bc 459 vap = &vattr;
dadab5e9 460 error = VOP_GETATTR(vp, vap, p->p_ucred, td);
984263bc
MD
461 if (error)
462 return (error);
463
464 /*
465 * Zero the spare stat fields
466 */
467 sb->st_lspare = 0;
468 sb->st_qspare[0] = 0;
469 sb->st_qspare[1] = 0;
470
471 /*
472 * Copy from vattr table
473 */
474 if (vap->va_fsid != VNOVAL)
475 sb->st_dev = vap->va_fsid;
476 else
477 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
478 sb->st_ino = vap->va_fileid;
479 mode = vap->va_mode;
480 switch (vap->va_type) {
481 case VREG:
482 mode |= S_IFREG;
483 break;
484 case VDIR:
485 mode |= S_IFDIR;
486 break;
487 case VBLK:
488 mode |= S_IFBLK;
489 break;
490 case VCHR:
491 mode |= S_IFCHR;
492 break;
493 case VLNK:
494 mode |= S_IFLNK;
495 /* This is a cosmetic change, symlinks do not have a mode. */
496 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
497 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
498 else
499 sb->st_mode |= ACCESSPERMS; /* 0777 */
500 break;
501 case VSOCK:
502 mode |= S_IFSOCK;
503 break;
504 case VFIFO:
505 mode |= S_IFIFO;
506 break;
507 default:
508 return (EBADF);
509 };
510 sb->st_mode = mode;
511 sb->st_nlink = vap->va_nlink;
512 sb->st_uid = vap->va_uid;
513 sb->st_gid = vap->va_gid;
514 sb->st_rdev = vap->va_rdev;
515 sb->st_size = vap->va_size;
516 sb->st_atimespec = vap->va_atime;
517 sb->st_mtimespec = vap->va_mtime;
518 sb->st_ctimespec = vap->va_ctime;
519
520 /*
521 * According to www.opengroup.org, the meaning of st_blksize is
522 * "a filesystem-specific preferred I/O block size for this
523 * object. In some filesystem types, this may vary from file
524 * to file"
525 * Default to PAGE_SIZE after much discussion.
526 */
527
528 if (vap->va_type == VREG) {
529 sb->st_blksize = vap->va_blocksize;
530 } else if (vn_isdisk(vp, NULL)) {
531 sb->st_blksize = vp->v_rdev->si_bsize_best;
532 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
533 sb->st_blksize = vp->v_rdev->si_bsize_phys;
534 if (sb->st_blksize < BLKDEV_IOSIZE)
535 sb->st_blksize = BLKDEV_IOSIZE;
536 } else {
537 sb->st_blksize = PAGE_SIZE;
538 }
539
540 sb->st_flags = vap->va_flags;
dadab5e9 541 if (suser(td))
984263bc
MD
542 sb->st_gen = 0;
543 else
544 sb->st_gen = vap->va_gen;
545
546#if (S_BLKSIZE == 512)
547 /* Optimize this case */
548 sb->st_blocks = vap->va_bytes >> 9;
549#else
550 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
551#endif
552 return (0);
553}
554
555/*
556 * File table vnode ioctl routine.
557 */
558static int
dadab5e9 559vn_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
984263bc 560{
dadab5e9
MD
561 struct vnode *vp = ((struct vnode *)fp->f_data);
562 struct ucred *ucred;
984263bc
MD
563 struct vattr vattr;
564 int error;
565
dadab5e9
MD
566 KKASSERT(td->td_proc != NULL);
567 ucred = td->td_proc->p_ucred;
984263bc 568
dadab5e9 569 switch (vp->v_type) {
984263bc
MD
570 case VREG:
571 case VDIR:
572 if (com == FIONREAD) {
dadab5e9 573 error = VOP_GETATTR(vp, &vattr, ucred, td);
984263bc
MD
574 if (error)
575 return (error);
576 *(int *)data = vattr.va_size - fp->f_offset;
577 return (0);
578 }
579 if (com == FIONBIO || com == FIOASYNC) /* XXX */
580 return (0); /* XXX */
581 /* fall into ... */
984263bc
MD
582 default:
583#if 0
584 return (ENOTTY);
585#endif
586 case VFIFO:
587 case VCHR:
588 case VBLK:
589 if (com == FIODTYPE) {
590 if (vp->v_type != VCHR && vp->v_type != VBLK)
591 return (ENOTTY);
592 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
593 return (0);
594 }
dadab5e9 595 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, td);
984263bc 596 if (error == 0 && com == TIOCSCTTY) {
dadab5e9 597 struct session *sess = td->td_proc->p_session;
984263bc
MD
598
599 /* Do nothing if reassigning same control tty */
dadab5e9 600 if (sess->s_ttyvp == vp)
984263bc
MD
601 return (0);
602
603 /* Get rid of reference to old control tty */
dadab5e9
MD
604 if (sess->s_ttyvp)
605 vrele(sess->s_ttyvp);
984263bc 606
dadab5e9 607 sess->s_ttyvp = vp;
984263bc
MD
608 VREF(vp);
609 }
610 return (error);
611 }
612}
613
614/*
615 * File table vnode poll routine.
616 */
617static int
dadab5e9 618vn_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
984263bc 619{
dadab5e9 620 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, td));
984263bc
MD
621}
622
623/*
624 * Check that the vnode is still valid, and if so
625 * acquire requested lock.
626 */
627int
628#ifndef DEBUG_LOCKS
dadab5e9 629vn_lock(struct vnode *vp, int flags, struct thread *td)
984263bc 630#else
dadab5e9
MD
631debug_vn_lock(struct vnode *vp, int flags, struct thread *td,
632 const char *filename, int line)
984263bc
MD
633#endif
634{
635 int error;
636
637 do {
638 if ((flags & LK_INTERLOCK) == 0)
639 simple_lock(&vp->v_interlock);
640 if ((vp->v_flag & VXLOCK) && vp->v_vxproc != curproc) {
641 vp->v_flag |= VXWANT;
642 simple_unlock(&vp->v_interlock);
643 tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
644 error = ENOENT;
645 } else {
646#if 0
647 /* this can now occur in normal operation */
648 if (vp->v_vxproc != NULL)
649 log(LOG_INFO, "VXLOCK interlock avoided in vn_lock\n");
650#endif
651#ifdef DEBUG_LOCKS
652 vp->filename = filename;
653 vp->line = line;
654#endif
655 error = VOP_LOCK(vp,
dadab5e9 656 flags | LK_NOPAUSE | LK_INTERLOCK, td);
984263bc
MD
657 if (error == 0)
658 return (error);
659 }
660 flags &= ~LK_INTERLOCK;
661 } while (flags & LK_RETRY);
662 return (error);
663}
664
665/*
666 * File table vnode close routine.
667 */
668static int
dadab5e9 669vn_closefile(struct file *fp, struct thread *td)
984263bc 670{
dadab5e9 671 int err;
984263bc
MD
672
673 fp->f_ops = &badfileops;
dadab5e9
MD
674 err = vn_close(((struct vnode *)fp->f_data), fp->f_flag, fp->f_cred, td);
675 return(err);
984263bc
MD
676}
677
678static int
679vn_kqfilter(struct file *fp, struct knote *kn)
680{
681
682 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
683}