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 + vm_cnt.v_free_count + vm_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))
139 /* --------------------------------------------------------------------- */
142 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
143 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
144 * using the credentials of the process 'p'.
146 * If the node type is set to 'VDIR', then the parent parameter must point
147 * to the parent directory of the node being created. It may only be NULL
148 * while allocating the root node.
150 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
151 * specifies the device the node represents.
153 * If the node type is set to 'VLNK', then the parameter target specifies
154 * the file name of the target file for the symbolic link that is being
157 * Note that new nodes are retrieved from the available list if it has
158 * items or, if it is empty, from the node pool as long as there is enough
159 * space to create them.
161 * Returns zero on success or an appropriate error code on failure.
164 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
165 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
166 char *target, dev_t rdev, struct tmpfs_node **node)
168 struct tmpfs_node *nnode;
171 /* If the root directory of the 'tmp' file system is not yet
172 * allocated, this must be the request to do it. */
173 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
175 MPASS(IFF(type == VLNK, target != NULL));
176 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
178 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
180 if (tmpfs_pages_check_avail(tmp, 1) == 0)
183 nnode = (struct tmpfs_node *)uma_zalloc_arg(
184 tmp->tm_node_pool, tmp, M_WAITOK);
186 /* Generic initialization. */
187 nnode->tn_type = type;
188 vfs_timestamp(&nnode->tn_atime);
189 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
193 nnode->tn_mode = mode;
194 nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
196 /* Type-specific initialization. */
197 switch (nnode->tn_type) {
200 nnode->tn_rdev = rdev;
204 RB_INIT(&nnode->tn_dir.tn_dirhead);
205 LIST_INIT(&nnode->tn_dir.tn_dupindex);
206 MPASS(parent != nnode);
207 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
208 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
209 nnode->tn_dir.tn_readdir_lastn = 0;
210 nnode->tn_dir.tn_readdir_lastp = NULL;
212 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
213 nnode->tn_dir.tn_parent->tn_links++;
214 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
223 MPASS(strlen(target) < MAXPATHLEN);
224 nnode->tn_size = strlen(target);
225 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
227 memcpy(nnode->tn_link, target, nnode->tn_size);
231 obj = nnode->tn_reg.tn_aobj =
232 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
233 NULL /* XXXKIB - tmpfs needs swap reservation */);
234 VM_OBJECT_WLOCK(obj);
235 /* OBJ_TMPFS is set together with the setting of vp->v_object */
236 vm_object_set_flag(obj, OBJ_NOSPLIT);
237 vm_object_clear_flag(obj, OBJ_ONEMAPPING);
238 VM_OBJECT_WUNLOCK(obj);
242 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
246 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
247 tmp->tm_nodes_inuse++;
254 /* --------------------------------------------------------------------- */
257 * Destroys the node pointed to by node from the file system 'tmp'.
258 * If the node does not belong to the given mount point, the results are
261 * If the node references a directory; no entries are allowed because
262 * their removal could need a recursive algorithm, something forbidden in
263 * kernel space. Furthermore, there is not need to provide such
264 * functionality (recursive removal) because the only primitives offered
265 * to the user are the removal of empty directories and the deletion of
268 * Note that nodes are not really deleted; in fact, when a node has been
269 * allocated, it cannot be deleted during the whole life of the file
270 * system. Instead, they are moved to the available list and remain there
274 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
279 TMPFS_NODE_LOCK(node);
280 MPASS(node->tn_vnode == NULL);
281 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
282 TMPFS_NODE_UNLOCK(node);
286 LIST_REMOVE(node, tn_entries);
287 tmp->tm_nodes_inuse--;
290 switch (node->tn_type) {
292 /* Do not do anything. VNON is provided to let the
293 * allocation routine clean itself easily by avoiding
294 * duplicating code in it. */
308 free(node->tn_link, M_TMPFSNAME);
312 uobj = node->tn_reg.tn_aobj;
315 tmp->tm_pages_used -= uobj->size;
317 KASSERT((uobj->flags & OBJ_TMPFS) == 0,
318 ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
319 vm_object_deallocate(uobj);
324 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
327 free_unr(tmp->tm_ino_unr, node->tn_id);
328 uma_zfree(tmp->tm_node_pool, node);
331 /* --------------------------------------------------------------------- */
333 static __inline uint32_t
334 tmpfs_dirent_hash(const char *name, u_int len)
338 hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
339 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
342 if (hash < TMPFS_DIRCOOKIE_MIN)
343 hash += TMPFS_DIRCOOKIE_MIN;
348 static __inline off_t
349 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
352 return (TMPFS_DIRCOOKIE_EOF);
354 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
356 return (de->td_cookie);
359 static __inline boolean_t
360 tmpfs_dirent_dup(struct tmpfs_dirent *de)
362 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
365 static __inline boolean_t
366 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
368 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
372 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
374 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
375 memcpy(de->ud.td_name, name, namelen);
376 de->td_namelen = namelen;
380 * Allocates a new directory entry for the node node with a name of name.
381 * The new directory entry is returned in *de.
383 * The link count of node is increased by one to reflect the new object
386 * Returns zero on success or an appropriate error code on failure.
389 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
390 const char *name, u_int len, struct tmpfs_dirent **de)
392 struct tmpfs_dirent *nde;
394 nde = uma_zalloc(tmp->tm_dirent_pool, M_WAITOK);
397 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
398 tmpfs_dirent_init(nde, name, len);
409 /* --------------------------------------------------------------------- */
412 * Frees a directory entry. It is the caller's responsibility to destroy
413 * the node referenced by it if needed.
415 * The link count of node is decreased by one to reflect the removal of an
416 * object that referenced it. This only happens if 'node_exists' is true;
417 * otherwise the function will not access the node referred to by the
418 * directory entry, as it may already have been released from the outside.
421 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
423 struct tmpfs_node *node;
427 MPASS(node->tn_links > 0);
430 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
431 free(de->ud.td_name, M_TMPFSNAME);
432 uma_zfree(tmp->tm_dirent_pool, de);
435 /* --------------------------------------------------------------------- */
438 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
441 if (vp->v_type != VREG || obj == NULL)
444 VM_OBJECT_WLOCK(obj);
446 vm_object_clear_flag(obj, OBJ_TMPFS);
447 obj->un_pager.swp.swp_tmpfs = NULL;
449 VM_OBJECT_WUNLOCK(obj);
453 * Need to clear v_object for insmntque failure.
456 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
459 tmpfs_destroy_vobject(vp, vp->v_object);
462 vp->v_op = &dead_vnodeops;
468 * Allocates a new vnode for the node node or returns a new reference to
469 * an existing one if the node had already a vnode referencing it. The
470 * resulting locked vnode is returned in *vpp.
472 * Returns zero on success or an appropriate error code on failure.
475 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
484 TMPFS_NODE_LOCK(node);
486 if ((vp = node->tn_vnode) != NULL) {
487 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
489 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
490 ((vp->v_iflag & VI_DOOMED) != 0 &&
491 (lkflag & LK_NOWAIT) != 0)) {
493 TMPFS_NODE_UNLOCK(node);
498 if ((vp->v_iflag & VI_DOOMED) != 0) {
500 node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
501 while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
502 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
507 TMPFS_NODE_UNLOCK(node);
508 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
517 * Make sure the vnode is still there after
518 * getting the interlock to avoid racing a free.
520 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
528 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
529 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
530 TMPFS_NODE_UNLOCK(node);
537 * otherwise lock the vp list while we call getnewvnode
538 * since that can block.
540 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
541 node->tn_vpstate |= TMPFS_VNODE_WANT;
542 error = msleep((caddr_t) &node->tn_vpstate,
543 TMPFS_NODE_MTX(node), PDROP | PCATCH,
544 "tmpfs_alloc_vp", 0);
550 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
552 TMPFS_NODE_UNLOCK(node);
554 /* Get a new vnode and associate it with our node. */
555 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
560 (void) vn_lock(vp, lkflag | LK_RETRY);
563 vp->v_type = node->tn_type;
565 /* Type-specific initialization. */
566 switch (node->tn_type) {
576 vp->v_op = &tmpfs_fifoop_entries;
579 object = node->tn_reg.tn_aobj;
580 VM_OBJECT_WLOCK(object);
582 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
583 vp->v_object = object;
584 object->un_pager.swp.swp_tmpfs = vp;
585 vm_object_set_flag(object, OBJ_TMPFS);
587 VM_OBJECT_WUNLOCK(object);
590 MPASS(node->tn_dir.tn_parent != NULL);
591 if (node->tn_dir.tn_parent == node)
592 vp->v_vflag |= VV_ROOT;
596 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
598 if (vp->v_type != VFIFO)
601 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
606 TMPFS_NODE_LOCK(node);
608 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
609 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
612 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
613 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
614 TMPFS_NODE_UNLOCK(node);
615 wakeup((caddr_t) &node->tn_vpstate);
617 TMPFS_NODE_UNLOCK(node);
624 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
625 TMPFS_NODE_LOCK(node);
626 MPASS(*vpp == node->tn_vnode);
627 TMPFS_NODE_UNLOCK(node);
634 /* --------------------------------------------------------------------- */
637 * Destroys the association between the vnode vp and the node it
641 tmpfs_free_vp(struct vnode *vp)
643 struct tmpfs_node *node;
645 node = VP_TO_TMPFS_NODE(vp);
647 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
648 node->tn_vnode = NULL;
649 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
650 wakeup(&node->tn_vnode);
651 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
655 /* --------------------------------------------------------------------- */
658 * Allocates a new file of type 'type' and adds it to the parent directory
659 * 'dvp'; this addition is done using the component name given in 'cnp'.
660 * The ownership of the new file is automatically assigned based on the
661 * credentials of the caller (through 'cnp'), the group is set based on
662 * the parent directory and the mode is determined from the 'vap' argument.
663 * If successful, *vpp holds a vnode to the newly created file and zero
664 * is returned. Otherwise *vpp is NULL and the function returns an
665 * appropriate error code.
668 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
669 struct componentname *cnp, char *target)
672 struct tmpfs_dirent *de;
673 struct tmpfs_mount *tmp;
674 struct tmpfs_node *dnode;
675 struct tmpfs_node *node;
676 struct tmpfs_node *parent;
678 MPASS(VOP_ISLOCKED(dvp));
679 MPASS(cnp->cn_flags & HASBUF);
681 tmp = VFS_TO_TMPFS(dvp->v_mount);
682 dnode = VP_TO_TMPFS_DIR(dvp);
685 /* If the entry we are creating is a directory, we cannot overflow
686 * the number of links of its parent, because it will get a new
688 if (vap->va_type == VDIR) {
689 /* Ensure that we do not overflow the maximum number of links
690 * imposed by the system. */
691 MPASS(dnode->tn_links <= LINK_MAX);
692 if (dnode->tn_links == LINK_MAX) {
698 MPASS(parent != NULL);
702 /* Allocate a node that represents the new file. */
703 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
704 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
708 /* Allocate a directory entry that points to the new file. */
709 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
712 tmpfs_free_node(tmp, node);
716 /* Allocate a vnode for the new file. */
717 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
719 tmpfs_free_dirent(tmp, de);
720 tmpfs_free_node(tmp, node);
724 /* Now that all required items are allocated, we can proceed to
725 * insert the new node into the directory, an operation that
727 if (cnp->cn_flags & ISWHITEOUT)
728 tmpfs_dir_whiteout_remove(dvp, cnp);
729 tmpfs_dir_attach(dvp, de);
736 /* --------------------------------------------------------------------- */
738 static struct tmpfs_dirent *
739 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
741 struct tmpfs_dirent *de;
743 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
745 if (de != NULL && tmpfs_dirent_duphead(de))
746 de = LIST_FIRST(&de->ud.td_duphead);
747 dc->tdc_current = de;
749 return (dc->tdc_current);
752 static struct tmpfs_dirent *
753 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
755 struct tmpfs_dirent *de;
757 MPASS(dc->tdc_tree != NULL);
758 if (tmpfs_dirent_dup(dc->tdc_current)) {
759 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
760 if (dc->tdc_current != NULL)
761 return (dc->tdc_current);
763 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
764 &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
765 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
766 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
767 MPASS(dc->tdc_current != NULL);
770 return (dc->tdc_current);
773 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
774 static struct tmpfs_dirent *
775 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
777 struct tmpfs_dirent *de, dekey;
779 dekey.td_hash = hash;
780 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
784 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
785 static struct tmpfs_dirent *
786 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
787 struct tmpfs_dir_cursor *dc)
789 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
790 struct tmpfs_dirent *de, dekey;
792 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
794 if (cookie == node->tn_dir.tn_readdir_lastn &&
795 (de = node->tn_dir.tn_readdir_lastp) != NULL) {
796 /* Protect against possible race, tn_readdir_last[pn]
797 * may be updated with only shared vnode lock held. */
798 if (cookie == tmpfs_dirent_cookie(de))
802 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
803 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
804 uh.td_dup.index_entries) {
805 MPASS(tmpfs_dirent_dup(de));
806 if (de->td_cookie == cookie)
808 /* dupindex list is sorted. */
809 if (de->td_cookie < cookie) {
818 MPASS((cookie & TMPFS_DIRCOOKIE_MASK) == cookie);
819 dekey.td_hash = cookie;
820 /* Recover if direntry for cookie was removed */
821 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
823 dc->tdc_current = de;
824 if (de != NULL && tmpfs_dirent_duphead(de)) {
825 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
826 MPASS(dc->tdc_current != NULL);
828 return (dc->tdc_current);
832 dc->tdc_current = de;
833 if (de != NULL && tmpfs_dirent_dup(de))
834 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
836 return (dc->tdc_current);
840 * Looks for a directory entry in the directory represented by node.
841 * 'cnp' describes the name of the entry to look for. Note that the .
842 * and .. components are not allowed as they do not physically exist
843 * within directories.
845 * Returns a pointer to the entry when found, otherwise NULL.
847 struct tmpfs_dirent *
848 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
849 struct componentname *cnp)
851 struct tmpfs_dir_duphead *duphead;
852 struct tmpfs_dirent *de;
855 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
856 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
857 cnp->cn_nameptr[1] == '.')));
858 TMPFS_VALIDATE_DIR(node);
860 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
861 de = tmpfs_dir_xlookup_hash(node, hash);
862 if (de != NULL && tmpfs_dirent_duphead(de)) {
863 duphead = &de->ud.td_duphead;
864 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
865 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
869 } else if (de != NULL) {
870 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
874 if (de != NULL && f != NULL && de->td_node != f)
881 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
882 * list, allocate new cookie value.
885 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
886 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
888 struct tmpfs_dir_duphead *dupindex;
889 struct tmpfs_dirent *de, *pde;
891 dupindex = &dnode->tn_dir.tn_dupindex;
892 de = LIST_FIRST(dupindex);
893 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
895 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
897 nde->td_cookie = de->td_cookie + 1;
898 MPASS(tmpfs_dirent_dup(nde));
899 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
900 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
905 * Cookie numbers are near exhaustion. Scan dupindex list for unused
906 * numbers. dupindex list is sorted in descending order. Keep it so
907 * after inserting nde.
911 de = LIST_NEXT(de, uh.td_dup.index_entries);
912 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
914 * Last element of the index doesn't have minimal cookie
917 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
918 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
919 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
921 } else if (de == NULL) {
923 * We are so lucky have 2^30 hash duplicates in single
924 * directory :) Return largest possible cookie value.
925 * It should be fine except possible issues with
926 * VOP_READDIR restart.
928 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
929 LIST_INSERT_HEAD(dupindex, nde,
930 uh.td_dup.index_entries);
931 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
934 if (de->td_cookie + 1 == pde->td_cookie ||
935 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
936 continue; /* No hole or invalid cookie. */
937 nde->td_cookie = de->td_cookie + 1;
938 MPASS(tmpfs_dirent_dup(nde));
939 MPASS(pde->td_cookie > nde->td_cookie);
940 MPASS(nde->td_cookie > de->td_cookie);
941 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
942 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
948 * Attaches the directory entry de to the directory represented by vp.
949 * Note that this does not change the link count of the node pointed by
950 * the directory entry, as this is done by tmpfs_alloc_dirent.
953 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
955 struct tmpfs_node *dnode;
956 struct tmpfs_dirent *xde, *nde;
958 ASSERT_VOP_ELOCKED(vp, __func__);
959 MPASS(de->td_namelen > 0);
960 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
961 MPASS(de->td_cookie == de->td_hash);
963 dnode = VP_TO_TMPFS_DIR(vp);
964 dnode->tn_dir.tn_readdir_lastn = 0;
965 dnode->tn_dir.tn_readdir_lastp = NULL;
967 MPASS(!tmpfs_dirent_dup(de));
968 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
969 if (xde != NULL && tmpfs_dirent_duphead(xde))
970 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
971 else if (xde != NULL) {
973 * Allocate new duphead. Swap xde with duphead to avoid
974 * adding/removing elements with the same hash.
976 MPASS(!tmpfs_dirent_dup(xde));
977 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
979 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
980 memcpy(nde, xde, sizeof(*xde));
981 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
982 LIST_INIT(&xde->ud.td_duphead);
985 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
986 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
988 dnode->tn_size += sizeof(struct tmpfs_dirent);
989 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
993 /* --------------------------------------------------------------------- */
996 * Detaches the directory entry de from the directory represented by vp.
997 * Note that this does not change the link count of the node pointed by
998 * the directory entry, as this is done by tmpfs_free_dirent.
1001 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
1003 struct tmpfs_mount *tmp;
1004 struct tmpfs_dir *head;
1005 struct tmpfs_node *dnode;
1006 struct tmpfs_dirent *xde;
1008 ASSERT_VOP_ELOCKED(vp, __func__);
1010 dnode = VP_TO_TMPFS_DIR(vp);
1011 head = &dnode->tn_dir.tn_dirhead;
1012 dnode->tn_dir.tn_readdir_lastn = 0;
1013 dnode->tn_dir.tn_readdir_lastp = NULL;
1015 if (tmpfs_dirent_dup(de)) {
1016 /* Remove duphead if de was last entry. */
1017 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1018 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1019 MPASS(tmpfs_dirent_duphead(xde));
1022 LIST_REMOVE(de, uh.td_dup.entries);
1023 LIST_REMOVE(de, uh.td_dup.index_entries);
1025 if (LIST_EMPTY(&xde->ud.td_duphead)) {
1026 RB_REMOVE(tmpfs_dir, head, xde);
1027 tmp = VFS_TO_TMPFS(vp->v_mount);
1028 MPASS(xde->td_node == NULL);
1029 tmpfs_free_dirent(tmp, xde);
1033 RB_REMOVE(tmpfs_dir, head, de);
1035 dnode->tn_size -= sizeof(struct tmpfs_dirent);
1036 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1037 TMPFS_NODE_MODIFIED;
1041 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1043 struct tmpfs_dirent *de, *dde, *nde;
1045 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1046 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1047 /* Node may already be destroyed. */
1049 if (tmpfs_dirent_duphead(de)) {
1050 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1051 LIST_REMOVE(dde, uh.td_dup.entries);
1052 dde->td_node = NULL;
1053 tmpfs_free_dirent(tmp, dde);
1056 tmpfs_free_dirent(tmp, de);
1060 /* --------------------------------------------------------------------- */
1063 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
1064 * directory and returns it in the uio space. The function returns 0
1065 * on success, -1 if there was not enough space in the uio structure to
1066 * hold the directory entry or an appropriate error code if another
1070 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
1075 TMPFS_VALIDATE_DIR(node);
1076 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1078 dent.d_fileno = node->tn_id;
1079 dent.d_type = DT_DIR;
1081 dent.d_name[0] = '.';
1082 dent.d_name[1] = '\0';
1083 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1085 if (dent.d_reclen > uio->uio_resid)
1086 error = EJUSTRETURN;
1088 error = uiomove(&dent, dent.d_reclen, uio);
1090 node->tn_status |= TMPFS_NODE_ACCESSED;
1095 /* --------------------------------------------------------------------- */
1098 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
1099 * directory and returns it in the uio space. The function returns 0
1100 * on success, -1 if there was not enough space in the uio structure to
1101 * hold the directory entry or an appropriate error code if another
1105 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
1110 TMPFS_VALIDATE_DIR(node);
1111 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1114 * Return ENOENT if the current node is already removed.
1116 TMPFS_ASSERT_LOCKED(node);
1117 if (node->tn_dir.tn_parent == NULL) {
1121 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
1122 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
1123 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
1125 dent.d_type = DT_DIR;
1127 dent.d_name[0] = '.';
1128 dent.d_name[1] = '.';
1129 dent.d_name[2] = '\0';
1130 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1132 if (dent.d_reclen > uio->uio_resid)
1133 error = EJUSTRETURN;
1135 error = uiomove(&dent, dent.d_reclen, uio);
1137 node->tn_status |= TMPFS_NODE_ACCESSED;
1142 /* --------------------------------------------------------------------- */
1145 * Helper function for tmpfs_readdir. Returns as much directory entries
1146 * as can fit in the uio space. The read starts at uio->uio_offset.
1147 * The function returns 0 on success, -1 if there was not enough space
1148 * in the uio structure to hold the directory entry or an appropriate
1149 * error code if another error happens.
1152 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, int maxcookies,
1153 u_long *cookies, int *ncookies)
1155 struct tmpfs_dir_cursor dc;
1156 struct tmpfs_dirent *de;
1160 TMPFS_VALIDATE_DIR(node);
1165 * Lookup the node from the current offset. The starting offset of
1166 * 0 will lookup both '.' and '..', and then the first real entry,
1167 * or EOF if there are none. Then find all entries for the dir that
1168 * fit into the buffer. Once no more entries are found (de == NULL),
1169 * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1172 switch (uio->uio_offset) {
1173 case TMPFS_DIRCOOKIE_DOT:
1174 error = tmpfs_dir_getdotdent(node, uio);
1177 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1178 if (cookies != NULL)
1179 cookies[(*ncookies)++] = off = uio->uio_offset;
1181 case TMPFS_DIRCOOKIE_DOTDOT:
1182 error = tmpfs_dir_getdotdotdent(node, uio);
1185 de = tmpfs_dir_first(node, &dc);
1186 uio->uio_offset = tmpfs_dirent_cookie(de);
1187 if (cookies != NULL)
1188 cookies[(*ncookies)++] = off = uio->uio_offset;
1193 case TMPFS_DIRCOOKIE_EOF:
1196 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1199 if (cookies != NULL)
1200 off = tmpfs_dirent_cookie(de);
1203 /* Read as much entries as possible; i.e., until we reach the end of
1204 * the directory or we exhaust uio space. */
1208 /* Create a dirent structure representing the current
1209 * tmpfs_node and fill it. */
1210 if (de->td_node == NULL) {
1214 d.d_fileno = de->td_node->tn_id;
1215 switch (de->td_node->tn_type) {
1245 panic("tmpfs_dir_getdents: type %p %d",
1246 de->td_node, (int)de->td_node->tn_type);
1249 d.d_namlen = de->td_namelen;
1250 MPASS(de->td_namelen < sizeof(d.d_name));
1251 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1252 d.d_name[de->td_namelen] = '\0';
1253 d.d_reclen = GENERIC_DIRSIZ(&d);
1255 /* Stop reading if the directory entry we are treating is
1256 * bigger than the amount of data that can be returned. */
1257 if (d.d_reclen > uio->uio_resid) {
1258 error = EJUSTRETURN;
1262 /* Copy the new dirent structure into the output buffer and
1263 * advance pointers. */
1264 error = uiomove(&d, d.d_reclen, uio);
1266 de = tmpfs_dir_next(node, &dc);
1267 if (cookies != NULL) {
1268 off = tmpfs_dirent_cookie(de);
1269 MPASS(*ncookies < maxcookies);
1270 cookies[(*ncookies)++] = off;
1273 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1275 /* Skip setting off when using cookies as it is already done above. */
1276 if (cookies == NULL)
1277 off = tmpfs_dirent_cookie(de);
1279 /* Update the offset and cache. */
1280 uio->uio_offset = off;
1281 node->tn_dir.tn_readdir_lastn = off;
1282 node->tn_dir.tn_readdir_lastp = de;
1284 node->tn_status |= TMPFS_NODE_ACCESSED;
1289 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1291 struct tmpfs_dirent *de;
1294 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1295 cnp->cn_nameptr, cnp->cn_namelen, &de);
1298 tmpfs_dir_attach(dvp, de);
1303 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1305 struct tmpfs_dirent *de;
1307 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1308 MPASS(de != NULL && de->td_node == NULL);
1309 tmpfs_dir_detach(dvp, de);
1310 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1313 /* --------------------------------------------------------------------- */
1316 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1317 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
1318 * 'newsize' must be positive.
1320 * Returns zero on success or an appropriate error code on failure.
1323 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1325 struct tmpfs_mount *tmp;
1326 struct tmpfs_node *node;
1329 vm_pindex_t idx, newpages, oldpages;
1333 MPASS(vp->v_type == VREG);
1334 MPASS(newsize >= 0);
1336 node = VP_TO_TMPFS_NODE(vp);
1337 uobj = node->tn_reg.tn_aobj;
1338 tmp = VFS_TO_TMPFS(vp->v_mount);
1341 * Convert the old and new sizes to the number of pages needed to
1342 * store them. It may happen that we do not need to do anything
1343 * because the last allocated page can accommodate the change on
1346 oldsize = node->tn_size;
1347 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1348 MPASS(oldpages == uobj->size);
1349 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1350 if (newpages > oldpages &&
1351 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1354 VM_OBJECT_WLOCK(uobj);
1355 if (newsize < oldsize) {
1357 * Zero the truncated part of the last page.
1359 base = newsize & PAGE_MASK;
1361 idx = OFF_TO_IDX(newsize);
1363 m = vm_page_lookup(uobj, idx);
1365 if (vm_page_sleep_if_busy(m, "tmfssz"))
1367 MPASS(m->valid == VM_PAGE_BITS_ALL);
1368 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1369 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
1371 VM_OBJECT_WUNLOCK(uobj);
1373 VM_OBJECT_WLOCK(uobj);
1375 } else if (m->valid != VM_PAGE_BITS_ALL) {
1377 rv = vm_pager_get_pages(uobj, ma, 1, 0);
1378 m = vm_page_lookup(uobj, idx);
1380 /* A cached page was reactivated. */
1383 if (rv == VM_PAGER_OK) {
1384 vm_page_deactivate(m);
1393 VM_OBJECT_WUNLOCK(uobj);
1399 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1401 vm_pager_page_unswapped(m);
1406 * Release any swap space and free any whole pages.
1408 if (newpages < oldpages) {
1409 swap_pager_freespace(uobj, newpages, oldpages -
1411 vm_object_page_remove(uobj, newpages, 0, 0);
1414 uobj->size = newpages;
1415 VM_OBJECT_WUNLOCK(uobj);
1418 tmp->tm_pages_used += (newpages - oldpages);
1421 node->tn_size = newsize;
1425 /* --------------------------------------------------------------------- */
1428 * Change flags of the given vnode.
1429 * Caller should execute tmpfs_update on vp after a successful execution.
1430 * The vnode must be locked on entry and remain locked on exit.
1433 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1437 struct tmpfs_node *node;
1439 MPASS(VOP_ISLOCKED(vp));
1441 node = VP_TO_TMPFS_NODE(vp);
1443 if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1444 UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1445 UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1446 UF_SPARSE | UF_SYSTEM)) != 0)
1447 return (EOPNOTSUPP);
1449 /* Disallow this operation if the file system is mounted read-only. */
1450 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1454 * Callers may only modify the file flags on objects they
1455 * have VADMIN rights for.
1457 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1460 * Unprivileged processes are not permitted to unset system
1461 * flags, or modify flags if any system flags are set.
1463 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1464 if (node->tn_flags &
1465 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1466 error = securelevel_gt(cred, 0);
1471 if (node->tn_flags &
1472 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1473 ((flags ^ node->tn_flags) & SF_SETTABLE))
1476 node->tn_flags = flags;
1477 node->tn_status |= TMPFS_NODE_CHANGED;
1479 MPASS(VOP_ISLOCKED(vp));
1484 /* --------------------------------------------------------------------- */
1487 * Change access mode on the given vnode.
1488 * Caller should execute tmpfs_update on vp after a successful execution.
1489 * The vnode must be locked on entry and remain locked on exit.
1492 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1495 struct tmpfs_node *node;
1497 MPASS(VOP_ISLOCKED(vp));
1499 node = VP_TO_TMPFS_NODE(vp);
1501 /* Disallow this operation if the file system is mounted read-only. */
1502 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1505 /* Immutable or append-only files cannot be modified, either. */
1506 if (node->tn_flags & (IMMUTABLE | APPEND))
1510 * To modify the permissions on a file, must possess VADMIN
1513 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1517 * Privileged processes may set the sticky bit on non-directories,
1518 * as well as set the setgid bit on a file with a group that the
1519 * process is not a member of.
1521 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1522 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1525 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1526 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1532 node->tn_mode &= ~ALLPERMS;
1533 node->tn_mode |= mode & ALLPERMS;
1535 node->tn_status |= TMPFS_NODE_CHANGED;
1537 MPASS(VOP_ISLOCKED(vp));
1542 /* --------------------------------------------------------------------- */
1545 * Change ownership of the given vnode. At least one of uid or gid must
1546 * be different than VNOVAL. If one is set to that value, the attribute
1548 * Caller should execute tmpfs_update on vp after a successful execution.
1549 * The vnode must be locked on entry and remain locked on exit.
1552 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1556 struct tmpfs_node *node;
1560 MPASS(VOP_ISLOCKED(vp));
1562 node = VP_TO_TMPFS_NODE(vp);
1564 /* Assign default values if they are unknown. */
1565 MPASS(uid != VNOVAL || gid != VNOVAL);
1570 MPASS(uid != VNOVAL && gid != VNOVAL);
1572 /* Disallow this operation if the file system is mounted read-only. */
1573 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1576 /* Immutable or append-only files cannot be modified, either. */
1577 if (node->tn_flags & (IMMUTABLE | APPEND))
1581 * To modify the ownership of a file, must possess VADMIN for that
1584 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1588 * To change the owner of a file, or change the group of a file to a
1589 * group of which we are not a member, the caller must have
1592 if ((uid != node->tn_uid ||
1593 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1594 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1597 ogid = node->tn_gid;
1598 ouid = node->tn_uid;
1603 node->tn_status |= TMPFS_NODE_CHANGED;
1605 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1606 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1607 node->tn_mode &= ~(S_ISUID | S_ISGID);
1610 MPASS(VOP_ISLOCKED(vp));
1615 /* --------------------------------------------------------------------- */
1618 * Change size of the given vnode.
1619 * Caller should execute tmpfs_update on vp after a successful execution.
1620 * The vnode must be locked on entry and remain locked on exit.
1623 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1627 struct tmpfs_node *node;
1629 MPASS(VOP_ISLOCKED(vp));
1631 node = VP_TO_TMPFS_NODE(vp);
1633 /* Decide whether this is a valid operation based on the file type. */
1635 switch (vp->v_type) {
1640 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1649 /* Allow modifications of special files even if in the file
1650 * system is mounted read-only (we are not modifying the
1651 * files themselves, but the objects they represent). */
1655 /* Anything else is unsupported. */
1659 /* Immutable or append-only files cannot be modified, either. */
1660 if (node->tn_flags & (IMMUTABLE | APPEND))
1663 error = tmpfs_truncate(vp, size);
1664 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1665 * for us, as will update tn_status; no need to do that here. */
1667 MPASS(VOP_ISLOCKED(vp));
1672 /* --------------------------------------------------------------------- */
1675 * Change access and modification times of the given vnode.
1676 * Caller should execute tmpfs_update on vp after a successful execution.
1677 * The vnode must be locked on entry and remain locked on exit.
1680 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
1681 struct ucred *cred, struct thread *l)
1684 struct tmpfs_node *node;
1686 MPASS(VOP_ISLOCKED(vp));
1688 node = VP_TO_TMPFS_NODE(vp);
1690 /* Disallow this operation if the file system is mounted read-only. */
1691 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1694 /* Immutable or append-only files cannot be modified, either. */
1695 if (node->tn_flags & (IMMUTABLE | APPEND))
1698 error = vn_utimes_perm(vp, vap, cred, l);
1702 if (vap->va_atime.tv_sec != VNOVAL && vap->va_atime.tv_nsec != VNOVAL)
1703 node->tn_status |= TMPFS_NODE_ACCESSED;
1705 if (vap->va_mtime.tv_sec != VNOVAL && vap->va_mtime.tv_nsec != VNOVAL)
1706 node->tn_status |= TMPFS_NODE_MODIFIED;
1708 if (vap->va_birthtime.tv_nsec != VNOVAL &&
1709 vap->va_birthtime.tv_nsec != VNOVAL)
1710 node->tn_status |= TMPFS_NODE_MODIFIED;
1712 tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
1714 if (vap->va_birthtime.tv_nsec != VNOVAL &&
1715 vap->va_birthtime.tv_nsec != VNOVAL)
1716 node->tn_birthtime = vap->va_birthtime;
1717 MPASS(VOP_ISLOCKED(vp));
1722 /* --------------------------------------------------------------------- */
1723 /* Sync timestamps */
1725 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1726 const struct timespec *mod)
1728 struct tmpfs_node *node;
1729 struct timespec now;
1731 node = VP_TO_TMPFS_NODE(vp);
1733 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1734 TMPFS_NODE_CHANGED)) == 0)
1737 vfs_timestamp(&now);
1738 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1741 node->tn_atime = *acc;
1743 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1746 node->tn_mtime = *mod;
1748 if (node->tn_status & TMPFS_NODE_CHANGED) {
1749 node->tn_ctime = now;
1752 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1755 /* --------------------------------------------------------------------- */
1758 tmpfs_update(struct vnode *vp)
1761 tmpfs_itimes(vp, NULL, NULL);
1764 /* --------------------------------------------------------------------- */
1767 tmpfs_truncate(struct vnode *vp, off_t length)
1770 struct tmpfs_node *node;
1772 node = VP_TO_TMPFS_NODE(vp);
1779 if (node->tn_size == length) {
1784 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1787 error = tmpfs_reg_resize(vp, length, FALSE);
1789 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1799 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1801 if (a->td_hash > b->td_hash)
1803 else if (a->td_hash < b->td_hash)
1808 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);