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$");
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. */
198170808Sdelphij	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
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;
216170808Sdelphij
217170808Sdelphij	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
218170808Sdelphij
219170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
220170808Sdelphij}
221170808Sdelphij
222171069Sdelphijstatic int
223170808Sdelphijtmpfs_mknod(struct vop_mknod_args *v)
224170808Sdelphij{
225170808Sdelphij	struct vnode *dvp = v->a_dvp;
226170808Sdelphij	struct vnode **vpp = v->a_vpp;
227170808Sdelphij	struct componentname *cnp = v->a_cnp;
228170808Sdelphij	struct vattr *vap = v->a_vap;
229170808Sdelphij
230170808Sdelphij	if (vap->va_type != VBLK && vap->va_type != VCHR &&
231170808Sdelphij	    vap->va_type != VFIFO)
232170808Sdelphij		return EINVAL;
233170808Sdelphij
234170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
235170808Sdelphij}
236170808Sdelphij
237171069Sdelphijstatic int
238170808Sdelphijtmpfs_open(struct vop_open_args *v)
239170808Sdelphij{
240170808Sdelphij	struct vnode *vp = v->a_vp;
241170808Sdelphij	int mode = v->a_mode;
242170808Sdelphij
243170808Sdelphij	int error;
244170808Sdelphij	struct tmpfs_node *node;
245170808Sdelphij
246176559Sattilio	MPASS(VOP_ISLOCKED(vp));
247170808Sdelphij
248170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
249171070Sdelphij
250170808Sdelphij	/* The file is still active but all its names have been removed
251170808Sdelphij	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
252170808Sdelphij	 * it is about to die. */
253170808Sdelphij	if (node->tn_links < 1)
254170808Sdelphij		return (ENOENT);
255170808Sdelphij
256170808Sdelphij	/* If the file is marked append-only, deny write requests. */
257170808Sdelphij	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
258170808Sdelphij		error = EPERM;
259170808Sdelphij	else {
260170808Sdelphij		error = 0;
261250190Skib		/* For regular files, the call below is nop. */
262269168Skib		KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
263269168Skib		    OBJ_DEAD) == 0, ("dead object"));
264171070Sdelphij		vnode_create_vobject(vp, node->tn_size, v->a_td);
265170808Sdelphij	}
266170808Sdelphij
267176559Sattilio	MPASS(VOP_ISLOCKED(vp));
268170808Sdelphij	return error;
269170808Sdelphij}
270170808Sdelphij
271171069Sdelphijstatic int
272170808Sdelphijtmpfs_close(struct vop_close_args *v)
273170808Sdelphij{
274170808Sdelphij	struct vnode *vp = v->a_vp;
275170808Sdelphij
276218949Salc	/* Update node times. */
277218949Salc	tmpfs_update(vp);
278170808Sdelphij
279218949Salc	return (0);
280170808Sdelphij}
281170808Sdelphij
282170808Sdelphijint
283170808Sdelphijtmpfs_access(struct vop_access_args *v)
284170808Sdelphij{
285170808Sdelphij	struct vnode *vp = v->a_vp;
286184413Strasz	accmode_t accmode = v->a_accmode;
287170808Sdelphij	struct ucred *cred = v->a_cred;
288170808Sdelphij
289170808Sdelphij	int error;
290170808Sdelphij	struct tmpfs_node *node;
291170808Sdelphij
292176559Sattilio	MPASS(VOP_ISLOCKED(vp));
293170808Sdelphij
294170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
295170808Sdelphij
296170808Sdelphij	switch (vp->v_type) {
297170808Sdelphij	case VDIR:
298170808Sdelphij		/* FALLTHROUGH */
299170808Sdelphij	case VLNK:
300170808Sdelphij		/* FALLTHROUGH */
301170808Sdelphij	case VREG:
302184413Strasz		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
303170808Sdelphij			error = EROFS;
304170808Sdelphij			goto out;
305170808Sdelphij		}
306170808Sdelphij		break;
307170808Sdelphij
308170808Sdelphij	case VBLK:
309170808Sdelphij		/* FALLTHROUGH */
310170808Sdelphij	case VCHR:
311170808Sdelphij		/* FALLTHROUGH */
312170808Sdelphij	case VSOCK:
313170808Sdelphij		/* FALLTHROUGH */
314170808Sdelphij	case VFIFO:
315170808Sdelphij		break;
316170808Sdelphij
317170808Sdelphij	default:
318170808Sdelphij		error = EINVAL;
319170808Sdelphij		goto out;
320170808Sdelphij	}
321170808Sdelphij
322184413Strasz	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
323170808Sdelphij		error = EPERM;
324170808Sdelphij		goto out;
325170808Sdelphij	}
326170808Sdelphij
327170808Sdelphij	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
328184413Strasz	    node->tn_gid, accmode, cred, NULL);
329170808Sdelphij
330170808Sdelphijout:
331176559Sattilio	MPASS(VOP_ISLOCKED(vp));
332170808Sdelphij
333170808Sdelphij	return error;
334170808Sdelphij}
335170808Sdelphij
336170808Sdelphijint
337170808Sdelphijtmpfs_getattr(struct vop_getattr_args *v)
338170808Sdelphij{
339170808Sdelphij	struct vnode *vp = v->a_vp;
340170808Sdelphij	struct vattr *vap = v->a_vap;
341170808Sdelphij
342170808Sdelphij	struct tmpfs_node *node;
343170808Sdelphij
344170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
345170808Sdelphij
346170808Sdelphij	tmpfs_update(vp);
347170808Sdelphij
348170808Sdelphij	vap->va_type = vp->v_type;
349170808Sdelphij	vap->va_mode = node->tn_mode;
350170808Sdelphij	vap->va_nlink = node->tn_links;
351170808Sdelphij	vap->va_uid = node->tn_uid;
352170808Sdelphij	vap->va_gid = node->tn_gid;
353170808Sdelphij	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
354170808Sdelphij	vap->va_fileid = node->tn_id;
355170808Sdelphij	vap->va_size = node->tn_size;
356170808Sdelphij	vap->va_blocksize = PAGE_SIZE;
357170808Sdelphij	vap->va_atime = node->tn_atime;
358170808Sdelphij	vap->va_mtime = node->tn_mtime;
359170808Sdelphij	vap->va_ctime = node->tn_ctime;
360170808Sdelphij	vap->va_birthtime = node->tn_birthtime;
361170808Sdelphij	vap->va_gen = node->tn_gen;
362170808Sdelphij	vap->va_flags = node->tn_flags;
363170808Sdelphij	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
364183214Skib		node->tn_rdev : NODEV;
365170808Sdelphij	vap->va_bytes = round_page(node->tn_size);
366183212Skib	vap->va_filerev = 0;
367170808Sdelphij
368170808Sdelphij	return 0;
369170808Sdelphij}
370170808Sdelphij
371170808Sdelphijint
372170808Sdelphijtmpfs_setattr(struct vop_setattr_args *v)
373170808Sdelphij{
374170808Sdelphij	struct vnode *vp = v->a_vp;
375170808Sdelphij	struct vattr *vap = v->a_vap;
376170808Sdelphij	struct ucred *cred = v->a_cred;
377182371Sattilio	struct thread *td = curthread;
378170808Sdelphij
379170808Sdelphij	int error;
380170808Sdelphij
381176559Sattilio	MPASS(VOP_ISLOCKED(vp));
382170808Sdelphij
383170808Sdelphij	error = 0;
384170808Sdelphij
385170808Sdelphij	/* Abort if any unsettable attribute is given. */
386170808Sdelphij	if (vap->va_type != VNON ||
387170808Sdelphij	    vap->va_nlink != VNOVAL ||
388170808Sdelphij	    vap->va_fsid != VNOVAL ||
389170808Sdelphij	    vap->va_fileid != VNOVAL ||
390170808Sdelphij	    vap->va_blocksize != VNOVAL ||
391170808Sdelphij	    vap->va_gen != VNOVAL ||
392170808Sdelphij	    vap->va_rdev != VNOVAL ||
393170808Sdelphij	    vap->va_bytes != VNOVAL)
394170808Sdelphij		error = EINVAL;
395170808Sdelphij
396170808Sdelphij	if (error == 0 && (vap->va_flags != VNOVAL))
397182371Sattilio		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
398170808Sdelphij
399170808Sdelphij	if (error == 0 && (vap->va_size != VNOVAL))
400182371Sattilio		error = tmpfs_chsize(vp, vap->va_size, cred, td);
401170808Sdelphij
402170808Sdelphij	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
403182371Sattilio		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
404170808Sdelphij
405170808Sdelphij	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
406182371Sattilio		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
407170808Sdelphij
408170808Sdelphij	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
409170808Sdelphij	    vap->va_atime.tv_nsec != VNOVAL) ||
410170808Sdelphij	    (vap->va_mtime.tv_sec != VNOVAL &&
411170808Sdelphij	    vap->va_mtime.tv_nsec != VNOVAL) ||
412170808Sdelphij	    (vap->va_birthtime.tv_sec != VNOVAL &&
413170808Sdelphij	    vap->va_birthtime.tv_nsec != VNOVAL)))
414267816Skib		error = tmpfs_chtimes(vp, vap, cred, td);
415170808Sdelphij
416170808Sdelphij	/* Update the node times.  We give preference to the error codes
417170808Sdelphij	 * generated by this function rather than the ones that may arise
418170808Sdelphij	 * from tmpfs_update. */
419170808Sdelphij	tmpfs_update(vp);
420170808Sdelphij
421176559Sattilio	MPASS(VOP_ISLOCKED(vp));
422170808Sdelphij
423170808Sdelphij	return error;
424170808Sdelphij}
425170808Sdelphij
426197850Sdelphijstatic int
427170808Sdelphijtmpfs_read(struct vop_read_args *v)
428170808Sdelphij{
429254601Skib	struct vnode *vp;
430254601Skib	struct uio *uio;
431170808Sdelphij	struct tmpfs_node *node;
432170808Sdelphij
433254601Skib	vp = v->a_vp;
434254601Skib	if (vp->v_type != VREG)
435254601Skib		return (EISDIR);
436254601Skib	uio = v->a_uio;
437254601Skib	if (uio->uio_offset < 0)
438254601Skib		return (EINVAL);
439170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
440170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
441254601Skib	return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
442170808Sdelphij}
443170808Sdelphij
444171069Sdelphijstatic int
445170808Sdelphijtmpfs_write(struct vop_write_args *v)
446170808Sdelphij{
447254601Skib	struct vnode *vp;
448254601Skib	struct uio *uio;
449254601Skib	struct tmpfs_node *node;
450254601Skib	off_t oldsize;
451254601Skib	int error, ioflag;
452170808Sdelphij	boolean_t extended;
453170808Sdelphij
454254601Skib	vp = v->a_vp;
455254601Skib	uio = v->a_uio;
456254601Skib	ioflag = v->a_ioflag;
457254601Skib	error = 0;
458170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
459170808Sdelphij	oldsize = node->tn_size;
460170808Sdelphij
461254601Skib	if (uio->uio_offset < 0 || vp->v_type != VREG)
462254601Skib		return (EINVAL);
463254601Skib	if (uio->uio_resid == 0)
464254601Skib		return (0);
465170808Sdelphij	if (ioflag & IO_APPEND)
466170808Sdelphij		uio->uio_offset = node->tn_size;
467171070Sdelphij	if (uio->uio_offset + uio->uio_resid >
468170808Sdelphij	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
469170808Sdelphij		return (EFBIG);
470207719Strasz	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
471207662Strasz		return (EFBIG);
472170808Sdelphij	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
473170808Sdelphij	if (extended) {
474230180Salc		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
475230180Salc		    FALSE);
476170808Sdelphij		if (error != 0)
477170808Sdelphij			goto out;
478170808Sdelphij	}
479170808Sdelphij
480254601Skib	error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
481170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
482170808Sdelphij	    (extended ? TMPFS_NODE_CHANGED : 0);
483170808Sdelphij	if (node->tn_mode & (S_ISUID | S_ISGID)) {
484170808Sdelphij		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
485170808Sdelphij			node->tn_mode &= ~(S_ISUID | S_ISGID);
486170808Sdelphij	}
487170808Sdelphij	if (error != 0)
488230180Salc		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
489170808Sdelphij
490170808Sdelphijout:
491170808Sdelphij	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
492170808Sdelphij	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
493170808Sdelphij
494254601Skib	return (error);
495170808Sdelphij}
496170808Sdelphij
497171069Sdelphijstatic int
498170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v)
499170808Sdelphij{
500170808Sdelphij	struct vnode *vp = v->a_vp;
501170808Sdelphij
502176559Sattilio	MPASS(VOP_ISLOCKED(vp));
503170808Sdelphij
504170808Sdelphij	tmpfs_update(vp);
505170808Sdelphij
506170808Sdelphij	return 0;
507170808Sdelphij}
508170808Sdelphij
509171069Sdelphijstatic int
510170808Sdelphijtmpfs_remove(struct vop_remove_args *v)
511170808Sdelphij{
512170808Sdelphij	struct vnode *dvp = v->a_dvp;
513170808Sdelphij	struct vnode *vp = v->a_vp;
514170808Sdelphij
515170808Sdelphij	int error;
516170808Sdelphij	struct tmpfs_dirent *de;
517170808Sdelphij	struct tmpfs_mount *tmp;
518170808Sdelphij	struct tmpfs_node *dnode;
519170808Sdelphij	struct tmpfs_node *node;
520170808Sdelphij
521176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
522176559Sattilio	MPASS(VOP_ISLOCKED(vp));
523170808Sdelphij
524170808Sdelphij	if (vp->v_type == VDIR) {
525170808Sdelphij		error = EISDIR;
526170808Sdelphij		goto out;
527170808Sdelphij	}
528170808Sdelphij
529170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
530170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
531170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
532188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
533170808Sdelphij	MPASS(de != NULL);
534170808Sdelphij
535170808Sdelphij	/* Files marked as immutable or append-only cannot be deleted. */
536170808Sdelphij	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
537170808Sdelphij	    (dnode->tn_flags & APPEND)) {
538170808Sdelphij		error = EPERM;
539170808Sdelphij		goto out;
540170808Sdelphij	}
541170808Sdelphij
542170808Sdelphij	/* Remove the entry from the directory; as it is a file, we do not
543170808Sdelphij	 * have to change the number of hard links of the directory. */
544170808Sdelphij	tmpfs_dir_detach(dvp, de);
545211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
546211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
547170808Sdelphij
548170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
549170808Sdelphij	 * referred by it will not be removed until the vnode is really
550170808Sdelphij	 * reclaimed. */
551245115Sgleb	tmpfs_free_dirent(tmp, de);
552170808Sdelphij
553218949Salc	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
554170808Sdelphij	error = 0;
555170808Sdelphij
556170808Sdelphijout:
557170808Sdelphij
558170808Sdelphij	return error;
559170808Sdelphij}
560170808Sdelphij
561171069Sdelphijstatic int
562170808Sdelphijtmpfs_link(struct vop_link_args *v)
563170808Sdelphij{
564170808Sdelphij	struct vnode *dvp = v->a_tdvp;
565170808Sdelphij	struct vnode *vp = v->a_vp;
566170808Sdelphij	struct componentname *cnp = v->a_cnp;
567170808Sdelphij
568170808Sdelphij	int error;
569170808Sdelphij	struct tmpfs_dirent *de;
570170808Sdelphij	struct tmpfs_node *node;
571170808Sdelphij
572176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
573170808Sdelphij	MPASS(cnp->cn_flags & HASBUF);
574170808Sdelphij	MPASS(dvp != vp); /* XXX When can this be false? */
575269167Skib	node = VP_TO_TMPFS_NODE(vp);
576269167Skib
577170808Sdelphij	/* Ensure that we do not overflow the maximum number of links imposed
578170808Sdelphij	 * by the system. */
579170808Sdelphij	MPASS(node->tn_links <= LINK_MAX);
580170808Sdelphij	if (node->tn_links == LINK_MAX) {
581170808Sdelphij		error = EMLINK;
582170808Sdelphij		goto out;
583170808Sdelphij	}
584170808Sdelphij
585170808Sdelphij	/* We cannot create links of files marked immutable or append-only. */
586170808Sdelphij	if (node->tn_flags & (IMMUTABLE | APPEND)) {
587170808Sdelphij		error = EPERM;
588170808Sdelphij		goto out;
589170808Sdelphij	}
590170808Sdelphij
591170808Sdelphij	/* Allocate a new directory entry to represent the node. */
592170808Sdelphij	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
593170808Sdelphij	    cnp->cn_nameptr, cnp->cn_namelen, &de);
594170808Sdelphij	if (error != 0)
595170808Sdelphij		goto out;
596170808Sdelphij
597170808Sdelphij	/* Insert the new directory entry into the appropriate directory. */
598211598Sed	if (cnp->cn_flags & ISWHITEOUT)
599211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
600170808Sdelphij	tmpfs_dir_attach(dvp, de);
601170808Sdelphij
602170808Sdelphij	/* vp link count has changed, so update node times. */
603170808Sdelphij	node->tn_status |= TMPFS_NODE_CHANGED;
604170808Sdelphij	tmpfs_update(vp);
605170808Sdelphij
606170808Sdelphij	error = 0;
607171070Sdelphij
608170808Sdelphijout:
609170808Sdelphij	return error;
610170808Sdelphij}
611170808Sdelphij
612232960Sgleb/*
613232960Sgleb * We acquire all but fdvp locks using non-blocking acquisitions.  If we
614232960Sgleb * fail to acquire any lock in the path we will drop all held locks,
615232960Sgleb * acquire the new lock in a blocking fashion, and then release it and
616232960Sgleb * restart the rename.  This acquire/release step ensures that we do not
617232960Sgleb * spin on a lock waiting for release.  On error release all vnode locks
618232960Sgleb * and decrement references the way tmpfs_rename() would do.
619232960Sgleb */
620171069Sdelphijstatic int
621232960Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
622232960Sgleb    struct vnode *tdvp, struct vnode **tvpp,
623232960Sgleb    struct componentname *fcnp, struct componentname *tcnp)
624232960Sgleb{
625232960Sgleb	struct vnode *nvp;
626232960Sgleb	struct mount *mp;
627232960Sgleb	struct tmpfs_dirent *de;
628232960Sgleb	int error, restarts = 0;
629232960Sgleb
630232960Sgleb	VOP_UNLOCK(tdvp, 0);
631232960Sgleb	if (*tvpp != NULL && *tvpp != tdvp)
632232960Sgleb		VOP_UNLOCK(*tvpp, 0);
633232960Sgleb	mp = fdvp->v_mount;
634232960Sgleb
635232960Sglebrelock:
636232960Sgleb	restarts += 1;
637232960Sgleb	error = vn_lock(fdvp, LK_EXCLUSIVE);
638232960Sgleb	if (error)
639232960Sgleb		goto releout;
640232960Sgleb	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
641232960Sgleb		VOP_UNLOCK(fdvp, 0);
642232960Sgleb		error = vn_lock(tdvp, LK_EXCLUSIVE);
643232960Sgleb		if (error)
644232960Sgleb			goto releout;
645232960Sgleb		VOP_UNLOCK(tdvp, 0);
646232960Sgleb		goto relock;
647232960Sgleb	}
648232960Sgleb	/*
649232960Sgleb	 * Re-resolve fvp to be certain it still exists and fetch the
650232960Sgleb	 * correct vnode.
651232960Sgleb	 */
652232960Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
653232960Sgleb	if (de == NULL) {
654232960Sgleb		VOP_UNLOCK(fdvp, 0);
655232960Sgleb		VOP_UNLOCK(tdvp, 0);
656232960Sgleb		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
657232960Sgleb		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
658232960Sgleb			error = EINVAL;
659232960Sgleb		else
660232960Sgleb			error = ENOENT;
661232960Sgleb		goto releout;
662232960Sgleb	}
663232960Sgleb	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
664232960Sgleb	if (error != 0) {
665232960Sgleb		VOP_UNLOCK(fdvp, 0);
666232960Sgleb		VOP_UNLOCK(tdvp, 0);
667232960Sgleb		if (error != EBUSY)
668232960Sgleb			goto releout;
669232960Sgleb		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
670232960Sgleb		if (error != 0)
671232960Sgleb			goto releout;
672232960Sgleb		VOP_UNLOCK(nvp, 0);
673232960Sgleb		/*
674232960Sgleb		 * Concurrent rename race.
675232960Sgleb		 */
676232960Sgleb		if (nvp == tdvp) {
677232960Sgleb			vrele(nvp);
678232960Sgleb			error = EINVAL;
679232960Sgleb			goto releout;
680232960Sgleb		}
681232960Sgleb		vrele(*fvpp);
682232960Sgleb		*fvpp = nvp;
683232960Sgleb		goto relock;
684232960Sgleb	}
685232960Sgleb	vrele(*fvpp);
686232960Sgleb	*fvpp = nvp;
687232960Sgleb	VOP_UNLOCK(*fvpp, 0);
688232960Sgleb	/*
689232960Sgleb	 * Re-resolve tvp and acquire the vnode lock if present.
690232960Sgleb	 */
691232960Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
692232960Sgleb	/*
693232960Sgleb	 * If tvp disappeared we just carry on.
694232960Sgleb	 */
695232960Sgleb	if (de == NULL && *tvpp != NULL) {
696232960Sgleb		vrele(*tvpp);
697232960Sgleb		*tvpp = NULL;
698232960Sgleb	}
699232960Sgleb	/*
700232960Sgleb	 * Get the tvp ino if the lookup succeeded.  We may have to restart
701232960Sgleb	 * if the non-blocking acquire fails.
702232960Sgleb	 */
703232960Sgleb	if (de != NULL) {
704232960Sgleb		nvp = NULL;
705232960Sgleb		error = tmpfs_alloc_vp(mp, de->td_node,
706232960Sgleb		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
707232960Sgleb		if (*tvpp != NULL)
708232960Sgleb			vrele(*tvpp);
709232960Sgleb		*tvpp = nvp;
710232960Sgleb		if (error != 0) {
711232960Sgleb			VOP_UNLOCK(fdvp, 0);
712232960Sgleb			VOP_UNLOCK(tdvp, 0);
713232960Sgleb			if (error != EBUSY)
714232960Sgleb				goto releout;
715232960Sgleb			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
716232960Sgleb			    &nvp);
717232960Sgleb			if (error != 0)
718232960Sgleb				goto releout;
719232960Sgleb			VOP_UNLOCK(nvp, 0);
720232960Sgleb			/*
721232960Sgleb			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
722232960Sgleb			 */
723232960Sgleb			if (nvp == fdvp) {
724232960Sgleb				error = ENOTEMPTY;
725232960Sgleb				goto releout;
726232960Sgleb			}
727232960Sgleb			goto relock;
728232960Sgleb		}
729232960Sgleb	}
730232960Sgleb	tmpfs_rename_restarts += restarts;
731232960Sgleb
732232960Sgleb	return (0);
733232960Sgleb
734232960Sglebreleout:
735232960Sgleb	vrele(fdvp);
736232960Sgleb	vrele(*fvpp);
737232960Sgleb	vrele(tdvp);
738232960Sgleb	if (*tvpp != NULL)
739232960Sgleb		vrele(*tvpp);
740232960Sgleb	tmpfs_rename_restarts += restarts;
741232960Sgleb
742232960Sgleb	return (error);
743232960Sgleb}
744232960Sgleb
745232960Sglebstatic int
746170808Sdelphijtmpfs_rename(struct vop_rename_args *v)
747170808Sdelphij{
748170808Sdelphij	struct vnode *fdvp = v->a_fdvp;
749170808Sdelphij	struct vnode *fvp = v->a_fvp;
750170808Sdelphij	struct componentname *fcnp = v->a_fcnp;
751170808Sdelphij	struct vnode *tdvp = v->a_tdvp;
752170808Sdelphij	struct vnode *tvp = v->a_tvp;
753170808Sdelphij	struct componentname *tcnp = v->a_tcnp;
754232960Sgleb	struct mount *mp = NULL;
755170808Sdelphij
756170808Sdelphij	char *newname;
757170808Sdelphij	int error;
758170808Sdelphij	struct tmpfs_dirent *de;
759197953Sdelphij	struct tmpfs_mount *tmp;
760170808Sdelphij	struct tmpfs_node *fdnode;
761170808Sdelphij	struct tmpfs_node *fnode;
762171799Sdelphij	struct tmpfs_node *tnode;
763170808Sdelphij	struct tmpfs_node *tdnode;
764170808Sdelphij
765176559Sattilio	MPASS(VOP_ISLOCKED(tdvp));
766176559Sattilio	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
767170808Sdelphij	MPASS(fcnp->cn_flags & HASBUF);
768170808Sdelphij	MPASS(tcnp->cn_flags & HASBUF);
769170808Sdelphij
770170808Sdelphij	/* Disallow cross-device renames.
771170808Sdelphij	 * XXX Why isn't this done by the caller? */
772170808Sdelphij	if (fvp->v_mount != tdvp->v_mount ||
773170808Sdelphij	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
774170808Sdelphij		error = EXDEV;
775170808Sdelphij		goto out;
776170808Sdelphij	}
777170808Sdelphij
778170808Sdelphij	/* If source and target are the same file, there is nothing to do. */
779170808Sdelphij	if (fvp == tvp) {
780170808Sdelphij		error = 0;
781170808Sdelphij		goto out;
782170808Sdelphij	}
783170808Sdelphij
784173725Sdelphij	/* If we need to move the directory between entries, lock the
785173725Sdelphij	 * source so that we can safely operate on it. */
786232960Sgleb	if (fdvp != tdvp && fdvp != tvp) {
787232960Sgleb		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
788232960Sgleb			mp = tdvp->v_mount;
789232960Sgleb			error = vfs_busy(mp, 0);
790232960Sgleb			if (error != 0) {
791232960Sgleb				mp = NULL;
792232960Sgleb				goto out;
793232960Sgleb			}
794232960Sgleb			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
795232960Sgleb			    fcnp, tcnp);
796232960Sgleb			if (error != 0) {
797232960Sgleb				vfs_unbusy(mp);
798232960Sgleb				return (error);
799232960Sgleb			}
800232960Sgleb			ASSERT_VOP_ELOCKED(fdvp,
801232960Sgleb			    "tmpfs_rename: fdvp not locked");
802232960Sgleb			ASSERT_VOP_ELOCKED(tdvp,
803232960Sgleb			    "tmpfs_rename: tdvp not locked");
804232960Sgleb			if (tvp != NULL)
805232960Sgleb				ASSERT_VOP_ELOCKED(tvp,
806232960Sgleb				    "tmpfs_rename: tvp not locked");
807232960Sgleb			if (fvp == tvp) {
808232960Sgleb				error = 0;
809232960Sgleb				goto out_locked;
810232960Sgleb			}
811232960Sgleb		}
812232960Sgleb	}
813232960Sgleb
814232960Sgleb	tmp = VFS_TO_TMPFS(tdvp->v_mount);
815232960Sgleb	tdnode = VP_TO_TMPFS_DIR(tdvp);
816232960Sgleb	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
817173725Sdelphij	fdnode = VP_TO_TMPFS_DIR(fdvp);
818173725Sdelphij	fnode = VP_TO_TMPFS_NODE(fvp);
819188318Skib	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
820173725Sdelphij
821212305Sivoras	/* Entry can disappear before we lock fdvp,
822212305Sivoras	 * also avoid manipulating '.' and '..' entries. */
823170808Sdelphij	if (de == NULL) {
824212305Sivoras		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
825212305Sivoras		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
826212305Sivoras			error = EINVAL;
827212305Sivoras		else
828212305Sivoras			error = ENOENT;
829173725Sdelphij		goto out_locked;
830170808Sdelphij	}
831170808Sdelphij	MPASS(de->td_node == fnode);
832170808Sdelphij
833171070Sdelphij	/* If re-naming a directory to another preexisting directory
834170808Sdelphij	 * ensure that the target directory is empty so that its
835171070Sdelphij	 * removal causes no side effects.
836170808Sdelphij	 * Kern_rename gurantees the destination to be a directory
837170808Sdelphij	 * if the source is one. */
838170808Sdelphij	if (tvp != NULL) {
839171799Sdelphij		MPASS(tnode != NULL);
840171070Sdelphij
841170808Sdelphij		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
842170808Sdelphij		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
843170808Sdelphij			error = EPERM;
844173725Sdelphij			goto out_locked;
845170808Sdelphij		}
846170808Sdelphij
847171799Sdelphij		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
848171799Sdelphij			if (tnode->tn_size > 0) {
849171799Sdelphij				error = ENOTEMPTY;
850173725Sdelphij				goto out_locked;
851171799Sdelphij			}
852171799Sdelphij		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
853171799Sdelphij			error = ENOTDIR;
854173725Sdelphij			goto out_locked;
855171799Sdelphij		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
856171799Sdelphij			error = EISDIR;
857173725Sdelphij			goto out_locked;
858171799Sdelphij		} else {
859171799Sdelphij			MPASS(fnode->tn_type != VDIR &&
860171799Sdelphij				tnode->tn_type != VDIR);
861170808Sdelphij		}
862170808Sdelphij	}
863170808Sdelphij
864170808Sdelphij	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
865170808Sdelphij	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
866170808Sdelphij		error = EPERM;
867170808Sdelphij		goto out_locked;
868170808Sdelphij	}
869170808Sdelphij
870170808Sdelphij	/* Ensure that we have enough memory to hold the new name, if it
871170808Sdelphij	 * has to be changed. */
872170808Sdelphij	if (fcnp->cn_namelen != tcnp->cn_namelen ||
873183299Sobrien	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
874171087Sdelphij		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
875170808Sdelphij	} else
876170808Sdelphij		newname = NULL;
877170808Sdelphij
878170808Sdelphij	/* If the node is being moved to another directory, we have to do
879170808Sdelphij	 * the move. */
880170808Sdelphij	if (fdnode != tdnode) {
881170808Sdelphij		/* In case we are moving a directory, we have to adjust its
882170808Sdelphij		 * parent to point to the new parent. */
883170808Sdelphij		if (de->td_node->tn_type == VDIR) {
884170808Sdelphij			struct tmpfs_node *n;
885170808Sdelphij
886170808Sdelphij			/* Ensure the target directory is not a child of the
887170808Sdelphij			 * directory being moved.  Otherwise, we'd end up
888170808Sdelphij			 * with stale nodes. */
889170808Sdelphij			n = tdnode;
890197953Sdelphij			/* TMPFS_LOCK garanties that no nodes are freed while
891197953Sdelphij			 * traversing the list. Nodes can only be marked as
892197953Sdelphij			 * removed: tn_parent == NULL. */
893197953Sdelphij			TMPFS_LOCK(tmp);
894197953Sdelphij			TMPFS_NODE_LOCK(n);
895170808Sdelphij			while (n != n->tn_dir.tn_parent) {
896197953Sdelphij				struct tmpfs_node *parent;
897197953Sdelphij
898170808Sdelphij				if (n == fnode) {
899197953Sdelphij					TMPFS_NODE_UNLOCK(n);
900197953Sdelphij					TMPFS_UNLOCK(tmp);
901170808Sdelphij					error = EINVAL;
902170808Sdelphij					if (newname != NULL)
903171087Sdelphij						    free(newname, M_TMPFSNAME);
904170808Sdelphij					goto out_locked;
905170808Sdelphij				}
906197953Sdelphij				parent = n->tn_dir.tn_parent;
907197953Sdelphij				TMPFS_NODE_UNLOCK(n);
908197953Sdelphij				if (parent == NULL) {
909197953Sdelphij					n = NULL;
910197953Sdelphij					break;
911197953Sdelphij				}
912197953Sdelphij				TMPFS_NODE_LOCK(parent);
913197953Sdelphij				if (parent->tn_dir.tn_parent == NULL) {
914197953Sdelphij					TMPFS_NODE_UNLOCK(parent);
915197953Sdelphij					n = NULL;
916197953Sdelphij					break;
917197953Sdelphij				}
918197953Sdelphij				n = parent;
919170808Sdelphij			}
920197953Sdelphij			TMPFS_UNLOCK(tmp);
921197953Sdelphij			if (n == NULL) {
922197953Sdelphij				error = EINVAL;
923197953Sdelphij				if (newname != NULL)
924197953Sdelphij					    free(newname, M_TMPFSNAME);
925197953Sdelphij				goto out_locked;
926197953Sdelphij			}
927197953Sdelphij			TMPFS_NODE_UNLOCK(n);
928170808Sdelphij
929170808Sdelphij			/* Adjust the parent pointer. */
930170808Sdelphij			TMPFS_VALIDATE_DIR(fnode);
931197953Sdelphij			TMPFS_NODE_LOCK(de->td_node);
932170808Sdelphij			de->td_node->tn_dir.tn_parent = tdnode;
933197953Sdelphij			TMPFS_NODE_UNLOCK(de->td_node);
934170808Sdelphij
935170808Sdelphij			/* As a result of changing the target of the '..'
936170808Sdelphij			 * entry, the link count of the source and target
937170808Sdelphij			 * directories has to be adjusted. */
938197953Sdelphij			TMPFS_NODE_LOCK(tdnode);
939197953Sdelphij			TMPFS_ASSERT_LOCKED(tdnode);
940197953Sdelphij			tdnode->tn_links++;
941197953Sdelphij			TMPFS_NODE_UNLOCK(tdnode);
942197953Sdelphij
943197953Sdelphij			TMPFS_NODE_LOCK(fdnode);
944197953Sdelphij			TMPFS_ASSERT_LOCKED(fdnode);
945170808Sdelphij			fdnode->tn_links--;
946197953Sdelphij			TMPFS_NODE_UNLOCK(fdnode);
947170808Sdelphij		}
948170808Sdelphij	}
949170808Sdelphij
950245115Sgleb	/* Do the move: just remove the entry from the source directory
951245115Sgleb	 * and insert it into the target one. */
952245115Sgleb	tmpfs_dir_detach(fdvp, de);
953245115Sgleb
954245115Sgleb	if (fcnp->cn_flags & DOWHITEOUT)
955245115Sgleb		tmpfs_dir_whiteout_add(fdvp, fcnp);
956245115Sgleb	if (tcnp->cn_flags & ISWHITEOUT)
957245115Sgleb		tmpfs_dir_whiteout_remove(tdvp, tcnp);
958245115Sgleb
959170808Sdelphij	/* If the name has changed, we need to make it effective by changing
960170808Sdelphij	 * it in the directory entry. */
961170808Sdelphij	if (newname != NULL) {
962170808Sdelphij		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
963170808Sdelphij
964245115Sgleb		free(de->ud.td_name, M_TMPFSNAME);
965245115Sgleb		de->ud.td_name = newname;
966245115Sgleb		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
967170808Sdelphij
968170808Sdelphij		fnode->tn_status |= TMPFS_NODE_CHANGED;
969170808Sdelphij		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
970170808Sdelphij	}
971170808Sdelphij
972170808Sdelphij	/* If we are overwriting an entry, we have to remove the old one
973170808Sdelphij	 * from the target directory. */
974170808Sdelphij	if (tvp != NULL) {
975245115Sgleb		struct tmpfs_dirent *tde;
976245115Sgleb
977170808Sdelphij		/* Remove the old entry from the target directory. */
978245115Sgleb		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
979245115Sgleb		tmpfs_dir_detach(tdvp, tde);
980170808Sdelphij
981170808Sdelphij		/* Free the directory entry we just deleted.  Note that the
982170808Sdelphij		 * node referred by it will not be removed until the vnode is
983170808Sdelphij		 * really reclaimed. */
984245115Sgleb		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
985170808Sdelphij	}
986245115Sgleb
987245115Sgleb	tmpfs_dir_attach(tdvp, de);
988245115Sgleb
989226987Spho	cache_purge(fvp);
990232401Sjhb	if (tvp != NULL)
991232401Sjhb		cache_purge(tvp);
992248422Skib	cache_purge_negative(tdvp);
993170808Sdelphij
994170808Sdelphij	error = 0;
995170808Sdelphij
996170808Sdelphijout_locked:
997227822Sivoras	if (fdvp != tdvp && fdvp != tvp)
998175294Sattilio		VOP_UNLOCK(fdvp, 0);
999170808Sdelphij
1000170808Sdelphijout:
1001170808Sdelphij	/* Release target nodes. */
1002170808Sdelphij	/* XXX: I don't understand when tdvp can be the same as tvp, but
1003170808Sdelphij	 * other code takes care of this... */
1004170808Sdelphij	if (tdvp == tvp)
1005170808Sdelphij		vrele(tdvp);
1006170808Sdelphij	else
1007170808Sdelphij		vput(tdvp);
1008170808Sdelphij	if (tvp != NULL)
1009170808Sdelphij		vput(tvp);
1010170808Sdelphij
1011170808Sdelphij	/* Release source nodes. */
1012170808Sdelphij	vrele(fdvp);
1013170808Sdelphij	vrele(fvp);
1014170808Sdelphij
1015232960Sgleb	if (mp != NULL)
1016232960Sgleb		vfs_unbusy(mp);
1017232960Sgleb
1018170808Sdelphij	return error;
1019170808Sdelphij}
1020170808Sdelphij
1021171069Sdelphijstatic int
1022170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v)
1023170808Sdelphij{
1024170808Sdelphij	struct vnode *dvp = v->a_dvp;
1025170808Sdelphij	struct vnode **vpp = v->a_vpp;
1026170808Sdelphij	struct componentname *cnp = v->a_cnp;
1027170808Sdelphij	struct vattr *vap = v->a_vap;
1028170808Sdelphij
1029170808Sdelphij	MPASS(vap->va_type == VDIR);
1030170808Sdelphij
1031170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1032170808Sdelphij}
1033170808Sdelphij
1034171069Sdelphijstatic int
1035170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v)
1036170808Sdelphij{
1037170808Sdelphij	struct vnode *dvp = v->a_dvp;
1038170808Sdelphij	struct vnode *vp = v->a_vp;
1039170808Sdelphij
1040170808Sdelphij	int error;
1041170808Sdelphij	struct tmpfs_dirent *de;
1042170808Sdelphij	struct tmpfs_mount *tmp;
1043170808Sdelphij	struct tmpfs_node *dnode;
1044170808Sdelphij	struct tmpfs_node *node;
1045170808Sdelphij
1046176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
1047176559Sattilio	MPASS(VOP_ISLOCKED(vp));
1048170808Sdelphij
1049170808Sdelphij	tmp = VFS_TO_TMPFS(dvp->v_mount);
1050170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
1051170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1052170808Sdelphij
1053171070Sdelphij	/* Directories with more than two entries ('.' and '..') cannot be
1054171070Sdelphij	 * removed. */
1055171070Sdelphij	 if (node->tn_size > 0) {
1056171070Sdelphij		 error = ENOTEMPTY;
1057171070Sdelphij		 goto out;
1058171070Sdelphij	 }
1059170808Sdelphij
1060170808Sdelphij	if ((dnode->tn_flags & APPEND)
1061170808Sdelphij	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1062170808Sdelphij		error = EPERM;
1063170808Sdelphij		goto out;
1064170808Sdelphij	}
1065170808Sdelphij
1066171070Sdelphij	/* This invariant holds only if we are not trying to remove "..".
1067171070Sdelphij	  * We checked for that above so this is safe now. */
1068170808Sdelphij	MPASS(node->tn_dir.tn_parent == dnode);
1069170808Sdelphij
1070170808Sdelphij	/* Get the directory entry associated with node (vp).  This was
1071170808Sdelphij	 * filled by tmpfs_lookup while looking up the entry. */
1072188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1073170808Sdelphij	MPASS(TMPFS_DIRENT_MATCHES(de,
1074170808Sdelphij	    v->a_cnp->cn_nameptr,
1075170808Sdelphij	    v->a_cnp->cn_namelen));
1076170808Sdelphij
1077170808Sdelphij	/* Check flags to see if we are allowed to remove the directory. */
1078170808Sdelphij	if (dnode->tn_flags & APPEND
1079170808Sdelphij		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1080170808Sdelphij		error = EPERM;
1081170808Sdelphij		goto out;
1082170808Sdelphij	}
1083170808Sdelphij
1084197953Sdelphij
1085170808Sdelphij	/* Detach the directory entry from the directory (dnode). */
1086170808Sdelphij	tmpfs_dir_detach(dvp, de);
1087211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
1088211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1089170808Sdelphij
1090197953Sdelphij	/* No vnode should be allocated for this entry from this point */
1091197953Sdelphij	TMPFS_NODE_LOCK(node);
1092197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1093170808Sdelphij	node->tn_links--;
1094197953Sdelphij	node->tn_dir.tn_parent = NULL;
1095170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1096170808Sdelphij	    TMPFS_NODE_MODIFIED;
1097197953Sdelphij
1098197953Sdelphij	TMPFS_NODE_UNLOCK(node);
1099197953Sdelphij
1100197953Sdelphij	TMPFS_NODE_LOCK(dnode);
1101197953Sdelphij	TMPFS_ASSERT_ELOCKED(dnode);
1102197953Sdelphij	dnode->tn_links--;
1103197953Sdelphij	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1104170808Sdelphij	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1105197953Sdelphij	TMPFS_NODE_UNLOCK(dnode);
1106170808Sdelphij
1107171070Sdelphij	cache_purge(dvp);
1108170808Sdelphij	cache_purge(vp);
1109170808Sdelphij
1110170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
1111170808Sdelphij	 * referred by it will not be removed until the vnode is really
1112170808Sdelphij	 * reclaimed. */
1113245115Sgleb	tmpfs_free_dirent(tmp, de);
1114170808Sdelphij
1115170808Sdelphij	/* Release the deleted vnode (will destroy the node, notify
1116170808Sdelphij	 * interested parties and clean it from the cache). */
1117170808Sdelphij
1118170808Sdelphij	dnode->tn_status |= TMPFS_NODE_CHANGED;
1119170808Sdelphij	tmpfs_update(dvp);
1120170808Sdelphij
1121170808Sdelphij	error = 0;
1122170808Sdelphij
1123170808Sdelphijout:
1124170808Sdelphij	return error;
1125170808Sdelphij}
1126170808Sdelphij
1127171069Sdelphijstatic int
1128170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v)
1129170808Sdelphij{
1130170808Sdelphij	struct vnode *dvp = v->a_dvp;
1131170808Sdelphij	struct vnode **vpp = v->a_vpp;
1132170808Sdelphij	struct componentname *cnp = v->a_cnp;
1133170808Sdelphij	struct vattr *vap = v->a_vap;
1134170808Sdelphij	char *target = v->a_target;
1135170808Sdelphij
1136170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1137170808Sdelphij	MPASS(vap->va_type == VLNK);
1138170808Sdelphij#else
1139170808Sdelphij	vap->va_type = VLNK;
1140170808Sdelphij#endif
1141170808Sdelphij
1142170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1143170808Sdelphij}
1144170808Sdelphij
1145171069Sdelphijstatic int
1146170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v)
1147170808Sdelphij{
1148170808Sdelphij	struct vnode *vp = v->a_vp;
1149170808Sdelphij	struct uio *uio = v->a_uio;
1150170808Sdelphij	int *eofflag = v->a_eofflag;
1151170808Sdelphij	u_long **cookies = v->a_cookies;
1152170808Sdelphij	int *ncookies = v->a_ncookies;
1153170808Sdelphij
1154170808Sdelphij	int error;
1155245115Sgleb	ssize_t startresid;
1156263946Sbdrewery	int maxcookies;
1157170808Sdelphij	struct tmpfs_node *node;
1158170808Sdelphij
1159170808Sdelphij	/* This operation only makes sense on directory nodes. */
1160171802Sdelphij	if (vp->v_type != VDIR)
1161171802Sdelphij		return ENOTDIR;
1162170808Sdelphij
1163263946Sbdrewery	maxcookies = 0;
1164170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1165170808Sdelphij
1166245115Sgleb	startresid = uio->uio_resid;
1167170808Sdelphij
1168263946Sbdrewery	/* Allocate cookies for NFS and compat modules. */
1169245115Sgleb	if (cookies != NULL && ncookies != NULL) {
1170263946Sbdrewery		maxcookies = howmany(node->tn_size,
1171263946Sbdrewery		    sizeof(struct tmpfs_dirent)) + 2;
1172263946Sbdrewery		*cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1173263946Sbdrewery		    M_WAITOK);
1174245115Sgleb		*ncookies = 0;
1175171862Sdelphij	}
1176171862Sdelphij
1177263946Sbdrewery	if (cookies == NULL)
1178245115Sgleb		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1179245115Sgleb	else
1180263946Sbdrewery		error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1181263946Sbdrewery		    ncookies);
1182170808Sdelphij
1183263946Sbdrewery	/* Buffer was filled without hitting EOF. */
1184245115Sgleb	if (error == EJUSTRETURN)
1185245115Sgleb		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1186171862Sdelphij
1187263946Sbdrewery	if (error != 0 && cookies != NULL)
1188245115Sgleb		free(*cookies, M_TEMP);
1189171862Sdelphij
1190170808Sdelphij	if (eofflag != NULL)
1191170808Sdelphij		*eofflag =
1192170808Sdelphij		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1193170808Sdelphij
1194170808Sdelphij	return error;
1195170808Sdelphij}
1196170808Sdelphij
1197171069Sdelphijstatic int
1198170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v)
1199170808Sdelphij{
1200170808Sdelphij	struct vnode *vp = v->a_vp;
1201170808Sdelphij	struct uio *uio = v->a_uio;
1202170808Sdelphij
1203170808Sdelphij	int error;
1204170808Sdelphij	struct tmpfs_node *node;
1205170808Sdelphij
1206170808Sdelphij	MPASS(uio->uio_offset == 0);
1207170808Sdelphij	MPASS(vp->v_type == VLNK);
1208170808Sdelphij
1209170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1210170808Sdelphij
1211170808Sdelphij	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1212170808Sdelphij	    uio);
1213170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
1214170808Sdelphij
1215170808Sdelphij	return error;
1216170808Sdelphij}
1217170808Sdelphij
1218171069Sdelphijstatic int
1219170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v)
1220170808Sdelphij{
1221170808Sdelphij	struct vnode *vp = v->a_vp;
1222170808Sdelphij
1223170808Sdelphij	struct tmpfs_node *node;
1224170808Sdelphij
1225170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1226170808Sdelphij
1227170808Sdelphij	if (node->tn_links == 0)
1228234607Strasz		vrecycle(vp);
1229170808Sdelphij
1230170808Sdelphij	return 0;
1231170808Sdelphij}
1232170808Sdelphij
1233170808Sdelphijint
1234170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v)
1235170808Sdelphij{
1236170808Sdelphij	struct vnode *vp = v->a_vp;
1237170808Sdelphij
1238170808Sdelphij	struct tmpfs_mount *tmp;
1239170808Sdelphij	struct tmpfs_node *node;
1240170808Sdelphij
1241170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1242170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
1243171070Sdelphij
1244250189Skib	if (vp->v_type == VREG)
1245250189Skib		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1246250190Skib	else
1247250190Skib		vnode_destroy_vobject(vp);
1248250030Skib	vp->v_object = NULL;
1249170808Sdelphij	cache_purge(vp);
1250197953Sdelphij
1251197953Sdelphij	TMPFS_NODE_LOCK(node);
1252197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1253170808Sdelphij	tmpfs_free_vp(vp);
1254170808Sdelphij
1255170808Sdelphij	/* If the node referenced by this vnode was deleted by the user,
1256170808Sdelphij	 * we must free its associated data structures (now that the vnode
1257170808Sdelphij	 * is being reclaimed). */
1258197953Sdelphij	if (node->tn_links == 0 &&
1259197953Sdelphij	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1260197953Sdelphij		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1261197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1262170808Sdelphij		tmpfs_free_node(tmp, node);
1263197953Sdelphij	} else
1264197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1265170808Sdelphij
1266170808Sdelphij	MPASS(vp->v_data == NULL);
1267170808Sdelphij	return 0;
1268170808Sdelphij}
1269170808Sdelphij
1270171069Sdelphijstatic int
1271170808Sdelphijtmpfs_print(struct vop_print_args *v)
1272170808Sdelphij{
1273170808Sdelphij	struct vnode *vp = v->a_vp;
1274170808Sdelphij
1275170808Sdelphij	struct tmpfs_node *node;
1276170808Sdelphij
1277170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1278170808Sdelphij
1279248610Spjd	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1280170808Sdelphij	    node, node->tn_flags, node->tn_links);
1281231669Stijl	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1282170808Sdelphij	    node->tn_mode, node->tn_uid, node->tn_gid,
1283231669Stijl	    (intmax_t)node->tn_size, node->tn_status);
1284170808Sdelphij
1285170808Sdelphij	if (vp->v_type == VFIFO)
1286170808Sdelphij		fifo_printinfo(vp);
1287170808Sdelphij
1288170808Sdelphij	printf("\n");
1289170808Sdelphij
1290170808Sdelphij	return 0;
1291170808Sdelphij}
1292170808Sdelphij
1293171069Sdelphijstatic int
1294170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v)
1295170808Sdelphij{
1296170808Sdelphij	int name = v->a_name;
1297170808Sdelphij	register_t *retval = v->a_retval;
1298170808Sdelphij
1299170808Sdelphij	int error;
1300170808Sdelphij
1301170808Sdelphij	error = 0;
1302170808Sdelphij
1303170808Sdelphij	switch (name) {
1304170808Sdelphij	case _PC_LINK_MAX:
1305170808Sdelphij		*retval = LINK_MAX;
1306170808Sdelphij		break;
1307170808Sdelphij
1308170808Sdelphij	case _PC_NAME_MAX:
1309170808Sdelphij		*retval = NAME_MAX;
1310170808Sdelphij		break;
1311170808Sdelphij
1312170808Sdelphij	case _PC_PATH_MAX:
1313170808Sdelphij		*retval = PATH_MAX;
1314170808Sdelphij		break;
1315170808Sdelphij
1316170808Sdelphij	case _PC_PIPE_BUF:
1317170808Sdelphij		*retval = PIPE_BUF;
1318170808Sdelphij		break;
1319170808Sdelphij
1320170808Sdelphij	case _PC_CHOWN_RESTRICTED:
1321170808Sdelphij		*retval = 1;
1322170808Sdelphij		break;
1323170808Sdelphij
1324170808Sdelphij	case _PC_NO_TRUNC:
1325170808Sdelphij		*retval = 1;
1326170808Sdelphij		break;
1327170808Sdelphij
1328170808Sdelphij	case _PC_SYNC_IO:
1329170808Sdelphij		*retval = 1;
1330170808Sdelphij		break;
1331170808Sdelphij
1332170808Sdelphij	case _PC_FILESIZEBITS:
1333170808Sdelphij		*retval = 0; /* XXX Don't know which value should I return. */
1334170808Sdelphij		break;
1335170808Sdelphij
1336170808Sdelphij	default:
1337170808Sdelphij		error = EINVAL;
1338170808Sdelphij	}
1339170808Sdelphij
1340170808Sdelphij	return error;
1341170808Sdelphij}
1342170808Sdelphij
1343171069Sdelphijstatic int
1344170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap)
1345170808Sdelphij{
1346170808Sdelphij	struct tmpfs_fid *tfhp;
1347170808Sdelphij	struct tmpfs_node *node;
1348170808Sdelphij
1349170808Sdelphij	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1350170808Sdelphij	node = VP_TO_TMPFS_NODE(ap->a_vp);
1351170808Sdelphij
1352170808Sdelphij	tfhp->tf_len = sizeof(struct tmpfs_fid);
1353170808Sdelphij	tfhp->tf_id = node->tn_id;
1354170808Sdelphij	tfhp->tf_gen = node->tn_gen;
1355171070Sdelphij
1356170808Sdelphij	return (0);
1357170808Sdelphij}
1358171069Sdelphij
1359211598Sedstatic int
1360211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap)
1361211598Sed{
1362211598Sed	struct vnode *dvp = ap->a_dvp;
1363211598Sed	struct componentname *cnp = ap->a_cnp;
1364211598Sed	struct tmpfs_dirent *de;
1365211598Sed
1366211598Sed	switch (ap->a_flags) {
1367211598Sed	case LOOKUP:
1368211598Sed		return (0);
1369211598Sed	case CREATE:
1370211598Sed		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1371211598Sed		if (de != NULL)
1372211598Sed			return (de->td_node == NULL ? 0 : EEXIST);
1373211598Sed		return (tmpfs_dir_whiteout_add(dvp, cnp));
1374211598Sed	case DELETE:
1375211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
1376211598Sed		return (0);
1377211598Sed	default:
1378211598Sed		panic("tmpfs_whiteout: unknown op");
1379211598Sed	}
1380211598Sed}
1381211598Sed
1382171069Sdelphij/*
1383171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system.
1384171069Sdelphij */
1385171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = {
1386171069Sdelphij	.vop_default =			&default_vnodeops,
1387171069Sdelphij	.vop_lookup =			vfs_cache_lookup,
1388171069Sdelphij	.vop_cachedlookup =		tmpfs_lookup,
1389171069Sdelphij	.vop_create =			tmpfs_create,
1390171069Sdelphij	.vop_mknod =			tmpfs_mknod,
1391171069Sdelphij	.vop_open =			tmpfs_open,
1392171069Sdelphij	.vop_close =			tmpfs_close,
1393171069Sdelphij	.vop_access =			tmpfs_access,
1394171069Sdelphij	.vop_getattr =			tmpfs_getattr,
1395171069Sdelphij	.vop_setattr =			tmpfs_setattr,
1396171069Sdelphij	.vop_read =			tmpfs_read,
1397171069Sdelphij	.vop_write =			tmpfs_write,
1398171069Sdelphij	.vop_fsync =			tmpfs_fsync,
1399171069Sdelphij	.vop_remove =			tmpfs_remove,
1400171069Sdelphij	.vop_link =			tmpfs_link,
1401171069Sdelphij	.vop_rename =			tmpfs_rename,
1402171069Sdelphij	.vop_mkdir =			tmpfs_mkdir,
1403171069Sdelphij	.vop_rmdir =			tmpfs_rmdir,
1404171069Sdelphij	.vop_symlink =			tmpfs_symlink,
1405171069Sdelphij	.vop_readdir =			tmpfs_readdir,
1406171069Sdelphij	.vop_readlink =			tmpfs_readlink,
1407171069Sdelphij	.vop_inactive =			tmpfs_inactive,
1408171069Sdelphij	.vop_reclaim =			tmpfs_reclaim,
1409171069Sdelphij	.vop_print =			tmpfs_print,
1410171069Sdelphij	.vop_pathconf =			tmpfs_pathconf,
1411171069Sdelphij	.vop_vptofh =			tmpfs_vptofh,
1412211598Sed	.vop_whiteout =			tmpfs_whiteout,
1413171069Sdelphij	.vop_bmap =			VOP_EOPNOTSUPP,
1414171069Sdelphij};
1415171069Sdelphij
1416