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);
381 /* --------------------------------------------------------------------- */
383 /* XXX Should this operation be atomic? I think it should, but code in
384 * XXX other places (e.g., ufs) doesn't seem to be... */
386 tmpfs_setattr(struct vop_setattr_args *v)
388 struct vnode *vp = v->a_vp;
389 struct vattr *vap = v->a_vap;
390 struct ucred *cred = v->a_cred;
391 struct thread *td = curthread;
395 MPASS(VOP_ISLOCKED(vp));
399 /* Abort if any unsettable attribute is given. */
400 if (vap->va_type != VNON ||
401 vap->va_nlink != VNOVAL ||
402 vap->va_fsid != VNOVAL ||
403 vap->va_fileid != VNOVAL ||
404 vap->va_blocksize != VNOVAL ||
405 vap->va_gen != VNOVAL ||
406 vap->va_rdev != VNOVAL ||
407 vap->va_bytes != VNOVAL)
410 if (error == 0 && (vap->va_flags != VNOVAL))
411 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
413 if (error == 0 && (vap->va_size != VNOVAL))
414 error = tmpfs_chsize(vp, vap->va_size, cred, td);
416 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
417 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
419 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
420 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
422 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
423 vap->va_atime.tv_nsec != VNOVAL) ||
424 (vap->va_mtime.tv_sec != VNOVAL &&
425 vap->va_mtime.tv_nsec != VNOVAL) ||
426 (vap->va_birthtime.tv_sec != VNOVAL &&
427 vap->va_birthtime.tv_nsec != VNOVAL)))
428 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
429 &vap->va_birthtime, vap->va_vaflags, cred, td);
431 /* Update the node times. We give preference to the error codes
432 * generated by this function rather than the ones that may arise
433 * from tmpfs_update. */
436 MPASS(VOP_ISLOCKED(vp));
442 tmpfs_read(struct vop_read_args *v)
446 struct tmpfs_node *node;
449 if (vp->v_type != VREG)
452 if (uio->uio_offset < 0)
454 node = VP_TO_TMPFS_NODE(vp);
455 node->tn_status |= TMPFS_NODE_ACCESSED;
456 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
460 tmpfs_write(struct vop_write_args *v)
464 struct tmpfs_node *node;
471 ioflag = v->a_ioflag;
473 node = VP_TO_TMPFS_NODE(vp);
474 oldsize = node->tn_size;
476 if (uio->uio_offset < 0 || vp->v_type != VREG)
478 if (uio->uio_resid == 0)
480 if (ioflag & IO_APPEND)
481 uio->uio_offset = node->tn_size;
482 if (uio->uio_offset + uio->uio_resid >
483 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
485 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
487 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
489 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
495 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
496 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
497 (extended ? TMPFS_NODE_CHANGED : 0);
498 if (node->tn_mode & (S_ISUID | S_ISGID)) {
499 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
500 node->tn_mode &= ~(S_ISUID | S_ISGID);
503 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
506 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
507 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
512 /* --------------------------------------------------------------------- */
515 tmpfs_fsync(struct vop_fsync_args *v)
517 struct vnode *vp = v->a_vp;
519 MPASS(VOP_ISLOCKED(vp));
526 /* --------------------------------------------------------------------- */
529 tmpfs_remove(struct vop_remove_args *v)
531 struct vnode *dvp = v->a_dvp;
532 struct vnode *vp = v->a_vp;
535 struct tmpfs_dirent *de;
536 struct tmpfs_mount *tmp;
537 struct tmpfs_node *dnode;
538 struct tmpfs_node *node;
540 MPASS(VOP_ISLOCKED(dvp));
541 MPASS(VOP_ISLOCKED(vp));
543 if (vp->v_type == VDIR) {
548 dnode = VP_TO_TMPFS_DIR(dvp);
549 node = VP_TO_TMPFS_NODE(vp);
550 tmp = VFS_TO_TMPFS(vp->v_mount);
551 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
554 /* Files marked as immutable or append-only cannot be deleted. */
555 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
556 (dnode->tn_flags & APPEND)) {
561 /* Remove the entry from the directory; as it is a file, we do not
562 * have to change the number of hard links of the directory. */
563 tmpfs_dir_detach(dvp, de);
564 if (v->a_cnp->cn_flags & DOWHITEOUT)
565 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
567 /* Free the directory entry we just deleted. Note that the node
568 * referred by it will not be removed until the vnode is really
570 tmpfs_free_dirent(tmp, de);
572 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
580 /* --------------------------------------------------------------------- */
583 tmpfs_link(struct vop_link_args *v)
585 struct vnode *dvp = v->a_tdvp;
586 struct vnode *vp = v->a_vp;
587 struct componentname *cnp = v->a_cnp;
590 struct tmpfs_dirent *de;
591 struct tmpfs_node *node;
593 MPASS(VOP_ISLOCKED(dvp));
594 MPASS(cnp->cn_flags & HASBUF);
595 MPASS(dvp != vp); /* XXX When can this be false? */
597 node = VP_TO_TMPFS_NODE(vp);
599 /* XXX: Why aren't the following two tests done by the caller? */
601 /* Hard links of directories are forbidden. */
602 if (vp->v_type == VDIR) {
607 /* Cannot create cross-device links. */
608 if (dvp->v_mount != vp->v_mount) {
613 /* Ensure that we do not overflow the maximum number of links imposed
615 MPASS(node->tn_links <= LINK_MAX);
616 if (node->tn_links == LINK_MAX) {
621 /* We cannot create links of files marked immutable or append-only. */
622 if (node->tn_flags & (IMMUTABLE | APPEND)) {
627 /* Allocate a new directory entry to represent the node. */
628 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
629 cnp->cn_nameptr, cnp->cn_namelen, &de);
633 /* Insert the new directory entry into the appropriate directory. */
634 if (cnp->cn_flags & ISWHITEOUT)
635 tmpfs_dir_whiteout_remove(dvp, cnp);
636 tmpfs_dir_attach(dvp, de);
638 /* vp link count has changed, so update node times. */
639 node->tn_status |= TMPFS_NODE_CHANGED;
648 /* --------------------------------------------------------------------- */
651 * We acquire all but fdvp locks using non-blocking acquisitions. If we
652 * fail to acquire any lock in the path we will drop all held locks,
653 * acquire the new lock in a blocking fashion, and then release it and
654 * restart the rename. This acquire/release step ensures that we do not
655 * spin on a lock waiting for release. On error release all vnode locks
656 * and decrement references the way tmpfs_rename() would do.
659 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
660 struct vnode *tdvp, struct vnode **tvpp,
661 struct componentname *fcnp, struct componentname *tcnp)
665 struct tmpfs_dirent *de;
666 int error, restarts = 0;
669 if (*tvpp != NULL && *tvpp != tdvp)
670 VOP_UNLOCK(*tvpp, 0);
675 error = vn_lock(fdvp, LK_EXCLUSIVE);
678 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
680 error = vn_lock(tdvp, LK_EXCLUSIVE);
687 * Re-resolve fvp to be certain it still exists and fetch the
690 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
694 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
695 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
701 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
707 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
712 * Concurrent rename race.
725 VOP_UNLOCK(*fvpp, 0);
727 * Re-resolve tvp and acquire the vnode lock if present.
729 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
731 * If tvp disappeared we just carry on.
733 if (de == NULL && *tvpp != NULL) {
738 * Get the tvp ino if the lookup succeeded. We may have to restart
739 * if the non-blocking acquire fails.
743 error = tmpfs_alloc_vp(mp, de->td_node,
744 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
753 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
759 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
768 tmpfs_rename_restarts += restarts;
778 tmpfs_rename_restarts += restarts;
784 tmpfs_rename(struct vop_rename_args *v)
786 struct vnode *fdvp = v->a_fdvp;
787 struct vnode *fvp = v->a_fvp;
788 struct componentname *fcnp = v->a_fcnp;
789 struct vnode *tdvp = v->a_tdvp;
790 struct vnode *tvp = v->a_tvp;
791 struct componentname *tcnp = v->a_tcnp;
792 struct mount *mp = NULL;
796 struct tmpfs_dirent *de;
797 struct tmpfs_mount *tmp;
798 struct tmpfs_node *fdnode;
799 struct tmpfs_node *fnode;
800 struct tmpfs_node *tnode;
801 struct tmpfs_node *tdnode;
803 MPASS(VOP_ISLOCKED(tdvp));
804 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
805 MPASS(fcnp->cn_flags & HASBUF);
806 MPASS(tcnp->cn_flags & HASBUF);
808 /* Disallow cross-device renames.
809 * XXX Why isn't this done by the caller? */
810 if (fvp->v_mount != tdvp->v_mount ||
811 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
816 /* If source and target are the same file, there is nothing to do. */
822 /* If we need to move the directory between entries, lock the
823 * source so that we can safely operate on it. */
824 if (fdvp != tdvp && fdvp != tvp) {
825 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
827 error = vfs_busy(mp, 0);
832 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
838 ASSERT_VOP_ELOCKED(fdvp,
839 "tmpfs_rename: fdvp not locked");
840 ASSERT_VOP_ELOCKED(tdvp,
841 "tmpfs_rename: tdvp not locked");
843 ASSERT_VOP_ELOCKED(tvp,
844 "tmpfs_rename: tvp not locked");
852 tmp = VFS_TO_TMPFS(tdvp->v_mount);
853 tdnode = VP_TO_TMPFS_DIR(tdvp);
854 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
855 fdnode = VP_TO_TMPFS_DIR(fdvp);
856 fnode = VP_TO_TMPFS_NODE(fvp);
857 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
859 /* Entry can disappear before we lock fdvp,
860 * also avoid manipulating '.' and '..' entries. */
862 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
863 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
869 MPASS(de->td_node == fnode);
871 /* If re-naming a directory to another preexisting directory
872 * ensure that the target directory is empty so that its
873 * removal causes no side effects.
874 * Kern_rename gurantees the destination to be a directory
875 * if the source is one. */
877 MPASS(tnode != NULL);
879 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
880 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
885 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
886 if (tnode->tn_size > 0) {
890 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
893 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
897 MPASS(fnode->tn_type != VDIR &&
898 tnode->tn_type != VDIR);
902 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
903 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
908 /* Ensure that we have enough memory to hold the new name, if it
909 * has to be changed. */
910 if (fcnp->cn_namelen != tcnp->cn_namelen ||
911 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
912 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
916 /* If the node is being moved to another directory, we have to do
918 if (fdnode != tdnode) {
919 /* In case we are moving a directory, we have to adjust its
920 * parent to point to the new parent. */
921 if (de->td_node->tn_type == VDIR) {
922 struct tmpfs_node *n;
924 /* Ensure the target directory is not a child of the
925 * directory being moved. Otherwise, we'd end up
926 * with stale nodes. */
928 /* TMPFS_LOCK garanties that no nodes are freed while
929 * traversing the list. Nodes can only be marked as
930 * removed: tn_parent == NULL. */
933 while (n != n->tn_dir.tn_parent) {
934 struct tmpfs_node *parent;
937 TMPFS_NODE_UNLOCK(n);
941 free(newname, M_TMPFSNAME);
944 parent = n->tn_dir.tn_parent;
945 TMPFS_NODE_UNLOCK(n);
946 if (parent == NULL) {
950 TMPFS_NODE_LOCK(parent);
951 if (parent->tn_dir.tn_parent == NULL) {
952 TMPFS_NODE_UNLOCK(parent);
962 free(newname, M_TMPFSNAME);
965 TMPFS_NODE_UNLOCK(n);
967 /* Adjust the parent pointer. */
968 TMPFS_VALIDATE_DIR(fnode);
969 TMPFS_NODE_LOCK(de->td_node);
970 de->td_node->tn_dir.tn_parent = tdnode;
971 TMPFS_NODE_UNLOCK(de->td_node);
973 /* As a result of changing the target of the '..'
974 * entry, the link count of the source and target
975 * directories has to be adjusted. */
976 TMPFS_NODE_LOCK(tdnode);
977 TMPFS_ASSERT_LOCKED(tdnode);
979 TMPFS_NODE_UNLOCK(tdnode);
981 TMPFS_NODE_LOCK(fdnode);
982 TMPFS_ASSERT_LOCKED(fdnode);
984 TMPFS_NODE_UNLOCK(fdnode);
988 /* Do the move: just remove the entry from the source directory
989 * and insert it into the target one. */
990 tmpfs_dir_detach(fdvp, de);
992 if (fcnp->cn_flags & DOWHITEOUT)
993 tmpfs_dir_whiteout_add(fdvp, fcnp);
994 if (tcnp->cn_flags & ISWHITEOUT)
995 tmpfs_dir_whiteout_remove(tdvp, tcnp);
997 /* If the name has changed, we need to make it effective by changing
998 * it in the directory entry. */
999 if (newname != NULL) {
1000 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1002 free(de->ud.td_name, M_TMPFSNAME);
1003 de->ud.td_name = newname;
1004 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1006 fnode->tn_status |= TMPFS_NODE_CHANGED;
1007 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1010 /* If we are overwriting an entry, we have to remove the old one
1011 * from the target directory. */
1013 struct tmpfs_dirent *tde;
1015 /* Remove the old entry from the target directory. */
1016 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1017 tmpfs_dir_detach(tdvp, tde);
1019 /* Free the directory entry we just deleted. Note that the
1020 * node referred by it will not be removed until the vnode is
1021 * really reclaimed. */
1022 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1025 tmpfs_dir_attach(tdvp, de);
1030 cache_purge_negative(tdvp);
1035 if (fdvp != tdvp && fdvp != tvp)
1036 VOP_UNLOCK(fdvp, 0);
1039 /* Release target nodes. */
1040 /* XXX: I don't understand when tdvp can be the same as tvp, but
1041 * other code takes care of this... */
1049 /* Release source nodes. */
1059 /* --------------------------------------------------------------------- */
1062 tmpfs_mkdir(struct vop_mkdir_args *v)
1064 struct vnode *dvp = v->a_dvp;
1065 struct vnode **vpp = v->a_vpp;
1066 struct componentname *cnp = v->a_cnp;
1067 struct vattr *vap = v->a_vap;
1069 MPASS(vap->va_type == VDIR);
1071 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1074 /* --------------------------------------------------------------------- */
1077 tmpfs_rmdir(struct vop_rmdir_args *v)
1079 struct vnode *dvp = v->a_dvp;
1080 struct vnode *vp = v->a_vp;
1083 struct tmpfs_dirent *de;
1084 struct tmpfs_mount *tmp;
1085 struct tmpfs_node *dnode;
1086 struct tmpfs_node *node;
1088 MPASS(VOP_ISLOCKED(dvp));
1089 MPASS(VOP_ISLOCKED(vp));
1091 tmp = VFS_TO_TMPFS(dvp->v_mount);
1092 dnode = VP_TO_TMPFS_DIR(dvp);
1093 node = VP_TO_TMPFS_DIR(vp);
1095 /* Directories with more than two entries ('.' and '..') cannot be
1097 if (node->tn_size > 0) {
1102 if ((dnode->tn_flags & APPEND)
1103 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1108 /* This invariant holds only if we are not trying to remove "..".
1109 * We checked for that above so this is safe now. */
1110 MPASS(node->tn_dir.tn_parent == dnode);
1112 /* Get the directory entry associated with node (vp). This was
1113 * filled by tmpfs_lookup while looking up the entry. */
1114 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1115 MPASS(TMPFS_DIRENT_MATCHES(de,
1116 v->a_cnp->cn_nameptr,
1117 v->a_cnp->cn_namelen));
1119 /* Check flags to see if we are allowed to remove the directory. */
1120 if (dnode->tn_flags & APPEND
1121 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1127 /* Detach the directory entry from the directory (dnode). */
1128 tmpfs_dir_detach(dvp, de);
1129 if (v->a_cnp->cn_flags & DOWHITEOUT)
1130 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1132 /* No vnode should be allocated for this entry from this point */
1133 TMPFS_NODE_LOCK(node);
1134 TMPFS_ASSERT_ELOCKED(node);
1136 node->tn_dir.tn_parent = NULL;
1137 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1138 TMPFS_NODE_MODIFIED;
1140 TMPFS_NODE_UNLOCK(node);
1142 TMPFS_NODE_LOCK(dnode);
1143 TMPFS_ASSERT_ELOCKED(dnode);
1145 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1146 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1147 TMPFS_NODE_UNLOCK(dnode);
1152 /* Free the directory entry we just deleted. Note that the node
1153 * referred by it will not be removed until the vnode is really
1155 tmpfs_free_dirent(tmp, de);
1157 /* Release the deleted vnode (will destroy the node, notify
1158 * interested parties and clean it from the cache). */
1160 dnode->tn_status |= TMPFS_NODE_CHANGED;
1169 /* --------------------------------------------------------------------- */
1172 tmpfs_symlink(struct vop_symlink_args *v)
1174 struct vnode *dvp = v->a_dvp;
1175 struct vnode **vpp = v->a_vpp;
1176 struct componentname *cnp = v->a_cnp;
1177 struct vattr *vap = v->a_vap;
1178 char *target = v->a_target;
1180 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1181 MPASS(vap->va_type == VLNK);
1183 vap->va_type = VLNK;
1186 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1189 /* --------------------------------------------------------------------- */
1192 tmpfs_readdir(struct vop_readdir_args *v)
1194 struct vnode *vp = v->a_vp;
1195 struct uio *uio = v->a_uio;
1196 int *eofflag = v->a_eofflag;
1197 u_long **cookies = v->a_cookies;
1198 int *ncookies = v->a_ncookies;
1203 struct tmpfs_node *node;
1205 /* This operation only makes sense on directory nodes. */
1206 if (vp->v_type != VDIR)
1210 node = VP_TO_TMPFS_DIR(vp);
1212 startresid = uio->uio_resid;
1214 /* Allocate cookies for NFS and compat modules. */
1215 if (cookies != NULL && ncookies != NULL) {
1216 maxcookies = howmany(node->tn_size,
1217 sizeof(struct tmpfs_dirent)) + 2;
1218 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1223 if (cookies == NULL)
1224 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1226 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1229 /* Buffer was filled without hitting EOF. */
1230 if (error == EJUSTRETURN)
1231 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1233 if (error != 0 && cookies != NULL)
1234 free(*cookies, M_TEMP);
1236 if (eofflag != NULL)
1238 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1243 /* --------------------------------------------------------------------- */
1246 tmpfs_readlink(struct vop_readlink_args *v)
1248 struct vnode *vp = v->a_vp;
1249 struct uio *uio = v->a_uio;
1252 struct tmpfs_node *node;
1254 MPASS(uio->uio_offset == 0);
1255 MPASS(vp->v_type == VLNK);
1257 node = VP_TO_TMPFS_NODE(vp);
1259 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1261 node->tn_status |= TMPFS_NODE_ACCESSED;
1266 /* --------------------------------------------------------------------- */
1269 tmpfs_inactive(struct vop_inactive_args *v)
1271 struct vnode *vp = v->a_vp;
1273 struct tmpfs_node *node;
1275 node = VP_TO_TMPFS_NODE(vp);
1277 if (node->tn_links == 0)
1283 /* --------------------------------------------------------------------- */
1286 tmpfs_reclaim(struct vop_reclaim_args *v)
1288 struct vnode *vp = v->a_vp;
1290 struct tmpfs_mount *tmp;
1291 struct tmpfs_node *node;
1293 node = VP_TO_TMPFS_NODE(vp);
1294 tmp = VFS_TO_TMPFS(vp->v_mount);
1296 if (vp->v_type == VREG)
1297 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1299 vnode_destroy_vobject(vp);
1300 vp->v_object = NULL;
1303 TMPFS_NODE_LOCK(node);
1304 TMPFS_ASSERT_ELOCKED(node);
1307 /* If the node referenced by this vnode was deleted by the user,
1308 * we must free its associated data structures (now that the vnode
1309 * is being reclaimed). */
1310 if (node->tn_links == 0 &&
1311 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1312 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1313 TMPFS_NODE_UNLOCK(node);
1314 tmpfs_free_node(tmp, node);
1316 TMPFS_NODE_UNLOCK(node);
1318 MPASS(vp->v_data == NULL);
1322 /* --------------------------------------------------------------------- */
1325 tmpfs_print(struct vop_print_args *v)
1327 struct vnode *vp = v->a_vp;
1329 struct tmpfs_node *node;
1331 node = VP_TO_TMPFS_NODE(vp);
1333 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1334 node, node->tn_flags, node->tn_links);
1335 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1336 node->tn_mode, node->tn_uid, node->tn_gid,
1337 (intmax_t)node->tn_size, node->tn_status);
1339 if (vp->v_type == VFIFO)
1347 /* --------------------------------------------------------------------- */
1350 tmpfs_pathconf(struct vop_pathconf_args *v)
1352 int name = v->a_name;
1353 register_t *retval = v->a_retval;
1376 case _PC_CHOWN_RESTRICTED:
1388 case _PC_FILESIZEBITS:
1389 *retval = 0; /* XXX Don't know which value should I return. */
1400 tmpfs_vptofh(struct vop_vptofh_args *ap)
1402 struct tmpfs_fid *tfhp;
1403 struct tmpfs_node *node;
1405 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1406 node = VP_TO_TMPFS_NODE(ap->a_vp);
1408 tfhp->tf_len = sizeof(struct tmpfs_fid);
1409 tfhp->tf_id = node->tn_id;
1410 tfhp->tf_gen = node->tn_gen;
1416 tmpfs_whiteout(struct vop_whiteout_args *ap)
1418 struct vnode *dvp = ap->a_dvp;
1419 struct componentname *cnp = ap->a_cnp;
1420 struct tmpfs_dirent *de;
1422 switch (ap->a_flags) {
1426 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1428 return (de->td_node == NULL ? 0 : EEXIST);
1429 return (tmpfs_dir_whiteout_add(dvp, cnp));
1431 tmpfs_dir_whiteout_remove(dvp, cnp);
1434 panic("tmpfs_whiteout: unknown op");
1438 /* --------------------------------------------------------------------- */
1441 * vnode operations vector used for files stored in a tmpfs file system.
1443 struct vop_vector tmpfs_vnodeop_entries = {
1444 .vop_default = &default_vnodeops,
1445 .vop_lookup = vfs_cache_lookup,
1446 .vop_cachedlookup = tmpfs_lookup,
1447 .vop_create = tmpfs_create,
1448 .vop_mknod = tmpfs_mknod,
1449 .vop_open = tmpfs_open,
1450 .vop_close = tmpfs_close,
1451 .vop_access = tmpfs_access,
1452 .vop_getattr = tmpfs_getattr,
1453 .vop_setattr = tmpfs_setattr,
1454 .vop_read = tmpfs_read,
1455 .vop_write = tmpfs_write,
1456 .vop_fsync = tmpfs_fsync,
1457 .vop_remove = tmpfs_remove,
1458 .vop_link = tmpfs_link,
1459 .vop_rename = tmpfs_rename,
1460 .vop_mkdir = tmpfs_mkdir,
1461 .vop_rmdir = tmpfs_rmdir,
1462 .vop_symlink = tmpfs_symlink,
1463 .vop_readdir = tmpfs_readdir,
1464 .vop_readlink = tmpfs_readlink,
1465 .vop_inactive = tmpfs_inactive,
1466 .vop_reclaim = tmpfs_reclaim,
1467 .vop_print = tmpfs_print,
1468 .vop_pathconf = tmpfs_pathconf,
1469 .vop_vptofh = tmpfs_vptofh,
1470 .vop_whiteout = tmpfs_whiteout,
1471 .vop_bmap = VOP_EOPNOTSUPP,