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;
92 struct tmpfs_dirent *de;
93 struct tmpfs_node *dnode;
98 dnode = VP_TO_TMPFS_DIR(dvp);
100 TMPFS_NODE_LOCK_SH(dnode);
101 de = tmpfs_dir_lookup(dnode, NULL, ncp);
106 * Allocate a vnode for the node we found.
109 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
110 LK_EXCLUSIVE | LK_RETRY, &vp);
117 TMPFS_NODE_UNLOCK(dnode);
119 if ((dnode->tn_status & TMPFS_NODE_ACCESSED) == 0) {
120 TMPFS_NODE_LOCK(dnode);
121 dnode->tn_status |= TMPFS_NODE_ACCESSED;
122 TMPFS_NODE_UNLOCK(dnode);
126 * Store the result of this lookup in the cache. Avoid this if the
127 * request was for creation, as it does not improve timings on
132 cache_setvp(v->a_nch, vp);
134 } else if (error == ENOENT) {
135 cache_setvp(v->a_nch, NULL);
141 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
143 struct vnode *dvp = v->a_dvp;
144 struct vnode **vpp = v->a_vpp;
145 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
146 struct ucred *cred = v->a_cred;
154 /* Check accessibility of requested node as a first step. */
155 error = VOP_ACCESS(dvp, VEXEC, cred);
159 if (dnode->tn_dir.tn_parent != NULL) {
160 /* Allocate a new vnode on the matching entry. */
161 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
162 LK_EXCLUSIVE | LK_RETRY, vpp);
167 return (*vpp == NULL) ? ENOENT : 0;
170 /* --------------------------------------------------------------------- */
173 tmpfs_ncreate(struct vop_ncreate_args *v)
175 struct vnode *dvp = v->a_dvp;
176 struct vnode **vpp = v->a_vpp;
177 struct namecache *ncp = v->a_nch->ncp;
178 struct vattr *vap = v->a_vap;
179 struct ucred *cred = v->a_cred;
185 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
187 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
189 cache_setunresolved(v->a_nch);
190 cache_setvp(v->a_nch, *vpp);
191 tmpfs_knote(dvp, NOTE_WRITE);
195 /* --------------------------------------------------------------------- */
198 tmpfs_nmknod(struct vop_nmknod_args *v)
200 struct vnode *dvp = v->a_dvp;
201 struct vnode **vpp = v->a_vpp;
202 struct namecache *ncp = v->a_nch->ncp;
203 struct vattr *vap = v->a_vap;
204 struct ucred *cred = v->a_cred;
207 if (vap->va_type != VBLK && vap->va_type != VCHR &&
208 vap->va_type != VFIFO) {
212 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
214 cache_setunresolved(v->a_nch);
215 cache_setvp(v->a_nch, *vpp);
216 tmpfs_knote(dvp, NOTE_WRITE);
221 /* --------------------------------------------------------------------- */
224 tmpfs_open(struct vop_open_args *v)
226 struct vnode *vp = v->a_vp;
227 int mode = v->a_mode;
228 struct tmpfs_node *node;
231 node = VP_TO_TMPFS_NODE(vp);
234 /* The file is still active but all its names have been removed
235 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
236 * it is about to die. */
237 if (node->tn_links < 1)
241 /* If the file is marked append-only, deny write requests. */
242 if ((node->tn_flags & APPEND) &&
243 (mode & (FWRITE | O_APPEND)) == FWRITE) {
246 error = (vop_stdopen(v));
252 /* --------------------------------------------------------------------- */
255 tmpfs_close(struct vop_close_args *v)
257 struct vnode *vp = v->a_vp;
258 struct tmpfs_node *node;
261 node = VP_TO_TMPFS_NODE(vp);
263 if (node->tn_links > 0) {
265 * Update node times. No need to do it if the node has
266 * been deleted, because it will vanish after we return.
271 error = vop_stdclose(v);
276 /* --------------------------------------------------------------------- */
279 tmpfs_access(struct vop_access_args *v)
281 struct vnode *vp = v->a_vp;
283 struct tmpfs_node *node;
285 node = VP_TO_TMPFS_NODE(vp);
287 switch (vp->v_type) {
293 if ((v->a_mode & VWRITE) &&
294 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
314 if ((v->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
319 error = vop_helper_access(v, node->tn_uid, node->tn_gid,
325 /* --------------------------------------------------------------------- */
328 tmpfs_getattr(struct vop_getattr_args *v)
330 struct vnode *vp = v->a_vp;
331 struct vattr *vap = v->a_vap;
332 struct tmpfs_node *node;
334 node = VP_TO_TMPFS_NODE(vp);
338 TMPFS_NODE_LOCK_SH(node);
339 vap->va_type = vp->v_type;
340 vap->va_mode = node->tn_mode;
341 vap->va_nlink = node->tn_links;
342 vap->va_uid = node->tn_uid;
343 vap->va_gid = node->tn_gid;
344 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
345 vap->va_fileid = node->tn_id;
346 vap->va_size = node->tn_size;
347 vap->va_blocksize = PAGE_SIZE;
348 vap->va_atime.tv_sec = node->tn_atime;
349 vap->va_atime.tv_nsec = node->tn_atimensec;
350 vap->va_mtime.tv_sec = node->tn_mtime;
351 vap->va_mtime.tv_nsec = node->tn_mtimensec;
352 vap->va_ctime.tv_sec = node->tn_ctime;
353 vap->va_ctime.tv_nsec = node->tn_ctimensec;
354 vap->va_gen = node->tn_gen;
355 vap->va_flags = node->tn_flags;
356 if (vp->v_type == VBLK || vp->v_type == VCHR) {
357 vap->va_rmajor = umajor(node->tn_rdev);
358 vap->va_rminor = uminor(node->tn_rdev);
360 vap->va_bytes = round_page(node->tn_size);
362 TMPFS_NODE_UNLOCK(node);
367 /* --------------------------------------------------------------------- */
370 tmpfs_setattr(struct vop_setattr_args *v)
372 struct vnode *vp = v->a_vp;
373 struct vattr *vap = v->a_vap;
374 struct ucred *cred = v->a_cred;
375 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
379 TMPFS_NODE_LOCK(node);
380 if (error == 0 && (vap->va_flags != VNOVAL)) {
381 error = tmpfs_chflags(vp, vap->va_flags, cred);
382 kflags |= NOTE_ATTRIB;
385 if (error == 0 && (vap->va_size != VNOVAL)) {
386 if (vap->va_size > node->tn_size)
387 kflags |= NOTE_WRITE | NOTE_EXTEND;
389 kflags |= NOTE_WRITE;
390 error = tmpfs_chsize(vp, vap->va_size, cred);
393 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
394 vap->va_gid != (gid_t)VNOVAL)) {
395 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
396 kflags |= NOTE_ATTRIB;
399 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
400 error = tmpfs_chmod(vp, vap->va_mode, cred);
401 kflags |= NOTE_ATTRIB;
404 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
405 vap->va_atime.tv_nsec != VNOVAL) ||
406 (vap->va_mtime.tv_sec != VNOVAL &&
407 vap->va_mtime.tv_nsec != VNOVAL) )) {
408 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
409 vap->va_vaflags, cred);
410 kflags |= NOTE_ATTRIB;
414 * Update the node times. We give preference to the error codes
415 * generated by this function rather than the ones that may arise
419 TMPFS_NODE_UNLOCK(node);
420 tmpfs_knote(vp, kflags);
425 /* --------------------------------------------------------------------- */
428 * fsync is usually a NOP, but we must take action when unmounting or
432 tmpfs_fsync(struct vop_fsync_args *v)
434 struct tmpfs_node *node;
435 struct vnode *vp = v->a_vp;
437 node = VP_TO_TMPFS_NODE(vp);
440 if (vp->v_type == VREG) {
441 if (vp->v_flag & VRECLAIMED) {
442 if (node->tn_links == 0)
443 tmpfs_truncate(vp, 0);
445 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
451 /* --------------------------------------------------------------------- */
454 tmpfs_read (struct vop_read_args *ap)
457 struct vnode *vp = ap->a_vp;
458 struct uio *uio = ap->a_uio;
459 struct tmpfs_node *node;
469 if (uio->uio_offset < 0)
471 if (vp->v_type != VREG)
475 * Extract node, try to shortcut the operation through
476 * the VM page cache, allowing us to avoid buffer cache
479 node = VP_TO_TMPFS_NODE(vp);
480 resid = uio->uio_resid;
481 error = vop_helper_read_shortcut(ap);
484 if (uio->uio_resid == 0) {
491 * Fall-through to our normal read code.
493 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
495 * Use buffer cache I/O (via tmpfs_strategy)
497 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
498 base_offset = (off_t)uio->uio_offset - offset;
499 bp = getcacheblk(vp, base_offset, TMPFS_BLKSIZE, 0);
501 error = bread(vp, base_offset, TMPFS_BLKSIZE, &bp);
504 kprintf("tmpfs_read bread error %d\n", error);
509 * tmpfs pretty much fiddles directly with the VM
510 * system, don't let it exhaust it or we won't play
511 * nice with other processes.
513 * Only do this if the VOP is coming from a normal
514 * read/write. The VM system handles the case for
517 if (uio->uio_segflg != UIO_NOCOPY)
520 bp->b_flags |= B_CLUSTEROK;
523 * Figure out how many bytes we can actually copy this loop.
525 len = TMPFS_BLKSIZE - offset;
526 if (len > uio->uio_resid)
527 len = uio->uio_resid;
528 if (len > node->tn_size - uio->uio_offset)
529 len = (size_t)(node->tn_size - uio->uio_offset);
531 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
534 kprintf("tmpfs_read uiomove error %d\n", error);
540 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
541 TMPFS_NODE_LOCK(node);
542 node->tn_status |= TMPFS_NODE_ACCESSED;
543 TMPFS_NODE_UNLOCK(node);
549 tmpfs_write (struct vop_write_args *ap)
552 struct vnode *vp = ap->a_vp;
553 struct uio *uio = ap->a_uio;
554 struct thread *td = uio->uio_td;
555 struct tmpfs_node *node;
568 if (uio->uio_resid == 0) {
572 node = VP_TO_TMPFS_NODE(vp);
574 if (vp->v_type != VREG)
576 seqcount = ap->a_ioflag >> 16;
578 oldsize = node->tn_size;
579 if (ap->a_ioflag & IO_APPEND)
580 uio->uio_offset = node->tn_size;
583 * Check for illegal write offsets.
585 if (uio->uio_offset + uio->uio_resid >
586 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) {
591 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN).
593 if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) {
594 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
598 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
599 ksignal(td->td_proc, SIGXFSZ);
606 * Extend the file's size if necessary
608 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
610 while (uio->uio_resid > 0) {
612 * Don't completely blow out running buffer I/O
613 * when being hit from the pageout daemon.
615 if (uio->uio_segflg == UIO_NOCOPY &&
616 (ap->a_ioflag & IO_RECURSE) == 0) {
617 bwillwrite(TMPFS_BLKSIZE);
621 * Use buffer cache I/O (via tmpfs_strategy)
623 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
624 base_offset = (off_t)uio->uio_offset - offset;
625 len = TMPFS_BLKSIZE - offset;
626 if (len > uio->uio_resid)
627 len = uio->uio_resid;
629 if ((uio->uio_offset + len) > node->tn_size) {
630 trivial = (uio->uio_offset <= node->tn_size);
631 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
637 * Read to fill in any gaps. Theoretically we could
638 * optimize this if the write covers the entire buffer
639 * and is not a UIO_NOCOPY write, however this can lead
640 * to a security violation exposing random kernel memory
641 * (whatever junk was in the backing VM pages before).
643 * So just use bread() to do the right thing.
645 error = bread(vp, base_offset, TMPFS_BLKSIZE, &bp);
646 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
648 kprintf("tmpfs_write uiomove error %d\n", error);
653 if (uio->uio_offset > node->tn_size) {
654 node->tn_size = uio->uio_offset;
655 kflags |= NOTE_EXTEND;
657 kflags |= NOTE_WRITE;
660 * Always try to flush the page in the UIO_NOCOPY case. This
661 * can come from the pageout daemon or during vnode eviction.
662 * It is not necessarily going to be marked IO_ASYNC/IO_SYNC.
664 * For the normal case we buwrite(), dirtying the underlying
665 * VM pages instead of dirtying the buffer and releasing the
666 * buffer as a clean buffer. This allows tmpfs to use
667 * essentially all available memory to cache file data.
668 * If we used bdwrite() the buffer cache would wind up
669 * flushing the data to swap too quickly.
671 * But because tmpfs can seriously load the VM system we
672 * fall-back to using bdwrite() when free memory starts
673 * to get low. This shifts the load away from the VM system
674 * and makes tmpfs act more like a normal filesystem with
675 * regards to disk activity.
677 * tmpfs pretty much fiddles directly with the VM
678 * system, don't let it exhaust it or we won't play
679 * nice with other processes. Only do this if the
680 * VOP is coming from a normal read/write. The VM system
681 * handles the case for UIO_NOCOPY.
683 bp->b_flags |= B_CLUSTEROK;
684 if (uio->uio_segflg == UIO_NOCOPY) {
686 * Flush from the pageout daemon, deal with
687 * potentially very heavy tmpfs write activity
688 * causing long stalls in the pageout daemon
689 * before pages get to free/cache.
691 * (a) Under severe pressure setting B_DIRECT will
692 * cause a buffer release to try to free the
695 * (b) Under modest memory pressure the B_RELBUF
696 * alone is sufficient to get the pages moved
697 * to the cache. We could also force this by
698 * setting B_NOTMETA but that might have other
699 * unintended side-effects (e.g. setting
700 * PG_NOTMETA on the VM page).
702 * Hopefully this will unblock the VM system more
703 * quickly under extreme tmpfs write load.
705 if (vm_page_count_min(vm_page_free_hysteresis))
706 bp->b_flags |= B_DIRECT;
707 bp->b_flags |= B_AGE | B_RELBUF;
708 bp->b_act_count = 0; /* buffer->deactivate pgs */
710 } else if (vm_page_count_target()) {
712 * Normal (userland) write but we are low on memory,
713 * run the buffer the buffer cache.
715 bp->b_act_count = 0; /* buffer->deactivate pgs */
719 * Otherwise run the buffer directly through to the
723 /*vm_wait_nominal();*/
727 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
734 (void)tmpfs_reg_resize(vp, oldsize, trivial);
735 kflags &= ~NOTE_EXTEND;
741 * Currently we don't set the mtime on files modified via mmap()
742 * because we can't tell the difference between those modifications
743 * and an attempt by the pageout daemon to flush tmpfs pages to
746 * This is because in order to defer flushes as long as possible
747 * buwrite() works by marking the underlying VM pages dirty in
748 * order to be able to dispose of the buffer cache buffer without
751 TMPFS_NODE_LOCK(node);
752 if (uio->uio_segflg != UIO_NOCOPY)
753 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED;
755 node->tn_status |= TMPFS_NODE_CHANGED;
757 if (node->tn_mode & (S_ISUID | S_ISGID)) {
758 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
759 node->tn_mode &= ~(S_ISUID | S_ISGID);
761 TMPFS_NODE_UNLOCK(node);
763 tmpfs_knote(vp, kflags);
769 tmpfs_advlock (struct vop_advlock_args *ap)
771 struct tmpfs_node *node;
772 struct vnode *vp = ap->a_vp;
775 node = VP_TO_TMPFS_NODE(vp);
776 error = (lf_advlock(ap, &node->tn_advlock, node->tn_size));
782 * The strategy function is typically only called when memory pressure
783 * forces the system to attempt to pageout pages. It can also be called
784 * by [n]vtruncbuf() when a truncation cuts a page in half. Normal write
788 tmpfs_strategy(struct vop_strategy_args *ap)
790 struct bio *bio = ap->a_bio;
792 struct buf *bp = bio->bio_buf;
793 struct vnode *vp = ap->a_vp;
794 struct tmpfs_node *node;
799 if (vp->v_type != VREG) {
800 bp->b_resid = bp->b_bcount;
801 bp->b_flags |= B_ERROR | B_INVAL;
802 bp->b_error = EINVAL;
807 node = VP_TO_TMPFS_NODE(vp);
809 uobj = node->tn_reg.tn_aobj;
812 * Don't bother flushing to swap if there is no swap, just
813 * ensure that the pages are marked as needing a commit (still).
815 if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) {
816 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
817 m = bp->b_xio.xio_pages[i];
818 vm_page_need_commit(m);
824 nbio = push_bio(bio);
825 nbio->bio_done = tmpfs_strategy_done;
826 nbio->bio_offset = bio->bio_offset;
827 swap_pager_strategy(uobj, nbio);
833 * If we were unable to commit the pages to swap make sure they are marked
834 * as needing a commit (again). If we were, clear the flag to allow the
838 tmpfs_strategy_done(struct bio *bio)
846 if (bp->b_flags & B_ERROR) {
847 bp->b_flags &= ~B_ERROR;
850 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
851 m = bp->b_xio.xio_pages[i];
852 vm_page_need_commit(m);
855 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
856 m = bp->b_xio.xio_pages[i];
857 vm_page_clear_commit(m);
865 tmpfs_bmap(struct vop_bmap_args *ap)
867 if (ap->a_doffsetp != NULL)
868 *ap->a_doffsetp = ap->a_loffset;
869 if (ap->a_runp != NULL)
871 if (ap->a_runb != NULL)
877 /* --------------------------------------------------------------------- */
880 tmpfs_nremove(struct vop_nremove_args *v)
882 struct vnode *dvp = v->a_dvp;
883 struct namecache *ncp = v->a_nch->ncp;
886 struct tmpfs_dirent *de;
887 struct tmpfs_mount *tmp;
888 struct tmpfs_node *dnode;
889 struct tmpfs_node *node;
895 * We have to acquire the vp from v->a_nch because we will likely
896 * unresolve the namecache entry, and a vrele/vput is needed to
897 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
899 * We have to use vget to clear any inactive state on the vnode,
900 * otherwise the vnode may remain inactive and thus tmpfs_inactive
901 * will not get called when we release it.
903 error = cache_vget(v->a_nch, v->a_cred, LK_SHARED, &vp);
904 KKASSERT(vp->v_mount == dvp->v_mount);
905 KKASSERT(error == 0);
908 if (vp->v_type == VDIR) {
913 dnode = VP_TO_TMPFS_DIR(dvp);
914 node = VP_TO_TMPFS_NODE(vp);
915 tmp = VFS_TO_TMPFS(vp->v_mount);
917 TMPFS_NODE_LOCK(dnode);
918 de = tmpfs_dir_lookup(dnode, node, ncp);
924 /* Files marked as immutable or append-only cannot be deleted. */
925 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
926 (dnode->tn_flags & APPEND)) {
931 /* Remove the entry from the directory; as it is a file, we do not
932 * have to change the number of hard links of the directory. */
933 tmpfs_dir_detach(dnode, de);
935 /* Free the directory entry we just deleted. Note that the node
936 * referred by it will not be removed until the vnode is really
938 tmpfs_free_dirent(tmp, de);
940 if (node->tn_links > 0) {
941 TMPFS_NODE_LOCK(node);
942 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
944 TMPFS_NODE_UNLOCK(node);
947 cache_unlink(v->a_nch);
948 tmpfs_knote(vp, NOTE_DELETE);
952 TMPFS_NODE_UNLOCK(dnode);
954 tmpfs_knote(dvp, NOTE_WRITE);
961 /* --------------------------------------------------------------------- */
964 tmpfs_nlink(struct vop_nlink_args *v)
966 struct vnode *dvp = v->a_dvp;
967 struct vnode *vp = v->a_vp;
968 struct namecache *ncp = v->a_nch->ncp;
969 struct tmpfs_dirent *de;
970 struct tmpfs_node *node;
971 struct tmpfs_node *dnode;
975 if (dvp->v_mount != vp->v_mount)
979 KKASSERT(dvp != vp); /* XXX When can this be false? */
981 node = VP_TO_TMPFS_NODE(vp);
982 dnode = VP_TO_TMPFS_NODE(dvp);
983 TMPFS_NODE_LOCK(dnode);
985 /* XXX: Why aren't the following two tests done by the caller? */
987 /* Hard links of directories are forbidden. */
988 if (vp->v_type == VDIR) {
993 /* Cannot create cross-device links. */
994 if (dvp->v_mount != vp->v_mount) {
999 /* Ensure that we do not overflow the maximum number of links imposed
1001 KKASSERT(node->tn_links <= LINK_MAX);
1002 if (node->tn_links >= LINK_MAX) {
1007 /* We cannot create links of files marked immutable or append-only. */
1008 if (node->tn_flags & (IMMUTABLE | APPEND)) {
1013 /* Allocate a new directory entry to represent the node. */
1014 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
1015 ncp->nc_name, ncp->nc_nlen, &de);
1019 /* Insert the new directory entry into the appropriate directory. */
1020 tmpfs_dir_attach(dnode, de);
1022 /* vp link count has changed, so update node times. */
1024 TMPFS_NODE_LOCK(node);
1025 node->tn_status |= TMPFS_NODE_CHANGED;
1026 TMPFS_NODE_UNLOCK(node);
1029 tmpfs_knote(vp, NOTE_LINK);
1030 cache_setunresolved(v->a_nch);
1031 cache_setvp(v->a_nch, vp);
1035 TMPFS_NODE_UNLOCK(dnode);
1037 tmpfs_knote(dvp, NOTE_WRITE);
1041 /* --------------------------------------------------------------------- */
1044 tmpfs_nrename(struct vop_nrename_args *v)
1046 struct vnode *fdvp = v->a_fdvp;
1047 struct namecache *fncp = v->a_fnch->ncp;
1048 struct vnode *fvp = fncp->nc_vp;
1049 struct vnode *tdvp = v->a_tdvp;
1050 struct namecache *tncp = v->a_tnch->ncp;
1052 struct tmpfs_dirent *de, *tde;
1053 struct tmpfs_mount *tmp;
1054 struct tmpfs_node *fdnode;
1055 struct tmpfs_node *fnode;
1056 struct tmpfs_node *tnode;
1057 struct tmpfs_node *tdnode;
1064 KKASSERT(fdvp->v_mount == fvp->v_mount);
1067 * Because tvp can get overwritten we have to vget it instead of
1068 * just vref or use it, otherwise it's VINACTIVE flag may not get
1069 * cleared and the node won't get destroyed.
1071 error = cache_vget(v->a_tnch, v->a_cred, LK_SHARED, &tvp);
1073 tnode = VP_TO_TMPFS_NODE(tvp);
1079 /* Disallow cross-device renames.
1080 * XXX Why isn't this done by the caller? */
1081 if (fvp->v_mount != tdvp->v_mount ||
1082 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1087 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1088 tdnode = VP_TO_TMPFS_DIR(tdvp);
1090 /* If source and target are the same file, there is nothing to do. */
1096 fdnode = VP_TO_TMPFS_DIR(fdvp);
1097 fnode = VP_TO_TMPFS_NODE(fvp);
1098 TMPFS_NODE_LOCK(fdnode);
1099 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
1100 TMPFS_NODE_UNLOCK(fdnode); /* XXX depend on namecache lock */
1102 /* Avoid manipulating '.' and '..' entries. */
1107 KKASSERT(de->td_node == fnode);
1110 * If replacing an entry in the target directory and that entry
1111 * is a directory, it must be empty.
1113 * Kern_rename gurantees the destination to be a directory
1114 * if the source is one (it does?).
1117 KKASSERT(tnode != NULL);
1119 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1120 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1125 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1126 if (tnode->tn_size > 0) {
1130 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1133 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1137 KKASSERT(fnode->tn_type != VDIR &&
1138 tnode->tn_type != VDIR);
1142 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1143 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1149 * Ensure that we have enough memory to hold the new name, if it
1150 * has to be changed.
1152 if (fncp->nc_nlen != tncp->nc_nlen ||
1153 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
1154 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
1155 M_WAITOK | M_NULLOK);
1156 if (newname == NULL) {
1160 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1161 newname[tncp->nc_nlen] = '\0';
1167 * Unlink entry from source directory. Note that the kernel has
1168 * already checked for illegal recursion cases (renaming a directory
1169 * into a subdirectory of itself).
1171 if (fdnode != tdnode) {
1172 tmpfs_dir_detach(fdnode, de);
1174 /* XXX depend on namecache lock */
1175 TMPFS_NODE_LOCK(fdnode);
1176 KKASSERT(de == tmpfs_dir_lookup(fdnode, fnode, fncp));
1177 RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de);
1178 RB_REMOVE(tmpfs_dirtree_cookie,
1179 &fdnode->tn_dir.tn_cookietree, de);
1180 TMPFS_NODE_UNLOCK(fdnode);
1184 * Handle any name change. Swap with newname, we will
1185 * deallocate it at the end.
1187 if (newname != NULL) {
1189 TMPFS_NODE_LOCK(fnode);
1190 fnode->tn_status |= TMPFS_NODE_CHANGED;
1191 TMPFS_NODE_UNLOCK(fnode);
1193 oldname = de->td_name;
1194 de->td_name = newname;
1195 de->td_namelen = (uint16_t)tncp->nc_nlen;
1200 * If we are overwriting an entry, we have to remove the old one
1201 * from the target directory.
1204 /* Remove the old entry from the target directory. */
1205 TMPFS_NODE_LOCK(tdnode);
1206 tde = tmpfs_dir_lookup(tdnode, tnode, tncp);
1207 tmpfs_dir_detach(tdnode, tde);
1208 TMPFS_NODE_UNLOCK(tdnode);
1209 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1212 * Free the directory entry we just deleted. Note that the
1213 * node referred by it will not be removed until the vnode is
1216 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1217 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1221 * Link entry to target directory. If the entry
1222 * represents a directory move the parent linkage
1225 if (fdnode != tdnode) {
1226 if (de->td_node->tn_type == VDIR) {
1227 TMPFS_VALIDATE_DIR(fnode);
1229 tmpfs_dir_attach(tdnode, de);
1231 TMPFS_NODE_LOCK(tdnode);
1232 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1233 RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de);
1234 RB_INSERT(tmpfs_dirtree_cookie,
1235 &tdnode->tn_dir.tn_cookietree, de);
1236 TMPFS_NODE_UNLOCK(tdnode);
1243 kfree(newname, tmp->tm_name_zone);
1246 cache_rename(v->a_fnch, v->a_tnch);
1247 tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1248 tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1249 if (fnode->tn_vnode)
1250 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1261 /* --------------------------------------------------------------------- */
1264 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1266 struct vnode *dvp = v->a_dvp;
1267 struct vnode **vpp = v->a_vpp;
1268 struct namecache *ncp = v->a_nch->ncp;
1269 struct vattr *vap = v->a_vap;
1270 struct ucred *cred = v->a_cred;
1276 KKASSERT(vap->va_type == VDIR);
1278 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1280 cache_setunresolved(v->a_nch);
1281 cache_setvp(v->a_nch, *vpp);
1282 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1287 /* --------------------------------------------------------------------- */
1290 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1292 struct vnode *dvp = v->a_dvp;
1293 struct namecache *ncp = v->a_nch->ncp;
1295 struct tmpfs_dirent *de;
1296 struct tmpfs_mount *tmp;
1297 struct tmpfs_node *dnode;
1298 struct tmpfs_node *node;
1305 * We have to acquire the vp from v->a_nch because we will likely
1306 * unresolve the namecache entry, and a vrele/vput is needed to
1307 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1309 * We have to use vget to clear any inactive state on the vnode,
1310 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1311 * will not get called when we release it.
1313 error = cache_vget(v->a_nch, v->a_cred, LK_SHARED, &vp);
1314 KKASSERT(error == 0);
1318 * Prevalidate so we don't hit an assertion later
1320 if (vp->v_type != VDIR) {
1325 tmp = VFS_TO_TMPFS(dvp->v_mount);
1326 dnode = VP_TO_TMPFS_DIR(dvp);
1327 node = VP_TO_TMPFS_DIR(vp);
1330 * Directories with more than two entries ('.' and '..') cannot
1333 if (node->tn_size > 0) {
1338 if ((dnode->tn_flags & APPEND)
1339 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1345 * This invariant holds only if we are not trying to
1346 * remove "..". We checked for that above so this is safe now.
1348 KKASSERT(node->tn_dir.tn_parent == dnode);
1351 * Get the directory entry associated with node (vp). This
1352 * was filled by tmpfs_lookup while looking up the entry.
1354 TMPFS_NODE_LOCK(dnode);
1355 de = tmpfs_dir_lookup(dnode, node, ncp);
1356 KKASSERT(TMPFS_DIRENT_MATCHES(de, ncp->nc_name, ncp->nc_nlen));
1358 /* Check flags to see if we are allowed to remove the directory. */
1359 if ((dnode->tn_flags & APPEND) ||
1360 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1362 TMPFS_NODE_UNLOCK(dnode);
1366 /* Detach the directory entry from the directory (dnode). */
1367 tmpfs_dir_detach(dnode, de);
1368 TMPFS_NODE_UNLOCK(dnode);
1370 /* No vnode should be allocated for this entry from this point */
1371 TMPFS_NODE_LOCK(node);
1372 TMPFS_ASSERT_ELOCKED(node);
1373 TMPFS_NODE_LOCK(dnode);
1374 TMPFS_ASSERT_ELOCKED(dnode);
1377 * Must set parent linkage to NULL (tested by ncreate to disallow
1378 * the creation of new files/dirs in a deleted directory)
1380 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1381 TMPFS_NODE_MODIFIED;
1383 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1384 TMPFS_NODE_MODIFIED;
1386 TMPFS_NODE_UNLOCK(dnode);
1387 TMPFS_NODE_UNLOCK(node);
1389 /* Free the directory entry we just deleted. Note that the node
1390 * referred by it will not be removed until the vnode is really
1392 tmpfs_free_dirent(tmp, de);
1394 /* Release the deleted vnode (will destroy the node, notify
1395 * interested parties and clean it from the cache). */
1397 TMPFS_NODE_LOCK(dnode);
1398 dnode->tn_status |= TMPFS_NODE_CHANGED;
1399 TMPFS_NODE_UNLOCK(dnode);
1402 cache_unlink(v->a_nch);
1403 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1412 /* --------------------------------------------------------------------- */
1415 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1417 struct vnode *dvp = v->a_dvp;
1418 struct vnode **vpp = v->a_vpp;
1419 struct namecache *ncp = v->a_nch->ncp;
1420 struct vattr *vap = v->a_vap;
1421 struct ucred *cred = v->a_cred;
1422 char *target = v->a_target;
1425 vap->va_type = VLNK;
1426 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1428 tmpfs_knote(*vpp, NOTE_WRITE);
1429 cache_setunresolved(v->a_nch);
1430 cache_setvp(v->a_nch, *vpp);
1435 /* --------------------------------------------------------------------- */
1438 tmpfs_readdir(struct vop_readdir_args *v)
1440 struct vnode *vp = v->a_vp;
1441 struct uio *uio = v->a_uio;
1442 int *eofflag = v->a_eofflag;
1443 off_t **cookies = v->a_cookies;
1444 int *ncookies = v->a_ncookies;
1445 struct tmpfs_mount *tmp;
1449 struct tmpfs_node *node;
1451 /* This operation only makes sense on directory nodes. */
1452 if (vp->v_type != VDIR) {
1456 tmp = VFS_TO_TMPFS(vp->v_mount);
1457 node = VP_TO_TMPFS_DIR(vp);
1458 startoff = uio->uio_offset;
1460 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1461 error = tmpfs_dir_getdotdent(node, uio);
1463 TMPFS_NODE_LOCK_SH(node);
1469 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1470 /* may lock parent, cannot hold node lock */
1471 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1473 TMPFS_NODE_LOCK_SH(node);
1479 TMPFS_NODE_LOCK_SH(node);
1480 error = tmpfs_dir_getdents(node, uio, &cnt);
1483 KKASSERT(error >= -1);
1488 if (eofflag != NULL)
1490 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1492 /* Update NFS-related variables. */
1493 if (error == 0 && cookies != NULL && ncookies != NULL) {
1495 off_t off = startoff;
1496 struct tmpfs_dirent *de = NULL;
1499 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1501 for (i = 0; i < cnt; i++) {
1502 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1503 if (off == TMPFS_DIRCOOKIE_DOT) {
1504 off = TMPFS_DIRCOOKIE_DOTDOT;
1506 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1507 de = RB_MIN(tmpfs_dirtree_cookie,
1508 &node->tn_dir.tn_cookietree);
1509 } else if (de != NULL) {
1510 de = RB_NEXT(tmpfs_dirtree_cookie,
1511 &node->tn_dir.tn_cookietree, de);
1513 de = tmpfs_dir_lookupbycookie(node,
1515 KKASSERT(de != NULL);
1516 de = RB_NEXT(tmpfs_dirtree_cookie,
1517 &node->tn_dir.tn_cookietree, de);
1520 off = TMPFS_DIRCOOKIE_EOF;
1522 off = tmpfs_dircookie(de);
1524 (*cookies)[i] = off;
1526 KKASSERT(uio->uio_offset == off);
1528 TMPFS_NODE_UNLOCK(node);
1530 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1531 TMPFS_NODE_LOCK(node);
1532 node->tn_status |= TMPFS_NODE_ACCESSED;
1533 TMPFS_NODE_UNLOCK(node);
1538 /* --------------------------------------------------------------------- */
1541 tmpfs_readlink(struct vop_readlink_args *v)
1543 struct vnode *vp = v->a_vp;
1544 struct uio *uio = v->a_uio;
1546 struct tmpfs_node *node;
1548 KKASSERT(uio->uio_offset == 0);
1549 KKASSERT(vp->v_type == VLNK);
1551 node = VP_TO_TMPFS_NODE(vp);
1552 TMPFS_NODE_LOCK_SH(node);
1553 error = uiomove(node->tn_link,
1554 MIN(node->tn_size, uio->uio_resid), uio);
1555 TMPFS_NODE_UNLOCK(node);
1556 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1557 TMPFS_NODE_LOCK(node);
1558 node->tn_status |= TMPFS_NODE_ACCESSED;
1559 TMPFS_NODE_UNLOCK(node);
1564 /* --------------------------------------------------------------------- */
1567 tmpfs_inactive(struct vop_inactive_args *v)
1569 struct vnode *vp = v->a_vp;
1570 struct tmpfs_node *node;
1574 lwkt_gettoken(&mp->mnt_token);
1575 node = VP_TO_TMPFS_NODE(vp);
1582 lwkt_reltoken(&mp->mnt_token);
1587 * Get rid of unreferenced deleted vnodes sooner rather than
1588 * later so the data memory can be recovered immediately.
1590 * We must truncate the vnode to prevent the normal reclamation
1591 * path from flushing the data for the removed file to disk.
1593 TMPFS_NODE_LOCK(node);
1594 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1595 node->tn_links == 0)
1597 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1598 TMPFS_NODE_UNLOCK(node);
1599 if (node->tn_type == VREG)
1600 tmpfs_truncate(vp, 0);
1603 TMPFS_NODE_UNLOCK(node);
1605 lwkt_reltoken(&mp->mnt_token);
1610 /* --------------------------------------------------------------------- */
1613 tmpfs_reclaim(struct vop_reclaim_args *v)
1615 struct vnode *vp = v->a_vp;
1616 struct tmpfs_mount *tmp;
1617 struct tmpfs_node *node;
1621 lwkt_gettoken(&mp->mnt_token);
1623 node = VP_TO_TMPFS_NODE(vp);
1624 tmp = VFS_TO_TMPFS(vp->v_mount);
1625 KKASSERT(mp == tmp->tm_mount);
1630 * If the node referenced by this vnode was deleted by the
1631 * user, we must free its associated data structures now that
1632 * the vnode is being reclaimed.
1634 * Directories have an extra link ref.
1636 TMPFS_NODE_LOCK(node);
1637 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1638 node->tn_links == 0) {
1639 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1640 tmpfs_free_node(tmp, node);
1643 TMPFS_NODE_UNLOCK(node);
1645 lwkt_reltoken(&mp->mnt_token);
1647 KKASSERT(vp->v_data == NULL);
1651 /* --------------------------------------------------------------------- */
1654 tmpfs_mountctl(struct vop_mountctl_args *ap)
1656 struct tmpfs_mount *tmp;
1660 mp = ap->a_head.a_ops->head.vv_mount;
1661 lwkt_gettoken(&mp->mnt_token);
1664 case (MOUNTCTL_SET_EXPORT):
1665 tmp = (struct tmpfs_mount *) mp->mnt_data;
1667 if (ap->a_ctllen != sizeof(struct export_args))
1670 rc = vfs_export(mp, &tmp->tm_export,
1671 (const struct export_args *) ap->a_ctl);
1674 rc = vop_stdmountctl(ap);
1678 lwkt_reltoken(&mp->mnt_token);
1682 /* --------------------------------------------------------------------- */
1685 tmpfs_print(struct vop_print_args *v)
1687 struct vnode *vp = v->a_vp;
1689 struct tmpfs_node *node;
1691 node = VP_TO_TMPFS_NODE(vp);
1693 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1694 node, node->tn_flags, node->tn_links);
1695 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1696 node->tn_mode, node->tn_uid, node->tn_gid,
1697 (uintmax_t)node->tn_size, node->tn_status);
1699 if (vp->v_type == VFIFO)
1707 /* --------------------------------------------------------------------- */
1710 tmpfs_pathconf(struct vop_pathconf_args *v)
1712 int name = v->a_name;
1713 register_t *retval = v->a_retval;
1736 case _PC_CHOWN_RESTRICTED:
1748 case _PC_FILESIZEBITS:
1749 *retval = 0; /* XXX Don't know which value should I return. */
1759 /************************************************************************
1761 ************************************************************************/
1763 static void filt_tmpfsdetach(struct knote *kn);
1764 static int filt_tmpfsread(struct knote *kn, long hint);
1765 static int filt_tmpfswrite(struct knote *kn, long hint);
1766 static int filt_tmpfsvnode(struct knote *kn, long hint);
1768 static struct filterops tmpfsread_filtops =
1769 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1770 NULL, filt_tmpfsdetach, filt_tmpfsread };
1771 static struct filterops tmpfswrite_filtops =
1772 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1773 NULL, filt_tmpfsdetach, filt_tmpfswrite };
1774 static struct filterops tmpfsvnode_filtops =
1775 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1776 NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1779 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1781 struct vnode *vp = ap->a_vp;
1782 struct knote *kn = ap->a_kn;
1784 switch (kn->kn_filter) {
1786 kn->kn_fop = &tmpfsread_filtops;
1789 kn->kn_fop = &tmpfswrite_filtops;
1792 kn->kn_fop = &tmpfsvnode_filtops;
1795 return (EOPNOTSUPP);
1798 kn->kn_hook = (caddr_t)vp;
1800 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1806 filt_tmpfsdetach(struct knote *kn)
1808 struct vnode *vp = (void *)kn->kn_hook;
1810 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1814 filt_tmpfsread(struct knote *kn, long hint)
1816 struct vnode *vp = (void *)kn->kn_hook;
1817 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1820 if (hint == NOTE_REVOKE) {
1821 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1826 * Interlock against MP races when performing this function.
1828 TMPFS_NODE_LOCK_SH(node);
1829 off = node->tn_size - kn->kn_fp->f_offset;
1830 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1831 if (kn->kn_sfflags & NOTE_OLDAPI) {
1832 TMPFS_NODE_UNLOCK(node);
1835 if (kn->kn_data == 0) {
1836 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1838 TMPFS_NODE_UNLOCK(node);
1839 return (kn->kn_data != 0);
1843 filt_tmpfswrite(struct knote *kn, long hint)
1845 if (hint == NOTE_REVOKE)
1846 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1852 filt_tmpfsvnode(struct knote *kn, long hint)
1854 if (kn->kn_sfflags & hint)
1855 kn->kn_fflags |= hint;
1856 if (hint == NOTE_REVOKE) {
1857 kn->kn_flags |= (EV_EOF | EV_NODATA);
1860 return (kn->kn_fflags != 0);
1864 /* --------------------------------------------------------------------- */
1867 * vnode operations vector used for files stored in a tmpfs file system.
1869 struct vop_ops tmpfs_vnode_vops = {
1870 .vop_default = vop_defaultop,
1871 .vop_getpages = vop_stdgetpages,
1872 .vop_putpages = vop_stdputpages,
1873 .vop_ncreate = tmpfs_ncreate,
1874 .vop_nresolve = tmpfs_nresolve,
1875 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1876 .vop_nmknod = tmpfs_nmknod,
1877 .vop_open = tmpfs_open,
1878 .vop_close = tmpfs_close,
1879 .vop_access = tmpfs_access,
1880 .vop_getattr = tmpfs_getattr,
1881 .vop_setattr = tmpfs_setattr,
1882 .vop_read = tmpfs_read,
1883 .vop_write = tmpfs_write,
1884 .vop_fsync = tmpfs_fsync,
1885 .vop_mountctl = tmpfs_mountctl,
1886 .vop_nremove = tmpfs_nremove,
1887 .vop_nlink = tmpfs_nlink,
1888 .vop_nrename = tmpfs_nrename,
1889 .vop_nmkdir = tmpfs_nmkdir,
1890 .vop_nrmdir = tmpfs_nrmdir,
1891 .vop_nsymlink = tmpfs_nsymlink,
1892 .vop_readdir = tmpfs_readdir,
1893 .vop_readlink = tmpfs_readlink,
1894 .vop_inactive = tmpfs_inactive,
1895 .vop_reclaim = tmpfs_reclaim,
1896 .vop_print = tmpfs_print,
1897 .vop_pathconf = tmpfs_pathconf,
1898 .vop_bmap = tmpfs_bmap,
1899 .vop_strategy = tmpfs_strategy,
1900 .vop_advlock = tmpfs_advlock,
1901 .vop_kqfilter = tmpfs_kqfilter