kernel NFS - Fix another deadlock in the readdirplus code
[dragonfly.git] / sys / kern / vfs_vnops.c
blobabe40630a447d64cf141cdab238418988809ed57
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.
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.
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.
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.58 2008/06/28 17:59:49 dillon Exp $
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/priv.h>
50 #include <sys/mount.h>
51 #include <sys/nlookup.h>
52 #include <sys/vnode.h>
53 #include <sys/buf.h>
54 #include <sys/filio.h>
55 #include <sys/ttycom.h>
56 #include <sys/conf.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
60 #include <sys/thread2.h>
61 #include <sys/mplock2.h>
63 static int vn_closefile (struct file *fp);
64 static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
65 struct ucred *cred, struct sysmsg *msg);
66 static int vn_read (struct file *fp, struct uio *uio,
67 struct ucred *cred, int flags);
68 static int vn_poll (struct file *fp, int events, struct ucred *cred);
69 static int vn_kqfilter (struct file *fp, struct knote *kn);
70 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
71 static int vn_write (struct file *fp, struct uio *uio,
72 struct ucred *cred, int flags);
74 struct fileops vnode_fileops = {
75 .fo_read = vn_read,
76 .fo_write = vn_write,
77 .fo_ioctl = vn_ioctl,
78 .fo_poll = vn_poll,
79 .fo_kqfilter = vn_kqfilter,
80 .fo_stat = vn_statfile,
81 .fo_close = vn_closefile,
82 .fo_shutdown = nofo_shutdown
86 * Common code for vnode open operations. Check permissions, and call
87 * the VOP_NOPEN or VOP_NCREATE routine.
89 * The caller is responsible for setting up nd with nlookup_init() and
90 * for cleaning it up with nlookup_done(), whether we return an error
91 * or not.
93 * On success nd->nl_open_vp will hold a referenced and, if requested,
94 * locked vnode. A locked vnode is requested via NLC_LOCKVP. If fp
95 * is non-NULL the vnode will be installed in the file pointer.
97 * NOTE: The vnode is referenced just once on return whether or not it
98 * is also installed in the file pointer.
101 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
103 struct vnode *vp;
104 struct ucred *cred = nd->nl_cred;
105 struct vattr vat;
106 struct vattr *vap = &vat;
107 int error;
110 * Certain combinations are illegal
112 if ((fmode & (FWRITE | O_TRUNC)) == O_TRUNC)
113 return(EACCES);
116 * Lookup the path and create or obtain the vnode. After a
117 * successful lookup a locked nd->nl_nch will be returned.
119 * The result of this section should be a locked vnode.
121 * XXX with only a little work we should be able to avoid locking
122 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
124 nd->nl_flags |= NLC_OPEN;
125 if (fmode & O_APPEND)
126 nd->nl_flags |= NLC_APPEND;
127 if (fmode & O_TRUNC)
128 nd->nl_flags |= NLC_TRUNCATE;
129 if (fmode & FREAD)
130 nd->nl_flags |= NLC_READ;
131 if (fmode & FWRITE)
132 nd->nl_flags |= NLC_WRITE;
133 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
134 nd->nl_flags |= NLC_FOLLOW;
136 if (fmode & O_CREAT) {
138 * CONDITIONAL CREATE FILE CASE
140 * Setting NLC_CREATE causes a negative hit to store
141 * the negative hit ncp and not return an error. Then
142 * nc_error or nc_vp may be checked to see if the ncp
143 * represents a negative hit. NLC_CREATE also requires
144 * write permission on the governing directory or EPERM
145 * is returned.
147 nd->nl_flags |= NLC_CREATE;
148 nd->nl_flags |= NLC_REFDVP;
149 bwillinode(1);
150 error = nlookup(nd);
151 } else {
153 * NORMAL OPEN FILE CASE
155 error = nlookup(nd);
158 if (error)
159 return (error);
162 * split case to allow us to re-resolve and retry the ncp in case
163 * we get ESTALE.
165 again:
166 if (fmode & O_CREAT) {
167 if (nd->nl_nch.ncp->nc_vp == NULL) {
168 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
169 return (error);
170 VATTR_NULL(vap);
171 vap->va_type = VREG;
172 vap->va_mode = cmode;
173 if (fmode & O_EXCL)
174 vap->va_vaflags |= VA_EXCLUSIVE;
175 error = VOP_NCREATE(&nd->nl_nch, nd->nl_dvp, &vp,
176 nd->nl_cred, vap);
177 if (error)
178 return (error);
179 fmode &= ~O_TRUNC;
180 /* locked vnode is returned */
181 } else {
182 if (fmode & O_EXCL) {
183 error = EEXIST;
184 } else {
185 error = cache_vget(&nd->nl_nch, cred,
186 LK_EXCLUSIVE, &vp);
188 if (error)
189 return (error);
190 fmode &= ~O_CREAT;
192 } else {
193 error = cache_vget(&nd->nl_nch, cred, LK_EXCLUSIVE, &vp);
194 if (error)
195 return (error);
199 * We have a locked vnode and ncp now. Note that the ncp will
200 * be cleaned up by the caller if nd->nl_nch is left intact.
202 if (vp->v_type == VLNK) {
203 error = EMLINK;
204 goto bad;
206 if (vp->v_type == VSOCK) {
207 error = EOPNOTSUPP;
208 goto bad;
210 if ((fmode & O_CREAT) == 0) {
211 if (fmode & (FWRITE | O_TRUNC)) {
212 if (vp->v_type == VDIR) {
213 error = EISDIR;
214 goto bad;
216 error = vn_writechk(vp, &nd->nl_nch);
217 if (error) {
219 * Special stale handling, re-resolve the
220 * vnode.
222 if (error == ESTALE) {
223 vput(vp);
224 vp = NULL;
225 cache_setunresolved(&nd->nl_nch);
226 error = cache_resolve(&nd->nl_nch, cred);
227 if (error == 0)
228 goto again;
230 goto bad;
234 if (fmode & O_TRUNC) {
235 vn_unlock(vp); /* XXX */
236 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
237 VATTR_NULL(vap);
238 vap->va_size = 0;
239 error = VOP_SETATTR(vp, vap, cred);
240 if (error)
241 goto bad;
245 * Setup the fp so VOP_OPEN can override it. No descriptor has been
246 * associated with the fp yet so we own it clean.
248 * f_nchandle inherits nl_nch. This used to be necessary only for
249 * directories but now we do it unconditionally so f*() ops
250 * such as fchmod() can access the actual namespace that was
251 * used to open the file.
253 if (fp) {
254 if (nd->nl_flags & NLC_APPENDONLY)
255 fmode |= FAPPENDONLY;
256 fp->f_nchandle = nd->nl_nch;
257 cache_zero(&nd->nl_nch);
258 cache_unlock(&fp->f_nchandle);
262 * Get rid of nl_nch. vn_open does not return it (it returns the
263 * vnode or the file pointer). Note: we can't leave nl_nch locked
264 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
265 * on /dev/ttyd0
267 if (nd->nl_nch.ncp)
268 cache_put(&nd->nl_nch);
270 error = VOP_OPEN(vp, fmode, cred, fp);
271 if (error) {
273 * setting f_ops to &badfileops will prevent the descriptor
274 * code from trying to close and release the vnode, since
275 * the open failed we do not want to call close.
277 if (fp) {
278 fp->f_data = NULL;
279 fp->f_ops = &badfileops;
281 goto bad;
284 #if 0
286 * Assert that VREG files have been setup for vmio.
288 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
289 ("vn_open: regular file was not VMIO enabled!"));
290 #endif
293 * Return the vnode. XXX needs some cleaning up. The vnode is
294 * only returned in the fp == NULL case.
296 if (fp == NULL) {
297 nd->nl_open_vp = vp;
298 nd->nl_vp_fmode = fmode;
299 if ((nd->nl_flags & NLC_LOCKVP) == 0)
300 vn_unlock(vp);
301 } else {
302 vput(vp);
304 return (0);
305 bad:
306 if (vp)
307 vput(vp);
308 return (error);
312 vn_opendisk(const char *devname, int fmode, struct vnode **vpp)
314 struct vnode *vp;
315 int error;
317 if (strncmp(devname, "/dev/", 5) == 0)
318 devname += 5;
319 if ((vp = getsynthvnode(devname)) == NULL) {
320 error = ENODEV;
321 } else {
322 error = VOP_OPEN(vp, fmode, proc0.p_ucred, NULL);
323 vn_unlock(vp);
324 if (error) {
325 vrele(vp);
326 vp = NULL;
329 *vpp = vp;
330 return (error);
334 * Check for write permissions on the specified vnode. nch may be NULL.
337 vn_writechk(struct vnode *vp, struct nchandle *nch)
340 * If there's shared text associated with
341 * the vnode, try to free it up once. If
342 * we fail, we can't allow writing.
344 if (vp->v_flag & VTEXT)
345 return (ETXTBSY);
348 * If the vnode represents a regular file, check the mount
349 * point via the nch. This may be a different mount point
350 * then the one embedded in the vnode (e.g. nullfs).
352 * We can still write to non-regular files (e.g. devices)
353 * via read-only mounts.
355 if (nch && nch->ncp && vp->v_type == VREG)
356 return (ncp_writechk(nch));
357 return (0);
361 * Check whether the underlying mount is read-only. The mount point
362 * referenced by the namecache may be different from the mount point
363 * used by the underlying vnode in the case of NULLFS, so a separate
364 * check is needed.
367 ncp_writechk(struct nchandle *nch)
369 if (nch->mount && (nch->mount->mnt_flag & MNT_RDONLY))
370 return (EROFS);
371 return(0);
375 * Vnode close call
377 * MPSAFE
380 vn_close(struct vnode *vp, int flags)
382 int error;
384 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
385 if (error == 0) {
386 error = VOP_CLOSE(vp, flags);
387 vn_unlock(vp);
389 vrele(vp);
390 return (error);
394 * Sequential heuristic.
396 * MPSAFE (f_seqcount and f_nextoff are allowed to race)
398 static __inline
400 sequential_heuristic(struct uio *uio, struct file *fp)
403 * Sequential heuristic - detect sequential operation
405 * NOTE: SMP: We allow f_seqcount updates to race.
407 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
408 uio->uio_offset == fp->f_nextoff) {
409 int tmpseq = fp->f_seqcount;
411 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
412 if (tmpseq > IO_SEQMAX)
413 tmpseq = IO_SEQMAX;
414 fp->f_seqcount = tmpseq;
415 return(fp->f_seqcount << IO_SEQSHIFT);
419 * Not sequential, quick draw-down of seqcount
421 * NOTE: SMP: We allow f_seqcount updates to race.
423 if (fp->f_seqcount > 1)
424 fp->f_seqcount = 1;
425 else
426 fp->f_seqcount = 0;
427 return(0);
431 * get - lock and return the f_offset field.
432 * set - set and unlock the f_offset field.
434 * These routines serve the dual purpose of serializing access to the
435 * f_offset field (at least on i386) and guaranteeing operational integrity
436 * when multiple read()ers and write()ers are present on the same fp.
438 * MPSAFE
440 static __inline off_t
441 vn_get_fpf_offset(struct file *fp)
443 u_int flags;
444 u_int nflags;
447 * Shortcut critical path.
449 flags = fp->f_flag & ~FOFFSETLOCK;
450 if (atomic_cmpset_int(&fp->f_flag, flags, flags | FOFFSETLOCK))
451 return(fp->f_offset);
454 * The hard way
456 for (;;) {
457 flags = fp->f_flag;
458 if (flags & FOFFSETLOCK) {
459 nflags = flags | FOFFSETWAKE;
460 tsleep_interlock(&fp->f_flag, 0);
461 if (atomic_cmpset_int(&fp->f_flag, flags, nflags))
462 tsleep(&fp->f_flag, PINTERLOCKED, "fpoff", 0);
463 } else {
464 nflags = flags | FOFFSETLOCK;
465 if (atomic_cmpset_int(&fp->f_flag, flags, nflags))
466 break;
469 return(fp->f_offset);
473 * MPSAFE
475 static __inline void
476 vn_set_fpf_offset(struct file *fp, off_t offset)
478 u_int flags;
479 u_int nflags;
482 * We hold the lock so we can set the offset without interference.
484 fp->f_offset = offset;
487 * Normal release is already a reasonably critical path.
489 for (;;) {
490 flags = fp->f_flag;
491 nflags = flags & ~(FOFFSETLOCK | FOFFSETWAKE);
492 if (atomic_cmpset_int(&fp->f_flag, flags, nflags)) {
493 if (flags & FOFFSETWAKE)
494 wakeup(&fp->f_flag);
495 break;
501 * MPSAFE
503 static __inline off_t
504 vn_poll_fpf_offset(struct file *fp)
506 #if defined(__x86_64__) || !defined(SMP)
507 return(fp->f_offset);
508 #else
509 off_t off = vn_get_fpf_offset(fp);
510 vn_set_fpf_offset(fp, off);
511 return(off);
512 #endif
516 * Package up an I/O request on a vnode into a uio and do it.
518 * MPSAFE
521 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
522 off_t offset, enum uio_seg segflg, int ioflg,
523 struct ucred *cred, int *aresid)
525 struct uio auio;
526 struct iovec aiov;
527 struct ccms_lock ccms_lock;
528 int error;
530 if ((ioflg & IO_NODELOCKED) == 0)
531 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
532 auio.uio_iov = &aiov;
533 auio.uio_iovcnt = 1;
534 aiov.iov_base = base;
535 aiov.iov_len = len;
536 auio.uio_resid = len;
537 auio.uio_offset = offset;
538 auio.uio_segflg = segflg;
539 auio.uio_rw = rw;
540 auio.uio_td = curthread;
541 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, &auio);
542 if (rw == UIO_READ) {
543 error = VOP_READ(vp, &auio, ioflg, cred);
544 } else {
545 error = VOP_WRITE(vp, &auio, ioflg, cred);
547 ccms_lock_put(&vp->v_ccms, &ccms_lock);
548 if (aresid)
549 *aresid = auio.uio_resid;
550 else
551 if (auio.uio_resid && error == 0)
552 error = EIO;
553 if ((ioflg & IO_NODELOCKED) == 0)
554 vn_unlock(vp);
555 return (error);
559 * Package up an I/O request on a vnode into a uio and do it. The I/O
560 * request is split up into smaller chunks and we try to avoid saturating
561 * the buffer cache while potentially holding a vnode locked, so we
562 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
563 * to give other processes a chance to lock the vnode (either other processes
564 * core'ing the same binary, or unrelated processes scanning the directory).
566 * MPSAFE
569 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
570 off_t offset, enum uio_seg segflg, int ioflg,
571 struct ucred *cred, int *aresid)
573 int error = 0;
575 do {
576 int chunk;
579 * Force `offset' to a multiple of MAXBSIZE except possibly
580 * for the first chunk, so that filesystems only need to
581 * write full blocks except possibly for the first and last
582 * chunks.
584 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
586 if (chunk > len)
587 chunk = len;
588 if (vp->v_type == VREG) {
589 switch(rw) {
590 case UIO_READ:
591 bwillread(chunk);
592 break;
593 case UIO_WRITE:
594 bwillwrite(chunk);
595 break;
598 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
599 ioflg, cred, aresid);
600 len -= chunk; /* aresid calc already includes length */
601 if (error)
602 break;
603 offset += chunk;
604 base += chunk;
605 uio_yield();
606 } while (len);
607 if (aresid)
608 *aresid += len;
609 return (error);
613 * File pointers can no longer get ripped up by revoke so
614 * we don't need to lock access to the vp.
616 * f_offset updates are not guaranteed against multiple readers
618 * MPSAFE
620 static int
621 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
623 struct ccms_lock ccms_lock;
624 struct vnode *vp;
625 int error, ioflag;
627 KASSERT(uio->uio_td == curthread,
628 ("uio_td %p is not td %p", uio->uio_td, curthread));
629 vp = (struct vnode *)fp->f_data;
631 ioflag = 0;
632 if (flags & O_FBLOCKING) {
633 /* ioflag &= ~IO_NDELAY; */
634 } else if (flags & O_FNONBLOCKING) {
635 ioflag |= IO_NDELAY;
636 } else if (fp->f_flag & FNONBLOCK) {
637 ioflag |= IO_NDELAY;
639 if (flags & O_FBUFFERED) {
640 /* ioflag &= ~IO_DIRECT; */
641 } else if (flags & O_FUNBUFFERED) {
642 ioflag |= IO_DIRECT;
643 } else if (fp->f_flag & O_DIRECT) {
644 ioflag |= IO_DIRECT;
646 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
647 uio->uio_offset = vn_get_fpf_offset(fp);
648 vn_lock(vp, LK_SHARED | LK_RETRY);
649 ioflag |= sequential_heuristic(uio, fp);
651 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
652 error = VOP_READ(vp, uio, ioflag, cred);
653 ccms_lock_put(&vp->v_ccms, &ccms_lock);
654 fp->f_nextoff = uio->uio_offset;
655 vn_unlock(vp);
656 if ((flags & O_FOFFSET) == 0 && (vp->v_flag & VNOTSEEKABLE) == 0)
657 vn_set_fpf_offset(fp, uio->uio_offset);
658 return (error);
662 * MPSAFE
664 static int
665 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
667 struct ccms_lock ccms_lock;
668 struct vnode *vp;
669 int error, ioflag;
671 KASSERT(uio->uio_td == curthread,
672 ("uio_td %p is not p %p", uio->uio_td, curthread));
673 vp = (struct vnode *)fp->f_data;
675 ioflag = IO_UNIT;
676 if (vp->v_type == VREG &&
677 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
678 ioflag |= IO_APPEND;
681 if (flags & O_FBLOCKING) {
682 /* ioflag &= ~IO_NDELAY; */
683 } else if (flags & O_FNONBLOCKING) {
684 ioflag |= IO_NDELAY;
685 } else if (fp->f_flag & FNONBLOCK) {
686 ioflag |= IO_NDELAY;
688 if (flags & O_FBUFFERED) {
689 /* ioflag &= ~IO_DIRECT; */
690 } else if (flags & O_FUNBUFFERED) {
691 ioflag |= IO_DIRECT;
692 } else if (fp->f_flag & O_DIRECT) {
693 ioflag |= IO_DIRECT;
695 if (flags & O_FASYNCWRITE) {
696 /* ioflag &= ~IO_SYNC; */
697 } else if (flags & O_FSYNCWRITE) {
698 ioflag |= IO_SYNC;
699 } else if (fp->f_flag & O_FSYNC) {
700 ioflag |= IO_SYNC;
703 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
704 ioflag |= IO_SYNC;
705 if ((flags & O_FOFFSET) == 0)
706 uio->uio_offset = vn_get_fpf_offset(fp);
707 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
708 ioflag |= sequential_heuristic(uio, fp);
709 ccms_lock_get_uio(&vp->v_ccms, &ccms_lock, uio);
710 error = VOP_WRITE(vp, uio, ioflag, cred);
711 ccms_lock_put(&vp->v_ccms, &ccms_lock);
712 fp->f_nextoff = uio->uio_offset;
713 vn_unlock(vp);
714 if ((flags & O_FOFFSET) == 0)
715 vn_set_fpf_offset(fp, uio->uio_offset);
716 return (error);
720 * MPSAFE
722 static int
723 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
725 struct vnode *vp;
726 int error;
728 vp = (struct vnode *)fp->f_data;
729 error = vn_stat(vp, sb, cred);
730 return (error);
734 * MPSAFE
737 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
739 struct vattr vattr;
740 struct vattr *vap;
741 int error;
742 u_short mode;
743 cdev_t dev;
745 vap = &vattr;
746 error = VOP_GETATTR(vp, vap);
747 if (error)
748 return (error);
751 * Zero the spare stat fields
753 sb->st_lspare = 0;
754 sb->st_qspare1 = 0;
755 sb->st_qspare2 = 0;
758 * Copy from vattr table
760 if (vap->va_fsid != VNOVAL)
761 sb->st_dev = vap->va_fsid;
762 else
763 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
764 sb->st_ino = vap->va_fileid;
765 mode = vap->va_mode;
766 switch (vap->va_type) {
767 case VREG:
768 mode |= S_IFREG;
769 break;
770 case VDATABASE:
771 mode |= S_IFDB;
772 break;
773 case VDIR:
774 mode |= S_IFDIR;
775 break;
776 case VBLK:
777 mode |= S_IFBLK;
778 break;
779 case VCHR:
780 mode |= S_IFCHR;
781 break;
782 case VLNK:
783 mode |= S_IFLNK;
784 /* This is a cosmetic change, symlinks do not have a mode. */
785 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
786 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
787 else
788 sb->st_mode |= ACCESSPERMS; /* 0777 */
789 break;
790 case VSOCK:
791 mode |= S_IFSOCK;
792 break;
793 case VFIFO:
794 mode |= S_IFIFO;
795 break;
796 default:
797 return (EBADF);
799 sb->st_mode = mode;
800 if (vap->va_nlink > (nlink_t)-1)
801 sb->st_nlink = (nlink_t)-1;
802 else
803 sb->st_nlink = vap->va_nlink;
804 sb->st_uid = vap->va_uid;
805 sb->st_gid = vap->va_gid;
806 sb->st_rdev = dev2udev(vp->v_rdev);
807 sb->st_size = vap->va_size;
808 sb->st_atimespec = vap->va_atime;
809 sb->st_mtimespec = vap->va_mtime;
810 sb->st_ctimespec = vap->va_ctime;
813 * A VCHR and VBLK device may track the last access and last modified
814 * time independantly of the filesystem. This is particularly true
815 * because device read and write calls may bypass the filesystem.
817 if (vp->v_type == VCHR || vp->v_type == VBLK) {
818 dev = vp->v_rdev;
819 if (dev != NULL) {
820 if (dev->si_lastread) {
821 sb->st_atimespec.tv_sec = dev->si_lastread;
822 sb->st_atimespec.tv_nsec = 0;
824 if (dev->si_lastwrite) {
825 sb->st_atimespec.tv_sec = dev->si_lastwrite;
826 sb->st_atimespec.tv_nsec = 0;
832 * According to www.opengroup.org, the meaning of st_blksize is
833 * "a filesystem-specific preferred I/O block size for this
834 * object. In some filesystem types, this may vary from file
835 * to file"
836 * Default to PAGE_SIZE after much discussion.
839 if (vap->va_type == VREG) {
840 sb->st_blksize = vap->va_blocksize;
841 } else if (vn_isdisk(vp, NULL)) {
843 * XXX this is broken. If the device is not yet open (aka
844 * stat() call, aka v_rdev == NULL), how are we supposed
845 * to get a valid block size out of it?
847 dev = vp->v_rdev;
849 sb->st_blksize = dev->si_bsize_best;
850 if (sb->st_blksize < dev->si_bsize_phys)
851 sb->st_blksize = dev->si_bsize_phys;
852 if (sb->st_blksize < BLKDEV_IOSIZE)
853 sb->st_blksize = BLKDEV_IOSIZE;
854 } else {
855 sb->st_blksize = PAGE_SIZE;
858 sb->st_flags = vap->va_flags;
860 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
861 if (error)
862 sb->st_gen = 0;
863 else
864 sb->st_gen = (u_int32_t)vap->va_gen;
866 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
867 return (0);
871 * MPALMOSTSAFE - acquires mplock
873 static int
874 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred,
875 struct sysmsg *msg)
877 struct vnode *vp = ((struct vnode *)fp->f_data);
878 struct vnode *ovp;
879 struct vattr vattr;
880 int error;
881 off_t size;
883 switch (vp->v_type) {
884 case VREG:
885 case VDIR:
886 if (com == FIONREAD) {
887 error = VOP_GETATTR(vp, &vattr);
888 if (error)
889 break;
890 size = vattr.va_size;
891 if ((vp->v_flag & VNOTSEEKABLE) == 0)
892 size -= vn_poll_fpf_offset(fp);
893 if (size > 0x7FFFFFFF)
894 size = 0x7FFFFFFF;
895 *(int *)data = size;
896 error = 0;
897 break;
899 if (com == FIOASYNC) { /* XXX */
900 error = 0; /* XXX */
901 break;
903 /* fall into ... */
904 default:
905 #if 0
906 return (ENOTTY);
907 #endif
908 case VFIFO:
909 case VCHR:
910 case VBLK:
911 if (com == FIODTYPE) {
912 if (vp->v_type != VCHR && vp->v_type != VBLK) {
913 error = ENOTTY;
914 break;
916 *(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
917 error = 0;
918 break;
920 error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred, msg);
921 if (error == 0 && com == TIOCSCTTY) {
922 struct proc *p = curthread->td_proc;
923 struct session *sess;
925 if (p == NULL) {
926 error = ENOTTY;
927 break;
930 get_mplock();
931 sess = p->p_session;
932 /* Do nothing if reassigning same control tty */
933 if (sess->s_ttyvp == vp) {
934 error = 0;
935 rel_mplock();
936 break;
939 /* Get rid of reference to old control tty */
940 ovp = sess->s_ttyvp;
941 vref(vp);
942 sess->s_ttyvp = vp;
943 if (ovp)
944 vrele(ovp);
945 rel_mplock();
947 break;
949 return (error);
953 * MPSAFE
955 static int
956 vn_poll(struct file *fp, int events, struct ucred *cred)
958 int error;
960 error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
961 return (error);
965 * Check that the vnode is still valid, and if so
966 * acquire requested lock.
969 #ifndef DEBUG_LOCKS
970 vn_lock(struct vnode *vp, int flags)
971 #else
972 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
973 #endif
975 int error;
977 do {
978 #ifdef DEBUG_LOCKS
979 vp->filename = filename;
980 vp->line = line;
981 error = debuglockmgr(&vp->v_lock, flags,
982 "vn_lock", filename, line);
983 #else
984 error = lockmgr(&vp->v_lock, flags);
985 #endif
986 if (error == 0)
987 break;
988 } while (flags & LK_RETRY);
991 * Because we (had better!) have a ref on the vnode, once it
992 * goes to VRECLAIMED state it will not be recycled until all
993 * refs go away. So we can just check the flag.
995 if (error == 0 && (vp->v_flag & VRECLAIMED)) {
996 lockmgr(&vp->v_lock, LK_RELEASE);
997 error = ENOENT;
999 return (error);
1003 * MPSAFE
1005 void
1006 vn_unlock(struct vnode *vp)
1008 lockmgr(&vp->v_lock, LK_RELEASE);
1012 * MPSAFE
1015 vn_islocked(struct vnode *vp)
1017 return (lockstatus(&vp->v_lock, curthread));
1021 * Return the lock status of a vnode and unlock the vnode
1022 * if we owned the lock. This is not a boolean, if the
1023 * caller cares what the lock status is the caller must
1024 * check the various possible values.
1026 * This only unlocks exclusive locks held by the caller,
1027 * it will NOT unlock shared locks (there is no way to
1028 * tell who the shared lock belongs to).
1030 * MPSAFE
1033 vn_islocked_unlock(struct vnode *vp)
1035 int vpls;
1037 vpls = lockstatus(&vp->v_lock, curthread);
1038 if (vpls == LK_EXCLUSIVE)
1039 lockmgr(&vp->v_lock, LK_RELEASE);
1040 return(vpls);
1044 * Restore a vnode lock that we previously released via
1045 * vn_islocked_unlock(). This is a NOP if we did not
1046 * own the original lock.
1048 * MPSAFE
1050 void
1051 vn_islocked_relock(struct vnode *vp, int vpls)
1053 int error;
1055 if (vpls == LK_EXCLUSIVE)
1056 error = lockmgr(&vp->v_lock, vpls);
1060 * MPSAFE
1062 static int
1063 vn_closefile(struct file *fp)
1065 int error;
1067 fp->f_ops = &badfileops;
1068 error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
1069 return (error);
1073 * MPSAFE
1075 static int
1076 vn_kqfilter(struct file *fp, struct knote *kn)
1078 int error;
1080 error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);
1081 return (error);