xref: /openbsd-src/sys/tmpfs/tmpfs_vnops.c (revision 8e0c768258d4632c51876b4397034bc3152bf8db)
1 /*	$OpenBSD: tmpfs_vnops.c,v 1.36 2019/08/05 08:35:59 anton Exp $	*/
2 /*	$NetBSD: tmpfs_vnops.c,v 1.100 2012/11/05 17:27:39 dholland Exp $	*/
3 
4 /*
5  * Copyright (c) 2005, 2006, 2007, 2012 The NetBSD Foundation, Inc.
6  * Copyright (c) 2013 Pedro Martelletto
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, and by Taylor R Campbell.
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 
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/event.h>
42 #include <sys/namei.h>
43 #include <sys/stat.h>
44 #include <sys/uio.h>
45 #include <sys/unistd.h>
46 #include <sys/vnode.h>
47 #include <sys/lockf.h>
48 #include <sys/poll.h>
49 #include <sys/file.h>
50 
51 #include <miscfs/fifofs/fifo.h>
52 #include <tmpfs/tmpfs_vnops.h>
53 #include <tmpfs/tmpfs.h>
54 
55 int tmpfs_kqfilter(void *v);
56 
57 /*
58  * vnode operations vector used for files stored in a tmpfs file system.
59  */
60 struct vops tmpfs_vops = {
61 	.vop_lookup	= tmpfs_lookup,
62 	.vop_create	= tmpfs_create,
63 	.vop_mknod	= tmpfs_mknod,
64 	.vop_open	= tmpfs_open,
65 	.vop_close	= tmpfs_close,
66 	.vop_access	= tmpfs_access,
67 	.vop_getattr	= tmpfs_getattr,
68 	.vop_setattr	= tmpfs_setattr,
69 	.vop_read	= tmpfs_read,
70 	.vop_write	= tmpfs_write,
71 	.vop_ioctl	= tmpfs_ioctl,
72 	.vop_poll	= tmpfs_poll,
73 	.vop_kqfilter	= tmpfs_kqfilter,
74 	.vop_revoke	= vop_generic_revoke,
75 	.vop_fsync	= tmpfs_fsync,
76 	.vop_remove	= tmpfs_remove,
77 	.vop_link	= tmpfs_link,
78 	.vop_rename	= tmpfs_rename,
79 	.vop_mkdir	= tmpfs_mkdir,
80 	.vop_rmdir	= tmpfs_rmdir,
81 	.vop_symlink	= tmpfs_symlink,
82 	.vop_readdir	= tmpfs_readdir,
83 	.vop_readlink	= tmpfs_readlink,
84 	.vop_abortop	= vop_generic_abortop,
85 	.vop_inactive	= tmpfs_inactive,
86 	.vop_reclaim	= tmpfs_reclaim,
87 	.vop_lock	= tmpfs_lock,
88 	.vop_unlock	= tmpfs_unlock,
89 	.vop_bmap	= vop_generic_bmap,
90 	.vop_strategy	= tmpfs_strategy,
91 	.vop_print	= tmpfs_print,
92 	.vop_islocked	= tmpfs_islocked,
93 	.vop_pathconf	= tmpfs_pathconf,
94 	.vop_advlock	= tmpfs_advlock,
95 	.vop_bwrite	= tmpfs_bwrite,
96 };
97 
98 /*
99  * tmpfs_lookup: path name traversal routine.
100  *
101  * Arguments: dvp (directory being searched), vpp (result),
102  * cnp (component name - path).
103  *
104  * => Caller holds a reference and lock on dvp.
105  * => We return looked-up vnode (vpp) locked, with a reference held.
106  */
107 int
108 tmpfs_lookup(void *v)
109 {
110 	struct vop_lookup_args /* {
111 		struct vnode *a_dvp;
112 		struct vnode **a_vpp;
113 		struct componentname *a_cnp;
114 	} */ *ap = v;
115 	struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
116 	struct componentname *cnp = ap->a_cnp;
117 	struct ucred *cred = cnp->cn_cred;
118 	const int lastcn = (cnp->cn_flags & ISLASTCN) != 0;
119 	const int lockparent = (cnp->cn_flags & LOCKPARENT) != 0;
120 	tmpfs_node_t *dnode, *tnode;
121 	tmpfs_dirent_t *de;
122 	int cachefound;
123 	int error;
124 
125 	KASSERT(VOP_ISLOCKED(dvp));
126 
127 	dnode = VP_TO_TMPFS_DIR(dvp);
128 	cnp->cn_flags &= ~PDIRUNLOCK;
129 	*vpp = NULL;
130 
131 	/* Check accessibility of directory. */
132 	error = VOP_ACCESS(dvp, VEXEC, cred, curproc);
133 	if (error) {
134 		goto out;
135 	}
136 
137 	/*
138 	 * If requesting the last path component on a read-only file system
139 	 * with a write operation, deny it.
140 	 */
141 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
142 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
143 		error = EROFS;
144 		goto out;
145 	}
146 
147 	/*
148 	 * Avoid doing a linear scan of the directory if the requested
149 	 * directory/name couple is already in the cache.
150 	 */
151 	cachefound = cache_lookup(dvp, vpp, cnp);
152 	if (cachefound == ENOENT /* && *vpp == NULLVP */)
153 		return ENOENT; /* Negative cache hit. */
154 	else if (cachefound != -1)
155 		return 0; /* Found in cache. */
156 
157 	if (cnp->cn_flags & ISDOTDOT) {
158 		tmpfs_node_t *pnode;
159 
160 		/*
161 		 * Lookup of ".." case.
162 		 */
163 		if (lastcn) {
164 			if (cnp->cn_nameiop == RENAME) {
165 				error = EINVAL;
166 				goto out;
167 			}
168 			if (cnp->cn_nameiop == DELETE) {
169 				/* Keep the name for tmpfs_rmdir(). */
170 				cnp->cn_flags |= SAVENAME;
171 			}
172 		}
173 		KASSERT(dnode->tn_type == VDIR);
174 		pnode = dnode->tn_spec.tn_dir.tn_parent;
175 		if (pnode == NULL) {
176 			error = ENOENT;
177 			goto out;
178 		}
179 
180 		/*
181 		 * Lock the parent tn_nlock before releasing the vnode lock,
182 		 * and thus prevents parent from disappearing.
183 		 */
184 		rw_enter_write(&pnode->tn_nlock);
185 		VOP_UNLOCK(dvp);
186 
187 		/*
188 		 * Get a vnode of the '..' entry and re-acquire the lock.
189 		 * Release the tn_nlock.
190 		 */
191 		error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
192 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
193 		goto out;
194 
195 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
196 		/*
197 		 * Lookup of "." case.
198 		 */
199 		if (lastcn && cnp->cn_nameiop == RENAME) {
200 			error = EISDIR;
201 			goto out;
202 		}
203 		vref(dvp);
204 		*vpp = dvp;
205 		error = 0;
206 		goto done;
207 	}
208 
209 	/*
210 	 * Other lookup cases: perform directory scan.
211 	 */
212 	de = tmpfs_dir_lookup(dnode, cnp);
213 	if (de == NULL) {
214 		/*
215 		 * The entry was not found in the directory.  This is valid
216 		 * if we are creating or renaming an entry and are working
217 		 * on the last component of the path name.
218 		 */
219 		if (lastcn && (cnp->cn_nameiop == CREATE ||
220 		    cnp->cn_nameiop == RENAME)) {
221 			error = VOP_ACCESS(dvp, VWRITE, cred, curproc);
222 			if (error) {
223 				goto out;
224 			}
225 			/*
226 			 * We are creating an entry in the file system, so
227 			 * save its name for further use by tmpfs_create().
228 			 */
229 			cnp->cn_flags |= SAVENAME;
230 			error = EJUSTRETURN;
231 		} else {
232 			error = ENOENT;
233 		}
234 		goto done;
235 	}
236 
237 	tnode = de->td_node;
238 
239 	/*
240 	 * If it is not the last path component and found a non-directory
241 	 * or non-link entry (which may itself be pointing to a directory),
242 	 * raise an error.
243 	 */
244 	if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
245 		error = ENOTDIR;
246 		goto out;
247 	}
248 
249 	/* Check the permissions. */
250 	if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
251 		error = VOP_ACCESS(dvp, VWRITE, cred, curproc);
252 		if (error)
253 			goto out;
254 
255 		/*
256 		 * If not root and directory is sticky, check for permission
257 		 * on directory or on file. This implements append-only
258 		 * directories.
259 		 */
260 		if ((dnode->tn_mode & S_ISTXT) != 0) {
261 			if (cred->cr_uid != 0 &&
262 			    cred->cr_uid != dnode->tn_uid &&
263 			    cred->cr_uid != tnode->tn_uid) {
264 				error = EPERM;
265 				goto out;
266 			}
267 		}
268 
269 		/*
270 		 * XXX pedro: We might need cn_nameptr later in tmpfs_remove()
271 		 * or tmpfs_rmdir() for a tmpfs_dir_lookup(). We should really
272 		 * get rid of SAVENAME at some point.
273 		 */
274 		if (cnp->cn_nameiop == DELETE)
275 			cnp->cn_flags |= SAVENAME;
276 	}
277 
278 	/* Get a vnode for the matching entry. */
279 	rw_enter_write(&tnode->tn_nlock);
280 	error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
281 done:
282 	/*
283 	 * Cache the result, unless request was for creation (as it does
284 	 * not improve the performance).
285 	 */
286 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
287 		cache_enter(dvp, *vpp, cnp);
288 	}
289 out:
290 	/*
291 	 * If (1) we succeded, (2) found a distinct vnode to return and (3) were
292 	 * either explicitly told to keep the parent locked or are in the
293 	 * middle of a lookup, unlock the parent vnode.
294 	 */
295 	if ((error == 0 || error == EJUSTRETURN) && /* (1) */
296 	    *vpp != dvp &&			    /* (2) */
297 	    (!lockparent || !lastcn)) {		    /* (3) */
298 		VOP_UNLOCK(dvp);
299 		cnp->cn_flags |= PDIRUNLOCK;
300 	} else
301 		KASSERT(VOP_ISLOCKED(dvp));
302 
303 	KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
304 
305 	return error;
306 }
307 
308 int
309 tmpfs_create(void *v)
310 {
311 	struct vop_create_args /* {
312 		struct vnode		*a_dvp;
313 		struct vnode		**a_vpp;
314 		struct componentname	*a_cnp;
315 		struct vattr		*a_vap;
316 	} */ *ap = v;
317 	struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
318 	struct componentname *cnp = ap->a_cnp;
319 	struct vattr *vap = ap->a_vap;
320 
321 	KASSERT(VOP_ISLOCKED(dvp));
322 	KASSERT(cnp->cn_flags & HASBUF);
323 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
324 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
325 }
326 
327 int
328 tmpfs_mknod(void *v)
329 {
330 	struct vop_mknod_args /* {
331 		struct vnode		*a_dvp;
332 		struct vnode		**a_vpp;
333 		struct componentname	*a_cnp;
334 		struct vattr		*a_vap;
335 	} */ *ap = v;
336 	struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
337 	struct componentname *cnp = ap->a_cnp;
338 	struct vattr *vap = ap->a_vap;
339 	enum vtype vt = vap->va_type;
340 	int error;
341 
342 	if (vt != VBLK && vt != VCHR && vt != VFIFO)
343 		return EINVAL;
344 
345 	error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
346 
347 	if (error == 0)
348 		vput(*vpp);
349 
350 	return error;
351 }
352 
353 int
354 tmpfs_open(void *v)
355 {
356 	struct vop_open_args /* {
357 		struct vnode	*a_vp;
358 		int		a_mode;
359 		kauth_cred_t	a_cred;
360 	} */ *ap = v;
361 	struct vnode *vp = ap->a_vp;
362 	mode_t mode = ap->a_mode;
363 	tmpfs_node_t *node;
364 
365 	KASSERT(VOP_ISLOCKED(vp));
366 
367 	node = VP_TO_TMPFS_NODE(vp);
368 	if (node->tn_links < 1) {
369 		/*
370 		 * The file is still active, but all its names have been
371 		 * removed (e.g. by a "rmdir $(pwd)").  It cannot be opened
372 		 * any more, as it is about to be destroyed.
373 		 */
374 		return ENOENT;
375 	}
376 
377 	/* If the file is marked append-only, deny write requests. */
378 	if ((node->tn_flags & APPEND) != 0 &&
379 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
380 		return EPERM;
381 	}
382 	return 0;
383 }
384 
385 int
386 tmpfs_close(void *v)
387 {
388 #ifdef DIAGNOSTIC
389 	struct vop_close_args /* {
390 		struct vnode	*a_vp;
391 		int		a_fflag;
392 		kauth_cred_t	a_cred;
393 	} */ *ap = v;
394 	struct vnode *vp = ap->a_vp;
395 
396 	KASSERT(VOP_ISLOCKED(vp));
397 #endif
398 	return 0;
399 }
400 
401 int
402 tmpfs_access(void *v)
403 {
404 	struct vop_access_args /* {
405 		struct vnode	*a_vp;
406 		int		a_mode;
407 		kauth_cred_t	a_cred;
408 	} */ *ap = v;
409 	struct vnode *vp = ap->a_vp;
410 	mode_t mode = ap->a_mode;
411 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
412 	const int writing = (mode & VWRITE) != 0;
413 
414 	KASSERT(VOP_ISLOCKED(vp));
415 
416 	/* Possible? */
417 	switch (vp->v_type) {
418 	case VDIR:
419 	case VLNK:
420 	case VREG:
421 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
422 			return EROFS;
423 		}
424 		break;
425 	case VBLK:
426 	case VCHR:
427 	case VSOCK:
428 	case VFIFO:
429 		break;
430 	default:
431 		return EINVAL;
432 	}
433 	if (writing && (node->tn_flags & IMMUTABLE) != 0) {
434 		return EPERM;
435 	}
436 
437 	return (vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
438 	    mode, ap->a_cred));
439 }
440 
441 int
442 tmpfs_getattr(void *v)
443 {
444 	struct vop_getattr_args /* {
445 		struct vnode	*a_vp;
446 		struct vattr	*a_vap;
447 		kauth_cred_t	a_cred;
448 	} */ *ap = v;
449 	struct vnode *vp = ap->a_vp;
450 	struct vattr *vap = ap->a_vap;
451 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
452 
453 	vattr_null(vap);
454 
455 	vap->va_type = vp->v_type;
456 	vap->va_mode = node->tn_mode;
457 	vap->va_nlink = node->tn_links;
458 	vap->va_uid = node->tn_uid;
459 	vap->va_gid = node->tn_gid;
460 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
461 	vap->va_fileid = node->tn_id;
462 	vap->va_size = node->tn_size;
463 	vap->va_blocksize = PAGE_SIZE;
464 	vap->va_atime = node->tn_atime;
465 	vap->va_mtime = node->tn_mtime;
466 	vap->va_ctime = node->tn_ctime;
467 	/* vap->va_birthtime = node->tn_birthtime; */
468 	vap->va_gen = TMPFS_NODE_GEN(node);
469 	vap->va_flags = node->tn_flags;
470 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
471 	    node->tn_spec.tn_dev.tn_rdev : VNOVAL;
472 	vap->va_bytes = round_page(node->tn_size);
473 	vap->va_filerev = VNOVAL;
474 	vap->va_vaflags = 0;
475 	vap->va_spare = VNOVAL; /* XXX */
476 
477 	return 0;
478 }
479 
480 #define GOODTIME(tv)	((tv)->tv_nsec != VNOVAL)
481 /* XXX Should this operation be atomic?  I think it should, but code in
482  * XXX other places (e.g., ufs) doesn't seem to be... */
483 int
484 tmpfs_setattr(void *v)
485 {
486 	struct vop_setattr_args /* {
487 		struct vnode	*a_vp;
488 		struct vattr	*a_vap;
489 		kauth_cred_t	a_cred;
490 	} */ *ap = v;
491 	struct vnode *vp = ap->a_vp;
492 	struct vattr *vap = ap->a_vap;
493 	struct ucred *cred = ap->a_cred;
494 	struct proc *p = curproc;
495 	int error = 0;
496 
497 	KASSERT(VOP_ISLOCKED(vp));
498 
499 	/* Abort if any unsettable attribute is given. */
500 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
501 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
502 	    vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
503 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
504 	    vap->va_bytes != VNOVAL) {
505 		return EINVAL;
506 	}
507 	if (error == 0 && (vap->va_flags != VNOVAL))
508 		error = tmpfs_chflags(vp, vap->va_flags, cred, p);
509 
510 	if (error == 0 && (vap->va_size != VNOVAL))
511 		error = tmpfs_chsize(vp, vap->va_size, cred, p);
512 
513 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
514 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
515 
516 	if (error == 0 && (vap->va_mode != VNOVAL))
517 		error = tmpfs_chmod(vp, vap->va_mode, cred, p);
518 
519 	if (error == 0 && ((vap->va_vaflags & VA_UTIMES_CHANGE)
520 	    || GOODTIME(&vap->va_atime)
521 	    || GOODTIME(&vap->va_mtime)))
522 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
523 		    vap->va_vaflags, cred, p);
524 
525 	return error;
526 }
527 
528 int
529 tmpfs_read(void *v)
530 {
531 	struct vop_read_args /* {
532 		struct vnode *a_vp;
533 		struct uio *a_uio;
534 		int a_ioflag;
535 		struct ucred *a_cred;
536 	} */ *ap = v;
537 	struct vnode *vp = ap->a_vp;
538 	struct uio *uio = ap->a_uio;
539 	/* const int ioflag = ap->a_ioflag; */
540 	tmpfs_node_t *node;
541 	int error;
542 
543 	KASSERT(VOP_ISLOCKED(vp));
544 
545 	if (vp->v_type != VREG) {
546 		return EISDIR;
547 	}
548 	if (uio->uio_offset < 0) {
549 		return EINVAL;
550 	}
551 	if (uio->uio_resid == 0)
552 		return 0;
553 
554 	node = VP_TO_TMPFS_NODE(vp);
555 	error = 0;
556 
557 	while (error == 0 && uio->uio_resid > 0) {
558 		vsize_t len;
559 
560 		if (node->tn_size <= uio->uio_offset) {
561 			break;
562 		}
563 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
564 		if (len == 0) {
565 			break;
566 		}
567 		error = tmpfs_uiomove(node, uio, len);
568 	}
569 
570 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
571 		tmpfs_update(node, TMPFS_NODE_ACCESSED);
572 
573 	return error;
574 }
575 
576 int
577 tmpfs_write(void *v)
578 {
579 	struct vop_write_args /* {
580 		struct vnode	*a_vp;
581 		struct uio	*a_uio;
582 		int		a_ioflag;
583 		kauth_cred_t	a_cred;
584 	} */ *ap = v;
585 	struct vnode *vp = ap->a_vp;
586 	struct uio *uio = ap->a_uio;
587 	const int ioflag = ap->a_ioflag;
588 	tmpfs_node_t *node;
589 	off_t oldsize;
590 	ssize_t overrun;
591 	int extended;
592 	int error;
593 
594 	KASSERT(VOP_ISLOCKED(vp));
595 
596 	node = VP_TO_TMPFS_NODE(vp);
597 	oldsize = node->tn_size;
598 
599 	if (vp->v_type != VREG)
600 		return (EINVAL);
601 
602 	if (uio->uio_resid == 0)
603 		return (0);
604 
605 	if (ioflag & IO_APPEND) {
606 		uio->uio_offset = node->tn_size;
607 	}
608 
609 	if (uio->uio_offset < 0 ||
610 	    (u_int64_t)uio->uio_offset + uio->uio_resid > LLONG_MAX)
611 		return (EFBIG);
612 
613 	/* do the filesize rlimit check */
614 	if ((error = vn_fsizechk(vp, uio, ioflag, &overrun)))
615 		return (error);
616 
617 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
618 	if (extended) {
619 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
620 		if (error)
621 			goto out;
622 	}
623 
624 	error = 0;
625 	while (error == 0 && uio->uio_resid > 0) {
626 		vsize_t len;
627 
628 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
629 		if (len == 0) {
630 			break;
631 		}
632 		error = tmpfs_uiomove(node, uio, len);
633 	}
634 	if (error) {
635 		(void)tmpfs_reg_resize(vp, oldsize);
636 	}
637 
638 	tmpfs_update(node, TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
639 	if (extended)
640 		VN_KNOTE(vp, NOTE_WRITE | NOTE_EXTEND);
641 	else
642 		VN_KNOTE(vp, NOTE_WRITE);
643 out:
644 	if (error) {
645 		KASSERT(oldsize == node->tn_size);
646 	} else {
647 		KASSERT(uio->uio_resid == 0);
648 
649 		/* correct the result for writes clamped by vn_fsizechk() */
650 		uio->uio_resid += overrun;
651 
652 	}
653 	return error;
654 }
655 
656 int
657 tmpfs_fsync(void *v)
658 {
659 #ifdef DIAGNOSTIC
660 	struct vop_fsync_args /* {
661 		struct vnode *a_vp;
662 		struct ucred *a_cred;
663 		int a_flags;
664 		off_t a_offlo;
665 		off_t a_offhi;
666 		struct lwp *a_l;
667 	} */ *ap = v;
668 	struct vnode *vp = ap->a_vp;
669 
670 	/* Nothing to do.  Just update. */
671 	KASSERT(VOP_ISLOCKED(vp));
672 #endif
673 	return 0;
674 }
675 
676 /*
677  * tmpfs_remove: unlink a file.
678  *
679  * => Both directory (dvp) and file (vp) are locked.
680  * => We unlock and drop the reference on both.
681  */
682 int
683 tmpfs_remove(void *v)
684 {
685 	struct vop_remove_args /* {
686 		struct vnode *a_dvp;
687 		struct vnode *a_vp;
688 		struct componentname *a_cnp;
689 	} */ *ap = v;
690 	struct vnode *dvp = ap->a_dvp, *vp = ap->a_vp;
691 	struct componentname *cnp = ap->a_cnp;
692 	tmpfs_node_t *dnode, *node;
693 	tmpfs_dirent_t *de;
694 	int error;
695 
696 	KASSERT(VOP_ISLOCKED(dvp));
697 	KASSERT(VOP_ISLOCKED(vp));
698 	KASSERT(cnp->cn_flags & HASBUF);
699 
700 	if (vp->v_type == VDIR) {
701 		error = EPERM;
702 		goto out;
703 	}
704 
705 	dnode = VP_TO_TMPFS_NODE(dvp);
706 	node = VP_TO_TMPFS_NODE(vp);
707 
708 	/* Files marked as immutable or append-only cannot be deleted. */
709 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
710 		error = EPERM;
711 		goto out;
712 	}
713 
714 	/*
715 	 * Likewise, files residing on directories marked as append-only cannot
716 	 * be deleted.
717 	 */
718 	if (dnode->tn_flags & APPEND) {
719 		error = EPERM;
720 		goto out;
721 	}
722 
723 	/* Lookup the directory entry (check the cached hint first). */
724 	de = tmpfs_dir_cached(node);
725 	if (de == NULL) {
726 		de = tmpfs_dir_lookup(dnode, cnp);
727 	}
728 
729 	KASSERT(de && de->td_node == node);
730 
731 	/*
732 	 * Remove the entry from the directory (drops the link count) and
733 	 * destroy it.
734 	 * Note: the inode referred by it will not be destroyed
735 	 * until the vnode is reclaimed/recycled.
736 	 */
737 	tmpfs_dir_detach(dnode, de);
738 	tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
739 	if (node->tn_links > 0)  {
740 		/* We removed a hard link. */
741 		tmpfs_update(node, TMPFS_NODE_CHANGED);
742 	}
743 	error = 0;
744 out:
745 	pool_put(&namei_pool, cnp->cn_pnbuf);
746 	/* Drop the references and unlock the vnodes. */
747 	vput(vp);
748 	if (dvp == vp) {
749 		vrele(dvp);
750 	} else {
751 		vput(dvp);
752 	}
753 	return error;
754 }
755 
756 /*
757  * tmpfs_link: create a hard link.
758  */
759 int
760 tmpfs_link(void *v)
761 {
762 	struct vop_link_args /* {
763 		struct vnode *a_dvp;
764 		struct vnode *a_vp;
765 		struct componentname *a_cnp;
766 	} */ *ap = v;
767 	struct vnode *dvp = ap->a_dvp;
768 	struct vnode *vp = ap->a_vp;
769 	struct componentname *cnp = ap->a_cnp;
770 	tmpfs_node_t *dnode, *node;
771 	tmpfs_dirent_t *de;
772 	int error;
773 
774 	KASSERT(VOP_ISLOCKED(dvp));
775 
776 	if (vp->v_type == VDIR) {
777 		VOP_ABORTOP(dvp, cnp);
778 		vput(dvp);
779 		return EPERM;
780 	}
781 
782 	KASSERT(dvp != vp);
783 
784 	if (dvp->v_mount != vp->v_mount) {
785 		VOP_ABORTOP(dvp, cnp);
786 		vput(dvp);
787 		return EXDEV;
788 	}
789 
790 	dnode = VP_TO_TMPFS_DIR(dvp);
791 	node = VP_TO_TMPFS_NODE(vp);
792 
793 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
794 
795 	/* Check for maximum number of links limit. */
796 	if (node->tn_links == LINK_MAX) {
797 		error = EMLINK;
798 		goto out;
799 	}
800 	KASSERT(node->tn_links < LINK_MAX);
801 
802 	/* We cannot create links of files marked immutable or append-only. */
803 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
804 		error = EPERM;
805 		goto out;
806 	}
807 
808 	if (TMPFS_DIRSEQ_FULL(dnode)) {
809 		error = ENOSPC;
810 		goto out;
811 	}
812 
813 	/* Allocate a new directory entry to represent the inode. */
814 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
815 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
816 	if (error) {
817 		goto out;
818 	}
819 
820 	/*
821 	 * Insert the entry into the directory.
822 	 * It will increase the inode link count.
823 	 */
824 	tmpfs_dir_attach(dnode, de, node);
825 
826 	/* Update the timestamps and trigger the event. */
827 	if (node->tn_vnode) {
828 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
829 	}
830 	tmpfs_update(node, TMPFS_NODE_CHANGED);
831 	error = 0;
832 out:
833 	pool_put(&namei_pool, cnp->cn_pnbuf);
834 	VOP_UNLOCK(vp);
835 	vput(dvp);
836 	return error;
837 }
838 
839 int
840 tmpfs_mkdir(void *v)
841 {
842 	struct vop_mkdir_args /* {
843 		struct vnode		*a_dvp;
844 		struct vnode		**a_vpp;
845 		struct componentname	*a_cnp;
846 		struct vattr		*a_vap;
847 	} */ *ap = v;
848 	struct vnode *dvp = ap->a_dvp;
849 	struct vnode **vpp = ap->a_vpp;
850 	struct componentname *cnp = ap->a_cnp;
851 	struct vattr *vap = ap->a_vap;
852 	int error;
853 
854 	KASSERT(vap->va_type == VDIR);
855 	error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
856 	vput(dvp);
857 	return error;
858 }
859 
860 int
861 tmpfs_rmdir(void *v)
862 {
863 	struct vop_rmdir_args /* {
864 		struct vnode		*a_dvp;
865 		struct vnode		*a_vp;
866 		struct componentname	*a_cnp;
867 	} */ *ap = v;
868 	struct vnode *dvp = ap->a_dvp;
869 	struct vnode *vp = ap->a_vp;
870 	struct componentname *cnp = ap->a_cnp;
871 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
872 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
873 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
874 	tmpfs_dirent_t *de;
875 	int error = 0;
876 
877 	KASSERT(VOP_ISLOCKED(dvp));
878 	KASSERT(VOP_ISLOCKED(vp));
879 	KASSERT(cnp->cn_flags & HASBUF);
880 
881 	if (cnp->cn_namelen == 2 && cnp->cn_nameptr[0] == '.' &&
882 	    cnp->cn_nameptr[1] == '.') {
883 		error = ENOTEMPTY;
884 		goto out;
885 	}
886 
887 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
888 
889 	/*
890 	 * Directories with more than two entries ('.' and '..') cannot be
891 	 * removed.
892 	 */
893 	if (node->tn_size > 0) {
894 		KASSERT(error == 0);
895 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
896 			error = ENOTEMPTY;
897 			break;
898 		}
899 		if (error)
900 			goto out;
901 	}
902 
903 	/* Lookup the directory entry (check the cached hint first). */
904 	de = tmpfs_dir_cached(node);
905 	if (de == NULL)
906 		de = tmpfs_dir_lookup(dnode, cnp);
907 
908 	KASSERT(de && de->td_node == node);
909 
910 	/* Check flags to see if we are allowed to remove the directory. */
911 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
912 		error = EPERM;
913 		goto out;
914 	}
915 
916 	/* Decrement the link count for the virtual '.' entry. */
917 	node->tn_links--;
918 	tmpfs_update(node, TMPFS_NODE_STATUSALL);
919 
920 	/* Detach the directory entry from the directory. */
921 	tmpfs_dir_detach(dnode, de);
922 
923 	/* Purge the cache for parent. */
924 	cache_purge(dvp);
925 
926 	/*
927 	 * Destroy the directory entry.
928 	 * Note: the inode referred by it will not be destroyed
929 	 * until the vnode is reclaimed.
930 	 */
931 	tmpfs_free_dirent(tmp, de);
932 	KASSERT(TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir) == NULL);
933 
934 	KASSERT(node->tn_links == 0);
935 out:
936 	pool_put(&namei_pool, cnp->cn_pnbuf);
937 	/* Release the nodes. */
938 	vput(dvp);
939 	vput(vp);
940 	return error;
941 }
942 
943 int
944 tmpfs_symlink(void *v)
945 {
946 	struct vop_symlink_args /* {
947 		struct vnode		*a_dvp;
948 		struct vnode		**a_vpp;
949 		struct componentname	*a_cnp;
950 		struct vattr		*a_vap;
951 		char			*a_target;
952 	} */ *ap = v;
953 	struct vnode *dvp = ap->a_dvp;
954 	struct vnode **vpp = ap->a_vpp;
955 	struct componentname *cnp = ap->a_cnp;
956 	struct vattr *vap = ap->a_vap;
957 	char *target = ap->a_target;
958 	int error;
959 
960 	KASSERT(vap->va_type == 0);
961 	vap->va_type = VLNK;
962 
963 	error = tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
964 	vput(dvp);
965 	if (error == 0)
966 		vput(*vpp);
967 
968 	return error;
969 }
970 
971 int
972 tmpfs_readdir(void *v)
973 {
974 	struct vop_readdir_args /* {
975 		struct vnode	*a_vp;
976 		struct uio	*a_uio;
977 		kauth_cred_t	a_cred;
978 		int		*a_eofflag;
979 	} */ *ap = v;
980 	struct vnode *vp = ap->a_vp;
981 	struct uio *uio = ap->a_uio;
982 	int *eofflag = ap->a_eofflag;
983 	tmpfs_node_t *node;
984 	int error;
985 
986 	KASSERT(VOP_ISLOCKED(vp));
987 
988 	/* This operation only makes sense on directory nodes. */
989 	if (vp->v_type != VDIR) {
990 		return ENOTDIR;
991 	}
992 	node = VP_TO_TMPFS_DIR(vp);
993 	/*
994 	 * Retrieve the directory entries, unless it is being destroyed.
995 	 */
996 	if (node->tn_links) {
997 		error = tmpfs_dir_getdents(node, uio);
998 	} else {
999 		error = 0;
1000 	}
1001 
1002 	if (eofflag != NULL) {
1003 		*eofflag = !error && uio->uio_offset == TMPFS_DIRSEQ_EOF;
1004 	}
1005 	return error;
1006 }
1007 
1008 int
1009 tmpfs_readlink(void *v)
1010 {
1011 	struct vop_readlink_args /* {
1012 		struct vnode	*a_vp;
1013 		struct uio	*a_uio;
1014 		kauth_cred_t	a_cred;
1015 	} */ *ap = v;
1016 	struct vnode *vp = ap->a_vp;
1017 	struct uio *uio = ap->a_uio;
1018 	tmpfs_node_t *node;
1019 	int error;
1020 
1021 	KASSERT(VOP_ISLOCKED(vp));
1022 	KASSERT(uio->uio_offset == 0);
1023 	KASSERT(vp->v_type == VLNK);
1024 
1025 	node = VP_TO_TMPFS_NODE(vp);
1026 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
1027 	    MIN((size_t)node->tn_size, uio->uio_resid), uio);
1028 
1029 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
1030 		tmpfs_update(node, TMPFS_NODE_ACCESSED);
1031 
1032 	return error;
1033 }
1034 
1035 int
1036 tmpfs_inactive(void *v)
1037 {
1038 	struct vop_inactive_args /* {
1039 		struct vnode *a_vp;
1040 		int *a_recycle;
1041 	} */ *ap = v;
1042 	struct vnode *vp = ap->a_vp;
1043 	tmpfs_node_t *node;
1044 
1045 	KASSERT(VOP_ISLOCKED(vp));
1046 
1047 	node = VP_TO_TMPFS_NODE(vp);
1048 
1049 	if (vp->v_type == VREG && tmpfs_uio_cached(node))
1050 		tmpfs_uio_uncache(node);
1051 
1052 	VOP_UNLOCK(vp);
1053 
1054 	/*
1055 	 * If we are done with the node, reclaim it so that it can be reused
1056 	 * immediately.
1057 	 */
1058 	if (node->tn_links == 0)
1059 		vrecycle(vp, curproc);
1060 
1061 	return 0;
1062 }
1063 
1064 int
1065 tmpfs_reclaim(void *v)
1066 {
1067 	struct vop_reclaim_args /* {
1068 		struct vnode *a_vp;
1069 	} */ *ap = v;
1070 	struct vnode *vp = ap->a_vp;
1071 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
1072 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1073 	int racing;
1074 
1075 	/* Disassociate inode from vnode. */
1076 	rw_enter_write(&node->tn_nlock);
1077 	node->tn_vnode = NULL;
1078 	vp->v_data = NULL;
1079 	/* Check if tmpfs_vnode_get() is racing with us. */
1080 	racing = TMPFS_NODE_RECLAIMING(node);
1081 	rw_exit_write(&node->tn_nlock);
1082 
1083 	/*
1084 	 * If inode is not referenced, i.e. no links, then destroy it.
1085 	 * Note: if racing - inode is about to get a new vnode, leave it.
1086 	 */
1087 	if (node->tn_links == 0 && !racing) {
1088 		tmpfs_free_node(tmp, node);
1089 	}
1090 	return 0;
1091 }
1092 
1093 int
1094 tmpfs_pathconf(void *v)
1095 {
1096 	struct vop_pathconf_args /* {
1097 		struct vnode	*a_vp;
1098 		int		a_name;
1099 		register_t	*a_retval;
1100 	} */ *ap = v;
1101 	const int name = ap->a_name;
1102 	register_t *retval = ap->a_retval;
1103 	int error = 0;
1104 
1105 	switch (name) {
1106 	case _PC_LINK_MAX:
1107 		*retval = LINK_MAX;
1108 		break;
1109 	case _PC_NAME_MAX:
1110 		*retval = TMPFS_MAXNAMLEN;
1111 		break;
1112 	case _PC_CHOWN_RESTRICTED:
1113 		*retval = 1;
1114 		break;
1115 	case _PC_NO_TRUNC:
1116 		*retval = 1;
1117 		break;
1118 	case _PC_FILESIZEBITS:
1119 		*retval = 64;
1120 		break;
1121 	case _PC_TIMESTAMP_RESOLUTION:
1122 		*retval = 1;
1123 		break;
1124 	default:
1125 		error = EINVAL;
1126 	}
1127 	return error;
1128 }
1129 
1130 int
1131 tmpfs_advlock(void *v)
1132 {
1133 	struct vop_advlock_args /* {
1134 		struct vnode	*a_vp;
1135 		void *		a_id;
1136 		int		a_op;
1137 		struct flock	*a_fl;
1138 		int		a_flags;
1139 	} */ *ap = v;
1140 	struct vnode *vp = ap->a_vp;
1141 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1142 
1143 	return lf_advlock(&node->tn_lockf, node->tn_size, ap->a_id, ap->a_op,
1144 	    ap->a_fl, ap->a_flags);
1145 }
1146 
1147 int
1148 tmpfs_print(void *v)
1149 {
1150 	struct vop_print_args /* {
1151 		struct vnode	*a_vp;
1152 	} */ *ap = v;
1153 	struct vnode *vp = ap->a_vp;
1154 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1155 
1156 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
1157 	    "\tmode 0%o, owner %d, group %d, size %lld",
1158 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
1159 	    node->tn_gid, node->tn_size);
1160 #ifdef FIFO
1161 	if (vp->v_type == VFIFO)
1162 		fifo_printinfo(vp);
1163 #endif
1164 	printf("\n");
1165 	return 0;
1166 }
1167 
1168 /* a null op */
1169 int
1170 tmpfs_bwrite(void *v)
1171 {
1172 	return 0;
1173 }
1174 
1175 int
1176 tmpfs_poll(void *v)
1177 {
1178 	struct vop_poll_args *ap = v;
1179 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1180 }
1181 
1182 int
1183 tmpfs_strategy(void *v)
1184 {
1185 	return EOPNOTSUPP;
1186 }
1187 
1188 int
1189 tmpfs_ioctl(void *v)
1190 {
1191 	return ENOTTY;
1192 }
1193 
1194 int
1195 tmpfs_lock(void *v)
1196 {
1197 	struct vop_lock_args *ap = v;
1198 	tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp);
1199 
1200 	return rrw_enter(&tnp->tn_vlock, ap->a_flags & LK_RWFLAGS);
1201 }
1202 
1203 int
1204 tmpfs_unlock(void *v)
1205 {
1206 	struct vop_unlock_args *ap = v;
1207 	tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp);
1208 
1209 	rrw_exit(&tnp->tn_vlock);
1210 	return 0;
1211 }
1212 
1213 int
1214 tmpfs_islocked(void *v)
1215 {
1216 	struct vop_islocked_args *ap = v;
1217 	tmpfs_node_t *tnp = VP_TO_TMPFS_NODE(ap->a_vp);
1218 
1219 	return rrw_status(&tnp->tn_vlock);
1220 }
1221 
1222 /*
1223  * tmpfs_rename: rename routine, the hairiest system call, with the
1224  * insane API.
1225  *
1226  * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
1227  * and tvp (to-leaf), if exists (NULL if not).
1228  *
1229  * => Caller holds a reference on fdvp and fvp, they are unlocked.
1230  *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
1231  *
1232  * => Both tdvp and tvp are referenced and locked.  It is our responsibility
1233  *    to release the references and unlock them (or destroy).
1234  */
1235 
1236 /*
1237  * First, some forward declarations of subroutines.
1238  */
1239 
1240 int tmpfs_sane_rename(struct vnode *, struct componentname *,
1241     struct vnode *, struct componentname *, struct ucred *, int);
1242 int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *,
1243     struct ucred *,
1244     struct vnode *, struct tmpfs_node *, struct componentname *,
1245     struct tmpfs_dirent **, struct vnode **,
1246     struct vnode *, struct tmpfs_node *, struct componentname *,
1247     struct tmpfs_dirent **, struct vnode **);
1248 int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *,
1249     struct ucred *,
1250     struct vnode *, struct tmpfs_node *,
1251     struct componentname *, struct tmpfs_dirent **, struct vnode **,
1252     struct componentname *, struct tmpfs_dirent **, struct vnode **);
1253 int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *,
1254     struct ucred *,
1255     struct vnode *, struct tmpfs_node *, struct componentname *,
1256     struct tmpfs_dirent **, struct vnode **,
1257     struct vnode *, struct tmpfs_node *, struct componentname *,
1258     struct tmpfs_dirent **, struct vnode **);
1259 void tmpfs_rename_exit(struct tmpfs_mount *,
1260     struct vnode *, struct vnode *, struct vnode *, struct vnode *);
1261 int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *);
1262 int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *,
1263     struct tmpfs_node **);
1264 int tmpfs_rename_lock(struct mount *, struct ucred *, int,
1265     struct vnode *, struct tmpfs_node *, struct componentname *, int,
1266     struct tmpfs_dirent **, struct vnode **,
1267     struct vnode *, struct tmpfs_node *, struct componentname *, int,
1268     struct tmpfs_dirent **, struct vnode **);
1269 void tmpfs_rename_attachdetach(struct tmpfs_mount *,
1270     struct vnode *, struct tmpfs_dirent *, struct vnode *,
1271     struct vnode *, struct tmpfs_dirent *, struct vnode *);
1272 int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *,
1273     struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, struct ucred *);
1274 int tmpfs_rename_check_possible(struct tmpfs_node *,
1275     struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *);
1276 int tmpfs_rename_check_permitted(struct ucred *,
1277     struct tmpfs_node *, struct tmpfs_node *,
1278     struct tmpfs_node *, struct tmpfs_node *);
1279 int tmpfs_remove_check_possible(struct tmpfs_node *,
1280     struct tmpfs_node *);
1281 int tmpfs_remove_check_permitted(struct ucred *,
1282     struct tmpfs_node *, struct tmpfs_node *);
1283 int tmpfs_check_sticky(struct ucred *,
1284     struct tmpfs_node *, struct tmpfs_node *);
1285 void tmpfs_rename_cache_purge(struct vnode *, struct vnode *, struct vnode *,
1286     struct vnode *);
1287 void tmpfs_rename_abort(void *);
1288 
1289 int
1290 tmpfs_rename(void *v)
1291 {
1292 	struct vop_rename_args  /* {
1293 		struct vnode		*a_fdvp;
1294 		struct vnode		*a_fvp;
1295 		struct componentname	*a_fcnp;
1296 		struct vnode		*a_tdvp;
1297 		struct vnode		*a_tvp;
1298 		struct componentname	*a_tcnp;
1299 	} */ *ap = v;
1300 	struct vnode *fdvp = ap->a_fdvp;
1301 	struct vnode *fvp = ap->a_fvp;
1302 	struct componentname *fcnp = ap->a_fcnp;
1303 	struct vnode *tdvp = ap->a_tdvp;
1304 	struct vnode *tvp = ap->a_tvp;
1305 	struct componentname *tcnp = ap->a_tcnp;
1306 	struct ucred *cred;
1307 	int error;
1308 
1309 	KASSERT(fdvp != NULL);
1310 	KASSERT(fvp != NULL);
1311 	KASSERT(fcnp != NULL);
1312 	KASSERT(fcnp->cn_nameptr != NULL);
1313 	KASSERT(tdvp != NULL);
1314 	KASSERT(tcnp != NULL);
1315 	KASSERT(fcnp->cn_nameptr != NULL);
1316 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
1317 	/* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
1318 	KASSERT(fdvp->v_type == VDIR);
1319 	KASSERT(tdvp->v_type == VDIR);
1320 	KASSERT(fcnp->cn_flags & HASBUF);
1321 	KASSERT(tcnp->cn_flags & HASBUF);
1322 
1323 	cred = fcnp->cn_cred;
1324 	KASSERT(tcnp->cn_cred == cred);
1325 
1326 	/*
1327 	 * Check for cross-device rename.
1328 	 */
1329 	if (fvp->v_mount != tdvp->v_mount ||
1330 	    (tvp != NULL && (fvp->v_mount != tvp->v_mount))) {
1331 	    	tmpfs_rename_abort(v);
1332 		return EXDEV;
1333 	}
1334 
1335 	/*
1336 	 * Can't check the locks on these until we know they're on
1337 	 * the same FS, as not all FS do locking the same way.
1338 	 */
1339 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1340 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1341 
1342 	/*
1343 	 * Reject renaming '.' and '..'.
1344 	 */
1345 	if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1346 	    (fcnp->cn_namelen == 2 && fcnp->cn_nameptr[0] == '.' &&
1347 	    fcnp->cn_nameptr[1] == '.')) {
1348 		tmpfs_rename_abort(v);
1349 		return EINVAL;
1350 	}
1351 
1352 	/*
1353 	 * Sanitize our world from the VFS insanity.  Unlock the target
1354 	 * directory and node, which are locked.  Release the children,
1355 	 * which are referenced.  Check for rename("x", "y/."), which
1356 	 * it is our responsibility to reject, not the caller's.  (But
1357 	 * the caller does reject rename("x/.", "y").  Go figure.)
1358 	 */
1359 
1360 	VOP_UNLOCK(tdvp);
1361 	if ((tvp != NULL) && (tvp != tdvp))
1362 		VOP_UNLOCK(tvp);
1363 
1364 	vrele(fvp);
1365 	if (tvp != NULL)
1366 		vrele(tvp);
1367 
1368 	if (tvp == tdvp) {
1369 		error = EINVAL;
1370 		goto out;
1371 	}
1372 
1373 	error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, 0);
1374 
1375 out:	/*
1376 	 * All done, whether with success or failure.  Release the
1377 	 * directory nodes now, as the caller expects from the VFS
1378 	 * protocol.
1379 	 */
1380 	vrele(fdvp);
1381 	vrele(tdvp);
1382 
1383 	return error;
1384 }
1385 
1386 /*
1387  * tmpfs_sane_rename: rename routine, the hairiest system call, with
1388  * the sane API.
1389  *
1390  * Arguments:
1391  *
1392  * . fdvp (from directory vnode),
1393  * . fcnp (from component name),
1394  * . tdvp (to directory vnode), and
1395  * . tcnp (to component name).
1396  *
1397  * fdvp and tdvp must be referenced and unlocked.
1398  */
1399 int
1400 tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp,
1401     struct vnode *tdvp, struct componentname *tcnp, struct ucred *cred,
1402     int posixly_correct)
1403 {
1404 	struct mount *mount;
1405 	struct tmpfs_mount *tmpfs;
1406 	struct tmpfs_node *fdnode, *tdnode;
1407 	struct tmpfs_dirent *fde, *tde;
1408 	struct vnode *fvp, *tvp;
1409 	char *newname;
1410 	int error;
1411 
1412 	KASSERT(fdvp != NULL);
1413 	KASSERT(fcnp != NULL);
1414 	KASSERT(tdvp != NULL);
1415 	KASSERT(tcnp != NULL);
1416 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
1417 	/* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
1418 	KASSERT(fdvp->v_type == VDIR);
1419 	KASSERT(tdvp->v_type == VDIR);
1420 	KASSERT(fdvp->v_mount == tdvp->v_mount);
1421 	KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
1422 	KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
1423 	KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.'));
1424 	KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.'));
1425 	KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') ||
1426 	    (fcnp->cn_nameptr[1] != '.'));
1427 	KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') ||
1428 	    (tcnp->cn_nameptr[1] != '.'));
1429 
1430 	/*
1431 	 * Pull out the tmpfs data structures.
1432 	 */
1433 	fdnode = VP_TO_TMPFS_NODE(fdvp);
1434 	tdnode = VP_TO_TMPFS_NODE(tdvp);
1435 	KASSERT(fdnode != NULL);
1436 	KASSERT(tdnode != NULL);
1437 	KASSERT(fdnode->tn_vnode == fdvp);
1438 	KASSERT(tdnode->tn_vnode == tdvp);
1439 	KASSERT(fdnode->tn_type == VDIR);
1440 	KASSERT(tdnode->tn_type == VDIR);
1441 
1442 	mount = fdvp->v_mount;
1443 	KASSERT(mount != NULL);
1444 	KASSERT(mount == tdvp->v_mount);
1445 	/* XXX How can we be sure this stays true?  (Not that you're
1446 	 * likely to mount a tmpfs read-only...)  */
1447 	KASSERT((mount->mnt_flag & MNT_RDONLY) == 0);
1448 	tmpfs = VFS_TO_TMPFS(mount);
1449 	KASSERT(tmpfs != NULL);
1450 
1451 	/*
1452 	 * Decide whether we need a new name, and allocate memory for
1453 	 * it if so.  Do this before locking anything or taking
1454 	 * destructive actions so that we can back out safely and sleep
1455 	 * safely.  XXX Is sleeping an issue here?  Can this just be
1456 	 * moved into tmpfs_rename_attachdetach?
1457 	 */
1458 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
1459 		newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen);
1460 		if (newname == NULL) {
1461 			error = ENOSPC;
1462 			goto out_unlocked;
1463 		}
1464 	} else {
1465 		newname = NULL;
1466 	}
1467 
1468 	/*
1469 	 * Lock and look up everything.  GCC is not very clever.
1470 	 */
1471 	fde = tde = NULL;
1472 	fvp = tvp = NULL;
1473 	error = tmpfs_rename_enter(mount, tmpfs, cred,
1474 	    fdvp, fdnode, fcnp, &fde, &fvp,
1475 	    tdvp, tdnode, tcnp, &tde, &tvp);
1476 	if (error)
1477 		goto out_unlocked;
1478 
1479 	/*
1480 	 * Check that everything is locked and looks right.
1481 	 */
1482 	KASSERT(fde != NULL);
1483 	KASSERT(fvp != NULL);
1484 	KASSERT(fde->td_node != NULL);
1485 	KASSERT(fde->td_node->tn_vnode == fvp);
1486 	KASSERT(fde->td_node->tn_type == fvp->v_type);
1487 	KASSERT((tde == NULL) == (tvp == NULL));
1488 	KASSERT((tde == NULL) || (tde->td_node != NULL));
1489 	KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
1490 	KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type));
1491 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1492 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1493 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1494 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1495 
1496 	/*
1497 	 * If the source and destination are the same object, we need
1498 	 * only at most delete the source entry.
1499 	 */
1500 	if (fvp == tvp) {
1501 		KASSERT(tvp != NULL);
1502 		if (fde->td_node->tn_type == VDIR) {
1503 			/* XXX How can this possibly happen?  */
1504 			error = EINVAL;
1505 			goto out_locked;
1506 		}
1507 		if (!posixly_correct && (fde != tde)) {
1508 			/* XXX Doesn't work because of locking.
1509 			 * error = VOP_REMOVE(fdvp, fvp);
1510 			 */
1511 			error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp,
1512 			    cred);
1513 			if (error)
1514 				goto out_locked;
1515 		}
1516 		goto success;
1517 	}
1518 	KASSERT(fde != tde);
1519 	KASSERT(fvp != tvp);
1520 
1521 	/*
1522 	 * If the target exists, refuse to rename a directory over a
1523 	 * non-directory or vice versa, or to clobber a non-empty
1524 	 * directory.
1525 	 */
1526 	if (tvp != NULL) {
1527 		KASSERT(tde != NULL);
1528 		KASSERT(tde->td_node != NULL);
1529 		if (fvp->v_type == VDIR && tvp->v_type == VDIR)
1530 			error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0);
1531 		else if (fvp->v_type == VDIR && tvp->v_type != VDIR)
1532 			error = ENOTDIR;
1533 		else if (fvp->v_type != VDIR && tvp->v_type == VDIR)
1534 			error = EISDIR;
1535 		else
1536 			error = 0;
1537 		if (error)
1538 			goto out_locked;
1539 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
1540 	}
1541 
1542 	/*
1543 	 * Authorize the rename.
1544 	 */
1545 	error = tmpfs_rename_check_possible(fdnode, fde->td_node,
1546 	    tdnode, (tde? tde->td_node : NULL));
1547 	if (error)
1548 		goto out_locked;
1549 	error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node,
1550 	    tdnode, (tde? tde->td_node : NULL));
1551 	if (error)
1552 		goto out_locked;
1553 
1554 	/*
1555 	 * Everything is hunky-dory.  Shuffle the directory entries.
1556 	 */
1557 	tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp);
1558 
1559 	/*
1560 	 * Update the directory entry's name necessary, and flag
1561 	 * metadata updates.  A memory allocation failure here is not
1562 	 * OK because we've already committed some changes that we
1563 	 * can't back out at this point, and we have things locked so
1564 	 * we can't sleep, hence the early allocation above.
1565 	 */
1566 	if (newname != NULL) {
1567 		KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
1568 
1569 		tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen);
1570 		fde->td_namelen = (uint16_t)tcnp->cn_namelen;
1571 		(void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1572 		/* Commit newname and don't free it on the way out.  */
1573 		fde->td_name = newname;
1574 		newname = NULL;
1575 
1576 		tmpfs_update(fde->td_node, TMPFS_NODE_CHANGED);
1577 		tmpfs_update(tdnode, TMPFS_NODE_MODIFIED);
1578 	}
1579 
1580 success:
1581 	VN_KNOTE(fvp, NOTE_RENAME);
1582 	tmpfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
1583 	error = 0;
1584 
1585 out_locked:
1586 	tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
1587 
1588 out_unlocked:
1589 	/* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
1590 	/* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
1591 	/* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */
1592 	/* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
1593 
1594 	if (newname != NULL)
1595 		tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen);
1596 
1597 	return error;
1598 }
1599 
1600 /*
1601  * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret
1602  * and the associated vnode in fvp_ret; fail if not found.  Look up
1603  * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the
1604  * associated vnode in tvp_ret; store null instead if not found.  Fail
1605  * if anything has been mounted on any of the nodes involved.
1606  *
1607  * fdvp and tdvp must be referenced.
1608  *
1609  * On entry, nothing is locked.
1610  *
1611  * On success, everything is locked, and *fvp_ret, and *tvp_ret if
1612  * nonnull, are referenced.  The only pairs of vnodes that may be
1613  * identical are {fdvp, tdvp} and {fvp, tvp}.
1614  *
1615  * On failure, everything remains as was.
1616  *
1617  * Locking everything including the source and target nodes is
1618  * necessary to make sure that, e.g., link count updates are OK.  The
1619  * locking order is, in general, ancestor-first, matching the order you
1620  * need to use to look up a descendant anyway.
1621  */
1622 int
1623 tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs,
1624     struct ucred *cred,
1625     struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
1626     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1627     struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
1628     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1629 {
1630 	int error;
1631 
1632 	KASSERT(mount != NULL);
1633 	KASSERT(tmpfs != NULL);
1634 	KASSERT(fdvp != NULL);
1635 	KASSERT(fdnode != NULL);
1636 	KASSERT(fcnp != NULL);
1637 	KASSERT(fde_ret != NULL);
1638 	KASSERT(fvp_ret != NULL);
1639 	KASSERT(tdvp != NULL);
1640 	KASSERT(tdnode != NULL);
1641 	KASSERT(tcnp != NULL);
1642 	KASSERT(tde_ret != NULL);
1643 	KASSERT(tvp_ret != NULL);
1644 	KASSERT(fdnode->tn_vnode == fdvp);
1645 	KASSERT(tdnode->tn_vnode == tdvp);
1646 	KASSERT(fdnode->tn_type == VDIR);
1647 	KASSERT(tdnode->tn_type == VDIR);
1648 
1649 	if (fdvp == tdvp) {
1650 		KASSERT(fdnode == tdnode);
1651 		error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp,
1652 		    fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret);
1653 	} else {
1654 		KASSERT(fdnode != tdnode);
1655 		error = tmpfs_rename_enter_separate(mount, tmpfs, cred,
1656 		    fdvp, fdnode, fcnp, fde_ret, fvp_ret,
1657 		    tdvp, tdnode, tcnp, tde_ret, tvp_ret);
1658 	}
1659 
1660 	if (error)
1661 		return error;
1662 
1663 	KASSERT(*fde_ret != NULL);
1664 	KASSERT(*fvp_ret != NULL);
1665 	KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL));
1666 	KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL));
1667 	KASSERT((*tde_ret == NULL) ||
1668 	    ((*tde_ret)->td_node->tn_vnode == *tvp_ret));
1669 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1670 	KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE);
1671 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1672 	KASSERT((*tvp_ret == NULL) ||
1673 	    (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE));
1674 	KASSERT(*fvp_ret != fdvp);
1675 	KASSERT(*fvp_ret != tdvp);
1676 	KASSERT(*tvp_ret != fdvp);
1677 	KASSERT(*tvp_ret != tdvp);
1678 	return 0;
1679 }
1680 
1681 /*
1682  * Lock and look up with a common source/target directory.
1683  */
1684 int
1685 tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs,
1686     struct ucred *cred,
1687     struct vnode *dvp, struct tmpfs_node *dnode,
1688     struct componentname *fcnp,
1689     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1690     struct componentname *tcnp,
1691     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1692 {
1693 	struct tmpfs_dirent *fde, *tde;
1694 	struct vnode *fvp, *tvp;
1695 	int error;
1696 
1697 	error = tmpfs_rename_lock_directory(dvp, dnode);
1698 	if (error)
1699 		goto fail0;
1700 
1701 	/* Did we lose a race with mount?  */
1702 	if (dvp->v_mountedhere != NULL) {
1703 		error = EBUSY;
1704 		goto fail1;
1705 	}
1706 
1707 	/* Make sure the caller may read the directory.  */
1708 	error = VOP_ACCESS(dvp, VEXEC, cred, curproc);
1709 	if (error)
1710 		goto fail1;
1711 
1712 	/*
1713 	 * The order in which we lock the source and target nodes is
1714 	 * irrelevant because there can only be one rename on this
1715 	 * directory in flight at a time, and we have it locked.
1716 	 */
1717 
1718 	fde = tmpfs_dir_lookup(dnode, fcnp);
1719 	if (fde == NULL) {
1720 		error = ENOENT;
1721 		goto fail1;
1722 	}
1723 
1724 	KASSERT(fde->td_node != NULL);
1725 	/* We ruled out `.' earlier.  */
1726 	KASSERT(fde->td_node != dnode);
1727 	/* We ruled out `..' earlier.  */
1728 	KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent);
1729 	rw_enter_write(&fde->td_node->tn_nlock);
1730 	error = tmpfs_vnode_get(mount, fde->td_node, &fvp);
1731 	if (error)
1732 		goto fail1;
1733 	KASSERT(fvp != NULL);
1734 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1735 	KASSERT(fvp != dvp);
1736 	KASSERT(fvp->v_mount == mount);
1737 
1738 	/* Refuse to rename a mount point.  */
1739 	if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) {
1740 		error = EBUSY;
1741 		goto fail2;
1742 	}
1743 
1744 	tde = tmpfs_dir_lookup(dnode, tcnp);
1745 	if (tde == NULL) {
1746 		tvp = NULL;
1747 	} else {
1748 		KASSERT(tde->td_node != NULL);
1749 		/* We ruled out `.' earlier.  */
1750 		KASSERT(tde->td_node != dnode);
1751 		/* We ruled out `..' earlier.  */
1752 		KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent);
1753 		if (tde->td_node != fde->td_node) {
1754 			rw_enter_write(&tde->td_node->tn_nlock);
1755 			error = tmpfs_vnode_get(mount, tde->td_node, &tvp);
1756 			if (error)
1757 				goto fail2;
1758 			KASSERT(tvp->v_mount == mount);
1759 			/* Refuse to rename over a mount point.  */
1760 			if ((tvp->v_type == VDIR) &&
1761 			    (tvp->v_mountedhere != NULL)) {
1762 				error = EBUSY;
1763 				goto fail3;
1764 			}
1765 		} else {
1766 			tvp = fvp;
1767 			vref(tvp);
1768 		}
1769 		KASSERT(tvp != NULL);
1770 		KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
1771 	}
1772 	KASSERT(tvp != dvp);
1773 
1774 	*fde_ret = fde;
1775 	*fvp_ret = fvp;
1776 	*tde_ret = tde;
1777 	*tvp_ret = tvp;
1778 	return 0;
1779 
1780 fail3:	if (tvp != NULL) {
1781 		if (tvp != fvp)
1782 			vput(tvp);
1783 		else
1784 			vrele(tvp);
1785 	}
1786 
1787 fail2:	vput(fvp);
1788 fail1:	VOP_UNLOCK(dvp);
1789 fail0:	return error;
1790 }
1791 
1792 /*
1793  * Lock and look up with separate source and target directories.
1794  */
1795 int
1796 tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs,
1797     struct ucred *cred,
1798     struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
1799     struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1800     struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
1801     struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1802 {
1803 	struct tmpfs_node *intermediate_node;
1804 	struct tmpfs_dirent *fde, *tde;
1805 	struct vnode *fvp, *tvp;
1806 	int error;
1807 
1808 	KASSERT(fdvp != tdvp);
1809 	KASSERT(fdnode != tdnode);
1810 
1811 #if 0				/* XXX */
1812 	mutex_enter(&tmpfs->tm_rename_lock);
1813 #endif
1814 
1815 	error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node);
1816 	if (error)
1817 		goto fail;
1818 
1819 	/*
1820 	 * intermediate_node == NULL means fdnode is not an ancestor of
1821 	 * tdnode.
1822 	 */
1823 	if (intermediate_node == NULL)
1824 		error = tmpfs_rename_lock(mount, cred, ENOTEMPTY,
1825 		    tdvp, tdnode, tcnp, 1, &tde, &tvp,
1826 		    fdvp, fdnode, fcnp, 0, &fde, &fvp);
1827 	else
1828 		error = tmpfs_rename_lock(mount, cred, EINVAL,
1829 		    fdvp, fdnode, fcnp, 0, &fde, &fvp,
1830 		    tdvp, tdnode, tcnp, 1, &tde, &tvp);
1831 	if (error)
1832 		goto fail;
1833 
1834 	KASSERT(fde != NULL);
1835 	KASSERT(fde->td_node != NULL);
1836 
1837 	/*
1838 	 * Reject rename("foo/bar", "foo/bar/baz/quux/zot").
1839 	 */
1840 	if (fde->td_node == intermediate_node) {
1841 		tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
1842 		return EINVAL;
1843 	}
1844 
1845 	*fde_ret = fde;
1846 	*fvp_ret = fvp;
1847 	*tde_ret = tde;
1848 	*tvp_ret = tvp;
1849 	return 0;
1850 
1851 fail:
1852 #if 0				/* XXX */
1853 	mutex_exit(&tmpfs->tm_rename_lock);
1854 #endif
1855 	return error;
1856 }
1857 
1858 /*
1859  * Unlock everything we locked for rename.
1860  *
1861  * fdvp and tdvp must be referenced.
1862  *
1863  * On entry, everything is locked, and fvp and tvp referenced.
1864  *
1865  * On exit, everything is unlocked, and fvp and tvp are released.
1866  */
1867 void
1868 tmpfs_rename_exit(struct tmpfs_mount *tmpfs,
1869     struct vnode *fdvp, struct vnode *fvp,
1870     struct vnode *tdvp, struct vnode *tvp)
1871 {
1872 
1873 	KASSERT(tmpfs != NULL);
1874 	KASSERT(fdvp != NULL);
1875 	KASSERT(fvp != NULL);
1876 	KASSERT(fdvp != fvp);
1877 	KASSERT(fdvp != tvp);
1878 	KASSERT(tdvp != tvp);
1879 	KASSERT(tdvp != fvp);
1880 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1881 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1882 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1883 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1884 
1885 	if (tvp != NULL) {
1886 		if (tvp != fvp)
1887 			vput(tvp);
1888 		else
1889 			vrele(tvp);
1890 	}
1891 	VOP_UNLOCK(tdvp);
1892 	vput(fvp);
1893 	if (fdvp != tdvp)
1894 		VOP_UNLOCK(fdvp);
1895 
1896 #if 0				/* XXX */
1897 	if (fdvp != tdvp)
1898 		mutex_exit(&tmpfs->tm_rename_lock);
1899 #endif
1900 }
1901 
1902 /*
1903  * Lock a directory, but fail if it has been rmdir'd.
1904  *
1905  * vp must be referenced.
1906  */
1907 int
1908 tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node)
1909 {
1910 
1911 	KASSERT(vp != NULL);
1912 	KASSERT(node != NULL);
1913 	KASSERT(node->tn_vnode == vp);
1914 	KASSERT(node->tn_type == VDIR);
1915 
1916 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1917 	if (node->tn_spec.tn_dir.tn_parent == NULL) {
1918 		VOP_UNLOCK(vp);
1919 		return ENOENT;
1920 	}
1921 
1922 	return 0;
1923 }
1924 
1925 /*
1926  * Analyze the genealogy of the source and target nodes.
1927  *
1928  * On success, stores in *intermediate_node_ret either the child of
1929  * fdnode of which tdnode is a descendant, or null if tdnode is not a
1930  * descendant of fdnode at all.
1931  *
1932  * fdnode and tdnode must be unlocked and referenced.  The file
1933  * system's rename lock must also be held, to exclude concurrent
1934  * changes to the file system's genealogy other than rmdir.
1935  *
1936  * XXX This causes an extra lock/unlock of tdnode in the case when
1937  * we're just about to lock it again before locking anything else.
1938  * However, changing that requires reorganizing the code to make it
1939  * even more horrifically obscure.
1940  */
1941 int
1942 tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode,
1943     struct tmpfs_node **intermediate_node_ret)
1944 {
1945 	struct tmpfs_node *node = tdnode, *parent;
1946 	int error;
1947 
1948 	KASSERT(fdnode != NULL);
1949 	KASSERT(tdnode != NULL);
1950 	KASSERT(fdnode != tdnode);
1951 	KASSERT(intermediate_node_ret != NULL);
1952 
1953 	KASSERT(fdnode->tn_vnode != NULL);
1954 	KASSERT(tdnode->tn_vnode != NULL);
1955 	KASSERT(fdnode->tn_type == VDIR);
1956 	KASSERT(tdnode->tn_type == VDIR);
1957 
1958 	/*
1959 	 * We need to provisionally lock tdnode->tn_vnode to keep rmdir
1960 	 * from deleting it -- or any ancestor -- at an inopportune
1961 	 * moment.
1962 	 */
1963 	error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode);
1964 	if (error)
1965 		return error;
1966 
1967 	for (;;) {
1968 		parent = node->tn_spec.tn_dir.tn_parent;
1969 		KASSERT(parent != NULL);
1970 		KASSERT(parent->tn_type == VDIR);
1971 
1972 		/* Did we hit the root without finding fdnode?  */
1973 		if (parent == node) {
1974 			*intermediate_node_ret = NULL;
1975 			break;
1976 		}
1977 
1978 		/* Did we find that fdnode is an ancestor?  */
1979 		if (parent == fdnode) {
1980 			*intermediate_node_ret = node;
1981 			break;
1982 		}
1983 
1984 		/* Neither -- keep ascending the family tree.  */
1985 		node = parent;
1986 	}
1987 
1988 	VOP_UNLOCK(tdnode->tn_vnode);
1989 	return 0;
1990 }
1991 
1992 /*
1993  * Lock directories a and b, which must be distinct, and look up and
1994  * lock nodes a and b.  Do a first and then b.  Directory b may not be
1995  * an ancestor of directory a, although directory a may be an ancestor
1996  * of directory b.  Fail with overlap_error if node a is directory b.
1997  * Neither componentname may be `.' or `..'.
1998  *
1999  * a_dvp and b_dvp must be referenced.
2000  *
2001  * On entry, a_dvp and b_dvp are unlocked.
2002  *
2003  * On success,
2004  * . a_dvp and b_dvp are locked,
2005  * . *a_dirent_ret is filled with a directory entry whose node is
2006  *     locked and referenced,
2007  * . *b_vp_ret is filled with the corresponding vnode,
2008  * . *b_dirent_ret is filled either with null or with a directory entry
2009  *     whose node is locked and referenced,
2010  * . *b_vp is filled either with null or with the corresponding vnode,
2011  *     and
2012  * . the only pair of vnodes that may be identical is a_vp and b_vp.
2013  *
2014  * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret,
2015  * *a_vp, *b_dirent_ret, and *b_vp are left alone.
2016  */
2017 int
2018 tmpfs_rename_lock(struct mount *mount, struct ucred *cred, int overlap_error,
2019     struct vnode *a_dvp, struct tmpfs_node *a_dnode,
2020     struct componentname *a_cnp, int a_missing_ok,
2021     struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret,
2022     struct vnode *b_dvp, struct tmpfs_node *b_dnode,
2023     struct componentname *b_cnp, int b_missing_ok,
2024     struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret)
2025 {
2026 	struct tmpfs_dirent *a_dirent, *b_dirent;
2027 	struct vnode *a_vp, *b_vp;
2028 	int error;
2029 
2030 	KASSERT(a_dvp != NULL);
2031 	KASSERT(a_dnode != NULL);
2032 	KASSERT(a_cnp != NULL);
2033 	KASSERT(a_dirent_ret != NULL);
2034 	KASSERT(a_vp_ret != NULL);
2035 	KASSERT(b_dvp != NULL);
2036 	KASSERT(b_dnode != NULL);
2037 	KASSERT(b_cnp != NULL);
2038 	KASSERT(b_dirent_ret != NULL);
2039 	KASSERT(b_vp_ret != NULL);
2040 	KASSERT(a_dvp != b_dvp);
2041 	KASSERT(a_dnode != b_dnode);
2042 	KASSERT(a_dnode->tn_vnode == a_dvp);
2043 	KASSERT(b_dnode->tn_vnode == b_dvp);
2044 	KASSERT(a_dnode->tn_type == VDIR);
2045 	KASSERT(b_dnode->tn_type == VDIR);
2046 	KASSERT(a_missing_ok != b_missing_ok);
2047 
2048 	error = tmpfs_rename_lock_directory(a_dvp, a_dnode);
2049 	if (error)
2050 		goto fail0;
2051 
2052 	/* Did we lose a race with mount?  */
2053 	if (a_dvp->v_mountedhere != NULL) {
2054 		error = EBUSY;
2055 		goto fail1;
2056 	}
2057 
2058 	/* Make sure the caller may read the directory.  */
2059 	error = VOP_ACCESS(a_dvp, VEXEC, cred, curproc);
2060 	if (error)
2061 		goto fail1;
2062 
2063 	a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp);
2064 	if (a_dirent != NULL) {
2065 		KASSERT(a_dirent->td_node != NULL);
2066 		/* We ruled out `.' earlier.  */
2067 		KASSERT(a_dirent->td_node != a_dnode);
2068 		/* We ruled out `..' earlier.  */
2069 		KASSERT(a_dirent->td_node !=
2070 		    a_dnode->tn_spec.tn_dir.tn_parent);
2071 		if (a_dirent->td_node == b_dnode) {
2072 			error = overlap_error;
2073 			goto fail1;
2074 		}
2075 		rw_enter_write(&a_dirent->td_node->tn_nlock);
2076 		error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp);
2077 		if (error)
2078 			goto fail1;
2079 		KASSERT(a_vp->v_mount == mount);
2080 		/* Refuse to rename (over) a mount point.  */
2081 		if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) {
2082 			error = EBUSY;
2083 			goto fail2;
2084 		}
2085 	} else if (!a_missing_ok) {
2086 		error = ENOENT;
2087 		goto fail1;
2088 	} else {
2089 		a_vp = NULL;
2090 	}
2091 	KASSERT(a_vp != a_dvp);
2092 	KASSERT(a_vp != b_dvp);
2093 
2094 	error = tmpfs_rename_lock_directory(b_dvp, b_dnode);
2095 	if (error)
2096 		goto fail2;
2097 
2098 	/* Did we lose a race with mount?  */
2099 	if (b_dvp->v_mountedhere != NULL) {
2100 		error = EBUSY;
2101 		goto fail3;
2102 	}
2103 
2104 	/* Make sure the caller may read the directory.  */
2105 	error = VOP_ACCESS(b_dvp, VEXEC, cred, curproc);
2106 	if (error)
2107 		goto fail3;
2108 
2109 	b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp);
2110 	if (b_dirent != NULL) {
2111 		KASSERT(b_dirent->td_node != NULL);
2112 		/* We ruled out `.' earlier.  */
2113 		KASSERT(b_dirent->td_node != b_dnode);
2114 		/* We ruled out `..' earlier.  */
2115 		KASSERT(b_dirent->td_node !=
2116 		    b_dnode->tn_spec.tn_dir.tn_parent);
2117 		/* b is not an ancestor of a.  */
2118 		KASSERT(b_dirent->td_node != a_dnode);
2119 		/* But the source and target nodes might be the same.  */
2120 		if ((a_dirent == NULL) ||
2121 		    (a_dirent->td_node != b_dirent->td_node)) {
2122 			rw_enter_write(&b_dirent->td_node->tn_nlock);
2123 			error = tmpfs_vnode_get(mount, b_dirent->td_node,
2124 			    &b_vp);
2125 			if (error)
2126 				goto fail3;
2127 			KASSERT(b_vp->v_mount == mount);
2128 			KASSERT(a_vp != b_vp);
2129 			/* Refuse to rename (over) a mount point.  */
2130 			if ((b_vp->v_type == VDIR) &&
2131 			    (b_vp->v_mountedhere != NULL)) {
2132 				error = EBUSY;
2133 				goto fail4;
2134 			}
2135 		} else {
2136 			b_vp = a_vp;
2137 			vref(b_vp);
2138 		}
2139 	} else if (!b_missing_ok) {
2140 		error = ENOENT;
2141 		goto fail3;
2142 	} else {
2143 		b_vp = NULL;
2144 	}
2145 	KASSERT(b_vp != a_dvp);
2146 	KASSERT(b_vp != b_dvp);
2147 
2148 	KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
2149 	KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
2150 	KASSERT(a_missing_ok || (a_dirent != NULL));
2151 	KASSERT(a_missing_ok || (a_dirent->td_node != NULL));
2152 	KASSERT(b_missing_ok || (b_dirent != NULL));
2153 	KASSERT(b_missing_ok || (b_dirent->td_node != NULL));
2154 	KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL));
2155 	KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp));
2156 	KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL));
2157 	KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp));
2158 	KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE));
2159 	KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE));
2160 
2161 	*a_dirent_ret = a_dirent;
2162 	*b_dirent_ret = b_dirent;
2163 	*a_vp_ret = a_vp;
2164 	*b_vp_ret = b_vp;
2165 	return 0;
2166 
2167 fail4:	if (b_vp != NULL) {
2168 		KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE);
2169 		if (b_vp != a_vp)
2170 			vput(b_vp);
2171 		else
2172 			vrele(a_vp);
2173 	}
2174 
2175 fail3:	KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
2176 	VOP_UNLOCK(b_dvp);
2177 
2178 fail2:	if (a_vp != NULL) {
2179 		KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE);
2180 		vput(a_vp);
2181 	}
2182 
2183 fail1:	KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
2184 	VOP_UNLOCK(a_dvp);
2185 
2186 fail0:	/* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */
2187 	/* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */
2188 	/* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */
2189 	/* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */
2190 	return error;
2191 }
2192 
2193 /*
2194  * Shuffle the directory entries to move fvp from the directory fdvp
2195  * into the directory tdvp.  fde is fvp's directory entry in fdvp.  If
2196  * we are overwriting a target node, it is tvp, and tde is its
2197  * directory entry in tdvp.
2198  *
2199  * fdvp, fvp, tdvp, and tvp must all be locked and referenced.
2200  */
2201 void
2202 tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs,
2203     struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp,
2204     struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp)
2205 {
2206 
2207 	KASSERT(tmpfs != NULL);
2208 	KASSERT(fdvp != NULL);
2209 	KASSERT(fde != NULL);
2210 	KASSERT(fvp != NULL);
2211 	KASSERT(tdvp != NULL);
2212 	KASSERT(fde->td_node != NULL);
2213 	KASSERT(fde->td_node->tn_vnode == fvp);
2214 	KASSERT((tde == NULL) == (tvp == NULL));
2215 	KASSERT((tde == NULL) || (tde->td_node != NULL));
2216 	KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
2217 	KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
2218 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
2219 	KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
2220 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
2221 
2222 	/*
2223 	 * If we are moving from one directory to another, detach the
2224 	 * source entry and reattach it to the target directory.
2225 	 */
2226 	if (fdvp != tdvp) {
2227 		/* tmpfs_dir_detach clobbers fde->td_node, so save it.  */
2228 		struct tmpfs_node *fnode = fde->td_node;
2229 		tmpfs_node_t *fdnode = VP_TO_TMPFS_DIR(fdvp);
2230 		tmpfs_node_t *tdnode = VP_TO_TMPFS_DIR(tdvp);
2231 		tmpfs_dir_detach(fdnode, fde);
2232 		tmpfs_dir_attach(tdnode, fde, fnode);
2233 	} else if (tvp == NULL) {
2234 		/*
2235 		 * We are changing the directory.  tmpfs_dir_attach and
2236 		 * tmpfs_dir_detach note the events for us, but for
2237 		 * this case we don't call them, so we must note the
2238 		 * event explicitly.
2239 		 */
2240 		VN_KNOTE(fdvp, NOTE_WRITE);
2241 	}
2242 
2243 	/*
2244 	 * If we are replacing an existing target entry, delete it.
2245 	 */
2246 	if (tde != NULL) {
2247 		tmpfs_node_t *tdnode = VP_TO_TMPFS_DIR(tdvp);
2248 		KASSERT(tvp != NULL);
2249 		KASSERT(tde->td_node != NULL);
2250 		KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
2251 		if (tde->td_node->tn_type == VDIR) {
2252 			KASSERT(tde->td_node->tn_size == 0);
2253 			KASSERT(tde->td_node->tn_links == 2);
2254 			/* Decrement the extra link count for `.' so
2255 			 * the vnode will be recycled when released.  */
2256 			tde->td_node->tn_links--;
2257 		}
2258 		tmpfs_dir_detach(tdnode, tde);
2259 		tmpfs_free_dirent(tmpfs, tde);
2260 	}
2261 }
2262 
2263 /*
2264  * Remove the entry de for the non-directory vp from the directory dvp.
2265  *
2266  * Everything must be locked and referenced.
2267  */
2268 int
2269 tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp,
2270     struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp,
2271     struct ucred *cred)
2272 {
2273 	int error;
2274 
2275 	KASSERT(tmpfs != NULL);
2276 	KASSERT(dvp != NULL);
2277 	KASSERT(dnode != NULL);
2278 	KASSERT(de != NULL);
2279 	KASSERT(vp != NULL);
2280 	KASSERT(dnode->tn_vnode == dvp);
2281 	KASSERT(de->td_node != NULL);
2282 	KASSERT(de->td_node->tn_vnode == vp);
2283 	KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
2284 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
2285 
2286 	error = tmpfs_remove_check_possible(dnode, de->td_node);
2287 	if (error)
2288 		return error;
2289 
2290 	error = tmpfs_remove_check_permitted(cred, dnode, de->td_node);
2291 	if (error)
2292 		return error;
2293 
2294 	/*
2295 	 * If not root and directory is sticky, check for permission on
2296 	 * directory or on file. This implements append-only directories.
2297 	 */
2298 	if ((dnode->tn_mode & S_ISTXT) != 0)
2299 		if (cred->cr_uid != 0 && cred->cr_uid != dnode->tn_uid &&
2300 		    cred->cr_uid != de->td_node->tn_uid)
2301 			return EPERM;
2302 
2303 	tmpfs_dir_detach(dnode, de);
2304 	tmpfs_free_dirent(tmpfs, de);
2305 
2306 	return 0;
2307 }
2308 
2309 /*
2310  * Check whether a rename is possible independent of credentials.
2311  *
2312  * Everything must be locked and referenced.
2313  */
2314 int
2315 tmpfs_rename_check_possible(
2316     struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
2317     struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
2318 {
2319 
2320 	KASSERT(fdnode != NULL);
2321 	KASSERT(fnode != NULL);
2322 	KASSERT(tdnode != NULL);
2323 	KASSERT(fdnode != fnode);
2324 	KASSERT(tdnode != tnode);
2325 	KASSERT(fnode != tnode);
2326 	KASSERT(fdnode->tn_vnode != NULL);
2327 	KASSERT(fnode->tn_vnode != NULL);
2328 	KASSERT(tdnode->tn_vnode != NULL);
2329 	KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
2330 	KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
2331 	KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
2332 	KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
2333 	KASSERT((tnode == NULL) ||
2334 	    (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
2335 
2336 	/*
2337 	 * If fdnode is immutable, we can't write to it.  If fdnode is
2338 	 * append-only, the only change we can make is to add entries
2339 	 * to it.  If fnode is immutable, we can't change the links to
2340 	 * it.  If fnode is append-only...well, this is what UFS does.
2341 	 */
2342 	if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND))
2343 		return EPERM;
2344 
2345 	/*
2346 	 * If tdnode is immutable, we can't write to it.  If tdnode is
2347 	 * append-only, we can add entries, but we can't change
2348 	 * existing entries.
2349 	 */
2350 	if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0)))
2351 		return EPERM;
2352 
2353 	/*
2354 	 * If tnode is immutable, we can't replace links to it.  If
2355 	 * tnode is append-only...well, this is what UFS does.
2356 	 */
2357 	if (tnode != NULL) {
2358 		KASSERT(tnode != NULL);
2359 		if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0)
2360 			return EPERM;
2361 	}
2362 
2363 	return 0;
2364 }
2365 
2366 /*
2367  * Check whether a rename is permitted given our credentials.
2368  *
2369  * Everything must be locked and referenced.
2370  */
2371 int
2372 tmpfs_rename_check_permitted(struct ucred *cred,
2373     struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
2374     struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
2375 {
2376 	int error;
2377 
2378 	KASSERT(fdnode != NULL);
2379 	KASSERT(fnode != NULL);
2380 	KASSERT(tdnode != NULL);
2381 	KASSERT(fdnode != fnode);
2382 	KASSERT(tdnode != tnode);
2383 	KASSERT(fnode != tnode);
2384 	KASSERT(fdnode->tn_vnode != NULL);
2385 	KASSERT(fnode->tn_vnode != NULL);
2386 	KASSERT(tdnode->tn_vnode != NULL);
2387 	KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
2388 	KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
2389 	KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
2390 	KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
2391 	KASSERT((tnode == NULL) ||
2392 	    (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
2393 
2394 	/*
2395 	 * We need to remove or change an entry in the source directory.
2396 	 */
2397 	error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred, curproc);
2398 	if (error)
2399 		return error;
2400 
2401 	/*
2402 	 * If we are changing directories, then we need to write to the
2403 	 * target directory to add or change an entry.  Also, if fnode
2404 	 * is a directory, we need to write to it to change its `..'
2405 	 * entry.
2406 	 */
2407 	if (fdnode != tdnode) {
2408 		error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred, curproc);
2409 		if (error)
2410 			return error;
2411 		if (fnode->tn_type == VDIR) {
2412 			error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred,
2413 			    curproc);
2414 			if (error)
2415 				return error;
2416 		}
2417 	}
2418 
2419 	error = tmpfs_check_sticky(cred, fdnode, fnode);
2420 	if (error)
2421 		return error;
2422 
2423 	if (TMPFS_DIRSEQ_FULL(tdnode))
2424 		return (ENOSPC);
2425 
2426 	error = tmpfs_check_sticky(cred, tdnode, tnode);
2427 	if (error)
2428 		return error;
2429 
2430 	return 0;
2431 }
2432 
2433 /*
2434  * Check whether removing node's entry in dnode is possible independent
2435  * of credentials.
2436  *
2437  * Everything must be locked and referenced.
2438  */
2439 int
2440 tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node)
2441 {
2442 
2443 	KASSERT(dnode != NULL);
2444 	KASSERT(dnode->tn_vnode != NULL);
2445 	KASSERT(node != NULL);
2446 	KASSERT(dnode != node);
2447 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2448 	KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
2449 
2450 	/*
2451 	 * We want to delete the entry.  If dnode is immutable, we
2452 	 * can't write to it to delete the entry.  If dnode is
2453 	 * append-only, the only change we can make is to add entries,
2454 	 * so we can't delete entries.  If node is immutable, we can't
2455 	 * change the links to it, so we can't delete the entry.  If
2456 	 * node is append-only...well, this is what UFS does.
2457 	 */
2458 	if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND))
2459 		return EPERM;
2460 
2461 	return 0;
2462 }
2463 
2464 /*
2465  * Check whether removing node's entry in dnode is permitted given our
2466  * credentials.
2467  *
2468  * Everything must be locked and referenced.
2469  */
2470 int
2471 tmpfs_remove_check_permitted(struct ucred *cred,
2472     struct tmpfs_node *dnode, struct tmpfs_node *node)
2473 {
2474 	int error;
2475 
2476 	KASSERT(dnode != NULL);
2477 	KASSERT(dnode->tn_vnode != NULL);
2478 	KASSERT(node != NULL);
2479 	KASSERT(dnode != node);
2480 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2481 	KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
2482 
2483 	/*
2484 	 * Check whether we are permitted to write to the source
2485 	 * directory in order to delete an entry from it.
2486 	 */
2487 	error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred, curproc);
2488 	if (error)
2489 		return error;
2490 
2491 	error = tmpfs_check_sticky(cred, dnode, node);
2492 	if (error)
2493 		return error;
2494 
2495 	return 0;
2496 }
2497 
2498 /*
2499  * Check whether we may change an entry in a sticky directory.  If the
2500  * directory is sticky, the user must own either the directory or, if
2501  * it exists, the node, in order to change the entry.
2502  *
2503  * Everything must be locked and referenced.
2504  */
2505 int
2506 tmpfs_check_sticky(struct ucred *cred,
2507     struct tmpfs_node *dnode, struct tmpfs_node *node)
2508 {
2509 
2510 	KASSERT(dnode != NULL);
2511 	KASSERT(dnode->tn_vnode != NULL);
2512 	KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2513 	KASSERT((node == NULL) || (node->tn_vnode != NULL));
2514 	KASSERT((node == NULL) ||
2515 	    (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE));
2516 
2517 	if (node == NULL)
2518 		return 0;
2519 
2520 	if (dnode->tn_mode & S_ISTXT) {
2521 		if (cred->cr_uid != 0 &&
2522 		    cred->cr_uid != dnode->tn_uid &&
2523 		    cred->cr_uid != node->tn_uid)
2524 			return EPERM;
2525 	}
2526 
2527 	return 0;
2528 }
2529 
2530 void
2531 tmpfs_rename_cache_purge(struct vnode *fdvp, struct vnode *fvp,
2532     struct vnode *tdvp, struct vnode *tvp)
2533 {
2534 
2535 	KASSERT(fdvp != NULL);
2536 	KASSERT(fvp != NULL);
2537 	KASSERT(tdvp != NULL);
2538 	KASSERT(fdvp != fvp);
2539 	KASSERT(fdvp != tvp);
2540 	KASSERT(tdvp != fvp);
2541 	KASSERT(tdvp != tvp);
2542 	KASSERT(fvp != tvp);
2543 	KASSERT(fdvp->v_type == VDIR);
2544 	KASSERT(tdvp->v_type == VDIR);
2545 
2546 	/*
2547 	 * XXX What actually needs to be purged?
2548 	 */
2549 
2550 	cache_purge(fdvp);
2551 
2552 	if (fvp->v_type == VDIR)
2553 		cache_purge(fvp);
2554 
2555 	if (tdvp != fdvp)
2556 		cache_purge(tdvp);
2557 
2558 	if ((tvp != NULL) && (tvp->v_type == VDIR))
2559 		cache_purge(tvp);
2560 }
2561 
2562 void
2563 tmpfs_rename_abort(void *v)
2564 {
2565 	struct vop_rename_args *ap = v;
2566 	struct vnode *fdvp = ap->a_fdvp;
2567 	struct vnode *fvp = ap->a_fvp;
2568 	struct componentname *fcnp = ap->a_fcnp;
2569 	struct vnode *tdvp = ap->a_tdvp;
2570 	struct vnode *tvp = ap->a_tvp;
2571 	struct componentname *tcnp = ap->a_tcnp;
2572 
2573 	VOP_ABORTOP(tdvp, tcnp);
2574 	if (tdvp == tvp)
2575 		vrele(tdvp);
2576 	else
2577 		vput(tdvp);
2578 	if (tvp != NULL)
2579 		vput(tvp);
2580 	VOP_ABORTOP(fdvp, fcnp);
2581 	vrele(fdvp);
2582 	vrele(fvp);
2583 }
2584 
2585 void filt_tmpfsdetach(struct knote *kn);
2586 int filt_tmpfsread(struct knote *kn, long hint);
2587 int filt_tmpfswrite(struct knote *kn, long hint);
2588 int filt_tmpfsvnode(struct knote *kn, long hint);
2589 
2590 struct filterops tmpfsread_filtops =
2591 	{ 1, NULL, filt_tmpfsdetach, filt_tmpfsread };
2592 struct filterops tmpfswrite_filtops =
2593 	{ 1, NULL, filt_tmpfsdetach, filt_tmpfswrite };
2594 struct filterops tmpfsvnode_filtops =
2595 	{ 1, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
2596 
2597 int
2598 tmpfs_kqfilter(void *v)
2599 {
2600 	struct vop_kqfilter_args *ap = v;
2601 	struct vnode *vp = ap->a_vp;
2602 	struct knote *kn = ap->a_kn;
2603 
2604 	switch (kn->kn_filter) {
2605 	case EVFILT_READ:
2606 		kn->kn_fop = &tmpfsread_filtops;
2607 		break;
2608 	case EVFILT_WRITE:
2609 		kn->kn_fop = &tmpfswrite_filtops;
2610 		break;
2611 	case EVFILT_VNODE:
2612 		kn->kn_fop = &tmpfsvnode_filtops;
2613 		break;
2614 	default:
2615 		return (EINVAL);
2616 	}
2617 
2618 	kn->kn_hook = (caddr_t)vp;
2619 
2620 	SLIST_INSERT_HEAD(&vp->v_selectinfo.si_note, kn, kn_selnext);
2621 
2622 	return (0);
2623 }
2624 
2625 void
2626 filt_tmpfsdetach(struct knote *kn)
2627 {
2628 	struct vnode *vp = (struct vnode *)kn->kn_hook;
2629 
2630 	SLIST_REMOVE(&vp->v_selectinfo.si_note, kn, knote, kn_selnext);
2631 }
2632 
2633 int
2634 filt_tmpfsread(struct knote *kn, long hint)
2635 {
2636 	struct vnode *vp = (struct vnode *)kn->kn_hook;
2637 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2638 
2639 	/*
2640 	 * filesystem is gone, so set the EOF flag and schedule
2641 	 * the knote for deletion.
2642 	 */
2643 	if (hint == NOTE_REVOKE) {
2644 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2645 		return (1);
2646 	}
2647 
2648 	kn->kn_data = node->tn_size - foffset(kn->kn_fp);
2649 	if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
2650 		kn->kn_fflags |= NOTE_EOF;
2651 		return (1);
2652 	}
2653 
2654 	return (kn->kn_data != 0);
2655 }
2656 
2657 int
2658 filt_tmpfswrite(struct knote *kn, long hint)
2659 {
2660 	/*
2661 	 * filesystem is gone, so set the EOF flag and schedule
2662 	 * the knote for deletion.
2663 	 */
2664 	if (hint == NOTE_REVOKE) {
2665 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2666 		return (1);
2667 	}
2668 
2669 	kn->kn_data = 0;
2670 	return (1);
2671 }
2672 
2673 int
2674 filt_tmpfsvnode(struct knote *kn, long hint)
2675 {
2676 	if (kn->kn_sfflags & hint)
2677 		kn->kn_fflags |= hint;
2678 	if (hint == NOTE_REVOKE) {
2679 		kn->kn_flags |= EV_EOF;
2680 		return (1);
2681 	}
2682 	return (kn->kn_fflags != 0);
2683 }
2684