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