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 | CTLFLAG_MPSAFE, 0,
74 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
76 static uma_zone_t tmpfs_dirent_pool;
77 static uma_zone_t tmpfs_node_pool;
81 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
83 struct tmpfs_node *node;
91 node->tn_vnode = NULL;
97 tmpfs_node_dtor(void *mem, int size, void *arg)
99 struct tmpfs_node *node;
102 node->tn_type = VNON;
106 tmpfs_node_init(void *mem, int size, int flags)
108 struct tmpfs_node *node;
112 mtx_init(&node->tn_interlock, "tmpfsni", NULL, MTX_DEF);
113 node->tn_gen = arc4random();
118 tmpfs_node_fini(void *mem, int size)
120 struct tmpfs_node *node;
123 mtx_destroy(&node->tn_interlock);
127 tmpfs_subr_init(void)
129 tmpfs_dirent_pool = uma_zcreate("TMPFS dirent",
130 sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
132 tmpfs_node_pool = uma_zcreate("TMPFS node",
133 sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
134 tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
135 VFS_SMR_ZONE_SET(tmpfs_node_pool);
139 tmpfs_subr_uninit(void)
141 uma_zdestroy(tmpfs_node_pool);
142 uma_zdestroy(tmpfs_dirent_pool);
146 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
151 pages = *(long *)arg1;
152 bytes = pages * PAGE_SIZE;
154 error = sysctl_handle_long(oidp, &bytes, 0, req);
155 if (error || !req->newptr)
158 pages = bytes / PAGE_SIZE;
159 if (pages < TMPFS_PAGES_MINRESERVED)
162 *(long *)arg1 = pages;
166 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved,
167 CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RW, &tmpfs_pages_reserved, 0,
168 sysctl_mem_reserved, "L",
169 "Amount of available memory and swap below which tmpfs growth stops");
171 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
172 struct tmpfs_dirent *b);
173 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
176 tmpfs_mem_avail(void)
180 avail = swap_pager_avail + vm_free_count() - tmpfs_pages_reserved;
181 if (__predict_false(avail < 0))
187 tmpfs_pages_used(struct tmpfs_mount *tmp)
189 const size_t node_size = sizeof(struct tmpfs_node) +
190 sizeof(struct tmpfs_dirent);
193 meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
195 return (meta_pages + tmp->tm_pages_used);
199 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
201 if (tmpfs_mem_avail() < req_pages)
204 if (tmp->tm_pages_max != ULONG_MAX &&
205 tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
212 tmpfs_ref_node(struct tmpfs_node *node)
215 TMPFS_NODE_LOCK(node);
216 tmpfs_ref_node_locked(node);
217 TMPFS_NODE_UNLOCK(node);
221 tmpfs_ref_node_locked(struct tmpfs_node *node)
224 TMPFS_NODE_ASSERT_LOCKED(node);
225 KASSERT(node->tn_refcount > 0, ("node %p zero refcount", node));
226 KASSERT(node->tn_refcount < UINT_MAX, ("node %p refcount %u", node,
232 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
233 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
234 * using the credentials of the process 'p'.
236 * If the node type is set to 'VDIR', then the parent parameter must point
237 * to the parent directory of the node being created. It may only be NULL
238 * while allocating the root node.
240 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
241 * specifies the device the node represents.
243 * If the node type is set to 'VLNK', then the parameter target specifies
244 * the file name of the target file for the symbolic link that is being
247 * Note that new nodes are retrieved from the available list if it has
248 * items or, if it is empty, from the node pool as long as there is enough
249 * space to create them.
251 * Returns zero on success or an appropriate error code on failure.
254 tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
255 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
256 const char *target, dev_t rdev, struct tmpfs_node **node)
258 struct tmpfs_node *nnode;
261 /* If the root directory of the 'tmp' file system is not yet
262 * allocated, this must be the request to do it. */
263 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
265 MPASS(IFF(type == VLNK, target != NULL));
266 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
268 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
270 if (tmpfs_pages_check_avail(tmp, 1) == 0)
273 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
275 * When a new tmpfs node is created for fully
276 * constructed mount point, there must be a parent
277 * node, which vnode is locked exclusively. As
278 * consequence, if the unmount is executing in
279 * parallel, vflush() cannot reclaim the parent vnode.
280 * Due to this, the check for MNTK_UNMOUNT flag is not
281 * racy: if we did not see MNTK_UNMOUNT flag, then tmp
282 * cannot be destroyed until node construction is
283 * finished and the parent vnode unlocked.
285 * Tmpfs does not need to instantiate new nodes during
290 if ((mp->mnt_kern_flag & MNT_RDONLY) != 0)
293 nnode = uma_zalloc_smr(tmpfs_node_pool, M_WAITOK);
295 /* Generic initialization. */
296 nnode->tn_type = type;
297 vfs_timestamp(&nnode->tn_atime);
298 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
302 nnode->tn_mode = mode;
303 nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
304 nnode->tn_refcount = 1;
306 /* Type-specific initialization. */
307 switch (nnode->tn_type) {
310 nnode->tn_rdev = rdev;
314 RB_INIT(&nnode->tn_dir.tn_dirhead);
315 LIST_INIT(&nnode->tn_dir.tn_dupindex);
316 MPASS(parent != nnode);
317 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
318 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
319 nnode->tn_dir.tn_readdir_lastn = 0;
320 nnode->tn_dir.tn_readdir_lastp = NULL;
322 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
323 nnode->tn_dir.tn_parent->tn_links++;
324 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
333 MPASS(strlen(target) < MAXPATHLEN);
334 nnode->tn_size = strlen(target);
335 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
337 memcpy(nnode->tn_link, target, nnode->tn_size);
341 obj = nnode->tn_reg.tn_aobj =
342 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
343 NULL /* XXXKIB - tmpfs needs swap reservation */);
344 VM_OBJECT_WLOCK(obj);
345 /* OBJ_TMPFS is set together with the setting of vp->v_object */
346 vm_object_set_flag(obj, OBJ_TMPFS_NODE);
347 VM_OBJECT_WUNLOCK(obj);
351 panic("tmpfs_alloc_node: type %p %d", nnode,
352 (int)nnode->tn_type);
356 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
357 nnode->tn_attached = true;
358 tmp->tm_nodes_inuse++;
367 * Destroys the node pointed to by node from the file system 'tmp'.
368 * If the node references a directory, no entries are allowed.
371 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
375 TMPFS_NODE_LOCK(node);
376 if (!tmpfs_free_node_locked(tmp, node, false)) {
377 TMPFS_NODE_UNLOCK(node);
383 tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node,
388 TMPFS_MP_ASSERT_LOCKED(tmp);
389 TMPFS_NODE_ASSERT_LOCKED(node);
390 KASSERT(node->tn_refcount > 0, ("node %p refcount zero", node));
393 if (node->tn_attached && (detach || node->tn_refcount == 0)) {
394 MPASS(tmp->tm_nodes_inuse > 0);
395 tmp->tm_nodes_inuse--;
396 LIST_REMOVE(node, tn_entries);
397 node->tn_attached = false;
399 if (node->tn_refcount > 0)
403 MPASS(node->tn_vnode == NULL);
404 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
406 TMPFS_NODE_UNLOCK(node);
409 switch (node->tn_type) {
422 free(node->tn_link, M_TMPFSNAME);
426 uobj = node->tn_reg.tn_aobj;
429 atomic_subtract_long(&tmp->tm_pages_used, uobj->size);
430 KASSERT((uobj->flags & OBJ_TMPFS) == 0,
431 ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
432 vm_object_deallocate(uobj);
437 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
440 uma_zfree_smr(tmpfs_node_pool, node);
446 static __inline uint32_t
447 tmpfs_dirent_hash(const char *name, u_int len)
451 hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
452 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
455 if (hash < TMPFS_DIRCOOKIE_MIN)
456 hash += TMPFS_DIRCOOKIE_MIN;
461 static __inline off_t
462 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
465 return (TMPFS_DIRCOOKIE_EOF);
467 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
469 return (de->td_cookie);
472 static __inline boolean_t
473 tmpfs_dirent_dup(struct tmpfs_dirent *de)
475 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
478 static __inline boolean_t
479 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
481 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
485 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
487 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
488 memcpy(de->ud.td_name, name, namelen);
489 de->td_namelen = namelen;
493 * Allocates a new directory entry for the node node with a name of name.
494 * The new directory entry is returned in *de.
496 * The link count of node is increased by one to reflect the new object
499 * Returns zero on success or an appropriate error code on failure.
502 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
503 const char *name, u_int len, struct tmpfs_dirent **de)
505 struct tmpfs_dirent *nde;
507 nde = uma_zalloc(tmpfs_dirent_pool, M_WAITOK);
510 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
511 tmpfs_dirent_init(nde, name, len);
523 * Frees a directory entry. It is the caller's responsibility to destroy
524 * the node referenced by it if needed.
526 * The link count of node is decreased by one to reflect the removal of an
527 * object that referenced it. This only happens if 'node_exists' is true;
528 * otherwise the function will not access the node referred to by the
529 * directory entry, as it may already have been released from the outside.
532 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
534 struct tmpfs_node *node;
538 MPASS(node->tn_links > 0);
541 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
542 free(de->ud.td_name, M_TMPFSNAME);
543 uma_zfree(tmpfs_dirent_pool, de);
547 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
550 ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
551 if (vp->v_type != VREG || obj == NULL)
554 VM_OBJECT_WLOCK(obj);
556 vm_object_clear_flag(obj, OBJ_TMPFS);
557 obj->un_pager.swp.swp_tmpfs = NULL;
558 if (vp->v_writecount < 0)
559 vp->v_writecount = 0;
561 VM_OBJECT_WUNLOCK(obj);
565 * Need to clear v_object for insmntque failure.
568 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
571 tmpfs_destroy_vobject(vp, vp->v_object);
574 vp->v_op = &dead_vnodeops;
580 * Allocates a new vnode for the node node or returns a new reference to
581 * an existing one if the node had already a vnode referencing it. The
582 * resulting locked vnode is returned in *vpp.
584 * Returns zero on success or an appropriate error code on failure.
587 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
592 struct tmpfs_mount *tm;
597 tm = VFS_TO_TMPFS(mp);
598 TMPFS_NODE_LOCK(node);
599 tmpfs_ref_node_locked(node);
601 TMPFS_NODE_ASSERT_LOCKED(node);
602 if ((vp = node->tn_vnode) != NULL) {
603 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
604 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
606 (lkflag & LK_NOWAIT) != 0)) {
607 TMPFS_NODE_UNLOCK(node);
612 if (VN_IS_DOOMED(vp)) {
613 node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
614 while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
615 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
621 TMPFS_NODE_UNLOCK(node);
622 error = vget_finish(vp, lkflag, vs);
623 if (error == ENOENT) {
624 TMPFS_NODE_LOCK(node);
633 * Make sure the vnode is still there after
634 * getting the interlock to avoid racing a free.
636 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
638 TMPFS_NODE_LOCK(node);
645 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
646 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
647 TMPFS_NODE_UNLOCK(node);
654 * otherwise lock the vp list while we call getnewvnode
655 * since that can block.
657 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
658 node->tn_vpstate |= TMPFS_VNODE_WANT;
659 error = msleep((caddr_t) &node->tn_vpstate,
660 TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0);
665 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
667 TMPFS_NODE_UNLOCK(node);
669 /* Get a new vnode and associate it with our node. */
670 error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ?
671 &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp);
676 /* lkflag is ignored, the lock is exclusive */
677 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
680 vp->v_type = node->tn_type;
682 /* Type-specific initialization. */
683 switch (node->tn_type) {
693 vp->v_op = &tmpfs_fifoop_entries;
696 object = node->tn_reg.tn_aobj;
697 VM_OBJECT_WLOCK(object);
699 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
700 vp->v_object = object;
701 object->un_pager.swp.swp_tmpfs = vp;
702 vm_object_set_flag(object, OBJ_TMPFS);
704 VM_OBJECT_WUNLOCK(object);
707 MPASS(node->tn_dir.tn_parent != NULL);
708 if (node->tn_dir.tn_parent == node)
709 vp->v_vflag |= VV_ROOT;
713 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
715 if (vp->v_type != VFIFO)
718 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
723 TMPFS_NODE_LOCK(node);
725 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
726 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
729 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
730 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
731 TMPFS_NODE_UNLOCK(node);
732 wakeup((caddr_t) &node->tn_vpstate);
734 TMPFS_NODE_UNLOCK(node);
741 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
742 TMPFS_NODE_LOCK(node);
743 MPASS(*vpp == node->tn_vnode);
744 TMPFS_NODE_UNLOCK(node);
747 tmpfs_free_node(tm, node);
753 * Destroys the association between the vnode vp and the node it
757 tmpfs_free_vp(struct vnode *vp)
759 struct tmpfs_node *node;
761 node = VP_TO_TMPFS_NODE(vp);
763 TMPFS_NODE_ASSERT_LOCKED(node);
764 node->tn_vnode = NULL;
765 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
766 wakeup(&node->tn_vnode);
767 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
772 * Allocates a new file of type 'type' and adds it to the parent directory
773 * 'dvp'; this addition is done using the component name given in 'cnp'.
774 * The ownership of the new file is automatically assigned based on the
775 * credentials of the caller (through 'cnp'), the group is set based on
776 * the parent directory and the mode is determined from the 'vap' argument.
777 * If successful, *vpp holds a vnode to the newly created file and zero
778 * is returned. Otherwise *vpp is NULL and the function returns an
779 * appropriate error code.
782 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
783 struct componentname *cnp, const char *target)
786 struct tmpfs_dirent *de;
787 struct tmpfs_mount *tmp;
788 struct tmpfs_node *dnode;
789 struct tmpfs_node *node;
790 struct tmpfs_node *parent;
792 ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file");
793 MPASS(cnp->cn_flags & HASBUF);
795 tmp = VFS_TO_TMPFS(dvp->v_mount);
796 dnode = VP_TO_TMPFS_DIR(dvp);
799 /* If the entry we are creating is a directory, we cannot overflow
800 * the number of links of its parent, because it will get a new
802 if (vap->va_type == VDIR) {
803 /* Ensure that we do not overflow the maximum number of links
804 * imposed by the system. */
805 MPASS(dnode->tn_links <= TMPFS_LINK_MAX);
806 if (dnode->tn_links == TMPFS_LINK_MAX) {
811 MPASS(parent != NULL);
815 /* Allocate a node that represents the new file. */
816 error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
817 cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
818 target, vap->va_rdev, &node);
822 /* Allocate a directory entry that points to the new file. */
823 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
826 tmpfs_free_node(tmp, node);
830 /* Allocate a vnode for the new file. */
831 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
833 tmpfs_free_dirent(tmp, de);
834 tmpfs_free_node(tmp, node);
838 /* Now that all required items are allocated, we can proceed to
839 * insert the new node into the directory, an operation that
841 if (cnp->cn_flags & ISWHITEOUT)
842 tmpfs_dir_whiteout_remove(dvp, cnp);
843 tmpfs_dir_attach(dvp, de);
847 struct tmpfs_dirent *
848 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
850 struct tmpfs_dirent *de;
852 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
854 if (de != NULL && tmpfs_dirent_duphead(de))
855 de = LIST_FIRST(&de->ud.td_duphead);
856 dc->tdc_current = de;
858 return (dc->tdc_current);
861 struct tmpfs_dirent *
862 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
864 struct tmpfs_dirent *de;
866 MPASS(dc->tdc_tree != NULL);
867 if (tmpfs_dirent_dup(dc->tdc_current)) {
868 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
869 if (dc->tdc_current != NULL)
870 return (dc->tdc_current);
872 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
873 &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
874 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
875 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
876 MPASS(dc->tdc_current != NULL);
879 return (dc->tdc_current);
882 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
883 static struct tmpfs_dirent *
884 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
886 struct tmpfs_dirent *de, dekey;
888 dekey.td_hash = hash;
889 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
893 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
894 static struct tmpfs_dirent *
895 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
896 struct tmpfs_dir_cursor *dc)
898 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
899 struct tmpfs_dirent *de, dekey;
901 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
903 if (cookie == node->tn_dir.tn_readdir_lastn &&
904 (de = node->tn_dir.tn_readdir_lastp) != NULL) {
905 /* Protect against possible race, tn_readdir_last[pn]
906 * may be updated with only shared vnode lock held. */
907 if (cookie == tmpfs_dirent_cookie(de))
911 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
912 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
913 uh.td_dup.index_entries) {
914 MPASS(tmpfs_dirent_dup(de));
915 if (de->td_cookie == cookie)
917 /* dupindex list is sorted. */
918 if (de->td_cookie < cookie) {
927 if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) {
930 dekey.td_hash = cookie;
931 /* Recover if direntry for cookie was removed */
932 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
935 dc->tdc_current = de;
936 if (de != NULL && tmpfs_dirent_duphead(de)) {
937 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
938 MPASS(dc->tdc_current != NULL);
940 return (dc->tdc_current);
944 dc->tdc_current = de;
945 if (de != NULL && tmpfs_dirent_dup(de))
946 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
948 return (dc->tdc_current);
952 * Looks for a directory entry in the directory represented by node.
953 * 'cnp' describes the name of the entry to look for. Note that the .
954 * and .. components are not allowed as they do not physically exist
955 * within directories.
957 * Returns a pointer to the entry when found, otherwise NULL.
959 struct tmpfs_dirent *
960 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
961 struct componentname *cnp)
963 struct tmpfs_dir_duphead *duphead;
964 struct tmpfs_dirent *de;
967 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
968 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
969 cnp->cn_nameptr[1] == '.')));
970 TMPFS_VALIDATE_DIR(node);
972 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
973 de = tmpfs_dir_xlookup_hash(node, hash);
974 if (de != NULL && tmpfs_dirent_duphead(de)) {
975 duphead = &de->ud.td_duphead;
976 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
977 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
981 } else if (de != NULL) {
982 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
986 if (de != NULL && f != NULL && de->td_node != f)
993 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
994 * list, allocate new cookie value.
997 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
998 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
1000 struct tmpfs_dir_duphead *dupindex;
1001 struct tmpfs_dirent *de, *pde;
1003 dupindex = &dnode->tn_dir.tn_dupindex;
1004 de = LIST_FIRST(dupindex);
1005 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
1007 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
1009 nde->td_cookie = de->td_cookie + 1;
1010 MPASS(tmpfs_dirent_dup(nde));
1011 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
1012 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1017 * Cookie numbers are near exhaustion. Scan dupindex list for unused
1018 * numbers. dupindex list is sorted in descending order. Keep it so
1019 * after inserting nde.
1023 de = LIST_NEXT(de, uh.td_dup.index_entries);
1024 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
1026 * Last element of the index doesn't have minimal cookie
1029 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
1030 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
1031 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1033 } else if (de == NULL) {
1035 * We are so lucky have 2^30 hash duplicates in single
1036 * directory :) Return largest possible cookie value.
1037 * It should be fine except possible issues with
1038 * VOP_READDIR restart.
1040 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
1041 LIST_INSERT_HEAD(dupindex, nde,
1042 uh.td_dup.index_entries);
1043 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1046 if (de->td_cookie + 1 == pde->td_cookie ||
1047 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
1048 continue; /* No hole or invalid cookie. */
1049 nde->td_cookie = de->td_cookie + 1;
1050 MPASS(tmpfs_dirent_dup(nde));
1051 MPASS(pde->td_cookie > nde->td_cookie);
1052 MPASS(nde->td_cookie > de->td_cookie);
1053 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
1054 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1060 * Attaches the directory entry de to the directory represented by vp.
1061 * Note that this does not change the link count of the node pointed by
1062 * the directory entry, as this is done by tmpfs_alloc_dirent.
1065 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
1067 struct tmpfs_node *dnode;
1068 struct tmpfs_dirent *xde, *nde;
1070 ASSERT_VOP_ELOCKED(vp, __func__);
1071 MPASS(de->td_namelen > 0);
1072 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
1073 MPASS(de->td_cookie == de->td_hash);
1075 dnode = VP_TO_TMPFS_DIR(vp);
1076 dnode->tn_dir.tn_readdir_lastn = 0;
1077 dnode->tn_dir.tn_readdir_lastp = NULL;
1079 MPASS(!tmpfs_dirent_dup(de));
1080 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1081 if (xde != NULL && tmpfs_dirent_duphead(xde))
1082 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1083 else if (xde != NULL) {
1085 * Allocate new duphead. Swap xde with duphead to avoid
1086 * adding/removing elements with the same hash.
1088 MPASS(!tmpfs_dirent_dup(xde));
1089 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
1091 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
1092 memcpy(nde, xde, sizeof(*xde));
1093 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
1094 LIST_INIT(&xde->ud.td_duphead);
1095 xde->td_namelen = 0;
1096 xde->td_node = NULL;
1097 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
1098 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1100 dnode->tn_size += sizeof(struct tmpfs_dirent);
1101 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1102 TMPFS_NODE_MODIFIED;
1107 * Detaches the directory entry de from the directory represented by vp.
1108 * Note that this does not change the link count of the node pointed by
1109 * the directory entry, as this is done by tmpfs_free_dirent.
1112 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
1114 struct tmpfs_mount *tmp;
1115 struct tmpfs_dir *head;
1116 struct tmpfs_node *dnode;
1117 struct tmpfs_dirent *xde;
1119 ASSERT_VOP_ELOCKED(vp, __func__);
1121 dnode = VP_TO_TMPFS_DIR(vp);
1122 head = &dnode->tn_dir.tn_dirhead;
1123 dnode->tn_dir.tn_readdir_lastn = 0;
1124 dnode->tn_dir.tn_readdir_lastp = NULL;
1126 if (tmpfs_dirent_dup(de)) {
1127 /* Remove duphead if de was last entry. */
1128 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1129 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1130 MPASS(tmpfs_dirent_duphead(xde));
1133 LIST_REMOVE(de, uh.td_dup.entries);
1134 LIST_REMOVE(de, uh.td_dup.index_entries);
1136 if (LIST_EMPTY(&xde->ud.td_duphead)) {
1137 RB_REMOVE(tmpfs_dir, head, xde);
1138 tmp = VFS_TO_TMPFS(vp->v_mount);
1139 MPASS(xde->td_node == NULL);
1140 tmpfs_free_dirent(tmp, xde);
1143 de->td_cookie = de->td_hash;
1145 RB_REMOVE(tmpfs_dir, head, de);
1147 dnode->tn_size -= sizeof(struct tmpfs_dirent);
1148 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1149 TMPFS_NODE_MODIFIED;
1154 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1156 struct tmpfs_dirent *de, *dde, *nde;
1158 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1159 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1160 /* Node may already be destroyed. */
1162 if (tmpfs_dirent_duphead(de)) {
1163 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1164 LIST_REMOVE(dde, uh.td_dup.entries);
1165 dde->td_node = NULL;
1166 tmpfs_free_dirent(tmp, dde);
1169 tmpfs_free_dirent(tmp, de);
1174 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
1175 * directory and returns it in the uio space. The function returns 0
1176 * on success, -1 if there was not enough space in the uio structure to
1177 * hold the directory entry or an appropriate error code if another
1181 tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1187 TMPFS_VALIDATE_DIR(node);
1188 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1190 dent.d_fileno = node->tn_id;
1191 dent.d_type = DT_DIR;
1193 dent.d_name[0] = '.';
1194 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1195 dirent_terminate(&dent);
1197 if (dent.d_reclen > uio->uio_resid)
1198 error = EJUSTRETURN;
1200 error = uiomove(&dent, dent.d_reclen, uio);
1202 tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1208 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
1209 * directory and returns it in the uio space. The function returns 0
1210 * on success, -1 if there was not enough space in the uio structure to
1211 * hold the directory entry or an appropriate error code if another
1215 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1218 struct tmpfs_node *parent;
1222 TMPFS_VALIDATE_DIR(node);
1223 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1226 * Return ENOENT if the current node is already removed.
1228 TMPFS_ASSERT_LOCKED(node);
1229 parent = node->tn_dir.tn_parent;
1233 TMPFS_NODE_LOCK(parent);
1234 dent.d_fileno = parent->tn_id;
1235 TMPFS_NODE_UNLOCK(parent);
1237 dent.d_type = DT_DIR;
1239 dent.d_name[0] = '.';
1240 dent.d_name[1] = '.';
1241 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1242 dirent_terminate(&dent);
1244 if (dent.d_reclen > uio->uio_resid)
1245 error = EJUSTRETURN;
1247 error = uiomove(&dent, dent.d_reclen, uio);
1249 tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1255 * Helper function for tmpfs_readdir. Returns as much directory entries
1256 * as can fit in the uio space. The read starts at uio->uio_offset.
1257 * The function returns 0 on success, -1 if there was not enough space
1258 * in the uio structure to hold the directory entry or an appropriate
1259 * error code if another error happens.
1262 tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node,
1263 struct uio *uio, int maxcookies, u_long *cookies, int *ncookies)
1265 struct tmpfs_dir_cursor dc;
1266 struct tmpfs_dirent *de;
1270 TMPFS_VALIDATE_DIR(node);
1275 * Lookup the node from the current offset. The starting offset of
1276 * 0 will lookup both '.' and '..', and then the first real entry,
1277 * or EOF if there are none. Then find all entries for the dir that
1278 * fit into the buffer. Once no more entries are found (de == NULL),
1279 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1282 switch (uio->uio_offset) {
1283 case TMPFS_DIRCOOKIE_DOT:
1284 error = tmpfs_dir_getdotdent(tm, node, uio);
1287 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1288 if (cookies != NULL)
1289 cookies[(*ncookies)++] = off = uio->uio_offset;
1291 case TMPFS_DIRCOOKIE_DOTDOT:
1292 error = tmpfs_dir_getdotdotdent(tm, node, uio);
1295 de = tmpfs_dir_first(node, &dc);
1296 uio->uio_offset = tmpfs_dirent_cookie(de);
1297 if (cookies != NULL)
1298 cookies[(*ncookies)++] = off = uio->uio_offset;
1303 case TMPFS_DIRCOOKIE_EOF:
1306 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1309 if (cookies != NULL)
1310 off = tmpfs_dirent_cookie(de);
1313 /* Read as much entries as possible; i.e., until we reach the end of
1314 * the directory or we exhaust uio space. */
1318 /* Create a dirent structure representing the current
1319 * tmpfs_node and fill it. */
1320 if (de->td_node == NULL) {
1324 d.d_fileno = de->td_node->tn_id;
1325 switch (de->td_node->tn_type) {
1355 panic("tmpfs_dir_getdents: type %p %d",
1356 de->td_node, (int)de->td_node->tn_type);
1359 d.d_namlen = de->td_namelen;
1360 MPASS(de->td_namelen < sizeof(d.d_name));
1361 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1362 d.d_reclen = GENERIC_DIRSIZ(&d);
1363 dirent_terminate(&d);
1365 /* Stop reading if the directory entry we are treating is
1366 * bigger than the amount of data that can be returned. */
1367 if (d.d_reclen > uio->uio_resid) {
1368 error = EJUSTRETURN;
1372 /* Copy the new dirent structure into the output buffer and
1373 * advance pointers. */
1374 error = uiomove(&d, d.d_reclen, uio);
1376 de = tmpfs_dir_next(node, &dc);
1377 if (cookies != NULL) {
1378 off = tmpfs_dirent_cookie(de);
1379 MPASS(*ncookies < maxcookies);
1380 cookies[(*ncookies)++] = off;
1383 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1385 /* Skip setting off when using cookies as it is already done above. */
1386 if (cookies == NULL)
1387 off = tmpfs_dirent_cookie(de);
1389 /* Update the offset and cache. */
1390 uio->uio_offset = off;
1391 node->tn_dir.tn_readdir_lastn = off;
1392 node->tn_dir.tn_readdir_lastp = de;
1394 tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1399 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1401 struct tmpfs_dirent *de;
1404 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1405 cnp->cn_nameptr, cnp->cn_namelen, &de);
1408 tmpfs_dir_attach(dvp, de);
1413 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1415 struct tmpfs_dirent *de;
1417 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1418 MPASS(de != NULL && de->td_node == NULL);
1419 tmpfs_dir_detach(dvp, de);
1420 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1424 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1425 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
1426 * 'newsize' must be positive.
1428 * Returns zero on success or an appropriate error code on failure.
1431 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1433 struct tmpfs_mount *tmp;
1434 struct tmpfs_node *node;
1437 vm_pindex_t idx, newpages, oldpages;
1441 MPASS(vp->v_type == VREG);
1442 MPASS(newsize >= 0);
1444 node = VP_TO_TMPFS_NODE(vp);
1445 uobj = node->tn_reg.tn_aobj;
1446 tmp = VFS_TO_TMPFS(vp->v_mount);
1449 * Convert the old and new sizes to the number of pages needed to
1450 * store them. It may happen that we do not need to do anything
1451 * because the last allocated page can accommodate the change on
1454 oldsize = node->tn_size;
1455 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1456 MPASS(oldpages == uobj->size);
1457 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1459 if (__predict_true(newpages == oldpages && newsize >= oldsize)) {
1460 node->tn_size = newsize;
1464 if (newpages > oldpages &&
1465 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1468 VM_OBJECT_WLOCK(uobj);
1469 if (newsize < oldsize) {
1471 * Zero the truncated part of the last page.
1473 base = newsize & PAGE_MASK;
1475 idx = OFF_TO_IDX(newsize);
1477 m = vm_page_grab(uobj, idx, VM_ALLOC_NOCREAT);
1479 MPASS(vm_page_all_valid(m));
1480 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1481 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL |
1485 vm_object_pip_add(uobj, 1);
1486 VM_OBJECT_WUNLOCK(uobj);
1487 rv = vm_pager_get_pages(uobj, &m, 1, NULL,
1489 VM_OBJECT_WLOCK(uobj);
1490 vm_object_pip_wakeup(uobj);
1491 if (rv == VM_PAGER_OK) {
1493 * Since the page was not resident,
1494 * and therefore not recently
1495 * accessed, immediately enqueue it
1496 * for asynchronous laundering. The
1497 * current operation is not regarded
1506 VM_OBJECT_WUNLOCK(uobj);
1512 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1513 vm_page_set_dirty(m);
1519 * Release any swap space and free any whole pages.
1521 if (newpages < oldpages)
1522 vm_object_page_remove(uobj, newpages, 0, 0);
1524 uobj->size = newpages;
1525 VM_OBJECT_WUNLOCK(uobj);
1527 atomic_add_long(&tmp->tm_pages_used, newpages - oldpages);
1529 node->tn_size = newsize;
1534 tmpfs_check_mtime(struct vnode *vp)
1536 struct tmpfs_node *node;
1537 struct vm_object *obj;
1539 ASSERT_VOP_ELOCKED(vp, "check_mtime");
1540 if (vp->v_type != VREG)
1543 KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
1544 (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
1546 if (obj->generation != obj->cleangeneration) {
1547 VM_OBJECT_WLOCK(obj);
1548 if (obj->generation != obj->cleangeneration) {
1549 obj->cleangeneration = obj->generation;
1550 node = VP_TO_TMPFS_NODE(vp);
1551 node->tn_status |= TMPFS_NODE_MODIFIED |
1554 VM_OBJECT_WUNLOCK(obj);
1559 * Change flags of the given vnode.
1560 * Caller should execute tmpfs_update on vp after a successful execution.
1561 * The vnode must be locked on entry and remain locked on exit.
1564 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1568 struct tmpfs_node *node;
1570 ASSERT_VOP_ELOCKED(vp, "chflags");
1572 node = VP_TO_TMPFS_NODE(vp);
1574 if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1575 UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1576 UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1577 UF_SPARSE | UF_SYSTEM)) != 0)
1578 return (EOPNOTSUPP);
1580 /* Disallow this operation if the file system is mounted read-only. */
1581 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1585 * Callers may only modify the file flags on objects they
1586 * have VADMIN rights for.
1588 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1591 * Unprivileged processes are not permitted to unset system
1592 * flags, or modify flags if any system flags are set.
1594 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
1595 if (node->tn_flags &
1596 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1597 error = securelevel_gt(cred, 0);
1602 if (node->tn_flags &
1603 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1604 ((flags ^ node->tn_flags) & SF_SETTABLE))
1607 node->tn_flags = flags;
1608 node->tn_status |= TMPFS_NODE_CHANGED;
1610 ASSERT_VOP_ELOCKED(vp, "chflags2");
1616 * Change access mode on the given vnode.
1617 * Caller should execute tmpfs_update on vp after a successful execution.
1618 * The vnode must be locked on entry and remain locked on exit.
1621 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1624 struct tmpfs_node *node;
1627 ASSERT_VOP_ELOCKED(vp, "chmod");
1628 ASSERT_VOP_IN_SEQC(vp);
1630 node = VP_TO_TMPFS_NODE(vp);
1632 /* Disallow this operation if the file system is mounted read-only. */
1633 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1636 /* Immutable or append-only files cannot be modified, either. */
1637 if (node->tn_flags & (IMMUTABLE | APPEND))
1641 * To modify the permissions on a file, must possess VADMIN
1644 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1648 * Privileged processes may set the sticky bit on non-directories,
1649 * as well as set the setgid bit on a file with a group that the
1650 * process is not a member of.
1652 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1653 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1656 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1657 error = priv_check_cred(cred, PRIV_VFS_SETGID);
1662 newmode = node->tn_mode & ~ALLPERMS;
1663 newmode |= mode & ALLPERMS;
1664 atomic_store_short(&node->tn_mode, newmode);
1666 node->tn_status |= TMPFS_NODE_CHANGED;
1668 ASSERT_VOP_ELOCKED(vp, "chmod2");
1674 * Change ownership of the given vnode. At least one of uid or gid must
1675 * be different than VNOVAL. If one is set to that value, the attribute
1677 * Caller should execute tmpfs_update on vp after a successful execution.
1678 * The vnode must be locked on entry and remain locked on exit.
1681 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1685 struct tmpfs_node *node;
1690 ASSERT_VOP_ELOCKED(vp, "chown");
1691 ASSERT_VOP_IN_SEQC(vp);
1693 node = VP_TO_TMPFS_NODE(vp);
1695 /* Assign default values if they are unknown. */
1696 MPASS(uid != VNOVAL || gid != VNOVAL);
1701 MPASS(uid != VNOVAL && gid != VNOVAL);
1703 /* Disallow this operation if the file system is mounted read-only. */
1704 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1707 /* Immutable or append-only files cannot be modified, either. */
1708 if (node->tn_flags & (IMMUTABLE | APPEND))
1712 * To modify the ownership of a file, must possess VADMIN for that
1715 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1719 * To change the owner of a file, or change the group of a file to a
1720 * group of which we are not a member, the caller must have
1723 if ((uid != node->tn_uid ||
1724 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1725 (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
1728 ogid = node->tn_gid;
1729 ouid = node->tn_uid;
1734 node->tn_status |= TMPFS_NODE_CHANGED;
1736 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1737 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
1738 newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
1739 atomic_store_short(&node->tn_mode, newmode);
1743 ASSERT_VOP_ELOCKED(vp, "chown2");
1749 * Change size of the given vnode.
1750 * Caller should execute tmpfs_update on vp after a successful execution.
1751 * The vnode must be locked on entry and remain locked on exit.
1754 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1758 struct tmpfs_node *node;
1760 ASSERT_VOP_ELOCKED(vp, "chsize");
1762 node = VP_TO_TMPFS_NODE(vp);
1764 /* Decide whether this is a valid operation based on the file type. */
1766 switch (vp->v_type) {
1771 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1780 /* Allow modifications of special files even if in the file
1781 * system is mounted read-only (we are not modifying the
1782 * files themselves, but the objects they represent). */
1786 /* Anything else is unsupported. */
1790 /* Immutable or append-only files cannot be modified, either. */
1791 if (node->tn_flags & (IMMUTABLE | APPEND))
1794 error = tmpfs_truncate(vp, size);
1795 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1796 * for us, as will update tn_status; no need to do that here. */
1798 ASSERT_VOP_ELOCKED(vp, "chsize2");
1804 * Change access and modification times of the given vnode.
1805 * Caller should execute tmpfs_update on vp after a successful execution.
1806 * The vnode must be locked on entry and remain locked on exit.
1809 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
1810 struct ucred *cred, struct thread *l)
1813 struct tmpfs_node *node;
1815 ASSERT_VOP_ELOCKED(vp, "chtimes");
1817 node = VP_TO_TMPFS_NODE(vp);
1819 /* Disallow this operation if the file system is mounted read-only. */
1820 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1823 /* Immutable or append-only files cannot be modified, either. */
1824 if (node->tn_flags & (IMMUTABLE | APPEND))
1827 error = vn_utimes_perm(vp, vap, cred, l);
1831 if (vap->va_atime.tv_sec != VNOVAL)
1832 node->tn_status |= TMPFS_NODE_ACCESSED;
1834 if (vap->va_mtime.tv_sec != VNOVAL)
1835 node->tn_status |= TMPFS_NODE_MODIFIED;
1837 if (vap->va_birthtime.tv_sec != VNOVAL)
1838 node->tn_status |= TMPFS_NODE_MODIFIED;
1840 tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
1842 if (vap->va_birthtime.tv_sec != VNOVAL)
1843 node->tn_birthtime = vap->va_birthtime;
1844 ASSERT_VOP_ELOCKED(vp, "chtimes2");
1850 tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status)
1853 if ((node->tn_status & status) == status || tm->tm_ronly)
1855 TMPFS_NODE_LOCK(node);
1856 node->tn_status |= status;
1857 TMPFS_NODE_UNLOCK(node);
1860 /* Sync timestamps */
1862 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1863 const struct timespec *mod)
1865 struct tmpfs_node *node;
1866 struct timespec now;
1868 ASSERT_VOP_LOCKED(vp, "tmpfs_itimes");
1869 node = VP_TO_TMPFS_NODE(vp);
1871 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1872 TMPFS_NODE_CHANGED)) == 0)
1875 vfs_timestamp(&now);
1876 TMPFS_NODE_LOCK(node);
1877 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1880 node->tn_atime = *acc;
1882 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1885 node->tn_mtime = *mod;
1887 if (node->tn_status & TMPFS_NODE_CHANGED)
1888 node->tn_ctime = now;
1889 node->tn_status &= ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1890 TMPFS_NODE_CHANGED);
1891 TMPFS_NODE_UNLOCK(node);
1893 /* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */
1894 random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME);
1898 tmpfs_truncate(struct vnode *vp, off_t length)
1901 struct tmpfs_node *node;
1903 node = VP_TO_TMPFS_NODE(vp);
1910 if (node->tn_size == length) {
1915 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1918 error = tmpfs_reg_resize(vp, length, FALSE);
1920 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1929 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1931 if (a->td_hash > b->td_hash)
1933 else if (a->td_hash < b->td_hash)
1938 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);