xref: /netbsd-src/sys/fs/tmpfs/tmpfs_subr.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: tmpfs_subr.c,v 1.41 2007/11/26 19:01:54 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Efficient memory file system supporting functions.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.41 2007/11/26 19:01:54 pooka Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/dirent.h>
49 #include <sys/event.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/time.h>
54 #include <sys/stat.h>
55 #include <sys/systm.h>
56 #include <sys/swap.h>
57 #include <sys/vnode.h>
58 #include <sys/kauth.h>
59 #include <sys/proc.h>
60 
61 #include <uvm/uvm.h>
62 
63 #include <miscfs/specfs/specdev.h>
64 #include <fs/tmpfs/tmpfs.h>
65 #include <fs/tmpfs/tmpfs_fifoops.h>
66 #include <fs/tmpfs/tmpfs_specops.h>
67 #include <fs/tmpfs/tmpfs_vnops.h>
68 
69 MALLOC_DECLARE(M_TMPFSTMP);
70 
71 /* --------------------------------------------------------------------- */
72 
73 /*
74  * Allocates a new node of type 'type' inside the 'tmp' mount point, with
75  * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
76  * using the credentials of the process 'p'.
77  *
78  * If the node type is set to 'VDIR', then the parent parameter must point
79  * to the parent directory of the node being created.  It may only be NULL
80  * while allocating the root node.
81  *
82  * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
83  * specifies the device the node represents.
84  *
85  * If the node type is set to 'VLNK', then the parameter target specifies
86  * the file name of the target file for the symbolic link that is being
87  * created.
88  *
89  * Note that new nodes are retrieved from the available list if it has
90  * items or, if it is empty, from the node pool as long as there is enough
91  * space to create them.
92  *
93  * Returns zero on success or an appropriate error code on failure.
94  */
95 int
96 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
97     uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
98     char *target, dev_t rdev, struct proc *p, struct tmpfs_node **node)
99 {
100 	struct tmpfs_node *nnode;
101 
102 	/* If the root directory of the 'tmp' file system is not yet
103 	 * allocated, this must be the request to do it. */
104 	KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
105 
106 	KASSERT(IFF(type == VLNK, target != NULL));
107 	KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
108 
109 	KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
110 
111 	nnode = NULL;
112 	if (LIST_EMPTY(&tmp->tm_nodes_avail)) {
113 		KASSERT(tmp->tm_nodes_last <= tmp->tm_nodes_max);
114 		if (tmp->tm_nodes_last == tmp->tm_nodes_max)
115 			return ENOSPC;
116 
117 		nnode =
118 		    (struct tmpfs_node *)TMPFS_POOL_GET(&tmp->tm_node_pool, 0);
119 		if (nnode == NULL)
120 			return ENOSPC;
121 		nnode->tn_id = tmp->tm_nodes_last++;
122 		nnode->tn_gen = arc4random();
123 	} else {
124 		nnode = LIST_FIRST(&tmp->tm_nodes_avail);
125 		LIST_REMOVE(nnode, tn_entries);
126 		nnode->tn_gen++;
127 	}
128 	KASSERT(nnode != NULL);
129 	LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
130 
131 	/* Generic initialization. */
132 	nnode->tn_type = type;
133 	nnode->tn_size = 0;
134 	nnode->tn_status = 0;
135 	nnode->tn_flags = 0;
136 	nnode->tn_links = 0;
137 	getnanotime(&nnode->tn_atime);
138 	nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
139 	    nnode->tn_atime;
140 	nnode->tn_uid = uid;
141 	nnode->tn_gid = gid;
142 	nnode->tn_mode = mode;
143 	nnode->tn_lockf = NULL;
144 	nnode->tn_vnode = NULL;
145 
146 	/* Type-specific initialization. */
147 	switch (nnode->tn_type) {
148 	case VBLK:
149 	case VCHR:
150 		nnode->tn_spec.tn_dev.tn_rdev = rdev;
151 		break;
152 
153 	case VDIR:
154 		TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
155 		nnode->tn_spec.tn_dir.tn_parent =
156 		    (parent == NULL) ? nnode : parent;
157 		nnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
158 		nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
159 		nnode->tn_links++;
160 		nnode->tn_spec.tn_dir.tn_parent->tn_links++;
161 		if (parent != NULL) {
162 			KASSERT(parent->tn_vnode != NULL);
163 			VN_KNOTE(parent->tn_vnode, NOTE_LINK);
164 		}
165 		break;
166 
167 	case VFIFO:
168 		/* FALLTHROUGH */
169 	case VSOCK:
170 		break;
171 
172 	case VLNK:
173 		KASSERT(strlen(target) < MAXPATHLEN);
174 		nnode->tn_size = strlen(target);
175 		nnode->tn_spec.tn_lnk.tn_link =
176 		    tmpfs_str_pool_get(&tmp->tm_str_pool, nnode->tn_size, 0);
177 		if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
178 			nnode->tn_type = VNON;
179 			tmpfs_free_node(tmp, nnode);
180 			return ENOSPC;
181 		}
182 		memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
183 		break;
184 
185 	case VREG:
186 		nnode->tn_spec.tn_reg.tn_aobj =
187 		    uao_create(INT32_MAX - PAGE_SIZE, 0);
188 		nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
189 		break;
190 
191 	default:
192 		KASSERT(0);
193 	}
194 
195 	*node = nnode;
196 	return 0;
197 }
198 
199 /* --------------------------------------------------------------------- */
200 
201 /*
202  * Destroys the node pointed to by node from the file system 'tmp'.
203  * If the node does not belong to the given mount point, the results are
204  * unpredicted.
205  *
206  * If the node references a directory; no entries are allowed because
207  * their removal could need a recursive algorithm, something forbidden in
208  * kernel space.  Furthermore, there is not need to provide such
209  * functionality (recursive removal) because the only primitives offered
210  * to the user are the removal of empty directories and the deletion of
211  * individual files.
212  *
213  * Note that nodes are not really deleted; in fact, when a node has been
214  * allocated, it cannot be deleted during the whole life of the file
215  * system.  Instead, they are moved to the available list and remain there
216  * until reused.
217  */
218 void
219 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
220 {
221 	ino_t id;
222 	unsigned long gen;
223 	size_t pages;
224 
225 	switch (node->tn_type) {
226 	case VNON:
227 		/* Do not do anything.  VNON is provided to let the
228 		 * allocation routine clean itself easily by avoiding
229 		 * duplicating code in it. */
230 		/* FALLTHROUGH */
231 	case VBLK:
232 		/* FALLTHROUGH */
233 	case VCHR:
234 		/* FALLTHROUGH */
235 	case VDIR:
236 		/* FALLTHROUGH */
237 	case VFIFO:
238 		/* FALLTHROUGH */
239 	case VSOCK:
240 		pages = 0;
241 		break;
242 
243 	case VLNK:
244 		tmpfs_str_pool_put(&tmp->tm_str_pool,
245 		    node->tn_spec.tn_lnk.tn_link, node->tn_size);
246 		pages = 0;
247 		break;
248 
249 	case VREG:
250 		if (node->tn_spec.tn_reg.tn_aobj != NULL)
251 			uao_detach(node->tn_spec.tn_reg.tn_aobj);
252 		pages = node->tn_spec.tn_reg.tn_aobj_pages;
253 		break;
254 
255 	default:
256 		KASSERT(0);
257 		pages = 0; /* Shut up gcc when !DIAGNOSTIC. */
258 		break;
259 	}
260 
261 	tmp->tm_pages_used -= pages;
262 
263 	LIST_REMOVE(node, tn_entries);
264 	id = node->tn_id;
265 	gen = node->tn_gen;
266 	memset(node, 0, sizeof(struct tmpfs_node));
267 	node->tn_id = id;
268 	node->tn_type = VNON;
269 	node->tn_gen = gen;
270 	LIST_INSERT_HEAD(&tmp->tm_nodes_avail, node, tn_entries);
271 }
272 
273 /* --------------------------------------------------------------------- */
274 
275 /*
276  * Allocates a new directory entry for the node node with a name of name.
277  * The new directory entry is returned in *de.
278  *
279  * The link count of node is increased by one to reflect the new object
280  * referencing it.  This takes care of notifying kqueue listeners about
281  * this change.
282  *
283  * Returns zero on success or an appropriate error code on failure.
284  */
285 int
286 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
287     const char *name, uint16_t len, struct tmpfs_dirent **de)
288 {
289 	struct tmpfs_dirent *nde;
290 
291 	nde = (struct tmpfs_dirent *)TMPFS_POOL_GET(&tmp->tm_dirent_pool, 0);
292 	if (nde == NULL)
293 		return ENOSPC;
294 
295 	nde->td_name = tmpfs_str_pool_get(&tmp->tm_str_pool, len, 0);
296 	if (nde->td_name == NULL) {
297 		TMPFS_POOL_PUT(&tmp->tm_dirent_pool, nde);
298 		return ENOSPC;
299 	}
300 	nde->td_namelen = len;
301 	memcpy(nde->td_name, name, len);
302 	nde->td_node = node;
303 
304 	node->tn_links++;
305 	if (node->tn_links > 1 && node->tn_vnode != NULL)
306 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
307 	*de = nde;
308 
309 	return 0;
310 }
311 
312 /* --------------------------------------------------------------------- */
313 
314 /*
315  * Frees a directory entry.  It is the caller's responsibility to destroy
316  * the node referenced by it if needed.
317  *
318  * The link count of node is decreased by one to reflect the removal of an
319  * object that referenced it.  This only happens if 'node_exists' is true;
320  * otherwise the function will not access the node referred to by the
321  * directory entry, as it may already have been released from the outside.
322  *
323  * Interested parties (kqueue) are notified of the link count change; note
324  * that this can include both the node pointed to by the directory entry
325  * as well as its parent.
326  */
327 void
328 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
329     bool node_exists)
330 {
331 	if (node_exists) {
332 		struct tmpfs_node *node;
333 
334 		node = de->td_node;
335 
336 		KASSERT(node->tn_links > 0);
337 		node->tn_links--;
338 		if (node->tn_vnode != NULL)
339 			VN_KNOTE(node->tn_vnode, node->tn_links == 0 ?
340 			    NOTE_DELETE : NOTE_LINK);
341 		if (node->tn_type == VDIR)
342 			VN_KNOTE(node->tn_spec.tn_dir.tn_parent->tn_vnode,
343 			    NOTE_LINK);
344 	}
345 
346 	tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name, de->td_namelen);
347 	TMPFS_POOL_PUT(&tmp->tm_dirent_pool, de);
348 }
349 
350 /* --------------------------------------------------------------------- */
351 
352 /*
353  * Allocates a new vnode for the node node or returns a new reference to
354  * an existing one if the node had already a vnode referencing it.  The
355  * resulting locked vnode is returned in *vpp.
356  *
357  * Returns zero on success or an appropriate error code on failure.
358  */
359 int
360 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
361 {
362 	int error;
363 	struct vnode *nvp;
364 	struct vnode *vp;
365 
366 	vp = NULL;
367 
368 	if (node->tn_vnode != NULL) {
369 		vp = node->tn_vnode;
370 		vget(vp, LK_EXCLUSIVE | LK_RETRY);
371 		error = 0;
372 		goto out;
373 	}
374 
375 	/* Get a new vnode and associate it with our node. */
376 	error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp);
377 	if (error != 0)
378 		goto out;
379 	KASSERT(vp != NULL);
380 
381 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
382 	if (error != 0) {
383 		vp->v_data = NULL;
384 		ungetnewvnode(vp);
385 		vp = NULL;
386 		goto out;
387 	}
388 
389 	vp->v_data = node;
390 	vp->v_type = node->tn_type;
391 
392 	/* Type-specific initialization. */
393 	switch (node->tn_type) {
394 	case VBLK:
395 		/* FALLTHROUGH */
396 	case VCHR:
397 		vp->v_op = tmpfs_specop_p;
398 		nvp = checkalias(vp, node->tn_spec.tn_dev.tn_rdev, mp);
399 		if (nvp != NULL) {
400 			/* Discard unneeded vnode, but save its inode. */
401 			nvp->v_data = vp->v_data;
402 			vp->v_data = NULL;
403 
404 			/* XXX spec_vnodeops has no locking, so we have to
405 			 * do it explicitly. */
406 			vp->v_vflag &= ~VV_LOCKSWORK;
407 			VOP_UNLOCK(vp, 0);
408 			vp->v_op = spec_vnodeop_p;
409 			vrele(vp);
410 			vgone(vp);
411 
412 			/* Reinitialize aliased node. */
413 			vp = nvp;
414 			error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
415 			if (error != 0) {
416 				vp->v_data = NULL;
417 				vp = NULL;
418 				goto out;
419 			}
420 		}
421 		break;
422 
423 	case VDIR:
424 		vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
425 		    VV_ROOT : 0;
426 		break;
427 
428 	case VFIFO:
429 		vp->v_op = tmpfs_fifoop_p;
430 		break;
431 
432 	case VLNK:
433 		/* FALLTHROUGH */
434 	case VREG:
435 		/* FALLTHROUGH */
436 	case VSOCK:
437 		break;
438 
439 	default:
440 		KASSERT(0);
441 	}
442 
443 	uvm_vnp_setsize(vp, node->tn_size);
444 
445 	error = 0;
446 
447 out:
448 	*vpp = node->tn_vnode = vp;
449 
450 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
451 	KASSERT(*vpp == node->tn_vnode);
452 
453 	return error;
454 }
455 
456 /* --------------------------------------------------------------------- */
457 
458 /*
459  * Destroys the association between the vnode vp and the node it
460  * references.
461  */
462 void
463 tmpfs_free_vp(struct vnode *vp)
464 {
465 	struct tmpfs_node *node;
466 
467 	node = VP_TO_TMPFS_NODE(vp);
468 
469 	node->tn_vnode = NULL;
470 	vp->v_data = NULL;
471 }
472 
473 /* --------------------------------------------------------------------- */
474 
475 /*
476  * Allocates a new file of type 'type' and adds it to the parent directory
477  * 'dvp'; this addition is done using the component name given in 'cnp'.
478  * The ownership of the new file is automatically assigned based on the
479  * credentials of the caller (through 'cnp'), the group is set based on
480  * the parent directory and the mode is determined from the 'vap' argument.
481  * If successful, *vpp holds a vnode to the newly created file and zero
482  * is returned.  Otherwise *vpp is NULL and the function returns an
483  * appropriate error code.
484  */
485 int
486 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
487     struct componentname *cnp, char *target)
488 {
489 	int error;
490 	struct tmpfs_dirent *de;
491 	struct tmpfs_mount *tmp;
492 	struct tmpfs_node *dnode;
493 	struct tmpfs_node *node;
494 	struct tmpfs_node *parent;
495 
496 	KASSERT(VOP_ISLOCKED(dvp));
497 	KASSERT(cnp->cn_flags & HASBUF);
498 
499 	tmp = VFS_TO_TMPFS(dvp->v_mount);
500 	dnode = VP_TO_TMPFS_DIR(dvp);
501 	*vpp = NULL;
502 
503 	/* If the entry we are creating is a directory, we cannot overflow
504 	 * the number of links of its parent, because it will get a new
505 	 * link. */
506 	if (vap->va_type == VDIR) {
507 		/* Ensure that we do not overflow the maximum number of links
508 		 * imposed by the system. */
509 		KASSERT(dnode->tn_links <= LINK_MAX);
510 		if (dnode->tn_links == LINK_MAX) {
511 			error = EMLINK;
512 			goto out;
513 		}
514 
515 		parent = dnode;
516 	} else
517 		parent = NULL;
518 
519 	/* Allocate a node that represents the new file. */
520 	error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
521 	    dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev,
522 	    cnp->cn_lwp->l_proc, &node);
523 	if (error != 0)
524 		goto out;
525 
526 	/* Allocate a directory entry that points to the new file. */
527 	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
528 	    &de);
529 	if (error != 0) {
530 		tmpfs_free_node(tmp, node);
531 		goto out;
532 	}
533 
534 	/* Allocate a vnode for the new file. */
535 	error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
536 	if (error != 0) {
537 		tmpfs_free_dirent(tmp, de, true);
538 		tmpfs_free_node(tmp, node);
539 		goto out;
540 	}
541 
542 	/* Now that all required items are allocated, we can proceed to
543 	 * insert the new node into the directory, an operation that
544 	 * cannot fail. */
545 	tmpfs_dir_attach(dvp, de);
546 
547 out:
548 	if (error != 0 || !(cnp->cn_flags & SAVESTART))
549 		PNBUF_PUT(cnp->cn_pnbuf);
550 	vput(dvp);
551 
552 	KASSERT(!VOP_ISLOCKED(dvp));
553 	KASSERT(IFF(error == 0, *vpp != NULL));
554 
555 	return error;
556 }
557 
558 /* --------------------------------------------------------------------- */
559 
560 /*
561  * Attaches the directory entry de to the directory represented by vp.
562  * Note that this does not change the link count of the node pointed by
563  * the directory entry, as this is done by tmpfs_alloc_dirent.
564  *
565  * As the "parent" directory changes, interested parties are notified of
566  * a write to it.
567  */
568 void
569 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
570 {
571 	struct tmpfs_node *dnode;
572 
573 	dnode = VP_TO_TMPFS_DIR(vp);
574 
575 	TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
576 	dnode->tn_size += sizeof(struct tmpfs_dirent);
577 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
578 	    TMPFS_NODE_MODIFIED;
579 	uvm_vnp_setsize(vp, dnode->tn_size);
580 
581 	VN_KNOTE(vp, NOTE_WRITE);
582 }
583 
584 /* --------------------------------------------------------------------- */
585 
586 /*
587  * Detaches the directory entry de from the directory represented by vp.
588  * Note that this does not change the link count of the node pointed by
589  * the directory entry, as this is done by tmpfs_free_dirent.
590  *
591  * As the "parent" directory changes, interested parties are notified of
592  * a write to it.
593  */
594 void
595 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
596 {
597 	struct tmpfs_node *dnode;
598 
599 	KASSERT(VOP_ISLOCKED(vp));
600 
601 	dnode = VP_TO_TMPFS_DIR(vp);
602 
603 	if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
604 		dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
605 		dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
606 	}
607 
608 	TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
609 	dnode->tn_size -= sizeof(struct tmpfs_dirent);
610 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
611 	    TMPFS_NODE_MODIFIED;
612 	uvm_vnp_setsize(vp, dnode->tn_size);
613 
614 	VN_KNOTE(vp, NOTE_WRITE);
615 }
616 
617 /* --------------------------------------------------------------------- */
618 
619 /*
620  * Looks for a directory entry in the directory represented by node.
621  * 'cnp' describes the name of the entry to look for.  Note that the .
622  * and .. components are not allowed as they do not physically exist
623  * within directories.
624  *
625  * Returns a pointer to the entry when found, otherwise NULL.
626  */
627 struct tmpfs_dirent *
628 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp)
629 {
630 	bool found;
631 	struct tmpfs_dirent *de;
632 
633 	KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
634 	KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
635 	    cnp->cn_nameptr[1] == '.')));
636 	TMPFS_VALIDATE_DIR(node);
637 
638 	node->tn_status |= TMPFS_NODE_ACCESSED;
639 
640 	found = 0;
641 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
642 		KASSERT(cnp->cn_namelen < 0xffff);
643 		if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
644 		    memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
645 			found = 1;
646 			break;
647 		}
648 	}
649 
650 	return found ? de : NULL;
651 }
652 
653 /* --------------------------------------------------------------------- */
654 
655 /*
656  * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
657  * directory and returns it in the uio space.  The function returns 0
658  * on success, -1 if there was not enough space in the uio structure to
659  * hold the directory entry or an appropriate error code if another
660  * error happens.
661  */
662 int
663 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
664 {
665 	int error;
666 	struct dirent *dentp;
667 
668 	TMPFS_VALIDATE_DIR(node);
669 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
670 
671 	dentp = malloc(sizeof(struct dirent), M_TMPFSTMP, M_WAITOK | M_ZERO);
672 
673 	dentp->d_fileno = node->tn_id;
674 	dentp->d_type = DT_DIR;
675 	dentp->d_namlen = 1;
676 	dentp->d_name[0] = '.';
677 	dentp->d_name[1] = '\0';
678 	dentp->d_reclen = _DIRENT_SIZE(dentp);
679 
680 	if (dentp->d_reclen > uio->uio_resid)
681 		error = -1;
682 	else {
683 		error = uiomove(dentp, dentp->d_reclen, uio);
684 		if (error == 0)
685 			uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
686 	}
687 
688 	node->tn_status |= TMPFS_NODE_ACCESSED;
689 
690 	free(dentp, M_TMPFSTMP);
691 	return error;
692 }
693 
694 /* --------------------------------------------------------------------- */
695 
696 /*
697  * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
698  * directory and returns it in the uio space.  The function returns 0
699  * on success, -1 if there was not enough space in the uio structure to
700  * hold the directory entry or an appropriate error code if another
701  * error happens.
702  */
703 int
704 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
705 {
706 	int error;
707 	struct dirent *dentp;
708 
709 	TMPFS_VALIDATE_DIR(node);
710 	KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
711 
712 	dentp = malloc(sizeof(struct dirent), M_TMPFSTMP, M_WAITOK | M_ZERO);
713 
714 	dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
715 	dentp->d_type = DT_DIR;
716 	dentp->d_namlen = 2;
717 	dentp->d_name[0] = '.';
718 	dentp->d_name[1] = '.';
719 	dentp->d_name[2] = '\0';
720 	dentp->d_reclen = _DIRENT_SIZE(dentp);
721 
722 	if (dentp->d_reclen > uio->uio_resid)
723 		error = -1;
724 	else {
725 		error = uiomove(dentp, dentp->d_reclen, uio);
726 		if (error == 0) {
727 			struct tmpfs_dirent *de;
728 
729 			de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
730 			if (de == NULL)
731 				uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
732 			else
733 				uio->uio_offset = tmpfs_dircookie(de);
734 		}
735 	}
736 
737 	node->tn_status |= TMPFS_NODE_ACCESSED;
738 
739 	free(dentp, M_TMPFSTMP);
740 	return error;
741 }
742 
743 /* --------------------------------------------------------------------- */
744 
745 /*
746  * Lookup a directory entry by its associated cookie.
747  */
748 struct tmpfs_dirent *
749 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
750 {
751 	struct tmpfs_dirent *de;
752 
753 	if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
754 	    node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
755 		return node->tn_spec.tn_dir.tn_readdir_lastp;
756 	}
757 
758 	TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
759 		if (tmpfs_dircookie(de) == cookie) {
760 			break;
761 		}
762 	}
763 
764 	return de;
765 }
766 
767 /* --------------------------------------------------------------------- */
768 
769 /*
770  * Helper function for tmpfs_readdir.  Returns as much directory entries
771  * as can fit in the uio space.  The read starts at uio->uio_offset.
772  * The function returns 0 on success, -1 if there was not enough space
773  * in the uio structure to hold the directory entry or an appropriate
774  * error code if another error happens.
775  */
776 int
777 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
778 {
779 	int error;
780 	off_t startcookie;
781 	struct dirent *dentp;
782 	struct tmpfs_dirent *de;
783 
784 	TMPFS_VALIDATE_DIR(node);
785 
786 	/* Locate the first directory entry we have to return.  We have cached
787 	 * the last readdir in the node, so use those values if appropriate.
788 	 * Otherwise do a linear scan to find the requested entry. */
789 	startcookie = uio->uio_offset;
790 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
791 	KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
792 	if (startcookie == TMPFS_DIRCOOKIE_EOF) {
793 		return 0;
794 	} else {
795 		de = tmpfs_dir_lookupbycookie(node, startcookie);
796 	}
797 	if (de == NULL) {
798 		return EINVAL;
799 	}
800 
801 	dentp = malloc(sizeof(struct dirent), M_TMPFSTMP, M_WAITOK | M_ZERO);
802 
803 	/* Read as much entries as possible; i.e., until we reach the end of
804 	 * the directory or we exhaust uio space. */
805 	do {
806 		/* Create a dirent structure representing the current
807 		 * tmpfs_node and fill it. */
808 		dentp->d_fileno = de->td_node->tn_id;
809 		switch (de->td_node->tn_type) {
810 		case VBLK:
811 			dentp->d_type = DT_BLK;
812 			break;
813 
814 		case VCHR:
815 			dentp->d_type = DT_CHR;
816 			break;
817 
818 		case VDIR:
819 			dentp->d_type = DT_DIR;
820 			break;
821 
822 		case VFIFO:
823 			dentp->d_type = DT_FIFO;
824 			break;
825 
826 		case VLNK:
827 			dentp->d_type = DT_LNK;
828 			break;
829 
830 		case VREG:
831 			dentp->d_type = DT_REG;
832 			break;
833 
834 		case VSOCK:
835 			dentp->d_type = DT_SOCK;
836 			break;
837 
838 		default:
839 			KASSERT(0);
840 		}
841 		dentp->d_namlen = de->td_namelen;
842 		KASSERT(de->td_namelen < sizeof(dentp->d_name));
843 		(void)memcpy(dentp->d_name, de->td_name, de->td_namelen);
844 		dentp->d_name[de->td_namelen] = '\0';
845 		dentp->d_reclen = _DIRENT_SIZE(dentp);
846 
847 		/* Stop reading if the directory entry we are treating is
848 		 * bigger than the amount of data that can be returned. */
849 		if (dentp->d_reclen > uio->uio_resid) {
850 			error = -1;
851 			break;
852 		}
853 
854 		/* Copy the new dirent structure into the output buffer and
855 		 * advance pointers. */
856 		error = uiomove(dentp, dentp->d_reclen, uio);
857 
858 		(*cntp)++;
859 		de = TAILQ_NEXT(de, td_entries);
860 	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
861 
862 	/* Update the offset and cache. */
863 	if (de == NULL) {
864 		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
865 		node->tn_spec.tn_dir.tn_readdir_lastn = 0;
866 		node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
867 	} else {
868 		node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
869 		    tmpfs_dircookie(de);
870 		node->tn_spec.tn_dir.tn_readdir_lastp = de;
871 	}
872 
873 	node->tn_status |= TMPFS_NODE_ACCESSED;
874 
875 	free(dentp, M_TMPFSTMP);
876 	return error;
877 }
878 
879 /* --------------------------------------------------------------------- */
880 
881 /*
882  * Resizes the aobj associated to the regular file pointed to by vp to
883  * the size newsize.  'vp' must point to a vnode that represents a regular
884  * file.  'newsize' must be positive.
885  *
886  * If the file is extended, the appropriate kevent is raised.  This does
887  * not rise a write event though because resizing is not the same as
888  * writing.
889  *
890  * Returns zero on success or an appropriate error code on failure.
891  */
892 int
893 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
894 {
895 	int error;
896 	size_t newpages, oldpages;
897 	struct tmpfs_mount *tmp;
898 	struct tmpfs_node *node;
899 	off_t oldsize;
900 
901 	KASSERT(vp->v_type == VREG);
902 	KASSERT(newsize >= 0);
903 
904 	node = VP_TO_TMPFS_NODE(vp);
905 	tmp = VFS_TO_TMPFS(vp->v_mount);
906 
907 	/* Convert the old and new sizes to the number of pages needed to
908 	 * store them.  It may happen that we do not need to do anything
909 	 * because the last allocated page can accommodate the change on
910 	 * its own. */
911 	oldsize = node->tn_size;
912 	oldpages = round_page(oldsize) / PAGE_SIZE;
913 	KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
914 	newpages = round_page(newsize) / PAGE_SIZE;
915 
916 	if (newpages > oldpages &&
917 	    newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) {
918 		error = ENOSPC;
919 		goto out;
920 	}
921 
922 	if (newsize < oldsize) {
923 		int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
924 
925 		/*
926 		 * free "backing store"
927 		 */
928 
929 		if (newpages < oldpages) {
930 			struct uvm_object *uobj;
931 
932 			uobj = node->tn_spec.tn_reg.tn_aobj;
933 
934 			simple_lock(&uobj->vmobjlock);
935 			uao_dropswap_range(uobj, newpages, oldpages);
936 			simple_unlock(&uobj->vmobjlock);
937 		}
938 
939 		/*
940 		 * zero out the truncated part of the last page.
941 		 */
942 
943 		uvm_vnp_zerorange(vp, newsize, zerolen);
944 	}
945 
946 	node->tn_spec.tn_reg.tn_aobj_pages = newpages;
947 	node->tn_size = newsize;
948 	uvm_vnp_setsize(vp, newsize);
949 
950 	tmp->tm_pages_used += (newpages - oldpages);
951 
952 	error = 0;
953 
954 	if (newsize > oldsize)
955 		VN_KNOTE(vp, NOTE_EXTEND);
956 
957 out:
958 	return error;
959 }
960 
961 /* --------------------------------------------------------------------- */
962 
963 /*
964  * Returns information about the number of available memory pages,
965  * including physical and virtual ones.
966  *
967  * If 'total' is true, the value returned is the total amount of memory
968  * pages configured for the system (either in use or free).
969  * If it is FALSE, the value returned is the amount of free memory pages.
970  *
971  * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
972  * excessive memory usage.
973  *
974  */
975 size_t
976 tmpfs_mem_info(bool total)
977 {
978 	size_t size;
979 
980 	size = 0;
981 	size += uvmexp.swpgavail;
982 	if (!total) {
983 		size -= uvmexp.swpgonly;
984 	}
985 	size += uvmexp.free;
986 	size += uvmexp.filepages;
987 	if (size > uvmexp.wired) {
988 		size -= uvmexp.wired;
989 	} else {
990 		size = 0;
991 	}
992 
993 	return size;
994 }
995 
996 /* --------------------------------------------------------------------- */
997 
998 /*
999  * Change flags of the given vnode.
1000  * Caller should execute tmpfs_update on vp after a successful execution.
1001  * The vnode must be locked on entry and remain locked on exit.
1002  */
1003 int
1004 tmpfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
1005 {
1006 	int error;
1007 	struct tmpfs_node *node;
1008 
1009 	KASSERT(VOP_ISLOCKED(vp));
1010 
1011 	node = VP_TO_TMPFS_NODE(vp);
1012 
1013 	/* Disallow this operation if the file system is mounted read-only. */
1014 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1015 		return EROFS;
1016 
1017 	/* XXX: The following comes from UFS code, and can be found in
1018 	 * several other file systems.  Shouldn't this be centralized
1019 	 * somewhere? */
1020 	if (kauth_cred_geteuid(cred) != node->tn_uid &&
1021 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1022 	    NULL)))
1023 		return error;
1024 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) {
1025 		/* The super-user is only allowed to change flags if the file
1026 		 * wasn't protected before and the securelevel is zero. */
1027 		if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) &&
1028 		    kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHSYSFLAGS,
1029 		     0, NULL, NULL, NULL))
1030 			return EPERM;
1031 		node->tn_flags = flags;
1032 	} else {
1033 		/* Regular users can change flags provided they only want to
1034 		 * change user-specific ones, not those reserved for the
1035 		 * super-user. */
1036 		if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) ||
1037 		    (flags & UF_SETTABLE) != flags)
1038 			return EPERM;
1039 		if ((node->tn_flags & SF_SETTABLE) != (flags & SF_SETTABLE))
1040 			return EPERM;
1041 		node->tn_flags &= SF_SETTABLE;
1042 		node->tn_flags |= (flags & UF_SETTABLE);
1043 	}
1044 
1045 	node->tn_status |= TMPFS_NODE_CHANGED;
1046 	VN_KNOTE(vp, NOTE_ATTRIB);
1047 
1048 	KASSERT(VOP_ISLOCKED(vp));
1049 
1050 	return 0;
1051 }
1052 
1053 /* --------------------------------------------------------------------- */
1054 
1055 /*
1056  * Change access mode on the given vnode.
1057  * Caller should execute tmpfs_update on vp after a successful execution.
1058  * The vnode must be locked on entry and remain locked on exit.
1059  */
1060 int
1061 tmpfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
1062 {
1063 	int error, ismember = 0;
1064 	struct tmpfs_node *node;
1065 
1066 	KASSERT(VOP_ISLOCKED(vp));
1067 
1068 	node = VP_TO_TMPFS_NODE(vp);
1069 
1070 	/* Disallow this operation if the file system is mounted read-only. */
1071 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1072 		return EROFS;
1073 
1074 	/* Immutable or append-only files cannot be modified, either. */
1075 	if (node->tn_flags & (IMMUTABLE | APPEND))
1076 		return EPERM;
1077 
1078 	/* XXX: The following comes from UFS code, and can be found in
1079 	 * several other file systems.  Shouldn't this be centralized
1080 	 * somewhere? */
1081 	if (kauth_cred_geteuid(cred) != node->tn_uid &&
1082 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1083 	    NULL)))
1084 		return error;
1085 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
1086 		if (vp->v_type != VDIR && (mode & S_ISTXT))
1087 			return EFTYPE;
1088 
1089 		if ((kauth_cred_ismember_gid(cred, node->tn_gid,
1090 		    &ismember) != 0 || !ismember) && (mode & S_ISGID))
1091 			return EPERM;
1092 	}
1093 
1094 	node->tn_mode = (mode & ALLPERMS);
1095 
1096 	node->tn_status |= TMPFS_NODE_CHANGED;
1097 	VN_KNOTE(vp, NOTE_ATTRIB);
1098 
1099 	KASSERT(VOP_ISLOCKED(vp));
1100 
1101 	return 0;
1102 }
1103 
1104 /* --------------------------------------------------------------------- */
1105 
1106 /*
1107  * Change ownership of the given vnode.  At least one of uid or gid must
1108  * be different than VNOVAL.  If one is set to that value, the attribute
1109  * is unchanged.
1110  * Caller should execute tmpfs_update on vp after a successful execution.
1111  * The vnode must be locked on entry and remain locked on exit.
1112  */
1113 int
1114 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
1115     struct lwp *l)
1116 {
1117 	int error, ismember = 0;
1118 	struct tmpfs_node *node;
1119 
1120 	KASSERT(VOP_ISLOCKED(vp));
1121 
1122 	node = VP_TO_TMPFS_NODE(vp);
1123 
1124 	/* Assign default values if they are unknown. */
1125 	KASSERT(uid != VNOVAL || gid != VNOVAL);
1126 	if (uid == VNOVAL)
1127 		uid = node->tn_uid;
1128 	if (gid == VNOVAL)
1129 		gid = node->tn_gid;
1130 	KASSERT(uid != VNOVAL && gid != VNOVAL);
1131 
1132 	/* Disallow this operation if the file system is mounted read-only. */
1133 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1134 		return EROFS;
1135 
1136 	/* Immutable or append-only files cannot be modified, either. */
1137 	if (node->tn_flags & (IMMUTABLE | APPEND))
1138 		return EPERM;
1139 
1140 	/* XXX: The following comes from UFS code, and can be found in
1141 	 * several other file systems.  Shouldn't this be centralized
1142 	 * somewhere? */
1143 	if ((kauth_cred_geteuid(cred) != node->tn_uid || uid != node->tn_uid ||
1144 	    (gid != node->tn_gid && !(kauth_cred_getegid(cred) == node->tn_gid ||
1145 	    (kauth_cred_ismember_gid(cred, gid, &ismember) == 0 && ismember)))) &&
1146 	    ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1147 	    NULL)) != 0))
1148 		return error;
1149 
1150 	node->tn_uid = uid;
1151 	node->tn_gid = gid;
1152 
1153 	node->tn_status |= TMPFS_NODE_CHANGED;
1154 	VN_KNOTE(vp, NOTE_ATTRIB);
1155 
1156 	KASSERT(VOP_ISLOCKED(vp));
1157 
1158 	return 0;
1159 }
1160 
1161 /* --------------------------------------------------------------------- */
1162 
1163 /*
1164  * Change size of the given vnode.
1165  * Caller should execute tmpfs_update on vp after a successful execution.
1166  * The vnode must be locked on entry and remain locked on exit.
1167  */
1168 int
1169 tmpfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred,
1170     struct lwp *l)
1171 {
1172 	int error;
1173 	struct tmpfs_node *node;
1174 
1175 	KASSERT(VOP_ISLOCKED(vp));
1176 
1177 	node = VP_TO_TMPFS_NODE(vp);
1178 
1179 	/* Decide whether this is a valid operation based on the file type. */
1180 	error = 0;
1181 	switch (vp->v_type) {
1182 	case VDIR:
1183 		return EISDIR;
1184 
1185 	case VREG:
1186 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1187 			return EROFS;
1188 		break;
1189 
1190 	case VBLK:
1191 		/* FALLTHROUGH */
1192 	case VCHR:
1193 		/* FALLTHROUGH */
1194 	case VFIFO:
1195 		/* Allow modifications of special files even if in the file
1196 		 * system is mounted read-only (we are not modifying the
1197 		 * files themselves, but the objects they represent). */
1198 		return 0;
1199 
1200 	default:
1201 		/* Anything else is unsupported. */
1202 		return EOPNOTSUPP;
1203 	}
1204 
1205 	/* Immutable or append-only files cannot be modified, either. */
1206 	if (node->tn_flags & (IMMUTABLE | APPEND))
1207 		return EPERM;
1208 
1209 	error = tmpfs_truncate(vp, size);
1210 	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1211 	 * for us, as will update tn_status; no need to do that here. */
1212 
1213 	KASSERT(VOP_ISLOCKED(vp));
1214 
1215 	return error;
1216 }
1217 
1218 /* --------------------------------------------------------------------- */
1219 
1220 /*
1221  * Change access and modification times of the given vnode.
1222  * Caller should execute tmpfs_update on vp after a successful execution.
1223  * The vnode must be locked on entry and remain locked on exit.
1224  */
1225 int
1226 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1227     int vaflags, kauth_cred_t cred, struct lwp *l)
1228 {
1229 	int error;
1230 	struct tmpfs_node *node;
1231 
1232 	KASSERT(VOP_ISLOCKED(vp));
1233 
1234 	node = VP_TO_TMPFS_NODE(vp);
1235 
1236 	/* Disallow this operation if the file system is mounted read-only. */
1237 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1238 		return EROFS;
1239 
1240 	/* Immutable or append-only files cannot be modified, either. */
1241 	if (node->tn_flags & (IMMUTABLE | APPEND))
1242 		return EPERM;
1243 
1244 	/* XXX: The following comes from UFS code, and can be found in
1245 	 * several other file systems.  Shouldn't this be centralized
1246 	 * somewhere? */
1247 	if (kauth_cred_geteuid(cred) != node->tn_uid &&
1248 	    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1249 	    NULL)) && ((vaflags & VA_UTIMES_NULL) == 0 ||
1250 	    (error = VOP_ACCESS(vp, VWRITE, cred))))
1251 		return error;
1252 
1253 	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1254 		node->tn_status |= TMPFS_NODE_ACCESSED;
1255 
1256 	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1257 		node->tn_status |= TMPFS_NODE_MODIFIED;
1258 
1259 	tmpfs_update(vp, atime, mtime, 0);
1260 	VN_KNOTE(vp, NOTE_ATTRIB);
1261 
1262 	KASSERT(VOP_ISLOCKED(vp));
1263 
1264 	return 0;
1265 }
1266 
1267 /* --------------------------------------------------------------------- */
1268 
1269 /* Sync timestamps */
1270 void
1271 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1272     const struct timespec *mod)
1273 {
1274 	struct timespec now;
1275 	struct tmpfs_node *node;
1276 
1277 	node = VP_TO_TMPFS_NODE(vp);
1278 
1279 	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1280 	    TMPFS_NODE_CHANGED)) == 0)
1281 		return;
1282 
1283 	getnanotime(&now);
1284 	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1285 		if (acc == NULL)
1286 			acc = &now;
1287 		node->tn_atime = *acc;
1288 	}
1289 	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1290 		if (mod == NULL)
1291 			mod = &now;
1292 		node->tn_mtime = *mod;
1293 	}
1294 	if (node->tn_status & TMPFS_NODE_CHANGED)
1295 		node->tn_ctime = now;
1296 
1297 	node->tn_status &=
1298 	    ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1299 }
1300 
1301 /* --------------------------------------------------------------------- */
1302 
1303 void
1304 tmpfs_update(struct vnode *vp, const struct timespec *acc,
1305     const struct timespec *mod, int flags)
1306 {
1307 
1308 	struct tmpfs_node *node;
1309 
1310 	KASSERT(VOP_ISLOCKED(vp));
1311 
1312 	node = VP_TO_TMPFS_NODE(vp);
1313 
1314 #if 0
1315 	if (flags & UPDATE_CLOSE)
1316 		; /* XXX Need to do anything special? */
1317 #endif
1318 
1319 	tmpfs_itimes(vp, acc, mod);
1320 
1321 	KASSERT(VOP_ISLOCKED(vp));
1322 }
1323 
1324 /* --------------------------------------------------------------------- */
1325 
1326 int
1327 tmpfs_truncate(struct vnode *vp, off_t length)
1328 {
1329 	bool extended;
1330 	int error;
1331 	struct tmpfs_node *node;
1332 
1333 	node = VP_TO_TMPFS_NODE(vp);
1334 	extended = length > node->tn_size;
1335 
1336 	if (length < 0) {
1337 		error = EINVAL;
1338 		goto out;
1339 	}
1340 
1341 	if (node->tn_size == length) {
1342 		error = 0;
1343 		goto out;
1344 	}
1345 
1346 	error = tmpfs_reg_resize(vp, length);
1347 	if (error == 0)
1348 		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1349 
1350 out:
1351 	tmpfs_update(vp, NULL, NULL, 0);
1352 
1353 	return error;
1354 }
1355