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