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_lite(struct vop_getattr_lite_args *ap)
406 struct vnode *vp = ap->a_vp;
407 struct vattr_lite *lvap = ap->a_lvap;
408 struct tmpfs_node *node;
410 node = VP_TO_TMPFS_NODE(vp);
414 lvap->va_type = vp->v_type;
415 lvap->va_mode = node->tn_mode;
416 lvap->va_nlink = node->tn_links;
417 lvap->va_uid = node->tn_uid;
418 lvap->va_gid = node->tn_gid;
420 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
421 vap->va_fileid = node->tn_id;
423 lvap->va_size = node->tn_size;
425 vap->va_blocksize = PAGE_SIZE;
426 vap->va_gen = node->tn_gen;
428 lvap->va_flags = node->tn_flags;
430 if (vp->v_type == VBLK || vp->v_type == VCHR) {
431 vap->va_rmajor = umajor(node->tn_rdev);
432 vap->va_rminor = uminor(node->tn_rdev);
442 /* --------------------------------------------------------------------- */
445 tmpfs_setattr(struct vop_setattr_args *ap)
447 struct vnode *vp = ap->a_vp;
448 struct vattr *vap = ap->a_vap;
449 struct ucred *cred = ap->a_cred;
450 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
454 TMPFS_NODE_LOCK(node);
455 if (error == 0 && (vap->va_flags != VNOVAL)) {
456 error = tmpfs_chflags(vp, vap->va_flags, cred);
457 kflags |= NOTE_ATTRIB;
460 if (error == 0 && (vap->va_size != VNOVAL)) {
461 /* restore any saved pages before proceeding */
462 if (node->tn_reg.tn_pages_in_aobj) {
463 tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
464 TMPFS_MOVF_FROMBACKING |
465 TMPFS_MOVF_DEACTIVATE);
466 node->tn_reg.tn_pages_in_aobj = 0;
468 if (vap->va_size > node->tn_size)
469 kflags |= NOTE_WRITE | NOTE_EXTEND;
471 kflags |= NOTE_WRITE;
472 error = tmpfs_chsize(vp, vap->va_size, cred);
475 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
476 vap->va_gid != (gid_t)VNOVAL)) {
477 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
478 kflags |= NOTE_ATTRIB;
481 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
482 error = tmpfs_chmod(vp, vap->va_mode, cred);
483 kflags |= NOTE_ATTRIB;
486 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
487 vap->va_atime.tv_nsec != VNOVAL) ||
488 (vap->va_mtime.tv_sec != VNOVAL &&
489 vap->va_mtime.tv_nsec != VNOVAL) )) {
490 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
491 vap->va_vaflags, cred);
492 kflags |= NOTE_ATTRIB;
496 * Update the node times. We give preference to the error codes
497 * generated by this function rather than the ones that may arise
501 TMPFS_NODE_UNLOCK(node);
502 tmpfs_knote(vp, kflags);
507 /* --------------------------------------------------------------------- */
510 * fsync is usually a NOP, but we must take action when unmounting or
514 tmpfs_fsync(struct vop_fsync_args *ap)
516 struct tmpfs_node *node;
517 struct vnode *vp = ap->a_vp;
519 node = VP_TO_TMPFS_NODE(vp);
522 * tmpfs vnodes typically remain dirty, avoid long syncer scans
523 * by forcing removal from the syncer list.
525 vn_syncer_remove(vp, 1);
528 if (vp->v_type == VREG) {
529 if (vp->v_flag & VRECLAIMED) {
530 if (node->tn_links == 0)
531 tmpfs_truncate(vp, 0);
533 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
540 /* --------------------------------------------------------------------- */
543 tmpfs_read(struct vop_read_args *ap)
546 struct vnode *vp = ap->a_vp;
547 struct uio *uio = ap->a_uio;
548 struct tmpfs_node *node;
559 if (uio->uio_offset < 0)
561 if (vp->v_type != VREG)
565 * Extract node, try to shortcut the operation through
566 * the VM page cache, allowing us to avoid buffer cache
569 node = VP_TO_TMPFS_NODE(vp);
570 resid = uio->uio_resid;
571 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
572 error = vop_helper_read_shortcut(ap);
575 if (uio->uio_resid == 0) {
582 * restore any saved pages before proceeding
584 if (node->tn_reg.tn_pages_in_aobj) {
585 TMPFS_NODE_LOCK(node);
586 if (node->tn_reg.tn_pages_in_aobj) {
587 tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
588 TMPFS_MOVF_FROMBACKING);
589 node->tn_reg.tn_pages_in_aobj = 0;
591 TMPFS_NODE_UNLOCK(node);
595 * Fall-through to our normal read code.
597 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
599 * Use buffer cache I/O (via tmpfs_strategy)
601 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
602 base_offset = (off_t)uio->uio_offset - offset;
603 bp = getcacheblk(vp, base_offset,
604 node->tn_blksize, GETBLK_KVABIO);
606 if (tmpfs_cluster_rd_enable) {
607 error = cluster_readx(vp, node->tn_size,
610 B_NOTMETA | B_KVABIO,
615 error = bread_kvabio(vp, base_offset,
616 node->tn_blksize, &bp);
620 kprintf("tmpfs_read bread error %d\n", error);
625 * tmpfs pretty much fiddles directly with the VM
626 * system, don't let it exhaust it or we won't play
627 * nice with other processes.
629 * Only do this if the VOP is coming from a normal
630 * read/write. The VM system handles the case for
633 if (uio->uio_segflg != UIO_NOCOPY)
636 bp->b_flags |= B_CLUSTEROK;
640 * Figure out how many bytes we can actually copy this loop.
642 len = node->tn_blksize - offset;
643 if (len > uio->uio_resid)
644 len = uio->uio_resid;
645 if (len > node->tn_size - uio->uio_offset)
646 len = (size_t)(node->tn_size - uio->uio_offset);
648 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
651 kprintf("tmpfs_read uiomove error %d\n", error);
657 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
658 TMPFS_NODE_LOCK(node);
659 node->tn_status |= TMPFS_NODE_ACCESSED;
660 TMPFS_NODE_UNLOCK(node);
666 tmpfs_write(struct vop_write_args *ap)
669 struct vnode *vp = ap->a_vp;
670 struct uio *uio = ap->a_uio;
671 struct thread *td = uio->uio_td;
672 struct tmpfs_node *node;
685 if (uio->uio_resid == 0) {
689 node = VP_TO_TMPFS_NODE(vp);
691 if (vp->v_type != VREG)
693 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
695 TMPFS_NODE_LOCK(node);
698 * restore any saved pages before proceeding
700 if (node->tn_reg.tn_pages_in_aobj) {
701 tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
702 TMPFS_MOVF_FROMBACKING);
703 node->tn_reg.tn_pages_in_aobj = 0;
706 oldsize = node->tn_size;
707 if (ap->a_ioflag & IO_APPEND)
708 uio->uio_offset = node->tn_size;
711 * Check for illegal write offsets.
713 if (uio->uio_offset + uio->uio_resid >
714 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) {
720 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN).
722 if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) {
723 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
726 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
727 ksignal(td->td_proc, SIGXFSZ);
734 * Extend the file's size if necessary
736 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
738 while (uio->uio_resid > 0) {
740 * Don't completely blow out running buffer I/O
741 * when being hit from the pageout daemon.
743 if (uio->uio_segflg == UIO_NOCOPY &&
744 (ap->a_ioflag & IO_RECURSE) == 0) {
745 bwillwrite(node->tn_blksize);
749 * Use buffer cache I/O (via tmpfs_strategy)
751 * Calculate the maximum bytes we can write to the buffer at
752 * this offset (after resizing).
754 offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
755 base_offset = (off_t)uio->uio_offset - offset;
756 len = uio->uio_resid;
757 if (len > TMPFS_BLKSIZE - offset)
758 len = TMPFS_BLKSIZE - offset;
760 if ((uio->uio_offset + len) > node->tn_size) {
761 trivial = (uio->uio_offset <= node->tn_size);
762 error = tmpfs_reg_resize(vp, uio->uio_offset + len,
769 * Read to fill in any gaps. Theoretically we could
770 * optimize this if the write covers the entire buffer
771 * and is not a UIO_NOCOPY write, however this can lead
772 * to a security violation exposing random kernel memory
773 * (whatever junk was in the backing VM pages before).
775 * So just use bread() to do the right thing.
777 error = bread_kvabio(vp, base_offset, node->tn_blksize, &bp);
779 error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
781 kprintf("tmpfs_write uiomove error %d\n", error);
786 if (uio->uio_offset > node->tn_size) {
787 node->tn_size = uio->uio_offset;
788 kflags |= NOTE_EXTEND;
790 kflags |= NOTE_WRITE;
793 * UIO_NOCOPY is a sensitive state due to potentially being
794 * issued from the pageout daemon while in a low-memory
795 * situation. However, in order to cluster the I/O nicely
796 * (e.g. 64KB+ writes instead of 16KB writes), we still try
797 * to follow the same semantics that any other filesystem
800 * For the normal case we buwrite(), dirtying the underlying
801 * VM pages instead of dirtying the buffer and releasing the
802 * buffer as a clean buffer. This allows tmpfs to use
803 * essentially all available memory to cache file data.
804 * If we used bdwrite() the buffer cache would wind up
805 * flushing the data to swap too quickly.
807 * But because tmpfs can seriously load the VM system we
808 * fall-back to using bdwrite() when free memory starts
809 * to get low. This shifts the load away from the VM system
810 * and makes tmpfs act more like a normal filesystem with
811 * regards to disk activity.
813 * tmpfs pretty much fiddles directly with the VM
814 * system, don't let it exhaust it or we won't play
815 * nice with other processes. Only do this if the
816 * VOP is coming from a normal read/write. The VM system
817 * handles the case for UIO_NOCOPY.
819 bp->b_flags |= B_CLUSTEROK;
820 if (uio->uio_segflg == UIO_NOCOPY) {
822 * Flush from the pageout daemon, deal with potentially
823 * very heavy tmpfs write activity causing long stalls
824 * in the pageout daemon before pages get to free/cache.
826 * We have to be careful not to bypass the page queues
827 * entirely or we can cause write-read thrashing and
828 * delay the paging of data that is more pageable then
831 * (a) Under severe pressure setting B_DIRECT will
832 * cause a buffer release to try to free the
835 * (b) Under modest memory pressure the B_AGE flag
836 * we retire the buffer and its underlying pages
837 * more quickly than normal.
839 * We could also force this by setting B_NOTMETA
840 * but that might have other unintended side-
841 * effects (e.g. setting PG_NOTMETA on the VM page).
843 * (c) For the pageout->putpages->generic_putpages->
844 * UIO_NOCOPY-write (here), issuing an immediate
845 * write prevents any real clustering from
846 * happening because the buffers probably aren't
847 * (yet) marked dirty, or lost due to prior use
848 * of buwrite(). Try to use the normal
849 * cluster_write() mechanism for performance.
851 * Hopefully this will unblock the VM system more
852 * quickly under extreme tmpfs write load.
854 if (tmpfs_bufcache_mode >= 2) {
855 if (vm_page_count_min(vm_page_free_hysteresis))
856 bp->b_flags |= B_DIRECT | B_TTC;
857 if (vm_pages_needed || vm_paging_needed(0))
858 bp->b_flags |= B_AGE;
860 bp->b_flags |= B_RELBUF;
861 bp->b_act_count = 0; /* buffer->deactivate pgs */
862 if (tmpfs_cluster_wr_enable &&
863 (ap->a_ioflag & (IO_SYNC | IO_DIRECT)) == 0) {
864 cluster_write(bp, node->tn_size,
865 node->tn_blksize, seqcount);
869 } else if (vm_page_count_min(0) ||
870 ((vm_pages_needed || vm_paging_needed(0)) &&
871 tmpfs_bufcache_mode >= 1)) {
873 * If the pageout daemon is running we cycle the
874 * write through the buffer cache normally to
875 * pipeline the flush, thus avoiding adding any
876 * more memory pressure to the pageout daemon.
878 bp->b_act_count = 0; /* buffer->deactivate pgs */
879 if (tmpfs_cluster_wr_enable) {
880 cluster_write(bp, node->tn_size,
881 node->tn_blksize, seqcount);
887 * Otherwise run the buffer directly through to the
888 * backing VM store, leaving the buffer clean so
889 * buffer limits do not force early flushes to swap.
892 /*vm_wait_nominal();*/
896 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
903 (void)tmpfs_reg_resize(vp, oldsize, trivial);
904 kflags &= ~NOTE_EXTEND;
910 * Currently we don't set the mtime on files modified via mmap()
911 * because we can't tell the difference between those modifications
912 * and an attempt by the pageout daemon to flush tmpfs pages to
915 * This is because in order to defer flushes as long as possible
916 * buwrite() works by marking the underlying VM pages dirty in
917 * order to be able to dispose of the buffer cache buffer without
920 if (uio->uio_segflg == UIO_NOCOPY) {
921 if (vp->v_flag & VLASTWRITETS) {
922 node->tn_mtime = vp->v_lastwrite_ts.tv_sec;
923 node->tn_mtimensec = vp->v_lastwrite_ts.tv_nsec;
926 node->tn_status |= TMPFS_NODE_MODIFIED;
927 vclrflags(vp, VLASTWRITETS);
931 node->tn_status |= TMPFS_NODE_CHANGED;
933 if (node->tn_mode & (S_ISUID | S_ISGID)) {
934 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
935 node->tn_mode &= ~(S_ISUID | S_ISGID);
938 TMPFS_NODE_UNLOCK(node);
940 tmpfs_knote(vp, kflags);
946 tmpfs_advlock(struct vop_advlock_args *ap)
948 struct tmpfs_node *node;
949 struct vnode *vp = ap->a_vp;
952 node = VP_TO_TMPFS_NODE(vp);
953 error = (lf_advlock(ap, &node->tn_advlock, node->tn_size));
959 * The strategy function is typically only called when memory pressure
960 * forces the system to attempt to pageout pages. It can also be called
961 * by [n]vtruncbuf() when a truncation cuts a page in half. Normal write
964 * We set VKVABIO for VREG files so bp->b_data may not be synchronized to
965 * our cpu. swap_pager_strategy() is all we really use, and it directly
969 tmpfs_strategy(struct vop_strategy_args *ap)
971 struct bio *bio = ap->a_bio;
973 struct buf *bp = bio->bio_buf;
974 struct vnode *vp = ap->a_vp;
975 struct tmpfs_node *node;
980 if (vp->v_type != VREG) {
981 bp->b_resid = bp->b_bcount;
982 bp->b_flags |= B_ERROR | B_INVAL;
983 bp->b_error = EINVAL;
988 node = VP_TO_TMPFS_NODE(vp);
990 uobj = node->tn_reg.tn_aobj;
993 * Don't bother flushing to swap if there is no swap, just
994 * ensure that the pages are marked as needing a commit (still).
996 if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) {
997 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
998 m = bp->b_xio.xio_pages[i];
999 vm_page_need_commit(m);
1007 * XXX removed, this does not work well because under heavy
1008 * filesystem loads it often
1009 * forces the data to be read right back in again after
1010 * being written due to bypassing normal LRU operation.
1012 * Tell the buffer cache to try to recycle the pages
1013 * to PQ_CACHE on release.
1015 if (tmpfs_bufcache_mode >= 2 ||
1016 (tmpfs_bufcache_mode == 1 && vm_paging_needed(0))) {
1017 bp->b_flags |= B_TTC;
1020 nbio = push_bio(bio);
1021 nbio->bio_done = tmpfs_strategy_done;
1022 nbio->bio_offset = bio->bio_offset;
1023 swap_pager_strategy(uobj, nbio);
1029 * If we were unable to commit the pages to swap make sure they are marked
1030 * as needing a commit (again). If we were, clear the flag to allow the
1031 * pages to be freed.
1033 * Do not error-out the buffer. In particular, vinvalbuf() needs to
1037 tmpfs_strategy_done(struct bio *bio)
1045 if (bp->b_flags & B_ERROR) {
1046 bp->b_flags &= ~B_ERROR;
1049 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1050 m = bp->b_xio.xio_pages[i];
1051 vm_page_need_commit(m);
1054 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1055 m = bp->b_xio.xio_pages[i];
1056 vm_page_clear_commit(m);
1064 * To make write clustering work well make the backing store look
1065 * contiguous to the cluster_*() code. The swap_strategy() function
1066 * will take it from there.
1068 * Use MAXBSIZE-sized chunks as a micro-optimization to make random
1069 * flushes leave full-sized gaps.
1072 tmpfs_bmap(struct vop_bmap_args *ap)
1074 if (ap->a_doffsetp != NULL)
1075 *ap->a_doffsetp = ap->a_loffset;
1076 if (ap->a_runp != NULL)
1077 *ap->a_runp = MAXBSIZE - (ap->a_loffset & (MAXBSIZE - 1));
1078 if (ap->a_runb != NULL)
1079 *ap->a_runb = ap->a_loffset & (MAXBSIZE - 1);
1084 /* --------------------------------------------------------------------- */
1087 tmpfs_nremove(struct vop_nremove_args *ap)
1089 struct vnode *dvp = ap->a_dvp;
1090 struct namecache *ncp = ap->a_nch->ncp;
1093 struct tmpfs_dirent *de;
1094 struct tmpfs_mount *tmp;
1095 struct tmpfs_node *dnode;
1096 struct tmpfs_node *node;
1099 * We have to acquire the vp from ap->a_nch because we will likely
1100 * unresolve the namecache entry, and a vrele/vput is needed to
1101 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1103 * We have to use vget to clear any inactive state on the vnode,
1104 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1105 * will not get called when we release it.
1107 error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp);
1108 KKASSERT(vp->v_mount == dvp->v_mount);
1109 KKASSERT(error == 0);
1112 if (vp->v_type == VDIR) {
1117 dnode = VP_TO_TMPFS_DIR(dvp);
1118 node = VP_TO_TMPFS_NODE(vp);
1119 tmp = VFS_TO_TMPFS(vp->v_mount);
1121 TMPFS_NODE_LOCK(dnode);
1122 TMPFS_NODE_LOCK(node);
1123 de = tmpfs_dir_lookup(dnode, node, ncp);
1126 TMPFS_NODE_UNLOCK(node);
1127 TMPFS_NODE_UNLOCK(dnode);
1131 /* Files marked as immutable or append-only cannot be deleted. */
1132 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
1133 (dnode->tn_flags & APPEND)) {
1135 TMPFS_NODE_UNLOCK(node);
1136 TMPFS_NODE_UNLOCK(dnode);
1140 /* Remove the entry from the directory; as it is a file, we do not
1141 * have to change the number of hard links of the directory. */
1142 tmpfs_dir_detach_locked(dnode, de);
1143 TMPFS_NODE_UNLOCK(dnode);
1145 /* Free the directory entry we just deleted. Note that the node
1146 * referred by it will not be removed until the vnode is really
1148 tmpfs_free_dirent(tmp, de);
1150 if (node->tn_links > 0)
1151 node->tn_status |= TMPFS_NODE_CHANGED;
1152 TMPFS_NODE_UNLOCK(node);
1154 cache_unlink(ap->a_nch);
1155 tmpfs_knote(vp, NOTE_DELETE);
1160 tmpfs_knote(dvp, NOTE_WRITE);
1167 /* --------------------------------------------------------------------- */
1170 tmpfs_nlink(struct vop_nlink_args *ap)
1172 struct vnode *dvp = ap->a_dvp;
1173 struct vnode *vp = ap->a_vp;
1174 struct tmpfs_mount *tmp = VFS_TO_TMPFS(vp->v_mount);
1175 struct namecache *ncp = ap->a_nch->ncp;
1176 struct tmpfs_dirent *de;
1177 struct tmpfs_node *node;
1178 struct tmpfs_node *dnode;
1181 KKASSERT(dvp != vp); /* XXX When can this be false? */
1183 node = VP_TO_TMPFS_NODE(vp);
1184 dnode = VP_TO_TMPFS_NODE(dvp);
1185 TMPFS_NODE_LOCK(dnode);
1187 /* XXX: Why aren't the following two tests done by the caller? */
1189 /* Hard links of directories are forbidden. */
1190 if (vp->v_type == VDIR) {
1195 /* Cannot create cross-device links. */
1196 if (dvp->v_mount != vp->v_mount) {
1201 /* Cannot hard-link into a deleted directory */
1202 if (dnode != tmp->tm_root && dnode->tn_dir.tn_parent == NULL) {
1207 /* Ensure that we do not overflow the maximum number of links imposed
1209 KKASSERT(node->tn_links <= LINK_MAX);
1210 if (node->tn_links >= LINK_MAX) {
1215 /* We cannot create links of files marked immutable or append-only. */
1216 if (node->tn_flags & (IMMUTABLE | APPEND)) {
1221 /* Allocate a new directory entry to represent the node. */
1222 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
1223 ncp->nc_name, ncp->nc_nlen, &de);
1227 /* Insert the new directory entry into the appropriate directory. */
1228 tmpfs_dir_attach_locked(dnode, de);
1230 /* vp link count has changed, so update node times. */
1232 TMPFS_NODE_LOCK(node);
1233 node->tn_status |= TMPFS_NODE_CHANGED;
1234 TMPFS_NODE_UNLOCK(node);
1237 tmpfs_knote(vp, NOTE_LINK);
1238 cache_setunresolved(ap->a_nch);
1239 cache_setvp(ap->a_nch, vp);
1243 TMPFS_NODE_UNLOCK(dnode);
1245 tmpfs_knote(dvp, NOTE_WRITE);
1249 /* --------------------------------------------------------------------- */
1252 tmpfs_nrename(struct vop_nrename_args *ap)
1254 struct vnode *fdvp = ap->a_fdvp;
1255 struct namecache *fncp = ap->a_fnch->ncp;
1256 struct vnode *fvp = fncp->nc_vp;
1257 struct vnode *tdvp = ap->a_tdvp;
1258 struct namecache *tncp = ap->a_tnch->ncp;
1260 struct tmpfs_dirent *de, *tde, *de2;
1261 struct tmpfs_mount *tmp;
1262 struct tmpfs_node *fdnode;
1263 struct tmpfs_node *tdnode;
1264 struct tmpfs_node *fnode;
1265 struct tmpfs_node *tnode;
1270 KKASSERT(fdvp->v_mount == fvp->v_mount);
1273 * Because tvp can get overwritten we have to vget it instead of
1274 * just vref or use it, otherwise it's VINACTIVE flag may not get
1275 * cleared and the node won't get destroyed.
1277 error = cache_vget(ap->a_tnch, ap->a_cred, LK_SHARED, &tvp);
1279 tnode = VP_TO_TMPFS_NODE(tvp);
1285 /* Disallow cross-device renames.
1286 * XXX Why isn't this done by the caller? */
1287 if (fvp->v_mount != tdvp->v_mount ||
1288 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1293 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1294 tdnode = VP_TO_TMPFS_DIR(tdvp);
1296 /* If source and target are the same file, there is nothing to do. */
1302 fdnode = VP_TO_TMPFS_DIR(fdvp);
1303 fnode = VP_TO_TMPFS_NODE(fvp);
1305 tmpfs_lock4(fdnode, tdnode, fnode, tnode);
1308 * Cannot rename into a deleted directory
1310 if (tdnode != tmp->tm_root && tdnode->tn_dir.tn_parent == NULL) {
1315 /* Avoid manipulating '.' and '..' entries. */
1316 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
1321 KKASSERT(de->td_node == fnode);
1324 * If replacing an entry in the target directory and that entry
1325 * is a directory, it must be empty.
1327 * Kern_rename gurantees the destination to be a directory
1328 * if the source is one (it does?).
1331 KKASSERT(tnode != NULL);
1333 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1334 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1339 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1340 if (tnode->tn_size > 0) {
1344 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1347 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1351 KKASSERT(fnode->tn_type != VDIR &&
1352 tnode->tn_type != VDIR);
1356 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1357 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1363 * Ensure that we have enough memory to hold the new name, if it
1364 * has to be changed.
1366 if (fncp->nc_nlen != tncp->nc_nlen ||
1367 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
1368 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
1369 M_WAITOK | M_NULLOK);
1370 if (newname == NULL) {
1374 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1375 newname[tncp->nc_nlen] = '\0';
1381 * Unlink entry from source directory. Note that the kernel has
1382 * already checked for illegal recursion cases (renaming a directory
1383 * into a subdirectory of itself).
1385 if (fdnode != tdnode) {
1386 tmpfs_dir_detach_locked(fdnode, de);
1388 /* XXX depend on namecache lock */
1389 KKASSERT(de == tmpfs_dir_lookup(fdnode, fnode, fncp));
1390 RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de);
1391 RB_REMOVE(tmpfs_dirtree_cookie,
1392 &fdnode->tn_dir.tn_cookietree, de);
1396 * Handle any name change. Swap with newname, we will
1397 * deallocate it at the end.
1399 if (newname != NULL) {
1400 oldname = de->td_name;
1401 de->td_name = newname;
1402 de->td_namelen = (uint16_t)tncp->nc_nlen;
1407 * If we are overwriting an entry, we have to remove the old one
1408 * from the target directory.
1411 /* Remove the old entry from the target directory. */
1412 tde = tmpfs_dir_lookup(tdnode, tnode, tncp);
1413 tmpfs_dir_detach_locked(tdnode, tde);
1414 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1417 * Free the directory entry we just deleted. Note that the
1418 * node referred by it will not be removed until the vnode is
1421 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1422 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1426 * Link entry to target directory. If the entry
1427 * represents a directory move the parent linkage
1430 if (fdnode != tdnode) {
1431 if (de->td_node->tn_type == VDIR) {
1432 TMPFS_VALIDATE_DIR(fnode);
1434 tmpfs_dir_attach_locked(tdnode, de);
1436 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1437 de2 = RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de);
1438 KASSERT(de2 == NULL,
1439 ("tmpfs_nrenameA: duplicate insertion of %p, has %p\n",
1441 de2 = RB_INSERT(tmpfs_dirtree_cookie,
1442 &tdnode->tn_dir.tn_cookietree, de);
1443 KASSERT(de2 == NULL,
1444 ("tmpfs_nrenameB: duplicate insertion of %p, has %p\n",
1447 tmpfs_unlock4(fdnode, tdnode, fnode, tnode);
1453 kfree(newname, tmp->tm_name_zone);
1456 cache_rename(ap->a_fnch, ap->a_tnch);
1457 tmpfs_knote(ap->a_fdvp, NOTE_WRITE);
1458 tmpfs_knote(ap->a_tdvp, NOTE_WRITE);
1459 if (fnode->tn_vnode)
1460 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1466 tmpfs_unlock4(fdnode, tdnode, fnode, tnode);
1473 /* --------------------------------------------------------------------- */
1476 tmpfs_nmkdir(struct vop_nmkdir_args *ap)
1478 struct vnode *dvp = ap->a_dvp;
1479 struct vnode **vpp = ap->a_vpp;
1480 struct namecache *ncp = ap->a_nch->ncp;
1481 struct vattr *vap = ap->a_vap;
1482 struct ucred *cred = ap->a_cred;
1485 KKASSERT(vap->va_type == VDIR);
1487 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1489 cache_setunresolved(ap->a_nch);
1490 cache_setvp(ap->a_nch, *vpp);
1491 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1496 /* --------------------------------------------------------------------- */
1499 tmpfs_nrmdir(struct vop_nrmdir_args *ap)
1501 struct vnode *dvp = ap->a_dvp;
1502 struct namecache *ncp = ap->a_nch->ncp;
1504 struct tmpfs_dirent *de;
1505 struct tmpfs_mount *tmp;
1506 struct tmpfs_node *dnode;
1507 struct tmpfs_node *node;
1511 * We have to acquire the vp from ap->a_nch because we will likely
1512 * unresolve the namecache entry, and a vrele/vput is needed to
1513 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1515 * We have to use vget to clear any inactive state on the vnode,
1516 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1517 * will not get called when we release it.
1519 error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp);
1520 KKASSERT(error == 0);
1524 * Prevalidate so we don't hit an assertion later
1526 if (vp->v_type != VDIR) {
1531 tmp = VFS_TO_TMPFS(dvp->v_mount);
1532 dnode = VP_TO_TMPFS_DIR(dvp);
1533 node = VP_TO_TMPFS_DIR(vp);
1538 TMPFS_NODE_LOCK(dnode);
1539 TMPFS_NODE_LOCK(node);
1542 * Only empty directories can be removed.
1544 if (node->tn_size > 0) {
1549 if ((dnode->tn_flags & APPEND)
1550 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1556 * This invariant holds only if we are not trying to
1557 * remove "..". We checked for that above so this is safe now.
1559 KKASSERT(node->tn_dir.tn_parent == dnode);
1562 * Get the directory entry associated with node (vp)
1564 de = tmpfs_dir_lookup(dnode, node, ncp);
1565 KKASSERT(TMPFS_DIRENT_MATCHES(de, ncp->nc_name, ncp->nc_nlen));
1567 /* Check flags to see if we are allowed to remove the directory. */
1568 if ((dnode->tn_flags & APPEND) ||
1569 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1574 /* Detach the directory entry from the directory (dnode). */
1575 tmpfs_dir_detach_locked(dnode, de);
1578 * Must set parent linkage to NULL (tested by ncreate to disallow
1579 * the creation of new files/dirs in a deleted directory)
1581 node->tn_status |= TMPFS_NODE_CHANGED;
1583 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1584 TMPFS_NODE_MODIFIED;
1586 /* Free the directory entry we just deleted. Note that the node
1587 * referred by it will not be removed until the vnode is really
1589 tmpfs_free_dirent(tmp, de);
1591 /* Release the deleted vnode (will destroy the node, notify
1592 * interested parties and clean it from the cache). */
1594 dnode->tn_status |= TMPFS_NODE_CHANGED;
1596 TMPFS_NODE_UNLOCK(node);
1597 TMPFS_NODE_UNLOCK(dnode);
1600 cache_unlink(ap->a_nch);
1601 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1606 TMPFS_NODE_UNLOCK(node);
1607 TMPFS_NODE_UNLOCK(dnode);
1615 /* --------------------------------------------------------------------- */
1618 tmpfs_nsymlink(struct vop_nsymlink_args *ap)
1620 struct vnode *dvp = ap->a_dvp;
1621 struct vnode **vpp = ap->a_vpp;
1622 struct namecache *ncp = ap->a_nch->ncp;
1623 struct vattr *vap = ap->a_vap;
1624 struct ucred *cred = ap->a_cred;
1625 char *target = ap->a_target;
1628 vap->va_type = VLNK;
1629 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1631 tmpfs_knote(*vpp, NOTE_WRITE);
1632 cache_setunresolved(ap->a_nch);
1633 cache_setvp(ap->a_nch, *vpp);
1638 /* --------------------------------------------------------------------- */
1641 tmpfs_readdir(struct vop_readdir_args *ap)
1643 struct vnode *vp = ap->a_vp;
1644 struct uio *uio = ap->a_uio;
1645 int *eofflag = ap->a_eofflag;
1646 off_t **cookies = ap->a_cookies;
1647 int *ncookies = ap->a_ncookies;
1648 struct tmpfs_mount *tmp;
1652 struct tmpfs_node *node;
1654 /* This operation only makes sense on directory nodes. */
1655 if (vp->v_type != VDIR) {
1659 tmp = VFS_TO_TMPFS(vp->v_mount);
1660 node = VP_TO_TMPFS_DIR(vp);
1661 startoff = uio->uio_offset;
1663 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1664 error = tmpfs_dir_getdotdent(node, uio);
1666 TMPFS_NODE_LOCK_SH(node);
1672 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1673 /* may lock parent, cannot hold node lock */
1674 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1676 TMPFS_NODE_LOCK_SH(node);
1682 TMPFS_NODE_LOCK_SH(node);
1683 error = tmpfs_dir_getdents(node, uio, &cnt);
1686 KKASSERT(error >= -1);
1691 if (eofflag != NULL)
1693 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1695 /* Update NFS-related variables. */
1696 if (error == 0 && cookies != NULL && ncookies != NULL) {
1698 off_t off = startoff;
1699 struct tmpfs_dirent *de = NULL;
1702 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1704 for (i = 0; i < cnt; i++) {
1705 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1706 if (off == TMPFS_DIRCOOKIE_DOT) {
1707 off = TMPFS_DIRCOOKIE_DOTDOT;
1709 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1710 de = RB_MIN(tmpfs_dirtree_cookie,
1711 &node->tn_dir.tn_cookietree);
1712 } else if (de != NULL) {
1713 de = RB_NEXT(tmpfs_dirtree_cookie,
1714 &node->tn_dir.tn_cookietree, de);
1716 de = tmpfs_dir_lookupbycookie(node,
1718 KKASSERT(de != NULL);
1719 de = RB_NEXT(tmpfs_dirtree_cookie,
1720 &node->tn_dir.tn_cookietree, de);
1723 off = TMPFS_DIRCOOKIE_EOF;
1725 off = tmpfs_dircookie(de);
1727 (*cookies)[i] = off;
1729 KKASSERT(uio->uio_offset == off);
1731 TMPFS_NODE_UNLOCK(node);
1733 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1734 TMPFS_NODE_LOCK(node);
1735 node->tn_status |= TMPFS_NODE_ACCESSED;
1736 TMPFS_NODE_UNLOCK(node);
1741 /* --------------------------------------------------------------------- */
1744 tmpfs_readlink(struct vop_readlink_args *ap)
1746 struct vnode *vp = ap->a_vp;
1747 struct uio *uio = ap->a_uio;
1749 struct tmpfs_node *node;
1751 KKASSERT(uio->uio_offset == 0);
1752 KKASSERT(vp->v_type == VLNK);
1754 node = VP_TO_TMPFS_NODE(vp);
1755 TMPFS_NODE_LOCK_SH(node);
1756 error = uiomove(node->tn_link,
1757 MIN(node->tn_size, uio->uio_resid), uio);
1758 TMPFS_NODE_UNLOCK(node);
1759 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1760 TMPFS_NODE_LOCK(node);
1761 node->tn_status |= TMPFS_NODE_ACCESSED;
1762 TMPFS_NODE_UNLOCK(node);
1767 /* --------------------------------------------------------------------- */
1770 tmpfs_inactive(struct vop_inactive_args *ap)
1772 struct vnode *vp = ap->a_vp;
1773 struct tmpfs_node *node;
1777 lwkt_gettoken(&mp->mnt_token);
1778 node = VP_TO_TMPFS_NODE(vp);
1785 lwkt_reltoken(&mp->mnt_token);
1790 * Get rid of unreferenced deleted vnodes sooner rather than
1791 * later so the data memory can be recovered immediately.
1793 * We must truncate the vnode to prevent the normal reclamation
1794 * path from flushing the data for the removed file to disk.
1796 TMPFS_NODE_LOCK(node);
1797 if (node->tn_links == 0) {
1798 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1799 TMPFS_NODE_UNLOCK(node);
1800 if (node->tn_type == VREG)
1801 tmpfs_truncate(vp, 0);
1805 * We must retain any VM pages belonging to the vnode's
1806 * object as the vnode will destroy the object during a
1807 * later reclaim. We call vinvalbuf(V_SAVE) to clean
1808 * out the buffer cache.
1810 * On DragonFlyBSD, vnodes are not immediately deactivated
1811 * on the 1->0 refs, so this is a relatively optimal
1812 * operation. We have to do this in tmpfs_inactive()
1813 * because the pages will have already been thrown away
1814 * at the time tmpfs_reclaim() is called.
1816 if (node->tn_type == VREG &&
1817 node->tn_reg.tn_pages_in_aobj == 0) {
1818 vinvalbuf(vp, V_SAVE, 0, 0);
1819 KKASSERT(RB_EMPTY(&vp->v_rbdirty_tree));
1820 KKASSERT(RB_EMPTY(&vp->v_rbclean_tree));
1821 tmpfs_move_pages(vp->v_object, node->tn_reg.tn_aobj,
1822 TMPFS_MOVF_DEACTIVATE);
1823 node->tn_reg.tn_pages_in_aobj = 1;
1826 TMPFS_NODE_UNLOCK(node);
1828 lwkt_reltoken(&mp->mnt_token);
1833 /* --------------------------------------------------------------------- */
1836 tmpfs_reclaim(struct vop_reclaim_args *ap)
1838 struct vnode *vp = ap->a_vp;
1839 struct tmpfs_mount *tmp;
1840 struct tmpfs_node *node;
1844 lwkt_gettoken(&mp->mnt_token);
1846 node = VP_TO_TMPFS_NODE(vp);
1847 tmp = VFS_TO_TMPFS(vp->v_mount);
1848 KKASSERT(mp == tmp->tm_mount);
1850 TMPFS_NODE_LOCK(node);
1851 KKASSERT(node->tn_vnode == vp);
1852 node->tn_vnode = NULL;
1856 * If the node referenced by this vnode was deleted by the
1857 * user, we must free its associated data structures now that
1858 * the vnode is being reclaimed.
1860 * Directories have an extra link ref.
1862 if (node->tn_links == 0) {
1863 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1864 tmpfs_free_node(tmp, node);
1867 TMPFS_NODE_UNLOCK(node);
1869 lwkt_reltoken(&mp->mnt_token);
1871 KKASSERT(vp->v_data == NULL);
1875 /* --------------------------------------------------------------------- */
1878 tmpfs_mountctl(struct vop_mountctl_args *ap)
1880 struct tmpfs_mount *tmp;
1884 mp = ap->a_head.a_ops->head.vv_mount;
1885 lwkt_gettoken(&mp->mnt_token);
1888 case (MOUNTCTL_SET_EXPORT):
1889 tmp = (struct tmpfs_mount *) mp->mnt_data;
1891 if (ap->a_ctllen != sizeof(struct export_args))
1894 rc = vfs_export(mp, &tmp->tm_export,
1895 (const struct export_args *) ap->a_ctl);
1898 rc = vop_stdmountctl(ap);
1902 lwkt_reltoken(&mp->mnt_token);
1906 /* --------------------------------------------------------------------- */
1909 tmpfs_print(struct vop_print_args *ap)
1911 struct vnode *vp = ap->a_vp;
1913 struct tmpfs_node *node;
1915 node = VP_TO_TMPFS_NODE(vp);
1917 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1918 node, node->tn_flags, node->tn_links);
1919 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1920 node->tn_mode, node->tn_uid, node->tn_gid,
1921 (uintmax_t)node->tn_size, node->tn_status);
1923 if (vp->v_type == VFIFO)
1931 /* --------------------------------------------------------------------- */
1934 tmpfs_pathconf(struct vop_pathconf_args *ap)
1936 struct vnode *vp = ap->a_vp;
1937 int name = ap->a_name;
1938 register_t *retval = ap->a_retval;
1939 struct tmpfs_mount *tmp;
1945 case _PC_CHOWN_RESTRICTED:
1949 case _PC_FILESIZEBITS:
1950 tmp = VFS_TO_TMPFS(vp->v_mount);
1951 *retval = max(32, flsll(tmp->tm_pages_max * PAGE_SIZE) + 1);
1978 case _PC_2_SYMLINKS:
1989 /************************************************************************
1991 ************************************************************************/
1993 static void filt_tmpfsdetach(struct knote *kn);
1994 static int filt_tmpfsread(struct knote *kn, long hint);
1995 static int filt_tmpfswrite(struct knote *kn, long hint);
1996 static int filt_tmpfsvnode(struct knote *kn, long hint);
1998 static struct filterops tmpfsread_filtops =
1999 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2000 NULL, filt_tmpfsdetach, filt_tmpfsread };
2001 static struct filterops tmpfswrite_filtops =
2002 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2003 NULL, filt_tmpfsdetach, filt_tmpfswrite };
2004 static struct filterops tmpfsvnode_filtops =
2005 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2006 NULL, filt_tmpfsdetach, filt_tmpfsvnode };
2009 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
2011 struct vnode *vp = ap->a_vp;
2012 struct knote *kn = ap->a_kn;
2014 switch (kn->kn_filter) {
2016 kn->kn_fop = &tmpfsread_filtops;
2019 kn->kn_fop = &tmpfswrite_filtops;
2022 kn->kn_fop = &tmpfsvnode_filtops;
2025 return (EOPNOTSUPP);
2028 kn->kn_hook = (caddr_t)vp;
2030 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2036 filt_tmpfsdetach(struct knote *kn)
2038 struct vnode *vp = (void *)kn->kn_hook;
2040 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2044 filt_tmpfsread(struct knote *kn, long hint)
2046 struct vnode *vp = (void *)kn->kn_hook;
2047 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
2050 if (hint == NOTE_REVOKE) {
2051 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2056 * Interlock against MP races when performing this function.
2058 TMPFS_NODE_LOCK_SH(node);
2059 off = node->tn_size - kn->kn_fp->f_offset;
2060 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2061 if (kn->kn_sfflags & NOTE_OLDAPI) {
2062 TMPFS_NODE_UNLOCK(node);
2065 if (kn->kn_data == 0) {
2066 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2068 TMPFS_NODE_UNLOCK(node);
2069 return (kn->kn_data != 0);
2073 filt_tmpfswrite(struct knote *kn, long hint)
2075 if (hint == NOTE_REVOKE)
2076 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2082 filt_tmpfsvnode(struct knote *kn, long hint)
2084 if (kn->kn_sfflags & hint)
2085 kn->kn_fflags |= hint;
2086 if (hint == NOTE_REVOKE) {
2087 kn->kn_flags |= (EV_EOF | EV_NODATA);
2090 return (kn->kn_fflags != 0);
2094 * Helper to move VM pages between objects
2096 * NOTE: The vm_page_rename() dirties the page, so we can clear the
2097 * PG_NEED_COMMIT flag. If the pages are being moved into tn_aobj,
2098 * the pageout daemon will be able to page them out.
2101 tmpfs_move_pages_callback(vm_page_t p, void *data)
2103 struct rb_vm_page_scan_info *info = data;
2107 * Take control of the page
2110 if (vm_page_busy_try(p, TRUE)) {
2111 vm_page_sleep_busy(p, TRUE, "tpgmov");
2115 if (p->object != info->object || p->pindex != pindex) {
2122 * Make sure the page is not mapped. These flags might also still be
2123 * set heuristically even if we know the page is not mapped and must
2124 * be properly cleaned up.
2126 if (__predict_false((p->flags & (PG_MAPPED|PG_WRITEABLE)) != 0))
2127 vm_page_protect(p, VM_PROT_NONE);
2130 * Free or rename the page as appropriate
2132 if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) &&
2133 (p->flags & PG_SWAPPED) &&
2134 (p->flags & PG_NEED_COMMIT) == 0 &&
2137 * If the page in the backing aobj was paged out to swap
2138 * it will be clean and it is better to free it rather
2139 * than re-dirty it. We will assume that the page was
2140 * paged out to swap for a reason!
2142 * This helps avoid unnecessary swap thrashing on the page.
2145 } else if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) == 0 &&
2146 (p->flags & PG_NEED_COMMIT) == 0 &&
2149 * If the page associated with the vnode was cleaned via
2150 * a tmpfs_strategy() call, it exists as a swap block in
2151 * aobj and it is again better to free it rather than
2152 * re-dirty it. We will assume that the page was
2153 * paged out to swap for a reason!
2155 * This helps avoid unnecessary swap thrashing on the page.
2160 * Rename the page, which will also ensure that it is flagged
2161 * as dirty and check whether a swap block association exists
2162 * in the target object or not, setting appropriate flags if
2165 vm_page_rename(p, info->dest_object, pindex);
2166 vm_page_clear_commit(p);
2167 if (info->pagerflags & TMPFS_MOVF_DEACTIVATE)
2168 vm_page_deactivate(p);
2170 /* page automaticaly made dirty */
2178 tmpfs_move_pages(vm_object_t src, vm_object_t dst, int movflags)
2180 struct rb_vm_page_scan_info info;
2182 vm_object_hold(src);
2183 vm_object_hold(dst);
2185 info.dest_object = dst;
2186 info.pagerflags = movflags;
2188 if (src->paging_in_progress)
2189 vm_object_pip_wait(src, "objtfs");
2191 vm_page_rb_tree_RB_SCAN(&src->rb_memq, NULL,
2192 tmpfs_move_pages_callback, &info);
2193 } while (info.error < 0 || !RB_EMPTY(&src->rb_memq) ||
2194 src->paging_in_progress);
2195 vm_object_drop(dst);
2196 vm_object_drop(src);
2199 /* --------------------------------------------------------------------- */
2202 * vnode operations vector used for files stored in a tmpfs file system.
2204 struct vop_ops tmpfs_vnode_vops = {
2205 .vop_default = vop_defaultop,
2206 .vop_getpages = vop_stdgetpages,
2207 .vop_putpages = vop_stdputpages,
2208 .vop_ncreate = tmpfs_ncreate,
2209 .vop_nresolve = tmpfs_nresolve,
2210 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
2211 .vop_nmknod = tmpfs_nmknod,
2212 .vop_open = tmpfs_open,
2213 .vop_close = tmpfs_close,
2214 .vop_access = tmpfs_access,
2215 .vop_getattr = tmpfs_getattr,
2216 .vop_getattr_lite = tmpfs_getattr_lite,
2217 .vop_setattr = tmpfs_setattr,
2218 .vop_read = tmpfs_read,
2219 .vop_write = tmpfs_write,
2220 .vop_fsync = tmpfs_fsync,
2221 .vop_mountctl = tmpfs_mountctl,
2222 .vop_nremove = tmpfs_nremove,
2223 .vop_nlink = tmpfs_nlink,
2224 .vop_nrename = tmpfs_nrename,
2225 .vop_nmkdir = tmpfs_nmkdir,
2226 .vop_nrmdir = tmpfs_nrmdir,
2227 .vop_nsymlink = tmpfs_nsymlink,
2228 .vop_readdir = tmpfs_readdir,
2229 .vop_readlink = tmpfs_readlink,
2230 .vop_inactive = tmpfs_inactive,
2231 .vop_reclaim = tmpfs_reclaim,
2232 .vop_print = tmpfs_print,
2233 .vop_pathconf = tmpfs_pathconf,
2234 .vop_bmap = tmpfs_bmap,
2235 .vop_strategy = tmpfs_strategy,
2236 .vop_advlock = tmpfs_advlock,
2237 .vop_kqfilter = tmpfs_kqfilter