1171802Sdelphij/*	$NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $	*/
2170808Sdelphij
3182739Sdelphij/*-
4171802Sdelphij * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5170808Sdelphij * All rights reserved.
6170808Sdelphij *
7170808Sdelphij * This code is derived from software contributed to The NetBSD Foundation
8170808Sdelphij * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9170808Sdelphij * 2005 program.
10170808Sdelphij *
11170808Sdelphij * Redistribution and use in source and binary forms, with or without
12170808Sdelphij * modification, are permitted provided that the following conditions
13170808Sdelphij * are met:
14170808Sdelphij * 1. Redistributions of source code must retain the above copyright
15170808Sdelphij *    notice, this list of conditions and the following disclaimer.
16170808Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
17170808Sdelphij *    notice, this list of conditions and the following disclaimer in the
18170808Sdelphij *    documentation and/or other materials provided with the distribution.
19170808Sdelphij *
20170808Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21170808Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22170808Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23170808Sdelphij * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24170808Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25170808Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26170808Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27170808Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28170808Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29170808Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30170808Sdelphij * POSSIBILITY OF SUCH DAMAGE.
31170808Sdelphij */
32170808Sdelphij
33170808Sdelphij/*
34170808Sdelphij * tmpfs vnode interface.
35170808Sdelphij */
36170808Sdelphij#include <sys/cdefs.h>
37170808Sdelphij__FBSDID("$FreeBSD: releng/10.3/sys/fs/tmpfs/tmpfs_vnops.c 295897 2016-02-22 20:18:10Z markj $");
38170808Sdelphij
39170808Sdelphij#include <sys/param.h>
40170808Sdelphij#include <sys/fcntl.h>
41170808Sdelphij#include <sys/lockf.h>
42248084Sattilio#include <sys/lock.h>
43170808Sdelphij#include <sys/namei.h>
44170808Sdelphij#include <sys/priv.h>
45170808Sdelphij#include <sys/proc.h>
46248084Sattilio#include <sys/rwlock.h>
47197850Sdelphij#include <sys/sched.h>
48170808Sdelphij#include <sys/stat.h>
49170808Sdelphij#include <sys/systm.h>
50232960Sgleb#include <sys/sysctl.h>
51170808Sdelphij#include <sys/unistd.h>
52170808Sdelphij#include <sys/vnode.h>
53170808Sdelphij
54170808Sdelphij#include <vm/vm.h>
55239065Skib#include <vm/vm_param.h>
56170808Sdelphij#include <vm/vm_object.h>
57170808Sdelphij#include <vm/vm_page.h>
58170808Sdelphij#include <vm/vm_pager.h>
59188929Salc
60170808Sdelphij#include <fs/tmpfs/tmpfs_vnops.h>
61170808Sdelphij#include <fs/tmpfs/tmpfs.h>
62170808Sdelphij
63232960SglebSYSCTL_DECL(_vfs_tmpfs);
64232960Sgleb
65232960Sglebstatic volatile int tmpfs_rename_restarts;
66232960SglebSYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
67232960Sgleb    __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
68232960Sgleb    "Times rename had to restart due to lock contention");
69232960Sgleb
70171069Sdelphijstatic int
71269173Skibtmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
72269173Skib    struct vnode **rvp)
73269173Skib{
74269173Skib
75269173Skib	return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
76269173Skib}
77269173Skib
78269173Skibstatic int
79170808Sdelphijtmpfs_lookup(struct vop_cachedlookup_args *v)
80170808Sdelphij{
81170808Sdelphij	struct vnode *dvp = v->a_dvp;
82170808Sdelphij	struct vnode **vpp = v->a_vpp;
83170808Sdelphij	struct componentname *cnp = v->a_cnp;
84170808Sdelphij	struct tmpfs_dirent *de;
85170808Sdelphij	struct tmpfs_node *dnode;
86269173Skib	int error;
87170808Sdelphij
88170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
89170808Sdelphij	*vpp = NULLVP;
90170808Sdelphij
91170808Sdelphij	/* Check accessibility of requested node as a first step. */
92191990Sattilio	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
93170808Sdelphij	if (error != 0)
94170808Sdelphij		goto out;
95170808Sdelphij
96170808Sdelphij	/* We cannot be requesting the parent directory of the root node. */
97170808Sdelphij	MPASS(IMPLIES(dnode->tn_type == VDIR &&
98170808Sdelphij	    dnode->tn_dir.tn_parent == dnode,
99170808Sdelphij	    !(cnp->cn_flags & ISDOTDOT)));
100170808Sdelphij
101197953Sdelphij	TMPFS_ASSERT_LOCKED(dnode);
102197953Sdelphij	if (dnode->tn_dir.tn_parent == NULL) {
103197953Sdelphij		error = ENOENT;
104197953Sdelphij		goto out;
105197953Sdelphij	}
106170808Sdelphij	if (cnp->cn_flags & ISDOTDOT) {
107269173Skib		error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
108269173Skib		    dnode->tn_dir.tn_parent, cnp->cn_lkflags, vpp);
109269173Skib		if (error != 0)
110269173Skib			goto out;
111170808Sdelphij	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
112170808Sdelphij		VREF(dvp);
113170808Sdelphij		*vpp = dvp;
114170808Sdelphij		error = 0;
115170808Sdelphij	} else {
116188318Skib		de = tmpfs_dir_lookup(dnode, NULL, cnp);
117211598Sed		if (de != NULL && de->td_node == NULL)
118211598Sed			cnp->cn_flags |= ISWHITEOUT;
119211598Sed		if (de == NULL || de->td_node == NULL) {
120170808Sdelphij			/* The entry was not found in the directory.
121170808Sdelphij			 * This is OK if we are creating or renaming an
122170808Sdelphij			 * entry and are working on the last component of
123170808Sdelphij			 * the path name. */
124170808Sdelphij			if ((cnp->cn_flags & ISLASTCN) &&
125170808Sdelphij			    (cnp->cn_nameiop == CREATE || \
126211598Sed			    cnp->cn_nameiop == RENAME ||
127211598Sed			    (cnp->cn_nameiop == DELETE &&
128211598Sed			    cnp->cn_flags & DOWHITEOUT &&
129211598Sed			    cnp->cn_flags & ISWHITEOUT))) {
130170808Sdelphij				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
131170808Sdelphij				    cnp->cn_thread);
132170808Sdelphij				if (error != 0)
133170808Sdelphij					goto out;
134170808Sdelphij
135170808Sdelphij				/* Keep the component name in the buffer for
136170808Sdelphij				 * future uses. */
137170808Sdelphij				cnp->cn_flags |= SAVENAME;
138170808Sdelphij
139170808Sdelphij				error = EJUSTRETURN;
140170808Sdelphij			} else
141170808Sdelphij				error = ENOENT;
142170808Sdelphij		} else {
143170808Sdelphij			struct tmpfs_node *tnode;
144170808Sdelphij
145170808Sdelphij			/* The entry was found, so get its associated
146170808Sdelphij			 * tmpfs_node. */
147170808Sdelphij			tnode = de->td_node;
148170808Sdelphij
149170808Sdelphij			/* If we are not at the last path component and
150170808Sdelphij			 * found a non-directory or non-link entry (which
151170808Sdelphij			 * may itself be pointing to a directory), raise
152170808Sdelphij			 * an error. */
153170808Sdelphij			if ((tnode->tn_type != VDIR &&
154170808Sdelphij			    tnode->tn_type != VLNK) &&
155170808Sdelphij			    !(cnp->cn_flags & ISLASTCN)) {
156170808Sdelphij				error = ENOTDIR;
157170808Sdelphij				goto out;
158170808Sdelphij			}
159170808Sdelphij
160170808Sdelphij			/* If we are deleting or renaming the entry, keep
161170808Sdelphij			 * track of its tmpfs_dirent so that it can be
162170808Sdelphij			 * easily deleted later. */
163170808Sdelphij			if ((cnp->cn_flags & ISLASTCN) &&
164170808Sdelphij			    (cnp->cn_nameiop == DELETE ||
165170808Sdelphij			    cnp->cn_nameiop == RENAME)) {
166170808Sdelphij				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
167170808Sdelphij				    cnp->cn_thread);
168170808Sdelphij				if (error != 0)
169170808Sdelphij					goto out;
170171070Sdelphij
171170808Sdelphij				/* Allocate a new vnode on the matching entry. */
172171799Sdelphij				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
173269172Skib				    cnp->cn_lkflags, vpp);
174170808Sdelphij				if (error != 0)
175170808Sdelphij					goto out;
176170808Sdelphij
177170808Sdelphij				if ((dnode->tn_mode & S_ISTXT) &&
178170808Sdelphij				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
179170808Sdelphij				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
180170808Sdelphij					error = EPERM;
181170808Sdelphij					vput(*vpp);
182170808Sdelphij					*vpp = NULL;
183170808Sdelphij					goto out;
184171070Sdelphij				}
185170808Sdelphij				cnp->cn_flags |= SAVENAME;
186171799Sdelphij			} else {
187171799Sdelphij				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
188269176Skib				    cnp->cn_lkflags, vpp);
189269176Skib				if (error != 0)
190269176Skib					goto out;
191170808Sdelphij			}
192170808Sdelphij		}
193170808Sdelphij	}
194170808Sdelphij
195170808Sdelphij	/* Store the result of this lookup in the cache.  Avoid this if the
196170808Sdelphij	 * request was for creation, as it does not improve timings on
197170808Sdelphij	 * emprical tests. */
198276500Skib	if ((cnp->cn_flags & MAKEENTRY) != 0)
199170808Sdelphij		cache_enter(dvp, *vpp, cnp);
200170808Sdelphij
201170808Sdelphijout:
202170808Sdelphij	/* If there were no errors, *vpp cannot be null and it must be
203170808Sdelphij	 * locked. */
204176559Sattilio	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
205170808Sdelphij
206170808Sdelphij	return error;
207170808Sdelphij}
208170808Sdelphij
209171069Sdelphijstatic int
210170808Sdelphijtmpfs_create(struct vop_create_args *v)
211170808Sdelphij{
212170808Sdelphij	struct vnode *dvp = v->a_dvp;
213170808Sdelphij	struct vnode **vpp = v->a_vpp;
214170808Sdelphij	struct componentname *cnp = v->a_cnp;
215170808Sdelphij	struct vattr *vap = v->a_vap;
216276648Skib	int error;
217170808Sdelphij
218170808Sdelphij	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
219170808Sdelphij
220276648Skib	error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
221276648Skib	if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
222276648Skib		cache_enter(dvp, *vpp, cnp);
223276648Skib	return (error);
224170808Sdelphij}
225170808Sdelphij
226171069Sdelphijstatic int
227170808Sdelphijtmpfs_mknod(struct vop_mknod_args *v)
228170808Sdelphij{
229170808Sdelphij	struct vnode *dvp = v->a_dvp;
230170808Sdelphij	struct vnode **vpp = v->a_vpp;
231170808Sdelphij	struct componentname *cnp = v->a_cnp;
232170808Sdelphij	struct vattr *vap = v->a_vap;
233170808Sdelphij
234170808Sdelphij	if (vap->va_type != VBLK && vap->va_type != VCHR &&
235170808Sdelphij	    vap->va_type != VFIFO)
236170808Sdelphij		return EINVAL;
237170808Sdelphij
238170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
239170808Sdelphij}
240170808Sdelphij
241171069Sdelphijstatic int
242170808Sdelphijtmpfs_open(struct vop_open_args *v)
243170808Sdelphij{
244170808Sdelphij	struct vnode *vp = v->a_vp;
245170808Sdelphij	int mode = v->a_mode;
246170808Sdelphij
247170808Sdelphij	int error;
248170808Sdelphij	struct tmpfs_node *node;
249170808Sdelphij
250176559Sattilio	MPASS(VOP_ISLOCKED(vp));
251170808Sdelphij
252170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
253171070Sdelphij
254170808Sdelphij	/* The file is still active but all its names have been removed
255170808Sdelphij	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
256170808Sdelphij	 * it is about to die. */
257170808Sdelphij	if (node->tn_links < 1)
258170808Sdelphij		return (ENOENT);
259170808Sdelphij
260170808Sdelphij	/* If the file is marked append-only, deny write requests. */
261170808Sdelphij	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
262170808Sdelphij		error = EPERM;
263170808Sdelphij	else {
264170808Sdelphij		error = 0;
265250190Skib		/* For regular files, the call below is nop. */
266269168Skib		KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
267269168Skib		    OBJ_DEAD) == 0, ("dead object"));
268171070Sdelphij		vnode_create_vobject(vp, node->tn_size, v->a_td);
269170808Sdelphij	}
270170808Sdelphij
271176559Sattilio	MPASS(VOP_ISLOCKED(vp));
272170808Sdelphij	return error;
273170808Sdelphij}
274170808Sdelphij
275171069Sdelphijstatic int
276170808Sdelphijtmpfs_close(struct vop_close_args *v)
277170808Sdelphij{
278170808Sdelphij	struct vnode *vp = v->a_vp;
279170808Sdelphij
280218949Salc	/* Update node times. */
281218949Salc	tmpfs_update(vp);
282170808Sdelphij
283218949Salc	return (0);
284170808Sdelphij}
285170808Sdelphij
286170808Sdelphijint
287170808Sdelphijtmpfs_access(struct vop_access_args *v)
288170808Sdelphij{
289170808Sdelphij	struct vnode *vp = v->a_vp;
290184413Strasz	accmode_t accmode = v->a_accmode;
291170808Sdelphij	struct ucred *cred = v->a_cred;
292170808Sdelphij
293170808Sdelphij	int error;
294170808Sdelphij	struct tmpfs_node *node;
295170808Sdelphij
296176559Sattilio	MPASS(VOP_ISLOCKED(vp));
297170808Sdelphij
298170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
299170808Sdelphij
300170808Sdelphij	switch (vp->v_type) {
301170808Sdelphij	case VDIR:
302170808Sdelphij		/* FALLTHROUGH */
303170808Sdelphij	case VLNK:
304170808Sdelphij		/* FALLTHROUGH */
305170808Sdelphij	case VREG:
306184413Strasz		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
307170808Sdelphij			error = EROFS;
308170808Sdelphij			goto out;
309170808Sdelphij		}
310170808Sdelphij		break;
311170808Sdelphij
312170808Sdelphij	case VBLK:
313170808Sdelphij		/* FALLTHROUGH */
314170808Sdelphij	case VCHR:
315170808Sdelphij		/* FALLTHROUGH */
316170808Sdelphij	case VSOCK:
317170808Sdelphij		/* FALLTHROUGH */
318170808Sdelphij	case VFIFO:
319170808Sdelphij		break;
320170808Sdelphij
321170808Sdelphij	default:
322170808Sdelphij		error = EINVAL;
323170808Sdelphij		goto out;
324170808Sdelphij	}
325170808Sdelphij
326184413Strasz	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
327170808Sdelphij		error = EPERM;
328170808Sdelphij		goto out;
329170808Sdelphij	}
330170808Sdelphij
331170808Sdelphij	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
332184413Strasz	    node->tn_gid, accmode, cred, NULL);
333170808Sdelphij
334170808Sdelphijout:
335176559Sattilio	MPASS(VOP_ISLOCKED(vp));
336170808Sdelphij
337170808Sdelphij	return error;
338170808Sdelphij}
339170808Sdelphij
340170808Sdelphijint
341170808Sdelphijtmpfs_getattr(struct vop_getattr_args *v)
342170808Sdelphij{
343170808Sdelphij	struct vnode *vp = v->a_vp;
344170808Sdelphij	struct vattr *vap = v->a_vap;
345170808Sdelphij
346170808Sdelphij	struct tmpfs_node *node;
347170808Sdelphij
348170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
349170808Sdelphij
350170808Sdelphij	tmpfs_update(vp);
351170808Sdelphij
352170808Sdelphij	vap->va_type = vp->v_type;
353170808Sdelphij	vap->va_mode = node->tn_mode;
354170808Sdelphij	vap->va_nlink = node->tn_links;
355170808Sdelphij	vap->va_uid = node->tn_uid;
356170808Sdelphij	vap->va_gid = node->tn_gid;
357170808Sdelphij	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
358170808Sdelphij	vap->va_fileid = node->tn_id;
359170808Sdelphij	vap->va_size = node->tn_size;
360170808Sdelphij	vap->va_blocksize = PAGE_SIZE;
361170808Sdelphij	vap->va_atime = node->tn_atime;
362170808Sdelphij	vap->va_mtime = node->tn_mtime;
363170808Sdelphij	vap->va_ctime = node->tn_ctime;
364170808Sdelphij	vap->va_birthtime = node->tn_birthtime;
365170808Sdelphij	vap->va_gen = node->tn_gen;
366170808Sdelphij	vap->va_flags = node->tn_flags;
367170808Sdelphij	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
368183214Skib		node->tn_rdev : NODEV;
369170808Sdelphij	vap->va_bytes = round_page(node->tn_size);
370183212Skib	vap->va_filerev = 0;
371170808Sdelphij
372170808Sdelphij	return 0;
373170808Sdelphij}
374170808Sdelphij
375170808Sdelphijint
376170808Sdelphijtmpfs_setattr(struct vop_setattr_args *v)
377170808Sdelphij{
378170808Sdelphij	struct vnode *vp = v->a_vp;
379170808Sdelphij	struct vattr *vap = v->a_vap;
380170808Sdelphij	struct ucred *cred = v->a_cred;
381182371Sattilio	struct thread *td = curthread;
382170808Sdelphij
383170808Sdelphij	int error;
384170808Sdelphij
385176559Sattilio	MPASS(VOP_ISLOCKED(vp));
386170808Sdelphij
387170808Sdelphij	error = 0;
388170808Sdelphij
389170808Sdelphij	/* Abort if any unsettable attribute is given. */
390170808Sdelphij	if (vap->va_type != VNON ||
391170808Sdelphij	    vap->va_nlink != VNOVAL ||
392170808Sdelphij	    vap->va_fsid != VNOVAL ||
393170808Sdelphij	    vap->va_fileid != VNOVAL ||
394170808Sdelphij	    vap->va_blocksize != VNOVAL ||
395170808Sdelphij	    vap->va_gen != VNOVAL ||
396170808Sdelphij	    vap->va_rdev != VNOVAL ||
397170808Sdelphij	    vap->va_bytes != VNOVAL)
398170808Sdelphij		error = EINVAL;
399170808Sdelphij
400170808Sdelphij	if (error == 0 && (vap->va_flags != VNOVAL))
401182371Sattilio		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
402170808Sdelphij
403170808Sdelphij	if (error == 0 && (vap->va_size != VNOVAL))
404182371Sattilio		error = tmpfs_chsize(vp, vap->va_size, cred, td);
405170808Sdelphij
406170808Sdelphij	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
407182371Sattilio		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
408170808Sdelphij
409170808Sdelphij	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
410182371Sattilio		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
411170808Sdelphij
412170808Sdelphij	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
413170808Sdelphij	    vap->va_atime.tv_nsec != VNOVAL) ||
414170808Sdelphij	    (vap->va_mtime.tv_sec != VNOVAL &&
415170808Sdelphij	    vap->va_mtime.tv_nsec != VNOVAL) ||
416170808Sdelphij	    (vap->va_birthtime.tv_sec != VNOVAL &&
417170808Sdelphij	    vap->va_birthtime.tv_nsec != VNOVAL)))
418267816Skib		error = tmpfs_chtimes(vp, vap, cred, td);
419170808Sdelphij
420170808Sdelphij	/* Update the node times.  We give preference to the error codes
421170808Sdelphij	 * generated by this function rather than the ones that may arise
422170808Sdelphij	 * from tmpfs_update. */
423170808Sdelphij	tmpfs_update(vp);
424170808Sdelphij
425176559Sattilio	MPASS(VOP_ISLOCKED(vp));
426170808Sdelphij
427170808Sdelphij	return error;
428170808Sdelphij}
429170808Sdelphij
430197850Sdelphijstatic int
431170808Sdelphijtmpfs_read(struct vop_read_args *v)
432170808Sdelphij{
433254601Skib	struct vnode *vp;
434254601Skib	struct uio *uio;
435170808Sdelphij	struct tmpfs_node *node;
436170808Sdelphij
437254601Skib	vp = v->a_vp;
438254601Skib	if (vp->v_type != VREG)
439254601Skib		return (EISDIR);
440254601Skib	uio = v->a_uio;
441254601Skib	if (uio->uio_offset < 0)
442254601Skib		return (EINVAL);
443170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
444170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
445254601Skib	return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
446170808Sdelphij}
447170808Sdelphij
448171069Sdelphijstatic int
449170808Sdelphijtmpfs_write(struct vop_write_args *v)
450170808Sdelphij{
451254601Skib	struct vnode *vp;
452254601Skib	struct uio *uio;
453254601Skib	struct tmpfs_node *node;
454254601Skib	off_t oldsize;
455254601Skib	int error, ioflag;
456170808Sdelphij
457254601Skib	vp = v->a_vp;
458254601Skib	uio = v->a_uio;
459254601Skib	ioflag = v->a_ioflag;
460254601Skib	error = 0;
461170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
462170808Sdelphij	oldsize = node->tn_size;
463170808Sdelphij
464254601Skib	if (uio->uio_offset < 0 || vp->v_type != VREG)
465254601Skib		return (EINVAL);
466254601Skib	if (uio->uio_resid == 0)
467254601Skib		return (0);
468170808Sdelphij	if (ioflag & IO_APPEND)
469170808Sdelphij		uio->uio_offset = node->tn_size;
470171070Sdelphij	if (uio->uio_offset + uio->uio_resid >
471170808Sdelphij	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
472170808Sdelphij		return (EFBIG);
473207719Strasz	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
474207662Strasz		return (EFBIG);
475278571Skib	if (uio->uio_offset + uio->uio_resid > node->tn_size) {
476230180Salc		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
477230180Salc		    FALSE);
478170808Sdelphij		if (error != 0)
479170808Sdelphij			goto out;
480170808Sdelphij	}
481170808Sdelphij
482254601Skib	error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
483170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
484278571Skib	    TMPFS_NODE_CHANGED;
485170808Sdelphij	if (node->tn_mode & (S_ISUID | S_ISGID)) {
486170808Sdelphij		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
487170808Sdelphij			node->tn_mode &= ~(S_ISUID | S_ISGID);
488170808Sdelphij	}
489170808Sdelphij	if (error != 0)
490230180Salc		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
491170808Sdelphij
492170808Sdelphijout:
493170808Sdelphij	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
494170808Sdelphij	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
495170808Sdelphij
496254601Skib	return (error);
497170808Sdelphij}
498170808Sdelphij
499171069Sdelphijstatic int
500170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v)
501170808Sdelphij{
502170808Sdelphij	struct vnode *vp = v->a_vp;
503170808Sdelphij
504176559Sattilio	MPASS(VOP_ISLOCKED(vp));
505170808Sdelphij
506278571Skib	tmpfs_check_mtime(vp);
507170808Sdelphij	tmpfs_update(vp);
508170808Sdelphij
509170808Sdelphij	return 0;
510170808Sdelphij}
511170808Sdelphij
512171069Sdelphijstatic int
513170808Sdelphijtmpfs_remove(struct vop_remove_args *v)
514170808Sdelphij{
515170808Sdelphij	struct vnode *dvp = v->a_dvp;
516170808Sdelphij	struct vnode *vp = v->a_vp;
517170808Sdelphij
518170808Sdelphij	int error;
519170808Sdelphij	struct tmpfs_dirent *de;
520170808Sdelphij	struct tmpfs_mount *tmp;
521170808Sdelphij	struct tmpfs_node *dnode;
522170808Sdelphij	struct tmpfs_node *node;
523170808Sdelphij
524176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
525176559Sattilio	MPASS(VOP_ISLOCKED(vp));
526170808Sdelphij
527170808Sdelphij	if (vp->v_type == VDIR) {
528170808Sdelphij		error = EISDIR;
529170808Sdelphij		goto out;
530170808Sdelphij	}
531170808Sdelphij
532170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
533170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
534170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
535188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
536170808Sdelphij	MPASS(de != NULL);
537170808Sdelphij
538170808Sdelphij	/* Files marked as immutable or append-only cannot be deleted. */
539170808Sdelphij	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
540170808Sdelphij	    (dnode->tn_flags & APPEND)) {
541170808Sdelphij		error = EPERM;
542170808Sdelphij		goto out;
543170808Sdelphij	}
544170808Sdelphij
545170808Sdelphij	/* Remove the entry from the directory; as it is a file, we do not
546170808Sdelphij	 * have to change the number of hard links of the directory. */
547170808Sdelphij	tmpfs_dir_detach(dvp, de);
548211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
549211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
550170808Sdelphij
551170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
552170808Sdelphij	 * referred by it will not be removed until the vnode is really
553170808Sdelphij	 * reclaimed. */
554245115Sgleb	tmpfs_free_dirent(tmp, de);
555170808Sdelphij
556218949Salc	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
557170808Sdelphij	error = 0;
558170808Sdelphij
559170808Sdelphijout:
560170808Sdelphij
561170808Sdelphij	return error;
562170808Sdelphij}
563170808Sdelphij
564171069Sdelphijstatic int
565170808Sdelphijtmpfs_link(struct vop_link_args *v)
566170808Sdelphij{
567170808Sdelphij	struct vnode *dvp = v->a_tdvp;
568170808Sdelphij	struct vnode *vp = v->a_vp;
569170808Sdelphij	struct componentname *cnp = v->a_cnp;
570170808Sdelphij
571170808Sdelphij	int error;
572170808Sdelphij	struct tmpfs_dirent *de;
573170808Sdelphij	struct tmpfs_node *node;
574170808Sdelphij
575176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
576170808Sdelphij	MPASS(cnp->cn_flags & HASBUF);
577170808Sdelphij	MPASS(dvp != vp); /* XXX When can this be false? */
578269167Skib	node = VP_TO_TMPFS_NODE(vp);
579269167Skib
580170808Sdelphij	/* Ensure that we do not overflow the maximum number of links imposed
581170808Sdelphij	 * by the system. */
582170808Sdelphij	MPASS(node->tn_links <= LINK_MAX);
583170808Sdelphij	if (node->tn_links == LINK_MAX) {
584170808Sdelphij		error = EMLINK;
585170808Sdelphij		goto out;
586170808Sdelphij	}
587170808Sdelphij
588170808Sdelphij	/* We cannot create links of files marked immutable or append-only. */
589170808Sdelphij	if (node->tn_flags & (IMMUTABLE | APPEND)) {
590170808Sdelphij		error = EPERM;
591170808Sdelphij		goto out;
592170808Sdelphij	}
593170808Sdelphij
594170808Sdelphij	/* Allocate a new directory entry to represent the node. */
595170808Sdelphij	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
596170808Sdelphij	    cnp->cn_nameptr, cnp->cn_namelen, &de);
597170808Sdelphij	if (error != 0)
598170808Sdelphij		goto out;
599170808Sdelphij
600170808Sdelphij	/* Insert the new directory entry into the appropriate directory. */
601211598Sed	if (cnp->cn_flags & ISWHITEOUT)
602211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
603170808Sdelphij	tmpfs_dir_attach(dvp, de);
604170808Sdelphij
605170808Sdelphij	/* vp link count has changed, so update node times. */
606170808Sdelphij	node->tn_status |= TMPFS_NODE_CHANGED;
607170808Sdelphij	tmpfs_update(vp);
608170808Sdelphij
609170808Sdelphij	error = 0;
610171070Sdelphij
611170808Sdelphijout:
612170808Sdelphij	return error;
613170808Sdelphij}
614170808Sdelphij
615232960Sgleb/*
616232960Sgleb * We acquire all but fdvp locks using non-blocking acquisitions.  If we
617232960Sgleb * fail to acquire any lock in the path we will drop all held locks,
618232960Sgleb * acquire the new lock in a blocking fashion, and then release it and
619232960Sgleb * restart the rename.  This acquire/release step ensures that we do not
620232960Sgleb * spin on a lock waiting for release.  On error release all vnode locks
621232960Sgleb * and decrement references the way tmpfs_rename() would do.
622232960Sgleb */
623171069Sdelphijstatic int
624232960Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
625232960Sgleb    struct vnode *tdvp, struct vnode **tvpp,
626232960Sgleb    struct componentname *fcnp, struct componentname *tcnp)
627232960Sgleb{
628232960Sgleb	struct vnode *nvp;
629232960Sgleb	struct mount *mp;
630232960Sgleb	struct tmpfs_dirent *de;
631232960Sgleb	int error, restarts = 0;
632232960Sgleb
633232960Sgleb	VOP_UNLOCK(tdvp, 0);
634232960Sgleb	if (*tvpp != NULL && *tvpp != tdvp)
635232960Sgleb		VOP_UNLOCK(*tvpp, 0);
636232960Sgleb	mp = fdvp->v_mount;
637232960Sgleb
638232960Sglebrelock:
639232960Sgleb	restarts += 1;
640232960Sgleb	error = vn_lock(fdvp, LK_EXCLUSIVE);
641232960Sgleb	if (error)
642232960Sgleb		goto releout;
643232960Sgleb	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
644232960Sgleb		VOP_UNLOCK(fdvp, 0);
645232960Sgleb		error = vn_lock(tdvp, LK_EXCLUSIVE);
646232960Sgleb		if (error)
647232960Sgleb			goto releout;
648232960Sgleb		VOP_UNLOCK(tdvp, 0);
649232960Sgleb		goto relock;
650232960Sgleb	}
651232960Sgleb	/*
652232960Sgleb	 * Re-resolve fvp to be certain it still exists and fetch the
653232960Sgleb	 * correct vnode.
654232960Sgleb	 */
655232960Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
656232960Sgleb	if (de == NULL) {
657232960Sgleb		VOP_UNLOCK(fdvp, 0);
658232960Sgleb		VOP_UNLOCK(tdvp, 0);
659232960Sgleb		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
660232960Sgleb		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
661232960Sgleb			error = EINVAL;
662232960Sgleb		else
663232960Sgleb			error = ENOENT;
664232960Sgleb		goto releout;
665232960Sgleb	}
666232960Sgleb	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
667232960Sgleb	if (error != 0) {
668232960Sgleb		VOP_UNLOCK(fdvp, 0);
669232960Sgleb		VOP_UNLOCK(tdvp, 0);
670232960Sgleb		if (error != EBUSY)
671232960Sgleb			goto releout;
672232960Sgleb		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
673232960Sgleb		if (error != 0)
674232960Sgleb			goto releout;
675232960Sgleb		VOP_UNLOCK(nvp, 0);
676232960Sgleb		/*
677232960Sgleb		 * Concurrent rename race.
678232960Sgleb		 */
679232960Sgleb		if (nvp == tdvp) {
680232960Sgleb			vrele(nvp);
681232960Sgleb			error = EINVAL;
682232960Sgleb			goto releout;
683232960Sgleb		}
684232960Sgleb		vrele(*fvpp);
685232960Sgleb		*fvpp = nvp;
686232960Sgleb		goto relock;
687232960Sgleb	}
688232960Sgleb	vrele(*fvpp);
689232960Sgleb	*fvpp = nvp;
690232960Sgleb	VOP_UNLOCK(*fvpp, 0);
691232960Sgleb	/*
692232960Sgleb	 * Re-resolve tvp and acquire the vnode lock if present.
693232960Sgleb	 */
694232960Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
695232960Sgleb	/*
696232960Sgleb	 * If tvp disappeared we just carry on.
697232960Sgleb	 */
698232960Sgleb	if (de == NULL && *tvpp != NULL) {
699232960Sgleb		vrele(*tvpp);
700232960Sgleb		*tvpp = NULL;
701232960Sgleb	}
702232960Sgleb	/*
703232960Sgleb	 * Get the tvp ino if the lookup succeeded.  We may have to restart
704232960Sgleb	 * if the non-blocking acquire fails.
705232960Sgleb	 */
706232960Sgleb	if (de != NULL) {
707232960Sgleb		nvp = NULL;
708232960Sgleb		error = tmpfs_alloc_vp(mp, de->td_node,
709232960Sgleb		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
710232960Sgleb		if (*tvpp != NULL)
711232960Sgleb			vrele(*tvpp);
712232960Sgleb		*tvpp = nvp;
713232960Sgleb		if (error != 0) {
714232960Sgleb			VOP_UNLOCK(fdvp, 0);
715232960Sgleb			VOP_UNLOCK(tdvp, 0);
716232960Sgleb			if (error != EBUSY)
717232960Sgleb				goto releout;
718232960Sgleb			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
719232960Sgleb			    &nvp);
720232960Sgleb			if (error != 0)
721232960Sgleb				goto releout;
722232960Sgleb			VOP_UNLOCK(nvp, 0);
723232960Sgleb			/*
724232960Sgleb			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
725232960Sgleb			 */
726232960Sgleb			if (nvp == fdvp) {
727232960Sgleb				error = ENOTEMPTY;
728232960Sgleb				goto releout;
729232960Sgleb			}
730232960Sgleb			goto relock;
731232960Sgleb		}
732232960Sgleb	}
733232960Sgleb	tmpfs_rename_restarts += restarts;
734232960Sgleb
735232960Sgleb	return (0);
736232960Sgleb
737232960Sglebreleout:
738232960Sgleb	vrele(fdvp);
739232960Sgleb	vrele(*fvpp);
740232960Sgleb	vrele(tdvp);
741232960Sgleb	if (*tvpp != NULL)
742232960Sgleb		vrele(*tvpp);
743232960Sgleb	tmpfs_rename_restarts += restarts;
744232960Sgleb
745232960Sgleb	return (error);
746232960Sgleb}
747232960Sgleb
748232960Sglebstatic int
749170808Sdelphijtmpfs_rename(struct vop_rename_args *v)
750170808Sdelphij{
751170808Sdelphij	struct vnode *fdvp = v->a_fdvp;
752170808Sdelphij	struct vnode *fvp = v->a_fvp;
753170808Sdelphij	struct componentname *fcnp = v->a_fcnp;
754170808Sdelphij	struct vnode *tdvp = v->a_tdvp;
755170808Sdelphij	struct vnode *tvp = v->a_tvp;
756170808Sdelphij	struct componentname *tcnp = v->a_tcnp;
757232960Sgleb	struct mount *mp = NULL;
758170808Sdelphij
759170808Sdelphij	char *newname;
760170808Sdelphij	int error;
761170808Sdelphij	struct tmpfs_dirent *de;
762197953Sdelphij	struct tmpfs_mount *tmp;
763170808Sdelphij	struct tmpfs_node *fdnode;
764170808Sdelphij	struct tmpfs_node *fnode;
765171799Sdelphij	struct tmpfs_node *tnode;
766170808Sdelphij	struct tmpfs_node *tdnode;
767170808Sdelphij
768176559Sattilio	MPASS(VOP_ISLOCKED(tdvp));
769176559Sattilio	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
770170808Sdelphij	MPASS(fcnp->cn_flags & HASBUF);
771170808Sdelphij	MPASS(tcnp->cn_flags & HASBUF);
772170808Sdelphij
773170808Sdelphij	/* Disallow cross-device renames.
774170808Sdelphij	 * XXX Why isn't this done by the caller? */
775170808Sdelphij	if (fvp->v_mount != tdvp->v_mount ||
776170808Sdelphij	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
777170808Sdelphij		error = EXDEV;
778170808Sdelphij		goto out;
779170808Sdelphij	}
780170808Sdelphij
781170808Sdelphij	/* If source and target are the same file, there is nothing to do. */
782170808Sdelphij	if (fvp == tvp) {
783170808Sdelphij		error = 0;
784170808Sdelphij		goto out;
785170808Sdelphij	}
786170808Sdelphij
787173725Sdelphij	/* If we need to move the directory between entries, lock the
788173725Sdelphij	 * source so that we can safely operate on it. */
789232960Sgleb	if (fdvp != tdvp && fdvp != tvp) {
790232960Sgleb		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
791232960Sgleb			mp = tdvp->v_mount;
792232960Sgleb			error = vfs_busy(mp, 0);
793232960Sgleb			if (error != 0) {
794232960Sgleb				mp = NULL;
795232960Sgleb				goto out;
796232960Sgleb			}
797232960Sgleb			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
798232960Sgleb			    fcnp, tcnp);
799232960Sgleb			if (error != 0) {
800232960Sgleb				vfs_unbusy(mp);
801232960Sgleb				return (error);
802232960Sgleb			}
803232960Sgleb			ASSERT_VOP_ELOCKED(fdvp,
804232960Sgleb			    "tmpfs_rename: fdvp not locked");
805232960Sgleb			ASSERT_VOP_ELOCKED(tdvp,
806232960Sgleb			    "tmpfs_rename: tdvp not locked");
807232960Sgleb			if (tvp != NULL)
808232960Sgleb				ASSERT_VOP_ELOCKED(tvp,
809232960Sgleb				    "tmpfs_rename: tvp not locked");
810232960Sgleb			if (fvp == tvp) {
811232960Sgleb				error = 0;
812232960Sgleb				goto out_locked;
813232960Sgleb			}
814232960Sgleb		}
815232960Sgleb	}
816232960Sgleb
817232960Sgleb	tmp = VFS_TO_TMPFS(tdvp->v_mount);
818232960Sgleb	tdnode = VP_TO_TMPFS_DIR(tdvp);
819232960Sgleb	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
820173725Sdelphij	fdnode = VP_TO_TMPFS_DIR(fdvp);
821173725Sdelphij	fnode = VP_TO_TMPFS_NODE(fvp);
822188318Skib	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
823173725Sdelphij
824212305Sivoras	/* Entry can disappear before we lock fdvp,
825212305Sivoras	 * also avoid manipulating '.' and '..' entries. */
826170808Sdelphij	if (de == NULL) {
827212305Sivoras		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
828212305Sivoras		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
829212305Sivoras			error = EINVAL;
830212305Sivoras		else
831212305Sivoras			error = ENOENT;
832173725Sdelphij		goto out_locked;
833170808Sdelphij	}
834170808Sdelphij	MPASS(de->td_node == fnode);
835170808Sdelphij
836171070Sdelphij	/* If re-naming a directory to another preexisting directory
837170808Sdelphij	 * ensure that the target directory is empty so that its
838171070Sdelphij	 * removal causes no side effects.
839170808Sdelphij	 * Kern_rename gurantees the destination to be a directory
840170808Sdelphij	 * if the source is one. */
841170808Sdelphij	if (tvp != NULL) {
842171799Sdelphij		MPASS(tnode != NULL);
843171070Sdelphij
844170808Sdelphij		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
845170808Sdelphij		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
846170808Sdelphij			error = EPERM;
847173725Sdelphij			goto out_locked;
848170808Sdelphij		}
849170808Sdelphij
850171799Sdelphij		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
851171799Sdelphij			if (tnode->tn_size > 0) {
852171799Sdelphij				error = ENOTEMPTY;
853173725Sdelphij				goto out_locked;
854171799Sdelphij			}
855171799Sdelphij		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
856171799Sdelphij			error = ENOTDIR;
857173725Sdelphij			goto out_locked;
858171799Sdelphij		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
859171799Sdelphij			error = EISDIR;
860173725Sdelphij			goto out_locked;
861171799Sdelphij		} else {
862171799Sdelphij			MPASS(fnode->tn_type != VDIR &&
863171799Sdelphij				tnode->tn_type != VDIR);
864170808Sdelphij		}
865170808Sdelphij	}
866170808Sdelphij
867170808Sdelphij	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
868170808Sdelphij	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
869170808Sdelphij		error = EPERM;
870170808Sdelphij		goto out_locked;
871170808Sdelphij	}
872170808Sdelphij
873170808Sdelphij	/* Ensure that we have enough memory to hold the new name, if it
874170808Sdelphij	 * has to be changed. */
875170808Sdelphij	if (fcnp->cn_namelen != tcnp->cn_namelen ||
876183299Sobrien	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
877171087Sdelphij		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
878170808Sdelphij	} else
879170808Sdelphij		newname = NULL;
880170808Sdelphij
881170808Sdelphij	/* If the node is being moved to another directory, we have to do
882170808Sdelphij	 * the move. */
883170808Sdelphij	if (fdnode != tdnode) {
884170808Sdelphij		/* In case we are moving a directory, we have to adjust its
885170808Sdelphij		 * parent to point to the new parent. */
886170808Sdelphij		if (de->td_node->tn_type == VDIR) {
887170808Sdelphij			struct tmpfs_node *n;
888170808Sdelphij
889170808Sdelphij			/* Ensure the target directory is not a child of the
890170808Sdelphij			 * directory being moved.  Otherwise, we'd end up
891170808Sdelphij			 * with stale nodes. */
892170808Sdelphij			n = tdnode;
893197953Sdelphij			/* TMPFS_LOCK garanties that no nodes are freed while
894197953Sdelphij			 * traversing the list. Nodes can only be marked as
895197953Sdelphij			 * removed: tn_parent == NULL. */
896197953Sdelphij			TMPFS_LOCK(tmp);
897197953Sdelphij			TMPFS_NODE_LOCK(n);
898170808Sdelphij			while (n != n->tn_dir.tn_parent) {
899197953Sdelphij				struct tmpfs_node *parent;
900197953Sdelphij
901170808Sdelphij				if (n == fnode) {
902197953Sdelphij					TMPFS_NODE_UNLOCK(n);
903197953Sdelphij					TMPFS_UNLOCK(tmp);
904170808Sdelphij					error = EINVAL;
905170808Sdelphij					if (newname != NULL)
906171087Sdelphij						    free(newname, M_TMPFSNAME);
907170808Sdelphij					goto out_locked;
908170808Sdelphij				}
909197953Sdelphij				parent = n->tn_dir.tn_parent;
910197953Sdelphij				TMPFS_NODE_UNLOCK(n);
911197953Sdelphij				if (parent == NULL) {
912197953Sdelphij					n = NULL;
913197953Sdelphij					break;
914197953Sdelphij				}
915197953Sdelphij				TMPFS_NODE_LOCK(parent);
916197953Sdelphij				if (parent->tn_dir.tn_parent == NULL) {
917197953Sdelphij					TMPFS_NODE_UNLOCK(parent);
918197953Sdelphij					n = NULL;
919197953Sdelphij					break;
920197953Sdelphij				}
921197953Sdelphij				n = parent;
922170808Sdelphij			}
923197953Sdelphij			TMPFS_UNLOCK(tmp);
924197953Sdelphij			if (n == NULL) {
925197953Sdelphij				error = EINVAL;
926197953Sdelphij				if (newname != NULL)
927197953Sdelphij					    free(newname, M_TMPFSNAME);
928197953Sdelphij				goto out_locked;
929197953Sdelphij			}
930197953Sdelphij			TMPFS_NODE_UNLOCK(n);
931170808Sdelphij
932170808Sdelphij			/* Adjust the parent pointer. */
933170808Sdelphij			TMPFS_VALIDATE_DIR(fnode);
934197953Sdelphij			TMPFS_NODE_LOCK(de->td_node);
935170808Sdelphij			de->td_node->tn_dir.tn_parent = tdnode;
936197953Sdelphij			TMPFS_NODE_UNLOCK(de->td_node);
937170808Sdelphij
938170808Sdelphij			/* As a result of changing the target of the '..'
939170808Sdelphij			 * entry, the link count of the source and target
940170808Sdelphij			 * directories has to be adjusted. */
941197953Sdelphij			TMPFS_NODE_LOCK(tdnode);
942197953Sdelphij			TMPFS_ASSERT_LOCKED(tdnode);
943197953Sdelphij			tdnode->tn_links++;
944197953Sdelphij			TMPFS_NODE_UNLOCK(tdnode);
945197953Sdelphij
946197953Sdelphij			TMPFS_NODE_LOCK(fdnode);
947197953Sdelphij			TMPFS_ASSERT_LOCKED(fdnode);
948170808Sdelphij			fdnode->tn_links--;
949197953Sdelphij			TMPFS_NODE_UNLOCK(fdnode);
950170808Sdelphij		}
951170808Sdelphij	}
952170808Sdelphij
953245115Sgleb	/* Do the move: just remove the entry from the source directory
954245115Sgleb	 * and insert it into the target one. */
955245115Sgleb	tmpfs_dir_detach(fdvp, de);
956245115Sgleb
957245115Sgleb	if (fcnp->cn_flags & DOWHITEOUT)
958245115Sgleb		tmpfs_dir_whiteout_add(fdvp, fcnp);
959245115Sgleb	if (tcnp->cn_flags & ISWHITEOUT)
960245115Sgleb		tmpfs_dir_whiteout_remove(tdvp, tcnp);
961245115Sgleb
962170808Sdelphij	/* If the name has changed, we need to make it effective by changing
963170808Sdelphij	 * it in the directory entry. */
964170808Sdelphij	if (newname != NULL) {
965170808Sdelphij		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
966170808Sdelphij
967245115Sgleb		free(de->ud.td_name, M_TMPFSNAME);
968245115Sgleb		de->ud.td_name = newname;
969245115Sgleb		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
970170808Sdelphij
971170808Sdelphij		fnode->tn_status |= TMPFS_NODE_CHANGED;
972170808Sdelphij		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
973170808Sdelphij	}
974170808Sdelphij
975170808Sdelphij	/* If we are overwriting an entry, we have to remove the old one
976170808Sdelphij	 * from the target directory. */
977170808Sdelphij	if (tvp != NULL) {
978245115Sgleb		struct tmpfs_dirent *tde;
979245115Sgleb
980170808Sdelphij		/* Remove the old entry from the target directory. */
981245115Sgleb		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
982245115Sgleb		tmpfs_dir_detach(tdvp, tde);
983170808Sdelphij
984170808Sdelphij		/* Free the directory entry we just deleted.  Note that the
985170808Sdelphij		 * node referred by it will not be removed until the vnode is
986170808Sdelphij		 * really reclaimed. */
987245115Sgleb		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
988170808Sdelphij	}
989245115Sgleb
990245115Sgleb	tmpfs_dir_attach(tdvp, de);
991245115Sgleb
992226987Spho	cache_purge(fvp);
993232401Sjhb	if (tvp != NULL)
994232401Sjhb		cache_purge(tvp);
995248422Skib	cache_purge_negative(tdvp);
996170808Sdelphij
997170808Sdelphij	error = 0;
998170808Sdelphij
999170808Sdelphijout_locked:
1000227822Sivoras	if (fdvp != tdvp && fdvp != tvp)
1001175294Sattilio		VOP_UNLOCK(fdvp, 0);
1002170808Sdelphij
1003170808Sdelphijout:
1004170808Sdelphij	/* Release target nodes. */
1005170808Sdelphij	/* XXX: I don't understand when tdvp can be the same as tvp, but
1006170808Sdelphij	 * other code takes care of this... */
1007170808Sdelphij	if (tdvp == tvp)
1008170808Sdelphij		vrele(tdvp);
1009170808Sdelphij	else
1010170808Sdelphij		vput(tdvp);
1011170808Sdelphij	if (tvp != NULL)
1012170808Sdelphij		vput(tvp);
1013170808Sdelphij
1014170808Sdelphij	/* Release source nodes. */
1015170808Sdelphij	vrele(fdvp);
1016170808Sdelphij	vrele(fvp);
1017170808Sdelphij
1018232960Sgleb	if (mp != NULL)
1019232960Sgleb		vfs_unbusy(mp);
1020232960Sgleb
1021170808Sdelphij	return error;
1022170808Sdelphij}
1023170808Sdelphij
1024171069Sdelphijstatic int
1025170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v)
1026170808Sdelphij{
1027170808Sdelphij	struct vnode *dvp = v->a_dvp;
1028170808Sdelphij	struct vnode **vpp = v->a_vpp;
1029170808Sdelphij	struct componentname *cnp = v->a_cnp;
1030170808Sdelphij	struct vattr *vap = v->a_vap;
1031170808Sdelphij
1032170808Sdelphij	MPASS(vap->va_type == VDIR);
1033170808Sdelphij
1034170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1035170808Sdelphij}
1036170808Sdelphij
1037171069Sdelphijstatic int
1038170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v)
1039170808Sdelphij{
1040170808Sdelphij	struct vnode *dvp = v->a_dvp;
1041170808Sdelphij	struct vnode *vp = v->a_vp;
1042170808Sdelphij
1043170808Sdelphij	int error;
1044170808Sdelphij	struct tmpfs_dirent *de;
1045170808Sdelphij	struct tmpfs_mount *tmp;
1046170808Sdelphij	struct tmpfs_node *dnode;
1047170808Sdelphij	struct tmpfs_node *node;
1048170808Sdelphij
1049176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
1050176559Sattilio	MPASS(VOP_ISLOCKED(vp));
1051170808Sdelphij
1052170808Sdelphij	tmp = VFS_TO_TMPFS(dvp->v_mount);
1053170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
1054170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1055170808Sdelphij
1056171070Sdelphij	/* Directories with more than two entries ('.' and '..') cannot be
1057171070Sdelphij	 * removed. */
1058171070Sdelphij	 if (node->tn_size > 0) {
1059171070Sdelphij		 error = ENOTEMPTY;
1060171070Sdelphij		 goto out;
1061171070Sdelphij	 }
1062170808Sdelphij
1063170808Sdelphij	if ((dnode->tn_flags & APPEND)
1064170808Sdelphij	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1065170808Sdelphij		error = EPERM;
1066170808Sdelphij		goto out;
1067170808Sdelphij	}
1068170808Sdelphij
1069171070Sdelphij	/* This invariant holds only if we are not trying to remove "..".
1070171070Sdelphij	  * We checked for that above so this is safe now. */
1071170808Sdelphij	MPASS(node->tn_dir.tn_parent == dnode);
1072170808Sdelphij
1073170808Sdelphij	/* Get the directory entry associated with node (vp).  This was
1074170808Sdelphij	 * filled by tmpfs_lookup while looking up the entry. */
1075188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1076170808Sdelphij	MPASS(TMPFS_DIRENT_MATCHES(de,
1077170808Sdelphij	    v->a_cnp->cn_nameptr,
1078170808Sdelphij	    v->a_cnp->cn_namelen));
1079170808Sdelphij
1080170808Sdelphij	/* Check flags to see if we are allowed to remove the directory. */
1081170808Sdelphij	if (dnode->tn_flags & APPEND
1082170808Sdelphij		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1083170808Sdelphij		error = EPERM;
1084170808Sdelphij		goto out;
1085170808Sdelphij	}
1086170808Sdelphij
1087197953Sdelphij
1088170808Sdelphij	/* Detach the directory entry from the directory (dnode). */
1089170808Sdelphij	tmpfs_dir_detach(dvp, de);
1090211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
1091211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1092170808Sdelphij
1093197953Sdelphij	/* No vnode should be allocated for this entry from this point */
1094197953Sdelphij	TMPFS_NODE_LOCK(node);
1095197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1096170808Sdelphij	node->tn_links--;
1097197953Sdelphij	node->tn_dir.tn_parent = NULL;
1098170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1099170808Sdelphij	    TMPFS_NODE_MODIFIED;
1100197953Sdelphij
1101197953Sdelphij	TMPFS_NODE_UNLOCK(node);
1102197953Sdelphij
1103197953Sdelphij	TMPFS_NODE_LOCK(dnode);
1104197953Sdelphij	TMPFS_ASSERT_ELOCKED(dnode);
1105197953Sdelphij	dnode->tn_links--;
1106197953Sdelphij	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1107170808Sdelphij	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1108197953Sdelphij	TMPFS_NODE_UNLOCK(dnode);
1109170808Sdelphij
1110171070Sdelphij	cache_purge(dvp);
1111170808Sdelphij	cache_purge(vp);
1112170808Sdelphij
1113170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
1114170808Sdelphij	 * referred by it will not be removed until the vnode is really
1115170808Sdelphij	 * reclaimed. */
1116245115Sgleb	tmpfs_free_dirent(tmp, de);
1117170808Sdelphij
1118170808Sdelphij	/* Release the deleted vnode (will destroy the node, notify
1119170808Sdelphij	 * interested parties and clean it from the cache). */
1120170808Sdelphij
1121170808Sdelphij	dnode->tn_status |= TMPFS_NODE_CHANGED;
1122170808Sdelphij	tmpfs_update(dvp);
1123170808Sdelphij
1124170808Sdelphij	error = 0;
1125170808Sdelphij
1126170808Sdelphijout:
1127170808Sdelphij	return error;
1128170808Sdelphij}
1129170808Sdelphij
1130171069Sdelphijstatic int
1131170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v)
1132170808Sdelphij{
1133170808Sdelphij	struct vnode *dvp = v->a_dvp;
1134170808Sdelphij	struct vnode **vpp = v->a_vpp;
1135170808Sdelphij	struct componentname *cnp = v->a_cnp;
1136170808Sdelphij	struct vattr *vap = v->a_vap;
1137170808Sdelphij	char *target = v->a_target;
1138170808Sdelphij
1139170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1140170808Sdelphij	MPASS(vap->va_type == VLNK);
1141170808Sdelphij#else
1142170808Sdelphij	vap->va_type = VLNK;
1143170808Sdelphij#endif
1144170808Sdelphij
1145170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1146170808Sdelphij}
1147170808Sdelphij
1148171069Sdelphijstatic int
1149170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v)
1150170808Sdelphij{
1151170808Sdelphij	struct vnode *vp = v->a_vp;
1152170808Sdelphij	struct uio *uio = v->a_uio;
1153170808Sdelphij	int *eofflag = v->a_eofflag;
1154170808Sdelphij	u_long **cookies = v->a_cookies;
1155170808Sdelphij	int *ncookies = v->a_ncookies;
1156170808Sdelphij
1157170808Sdelphij	int error;
1158245115Sgleb	ssize_t startresid;
1159263946Sbdrewery	int maxcookies;
1160170808Sdelphij	struct tmpfs_node *node;
1161170808Sdelphij
1162170808Sdelphij	/* This operation only makes sense on directory nodes. */
1163171802Sdelphij	if (vp->v_type != VDIR)
1164171802Sdelphij		return ENOTDIR;
1165170808Sdelphij
1166263946Sbdrewery	maxcookies = 0;
1167170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1168170808Sdelphij
1169245115Sgleb	startresid = uio->uio_resid;
1170170808Sdelphij
1171263946Sbdrewery	/* Allocate cookies for NFS and compat modules. */
1172245115Sgleb	if (cookies != NULL && ncookies != NULL) {
1173263946Sbdrewery		maxcookies = howmany(node->tn_size,
1174263946Sbdrewery		    sizeof(struct tmpfs_dirent)) + 2;
1175263946Sbdrewery		*cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1176263946Sbdrewery		    M_WAITOK);
1177245115Sgleb		*ncookies = 0;
1178171862Sdelphij	}
1179171862Sdelphij
1180263946Sbdrewery	if (cookies == NULL)
1181245115Sgleb		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1182245115Sgleb	else
1183263946Sbdrewery		error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1184263946Sbdrewery		    ncookies);
1185170808Sdelphij
1186263946Sbdrewery	/* Buffer was filled without hitting EOF. */
1187245115Sgleb	if (error == EJUSTRETURN)
1188245115Sgleb		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1189171862Sdelphij
1190295897Smarkj	if (error != 0 && cookies != NULL && ncookies != NULL) {
1191245115Sgleb		free(*cookies, M_TEMP);
1192295897Smarkj		*cookies = NULL;
1193295897Smarkj		*ncookies = 0;
1194295897Smarkj	}
1195171862Sdelphij
1196170808Sdelphij	if (eofflag != NULL)
1197170808Sdelphij		*eofflag =
1198170808Sdelphij		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1199170808Sdelphij
1200170808Sdelphij	return error;
1201170808Sdelphij}
1202170808Sdelphij
1203171069Sdelphijstatic int
1204170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v)
1205170808Sdelphij{
1206170808Sdelphij	struct vnode *vp = v->a_vp;
1207170808Sdelphij	struct uio *uio = v->a_uio;
1208170808Sdelphij
1209170808Sdelphij	int error;
1210170808Sdelphij	struct tmpfs_node *node;
1211170808Sdelphij
1212170808Sdelphij	MPASS(uio->uio_offset == 0);
1213170808Sdelphij	MPASS(vp->v_type == VLNK);
1214170808Sdelphij
1215170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1216170808Sdelphij
1217170808Sdelphij	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1218170808Sdelphij	    uio);
1219170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
1220170808Sdelphij
1221170808Sdelphij	return error;
1222170808Sdelphij}
1223170808Sdelphij
1224171069Sdelphijstatic int
1225170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v)
1226170808Sdelphij{
1227278571Skib	struct vnode *vp;
1228170808Sdelphij	struct tmpfs_node *node;
1229170808Sdelphij
1230278571Skib	vp = v->a_vp;
1231170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1232170808Sdelphij	if (node->tn_links == 0)
1233234607Strasz		vrecycle(vp);
1234278571Skib	else
1235278571Skib		tmpfs_check_mtime(vp);
1236278571Skib	return (0);
1237170808Sdelphij}
1238170808Sdelphij
1239170808Sdelphijint
1240170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v)
1241170808Sdelphij{
1242170808Sdelphij	struct vnode *vp = v->a_vp;
1243170808Sdelphij
1244170808Sdelphij	struct tmpfs_mount *tmp;
1245170808Sdelphij	struct tmpfs_node *node;
1246170808Sdelphij
1247170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1248170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
1249171070Sdelphij
1250250189Skib	if (vp->v_type == VREG)
1251250189Skib		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1252250190Skib	else
1253250190Skib		vnode_destroy_vobject(vp);
1254250030Skib	vp->v_object = NULL;
1255170808Sdelphij	cache_purge(vp);
1256197953Sdelphij
1257197953Sdelphij	TMPFS_NODE_LOCK(node);
1258197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1259170808Sdelphij	tmpfs_free_vp(vp);
1260170808Sdelphij
1261170808Sdelphij	/* If the node referenced by this vnode was deleted by the user,
1262170808Sdelphij	 * we must free its associated data structures (now that the vnode
1263170808Sdelphij	 * is being reclaimed). */
1264197953Sdelphij	if (node->tn_links == 0 &&
1265197953Sdelphij	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1266197953Sdelphij		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1267197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1268170808Sdelphij		tmpfs_free_node(tmp, node);
1269197953Sdelphij	} else
1270197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1271170808Sdelphij
1272170808Sdelphij	MPASS(vp->v_data == NULL);
1273170808Sdelphij	return 0;
1274170808Sdelphij}
1275170808Sdelphij
1276171069Sdelphijstatic int
1277170808Sdelphijtmpfs_print(struct vop_print_args *v)
1278170808Sdelphij{
1279170808Sdelphij	struct vnode *vp = v->a_vp;
1280170808Sdelphij
1281170808Sdelphij	struct tmpfs_node *node;
1282170808Sdelphij
1283170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1284170808Sdelphij
1285248610Spjd	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1286170808Sdelphij	    node, node->tn_flags, node->tn_links);
1287231669Stijl	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1288170808Sdelphij	    node->tn_mode, node->tn_uid, node->tn_gid,
1289231669Stijl	    (intmax_t)node->tn_size, node->tn_status);
1290170808Sdelphij
1291170808Sdelphij	if (vp->v_type == VFIFO)
1292170808Sdelphij		fifo_printinfo(vp);
1293170808Sdelphij
1294170808Sdelphij	printf("\n");
1295170808Sdelphij
1296170808Sdelphij	return 0;
1297170808Sdelphij}
1298170808Sdelphij
1299171069Sdelphijstatic int
1300170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v)
1301170808Sdelphij{
1302170808Sdelphij	int name = v->a_name;
1303170808Sdelphij	register_t *retval = v->a_retval;
1304170808Sdelphij
1305170808Sdelphij	int error;
1306170808Sdelphij
1307170808Sdelphij	error = 0;
1308170808Sdelphij
1309170808Sdelphij	switch (name) {
1310170808Sdelphij	case _PC_LINK_MAX:
1311170808Sdelphij		*retval = LINK_MAX;
1312170808Sdelphij		break;
1313170808Sdelphij
1314170808Sdelphij	case _PC_NAME_MAX:
1315170808Sdelphij		*retval = NAME_MAX;
1316170808Sdelphij		break;
1317170808Sdelphij
1318170808Sdelphij	case _PC_PATH_MAX:
1319170808Sdelphij		*retval = PATH_MAX;
1320170808Sdelphij		break;
1321170808Sdelphij
1322170808Sdelphij	case _PC_PIPE_BUF:
1323170808Sdelphij		*retval = PIPE_BUF;
1324170808Sdelphij		break;
1325170808Sdelphij
1326170808Sdelphij	case _PC_CHOWN_RESTRICTED:
1327170808Sdelphij		*retval = 1;
1328170808Sdelphij		break;
1329170808Sdelphij
1330170808Sdelphij	case _PC_NO_TRUNC:
1331170808Sdelphij		*retval = 1;
1332170808Sdelphij		break;
1333170808Sdelphij
1334170808Sdelphij	case _PC_SYNC_IO:
1335170808Sdelphij		*retval = 1;
1336170808Sdelphij		break;
1337170808Sdelphij
1338170808Sdelphij	case _PC_FILESIZEBITS:
1339170808Sdelphij		*retval = 0; /* XXX Don't know which value should I return. */
1340170808Sdelphij		break;
1341170808Sdelphij
1342170808Sdelphij	default:
1343170808Sdelphij		error = EINVAL;
1344170808Sdelphij	}
1345170808Sdelphij
1346170808Sdelphij	return error;
1347170808Sdelphij}
1348170808Sdelphij
1349171069Sdelphijstatic int
1350170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap)
1351170808Sdelphij{
1352170808Sdelphij	struct tmpfs_fid *tfhp;
1353170808Sdelphij	struct tmpfs_node *node;
1354170808Sdelphij
1355170808Sdelphij	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1356170808Sdelphij	node = VP_TO_TMPFS_NODE(ap->a_vp);
1357170808Sdelphij
1358170808Sdelphij	tfhp->tf_len = sizeof(struct tmpfs_fid);
1359170808Sdelphij	tfhp->tf_id = node->tn_id;
1360170808Sdelphij	tfhp->tf_gen = node->tn_gen;
1361171070Sdelphij
1362170808Sdelphij	return (0);
1363170808Sdelphij}
1364171069Sdelphij
1365211598Sedstatic int
1366211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap)
1367211598Sed{
1368211598Sed	struct vnode *dvp = ap->a_dvp;
1369211598Sed	struct componentname *cnp = ap->a_cnp;
1370211598Sed	struct tmpfs_dirent *de;
1371211598Sed
1372211598Sed	switch (ap->a_flags) {
1373211598Sed	case LOOKUP:
1374211598Sed		return (0);
1375211598Sed	case CREATE:
1376211598Sed		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1377211598Sed		if (de != NULL)
1378211598Sed			return (de->td_node == NULL ? 0 : EEXIST);
1379211598Sed		return (tmpfs_dir_whiteout_add(dvp, cnp));
1380211598Sed	case DELETE:
1381211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
1382211598Sed		return (0);
1383211598Sed	default:
1384211598Sed		panic("tmpfs_whiteout: unknown op");
1385211598Sed	}
1386211598Sed}
1387211598Sed
1388171069Sdelphij/*
1389171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system.
1390171069Sdelphij */
1391171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = {
1392171069Sdelphij	.vop_default =			&default_vnodeops,
1393171069Sdelphij	.vop_lookup =			vfs_cache_lookup,
1394171069Sdelphij	.vop_cachedlookup =		tmpfs_lookup,
1395171069Sdelphij	.vop_create =			tmpfs_create,
1396171069Sdelphij	.vop_mknod =			tmpfs_mknod,
1397171069Sdelphij	.vop_open =			tmpfs_open,
1398171069Sdelphij	.vop_close =			tmpfs_close,
1399171069Sdelphij	.vop_access =			tmpfs_access,
1400171069Sdelphij	.vop_getattr =			tmpfs_getattr,
1401171069Sdelphij	.vop_setattr =			tmpfs_setattr,
1402171069Sdelphij	.vop_read =			tmpfs_read,
1403171069Sdelphij	.vop_write =			tmpfs_write,
1404171069Sdelphij	.vop_fsync =			tmpfs_fsync,
1405171069Sdelphij	.vop_remove =			tmpfs_remove,
1406171069Sdelphij	.vop_link =			tmpfs_link,
1407171069Sdelphij	.vop_rename =			tmpfs_rename,
1408171069Sdelphij	.vop_mkdir =			tmpfs_mkdir,
1409171069Sdelphij	.vop_rmdir =			tmpfs_rmdir,
1410171069Sdelphij	.vop_symlink =			tmpfs_symlink,
1411171069Sdelphij	.vop_readdir =			tmpfs_readdir,
1412171069Sdelphij	.vop_readlink =			tmpfs_readlink,
1413171069Sdelphij	.vop_inactive =			tmpfs_inactive,
1414171069Sdelphij	.vop_reclaim =			tmpfs_reclaim,
1415171069Sdelphij	.vop_print =			tmpfs_print,
1416171069Sdelphij	.vop_pathconf =			tmpfs_pathconf,
1417171069Sdelphij	.vop_vptofh =			tmpfs_vptofh,
1418211598Sed	.vop_whiteout =			tmpfs_whiteout,
1419171069Sdelphij	.vop_bmap =			VOP_EOPNOTSUPP,
1420171069Sdelphij};
1421171069Sdelphij
1422