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