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>
42 #include <sys/namei.h>
45 #include <sys/resourcevar.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_pager.h>
56 #include <machine/_inttypes.h>
58 #include <fs/fifofs/fifo.h>
59 #include <fs/tmpfs/tmpfs_vnops.h>
60 #include <fs/tmpfs/tmpfs.h>
62 /* --------------------------------------------------------------------- */
65 tmpfs_lookup(struct vop_cachedlookup_args *v)
67 struct vnode *dvp = v->a_dvp;
68 struct vnode **vpp = v->a_vpp;
69 struct componentname *cnp = v->a_cnp;
72 struct tmpfs_dirent *de;
73 struct tmpfs_node *dnode;
75 dnode = VP_TO_TMPFS_DIR(dvp);
78 /* Check accessibility of requested node as a first step. */
79 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
83 /* We cannot be requesting the parent directory of the root node. */
84 MPASS(IMPLIES(dnode->tn_type == VDIR &&
85 dnode->tn_dir.tn_parent == dnode,
86 !(cnp->cn_flags & ISDOTDOT)));
88 if (cnp->cn_flags & ISDOTDOT) {
91 ltype = VOP_ISLOCKED(dvp);
94 /* Allocate a new vnode on the matching entry. */
95 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
96 cnp->cn_lkflags, vpp);
98 vn_lock(dvp, ltype | LK_RETRY);
100 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
105 de = tmpfs_dir_lookup(dnode, NULL, cnp);
107 /* The entry was not found in the directory.
108 * This is OK if we are creating or renaming an
109 * entry and are working on the last component of
111 if ((cnp->cn_flags & ISLASTCN) &&
112 (cnp->cn_nameiop == CREATE || \
113 cnp->cn_nameiop == RENAME)) {
114 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
119 /* Keep the component name in the buffer for
121 cnp->cn_flags |= SAVENAME;
127 struct tmpfs_node *tnode;
129 /* The entry was found, so get its associated
133 /* If we are not at the last path component and
134 * found a non-directory or non-link entry (which
135 * may itself be pointing to a directory), raise
137 if ((tnode->tn_type != VDIR &&
138 tnode->tn_type != VLNK) &&
139 !(cnp->cn_flags & ISLASTCN)) {
144 /* If we are deleting or renaming the entry, keep
145 * track of its tmpfs_dirent so that it can be
146 * easily deleted later. */
147 if ((cnp->cn_flags & ISLASTCN) &&
148 (cnp->cn_nameiop == DELETE ||
149 cnp->cn_nameiop == RENAME)) {
150 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
155 /* Allocate a new vnode on the matching entry. */
156 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
157 cnp->cn_lkflags, vpp);
161 if ((dnode->tn_mode & S_ISTXT) &&
162 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
163 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
169 cnp->cn_flags |= SAVENAME;
171 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
172 cnp->cn_lkflags, vpp);
177 /* Store the result of this lookup in the cache. Avoid this if the
178 * request was for creation, as it does not improve timings on
180 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
181 cache_enter(dvp, *vpp, cnp);
184 /* If there were no errors, *vpp cannot be null and it must be
186 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
191 /* --------------------------------------------------------------------- */
194 tmpfs_create(struct vop_create_args *v)
196 struct vnode *dvp = v->a_dvp;
197 struct vnode **vpp = v->a_vpp;
198 struct componentname *cnp = v->a_cnp;
199 struct vattr *vap = v->a_vap;
201 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
203 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
205 /* --------------------------------------------------------------------- */
208 tmpfs_mknod(struct vop_mknod_args *v)
210 struct vnode *dvp = v->a_dvp;
211 struct vnode **vpp = v->a_vpp;
212 struct componentname *cnp = v->a_cnp;
213 struct vattr *vap = v->a_vap;
215 if (vap->va_type != VBLK && vap->va_type != VCHR &&
216 vap->va_type != VFIFO)
219 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
222 /* --------------------------------------------------------------------- */
225 tmpfs_open(struct vop_open_args *v)
227 struct vnode *vp = v->a_vp;
228 int mode = v->a_mode;
231 struct tmpfs_node *node;
233 MPASS(VOP_ISLOCKED(vp));
235 node = VP_TO_TMPFS_NODE(vp);
237 /* The file is still active but all its names have been removed
238 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
239 * it is about to die. */
240 if (node->tn_links < 1)
243 /* If the file is marked append-only, deny write requests. */
244 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
248 vnode_create_vobject(vp, node->tn_size, v->a_td);
251 MPASS(VOP_ISLOCKED(vp));
255 /* --------------------------------------------------------------------- */
258 tmpfs_close(struct vop_close_args *v)
260 struct vnode *vp = v->a_vp;
262 struct tmpfs_node *node;
264 MPASS(VOP_ISLOCKED(vp));
266 node = VP_TO_TMPFS_NODE(vp);
268 if (node->tn_links > 0) {
269 /* Update node times. No need to do it if the node has
270 * been deleted, because it will vanish after we return. */
277 /* --------------------------------------------------------------------- */
280 tmpfs_access(struct vop_access_args *v)
282 struct vnode *vp = v->a_vp;
283 accmode_t accmode = v->a_accmode;
284 struct ucred *cred = v->a_cred;
287 struct tmpfs_node *node;
289 MPASS(VOP_ISLOCKED(vp));
291 node = VP_TO_TMPFS_NODE(vp);
293 switch (vp->v_type) {
299 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
319 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
324 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
325 node->tn_gid, accmode, cred, NULL);
328 MPASS(VOP_ISLOCKED(vp));
333 /* --------------------------------------------------------------------- */
336 tmpfs_getattr(struct vop_getattr_args *v)
338 struct vnode *vp = v->a_vp;
339 struct vattr *vap = v->a_vap;
341 struct tmpfs_node *node;
343 node = VP_TO_TMPFS_NODE(vp);
347 vap->va_type = vp->v_type;
348 vap->va_mode = node->tn_mode;
349 vap->va_nlink = node->tn_links;
350 vap->va_uid = node->tn_uid;
351 vap->va_gid = node->tn_gid;
352 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
353 vap->va_fileid = node->tn_id;
354 vap->va_size = node->tn_size;
355 vap->va_blocksize = PAGE_SIZE;
356 vap->va_atime = node->tn_atime;
357 vap->va_mtime = node->tn_mtime;
358 vap->va_ctime = node->tn_ctime;
359 vap->va_birthtime = node->tn_birthtime;
360 vap->va_gen = node->tn_gen;
361 vap->va_flags = node->tn_flags;
362 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
363 node->tn_rdev : NODEV;
364 vap->va_bytes = round_page(node->tn_size);
370 /* --------------------------------------------------------------------- */
372 /* XXX Should this operation be atomic? I think it should, but code in
373 * XXX other places (e.g., ufs) doesn't seem to be... */
375 tmpfs_setattr(struct vop_setattr_args *v)
377 struct vnode *vp = v->a_vp;
378 struct vattr *vap = v->a_vap;
379 struct ucred *cred = v->a_cred;
380 struct thread *td = curthread;
384 MPASS(VOP_ISLOCKED(vp));
388 /* Abort if any unsettable attribute is given. */
389 if (vap->va_type != VNON ||
390 vap->va_nlink != VNOVAL ||
391 vap->va_fsid != VNOVAL ||
392 vap->va_fileid != VNOVAL ||
393 vap->va_blocksize != VNOVAL ||
394 vap->va_gen != VNOVAL ||
395 vap->va_rdev != VNOVAL ||
396 vap->va_bytes != VNOVAL)
399 if (error == 0 && (vap->va_flags != VNOVAL))
400 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
402 if (error == 0 && (vap->va_size != VNOVAL))
403 error = tmpfs_chsize(vp, vap->va_size, cred, td);
405 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
406 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
408 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
409 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
411 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
412 vap->va_atime.tv_nsec != VNOVAL) ||
413 (vap->va_mtime.tv_sec != VNOVAL &&
414 vap->va_mtime.tv_nsec != VNOVAL) ||
415 (vap->va_birthtime.tv_sec != VNOVAL &&
416 vap->va_birthtime.tv_nsec != VNOVAL)))
417 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
418 &vap->va_birthtime, vap->va_vaflags, cred, td);
420 /* Update the node times. We give preference to the error codes
421 * generated by this function rather than the ones that may arise
422 * from tmpfs_update. */
425 MPASS(VOP_ISLOCKED(vp));
430 /* --------------------------------------------------------------------- */
433 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
442 addr = uio->uio_offset;
443 idx = OFF_TO_IDX(addr);
444 offset = addr & PAGE_MASK;
445 tlen = MIN(PAGE_SIZE - offset, len);
447 if ((vobj == NULL) || (vobj->resident_page_count == 0))
450 VM_OBJECT_LOCK(vobj);
452 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
453 vm_page_is_valid(m, offset, tlen)) {
454 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
457 VM_OBJECT_UNLOCK(vobj);
458 error = uiomove_fromphys(&m, offset, tlen, uio);
459 VM_OBJECT_LOCK(vobj);
461 VM_OBJECT_UNLOCK(vobj);
464 VM_OBJECT_UNLOCK(vobj);
466 VM_OBJECT_LOCK(tobj);
467 vm_object_pip_add(tobj, 1);
468 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
469 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
470 if (m->valid != VM_PAGE_BITS_ALL) {
471 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
472 error = vm_pager_get_pages(tobj, &m, 1, 0);
474 printf("tmpfs get pages from pager error [read]\n");
478 vm_page_zero_invalid(m, TRUE);
480 VM_OBJECT_UNLOCK(tobj);
481 error = uiomove_fromphys(&m, offset, tlen, uio);
482 VM_OBJECT_LOCK(tobj);
484 vm_page_lock_queues();
485 vm_page_unwire(m, TRUE);
486 vm_page_unlock_queues();
488 vm_object_pip_subtract(tobj, 1);
489 VM_OBJECT_UNLOCK(tobj);
495 tmpfs_read(struct vop_read_args *v)
497 struct vnode *vp = v->a_vp;
498 struct uio *uio = v->a_uio;
500 struct tmpfs_node *node;
507 node = VP_TO_TMPFS_NODE(vp);
509 if (vp->v_type != VREG) {
514 if (uio->uio_offset < 0) {
519 node->tn_status |= TMPFS_NODE_ACCESSED;
521 uobj = node->tn_reg.tn_aobj;
522 while ((resid = uio->uio_resid) > 0) {
524 if (node->tn_size <= uio->uio_offset)
526 len = MIN(node->tn_size - uio->uio_offset, resid);
529 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
530 if ((error != 0) || (resid == uio->uio_resid))
539 /* --------------------------------------------------------------------- */
542 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
553 addr = uio->uio_offset;
554 idx = OFF_TO_IDX(addr);
555 offset = addr & PAGE_MASK;
556 tlen = MIN(PAGE_SIZE - offset, len);
558 if ((vobj == NULL) || (vobj->resident_page_count == 0)) {
563 VM_OBJECT_LOCK(vobj);
565 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
566 vm_page_is_valid(vpg, offset, tlen)) {
567 if (vm_page_sleep_if_busy(vpg, FALSE, "tmfsmw"))
570 vm_page_lock_queues();
571 vm_page_undirty(vpg);
572 vm_page_unlock_queues();
573 VM_OBJECT_UNLOCK(vobj);
574 error = uiomove_fromphys(&vpg, offset, tlen, uio);
576 VM_OBJECT_UNLOCK(vobj);
580 VM_OBJECT_LOCK(tobj);
581 vm_object_pip_add(tobj, 1);
582 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
583 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
584 if (tpg->valid != VM_PAGE_BITS_ALL) {
585 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
586 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
588 printf("tmpfs get pages from pager error [write]\n");
592 vm_page_zero_invalid(tpg, TRUE);
594 VM_OBJECT_UNLOCK(tobj);
596 error = uiomove_fromphys(&tpg, offset, tlen, uio);
598 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
599 pmap_copy_page(vpg, tpg);
601 VM_OBJECT_LOCK(tobj);
604 VM_OBJECT_LOCK(vobj);
605 vm_page_lock_queues();
607 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
608 ("parts of tpg invalid"));
611 vm_page_unwire(tpg, TRUE);
612 vm_page_unlock_queues();
617 VM_OBJECT_UNLOCK(vobj);
618 vm_object_pip_subtract(tobj, 1);
619 VM_OBJECT_UNLOCK(tobj);
625 tmpfs_write(struct vop_write_args *v)
627 struct vnode *vp = v->a_vp;
628 struct uio *uio = v->a_uio;
629 int ioflag = v->a_ioflag;
630 struct thread *td = uio->uio_td;
635 struct tmpfs_node *node;
640 node = VP_TO_TMPFS_NODE(vp);
641 oldsize = node->tn_size;
643 if (uio->uio_offset < 0 || vp->v_type != VREG) {
648 if (uio->uio_resid == 0) {
653 if (ioflag & IO_APPEND)
654 uio->uio_offset = node->tn_size;
656 if (uio->uio_offset + uio->uio_resid >
657 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
660 if (vp->v_type == VREG && td != NULL) {
661 PROC_LOCK(td->td_proc);
662 if (uio->uio_offset + uio->uio_resid >
663 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
664 psignal(td->td_proc, SIGXFSZ);
665 PROC_UNLOCK(td->td_proc);
668 PROC_UNLOCK(td->td_proc);
671 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
673 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
678 uobj = node->tn_reg.tn_aobj;
679 while ((resid = uio->uio_resid) > 0) {
680 if (node->tn_size <= uio->uio_offset)
682 len = MIN(node->tn_size - uio->uio_offset, resid);
685 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
686 if ((error != 0) || (resid == uio->uio_resid))
690 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
691 (extended ? TMPFS_NODE_CHANGED : 0);
693 if (node->tn_mode & (S_ISUID | S_ISGID)) {
694 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
695 node->tn_mode &= ~(S_ISUID | S_ISGID);
699 (void)tmpfs_reg_resize(vp, oldsize);
702 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
703 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
708 /* --------------------------------------------------------------------- */
711 tmpfs_fsync(struct vop_fsync_args *v)
713 struct vnode *vp = v->a_vp;
715 MPASS(VOP_ISLOCKED(vp));
722 /* --------------------------------------------------------------------- */
725 tmpfs_remove(struct vop_remove_args *v)
727 struct vnode *dvp = v->a_dvp;
728 struct vnode *vp = v->a_vp;
731 struct tmpfs_dirent *de;
732 struct tmpfs_mount *tmp;
733 struct tmpfs_node *dnode;
734 struct tmpfs_node *node;
736 MPASS(VOP_ISLOCKED(dvp));
737 MPASS(VOP_ISLOCKED(vp));
739 if (vp->v_type == VDIR) {
744 dnode = VP_TO_TMPFS_DIR(dvp);
745 node = VP_TO_TMPFS_NODE(vp);
746 tmp = VFS_TO_TMPFS(vp->v_mount);
747 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
750 /* Files marked as immutable or append-only cannot be deleted. */
751 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
752 (dnode->tn_flags & APPEND)) {
757 /* Remove the entry from the directory; as it is a file, we do not
758 * have to change the number of hard links of the directory. */
759 tmpfs_dir_detach(dvp, de);
761 /* Free the directory entry we just deleted. Note that the node
762 * referred by it will not be removed until the vnode is really
764 tmpfs_free_dirent(tmp, de, TRUE);
766 if (node->tn_links > 0)
767 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
776 /* --------------------------------------------------------------------- */
779 tmpfs_link(struct vop_link_args *v)
781 struct vnode *dvp = v->a_tdvp;
782 struct vnode *vp = v->a_vp;
783 struct componentname *cnp = v->a_cnp;
786 struct tmpfs_dirent *de;
787 struct tmpfs_node *node;
789 MPASS(VOP_ISLOCKED(dvp));
790 MPASS(cnp->cn_flags & HASBUF);
791 MPASS(dvp != vp); /* XXX When can this be false? */
793 node = VP_TO_TMPFS_NODE(vp);
795 /* XXX: Why aren't the following two tests done by the caller? */
797 /* Hard links of directories are forbidden. */
798 if (vp->v_type == VDIR) {
803 /* Cannot create cross-device links. */
804 if (dvp->v_mount != vp->v_mount) {
809 /* Ensure that we do not overflow the maximum number of links imposed
811 MPASS(node->tn_links <= LINK_MAX);
812 if (node->tn_links == LINK_MAX) {
817 /* We cannot create links of files marked immutable or append-only. */
818 if (node->tn_flags & (IMMUTABLE | APPEND)) {
823 /* Allocate a new directory entry to represent the node. */
824 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
825 cnp->cn_nameptr, cnp->cn_namelen, &de);
829 /* Insert the new directory entry into the appropriate directory. */
830 tmpfs_dir_attach(dvp, de);
832 /* vp link count has changed, so update node times. */
833 node->tn_status |= TMPFS_NODE_CHANGED;
842 /* --------------------------------------------------------------------- */
845 tmpfs_rename(struct vop_rename_args *v)
847 struct vnode *fdvp = v->a_fdvp;
848 struct vnode *fvp = v->a_fvp;
849 struct componentname *fcnp = v->a_fcnp;
850 struct vnode *tdvp = v->a_tdvp;
851 struct vnode *tvp = v->a_tvp;
852 struct componentname *tcnp = v->a_tcnp;
856 struct tmpfs_dirent *de;
857 struct tmpfs_node *fdnode;
858 struct tmpfs_node *fnode;
859 struct tmpfs_node *tnode;
860 struct tmpfs_node *tdnode;
862 MPASS(VOP_ISLOCKED(tdvp));
863 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
864 MPASS(fcnp->cn_flags & HASBUF);
865 MPASS(tcnp->cn_flags & HASBUF);
867 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
869 /* Disallow cross-device renames.
870 * XXX Why isn't this done by the caller? */
871 if (fvp->v_mount != tdvp->v_mount ||
872 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
877 tdnode = VP_TO_TMPFS_DIR(tdvp);
879 /* If source and target are the same file, there is nothing to do. */
885 /* If we need to move the directory between entries, lock the
886 * source so that we can safely operate on it. */
888 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
892 fdnode = VP_TO_TMPFS_DIR(fdvp);
893 fnode = VP_TO_TMPFS_NODE(fvp);
894 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
896 /* Avoid manipulating '.' and '..' entries. */
898 MPASS(fvp->v_type == VDIR);
902 MPASS(de->td_node == fnode);
904 /* If re-naming a directory to another preexisting directory
905 * ensure that the target directory is empty so that its
906 * removal causes no side effects.
907 * Kern_rename gurantees the destination to be a directory
908 * if the source is one. */
910 MPASS(tnode != NULL);
912 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
913 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
918 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
919 if (tnode->tn_size > 0) {
923 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
926 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
930 MPASS(fnode->tn_type != VDIR &&
931 tnode->tn_type != VDIR);
935 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
936 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
941 /* Ensure that we have enough memory to hold the new name, if it
942 * has to be changed. */
943 if (fcnp->cn_namelen != tcnp->cn_namelen ||
944 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
945 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
949 /* If the node is being moved to another directory, we have to do
951 if (fdnode != tdnode) {
952 /* In case we are moving a directory, we have to adjust its
953 * parent to point to the new parent. */
954 if (de->td_node->tn_type == VDIR) {
955 struct tmpfs_node *n;
957 /* Ensure the target directory is not a child of the
958 * directory being moved. Otherwise, we'd end up
959 * with stale nodes. */
961 while (n != n->tn_dir.tn_parent) {
965 free(newname, M_TMPFSNAME);
968 n = n->tn_dir.tn_parent;
971 /* Adjust the parent pointer. */
972 TMPFS_VALIDATE_DIR(fnode);
973 de->td_node->tn_dir.tn_parent = tdnode;
975 /* As a result of changing the target of the '..'
976 * entry, the link count of the source and target
977 * directories has to be adjusted. */
982 /* Do the move: just remove the entry from the source directory
983 * and insert it into the target one. */
984 tmpfs_dir_detach(fdvp, de);
985 tmpfs_dir_attach(tdvp, de);
988 /* If the name has changed, we need to make it effective by changing
989 * it in the directory entry. */
990 if (newname != NULL) {
991 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
993 free(de->td_name, M_TMPFSNAME);
994 de->td_namelen = (uint16_t)tcnp->cn_namelen;
995 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
996 de->td_name = newname;
998 fnode->tn_status |= TMPFS_NODE_CHANGED;
999 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1002 /* If we are overwriting an entry, we have to remove the old one
1003 * from the target directory. */
1005 /* Remove the old entry from the target directory. */
1006 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1007 tmpfs_dir_detach(tdvp, de);
1009 /* Free the directory entry we just deleted. Note that the
1010 * node referred by it will not be removed until the vnode is
1011 * really reclaimed. */
1012 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1018 if (fdnode != tdnode)
1019 VOP_UNLOCK(fdvp, 0);
1022 /* Release target nodes. */
1023 /* XXX: I don't understand when tdvp can be the same as tvp, but
1024 * other code takes care of this... */
1032 /* Release source nodes. */
1039 /* --------------------------------------------------------------------- */
1042 tmpfs_mkdir(struct vop_mkdir_args *v)
1044 struct vnode *dvp = v->a_dvp;
1045 struct vnode **vpp = v->a_vpp;
1046 struct componentname *cnp = v->a_cnp;
1047 struct vattr *vap = v->a_vap;
1049 MPASS(vap->va_type == VDIR);
1051 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1054 /* --------------------------------------------------------------------- */
1057 tmpfs_rmdir(struct vop_rmdir_args *v)
1059 struct vnode *dvp = v->a_dvp;
1060 struct vnode *vp = v->a_vp;
1063 struct tmpfs_dirent *de;
1064 struct tmpfs_mount *tmp;
1065 struct tmpfs_node *dnode;
1066 struct tmpfs_node *node;
1068 MPASS(VOP_ISLOCKED(dvp));
1069 MPASS(VOP_ISLOCKED(vp));
1071 tmp = VFS_TO_TMPFS(dvp->v_mount);
1072 dnode = VP_TO_TMPFS_DIR(dvp);
1073 node = VP_TO_TMPFS_DIR(vp);
1075 /* Directories with more than two entries ('.' and '..') cannot be
1077 if (node->tn_size > 0) {
1082 if ((dnode->tn_flags & APPEND)
1083 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1088 /* This invariant holds only if we are not trying to remove "..".
1089 * We checked for that above so this is safe now. */
1090 MPASS(node->tn_dir.tn_parent == dnode);
1092 /* Get the directory entry associated with node (vp). This was
1093 * filled by tmpfs_lookup while looking up the entry. */
1094 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1095 MPASS(TMPFS_DIRENT_MATCHES(de,
1096 v->a_cnp->cn_nameptr,
1097 v->a_cnp->cn_namelen));
1099 /* Check flags to see if we are allowed to remove the directory. */
1100 if (dnode->tn_flags & APPEND
1101 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1106 /* Detach the directory entry from the directory (dnode). */
1107 tmpfs_dir_detach(dvp, de);
1110 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1111 TMPFS_NODE_MODIFIED;
1112 node->tn_dir.tn_parent->tn_links--;
1113 node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1114 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1119 /* Free the directory entry we just deleted. Note that the node
1120 * referred by it will not be removed until the vnode is really
1122 tmpfs_free_dirent(tmp, de, TRUE);
1124 /* Release the deleted vnode (will destroy the node, notify
1125 * interested parties and clean it from the cache). */
1127 dnode->tn_status |= TMPFS_NODE_CHANGED;
1136 /* --------------------------------------------------------------------- */
1139 tmpfs_symlink(struct vop_symlink_args *v)
1141 struct vnode *dvp = v->a_dvp;
1142 struct vnode **vpp = v->a_vpp;
1143 struct componentname *cnp = v->a_cnp;
1144 struct vattr *vap = v->a_vap;
1145 char *target = v->a_target;
1147 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1148 MPASS(vap->va_type == VLNK);
1150 vap->va_type = VLNK;
1153 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1156 /* --------------------------------------------------------------------- */
1159 tmpfs_readdir(struct vop_readdir_args *v)
1161 struct vnode *vp = v->a_vp;
1162 struct uio *uio = v->a_uio;
1163 int *eofflag = v->a_eofflag;
1164 u_long **cookies = v->a_cookies;
1165 int *ncookies = v->a_ncookies;
1170 struct tmpfs_node *node;
1172 /* This operation only makes sense on directory nodes. */
1173 if (vp->v_type != VDIR)
1176 node = VP_TO_TMPFS_DIR(vp);
1178 startoff = uio->uio_offset;
1180 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1181 error = tmpfs_dir_getdotdent(node, uio);
1187 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1188 error = tmpfs_dir_getdotdotdent(node, uio);
1194 error = tmpfs_dir_getdents(node, uio, &cnt);
1202 if (eofflag != NULL)
1204 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1206 /* Update NFS-related variables. */
1207 if (error == 0 && cookies != NULL && ncookies != NULL) {
1209 off_t off = startoff;
1210 struct tmpfs_dirent *de = NULL;
1213 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1215 for (i = 0; i < cnt; i++) {
1216 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1217 if (off == TMPFS_DIRCOOKIE_DOT) {
1218 off = TMPFS_DIRCOOKIE_DOTDOT;
1220 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1221 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1222 } else if (de != NULL) {
1223 de = TAILQ_NEXT(de, td_entries);
1225 de = tmpfs_dir_lookupbycookie(node,
1228 de = TAILQ_NEXT(de, td_entries);
1231 off = TMPFS_DIRCOOKIE_EOF;
1233 off = tmpfs_dircookie(de);
1236 (*cookies)[i] = off;
1238 MPASS(uio->uio_offset == off);
1244 /* --------------------------------------------------------------------- */
1247 tmpfs_readlink(struct vop_readlink_args *v)
1249 struct vnode *vp = v->a_vp;
1250 struct uio *uio = v->a_uio;
1253 struct tmpfs_node *node;
1255 MPASS(uio->uio_offset == 0);
1256 MPASS(vp->v_type == VLNK);
1258 node = VP_TO_TMPFS_NODE(vp);
1260 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1262 node->tn_status |= TMPFS_NODE_ACCESSED;
1267 /* --------------------------------------------------------------------- */
1270 tmpfs_inactive(struct vop_inactive_args *v)
1272 struct vnode *vp = v->a_vp;
1273 struct thread *l = v->a_td;
1275 struct tmpfs_node *node;
1277 MPASS(VOP_ISLOCKED(vp));
1279 node = VP_TO_TMPFS_NODE(vp);
1281 if (node->tn_links == 0)
1287 /* --------------------------------------------------------------------- */
1290 tmpfs_reclaim(struct vop_reclaim_args *v)
1292 struct vnode *vp = v->a_vp;
1294 struct tmpfs_mount *tmp;
1295 struct tmpfs_node *node;
1297 node = VP_TO_TMPFS_NODE(vp);
1298 tmp = VFS_TO_TMPFS(vp->v_mount);
1300 vnode_destroy_vobject(vp);
1304 /* If the node referenced by this vnode was deleted by the user,
1305 * we must free its associated data structures (now that the vnode
1306 * is being reclaimed). */
1307 if (node->tn_links == 0)
1308 tmpfs_free_node(tmp, node);
1310 MPASS(vp->v_data == NULL);
1314 /* --------------------------------------------------------------------- */
1317 tmpfs_print(struct vop_print_args *v)
1319 struct vnode *vp = v->a_vp;
1321 struct tmpfs_node *node;
1323 node = VP_TO_TMPFS_NODE(vp);
1325 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1326 node, node->tn_flags, node->tn_links);
1327 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1329 node->tn_mode, node->tn_uid, node->tn_gid,
1330 (uintmax_t)node->tn_size, node->tn_status);
1332 if (vp->v_type == VFIFO)
1340 /* --------------------------------------------------------------------- */
1343 tmpfs_pathconf(struct vop_pathconf_args *v)
1345 int name = v->a_name;
1346 register_t *retval = v->a_retval;
1369 case _PC_CHOWN_RESTRICTED:
1381 case _PC_FILESIZEBITS:
1382 *retval = 0; /* XXX Don't know which value should I return. */
1393 tmpfs_vptofh(struct vop_vptofh_args *ap)
1395 struct tmpfs_fid *tfhp;
1396 struct tmpfs_node *node;
1398 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1399 node = VP_TO_TMPFS_NODE(ap->a_vp);
1401 tfhp->tf_len = sizeof(struct tmpfs_fid);
1402 tfhp->tf_id = node->tn_id;
1403 tfhp->tf_gen = node->tn_gen;
1408 /* --------------------------------------------------------------------- */
1411 * vnode operations vector used for files stored in a tmpfs file system.
1413 struct vop_vector tmpfs_vnodeop_entries = {
1414 .vop_default = &default_vnodeops,
1415 .vop_lookup = vfs_cache_lookup,
1416 .vop_cachedlookup = tmpfs_lookup,
1417 .vop_create = tmpfs_create,
1418 .vop_mknod = tmpfs_mknod,
1419 .vop_open = tmpfs_open,
1420 .vop_close = tmpfs_close,
1421 .vop_access = tmpfs_access,
1422 .vop_getattr = tmpfs_getattr,
1423 .vop_setattr = tmpfs_setattr,
1424 .vop_read = tmpfs_read,
1425 .vop_write = tmpfs_write,
1426 .vop_fsync = tmpfs_fsync,
1427 .vop_remove = tmpfs_remove,
1428 .vop_link = tmpfs_link,
1429 .vop_rename = tmpfs_rename,
1430 .vop_mkdir = tmpfs_mkdir,
1431 .vop_rmdir = tmpfs_rmdir,
1432 .vop_symlink = tmpfs_symlink,
1433 .vop_readdir = tmpfs_readdir,
1434 .vop_readlink = tmpfs_readlink,
1435 .vop_inactive = tmpfs_inactive,
1436 .vop_reclaim = tmpfs_reclaim,
1437 .vop_print = tmpfs_print,
1438 .vop_pathconf = tmpfs_pathconf,
1439 .vop_vptofh = tmpfs_vptofh,
1440 .vop_bmap = VOP_EOPNOTSUPP,