Fix a race with the clearing of p->p_session->s_ttyvp. NULL the pointer
[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 $
1fbb5fc0 40 * $DragonFly: src/sys/kern/vfs_vnops.c,v 1.22 2004/06/15 00:30:53 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
402ed7e1
RG
58static int vn_closefile (struct file *fp, struct thread *td);
59static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
60 struct thread *td);
61static int vn_read (struct file *fp, struct uio *uio,
62 struct ucred *cred, int flags, struct thread *td);
63static int vn_poll (struct file *fp, int events, struct ucred *cred,
64 struct thread *td);
65static int vn_kqfilter (struct file *fp, struct knote *kn);
66static int vn_statfile (struct file *fp, struct stat *sb, struct thread *td);
67static int vn_write (struct file *fp, struct uio *uio,
68 struct ucred *cred, int flags, struct thread *td);
984263bc
MD
69
70struct fileops vnops = {
f53ede20 71 NULL, /* port */
455fcd7e 72 NULL, /* clone */
984263bc
MD
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 */
84int
85vn_open(ndp, fmode, cmode)
1fd87d54 86 struct nameidata *ndp;
984263bc
MD
87 int fmode, cmode;
88{
1fd87d54
RG
89 struct vnode *vp;
90 struct thread *td = ndp->ni_cnd.cn_td;
91 struct ucred *cred = ndp->ni_cnd.cn_cred;
984263bc
MD
92 struct vattr vat;
93 struct vattr *vap = &vat;
94 int mode, error;
95
dadab5e9
MD
96 KKASSERT(cred == td->td_proc->p_ucred);
97
984263bc 98 if (fmode & O_CREAT) {
2b69e610
MD
99 ndp->ni_cnd.cn_nameiop = NAMEI_CREATE;
100 ndp->ni_cnd.cn_flags = CNP_LOCKPARENT | CNP_LOCKLEAF;
984263bc 101 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
2b69e610 102 ndp->ni_cnd.cn_flags |= CNP_FOLLOW;
984263bc
MD
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;
dadab5e9 113 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
bc0c094e 114 error = VOP_CREATE(ndp->ni_dvp, NCPNULL, &ndp->ni_vp,
984263bc
MD
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 {
2b69e610
MD
140 ndp->ni_cnd.cn_nameiop = NAMEI_LOOKUP;
141 ndp->ni_cnd.cn_flags = CNP_LOCKLEAF |
6fc0e645 142 ((fmode & O_NOFOLLOW) ? 0 : CNP_FOLLOW);
984263bc
MD
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) {
dadab5e9 171 error = VOP_ACCESS(vp, mode, cred, td);
984263bc
MD
172 if (error)
173 goto bad;
174 }
175 }
176 if (fmode & O_TRUNC) {
41a01a4d 177 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
dadab5e9 178 VOP_LEASE(vp, td, cred, LEASE_WRITE);
41a01a4d 179 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
984263bc
MD
180 VATTR_NULL(vap);
181 vap->va_size = 0;
dadab5e9 182 error = VOP_SETATTR(vp, vap, cred, td);
984263bc
MD
183 if (error)
184 goto bad;
185 }
dadab5e9 186 error = VOP_OPEN(vp, fmode, cred, td);
984263bc
MD
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) {
3b568787 193 if ((error = vfs_object_create(vp, td)) != 0)
984263bc
MD
194 goto bad;
195 }
196
197 if (fmode & FWRITE)
198 vp->v_writecount++;
199 return (0);
200bad:
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 */
210int
211vn_writechk(vp)
1fd87d54 212 struct vnode *vp;
984263bc
MD
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 */
228int
3b568787 229vn_close(struct vnode *vp, int flags, struct thread *td)
984263bc
MD
230{
231 int error;
232
233 if (flags & FWRITE)
234 vp->v_writecount--;
3b568787 235 error = VOP_CLOSE(vp, flags, td);
984263bc
MD
236 vrele(vp);
237 return (error);
238}
239
240static __inline
241int
242sequential_heuristic(struct uio *uio, struct file *fp)
243{
244 /*
245 * Sequential heuristic - detect sequential operation
246 */
247 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
248 uio->uio_offset == fp->f_nextoff) {
249 int tmpseq = fp->f_seqcount;
250 /*
251 * XXX we assume that the filesystem block size is
252 * the default. Not true, but still gives us a pretty
253 * good indicator of how sequential the read operations
254 * are.
255 */
256 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
257 if (tmpseq > IO_SEQMAX)
258 tmpseq = IO_SEQMAX;
259 fp->f_seqcount = tmpseq;
260 return(fp->f_seqcount << IO_SEQSHIFT);
261 }
262
263 /*
264 * Not sequential, quick draw-down of seqcount
265 */
266 if (fp->f_seqcount > 1)
267 fp->f_seqcount = 1;
268 else
269 fp->f_seqcount = 0;
270 return(0);
271}
272
273/*
274 * Package up an I/O request on a vnode into a uio and do it.
6bdbb368
DR
275 *
276 * We are going to assume the caller has done the appropriate
277 * VOP_LEASE() call before calling vn_rdwr()
984263bc
MD
278 */
279int
dadab5e9 280vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
984263bc
MD
281 enum uio_rw rw;
282 struct vnode *vp;
283 caddr_t base;
284 int len;
285 off_t offset;
286 enum uio_seg segflg;
287 int ioflg;
288 struct ucred *cred;
289 int *aresid;
dadab5e9 290 struct thread *td;
984263bc
MD
291{
292 struct uio auio;
293 struct iovec aiov;
294 int error;
295
296 if ((ioflg & IO_NODELOCKED) == 0)
41a01a4d 297 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
298 auio.uio_iov = &aiov;
299 auio.uio_iovcnt = 1;
300 aiov.iov_base = base;
301 aiov.iov_len = len;
302 auio.uio_resid = len;
303 auio.uio_offset = offset;
304 auio.uio_segflg = segflg;
305 auio.uio_rw = rw;
dadab5e9 306 auio.uio_td = td;
984263bc
MD
307 if (rw == UIO_READ) {
308 error = VOP_READ(vp, &auio, ioflg, cred);
309 } else {
310 error = VOP_WRITE(vp, &auio, ioflg, cred);
311 }
312 if (aresid)
313 *aresid = auio.uio_resid;
314 else
315 if (auio.uio_resid && error == 0)
316 error = EIO;
317 if ((ioflg & IO_NODELOCKED) == 0)
41a01a4d 318 VOP_UNLOCK(vp, NULL, 0, td);
984263bc
MD
319 return (error);
320}
321
322/*
323 * Package up an I/O request on a vnode into a uio and do it. The I/O
324 * request is split up into smaller chunks and we try to avoid saturating
325 * the buffer cache while potentially holding a vnode locked, so we
326 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
327 * to give other processes a chance to lock the vnode (either other processes
328 * core'ing the same binary, or unrelated processes scanning the directory).
329 */
330int
dadab5e9 331vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, td)
984263bc
MD
332 enum uio_rw rw;
333 struct vnode *vp;
334 caddr_t base;
335 int len;
336 off_t offset;
337 enum uio_seg segflg;
338 int ioflg;
339 struct ucred *cred;
340 int *aresid;
dadab5e9 341 struct thread *td;
984263bc
MD
342{
343 int error = 0;
344
345 do {
9a0222ac 346 int chunk;
984263bc 347
9a0222ac
DR
348 /*
349 * Force `offset' to a multiple of MAXBSIZE except possibly
350 * for the first chunk, so that filesystems only need to
351 * write full blocks except possibly for the first and last
352 * chunks.
353 */
354 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
355
356 if (chunk > len)
357 chunk = len;
984263bc
MD
358 if (rw != UIO_READ && vp->v_type == VREG)
359 bwillwrite();
360 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
dadab5e9 361 ioflg, cred, aresid, td);
984263bc
MD
362 len -= chunk; /* aresid calc already includes length */
363 if (error)
364 break;
365 offset += chunk;
366 base += chunk;
367 uio_yield();
368 } while (len);
369 if (aresid)
370 *aresid += len;
371 return (error);
372}
373
374/*
375 * File table vnode read routine.
376 */
377static int
dadab5e9 378vn_read(fp, uio, cred, flags, td)
984263bc
MD
379 struct file *fp;
380 struct uio *uio;
381 struct ucred *cred;
dadab5e9 382 struct thread *td;
984263bc
MD
383 int flags;
384{
385 struct vnode *vp;
386 int error, ioflag;
387
dadab5e9 388 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
984263bc
MD
389 vp = (struct vnode *)fp->f_data;
390 ioflag = 0;
391 if (fp->f_flag & FNONBLOCK)
392 ioflag |= IO_NDELAY;
393 if (fp->f_flag & O_DIRECT)
394 ioflag |= IO_DIRECT;
dadab5e9 395 VOP_LEASE(vp, td, cred, LEASE_READ);
41a01a4d 396 vn_lock(vp, NULL, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
984263bc
MD
397 if ((flags & FOF_OFFSET) == 0)
398 uio->uio_offset = fp->f_offset;
399
400 ioflag |= sequential_heuristic(uio, fp);
401
402 error = VOP_READ(vp, uio, ioflag, cred);
403 if ((flags & FOF_OFFSET) == 0)
404 fp->f_offset = uio->uio_offset;
405 fp->f_nextoff = uio->uio_offset;
41a01a4d 406 VOP_UNLOCK(vp, NULL, 0, td);
984263bc
MD
407 return (error);
408}
409
410/*
411 * File table vnode write routine.
412 */
413static int
dadab5e9 414vn_write(fp, uio, cred, flags, td)
984263bc
MD
415 struct file *fp;
416 struct uio *uio;
417 struct ucred *cred;
dadab5e9 418 struct thread *td;
984263bc
MD
419 int flags;
420{
421 struct vnode *vp;
422 int error, ioflag;
423
dadab5e9
MD
424 KASSERT(uio->uio_td == td, ("uio_procp %p is not p %p",
425 uio->uio_td, td));
984263bc
MD
426 vp = (struct vnode *)fp->f_data;
427 if (vp->v_type == VREG)
428 bwillwrite();
429 vp = (struct vnode *)fp->f_data; /* XXX needed? */
430 ioflag = IO_UNIT;
431 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
432 ioflag |= IO_APPEND;
433 if (fp->f_flag & FNONBLOCK)
434 ioflag |= IO_NDELAY;
435 if (fp->f_flag & O_DIRECT)
436 ioflag |= IO_DIRECT;
437 if ((fp->f_flag & O_FSYNC) ||
438 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
439 ioflag |= IO_SYNC;
dadab5e9 440 VOP_LEASE(vp, td, cred, LEASE_WRITE);
41a01a4d 441 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
442 if ((flags & FOF_OFFSET) == 0)
443 uio->uio_offset = fp->f_offset;
444 ioflag |= sequential_heuristic(uio, fp);
445 error = VOP_WRITE(vp, uio, ioflag, cred);
446 if ((flags & FOF_OFFSET) == 0)
447 fp->f_offset = uio->uio_offset;
448 fp->f_nextoff = uio->uio_offset;
41a01a4d 449 VOP_UNLOCK(vp, NULL, 0, td);
984263bc
MD
450 return (error);
451}
452
453/*
454 * File table vnode stat routine.
455 */
456static int
dadab5e9 457vn_statfile(struct file *fp, struct stat *sb, struct thread *td)
984263bc
MD
458{
459 struct vnode *vp = (struct vnode *)fp->f_data;
460
dadab5e9 461 return vn_stat(vp, sb, td);
984263bc
MD
462}
463
464int
dadab5e9 465vn_stat(struct vnode *vp, struct stat *sb, struct thread *td)
984263bc
MD
466{
467 struct vattr vattr;
dadab5e9 468 struct vattr *vap;
984263bc
MD
469 int error;
470 u_short mode;
471
472 vap = &vattr;
3b568787 473 error = VOP_GETATTR(vp, vap, td);
984263bc
MD
474 if (error)
475 return (error);
476
477 /*
478 * Zero the spare stat fields
479 */
480 sb->st_lspare = 0;
481 sb->st_qspare[0] = 0;
482 sb->st_qspare[1] = 0;
483
484 /*
485 * Copy from vattr table
486 */
487 if (vap->va_fsid != VNOVAL)
488 sb->st_dev = vap->va_fsid;
489 else
490 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
491 sb->st_ino = vap->va_fileid;
492 mode = vap->va_mode;
493 switch (vap->va_type) {
494 case VREG:
495 mode |= S_IFREG;
496 break;
497 case VDIR:
498 mode |= S_IFDIR;
499 break;
500 case VBLK:
501 mode |= S_IFBLK;
502 break;
503 case VCHR:
504 mode |= S_IFCHR;
505 break;
506 case VLNK:
507 mode |= S_IFLNK;
508 /* This is a cosmetic change, symlinks do not have a mode. */
509 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
510 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
511 else
512 sb->st_mode |= ACCESSPERMS; /* 0777 */
513 break;
514 case VSOCK:
515 mode |= S_IFSOCK;
516 break;
517 case VFIFO:
518 mode |= S_IFIFO;
519 break;
520 default:
521 return (EBADF);
522 };
523 sb->st_mode = mode;
524 sb->st_nlink = vap->va_nlink;
525 sb->st_uid = vap->va_uid;
526 sb->st_gid = vap->va_gid;
527 sb->st_rdev = vap->va_rdev;
528 sb->st_size = vap->va_size;
529 sb->st_atimespec = vap->va_atime;
530 sb->st_mtimespec = vap->va_mtime;
531 sb->st_ctimespec = vap->va_ctime;
532
533 /*
534 * According to www.opengroup.org, the meaning of st_blksize is
535 * "a filesystem-specific preferred I/O block size for this
536 * object. In some filesystem types, this may vary from file
537 * to file"
538 * Default to PAGE_SIZE after much discussion.
539 */
540
541 if (vap->va_type == VREG) {
542 sb->st_blksize = vap->va_blocksize;
543 } else if (vn_isdisk(vp, NULL)) {
e4c9c0c8
MD
544 /*
545 * XXX this is broken. If the device is not yet open (aka
546 * stat() call, aka v_rdev == NULL), how are we supposed
547 * to get a valid block size out of it?
548 */
549 dev_t dev;
550
551 if ((dev = vp->v_rdev) == NULL)
552 dev = udev2dev(vp->v_udev, vp->v_type == VBLK);
553 sb->st_blksize = dev->si_bsize_best;
554 if (sb->st_blksize < dev->si_bsize_phys)
555 sb->st_blksize = dev->si_bsize_phys;
984263bc
MD
556 if (sb->st_blksize < BLKDEV_IOSIZE)
557 sb->st_blksize = BLKDEV_IOSIZE;
558 } else {
559 sb->st_blksize = PAGE_SIZE;
560 }
561
562 sb->st_flags = vap->va_flags;
dadab5e9 563 if (suser(td))
984263bc
MD
564 sb->st_gen = 0;
565 else
566 sb->st_gen = vap->va_gen;
567
568#if (S_BLKSIZE == 512)
569 /* Optimize this case */
570 sb->st_blocks = vap->va_bytes >> 9;
571#else
572 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
573#endif
574 return (0);
575}
576
577/*
578 * File table vnode ioctl routine.
579 */
580static int
dadab5e9 581vn_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
984263bc 582{
dadab5e9 583 struct vnode *vp = ((struct vnode *)fp->f_data);
1fbb5fc0 584 struct vnode *ovp;
dadab5e9 585 struct ucred *ucred;
984263bc
MD
586 struct vattr vattr;
587 int error;
588
dadab5e9
MD
589 KKASSERT(td->td_proc != NULL);
590 ucred = td->td_proc->p_ucred;
984263bc 591
dadab5e9 592 switch (vp->v_type) {
984263bc
MD
593 case VREG:
594 case VDIR:
595 if (com == FIONREAD) {
3b568787 596 error = VOP_GETATTR(vp, &vattr, td);
984263bc
MD
597 if (error)
598 return (error);
599 *(int *)data = vattr.va_size - fp->f_offset;
600 return (0);
601 }
602 if (com == FIONBIO || com == FIOASYNC) /* XXX */
603 return (0); /* XXX */
604 /* fall into ... */
984263bc
MD
605 default:
606#if 0
607 return (ENOTTY);
608#endif
609 case VFIFO:
610 case VCHR:
611 case VBLK:
612 if (com == FIODTYPE) {
613 if (vp->v_type != VCHR && vp->v_type != VBLK)
614 return (ENOTTY);
335dda38 615 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
984263bc
MD
616 return (0);
617 }
dadab5e9 618 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, td);
984263bc 619 if (error == 0 && com == TIOCSCTTY) {
dadab5e9 620 struct session *sess = td->td_proc->p_session;
984263bc
MD
621
622 /* Do nothing if reassigning same control tty */
dadab5e9 623 if (sess->s_ttyvp == vp)
984263bc
MD
624 return (0);
625
626 /* Get rid of reference to old control tty */
1fbb5fc0 627 ovp = sess->s_ttyvp;
597aea93 628 vref(vp);
1fbb5fc0
MD
629 sess->s_ttyvp = vp;
630 if (ovp)
631 vrele(ovp);
984263bc
MD
632 }
633 return (error);
634 }
635}
636
637/*
638 * File table vnode poll routine.
639 */
640static int
dadab5e9 641vn_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
984263bc 642{
dadab5e9 643 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, td));
984263bc
MD
644}
645
646/*
647 * Check that the vnode is still valid, and if so
648 * acquire requested lock.
649 */
650int
651#ifndef DEBUG_LOCKS
41a01a4d 652vn_lock(struct vnode *vp, lwkt_tokref_t vlock, int flags, struct thread *td)
984263bc 653#else
41a01a4d
MD
654debug_vn_lock(struct vnode *vp, lwkt_tokref_t vlock, int flags,
655 struct thread *td, const char *filename, int line)
984263bc
MD
656#endif
657{
658 int error;
41a01a4d 659 lwkt_tokref vvlock;
984263bc
MD
660
661 do {
41a01a4d
MD
662 if ((flags & LK_INTERLOCK) == 0) {
663 lwkt_gettoken(&vvlock, vp->v_interlock);
664 vlock = &vvlock;
665 }
666 if ((vp->v_flag & VXLOCK) && vp->v_vxthread != curthread) {
984263bc 667 vp->v_flag |= VXWANT;
41a01a4d 668 lwkt_reltoken(vlock);
377d4740 669 tsleep((caddr_t)vp, 0, "vn_lock", 0);
984263bc
MD
670 error = ENOENT;
671 } else {
672#if 0
673 /* this can now occur in normal operation */
41a01a4d 674 if (vp->v_vxthread != NULL)
984263bc
MD
675 log(LOG_INFO, "VXLOCK interlock avoided in vn_lock\n");
676#endif
677#ifdef DEBUG_LOCKS
678 vp->filename = filename;
679 vp->line = line;
680#endif
41a01a4d 681 error = VOP_LOCK(vp, vlock,
dadab5e9 682 flags | LK_NOPAUSE | LK_INTERLOCK, td);
984263bc 683 if (error == 0)
9a0222ac 684 return (0);
984263bc
MD
685 }
686 flags &= ~LK_INTERLOCK;
687 } while (flags & LK_RETRY);
688 return (error);
689}
690
691/*
692 * File table vnode close routine.
693 */
694static int
dadab5e9 695vn_closefile(struct file *fp, struct thread *td)
984263bc 696{
dadab5e9 697 int err;
984263bc
MD
698
699 fp->f_ops = &badfileops;
3b568787 700 err = vn_close(((struct vnode *)fp->f_data), fp->f_flag, td);
dadab5e9 701 return(err);
984263bc
MD
702}
703
704static int
705vn_kqfilter(struct file *fp, struct knote *kn)
706{
707
708 return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
709}