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>
48 #include <sys/sf_buf.h>
50 #include <sys/systm.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
56 #include <vm/vm_param.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
61 #include <fs/tmpfs/tmpfs_vnops.h>
62 #include <fs/tmpfs/tmpfs.h>
64 SYSCTL_DECL(_vfs_tmpfs);
66 static volatile int tmpfs_rename_restarts;
67 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
68 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
69 "Times rename had to restart due to lock contention");
71 /* --------------------------------------------------------------------- */
74 tmpfs_lookup(struct vop_cachedlookup_args *v)
76 struct vnode *dvp = v->a_dvp;
77 struct vnode **vpp = v->a_vpp;
78 struct componentname *cnp = v->a_cnp;
81 struct tmpfs_dirent *de;
82 struct tmpfs_node *dnode;
84 dnode = VP_TO_TMPFS_DIR(dvp);
87 /* Check accessibility of requested node as a first step. */
88 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
92 /* We cannot be requesting the parent directory of the root node. */
93 MPASS(IMPLIES(dnode->tn_type == VDIR &&
94 dnode->tn_dir.tn_parent == dnode,
95 !(cnp->cn_flags & ISDOTDOT)));
97 TMPFS_ASSERT_LOCKED(dnode);
98 if (dnode->tn_dir.tn_parent == NULL) {
102 if (cnp->cn_flags & ISDOTDOT) {
105 ltype = VOP_ISLOCKED(dvp);
108 /* Allocate a new vnode on the matching entry. */
109 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
110 cnp->cn_lkflags, vpp);
112 vn_lock(dvp, ltype | LK_RETRY);
114 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
119 de = tmpfs_dir_lookup(dnode, NULL, cnp);
120 if (de != NULL && de->td_node == NULL)
121 cnp->cn_flags |= ISWHITEOUT;
122 if (de == NULL || de->td_node == NULL) {
123 /* The entry was not found in the directory.
124 * This is OK if we are creating or renaming an
125 * entry and are working on the last component of
127 if ((cnp->cn_flags & ISLASTCN) &&
128 (cnp->cn_nameiop == CREATE || \
129 cnp->cn_nameiop == RENAME ||
130 (cnp->cn_nameiop == DELETE &&
131 cnp->cn_flags & DOWHITEOUT &&
132 cnp->cn_flags & ISWHITEOUT))) {
133 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
138 /* Keep the component name in the buffer for
140 cnp->cn_flags |= SAVENAME;
146 struct tmpfs_node *tnode;
148 /* The entry was found, so get its associated
152 /* If we are not at the last path component and
153 * found a non-directory or non-link entry (which
154 * may itself be pointing to a directory), raise
156 if ((tnode->tn_type != VDIR &&
157 tnode->tn_type != VLNK) &&
158 !(cnp->cn_flags & ISLASTCN)) {
163 /* If we are deleting or renaming the entry, keep
164 * track of its tmpfs_dirent so that it can be
165 * easily deleted later. */
166 if ((cnp->cn_flags & ISLASTCN) &&
167 (cnp->cn_nameiop == DELETE ||
168 cnp->cn_nameiop == RENAME)) {
169 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
174 /* Allocate a new vnode on the matching entry. */
175 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
176 cnp->cn_lkflags, vpp);
180 if ((dnode->tn_mode & S_ISTXT) &&
181 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
182 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
188 cnp->cn_flags |= SAVENAME;
190 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
191 cnp->cn_lkflags, vpp);
196 /* Store the result of this lookup in the cache. Avoid this if the
197 * request was for creation, as it does not improve timings on
199 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
200 cache_enter(dvp, *vpp, cnp);
203 /* If there were no errors, *vpp cannot be null and it must be
205 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
210 /* --------------------------------------------------------------------- */
213 tmpfs_create(struct vop_create_args *v)
215 struct vnode *dvp = v->a_dvp;
216 struct vnode **vpp = v->a_vpp;
217 struct componentname *cnp = v->a_cnp;
218 struct vattr *vap = v->a_vap;
220 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
222 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
224 /* --------------------------------------------------------------------- */
227 tmpfs_mknod(struct vop_mknod_args *v)
229 struct vnode *dvp = v->a_dvp;
230 struct vnode **vpp = v->a_vpp;
231 struct componentname *cnp = v->a_cnp;
232 struct vattr *vap = v->a_vap;
234 if (vap->va_type != VBLK && vap->va_type != VCHR &&
235 vap->va_type != VFIFO)
238 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
241 /* --------------------------------------------------------------------- */
244 tmpfs_open(struct vop_open_args *v)
246 struct vnode *vp = v->a_vp;
247 int mode = v->a_mode;
250 struct tmpfs_node *node;
252 MPASS(VOP_ISLOCKED(vp));
254 node = VP_TO_TMPFS_NODE(vp);
256 /* The file is still active but all its names have been removed
257 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
258 * it is about to die. */
259 if (node->tn_links < 1)
262 /* If the file is marked append-only, deny write requests. */
263 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
267 /* For regular files, the call below is nop. */
268 vnode_create_vobject(vp, node->tn_size, v->a_td);
271 MPASS(VOP_ISLOCKED(vp));
275 /* --------------------------------------------------------------------- */
278 tmpfs_close(struct vop_close_args *v)
280 struct vnode *vp = v->a_vp;
282 /* Update node times. */
288 /* --------------------------------------------------------------------- */
291 tmpfs_access(struct vop_access_args *v)
293 struct vnode *vp = v->a_vp;
294 accmode_t accmode = v->a_accmode;
295 struct ucred *cred = v->a_cred;
298 struct tmpfs_node *node;
300 MPASS(VOP_ISLOCKED(vp));
302 node = VP_TO_TMPFS_NODE(vp);
304 switch (vp->v_type) {
310 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
330 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
335 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
336 node->tn_gid, accmode, cred, NULL);
339 MPASS(VOP_ISLOCKED(vp));
344 /* --------------------------------------------------------------------- */
347 tmpfs_getattr(struct vop_getattr_args *v)
349 struct vnode *vp = v->a_vp;
350 struct vattr *vap = v->a_vap;
352 struct tmpfs_node *node;
354 node = VP_TO_TMPFS_NODE(vp);
358 vap->va_type = vp->v_type;
359 vap->va_mode = node->tn_mode;
360 vap->va_nlink = node->tn_links;
361 vap->va_uid = node->tn_uid;
362 vap->va_gid = node->tn_gid;
363 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
364 vap->va_fileid = node->tn_id;
365 vap->va_size = node->tn_size;
366 vap->va_blocksize = PAGE_SIZE;
367 vap->va_atime = node->tn_atime;
368 vap->va_mtime = node->tn_mtime;
369 vap->va_ctime = node->tn_ctime;
370 vap->va_birthtime = node->tn_birthtime;
371 vap->va_gen = node->tn_gen;
372 vap->va_flags = node->tn_flags;
373 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
374 node->tn_rdev : NODEV;
375 vap->va_bytes = round_page(node->tn_size);
382 tmpfs_setattr(struct vop_setattr_args *v)
384 struct vnode *vp = v->a_vp;
385 struct vattr *vap = v->a_vap;
386 struct ucred *cred = v->a_cred;
387 struct thread *td = curthread;
391 MPASS(VOP_ISLOCKED(vp));
395 /* Abort if any unsettable attribute is given. */
396 if (vap->va_type != VNON ||
397 vap->va_nlink != VNOVAL ||
398 vap->va_fsid != VNOVAL ||
399 vap->va_fileid != VNOVAL ||
400 vap->va_blocksize != VNOVAL ||
401 vap->va_gen != VNOVAL ||
402 vap->va_rdev != VNOVAL ||
403 vap->va_bytes != VNOVAL)
406 if (error == 0 && (vap->va_flags != VNOVAL))
407 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
409 if (error == 0 && (vap->va_size != VNOVAL))
410 error = tmpfs_chsize(vp, vap->va_size, cred, td);
412 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
413 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
415 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
416 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
418 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
419 vap->va_atime.tv_nsec != VNOVAL) ||
420 (vap->va_mtime.tv_sec != VNOVAL &&
421 vap->va_mtime.tv_nsec != VNOVAL) ||
422 (vap->va_birthtime.tv_sec != VNOVAL &&
423 vap->va_birthtime.tv_nsec != VNOVAL)))
424 error = tmpfs_chtimes(vp, vap, cred, td);
426 /* Update the node times. We give preference to the error codes
427 * generated by this function rather than the ones that may arise
428 * from tmpfs_update. */
431 MPASS(VOP_ISLOCKED(vp));
437 tmpfs_read(struct vop_read_args *v)
441 struct tmpfs_node *node;
444 if (vp->v_type != VREG)
447 if (uio->uio_offset < 0)
449 node = VP_TO_TMPFS_NODE(vp);
450 node->tn_status |= TMPFS_NODE_ACCESSED;
451 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
455 tmpfs_write(struct vop_write_args *v)
459 struct tmpfs_node *node;
466 ioflag = v->a_ioflag;
468 node = VP_TO_TMPFS_NODE(vp);
469 oldsize = node->tn_size;
471 if (uio->uio_offset < 0 || vp->v_type != VREG)
473 if (uio->uio_resid == 0)
475 if (ioflag & IO_APPEND)
476 uio->uio_offset = node->tn_size;
477 if (uio->uio_offset + uio->uio_resid >
478 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
480 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
482 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
484 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
490 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
491 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
492 (extended ? TMPFS_NODE_CHANGED : 0);
493 if (node->tn_mode & (S_ISUID | S_ISGID)) {
494 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
495 node->tn_mode &= ~(S_ISUID | S_ISGID);
498 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
501 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
502 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
507 /* --------------------------------------------------------------------- */
510 tmpfs_fsync(struct vop_fsync_args *v)
512 struct vnode *vp = v->a_vp;
514 MPASS(VOP_ISLOCKED(vp));
521 /* --------------------------------------------------------------------- */
524 tmpfs_remove(struct vop_remove_args *v)
526 struct vnode *dvp = v->a_dvp;
527 struct vnode *vp = v->a_vp;
530 struct tmpfs_dirent *de;
531 struct tmpfs_mount *tmp;
532 struct tmpfs_node *dnode;
533 struct tmpfs_node *node;
535 MPASS(VOP_ISLOCKED(dvp));
536 MPASS(VOP_ISLOCKED(vp));
538 if (vp->v_type == VDIR) {
543 dnode = VP_TO_TMPFS_DIR(dvp);
544 node = VP_TO_TMPFS_NODE(vp);
545 tmp = VFS_TO_TMPFS(vp->v_mount);
546 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
549 /* Files marked as immutable or append-only cannot be deleted. */
550 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
551 (dnode->tn_flags & APPEND)) {
556 /* Remove the entry from the directory; as it is a file, we do not
557 * have to change the number of hard links of the directory. */
558 tmpfs_dir_detach(dvp, de);
559 if (v->a_cnp->cn_flags & DOWHITEOUT)
560 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
562 /* Free the directory entry we just deleted. Note that the node
563 * referred by it will not be removed until the vnode is really
565 tmpfs_free_dirent(tmp, de);
567 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
575 /* --------------------------------------------------------------------- */
578 tmpfs_link(struct vop_link_args *v)
580 struct vnode *dvp = v->a_tdvp;
581 struct vnode *vp = v->a_vp;
582 struct componentname *cnp = v->a_cnp;
585 struct tmpfs_dirent *de;
586 struct tmpfs_node *node;
588 MPASS(VOP_ISLOCKED(dvp));
589 MPASS(cnp->cn_flags & HASBUF);
590 MPASS(dvp != vp); /* XXX When can this be false? */
592 node = VP_TO_TMPFS_NODE(vp);
594 /* XXX: Why aren't the following two tests done by the caller? */
596 /* Hard links of directories are forbidden. */
597 if (vp->v_type == VDIR) {
602 /* Cannot create cross-device links. */
603 if (dvp->v_mount != vp->v_mount) {
608 /* Ensure that we do not overflow the maximum number of links imposed
610 MPASS(node->tn_links <= LINK_MAX);
611 if (node->tn_links == LINK_MAX) {
616 /* We cannot create links of files marked immutable or append-only. */
617 if (node->tn_flags & (IMMUTABLE | APPEND)) {
622 /* Allocate a new directory entry to represent the node. */
623 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
624 cnp->cn_nameptr, cnp->cn_namelen, &de);
628 /* Insert the new directory entry into the appropriate directory. */
629 if (cnp->cn_flags & ISWHITEOUT)
630 tmpfs_dir_whiteout_remove(dvp, cnp);
631 tmpfs_dir_attach(dvp, de);
633 /* vp link count has changed, so update node times. */
634 node->tn_status |= TMPFS_NODE_CHANGED;
643 /* --------------------------------------------------------------------- */
646 * We acquire all but fdvp locks using non-blocking acquisitions. If we
647 * fail to acquire any lock in the path we will drop all held locks,
648 * acquire the new lock in a blocking fashion, and then release it and
649 * restart the rename. This acquire/release step ensures that we do not
650 * spin on a lock waiting for release. On error release all vnode locks
651 * and decrement references the way tmpfs_rename() would do.
654 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
655 struct vnode *tdvp, struct vnode **tvpp,
656 struct componentname *fcnp, struct componentname *tcnp)
660 struct tmpfs_dirent *de;
661 int error, restarts = 0;
664 if (*tvpp != NULL && *tvpp != tdvp)
665 VOP_UNLOCK(*tvpp, 0);
670 error = vn_lock(fdvp, LK_EXCLUSIVE);
673 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
675 error = vn_lock(tdvp, LK_EXCLUSIVE);
682 * Re-resolve fvp to be certain it still exists and fetch the
685 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
689 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
690 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
696 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
702 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
707 * Concurrent rename race.
720 VOP_UNLOCK(*fvpp, 0);
722 * Re-resolve tvp and acquire the vnode lock if present.
724 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
726 * If tvp disappeared we just carry on.
728 if (de == NULL && *tvpp != NULL) {
733 * Get the tvp ino if the lookup succeeded. We may have to restart
734 * if the non-blocking acquire fails.
738 error = tmpfs_alloc_vp(mp, de->td_node,
739 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
748 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
754 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
763 tmpfs_rename_restarts += restarts;
773 tmpfs_rename_restarts += restarts;
779 tmpfs_rename(struct vop_rename_args *v)
781 struct vnode *fdvp = v->a_fdvp;
782 struct vnode *fvp = v->a_fvp;
783 struct componentname *fcnp = v->a_fcnp;
784 struct vnode *tdvp = v->a_tdvp;
785 struct vnode *tvp = v->a_tvp;
786 struct componentname *tcnp = v->a_tcnp;
787 struct mount *mp = NULL;
791 struct tmpfs_dirent *de;
792 struct tmpfs_mount *tmp;
793 struct tmpfs_node *fdnode;
794 struct tmpfs_node *fnode;
795 struct tmpfs_node *tnode;
796 struct tmpfs_node *tdnode;
798 MPASS(VOP_ISLOCKED(tdvp));
799 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
800 MPASS(fcnp->cn_flags & HASBUF);
801 MPASS(tcnp->cn_flags & HASBUF);
803 /* Disallow cross-device renames.
804 * XXX Why isn't this done by the caller? */
805 if (fvp->v_mount != tdvp->v_mount ||
806 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
811 /* If source and target are the same file, there is nothing to do. */
817 /* If we need to move the directory between entries, lock the
818 * source so that we can safely operate on it. */
819 if (fdvp != tdvp && fdvp != tvp) {
820 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
822 error = vfs_busy(mp, 0);
827 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
833 ASSERT_VOP_ELOCKED(fdvp,
834 "tmpfs_rename: fdvp not locked");
835 ASSERT_VOP_ELOCKED(tdvp,
836 "tmpfs_rename: tdvp not locked");
838 ASSERT_VOP_ELOCKED(tvp,
839 "tmpfs_rename: tvp not locked");
847 tmp = VFS_TO_TMPFS(tdvp->v_mount);
848 tdnode = VP_TO_TMPFS_DIR(tdvp);
849 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
850 fdnode = VP_TO_TMPFS_DIR(fdvp);
851 fnode = VP_TO_TMPFS_NODE(fvp);
852 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
854 /* Entry can disappear before we lock fdvp,
855 * also avoid manipulating '.' and '..' entries. */
857 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
858 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
864 MPASS(de->td_node == fnode);
866 /* If re-naming a directory to another preexisting directory
867 * ensure that the target directory is empty so that its
868 * removal causes no side effects.
869 * Kern_rename gurantees the destination to be a directory
870 * if the source is one. */
872 MPASS(tnode != NULL);
874 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
875 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
880 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
881 if (tnode->tn_size > 0) {
885 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
888 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
892 MPASS(fnode->tn_type != VDIR &&
893 tnode->tn_type != VDIR);
897 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
898 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
903 /* Ensure that we have enough memory to hold the new name, if it
904 * has to be changed. */
905 if (fcnp->cn_namelen != tcnp->cn_namelen ||
906 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
907 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
911 /* If the node is being moved to another directory, we have to do
913 if (fdnode != tdnode) {
914 /* In case we are moving a directory, we have to adjust its
915 * parent to point to the new parent. */
916 if (de->td_node->tn_type == VDIR) {
917 struct tmpfs_node *n;
919 /* Ensure the target directory is not a child of the
920 * directory being moved. Otherwise, we'd end up
921 * with stale nodes. */
923 /* TMPFS_LOCK garanties that no nodes are freed while
924 * traversing the list. Nodes can only be marked as
925 * removed: tn_parent == NULL. */
928 while (n != n->tn_dir.tn_parent) {
929 struct tmpfs_node *parent;
932 TMPFS_NODE_UNLOCK(n);
936 free(newname, M_TMPFSNAME);
939 parent = n->tn_dir.tn_parent;
940 TMPFS_NODE_UNLOCK(n);
941 if (parent == NULL) {
945 TMPFS_NODE_LOCK(parent);
946 if (parent->tn_dir.tn_parent == NULL) {
947 TMPFS_NODE_UNLOCK(parent);
957 free(newname, M_TMPFSNAME);
960 TMPFS_NODE_UNLOCK(n);
962 /* Adjust the parent pointer. */
963 TMPFS_VALIDATE_DIR(fnode);
964 TMPFS_NODE_LOCK(de->td_node);
965 de->td_node->tn_dir.tn_parent = tdnode;
966 TMPFS_NODE_UNLOCK(de->td_node);
968 /* As a result of changing the target of the '..'
969 * entry, the link count of the source and target
970 * directories has to be adjusted. */
971 TMPFS_NODE_LOCK(tdnode);
972 TMPFS_ASSERT_LOCKED(tdnode);
974 TMPFS_NODE_UNLOCK(tdnode);
976 TMPFS_NODE_LOCK(fdnode);
977 TMPFS_ASSERT_LOCKED(fdnode);
979 TMPFS_NODE_UNLOCK(fdnode);
983 /* Do the move: just remove the entry from the source directory
984 * and insert it into the target one. */
985 tmpfs_dir_detach(fdvp, de);
987 if (fcnp->cn_flags & DOWHITEOUT)
988 tmpfs_dir_whiteout_add(fdvp, fcnp);
989 if (tcnp->cn_flags & ISWHITEOUT)
990 tmpfs_dir_whiteout_remove(tdvp, tcnp);
992 /* If the name has changed, we need to make it effective by changing
993 * it in the directory entry. */
994 if (newname != NULL) {
995 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
997 free(de->ud.td_name, M_TMPFSNAME);
998 de->ud.td_name = newname;
999 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1001 fnode->tn_status |= TMPFS_NODE_CHANGED;
1002 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1005 /* If we are overwriting an entry, we have to remove the old one
1006 * from the target directory. */
1008 struct tmpfs_dirent *tde;
1010 /* Remove the old entry from the target directory. */
1011 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1012 tmpfs_dir_detach(tdvp, tde);
1014 /* Free the directory entry we just deleted. Note that the
1015 * node referred by it will not be removed until the vnode is
1016 * really reclaimed. */
1017 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1020 tmpfs_dir_attach(tdvp, de);
1025 cache_purge_negative(tdvp);
1030 if (fdvp != tdvp && fdvp != tvp)
1031 VOP_UNLOCK(fdvp, 0);
1034 /* Release target nodes. */
1035 /* XXX: I don't understand when tdvp can be the same as tvp, but
1036 * other code takes care of this... */
1044 /* Release source nodes. */
1054 /* --------------------------------------------------------------------- */
1057 tmpfs_mkdir(struct vop_mkdir_args *v)
1059 struct vnode *dvp = v->a_dvp;
1060 struct vnode **vpp = v->a_vpp;
1061 struct componentname *cnp = v->a_cnp;
1062 struct vattr *vap = v->a_vap;
1064 MPASS(vap->va_type == VDIR);
1066 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1069 /* --------------------------------------------------------------------- */
1072 tmpfs_rmdir(struct vop_rmdir_args *v)
1074 struct vnode *dvp = v->a_dvp;
1075 struct vnode *vp = v->a_vp;
1078 struct tmpfs_dirent *de;
1079 struct tmpfs_mount *tmp;
1080 struct tmpfs_node *dnode;
1081 struct tmpfs_node *node;
1083 MPASS(VOP_ISLOCKED(dvp));
1084 MPASS(VOP_ISLOCKED(vp));
1086 tmp = VFS_TO_TMPFS(dvp->v_mount);
1087 dnode = VP_TO_TMPFS_DIR(dvp);
1088 node = VP_TO_TMPFS_DIR(vp);
1090 /* Directories with more than two entries ('.' and '..') cannot be
1092 if (node->tn_size > 0) {
1097 if ((dnode->tn_flags & APPEND)
1098 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1103 /* This invariant holds only if we are not trying to remove "..".
1104 * We checked for that above so this is safe now. */
1105 MPASS(node->tn_dir.tn_parent == dnode);
1107 /* Get the directory entry associated with node (vp). This was
1108 * filled by tmpfs_lookup while looking up the entry. */
1109 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1110 MPASS(TMPFS_DIRENT_MATCHES(de,
1111 v->a_cnp->cn_nameptr,
1112 v->a_cnp->cn_namelen));
1114 /* Check flags to see if we are allowed to remove the directory. */
1115 if (dnode->tn_flags & APPEND
1116 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1122 /* Detach the directory entry from the directory (dnode). */
1123 tmpfs_dir_detach(dvp, de);
1124 if (v->a_cnp->cn_flags & DOWHITEOUT)
1125 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1127 /* No vnode should be allocated for this entry from this point */
1128 TMPFS_NODE_LOCK(node);
1129 TMPFS_ASSERT_ELOCKED(node);
1131 node->tn_dir.tn_parent = NULL;
1132 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1133 TMPFS_NODE_MODIFIED;
1135 TMPFS_NODE_UNLOCK(node);
1137 TMPFS_NODE_LOCK(dnode);
1138 TMPFS_ASSERT_ELOCKED(dnode);
1140 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1141 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1142 TMPFS_NODE_UNLOCK(dnode);
1147 /* Free the directory entry we just deleted. Note that the node
1148 * referred by it will not be removed until the vnode is really
1150 tmpfs_free_dirent(tmp, de);
1152 /* Release the deleted vnode (will destroy the node, notify
1153 * interested parties and clean it from the cache). */
1155 dnode->tn_status |= TMPFS_NODE_CHANGED;
1164 /* --------------------------------------------------------------------- */
1167 tmpfs_symlink(struct vop_symlink_args *v)
1169 struct vnode *dvp = v->a_dvp;
1170 struct vnode **vpp = v->a_vpp;
1171 struct componentname *cnp = v->a_cnp;
1172 struct vattr *vap = v->a_vap;
1173 char *target = v->a_target;
1175 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1176 MPASS(vap->va_type == VLNK);
1178 vap->va_type = VLNK;
1181 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1184 /* --------------------------------------------------------------------- */
1187 tmpfs_readdir(struct vop_readdir_args *v)
1189 struct vnode *vp = v->a_vp;
1190 struct uio *uio = v->a_uio;
1191 int *eofflag = v->a_eofflag;
1192 u_long **cookies = v->a_cookies;
1193 int *ncookies = v->a_ncookies;
1198 struct tmpfs_node *node;
1200 /* This operation only makes sense on directory nodes. */
1201 if (vp->v_type != VDIR)
1205 node = VP_TO_TMPFS_DIR(vp);
1207 startresid = uio->uio_resid;
1209 /* Allocate cookies for NFS and compat modules. */
1210 if (cookies != NULL && ncookies != NULL) {
1211 maxcookies = howmany(node->tn_size,
1212 sizeof(struct tmpfs_dirent)) + 2;
1213 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1218 if (cookies == NULL)
1219 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1221 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1224 /* Buffer was filled without hitting EOF. */
1225 if (error == EJUSTRETURN)
1226 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1228 if (error != 0 && cookies != NULL)
1229 free(*cookies, M_TEMP);
1231 if (eofflag != NULL)
1233 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1238 /* --------------------------------------------------------------------- */
1241 tmpfs_readlink(struct vop_readlink_args *v)
1243 struct vnode *vp = v->a_vp;
1244 struct uio *uio = v->a_uio;
1247 struct tmpfs_node *node;
1249 MPASS(uio->uio_offset == 0);
1250 MPASS(vp->v_type == VLNK);
1252 node = VP_TO_TMPFS_NODE(vp);
1254 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1256 node->tn_status |= TMPFS_NODE_ACCESSED;
1261 /* --------------------------------------------------------------------- */
1264 tmpfs_inactive(struct vop_inactive_args *v)
1266 struct vnode *vp = v->a_vp;
1268 struct tmpfs_node *node;
1270 node = VP_TO_TMPFS_NODE(vp);
1272 if (node->tn_links == 0)
1278 /* --------------------------------------------------------------------- */
1281 tmpfs_reclaim(struct vop_reclaim_args *v)
1283 struct vnode *vp = v->a_vp;
1285 struct tmpfs_mount *tmp;
1286 struct tmpfs_node *node;
1288 node = VP_TO_TMPFS_NODE(vp);
1289 tmp = VFS_TO_TMPFS(vp->v_mount);
1291 if (vp->v_type == VREG)
1292 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1294 vnode_destroy_vobject(vp);
1295 vp->v_object = NULL;
1298 TMPFS_NODE_LOCK(node);
1299 TMPFS_ASSERT_ELOCKED(node);
1302 /* If the node referenced by this vnode was deleted by the user,
1303 * we must free its associated data structures (now that the vnode
1304 * is being reclaimed). */
1305 if (node->tn_links == 0 &&
1306 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1307 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1308 TMPFS_NODE_UNLOCK(node);
1309 tmpfs_free_node(tmp, node);
1311 TMPFS_NODE_UNLOCK(node);
1313 MPASS(vp->v_data == NULL);
1317 /* --------------------------------------------------------------------- */
1320 tmpfs_print(struct vop_print_args *v)
1322 struct vnode *vp = v->a_vp;
1324 struct tmpfs_node *node;
1326 node = VP_TO_TMPFS_NODE(vp);
1328 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1329 node, node->tn_flags, node->tn_links);
1330 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1331 node->tn_mode, node->tn_uid, node->tn_gid,
1332 (intmax_t)node->tn_size, node->tn_status);
1334 if (vp->v_type == VFIFO)
1342 /* --------------------------------------------------------------------- */
1345 tmpfs_pathconf(struct vop_pathconf_args *v)
1347 int name = v->a_name;
1348 register_t *retval = v->a_retval;
1371 case _PC_CHOWN_RESTRICTED:
1383 case _PC_FILESIZEBITS:
1384 *retval = 0; /* XXX Don't know which value should I return. */
1395 tmpfs_vptofh(struct vop_vptofh_args *ap)
1397 struct tmpfs_fid *tfhp;
1398 struct tmpfs_node *node;
1400 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1401 node = VP_TO_TMPFS_NODE(ap->a_vp);
1403 tfhp->tf_len = sizeof(struct tmpfs_fid);
1404 tfhp->tf_id = node->tn_id;
1405 tfhp->tf_gen = node->tn_gen;
1411 tmpfs_whiteout(struct vop_whiteout_args *ap)
1413 struct vnode *dvp = ap->a_dvp;
1414 struct componentname *cnp = ap->a_cnp;
1415 struct tmpfs_dirent *de;
1417 switch (ap->a_flags) {
1421 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1423 return (de->td_node == NULL ? 0 : EEXIST);
1424 return (tmpfs_dir_whiteout_add(dvp, cnp));
1426 tmpfs_dir_whiteout_remove(dvp, cnp);
1429 panic("tmpfs_whiteout: unknown op");
1433 /* --------------------------------------------------------------------- */
1436 * vnode operations vector used for files stored in a tmpfs file system.
1438 struct vop_vector tmpfs_vnodeop_entries = {
1439 .vop_default = &default_vnodeops,
1440 .vop_lookup = vfs_cache_lookup,
1441 .vop_cachedlookup = tmpfs_lookup,
1442 .vop_create = tmpfs_create,
1443 .vop_mknod = tmpfs_mknod,
1444 .vop_open = tmpfs_open,
1445 .vop_close = tmpfs_close,
1446 .vop_access = tmpfs_access,
1447 .vop_getattr = tmpfs_getattr,
1448 .vop_setattr = tmpfs_setattr,
1449 .vop_read = tmpfs_read,
1450 .vop_write = tmpfs_write,
1451 .vop_fsync = tmpfs_fsync,
1452 .vop_remove = tmpfs_remove,
1453 .vop_link = tmpfs_link,
1454 .vop_rename = tmpfs_rename,
1455 .vop_mkdir = tmpfs_mkdir,
1456 .vop_rmdir = tmpfs_rmdir,
1457 .vop_symlink = tmpfs_symlink,
1458 .vop_readdir = tmpfs_readdir,
1459 .vop_readlink = tmpfs_readlink,
1460 .vop_inactive = tmpfs_inactive,
1461 .vop_reclaim = tmpfs_reclaim,
1462 .vop_print = tmpfs_print,
1463 .vop_pathconf = tmpfs_pathconf,
1464 .vop_vptofh = tmpfs_vptofh,
1465 .vop_whiteout = tmpfs_whiteout,
1466 .vop_bmap = VOP_EOPNOTSUPP,