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>
68 static void tmpfs_strategy_done(struct bio *bio);
72 tmpfs_knote(struct vnode *vp, int flags)
75 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
79 /* --------------------------------------------------------------------- */
82 tmpfs_nresolve(struct vop_nresolve_args *v)
84 struct vnode *dvp = v->a_dvp;
85 struct vnode *vp = NULL;
86 struct namecache *ncp = v->a_nch->ncp;
87 struct tmpfs_node *tnode;
89 struct tmpfs_dirent *de;
90 struct tmpfs_node *dnode;
95 dnode = VP_TO_TMPFS_DIR(dvp);
97 TMPFS_NODE_LOCK_SH(dnode);
98 de = tmpfs_dir_lookup(dnode, NULL, ncp);
103 * Allocate a vnode for the node we found.
106 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
107 LK_EXCLUSIVE | LK_RETRY, &vp);
111 TMPFS_NODE_UNLOCK(dnode);
113 if ((dnode->tn_status & TMPFS_NODE_ACCESSED) == 0) {
114 TMPFS_NODE_LOCK(dnode);
115 dnode->tn_status |= TMPFS_NODE_ACCESSED;
116 TMPFS_NODE_UNLOCK(dnode);
120 * Store the result of this lookup in the cache. Avoid this if the
121 * request was for creation, as it does not improve timings on
126 cache_setvp(v->a_nch, vp);
128 } else if (error == ENOENT) {
129 cache_setvp(v->a_nch, NULL);
135 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
137 struct vnode *dvp = v->a_dvp;
138 struct vnode **vpp = v->a_vpp;
139 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
140 struct ucred *cred = v->a_cred;
148 /* Check accessibility of requested node as a first step. */
149 error = VOP_ACCESS(dvp, VEXEC, cred);
153 if (dnode->tn_dir.tn_parent != NULL) {
154 /* Allocate a new vnode on the matching entry. */
155 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
156 LK_EXCLUSIVE | LK_RETRY, vpp);
161 return (*vpp == NULL) ? ENOENT : 0;
164 /* --------------------------------------------------------------------- */
167 tmpfs_ncreate(struct vop_ncreate_args *v)
169 struct vnode *dvp = v->a_dvp;
170 struct vnode **vpp = v->a_vpp;
171 struct namecache *ncp = v->a_nch->ncp;
172 struct vattr *vap = v->a_vap;
173 struct ucred *cred = v->a_cred;
179 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
181 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
183 cache_setunresolved(v->a_nch);
184 cache_setvp(v->a_nch, *vpp);
185 tmpfs_knote(dvp, NOTE_WRITE);
189 /* --------------------------------------------------------------------- */
192 tmpfs_nmknod(struct vop_nmknod_args *v)
194 struct vnode *dvp = v->a_dvp;
195 struct vnode **vpp = v->a_vpp;
196 struct namecache *ncp = v->a_nch->ncp;
197 struct vattr *vap = v->a_vap;
198 struct ucred *cred = v->a_cred;
201 if (vap->va_type != VBLK && vap->va_type != VCHR &&
202 vap->va_type != VFIFO) {
206 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
208 cache_setunresolved(v->a_nch);
209 cache_setvp(v->a_nch, *vpp);
210 tmpfs_knote(dvp, NOTE_WRITE);
215 /* --------------------------------------------------------------------- */
218 tmpfs_open(struct vop_open_args *v)
220 struct vnode *vp = v->a_vp;
221 int mode = v->a_mode;
222 struct tmpfs_node *node;
225 node = VP_TO_TMPFS_NODE(vp);
228 /* The file is still active but all its names have been removed
229 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
230 * it is about to die. */
231 if (node->tn_links < 1)
235 /* If the file is marked append-only, deny write requests. */
236 if ((node->tn_flags & APPEND) &&
237 (mode & (FWRITE | O_APPEND)) == FWRITE) {
240 error = (vop_stdopen(v));
246 /* --------------------------------------------------------------------- */
249 tmpfs_close(struct vop_close_args *v)
251 struct vnode *vp = v->a_vp;
252 struct tmpfs_node *node;
255 node = VP_TO_TMPFS_NODE(vp);
257 if (node->tn_links > 0) {
259 * Update node times. No need to do it if the node has
260 * been deleted, because it will vanish after we return.
265 error = vop_stdclose(v);
270 /* --------------------------------------------------------------------- */
273 tmpfs_access(struct vop_access_args *v)
275 struct vnode *vp = v->a_vp;
277 struct tmpfs_node *node;
279 node = VP_TO_TMPFS_NODE(vp);
281 switch (vp->v_type) {
287 if ((v->a_mode & VWRITE) &&
288 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
308 if ((v->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
313 error = vop_helper_access(v, node->tn_uid, node->tn_gid,
319 /* --------------------------------------------------------------------- */
322 tmpfs_getattr(struct vop_getattr_args *v)
324 struct vnode *vp = v->a_vp;
325 struct vattr *vap = v->a_vap;
326 struct tmpfs_node *node;
328 node = VP_TO_TMPFS_NODE(vp);
332 TMPFS_NODE_LOCK_SH(node);
333 vap->va_type = vp->v_type;
334 vap->va_mode = node->tn_mode;
335 vap->va_nlink = node->tn_links;
336 vap->va_uid = node->tn_uid;
337 vap->va_gid = node->tn_gid;
338 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
339 vap->va_fileid = node->tn_id;
340 vap->va_size = node->tn_size;
341 vap->va_blocksize = PAGE_SIZE;
342 vap->va_atime.tv_sec = node->tn_atime;
343 vap->va_atime.tv_nsec = node->tn_atimensec;
344 vap->va_mtime.tv_sec = node->tn_mtime;
345 vap->va_mtime.tv_nsec = node->tn_mtimensec;
346 vap->va_ctime.tv_sec = node->tn_ctime;
347 vap->va_ctime.tv_nsec = node->tn_ctimensec;
348 vap->va_gen = node->tn_gen;
349 vap->va_flags = node->tn_flags;
350 if (vp->v_type == VBLK || vp->v_type == VCHR) {
351 vap->va_rmajor = umajor(node->tn_rdev);
352 vap->va_rminor = uminor(node->tn_rdev);
354 vap->va_bytes = round_page(node->tn_size);
356 TMPFS_NODE_UNLOCK(node);
361 /* --------------------------------------------------------------------- */
364 tmpfs_setattr(struct vop_setattr_args *v)
366 struct vnode *vp = v->a_vp;
367 struct vattr *vap = v->a_vap;
368 struct ucred *cred = v->a_cred;
369 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
373 TMPFS_NODE_LOCK(node);
374 if (error == 0 && (vap->va_flags != VNOVAL)) {
375 error = tmpfs_chflags(vp, vap->va_flags, cred);
376 kflags |= NOTE_ATTRIB;
379 if (error == 0 && (vap->va_size != VNOVAL)) {
380 if (vap->va_size > node->tn_size)
381 kflags |= NOTE_WRITE | NOTE_EXTEND;
383 kflags |= NOTE_WRITE;
384 error = tmpfs_chsize(vp, vap->va_size, cred);
387 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
388 vap->va_gid != (gid_t)VNOVAL)) {
389 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
390 kflags |= NOTE_ATTRIB;
393 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
394 error = tmpfs_chmod(vp, vap->va_mode, cred);
395 kflags |= NOTE_ATTRIB;
398 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
399 vap->va_atime.tv_nsec != VNOVAL) ||
400 (vap->va_mtime.tv_sec != VNOVAL &&
401 vap->va_mtime.tv_nsec != VNOVAL) )) {
402 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
403 vap->va_vaflags, cred);
404 kflags |= NOTE_ATTRIB;
408 * Update the node times. We give preference to the error codes
409 * generated by this function rather than the ones that may arise
413 TMPFS_NODE_UNLOCK(node);
414 tmpfs_knote(vp, kflags);
419 /* --------------------------------------------------------------------- */
422 * fsync is usually a NOP, but we must take action when unmounting or
426 tmpfs_fsync(struct vop_fsync_args *v)
428 struct tmpfs_node *node;
429 struct vnode *vp = v->a_vp;
431 node = VP_TO_TMPFS_NODE(vp);
434 if (vp->v_type == VREG) {
435 if (vp->v_flag & VRECLAIMED) {
436 if (node->tn_links == 0)
437 tmpfs_truncate(vp, 0);
439 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
445 /* --------------------------------------------------------------------- */
448 tmpfs_read (struct vop_read_args *ap)
451 struct vnode *vp = ap->a_vp;
452 struct uio *uio = ap->a_uio;
453 struct tmpfs_node *node;
463 if (uio->uio_offset < 0)
465 if (vp->v_type != VREG)
469 * Extract node, try to shortcut the operation through
470 * the VM page cache, allowing us to avoid buffer cache
473 node = VP_TO_TMPFS_NODE(vp);
474 resid = uio->uio_resid;
475 error = vop_helper_read_shortcut(ap);
478 if (uio->uio_resid == 0) {
485 * Fall-through to our normal read code.
487 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
489 * Use buffer cache I/O (via tmpfs_strategy)
491 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
492 base_offset = (off_t)uio->uio_offset - offset;
493 bp = getcacheblk(vp, base_offset, TMPFS_BLKSIZE, 0);
495 error = bread(vp, base_offset, TMPFS_BLKSIZE, &bp);
498 kprintf("tmpfs_read bread error %d\n", error);
503 * tmpfs pretty much fiddles directly with the VM
504 * system, don't let it exhaust it or we won't play
505 * nice with other processes.
507 * Only do this if the VOP is coming from a normal
508 * read/write. The VM system handles the case for
511 if (uio->uio_segflg != UIO_NOCOPY)
514 bp->b_flags |= B_CLUSTEROK;
517 * Figure out how many bytes we can actually copy this loop.
519 len = TMPFS_BLKSIZE - offset;
520 if (len > uio->uio_resid)
521 len = uio->uio_resid;
522 if (len > node->tn_size - uio->uio_offset)
523 len = (size_t)(node->tn_size - uio->uio_offset);
525 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
528 kprintf("tmpfs_read uiomove error %d\n", error);
534 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
535 TMPFS_NODE_LOCK(node);
536 node->tn_status |= TMPFS_NODE_ACCESSED;
537 TMPFS_NODE_UNLOCK(node);
543 tmpfs_write (struct vop_write_args *ap)
546 struct vnode *vp = ap->a_vp;
547 struct uio *uio = ap->a_uio;
548 struct thread *td = uio->uio_td;
549 struct tmpfs_node *node;
562 if (uio->uio_resid == 0) {
566 node = VP_TO_TMPFS_NODE(vp);
568 if (vp->v_type != VREG)
570 seqcount = ap->a_ioflag >> 16;
572 oldsize = node->tn_size;
573 if (ap->a_ioflag & IO_APPEND)
574 uio->uio_offset = node->tn_size;
577 * Check for illegal write offsets.
579 if (uio->uio_offset + uio->uio_resid >
580 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) {
585 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN).
587 if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) {
588 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
592 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
593 ksignal(td->td_proc, SIGXFSZ);
600 * Extend the file's size if necessary
602 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
604 while (uio->uio_resid > 0) {
606 * Don't completely blow out running buffer I/O
607 * when being hit from the pageout daemon.
609 if (uio->uio_segflg == UIO_NOCOPY &&
610 (ap->a_ioflag & IO_RECURSE) == 0) {
611 bwillwrite(TMPFS_BLKSIZE);
615 * Use buffer cache I/O (via tmpfs_strategy)
617 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
618 base_offset = (off_t)uio->uio_offset - offset;
619 len = TMPFS_BLKSIZE - offset;
620 if (len > uio->uio_resid)
621 len = uio->uio_resid;
623 if ((uio->uio_offset + len) > node->tn_size) {
624 trivial = (uio->uio_offset <= node->tn_size);
625 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
631 * Read to fill in any gaps. Theoretically we could
632 * optimize this if the write covers the entire buffer
633 * and is not a UIO_NOCOPY write, however this can lead
634 * to a security violation exposing random kernel memory
635 * (whatever junk was in the backing VM pages before).
637 * So just use bread() to do the right thing.
639 error = bread(vp, base_offset, TMPFS_BLKSIZE, &bp);
640 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
642 kprintf("tmpfs_write uiomove error %d\n", error);
647 if (uio->uio_offset > node->tn_size) {
648 node->tn_size = uio->uio_offset;
649 kflags |= NOTE_EXTEND;
651 kflags |= NOTE_WRITE;
654 * Always try to flush the page in the UIO_NOCOPY case. This
655 * can come from the pageout daemon or during vnode eviction.
656 * It is not necessarily going to be marked IO_ASYNC/IO_SYNC.
658 * For the normal case we buwrite(), dirtying the underlying
659 * VM pages instead of dirtying the buffer and releasing the
660 * buffer as a clean buffer. This allows tmpfs to use
661 * essentially all available memory to cache file data.
662 * If we used bdwrite() the buffer cache would wind up
663 * flushing the data to swap too quickly.
665 * But because tmpfs can seriously load the VM system we
666 * fall-back to using bdwrite() when free memory starts
667 * to get low. This shifts the load away from the VM system
668 * and makes tmpfs act more like a normal filesystem with
669 * regards to disk activity.
671 * tmpfs pretty much fiddles directly with the VM
672 * system, don't let it exhaust it or we won't play
673 * nice with other processes. Only do this if the
674 * VOP is coming from a normal read/write. The VM system
675 * handles the case for UIO_NOCOPY.
677 bp->b_flags |= B_CLUSTEROK;
678 if (uio->uio_segflg == UIO_NOCOPY) {
680 * Flush from the pageout daemon, deal with
681 * potentially very heavy tmpfs write activity
682 * causing long stalls in the pageout daemon
683 * before pages get to free/cache.
685 * (a) Under severe pressure setting B_DIRECT will
686 * cause a buffer release to try to free the
689 * (b) Under modest memory pressure the B_RELBUF
690 * alone is sufficient to get the pages moved
691 * to the cache. We could also force this by
692 * setting B_NOTMETA but that might have other
693 * unintended side-effects (e.g. setting
694 * PG_NOTMETA on the VM page).
696 * Hopefully this will unblock the VM system more
697 * quickly under extreme tmpfs write load.
699 if (vm_page_count_min(vm_page_free_hysteresis))
700 bp->b_flags |= B_DIRECT;
701 bp->b_flags |= B_AGE | B_RELBUF;
702 bp->b_act_count = 0; /* buffer->deactivate pgs */
704 } else if (vm_page_count_target()) {
706 * Normal (userland) write but we are low on memory,
707 * run the buffer the buffer cache.
709 bp->b_act_count = 0; /* buffer->deactivate pgs */
713 * Otherwise run the buffer directly through to the
717 /*vm_wait_nominal();*/
721 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
728 (void)tmpfs_reg_resize(vp, oldsize, trivial);
729 kflags &= ~NOTE_EXTEND;
735 * Currently we don't set the mtime on files modified via mmap()
736 * because we can't tell the difference between those modifications
737 * and an attempt by the pageout daemon to flush tmpfs pages to
740 * This is because in order to defer flushes as long as possible
741 * buwrite() works by marking the underlying VM pages dirty in
742 * order to be able to dispose of the buffer cache buffer without
745 TMPFS_NODE_LOCK(node);
746 if (uio->uio_segflg != UIO_NOCOPY)
747 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED;
749 node->tn_status |= TMPFS_NODE_CHANGED;
751 if (node->tn_mode & (S_ISUID | S_ISGID)) {
752 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
753 node->tn_mode &= ~(S_ISUID | S_ISGID);
755 TMPFS_NODE_UNLOCK(node);
757 tmpfs_knote(vp, kflags);
763 tmpfs_advlock (struct vop_advlock_args *ap)
765 struct tmpfs_node *node;
766 struct vnode *vp = ap->a_vp;
769 node = VP_TO_TMPFS_NODE(vp);
770 error = (lf_advlock(ap, &node->tn_advlock, node->tn_size));
776 * The strategy function is typically only called when memory pressure
777 * forces the system to attempt to pageout pages. It can also be called
778 * by [n]vtruncbuf() when a truncation cuts a page in half. Normal write
782 tmpfs_strategy(struct vop_strategy_args *ap)
784 struct bio *bio = ap->a_bio;
786 struct buf *bp = bio->bio_buf;
787 struct vnode *vp = ap->a_vp;
788 struct tmpfs_node *node;
793 if (vp->v_type != VREG) {
794 bp->b_resid = bp->b_bcount;
795 bp->b_flags |= B_ERROR | B_INVAL;
796 bp->b_error = EINVAL;
801 node = VP_TO_TMPFS_NODE(vp);
803 uobj = node->tn_reg.tn_aobj;
806 * Don't bother flushing to swap if there is no swap, just
807 * ensure that the pages are marked as needing a commit (still).
809 if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) {
810 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
811 m = bp->b_xio.xio_pages[i];
812 vm_page_need_commit(m);
818 nbio = push_bio(bio);
819 nbio->bio_done = tmpfs_strategy_done;
820 nbio->bio_offset = bio->bio_offset;
821 swap_pager_strategy(uobj, nbio);
827 * If we were unable to commit the pages to swap make sure they are marked
828 * as needing a commit (again). If we were, clear the flag to allow the
832 tmpfs_strategy_done(struct bio *bio)
840 if (bp->b_flags & B_ERROR) {
841 bp->b_flags &= ~B_ERROR;
844 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
845 m = bp->b_xio.xio_pages[i];
846 vm_page_need_commit(m);
849 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
850 m = bp->b_xio.xio_pages[i];
851 vm_page_clear_commit(m);
859 tmpfs_bmap(struct vop_bmap_args *ap)
861 if (ap->a_doffsetp != NULL)
862 *ap->a_doffsetp = ap->a_loffset;
863 if (ap->a_runp != NULL)
865 if (ap->a_runb != NULL)
871 /* --------------------------------------------------------------------- */
874 tmpfs_nremove(struct vop_nremove_args *v)
876 struct vnode *dvp = v->a_dvp;
877 struct namecache *ncp = v->a_nch->ncp;
880 struct tmpfs_dirent *de;
881 struct tmpfs_mount *tmp;
882 struct tmpfs_node *dnode;
883 struct tmpfs_node *node;
889 * We have to acquire the vp from v->a_nch because we will likely
890 * unresolve the namecache entry, and a vrele/vput is needed to
891 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
893 * We have to use vget to clear any inactive state on the vnode,
894 * otherwise the vnode may remain inactive and thus tmpfs_inactive
895 * will not get called when we release it.
897 error = cache_vget(v->a_nch, v->a_cred, LK_SHARED, &vp);
898 KKASSERT(vp->v_mount == dvp->v_mount);
899 KKASSERT(error == 0);
902 if (vp->v_type == VDIR) {
907 dnode = VP_TO_TMPFS_DIR(dvp);
908 node = VP_TO_TMPFS_NODE(vp);
909 tmp = VFS_TO_TMPFS(vp->v_mount);
911 TMPFS_NODE_LOCK(dnode);
912 de = tmpfs_dir_lookup(dnode, node, ncp);
918 /* Files marked as immutable or append-only cannot be deleted. */
919 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
920 (dnode->tn_flags & APPEND)) {
925 /* Remove the entry from the directory; as it is a file, we do not
926 * have to change the number of hard links of the directory. */
927 tmpfs_dir_detach(dnode, de);
929 /* Free the directory entry we just deleted. Note that the node
930 * referred by it will not be removed until the vnode is really
932 tmpfs_free_dirent(tmp, de);
934 if (node->tn_links > 0) {
935 TMPFS_NODE_LOCK(node);
936 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
938 TMPFS_NODE_UNLOCK(node);
941 cache_unlink(v->a_nch);
942 tmpfs_knote(vp, NOTE_DELETE);
946 TMPFS_NODE_UNLOCK(dnode);
948 tmpfs_knote(dvp, NOTE_WRITE);
955 /* --------------------------------------------------------------------- */
958 tmpfs_nlink(struct vop_nlink_args *v)
960 struct vnode *dvp = v->a_dvp;
961 struct vnode *vp = v->a_vp;
962 struct namecache *ncp = v->a_nch->ncp;
963 struct tmpfs_dirent *de;
964 struct tmpfs_node *node;
965 struct tmpfs_node *dnode;
969 if (dvp->v_mount != vp->v_mount)
973 KKASSERT(dvp != vp); /* XXX When can this be false? */
975 node = VP_TO_TMPFS_NODE(vp);
976 dnode = VP_TO_TMPFS_NODE(dvp);
977 TMPFS_NODE_LOCK(dnode);
979 /* XXX: Why aren't the following two tests done by the caller? */
981 /* Hard links of directories are forbidden. */
982 if (vp->v_type == VDIR) {
987 /* Cannot create cross-device links. */
988 if (dvp->v_mount != vp->v_mount) {
993 /* Ensure that we do not overflow the maximum number of links imposed
995 KKASSERT(node->tn_links <= LINK_MAX);
996 if (node->tn_links >= LINK_MAX) {
1001 /* We cannot create links of files marked immutable or append-only. */
1002 if (node->tn_flags & (IMMUTABLE | APPEND)) {
1007 /* Allocate a new directory entry to represent the node. */
1008 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
1009 ncp->nc_name, ncp->nc_nlen, &de);
1013 /* Insert the new directory entry into the appropriate directory. */
1014 tmpfs_dir_attach(dnode, de);
1016 /* vp link count has changed, so update node times. */
1018 TMPFS_NODE_LOCK(node);
1019 node->tn_status |= TMPFS_NODE_CHANGED;
1020 TMPFS_NODE_UNLOCK(node);
1023 tmpfs_knote(vp, NOTE_LINK);
1024 cache_setunresolved(v->a_nch);
1025 cache_setvp(v->a_nch, vp);
1029 TMPFS_NODE_UNLOCK(dnode);
1031 tmpfs_knote(dvp, NOTE_WRITE);
1035 /* --------------------------------------------------------------------- */
1038 tmpfs_nrename(struct vop_nrename_args *v)
1040 struct vnode *fdvp = v->a_fdvp;
1041 struct namecache *fncp = v->a_fnch->ncp;
1042 struct vnode *fvp = fncp->nc_vp;
1043 struct vnode *tdvp = v->a_tdvp;
1044 struct namecache *tncp = v->a_tnch->ncp;
1046 struct tmpfs_dirent *de, *tde;
1047 struct tmpfs_mount *tmp;
1048 struct tmpfs_node *fdnode;
1049 struct tmpfs_node *fnode;
1050 struct tmpfs_node *tnode;
1051 struct tmpfs_node *tdnode;
1058 KKASSERT(fdvp->v_mount == fvp->v_mount);
1061 * Because tvp can get overwritten we have to vget it instead of
1062 * just vref or use it, otherwise it's VINACTIVE flag may not get
1063 * cleared and the node won't get destroyed.
1065 error = cache_vget(v->a_tnch, v->a_cred, LK_SHARED, &tvp);
1067 tnode = VP_TO_TMPFS_NODE(tvp);
1073 /* Disallow cross-device renames.
1074 * XXX Why isn't this done by the caller? */
1075 if (fvp->v_mount != tdvp->v_mount ||
1076 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1081 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1082 tdnode = VP_TO_TMPFS_DIR(tdvp);
1084 /* If source and target are the same file, there is nothing to do. */
1090 fdnode = VP_TO_TMPFS_DIR(fdvp);
1091 fnode = VP_TO_TMPFS_NODE(fvp);
1092 TMPFS_NODE_LOCK(fdnode);
1093 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
1094 TMPFS_NODE_UNLOCK(fdnode); /* XXX depend on namecache lock */
1096 /* Avoid manipulating '.' and '..' entries. */
1101 KKASSERT(de->td_node == fnode);
1104 * If replacing an entry in the target directory and that entry
1105 * is a directory, it must be empty.
1107 * Kern_rename gurantees the destination to be a directory
1108 * if the source is one (it does?).
1111 KKASSERT(tnode != NULL);
1113 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1114 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1119 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1120 if (tnode->tn_size > 0) {
1124 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1127 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1131 KKASSERT(fnode->tn_type != VDIR &&
1132 tnode->tn_type != VDIR);
1136 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1137 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1143 * Ensure that we have enough memory to hold the new name, if it
1144 * has to be changed.
1146 if (fncp->nc_nlen != tncp->nc_nlen ||
1147 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
1148 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
1149 M_WAITOK | M_NULLOK);
1150 if (newname == NULL) {
1154 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1155 newname[tncp->nc_nlen] = '\0';
1161 * Unlink entry from source directory. Note that the kernel has
1162 * already checked for illegal recursion cases (renaming a directory
1163 * into a subdirectory of itself).
1165 if (fdnode != tdnode) {
1166 tmpfs_dir_detach(fdnode, de);
1168 /* XXX depend on namecache lock */
1169 TMPFS_NODE_LOCK(fdnode);
1170 KKASSERT(de == tmpfs_dir_lookup(fdnode, fnode, fncp));
1171 RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de);
1172 RB_REMOVE(tmpfs_dirtree_cookie,
1173 &fdnode->tn_dir.tn_cookietree, de);
1174 TMPFS_NODE_UNLOCK(fdnode);
1178 * Handle any name change. Swap with newname, we will
1179 * deallocate it at the end.
1181 if (newname != NULL) {
1183 TMPFS_NODE_LOCK(fnode);
1184 fnode->tn_status |= TMPFS_NODE_CHANGED;
1185 TMPFS_NODE_UNLOCK(fnode);
1187 oldname = de->td_name;
1188 de->td_name = newname;
1189 de->td_namelen = (uint16_t)tncp->nc_nlen;
1194 * If we are overwriting an entry, we have to remove the old one
1195 * from the target directory.
1198 /* Remove the old entry from the target directory. */
1199 TMPFS_NODE_LOCK(tdnode);
1200 tde = tmpfs_dir_lookup(tdnode, tnode, tncp);
1201 tmpfs_dir_detach(tdnode, tde);
1202 TMPFS_NODE_UNLOCK(tdnode);
1203 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1206 * Free the directory entry we just deleted. Note that the
1207 * node referred by it will not be removed until the vnode is
1210 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1211 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1215 * Link entry to target directory. If the entry
1216 * represents a directory move the parent linkage
1219 if (fdnode != tdnode) {
1220 if (de->td_node->tn_type == VDIR) {
1221 TMPFS_VALIDATE_DIR(fnode);
1223 tmpfs_dir_attach(tdnode, de);
1225 TMPFS_NODE_LOCK(tdnode);
1226 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1227 RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de);
1228 RB_INSERT(tmpfs_dirtree_cookie,
1229 &tdnode->tn_dir.tn_cookietree, de);
1230 TMPFS_NODE_UNLOCK(tdnode);
1237 kfree(newname, tmp->tm_name_zone);
1240 cache_rename(v->a_fnch, v->a_tnch);
1241 tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1242 tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1243 if (fnode->tn_vnode)
1244 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1255 /* --------------------------------------------------------------------- */
1258 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1260 struct vnode *dvp = v->a_dvp;
1261 struct vnode **vpp = v->a_vpp;
1262 struct namecache *ncp = v->a_nch->ncp;
1263 struct vattr *vap = v->a_vap;
1264 struct ucred *cred = v->a_cred;
1270 KKASSERT(vap->va_type == VDIR);
1272 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1274 cache_setunresolved(v->a_nch);
1275 cache_setvp(v->a_nch, *vpp);
1276 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1281 /* --------------------------------------------------------------------- */
1284 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1286 struct vnode *dvp = v->a_dvp;
1287 struct namecache *ncp = v->a_nch->ncp;
1289 struct tmpfs_dirent *de;
1290 struct tmpfs_mount *tmp;
1291 struct tmpfs_node *dnode;
1292 struct tmpfs_node *node;
1299 * We have to acquire the vp from v->a_nch because we will likely
1300 * unresolve the namecache entry, and a vrele/vput is needed to
1301 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1303 * We have to use vget to clear any inactive state on the vnode,
1304 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1305 * will not get called when we release it.
1307 error = cache_vget(v->a_nch, v->a_cred, LK_SHARED, &vp);
1308 KKASSERT(error == 0);
1312 * Prevalidate so we don't hit an assertion later
1314 if (vp->v_type != VDIR) {
1319 tmp = VFS_TO_TMPFS(dvp->v_mount);
1320 dnode = VP_TO_TMPFS_DIR(dvp);
1321 node = VP_TO_TMPFS_DIR(vp);
1324 * Directories with more than two entries ('.' and '..') cannot
1327 if (node->tn_size > 0) {
1332 if ((dnode->tn_flags & APPEND)
1333 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1339 * This invariant holds only if we are not trying to
1340 * remove "..". We checked for that above so this is safe now.
1342 KKASSERT(node->tn_dir.tn_parent == dnode);
1345 * Get the directory entry associated with node (vp). This
1346 * was filled by tmpfs_lookup while looking up the entry.
1348 TMPFS_NODE_LOCK(dnode);
1349 de = tmpfs_dir_lookup(dnode, node, ncp);
1350 KKASSERT(TMPFS_DIRENT_MATCHES(de, ncp->nc_name, ncp->nc_nlen));
1352 /* Check flags to see if we are allowed to remove the directory. */
1353 if ((dnode->tn_flags & APPEND) ||
1354 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1356 TMPFS_NODE_UNLOCK(dnode);
1360 /* Detach the directory entry from the directory (dnode). */
1361 tmpfs_dir_detach(dnode, de);
1362 TMPFS_NODE_UNLOCK(dnode);
1364 /* No vnode should be allocated for this entry from this point */
1365 TMPFS_NODE_LOCK(dnode);
1366 TMPFS_ASSERT_ELOCKED(dnode);
1367 TMPFS_NODE_LOCK(node);
1368 TMPFS_ASSERT_ELOCKED(node);
1371 * Must set parent linkage to NULL (tested by ncreate to disallow
1372 * the creation of new files/dirs in a deleted directory)
1374 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1375 TMPFS_NODE_MODIFIED;
1377 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1378 TMPFS_NODE_MODIFIED;
1380 TMPFS_NODE_UNLOCK(node);
1381 TMPFS_NODE_UNLOCK(dnode);
1383 /* Free the directory entry we just deleted. Note that the node
1384 * referred by it will not be removed until the vnode is really
1386 tmpfs_free_dirent(tmp, de);
1388 /* Release the deleted vnode (will destroy the node, notify
1389 * interested parties and clean it from the cache). */
1391 TMPFS_NODE_LOCK(dnode);
1392 dnode->tn_status |= TMPFS_NODE_CHANGED;
1393 TMPFS_NODE_UNLOCK(dnode);
1396 cache_unlink(v->a_nch);
1397 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1406 /* --------------------------------------------------------------------- */
1409 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1411 struct vnode *dvp = v->a_dvp;
1412 struct vnode **vpp = v->a_vpp;
1413 struct namecache *ncp = v->a_nch->ncp;
1414 struct vattr *vap = v->a_vap;
1415 struct ucred *cred = v->a_cred;
1416 char *target = v->a_target;
1419 vap->va_type = VLNK;
1420 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1422 tmpfs_knote(*vpp, NOTE_WRITE);
1423 cache_setunresolved(v->a_nch);
1424 cache_setvp(v->a_nch, *vpp);
1429 /* --------------------------------------------------------------------- */
1432 tmpfs_readdir(struct vop_readdir_args *v)
1434 struct vnode *vp = v->a_vp;
1435 struct uio *uio = v->a_uio;
1436 int *eofflag = v->a_eofflag;
1437 off_t **cookies = v->a_cookies;
1438 int *ncookies = v->a_ncookies;
1439 struct tmpfs_mount *tmp;
1443 struct tmpfs_node *node;
1445 /* This operation only makes sense on directory nodes. */
1446 if (vp->v_type != VDIR) {
1450 tmp = VFS_TO_TMPFS(vp->v_mount);
1451 node = VP_TO_TMPFS_DIR(vp);
1452 startoff = uio->uio_offset;
1454 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1455 error = tmpfs_dir_getdotdent(node, uio);
1457 TMPFS_NODE_LOCK_SH(node);
1463 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1464 /* may lock parent, cannot hold node lock */
1465 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1467 TMPFS_NODE_LOCK_SH(node);
1473 TMPFS_NODE_LOCK_SH(node);
1474 error = tmpfs_dir_getdents(node, uio, &cnt);
1477 KKASSERT(error >= -1);
1482 if (eofflag != NULL)
1484 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1486 /* Update NFS-related variables. */
1487 if (error == 0 && cookies != NULL && ncookies != NULL) {
1489 off_t off = startoff;
1490 struct tmpfs_dirent *de = NULL;
1493 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1495 for (i = 0; i < cnt; i++) {
1496 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1497 if (off == TMPFS_DIRCOOKIE_DOT) {
1498 off = TMPFS_DIRCOOKIE_DOTDOT;
1500 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1501 de = RB_MIN(tmpfs_dirtree_cookie,
1502 &node->tn_dir.tn_cookietree);
1503 } else if (de != NULL) {
1504 de = RB_NEXT(tmpfs_dirtree_cookie,
1505 &node->tn_dir.tn_cookietree, de);
1507 de = tmpfs_dir_lookupbycookie(node,
1509 KKASSERT(de != NULL);
1510 de = RB_NEXT(tmpfs_dirtree_cookie,
1511 &node->tn_dir.tn_cookietree, de);
1514 off = TMPFS_DIRCOOKIE_EOF;
1516 off = tmpfs_dircookie(de);
1518 (*cookies)[i] = off;
1520 KKASSERT(uio->uio_offset == off);
1522 TMPFS_NODE_UNLOCK(node);
1524 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1525 TMPFS_NODE_LOCK(node);
1526 node->tn_status |= TMPFS_NODE_ACCESSED;
1527 TMPFS_NODE_UNLOCK(node);
1532 /* --------------------------------------------------------------------- */
1535 tmpfs_readlink(struct vop_readlink_args *v)
1537 struct vnode *vp = v->a_vp;
1538 struct uio *uio = v->a_uio;
1540 struct tmpfs_node *node;
1542 KKASSERT(uio->uio_offset == 0);
1543 KKASSERT(vp->v_type == VLNK);
1545 node = VP_TO_TMPFS_NODE(vp);
1546 TMPFS_NODE_LOCK_SH(node);
1547 error = uiomove(node->tn_link,
1548 MIN(node->tn_size, uio->uio_resid), uio);
1549 TMPFS_NODE_UNLOCK(node);
1550 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1551 TMPFS_NODE_LOCK(node);
1552 node->tn_status |= TMPFS_NODE_ACCESSED;
1553 TMPFS_NODE_UNLOCK(node);
1558 /* --------------------------------------------------------------------- */
1561 tmpfs_inactive(struct vop_inactive_args *v)
1563 struct vnode *vp = v->a_vp;
1564 struct tmpfs_node *node;
1568 lwkt_gettoken(&mp->mnt_token);
1569 node = VP_TO_TMPFS_NODE(vp);
1576 lwkt_reltoken(&mp->mnt_token);
1581 * Get rid of unreferenced deleted vnodes sooner rather than
1582 * later so the data memory can be recovered immediately.
1584 * We must truncate the vnode to prevent the normal reclamation
1585 * path from flushing the data for the removed file to disk.
1587 TMPFS_NODE_LOCK(node);
1588 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1589 node->tn_links == 0)
1591 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1592 TMPFS_NODE_UNLOCK(node);
1593 if (node->tn_type == VREG)
1594 tmpfs_truncate(vp, 0);
1597 TMPFS_NODE_UNLOCK(node);
1599 lwkt_reltoken(&mp->mnt_token);
1604 /* --------------------------------------------------------------------- */
1607 tmpfs_reclaim(struct vop_reclaim_args *v)
1609 struct vnode *vp = v->a_vp;
1610 struct tmpfs_mount *tmp;
1611 struct tmpfs_node *node;
1615 lwkt_gettoken(&mp->mnt_token);
1617 node = VP_TO_TMPFS_NODE(vp);
1618 tmp = VFS_TO_TMPFS(vp->v_mount);
1619 KKASSERT(mp == tmp->tm_mount);
1624 * If the node referenced by this vnode was deleted by the
1625 * user, we must free its associated data structures now that
1626 * the vnode is being reclaimed.
1628 * Directories have an extra link ref.
1630 TMPFS_NODE_LOCK(node);
1631 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1632 node->tn_links == 0) {
1633 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1634 tmpfs_free_node(tmp, node);
1637 TMPFS_NODE_UNLOCK(node);
1639 lwkt_reltoken(&mp->mnt_token);
1641 KKASSERT(vp->v_data == NULL);
1645 /* --------------------------------------------------------------------- */
1648 tmpfs_mountctl(struct vop_mountctl_args *ap)
1650 struct tmpfs_mount *tmp;
1654 mp = ap->a_head.a_ops->head.vv_mount;
1655 lwkt_gettoken(&mp->mnt_token);
1658 case (MOUNTCTL_SET_EXPORT):
1659 tmp = (struct tmpfs_mount *) mp->mnt_data;
1661 if (ap->a_ctllen != sizeof(struct export_args))
1664 rc = vfs_export(mp, &tmp->tm_export,
1665 (const struct export_args *) ap->a_ctl);
1668 rc = vop_stdmountctl(ap);
1672 lwkt_reltoken(&mp->mnt_token);
1676 /* --------------------------------------------------------------------- */
1679 tmpfs_print(struct vop_print_args *v)
1681 struct vnode *vp = v->a_vp;
1683 struct tmpfs_node *node;
1685 node = VP_TO_TMPFS_NODE(vp);
1687 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1688 node, node->tn_flags, node->tn_links);
1689 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1690 node->tn_mode, node->tn_uid, node->tn_gid,
1691 (uintmax_t)node->tn_size, node->tn_status);
1693 if (vp->v_type == VFIFO)
1701 /* --------------------------------------------------------------------- */
1704 tmpfs_pathconf(struct vop_pathconf_args *v)
1706 int name = v->a_name;
1707 register_t *retval = v->a_retval;
1730 case _PC_CHOWN_RESTRICTED:
1742 case _PC_FILESIZEBITS:
1743 *retval = 0; /* XXX Don't know which value should I return. */
1753 /************************************************************************
1755 ************************************************************************/
1757 static void filt_tmpfsdetach(struct knote *kn);
1758 static int filt_tmpfsread(struct knote *kn, long hint);
1759 static int filt_tmpfswrite(struct knote *kn, long hint);
1760 static int filt_tmpfsvnode(struct knote *kn, long hint);
1762 static struct filterops tmpfsread_filtops =
1763 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1764 NULL, filt_tmpfsdetach, filt_tmpfsread };
1765 static struct filterops tmpfswrite_filtops =
1766 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1767 NULL, filt_tmpfsdetach, filt_tmpfswrite };
1768 static struct filterops tmpfsvnode_filtops =
1769 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1770 NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1773 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1775 struct vnode *vp = ap->a_vp;
1776 struct knote *kn = ap->a_kn;
1778 switch (kn->kn_filter) {
1780 kn->kn_fop = &tmpfsread_filtops;
1783 kn->kn_fop = &tmpfswrite_filtops;
1786 kn->kn_fop = &tmpfsvnode_filtops;
1789 return (EOPNOTSUPP);
1792 kn->kn_hook = (caddr_t)vp;
1794 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1800 filt_tmpfsdetach(struct knote *kn)
1802 struct vnode *vp = (void *)kn->kn_hook;
1804 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1808 filt_tmpfsread(struct knote *kn, long hint)
1810 struct vnode *vp = (void *)kn->kn_hook;
1811 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1814 if (hint == NOTE_REVOKE) {
1815 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1820 * Interlock against MP races when performing this function.
1822 TMPFS_NODE_LOCK_SH(node);
1823 off = node->tn_size - kn->kn_fp->f_offset;
1824 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1825 if (kn->kn_sfflags & NOTE_OLDAPI) {
1826 TMPFS_NODE_UNLOCK(node);
1829 if (kn->kn_data == 0) {
1830 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1832 TMPFS_NODE_UNLOCK(node);
1833 return (kn->kn_data != 0);
1837 filt_tmpfswrite(struct knote *kn, long hint)
1839 if (hint == NOTE_REVOKE)
1840 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1846 filt_tmpfsvnode(struct knote *kn, long hint)
1848 if (kn->kn_sfflags & hint)
1849 kn->kn_fflags |= hint;
1850 if (hint == NOTE_REVOKE) {
1851 kn->kn_flags |= (EV_EOF | EV_NODATA);
1854 return (kn->kn_fflags != 0);
1858 /* --------------------------------------------------------------------- */
1861 * vnode operations vector used for files stored in a tmpfs file system.
1863 struct vop_ops tmpfs_vnode_vops = {
1864 .vop_default = vop_defaultop,
1865 .vop_getpages = vop_stdgetpages,
1866 .vop_putpages = vop_stdputpages,
1867 .vop_ncreate = tmpfs_ncreate,
1868 .vop_nresolve = tmpfs_nresolve,
1869 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1870 .vop_nmknod = tmpfs_nmknod,
1871 .vop_open = tmpfs_open,
1872 .vop_close = tmpfs_close,
1873 .vop_access = tmpfs_access,
1874 .vop_getattr = tmpfs_getattr,
1875 .vop_setattr = tmpfs_setattr,
1876 .vop_read = tmpfs_read,
1877 .vop_write = tmpfs_write,
1878 .vop_fsync = tmpfs_fsync,
1879 .vop_mountctl = tmpfs_mountctl,
1880 .vop_nremove = tmpfs_nremove,
1881 .vop_nlink = tmpfs_nlink,
1882 .vop_nrename = tmpfs_nrename,
1883 .vop_nmkdir = tmpfs_nmkdir,
1884 .vop_nrmdir = tmpfs_nrmdir,
1885 .vop_nsymlink = tmpfs_nsymlink,
1886 .vop_readdir = tmpfs_readdir,
1887 .vop_readlink = tmpfs_readlink,
1888 .vop_inactive = tmpfs_inactive,
1889 .vop_reclaim = tmpfs_reclaim,
1890 .vop_print = tmpfs_print,
1891 .vop_pathconf = tmpfs_pathconf,
1892 .vop_bmap = tmpfs_bmap,
1893 .vop_strategy = tmpfs_strategy,
1894 .vop_advlock = tmpfs_advlock,
1895 .vop_kqfilter = tmpfs_kqfilter