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_lookup(struct vop_cachedlookup_args *v)
81 struct vnode *dvp = v->a_dvp;
82 struct vnode **vpp = v->a_vpp;
83 struct componentname *cnp = v->a_cnp;
84 struct tmpfs_dirent *de;
85 struct tmpfs_node *dnode;
88 dnode = VP_TO_TMPFS_DIR(dvp);
91 /* Check accessibility of requested node as a first step. */
92 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
96 /* We cannot be requesting the parent directory of the root node. */
97 MPASS(IMPLIES(dnode->tn_type == VDIR &&
98 dnode->tn_dir.tn_parent == dnode,
99 !(cnp->cn_flags & ISDOTDOT)));
101 TMPFS_ASSERT_LOCKED(dnode);
102 if (dnode->tn_dir.tn_parent == NULL) {
106 if (cnp->cn_flags & ISDOTDOT) {
107 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
108 dnode->tn_dir.tn_parent, cnp->cn_lkflags, vpp);
111 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
116 de = tmpfs_dir_lookup(dnode, NULL, cnp);
117 if (de != NULL && de->td_node == NULL)
118 cnp->cn_flags |= ISWHITEOUT;
119 if (de == NULL || de->td_node == NULL) {
121 * The entry was not found in the directory.
122 * This is OK if we are creating or renaming an
123 * entry and are working on the last component of
126 if ((cnp->cn_flags & ISLASTCN) &&
127 (cnp->cn_nameiop == CREATE || \
128 cnp->cn_nameiop == RENAME ||
129 (cnp->cn_nameiop == DELETE &&
130 cnp->cn_flags & DOWHITEOUT &&
131 cnp->cn_flags & ISWHITEOUT))) {
132 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
138 * Keep the component name in the buffer for
141 cnp->cn_flags |= SAVENAME;
147 struct tmpfs_node *tnode;
150 * The entry was found, so get its associated
156 * If we are not at the last path component and
157 * found a non-directory or non-link entry (which
158 * may itself be pointing to a directory), raise
161 if ((tnode->tn_type != VDIR &&
162 tnode->tn_type != VLNK) &&
163 !(cnp->cn_flags & ISLASTCN)) {
169 * If we are deleting or renaming the entry, keep
170 * track of its tmpfs_dirent so that it can be
171 * easily deleted later.
173 if ((cnp->cn_flags & ISLASTCN) &&
174 (cnp->cn_nameiop == DELETE ||
175 cnp->cn_nameiop == RENAME)) {
176 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
181 /* Allocate a new vnode on the matching entry. */
182 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
183 cnp->cn_lkflags, vpp);
187 if ((dnode->tn_mode & S_ISTXT) &&
188 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
189 cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
190 cnp->cn_cred, cnp->cn_thread)) {
196 cnp->cn_flags |= SAVENAME;
198 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
199 cnp->cn_lkflags, vpp);
207 * Store the result of this lookup in the cache. Avoid this if the
208 * request was for creation, as it does not improve timings on
211 if ((cnp->cn_flags & MAKEENTRY) != 0)
212 cache_enter(dvp, *vpp, cnp);
216 * If there were no errors, *vpp cannot be null and it must be
219 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
225 tmpfs_create(struct vop_create_args *v)
227 struct vnode *dvp = v->a_dvp;
228 struct vnode **vpp = v->a_vpp;
229 struct componentname *cnp = v->a_cnp;
230 struct vattr *vap = v->a_vap;
233 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
235 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
236 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
237 cache_enter(dvp, *vpp, cnp);
242 tmpfs_mknod(struct vop_mknod_args *v)
244 struct vnode *dvp = v->a_dvp;
245 struct vnode **vpp = v->a_vpp;
246 struct componentname *cnp = v->a_cnp;
247 struct vattr *vap = v->a_vap;
249 if (vap->va_type != VBLK && vap->va_type != VCHR &&
250 vap->va_type != VFIFO)
253 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
257 tmpfs_open(struct vop_open_args *v)
259 struct vnode *vp = v->a_vp;
260 int mode = v->a_mode;
263 struct tmpfs_node *node;
265 MPASS(VOP_ISLOCKED(vp));
267 node = VP_TO_TMPFS_NODE(vp);
269 /* The file is still active but all its names have been removed
270 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
271 * it is about to die. */
272 if (node->tn_links < 1)
275 /* If the file is marked append-only, deny write requests. */
276 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
280 /* For regular files, the call below is nop. */
281 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
282 OBJ_DEAD) == 0, ("dead object"));
283 vnode_create_vobject(vp, node->tn_size, v->a_td);
286 MPASS(VOP_ISLOCKED(vp));
291 tmpfs_close(struct vop_close_args *v)
293 struct vnode *vp = v->a_vp;
295 /* Update node times. */
302 tmpfs_access(struct vop_access_args *v)
304 struct vnode *vp = v->a_vp;
305 accmode_t accmode = v->a_accmode;
306 struct ucred *cred = v->a_cred;
309 struct tmpfs_node *node;
311 MPASS(VOP_ISLOCKED(vp));
313 node = VP_TO_TMPFS_NODE(vp);
315 switch (vp->v_type) {
321 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
341 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
346 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
347 node->tn_gid, accmode, cred, NULL);
350 MPASS(VOP_ISLOCKED(vp));
356 tmpfs_getattr(struct vop_getattr_args *v)
358 struct vnode *vp = v->a_vp;
359 struct vattr *vap = v->a_vap;
361 struct tmpfs_node *node;
363 node = VP_TO_TMPFS_NODE(vp);
367 vap->va_type = vp->v_type;
368 vap->va_mode = node->tn_mode;
369 vap->va_nlink = node->tn_links;
370 vap->va_uid = node->tn_uid;
371 vap->va_gid = node->tn_gid;
372 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
373 vap->va_fileid = node->tn_id;
374 vap->va_size = node->tn_size;
375 vap->va_blocksize = PAGE_SIZE;
376 vap->va_atime = node->tn_atime;
377 vap->va_mtime = node->tn_mtime;
378 vap->va_ctime = node->tn_ctime;
379 vap->va_birthtime = node->tn_birthtime;
380 vap->va_gen = node->tn_gen;
381 vap->va_flags = node->tn_flags;
382 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
383 node->tn_rdev : NODEV;
384 vap->va_bytes = round_page(node->tn_size);
391 tmpfs_setattr(struct vop_setattr_args *v)
393 struct vnode *vp = v->a_vp;
394 struct vattr *vap = v->a_vap;
395 struct ucred *cred = v->a_cred;
396 struct thread *td = curthread;
400 MPASS(VOP_ISLOCKED(vp));
404 /* Abort if any unsettable attribute is given. */
405 if (vap->va_type != VNON ||
406 vap->va_nlink != VNOVAL ||
407 vap->va_fsid != VNOVAL ||
408 vap->va_fileid != VNOVAL ||
409 vap->va_blocksize != VNOVAL ||
410 vap->va_gen != VNOVAL ||
411 vap->va_rdev != VNOVAL ||
412 vap->va_bytes != VNOVAL)
415 if (error == 0 && (vap->va_flags != VNOVAL))
416 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
418 if (error == 0 && (vap->va_size != VNOVAL))
419 error = tmpfs_chsize(vp, vap->va_size, cred, td);
421 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
422 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
424 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
425 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
427 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
428 vap->va_atime.tv_nsec != VNOVAL) ||
429 (vap->va_mtime.tv_sec != VNOVAL &&
430 vap->va_mtime.tv_nsec != VNOVAL) ||
431 (vap->va_birthtime.tv_sec != VNOVAL &&
432 vap->va_birthtime.tv_nsec != VNOVAL)))
433 error = tmpfs_chtimes(vp, vap, cred, td);
435 /* Update the node times. We give preference to the error codes
436 * generated by this function rather than the ones that may arise
437 * from tmpfs_update. */
440 MPASS(VOP_ISLOCKED(vp));
446 tmpfs_read(struct vop_read_args *v)
450 struct tmpfs_node *node;
453 if (vp->v_type != VREG)
456 if (uio->uio_offset < 0)
458 node = VP_TO_TMPFS_NODE(vp);
459 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
460 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
464 tmpfs_write(struct vop_write_args *v)
468 struct tmpfs_node *node;
474 ioflag = v->a_ioflag;
476 node = VP_TO_TMPFS_NODE(vp);
477 oldsize = node->tn_size;
479 if (uio->uio_offset < 0 || vp->v_type != VREG)
481 if (uio->uio_resid == 0)
483 if (ioflag & IO_APPEND)
484 uio->uio_offset = node->tn_size;
485 if (uio->uio_offset + uio->uio_resid >
486 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
488 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
490 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
491 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
497 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
498 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
500 if (node->tn_mode & (S_ISUID | S_ISGID)) {
501 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
502 node->tn_mode &= ~(S_ISUID | S_ISGID);
505 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
508 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
509 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
515 tmpfs_fsync(struct vop_fsync_args *v)
517 struct vnode *vp = v->a_vp;
519 MPASS(VOP_ISLOCKED(vp));
521 tmpfs_check_mtime(vp);
528 tmpfs_remove(struct vop_remove_args *v)
530 struct vnode *dvp = v->a_dvp;
531 struct vnode *vp = v->a_vp;
534 struct tmpfs_dirent *de;
535 struct tmpfs_mount *tmp;
536 struct tmpfs_node *dnode;
537 struct tmpfs_node *node;
539 MPASS(VOP_ISLOCKED(dvp));
540 MPASS(VOP_ISLOCKED(vp));
542 if (vp->v_type == VDIR) {
547 dnode = VP_TO_TMPFS_DIR(dvp);
548 node = VP_TO_TMPFS_NODE(vp);
549 tmp = VFS_TO_TMPFS(vp->v_mount);
550 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
553 /* Files marked as immutable or append-only cannot be deleted. */
554 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
555 (dnode->tn_flags & APPEND)) {
560 /* Remove the entry from the directory; as it is a file, we do not
561 * have to change the number of hard links of the directory. */
562 tmpfs_dir_detach(dvp, de);
563 if (v->a_cnp->cn_flags & DOWHITEOUT)
564 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
566 /* Free the directory entry we just deleted. Note that the node
567 * referred by it will not be removed until the vnode is really
569 tmpfs_free_dirent(tmp, de);
571 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
580 tmpfs_link(struct vop_link_args *v)
582 struct vnode *dvp = v->a_tdvp;
583 struct vnode *vp = v->a_vp;
584 struct componentname *cnp = v->a_cnp;
587 struct tmpfs_dirent *de;
588 struct tmpfs_node *node;
590 MPASS(VOP_ISLOCKED(dvp));
591 MPASS(cnp->cn_flags & HASBUF);
592 MPASS(dvp != vp); /* XXX When can this be false? */
593 node = VP_TO_TMPFS_NODE(vp);
595 /* Ensure that we do not overflow the maximum number of links imposed
597 MPASS(node->tn_links <= LINK_MAX);
598 if (node->tn_links == LINK_MAX) {
603 /* We cannot create links of files marked immutable or append-only. */
604 if (node->tn_flags & (IMMUTABLE | APPEND)) {
609 /* Allocate a new directory entry to represent the node. */
610 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
611 cnp->cn_nameptr, cnp->cn_namelen, &de);
615 /* Insert the new directory entry into the appropriate directory. */
616 if (cnp->cn_flags & ISWHITEOUT)
617 tmpfs_dir_whiteout_remove(dvp, cnp);
618 tmpfs_dir_attach(dvp, de);
620 /* vp link count has changed, so update node times. */
621 node->tn_status |= TMPFS_NODE_CHANGED;
631 * We acquire all but fdvp locks using non-blocking acquisitions. If we
632 * fail to acquire any lock in the path we will drop all held locks,
633 * acquire the new lock in a blocking fashion, and then release it and
634 * restart the rename. This acquire/release step ensures that we do not
635 * spin on a lock waiting for release. On error release all vnode locks
636 * and decrement references the way tmpfs_rename() would do.
639 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
640 struct vnode *tdvp, struct vnode **tvpp,
641 struct componentname *fcnp, struct componentname *tcnp)
645 struct tmpfs_dirent *de;
646 int error, restarts = 0;
649 if (*tvpp != NULL && *tvpp != tdvp)
650 VOP_UNLOCK(*tvpp, 0);
655 error = vn_lock(fdvp, LK_EXCLUSIVE);
658 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
660 error = vn_lock(tdvp, LK_EXCLUSIVE);
667 * Re-resolve fvp to be certain it still exists and fetch the
670 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
674 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
675 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
681 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
687 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
692 * Concurrent rename race.
705 VOP_UNLOCK(*fvpp, 0);
707 * Re-resolve tvp and acquire the vnode lock if present.
709 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
711 * If tvp disappeared we just carry on.
713 if (de == NULL && *tvpp != NULL) {
718 * Get the tvp ino if the lookup succeeded. We may have to restart
719 * if the non-blocking acquire fails.
723 error = tmpfs_alloc_vp(mp, de->td_node,
724 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
733 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
739 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
748 tmpfs_rename_restarts += restarts;
758 tmpfs_rename_restarts += restarts;
764 tmpfs_rename(struct vop_rename_args *v)
766 struct vnode *fdvp = v->a_fdvp;
767 struct vnode *fvp = v->a_fvp;
768 struct componentname *fcnp = v->a_fcnp;
769 struct vnode *tdvp = v->a_tdvp;
770 struct vnode *tvp = v->a_tvp;
771 struct componentname *tcnp = v->a_tcnp;
772 struct mount *mp = NULL;
776 struct tmpfs_dirent *de;
777 struct tmpfs_mount *tmp;
778 struct tmpfs_node *fdnode;
779 struct tmpfs_node *fnode;
780 struct tmpfs_node *tnode;
781 struct tmpfs_node *tdnode;
783 MPASS(VOP_ISLOCKED(tdvp));
784 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
785 MPASS(fcnp->cn_flags & HASBUF);
786 MPASS(tcnp->cn_flags & HASBUF);
788 /* Disallow cross-device renames.
789 * XXX Why isn't this done by the caller? */
790 if (fvp->v_mount != tdvp->v_mount ||
791 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
796 /* If source and target are the same file, there is nothing to do. */
802 /* If we need to move the directory between entries, lock the
803 * source so that we can safely operate on it. */
804 if (fdvp != tdvp && fdvp != tvp) {
805 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
807 error = vfs_busy(mp, 0);
812 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
818 ASSERT_VOP_ELOCKED(fdvp,
819 "tmpfs_rename: fdvp not locked");
820 ASSERT_VOP_ELOCKED(tdvp,
821 "tmpfs_rename: tdvp not locked");
823 ASSERT_VOP_ELOCKED(tvp,
824 "tmpfs_rename: tvp not locked");
832 tmp = VFS_TO_TMPFS(tdvp->v_mount);
833 tdnode = VP_TO_TMPFS_DIR(tdvp);
834 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
835 fdnode = VP_TO_TMPFS_DIR(fdvp);
836 fnode = VP_TO_TMPFS_NODE(fvp);
837 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
839 /* Entry can disappear before we lock fdvp,
840 * also avoid manipulating '.' and '..' entries. */
842 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
843 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
849 MPASS(de->td_node == fnode);
851 /* If re-naming a directory to another preexisting directory
852 * ensure that the target directory is empty so that its
853 * removal causes no side effects.
854 * Kern_rename gurantees the destination to be a directory
855 * if the source is one. */
857 MPASS(tnode != NULL);
859 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
860 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
865 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
866 if (tnode->tn_size > 0) {
870 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
873 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
877 MPASS(fnode->tn_type != VDIR &&
878 tnode->tn_type != VDIR);
882 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
883 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
888 /* Ensure that we have enough memory to hold the new name, if it
889 * has to be changed. */
890 if (fcnp->cn_namelen != tcnp->cn_namelen ||
891 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
892 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
896 /* If the node is being moved to another directory, we have to do
898 if (fdnode != tdnode) {
899 /* In case we are moving a directory, we have to adjust its
900 * parent to point to the new parent. */
901 if (de->td_node->tn_type == VDIR) {
902 struct tmpfs_node *n;
904 /* Ensure the target directory is not a child of the
905 * directory being moved. Otherwise, we'd end up
906 * with stale nodes. */
908 /* TMPFS_LOCK garanties that no nodes are freed while
909 * traversing the list. Nodes can only be marked as
910 * removed: tn_parent == NULL. */
913 while (n != n->tn_dir.tn_parent) {
914 struct tmpfs_node *parent;
917 TMPFS_NODE_UNLOCK(n);
921 free(newname, M_TMPFSNAME);
924 parent = n->tn_dir.tn_parent;
925 TMPFS_NODE_UNLOCK(n);
926 if (parent == NULL) {
930 TMPFS_NODE_LOCK(parent);
931 if (parent->tn_dir.tn_parent == NULL) {
932 TMPFS_NODE_UNLOCK(parent);
942 free(newname, M_TMPFSNAME);
945 TMPFS_NODE_UNLOCK(n);
947 /* Adjust the parent pointer. */
948 TMPFS_VALIDATE_DIR(fnode);
949 TMPFS_NODE_LOCK(de->td_node);
950 de->td_node->tn_dir.tn_parent = tdnode;
951 TMPFS_NODE_UNLOCK(de->td_node);
953 /* As a result of changing the target of the '..'
954 * entry, the link count of the source and target
955 * directories has to be adjusted. */
956 TMPFS_NODE_LOCK(tdnode);
957 TMPFS_ASSERT_LOCKED(tdnode);
959 TMPFS_NODE_UNLOCK(tdnode);
961 TMPFS_NODE_LOCK(fdnode);
962 TMPFS_ASSERT_LOCKED(fdnode);
964 TMPFS_NODE_UNLOCK(fdnode);
968 /* Do the move: just remove the entry from the source directory
969 * and insert it into the target one. */
970 tmpfs_dir_detach(fdvp, de);
972 if (fcnp->cn_flags & DOWHITEOUT)
973 tmpfs_dir_whiteout_add(fdvp, fcnp);
974 if (tcnp->cn_flags & ISWHITEOUT)
975 tmpfs_dir_whiteout_remove(tdvp, tcnp);
977 /* If the name has changed, we need to make it effective by changing
978 * it in the directory entry. */
979 if (newname != NULL) {
980 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
982 free(de->ud.td_name, M_TMPFSNAME);
983 de->ud.td_name = newname;
984 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
986 fnode->tn_status |= TMPFS_NODE_CHANGED;
987 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
990 /* If we are overwriting an entry, we have to remove the old one
991 * from the target directory. */
993 struct tmpfs_dirent *tde;
995 /* Remove the old entry from the target directory. */
996 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
997 tmpfs_dir_detach(tdvp, tde);
999 /* Free the directory entry we just deleted. Note that the
1000 * node referred by it will not be removed until the vnode is
1001 * really reclaimed. */
1002 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1005 tmpfs_dir_attach(tdvp, de);
1010 cache_purge_negative(tdvp);
1015 if (fdvp != tdvp && fdvp != tvp)
1016 VOP_UNLOCK(fdvp, 0);
1019 /* Release target nodes. */
1020 /* XXX: I don't understand when tdvp can be the same as tvp, but
1021 * other code takes care of this... */
1029 /* Release source nodes. */
1040 tmpfs_mkdir(struct vop_mkdir_args *v)
1042 struct vnode *dvp = v->a_dvp;
1043 struct vnode **vpp = v->a_vpp;
1044 struct componentname *cnp = v->a_cnp;
1045 struct vattr *vap = v->a_vap;
1047 MPASS(vap->va_type == VDIR);
1049 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1053 tmpfs_rmdir(struct vop_rmdir_args *v)
1055 struct vnode *dvp = v->a_dvp;
1056 struct vnode *vp = v->a_vp;
1059 struct tmpfs_dirent *de;
1060 struct tmpfs_mount *tmp;
1061 struct tmpfs_node *dnode;
1062 struct tmpfs_node *node;
1064 MPASS(VOP_ISLOCKED(dvp));
1065 MPASS(VOP_ISLOCKED(vp));
1067 tmp = VFS_TO_TMPFS(dvp->v_mount);
1068 dnode = VP_TO_TMPFS_DIR(dvp);
1069 node = VP_TO_TMPFS_DIR(vp);
1071 /* Directories with more than two entries ('.' and '..') cannot be
1073 if (node->tn_size > 0) {
1078 if ((dnode->tn_flags & APPEND)
1079 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1084 /* This invariant holds only if we are not trying to remove "..".
1085 * We checked for that above so this is safe now. */
1086 MPASS(node->tn_dir.tn_parent == dnode);
1088 /* Get the directory entry associated with node (vp). This was
1089 * filled by tmpfs_lookup while looking up the entry. */
1090 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1091 MPASS(TMPFS_DIRENT_MATCHES(de,
1092 v->a_cnp->cn_nameptr,
1093 v->a_cnp->cn_namelen));
1095 /* Check flags to see if we are allowed to remove the directory. */
1096 if ((dnode->tn_flags & APPEND) != 0 ||
1097 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1103 /* Detach the directory entry from the directory (dnode). */
1104 tmpfs_dir_detach(dvp, de);
1105 if (v->a_cnp->cn_flags & DOWHITEOUT)
1106 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1108 /* No vnode should be allocated for this entry from this point */
1109 TMPFS_NODE_LOCK(node);
1111 node->tn_dir.tn_parent = NULL;
1112 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1113 TMPFS_NODE_MODIFIED;
1115 TMPFS_NODE_UNLOCK(node);
1117 TMPFS_NODE_LOCK(dnode);
1119 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1120 TMPFS_NODE_MODIFIED;
1121 TMPFS_NODE_UNLOCK(dnode);
1126 /* Free the directory entry we just deleted. Note that the node
1127 * referred by it will not be removed until the vnode is really
1129 tmpfs_free_dirent(tmp, de);
1131 /* Release the deleted vnode (will destroy the node, notify
1132 * interested parties and clean it from the cache). */
1134 dnode->tn_status |= TMPFS_NODE_CHANGED;
1144 tmpfs_symlink(struct vop_symlink_args *v)
1146 struct vnode *dvp = v->a_dvp;
1147 struct vnode **vpp = v->a_vpp;
1148 struct componentname *cnp = v->a_cnp;
1149 struct vattr *vap = v->a_vap;
1150 char *target = v->a_target;
1152 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1153 MPASS(vap->va_type == VLNK);
1155 vap->va_type = VLNK;
1158 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1162 tmpfs_readdir(struct vop_readdir_args *v)
1164 struct vnode *vp = v->a_vp;
1165 struct uio *uio = v->a_uio;
1166 int *eofflag = v->a_eofflag;
1167 u_long **cookies = v->a_cookies;
1168 int *ncookies = v->a_ncookies;
1173 struct tmpfs_node *node;
1175 /* This operation only makes sense on directory nodes. */
1176 if (vp->v_type != VDIR)
1180 node = VP_TO_TMPFS_DIR(vp);
1182 startresid = uio->uio_resid;
1184 /* Allocate cookies for NFS and compat modules. */
1185 if (cookies != NULL && ncookies != NULL) {
1186 maxcookies = howmany(node->tn_size,
1187 sizeof(struct tmpfs_dirent)) + 2;
1188 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1193 if (cookies == NULL)
1194 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1196 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1199 /* Buffer was filled without hitting EOF. */
1200 if (error == EJUSTRETURN)
1201 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1203 if (error != 0 && cookies != NULL && ncookies != NULL) {
1204 free(*cookies, M_TEMP);
1209 if (eofflag != NULL)
1211 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1217 tmpfs_readlink(struct vop_readlink_args *v)
1219 struct vnode *vp = v->a_vp;
1220 struct uio *uio = v->a_uio;
1223 struct tmpfs_node *node;
1225 MPASS(uio->uio_offset == 0);
1226 MPASS(vp->v_type == VLNK);
1228 node = VP_TO_TMPFS_NODE(vp);
1230 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1232 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
1238 tmpfs_inactive(struct vop_inactive_args *v)
1241 struct tmpfs_node *node;
1244 node = VP_TO_TMPFS_NODE(vp);
1245 if (node->tn_links == 0)
1248 tmpfs_check_mtime(vp);
1253 tmpfs_reclaim(struct vop_reclaim_args *v)
1255 struct vnode *vp = v->a_vp;
1257 struct tmpfs_mount *tmp;
1258 struct tmpfs_node *node;
1260 node = VP_TO_TMPFS_NODE(vp);
1261 tmp = VFS_TO_TMPFS(vp->v_mount);
1263 if (vp->v_type == VREG)
1264 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1266 vnode_destroy_vobject(vp);
1267 vp->v_object = NULL;
1270 TMPFS_NODE_LOCK(node);
1273 /* If the node referenced by this vnode was deleted by the user,
1274 * we must free its associated data structures (now that the vnode
1275 * is being reclaimed). */
1276 if (node->tn_links == 0 &&
1277 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1278 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1279 TMPFS_NODE_UNLOCK(node);
1280 tmpfs_free_node(tmp, node);
1282 TMPFS_NODE_UNLOCK(node);
1284 MPASS(vp->v_data == NULL);
1289 tmpfs_print(struct vop_print_args *v)
1291 struct vnode *vp = v->a_vp;
1293 struct tmpfs_node *node;
1295 node = VP_TO_TMPFS_NODE(vp);
1297 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1298 node, node->tn_flags, node->tn_links);
1299 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1300 node->tn_mode, node->tn_uid, node->tn_gid,
1301 (intmax_t)node->tn_size, node->tn_status);
1303 if (vp->v_type == VFIFO)
1312 tmpfs_pathconf(struct vop_pathconf_args *v)
1314 int name = v->a_name;
1315 register_t *retval = v->a_retval;
1338 case _PC_CHOWN_RESTRICTED:
1350 case _PC_FILESIZEBITS:
1351 *retval = 0; /* XXX Don't know which value should I return. */
1362 tmpfs_vptofh(struct vop_vptofh_args *ap)
1364 struct tmpfs_fid *tfhp;
1365 struct tmpfs_node *node;
1367 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1368 node = VP_TO_TMPFS_NODE(ap->a_vp);
1370 tfhp->tf_len = sizeof(struct tmpfs_fid);
1371 tfhp->tf_id = node->tn_id;
1372 tfhp->tf_gen = node->tn_gen;
1378 tmpfs_whiteout(struct vop_whiteout_args *ap)
1380 struct vnode *dvp = ap->a_dvp;
1381 struct componentname *cnp = ap->a_cnp;
1382 struct tmpfs_dirent *de;
1384 switch (ap->a_flags) {
1388 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1390 return (de->td_node == NULL ? 0 : EEXIST);
1391 return (tmpfs_dir_whiteout_add(dvp, cnp));
1393 tmpfs_dir_whiteout_remove(dvp, cnp);
1396 panic("tmpfs_whiteout: unknown op");
1401 * Vnode operations vector used for files stored in a tmpfs file system.
1403 struct vop_vector tmpfs_vnodeop_entries = {
1404 .vop_default = &default_vnodeops,
1405 .vop_lookup = vfs_cache_lookup,
1406 .vop_cachedlookup = tmpfs_lookup,
1407 .vop_create = tmpfs_create,
1408 .vop_mknod = tmpfs_mknod,
1409 .vop_open = tmpfs_open,
1410 .vop_close = tmpfs_close,
1411 .vop_access = tmpfs_access,
1412 .vop_getattr = tmpfs_getattr,
1413 .vop_setattr = tmpfs_setattr,
1414 .vop_read = tmpfs_read,
1415 .vop_write = tmpfs_write,
1416 .vop_fsync = tmpfs_fsync,
1417 .vop_remove = tmpfs_remove,
1418 .vop_link = tmpfs_link,
1419 .vop_rename = tmpfs_rename,
1420 .vop_mkdir = tmpfs_mkdir,
1421 .vop_rmdir = tmpfs_rmdir,
1422 .vop_symlink = tmpfs_symlink,
1423 .vop_readdir = tmpfs_readdir,
1424 .vop_readlink = tmpfs_readlink,
1425 .vop_inactive = tmpfs_inactive,
1426 .vop_reclaim = tmpfs_reclaim,
1427 .vop_print = tmpfs_print,
1428 .vop_pathconf = tmpfs_pathconf,
1429 .vop_vptofh = tmpfs_vptofh,
1430 .vop_whiteout = tmpfs_whiteout,
1431 .vop_bmap = VOP_EOPNOTSUPP,