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, *pnode;
86 struct tmpfs_mount *tm;
89 dnode = VP_TO_TMPFS_DIR(dvp);
92 /* Check accessibility of requested node as a first step. */
93 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
97 /* We cannot be requesting the parent directory of the root node. */
98 MPASS(IMPLIES(dnode->tn_type == VDIR &&
99 dnode->tn_dir.tn_parent == dnode,
100 !(cnp->cn_flags & ISDOTDOT)));
102 TMPFS_ASSERT_LOCKED(dnode);
103 if (dnode->tn_dir.tn_parent == NULL) {
107 if (cnp->cn_flags & ISDOTDOT) {
108 tm = VFS_TO_TMPFS(dvp->v_mount);
109 pnode = dnode->tn_dir.tn_parent;
110 tmpfs_ref_node(pnode);
111 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
112 pnode, cnp->cn_lkflags, vpp);
113 tmpfs_free_node(tm, pnode);
116 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
121 de = tmpfs_dir_lookup(dnode, NULL, cnp);
122 if (de != NULL && de->td_node == NULL)
123 cnp->cn_flags |= ISWHITEOUT;
124 if (de == NULL || de->td_node == NULL) {
126 * The entry was not found in the directory.
127 * This is OK if we are creating or renaming an
128 * entry and are working on the last component of
131 if ((cnp->cn_flags & ISLASTCN) &&
132 (cnp->cn_nameiop == CREATE || \
133 cnp->cn_nameiop == RENAME ||
134 (cnp->cn_nameiop == DELETE &&
135 cnp->cn_flags & DOWHITEOUT &&
136 cnp->cn_flags & ISWHITEOUT))) {
137 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
143 * Keep the component name in the buffer for
146 cnp->cn_flags |= SAVENAME;
152 struct tmpfs_node *tnode;
155 * The entry was found, so get its associated
161 * If we are not at the last path component and
162 * found a non-directory or non-link entry (which
163 * may itself be pointing to a directory), raise
166 if ((tnode->tn_type != VDIR &&
167 tnode->tn_type != VLNK) &&
168 !(cnp->cn_flags & ISLASTCN)) {
174 * If we are deleting or renaming the entry, keep
175 * track of its tmpfs_dirent so that it can be
176 * easily deleted later.
178 if ((cnp->cn_flags & ISLASTCN) &&
179 (cnp->cn_nameiop == DELETE ||
180 cnp->cn_nameiop == RENAME)) {
181 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
186 /* Allocate a new vnode on the matching entry. */
187 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
188 cnp->cn_lkflags, vpp);
192 if ((dnode->tn_mode & S_ISTXT) &&
193 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
194 cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
195 cnp->cn_cred, cnp->cn_thread)) {
201 cnp->cn_flags |= SAVENAME;
203 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
204 cnp->cn_lkflags, vpp);
212 * Store the result of this lookup in the cache. Avoid this if the
213 * request was for creation, as it does not improve timings on
216 if ((cnp->cn_flags & MAKEENTRY) != 0)
217 cache_enter(dvp, *vpp, cnp);
221 * If there were no errors, *vpp cannot be null and it must be
224 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
230 tmpfs_create(struct vop_create_args *v)
232 struct vnode *dvp = v->a_dvp;
233 struct vnode **vpp = v->a_vpp;
234 struct componentname *cnp = v->a_cnp;
235 struct vattr *vap = v->a_vap;
238 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
240 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
241 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
242 cache_enter(dvp, *vpp, cnp);
247 tmpfs_mknod(struct vop_mknod_args *v)
249 struct vnode *dvp = v->a_dvp;
250 struct vnode **vpp = v->a_vpp;
251 struct componentname *cnp = v->a_cnp;
252 struct vattr *vap = v->a_vap;
254 if (vap->va_type != VBLK && vap->va_type != VCHR &&
255 vap->va_type != VFIFO)
258 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
262 tmpfs_open(struct vop_open_args *v)
264 struct vnode *vp = v->a_vp;
265 int mode = v->a_mode;
268 struct tmpfs_node *node;
270 MPASS(VOP_ISLOCKED(vp));
272 node = VP_TO_TMPFS_NODE(vp);
274 /* The file is still active but all its names have been removed
275 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
276 * it is about to die. */
277 if (node->tn_links < 1)
280 /* If the file is marked append-only, deny write requests. */
281 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
285 /* For regular files, the call below is nop. */
286 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
287 OBJ_DEAD) == 0, ("dead object"));
288 vnode_create_vobject(vp, node->tn_size, v->a_td);
291 MPASS(VOP_ISLOCKED(vp));
296 tmpfs_close(struct vop_close_args *v)
298 struct vnode *vp = v->a_vp;
300 /* Update node times. */
307 tmpfs_access(struct vop_access_args *v)
309 struct vnode *vp = v->a_vp;
310 accmode_t accmode = v->a_accmode;
311 struct ucred *cred = v->a_cred;
314 struct tmpfs_node *node;
316 MPASS(VOP_ISLOCKED(vp));
318 node = VP_TO_TMPFS_NODE(vp);
320 switch (vp->v_type) {
326 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
346 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
351 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
352 node->tn_gid, accmode, cred, NULL);
355 MPASS(VOP_ISLOCKED(vp));
361 tmpfs_getattr(struct vop_getattr_args *v)
363 struct vnode *vp = v->a_vp;
364 struct vattr *vap = v->a_vap;
366 struct tmpfs_node *node;
368 node = VP_TO_TMPFS_NODE(vp);
372 vap->va_type = vp->v_type;
373 vap->va_mode = node->tn_mode;
374 vap->va_nlink = node->tn_links;
375 vap->va_uid = node->tn_uid;
376 vap->va_gid = node->tn_gid;
377 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
378 vap->va_fileid = node->tn_id;
379 vap->va_size = node->tn_size;
380 vap->va_blocksize = PAGE_SIZE;
381 vap->va_atime = node->tn_atime;
382 vap->va_mtime = node->tn_mtime;
383 vap->va_ctime = node->tn_ctime;
384 vap->va_birthtime = node->tn_birthtime;
385 vap->va_gen = node->tn_gen;
386 vap->va_flags = node->tn_flags;
387 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
388 node->tn_rdev : NODEV;
389 vap->va_bytes = round_page(node->tn_size);
396 tmpfs_setattr(struct vop_setattr_args *v)
398 struct vnode *vp = v->a_vp;
399 struct vattr *vap = v->a_vap;
400 struct ucred *cred = v->a_cred;
401 struct thread *td = curthread;
405 MPASS(VOP_ISLOCKED(vp));
409 /* Abort if any unsettable attribute is given. */
410 if (vap->va_type != VNON ||
411 vap->va_nlink != VNOVAL ||
412 vap->va_fsid != VNOVAL ||
413 vap->va_fileid != VNOVAL ||
414 vap->va_blocksize != VNOVAL ||
415 vap->va_gen != VNOVAL ||
416 vap->va_rdev != VNOVAL ||
417 vap->va_bytes != VNOVAL)
420 if (error == 0 && (vap->va_flags != VNOVAL))
421 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
423 if (error == 0 && (vap->va_size != VNOVAL))
424 error = tmpfs_chsize(vp, vap->va_size, cred, td);
426 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
427 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
429 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
430 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
432 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
433 vap->va_atime.tv_nsec != VNOVAL) ||
434 (vap->va_mtime.tv_sec != VNOVAL &&
435 vap->va_mtime.tv_nsec != VNOVAL) ||
436 (vap->va_birthtime.tv_sec != VNOVAL &&
437 vap->va_birthtime.tv_nsec != VNOVAL)))
438 error = tmpfs_chtimes(vp, vap, cred, td);
440 /* Update the node times. We give preference to the error codes
441 * generated by this function rather than the ones that may arise
442 * from tmpfs_update. */
445 MPASS(VOP_ISLOCKED(vp));
451 tmpfs_read(struct vop_read_args *v)
455 struct tmpfs_node *node;
458 if (vp->v_type != VREG)
461 if (uio->uio_offset < 0)
463 node = VP_TO_TMPFS_NODE(vp);
464 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
465 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
469 tmpfs_write(struct vop_write_args *v)
473 struct tmpfs_node *node;
479 ioflag = v->a_ioflag;
481 node = VP_TO_TMPFS_NODE(vp);
482 oldsize = node->tn_size;
484 if (uio->uio_offset < 0 || vp->v_type != VREG)
486 if (uio->uio_resid == 0)
488 if (ioflag & IO_APPEND)
489 uio->uio_offset = node->tn_size;
490 if (uio->uio_offset + uio->uio_resid >
491 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
493 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
495 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
496 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
502 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
503 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
505 if (node->tn_mode & (S_ISUID | S_ISGID)) {
506 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
507 node->tn_mode &= ~(S_ISUID | S_ISGID);
510 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
513 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
514 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
520 tmpfs_fsync(struct vop_fsync_args *v)
522 struct vnode *vp = v->a_vp;
524 MPASS(VOP_ISLOCKED(vp));
526 tmpfs_check_mtime(vp);
533 tmpfs_remove(struct vop_remove_args *v)
535 struct vnode *dvp = v->a_dvp;
536 struct vnode *vp = v->a_vp;
539 struct tmpfs_dirent *de;
540 struct tmpfs_mount *tmp;
541 struct tmpfs_node *dnode;
542 struct tmpfs_node *node;
544 MPASS(VOP_ISLOCKED(dvp));
545 MPASS(VOP_ISLOCKED(vp));
547 if (vp->v_type == VDIR) {
552 dnode = VP_TO_TMPFS_DIR(dvp);
553 node = VP_TO_TMPFS_NODE(vp);
554 tmp = VFS_TO_TMPFS(vp->v_mount);
555 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
558 /* Files marked as immutable or append-only cannot be deleted. */
559 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
560 (dnode->tn_flags & APPEND)) {
565 /* Remove the entry from the directory; as it is a file, we do not
566 * have to change the number of hard links of the directory. */
567 tmpfs_dir_detach(dvp, de);
568 if (v->a_cnp->cn_flags & DOWHITEOUT)
569 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
571 /* Free the directory entry we just deleted. Note that the node
572 * referred by it will not be removed until the vnode is really
574 tmpfs_free_dirent(tmp, de);
576 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
585 tmpfs_link(struct vop_link_args *v)
587 struct vnode *dvp = v->a_tdvp;
588 struct vnode *vp = v->a_vp;
589 struct componentname *cnp = v->a_cnp;
592 struct tmpfs_dirent *de;
593 struct tmpfs_node *node;
595 MPASS(VOP_ISLOCKED(dvp));
596 MPASS(cnp->cn_flags & HASBUF);
597 MPASS(dvp != vp); /* XXX When can this be false? */
598 node = VP_TO_TMPFS_NODE(vp);
600 /* Ensure that we do not overflow the maximum number of links imposed
602 MPASS(node->tn_links <= LINK_MAX);
603 if (node->tn_links == LINK_MAX) {
608 /* We cannot create links of files marked immutable or append-only. */
609 if (node->tn_flags & (IMMUTABLE | APPEND)) {
614 /* Allocate a new directory entry to represent the node. */
615 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
616 cnp->cn_nameptr, cnp->cn_namelen, &de);
620 /* Insert the new directory entry into the appropriate directory. */
621 if (cnp->cn_flags & ISWHITEOUT)
622 tmpfs_dir_whiteout_remove(dvp, cnp);
623 tmpfs_dir_attach(dvp, de);
625 /* vp link count has changed, so update node times. */
626 node->tn_status |= TMPFS_NODE_CHANGED;
636 * We acquire all but fdvp locks using non-blocking acquisitions. If we
637 * fail to acquire any lock in the path we will drop all held locks,
638 * acquire the new lock in a blocking fashion, and then release it and
639 * restart the rename. This acquire/release step ensures that we do not
640 * spin on a lock waiting for release. On error release all vnode locks
641 * and decrement references the way tmpfs_rename() would do.
644 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
645 struct vnode *tdvp, struct vnode **tvpp,
646 struct componentname *fcnp, struct componentname *tcnp)
650 struct tmpfs_dirent *de;
651 int error, restarts = 0;
654 if (*tvpp != NULL && *tvpp != tdvp)
655 VOP_UNLOCK(*tvpp, 0);
660 error = vn_lock(fdvp, LK_EXCLUSIVE);
663 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
665 error = vn_lock(tdvp, LK_EXCLUSIVE);
672 * Re-resolve fvp to be certain it still exists and fetch the
675 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
679 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
680 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
686 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
692 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
697 * Concurrent rename race.
710 VOP_UNLOCK(*fvpp, 0);
712 * Re-resolve tvp and acquire the vnode lock if present.
714 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
716 * If tvp disappeared we just carry on.
718 if (de == NULL && *tvpp != NULL) {
723 * Get the tvp ino if the lookup succeeded. We may have to restart
724 * if the non-blocking acquire fails.
728 error = tmpfs_alloc_vp(mp, de->td_node,
729 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
738 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
744 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
753 tmpfs_rename_restarts += restarts;
763 tmpfs_rename_restarts += restarts;
769 tmpfs_rename(struct vop_rename_args *v)
771 struct vnode *fdvp = v->a_fdvp;
772 struct vnode *fvp = v->a_fvp;
773 struct componentname *fcnp = v->a_fcnp;
774 struct vnode *tdvp = v->a_tdvp;
775 struct vnode *tvp = v->a_tvp;
776 struct componentname *tcnp = v->a_tcnp;
777 struct mount *mp = NULL;
781 struct tmpfs_dirent *de;
782 struct tmpfs_mount *tmp;
783 struct tmpfs_node *fdnode;
784 struct tmpfs_node *fnode;
785 struct tmpfs_node *tnode;
786 struct tmpfs_node *tdnode;
788 MPASS(VOP_ISLOCKED(tdvp));
789 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
790 MPASS(fcnp->cn_flags & HASBUF);
791 MPASS(tcnp->cn_flags & HASBUF);
793 /* Disallow cross-device renames.
794 * XXX Why isn't this done by the caller? */
795 if (fvp->v_mount != tdvp->v_mount ||
796 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
801 /* If source and target are the same file, there is nothing to do. */
807 /* If we need to move the directory between entries, lock the
808 * source so that we can safely operate on it. */
809 if (fdvp != tdvp && fdvp != tvp) {
810 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
812 error = vfs_busy(mp, 0);
817 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
823 ASSERT_VOP_ELOCKED(fdvp,
824 "tmpfs_rename: fdvp not locked");
825 ASSERT_VOP_ELOCKED(tdvp,
826 "tmpfs_rename: tdvp not locked");
828 ASSERT_VOP_ELOCKED(tvp,
829 "tmpfs_rename: tvp not locked");
837 tmp = VFS_TO_TMPFS(tdvp->v_mount);
838 tdnode = VP_TO_TMPFS_DIR(tdvp);
839 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
840 fdnode = VP_TO_TMPFS_DIR(fdvp);
841 fnode = VP_TO_TMPFS_NODE(fvp);
842 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
844 /* Entry can disappear before we lock fdvp,
845 * also avoid manipulating '.' and '..' entries. */
847 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
848 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
854 MPASS(de->td_node == fnode);
856 /* If re-naming a directory to another preexisting directory
857 * ensure that the target directory is empty so that its
858 * removal causes no side effects.
859 * Kern_rename gurantees the destination to be a directory
860 * if the source is one. */
862 MPASS(tnode != NULL);
864 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
865 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
870 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
871 if (tnode->tn_size > 0) {
875 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
878 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
882 MPASS(fnode->tn_type != VDIR &&
883 tnode->tn_type != VDIR);
887 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
888 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
893 /* Ensure that we have enough memory to hold the new name, if it
894 * has to be changed. */
895 if (fcnp->cn_namelen != tcnp->cn_namelen ||
896 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
897 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
901 /* If the node is being moved to another directory, we have to do
903 if (fdnode != tdnode) {
904 /* In case we are moving a directory, we have to adjust its
905 * parent to point to the new parent. */
906 if (de->td_node->tn_type == VDIR) {
907 struct tmpfs_node *n;
909 /* Ensure the target directory is not a child of the
910 * directory being moved. Otherwise, we'd end up
911 * with stale nodes. */
913 /* TMPFS_LOCK garanties that no nodes are freed while
914 * traversing the list. Nodes can only be marked as
915 * removed: tn_parent == NULL. */
918 while (n != n->tn_dir.tn_parent) {
919 struct tmpfs_node *parent;
922 TMPFS_NODE_UNLOCK(n);
926 free(newname, M_TMPFSNAME);
929 parent = n->tn_dir.tn_parent;
930 TMPFS_NODE_UNLOCK(n);
931 if (parent == NULL) {
935 TMPFS_NODE_LOCK(parent);
936 if (parent->tn_dir.tn_parent == NULL) {
937 TMPFS_NODE_UNLOCK(parent);
947 free(newname, M_TMPFSNAME);
950 TMPFS_NODE_UNLOCK(n);
952 /* Adjust the parent pointer. */
953 TMPFS_VALIDATE_DIR(fnode);
954 TMPFS_NODE_LOCK(de->td_node);
955 de->td_node->tn_dir.tn_parent = tdnode;
956 TMPFS_NODE_UNLOCK(de->td_node);
958 /* As a result of changing the target of the '..'
959 * entry, the link count of the source and target
960 * directories has to be adjusted. */
961 TMPFS_NODE_LOCK(tdnode);
962 TMPFS_ASSERT_LOCKED(tdnode);
964 TMPFS_NODE_UNLOCK(tdnode);
966 TMPFS_NODE_LOCK(fdnode);
967 TMPFS_ASSERT_LOCKED(fdnode);
969 TMPFS_NODE_UNLOCK(fdnode);
973 /* Do the move: just remove the entry from the source directory
974 * and insert it into the target one. */
975 tmpfs_dir_detach(fdvp, de);
977 if (fcnp->cn_flags & DOWHITEOUT)
978 tmpfs_dir_whiteout_add(fdvp, fcnp);
979 if (tcnp->cn_flags & ISWHITEOUT)
980 tmpfs_dir_whiteout_remove(tdvp, tcnp);
982 /* If the name has changed, we need to make it effective by changing
983 * it in the directory entry. */
984 if (newname != NULL) {
985 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
987 free(de->ud.td_name, M_TMPFSNAME);
988 de->ud.td_name = newname;
989 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
991 fnode->tn_status |= TMPFS_NODE_CHANGED;
992 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
995 /* If we are overwriting an entry, we have to remove the old one
996 * from the target directory. */
998 struct tmpfs_dirent *tde;
1000 /* Remove the old entry from the target directory. */
1001 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1002 tmpfs_dir_detach(tdvp, tde);
1004 /* Free the directory entry we just deleted. Note that the
1005 * node referred by it will not be removed until the vnode is
1006 * really reclaimed. */
1007 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1010 tmpfs_dir_attach(tdvp, de);
1015 cache_purge_negative(tdvp);
1020 if (fdvp != tdvp && fdvp != tvp)
1021 VOP_UNLOCK(fdvp, 0);
1024 /* Release target nodes. */
1025 /* XXX: I don't understand when tdvp can be the same as tvp, but
1026 * other code takes care of this... */
1034 /* Release source nodes. */
1045 tmpfs_mkdir(struct vop_mkdir_args *v)
1047 struct vnode *dvp = v->a_dvp;
1048 struct vnode **vpp = v->a_vpp;
1049 struct componentname *cnp = v->a_cnp;
1050 struct vattr *vap = v->a_vap;
1052 MPASS(vap->va_type == VDIR);
1054 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1058 tmpfs_rmdir(struct vop_rmdir_args *v)
1060 struct vnode *dvp = v->a_dvp;
1061 struct vnode *vp = v->a_vp;
1064 struct tmpfs_dirent *de;
1065 struct tmpfs_mount *tmp;
1066 struct tmpfs_node *dnode;
1067 struct tmpfs_node *node;
1069 MPASS(VOP_ISLOCKED(dvp));
1070 MPASS(VOP_ISLOCKED(vp));
1072 tmp = VFS_TO_TMPFS(dvp->v_mount);
1073 dnode = VP_TO_TMPFS_DIR(dvp);
1074 node = VP_TO_TMPFS_DIR(vp);
1076 /* Directories with more than two entries ('.' and '..') cannot be
1078 if (node->tn_size > 0) {
1083 if ((dnode->tn_flags & APPEND)
1084 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1089 /* This invariant holds only if we are not trying to remove "..".
1090 * We checked for that above so this is safe now. */
1091 MPASS(node->tn_dir.tn_parent == dnode);
1093 /* Get the directory entry associated with node (vp). This was
1094 * filled by tmpfs_lookup while looking up the entry. */
1095 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1096 MPASS(TMPFS_DIRENT_MATCHES(de,
1097 v->a_cnp->cn_nameptr,
1098 v->a_cnp->cn_namelen));
1100 /* Check flags to see if we are allowed to remove the directory. */
1101 if ((dnode->tn_flags & APPEND) != 0 ||
1102 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1108 /* Detach the directory entry from the directory (dnode). */
1109 tmpfs_dir_detach(dvp, de);
1110 if (v->a_cnp->cn_flags & DOWHITEOUT)
1111 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1113 /* No vnode should be allocated for this entry from this point */
1114 TMPFS_NODE_LOCK(node);
1116 node->tn_dir.tn_parent = NULL;
1117 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1118 TMPFS_NODE_MODIFIED;
1120 TMPFS_NODE_UNLOCK(node);
1122 TMPFS_NODE_LOCK(dnode);
1124 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1125 TMPFS_NODE_MODIFIED;
1126 TMPFS_NODE_UNLOCK(dnode);
1131 /* Free the directory entry we just deleted. Note that the node
1132 * referred by it will not be removed until the vnode is really
1134 tmpfs_free_dirent(tmp, de);
1136 /* Release the deleted vnode (will destroy the node, notify
1137 * interested parties and clean it from the cache). */
1139 dnode->tn_status |= TMPFS_NODE_CHANGED;
1149 tmpfs_symlink(struct vop_symlink_args *v)
1151 struct vnode *dvp = v->a_dvp;
1152 struct vnode **vpp = v->a_vpp;
1153 struct componentname *cnp = v->a_cnp;
1154 struct vattr *vap = v->a_vap;
1155 char *target = v->a_target;
1157 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1158 MPASS(vap->va_type == VLNK);
1160 vap->va_type = VLNK;
1163 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1167 tmpfs_readdir(struct vop_readdir_args *v)
1169 struct vnode *vp = v->a_vp;
1170 struct uio *uio = v->a_uio;
1171 int *eofflag = v->a_eofflag;
1172 u_long **cookies = v->a_cookies;
1173 int *ncookies = v->a_ncookies;
1178 struct tmpfs_node *node;
1180 /* This operation only makes sense on directory nodes. */
1181 if (vp->v_type != VDIR)
1185 node = VP_TO_TMPFS_DIR(vp);
1187 startresid = uio->uio_resid;
1189 /* Allocate cookies for NFS and compat modules. */
1190 if (cookies != NULL && ncookies != NULL) {
1191 maxcookies = howmany(node->tn_size,
1192 sizeof(struct tmpfs_dirent)) + 2;
1193 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1198 if (cookies == NULL)
1199 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1201 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1204 /* Buffer was filled without hitting EOF. */
1205 if (error == EJUSTRETURN)
1206 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1208 if (error != 0 && cookies != NULL && ncookies != NULL) {
1209 free(*cookies, M_TEMP);
1214 if (eofflag != NULL)
1216 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1222 tmpfs_readlink(struct vop_readlink_args *v)
1224 struct vnode *vp = v->a_vp;
1225 struct uio *uio = v->a_uio;
1228 struct tmpfs_node *node;
1230 MPASS(uio->uio_offset == 0);
1231 MPASS(vp->v_type == VLNK);
1233 node = VP_TO_TMPFS_NODE(vp);
1235 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1237 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
1243 tmpfs_inactive(struct vop_inactive_args *v)
1246 struct tmpfs_node *node;
1249 node = VP_TO_TMPFS_NODE(vp);
1250 if (node->tn_links == 0)
1253 tmpfs_check_mtime(vp);
1258 tmpfs_reclaim(struct vop_reclaim_args *v)
1260 struct vnode *vp = v->a_vp;
1262 struct tmpfs_mount *tmp;
1263 struct tmpfs_node *node;
1265 node = VP_TO_TMPFS_NODE(vp);
1266 tmp = VFS_TO_TMPFS(vp->v_mount);
1268 if (vp->v_type == VREG)
1269 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1271 vnode_destroy_vobject(vp);
1272 vp->v_object = NULL;
1275 TMPFS_NODE_LOCK(node);
1278 /* If the node referenced by this vnode was deleted by the user,
1279 * we must free its associated data structures (now that the vnode
1280 * is being reclaimed). */
1281 if (node->tn_links == 0 &&
1282 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1283 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1284 TMPFS_NODE_UNLOCK(node);
1285 tmpfs_free_node(tmp, node);
1287 TMPFS_NODE_UNLOCK(node);
1289 MPASS(vp->v_data == NULL);
1294 tmpfs_print(struct vop_print_args *v)
1296 struct vnode *vp = v->a_vp;
1298 struct tmpfs_node *node;
1300 node = VP_TO_TMPFS_NODE(vp);
1302 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1303 node, node->tn_flags, node->tn_links);
1304 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1305 node->tn_mode, node->tn_uid, node->tn_gid,
1306 (intmax_t)node->tn_size, node->tn_status);
1308 if (vp->v_type == VFIFO)
1317 tmpfs_pathconf(struct vop_pathconf_args *v)
1319 int name = v->a_name;
1320 register_t *retval = v->a_retval;
1343 case _PC_CHOWN_RESTRICTED:
1355 case _PC_FILESIZEBITS:
1356 *retval = 0; /* XXX Don't know which value should I return. */
1367 tmpfs_vptofh(struct vop_vptofh_args *ap)
1369 struct tmpfs_fid *tfhp;
1370 struct tmpfs_node *node;
1372 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1373 node = VP_TO_TMPFS_NODE(ap->a_vp);
1375 tfhp->tf_len = sizeof(struct tmpfs_fid);
1376 tfhp->tf_id = node->tn_id;
1377 tfhp->tf_gen = node->tn_gen;
1383 tmpfs_whiteout(struct vop_whiteout_args *ap)
1385 struct vnode *dvp = ap->a_dvp;
1386 struct componentname *cnp = ap->a_cnp;
1387 struct tmpfs_dirent *de;
1389 switch (ap->a_flags) {
1393 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1395 return (de->td_node == NULL ? 0 : EEXIST);
1396 return (tmpfs_dir_whiteout_add(dvp, cnp));
1398 tmpfs_dir_whiteout_remove(dvp, cnp);
1401 panic("tmpfs_whiteout: unknown op");
1406 * Vnode operations vector used for files stored in a tmpfs file system.
1408 struct vop_vector tmpfs_vnodeop_entries = {
1409 .vop_default = &default_vnodeops,
1410 .vop_lookup = vfs_cache_lookup,
1411 .vop_cachedlookup = tmpfs_lookup,
1412 .vop_create = tmpfs_create,
1413 .vop_mknod = tmpfs_mknod,
1414 .vop_open = tmpfs_open,
1415 .vop_close = tmpfs_close,
1416 .vop_access = tmpfs_access,
1417 .vop_getattr = tmpfs_getattr,
1418 .vop_setattr = tmpfs_setattr,
1419 .vop_read = tmpfs_read,
1420 .vop_write = tmpfs_write,
1421 .vop_fsync = tmpfs_fsync,
1422 .vop_remove = tmpfs_remove,
1423 .vop_link = tmpfs_link,
1424 .vop_rename = tmpfs_rename,
1425 .vop_mkdir = tmpfs_mkdir,
1426 .vop_rmdir = tmpfs_rmdir,
1427 .vop_symlink = tmpfs_symlink,
1428 .vop_readdir = tmpfs_readdir,
1429 .vop_readlink = tmpfs_readlink,
1430 .vop_inactive = tmpfs_inactive,
1431 .vop_reclaim = tmpfs_reclaim,
1432 .vop_print = tmpfs_print,
1433 .vop_pathconf = tmpfs_pathconf,
1434 .vop_vptofh = tmpfs_vptofh,
1435 .vop_whiteout = tmpfs_whiteout,
1436 .vop_bmap = VOP_EOPNOTSUPP,