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