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;
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)
351 MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
353 return (de->td_cookie);
356 static __inline boolean_t
357 tmpfs_dirent_dup(struct tmpfs_dirent *de)
359 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
362 static __inline boolean_t
363 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
365 return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
369 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
371 de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
372 memcpy(de->ud.td_name, name, namelen);
373 de->td_namelen = namelen;
377 * Allocates a new directory entry for the node node with a name of name.
378 * The new directory entry is returned in *de.
380 * The link count of node is increased by one to reflect the new object
383 * Returns zero on success or an appropriate error code on failure.
386 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
387 const char *name, u_int len, struct tmpfs_dirent **de)
389 struct tmpfs_dirent *nde;
391 nde = uma_zalloc(tmp->tm_dirent_pool, M_WAITOK);
394 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
395 tmpfs_dirent_init(nde, name, len);
406 /* --------------------------------------------------------------------- */
409 * Frees a directory entry. It is the caller's responsibility to destroy
410 * the node referenced by it if needed.
412 * The link count of node is decreased by one to reflect the removal of an
413 * object that referenced it. This only happens if 'node_exists' is true;
414 * otherwise the function will not access the node referred to by the
415 * directory entry, as it may already have been released from the outside.
418 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
420 struct tmpfs_node *node;
424 MPASS(node->tn_links > 0);
427 if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
428 free(de->ud.td_name, M_TMPFSNAME);
429 uma_zfree(tmp->tm_dirent_pool, de);
432 /* --------------------------------------------------------------------- */
435 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
438 if (vp->v_type != VREG || obj == NULL)
441 VM_OBJECT_WLOCK(obj);
443 vm_object_clear_flag(obj, OBJ_TMPFS);
444 obj->un_pager.swp.swp_tmpfs = NULL;
446 VM_OBJECT_WUNLOCK(obj);
450 * Need to clear v_object for insmntque failure.
453 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
456 tmpfs_destroy_vobject(vp, vp->v_object);
459 vp->v_op = &dead_vnodeops;
465 * Allocates a new vnode for the node node or returns a new reference to
466 * an existing one if the node had already a vnode referencing it. The
467 * resulting locked vnode is returned in *vpp.
469 * Returns zero on success or an appropriate error code on failure.
472 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
481 TMPFS_NODE_LOCK(node);
482 if ((vp = node->tn_vnode) != NULL) {
483 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
485 TMPFS_NODE_UNLOCK(node);
486 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
493 * Make sure the vnode is still there after
494 * getting the interlock to avoid racing a free.
496 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
504 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
505 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
506 TMPFS_NODE_UNLOCK(node);
513 * otherwise lock the vp list while we call getnewvnode
514 * since that can block.
516 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
517 node->tn_vpstate |= TMPFS_VNODE_WANT;
518 error = msleep((caddr_t) &node->tn_vpstate,
519 TMPFS_NODE_MTX(node), PDROP | PCATCH,
520 "tmpfs_alloc_vp", 0);
526 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
528 TMPFS_NODE_UNLOCK(node);
530 /* Get a new vnode and associate it with our node. */
531 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
536 (void) vn_lock(vp, lkflag | LK_RETRY);
539 vp->v_type = node->tn_type;
541 /* Type-specific initialization. */
542 switch (node->tn_type) {
552 vp->v_op = &tmpfs_fifoop_entries;
555 object = node->tn_reg.tn_aobj;
556 VM_OBJECT_WLOCK(object);
558 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
559 vp->v_object = object;
560 object->un_pager.swp.swp_tmpfs = vp;
561 vm_object_set_flag(object, OBJ_TMPFS);
563 VM_OBJECT_WUNLOCK(object);
566 MPASS(node->tn_dir.tn_parent != NULL);
567 if (node->tn_dir.tn_parent == node)
568 vp->v_vflag |= VV_ROOT;
572 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
575 error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
580 TMPFS_NODE_LOCK(node);
582 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
583 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
586 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
587 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
588 TMPFS_NODE_UNLOCK(node);
589 wakeup((caddr_t) &node->tn_vpstate);
591 TMPFS_NODE_UNLOCK(node);
598 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
599 TMPFS_NODE_LOCK(node);
600 MPASS(*vpp == node->tn_vnode);
601 TMPFS_NODE_UNLOCK(node);
608 /* --------------------------------------------------------------------- */
611 * Destroys the association between the vnode vp and the node it
615 tmpfs_free_vp(struct vnode *vp)
617 struct tmpfs_node *node;
619 node = VP_TO_TMPFS_NODE(vp);
621 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
622 node->tn_vnode = NULL;
626 /* --------------------------------------------------------------------- */
629 * Allocates a new file of type 'type' and adds it to the parent directory
630 * 'dvp'; this addition is done using the component name given in 'cnp'.
631 * The ownership of the new file is automatically assigned based on the
632 * credentials of the caller (through 'cnp'), the group is set based on
633 * the parent directory and the mode is determined from the 'vap' argument.
634 * If successful, *vpp holds a vnode to the newly created file and zero
635 * is returned. Otherwise *vpp is NULL and the function returns an
636 * appropriate error code.
639 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
640 struct componentname *cnp, char *target)
643 struct tmpfs_dirent *de;
644 struct tmpfs_mount *tmp;
645 struct tmpfs_node *dnode;
646 struct tmpfs_node *node;
647 struct tmpfs_node *parent;
649 MPASS(VOP_ISLOCKED(dvp));
650 MPASS(cnp->cn_flags & HASBUF);
652 tmp = VFS_TO_TMPFS(dvp->v_mount);
653 dnode = VP_TO_TMPFS_DIR(dvp);
656 /* If the entry we are creating is a directory, we cannot overflow
657 * the number of links of its parent, because it will get a new
659 if (vap->va_type == VDIR) {
660 /* Ensure that we do not overflow the maximum number of links
661 * imposed by the system. */
662 MPASS(dnode->tn_links <= LINK_MAX);
663 if (dnode->tn_links == LINK_MAX) {
669 MPASS(parent != NULL);
673 /* Allocate a node that represents the new file. */
674 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
675 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
679 /* Allocate a directory entry that points to the new file. */
680 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
683 tmpfs_free_node(tmp, node);
687 /* Allocate a vnode for the new file. */
688 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
690 tmpfs_free_dirent(tmp, de);
691 tmpfs_free_node(tmp, node);
695 /* Now that all required items are allocated, we can proceed to
696 * insert the new node into the directory, an operation that
698 if (cnp->cn_flags & ISWHITEOUT)
699 tmpfs_dir_whiteout_remove(dvp, cnp);
700 tmpfs_dir_attach(dvp, de);
707 /* --------------------------------------------------------------------- */
709 static struct tmpfs_dirent *
710 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
712 struct tmpfs_dirent *de;
714 de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
716 if (de != NULL && tmpfs_dirent_duphead(de))
717 de = LIST_FIRST(&de->ud.td_duphead);
718 dc->tdc_current = de;
720 return (dc->tdc_current);
723 static struct tmpfs_dirent *
724 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
726 struct tmpfs_dirent *de;
728 MPASS(dc->tdc_tree != NULL);
729 if (tmpfs_dirent_dup(dc->tdc_current)) {
730 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
731 if (dc->tdc_current != NULL)
732 return (dc->tdc_current);
734 dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
735 &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
736 if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
737 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
738 MPASS(dc->tdc_current != NULL);
741 return (dc->tdc_current);
744 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
745 static struct tmpfs_dirent *
746 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
748 struct tmpfs_dirent *de, dekey;
750 dekey.td_hash = hash;
751 de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
755 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
756 static struct tmpfs_dirent *
757 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
758 struct tmpfs_dir_cursor *dc)
760 struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
761 struct tmpfs_dirent *de, dekey;
763 MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
765 if (cookie == node->tn_dir.tn_readdir_lastn &&
766 (de = node->tn_dir.tn_readdir_lastp) != NULL) {
767 /* Protect against possible race, tn_readdir_last[pn]
768 * may be updated with only shared vnode lock held. */
769 if (cookie == tmpfs_dirent_cookie(de))
773 if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
774 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
775 uh.td_dup.index_entries) {
776 MPASS(tmpfs_dirent_dup(de));
777 if (de->td_cookie == cookie)
779 /* dupindex list is sorted. */
780 if (de->td_cookie < cookie) {
789 MPASS((cookie & TMPFS_DIRCOOKIE_MASK) == cookie);
790 dekey.td_hash = cookie;
791 /* Recover if direntry for cookie was removed */
792 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
794 dc->tdc_current = de;
795 if (de != NULL && tmpfs_dirent_duphead(de)) {
796 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
797 MPASS(dc->tdc_current != NULL);
799 return (dc->tdc_current);
803 dc->tdc_current = de;
804 if (de != NULL && tmpfs_dirent_dup(de))
805 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
807 return (dc->tdc_current);
811 * Looks for a directory entry in the directory represented by node.
812 * 'cnp' describes the name of the entry to look for. Note that the .
813 * and .. components are not allowed as they do not physically exist
814 * within directories.
816 * Returns a pointer to the entry when found, otherwise NULL.
818 struct tmpfs_dirent *
819 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
820 struct componentname *cnp)
822 struct tmpfs_dir_duphead *duphead;
823 struct tmpfs_dirent *de;
826 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
827 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
828 cnp->cn_nameptr[1] == '.')));
829 TMPFS_VALIDATE_DIR(node);
831 hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
832 de = tmpfs_dir_xlookup_hash(node, hash);
833 if (de != NULL && tmpfs_dirent_duphead(de)) {
834 duphead = &de->ud.td_duphead;
835 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
836 if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
840 } else if (de != NULL) {
841 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
845 if (de != NULL && f != NULL && de->td_node != f)
852 * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
853 * list, allocate new cookie value.
856 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
857 struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
859 struct tmpfs_dir_duphead *dupindex;
860 struct tmpfs_dirent *de, *pde;
862 dupindex = &dnode->tn_dir.tn_dupindex;
863 de = LIST_FIRST(dupindex);
864 if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
866 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
868 nde->td_cookie = de->td_cookie + 1;
869 MPASS(tmpfs_dirent_dup(nde));
870 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
871 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
876 * Cookie numbers are near exhaustion. Scan dupindex list for unused
877 * numbers. dupindex list is sorted in descending order. Keep it so
878 * after inserting nde.
882 de = LIST_NEXT(de, uh.td_dup.index_entries);
883 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
885 * Last element of the index doesn't have minimal cookie
888 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
889 LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
890 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
892 } else if (de == NULL) {
894 * We are so lucky have 2^30 hash duplicates in single
895 * directory :) Return largest possible cookie value.
896 * It should be fine except possible issues with
897 * VOP_READDIR restart.
899 nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
900 LIST_INSERT_HEAD(dupindex, nde,
901 uh.td_dup.index_entries);
902 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
905 if (de->td_cookie + 1 == pde->td_cookie ||
906 de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
907 continue; /* No hole or invalid cookie. */
908 nde->td_cookie = de->td_cookie + 1;
909 MPASS(tmpfs_dirent_dup(nde));
910 MPASS(pde->td_cookie > nde->td_cookie);
911 MPASS(nde->td_cookie > de->td_cookie);
912 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
913 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
919 * Attaches the directory entry de to 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_alloc_dirent.
924 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
926 struct tmpfs_node *dnode;
927 struct tmpfs_dirent *xde, *nde;
929 ASSERT_VOP_ELOCKED(vp, __func__);
930 MPASS(de->td_namelen > 0);
931 MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
932 MPASS(de->td_cookie == de->td_hash);
934 dnode = VP_TO_TMPFS_DIR(vp);
935 dnode->tn_dir.tn_readdir_lastn = 0;
936 dnode->tn_dir.tn_readdir_lastp = NULL;
938 MPASS(!tmpfs_dirent_dup(de));
939 xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
940 if (xde != NULL && tmpfs_dirent_duphead(xde))
941 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
942 else if (xde != NULL) {
944 * Allocate new duphead. Swap xde with duphead to avoid
945 * adding/removing elements with the same hash.
947 MPASS(!tmpfs_dirent_dup(xde));
948 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
950 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
951 memcpy(nde, xde, sizeof(*xde));
952 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
953 LIST_INIT(&xde->ud.td_duphead);
956 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
957 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
959 dnode->tn_size += sizeof(struct tmpfs_dirent);
960 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
964 /* --------------------------------------------------------------------- */
967 * Detaches the directory entry de from the directory represented by vp.
968 * Note that this does not change the link count of the node pointed by
969 * the directory entry, as this is done by tmpfs_free_dirent.
972 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
974 struct tmpfs_mount *tmp;
975 struct tmpfs_dir *head;
976 struct tmpfs_node *dnode;
977 struct tmpfs_dirent *xde;
979 ASSERT_VOP_ELOCKED(vp, __func__);
981 dnode = VP_TO_TMPFS_DIR(vp);
982 head = &dnode->tn_dir.tn_dirhead;
983 dnode->tn_dir.tn_readdir_lastn = 0;
984 dnode->tn_dir.tn_readdir_lastp = NULL;
986 if (tmpfs_dirent_dup(de)) {
987 /* Remove duphead if de was last entry. */
988 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
989 xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
990 MPASS(tmpfs_dirent_duphead(xde));
993 LIST_REMOVE(de, uh.td_dup.entries);
994 LIST_REMOVE(de, uh.td_dup.index_entries);
996 if (LIST_EMPTY(&xde->ud.td_duphead)) {
997 RB_REMOVE(tmpfs_dir, head, xde);
998 tmp = VFS_TO_TMPFS(vp->v_mount);
999 MPASS(xde->td_node == NULL);
1000 tmpfs_free_dirent(tmp, xde);
1004 RB_REMOVE(tmpfs_dir, head, de);
1006 dnode->tn_size -= sizeof(struct tmpfs_dirent);
1007 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1008 TMPFS_NODE_MODIFIED;
1012 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1014 struct tmpfs_dirent *de, *dde, *nde;
1016 RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1017 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1018 /* Node may already be destroyed. */
1020 if (tmpfs_dirent_duphead(de)) {
1021 while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1022 LIST_REMOVE(dde, uh.td_dup.entries);
1023 dde->td_node = NULL;
1024 tmpfs_free_dirent(tmp, dde);
1027 tmpfs_free_dirent(tmp, de);
1031 /* --------------------------------------------------------------------- */
1034 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
1035 * directory and returns it in the uio space. The function returns 0
1036 * on success, -1 if there was not enough space in the uio structure to
1037 * hold the directory entry or an appropriate error code if another
1041 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
1046 TMPFS_VALIDATE_DIR(node);
1047 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1049 dent.d_fileno = node->tn_id;
1050 dent.d_type = DT_DIR;
1052 dent.d_name[0] = '.';
1053 dent.d_name[1] = '\0';
1054 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1056 if (dent.d_reclen > uio->uio_resid)
1057 error = EJUSTRETURN;
1059 error = uiomove(&dent, dent.d_reclen, uio);
1061 node->tn_status |= TMPFS_NODE_ACCESSED;
1066 /* --------------------------------------------------------------------- */
1069 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
1070 * directory and returns it in the uio space. The function returns 0
1071 * on success, -1 if there was not enough space in the uio structure to
1072 * hold the directory entry or an appropriate error code if another
1076 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
1081 TMPFS_VALIDATE_DIR(node);
1082 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1085 * Return ENOENT if the current node is already removed.
1087 TMPFS_ASSERT_LOCKED(node);
1088 if (node->tn_dir.tn_parent == NULL) {
1092 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
1093 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
1094 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
1096 dent.d_type = DT_DIR;
1098 dent.d_name[0] = '.';
1099 dent.d_name[1] = '.';
1100 dent.d_name[2] = '\0';
1101 dent.d_reclen = GENERIC_DIRSIZ(&dent);
1103 if (dent.d_reclen > uio->uio_resid)
1104 error = EJUSTRETURN;
1106 error = uiomove(&dent, dent.d_reclen, uio);
1108 node->tn_status |= TMPFS_NODE_ACCESSED;
1113 /* --------------------------------------------------------------------- */
1116 * Helper function for tmpfs_readdir. Returns as much directory entries
1117 * as can fit in the uio space. The read starts at uio->uio_offset.
1118 * The function returns 0 on success, -1 if there was not enough space
1119 * in the uio structure to hold the directory entry or an appropriate
1120 * error code if another error happens.
1123 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, int cnt,
1124 u_long *cookies, int *ncookies)
1126 struct tmpfs_dir_cursor dc;
1127 struct tmpfs_dirent *de;
1131 TMPFS_VALIDATE_DIR(node);
1134 switch (uio->uio_offset) {
1135 case TMPFS_DIRCOOKIE_DOT:
1136 error = tmpfs_dir_getdotdent(node, uio);
1139 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1141 cookies[(*ncookies)++] = off = uio->uio_offset;
1142 case TMPFS_DIRCOOKIE_DOTDOT:
1143 error = tmpfs_dir_getdotdotdent(node, uio);
1146 de = tmpfs_dir_first(node, &dc);
1148 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
1150 uio->uio_offset = tmpfs_dirent_cookie(de);
1152 cookies[(*ncookies)++] = off = uio->uio_offset;
1156 case TMPFS_DIRCOOKIE_EOF:
1159 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1163 off = tmpfs_dirent_cookie(de);
1166 /* Read as much entries as possible; i.e., until we reach the end of
1167 * the directory or we exhaust uio space. */
1171 /* Create a dirent structure representing the current
1172 * tmpfs_node and fill it. */
1173 if (de->td_node == NULL) {
1177 d.d_fileno = de->td_node->tn_id;
1178 switch (de->td_node->tn_type) {
1208 panic("tmpfs_dir_getdents: type %p %d",
1209 de->td_node, (int)de->td_node->tn_type);
1212 d.d_namlen = de->td_namelen;
1213 MPASS(de->td_namelen < sizeof(d.d_name));
1214 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1215 d.d_name[de->td_namelen] = '\0';
1216 d.d_reclen = GENERIC_DIRSIZ(&d);
1218 /* Stop reading if the directory entry we are treating is
1219 * bigger than the amount of data that can be returned. */
1220 if (d.d_reclen > uio->uio_resid) {
1221 error = EJUSTRETURN;
1225 /* Copy the new dirent structure into the output buffer and
1226 * advance pointers. */
1227 error = uiomove(&d, d.d_reclen, uio);
1229 de = tmpfs_dir_next(node, &dc);
1232 off = TMPFS_DIRCOOKIE_EOF;
1234 off = tmpfs_dirent_cookie(de);
1235 MPASS(*ncookies < cnt);
1236 cookies[(*ncookies)++] = off;
1239 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1241 /* Update the offset and cache. */
1244 off = TMPFS_DIRCOOKIE_EOF;
1246 off = tmpfs_dirent_cookie(de);
1249 uio->uio_offset = off;
1250 node->tn_dir.tn_readdir_lastn = off;
1251 node->tn_dir.tn_readdir_lastp = de;
1253 node->tn_status |= TMPFS_NODE_ACCESSED;
1258 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1260 struct tmpfs_dirent *de;
1263 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1264 cnp->cn_nameptr, cnp->cn_namelen, &de);
1267 tmpfs_dir_attach(dvp, de);
1272 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1274 struct tmpfs_dirent *de;
1276 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1277 MPASS(de != NULL && de->td_node == NULL);
1278 tmpfs_dir_detach(dvp, de);
1279 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1282 /* --------------------------------------------------------------------- */
1285 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1286 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
1287 * 'newsize' must be positive.
1289 * Returns zero on success or an appropriate error code on failure.
1292 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1294 struct tmpfs_mount *tmp;
1295 struct tmpfs_node *node;
1298 vm_pindex_t idx, newpages, oldpages;
1302 MPASS(vp->v_type == VREG);
1303 MPASS(newsize >= 0);
1305 node = VP_TO_TMPFS_NODE(vp);
1306 uobj = node->tn_reg.tn_aobj;
1307 tmp = VFS_TO_TMPFS(vp->v_mount);
1310 * Convert the old and new sizes to the number of pages needed to
1311 * store them. It may happen that we do not need to do anything
1312 * because the last allocated page can accommodate the change on
1315 oldsize = node->tn_size;
1316 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1317 MPASS(oldpages == uobj->size);
1318 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1319 if (newpages > oldpages &&
1320 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1323 VM_OBJECT_WLOCK(uobj);
1324 if (newsize < oldsize) {
1326 * Zero the truncated part of the last page.
1328 base = newsize & PAGE_MASK;
1330 idx = OFF_TO_IDX(newsize);
1332 m = vm_page_lookup(uobj, idx);
1334 if ((m->oflags & VPO_BUSY) != 0 ||
1336 vm_page_sleep(m, "tmfssz");
1339 MPASS(m->valid == VM_PAGE_BITS_ALL);
1340 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1341 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
1343 VM_OBJECT_WUNLOCK(uobj);
1345 VM_OBJECT_WLOCK(uobj);
1347 } else if (m->valid != VM_PAGE_BITS_ALL) {
1349 rv = vm_pager_get_pages(uobj, ma, 1, 0);
1350 m = vm_page_lookup(uobj, idx);
1352 /* A cached page was reactivated. */
1355 if (rv == VM_PAGER_OK) {
1356 vm_page_deactivate(m);
1365 VM_OBJECT_WUNLOCK(uobj);
1371 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1373 vm_pager_page_unswapped(m);
1378 * Release any swap space and free any whole pages.
1380 if (newpages < oldpages) {
1381 swap_pager_freespace(uobj, newpages, oldpages -
1383 vm_object_page_remove(uobj, newpages, 0, 0);
1386 uobj->size = newpages;
1387 VM_OBJECT_WUNLOCK(uobj);
1390 tmp->tm_pages_used += (newpages - oldpages);
1393 node->tn_size = newsize;
1397 /* --------------------------------------------------------------------- */
1400 * Change flags of the given vnode.
1401 * Caller should execute tmpfs_update on vp after a successful execution.
1402 * The vnode must be locked on entry and remain locked on exit.
1405 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1409 struct tmpfs_node *node;
1411 MPASS(VOP_ISLOCKED(vp));
1413 node = VP_TO_TMPFS_NODE(vp);
1415 if ((flags & ~(UF_NODUMP | UF_IMMUTABLE | UF_APPEND | UF_OPAQUE |
1416 UF_NOUNLINK | SF_ARCHIVED | SF_IMMUTABLE | SF_APPEND |
1418 return (EOPNOTSUPP);
1420 /* Disallow this operation if the file system is mounted read-only. */
1421 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1425 * Callers may only modify the file flags on objects they
1426 * have VADMIN rights for.
1428 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1431 * Unprivileged processes are not permitted to unset system
1432 * flags, or modify flags if any system flags are set.
1434 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1435 if (node->tn_flags &
1436 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1437 error = securelevel_gt(cred, 0);
1442 if (node->tn_flags &
1443 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1444 ((flags ^ node->tn_flags) & SF_SETTABLE))
1447 node->tn_flags = flags;
1448 node->tn_status |= TMPFS_NODE_CHANGED;
1450 MPASS(VOP_ISLOCKED(vp));
1455 /* --------------------------------------------------------------------- */
1458 * Change access mode on the given vnode.
1459 * Caller should execute tmpfs_update on vp after a successful execution.
1460 * The vnode must be locked on entry and remain locked on exit.
1463 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1466 struct tmpfs_node *node;
1468 MPASS(VOP_ISLOCKED(vp));
1470 node = VP_TO_TMPFS_NODE(vp);
1472 /* Disallow this operation if the file system is mounted read-only. */
1473 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1476 /* Immutable or append-only files cannot be modified, either. */
1477 if (node->tn_flags & (IMMUTABLE | APPEND))
1481 * To modify the permissions on a file, must possess VADMIN
1484 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1488 * Privileged processes may set the sticky bit on non-directories,
1489 * as well as set the setgid bit on a file with a group that the
1490 * process is not a member of.
1492 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1493 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1496 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1497 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1503 node->tn_mode &= ~ALLPERMS;
1504 node->tn_mode |= mode & ALLPERMS;
1506 node->tn_status |= TMPFS_NODE_CHANGED;
1508 MPASS(VOP_ISLOCKED(vp));
1513 /* --------------------------------------------------------------------- */
1516 * Change ownership of the given vnode. At least one of uid or gid must
1517 * be different than VNOVAL. If one is set to that value, the attribute
1519 * Caller should execute tmpfs_update on vp after a successful execution.
1520 * The vnode must be locked on entry and remain locked on exit.
1523 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1527 struct tmpfs_node *node;
1531 MPASS(VOP_ISLOCKED(vp));
1533 node = VP_TO_TMPFS_NODE(vp);
1535 /* Assign default values if they are unknown. */
1536 MPASS(uid != VNOVAL || gid != VNOVAL);
1541 MPASS(uid != VNOVAL && gid != VNOVAL);
1543 /* Disallow this operation if the file system is mounted read-only. */
1544 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1547 /* Immutable or append-only files cannot be modified, either. */
1548 if (node->tn_flags & (IMMUTABLE | APPEND))
1552 * To modify the ownership of a file, must possess VADMIN for that
1555 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1559 * To change the owner of a file, or change the group of a file to a
1560 * group of which we are not a member, the caller must have
1563 if ((uid != node->tn_uid ||
1564 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1565 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1568 ogid = node->tn_gid;
1569 ouid = node->tn_uid;
1574 node->tn_status |= TMPFS_NODE_CHANGED;
1576 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1577 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1578 node->tn_mode &= ~(S_ISUID | S_ISGID);
1581 MPASS(VOP_ISLOCKED(vp));
1586 /* --------------------------------------------------------------------- */
1589 * Change size of the given vnode.
1590 * Caller should execute tmpfs_update on vp after a successful execution.
1591 * The vnode must be locked on entry and remain locked on exit.
1594 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1598 struct tmpfs_node *node;
1600 MPASS(VOP_ISLOCKED(vp));
1602 node = VP_TO_TMPFS_NODE(vp);
1604 /* Decide whether this is a valid operation based on the file type. */
1606 switch (vp->v_type) {
1611 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1620 /* Allow modifications of special files even if in the file
1621 * system is mounted read-only (we are not modifying the
1622 * files themselves, but the objects they represent). */
1626 /* Anything else is unsupported. */
1630 /* Immutable or append-only files cannot be modified, either. */
1631 if (node->tn_flags & (IMMUTABLE | APPEND))
1634 error = tmpfs_truncate(vp, size);
1635 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1636 * for us, as will update tn_status; no need to do that here. */
1638 MPASS(VOP_ISLOCKED(vp));
1643 /* --------------------------------------------------------------------- */
1646 * Change access and modification times of the given vnode.
1647 * Caller should execute tmpfs_update on vp after a successful execution.
1648 * The vnode must be locked on entry and remain locked on exit.
1651 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1652 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l)
1655 struct tmpfs_node *node;
1657 MPASS(VOP_ISLOCKED(vp));
1659 node = VP_TO_TMPFS_NODE(vp);
1661 /* Disallow this operation if the file system is mounted read-only. */
1662 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1665 /* Immutable or append-only files cannot be modified, either. */
1666 if (node->tn_flags & (IMMUTABLE | APPEND))
1669 /* Determine if the user have proper privilege to update time. */
1670 if (vaflags & VA_UTIMES_NULL) {
1671 error = VOP_ACCESS(vp, VADMIN, cred, l);
1673 error = VOP_ACCESS(vp, VWRITE, cred, l);
1675 error = VOP_ACCESS(vp, VADMIN, cred, l);
1679 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1680 node->tn_status |= TMPFS_NODE_ACCESSED;
1682 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1683 node->tn_status |= TMPFS_NODE_MODIFIED;
1685 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1686 node->tn_status |= TMPFS_NODE_MODIFIED;
1688 tmpfs_itimes(vp, atime, mtime);
1690 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1691 node->tn_birthtime = *birthtime;
1692 MPASS(VOP_ISLOCKED(vp));
1697 /* --------------------------------------------------------------------- */
1698 /* Sync timestamps */
1700 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1701 const struct timespec *mod)
1703 struct tmpfs_node *node;
1704 struct timespec now;
1706 node = VP_TO_TMPFS_NODE(vp);
1708 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1709 TMPFS_NODE_CHANGED)) == 0)
1712 vfs_timestamp(&now);
1713 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1716 node->tn_atime = *acc;
1718 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1721 node->tn_mtime = *mod;
1723 if (node->tn_status & TMPFS_NODE_CHANGED) {
1724 node->tn_ctime = now;
1727 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1730 /* --------------------------------------------------------------------- */
1733 tmpfs_update(struct vnode *vp)
1736 tmpfs_itimes(vp, NULL, NULL);
1739 /* --------------------------------------------------------------------- */
1742 tmpfs_truncate(struct vnode *vp, off_t length)
1745 struct tmpfs_node *node;
1747 node = VP_TO_TMPFS_NODE(vp);
1754 if (node->tn_size == length) {
1759 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1762 error = tmpfs_reg_resize(vp, length, FALSE);
1764 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1774 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1776 if (a->td_hash > b->td_hash)
1778 else if (a->td_hash < b->td_hash)
1783 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);