1 /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */
4 * Copyright (c) 2005 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 * Efficient memory file system supporting functions.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/fnv_hash.h>
42 #include <sys/namei.h>
45 #include <sys/rwlock.h>
47 #include <sys/systm.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/vmmeter.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pageout.h>
57 #include <vm/vm_pager.h>
58 #include <vm/vm_extern.h>
60 #include <fs/tmpfs/tmpfs.h>
61 #include <fs/tmpfs/tmpfs_fifoops.h>
62 #include <fs/tmpfs/tmpfs_vnops.h>
64 struct tmpfs_dir_cursor {
65 struct tmpfs_dirent *tdc_current;
66 struct tmpfs_dirent *tdc_tree;
69 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "tmpfs file system");
71 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
74 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
79 pages = *(long *)arg1;
80 bytes = pages * PAGE_SIZE;
82 error = sysctl_handle_long(oidp, &bytes, 0, req);
83 if (error || !req->newptr)
86 pages = bytes / PAGE_SIZE;
87 if (pages < TMPFS_PAGES_MINRESERVED)
90 *(long *)arg1 = pages;
94 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved, CTLTYPE_LONG|CTLFLAG_RW,
95 &tmpfs_pages_reserved, 0, sysctl_mem_reserved, "L",
96 "Amount of available memory and swap below which tmpfs growth stops");
98 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
99 struct tmpfs_dirent *b);
100 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
103 tmpfs_mem_avail(void)
107 avail = swap_pager_avail + cnt.v_free_count + cnt.v_cache_count -
108 tmpfs_pages_reserved;
109 if (__predict_false(avail < 0))
115 tmpfs_pages_used(struct tmpfs_mount *tmp)
117 const size_t node_size = sizeof(struct tmpfs_node) +
118 sizeof(struct tmpfs_dirent);
121 meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
123 return (meta_pages + tmp->tm_pages_used);
127 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
129 if (tmpfs_mem_avail() < req_pages)
132 if (tmp->tm_pages_max != SIZE_MAX &&
133 tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
140 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
141 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
142 * using the credentials of the process 'p'.
144 * If the node type is set to 'VDIR', then the parent parameter must point
145 * to the parent directory of the node being created. It may only be NULL
146 * while allocating the root node.
148 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
149 * specifies the device the node represents.
151 * If the node type is set to 'VLNK', then the parameter target specifies
152 * the file name of the target file for the symbolic link that is being
155 * Note that new nodes are retrieved from the available list if it has
156 * items or, if it is empty, from the node pool as long as there is enough
157 * space to create them.
159 * Returns zero on success or an appropriate error code on failure.
162 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
163 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
164 char *target, dev_t rdev, struct tmpfs_node **node)
166 struct tmpfs_node *nnode;
169 /* If the root directory of the 'tmp' file system is not yet
170 * allocated, this must be the request to do it. */
171 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
173 MPASS(IFF(type == VLNK, target != NULL));
174 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
176 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
178 if (tmpfs_pages_check_avail(tmp, 1) == 0)
181 nnode = (struct tmpfs_node *)uma_zalloc_arg(
182 tmp->tm_node_pool, tmp, M_WAITOK);
184 /* Generic initialization. */
185 nnode->tn_type = type;
186 vfs_timestamp(&nnode->tn_atime);
187 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
191 nnode->tn_mode = mode;
192 nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
194 /* Type-specific initialization. */
195 switch (nnode->tn_type) {
198 nnode->tn_rdev = rdev;
202 RB_INIT(&nnode->tn_dir.tn_dirhead);
203 LIST_INIT(&nnode->tn_dir.tn_dupindex);
204 MPASS(parent != nnode);
205 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
206 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
207 nnode->tn_dir.tn_readdir_lastn = 0;
208 nnode->tn_dir.tn_readdir_lastp = NULL;
210 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
211 nnode->tn_dir.tn_parent->tn_links++;
212 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
221 MPASS(strlen(target) < MAXPATHLEN);
222 nnode->tn_size = strlen(target);
223 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
225 memcpy(nnode->tn_link, target, nnode->tn_size);
229 obj = nnode->tn_reg.tn_aobj =
230 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
231 NULL /* XXXKIB - tmpfs needs swap reservation */);
232 VM_OBJECT_WLOCK(obj);
233 /* OBJ_TMPFS is set together with the setting of vp->v_object */
234 vm_object_set_flag(obj, OBJ_NOSPLIT);
235 vm_object_clear_flag(obj, OBJ_ONEMAPPING);
236 VM_OBJECT_WUNLOCK(obj);
240 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
244 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
245 tmp->tm_nodes_inuse++;
253 * Destroys the node pointed to by node from the file system 'tmp'.
254 * If the node does not belong to the given mount point, the results are
257 * If the node references a directory; no entries are allowed because
258 * their removal could need a recursive algorithm, something forbidden in
259 * kernel space. Furthermore, there is not need to provide such
260 * functionality (recursive removal) because the only primitives offered
261 * to the user are the removal of empty directories and the deletion of
264 * Note that nodes are not really deleted; in fact, when a node has been
265 * allocated, it cannot be deleted during the whole life of the file
266 * system. Instead, they are moved to the available list and remain there
270 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
275 TMPFS_NODE_LOCK(node);
276 MPASS(node->tn_vnode == NULL);
277 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
278 TMPFS_NODE_UNLOCK(node);
282 LIST_REMOVE(node, tn_entries);
283 tmp->tm_nodes_inuse--;
286 switch (node->tn_type) {
288 /* Do not do anything. VNON is provided to let the
289 * allocation routine clean itself easily by avoiding
290 * duplicating code in it. */
304 free(node->tn_link, M_TMPFSNAME);
308 uobj = node->tn_reg.tn_aobj;
311 tmp->tm_pages_used -= uobj->size;
313 KASSERT((uobj->flags & OBJ_TMPFS) == 0,
314 ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
315 vm_object_deallocate(uobj);
320 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
323 free_unr(tmp->tm_ino_unr, node->tn_id);
324 uma_zfree(tmp->tm_node_pool, node);
327 static __inline uint32_t
328 tmpfs_dirent_hash(const char *name, u_int len)
332 hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
333 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
336 if (hash < TMPFS_DIRCOOKIE_MIN)
337 hash += TMPFS_DIRCOOKIE_MIN;
342 static __inline off_t
343 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
346 return (TMPFS_DIRCOOKIE_EOF);
348 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
350 return (de->td_cookie);
353 static __inline boolean_t
354 tmpfs_dirent_dup(struct tmpfs_dirent *de)
356 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
359 static __inline boolean_t
360 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
362 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
366 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
368 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
369 memcpy(de->ud.td_name, name, namelen);
370 de->td_namelen = namelen;
374 * Allocates a new directory entry for the node node with a name of name.
375 * The new directory entry is returned in *de.
377 * The link count of node is increased by one to reflect the new object
380 * Returns zero on success or an appropriate error code on failure.
383 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
384 const char *name, u_int len, struct tmpfs_dirent **de)
386 struct tmpfs_dirent *nde;
388 nde = uma_zalloc(tmp->tm_dirent_pool, M_WAITOK);
391 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
392 tmpfs_dirent_init(nde, name, len);
404 * Frees a directory entry. It is the caller's responsibility to destroy
405 * the node referenced by it if needed.
407 * The link count of node is decreased by one to reflect the removal of an
408 * object that referenced it. This only happens if 'node_exists' is true;
409 * otherwise the function will not access the node referred to by the
410 * directory entry, as it may already have been released from the outside.
413 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
415 struct tmpfs_node *node;
419 MPASS(node->tn_links > 0);
422 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
423 free(de->ud.td_name, M_TMPFSNAME);
424 uma_zfree(tmp->tm_dirent_pool, de);
428 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
431 ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
432 if (vp->v_type != VREG || obj == NULL)
435 VM_OBJECT_WLOCK(obj);
437 vm_object_clear_flag(obj, OBJ_TMPFS);
438 obj->un_pager.swp.swp_tmpfs = NULL;
440 VM_OBJECT_WUNLOCK(obj);
444 * Need to clear v_object for insmntque failure.
447 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
450 tmpfs_destroy_vobject(vp, vp->v_object);
453 vp->v_op = &dead_vnodeops;
459 * Allocates a new vnode for the node node or returns a new reference to
460 * an existing one if the node had already a vnode referencing it. The
461 * resulting locked vnode is returned in *vpp.
463 * Returns zero on success or an appropriate error code on failure.
466 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
475 TMPFS_NODE_LOCK(node);
477 if ((vp = node->tn_vnode) != NULL) {
478 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
480 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
481 ((vp->v_iflag & VI_DOOMED) != 0 &&
482 (lkflag & LK_NOWAIT) != 0)) {
484 TMPFS_NODE_UNLOCK(node);
489 if ((vp->v_iflag & VI_DOOMED) != 0) {
491 node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
492 while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
493 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
498 TMPFS_NODE_UNLOCK(node);
499 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
508 * Make sure the vnode is still there after
509 * getting the interlock to avoid racing a free.
511 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
519 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
520 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
521 TMPFS_NODE_UNLOCK(node);
528 * otherwise lock the vp list while we call getnewvnode
529 * since that can block.
531 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
532 node->tn_vpstate |= TMPFS_VNODE_WANT;
533 error = msleep((caddr_t) &node->tn_vpstate,
534 TMPFS_NODE_MTX(node), PDROP | PCATCH,
535 "tmpfs_alloc_vp", 0);
541 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
543 TMPFS_NODE_UNLOCK(node);
545 /* Get a new vnode and associate it with our node. */
546 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
551 (void) vn_lock(vp, lkflag | LK_RETRY);
554 vp->v_type = node->tn_type;
556 /* Type-specific initialization. */
557 switch (node->tn_type) {
567 vp->v_op = &tmpfs_fifoop_entries;
570 object = node->tn_reg.tn_aobj;
571 VM_OBJECT_WLOCK(object);
573 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
574 vp->v_object = object;
575 object->un_pager.swp.swp_tmpfs = vp;
576 vm_object_set_flag(object, OBJ_TMPFS);
578 VM_OBJECT_WUNLOCK(object);
581 MPASS(node->tn_dir.tn_parent != NULL);
582 if (node->tn_dir.tn_parent == node)
583 vp->v_vflag |= VV_ROOT;
587 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
589 if (vp->v_type != VFIFO)
592 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
597 TMPFS_NODE_LOCK(node);
599 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
600 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
603 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
604 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
605 TMPFS_NODE_UNLOCK(node);
606 wakeup((caddr_t) &node->tn_vpstate);
608 TMPFS_NODE_UNLOCK(node);
615 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
616 TMPFS_NODE_LOCK(node);
617 MPASS(*vpp == node->tn_vnode);
618 TMPFS_NODE_UNLOCK(node);
626 * Destroys the association between the vnode vp and the node it
630 tmpfs_free_vp(struct vnode *vp)
632 struct tmpfs_node *node;
634 node = VP_TO_TMPFS_NODE(vp);
636 TMPFS_NODE_ASSERT_LOCKED(node);
637 node->tn_vnode = NULL;
638 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
639 wakeup(&node->tn_vnode);
640 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
645 * Allocates a new file of type 'type' and adds it to the parent directory
646 * 'dvp'; this addition is done using the component name given in 'cnp'.
647 * The ownership of the new file is automatically assigned based on the
648 * credentials of the caller (through 'cnp'), the group is set based on
649 * the parent directory and the mode is determined from the 'vap' argument.
650 * If successful, *vpp holds a vnode to the newly created file and zero
651 * is returned. Otherwise *vpp is NULL and the function returns an
652 * appropriate error code.
655 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
656 struct componentname *cnp, char *target)
659 struct tmpfs_dirent *de;
660 struct tmpfs_mount *tmp;
661 struct tmpfs_node *dnode;
662 struct tmpfs_node *node;
663 struct tmpfs_node *parent;
665 MPASS(VOP_ISLOCKED(dvp));
666 MPASS(cnp->cn_flags & HASBUF);
668 tmp = VFS_TO_TMPFS(dvp->v_mount);
669 dnode = VP_TO_TMPFS_DIR(dvp);
672 /* If the entry we are creating is a directory, we cannot overflow
673 * the number of links of its parent, because it will get a new
675 if (vap->va_type == VDIR) {
676 /* Ensure that we do not overflow the maximum number of links
677 * imposed by the system. */
678 MPASS(dnode->tn_links <= LINK_MAX);
679 if (dnode->tn_links == LINK_MAX) {
685 MPASS(parent != NULL);
689 /* Allocate a node that represents the new file. */
690 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
691 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
695 /* Allocate a directory entry that points to the new file. */
696 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
699 tmpfs_free_node(tmp, node);
703 /* Allocate a vnode for the new file. */
704 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
706 tmpfs_free_dirent(tmp, de);
707 tmpfs_free_node(tmp, node);
711 /* Now that all required items are allocated, we can proceed to
712 * insert the new node into the directory, an operation that
714 if (cnp->cn_flags & ISWHITEOUT)
715 tmpfs_dir_whiteout_remove(dvp, cnp);
716 tmpfs_dir_attach(dvp, de);
723 static struct tmpfs_dirent *
724 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
726 struct tmpfs_dirent *de;
728 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
730 if (de != NULL && tmpfs_dirent_duphead(de))
731 de = LIST_FIRST(&de->ud.td_duphead);
732 dc->tdc_current = de;
734 return (dc->tdc_current);
737 static struct tmpfs_dirent *
738 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
740 struct tmpfs_dirent *de;
742 MPASS(dc->tdc_tree != NULL);
743 if (tmpfs_dirent_dup(dc->tdc_current)) {
744 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
745 if (dc->tdc_current != NULL)
746 return (dc->tdc_current);
748 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
749 &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
750 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
751 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
752 MPASS(dc->tdc_current != NULL);
755 return (dc->tdc_current);
758 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
759 static struct tmpfs_dirent *
760 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
762 struct tmpfs_dirent *de, dekey;
764 dekey.td_hash = hash;
765 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
769 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
770 static struct tmpfs_dirent *
771 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
772 struct tmpfs_dir_cursor *dc)
774 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
775 struct tmpfs_dirent *de, dekey;
777 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
779 if (cookie == node->tn_dir.tn_readdir_lastn &&
780 (de = node->tn_dir.tn_readdir_lastp) != NULL) {
781 /* Protect against possible race, tn_readdir_last[pn]
782 * may be updated with only shared vnode lock held. */
783 if (cookie == tmpfs_dirent_cookie(de))
787 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
788 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
789 uh.td_dup.index_entries) {
790 MPASS(tmpfs_dirent_dup(de));
791 if (de->td_cookie == cookie)
793 /* dupindex list is sorted. */
794 if (de->td_cookie < cookie) {
803 MPASS((cookie & TMPFS_DIRCOOKIE_MASK) == cookie);
804 dekey.td_hash = cookie;
805 /* Recover if direntry for cookie was removed */
806 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
808 dc->tdc_current = de;
809 if (de != NULL && tmpfs_dirent_duphead(de)) {
810 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
811 MPASS(dc->tdc_current != NULL);
813 return (dc->tdc_current);
817 dc->tdc_current = de;
818 if (de != NULL && tmpfs_dirent_dup(de))
819 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
821 return (dc->tdc_current);
825 * Looks for a directory entry in the directory represented by node.
826 * 'cnp' describes the name of the entry to look for. Note that the .
827 * and .. components are not allowed as they do not physically exist
828 * within directories.
830 * Returns a pointer to the entry when found, otherwise NULL.
832 struct tmpfs_dirent *
833 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
834 struct componentname *cnp)
836 struct tmpfs_dir_duphead *duphead;
837 struct tmpfs_dirent *de;
840 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
841 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
842 cnp->cn_nameptr[1] == '.')));
843 TMPFS_VALIDATE_DIR(node);
845 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
846 de = tmpfs_dir_xlookup_hash(node, hash);
847 if (de != NULL && tmpfs_dirent_duphead(de)) {
848 duphead = &de->ud.td_duphead;
849 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
850 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
854 } else if (de != NULL) {
855 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
859 if (de != NULL && f != NULL && de->td_node != f)
866 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
867 * list, allocate new cookie value.
870 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
871 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
873 struct tmpfs_dir_duphead *dupindex;
874 struct tmpfs_dirent *de, *pde;
876 dupindex = &dnode->tn_dir.tn_dupindex;
877 de = LIST_FIRST(dupindex);
878 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
880 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
882 nde->td_cookie = de->td_cookie + 1;
883 MPASS(tmpfs_dirent_dup(nde));
884 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
885 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
890 * Cookie numbers are near exhaustion. Scan dupindex list for unused
891 * numbers. dupindex list is sorted in descending order. Keep it so
892 * after inserting nde.
896 de = LIST_NEXT(de, uh.td_dup.index_entries);
897 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
899 * Last element of the index doesn't have minimal cookie
902 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
903 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
904 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
906 } else if (de == NULL) {
908 * We are so lucky have 2^30 hash duplicates in single
909 * directory :) Return largest possible cookie value.
910 * It should be fine except possible issues with
911 * VOP_READDIR restart.
913 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
914 LIST_INSERT_HEAD(dupindex, nde,
915 uh.td_dup.index_entries);
916 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
919 if (de->td_cookie + 1 == pde->td_cookie ||
920 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
921 continue; /* No hole or invalid cookie. */
922 nde->td_cookie = de->td_cookie + 1;
923 MPASS(tmpfs_dirent_dup(nde));
924 MPASS(pde->td_cookie > nde->td_cookie);
925 MPASS(nde->td_cookie > de->td_cookie);
926 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
927 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
933 * Attaches the directory entry de to the directory represented by vp.
934 * Note that this does not change the link count of the node pointed by
935 * the directory entry, as this is done by tmpfs_alloc_dirent.
938 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
940 struct tmpfs_node *dnode;
941 struct tmpfs_dirent *xde, *nde;
943 ASSERT_VOP_ELOCKED(vp, __func__);
944 MPASS(de->td_namelen > 0);
945 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
946 MPASS(de->td_cookie == de->td_hash);
948 dnode = VP_TO_TMPFS_DIR(vp);
949 dnode->tn_dir.tn_readdir_lastn = 0;
950 dnode->tn_dir.tn_readdir_lastp = NULL;
952 MPASS(!tmpfs_dirent_dup(de));
953 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
954 if (xde != NULL && tmpfs_dirent_duphead(xde))
955 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
956 else if (xde != NULL) {
958 * Allocate new duphead. Swap xde with duphead to avoid
959 * adding/removing elements with the same hash.
961 MPASS(!tmpfs_dirent_dup(xde));
962 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
964 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
965 memcpy(nde, xde, sizeof(*xde));
966 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
967 LIST_INIT(&xde->ud.td_duphead);
970 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
971 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
973 dnode->tn_size += sizeof(struct tmpfs_dirent);
974 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
979 * Detaches the directory entry de from the directory represented by vp.
980 * Note that this does not change the link count of the node pointed by
981 * the directory entry, as this is done by tmpfs_free_dirent.
984 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
986 struct tmpfs_mount *tmp;
987 struct tmpfs_dir *head;
988 struct tmpfs_node *dnode;
989 struct tmpfs_dirent *xde;
991 ASSERT_VOP_ELOCKED(vp, __func__);
993 dnode = VP_TO_TMPFS_DIR(vp);
994 head = &dnode->tn_dir.tn_dirhead;
995 dnode->tn_dir.tn_readdir_lastn = 0;
996 dnode->tn_dir.tn_readdir_lastp = NULL;
998 if (tmpfs_dirent_dup(de)) {
999 /* Remove duphead if de was last entry. */
1000 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1001 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1002 MPASS(tmpfs_dirent_duphead(xde));
1005 LIST_REMOVE(de, uh.td_dup.entries);
1006 LIST_REMOVE(de, uh.td_dup.index_entries);
1008 if (LIST_EMPTY(&xde->ud.td_duphead)) {
1009 RB_REMOVE(tmpfs_dir, head, xde);
1010 tmp = VFS_TO_TMPFS(vp->v_mount);
1011 MPASS(xde->td_node == NULL);
1012 tmpfs_free_dirent(tmp, xde);
1016 RB_REMOVE(tmpfs_dir, head, de);
1018 dnode->tn_size -= sizeof(struct tmpfs_dirent);
1019 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1020 TMPFS_NODE_MODIFIED;
1024 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1026 struct tmpfs_dirent *de, *dde, *nde;
1028 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1029 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1030 /* Node may already be destroyed. */
1032 if (tmpfs_dirent_duphead(de)) {
1033 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1034 LIST_REMOVE(dde, uh.td_dup.entries);
1035 dde->td_node = NULL;
1036 tmpfs_free_dirent(tmp, dde);
1039 tmpfs_free_dirent(tmp, de);
1044 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
1045 * directory and returns it in the uio space. The function returns 0
1046 * on success, -1 if there was not enough space in the uio structure to
1047 * hold the directory entry or an appropriate error code if another
1051 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
1056 TMPFS_VALIDATE_DIR(node);
1057 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1059 dent.d_fileno = node->tn_id;
1060 dent.d_type = DT_DIR;
1062 dent.d_name[0] = '.';
1063 dent.d_name[1] = '\0';
1064 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1066 if (dent.d_reclen > uio->uio_resid)
1067 error = EJUSTRETURN;
1069 error = uiomove(&dent, dent.d_reclen, uio);
1071 node->tn_status |= TMPFS_NODE_ACCESSED;
1077 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
1078 * directory and returns it in the uio space. The function returns 0
1079 * on success, -1 if there was not enough space in the uio structure to
1080 * hold the directory entry or an appropriate error code if another
1084 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
1089 TMPFS_VALIDATE_DIR(node);
1090 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1093 * Return ENOENT if the current node is already removed.
1095 TMPFS_ASSERT_LOCKED(node);
1096 if (node->tn_dir.tn_parent == NULL) {
1100 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
1101 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
1102 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
1104 dent.d_type = DT_DIR;
1106 dent.d_name[0] = '.';
1107 dent.d_name[1] = '.';
1108 dent.d_name[2] = '\0';
1109 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1111 if (dent.d_reclen > uio->uio_resid)
1112 error = EJUSTRETURN;
1114 error = uiomove(&dent, dent.d_reclen, uio);
1116 node->tn_status |= TMPFS_NODE_ACCESSED;
1122 * Helper function for tmpfs_readdir. Returns as much directory entries
1123 * as can fit in the uio space. The read starts at uio->uio_offset.
1124 * The function returns 0 on success, -1 if there was not enough space
1125 * in the uio structure to hold the directory entry or an appropriate
1126 * error code if another error happens.
1129 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, int maxcookies,
1130 u_long *cookies, int *ncookies)
1132 struct tmpfs_dir_cursor dc;
1133 struct tmpfs_dirent *de;
1137 TMPFS_VALIDATE_DIR(node);
1142 * Lookup the node from the current offset. The starting offset of
1143 * 0 will lookup both '.' and '..', and then the first real entry,
1144 * or EOF if there are none. Then find all entries for the dir that
1145 * fit into the buffer. Once no more entries are found (de == NULL),
1146 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1149 switch (uio->uio_offset) {
1150 case TMPFS_DIRCOOKIE_DOT:
1151 error = tmpfs_dir_getdotdent(node, uio);
1154 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1155 if (cookies != NULL)
1156 cookies[(*ncookies)++] = off = uio->uio_offset;
1158 case TMPFS_DIRCOOKIE_DOTDOT:
1159 error = tmpfs_dir_getdotdotdent(node, uio);
1162 de = tmpfs_dir_first(node, &dc);
1163 uio->uio_offset = tmpfs_dirent_cookie(de);
1164 if (cookies != NULL)
1165 cookies[(*ncookies)++] = off = uio->uio_offset;
1170 case TMPFS_DIRCOOKIE_EOF:
1173 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1176 if (cookies != NULL)
1177 off = tmpfs_dirent_cookie(de);
1180 /* Read as much entries as possible; i.e., until we reach the end of
1181 * the directory or we exhaust uio space. */
1185 /* Create a dirent structure representing the current
1186 * tmpfs_node and fill it. */
1187 if (de->td_node == NULL) {
1191 d.d_fileno = de->td_node->tn_id;
1192 switch (de->td_node->tn_type) {
1222 panic("tmpfs_dir_getdents: type %p %d",
1223 de->td_node, (int)de->td_node->tn_type);
1226 d.d_namlen = de->td_namelen;
1227 MPASS(de->td_namelen < sizeof(d.d_name));
1228 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1229 d.d_name[de->td_namelen] = '\0';
1230 d.d_reclen = GENERIC_DIRSIZ(&d);
1232 /* Stop reading if the directory entry we are treating is
1233 * bigger than the amount of data that can be returned. */
1234 if (d.d_reclen > uio->uio_resid) {
1235 error = EJUSTRETURN;
1239 /* Copy the new dirent structure into the output buffer and
1240 * advance pointers. */
1241 error = uiomove(&d, d.d_reclen, uio);
1243 de = tmpfs_dir_next(node, &dc);
1244 if (cookies != NULL) {
1245 off = tmpfs_dirent_cookie(de);
1246 MPASS(*ncookies < maxcookies);
1247 cookies[(*ncookies)++] = off;
1250 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1252 /* Skip setting off when using cookies as it is already done above. */
1253 if (cookies == NULL)
1254 off = tmpfs_dirent_cookie(de);
1256 /* Update the offset and cache. */
1257 uio->uio_offset = off;
1258 node->tn_dir.tn_readdir_lastn = off;
1259 node->tn_dir.tn_readdir_lastp = de;
1261 node->tn_status |= TMPFS_NODE_ACCESSED;
1266 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1268 struct tmpfs_dirent *de;
1271 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1272 cnp->cn_nameptr, cnp->cn_namelen, &de);
1275 tmpfs_dir_attach(dvp, de);
1280 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1282 struct tmpfs_dirent *de;
1284 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1285 MPASS(de != NULL && de->td_node == NULL);
1286 tmpfs_dir_detach(dvp, de);
1287 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1291 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1292 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
1293 * 'newsize' must be positive.
1295 * Returns zero on success or an appropriate error code on failure.
1298 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1300 struct tmpfs_mount *tmp;
1301 struct tmpfs_node *node;
1304 vm_pindex_t idx, newpages, oldpages;
1308 MPASS(vp->v_type == VREG);
1309 MPASS(newsize >= 0);
1311 node = VP_TO_TMPFS_NODE(vp);
1312 uobj = node->tn_reg.tn_aobj;
1313 tmp = VFS_TO_TMPFS(vp->v_mount);
1316 * Convert the old and new sizes to the number of pages needed to
1317 * store them. It may happen that we do not need to do anything
1318 * because the last allocated page can accommodate the change on
1321 oldsize = node->tn_size;
1322 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1323 MPASS(oldpages == uobj->size);
1324 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1325 if (newpages > oldpages &&
1326 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1329 VM_OBJECT_WLOCK(uobj);
1330 if (newsize < oldsize) {
1332 * Zero the truncated part of the last page.
1334 base = newsize & PAGE_MASK;
1336 idx = OFF_TO_IDX(newsize);
1338 m = vm_page_lookup(uobj, idx);
1340 if (vm_page_sleep_if_busy(m, "tmfssz"))
1342 MPASS(m->valid == VM_PAGE_BITS_ALL);
1343 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1344 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
1346 VM_OBJECT_WUNLOCK(uobj);
1348 VM_OBJECT_WLOCK(uobj);
1350 } else if (m->valid != VM_PAGE_BITS_ALL) {
1352 rv = vm_pager_get_pages(uobj, ma, 1, 0);
1353 m = vm_page_lookup(uobj, idx);
1355 /* A cached page was reactivated. */
1358 if (rv == VM_PAGER_OK) {
1359 vm_page_deactivate(m);
1368 VM_OBJECT_WUNLOCK(uobj);
1374 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1376 vm_pager_page_unswapped(m);
1381 * Release any swap space and free any whole pages.
1383 if (newpages < oldpages) {
1384 swap_pager_freespace(uobj, newpages, oldpages -
1386 vm_object_page_remove(uobj, newpages, 0, 0);
1389 uobj->size = newpages;
1390 VM_OBJECT_WUNLOCK(uobj);
1393 tmp->tm_pages_used += (newpages - oldpages);
1396 node->tn_size = newsize;
1401 * Change flags of the given vnode.
1402 * Caller should execute tmpfs_update on vp after a successful execution.
1403 * The vnode must be locked on entry and remain locked on exit.
1406 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1410 struct tmpfs_node *node;
1412 MPASS(VOP_ISLOCKED(vp));
1414 node = VP_TO_TMPFS_NODE(vp);
1416 if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1417 UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1418 UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1419 UF_SPARSE | UF_SYSTEM)) != 0)
1420 return (EOPNOTSUPP);
1422 /* Disallow this operation if the file system is mounted read-only. */
1423 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1427 * Callers may only modify the file flags on objects they
1428 * have VADMIN rights for.
1430 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1433 * Unprivileged processes are not permitted to unset system
1434 * flags, or modify flags if any system flags are set.
1436 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1437 if (node->tn_flags &
1438 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1439 error = securelevel_gt(cred, 0);
1444 if (node->tn_flags &
1445 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1446 ((flags ^ node->tn_flags) & SF_SETTABLE))
1449 node->tn_flags = flags;
1450 node->tn_status |= TMPFS_NODE_CHANGED;
1452 MPASS(VOP_ISLOCKED(vp));
1458 * Change access mode on the given vnode.
1459 * Caller should execute tmpfs_update on vp after a successful execution.
1460 * The vnode must be locked on entry and remain locked on exit.
1463 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1466 struct tmpfs_node *node;
1468 MPASS(VOP_ISLOCKED(vp));
1470 node = VP_TO_TMPFS_NODE(vp);
1472 /* Disallow this operation if the file system is mounted read-only. */
1473 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1476 /* Immutable or append-only files cannot be modified, either. */
1477 if (node->tn_flags & (IMMUTABLE | APPEND))
1481 * To modify the permissions on a file, must possess VADMIN
1484 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1488 * Privileged processes may set the sticky bit on non-directories,
1489 * as well as set the setgid bit on a file with a group that the
1490 * process is not a member of.
1492 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1493 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1496 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1497 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1503 node->tn_mode &= ~ALLPERMS;
1504 node->tn_mode |= mode & ALLPERMS;
1506 node->tn_status |= TMPFS_NODE_CHANGED;
1508 MPASS(VOP_ISLOCKED(vp));
1514 * Change ownership of the given vnode. At least one of uid or gid must
1515 * be different than VNOVAL. If one is set to that value, the attribute
1517 * Caller should execute tmpfs_update on vp after a successful execution.
1518 * The vnode must be locked on entry and remain locked on exit.
1521 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1525 struct tmpfs_node *node;
1529 MPASS(VOP_ISLOCKED(vp));
1531 node = VP_TO_TMPFS_NODE(vp);
1533 /* Assign default values if they are unknown. */
1534 MPASS(uid != VNOVAL || gid != VNOVAL);
1539 MPASS(uid != VNOVAL && gid != VNOVAL);
1541 /* Disallow this operation if the file system is mounted read-only. */
1542 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1545 /* Immutable or append-only files cannot be modified, either. */
1546 if (node->tn_flags & (IMMUTABLE | APPEND))
1550 * To modify the ownership of a file, must possess VADMIN for that
1553 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1557 * To change the owner of a file, or change the group of a file to a
1558 * group of which we are not a member, the caller must have
1561 if ((uid != node->tn_uid ||
1562 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1563 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1566 ogid = node->tn_gid;
1567 ouid = node->tn_uid;
1572 node->tn_status |= TMPFS_NODE_CHANGED;
1574 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1575 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1576 node->tn_mode &= ~(S_ISUID | S_ISGID);
1579 MPASS(VOP_ISLOCKED(vp));
1585 * Change size of the given vnode.
1586 * Caller should execute tmpfs_update on vp after a successful execution.
1587 * The vnode must be locked on entry and remain locked on exit.
1590 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1594 struct tmpfs_node *node;
1596 MPASS(VOP_ISLOCKED(vp));
1598 node = VP_TO_TMPFS_NODE(vp);
1600 /* Decide whether this is a valid operation based on the file type. */
1602 switch (vp->v_type) {
1607 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1616 /* Allow modifications of special files even if in the file
1617 * system is mounted read-only (we are not modifying the
1618 * files themselves, but the objects they represent). */
1622 /* Anything else is unsupported. */
1626 /* Immutable or append-only files cannot be modified, either. */
1627 if (node->tn_flags & (IMMUTABLE | APPEND))
1630 error = tmpfs_truncate(vp, size);
1631 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1632 * for us, as will update tn_status; no need to do that here. */
1634 MPASS(VOP_ISLOCKED(vp));
1640 * Change access and modification times of the given vnode.
1641 * Caller should execute tmpfs_update on vp after a successful execution.
1642 * The vnode must be locked on entry and remain locked on exit.
1645 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
1646 struct ucred *cred, struct thread *l)
1649 struct tmpfs_node *node;
1651 MPASS(VOP_ISLOCKED(vp));
1653 node = VP_TO_TMPFS_NODE(vp);
1655 /* Disallow this operation if the file system is mounted read-only. */
1656 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1659 /* Immutable or append-only files cannot be modified, either. */
1660 if (node->tn_flags & (IMMUTABLE | APPEND))
1663 error = vn_utimes_perm(vp, vap, cred, l);
1667 if (vap->va_atime.tv_sec != VNOVAL && vap->va_atime.tv_nsec != VNOVAL)
1668 node->tn_status |= TMPFS_NODE_ACCESSED;
1670 if (vap->va_mtime.tv_sec != VNOVAL && vap->va_mtime.tv_nsec != VNOVAL)
1671 node->tn_status |= TMPFS_NODE_MODIFIED;
1673 if (vap->va_birthtime.tv_nsec != VNOVAL &&
1674 vap->va_birthtime.tv_nsec != VNOVAL)
1675 node->tn_status |= TMPFS_NODE_MODIFIED;
1677 tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
1679 if (vap->va_birthtime.tv_nsec != VNOVAL &&
1680 vap->va_birthtime.tv_nsec != VNOVAL)
1681 node->tn_birthtime = vap->va_birthtime;
1682 MPASS(VOP_ISLOCKED(vp));
1687 /* Sync timestamps */
1689 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1690 const struct timespec *mod)
1692 struct tmpfs_node *node;
1693 struct timespec now;
1695 node = VP_TO_TMPFS_NODE(vp);
1697 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1698 TMPFS_NODE_CHANGED)) == 0)
1701 vfs_timestamp(&now);
1702 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1705 node->tn_atime = *acc;
1707 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1710 node->tn_mtime = *mod;
1712 if (node->tn_status & TMPFS_NODE_CHANGED) {
1713 node->tn_ctime = now;
1716 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1720 tmpfs_update(struct vnode *vp)
1723 tmpfs_itimes(vp, NULL, NULL);
1727 tmpfs_truncate(struct vnode *vp, off_t length)
1730 struct tmpfs_node *node;
1732 node = VP_TO_TMPFS_NODE(vp);
1739 if (node->tn_size == length) {
1744 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1747 error = tmpfs_reg_resize(vp, length, FALSE);
1749 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1759 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1761 if (a->td_hash > b->td_hash)
1763 else if (a->td_hash < b->td_hash)
1768 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);