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