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>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
43 #include <sys/namei.h>
46 #include <sys/rwlock.h>
47 #include <sys/sched.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/vnode.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
60 #include <fs/tmpfs/tmpfs_vnops.h>
61 #include <fs/tmpfs/tmpfs.h>
63 SYSCTL_DECL(_vfs_tmpfs);
65 static volatile int tmpfs_rename_restarts;
66 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
67 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
68 "Times rename had to restart due to lock contention");
71 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
75 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
79 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
81 struct tmpfs_dirent *de;
82 struct tmpfs_node *dnode, *pnode;
83 struct tmpfs_mount *tm;
86 dnode = VP_TO_TMPFS_DIR(dvp);
89 /* Check accessibility of requested node as a first step. */
90 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
94 /* We cannot be requesting the parent directory of the root node. */
95 MPASS(IMPLIES(dnode->tn_type == VDIR &&
96 dnode->tn_dir.tn_parent == dnode,
97 !(cnp->cn_flags & ISDOTDOT)));
99 TMPFS_ASSERT_LOCKED(dnode);
100 if (dnode->tn_dir.tn_parent == NULL) {
104 if (cnp->cn_flags & ISDOTDOT) {
105 tm = VFS_TO_TMPFS(dvp->v_mount);
106 pnode = dnode->tn_dir.tn_parent;
107 tmpfs_ref_node(pnode);
108 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
109 pnode, cnp->cn_lkflags, vpp);
110 tmpfs_free_node(tm, pnode);
113 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
118 de = tmpfs_dir_lookup(dnode, NULL, cnp);
119 if (de != NULL && de->td_node == NULL)
120 cnp->cn_flags |= ISWHITEOUT;
121 if (de == NULL || de->td_node == NULL) {
123 * The entry was not found in the directory.
124 * This is OK if we are creating or renaming an
125 * entry and are working on the last component of
128 if ((cnp->cn_flags & ISLASTCN) &&
129 (cnp->cn_nameiop == CREATE || \
130 cnp->cn_nameiop == RENAME ||
131 (cnp->cn_nameiop == DELETE &&
132 cnp->cn_flags & DOWHITEOUT &&
133 cnp->cn_flags & ISWHITEOUT))) {
134 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
140 * Keep the component name in the buffer for
143 cnp->cn_flags |= SAVENAME;
149 struct tmpfs_node *tnode;
152 * The entry was found, so get its associated
158 * If we are not at the last path component and
159 * found a non-directory or non-link entry (which
160 * may itself be pointing to a directory), raise
163 if ((tnode->tn_type != VDIR &&
164 tnode->tn_type != VLNK) &&
165 !(cnp->cn_flags & ISLASTCN)) {
171 * If we are deleting or renaming the entry, keep
172 * track of its tmpfs_dirent so that it can be
173 * easily deleted later.
175 if ((cnp->cn_flags & ISLASTCN) &&
176 (cnp->cn_nameiop == DELETE ||
177 cnp->cn_nameiop == RENAME)) {
178 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
183 /* Allocate a new vnode on the matching entry. */
184 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
185 cnp->cn_lkflags, vpp);
189 if ((dnode->tn_mode & S_ISTXT) &&
190 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
191 cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
192 cnp->cn_cred, cnp->cn_thread)) {
198 cnp->cn_flags |= SAVENAME;
200 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
201 cnp->cn_lkflags, vpp);
209 * Store the result of this lookup in the cache. Avoid this if the
210 * request was for creation, as it does not improve timings on
213 if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
214 cache_enter(dvp, *vpp, cnp);
218 * If there were no errors, *vpp cannot be null and it must be
221 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
227 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
230 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
234 tmpfs_lookup(struct vop_lookup_args *v)
237 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
241 tmpfs_create(struct vop_create_args *v)
243 struct vnode *dvp = v->a_dvp;
244 struct vnode **vpp = v->a_vpp;
245 struct componentname *cnp = v->a_cnp;
246 struct vattr *vap = v->a_vap;
249 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
251 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
252 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
253 cache_enter(dvp, *vpp, cnp);
258 tmpfs_mknod(struct vop_mknod_args *v)
260 struct vnode *dvp = v->a_dvp;
261 struct vnode **vpp = v->a_vpp;
262 struct componentname *cnp = v->a_cnp;
263 struct vattr *vap = v->a_vap;
265 if (vap->va_type != VBLK && vap->va_type != VCHR &&
266 vap->va_type != VFIFO)
269 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
273 tmpfs_open(struct vop_open_args *v)
275 struct vnode *vp = v->a_vp;
276 int mode = v->a_mode;
279 struct tmpfs_node *node;
281 MPASS(VOP_ISLOCKED(vp));
283 node = VP_TO_TMPFS_NODE(vp);
285 /* The file is still active but all its names have been removed
286 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
287 * it is about to die. */
288 if (node->tn_links < 1)
291 /* If the file is marked append-only, deny write requests. */
292 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
296 /* For regular files, the call below is nop. */
297 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
298 OBJ_DEAD) == 0, ("dead object"));
299 vnode_create_vobject(vp, node->tn_size, v->a_td);
302 MPASS(VOP_ISLOCKED(vp));
307 tmpfs_close(struct vop_close_args *v)
309 struct vnode *vp = v->a_vp;
311 /* Update node times. */
318 tmpfs_access(struct vop_access_args *v)
320 struct vnode *vp = v->a_vp;
321 accmode_t accmode = v->a_accmode;
322 struct ucred *cred = v->a_cred;
325 struct tmpfs_node *node;
327 MPASS(VOP_ISLOCKED(vp));
329 node = VP_TO_TMPFS_NODE(vp);
331 switch (vp->v_type) {
337 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
357 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
362 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
363 node->tn_gid, accmode, cred, NULL);
366 MPASS(VOP_ISLOCKED(vp));
372 tmpfs_getattr(struct vop_getattr_args *v)
374 struct vnode *vp = v->a_vp;
375 struct vattr *vap = v->a_vap;
377 struct tmpfs_node *node;
379 node = VP_TO_TMPFS_NODE(vp);
383 vap->va_type = vp->v_type;
384 vap->va_mode = node->tn_mode;
385 vap->va_nlink = node->tn_links;
386 vap->va_uid = node->tn_uid;
387 vap->va_gid = node->tn_gid;
388 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
389 vap->va_fileid = node->tn_id;
390 vap->va_size = node->tn_size;
391 vap->va_blocksize = PAGE_SIZE;
392 vap->va_atime = node->tn_atime;
393 vap->va_mtime = node->tn_mtime;
394 vap->va_ctime = node->tn_ctime;
395 vap->va_birthtime = node->tn_birthtime;
396 vap->va_gen = node->tn_gen;
397 vap->va_flags = node->tn_flags;
398 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
399 node->tn_rdev : NODEV;
400 vap->va_bytes = round_page(node->tn_size);
407 tmpfs_setattr(struct vop_setattr_args *v)
409 struct vnode *vp = v->a_vp;
410 struct vattr *vap = v->a_vap;
411 struct ucred *cred = v->a_cred;
412 struct thread *td = curthread;
416 MPASS(VOP_ISLOCKED(vp));
420 /* Abort if any unsettable attribute is given. */
421 if (vap->va_type != VNON ||
422 vap->va_nlink != VNOVAL ||
423 vap->va_fsid != VNOVAL ||
424 vap->va_fileid != VNOVAL ||
425 vap->va_blocksize != VNOVAL ||
426 vap->va_gen != VNOVAL ||
427 vap->va_rdev != VNOVAL ||
428 vap->va_bytes != VNOVAL)
431 if (error == 0 && (vap->va_flags != VNOVAL))
432 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
434 if (error == 0 && (vap->va_size != VNOVAL))
435 error = tmpfs_chsize(vp, vap->va_size, cred, td);
437 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
438 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
440 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
441 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
443 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
444 vap->va_atime.tv_nsec != VNOVAL) ||
445 (vap->va_mtime.tv_sec != VNOVAL &&
446 vap->va_mtime.tv_nsec != VNOVAL) ||
447 (vap->va_birthtime.tv_sec != VNOVAL &&
448 vap->va_birthtime.tv_nsec != VNOVAL)))
449 error = tmpfs_chtimes(vp, vap, cred, td);
451 /* Update the node times. We give preference to the error codes
452 * generated by this function rather than the ones that may arise
453 * from tmpfs_update. */
456 MPASS(VOP_ISLOCKED(vp));
462 tmpfs_read(struct vop_read_args *v)
466 struct tmpfs_node *node;
469 if (vp->v_type != VREG)
472 if (uio->uio_offset < 0)
474 node = VP_TO_TMPFS_NODE(vp);
475 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
476 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
480 tmpfs_write(struct vop_write_args *v)
484 struct tmpfs_node *node;
490 ioflag = v->a_ioflag;
492 node = VP_TO_TMPFS_NODE(vp);
493 oldsize = node->tn_size;
495 if (uio->uio_offset < 0 || vp->v_type != VREG)
497 if (uio->uio_resid == 0)
499 if (ioflag & IO_APPEND)
500 uio->uio_offset = node->tn_size;
501 if (uio->uio_offset + uio->uio_resid >
502 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
504 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
506 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
507 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
513 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
514 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
516 if (node->tn_mode & (S_ISUID | S_ISGID)) {
517 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
518 node->tn_mode &= ~(S_ISUID | S_ISGID);
521 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
524 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
525 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
531 tmpfs_fsync(struct vop_fsync_args *v)
533 struct vnode *vp = v->a_vp;
535 MPASS(VOP_ISLOCKED(vp));
537 tmpfs_check_mtime(vp);
544 tmpfs_remove(struct vop_remove_args *v)
546 struct vnode *dvp = v->a_dvp;
547 struct vnode *vp = v->a_vp;
550 struct tmpfs_dirent *de;
551 struct tmpfs_mount *tmp;
552 struct tmpfs_node *dnode;
553 struct tmpfs_node *node;
555 MPASS(VOP_ISLOCKED(dvp));
556 MPASS(VOP_ISLOCKED(vp));
558 if (vp->v_type == VDIR) {
563 dnode = VP_TO_TMPFS_DIR(dvp);
564 node = VP_TO_TMPFS_NODE(vp);
565 tmp = VFS_TO_TMPFS(vp->v_mount);
566 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
569 /* Files marked as immutable or append-only cannot be deleted. */
570 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
571 (dnode->tn_flags & APPEND)) {
576 /* Remove the entry from the directory; as it is a file, we do not
577 * have to change the number of hard links of the directory. */
578 tmpfs_dir_detach(dvp, de);
579 if (v->a_cnp->cn_flags & DOWHITEOUT)
580 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
582 /* Free the directory entry we just deleted. Note that the node
583 * referred by it will not be removed until the vnode is really
585 tmpfs_free_dirent(tmp, de);
587 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
596 tmpfs_link(struct vop_link_args *v)
598 struct vnode *dvp = v->a_tdvp;
599 struct vnode *vp = v->a_vp;
600 struct componentname *cnp = v->a_cnp;
603 struct tmpfs_dirent *de;
604 struct tmpfs_node *node;
606 MPASS(VOP_ISLOCKED(dvp));
607 MPASS(cnp->cn_flags & HASBUF);
608 MPASS(dvp != vp); /* XXX When can this be false? */
609 node = VP_TO_TMPFS_NODE(vp);
611 /* Ensure that we do not overflow the maximum number of links imposed
613 MPASS(node->tn_links <= LINK_MAX);
614 if (node->tn_links == LINK_MAX) {
619 /* We cannot create links of files marked immutable or append-only. */
620 if (node->tn_flags & (IMMUTABLE | APPEND)) {
625 /* Allocate a new directory entry to represent the node. */
626 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
627 cnp->cn_nameptr, cnp->cn_namelen, &de);
631 /* Insert the new directory entry into the appropriate directory. */
632 if (cnp->cn_flags & ISWHITEOUT)
633 tmpfs_dir_whiteout_remove(dvp, cnp);
634 tmpfs_dir_attach(dvp, de);
636 /* vp link count has changed, so update node times. */
637 node->tn_status |= TMPFS_NODE_CHANGED;
647 * We acquire all but fdvp locks using non-blocking acquisitions. If we
648 * fail to acquire any lock in the path we will drop all held locks,
649 * acquire the new lock in a blocking fashion, and then release it and
650 * restart the rename. This acquire/release step ensures that we do not
651 * spin on a lock waiting for release. On error release all vnode locks
652 * and decrement references the way tmpfs_rename() would do.
655 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
656 struct vnode *tdvp, struct vnode **tvpp,
657 struct componentname *fcnp, struct componentname *tcnp)
661 struct tmpfs_dirent *de;
662 int error, restarts = 0;
665 if (*tvpp != NULL && *tvpp != tdvp)
666 VOP_UNLOCK(*tvpp, 0);
671 error = vn_lock(fdvp, LK_EXCLUSIVE);
674 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
676 error = vn_lock(tdvp, LK_EXCLUSIVE);
683 * Re-resolve fvp to be certain it still exists and fetch the
686 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
690 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
691 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
697 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
703 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
708 * Concurrent rename race.
721 VOP_UNLOCK(*fvpp, 0);
723 * Re-resolve tvp and acquire the vnode lock if present.
725 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
727 * If tvp disappeared we just carry on.
729 if (de == NULL && *tvpp != NULL) {
734 * Get the tvp ino if the lookup succeeded. We may have to restart
735 * if the non-blocking acquire fails.
739 error = tmpfs_alloc_vp(mp, de->td_node,
740 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
749 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
755 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
764 tmpfs_rename_restarts += restarts;
774 tmpfs_rename_restarts += restarts;
780 tmpfs_rename(struct vop_rename_args *v)
782 struct vnode *fdvp = v->a_fdvp;
783 struct vnode *fvp = v->a_fvp;
784 struct componentname *fcnp = v->a_fcnp;
785 struct vnode *tdvp = v->a_tdvp;
786 struct vnode *tvp = v->a_tvp;
787 struct componentname *tcnp = v->a_tcnp;
788 struct mount *mp = NULL;
792 struct tmpfs_dirent *de;
793 struct tmpfs_mount *tmp;
794 struct tmpfs_node *fdnode;
795 struct tmpfs_node *fnode;
796 struct tmpfs_node *tnode;
797 struct tmpfs_node *tdnode;
799 MPASS(VOP_ISLOCKED(tdvp));
800 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
801 MPASS(fcnp->cn_flags & HASBUF);
802 MPASS(tcnp->cn_flags & HASBUF);
804 /* Disallow cross-device renames.
805 * XXX Why isn't this done by the caller? */
806 if (fvp->v_mount != tdvp->v_mount ||
807 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
812 /* If source and target are the same file, there is nothing to do. */
818 /* If we need to move the directory between entries, lock the
819 * source so that we can safely operate on it. */
820 if (fdvp != tdvp && fdvp != tvp) {
821 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
823 error = vfs_busy(mp, 0);
828 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
834 ASSERT_VOP_ELOCKED(fdvp,
835 "tmpfs_rename: fdvp not locked");
836 ASSERT_VOP_ELOCKED(tdvp,
837 "tmpfs_rename: tdvp not locked");
839 ASSERT_VOP_ELOCKED(tvp,
840 "tmpfs_rename: tvp not locked");
848 tmp = VFS_TO_TMPFS(tdvp->v_mount);
849 tdnode = VP_TO_TMPFS_DIR(tdvp);
850 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
851 fdnode = VP_TO_TMPFS_DIR(fdvp);
852 fnode = VP_TO_TMPFS_NODE(fvp);
853 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
855 /* Entry can disappear before we lock fdvp,
856 * also avoid manipulating '.' and '..' entries. */
858 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
859 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
865 MPASS(de->td_node == fnode);
867 /* If re-naming a directory to another preexisting directory
868 * ensure that the target directory is empty so that its
869 * removal causes no side effects.
870 * Kern_rename gurantees the destination to be a directory
871 * if the source is one. */
873 MPASS(tnode != NULL);
875 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
876 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
881 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
882 if (tnode->tn_size > 0) {
886 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
889 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
893 MPASS(fnode->tn_type != VDIR &&
894 tnode->tn_type != VDIR);
898 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
899 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
904 /* Ensure that we have enough memory to hold the new name, if it
905 * has to be changed. */
906 if (fcnp->cn_namelen != tcnp->cn_namelen ||
907 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
908 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
912 /* If the node is being moved to another directory, we have to do
914 if (fdnode != tdnode) {
915 /* In case we are moving a directory, we have to adjust its
916 * parent to point to the new parent. */
917 if (de->td_node->tn_type == VDIR) {
918 struct tmpfs_node *n;
920 /* Ensure the target directory is not a child of the
921 * directory being moved. Otherwise, we'd end up
922 * with stale nodes. */
924 /* TMPFS_LOCK garanties that no nodes are freed while
925 * traversing the list. Nodes can only be marked as
926 * removed: tn_parent == NULL. */
929 while (n != n->tn_dir.tn_parent) {
930 struct tmpfs_node *parent;
933 TMPFS_NODE_UNLOCK(n);
937 free(newname, M_TMPFSNAME);
940 parent = n->tn_dir.tn_parent;
941 TMPFS_NODE_UNLOCK(n);
942 if (parent == NULL) {
946 TMPFS_NODE_LOCK(parent);
947 if (parent->tn_dir.tn_parent == NULL) {
948 TMPFS_NODE_UNLOCK(parent);
958 free(newname, M_TMPFSNAME);
961 TMPFS_NODE_UNLOCK(n);
963 /* Adjust the parent pointer. */
964 TMPFS_VALIDATE_DIR(fnode);
965 TMPFS_NODE_LOCK(de->td_node);
966 de->td_node->tn_dir.tn_parent = tdnode;
967 TMPFS_NODE_UNLOCK(de->td_node);
969 /* As a result of changing the target of the '..'
970 * entry, the link count of the source and target
971 * directories has to be adjusted. */
972 TMPFS_NODE_LOCK(tdnode);
973 TMPFS_ASSERT_LOCKED(tdnode);
975 TMPFS_NODE_UNLOCK(tdnode);
977 TMPFS_NODE_LOCK(fdnode);
978 TMPFS_ASSERT_LOCKED(fdnode);
980 TMPFS_NODE_UNLOCK(fdnode);
984 /* Do the move: just remove the entry from the source directory
985 * and insert it into the target one. */
986 tmpfs_dir_detach(fdvp, de);
988 if (fcnp->cn_flags & DOWHITEOUT)
989 tmpfs_dir_whiteout_add(fdvp, fcnp);
990 if (tcnp->cn_flags & ISWHITEOUT)
991 tmpfs_dir_whiteout_remove(tdvp, tcnp);
993 /* If the name has changed, we need to make it effective by changing
994 * it in the directory entry. */
995 if (newname != NULL) {
996 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
998 free(de->ud.td_name, M_TMPFSNAME);
999 de->ud.td_name = newname;
1000 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1002 fnode->tn_status |= TMPFS_NODE_CHANGED;
1003 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1006 /* If we are overwriting an entry, we have to remove the old one
1007 * from the target directory. */
1009 struct tmpfs_dirent *tde;
1011 /* Remove the old entry from the target directory. */
1012 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1013 tmpfs_dir_detach(tdvp, tde);
1015 /* Free the directory entry we just deleted. Note that the
1016 * node referred by it will not be removed until the vnode is
1017 * really reclaimed. */
1018 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1021 tmpfs_dir_attach(tdvp, de);
1023 if (tmpfs_use_nc(fvp)) {
1027 cache_purge_negative(tdvp);
1033 if (fdvp != tdvp && fdvp != tvp)
1034 VOP_UNLOCK(fdvp, 0);
1037 /* Release target nodes. */
1038 /* XXX: I don't understand when tdvp can be the same as tvp, but
1039 * other code takes care of this... */
1047 /* Release source nodes. */
1058 tmpfs_mkdir(struct vop_mkdir_args *v)
1060 struct vnode *dvp = v->a_dvp;
1061 struct vnode **vpp = v->a_vpp;
1062 struct componentname *cnp = v->a_cnp;
1063 struct vattr *vap = v->a_vap;
1065 MPASS(vap->va_type == VDIR);
1067 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1071 tmpfs_rmdir(struct vop_rmdir_args *v)
1073 struct vnode *dvp = v->a_dvp;
1074 struct vnode *vp = v->a_vp;
1077 struct tmpfs_dirent *de;
1078 struct tmpfs_mount *tmp;
1079 struct tmpfs_node *dnode;
1080 struct tmpfs_node *node;
1082 MPASS(VOP_ISLOCKED(dvp));
1083 MPASS(VOP_ISLOCKED(vp));
1085 tmp = VFS_TO_TMPFS(dvp->v_mount);
1086 dnode = VP_TO_TMPFS_DIR(dvp);
1087 node = VP_TO_TMPFS_DIR(vp);
1089 /* Directories with more than two entries ('.' and '..') cannot be
1091 if (node->tn_size > 0) {
1096 if ((dnode->tn_flags & APPEND)
1097 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1102 /* This invariant holds only if we are not trying to remove "..".
1103 * We checked for that above so this is safe now. */
1104 MPASS(node->tn_dir.tn_parent == dnode);
1106 /* Get the directory entry associated with node (vp). This was
1107 * filled by tmpfs_lookup while looking up the entry. */
1108 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1109 MPASS(TMPFS_DIRENT_MATCHES(de,
1110 v->a_cnp->cn_nameptr,
1111 v->a_cnp->cn_namelen));
1113 /* Check flags to see if we are allowed to remove the directory. */
1114 if ((dnode->tn_flags & APPEND) != 0 ||
1115 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1121 /* Detach the directory entry from the directory (dnode). */
1122 tmpfs_dir_detach(dvp, de);
1123 if (v->a_cnp->cn_flags & DOWHITEOUT)
1124 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1126 /* No vnode should be allocated for this entry from this point */
1127 TMPFS_NODE_LOCK(node);
1129 node->tn_dir.tn_parent = NULL;
1130 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1131 TMPFS_NODE_MODIFIED;
1133 TMPFS_NODE_UNLOCK(node);
1135 TMPFS_NODE_LOCK(dnode);
1137 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1138 TMPFS_NODE_MODIFIED;
1139 TMPFS_NODE_UNLOCK(dnode);
1141 if (tmpfs_use_nc(dvp)) {
1146 /* Free the directory entry we just deleted. Note that the node
1147 * referred by it will not be removed until the vnode is really
1149 tmpfs_free_dirent(tmp, de);
1151 /* Release the deleted vnode (will destroy the node, notify
1152 * interested parties and clean it from the cache). */
1154 dnode->tn_status |= TMPFS_NODE_CHANGED;
1164 tmpfs_symlink(struct vop_symlink_args *v)
1166 struct vnode *dvp = v->a_dvp;
1167 struct vnode **vpp = v->a_vpp;
1168 struct componentname *cnp = v->a_cnp;
1169 struct vattr *vap = v->a_vap;
1170 char *target = v->a_target;
1172 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1173 MPASS(vap->va_type == VLNK);
1175 vap->va_type = VLNK;
1178 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1182 tmpfs_readdir(struct vop_readdir_args *v)
1184 struct vnode *vp = v->a_vp;
1185 struct uio *uio = v->a_uio;
1186 int *eofflag = v->a_eofflag;
1187 u_long **cookies = v->a_cookies;
1188 int *ncookies = v->a_ncookies;
1193 struct tmpfs_node *node;
1195 /* This operation only makes sense on directory nodes. */
1196 if (vp->v_type != VDIR)
1200 node = VP_TO_TMPFS_DIR(vp);
1202 startresid = uio->uio_resid;
1204 /* Allocate cookies for NFS and compat modules. */
1205 if (cookies != NULL && ncookies != NULL) {
1206 maxcookies = howmany(node->tn_size,
1207 sizeof(struct tmpfs_dirent)) + 2;
1208 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1213 if (cookies == NULL)
1214 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1216 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1219 /* Buffer was filled without hitting EOF. */
1220 if (error == EJUSTRETURN)
1221 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1223 if (error != 0 && cookies != NULL && ncookies != NULL) {
1224 free(*cookies, M_TEMP);
1229 if (eofflag != NULL)
1231 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1237 tmpfs_readlink(struct vop_readlink_args *v)
1239 struct vnode *vp = v->a_vp;
1240 struct uio *uio = v->a_uio;
1243 struct tmpfs_node *node;
1245 MPASS(uio->uio_offset == 0);
1246 MPASS(vp->v_type == VLNK);
1248 node = VP_TO_TMPFS_NODE(vp);
1250 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1252 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
1258 tmpfs_inactive(struct vop_inactive_args *v)
1261 struct tmpfs_node *node;
1264 node = VP_TO_TMPFS_NODE(vp);
1265 if (node->tn_links == 0)
1268 tmpfs_check_mtime(vp);
1273 tmpfs_reclaim(struct vop_reclaim_args *v)
1275 struct vnode *vp = v->a_vp;
1277 struct tmpfs_mount *tmp;
1278 struct tmpfs_node *node;
1280 node = VP_TO_TMPFS_NODE(vp);
1281 tmp = VFS_TO_TMPFS(vp->v_mount);
1283 if (vp->v_type == VREG)
1284 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1286 vnode_destroy_vobject(vp);
1287 vp->v_object = NULL;
1288 if (tmpfs_use_nc(vp))
1291 TMPFS_NODE_LOCK(node);
1294 /* If the node referenced by this vnode was deleted by the user,
1295 * we must free its associated data structures (now that the vnode
1296 * is being reclaimed). */
1297 if (node->tn_links == 0 &&
1298 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1299 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1300 TMPFS_NODE_UNLOCK(node);
1301 tmpfs_free_node(tmp, node);
1303 TMPFS_NODE_UNLOCK(node);
1305 MPASS(vp->v_data == NULL);
1310 tmpfs_print(struct vop_print_args *v)
1312 struct vnode *vp = v->a_vp;
1314 struct tmpfs_node *node;
1316 node = VP_TO_TMPFS_NODE(vp);
1318 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1319 node, node->tn_flags, node->tn_links);
1320 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1321 node->tn_mode, node->tn_uid, node->tn_gid,
1322 (intmax_t)node->tn_size, node->tn_status);
1324 if (vp->v_type == VFIFO)
1333 tmpfs_pathconf(struct vop_pathconf_args *v)
1335 int name = v->a_name;
1336 register_t *retval = v->a_retval;
1359 case _PC_CHOWN_RESTRICTED:
1371 case _PC_FILESIZEBITS:
1372 *retval = 0; /* XXX Don't know which value should I return. */
1383 tmpfs_vptofh(struct vop_vptofh_args *ap)
1385 struct tmpfs_fid *tfhp;
1386 struct tmpfs_node *node;
1388 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1389 node = VP_TO_TMPFS_NODE(ap->a_vp);
1391 tfhp->tf_len = sizeof(struct tmpfs_fid);
1392 tfhp->tf_id = node->tn_id;
1393 tfhp->tf_gen = node->tn_gen;
1399 tmpfs_whiteout(struct vop_whiteout_args *ap)
1401 struct vnode *dvp = ap->a_dvp;
1402 struct componentname *cnp = ap->a_cnp;
1403 struct tmpfs_dirent *de;
1405 switch (ap->a_flags) {
1409 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1411 return (de->td_node == NULL ? 0 : EEXIST);
1412 return (tmpfs_dir_whiteout_add(dvp, cnp));
1414 tmpfs_dir_whiteout_remove(dvp, cnp);
1417 panic("tmpfs_whiteout: unknown op");
1422 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1423 struct tmpfs_dirent **pde)
1425 struct tmpfs_dir_cursor dc;
1426 struct tmpfs_dirent *de;
1428 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1429 de = tmpfs_dir_next(tnp, &dc)) {
1430 if (de->td_node == tn) {
1439 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1440 struct tmpfs_node *tnp, char *buf, int *buflen, struct vnode **dvp)
1442 struct tmpfs_dirent *de;
1445 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1449 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1452 i -= de->td_namelen;
1456 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1462 VOP_UNLOCK(*dvp, 0);
1473 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1475 struct vnode *vp, **dvp;
1476 struct tmpfs_node *tn, *tnp, *tnp1;
1477 struct tmpfs_dirent *de;
1478 struct tmpfs_mount *tm;
1486 buflen = ap->a_buflen;
1488 tm = VFS_TO_TMPFS(vp->v_mount);
1489 tn = VP_TO_TMPFS_NODE(vp);
1490 if (tn->tn_type == VDIR) {
1491 tnp = tn->tn_dir.tn_parent;
1494 tmpfs_ref_node(tnp);
1495 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1497 tmpfs_free_node(tm, tnp);
1502 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1503 if (tnp->tn_type != VDIR)
1505 TMPFS_NODE_LOCK(tnp);
1506 tmpfs_ref_node_locked(tnp);
1509 * tn_vnode cannot be instantiated while we hold the
1510 * node lock, so the directory cannot be changed while
1511 * we iterate over it. Do this to avoid instantiating
1512 * vnode for directories which cannot point to our
1515 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1519 TMPFS_NODE_UNLOCK(tnp);
1521 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1524 tmpfs_free_node(tm, tnp);
1527 if ((vp->v_iflag & VI_DOOMED) != 0) {
1528 tmpfs_free_node(tm, tnp);
1532 TMPFS_NODE_LOCK(tnp);
1534 if (tmpfs_free_node_locked(tm, tnp, false)) {
1537 KASSERT(tnp->tn_refcount > 0,
1538 ("node %p refcount zero", tnp));
1539 tnp1 = LIST_NEXT(tnp, tn_entries);
1540 TMPFS_NODE_UNLOCK(tnp);
1548 * Vnode operations vector used for files stored in a tmpfs file system.
1550 struct vop_vector tmpfs_vnodeop_entries = {
1551 .vop_default = &default_vnodeops,
1552 .vop_lookup = vfs_cache_lookup,
1553 .vop_cachedlookup = tmpfs_cached_lookup,
1554 .vop_create = tmpfs_create,
1555 .vop_mknod = tmpfs_mknod,
1556 .vop_open = tmpfs_open,
1557 .vop_close = tmpfs_close,
1558 .vop_access = tmpfs_access,
1559 .vop_getattr = tmpfs_getattr,
1560 .vop_setattr = tmpfs_setattr,
1561 .vop_read = tmpfs_read,
1562 .vop_write = tmpfs_write,
1563 .vop_fsync = tmpfs_fsync,
1564 .vop_remove = tmpfs_remove,
1565 .vop_link = tmpfs_link,
1566 .vop_rename = tmpfs_rename,
1567 .vop_mkdir = tmpfs_mkdir,
1568 .vop_rmdir = tmpfs_rmdir,
1569 .vop_symlink = tmpfs_symlink,
1570 .vop_readdir = tmpfs_readdir,
1571 .vop_readlink = tmpfs_readlink,
1572 .vop_inactive = tmpfs_inactive,
1573 .vop_reclaim = tmpfs_reclaim,
1574 .vop_print = tmpfs_print,
1575 .vop_pathconf = tmpfs_pathconf,
1576 .vop_vptofh = tmpfs_vptofh,
1577 .vop_whiteout = tmpfs_whiteout,
1578 .vop_bmap = VOP_EOPNOTSUPP,
1579 .vop_vptocnp = tmpfs_vptocnp,
1583 * Same vector for mounts which do not use namecache.
1585 struct vop_vector tmpfs_vnodeop_nonc_entries = {
1586 .vop_default = &tmpfs_vnodeop_entries,
1587 .vop_lookup = tmpfs_lookup,