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