2 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $
34 * tmpfs vnode interface.
37 #include <sys/kernel.h>
38 #include <sys/kern_syscall.h>
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
44 #include <sys/resourcevar.h>
45 #include <sys/sched.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vfsops.h>
50 #include <sys/vnode.h>
51 #include <sys/mountctl.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pageout.h>
58 #include <vm/vm_pager.h>
59 #include <vm/swap_pager.h>
62 #include <vm/vm_page2.h>
64 #include <vfs/fifofs/fifo.h>
65 #include <vfs/tmpfs/tmpfs_vnops.h>
67 #include <vfs/tmpfs/tmpfs.h>
71 static void tmpfs_strategy_done(struct bio *bio);
75 tmpfs_knote(struct vnode *vp, int flags)
78 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
82 /* --------------------------------------------------------------------- */
85 tmpfs_nresolve(struct vop_nresolve_args *v)
87 struct vnode *dvp = v->a_dvp;
88 struct vnode *vp = NULL;
89 struct namecache *ncp = v->a_nch->ncp;
90 struct tmpfs_node *tnode;
94 struct tmpfs_dirent *de;
95 struct tmpfs_node *dnode;
98 lwkt_gettoken(&mp->mnt_token);
100 dnode = VP_TO_TMPFS_DIR(dvp);
102 de = tmpfs_dir_lookup(dnode, NULL, ncp);
107 * Allocate a vnode for the node we found.
110 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
111 LK_EXCLUSIVE | LK_RETRY, &vp);
119 * Store the result of this lookup in the cache. Avoid this if the
120 * request was for creation, as it does not improve timings on
125 cache_setvp(v->a_nch, vp);
127 } else if (error == ENOENT) {
128 cache_setvp(v->a_nch, NULL);
131 lwkt_reltoken(&mp->mnt_token);
136 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
138 struct vnode *dvp = v->a_dvp;
139 struct vnode **vpp = v->a_vpp;
140 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
141 struct ucred *cred = v->a_cred;
148 lwkt_gettoken(&mp->mnt_token);
150 /* Check accessibility of requested node as a first step. */
151 error = VOP_ACCESS(dvp, VEXEC, cred);
153 lwkt_reltoken(&mp->mnt_token);
157 if (dnode->tn_dir.tn_parent != NULL) {
158 /* Allocate a new vnode on the matching entry. */
159 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
160 LK_EXCLUSIVE | LK_RETRY, vpp);
166 lwkt_reltoken(&mp->mnt_token);
168 return (*vpp == NULL) ? ENOENT : 0;
171 /* --------------------------------------------------------------------- */
174 tmpfs_ncreate(struct vop_ncreate_args *v)
176 struct vnode *dvp = v->a_dvp;
177 struct vnode **vpp = v->a_vpp;
178 struct namecache *ncp = v->a_nch->ncp;
179 struct vattr *vap = v->a_vap;
180 struct ucred *cred = v->a_cred;
185 lwkt_gettoken(&mp->mnt_token);
187 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
189 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
191 cache_setunresolved(v->a_nch);
192 cache_setvp(v->a_nch, *vpp);
193 tmpfs_knote(dvp, NOTE_WRITE);
196 lwkt_reltoken(&mp->mnt_token);
200 /* --------------------------------------------------------------------- */
203 tmpfs_nmknod(struct vop_nmknod_args *v)
205 struct vnode *dvp = v->a_dvp;
206 struct vnode **vpp = v->a_vpp;
207 struct namecache *ncp = v->a_nch->ncp;
208 struct vattr *vap = v->a_vap;
209 struct ucred *cred = v->a_cred;
210 struct mount *mp = dvp->v_mount;
213 lwkt_gettoken(&mp->mnt_token);
215 if (vap->va_type != VBLK && vap->va_type != VCHR &&
216 vap->va_type != VFIFO) {
217 lwkt_reltoken(&mp->mnt_token);
221 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
223 cache_setunresolved(v->a_nch);
224 cache_setvp(v->a_nch, *vpp);
225 tmpfs_knote(dvp, NOTE_WRITE);
228 lwkt_reltoken(&mp->mnt_token);
233 /* --------------------------------------------------------------------- */
236 tmpfs_open(struct vop_open_args *v)
238 struct vnode *vp = v->a_vp;
239 int mode = v->a_mode;
240 struct mount *mp = vp->v_mount;
241 struct tmpfs_node *node;
244 lwkt_gettoken(&mp->mnt_token);
245 node = VP_TO_TMPFS_NODE(vp);
248 /* The file is still active but all its names have been removed
249 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
250 * it is about to die. */
251 if (node->tn_links < 1)
255 /* If the file is marked append-only, deny write requests. */
256 if ((node->tn_flags & APPEND) &&
257 (mode & (FWRITE | O_APPEND)) == FWRITE) {
260 error = (vop_stdopen(v));
263 lwkt_reltoken(&mp->mnt_token);
267 /* --------------------------------------------------------------------- */
270 tmpfs_close(struct vop_close_args *v)
272 struct vnode *vp = v->a_vp;
273 struct tmpfs_node *node;
276 lwkt_gettoken(&vp->v_mount->mnt_token);
277 node = VP_TO_TMPFS_NODE(vp);
279 if (node->tn_links > 0) {
281 * Update node times. No need to do it if the node has
282 * been deleted, because it will vanish after we return.
287 error = vop_stdclose(v);
289 lwkt_reltoken(&vp->v_mount->mnt_token);
294 /* --------------------------------------------------------------------- */
297 tmpfs_access(struct vop_access_args *v)
299 struct vnode *vp = v->a_vp;
301 struct tmpfs_node *node;
303 lwkt_gettoken(&vp->v_mount->mnt_token);
304 node = VP_TO_TMPFS_NODE(vp);
306 switch (vp->v_type) {
312 if ((v->a_mode & VWRITE) &&
313 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
333 if ((v->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
338 error = vop_helper_access(v, node->tn_uid, node->tn_gid,
342 lwkt_reltoken(&vp->v_mount->mnt_token);
346 /* --------------------------------------------------------------------- */
349 tmpfs_getattr(struct vop_getattr_args *v)
351 struct vnode *vp = v->a_vp;
352 struct vattr *vap = v->a_vap;
353 struct tmpfs_node *node;
355 lwkt_gettoken(&vp->v_mount->mnt_token);
356 node = VP_TO_TMPFS_NODE(vp);
360 vap->va_type = vp->v_type;
361 vap->va_mode = node->tn_mode;
362 vap->va_nlink = node->tn_links;
363 vap->va_uid = node->tn_uid;
364 vap->va_gid = node->tn_gid;
365 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
366 vap->va_fileid = node->tn_id;
367 vap->va_size = node->tn_size;
368 vap->va_blocksize = PAGE_SIZE;
369 vap->va_atime.tv_sec = node->tn_atime;
370 vap->va_atime.tv_nsec = node->tn_atimensec;
371 vap->va_mtime.tv_sec = node->tn_mtime;
372 vap->va_mtime.tv_nsec = node->tn_mtimensec;
373 vap->va_ctime.tv_sec = node->tn_ctime;
374 vap->va_ctime.tv_nsec = node->tn_ctimensec;
375 vap->va_gen = node->tn_gen;
376 vap->va_flags = node->tn_flags;
377 if (vp->v_type == VBLK || vp->v_type == VCHR)
379 vap->va_rmajor = umajor(node->tn_rdev);
380 vap->va_rminor = uminor(node->tn_rdev);
382 vap->va_bytes = round_page(node->tn_size);
385 lwkt_reltoken(&vp->v_mount->mnt_token);
390 /* --------------------------------------------------------------------- */
393 tmpfs_setattr(struct vop_setattr_args *v)
395 struct vnode *vp = v->a_vp;
396 struct vattr *vap = v->a_vap;
397 struct ucred *cred = v->a_cred;
398 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
402 lwkt_gettoken(&vp->v_mount->mnt_token);
403 if (error == 0 && (vap->va_flags != VNOVAL)) {
404 error = tmpfs_chflags(vp, vap->va_flags, cred);
405 kflags |= NOTE_ATTRIB;
408 if (error == 0 && (vap->va_size != VNOVAL)) {
409 if (vap->va_size > node->tn_size)
410 kflags |= NOTE_WRITE | NOTE_EXTEND;
412 kflags |= NOTE_WRITE;
413 error = tmpfs_chsize(vp, vap->va_size, cred);
416 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
417 vap->va_gid != (gid_t)VNOVAL)) {
418 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
419 kflags |= NOTE_ATTRIB;
422 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
423 error = tmpfs_chmod(vp, vap->va_mode, cred);
424 kflags |= NOTE_ATTRIB;
427 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
428 vap->va_atime.tv_nsec != VNOVAL) ||
429 (vap->va_mtime.tv_sec != VNOVAL &&
430 vap->va_mtime.tv_nsec != VNOVAL) )) {
431 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
432 vap->va_vaflags, cred);
433 kflags |= NOTE_ATTRIB;
436 /* Update the node times. We give preference to the error codes
437 * generated by this function rather than the ones that may arise
438 * from tmpfs_update. */
440 tmpfs_knote(vp, kflags);
442 lwkt_reltoken(&vp->v_mount->mnt_token);
447 /* --------------------------------------------------------------------- */
450 * fsync is usually a NOP, but we must take action when unmounting or
454 tmpfs_fsync(struct vop_fsync_args *v)
456 struct tmpfs_node *node;
457 struct vnode *vp = v->a_vp;
459 lwkt_gettoken(&vp->v_mount->mnt_token);
460 node = VP_TO_TMPFS_NODE(vp);
463 if (vp->v_type == VREG) {
464 if (vp->v_flag & VRECLAIMED) {
465 if (node->tn_links == 0)
466 tmpfs_truncate(vp, 0);
468 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
472 lwkt_reltoken(&vp->v_mount->mnt_token);
476 /* --------------------------------------------------------------------- */
479 tmpfs_read (struct vop_read_args *ap)
482 struct vnode *vp = ap->a_vp;
483 struct uio *uio = ap->a_uio;
484 struct tmpfs_node *node;
494 if (uio->uio_offset < 0)
496 if (vp->v_type != VREG)
500 * Extract node, try to shortcut the operation through
501 * the VM page cache, allowing us to avoid buffer cache
504 node = VP_TO_TMPFS_NODE(vp);
505 resid = uio->uio_resid;
506 error = vop_helper_read_shortcut(ap);
509 if (uio->uio_resid == 0) {
516 * Fall-through to our normal read code.
518 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
520 * Use buffer cache I/O (via tmpfs_strategy)
522 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
523 base_offset = (off_t)uio->uio_offset - offset;
524 bp = getcacheblk(vp, base_offset, TMPFS_BLKSIZE, 0);
526 lwkt_gettoken(&vp->v_mount->mnt_token);
527 error = bread(vp, base_offset, TMPFS_BLKSIZE, &bp);
530 lwkt_reltoken(&vp->v_mount->mnt_token);
531 kprintf("tmpfs_read bread error %d\n", error);
534 lwkt_reltoken(&vp->v_mount->mnt_token);
537 * tmpfs pretty much fiddles directly with the VM
538 * system, don't let it exhaust it or we won't play
539 * nice with other processes.
541 * Only do this if the VOP is coming from a normal
542 * read/write. The VM system handles the case for
545 if (uio->uio_segflg != UIO_NOCOPY)
548 bp->b_flags |= B_CLUSTEROK;
551 * Figure out how many bytes we can actually copy this loop.
553 len = TMPFS_BLKSIZE - offset;
554 if (len > uio->uio_resid)
555 len = uio->uio_resid;
556 if (len > node->tn_size - uio->uio_offset)
557 len = (size_t)(node->tn_size - uio->uio_offset);
559 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
562 kprintf("tmpfs_read uiomove error %d\n", error);
568 TMPFS_NODE_LOCK(node);
569 node->tn_status |= TMPFS_NODE_ACCESSED;
570 TMPFS_NODE_UNLOCK(node);
576 tmpfs_write (struct vop_write_args *ap)
579 struct vnode *vp = ap->a_vp;
580 struct uio *uio = ap->a_uio;
581 struct thread *td = uio->uio_td;
582 struct tmpfs_node *node;
595 if (uio->uio_resid == 0) {
599 node = VP_TO_TMPFS_NODE(vp);
601 if (vp->v_type != VREG)
603 seqcount = ap->a_ioflag >> 16;
605 lwkt_gettoken(&vp->v_mount->mnt_token);
607 oldsize = node->tn_size;
608 if (ap->a_ioflag & IO_APPEND)
609 uio->uio_offset = node->tn_size;
612 * Check for illegal write offsets.
614 if (uio->uio_offset + uio->uio_resid >
615 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) {
616 lwkt_reltoken(&vp->v_mount->mnt_token);
621 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN).
623 if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) {
624 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
626 lwkt_reltoken(&vp->v_mount->mnt_token);
629 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
630 ksignal(td->td_proc, SIGXFSZ);
631 lwkt_reltoken(&vp->v_mount->mnt_token);
638 * Extend the file's size if necessary
640 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
642 while (uio->uio_resid > 0) {
644 * Don't completely blow out running buffer I/O
645 * when being hit from the pageout daemon.
647 if (uio->uio_segflg == UIO_NOCOPY &&
648 (ap->a_ioflag & IO_RECURSE) == 0) {
649 bwillwrite(TMPFS_BLKSIZE);
653 * Use buffer cache I/O (via tmpfs_strategy)
655 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
656 base_offset = (off_t)uio->uio_offset - offset;
657 len = TMPFS_BLKSIZE - offset;
658 if (len > uio->uio_resid)
659 len = uio->uio_resid;
661 if ((uio->uio_offset + len) > node->tn_size) {
662 trivial = (uio->uio_offset <= node->tn_size);
663 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
669 * Read to fill in any gaps. Theoretically we could
670 * optimize this if the write covers the entire buffer
671 * and is not a UIO_NOCOPY write, however this can lead
672 * to a security violation exposing random kernel memory
673 * (whatever junk was in the backing VM pages before).
675 * So just use bread() to do the right thing.
677 error = bread(vp, base_offset, TMPFS_BLKSIZE, &bp);
678 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
680 kprintf("tmpfs_write uiomove error %d\n", error);
685 if (uio->uio_offset > node->tn_size) {
686 node->tn_size = uio->uio_offset;
687 kflags |= NOTE_EXTEND;
689 kflags |= NOTE_WRITE;
692 * Always try to flush the page in the UIO_NOCOPY case. This
693 * can come from the pageout daemon or during vnode eviction.
694 * It is not necessarily going to be marked IO_ASYNC/IO_SYNC.
696 * For the normal case we buwrite(), dirtying the underlying
697 * VM pages instead of dirtying the buffer and releasing the
698 * buffer as a clean buffer. This allows tmpfs to use
699 * essentially all available memory to cache file data.
700 * If we used bdwrite() the buffer cache would wind up
701 * flushing the data to swap too quickly.
703 * But because tmpfs can seriously load the VM system we
704 * fall-back to using bdwrite() when free memory starts
705 * to get low. This shifts the load away from the VM system
706 * and makes tmpfs act more like a normal filesystem with
707 * regards to disk activity.
709 * tmpfs pretty much fiddles directly with the VM
710 * system, don't let it exhaust it or we won't play
711 * nice with other processes. Only do this if the
712 * VOP is coming from a normal read/write. The VM system
713 * handles the case for UIO_NOCOPY.
715 bp->b_flags |= B_CLUSTEROK;
716 if (uio->uio_segflg == UIO_NOCOPY) {
718 * Flush from the pageout daemon, deal with
719 * potentially very heavy tmpfs write activity
720 * causing long stalls in the pageout daemon
721 * before pages get to free/cache.
723 * (a) Under severe pressure setting B_DIRECT will
724 * cause a buffer release to try to free the
727 * (b) Under modest memory pressure the B_RELBUF
728 * alone is sufficient to get the pages moved
729 * to the cache. We could also force this by
730 * setting B_NOTMETA but that might have other
731 * unintended side-effects (e.g. setting
732 * PG_NOTMETA on the VM page).
734 * Hopefully this will unblock the VM system more
735 * quickly under extreme tmpfs write load.
737 if (vm_page_count_min(vm_page_free_hysteresis))
738 bp->b_flags |= B_DIRECT;
739 bp->b_flags |= B_AGE | B_RELBUF;
740 bp->b_act_count = 0; /* buffer->deactivate pgs */
742 } else if (vm_page_count_target()) {
744 * Normal (userland) write but we are low on memory,
745 * run the buffer the buffer cache.
747 bp->b_act_count = 0; /* buffer->deactivate pgs */
751 * Otherwise run the buffer directly through to the
755 /*vm_wait_nominal();*/
759 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
766 (void)tmpfs_reg_resize(vp, oldsize, trivial);
767 kflags &= ~NOTE_EXTEND;
773 * Currently we don't set the mtime on files modified via mmap()
774 * because we can't tell the difference between those modifications
775 * and an attempt by the pageout daemon to flush tmpfs pages to
778 * This is because in order to defer flushes as long as possible
779 * buwrite() works by marking the underlying VM pages dirty in
780 * order to be able to dispose of the buffer cache buffer without
783 TMPFS_NODE_LOCK(node);
784 if (uio->uio_segflg != UIO_NOCOPY)
785 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED;
787 node->tn_status |= TMPFS_NODE_CHANGED;
789 if (node->tn_mode & (S_ISUID | S_ISGID)) {
790 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
791 node->tn_mode &= ~(S_ISUID | S_ISGID);
793 TMPFS_NODE_UNLOCK(node);
796 tmpfs_knote(vp, kflags);
798 lwkt_reltoken(&vp->v_mount->mnt_token);
803 tmpfs_advlock (struct vop_advlock_args *ap)
805 struct tmpfs_node *node;
806 struct vnode *vp = ap->a_vp;
809 lwkt_gettoken(&vp->v_mount->mnt_token);
810 node = VP_TO_TMPFS_NODE(vp);
812 error = (lf_advlock(ap, &node->tn_advlock, node->tn_size));
813 lwkt_reltoken(&vp->v_mount->mnt_token);
819 * The strategy function is typically only called when memory pressure
820 * forces the system to attempt to pageout pages. It can also be called
821 * by [n]vtruncbuf() when a truncation cuts a page in half. Normal write
825 tmpfs_strategy(struct vop_strategy_args *ap)
827 struct bio *bio = ap->a_bio;
829 struct buf *bp = bio->bio_buf;
830 struct vnode *vp = ap->a_vp;
831 struct tmpfs_node *node;
836 if (vp->v_type != VREG) {
837 bp->b_resid = bp->b_bcount;
838 bp->b_flags |= B_ERROR | B_INVAL;
839 bp->b_error = EINVAL;
844 lwkt_gettoken(&vp->v_mount->mnt_token);
845 node = VP_TO_TMPFS_NODE(vp);
847 uobj = node->tn_reg.tn_aobj;
850 * Don't bother flushing to swap if there is no swap, just
851 * ensure that the pages are marked as needing a commit (still).
853 if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) {
854 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
855 m = bp->b_xio.xio_pages[i];
856 vm_page_need_commit(m);
862 nbio = push_bio(bio);
863 nbio->bio_done = tmpfs_strategy_done;
864 nbio->bio_offset = bio->bio_offset;
865 swap_pager_strategy(uobj, nbio);
868 lwkt_reltoken(&vp->v_mount->mnt_token);
873 * If we were unable to commit the pages to swap make sure they are marked
874 * as needing a commit (again). If we were, clear the flag to allow the
878 tmpfs_strategy_done(struct bio *bio)
886 if (bp->b_flags & B_ERROR) {
887 bp->b_flags &= ~B_ERROR;
890 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
891 m = bp->b_xio.xio_pages[i];
892 vm_page_need_commit(m);
895 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
896 m = bp->b_xio.xio_pages[i];
897 vm_page_clear_commit(m);
905 tmpfs_bmap(struct vop_bmap_args *ap)
907 if (ap->a_doffsetp != NULL)
908 *ap->a_doffsetp = ap->a_loffset;
909 if (ap->a_runp != NULL)
911 if (ap->a_runb != NULL)
917 /* --------------------------------------------------------------------- */
920 tmpfs_nremove(struct vop_nremove_args *v)
922 struct vnode *dvp = v->a_dvp;
923 struct namecache *ncp = v->a_nch->ncp;
926 struct tmpfs_dirent *de;
927 struct tmpfs_mount *tmp;
928 struct tmpfs_node *dnode;
929 struct tmpfs_node *node;
934 lwkt_gettoken(&mp->mnt_token);
937 * We have to acquire the vp from v->a_nch because we will likely
938 * unresolve the namecache entry, and a vrele/vput is needed to
939 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
941 * We have to use vget to clear any inactive state on the vnode,
942 * otherwise the vnode may remain inactive and thus tmpfs_inactive
943 * will not get called when we release it.
945 error = cache_vget(v->a_nch, v->a_cred, LK_SHARED, &vp);
946 KKASSERT(vp->v_mount == dvp->v_mount);
947 KKASSERT(error == 0);
950 if (vp->v_type == VDIR) {
955 dnode = VP_TO_TMPFS_DIR(dvp);
956 node = VP_TO_TMPFS_NODE(vp);
957 tmp = VFS_TO_TMPFS(vp->v_mount);
958 de = tmpfs_dir_lookup(dnode, node, ncp);
964 /* Files marked as immutable or append-only cannot be deleted. */
965 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
966 (dnode->tn_flags & APPEND)) {
971 /* Remove the entry from the directory; as it is a file, we do not
972 * have to change the number of hard links of the directory. */
973 tmpfs_dir_detach(dnode, de);
975 /* Free the directory entry we just deleted. Note that the node
976 * referred by it will not be removed until the vnode is really
978 tmpfs_free_dirent(tmp, de);
980 if (node->tn_links > 0) {
981 TMPFS_NODE_LOCK(node);
982 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
984 TMPFS_NODE_UNLOCK(node);
987 cache_unlink(v->a_nch);
988 tmpfs_knote(vp, NOTE_DELETE);
989 tmpfs_knote(dvp, NOTE_WRITE);
994 lwkt_reltoken(&mp->mnt_token);
999 /* --------------------------------------------------------------------- */
1002 tmpfs_nlink(struct vop_nlink_args *v)
1004 struct vnode *dvp = v->a_dvp;
1005 struct vnode *vp = v->a_vp;
1006 struct namecache *ncp = v->a_nch->ncp;
1007 struct tmpfs_dirent *de;
1008 struct tmpfs_node *node;
1009 struct tmpfs_node *dnode;
1013 if (dvp->v_mount != vp->v_mount)
1017 lwkt_gettoken(&mp->mnt_token);
1018 KKASSERT(dvp != vp); /* XXX When can this be false? */
1020 node = VP_TO_TMPFS_NODE(vp);
1021 dnode = VP_TO_TMPFS_NODE(dvp);
1023 /* XXX: Why aren't the following two tests done by the caller? */
1025 /* Hard links of directories are forbidden. */
1026 if (vp->v_type == VDIR) {
1031 /* Cannot create cross-device links. */
1032 if (dvp->v_mount != vp->v_mount) {
1037 /* Ensure that we do not overflow the maximum number of links imposed
1039 KKASSERT(node->tn_links <= LINK_MAX);
1040 if (node->tn_links == LINK_MAX) {
1045 /* We cannot create links of files marked immutable or append-only. */
1046 if (node->tn_flags & (IMMUTABLE | APPEND)) {
1051 /* Allocate a new directory entry to represent the node. */
1052 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
1053 ncp->nc_name, ncp->nc_nlen, &de);
1057 /* Insert the new directory entry into the appropriate directory. */
1058 tmpfs_dir_attach(dnode, de);
1060 /* vp link count has changed, so update node times. */
1062 TMPFS_NODE_LOCK(node);
1063 node->tn_status |= TMPFS_NODE_CHANGED;
1064 TMPFS_NODE_UNLOCK(node);
1067 tmpfs_knote(vp, NOTE_LINK);
1068 cache_setunresolved(v->a_nch);
1069 cache_setvp(v->a_nch, vp);
1070 tmpfs_knote(dvp, NOTE_WRITE);
1074 lwkt_reltoken(&mp->mnt_token);
1078 /* --------------------------------------------------------------------- */
1081 tmpfs_nrename(struct vop_nrename_args *v)
1083 struct vnode *fdvp = v->a_fdvp;
1084 struct namecache *fncp = v->a_fnch->ncp;
1085 struct vnode *fvp = fncp->nc_vp;
1086 struct vnode *tdvp = v->a_tdvp;
1087 struct namecache *tncp = v->a_tnch->ncp;
1089 struct tmpfs_dirent *de, *tde;
1090 struct tmpfs_mount *tmp;
1091 struct tmpfs_node *fdnode;
1092 struct tmpfs_node *fnode;
1093 struct tmpfs_node *tnode;
1094 struct tmpfs_node *tdnode;
1101 KKASSERT(fdvp->v_mount == fvp->v_mount);
1103 lwkt_gettoken(&mp->mnt_token);
1105 * Because tvp can get overwritten we have to vget it instead of
1106 * just vref or use it, otherwise it's VINACTIVE flag may not get
1107 * cleared and the node won't get destroyed.
1109 error = cache_vget(v->a_tnch, v->a_cred, LK_SHARED, &tvp);
1111 tnode = VP_TO_TMPFS_NODE(tvp);
1117 /* Disallow cross-device renames.
1118 * XXX Why isn't this done by the caller? */
1119 if (fvp->v_mount != tdvp->v_mount ||
1120 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1125 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1126 tdnode = VP_TO_TMPFS_DIR(tdvp);
1128 /* If source and target are the same file, there is nothing to do. */
1134 fdnode = VP_TO_TMPFS_DIR(fdvp);
1135 fnode = VP_TO_TMPFS_NODE(fvp);
1136 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
1138 /* Avoid manipulating '.' and '..' entries. */
1143 KKASSERT(de->td_node == fnode);
1146 * If replacing an entry in the target directory and that entry
1147 * is a directory, it must be empty.
1149 * Kern_rename gurantees the destination to be a directory
1150 * if the source is one (it does?).
1153 KKASSERT(tnode != NULL);
1155 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1156 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1161 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1162 if (tnode->tn_size > 0) {
1166 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1169 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1173 KKASSERT(fnode->tn_type != VDIR &&
1174 tnode->tn_type != VDIR);
1178 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1179 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1185 * Ensure that we have enough memory to hold the new name, if it
1186 * has to be changed.
1188 if (fncp->nc_nlen != tncp->nc_nlen ||
1189 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
1190 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
1191 M_WAITOK | M_NULLOK);
1192 if (newname == NULL) {
1196 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1197 newname[tncp->nc_nlen] = '\0';
1203 * Unlink entry from source directory. Note that the kernel has
1204 * already checked for illegal recursion cases (renaming a directory
1205 * into a subdirectory of itself).
1207 if (fdnode != tdnode) {
1208 tmpfs_dir_detach(fdnode, de);
1210 RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de);
1214 * Handle any name change. Swap with newname, we will
1215 * deallocate it at the end.
1217 if (newname != NULL) {
1219 TMPFS_NODE_LOCK(fnode);
1220 fnode->tn_status |= TMPFS_NODE_CHANGED;
1221 TMPFS_NODE_UNLOCK(fnode);
1223 oldname = de->td_name;
1224 de->td_name = newname;
1225 de->td_namelen = (uint16_t)tncp->nc_nlen;
1230 * If we are overwriting an entry, we have to remove the old one
1231 * from the target directory.
1234 /* Remove the old entry from the target directory. */
1235 tde = tmpfs_dir_lookup(tdnode, tnode, tncp);
1236 tmpfs_dir_detach(tdnode, tde);
1237 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1240 * Free the directory entry we just deleted. Note that the
1241 * node referred by it will not be removed until the vnode is
1244 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1245 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1249 * Link entry to target directory. If the entry
1250 * represents a directory move the parent linkage
1253 if (fdnode != tdnode) {
1254 if (de->td_node->tn_type == VDIR) {
1255 TMPFS_VALIDATE_DIR(fnode);
1257 tmpfs_dir_attach(tdnode, de);
1259 TMPFS_NODE_LOCK(tdnode);
1260 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1261 RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de);
1262 TMPFS_NODE_UNLOCK(tdnode);
1269 kfree(newname, tmp->tm_name_zone);
1272 cache_rename(v->a_fnch, v->a_tnch);
1273 tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1274 tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1275 if (fnode->tn_vnode)
1276 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1286 lwkt_reltoken(&mp->mnt_token);
1291 /* --------------------------------------------------------------------- */
1294 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1296 struct vnode *dvp = v->a_dvp;
1297 struct vnode **vpp = v->a_vpp;
1298 struct namecache *ncp = v->a_nch->ncp;
1299 struct vattr *vap = v->a_vap;
1300 struct ucred *cred = v->a_cred;
1306 lwkt_gettoken(&mp->mnt_token);
1307 KKASSERT(vap->va_type == VDIR);
1309 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1311 cache_setunresolved(v->a_nch);
1312 cache_setvp(v->a_nch, *vpp);
1313 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1316 lwkt_reltoken(&mp->mnt_token);
1321 /* --------------------------------------------------------------------- */
1324 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1326 struct vnode *dvp = v->a_dvp;
1327 struct namecache *ncp = v->a_nch->ncp;
1329 struct tmpfs_dirent *de;
1330 struct tmpfs_mount *tmp;
1331 struct tmpfs_node *dnode;
1332 struct tmpfs_node *node;
1337 lwkt_gettoken(&mp->mnt_token);
1340 * We have to acquire the vp from v->a_nch because we will likely
1341 * unresolve the namecache entry, and a vrele/vput is needed to
1342 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1344 * We have to use vget to clear any inactive state on the vnode,
1345 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1346 * will not get called when we release it.
1348 error = cache_vget(v->a_nch, v->a_cred, LK_SHARED, &vp);
1349 KKASSERT(error == 0);
1353 * Prevalidate so we don't hit an assertion later
1355 if (vp->v_type != VDIR) {
1360 tmp = VFS_TO_TMPFS(dvp->v_mount);
1361 dnode = VP_TO_TMPFS_DIR(dvp);
1362 node = VP_TO_TMPFS_DIR(vp);
1364 /* Directories with more than two entries ('.' and '..') cannot be
1366 if (node->tn_size > 0) {
1371 if ((dnode->tn_flags & APPEND)
1372 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1377 /* This invariant holds only if we are not trying to remove "..".
1378 * We checked for that above so this is safe now. */
1379 KKASSERT(node->tn_dir.tn_parent == dnode);
1381 /* Get the directory entry associated with node (vp). This was
1382 * filled by tmpfs_lookup while looking up the entry. */
1383 de = tmpfs_dir_lookup(dnode, node, ncp);
1384 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1388 /* Check flags to see if we are allowed to remove the directory. */
1389 if ((dnode->tn_flags & APPEND) ||
1390 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1396 /* Detach the directory entry from the directory (dnode). */
1397 tmpfs_dir_detach(dnode, de);
1399 /* No vnode should be allocated for this entry from this point */
1400 TMPFS_NODE_LOCK(node);
1401 TMPFS_ASSERT_ELOCKED(node);
1402 TMPFS_NODE_LOCK(dnode);
1403 TMPFS_ASSERT_ELOCKED(dnode);
1406 * Must set parent linkage to NULL (tested by ncreate to disallow
1407 * the creation of new files/dirs in a deleted directory)
1409 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1410 TMPFS_NODE_MODIFIED;
1412 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1413 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1415 TMPFS_NODE_UNLOCK(dnode);
1416 TMPFS_NODE_UNLOCK(node);
1418 /* Free the directory entry we just deleted. Note that the node
1419 * referred by it will not be removed until the vnode is really
1421 tmpfs_free_dirent(tmp, de);
1423 /* Release the deleted vnode (will destroy the node, notify
1424 * interested parties and clean it from the cache). */
1426 TMPFS_NODE_LOCK(dnode);
1427 dnode->tn_status |= TMPFS_NODE_CHANGED;
1428 TMPFS_NODE_UNLOCK(dnode);
1431 cache_unlink(v->a_nch);
1432 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1438 lwkt_reltoken(&mp->mnt_token);
1443 /* --------------------------------------------------------------------- */
1446 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1448 struct vnode *dvp = v->a_dvp;
1449 struct vnode **vpp = v->a_vpp;
1450 struct namecache *ncp = v->a_nch->ncp;
1451 struct vattr *vap = v->a_vap;
1452 struct ucred *cred = v->a_cred;
1453 char *target = v->a_target;
1454 struct mount *mp = dvp->v_mount;
1457 lwkt_gettoken(&mp->mnt_token);
1458 vap->va_type = VLNK;
1459 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1461 tmpfs_knote(*vpp, NOTE_WRITE);
1462 cache_setunresolved(v->a_nch);
1463 cache_setvp(v->a_nch, *vpp);
1466 lwkt_reltoken(&mp->mnt_token);
1471 /* --------------------------------------------------------------------- */
1474 tmpfs_readdir(struct vop_readdir_args *v)
1476 struct vnode *vp = v->a_vp;
1477 struct uio *uio = v->a_uio;
1478 int *eofflag = v->a_eofflag;
1479 off_t **cookies = v->a_cookies;
1480 int *ncookies = v->a_ncookies;
1481 struct tmpfs_mount *tmp;
1485 struct tmpfs_node *node;
1486 struct mount *mp = vp->v_mount;
1488 lwkt_gettoken(&mp->mnt_token);
1490 /* This operation only makes sense on directory nodes. */
1491 if (vp->v_type != VDIR) {
1492 lwkt_reltoken(&mp->mnt_token);
1496 tmp = VFS_TO_TMPFS(vp->v_mount);
1497 node = VP_TO_TMPFS_DIR(vp);
1498 startoff = uio->uio_offset;
1500 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1501 error = tmpfs_dir_getdotdent(node, uio);
1507 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1508 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1514 error = tmpfs_dir_getdents(node, uio, &cnt);
1517 KKASSERT(error >= -1);
1522 if (eofflag != NULL)
1524 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1526 /* Update NFS-related variables. */
1527 if (error == 0 && cookies != NULL && ncookies != NULL) {
1529 off_t off = startoff;
1530 struct tmpfs_dirent *de = NULL;
1533 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1535 for (i = 0; i < cnt; i++) {
1536 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1537 if (off == TMPFS_DIRCOOKIE_DOT) {
1538 off = TMPFS_DIRCOOKIE_DOTDOT;
1540 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1541 de = RB_MIN(tmpfs_dirtree, &node->tn_dir.tn_dirtree);
1542 } else if (de != NULL) {
1543 de = RB_NEXT(tmpfs_dirtree, &node->tn_dir.tn_dirtree, de);
1545 de = tmpfs_dir_lookupbycookie(node,
1547 KKASSERT(de != NULL);
1548 de = RB_NEXT(tmpfs_dirtree, &node->tn_dir.tn_dirtree, de);
1551 off = TMPFS_DIRCOOKIE_EOF;
1553 off = tmpfs_dircookie(de);
1556 (*cookies)[i] = off;
1558 KKASSERT(uio->uio_offset == off);
1561 lwkt_reltoken(&mp->mnt_token);
1566 /* --------------------------------------------------------------------- */
1569 tmpfs_readlink(struct vop_readlink_args *v)
1571 struct vnode *vp = v->a_vp;
1572 struct uio *uio = v->a_uio;
1573 struct mount *mp = vp->v_mount;
1575 struct tmpfs_node *node;
1577 lwkt_gettoken(&mp->mnt_token);
1579 KKASSERT(uio->uio_offset == 0);
1580 KKASSERT(vp->v_type == VLNK);
1582 node = VP_TO_TMPFS_NODE(vp);
1584 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1586 TMPFS_NODE_LOCK(node);
1587 node->tn_status |= TMPFS_NODE_ACCESSED;
1588 TMPFS_NODE_UNLOCK(node);
1590 lwkt_reltoken(&mp->mnt_token);
1595 /* --------------------------------------------------------------------- */
1598 tmpfs_inactive(struct vop_inactive_args *v)
1600 struct vnode *vp = v->a_vp;
1601 struct tmpfs_node *node;
1605 lwkt_gettoken(&mp->mnt_token);
1606 node = VP_TO_TMPFS_NODE(vp);
1613 lwkt_reltoken(&mp->mnt_token);
1618 * Get rid of unreferenced deleted vnodes sooner rather than
1619 * later so the data memory can be recovered immediately.
1621 * We must truncate the vnode to prevent the normal reclamation
1622 * path from flushing the data for the removed file to disk.
1624 TMPFS_NODE_LOCK(node);
1625 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1626 node->tn_links == 0)
1628 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1629 TMPFS_NODE_UNLOCK(node);
1630 if (node->tn_type == VREG)
1631 tmpfs_truncate(vp, 0);
1634 TMPFS_NODE_UNLOCK(node);
1636 lwkt_reltoken(&mp->mnt_token);
1641 /* --------------------------------------------------------------------- */
1644 tmpfs_reclaim(struct vop_reclaim_args *v)
1646 struct vnode *vp = v->a_vp;
1647 struct tmpfs_mount *tmp;
1648 struct tmpfs_node *node;
1652 lwkt_gettoken(&mp->mnt_token);
1654 node = VP_TO_TMPFS_NODE(vp);
1655 tmp = VFS_TO_TMPFS(vp->v_mount);
1656 KKASSERT(mp == tmp->tm_mount);
1661 * If the node referenced by this vnode was deleted by the
1662 * user, we must free its associated data structures now that
1663 * the vnode is being reclaimed.
1665 * Directories have an extra link ref.
1667 TMPFS_NODE_LOCK(node);
1668 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1669 node->tn_links == 0) {
1670 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1671 tmpfs_free_node(tmp, node);
1674 TMPFS_NODE_UNLOCK(node);
1676 lwkt_reltoken(&mp->mnt_token);
1678 KKASSERT(vp->v_data == NULL);
1682 /* --------------------------------------------------------------------- */
1685 tmpfs_mountctl(struct vop_mountctl_args *ap)
1687 struct tmpfs_mount *tmp;
1691 mp = ap->a_head.a_ops->head.vv_mount;
1692 lwkt_gettoken(&mp->mnt_token);
1695 case (MOUNTCTL_SET_EXPORT):
1696 tmp = (struct tmpfs_mount *) mp->mnt_data;
1698 if (ap->a_ctllen != sizeof(struct export_args))
1701 rc = vfs_export(mp, &tmp->tm_export,
1702 (const struct export_args *) ap->a_ctl);
1705 rc = vop_stdmountctl(ap);
1709 lwkt_reltoken(&mp->mnt_token);
1713 /* --------------------------------------------------------------------- */
1716 tmpfs_print(struct vop_print_args *v)
1718 struct vnode *vp = v->a_vp;
1720 struct tmpfs_node *node;
1722 node = VP_TO_TMPFS_NODE(vp);
1724 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1725 node, node->tn_flags, node->tn_links);
1726 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1727 node->tn_mode, node->tn_uid, node->tn_gid,
1728 (uintmax_t)node->tn_size, node->tn_status);
1730 if (vp->v_type == VFIFO)
1738 /* --------------------------------------------------------------------- */
1741 tmpfs_pathconf(struct vop_pathconf_args *v)
1743 int name = v->a_name;
1744 register_t *retval = v->a_retval;
1767 case _PC_CHOWN_RESTRICTED:
1779 case _PC_FILESIZEBITS:
1780 *retval = 0; /* XXX Don't know which value should I return. */
1790 /************************************************************************
1792 ************************************************************************/
1794 static void filt_tmpfsdetach(struct knote *kn);
1795 static int filt_tmpfsread(struct knote *kn, long hint);
1796 static int filt_tmpfswrite(struct knote *kn, long hint);
1797 static int filt_tmpfsvnode(struct knote *kn, long hint);
1799 static struct filterops tmpfsread_filtops =
1800 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsread };
1801 static struct filterops tmpfswrite_filtops =
1802 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfswrite };
1803 static struct filterops tmpfsvnode_filtops =
1804 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1807 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1809 struct vnode *vp = ap->a_vp;
1810 struct knote *kn = ap->a_kn;
1812 switch (kn->kn_filter) {
1814 kn->kn_fop = &tmpfsread_filtops;
1817 kn->kn_fop = &tmpfswrite_filtops;
1820 kn->kn_fop = &tmpfsvnode_filtops;
1823 return (EOPNOTSUPP);
1826 kn->kn_hook = (caddr_t)vp;
1828 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1834 filt_tmpfsdetach(struct knote *kn)
1836 struct vnode *vp = (void *)kn->kn_hook;
1838 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1842 filt_tmpfsread(struct knote *kn, long hint)
1844 struct vnode *vp = (void *)kn->kn_hook;
1845 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1848 if (hint == NOTE_REVOKE) {
1849 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1854 * Interlock against MP races when performing this function.
1856 lwkt_gettoken(&vp->v_mount->mnt_token);
1857 off = node->tn_size - kn->kn_fp->f_offset;
1858 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1859 if (kn->kn_sfflags & NOTE_OLDAPI) {
1860 lwkt_reltoken(&vp->v_mount->mnt_token);
1864 if (kn->kn_data == 0) {
1865 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1867 lwkt_reltoken(&vp->v_mount->mnt_token);
1868 return (kn->kn_data != 0);
1872 filt_tmpfswrite(struct knote *kn, long hint)
1874 if (hint == NOTE_REVOKE)
1875 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1881 filt_tmpfsvnode(struct knote *kn, long hint)
1883 if (kn->kn_sfflags & hint)
1884 kn->kn_fflags |= hint;
1885 if (hint == NOTE_REVOKE) {
1886 kn->kn_flags |= (EV_EOF | EV_NODATA);
1889 return (kn->kn_fflags != 0);
1893 /* --------------------------------------------------------------------- */
1896 * vnode operations vector used for files stored in a tmpfs file system.
1898 struct vop_ops tmpfs_vnode_vops = {
1899 .vop_default = vop_defaultop,
1900 .vop_getpages = vop_stdgetpages,
1901 .vop_putpages = vop_stdputpages,
1902 .vop_ncreate = tmpfs_ncreate,
1903 .vop_nresolve = tmpfs_nresolve,
1904 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1905 .vop_nmknod = tmpfs_nmknod,
1906 .vop_open = tmpfs_open,
1907 .vop_close = tmpfs_close,
1908 .vop_access = tmpfs_access,
1909 .vop_getattr = tmpfs_getattr,
1910 .vop_setattr = tmpfs_setattr,
1911 .vop_read = tmpfs_read,
1912 .vop_write = tmpfs_write,
1913 .vop_fsync = tmpfs_fsync,
1914 .vop_mountctl = tmpfs_mountctl,
1915 .vop_nremove = tmpfs_nremove,
1916 .vop_nlink = tmpfs_nlink,
1917 .vop_nrename = tmpfs_nrename,
1918 .vop_nmkdir = tmpfs_nmkdir,
1919 .vop_nrmdir = tmpfs_nrmdir,
1920 .vop_nsymlink = tmpfs_nsymlink,
1921 .vop_readdir = tmpfs_readdir,
1922 .vop_readlink = tmpfs_readlink,
1923 .vop_inactive = tmpfs_inactive,
1924 .vop_reclaim = tmpfs_reclaim,
1925 .vop_print = tmpfs_print,
1926 .vop_pathconf = tmpfs_pathconf,
1927 .vop_bmap = tmpfs_bmap,
1928 .vop_strategy = tmpfs_strategy,
1929 .vop_advlock = tmpfs_advlock,
1930 .vop_kqfilter = tmpfs_kqfilter