]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/tmpfs/tmpfs_subr.c
MFH
[FreeBSD/FreeBSD.git] / sys / fs / tmpfs / tmpfs_subr.c
1 /*      $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $       */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11  * 2005 program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * Efficient memory file system supporting functions.
37  */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/dirent.h>
44 #include <sys/fnv_hash.h>
45 #include <sys/lock.h>
46 #include <sys/limits.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/random.h>
52 #include <sys/rwlock.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
56 #include <sys/vmmeter.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pageout.h>
63 #include <vm/vm_pager.h>
64 #include <vm/vm_extern.h>
65 #include <vm/swap_pager.h>
66
67 #include <fs/tmpfs/tmpfs.h>
68 #include <fs/tmpfs/tmpfs_fifoops.h>
69 #include <fs/tmpfs/tmpfs_vnops.h>
70
71 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
72     "tmpfs file system");
73
74 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
75
76 static uma_zone_t tmpfs_dirent_pool;
77 static uma_zone_t tmpfs_node_pool;
78 VFS_SMR_DECLARE;
79
80 static int
81 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
82 {
83         struct tmpfs_node *node;
84
85         node = mem;
86         node->tn_gen++;
87         node->tn_size = 0;
88         node->tn_status = 0;
89         node->tn_flags = 0;
90         node->tn_links = 0;
91         node->tn_vnode = NULL;
92         node->tn_vpstate = 0;
93         return (0);
94 }
95
96 static void
97 tmpfs_node_dtor(void *mem, int size, void *arg)
98 {
99         struct tmpfs_node *node;
100
101         node = mem;
102         node->tn_type = VNON;
103 }
104
105 static int
106 tmpfs_node_init(void *mem, int size, int flags)
107 {
108         struct tmpfs_node *node;
109
110         node = mem;
111         node->tn_id = 0;
112         mtx_init(&node->tn_interlock, "tmpfsni", NULL, MTX_DEF);
113         node->tn_gen = arc4random();
114         return (0);
115 }
116
117 static void
118 tmpfs_node_fini(void *mem, int size)
119 {
120         struct tmpfs_node *node;
121
122         node = mem;
123         mtx_destroy(&node->tn_interlock);
124 }
125
126 void
127 tmpfs_subr_init(void)
128 {
129         tmpfs_dirent_pool = uma_zcreate("TMPFS dirent",
130             sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
131             UMA_ALIGN_PTR, 0);
132         tmpfs_node_pool = uma_zcreate("TMPFS node",
133             sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
134             tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
135         VFS_SMR_ZONE_SET(tmpfs_node_pool);
136 }
137
138 void
139 tmpfs_subr_uninit(void)
140 {
141         uma_zdestroy(tmpfs_node_pool);
142         uma_zdestroy(tmpfs_dirent_pool);
143 }
144
145 static int
146 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
147 {
148         int error;
149         long pages, bytes;
150
151         pages = *(long *)arg1;
152         bytes = pages * PAGE_SIZE;
153
154         error = sysctl_handle_long(oidp, &bytes, 0, req);
155         if (error || !req->newptr)
156                 return (error);
157
158         pages = bytes / PAGE_SIZE;
159         if (pages < TMPFS_PAGES_MINRESERVED)
160                 return (EINVAL);
161
162         *(long *)arg1 = pages;
163         return (0);
164 }
165
166 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved,
167     CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RW, &tmpfs_pages_reserved, 0,
168     sysctl_mem_reserved, "L",
169     "Amount of available memory and swap below which tmpfs growth stops");
170
171 static __inline int tmpfs_dirtree_cmp(struct tmpfs_dirent *a,
172     struct tmpfs_dirent *b);
173 RB_PROTOTYPE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);
174
175 size_t
176 tmpfs_mem_avail(void)
177 {
178         vm_ooffset_t avail;
179
180         avail = swap_pager_avail + vm_free_count() - tmpfs_pages_reserved;
181         if (__predict_false(avail < 0))
182                 avail = 0;
183         return (avail);
184 }
185
186 size_t
187 tmpfs_pages_used(struct tmpfs_mount *tmp)
188 {
189         const size_t node_size = sizeof(struct tmpfs_node) +
190             sizeof(struct tmpfs_dirent);
191         size_t meta_pages;
192
193         meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
194             PAGE_SIZE);
195         return (meta_pages + tmp->tm_pages_used);
196 }
197
198 static size_t
199 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
200 {
201         if (tmpfs_mem_avail() < req_pages)
202                 return (0);
203
204         if (tmp->tm_pages_max != ULONG_MAX &&
205             tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
206                         return (0);
207
208         return (1);
209 }
210
211 void
212 tmpfs_ref_node(struct tmpfs_node *node)
213 {
214
215         TMPFS_NODE_LOCK(node);
216         tmpfs_ref_node_locked(node);
217         TMPFS_NODE_UNLOCK(node);
218 }
219
220 void
221 tmpfs_ref_node_locked(struct tmpfs_node *node)
222 {
223
224         TMPFS_NODE_ASSERT_LOCKED(node);
225         KASSERT(node->tn_refcount > 0, ("node %p zero refcount", node));
226         KASSERT(node->tn_refcount < UINT_MAX, ("node %p refcount %u", node,
227             node->tn_refcount));
228         node->tn_refcount++;
229 }
230
231 /*
232  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
233  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
234  * using the credentials of the process 'p'.
235  *
236  * If the node type is set to 'VDIR', then the parent parameter must point
237  * to the parent directory of the node being created.  It may only be NULL
238  * while allocating the root node.
239  *
240  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
241  * specifies the device the node represents.
242  *
243  * If the node type is set to 'VLNK', then the parameter target specifies
244  * the file name of the target file for the symbolic link that is being
245  * created.
246  *
247  * Note that new nodes are retrieved from the available list if it has
248  * items or, if it is empty, from the node pool as long as there is enough
249  * space to create them.
250  *
251  * Returns zero on success or an appropriate error code on failure.
252  */
253 int
254 tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *tmp, enum vtype type,
255     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
256     const char *target, dev_t rdev, struct tmpfs_node **node)
257 {
258         struct tmpfs_node *nnode;
259         vm_object_t obj;
260
261         /* If the root directory of the 'tmp' file system is not yet
262          * allocated, this must be the request to do it. */
263         MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
264
265         MPASS(IFF(type == VLNK, target != NULL));
266         MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
267
268         if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
269                 return (ENOSPC);
270         if (tmpfs_pages_check_avail(tmp, 1) == 0)
271                 return (ENOSPC);
272
273         if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
274                 /*
275                  * When a new tmpfs node is created for fully
276                  * constructed mount point, there must be a parent
277                  * node, which vnode is locked exclusively.  As
278                  * consequence, if the unmount is executing in
279                  * parallel, vflush() cannot reclaim the parent vnode.
280                  * Due to this, the check for MNTK_UNMOUNT flag is not
281                  * racy: if we did not see MNTK_UNMOUNT flag, then tmp
282                  * cannot be destroyed until node construction is
283                  * finished and the parent vnode unlocked.
284                  *
285                  * Tmpfs does not need to instantiate new nodes during
286                  * unmount.
287                  */
288                 return (EBUSY);
289         }
290         if ((mp->mnt_kern_flag & MNT_RDONLY) != 0)
291                 return (EROFS);
292
293         nnode = uma_zalloc_smr(tmpfs_node_pool, M_WAITOK);
294
295         /* Generic initialization. */
296         nnode->tn_type = type;
297         vfs_timestamp(&nnode->tn_atime);
298         nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
299             nnode->tn_atime;
300         nnode->tn_uid = uid;
301         nnode->tn_gid = gid;
302         nnode->tn_mode = mode;
303         nnode->tn_id = alloc_unr64(&tmp->tm_ino_unr);
304         nnode->tn_refcount = 1;
305
306         /* Type-specific initialization. */
307         switch (nnode->tn_type) {
308         case VBLK:
309         case VCHR:
310                 nnode->tn_rdev = rdev;
311                 break;
312
313         case VDIR:
314                 RB_INIT(&nnode->tn_dir.tn_dirhead);
315                 LIST_INIT(&nnode->tn_dir.tn_dupindex);
316                 MPASS(parent != nnode);
317                 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
318                 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
319                 nnode->tn_dir.tn_readdir_lastn = 0;
320                 nnode->tn_dir.tn_readdir_lastp = NULL;
321                 nnode->tn_links++;
322                 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
323                 nnode->tn_dir.tn_parent->tn_links++;
324                 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
325                 break;
326
327         case VFIFO:
328                 /* FALLTHROUGH */
329         case VSOCK:
330                 break;
331
332         case VLNK:
333                 MPASS(strlen(target) < MAXPATHLEN);
334                 nnode->tn_size = strlen(target);
335                 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
336                     M_WAITOK);
337                 memcpy(nnode->tn_link, target, nnode->tn_size);
338                 break;
339
340         case VREG:
341                 obj = nnode->tn_reg.tn_aobj =
342                     vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
343                         NULL /* XXXKIB - tmpfs needs swap reservation */);
344                 VM_OBJECT_WLOCK(obj);
345                 /* OBJ_TMPFS is set together with the setting of vp->v_object */
346                 vm_object_set_flag(obj, OBJ_TMPFS_NODE);
347                 VM_OBJECT_WUNLOCK(obj);
348                 break;
349
350         default:
351                 panic("tmpfs_alloc_node: type %p %d", nnode,
352                     (int)nnode->tn_type);
353         }
354
355         TMPFS_LOCK(tmp);
356         LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
357         nnode->tn_attached = true;
358         tmp->tm_nodes_inuse++;
359         tmp->tm_refcount++;
360         TMPFS_UNLOCK(tmp);
361
362         *node = nnode;
363         return (0);
364 }
365
366 /*
367  * Destroys the node pointed to by node from the file system 'tmp'.
368  * If the node references a directory, no entries are allowed.
369  */
370 void
371 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
372 {
373
374         TMPFS_LOCK(tmp);
375         TMPFS_NODE_LOCK(node);
376         if (!tmpfs_free_node_locked(tmp, node, false)) {
377                 TMPFS_NODE_UNLOCK(node);
378                 TMPFS_UNLOCK(tmp);
379         }
380 }
381
382 bool
383 tmpfs_free_node_locked(struct tmpfs_mount *tmp, struct tmpfs_node *node,
384     bool detach)
385 {
386         vm_object_t uobj;
387
388         TMPFS_MP_ASSERT_LOCKED(tmp);
389         TMPFS_NODE_ASSERT_LOCKED(node);
390         KASSERT(node->tn_refcount > 0, ("node %p refcount zero", node));
391
392         node->tn_refcount--;
393         if (node->tn_attached && (detach || node->tn_refcount == 0)) {
394                 MPASS(tmp->tm_nodes_inuse > 0);
395                 tmp->tm_nodes_inuse--;
396                 LIST_REMOVE(node, tn_entries);
397                 node->tn_attached = false;
398         }
399         if (node->tn_refcount > 0)
400                 return (false);
401
402 #ifdef INVARIANTS
403         MPASS(node->tn_vnode == NULL);
404         MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
405 #endif
406         TMPFS_NODE_UNLOCK(node);
407         TMPFS_UNLOCK(tmp);
408
409         switch (node->tn_type) {
410         case VBLK:
411                 /* FALLTHROUGH */
412         case VCHR:
413                 /* FALLTHROUGH */
414         case VDIR:
415                 /* FALLTHROUGH */
416         case VFIFO:
417                 /* FALLTHROUGH */
418         case VSOCK:
419                 break;
420
421         case VLNK:
422                 free(node->tn_link, M_TMPFSNAME);
423                 break;
424
425         case VREG:
426                 uobj = node->tn_reg.tn_aobj;
427                 if (uobj != NULL) {
428                         if (uobj->size != 0)
429                                 atomic_subtract_long(&tmp->tm_pages_used, uobj->size);
430                         KASSERT((uobj->flags & OBJ_TMPFS) == 0,
431                             ("leaked OBJ_TMPFS node %p vm_obj %p", node, uobj));
432                         vm_object_deallocate(uobj);
433                 }
434                 break;
435
436         default:
437                 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
438         }
439
440         uma_zfree_smr(tmpfs_node_pool, node);
441         TMPFS_LOCK(tmp);
442         tmpfs_free_tmp(tmp);
443         return (true);
444 }
445
446 static __inline uint32_t
447 tmpfs_dirent_hash(const char *name, u_int len)
448 {
449         uint32_t hash;
450
451         hash = fnv_32_buf(name, len, FNV1_32_INIT + len) & TMPFS_DIRCOOKIE_MASK;
452 #ifdef TMPFS_DEBUG_DIRCOOKIE_DUP
453         hash &= 0xf;
454 #endif
455         if (hash < TMPFS_DIRCOOKIE_MIN)
456                 hash += TMPFS_DIRCOOKIE_MIN;
457
458         return (hash);
459 }
460
461 static __inline off_t
462 tmpfs_dirent_cookie(struct tmpfs_dirent *de)
463 {
464         if (de == NULL)
465                 return (TMPFS_DIRCOOKIE_EOF);
466
467         MPASS(de->td_cookie >= TMPFS_DIRCOOKIE_MIN);
468
469         return (de->td_cookie);
470 }
471
472 static __inline boolean_t
473 tmpfs_dirent_dup(struct tmpfs_dirent *de)
474 {
475         return ((de->td_cookie & TMPFS_DIRCOOKIE_DUP) != 0);
476 }
477
478 static __inline boolean_t
479 tmpfs_dirent_duphead(struct tmpfs_dirent *de)
480 {
481         return ((de->td_cookie & TMPFS_DIRCOOKIE_DUPHEAD) != 0);
482 }
483
484 void
485 tmpfs_dirent_init(struct tmpfs_dirent *de, const char *name, u_int namelen)
486 {
487         de->td_hash = de->td_cookie = tmpfs_dirent_hash(name, namelen);
488         memcpy(de->ud.td_name, name, namelen);
489         de->td_namelen = namelen;
490 }
491
492 /*
493  * Allocates a new directory entry for the node node with a name of name.
494  * The new directory entry is returned in *de.
495  *
496  * The link count of node is increased by one to reflect the new object
497  * referencing it.
498  *
499  * Returns zero on success or an appropriate error code on failure.
500  */
501 int
502 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
503     const char *name, u_int len, struct tmpfs_dirent **de)
504 {
505         struct tmpfs_dirent *nde;
506
507         nde = uma_zalloc(tmpfs_dirent_pool, M_WAITOK);
508         nde->td_node = node;
509         if (name != NULL) {
510                 nde->ud.td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
511                 tmpfs_dirent_init(nde, name, len);
512         } else
513                 nde->td_namelen = 0;
514         if (node != NULL)
515                 node->tn_links++;
516
517         *de = nde;
518
519         return 0;
520 }
521
522 /*
523  * Frees a directory entry.  It is the caller's responsibility to destroy
524  * the node referenced by it if needed.
525  *
526  * The link count of node is decreased by one to reflect the removal of an
527  * object that referenced it.  This only happens if 'node_exists' is true;
528  * otherwise the function will not access the node referred to by the
529  * directory entry, as it may already have been released from the outside.
530  */
531 void
532 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
533 {
534         struct tmpfs_node *node;
535
536         node = de->td_node;
537         if (node != NULL) {
538                 MPASS(node->tn_links > 0);
539                 node->tn_links--;
540         }
541         if (!tmpfs_dirent_duphead(de) && de->ud.td_name != NULL)
542                 free(de->ud.td_name, M_TMPFSNAME);
543         uma_zfree(tmpfs_dirent_pool, de);
544 }
545
546 void
547 tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj)
548 {
549
550         ASSERT_VOP_ELOCKED(vp, "tmpfs_destroy_vobject");
551         if (vp->v_type != VREG || obj == NULL)
552                 return;
553
554         VM_OBJECT_WLOCK(obj);
555         VI_LOCK(vp);
556         vm_object_clear_flag(obj, OBJ_TMPFS);
557         obj->un_pager.swp.swp_tmpfs = NULL;
558         if (vp->v_writecount < 0)
559                 vp->v_writecount = 0;
560         VI_UNLOCK(vp);
561         VM_OBJECT_WUNLOCK(obj);
562 }
563
564 /*
565  * Need to clear v_object for insmntque failure.
566  */
567 static void
568 tmpfs_insmntque_dtr(struct vnode *vp, void *dtr_arg)
569 {
570
571         tmpfs_destroy_vobject(vp, vp->v_object);
572         vp->v_object = NULL;
573         vp->v_data = NULL;
574         vp->v_op = &dead_vnodeops;
575         vgone(vp);
576         vput(vp);
577 }
578
579 /*
580  * Allocates a new vnode for the node node or returns a new reference to
581  * an existing one if the node had already a vnode referencing it.  The
582  * resulting locked vnode is returned in *vpp.
583  *
584  * Returns zero on success or an appropriate error code on failure.
585  */
586 int
587 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
588     struct vnode **vpp)
589 {
590         struct vnode *vp;
591         struct tmpfs_mount *tm;
592         vm_object_t object;
593         int error;
594
595         error = 0;
596         tm = VFS_TO_TMPFS(mp);
597         TMPFS_NODE_LOCK(node);
598         tmpfs_ref_node_locked(node);
599 loop:
600         TMPFS_NODE_ASSERT_LOCKED(node);
601         if ((vp = node->tn_vnode) != NULL) {
602                 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
603                 VI_LOCK(vp);
604                 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
605                     (VN_IS_DOOMED(vp) &&
606                      (lkflag & LK_NOWAIT) != 0)) {
607                         VI_UNLOCK(vp);
608                         TMPFS_NODE_UNLOCK(node);
609                         error = ENOENT;
610                         vp = NULL;
611                         goto out;
612                 }
613                 if (VN_IS_DOOMED(vp)) {
614                         VI_UNLOCK(vp);
615                         node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
616                         while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
617                                 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
618                                     0, "tmpfsE", 0);
619                         }
620                         goto loop;
621                 }
622                 TMPFS_NODE_UNLOCK(node);
623                 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
624                 if (error == ENOENT) {
625                         TMPFS_NODE_LOCK(node);
626                         goto loop;
627                 }
628                 if (error != 0) {
629                         vp = NULL;
630                         goto out;
631                 }
632
633                 /*
634                  * Make sure the vnode is still there after
635                  * getting the interlock to avoid racing a free.
636                  */
637                 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
638                         vput(vp);
639                         TMPFS_NODE_LOCK(node);
640                         goto loop;
641                 }
642
643                 goto out;
644         }
645
646         if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
647             (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
648                 TMPFS_NODE_UNLOCK(node);
649                 error = ENOENT;
650                 vp = NULL;
651                 goto out;
652         }
653
654         /*
655          * otherwise lock the vp list while we call getnewvnode
656          * since that can block.
657          */
658         if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
659                 node->tn_vpstate |= TMPFS_VNODE_WANT;
660                 error = msleep((caddr_t) &node->tn_vpstate,
661                     TMPFS_NODE_MTX(node), 0, "tmpfs_alloc_vp", 0);
662                 if (error != 0)
663                         goto out;
664                 goto loop;
665         } else
666                 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
667         
668         TMPFS_NODE_UNLOCK(node);
669
670         /* Get a new vnode and associate it with our node. */
671         error = getnewvnode("tmpfs", mp, VFS_TO_TMPFS(mp)->tm_nonc ?
672             &tmpfs_vnodeop_nonc_entries : &tmpfs_vnodeop_entries, &vp);
673         if (error != 0)
674                 goto unlock;
675         MPASS(vp != NULL);
676
677         /* lkflag is ignored, the lock is exclusive */
678         (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
679
680         vp->v_data = node;
681         vp->v_type = node->tn_type;
682
683         /* Type-specific initialization. */
684         switch (node->tn_type) {
685         case VBLK:
686                 /* FALLTHROUGH */
687         case VCHR:
688                 /* FALLTHROUGH */
689         case VLNK:
690                 /* FALLTHROUGH */
691         case VSOCK:
692                 break;
693         case VFIFO:
694                 vp->v_op = &tmpfs_fifoop_entries;
695                 break;
696         case VREG:
697                 object = node->tn_reg.tn_aobj;
698                 VM_OBJECT_WLOCK(object);
699                 VI_LOCK(vp);
700                 KASSERT(vp->v_object == NULL, ("Not NULL v_object in tmpfs"));
701                 vp->v_object = object;
702                 object->un_pager.swp.swp_tmpfs = vp;
703                 vm_object_set_flag(object, OBJ_TMPFS);
704                 VI_UNLOCK(vp);
705                 VM_OBJECT_WUNLOCK(object);
706                 break;
707         case VDIR:
708                 MPASS(node->tn_dir.tn_parent != NULL);
709                 if (node->tn_dir.tn_parent == node)
710                         vp->v_vflag |= VV_ROOT;
711                 break;
712
713         default:
714                 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
715         }
716         if (vp->v_type != VFIFO)
717                 VN_LOCK_ASHARE(vp);
718
719         error = insmntque1(vp, mp, tmpfs_insmntque_dtr, NULL);
720         if (error != 0)
721                 vp = NULL;
722
723 unlock:
724         TMPFS_NODE_LOCK(node);
725
726         MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
727         node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
728         node->tn_vnode = vp;
729
730         if (node->tn_vpstate & TMPFS_VNODE_WANT) {
731                 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
732                 TMPFS_NODE_UNLOCK(node);
733                 wakeup((caddr_t) &node->tn_vpstate);
734         } else
735                 TMPFS_NODE_UNLOCK(node);
736
737 out:
738         if (error == 0) {
739                 *vpp = vp;
740
741 #ifdef INVARIANTS
742                 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
743                 TMPFS_NODE_LOCK(node);
744                 MPASS(*vpp == node->tn_vnode);
745                 TMPFS_NODE_UNLOCK(node);
746 #endif
747         }
748         tmpfs_free_node(tm, node);
749
750         return (error);
751 }
752
753 /*
754  * Destroys the association between the vnode vp and the node it
755  * references.
756  */
757 void
758 tmpfs_free_vp(struct vnode *vp)
759 {
760         struct tmpfs_node *node;
761
762         node = VP_TO_TMPFS_NODE(vp);
763
764         TMPFS_NODE_ASSERT_LOCKED(node);
765         node->tn_vnode = NULL;
766         if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
767                 wakeup(&node->tn_vnode);
768         node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
769         vp->v_data = NULL;
770 }
771
772 /*
773  * Allocates a new file of type 'type' and adds it to the parent directory
774  * 'dvp'; this addition is done using the component name given in 'cnp'.
775  * The ownership of the new file is automatically assigned based on the
776  * credentials of the caller (through 'cnp'), the group is set based on
777  * the parent directory and the mode is determined from the 'vap' argument.
778  * If successful, *vpp holds a vnode to the newly created file and zero
779  * is returned.  Otherwise *vpp is NULL and the function returns an
780  * appropriate error code.
781  */
782 int
783 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
784     struct componentname *cnp, const char *target)
785 {
786         int error;
787         struct tmpfs_dirent *de;
788         struct tmpfs_mount *tmp;
789         struct tmpfs_node *dnode;
790         struct tmpfs_node *node;
791         struct tmpfs_node *parent;
792
793         ASSERT_VOP_ELOCKED(dvp, "tmpfs_alloc_file");
794         MPASS(cnp->cn_flags & HASBUF);
795
796         tmp = VFS_TO_TMPFS(dvp->v_mount);
797         dnode = VP_TO_TMPFS_DIR(dvp);
798         *vpp = NULL;
799
800         /* If the entry we are creating is a directory, we cannot overflow
801          * the number of links of its parent, because it will get a new
802          * link. */
803         if (vap->va_type == VDIR) {
804                 /* Ensure that we do not overflow the maximum number of links
805                  * imposed by the system. */
806                 MPASS(dnode->tn_links <= TMPFS_LINK_MAX);
807                 if (dnode->tn_links == TMPFS_LINK_MAX) {
808                         return (EMLINK);
809                 }
810
811                 parent = dnode;
812                 MPASS(parent != NULL);
813         } else
814                 parent = NULL;
815
816         /* Allocate a node that represents the new file. */
817         error = tmpfs_alloc_node(dvp->v_mount, tmp, vap->va_type,
818             cnp->cn_cred->cr_uid, dnode->tn_gid, vap->va_mode, parent,
819             target, vap->va_rdev, &node);
820         if (error != 0)
821                 return (error);
822
823         /* Allocate a directory entry that points to the new file. */
824         error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
825             &de);
826         if (error != 0) {
827                 tmpfs_free_node(tmp, node);
828                 return (error);
829         }
830
831         /* Allocate a vnode for the new file. */
832         error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
833         if (error != 0) {
834                 tmpfs_free_dirent(tmp, de);
835                 tmpfs_free_node(tmp, node);
836                 return (error);
837         }
838
839         /* Now that all required items are allocated, we can proceed to
840          * insert the new node into the directory, an operation that
841          * cannot fail. */
842         if (cnp->cn_flags & ISWHITEOUT)
843                 tmpfs_dir_whiteout_remove(dvp, cnp);
844         tmpfs_dir_attach(dvp, de);
845         return (0);
846 }
847
848 struct tmpfs_dirent *
849 tmpfs_dir_first(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
850 {
851         struct tmpfs_dirent *de;
852
853         de = RB_MIN(tmpfs_dir, &dnode->tn_dir.tn_dirhead);
854         dc->tdc_tree = de;
855         if (de != NULL && tmpfs_dirent_duphead(de))
856                 de = LIST_FIRST(&de->ud.td_duphead);
857         dc->tdc_current = de;
858
859         return (dc->tdc_current);
860 }
861
862 struct tmpfs_dirent *
863 tmpfs_dir_next(struct tmpfs_node *dnode, struct tmpfs_dir_cursor *dc)
864 {
865         struct tmpfs_dirent *de;
866
867         MPASS(dc->tdc_tree != NULL);
868         if (tmpfs_dirent_dup(dc->tdc_current)) {
869                 dc->tdc_current = LIST_NEXT(dc->tdc_current, uh.td_dup.entries);
870                 if (dc->tdc_current != NULL)
871                         return (dc->tdc_current);
872         }
873         dc->tdc_tree = dc->tdc_current = RB_NEXT(tmpfs_dir,
874             &dnode->tn_dir.tn_dirhead, dc->tdc_tree);
875         if ((de = dc->tdc_current) != NULL && tmpfs_dirent_duphead(de)) {
876                 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
877                 MPASS(dc->tdc_current != NULL);
878         }
879
880         return (dc->tdc_current);
881 }
882
883 /* Lookup directory entry in RB-Tree. Function may return duphead entry. */
884 static struct tmpfs_dirent *
885 tmpfs_dir_xlookup_hash(struct tmpfs_node *dnode, uint32_t hash)
886 {
887         struct tmpfs_dirent *de, dekey;
888
889         dekey.td_hash = hash;
890         de = RB_FIND(tmpfs_dir, &dnode->tn_dir.tn_dirhead, &dekey);
891         return (de);
892 }
893
894 /* Lookup directory entry by cookie, initialize directory cursor accordingly. */
895 static struct tmpfs_dirent *
896 tmpfs_dir_lookup_cookie(struct tmpfs_node *node, off_t cookie,
897     struct tmpfs_dir_cursor *dc)
898 {
899         struct tmpfs_dir *dirhead = &node->tn_dir.tn_dirhead;
900         struct tmpfs_dirent *de, dekey;
901
902         MPASS(cookie >= TMPFS_DIRCOOKIE_MIN);
903
904         if (cookie == node->tn_dir.tn_readdir_lastn &&
905             (de = node->tn_dir.tn_readdir_lastp) != NULL) {
906                 /* Protect against possible race, tn_readdir_last[pn]
907                  * may be updated with only shared vnode lock held. */
908                 if (cookie == tmpfs_dirent_cookie(de))
909                         goto out;
910         }
911
912         if ((cookie & TMPFS_DIRCOOKIE_DUP) != 0) {
913                 LIST_FOREACH(de, &node->tn_dir.tn_dupindex,
914                     uh.td_dup.index_entries) {
915                         MPASS(tmpfs_dirent_dup(de));
916                         if (de->td_cookie == cookie)
917                                 goto out;
918                         /* dupindex list is sorted. */
919                         if (de->td_cookie < cookie) {
920                                 de = NULL;
921                                 goto out;
922                         }
923                 }
924                 MPASS(de == NULL);
925                 goto out;
926         }
927
928         if ((cookie & TMPFS_DIRCOOKIE_MASK) != cookie) {
929                 de = NULL;
930         } else {
931                 dekey.td_hash = cookie;
932                 /* Recover if direntry for cookie was removed */
933                 de = RB_NFIND(tmpfs_dir, dirhead, &dekey);
934         }
935         dc->tdc_tree = de;
936         dc->tdc_current = de;
937         if (de != NULL && tmpfs_dirent_duphead(de)) {
938                 dc->tdc_current = LIST_FIRST(&de->ud.td_duphead);
939                 MPASS(dc->tdc_current != NULL);
940         }
941         return (dc->tdc_current);
942
943 out:
944         dc->tdc_tree = de;
945         dc->tdc_current = de;
946         if (de != NULL && tmpfs_dirent_dup(de))
947                 dc->tdc_tree = tmpfs_dir_xlookup_hash(node,
948                     de->td_hash);
949         return (dc->tdc_current);
950 }
951
952 /*
953  * Looks for a directory entry in the directory represented by node.
954  * 'cnp' describes the name of the entry to look for.  Note that the .
955  * and .. components are not allowed as they do not physically exist
956  * within directories.
957  *
958  * Returns a pointer to the entry when found, otherwise NULL.
959  */
960 struct tmpfs_dirent *
961 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
962     struct componentname *cnp)
963 {
964         struct tmpfs_dir_duphead *duphead;
965         struct tmpfs_dirent *de;
966         uint32_t hash;
967
968         MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
969         MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
970             cnp->cn_nameptr[1] == '.')));
971         TMPFS_VALIDATE_DIR(node);
972
973         hash = tmpfs_dirent_hash(cnp->cn_nameptr, cnp->cn_namelen);
974         de = tmpfs_dir_xlookup_hash(node, hash);
975         if (de != NULL && tmpfs_dirent_duphead(de)) {
976                 duphead = &de->ud.td_duphead;
977                 LIST_FOREACH(de, duphead, uh.td_dup.entries) {
978                         if (TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
979                             cnp->cn_namelen))
980                                 break;
981                 }
982         } else if (de != NULL) {
983                 if (!TMPFS_DIRENT_MATCHES(de, cnp->cn_nameptr,
984                     cnp->cn_namelen))
985                         de = NULL;
986         }
987         if (de != NULL && f != NULL && de->td_node != f)
988                 de = NULL;
989
990         return (de);
991 }
992
993 /*
994  * Attach duplicate-cookie directory entry nde to dnode and insert to dupindex
995  * list, allocate new cookie value.
996  */
997 static void
998 tmpfs_dir_attach_dup(struct tmpfs_node *dnode,
999     struct tmpfs_dir_duphead *duphead, struct tmpfs_dirent *nde)
1000 {
1001         struct tmpfs_dir_duphead *dupindex;
1002         struct tmpfs_dirent *de, *pde;
1003
1004         dupindex = &dnode->tn_dir.tn_dupindex;
1005         de = LIST_FIRST(dupindex);
1006         if (de == NULL || de->td_cookie < TMPFS_DIRCOOKIE_DUP_MAX) {
1007                 if (de == NULL)
1008                         nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
1009                 else
1010                         nde->td_cookie = de->td_cookie + 1;
1011                 MPASS(tmpfs_dirent_dup(nde));
1012                 LIST_INSERT_HEAD(dupindex, nde, uh.td_dup.index_entries);
1013                 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1014                 return;
1015         }
1016
1017         /*
1018          * Cookie numbers are near exhaustion. Scan dupindex list for unused
1019          * numbers. dupindex list is sorted in descending order. Keep it so
1020          * after inserting nde.
1021          */
1022         while (1) {
1023                 pde = de;
1024                 de = LIST_NEXT(de, uh.td_dup.index_entries);
1025                 if (de == NULL && pde->td_cookie != TMPFS_DIRCOOKIE_DUP_MIN) {
1026                         /*
1027                          * Last element of the index doesn't have minimal cookie
1028                          * value, use it.
1029                          */
1030                         nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MIN;
1031                         LIST_INSERT_AFTER(pde, nde, uh.td_dup.index_entries);
1032                         LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1033                         return;
1034                 } else if (de == NULL) {
1035                         /*
1036                          * We are so lucky have 2^30 hash duplicates in single
1037                          * directory :) Return largest possible cookie value.
1038                          * It should be fine except possible issues with
1039                          * VOP_READDIR restart.
1040                          */
1041                         nde->td_cookie = TMPFS_DIRCOOKIE_DUP_MAX;
1042                         LIST_INSERT_HEAD(dupindex, nde,
1043                             uh.td_dup.index_entries);
1044                         LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1045                         return;
1046                 }
1047                 if (de->td_cookie + 1 == pde->td_cookie ||
1048                     de->td_cookie >= TMPFS_DIRCOOKIE_DUP_MAX)
1049                         continue;       /* No hole or invalid cookie. */
1050                 nde->td_cookie = de->td_cookie + 1;
1051                 MPASS(tmpfs_dirent_dup(nde));
1052                 MPASS(pde->td_cookie > nde->td_cookie);
1053                 MPASS(nde->td_cookie > de->td_cookie);
1054                 LIST_INSERT_BEFORE(de, nde, uh.td_dup.index_entries);
1055                 LIST_INSERT_HEAD(duphead, nde, uh.td_dup.entries);
1056                 return;
1057         }
1058 }
1059
1060 /*
1061  * Attaches the directory entry de to the directory represented by vp.
1062  * Note that this does not change the link count of the node pointed by
1063  * the directory entry, as this is done by tmpfs_alloc_dirent.
1064  */
1065 void
1066 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
1067 {
1068         struct tmpfs_node *dnode;
1069         struct tmpfs_dirent *xde, *nde;
1070
1071         ASSERT_VOP_ELOCKED(vp, __func__);
1072         MPASS(de->td_namelen > 0);
1073         MPASS(de->td_hash >= TMPFS_DIRCOOKIE_MIN);
1074         MPASS(de->td_cookie == de->td_hash);
1075
1076         dnode = VP_TO_TMPFS_DIR(vp);
1077         dnode->tn_dir.tn_readdir_lastn = 0;
1078         dnode->tn_dir.tn_readdir_lastp = NULL;
1079
1080         MPASS(!tmpfs_dirent_dup(de));
1081         xde = RB_INSERT(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1082         if (xde != NULL && tmpfs_dirent_duphead(xde))
1083                 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1084         else if (xde != NULL) {
1085                 /*
1086                  * Allocate new duphead. Swap xde with duphead to avoid
1087                  * adding/removing elements with the same hash.
1088                  */
1089                 MPASS(!tmpfs_dirent_dup(xde));
1090                 tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), NULL, NULL, 0,
1091                     &nde);
1092                 /* *nde = *xde; XXX gcc 4.2.1 may generate invalid code. */
1093                 memcpy(nde, xde, sizeof(*xde));
1094                 xde->td_cookie |= TMPFS_DIRCOOKIE_DUPHEAD;
1095                 LIST_INIT(&xde->ud.td_duphead);
1096                 xde->td_namelen = 0;
1097                 xde->td_node = NULL;
1098                 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, nde);
1099                 tmpfs_dir_attach_dup(dnode, &xde->ud.td_duphead, de);
1100         }
1101         dnode->tn_size += sizeof(struct tmpfs_dirent);
1102         dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1103             TMPFS_NODE_MODIFIED;
1104         tmpfs_update(vp);
1105 }
1106
1107 /*
1108  * Detaches the directory entry de from the directory represented by vp.
1109  * Note that this does not change the link count of the node pointed by
1110  * the directory entry, as this is done by tmpfs_free_dirent.
1111  */
1112 void
1113 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
1114 {
1115         struct tmpfs_mount *tmp;
1116         struct tmpfs_dir *head;
1117         struct tmpfs_node *dnode;
1118         struct tmpfs_dirent *xde;
1119
1120         ASSERT_VOP_ELOCKED(vp, __func__);
1121
1122         dnode = VP_TO_TMPFS_DIR(vp);
1123         head = &dnode->tn_dir.tn_dirhead;
1124         dnode->tn_dir.tn_readdir_lastn = 0;
1125         dnode->tn_dir.tn_readdir_lastp = NULL;
1126
1127         if (tmpfs_dirent_dup(de)) {
1128                 /* Remove duphead if de was last entry. */
1129                 if (LIST_NEXT(de, uh.td_dup.entries) == NULL) {
1130                         xde = tmpfs_dir_xlookup_hash(dnode, de->td_hash);
1131                         MPASS(tmpfs_dirent_duphead(xde));
1132                 } else
1133                         xde = NULL;
1134                 LIST_REMOVE(de, uh.td_dup.entries);
1135                 LIST_REMOVE(de, uh.td_dup.index_entries);
1136                 if (xde != NULL) {
1137                         if (LIST_EMPTY(&xde->ud.td_duphead)) {
1138                                 RB_REMOVE(tmpfs_dir, head, xde);
1139                                 tmp = VFS_TO_TMPFS(vp->v_mount);
1140                                 MPASS(xde->td_node == NULL);
1141                                 tmpfs_free_dirent(tmp, xde);
1142                         }
1143                 }
1144                 de->td_cookie = de->td_hash;
1145         } else
1146                 RB_REMOVE(tmpfs_dir, head, de);
1147
1148         dnode->tn_size -= sizeof(struct tmpfs_dirent);
1149         dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1150             TMPFS_NODE_MODIFIED;
1151         tmpfs_update(vp);
1152 }
1153
1154 void
1155 tmpfs_dir_destroy(struct tmpfs_mount *tmp, struct tmpfs_node *dnode)
1156 {
1157         struct tmpfs_dirent *de, *dde, *nde;
1158
1159         RB_FOREACH_SAFE(de, tmpfs_dir, &dnode->tn_dir.tn_dirhead, nde) {
1160                 RB_REMOVE(tmpfs_dir, &dnode->tn_dir.tn_dirhead, de);
1161                 /* Node may already be destroyed. */
1162                 de->td_node = NULL;
1163                 if (tmpfs_dirent_duphead(de)) {
1164                         while ((dde = LIST_FIRST(&de->ud.td_duphead)) != NULL) {
1165                                 LIST_REMOVE(dde, uh.td_dup.entries);
1166                                 dde->td_node = NULL;
1167                                 tmpfs_free_dirent(tmp, dde);
1168                         }
1169                 }
1170                 tmpfs_free_dirent(tmp, de);
1171         }
1172 }
1173
1174 /*
1175  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
1176  * directory and returns it in the uio space.  The function returns 0
1177  * on success, -1 if there was not enough space in the uio structure to
1178  * hold the directory entry or an appropriate error code if another
1179  * error happens.
1180  */
1181 static int
1182 tmpfs_dir_getdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1183     struct uio *uio)
1184 {
1185         int error;
1186         struct dirent dent;
1187
1188         TMPFS_VALIDATE_DIR(node);
1189         MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
1190
1191         dent.d_fileno = node->tn_id;
1192         dent.d_type = DT_DIR;
1193         dent.d_namlen = 1;
1194         dent.d_name[0] = '.';
1195         dent.d_reclen = GENERIC_DIRSIZ(&dent);
1196         dirent_terminate(&dent);
1197
1198         if (dent.d_reclen > uio->uio_resid)
1199                 error = EJUSTRETURN;
1200         else
1201                 error = uiomove(&dent, dent.d_reclen, uio);
1202
1203         tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1204
1205         return (error);
1206 }
1207
1208 /*
1209  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
1210  * directory and returns it in the uio space.  The function returns 0
1211  * on success, -1 if there was not enough space in the uio structure to
1212  * hold the directory entry or an appropriate error code if another
1213  * error happens.
1214  */
1215 static int
1216 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tm, struct tmpfs_node *node,
1217     struct uio *uio)
1218 {
1219         struct tmpfs_node *parent;
1220         struct dirent dent;
1221         int error;
1222
1223         TMPFS_VALIDATE_DIR(node);
1224         MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
1225
1226         /*
1227          * Return ENOENT if the current node is already removed.
1228          */
1229         TMPFS_ASSERT_LOCKED(node);
1230         parent = node->tn_dir.tn_parent;
1231         if (parent == NULL)
1232                 return (ENOENT);
1233
1234         TMPFS_NODE_LOCK(parent);
1235         dent.d_fileno = parent->tn_id;
1236         TMPFS_NODE_UNLOCK(parent);
1237
1238         dent.d_type = DT_DIR;
1239         dent.d_namlen = 2;
1240         dent.d_name[0] = '.';
1241         dent.d_name[1] = '.';
1242         dent.d_reclen = GENERIC_DIRSIZ(&dent);
1243         dirent_terminate(&dent);
1244
1245         if (dent.d_reclen > uio->uio_resid)
1246                 error = EJUSTRETURN;
1247         else
1248                 error = uiomove(&dent, dent.d_reclen, uio);
1249
1250         tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1251
1252         return (error);
1253 }
1254
1255 /*
1256  * Helper function for tmpfs_readdir.  Returns as much directory entries
1257  * as can fit in the uio space.  The read starts at uio->uio_offset.
1258  * The function returns 0 on success, -1 if there was not enough space
1259  * in the uio structure to hold the directory entry or an appropriate
1260  * error code if another error happens.
1261  */
1262 int
1263 tmpfs_dir_getdents(struct tmpfs_mount *tm, struct tmpfs_node *node,
1264     struct uio *uio, int maxcookies, u_long *cookies, int *ncookies)
1265 {
1266         struct tmpfs_dir_cursor dc;
1267         struct tmpfs_dirent *de;
1268         off_t off;
1269         int error;
1270
1271         TMPFS_VALIDATE_DIR(node);
1272
1273         off = 0;
1274
1275         /*
1276          * Lookup the node from the current offset.  The starting offset of
1277          * 0 will lookup both '.' and '..', and then the first real entry,
1278          * or EOF if there are none.  Then find all entries for the dir that
1279          * fit into the buffer.  Once no more entries are found (de == NULL),
1280          * the offset is set to TMPFS_DIRCOOKIE_EOF, which will cause the next
1281          * call to return 0.
1282          */
1283         switch (uio->uio_offset) {
1284         case TMPFS_DIRCOOKIE_DOT:
1285                 error = tmpfs_dir_getdotdent(tm, node, uio);
1286                 if (error != 0)
1287                         return (error);
1288                 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
1289                 if (cookies != NULL)
1290                         cookies[(*ncookies)++] = off = uio->uio_offset;
1291                 /* FALLTHROUGH */
1292         case TMPFS_DIRCOOKIE_DOTDOT:
1293                 error = tmpfs_dir_getdotdotdent(tm, node, uio);
1294                 if (error != 0)
1295                         return (error);
1296                 de = tmpfs_dir_first(node, &dc);
1297                 uio->uio_offset = tmpfs_dirent_cookie(de);
1298                 if (cookies != NULL)
1299                         cookies[(*ncookies)++] = off = uio->uio_offset;
1300                 /* EOF. */
1301                 if (de == NULL)
1302                         return (0);
1303                 break;
1304         case TMPFS_DIRCOOKIE_EOF:
1305                 return (0);
1306         default:
1307                 de = tmpfs_dir_lookup_cookie(node, uio->uio_offset, &dc);
1308                 if (de == NULL)
1309                         return (EINVAL);
1310                 if (cookies != NULL)
1311                         off = tmpfs_dirent_cookie(de);
1312         }
1313
1314         /* Read as much entries as possible; i.e., until we reach the end of
1315          * the directory or we exhaust uio space. */
1316         do {
1317                 struct dirent d;
1318
1319                 /* Create a dirent structure representing the current
1320                  * tmpfs_node and fill it. */
1321                 if (de->td_node == NULL) {
1322                         d.d_fileno = 1;
1323                         d.d_type = DT_WHT;
1324                 } else {
1325                         d.d_fileno = de->td_node->tn_id;
1326                         switch (de->td_node->tn_type) {
1327                         case VBLK:
1328                                 d.d_type = DT_BLK;
1329                                 break;
1330
1331                         case VCHR:
1332                                 d.d_type = DT_CHR;
1333                                 break;
1334
1335                         case VDIR:
1336                                 d.d_type = DT_DIR;
1337                                 break;
1338
1339                         case VFIFO:
1340                                 d.d_type = DT_FIFO;
1341                                 break;
1342
1343                         case VLNK:
1344                                 d.d_type = DT_LNK;
1345                                 break;
1346
1347                         case VREG:
1348                                 d.d_type = DT_REG;
1349                                 break;
1350
1351                         case VSOCK:
1352                                 d.d_type = DT_SOCK;
1353                                 break;
1354
1355                         default:
1356                                 panic("tmpfs_dir_getdents: type %p %d",
1357                                     de->td_node, (int)de->td_node->tn_type);
1358                         }
1359                 }
1360                 d.d_namlen = de->td_namelen;
1361                 MPASS(de->td_namelen < sizeof(d.d_name));
1362                 (void)memcpy(d.d_name, de->ud.td_name, de->td_namelen);
1363                 d.d_reclen = GENERIC_DIRSIZ(&d);
1364                 dirent_terminate(&d);
1365
1366                 /* Stop reading if the directory entry we are treating is
1367                  * bigger than the amount of data that can be returned. */
1368                 if (d.d_reclen > uio->uio_resid) {
1369                         error = EJUSTRETURN;
1370                         break;
1371                 }
1372
1373                 /* Copy the new dirent structure into the output buffer and
1374                  * advance pointers. */
1375                 error = uiomove(&d, d.d_reclen, uio);
1376                 if (error == 0) {
1377                         de = tmpfs_dir_next(node, &dc);
1378                         if (cookies != NULL) {
1379                                 off = tmpfs_dirent_cookie(de);
1380                                 MPASS(*ncookies < maxcookies);
1381                                 cookies[(*ncookies)++] = off;
1382                         }
1383                 }
1384         } while (error == 0 && uio->uio_resid > 0 && de != NULL);
1385
1386         /* Skip setting off when using cookies as it is already done above. */
1387         if (cookies == NULL)
1388                 off = tmpfs_dirent_cookie(de);
1389
1390         /* Update the offset and cache. */
1391         uio->uio_offset = off;
1392         node->tn_dir.tn_readdir_lastn = off;
1393         node->tn_dir.tn_readdir_lastp = de;
1394
1395         tmpfs_set_status(tm, node, TMPFS_NODE_ACCESSED);
1396         return error;
1397 }
1398
1399 int
1400 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
1401 {
1402         struct tmpfs_dirent *de;
1403         int error;
1404
1405         error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
1406             cnp->cn_nameptr, cnp->cn_namelen, &de);
1407         if (error != 0)
1408                 return (error);
1409         tmpfs_dir_attach(dvp, de);
1410         return (0);
1411 }
1412
1413 void
1414 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
1415 {
1416         struct tmpfs_dirent *de;
1417
1418         de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1419         MPASS(de != NULL && de->td_node == NULL);
1420         tmpfs_dir_detach(dvp, de);
1421         tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de);
1422 }
1423
1424 /*
1425  * Resizes the aobj associated with the regular file pointed to by 'vp' to the
1426  * size 'newsize'.  'vp' must point to a vnode that represents a regular file.
1427  * 'newsize' must be positive.
1428  *
1429  * Returns zero on success or an appropriate error code on failure.
1430  */
1431 int
1432 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
1433 {
1434         struct tmpfs_mount *tmp;
1435         struct tmpfs_node *node;
1436         vm_object_t uobj;
1437         vm_page_t m;
1438         vm_pindex_t idx, newpages, oldpages;
1439         off_t oldsize;
1440         int base, rv;
1441
1442         MPASS(vp->v_type == VREG);
1443         MPASS(newsize >= 0);
1444
1445         node = VP_TO_TMPFS_NODE(vp);
1446         uobj = node->tn_reg.tn_aobj;
1447         tmp = VFS_TO_TMPFS(vp->v_mount);
1448
1449         /*
1450          * Convert the old and new sizes to the number of pages needed to
1451          * store them.  It may happen that we do not need to do anything
1452          * because the last allocated page can accommodate the change on
1453          * its own.
1454          */
1455         oldsize = node->tn_size;
1456         oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1457         MPASS(oldpages == uobj->size);
1458         newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1459
1460         if (__predict_true(newpages == oldpages && newsize >= oldsize)) {
1461                 node->tn_size = newsize;
1462                 return (0);
1463         }
1464
1465         if (newpages > oldpages &&
1466             tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1467                 return (ENOSPC);
1468
1469         VM_OBJECT_WLOCK(uobj);
1470         if (newsize < oldsize) {
1471                 /*
1472                  * Zero the truncated part of the last page.
1473                  */
1474                 base = newsize & PAGE_MASK;
1475                 if (base != 0) {
1476                         idx = OFF_TO_IDX(newsize);
1477 retry:
1478                         m = vm_page_grab(uobj, idx, VM_ALLOC_NOCREAT);
1479                         if (m != NULL) {
1480                                 MPASS(vm_page_all_valid(m));
1481                         } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1482                                 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL |
1483                                     VM_ALLOC_WAITFAIL);
1484                                 if (m == NULL)
1485                                         goto retry;
1486                                 vm_object_pip_add(uobj, 1);
1487                                 VM_OBJECT_WUNLOCK(uobj);
1488                                 rv = vm_pager_get_pages(uobj, &m, 1, NULL,
1489                                     NULL);
1490                                 VM_OBJECT_WLOCK(uobj);
1491                                 vm_object_pip_wakeup(uobj);
1492                                 if (rv == VM_PAGER_OK) {
1493                                         /*
1494                                          * Since the page was not resident,
1495                                          * and therefore not recently
1496                                          * accessed, immediately enqueue it
1497                                          * for asynchronous laundering.  The
1498                                          * current operation is not regarded
1499                                          * as an access.
1500                                          */
1501                                         vm_page_launder(m);
1502                                 } else {
1503                                         vm_page_free(m);
1504                                         if (ignerr)
1505                                                 m = NULL;
1506                                         else {
1507                                                 VM_OBJECT_WUNLOCK(uobj);
1508                                                 return (EIO);
1509                                         }
1510                                 }
1511                         }
1512                         if (m != NULL) {
1513                                 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1514                                 vm_page_set_dirty(m);
1515                                 vm_page_xunbusy(m);
1516                         }
1517                 }
1518
1519                 /*
1520                  * Release any swap space and free any whole pages.
1521                  */
1522                 if (newpages < oldpages)
1523                         vm_object_page_remove(uobj, newpages, 0, 0);
1524         }
1525         uobj->size = newpages;
1526         VM_OBJECT_WUNLOCK(uobj);
1527
1528         atomic_add_long(&tmp->tm_pages_used, newpages - oldpages);
1529
1530         node->tn_size = newsize;
1531         return (0);
1532 }
1533
1534 void
1535 tmpfs_check_mtime(struct vnode *vp)
1536 {
1537         struct tmpfs_node *node;
1538         struct vm_object *obj;
1539
1540         ASSERT_VOP_ELOCKED(vp, "check_mtime");
1541         if (vp->v_type != VREG)
1542                 return;
1543         obj = vp->v_object;
1544         KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
1545             (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
1546         /* unlocked read */
1547         if (obj->generation != obj->cleangeneration) {
1548                 VM_OBJECT_WLOCK(obj);
1549                 if (obj->generation != obj->cleangeneration) {
1550                         obj->cleangeneration = obj->generation;
1551                         node = VP_TO_TMPFS_NODE(vp);
1552                         node->tn_status |= TMPFS_NODE_MODIFIED |
1553                             TMPFS_NODE_CHANGED;
1554                 }
1555                 VM_OBJECT_WUNLOCK(obj);
1556         }
1557 }
1558
1559 /*
1560  * Change flags of the given vnode.
1561  * Caller should execute tmpfs_update on vp after a successful execution.
1562  * The vnode must be locked on entry and remain locked on exit.
1563  */
1564 int
1565 tmpfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
1566     struct thread *p)
1567 {
1568         int error;
1569         struct tmpfs_node *node;
1570
1571         ASSERT_VOP_ELOCKED(vp, "chflags");
1572
1573         node = VP_TO_TMPFS_NODE(vp);
1574
1575         if ((flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE | SF_NOUNLINK |
1576             UF_APPEND | UF_ARCHIVE | UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP |
1577             UF_NOUNLINK | UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
1578             UF_SPARSE | UF_SYSTEM)) != 0)
1579                 return (EOPNOTSUPP);
1580
1581         /* Disallow this operation if the file system is mounted read-only. */
1582         if (vp->v_mount->mnt_flag & MNT_RDONLY)
1583                 return EROFS;
1584
1585         /*
1586          * Callers may only modify the file flags on objects they
1587          * have VADMIN rights for.
1588          */
1589         if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1590                 return (error);
1591         /*
1592          * Unprivileged processes are not permitted to unset system
1593          * flags, or modify flags if any system flags are set.
1594          */
1595         if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
1596                 if (node->tn_flags &
1597                     (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1598                         error = securelevel_gt(cred, 0);
1599                         if (error)
1600                                 return (error);
1601                 }
1602         } else {
1603                 if (node->tn_flags &
1604                     (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1605                     ((flags ^ node->tn_flags) & SF_SETTABLE))
1606                         return (EPERM);
1607         }
1608         node->tn_flags = flags;
1609         node->tn_status |= TMPFS_NODE_CHANGED;
1610
1611         ASSERT_VOP_ELOCKED(vp, "chflags2");
1612
1613         return (0);
1614 }
1615
1616 /*
1617  * Change access mode on the given vnode.
1618  * Caller should execute tmpfs_update on vp after a successful execution.
1619  * The vnode must be locked on entry and remain locked on exit.
1620  */
1621 int
1622 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1623 {
1624         int error;
1625         struct tmpfs_node *node;
1626         mode_t newmode;
1627
1628         ASSERT_VOP_ELOCKED(vp, "chmod");
1629         ASSERT_VOP_IN_SEQC(vp);
1630
1631         node = VP_TO_TMPFS_NODE(vp);
1632
1633         /* Disallow this operation if the file system is mounted read-only. */
1634         if (vp->v_mount->mnt_flag & MNT_RDONLY)
1635                 return EROFS;
1636
1637         /* Immutable or append-only files cannot be modified, either. */
1638         if (node->tn_flags & (IMMUTABLE | APPEND))
1639                 return EPERM;
1640
1641         /*
1642          * To modify the permissions on a file, must possess VADMIN
1643          * for that file.
1644          */
1645         if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1646                 return (error);
1647
1648         /*
1649          * Privileged processes may set the sticky bit on non-directories,
1650          * as well as set the setgid bit on a file with a group that the
1651          * process is not a member of.
1652          */
1653         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1654                 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
1655                         return (EFTYPE);
1656         }
1657         if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1658                 error = priv_check_cred(cred, PRIV_VFS_SETGID);
1659                 if (error)
1660                         return (error);
1661         }
1662
1663         newmode = node->tn_mode & ~ALLPERMS;
1664         newmode |= mode & ALLPERMS;
1665         atomic_store_short(&node->tn_mode, newmode);
1666
1667         node->tn_status |= TMPFS_NODE_CHANGED;
1668
1669         ASSERT_VOP_ELOCKED(vp, "chmod2");
1670
1671         return (0);
1672 }
1673
1674 /*
1675  * Change ownership of the given vnode.  At least one of uid or gid must
1676  * be different than VNOVAL.  If one is set to that value, the attribute
1677  * is unchanged.
1678  * Caller should execute tmpfs_update on vp after a successful execution.
1679  * The vnode must be locked on entry and remain locked on exit.
1680  */
1681 int
1682 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1683     struct thread *p)
1684 {
1685         int error;
1686         struct tmpfs_node *node;
1687         uid_t ouid;
1688         gid_t ogid;
1689         mode_t newmode;
1690
1691         ASSERT_VOP_ELOCKED(vp, "chown");
1692         ASSERT_VOP_IN_SEQC(vp);
1693
1694         node = VP_TO_TMPFS_NODE(vp);
1695
1696         /* Assign default values if they are unknown. */
1697         MPASS(uid != VNOVAL || gid != VNOVAL);
1698         if (uid == VNOVAL)
1699                 uid = node->tn_uid;
1700         if (gid == VNOVAL)
1701                 gid = node->tn_gid;
1702         MPASS(uid != VNOVAL && gid != VNOVAL);
1703
1704         /* Disallow this operation if the file system is mounted read-only. */
1705         if (vp->v_mount->mnt_flag & MNT_RDONLY)
1706                 return EROFS;
1707
1708         /* Immutable or append-only files cannot be modified, either. */
1709         if (node->tn_flags & (IMMUTABLE | APPEND))
1710                 return EPERM;
1711
1712         /*
1713          * To modify the ownership of a file, must possess VADMIN for that
1714          * file.
1715          */
1716         if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1717                 return (error);
1718
1719         /*
1720          * To change the owner of a file, or change the group of a file to a
1721          * group of which we are not a member, the caller must have
1722          * privilege.
1723          */
1724         if ((uid != node->tn_uid ||
1725             (gid != node->tn_gid && !groupmember(gid, cred))) &&
1726             (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
1727                 return (error);
1728
1729         ogid = node->tn_gid;
1730         ouid = node->tn_uid;
1731
1732         node->tn_uid = uid;
1733         node->tn_gid = gid;
1734
1735         node->tn_status |= TMPFS_NODE_CHANGED;
1736
1737         if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1738                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
1739                         newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
1740                         atomic_store_short(&node->tn_mode, newmode);
1741                 }
1742         }
1743
1744         ASSERT_VOP_ELOCKED(vp, "chown2");
1745
1746         return (0);
1747 }
1748
1749 /*
1750  * Change size of the given vnode.
1751  * Caller should execute tmpfs_update on vp after a successful execution.
1752  * The vnode must be locked on entry and remain locked on exit.
1753  */
1754 int
1755 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1756     struct thread *p)
1757 {
1758         int error;
1759         struct tmpfs_node *node;
1760
1761         ASSERT_VOP_ELOCKED(vp, "chsize");
1762
1763         node = VP_TO_TMPFS_NODE(vp);
1764
1765         /* Decide whether this is a valid operation based on the file type. */
1766         error = 0;
1767         switch (vp->v_type) {
1768         case VDIR:
1769                 return EISDIR;
1770
1771         case VREG:
1772                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1773                         return EROFS;
1774                 break;
1775
1776         case VBLK:
1777                 /* FALLTHROUGH */
1778         case VCHR:
1779                 /* FALLTHROUGH */
1780         case VFIFO:
1781                 /* Allow modifications of special files even if in the file
1782                  * system is mounted read-only (we are not modifying the
1783                  * files themselves, but the objects they represent). */
1784                 return 0;
1785
1786         default:
1787                 /* Anything else is unsupported. */
1788                 return EOPNOTSUPP;
1789         }
1790
1791         /* Immutable or append-only files cannot be modified, either. */
1792         if (node->tn_flags & (IMMUTABLE | APPEND))
1793                 return EPERM;
1794
1795         error = tmpfs_truncate(vp, size);
1796         /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1797          * for us, as will update tn_status; no need to do that here. */
1798
1799         ASSERT_VOP_ELOCKED(vp, "chsize2");
1800
1801         return (error);
1802 }
1803
1804 /*
1805  * Change access and modification times of the given vnode.
1806  * Caller should execute tmpfs_update on vp after a successful execution.
1807  * The vnode must be locked on entry and remain locked on exit.
1808  */
1809 int
1810 tmpfs_chtimes(struct vnode *vp, struct vattr *vap,
1811     struct ucred *cred, struct thread *l)
1812 {
1813         int error;
1814         struct tmpfs_node *node;
1815
1816         ASSERT_VOP_ELOCKED(vp, "chtimes");
1817
1818         node = VP_TO_TMPFS_NODE(vp);
1819
1820         /* Disallow this operation if the file system is mounted read-only. */
1821         if (vp->v_mount->mnt_flag & MNT_RDONLY)
1822                 return EROFS;
1823
1824         /* Immutable or append-only files cannot be modified, either. */
1825         if (node->tn_flags & (IMMUTABLE | APPEND))
1826                 return EPERM;
1827
1828         error = vn_utimes_perm(vp, vap, cred, l);
1829         if (error != 0)
1830                 return (error);
1831
1832         if (vap->va_atime.tv_sec != VNOVAL)
1833                 node->tn_status |= TMPFS_NODE_ACCESSED;
1834
1835         if (vap->va_mtime.tv_sec != VNOVAL)
1836                 node->tn_status |= TMPFS_NODE_MODIFIED;
1837
1838         if (vap->va_birthtime.tv_sec != VNOVAL)
1839                 node->tn_status |= TMPFS_NODE_MODIFIED;
1840
1841         tmpfs_itimes(vp, &vap->va_atime, &vap->va_mtime);
1842
1843         if (vap->va_birthtime.tv_sec != VNOVAL)
1844                 node->tn_birthtime = vap->va_birthtime;
1845         ASSERT_VOP_ELOCKED(vp, "chtimes2");
1846
1847         return (0);
1848 }
1849
1850 void
1851 tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, int status)
1852 {
1853
1854         if ((node->tn_status & status) == status || tm->tm_ronly)
1855                 return;
1856         TMPFS_NODE_LOCK(node);
1857         node->tn_status |= status;
1858         TMPFS_NODE_UNLOCK(node);
1859 }
1860
1861 /* Sync timestamps */
1862 void
1863 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1864     const struct timespec *mod)
1865 {
1866         struct tmpfs_node *node;
1867         struct timespec now;
1868
1869         ASSERT_VOP_LOCKED(vp, "tmpfs_itimes");
1870         node = VP_TO_TMPFS_NODE(vp);
1871
1872         if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1873             TMPFS_NODE_CHANGED)) == 0)
1874                 return;
1875
1876         vfs_timestamp(&now);
1877         TMPFS_NODE_LOCK(node);
1878         if (node->tn_status & TMPFS_NODE_ACCESSED) {
1879                 if (acc == NULL)
1880                          acc = &now;
1881                 node->tn_atime = *acc;
1882         }
1883         if (node->tn_status & TMPFS_NODE_MODIFIED) {
1884                 if (mod == NULL)
1885                         mod = &now;
1886                 node->tn_mtime = *mod;
1887         }
1888         if (node->tn_status & TMPFS_NODE_CHANGED)
1889                 node->tn_ctime = now;
1890         node->tn_status &= ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1891             TMPFS_NODE_CHANGED);
1892         TMPFS_NODE_UNLOCK(node);
1893
1894         /* XXX: FIX? The entropy here is desirable, but the harvesting may be expensive */
1895         random_harvest_queue(node, sizeof(*node), RANDOM_FS_ATIME);
1896 }
1897
1898 int
1899 tmpfs_truncate(struct vnode *vp, off_t length)
1900 {
1901         int error;
1902         struct tmpfs_node *node;
1903
1904         node = VP_TO_TMPFS_NODE(vp);
1905
1906         if (length < 0) {
1907                 error = EINVAL;
1908                 goto out;
1909         }
1910
1911         if (node->tn_size == length) {
1912                 error = 0;
1913                 goto out;
1914         }
1915
1916         if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1917                 return (EFBIG);
1918
1919         error = tmpfs_reg_resize(vp, length, FALSE);
1920         if (error == 0)
1921                 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1922
1923 out:
1924         tmpfs_update(vp);
1925
1926         return (error);
1927 }
1928
1929 static __inline int
1930 tmpfs_dirtree_cmp(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1931 {
1932         if (a->td_hash > b->td_hash)
1933                 return (1);
1934         else if (a->td_hash < b->td_hash)
1935                 return (-1);
1936         return (0);
1937 }
1938
1939 RB_GENERATE_STATIC(tmpfs_dir, tmpfs_dirent, uh.td_entries, tmpfs_dirtree_cmp);