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.
36 #include <sys/cdefs.h>
38 #include <sys/kernel.h>
39 #include <sys/kern_syscall.h>
40 #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/unistd.h>
50 #include <sys/vfsops.h>
51 #include <sys/vnode.h>
53 #include <sys/mplock2.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
59 #include <vm/swap_pager.h>
61 #include <vfs/fifofs/fifo.h>
62 #include <vfs/tmpfs/tmpfs_vnops.h>
63 #include <vfs/tmpfs/tmpfs.h>
65 MALLOC_DECLARE(M_TMPFS);
69 tmpfs_knote(struct vnode *vp, int flags)
72 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
76 /* --------------------------------------------------------------------- */
79 tmpfs_nresolve(struct vop_nresolve_args *v)
81 struct vnode *dvp = v->a_dvp;
82 struct vnode *vp = NULL;
83 struct namecache *ncp = v->a_nch->ncp;
84 struct tmpfs_node *tnode;
87 struct tmpfs_dirent *de;
88 struct tmpfs_node *dnode;
90 dnode = VP_TO_TMPFS_DIR(dvp);
92 de = tmpfs_dir_lookup(dnode, NULL, ncp);
97 * Allocate a vnode for the node we found.
100 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
101 LK_EXCLUSIVE | LK_RETRY, &vp);
109 * Store the result of this lookup in the cache. Avoid this if the
110 * request was for creation, as it does not improve timings on
115 cache_setvp(v->a_nch, vp);
117 } else if (error == ENOENT) {
118 cache_setvp(v->a_nch, NULL);
124 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
126 struct vnode *dvp = v->a_dvp;
127 struct vnode **vpp = v->a_vpp;
128 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
129 struct ucred *cred = v->a_cred;
133 /* Check accessibility of requested node as a first step. */
134 error = VOP_ACCESS(dvp, VEXEC, cred);
138 if (dnode->tn_dir.tn_parent != NULL) {
139 /* Allocate a new vnode on the matching entry. */
140 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
141 LK_EXCLUSIVE | LK_RETRY, vpp);
147 return (*vpp == NULL) ? ENOENT : 0;
150 /* --------------------------------------------------------------------- */
153 tmpfs_ncreate(struct vop_ncreate_args *v)
155 struct vnode *dvp = v->a_dvp;
156 struct vnode **vpp = v->a_vpp;
157 struct namecache *ncp = v->a_nch->ncp;
158 struct vattr *vap = v->a_vap;
159 struct ucred *cred = v->a_cred;
162 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
164 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
166 cache_setunresolved(v->a_nch);
167 cache_setvp(v->a_nch, *vpp);
168 tmpfs_knote(dvp, NOTE_WRITE);
173 /* --------------------------------------------------------------------- */
176 tmpfs_nmknod(struct vop_nmknod_args *v)
178 struct vnode *dvp = v->a_dvp;
179 struct vnode **vpp = v->a_vpp;
180 struct namecache *ncp = v->a_nch->ncp;
181 struct vattr *vap = v->a_vap;
182 struct ucred *cred = v->a_cred;
185 if (vap->va_type != VBLK && vap->va_type != VCHR &&
186 vap->va_type != VFIFO)
189 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
191 cache_setunresolved(v->a_nch);
192 cache_setvp(v->a_nch, *vpp);
193 tmpfs_knote(dvp, NOTE_WRITE);
199 /* --------------------------------------------------------------------- */
202 tmpfs_open(struct vop_open_args *v)
204 struct vnode *vp = v->a_vp;
205 int mode = v->a_mode;
208 struct tmpfs_node *node;
210 node = VP_TO_TMPFS_NODE(vp);
212 /* The file is still active but all its names have been removed
213 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
214 * it is about to die. */
215 if (node->tn_links < 1)
218 /* If the file is marked append-only, deny write requests. */
219 if ((node->tn_flags & APPEND) &&
220 (mode & (FWRITE | O_APPEND)) == FWRITE) {
223 return (vop_stdopen(v));
228 /* --------------------------------------------------------------------- */
231 tmpfs_close(struct vop_close_args *v)
233 struct vnode *vp = v->a_vp;
234 struct tmpfs_node *node;
236 node = VP_TO_TMPFS_NODE(vp);
238 if (node->tn_links > 0) {
239 /* Update node times. No need to do it if the node has
240 * been deleted, because it will vanish after we return. */
244 return vop_stdclose(v);
247 /* --------------------------------------------------------------------- */
250 tmpfs_access(struct vop_access_args *v)
252 struct vnode *vp = v->a_vp;
254 struct tmpfs_node *node;
256 node = VP_TO_TMPFS_NODE(vp);
258 switch (vp->v_type) {
264 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
284 if (VWRITE && node->tn_flags & IMMUTABLE) {
289 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
296 /* --------------------------------------------------------------------- */
299 tmpfs_getattr(struct vop_getattr_args *v)
301 struct vnode *vp = v->a_vp;
302 struct vattr *vap = v->a_vap;
303 struct tmpfs_node *node;
305 node = VP_TO_TMPFS_NODE(vp);
309 vap->va_type = vp->v_type;
310 vap->va_mode = node->tn_mode;
311 vap->va_nlink = node->tn_links;
312 vap->va_uid = node->tn_uid;
313 vap->va_gid = node->tn_gid;
314 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
315 vap->va_fileid = node->tn_id;
316 vap->va_size = node->tn_size;
317 vap->va_blocksize = PAGE_SIZE;
318 vap->va_atime.tv_sec = node->tn_atime;
319 vap->va_atime.tv_nsec = node->tn_atimensec;
320 vap->va_mtime.tv_sec = node->tn_mtime;
321 vap->va_mtime.tv_nsec = node->tn_mtimensec;
322 vap->va_ctime.tv_sec = node->tn_ctime;
323 vap->va_ctime.tv_nsec = node->tn_ctimensec;
324 vap->va_gen = node->tn_gen;
325 vap->va_flags = node->tn_flags;
326 if (vp->v_type == VBLK || vp->v_type == VCHR)
328 vap->va_rmajor = umajor(node->tn_rdev);
329 vap->va_rminor = uminor(node->tn_rdev);
331 vap->va_bytes = round_page(node->tn_size);
337 /* --------------------------------------------------------------------- */
340 tmpfs_setattr(struct vop_setattr_args *v)
342 struct vnode *vp = v->a_vp;
343 struct vattr *vap = v->a_vap;
344 struct ucred *cred = v->a_cred;
345 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
349 if (error == 0 && (vap->va_flags != VNOVAL)) {
350 error = tmpfs_chflags(vp, vap->va_flags, cred);
351 kflags |= NOTE_ATTRIB;
354 if (error == 0 && (vap->va_size != VNOVAL)) {
355 if (vap->va_size > node->tn_size)
356 kflags |= NOTE_WRITE | NOTE_EXTEND;
358 kflags |= NOTE_WRITE;
359 error = tmpfs_chsize(vp, vap->va_size, cred);
362 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
363 vap->va_gid != (gid_t)VNOVAL)) {
364 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
365 kflags |= NOTE_ATTRIB;
368 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
369 error = tmpfs_chmod(vp, vap->va_mode, cred);
370 kflags |= NOTE_ATTRIB;
373 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
374 vap->va_atime.tv_nsec != VNOVAL) ||
375 (vap->va_mtime.tv_sec != VNOVAL &&
376 vap->va_mtime.tv_nsec != VNOVAL) )) {
377 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
378 vap->va_vaflags, cred);
379 kflags |= NOTE_ATTRIB;
382 /* Update the node times. We give preference to the error codes
383 * generated by this function rather than the ones that may arise
384 * from tmpfs_update. */
386 tmpfs_knote(vp, kflags);
391 /* --------------------------------------------------------------------- */
394 * fsync is usually a NOP, but we must take action when unmounting or
398 tmpfs_fsync(struct vop_fsync_args *v)
400 struct tmpfs_mount *tmp;
401 struct tmpfs_node *node;
402 struct vnode *vp = v->a_vp;
404 tmp = VFS_TO_TMPFS(vp->v_mount);
405 node = VP_TO_TMPFS_NODE(vp);
408 if (vp->v_type == VREG) {
409 if (vp->v_flag & VRECLAIMED) {
410 if (node->tn_links == 0)
411 tmpfs_truncate(vp, 0);
413 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
419 /* --------------------------------------------------------------------- */
422 tmpfs_read (struct vop_read_args *ap)
425 struct vnode *vp = ap->a_vp;
426 struct uio *uio = ap->a_uio;
427 struct tmpfs_node *node;
435 if (uio->uio_resid == 0) {
439 node = VP_TO_TMPFS_NODE(vp);
441 if (uio->uio_offset < 0)
443 if (vp->v_type != VREG)
447 if(curthread->td_mpcount)
455 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
457 * Use buffer cache I/O (via tmpfs_strategy)
459 offset = (size_t)uio->uio_offset & BMASK;
460 base_offset = (off_t)uio->uio_offset - offset;
461 bp = getcacheblk(vp, base_offset);
464 if (got_mplock == 0) {
469 error = bread(vp, base_offset, BSIZE, &bp);
472 kprintf("tmpfs_read bread error %d\n", error);
477 if (got_mplock == 0) {
483 * Figure out how many bytes we can actually copy this loop.
485 len = BSIZE - offset;
486 if (len > uio->uio_resid)
487 len = uio->uio_resid;
488 if (len > node->tn_size - uio->uio_offset)
489 len = (size_t)(node->tn_size - uio->uio_offset);
491 error = uiomove((char *)bp->b_data + offset, len, uio);
494 kprintf("tmpfs_read uiomove error %d\n", error);
502 TMPFS_NODE_LOCK(node);
503 node->tn_status |= TMPFS_NODE_ACCESSED;
504 TMPFS_NODE_UNLOCK(node);
510 tmpfs_write (struct vop_write_args *ap)
513 struct vnode *vp = ap->a_vp;
514 struct uio *uio = ap->a_uio;
515 struct thread *td = uio->uio_td;
516 struct tmpfs_node *node;
529 if (uio->uio_resid == 0) {
533 node = VP_TO_TMPFS_NODE(vp);
535 if (vp->v_type != VREG)
538 oldsize = node->tn_size;
539 if (ap->a_ioflag & IO_APPEND)
540 uio->uio_offset = node->tn_size;
543 * Check for illegal write offsets.
545 if (uio->uio_offset + uio->uio_resid >
546 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
549 if (vp->v_type == VREG && td != NULL) {
550 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
553 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
554 ksignal(td->td_proc, SIGXFSZ);
561 * Extend the file's size if necessary
563 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
566 if (curthread->td_mpcount) {
575 while (uio->uio_resid > 0) {
577 * Use buffer cache I/O (via tmpfs_strategy)
579 offset = (size_t)uio->uio_offset & BMASK;
580 base_offset = (off_t)uio->uio_offset - offset;
581 len = BSIZE - offset;
582 if (len > uio->uio_resid)
583 len = uio->uio_resid;
585 if ((uio->uio_offset + len) > node->tn_size) {
586 trivial = (uio->uio_offset <= node->tn_size);
587 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
593 * Read to fill in any gaps. Theoretically we could
594 * optimize this if the write covers the entire buffer
595 * and is not a UIO_NOCOPY write, however this can lead
596 * to a security violation exposing random kernel memory
597 * (whatever junk was in the backing VM pages before).
599 * So just use bread() to do the right thing.
601 error = bread(vp, base_offset, BSIZE, &bp);
602 error = uiomove((char *)bp->b_data + offset, len, uio);
604 kprintf("tmpfs_write uiomove error %d\n", error);
609 if (uio->uio_offset > node->tn_size) {
610 node->tn_size = uio->uio_offset;
611 kflags |= NOTE_EXTEND;
613 kflags |= NOTE_WRITE;
616 * The data has been loaded into the buffer, write it out.
618 * We want tmpfs to be able to use all available ram, not
619 * just the buffer cache, so if not explicitly paging we
620 * use buwrite() to leave the buffer clean but mark all the
621 * VM pages valid+dirty.
623 * When the kernel is paging, either via normal pageout
624 * operation or when cleaning the object during a recycle,
625 * the underlying VM pages are going to get thrown away
626 * so we MUST write them to swap.
628 * XXX unfortunately this catches msync() system calls too
631 if (vm_swap_size == 0) {
633 * if swap isn't configured yet, force a buwrite() to
634 * avoid problems further down the line, due to flushing
639 if (ap->a_ioflag & IO_SYNC) {
641 } else if ((ap->a_ioflag & IO_ASYNC) ||
642 (uio->uio_segflg == UIO_NOCOPY)) {
650 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
660 (void)tmpfs_reg_resize(vp, oldsize, trivial);
661 kflags &= ~NOTE_EXTEND;
666 TMPFS_NODE_LOCK(node);
667 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
668 (extended? TMPFS_NODE_CHANGED : 0);
670 if (node->tn_mode & (S_ISUID | S_ISGID)) {
671 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
672 node->tn_mode &= ~(S_ISUID | S_ISGID);
674 TMPFS_NODE_UNLOCK(node);
677 tmpfs_knote(vp, kflags);
682 tmpfs_advlock (struct vop_advlock_args *ap)
684 struct tmpfs_node *node;
685 struct vnode *vp = ap->a_vp;
687 node = VP_TO_TMPFS_NODE(vp);
689 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
693 tmpfs_strategy(struct vop_strategy_args *ap)
695 struct bio *bio = ap->a_bio;
696 struct buf *bp = bio->bio_buf;
697 struct vnode *vp = ap->a_vp;
698 struct tmpfs_node *node;
701 if (vp->v_type != VREG) {
702 bp->b_resid = bp->b_bcount;
703 bp->b_flags |= B_ERROR | B_INVAL;
704 bp->b_error = EINVAL;
709 node = VP_TO_TMPFS_NODE(vp);
711 uobj = node->tn_reg.tn_aobj;
714 * Call swap_pager_strategy to read or write between the VM
715 * object and the buffer cache.
717 swap_pager_strategy(uobj, bio);
723 tmpfs_bmap(struct vop_bmap_args *ap)
725 if (ap->a_doffsetp != NULL)
726 *ap->a_doffsetp = ap->a_loffset;
727 if (ap->a_runp != NULL)
729 if (ap->a_runb != NULL)
735 /* --------------------------------------------------------------------- */
738 tmpfs_nremove(struct vop_nremove_args *v)
740 struct vnode *dvp = v->a_dvp;
741 struct namecache *ncp = v->a_nch->ncp;
744 struct tmpfs_dirent *de;
745 struct tmpfs_mount *tmp;
746 struct tmpfs_node *dnode;
747 struct tmpfs_node *node;
750 * We have to acquire the vp from v->a_nch because
751 * we will likely unresolve the namecache entry, and
752 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
753 * sequence to recover space from the file.
755 error = cache_vref(v->a_nch, v->a_cred, &vp);
756 KKASSERT(error == 0);
758 if (vp->v_type == VDIR) {
763 dnode = VP_TO_TMPFS_DIR(dvp);
764 node = VP_TO_TMPFS_NODE(vp);
765 tmp = VFS_TO_TMPFS(vp->v_mount);
766 de = tmpfs_dir_lookup(dnode, node, ncp);
772 /* Files marked as immutable or append-only cannot be deleted. */
773 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
774 (dnode->tn_flags & APPEND)) {
779 /* Remove the entry from the directory; as it is a file, we do not
780 * have to change the number of hard links of the directory. */
781 tmpfs_dir_detach(dnode, de);
783 /* Free the directory entry we just deleted. Note that the node
784 * referred by it will not be removed until the vnode is really
786 tmpfs_free_dirent(tmp, de);
788 if (node->tn_links > 0) {
789 TMPFS_NODE_LOCK(node);
790 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
792 TMPFS_NODE_UNLOCK(node);
795 cache_setunresolved(v->a_nch);
796 cache_setvp(v->a_nch, NULL);
797 tmpfs_knote(vp, NOTE_DELETE);
798 /*cache_inval_vp(vp, CINV_DESTROY);*/
799 tmpfs_knote(dvp, NOTE_WRITE);
808 /* --------------------------------------------------------------------- */
811 tmpfs_nlink(struct vop_nlink_args *v)
813 struct vnode *dvp = v->a_dvp;
814 struct vnode *vp = v->a_vp;
815 struct namecache *ncp = v->a_nch->ncp;
816 struct tmpfs_dirent *de;
817 struct tmpfs_node *node;
818 struct tmpfs_node *dnode;
821 KKASSERT(dvp != vp); /* XXX When can this be false? */
823 node = VP_TO_TMPFS_NODE(vp);
824 dnode = VP_TO_TMPFS_NODE(dvp);
826 /* XXX: Why aren't the following two tests done by the caller? */
828 /* Hard links of directories are forbidden. */
829 if (vp->v_type == VDIR) {
834 /* Cannot create cross-device links. */
835 if (dvp->v_mount != vp->v_mount) {
840 /* Ensure that we do not overflow the maximum number of links imposed
842 KKASSERT(node->tn_links <= LINK_MAX);
843 if (node->tn_links == LINK_MAX) {
848 /* We cannot create links of files marked immutable or append-only. */
849 if (node->tn_flags & (IMMUTABLE | APPEND)) {
854 /* Allocate a new directory entry to represent the node. */
855 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
856 ncp->nc_name, ncp->nc_nlen, &de);
860 /* Insert the new directory entry into the appropriate directory. */
861 tmpfs_dir_attach(dnode, de);
863 /* vp link count has changed, so update node times. */
865 TMPFS_NODE_LOCK(node);
866 node->tn_status |= TMPFS_NODE_CHANGED;
867 TMPFS_NODE_UNLOCK(node);
870 tmpfs_knote(vp, NOTE_LINK);
871 cache_setunresolved(v->a_nch);
872 cache_setvp(v->a_nch, vp);
873 tmpfs_knote(dvp, NOTE_WRITE);
880 /* --------------------------------------------------------------------- */
883 tmpfs_nrename(struct vop_nrename_args *v)
885 struct vnode *fdvp = v->a_fdvp;
886 struct namecache *fncp = v->a_fnch->ncp;
887 struct vnode *fvp = fncp->nc_vp;
888 struct vnode *tdvp = v->a_tdvp;
889 struct namecache *tncp = v->a_tnch->ncp;
890 struct vnode *tvp = tncp->nc_vp;
891 struct tmpfs_dirent *de;
892 struct tmpfs_mount *tmp;
893 struct tmpfs_node *fdnode;
894 struct tmpfs_node *fnode;
895 struct tmpfs_node *tnode;
896 struct tmpfs_node *tdnode;
901 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
903 /* Disallow cross-device renames.
904 * XXX Why isn't this done by the caller? */
905 if (fvp->v_mount != tdvp->v_mount ||
906 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
911 tmp = VFS_TO_TMPFS(tdvp->v_mount);
912 tdnode = VP_TO_TMPFS_DIR(tdvp);
914 /* If source and target are the same file, there is nothing to do. */
920 fdnode = VP_TO_TMPFS_DIR(fdvp);
921 fnode = VP_TO_TMPFS_NODE(fvp);
922 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
924 /* Avoid manipulating '.' and '..' entries. */
929 KKASSERT(de->td_node == fnode);
932 * If replacing an entry in the target directory and that entry
933 * is a directory, it must be empty.
935 * Kern_rename gurantees the destination to be a directory
936 * if the source is one (it does?).
939 KKASSERT(tnode != NULL);
941 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
942 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
947 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
948 if (tnode->tn_size > 0) {
952 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
955 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
959 KKASSERT(fnode->tn_type != VDIR &&
960 tnode->tn_type != VDIR);
964 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
965 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
971 * Ensure that we have enough memory to hold the new name, if it
974 if (fncp->nc_nlen != tncp->nc_nlen ||
975 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
976 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
977 M_WAITOK | M_NULLOK);
978 if (newname == NULL) {
982 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
983 newname[tncp->nc_nlen] = '\0';
989 * Unlink entry from source directory. Note that the kernel has
990 * already checked for illegal recursion cases (renaming a directory
991 * into a subdirectory of itself).
993 if (fdnode != tdnode)
994 tmpfs_dir_detach(fdnode, de);
997 * Handle any name change. Swap with newname, we will
998 * deallocate it at the end.
1000 if (newname != NULL) {
1002 TMPFS_NODE_LOCK(fnode);
1003 fnode->tn_status |= TMPFS_NODE_CHANGED;
1004 TMPFS_NODE_UNLOCK(fnode);
1006 oldname = de->td_name;
1007 de->td_name = newname;
1008 de->td_namelen = (uint16_t)tncp->nc_nlen;
1013 * Link entry to target directory. If the entry
1014 * represents a directory move the parent linkage
1017 if (fdnode != tdnode) {
1018 if (de->td_node->tn_type == VDIR) {
1019 TMPFS_VALIDATE_DIR(fnode);
1021 TMPFS_NODE_LOCK(tdnode);
1023 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1024 TMPFS_NODE_UNLOCK(tdnode);
1026 TMPFS_NODE_LOCK(fnode);
1027 fnode->tn_dir.tn_parent = tdnode;
1028 fnode->tn_status |= TMPFS_NODE_CHANGED;
1029 TMPFS_NODE_UNLOCK(fnode);
1031 TMPFS_NODE_LOCK(fdnode);
1033 fdnode->tn_status |= TMPFS_NODE_MODIFIED;
1034 TMPFS_NODE_UNLOCK(fdnode);
1036 tmpfs_dir_attach(tdnode, de);
1038 TMPFS_NODE_LOCK(tdnode);
1039 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1040 TMPFS_NODE_UNLOCK(tdnode);
1044 * If we are overwriting an entry, we have to remove the old one
1045 * from the target directory.
1048 /* Remove the old entry from the target directory. */
1049 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1050 tmpfs_dir_detach(tdnode, de);
1051 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1054 * Free the directory entry we just deleted. Note that the
1055 * node referred by it will not be removed until the vnode is
1058 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1059 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1066 kfree(newname, tmp->tm_name_zone);
1069 cache_rename(v->a_fnch, v->a_tnch);
1070 tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1071 tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1072 if (fnode->tn_vnode)
1073 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1080 /* Release target nodes. */
1081 /* XXX: I don't understand when tdvp can be the same as tvp, but
1082 * other code takes care of this... */
1089 /* --------------------------------------------------------------------- */
1092 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1094 struct vnode *dvp = v->a_dvp;
1095 struct vnode **vpp = v->a_vpp;
1096 struct namecache *ncp = v->a_nch->ncp;
1097 struct vattr *vap = v->a_vap;
1098 struct ucred *cred = v->a_cred;
1101 KKASSERT(vap->va_type == VDIR);
1103 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1105 cache_setunresolved(v->a_nch);
1106 cache_setvp(v->a_nch, *vpp);
1107 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1113 /* --------------------------------------------------------------------- */
1116 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1118 struct vnode *dvp = v->a_dvp;
1119 struct namecache *ncp = v->a_nch->ncp;
1121 struct tmpfs_dirent *de;
1122 struct tmpfs_mount *tmp;
1123 struct tmpfs_node *dnode;
1124 struct tmpfs_node *node;
1128 * We have to acquire the vp from v->a_nch because
1129 * we will likely unresolve the namecache entry, and
1130 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1133 error = cache_vref(v->a_nch, v->a_cred, &vp);
1134 KKASSERT(error == 0);
1137 * Prevalidate so we don't hit an assertion later
1139 if (vp->v_type != VDIR) {
1144 tmp = VFS_TO_TMPFS(dvp->v_mount);
1145 dnode = VP_TO_TMPFS_DIR(dvp);
1146 node = VP_TO_TMPFS_DIR(vp);
1148 /* Directories with more than two entries ('.' and '..') cannot be
1150 if (node->tn_size > 0) {
1155 if ((dnode->tn_flags & APPEND)
1156 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1161 /* This invariant holds only if we are not trying to remove "..".
1162 * We checked for that above so this is safe now. */
1163 KKASSERT(node->tn_dir.tn_parent == dnode);
1165 /* Get the directory entry associated with node (vp). This was
1166 * filled by tmpfs_lookup while looking up the entry. */
1167 de = tmpfs_dir_lookup(dnode, node, ncp);
1168 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1172 /* Check flags to see if we are allowed to remove the directory. */
1173 if ((dnode->tn_flags & APPEND) ||
1174 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1180 /* Detach the directory entry from the directory (dnode). */
1181 tmpfs_dir_detach(dnode, de);
1183 /* No vnode should be allocated for this entry from this point */
1184 TMPFS_NODE_LOCK(node);
1185 TMPFS_ASSERT_ELOCKED(node);
1186 TMPFS_NODE_LOCK(dnode);
1187 TMPFS_ASSERT_ELOCKED(dnode);
1190 /* handled by tmpfs_free_node */
1191 KKASSERT(node->tn_links > 0);
1193 node->tn_dir.tn_parent = NULL;
1195 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1196 TMPFS_NODE_MODIFIED;
1199 /* handled by tmpfs_free_node */
1200 KKASSERT(dnode->tn_links > 0);
1203 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1204 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1206 TMPFS_NODE_UNLOCK(dnode);
1207 TMPFS_NODE_UNLOCK(node);
1209 /* Free the directory entry we just deleted. Note that the node
1210 * referred by it will not be removed until the vnode is really
1212 tmpfs_free_dirent(tmp, de);
1214 /* Release the deleted vnode (will destroy the node, notify
1215 * interested parties and clean it from the cache). */
1217 TMPFS_NODE_LOCK(dnode);
1218 dnode->tn_status |= TMPFS_NODE_CHANGED;
1219 TMPFS_NODE_UNLOCK(dnode);
1222 cache_setunresolved(v->a_nch);
1223 cache_setvp(v->a_nch, NULL);
1224 /*cache_inval_vp(vp, CINV_DESTROY);*/
1225 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1234 /* --------------------------------------------------------------------- */
1237 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1239 struct vnode *dvp = v->a_dvp;
1240 struct vnode **vpp = v->a_vpp;
1241 struct namecache *ncp = v->a_nch->ncp;
1242 struct vattr *vap = v->a_vap;
1243 struct ucred *cred = v->a_cred;
1244 char *target = v->a_target;
1247 vap->va_type = VLNK;
1248 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1250 tmpfs_knote(*vpp, NOTE_WRITE);
1251 cache_setunresolved(v->a_nch);
1252 cache_setvp(v->a_nch, *vpp);
1258 /* --------------------------------------------------------------------- */
1261 tmpfs_readdir(struct vop_readdir_args *v)
1263 struct vnode *vp = v->a_vp;
1264 struct uio *uio = v->a_uio;
1265 int *eofflag = v->a_eofflag;
1266 off_t **cookies = v->a_cookies;
1267 int *ncookies = v->a_ncookies;
1268 struct tmpfs_mount *tmp;
1272 struct tmpfs_node *node;
1274 /* This operation only makes sense on directory nodes. */
1275 if (vp->v_type != VDIR)
1278 tmp = VFS_TO_TMPFS(vp->v_mount);
1279 node = VP_TO_TMPFS_DIR(vp);
1280 startoff = uio->uio_offset;
1282 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1283 error = tmpfs_dir_getdotdent(node, uio);
1289 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1290 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1296 error = tmpfs_dir_getdents(node, uio, &cnt);
1299 KKASSERT(error >= -1);
1304 if (eofflag != NULL)
1306 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1308 /* Update NFS-related variables. */
1309 if (error == 0 && cookies != NULL && ncookies != NULL) {
1311 off_t off = startoff;
1312 struct tmpfs_dirent *de = NULL;
1315 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1317 for (i = 0; i < cnt; i++) {
1318 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1319 if (off == TMPFS_DIRCOOKIE_DOT) {
1320 off = TMPFS_DIRCOOKIE_DOTDOT;
1322 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1323 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1324 } else if (de != NULL) {
1325 de = TAILQ_NEXT(de, td_entries);
1327 de = tmpfs_dir_lookupbycookie(node,
1329 KKASSERT(de != NULL);
1330 de = TAILQ_NEXT(de, td_entries);
1333 off = TMPFS_DIRCOOKIE_EOF;
1335 off = tmpfs_dircookie(de);
1338 (*cookies)[i] = off;
1340 KKASSERT(uio->uio_offset == off);
1346 /* --------------------------------------------------------------------- */
1349 tmpfs_readlink(struct vop_readlink_args *v)
1351 struct vnode *vp = v->a_vp;
1352 struct uio *uio = v->a_uio;
1355 struct tmpfs_node *node;
1357 KKASSERT(uio->uio_offset == 0);
1358 KKASSERT(vp->v_type == VLNK);
1360 node = VP_TO_TMPFS_NODE(vp);
1362 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1364 TMPFS_NODE_LOCK(node);
1365 node->tn_status |= TMPFS_NODE_ACCESSED;
1366 TMPFS_NODE_UNLOCK(node);
1371 /* --------------------------------------------------------------------- */
1374 tmpfs_inactive(struct vop_inactive_args *v)
1376 struct vnode *vp = v->a_vp;
1378 struct tmpfs_node *node;
1380 node = VP_TO_TMPFS_NODE(vp);
1383 * Get rid of unreferenced deleted vnodes sooner rather than
1384 * later so the data memory can be recovered immediately.
1386 * We must truncate the vnode to prevent the normal reclamation
1387 * path from flushing the data for the removed file to disk.
1389 TMPFS_NODE_LOCK(node);
1390 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1391 (node->tn_links == 0 ||
1392 (node->tn_links == 1 && node->tn_type == VDIR &&
1393 node->tn_dir.tn_parent)))
1395 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1396 TMPFS_NODE_UNLOCK(node);
1397 if (node->tn_type == VREG)
1398 tmpfs_truncate(vp, 0);
1401 TMPFS_NODE_UNLOCK(node);
1407 /* --------------------------------------------------------------------- */
1410 tmpfs_reclaim(struct vop_reclaim_args *v)
1412 struct vnode *vp = v->a_vp;
1413 struct tmpfs_mount *tmp;
1414 struct tmpfs_node *node;
1416 node = VP_TO_TMPFS_NODE(vp);
1417 tmp = VFS_TO_TMPFS(vp->v_mount);
1422 * If the node referenced by this vnode was deleted by the
1423 * user, we must free its associated data structures now that
1424 * the vnode is being reclaimed.
1426 * Directories have an extra link ref.
1428 TMPFS_NODE_LOCK(node);
1429 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1430 (node->tn_links == 0 ||
1431 (node->tn_links == 1 && node->tn_type == VDIR &&
1432 node->tn_dir.tn_parent)))
1434 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1435 tmpfs_free_node(tmp, node);
1438 TMPFS_NODE_UNLOCK(node);
1441 KKASSERT(vp->v_data == NULL);
1445 /* --------------------------------------------------------------------- */
1448 tmpfs_print(struct vop_print_args *v)
1450 struct vnode *vp = v->a_vp;
1452 struct tmpfs_node *node;
1454 node = VP_TO_TMPFS_NODE(vp);
1456 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1457 node, node->tn_flags, node->tn_links);
1458 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1459 node->tn_mode, node->tn_uid, node->tn_gid,
1460 (uintmax_t)node->tn_size, node->tn_status);
1462 if (vp->v_type == VFIFO)
1470 /* --------------------------------------------------------------------- */
1473 tmpfs_pathconf(struct vop_pathconf_args *v)
1475 int name = v->a_name;
1476 register_t *retval = v->a_retval;
1499 case _PC_CHOWN_RESTRICTED:
1511 case _PC_FILESIZEBITS:
1512 *retval = 0; /* XXX Don't know which value should I return. */
1522 /************************************************************************
1524 ************************************************************************/
1526 static void filt_tmpfsdetach(struct knote *kn);
1527 static int filt_tmpfsread(struct knote *kn, long hint);
1528 static int filt_tmpfswrite(struct knote *kn, long hint);
1529 static int filt_tmpfsvnode(struct knote *kn, long hint);
1531 static struct filterops tmpfsread_filtops =
1532 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsread };
1533 static struct filterops tmpfswrite_filtops =
1534 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfswrite };
1535 static struct filterops tmpfsvnode_filtops =
1536 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1539 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1541 struct vnode *vp = ap->a_vp;
1542 struct knote *kn = ap->a_kn;
1544 switch (kn->kn_filter) {
1546 kn->kn_fop = &tmpfsread_filtops;
1549 kn->kn_fop = &tmpfswrite_filtops;
1552 kn->kn_fop = &tmpfsvnode_filtops;
1555 return (EOPNOTSUPP);
1558 kn->kn_hook = (caddr_t)vp;
1560 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1566 filt_tmpfsdetach(struct knote *kn)
1568 struct vnode *vp = (void *)kn->kn_hook;
1570 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1574 filt_tmpfsread(struct knote *kn, long hint)
1576 struct vnode *vp = (void *)kn->kn_hook;
1577 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1580 if (hint == NOTE_REVOKE) {
1581 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1584 off = node->tn_size - kn->kn_fp->f_offset;
1585 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1586 if (kn->kn_sfflags & NOTE_OLDAPI)
1590 * Handle possible MP race interlock on filter check/write
1592 if (kn->kn_data == 0) {
1594 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1597 return (kn->kn_data != 0);
1601 filt_tmpfswrite(struct knote *kn, long hint)
1603 if (hint == NOTE_REVOKE)
1604 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1610 filt_tmpfsvnode(struct knote *kn, long hint)
1612 if (kn->kn_sfflags & hint)
1613 kn->kn_fflags |= hint;
1614 if (hint == NOTE_REVOKE) {
1615 kn->kn_flags |= EV_EOF;
1618 return (kn->kn_fflags != 0);
1622 /* --------------------------------------------------------------------- */
1625 * vnode operations vector used for files stored in a tmpfs file system.
1627 struct vop_ops tmpfs_vnode_vops = {
1628 .vop_default = vop_defaultop,
1629 .vop_getpages = vop_stdgetpages,
1630 .vop_putpages = vop_stdputpages,
1631 .vop_ncreate = tmpfs_ncreate,
1632 .vop_nresolve = tmpfs_nresolve,
1633 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1634 .vop_nmknod = tmpfs_nmknod,
1635 .vop_open = tmpfs_open,
1636 .vop_close = tmpfs_close,
1637 .vop_access = tmpfs_access,
1638 .vop_getattr = tmpfs_getattr,
1639 .vop_setattr = tmpfs_setattr,
1640 .vop_read = tmpfs_read,
1641 .vop_write = tmpfs_write,
1642 .vop_fsync = tmpfs_fsync,
1643 .vop_nremove = tmpfs_nremove,
1644 .vop_nlink = tmpfs_nlink,
1645 .vop_nrename = tmpfs_nrename,
1646 .vop_nmkdir = tmpfs_nmkdir,
1647 .vop_nrmdir = tmpfs_nrmdir,
1648 .vop_nsymlink = tmpfs_nsymlink,
1649 .vop_readdir = tmpfs_readdir,
1650 .vop_readlink = tmpfs_readlink,
1651 .vop_inactive = tmpfs_inactive,
1652 .vop_reclaim = tmpfs_reclaim,
1653 .vop_print = tmpfs_print,
1654 .vop_pathconf = tmpfs_pathconf,
1655 .vop_bmap = tmpfs_bmap,
1656 .vop_strategy = tmpfs_strategy,
1657 .vop_advlock = tmpfs_advlock,
1658 .vop_kqfilter = tmpfs_kqfilter