1 /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2005 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Efficient memory file system supporting functions.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/fnv_hash.h>
46 #include <sys/limits.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
51 #include <sys/random.h>
52 #include <sys/rwlock.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
56 #include <sys/vmmeter.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pageout.h>
63 #include <vm/vm_pager.h>
64 #include <vm/vm_extern.h>
65 #include <vm/swap_pager.h>
67 #include <fs/tmpfs/tmpfs.h>
68 #include <fs/tmpfs/tmpfs_fifoops.h>
69 #include <fs/tmpfs/tmpfs_vnops.h>
71 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "tmpfs file system");
73 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
76 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
81 pages = *(long *)arg1;
82 bytes = pages * PAGE_SIZE;
84 error = sysctl_handle_long(oidp, &bytes, 0, req);
85 if (error || !req->newptr)
88 pages = bytes / PAGE_SIZE;
89 if (pages < TMPFS_PAGES_MINRESERVED)
92 *(long *)arg1 = pages;
96 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved, CTLTYPE_LONG|CTLFLAG_RW,
97 &tmpfs_pages_reserved, 0, sysctl_mem_reserved, "L",
98 "Amount of available memory and swap below which tmpfs growth stops");
100 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
101 struct tmpfs_dirent *b);
102 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
105 tmpfs_mem_avail(void)
109 avail = swap_pager_avail + vm_free_count() - tmpfs_pages_reserved;
110 if (__predict_false(avail < 0))
116 tmpfs_pages_used(struct tmpfs_mount *tmp)
118 const size_t node_size = sizeof(struct tmpfs_node) +
119 sizeof(struct tmpfs_dirent);
122 meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
124 return (meta_pages + tmp->tm_pages_used);
128 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
130 if (tmpfs_mem_avail() < req_pages)
133 if (tmp->tm_pages_max != ULONG_MAX &&
134 tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
141 tmpfs_ref_node(struct tmpfs_node *node)
144 TMPFS_NODE_LOCK(node);
145 tmpfs_ref_node_locked(node);
146 TMPFS_NODE_UNLOCK(node);
150 tmpfs_ref_node_locked(struct tmpfs_node *node)
153 TMPFS_NODE_ASSERT_LOCKED(node);
154 KASSERT(node->tn_refcount > 0, ("node %p zero refcount", node));
155 KASSERT(node->tn_refcount < UINT_MAX, ("node %p refcount %u", node,
161 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
162 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
163 * using the credentials of the process 'p'.
165 * If the node type is set to 'VDIR', then the parent parameter must point
166 * to the parent directory of the node being created. It may only be NULL
167 * while allocating the root node.
169 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
170 * specifies the device the node represents.
172 * If the node type is set to 'VLNK', then the parameter target specifies
173 * the file name of the target file for the symbolic link that is being
176 * Note that new nodes are retrieved from the available list if it has
177 * items or, if it is empty, from the node pool as long as there is enough
178 * space to create them.
180 * Returns zero on success or an appropriate error code on failure.
183 tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
184 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
185 const char *target, dev_t rdev, struct tmpfs_node **node)
187 struct tmpfs_node *nnode;
190 /* If the root directory of the 'tmp' file system is not yet
191 * allocated, this must be the request to do it. */
192 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
193 KASSERT(tmp->tm_root == NULL || mp->mnt_writeopcount > 0,
194 ("creating node not under vn_start_write"));
196 MPASS(IFF(type == VLNK, target != NULL));
197 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
199 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
201 if (tmpfs_pages_check_avail(tmp, 1) == 0)
204 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
206 * When a new tmpfs node is created for fully
207 * constructed mount point, there must be a parent
208 * node, which vnode is locked exclusively. As
209 * consequence, if the unmount is executing in
210 * parallel, vflush() cannot reclaim the parent vnode.
211 * Due to this, the check for MNTK_UNMOUNT flag is not
212 * racy: if we did not see MNTK_UNMOUNT flag, then tmp
213 * cannot be destroyed until node construction is
214 * finished and the parent vnode unlocked.
216 * Tmpfs does not need to instantiate new nodes during
221 if ((mp->mnt_kern_flag & MNT_RDONLY) != 0)
224 nnode = (struct tmpfs_node *)uma_zalloc_arg(tmp->tm_node_pool, tmp,
227 /* Generic initialization. */
228 nnode->tn_type = type;
229 vfs_timestamp(&nnode->tn_atime);
230 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
234 nnode->tn_mode = mode;
235 nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
236 nnode->tn_refcount = 1;
238 /* Type-specific initialization. */
239 switch (nnode->tn_type) {
242 nnode->tn_rdev = rdev;
246 RB_INIT(&nnode->tn_dir.tn_dirhead);
247 LIST_INIT(&nnode->tn_dir.tn_dupindex);
248 MPASS(parent != nnode);
249 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
250 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
251 nnode->tn_dir.tn_readdir_lastn = 0;
252 nnode->tn_dir.tn_readdir_lastp = NULL;
254 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
255 nnode->tn_dir.tn_parent->tn_links++;
256 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
265 MPASS(strlen(target) < MAXPATHLEN);
266 nnode->tn_size = strlen(target);
267 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
269 memcpy(nnode->tn_link, target, nnode->tn_size);
273 obj = nnode->tn_reg.tn_aobj =
274 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
275 NULL /* XXXKIB - tmpfs needs swap reservation */);
276 VM_OBJECT_WLOCK(obj);
277 /* OBJ_TMPFS is set together with the setting of vp->v_object */
278 vm_object_set_flag(obj, OBJ_NOSPLIT | OBJ_TMPFS_NODE);
279 vm_object_clear_flag(obj, OBJ_ONEMAPPING);
280 VM_OBJECT_WUNLOCK(obj);
284 panic("tmpfs_alloc_node: type %p %d", nnode,
285 (int)nnode->tn_type);
289 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
290 nnode->tn_attached = true;
291 tmp->tm_nodes_inuse++;
300 * Destroys the node pointed to by node from the file system 'tmp'.
301 * If the node references a directory, no entries are allowed.
304 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
308 TMPFS_NODE_LOCK(node);
309 if (!tmpfs_free_node_locked(tmp, node, false)) {
310 TMPFS_NODE_UNLOCK(node);
316 tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node,
321 TMPFS_MP_ASSERT_LOCKED(tmp);
322 TMPFS_NODE_ASSERT_LOCKED(node);
323 KASSERT(node->tn_refcount > 0, ("node %p refcount zero", node));
326 if (node->tn_attached && (detach || node->tn_refcount == 0)) {
327 MPASS(tmp->tm_nodes_inuse > 0);
328 tmp->tm_nodes_inuse--;
329 LIST_REMOVE(node, tn_entries);
330 node->tn_attached = false;
332 if (node->tn_refcount > 0)
336 MPASS(node->tn_vnode == NULL);
337 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
339 TMPFS_NODE_UNLOCK(node);
342 switch (node->tn_type) {
355 free(node->tn_link, M_TMPFSNAME);
359 uobj = node->tn_reg.tn_aobj;
362 atomic_subtract_long(&tmp->tm_pages_used, uobj->size);
363 KASSERT((uobj->flags & OBJ_TMPFS) == 0,
364 ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
365 vm_object_deallocate(uobj);
370 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
373 uma_zfree(tmp->tm_node_pool, node);
379 static __inline uint32_t
380 tmpfs_dirent_hash(const char *name, u_int len)
384 hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
385 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
388 if (hash < TMPFS_DIRCOOKIE_MIN)
389 hash += TMPFS_DIRCOOKIE_MIN;
394 static __inline off_t
395 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
398 return (TMPFS_DIRCOOKIE_EOF);
400 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
402 return (de->td_cookie);
405 static __inline boolean_t
406 tmpfs_dirent_dup(struct tmpfs_dirent *de)
408 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
411 static __inline boolean_t
412 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
414 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
418 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
420 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
421 memcpy(de->ud.td_name, name, namelen);
422 de->td_namelen = namelen;
426 * Allocates a new directory entry for the node node with a name of name.
427 * The new directory entry is returned in *de.
429 * The link count of node is increased by one to reflect the new object
432 * Returns zero on success or an appropriate error code on failure.
435 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
436 const char *name, u_int len, struct tmpfs_dirent **de)
438 struct tmpfs_dirent *nde;
440 nde = uma_zalloc(tmp->tm_dirent_pool, M_WAITOK);
443 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
444 tmpfs_dirent_init(nde, name, len);
456 * Frees a directory entry. It is the caller's responsibility to destroy
457 * the node referenced by it if needed.
459 * The link count of node is decreased by one to reflect the removal of an
460 * object that referenced it. This only happens if 'node_exists' is true;
461 * otherwise the function will not access the node referred to by the
462 * directory entry, as it may already have been released from the outside.
465 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
467 struct tmpfs_node *node;
471 MPASS(node->tn_links > 0);
474 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
475 free(de->ud.td_name, M_TMPFSNAME);
476 uma_zfree(tmp->tm_dirent_pool, de);
480 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
483 ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
484 if (vp->v_type != VREG || obj == NULL)
487 VM_OBJECT_WLOCK(obj);
489 vm_object_clear_flag(obj, OBJ_TMPFS);
490 obj->un_pager.swp.swp_tmpfs = NULL;
492 VM_OBJECT_WUNLOCK(obj);
496 * Need to clear v_object for insmntque failure.
499 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
502 tmpfs_destroy_vobject(vp, vp->v_object);
505 vp->v_op = &dead_vnodeops;
511 * Allocates a new vnode for the node node or returns a new reference to
512 * an existing one if the node had already a vnode referencing it. The
513 * resulting locked vnode is returned in *vpp.
515 * Returns zero on success or an appropriate error code on failure.
518 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
522 struct tmpfs_mount *tm;
527 tm = VFS_TO_TMPFS(mp);
528 TMPFS_NODE_LOCK(node);
529 tmpfs_ref_node_locked(node);
531 TMPFS_NODE_ASSERT_LOCKED(node);
532 if ((vp = node->tn_vnode) != NULL) {
533 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
535 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
536 ((vp->v_iflag & VI_DOOMED) != 0 &&
537 (lkflag & LK_NOWAIT) != 0)) {
539 TMPFS_NODE_UNLOCK(node);
544 if ((vp->v_iflag & VI_DOOMED) != 0) {
546 node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
547 while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
548 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
553 TMPFS_NODE_UNLOCK(node);
554 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
555 if (error == ENOENT) {
556 TMPFS_NODE_LOCK(node);
565 * Make sure the vnode is still there after
566 * getting the interlock to avoid racing a free.
568 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
570 TMPFS_NODE_LOCK(node);
577 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
578 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
579 TMPFS_NODE_UNLOCK(node);
586 * otherwise lock the vp list while we call getnewvnode
587 * since that can block.
589 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
590 node->tn_vpstate |= TMPFS_VNODE_WANT;
591 error = msleep((caddr_t) &node->tn_vpstate,
592 TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0);
597 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
599 TMPFS_NODE_UNLOCK(node);
601 /* Get a new vnode and associate it with our node. */
602 error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ?
603 &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp);
608 /* lkflag is ignored, the lock is exclusive */
609 (void) vn_lock(vp, lkflag | LK_RETRY);
612 vp->v_type = node->tn_type;
614 /* Type-specific initialization. */
615 switch (node->tn_type) {
625 vp->v_op = &tmpfs_fifoop_entries;
628 object = node->tn_reg.tn_aobj;
629 VM_OBJECT_WLOCK(object);
631 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
632 vp->v_object = object;
633 object->un_pager.swp.swp_tmpfs = vp;
634 vm_object_set_flag(object, OBJ_TMPFS);
636 VM_OBJECT_WUNLOCK(object);
639 MPASS(node->tn_dir.tn_parent != NULL);
640 if (node->tn_dir.tn_parent == node)
641 vp->v_vflag |= VV_ROOT;
645 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
647 if (vp->v_type != VFIFO)
650 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
655 TMPFS_NODE_LOCK(node);
657 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
658 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
661 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
662 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
663 TMPFS_NODE_UNLOCK(node);
664 wakeup((caddr_t) &node->tn_vpstate);
666 TMPFS_NODE_UNLOCK(node);
673 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
674 TMPFS_NODE_LOCK(node);
675 MPASS(*vpp == node->tn_vnode);
676 TMPFS_NODE_UNLOCK(node);
679 tmpfs_free_node(tm, node);
685 * Destroys the association between the vnode vp and the node it
689 tmpfs_free_vp(struct vnode *vp)
691 struct tmpfs_node *node;
693 node = VP_TO_TMPFS_NODE(vp);
695 TMPFS_NODE_ASSERT_LOCKED(node);
696 node->tn_vnode = NULL;
697 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
698 wakeup(&node->tn_vnode);
699 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
704 * Allocates a new file of type 'type' and adds it to the parent directory
705 * 'dvp'; this addition is done using the component name given in 'cnp'.
706 * The ownership of the new file is automatically assigned based on the
707 * credentials of the caller (through 'cnp'), the group is set based on
708 * the parent directory and the mode is determined from the 'vap' argument.
709 * If successful, *vpp holds a vnode to the newly created file and zero
710 * is returned. Otherwise *vpp is NULL and the function returns an
711 * appropriate error code.
714 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
715 struct componentname *cnp, const char *target)
718 struct tmpfs_dirent *de;
719 struct tmpfs_mount *tmp;
720 struct tmpfs_node *dnode;
721 struct tmpfs_node *node;
722 struct tmpfs_node *parent;
724 ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file");
725 MPASS(cnp->cn_flags & HASBUF);
727 tmp = VFS_TO_TMPFS(dvp->v_mount);
728 dnode = VP_TO_TMPFS_DIR(dvp);
731 /* If the entry we are creating is a directory, we cannot overflow
732 * the number of links of its parent, because it will get a new
734 if (vap->va_type == VDIR) {
735 /* Ensure that we do not overflow the maximum number of links
736 * imposed by the system. */
737 MPASS(dnode->tn_links <= TMPFS_LINK_MAX);
738 if (dnode->tn_links == TMPFS_LINK_MAX) {
743 MPASS(parent != NULL);
747 /* Allocate a node that represents the new file. */
748 error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
749 cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
750 target, vap->va_rdev, &node);
754 /* Allocate a directory entry that points to the new file. */
755 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
758 tmpfs_free_node(tmp, node);
762 /* Allocate a vnode for the new file. */
763 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
765 tmpfs_free_dirent(tmp, de);
766 tmpfs_free_node(tmp, node);
770 /* Now that all required items are allocated, we can proceed to
771 * insert the new node into the directory, an operation that
773 if (cnp->cn_flags & ISWHITEOUT)
774 tmpfs_dir_whiteout_remove(dvp, cnp);
775 tmpfs_dir_attach(dvp, de);
779 struct tmpfs_dirent *
780 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
782 struct tmpfs_dirent *de;
784 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
786 if (de != NULL && tmpfs_dirent_duphead(de))
787 de = LIST_FIRST(&de->ud.td_duphead);
788 dc->tdc_current = de;
790 return (dc->tdc_current);
793 struct tmpfs_dirent *
794 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
796 struct tmpfs_dirent *de;
798 MPASS(dc->tdc_tree != NULL);
799 if (tmpfs_dirent_dup(dc->tdc_current)) {
800 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
801 if (dc->tdc_current != NULL)
802 return (dc->tdc_current);
804 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
805 &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
806 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
807 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
808 MPASS(dc->tdc_current != NULL);
811 return (dc->tdc_current);
814 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
815 static struct tmpfs_dirent *
816 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
818 struct tmpfs_dirent *de, dekey;
820 dekey.td_hash = hash;
821 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
825 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
826 static struct tmpfs_dirent *
827 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
828 struct tmpfs_dir_cursor *dc)
830 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
831 struct tmpfs_dirent *de, dekey;
833 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
835 if (cookie == node->tn_dir.tn_readdir_lastn &&
836 (de = node->tn_dir.tn_readdir_lastp) != NULL) {
837 /* Protect against possible race, tn_readdir_last[pn]
838 * may be updated with only shared vnode lock held. */
839 if (cookie == tmpfs_dirent_cookie(de))
843 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
844 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
845 uh.td_dup.index_entries) {
846 MPASS(tmpfs_dirent_dup(de));
847 if (de->td_cookie == cookie)
849 /* dupindex list is sorted. */
850 if (de->td_cookie < cookie) {
859 if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) {
862 dekey.td_hash = cookie;
863 /* Recover if direntry for cookie was removed */
864 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
867 dc->tdc_current = de;
868 if (de != NULL && tmpfs_dirent_duphead(de)) {
869 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
870 MPASS(dc->tdc_current != NULL);
872 return (dc->tdc_current);
876 dc->tdc_current = de;
877 if (de != NULL && tmpfs_dirent_dup(de))
878 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
880 return (dc->tdc_current);
884 * Looks for a directory entry in the directory represented by node.
885 * 'cnp' describes the name of the entry to look for. Note that the .
886 * and .. components are not allowed as they do not physically exist
887 * within directories.
889 * Returns a pointer to the entry when found, otherwise NULL.
891 struct tmpfs_dirent *
892 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
893 struct componentname *cnp)
895 struct tmpfs_dir_duphead *duphead;
896 struct tmpfs_dirent *de;
899 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
900 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
901 cnp->cn_nameptr[1] == '.')));
902 TMPFS_VALIDATE_DIR(node);
904 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
905 de = tmpfs_dir_xlookup_hash(node, hash);
906 if (de != NULL && tmpfs_dirent_duphead(de)) {
907 duphead = &de->ud.td_duphead;
908 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
909 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
913 } else if (de != NULL) {
914 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
918 if (de != NULL && f != NULL && de->td_node != f)
925 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
926 * list, allocate new cookie value.
929 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
930 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
932 struct tmpfs_dir_duphead *dupindex;
933 struct tmpfs_dirent *de, *pde;
935 dupindex = &dnode->tn_dir.tn_dupindex;
936 de = LIST_FIRST(dupindex);
937 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
939 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
941 nde->td_cookie = de->td_cookie + 1;
942 MPASS(tmpfs_dirent_dup(nde));
943 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
944 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
949 * Cookie numbers are near exhaustion. Scan dupindex list for unused
950 * numbers. dupindex list is sorted in descending order. Keep it so
951 * after inserting nde.
955 de = LIST_NEXT(de, uh.td_dup.index_entries);
956 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
958 * Last element of the index doesn't have minimal cookie
961 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
962 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
963 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
965 } else if (de == NULL) {
967 * We are so lucky have 2^30 hash duplicates in single
968 * directory :) Return largest possible cookie value.
969 * It should be fine except possible issues with
970 * VOP_READDIR restart.
972 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
973 LIST_INSERT_HEAD(dupindex, nde,
974 uh.td_dup.index_entries);
975 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
978 if (de->td_cookie + 1 == pde->td_cookie ||
979 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
980 continue; /* No hole or invalid cookie. */
981 nde->td_cookie = de->td_cookie + 1;
982 MPASS(tmpfs_dirent_dup(nde));
983 MPASS(pde->td_cookie > nde->td_cookie);
984 MPASS(nde->td_cookie > de->td_cookie);
985 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
986 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
992 * Attaches the directory entry de to the directory represented by vp.
993 * Note that this does not change the link count of the node pointed by
994 * the directory entry, as this is done by tmpfs_alloc_dirent.
997 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
999 struct tmpfs_node *dnode;
1000 struct tmpfs_dirent *xde, *nde;
1002 ASSERT_VOP_ELOCKED(vp, __func__);
1003 MPASS(de->td_namelen > 0);
1004 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
1005 MPASS(de->td_cookie == de->td_hash);
1007 dnode = VP_TO_TMPFS_DIR(vp);
1008 dnode->tn_dir.tn_readdir_lastn = 0;
1009 dnode->tn_dir.tn_readdir_lastp = NULL;
1011 MPASS(!tmpfs_dirent_dup(de));
1012 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1013 if (xde != NULL && tmpfs_dirent_duphead(xde))
1014 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1015 else if (xde != NULL) {
1017 * Allocate new duphead. Swap xde with duphead to avoid
1018 * adding/removing elements with the same hash.
1020 MPASS(!tmpfs_dirent_dup(xde));
1021 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
1023 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
1024 memcpy(nde, xde, sizeof(*xde));
1025 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
1026 LIST_INIT(&xde->ud.td_duphead);
1027 xde->td_namelen = 0;
1028 xde->td_node = NULL;
1029 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
1030 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1032 dnode->tn_size += sizeof(struct tmpfs_dirent);
1033 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1034 TMPFS_NODE_MODIFIED;
1039 * Detaches the directory entry de from the directory represented by vp.
1040 * Note that this does not change the link count of the node pointed by
1041 * the directory entry, as this is done by tmpfs_free_dirent.
1044 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
1046 struct tmpfs_mount *tmp;
1047 struct tmpfs_dir *head;
1048 struct tmpfs_node *dnode;
1049 struct tmpfs_dirent *xde;
1051 ASSERT_VOP_ELOCKED(vp, __func__);
1053 dnode = VP_TO_TMPFS_DIR(vp);
1054 head = &dnode->tn_dir.tn_dirhead;
1055 dnode->tn_dir.tn_readdir_lastn = 0;
1056 dnode->tn_dir.tn_readdir_lastp = NULL;
1058 if (tmpfs_dirent_dup(de)) {
1059 /* Remove duphead if de was last entry. */
1060 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1061 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1062 MPASS(tmpfs_dirent_duphead(xde));
1065 LIST_REMOVE(de, uh.td_dup.entries);
1066 LIST_REMOVE(de, uh.td_dup.index_entries);
1068 if (LIST_EMPTY(&xde->ud.td_duphead)) {
1069 RB_REMOVE(tmpfs_dir, head, xde);
1070 tmp = VFS_TO_TMPFS(vp->v_mount);
1071 MPASS(xde->td_node == NULL);
1072 tmpfs_free_dirent(tmp, xde);
1075 de->td_cookie = de->td_hash;
1077 RB_REMOVE(tmpfs_dir, head, de);
1079 dnode->tn_size -= sizeof(struct tmpfs_dirent);
1080 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1081 TMPFS_NODE_MODIFIED;
1086 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1088 struct tmpfs_dirent *de, *dde, *nde;
1090 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1091 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1092 /* Node may already be destroyed. */
1094 if (tmpfs_dirent_duphead(de)) {
1095 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1096 LIST_REMOVE(dde, uh.td_dup.entries);
1097 dde->td_node = NULL;
1098 tmpfs_free_dirent(tmp, dde);
1101 tmpfs_free_dirent(tmp, de);
1106 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
1107 * directory and returns it in the uio space. The function returns 0
1108 * on success, -1 if there was not enough space in the uio structure to
1109 * hold the directory entry or an appropriate error code if another
1113 tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1119 TMPFS_VALIDATE_DIR(node);
1120 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1122 dent.d_fileno = node->tn_id;
1123 dent.d_type = DT_DIR;
1125 dent.d_name[0] = '.';
1126 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1127 dirent_terminate(&dent);
1129 if (dent.d_reclen > uio->uio_resid)
1130 error = EJUSTRETURN;
1132 error = uiomove(&dent, dent.d_reclen, uio);
1134 tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1140 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
1141 * directory and returns it in the uio space. The function returns 0
1142 * on success, -1 if there was not enough space in the uio structure to
1143 * hold the directory entry or an appropriate error code if another
1147 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1153 TMPFS_VALIDATE_DIR(node);
1154 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1157 * Return ENOENT if the current node is already removed.
1159 TMPFS_ASSERT_LOCKED(node);
1160 if (node->tn_dir.tn_parent == NULL)
1163 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
1164 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
1165 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
1167 dent.d_type = DT_DIR;
1169 dent.d_name[0] = '.';
1170 dent.d_name[1] = '.';
1171 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1172 dirent_terminate(&dent);
1174 if (dent.d_reclen > uio->uio_resid)
1175 error = EJUSTRETURN;
1177 error = uiomove(&dent, dent.d_reclen, uio);
1179 tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1185 * Helper function for tmpfs_readdir. Returns as much directory entries
1186 * as can fit in the uio space. The read starts at uio->uio_offset.
1187 * The function returns 0 on success, -1 if there was not enough space
1188 * in the uio structure to hold the directory entry or an appropriate
1189 * error code if another error happens.
1192 tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node,
1193 struct uio *uio, int maxcookies, u_long *cookies, int *ncookies)
1195 struct tmpfs_dir_cursor dc;
1196 struct tmpfs_dirent *de;
1200 TMPFS_VALIDATE_DIR(node);
1205 * Lookup the node from the current offset. The starting offset of
1206 * 0 will lookup both '.' and '..', and then the first real entry,
1207 * or EOF if there are none. Then find all entries for the dir that
1208 * fit into the buffer. Once no more entries are found (de == NULL),
1209 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1212 switch (uio->uio_offset) {
1213 case TMPFS_DIRCOOKIE_DOT:
1214 error = tmpfs_dir_getdotdent(tm, node, uio);
1217 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1218 if (cookies != NULL)
1219 cookies[(*ncookies)++] = off = uio->uio_offset;
1221 case TMPFS_DIRCOOKIE_DOTDOT:
1222 error = tmpfs_dir_getdotdotdent(tm, node, uio);
1225 de = tmpfs_dir_first(node, &dc);
1226 uio->uio_offset = tmpfs_dirent_cookie(de);
1227 if (cookies != NULL)
1228 cookies[(*ncookies)++] = off = uio->uio_offset;
1233 case TMPFS_DIRCOOKIE_EOF:
1236 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1239 if (cookies != NULL)
1240 off = tmpfs_dirent_cookie(de);
1243 /* Read as much entries as possible; i.e., until we reach the end of
1244 * the directory or we exhaust uio space. */
1248 /* Create a dirent structure representing the current
1249 * tmpfs_node and fill it. */
1250 if (de->td_node == NULL) {
1254 d.d_fileno = de->td_node->tn_id;
1255 switch (de->td_node->tn_type) {
1285 panic("tmpfs_dir_getdents: type %p %d",
1286 de->td_node, (int)de->td_node->tn_type);
1289 d.d_namlen = de->td_namelen;
1290 MPASS(de->td_namelen < sizeof(d.d_name));
1291 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1292 d.d_reclen = GENERIC_DIRSIZ(&d);
1293 dirent_terminate(&d);
1295 /* Stop reading if the directory entry we are treating is
1296 * bigger than the amount of data that can be returned. */
1297 if (d.d_reclen > uio->uio_resid) {
1298 error = EJUSTRETURN;
1302 /* Copy the new dirent structure into the output buffer and
1303 * advance pointers. */
1304 error = uiomove(&d, d.d_reclen, uio);
1306 de = tmpfs_dir_next(node, &dc);
1307 if (cookies != NULL) {
1308 off = tmpfs_dirent_cookie(de);
1309 MPASS(*ncookies < maxcookies);
1310 cookies[(*ncookies)++] = off;
1313 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1315 /* Skip setting off when using cookies as it is already done above. */
1316 if (cookies == NULL)
1317 off = tmpfs_dirent_cookie(de);
1319 /* Update the offset and cache. */
1320 uio->uio_offset = off;
1321 node->tn_dir.tn_readdir_lastn = off;
1322 node->tn_dir.tn_readdir_lastp = de;
1324 tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1329 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1331 struct tmpfs_dirent *de;
1334 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1335 cnp->cn_nameptr, cnp->cn_namelen, &de);
1338 tmpfs_dir_attach(dvp, de);
1343 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1345 struct tmpfs_dirent *de;
1347 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1348 MPASS(de != NULL && de->td_node == NULL);
1349 tmpfs_dir_detach(dvp, de);
1350 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1354 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1355 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
1356 * 'newsize' must be positive.
1358 * Returns zero on success or an appropriate error code on failure.
1361 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1363 struct tmpfs_mount *tmp;
1364 struct tmpfs_node *node;
1367 vm_pindex_t idx, newpages, oldpages;
1371 MPASS(vp->v_type == VREG);
1372 MPASS(newsize >= 0);
1374 node = VP_TO_TMPFS_NODE(vp);
1375 uobj = node->tn_reg.tn_aobj;
1376 tmp = VFS_TO_TMPFS(vp->v_mount);
1379 * Convert the old and new sizes to the number of pages needed to
1380 * store them. It may happen that we do not need to do anything
1381 * because the last allocated page can accommodate the change on
1384 oldsize = node->tn_size;
1385 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1386 MPASS(oldpages == uobj->size);
1387 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1389 if (__predict_true(newpages == oldpages && newsize >= oldsize)) {
1390 node->tn_size = newsize;
1394 if (newpages > oldpages &&
1395 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1398 VM_OBJECT_WLOCK(uobj);
1399 if (newsize < oldsize) {
1401 * Zero the truncated part of the last page.
1403 base = newsize & PAGE_MASK;
1405 idx = OFF_TO_IDX(newsize);
1407 m = vm_page_lookup(uobj, idx);
1409 if (vm_page_sleep_if_busy(m, "tmfssz"))
1411 MPASS(m->valid == VM_PAGE_BITS_ALL);
1412 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1413 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL |
1417 rv = vm_pager_get_pages(uobj, &m, 1, NULL,
1420 if (rv == VM_PAGER_OK) {
1422 * Since the page was not resident,
1423 * and therefore not recently
1424 * accessed, immediately enqueue it
1425 * for asynchronous laundering. The
1426 * current operation is not regarded
1438 VM_OBJECT_WUNLOCK(uobj);
1444 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1446 vm_pager_page_unswapped(m);
1451 * Release any swap space and free any whole pages.
1453 if (newpages < oldpages) {
1454 swap_pager_freespace(uobj, newpages, oldpages -
1456 vm_object_page_remove(uobj, newpages, 0, 0);
1459 uobj->size = newpages;
1460 VM_OBJECT_WUNLOCK(uobj);
1462 atomic_add_long(&tmp->tm_pages_used, newpages - oldpages);
1464 node->tn_size = newsize;
1469 tmpfs_check_mtime(struct vnode *vp)
1471 struct tmpfs_node *node;
1472 struct vm_object *obj;
1474 ASSERT_VOP_ELOCKED(vp, "check_mtime");
1475 if (vp->v_type != VREG)
1478 KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
1479 (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
1481 if ((obj->flags & OBJ_TMPFS_DIRTY) != 0) {
1482 VM_OBJECT_WLOCK(obj);
1483 if ((obj->flags & OBJ_TMPFS_DIRTY) != 0) {
1484 obj->flags &= ~OBJ_TMPFS_DIRTY;
1485 node = VP_TO_TMPFS_NODE(vp);
1486 node->tn_status |= TMPFS_NODE_MODIFIED |
1489 VM_OBJECT_WUNLOCK(obj);
1494 * Change flags of the given vnode.
1495 * Caller should execute tmpfs_update on vp after a successful execution.
1496 * The vnode must be locked on entry and remain locked on exit.
1499 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1503 struct tmpfs_node *node;
1505 ASSERT_VOP_ELOCKED(vp, "chflags");
1507 node = VP_TO_TMPFS_NODE(vp);
1509 if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1510 UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1511 UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1512 UF_SPARSE | UF_SYSTEM)) != 0)
1513 return (EOPNOTSUPP);
1515 /* Disallow this operation if the file system is mounted read-only. */
1516 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1520 * Callers may only modify the file flags on objects they
1521 * have VADMIN rights for.
1523 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1526 * Unprivileged processes are not permitted to unset system
1527 * flags, or modify flags if any system flags are set.
1529 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
1530 if (node->tn_flags &
1531 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1532 error = securelevel_gt(cred, 0);
1537 if (node->tn_flags &
1538 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1539 ((flags ^ node->tn_flags) & SF_SETTABLE))
1542 node->tn_flags = flags;
1543 node->tn_status |= TMPFS_NODE_CHANGED;
1545 ASSERT_VOP_ELOCKED(vp, "chflags2");
1551 * Change access mode on the given vnode.
1552 * Caller should execute tmpfs_update on vp after a successful execution.
1553 * The vnode must be locked on entry and remain locked on exit.
1556 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1559 struct tmpfs_node *node;
1561 ASSERT_VOP_ELOCKED(vp, "chmod");
1563 node = VP_TO_TMPFS_NODE(vp);
1565 /* Disallow this operation if the file system is mounted read-only. */
1566 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1569 /* Immutable or append-only files cannot be modified, either. */
1570 if (node->tn_flags & (IMMUTABLE | APPEND))
1574 * To modify the permissions on a file, must possess VADMIN
1577 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1581 * Privileged processes may set the sticky bit on non-directories,
1582 * as well as set the setgid bit on a file with a group that the
1583 * process is not a member of.
1585 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1586 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1589 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1590 error = priv_check_cred(cred, PRIV_VFS_SETGID);
1596 node->tn_mode &= ~ALLPERMS;
1597 node->tn_mode |= mode & ALLPERMS;
1599 node->tn_status |= TMPFS_NODE_CHANGED;
1601 ASSERT_VOP_ELOCKED(vp, "chmod2");
1607 * Change ownership of the given vnode. At least one of uid or gid must
1608 * be different than VNOVAL. If one is set to that value, the attribute
1610 * Caller should execute tmpfs_update on vp after a successful execution.
1611 * The vnode must be locked on entry and remain locked on exit.
1614 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1618 struct tmpfs_node *node;
1622 ASSERT_VOP_ELOCKED(vp, "chown");
1624 node = VP_TO_TMPFS_NODE(vp);
1626 /* Assign default values if they are unknown. */
1627 MPASS(uid != VNOVAL || gid != VNOVAL);
1632 MPASS(uid != VNOVAL && gid != VNOVAL);
1634 /* Disallow this operation if the file system is mounted read-only. */
1635 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1638 /* Immutable or append-only files cannot be modified, either. */
1639 if (node->tn_flags & (IMMUTABLE | APPEND))
1643 * To modify the ownership of a file, must possess VADMIN for that
1646 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1650 * To change the owner of a file, or change the group of a file to a
1651 * group of which we are not a member, the caller must have
1654 if ((uid != node->tn_uid ||
1655 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1656 (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
1659 ogid = node->tn_gid;
1660 ouid = node->tn_uid;
1665 node->tn_status |= TMPFS_NODE_CHANGED;
1667 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1668 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID))
1669 node->tn_mode &= ~(S_ISUID | S_ISGID);
1672 ASSERT_VOP_ELOCKED(vp, "chown2");
1678 * Change size of the given vnode.
1679 * Caller should execute tmpfs_update on vp after a successful execution.
1680 * The vnode must be locked on entry and remain locked on exit.
1683 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1687 struct tmpfs_node *node;
1689 ASSERT_VOP_ELOCKED(vp, "chsize");
1691 node = VP_TO_TMPFS_NODE(vp);
1693 /* Decide whether this is a valid operation based on the file type. */
1695 switch (vp->v_type) {
1700 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1709 /* Allow modifications of special files even if in the file
1710 * system is mounted read-only (we are not modifying the
1711 * files themselves, but the objects they represent). */
1715 /* Anything else is unsupported. */
1719 /* Immutable or append-only files cannot be modified, either. */
1720 if (node->tn_flags & (IMMUTABLE | APPEND))
1723 error = tmpfs_truncate(vp, size);
1724 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1725 * for us, as will update tn_status; no need to do that here. */
1727 ASSERT_VOP_ELOCKED(vp, "chsize2");
1733 * Change access and modification times of the given vnode.
1734 * Caller should execute tmpfs_update on vp after a successful execution.
1735 * The vnode must be locked on entry and remain locked on exit.
1738 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
1739 struct ucred *cred, struct thread *l)
1742 struct tmpfs_node *node;
1744 ASSERT_VOP_ELOCKED(vp, "chtimes");
1746 node = VP_TO_TMPFS_NODE(vp);
1748 /* Disallow this operation if the file system is mounted read-only. */
1749 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1752 /* Immutable or append-only files cannot be modified, either. */
1753 if (node->tn_flags & (IMMUTABLE | APPEND))
1756 error = vn_utimes_perm(vp, vap, cred, l);
1760 if (vap->va_atime.tv_sec != VNOVAL)
1761 node->tn_status |= TMPFS_NODE_ACCESSED;
1763 if (vap->va_mtime.tv_sec != VNOVAL)
1764 node->tn_status |= TMPFS_NODE_MODIFIED;
1766 if (vap->va_birthtime.tv_sec != VNOVAL)
1767 node->tn_status |= TMPFS_NODE_MODIFIED;
1769 tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
1771 if (vap->va_birthtime.tv_sec != VNOVAL)
1772 node->tn_birthtime = vap->va_birthtime;
1773 ASSERT_VOP_ELOCKED(vp, "chtimes2");
1779 tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status)
1782 if ((node->tn_status & status) == status || tm->tm_ronly)
1784 TMPFS_NODE_LOCK(node);
1785 node->tn_status |= status;
1786 TMPFS_NODE_UNLOCK(node);
1789 /* Sync timestamps */
1791 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1792 const struct timespec *mod)
1794 struct tmpfs_node *node;
1795 struct timespec now;
1797 ASSERT_VOP_LOCKED(vp, "tmpfs_itimes");
1798 node = VP_TO_TMPFS_NODE(vp);
1800 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1801 TMPFS_NODE_CHANGED)) == 0)
1804 vfs_timestamp(&now);
1805 TMPFS_NODE_LOCK(node);
1806 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1809 node->tn_atime = *acc;
1811 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1814 node->tn_mtime = *mod;
1816 if (node->tn_status & TMPFS_NODE_CHANGED)
1817 node->tn_ctime = now;
1818 node->tn_status &= ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1819 TMPFS_NODE_CHANGED);
1820 TMPFS_NODE_UNLOCK(node);
1822 /* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */
1823 random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME);
1827 tmpfs_update(struct vnode *vp)
1830 tmpfs_itimes(vp, NULL, NULL);
1834 tmpfs_truncate(struct vnode *vp, off_t length)
1837 struct tmpfs_node *node;
1839 node = VP_TO_TMPFS_NODE(vp);
1846 if (node->tn_size == length) {
1851 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1854 error = tmpfs_reg_resize(vp, length, FALSE);
1856 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1865 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1867 if (a->td_hash > b->td_hash)
1869 else if (a->td_hash < b->td_hash)
1874 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);