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