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