xref: /freebsd/sys/fs/tmpfs/tmpfs_vnops.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11  * 2005 program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * tmpfs vnode interface.
37  */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/dirent.h>
43 #include <sys/fcntl.h>
44 #include <sys/lockf.h>
45 #include <sys/lock.h>
46 #include <sys/mount.h>
47 #include <sys/namei.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/rwlock.h>
51 #include <sys/sched.h>
52 #include <sys/stat.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
57 
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pager.h>
63 
64 #include <fs/tmpfs/tmpfs_vnops.h>
65 #include <fs/tmpfs/tmpfs.h>
66 
67 SYSCTL_DECL(_vfs_tmpfs);
68 
69 static volatile int tmpfs_rename_restarts;
70 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
71     __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
72     "Times rename had to restart due to lock contention");
73 
74 static int
75 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
76     struct vnode **rvp)
77 {
78 
79 	return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
80 }
81 
82 static int
83 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
84 {
85 	struct tmpfs_dirent *de;
86 	struct tmpfs_node *dnode, *pnode;
87 	struct tmpfs_mount *tm;
88 	int error;
89 
90 	dnode = VP_TO_TMPFS_DIR(dvp);
91 	*vpp = NULLVP;
92 
93 	/* Check accessibility of requested node as a first step. */
94 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
95 	if (error != 0)
96 		goto out;
97 
98 	/* We cannot be requesting the parent directory of the root node. */
99 	MPASS(IMPLIES(dnode->tn_type == VDIR &&
100 	    dnode->tn_dir.tn_parent == dnode,
101 	    !(cnp->cn_flags & ISDOTDOT)));
102 
103 	TMPFS_ASSERT_LOCKED(dnode);
104 	if (dnode->tn_dir.tn_parent == NULL) {
105 		error = ENOENT;
106 		goto out;
107 	}
108 	if (cnp->cn_flags & ISDOTDOT) {
109 		tm = VFS_TO_TMPFS(dvp->v_mount);
110 		pnode = dnode->tn_dir.tn_parent;
111 		tmpfs_ref_node(pnode);
112 		error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
113 		    pnode, cnp->cn_lkflags, vpp);
114 		tmpfs_free_node(tm, pnode);
115 		if (error != 0)
116 			goto out;
117 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
118 		VREF(dvp);
119 		*vpp = dvp;
120 		error = 0;
121 	} else {
122 		de = tmpfs_dir_lookup(dnode, NULL, cnp);
123 		if (de != NULL && de->td_node == NULL)
124 			cnp->cn_flags |= ISWHITEOUT;
125 		if (de == NULL || de->td_node == NULL) {
126 			/*
127 			 * The entry was not found in the directory.
128 			 * This is OK if we are creating or renaming an
129 			 * entry and are working on the last component of
130 			 * the path name.
131 			 */
132 			if ((cnp->cn_flags & ISLASTCN) &&
133 			    (cnp->cn_nameiop == CREATE || \
134 			    cnp->cn_nameiop == RENAME ||
135 			    (cnp->cn_nameiop == DELETE &&
136 			    cnp->cn_flags & DOWHITEOUT &&
137 			    cnp->cn_flags & ISWHITEOUT))) {
138 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
139 				    cnp->cn_thread);
140 				if (error != 0)
141 					goto out;
142 
143 				/*
144 				 * Keep the component name in the buffer for
145 				 * future uses.
146 				 */
147 				cnp->cn_flags |= SAVENAME;
148 
149 				error = EJUSTRETURN;
150 			} else
151 				error = ENOENT;
152 		} else {
153 			struct tmpfs_node *tnode;
154 
155 			/*
156 			 * The entry was found, so get its associated
157 			 * tmpfs_node.
158 			 */
159 			tnode = de->td_node;
160 
161 			/*
162 			 * If we are not at the last path component and
163 			 * found a non-directory or non-link entry (which
164 			 * may itself be pointing to a directory), raise
165 			 * an error.
166 			 */
167 			if ((tnode->tn_type != VDIR &&
168 			    tnode->tn_type != VLNK) &&
169 			    !(cnp->cn_flags & ISLASTCN)) {
170 				error = ENOTDIR;
171 				goto out;
172 			}
173 
174 			/*
175 			 * If we are deleting or renaming the entry, keep
176 			 * track of its tmpfs_dirent so that it can be
177 			 * easily deleted later.
178 			 */
179 			if ((cnp->cn_flags & ISLASTCN) &&
180 			    (cnp->cn_nameiop == DELETE ||
181 			    cnp->cn_nameiop == RENAME)) {
182 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
183 				    cnp->cn_thread);
184 				if (error != 0)
185 					goto out;
186 
187 				/* Allocate a new vnode on the matching entry. */
188 				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
189 				    cnp->cn_lkflags, vpp);
190 				if (error != 0)
191 					goto out;
192 
193 				if ((dnode->tn_mode & S_ISTXT) &&
194 				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
195 				  cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
196 				  cnp->cn_cred, cnp->cn_thread)) {
197 					error = EPERM;
198 					vput(*vpp);
199 					*vpp = NULL;
200 					goto out;
201 				}
202 				cnp->cn_flags |= SAVENAME;
203 			} else {
204 				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
205 				    cnp->cn_lkflags, vpp);
206 				if (error != 0)
207 					goto out;
208 			}
209 		}
210 	}
211 
212 	/*
213 	 * Store the result of this lookup in the cache.  Avoid this if the
214 	 * request was for creation, as it does not improve timings on
215 	 * emprical tests.
216 	 */
217 	if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
218 		cache_enter(dvp, *vpp, cnp);
219 
220 out:
221 	/*
222 	 * If there were no errors, *vpp cannot be null and it must be
223 	 * locked.
224 	 */
225 	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
226 
227 	return (error);
228 }
229 
230 static int
231 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
232 {
233 
234 	return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
235 }
236 
237 static int
238 tmpfs_lookup(struct vop_lookup_args *v)
239 {
240 
241 	return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
242 }
243 
244 static int
245 tmpfs_create(struct vop_create_args *v)
246 {
247 	struct vnode *dvp = v->a_dvp;
248 	struct vnode **vpp = v->a_vpp;
249 	struct componentname *cnp = v->a_cnp;
250 	struct vattr *vap = v->a_vap;
251 	int error;
252 
253 	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
254 
255 	error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
256 	if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
257 		cache_enter(dvp, *vpp, cnp);
258 	return (error);
259 }
260 
261 static int
262 tmpfs_mknod(struct vop_mknod_args *v)
263 {
264 	struct vnode *dvp = v->a_dvp;
265 	struct vnode **vpp = v->a_vpp;
266 	struct componentname *cnp = v->a_cnp;
267 	struct vattr *vap = v->a_vap;
268 
269 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
270 	    vap->va_type != VFIFO)
271 		return EINVAL;
272 
273 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
274 }
275 
276 static int
277 tmpfs_open(struct vop_open_args *v)
278 {
279 	struct vnode *vp = v->a_vp;
280 	int mode = v->a_mode;
281 
282 	int error;
283 	struct tmpfs_node *node;
284 
285 	MPASS(VOP_ISLOCKED(vp));
286 
287 	node = VP_TO_TMPFS_NODE(vp);
288 
289 	/* The file is still active but all its names have been removed
290 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
291 	 * it is about to die. */
292 	if (node->tn_links < 1)
293 		return (ENOENT);
294 
295 	/* If the file is marked append-only, deny write requests. */
296 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
297 		error = EPERM;
298 	else {
299 		error = 0;
300 		/* For regular files, the call below is nop. */
301 		KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
302 		    OBJ_DEAD) == 0, ("dead object"));
303 		vnode_create_vobject(vp, node->tn_size, v->a_td);
304 	}
305 
306 	MPASS(VOP_ISLOCKED(vp));
307 	return error;
308 }
309 
310 static int
311 tmpfs_close(struct vop_close_args *v)
312 {
313 	struct vnode *vp = v->a_vp;
314 
315 	/* Update node times. */
316 	tmpfs_update(vp);
317 
318 	return (0);
319 }
320 
321 int
322 tmpfs_access(struct vop_access_args *v)
323 {
324 	struct vnode *vp = v->a_vp;
325 	accmode_t accmode = v->a_accmode;
326 	struct ucred *cred = v->a_cred;
327 
328 	int error;
329 	struct tmpfs_node *node;
330 
331 	MPASS(VOP_ISLOCKED(vp));
332 
333 	node = VP_TO_TMPFS_NODE(vp);
334 
335 	switch (vp->v_type) {
336 	case VDIR:
337 		/* FALLTHROUGH */
338 	case VLNK:
339 		/* FALLTHROUGH */
340 	case VREG:
341 		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
342 			error = EROFS;
343 			goto out;
344 		}
345 		break;
346 
347 	case VBLK:
348 		/* FALLTHROUGH */
349 	case VCHR:
350 		/* FALLTHROUGH */
351 	case VSOCK:
352 		/* FALLTHROUGH */
353 	case VFIFO:
354 		break;
355 
356 	default:
357 		error = EINVAL;
358 		goto out;
359 	}
360 
361 	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
362 		error = EPERM;
363 		goto out;
364 	}
365 
366 	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
367 	    node->tn_gid, accmode, cred, NULL);
368 
369 out:
370 	MPASS(VOP_ISLOCKED(vp));
371 
372 	return error;
373 }
374 
375 int
376 tmpfs_getattr(struct vop_getattr_args *v)
377 {
378 	struct vnode *vp = v->a_vp;
379 	struct vattr *vap = v->a_vap;
380 	vm_object_t obj;
381 	struct tmpfs_node *node;
382 
383 	node = VP_TO_TMPFS_NODE(vp);
384 
385 	tmpfs_update(vp);
386 
387 	vap->va_type = vp->v_type;
388 	vap->va_mode = node->tn_mode;
389 	vap->va_nlink = node->tn_links;
390 	vap->va_uid = node->tn_uid;
391 	vap->va_gid = node->tn_gid;
392 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
393 	vap->va_fileid = node->tn_id;
394 	vap->va_size = node->tn_size;
395 	vap->va_blocksize = PAGE_SIZE;
396 	vap->va_atime = node->tn_atime;
397 	vap->va_mtime = node->tn_mtime;
398 	vap->va_ctime = node->tn_ctime;
399 	vap->va_birthtime = node->tn_birthtime;
400 	vap->va_gen = node->tn_gen;
401 	vap->va_flags = node->tn_flags;
402 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
403 		node->tn_rdev : NODEV;
404 	if (vp->v_type == VREG) {
405 		obj = node->tn_reg.tn_aobj;
406 		vap->va_bytes = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
407 	} else
408 		vap->va_bytes = node->tn_size;
409 	vap->va_filerev = 0;
410 
411 	return 0;
412 }
413 
414 int
415 tmpfs_setattr(struct vop_setattr_args *v)
416 {
417 	struct vnode *vp = v->a_vp;
418 	struct vattr *vap = v->a_vap;
419 	struct ucred *cred = v->a_cred;
420 	struct thread *td = curthread;
421 
422 	int error;
423 
424 	MPASS(VOP_ISLOCKED(vp));
425 
426 	error = 0;
427 
428 	/* Abort if any unsettable attribute is given. */
429 	if (vap->va_type != VNON ||
430 	    vap->va_nlink != VNOVAL ||
431 	    vap->va_fsid != VNOVAL ||
432 	    vap->va_fileid != VNOVAL ||
433 	    vap->va_blocksize != VNOVAL ||
434 	    vap->va_gen != VNOVAL ||
435 	    vap->va_rdev != VNOVAL ||
436 	    vap->va_bytes != VNOVAL)
437 		error = EINVAL;
438 
439 	if (error == 0 && (vap->va_flags != VNOVAL))
440 		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
441 
442 	if (error == 0 && (vap->va_size != VNOVAL))
443 		error = tmpfs_chsize(vp, vap->va_size, cred, td);
444 
445 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
446 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
447 
448 	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
449 		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
450 
451 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
452 	    vap->va_atime.tv_nsec != VNOVAL) ||
453 	    (vap->va_mtime.tv_sec != VNOVAL &&
454 	    vap->va_mtime.tv_nsec != VNOVAL) ||
455 	    (vap->va_birthtime.tv_sec != VNOVAL &&
456 	    vap->va_birthtime.tv_nsec != VNOVAL)))
457 		error = tmpfs_chtimes(vp, vap, cred, td);
458 
459 	/* Update the node times.  We give preference to the error codes
460 	 * generated by this function rather than the ones that may arise
461 	 * from tmpfs_update. */
462 	tmpfs_update(vp);
463 
464 	MPASS(VOP_ISLOCKED(vp));
465 
466 	return error;
467 }
468 
469 static int
470 tmpfs_read(struct vop_read_args *v)
471 {
472 	struct vnode *vp;
473 	struct uio *uio;
474 	struct tmpfs_node *node;
475 
476 	vp = v->a_vp;
477 	if (vp->v_type != VREG)
478 		return (EISDIR);
479 	uio = v->a_uio;
480 	if (uio->uio_offset < 0)
481 		return (EINVAL);
482 	node = VP_TO_TMPFS_NODE(vp);
483 	tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
484 	return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
485 }
486 
487 static int
488 tmpfs_write(struct vop_write_args *v)
489 {
490 	struct vnode *vp;
491 	struct uio *uio;
492 	struct tmpfs_node *node;
493 	off_t oldsize;
494 	int error, ioflag;
495 
496 	vp = v->a_vp;
497 	uio = v->a_uio;
498 	ioflag = v->a_ioflag;
499 	error = 0;
500 	node = VP_TO_TMPFS_NODE(vp);
501 	oldsize = node->tn_size;
502 
503 	if (uio->uio_offset < 0 || vp->v_type != VREG)
504 		return (EINVAL);
505 	if (uio->uio_resid == 0)
506 		return (0);
507 	if (ioflag & IO_APPEND)
508 		uio->uio_offset = node->tn_size;
509 	if (uio->uio_offset + uio->uio_resid >
510 	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
511 		return (EFBIG);
512 	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
513 		return (EFBIG);
514 	if (uio->uio_offset + uio->uio_resid > node->tn_size) {
515 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
516 		    FALSE);
517 		if (error != 0)
518 			goto out;
519 	}
520 
521 	error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
522 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
523 	    TMPFS_NODE_CHANGED;
524 	if (node->tn_mode & (S_ISUID | S_ISGID)) {
525 		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
526 			node->tn_mode &= ~(S_ISUID | S_ISGID);
527 	}
528 	if (error != 0)
529 		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
530 
531 out:
532 	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
533 	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
534 
535 	return (error);
536 }
537 
538 static int
539 tmpfs_fsync(struct vop_fsync_args *v)
540 {
541 	struct vnode *vp = v->a_vp;
542 
543 	MPASS(VOP_ISLOCKED(vp));
544 
545 	tmpfs_check_mtime(vp);
546 	tmpfs_update(vp);
547 
548 	return 0;
549 }
550 
551 static int
552 tmpfs_remove(struct vop_remove_args *v)
553 {
554 	struct vnode *dvp = v->a_dvp;
555 	struct vnode *vp = v->a_vp;
556 
557 	int error;
558 	struct tmpfs_dirent *de;
559 	struct tmpfs_mount *tmp;
560 	struct tmpfs_node *dnode;
561 	struct tmpfs_node *node;
562 
563 	MPASS(VOP_ISLOCKED(dvp));
564 	MPASS(VOP_ISLOCKED(vp));
565 
566 	if (vp->v_type == VDIR) {
567 		error = EISDIR;
568 		goto out;
569 	}
570 
571 	dnode = VP_TO_TMPFS_DIR(dvp);
572 	node = VP_TO_TMPFS_NODE(vp);
573 	tmp = VFS_TO_TMPFS(vp->v_mount);
574 	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
575 	MPASS(de != NULL);
576 
577 	/* Files marked as immutable or append-only cannot be deleted. */
578 	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
579 	    (dnode->tn_flags & APPEND)) {
580 		error = EPERM;
581 		goto out;
582 	}
583 
584 	/* Remove the entry from the directory; as it is a file, we do not
585 	 * have to change the number of hard links of the directory. */
586 	tmpfs_dir_detach(dvp, de);
587 	if (v->a_cnp->cn_flags & DOWHITEOUT)
588 		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
589 
590 	/* Free the directory entry we just deleted.  Note that the node
591 	 * referred by it will not be removed until the vnode is really
592 	 * reclaimed. */
593 	tmpfs_free_dirent(tmp, de);
594 
595 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
596 	error = 0;
597 
598 out:
599 
600 	return error;
601 }
602 
603 static int
604 tmpfs_link(struct vop_link_args *v)
605 {
606 	struct vnode *dvp = v->a_tdvp;
607 	struct vnode *vp = v->a_vp;
608 	struct componentname *cnp = v->a_cnp;
609 
610 	int error;
611 	struct tmpfs_dirent *de;
612 	struct tmpfs_node *node;
613 
614 	MPASS(VOP_ISLOCKED(dvp));
615 	MPASS(cnp->cn_flags & HASBUF);
616 	MPASS(dvp != vp); /* XXX When can this be false? */
617 	node = VP_TO_TMPFS_NODE(vp);
618 
619 	/* Ensure that we do not overflow the maximum number of links imposed
620 	 * by the system. */
621 	MPASS(node->tn_links <= LINK_MAX);
622 	if (node->tn_links == LINK_MAX) {
623 		error = EMLINK;
624 		goto out;
625 	}
626 
627 	/* We cannot create links of files marked immutable or append-only. */
628 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
629 		error = EPERM;
630 		goto out;
631 	}
632 
633 	/* Allocate a new directory entry to represent the node. */
634 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
635 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
636 	if (error != 0)
637 		goto out;
638 
639 	/* Insert the new directory entry into the appropriate directory. */
640 	if (cnp->cn_flags & ISWHITEOUT)
641 		tmpfs_dir_whiteout_remove(dvp, cnp);
642 	tmpfs_dir_attach(dvp, de);
643 
644 	/* vp link count has changed, so update node times. */
645 	node->tn_status |= TMPFS_NODE_CHANGED;
646 	tmpfs_update(vp);
647 
648 	error = 0;
649 
650 out:
651 	return error;
652 }
653 
654 /*
655  * We acquire all but fdvp locks using non-blocking acquisitions.  If we
656  * fail to acquire any lock in the path we will drop all held locks,
657  * acquire the new lock in a blocking fashion, and then release it and
658  * restart the rename.  This acquire/release step ensures that we do not
659  * spin on a lock waiting for release.  On error release all vnode locks
660  * and decrement references the way tmpfs_rename() would do.
661  */
662 static int
663 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
664     struct vnode *tdvp, struct vnode **tvpp,
665     struct componentname *fcnp, struct componentname *tcnp)
666 {
667 	struct vnode *nvp;
668 	struct mount *mp;
669 	struct tmpfs_dirent *de;
670 	int error, restarts = 0;
671 
672 	VOP_UNLOCK(tdvp, 0);
673 	if (*tvpp != NULL && *tvpp != tdvp)
674 		VOP_UNLOCK(*tvpp, 0);
675 	mp = fdvp->v_mount;
676 
677 relock:
678 	restarts += 1;
679 	error = vn_lock(fdvp, LK_EXCLUSIVE);
680 	if (error)
681 		goto releout;
682 	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
683 		VOP_UNLOCK(fdvp, 0);
684 		error = vn_lock(tdvp, LK_EXCLUSIVE);
685 		if (error)
686 			goto releout;
687 		VOP_UNLOCK(tdvp, 0);
688 		goto relock;
689 	}
690 	/*
691 	 * Re-resolve fvp to be certain it still exists and fetch the
692 	 * correct vnode.
693 	 */
694 	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
695 	if (de == NULL) {
696 		VOP_UNLOCK(fdvp, 0);
697 		VOP_UNLOCK(tdvp, 0);
698 		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
699 		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
700 			error = EINVAL;
701 		else
702 			error = ENOENT;
703 		goto releout;
704 	}
705 	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
706 	if (error != 0) {
707 		VOP_UNLOCK(fdvp, 0);
708 		VOP_UNLOCK(tdvp, 0);
709 		if (error != EBUSY)
710 			goto releout;
711 		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
712 		if (error != 0)
713 			goto releout;
714 		VOP_UNLOCK(nvp, 0);
715 		/*
716 		 * Concurrent rename race.
717 		 */
718 		if (nvp == tdvp) {
719 			vrele(nvp);
720 			error = EINVAL;
721 			goto releout;
722 		}
723 		vrele(*fvpp);
724 		*fvpp = nvp;
725 		goto relock;
726 	}
727 	vrele(*fvpp);
728 	*fvpp = nvp;
729 	VOP_UNLOCK(*fvpp, 0);
730 	/*
731 	 * Re-resolve tvp and acquire the vnode lock if present.
732 	 */
733 	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
734 	/*
735 	 * If tvp disappeared we just carry on.
736 	 */
737 	if (de == NULL && *tvpp != NULL) {
738 		vrele(*tvpp);
739 		*tvpp = NULL;
740 	}
741 	/*
742 	 * Get the tvp ino if the lookup succeeded.  We may have to restart
743 	 * if the non-blocking acquire fails.
744 	 */
745 	if (de != NULL) {
746 		nvp = NULL;
747 		error = tmpfs_alloc_vp(mp, de->td_node,
748 		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
749 		if (*tvpp != NULL)
750 			vrele(*tvpp);
751 		*tvpp = nvp;
752 		if (error != 0) {
753 			VOP_UNLOCK(fdvp, 0);
754 			VOP_UNLOCK(tdvp, 0);
755 			if (error != EBUSY)
756 				goto releout;
757 			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
758 			    &nvp);
759 			if (error != 0)
760 				goto releout;
761 			VOP_UNLOCK(nvp, 0);
762 			/*
763 			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
764 			 */
765 			if (nvp == fdvp) {
766 				error = ENOTEMPTY;
767 				goto releout;
768 			}
769 			goto relock;
770 		}
771 	}
772 	tmpfs_rename_restarts += restarts;
773 
774 	return (0);
775 
776 releout:
777 	vrele(fdvp);
778 	vrele(*fvpp);
779 	vrele(tdvp);
780 	if (*tvpp != NULL)
781 		vrele(*tvpp);
782 	tmpfs_rename_restarts += restarts;
783 
784 	return (error);
785 }
786 
787 static int
788 tmpfs_rename(struct vop_rename_args *v)
789 {
790 	struct vnode *fdvp = v->a_fdvp;
791 	struct vnode *fvp = v->a_fvp;
792 	struct componentname *fcnp = v->a_fcnp;
793 	struct vnode *tdvp = v->a_tdvp;
794 	struct vnode *tvp = v->a_tvp;
795 	struct componentname *tcnp = v->a_tcnp;
796 	struct mount *mp = NULL;
797 
798 	char *newname;
799 	int error;
800 	struct tmpfs_dirent *de;
801 	struct tmpfs_mount *tmp;
802 	struct tmpfs_node *fdnode;
803 	struct tmpfs_node *fnode;
804 	struct tmpfs_node *tnode;
805 	struct tmpfs_node *tdnode;
806 
807 	MPASS(VOP_ISLOCKED(tdvp));
808 	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
809 	MPASS(fcnp->cn_flags & HASBUF);
810 	MPASS(tcnp->cn_flags & HASBUF);
811 
812 	/* Disallow cross-device renames.
813 	 * XXX Why isn't this done by the caller? */
814 	if (fvp->v_mount != tdvp->v_mount ||
815 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
816 		error = EXDEV;
817 		goto out;
818 	}
819 
820 	/* If source and target are the same file, there is nothing to do. */
821 	if (fvp == tvp) {
822 		error = 0;
823 		goto out;
824 	}
825 
826 	/* If we need to move the directory between entries, lock the
827 	 * source so that we can safely operate on it. */
828 	if (fdvp != tdvp && fdvp != tvp) {
829 		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
830 			mp = tdvp->v_mount;
831 			error = vfs_busy(mp, 0);
832 			if (error != 0) {
833 				mp = NULL;
834 				goto out;
835 			}
836 			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
837 			    fcnp, tcnp);
838 			if (error != 0) {
839 				vfs_unbusy(mp);
840 				return (error);
841 			}
842 			ASSERT_VOP_ELOCKED(fdvp,
843 			    "tmpfs_rename: fdvp not locked");
844 			ASSERT_VOP_ELOCKED(tdvp,
845 			    "tmpfs_rename: tdvp not locked");
846 			if (tvp != NULL)
847 				ASSERT_VOP_ELOCKED(tvp,
848 				    "tmpfs_rename: tvp not locked");
849 			if (fvp == tvp) {
850 				error = 0;
851 				goto out_locked;
852 			}
853 		}
854 	}
855 
856 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
857 	tdnode = VP_TO_TMPFS_DIR(tdvp);
858 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
859 	fdnode = VP_TO_TMPFS_DIR(fdvp);
860 	fnode = VP_TO_TMPFS_NODE(fvp);
861 	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
862 
863 	/* Entry can disappear before we lock fdvp,
864 	 * also avoid manipulating '.' and '..' entries. */
865 	if (de == NULL) {
866 		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
867 		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
868 			error = EINVAL;
869 		else
870 			error = ENOENT;
871 		goto out_locked;
872 	}
873 	MPASS(de->td_node == fnode);
874 
875 	/* If re-naming a directory to another preexisting directory
876 	 * ensure that the target directory is empty so that its
877 	 * removal causes no side effects.
878 	 * Kern_rename guarantees the destination to be a directory
879 	 * if the source is one. */
880 	if (tvp != NULL) {
881 		MPASS(tnode != NULL);
882 
883 		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
884 		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
885 			error = EPERM;
886 			goto out_locked;
887 		}
888 
889 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
890 			if (tnode->tn_size > 0) {
891 				error = ENOTEMPTY;
892 				goto out_locked;
893 			}
894 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
895 			error = ENOTDIR;
896 			goto out_locked;
897 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
898 			error = EISDIR;
899 			goto out_locked;
900 		} else {
901 			MPASS(fnode->tn_type != VDIR &&
902 				tnode->tn_type != VDIR);
903 		}
904 	}
905 
906 	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
907 	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
908 		error = EPERM;
909 		goto out_locked;
910 	}
911 
912 	/* Ensure that we have enough memory to hold the new name, if it
913 	 * has to be changed. */
914 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
915 	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
916 		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
917 	} else
918 		newname = NULL;
919 
920 	/* If the node is being moved to another directory, we have to do
921 	 * the move. */
922 	if (fdnode != tdnode) {
923 		/* In case we are moving a directory, we have to adjust its
924 		 * parent to point to the new parent. */
925 		if (de->td_node->tn_type == VDIR) {
926 			struct tmpfs_node *n;
927 
928 			/* Ensure the target directory is not a child of the
929 			 * directory being moved.  Otherwise, we'd end up
930 			 * with stale nodes. */
931 			n = tdnode;
932 			/* TMPFS_LOCK garanties that no nodes are freed while
933 			 * traversing the list. Nodes can only be marked as
934 			 * removed: tn_parent == NULL. */
935 			TMPFS_LOCK(tmp);
936 			TMPFS_NODE_LOCK(n);
937 			while (n != n->tn_dir.tn_parent) {
938 				struct tmpfs_node *parent;
939 
940 				if (n == fnode) {
941 					TMPFS_NODE_UNLOCK(n);
942 					TMPFS_UNLOCK(tmp);
943 					error = EINVAL;
944 					if (newname != NULL)
945 						    free(newname, M_TMPFSNAME);
946 					goto out_locked;
947 				}
948 				parent = n->tn_dir.tn_parent;
949 				TMPFS_NODE_UNLOCK(n);
950 				if (parent == NULL) {
951 					n = NULL;
952 					break;
953 				}
954 				TMPFS_NODE_LOCK(parent);
955 				if (parent->tn_dir.tn_parent == NULL) {
956 					TMPFS_NODE_UNLOCK(parent);
957 					n = NULL;
958 					break;
959 				}
960 				n = parent;
961 			}
962 			TMPFS_UNLOCK(tmp);
963 			if (n == NULL) {
964 				error = EINVAL;
965 				if (newname != NULL)
966 					    free(newname, M_TMPFSNAME);
967 				goto out_locked;
968 			}
969 			TMPFS_NODE_UNLOCK(n);
970 
971 			/* Adjust the parent pointer. */
972 			TMPFS_VALIDATE_DIR(fnode);
973 			TMPFS_NODE_LOCK(de->td_node);
974 			de->td_node->tn_dir.tn_parent = tdnode;
975 			TMPFS_NODE_UNLOCK(de->td_node);
976 
977 			/* As a result of changing the target of the '..'
978 			 * entry, the link count of the source and target
979 			 * directories has to be adjusted. */
980 			TMPFS_NODE_LOCK(tdnode);
981 			TMPFS_ASSERT_LOCKED(tdnode);
982 			tdnode->tn_links++;
983 			TMPFS_NODE_UNLOCK(tdnode);
984 
985 			TMPFS_NODE_LOCK(fdnode);
986 			TMPFS_ASSERT_LOCKED(fdnode);
987 			fdnode->tn_links--;
988 			TMPFS_NODE_UNLOCK(fdnode);
989 		}
990 	}
991 
992 	/* Do the move: just remove the entry from the source directory
993 	 * and insert it into the target one. */
994 	tmpfs_dir_detach(fdvp, de);
995 
996 	if (fcnp->cn_flags & DOWHITEOUT)
997 		tmpfs_dir_whiteout_add(fdvp, fcnp);
998 	if (tcnp->cn_flags & ISWHITEOUT)
999 		tmpfs_dir_whiteout_remove(tdvp, tcnp);
1000 
1001 	/* If the name has changed, we need to make it effective by changing
1002 	 * it in the directory entry. */
1003 	if (newname != NULL) {
1004 		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1005 
1006 		free(de->ud.td_name, M_TMPFSNAME);
1007 		de->ud.td_name = newname;
1008 		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1009 
1010 		fnode->tn_status |= TMPFS_NODE_CHANGED;
1011 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1012 	}
1013 
1014 	/* If we are overwriting an entry, we have to remove the old one
1015 	 * from the target directory. */
1016 	if (tvp != NULL) {
1017 		struct tmpfs_dirent *tde;
1018 
1019 		/* Remove the old entry from the target directory. */
1020 		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1021 		tmpfs_dir_detach(tdvp, tde);
1022 
1023 		/* Free the directory entry we just deleted.  Note that the
1024 		 * node referred by it will not be removed until the vnode is
1025 		 * really reclaimed. */
1026 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1027 	}
1028 
1029 	tmpfs_dir_attach(tdvp, de);
1030 
1031 	if (tmpfs_use_nc(fvp)) {
1032 		cache_purge(fvp);
1033 		if (tvp != NULL)
1034 			cache_purge(tvp);
1035 		cache_purge_negative(tdvp);
1036 	}
1037 
1038 	error = 0;
1039 
1040 out_locked:
1041 	if (fdvp != tdvp && fdvp != tvp)
1042 		VOP_UNLOCK(fdvp, 0);
1043 
1044 out:
1045 	/* Release target nodes. */
1046 	/* XXX: I don't understand when tdvp can be the same as tvp, but
1047 	 * other code takes care of this... */
1048 	if (tdvp == tvp)
1049 		vrele(tdvp);
1050 	else
1051 		vput(tdvp);
1052 	if (tvp != NULL)
1053 		vput(tvp);
1054 
1055 	/* Release source nodes. */
1056 	vrele(fdvp);
1057 	vrele(fvp);
1058 
1059 	if (mp != NULL)
1060 		vfs_unbusy(mp);
1061 
1062 	return error;
1063 }
1064 
1065 static int
1066 tmpfs_mkdir(struct vop_mkdir_args *v)
1067 {
1068 	struct vnode *dvp = v->a_dvp;
1069 	struct vnode **vpp = v->a_vpp;
1070 	struct componentname *cnp = v->a_cnp;
1071 	struct vattr *vap = v->a_vap;
1072 
1073 	MPASS(vap->va_type == VDIR);
1074 
1075 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1076 }
1077 
1078 static int
1079 tmpfs_rmdir(struct vop_rmdir_args *v)
1080 {
1081 	struct vnode *dvp = v->a_dvp;
1082 	struct vnode *vp = v->a_vp;
1083 
1084 	int error;
1085 	struct tmpfs_dirent *de;
1086 	struct tmpfs_mount *tmp;
1087 	struct tmpfs_node *dnode;
1088 	struct tmpfs_node *node;
1089 
1090 	MPASS(VOP_ISLOCKED(dvp));
1091 	MPASS(VOP_ISLOCKED(vp));
1092 
1093 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1094 	dnode = VP_TO_TMPFS_DIR(dvp);
1095 	node = VP_TO_TMPFS_DIR(vp);
1096 
1097 	/* Directories with more than two entries ('.' and '..') cannot be
1098 	 * removed. */
1099 	 if (node->tn_size > 0) {
1100 		 error = ENOTEMPTY;
1101 		 goto out;
1102 	 }
1103 
1104 	if ((dnode->tn_flags & APPEND)
1105 	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1106 		error = EPERM;
1107 		goto out;
1108 	}
1109 
1110 	/* This invariant holds only if we are not trying to remove "..".
1111 	  * We checked for that above so this is safe now. */
1112 	MPASS(node->tn_dir.tn_parent == dnode);
1113 
1114 	/* Get the directory entry associated with node (vp).  This was
1115 	 * filled by tmpfs_lookup while looking up the entry. */
1116 	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1117 	MPASS(TMPFS_DIRENT_MATCHES(de,
1118 	    v->a_cnp->cn_nameptr,
1119 	    v->a_cnp->cn_namelen));
1120 
1121 	/* Check flags to see if we are allowed to remove the directory. */
1122 	if ((dnode->tn_flags & APPEND) != 0 ||
1123 	    (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1124 		error = EPERM;
1125 		goto out;
1126 	}
1127 
1128 
1129 	/* Detach the directory entry from the directory (dnode). */
1130 	tmpfs_dir_detach(dvp, de);
1131 	if (v->a_cnp->cn_flags & DOWHITEOUT)
1132 		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1133 
1134 	/* No vnode should be allocated for this entry from this point */
1135 	TMPFS_NODE_LOCK(node);
1136 	node->tn_links--;
1137 	node->tn_dir.tn_parent = NULL;
1138 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1139 	    TMPFS_NODE_MODIFIED;
1140 
1141 	TMPFS_NODE_UNLOCK(node);
1142 
1143 	TMPFS_NODE_LOCK(dnode);
1144 	dnode->tn_links--;
1145 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1146 	    TMPFS_NODE_MODIFIED;
1147 	TMPFS_NODE_UNLOCK(dnode);
1148 
1149 	if (tmpfs_use_nc(dvp)) {
1150 		cache_purge(dvp);
1151 		cache_purge(vp);
1152 	}
1153 
1154 	/* Free the directory entry we just deleted.  Note that the node
1155 	 * referred by it will not be removed until the vnode is really
1156 	 * reclaimed. */
1157 	tmpfs_free_dirent(tmp, de);
1158 
1159 	/* Release the deleted vnode (will destroy the node, notify
1160 	 * interested parties and clean it from the cache). */
1161 
1162 	dnode->tn_status |= TMPFS_NODE_CHANGED;
1163 	tmpfs_update(dvp);
1164 
1165 	error = 0;
1166 
1167 out:
1168 	return error;
1169 }
1170 
1171 static int
1172 tmpfs_symlink(struct vop_symlink_args *v)
1173 {
1174 	struct vnode *dvp = v->a_dvp;
1175 	struct vnode **vpp = v->a_vpp;
1176 	struct componentname *cnp = v->a_cnp;
1177 	struct vattr *vap = v->a_vap;
1178 	char *target = v->a_target;
1179 
1180 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1181 	MPASS(vap->va_type == VLNK);
1182 #else
1183 	vap->va_type = VLNK;
1184 #endif
1185 
1186 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1187 }
1188 
1189 static int
1190 tmpfs_readdir(struct vop_readdir_args *v)
1191 {
1192 	struct vnode *vp = v->a_vp;
1193 	struct uio *uio = v->a_uio;
1194 	int *eofflag = v->a_eofflag;
1195 	u_long **cookies = v->a_cookies;
1196 	int *ncookies = v->a_ncookies;
1197 
1198 	int error;
1199 	ssize_t startresid;
1200 	int maxcookies;
1201 	struct tmpfs_node *node;
1202 
1203 	/* This operation only makes sense on directory nodes. */
1204 	if (vp->v_type != VDIR)
1205 		return ENOTDIR;
1206 
1207 	maxcookies = 0;
1208 	node = VP_TO_TMPFS_DIR(vp);
1209 
1210 	startresid = uio->uio_resid;
1211 
1212 	/* Allocate cookies for NFS and compat modules. */
1213 	if (cookies != NULL && ncookies != NULL) {
1214 		maxcookies = howmany(node->tn_size,
1215 		    sizeof(struct tmpfs_dirent)) + 2;
1216 		*cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1217 		    M_WAITOK);
1218 		*ncookies = 0;
1219 	}
1220 
1221 	if (cookies == NULL)
1222 		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1223 	else
1224 		error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1225 		    ncookies);
1226 
1227 	/* Buffer was filled without hitting EOF. */
1228 	if (error == EJUSTRETURN)
1229 		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1230 
1231 	if (error != 0 && cookies != NULL && ncookies != NULL) {
1232 		free(*cookies, M_TEMP);
1233 		*cookies = NULL;
1234 		*ncookies = 0;
1235 	}
1236 
1237 	if (eofflag != NULL)
1238 		*eofflag =
1239 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1240 
1241 	return error;
1242 }
1243 
1244 static int
1245 tmpfs_readlink(struct vop_readlink_args *v)
1246 {
1247 	struct vnode *vp = v->a_vp;
1248 	struct uio *uio = v->a_uio;
1249 
1250 	int error;
1251 	struct tmpfs_node *node;
1252 
1253 	MPASS(uio->uio_offset == 0);
1254 	MPASS(vp->v_type == VLNK);
1255 
1256 	node = VP_TO_TMPFS_NODE(vp);
1257 
1258 	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1259 	    uio);
1260 	tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
1261 
1262 	return (error);
1263 }
1264 
1265 static int
1266 tmpfs_inactive(struct vop_inactive_args *v)
1267 {
1268 	struct vnode *vp;
1269 	struct tmpfs_node *node;
1270 
1271 	vp = v->a_vp;
1272 	node = VP_TO_TMPFS_NODE(vp);
1273 	if (node->tn_links == 0)
1274 		vrecycle(vp);
1275 	else
1276 		tmpfs_check_mtime(vp);
1277 	return (0);
1278 }
1279 
1280 int
1281 tmpfs_reclaim(struct vop_reclaim_args *v)
1282 {
1283 	struct vnode *vp = v->a_vp;
1284 
1285 	struct tmpfs_mount *tmp;
1286 	struct tmpfs_node *node;
1287 
1288 	node = VP_TO_TMPFS_NODE(vp);
1289 	tmp = VFS_TO_TMPFS(vp->v_mount);
1290 
1291 	if (vp->v_type == VREG)
1292 		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1293 	else
1294 		vnode_destroy_vobject(vp);
1295 	vp->v_object = NULL;
1296 	if (tmpfs_use_nc(vp))
1297 		cache_purge(vp);
1298 
1299 	TMPFS_NODE_LOCK(node);
1300 	tmpfs_free_vp(vp);
1301 
1302 	/* If the node referenced by this vnode was deleted by the user,
1303 	 * we must free its associated data structures (now that the vnode
1304 	 * is being reclaimed). */
1305 	if (node->tn_links == 0 &&
1306 	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1307 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1308 		TMPFS_NODE_UNLOCK(node);
1309 		tmpfs_free_node(tmp, node);
1310 	} else
1311 		TMPFS_NODE_UNLOCK(node);
1312 
1313 	MPASS(vp->v_data == NULL);
1314 	return 0;
1315 }
1316 
1317 int
1318 tmpfs_print(struct vop_print_args *v)
1319 {
1320 	struct vnode *vp = v->a_vp;
1321 
1322 	struct tmpfs_node *node;
1323 
1324 	node = VP_TO_TMPFS_NODE(vp);
1325 
1326 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
1327 	    node, node->tn_flags, (uintmax_t)node->tn_links);
1328 	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1329 	    node->tn_mode, node->tn_uid, node->tn_gid,
1330 	    (intmax_t)node->tn_size, node->tn_status);
1331 
1332 	if (vp->v_type == VFIFO)
1333 		fifo_printinfo(vp);
1334 
1335 	printf("\n");
1336 
1337 	return 0;
1338 }
1339 
1340 static int
1341 tmpfs_pathconf(struct vop_pathconf_args *v)
1342 {
1343 	int name = v->a_name;
1344 	register_t *retval = v->a_retval;
1345 
1346 	int error;
1347 
1348 	error = 0;
1349 
1350 	switch (name) {
1351 	case _PC_NO_TRUNC:
1352 		*retval = 1;
1353 		break;
1354 
1355 	case _PC_SYNC_IO:
1356 		*retval = 1;
1357 		break;
1358 
1359 	case _PC_FILESIZEBITS:
1360 		*retval = 64;
1361 		break;
1362 
1363 	default:
1364 		error = vop_stdpathconf(v);
1365 	}
1366 
1367 	return error;
1368 }
1369 
1370 static int
1371 tmpfs_vptofh(struct vop_vptofh_args *ap)
1372 {
1373 	struct tmpfs_fid *tfhp;
1374 	struct tmpfs_node *node;
1375 
1376 	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1377 	node = VP_TO_TMPFS_NODE(ap->a_vp);
1378 
1379 	tfhp->tf_len = sizeof(struct tmpfs_fid);
1380 	tfhp->tf_id = node->tn_id;
1381 	tfhp->tf_gen = node->tn_gen;
1382 
1383 	return (0);
1384 }
1385 
1386 static int
1387 tmpfs_whiteout(struct vop_whiteout_args *ap)
1388 {
1389 	struct vnode *dvp = ap->a_dvp;
1390 	struct componentname *cnp = ap->a_cnp;
1391 	struct tmpfs_dirent *de;
1392 
1393 	switch (ap->a_flags) {
1394 	case LOOKUP:
1395 		return (0);
1396 	case CREATE:
1397 		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1398 		if (de != NULL)
1399 			return (de->td_node == NULL ? 0 : EEXIST);
1400 		return (tmpfs_dir_whiteout_add(dvp, cnp));
1401 	case DELETE:
1402 		tmpfs_dir_whiteout_remove(dvp, cnp);
1403 		return (0);
1404 	default:
1405 		panic("tmpfs_whiteout: unknown op");
1406 	}
1407 }
1408 
1409 static int
1410 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1411     struct tmpfs_dirent **pde)
1412 {
1413 	struct tmpfs_dir_cursor dc;
1414 	struct tmpfs_dirent *de;
1415 
1416 	for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1417 	     de = tmpfs_dir_next(tnp, &dc)) {
1418 		if (de->td_node == tn) {
1419 			*pde = de;
1420 			return (0);
1421 		}
1422 	}
1423 	return (ENOENT);
1424 }
1425 
1426 static int
1427 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1428     struct tmpfs_node *tnp, char *buf, int *buflen, struct vnode **dvp)
1429 {
1430 	struct tmpfs_dirent *de;
1431 	int error, i;
1432 
1433 	error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1434 	    dvp);
1435 	if (error != 0)
1436 		return (error);
1437 	error = tmpfs_vptocnp_dir(tn, tnp, &de);
1438 	if (error == 0) {
1439 		i = *buflen;
1440 		i -= de->td_namelen;
1441 		if (i < 0) {
1442 			error = ENOMEM;
1443 		} else {
1444 			bcopy(de->ud.td_name, buf + i, de->td_namelen);
1445 			*buflen = i;
1446 		}
1447 	}
1448 	if (error == 0) {
1449 		if (vp != *dvp)
1450 			VOP_UNLOCK(*dvp, 0);
1451 	} else {
1452 		if (vp != *dvp)
1453 			vput(*dvp);
1454 		else
1455 			vrele(vp);
1456 	}
1457 	return (error);
1458 }
1459 
1460 static int
1461 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1462 {
1463 	struct vnode *vp, **dvp;
1464 	struct tmpfs_node *tn, *tnp, *tnp1;
1465 	struct tmpfs_dirent *de;
1466 	struct tmpfs_mount *tm;
1467 	char *buf;
1468 	int *buflen;
1469 	int error;
1470 
1471 	vp = ap->a_vp;
1472 	dvp = ap->a_vpp;
1473 	buf = ap->a_buf;
1474 	buflen = ap->a_buflen;
1475 
1476 	tm = VFS_TO_TMPFS(vp->v_mount);
1477 	tn = VP_TO_TMPFS_NODE(vp);
1478 	if (tn->tn_type == VDIR) {
1479 		tnp = tn->tn_dir.tn_parent;
1480 		if (tnp == NULL)
1481 			return (ENOENT);
1482 		tmpfs_ref_node(tnp);
1483 		error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1484 		    buflen, dvp);
1485 		tmpfs_free_node(tm, tnp);
1486 		return (error);
1487 	}
1488 restart:
1489 	TMPFS_LOCK(tm);
1490 	LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1491 		if (tnp->tn_type != VDIR)
1492 			continue;
1493 		TMPFS_NODE_LOCK(tnp);
1494 		tmpfs_ref_node_locked(tnp);
1495 
1496 		/*
1497 		 * tn_vnode cannot be instantiated while we hold the
1498 		 * node lock, so the directory cannot be changed while
1499 		 * we iterate over it.  Do this to avoid instantiating
1500 		 * vnode for directories which cannot point to our
1501 		 * node.
1502 		 */
1503 		error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1504 		    &de) : 0;
1505 
1506 		if (error == 0) {
1507 			TMPFS_NODE_UNLOCK(tnp);
1508 			TMPFS_UNLOCK(tm);
1509 			error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1510 			    dvp);
1511 			if (error == 0) {
1512 				tmpfs_free_node(tm, tnp);
1513 				return (0);
1514 			}
1515 			if ((vp->v_iflag & VI_DOOMED) != 0) {
1516 				tmpfs_free_node(tm, tnp);
1517 				return (ENOENT);
1518 			}
1519 			TMPFS_LOCK(tm);
1520 			TMPFS_NODE_LOCK(tnp);
1521 		}
1522 		if (tmpfs_free_node_locked(tm, tnp, false)) {
1523 			goto restart;
1524 		} else {
1525 			KASSERT(tnp->tn_refcount > 0,
1526 			    ("node %p refcount zero", tnp));
1527 			tnp1 = LIST_NEXT(tnp, tn_entries);
1528 			TMPFS_NODE_UNLOCK(tnp);
1529 		}
1530 	}
1531 	TMPFS_UNLOCK(tm);
1532 	return (ENOENT);
1533 }
1534 
1535 /*
1536  * Vnode operations vector used for files stored in a tmpfs file system.
1537  */
1538 struct vop_vector tmpfs_vnodeop_entries = {
1539 	.vop_default =			&default_vnodeops,
1540 	.vop_lookup =			vfs_cache_lookup,
1541 	.vop_cachedlookup =		tmpfs_cached_lookup,
1542 	.vop_create =			tmpfs_create,
1543 	.vop_mknod =			tmpfs_mknod,
1544 	.vop_open =			tmpfs_open,
1545 	.vop_close =			tmpfs_close,
1546 	.vop_access =			tmpfs_access,
1547 	.vop_getattr =			tmpfs_getattr,
1548 	.vop_setattr =			tmpfs_setattr,
1549 	.vop_read =			tmpfs_read,
1550 	.vop_write =			tmpfs_write,
1551 	.vop_fsync =			tmpfs_fsync,
1552 	.vop_remove =			tmpfs_remove,
1553 	.vop_link =			tmpfs_link,
1554 	.vop_rename =			tmpfs_rename,
1555 	.vop_mkdir =			tmpfs_mkdir,
1556 	.vop_rmdir =			tmpfs_rmdir,
1557 	.vop_symlink =			tmpfs_symlink,
1558 	.vop_readdir =			tmpfs_readdir,
1559 	.vop_readlink =			tmpfs_readlink,
1560 	.vop_inactive =			tmpfs_inactive,
1561 	.vop_reclaim =			tmpfs_reclaim,
1562 	.vop_print =			tmpfs_print,
1563 	.vop_pathconf =			tmpfs_pathconf,
1564 	.vop_vptofh =			tmpfs_vptofh,
1565 	.vop_whiteout =			tmpfs_whiteout,
1566 	.vop_bmap =			VOP_EOPNOTSUPP,
1567 	.vop_vptocnp =			tmpfs_vptocnp,
1568 };
1569 
1570 /*
1571  * Same vector for mounts which do not use namecache.
1572  */
1573 struct vop_vector tmpfs_vnodeop_nonc_entries = {
1574 	.vop_default =			&tmpfs_vnodeop_entries,
1575 	.vop_lookup =			tmpfs_lookup,
1576 };
1577