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