1 /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Efficient memory file system supporting functions.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/fnv_hash.h>
42 #include <sys/namei.h>
45 #include <sys/rwlock.h>
47 #include <sys/systm.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/vmmeter.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pageout.h>
57 #include <vm/vm_pager.h>
58 #include <vm/vm_extern.h>
60 #include <fs/tmpfs/tmpfs.h>
61 #include <fs/tmpfs/tmpfs_fifoops.h>
62 #include <fs/tmpfs/tmpfs_vnops.h>
64 struct tmpfs_dir_cursor {
65 struct tmpfs_dirent *tdc_current;
66 struct tmpfs_dirent *tdc_tree;
69 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "tmpfs file system");
71 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
74 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
79 pages = *(long *)arg1;
80 bytes = pages * PAGE_SIZE;
82 error = sysctl_handle_long(oidp, &bytes, 0, req);
83 if (error || !req->newptr)
86 pages = bytes / PAGE_SIZE;
87 if (pages < TMPFS_PAGES_MINRESERVED)
90 *(long *)arg1 = pages;
94 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved, CTLTYPE_LONG|CTLFLAG_RW,
95 &tmpfs_pages_reserved, 0, sysctl_mem_reserved, "L",
96 "Amount of available memory and swap below which tmpfs growth stops");
98 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
99 struct tmpfs_dirent *b);
100 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
103 tmpfs_mem_avail(void)
107 avail = swap_pager_avail + cnt.v_free_count + cnt.v_cache_count -
108 tmpfs_pages_reserved;
109 if (__predict_false(avail < 0))
115 tmpfs_pages_used(struct tmpfs_mount *tmp)
117 const size_t node_size = sizeof(struct tmpfs_node) +
118 sizeof(struct tmpfs_dirent);
121 meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
123 return (meta_pages + tmp->tm_pages_used);
127 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
129 if (tmpfs_mem_avail() < req_pages)
132 if (tmp->tm_pages_max != SIZE_MAX &&
133 tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
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;
170 /* If the root directory of the 'tmp' file system is not yet
171 * allocated, this must be the request to do it. */
172 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
174 MPASS(IFF(type == VLNK, target != NULL));
175 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
177 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
179 if (tmpfs_pages_check_avail(tmp, 1) == 0)
182 nnode = (struct tmpfs_node *)uma_zalloc_arg(
183 tmp->tm_node_pool, tmp, M_WAITOK);
185 /* Generic initialization. */
186 nnode->tn_type = type;
187 vfs_timestamp(&nnode->tn_atime);
188 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
192 nnode->tn_mode = mode;
193 nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
195 /* Type-specific initialization. */
196 switch (nnode->tn_type) {
199 nnode->tn_rdev = rdev;
203 RB_INIT(&nnode->tn_dir.tn_dirhead);
204 LIST_INIT(&nnode->tn_dir.tn_dupindex);
205 MPASS(parent != nnode);
206 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
207 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
208 nnode->tn_dir.tn_readdir_lastn = 0;
209 nnode->tn_dir.tn_readdir_lastp = NULL;
211 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
212 nnode->tn_dir.tn_parent->tn_links++;
213 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
222 MPASS(strlen(target) < MAXPATHLEN);
223 nnode->tn_size = strlen(target);
224 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
226 memcpy(nnode->tn_link, target, nnode->tn_size);
230 nnode->tn_reg.tn_aobj =
231 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
232 NULL /* XXXKIB - tmpfs needs swap reservation */);
236 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
240 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
241 tmp->tm_nodes_inuse++;
248 /* --------------------------------------------------------------------- */
251 * Destroys the node pointed to by node from the file system 'tmp'.
252 * If the node does not belong to the given mount point, the results are
255 * If the node references a directory; no entries are allowed because
256 * their removal could need a recursive algorithm, something forbidden in
257 * kernel space. Furthermore, there is not need to provide such
258 * functionality (recursive removal) because the only primitives offered
259 * to the user are the removal of empty directories and the deletion of
262 * Note that nodes are not really deleted; in fact, when a node has been
263 * allocated, it cannot be deleted during the whole life of the file
264 * system. Instead, they are moved to the available list and remain there
268 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
273 TMPFS_NODE_LOCK(node);
274 MPASS(node->tn_vnode == NULL);
275 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
276 TMPFS_NODE_UNLOCK(node);
280 LIST_REMOVE(node, tn_entries);
281 tmp->tm_nodes_inuse--;
284 switch (node->tn_type) {
286 /* Do not do anything. VNON is provided to let the
287 * allocation routine clean itself easily by avoiding
288 * duplicating code in it. */
302 free(node->tn_link, M_TMPFSNAME);
306 uobj = node->tn_reg.tn_aobj;
309 tmp->tm_pages_used -= uobj->size;
311 vm_object_deallocate(uobj);
316 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
319 free_unr(tmp->tm_ino_unr, node->tn_id);
320 uma_zfree(tmp->tm_node_pool, node);
323 /* --------------------------------------------------------------------- */
325 static __inline uint32_t
326 tmpfs_dirent_hash(const char *name, u_int len)
330 hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
331 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
334 if (hash < TMPFS_DIRCOOKIE_MIN)
335 hash += TMPFS_DIRCOOKIE_MIN;
340 static __inline off_t
341 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
343 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
345 return (de->td_cookie);
348 static __inline boolean_t
349 tmpfs_dirent_dup(struct tmpfs_dirent *de)
351 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
354 static __inline boolean_t
355 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
357 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
361 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
363 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
364 memcpy(de->ud.td_name, name, namelen);
365 de->td_namelen = namelen;
369 * Allocates a new directory entry for the node node with a name of name.
370 * The new directory entry is returned in *de.
372 * The link count of node is increased by one to reflect the new object
375 * Returns zero on success or an appropriate error code on failure.
378 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
379 const char *name, u_int len, struct tmpfs_dirent **de)
381 struct tmpfs_dirent *nde;
383 nde = uma_zalloc(tmp->tm_dirent_pool, M_WAITOK);
386 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
387 tmpfs_dirent_init(nde, name, len);
398 /* --------------------------------------------------------------------- */
401 * Frees a directory entry. It is the caller's responsibility to destroy
402 * the node referenced by it if needed.
404 * The link count of node is decreased by one to reflect the removal of an
405 * object that referenced it. This only happens if 'node_exists' is true;
406 * otherwise the function will not access the node referred to by the
407 * directory entry, as it may already have been released from the outside.
410 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
412 struct tmpfs_node *node;
416 MPASS(node->tn_links > 0);
419 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
420 free(de->ud.td_name, M_TMPFSNAME);
421 uma_zfree(tmp->tm_dirent_pool, de);
424 /* --------------------------------------------------------------------- */
427 * Allocates a new vnode for the node node or returns a new reference to
428 * an existing one if the node had already a vnode referencing it. The
429 * resulting locked vnode is returned in *vpp.
431 * Returns zero on success or an appropriate error code on failure.
434 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
441 TMPFS_NODE_LOCK(node);
442 if ((vp = node->tn_vnode) != NULL) {
443 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
445 TMPFS_NODE_UNLOCK(node);
446 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
453 * Make sure the vnode is still there after
454 * getting the interlock to avoid racing a free.
456 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
464 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
465 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
466 TMPFS_NODE_UNLOCK(node);
473 * otherwise lock the vp list while we call getnewvnode
474 * since that can block.
476 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
477 node->tn_vpstate |= TMPFS_VNODE_WANT;
478 error = msleep((caddr_t) &node->tn_vpstate,
479 TMPFS_NODE_MTX(node), PDROP | PCATCH,
480 "tmpfs_alloc_vp", 0);
486 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
488 TMPFS_NODE_UNLOCK(node);
490 /* Get a new vnode and associate it with our node. */
491 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
496 (void) vn_lock(vp, lkflag | LK_RETRY);
499 vp->v_type = node->tn_type;
501 /* Type-specific initialization. */
502 switch (node->tn_type) {
514 vp->v_op = &tmpfs_fifoop_entries;
517 MPASS(node->tn_dir.tn_parent != NULL);
518 if (node->tn_dir.tn_parent == node)
519 vp->v_vflag |= VV_ROOT;
523 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
526 vnode_pager_setsize(vp, node->tn_size);
527 error = insmntque(vp, mp);
532 TMPFS_NODE_LOCK(node);
534 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
535 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
538 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
539 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
540 TMPFS_NODE_UNLOCK(node);
541 wakeup((caddr_t) &node->tn_vpstate);
543 TMPFS_NODE_UNLOCK(node);
550 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
551 TMPFS_NODE_LOCK(node);
552 MPASS(*vpp == node->tn_vnode);
553 TMPFS_NODE_UNLOCK(node);
560 /* --------------------------------------------------------------------- */
563 * Destroys the association between the vnode vp and the node it
567 tmpfs_free_vp(struct vnode *vp)
569 struct tmpfs_node *node;
571 node = VP_TO_TMPFS_NODE(vp);
573 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
574 node->tn_vnode = NULL;
578 /* --------------------------------------------------------------------- */
581 * Allocates a new file of type 'type' and adds it to the parent directory
582 * 'dvp'; this addition is done using the component name given in 'cnp'.
583 * The ownership of the new file is automatically assigned based on the
584 * credentials of the caller (through 'cnp'), the group is set based on
585 * the parent directory and the mode is determined from the 'vap' argument.
586 * If successful, *vpp holds a vnode to the newly created file and zero
587 * is returned. Otherwise *vpp is NULL and the function returns an
588 * appropriate error code.
591 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
592 struct componentname *cnp, char *target)
595 struct tmpfs_dirent *de;
596 struct tmpfs_mount *tmp;
597 struct tmpfs_node *dnode;
598 struct tmpfs_node *node;
599 struct tmpfs_node *parent;
601 MPASS(VOP_ISLOCKED(dvp));
602 MPASS(cnp->cn_flags & HASBUF);
604 tmp = VFS_TO_TMPFS(dvp->v_mount);
605 dnode = VP_TO_TMPFS_DIR(dvp);
608 /* If the entry we are creating is a directory, we cannot overflow
609 * the number of links of its parent, because it will get a new
611 if (vap->va_type == VDIR) {
612 /* Ensure that we do not overflow the maximum number of links
613 * imposed by the system. */
614 MPASS(dnode->tn_links <= LINK_MAX);
615 if (dnode->tn_links == LINK_MAX) {
621 MPASS(parent != NULL);
625 /* Allocate a node that represents the new file. */
626 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
627 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
631 /* Allocate a directory entry that points to the new file. */
632 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
635 tmpfs_free_node(tmp, node);
639 /* Allocate a vnode for the new file. */
640 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
642 tmpfs_free_dirent(tmp, de);
643 tmpfs_free_node(tmp, node);
647 /* Now that all required items are allocated, we can proceed to
648 * insert the new node into the directory, an operation that
650 if (cnp->cn_flags & ISWHITEOUT)
651 tmpfs_dir_whiteout_remove(dvp, cnp);
652 tmpfs_dir_attach(dvp, de);
659 /* --------------------------------------------------------------------- */
661 static struct tmpfs_dirent *
662 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
664 struct tmpfs_dirent *de;
666 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
668 if (de != NULL && tmpfs_dirent_duphead(de))
669 de = LIST_FIRST(&de->ud.td_duphead);
670 dc->tdc_current = de;
672 return (dc->tdc_current);
675 static struct tmpfs_dirent *
676 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
678 struct tmpfs_dirent *de;
680 MPASS(dc->tdc_tree != NULL);
681 if (tmpfs_dirent_dup(dc->tdc_current)) {
682 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
683 if (dc->tdc_current != NULL)
684 return (dc->tdc_current);
686 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
687 &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
688 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
689 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
690 MPASS(dc->tdc_current != NULL);
693 return (dc->tdc_current);
696 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
697 static struct tmpfs_dirent *
698 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
700 struct tmpfs_dirent *de, dekey;
702 dekey.td_hash = hash;
703 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
707 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
708 static struct tmpfs_dirent *
709 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
710 struct tmpfs_dir_cursor *dc)
712 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
713 struct tmpfs_dirent *de, dekey;
715 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
717 if (cookie == node->tn_dir.tn_readdir_lastn &&
718 (de = node->tn_dir.tn_readdir_lastp) != NULL) {
719 /* Protect against possible race, tn_readdir_last[pn]
720 * may be updated with only shared vnode lock held. */
721 if (cookie == tmpfs_dirent_cookie(de))
725 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
726 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
727 uh.td_dup.index_entries) {
728 MPASS(tmpfs_dirent_dup(de));
729 if (de->td_cookie == cookie)
731 /* dupindex list is sorted. */
732 if (de->td_cookie < cookie) {
741 MPASS((cookie & TMPFS_DIRCOOKIE_MASK) == cookie);
742 dekey.td_hash = cookie;
743 /* Recover if direntry for cookie was removed */
744 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
746 dc->tdc_current = de;
747 if (de != NULL && tmpfs_dirent_duphead(de)) {
748 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
749 MPASS(dc->tdc_current != NULL);
751 return (dc->tdc_current);
755 dc->tdc_current = de;
756 if (de != NULL && tmpfs_dirent_dup(de))
757 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
759 return (dc->tdc_current);
763 * Looks for a directory entry in the directory represented by node.
764 * 'cnp' describes the name of the entry to look for. Note that the .
765 * and .. components are not allowed as they do not physically exist
766 * within directories.
768 * Returns a pointer to the entry when found, otherwise NULL.
770 struct tmpfs_dirent *
771 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
772 struct componentname *cnp)
774 struct tmpfs_dir_duphead *duphead;
775 struct tmpfs_dirent *de;
778 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
779 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
780 cnp->cn_nameptr[1] == '.')));
781 TMPFS_VALIDATE_DIR(node);
783 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
784 de = tmpfs_dir_xlookup_hash(node, hash);
785 if (de != NULL && tmpfs_dirent_duphead(de)) {
786 duphead = &de->ud.td_duphead;
787 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
788 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
792 } else if (de != NULL) {
793 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
797 if (de != NULL && f != NULL && de->td_node != f)
804 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
805 * list, allocate new cookie value.
808 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
809 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
811 struct tmpfs_dir_duphead *dupindex;
812 struct tmpfs_dirent *de, *pde;
814 dupindex = &dnode->tn_dir.tn_dupindex;
815 de = LIST_FIRST(dupindex);
816 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
818 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
820 nde->td_cookie = de->td_cookie + 1;
821 MPASS(tmpfs_dirent_dup(nde));
822 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
823 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
828 * Cookie numbers are near exhaustion. Scan dupindex list for unused
829 * numbers. dupindex list is sorted in descending order. Keep it so
830 * after inserting nde.
834 de = LIST_NEXT(de, uh.td_dup.index_entries);
835 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
837 * Last element of the index doesn't have minimal cookie
840 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
841 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
842 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
844 } else if (de == NULL) {
846 * We are so lucky have 2^30 hash duplicates in single
847 * directory :) Return largest possible cookie value.
848 * It should be fine except possible issues with
849 * VOP_READDIR restart.
851 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
852 LIST_INSERT_HEAD(dupindex, nde,
853 uh.td_dup.index_entries);
854 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
857 if (de->td_cookie + 1 == pde->td_cookie ||
858 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
859 continue; /* No hole or invalid cookie. */
860 nde->td_cookie = de->td_cookie + 1;
861 MPASS(tmpfs_dirent_dup(nde));
862 MPASS(pde->td_cookie > nde->td_cookie);
863 MPASS(nde->td_cookie > de->td_cookie);
864 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
865 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
871 * Attaches the directory entry de to the directory represented by vp.
872 * Note that this does not change the link count of the node pointed by
873 * the directory entry, as this is done by tmpfs_alloc_dirent.
876 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
878 struct tmpfs_node *dnode;
879 struct tmpfs_dirent *xde, *nde;
881 ASSERT_VOP_ELOCKED(vp, __func__);
882 MPASS(de->td_namelen > 0);
883 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
884 MPASS(de->td_cookie == de->td_hash);
886 dnode = VP_TO_TMPFS_DIR(vp);
887 dnode->tn_dir.tn_readdir_lastn = 0;
888 dnode->tn_dir.tn_readdir_lastp = NULL;
890 MPASS(!tmpfs_dirent_dup(de));
891 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
892 if (xde != NULL && tmpfs_dirent_duphead(xde))
893 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
894 else if (xde != NULL) {
896 * Allocate new duphead. Swap xde with duphead to avoid
897 * adding/removing elements with the same hash.
899 MPASS(!tmpfs_dirent_dup(xde));
900 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
902 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
903 memcpy(nde, xde, sizeof(*xde));
904 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
905 LIST_INIT(&xde->ud.td_duphead);
908 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
909 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
911 dnode->tn_size += sizeof(struct tmpfs_dirent);
912 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
916 /* --------------------------------------------------------------------- */
919 * Detaches the directory entry de from the directory represented by vp.
920 * Note that this does not change the link count of the node pointed by
921 * the directory entry, as this is done by tmpfs_free_dirent.
924 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
926 struct tmpfs_mount *tmp;
927 struct tmpfs_dir *head;
928 struct tmpfs_node *dnode;
929 struct tmpfs_dirent *xde;
931 ASSERT_VOP_ELOCKED(vp, __func__);
933 dnode = VP_TO_TMPFS_DIR(vp);
934 head = &dnode->tn_dir.tn_dirhead;
935 dnode->tn_dir.tn_readdir_lastn = 0;
936 dnode->tn_dir.tn_readdir_lastp = NULL;
938 if (tmpfs_dirent_dup(de)) {
939 /* Remove duphead if de was last entry. */
940 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
941 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
942 MPASS(tmpfs_dirent_duphead(xde));
945 LIST_REMOVE(de, uh.td_dup.entries);
946 LIST_REMOVE(de, uh.td_dup.index_entries);
948 if (LIST_EMPTY(&xde->ud.td_duphead)) {
949 RB_REMOVE(tmpfs_dir, head, xde);
950 tmp = VFS_TO_TMPFS(vp->v_mount);
951 MPASS(xde->td_node == NULL);
952 tmpfs_free_dirent(tmp, xde);
956 RB_REMOVE(tmpfs_dir, head, de);
958 dnode->tn_size -= sizeof(struct tmpfs_dirent);
959 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
964 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
966 struct tmpfs_dirent *de, *dde, *nde;
968 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
969 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
970 /* Node may already be destroyed. */
972 if (tmpfs_dirent_duphead(de)) {
973 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
974 LIST_REMOVE(dde, uh.td_dup.entries);
976 tmpfs_free_dirent(tmp, dde);
979 tmpfs_free_dirent(tmp, de);
983 /* --------------------------------------------------------------------- */
986 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
987 * directory and returns it in the uio space. The function returns 0
988 * on success, -1 if there was not enough space in the uio structure to
989 * hold the directory entry or an appropriate error code if another
993 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
998 TMPFS_VALIDATE_DIR(node);
999 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1001 dent.d_fileno = node->tn_id;
1002 dent.d_type = DT_DIR;
1004 dent.d_name[0] = '.';
1005 dent.d_name[1] = '\0';
1006 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1008 if (dent.d_reclen > uio->uio_resid)
1009 error = EJUSTRETURN;
1011 error = uiomove(&dent, dent.d_reclen, uio);
1013 node->tn_status |= TMPFS_NODE_ACCESSED;
1018 /* --------------------------------------------------------------------- */
1021 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
1022 * directory and returns it in the uio space. The function returns 0
1023 * on success, -1 if there was not enough space in the uio structure to
1024 * hold the directory entry or an appropriate error code if another
1028 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
1033 TMPFS_VALIDATE_DIR(node);
1034 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1037 * Return ENOENT if the current node is already removed.
1039 TMPFS_ASSERT_LOCKED(node);
1040 if (node->tn_dir.tn_parent == NULL) {
1044 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
1045 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
1046 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
1048 dent.d_type = DT_DIR;
1050 dent.d_name[0] = '.';
1051 dent.d_name[1] = '.';
1052 dent.d_name[2] = '\0';
1053 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1055 if (dent.d_reclen > uio->uio_resid)
1056 error = EJUSTRETURN;
1058 error = uiomove(&dent, dent.d_reclen, uio);
1060 node->tn_status |= TMPFS_NODE_ACCESSED;
1065 /* --------------------------------------------------------------------- */
1068 * Helper function for tmpfs_readdir. Returns as much directory entries
1069 * as can fit in the uio space. The read starts at uio->uio_offset.
1070 * The function returns 0 on success, -1 if there was not enough space
1071 * in the uio structure to hold the directory entry or an appropriate
1072 * error code if another error happens.
1075 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, int cnt,
1076 u_long *cookies, int *ncookies)
1078 struct tmpfs_dir_cursor dc;
1079 struct tmpfs_dirent *de;
1083 TMPFS_VALIDATE_DIR(node);
1086 switch (uio->uio_offset) {
1087 case TMPFS_DIRCOOKIE_DOT:
1088 error = tmpfs_dir_getdotdent(node, uio);
1091 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1093 cookies[(*ncookies)++] = off = uio->uio_offset;
1094 case TMPFS_DIRCOOKIE_DOTDOT:
1095 error = tmpfs_dir_getdotdotdent(node, uio);
1098 de = tmpfs_dir_first(node, &dc);
1100 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
1102 uio->uio_offset = tmpfs_dirent_cookie(de);
1104 cookies[(*ncookies)++] = off = uio->uio_offset;
1108 case TMPFS_DIRCOOKIE_EOF:
1111 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1115 off = tmpfs_dirent_cookie(de);
1118 /* Read as much entries as possible; i.e., until we reach the end of
1119 * the directory or we exhaust uio space. */
1123 /* Create a dirent structure representing the current
1124 * tmpfs_node and fill it. */
1125 if (de->td_node == NULL) {
1129 d.d_fileno = de->td_node->tn_id;
1130 switch (de->td_node->tn_type) {
1160 panic("tmpfs_dir_getdents: type %p %d",
1161 de->td_node, (int)de->td_node->tn_type);
1164 d.d_namlen = de->td_namelen;
1165 MPASS(de->td_namelen < sizeof(d.d_name));
1166 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1167 d.d_name[de->td_namelen] = '\0';
1168 d.d_reclen = GENERIC_DIRSIZ(&d);
1170 /* Stop reading if the directory entry we are treating is
1171 * bigger than the amount of data that can be returned. */
1172 if (d.d_reclen > uio->uio_resid) {
1173 error = EJUSTRETURN;
1177 /* Copy the new dirent structure into the output buffer and
1178 * advance pointers. */
1179 error = uiomove(&d, d.d_reclen, uio);
1181 de = tmpfs_dir_next(node, &dc);
1184 off = TMPFS_DIRCOOKIE_EOF;
1186 off = tmpfs_dirent_cookie(de);
1187 MPASS(*ncookies < cnt);
1188 cookies[(*ncookies)++] = off;
1191 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1193 /* Update the offset and cache. */
1196 off = TMPFS_DIRCOOKIE_EOF;
1198 off = tmpfs_dirent_cookie(de);
1201 uio->uio_offset = off;
1202 node->tn_dir.tn_readdir_lastn = off;
1203 node->tn_dir.tn_readdir_lastp = de;
1205 node->tn_status |= TMPFS_NODE_ACCESSED;
1210 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1212 struct tmpfs_dirent *de;
1215 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1216 cnp->cn_nameptr, cnp->cn_namelen, &de);
1219 tmpfs_dir_attach(dvp, de);
1224 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1226 struct tmpfs_dirent *de;
1228 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1229 MPASS(de != NULL && de->td_node == NULL);
1230 tmpfs_dir_detach(dvp, de);
1231 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1234 /* --------------------------------------------------------------------- */
1237 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1238 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
1239 * 'newsize' must be positive.
1241 * Returns zero on success or an appropriate error code on failure.
1244 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1246 struct tmpfs_mount *tmp;
1247 struct tmpfs_node *node;
1250 vm_pindex_t idx, newpages, oldpages;
1254 MPASS(vp->v_type == VREG);
1255 MPASS(newsize >= 0);
1257 node = VP_TO_TMPFS_NODE(vp);
1258 uobj = node->tn_reg.tn_aobj;
1259 tmp = VFS_TO_TMPFS(vp->v_mount);
1262 * Convert the old and new sizes to the number of pages needed to
1263 * store them. It may happen that we do not need to do anything
1264 * because the last allocated page can accommodate the change on
1267 oldsize = node->tn_size;
1268 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1269 MPASS(oldpages == uobj->size);
1270 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1271 if (newpages > oldpages &&
1272 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1275 VM_OBJECT_WLOCK(uobj);
1276 if (newsize < oldsize) {
1278 * Zero the truncated part of the last page.
1280 base = newsize & PAGE_MASK;
1282 idx = OFF_TO_IDX(newsize);
1284 m = vm_page_lookup(uobj, idx);
1286 if ((m->oflags & VPO_BUSY) != 0 ||
1288 vm_page_sleep(m, "tmfssz");
1291 MPASS(m->valid == VM_PAGE_BITS_ALL);
1292 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1293 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
1295 VM_OBJECT_WUNLOCK(uobj);
1297 VM_OBJECT_WLOCK(uobj);
1299 } else if (m->valid != VM_PAGE_BITS_ALL) {
1301 rv = vm_pager_get_pages(uobj, ma, 1, 0);
1302 m = vm_page_lookup(uobj, idx);
1304 /* A cached page was reactivated. */
1307 if (rv == VM_PAGER_OK) {
1308 vm_page_deactivate(m);
1317 VM_OBJECT_WUNLOCK(uobj);
1323 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1325 vm_pager_page_unswapped(m);
1330 * Release any swap space and free any whole pages.
1332 if (newpages < oldpages) {
1333 swap_pager_freespace(uobj, newpages, oldpages -
1335 vm_object_page_remove(uobj, newpages, 0, 0);
1338 uobj->size = newpages;
1339 VM_OBJECT_WUNLOCK(uobj);
1342 tmp->tm_pages_used += (newpages - oldpages);
1345 node->tn_size = newsize;
1346 vnode_pager_setsize(vp, newsize);
1350 /* --------------------------------------------------------------------- */
1353 * Change flags of the given vnode.
1354 * Caller should execute tmpfs_update on vp after a successful execution.
1355 * The vnode must be locked on entry and remain locked on exit.
1358 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p)
1361 struct tmpfs_node *node;
1363 MPASS(VOP_ISLOCKED(vp));
1365 node = VP_TO_TMPFS_NODE(vp);
1367 if ((flags & ~(UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE |
1368 UF_NOUNLINK | SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND |
1370 return (EOPNOTSUPP);
1372 /* Disallow this operation if the file system is mounted read-only. */
1373 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1377 * Callers may only modify the file flags on objects they
1378 * have VADMIN rights for.
1380 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1383 * Unprivileged processes are not permitted to unset system
1384 * flags, or modify flags if any system flags are set.
1386 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1387 if (node->tn_flags &
1388 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1389 error = securelevel_gt(cred, 0);
1394 if (node->tn_flags &
1395 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1396 ((flags ^ node->tn_flags) & SF_SETTABLE))
1399 node->tn_flags = flags;
1400 node->tn_status |= TMPFS_NODE_CHANGED;
1402 MPASS(VOP_ISLOCKED(vp));
1407 /* --------------------------------------------------------------------- */
1410 * Change access mode on the given vnode.
1411 * Caller should execute tmpfs_update on vp after a successful execution.
1412 * The vnode must be locked on entry and remain locked on exit.
1415 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1418 struct tmpfs_node *node;
1420 MPASS(VOP_ISLOCKED(vp));
1422 node = VP_TO_TMPFS_NODE(vp);
1424 /* Disallow this operation if the file system is mounted read-only. */
1425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1428 /* Immutable or append-only files cannot be modified, either. */
1429 if (node->tn_flags & (IMMUTABLE | APPEND))
1433 * To modify the permissions on a file, must possess VADMIN
1436 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1440 * Privileged processes may set the sticky bit on non-directories,
1441 * as well as set the setgid bit on a file with a group that the
1442 * process is not a member of.
1444 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1445 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1448 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1449 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1455 node->tn_mode &= ~ALLPERMS;
1456 node->tn_mode |= mode & ALLPERMS;
1458 node->tn_status |= TMPFS_NODE_CHANGED;
1460 MPASS(VOP_ISLOCKED(vp));
1465 /* --------------------------------------------------------------------- */
1468 * Change ownership of the given vnode. At least one of uid or gid must
1469 * be different than VNOVAL. If one is set to that value, the attribute
1471 * Caller should execute tmpfs_update on vp after a successful execution.
1472 * The vnode must be locked on entry and remain locked on exit.
1475 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1479 struct tmpfs_node *node;
1483 MPASS(VOP_ISLOCKED(vp));
1485 node = VP_TO_TMPFS_NODE(vp);
1487 /* Assign default values if they are unknown. */
1488 MPASS(uid != VNOVAL || gid != VNOVAL);
1493 MPASS(uid != VNOVAL && gid != VNOVAL);
1495 /* Disallow this operation if the file system is mounted read-only. */
1496 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1499 /* Immutable or append-only files cannot be modified, either. */
1500 if (node->tn_flags & (IMMUTABLE | APPEND))
1504 * To modify the ownership of a file, must possess VADMIN for that
1507 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1511 * To change the owner of a file, or change the group of a file to a
1512 * group of which we are not a member, the caller must have
1515 if ((uid != node->tn_uid ||
1516 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1517 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1520 ogid = node->tn_gid;
1521 ouid = node->tn_uid;
1526 node->tn_status |= TMPFS_NODE_CHANGED;
1528 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1529 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1530 node->tn_mode &= ~(S_ISUID | S_ISGID);
1533 MPASS(VOP_ISLOCKED(vp));
1538 /* --------------------------------------------------------------------- */
1541 * Change size of the given vnode.
1542 * Caller should execute tmpfs_update on vp after a successful execution.
1543 * The vnode must be locked on entry and remain locked on exit.
1546 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1550 struct tmpfs_node *node;
1552 MPASS(VOP_ISLOCKED(vp));
1554 node = VP_TO_TMPFS_NODE(vp);
1556 /* Decide whether this is a valid operation based on the file type. */
1558 switch (vp->v_type) {
1563 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1572 /* Allow modifications of special files even if in the file
1573 * system is mounted read-only (we are not modifying the
1574 * files themselves, but the objects they represent). */
1578 /* Anything else is unsupported. */
1582 /* Immutable or append-only files cannot be modified, either. */
1583 if (node->tn_flags & (IMMUTABLE | APPEND))
1586 error = tmpfs_truncate(vp, size);
1587 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1588 * for us, as will update tn_status; no need to do that here. */
1590 MPASS(VOP_ISLOCKED(vp));
1595 /* --------------------------------------------------------------------- */
1598 * Change access and modification times of the given vnode.
1599 * Caller should execute tmpfs_update on vp after a successful execution.
1600 * The vnode must be locked on entry and remain locked on exit.
1603 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1604 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l)
1607 struct tmpfs_node *node;
1609 MPASS(VOP_ISLOCKED(vp));
1611 node = VP_TO_TMPFS_NODE(vp);
1613 /* Disallow this operation if the file system is mounted read-only. */
1614 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1617 /* Immutable or append-only files cannot be modified, either. */
1618 if (node->tn_flags & (IMMUTABLE | APPEND))
1621 /* Determine if the user have proper privilege to update time. */
1622 if (vaflags & VA_UTIMES_NULL) {
1623 error = VOP_ACCESS(vp, VADMIN, cred, l);
1625 error = VOP_ACCESS(vp, VWRITE, cred, l);
1627 error = VOP_ACCESS(vp, VADMIN, cred, l);
1631 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1632 node->tn_status |= TMPFS_NODE_ACCESSED;
1634 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1635 node->tn_status |= TMPFS_NODE_MODIFIED;
1637 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1638 node->tn_status |= TMPFS_NODE_MODIFIED;
1640 tmpfs_itimes(vp, atime, mtime);
1642 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1643 node->tn_birthtime = *birthtime;
1644 MPASS(VOP_ISLOCKED(vp));
1649 /* --------------------------------------------------------------------- */
1650 /* Sync timestamps */
1652 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1653 const struct timespec *mod)
1655 struct tmpfs_node *node;
1656 struct timespec now;
1658 node = VP_TO_TMPFS_NODE(vp);
1660 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1661 TMPFS_NODE_CHANGED)) == 0)
1664 vfs_timestamp(&now);
1665 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1668 node->tn_atime = *acc;
1670 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1673 node->tn_mtime = *mod;
1675 if (node->tn_status & TMPFS_NODE_CHANGED) {
1676 node->tn_ctime = now;
1679 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1682 /* --------------------------------------------------------------------- */
1685 tmpfs_update(struct vnode *vp)
1688 tmpfs_itimes(vp, NULL, NULL);
1691 /* --------------------------------------------------------------------- */
1694 tmpfs_truncate(struct vnode *vp, off_t length)
1697 struct tmpfs_node *node;
1699 node = VP_TO_TMPFS_NODE(vp);
1706 if (node->tn_size == length) {
1711 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1714 error = tmpfs_reg_resize(vp, length, FALSE);
1716 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1726 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1728 if (a->td_hash > b->td_hash)
1730 else if (a->td_hash < b->td_hash)
1735 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);