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