xref: /dflybsd-src/sys/vfs/tmpfs/tmpfs_subr.c (revision fc36a10bce8c5678d103e0498db849506d9dac68)
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_dir.tn_parent = NULL;
144 		nnode->tn_size = 0;
145 		break;
146 
147 	case VFIFO:
148 		/* FALLTHROUGH */
149 	case VSOCK:
150 		break;
151 
152 	case VLNK:
153 		nnode->tn_size = strlen(target);
154 		nnode->tn_link = kmalloc(nnode->tn_size + 1, tmp->tm_name_zone,
155 					 M_WAITOK | M_NULLOK);
156 		if (nnode->tn_link == NULL) {
157 			objcache_put(tmp->tm_node_pool, nnode);
158 			return (ENOSPC);
159 		}
160 		bcopy(target, nnode->tn_link, nnode->tn_size);
161 		nnode->tn_link[nnode->tn_size] = '\0';
162 		break;
163 
164 	case VREG:
165 		nnode->tn_reg.tn_aobj = swap_pager_alloc(NULL, 0,
166 							 VM_PROT_DEFAULT, 0);
167 		nnode->tn_reg.tn_aobj_pages = 0;
168 		nnode->tn_size = 0;
169 		vm_object_set_flag(nnode->tn_reg.tn_aobj, OBJ_NOPAGEIN);
170 		break;
171 
172 	default:
173 		panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
174 	}
175 
176 	TMPFS_NODE_LOCK(nnode);
177 	TMPFS_LOCK(tmp);
178 	LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
179 	tmp->tm_nodes_inuse++;
180 	TMPFS_UNLOCK(tmp);
181 	TMPFS_NODE_UNLOCK(nnode);
182 
183 	*node = nnode;
184 	return 0;
185 }
186 
187 /* --------------------------------------------------------------------- */
188 
189 /*
190  * Destroys the node pointed to by node from the file system 'tmp'.
191  * If the node does not belong to the given mount point, the results are
192  * unpredicted.
193  *
194  * If the node references a directory; no entries are allowed because
195  * their removal could need a recursive algorithm, something forbidden in
196  * kernel space.  Furthermore, there is not need to provide such
197  * functionality (recursive removal) because the only primitives offered
198  * to the user are the removal of empty directories and the deletion of
199  * individual files.
200  *
201  * Note that nodes are not really deleted; in fact, when a node has been
202  * allocated, it cannot be deleted during the whole life of the file
203  * system.  Instead, they are moved to the available list and remain there
204  * until reused.
205  *
206  * A caller must have TMPFS_NODE_LOCK(node) and this function unlocks it.
207  */
208 void
209 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
210 {
211 	vm_pindex_t pages = 0;
212 
213 #ifdef INVARIANTS
214 	TMPFS_ASSERT_ELOCKED(node);
215 	KKASSERT(node->tn_vnode == NULL);
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 	vp = NULL;
384 	if (node->tn_vnode == NULL) {
385 		error = getnewvnode(VT_TMPFS, mp, &vp,
386 				    VLKTIMEOUT, LK_CANRECURSE);
387 		if (error)
388 			goto out;
389 	}
390 
391 	/*
392 	 * Interlocked extraction from node.  This can race many things.
393 	 * We have to get a soft reference on the vnode while we hold
394 	 * the node locked, then acquire it properly and check for races.
395 	 */
396 	TMPFS_NODE_LOCK(node);
397 	if (node->tn_vnode) {
398 		if (vp) {
399 			vp->v_type = VBAD;
400 			vx_put(vp);
401 		}
402 		vp = node->tn_vnode;
403 
404 		KKASSERT((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
405 		vhold(vp);
406 		TMPFS_NODE_UNLOCK(node);
407 
408 		if (dnode) {
409 			/*
410 			 * Special-case handling to avoid deadlocking against
411 			 * dnode.  This case has been validated and occurs
412 			 * every so often during synth builds.
413 			 */
414 			if (vget(vp, (lkflag & ~LK_RETRY) |
415 				     LK_NOWAIT |
416 				     LK_EXCLUSIVE) != 0) {
417 				TMPFS_NODE_UNLOCK(dnode);
418 				if (vget(vp, (lkflag & ~LK_RETRY) |
419 					     LK_SLEEPFAIL |
420 					     LK_EXCLUSIVE) == 0) {
421 					vn_unlock(vp);
422 				}
423 				vdrop(vp);
424 				TMPFS_NODE_LOCK_SH(dnode);
425 
426 				return EAGAIN;
427 			}
428 		} else {
429 			/*
430 			 * Normal path
431 			 */
432 			if (vget(vp, lkflag | LK_EXCLUSIVE) != 0) {
433 				vdrop(vp);
434 				goto loop;
435 			}
436 		}
437 		if (node->tn_vnode != vp) {
438 			vput(vp);
439 			vdrop(vp);
440 			goto loop;
441 		}
442 		vdrop(vp);
443 		goto out;
444 	}
445 
446 	/*
447 	 * We need to assign node->tn_vnode.  If vp is NULL, loop up to
448 	 * allocate the vp.  This can happen due to SMP races.
449 	 */
450 	if (vp == NULL)
451 		goto loop;
452 
453 	/*
454 	 * This should never happen.
455 	 */
456 	if (node->tn_vpstate & TMPFS_VNODE_DOOMED) {
457 		TMPFS_NODE_UNLOCK(node);
458 		vp->v_type = VBAD;
459 		vx_put(vp);
460 		error = ENOENT;
461 		goto out;
462 	}
463 
464 	KKASSERT(node->tn_vnode == NULL);
465 	KKASSERT(vp != NULL);
466 	vp->v_data = node;
467 	vp->v_type = node->tn_type;
468 
469 	/* Type-specific initialization. */
470 	switch (node->tn_type) {
471 	case VBLK:
472 		/* FALLTHROUGH */
473 	case VCHR:
474 		/* FALLTHROUGH */
475 	case VSOCK:
476 		break;
477 	case VREG:
478 		/*
479 		 * VMIO is mandatory.  Tmpfs also supports KVABIO
480 		 * for its tmpfs_strategy().
481 		 */
482 		vsetflags(vp, VKVABIO);
483 		vinitvmio(vp, node->tn_size, node->tn_blksize, -1);
484 		break;
485 	case VLNK:
486 		break;
487 	case VFIFO:
488 		vp->v_ops = &mp->mnt_vn_fifo_ops;
489 		break;
490 	case VDIR:
491 		break;
492 
493 	default:
494 		panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
495 	}
496 
497 	node->tn_vnode = vp;
498 	TMPFS_NODE_UNLOCK(node);
499 
500 	vx_downgrade(vp);
501 out:
502 	*vpp = vp;
503 	KKASSERT(IFF(error == 0, *vpp != NULL && vn_islocked(*vpp)));
504 
505 	return error;
506 }
507 
508 /* --------------------------------------------------------------------- */
509 
510 /*
511  * Allocates a new file of type 'type' and adds it to the parent directory
512  * 'dvp'; this addition is done using the component name given in 'cnp'.
513  * The ownership of the new file is automatically assigned based on the
514  * credentials of the caller (through 'cnp'), the group is set based on
515  * the parent directory and the mode is determined from the 'vap' argument.
516  * If successful, *vpp holds a vnode to the newly created file and zero
517  * is returned.  Otherwise *vpp is NULL and the function returns an
518  * appropriate error code.
519  */
520 int
521 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
522 		 struct namecache *ncp, struct ucred *cred, char *target)
523 {
524 	int error;
525 	struct tmpfs_dirent *de;
526 	struct tmpfs_mount *tmp;
527 	struct tmpfs_node *dnode;
528 	struct tmpfs_node *node;
529 
530 	tmp = VFS_TO_TMPFS(dvp->v_mount);
531 	dnode = VP_TO_TMPFS_DIR(dvp);
532 	*vpp = NULL;
533 
534 	/*
535 	 * If the directory was removed but a process was CD'd into it,
536 	 * we do not allow any more file/dir creation within it.  Otherwise
537 	 * we will lose track of it.
538 	 */
539 	KKASSERT(dnode->tn_type == VDIR);
540 	if (dnode != tmp->tm_root && dnode->tn_dir.tn_parent == NULL)
541 		return ENOENT;
542 
543 	/*
544 	 * Make sure the link count does not overflow.
545 	 */
546 	if (vap->va_type == VDIR && dnode->tn_links >= LINK_MAX)
547 		return EMLINK;
548 
549 	/* Allocate a node that represents the new file. */
550 	error = tmpfs_alloc_node(tmp, vap->va_type, cred->cr_uid,
551 				 dnode->tn_gid, vap->va_mode, target,
552 				 vap->va_rmajor, vap->va_rminor, &node);
553 	if (error != 0)
554 		return error;
555 	TMPFS_NODE_LOCK(node);
556 
557 	/* Allocate a directory entry that points to the new file. */
558 	error = tmpfs_alloc_dirent(tmp, node, ncp->nc_name, ncp->nc_nlen, &de);
559 	if (error != 0) {
560 		tmpfs_free_node(tmp, node);
561 		/* eats node lock */
562 		return error;
563 	}
564 
565 	/* Allocate a vnode for the new file. */
566 	error = tmpfs_alloc_vp(dvp->v_mount, NULL, node, LK_EXCLUSIVE, vpp);
567 	if (error != 0) {
568 		tmpfs_free_dirent(tmp, de);
569 		tmpfs_free_node(tmp, node);
570 		/* eats node lock */
571 		return error;
572 	}
573 
574 	/*
575 	 * Now that all required items are allocated, we can proceed to
576 	 * insert the new node into the directory, an operation that
577 	 * cannot fail.
578 	 */
579 	tmpfs_dir_attach(dnode, de);
580 	TMPFS_NODE_UNLOCK(node);
581 
582 	return error;
583 }
584 
585 /* --------------------------------------------------------------------- */
586 
587 /*
588  * Attaches the directory entry de to the directory represented by dnode.
589  * Note that this does not change the link count of the node pointed by
590  * the directory entry, as this is done by tmpfs_alloc_dirent.
591  */
592 void
593 tmpfs_dir_attach(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
594 {
595 	struct tmpfs_node *node = de->td_node;
596 
597 	TMPFS_NODE_LOCK(dnode);
598 	if (node && node->tn_type == VDIR) {
599 		TMPFS_NODE_LOCK(node);
600 		++node->tn_links;
601 		node->tn_status |= TMPFS_NODE_CHANGED;
602 		node->tn_dir.tn_parent = dnode;
603 		++dnode->tn_links;
604 		TMPFS_NODE_UNLOCK(node);
605 	}
606 	RB_INSERT(tmpfs_dirtree, &dnode->tn_dir.tn_dirtree, de);
607 	RB_INSERT(tmpfs_dirtree_cookie, &dnode->tn_dir.tn_cookietree, de);
608 	dnode->tn_size += sizeof(struct tmpfs_dirent);
609 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
610 			    TMPFS_NODE_MODIFIED;
611 	TMPFS_NODE_UNLOCK(dnode);
612 }
613 
614 /* --------------------------------------------------------------------- */
615 
616 /*
617  * Detaches the directory entry de from the directory represented by dnode.
618  * Note that this does not change the link count of the node pointed by
619  * the directory entry, as this is done by tmpfs_free_dirent.
620  */
621 void
622 tmpfs_dir_detach(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
623 {
624 	struct tmpfs_node *node = de->td_node;
625 
626 	TMPFS_NODE_LOCK(dnode);
627 	RB_REMOVE(tmpfs_dirtree, &dnode->tn_dir.tn_dirtree, de);
628 	RB_REMOVE(tmpfs_dirtree_cookie, &dnode->tn_dir.tn_cookietree, de);
629 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
630 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
631 			    TMPFS_NODE_MODIFIED;
632 	TMPFS_NODE_UNLOCK(dnode);
633 
634 	/*
635 	 * Clean out the tn_parent pointer immediately when removing a
636 	 * directory.
637 	 *
638 	 * Removal of the parent linkage also cleans out the extra tn_links
639 	 * count we had on both node and dnode.
640 	 *
641 	 * node can be NULL (typ during a forced umount), in which case
642 	 * the mount code is dealing with the linkages from a linked list
643 	 * scan.
644 	 */
645 	if (node && node->tn_type == VDIR && node->tn_dir.tn_parent) {
646 		TMPFS_NODE_LOCK(dnode);
647 		TMPFS_NODE_LOCK(node);
648 		KKASSERT(node->tn_dir.tn_parent == dnode);
649 		dnode->tn_links--;
650 		node->tn_links--;
651 		node->tn_dir.tn_parent = NULL;
652 		TMPFS_NODE_UNLOCK(node);
653 		TMPFS_NODE_UNLOCK(dnode);
654 	}
655 }
656 
657 /* --------------------------------------------------------------------- */
658 
659 /*
660  * Looks for a directory entry in the directory represented by node.
661  * 'ncp' describes the name of the entry to look for.  Note that the .
662  * and .. components are not allowed as they do not physically exist
663  * within directories.
664  *
665  * Returns a pointer to the entry when found, otherwise NULL.
666  *
667  * Caller must hold the node locked (shared ok)
668  */
669 struct tmpfs_dirent *
670 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
671 		 struct namecache *ncp)
672 {
673 	struct tmpfs_dirent *de;
674 	int len = ncp->nc_nlen;
675 	struct tmpfs_dirent wanted;
676 
677 	wanted.td_namelen = len;
678 	wanted.td_name = ncp->nc_name;
679 
680 	TMPFS_VALIDATE_DIR(node);
681 
682 	de = RB_FIND(tmpfs_dirtree, &node->tn_dir.tn_dirtree, &wanted);
683 
684 	KKASSERT(f == NULL || de == NULL || f == de->td_node);
685 
686 	return de;
687 }
688 
689 /* --------------------------------------------------------------------- */
690 
691 /*
692  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
693  * directory and returns it in the uio space.  The function returns 0
694  * on success, -1 if there was not enough space in the uio structure to
695  * hold the directory entry or an appropriate error code if another
696  * error happens.
697  */
698 int
699 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
700 {
701 	int error;
702 
703 	TMPFS_VALIDATE_DIR(node);
704 	KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
705 
706 	if (vop_write_dirent(&error, uio, node->tn_id, DT_DIR, 1, "."))
707 		return -1;
708 	if (error == 0)
709 		uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
710 	return error;
711 }
712 
713 /* --------------------------------------------------------------------- */
714 
715 /*
716  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
717  * directory and returns it in the uio space.  The function returns 0
718  * on success, -1 if there was not enough space in the uio structure to
719  * hold the directory entry or an appropriate error code if another
720  * error happens.
721  */
722 int
723 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
724 			struct uio *uio)
725 {
726 	int error;
727 	ino_t d_ino;
728 
729 	TMPFS_VALIDATE_DIR(node);
730 	KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
731 
732 	if (node->tn_dir.tn_parent) {
733 		TMPFS_NODE_LOCK(node);
734 		if (node->tn_dir.tn_parent)
735 			d_ino = node->tn_dir.tn_parent->tn_id;
736 		else
737 			d_ino = tmp->tm_root->tn_id;
738 		TMPFS_NODE_UNLOCK(node);
739 	} else {
740 		d_ino = tmp->tm_root->tn_id;
741 	}
742 
743 	if (vop_write_dirent(&error, uio, d_ino, DT_DIR, 2, ".."))
744 		return -1;
745 	if (error == 0) {
746 		struct tmpfs_dirent *de;
747 		de = RB_MIN(tmpfs_dirtree_cookie, &node->tn_dir.tn_cookietree);
748 		if (de == NULL)
749 			uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
750 		else
751 			uio->uio_offset = tmpfs_dircookie(de);
752 	}
753 	return error;
754 }
755 
756 /* --------------------------------------------------------------------- */
757 
758 /*
759  * Lookup a directory entry by its associated cookie.
760  *
761  * Must be called with the directory node locked (shared ok)
762  */
763 struct lubycookie_info {
764 	off_t	cookie;
765 	struct tmpfs_dirent *de;
766 };
767 
768 static int
769 lubycookie_cmp(struct tmpfs_dirent *de, void *arg)
770 {
771 	struct lubycookie_info *info = arg;
772 	off_t cookie = tmpfs_dircookie(de);
773 
774 	if (cookie < info->cookie)
775 		return(-1);
776 	if (cookie > info->cookie)
777 		return(1);
778 	return(0);
779 }
780 
781 static int
782 lubycookie_callback(struct tmpfs_dirent *de, void *arg)
783 {
784 	struct lubycookie_info *info = arg;
785 
786 	if (tmpfs_dircookie(de) == info->cookie) {
787 		info->de = de;
788 		return(-1);
789 	}
790 	return(0);
791 }
792 
793 struct tmpfs_dirent *
794 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
795 {
796 	struct lubycookie_info info;
797 
798 	info.cookie = cookie;
799 	info.de = NULL;
800 	RB_SCAN(tmpfs_dirtree_cookie, &node->tn_dir.tn_cookietree,
801 		lubycookie_cmp, lubycookie_callback, &info);
802 	return (info.de);
803 }
804 
805 /* --------------------------------------------------------------------- */
806 
807 /*
808  * Helper function for tmpfs_readdir.  Returns as much directory entries
809  * as can fit in the uio space.  The read starts at uio->uio_offset.
810  * The function returns 0 on success, -1 if there was not enough space
811  * in the uio structure to hold the directory entry or an appropriate
812  * error code if another error happens.
813  *
814  * Caller must hold the node locked (shared ok)
815  */
816 int
817 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
818 {
819 	int error;
820 	off_t startcookie;
821 	struct tmpfs_dirent *de;
822 
823 	TMPFS_VALIDATE_DIR(node);
824 
825 	/*
826 	 * Locate the first directory entry we have to return.  We have cached
827 	 * the last readdir in the node, so use those values if appropriate.
828 	 * Otherwise do a linear scan to find the requested entry.
829 	 */
830 	startcookie = uio->uio_offset;
831 	KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
832 	KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
833 
834 	if (startcookie == TMPFS_DIRCOOKIE_EOF)
835 		return 0;
836 
837 	de = tmpfs_dir_lookupbycookie(node, startcookie);
838 	if (de == NULL)
839 		return EINVAL;
840 
841 	/*
842 	 * Read as much entries as possible; i.e., until we reach the end of
843 	 * the directory or we exhaust uio space.
844 	 */
845 	do {
846 		ino_t d_ino;
847 		uint8_t d_type;
848 
849 		/* Create a dirent structure representing the current
850 		 * tmpfs_node and fill it. */
851 		d_ino = de->td_node->tn_id;
852 		switch (de->td_node->tn_type) {
853 		case VBLK:
854 			d_type = DT_BLK;
855 			break;
856 
857 		case VCHR:
858 			d_type = DT_CHR;
859 			break;
860 
861 		case VDIR:
862 			d_type = DT_DIR;
863 			break;
864 
865 		case VFIFO:
866 			d_type = DT_FIFO;
867 			break;
868 
869 		case VLNK:
870 			d_type = DT_LNK;
871 			break;
872 
873 		case VREG:
874 			d_type = DT_REG;
875 			break;
876 
877 		case VSOCK:
878 			d_type = DT_SOCK;
879 			break;
880 
881 		default:
882 			panic("tmpfs_dir_getdents: type %p %d",
883 			    de->td_node, (int)de->td_node->tn_type);
884 		}
885 		KKASSERT(de->td_namelen < 256); /* 255 + 1 */
886 
887 		if (vop_write_dirent(&error, uio, d_ino, d_type,
888 		    de->td_namelen, de->td_name)) {
889 			error = -1;
890 			break;
891 		}
892 
893 		(*cntp)++;
894 		de = RB_NEXT(tmpfs_dirtree_cookie,
895 			     node->tn_dir.tn_cookietree, de);
896 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
897 
898 	/* Update the offset and cache. */
899 	if (de == NULL) {
900 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
901 	} else {
902 		uio->uio_offset = tmpfs_dircookie(de);
903 	}
904 
905 	return error;
906 }
907 
908 /* --------------------------------------------------------------------- */
909 
910 /*
911  * Resizes the aobj associated to the regular file pointed to by vp to
912  * the size newsize.  'vp' must point to a vnode that represents a regular
913  * file.  'newsize' must be positive.
914  *
915  * pass NVEXTF_TRIVIAL when buf content will be overwritten, otherwise set 0
916  * to be zero filled.
917  *
918  * Returns zero on success or an appropriate error code on failure.
919  *
920  * Caller must hold the node exclusively locked.
921  */
922 int
923 tmpfs_reg_resize(struct vnode *vp, off_t newsize, int trivial)
924 {
925 	int error;
926 	vm_pindex_t newpages, oldpages;
927 	struct tmpfs_mount *tmp;
928 	struct tmpfs_node *node;
929 	off_t oldsize;
930 	int nvextflags;
931 
932 #ifdef INVARIANTS
933 	KKASSERT(vp->v_type == VREG);
934 	KKASSERT(newsize >= 0);
935 #endif
936 
937 	node = VP_TO_TMPFS_NODE(vp);
938 	tmp = VFS_TO_TMPFS(vp->v_mount);
939 
940 	/*
941 	 * Convert the old and new sizes to the number of pages needed to
942 	 * store them.  It may happen that we do not need to do anything
943 	 * because the last allocated page can accommodate the change on
944 	 * its own.
945 	 */
946 	oldsize = node->tn_size;
947 	oldpages = round_page64(oldsize) / PAGE_SIZE;
948 	KKASSERT(oldpages == node->tn_reg.tn_aobj_pages);
949 	newpages = round_page64(newsize) / PAGE_SIZE;
950 
951 	if (newpages > oldpages &&
952 	   tmp->tm_pages_used + newpages - oldpages > tmp->tm_pages_max) {
953 		error = ENOSPC;
954 		goto out;
955 	}
956 	node->tn_reg.tn_aobj_pages = newpages;
957 	node->tn_size = newsize;
958 
959 	if (newpages != oldpages)
960 		atomic_add_long(&tmp->tm_pages_used, (newpages - oldpages));
961 
962 	/*
963 	 * nvextflags to pass along for bdwrite() vs buwrite()
964 	 */
965 	if (vm_pages_needed || vm_paging_needed(0) ||
966 	    tmpfs_bufcache_mode >= 2) {
967 		nvextflags = 0;
968 	} else {
969 		nvextflags = NVEXTF_BUWRITE;
970 	}
971 
972 
973 	/*
974 	 * When adjusting the vnode filesize and its VM object we must
975 	 * also adjust our backing VM object (aobj).  The blocksize
976 	 * used must match the block sized we use for the buffer cache.
977 	 *
978 	 * The backing VM object may contain VM pages as well as swap
979 	 * assignments if we previously renamed main object pages into
980 	 * it during deactivation.
981 	 *
982 	 * To make things easier tmpfs uses a blksize in multiples of
983 	 * PAGE_SIZE, and will only increase the blksize as a small file
984 	 * increases in size.  Once a file has exceeded TMPFS_BLKSIZE (16KB),
985 	 * the blksize is maxed out.  Truncating the file does not reduce
986 	 * the blksize.
987 	 */
988 	if (newsize < oldsize) {
989 		vm_pindex_t osize;
990 		vm_pindex_t nsize;
991 		vm_object_t aobj;
992 
993 		error = nvtruncbuf(vp, newsize, node->tn_blksize,
994 				   -1, nvextflags);
995 		aobj = node->tn_reg.tn_aobj;
996 		if (aobj) {
997 			osize = aobj->size;
998 			nsize = vp->v_object->size;
999 			if (nsize < osize) {
1000 				aobj->size = osize;
1001 				swap_pager_freespace(aobj, nsize,
1002 						     osize - nsize);
1003 				vm_object_page_remove(aobj, nsize, osize,
1004 						      FALSE);
1005 			}
1006 		}
1007 	} else {
1008 		vm_object_t aobj;
1009 		int nblksize;
1010 
1011 		/*
1012 		 * The first (and only the first) buffer in the file is resized
1013 		 * in multiples of PAGE_SIZE, up to TMPFS_BLKSIZE.
1014 		 */
1015 		nblksize = node->tn_blksize;
1016 		while (nblksize < TMPFS_BLKSIZE &&
1017 		       nblksize < newsize) {
1018 			nblksize += PAGE_SIZE;
1019 		}
1020 
1021 		if (trivial)
1022 			nvextflags |= NVEXTF_TRIVIAL;
1023 
1024 		error = nvextendbuf(vp, oldsize, newsize,
1025 				    node->tn_blksize, nblksize,
1026 				    -1, -1, nvextflags);
1027 		node->tn_blksize = nblksize;
1028 		aobj = node->tn_reg.tn_aobj;
1029 		if (aobj)
1030 			aobj->size = vp->v_object->size;
1031 	}
1032 
1033 out:
1034 	return error;
1035 }
1036 
1037 /* --------------------------------------------------------------------- */
1038 
1039 /*
1040  * Change flags of the given vnode.
1041  * Caller should execute tmpfs_update on vp after a successful execution.
1042  * The vnode must be locked on entry and remain locked on exit.
1043  */
1044 int
1045 tmpfs_chflags(struct vnode *vp, u_long vaflags, struct ucred *cred)
1046 {
1047 	int error;
1048 	struct tmpfs_node *node;
1049 	int flags;
1050 
1051 	KKASSERT(vn_islocked(vp));
1052 
1053 	node = VP_TO_TMPFS_NODE(vp);
1054 	flags = node->tn_flags;
1055 
1056 	/* Disallow this operation if the file system is mounted read-only. */
1057 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1058 		return EROFS;
1059 	error = vop_helper_setattr_flags(&flags, vaflags, node->tn_uid, cred);
1060 
1061 	/* Actually change the flags on the node itself */
1062 	if (error == 0) {
1063 		TMPFS_NODE_LOCK(node);
1064 		node->tn_flags = flags;
1065 		node->tn_status |= TMPFS_NODE_CHANGED;
1066 		TMPFS_NODE_UNLOCK(node);
1067 	}
1068 
1069 	KKASSERT(vn_islocked(vp));
1070 
1071 	return error;
1072 }
1073 
1074 /* --------------------------------------------------------------------- */
1075 
1076 /*
1077  * Change access mode on the given vnode.
1078  * Caller should execute tmpfs_update on vp after a successful execution.
1079  * The vnode must be locked on entry and remain locked on exit.
1080  */
1081 int
1082 tmpfs_chmod(struct vnode *vp, mode_t vamode, struct ucred *cred)
1083 {
1084 	struct tmpfs_node *node;
1085 	mode_t cur_mode;
1086 	int error;
1087 
1088 	KKASSERT(vn_islocked(vp));
1089 
1090 	node = VP_TO_TMPFS_NODE(vp);
1091 
1092 	/* Disallow this operation if the file system is mounted read-only. */
1093 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1094 		return EROFS;
1095 
1096 	/* Immutable or append-only files cannot be modified, either. */
1097 	if (node->tn_flags & (IMMUTABLE | APPEND))
1098 		return EPERM;
1099 
1100 	cur_mode = node->tn_mode;
1101 	error = vop_helper_chmod(vp, vamode, cred, node->tn_uid, node->tn_gid,
1102 				 &cur_mode);
1103 
1104 	if (error == 0 &&
1105 	    (node->tn_mode & ALLPERMS) != (cur_mode & ALLPERMS)) {
1106 		TMPFS_NODE_LOCK(node);
1107 		node->tn_mode &= ~ALLPERMS;
1108 		node->tn_mode |= cur_mode & ALLPERMS;
1109 
1110 		node->tn_status |= TMPFS_NODE_CHANGED;
1111 		TMPFS_NODE_UNLOCK(node);
1112 	}
1113 
1114 	KKASSERT(vn_islocked(vp));
1115 
1116 	return 0;
1117 }
1118 
1119 /* --------------------------------------------------------------------- */
1120 
1121 /*
1122  * Change ownership of the given vnode.  At least one of uid or gid must
1123  * be different than VNOVAL.  If one is set to that value, the attribute
1124  * is unchanged.
1125  * Caller should execute tmpfs_update on vp after a successful execution.
1126  * The vnode must be locked on entry and remain locked on exit.
1127  */
1128 int
1129 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1130 {
1131 	mode_t cur_mode;
1132 	uid_t cur_uid;
1133 	gid_t cur_gid;
1134 	struct tmpfs_node *node;
1135 	int error;
1136 
1137 	KKASSERT(vn_islocked(vp));
1138 	node = VP_TO_TMPFS_NODE(vp);
1139 
1140 	/* Disallow this operation if the file system is mounted read-only. */
1141 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1142 		return EROFS;
1143 
1144 	/* Immutable or append-only files cannot be modified, either. */
1145 	if (node->tn_flags & (IMMUTABLE | APPEND))
1146 		return EPERM;
1147 
1148 	cur_uid = node->tn_uid;
1149 	cur_gid = node->tn_gid;
1150 	cur_mode = node->tn_mode;
1151 	error = vop_helper_chown(vp, uid, gid, cred,
1152 				 &cur_uid, &cur_gid, &cur_mode);
1153 
1154 	if (error == 0) {
1155 		TMPFS_NODE_LOCK(node);
1156 		if (cur_uid != node->tn_uid ||
1157 		    cur_gid != node->tn_gid ||
1158 		    cur_mode != node->tn_mode) {
1159 			node->tn_uid = cur_uid;
1160 			node->tn_gid = cur_gid;
1161 			node->tn_mode = cur_mode;
1162 			node->tn_status |= TMPFS_NODE_CHANGED;
1163 		}
1164 		TMPFS_NODE_UNLOCK(node);
1165 	}
1166 
1167 	return error;
1168 }
1169 
1170 /* --------------------------------------------------------------------- */
1171 
1172 /*
1173  * Change size of the given vnode.
1174  * Caller should execute tmpfs_update on vp after a successful execution.
1175  * The vnode must be locked on entry and remain locked on exit.
1176  */
1177 int
1178 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred)
1179 {
1180 	int error;
1181 	struct tmpfs_node *node;
1182 
1183 	KKASSERT(vn_islocked(vp));
1184 
1185 	node = VP_TO_TMPFS_NODE(vp);
1186 
1187 	/* Decide whether this is a valid operation based on the file type. */
1188 	error = 0;
1189 	switch (vp->v_type) {
1190 	case VDIR:
1191 		return EISDIR;
1192 
1193 	case VREG:
1194 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1195 			return EROFS;
1196 		break;
1197 
1198 	case VBLK:
1199 		/* FALLTHROUGH */
1200 	case VCHR:
1201 		/* FALLTHROUGH */
1202 	case VFIFO:
1203 		/* Allow modifications of special files even if in the file
1204 		 * system is mounted read-only (we are not modifying the
1205 		 * files themselves, but the objects they represent). */
1206 		return 0;
1207 
1208 	default:
1209 		/* Anything else is unsupported. */
1210 		return EOPNOTSUPP;
1211 	}
1212 
1213 	/* Immutable or append-only files cannot be modified, either. */
1214 	if (node->tn_flags & (IMMUTABLE | APPEND))
1215 		return EPERM;
1216 
1217 	error = tmpfs_truncate(vp, size);
1218 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1219 	 * for us, as will update tn_status; no need to do that here. */
1220 
1221 	KKASSERT(vn_islocked(vp));
1222 
1223 	return error;
1224 }
1225 
1226 /* --------------------------------------------------------------------- */
1227 
1228 /*
1229  * Change access and modification times of the given vnode.
1230  * Caller should execute tmpfs_update on vp after a successful execution.
1231  * The vnode must be locked on entry and remain locked on exit.
1232  */
1233 int
1234 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1235 	      int vaflags, struct ucred *cred)
1236 {
1237 	struct tmpfs_node *node;
1238 
1239 	KKASSERT(vn_islocked(vp));
1240 
1241 	node = VP_TO_TMPFS_NODE(vp);
1242 
1243 	/* Disallow this operation if the file system is mounted read-only. */
1244 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1245 		return EROFS;
1246 
1247 	/* Immutable or append-only files cannot be modified, either. */
1248 	if (node->tn_flags & (IMMUTABLE | APPEND))
1249 		return EPERM;
1250 
1251 	TMPFS_NODE_LOCK(node);
1252 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1253 		node->tn_status |= TMPFS_NODE_ACCESSED;
1254 
1255 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) {
1256 		node->tn_status |= TMPFS_NODE_MODIFIED;
1257 		vclrflags(vp, VLASTWRITETS);
1258 	}
1259 
1260 	TMPFS_NODE_UNLOCK(node);
1261 
1262 	tmpfs_itimes(vp, atime, mtime);
1263 
1264 	KKASSERT(vn_islocked(vp));
1265 
1266 	return 0;
1267 }
1268 
1269 /* --------------------------------------------------------------------- */
1270 /* Sync timestamps */
1271 void
1272 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1273 	     const struct timespec *mod)
1274 {
1275 	struct tmpfs_node *node;
1276 	struct timespec now;
1277 
1278 	node = VP_TO_TMPFS_NODE(vp);
1279 
1280 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1281 	    TMPFS_NODE_CHANGED)) == 0)
1282 		return;
1283 
1284 	vfs_timestamp(&now);
1285 
1286 	TMPFS_NODE_LOCK(node);
1287 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1288 		if (acc == NULL)
1289 			 acc = &now;
1290 		node->tn_atime = acc->tv_sec;
1291 		node->tn_atimensec = acc->tv_nsec;
1292 	}
1293 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1294 		if (mod == NULL)
1295 			mod = &now;
1296 		node->tn_mtime = mod->tv_sec;
1297 		node->tn_mtimensec = mod->tv_nsec;
1298 	}
1299 	if (node->tn_status & TMPFS_NODE_CHANGED) {
1300 		node->tn_ctime = now.tv_sec;
1301 		node->tn_ctimensec = now.tv_nsec;
1302 	}
1303 
1304 	node->tn_status &= ~(TMPFS_NODE_ACCESSED |
1305 			     TMPFS_NODE_MODIFIED |
1306 			     TMPFS_NODE_CHANGED);
1307 	TMPFS_NODE_UNLOCK(node);
1308 }
1309 
1310 /* --------------------------------------------------------------------- */
1311 
1312 void
1313 tmpfs_update(struct vnode *vp)
1314 {
1315 	tmpfs_itimes(vp, NULL, NULL);
1316 }
1317 
1318 /* --------------------------------------------------------------------- */
1319 
1320 /*
1321  * Caller must hold an exclusive node lock.
1322  */
1323 int
1324 tmpfs_truncate(struct vnode *vp, off_t length)
1325 {
1326 	int error;
1327 	struct tmpfs_node *node;
1328 
1329 	node = VP_TO_TMPFS_NODE(vp);
1330 
1331 	if (length < 0) {
1332 		error = EINVAL;
1333 		goto out;
1334 	}
1335 
1336 	if (node->tn_size == length) {
1337 		error = 0;
1338 		goto out;
1339 	}
1340 
1341 	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1342 		return (EFBIG);
1343 
1344 
1345 	error = tmpfs_reg_resize(vp, length, 1);
1346 
1347 	if (error == 0)
1348 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1349 
1350 out:
1351 	tmpfs_update(vp);
1352 
1353 	return error;
1354 }
1355 
1356 /* --------------------------------------------------------------------- */
1357 
1358 static ino_t
1359 tmpfs_fetch_ino(struct tmpfs_mount *tmp)
1360 {
1361 	ino_t ret;
1362 
1363 	ret = atomic_fetchadd_64(&tmp->tm_ino, 1);
1364 
1365 	return (ret);
1366 }
1367 
1368 static int
1369 tmpfs_dirtree_compare(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1370 {
1371 	if (a->td_namelen > b->td_namelen)
1372 		return 1;
1373 	else if (a->td_namelen < b->td_namelen)
1374 		return -1;
1375 	else
1376 		return strncmp(a->td_name, b->td_name, a->td_namelen);
1377 }
1378 
1379 static int
1380 tmpfs_dirtree_compare_cookie(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1381 {
1382 	if (a < b)
1383 		return(-1);
1384 	if (a > b)
1385 		return(1);
1386 	return 0;
1387 }
1388 
1389 /*
1390  * Lock for rename.  The namecache entries are already locked so
1391  * theoretically we should be able to lock the directories in any
1392  * order.  Underlying files must be locked after the related directory.
1393  */
1394 void
1395 tmpfs_lock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
1396 	    struct tmpfs_node *node3, struct tmpfs_node *node4)
1397 {
1398 	TMPFS_NODE_LOCK(node1);		/* fdir */
1399 	TMPFS_NODE_LOCK(node3);		/* ffile */
1400 	TMPFS_NODE_LOCK(node2);		/* tdir */
1401 	if (node4)
1402 		TMPFS_NODE_LOCK(node4);	/* tfile */
1403 }
1404 
1405 void
1406 tmpfs_unlock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
1407 	      struct tmpfs_node *node3, struct tmpfs_node *node4)
1408 {
1409 	TMPFS_NODE_UNLOCK(node1);
1410 	TMPFS_NODE_UNLOCK(node2);
1411 	TMPFS_NODE_UNLOCK(node3);
1412 	if (node4)
1413 		TMPFS_NODE_UNLOCK(node4);
1414 }
1415