1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * tmpfs vnode interface.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/fcntl.h>
45 #include <sys/limits.h>
46 #include <sys/lockf.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
52 #include <sys/rwlock.h>
53 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
60 #include <vm/vm_param.h>
61 #include <vm/vm_object.h>
63 #include <fs/tmpfs/tmpfs_vnops.h>
64 #include <fs/tmpfs/tmpfs.h>
66 SYSCTL_DECL(_vfs_tmpfs);
68 static volatile int tmpfs_rename_restarts;
69 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
70 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
71 "Times rename had to restart due to lock contention");
74 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
78 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
82 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
84 struct tmpfs_dirent *de;
85 struct tmpfs_node *dnode, *pnode;
86 struct tmpfs_mount *tm;
89 dnode = VP_TO_TMPFS_DIR(dvp);
92 /* Check accessibility of requested node as a first step. */
93 error = vn_dir_check_exec(dvp, cnp);
97 /* We cannot be requesting the parent directory of the root node. */
98 MPASS(IMPLIES(dnode->tn_type == VDIR &&
99 dnode->tn_dir.tn_parent == dnode,
100 !(cnp->cn_flags & ISDOTDOT)));
102 TMPFS_ASSERT_LOCKED(dnode);
103 if (dnode->tn_dir.tn_parent == NULL) {
107 if (cnp->cn_flags & ISDOTDOT) {
108 tm = VFS_TO_TMPFS(dvp->v_mount);
109 pnode = dnode->tn_dir.tn_parent;
110 tmpfs_ref_node(pnode);
111 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
112 pnode, cnp->cn_lkflags, vpp);
113 tmpfs_free_node(tm, pnode);
116 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
121 de = tmpfs_dir_lookup(dnode, NULL, cnp);
122 if (de != NULL && de->td_node == NULL)
123 cnp->cn_flags |= ISWHITEOUT;
124 if (de == NULL || de->td_node == NULL) {
126 * The entry was not found in the directory.
127 * This is OK if we are creating or renaming an
128 * entry and are working on the last component of
131 if ((cnp->cn_flags & ISLASTCN) &&
132 (cnp->cn_nameiop == CREATE || \
133 cnp->cn_nameiop == RENAME ||
134 (cnp->cn_nameiop == DELETE &&
135 cnp->cn_flags & DOWHITEOUT &&
136 cnp->cn_flags & ISWHITEOUT))) {
137 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
143 * Keep the component name in the buffer for
146 cnp->cn_flags |= SAVENAME;
152 struct tmpfs_node *tnode;
155 * The entry was found, so get its associated
161 * If we are not at the last path component and
162 * found a non-directory or non-link entry (which
163 * may itself be pointing to a directory), raise
166 if ((tnode->tn_type != VDIR &&
167 tnode->tn_type != VLNK) &&
168 !(cnp->cn_flags & ISLASTCN)) {
174 * If we are deleting or renaming the entry, keep
175 * track of its tmpfs_dirent so that it can be
176 * easily deleted later.
178 if ((cnp->cn_flags & ISLASTCN) &&
179 (cnp->cn_nameiop == DELETE ||
180 cnp->cn_nameiop == RENAME)) {
181 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
186 /* Allocate a new vnode on the matching entry. */
187 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
188 cnp->cn_lkflags, vpp);
192 if ((dnode->tn_mode & S_ISTXT) &&
193 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
194 cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
195 cnp->cn_cred, cnp->cn_thread)) {
201 cnp->cn_flags |= SAVENAME;
203 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
204 cnp->cn_lkflags, vpp);
212 * Store the result of this lookup in the cache. Avoid this if the
213 * request was for creation, as it does not improve timings on
216 if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
217 cache_enter(dvp, *vpp, cnp);
221 * If there were no errors, *vpp cannot be null and it must be
224 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
230 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
233 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
237 tmpfs_lookup(struct vop_lookup_args *v)
240 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
244 tmpfs_create(struct vop_create_args *v)
246 struct vnode *dvp = v->a_dvp;
247 struct vnode **vpp = v->a_vpp;
248 struct componentname *cnp = v->a_cnp;
249 struct vattr *vap = v->a_vap;
252 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
254 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
255 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
256 cache_enter(dvp, *vpp, cnp);
261 tmpfs_mknod(struct vop_mknod_args *v)
263 struct vnode *dvp = v->a_dvp;
264 struct vnode **vpp = v->a_vpp;
265 struct componentname *cnp = v->a_cnp;
266 struct vattr *vap = v->a_vap;
268 if (vap->va_type != VBLK && vap->va_type != VCHR &&
269 vap->va_type != VFIFO)
272 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
276 tmpfs_open(struct vop_open_args *v)
278 struct vnode *vp = v->a_vp;
279 int mode = v->a_mode;
282 struct tmpfs_node *node;
284 MPASS(VOP_ISLOCKED(vp));
286 node = VP_TO_TMPFS_NODE(vp);
288 /* The file is still active but all its names have been removed
289 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
290 * it is about to die. */
291 if (node->tn_links < 1)
294 /* If the file is marked append-only, deny write requests. */
295 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
299 /* For regular files, the call below is nop. */
300 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
301 OBJ_DEAD) == 0, ("dead object"));
302 vnode_create_vobject(vp, node->tn_size, v->a_td);
305 MPASS(VOP_ISLOCKED(vp));
310 tmpfs_close(struct vop_close_args *v)
312 struct vnode *vp = v->a_vp;
314 /* Update node times. */
321 tmpfs_access(struct vop_access_args *v)
323 struct vnode *vp = v->a_vp;
324 accmode_t accmode = v->a_accmode;
325 struct ucred *cred = v->a_cred;
326 mode_t all_x = S_IXUSR | S_IXGRP | S_IXOTH;
328 struct tmpfs_node *node;
330 MPASS(VOP_ISLOCKED(vp));
332 node = VP_TO_TMPFS_NODE(vp);
335 * Common case path lookup.
337 if (__predict_true(accmode == VEXEC && (node->tn_mode & all_x) == all_x))
340 switch (vp->v_type) {
346 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
366 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
371 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
372 node->tn_gid, accmode, cred, NULL);
375 MPASS(VOP_ISLOCKED(vp));
381 tmpfs_getattr(struct vop_getattr_args *v)
383 struct vnode *vp = v->a_vp;
384 struct vattr *vap = v->a_vap;
386 struct tmpfs_node *node;
388 node = VP_TO_TMPFS_NODE(vp);
390 tmpfs_update_getattr(vp);
392 vap->va_type = vp->v_type;
393 vap->va_mode = node->tn_mode;
394 vap->va_nlink = node->tn_links;
395 vap->va_uid = node->tn_uid;
396 vap->va_gid = node->tn_gid;
397 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
398 vap->va_fileid = node->tn_id;
399 vap->va_size = node->tn_size;
400 vap->va_blocksize = PAGE_SIZE;
401 vap->va_atime = node->tn_atime;
402 vap->va_mtime = node->tn_mtime;
403 vap->va_ctime = node->tn_ctime;
404 vap->va_birthtime = node->tn_birthtime;
405 vap->va_gen = node->tn_gen;
406 vap->va_flags = node->tn_flags;
407 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
408 node->tn_rdev : NODEV;
409 if (vp->v_type == VREG) {
410 obj = node->tn_reg.tn_aobj;
411 vap->va_bytes = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
413 vap->va_bytes = node->tn_size;
420 tmpfs_setattr(struct vop_setattr_args *v)
422 struct vnode *vp = v->a_vp;
423 struct vattr *vap = v->a_vap;
424 struct ucred *cred = v->a_cred;
425 struct thread *td = curthread;
429 MPASS(VOP_ISLOCKED(vp));
433 /* Abort if any unsettable attribute is given. */
434 if (vap->va_type != VNON ||
435 vap->va_nlink != VNOVAL ||
436 vap->va_fsid != VNOVAL ||
437 vap->va_fileid != VNOVAL ||
438 vap->va_blocksize != VNOVAL ||
439 vap->va_gen != VNOVAL ||
440 vap->va_rdev != VNOVAL ||
441 vap->va_bytes != VNOVAL)
444 if (error == 0 && (vap->va_flags != VNOVAL))
445 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
447 if (error == 0 && (vap->va_size != VNOVAL))
448 error = tmpfs_chsize(vp, vap->va_size, cred, td);
450 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
451 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
453 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
454 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
456 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
457 vap->va_atime.tv_nsec != VNOVAL) ||
458 (vap->va_mtime.tv_sec != VNOVAL &&
459 vap->va_mtime.tv_nsec != VNOVAL) ||
460 (vap->va_birthtime.tv_sec != VNOVAL &&
461 vap->va_birthtime.tv_nsec != VNOVAL)))
462 error = tmpfs_chtimes(vp, vap, cred, td);
464 /* Update the node times. We give preference to the error codes
465 * generated by this function rather than the ones that may arise
466 * from tmpfs_update. */
469 MPASS(VOP_ISLOCKED(vp));
475 tmpfs_read(struct vop_read_args *v)
479 struct tmpfs_node *node;
482 if (vp->v_type != VREG)
485 if (uio->uio_offset < 0)
487 node = VP_TO_TMPFS_NODE(vp);
488 tmpfs_set_status(VFS_TO_TMPFS(vp->v_mount), node, TMPFS_NODE_ACCESSED);
489 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
493 tmpfs_write(struct vop_write_args *v)
497 struct tmpfs_node *node;
503 ioflag = v->a_ioflag;
505 node = VP_TO_TMPFS_NODE(vp);
506 oldsize = node->tn_size;
508 if (uio->uio_offset < 0 || vp->v_type != VREG)
510 if (uio->uio_resid == 0)
512 if (ioflag & IO_APPEND)
513 uio->uio_offset = node->tn_size;
514 if (uio->uio_offset + uio->uio_resid >
515 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
517 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
519 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
520 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
526 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
527 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
529 if (node->tn_mode & (S_ISUID | S_ISGID)) {
530 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID))
531 node->tn_mode &= ~(S_ISUID | S_ISGID);
534 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
537 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
538 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
544 tmpfs_fsync(struct vop_fsync_args *v)
546 struct vnode *vp = v->a_vp;
548 MPASS(VOP_ISLOCKED(vp));
550 tmpfs_check_mtime(vp);
557 tmpfs_remove(struct vop_remove_args *v)
559 struct vnode *dvp = v->a_dvp;
560 struct vnode *vp = v->a_vp;
563 struct tmpfs_dirent *de;
564 struct tmpfs_mount *tmp;
565 struct tmpfs_node *dnode;
566 struct tmpfs_node *node;
568 MPASS(VOP_ISLOCKED(dvp));
569 MPASS(VOP_ISLOCKED(vp));
571 if (vp->v_type == VDIR) {
576 dnode = VP_TO_TMPFS_DIR(dvp);
577 node = VP_TO_TMPFS_NODE(vp);
578 tmp = VFS_TO_TMPFS(vp->v_mount);
579 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
582 /* Files marked as immutable or append-only cannot be deleted. */
583 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
584 (dnode->tn_flags & APPEND)) {
589 /* Remove the entry from the directory; as it is a file, we do not
590 * have to change the number of hard links of the directory. */
591 tmpfs_dir_detach(dvp, de);
592 if (v->a_cnp->cn_flags & DOWHITEOUT)
593 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
595 /* Free the directory entry we just deleted. Note that the node
596 * referred by it will not be removed until the vnode is really
598 tmpfs_free_dirent(tmp, de);
600 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
609 tmpfs_link(struct vop_link_args *v)
611 struct vnode *dvp = v->a_tdvp;
612 struct vnode *vp = v->a_vp;
613 struct componentname *cnp = v->a_cnp;
616 struct tmpfs_dirent *de;
617 struct tmpfs_node *node;
619 MPASS(VOP_ISLOCKED(dvp));
620 MPASS(cnp->cn_flags & HASBUF);
621 MPASS(dvp != vp); /* XXX When can this be false? */
622 node = VP_TO_TMPFS_NODE(vp);
624 /* Ensure that we do not overflow the maximum number of links imposed
626 MPASS(node->tn_links <= TMPFS_LINK_MAX);
627 if (node->tn_links == TMPFS_LINK_MAX) {
632 /* We cannot create links of files marked immutable or append-only. */
633 if (node->tn_flags & (IMMUTABLE | APPEND)) {
638 /* Allocate a new directory entry to represent the node. */
639 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
640 cnp->cn_nameptr, cnp->cn_namelen, &de);
644 /* Insert the new directory entry into the appropriate directory. */
645 if (cnp->cn_flags & ISWHITEOUT)
646 tmpfs_dir_whiteout_remove(dvp, cnp);
647 tmpfs_dir_attach(dvp, de);
649 /* vp link count has changed, so update node times. */
650 node->tn_status |= TMPFS_NODE_CHANGED;
660 * We acquire all but fdvp locks using non-blocking acquisitions. If we
661 * fail to acquire any lock in the path we will drop all held locks,
662 * acquire the new lock in a blocking fashion, and then release it and
663 * restart the rename. This acquire/release step ensures that we do not
664 * spin on a lock waiting for release. On error release all vnode locks
665 * and decrement references the way tmpfs_rename() would do.
668 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
669 struct vnode *tdvp, struct vnode **tvpp,
670 struct componentname *fcnp, struct componentname *tcnp)
674 struct tmpfs_dirent *de;
675 int error, restarts = 0;
678 if (*tvpp != NULL && *tvpp != tdvp)
684 error = vn_lock(fdvp, LK_EXCLUSIVE);
687 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
689 error = vn_lock(tdvp, LK_EXCLUSIVE);
696 * Re-resolve fvp to be certain it still exists and fetch the
699 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
703 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
704 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
710 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
716 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
721 * Concurrent rename race.
736 * Re-resolve tvp and acquire the vnode lock if present.
738 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
740 * If tvp disappeared we just carry on.
742 if (de == NULL && *tvpp != NULL) {
747 * Get the tvp ino if the lookup succeeded. We may have to restart
748 * if the non-blocking acquire fails.
752 error = tmpfs_alloc_vp(mp, de->td_node,
753 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
762 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
768 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
777 tmpfs_rename_restarts += restarts;
787 tmpfs_rename_restarts += restarts;
793 tmpfs_rename(struct vop_rename_args *v)
795 struct vnode *fdvp = v->a_fdvp;
796 struct vnode *fvp = v->a_fvp;
797 struct componentname *fcnp = v->a_fcnp;
798 struct vnode *tdvp = v->a_tdvp;
799 struct vnode *tvp = v->a_tvp;
800 struct componentname *tcnp = v->a_tcnp;
802 struct tmpfs_dirent *de;
803 struct tmpfs_mount *tmp;
804 struct tmpfs_node *fdnode;
805 struct tmpfs_node *fnode;
806 struct tmpfs_node *tnode;
807 struct tmpfs_node *tdnode;
810 MPASS(VOP_ISLOCKED(tdvp));
811 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
812 MPASS(fcnp->cn_flags & HASBUF);
813 MPASS(tcnp->cn_flags & HASBUF);
816 * Disallow cross-device renames.
817 * XXX Why isn't this done by the caller?
819 if (fvp->v_mount != tdvp->v_mount ||
820 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
825 /* If source and target are the same file, there is nothing to do. */
832 * If we need to move the directory between entries, lock the
833 * source so that we can safely operate on it.
835 if (fdvp != tdvp && fdvp != tvp) {
836 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
837 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
841 ASSERT_VOP_ELOCKED(fdvp,
842 "tmpfs_rename: fdvp not locked");
843 ASSERT_VOP_ELOCKED(tdvp,
844 "tmpfs_rename: tdvp not locked");
846 ASSERT_VOP_ELOCKED(tvp,
847 "tmpfs_rename: tvp not locked");
855 tmp = VFS_TO_TMPFS(tdvp->v_mount);
856 tdnode = VP_TO_TMPFS_DIR(tdvp);
857 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
858 fdnode = VP_TO_TMPFS_DIR(fdvp);
859 fnode = VP_TO_TMPFS_NODE(fvp);
860 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
863 * Entry can disappear before we lock fdvp,
864 * also avoid manipulating '.' and '..' entries.
867 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
868 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
874 MPASS(de->td_node == fnode);
877 * If re-naming a directory to another preexisting directory
878 * ensure that the target directory is empty so that its
879 * removal causes no side effects.
880 * Kern_rename guarantees the destination to be a directory
881 * if the source is one.
884 MPASS(tnode != NULL);
886 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
887 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
892 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
893 if (tnode->tn_size > 0) {
897 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
900 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
904 MPASS(fnode->tn_type != VDIR &&
905 tnode->tn_type != VDIR);
909 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
910 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
916 * Ensure that we have enough memory to hold the new name, if it
919 if (fcnp->cn_namelen != tcnp->cn_namelen ||
920 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
921 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
926 * If the node is being moved to another directory, we have to do
929 if (fdnode != tdnode) {
931 * In case we are moving a directory, we have to adjust its
932 * parent to point to the new parent.
934 if (de->td_node->tn_type == VDIR) {
935 struct tmpfs_node *n;
938 * Ensure the target directory is not a child of the
939 * directory being moved. Otherwise, we'd end up
944 * TMPFS_LOCK guaranties that no nodes are freed while
945 * traversing the list. Nodes can only be marked as
946 * removed: tn_parent == NULL.
950 while (n != n->tn_dir.tn_parent) {
951 struct tmpfs_node *parent;
954 TMPFS_NODE_UNLOCK(n);
958 free(newname, M_TMPFSNAME);
961 parent = n->tn_dir.tn_parent;
962 TMPFS_NODE_UNLOCK(n);
963 if (parent == NULL) {
967 TMPFS_NODE_LOCK(parent);
968 if (parent->tn_dir.tn_parent == NULL) {
969 TMPFS_NODE_UNLOCK(parent);
979 free(newname, M_TMPFSNAME);
982 TMPFS_NODE_UNLOCK(n);
984 /* Adjust the parent pointer. */
985 TMPFS_VALIDATE_DIR(fnode);
986 TMPFS_NODE_LOCK(de->td_node);
987 de->td_node->tn_dir.tn_parent = tdnode;
988 TMPFS_NODE_UNLOCK(de->td_node);
991 * As a result of changing the target of the '..'
992 * entry, the link count of the source and target
993 * directories has to be adjusted.
995 TMPFS_NODE_LOCK(tdnode);
996 TMPFS_ASSERT_LOCKED(tdnode);
998 TMPFS_NODE_UNLOCK(tdnode);
1000 TMPFS_NODE_LOCK(fdnode);
1001 TMPFS_ASSERT_LOCKED(fdnode);
1003 TMPFS_NODE_UNLOCK(fdnode);
1008 * Do the move: just remove the entry from the source directory
1009 * and insert it into the target one.
1011 tmpfs_dir_detach(fdvp, de);
1013 if (fcnp->cn_flags & DOWHITEOUT)
1014 tmpfs_dir_whiteout_add(fdvp, fcnp);
1015 if (tcnp->cn_flags & ISWHITEOUT)
1016 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1019 * If the name has changed, we need to make it effective by changing
1020 * it in the directory entry.
1022 if (newname != NULL) {
1023 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1025 free(de->ud.td_name, M_TMPFSNAME);
1026 de->ud.td_name = newname;
1027 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1029 fnode->tn_status |= TMPFS_NODE_CHANGED;
1030 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1034 * If we are overwriting an entry, we have to remove the old one
1035 * from the target directory.
1038 struct tmpfs_dirent *tde;
1040 /* Remove the old entry from the target directory. */
1041 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1042 tmpfs_dir_detach(tdvp, tde);
1045 * Free the directory entry we just deleted. Note that the
1046 * node referred by it will not be removed until the vnode is
1049 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1052 tmpfs_dir_attach(tdvp, de);
1054 if (tmpfs_use_nc(fvp)) {
1058 cache_purge_negative(tdvp);
1064 if (fdvp != tdvp && fdvp != tvp)
1069 * Release target nodes.
1070 * XXX: I don't understand when tdvp can be the same as tvp, but
1071 * other code takes care of this...
1080 /* Release source nodes. */
1088 tmpfs_mkdir(struct vop_mkdir_args *v)
1090 struct vnode *dvp = v->a_dvp;
1091 struct vnode **vpp = v->a_vpp;
1092 struct componentname *cnp = v->a_cnp;
1093 struct vattr *vap = v->a_vap;
1095 MPASS(vap->va_type == VDIR);
1097 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1101 tmpfs_rmdir(struct vop_rmdir_args *v)
1103 struct vnode *dvp = v->a_dvp;
1104 struct vnode *vp = v->a_vp;
1107 struct tmpfs_dirent *de;
1108 struct tmpfs_mount *tmp;
1109 struct tmpfs_node *dnode;
1110 struct tmpfs_node *node;
1112 MPASS(VOP_ISLOCKED(dvp));
1113 MPASS(VOP_ISLOCKED(vp));
1115 tmp = VFS_TO_TMPFS(dvp->v_mount);
1116 dnode = VP_TO_TMPFS_DIR(dvp);
1117 node = VP_TO_TMPFS_DIR(vp);
1119 /* Directories with more than two entries ('.' and '..') cannot be
1121 if (node->tn_size > 0) {
1126 if ((dnode->tn_flags & APPEND)
1127 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1132 /* This invariant holds only if we are not trying to remove "..".
1133 * We checked for that above so this is safe now. */
1134 MPASS(node->tn_dir.tn_parent == dnode);
1136 /* Get the directory entry associated with node (vp). This was
1137 * filled by tmpfs_lookup while looking up the entry. */
1138 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1139 MPASS(TMPFS_DIRENT_MATCHES(de,
1140 v->a_cnp->cn_nameptr,
1141 v->a_cnp->cn_namelen));
1143 /* Check flags to see if we are allowed to remove the directory. */
1144 if ((dnode->tn_flags & APPEND) != 0 ||
1145 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1151 /* Detach the directory entry from the directory (dnode). */
1152 tmpfs_dir_detach(dvp, de);
1153 if (v->a_cnp->cn_flags & DOWHITEOUT)
1154 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1156 /* No vnode should be allocated for this entry from this point */
1157 TMPFS_NODE_LOCK(node);
1159 node->tn_dir.tn_parent = NULL;
1160 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1161 TMPFS_NODE_MODIFIED;
1163 TMPFS_NODE_UNLOCK(node);
1165 TMPFS_NODE_LOCK(dnode);
1167 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1168 TMPFS_NODE_MODIFIED;
1169 TMPFS_NODE_UNLOCK(dnode);
1171 if (tmpfs_use_nc(dvp)) {
1176 /* Free the directory entry we just deleted. Note that the node
1177 * referred by it will not be removed until the vnode is really
1179 tmpfs_free_dirent(tmp, de);
1181 /* Release the deleted vnode (will destroy the node, notify
1182 * interested parties and clean it from the cache). */
1184 dnode->tn_status |= TMPFS_NODE_CHANGED;
1194 tmpfs_symlink(struct vop_symlink_args *v)
1196 struct vnode *dvp = v->a_dvp;
1197 struct vnode **vpp = v->a_vpp;
1198 struct componentname *cnp = v->a_cnp;
1199 struct vattr *vap = v->a_vap;
1200 const char *target = v->a_target;
1202 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1203 MPASS(vap->va_type == VLNK);
1205 vap->va_type = VLNK;
1208 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1212 tmpfs_readdir(struct vop_readdir_args *va)
1216 struct tmpfs_mount *tm;
1217 struct tmpfs_node *node;
1219 int *eofflag, *ncookies;
1221 int error, maxcookies;
1225 eofflag = va->a_eofflag;
1226 cookies = va->a_cookies;
1227 ncookies = va->a_ncookies;
1229 /* This operation only makes sense on directory nodes. */
1230 if (vp->v_type != VDIR)
1234 node = VP_TO_TMPFS_DIR(vp);
1235 tm = VFS_TO_TMPFS(vp->v_mount);
1237 startresid = uio->uio_resid;
1239 /* Allocate cookies for NFS and compat modules. */
1240 if (cookies != NULL && ncookies != NULL) {
1241 maxcookies = howmany(node->tn_size,
1242 sizeof(struct tmpfs_dirent)) + 2;
1243 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1248 if (cookies == NULL)
1249 error = tmpfs_dir_getdents(tm, node, uio, 0, NULL, NULL);
1251 error = tmpfs_dir_getdents(tm, node, uio, maxcookies, *cookies,
1254 /* Buffer was filled without hitting EOF. */
1255 if (error == EJUSTRETURN)
1256 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1258 if (error != 0 && cookies != NULL && ncookies != NULL) {
1259 free(*cookies, M_TEMP);
1264 if (eofflag != NULL)
1266 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1272 tmpfs_readlink(struct vop_readlink_args *v)
1274 struct vnode *vp = v->a_vp;
1275 struct uio *uio = v->a_uio;
1278 struct tmpfs_node *node;
1280 MPASS(uio->uio_offset == 0);
1281 MPASS(vp->v_type == VLNK);
1283 node = VP_TO_TMPFS_NODE(vp);
1285 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1287 tmpfs_set_status(VFS_TO_TMPFS(vp->v_mount), node, TMPFS_NODE_ACCESSED);
1293 tmpfs_inactive(struct vop_inactive_args *v)
1296 struct tmpfs_node *node;
1299 node = VP_TO_TMPFS_NODE(vp);
1300 if (node->tn_links == 0)
1303 tmpfs_check_mtime(vp);
1308 tmpfs_need_inactive(struct vop_need_inactive_args *ap)
1311 struct tmpfs_node *node;
1312 struct vm_object *obj;
1315 node = VP_TO_TMPFS_NODE(vp);
1316 if (node->tn_links == 0)
1318 if (vp->v_type == VREG) {
1320 if (obj->generation != obj->cleangeneration)
1329 tmpfs_reclaim(struct vop_reclaim_args *v)
1331 struct vnode *vp = v->a_vp;
1333 struct tmpfs_mount *tmp;
1334 struct tmpfs_node *node;
1336 node = VP_TO_TMPFS_NODE(vp);
1337 tmp = VFS_TO_TMPFS(vp->v_mount);
1339 if (vp->v_type == VREG)
1340 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1341 vp->v_object = NULL;
1342 if (tmpfs_use_nc(vp))
1345 TMPFS_NODE_LOCK(node);
1348 /* If the node referenced by this vnode was deleted by the user,
1349 * we must free its associated data structures (now that the vnode
1350 * is being reclaimed). */
1351 if (node->tn_links == 0 &&
1352 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1353 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1354 TMPFS_NODE_UNLOCK(node);
1355 tmpfs_free_node(tmp, node);
1357 TMPFS_NODE_UNLOCK(node);
1359 MPASS(vp->v_data == NULL);
1364 tmpfs_print(struct vop_print_args *v)
1366 struct vnode *vp = v->a_vp;
1368 struct tmpfs_node *node;
1370 node = VP_TO_TMPFS_NODE(vp);
1372 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
1373 node, node->tn_flags, (uintmax_t)node->tn_links);
1374 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1375 node->tn_mode, node->tn_uid, node->tn_gid,
1376 (intmax_t)node->tn_size, node->tn_status);
1378 if (vp->v_type == VFIFO)
1387 tmpfs_pathconf(struct vop_pathconf_args *v)
1389 struct vnode *vp = v->a_vp;
1390 int name = v->a_name;
1391 long *retval = v->a_retval;
1399 *retval = TMPFS_LINK_MAX;
1407 if (vp->v_type == VDIR || vp->v_type == VFIFO)
1413 case _PC_CHOWN_RESTRICTED:
1425 case _PC_FILESIZEBITS:
1430 error = vop_stdpathconf(v);
1437 tmpfs_vptofh(struct vop_vptofh_args *ap)
1440 IN struct vnode *a_vp;
1441 IN struct fid *a_fhp;
1445 struct tmpfs_fid_data tfd;
1446 struct tmpfs_node *node;
1449 node = VP_TO_TMPFS_NODE(ap->a_vp);
1451 fhp->fid_len = sizeof(tfd);
1454 * Copy into fid_data from the stack to avoid unaligned pointer use.
1455 * See the comment in sys/mount.h on struct fid for details.
1457 tfd.tfd_id = node->tn_id;
1458 tfd.tfd_gen = node->tn_gen;
1459 memcpy(fhp->fid_data, &tfd, fhp->fid_len);
1465 tmpfs_whiteout(struct vop_whiteout_args *ap)
1467 struct vnode *dvp = ap->a_dvp;
1468 struct componentname *cnp = ap->a_cnp;
1469 struct tmpfs_dirent *de;
1471 switch (ap->a_flags) {
1475 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1477 return (de->td_node == NULL ? 0 : EEXIST);
1478 return (tmpfs_dir_whiteout_add(dvp, cnp));
1480 tmpfs_dir_whiteout_remove(dvp, cnp);
1483 panic("tmpfs_whiteout: unknown op");
1488 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1489 struct tmpfs_dirent **pde)
1491 struct tmpfs_dir_cursor dc;
1492 struct tmpfs_dirent *de;
1494 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1495 de = tmpfs_dir_next(tnp, &dc)) {
1496 if (de->td_node == tn) {
1505 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1506 struct tmpfs_node *tnp, char *buf, size_t *buflen, struct vnode **dvp)
1508 struct tmpfs_dirent *de;
1511 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1515 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1518 i -= de->td_namelen;
1522 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1539 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1541 struct vnode *vp, **dvp;
1542 struct tmpfs_node *tn, *tnp, *tnp1;
1543 struct tmpfs_dirent *de;
1544 struct tmpfs_mount *tm;
1552 buflen = ap->a_buflen;
1554 tm = VFS_TO_TMPFS(vp->v_mount);
1555 tn = VP_TO_TMPFS_NODE(vp);
1556 if (tn->tn_type == VDIR) {
1557 tnp = tn->tn_dir.tn_parent;
1560 tmpfs_ref_node(tnp);
1561 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1563 tmpfs_free_node(tm, tnp);
1568 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1569 if (tnp->tn_type != VDIR)
1571 TMPFS_NODE_LOCK(tnp);
1572 tmpfs_ref_node_locked(tnp);
1575 * tn_vnode cannot be instantiated while we hold the
1576 * node lock, so the directory cannot be changed while
1577 * we iterate over it. Do this to avoid instantiating
1578 * vnode for directories which cannot point to our
1581 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1585 TMPFS_NODE_UNLOCK(tnp);
1587 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1590 tmpfs_free_node(tm, tnp);
1593 if (VN_IS_DOOMED(vp)) {
1594 tmpfs_free_node(tm, tnp);
1598 TMPFS_NODE_LOCK(tnp);
1600 if (tmpfs_free_node_locked(tm, tnp, false)) {
1603 KASSERT(tnp->tn_refcount > 0,
1604 ("node %p refcount zero", tnp));
1605 tnp1 = LIST_NEXT(tnp, tn_entries);
1606 TMPFS_NODE_UNLOCK(tnp);
1614 * Vnode operations vector used for files stored in a tmpfs file system.
1616 struct vop_vector tmpfs_vnodeop_entries = {
1617 .vop_default = &default_vnodeops,
1618 .vop_lookup = vfs_cache_lookup,
1619 .vop_cachedlookup = tmpfs_cached_lookup,
1620 .vop_create = tmpfs_create,
1621 .vop_mknod = tmpfs_mknod,
1622 .vop_open = tmpfs_open,
1623 .vop_close = tmpfs_close,
1624 .vop_access = tmpfs_access,
1625 .vop_getattr = tmpfs_getattr,
1626 .vop_setattr = tmpfs_setattr,
1627 .vop_read = tmpfs_read,
1628 .vop_write = tmpfs_write,
1629 .vop_fsync = tmpfs_fsync,
1630 .vop_remove = tmpfs_remove,
1631 .vop_link = tmpfs_link,
1632 .vop_rename = tmpfs_rename,
1633 .vop_mkdir = tmpfs_mkdir,
1634 .vop_rmdir = tmpfs_rmdir,
1635 .vop_symlink = tmpfs_symlink,
1636 .vop_readdir = tmpfs_readdir,
1637 .vop_readlink = tmpfs_readlink,
1638 .vop_inactive = tmpfs_inactive,
1639 .vop_need_inactive = tmpfs_need_inactive,
1640 .vop_reclaim = tmpfs_reclaim,
1641 .vop_print = tmpfs_print,
1642 .vop_pathconf = tmpfs_pathconf,
1643 .vop_vptofh = tmpfs_vptofh,
1644 .vop_whiteout = tmpfs_whiteout,
1645 .vop_bmap = VOP_EOPNOTSUPP,
1646 .vop_vptocnp = tmpfs_vptocnp,
1647 .vop_lock1 = vop_lock,
1648 .vop_unlock = vop_unlock,
1649 .vop_islocked = vop_islocked,
1651 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);
1654 * Same vector for mounts which do not use namecache.
1656 struct vop_vector tmpfs_vnodeop_nonc_entries = {
1657 .vop_default = &tmpfs_vnodeop_entries,
1658 .vop_lookup = tmpfs_lookup,
1660 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_nonc_entries);