xref: /dragonfly/sys/vfs/tmpfs/tmpfs_subr.c (revision c8860c9a)
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(), this is
982 	 * so tmpfs activity doesn't eat memory being freed by the pageout
983 	 * daemon.
984 	 */
985 	if (vm_pages_needed || vm_paging_start(0) ||
986 	    tmpfs_bufcache_mode >= 2) {
987 		nvextflags = 0;
988 	} else {
989 		nvextflags = NVEXTF_BUWRITE;
990 	}
991 
992 
993 	/*
994 	 * When adjusting the vnode filesize and its VM object we must
995 	 * also adjust our backing VM object (aobj).  The blocksize
996 	 * used must match the block sized we use for the buffer cache.
997 	 *
998 	 * The backing VM object may contain VM pages as well as swap
999 	 * assignments if we previously renamed main object pages into
1000 	 * it during deactivation.
1001 	 *
1002 	 * To make things easier tmpfs uses a blksize in multiples of
1003 	 * PAGE_SIZE, and will only increase the blksize as a small file
1004 	 * increases in size.  Once a file has exceeded TMPFS_BLKSIZE (16KB),
1005 	 * the blksize is maxed out.  Truncating the file does not reduce
1006 	 * the blksize.
1007 	 */
1008 	if (newsize < oldsize) {
1009 		vm_pindex_t osize;
1010 		vm_pindex_t nsize;
1011 		vm_object_t aobj;
1012 
1013 		error = nvtruncbuf(vp, newsize, node->tn_blksize,
1014 				   -1, nvextflags);
1015 		aobj = node->tn_reg.tn_aobj;
1016 		if (aobj) {
1017 			osize = aobj->size;
1018 			nsize = vp->v_object->size;
1019 			if (nsize < osize) {
1020 				aobj->size = osize;
1021 				swap_pager_freespace(aobj, nsize,
1022 						     osize - nsize);
1023 				vm_object_page_remove(aobj, nsize, osize,
1024 						      FALSE);
1025 			}
1026 		}
1027 	} else {
1028 		vm_object_t aobj;
1029 		int nblksize;
1030 
1031 		/*
1032 		 * The first (and only the first) buffer in the file is resized
1033 		 * in multiples of PAGE_SIZE, up to TMPFS_BLKSIZE.
1034 		 */
1035 		nblksize = node->tn_blksize;
1036 		while (nblksize < TMPFS_BLKSIZE &&
1037 		       nblksize < newsize) {
1038 			nblksize += PAGE_SIZE;
1039 		}
1040 
1041 		if (trivial)
1042 			nvextflags |= NVEXTF_TRIVIAL;
1043 
1044 		error = nvextendbuf(vp, oldsize, newsize,
1045 				    node->tn_blksize, nblksize,
1046 				    -1, -1, nvextflags);
1047 		node->tn_blksize = nblksize;
1048 		aobj = node->tn_reg.tn_aobj;
1049 		if (aobj)
1050 			aobj->size = vp->v_object->size;
1051 	}
1052 
1053 out:
1054 	return error;
1055 }
1056 
1057 /* --------------------------------------------------------------------- */
1058 
1059 /*
1060  * Change flags of the given vnode.
1061  * Caller should execute tmpfs_update on vp after a successful execution.
1062  * The vnode must be locked on entry and remain locked on exit.
1063  */
1064 int
1065 tmpfs_chflags(struct vnode *vp, u_long vaflags, struct ucred *cred)
1066 {
1067 	int error;
1068 	struct tmpfs_node *node;
1069 	int flags;
1070 
1071 	KKASSERT(vn_islocked(vp));
1072 
1073 	node = VP_TO_TMPFS_NODE(vp);
1074 	flags = node->tn_flags;
1075 
1076 	/* Disallow this operation if the file system is mounted read-only. */
1077 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1078 		return EROFS;
1079 	error = vop_helper_setattr_flags(&flags, vaflags, node->tn_uid, cred);
1080 
1081 	/* Actually change the flags on the node itself */
1082 	if (error == 0) {
1083 		TMPFS_NODE_LOCK(node);
1084 		node->tn_flags = flags;
1085 		node->tn_status |= TMPFS_NODE_CHANGED;
1086 		TMPFS_NODE_UNLOCK(node);
1087 	}
1088 
1089 	KKASSERT(vn_islocked(vp));
1090 
1091 	return error;
1092 }
1093 
1094 /* --------------------------------------------------------------------- */
1095 
1096 /*
1097  * Change access mode on the given vnode.
1098  * Caller should execute tmpfs_update on vp after a successful execution.
1099  * The vnode must be locked on entry and remain locked on exit.
1100  */
1101 int
1102 tmpfs_chmod(struct vnode *vp, mode_t vamode, struct ucred *cred)
1103 {
1104 	struct tmpfs_node *node;
1105 	mode_t cur_mode;
1106 	int error;
1107 
1108 	KKASSERT(vn_islocked(vp));
1109 
1110 	node = VP_TO_TMPFS_NODE(vp);
1111 
1112 	/* Disallow this operation if the file system is mounted read-only. */
1113 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1114 		return EROFS;
1115 
1116 	/* Immutable or append-only files cannot be modified, either. */
1117 	if (node->tn_flags & (IMMUTABLE | APPEND))
1118 		return EPERM;
1119 
1120 	cur_mode = node->tn_mode;
1121 	error = vop_helper_chmod(vp, vamode, cred, node->tn_uid, node->tn_gid,
1122 				 &cur_mode);
1123 
1124 	if (error == 0 &&
1125 	    (node->tn_mode & ALLPERMS) != (cur_mode & ALLPERMS)) {
1126 		TMPFS_NODE_LOCK(node);
1127 		node->tn_mode &= ~ALLPERMS;
1128 		node->tn_mode |= cur_mode & ALLPERMS;
1129 
1130 		node->tn_status |= TMPFS_NODE_CHANGED;
1131 		TMPFS_NODE_UNLOCK(node);
1132 	}
1133 
1134 	KKASSERT(vn_islocked(vp));
1135 
1136 	return 0;
1137 }
1138 
1139 /* --------------------------------------------------------------------- */
1140 
1141 /*
1142  * Change ownership of the given vnode.  At least one of uid or gid must
1143  * be different than VNOVAL.  If one is set to that value, the attribute
1144  * is unchanged.
1145  * Caller should execute tmpfs_update on vp after a successful execution.
1146  * The vnode must be locked on entry and remain locked on exit.
1147  */
1148 int
1149 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1150 {
1151 	mode_t cur_mode;
1152 	uid_t cur_uid;
1153 	gid_t cur_gid;
1154 	struct tmpfs_node *node;
1155 	int error;
1156 
1157 	KKASSERT(vn_islocked(vp));
1158 	node = VP_TO_TMPFS_NODE(vp);
1159 
1160 	/* Disallow this operation if the file system is mounted read-only. */
1161 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1162 		return EROFS;
1163 
1164 	/* Immutable or append-only files cannot be modified, either. */
1165 	if (node->tn_flags & (IMMUTABLE | APPEND))
1166 		return EPERM;
1167 
1168 	cur_uid = node->tn_uid;
1169 	cur_gid = node->tn_gid;
1170 	cur_mode = node->tn_mode;
1171 	error = vop_helper_chown(vp, uid, gid, cred,
1172 				 &cur_uid, &cur_gid, &cur_mode);
1173 
1174 	if (error == 0) {
1175 		TMPFS_NODE_LOCK(node);
1176 		if (cur_uid != node->tn_uid ||
1177 		    cur_gid != node->tn_gid ||
1178 		    cur_mode != node->tn_mode) {
1179 			node->tn_uid = cur_uid;
1180 			node->tn_gid = cur_gid;
1181 			node->tn_mode = cur_mode;
1182 			node->tn_status |= TMPFS_NODE_CHANGED;
1183 		}
1184 		TMPFS_NODE_UNLOCK(node);
1185 	}
1186 
1187 	return error;
1188 }
1189 
1190 /* --------------------------------------------------------------------- */
1191 
1192 /*
1193  * Change size of the given vnode.
1194  * Caller should execute tmpfs_update on vp after a successful execution.
1195  * The vnode must be locked on entry and remain locked on exit.
1196  */
1197 int
1198 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred)
1199 {
1200 	int error;
1201 	struct tmpfs_node *node;
1202 
1203 	KKASSERT(vn_islocked(vp));
1204 
1205 	node = VP_TO_TMPFS_NODE(vp);
1206 
1207 	/* Decide whether this is a valid operation based on the file type. */
1208 	error = 0;
1209 	switch (vp->v_type) {
1210 	case VDIR:
1211 		return EISDIR;
1212 
1213 	case VREG:
1214 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1215 			return EROFS;
1216 		break;
1217 
1218 	case VBLK:
1219 		/* FALLTHROUGH */
1220 	case VCHR:
1221 		/* FALLTHROUGH */
1222 	case VFIFO:
1223 		/* Allow modifications of special files even if in the file
1224 		 * system is mounted read-only (we are not modifying the
1225 		 * files themselves, but the objects they represent). */
1226 		return 0;
1227 
1228 	default:
1229 		/* Anything else is unsupported. */
1230 		return EOPNOTSUPP;
1231 	}
1232 
1233 	/* Immutable or append-only files cannot be modified, either. */
1234 	if (node->tn_flags & (IMMUTABLE | APPEND))
1235 		return EPERM;
1236 
1237 	error = tmpfs_truncate(vp, size);
1238 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1239 	 * for us, as will update tn_status; no need to do that here. */
1240 
1241 	KKASSERT(vn_islocked(vp));
1242 
1243 	return error;
1244 }
1245 
1246 /* --------------------------------------------------------------------- */
1247 
1248 /*
1249  * Change access and modification times of the given vnode.
1250  * Caller should execute tmpfs_update on vp after a successful execution.
1251  * The vnode must be locked on entry and remain locked on exit.
1252  */
1253 int
1254 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1255 	      int vaflags, struct ucred *cred)
1256 {
1257 	struct tmpfs_node *node;
1258 
1259 	KKASSERT(vn_islocked(vp));
1260 
1261 	node = VP_TO_TMPFS_NODE(vp);
1262 
1263 	/* Disallow this operation if the file system is mounted read-only. */
1264 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1265 		return EROFS;
1266 
1267 	/* Immutable or append-only files cannot be modified, either. */
1268 	if (node->tn_flags & (IMMUTABLE | APPEND))
1269 		return EPERM;
1270 
1271 	TMPFS_NODE_LOCK(node);
1272 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1273 		node->tn_status |= TMPFS_NODE_ACCESSED;
1274 
1275 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) {
1276 		node->tn_status |= TMPFS_NODE_MODIFIED;
1277 		vclrflags(vp, VLASTWRITETS);
1278 	}
1279 
1280 	TMPFS_NODE_UNLOCK(node);
1281 
1282 	tmpfs_itimes(vp, atime, mtime);
1283 
1284 	KKASSERT(vn_islocked(vp));
1285 
1286 	return 0;
1287 }
1288 
1289 /* --------------------------------------------------------------------- */
1290 /* Sync timestamps */
1291 void
1292 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1293 	     const struct timespec *mod)
1294 {
1295 	struct tmpfs_node *node;
1296 	struct timespec now;
1297 
1298 	node = VP_TO_TMPFS_NODE(vp);
1299 
1300 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1301 	    TMPFS_NODE_CHANGED)) == 0) {
1302 		return;
1303 	}
1304 
1305 	vfs_timestamp(&now);
1306 
1307 	TMPFS_NODE_LOCK(node);
1308 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1309 		if (acc == NULL)
1310 			 acc = &now;
1311 		node->tn_atime = acc->tv_sec;
1312 		node->tn_atimensec = acc->tv_nsec;
1313 	}
1314 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1315 		if (mod == NULL)
1316 			mod = &now;
1317 		node->tn_mtime = mod->tv_sec;
1318 		node->tn_mtimensec = mod->tv_nsec;
1319 	}
1320 	if (node->tn_status & TMPFS_NODE_CHANGED) {
1321 		node->tn_ctime = now.tv_sec;
1322 		node->tn_ctimensec = now.tv_nsec;
1323 	}
1324 
1325 	node->tn_status &= ~(TMPFS_NODE_ACCESSED |
1326 			     TMPFS_NODE_MODIFIED |
1327 			     TMPFS_NODE_CHANGED);
1328 	TMPFS_NODE_UNLOCK(node);
1329 }
1330 
1331 /* --------------------------------------------------------------------- */
1332 
1333 void
1334 tmpfs_update(struct vnode *vp)
1335 {
1336 	tmpfs_itimes(vp, NULL, NULL);
1337 }
1338 
1339 /* --------------------------------------------------------------------- */
1340 
1341 /*
1342  * Caller must hold an exclusive node lock.
1343  */
1344 int
1345 tmpfs_truncate(struct vnode *vp, off_t length)
1346 {
1347 	int error;
1348 	struct tmpfs_node *node;
1349 
1350 	node = VP_TO_TMPFS_NODE(vp);
1351 
1352 	if (length < 0) {
1353 		error = EINVAL;
1354 		goto out;
1355 	}
1356 
1357 	if (node->tn_size == length) {
1358 		error = 0;
1359 		goto out;
1360 	}
1361 
1362 	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1363 		return (EFBIG);
1364 
1365 
1366 	error = tmpfs_reg_resize(vp, length, 1);
1367 
1368 	if (error == 0)
1369 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1370 
1371 out:
1372 	tmpfs_update(vp);
1373 
1374 	return error;
1375 }
1376 
1377 /* --------------------------------------------------------------------- */
1378 
1379 static ino_t
1380 tmpfs_fetch_ino(struct tmpfs_mount *tmp)
1381 {
1382 	ino_t ret;
1383 
1384 	ret = atomic_fetchadd_64(&tmp->tm_ino, 1);
1385 
1386 	return (ret);
1387 }
1388 
1389 static int
1390 tmpfs_dirtree_compare(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1391 {
1392 	if (a->td_namelen > b->td_namelen)
1393 		return 1;
1394 	else if (a->td_namelen < b->td_namelen)
1395 		return -1;
1396 	else
1397 		return strncmp(a->td_name, b->td_name, a->td_namelen);
1398 }
1399 
1400 static int
1401 tmpfs_dirtree_compare_cookie(struct tmpfs_dirent *a, struct tmpfs_dirent *b)
1402 {
1403 	if (a < b)
1404 		return(-1);
1405 	if (a > b)
1406 		return(1);
1407 	return 0;
1408 }
1409 
1410 /*
1411  * Lock for rename.  The namecache entries for the related terminal files
1412  * are already locked but the directories are not.  A directory lock order
1413  * reversal is possible so use a deterministic order.
1414  *
1415  * Generally order path parent-to-child or using a simple pointer comparison.
1416  * Probably not perfect but it should catch most of the cases.
1417  *
1418  * Underlying files must be locked after the related directory.
1419  */
1420 void
1421 tmpfs_lock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
1422 	    struct tmpfs_node *node3, struct tmpfs_node *node4)
1423 {
1424 	if (node1->tn_dir.tn_parent != node2 &&
1425 	    (node1 < node2 || node2->tn_dir.tn_parent == node1)) {
1426 		TMPFS_NODE_LOCK(node1);		/* fdir */
1427 		TMPFS_NODE_LOCK(node3);		/* ffile */
1428 		TMPFS_NODE_LOCK(node2);		/* tdir */
1429 		if (node4)
1430 			TMPFS_NODE_LOCK(node4);	/* tfile */
1431 	} else {
1432 		TMPFS_NODE_LOCK(node2);		/* tdir */
1433 		if (node4)
1434 			TMPFS_NODE_LOCK(node4);	/* tfile */
1435 		TMPFS_NODE_LOCK(node1);		/* fdir */
1436 		TMPFS_NODE_LOCK(node3);		/* ffile */
1437 	}
1438 }
1439 
1440 void
1441 tmpfs_unlock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
1442 	      struct tmpfs_node *node3, struct tmpfs_node *node4)
1443 {
1444 	if (node4)
1445 		TMPFS_NODE_UNLOCK(node4);
1446 	TMPFS_NODE_UNLOCK(node2);
1447 	TMPFS_NODE_UNLOCK(node3);
1448 	TMPFS_NODE_UNLOCK(node1);
1449 }
1450