1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
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>
60 #include <vfs/fifofs/fifo.h>
61 #include <vfs/tmpfs/tmpfs_vnops.h>
62 #include <vfs/tmpfs/tmpfs.h>
64 MALLOC_DECLARE(M_TMPFS);
66 /* --------------------------------------------------------------------- */
69 tmpfs_nresolve(struct vop_nresolve_args *v)
71 struct vnode *dvp = v->a_dvp;
72 struct vnode *vp = NULL;
73 struct namecache *ncp = v->a_nch->ncp;
74 struct tmpfs_node *tnode;
77 struct tmpfs_dirent *de;
78 struct tmpfs_node *dnode;
80 dnode = VP_TO_TMPFS_DIR(dvp);
82 de = tmpfs_dir_lookup(dnode, NULL, ncp);
87 * Allocate a vnode for the node we found.
90 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
91 LK_EXCLUSIVE | LK_RETRY, &vp);
99 * Store the result of this lookup in the cache. Avoid this if the
100 * request was for creation, as it does not improve timings on
105 cache_setvp(v->a_nch, vp);
107 } else if (error == ENOENT) {
108 cache_setvp(v->a_nch, NULL);
114 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
116 struct vnode *dvp = v->a_dvp;
117 struct vnode **vpp = v->a_vpp;
118 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
119 struct ucred *cred = v->a_cred;
123 /* Check accessibility of requested node as a first step. */
124 error = VOP_ACCESS(dvp, VEXEC, cred);
128 if (dnode->tn_dir.tn_parent != NULL) {
129 /* Allocate a new vnode on the matching entry. */
130 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
131 LK_EXCLUSIVE | LK_RETRY, vpp);
137 return (*vpp == NULL) ? ENOENT : 0;
140 /* --------------------------------------------------------------------- */
143 tmpfs_ncreate(struct vop_ncreate_args *v)
145 struct vnode *dvp = v->a_dvp;
146 struct vnode **vpp = v->a_vpp;
147 struct namecache *ncp = v->a_nch->ncp;
148 struct vattr *vap = v->a_vap;
149 struct ucred *cred = v->a_cred;
152 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
154 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
156 cache_setunresolved(v->a_nch);
157 cache_setvp(v->a_nch, *vpp);
162 /* --------------------------------------------------------------------- */
165 tmpfs_nmknod(struct vop_nmknod_args *v)
167 struct vnode *dvp = v->a_dvp;
168 struct vnode **vpp = v->a_vpp;
169 struct namecache *ncp = v->a_nch->ncp;
170 struct vattr *vap = v->a_vap;
171 struct ucred *cred = v->a_cred;
174 if (vap->va_type != VBLK && vap->va_type != VCHR &&
175 vap->va_type != VFIFO)
178 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
180 cache_setunresolved(v->a_nch);
181 cache_setvp(v->a_nch, *vpp);
187 /* --------------------------------------------------------------------- */
190 tmpfs_open(struct vop_open_args *v)
192 struct vnode *vp = v->a_vp;
193 int mode = v->a_mode;
196 struct tmpfs_node *node;
198 node = VP_TO_TMPFS_NODE(vp);
200 /* The file is still active but all its names have been removed
201 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
202 * it is about to die. */
203 if (node->tn_links < 1)
206 /* If the file is marked append-only, deny write requests. */
207 if ((node->tn_flags & APPEND) &&
208 (mode & (FWRITE | O_APPEND)) == FWRITE) {
211 return (vop_stdopen(v));
216 /* --------------------------------------------------------------------- */
219 tmpfs_close(struct vop_close_args *v)
221 struct vnode *vp = v->a_vp;
222 struct tmpfs_node *node;
224 node = VP_TO_TMPFS_NODE(vp);
226 if (node->tn_links > 0) {
227 /* Update node times. No need to do it if the node has
228 * been deleted, because it will vanish after we return. */
232 return vop_stdclose(v);
235 /* --------------------------------------------------------------------- */
238 tmpfs_access(struct vop_access_args *v)
240 struct vnode *vp = v->a_vp;
242 struct tmpfs_node *node;
244 node = VP_TO_TMPFS_NODE(vp);
246 switch (vp->v_type) {
252 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
272 if (VWRITE && node->tn_flags & IMMUTABLE) {
277 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
284 /* --------------------------------------------------------------------- */
287 tmpfs_getattr(struct vop_getattr_args *v)
289 struct vnode *vp = v->a_vp;
290 struct vattr *vap = v->a_vap;
291 struct tmpfs_node *node;
293 node = VP_TO_TMPFS_NODE(vp);
297 vap->va_type = vp->v_type;
298 vap->va_mode = node->tn_mode;
299 vap->va_nlink = node->tn_links;
300 vap->va_uid = node->tn_uid;
301 vap->va_gid = node->tn_gid;
302 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
303 vap->va_fileid = node->tn_id;
304 vap->va_size = node->tn_size;
305 vap->va_blocksize = PAGE_SIZE;
306 vap->va_atime.tv_sec = node->tn_atime;
307 vap->va_atime.tv_nsec = node->tn_atimensec;
308 vap->va_mtime.tv_sec = node->tn_mtime;
309 vap->va_mtime.tv_nsec = node->tn_mtimensec;
310 vap->va_ctime.tv_sec = node->tn_ctime;
311 vap->va_ctime.tv_nsec = node->tn_ctimensec;
312 vap->va_gen = node->tn_gen;
313 vap->va_flags = node->tn_flags;
314 if (vp->v_type == VBLK || vp->v_type == VCHR)
316 vap->va_rmajor = umajor(node->tn_rdev);
317 vap->va_rminor = uminor(node->tn_rdev);
319 vap->va_bytes = round_page(node->tn_size);
325 /* --------------------------------------------------------------------- */
328 tmpfs_setattr(struct vop_setattr_args *v)
330 struct vnode *vp = v->a_vp;
331 struct vattr *vap = v->a_vap;
332 struct ucred *cred = v->a_cred;
335 if (error == 0 && (vap->va_flags != VNOVAL))
336 error = tmpfs_chflags(vp, vap->va_flags, cred);
338 if (error == 0 && (vap->va_size != VNOVAL))
339 error = tmpfs_chsize(vp, vap->va_size, cred);
341 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
342 vap->va_gid != (gid_t)VNOVAL)) {
343 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
346 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
347 error = tmpfs_chmod(vp, vap->va_mode, cred);
349 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
350 vap->va_atime.tv_nsec != VNOVAL) ||
351 (vap->va_mtime.tv_sec != VNOVAL &&
352 vap->va_mtime.tv_nsec != VNOVAL) )) {
353 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
354 vap->va_vaflags, cred);
357 /* Update the node times. We give preference to the error codes
358 * generated by this function rather than the ones that may arise
359 * from tmpfs_update. */
365 /* --------------------------------------------------------------------- */
368 * fsync is usually a NOP, but we must take action when unmounting or
372 tmpfs_fsync(struct vop_fsync_args *v)
374 struct tmpfs_mount *tmp;
375 struct tmpfs_node *node;
376 struct vnode *vp = v->a_vp;
378 tmp = VFS_TO_TMPFS(vp->v_mount);
379 node = VP_TO_TMPFS_NODE(vp);
382 if (vp->v_type == VREG) {
383 if (vp->v_flag & VRECLAIMED) {
384 if (node->tn_links == 0)
385 tmpfs_truncate(vp, 0);
387 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
393 /* --------------------------------------------------------------------- */
396 tmpfs_read (struct vop_read_args *ap)
399 struct vnode *vp = ap->a_vp;
400 struct uio *uio = ap->a_uio;
401 struct tmpfs_node *node;
409 if (uio->uio_resid == 0) {
413 node = VP_TO_TMPFS_NODE(vp);
415 if (uio->uio_offset < 0)
417 if (vp->v_type != VREG)
421 if(curthread->td_mpcount)
429 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
431 * Use buffer cache I/O (via tmpfs_strategy)
433 offset = (size_t)uio->uio_offset & BMASK;
434 base_offset = (off_t)uio->uio_offset - offset;
435 bp = getcacheblk(vp, base_offset);
438 if (got_mplock == 0) {
443 error = bread(vp, base_offset, BSIZE, &bp);
446 kprintf("tmpfs_read bread error %d\n", error);
451 if (got_mplock == 0) {
457 * Figure out how many bytes we can actually copy this loop.
459 len = BSIZE - offset;
460 if (len > uio->uio_resid)
461 len = uio->uio_resid;
462 if (len > node->tn_size - uio->uio_offset)
463 len = (size_t)(node->tn_size - uio->uio_offset);
465 error = uiomove((char *)bp->b_data + offset, len, uio);
468 kprintf("tmpfs_read uiomove error %d\n", error);
476 TMPFS_NODE_LOCK(node);
477 node->tn_status |= TMPFS_NODE_ACCESSED;
478 TMPFS_NODE_UNLOCK(node);
484 tmpfs_write (struct vop_write_args *ap)
487 struct vnode *vp = ap->a_vp;
488 struct uio *uio = ap->a_uio;
489 struct thread *td = uio->uio_td;
490 struct tmpfs_node *node;
502 if (uio->uio_resid == 0) {
506 node = VP_TO_TMPFS_NODE(vp);
508 if (vp->v_type != VREG)
511 oldsize = node->tn_size;
512 if (ap->a_ioflag & IO_APPEND)
513 uio->uio_offset = node->tn_size;
516 * Check for illegal write offsets.
518 if (uio->uio_offset + uio->uio_resid >
519 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
522 if (vp->v_type == VREG && td != NULL) {
523 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
526 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
527 ksignal(td->td_proc, SIGXFSZ);
534 * Extend the file's size if necessary
536 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
539 if (curthread->td_mpcount) {
548 while (uio->uio_resid > 0) {
550 * Use buffer cache I/O (via tmpfs_strategy)
552 offset = (size_t)uio->uio_offset & BMASK;
553 base_offset = (off_t)uio->uio_offset - offset;
554 len = BSIZE - offset;
555 if (len > uio->uio_resid)
556 len = uio->uio_resid;
558 if ((uio->uio_offset + len) > node->tn_size) {
559 trivial = (uio->uio_offset <= node->tn_size);
560 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
566 * Read to fill in any gaps. Theoretically we could
567 * optimize this if the write covers the entire buffer
568 * and is not a UIO_NOCOPY write, however this can lead
569 * to a security violation exposing random kernel memory
570 * (whatever junk was in the backing VM pages before).
572 * So just use bread() to do the right thing.
574 error = bread(vp, base_offset, BSIZE, &bp);
575 error = uiomove((char *)bp->b_data + offset, len, uio);
577 kprintf("tmpfs_write uiomove error %d\n", error);
582 if (uio->uio_offset > node->tn_size)
583 node->tn_size = uio->uio_offset;
586 * The data has been loaded into the buffer, write it out.
588 * We want tmpfs to be able to use all available ram, not
589 * just the buffer cache, so if not explicitly paging we
590 * use buwrite() to leave the buffer clean but mark all the
591 * VM pages valid+dirty.
593 * When the kernel is paging, either via normal pageout
594 * operation or when cleaning the object during a recycle,
595 * the underlying VM pages are going to get thrown away
596 * so we MUST write them to swap.
598 * XXX unfortunately this catches msync() system calls too
601 if (ap->a_ioflag & IO_SYNC) {
603 } else if ((ap->a_ioflag & IO_ASYNC) ||
604 (uio->uio_segflg == UIO_NOCOPY)) {
611 kprintf("tmpfs_write bwrite error %d\n", error);
621 (void)tmpfs_reg_resize(vp, oldsize, trivial);
625 TMPFS_NODE_LOCK(node);
626 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
627 (extended? TMPFS_NODE_CHANGED : 0);
629 if (node->tn_mode & (S_ISUID | S_ISGID)) {
630 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
631 node->tn_mode &= ~(S_ISUID | S_ISGID);
633 TMPFS_NODE_UNLOCK(node);
639 tmpfs_advlock (struct vop_advlock_args *ap)
641 struct tmpfs_node *node;
642 struct vnode *vp = ap->a_vp;
644 node = VP_TO_TMPFS_NODE(vp);
646 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
651 tmpfs_strategy(struct vop_strategy_args *ap)
653 struct bio *bio = ap->a_bio;
654 struct buf *bp = bio->bio_buf;
655 struct vnode *vp = ap->a_vp;
656 struct tmpfs_node *node;
659 if (vp->v_type != VREG) {
660 bp->b_resid = bp->b_bcount;
661 bp->b_flags |= B_ERROR | B_INVAL;
662 bp->b_error = EINVAL;
667 node = VP_TO_TMPFS_NODE(vp);
669 uobj = node->tn_reg.tn_aobj;
672 * Call swap_pager_strategy to read or write between the VM
673 * object and the buffer cache.
675 swap_pager_strategy(uobj, bio);
681 tmpfs_bmap(struct vop_bmap_args *ap)
683 if (ap->a_doffsetp != NULL)
684 *ap->a_doffsetp = ap->a_loffset;
685 if (ap->a_runp != NULL)
687 if (ap->a_runb != NULL)
693 /* --------------------------------------------------------------------- */
696 tmpfs_nremove(struct vop_nremove_args *v)
698 struct vnode *dvp = v->a_dvp;
699 struct namecache *ncp = v->a_nch->ncp;
702 struct tmpfs_dirent *de;
703 struct tmpfs_mount *tmp;
704 struct tmpfs_node *dnode;
705 struct tmpfs_node *node;
708 * We have to acquire the vp from v->a_nch because
709 * we will likely unresolve the namecache entry, and
710 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
711 * sequence to recover space from the file.
713 error = cache_vref(v->a_nch, v->a_cred, &vp);
714 KKASSERT(error == 0);
716 if (vp->v_type == VDIR) {
721 dnode = VP_TO_TMPFS_DIR(dvp);
722 node = VP_TO_TMPFS_NODE(vp);
723 tmp = VFS_TO_TMPFS(vp->v_mount);
724 de = tmpfs_dir_lookup(dnode, node, ncp);
730 /* Files marked as immutable or append-only cannot be deleted. */
731 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
732 (dnode->tn_flags & APPEND)) {
737 /* Remove the entry from the directory; as it is a file, we do not
738 * have to change the number of hard links of the directory. */
739 tmpfs_dir_detach(dnode, de);
741 /* Free the directory entry we just deleted. Note that the node
742 * referred by it will not be removed until the vnode is really
744 tmpfs_free_dirent(tmp, de);
746 if (node->tn_links > 0) {
747 TMPFS_NODE_LOCK(node);
748 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
750 TMPFS_NODE_UNLOCK(node);
753 cache_setunresolved(v->a_nch);
754 cache_setvp(v->a_nch, NULL);
755 /*cache_inval_vp(vp, CINV_DESTROY);*/
764 /* --------------------------------------------------------------------- */
767 tmpfs_nlink(struct vop_nlink_args *v)
769 struct vnode *dvp = v->a_dvp;
770 struct vnode *vp = v->a_vp;
771 struct namecache *ncp = v->a_nch->ncp;
772 struct tmpfs_dirent *de;
773 struct tmpfs_node *node;
774 struct tmpfs_node *dnode;
777 KKASSERT(dvp != vp); /* XXX When can this be false? */
779 node = VP_TO_TMPFS_NODE(vp);
780 dnode = VP_TO_TMPFS_NODE(dvp);
782 /* XXX: Why aren't the following two tests done by the caller? */
784 /* Hard links of directories are forbidden. */
785 if (vp->v_type == VDIR) {
790 /* Cannot create cross-device links. */
791 if (dvp->v_mount != vp->v_mount) {
796 /* Ensure that we do not overflow the maximum number of links imposed
798 KKASSERT(node->tn_links <= LINK_MAX);
799 if (node->tn_links == LINK_MAX) {
804 /* We cannot create links of files marked immutable or append-only. */
805 if (node->tn_flags & (IMMUTABLE | APPEND)) {
810 /* Allocate a new directory entry to represent the node. */
811 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
812 ncp->nc_name, ncp->nc_nlen, &de);
816 /* Insert the new directory entry into the appropriate directory. */
817 tmpfs_dir_attach(dnode, de);
819 /* vp link count has changed, so update node times. */
821 TMPFS_NODE_LOCK(node);
822 node->tn_status |= TMPFS_NODE_CHANGED;
823 TMPFS_NODE_UNLOCK(node);
826 cache_setunresolved(v->a_nch);
827 cache_setvp(v->a_nch, vp);
834 /* --------------------------------------------------------------------- */
837 tmpfs_nrename(struct vop_nrename_args *v)
839 struct vnode *fdvp = v->a_fdvp;
840 struct namecache *fncp = v->a_fnch->ncp;
841 struct vnode *fvp = fncp->nc_vp;
842 struct vnode *tdvp = v->a_tdvp;
843 struct namecache *tncp = v->a_tnch->ncp;
844 struct vnode *tvp = tncp->nc_vp;
845 struct tmpfs_dirent *de;
846 struct tmpfs_mount *tmp;
847 struct tmpfs_node *fdnode;
848 struct tmpfs_node *fnode;
849 struct tmpfs_node *tnode;
850 struct tmpfs_node *tdnode;
855 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
857 /* Disallow cross-device renames.
858 * XXX Why isn't this done by the caller? */
859 if (fvp->v_mount != tdvp->v_mount ||
860 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
865 tmp = VFS_TO_TMPFS(tdvp->v_mount);
866 tdnode = VP_TO_TMPFS_DIR(tdvp);
868 /* If source and target are the same file, there is nothing to do. */
874 fdnode = VP_TO_TMPFS_DIR(fdvp);
875 fnode = VP_TO_TMPFS_NODE(fvp);
876 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
878 /* Avoid manipulating '.' and '..' entries. */
883 KKASSERT(de->td_node == fnode);
886 * If replacing an entry in the target directory and that entry
887 * is a directory, it must be empty.
889 * Kern_rename gurantees the destination to be a directory
890 * if the source is one (it does?).
893 KKASSERT(tnode != NULL);
895 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
896 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
901 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
902 if (tnode->tn_size > 0) {
906 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
909 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
913 KKASSERT(fnode->tn_type != VDIR &&
914 tnode->tn_type != VDIR);
918 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
919 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
925 * Ensure that we have enough memory to hold the new name, if it
928 if (fncp->nc_nlen != tncp->nc_nlen ||
929 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
930 newname = kmalloc(tncp->nc_nlen + 1, M_TMPFSNAME, M_WAITOK);
931 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
932 newname[tncp->nc_nlen] = '\0';
938 * Unlink entry from source directory. Note that the kernel has
939 * already checked for illegal recursion cases (renaming a directory
940 * into a subdirectory of itself).
942 if (fdnode != tdnode)
943 tmpfs_dir_detach(fdnode, de);
946 * Handle any name change. Swap with newname, we will
947 * deallocate it at the end.
949 if (newname != NULL) {
951 TMPFS_NODE_LOCK(fnode);
952 fnode->tn_status |= TMPFS_NODE_CHANGED;
953 TMPFS_NODE_UNLOCK(fnode);
955 oldname = de->td_name;
956 de->td_name = newname;
957 de->td_namelen = (uint16_t)tncp->nc_nlen;
962 * Link entry to target directory. If the entry
963 * represents a directory move the parent linkage
966 if (fdnode != tdnode) {
967 if (de->td_node->tn_type == VDIR) {
968 TMPFS_VALIDATE_DIR(fnode);
970 TMPFS_NODE_LOCK(tdnode);
972 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
973 TMPFS_NODE_UNLOCK(tdnode);
975 TMPFS_NODE_LOCK(fnode);
976 fnode->tn_dir.tn_parent = tdnode;
977 fnode->tn_status |= TMPFS_NODE_CHANGED;
978 TMPFS_NODE_UNLOCK(fnode);
980 TMPFS_NODE_LOCK(fdnode);
982 fdnode->tn_status |= TMPFS_NODE_MODIFIED;
983 TMPFS_NODE_UNLOCK(fdnode);
985 tmpfs_dir_attach(tdnode, de);
987 TMPFS_NODE_LOCK(tdnode);
988 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
989 TMPFS_NODE_UNLOCK(tdnode);
993 * If we are overwriting an entry, we have to remove the old one
994 * from the target directory.
997 /* Remove the old entry from the target directory. */
998 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
999 tmpfs_dir_detach(tdnode, de);
1002 * Free the directory entry we just deleted. Note that the
1003 * node referred by it will not be removed until the vnode is
1006 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1007 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1014 kfree(newname, M_TMPFSNAME);
1017 cache_rename(v->a_fnch, v->a_tnch);
1024 /* Release target nodes. */
1025 /* XXX: I don't understand when tdvp can be the same as tvp, but
1026 * other code takes care of this... */
1033 /* --------------------------------------------------------------------- */
1036 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1038 struct vnode *dvp = v->a_dvp;
1039 struct vnode **vpp = v->a_vpp;
1040 struct namecache *ncp = v->a_nch->ncp;
1041 struct vattr *vap = v->a_vap;
1042 struct ucred *cred = v->a_cred;
1045 KKASSERT(vap->va_type == VDIR);
1047 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1049 cache_setunresolved(v->a_nch);
1050 cache_setvp(v->a_nch, *vpp);
1056 /* --------------------------------------------------------------------- */
1059 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1061 struct vnode *dvp = v->a_dvp;
1062 struct namecache *ncp = v->a_nch->ncp;
1064 struct tmpfs_dirent *de;
1065 struct tmpfs_mount *tmp;
1066 struct tmpfs_node *dnode;
1067 struct tmpfs_node *node;
1071 * We have to acquire the vp from v->a_nch because
1072 * we will likely unresolve the namecache entry, and
1073 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1076 error = cache_vref(v->a_nch, v->a_cred, &vp);
1077 KKASSERT(error == 0);
1080 * Prevalidate so we don't hit an assertion later
1082 if (vp->v_type != VDIR) {
1087 tmp = VFS_TO_TMPFS(dvp->v_mount);
1088 dnode = VP_TO_TMPFS_DIR(dvp);
1089 node = VP_TO_TMPFS_DIR(vp);
1091 /* Directories with more than two entries ('.' and '..') cannot be
1093 if (node->tn_size > 0) {
1098 if ((dnode->tn_flags & APPEND)
1099 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1104 /* This invariant holds only if we are not trying to remove "..".
1105 * We checked for that above so this is safe now. */
1106 KKASSERT(node->tn_dir.tn_parent == dnode);
1108 /* Get the directory entry associated with node (vp). This was
1109 * filled by tmpfs_lookup while looking up the entry. */
1110 de = tmpfs_dir_lookup(dnode, node, ncp);
1111 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1115 /* Check flags to see if we are allowed to remove the directory. */
1116 if ((dnode->tn_flags & APPEND) ||
1117 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1123 /* Detach the directory entry from the directory (dnode). */
1124 tmpfs_dir_detach(dnode, de);
1126 /* No vnode should be allocated for this entry from this point */
1127 TMPFS_NODE_LOCK(node);
1128 TMPFS_ASSERT_ELOCKED(node);
1129 TMPFS_NODE_LOCK(dnode);
1130 TMPFS_ASSERT_ELOCKED(dnode);
1133 /* handled by tmpfs_free_node */
1134 KKASSERT(node->tn_links > 0);
1136 node->tn_dir.tn_parent = NULL;
1138 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1139 TMPFS_NODE_MODIFIED;
1142 /* handled by tmpfs_free_node */
1143 KKASSERT(dnode->tn_links > 0);
1146 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1147 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1149 TMPFS_NODE_UNLOCK(dnode);
1150 TMPFS_NODE_UNLOCK(node);
1152 /* Free the directory entry we just deleted. Note that the node
1153 * referred by it will not be removed until the vnode is really
1155 tmpfs_free_dirent(tmp, de);
1157 /* Release the deleted vnode (will destroy the node, notify
1158 * interested parties and clean it from the cache). */
1160 TMPFS_NODE_LOCK(dnode);
1161 dnode->tn_status |= TMPFS_NODE_CHANGED;
1162 TMPFS_NODE_UNLOCK(dnode);
1165 cache_setunresolved(v->a_nch);
1166 cache_setvp(v->a_nch, NULL);
1167 /*cache_inval_vp(vp, CINV_DESTROY);*/
1176 /* --------------------------------------------------------------------- */
1179 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1181 struct vnode *dvp = v->a_dvp;
1182 struct vnode **vpp = v->a_vpp;
1183 struct namecache *ncp = v->a_nch->ncp;
1184 struct vattr *vap = v->a_vap;
1185 struct ucred *cred = v->a_cred;
1186 char *target = v->a_target;
1189 vap->va_type = VLNK;
1190 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1192 cache_setunresolved(v->a_nch);
1193 cache_setvp(v->a_nch, *vpp);
1199 /* --------------------------------------------------------------------- */
1202 tmpfs_readdir(struct vop_readdir_args *v)
1204 struct vnode *vp = v->a_vp;
1205 struct uio *uio = v->a_uio;
1206 int *eofflag = v->a_eofflag;
1207 off_t **cookies = v->a_cookies;
1208 int *ncookies = v->a_ncookies;
1209 struct tmpfs_mount *tmp;
1213 struct tmpfs_node *node;
1215 /* This operation only makes sense on directory nodes. */
1216 if (vp->v_type != VDIR)
1219 tmp = VFS_TO_TMPFS(vp->v_mount);
1220 node = VP_TO_TMPFS_DIR(vp);
1221 startoff = uio->uio_offset;
1223 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1224 error = tmpfs_dir_getdotdent(node, uio);
1230 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1231 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1237 error = tmpfs_dir_getdents(node, uio, &cnt);
1240 KKASSERT(error >= -1);
1245 if (eofflag != NULL)
1247 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1249 /* Update NFS-related variables. */
1250 if (error == 0 && cookies != NULL && ncookies != NULL) {
1252 off_t off = startoff;
1253 struct tmpfs_dirent *de = NULL;
1256 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1258 for (i = 0; i < cnt; i++) {
1259 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1260 if (off == TMPFS_DIRCOOKIE_DOT) {
1261 off = TMPFS_DIRCOOKIE_DOTDOT;
1263 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1264 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1265 } else if (de != NULL) {
1266 de = TAILQ_NEXT(de, td_entries);
1268 de = tmpfs_dir_lookupbycookie(node,
1270 KKASSERT(de != NULL);
1271 de = TAILQ_NEXT(de, td_entries);
1274 off = TMPFS_DIRCOOKIE_EOF;
1276 off = tmpfs_dircookie(de);
1279 (*cookies)[i] = off;
1281 KKASSERT(uio->uio_offset == off);
1287 /* --------------------------------------------------------------------- */
1290 tmpfs_readlink(struct vop_readlink_args *v)
1292 struct vnode *vp = v->a_vp;
1293 struct uio *uio = v->a_uio;
1296 struct tmpfs_node *node;
1298 KKASSERT(uio->uio_offset == 0);
1299 KKASSERT(vp->v_type == VLNK);
1301 node = VP_TO_TMPFS_NODE(vp);
1303 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1305 TMPFS_NODE_LOCK(node);
1306 node->tn_status |= TMPFS_NODE_ACCESSED;
1307 TMPFS_NODE_UNLOCK(node);
1312 /* --------------------------------------------------------------------- */
1315 tmpfs_inactive(struct vop_inactive_args *v)
1317 struct vnode *vp = v->a_vp;
1319 struct tmpfs_node *node;
1321 node = VP_TO_TMPFS_NODE(vp);
1324 * Get rid of unreferenced deleted vnodes sooner rather than
1325 * later so the data memory can be recovered immediately.
1327 * We must truncate the vnode to prevent the normal reclamation
1328 * path from flushing the data for the removed file to disk.
1330 TMPFS_NODE_LOCK(node);
1331 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1332 (node->tn_links == 0 ||
1333 (node->tn_links == 1 && node->tn_type == VDIR &&
1334 node->tn_dir.tn_parent)))
1336 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1337 TMPFS_NODE_UNLOCK(node);
1338 if (node->tn_type == VREG)
1339 tmpfs_truncate(vp, 0);
1342 TMPFS_NODE_UNLOCK(node);
1348 /* --------------------------------------------------------------------- */
1351 tmpfs_reclaim(struct vop_reclaim_args *v)
1353 struct vnode *vp = v->a_vp;
1354 struct tmpfs_mount *tmp;
1355 struct tmpfs_node *node;
1357 node = VP_TO_TMPFS_NODE(vp);
1358 tmp = VFS_TO_TMPFS(vp->v_mount);
1363 * If the node referenced by this vnode was deleted by the
1364 * user, we must free its associated data structures now that
1365 * the vnode is being reclaimed.
1367 * Directories have an extra link ref.
1369 TMPFS_NODE_LOCK(node);
1370 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1371 (node->tn_links == 0 ||
1372 (node->tn_links == 1 && node->tn_type == VDIR &&
1373 node->tn_dir.tn_parent)))
1375 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1376 tmpfs_free_node(tmp, node);
1379 TMPFS_NODE_UNLOCK(node);
1382 KKASSERT(vp->v_data == NULL);
1386 /* --------------------------------------------------------------------- */
1389 tmpfs_print(struct vop_print_args *v)
1391 struct vnode *vp = v->a_vp;
1393 struct tmpfs_node *node;
1395 node = VP_TO_TMPFS_NODE(vp);
1397 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1398 node, node->tn_flags, node->tn_links);
1399 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1400 node->tn_mode, node->tn_uid, node->tn_gid,
1401 (uintmax_t)node->tn_size, node->tn_status);
1403 if (vp->v_type == VFIFO)
1411 /* --------------------------------------------------------------------- */
1414 tmpfs_pathconf(struct vop_pathconf_args *v)
1416 int name = v->a_name;
1417 register_t *retval = v->a_retval;
1440 case _PC_CHOWN_RESTRICTED:
1452 case _PC_FILESIZEBITS:
1453 *retval = 0; /* XXX Don't know which value should I return. */
1463 /* --------------------------------------------------------------------- */
1466 * vnode operations vector used for files stored in a tmpfs file system.
1468 struct vop_ops tmpfs_vnode_vops = {
1469 .vop_default = vop_defaultop,
1470 .vop_getpages = vop_stdgetpages,
1471 .vop_putpages = vop_stdputpages,
1472 .vop_ncreate = tmpfs_ncreate,
1473 .vop_nresolve = tmpfs_nresolve,
1474 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1475 .vop_nmknod = tmpfs_nmknod,
1476 .vop_open = tmpfs_open,
1477 .vop_close = tmpfs_close,
1478 .vop_access = tmpfs_access,
1479 .vop_getattr = tmpfs_getattr,
1480 .vop_setattr = tmpfs_setattr,
1481 .vop_read = tmpfs_read,
1482 .vop_write = tmpfs_write,
1483 .vop_fsync = tmpfs_fsync,
1484 .vop_nremove = tmpfs_nremove,
1485 .vop_nlink = tmpfs_nlink,
1486 .vop_nrename = tmpfs_nrename,
1487 .vop_nmkdir = tmpfs_nmkdir,
1488 .vop_nrmdir = tmpfs_nrmdir,
1489 .vop_nsymlink = tmpfs_nsymlink,
1490 .vop_readdir = tmpfs_readdir,
1491 .vop_readlink = tmpfs_readlink,
1492 .vop_inactive = tmpfs_inactive,
1493 .vop_reclaim = tmpfs_reclaim,
1494 .vop_print = tmpfs_print,
1495 .vop_pathconf = tmpfs_pathconf,
1496 .vop_bmap = tmpfs_bmap,
1497 .vop_strategy = tmpfs_strategy,
1498 .vop_advlock = tmpfs_advlock,