2 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $
34 * tmpfs vnode interface.
37 #include <sys/kernel.h>
38 #include <sys/kern_syscall.h>
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
44 #include <sys/resourcevar.h>
45 #include <sys/sched.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vfsops.h>
50 #include <sys/vnode.h>
52 #include <sys/mplock2.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
58 #include <vm/swap_pager.h>
60 #include <vfs/fifofs/fifo.h>
61 #include <vfs/tmpfs/tmpfs_vnops.h>
62 #include <vfs/tmpfs/tmpfs.h>
64 MALLOC_DECLARE(M_TMPFS);
68 tmpfs_knote(struct vnode *vp, int flags)
71 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
75 /* --------------------------------------------------------------------- */
78 tmpfs_nresolve(struct vop_nresolve_args *v)
80 struct vnode *dvp = v->a_dvp;
81 struct vnode *vp = NULL;
82 struct namecache *ncp = v->a_nch->ncp;
83 struct tmpfs_node *tnode;
86 struct tmpfs_dirent *de;
87 struct tmpfs_node *dnode;
89 dnode = VP_TO_TMPFS_DIR(dvp);
91 de = tmpfs_dir_lookup(dnode, NULL, ncp);
96 * Allocate a vnode for the node we found.
99 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
100 LK_EXCLUSIVE | LK_RETRY, &vp);
108 * Store the result of this lookup in the cache. Avoid this if the
109 * request was for creation, as it does not improve timings on
114 cache_setvp(v->a_nch, vp);
116 } else if (error == ENOENT) {
117 cache_setvp(v->a_nch, NULL);
123 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
125 struct vnode *dvp = v->a_dvp;
126 struct vnode **vpp = v->a_vpp;
127 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
128 struct ucred *cred = v->a_cred;
132 /* Check accessibility of requested node as a first step. */
133 error = VOP_ACCESS(dvp, VEXEC, cred);
137 if (dnode->tn_dir.tn_parent != NULL) {
138 /* Allocate a new vnode on the matching entry. */
139 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
140 LK_EXCLUSIVE | LK_RETRY, vpp);
146 return (*vpp == NULL) ? ENOENT : 0;
149 /* --------------------------------------------------------------------- */
152 tmpfs_ncreate(struct vop_ncreate_args *v)
154 struct vnode *dvp = v->a_dvp;
155 struct vnode **vpp = v->a_vpp;
156 struct namecache *ncp = v->a_nch->ncp;
157 struct vattr *vap = v->a_vap;
158 struct ucred *cred = v->a_cred;
161 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
163 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
165 cache_setunresolved(v->a_nch);
166 cache_setvp(v->a_nch, *vpp);
167 tmpfs_knote(dvp, NOTE_WRITE);
172 /* --------------------------------------------------------------------- */
175 tmpfs_nmknod(struct vop_nmknod_args *v)
177 struct vnode *dvp = v->a_dvp;
178 struct vnode **vpp = v->a_vpp;
179 struct namecache *ncp = v->a_nch->ncp;
180 struct vattr *vap = v->a_vap;
181 struct ucred *cred = v->a_cred;
184 if (vap->va_type != VBLK && vap->va_type != VCHR &&
185 vap->va_type != VFIFO)
188 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
190 cache_setunresolved(v->a_nch);
191 cache_setvp(v->a_nch, *vpp);
192 tmpfs_knote(dvp, NOTE_WRITE);
198 /* --------------------------------------------------------------------- */
201 tmpfs_open(struct vop_open_args *v)
203 struct vnode *vp = v->a_vp;
204 int mode = v->a_mode;
207 struct tmpfs_node *node;
209 node = VP_TO_TMPFS_NODE(vp);
211 /* The file is still active but all its names have been removed
212 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
213 * it is about to die. */
214 if (node->tn_links < 1)
217 /* If the file is marked append-only, deny write requests. */
218 if ((node->tn_flags & APPEND) &&
219 (mode & (FWRITE | O_APPEND)) == FWRITE) {
222 return (vop_stdopen(v));
227 /* --------------------------------------------------------------------- */
230 tmpfs_close(struct vop_close_args *v)
232 struct vnode *vp = v->a_vp;
233 struct tmpfs_node *node;
235 node = VP_TO_TMPFS_NODE(vp);
237 if (node->tn_links > 0) {
238 /* Update node times. No need to do it if the node has
239 * been deleted, because it will vanish after we return. */
243 return vop_stdclose(v);
246 /* --------------------------------------------------------------------- */
249 tmpfs_access(struct vop_access_args *v)
251 struct vnode *vp = v->a_vp;
253 struct tmpfs_node *node;
255 node = VP_TO_TMPFS_NODE(vp);
257 switch (vp->v_type) {
263 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
283 if (VWRITE && node->tn_flags & IMMUTABLE) {
288 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
295 /* --------------------------------------------------------------------- */
298 tmpfs_getattr(struct vop_getattr_args *v)
300 struct vnode *vp = v->a_vp;
301 struct vattr *vap = v->a_vap;
302 struct tmpfs_node *node;
304 node = VP_TO_TMPFS_NODE(vp);
308 vap->va_type = vp->v_type;
309 vap->va_mode = node->tn_mode;
310 vap->va_nlink = node->tn_links;
311 vap->va_uid = node->tn_uid;
312 vap->va_gid = node->tn_gid;
313 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
314 vap->va_fileid = node->tn_id;
315 vap->va_size = node->tn_size;
316 vap->va_blocksize = PAGE_SIZE;
317 vap->va_atime.tv_sec = node->tn_atime;
318 vap->va_atime.tv_nsec = node->tn_atimensec;
319 vap->va_mtime.tv_sec = node->tn_mtime;
320 vap->va_mtime.tv_nsec = node->tn_mtimensec;
321 vap->va_ctime.tv_sec = node->tn_ctime;
322 vap->va_ctime.tv_nsec = node->tn_ctimensec;
323 vap->va_gen = node->tn_gen;
324 vap->va_flags = node->tn_flags;
325 if (vp->v_type == VBLK || vp->v_type == VCHR)
327 vap->va_rmajor = umajor(node->tn_rdev);
328 vap->va_rminor = uminor(node->tn_rdev);
330 vap->va_bytes = round_page(node->tn_size);
336 /* --------------------------------------------------------------------- */
339 tmpfs_setattr(struct vop_setattr_args *v)
341 struct vnode *vp = v->a_vp;
342 struct vattr *vap = v->a_vap;
343 struct ucred *cred = v->a_cred;
344 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
348 if (error == 0 && (vap->va_flags != VNOVAL)) {
349 error = tmpfs_chflags(vp, vap->va_flags, cred);
350 kflags |= NOTE_ATTRIB;
353 if (error == 0 && (vap->va_size != VNOVAL)) {
354 if (vap->va_size > node->tn_size)
355 kflags |= NOTE_WRITE | NOTE_EXTEND;
357 kflags |= NOTE_WRITE;
358 error = tmpfs_chsize(vp, vap->va_size, cred);
361 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
362 vap->va_gid != (gid_t)VNOVAL)) {
363 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
364 kflags |= NOTE_ATTRIB;
367 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
368 error = tmpfs_chmod(vp, vap->va_mode, cred);
369 kflags |= NOTE_ATTRIB;
372 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
373 vap->va_atime.tv_nsec != VNOVAL) ||
374 (vap->va_mtime.tv_sec != VNOVAL &&
375 vap->va_mtime.tv_nsec != VNOVAL) )) {
376 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
377 vap->va_vaflags, cred);
378 kflags |= NOTE_ATTRIB;
381 /* Update the node times. We give preference to the error codes
382 * generated by this function rather than the ones that may arise
383 * from tmpfs_update. */
385 tmpfs_knote(vp, kflags);
390 /* --------------------------------------------------------------------- */
393 * fsync is usually a NOP, but we must take action when unmounting or
397 tmpfs_fsync(struct vop_fsync_args *v)
399 struct tmpfs_mount *tmp;
400 struct tmpfs_node *node;
401 struct vnode *vp = v->a_vp;
403 tmp = VFS_TO_TMPFS(vp->v_mount);
404 node = VP_TO_TMPFS_NODE(vp);
407 if (vp->v_type == VREG) {
408 if (vp->v_flag & VRECLAIMED) {
409 if (node->tn_links == 0)
410 tmpfs_truncate(vp, 0);
412 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
418 /* --------------------------------------------------------------------- */
421 tmpfs_read (struct vop_read_args *ap)
424 struct vnode *vp = ap->a_vp;
425 struct uio *uio = ap->a_uio;
426 struct tmpfs_node *node;
434 if (uio->uio_resid == 0) {
438 node = VP_TO_TMPFS_NODE(vp);
440 if (uio->uio_offset < 0)
442 if (vp->v_type != VREG)
446 if(curthread->td_mpcount)
454 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
456 * Use buffer cache I/O (via tmpfs_strategy)
458 offset = (size_t)uio->uio_offset & BMASK;
459 base_offset = (off_t)uio->uio_offset - offset;
460 bp = getcacheblk(vp, base_offset);
463 if (got_mplock == 0) {
468 error = bread(vp, base_offset, BSIZE, &bp);
471 kprintf("tmpfs_read bread error %d\n", error);
476 if (got_mplock == 0) {
482 * Figure out how many bytes we can actually copy this loop.
484 len = BSIZE - offset;
485 if (len > uio->uio_resid)
486 len = uio->uio_resid;
487 if (len > node->tn_size - uio->uio_offset)
488 len = (size_t)(node->tn_size - uio->uio_offset);
490 error = uiomove((char *)bp->b_data + offset, len, uio);
493 kprintf("tmpfs_read uiomove error %d\n", error);
501 TMPFS_NODE_LOCK(node);
502 node->tn_status |= TMPFS_NODE_ACCESSED;
503 TMPFS_NODE_UNLOCK(node);
509 tmpfs_write (struct vop_write_args *ap)
512 struct vnode *vp = ap->a_vp;
513 struct uio *uio = ap->a_uio;
514 struct thread *td = uio->uio_td;
515 struct tmpfs_node *node;
528 if (uio->uio_resid == 0) {
532 node = VP_TO_TMPFS_NODE(vp);
534 if (vp->v_type != VREG)
537 oldsize = node->tn_size;
538 if (ap->a_ioflag & IO_APPEND)
539 uio->uio_offset = node->tn_size;
542 * Check for illegal write offsets.
544 if (uio->uio_offset + uio->uio_resid >
545 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
548 if (vp->v_type == VREG && td != NULL) {
549 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
552 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
553 ksignal(td->td_proc, SIGXFSZ);
560 * Extend the file's size if necessary
562 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
565 if (curthread->td_mpcount) {
574 while (uio->uio_resid > 0) {
576 * Use buffer cache I/O (via tmpfs_strategy)
578 offset = (size_t)uio->uio_offset & BMASK;
579 base_offset = (off_t)uio->uio_offset - offset;
580 len = BSIZE - offset;
581 if (len > uio->uio_resid)
582 len = uio->uio_resid;
584 if ((uio->uio_offset + len) > node->tn_size) {
585 trivial = (uio->uio_offset <= node->tn_size);
586 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
592 * Read to fill in any gaps. Theoretically we could
593 * optimize this if the write covers the entire buffer
594 * and is not a UIO_NOCOPY write, however this can lead
595 * to a security violation exposing random kernel memory
596 * (whatever junk was in the backing VM pages before).
598 * So just use bread() to do the right thing.
600 error = bread(vp, base_offset, BSIZE, &bp);
601 error = uiomove((char *)bp->b_data + offset, len, uio);
603 kprintf("tmpfs_write uiomove error %d\n", error);
608 if (uio->uio_offset > node->tn_size) {
609 node->tn_size = uio->uio_offset;
610 kflags |= NOTE_EXTEND;
612 kflags |= NOTE_WRITE;
615 * The data has been loaded into the buffer, write it out.
617 * We want tmpfs to be able to use all available ram, not
618 * just the buffer cache, so if not explicitly paging we
619 * use buwrite() to leave the buffer clean but mark all the
620 * VM pages valid+dirty.
622 * When the kernel is paging, either via normal pageout
623 * operation or when cleaning the object during a recycle,
624 * the underlying VM pages are going to get thrown away
625 * so we MUST write them to swap.
627 * XXX unfortunately this catches msync() system calls too
630 if (vm_swap_size == 0) {
632 * if swap isn't configured yet, force a buwrite() to
633 * avoid problems further down the line, due to flushing
638 if (ap->a_ioflag & IO_SYNC) {
640 } else if ((ap->a_ioflag & IO_ASYNC) ||
641 (uio->uio_segflg == UIO_NOCOPY)) {
649 kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
659 (void)tmpfs_reg_resize(vp, oldsize, trivial);
660 kflags &= ~NOTE_EXTEND;
665 TMPFS_NODE_LOCK(node);
666 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
667 (extended? TMPFS_NODE_CHANGED : 0);
669 if (node->tn_mode & (S_ISUID | S_ISGID)) {
670 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
671 node->tn_mode &= ~(S_ISUID | S_ISGID);
673 TMPFS_NODE_UNLOCK(node);
676 tmpfs_knote(vp, kflags);
681 tmpfs_advlock (struct vop_advlock_args *ap)
683 struct tmpfs_node *node;
684 struct vnode *vp = ap->a_vp;
686 node = VP_TO_TMPFS_NODE(vp);
688 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
692 tmpfs_strategy(struct vop_strategy_args *ap)
694 struct bio *bio = ap->a_bio;
695 struct buf *bp = bio->bio_buf;
696 struct vnode *vp = ap->a_vp;
697 struct tmpfs_node *node;
700 if (vp->v_type != VREG) {
701 bp->b_resid = bp->b_bcount;
702 bp->b_flags |= B_ERROR | B_INVAL;
703 bp->b_error = EINVAL;
708 node = VP_TO_TMPFS_NODE(vp);
710 uobj = node->tn_reg.tn_aobj;
713 * Call swap_pager_strategy to read or write between the VM
714 * object and the buffer cache.
716 swap_pager_strategy(uobj, bio);
722 tmpfs_bmap(struct vop_bmap_args *ap)
724 if (ap->a_doffsetp != NULL)
725 *ap->a_doffsetp = ap->a_loffset;
726 if (ap->a_runp != NULL)
728 if (ap->a_runb != NULL)
734 /* --------------------------------------------------------------------- */
737 tmpfs_nremove(struct vop_nremove_args *v)
739 struct vnode *dvp = v->a_dvp;
740 struct namecache *ncp = v->a_nch->ncp;
743 struct tmpfs_dirent *de;
744 struct tmpfs_mount *tmp;
745 struct tmpfs_node *dnode;
746 struct tmpfs_node *node;
749 * We have to acquire the vp from v->a_nch because
750 * we will likely unresolve the namecache entry, and
751 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
752 * sequence to recover space from the file.
754 error = cache_vref(v->a_nch, v->a_cred, &vp);
755 KKASSERT(error == 0);
757 if (vp->v_type == VDIR) {
762 dnode = VP_TO_TMPFS_DIR(dvp);
763 node = VP_TO_TMPFS_NODE(vp);
764 tmp = VFS_TO_TMPFS(vp->v_mount);
765 de = tmpfs_dir_lookup(dnode, node, ncp);
771 /* Files marked as immutable or append-only cannot be deleted. */
772 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
773 (dnode->tn_flags & APPEND)) {
778 /* Remove the entry from the directory; as it is a file, we do not
779 * have to change the number of hard links of the directory. */
780 tmpfs_dir_detach(dnode, de);
782 /* Free the directory entry we just deleted. Note that the node
783 * referred by it will not be removed until the vnode is really
785 tmpfs_free_dirent(tmp, de);
787 if (node->tn_links > 0) {
788 TMPFS_NODE_LOCK(node);
789 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
791 TMPFS_NODE_UNLOCK(node);
794 cache_setunresolved(v->a_nch);
795 cache_setvp(v->a_nch, NULL);
796 tmpfs_knote(vp, NOTE_DELETE);
797 /*cache_inval_vp(vp, CINV_DESTROY);*/
798 tmpfs_knote(dvp, NOTE_WRITE);
807 /* --------------------------------------------------------------------- */
810 tmpfs_nlink(struct vop_nlink_args *v)
812 struct vnode *dvp = v->a_dvp;
813 struct vnode *vp = v->a_vp;
814 struct namecache *ncp = v->a_nch->ncp;
815 struct tmpfs_dirent *de;
816 struct tmpfs_node *node;
817 struct tmpfs_node *dnode;
820 KKASSERT(dvp != vp); /* XXX When can this be false? */
822 node = VP_TO_TMPFS_NODE(vp);
823 dnode = VP_TO_TMPFS_NODE(dvp);
825 /* XXX: Why aren't the following two tests done by the caller? */
827 /* Hard links of directories are forbidden. */
828 if (vp->v_type == VDIR) {
833 /* Cannot create cross-device links. */
834 if (dvp->v_mount != vp->v_mount) {
839 /* Ensure that we do not overflow the maximum number of links imposed
841 KKASSERT(node->tn_links <= LINK_MAX);
842 if (node->tn_links == LINK_MAX) {
847 /* We cannot create links of files marked immutable or append-only. */
848 if (node->tn_flags & (IMMUTABLE | APPEND)) {
853 /* Allocate a new directory entry to represent the node. */
854 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
855 ncp->nc_name, ncp->nc_nlen, &de);
859 /* Insert the new directory entry into the appropriate directory. */
860 tmpfs_dir_attach(dnode, de);
862 /* vp link count has changed, so update node times. */
864 TMPFS_NODE_LOCK(node);
865 node->tn_status |= TMPFS_NODE_CHANGED;
866 TMPFS_NODE_UNLOCK(node);
869 tmpfs_knote(vp, NOTE_LINK);
870 cache_setunresolved(v->a_nch);
871 cache_setvp(v->a_nch, vp);
872 tmpfs_knote(dvp, NOTE_WRITE);
879 /* --------------------------------------------------------------------- */
882 tmpfs_nrename(struct vop_nrename_args *v)
884 struct vnode *fdvp = v->a_fdvp;
885 struct namecache *fncp = v->a_fnch->ncp;
886 struct vnode *fvp = fncp->nc_vp;
887 struct vnode *tdvp = v->a_tdvp;
888 struct namecache *tncp = v->a_tnch->ncp;
889 struct vnode *tvp = tncp->nc_vp;
890 struct tmpfs_dirent *de;
891 struct tmpfs_mount *tmp;
892 struct tmpfs_node *fdnode;
893 struct tmpfs_node *fnode;
894 struct tmpfs_node *tnode;
895 struct tmpfs_node *tdnode;
900 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
902 /* Disallow cross-device renames.
903 * XXX Why isn't this done by the caller? */
904 if (fvp->v_mount != tdvp->v_mount ||
905 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
910 tmp = VFS_TO_TMPFS(tdvp->v_mount);
911 tdnode = VP_TO_TMPFS_DIR(tdvp);
913 /* If source and target are the same file, there is nothing to do. */
919 fdnode = VP_TO_TMPFS_DIR(fdvp);
920 fnode = VP_TO_TMPFS_NODE(fvp);
921 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
923 /* Avoid manipulating '.' and '..' entries. */
928 KKASSERT(de->td_node == fnode);
931 * If replacing an entry in the target directory and that entry
932 * is a directory, it must be empty.
934 * Kern_rename gurantees the destination to be a directory
935 * if the source is one (it does?).
938 KKASSERT(tnode != NULL);
940 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
941 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
946 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
947 if (tnode->tn_size > 0) {
951 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
954 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
958 KKASSERT(fnode->tn_type != VDIR &&
959 tnode->tn_type != VDIR);
963 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
964 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
970 * Ensure that we have enough memory to hold the new name, if it
973 if (fncp->nc_nlen != tncp->nc_nlen ||
974 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
975 newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
976 M_WAITOK | M_NULLOK);
977 if (newname == NULL) {
981 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
982 newname[tncp->nc_nlen] = '\0';
988 * Unlink entry from source directory. Note that the kernel has
989 * already checked for illegal recursion cases (renaming a directory
990 * into a subdirectory of itself).
992 if (fdnode != tdnode)
993 tmpfs_dir_detach(fdnode, de);
996 * Handle any name change. Swap with newname, we will
997 * deallocate it at the end.
999 if (newname != NULL) {
1001 TMPFS_NODE_LOCK(fnode);
1002 fnode->tn_status |= TMPFS_NODE_CHANGED;
1003 TMPFS_NODE_UNLOCK(fnode);
1005 oldname = de->td_name;
1006 de->td_name = newname;
1007 de->td_namelen = (uint16_t)tncp->nc_nlen;
1012 * Link entry to target directory. If the entry
1013 * represents a directory move the parent linkage
1016 if (fdnode != tdnode) {
1017 if (de->td_node->tn_type == VDIR) {
1018 TMPFS_VALIDATE_DIR(fnode);
1020 TMPFS_NODE_LOCK(tdnode);
1022 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1023 TMPFS_NODE_UNLOCK(tdnode);
1025 TMPFS_NODE_LOCK(fnode);
1026 fnode->tn_dir.tn_parent = tdnode;
1027 fnode->tn_status |= TMPFS_NODE_CHANGED;
1028 TMPFS_NODE_UNLOCK(fnode);
1030 TMPFS_NODE_LOCK(fdnode);
1032 fdnode->tn_status |= TMPFS_NODE_MODIFIED;
1033 TMPFS_NODE_UNLOCK(fdnode);
1035 tmpfs_dir_attach(tdnode, de);
1037 TMPFS_NODE_LOCK(tdnode);
1038 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1039 TMPFS_NODE_UNLOCK(tdnode);
1043 * If we are overwriting an entry, we have to remove the old one
1044 * from the target directory.
1047 /* Remove the old entry from the target directory. */
1048 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1049 tmpfs_dir_detach(tdnode, de);
1050 tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1053 * Free the directory entry we just deleted. Note that the
1054 * node referred by it will not be removed until the vnode is
1057 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1058 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1065 kfree(newname, tmp->tm_name_zone);
1068 cache_rename(v->a_fnch, v->a_tnch);
1069 tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1070 tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1071 if (fnode->tn_vnode)
1072 tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1079 /* Release target nodes. */
1080 /* XXX: I don't understand when tdvp can be the same as tvp, but
1081 * other code takes care of this... */
1088 /* --------------------------------------------------------------------- */
1091 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1093 struct vnode *dvp = v->a_dvp;
1094 struct vnode **vpp = v->a_vpp;
1095 struct namecache *ncp = v->a_nch->ncp;
1096 struct vattr *vap = v->a_vap;
1097 struct ucred *cred = v->a_cred;
1100 KKASSERT(vap->va_type == VDIR);
1102 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1104 cache_setunresolved(v->a_nch);
1105 cache_setvp(v->a_nch, *vpp);
1106 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1112 /* --------------------------------------------------------------------- */
1115 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1117 struct vnode *dvp = v->a_dvp;
1118 struct namecache *ncp = v->a_nch->ncp;
1120 struct tmpfs_dirent *de;
1121 struct tmpfs_mount *tmp;
1122 struct tmpfs_node *dnode;
1123 struct tmpfs_node *node;
1127 * We have to acquire the vp from v->a_nch because
1128 * we will likely unresolve the namecache entry, and
1129 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1132 error = cache_vref(v->a_nch, v->a_cred, &vp);
1133 KKASSERT(error == 0);
1136 * Prevalidate so we don't hit an assertion later
1138 if (vp->v_type != VDIR) {
1143 tmp = VFS_TO_TMPFS(dvp->v_mount);
1144 dnode = VP_TO_TMPFS_DIR(dvp);
1145 node = VP_TO_TMPFS_DIR(vp);
1147 /* Directories with more than two entries ('.' and '..') cannot be
1149 if (node->tn_size > 0) {
1154 if ((dnode->tn_flags & APPEND)
1155 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1160 /* This invariant holds only if we are not trying to remove "..".
1161 * We checked for that above so this is safe now. */
1162 KKASSERT(node->tn_dir.tn_parent == dnode);
1164 /* Get the directory entry associated with node (vp). This was
1165 * filled by tmpfs_lookup while looking up the entry. */
1166 de = tmpfs_dir_lookup(dnode, node, ncp);
1167 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1171 /* Check flags to see if we are allowed to remove the directory. */
1172 if ((dnode->tn_flags & APPEND) ||
1173 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1179 /* Detach the directory entry from the directory (dnode). */
1180 tmpfs_dir_detach(dnode, de);
1182 /* No vnode should be allocated for this entry from this point */
1183 TMPFS_NODE_LOCK(node);
1184 TMPFS_ASSERT_ELOCKED(node);
1185 TMPFS_NODE_LOCK(dnode);
1186 TMPFS_ASSERT_ELOCKED(dnode);
1189 /* handled by tmpfs_free_node */
1190 KKASSERT(node->tn_links > 0);
1192 node->tn_dir.tn_parent = NULL;
1194 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1195 TMPFS_NODE_MODIFIED;
1198 /* handled by tmpfs_free_node */
1199 KKASSERT(dnode->tn_links > 0);
1202 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1203 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1205 TMPFS_NODE_UNLOCK(dnode);
1206 TMPFS_NODE_UNLOCK(node);
1208 /* Free the directory entry we just deleted. Note that the node
1209 * referred by it will not be removed until the vnode is really
1211 tmpfs_free_dirent(tmp, de);
1213 /* Release the deleted vnode (will destroy the node, notify
1214 * interested parties and clean it from the cache). */
1216 TMPFS_NODE_LOCK(dnode);
1217 dnode->tn_status |= TMPFS_NODE_CHANGED;
1218 TMPFS_NODE_UNLOCK(dnode);
1221 cache_setunresolved(v->a_nch);
1222 cache_setvp(v->a_nch, NULL);
1223 /*cache_inval_vp(vp, CINV_DESTROY);*/
1224 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1233 /* --------------------------------------------------------------------- */
1236 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1238 struct vnode *dvp = v->a_dvp;
1239 struct vnode **vpp = v->a_vpp;
1240 struct namecache *ncp = v->a_nch->ncp;
1241 struct vattr *vap = v->a_vap;
1242 struct ucred *cred = v->a_cred;
1243 char *target = v->a_target;
1246 vap->va_type = VLNK;
1247 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1249 tmpfs_knote(*vpp, NOTE_WRITE);
1250 cache_setunresolved(v->a_nch);
1251 cache_setvp(v->a_nch, *vpp);
1257 /* --------------------------------------------------------------------- */
1260 tmpfs_readdir(struct vop_readdir_args *v)
1262 struct vnode *vp = v->a_vp;
1263 struct uio *uio = v->a_uio;
1264 int *eofflag = v->a_eofflag;
1265 off_t **cookies = v->a_cookies;
1266 int *ncookies = v->a_ncookies;
1267 struct tmpfs_mount *tmp;
1271 struct tmpfs_node *node;
1273 /* This operation only makes sense on directory nodes. */
1274 if (vp->v_type != VDIR)
1277 tmp = VFS_TO_TMPFS(vp->v_mount);
1278 node = VP_TO_TMPFS_DIR(vp);
1279 startoff = uio->uio_offset;
1281 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1282 error = tmpfs_dir_getdotdent(node, uio);
1288 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1289 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1295 error = tmpfs_dir_getdents(node, uio, &cnt);
1298 KKASSERT(error >= -1);
1303 if (eofflag != NULL)
1305 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1307 /* Update NFS-related variables. */
1308 if (error == 0 && cookies != NULL && ncookies != NULL) {
1310 off_t off = startoff;
1311 struct tmpfs_dirent *de = NULL;
1314 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1316 for (i = 0; i < cnt; i++) {
1317 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1318 if (off == TMPFS_DIRCOOKIE_DOT) {
1319 off = TMPFS_DIRCOOKIE_DOTDOT;
1321 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1322 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1323 } else if (de != NULL) {
1324 de = TAILQ_NEXT(de, td_entries);
1326 de = tmpfs_dir_lookupbycookie(node,
1328 KKASSERT(de != NULL);
1329 de = TAILQ_NEXT(de, td_entries);
1332 off = TMPFS_DIRCOOKIE_EOF;
1334 off = tmpfs_dircookie(de);
1337 (*cookies)[i] = off;
1339 KKASSERT(uio->uio_offset == off);
1345 /* --------------------------------------------------------------------- */
1348 tmpfs_readlink(struct vop_readlink_args *v)
1350 struct vnode *vp = v->a_vp;
1351 struct uio *uio = v->a_uio;
1354 struct tmpfs_node *node;
1356 KKASSERT(uio->uio_offset == 0);
1357 KKASSERT(vp->v_type == VLNK);
1359 node = VP_TO_TMPFS_NODE(vp);
1361 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1363 TMPFS_NODE_LOCK(node);
1364 node->tn_status |= TMPFS_NODE_ACCESSED;
1365 TMPFS_NODE_UNLOCK(node);
1370 /* --------------------------------------------------------------------- */
1373 tmpfs_inactive(struct vop_inactive_args *v)
1375 struct vnode *vp = v->a_vp;
1377 struct tmpfs_node *node;
1379 node = VP_TO_TMPFS_NODE(vp);
1382 * Get rid of unreferenced deleted vnodes sooner rather than
1383 * later so the data memory can be recovered immediately.
1385 * We must truncate the vnode to prevent the normal reclamation
1386 * path from flushing the data for the removed file to disk.
1388 TMPFS_NODE_LOCK(node);
1389 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1390 (node->tn_links == 0 ||
1391 (node->tn_links == 1 && node->tn_type == VDIR &&
1392 node->tn_dir.tn_parent)))
1394 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1395 TMPFS_NODE_UNLOCK(node);
1396 if (node->tn_type == VREG)
1397 tmpfs_truncate(vp, 0);
1400 TMPFS_NODE_UNLOCK(node);
1406 /* --------------------------------------------------------------------- */
1409 tmpfs_reclaim(struct vop_reclaim_args *v)
1411 struct vnode *vp = v->a_vp;
1412 struct tmpfs_mount *tmp;
1413 struct tmpfs_node *node;
1415 node = VP_TO_TMPFS_NODE(vp);
1416 tmp = VFS_TO_TMPFS(vp->v_mount);
1421 * If the node referenced by this vnode was deleted by the
1422 * user, we must free its associated data structures now that
1423 * the vnode is being reclaimed.
1425 * Directories have an extra link ref.
1427 TMPFS_NODE_LOCK(node);
1428 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1429 (node->tn_links == 0 ||
1430 (node->tn_links == 1 && node->tn_type == VDIR &&
1431 node->tn_dir.tn_parent)))
1433 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1434 tmpfs_free_node(tmp, node);
1437 TMPFS_NODE_UNLOCK(node);
1440 KKASSERT(vp->v_data == NULL);
1444 /* --------------------------------------------------------------------- */
1447 tmpfs_print(struct vop_print_args *v)
1449 struct vnode *vp = v->a_vp;
1451 struct tmpfs_node *node;
1453 node = VP_TO_TMPFS_NODE(vp);
1455 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1456 node, node->tn_flags, node->tn_links);
1457 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1458 node->tn_mode, node->tn_uid, node->tn_gid,
1459 (uintmax_t)node->tn_size, node->tn_status);
1461 if (vp->v_type == VFIFO)
1469 /* --------------------------------------------------------------------- */
1472 tmpfs_pathconf(struct vop_pathconf_args *v)
1474 int name = v->a_name;
1475 register_t *retval = v->a_retval;
1498 case _PC_CHOWN_RESTRICTED:
1510 case _PC_FILESIZEBITS:
1511 *retval = 0; /* XXX Don't know which value should I return. */
1521 /************************************************************************
1523 ************************************************************************/
1525 static void filt_tmpfsdetach(struct knote *kn);
1526 static int filt_tmpfsread(struct knote *kn, long hint);
1527 static int filt_tmpfswrite(struct knote *kn, long hint);
1528 static int filt_tmpfsvnode(struct knote *kn, long hint);
1530 static struct filterops tmpfsread_filtops =
1531 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsread };
1532 static struct filterops tmpfswrite_filtops =
1533 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfswrite };
1534 static struct filterops tmpfsvnode_filtops =
1535 { FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1538 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1540 struct vnode *vp = ap->a_vp;
1541 struct knote *kn = ap->a_kn;
1543 switch (kn->kn_filter) {
1545 kn->kn_fop = &tmpfsread_filtops;
1548 kn->kn_fop = &tmpfswrite_filtops;
1551 kn->kn_fop = &tmpfsvnode_filtops;
1554 return (EOPNOTSUPP);
1557 kn->kn_hook = (caddr_t)vp;
1559 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1565 filt_tmpfsdetach(struct knote *kn)
1567 struct vnode *vp = (void *)kn->kn_hook;
1569 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1573 filt_tmpfsread(struct knote *kn, long hint)
1575 struct vnode *vp = (void *)kn->kn_hook;
1576 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1579 if (hint == NOTE_REVOKE) {
1580 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1583 off = node->tn_size - kn->kn_fp->f_offset;
1584 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1585 if (kn->kn_sfflags & NOTE_OLDAPI)
1589 * Handle possible MP race interlock on filter check/write
1591 if (kn->kn_data == 0) {
1593 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1596 return (kn->kn_data != 0);
1600 filt_tmpfswrite(struct knote *kn, long hint)
1602 if (hint == NOTE_REVOKE)
1603 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1609 filt_tmpfsvnode(struct knote *kn, long hint)
1611 if (kn->kn_sfflags & hint)
1612 kn->kn_fflags |= hint;
1613 if (hint == NOTE_REVOKE) {
1614 kn->kn_flags |= EV_EOF;
1617 return (kn->kn_fflags != 0);
1621 /* --------------------------------------------------------------------- */
1624 * vnode operations vector used for files stored in a tmpfs file system.
1626 struct vop_ops tmpfs_vnode_vops = {
1627 .vop_default = vop_defaultop,
1628 .vop_getpages = vop_stdgetpages,
1629 .vop_putpages = vop_stdputpages,
1630 .vop_ncreate = tmpfs_ncreate,
1631 .vop_nresolve = tmpfs_nresolve,
1632 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1633 .vop_nmknod = tmpfs_nmknod,
1634 .vop_open = tmpfs_open,
1635 .vop_close = tmpfs_close,
1636 .vop_access = tmpfs_access,
1637 .vop_getattr = tmpfs_getattr,
1638 .vop_setattr = tmpfs_setattr,
1639 .vop_read = tmpfs_read,
1640 .vop_write = tmpfs_write,
1641 .vop_fsync = tmpfs_fsync,
1642 .vop_nremove = tmpfs_nremove,
1643 .vop_nlink = tmpfs_nlink,
1644 .vop_nrename = tmpfs_nrename,
1645 .vop_nmkdir = tmpfs_nmkdir,
1646 .vop_nrmdir = tmpfs_nrmdir,
1647 .vop_nsymlink = tmpfs_nsymlink,
1648 .vop_readdir = tmpfs_readdir,
1649 .vop_readlink = tmpfs_readlink,
1650 .vop_inactive = tmpfs_inactive,
1651 .vop_reclaim = tmpfs_reclaim,
1652 .vop_print = tmpfs_print,
1653 .vop_pathconf = tmpfs_pathconf,
1654 .vop_bmap = tmpfs_bmap,
1655 .vop_strategy = tmpfs_strategy,
1656 .vop_advlock = tmpfs_advlock,
1657 .vop_kqfilter = tmpfs_kqfilter