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