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>
59 #include <security/audit/audit.h>
60 #include <security/mac/mac_framework.h>
63 #include <vm/vm_param.h>
64 #include <vm/vm_object.h>
66 #include <fs/tmpfs/tmpfs_vnops.h>
67 #include <fs/tmpfs/tmpfs.h>
69 SYSCTL_DECL(_vfs_tmpfs);
72 static volatile int tmpfs_rename_restarts;
73 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
74 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
75 "Times rename had to restart due to lock contention");
78 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
82 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
86 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
88 struct tmpfs_dirent *de;
89 struct tmpfs_node *dnode, *pnode;
90 struct tmpfs_mount *tm;
93 dnode = VP_TO_TMPFS_DIR(dvp);
96 /* Check accessibility of requested node as a first step. */
97 error = vn_dir_check_exec(dvp, cnp);
101 /* We cannot be requesting the parent directory of the root node. */
102 MPASS(IMPLIES(dnode->tn_type == VDIR &&
103 dnode->tn_dir.tn_parent == dnode,
104 !(cnp->cn_flags & ISDOTDOT)));
106 TMPFS_ASSERT_LOCKED(dnode);
107 if (dnode->tn_dir.tn_parent == NULL) {
111 if (cnp->cn_flags & ISDOTDOT) {
112 tm = VFS_TO_TMPFS(dvp->v_mount);
113 pnode = dnode->tn_dir.tn_parent;
114 tmpfs_ref_node(pnode);
115 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
116 pnode, cnp->cn_lkflags, vpp);
117 tmpfs_free_node(tm, pnode);
120 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
125 de = tmpfs_dir_lookup(dnode, NULL, cnp);
126 if (de != NULL && de->td_node == NULL)
127 cnp->cn_flags |= ISWHITEOUT;
128 if (de == NULL || de->td_node == NULL) {
130 * The entry was not found in the directory.
131 * This is OK if we are creating or renaming an
132 * entry and are working on the last component of
135 if ((cnp->cn_flags & ISLASTCN) &&
136 (cnp->cn_nameiop == CREATE || \
137 cnp->cn_nameiop == RENAME ||
138 (cnp->cn_nameiop == DELETE &&
139 cnp->cn_flags & DOWHITEOUT &&
140 cnp->cn_flags & ISWHITEOUT))) {
141 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
147 * Keep the component name in the buffer for
150 cnp->cn_flags |= SAVENAME;
156 struct tmpfs_node *tnode;
159 * The entry was found, so get its associated
165 * If we are not at the last path component and
166 * found a non-directory or non-link entry (which
167 * may itself be pointing to a directory), raise
170 if ((tnode->tn_type != VDIR &&
171 tnode->tn_type != VLNK) &&
172 !(cnp->cn_flags & ISLASTCN)) {
178 * If we are deleting or renaming the entry, keep
179 * track of its tmpfs_dirent so that it can be
180 * easily deleted later.
182 if ((cnp->cn_flags & ISLASTCN) &&
183 (cnp->cn_nameiop == DELETE ||
184 cnp->cn_nameiop == RENAME)) {
185 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
190 /* Allocate a new vnode on the matching entry. */
191 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
192 cnp->cn_lkflags, vpp);
196 if ((dnode->tn_mode & S_ISTXT) &&
197 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
198 cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
199 cnp->cn_cred, cnp->cn_thread)) {
205 cnp->cn_flags |= SAVENAME;
207 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
208 cnp->cn_lkflags, vpp);
216 * Store the result of this lookup in the cache. Avoid this if the
217 * request was for creation, as it does not improve timings on
220 if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
221 cache_enter(dvp, *vpp, cnp);
225 * If there were no errors, *vpp cannot be null and it must be
228 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
234 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
237 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
241 tmpfs_lookup(struct vop_lookup_args *v)
244 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
248 tmpfs_create(struct vop_create_args *v)
250 struct vnode *dvp = v->a_dvp;
251 struct vnode **vpp = v->a_vpp;
252 struct componentname *cnp = v->a_cnp;
253 struct vattr *vap = v->a_vap;
256 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
258 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
259 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
260 cache_enter(dvp, *vpp, cnp);
265 tmpfs_mknod(struct vop_mknod_args *v)
267 struct vnode *dvp = v->a_dvp;
268 struct vnode **vpp = v->a_vpp;
269 struct componentname *cnp = v->a_cnp;
270 struct vattr *vap = v->a_vap;
272 if (vap->va_type != VBLK && vap->va_type != VCHR &&
273 vap->va_type != VFIFO)
276 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
280 tmpfs_open(struct vop_open_args *v)
282 struct vnode *vp = v->a_vp;
283 int mode = v->a_mode;
286 struct tmpfs_node *node;
288 MPASS(VOP_ISLOCKED(vp));
290 node = VP_TO_TMPFS_NODE(vp);
292 /* The file is still active but all its names have been removed
293 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
294 * it is about to die. */
295 if (node->tn_links < 1)
298 /* If the file is marked append-only, deny write requests. */
299 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
303 /* For regular files, the call below is nop. */
304 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
305 OBJ_DEAD) == 0, ("dead object"));
306 vnode_create_vobject(vp, node->tn_size, v->a_td);
309 MPASS(VOP_ISLOCKED(vp));
314 tmpfs_close(struct vop_close_args *v)
316 struct vnode *vp = v->a_vp;
318 /* Update node times. */
325 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
326 * the comment above cache_fplookup for details.
329 tmpfs_fplookup_vexec(struct vop_fplookup_vexec_args *v)
332 struct tmpfs_node *node;
337 node = VP_TO_TMPFS_NODE_SMR(vp);
338 if (__predict_false(node == NULL))
341 all_x = S_IXUSR | S_IXGRP | S_IXOTH;
342 mode = atomic_load_short(&node->tn_mode);
343 if (__predict_true((mode & all_x) == all_x))
347 return (vaccess_vexec_smr(mode, node->tn_uid, node->tn_gid, cred));
351 tmpfs_access(struct vop_access_args *v)
353 struct vnode *vp = v->a_vp;
354 accmode_t accmode = v->a_accmode;
355 struct ucred *cred = v->a_cred;
356 mode_t all_x = S_IXUSR | S_IXGRP | S_IXOTH;
358 struct tmpfs_node *node;
360 MPASS(VOP_ISLOCKED(vp));
362 node = VP_TO_TMPFS_NODE(vp);
365 * Common case path lookup.
367 if (__predict_true(accmode == VEXEC && (node->tn_mode & all_x) == all_x))
370 switch (vp->v_type) {
376 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
396 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
401 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
405 MPASS(VOP_ISLOCKED(vp));
411 tmpfs_stat(struct vop_stat_args *v)
413 struct vnode *vp = v->a_vp;
414 struct stat *sb = v->a_sb;
416 struct tmpfs_node *node;
419 node = VP_TO_TMPFS_NODE(vp);
421 tmpfs_update_getattr(vp);
423 error = vop_stat_helper_pre(v);
424 if (__predict_false(error))
427 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
428 sb->st_ino = node->tn_id;
429 sb->st_mode = node->tn_mode | VTTOIF(vp->v_type);
430 sb->st_nlink = node->tn_links;
431 sb->st_uid = node->tn_uid;
432 sb->st_gid = node->tn_gid;
433 sb->st_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
434 node->tn_rdev : NODEV;
435 sb->st_size = node->tn_size;
436 sb->st_atim.tv_sec = node->tn_atime.tv_sec;
437 sb->st_atim.tv_nsec = node->tn_atime.tv_nsec;
438 sb->st_mtim.tv_sec = node->tn_mtime.tv_sec;
439 sb->st_mtim.tv_nsec = node->tn_mtime.tv_nsec;
440 sb->st_ctim.tv_sec = node->tn_ctime.tv_sec;
441 sb->st_ctim.tv_nsec = node->tn_ctime.tv_nsec;
442 sb->st_birthtim.tv_sec = node->tn_birthtime.tv_sec;
443 sb->st_birthtim.tv_nsec = node->tn_birthtime.tv_nsec;
444 sb->st_blksize = PAGE_SIZE;
445 sb->st_flags = node->tn_flags;
446 sb->st_gen = node->tn_gen;
447 if (vp->v_type == VREG) {
448 obj = node->tn_reg.tn_aobj;
449 sb->st_blocks = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
451 sb->st_blocks = node->tn_size;
452 sb->st_blocks /= S_BLKSIZE;
453 return (vop_stat_helper_post(v, error));
457 tmpfs_getattr(struct vop_getattr_args *v)
459 struct vnode *vp = v->a_vp;
460 struct vattr *vap = v->a_vap;
462 struct tmpfs_node *node;
464 node = VP_TO_TMPFS_NODE(vp);
466 tmpfs_update_getattr(vp);
468 vap->va_type = vp->v_type;
469 vap->va_mode = node->tn_mode;
470 vap->va_nlink = node->tn_links;
471 vap->va_uid = node->tn_uid;
472 vap->va_gid = node->tn_gid;
473 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
474 vap->va_fileid = node->tn_id;
475 vap->va_size = node->tn_size;
476 vap->va_blocksize = PAGE_SIZE;
477 vap->va_atime = node->tn_atime;
478 vap->va_mtime = node->tn_mtime;
479 vap->va_ctime = node->tn_ctime;
480 vap->va_birthtime = node->tn_birthtime;
481 vap->va_gen = node->tn_gen;
482 vap->va_flags = node->tn_flags;
483 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
484 node->tn_rdev : NODEV;
485 if (vp->v_type == VREG) {
486 obj = node->tn_reg.tn_aobj;
487 vap->va_bytes = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
489 vap->va_bytes = node->tn_size;
496 tmpfs_setattr(struct vop_setattr_args *v)
498 struct vnode *vp = v->a_vp;
499 struct vattr *vap = v->a_vap;
500 struct ucred *cred = v->a_cred;
501 struct thread *td = curthread;
505 MPASS(VOP_ISLOCKED(vp));
506 ASSERT_VOP_IN_SEQC(vp);
510 /* Abort if any unsettable attribute is given. */
511 if (vap->va_type != VNON ||
512 vap->va_nlink != VNOVAL ||
513 vap->va_fsid != VNOVAL ||
514 vap->va_fileid != VNOVAL ||
515 vap->va_blocksize != VNOVAL ||
516 vap->va_gen != VNOVAL ||
517 vap->va_rdev != VNOVAL ||
518 vap->va_bytes != VNOVAL)
521 if (error == 0 && (vap->va_flags != VNOVAL))
522 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
524 if (error == 0 && (vap->va_size != VNOVAL))
525 error = tmpfs_chsize(vp, vap->va_size, cred, td);
527 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
528 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
530 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
531 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
533 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
534 vap->va_atime.tv_nsec != VNOVAL) ||
535 (vap->va_mtime.tv_sec != VNOVAL &&
536 vap->va_mtime.tv_nsec != VNOVAL) ||
537 (vap->va_birthtime.tv_sec != VNOVAL &&
538 vap->va_birthtime.tv_nsec != VNOVAL)))
539 error = tmpfs_chtimes(vp, vap, cred, td);
541 /* Update the node times. We give preference to the error codes
542 * generated by this function rather than the ones that may arise
543 * from tmpfs_update. */
546 MPASS(VOP_ISLOCKED(vp));
552 tmpfs_read(struct vop_read_args *v)
556 struct tmpfs_node *node;
559 if (vp->v_type != VREG)
562 if (uio->uio_offset < 0)
564 node = VP_TO_TMPFS_NODE(vp);
565 tmpfs_set_status(VFS_TO_TMPFS(vp->v_mount), node, TMPFS_NODE_ACCESSED);
566 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
570 tmpfs_write(struct vop_write_args *v)
574 struct tmpfs_node *node;
581 ioflag = v->a_ioflag;
583 node = VP_TO_TMPFS_NODE(vp);
584 oldsize = node->tn_size;
586 if (uio->uio_offset < 0 || vp->v_type != VREG)
588 if (uio->uio_resid == 0)
590 if (ioflag & IO_APPEND)
591 uio->uio_offset = node->tn_size;
592 if (uio->uio_offset + uio->uio_resid >
593 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
595 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
597 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
598 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
604 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
605 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
607 if (node->tn_mode & (S_ISUID | S_ISGID)) {
608 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID)) {
609 newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
610 vn_seqc_write_begin(vp);
611 atomic_store_short(&node->tn_mode, newmode);
612 vn_seqc_write_end(vp);
616 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
619 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
620 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
626 tmpfs_fsync(struct vop_fsync_args *v)
628 struct vnode *vp = v->a_vp;
630 MPASS(VOP_ISLOCKED(vp));
632 tmpfs_check_mtime(vp);
639 tmpfs_remove(struct vop_remove_args *v)
641 struct vnode *dvp = v->a_dvp;
642 struct vnode *vp = v->a_vp;
645 struct tmpfs_dirent *de;
646 struct tmpfs_mount *tmp;
647 struct tmpfs_node *dnode;
648 struct tmpfs_node *node;
650 MPASS(VOP_ISLOCKED(dvp));
651 MPASS(VOP_ISLOCKED(vp));
653 if (vp->v_type == VDIR) {
658 dnode = VP_TO_TMPFS_DIR(dvp);
659 node = VP_TO_TMPFS_NODE(vp);
660 tmp = VFS_TO_TMPFS(vp->v_mount);
661 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
664 /* Files marked as immutable or append-only cannot be deleted. */
665 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
666 (dnode->tn_flags & APPEND)) {
671 /* Remove the entry from the directory; as it is a file, we do not
672 * have to change the number of hard links of the directory. */
673 tmpfs_dir_detach(dvp, de);
674 if (v->a_cnp->cn_flags & DOWHITEOUT)
675 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
677 /* Free the directory entry we just deleted. Note that the node
678 * referred by it will not be removed until the vnode is really
680 tmpfs_free_dirent(tmp, de);
682 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
691 tmpfs_link(struct vop_link_args *v)
693 struct vnode *dvp = v->a_tdvp;
694 struct vnode *vp = v->a_vp;
695 struct componentname *cnp = v->a_cnp;
698 struct tmpfs_dirent *de;
699 struct tmpfs_node *node;
701 MPASS(VOP_ISLOCKED(dvp));
702 MPASS(cnp->cn_flags & HASBUF);
703 MPASS(dvp != vp); /* XXX When can this be false? */
704 node = VP_TO_TMPFS_NODE(vp);
706 /* Ensure that we do not overflow the maximum number of links imposed
708 MPASS(node->tn_links <= TMPFS_LINK_MAX);
709 if (node->tn_links == TMPFS_LINK_MAX) {
714 /* We cannot create links of files marked immutable or append-only. */
715 if (node->tn_flags & (IMMUTABLE | APPEND)) {
720 /* Allocate a new directory entry to represent the node. */
721 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
722 cnp->cn_nameptr, cnp->cn_namelen, &de);
726 /* Insert the new directory entry into the appropriate directory. */
727 if (cnp->cn_flags & ISWHITEOUT)
728 tmpfs_dir_whiteout_remove(dvp, cnp);
729 tmpfs_dir_attach(dvp, de);
731 /* vp link count has changed, so update node times. */
732 node->tn_status |= TMPFS_NODE_CHANGED;
742 * We acquire all but fdvp locks using non-blocking acquisitions. If we
743 * fail to acquire any lock in the path we will drop all held locks,
744 * acquire the new lock in a blocking fashion, and then release it and
745 * restart the rename. This acquire/release step ensures that we do not
746 * spin on a lock waiting for release. On error release all vnode locks
747 * and decrement references the way tmpfs_rename() would do.
750 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
751 struct vnode *tdvp, struct vnode **tvpp,
752 struct componentname *fcnp, struct componentname *tcnp)
756 struct tmpfs_dirent *de;
757 int error, restarts = 0;
760 if (*tvpp != NULL && *tvpp != tdvp)
766 error = vn_lock(fdvp, LK_EXCLUSIVE);
769 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
771 error = vn_lock(tdvp, LK_EXCLUSIVE);
778 * Re-resolve fvp to be certain it still exists and fetch the
781 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
785 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
786 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
792 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
798 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
803 * Concurrent rename race.
818 * Re-resolve tvp and acquire the vnode lock if present.
820 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
822 * If tvp disappeared we just carry on.
824 if (de == NULL && *tvpp != NULL) {
829 * Get the tvp ino if the lookup succeeded. We may have to restart
830 * if the non-blocking acquire fails.
834 error = tmpfs_alloc_vp(mp, de->td_node,
835 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
844 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
850 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
859 tmpfs_rename_restarts += restarts;
869 tmpfs_rename_restarts += restarts;
875 tmpfs_rename(struct vop_rename_args *v)
877 struct vnode *fdvp = v->a_fdvp;
878 struct vnode *fvp = v->a_fvp;
879 struct componentname *fcnp = v->a_fcnp;
880 struct vnode *tdvp = v->a_tdvp;
881 struct vnode *tvp = v->a_tvp;
882 struct componentname *tcnp = v->a_tcnp;
884 struct tmpfs_dirent *de;
885 struct tmpfs_mount *tmp;
886 struct tmpfs_node *fdnode;
887 struct tmpfs_node *fnode;
888 struct tmpfs_node *tnode;
889 struct tmpfs_node *tdnode;
893 MPASS(VOP_ISLOCKED(tdvp));
894 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
895 MPASS(fcnp->cn_flags & HASBUF);
896 MPASS(tcnp->cn_flags & HASBUF);
898 want_seqc_end = false;
901 * Disallow cross-device renames.
902 * XXX Why isn't this done by the caller?
904 if (fvp->v_mount != tdvp->v_mount ||
905 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
910 /* If source and target are the same file, there is nothing to do. */
917 * If we need to move the directory between entries, lock the
918 * source so that we can safely operate on it.
920 if (fdvp != tdvp && fdvp != tvp) {
921 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
922 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
926 ASSERT_VOP_ELOCKED(fdvp,
927 "tmpfs_rename: fdvp not locked");
928 ASSERT_VOP_ELOCKED(tdvp,
929 "tmpfs_rename: tdvp not locked");
931 ASSERT_VOP_ELOCKED(tvp,
932 "tmpfs_rename: tvp not locked");
941 vn_seqc_write_begin(tvp);
942 vn_seqc_write_begin(tdvp);
943 vn_seqc_write_begin(fvp);
944 vn_seqc_write_begin(fdvp);
945 want_seqc_end = true;
947 tmp = VFS_TO_TMPFS(tdvp->v_mount);
948 tdnode = VP_TO_TMPFS_DIR(tdvp);
949 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
950 fdnode = VP_TO_TMPFS_DIR(fdvp);
951 fnode = VP_TO_TMPFS_NODE(fvp);
952 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
955 * Entry can disappear before we lock fdvp,
956 * also avoid manipulating '.' and '..' entries.
959 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
960 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
966 MPASS(de->td_node == fnode);
969 * If re-naming a directory to another preexisting directory
970 * ensure that the target directory is empty so that its
971 * removal causes no side effects.
972 * Kern_rename guarantees the destination to be a directory
973 * if the source is one.
976 MPASS(tnode != NULL);
978 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
979 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
984 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
985 if (tnode->tn_size > 0) {
989 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
992 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
996 MPASS(fnode->tn_type != VDIR &&
997 tnode->tn_type != VDIR);
1001 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1002 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1008 * Ensure that we have enough memory to hold the new name, if it
1009 * has to be changed.
1011 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1012 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1013 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1018 * If the node is being moved to another directory, we have to do
1021 if (fdnode != tdnode) {
1023 * In case we are moving a directory, we have to adjust its
1024 * parent to point to the new parent.
1026 if (de->td_node->tn_type == VDIR) {
1027 struct tmpfs_node *n;
1030 * Ensure the target directory is not a child of the
1031 * directory being moved. Otherwise, we'd end up
1036 * TMPFS_LOCK guaranties that no nodes are freed while
1037 * traversing the list. Nodes can only be marked as
1038 * removed: tn_parent == NULL.
1042 while (n != n->tn_dir.tn_parent) {
1043 struct tmpfs_node *parent;
1046 TMPFS_NODE_UNLOCK(n);
1049 if (newname != NULL)
1050 free(newname, M_TMPFSNAME);
1053 parent = n->tn_dir.tn_parent;
1054 TMPFS_NODE_UNLOCK(n);
1055 if (parent == NULL) {
1059 TMPFS_NODE_LOCK(parent);
1060 if (parent->tn_dir.tn_parent == NULL) {
1061 TMPFS_NODE_UNLOCK(parent);
1070 if (newname != NULL)
1071 free(newname, M_TMPFSNAME);
1074 TMPFS_NODE_UNLOCK(n);
1076 /* Adjust the parent pointer. */
1077 TMPFS_VALIDATE_DIR(fnode);
1078 TMPFS_NODE_LOCK(de->td_node);
1079 de->td_node->tn_dir.tn_parent = tdnode;
1080 TMPFS_NODE_UNLOCK(de->td_node);
1083 * As a result of changing the target of the '..'
1084 * entry, the link count of the source and target
1085 * directories has to be adjusted.
1087 TMPFS_NODE_LOCK(tdnode);
1088 TMPFS_ASSERT_LOCKED(tdnode);
1090 TMPFS_NODE_UNLOCK(tdnode);
1092 TMPFS_NODE_LOCK(fdnode);
1093 TMPFS_ASSERT_LOCKED(fdnode);
1095 TMPFS_NODE_UNLOCK(fdnode);
1100 * Do the move: just remove the entry from the source directory
1101 * and insert it into the target one.
1103 tmpfs_dir_detach(fdvp, de);
1105 if (fcnp->cn_flags & DOWHITEOUT)
1106 tmpfs_dir_whiteout_add(fdvp, fcnp);
1107 if (tcnp->cn_flags & ISWHITEOUT)
1108 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1111 * If the name has changed, we need to make it effective by changing
1112 * it in the directory entry.
1114 if (newname != NULL) {
1115 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1117 free(de->ud.td_name, M_TMPFSNAME);
1118 de->ud.td_name = newname;
1119 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1121 fnode->tn_status |= TMPFS_NODE_CHANGED;
1122 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1126 * If we are overwriting an entry, we have to remove the old one
1127 * from the target directory.
1130 struct tmpfs_dirent *tde;
1132 /* Remove the old entry from the target directory. */
1133 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1134 tmpfs_dir_detach(tdvp, tde);
1137 * Free the directory entry we just deleted. Note that the
1138 * node referred by it will not be removed until the vnode is
1141 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1144 tmpfs_dir_attach(tdvp, de);
1146 if (tmpfs_use_nc(fvp)) {
1147 cache_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1153 if (fdvp != tdvp && fdvp != tvp)
1157 if (want_seqc_end) {
1159 vn_seqc_write_end(tvp);
1160 vn_seqc_write_end(tdvp);
1161 vn_seqc_write_end(fvp);
1162 vn_seqc_write_end(fdvp);
1166 * Release target nodes.
1167 * XXX: I don't understand when tdvp can be the same as tvp, but
1168 * other code takes care of this...
1177 /* Release source nodes. */
1185 tmpfs_mkdir(struct vop_mkdir_args *v)
1187 struct vnode *dvp = v->a_dvp;
1188 struct vnode **vpp = v->a_vpp;
1189 struct componentname *cnp = v->a_cnp;
1190 struct vattr *vap = v->a_vap;
1192 MPASS(vap->va_type == VDIR);
1194 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1198 tmpfs_rmdir(struct vop_rmdir_args *v)
1200 struct vnode *dvp = v->a_dvp;
1201 struct vnode *vp = v->a_vp;
1204 struct tmpfs_dirent *de;
1205 struct tmpfs_mount *tmp;
1206 struct tmpfs_node *dnode;
1207 struct tmpfs_node *node;
1209 MPASS(VOP_ISLOCKED(dvp));
1210 MPASS(VOP_ISLOCKED(vp));
1212 tmp = VFS_TO_TMPFS(dvp->v_mount);
1213 dnode = VP_TO_TMPFS_DIR(dvp);
1214 node = VP_TO_TMPFS_DIR(vp);
1216 /* Directories with more than two entries ('.' and '..') cannot be
1218 if (node->tn_size > 0) {
1223 if ((dnode->tn_flags & APPEND)
1224 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1229 /* This invariant holds only if we are not trying to remove "..".
1230 * We checked for that above so this is safe now. */
1231 MPASS(node->tn_dir.tn_parent == dnode);
1233 /* Get the directory entry associated with node (vp). This was
1234 * filled by tmpfs_lookup while looking up the entry. */
1235 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1236 MPASS(TMPFS_DIRENT_MATCHES(de,
1237 v->a_cnp->cn_nameptr,
1238 v->a_cnp->cn_namelen));
1240 /* Check flags to see if we are allowed to remove the directory. */
1241 if ((dnode->tn_flags & APPEND) != 0 ||
1242 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1247 /* Detach the directory entry from the directory (dnode). */
1248 tmpfs_dir_detach(dvp, de);
1249 if (v->a_cnp->cn_flags & DOWHITEOUT)
1250 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1252 /* No vnode should be allocated for this entry from this point */
1253 TMPFS_NODE_LOCK(node);
1255 node->tn_dir.tn_parent = NULL;
1256 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1257 TMPFS_NODE_MODIFIED;
1259 TMPFS_NODE_UNLOCK(node);
1261 TMPFS_NODE_LOCK(dnode);
1263 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1264 TMPFS_NODE_MODIFIED;
1265 TMPFS_NODE_UNLOCK(dnode);
1267 if (tmpfs_use_nc(dvp)) {
1272 /* Free the directory entry we just deleted. Note that the node
1273 * referred by it will not be removed until the vnode is really
1275 tmpfs_free_dirent(tmp, de);
1277 /* Release the deleted vnode (will destroy the node, notify
1278 * interested parties and clean it from the cache). */
1280 dnode->tn_status |= TMPFS_NODE_CHANGED;
1290 tmpfs_symlink(struct vop_symlink_args *v)
1292 struct vnode *dvp = v->a_dvp;
1293 struct vnode **vpp = v->a_vpp;
1294 struct componentname *cnp = v->a_cnp;
1295 struct vattr *vap = v->a_vap;
1296 const char *target = v->a_target;
1298 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1299 MPASS(vap->va_type == VLNK);
1301 vap->va_type = VLNK;
1304 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1308 tmpfs_readdir(struct vop_readdir_args *va)
1312 struct tmpfs_mount *tm;
1313 struct tmpfs_node *node;
1315 int *eofflag, *ncookies;
1317 int error, maxcookies;
1321 eofflag = va->a_eofflag;
1322 cookies = va->a_cookies;
1323 ncookies = va->a_ncookies;
1325 /* This operation only makes sense on directory nodes. */
1326 if (vp->v_type != VDIR)
1330 node = VP_TO_TMPFS_DIR(vp);
1331 tm = VFS_TO_TMPFS(vp->v_mount);
1333 startresid = uio->uio_resid;
1335 /* Allocate cookies for NFS and compat modules. */
1336 if (cookies != NULL && ncookies != NULL) {
1337 maxcookies = howmany(node->tn_size,
1338 sizeof(struct tmpfs_dirent)) + 2;
1339 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1344 if (cookies == NULL)
1345 error = tmpfs_dir_getdents(tm, node, uio, 0, NULL, NULL);
1347 error = tmpfs_dir_getdents(tm, node, uio, maxcookies, *cookies,
1350 /* Buffer was filled without hitting EOF. */
1351 if (error == EJUSTRETURN)
1352 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1354 if (error != 0 && cookies != NULL && ncookies != NULL) {
1355 free(*cookies, M_TEMP);
1360 if (eofflag != NULL)
1362 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1368 tmpfs_readlink(struct vop_readlink_args *v)
1370 struct vnode *vp = v->a_vp;
1371 struct uio *uio = v->a_uio;
1374 struct tmpfs_node *node;
1376 MPASS(uio->uio_offset == 0);
1377 MPASS(vp->v_type == VLNK);
1379 node = VP_TO_TMPFS_NODE(vp);
1381 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1383 tmpfs_set_status(VFS_TO_TMPFS(vp->v_mount), node, TMPFS_NODE_ACCESSED);
1389 tmpfs_inactive(struct vop_inactive_args *v)
1392 struct tmpfs_node *node;
1395 node = VP_TO_TMPFS_NODE(vp);
1396 if (node->tn_links == 0)
1399 tmpfs_check_mtime(vp);
1404 tmpfs_need_inactive(struct vop_need_inactive_args *ap)
1407 struct tmpfs_node *node;
1408 struct vm_object *obj;
1411 node = VP_TO_TMPFS_NODE(vp);
1412 if (node->tn_links == 0)
1414 if (vp->v_type == VREG) {
1416 if (obj->generation != obj->cleangeneration)
1425 tmpfs_reclaim(struct vop_reclaim_args *v)
1427 struct vnode *vp = v->a_vp;
1429 struct tmpfs_mount *tmp;
1430 struct tmpfs_node *node;
1432 node = VP_TO_TMPFS_NODE(vp);
1433 tmp = VFS_TO_TMPFS(vp->v_mount);
1435 if (vp->v_type == VREG)
1436 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1437 vp->v_object = NULL;
1439 TMPFS_NODE_LOCK(node);
1442 /* If the node referenced by this vnode was deleted by the user,
1443 * we must free its associated data structures (now that the vnode
1444 * is being reclaimed). */
1445 if (node->tn_links == 0 &&
1446 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1447 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1448 TMPFS_NODE_UNLOCK(node);
1449 tmpfs_free_node(tmp, node);
1451 TMPFS_NODE_UNLOCK(node);
1453 MPASS(vp->v_data == NULL);
1458 tmpfs_print(struct vop_print_args *v)
1460 struct vnode *vp = v->a_vp;
1462 struct tmpfs_node *node;
1464 node = VP_TO_TMPFS_NODE(vp);
1466 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
1467 node, node->tn_flags, (uintmax_t)node->tn_links);
1468 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1469 node->tn_mode, node->tn_uid, node->tn_gid,
1470 (intmax_t)node->tn_size, node->tn_status);
1472 if (vp->v_type == VFIFO)
1481 tmpfs_pathconf(struct vop_pathconf_args *v)
1483 struct vnode *vp = v->a_vp;
1484 int name = v->a_name;
1485 long *retval = v->a_retval;
1493 *retval = TMPFS_LINK_MAX;
1501 if (vp->v_type == VDIR || vp->v_type == VFIFO)
1507 case _PC_CHOWN_RESTRICTED:
1519 case _PC_FILESIZEBITS:
1524 error = vop_stdpathconf(v);
1531 tmpfs_vptofh(struct vop_vptofh_args *ap)
1534 IN struct vnode *a_vp;
1535 IN struct fid *a_fhp;
1539 struct tmpfs_fid_data tfd;
1540 struct tmpfs_node *node;
1543 node = VP_TO_TMPFS_NODE(ap->a_vp);
1545 fhp->fid_len = sizeof(tfd);
1548 * Copy into fid_data from the stack to avoid unaligned pointer use.
1549 * See the comment in sys/mount.h on struct fid for details.
1551 tfd.tfd_id = node->tn_id;
1552 tfd.tfd_gen = node->tn_gen;
1553 memcpy(fhp->fid_data, &tfd, fhp->fid_len);
1559 tmpfs_whiteout(struct vop_whiteout_args *ap)
1561 struct vnode *dvp = ap->a_dvp;
1562 struct componentname *cnp = ap->a_cnp;
1563 struct tmpfs_dirent *de;
1565 switch (ap->a_flags) {
1569 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1571 return (de->td_node == NULL ? 0 : EEXIST);
1572 return (tmpfs_dir_whiteout_add(dvp, cnp));
1574 tmpfs_dir_whiteout_remove(dvp, cnp);
1577 panic("tmpfs_whiteout: unknown op");
1582 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1583 struct tmpfs_dirent **pde)
1585 struct tmpfs_dir_cursor dc;
1586 struct tmpfs_dirent *de;
1588 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1589 de = tmpfs_dir_next(tnp, &dc)) {
1590 if (de->td_node == tn) {
1599 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1600 struct tmpfs_node *tnp, char *buf, size_t *buflen, struct vnode **dvp)
1602 struct tmpfs_dirent *de;
1605 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1609 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1612 i -= de->td_namelen;
1616 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1633 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1635 struct vnode *vp, **dvp;
1636 struct tmpfs_node *tn, *tnp, *tnp1;
1637 struct tmpfs_dirent *de;
1638 struct tmpfs_mount *tm;
1646 buflen = ap->a_buflen;
1648 tm = VFS_TO_TMPFS(vp->v_mount);
1649 tn = VP_TO_TMPFS_NODE(vp);
1650 if (tn->tn_type == VDIR) {
1651 tnp = tn->tn_dir.tn_parent;
1654 tmpfs_ref_node(tnp);
1655 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1657 tmpfs_free_node(tm, tnp);
1662 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1663 if (tnp->tn_type != VDIR)
1665 TMPFS_NODE_LOCK(tnp);
1666 tmpfs_ref_node(tnp);
1669 * tn_vnode cannot be instantiated while we hold the
1670 * node lock, so the directory cannot be changed while
1671 * we iterate over it. Do this to avoid instantiating
1672 * vnode for directories which cannot point to our
1675 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1679 TMPFS_NODE_UNLOCK(tnp);
1681 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1684 tmpfs_free_node(tm, tnp);
1687 if (VN_IS_DOOMED(vp)) {
1688 tmpfs_free_node(tm, tnp);
1692 TMPFS_NODE_LOCK(tnp);
1694 if (tmpfs_free_node_locked(tm, tnp, false)) {
1697 KASSERT(tnp->tn_refcount > 0,
1698 ("node %p refcount zero", tnp));
1699 tnp1 = LIST_NEXT(tnp, tn_entries);
1700 TMPFS_NODE_UNLOCK(tnp);
1708 * Vnode operations vector used for files stored in a tmpfs file system.
1710 struct vop_vector tmpfs_vnodeop_entries = {
1711 .vop_default = &default_vnodeops,
1712 .vop_lookup = vfs_cache_lookup,
1713 .vop_cachedlookup = tmpfs_cached_lookup,
1714 .vop_create = tmpfs_create,
1715 .vop_mknod = tmpfs_mknod,
1716 .vop_open = tmpfs_open,
1717 .vop_close = tmpfs_close,
1718 .vop_fplookup_vexec = tmpfs_fplookup_vexec,
1719 .vop_access = tmpfs_access,
1720 .vop_stat = tmpfs_stat,
1721 .vop_getattr = tmpfs_getattr,
1722 .vop_setattr = tmpfs_setattr,
1723 .vop_read = tmpfs_read,
1724 .vop_write = tmpfs_write,
1725 .vop_fsync = tmpfs_fsync,
1726 .vop_remove = tmpfs_remove,
1727 .vop_link = tmpfs_link,
1728 .vop_rename = tmpfs_rename,
1729 .vop_mkdir = tmpfs_mkdir,
1730 .vop_rmdir = tmpfs_rmdir,
1731 .vop_symlink = tmpfs_symlink,
1732 .vop_readdir = tmpfs_readdir,
1733 .vop_readlink = tmpfs_readlink,
1734 .vop_inactive = tmpfs_inactive,
1735 .vop_need_inactive = tmpfs_need_inactive,
1736 .vop_reclaim = tmpfs_reclaim,
1737 .vop_print = tmpfs_print,
1738 .vop_pathconf = tmpfs_pathconf,
1739 .vop_vptofh = tmpfs_vptofh,
1740 .vop_whiteout = tmpfs_whiteout,
1741 .vop_bmap = VOP_EOPNOTSUPP,
1742 .vop_vptocnp = tmpfs_vptocnp,
1743 .vop_lock1 = vop_lock,
1744 .vop_unlock = vop_unlock,
1745 .vop_islocked = vop_islocked,
1747 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);
1750 * Same vector for mounts which do not use namecache.
1752 struct vop_vector tmpfs_vnodeop_nonc_entries = {
1753 .vop_default = &tmpfs_vnodeop_entries,
1754 .vop_lookup = tmpfs_lookup,
1756 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_nonc_entries);