tmpfs_subr.c revision 232959
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: head/sys/fs/tmpfs/tmpfs_subr.c 232959 2012-03-14 08:29:21Z gleb $");
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 */
83int
84tmpfs_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 */
184void
185tmpfs_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 */
251int
252tmpfs_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 */
283void
284tmpfs_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 */
310int
311tmpfs_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
317loop:
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		error = vget(vp, lkflag | LK_INTERLOCK, curthread);
324		if (error != 0) {
325			vp = NULL;
326			goto out;
327		}
328
329		/*
330		 * Make sure the vnode is still there after
331		 * getting the interlock to avoid racing a free.
332		 */
333		if (node->tn_vnode == NULL || node->tn_vnode != vp) {
334			vput(vp);
335			goto loop;
336		}
337
338		goto out;
339	}
340
341	if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
342	    (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
343		TMPFS_NODE_UNLOCK(node);
344		error = ENOENT;
345		vp = NULL;
346		goto out;
347	}
348
349	/*
350	 * otherwise lock the vp list while we call getnewvnode
351	 * since that can block.
352	 */
353	if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
354		node->tn_vpstate |= TMPFS_VNODE_WANT;
355		error = msleep((caddr_t) &node->tn_vpstate,
356		    TMPFS_NODE_MTX(node), PDROP | PCATCH,
357		    "tmpfs_alloc_vp", 0);
358		if (error)
359			return error;
360
361		goto loop;
362	} else
363		node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
364
365	TMPFS_NODE_UNLOCK(node);
366
367	/* Get a new vnode and associate it with our node. */
368	error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
369	if (error != 0)
370		goto unlock;
371	MPASS(vp != NULL);
372
373	(void) vn_lock(vp, lkflag | LK_RETRY);
374
375	vp->v_data = node;
376	vp->v_type = node->tn_type;
377
378	/* Type-specific initialization. */
379	switch (node->tn_type) {
380	case VBLK:
381		/* FALLTHROUGH */
382	case VCHR:
383		/* FALLTHROUGH */
384	case VLNK:
385		/* FALLTHROUGH */
386	case VREG:
387		/* FALLTHROUGH */
388	case VSOCK:
389		break;
390	case VFIFO:
391		vp->v_op = &tmpfs_fifoop_entries;
392		break;
393	case VDIR:
394		MPASS(node->tn_dir.tn_parent != NULL);
395		if (node->tn_dir.tn_parent == node)
396			vp->v_vflag |= VV_ROOT;
397		break;
398
399	default:
400		panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
401	}
402
403	vnode_pager_setsize(vp, node->tn_size);
404	error = insmntque(vp, mp);
405	if (error)
406		vp = NULL;
407
408unlock:
409	TMPFS_NODE_LOCK(node);
410
411	MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
412	node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
413	node->tn_vnode = vp;
414
415	if (node->tn_vpstate & TMPFS_VNODE_WANT) {
416		node->tn_vpstate &= ~TMPFS_VNODE_WANT;
417		TMPFS_NODE_UNLOCK(node);
418		wakeup((caddr_t) &node->tn_vpstate);
419	} else
420		TMPFS_NODE_UNLOCK(node);
421
422out:
423	*vpp = vp;
424
425#ifdef INVARIANTS
426	if (error == 0) {
427		MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
428		TMPFS_NODE_LOCK(node);
429		MPASS(*vpp == node->tn_vnode);
430		TMPFS_NODE_UNLOCK(node);
431	}
432#endif
433
434	return error;
435}
436
437/* --------------------------------------------------------------------- */
438
439/*
440 * Destroys the association between the vnode vp and the node it
441 * references.
442 */
443void
444tmpfs_free_vp(struct vnode *vp)
445{
446	struct tmpfs_node *node;
447
448	node = VP_TO_TMPFS_NODE(vp);
449
450	mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
451	node->tn_vnode = NULL;
452	vp->v_data = NULL;
453}
454
455/* --------------------------------------------------------------------- */
456
457/*
458 * Allocates a new file of type 'type' and adds it to the parent directory
459 * 'dvp'; this addition is done using the component name given in 'cnp'.
460 * The ownership of the new file is automatically assigned based on the
461 * credentials of the caller (through 'cnp'), the group is set based on
462 * the parent directory and the mode is determined from the 'vap' argument.
463 * If successful, *vpp holds a vnode to the newly created file and zero
464 * is returned.  Otherwise *vpp is NULL and the function returns an
465 * appropriate error code.
466 */
467int
468tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
469    struct componentname *cnp, char *target)
470{
471	int error;
472	struct tmpfs_dirent *de;
473	struct tmpfs_mount *tmp;
474	struct tmpfs_node *dnode;
475	struct tmpfs_node *node;
476	struct tmpfs_node *parent;
477
478	MPASS(VOP_ISLOCKED(dvp));
479	MPASS(cnp->cn_flags & HASBUF);
480
481	tmp = VFS_TO_TMPFS(dvp->v_mount);
482	dnode = VP_TO_TMPFS_DIR(dvp);
483	*vpp = NULL;
484
485	/* If the entry we are creating is a directory, we cannot overflow
486	 * the number of links of its parent, because it will get a new
487	 * link. */
488	if (vap->va_type == VDIR) {
489		/* Ensure that we do not overflow the maximum number of links
490		 * imposed by the system. */
491		MPASS(dnode->tn_links <= LINK_MAX);
492		if (dnode->tn_links == LINK_MAX) {
493			error = EMLINK;
494			goto out;
495		}
496
497		parent = dnode;
498		MPASS(parent != NULL);
499	} else
500		parent = NULL;
501
502	/* Allocate a node that represents the new file. */
503	error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
504	    dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
505	if (error != 0)
506		goto out;
507
508	/* Allocate a directory entry that points to the new file. */
509	error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
510	    &de);
511	if (error != 0) {
512		tmpfs_free_node(tmp, node);
513		goto out;
514	}
515
516	/* Allocate a vnode for the new file. */
517	error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
518	if (error != 0) {
519		tmpfs_free_dirent(tmp, de, TRUE);
520		tmpfs_free_node(tmp, node);
521		goto out;
522	}
523
524	/* Now that all required items are allocated, we can proceed to
525	 * insert the new node into the directory, an operation that
526	 * cannot fail. */
527	if (cnp->cn_flags & ISWHITEOUT)
528		tmpfs_dir_whiteout_remove(dvp, cnp);
529	tmpfs_dir_attach(dvp, de);
530
531out:
532
533	return error;
534}
535
536/* --------------------------------------------------------------------- */
537
538/*
539 * Attaches the directory entry de to the directory represented by vp.
540 * Note that this does not change the link count of the node pointed by
541 * the directory entry, as this is done by tmpfs_alloc_dirent.
542 */
543void
544tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
545{
546	struct tmpfs_node *dnode;
547
548	ASSERT_VOP_ELOCKED(vp, __func__);
549	dnode = VP_TO_TMPFS_DIR(vp);
550	TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries);
551	dnode->tn_size += sizeof(struct tmpfs_dirent);
552	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
553	    TMPFS_NODE_MODIFIED;
554}
555
556/* --------------------------------------------------------------------- */
557
558/*
559 * Detaches the directory entry de from the directory represented by vp.
560 * Note that this does not change the link count of the node pointed by
561 * the directory entry, as this is done by tmpfs_free_dirent.
562 */
563void
564tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
565{
566	struct tmpfs_node *dnode;
567
568	ASSERT_VOP_ELOCKED(vp, __func__);
569	dnode = VP_TO_TMPFS_DIR(vp);
570
571	if (dnode->tn_dir.tn_readdir_lastp == de) {
572		dnode->tn_dir.tn_readdir_lastn = 0;
573		dnode->tn_dir.tn_readdir_lastp = NULL;
574	}
575
576	TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries);
577	dnode->tn_size -= sizeof(struct tmpfs_dirent);
578	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
579	    TMPFS_NODE_MODIFIED;
580}
581
582/* --------------------------------------------------------------------- */
583
584/*
585 * Looks for a directory entry in the directory represented by node.
586 * 'cnp' describes the name of the entry to look for.  Note that the .
587 * and .. components are not allowed as they do not physically exist
588 * within directories.
589 *
590 * Returns a pointer to the entry when found, otherwise NULL.
591 */
592struct tmpfs_dirent *
593tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
594    struct componentname *cnp)
595{
596	boolean_t found;
597	struct tmpfs_dirent *de;
598
599	MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
600	MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
601	    cnp->cn_nameptr[1] == '.')));
602	TMPFS_VALIDATE_DIR(node);
603
604	found = 0;
605	TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
606		if (f != NULL && de->td_node != f)
607		    continue;
608		MPASS(cnp->cn_namelen < 0xffff);
609		if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
610		    bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
611			found = 1;
612			break;
613		}
614	}
615	node->tn_status |= TMPFS_NODE_ACCESSED;
616
617	return found ? de : NULL;
618}
619
620/* --------------------------------------------------------------------- */
621
622/*
623 * Helper function for tmpfs_readdir.  Creates a '.' entry for the given
624 * directory and returns it in the uio space.  The function returns 0
625 * on success, -1 if there was not enough space in the uio structure to
626 * hold the directory entry or an appropriate error code if another
627 * error happens.
628 */
629int
630tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
631{
632	int error;
633	struct dirent dent;
634
635	TMPFS_VALIDATE_DIR(node);
636	MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
637
638	dent.d_fileno = node->tn_id;
639	dent.d_type = DT_DIR;
640	dent.d_namlen = 1;
641	dent.d_name[0] = '.';
642	dent.d_name[1] = '\0';
643	dent.d_reclen = GENERIC_DIRSIZ(&dent);
644
645	if (dent.d_reclen > uio->uio_resid)
646		error = -1;
647	else {
648		error = uiomove(&dent, dent.d_reclen, uio);
649		if (error == 0)
650			uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
651	}
652
653	node->tn_status |= TMPFS_NODE_ACCESSED;
654
655	return error;
656}
657
658/* --------------------------------------------------------------------- */
659
660/*
661 * Helper function for tmpfs_readdir.  Creates a '..' entry for the given
662 * directory and returns it in the uio space.  The function returns 0
663 * on success, -1 if there was not enough space in the uio structure to
664 * hold the directory entry or an appropriate error code if another
665 * error happens.
666 */
667int
668tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
669{
670	int error;
671	struct dirent dent;
672
673	TMPFS_VALIDATE_DIR(node);
674	MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
675
676	/*
677	 * Return ENOENT if the current node is already removed.
678	 */
679	TMPFS_ASSERT_LOCKED(node);
680	if (node->tn_dir.tn_parent == NULL) {
681		return (ENOENT);
682	}
683
684	TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
685	dent.d_fileno = node->tn_dir.tn_parent->tn_id;
686	TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
687
688	dent.d_type = DT_DIR;
689	dent.d_namlen = 2;
690	dent.d_name[0] = '.';
691	dent.d_name[1] = '.';
692	dent.d_name[2] = '\0';
693	dent.d_reclen = GENERIC_DIRSIZ(&dent);
694
695	if (dent.d_reclen > uio->uio_resid)
696		error = -1;
697	else {
698		error = uiomove(&dent, dent.d_reclen, uio);
699		if (error == 0) {
700			struct tmpfs_dirent *de;
701
702			de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
703			if (de == NULL)
704				uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
705			else
706				uio->uio_offset = tmpfs_dircookie(de);
707		}
708	}
709
710	node->tn_status |= TMPFS_NODE_ACCESSED;
711
712	return error;
713}
714
715/* --------------------------------------------------------------------- */
716
717/*
718 * Lookup a directory entry by its associated cookie.
719 */
720struct tmpfs_dirent *
721tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
722{
723	struct tmpfs_dirent *de;
724
725	if (cookie == node->tn_dir.tn_readdir_lastn &&
726	    node->tn_dir.tn_readdir_lastp != NULL) {
727		return node->tn_dir.tn_readdir_lastp;
728	}
729
730	TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
731		if (tmpfs_dircookie(de) == cookie) {
732			break;
733		}
734	}
735
736	return de;
737}
738
739/* --------------------------------------------------------------------- */
740
741/*
742 * Helper function for tmpfs_readdir.  Returns as much directory entries
743 * as can fit in the uio space.  The read starts at uio->uio_offset.
744 * The function returns 0 on success, -1 if there was not enough space
745 * in the uio structure to hold the directory entry or an appropriate
746 * error code if another error happens.
747 */
748int
749tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
750{
751	int error;
752	off_t startcookie;
753	struct tmpfs_dirent *de;
754
755	TMPFS_VALIDATE_DIR(node);
756
757	/* Locate the first directory entry we have to return.  We have cached
758	 * the last readdir in the node, so use those values if appropriate.
759	 * Otherwise do a linear scan to find the requested entry. */
760	startcookie = uio->uio_offset;
761	MPASS(startcookie != TMPFS_DIRCOOKIE_DOT);
762	MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
763	if (startcookie == TMPFS_DIRCOOKIE_EOF) {
764		return 0;
765	} else {
766		de = tmpfs_dir_lookupbycookie(node, startcookie);
767	}
768	if (de == NULL) {
769		return EINVAL;
770	}
771
772	/* Read as much entries as possible; i.e., until we reach the end of
773	 * the directory or we exhaust uio space. */
774	do {
775		struct dirent d;
776
777		/* Create a dirent structure representing the current
778		 * tmpfs_node and fill it. */
779		if (de->td_node == NULL) {
780			d.d_fileno = 1;
781			d.d_type = DT_WHT;
782		} else {
783			d.d_fileno = de->td_node->tn_id;
784			switch (de->td_node->tn_type) {
785			case VBLK:
786				d.d_type = DT_BLK;
787				break;
788
789			case VCHR:
790				d.d_type = DT_CHR;
791				break;
792
793			case VDIR:
794				d.d_type = DT_DIR;
795				break;
796
797			case VFIFO:
798				d.d_type = DT_FIFO;
799				break;
800
801			case VLNK:
802				d.d_type = DT_LNK;
803				break;
804
805			case VREG:
806				d.d_type = DT_REG;
807				break;
808
809			case VSOCK:
810				d.d_type = DT_SOCK;
811				break;
812
813			default:
814				panic("tmpfs_dir_getdents: type %p %d",
815				    de->td_node, (int)de->td_node->tn_type);
816			}
817		}
818		d.d_namlen = de->td_namelen;
819		MPASS(de->td_namelen < sizeof(d.d_name));
820		(void)memcpy(d.d_name, de->td_name, de->td_namelen);
821		d.d_name[de->td_namelen] = '\0';
822		d.d_reclen = GENERIC_DIRSIZ(&d);
823
824		/* Stop reading if the directory entry we are treating is
825		 * bigger than the amount of data that can be returned. */
826		if (d.d_reclen > uio->uio_resid) {
827			error = -1;
828			break;
829		}
830
831		/* Copy the new dirent structure into the output buffer and
832		 * advance pointers. */
833		error = uiomove(&d, d.d_reclen, uio);
834		if (error == 0) {
835			(*cntp)++;
836			de = TAILQ_NEXT(de, td_entries);
837		}
838	} while (error == 0 && uio->uio_resid > 0 && de != NULL);
839
840	/* Update the offset and cache. */
841	if (de == NULL) {
842		uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
843		node->tn_dir.tn_readdir_lastn = 0;
844		node->tn_dir.tn_readdir_lastp = NULL;
845	} else {
846		node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de);
847		node->tn_dir.tn_readdir_lastp = de;
848	}
849
850	node->tn_status |= TMPFS_NODE_ACCESSED;
851	return error;
852}
853
854int
855tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
856{
857	struct tmpfs_dirent *de;
858	int error;
859
860	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
861	    cnp->cn_nameptr, cnp->cn_namelen, &de);
862	if (error != 0)
863		return (error);
864	tmpfs_dir_attach(dvp, de);
865	return (0);
866}
867
868void
869tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
870{
871	struct tmpfs_dirent *de;
872
873	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
874	MPASS(de != NULL && de->td_node == NULL);
875	tmpfs_dir_detach(dvp, de);
876	tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de, TRUE);
877}
878
879/* --------------------------------------------------------------------- */
880
881/*
882 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
883 * size 'newsize'.  'vp' must point to a vnode that represents a regular file.
884 * 'newsize' must be positive.
885 *
886 * Returns zero on success or an appropriate error code on failure.
887 */
888int
889tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
890{
891	struct tmpfs_mount *tmp;
892	struct tmpfs_node *node;
893	vm_object_t uobj;
894	vm_page_t m, ma[1];
895	vm_pindex_t idx, newpages, oldpages;
896	off_t oldsize;
897	int base, rv;
898
899	MPASS(vp->v_type == VREG);
900	MPASS(newsize >= 0);
901
902	node = VP_TO_TMPFS_NODE(vp);
903	uobj = node->tn_reg.tn_aobj;
904	tmp = VFS_TO_TMPFS(vp->v_mount);
905
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	 */
912	oldsize = node->tn_size;
913	oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
914	MPASS(oldpages == uobj->size);
915	newpages = OFF_TO_IDX(newsize + PAGE_MASK);
916	if (newpages > oldpages &&
917	    newpages - oldpages > TMPFS_PAGES_AVAIL(tmp))
918		return (ENOSPC);
919
920	VM_OBJECT_LOCK(uobj);
921	if (newsize < oldsize) {
922		/*
923		 * Zero the truncated part of the last page.
924		 */
925		base = newsize & PAGE_MASK;
926		if (base != 0) {
927			idx = OFF_TO_IDX(newsize);
928retry:
929			m = vm_page_lookup(uobj, idx);
930			if (m != NULL) {
931				if ((m->oflags & VPO_BUSY) != 0 ||
932				    m->busy != 0) {
933					vm_page_sleep(m, "tmfssz");
934					goto retry;
935				}
936				MPASS(m->valid == VM_PAGE_BITS_ALL);
937			} else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
938				m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
939				if (m == NULL) {
940					VM_OBJECT_UNLOCK(uobj);
941					VM_WAIT;
942					VM_OBJECT_LOCK(uobj);
943					goto retry;
944				} else if (m->valid != VM_PAGE_BITS_ALL) {
945					ma[0] = m;
946					rv = vm_pager_get_pages(uobj, ma, 1, 0);
947					m = vm_page_lookup(uobj, idx);
948				} else
949					/* A cached page was reactivated. */
950					rv = VM_PAGER_OK;
951				vm_page_lock(m);
952				if (rv == VM_PAGER_OK) {
953					vm_page_deactivate(m);
954					vm_page_unlock(m);
955					vm_page_wakeup(m);
956				} else {
957					vm_page_free(m);
958					vm_page_unlock(m);
959					if (ignerr)
960						m = NULL;
961					else {
962						VM_OBJECT_UNLOCK(uobj);
963						return (EIO);
964					}
965				}
966			}
967			if (m != NULL) {
968				pmap_zero_page_area(m, base, PAGE_SIZE - base);
969				vm_page_dirty(m);
970				vm_pager_page_unswapped(m);
971			}
972		}
973
974		/*
975		 * Release any swap space and free any whole pages.
976		 */
977		if (newpages < oldpages) {
978			swap_pager_freespace(uobj, newpages, oldpages -
979			    newpages);
980			vm_object_page_remove(uobj, newpages, 0, 0);
981		}
982	}
983	uobj->size = newpages;
984	VM_OBJECT_UNLOCK(uobj);
985
986	TMPFS_LOCK(tmp);
987	tmp->tm_pages_used += (newpages - oldpages);
988	TMPFS_UNLOCK(tmp);
989
990	node->tn_size = newsize;
991	vnode_pager_setsize(vp, newsize);
992	return (0);
993}
994
995/* --------------------------------------------------------------------- */
996
997/*
998 * Change flags of the given vnode.
999 * Caller should execute tmpfs_update on vp after a successful execution.
1000 * The vnode must be locked on entry and remain locked on exit.
1001 */
1002int
1003tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p)
1004{
1005	int error;
1006	struct tmpfs_node *node;
1007
1008	MPASS(VOP_ISLOCKED(vp));
1009
1010	node = VP_TO_TMPFS_NODE(vp);
1011
1012	/* Disallow this operation if the file system is mounted read-only. */
1013	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1014		return EROFS;
1015
1016	/*
1017	 * Callers may only modify the file flags on objects they
1018	 * have VADMIN rights for.
1019	 */
1020	if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1021		return (error);
1022	/*
1023	 * Unprivileged processes are not permitted to unset system
1024	 * flags, or modify flags if any system flags are set.
1025	 */
1026	if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1027		if (node->tn_flags
1028		  & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1029			error = securelevel_gt(cred, 0);
1030			if (error)
1031				return (error);
1032		}
1033		/* Snapshot flag cannot be set or cleared */
1034		if (((flags & SF_SNAPSHOT) != 0 &&
1035		  (node->tn_flags & SF_SNAPSHOT) == 0) ||
1036		  ((flags & SF_SNAPSHOT) == 0 &&
1037		  (node->tn_flags & SF_SNAPSHOT) != 0))
1038			return (EPERM);
1039		node->tn_flags = flags;
1040	} else {
1041		if (node->tn_flags
1042		  & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1043		  (flags & UF_SETTABLE) != flags)
1044			return (EPERM);
1045		node->tn_flags &= SF_SETTABLE;
1046		node->tn_flags |= (flags & UF_SETTABLE);
1047	}
1048	node->tn_status |= TMPFS_NODE_CHANGED;
1049
1050	MPASS(VOP_ISLOCKED(vp));
1051
1052	return 0;
1053}
1054
1055/* --------------------------------------------------------------------- */
1056
1057/*
1058 * Change access mode on the given vnode.
1059 * Caller should execute tmpfs_update on vp after a successful execution.
1060 * The vnode must be locked on entry and remain locked on exit.
1061 */
1062int
1063tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1064{
1065	int error;
1066	struct tmpfs_node *node;
1067
1068	MPASS(VOP_ISLOCKED(vp));
1069
1070	node = VP_TO_TMPFS_NODE(vp);
1071
1072	/* Disallow this operation if the file system is mounted read-only. */
1073	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1074		return EROFS;
1075
1076	/* Immutable or append-only files cannot be modified, either. */
1077	if (node->tn_flags & (IMMUTABLE | APPEND))
1078		return EPERM;
1079
1080	/*
1081	 * To modify the permissions on a file, must possess VADMIN
1082	 * for that file.
1083	 */
1084	if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1085		return (error);
1086
1087	/*
1088	 * Privileged processes may set the sticky bit on non-directories,
1089	 * as well as set the setgid bit on a file with a group that the
1090	 * process is not a member of.
1091	 */
1092	if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1093		if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1094			return (EFTYPE);
1095	}
1096	if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1097		error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1098		if (error)
1099			return (error);
1100	}
1101
1102
1103	node->tn_mode &= ~ALLPERMS;
1104	node->tn_mode |= mode & ALLPERMS;
1105
1106	node->tn_status |= TMPFS_NODE_CHANGED;
1107
1108	MPASS(VOP_ISLOCKED(vp));
1109
1110	return 0;
1111}
1112
1113/* --------------------------------------------------------------------- */
1114
1115/*
1116 * Change ownership of the given vnode.  At least one of uid or gid must
1117 * be different than VNOVAL.  If one is set to that value, the attribute
1118 * is unchanged.
1119 * Caller should execute tmpfs_update on vp after a successful execution.
1120 * The vnode must be locked on entry and remain locked on exit.
1121 */
1122int
1123tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1124    struct thread *p)
1125{
1126	int error;
1127	struct tmpfs_node *node;
1128	uid_t ouid;
1129	gid_t ogid;
1130
1131	MPASS(VOP_ISLOCKED(vp));
1132
1133	node = VP_TO_TMPFS_NODE(vp);
1134
1135	/* Assign default values if they are unknown. */
1136	MPASS(uid != VNOVAL || gid != VNOVAL);
1137	if (uid == VNOVAL)
1138		uid = node->tn_uid;
1139	if (gid == VNOVAL)
1140		gid = node->tn_gid;
1141	MPASS(uid != VNOVAL && gid != VNOVAL);
1142
1143	/* Disallow this operation if the file system is mounted read-only. */
1144	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1145		return EROFS;
1146
1147	/* Immutable or append-only files cannot be modified, either. */
1148	if (node->tn_flags & (IMMUTABLE | APPEND))
1149		return EPERM;
1150
1151	/*
1152	 * To modify the ownership of a file, must possess VADMIN for that
1153	 * file.
1154	 */
1155	if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1156		return (error);
1157
1158	/*
1159	 * To change the owner of a file, or change the group of a file to a
1160	 * group of which we are not a member, the caller must have
1161	 * privilege.
1162	 */
1163	if ((uid != node->tn_uid ||
1164	    (gid != node->tn_gid && !groupmember(gid, cred))) &&
1165	    (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1166		return (error);
1167
1168	ogid = node->tn_gid;
1169	ouid = node->tn_uid;
1170
1171	node->tn_uid = uid;
1172	node->tn_gid = gid;
1173
1174	node->tn_status |= TMPFS_NODE_CHANGED;
1175
1176	if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1177		if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1178			node->tn_mode &= ~(S_ISUID | S_ISGID);
1179	}
1180
1181	MPASS(VOP_ISLOCKED(vp));
1182
1183	return 0;
1184}
1185
1186/* --------------------------------------------------------------------- */
1187
1188/*
1189 * Change size of the given vnode.
1190 * Caller should execute tmpfs_update on vp after a successful execution.
1191 * The vnode must be locked on entry and remain locked on exit.
1192 */
1193int
1194tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1195    struct thread *p)
1196{
1197	int error;
1198	struct tmpfs_node *node;
1199
1200	MPASS(VOP_ISLOCKED(vp));
1201
1202	node = VP_TO_TMPFS_NODE(vp);
1203
1204	/* Decide whether this is a valid operation based on the file type. */
1205	error = 0;
1206	switch (vp->v_type) {
1207	case VDIR:
1208		return EISDIR;
1209
1210	case VREG:
1211		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1212			return EROFS;
1213		break;
1214
1215	case VBLK:
1216		/* FALLTHROUGH */
1217	case VCHR:
1218		/* FALLTHROUGH */
1219	case VFIFO:
1220		/* Allow modifications of special files even if in the file
1221		 * system is mounted read-only (we are not modifying the
1222		 * files themselves, but the objects they represent). */
1223		return 0;
1224
1225	default:
1226		/* Anything else is unsupported. */
1227		return EOPNOTSUPP;
1228	}
1229
1230	/* Immutable or append-only files cannot be modified, either. */
1231	if (node->tn_flags & (IMMUTABLE | APPEND))
1232		return EPERM;
1233
1234	error = tmpfs_truncate(vp, size);
1235	/* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1236	 * for us, as will update tn_status; no need to do that here. */
1237
1238	MPASS(VOP_ISLOCKED(vp));
1239
1240	return error;
1241}
1242
1243/* --------------------------------------------------------------------- */
1244
1245/*
1246 * Change access and modification times of the given vnode.
1247 * Caller should execute tmpfs_update on vp after a successful execution.
1248 * The vnode must be locked on entry and remain locked on exit.
1249 */
1250int
1251tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1252	struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l)
1253{
1254	int error;
1255	struct tmpfs_node *node;
1256
1257	MPASS(VOP_ISLOCKED(vp));
1258
1259	node = VP_TO_TMPFS_NODE(vp);
1260
1261	/* Disallow this operation if the file system is mounted read-only. */
1262	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1263		return EROFS;
1264
1265	/* Immutable or append-only files cannot be modified, either. */
1266	if (node->tn_flags & (IMMUTABLE | APPEND))
1267		return EPERM;
1268
1269	/* Determine if the user have proper privilege to update time. */
1270	if (vaflags & VA_UTIMES_NULL) {
1271		error = VOP_ACCESS(vp, VADMIN, cred, l);
1272		if (error)
1273			error = VOP_ACCESS(vp, VWRITE, cred, l);
1274	} else
1275		error = VOP_ACCESS(vp, VADMIN, cred, l);
1276	if (error)
1277		return (error);
1278
1279	if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1280		node->tn_status |= TMPFS_NODE_ACCESSED;
1281
1282	if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1283		node->tn_status |= TMPFS_NODE_MODIFIED;
1284
1285	if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1286		node->tn_status |= TMPFS_NODE_MODIFIED;
1287
1288	tmpfs_itimes(vp, atime, mtime);
1289
1290	if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1291		node->tn_birthtime = *birthtime;
1292	MPASS(VOP_ISLOCKED(vp));
1293
1294	return 0;
1295}
1296
1297/* --------------------------------------------------------------------- */
1298/* Sync timestamps */
1299void
1300tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1301    const struct timespec *mod)
1302{
1303	struct tmpfs_node *node;
1304	struct timespec now;
1305
1306	node = VP_TO_TMPFS_NODE(vp);
1307
1308	if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1309	    TMPFS_NODE_CHANGED)) == 0)
1310		return;
1311
1312	vfs_timestamp(&now);
1313	if (node->tn_status & TMPFS_NODE_ACCESSED) {
1314		if (acc == NULL)
1315			 acc = &now;
1316		node->tn_atime = *acc;
1317	}
1318	if (node->tn_status & TMPFS_NODE_MODIFIED) {
1319		if (mod == NULL)
1320			mod = &now;
1321		node->tn_mtime = *mod;
1322	}
1323	if (node->tn_status & TMPFS_NODE_CHANGED) {
1324		node->tn_ctime = now;
1325	}
1326	node->tn_status &=
1327	    ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1328}
1329
1330/* --------------------------------------------------------------------- */
1331
1332void
1333tmpfs_update(struct vnode *vp)
1334{
1335
1336	tmpfs_itimes(vp, NULL, NULL);
1337}
1338
1339/* --------------------------------------------------------------------- */
1340
1341int
1342tmpfs_truncate(struct vnode *vp, off_t length)
1343{
1344	int error;
1345	struct tmpfs_node *node;
1346
1347	node = VP_TO_TMPFS_NODE(vp);
1348
1349	if (length < 0) {
1350		error = EINVAL;
1351		goto out;
1352	}
1353
1354	if (node->tn_size == length) {
1355		error = 0;
1356		goto out;
1357	}
1358
1359	if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1360		return (EFBIG);
1361
1362	error = tmpfs_reg_resize(vp, length, FALSE);
1363	if (error == 0) {
1364		node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1365	}
1366
1367out:
1368	tmpfs_update(vp);
1369
1370	return error;
1371}
1372