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