xref: /dragonfly/sys/vfs/tmpfs/tmpfs_subr.c (revision 631c21f2)
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 					vn_unlock(vp);
420 				}
421 				vdrop(vp);
422 				TMPFS_NODE_LOCK_SH(dnode);
423 
424 				return EAGAIN;
425 			}
426 		} else {
427 			/*
428 			 * Normal path
429 			 */
430 			if (vget(vp, lkflag | LK_EXCLUSIVE) != 0) {
431 				vdrop(vp);
432 				goto loop;
433 			}
434 		}
435 		if (node->tn_vnode != vp) {
436 			vput(vp);
437 			vdrop(vp);
438 			goto loop;
439 		}
440 		vdrop(vp);
441 		goto out;
442 	}
443 
444 	/*
445 	 * We need to assign node->tn_vnode.  If vp is NULL, loop up to
446 	 * allocate the vp.  This can happen due to SMP races.
447 	 */
448 	if (vp == NULL) {
449 		TMPFS_NODE_UNLOCK(node);
450 		goto loop;
451 	}
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 	TMPFS_NODE_LOCK(dnode);
535 
536 	/*
537 	 * If the directory was removed but a process was CD'd into it,
538 	 * we do not allow any more file/dir creation within it.  Otherwise
539 	 * we will lose track of it.
540 	 */
541 	KKASSERT(dnode->tn_type == VDIR);
542 	if (dnode != tmp->tm_root && dnode->tn_dir.tn_parent == NULL) {
543 		TMPFS_NODE_UNLOCK(dnode);
544 		return ENOENT;
545 	}
546 
547 	/*
548 	 * Make sure the link count does not overflow.
549 	 */
550 	if (vap->va_type == VDIR && dnode->tn_links >= LINK_MAX) {
551 		TMPFS_NODE_UNLOCK(dnode);
552 		return EMLINK;
553 	}
554 
555 	/* Allocate a node that represents the new file. */
556 	error = tmpfs_alloc_node(tmp, vap->va_type, cred->cr_uid,
557 				 dnode->tn_gid, vap->va_mode, target,
558 				 vap->va_rmajor, vap->va_rminor, &node);
559 	if (error != 0) {
560 		TMPFS_NODE_UNLOCK(dnode);
561 		return error;
562 	}
563 	TMPFS_NODE_LOCK(node);
564 
565 	/* Allocate a directory entry that points to the new file. */
566 	error = tmpfs_alloc_dirent(tmp, node, ncp->nc_name, ncp->nc_nlen, &de);
567 	if (error != 0) {
568 		TMPFS_NODE_UNLOCK(dnode);
569 		tmpfs_free_node(tmp, node);
570 		/* eats node lock */
571 		return error;
572 	}
573 
574 	/* Allocate a vnode for the new file. */
575 	error = tmpfs_alloc_vp(dvp->v_mount, NULL, node, LK_EXCLUSIVE, vpp);
576 	if (error != 0) {
577 		TMPFS_NODE_UNLOCK(dnode);
578 		tmpfs_free_dirent(tmp, de);
579 		tmpfs_free_node(tmp, node);
580 		/* eats node lock */
581 		return error;
582 	}
583 
584 	/*
585 	 * Now that all required items are allocated, we can proceed to
586 	 * insert the new node into the directory, an operation that
587 	 * cannot fail.
588 	 */
589 	tmpfs_dir_attach_locked(dnode, de);
590 	TMPFS_NODE_UNLOCK(dnode);
591 	TMPFS_NODE_UNLOCK(node);
592 
593 	return error;
594 }
595 
596 /* --------------------------------------------------------------------- */
597 
598 /*
599  * Attaches the directory entry de to the directory represented by dnode.
600  * Note that this does not change the link count of the node pointed by
601  * the directory entry, as this is done by tmpfs_alloc_dirent.
602  *
603  * dnode must be locked.
604  */
605 void
606 tmpfs_dir_attach_locked(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
607 {
608 	struct tmpfs_node *node = de->td_node;
609 	struct tmpfs_dirent *de2;
610 
611 	if (node && node->tn_type == VDIR) {
612 		TMPFS_NODE_LOCK(node);
613 		atomic_add_int(&node->tn_links, 1);
614 		node->tn_status |= TMPFS_NODE_CHANGED;
615 		node->tn_dir.tn_parent = dnode;
616 		atomic_add_int(&dnode->tn_links, 1);
617 		TMPFS_NODE_UNLOCK(node);
618 	}
619 	de2 = RB_INSERT(tmpfs_dirtree, &dnode->tn_dir.tn_dirtree, de);
620 	KASSERT(de2 == NULL,
621 		("tmpfs_dir_attach_lockedA: duplicate insertion of %p, has %p\n",
622 		de, de2));
623 	de2 = RB_INSERT(tmpfs_dirtree_cookie, &dnode->tn_dir.tn_cookietree, de);
624 	KASSERT(de2 == NULL,
625 		("tmpfs_dir_attach_lockedB: duplicate insertion of %p, has %p\n",
626 		de, de2));
627 	dnode->tn_size += sizeof(struct tmpfs_dirent);
628 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
629 			    TMPFS_NODE_MODIFIED;
630 }
631 
632 /* --------------------------------------------------------------------- */
633 
634 /*
635  * Detaches the directory entry de from the directory represented by dnode.
636  * Note that this does not change the link count of the node pointed by
637  * the directory entry, as this is done by tmpfs_free_dirent.
638  *
639  * dnode must be locked.
640  */
641 void
642 tmpfs_dir_detach_locked(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
643 {
644 	struct tmpfs_node *node = de->td_node;
645 
646 	RB_REMOVE(tmpfs_dirtree, &dnode->tn_dir.tn_dirtree, de);
647 	RB_REMOVE(tmpfs_dirtree_cookie, &dnode->tn_dir.tn_cookietree, de);
648 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
649 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
650 			    TMPFS_NODE_MODIFIED;
651 
652 	/*
653 	 * Clean out the tn_parent pointer immediately when removing a
654 	 * directory.
655 	 *
656 	 * Removal of the parent linkage also cleans out the extra tn_links
657 	 * count we had on both node and dnode.
658 	 *
659 	 * node can be NULL (typ during a forced umount), in which case
660 	 * the mount code is dealing with the linkages from a linked list
661 	 * scan.
662 	 */
663 	if (node && node->tn_type == VDIR && node->tn_dir.tn_parent) {
664 		TMPFS_NODE_LOCK(node);
665 		KKASSERT(node->tn_dir.tn_parent == dnode);
666 		atomic_add_int(&dnode->tn_links, -1);
667 		atomic_add_int(&node->tn_links, -1);
668 		node->tn_dir.tn_parent = NULL;
669 		TMPFS_NODE_UNLOCK(node);
670 	}
671 }
672 
673 /* --------------------------------------------------------------------- */
674 
675 /*
676  * Looks for a directory entry in the directory represented by node.
677  * 'ncp' describes the name of the entry to look for.  Note that the .
678  * and .. components are not allowed as they do not physically exist
679  * within directories.
680  *
681  * Returns a pointer to the entry when found, otherwise NULL.
682  *
683  * Caller must hold the node locked (shared ok)
684  */
685 struct tmpfs_dirent *
686 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
687 		 struct namecache *ncp)
688 {
689 	struct tmpfs_dirent *de;
690 	int len = ncp->nc_nlen;
691 	struct tmpfs_dirent wanted;
692 
693 	wanted.td_namelen = len;
694 	wanted.td_name = ncp->nc_name;
695 
696 	TMPFS_VALIDATE_DIR(node);
697 
698 	de = RB_FIND(tmpfs_dirtree, &node->tn_dir.tn_dirtree, &wanted);
699 
700 	KASSERT((f == NULL || de == NULL || f == de->td_node),
701 		("tmpfs_dir_lookup: Incorrect node %p %p %p",
702 		 f, de, (de ? de->td_node : NULL)));
703 
704 	return de;
705 }
706 
707 /* --------------------------------------------------------------------- */
708 
709 /*
710  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
711  * directory and returns it in the uio space.  The function returns 0
712  * on success, -1 if there was not enough space in the uio structure to
713  * hold the directory entry or an appropriate error code if another
714  * error happens.
715  */
716 int
717 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
718 {
719 	int error;
720 
721 	TMPFS_VALIDATE_DIR(node);
722 	KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
723 
724 	if (vop_write_dirent(&error, uio, node->tn_id, DT_DIR, 1, "."))
725 		return -1;
726 	if (error == 0)
727 		uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
728 	return error;
729 }
730 
731 /* --------------------------------------------------------------------- */
732 
733 /*
734  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
735  * directory and returns it in the uio space.  The function returns 0
736  * on success, -1 if there was not enough space in the uio structure to
737  * hold the directory entry or an appropriate error code if another
738  * error happens.
739  */
740 int
741 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
742 			struct uio *uio)
743 {
744 	int error;
745 	ino_t d_ino;
746 
747 	TMPFS_VALIDATE_DIR(node);
748 	KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
749 
750 	if (node->tn_dir.tn_parent) {
751 		TMPFS_NODE_LOCK(node);
752 		if (node->tn_dir.tn_parent)
753 			d_ino = node->tn_dir.tn_parent->tn_id;
754 		else
755 			d_ino = tmp->tm_root->tn_id;
756 		TMPFS_NODE_UNLOCK(node);
757 	} else {
758 		d_ino = tmp->tm_root->tn_id;
759 	}
760 
761 	if (vop_write_dirent(&error, uio, d_ino, DT_DIR, 2, ".."))
762 		return -1;
763 	if (error == 0) {
764 		struct tmpfs_dirent *de;
765 		de = RB_MIN(tmpfs_dirtree_cookie, &node->tn_dir.tn_cookietree);
766 		if (de == NULL)
767 			uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
768 		else
769 			uio->uio_offset = tmpfs_dircookie(de);
770 	}
771 	return error;
772 }
773 
774 /* --------------------------------------------------------------------- */
775 
776 /*
777  * Lookup a directory entry by its associated cookie.
778  *
779  * Must be called with the directory node locked (shared ok)
780  */
781 struct lubycookie_info {
782 	off_t	cookie;
783 	struct tmpfs_dirent *de;
784 };
785 
786 static int
787 lubycookie_cmp(struct tmpfs_dirent *de, void *arg)
788 {
789 	struct lubycookie_info *info = arg;
790 	off_t cookie = tmpfs_dircookie(de);
791 
792 	if (cookie < info->cookie)
793 		return(-1);
794 	if (cookie > info->cookie)
795 		return(1);
796 	return(0);
797 }
798 
799 static int
800 lubycookie_callback(struct tmpfs_dirent *de, void *arg)
801 {
802 	struct lubycookie_info *info = arg;
803 
804 	if (tmpfs_dircookie(de) == info->cookie) {
805 		info->de = de;
806 		return(-1);
807 	}
808 	return(0);
809 }
810 
811 struct tmpfs_dirent *
812 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
813 {
814 	struct lubycookie_info info;
815 
816 	info.cookie = cookie;
817 	info.de = NULL;
818 	RB_SCAN(tmpfs_dirtree_cookie, &node->tn_dir.tn_cookietree,
819 		lubycookie_cmp, lubycookie_callback, &info);
820 	return (info.de);
821 }
822 
823 /* --------------------------------------------------------------------- */
824 
825 /*
826  * Helper function for tmpfs_readdir.  Returns as much directory entries
827  * as can fit in the uio space.  The read starts at uio->uio_offset.
828  * The function returns 0 on success, -1 if there was not enough space
829  * in the uio structure to hold the directory entry or an appropriate
830  * error code if another error happens.
831  *
832  * Caller must hold the node locked (shared ok)
833  */
834 int
835 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
836 {
837 	int error;
838 	off_t startcookie;
839 	struct tmpfs_dirent *de;
840 
841 	TMPFS_VALIDATE_DIR(node);
842 
843 	/*
844 	 * Locate the first directory entry we have to return.  We have cached
845 	 * the last readdir in the node, so use those values if appropriate.
846 	 * Otherwise do a linear scan to find the requested entry.
847 	 */
848 	startcookie = uio->uio_offset;
849 	KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
850 	KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
851 
852 	if (startcookie == TMPFS_DIRCOOKIE_EOF)
853 		return 0;
854 
855 	de = tmpfs_dir_lookupbycookie(node, startcookie);
856 	if (de == NULL)
857 		return EINVAL;
858 
859 	/*
860 	 * Read as much entries as possible; i.e., until we reach the end of
861 	 * the directory or we exhaust uio space.
862 	 */
863 	do {
864 		ino_t d_ino;
865 		uint8_t d_type;
866 
867 		/* Create a dirent structure representing the current
868 		 * tmpfs_node and fill it. */
869 		d_ino = de->td_node->tn_id;
870 		switch (de->td_node->tn_type) {
871 		case VBLK:
872 			d_type = DT_BLK;
873 			break;
874 
875 		case VCHR:
876 			d_type = DT_CHR;
877 			break;
878 
879 		case VDIR:
880 			d_type = DT_DIR;
881 			break;
882 
883 		case VFIFO:
884 			d_type = DT_FIFO;
885 			break;
886 
887 		case VLNK:
888 			d_type = DT_LNK;
889 			break;
890 
891 		case VREG:
892 			d_type = DT_REG;
893 			break;
894 
895 		case VSOCK:
896 			d_type = DT_SOCK;
897 			break;
898 
899 		default:
900 			panic("tmpfs_dir_getdents: type %p %d",
901 			    de->td_node, (int)de->td_node->tn_type);
902 		}
903 		KKASSERT(de->td_namelen < 256); /* 255 + 1 */
904 
905 		if (vop_write_dirent(&error, uio, d_ino, d_type,
906 		    de->td_namelen, de->td_name)) {
907 			error = -1;
908 			break;
909 		}
910 
911 		(*cntp)++;
912 		de = RB_NEXT(tmpfs_dirtree_cookie,
913 			     node->tn_dir.tn_cookietree, de);
914 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
915 
916 	/* Update the offset and cache. */
917 	if (de == NULL) {
918 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
919 	} else {
920 		uio->uio_offset = tmpfs_dircookie(de);
921 	}
922 
923 	return error;
924 }
925 
926 /* --------------------------------------------------------------------- */
927 
928 /*
929  * Resizes the aobj associated to the regular file pointed to by vp to
930  * the size newsize.  'vp' must point to a vnode that represents a regular
931  * file.  'newsize' must be positive.
932  *
933  * pass NVEXTF_TRIVIAL when buf content will be overwritten, otherwise set 0
934  * to be zero filled.
935  *
936  * Returns zero on success or an appropriate error code on failure.
937  *
938  * Caller must hold the node exclusively locked.
939  */
940 int
941 tmpfs_reg_resize(struct vnode *vp, off_t newsize, int trivial)
942 {
943 	int error;
944 	vm_pindex_t newpages, oldpages;
945 	struct tmpfs_mount *tmp;
946 	struct tmpfs_node *node;
947 	off_t oldsize;
948 	int nvextflags;
949 
950 #ifdef INVARIANTS
951 	KKASSERT(vp->v_type == VREG);
952 	KKASSERT(newsize >= 0);
953 #endif
954 
955 	node = VP_TO_TMPFS_NODE(vp);
956 	tmp = VFS_TO_TMPFS(vp->v_mount);
957 
958 	/*
959 	 * Convert the old and new sizes to the number of pages needed to
960 	 * store them.  It may happen that we do not need to do anything
961 	 * because the last allocated page can accommodate the change on
962 	 * its own.
963 	 */
964 	oldsize = node->tn_size;
965 	oldpages = round_page64(oldsize) / PAGE_SIZE;
966 	KKASSERT(oldpages == node->tn_reg.tn_aobj_pages);
967 	newpages = round_page64(newsize) / PAGE_SIZE;
968 
969 	if (newpages > oldpages &&
970 	   tmp->tm_pages_used + newpages - oldpages > tmp->tm_pages_max) {
971 		error = ENOSPC;
972 		goto out;
973 	}
974 	node->tn_reg.tn_aobj_pages = newpages;
975 	node->tn_size = newsize;
976 
977 	if (newpages != oldpages)
978 		atomic_add_long(&tmp->tm_pages_used, (newpages - oldpages));
979 
980 	/*
981 	 * nvextflags to pass along for bdwrite() vs buwrite()
982 	 */
983 	if (vm_pages_needed || vm_paging_needed(0) ||
984 	    tmpfs_bufcache_mode >= 2) {
985 		nvextflags = 0;
986 	} else {
987 		nvextflags = NVEXTF_BUWRITE;
988 	}
989 
990 
991 	/*
992 	 * When adjusting the vnode filesize and its VM object we must
993 	 * also adjust our backing VM object (aobj).  The blocksize
994 	 * used must match the block sized we use for the buffer cache.
995 	 *
996 	 * The backing VM object may contain VM pages as well as swap
997 	 * assignments if we previously renamed main object pages into
998 	 * it during deactivation.
999 	 *
1000 	 * To make things easier tmpfs uses a blksize in multiples of
1001 	 * PAGE_SIZE, and will only increase the blksize as a small file
1002 	 * increases in size.  Once a file has exceeded TMPFS_BLKSIZE (16KB),
1003 	 * the blksize is maxed out.  Truncating the file does not reduce
1004 	 * the blksize.
1005 	 */
1006 	if (newsize < oldsize) {
1007 		vm_pindex_t osize;
1008 		vm_pindex_t nsize;
1009 		vm_object_t aobj;
1010 
1011 		error = nvtruncbuf(vp, newsize, node->tn_blksize,
1012 				   -1, nvextflags);
1013 		aobj = node->tn_reg.tn_aobj;
1014 		if (aobj) {
1015 			osize = aobj->size;
1016 			nsize = vp->v_object->size;
1017 			if (nsize < osize) {
1018 				aobj->size = osize;
1019 				swap_pager_freespace(aobj, nsize,
1020 						     osize - nsize);
1021 				vm_object_page_remove(aobj, nsize, osize,
1022 						      FALSE);
1023 			}
1024 		}
1025 	} else {
1026 		vm_object_t aobj;
1027 		int nblksize;
1028 
1029 		/*
1030 		 * The first (and only the first) buffer in the file is resized
1031 		 * in multiples of PAGE_SIZE, up to TMPFS_BLKSIZE.
1032 		 */
1033 		nblksize = node->tn_blksize;
1034 		while (nblksize < TMPFS_BLKSIZE &&
1035 		       nblksize < newsize) {
1036 			nblksize += PAGE_SIZE;
1037 		}
1038 
1039 		if (trivial)
1040 			nvextflags |= NVEXTF_TRIVIAL;
1041 
1042 		error = nvextendbuf(vp, oldsize, newsize,
1043 				    node->tn_blksize, nblksize,
1044 				    -1, -1, nvextflags);
1045 		node->tn_blksize = nblksize;
1046 		aobj = node->tn_reg.tn_aobj;
1047 		if (aobj)
1048 			aobj->size = vp->v_object->size;
1049 	}
1050 
1051 out:
1052 	return error;
1053 }
1054 
1055 /* --------------------------------------------------------------------- */
1056 
1057 /*
1058  * Change flags of the given vnode.
1059  * Caller should execute tmpfs_update on vp after a successful execution.
1060  * The vnode must be locked on entry and remain locked on exit.
1061  */
1062 int
1063 tmpfs_chflags(struct vnode *vp, u_long vaflags, struct ucred *cred)
1064 {
1065 	int error;
1066 	struct tmpfs_node *node;
1067 	int flags;
1068 
1069 	KKASSERT(vn_islocked(vp));
1070 
1071 	node = VP_TO_TMPFS_NODE(vp);
1072 	flags = node->tn_flags;
1073 
1074 	/* Disallow this operation if the file system is mounted read-only. */
1075 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1076 		return EROFS;
1077 	error = vop_helper_setattr_flags(&flags, vaflags, node->tn_uid, cred);
1078 
1079 	/* Actually change the flags on the node itself */
1080 	if (error == 0) {
1081 		TMPFS_NODE_LOCK(node);
1082 		node->tn_flags = flags;
1083 		node->tn_status |= TMPFS_NODE_CHANGED;
1084 		TMPFS_NODE_UNLOCK(node);
1085 	}
1086 
1087 	KKASSERT(vn_islocked(vp));
1088 
1089 	return error;
1090 }
1091 
1092 /* --------------------------------------------------------------------- */
1093 
1094 /*
1095  * Change access mode on the given vnode.
1096  * Caller should execute tmpfs_update on vp after a successful execution.
1097  * The vnode must be locked on entry and remain locked on exit.
1098  */
1099 int
1100 tmpfs_chmod(struct vnode *vp, mode_t vamode, struct ucred *cred)
1101 {
1102 	struct tmpfs_node *node;
1103 	mode_t cur_mode;
1104 	int error;
1105 
1106 	KKASSERT(vn_islocked(vp));
1107 
1108 	node = VP_TO_TMPFS_NODE(vp);
1109 
1110 	/* Disallow this operation if the file system is mounted read-only. */
1111 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1112 		return EROFS;
1113 
1114 	/* Immutable or append-only files cannot be modified, either. */
1115 	if (node->tn_flags & (IMMUTABLE | APPEND))
1116 		return EPERM;
1117 
1118 	cur_mode = node->tn_mode;
1119 	error = vop_helper_chmod(vp, vamode, cred, node->tn_uid, node->tn_gid,
1120 				 &cur_mode);
1121 
1122 	if (error == 0 &&
1123 	    (node->tn_mode & ALLPERMS) != (cur_mode & ALLPERMS)) {
1124 		TMPFS_NODE_LOCK(node);
1125 		node->tn_mode &= ~ALLPERMS;
1126 		node->tn_mode |= cur_mode & ALLPERMS;
1127 
1128 		node->tn_status |= TMPFS_NODE_CHANGED;
1129 		TMPFS_NODE_UNLOCK(node);
1130 	}
1131 
1132 	KKASSERT(vn_islocked(vp));
1133 
1134 	return 0;
1135 }
1136 
1137 /* --------------------------------------------------------------------- */
1138 
1139 /*
1140  * Change ownership of the given vnode.  At least one of uid or gid must
1141  * be different than VNOVAL.  If one is set to that value, the attribute
1142  * is unchanged.
1143  * Caller should execute tmpfs_update on vp after a successful execution.
1144  * The vnode must be locked on entry and remain locked on exit.
1145  */
1146 int
1147 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1148 {
1149 	mode_t cur_mode;
1150 	uid_t cur_uid;
1151 	gid_t cur_gid;
1152 	struct tmpfs_node *node;
1153 	int error;
1154 
1155 	KKASSERT(vn_islocked(vp));
1156 	node = VP_TO_TMPFS_NODE(vp);
1157 
1158 	/* Disallow this operation if the file system is mounted read-only. */
1159 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1160 		return EROFS;
1161 
1162 	/* Immutable or append-only files cannot be modified, either. */
1163 	if (node->tn_flags & (IMMUTABLE | APPEND))
1164 		return EPERM;
1165 
1166 	cur_uid = node->tn_uid;
1167 	cur_gid = node->tn_gid;
1168 	cur_mode = node->tn_mode;
1169 	error = vop_helper_chown(vp, uid, gid, cred,
1170 				 &cur_uid, &cur_gid, &cur_mode);
1171 
1172 	if (error == 0) {
1173 		TMPFS_NODE_LOCK(node);
1174 		if (cur_uid != node->tn_uid ||
1175 		    cur_gid != node->tn_gid ||
1176 		    cur_mode != node->tn_mode) {
1177 			node->tn_uid = cur_uid;
1178 			node->tn_gid = cur_gid;
1179 			node->tn_mode = cur_mode;
1180 			node->tn_status |= TMPFS_NODE_CHANGED;
1181 		}
1182 		TMPFS_NODE_UNLOCK(node);
1183 	}
1184 
1185 	return error;
1186 }
1187 
1188 /* --------------------------------------------------------------------- */
1189 
1190 /*
1191  * Change size of the given vnode.
1192  * Caller should execute tmpfs_update on vp after a successful execution.
1193  * The vnode must be locked on entry and remain locked on exit.
1194  */
1195 int
1196 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred)
1197 {
1198 	int error;
1199 	struct tmpfs_node *node;
1200 
1201 	KKASSERT(vn_islocked(vp));
1202 
1203 	node = VP_TO_TMPFS_NODE(vp);
1204 
1205 	/* Decide whether this is a valid operation based on the file type. */
1206 	error = 0;
1207 	switch (vp->v_type) {
1208 	case VDIR:
1209 		return EISDIR;
1210 
1211 	case VREG:
1212 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1213 			return EROFS;
1214 		break;
1215 
1216 	case VBLK:
1217 		/* FALLTHROUGH */
1218 	case VCHR:
1219 		/* FALLTHROUGH */
1220 	case VFIFO:
1221 		/* Allow modifications of special files even if in the file
1222 		 * system is mounted read-only (we are not modifying the
1223 		 * files themselves, but the objects they represent). */
1224 		return 0;
1225 
1226 	default:
1227 		/* Anything else is unsupported. */
1228 		return EOPNOTSUPP;
1229 	}
1230 
1231 	/* Immutable or append-only files cannot be modified, either. */
1232 	if (node->tn_flags & (IMMUTABLE | APPEND))
1233 		return EPERM;
1234 
1235 	error = tmpfs_truncate(vp, size);
1236 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1237 	 * for us, as will update tn_status; no need to do that here. */
1238 
1239 	KKASSERT(vn_islocked(vp));
1240 
1241 	return error;
1242 }
1243 
1244 /* --------------------------------------------------------------------- */
1245 
1246 /*
1247  * Change access and modification times of the given vnode.
1248  * Caller should execute tmpfs_update on vp after a successful execution.
1249  * The vnode must be locked on entry and remain locked on exit.
1250  */
1251 int
1252 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1253 	      int vaflags, struct ucred *cred)
1254 {
1255 	struct tmpfs_node *node;
1256 
1257 	KKASSERT(vn_islocked(vp));
1258 
1259 	node = VP_TO_TMPFS_NODE(vp);
1260 
1261 	/* Disallow this operation if the file system is mounted read-only. */
1262 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1263 		return EROFS;
1264 
1265 	/* Immutable or append-only files cannot be modified, either. */
1266 	if (node->tn_flags & (IMMUTABLE | APPEND))
1267 		return EPERM;
1268 
1269 	TMPFS_NODE_LOCK(node);
1270 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1271 		node->tn_status |= TMPFS_NODE_ACCESSED;
1272 
1273 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) {
1274 		node->tn_status |= TMPFS_NODE_MODIFIED;
1275 		vclrflags(vp, VLASTWRITETS);
1276 	}
1277 
1278 	TMPFS_NODE_UNLOCK(node);
1279 
1280 	tmpfs_itimes(vp, atime, mtime);
1281 
1282 	KKASSERT(vn_islocked(vp));
1283 
1284 	return 0;
1285 }
1286 
1287 /* --------------------------------------------------------------------- */
1288 /* Sync timestamps */
1289 void
1290 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1291 	     const struct timespec *mod)
1292 {
1293 	struct tmpfs_node *node;
1294 	struct timespec now;
1295 
1296 	node = VP_TO_TMPFS_NODE(vp);
1297 
1298 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1299 	    TMPFS_NODE_CHANGED)) == 0) {
1300 		return;
1301 	}
1302 
1303 	vfs_timestamp(&now);
1304 
1305 	TMPFS_NODE_LOCK(node);
1306 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1307 		if (acc == NULL)
1308 			 acc = &now;
1309 		node->tn_atime = acc->tv_sec;
1310 		node->tn_atimensec = acc->tv_nsec;
1311 	}
1312 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1313 		if (mod == NULL)
1314 			mod = &now;
1315 		node->tn_mtime = mod->tv_sec;
1316 		node->tn_mtimensec = mod->tv_nsec;
1317 	}
1318 	if (node->tn_status & TMPFS_NODE_CHANGED) {
1319 		node->tn_ctime = now.tv_sec;
1320 		node->tn_ctimensec = now.tv_nsec;
1321 	}
1322 
1323 	node->tn_status &= ~(TMPFS_NODE_ACCESSED |
1324 			     TMPFS_NODE_MODIFIED |
1325 			     TMPFS_NODE_CHANGED);
1326 	TMPFS_NODE_UNLOCK(node);
1327 }
1328 
1329 /* --------------------------------------------------------------------- */
1330 
1331 void
1332 tmpfs_update(struct vnode *vp)
1333 {
1334 	tmpfs_itimes(vp, NULL, NULL);
1335 }
1336 
1337 /* --------------------------------------------------------------------- */
1338 
1339 /*
1340  * Caller must hold an exclusive node lock.
1341  */
1342 int
1343 tmpfs_truncate(struct vnode *vp, off_t length)
1344 {
1345 	int error;
1346 	struct tmpfs_node *node;
1347 
1348 	node = VP_TO_TMPFS_NODE(vp);
1349 
1350 	if (length < 0) {
1351 		error = EINVAL;
1352 		goto out;
1353 	}
1354 
1355 	if (node->tn_size == length) {
1356 		error = 0;
1357 		goto out;
1358 	}
1359 
1360 	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1361 		return (EFBIG);
1362 
1363 
1364 	error = tmpfs_reg_resize(vp, length, 1);
1365 
1366 	if (error == 0)
1367 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1368 
1369 out:
1370 	tmpfs_update(vp);
1371 
1372 	return error;
1373 }
1374 
1375 /* --------------------------------------------------------------------- */
1376 
1377 static ino_t
1378 tmpfs_fetch_ino(struct tmpfs_mount *tmp)
1379 {
1380 	ino_t ret;
1381 
1382 	ret = atomic_fetchadd_64(&tmp->tm_ino, 1);
1383 
1384 	return (ret);
1385 }
1386 
1387 static int
1388 tmpfs_dirtree_compare(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1389 {
1390 	if (a->td_namelen > b->td_namelen)
1391 		return 1;
1392 	else if (a->td_namelen < b->td_namelen)
1393 		return -1;
1394 	else
1395 		return strncmp(a->td_name, b->td_name, a->td_namelen);
1396 }
1397 
1398 static int
1399 tmpfs_dirtree_compare_cookie(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1400 {
1401 	if (a < b)
1402 		return(-1);
1403 	if (a > b)
1404 		return(1);
1405 	return 0;
1406 }
1407 
1408 /*
1409  * Lock for rename.  The namecache entries for the related terminal files
1410  * are already locked but the directories are not.  A directory lock order
1411  * reversal is possible so use a deterministic order.
1412  *
1413  * Generally order path parent-to-child or using a simple pointer comparison.
1414  * Probably not perfect but it should catch most of the cases.
1415  *
1416  * Underlying files must be locked after the related directory.
1417  */
1418 void
1419 tmpfs_lock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
1420 	    struct tmpfs_node *node3, struct tmpfs_node *node4)
1421 {
1422 	if (node1->tn_dir.tn_parent != node2 &&
1423 	    (node1 < node2 || node2->tn_dir.tn_parent == node1)) {
1424 		TMPFS_NODE_LOCK(node1);		/* fdir */
1425 		TMPFS_NODE_LOCK(node3);		/* ffile */
1426 		TMPFS_NODE_LOCK(node2);		/* tdir */
1427 		if (node4)
1428 			TMPFS_NODE_LOCK(node4);	/* tfile */
1429 	} else {
1430 		TMPFS_NODE_LOCK(node2);		/* tdir */
1431 		if (node4)
1432 			TMPFS_NODE_LOCK(node4);	/* tfile */
1433 		TMPFS_NODE_LOCK(node1);		/* fdir */
1434 		TMPFS_NODE_LOCK(node3);		/* ffile */
1435 	}
1436 }
1437 
1438 void
1439 tmpfs_unlock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
1440 	      struct tmpfs_node *node3, struct tmpfs_node *node4)
1441 {
1442 	if (node4)
1443 		TMPFS_NODE_UNLOCK(node4);
1444 	TMPFS_NODE_UNLOCK(node2);
1445 	TMPFS_NODE_UNLOCK(node3);
1446 	TMPFS_NODE_UNLOCK(node1);
1447 }
1448