tmpfs_vnops.c revision 269167
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: stable/10/sys/fs/tmpfs/tmpfs_vnops.c 269167 2014-07-28 01:01:20Z kib $");
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>
48197850Sdelphij#include <sys/sf_buf.h>
49170808Sdelphij#include <sys/stat.h>
50170808Sdelphij#include <sys/systm.h>
51232960Sgleb#include <sys/sysctl.h>
52170808Sdelphij#include <sys/unistd.h>
53170808Sdelphij#include <sys/vnode.h>
54170808Sdelphij
55170808Sdelphij#include <vm/vm.h>
56239065Skib#include <vm/vm_param.h>
57170808Sdelphij#include <vm/vm_object.h>
58170808Sdelphij#include <vm/vm_page.h>
59170808Sdelphij#include <vm/vm_pager.h>
60188929Salc
61170808Sdelphij#include <fs/tmpfs/tmpfs_vnops.h>
62170808Sdelphij#include <fs/tmpfs/tmpfs.h>
63170808Sdelphij
64232960SglebSYSCTL_DECL(_vfs_tmpfs);
65232960Sgleb
66232960Sglebstatic volatile int tmpfs_rename_restarts;
67232960SglebSYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
68232960Sgleb    __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
69232960Sgleb    "Times rename had to restart due to lock contention");
70232960Sgleb
71171069Sdelphijstatic int
72170808Sdelphijtmpfs_lookup(struct vop_cachedlookup_args *v)
73170808Sdelphij{
74170808Sdelphij	struct vnode *dvp = v->a_dvp;
75170808Sdelphij	struct vnode **vpp = v->a_vpp;
76170808Sdelphij	struct componentname *cnp = v->a_cnp;
77170808Sdelphij
78170808Sdelphij	int error;
79170808Sdelphij	struct tmpfs_dirent *de;
80170808Sdelphij	struct tmpfs_node *dnode;
81170808Sdelphij
82170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
83170808Sdelphij	*vpp = NULLVP;
84170808Sdelphij
85170808Sdelphij	/* Check accessibility of requested node as a first step. */
86191990Sattilio	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
87170808Sdelphij	if (error != 0)
88170808Sdelphij		goto out;
89170808Sdelphij
90170808Sdelphij	/* We cannot be requesting the parent directory of the root node. */
91170808Sdelphij	MPASS(IMPLIES(dnode->tn_type == VDIR &&
92170808Sdelphij	    dnode->tn_dir.tn_parent == dnode,
93170808Sdelphij	    !(cnp->cn_flags & ISDOTDOT)));
94170808Sdelphij
95197953Sdelphij	TMPFS_ASSERT_LOCKED(dnode);
96197953Sdelphij	if (dnode->tn_dir.tn_parent == NULL) {
97197953Sdelphij		error = ENOENT;
98197953Sdelphij		goto out;
99197953Sdelphij	}
100170808Sdelphij	if (cnp->cn_flags & ISDOTDOT) {
101171799Sdelphij		int ltype = 0;
102171799Sdelphij
103176559Sattilio		ltype = VOP_ISLOCKED(dvp);
104171802Sdelphij		vhold(dvp);
105175294Sattilio		VOP_UNLOCK(dvp, 0);
106170808Sdelphij		/* Allocate a new vnode on the matching entry. */
107171799Sdelphij		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
108191990Sattilio		    cnp->cn_lkflags, vpp);
109170808Sdelphij
110175202Sattilio		vn_lock(dvp, ltype | LK_RETRY);
111171802Sdelphij		vdrop(dvp);
112170808Sdelphij	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
113170808Sdelphij		VREF(dvp);
114170808Sdelphij		*vpp = dvp;
115170808Sdelphij		error = 0;
116170808Sdelphij	} else {
117188318Skib		de = tmpfs_dir_lookup(dnode, NULL, cnp);
118211598Sed		if (de != NULL && de->td_node == NULL)
119211598Sed			cnp->cn_flags |= ISWHITEOUT;
120211598Sed		if (de == NULL || de->td_node == NULL) {
121170808Sdelphij			/* The entry was not found in the directory.
122170808Sdelphij			 * This is OK if we are creating or renaming an
123170808Sdelphij			 * entry and are working on the last component of
124170808Sdelphij			 * the path name. */
125170808Sdelphij			if ((cnp->cn_flags & ISLASTCN) &&
126170808Sdelphij			    (cnp->cn_nameiop == CREATE || \
127211598Sed			    cnp->cn_nameiop == RENAME ||
128211598Sed			    (cnp->cn_nameiop == DELETE &&
129211598Sed			    cnp->cn_flags & DOWHITEOUT &&
130211598Sed			    cnp->cn_flags & ISWHITEOUT))) {
131170808Sdelphij				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
132170808Sdelphij				    cnp->cn_thread);
133170808Sdelphij				if (error != 0)
134170808Sdelphij					goto out;
135170808Sdelphij
136170808Sdelphij				/* Keep the component name in the buffer for
137170808Sdelphij				 * future uses. */
138170808Sdelphij				cnp->cn_flags |= SAVENAME;
139170808Sdelphij
140170808Sdelphij				error = EJUSTRETURN;
141170808Sdelphij			} else
142170808Sdelphij				error = ENOENT;
143170808Sdelphij		} else {
144170808Sdelphij			struct tmpfs_node *tnode;
145170808Sdelphij
146170808Sdelphij			/* The entry was found, so get its associated
147170808Sdelphij			 * tmpfs_node. */
148170808Sdelphij			tnode = de->td_node;
149170808Sdelphij
150170808Sdelphij			/* If we are not at the last path component and
151170808Sdelphij			 * found a non-directory or non-link entry (which
152170808Sdelphij			 * may itself be pointing to a directory), raise
153170808Sdelphij			 * an error. */
154170808Sdelphij			if ((tnode->tn_type != VDIR &&
155170808Sdelphij			    tnode->tn_type != VLNK) &&
156170808Sdelphij			    !(cnp->cn_flags & ISLASTCN)) {
157170808Sdelphij				error = ENOTDIR;
158170808Sdelphij				goto out;
159170808Sdelphij			}
160170808Sdelphij
161170808Sdelphij			/* If we are deleting or renaming the entry, keep
162170808Sdelphij			 * track of its tmpfs_dirent so that it can be
163170808Sdelphij			 * easily deleted later. */
164170808Sdelphij			if ((cnp->cn_flags & ISLASTCN) &&
165170808Sdelphij			    (cnp->cn_nameiop == DELETE ||
166170808Sdelphij			    cnp->cn_nameiop == RENAME)) {
167170808Sdelphij				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
168170808Sdelphij				    cnp->cn_thread);
169170808Sdelphij				if (error != 0)
170170808Sdelphij					goto out;
171171070Sdelphij
172170808Sdelphij				/* Allocate a new vnode on the matching entry. */
173171799Sdelphij				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
174191990Sattilio						cnp->cn_lkflags, vpp);
175170808Sdelphij				if (error != 0)
176170808Sdelphij					goto out;
177170808Sdelphij
178170808Sdelphij				if ((dnode->tn_mode & S_ISTXT) &&
179170808Sdelphij				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
180170808Sdelphij				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
181170808Sdelphij					error = EPERM;
182170808Sdelphij					vput(*vpp);
183170808Sdelphij					*vpp = NULL;
184170808Sdelphij					goto out;
185171070Sdelphij				}
186170808Sdelphij				cnp->cn_flags |= SAVENAME;
187171799Sdelphij			} else {
188171799Sdelphij				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
189191990Sattilio						cnp->cn_lkflags, vpp);
190170808Sdelphij			}
191170808Sdelphij		}
192170808Sdelphij	}
193170808Sdelphij
194170808Sdelphij	/* Store the result of this lookup in the cache.  Avoid this if the
195170808Sdelphij	 * request was for creation, as it does not improve timings on
196170808Sdelphij	 * emprical tests. */
197170808Sdelphij	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
198170808Sdelphij		cache_enter(dvp, *vpp, cnp);
199170808Sdelphij
200170808Sdelphijout:
201170808Sdelphij	/* If there were no errors, *vpp cannot be null and it must be
202170808Sdelphij	 * locked. */
203176559Sattilio	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
204170808Sdelphij
205170808Sdelphij	return error;
206170808Sdelphij}
207170808Sdelphij
208171069Sdelphijstatic int
209170808Sdelphijtmpfs_create(struct vop_create_args *v)
210170808Sdelphij{
211170808Sdelphij	struct vnode *dvp = v->a_dvp;
212170808Sdelphij	struct vnode **vpp = v->a_vpp;
213170808Sdelphij	struct componentname *cnp = v->a_cnp;
214170808Sdelphij	struct vattr *vap = v->a_vap;
215170808Sdelphij
216170808Sdelphij	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
217170808Sdelphij
218170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
219170808Sdelphij}
220170808Sdelphij
221171069Sdelphijstatic int
222170808Sdelphijtmpfs_mknod(struct vop_mknod_args *v)
223170808Sdelphij{
224170808Sdelphij	struct vnode *dvp = v->a_dvp;
225170808Sdelphij	struct vnode **vpp = v->a_vpp;
226170808Sdelphij	struct componentname *cnp = v->a_cnp;
227170808Sdelphij	struct vattr *vap = v->a_vap;
228170808Sdelphij
229170808Sdelphij	if (vap->va_type != VBLK && vap->va_type != VCHR &&
230170808Sdelphij	    vap->va_type != VFIFO)
231170808Sdelphij		return EINVAL;
232170808Sdelphij
233170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
234170808Sdelphij}
235170808Sdelphij
236171069Sdelphijstatic int
237170808Sdelphijtmpfs_open(struct vop_open_args *v)
238170808Sdelphij{
239170808Sdelphij	struct vnode *vp = v->a_vp;
240170808Sdelphij	int mode = v->a_mode;
241170808Sdelphij
242170808Sdelphij	int error;
243170808Sdelphij	struct tmpfs_node *node;
244170808Sdelphij
245176559Sattilio	MPASS(VOP_ISLOCKED(vp));
246170808Sdelphij
247170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
248171070Sdelphij
249170808Sdelphij	/* The file is still active but all its names have been removed
250170808Sdelphij	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
251170808Sdelphij	 * it is about to die. */
252170808Sdelphij	if (node->tn_links < 1)
253170808Sdelphij		return (ENOENT);
254170808Sdelphij
255170808Sdelphij	/* If the file is marked append-only, deny write requests. */
256170808Sdelphij	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
257170808Sdelphij		error = EPERM;
258170808Sdelphij	else {
259170808Sdelphij		error = 0;
260250190Skib		/* For regular files, the call below is nop. */
261171070Sdelphij		vnode_create_vobject(vp, node->tn_size, v->a_td);
262170808Sdelphij	}
263170808Sdelphij
264176559Sattilio	MPASS(VOP_ISLOCKED(vp));
265170808Sdelphij	return error;
266170808Sdelphij}
267170808Sdelphij
268171069Sdelphijstatic int
269170808Sdelphijtmpfs_close(struct vop_close_args *v)
270170808Sdelphij{
271170808Sdelphij	struct vnode *vp = v->a_vp;
272170808Sdelphij
273218949Salc	/* Update node times. */
274218949Salc	tmpfs_update(vp);
275170808Sdelphij
276218949Salc	return (0);
277170808Sdelphij}
278170808Sdelphij
279170808Sdelphijint
280170808Sdelphijtmpfs_access(struct vop_access_args *v)
281170808Sdelphij{
282170808Sdelphij	struct vnode *vp = v->a_vp;
283184413Strasz	accmode_t accmode = v->a_accmode;
284170808Sdelphij	struct ucred *cred = v->a_cred;
285170808Sdelphij
286170808Sdelphij	int error;
287170808Sdelphij	struct tmpfs_node *node;
288170808Sdelphij
289176559Sattilio	MPASS(VOP_ISLOCKED(vp));
290170808Sdelphij
291170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
292170808Sdelphij
293170808Sdelphij	switch (vp->v_type) {
294170808Sdelphij	case VDIR:
295170808Sdelphij		/* FALLTHROUGH */
296170808Sdelphij	case VLNK:
297170808Sdelphij		/* FALLTHROUGH */
298170808Sdelphij	case VREG:
299184413Strasz		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
300170808Sdelphij			error = EROFS;
301170808Sdelphij			goto out;
302170808Sdelphij		}
303170808Sdelphij		break;
304170808Sdelphij
305170808Sdelphij	case VBLK:
306170808Sdelphij		/* FALLTHROUGH */
307170808Sdelphij	case VCHR:
308170808Sdelphij		/* FALLTHROUGH */
309170808Sdelphij	case VSOCK:
310170808Sdelphij		/* FALLTHROUGH */
311170808Sdelphij	case VFIFO:
312170808Sdelphij		break;
313170808Sdelphij
314170808Sdelphij	default:
315170808Sdelphij		error = EINVAL;
316170808Sdelphij		goto out;
317170808Sdelphij	}
318170808Sdelphij
319184413Strasz	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
320170808Sdelphij		error = EPERM;
321170808Sdelphij		goto out;
322170808Sdelphij	}
323170808Sdelphij
324170808Sdelphij	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
325184413Strasz	    node->tn_gid, accmode, cred, NULL);
326170808Sdelphij
327170808Sdelphijout:
328176559Sattilio	MPASS(VOP_ISLOCKED(vp));
329170808Sdelphij
330170808Sdelphij	return error;
331170808Sdelphij}
332170808Sdelphij
333170808Sdelphijint
334170808Sdelphijtmpfs_getattr(struct vop_getattr_args *v)
335170808Sdelphij{
336170808Sdelphij	struct vnode *vp = v->a_vp;
337170808Sdelphij	struct vattr *vap = v->a_vap;
338170808Sdelphij
339170808Sdelphij	struct tmpfs_node *node;
340170808Sdelphij
341170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
342170808Sdelphij
343170808Sdelphij	tmpfs_update(vp);
344170808Sdelphij
345170808Sdelphij	vap->va_type = vp->v_type;
346170808Sdelphij	vap->va_mode = node->tn_mode;
347170808Sdelphij	vap->va_nlink = node->tn_links;
348170808Sdelphij	vap->va_uid = node->tn_uid;
349170808Sdelphij	vap->va_gid = node->tn_gid;
350170808Sdelphij	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
351170808Sdelphij	vap->va_fileid = node->tn_id;
352170808Sdelphij	vap->va_size = node->tn_size;
353170808Sdelphij	vap->va_blocksize = PAGE_SIZE;
354170808Sdelphij	vap->va_atime = node->tn_atime;
355170808Sdelphij	vap->va_mtime = node->tn_mtime;
356170808Sdelphij	vap->va_ctime = node->tn_ctime;
357170808Sdelphij	vap->va_birthtime = node->tn_birthtime;
358170808Sdelphij	vap->va_gen = node->tn_gen;
359170808Sdelphij	vap->va_flags = node->tn_flags;
360170808Sdelphij	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
361183214Skib		node->tn_rdev : NODEV;
362170808Sdelphij	vap->va_bytes = round_page(node->tn_size);
363183212Skib	vap->va_filerev = 0;
364170808Sdelphij
365170808Sdelphij	return 0;
366170808Sdelphij}
367170808Sdelphij
368170808Sdelphijint
369170808Sdelphijtmpfs_setattr(struct vop_setattr_args *v)
370170808Sdelphij{
371170808Sdelphij	struct vnode *vp = v->a_vp;
372170808Sdelphij	struct vattr *vap = v->a_vap;
373170808Sdelphij	struct ucred *cred = v->a_cred;
374182371Sattilio	struct thread *td = curthread;
375170808Sdelphij
376170808Sdelphij	int error;
377170808Sdelphij
378176559Sattilio	MPASS(VOP_ISLOCKED(vp));
379170808Sdelphij
380170808Sdelphij	error = 0;
381170808Sdelphij
382170808Sdelphij	/* Abort if any unsettable attribute is given. */
383170808Sdelphij	if (vap->va_type != VNON ||
384170808Sdelphij	    vap->va_nlink != VNOVAL ||
385170808Sdelphij	    vap->va_fsid != VNOVAL ||
386170808Sdelphij	    vap->va_fileid != VNOVAL ||
387170808Sdelphij	    vap->va_blocksize != VNOVAL ||
388170808Sdelphij	    vap->va_gen != VNOVAL ||
389170808Sdelphij	    vap->va_rdev != VNOVAL ||
390170808Sdelphij	    vap->va_bytes != VNOVAL)
391170808Sdelphij		error = EINVAL;
392170808Sdelphij
393170808Sdelphij	if (error == 0 && (vap->va_flags != VNOVAL))
394182371Sattilio		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
395170808Sdelphij
396170808Sdelphij	if (error == 0 && (vap->va_size != VNOVAL))
397182371Sattilio		error = tmpfs_chsize(vp, vap->va_size, cred, td);
398170808Sdelphij
399170808Sdelphij	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
400182371Sattilio		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
401170808Sdelphij
402170808Sdelphij	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
403182371Sattilio		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
404170808Sdelphij
405170808Sdelphij	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
406170808Sdelphij	    vap->va_atime.tv_nsec != VNOVAL) ||
407170808Sdelphij	    (vap->va_mtime.tv_sec != VNOVAL &&
408170808Sdelphij	    vap->va_mtime.tv_nsec != VNOVAL) ||
409170808Sdelphij	    (vap->va_birthtime.tv_sec != VNOVAL &&
410170808Sdelphij	    vap->va_birthtime.tv_nsec != VNOVAL)))
411267816Skib		error = tmpfs_chtimes(vp, vap, cred, td);
412170808Sdelphij
413170808Sdelphij	/* Update the node times.  We give preference to the error codes
414170808Sdelphij	 * generated by this function rather than the ones that may arise
415170808Sdelphij	 * from tmpfs_update. */
416170808Sdelphij	tmpfs_update(vp);
417170808Sdelphij
418176559Sattilio	MPASS(VOP_ISLOCKED(vp));
419170808Sdelphij
420170808Sdelphij	return error;
421170808Sdelphij}
422170808Sdelphij
423197850Sdelphijstatic int
424170808Sdelphijtmpfs_read(struct vop_read_args *v)
425170808Sdelphij{
426254601Skib	struct vnode *vp;
427254601Skib	struct uio *uio;
428170808Sdelphij	struct tmpfs_node *node;
429170808Sdelphij
430254601Skib	vp = v->a_vp;
431254601Skib	if (vp->v_type != VREG)
432254601Skib		return (EISDIR);
433254601Skib	uio = v->a_uio;
434254601Skib	if (uio->uio_offset < 0)
435254601Skib		return (EINVAL);
436170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
437170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
438254601Skib	return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
439170808Sdelphij}
440170808Sdelphij
441171069Sdelphijstatic int
442170808Sdelphijtmpfs_write(struct vop_write_args *v)
443170808Sdelphij{
444254601Skib	struct vnode *vp;
445254601Skib	struct uio *uio;
446254601Skib	struct tmpfs_node *node;
447254601Skib	off_t oldsize;
448254601Skib	int error, ioflag;
449170808Sdelphij	boolean_t extended;
450170808Sdelphij
451254601Skib	vp = v->a_vp;
452254601Skib	uio = v->a_uio;
453254601Skib	ioflag = v->a_ioflag;
454254601Skib	error = 0;
455170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
456170808Sdelphij	oldsize = node->tn_size;
457170808Sdelphij
458254601Skib	if (uio->uio_offset < 0 || vp->v_type != VREG)
459254601Skib		return (EINVAL);
460254601Skib	if (uio->uio_resid == 0)
461254601Skib		return (0);
462170808Sdelphij	if (ioflag & IO_APPEND)
463170808Sdelphij		uio->uio_offset = node->tn_size;
464171070Sdelphij	if (uio->uio_offset + uio->uio_resid >
465170808Sdelphij	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
466170808Sdelphij		return (EFBIG);
467207719Strasz	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
468207662Strasz		return (EFBIG);
469170808Sdelphij	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
470170808Sdelphij	if (extended) {
471230180Salc		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
472230180Salc		    FALSE);
473170808Sdelphij		if (error != 0)
474170808Sdelphij			goto out;
475170808Sdelphij	}
476170808Sdelphij
477254601Skib	error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
478170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
479170808Sdelphij	    (extended ? TMPFS_NODE_CHANGED : 0);
480170808Sdelphij	if (node->tn_mode & (S_ISUID | S_ISGID)) {
481170808Sdelphij		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
482170808Sdelphij			node->tn_mode &= ~(S_ISUID | S_ISGID);
483170808Sdelphij	}
484170808Sdelphij	if (error != 0)
485230180Salc		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
486170808Sdelphij
487170808Sdelphijout:
488170808Sdelphij	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
489170808Sdelphij	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
490170808Sdelphij
491254601Skib	return (error);
492170808Sdelphij}
493170808Sdelphij
494171069Sdelphijstatic int
495170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v)
496170808Sdelphij{
497170808Sdelphij	struct vnode *vp = v->a_vp;
498170808Sdelphij
499176559Sattilio	MPASS(VOP_ISLOCKED(vp));
500170808Sdelphij
501170808Sdelphij	tmpfs_update(vp);
502170808Sdelphij
503170808Sdelphij	return 0;
504170808Sdelphij}
505170808Sdelphij
506171069Sdelphijstatic int
507170808Sdelphijtmpfs_remove(struct vop_remove_args *v)
508170808Sdelphij{
509170808Sdelphij	struct vnode *dvp = v->a_dvp;
510170808Sdelphij	struct vnode *vp = v->a_vp;
511170808Sdelphij
512170808Sdelphij	int error;
513170808Sdelphij	struct tmpfs_dirent *de;
514170808Sdelphij	struct tmpfs_mount *tmp;
515170808Sdelphij	struct tmpfs_node *dnode;
516170808Sdelphij	struct tmpfs_node *node;
517170808Sdelphij
518176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
519176559Sattilio	MPASS(VOP_ISLOCKED(vp));
520170808Sdelphij
521170808Sdelphij	if (vp->v_type == VDIR) {
522170808Sdelphij		error = EISDIR;
523170808Sdelphij		goto out;
524170808Sdelphij	}
525170808Sdelphij
526170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
527170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
528170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
529188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
530170808Sdelphij	MPASS(de != NULL);
531170808Sdelphij
532170808Sdelphij	/* Files marked as immutable or append-only cannot be deleted. */
533170808Sdelphij	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
534170808Sdelphij	    (dnode->tn_flags & APPEND)) {
535170808Sdelphij		error = EPERM;
536170808Sdelphij		goto out;
537170808Sdelphij	}
538170808Sdelphij
539170808Sdelphij	/* Remove the entry from the directory; as it is a file, we do not
540170808Sdelphij	 * have to change the number of hard links of the directory. */
541170808Sdelphij	tmpfs_dir_detach(dvp, de);
542211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
543211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
544170808Sdelphij
545170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
546170808Sdelphij	 * referred by it will not be removed until the vnode is really
547170808Sdelphij	 * reclaimed. */
548245115Sgleb	tmpfs_free_dirent(tmp, de);
549170808Sdelphij
550218949Salc	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
551170808Sdelphij	error = 0;
552170808Sdelphij
553170808Sdelphijout:
554170808Sdelphij
555170808Sdelphij	return error;
556170808Sdelphij}
557170808Sdelphij
558171069Sdelphijstatic int
559170808Sdelphijtmpfs_link(struct vop_link_args *v)
560170808Sdelphij{
561170808Sdelphij	struct vnode *dvp = v->a_tdvp;
562170808Sdelphij	struct vnode *vp = v->a_vp;
563170808Sdelphij	struct componentname *cnp = v->a_cnp;
564170808Sdelphij
565170808Sdelphij	int error;
566170808Sdelphij	struct tmpfs_dirent *de;
567170808Sdelphij	struct tmpfs_node *node;
568170808Sdelphij
569176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
570170808Sdelphij	MPASS(cnp->cn_flags & HASBUF);
571170808Sdelphij	MPASS(dvp != vp); /* XXX When can this be false? */
572170808Sdelphij
573170808Sdelphij	/* XXX: Why aren't the following two tests done by the caller? */
574170808Sdelphij
575170808Sdelphij	/* Hard links of directories are forbidden. */
576170808Sdelphij	if (vp->v_type == VDIR) {
577170808Sdelphij		error = EPERM;
578170808Sdelphij		goto out;
579170808Sdelphij	}
580170808Sdelphij
581170808Sdelphij	/* Cannot create cross-device links. */
582170808Sdelphij	if (dvp->v_mount != vp->v_mount) {
583170808Sdelphij		error = EXDEV;
584170808Sdelphij		goto out;
585170808Sdelphij	}
586170808Sdelphij
587269167Skib	node = VP_TO_TMPFS_NODE(vp);
588269167Skib
589170808Sdelphij	/* Ensure that we do not overflow the maximum number of links imposed
590170808Sdelphij	 * by the system. */
591170808Sdelphij	MPASS(node->tn_links <= LINK_MAX);
592170808Sdelphij	if (node->tn_links == LINK_MAX) {
593170808Sdelphij		error = EMLINK;
594170808Sdelphij		goto out;
595170808Sdelphij	}
596170808Sdelphij
597170808Sdelphij	/* We cannot create links of files marked immutable or append-only. */
598170808Sdelphij	if (node->tn_flags & (IMMUTABLE | APPEND)) {
599170808Sdelphij		error = EPERM;
600170808Sdelphij		goto out;
601170808Sdelphij	}
602170808Sdelphij
603170808Sdelphij	/* Allocate a new directory entry to represent the node. */
604170808Sdelphij	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
605170808Sdelphij	    cnp->cn_nameptr, cnp->cn_namelen, &de);
606170808Sdelphij	if (error != 0)
607170808Sdelphij		goto out;
608170808Sdelphij
609170808Sdelphij	/* Insert the new directory entry into the appropriate directory. */
610211598Sed	if (cnp->cn_flags & ISWHITEOUT)
611211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
612170808Sdelphij	tmpfs_dir_attach(dvp, de);
613170808Sdelphij
614170808Sdelphij	/* vp link count has changed, so update node times. */
615170808Sdelphij	node->tn_status |= TMPFS_NODE_CHANGED;
616170808Sdelphij	tmpfs_update(vp);
617170808Sdelphij
618170808Sdelphij	error = 0;
619171070Sdelphij
620170808Sdelphijout:
621170808Sdelphij	return error;
622170808Sdelphij}
623170808Sdelphij
624232960Sgleb/*
625232960Sgleb * We acquire all but fdvp locks using non-blocking acquisitions.  If we
626232960Sgleb * fail to acquire any lock in the path we will drop all held locks,
627232960Sgleb * acquire the new lock in a blocking fashion, and then release it and
628232960Sgleb * restart the rename.  This acquire/release step ensures that we do not
629232960Sgleb * spin on a lock waiting for release.  On error release all vnode locks
630232960Sgleb * and decrement references the way tmpfs_rename() would do.
631232960Sgleb */
632171069Sdelphijstatic int
633232960Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
634232960Sgleb    struct vnode *tdvp, struct vnode **tvpp,
635232960Sgleb    struct componentname *fcnp, struct componentname *tcnp)
636232960Sgleb{
637232960Sgleb	struct vnode *nvp;
638232960Sgleb	struct mount *mp;
639232960Sgleb	struct tmpfs_dirent *de;
640232960Sgleb	int error, restarts = 0;
641232960Sgleb
642232960Sgleb	VOP_UNLOCK(tdvp, 0);
643232960Sgleb	if (*tvpp != NULL && *tvpp != tdvp)
644232960Sgleb		VOP_UNLOCK(*tvpp, 0);
645232960Sgleb	mp = fdvp->v_mount;
646232960Sgleb
647232960Sglebrelock:
648232960Sgleb	restarts += 1;
649232960Sgleb	error = vn_lock(fdvp, LK_EXCLUSIVE);
650232960Sgleb	if (error)
651232960Sgleb		goto releout;
652232960Sgleb	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
653232960Sgleb		VOP_UNLOCK(fdvp, 0);
654232960Sgleb		error = vn_lock(tdvp, LK_EXCLUSIVE);
655232960Sgleb		if (error)
656232960Sgleb			goto releout;
657232960Sgleb		VOP_UNLOCK(tdvp, 0);
658232960Sgleb		goto relock;
659232960Sgleb	}
660232960Sgleb	/*
661232960Sgleb	 * Re-resolve fvp to be certain it still exists and fetch the
662232960Sgleb	 * correct vnode.
663232960Sgleb	 */
664232960Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
665232960Sgleb	if (de == NULL) {
666232960Sgleb		VOP_UNLOCK(fdvp, 0);
667232960Sgleb		VOP_UNLOCK(tdvp, 0);
668232960Sgleb		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
669232960Sgleb		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
670232960Sgleb			error = EINVAL;
671232960Sgleb		else
672232960Sgleb			error = ENOENT;
673232960Sgleb		goto releout;
674232960Sgleb	}
675232960Sgleb	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
676232960Sgleb	if (error != 0) {
677232960Sgleb		VOP_UNLOCK(fdvp, 0);
678232960Sgleb		VOP_UNLOCK(tdvp, 0);
679232960Sgleb		if (error != EBUSY)
680232960Sgleb			goto releout;
681232960Sgleb		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
682232960Sgleb		if (error != 0)
683232960Sgleb			goto releout;
684232960Sgleb		VOP_UNLOCK(nvp, 0);
685232960Sgleb		/*
686232960Sgleb		 * Concurrent rename race.
687232960Sgleb		 */
688232960Sgleb		if (nvp == tdvp) {
689232960Sgleb			vrele(nvp);
690232960Sgleb			error = EINVAL;
691232960Sgleb			goto releout;
692232960Sgleb		}
693232960Sgleb		vrele(*fvpp);
694232960Sgleb		*fvpp = nvp;
695232960Sgleb		goto relock;
696232960Sgleb	}
697232960Sgleb	vrele(*fvpp);
698232960Sgleb	*fvpp = nvp;
699232960Sgleb	VOP_UNLOCK(*fvpp, 0);
700232960Sgleb	/*
701232960Sgleb	 * Re-resolve tvp and acquire the vnode lock if present.
702232960Sgleb	 */
703232960Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
704232960Sgleb	/*
705232960Sgleb	 * If tvp disappeared we just carry on.
706232960Sgleb	 */
707232960Sgleb	if (de == NULL && *tvpp != NULL) {
708232960Sgleb		vrele(*tvpp);
709232960Sgleb		*tvpp = NULL;
710232960Sgleb	}
711232960Sgleb	/*
712232960Sgleb	 * Get the tvp ino if the lookup succeeded.  We may have to restart
713232960Sgleb	 * if the non-blocking acquire fails.
714232960Sgleb	 */
715232960Sgleb	if (de != NULL) {
716232960Sgleb		nvp = NULL;
717232960Sgleb		error = tmpfs_alloc_vp(mp, de->td_node,
718232960Sgleb		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
719232960Sgleb		if (*tvpp != NULL)
720232960Sgleb			vrele(*tvpp);
721232960Sgleb		*tvpp = nvp;
722232960Sgleb		if (error != 0) {
723232960Sgleb			VOP_UNLOCK(fdvp, 0);
724232960Sgleb			VOP_UNLOCK(tdvp, 0);
725232960Sgleb			if (error != EBUSY)
726232960Sgleb				goto releout;
727232960Sgleb			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
728232960Sgleb			    &nvp);
729232960Sgleb			if (error != 0)
730232960Sgleb				goto releout;
731232960Sgleb			VOP_UNLOCK(nvp, 0);
732232960Sgleb			/*
733232960Sgleb			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
734232960Sgleb			 */
735232960Sgleb			if (nvp == fdvp) {
736232960Sgleb				error = ENOTEMPTY;
737232960Sgleb				goto releout;
738232960Sgleb			}
739232960Sgleb			goto relock;
740232960Sgleb		}
741232960Sgleb	}
742232960Sgleb	tmpfs_rename_restarts += restarts;
743232960Sgleb
744232960Sgleb	return (0);
745232960Sgleb
746232960Sglebreleout:
747232960Sgleb	vrele(fdvp);
748232960Sgleb	vrele(*fvpp);
749232960Sgleb	vrele(tdvp);
750232960Sgleb	if (*tvpp != NULL)
751232960Sgleb		vrele(*tvpp);
752232960Sgleb	tmpfs_rename_restarts += restarts;
753232960Sgleb
754232960Sgleb	return (error);
755232960Sgleb}
756232960Sgleb
757232960Sglebstatic int
758170808Sdelphijtmpfs_rename(struct vop_rename_args *v)
759170808Sdelphij{
760170808Sdelphij	struct vnode *fdvp = v->a_fdvp;
761170808Sdelphij	struct vnode *fvp = v->a_fvp;
762170808Sdelphij	struct componentname *fcnp = v->a_fcnp;
763170808Sdelphij	struct vnode *tdvp = v->a_tdvp;
764170808Sdelphij	struct vnode *tvp = v->a_tvp;
765170808Sdelphij	struct componentname *tcnp = v->a_tcnp;
766232960Sgleb	struct mount *mp = NULL;
767170808Sdelphij
768170808Sdelphij	char *newname;
769170808Sdelphij	int error;
770170808Sdelphij	struct tmpfs_dirent *de;
771197953Sdelphij	struct tmpfs_mount *tmp;
772170808Sdelphij	struct tmpfs_node *fdnode;
773170808Sdelphij	struct tmpfs_node *fnode;
774171799Sdelphij	struct tmpfs_node *tnode;
775170808Sdelphij	struct tmpfs_node *tdnode;
776170808Sdelphij
777176559Sattilio	MPASS(VOP_ISLOCKED(tdvp));
778176559Sattilio	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
779170808Sdelphij	MPASS(fcnp->cn_flags & HASBUF);
780170808Sdelphij	MPASS(tcnp->cn_flags & HASBUF);
781170808Sdelphij
782170808Sdelphij	/* Disallow cross-device renames.
783170808Sdelphij	 * XXX Why isn't this done by the caller? */
784170808Sdelphij	if (fvp->v_mount != tdvp->v_mount ||
785170808Sdelphij	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
786170808Sdelphij		error = EXDEV;
787170808Sdelphij		goto out;
788170808Sdelphij	}
789170808Sdelphij
790170808Sdelphij	/* If source and target are the same file, there is nothing to do. */
791170808Sdelphij	if (fvp == tvp) {
792170808Sdelphij		error = 0;
793170808Sdelphij		goto out;
794170808Sdelphij	}
795170808Sdelphij
796173725Sdelphij	/* If we need to move the directory between entries, lock the
797173725Sdelphij	 * source so that we can safely operate on it. */
798232960Sgleb	if (fdvp != tdvp && fdvp != tvp) {
799232960Sgleb		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
800232960Sgleb			mp = tdvp->v_mount;
801232960Sgleb			error = vfs_busy(mp, 0);
802232960Sgleb			if (error != 0) {
803232960Sgleb				mp = NULL;
804232960Sgleb				goto out;
805232960Sgleb			}
806232960Sgleb			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
807232960Sgleb			    fcnp, tcnp);
808232960Sgleb			if (error != 0) {
809232960Sgleb				vfs_unbusy(mp);
810232960Sgleb				return (error);
811232960Sgleb			}
812232960Sgleb			ASSERT_VOP_ELOCKED(fdvp,
813232960Sgleb			    "tmpfs_rename: fdvp not locked");
814232960Sgleb			ASSERT_VOP_ELOCKED(tdvp,
815232960Sgleb			    "tmpfs_rename: tdvp not locked");
816232960Sgleb			if (tvp != NULL)
817232960Sgleb				ASSERT_VOP_ELOCKED(tvp,
818232960Sgleb				    "tmpfs_rename: tvp not locked");
819232960Sgleb			if (fvp == tvp) {
820232960Sgleb				error = 0;
821232960Sgleb				goto out_locked;
822232960Sgleb			}
823232960Sgleb		}
824232960Sgleb	}
825232960Sgleb
826232960Sgleb	tmp = VFS_TO_TMPFS(tdvp->v_mount);
827232960Sgleb	tdnode = VP_TO_TMPFS_DIR(tdvp);
828232960Sgleb	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
829173725Sdelphij	fdnode = VP_TO_TMPFS_DIR(fdvp);
830173725Sdelphij	fnode = VP_TO_TMPFS_NODE(fvp);
831188318Skib	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
832173725Sdelphij
833212305Sivoras	/* Entry can disappear before we lock fdvp,
834212305Sivoras	 * also avoid manipulating '.' and '..' entries. */
835170808Sdelphij	if (de == NULL) {
836212305Sivoras		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
837212305Sivoras		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
838212305Sivoras			error = EINVAL;
839212305Sivoras		else
840212305Sivoras			error = ENOENT;
841173725Sdelphij		goto out_locked;
842170808Sdelphij	}
843170808Sdelphij	MPASS(de->td_node == fnode);
844170808Sdelphij
845171070Sdelphij	/* If re-naming a directory to another preexisting directory
846170808Sdelphij	 * ensure that the target directory is empty so that its
847171070Sdelphij	 * removal causes no side effects.
848170808Sdelphij	 * Kern_rename gurantees the destination to be a directory
849170808Sdelphij	 * if the source is one. */
850170808Sdelphij	if (tvp != NULL) {
851171799Sdelphij		MPASS(tnode != NULL);
852171070Sdelphij
853170808Sdelphij		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
854170808Sdelphij		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
855170808Sdelphij			error = EPERM;
856173725Sdelphij			goto out_locked;
857170808Sdelphij		}
858170808Sdelphij
859171799Sdelphij		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
860171799Sdelphij			if (tnode->tn_size > 0) {
861171799Sdelphij				error = ENOTEMPTY;
862173725Sdelphij				goto out_locked;
863171799Sdelphij			}
864171799Sdelphij		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
865171799Sdelphij			error = ENOTDIR;
866173725Sdelphij			goto out_locked;
867171799Sdelphij		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
868171799Sdelphij			error = EISDIR;
869173725Sdelphij			goto out_locked;
870171799Sdelphij		} else {
871171799Sdelphij			MPASS(fnode->tn_type != VDIR &&
872171799Sdelphij				tnode->tn_type != VDIR);
873170808Sdelphij		}
874170808Sdelphij	}
875170808Sdelphij
876170808Sdelphij	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
877170808Sdelphij	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
878170808Sdelphij		error = EPERM;
879170808Sdelphij		goto out_locked;
880170808Sdelphij	}
881170808Sdelphij
882170808Sdelphij	/* Ensure that we have enough memory to hold the new name, if it
883170808Sdelphij	 * has to be changed. */
884170808Sdelphij	if (fcnp->cn_namelen != tcnp->cn_namelen ||
885183299Sobrien	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
886171087Sdelphij		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
887170808Sdelphij	} else
888170808Sdelphij		newname = NULL;
889170808Sdelphij
890170808Sdelphij	/* If the node is being moved to another directory, we have to do
891170808Sdelphij	 * the move. */
892170808Sdelphij	if (fdnode != tdnode) {
893170808Sdelphij		/* In case we are moving a directory, we have to adjust its
894170808Sdelphij		 * parent to point to the new parent. */
895170808Sdelphij		if (de->td_node->tn_type == VDIR) {
896170808Sdelphij			struct tmpfs_node *n;
897170808Sdelphij
898170808Sdelphij			/* Ensure the target directory is not a child of the
899170808Sdelphij			 * directory being moved.  Otherwise, we'd end up
900170808Sdelphij			 * with stale nodes. */
901170808Sdelphij			n = tdnode;
902197953Sdelphij			/* TMPFS_LOCK garanties that no nodes are freed while
903197953Sdelphij			 * traversing the list. Nodes can only be marked as
904197953Sdelphij			 * removed: tn_parent == NULL. */
905197953Sdelphij			TMPFS_LOCK(tmp);
906197953Sdelphij			TMPFS_NODE_LOCK(n);
907170808Sdelphij			while (n != n->tn_dir.tn_parent) {
908197953Sdelphij				struct tmpfs_node *parent;
909197953Sdelphij
910170808Sdelphij				if (n == fnode) {
911197953Sdelphij					TMPFS_NODE_UNLOCK(n);
912197953Sdelphij					TMPFS_UNLOCK(tmp);
913170808Sdelphij					error = EINVAL;
914170808Sdelphij					if (newname != NULL)
915171087Sdelphij						    free(newname, M_TMPFSNAME);
916170808Sdelphij					goto out_locked;
917170808Sdelphij				}
918197953Sdelphij				parent = n->tn_dir.tn_parent;
919197953Sdelphij				TMPFS_NODE_UNLOCK(n);
920197953Sdelphij				if (parent == NULL) {
921197953Sdelphij					n = NULL;
922197953Sdelphij					break;
923197953Sdelphij				}
924197953Sdelphij				TMPFS_NODE_LOCK(parent);
925197953Sdelphij				if (parent->tn_dir.tn_parent == NULL) {
926197953Sdelphij					TMPFS_NODE_UNLOCK(parent);
927197953Sdelphij					n = NULL;
928197953Sdelphij					break;
929197953Sdelphij				}
930197953Sdelphij				n = parent;
931170808Sdelphij			}
932197953Sdelphij			TMPFS_UNLOCK(tmp);
933197953Sdelphij			if (n == NULL) {
934197953Sdelphij				error = EINVAL;
935197953Sdelphij				if (newname != NULL)
936197953Sdelphij					    free(newname, M_TMPFSNAME);
937197953Sdelphij				goto out_locked;
938197953Sdelphij			}
939197953Sdelphij			TMPFS_NODE_UNLOCK(n);
940170808Sdelphij
941170808Sdelphij			/* Adjust the parent pointer. */
942170808Sdelphij			TMPFS_VALIDATE_DIR(fnode);
943197953Sdelphij			TMPFS_NODE_LOCK(de->td_node);
944170808Sdelphij			de->td_node->tn_dir.tn_parent = tdnode;
945197953Sdelphij			TMPFS_NODE_UNLOCK(de->td_node);
946170808Sdelphij
947170808Sdelphij			/* As a result of changing the target of the '..'
948170808Sdelphij			 * entry, the link count of the source and target
949170808Sdelphij			 * directories has to be adjusted. */
950197953Sdelphij			TMPFS_NODE_LOCK(tdnode);
951197953Sdelphij			TMPFS_ASSERT_LOCKED(tdnode);
952197953Sdelphij			tdnode->tn_links++;
953197953Sdelphij			TMPFS_NODE_UNLOCK(tdnode);
954197953Sdelphij
955197953Sdelphij			TMPFS_NODE_LOCK(fdnode);
956197953Sdelphij			TMPFS_ASSERT_LOCKED(fdnode);
957170808Sdelphij			fdnode->tn_links--;
958197953Sdelphij			TMPFS_NODE_UNLOCK(fdnode);
959170808Sdelphij		}
960170808Sdelphij	}
961170808Sdelphij
962245115Sgleb	/* Do the move: just remove the entry from the source directory
963245115Sgleb	 * and insert it into the target one. */
964245115Sgleb	tmpfs_dir_detach(fdvp, de);
965245115Sgleb
966245115Sgleb	if (fcnp->cn_flags & DOWHITEOUT)
967245115Sgleb		tmpfs_dir_whiteout_add(fdvp, fcnp);
968245115Sgleb	if (tcnp->cn_flags & ISWHITEOUT)
969245115Sgleb		tmpfs_dir_whiteout_remove(tdvp, tcnp);
970245115Sgleb
971170808Sdelphij	/* If the name has changed, we need to make it effective by changing
972170808Sdelphij	 * it in the directory entry. */
973170808Sdelphij	if (newname != NULL) {
974170808Sdelphij		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
975170808Sdelphij
976245115Sgleb		free(de->ud.td_name, M_TMPFSNAME);
977245115Sgleb		de->ud.td_name = newname;
978245115Sgleb		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
979170808Sdelphij
980170808Sdelphij		fnode->tn_status |= TMPFS_NODE_CHANGED;
981170808Sdelphij		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
982170808Sdelphij	}
983170808Sdelphij
984170808Sdelphij	/* If we are overwriting an entry, we have to remove the old one
985170808Sdelphij	 * from the target directory. */
986170808Sdelphij	if (tvp != NULL) {
987245115Sgleb		struct tmpfs_dirent *tde;
988245115Sgleb
989170808Sdelphij		/* Remove the old entry from the target directory. */
990245115Sgleb		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
991245115Sgleb		tmpfs_dir_detach(tdvp, tde);
992170808Sdelphij
993170808Sdelphij		/* Free the directory entry we just deleted.  Note that the
994170808Sdelphij		 * node referred by it will not be removed until the vnode is
995170808Sdelphij		 * really reclaimed. */
996245115Sgleb		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
997170808Sdelphij	}
998245115Sgleb
999245115Sgleb	tmpfs_dir_attach(tdvp, de);
1000245115Sgleb
1001226987Spho	cache_purge(fvp);
1002232401Sjhb	if (tvp != NULL)
1003232401Sjhb		cache_purge(tvp);
1004248422Skib	cache_purge_negative(tdvp);
1005170808Sdelphij
1006170808Sdelphij	error = 0;
1007170808Sdelphij
1008170808Sdelphijout_locked:
1009227822Sivoras	if (fdvp != tdvp && fdvp != tvp)
1010175294Sattilio		VOP_UNLOCK(fdvp, 0);
1011170808Sdelphij
1012170808Sdelphijout:
1013170808Sdelphij	/* Release target nodes. */
1014170808Sdelphij	/* XXX: I don't understand when tdvp can be the same as tvp, but
1015170808Sdelphij	 * other code takes care of this... */
1016170808Sdelphij	if (tdvp == tvp)
1017170808Sdelphij		vrele(tdvp);
1018170808Sdelphij	else
1019170808Sdelphij		vput(tdvp);
1020170808Sdelphij	if (tvp != NULL)
1021170808Sdelphij		vput(tvp);
1022170808Sdelphij
1023170808Sdelphij	/* Release source nodes. */
1024170808Sdelphij	vrele(fdvp);
1025170808Sdelphij	vrele(fvp);
1026170808Sdelphij
1027232960Sgleb	if (mp != NULL)
1028232960Sgleb		vfs_unbusy(mp);
1029232960Sgleb
1030170808Sdelphij	return error;
1031170808Sdelphij}
1032170808Sdelphij
1033171069Sdelphijstatic int
1034170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v)
1035170808Sdelphij{
1036170808Sdelphij	struct vnode *dvp = v->a_dvp;
1037170808Sdelphij	struct vnode **vpp = v->a_vpp;
1038170808Sdelphij	struct componentname *cnp = v->a_cnp;
1039170808Sdelphij	struct vattr *vap = v->a_vap;
1040170808Sdelphij
1041170808Sdelphij	MPASS(vap->va_type == VDIR);
1042170808Sdelphij
1043170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1044170808Sdelphij}
1045170808Sdelphij
1046171069Sdelphijstatic int
1047170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v)
1048170808Sdelphij{
1049170808Sdelphij	struct vnode *dvp = v->a_dvp;
1050170808Sdelphij	struct vnode *vp = v->a_vp;
1051170808Sdelphij
1052170808Sdelphij	int error;
1053170808Sdelphij	struct tmpfs_dirent *de;
1054170808Sdelphij	struct tmpfs_mount *tmp;
1055170808Sdelphij	struct tmpfs_node *dnode;
1056170808Sdelphij	struct tmpfs_node *node;
1057170808Sdelphij
1058176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
1059176559Sattilio	MPASS(VOP_ISLOCKED(vp));
1060170808Sdelphij
1061170808Sdelphij	tmp = VFS_TO_TMPFS(dvp->v_mount);
1062170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
1063170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1064170808Sdelphij
1065171070Sdelphij	/* Directories with more than two entries ('.' and '..') cannot be
1066171070Sdelphij	 * removed. */
1067171070Sdelphij	 if (node->tn_size > 0) {
1068171070Sdelphij		 error = ENOTEMPTY;
1069171070Sdelphij		 goto out;
1070171070Sdelphij	 }
1071170808Sdelphij
1072170808Sdelphij	if ((dnode->tn_flags & APPEND)
1073170808Sdelphij	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1074170808Sdelphij		error = EPERM;
1075170808Sdelphij		goto out;
1076170808Sdelphij	}
1077170808Sdelphij
1078171070Sdelphij	/* This invariant holds only if we are not trying to remove "..".
1079171070Sdelphij	  * We checked for that above so this is safe now. */
1080170808Sdelphij	MPASS(node->tn_dir.tn_parent == dnode);
1081170808Sdelphij
1082170808Sdelphij	/* Get the directory entry associated with node (vp).  This was
1083170808Sdelphij	 * filled by tmpfs_lookup while looking up the entry. */
1084188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1085170808Sdelphij	MPASS(TMPFS_DIRENT_MATCHES(de,
1086170808Sdelphij	    v->a_cnp->cn_nameptr,
1087170808Sdelphij	    v->a_cnp->cn_namelen));
1088170808Sdelphij
1089170808Sdelphij	/* Check flags to see if we are allowed to remove the directory. */
1090170808Sdelphij	if (dnode->tn_flags & APPEND
1091170808Sdelphij		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1092170808Sdelphij		error = EPERM;
1093170808Sdelphij		goto out;
1094170808Sdelphij	}
1095170808Sdelphij
1096197953Sdelphij
1097170808Sdelphij	/* Detach the directory entry from the directory (dnode). */
1098170808Sdelphij	tmpfs_dir_detach(dvp, de);
1099211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
1100211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1101170808Sdelphij
1102197953Sdelphij	/* No vnode should be allocated for this entry from this point */
1103197953Sdelphij	TMPFS_NODE_LOCK(node);
1104197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1105170808Sdelphij	node->tn_links--;
1106197953Sdelphij	node->tn_dir.tn_parent = NULL;
1107170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1108170808Sdelphij	    TMPFS_NODE_MODIFIED;
1109197953Sdelphij
1110197953Sdelphij	TMPFS_NODE_UNLOCK(node);
1111197953Sdelphij
1112197953Sdelphij	TMPFS_NODE_LOCK(dnode);
1113197953Sdelphij	TMPFS_ASSERT_ELOCKED(dnode);
1114197953Sdelphij	dnode->tn_links--;
1115197953Sdelphij	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1116170808Sdelphij	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1117197953Sdelphij	TMPFS_NODE_UNLOCK(dnode);
1118170808Sdelphij
1119171070Sdelphij	cache_purge(dvp);
1120170808Sdelphij	cache_purge(vp);
1121170808Sdelphij
1122170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
1123170808Sdelphij	 * referred by it will not be removed until the vnode is really
1124170808Sdelphij	 * reclaimed. */
1125245115Sgleb	tmpfs_free_dirent(tmp, de);
1126170808Sdelphij
1127170808Sdelphij	/* Release the deleted vnode (will destroy the node, notify
1128170808Sdelphij	 * interested parties and clean it from the cache). */
1129170808Sdelphij
1130170808Sdelphij	dnode->tn_status |= TMPFS_NODE_CHANGED;
1131170808Sdelphij	tmpfs_update(dvp);
1132170808Sdelphij
1133170808Sdelphij	error = 0;
1134170808Sdelphij
1135170808Sdelphijout:
1136170808Sdelphij	return error;
1137170808Sdelphij}
1138170808Sdelphij
1139171069Sdelphijstatic int
1140170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v)
1141170808Sdelphij{
1142170808Sdelphij	struct vnode *dvp = v->a_dvp;
1143170808Sdelphij	struct vnode **vpp = v->a_vpp;
1144170808Sdelphij	struct componentname *cnp = v->a_cnp;
1145170808Sdelphij	struct vattr *vap = v->a_vap;
1146170808Sdelphij	char *target = v->a_target;
1147170808Sdelphij
1148170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1149170808Sdelphij	MPASS(vap->va_type == VLNK);
1150170808Sdelphij#else
1151170808Sdelphij	vap->va_type = VLNK;
1152170808Sdelphij#endif
1153170808Sdelphij
1154170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1155170808Sdelphij}
1156170808Sdelphij
1157171069Sdelphijstatic int
1158170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v)
1159170808Sdelphij{
1160170808Sdelphij	struct vnode *vp = v->a_vp;
1161170808Sdelphij	struct uio *uio = v->a_uio;
1162170808Sdelphij	int *eofflag = v->a_eofflag;
1163170808Sdelphij	u_long **cookies = v->a_cookies;
1164170808Sdelphij	int *ncookies = v->a_ncookies;
1165170808Sdelphij
1166170808Sdelphij	int error;
1167245115Sgleb	ssize_t startresid;
1168263946Sbdrewery	int maxcookies;
1169170808Sdelphij	struct tmpfs_node *node;
1170170808Sdelphij
1171170808Sdelphij	/* This operation only makes sense on directory nodes. */
1172171802Sdelphij	if (vp->v_type != VDIR)
1173171802Sdelphij		return ENOTDIR;
1174170808Sdelphij
1175263946Sbdrewery	maxcookies = 0;
1176170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1177170808Sdelphij
1178245115Sgleb	startresid = uio->uio_resid;
1179170808Sdelphij
1180263946Sbdrewery	/* Allocate cookies for NFS and compat modules. */
1181245115Sgleb	if (cookies != NULL && ncookies != NULL) {
1182263946Sbdrewery		maxcookies = howmany(node->tn_size,
1183263946Sbdrewery		    sizeof(struct tmpfs_dirent)) + 2;
1184263946Sbdrewery		*cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1185263946Sbdrewery		    M_WAITOK);
1186245115Sgleb		*ncookies = 0;
1187171862Sdelphij	}
1188171862Sdelphij
1189263946Sbdrewery	if (cookies == NULL)
1190245115Sgleb		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1191245115Sgleb	else
1192263946Sbdrewery		error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1193263946Sbdrewery		    ncookies);
1194170808Sdelphij
1195263946Sbdrewery	/* Buffer was filled without hitting EOF. */
1196245115Sgleb	if (error == EJUSTRETURN)
1197245115Sgleb		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1198171862Sdelphij
1199263946Sbdrewery	if (error != 0 && cookies != NULL)
1200245115Sgleb		free(*cookies, M_TEMP);
1201171862Sdelphij
1202170808Sdelphij	if (eofflag != NULL)
1203170808Sdelphij		*eofflag =
1204170808Sdelphij		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1205170808Sdelphij
1206170808Sdelphij	return error;
1207170808Sdelphij}
1208170808Sdelphij
1209171069Sdelphijstatic int
1210170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v)
1211170808Sdelphij{
1212170808Sdelphij	struct vnode *vp = v->a_vp;
1213170808Sdelphij	struct uio *uio = v->a_uio;
1214170808Sdelphij
1215170808Sdelphij	int error;
1216170808Sdelphij	struct tmpfs_node *node;
1217170808Sdelphij
1218170808Sdelphij	MPASS(uio->uio_offset == 0);
1219170808Sdelphij	MPASS(vp->v_type == VLNK);
1220170808Sdelphij
1221170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1222170808Sdelphij
1223170808Sdelphij	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1224170808Sdelphij	    uio);
1225170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
1226170808Sdelphij
1227170808Sdelphij	return error;
1228170808Sdelphij}
1229170808Sdelphij
1230171069Sdelphijstatic int
1231170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v)
1232170808Sdelphij{
1233170808Sdelphij	struct vnode *vp = v->a_vp;
1234170808Sdelphij
1235170808Sdelphij	struct tmpfs_node *node;
1236170808Sdelphij
1237170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1238170808Sdelphij
1239170808Sdelphij	if (node->tn_links == 0)
1240234607Strasz		vrecycle(vp);
1241170808Sdelphij
1242170808Sdelphij	return 0;
1243170808Sdelphij}
1244170808Sdelphij
1245170808Sdelphijint
1246170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v)
1247170808Sdelphij{
1248170808Sdelphij	struct vnode *vp = v->a_vp;
1249170808Sdelphij
1250170808Sdelphij	struct tmpfs_mount *tmp;
1251170808Sdelphij	struct tmpfs_node *node;
1252170808Sdelphij
1253170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1254170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
1255171070Sdelphij
1256250189Skib	if (vp->v_type == VREG)
1257250189Skib		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1258250190Skib	else
1259250190Skib		vnode_destroy_vobject(vp);
1260250030Skib	vp->v_object = NULL;
1261170808Sdelphij	cache_purge(vp);
1262197953Sdelphij
1263197953Sdelphij	TMPFS_NODE_LOCK(node);
1264197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1265170808Sdelphij	tmpfs_free_vp(vp);
1266170808Sdelphij
1267170808Sdelphij	/* If the node referenced by this vnode was deleted by the user,
1268170808Sdelphij	 * we must free its associated data structures (now that the vnode
1269170808Sdelphij	 * is being reclaimed). */
1270197953Sdelphij	if (node->tn_links == 0 &&
1271197953Sdelphij	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1272197953Sdelphij		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1273197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1274170808Sdelphij		tmpfs_free_node(tmp, node);
1275197953Sdelphij	} else
1276197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1277170808Sdelphij
1278170808Sdelphij	MPASS(vp->v_data == NULL);
1279170808Sdelphij	return 0;
1280170808Sdelphij}
1281170808Sdelphij
1282171069Sdelphijstatic int
1283170808Sdelphijtmpfs_print(struct vop_print_args *v)
1284170808Sdelphij{
1285170808Sdelphij	struct vnode *vp = v->a_vp;
1286170808Sdelphij
1287170808Sdelphij	struct tmpfs_node *node;
1288170808Sdelphij
1289170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1290170808Sdelphij
1291248610Spjd	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1292170808Sdelphij	    node, node->tn_flags, node->tn_links);
1293231669Stijl	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1294170808Sdelphij	    node->tn_mode, node->tn_uid, node->tn_gid,
1295231669Stijl	    (intmax_t)node->tn_size, node->tn_status);
1296170808Sdelphij
1297170808Sdelphij	if (vp->v_type == VFIFO)
1298170808Sdelphij		fifo_printinfo(vp);
1299170808Sdelphij
1300170808Sdelphij	printf("\n");
1301170808Sdelphij
1302170808Sdelphij	return 0;
1303170808Sdelphij}
1304170808Sdelphij
1305171069Sdelphijstatic int
1306170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v)
1307170808Sdelphij{
1308170808Sdelphij	int name = v->a_name;
1309170808Sdelphij	register_t *retval = v->a_retval;
1310170808Sdelphij
1311170808Sdelphij	int error;
1312170808Sdelphij
1313170808Sdelphij	error = 0;
1314170808Sdelphij
1315170808Sdelphij	switch (name) {
1316170808Sdelphij	case _PC_LINK_MAX:
1317170808Sdelphij		*retval = LINK_MAX;
1318170808Sdelphij		break;
1319170808Sdelphij
1320170808Sdelphij	case _PC_NAME_MAX:
1321170808Sdelphij		*retval = NAME_MAX;
1322170808Sdelphij		break;
1323170808Sdelphij
1324170808Sdelphij	case _PC_PATH_MAX:
1325170808Sdelphij		*retval = PATH_MAX;
1326170808Sdelphij		break;
1327170808Sdelphij
1328170808Sdelphij	case _PC_PIPE_BUF:
1329170808Sdelphij		*retval = PIPE_BUF;
1330170808Sdelphij		break;
1331170808Sdelphij
1332170808Sdelphij	case _PC_CHOWN_RESTRICTED:
1333170808Sdelphij		*retval = 1;
1334170808Sdelphij		break;
1335170808Sdelphij
1336170808Sdelphij	case _PC_NO_TRUNC:
1337170808Sdelphij		*retval = 1;
1338170808Sdelphij		break;
1339170808Sdelphij
1340170808Sdelphij	case _PC_SYNC_IO:
1341170808Sdelphij		*retval = 1;
1342170808Sdelphij		break;
1343170808Sdelphij
1344170808Sdelphij	case _PC_FILESIZEBITS:
1345170808Sdelphij		*retval = 0; /* XXX Don't know which value should I return. */
1346170808Sdelphij		break;
1347170808Sdelphij
1348170808Sdelphij	default:
1349170808Sdelphij		error = EINVAL;
1350170808Sdelphij	}
1351170808Sdelphij
1352170808Sdelphij	return error;
1353170808Sdelphij}
1354170808Sdelphij
1355171069Sdelphijstatic int
1356170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap)
1357170808Sdelphij{
1358170808Sdelphij	struct tmpfs_fid *tfhp;
1359170808Sdelphij	struct tmpfs_node *node;
1360170808Sdelphij
1361170808Sdelphij	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1362170808Sdelphij	node = VP_TO_TMPFS_NODE(ap->a_vp);
1363170808Sdelphij
1364170808Sdelphij	tfhp->tf_len = sizeof(struct tmpfs_fid);
1365170808Sdelphij	tfhp->tf_id = node->tn_id;
1366170808Sdelphij	tfhp->tf_gen = node->tn_gen;
1367171070Sdelphij
1368170808Sdelphij	return (0);
1369170808Sdelphij}
1370171069Sdelphij
1371211598Sedstatic int
1372211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap)
1373211598Sed{
1374211598Sed	struct vnode *dvp = ap->a_dvp;
1375211598Sed	struct componentname *cnp = ap->a_cnp;
1376211598Sed	struct tmpfs_dirent *de;
1377211598Sed
1378211598Sed	switch (ap->a_flags) {
1379211598Sed	case LOOKUP:
1380211598Sed		return (0);
1381211598Sed	case CREATE:
1382211598Sed		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1383211598Sed		if (de != NULL)
1384211598Sed			return (de->td_node == NULL ? 0 : EEXIST);
1385211598Sed		return (tmpfs_dir_whiteout_add(dvp, cnp));
1386211598Sed	case DELETE:
1387211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
1388211598Sed		return (0);
1389211598Sed	default:
1390211598Sed		panic("tmpfs_whiteout: unknown op");
1391211598Sed	}
1392211598Sed}
1393211598Sed
1394171069Sdelphij/*
1395171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system.
1396171069Sdelphij */
1397171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = {
1398171069Sdelphij	.vop_default =			&default_vnodeops,
1399171069Sdelphij	.vop_lookup =			vfs_cache_lookup,
1400171069Sdelphij	.vop_cachedlookup =		tmpfs_lookup,
1401171069Sdelphij	.vop_create =			tmpfs_create,
1402171069Sdelphij	.vop_mknod =			tmpfs_mknod,
1403171069Sdelphij	.vop_open =			tmpfs_open,
1404171069Sdelphij	.vop_close =			tmpfs_close,
1405171069Sdelphij	.vop_access =			tmpfs_access,
1406171069Sdelphij	.vop_getattr =			tmpfs_getattr,
1407171069Sdelphij	.vop_setattr =			tmpfs_setattr,
1408171069Sdelphij	.vop_read =			tmpfs_read,
1409171069Sdelphij	.vop_write =			tmpfs_write,
1410171069Sdelphij	.vop_fsync =			tmpfs_fsync,
1411171069Sdelphij	.vop_remove =			tmpfs_remove,
1412171069Sdelphij	.vop_link =			tmpfs_link,
1413171069Sdelphij	.vop_rename =			tmpfs_rename,
1414171069Sdelphij	.vop_mkdir =			tmpfs_mkdir,
1415171069Sdelphij	.vop_rmdir =			tmpfs_rmdir,
1416171069Sdelphij	.vop_symlink =			tmpfs_symlink,
1417171069Sdelphij	.vop_readdir =			tmpfs_readdir,
1418171069Sdelphij	.vop_readlink =			tmpfs_readlink,
1419171069Sdelphij	.vop_inactive =			tmpfs_inactive,
1420171069Sdelphij	.vop_reclaim =			tmpfs_reclaim,
1421171069Sdelphij	.vop_print =			tmpfs_print,
1422171069Sdelphij	.vop_pathconf =			tmpfs_pathconf,
1423171069Sdelphij	.vop_vptofh =			tmpfs_vptofh,
1424211598Sed	.vop_whiteout =			tmpfs_whiteout,
1425171069Sdelphij	.vop_bmap =			VOP_EOPNOTSUPP,
1426171069Sdelphij};
1427171069Sdelphij
1428