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/sched.h>
46 #include <sys/sf_buf.h>
48 #include <sys/systm.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/vnode.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
59 #include <machine/_inttypes.h>
61 #include <fs/fifofs/fifo.h>
62 #include <fs/tmpfs/tmpfs_vnops.h>
63 #include <fs/tmpfs/tmpfs.h>
65 SYSCTL_DECL(_vfs_tmpfs);
67 static volatile int tmpfs_rename_restarts;
68 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
69 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
70 "Times rename had to restart due to lock contention");
72 /* --------------------------------------------------------------------- */
75 tmpfs_lookup(struct vop_cachedlookup_args *v)
77 struct vnode *dvp = v->a_dvp;
78 struct vnode **vpp = v->a_vpp;
79 struct componentname *cnp = v->a_cnp;
82 struct tmpfs_dirent *de;
83 struct tmpfs_node *dnode;
85 dnode = VP_TO_TMPFS_DIR(dvp);
88 /* Check accessibility of requested node as a first step. */
89 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
93 /* We cannot be requesting the parent directory of the root node. */
94 MPASS(IMPLIES(dnode->tn_type == VDIR &&
95 dnode->tn_dir.tn_parent == dnode,
96 !(cnp->cn_flags & ISDOTDOT)));
98 TMPFS_ASSERT_LOCKED(dnode);
99 if (dnode->tn_dir.tn_parent == NULL) {
103 if (cnp->cn_flags & ISDOTDOT) {
106 ltype = VOP_ISLOCKED(dvp);
109 /* Allocate a new vnode on the matching entry. */
110 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
111 cnp->cn_lkflags, vpp);
113 vn_lock(dvp, ltype | LK_RETRY);
115 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
120 de = tmpfs_dir_lookup(dnode, NULL, cnp);
121 if (de != NULL && de->td_node == NULL)
122 cnp->cn_flags |= ISWHITEOUT;
123 if (de == NULL || de->td_node == NULL) {
124 /* The entry was not found in the directory.
125 * This is OK if we are creating or renaming an
126 * entry and are working on the last component of
128 if ((cnp->cn_flags & ISLASTCN) &&
129 (cnp->cn_nameiop == CREATE || \
130 cnp->cn_nameiop == RENAME ||
131 (cnp->cn_nameiop == DELETE &&
132 cnp->cn_flags & DOWHITEOUT &&
133 cnp->cn_flags & ISWHITEOUT))) {
134 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
139 /* Keep the component name in the buffer for
141 cnp->cn_flags |= SAVENAME;
147 struct tmpfs_node *tnode;
149 /* The entry was found, so get its associated
153 /* If we are not at the last path component and
154 * found a non-directory or non-link entry (which
155 * may itself be pointing to a directory), raise
157 if ((tnode->tn_type != VDIR &&
158 tnode->tn_type != VLNK) &&
159 !(cnp->cn_flags & ISLASTCN)) {
164 /* If we are deleting or renaming the entry, keep
165 * track of its tmpfs_dirent so that it can be
166 * easily deleted later. */
167 if ((cnp->cn_flags & ISLASTCN) &&
168 (cnp->cn_nameiop == DELETE ||
169 cnp->cn_nameiop == RENAME)) {
170 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
175 /* Allocate a new vnode on the matching entry. */
176 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
177 cnp->cn_lkflags, vpp);
181 if ((dnode->tn_mode & S_ISTXT) &&
182 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
183 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
189 cnp->cn_flags |= SAVENAME;
191 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
192 cnp->cn_lkflags, vpp);
197 /* Store the result of this lookup in the cache. Avoid this if the
198 * request was for creation, as it does not improve timings on
200 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
201 cache_enter(dvp, *vpp, cnp);
204 /* If there were no errors, *vpp cannot be null and it must be
206 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
211 /* --------------------------------------------------------------------- */
214 tmpfs_create(struct vop_create_args *v)
216 struct vnode *dvp = v->a_dvp;
217 struct vnode **vpp = v->a_vpp;
218 struct componentname *cnp = v->a_cnp;
219 struct vattr *vap = v->a_vap;
221 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
223 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
225 /* --------------------------------------------------------------------- */
228 tmpfs_mknod(struct vop_mknod_args *v)
230 struct vnode *dvp = v->a_dvp;
231 struct vnode **vpp = v->a_vpp;
232 struct componentname *cnp = v->a_cnp;
233 struct vattr *vap = v->a_vap;
235 if (vap->va_type != VBLK && vap->va_type != VCHR &&
236 vap->va_type != VFIFO)
239 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
242 /* --------------------------------------------------------------------- */
245 tmpfs_open(struct vop_open_args *v)
247 struct vnode *vp = v->a_vp;
248 int mode = v->a_mode;
251 struct tmpfs_node *node;
253 MPASS(VOP_ISLOCKED(vp));
255 node = VP_TO_TMPFS_NODE(vp);
257 /* The file is still active but all its names have been removed
258 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
259 * it is about to die. */
260 if (node->tn_links < 1)
263 /* If the file is marked append-only, deny write requests. */
264 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
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 MPASS(VOP_ISLOCKED(vp));
284 /* Update node times. */
290 /* --------------------------------------------------------------------- */
293 tmpfs_access(struct vop_access_args *v)
295 struct vnode *vp = v->a_vp;
296 accmode_t accmode = v->a_accmode;
297 struct ucred *cred = v->a_cred;
300 struct tmpfs_node *node;
302 MPASS(VOP_ISLOCKED(vp));
304 node = VP_TO_TMPFS_NODE(vp);
306 switch (vp->v_type) {
312 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
332 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
337 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
338 node->tn_gid, accmode, cred, NULL);
341 MPASS(VOP_ISLOCKED(vp));
346 /* --------------------------------------------------------------------- */
349 tmpfs_getattr(struct vop_getattr_args *v)
351 struct vnode *vp = v->a_vp;
352 struct vattr *vap = v->a_vap;
354 struct tmpfs_node *node;
356 node = VP_TO_TMPFS_NODE(vp);
360 vap->va_type = vp->v_type;
361 vap->va_mode = node->tn_mode;
362 vap->va_nlink = node->tn_links;
363 vap->va_uid = node->tn_uid;
364 vap->va_gid = node->tn_gid;
365 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
366 vap->va_fileid = node->tn_id;
367 vap->va_size = node->tn_size;
368 vap->va_blocksize = PAGE_SIZE;
369 vap->va_atime = node->tn_atime;
370 vap->va_mtime = node->tn_mtime;
371 vap->va_ctime = node->tn_ctime;
372 vap->va_birthtime = node->tn_birthtime;
373 vap->va_gen = node->tn_gen;
374 vap->va_flags = node->tn_flags;
375 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
376 node->tn_rdev : NODEV;
377 vap->va_bytes = round_page(node->tn_size);
383 /* --------------------------------------------------------------------- */
385 /* XXX Should this operation be atomic? I think it should, but code in
386 * XXX other places (e.g., ufs) doesn't seem to be... */
388 tmpfs_setattr(struct vop_setattr_args *v)
390 struct vnode *vp = v->a_vp;
391 struct vattr *vap = v->a_vap;
392 struct ucred *cred = v->a_cred;
393 struct thread *td = curthread;
397 MPASS(VOP_ISLOCKED(vp));
401 /* Abort if any unsettable attribute is given. */
402 if (vap->va_type != VNON ||
403 vap->va_nlink != VNOVAL ||
404 vap->va_fsid != VNOVAL ||
405 vap->va_fileid != VNOVAL ||
406 vap->va_blocksize != VNOVAL ||
407 vap->va_gen != VNOVAL ||
408 vap->va_rdev != VNOVAL ||
409 vap->va_bytes != VNOVAL)
412 if (error == 0 && (vap->va_flags != VNOVAL))
413 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
415 if (error == 0 && (vap->va_size != VNOVAL))
416 error = tmpfs_chsize(vp, vap->va_size, cred, td);
418 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
419 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
421 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
422 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
424 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
425 vap->va_atime.tv_nsec != VNOVAL) ||
426 (vap->va_mtime.tv_sec != VNOVAL &&
427 vap->va_mtime.tv_nsec != VNOVAL) ||
428 (vap->va_birthtime.tv_sec != VNOVAL &&
429 vap->va_birthtime.tv_nsec != VNOVAL)))
430 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
431 &vap->va_birthtime, vap->va_vaflags, cred, td);
433 /* Update the node times. We give preference to the error codes
434 * generated by this function rather than the ones that may arise
435 * from tmpfs_update. */
438 MPASS(VOP_ISLOCKED(vp));
443 /* --------------------------------------------------------------------- */
445 tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
446 vm_offset_t offset, size_t tlen, struct uio *uio)
451 VM_OBJECT_LOCK(tobj);
452 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
453 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
454 if (m->valid != VM_PAGE_BITS_ALL) {
455 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
456 rv = vm_pager_get_pages(tobj, &m, 1, 0);
457 if (rv != VM_PAGER_OK) {
461 VM_OBJECT_UNLOCK(tobj);
465 vm_page_zero_invalid(m, TRUE);
467 VM_OBJECT_UNLOCK(tobj);
468 error = uiomove_fromphys(&m, offset, tlen, uio);
469 VM_OBJECT_LOCK(tobj);
471 vm_page_unwire(m, TRUE);
474 VM_OBJECT_UNLOCK(tobj);
480 tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
481 vm_offset_t offset, size_t tlen, void *buf)
492 uio.uio_resid = tlen;
493 uio.uio_rw = UIO_READ;
494 uio.uio_segflg = UIO_SYSSPACE;
495 uio.uio_td = curthread;
497 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
501 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
512 addr = uio->uio_offset;
513 idx = OFF_TO_IDX(addr);
514 offset = addr & PAGE_MASK;
515 tlen = MIN(PAGE_SIZE - offset, len);
517 if ((vobj == NULL) ||
518 (vobj->resident_page_count == 0 && vobj->cache == NULL))
521 VM_OBJECT_LOCK(vobj);
523 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
524 vm_page_is_valid(m, offset, tlen)) {
525 if ((m->oflags & VPO_BUSY) != 0) {
527 * Reference the page before unlocking and sleeping so
528 * that the page daemon is less likely to reclaim it.
530 vm_page_reference(m);
531 vm_page_sleep(m, "tmfsmr");
535 VM_OBJECT_UNLOCK(vobj);
536 error = uiomove_fromphys(&m, offset, tlen, uio);
537 VM_OBJECT_LOCK(vobj);
539 VM_OBJECT_UNLOCK(vobj);
541 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
543 ("unexpected offset in tmpfs_mappedread for sendfile"));
544 if ((m->oflags & VPO_BUSY) != 0) {
546 * Reference the page before unlocking and sleeping so
547 * that the page daemon is less likely to reclaim it.
549 vm_page_reference(m);
550 vm_page_sleep(m, "tmfsmr");
554 VM_OBJECT_UNLOCK(vobj);
556 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
557 ma = (char *)sf_buf_kva(sf);
558 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
560 if (tlen != PAGE_SIZE)
561 bzero(ma + tlen, PAGE_SIZE - tlen);
562 uio->uio_offset += tlen;
563 uio->uio_resid -= tlen;
567 VM_OBJECT_LOCK(vobj);
569 m->valid = VM_PAGE_BITS_ALL;
571 VM_OBJECT_UNLOCK(vobj);
574 VM_OBJECT_UNLOCK(vobj);
576 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
582 tmpfs_read(struct vop_read_args *v)
584 struct vnode *vp = v->a_vp;
585 struct uio *uio = v->a_uio;
587 struct tmpfs_node *node;
594 node = VP_TO_TMPFS_NODE(vp);
596 if (vp->v_type != VREG) {
601 if (uio->uio_offset < 0) {
606 node->tn_status |= TMPFS_NODE_ACCESSED;
608 uobj = node->tn_reg.tn_aobj;
609 while ((resid = uio->uio_resid) > 0) {
611 if (node->tn_size <= uio->uio_offset)
613 len = MIN(node->tn_size - uio->uio_offset, resid);
616 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
617 if ((error != 0) || (resid == uio->uio_resid))
626 /* --------------------------------------------------------------------- */
629 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
640 addr = uio->uio_offset;
641 idx = OFF_TO_IDX(addr);
642 offset = addr & PAGE_MASK;
643 tlen = MIN(PAGE_SIZE - offset, len);
645 if ((vobj == NULL) ||
646 (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
651 VM_OBJECT_LOCK(vobj);
653 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
654 vm_page_is_valid(vpg, offset, tlen)) {
655 if ((vpg->oflags & VPO_BUSY) != 0) {
657 * Reference the page before unlocking and sleeping so
658 * that the page daemon is less likely to reclaim it.
660 vm_page_reference(vpg);
661 vm_page_sleep(vpg, "tmfsmw");
665 vm_page_undirty(vpg);
666 VM_OBJECT_UNLOCK(vobj);
667 error = uiomove_fromphys(&vpg, offset, tlen, uio);
669 if (__predict_false(vobj->cache != NULL))
670 vm_page_cache_free(vobj, idx, idx + 1);
671 VM_OBJECT_UNLOCK(vobj);
675 VM_OBJECT_LOCK(tobj);
676 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
677 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
678 if (tpg->valid != VM_PAGE_BITS_ALL) {
679 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
680 rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
681 if (rv != VM_PAGER_OK) {
689 vm_page_zero_invalid(tpg, TRUE);
691 VM_OBJECT_UNLOCK(tobj);
693 error = uiomove_fromphys(&tpg, offset, tlen, uio);
695 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
696 pmap_copy_page(vpg, tpg);
698 VM_OBJECT_LOCK(tobj);
700 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
701 ("parts of tpg invalid"));
705 vm_page_unwire(tpg, TRUE);
709 VM_OBJECT_UNLOCK(tobj);
711 VM_OBJECT_LOCK(vobj);
713 VM_OBJECT_UNLOCK(vobj);
720 tmpfs_write(struct vop_write_args *v)
722 struct vnode *vp = v->a_vp;
723 struct uio *uio = v->a_uio;
724 int ioflag = v->a_ioflag;
729 struct tmpfs_node *node;
734 node = VP_TO_TMPFS_NODE(vp);
735 oldsize = node->tn_size;
737 if (uio->uio_offset < 0 || vp->v_type != VREG) {
742 if (uio->uio_resid == 0) {
747 if (ioflag & IO_APPEND)
748 uio->uio_offset = node->tn_size;
750 if (uio->uio_offset + uio->uio_resid >
751 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
754 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
757 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
759 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
765 uobj = node->tn_reg.tn_aobj;
766 while ((resid = uio->uio_resid) > 0) {
767 if (node->tn_size <= uio->uio_offset)
769 len = MIN(node->tn_size - uio->uio_offset, resid);
772 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
773 if ((error != 0) || (resid == uio->uio_resid))
777 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
778 (extended ? TMPFS_NODE_CHANGED : 0);
780 if (node->tn_mode & (S_ISUID | S_ISGID)) {
781 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
782 node->tn_mode &= ~(S_ISUID | S_ISGID);
786 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
789 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
790 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
795 /* --------------------------------------------------------------------- */
798 tmpfs_fsync(struct vop_fsync_args *v)
800 struct vnode *vp = v->a_vp;
802 MPASS(VOP_ISLOCKED(vp));
809 /* --------------------------------------------------------------------- */
812 tmpfs_remove(struct vop_remove_args *v)
814 struct vnode *dvp = v->a_dvp;
815 struct vnode *vp = v->a_vp;
818 struct tmpfs_dirent *de;
819 struct tmpfs_mount *tmp;
820 struct tmpfs_node *dnode;
821 struct tmpfs_node *node;
823 MPASS(VOP_ISLOCKED(dvp));
824 MPASS(VOP_ISLOCKED(vp));
826 if (vp->v_type == VDIR) {
831 dnode = VP_TO_TMPFS_DIR(dvp);
832 node = VP_TO_TMPFS_NODE(vp);
833 tmp = VFS_TO_TMPFS(vp->v_mount);
834 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
837 /* Files marked as immutable or append-only cannot be deleted. */
838 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
839 (dnode->tn_flags & APPEND)) {
844 /* Remove the entry from the directory; as it is a file, we do not
845 * have to change the number of hard links of the directory. */
846 tmpfs_dir_detach(dvp, de);
847 if (v->a_cnp->cn_flags & DOWHITEOUT)
848 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
850 /* Free the directory entry we just deleted. Note that the node
851 * referred by it will not be removed until the vnode is really
853 tmpfs_free_dirent(tmp, de, TRUE);
855 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
863 /* --------------------------------------------------------------------- */
866 tmpfs_link(struct vop_link_args *v)
868 struct vnode *dvp = v->a_tdvp;
869 struct vnode *vp = v->a_vp;
870 struct componentname *cnp = v->a_cnp;
873 struct tmpfs_dirent *de;
874 struct tmpfs_node *node;
876 MPASS(VOP_ISLOCKED(dvp));
877 MPASS(cnp->cn_flags & HASBUF);
878 MPASS(dvp != vp); /* XXX When can this be false? */
880 node = VP_TO_TMPFS_NODE(vp);
882 /* XXX: Why aren't the following two tests done by the caller? */
884 /* Hard links of directories are forbidden. */
885 if (vp->v_type == VDIR) {
890 /* Cannot create cross-device links. */
891 if (dvp->v_mount != vp->v_mount) {
896 /* Ensure that we do not overflow the maximum number of links imposed
898 MPASS(node->tn_links <= LINK_MAX);
899 if (node->tn_links == LINK_MAX) {
904 /* We cannot create links of files marked immutable or append-only. */
905 if (node->tn_flags & (IMMUTABLE | APPEND)) {
910 /* Allocate a new directory entry to represent the node. */
911 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
912 cnp->cn_nameptr, cnp->cn_namelen, &de);
916 /* Insert the new directory entry into the appropriate directory. */
917 if (cnp->cn_flags & ISWHITEOUT)
918 tmpfs_dir_whiteout_remove(dvp, cnp);
919 tmpfs_dir_attach(dvp, de);
921 /* vp link count has changed, so update node times. */
922 node->tn_status |= TMPFS_NODE_CHANGED;
931 /* --------------------------------------------------------------------- */
934 * We acquire all but fdvp locks using non-blocking acquisitions. If we
935 * fail to acquire any lock in the path we will drop all held locks,
936 * acquire the new lock in a blocking fashion, and then release it and
937 * restart the rename. This acquire/release step ensures that we do not
938 * spin on a lock waiting for release. On error release all vnode locks
939 * and decrement references the way tmpfs_rename() would do.
942 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
943 struct vnode *tdvp, struct vnode **tvpp,
944 struct componentname *fcnp, struct componentname *tcnp)
948 struct tmpfs_dirent *de;
949 int error, restarts = 0;
952 if (*tvpp != NULL && *tvpp != tdvp)
953 VOP_UNLOCK(*tvpp, 0);
958 error = vn_lock(fdvp, LK_EXCLUSIVE);
961 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
963 error = vn_lock(tdvp, LK_EXCLUSIVE);
970 * Re-resolve fvp to be certain it still exists and fetch the
973 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
977 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
978 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
984 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
990 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
995 * Concurrent rename race.
1008 VOP_UNLOCK(*fvpp, 0);
1010 * Re-resolve tvp and acquire the vnode lock if present.
1012 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
1014 * If tvp disappeared we just carry on.
1016 if (de == NULL && *tvpp != NULL) {
1021 * Get the tvp ino if the lookup succeeded. We may have to restart
1022 * if the non-blocking acquire fails.
1026 error = tmpfs_alloc_vp(mp, de->td_node,
1027 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1032 VOP_UNLOCK(fdvp, 0);
1033 VOP_UNLOCK(tdvp, 0);
1036 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
1042 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
1051 tmpfs_rename_restarts += restarts;
1061 tmpfs_rename_restarts += restarts;
1067 tmpfs_rename(struct vop_rename_args *v)
1069 struct vnode *fdvp = v->a_fdvp;
1070 struct vnode *fvp = v->a_fvp;
1071 struct componentname *fcnp = v->a_fcnp;
1072 struct vnode *tdvp = v->a_tdvp;
1073 struct vnode *tvp = v->a_tvp;
1074 struct componentname *tcnp = v->a_tcnp;
1075 struct mount *mp = NULL;
1079 struct tmpfs_dirent *de;
1080 struct tmpfs_mount *tmp;
1081 struct tmpfs_node *fdnode;
1082 struct tmpfs_node *fnode;
1083 struct tmpfs_node *tnode;
1084 struct tmpfs_node *tdnode;
1086 MPASS(VOP_ISLOCKED(tdvp));
1087 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
1088 MPASS(fcnp->cn_flags & HASBUF);
1089 MPASS(tcnp->cn_flags & HASBUF);
1091 /* Disallow cross-device renames.
1092 * XXX Why isn't this done by the caller? */
1093 if (fvp->v_mount != tdvp->v_mount ||
1094 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1099 /* If source and target are the same file, there is nothing to do. */
1105 /* If we need to move the directory between entries, lock the
1106 * source so that we can safely operate on it. */
1107 if (fdvp != tdvp && fdvp != tvp) {
1108 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1110 error = vfs_busy(mp, 0);
1115 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1121 ASSERT_VOP_ELOCKED(fdvp,
1122 "tmpfs_rename: fdvp not locked");
1123 ASSERT_VOP_ELOCKED(tdvp,
1124 "tmpfs_rename: tdvp not locked");
1126 ASSERT_VOP_ELOCKED(tvp,
1127 "tmpfs_rename: tvp not locked");
1135 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1136 tdnode = VP_TO_TMPFS_DIR(tdvp);
1137 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1138 fdnode = VP_TO_TMPFS_DIR(fdvp);
1139 fnode = VP_TO_TMPFS_NODE(fvp);
1140 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1142 /* Entry can disappear before we lock fdvp,
1143 * also avoid manipulating '.' and '..' entries. */
1145 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1146 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1152 MPASS(de->td_node == fnode);
1154 /* If re-naming a directory to another preexisting directory
1155 * ensure that the target directory is empty so that its
1156 * removal causes no side effects.
1157 * Kern_rename gurantees the destination to be a directory
1158 * if the source is one. */
1160 MPASS(tnode != NULL);
1162 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1163 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1168 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1169 if (tnode->tn_size > 0) {
1173 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1176 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1180 MPASS(fnode->tn_type != VDIR &&
1181 tnode->tn_type != VDIR);
1185 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1186 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1191 /* Ensure that we have enough memory to hold the new name, if it
1192 * has to be changed. */
1193 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1194 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1195 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1199 /* If the node is being moved to another directory, we have to do
1201 if (fdnode != tdnode) {
1202 /* In case we are moving a directory, we have to adjust its
1203 * parent to point to the new parent. */
1204 if (de->td_node->tn_type == VDIR) {
1205 struct tmpfs_node *n;
1207 /* Ensure the target directory is not a child of the
1208 * directory being moved. Otherwise, we'd end up
1209 * with stale nodes. */
1211 /* TMPFS_LOCK garanties that no nodes are freed while
1212 * traversing the list. Nodes can only be marked as
1213 * removed: tn_parent == NULL. */
1216 while (n != n->tn_dir.tn_parent) {
1217 struct tmpfs_node *parent;
1220 TMPFS_NODE_UNLOCK(n);
1223 if (newname != NULL)
1224 free(newname, M_TMPFSNAME);
1227 parent = n->tn_dir.tn_parent;
1228 TMPFS_NODE_UNLOCK(n);
1229 if (parent == NULL) {
1233 TMPFS_NODE_LOCK(parent);
1234 if (parent->tn_dir.tn_parent == NULL) {
1235 TMPFS_NODE_UNLOCK(parent);
1244 if (newname != NULL)
1245 free(newname, M_TMPFSNAME);
1248 TMPFS_NODE_UNLOCK(n);
1250 /* Adjust the parent pointer. */
1251 TMPFS_VALIDATE_DIR(fnode);
1252 TMPFS_NODE_LOCK(de->td_node);
1253 de->td_node->tn_dir.tn_parent = tdnode;
1254 TMPFS_NODE_UNLOCK(de->td_node);
1256 /* As a result of changing the target of the '..'
1257 * entry, the link count of the source and target
1258 * directories has to be adjusted. */
1259 TMPFS_NODE_LOCK(tdnode);
1260 TMPFS_ASSERT_LOCKED(tdnode);
1262 TMPFS_NODE_UNLOCK(tdnode);
1264 TMPFS_NODE_LOCK(fdnode);
1265 TMPFS_ASSERT_LOCKED(fdnode);
1267 TMPFS_NODE_UNLOCK(fdnode);
1270 /* Do the move: just remove the entry from the source directory
1271 * and insert it into the target one. */
1272 tmpfs_dir_detach(fdvp, de);
1273 if (fcnp->cn_flags & DOWHITEOUT)
1274 tmpfs_dir_whiteout_add(fdvp, fcnp);
1275 if (tcnp->cn_flags & ISWHITEOUT)
1276 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1277 tmpfs_dir_attach(tdvp, de);
1280 /* If the name has changed, we need to make it effective by changing
1281 * it in the directory entry. */
1282 if (newname != NULL) {
1283 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1285 free(de->td_name, M_TMPFSNAME);
1286 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1287 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1288 de->td_name = newname;
1290 fnode->tn_status |= TMPFS_NODE_CHANGED;
1291 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1294 /* If we are overwriting an entry, we have to remove the old one
1295 * from the target directory. */
1297 /* Remove the old entry from the target directory. */
1298 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1299 tmpfs_dir_detach(tdvp, de);
1301 /* Free the directory entry we just deleted. Note that the
1302 * node referred by it will not be removed until the vnode is
1303 * really reclaimed. */
1304 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1309 cache_purge_negative(tdvp);
1314 if (fdvp != tdvp && fdvp != tvp)
1315 VOP_UNLOCK(fdvp, 0);
1318 /* Release target nodes. */
1319 /* XXX: I don't understand when tdvp can be the same as tvp, but
1320 * other code takes care of this... */
1328 /* Release source nodes. */
1338 /* --------------------------------------------------------------------- */
1341 tmpfs_mkdir(struct vop_mkdir_args *v)
1343 struct vnode *dvp = v->a_dvp;
1344 struct vnode **vpp = v->a_vpp;
1345 struct componentname *cnp = v->a_cnp;
1346 struct vattr *vap = v->a_vap;
1348 MPASS(vap->va_type == VDIR);
1350 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1353 /* --------------------------------------------------------------------- */
1356 tmpfs_rmdir(struct vop_rmdir_args *v)
1358 struct vnode *dvp = v->a_dvp;
1359 struct vnode *vp = v->a_vp;
1362 struct tmpfs_dirent *de;
1363 struct tmpfs_mount *tmp;
1364 struct tmpfs_node *dnode;
1365 struct tmpfs_node *node;
1367 MPASS(VOP_ISLOCKED(dvp));
1368 MPASS(VOP_ISLOCKED(vp));
1370 tmp = VFS_TO_TMPFS(dvp->v_mount);
1371 dnode = VP_TO_TMPFS_DIR(dvp);
1372 node = VP_TO_TMPFS_DIR(vp);
1374 /* Directories with more than two entries ('.' and '..') cannot be
1376 if (node->tn_size > 0) {
1381 if ((dnode->tn_flags & APPEND)
1382 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1387 /* This invariant holds only if we are not trying to remove "..".
1388 * We checked for that above so this is safe now. */
1389 MPASS(node->tn_dir.tn_parent == dnode);
1391 /* Get the directory entry associated with node (vp). This was
1392 * filled by tmpfs_lookup while looking up the entry. */
1393 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1394 MPASS(TMPFS_DIRENT_MATCHES(de,
1395 v->a_cnp->cn_nameptr,
1396 v->a_cnp->cn_namelen));
1398 /* Check flags to see if we are allowed to remove the directory. */
1399 if (dnode->tn_flags & APPEND
1400 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1406 /* Detach the directory entry from the directory (dnode). */
1407 tmpfs_dir_detach(dvp, de);
1408 if (v->a_cnp->cn_flags & DOWHITEOUT)
1409 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1411 /* No vnode should be allocated for this entry from this point */
1412 TMPFS_NODE_LOCK(node);
1413 TMPFS_ASSERT_ELOCKED(node);
1415 node->tn_dir.tn_parent = NULL;
1416 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1417 TMPFS_NODE_MODIFIED;
1419 TMPFS_NODE_UNLOCK(node);
1421 TMPFS_NODE_LOCK(dnode);
1422 TMPFS_ASSERT_ELOCKED(dnode);
1424 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1425 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1426 TMPFS_NODE_UNLOCK(dnode);
1431 /* Free the directory entry we just deleted. Note that the node
1432 * referred by it will not be removed until the vnode is really
1434 tmpfs_free_dirent(tmp, de, TRUE);
1436 /* Release the deleted vnode (will destroy the node, notify
1437 * interested parties and clean it from the cache). */
1439 dnode->tn_status |= TMPFS_NODE_CHANGED;
1448 /* --------------------------------------------------------------------- */
1451 tmpfs_symlink(struct vop_symlink_args *v)
1453 struct vnode *dvp = v->a_dvp;
1454 struct vnode **vpp = v->a_vpp;
1455 struct componentname *cnp = v->a_cnp;
1456 struct vattr *vap = v->a_vap;
1457 char *target = v->a_target;
1459 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1460 MPASS(vap->va_type == VLNK);
1462 vap->va_type = VLNK;
1465 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1468 /* --------------------------------------------------------------------- */
1471 tmpfs_readdir(struct vop_readdir_args *v)
1473 struct vnode *vp = v->a_vp;
1474 struct uio *uio = v->a_uio;
1475 int *eofflag = v->a_eofflag;
1476 u_long **cookies = v->a_cookies;
1477 int *ncookies = v->a_ncookies;
1482 struct tmpfs_node *node;
1484 /* This operation only makes sense on directory nodes. */
1485 if (vp->v_type != VDIR)
1488 node = VP_TO_TMPFS_DIR(vp);
1490 startoff = uio->uio_offset;
1492 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1493 error = tmpfs_dir_getdotdent(node, uio);
1499 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1500 error = tmpfs_dir_getdotdotdent(node, uio);
1506 error = tmpfs_dir_getdents(node, uio, &cnt);
1512 error = (cnt != 0) ? 0 : EINVAL;
1514 if (eofflag != NULL)
1516 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1518 /* Update NFS-related variables. */
1519 if (error == 0 && cookies != NULL && ncookies != NULL) {
1521 off_t off = startoff;
1522 struct tmpfs_dirent *de = NULL;
1525 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1527 for (i = 0; i < cnt; i++) {
1528 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1529 if (off == TMPFS_DIRCOOKIE_DOT) {
1530 off = TMPFS_DIRCOOKIE_DOTDOT;
1532 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1533 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1534 } else if (de != NULL) {
1535 de = TAILQ_NEXT(de, td_entries);
1537 de = tmpfs_dir_lookupbycookie(node,
1540 de = TAILQ_NEXT(de, td_entries);
1543 off = TMPFS_DIRCOOKIE_EOF;
1545 off = tmpfs_dircookie(de);
1548 (*cookies)[i] = off;
1550 MPASS(uio->uio_offset == off);
1556 /* --------------------------------------------------------------------- */
1559 tmpfs_readlink(struct vop_readlink_args *v)
1561 struct vnode *vp = v->a_vp;
1562 struct uio *uio = v->a_uio;
1565 struct tmpfs_node *node;
1567 MPASS(uio->uio_offset == 0);
1568 MPASS(vp->v_type == VLNK);
1570 node = VP_TO_TMPFS_NODE(vp);
1572 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1574 node->tn_status |= TMPFS_NODE_ACCESSED;
1579 /* --------------------------------------------------------------------- */
1582 tmpfs_inactive(struct vop_inactive_args *v)
1584 struct vnode *vp = v->a_vp;
1585 struct thread *l = v->a_td;
1587 struct tmpfs_node *node;
1589 MPASS(VOP_ISLOCKED(vp));
1591 node = VP_TO_TMPFS_NODE(vp);
1593 if (node->tn_links == 0)
1599 /* --------------------------------------------------------------------- */
1602 tmpfs_reclaim(struct vop_reclaim_args *v)
1604 struct vnode *vp = v->a_vp;
1606 struct tmpfs_mount *tmp;
1607 struct tmpfs_node *node;
1609 node = VP_TO_TMPFS_NODE(vp);
1610 tmp = VFS_TO_TMPFS(vp->v_mount);
1612 vnode_destroy_vobject(vp);
1615 TMPFS_NODE_LOCK(node);
1616 TMPFS_ASSERT_ELOCKED(node);
1619 /* If the node referenced by this vnode was deleted by the user,
1620 * we must free its associated data structures (now that the vnode
1621 * is being reclaimed). */
1622 if (node->tn_links == 0 &&
1623 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1624 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1625 TMPFS_NODE_UNLOCK(node);
1626 tmpfs_free_node(tmp, node);
1628 TMPFS_NODE_UNLOCK(node);
1630 MPASS(vp->v_data == NULL);
1634 /* --------------------------------------------------------------------- */
1637 tmpfs_print(struct vop_print_args *v)
1639 struct vnode *vp = v->a_vp;
1641 struct tmpfs_node *node;
1643 node = VP_TO_TMPFS_NODE(vp);
1645 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1646 node, node->tn_flags, node->tn_links);
1647 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1649 node->tn_mode, node->tn_uid, node->tn_gid,
1650 (uintmax_t)node->tn_size, node->tn_status);
1652 if (vp->v_type == VFIFO)
1660 /* --------------------------------------------------------------------- */
1663 tmpfs_pathconf(struct vop_pathconf_args *v)
1665 int name = v->a_name;
1666 register_t *retval = v->a_retval;
1689 case _PC_CHOWN_RESTRICTED:
1701 case _PC_FILESIZEBITS:
1702 *retval = 0; /* XXX Don't know which value should I return. */
1713 tmpfs_vptofh(struct vop_vptofh_args *ap)
1715 struct tmpfs_fid *tfhp;
1716 struct tmpfs_node *node;
1718 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1719 node = VP_TO_TMPFS_NODE(ap->a_vp);
1721 tfhp->tf_len = sizeof(struct tmpfs_fid);
1722 tfhp->tf_id = node->tn_id;
1723 tfhp->tf_gen = node->tn_gen;
1729 tmpfs_whiteout(struct vop_whiteout_args *ap)
1731 struct vnode *dvp = ap->a_dvp;
1732 struct componentname *cnp = ap->a_cnp;
1733 struct tmpfs_dirent *de;
1735 switch (ap->a_flags) {
1739 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1741 return (de->td_node == NULL ? 0 : EEXIST);
1742 return (tmpfs_dir_whiteout_add(dvp, cnp));
1744 tmpfs_dir_whiteout_remove(dvp, cnp);
1747 panic("tmpfs_whiteout: unknown op");
1751 /* --------------------------------------------------------------------- */
1754 * vnode operations vector used for files stored in a tmpfs file system.
1756 struct vop_vector tmpfs_vnodeop_entries = {
1757 .vop_default = &default_vnodeops,
1758 .vop_lookup = vfs_cache_lookup,
1759 .vop_cachedlookup = tmpfs_lookup,
1760 .vop_create = tmpfs_create,
1761 .vop_mknod = tmpfs_mknod,
1762 .vop_open = tmpfs_open,
1763 .vop_close = tmpfs_close,
1764 .vop_access = tmpfs_access,
1765 .vop_getattr = tmpfs_getattr,
1766 .vop_setattr = tmpfs_setattr,
1767 .vop_read = tmpfs_read,
1768 .vop_write = tmpfs_write,
1769 .vop_fsync = tmpfs_fsync,
1770 .vop_remove = tmpfs_remove,
1771 .vop_link = tmpfs_link,
1772 .vop_rename = tmpfs_rename,
1773 .vop_mkdir = tmpfs_mkdir,
1774 .vop_rmdir = tmpfs_rmdir,
1775 .vop_symlink = tmpfs_symlink,
1776 .vop_readdir = tmpfs_readdir,
1777 .vop_readlink = tmpfs_readlink,
1778 .vop_inactive = tmpfs_inactive,
1779 .vop_reclaim = tmpfs_reclaim,
1780 .vop_print = tmpfs_print,
1781 .vop_pathconf = tmpfs_pathconf,
1782 .vop_vptofh = tmpfs_vptofh,
1783 .vop_whiteout = tmpfs_whiteout,
1784 .vop_bmap = VOP_EOPNOTSUPP,