xref: /freebsd/sys/fs/tmpfs/tmpfs_vnops.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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 	vm_object_t obj;
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 	if (vp->v_type == VREG) {
370 		obj = node->tn_reg.tn_aobj;
371 		vap->va_bytes = (u_quad_t)obj->resident_page_count * PAGE_SIZE;
372 	} else
373 		vap->va_bytes = node->tn_size;
374 	vap->va_filerev = 0;
375 
376 	return 0;
377 }
378 
379 int
380 tmpfs_setattr(struct vop_setattr_args *v)
381 {
382 	struct vnode *vp = v->a_vp;
383 	struct vattr *vap = v->a_vap;
384 	struct ucred *cred = v->a_cred;
385 	struct thread *td = curthread;
386 
387 	int error;
388 
389 	MPASS(VOP_ISLOCKED(vp));
390 
391 	error = 0;
392 
393 	/* Abort if any unsettable attribute is given. */
394 	if (vap->va_type != VNON ||
395 	    vap->va_nlink != VNOVAL ||
396 	    vap->va_fsid != VNOVAL ||
397 	    vap->va_fileid != VNOVAL ||
398 	    vap->va_blocksize != VNOVAL ||
399 	    vap->va_gen != VNOVAL ||
400 	    vap->va_rdev != VNOVAL ||
401 	    vap->va_bytes != VNOVAL)
402 		error = EINVAL;
403 
404 	if (error == 0 && (vap->va_flags != VNOVAL))
405 		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
406 
407 	if (error == 0 && (vap->va_size != VNOVAL))
408 		error = tmpfs_chsize(vp, vap->va_size, cred, td);
409 
410 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
411 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
412 
413 	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
414 		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
415 
416 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
417 	    vap->va_atime.tv_nsec != VNOVAL) ||
418 	    (vap->va_mtime.tv_sec != VNOVAL &&
419 	    vap->va_mtime.tv_nsec != VNOVAL) ||
420 	    (vap->va_birthtime.tv_sec != VNOVAL &&
421 	    vap->va_birthtime.tv_nsec != VNOVAL)))
422 		error = tmpfs_chtimes(vp, vap, cred, td);
423 
424 	/* Update the node times.  We give preference to the error codes
425 	 * generated by this function rather than the ones that may arise
426 	 * from tmpfs_update. */
427 	tmpfs_update(vp);
428 
429 	MPASS(VOP_ISLOCKED(vp));
430 
431 	return error;
432 }
433 
434 static int
435 tmpfs_read(struct vop_read_args *v)
436 {
437 	struct vnode *vp;
438 	struct uio *uio;
439 	struct tmpfs_node *node;
440 
441 	vp = v->a_vp;
442 	if (vp->v_type != VREG)
443 		return (EISDIR);
444 	uio = v->a_uio;
445 	if (uio->uio_offset < 0)
446 		return (EINVAL);
447 	node = VP_TO_TMPFS_NODE(vp);
448 	node->tn_status |= TMPFS_NODE_ACCESSED;
449 	return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
450 }
451 
452 static int
453 tmpfs_write(struct vop_write_args *v)
454 {
455 	struct vnode *vp;
456 	struct uio *uio;
457 	struct tmpfs_node *node;
458 	off_t oldsize;
459 	int error, ioflag;
460 
461 	vp = v->a_vp;
462 	uio = v->a_uio;
463 	ioflag = v->a_ioflag;
464 	error = 0;
465 	node = VP_TO_TMPFS_NODE(vp);
466 	oldsize = node->tn_size;
467 
468 	if (uio->uio_offset < 0 || vp->v_type != VREG)
469 		return (EINVAL);
470 	if (uio->uio_resid == 0)
471 		return (0);
472 	if (ioflag & IO_APPEND)
473 		uio->uio_offset = node->tn_size;
474 	if (uio->uio_offset + uio->uio_resid >
475 	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
476 		return (EFBIG);
477 	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
478 		return (EFBIG);
479 	if (uio->uio_offset + uio->uio_resid > node->tn_size) {
480 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
481 		    FALSE);
482 		if (error != 0)
483 			goto out;
484 	}
485 
486 	error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
487 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
488 	    TMPFS_NODE_CHANGED;
489 	if (node->tn_mode & (S_ISUID | S_ISGID)) {
490 		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
491 			node->tn_mode &= ~(S_ISUID | S_ISGID);
492 	}
493 	if (error != 0)
494 		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
495 
496 out:
497 	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
498 	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
499 
500 	return (error);
501 }
502 
503 static int
504 tmpfs_fsync(struct vop_fsync_args *v)
505 {
506 	struct vnode *vp = v->a_vp;
507 
508 	MPASS(VOP_ISLOCKED(vp));
509 
510 	tmpfs_check_mtime(vp);
511 	tmpfs_update(vp);
512 
513 	return 0;
514 }
515 
516 static int
517 tmpfs_remove(struct vop_remove_args *v)
518 {
519 	struct vnode *dvp = v->a_dvp;
520 	struct vnode *vp = v->a_vp;
521 
522 	int error;
523 	struct tmpfs_dirent *de;
524 	struct tmpfs_mount *tmp;
525 	struct tmpfs_node *dnode;
526 	struct tmpfs_node *node;
527 
528 	MPASS(VOP_ISLOCKED(dvp));
529 	MPASS(VOP_ISLOCKED(vp));
530 
531 	if (vp->v_type == VDIR) {
532 		error = EISDIR;
533 		goto out;
534 	}
535 
536 	dnode = VP_TO_TMPFS_DIR(dvp);
537 	node = VP_TO_TMPFS_NODE(vp);
538 	tmp = VFS_TO_TMPFS(vp->v_mount);
539 	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
540 	MPASS(de != NULL);
541 
542 	/* Files marked as immutable or append-only cannot be deleted. */
543 	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
544 	    (dnode->tn_flags & APPEND)) {
545 		error = EPERM;
546 		goto out;
547 	}
548 
549 	/* Remove the entry from the directory; as it is a file, we do not
550 	 * have to change the number of hard links of the directory. */
551 	tmpfs_dir_detach(dvp, de);
552 	if (v->a_cnp->cn_flags & DOWHITEOUT)
553 		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
554 
555 	/* Free the directory entry we just deleted.  Note that the node
556 	 * referred by it will not be removed until the vnode is really
557 	 * reclaimed. */
558 	tmpfs_free_dirent(tmp, de);
559 
560 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
561 	error = 0;
562 
563 out:
564 
565 	return error;
566 }
567 
568 static int
569 tmpfs_link(struct vop_link_args *v)
570 {
571 	struct vnode *dvp = v->a_tdvp;
572 	struct vnode *vp = v->a_vp;
573 	struct componentname *cnp = v->a_cnp;
574 
575 	int error;
576 	struct tmpfs_dirent *de;
577 	struct tmpfs_node *node;
578 
579 	MPASS(VOP_ISLOCKED(dvp));
580 	MPASS(cnp->cn_flags & HASBUF);
581 	MPASS(dvp != vp); /* XXX When can this be false? */
582 	node = VP_TO_TMPFS_NODE(vp);
583 
584 	/* Ensure that we do not overflow the maximum number of links imposed
585 	 * by the system. */
586 	MPASS(node->tn_links <= LINK_MAX);
587 	if (node->tn_links == LINK_MAX) {
588 		error = EMLINK;
589 		goto out;
590 	}
591 
592 	/* We cannot create links of files marked immutable or append-only. */
593 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
594 		error = EPERM;
595 		goto out;
596 	}
597 
598 	/* Allocate a new directory entry to represent the node. */
599 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
600 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
601 	if (error != 0)
602 		goto out;
603 
604 	/* Insert the new directory entry into the appropriate directory. */
605 	if (cnp->cn_flags & ISWHITEOUT)
606 		tmpfs_dir_whiteout_remove(dvp, cnp);
607 	tmpfs_dir_attach(dvp, de);
608 
609 	/* vp link count has changed, so update node times. */
610 	node->tn_status |= TMPFS_NODE_CHANGED;
611 	tmpfs_update(vp);
612 
613 	error = 0;
614 
615 out:
616 	return error;
617 }
618 
619 /*
620  * We acquire all but fdvp locks using non-blocking acquisitions.  If we
621  * fail to acquire any lock in the path we will drop all held locks,
622  * acquire the new lock in a blocking fashion, and then release it and
623  * restart the rename.  This acquire/release step ensures that we do not
624  * spin on a lock waiting for release.  On error release all vnode locks
625  * and decrement references the way tmpfs_rename() would do.
626  */
627 static int
628 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
629     struct vnode *tdvp, struct vnode **tvpp,
630     struct componentname *fcnp, struct componentname *tcnp)
631 {
632 	struct vnode *nvp;
633 	struct mount *mp;
634 	struct tmpfs_dirent *de;
635 	int error, restarts = 0;
636 
637 	VOP_UNLOCK(tdvp, 0);
638 	if (*tvpp != NULL && *tvpp != tdvp)
639 		VOP_UNLOCK(*tvpp, 0);
640 	mp = fdvp->v_mount;
641 
642 relock:
643 	restarts += 1;
644 	error = vn_lock(fdvp, LK_EXCLUSIVE);
645 	if (error)
646 		goto releout;
647 	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
648 		VOP_UNLOCK(fdvp, 0);
649 		error = vn_lock(tdvp, LK_EXCLUSIVE);
650 		if (error)
651 			goto releout;
652 		VOP_UNLOCK(tdvp, 0);
653 		goto relock;
654 	}
655 	/*
656 	 * Re-resolve fvp to be certain it still exists and fetch the
657 	 * correct vnode.
658 	 */
659 	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
660 	if (de == NULL) {
661 		VOP_UNLOCK(fdvp, 0);
662 		VOP_UNLOCK(tdvp, 0);
663 		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
664 		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
665 			error = EINVAL;
666 		else
667 			error = ENOENT;
668 		goto releout;
669 	}
670 	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
671 	if (error != 0) {
672 		VOP_UNLOCK(fdvp, 0);
673 		VOP_UNLOCK(tdvp, 0);
674 		if (error != EBUSY)
675 			goto releout;
676 		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
677 		if (error != 0)
678 			goto releout;
679 		VOP_UNLOCK(nvp, 0);
680 		/*
681 		 * Concurrent rename race.
682 		 */
683 		if (nvp == tdvp) {
684 			vrele(nvp);
685 			error = EINVAL;
686 			goto releout;
687 		}
688 		vrele(*fvpp);
689 		*fvpp = nvp;
690 		goto relock;
691 	}
692 	vrele(*fvpp);
693 	*fvpp = nvp;
694 	VOP_UNLOCK(*fvpp, 0);
695 	/*
696 	 * Re-resolve tvp and acquire the vnode lock if present.
697 	 */
698 	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
699 	/*
700 	 * If tvp disappeared we just carry on.
701 	 */
702 	if (de == NULL && *tvpp != NULL) {
703 		vrele(*tvpp);
704 		*tvpp = NULL;
705 	}
706 	/*
707 	 * Get the tvp ino if the lookup succeeded.  We may have to restart
708 	 * if the non-blocking acquire fails.
709 	 */
710 	if (de != NULL) {
711 		nvp = NULL;
712 		error = tmpfs_alloc_vp(mp, de->td_node,
713 		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
714 		if (*tvpp != NULL)
715 			vrele(*tvpp);
716 		*tvpp = nvp;
717 		if (error != 0) {
718 			VOP_UNLOCK(fdvp, 0);
719 			VOP_UNLOCK(tdvp, 0);
720 			if (error != EBUSY)
721 				goto releout;
722 			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
723 			    &nvp);
724 			if (error != 0)
725 				goto releout;
726 			VOP_UNLOCK(nvp, 0);
727 			/*
728 			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
729 			 */
730 			if (nvp == fdvp) {
731 				error = ENOTEMPTY;
732 				goto releout;
733 			}
734 			goto relock;
735 		}
736 	}
737 	tmpfs_rename_restarts += restarts;
738 
739 	return (0);
740 
741 releout:
742 	vrele(fdvp);
743 	vrele(*fvpp);
744 	vrele(tdvp);
745 	if (*tvpp != NULL)
746 		vrele(*tvpp);
747 	tmpfs_rename_restarts += restarts;
748 
749 	return (error);
750 }
751 
752 static int
753 tmpfs_rename(struct vop_rename_args *v)
754 {
755 	struct vnode *fdvp = v->a_fdvp;
756 	struct vnode *fvp = v->a_fvp;
757 	struct componentname *fcnp = v->a_fcnp;
758 	struct vnode *tdvp = v->a_tdvp;
759 	struct vnode *tvp = v->a_tvp;
760 	struct componentname *tcnp = v->a_tcnp;
761 	struct mount *mp = NULL;
762 
763 	char *newname;
764 	int error;
765 	struct tmpfs_dirent *de;
766 	struct tmpfs_mount *tmp;
767 	struct tmpfs_node *fdnode;
768 	struct tmpfs_node *fnode;
769 	struct tmpfs_node *tnode;
770 	struct tmpfs_node *tdnode;
771 
772 	MPASS(VOP_ISLOCKED(tdvp));
773 	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
774 	MPASS(fcnp->cn_flags & HASBUF);
775 	MPASS(tcnp->cn_flags & HASBUF);
776 
777 	/* Disallow cross-device renames.
778 	 * XXX Why isn't this done by the caller? */
779 	if (fvp->v_mount != tdvp->v_mount ||
780 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
781 		error = EXDEV;
782 		goto out;
783 	}
784 
785 	/* If source and target are the same file, there is nothing to do. */
786 	if (fvp == tvp) {
787 		error = 0;
788 		goto out;
789 	}
790 
791 	/* If we need to move the directory between entries, lock the
792 	 * source so that we can safely operate on it. */
793 	if (fdvp != tdvp && fdvp != tvp) {
794 		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
795 			mp = tdvp->v_mount;
796 			error = vfs_busy(mp, 0);
797 			if (error != 0) {
798 				mp = NULL;
799 				goto out;
800 			}
801 			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
802 			    fcnp, tcnp);
803 			if (error != 0) {
804 				vfs_unbusy(mp);
805 				return (error);
806 			}
807 			ASSERT_VOP_ELOCKED(fdvp,
808 			    "tmpfs_rename: fdvp not locked");
809 			ASSERT_VOP_ELOCKED(tdvp,
810 			    "tmpfs_rename: tdvp not locked");
811 			if (tvp != NULL)
812 				ASSERT_VOP_ELOCKED(tvp,
813 				    "tmpfs_rename: tvp not locked");
814 			if (fvp == tvp) {
815 				error = 0;
816 				goto out_locked;
817 			}
818 		}
819 	}
820 
821 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
822 	tdnode = VP_TO_TMPFS_DIR(tdvp);
823 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
824 	fdnode = VP_TO_TMPFS_DIR(fdvp);
825 	fnode = VP_TO_TMPFS_NODE(fvp);
826 	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
827 
828 	/* Entry can disappear before we lock fdvp,
829 	 * also avoid manipulating '.' and '..' entries. */
830 	if (de == NULL) {
831 		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
832 		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
833 			error = EINVAL;
834 		else
835 			error = ENOENT;
836 		goto out_locked;
837 	}
838 	MPASS(de->td_node == fnode);
839 
840 	/* If re-naming a directory to another preexisting directory
841 	 * ensure that the target directory is empty so that its
842 	 * removal causes no side effects.
843 	 * Kern_rename guarantees the destination to be a directory
844 	 * if the source is one. */
845 	if (tvp != NULL) {
846 		MPASS(tnode != NULL);
847 
848 		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
849 		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
850 			error = EPERM;
851 			goto out_locked;
852 		}
853 
854 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
855 			if (tnode->tn_size > 0) {
856 				error = ENOTEMPTY;
857 				goto out_locked;
858 			}
859 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
860 			error = ENOTDIR;
861 			goto out_locked;
862 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
863 			error = EISDIR;
864 			goto out_locked;
865 		} else {
866 			MPASS(fnode->tn_type != VDIR &&
867 				tnode->tn_type != VDIR);
868 		}
869 	}
870 
871 	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
872 	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
873 		error = EPERM;
874 		goto out_locked;
875 	}
876 
877 	/* Ensure that we have enough memory to hold the new name, if it
878 	 * has to be changed. */
879 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
880 	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
881 		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
882 	} else
883 		newname = NULL;
884 
885 	/* If the node is being moved to another directory, we have to do
886 	 * the move. */
887 	if (fdnode != tdnode) {
888 		/* In case we are moving a directory, we have to adjust its
889 		 * parent to point to the new parent. */
890 		if (de->td_node->tn_type == VDIR) {
891 			struct tmpfs_node *n;
892 
893 			/* Ensure the target directory is not a child of the
894 			 * directory being moved.  Otherwise, we'd end up
895 			 * with stale nodes. */
896 			n = tdnode;
897 			/* TMPFS_LOCK garanties that no nodes are freed while
898 			 * traversing the list. Nodes can only be marked as
899 			 * removed: tn_parent == NULL. */
900 			TMPFS_LOCK(tmp);
901 			TMPFS_NODE_LOCK(n);
902 			while (n != n->tn_dir.tn_parent) {
903 				struct tmpfs_node *parent;
904 
905 				if (n == fnode) {
906 					TMPFS_NODE_UNLOCK(n);
907 					TMPFS_UNLOCK(tmp);
908 					error = EINVAL;
909 					if (newname != NULL)
910 						    free(newname, M_TMPFSNAME);
911 					goto out_locked;
912 				}
913 				parent = n->tn_dir.tn_parent;
914 				TMPFS_NODE_UNLOCK(n);
915 				if (parent == NULL) {
916 					n = NULL;
917 					break;
918 				}
919 				TMPFS_NODE_LOCK(parent);
920 				if (parent->tn_dir.tn_parent == NULL) {
921 					TMPFS_NODE_UNLOCK(parent);
922 					n = NULL;
923 					break;
924 				}
925 				n = parent;
926 			}
927 			TMPFS_UNLOCK(tmp);
928 			if (n == NULL) {
929 				error = EINVAL;
930 				if (newname != NULL)
931 					    free(newname, M_TMPFSNAME);
932 				goto out_locked;
933 			}
934 			TMPFS_NODE_UNLOCK(n);
935 
936 			/* Adjust the parent pointer. */
937 			TMPFS_VALIDATE_DIR(fnode);
938 			TMPFS_NODE_LOCK(de->td_node);
939 			de->td_node->tn_dir.tn_parent = tdnode;
940 			TMPFS_NODE_UNLOCK(de->td_node);
941 
942 			/* As a result of changing the target of the '..'
943 			 * entry, the link count of the source and target
944 			 * directories has to be adjusted. */
945 			TMPFS_NODE_LOCK(tdnode);
946 			TMPFS_ASSERT_LOCKED(tdnode);
947 			tdnode->tn_links++;
948 			TMPFS_NODE_UNLOCK(tdnode);
949 
950 			TMPFS_NODE_LOCK(fdnode);
951 			TMPFS_ASSERT_LOCKED(fdnode);
952 			fdnode->tn_links--;
953 			TMPFS_NODE_UNLOCK(fdnode);
954 		}
955 	}
956 
957 	/* Do the move: just remove the entry from the source directory
958 	 * and insert it into the target one. */
959 	tmpfs_dir_detach(fdvp, de);
960 
961 	if (fcnp->cn_flags & DOWHITEOUT)
962 		tmpfs_dir_whiteout_add(fdvp, fcnp);
963 	if (tcnp->cn_flags & ISWHITEOUT)
964 		tmpfs_dir_whiteout_remove(tdvp, tcnp);
965 
966 	/* If the name has changed, we need to make it effective by changing
967 	 * it in the directory entry. */
968 	if (newname != NULL) {
969 		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
970 
971 		free(de->ud.td_name, M_TMPFSNAME);
972 		de->ud.td_name = newname;
973 		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
974 
975 		fnode->tn_status |= TMPFS_NODE_CHANGED;
976 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
977 	}
978 
979 	/* If we are overwriting an entry, we have to remove the old one
980 	 * from the target directory. */
981 	if (tvp != NULL) {
982 		struct tmpfs_dirent *tde;
983 
984 		/* Remove the old entry from the target directory. */
985 		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
986 		tmpfs_dir_detach(tdvp, tde);
987 
988 		/* Free the directory entry we just deleted.  Note that the
989 		 * node referred by it will not be removed until the vnode is
990 		 * really reclaimed. */
991 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
992 	}
993 
994 	tmpfs_dir_attach(tdvp, de);
995 
996 	cache_purge(fvp);
997 	if (tvp != NULL)
998 		cache_purge(tvp);
999 	cache_purge_negative(tdvp);
1000 
1001 	error = 0;
1002 
1003 out_locked:
1004 	if (fdvp != tdvp && fdvp != tvp)
1005 		VOP_UNLOCK(fdvp, 0);
1006 
1007 out:
1008 	/* Release target nodes. */
1009 	/* XXX: I don't understand when tdvp can be the same as tvp, but
1010 	 * other code takes care of this... */
1011 	if (tdvp == tvp)
1012 		vrele(tdvp);
1013 	else
1014 		vput(tdvp);
1015 	if (tvp != NULL)
1016 		vput(tvp);
1017 
1018 	/* Release source nodes. */
1019 	vrele(fdvp);
1020 	vrele(fvp);
1021 
1022 	if (mp != NULL)
1023 		vfs_unbusy(mp);
1024 
1025 	return error;
1026 }
1027 
1028 static int
1029 tmpfs_mkdir(struct vop_mkdir_args *v)
1030 {
1031 	struct vnode *dvp = v->a_dvp;
1032 	struct vnode **vpp = v->a_vpp;
1033 	struct componentname *cnp = v->a_cnp;
1034 	struct vattr *vap = v->a_vap;
1035 
1036 	MPASS(vap->va_type == VDIR);
1037 
1038 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1039 }
1040 
1041 static int
1042 tmpfs_rmdir(struct vop_rmdir_args *v)
1043 {
1044 	struct vnode *dvp = v->a_dvp;
1045 	struct vnode *vp = v->a_vp;
1046 
1047 	int error;
1048 	struct tmpfs_dirent *de;
1049 	struct tmpfs_mount *tmp;
1050 	struct tmpfs_node *dnode;
1051 	struct tmpfs_node *node;
1052 
1053 	MPASS(VOP_ISLOCKED(dvp));
1054 	MPASS(VOP_ISLOCKED(vp));
1055 
1056 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1057 	dnode = VP_TO_TMPFS_DIR(dvp);
1058 	node = VP_TO_TMPFS_DIR(vp);
1059 
1060 	/* Directories with more than two entries ('.' and '..') cannot be
1061 	 * removed. */
1062 	 if (node->tn_size > 0) {
1063 		 error = ENOTEMPTY;
1064 		 goto out;
1065 	 }
1066 
1067 	if ((dnode->tn_flags & APPEND)
1068 	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1069 		error = EPERM;
1070 		goto out;
1071 	}
1072 
1073 	/* This invariant holds only if we are not trying to remove "..".
1074 	  * We checked for that above so this is safe now. */
1075 	MPASS(node->tn_dir.tn_parent == dnode);
1076 
1077 	/* Get the directory entry associated with node (vp).  This was
1078 	 * filled by tmpfs_lookup while looking up the entry. */
1079 	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1080 	MPASS(TMPFS_DIRENT_MATCHES(de,
1081 	    v->a_cnp->cn_nameptr,
1082 	    v->a_cnp->cn_namelen));
1083 
1084 	/* Check flags to see if we are allowed to remove the directory. */
1085 	if (dnode->tn_flags & APPEND
1086 		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1087 		error = EPERM;
1088 		goto out;
1089 	}
1090 
1091 
1092 	/* Detach the directory entry from the directory (dnode). */
1093 	tmpfs_dir_detach(dvp, de);
1094 	if (v->a_cnp->cn_flags & DOWHITEOUT)
1095 		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1096 
1097 	/* No vnode should be allocated for this entry from this point */
1098 	TMPFS_NODE_LOCK(node);
1099 	TMPFS_ASSERT_ELOCKED(node);
1100 	node->tn_links--;
1101 	node->tn_dir.tn_parent = NULL;
1102 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1103 	    TMPFS_NODE_MODIFIED;
1104 
1105 	TMPFS_NODE_UNLOCK(node);
1106 
1107 	TMPFS_NODE_LOCK(dnode);
1108 	TMPFS_ASSERT_ELOCKED(dnode);
1109 	dnode->tn_links--;
1110 	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1111 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1112 	TMPFS_NODE_UNLOCK(dnode);
1113 
1114 	cache_purge(dvp);
1115 	cache_purge(vp);
1116 
1117 	/* Free the directory entry we just deleted.  Note that the node
1118 	 * referred by it will not be removed until the vnode is really
1119 	 * reclaimed. */
1120 	tmpfs_free_dirent(tmp, de);
1121 
1122 	/* Release the deleted vnode (will destroy the node, notify
1123 	 * interested parties and clean it from the cache). */
1124 
1125 	dnode->tn_status |= TMPFS_NODE_CHANGED;
1126 	tmpfs_update(dvp);
1127 
1128 	error = 0;
1129 
1130 out:
1131 	return error;
1132 }
1133 
1134 static int
1135 tmpfs_symlink(struct vop_symlink_args *v)
1136 {
1137 	struct vnode *dvp = v->a_dvp;
1138 	struct vnode **vpp = v->a_vpp;
1139 	struct componentname *cnp = v->a_cnp;
1140 	struct vattr *vap = v->a_vap;
1141 	char *target = v->a_target;
1142 
1143 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1144 	MPASS(vap->va_type == VLNK);
1145 #else
1146 	vap->va_type = VLNK;
1147 #endif
1148 
1149 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1150 }
1151 
1152 static int
1153 tmpfs_readdir(struct vop_readdir_args *v)
1154 {
1155 	struct vnode *vp = v->a_vp;
1156 	struct uio *uio = v->a_uio;
1157 	int *eofflag = v->a_eofflag;
1158 	u_long **cookies = v->a_cookies;
1159 	int *ncookies = v->a_ncookies;
1160 
1161 	int error;
1162 	ssize_t startresid;
1163 	int maxcookies;
1164 	struct tmpfs_node *node;
1165 
1166 	/* This operation only makes sense on directory nodes. */
1167 	if (vp->v_type != VDIR)
1168 		return ENOTDIR;
1169 
1170 	maxcookies = 0;
1171 	node = VP_TO_TMPFS_DIR(vp);
1172 
1173 	startresid = uio->uio_resid;
1174 
1175 	/* Allocate cookies for NFS and compat modules. */
1176 	if (cookies != NULL && ncookies != NULL) {
1177 		maxcookies = howmany(node->tn_size,
1178 		    sizeof(struct tmpfs_dirent)) + 2;
1179 		*cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1180 		    M_WAITOK);
1181 		*ncookies = 0;
1182 	}
1183 
1184 	if (cookies == NULL)
1185 		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1186 	else
1187 		error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1188 		    ncookies);
1189 
1190 	/* Buffer was filled without hitting EOF. */
1191 	if (error == EJUSTRETURN)
1192 		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1193 
1194 	if (error != 0 && cookies != NULL && ncookies != NULL) {
1195 		free(*cookies, M_TEMP);
1196 		*cookies = NULL;
1197 		*ncookies = 0;
1198 	}
1199 
1200 	if (eofflag != NULL)
1201 		*eofflag =
1202 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1203 
1204 	return error;
1205 }
1206 
1207 static int
1208 tmpfs_readlink(struct vop_readlink_args *v)
1209 {
1210 	struct vnode *vp = v->a_vp;
1211 	struct uio *uio = v->a_uio;
1212 
1213 	int error;
1214 	struct tmpfs_node *node;
1215 
1216 	MPASS(uio->uio_offset == 0);
1217 	MPASS(vp->v_type == VLNK);
1218 
1219 	node = VP_TO_TMPFS_NODE(vp);
1220 
1221 	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1222 	    uio);
1223 	node->tn_status |= TMPFS_NODE_ACCESSED;
1224 
1225 	return error;
1226 }
1227 
1228 static int
1229 tmpfs_inactive(struct vop_inactive_args *v)
1230 {
1231 	struct vnode *vp;
1232 	struct tmpfs_node *node;
1233 
1234 	vp = v->a_vp;
1235 	node = VP_TO_TMPFS_NODE(vp);
1236 	if (node->tn_links == 0)
1237 		vrecycle(vp);
1238 	else
1239 		tmpfs_check_mtime(vp);
1240 	return (0);
1241 }
1242 
1243 int
1244 tmpfs_reclaim(struct vop_reclaim_args *v)
1245 {
1246 	struct vnode *vp = v->a_vp;
1247 
1248 	struct tmpfs_mount *tmp;
1249 	struct tmpfs_node *node;
1250 
1251 	node = VP_TO_TMPFS_NODE(vp);
1252 	tmp = VFS_TO_TMPFS(vp->v_mount);
1253 
1254 	if (vp->v_type == VREG)
1255 		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1256 	else
1257 		vnode_destroy_vobject(vp);
1258 	vp->v_object = NULL;
1259 	cache_purge(vp);
1260 
1261 	TMPFS_NODE_LOCK(node);
1262 	TMPFS_ASSERT_ELOCKED(node);
1263 	tmpfs_free_vp(vp);
1264 
1265 	/* If the node referenced by this vnode was deleted by the user,
1266 	 * we must free its associated data structures (now that the vnode
1267 	 * is being reclaimed). */
1268 	if (node->tn_links == 0 &&
1269 	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1270 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1271 		TMPFS_NODE_UNLOCK(node);
1272 		tmpfs_free_node(tmp, node);
1273 	} else
1274 		TMPFS_NODE_UNLOCK(node);
1275 
1276 	MPASS(vp->v_data == NULL);
1277 	return 0;
1278 }
1279 
1280 static int
1281 tmpfs_print(struct vop_print_args *v)
1282 {
1283 	struct vnode *vp = v->a_vp;
1284 
1285 	struct tmpfs_node *node;
1286 
1287 	node = VP_TO_TMPFS_NODE(vp);
1288 
1289 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1290 	    node, node->tn_flags, node->tn_links);
1291 	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1292 	    node->tn_mode, node->tn_uid, node->tn_gid,
1293 	    (intmax_t)node->tn_size, node->tn_status);
1294 
1295 	if (vp->v_type == VFIFO)
1296 		fifo_printinfo(vp);
1297 
1298 	printf("\n");
1299 
1300 	return 0;
1301 }
1302 
1303 static int
1304 tmpfs_pathconf(struct vop_pathconf_args *v)
1305 {
1306 	int name = v->a_name;
1307 	register_t *retval = v->a_retval;
1308 
1309 	int error;
1310 
1311 	error = 0;
1312 
1313 	switch (name) {
1314 	case _PC_LINK_MAX:
1315 		*retval = LINK_MAX;
1316 		break;
1317 
1318 	case _PC_NAME_MAX:
1319 		*retval = NAME_MAX;
1320 		break;
1321 
1322 	case _PC_PATH_MAX:
1323 		*retval = PATH_MAX;
1324 		break;
1325 
1326 	case _PC_PIPE_BUF:
1327 		*retval = PIPE_BUF;
1328 		break;
1329 
1330 	case _PC_CHOWN_RESTRICTED:
1331 		*retval = 1;
1332 		break;
1333 
1334 	case _PC_NO_TRUNC:
1335 		*retval = 1;
1336 		break;
1337 
1338 	case _PC_SYNC_IO:
1339 		*retval = 1;
1340 		break;
1341 
1342 	case _PC_FILESIZEBITS:
1343 		*retval = 0; /* XXX Don't know which value should I return. */
1344 		break;
1345 
1346 	default:
1347 		error = EINVAL;
1348 	}
1349 
1350 	return error;
1351 }
1352 
1353 static int
1354 tmpfs_vptofh(struct vop_vptofh_args *ap)
1355 {
1356 	struct tmpfs_fid *tfhp;
1357 	struct tmpfs_node *node;
1358 
1359 	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1360 	node = VP_TO_TMPFS_NODE(ap->a_vp);
1361 
1362 	tfhp->tf_len = sizeof(struct tmpfs_fid);
1363 	tfhp->tf_id = node->tn_id;
1364 	tfhp->tf_gen = node->tn_gen;
1365 
1366 	return (0);
1367 }
1368 
1369 static int
1370 tmpfs_whiteout(struct vop_whiteout_args *ap)
1371 {
1372 	struct vnode *dvp = ap->a_dvp;
1373 	struct componentname *cnp = ap->a_cnp;
1374 	struct tmpfs_dirent *de;
1375 
1376 	switch (ap->a_flags) {
1377 	case LOOKUP:
1378 		return (0);
1379 	case CREATE:
1380 		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1381 		if (de != NULL)
1382 			return (de->td_node == NULL ? 0 : EEXIST);
1383 		return (tmpfs_dir_whiteout_add(dvp, cnp));
1384 	case DELETE:
1385 		tmpfs_dir_whiteout_remove(dvp, cnp);
1386 		return (0);
1387 	default:
1388 		panic("tmpfs_whiteout: unknown op");
1389 	}
1390 }
1391 
1392 /*
1393  * vnode operations vector used for files stored in a tmpfs file system.
1394  */
1395 struct vop_vector tmpfs_vnodeop_entries = {
1396 	.vop_default =			&default_vnodeops,
1397 	.vop_lookup =			vfs_cache_lookup,
1398 	.vop_cachedlookup =		tmpfs_lookup,
1399 	.vop_create =			tmpfs_create,
1400 	.vop_mknod =			tmpfs_mknod,
1401 	.vop_open =			tmpfs_open,
1402 	.vop_close =			tmpfs_close,
1403 	.vop_access =			tmpfs_access,
1404 	.vop_getattr =			tmpfs_getattr,
1405 	.vop_setattr =			tmpfs_setattr,
1406 	.vop_read =			tmpfs_read,
1407 	.vop_write =			tmpfs_write,
1408 	.vop_fsync =			tmpfs_fsync,
1409 	.vop_remove =			tmpfs_remove,
1410 	.vop_link =			tmpfs_link,
1411 	.vop_rename =			tmpfs_rename,
1412 	.vop_mkdir =			tmpfs_mkdir,
1413 	.vop_rmdir =			tmpfs_rmdir,
1414 	.vop_symlink =			tmpfs_symlink,
1415 	.vop_readdir =			tmpfs_readdir,
1416 	.vop_readlink =			tmpfs_readlink,
1417 	.vop_inactive =			tmpfs_inactive,
1418 	.vop_reclaim =			tmpfs_reclaim,
1419 	.vop_print =			tmpfs_print,
1420 	.vop_pathconf =			tmpfs_pathconf,
1421 	.vop_vptofh =			tmpfs_vptofh,
1422 	.vop_whiteout =			tmpfs_whiteout,
1423 	.vop_bmap =			VOP_EOPNOTSUPP,
1424 };
1425 
1426