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>
55 #include <sys/sched.h>
56 #include <sys/sf_buf.h>
57 #include <machine/_inttypes.h>
59 #include <fs/fifofs/fifo.h>
60 #include <fs/tmpfs/tmpfs_vnops.h>
61 #include <fs/tmpfs/tmpfs.h>
63 /* --------------------------------------------------------------------- */
66 tmpfs_lookup(struct vop_cachedlookup_args *v)
68 struct vnode *dvp = v->a_dvp;
69 struct vnode **vpp = v->a_vpp;
70 struct componentname *cnp = v->a_cnp;
71 struct thread *td = cnp->cn_thread;
74 struct tmpfs_dirent *de;
75 struct tmpfs_node *dnode;
77 dnode = VP_TO_TMPFS_DIR(dvp);
80 /* Check accessibility of requested node as a first step. */
81 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
85 /* We cannot be requesting the parent directory of the root node. */
86 MPASS(IMPLIES(dnode->tn_type == VDIR &&
87 dnode->tn_dir.tn_parent == dnode,
88 !(cnp->cn_flags & ISDOTDOT)));
90 if (cnp->cn_flags & ISDOTDOT) {
93 ltype = VOP_ISLOCKED(dvp, td);
95 VOP_UNLOCK(dvp, 0, td);
96 /* Allocate a new vnode on the matching entry. */
97 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
98 cnp->cn_lkflags, vpp, td);
100 vn_lock(dvp, ltype | LK_RETRY, td);
102 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
107 de = tmpfs_dir_lookup(dnode, NULL, cnp);
109 /* The entry was not found in the directory.
110 * This is OK if we are creating or renaming an
111 * entry and are working on the last component of
113 if ((cnp->cn_flags & ISLASTCN) &&
114 (cnp->cn_nameiop == CREATE || \
115 cnp->cn_nameiop == RENAME)) {
116 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
121 /* Keep the component name in the buffer for
123 cnp->cn_flags |= SAVENAME;
129 struct tmpfs_node *tnode;
131 /* The entry was found, so get its associated
135 /* If we are not at the last path component and
136 * found a non-directory or non-link entry (which
137 * may itself be pointing to a directory), raise
139 if ((tnode->tn_type != VDIR &&
140 tnode->tn_type != VLNK) &&
141 !(cnp->cn_flags & ISLASTCN)) {
146 /* If we are deleting or renaming the entry, keep
147 * track of its tmpfs_dirent so that it can be
148 * easily deleted later. */
149 if ((cnp->cn_flags & ISLASTCN) &&
150 (cnp->cn_nameiop == DELETE ||
151 cnp->cn_nameiop == RENAME)) {
152 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
157 /* Allocate a new vnode on the matching entry. */
158 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
159 cnp->cn_lkflags, vpp, td);
163 if ((dnode->tn_mode & S_ISTXT) &&
164 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
165 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
171 cnp->cn_flags |= SAVENAME;
173 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
174 cnp->cn_lkflags, vpp, td);
179 /* Store the result of this lookup in the cache. Avoid this if the
180 * request was for creation, as it does not improve timings on
182 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
183 cache_enter(dvp, *vpp, cnp);
186 /* If there were no errors, *vpp cannot be null and it must be
188 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp, td)));
193 /* --------------------------------------------------------------------- */
196 tmpfs_create(struct vop_create_args *v)
198 struct vnode *dvp = v->a_dvp;
199 struct vnode **vpp = v->a_vpp;
200 struct componentname *cnp = v->a_cnp;
201 struct vattr *vap = v->a_vap;
203 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
205 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
207 /* --------------------------------------------------------------------- */
210 tmpfs_mknod(struct vop_mknod_args *v)
212 struct vnode *dvp = v->a_dvp;
213 struct vnode **vpp = v->a_vpp;
214 struct componentname *cnp = v->a_cnp;
215 struct vattr *vap = v->a_vap;
217 if (vap->va_type != VBLK && vap->va_type != VCHR &&
218 vap->va_type != VFIFO)
221 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
224 /* --------------------------------------------------------------------- */
227 tmpfs_open(struct vop_open_args *v)
229 struct vnode *vp = v->a_vp;
230 int mode = v->a_mode;
233 struct tmpfs_node *node;
235 MPASS(VOP_ISLOCKED(vp, v->a_td));
237 node = VP_TO_TMPFS_NODE(vp);
239 /* The file is still active but all its names have been removed
240 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
241 * it is about to die. */
242 if (node->tn_links < 1)
245 /* If the file is marked append-only, deny write requests. */
246 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
250 vnode_create_vobject(vp, node->tn_size, v->a_td);
253 MPASS(VOP_ISLOCKED(vp, v->a_td));
257 /* --------------------------------------------------------------------- */
260 tmpfs_close(struct vop_close_args *v)
262 struct vnode *vp = v->a_vp;
264 struct tmpfs_node *node;
266 MPASS(VOP_ISLOCKED(vp, v->a_td));
268 node = VP_TO_TMPFS_NODE(vp);
270 if (node->tn_links > 0) {
271 /* Update node times. No need to do it if the node has
272 * been deleted, because it will vanish after we return. */
279 /* --------------------------------------------------------------------- */
282 tmpfs_access(struct vop_access_args *v)
284 struct vnode *vp = v->a_vp;
285 int mode = v->a_mode;
286 struct ucred *cred = v->a_cred;
289 struct tmpfs_node *node;
291 MPASS(VOP_ISLOCKED(vp, v->a_td));
293 node = VP_TO_TMPFS_NODE(vp);
295 switch (vp->v_type) {
301 if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
321 if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
326 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
327 node->tn_gid, mode, cred, NULL);
330 MPASS(VOP_ISLOCKED(vp, v->a_td));
335 /* --------------------------------------------------------------------- */
338 tmpfs_getattr(struct vop_getattr_args *v)
340 struct vnode *vp = v->a_vp;
341 struct vattr *vap = v->a_vap;
343 struct tmpfs_node *node;
345 node = VP_TO_TMPFS_NODE(vp);
349 vap->va_type = vp->v_type;
350 vap->va_mode = node->tn_mode;
351 vap->va_nlink = node->tn_links;
352 vap->va_uid = node->tn_uid;
353 vap->va_gid = node->tn_gid;
354 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
355 vap->va_fileid = node->tn_id;
356 vap->va_size = node->tn_size;
357 vap->va_blocksize = PAGE_SIZE;
358 vap->va_atime = node->tn_atime;
359 vap->va_mtime = node->tn_mtime;
360 vap->va_ctime = node->tn_ctime;
361 vap->va_birthtime = node->tn_birthtime;
362 vap->va_gen = node->tn_gen;
363 vap->va_flags = node->tn_flags;
364 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
365 node->tn_rdev : NODEV;
366 vap->va_bytes = round_page(node->tn_size);
372 /* --------------------------------------------------------------------- */
374 /* XXX Should this operation be atomic? I think it should, but code in
375 * XXX other places (e.g., ufs) doesn't seem to be... */
377 tmpfs_setattr(struct vop_setattr_args *v)
379 struct vnode *vp = v->a_vp;
380 struct vattr *vap = v->a_vap;
381 struct ucred *cred = v->a_cred;
382 struct thread *l = v->a_td;
386 MPASS(VOP_ISLOCKED(vp, l));
390 /* Abort if any unsettable attribute is given. */
391 if (vap->va_type != VNON ||
392 vap->va_nlink != VNOVAL ||
393 vap->va_fsid != VNOVAL ||
394 vap->va_fileid != VNOVAL ||
395 vap->va_blocksize != VNOVAL ||
396 vap->va_gen != VNOVAL ||
397 vap->va_rdev != VNOVAL ||
398 vap->va_bytes != VNOVAL)
401 if (error == 0 && (vap->va_flags != VNOVAL))
402 error = tmpfs_chflags(vp, vap->va_flags, cred, l);
404 if (error == 0 && (vap->va_size != VNOVAL))
405 error = tmpfs_chsize(vp, vap->va_size, cred, l);
407 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
408 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred,
411 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
412 error = tmpfs_chmod(vp, vap->va_mode, cred, l);
414 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
415 vap->va_atime.tv_nsec != VNOVAL) ||
416 (vap->va_mtime.tv_sec != VNOVAL &&
417 vap->va_mtime.tv_nsec != VNOVAL) ||
418 (vap->va_birthtime.tv_sec != VNOVAL &&
419 vap->va_birthtime.tv_nsec != VNOVAL)))
420 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
421 &vap->va_birthtime, vap->va_vaflags, cred, l);
423 /* Update the node times. We give preference to the error codes
424 * generated by this function rather than the ones that may arise
425 * from tmpfs_update. */
428 MPASS(VOP_ISLOCKED(vp, l));
433 /* --------------------------------------------------------------------- */
436 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
446 addr = uio->uio_offset;
447 idx = OFF_TO_IDX(addr);
448 offset = addr & PAGE_MASK;
449 tlen = MIN(PAGE_SIZE - offset, len);
451 if ((vobj == NULL) || (vobj->resident_page_count == 0))
454 VM_OBJECT_LOCK(vobj);
456 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
457 vm_page_is_valid(m, offset, tlen)) {
458 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
461 VM_OBJECT_UNLOCK(vobj);
463 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
464 va = (caddr_t)sf_buf_kva(sf);
465 error = uiomove(va + offset, tlen, uio);
468 VM_OBJECT_LOCK(vobj);
470 VM_OBJECT_UNLOCK(vobj);
473 VM_OBJECT_UNLOCK(vobj);
475 VM_OBJECT_LOCK(tobj);
476 vm_object_pip_add(tobj, 1);
477 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
478 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
479 if (m->valid != VM_PAGE_BITS_ALL) {
481 if (vm_pager_has_page(tobj, idx, &behind, &ahead)) {
482 error = vm_pager_get_pages(tobj, &m, 1, 0);
484 printf("tmpfs get pages from pager error [read]\n");
488 vm_page_zero_invalid(m, TRUE);
490 VM_OBJECT_UNLOCK(tobj);
492 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
493 va = (caddr_t)sf_buf_kva(sf);
494 error = uiomove(va + offset, tlen, uio);
497 VM_OBJECT_LOCK(tobj);
499 vm_page_lock_queues();
500 vm_page_unwire(m, 0);
502 vm_page_unlock_queues();
504 vm_object_pip_subtract(tobj, 1);
505 VM_OBJECT_UNLOCK(tobj);
511 tmpfs_read(struct vop_read_args *v)
513 struct vnode *vp = v->a_vp;
514 struct uio *uio = v->a_uio;
516 struct tmpfs_node *node;
523 node = VP_TO_TMPFS_NODE(vp);
525 if (vp->v_type != VREG) {
530 if (uio->uio_offset < 0) {
535 node->tn_status |= TMPFS_NODE_ACCESSED;
537 uobj = node->tn_reg.tn_aobj;
538 while ((resid = uio->uio_resid) > 0) {
540 if (node->tn_size <= uio->uio_offset)
542 len = MIN(node->tn_size - uio->uio_offset, resid);
545 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
546 if ((error != 0) || (resid == uio->uio_resid))
555 /* --------------------------------------------------------------------- */
558 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
568 addr = uio->uio_offset;
569 idx = OFF_TO_IDX(addr);
570 offset = addr & PAGE_MASK;
571 tlen = MIN(PAGE_SIZE - offset, len);
573 if ((vobj == NULL) || (vobj->resident_page_count == 0)) {
578 VM_OBJECT_LOCK(vobj);
580 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
581 vm_page_is_valid(vpg, offset, tlen)) {
582 if (vm_page_sleep_if_busy(vpg, FALSE, "tmfsmw"))
585 vm_page_lock_queues();
586 vm_page_undirty(vpg);
587 vm_page_unlock_queues();
588 VM_OBJECT_UNLOCK(vobj);
590 sf = sf_buf_alloc(vpg, SFB_CPUPRIVATE);
591 va = (caddr_t)sf_buf_kva(sf);
592 error = uiomove(va + offset, tlen, uio);
596 VM_OBJECT_UNLOCK(vobj);
600 VM_OBJECT_LOCK(tobj);
601 vm_object_pip_add(tobj, 1);
602 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
603 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
604 if (tpg->valid != VM_PAGE_BITS_ALL) {
606 if (vm_pager_has_page(tobj, idx, &behind, &ahead)) {
607 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
609 printf("tmpfs get pages from pager error [write]\n");
613 vm_page_zero_invalid(tpg, TRUE);
615 VM_OBJECT_UNLOCK(tobj);
618 sf = sf_buf_alloc(tpg, SFB_CPUPRIVATE);
619 va = (caddr_t)sf_buf_kva(sf);
620 error = uiomove(va + offset, tlen, uio);
624 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
625 pmap_copy_page(vpg, tpg);
627 VM_OBJECT_LOCK(tobj);
630 VM_OBJECT_LOCK(vobj);
631 vm_page_lock_queues();
633 vm_page_set_validclean(tpg, offset, tlen);
634 vm_page_zero_invalid(tpg, TRUE);
637 vm_page_unwire(tpg, 0);
638 vm_page_activate(tpg);
639 vm_page_unlock_queues();
644 VM_OBJECT_UNLOCK(vobj);
645 vm_object_pip_subtract(tobj, 1);
646 VM_OBJECT_UNLOCK(tobj);
652 tmpfs_write(struct vop_write_args *v)
654 struct vnode *vp = v->a_vp;
655 struct uio *uio = v->a_uio;
656 int ioflag = v->a_ioflag;
657 struct thread *td = uio->uio_td;
662 struct tmpfs_node *node;
667 node = VP_TO_TMPFS_NODE(vp);
668 oldsize = node->tn_size;
670 if (uio->uio_offset < 0 || vp->v_type != VREG) {
675 if (uio->uio_resid == 0) {
680 if (ioflag & IO_APPEND)
681 uio->uio_offset = node->tn_size;
683 if (uio->uio_offset + uio->uio_resid >
684 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
687 if (vp->v_type == VREG && td != NULL) {
688 PROC_LOCK(td->td_proc);
689 if (uio->uio_offset + uio->uio_resid >
690 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
691 psignal(td->td_proc, SIGXFSZ);
692 PROC_UNLOCK(td->td_proc);
695 PROC_UNLOCK(td->td_proc);
698 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
700 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
705 uobj = node->tn_reg.tn_aobj;
706 while ((resid = uio->uio_resid) > 0) {
707 if (node->tn_size <= uio->uio_offset)
709 len = MIN(node->tn_size - uio->uio_offset, resid);
712 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
713 if ((error != 0) || (resid == uio->uio_resid))
717 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
718 (extended ? TMPFS_NODE_CHANGED : 0);
720 if (node->tn_mode & (S_ISUID | S_ISGID)) {
721 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
722 node->tn_mode &= ~(S_ISUID | S_ISGID);
726 (void)tmpfs_reg_resize(vp, oldsize);
729 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
730 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
735 /* --------------------------------------------------------------------- */
738 tmpfs_fsync(struct vop_fsync_args *v)
740 struct vnode *vp = v->a_vp;
742 MPASS(VOP_ISLOCKED(vp, v->a_td));
749 /* --------------------------------------------------------------------- */
752 tmpfs_remove(struct vop_remove_args *v)
754 struct vnode *dvp = v->a_dvp;
755 struct vnode *vp = v->a_vp;
758 struct tmpfs_dirent *de;
759 struct tmpfs_mount *tmp;
760 struct tmpfs_node *dnode;
761 struct tmpfs_node *node;
763 MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread));
764 MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread));
766 if (vp->v_type == VDIR) {
771 dnode = VP_TO_TMPFS_DIR(dvp);
772 node = VP_TO_TMPFS_NODE(vp);
773 tmp = VFS_TO_TMPFS(vp->v_mount);
774 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
777 /* Files marked as immutable or append-only cannot be deleted. */
778 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
779 (dnode->tn_flags & APPEND)) {
784 /* Remove the entry from the directory; as it is a file, we do not
785 * have to change the number of hard links of the directory. */
786 tmpfs_dir_detach(dvp, de);
788 /* Free the directory entry we just deleted. Note that the node
789 * referred by it will not be removed until the vnode is really
791 tmpfs_free_dirent(tmp, de, TRUE);
793 if (node->tn_links > 0)
794 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
803 /* --------------------------------------------------------------------- */
806 tmpfs_link(struct vop_link_args *v)
808 struct vnode *dvp = v->a_tdvp;
809 struct vnode *vp = v->a_vp;
810 struct componentname *cnp = v->a_cnp;
813 struct tmpfs_dirent *de;
814 struct tmpfs_node *node;
816 MPASS(VOP_ISLOCKED(dvp, cnp->cn_thread));
817 MPASS(cnp->cn_flags & HASBUF);
818 MPASS(dvp != vp); /* XXX When can this be false? */
820 node = VP_TO_TMPFS_NODE(vp);
822 /* XXX: Why aren't the following two tests done by the caller? */
824 /* Hard links of directories are forbidden. */
825 if (vp->v_type == VDIR) {
830 /* Cannot create cross-device links. */
831 if (dvp->v_mount != vp->v_mount) {
836 /* Ensure that we do not overflow the maximum number of links imposed
838 MPASS(node->tn_links <= LINK_MAX);
839 if (node->tn_links == LINK_MAX) {
844 /* We cannot create links of files marked immutable or append-only. */
845 if (node->tn_flags & (IMMUTABLE | APPEND)) {
850 /* Allocate a new directory entry to represent the node. */
851 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
852 cnp->cn_nameptr, cnp->cn_namelen, &de);
856 /* Insert the new directory entry into the appropriate directory. */
857 tmpfs_dir_attach(dvp, de);
859 /* vp link count has changed, so update node times. */
860 node->tn_status |= TMPFS_NODE_CHANGED;
869 /* --------------------------------------------------------------------- */
872 tmpfs_rename(struct vop_rename_args *v)
874 struct vnode *fdvp = v->a_fdvp;
875 struct vnode *fvp = v->a_fvp;
876 struct componentname *fcnp = v->a_fcnp;
877 struct vnode *tdvp = v->a_tdvp;
878 struct vnode *tvp = v->a_tvp;
879 struct componentname *tcnp = v->a_tcnp;
883 struct tmpfs_dirent *de;
884 struct tmpfs_node *fdnode;
885 struct tmpfs_node *fnode;
886 struct tmpfs_node *tnode;
887 struct tmpfs_node *tdnode;
889 MPASS(VOP_ISLOCKED(tdvp, tcnp->cn_thread));
890 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp, tcnp->cn_thread)));
891 MPASS(fcnp->cn_flags & HASBUF);
892 MPASS(tcnp->cn_flags & HASBUF);
894 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
896 /* Disallow cross-device renames.
897 * XXX Why isn't this done by the caller? */
898 if (fvp->v_mount != tdvp->v_mount ||
899 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
904 tdnode = VP_TO_TMPFS_DIR(tdvp);
906 /* If source and target are the same file, there is nothing to do. */
912 /* If we need to move the directory between entries, lock the
913 * source so that we can safely operate on it. */
915 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, tcnp->cn_thread);
919 fdnode = VP_TO_TMPFS_DIR(fdvp);
920 fnode = VP_TO_TMPFS_NODE(fvp);
921 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
923 /* Avoid manipulating '.' and '..' entries. */
925 MPASS(fvp->v_type == VDIR);
929 MPASS(de->td_node == fnode);
931 /* If re-naming a directory to another preexisting directory
932 * ensure that the target directory is empty so that its
933 * removal causes no side effects.
934 * Kern_rename gurantees the destination to be a directory
935 * if the source is one. */
937 MPASS(tnode != NULL);
939 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
940 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
945 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
946 if (tnode->tn_size > 0) {
950 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
953 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
957 MPASS(fnode->tn_type != VDIR &&
958 tnode->tn_type != VDIR);
962 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
963 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
968 /* Ensure that we have enough memory to hold the new name, if it
969 * has to be changed. */
970 if (fcnp->cn_namelen != tcnp->cn_namelen ||
971 memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
972 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
976 /* If the node is being moved to another directory, we have to do
978 if (fdnode != tdnode) {
979 /* In case we are moving a directory, we have to adjust its
980 * parent to point to the new parent. */
981 if (de->td_node->tn_type == VDIR) {
982 struct tmpfs_node *n;
984 /* Ensure the target directory is not a child of the
985 * directory being moved. Otherwise, we'd end up
986 * with stale nodes. */
988 while (n != n->tn_dir.tn_parent) {
992 free(newname, M_TMPFSNAME);
995 n = n->tn_dir.tn_parent;
998 /* Adjust the parent pointer. */
999 TMPFS_VALIDATE_DIR(fnode);
1000 de->td_node->tn_dir.tn_parent = tdnode;
1002 /* As a result of changing the target of the '..'
1003 * entry, the link count of the source and target
1004 * directories has to be adjusted. */
1009 /* Do the move: just remove the entry from the source directory
1010 * and insert it into the target one. */
1011 tmpfs_dir_detach(fdvp, de);
1012 tmpfs_dir_attach(tdvp, de);
1015 /* If the name has changed, we need to make it effective by changing
1016 * it in the directory entry. */
1017 if (newname != NULL) {
1018 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1020 free(de->td_name, M_TMPFSNAME);
1021 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1022 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1023 de->td_name = newname;
1025 fnode->tn_status |= TMPFS_NODE_CHANGED;
1026 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1029 /* If we are overwriting an entry, we have to remove the old one
1030 * from the target directory. */
1032 /* Remove the old entry from the target directory. */
1033 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1034 tmpfs_dir_detach(tdvp, de);
1036 /* Free the directory entry we just deleted. Note that the
1037 * node referred by it will not be removed until the vnode is
1038 * really reclaimed. */
1039 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1045 if (fdnode != tdnode)
1046 VOP_UNLOCK(fdvp, 0, tcnp->cn_thread);
1049 /* Release target nodes. */
1050 /* XXX: I don't understand when tdvp can be the same as tvp, but
1051 * other code takes care of this... */
1059 /* Release source nodes. */
1066 /* --------------------------------------------------------------------- */
1069 tmpfs_mkdir(struct vop_mkdir_args *v)
1071 struct vnode *dvp = v->a_dvp;
1072 struct vnode **vpp = v->a_vpp;
1073 struct componentname *cnp = v->a_cnp;
1074 struct vattr *vap = v->a_vap;
1076 MPASS(vap->va_type == VDIR);
1078 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1081 /* --------------------------------------------------------------------- */
1084 tmpfs_rmdir(struct vop_rmdir_args *v)
1086 struct vnode *dvp = v->a_dvp;
1087 struct vnode *vp = v->a_vp;
1090 struct tmpfs_dirent *de;
1091 struct tmpfs_mount *tmp;
1092 struct tmpfs_node *dnode;
1093 struct tmpfs_node *node;
1095 MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread));
1096 MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread));
1098 tmp = VFS_TO_TMPFS(dvp->v_mount);
1099 dnode = VP_TO_TMPFS_DIR(dvp);
1100 node = VP_TO_TMPFS_DIR(vp);
1102 /* Directories with more than two entries ('.' and '..') cannot be
1104 if (node->tn_size > 0) {
1109 if ((dnode->tn_flags & APPEND)
1110 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1115 /* This invariant holds only if we are not trying to remove "..".
1116 * We checked for that above so this is safe now. */
1117 MPASS(node->tn_dir.tn_parent == dnode);
1119 /* Get the directory entry associated with node (vp). This was
1120 * filled by tmpfs_lookup while looking up the entry. */
1121 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1122 MPASS(TMPFS_DIRENT_MATCHES(de,
1123 v->a_cnp->cn_nameptr,
1124 v->a_cnp->cn_namelen));
1126 /* Check flags to see if we are allowed to remove the directory. */
1127 if (dnode->tn_flags & APPEND
1128 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1133 /* Detach the directory entry from the directory (dnode). */
1134 tmpfs_dir_detach(dvp, de);
1137 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1138 TMPFS_NODE_MODIFIED;
1139 node->tn_dir.tn_parent->tn_links--;
1140 node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1141 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1146 /* Free the directory entry we just deleted. Note that the node
1147 * referred by it will not be removed until the vnode is really
1149 tmpfs_free_dirent(tmp, de, TRUE);
1151 /* Release the deleted vnode (will destroy the node, notify
1152 * interested parties and clean it from the cache). */
1154 dnode->tn_status |= TMPFS_NODE_CHANGED;
1163 /* --------------------------------------------------------------------- */
1166 tmpfs_symlink(struct vop_symlink_args *v)
1168 struct vnode *dvp = v->a_dvp;
1169 struct vnode **vpp = v->a_vpp;
1170 struct componentname *cnp = v->a_cnp;
1171 struct vattr *vap = v->a_vap;
1172 char *target = v->a_target;
1174 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1175 MPASS(vap->va_type == VLNK);
1177 vap->va_type = VLNK;
1180 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1183 /* --------------------------------------------------------------------- */
1186 tmpfs_readdir(struct vop_readdir_args *v)
1188 struct vnode *vp = v->a_vp;
1189 struct uio *uio = v->a_uio;
1190 int *eofflag = v->a_eofflag;
1191 u_long **cookies = v->a_cookies;
1192 int *ncookies = v->a_ncookies;
1197 struct tmpfs_node *node;
1199 /* This operation only makes sense on directory nodes. */
1200 if (vp->v_type != VDIR)
1203 node = VP_TO_TMPFS_DIR(vp);
1205 startoff = uio->uio_offset;
1207 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1208 error = tmpfs_dir_getdotdent(node, uio);
1214 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1215 error = tmpfs_dir_getdotdotdent(node, uio);
1221 error = tmpfs_dir_getdents(node, uio, &cnt);
1229 if (eofflag != NULL)
1231 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1233 /* Update NFS-related variables. */
1234 if (error == 0 && cookies != NULL && ncookies != NULL) {
1236 off_t off = startoff;
1237 struct tmpfs_dirent *de = NULL;
1240 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1242 for (i = 0; i < cnt; i++) {
1243 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1244 if (off == TMPFS_DIRCOOKIE_DOT) {
1245 off = TMPFS_DIRCOOKIE_DOTDOT;
1247 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1248 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1249 } else if (de != NULL) {
1250 de = TAILQ_NEXT(de, td_entries);
1252 de = tmpfs_dir_lookupbycookie(node,
1255 de = TAILQ_NEXT(de, td_entries);
1258 off = TMPFS_DIRCOOKIE_EOF;
1260 off = tmpfs_dircookie(de);
1263 (*cookies)[i] = off;
1265 MPASS(uio->uio_offset == off);
1271 /* --------------------------------------------------------------------- */
1274 tmpfs_readlink(struct vop_readlink_args *v)
1276 struct vnode *vp = v->a_vp;
1277 struct uio *uio = v->a_uio;
1280 struct tmpfs_node *node;
1282 MPASS(uio->uio_offset == 0);
1283 MPASS(vp->v_type == VLNK);
1285 node = VP_TO_TMPFS_NODE(vp);
1287 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1289 node->tn_status |= TMPFS_NODE_ACCESSED;
1294 /* --------------------------------------------------------------------- */
1297 tmpfs_inactive(struct vop_inactive_args *v)
1299 struct vnode *vp = v->a_vp;
1300 struct thread *l = v->a_td;
1302 struct tmpfs_node *node;
1304 MPASS(VOP_ISLOCKED(vp, l));
1306 node = VP_TO_TMPFS_NODE(vp);
1308 if (node->tn_links == 0)
1314 /* --------------------------------------------------------------------- */
1317 tmpfs_reclaim(struct vop_reclaim_args *v)
1319 struct vnode *vp = v->a_vp;
1321 struct tmpfs_mount *tmp;
1322 struct tmpfs_node *node;
1324 node = VP_TO_TMPFS_NODE(vp);
1325 tmp = VFS_TO_TMPFS(vp->v_mount);
1327 vnode_destroy_vobject(vp);
1331 /* If the node referenced by this vnode was deleted by the user,
1332 * we must free its associated data structures (now that the vnode
1333 * is being reclaimed). */
1334 if (node->tn_links == 0)
1335 tmpfs_free_node(tmp, node);
1337 MPASS(vp->v_data == NULL);
1341 /* --------------------------------------------------------------------- */
1344 tmpfs_print(struct vop_print_args *v)
1346 struct vnode *vp = v->a_vp;
1348 struct tmpfs_node *node;
1350 node = VP_TO_TMPFS_NODE(vp);
1352 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1353 node, node->tn_flags, node->tn_links);
1354 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1356 node->tn_mode, node->tn_uid, node->tn_gid,
1357 (uintmax_t)node->tn_size, node->tn_status);
1359 if (vp->v_type == VFIFO)
1367 /* --------------------------------------------------------------------- */
1370 tmpfs_pathconf(struct vop_pathconf_args *v)
1372 int name = v->a_name;
1373 register_t *retval = v->a_retval;
1396 case _PC_CHOWN_RESTRICTED:
1408 case _PC_FILESIZEBITS:
1409 *retval = 0; /* XXX Don't know which value should I return. */
1420 tmpfs_vptofh(struct vop_vptofh_args *ap)
1422 struct tmpfs_fid *tfhp;
1423 struct tmpfs_node *node;
1425 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1426 node = VP_TO_TMPFS_NODE(ap->a_vp);
1428 tfhp->tf_len = sizeof(struct tmpfs_fid);
1429 tfhp->tf_id = node->tn_id;
1430 tfhp->tf_gen = node->tn_gen;
1435 /* --------------------------------------------------------------------- */
1438 * vnode operations vector used for files stored in a tmpfs file system.
1440 struct vop_vector tmpfs_vnodeop_entries = {
1441 .vop_default = &default_vnodeops,
1442 .vop_lookup = vfs_cache_lookup,
1443 .vop_cachedlookup = tmpfs_lookup,
1444 .vop_create = tmpfs_create,
1445 .vop_mknod = tmpfs_mknod,
1446 .vop_open = tmpfs_open,
1447 .vop_close = tmpfs_close,
1448 .vop_access = tmpfs_access,
1449 .vop_getattr = tmpfs_getattr,
1450 .vop_setattr = tmpfs_setattr,
1451 .vop_read = tmpfs_read,
1452 .vop_write = tmpfs_write,
1453 .vop_fsync = tmpfs_fsync,
1454 .vop_remove = tmpfs_remove,
1455 .vop_link = tmpfs_link,
1456 .vop_rename = tmpfs_rename,
1457 .vop_mkdir = tmpfs_mkdir,
1458 .vop_rmdir = tmpfs_rmdir,
1459 .vop_symlink = tmpfs_symlink,
1460 .vop_readdir = tmpfs_readdir,
1461 .vop_readlink = tmpfs_readlink,
1462 .vop_inactive = tmpfs_inactive,
1463 .vop_reclaim = tmpfs_reclaim,
1464 .vop_print = tmpfs_print,
1465 .vop_pathconf = tmpfs_pathconf,
1466 .vop_vptofh = tmpfs_vptofh,
1467 .vop_bmap = VOP_EOPNOTSUPP,