xref: /dflybsd-src/sys/vfs/tmpfs/tmpfs_subr.c (revision ff3cb46dc85efe9ae4869916ad9a2bdad4d71983)
1 /*	$NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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
9  * 2005 program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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.
31  */
32 
33 /*
34  * Efficient memory file system supporting functions.
35  */
36 
37 #include <sys/kernel.h>
38 #include <sys/param.h>
39 #include <sys/namei.h>
40 #include <sys/priv.h>
41 #include <sys/proc.h>
42 #include <sys/stat.h>
43 #include <sys/systm.h>
44 #include <sys/vnode.h>
45 #include <sys/vmmeter.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_pager.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_pageout.h>
53 #include <vm/vm_page2.h>
54 
55 #include <vfs/tmpfs/tmpfs.h>
56 #include <vfs/tmpfs/tmpfs_vnops.h>
57 
58 static ino_t tmpfs_fetch_ino(struct tmpfs_mount *);
59 
60 static int tmpfs_dirtree_compare(struct tmpfs_dirent *a,
61 	struct tmpfs_dirent *b);
62 RB_GENERATE(tmpfs_dirtree, tmpfs_dirent, rb_node, tmpfs_dirtree_compare);
63 
64 static int tmpfs_dirtree_compare_cookie(struct tmpfs_dirent *a,
65 	struct tmpfs_dirent *b);
66 RB_GENERATE(tmpfs_dirtree_cookie, tmpfs_dirent,
67 	rb_cookienode, tmpfs_dirtree_compare_cookie);
68 
69 
70 /* --------------------------------------------------------------------- */
71 
72 /*
73  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
74  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
75  * using the credentials of the process 'p'.
76  *
77  * If the node type is set to 'VDIR', then the parent parameter must point
78  * to the parent directory of the node being created.  It may only be NULL
79  * while allocating the root node.
80  *
81  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
82  * specifies the device the node represents.
83  *
84  * If the node type is set to 'VLNK', then the parameter target specifies
85  * the file name of the target file for the symbolic link that is being
86  * created.
87  *
88  * Note that new nodes are retrieved from the available list if it has
89  * items or, if it is empty, from the node pool as long as there is enough
90  * space to create them.
91  *
92  * Returns zero on success or an appropriate error code on failure.
93  */
94 int
95 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
96 		 uid_t uid, gid_t gid, mode_t mode,
97 		 char *target, int rmajor, int rminor,
98 		 struct tmpfs_node **node)
99 {
100 	struct tmpfs_node *nnode;
101 	struct timespec ts;
102 	dev_t rdev;
103 
104 	KKASSERT(IFF(type == VLNK, target != NULL));
105 	KKASSERT(IFF(type == VBLK || type == VCHR, rmajor != VNOVAL));
106 
107 	if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
108 		return (ENOSPC);
109 
110 	nnode = objcache_get(tmp->tm_node_pool, M_WAITOK | M_NULLOK);
111 	if (nnode == NULL)
112 		return (ENOSPC);
113 
114 	/* Generic initialization. */
115 	nnode->tn_type = type;
116 	vfs_timestamp(&ts);
117 	nnode->tn_ctime = nnode->tn_mtime = nnode->tn_atime
118 		= ts.tv_sec;
119 	nnode->tn_ctimensec = nnode->tn_mtimensec = nnode->tn_atimensec
120 		= ts.tv_nsec;
121 	nnode->tn_uid = uid;
122 	nnode->tn_gid = gid;
123 	nnode->tn_mode = mode;
124 	nnode->tn_id = tmpfs_fetch_ino(tmp);
125 	nnode->tn_advlock.init_done = 0;
126 	KKASSERT(nnode->tn_links == 0);
127 
128 	/* Type-specific initialization. */
129 	switch (nnode->tn_type) {
130 	case VBLK:
131 	case VCHR:
132 		rdev = makeudev(rmajor, rminor);
133 		if (rdev == NOUDEV) {
134 			objcache_put(tmp->tm_node_pool, nnode);
135 			return(EINVAL);
136 		}
137 		nnode->tn_rdev = rdev;
138 		break;
139 
140 	case VDIR:
141 		RB_INIT(&nnode->tn_dir.tn_dirtree);
142 		RB_INIT(&nnode->tn_dir.tn_cookietree);
143 		nnode->tn_size = 0;
144 		break;
145 
146 	case VFIFO:
147 		/* FALLTHROUGH */
148 	case VSOCK:
149 		break;
150 
151 	case VLNK:
152 		nnode->tn_size = strlen(target);
153 		nnode->tn_link = kmalloc(nnode->tn_size + 1, tmp->tm_name_zone,
154 					 M_WAITOK | M_NULLOK);
155 		if (nnode->tn_link == NULL) {
156 			objcache_put(tmp->tm_node_pool, nnode);
157 			return (ENOSPC);
158 		}
159 		bcopy(target, nnode->tn_link, nnode->tn_size);
160 		nnode->tn_link[nnode->tn_size] = '\0';
161 		break;
162 
163 	case VREG:
164 		nnode->tn_reg.tn_aobj = swap_pager_alloc(NULL, 0,
165 							 VM_PROT_DEFAULT, 0);
166 		nnode->tn_reg.tn_aobj_pages = 0;
167 		nnode->tn_size = 0;
168 		vm_object_set_flag(nnode->tn_reg.tn_aobj, OBJ_NOPAGEIN);
169 		break;
170 
171 	default:
172 		panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
173 	}
174 
175 	TMPFS_NODE_LOCK(nnode);
176 	TMPFS_LOCK(tmp);
177 	LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
178 	tmp->tm_nodes_inuse++;
179 	TMPFS_UNLOCK(tmp);
180 	TMPFS_NODE_UNLOCK(nnode);
181 
182 	*node = nnode;
183 	return 0;
184 }
185 
186 /* --------------------------------------------------------------------- */
187 
188 /*
189  * Destroys the node pointed to by node from the file system 'tmp'.
190  * If the node does not belong to the given mount point, the results are
191  * unpredicted.
192  *
193  * If the node references a directory; no entries are allowed because
194  * their removal could need a recursive algorithm, something forbidden in
195  * kernel space.  Furthermore, there is not need to provide such
196  * functionality (recursive removal) because the only primitives offered
197  * to the user are the removal of empty directories and the deletion of
198  * individual files.
199  *
200  * Note that nodes are not really deleted; in fact, when a node has been
201  * allocated, it cannot be deleted during the whole life of the file
202  * system.  Instead, they are moved to the available list and remain there
203  * until reused.
204  *
205  * A caller must have TMPFS_NODE_LOCK(node) and this function unlocks it.
206  */
207 void
208 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
209 {
210 	vm_pindex_t pages = 0;
211 
212 #ifdef INVARIANTS
213 	TMPFS_ASSERT_ELOCKED(node);
214 	KKASSERT(node->tn_vnode == NULL);
215 	KKASSERT((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
216 #endif
217 
218 	TMPFS_LOCK(tmp);
219 	LIST_REMOVE(node, tn_entries);
220 	tmp->tm_nodes_inuse--;
221 	TMPFS_UNLOCK(tmp);
222 	TMPFS_NODE_UNLOCK(node);  /* Caller has this lock */
223 
224 	switch (node->tn_type) {
225 	case VNON:
226 		/* Do not do anything.  VNON is provided to let the
227 		 * allocation routine clean itself easily by avoiding
228 		 * duplicating code in it. */
229 		/* FALLTHROUGH */
230 	case VBLK:
231 		/* FALLTHROUGH */
232 	case VCHR:
233 		/* FALLTHROUGH */
234 		break;
235 	case VDIR:
236 		/*
237 		 * The parent link can be NULL if this is the root
238 		 * node or if it is a directory node that was rmdir'd.
239 		 *
240 		 * XXX what if node is a directory which still contains
241 		 * directory entries (e.g. due to a forced umount) ?
242 		 */
243 		node->tn_size = 0;
244 		KKASSERT(node->tn_dir.tn_parent == NULL);
245 
246 		/*
247 		 * If the root node is being destroyed don't leave a
248 		 * dangling pointer in tmpfs_mount.
249 		 */
250 		if (node == tmp->tm_root)
251 			tmp->tm_root = NULL;
252 		break;
253 	case VFIFO:
254 		/* FALLTHROUGH */
255 	case VSOCK:
256 		break;
257 
258 	case VLNK:
259 		kfree(node->tn_link, tmp->tm_name_zone);
260 		node->tn_link = NULL;
261 		node->tn_size = 0;
262 		break;
263 
264 	case VREG:
265 		if (node->tn_reg.tn_aobj != NULL)
266 			vm_object_deallocate(node->tn_reg.tn_aobj);
267 		node->tn_reg.tn_aobj = NULL;
268 		pages = node->tn_reg.tn_aobj_pages;
269 		break;
270 
271 	default:
272 		panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
273 	}
274 
275 	/*
276 	 * Clean up fields for the next allocation.  The objcache only ctors
277 	 * new allocations.
278 	 */
279 	tmpfs_node_ctor(node, NULL, 0);
280 	objcache_put(tmp->tm_node_pool, node);
281 	/* node is now invalid */
282 
283 	if (pages)
284 		atomic_add_long(&tmp->tm_pages_used, -(long)pages);
285 }
286 
287 /* --------------------------------------------------------------------- */
288 
289 /*
290  * Allocates a new directory entry for the node node with a name of name.
291  * The new directory entry is returned in *de.
292  *
293  * The link count of node is increased by one to reflect the new object
294  * referencing it.
295  *
296  * Returns zero on success or an appropriate error code on failure.
297  */
298 int
299 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
300 		   const char *name, uint16_t len, struct tmpfs_dirent **de)
301 {
302 	struct tmpfs_dirent *nde;
303 
304 	nde = objcache_get(tmp->tm_dirent_pool, M_WAITOK);
305 	nde->td_name = kmalloc(len + 1, tmp->tm_name_zone, M_WAITOK | M_NULLOK);
306 	if (nde->td_name == NULL) {
307 		objcache_put(tmp->tm_dirent_pool, nde);
308 		*de = NULL;
309 		return (ENOSPC);
310 	}
311 	nde->td_namelen = len;
312 	bcopy(name, nde->td_name, len);
313 	nde->td_name[len] = '\0';
314 
315 	nde->td_node = node;
316 
317 	TMPFS_NODE_LOCK(node);
318 	++node->tn_links;
319 	TMPFS_NODE_UNLOCK(node);
320 
321 	*de = nde;
322 
323 	return 0;
324 }
325 
326 /* --------------------------------------------------------------------- */
327 
328 /*
329  * Frees a directory entry.  It is the caller's responsibility to destroy
330  * the node referenced by it if needed.
331  *
332  * The link count of node is decreased by one to reflect the removal of an
333  * object that referenced it.  This only happens if 'node_exists' is true;
334  * otherwise the function will not access the node referred to by the
335  * directory entry, as it may already have been released from the outside.
336  */
337 void
338 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
339 {
340 	struct tmpfs_node *node;
341 
342 	node = de->td_node;
343 
344 	TMPFS_NODE_LOCK(node);
345 	TMPFS_ASSERT_ELOCKED(node);
346 	KKASSERT(node->tn_links > 0);
347 	node->tn_links--;
348 	TMPFS_NODE_UNLOCK(node);
349 
350 	kfree(de->td_name, tmp->tm_name_zone);
351 	de->td_namelen = 0;
352 	de->td_name = NULL;
353 	de->td_node = NULL;
354 	objcache_put(tmp->tm_dirent_pool, de);
355 }
356 
357 /* --------------------------------------------------------------------- */
358 
359 /*
360  * Allocates a new vnode for the node node or returns a new reference to
361  * an existing one if the node had already a vnode referencing it.  The
362  * resulting locked vnode is returned in *vpp.
363  *
364  * Returns zero on success or an appropriate error code on failure.
365  *
366  * The caller must ensure that node cannot go away (usually by holding
367  * the related directory entry).
368  *
369  * If dnode is non-NULL this routine avoids deadlocking against it but
370  * can return EAGAIN.  Caller must try again.  The dnode lock will cycle
371  * in this case, it remains locked on return in all cases.  dnode must
372  * be shared-locked.
373  */
374 int
375 tmpfs_alloc_vp(struct mount *mp,
376 	       struct tmpfs_node *dnode, struct tmpfs_node *node, int lkflag,
377 	       struct vnode **vpp)
378 {
379 	int error = 0;
380 	struct vnode *vp;
381 
382 loop:
383 	/*
384 	 * Interlocked extraction from node.  This can race many things.
385 	 * We have to get a soft reference on the vnode while we hold
386 	 * the node locked, then acquire it properly and check for races.
387 	 */
388 	TMPFS_NODE_LOCK(node);
389 	if ((vp = node->tn_vnode) != NULL) {
390 		KKASSERT((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
391 		vhold(vp);
392 		TMPFS_NODE_UNLOCK(node);
393 
394 		if (dnode) {
395 			/*
396 			 * Special-case handling to avoid deadlocking against
397 			 * dnode.  This case has been validated and occurs
398 			 * every so often during synth builds.
399 			 */
400 			if (vget(vp, (lkflag & ~LK_RETRY) |
401 				     LK_NOWAIT |
402 				     LK_EXCLUSIVE) != 0) {
403 				TMPFS_NODE_UNLOCK(dnode);
404 				if (vget(vp, (lkflag & ~LK_RETRY) |
405 					     LK_SLEEPFAIL |
406 					     LK_EXCLUSIVE) == 0) {
407 					vn_unlock(vp);
408 				}
409 				vdrop(vp);
410 				TMPFS_NODE_LOCK_SH(dnode);
411 
412 				return EAGAIN;
413 			}
414 		} else {
415 			/*
416 			 * Normal path
417 			 */
418 			if (vget(vp, lkflag | LK_EXCLUSIVE) != 0) {
419 				vdrop(vp);
420 				goto loop;
421 			}
422 		}
423 		if (node->tn_vnode != vp) {
424 			vput(vp);
425 			vdrop(vp);
426 			goto loop;
427 		}
428 		vdrop(vp);
429 		goto out;
430 	}
431 	/* vp is NULL */
432 
433 	/*
434 	 * This should never happen.
435 	 */
436 	if (node->tn_vpstate & TMPFS_VNODE_DOOMED) {
437 		TMPFS_NODE_UNLOCK(node);
438 		error = ENOENT;
439 		goto out;
440 	}
441 
442 	/*
443 	 * Interlock against other calls to tmpfs_alloc_vp() trying to
444 	 * allocate and assign a vp to node.
445 	 */
446 	if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
447 		node->tn_vpstate |= TMPFS_VNODE_WANT;
448 		error = tsleep(&node->tn_vpstate, PINTERLOCKED | PCATCH,
449 			       "tmpfs_alloc_vp", 0);
450 		TMPFS_NODE_UNLOCK(node);
451 		if (error)
452 			return error;
453 		goto loop;
454 	}
455 	node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
456 	TMPFS_NODE_UNLOCK(node);
457 
458 	/*
459 	 * Allocate a new vnode (may block).  The ALLOCATING flag should
460 	 * prevent a race against someone else assigning node->tn_vnode.
461 	 */
462 	error = getnewvnode(VT_TMPFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
463 	if (error != 0)
464 		goto unlock;
465 
466 	KKASSERT(node->tn_vnode == NULL);
467 	KKASSERT(vp != NULL);
468 	vp->v_data = node;
469 	vp->v_type = node->tn_type;
470 
471 	/* Type-specific initialization. */
472 	switch (node->tn_type) {
473 	case VBLK:
474 		/* FALLTHROUGH */
475 	case VCHR:
476 		/* FALLTHROUGH */
477 	case VSOCK:
478 		break;
479 	case VREG:
480 		/*
481 		 * VMIO is mandatory.  Tmpfs also supports KVABIO
482 		 * for its tmpfs_strategy().
483 		 */
484 		vsetflags(vp, VKVABIO);
485 		vinitvmio(vp, node->tn_size, node->tn_blksize, -1);
486 		break;
487 	case VLNK:
488 		break;
489 	case VFIFO:
490 		vp->v_ops = &mp->mnt_vn_fifo_ops;
491 		break;
492 	case VDIR:
493 		break;
494 
495 	default:
496 		panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
497 	}
498 
499 
500 unlock:
501 	TMPFS_NODE_LOCK(node);
502 
503 	KKASSERT(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
504 	node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
505 	node->tn_vnode = vp;
506 
507 	if (node->tn_vpstate & TMPFS_VNODE_WANT) {
508 		node->tn_vpstate &= ~TMPFS_VNODE_WANT;
509 		TMPFS_NODE_UNLOCK(node);
510 		wakeup(&node->tn_vpstate);
511 	} else {
512 		TMPFS_NODE_UNLOCK(node);
513 	}
514 
515 out:
516 	*vpp = vp;
517 	KKASSERT(IFF(error == 0, *vpp != NULL && vn_islocked(*vpp)));
518 
519 	return error;
520 }
521 
522 /* --------------------------------------------------------------------- */
523 
524 /*
525  * Allocates a new file of type 'type' and adds it to the parent directory
526  * 'dvp'; this addition is done using the component name given in 'cnp'.
527  * The ownership of the new file is automatically assigned based on the
528  * credentials of the caller (through 'cnp'), the group is set based on
529  * the parent directory and the mode is determined from the 'vap' argument.
530  * If successful, *vpp holds a vnode to the newly created file and zero
531  * is returned.  Otherwise *vpp is NULL and the function returns an
532  * appropriate error code.
533  */
534 int
535 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
536 		 struct namecache *ncp, struct ucred *cred, char *target)
537 {
538 	int error;
539 	struct tmpfs_dirent *de;
540 	struct tmpfs_mount *tmp;
541 	struct tmpfs_node *dnode;
542 	struct tmpfs_node *node;
543 
544 	tmp = VFS_TO_TMPFS(dvp->v_mount);
545 	dnode = VP_TO_TMPFS_DIR(dvp);
546 	*vpp = NULL;
547 
548 	/*
549 	 * If the directory was removed but a process was CD'd into it,
550 	 * we do not allow any more file/dir creation within it.  Otherwise
551 	 * we will lose track of it.
552 	 */
553 	KKASSERT(dnode->tn_type == VDIR);
554 	if (dnode != tmp->tm_root && dnode->tn_dir.tn_parent == NULL)
555 		return ENOENT;
556 
557 	/*
558 	 * Make sure the link count does not overflow.
559 	 */
560 	if (vap->va_type == VDIR && dnode->tn_links >= LINK_MAX)
561 		return EMLINK;
562 
563 	/* Allocate a node that represents the new file. */
564 	error = tmpfs_alloc_node(tmp, vap->va_type, cred->cr_uid,
565 				 dnode->tn_gid, vap->va_mode, target,
566 				 vap->va_rmajor, vap->va_rminor, &node);
567 	if (error != 0)
568 		return error;
569 	TMPFS_NODE_LOCK(node);
570 
571 	/* Allocate a directory entry that points to the new file. */
572 	error = tmpfs_alloc_dirent(tmp, node, ncp->nc_name, ncp->nc_nlen, &de);
573 	if (error != 0) {
574 		tmpfs_free_node(tmp, node);
575 		/* eats node lock */
576 		return error;
577 	}
578 
579 	/* Allocate a vnode for the new file. */
580 	error = tmpfs_alloc_vp(dvp->v_mount, NULL, node, LK_EXCLUSIVE, vpp);
581 	if (error != 0) {
582 		tmpfs_free_dirent(tmp, de);
583 		tmpfs_free_node(tmp, node);
584 		/* eats node lock */
585 		return error;
586 	}
587 
588 	/*
589 	 * Now that all required items are allocated, we can proceed to
590 	 * insert the new node into the directory, an operation that
591 	 * cannot fail.
592 	 */
593 	tmpfs_dir_attach(dnode, de);
594 	TMPFS_NODE_UNLOCK(node);
595 
596 	return error;
597 }
598 
599 /* --------------------------------------------------------------------- */
600 
601 /*
602  * Attaches the directory entry de to the directory represented by dnode.
603  * Note that this does not change the link count of the node pointed by
604  * the directory entry, as this is done by tmpfs_alloc_dirent.
605  */
606 void
607 tmpfs_dir_attach(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
608 {
609 	struct tmpfs_node *node = de->td_node;
610 
611 	TMPFS_NODE_LOCK(dnode);
612 	if (node && node->tn_type == VDIR) {
613 		TMPFS_NODE_LOCK(node);
614 		++node->tn_links;
615 		node->tn_status |= TMPFS_NODE_CHANGED;
616 		node->tn_dir.tn_parent = dnode;
617 		++dnode->tn_links;
618 		TMPFS_NODE_UNLOCK(node);
619 	}
620 	RB_INSERT(tmpfs_dirtree, &dnode->tn_dir.tn_dirtree, de);
621 	RB_INSERT(tmpfs_dirtree_cookie, &dnode->tn_dir.tn_cookietree, de);
622 	dnode->tn_size += sizeof(struct tmpfs_dirent);
623 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
624 			    TMPFS_NODE_MODIFIED;
625 	TMPFS_NODE_UNLOCK(dnode);
626 }
627 
628 /* --------------------------------------------------------------------- */
629 
630 /*
631  * Detaches the directory entry de from the directory represented by dnode.
632  * Note that this does not change the link count of the node pointed by
633  * the directory entry, as this is done by tmpfs_free_dirent.
634  */
635 void
636 tmpfs_dir_detach(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
637 {
638 	struct tmpfs_node *node = de->td_node;
639 
640 	TMPFS_NODE_LOCK(dnode);
641 	RB_REMOVE(tmpfs_dirtree, &dnode->tn_dir.tn_dirtree, de);
642 	RB_REMOVE(tmpfs_dirtree_cookie, &dnode->tn_dir.tn_cookietree, de);
643 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
644 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
645 			    TMPFS_NODE_MODIFIED;
646 	TMPFS_NODE_UNLOCK(dnode);
647 
648 	/*
649 	 * Clean out the tn_parent pointer immediately when removing a
650 	 * directory.
651 	 *
652 	 * Removal of the parent linkage also cleans out the extra tn_links
653 	 * count we had on both node and dnode.
654 	 *
655 	 * node can be NULL (typ during a forced umount), in which case
656 	 * the mount code is dealing with the linkages from a linked list
657 	 * scan.
658 	 */
659 	if (node && node->tn_type == VDIR && node->tn_dir.tn_parent) {
660 		TMPFS_NODE_LOCK(dnode);
661 		TMPFS_NODE_LOCK(node);
662 		KKASSERT(node->tn_dir.tn_parent == dnode);
663 		dnode->tn_links--;
664 		node->tn_links--;
665 		node->tn_dir.tn_parent = NULL;
666 		TMPFS_NODE_UNLOCK(node);
667 		TMPFS_NODE_UNLOCK(dnode);
668 	}
669 }
670 
671 /* --------------------------------------------------------------------- */
672 
673 /*
674  * Looks for a directory entry in the directory represented by node.
675  * 'ncp' describes the name of the entry to look for.  Note that the .
676  * and .. components are not allowed as they do not physically exist
677  * within directories.
678  *
679  * Returns a pointer to the entry when found, otherwise NULL.
680  *
681  * Caller must hold the node locked (shared ok)
682  */
683 struct tmpfs_dirent *
684 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
685 		 struct namecache *ncp)
686 {
687 	struct tmpfs_dirent *de;
688 	int len = ncp->nc_nlen;
689 	struct tmpfs_dirent wanted;
690 
691 	wanted.td_namelen = len;
692 	wanted.td_name = ncp->nc_name;
693 
694 	TMPFS_VALIDATE_DIR(node);
695 
696 	de = RB_FIND(tmpfs_dirtree, &node->tn_dir.tn_dirtree, &wanted);
697 
698 	KKASSERT(f == NULL || f == de->td_node);
699 
700 	return de;
701 }
702 
703 /* --------------------------------------------------------------------- */
704 
705 /*
706  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
707  * directory and returns it in the uio space.  The function returns 0
708  * on success, -1 if there was not enough space in the uio structure to
709  * hold the directory entry or an appropriate error code if another
710  * error happens.
711  */
712 int
713 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
714 {
715 	int error;
716 
717 	TMPFS_VALIDATE_DIR(node);
718 	KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
719 
720 	if (vop_write_dirent(&error, uio, node->tn_id, DT_DIR, 1, "."))
721 		return -1;
722 	if (error == 0)
723 		uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
724 	return error;
725 }
726 
727 /* --------------------------------------------------------------------- */
728 
729 /*
730  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
731  * directory and returns it in the uio space.  The function returns 0
732  * on success, -1 if there was not enough space in the uio structure to
733  * hold the directory entry or an appropriate error code if another
734  * error happens.
735  */
736 int
737 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
738 			struct uio *uio)
739 {
740 	int error;
741 	ino_t d_ino;
742 
743 	TMPFS_VALIDATE_DIR(node);
744 	KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
745 
746 	if (node->tn_dir.tn_parent) {
747 		TMPFS_NODE_LOCK(node);
748 		if (node->tn_dir.tn_parent)
749 			d_ino = node->tn_dir.tn_parent->tn_id;
750 		else
751 			d_ino = tmp->tm_root->tn_id;
752 		TMPFS_NODE_UNLOCK(node);
753 	} else {
754 		d_ino = tmp->tm_root->tn_id;
755 	}
756 
757 	if (vop_write_dirent(&error, uio, d_ino, DT_DIR, 2, ".."))
758 		return -1;
759 	if (error == 0) {
760 		struct tmpfs_dirent *de;
761 		de = RB_MIN(tmpfs_dirtree_cookie, &node->tn_dir.tn_cookietree);
762 		if (de == NULL)
763 			uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
764 		else
765 			uio->uio_offset = tmpfs_dircookie(de);
766 	}
767 	return error;
768 }
769 
770 /* --------------------------------------------------------------------- */
771 
772 /*
773  * Lookup a directory entry by its associated cookie.
774  *
775  * Must be called with the directory node locked (shared ok)
776  */
777 struct lubycookie_info {
778 	off_t	cookie;
779 	struct tmpfs_dirent *de;
780 };
781 
782 static int
783 lubycookie_cmp(struct tmpfs_dirent *de, void *arg)
784 {
785 	struct lubycookie_info *info = arg;
786 	off_t cookie = tmpfs_dircookie(de);
787 
788 	if (cookie < info->cookie)
789 		return(-1);
790 	if (cookie > info->cookie)
791 		return(1);
792 	return(0);
793 }
794 
795 static int
796 lubycookie_callback(struct tmpfs_dirent *de, void *arg)
797 {
798 	struct lubycookie_info *info = arg;
799 
800 	if (tmpfs_dircookie(de) == info->cookie) {
801 		info->de = de;
802 		return(-1);
803 	}
804 	return(0);
805 }
806 
807 struct tmpfs_dirent *
808 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
809 {
810 	struct lubycookie_info info;
811 
812 	info.cookie = cookie;
813 	info.de = NULL;
814 	RB_SCAN(tmpfs_dirtree_cookie, &node->tn_dir.tn_cookietree,
815 		lubycookie_cmp, lubycookie_callback, &info);
816 	return (info.de);
817 }
818 
819 /* --------------------------------------------------------------------- */
820 
821 /*
822  * Helper function for tmpfs_readdir.  Returns as much directory entries
823  * as can fit in the uio space.  The read starts at uio->uio_offset.
824  * The function returns 0 on success, -1 if there was not enough space
825  * in the uio structure to hold the directory entry or an appropriate
826  * error code if another error happens.
827  *
828  * Caller must hold the node locked (shared ok)
829  */
830 int
831 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
832 {
833 	int error;
834 	off_t startcookie;
835 	struct tmpfs_dirent *de;
836 
837 	TMPFS_VALIDATE_DIR(node);
838 
839 	/*
840 	 * Locate the first directory entry we have to return.  We have cached
841 	 * the last readdir in the node, so use those values if appropriate.
842 	 * Otherwise do a linear scan to find the requested entry.
843 	 */
844 	startcookie = uio->uio_offset;
845 	KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
846 	KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
847 
848 	if (startcookie == TMPFS_DIRCOOKIE_EOF)
849 		return 0;
850 
851 	de = tmpfs_dir_lookupbycookie(node, startcookie);
852 	if (de == NULL)
853 		return EINVAL;
854 
855 	/*
856 	 * Read as much entries as possible; i.e., until we reach the end of
857 	 * the directory or we exhaust uio space.
858 	 */
859 	do {
860 		ino_t d_ino;
861 		uint8_t d_type;
862 
863 		/* Create a dirent structure representing the current
864 		 * tmpfs_node and fill it. */
865 		d_ino = de->td_node->tn_id;
866 		switch (de->td_node->tn_type) {
867 		case VBLK:
868 			d_type = DT_BLK;
869 			break;
870 
871 		case VCHR:
872 			d_type = DT_CHR;
873 			break;
874 
875 		case VDIR:
876 			d_type = DT_DIR;
877 			break;
878 
879 		case VFIFO:
880 			d_type = DT_FIFO;
881 			break;
882 
883 		case VLNK:
884 			d_type = DT_LNK;
885 			break;
886 
887 		case VREG:
888 			d_type = DT_REG;
889 			break;
890 
891 		case VSOCK:
892 			d_type = DT_SOCK;
893 			break;
894 
895 		default:
896 			panic("tmpfs_dir_getdents: type %p %d",
897 			    de->td_node, (int)de->td_node->tn_type);
898 		}
899 		KKASSERT(de->td_namelen < 256); /* 255 + 1 */
900 
901 		if (vop_write_dirent(&error, uio, d_ino, d_type,
902 		    de->td_namelen, de->td_name)) {
903 			error = -1;
904 			break;
905 		}
906 
907 		(*cntp)++;
908 		de = RB_NEXT(tmpfs_dirtree_cookie,
909 			     node->tn_dir.tn_cookietree, de);
910 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
911 
912 	/* Update the offset and cache. */
913 	if (de == NULL) {
914 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
915 	} else {
916 		uio->uio_offset = tmpfs_dircookie(de);
917 	}
918 
919 	return error;
920 }
921 
922 /* --------------------------------------------------------------------- */
923 
924 /*
925  * Resizes the aobj associated to the regular file pointed to by vp to
926  * the size newsize.  'vp' must point to a vnode that represents a regular
927  * file.  'newsize' must be positive.
928  *
929  * pass NVEXTF_TRIVIAL when buf content will be overwritten, otherwise set 0
930  * to be zero filled.
931  *
932  * Returns zero on success or an appropriate error code on failure.
933  *
934  * Caller must hold the node exclusively locked.
935  */
936 int
937 tmpfs_reg_resize(struct vnode *vp, off_t newsize, int trivial)
938 {
939 	int error;
940 	vm_pindex_t newpages, oldpages;
941 	struct tmpfs_mount *tmp;
942 	struct tmpfs_node *node;
943 	off_t oldsize;
944 	int nvextflags;
945 
946 #ifdef INVARIANTS
947 	KKASSERT(vp->v_type == VREG);
948 	KKASSERT(newsize >= 0);
949 #endif
950 
951 	node = VP_TO_TMPFS_NODE(vp);
952 	tmp = VFS_TO_TMPFS(vp->v_mount);
953 
954 	/*
955 	 * Convert the old and new sizes to the number of pages needed to
956 	 * store them.  It may happen that we do not need to do anything
957 	 * because the last allocated page can accommodate the change on
958 	 * its own.
959 	 */
960 	oldsize = node->tn_size;
961 	oldpages = round_page64(oldsize) / PAGE_SIZE;
962 	KKASSERT(oldpages == node->tn_reg.tn_aobj_pages);
963 	newpages = round_page64(newsize) / PAGE_SIZE;
964 
965 	if (newpages > oldpages &&
966 	   tmp->tm_pages_used + newpages - oldpages > tmp->tm_pages_max) {
967 		error = ENOSPC;
968 		goto out;
969 	}
970 	node->tn_reg.tn_aobj_pages = newpages;
971 	node->tn_size = newsize;
972 
973 	if (newpages != oldpages)
974 		atomic_add_long(&tmp->tm_pages_used, (newpages - oldpages));
975 
976 	/*
977 	 * nvextflags to pass along for bdwrite() vs buwrite()
978 	 */
979 	if (vm_pages_needed || vm_paging_needed(0) ||
980 	    tmpfs_bufcache_mode >= 2) {
981 		nvextflags = 0;
982 	} else {
983 		nvextflags = NVEXTF_BUWRITE;
984 	}
985 
986 
987 	/*
988 	 * When adjusting the vnode filesize and its VM object we must
989 	 * also adjust our backing VM object (aobj).  The blocksize
990 	 * used must match the block sized we use for the buffer cache.
991 	 *
992 	 * The backing VM object may contain VM pages as well as swap
993 	 * assignments if we previously renamed main object pages into
994 	 * it during deactivation.
995 	 *
996 	 * To make things easier tmpfs uses a blksize in multiples of
997 	 * PAGE_SIZE, and will only increase the blksize as a small file
998 	 * increases in size.  Once a file has exceeded TMPFS_BLKSIZE (16KB),
999 	 * the blksize is maxed out.  Truncating the file does not reduce
1000 	 * the blksize.
1001 	 */
1002 	if (newsize < oldsize) {
1003 		vm_pindex_t osize;
1004 		vm_pindex_t nsize;
1005 		vm_object_t aobj;
1006 
1007 		error = nvtruncbuf(vp, newsize, node->tn_blksize,
1008 				   -1, nvextflags);
1009 		aobj = node->tn_reg.tn_aobj;
1010 		if (aobj) {
1011 			osize = aobj->size;
1012 			nsize = vp->v_object->size;
1013 			if (nsize < osize) {
1014 				aobj->size = osize;
1015 				swap_pager_freespace(aobj, nsize,
1016 						     osize - nsize);
1017 				vm_object_page_remove(aobj, nsize, osize,
1018 						      FALSE);
1019 			}
1020 		}
1021 	} else {
1022 		vm_object_t aobj;
1023 		int nblksize;
1024 
1025 		/*
1026 		 * The first (and only the first) buffer in the file is resized
1027 		 * in multiples of PAGE_SIZE, up to TMPFS_BLKSIZE.
1028 		 */
1029 		nblksize = node->tn_blksize;
1030 		while (nblksize < TMPFS_BLKSIZE &&
1031 		       nblksize < newsize) {
1032 			nblksize += PAGE_SIZE;
1033 		}
1034 
1035 		if (trivial)
1036 			nvextflags |= NVEXTF_TRIVIAL;
1037 
1038 		error = nvextendbuf(vp, oldsize, newsize,
1039 				    node->tn_blksize, nblksize,
1040 				    -1, -1, nvextflags);
1041 		node->tn_blksize = nblksize;
1042 		aobj = node->tn_reg.tn_aobj;
1043 		if (aobj)
1044 			aobj->size = vp->v_object->size;
1045 	}
1046 
1047 out:
1048 	return error;
1049 }
1050 
1051 /* --------------------------------------------------------------------- */
1052 
1053 /*
1054  * Change flags of the given vnode.
1055  * Caller should execute tmpfs_update on vp after a successful execution.
1056  * The vnode must be locked on entry and remain locked on exit.
1057  */
1058 int
1059 tmpfs_chflags(struct vnode *vp, u_long vaflags, struct ucred *cred)
1060 {
1061 	int error;
1062 	struct tmpfs_node *node;
1063 	int flags;
1064 
1065 	KKASSERT(vn_islocked(vp));
1066 
1067 	node = VP_TO_TMPFS_NODE(vp);
1068 	flags = node->tn_flags;
1069 
1070 	/* Disallow this operation if the file system is mounted read-only. */
1071 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1072 		return EROFS;
1073 	error = vop_helper_setattr_flags(&flags, vaflags, node->tn_uid, cred);
1074 
1075 	/* Actually change the flags on the node itself */
1076 	if (error == 0) {
1077 		TMPFS_NODE_LOCK(node);
1078 		node->tn_flags = flags;
1079 		node->tn_status |= TMPFS_NODE_CHANGED;
1080 		TMPFS_NODE_UNLOCK(node);
1081 	}
1082 
1083 	KKASSERT(vn_islocked(vp));
1084 
1085 	return error;
1086 }
1087 
1088 /* --------------------------------------------------------------------- */
1089 
1090 /*
1091  * Change access mode on the given vnode.
1092  * Caller should execute tmpfs_update on vp after a successful execution.
1093  * The vnode must be locked on entry and remain locked on exit.
1094  */
1095 int
1096 tmpfs_chmod(struct vnode *vp, mode_t vamode, struct ucred *cred)
1097 {
1098 	struct tmpfs_node *node;
1099 	mode_t cur_mode;
1100 	int error;
1101 
1102 	KKASSERT(vn_islocked(vp));
1103 
1104 	node = VP_TO_TMPFS_NODE(vp);
1105 
1106 	/* Disallow this operation if the file system is mounted read-only. */
1107 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1108 		return EROFS;
1109 
1110 	/* Immutable or append-only files cannot be modified, either. */
1111 	if (node->tn_flags & (IMMUTABLE | APPEND))
1112 		return EPERM;
1113 
1114 	cur_mode = node->tn_mode;
1115 	error = vop_helper_chmod(vp, vamode, cred, node->tn_uid, node->tn_gid,
1116 				 &cur_mode);
1117 
1118 	if (error == 0 &&
1119 	    (node->tn_mode & ALLPERMS) != (cur_mode & ALLPERMS)) {
1120 		TMPFS_NODE_LOCK(node);
1121 		node->tn_mode &= ~ALLPERMS;
1122 		node->tn_mode |= cur_mode & ALLPERMS;
1123 
1124 		node->tn_status |= TMPFS_NODE_CHANGED;
1125 		TMPFS_NODE_UNLOCK(node);
1126 	}
1127 
1128 	KKASSERT(vn_islocked(vp));
1129 
1130 	return 0;
1131 }
1132 
1133 /* --------------------------------------------------------------------- */
1134 
1135 /*
1136  * Change ownership of the given vnode.  At least one of uid or gid must
1137  * be different than VNOVAL.  If one is set to that value, the attribute
1138  * is unchanged.
1139  * Caller should execute tmpfs_update on vp after a successful execution.
1140  * The vnode must be locked on entry and remain locked on exit.
1141  */
1142 int
1143 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1144 {
1145 	mode_t cur_mode;
1146 	uid_t cur_uid;
1147 	gid_t cur_gid;
1148 	struct tmpfs_node *node;
1149 	int error;
1150 
1151 	KKASSERT(vn_islocked(vp));
1152 	node = VP_TO_TMPFS_NODE(vp);
1153 
1154 	/* Disallow this operation if the file system is mounted read-only. */
1155 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1156 		return EROFS;
1157 
1158 	/* Immutable or append-only files cannot be modified, either. */
1159 	if (node->tn_flags & (IMMUTABLE | APPEND))
1160 		return EPERM;
1161 
1162 	cur_uid = node->tn_uid;
1163 	cur_gid = node->tn_gid;
1164 	cur_mode = node->tn_mode;
1165 	error = vop_helper_chown(vp, uid, gid, cred,
1166 				 &cur_uid, &cur_gid, &cur_mode);
1167 
1168 	if (error == 0) {
1169 		TMPFS_NODE_LOCK(node);
1170 		if (cur_uid != node->tn_uid ||
1171 		    cur_gid != node->tn_gid ||
1172 		    cur_mode != node->tn_mode) {
1173 			node->tn_uid = cur_uid;
1174 			node->tn_gid = cur_gid;
1175 			node->tn_mode = cur_mode;
1176 			node->tn_status |= TMPFS_NODE_CHANGED;
1177 		}
1178 		TMPFS_NODE_UNLOCK(node);
1179 	}
1180 
1181 	return error;
1182 }
1183 
1184 /* --------------------------------------------------------------------- */
1185 
1186 /*
1187  * Change size of the given vnode.
1188  * Caller should execute tmpfs_update on vp after a successful execution.
1189  * The vnode must be locked on entry and remain locked on exit.
1190  */
1191 int
1192 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred)
1193 {
1194 	int error;
1195 	struct tmpfs_node *node;
1196 
1197 	KKASSERT(vn_islocked(vp));
1198 
1199 	node = VP_TO_TMPFS_NODE(vp);
1200 
1201 	/* Decide whether this is a valid operation based on the file type. */
1202 	error = 0;
1203 	switch (vp->v_type) {
1204 	case VDIR:
1205 		return EISDIR;
1206 
1207 	case VREG:
1208 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1209 			return EROFS;
1210 		break;
1211 
1212 	case VBLK:
1213 		/* FALLTHROUGH */
1214 	case VCHR:
1215 		/* FALLTHROUGH */
1216 	case VFIFO:
1217 		/* Allow modifications of special files even if in the file
1218 		 * system is mounted read-only (we are not modifying the
1219 		 * files themselves, but the objects they represent). */
1220 		return 0;
1221 
1222 	default:
1223 		/* Anything else is unsupported. */
1224 		return EOPNOTSUPP;
1225 	}
1226 
1227 	/* Immutable or append-only files cannot be modified, either. */
1228 	if (node->tn_flags & (IMMUTABLE | APPEND))
1229 		return EPERM;
1230 
1231 	error = tmpfs_truncate(vp, size);
1232 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1233 	 * for us, as will update tn_status; no need to do that here. */
1234 
1235 	KKASSERT(vn_islocked(vp));
1236 
1237 	return error;
1238 }
1239 
1240 /* --------------------------------------------------------------------- */
1241 
1242 /*
1243  * Change access and modification times of the given vnode.
1244  * Caller should execute tmpfs_update on vp after a successful execution.
1245  * The vnode must be locked on entry and remain locked on exit.
1246  */
1247 int
1248 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1249 	      int vaflags, struct ucred *cred)
1250 {
1251 	struct tmpfs_node *node;
1252 
1253 	KKASSERT(vn_islocked(vp));
1254 
1255 	node = VP_TO_TMPFS_NODE(vp);
1256 
1257 	/* Disallow this operation if the file system is mounted read-only. */
1258 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1259 		return EROFS;
1260 
1261 	/* Immutable or append-only files cannot be modified, either. */
1262 	if (node->tn_flags & (IMMUTABLE | APPEND))
1263 		return EPERM;
1264 
1265 	TMPFS_NODE_LOCK(node);
1266 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1267 		node->tn_status |= TMPFS_NODE_ACCESSED;
1268 
1269 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) {
1270 		node->tn_status |= TMPFS_NODE_MODIFIED;
1271 		vclrflags(vp, VLASTWRITETS);
1272 	}
1273 
1274 	TMPFS_NODE_UNLOCK(node);
1275 
1276 	tmpfs_itimes(vp, atime, mtime);
1277 
1278 	KKASSERT(vn_islocked(vp));
1279 
1280 	return 0;
1281 }
1282 
1283 /* --------------------------------------------------------------------- */
1284 /* Sync timestamps */
1285 void
1286 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1287 	     const struct timespec *mod)
1288 {
1289 	struct tmpfs_node *node;
1290 	struct timespec now;
1291 
1292 	node = VP_TO_TMPFS_NODE(vp);
1293 
1294 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1295 	    TMPFS_NODE_CHANGED)) == 0)
1296 		return;
1297 
1298 	vfs_timestamp(&now);
1299 
1300 	TMPFS_NODE_LOCK(node);
1301 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1302 		if (acc == NULL)
1303 			 acc = &now;
1304 		node->tn_atime = acc->tv_sec;
1305 		node->tn_atimensec = acc->tv_nsec;
1306 	}
1307 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1308 		if (mod == NULL)
1309 			mod = &now;
1310 		node->tn_mtime = mod->tv_sec;
1311 		node->tn_mtimensec = mod->tv_nsec;
1312 	}
1313 	if (node->tn_status & TMPFS_NODE_CHANGED) {
1314 		node->tn_ctime = now.tv_sec;
1315 		node->tn_ctimensec = now.tv_nsec;
1316 	}
1317 
1318 	node->tn_status &= ~(TMPFS_NODE_ACCESSED |
1319 			     TMPFS_NODE_MODIFIED |
1320 			     TMPFS_NODE_CHANGED);
1321 	TMPFS_NODE_UNLOCK(node);
1322 }
1323 
1324 /* --------------------------------------------------------------------- */
1325 
1326 void
1327 tmpfs_update(struct vnode *vp)
1328 {
1329 	tmpfs_itimes(vp, NULL, NULL);
1330 }
1331 
1332 /* --------------------------------------------------------------------- */
1333 
1334 /*
1335  * Caller must hold an exclusive node lock.
1336  */
1337 int
1338 tmpfs_truncate(struct vnode *vp, off_t length)
1339 {
1340 	int error;
1341 	struct tmpfs_node *node;
1342 
1343 	node = VP_TO_TMPFS_NODE(vp);
1344 
1345 	if (length < 0) {
1346 		error = EINVAL;
1347 		goto out;
1348 	}
1349 
1350 	if (node->tn_size == length) {
1351 		error = 0;
1352 		goto out;
1353 	}
1354 
1355 	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1356 		return (EFBIG);
1357 
1358 
1359 	error = tmpfs_reg_resize(vp, length, 1);
1360 
1361 	if (error == 0)
1362 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1363 
1364 out:
1365 	tmpfs_update(vp);
1366 
1367 	return error;
1368 }
1369 
1370 /* --------------------------------------------------------------------- */
1371 
1372 static ino_t
1373 tmpfs_fetch_ino(struct tmpfs_mount *tmp)
1374 {
1375 	ino_t ret;
1376 
1377 	ret = atomic_fetchadd_64(&tmp->tm_ino, 1);
1378 
1379 	return (ret);
1380 }
1381 
1382 static int
1383 tmpfs_dirtree_compare(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1384 {
1385 	if (a->td_namelen > b->td_namelen)
1386 		return 1;
1387 	else if (a->td_namelen < b->td_namelen)
1388 		return -1;
1389 	else
1390 		return strncmp(a->td_name, b->td_name, a->td_namelen);
1391 }
1392 
1393 static int
1394 tmpfs_dirtree_compare_cookie(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1395 {
1396 	if (a < b)
1397 		return(-1);
1398 	if (a > b)
1399 		return(1);
1400 	return 0;
1401 }
1402