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>
41 #include <sys/fcntl.h>
42 #include <sys/lockf.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
48 #include <sys/systm.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/vfsops.h>
52 #include <sys/vnode.h>
53 #include <sys/mountctl.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pageout.h>
60 #include <vm/vm_pager.h>
61 #include <vm/swap_pager.h>
64 #include <vm/vm_page2.h>
66 #include <vfs/fifofs/fifo.h>
67 #include <vfs/tmpfs/tmpfs_vnops.h>
70 static void tmpfs_strategy_done(struct bio *bio);
71 static void tmpfs_move_pages(vm_object_t src, vm_object_t dst, int movflags);
75 * 0 Normal page queue operation on flush. Run through the buffer
76 * cache if free memory is under the minimum.
78 * 1 Try to keep in memory, but run through the buffer cache if
79 * the system is under memory pressure (though this might just
80 * require inactive cleaning).
82 * 2 Be a bit more aggressive when running writes through the
83 * buffer cache when the system is under memory pressure.
85 * 3 Always run tmpfs writes through the buffer cache, thus forcing
88 __read_mostly static int tmpfs_cluster_rd_enable = 1;
89 __read_mostly static int tmpfs_cluster_wr_enable = 1;
90 __read_mostly int tmpfs_bufcache_mode = 0;
91 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "TMPFS filesystem");
92 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, cluster_rd_enable, CTLFLAG_RW,
93 &tmpfs_cluster_rd_enable, 0, "");
94 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, cluster_wr_enable, CTLFLAG_RW,
95 &tmpfs_cluster_wr_enable, 0, "");
96 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, bufcache_mode, CTLFLAG_RW,
97 &tmpfs_bufcache_mode, 0, "");
99 #define TMPFS_MOVF_FROMBACKING 0x0001
100 #define TMPFS_MOVF_DEACTIVATE 0x0002
105 tmpfs_knote(struct vnode *vp, int flags)
108 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
112 /* --------------------------------------------------------------------- */
115 tmpfs_nresolve(struct vop_nresolve_args *ap)
117 struct vnode *dvp = ap->a_dvp;
118 struct vnode *vp = NULL;
119 struct namecache *ncp = ap->a_nch->ncp;
120 struct tmpfs_node *tnode;
121 struct tmpfs_dirent *de;
122 struct tmpfs_node *dnode;
125 dnode = VP_TO_TMPFS_DIR(dvp);
127 TMPFS_NODE_LOCK_SH(dnode);
129 de = tmpfs_dir_lookup(dnode, NULL, ncp);
134 * Allocate a vnode for the node we found. Use
135 * tmpfs_alloc_vp()'s deadlock handling mode.
138 error = tmpfs_alloc_vp(dvp->v_mount, dnode, tnode,
139 LK_EXCLUSIVE | LK_RETRY, &vp);
148 TMPFS_NODE_UNLOCK(dnode);
150 if ((dnode->tn_status & TMPFS_NODE_ACCESSED) == 0) {
151 TMPFS_NODE_LOCK(dnode);
152 dnode->tn_status |= TMPFS_NODE_ACCESSED;
153 TMPFS_NODE_UNLOCK(dnode);
157 * Store the result of this lookup in the cache. Avoid this if the
158 * request was for creation, as it does not improve timings on
163 cache_setvp(ap->a_nch, vp);
165 } else if (error == ENOENT) {
166 cache_setvp(ap->a_nch, NULL);
172 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
174 struct vnode *dvp = ap->a_dvp;
175 struct vnode **vpp = ap->a_vpp;
176 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
177 struct ucred *cred = ap->a_cred;
182 /* Check accessibility of requested node as a first step. */
183 error = VOP_ACCESS(dvp, VEXEC, cred);
187 if (dnode->tn_dir.tn_parent != NULL) {
188 /* Allocate a new vnode on the matching entry. */
189 error = tmpfs_alloc_vp(dvp->v_mount,
190 NULL, dnode->tn_dir.tn_parent,
191 LK_EXCLUSIVE | LK_RETRY, vpp);
196 return (*vpp == NULL) ? ENOENT : 0;
199 /* --------------------------------------------------------------------- */
202 tmpfs_ncreate(struct vop_ncreate_args *ap)
204 struct vnode *dvp = ap->a_dvp;
205 struct vnode **vpp = ap->a_vpp;
206 struct namecache *ncp = ap->a_nch->ncp;
207 struct vattr *vap = ap->a_vap;
208 struct ucred *cred = ap->a_cred;
211 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
213 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
215 cache_setunresolved(ap->a_nch);
216 cache_setvp(ap->a_nch, *vpp);
217 tmpfs_knote(dvp, NOTE_WRITE);
221 /* --------------------------------------------------------------------- */
224 tmpfs_nmknod(struct vop_nmknod_args *ap)
226 struct vnode *dvp = ap->a_dvp;
227 struct vnode **vpp = ap->a_vpp;
228 struct namecache *ncp = ap->a_nch->ncp;
229 struct vattr *vap = ap->a_vap;
230 struct ucred *cred = ap->a_cred;
233 if (vap->va_type != VBLK && vap->va_type != VCHR &&
234 vap->va_type != VFIFO) {
238 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
240 cache_setunresolved(ap->a_nch);
241 cache_setvp(ap->a_nch, *vpp);
242 tmpfs_knote(dvp, NOTE_WRITE);
247 /* --------------------------------------------------------------------- */
250 tmpfs_open(struct vop_open_args *ap)
252 struct vnode *vp = ap->a_vp;
253 int mode = ap->a_mode;
254 struct tmpfs_node *node;
257 node = VP_TO_TMPFS_NODE(vp);
260 /* The file is still active but all its names have been removed
261 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
262 * it is about to die. */
263 if (node->tn_links < 1)
267 /* If the file is marked append-only, deny write requests. */
268 if ((node->tn_flags & APPEND) &&
269 (mode & (FWRITE | O_APPEND)) == FWRITE) {
272 if (node->tn_reg.tn_pages_in_aobj) {
273 TMPFS_NODE_LOCK(node);
274 if (node->tn_reg.tn_pages_in_aobj) {
275 tmpfs_move_pages(node->tn_reg.tn_aobj,
277 TMPFS_MOVF_FROMBACKING);
278 node->tn_reg.tn_pages_in_aobj = 0;
280 TMPFS_NODE_UNLOCK(node);
282 error = vop_stdopen(ap);
288 /* --------------------------------------------------------------------- */
291 tmpfs_close(struct vop_close_args *ap)
293 struct vnode *vp = ap->a_vp;
294 struct tmpfs_node *node;
297 node = VP_TO_TMPFS_NODE(vp);
299 if (node->tn_links > 0) {
301 * Update node times. No need to do it if the node has
302 * been deleted, because it will vanish after we return.
307 error = vop_stdclose(ap);
312 /* --------------------------------------------------------------------- */
315 tmpfs_access(struct vop_access_args *ap)
317 struct vnode *vp = ap->a_vp;
319 struct tmpfs_node *node;
321 node = VP_TO_TMPFS_NODE(vp);
323 switch (vp->v_type) {
329 if ((ap->a_mode & VWRITE) &&
330 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
350 if ((ap->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
355 error = vop_helper_access(ap, node->tn_uid, node->tn_gid,
361 /* --------------------------------------------------------------------- */
364 tmpfs_getattr(struct vop_getattr_args *ap)
366 struct vnode *vp = ap->a_vp;
367 struct vattr *vap = ap->a_vap;
368 struct tmpfs_node *node;
370 node = VP_TO_TMPFS_NODE(vp);
374 vap->va_type = vp->v_type;
375 vap->va_mode = node->tn_mode;
376 vap->va_nlink = node->tn_links;
377 vap->va_uid = node->tn_uid;
378 vap->va_gid = node->tn_gid;
379 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
380 vap->va_fileid = node->tn_id;
381 vap->va_size = node->tn_size;
382 vap->va_blocksize = PAGE_SIZE;
383 vap->va_atime.tv_sec = node->tn_atime;
384 vap->va_atime.tv_nsec = node->tn_atimensec;
385 vap->va_mtime.tv_sec = node->tn_mtime;
386 vap->va_mtime.tv_nsec = node->tn_mtimensec;
387 vap->va_ctime.tv_sec = node->tn_ctime;
388 vap->va_ctime.tv_nsec = node->tn_ctimensec;
389 vap->va_gen = node->tn_gen;
390 vap->va_flags = node->tn_flags;
391 if (vp->v_type == VBLK || vp->v_type == VCHR) {
392 vap->va_rmajor = umajor(node->tn_rdev);
393 vap->va_rminor = uminor(node->tn_rdev);
395 vap->va_bytes = round_page(node->tn_size);
401 /* --------------------------------------------------------------------- */
404 tmpfs_getattr_quick(struct vop_getattr_args *ap)
406 struct vnode *vp = ap->a_vp;
407 struct vattr *vap = ap->a_vap;
408 struct tmpfs_node *node;
410 node = VP_TO_TMPFS_NODE(vp);
414 vap->va_type = vp->v_type;
415 vap->va_mode = node->tn_mode;
416 vap->va_nlink = node->tn_links;
417 vap->va_uid = node->tn_uid;
418 vap->va_gid = node->tn_gid;
419 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
420 vap->va_fileid = node->tn_id;
421 vap->va_size = node->tn_size;
422 vap->va_blocksize = PAGE_SIZE;
423 vap->va_gen = node->tn_gen;
424 vap->va_flags = node->tn_flags;
425 if (vp->v_type == VBLK || vp->v_type == VCHR) {
426 vap->va_rmajor = umajor(node->tn_rdev);
427 vap->va_rminor = uminor(node->tn_rdev);
436 /* --------------------------------------------------------------------- */
439 tmpfs_setattr(struct vop_setattr_args *ap)
441 struct vnode *vp = ap->a_vp;
442 struct vattr *vap = ap->a_vap;
443 struct ucred *cred = ap->a_cred;
444 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
448 TMPFS_NODE_LOCK(node);
449 if (error == 0 && (vap->va_flags != VNOVAL)) {
450 error = tmpfs_chflags(vp, vap->va_flags, cred);
451 kflags |= NOTE_ATTRIB;
454 if (error == 0 && (vap->va_size != VNOVAL)) {
455 /* restore any saved pages before proceeding */
456 if (node->tn_reg.tn_pages_in_aobj) {
457 tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
458 TMPFS_MOVF_FROMBACKING |
459 TMPFS_MOVF_DEACTIVATE);
460 node->tn_reg.tn_pages_in_aobj = 0;
462 if (vap->va_size > node->tn_size)
463 kflags |= NOTE_WRITE | NOTE_EXTEND;
465 kflags |= NOTE_WRITE;
466 error = tmpfs_chsize(vp, vap->va_size, cred);
469 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
470 vap->va_gid != (gid_t)VNOVAL)) {
471 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
472 kflags |= NOTE_ATTRIB;
475 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
476 error = tmpfs_chmod(vp, vap->va_mode, cred);
477 kflags |= NOTE_ATTRIB;
480 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
481 vap->va_atime.tv_nsec != VNOVAL) ||
482 (vap->va_mtime.tv_sec != VNOVAL &&
483 vap->va_mtime.tv_nsec != VNOVAL) )) {
484 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
485 vap->va_vaflags, cred);
486 kflags |= NOTE_ATTRIB;
490 * Update the node times. We give preference to the error codes
491 * generated by this function rather than the ones that may arise
495 TMPFS_NODE_UNLOCK(node);
496 tmpfs_knote(vp, kflags);
501 /* --------------------------------------------------------------------- */
504 * fsync is usually a NOP, but we must take action when unmounting or
508 tmpfs_fsync(struct vop_fsync_args *ap)
510 struct tmpfs_node *node;
511 struct vnode *vp = ap->a_vp;
513 node = VP_TO_TMPFS_NODE(vp);
516 * tmpfs vnodes typically remain dirty, avoid long syncer scans
517 * by forcing removal from the syncer list.
519 vn_syncer_remove(vp, 1);
522 if (vp->v_type == VREG) {
523 if (vp->v_flag & VRECLAIMED) {
524 if (node->tn_links == 0)
525 tmpfs_truncate(vp, 0);
527 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
534 /* --------------------------------------------------------------------- */
537 tmpfs_read(struct vop_read_args *ap)
540 struct vnode *vp = ap->a_vp;
541 struct uio *uio = ap->a_uio;
542 struct tmpfs_node *node;
553 if (uio->uio_offset < 0)
555 if (vp->v_type != VREG)
559 * Extract node, try to shortcut the operation through
560 * the VM page cache, allowing us to avoid buffer cache
563 node = VP_TO_TMPFS_NODE(vp);
564 resid = uio->uio_resid;
565 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
566 error = vop_helper_read_shortcut(ap);
569 if (uio->uio_resid == 0) {
576 * restore any saved pages before proceeding
578 if (node->tn_reg.tn_pages_in_aobj) {
579 TMPFS_NODE_LOCK(node);
580 if (node->tn_reg.tn_pages_in_aobj) {
581 tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
582 TMPFS_MOVF_FROMBACKING);
583 node->tn_reg.tn_pages_in_aobj = 0;
585 TMPFS_NODE_UNLOCK(node);
589 * Fall-through to our normal read code.
591 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
593 * Use buffer cache I/O (via tmpfs_strategy)
595 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
596 base_offset = (off_t)uio->uio_offset - offset;
597 bp = getcacheblk(vp, base_offset,
598 node->tn_blksize, GETBLK_KVABIO);
600 if (tmpfs_cluster_rd_enable) {
601 error = cluster_readx(vp, node->tn_size,
604 B_NOTMETA | B_KVABIO,
609 error = bread_kvabio(vp, base_offset,
610 node->tn_blksize, &bp);
614 kprintf("tmpfs_read bread error %d\n", error);
619 * tmpfs pretty much fiddles directly with the VM
620 * system, don't let it exhaust it or we won't play
621 * nice with other processes.
623 * Only do this if the VOP is coming from a normal
624 * read/write. The VM system handles the case for
627 if (uio->uio_segflg != UIO_NOCOPY)
630 bp->b_flags |= B_CLUSTEROK;
634 * Figure out how many bytes we can actually copy this loop.
636 len = node->tn_blksize - offset;
637 if (len > uio->uio_resid)
638 len = uio->uio_resid;
639 if (len > node->tn_size - uio->uio_offset)
640 len = (size_t)(node->tn_size - uio->uio_offset);
642 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
645 kprintf("tmpfs_read uiomove error %d\n", error);
651 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
652 TMPFS_NODE_LOCK(node);
653 node->tn_status |= TMPFS_NODE_ACCESSED;
654 TMPFS_NODE_UNLOCK(node);
660 tmpfs_write(struct vop_write_args *ap)
663 struct vnode *vp = ap->a_vp;
664 struct uio *uio = ap->a_uio;
665 struct thread *td = uio->uio_td;
666 struct tmpfs_node *node;
679 if (uio->uio_resid == 0) {
683 node = VP_TO_TMPFS_NODE(vp);
685 if (vp->v_type != VREG)
687 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
689 TMPFS_NODE_LOCK(node);
692 * restore any saved pages before proceeding
694 if (node->tn_reg.tn_pages_in_aobj) {
695 tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
696 TMPFS_MOVF_FROMBACKING);
697 node->tn_reg.tn_pages_in_aobj = 0;
700 oldsize = node->tn_size;
701 if (ap->a_ioflag & IO_APPEND)
702 uio->uio_offset = node->tn_size;
705 * Check for illegal write offsets.
707 if (uio->uio_offset + uio->uio_resid >
708 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) {
714 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN).
716 if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) {
717 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
720 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
721 ksignal(td->td_proc, SIGXFSZ);
728 * Extend the file's size if necessary
730 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
732 while (uio->uio_resid > 0) {
734 * Don't completely blow out running buffer I/O
735 * when being hit from the pageout daemon.
737 if (uio->uio_segflg == UIO_NOCOPY &&
738 (ap->a_ioflag & IO_RECURSE) == 0) {
739 bwillwrite(node->tn_blksize);
743 * Use buffer cache I/O (via tmpfs_strategy)
745 * Calculate the maximum bytes we can write to the buffer at
746 * this offset (after resizing).
748 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
749 base_offset = (off_t)uio->uio_offset - offset;
750 len = uio->uio_resid;
751 if (len > TMPFS_BLKSIZE - offset)
752 len = TMPFS_BLKSIZE - offset;
754 if ((uio->uio_offset + len) > node->tn_size) {
755 trivial = (uio->uio_offset <= node->tn_size);
756 error = tmpfs_reg_resize(vp, uio->uio_offset + len,
763 * Read to fill in any gaps. Theoretically we could
764 * optimize this if the write covers the entire buffer
765 * and is not a UIO_NOCOPY write, however this can lead
766 * to a security violation exposing random kernel memory
767 * (whatever junk was in the backing VM pages before).
769 * So just use bread() to do the right thing.
771 error = bread_kvabio(vp, base_offset, node->tn_blksize, &bp);
773 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
775 kprintf("tmpfs_write uiomove error %d\n", error);
780 if (uio->uio_offset > node->tn_size) {
781 node->tn_size = uio->uio_offset;
782 kflags |= NOTE_EXTEND;
784 kflags |= NOTE_WRITE;
787 * UIO_NOCOPY is a sensitive state due to potentially being
788 * issued from the pageout daemon while in a low-memory
789 * situation. However, in order to cluster the I/O nicely
790 * (e.g. 64KB+ writes instead of 16KB writes), we still try
791 * to follow the same semantics that any other filesystem
794 * For the normal case we buwrite(), dirtying the underlying
795 * VM pages instead of dirtying the buffer and releasing the
796 * buffer as a clean buffer. This allows tmpfs to use
797 * essentially all available memory to cache file data.
798 * If we used bdwrite() the buffer cache would wind up
799 * flushing the data to swap too quickly.
801 * But because tmpfs can seriously load the VM system we
802 * fall-back to using bdwrite() when free memory starts
803 * to get low. This shifts the load away from the VM system
804 * and makes tmpfs act more like a normal filesystem with
805 * regards to disk activity.
807 * tmpfs pretty much fiddles directly with the VM
808 * system, don't let it exhaust it or we won't play
809 * nice with other processes. Only do this if the
810 * VOP is coming from a normal read/write. The VM system
811 * handles the case for UIO_NOCOPY.
813 bp->b_flags |= B_CLUSTEROK;
814 if (uio->uio_segflg == UIO_NOCOPY) {
816 * Flush from the pageout daemon, deal with potentially
817 * very heavy tmpfs write activity causing long stalls
818 * in the pageout daemon before pages get to free/cache.
820 * We have to be careful not to bypass the page queues
821 * entirely or we can cause write-read thrashing and
822 * delay the paging of data that is more pageable then
825 * (a) Under severe pressure setting B_DIRECT will
826 * cause a buffer release to try to free the
829 * (b) Under modest memory pressure the B_AGE flag
830 * we retire the buffer and its underlying pages
831 * more quickly than normal.
833 * We could also force this by setting B_NOTMETA
834 * but that might have other unintended side-
835 * effects (e.g. setting PG_NOTMETA on the VM page).
837 * (c) For the pageout->putpages->generic_putpages->
838 * UIO_NOCOPY-write (here), issuing an immediate
839 * write prevents any real clustering from
840 * happening because the buffers probably aren't
841 * (yet) marked dirty, or lost due to prior use
842 * of buwrite(). Try to use the normal
843 * cluster_write() mechanism for performance.
845 * Hopefully this will unblock the VM system more
846 * quickly under extreme tmpfs write load.
848 if (tmpfs_bufcache_mode >= 2) {
849 if (vm_page_count_min(vm_page_free_hysteresis))
850 bp->b_flags |= B_DIRECT | B_TTC;
851 if (vm_pages_needed || vm_paging_needed(0))
852 bp->b_flags |= B_AGE;
854 bp->b_flags |= B_RELBUF;
855 bp->b_act_count = 0; /* buffer->deactivate pgs */
856 if (tmpfs_cluster_wr_enable &&
857 (ap->a_ioflag & (IO_SYNC | IO_DIRECT)) == 0) {
858 cluster_write(bp, node->tn_size,
859 node->tn_blksize, seqcount);
863 } else if (vm_page_count_min(0) ||
864 ((vm_pages_needed || vm_paging_needed(0)) &&
865 tmpfs_bufcache_mode >= 1)) {
867 * If the pageout daemon is running we cycle the
868 * write through the buffer cache normally to
869 * pipeline the flush, thus avoiding adding any
870 * more memory pressure to the pageout daemon.
872 bp->b_act_count = 0; /* buffer->deactivate pgs */
873 if (tmpfs_cluster_wr_enable) {
874 cluster_write(bp, node->tn_size,
875 node->tn_blksize, seqcount);
881 * Otherwise run the buffer directly through to the
882 * backing VM store, leaving the buffer clean so
883 * buffer limits do not force early flushes to swap.
886 /*vm_wait_nominal();*/
890 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
897 (void)tmpfs_reg_resize(vp, oldsize, trivial);
898 kflags &= ~NOTE_EXTEND;
904 * Currently we don't set the mtime on files modified via mmap()
905 * because we can't tell the difference between those modifications
906 * and an attempt by the pageout daemon to flush tmpfs pages to
909 * This is because in order to defer flushes as long as possible
910 * buwrite() works by marking the underlying VM pages dirty in
911 * order to be able to dispose of the buffer cache buffer without
914 if (uio->uio_segflg == UIO_NOCOPY) {
915 if (vp->v_flag & VLASTWRITETS) {
916 node->tn_mtime = vp->v_lastwrite_ts.tv_sec;
917 node->tn_mtimensec = vp->v_lastwrite_ts.tv_nsec;
920 node->tn_status |= TMPFS_NODE_MODIFIED;
921 vclrflags(vp, VLASTWRITETS);
925 node->tn_status |= TMPFS_NODE_CHANGED;
927 if (node->tn_mode & (S_ISUID | S_ISGID)) {
928 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
929 node->tn_mode &= ~(S_ISUID | S_ISGID);
932 TMPFS_NODE_UNLOCK(node);
934 tmpfs_knote(vp, kflags);
940 tmpfs_advlock(struct vop_advlock_args *ap)
942 struct tmpfs_node *node;
943 struct vnode *vp = ap->a_vp;
946 node = VP_TO_TMPFS_NODE(vp);
947 error = (lf_advlock(ap, &node->tn_advlock, node->tn_size));
953 * The strategy function is typically only called when memory pressure
954 * forces the system to attempt to pageout pages. It can also be called
955 * by [n]vtruncbuf() when a truncation cuts a page in half. Normal write
958 * We set VKVABIO for VREG files so bp->b_data may not be synchronized to
959 * our cpu. swap_pager_strategy() is all we really use, and it directly
963 tmpfs_strategy(struct vop_strategy_args *ap)
965 struct bio *bio = ap->a_bio;
967 struct buf *bp = bio->bio_buf;
968 struct vnode *vp = ap->a_vp;
969 struct tmpfs_node *node;
974 if (vp->v_type != VREG) {
975 bp->b_resid = bp->b_bcount;
976 bp->b_flags |= B_ERROR | B_INVAL;
977 bp->b_error = EINVAL;
982 node = VP_TO_TMPFS_NODE(vp);
984 uobj = node->tn_reg.tn_aobj;
987 * Don't bother flushing to swap if there is no swap, just
988 * ensure that the pages are marked as needing a commit (still).
990 if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) {
991 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
992 m = bp->b_xio.xio_pages[i];
993 vm_page_need_commit(m);
1001 * XXX removed, this does not work well because under heavy
1002 * filesystem loads it often
1003 * forces the data to be read right back in again after
1004 * being written due to bypassing normal LRU operation.
1006 * Tell the buffer cache to try to recycle the pages
1007 * to PQ_CACHE on release.
1009 if (tmpfs_bufcache_mode >= 2 ||
1010 (tmpfs_bufcache_mode == 1 && vm_paging_needed(0))) {
1011 bp->b_flags |= B_TTC;
1014 nbio = push_bio(bio);
1015 nbio->bio_done = tmpfs_strategy_done;
1016 nbio->bio_offset = bio->bio_offset;
1017 swap_pager_strategy(uobj, nbio);
1023 * If we were unable to commit the pages to swap make sure they are marked
1024 * as needing a commit (again). If we were, clear the flag to allow the
1025 * pages to be freed.
1027 * Do not error-out the buffer. In particular, vinvalbuf() needs to
1031 tmpfs_strategy_done(struct bio *bio)
1039 if (bp->b_flags & B_ERROR) {
1040 bp->b_flags &= ~B_ERROR;
1043 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1044 m = bp->b_xio.xio_pages[i];
1045 vm_page_need_commit(m);
1048 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1049 m = bp->b_xio.xio_pages[i];
1050 vm_page_clear_commit(m);
1058 * To make write clustering work well make the backing store look
1059 * contiguous to the cluster_*() code. The swap_strategy() function
1060 * will take it from there.
1062 * Use MAXBSIZE-sized chunks as a micro-optimization to make random
1063 * flushes leave full-sized gaps.
1066 tmpfs_bmap(struct vop_bmap_args *ap)
1068 if (ap->a_doffsetp != NULL)
1069 *ap->a_doffsetp = ap->a_loffset;
1070 if (ap->a_runp != NULL)
1071 *ap->a_runp = MAXBSIZE - (ap->a_loffset & (MAXBSIZE - 1));
1072 if (ap->a_runb != NULL)
1073 *ap->a_runb = ap->a_loffset & (MAXBSIZE - 1);
1078 /* --------------------------------------------------------------------- */
1081 tmpfs_nremove(struct vop_nremove_args *ap)
1083 struct vnode *dvp = ap->a_dvp;
1084 struct namecache *ncp = ap->a_nch->ncp;
1087 struct tmpfs_dirent *de;
1088 struct tmpfs_mount *tmp;
1089 struct tmpfs_node *dnode;
1090 struct tmpfs_node *node;
1093 * We have to acquire the vp from ap->a_nch because we will likely
1094 * unresolve the namecache entry, and a vrele/vput is needed to
1095 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1097 * We have to use vget to clear any inactive state on the vnode,
1098 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1099 * will not get called when we release it.
1101 error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp);
1102 KKASSERT(vp->v_mount == dvp->v_mount);
1103 KKASSERT(error == 0);
1106 if (vp->v_type == VDIR) {
1111 dnode = VP_TO_TMPFS_DIR(dvp);
1112 node = VP_TO_TMPFS_NODE(vp);
1113 tmp = VFS_TO_TMPFS(vp->v_mount);
1115 TMPFS_NODE_LOCK(dnode);
1116 TMPFS_NODE_LOCK(node);
1117 de = tmpfs_dir_lookup(dnode, node, ncp);
1120 TMPFS_NODE_UNLOCK(node);
1121 TMPFS_NODE_UNLOCK(dnode);
1125 /* Files marked as immutable or append-only cannot be deleted. */
1126 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
1127 (dnode->tn_flags & APPEND)) {
1129 TMPFS_NODE_UNLOCK(node);
1130 TMPFS_NODE_UNLOCK(dnode);
1134 /* Remove the entry from the directory; as it is a file, we do not
1135 * have to change the number of hard links of the directory. */
1136 tmpfs_dir_detach_locked(dnode, de);
1137 TMPFS_NODE_UNLOCK(dnode);
1139 /* Free the directory entry we just deleted. Note that the node
1140 * referred by it will not be removed until the vnode is really
1142 tmpfs_free_dirent(tmp, de);
1144 if (node->tn_links > 0)
1145 node->tn_status |= TMPFS_NODE_CHANGED;
1146 TMPFS_NODE_UNLOCK(node);
1148 cache_unlink(ap->a_nch);
1149 tmpfs_knote(vp, NOTE_DELETE);
1154 tmpfs_knote(dvp, NOTE_WRITE);
1161 /* --------------------------------------------------------------------- */
1164 tmpfs_nlink(struct vop_nlink_args *ap)
1166 struct vnode *dvp = ap->a_dvp;
1167 struct vnode *vp = ap->a_vp;
1168 struct tmpfs_mount *tmp = VFS_TO_TMPFS(vp->v_mount);
1169 struct namecache *ncp = ap->a_nch->ncp;
1170 struct tmpfs_dirent *de;
1171 struct tmpfs_node *node;
1172 struct tmpfs_node *dnode;
1175 KKASSERT(dvp != vp); /* XXX When can this be false? */
1177 node = VP_TO_TMPFS_NODE(vp);
1178 dnode = VP_TO_TMPFS_NODE(dvp);
1179 TMPFS_NODE_LOCK(dnode);
1181 /* XXX: Why aren't the following two tests done by the caller? */
1183 /* Hard links of directories are forbidden. */
1184 if (vp->v_type == VDIR) {
1189 /* Cannot create cross-device links. */
1190 if (dvp->v_mount != vp->v_mount) {
1195 /* Cannot hard-link into a deleted directory */
1196 if (dnode != tmp->tm_root && dnode->tn_dir.tn_parent == NULL) {
1201 /* Ensure that we do not overflow the maximum number of links imposed
1203 KKASSERT(node->tn_links <= LINK_MAX);
1204 if (node->tn_links >= LINK_MAX) {
1209 /* We cannot create links of files marked immutable or append-only. */
1210 if (node->tn_flags & (IMMUTABLE | APPEND)) {
1215 /* Allocate a new directory entry to represent the node. */
1216 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
1217 ncp->nc_name, ncp->nc_nlen, &de);
1221 /* Insert the new directory entry into the appropriate directory. */
1222 tmpfs_dir_attach_locked(dnode, de);
1224 /* vp link count has changed, so update node times. */
1226 TMPFS_NODE_LOCK(node);
1227 node->tn_status |= TMPFS_NODE_CHANGED;
1228 TMPFS_NODE_UNLOCK(node);
1231 tmpfs_knote(vp, NOTE_LINK);
1232 cache_setunresolved(ap->a_nch);
1233 cache_setvp(ap->a_nch, vp);
1237 TMPFS_NODE_UNLOCK(dnode);
1239 tmpfs_knote(dvp, NOTE_WRITE);
1243 /* --------------------------------------------------------------------- */
1246 tmpfs_nrename(struct vop_nrename_args *ap)
1248 struct vnode *fdvp = ap->a_fdvp;
1249 struct namecache *fncp = ap->a_fnch->ncp;
1250 struct vnode *fvp = fncp->nc_vp;
1251 struct vnode *tdvp = ap->a_tdvp;
1252 struct namecache *tncp = ap->a_tnch->ncp;
1254 struct tmpfs_dirent *de, *tde;
1255 struct tmpfs_mount *tmp;
1256 struct tmpfs_node *fdnode;
1257 struct tmpfs_node *tdnode;
1258 struct tmpfs_node *fnode;
1259 struct tmpfs_node *tnode;
1264 KKASSERT(fdvp->v_mount == fvp->v_mount);
1267 * Because tvp can get overwritten we have to vget it instead of
1268 * just vref or use it, otherwise it's VINACTIVE flag may not get
1269 * cleared and the node won't get destroyed.
1271 error = cache_vget(ap->a_tnch, ap->a_cred, LK_SHARED, &tvp);
1273 tnode = VP_TO_TMPFS_NODE(tvp);
1279 /* Disallow cross-device renames.
1280 * XXX Why isn't this done by the caller? */
1281 if (fvp->v_mount != tdvp->v_mount ||
1282 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1287 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1288 tdnode = VP_TO_TMPFS_DIR(tdvp);
1290 /* If source and target are the same file, there is nothing to do. */
1296 fdnode = VP_TO_TMPFS_DIR(fdvp);
1297 fnode = VP_TO_TMPFS_NODE(fvp);
1299 tmpfs_lock4(fdnode, tdnode, fnode, tnode);
1302 * Cannot rename into a deleted directory
1304 if (tdnode != tmp->tm_root && tdnode->tn_dir.tn_parent == NULL) {
1309 /* Avoid manipulating '.' and '..' entries. */
1310 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
1315 KKASSERT(de->td_node == fnode);
1318 * If replacing an entry in the target directory and that entry
1319 * is a directory, it must be empty.
1321 * Kern_rename gurantees the destination to be a directory
1322 * if the source is one (it does?).
1325 KKASSERT(tnode != NULL);
1327 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1328 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1333 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1334 if (tnode->tn_size > 0) {
1338 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1341 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1345 KKASSERT(fnode->tn_type != VDIR &&
1346 tnode->tn_type != VDIR);
1350 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1351 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1357 * Ensure that we have enough memory to hold the new name, if it
1358 * has to be changed.
1360 if (fncp->nc_nlen != tncp->nc_nlen ||
1361 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
1362 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
1363 M_WAITOK | M_NULLOK);
1364 if (newname == NULL) {
1368 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1369 newname[tncp->nc_nlen] = '\0';
1375 * Unlink entry from source directory. Note that the kernel has
1376 * already checked for illegal recursion cases (renaming a directory
1377 * into a subdirectory of itself).
1379 if (fdnode != tdnode) {
1380 tmpfs_dir_detach_locked(fdnode, de);
1382 /* XXX depend on namecache lock */
1383 KKASSERT(de == tmpfs_dir_lookup(fdnode, fnode, fncp));
1384 RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de);
1385 RB_REMOVE(tmpfs_dirtree_cookie,
1386 &fdnode->tn_dir.tn_cookietree, de);
1390 * Handle any name change. Swap with newname, we will
1391 * deallocate it at the end.
1393 if (newname != NULL) {
1394 oldname = de->td_name;
1395 de->td_name = newname;
1396 de->td_namelen = (uint16_t)tncp->nc_nlen;
1401 * If we are overwriting an entry, we have to remove the old one
1402 * from the target directory.
1405 /* Remove the old entry from the target directory. */
1406 tde = tmpfs_dir_lookup(tdnode, tnode, tncp);
1407 tmpfs_dir_detach_locked(tdnode, tde);
1408 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1411 * Free the directory entry we just deleted. Note that the
1412 * node referred by it will not be removed until the vnode is
1415 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1416 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1420 * Link entry to target directory. If the entry
1421 * represents a directory move the parent linkage
1424 if (fdnode != tdnode) {
1425 if (de->td_node->tn_type == VDIR) {
1426 TMPFS_VALIDATE_DIR(fnode);
1428 tmpfs_dir_attach_locked(tdnode, de);
1430 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1431 RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de);
1432 RB_INSERT(tmpfs_dirtree_cookie,
1433 &tdnode->tn_dir.tn_cookietree, de);
1435 tmpfs_unlock4(fdnode, tdnode, fnode, tnode);
1441 kfree(newname, tmp->tm_name_zone);
1444 cache_rename(ap->a_fnch, ap->a_tnch);
1445 tmpfs_knote(ap->a_fdvp, NOTE_WRITE);
1446 tmpfs_knote(ap->a_tdvp, NOTE_WRITE);
1447 if (fnode->tn_vnode)
1448 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1454 tmpfs_unlock4(fdnode, tdnode, fnode, tnode);
1461 /* --------------------------------------------------------------------- */
1464 tmpfs_nmkdir(struct vop_nmkdir_args *ap)
1466 struct vnode *dvp = ap->a_dvp;
1467 struct vnode **vpp = ap->a_vpp;
1468 struct namecache *ncp = ap->a_nch->ncp;
1469 struct vattr *vap = ap->a_vap;
1470 struct ucred *cred = ap->a_cred;
1473 KKASSERT(vap->va_type == VDIR);
1475 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1477 cache_setunresolved(ap->a_nch);
1478 cache_setvp(ap->a_nch, *vpp);
1479 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1484 /* --------------------------------------------------------------------- */
1487 tmpfs_nrmdir(struct vop_nrmdir_args *ap)
1489 struct vnode *dvp = ap->a_dvp;
1490 struct namecache *ncp = ap->a_nch->ncp;
1492 struct tmpfs_dirent *de;
1493 struct tmpfs_mount *tmp;
1494 struct tmpfs_node *dnode;
1495 struct tmpfs_node *node;
1499 * We have to acquire the vp from ap->a_nch because we will likely
1500 * unresolve the namecache entry, and a vrele/vput is needed to
1501 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1503 * We have to use vget to clear any inactive state on the vnode,
1504 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1505 * will not get called when we release it.
1507 error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp);
1508 KKASSERT(error == 0);
1512 * Prevalidate so we don't hit an assertion later
1514 if (vp->v_type != VDIR) {
1519 tmp = VFS_TO_TMPFS(dvp->v_mount);
1520 dnode = VP_TO_TMPFS_DIR(dvp);
1521 node = VP_TO_TMPFS_DIR(vp);
1526 TMPFS_NODE_LOCK(dnode);
1527 TMPFS_NODE_LOCK(node);
1530 * Only empty directories can be removed.
1532 if (node->tn_size > 0) {
1537 if ((dnode->tn_flags & APPEND)
1538 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1544 * This invariant holds only if we are not trying to
1545 * remove "..". We checked for that above so this is safe now.
1547 KKASSERT(node->tn_dir.tn_parent == dnode);
1550 * Get the directory entry associated with node (vp)
1552 de = tmpfs_dir_lookup(dnode, node, ncp);
1553 KKASSERT(TMPFS_DIRENT_MATCHES(de, ncp->nc_name, ncp->nc_nlen));
1555 /* Check flags to see if we are allowed to remove the directory. */
1556 if ((dnode->tn_flags & APPEND) ||
1557 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1562 /* Detach the directory entry from the directory (dnode). */
1563 tmpfs_dir_detach_locked(dnode, de);
1566 * Must set parent linkage to NULL (tested by ncreate to disallow
1567 * the creation of new files/dirs in a deleted directory)
1569 node->tn_status |= TMPFS_NODE_CHANGED;
1571 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1572 TMPFS_NODE_MODIFIED;
1574 /* Free the directory entry we just deleted. Note that the node
1575 * referred by it will not be removed until the vnode is really
1577 tmpfs_free_dirent(tmp, de);
1579 /* Release the deleted vnode (will destroy the node, notify
1580 * interested parties and clean it from the cache). */
1582 dnode->tn_status |= TMPFS_NODE_CHANGED;
1584 TMPFS_NODE_UNLOCK(node);
1585 TMPFS_NODE_UNLOCK(dnode);
1588 cache_unlink(ap->a_nch);
1589 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1594 TMPFS_NODE_UNLOCK(node);
1595 TMPFS_NODE_UNLOCK(dnode);
1603 /* --------------------------------------------------------------------- */
1606 tmpfs_nsymlink(struct vop_nsymlink_args *ap)
1608 struct vnode *dvp = ap->a_dvp;
1609 struct vnode **vpp = ap->a_vpp;
1610 struct namecache *ncp = ap->a_nch->ncp;
1611 struct vattr *vap = ap->a_vap;
1612 struct ucred *cred = ap->a_cred;
1613 char *target = ap->a_target;
1616 vap->va_type = VLNK;
1617 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1619 tmpfs_knote(*vpp, NOTE_WRITE);
1620 cache_setunresolved(ap->a_nch);
1621 cache_setvp(ap->a_nch, *vpp);
1626 /* --------------------------------------------------------------------- */
1629 tmpfs_readdir(struct vop_readdir_args *ap)
1631 struct vnode *vp = ap->a_vp;
1632 struct uio *uio = ap->a_uio;
1633 int *eofflag = ap->a_eofflag;
1634 off_t **cookies = ap->a_cookies;
1635 int *ncookies = ap->a_ncookies;
1636 struct tmpfs_mount *tmp;
1640 struct tmpfs_node *node;
1642 /* This operation only makes sense on directory nodes. */
1643 if (vp->v_type != VDIR) {
1647 tmp = VFS_TO_TMPFS(vp->v_mount);
1648 node = VP_TO_TMPFS_DIR(vp);
1649 startoff = uio->uio_offset;
1651 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1652 error = tmpfs_dir_getdotdent(node, uio);
1654 TMPFS_NODE_LOCK_SH(node);
1660 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1661 /* may lock parent, cannot hold node lock */
1662 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1664 TMPFS_NODE_LOCK_SH(node);
1670 TMPFS_NODE_LOCK_SH(node);
1671 error = tmpfs_dir_getdents(node, uio, &cnt);
1674 KKASSERT(error >= -1);
1679 if (eofflag != NULL)
1681 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1683 /* Update NFS-related variables. */
1684 if (error == 0 && cookies != NULL && ncookies != NULL) {
1686 off_t off = startoff;
1687 struct tmpfs_dirent *de = NULL;
1690 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1692 for (i = 0; i < cnt; i++) {
1693 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1694 if (off == TMPFS_DIRCOOKIE_DOT) {
1695 off = TMPFS_DIRCOOKIE_DOTDOT;
1697 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1698 de = RB_MIN(tmpfs_dirtree_cookie,
1699 &node->tn_dir.tn_cookietree);
1700 } else if (de != NULL) {
1701 de = RB_NEXT(tmpfs_dirtree_cookie,
1702 &node->tn_dir.tn_cookietree, de);
1704 de = tmpfs_dir_lookupbycookie(node,
1706 KKASSERT(de != NULL);
1707 de = RB_NEXT(tmpfs_dirtree_cookie,
1708 &node->tn_dir.tn_cookietree, de);
1711 off = TMPFS_DIRCOOKIE_EOF;
1713 off = tmpfs_dircookie(de);
1715 (*cookies)[i] = off;
1717 KKASSERT(uio->uio_offset == off);
1719 TMPFS_NODE_UNLOCK(node);
1721 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1722 TMPFS_NODE_LOCK(node);
1723 node->tn_status |= TMPFS_NODE_ACCESSED;
1724 TMPFS_NODE_UNLOCK(node);
1729 /* --------------------------------------------------------------------- */
1732 tmpfs_readlink(struct vop_readlink_args *ap)
1734 struct vnode *vp = ap->a_vp;
1735 struct uio *uio = ap->a_uio;
1737 struct tmpfs_node *node;
1739 KKASSERT(uio->uio_offset == 0);
1740 KKASSERT(vp->v_type == VLNK);
1742 node = VP_TO_TMPFS_NODE(vp);
1743 TMPFS_NODE_LOCK_SH(node);
1744 error = uiomove(node->tn_link,
1745 MIN(node->tn_size, uio->uio_resid), uio);
1746 TMPFS_NODE_UNLOCK(node);
1747 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1748 TMPFS_NODE_LOCK(node);
1749 node->tn_status |= TMPFS_NODE_ACCESSED;
1750 TMPFS_NODE_UNLOCK(node);
1755 /* --------------------------------------------------------------------- */
1758 tmpfs_inactive(struct vop_inactive_args *ap)
1760 struct vnode *vp = ap->a_vp;
1761 struct tmpfs_node *node;
1765 lwkt_gettoken(&mp->mnt_token);
1766 node = VP_TO_TMPFS_NODE(vp);
1773 lwkt_reltoken(&mp->mnt_token);
1778 * Get rid of unreferenced deleted vnodes sooner rather than
1779 * later so the data memory can be recovered immediately.
1781 * We must truncate the vnode to prevent the normal reclamation
1782 * path from flushing the data for the removed file to disk.
1784 TMPFS_NODE_LOCK(node);
1785 if (node->tn_links == 0) {
1786 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1787 TMPFS_NODE_UNLOCK(node);
1788 if (node->tn_type == VREG)
1789 tmpfs_truncate(vp, 0);
1793 * We must retain any VM pages belonging to the vnode's
1794 * object as the vnode will destroy the object during a
1795 * later reclaim. We call vinvalbuf(V_SAVE) to clean
1796 * out the buffer cache.
1798 * On DragonFlyBSD, vnodes are not immediately deactivated
1799 * on the 1->0 refs, so this is a relatively optimal
1800 * operation. We have to do this in tmpfs_inactive()
1801 * because the pages will have already been thrown away
1802 * at the time tmpfs_reclaim() is called.
1804 if (node->tn_type == VREG &&
1805 node->tn_reg.tn_pages_in_aobj == 0) {
1806 vinvalbuf(vp, V_SAVE, 0, 0);
1807 KKASSERT(RB_EMPTY(&vp->v_rbdirty_tree));
1808 KKASSERT(RB_EMPTY(&vp->v_rbclean_tree));
1809 tmpfs_move_pages(vp->v_object, node->tn_reg.tn_aobj,
1810 TMPFS_MOVF_DEACTIVATE);
1811 node->tn_reg.tn_pages_in_aobj = 1;
1814 TMPFS_NODE_UNLOCK(node);
1816 lwkt_reltoken(&mp->mnt_token);
1821 /* --------------------------------------------------------------------- */
1824 tmpfs_reclaim(struct vop_reclaim_args *ap)
1826 struct vnode *vp = ap->a_vp;
1827 struct tmpfs_mount *tmp;
1828 struct tmpfs_node *node;
1832 lwkt_gettoken(&mp->mnt_token);
1834 node = VP_TO_TMPFS_NODE(vp);
1835 tmp = VFS_TO_TMPFS(vp->v_mount);
1836 KKASSERT(mp == tmp->tm_mount);
1838 TMPFS_NODE_LOCK(node);
1839 KKASSERT(node->tn_vnode == vp);
1840 node->tn_vnode = NULL;
1844 * If the node referenced by this vnode was deleted by the
1845 * user, we must free its associated data structures now that
1846 * the vnode is being reclaimed.
1848 * Directories have an extra link ref.
1850 if (node->tn_links == 0) {
1851 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1852 tmpfs_free_node(tmp, node);
1855 TMPFS_NODE_UNLOCK(node);
1857 lwkt_reltoken(&mp->mnt_token);
1859 KKASSERT(vp->v_data == NULL);
1863 /* --------------------------------------------------------------------- */
1866 tmpfs_mountctl(struct vop_mountctl_args *ap)
1868 struct tmpfs_mount *tmp;
1872 mp = ap->a_head.a_ops->head.vv_mount;
1873 lwkt_gettoken(&mp->mnt_token);
1876 case (MOUNTCTL_SET_EXPORT):
1877 tmp = (struct tmpfs_mount *) mp->mnt_data;
1879 if (ap->a_ctllen != sizeof(struct export_args))
1882 rc = vfs_export(mp, &tmp->tm_export,
1883 (const struct export_args *) ap->a_ctl);
1886 rc = vop_stdmountctl(ap);
1890 lwkt_reltoken(&mp->mnt_token);
1894 /* --------------------------------------------------------------------- */
1897 tmpfs_print(struct vop_print_args *ap)
1899 struct vnode *vp = ap->a_vp;
1901 struct tmpfs_node *node;
1903 node = VP_TO_TMPFS_NODE(vp);
1905 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1906 node, node->tn_flags, node->tn_links);
1907 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1908 node->tn_mode, node->tn_uid, node->tn_gid,
1909 (uintmax_t)node->tn_size, node->tn_status);
1911 if (vp->v_type == VFIFO)
1919 /* --------------------------------------------------------------------- */
1922 tmpfs_pathconf(struct vop_pathconf_args *ap)
1924 struct vnode *vp = ap->a_vp;
1925 int name = ap->a_name;
1926 register_t *retval = ap->a_retval;
1927 struct tmpfs_mount *tmp;
1933 case _PC_CHOWN_RESTRICTED:
1937 case _PC_FILESIZEBITS:
1938 tmp = VFS_TO_TMPFS(vp->v_mount);
1939 *retval = max(32, flsll(tmp->tm_pages_max * PAGE_SIZE) + 1);
1966 case _PC_2_SYMLINKS:
1977 /************************************************************************
1979 ************************************************************************/
1981 static void filt_tmpfsdetach(struct knote *kn);
1982 static int filt_tmpfsread(struct knote *kn, long hint);
1983 static int filt_tmpfswrite(struct knote *kn, long hint);
1984 static int filt_tmpfsvnode(struct knote *kn, long hint);
1986 static struct filterops tmpfsread_filtops =
1987 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1988 NULL, filt_tmpfsdetach, filt_tmpfsread };
1989 static struct filterops tmpfswrite_filtops =
1990 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1991 NULL, filt_tmpfsdetach, filt_tmpfswrite };
1992 static struct filterops tmpfsvnode_filtops =
1993 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1994 NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1997 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1999 struct vnode *vp = ap->a_vp;
2000 struct knote *kn = ap->a_kn;
2002 switch (kn->kn_filter) {
2004 kn->kn_fop = &tmpfsread_filtops;
2007 kn->kn_fop = &tmpfswrite_filtops;
2010 kn->kn_fop = &tmpfsvnode_filtops;
2013 return (EOPNOTSUPP);
2016 kn->kn_hook = (caddr_t)vp;
2018 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2024 filt_tmpfsdetach(struct knote *kn)
2026 struct vnode *vp = (void *)kn->kn_hook;
2028 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2032 filt_tmpfsread(struct knote *kn, long hint)
2034 struct vnode *vp = (void *)kn->kn_hook;
2035 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
2038 if (hint == NOTE_REVOKE) {
2039 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2044 * Interlock against MP races when performing this function.
2046 TMPFS_NODE_LOCK_SH(node);
2047 off = node->tn_size - kn->kn_fp->f_offset;
2048 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2049 if (kn->kn_sfflags & NOTE_OLDAPI) {
2050 TMPFS_NODE_UNLOCK(node);
2053 if (kn->kn_data == 0) {
2054 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2056 TMPFS_NODE_UNLOCK(node);
2057 return (kn->kn_data != 0);
2061 filt_tmpfswrite(struct knote *kn, long hint)
2063 if (hint == NOTE_REVOKE)
2064 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2070 filt_tmpfsvnode(struct knote *kn, long hint)
2072 if (kn->kn_sfflags & hint)
2073 kn->kn_fflags |= hint;
2074 if (hint == NOTE_REVOKE) {
2075 kn->kn_flags |= (EV_EOF | EV_NODATA);
2078 return (kn->kn_fflags != 0);
2082 * Helper to move VM pages between objects
2084 * NOTE: The vm_page_rename() dirties the page, so we can clear the
2085 * PG_NEED_COMMIT flag. If the pages are being moved into tn_aobj,
2086 * the pageout daemon will be able to page them out.
2089 tmpfs_move_pages_callback(vm_page_t p, void *data)
2091 struct rb_vm_page_scan_info *info = data;
2095 * Take control of the page
2098 if (vm_page_busy_try(p, TRUE)) {
2099 vm_page_sleep_busy(p, TRUE, "tpgmov");
2103 if (p->object != info->object || p->pindex != pindex) {
2110 * Make sure the page is not mapped. These flags might also still be
2111 * set heuristically even if we know the page is not mapped and must
2112 * be properly cleaned up.
2114 if (__predict_false((p->flags & (PG_MAPPED|PG_WRITEABLE)) != 0))
2115 vm_page_protect(p, VM_PROT_NONE);
2118 * Free or rename the page as appropriate
2120 if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) &&
2121 (p->flags & PG_SWAPPED) &&
2122 (p->flags & PG_NEED_COMMIT) == 0 &&
2125 * If the page in the backing aobj was paged out to swap
2126 * it will be clean and it is better to free it rather
2127 * than re-dirty it. We will assume that the page was
2128 * paged out to swap for a reason!
2130 * This helps avoid unnecessary swap thrashing on the page.
2133 } else if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) == 0 &&
2134 (p->flags & PG_NEED_COMMIT) == 0 &&
2137 * If the page associated with the vnode was cleaned via
2138 * a tmpfs_strategy() call, it exists as a swap block in
2139 * aobj and it is again better to free it rather than
2140 * re-dirty it. We will assume that the page was
2141 * paged out to swap for a reason!
2143 * This helps avoid unnecessary swap thrashing on the page.
2148 * Rename the page, which will also ensure that it is flagged
2149 * as dirty and check whether a swap block association exists
2150 * in the target object or not, setting appropriate flags if
2153 vm_page_rename(p, info->dest_object, pindex);
2154 vm_page_clear_commit(p);
2155 if (info->pagerflags & TMPFS_MOVF_DEACTIVATE)
2156 vm_page_deactivate(p);
2158 /* page automaticaly made dirty */
2166 tmpfs_move_pages(vm_object_t src, vm_object_t dst, int movflags)
2168 struct rb_vm_page_scan_info info;
2170 vm_object_hold(src);
2171 vm_object_hold(dst);
2173 info.dest_object = dst;
2174 info.pagerflags = movflags;
2176 if (src->paging_in_progress)
2177 vm_object_pip_wait(src, "objtfs");
2179 vm_page_rb_tree_RB_SCAN(&src->rb_memq, NULL,
2180 tmpfs_move_pages_callback, &info);
2181 } while (info.error < 0 || !RB_EMPTY(&src->rb_memq) ||
2182 src->paging_in_progress);
2183 vm_object_drop(dst);
2184 vm_object_drop(src);
2187 /* --------------------------------------------------------------------- */
2190 * vnode operations vector used for files stored in a tmpfs file system.
2192 struct vop_ops tmpfs_vnode_vops = {
2193 .vop_default = vop_defaultop,
2194 .vop_getpages = vop_stdgetpages,
2195 .vop_putpages = vop_stdputpages,
2196 .vop_ncreate = tmpfs_ncreate,
2197 .vop_nresolve = tmpfs_nresolve,
2198 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
2199 .vop_nmknod = tmpfs_nmknod,
2200 .vop_open = tmpfs_open,
2201 .vop_close = tmpfs_close,
2202 .vop_access = tmpfs_access,
2203 .vop_getattr = tmpfs_getattr,
2204 .vop_getattr_quick = tmpfs_getattr_quick,
2205 .vop_setattr = tmpfs_setattr,
2206 .vop_read = tmpfs_read,
2207 .vop_write = tmpfs_write,
2208 .vop_fsync = tmpfs_fsync,
2209 .vop_mountctl = tmpfs_mountctl,
2210 .vop_nremove = tmpfs_nremove,
2211 .vop_nlink = tmpfs_nlink,
2212 .vop_nrename = tmpfs_nrename,
2213 .vop_nmkdir = tmpfs_nmkdir,
2214 .vop_nrmdir = tmpfs_nrmdir,
2215 .vop_nsymlink = tmpfs_nsymlink,
2216 .vop_readdir = tmpfs_readdir,
2217 .vop_readlink = tmpfs_readlink,
2218 .vop_inactive = tmpfs_inactive,
2219 .vop_reclaim = tmpfs_reclaim,
2220 .vop_print = tmpfs_print,
2221 .vop_pathconf = tmpfs_pathconf,
2222 .vop_bmap = tmpfs_bmap,
2223 .vop_strategy = tmpfs_strategy,
2224 .vop_advlock = tmpfs_advlock,
2225 .vop_kqfilter = tmpfs_kqfilter