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>
47 #include <sys/sfbuf.h>
49 #include <sys/systm.h>
50 #include <sys/unistd.h>
51 #include <sys/vfsops.h>
52 #include <sys/vnode.h>
54 #include <sys/mplock2.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_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);
67 /* --------------------------------------------------------------------- */
70 tmpfs_nresolve(struct vop_nresolve_args *v)
72 struct vnode *dvp = v->a_dvp;
73 struct vnode *vp = NULL;
74 struct namecache *ncp = v->a_nch->ncp;
75 struct ucred *cred = v->a_cred;
78 struct tmpfs_dirent *de;
79 struct tmpfs_node *dnode;
81 dnode = VP_TO_TMPFS_DIR(dvp);
83 /* Check accessibility of requested node as a first step. */
84 error = VOP_ACCESS(dvp, VEXEC, cred);
88 if (dnode->tn_dir.tn_parent == NULL) {
93 de = tmpfs_dir_lookup(dnode, NULL, ncp);
95 /* The entry was not found in the directory.
96 * This is OK if we are creating or renaming an
97 * entry and are working on the last component of
99 error = VOP_ACCESS(dvp, VWRITE, cred);
107 struct tmpfs_node *tnode;
109 /* The entry was found, so get its associated
113 /* If we are not at the last path component and
114 * found a non-directory or non-link entry (which
115 * may itself be pointing to a directory), raise
117 if (tnode->tn_links > 1 &&
118 tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
123 error = VOP_ACCESS(dvp, VWRITE, cred);
127 /* Allocate a new vnode on the matching entry. */
128 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
129 LK_EXCLUSIVE | LK_RETRY, &vp);
133 if ((dnode->tn_mode & S_ISTXT) &&
134 VOP_ACCESS(vp, VWRITE, cred)) {
144 /* Store the result of this lookup in the cache. Avoid this if the
145 * request was for creation, as it does not improve timings on
149 cache_setvp(v->a_nch, vp);
152 if (error == ENOENT) {
153 cache_setvp(v->a_nch, NULL);
159 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
161 struct vnode *dvp = v->a_dvp;
162 struct vnode **vpp = v->a_vpp;
163 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
164 struct ucred *cred = v->a_cred;
168 /* Check accessibility of requested node as a first step. */
169 error = VOP_ACCESS(dvp, VEXEC, cred);
173 if (dnode->tn_dir.tn_parent != NULL) {
174 /* Allocate a new vnode on the matching entry. */
175 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
176 LK_EXCLUSIVE | LK_RETRY, vpp);
182 return (*vpp == NULL) ? ENOENT : 0;
185 /* --------------------------------------------------------------------- */
188 tmpfs_ncreate(struct vop_ncreate_args *v)
190 struct vnode *dvp = v->a_dvp;
191 struct vnode **vpp = v->a_vpp;
192 struct namecache *ncp = v->a_nch->ncp;
193 struct vattr *vap = v->a_vap;
194 struct ucred *cred = v->a_cred;
197 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
199 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
201 cache_setunresolved(v->a_nch);
202 cache_setvp(v->a_nch, *vpp);
207 /* --------------------------------------------------------------------- */
210 tmpfs_nmknod(struct vop_nmknod_args *v)
212 struct vnode *dvp = v->a_dvp;
213 struct vnode **vpp = v->a_vpp;
214 struct namecache *ncp = v->a_nch->ncp;
215 struct vattr *vap = v->a_vap;
216 struct ucred *cred = v->a_cred;
219 if (vap->va_type != VBLK && vap->va_type != VCHR &&
220 vap->va_type != VFIFO)
223 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
225 cache_setunresolved(v->a_nch);
226 cache_setvp(v->a_nch, *vpp);
232 /* --------------------------------------------------------------------- */
235 tmpfs_open(struct vop_open_args *v)
237 struct vnode *vp = v->a_vp;
238 int mode = v->a_mode;
241 struct tmpfs_node *node;
243 node = VP_TO_TMPFS_NODE(vp);
245 /* The file is still active but all its names have been removed
246 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
247 * it is about to die. */
248 if (node->tn_links < 1)
251 /* If the file is marked append-only, deny write requests. */
252 if ((node->tn_flags & APPEND) &&
253 (mode & (FWRITE | O_APPEND)) == FWRITE) {
256 return (vop_stdopen(v));
261 /* --------------------------------------------------------------------- */
264 tmpfs_close(struct vop_close_args *v)
266 struct vnode *vp = v->a_vp;
267 struct tmpfs_node *node;
269 node = VP_TO_TMPFS_NODE(vp);
271 if (node->tn_links > 0) {
272 /* Update node times. No need to do it if the node has
273 * been deleted, because it will vanish after we return. */
277 return vop_stdclose(v);
280 /* --------------------------------------------------------------------- */
283 tmpfs_access(struct vop_access_args *v)
285 struct vnode *vp = v->a_vp;
287 struct tmpfs_node *node;
289 node = VP_TO_TMPFS_NODE(vp);
291 switch (vp->v_type) {
297 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
317 if (VWRITE && node->tn_flags & IMMUTABLE) {
322 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
329 /* --------------------------------------------------------------------- */
332 tmpfs_getattr(struct vop_getattr_args *v)
334 struct vnode *vp = v->a_vp;
335 struct vattr *vap = v->a_vap;
336 struct tmpfs_node *node;
338 node = VP_TO_TMPFS_NODE(vp);
342 vap->va_type = vp->v_type;
343 vap->va_mode = node->tn_mode;
344 vap->va_nlink = node->tn_links;
345 vap->va_uid = node->tn_uid;
346 vap->va_gid = node->tn_gid;
347 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
348 vap->va_fileid = node->tn_id;
349 vap->va_size = node->tn_size;
350 vap->va_blocksize = PAGE_SIZE;
351 vap->va_atime.tv_sec = node->tn_atime;
352 vap->va_atime.tv_nsec = node->tn_atimensec;
353 vap->va_mtime.tv_sec = node->tn_mtime;
354 vap->va_mtime.tv_nsec = node->tn_mtimensec;
355 vap->va_ctime.tv_sec = node->tn_ctime;
356 vap->va_ctime.tv_nsec = node->tn_ctimensec;
357 vap->va_gen = node->tn_gen;
358 vap->va_flags = node->tn_flags;
359 if (vp->v_type == VBLK || vp->v_type == VCHR)
361 vap->va_rmajor = umajor(node->tn_rdev);
362 vap->va_rminor = uminor(node->tn_rdev);
364 vap->va_bytes = round_page(node->tn_size);
370 /* --------------------------------------------------------------------- */
373 tmpfs_setattr(struct vop_setattr_args *v)
375 struct vnode *vp = v->a_vp;
376 struct vattr *vap = v->a_vap;
377 struct ucred *cred = v->a_cred;
380 /* Abort if any unsettable attribute is given. */
381 if (vap->va_type != VNON ||
382 vap->va_nlink != VNOVAL ||
383 vap->va_fsid != VNOVAL ||
384 vap->va_fileid != VNOVAL ||
385 vap->va_blocksize != VNOVAL ||
386 vap->va_gen != VNOVAL ||
387 vap->va_rmajor != VNOVAL ||
388 vap->va_bytes != VNOVAL)
391 if (error == 0 && (vap->va_flags != VNOVAL))
392 error = tmpfs_chflags(vp, vap->va_flags, cred);
394 if (error == 0 && (vap->va_size != VNOVAL))
395 error = tmpfs_chsize(vp, vap->va_size, cred);
397 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
398 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
400 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
401 error = tmpfs_chmod(vp, vap->va_mode, cred);
403 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
404 vap->va_atime.tv_nsec != VNOVAL) ||
405 (vap->va_mtime.tv_sec != VNOVAL &&
406 vap->va_mtime.tv_nsec != VNOVAL) ))
407 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
408 vap->va_vaflags, cred);
410 /* Update the node times. We give preference to the error codes
411 * generated by this function rather than the ones that may arise
412 * from tmpfs_update. */
418 /* --------------------------------------------------------------------- */
421 * fsync is usually a NOP, but we must take action when unmounting or
425 tmpfs_fsync(struct vop_fsync_args *v)
427 struct tmpfs_mount *tmp;
428 struct tmpfs_node *node;
429 struct vnode *vp = v->a_vp;
431 tmp = VFS_TO_TMPFS(vp->v_mount);
432 node = VP_TO_TMPFS_NODE(vp);
435 if (vp->v_type == VREG) {
436 if (tmp->tm_flags & TMPFS_FLAG_UNMOUNTING) {
437 tmpfs_truncate(vp, 0);
438 } else if (vp->v_flag & VRECLAIMED) {
439 if (node->tn_links == 0)
440 tmpfs_truncate(vp, 0);
442 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
448 /* --------------------------------------------------------------------- */
451 tmpfs_read (struct vop_read_args *ap)
454 struct vnode *vp = ap->a_vp;
455 struct uio *uio = ap->a_uio;
456 struct tmpfs_node *node;
464 if (uio->uio_resid == 0) {
468 node = VP_TO_TMPFS_NODE(vp);
470 if (uio->uio_offset < 0)
472 if (vp->v_type != VREG)
476 if(curthread->td_mpcount)
484 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
486 * Use buffer cache I/O (via tmpfs_strategy)
488 offset = (size_t)uio->uio_offset & BMASK;
489 base_offset = (off_t)uio->uio_offset - offset;
490 bp = getcacheblk(vp, base_offset);
493 if (got_mplock == 0) {
498 error = bread(vp, base_offset, BSIZE, &bp);
501 kprintf("tmpfs_read bread error %d\n", error);
506 if (got_mplock == 0) {
512 * Figure out how many bytes we can actually copy this loop.
514 len = BSIZE - offset;
515 if (len > uio->uio_resid)
516 len = uio->uio_resid;
517 if (len > node->tn_size - uio->uio_offset)
518 len = (size_t)(node->tn_size - uio->uio_offset);
520 error = uiomove((char *)bp->b_data + offset, len, uio);
523 kprintf("tmpfs_read uiomove error %d\n", error);
531 TMPFS_NODE_LOCK(node);
532 node->tn_status |= TMPFS_NODE_ACCESSED;
533 TMPFS_NODE_UNLOCK(node);
539 tmpfs_write (struct vop_write_args *ap)
542 struct vnode *vp = ap->a_vp;
543 struct uio *uio = ap->a_uio;
544 struct thread *td = uio->uio_td;
545 struct tmpfs_node *node;
557 if (uio->uio_resid == 0) {
561 node = VP_TO_TMPFS_NODE(vp);
563 if (vp->v_type != VREG)
566 oldsize = node->tn_size;
567 if (ap->a_ioflag & IO_APPEND)
568 uio->uio_offset = node->tn_size;
571 * Check for illegal write offsets.
573 if (uio->uio_offset + uio->uio_resid >
574 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
577 if (vp->v_type == VREG && td != NULL) {
578 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
581 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
582 ksignal(td->td_proc, SIGXFSZ);
589 * Extend the file's size if necessary
591 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
594 if (curthread->td_mpcount) {
603 while (uio->uio_resid > 0) {
605 * Use buffer cache I/O (via tmpfs_strategy)
607 offset = (size_t)uio->uio_offset & BMASK;
608 base_offset = (off_t)uio->uio_offset - offset;
609 len = BSIZE - offset;
610 if (len > uio->uio_resid)
611 len = uio->uio_resid;
613 if ((uio->uio_offset + len) > node->tn_size) {
614 trivial = (uio->uio_offset <= node->tn_size);
615 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
621 * Read to fill in any gaps. Theoretically we could
622 * optimize this if the write covers the entire buffer
623 * and is not a UIO_NOCOPY write, however this can lead
624 * to a security violation exposing random kernel memory
625 * (whatever junk was in the backing VM pages before).
627 * So just use bread() to do the right thing.
629 error = bread(vp, base_offset, BSIZE, &bp);
630 error = uiomove((char *)bp->b_data + offset, len, uio);
632 kprintf("tmpfs_write uiomove error %d\n", error);
637 if (uio->uio_offset > node->tn_size)
638 node->tn_size = uio->uio_offset;
641 * The data has been loaded into the buffer, write it out.
643 * We want tmpfs to be able to use all available ram, not
644 * just the buffer cache, so if not explicitly paging we
645 * use buwrite() to leave the buffer clean but mark all the
646 * VM pages valid+dirty.
648 * Be careful, the kernel uses IO_SYNC/IO_ASYNC when it
649 * really really wants to flush the buffer, typically as
650 * part of the paging system, and we'll loop forever if
653 if (ap->a_ioflag & IO_SYNC)
655 else if (ap->a_ioflag & IO_ASYNC)
661 kprintf("tmpfs_write bwrite error %d\n", error);
671 (void)tmpfs_reg_resize(vp, oldsize, trivial);
675 TMPFS_NODE_LOCK(node);
676 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
677 (extended? TMPFS_NODE_CHANGED : 0);
679 if (node->tn_mode & (S_ISUID | S_ISGID)) {
680 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
681 node->tn_mode &= ~(S_ISUID | S_ISGID);
683 TMPFS_NODE_UNLOCK(node);
689 tmpfs_advlock (struct vop_advlock_args *ap)
691 struct tmpfs_node *node;
692 struct vnode *vp = ap->a_vp;
694 node = VP_TO_TMPFS_NODE(vp);
696 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
701 tmpfs_strategy(struct vop_strategy_args *ap)
703 struct bio *bio = ap->a_bio;
704 struct buf *bp = bio->bio_buf;
705 struct vnode *vp = ap->a_vp;
706 struct tmpfs_node *node;
709 if (vp->v_type != VREG) {
710 bp->b_resid = bp->b_bcount;
711 bp->b_flags |= B_ERROR | B_INVAL;
712 bp->b_error = EINVAL;
717 node = VP_TO_TMPFS_NODE(vp);
719 uobj = node->tn_reg.tn_aobj;
722 * Call swap_pager_strategy to read or write between the VM
723 * object and the buffer cache.
725 swap_pager_strategy(uobj, bio);
731 tmpfs_bmap(struct vop_bmap_args *ap)
733 if (ap->a_doffsetp != NULL)
734 *ap->a_doffsetp = ap->a_loffset;
735 if (ap->a_runp != NULL)
737 if (ap->a_runb != NULL)
743 /* --------------------------------------------------------------------- */
746 tmpfs_nremove(struct vop_nremove_args *v)
748 struct vnode *dvp = v->a_dvp;
749 struct namecache *ncp = v->a_nch->ncp;
752 struct tmpfs_dirent *de;
753 struct tmpfs_mount *tmp;
754 struct tmpfs_node *dnode;
755 struct tmpfs_node *node;
758 * We have to acquire the vp from v->a_nch because
759 * we will likely unresolve the namecache entry, and
760 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
761 * sequence to recover space from the file.
763 error = cache_vref(v->a_nch, v->a_cred, &vp);
764 KKASSERT(error == 0);
766 if (vp->v_type == VDIR) {
771 dnode = VP_TO_TMPFS_DIR(dvp);
772 node = VP_TO_TMPFS_NODE(vp);
773 tmp = VFS_TO_TMPFS(vp->v_mount);
774 de = tmpfs_dir_lookup(dnode, node, ncp);
780 /* Files marked as immutable or append-only cannot be deleted. */
781 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
782 (dnode->tn_flags & APPEND)) {
787 /* Remove the entry from the directory; as it is a file, we do not
788 * have to change the number of hard links of the directory. */
789 tmpfs_dir_detach(dvp, de);
791 /* Free the directory entry we just deleted. Note that the node
792 * referred by it will not be removed until the vnode is really
794 tmpfs_free_dirent(tmp, de);
796 if (node->tn_links > 0) {
797 TMPFS_NODE_LOCK(node);
798 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
800 TMPFS_NODE_UNLOCK(node);
803 cache_setunresolved(v->a_nch);
804 cache_setvp(v->a_nch, NULL);
805 /*cache_inval_vp(vp, CINV_DESTROY);*/
814 /* --------------------------------------------------------------------- */
817 tmpfs_nlink(struct vop_nlink_args *v)
819 struct vnode *dvp = v->a_dvp;
820 struct vnode *vp = v->a_vp;
821 struct namecache *ncp = v->a_nch->ncp;
824 struct tmpfs_dirent *de;
825 struct tmpfs_node *node;
827 KKASSERT(dvp != vp); /* XXX When can this be false? */
829 node = VP_TO_TMPFS_NODE(vp);
831 /* XXX: Why aren't the following two tests done by the caller? */
833 /* Hard links of directories are forbidden. */
834 if (vp->v_type == VDIR) {
839 /* Cannot create cross-device links. */
840 if (dvp->v_mount != vp->v_mount) {
845 /* Ensure that we do not overflow the maximum number of links imposed
847 KKASSERT(node->tn_links <= LINK_MAX);
848 if (node->tn_links == LINK_MAX) {
853 /* We cannot create links of files marked immutable or append-only. */
854 if (node->tn_flags & (IMMUTABLE | APPEND)) {
859 /* Allocate a new directory entry to represent the node. */
860 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
861 ncp->nc_name, ncp->nc_nlen, &de);
865 /* Insert the new directory entry into the appropriate directory. */
866 tmpfs_dir_attach(dvp, de);
868 /* vp link count has changed, so update node times. */
870 TMPFS_NODE_LOCK(node);
871 node->tn_status |= TMPFS_NODE_CHANGED;
872 TMPFS_NODE_UNLOCK(node);
875 cache_setunresolved(v->a_nch);
876 cache_setvp(v->a_nch, vp);
883 /* --------------------------------------------------------------------- */
886 tmpfs_nrename(struct vop_nrename_args *v)
888 struct vnode *fdvp = v->a_fdvp;
889 struct namecache *fncp = v->a_fnch->ncp;
890 struct vnode *fvp = fncp->nc_vp;
891 struct vnode *tdvp = v->a_tdvp;
892 struct namecache *tncp = v->a_tnch->ncp;
893 struct vnode *tvp = tncp->nc_vp;
897 struct tmpfs_dirent *de;
898 struct tmpfs_mount *tmp;
899 struct tmpfs_node *fdnode;
900 struct tmpfs_node *fnode;
901 struct tmpfs_node *tnode;
902 struct tmpfs_node *tdnode;
904 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
906 /* Disallow cross-device renames.
907 * XXX Why isn't this done by the caller? */
908 if (fvp->v_mount != tdvp->v_mount ||
909 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
914 tmp = VFS_TO_TMPFS(tdvp->v_mount);
915 tdnode = VP_TO_TMPFS_DIR(tdvp);
917 /* If source and target are the same file, there is nothing to do. */
923 fdnode = VP_TO_TMPFS_DIR(fdvp);
924 fnode = VP_TO_TMPFS_NODE(fvp);
925 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
927 /* Avoid manipulating '.' and '..' entries. */
932 KKASSERT(de->td_node == fnode);
934 /* If re-naming a directory to another preexisting directory
935 * ensure that the target directory is empty so that its
936 * removal causes no side effects.
937 * Kern_rename gurantees the destination to be a directory
938 * if the source is one. */
940 KKASSERT(tnode != NULL);
942 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
943 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
948 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
949 if (tnode->tn_size > 0) {
953 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
956 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
960 KKASSERT(fnode->tn_type != VDIR &&
961 tnode->tn_type != VDIR);
965 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
966 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
971 /* Ensure that we have enough memory to hold the new name, if it
972 * has to be changed. */
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, M_TMPFSNAME, M_WAITOK);
979 /* If the node is being moved to another directory, we have to do
981 if (fdnode != tdnode) {
982 /* In case we are moving a directory, we have to adjust its
983 * parent to point to the new parent. */
984 if (de->td_node->tn_type == VDIR) {
985 struct tmpfs_node *n;
987 /* Ensure the target directory is not a child of the
988 * directory being moved. Otherwise, we'd end up
989 * with stale nodes. */
991 /* TMPFS_LOCK garanties that no nodes are freed while
992 * traversing the list. Nodes can only be marked as
993 * removed: tn_parent == NULL. */
996 while (n != n->tn_dir.tn_parent) {
997 struct tmpfs_node *parent;
1000 TMPFS_NODE_UNLOCK(n);
1003 if (newname != NULL)
1004 kfree(newname, M_TMPFSNAME);
1007 parent = n->tn_dir.tn_parent;
1008 if (parent == NULL) {
1012 TMPFS_NODE_LOCK(parent);
1013 if (parent->tn_dir.tn_parent == NULL) {
1014 TMPFS_NODE_UNLOCK(parent);
1020 TMPFS_NODE_UNLOCK(n);
1024 if (newname != NULL)
1025 kfree(newname, M_TMPFSNAME);
1029 /* Adjust the parent pointer. */
1030 TMPFS_VALIDATE_DIR(fnode);
1031 TMPFS_NODE_LOCK(de->td_node);
1032 de->td_node->tn_dir.tn_parent = tdnode;
1034 /* As a result of changing the target of the '..'
1035 * entry, the link count of the source and target
1036 * directories has to be adjusted. */
1037 TMPFS_NODE_LOCK(tdnode);
1038 TMPFS_ASSERT_LOCKED(tdnode);
1039 TMPFS_NODE_LOCK(fdnode);
1040 TMPFS_ASSERT_LOCKED(fdnode);
1045 TMPFS_NODE_UNLOCK(fdnode);
1046 TMPFS_NODE_UNLOCK(tdnode);
1047 TMPFS_NODE_UNLOCK(de->td_node);
1050 /* Do the move: just remove the entry from the source directory
1051 * and insert it into the target one. */
1052 tmpfs_dir_detach(fdvp, de);
1053 tmpfs_dir_attach(tdvp, de);
1056 /* If the name has changed, we need to make it effective by changing
1057 * it in the directory entry. */
1058 if (newname != NULL) {
1060 kfree(de->td_name, M_TMPFSNAME);
1061 de->td_namelen = (uint16_t)tncp->nc_nlen;
1062 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1063 newname[tncp->nc_nlen] = '\0';
1064 de->td_name = newname;
1066 TMPFS_NODE_LOCK(tdnode);
1067 TMPFS_NODE_LOCK(fdnode);
1069 fnode->tn_status |= TMPFS_NODE_CHANGED;
1070 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1072 TMPFS_NODE_UNLOCK(fdnode);
1073 TMPFS_NODE_UNLOCK(tdnode);
1076 /* If we are overwriting an entry, we have to remove the old one
1077 * from the target directory. */
1079 /* Remove the old entry from the target directory. */
1080 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1081 tmpfs_dir_detach(tdvp, de);
1083 /* Free the directory entry we just deleted. Note that the
1084 * node referred by it will not be removed until the vnode is
1085 * really reclaimed. */
1086 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1087 /*cache_inval_vp(tvp, CINV_DESTROY);*/
1090 cache_rename(v->a_fnch, v->a_tnch);
1097 /* Release target nodes. */
1098 /* XXX: I don't understand when tdvp can be the same as tvp, but
1099 * other code takes care of this... */
1106 /* --------------------------------------------------------------------- */
1109 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1111 struct vnode *dvp = v->a_dvp;
1112 struct vnode **vpp = v->a_vpp;
1113 struct namecache *ncp = v->a_nch->ncp;
1114 struct vattr *vap = v->a_vap;
1115 struct ucred *cred = v->a_cred;
1118 KKASSERT(vap->va_type == VDIR);
1120 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1122 cache_setunresolved(v->a_nch);
1123 cache_setvp(v->a_nch, *vpp);
1129 /* --------------------------------------------------------------------- */
1132 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1134 struct vnode *dvp = v->a_dvp;
1135 struct namecache *ncp = v->a_nch->ncp;
1139 struct tmpfs_dirent *de;
1140 struct tmpfs_mount *tmp;
1141 struct tmpfs_node *dnode;
1142 struct tmpfs_node *node;
1145 * We have to acquire the vp from v->a_nch because
1146 * we will likely unresolve the namecache entry, and
1147 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1150 error = cache_vref(v->a_nch, v->a_cred, &vp);
1151 KKASSERT(error == 0);
1153 tmp = VFS_TO_TMPFS(dvp->v_mount);
1154 dnode = VP_TO_TMPFS_DIR(dvp);
1155 node = VP_TO_TMPFS_DIR(vp);
1157 /* Directories with more than two entries ('.' and '..') cannot be
1159 if (node->tn_size > 0) {
1164 if ((dnode->tn_flags & APPEND)
1165 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1170 /* This invariant holds only if we are not trying to remove "..".
1171 * We checked for that above so this is safe now. */
1172 KKASSERT(node->tn_dir.tn_parent == dnode);
1174 /* Get the directory entry associated with node (vp). This was
1175 * filled by tmpfs_lookup while looking up the entry. */
1176 de = tmpfs_dir_lookup(dnode, node, ncp);
1177 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1181 /* Check flags to see if we are allowed to remove the directory. */
1182 if (dnode->tn_flags & APPEND
1183 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1189 /* Detach the directory entry from the directory (dnode). */
1190 tmpfs_dir_detach(dvp, de);
1192 /* No vnode should be allocated for this entry from this point */
1193 TMPFS_NODE_LOCK(node);
1194 TMPFS_ASSERT_ELOCKED(node);
1195 TMPFS_NODE_LOCK(dnode);
1196 TMPFS_ASSERT_ELOCKED(dnode);
1199 /* handled by tmpfs_free_node */
1200 KKASSERT(node->tn_links > 0);
1202 node->tn_dir.tn_parent = NULL;
1204 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1205 TMPFS_NODE_MODIFIED;
1208 /* handled by tmpfs_free_node */
1209 KKASSERT(dnode->tn_links > 0);
1212 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1213 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1215 TMPFS_NODE_UNLOCK(dnode);
1216 TMPFS_NODE_UNLOCK(node);
1218 /* Free the directory entry we just deleted. Note that the node
1219 * referred by it will not be removed until the vnode is really
1221 tmpfs_free_dirent(tmp, de);
1223 /* Release the deleted vnode (will destroy the node, notify
1224 * interested parties and clean it from the cache). */
1226 TMPFS_NODE_LOCK(dnode);
1227 dnode->tn_status |= TMPFS_NODE_CHANGED;
1228 TMPFS_NODE_UNLOCK(dnode);
1231 cache_setunresolved(v->a_nch);
1232 cache_setvp(v->a_nch, NULL);
1233 /*cache_inval_vp(vp, CINV_DESTROY);*/
1242 /* --------------------------------------------------------------------- */
1245 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1247 struct vnode *dvp = v->a_dvp;
1248 struct vnode **vpp = v->a_vpp;
1249 struct namecache *ncp = v->a_nch->ncp;
1250 struct vattr *vap = v->a_vap;
1251 struct ucred *cred = v->a_cred;
1252 char *target = v->a_target;
1255 vap->va_type = VLNK;
1256 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1258 cache_setunresolved(v->a_nch);
1259 cache_setvp(v->a_nch, *vpp);
1265 /* --------------------------------------------------------------------- */
1268 tmpfs_readdir(struct vop_readdir_args *v)
1270 struct vnode *vp = v->a_vp;
1271 struct uio *uio = v->a_uio;
1272 int *eofflag = v->a_eofflag;
1273 off_t **cookies = v->a_cookies;
1274 int *ncookies = v->a_ncookies;
1279 struct tmpfs_node *node;
1281 /* This operation only makes sense on directory nodes. */
1282 if (vp->v_type != VDIR)
1285 node = VP_TO_TMPFS_DIR(vp);
1286 startoff = uio->uio_offset;
1288 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1289 error = tmpfs_dir_getdotdent(node, uio);
1295 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1296 error = tmpfs_dir_getdotdotdent(node, uio);
1302 error = tmpfs_dir_getdents(node, uio, &cnt);
1305 KKASSERT(error >= -1);
1310 if (eofflag != NULL)
1312 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1314 /* Update NFS-related variables. */
1315 if (error == 0 && cookies != NULL && ncookies != NULL) {
1317 off_t off = startoff;
1318 struct tmpfs_dirent *de = NULL;
1321 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1323 for (i = 0; i < cnt; i++) {
1324 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1325 if (off == TMPFS_DIRCOOKIE_DOT) {
1326 off = TMPFS_DIRCOOKIE_DOTDOT;
1328 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1329 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1330 } else if (de != NULL) {
1331 de = TAILQ_NEXT(de, td_entries);
1333 de = tmpfs_dir_lookupbycookie(node,
1335 KKASSERT(de != NULL);
1336 de = TAILQ_NEXT(de, td_entries);
1339 off = TMPFS_DIRCOOKIE_EOF;
1341 off = tmpfs_dircookie(de);
1344 (*cookies)[i] = off;
1346 KKASSERT(uio->uio_offset == off);
1352 /* --------------------------------------------------------------------- */
1355 tmpfs_readlink(struct vop_readlink_args *v)
1357 struct vnode *vp = v->a_vp;
1358 struct uio *uio = v->a_uio;
1361 struct tmpfs_node *node;
1363 KKASSERT(uio->uio_offset == 0);
1364 KKASSERT(vp->v_type == VLNK);
1366 node = VP_TO_TMPFS_NODE(vp);
1368 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1370 TMPFS_NODE_LOCK(node);
1371 node->tn_status |= TMPFS_NODE_ACCESSED;
1372 TMPFS_NODE_UNLOCK(node);
1377 /* --------------------------------------------------------------------- */
1380 tmpfs_inactive(struct vop_inactive_args *v)
1382 struct vnode *vp = v->a_vp;
1384 struct tmpfs_node *node;
1386 node = VP_TO_TMPFS_NODE(vp);
1389 * Get rid of unreferenced deleted vnodes sooner rather than
1390 * later so the data memory can be recovered immediately.
1392 TMPFS_NODE_LOCK(node);
1393 if (node->tn_links == 0 &&
1394 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1395 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1396 TMPFS_NODE_UNLOCK(node);
1399 TMPFS_NODE_UNLOCK(node);
1405 /* --------------------------------------------------------------------- */
1408 tmpfs_reclaim(struct vop_reclaim_args *v)
1410 struct vnode *vp = v->a_vp;
1411 struct tmpfs_mount *tmp;
1412 struct tmpfs_node *node;
1414 node = VP_TO_TMPFS_NODE(vp);
1415 tmp = VFS_TO_TMPFS(vp->v_mount);
1419 /* If the node referenced by this vnode was deleted by the user,
1420 * we must free its associated data structures (now that the vnode
1421 * is being reclaimed). */
1422 TMPFS_NODE_LOCK(node);
1423 if (node->tn_links == 0 &&
1424 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1425 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1426 tmpfs_free_node(tmp, node);
1429 TMPFS_NODE_UNLOCK(node);
1432 KKASSERT(vp->v_data == NULL);
1436 /* --------------------------------------------------------------------- */
1439 tmpfs_print(struct vop_print_args *v)
1441 struct vnode *vp = v->a_vp;
1443 struct tmpfs_node *node;
1445 node = VP_TO_TMPFS_NODE(vp);
1447 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1448 node, node->tn_flags, node->tn_links);
1449 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1450 node->tn_mode, node->tn_uid, node->tn_gid,
1451 (uintmax_t)node->tn_size, node->tn_status);
1453 if (vp->v_type == VFIFO)
1461 /* --------------------------------------------------------------------- */
1464 tmpfs_pathconf(struct vop_pathconf_args *v)
1466 int name = v->a_name;
1467 register_t *retval = v->a_retval;
1490 case _PC_CHOWN_RESTRICTED:
1502 case _PC_FILESIZEBITS:
1503 *retval = 0; /* XXX Don't know which value should I return. */
1513 /* --------------------------------------------------------------------- */
1516 * vnode operations vector used for files stored in a tmpfs file system.
1518 struct vop_ops tmpfs_vnode_vops = {
1519 .vop_default = vop_defaultop,
1520 .vop_getpages = vop_stdgetpages,
1521 .vop_putpages = vop_stdputpages,
1522 .vop_ncreate = tmpfs_ncreate,
1523 .vop_nresolve = tmpfs_nresolve,
1524 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1525 .vop_nmknod = tmpfs_nmknod,
1526 .vop_open = tmpfs_open,
1527 .vop_close = tmpfs_close,
1528 .vop_access = tmpfs_access,
1529 .vop_getattr = tmpfs_getattr,
1530 .vop_setattr = tmpfs_setattr,
1531 .vop_read = tmpfs_read,
1532 .vop_write = tmpfs_write,
1533 .vop_fsync = tmpfs_fsync,
1534 .vop_nremove = tmpfs_nremove,
1535 .vop_nlink = tmpfs_nlink,
1536 .vop_nrename = tmpfs_nrename,
1537 .vop_nmkdir = tmpfs_nmkdir,
1538 .vop_nrmdir = tmpfs_nrmdir,
1539 .vop_nsymlink = tmpfs_nsymlink,
1540 .vop_readdir = tmpfs_readdir,
1541 .vop_readlink = tmpfs_readlink,
1542 .vop_inactive = tmpfs_inactive,
1543 .vop_reclaim = tmpfs_reclaim,
1544 .vop_print = tmpfs_print,
1545 .vop_pathconf = tmpfs_pathconf,
1546 .vop_bmap = tmpfs_bmap,
1547 .vop_bmap = (void *)vop_eopnotsupp,
1548 .vop_strategy = tmpfs_strategy,
1549 .vop_advlock = tmpfs_advlock,