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>
42170808Sdelphij#include <sys/namei.h>
43170808Sdelphij#include <sys/priv.h>
44170808Sdelphij#include <sys/proc.h>
45197850Sdelphij#include <sys/sched.h>
46197850Sdelphij#include <sys/sf_buf.h>
47170808Sdelphij#include <sys/stat.h>
48170808Sdelphij#include <sys/systm.h>
49233851Sgleb#include <sys/sysctl.h>
50170808Sdelphij#include <sys/unistd.h>
51170808Sdelphij#include <sys/vnode.h>
52170808Sdelphij
53170808Sdelphij#include <vm/vm.h>
54240238Skib#include <vm/vm_param.h>
55170808Sdelphij#include <vm/vm_object.h>
56170808Sdelphij#include <vm/vm_page.h>
57170808Sdelphij#include <vm/vm_pager.h>
58188929Salc
59170808Sdelphij#include <machine/_inttypes.h>
60170808Sdelphij
61170808Sdelphij#include <fs/fifofs/fifo.h>
62170808Sdelphij#include <fs/tmpfs/tmpfs_vnops.h>
63170808Sdelphij#include <fs/tmpfs/tmpfs.h>
64170808Sdelphij
65233851SglebSYSCTL_DECL(_vfs_tmpfs);
66233851Sgleb
67233851Sglebstatic volatile int tmpfs_rename_restarts;
68233851SglebSYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
69233851Sgleb    __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
70233851Sgleb    "Times rename had to restart due to lock contention");
71233851Sgleb
72170808Sdelphij/* --------------------------------------------------------------------- */
73170808Sdelphij
74171069Sdelphijstatic int
75170808Sdelphijtmpfs_lookup(struct vop_cachedlookup_args *v)
76170808Sdelphij{
77170808Sdelphij	struct vnode *dvp = v->a_dvp;
78170808Sdelphij	struct vnode **vpp = v->a_vpp;
79170808Sdelphij	struct componentname *cnp = v->a_cnp;
80170808Sdelphij
81170808Sdelphij	int error;
82170808Sdelphij	struct tmpfs_dirent *de;
83170808Sdelphij	struct tmpfs_node *dnode;
84170808Sdelphij
85170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
86170808Sdelphij	*vpp = NULLVP;
87170808Sdelphij
88170808Sdelphij	/* Check accessibility of requested node as a first step. */
89191990Sattilio	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
90170808Sdelphij	if (error != 0)
91170808Sdelphij		goto out;
92170808Sdelphij
93170808Sdelphij	/* We cannot be requesting the parent directory of the root node. */
94170808Sdelphij	MPASS(IMPLIES(dnode->tn_type == VDIR &&
95170808Sdelphij	    dnode->tn_dir.tn_parent == dnode,
96170808Sdelphij	    !(cnp->cn_flags & ISDOTDOT)));
97170808Sdelphij
98197953Sdelphij	TMPFS_ASSERT_LOCKED(dnode);
99197953Sdelphij	if (dnode->tn_dir.tn_parent == NULL) {
100197953Sdelphij		error = ENOENT;
101197953Sdelphij		goto out;
102197953Sdelphij	}
103170808Sdelphij	if (cnp->cn_flags & ISDOTDOT) {
104171799Sdelphij		int ltype = 0;
105171799Sdelphij
106176559Sattilio		ltype = VOP_ISLOCKED(dvp);
107171802Sdelphij		vhold(dvp);
108175294Sattilio		VOP_UNLOCK(dvp, 0);
109170808Sdelphij		/* Allocate a new vnode on the matching entry. */
110171799Sdelphij		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
111191990Sattilio		    cnp->cn_lkflags, vpp);
112170808Sdelphij
113175202Sattilio		vn_lock(dvp, ltype | LK_RETRY);
114171802Sdelphij		vdrop(dvp);
115170808Sdelphij	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
116170808Sdelphij		VREF(dvp);
117170808Sdelphij		*vpp = dvp;
118170808Sdelphij		error = 0;
119170808Sdelphij	} else {
120188318Skib		de = tmpfs_dir_lookup(dnode, NULL, cnp);
121211598Sed		if (de != NULL && de->td_node == NULL)
122211598Sed			cnp->cn_flags |= ISWHITEOUT;
123211598Sed		if (de == NULL || de->td_node == NULL) {
124170808Sdelphij			/* The entry was not found in the directory.
125170808Sdelphij			 * This is OK if we are creating or renaming an
126170808Sdelphij			 * entry and are working on the last component of
127170808Sdelphij			 * the path name. */
128170808Sdelphij			if ((cnp->cn_flags & ISLASTCN) &&
129170808Sdelphij			    (cnp->cn_nameiop == CREATE || \
130211598Sed			    cnp->cn_nameiop == RENAME ||
131211598Sed			    (cnp->cn_nameiop == DELETE &&
132211598Sed			    cnp->cn_flags & DOWHITEOUT &&
133211598Sed			    cnp->cn_flags & ISWHITEOUT))) {
134170808Sdelphij				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
135170808Sdelphij				    cnp->cn_thread);
136170808Sdelphij				if (error != 0)
137170808Sdelphij					goto out;
138170808Sdelphij
139170808Sdelphij				/* Keep the component name in the buffer for
140170808Sdelphij				 * future uses. */
141170808Sdelphij				cnp->cn_flags |= SAVENAME;
142170808Sdelphij
143170808Sdelphij				error = EJUSTRETURN;
144170808Sdelphij			} else
145170808Sdelphij				error = ENOENT;
146170808Sdelphij		} else {
147170808Sdelphij			struct tmpfs_node *tnode;
148170808Sdelphij
149170808Sdelphij			/* The entry was found, so get its associated
150170808Sdelphij			 * tmpfs_node. */
151170808Sdelphij			tnode = de->td_node;
152170808Sdelphij
153170808Sdelphij			/* If we are not at the last path component and
154170808Sdelphij			 * found a non-directory or non-link entry (which
155170808Sdelphij			 * may itself be pointing to a directory), raise
156170808Sdelphij			 * an error. */
157170808Sdelphij			if ((tnode->tn_type != VDIR &&
158170808Sdelphij			    tnode->tn_type != VLNK) &&
159170808Sdelphij			    !(cnp->cn_flags & ISLASTCN)) {
160170808Sdelphij				error = ENOTDIR;
161170808Sdelphij				goto out;
162170808Sdelphij			}
163170808Sdelphij
164170808Sdelphij			/* If we are deleting or renaming the entry, keep
165170808Sdelphij			 * track of its tmpfs_dirent so that it can be
166170808Sdelphij			 * easily deleted later. */
167170808Sdelphij			if ((cnp->cn_flags & ISLASTCN) &&
168170808Sdelphij			    (cnp->cn_nameiop == DELETE ||
169170808Sdelphij			    cnp->cn_nameiop == RENAME)) {
170170808Sdelphij				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
171170808Sdelphij				    cnp->cn_thread);
172170808Sdelphij				if (error != 0)
173170808Sdelphij					goto out;
174171070Sdelphij
175170808Sdelphij				/* Allocate a new vnode on the matching entry. */
176171799Sdelphij				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
177191990Sattilio						cnp->cn_lkflags, vpp);
178170808Sdelphij				if (error != 0)
179170808Sdelphij					goto out;
180170808Sdelphij
181170808Sdelphij				if ((dnode->tn_mode & S_ISTXT) &&
182170808Sdelphij				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
183170808Sdelphij				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
184170808Sdelphij					error = EPERM;
185170808Sdelphij					vput(*vpp);
186170808Sdelphij					*vpp = NULL;
187170808Sdelphij					goto out;
188171070Sdelphij				}
189170808Sdelphij				cnp->cn_flags |= SAVENAME;
190171799Sdelphij			} else {
191171799Sdelphij				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
192191990Sattilio						cnp->cn_lkflags, vpp);
193170808Sdelphij			}
194170808Sdelphij		}
195170808Sdelphij	}
196170808Sdelphij
197170808Sdelphij	/* Store the result of this lookup in the cache.  Avoid this if the
198170808Sdelphij	 * request was for creation, as it does not improve timings on
199170808Sdelphij	 * emprical tests. */
200170808Sdelphij	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
201170808Sdelphij		cache_enter(dvp, *vpp, cnp);
202170808Sdelphij
203170808Sdelphijout:
204170808Sdelphij	/* If there were no errors, *vpp cannot be null and it must be
205170808Sdelphij	 * locked. */
206176559Sattilio	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
207170808Sdelphij
208170808Sdelphij	return error;
209170808Sdelphij}
210170808Sdelphij
211170808Sdelphij/* --------------------------------------------------------------------- */
212170808Sdelphij
213171069Sdelphijstatic int
214170808Sdelphijtmpfs_create(struct vop_create_args *v)
215170808Sdelphij{
216170808Sdelphij	struct vnode *dvp = v->a_dvp;
217170808Sdelphij	struct vnode **vpp = v->a_vpp;
218170808Sdelphij	struct componentname *cnp = v->a_cnp;
219170808Sdelphij	struct vattr *vap = v->a_vap;
220170808Sdelphij
221170808Sdelphij	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
222170808Sdelphij
223170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
224170808Sdelphij}
225170808Sdelphij/* --------------------------------------------------------------------- */
226170808Sdelphij
227171069Sdelphijstatic int
228170808Sdelphijtmpfs_mknod(struct vop_mknod_args *v)
229170808Sdelphij{
230170808Sdelphij	struct vnode *dvp = v->a_dvp;
231170808Sdelphij	struct vnode **vpp = v->a_vpp;
232170808Sdelphij	struct componentname *cnp = v->a_cnp;
233170808Sdelphij	struct vattr *vap = v->a_vap;
234170808Sdelphij
235170808Sdelphij	if (vap->va_type != VBLK && vap->va_type != VCHR &&
236170808Sdelphij	    vap->va_type != VFIFO)
237170808Sdelphij		return EINVAL;
238170808Sdelphij
239170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
240170808Sdelphij}
241170808Sdelphij
242170808Sdelphij/* --------------------------------------------------------------------- */
243170808Sdelphij
244171069Sdelphijstatic int
245170808Sdelphijtmpfs_open(struct vop_open_args *v)
246170808Sdelphij{
247170808Sdelphij	struct vnode *vp = v->a_vp;
248170808Sdelphij	int mode = v->a_mode;
249170808Sdelphij
250170808Sdelphij	int error;
251170808Sdelphij	struct tmpfs_node *node;
252170808Sdelphij
253176559Sattilio	MPASS(VOP_ISLOCKED(vp));
254170808Sdelphij
255170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
256171070Sdelphij
257170808Sdelphij	/* The file is still active but all its names have been removed
258170808Sdelphij	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
259170808Sdelphij	 * it is about to die. */
260170808Sdelphij	if (node->tn_links < 1)
261170808Sdelphij		return (ENOENT);
262170808Sdelphij
263170808Sdelphij	/* If the file is marked append-only, deny write requests. */
264170808Sdelphij	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
265170808Sdelphij		error = EPERM;
266170808Sdelphij	else {
267170808Sdelphij		error = 0;
268171070Sdelphij		vnode_create_vobject(vp, node->tn_size, v->a_td);
269170808Sdelphij	}
270170808Sdelphij
271176559Sattilio	MPASS(VOP_ISLOCKED(vp));
272170808Sdelphij	return error;
273170808Sdelphij}
274170808Sdelphij
275170808Sdelphij/* --------------------------------------------------------------------- */
276170808Sdelphij
277171069Sdelphijstatic int
278170808Sdelphijtmpfs_close(struct vop_close_args *v)
279170808Sdelphij{
280170808Sdelphij	struct vnode *vp = v->a_vp;
281170808Sdelphij
282176559Sattilio	MPASS(VOP_ISLOCKED(vp));
283170808Sdelphij
284218949Salc	/* Update node times. */
285218949Salc	tmpfs_update(vp);
286170808Sdelphij
287218949Salc	return (0);
288170808Sdelphij}
289170808Sdelphij
290170808Sdelphij/* --------------------------------------------------------------------- */
291170808Sdelphij
292170808Sdelphijint
293170808Sdelphijtmpfs_access(struct vop_access_args *v)
294170808Sdelphij{
295170808Sdelphij	struct vnode *vp = v->a_vp;
296184413Strasz	accmode_t accmode = v->a_accmode;
297170808Sdelphij	struct ucred *cred = v->a_cred;
298170808Sdelphij
299170808Sdelphij	int error;
300170808Sdelphij	struct tmpfs_node *node;
301170808Sdelphij
302176559Sattilio	MPASS(VOP_ISLOCKED(vp));
303170808Sdelphij
304170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
305170808Sdelphij
306170808Sdelphij	switch (vp->v_type) {
307170808Sdelphij	case VDIR:
308170808Sdelphij		/* FALLTHROUGH */
309170808Sdelphij	case VLNK:
310170808Sdelphij		/* FALLTHROUGH */
311170808Sdelphij	case VREG:
312184413Strasz		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
313170808Sdelphij			error = EROFS;
314170808Sdelphij			goto out;
315170808Sdelphij		}
316170808Sdelphij		break;
317170808Sdelphij
318170808Sdelphij	case VBLK:
319170808Sdelphij		/* FALLTHROUGH */
320170808Sdelphij	case VCHR:
321170808Sdelphij		/* FALLTHROUGH */
322170808Sdelphij	case VSOCK:
323170808Sdelphij		/* FALLTHROUGH */
324170808Sdelphij	case VFIFO:
325170808Sdelphij		break;
326170808Sdelphij
327170808Sdelphij	default:
328170808Sdelphij		error = EINVAL;
329170808Sdelphij		goto out;
330170808Sdelphij	}
331170808Sdelphij
332184413Strasz	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
333170808Sdelphij		error = EPERM;
334170808Sdelphij		goto out;
335170808Sdelphij	}
336170808Sdelphij
337170808Sdelphij	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
338184413Strasz	    node->tn_gid, accmode, cred, NULL);
339170808Sdelphij
340170808Sdelphijout:
341176559Sattilio	MPASS(VOP_ISLOCKED(vp));
342170808Sdelphij
343170808Sdelphij	return error;
344170808Sdelphij}
345170808Sdelphij
346170808Sdelphij/* --------------------------------------------------------------------- */
347170808Sdelphij
348170808Sdelphijint
349170808Sdelphijtmpfs_getattr(struct vop_getattr_args *v)
350170808Sdelphij{
351170808Sdelphij	struct vnode *vp = v->a_vp;
352170808Sdelphij	struct vattr *vap = v->a_vap;
353170808Sdelphij
354170808Sdelphij	struct tmpfs_node *node;
355170808Sdelphij
356170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
357170808Sdelphij
358170808Sdelphij	tmpfs_update(vp);
359170808Sdelphij
360170808Sdelphij	vap->va_type = vp->v_type;
361170808Sdelphij	vap->va_mode = node->tn_mode;
362170808Sdelphij	vap->va_nlink = node->tn_links;
363170808Sdelphij	vap->va_uid = node->tn_uid;
364170808Sdelphij	vap->va_gid = node->tn_gid;
365170808Sdelphij	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
366170808Sdelphij	vap->va_fileid = node->tn_id;
367170808Sdelphij	vap->va_size = node->tn_size;
368170808Sdelphij	vap->va_blocksize = PAGE_SIZE;
369170808Sdelphij	vap->va_atime = node->tn_atime;
370170808Sdelphij	vap->va_mtime = node->tn_mtime;
371170808Sdelphij	vap->va_ctime = node->tn_ctime;
372170808Sdelphij	vap->va_birthtime = node->tn_birthtime;
373170808Sdelphij	vap->va_gen = node->tn_gen;
374170808Sdelphij	vap->va_flags = node->tn_flags;
375170808Sdelphij	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
376183214Skib		node->tn_rdev : NODEV;
377170808Sdelphij	vap->va_bytes = round_page(node->tn_size);
378183212Skib	vap->va_filerev = 0;
379170808Sdelphij
380170808Sdelphij	return 0;
381170808Sdelphij}
382170808Sdelphij
383170808Sdelphij/* --------------------------------------------------------------------- */
384170808Sdelphij
385170808Sdelphij/* XXX Should this operation be atomic?  I think it should, but code in
386170808Sdelphij * XXX other places (e.g., ufs) doesn't seem to be... */
387170808Sdelphijint
388170808Sdelphijtmpfs_setattr(struct vop_setattr_args *v)
389170808Sdelphij{
390170808Sdelphij	struct vnode *vp = v->a_vp;
391170808Sdelphij	struct vattr *vap = v->a_vap;
392170808Sdelphij	struct ucred *cred = v->a_cred;
393182371Sattilio	struct thread *td = curthread;
394170808Sdelphij
395170808Sdelphij	int error;
396170808Sdelphij
397176559Sattilio	MPASS(VOP_ISLOCKED(vp));
398170808Sdelphij
399170808Sdelphij	error = 0;
400170808Sdelphij
401170808Sdelphij	/* Abort if any unsettable attribute is given. */
402170808Sdelphij	if (vap->va_type != VNON ||
403170808Sdelphij	    vap->va_nlink != VNOVAL ||
404170808Sdelphij	    vap->va_fsid != VNOVAL ||
405170808Sdelphij	    vap->va_fileid != VNOVAL ||
406170808Sdelphij	    vap->va_blocksize != VNOVAL ||
407170808Sdelphij	    vap->va_gen != VNOVAL ||
408170808Sdelphij	    vap->va_rdev != VNOVAL ||
409170808Sdelphij	    vap->va_bytes != VNOVAL)
410170808Sdelphij		error = EINVAL;
411170808Sdelphij
412170808Sdelphij	if (error == 0 && (vap->va_flags != VNOVAL))
413182371Sattilio		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
414170808Sdelphij
415170808Sdelphij	if (error == 0 && (vap->va_size != VNOVAL))
416182371Sattilio		error = tmpfs_chsize(vp, vap->va_size, cred, td);
417170808Sdelphij
418170808Sdelphij	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
419182371Sattilio		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
420170808Sdelphij
421170808Sdelphij	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
422182371Sattilio		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
423170808Sdelphij
424170808Sdelphij	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
425170808Sdelphij	    vap->va_atime.tv_nsec != VNOVAL) ||
426170808Sdelphij	    (vap->va_mtime.tv_sec != VNOVAL &&
427170808Sdelphij	    vap->va_mtime.tv_nsec != VNOVAL) ||
428170808Sdelphij	    (vap->va_birthtime.tv_sec != VNOVAL &&
429170808Sdelphij	    vap->va_birthtime.tv_nsec != VNOVAL)))
430171070Sdelphij		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
431182371Sattilio			&vap->va_birthtime, vap->va_vaflags, cred, td);
432170808Sdelphij
433170808Sdelphij	/* Update the node times.  We give preference to the error codes
434170808Sdelphij	 * generated by this function rather than the ones that may arise
435170808Sdelphij	 * from tmpfs_update. */
436170808Sdelphij	tmpfs_update(vp);
437170808Sdelphij
438176559Sattilio	MPASS(VOP_ISLOCKED(vp));
439170808Sdelphij
440170808Sdelphij	return error;
441170808Sdelphij}
442170808Sdelphij
443170808Sdelphij/* --------------------------------------------------------------------- */
444197850Sdelphijstatic int
445197850Sdelphijtmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
446197850Sdelphij    vm_offset_t offset, size_t tlen, struct uio *uio)
447197850Sdelphij{
448197850Sdelphij	vm_page_t	m;
449236209Salc	int		error, rv;
450171489Sdelphij
451197850Sdelphij	VM_OBJECT_LOCK(tobj);
452197850Sdelphij	m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
453231775Salc	    VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
454197850Sdelphij	if (m->valid != VM_PAGE_BITS_ALL) {
455197850Sdelphij		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
456236209Salc			rv = vm_pager_get_pages(tobj, &m, 1, 0);
457236209Salc			if (rv != VM_PAGER_OK) {
458236209Salc				vm_page_lock(m);
459236209Salc				vm_page_free(m);
460236209Salc				vm_page_unlock(m);
461236209Salc				VM_OBJECT_UNLOCK(tobj);
462236209Salc				return (EIO);
463197850Sdelphij			}
464197850Sdelphij		} else
465197850Sdelphij			vm_page_zero_invalid(m, TRUE);
466197850Sdelphij	}
467197850Sdelphij	VM_OBJECT_UNLOCK(tobj);
468197850Sdelphij	error = uiomove_fromphys(&m, offset, tlen, uio);
469197850Sdelphij	VM_OBJECT_LOCK(tobj);
470207573Salc	vm_page_lock(m);
471197850Sdelphij	vm_page_unwire(m, TRUE);
472207573Salc	vm_page_unlock(m);
473197850Sdelphij	vm_page_wakeup(m);
474197850Sdelphij	VM_OBJECT_UNLOCK(tobj);
475197850Sdelphij
476197850Sdelphij	return (error);
477197850Sdelphij}
478197850Sdelphij
479197850Sdelphijstatic __inline int
480197850Sdelphijtmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
481197850Sdelphij    vm_offset_t offset, size_t tlen, void *buf)
482197850Sdelphij{
483197850Sdelphij	struct uio uio;
484197850Sdelphij	struct iovec iov;
485197850Sdelphij
486197850Sdelphij	uio.uio_iovcnt = 1;
487197850Sdelphij	uio.uio_iov = &iov;
488197850Sdelphij	iov.iov_base = buf;
489197850Sdelphij	iov.iov_len = tlen;
490197850Sdelphij
491197850Sdelphij	uio.uio_offset = 0;
492197850Sdelphij	uio.uio_resid = tlen;
493197850Sdelphij	uio.uio_rw = UIO_READ;
494197850Sdelphij	uio.uio_segflg = UIO_SYSSPACE;
495197850Sdelphij	uio.uio_td = curthread;
496197850Sdelphij
497197850Sdelphij	return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
498197850Sdelphij}
499197850Sdelphij
500170808Sdelphijstatic int
501171489Sdelphijtmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
502170808Sdelphij{
503197850Sdelphij	struct sf_buf	*sf;
504171489Sdelphij	vm_pindex_t	idx;
505171489Sdelphij	vm_page_t	m;
506188929Salc	vm_offset_t	offset;
507188929Salc	off_t		addr;
508171489Sdelphij	size_t		tlen;
509197850Sdelphij	char		*ma;
510171489Sdelphij	int		error;
511170808Sdelphij
512171489Sdelphij	addr = uio->uio_offset;
513171489Sdelphij	idx = OFF_TO_IDX(addr);
514171489Sdelphij	offset = addr & PAGE_MASK;
515171489Sdelphij	tlen = MIN(PAGE_SIZE - offset, len);
516170808Sdelphij
517197740Sdelphij	if ((vobj == NULL) ||
518197740Sdelphij	    (vobj->resident_page_count == 0 && vobj->cache == NULL))
519171489Sdelphij		goto nocache;
520170808Sdelphij
521171489Sdelphij	VM_OBJECT_LOCK(vobj);
522171489Sdelphijlookupvpg:
523171489Sdelphij	if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
524171489Sdelphij	    vm_page_is_valid(m, offset, tlen)) {
525207530Salc		if ((m->oflags & VPO_BUSY) != 0) {
526207530Salc			/*
527207530Salc			 * Reference the page before unlocking and sleeping so
528207530Salc			 * that the page daemon is less likely to reclaim it.
529207530Salc			 */
530225418Skib			vm_page_reference(m);
531207530Salc			vm_page_sleep(m, "tmfsmr");
532171489Sdelphij			goto lookupvpg;
533207530Salc		}
534171489Sdelphij		vm_page_busy(m);
535171489Sdelphij		VM_OBJECT_UNLOCK(vobj);
536188929Salc		error = uiomove_fromphys(&m, offset, tlen, uio);
537171489Sdelphij		VM_OBJECT_LOCK(vobj);
538171489Sdelphij		vm_page_wakeup(m);
539171489Sdelphij		VM_OBJECT_UNLOCK(vobj);
540171489Sdelphij		return	(error);
541197850Sdelphij	} else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
542213735Savg		KASSERT(offset == 0,
543213735Savg		    ("unexpected offset in tmpfs_mappedread for sendfile"));
544207530Salc		if ((m->oflags & VPO_BUSY) != 0) {
545207530Salc			/*
546207530Salc			 * Reference the page before unlocking and sleeping so
547207530Salc			 * that the page daemon is less likely to reclaim it.
548207530Salc			 */
549225418Skib			vm_page_reference(m);
550207530Salc			vm_page_sleep(m, "tmfsmr");
551197850Sdelphij			goto lookupvpg;
552207530Salc		}
553197850Sdelphij		vm_page_busy(m);
554197850Sdelphij		VM_OBJECT_UNLOCK(vobj);
555197850Sdelphij		sched_pin();
556197850Sdelphij		sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
557197850Sdelphij		ma = (char *)sf_buf_kva(sf);
558213735Savg		error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
559197850Sdelphij		if (error == 0) {
560213735Savg			if (tlen != PAGE_SIZE)
561213735Savg				bzero(ma + tlen, PAGE_SIZE - tlen);
562197850Sdelphij			uio->uio_offset += tlen;
563197850Sdelphij			uio->uio_resid -= tlen;
564197850Sdelphij		}
565197850Sdelphij		sf_buf_free(sf);
566197850Sdelphij		sched_unpin();
567197850Sdelphij		VM_OBJECT_LOCK(vobj);
568212650Savg		if (error == 0)
569213735Savg			m->valid = VM_PAGE_BITS_ALL;
570197850Sdelphij		vm_page_wakeup(m);
571197850Sdelphij		VM_OBJECT_UNLOCK(vobj);
572197850Sdelphij		return	(error);
573171799Sdelphij	}
574171489Sdelphij	VM_OBJECT_UNLOCK(vobj);
575171489Sdelphijnocache:
576197850Sdelphij	error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
577171489Sdelphij
578171489Sdelphij	return	(error);
579170808Sdelphij}
580170808Sdelphij
581171069Sdelphijstatic int
582170808Sdelphijtmpfs_read(struct vop_read_args *v)
583170808Sdelphij{
584170808Sdelphij	struct vnode *vp = v->a_vp;
585170808Sdelphij	struct uio *uio = v->a_uio;
586170808Sdelphij
587170808Sdelphij	struct tmpfs_node *node;
588170808Sdelphij	vm_object_t uobj;
589171489Sdelphij	size_t len;
590171489Sdelphij	int resid;
591170808Sdelphij
592174265Swkoszek	int error = 0;
593170808Sdelphij
594170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
595170808Sdelphij
596170808Sdelphij	if (vp->v_type != VREG) {
597170808Sdelphij		error = EISDIR;
598170808Sdelphij		goto out;
599170808Sdelphij	}
600170808Sdelphij
601170808Sdelphij	if (uio->uio_offset < 0) {
602170808Sdelphij		error = EINVAL;
603170808Sdelphij		goto out;
604170808Sdelphij	}
605170808Sdelphij
606170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
607170808Sdelphij
608170808Sdelphij	uobj = node->tn_reg.tn_aobj;
609171489Sdelphij	while ((resid = uio->uio_resid) > 0) {
610171489Sdelphij		error = 0;
611171489Sdelphij		if (node->tn_size <= uio->uio_offset)
612171489Sdelphij			break;
613171489Sdelphij		len = MIN(node->tn_size - uio->uio_offset, resid);
614171489Sdelphij		if (len == 0)
615171489Sdelphij			break;
616171489Sdelphij		error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
617171489Sdelphij		if ((error != 0) || (resid == uio->uio_resid))
618171489Sdelphij			break;
619171489Sdelphij	}
620170808Sdelphij
621170808Sdelphijout:
622170808Sdelphij
623170808Sdelphij	return error;
624170808Sdelphij}
625170808Sdelphij
626170808Sdelphij/* --------------------------------------------------------------------- */
627170808Sdelphij
628171069Sdelphijstatic int
629171489Sdelphijtmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
630171489Sdelphij{
631171489Sdelphij	vm_pindex_t	idx;
632171489Sdelphij	vm_page_t	vpg, tpg;
633188929Salc	vm_offset_t	offset;
634188929Salc	off_t		addr;
635171489Sdelphij	size_t		tlen;
636236209Salc	int		error, rv;
637171489Sdelphij
638174265Swkoszek	error = 0;
639174265Swkoszek
640171489Sdelphij	addr = uio->uio_offset;
641171489Sdelphij	idx = OFF_TO_IDX(addr);
642171489Sdelphij	offset = addr & PAGE_MASK;
643171489Sdelphij	tlen = MIN(PAGE_SIZE - offset, len);
644171489Sdelphij
645197740Sdelphij	if ((vobj == NULL) ||
646197740Sdelphij	    (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
647171489Sdelphij		vpg = NULL;
648171489Sdelphij		goto nocache;
649171489Sdelphij	}
650171489Sdelphij
651171489Sdelphij	VM_OBJECT_LOCK(vobj);
652171489Sdelphijlookupvpg:
653171489Sdelphij	if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
654171489Sdelphij	    vm_page_is_valid(vpg, offset, tlen)) {
655207530Salc		if ((vpg->oflags & VPO_BUSY) != 0) {
656207530Salc			/*
657207530Salc			 * Reference the page before unlocking and sleeping so
658207530Salc			 * that the page daemon is less likely to reclaim it.
659207530Salc			 */
660225418Skib			vm_page_reference(vpg);
661207530Salc			vm_page_sleep(vpg, "tmfsmw");
662171489Sdelphij			goto lookupvpg;
663207530Salc		}
664171489Sdelphij		vm_page_busy(vpg);
665171489Sdelphij		vm_page_undirty(vpg);
666171489Sdelphij		VM_OBJECT_UNLOCK(vobj);
667188929Salc		error = uiomove_fromphys(&vpg, offset, tlen, uio);
668171489Sdelphij	} else {
669197740Sdelphij		if (__predict_false(vobj->cache != NULL))
670197740Sdelphij			vm_page_cache_free(vobj, idx, idx + 1);
671171489Sdelphij		VM_OBJECT_UNLOCK(vobj);
672171489Sdelphij		vpg = NULL;
673171489Sdelphij	}
674171489Sdelphijnocache:
675171489Sdelphij	VM_OBJECT_LOCK(tobj);
676171489Sdelphij	tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
677231775Salc	    VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
678171489Sdelphij	if (tpg->valid != VM_PAGE_BITS_ALL) {
679194124Salc		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
680236209Salc			rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
681236209Salc			if (rv != VM_PAGER_OK) {
682236209Salc				vm_page_lock(tpg);
683236209Salc				vm_page_free(tpg);
684236209Salc				vm_page_unlock(tpg);
685236209Salc				error = EIO;
686171489Sdelphij				goto out;
687171489Sdelphij			}
688171489Sdelphij		} else
689171489Sdelphij			vm_page_zero_invalid(tpg, TRUE);
690171489Sdelphij	}
691171489Sdelphij	VM_OBJECT_UNLOCK(tobj);
692188929Salc	if (vpg == NULL)
693188929Salc		error = uiomove_fromphys(&tpg, offset, tlen, uio);
694188929Salc	else {
695171489Sdelphij		KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
696171489Sdelphij		pmap_copy_page(vpg, tpg);
697171489Sdelphij	}
698171489Sdelphij	VM_OBJECT_LOCK(tobj);
699171489Sdelphij	if (error == 0) {
700192917Salc		KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
701192917Salc		    ("parts of tpg invalid"));
702171489Sdelphij		vm_page_dirty(tpg);
703171489Sdelphij	}
704209226Salc	vm_page_lock(tpg);
705188921Salc	vm_page_unwire(tpg, TRUE);
706207573Salc	vm_page_unlock(tpg);
707171489Sdelphij	vm_page_wakeup(tpg);
708236209Salcout:
709236209Salc	VM_OBJECT_UNLOCK(tobj);
710236209Salc	if (vpg != NULL) {
711236209Salc		VM_OBJECT_LOCK(vobj);
712171489Sdelphij		vm_page_wakeup(vpg);
713171489Sdelphij		VM_OBJECT_UNLOCK(vobj);
714236209Salc	}
715171489Sdelphij
716171489Sdelphij	return	(error);
717171489Sdelphij}
718171489Sdelphij
719171489Sdelphijstatic int
720170808Sdelphijtmpfs_write(struct vop_write_args *v)
721170808Sdelphij{
722170808Sdelphij	struct vnode *vp = v->a_vp;
723170808Sdelphij	struct uio *uio = v->a_uio;
724170808Sdelphij	int ioflag = v->a_ioflag;
725170808Sdelphij
726170808Sdelphij	boolean_t extended;
727171489Sdelphij	int error = 0;
728170808Sdelphij	off_t oldsize;
729170808Sdelphij	struct tmpfs_node *node;
730170808Sdelphij	vm_object_t uobj;
731171489Sdelphij	size_t len;
732171489Sdelphij	int resid;
733170808Sdelphij
734170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
735170808Sdelphij	oldsize = node->tn_size;
736170808Sdelphij
737170808Sdelphij	if (uio->uio_offset < 0 || vp->v_type != VREG) {
738170808Sdelphij		error = EINVAL;
739170808Sdelphij		goto out;
740170808Sdelphij	}
741170808Sdelphij
742170808Sdelphij	if (uio->uio_resid == 0) {
743170808Sdelphij		error = 0;
744170808Sdelphij		goto out;
745170808Sdelphij	}
746170808Sdelphij
747170808Sdelphij	if (ioflag & IO_APPEND)
748170808Sdelphij		uio->uio_offset = node->tn_size;
749171070Sdelphij
750171070Sdelphij	if (uio->uio_offset + uio->uio_resid >
751170808Sdelphij	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
752170808Sdelphij		return (EFBIG);
753170808Sdelphij
754207719Strasz	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
755207662Strasz		return (EFBIG);
756170808Sdelphij
757170808Sdelphij	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
758170808Sdelphij	if (extended) {
759236208Salc		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
760236208Salc		    FALSE);
761170808Sdelphij		if (error != 0)
762170808Sdelphij			goto out;
763170808Sdelphij	}
764170808Sdelphij
765170808Sdelphij	uobj = node->tn_reg.tn_aobj;
766171489Sdelphij	while ((resid = uio->uio_resid) > 0) {
767171489Sdelphij		if (node->tn_size <= uio->uio_offset)
768171489Sdelphij			break;
769171489Sdelphij		len = MIN(node->tn_size - uio->uio_offset, resid);
770171489Sdelphij		if (len == 0)
771171489Sdelphij			break;
772171489Sdelphij		error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
773171489Sdelphij		if ((error != 0) || (resid == uio->uio_resid))
774171489Sdelphij			break;
775171489Sdelphij	}
776170808Sdelphij
777170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
778170808Sdelphij	    (extended ? TMPFS_NODE_CHANGED : 0);
779170808Sdelphij
780170808Sdelphij	if (node->tn_mode & (S_ISUID | S_ISGID)) {
781170808Sdelphij		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
782170808Sdelphij			node->tn_mode &= ~(S_ISUID | S_ISGID);
783170808Sdelphij	}
784170808Sdelphij
785170808Sdelphij	if (error != 0)
786236208Salc		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
787170808Sdelphij
788170808Sdelphijout:
789170808Sdelphij	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
790170808Sdelphij	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
791170808Sdelphij
792170808Sdelphij	return error;
793170808Sdelphij}
794170808Sdelphij
795170808Sdelphij/* --------------------------------------------------------------------- */
796170808Sdelphij
797171069Sdelphijstatic int
798170808Sdelphijtmpfs_fsync(struct vop_fsync_args *v)
799170808Sdelphij{
800170808Sdelphij	struct vnode *vp = v->a_vp;
801170808Sdelphij
802176559Sattilio	MPASS(VOP_ISLOCKED(vp));
803170808Sdelphij
804170808Sdelphij	tmpfs_update(vp);
805170808Sdelphij
806170808Sdelphij	return 0;
807170808Sdelphij}
808170808Sdelphij
809170808Sdelphij/* --------------------------------------------------------------------- */
810170808Sdelphij
811171069Sdelphijstatic int
812170808Sdelphijtmpfs_remove(struct vop_remove_args *v)
813170808Sdelphij{
814170808Sdelphij	struct vnode *dvp = v->a_dvp;
815170808Sdelphij	struct vnode *vp = v->a_vp;
816170808Sdelphij
817170808Sdelphij	int error;
818170808Sdelphij	struct tmpfs_dirent *de;
819170808Sdelphij	struct tmpfs_mount *tmp;
820170808Sdelphij	struct tmpfs_node *dnode;
821170808Sdelphij	struct tmpfs_node *node;
822170808Sdelphij
823176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
824176559Sattilio	MPASS(VOP_ISLOCKED(vp));
825170808Sdelphij
826170808Sdelphij	if (vp->v_type == VDIR) {
827170808Sdelphij		error = EISDIR;
828170808Sdelphij		goto out;
829170808Sdelphij	}
830170808Sdelphij
831170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
832170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
833170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
834188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
835170808Sdelphij	MPASS(de != NULL);
836170808Sdelphij
837170808Sdelphij	/* Files marked as immutable or append-only cannot be deleted. */
838170808Sdelphij	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
839170808Sdelphij	    (dnode->tn_flags & APPEND)) {
840170808Sdelphij		error = EPERM;
841170808Sdelphij		goto out;
842170808Sdelphij	}
843170808Sdelphij
844170808Sdelphij	/* Remove the entry from the directory; as it is a file, we do not
845170808Sdelphij	 * have to change the number of hard links of the directory. */
846170808Sdelphij	tmpfs_dir_detach(dvp, de);
847211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
848211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
849170808Sdelphij
850170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
851170808Sdelphij	 * referred by it will not be removed until the vnode is really
852170808Sdelphij	 * reclaimed. */
853170808Sdelphij	tmpfs_free_dirent(tmp, de, TRUE);
854170808Sdelphij
855218949Salc	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
856170808Sdelphij	error = 0;
857170808Sdelphij
858170808Sdelphijout:
859170808Sdelphij
860170808Sdelphij	return error;
861170808Sdelphij}
862170808Sdelphij
863170808Sdelphij/* --------------------------------------------------------------------- */
864170808Sdelphij
865171069Sdelphijstatic int
866170808Sdelphijtmpfs_link(struct vop_link_args *v)
867170808Sdelphij{
868170808Sdelphij	struct vnode *dvp = v->a_tdvp;
869170808Sdelphij	struct vnode *vp = v->a_vp;
870170808Sdelphij	struct componentname *cnp = v->a_cnp;
871170808Sdelphij
872170808Sdelphij	int error;
873170808Sdelphij	struct tmpfs_dirent *de;
874170808Sdelphij	struct tmpfs_node *node;
875170808Sdelphij
876176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
877170808Sdelphij	MPASS(cnp->cn_flags & HASBUF);
878170808Sdelphij	MPASS(dvp != vp); /* XXX When can this be false? */
879170808Sdelphij
880170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
881170808Sdelphij
882170808Sdelphij	/* XXX: Why aren't the following two tests done by the caller? */
883170808Sdelphij
884170808Sdelphij	/* Hard links of directories are forbidden. */
885170808Sdelphij	if (vp->v_type == VDIR) {
886170808Sdelphij		error = EPERM;
887170808Sdelphij		goto out;
888170808Sdelphij	}
889170808Sdelphij
890170808Sdelphij	/* Cannot create cross-device links. */
891170808Sdelphij	if (dvp->v_mount != vp->v_mount) {
892170808Sdelphij		error = EXDEV;
893170808Sdelphij		goto out;
894170808Sdelphij	}
895170808Sdelphij
896170808Sdelphij	/* Ensure that we do not overflow the maximum number of links imposed
897170808Sdelphij	 * by the system. */
898170808Sdelphij	MPASS(node->tn_links <= LINK_MAX);
899170808Sdelphij	if (node->tn_links == LINK_MAX) {
900170808Sdelphij		error = EMLINK;
901170808Sdelphij		goto out;
902170808Sdelphij	}
903170808Sdelphij
904170808Sdelphij	/* We cannot create links of files marked immutable or append-only. */
905170808Sdelphij	if (node->tn_flags & (IMMUTABLE | APPEND)) {
906170808Sdelphij		error = EPERM;
907170808Sdelphij		goto out;
908170808Sdelphij	}
909170808Sdelphij
910170808Sdelphij	/* Allocate a new directory entry to represent the node. */
911170808Sdelphij	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
912170808Sdelphij	    cnp->cn_nameptr, cnp->cn_namelen, &de);
913170808Sdelphij	if (error != 0)
914170808Sdelphij		goto out;
915170808Sdelphij
916170808Sdelphij	/* Insert the new directory entry into the appropriate directory. */
917211598Sed	if (cnp->cn_flags & ISWHITEOUT)
918211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
919170808Sdelphij	tmpfs_dir_attach(dvp, de);
920170808Sdelphij
921170808Sdelphij	/* vp link count has changed, so update node times. */
922170808Sdelphij	node->tn_status |= TMPFS_NODE_CHANGED;
923170808Sdelphij	tmpfs_update(vp);
924170808Sdelphij
925170808Sdelphij	error = 0;
926171070Sdelphij
927170808Sdelphijout:
928170808Sdelphij	return error;
929170808Sdelphij}
930170808Sdelphij
931170808Sdelphij/* --------------------------------------------------------------------- */
932170808Sdelphij
933233851Sgleb/*
934233851Sgleb * We acquire all but fdvp locks using non-blocking acquisitions.  If we
935233851Sgleb * fail to acquire any lock in the path we will drop all held locks,
936233851Sgleb * acquire the new lock in a blocking fashion, and then release it and
937233851Sgleb * restart the rename.  This acquire/release step ensures that we do not
938233851Sgleb * spin on a lock waiting for release.  On error release all vnode locks
939233851Sgleb * and decrement references the way tmpfs_rename() would do.
940233851Sgleb */
941171069Sdelphijstatic int
942233851Sglebtmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
943233851Sgleb    struct vnode *tdvp, struct vnode **tvpp,
944233851Sgleb    struct componentname *fcnp, struct componentname *tcnp)
945233851Sgleb{
946233851Sgleb	struct vnode *nvp;
947233851Sgleb	struct mount *mp;
948233851Sgleb	struct tmpfs_dirent *de;
949233851Sgleb	int error, restarts = 0;
950233851Sgleb
951233851Sgleb	VOP_UNLOCK(tdvp, 0);
952233851Sgleb	if (*tvpp != NULL && *tvpp != tdvp)
953233851Sgleb		VOP_UNLOCK(*tvpp, 0);
954233851Sgleb	mp = fdvp->v_mount;
955233851Sgleb
956233851Sglebrelock:
957233851Sgleb	restarts += 1;
958233851Sgleb	error = vn_lock(fdvp, LK_EXCLUSIVE);
959233851Sgleb	if (error)
960233851Sgleb		goto releout;
961233851Sgleb	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
962233851Sgleb		VOP_UNLOCK(fdvp, 0);
963233851Sgleb		error = vn_lock(tdvp, LK_EXCLUSIVE);
964233851Sgleb		if (error)
965233851Sgleb			goto releout;
966233851Sgleb		VOP_UNLOCK(tdvp, 0);
967233851Sgleb		goto relock;
968233851Sgleb	}
969233851Sgleb	/*
970233851Sgleb	 * Re-resolve fvp to be certain it still exists and fetch the
971233851Sgleb	 * correct vnode.
972233851Sgleb	 */
973233851Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
974233851Sgleb	if (de == NULL) {
975233851Sgleb		VOP_UNLOCK(fdvp, 0);
976233851Sgleb		VOP_UNLOCK(tdvp, 0);
977233851Sgleb		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
978233851Sgleb		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
979233851Sgleb			error = EINVAL;
980233851Sgleb		else
981233851Sgleb			error = ENOENT;
982233851Sgleb		goto releout;
983233851Sgleb	}
984233851Sgleb	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
985233851Sgleb	if (error != 0) {
986233851Sgleb		VOP_UNLOCK(fdvp, 0);
987233851Sgleb		VOP_UNLOCK(tdvp, 0);
988233851Sgleb		if (error != EBUSY)
989233851Sgleb			goto releout;
990233851Sgleb		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
991233851Sgleb		if (error != 0)
992233851Sgleb			goto releout;
993233851Sgleb		VOP_UNLOCK(nvp, 0);
994233851Sgleb		/*
995233851Sgleb		 * Concurrent rename race.
996233851Sgleb		 */
997233851Sgleb		if (nvp == tdvp) {
998233851Sgleb			vrele(nvp);
999233851Sgleb			error = EINVAL;
1000233851Sgleb			goto releout;
1001233851Sgleb		}
1002233851Sgleb		vrele(*fvpp);
1003233851Sgleb		*fvpp = nvp;
1004233851Sgleb		goto relock;
1005233851Sgleb	}
1006233851Sgleb	vrele(*fvpp);
1007233851Sgleb	*fvpp = nvp;
1008233851Sgleb	VOP_UNLOCK(*fvpp, 0);
1009233851Sgleb	/*
1010233851Sgleb	 * Re-resolve tvp and acquire the vnode lock if present.
1011233851Sgleb	 */
1012233851Sgleb	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
1013233851Sgleb	/*
1014233851Sgleb	 * If tvp disappeared we just carry on.
1015233851Sgleb	 */
1016233851Sgleb	if (de == NULL && *tvpp != NULL) {
1017233851Sgleb		vrele(*tvpp);
1018233851Sgleb		*tvpp = NULL;
1019233851Sgleb	}
1020233851Sgleb	/*
1021233851Sgleb	 * Get the tvp ino if the lookup succeeded.  We may have to restart
1022233851Sgleb	 * if the non-blocking acquire fails.
1023233851Sgleb	 */
1024233851Sgleb	if (de != NULL) {
1025233851Sgleb		nvp = NULL;
1026233851Sgleb		error = tmpfs_alloc_vp(mp, de->td_node,
1027233851Sgleb		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1028233851Sgleb		if (*tvpp != NULL)
1029233851Sgleb			vrele(*tvpp);
1030233851Sgleb		*tvpp = nvp;
1031233851Sgleb		if (error != 0) {
1032233851Sgleb			VOP_UNLOCK(fdvp, 0);
1033233851Sgleb			VOP_UNLOCK(tdvp, 0);
1034233851Sgleb			if (error != EBUSY)
1035233851Sgleb				goto releout;
1036233851Sgleb			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
1037233851Sgleb			    &nvp);
1038233851Sgleb			if (error != 0)
1039233851Sgleb				goto releout;
1040233851Sgleb			VOP_UNLOCK(nvp, 0);
1041233851Sgleb			/*
1042233851Sgleb			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
1043233851Sgleb			 */
1044233851Sgleb			if (nvp == fdvp) {
1045233851Sgleb				error = ENOTEMPTY;
1046233851Sgleb				goto releout;
1047233851Sgleb			}
1048233851Sgleb			goto relock;
1049233851Sgleb		}
1050233851Sgleb	}
1051233851Sgleb	tmpfs_rename_restarts += restarts;
1052233851Sgleb
1053233851Sgleb	return (0);
1054233851Sgleb
1055233851Sglebreleout:
1056233851Sgleb	vrele(fdvp);
1057233851Sgleb	vrele(*fvpp);
1058233851Sgleb	vrele(tdvp);
1059233851Sgleb	if (*tvpp != NULL)
1060233851Sgleb		vrele(*tvpp);
1061233851Sgleb	tmpfs_rename_restarts += restarts;
1062233851Sgleb
1063233851Sgleb	return (error);
1064233851Sgleb}
1065233851Sgleb
1066233851Sglebstatic int
1067170808Sdelphijtmpfs_rename(struct vop_rename_args *v)
1068170808Sdelphij{
1069170808Sdelphij	struct vnode *fdvp = v->a_fdvp;
1070170808Sdelphij	struct vnode *fvp = v->a_fvp;
1071170808Sdelphij	struct componentname *fcnp = v->a_fcnp;
1072170808Sdelphij	struct vnode *tdvp = v->a_tdvp;
1073170808Sdelphij	struct vnode *tvp = v->a_tvp;
1074170808Sdelphij	struct componentname *tcnp = v->a_tcnp;
1075233851Sgleb	struct mount *mp = NULL;
1076170808Sdelphij
1077170808Sdelphij	char *newname;
1078170808Sdelphij	int error;
1079170808Sdelphij	struct tmpfs_dirent *de;
1080197953Sdelphij	struct tmpfs_mount *tmp;
1081170808Sdelphij	struct tmpfs_node *fdnode;
1082170808Sdelphij	struct tmpfs_node *fnode;
1083171799Sdelphij	struct tmpfs_node *tnode;
1084170808Sdelphij	struct tmpfs_node *tdnode;
1085170808Sdelphij
1086176559Sattilio	MPASS(VOP_ISLOCKED(tdvp));
1087176559Sattilio	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
1088170808Sdelphij	MPASS(fcnp->cn_flags & HASBUF);
1089170808Sdelphij	MPASS(tcnp->cn_flags & HASBUF);
1090170808Sdelphij
1091170808Sdelphij	/* Disallow cross-device renames.
1092170808Sdelphij	 * XXX Why isn't this done by the caller? */
1093170808Sdelphij	if (fvp->v_mount != tdvp->v_mount ||
1094170808Sdelphij	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1095170808Sdelphij		error = EXDEV;
1096170808Sdelphij		goto out;
1097170808Sdelphij	}
1098170808Sdelphij
1099170808Sdelphij	/* If source and target are the same file, there is nothing to do. */
1100170808Sdelphij	if (fvp == tvp) {
1101170808Sdelphij		error = 0;
1102170808Sdelphij		goto out;
1103170808Sdelphij	}
1104170808Sdelphij
1105173725Sdelphij	/* If we need to move the directory between entries, lock the
1106173725Sdelphij	 * source so that we can safely operate on it. */
1107233851Sgleb	if (fdvp != tdvp && fdvp != tvp) {
1108233851Sgleb		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1109233851Sgleb			mp = tdvp->v_mount;
1110233851Sgleb			error = vfs_busy(mp, 0);
1111233851Sgleb			if (error != 0) {
1112233851Sgleb				mp = NULL;
1113233851Sgleb				goto out;
1114233851Sgleb			}
1115233851Sgleb			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1116233851Sgleb			    fcnp, tcnp);
1117233851Sgleb			if (error != 0) {
1118233851Sgleb				vfs_unbusy(mp);
1119233851Sgleb				return (error);
1120233851Sgleb			}
1121233851Sgleb			ASSERT_VOP_ELOCKED(fdvp,
1122233851Sgleb			    "tmpfs_rename: fdvp not locked");
1123233851Sgleb			ASSERT_VOP_ELOCKED(tdvp,
1124233851Sgleb			    "tmpfs_rename: tdvp not locked");
1125233851Sgleb			if (tvp != NULL)
1126233851Sgleb				ASSERT_VOP_ELOCKED(tvp,
1127233851Sgleb				    "tmpfs_rename: tvp not locked");
1128233851Sgleb			if (fvp == tvp) {
1129233851Sgleb				error = 0;
1130233851Sgleb				goto out_locked;
1131233851Sgleb			}
1132233851Sgleb		}
1133233851Sgleb	}
1134233851Sgleb
1135233851Sgleb	tmp = VFS_TO_TMPFS(tdvp->v_mount);
1136233851Sgleb	tdnode = VP_TO_TMPFS_DIR(tdvp);
1137233851Sgleb	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1138173725Sdelphij	fdnode = VP_TO_TMPFS_DIR(fdvp);
1139173725Sdelphij	fnode = VP_TO_TMPFS_NODE(fvp);
1140188318Skib	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1141173725Sdelphij
1142212305Sivoras	/* Entry can disappear before we lock fdvp,
1143212305Sivoras	 * also avoid manipulating '.' and '..' entries. */
1144170808Sdelphij	if (de == NULL) {
1145212305Sivoras		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1146212305Sivoras		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1147212305Sivoras			error = EINVAL;
1148212305Sivoras		else
1149212305Sivoras			error = ENOENT;
1150173725Sdelphij		goto out_locked;
1151170808Sdelphij	}
1152170808Sdelphij	MPASS(de->td_node == fnode);
1153170808Sdelphij
1154171070Sdelphij	/* If re-naming a directory to another preexisting directory
1155170808Sdelphij	 * ensure that the target directory is empty so that its
1156171070Sdelphij	 * removal causes no side effects.
1157170808Sdelphij	 * Kern_rename gurantees the destination to be a directory
1158170808Sdelphij	 * if the source is one. */
1159170808Sdelphij	if (tvp != NULL) {
1160171799Sdelphij		MPASS(tnode != NULL);
1161171070Sdelphij
1162170808Sdelphij		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1163170808Sdelphij		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1164170808Sdelphij			error = EPERM;
1165173725Sdelphij			goto out_locked;
1166170808Sdelphij		}
1167170808Sdelphij
1168171799Sdelphij		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1169171799Sdelphij			if (tnode->tn_size > 0) {
1170171799Sdelphij				error = ENOTEMPTY;
1171173725Sdelphij				goto out_locked;
1172171799Sdelphij			}
1173171799Sdelphij		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1174171799Sdelphij			error = ENOTDIR;
1175173725Sdelphij			goto out_locked;
1176171799Sdelphij		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1177171799Sdelphij			error = EISDIR;
1178173725Sdelphij			goto out_locked;
1179171799Sdelphij		} else {
1180171799Sdelphij			MPASS(fnode->tn_type != VDIR &&
1181171799Sdelphij				tnode->tn_type != VDIR);
1182170808Sdelphij		}
1183170808Sdelphij	}
1184170808Sdelphij
1185170808Sdelphij	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1186170808Sdelphij	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1187170808Sdelphij		error = EPERM;
1188170808Sdelphij		goto out_locked;
1189170808Sdelphij	}
1190170808Sdelphij
1191170808Sdelphij	/* Ensure that we have enough memory to hold the new name, if it
1192170808Sdelphij	 * has to be changed. */
1193170808Sdelphij	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1194183299Sobrien	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1195171087Sdelphij		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1196170808Sdelphij	} else
1197170808Sdelphij		newname = NULL;
1198170808Sdelphij
1199170808Sdelphij	/* If the node is being moved to another directory, we have to do
1200170808Sdelphij	 * the move. */
1201170808Sdelphij	if (fdnode != tdnode) {
1202170808Sdelphij		/* In case we are moving a directory, we have to adjust its
1203170808Sdelphij		 * parent to point to the new parent. */
1204170808Sdelphij		if (de->td_node->tn_type == VDIR) {
1205170808Sdelphij			struct tmpfs_node *n;
1206170808Sdelphij
1207170808Sdelphij			/* Ensure the target directory is not a child of the
1208170808Sdelphij			 * directory being moved.  Otherwise, we'd end up
1209170808Sdelphij			 * with stale nodes. */
1210170808Sdelphij			n = tdnode;
1211197953Sdelphij			/* TMPFS_LOCK garanties that no nodes are freed while
1212197953Sdelphij			 * traversing the list. Nodes can only be marked as
1213197953Sdelphij			 * removed: tn_parent == NULL. */
1214197953Sdelphij			TMPFS_LOCK(tmp);
1215197953Sdelphij			TMPFS_NODE_LOCK(n);
1216170808Sdelphij			while (n != n->tn_dir.tn_parent) {
1217197953Sdelphij				struct tmpfs_node *parent;
1218197953Sdelphij
1219170808Sdelphij				if (n == fnode) {
1220197953Sdelphij					TMPFS_NODE_UNLOCK(n);
1221197953Sdelphij					TMPFS_UNLOCK(tmp);
1222170808Sdelphij					error = EINVAL;
1223170808Sdelphij					if (newname != NULL)
1224171087Sdelphij						    free(newname, M_TMPFSNAME);
1225170808Sdelphij					goto out_locked;
1226170808Sdelphij				}
1227197953Sdelphij				parent = n->tn_dir.tn_parent;
1228197953Sdelphij				TMPFS_NODE_UNLOCK(n);
1229197953Sdelphij				if (parent == NULL) {
1230197953Sdelphij					n = NULL;
1231197953Sdelphij					break;
1232197953Sdelphij				}
1233197953Sdelphij				TMPFS_NODE_LOCK(parent);
1234197953Sdelphij				if (parent->tn_dir.tn_parent == NULL) {
1235197953Sdelphij					TMPFS_NODE_UNLOCK(parent);
1236197953Sdelphij					n = NULL;
1237197953Sdelphij					break;
1238197953Sdelphij				}
1239197953Sdelphij				n = parent;
1240170808Sdelphij			}
1241197953Sdelphij			TMPFS_UNLOCK(tmp);
1242197953Sdelphij			if (n == NULL) {
1243197953Sdelphij				error = EINVAL;
1244197953Sdelphij				if (newname != NULL)
1245197953Sdelphij					    free(newname, M_TMPFSNAME);
1246197953Sdelphij				goto out_locked;
1247197953Sdelphij			}
1248197953Sdelphij			TMPFS_NODE_UNLOCK(n);
1249170808Sdelphij
1250170808Sdelphij			/* Adjust the parent pointer. */
1251170808Sdelphij			TMPFS_VALIDATE_DIR(fnode);
1252197953Sdelphij			TMPFS_NODE_LOCK(de->td_node);
1253170808Sdelphij			de->td_node->tn_dir.tn_parent = tdnode;
1254197953Sdelphij			TMPFS_NODE_UNLOCK(de->td_node);
1255170808Sdelphij
1256170808Sdelphij			/* As a result of changing the target of the '..'
1257170808Sdelphij			 * entry, the link count of the source and target
1258170808Sdelphij			 * directories has to be adjusted. */
1259197953Sdelphij			TMPFS_NODE_LOCK(tdnode);
1260197953Sdelphij			TMPFS_ASSERT_LOCKED(tdnode);
1261197953Sdelphij			tdnode->tn_links++;
1262197953Sdelphij			TMPFS_NODE_UNLOCK(tdnode);
1263197953Sdelphij
1264197953Sdelphij			TMPFS_NODE_LOCK(fdnode);
1265197953Sdelphij			TMPFS_ASSERT_LOCKED(fdnode);
1266170808Sdelphij			fdnode->tn_links--;
1267197953Sdelphij			TMPFS_NODE_UNLOCK(fdnode);
1268170808Sdelphij		}
1269170808Sdelphij
1270170808Sdelphij		/* Do the move: just remove the entry from the source directory
1271170808Sdelphij		 * and insert it into the target one. */
1272170808Sdelphij		tmpfs_dir_detach(fdvp, de);
1273211598Sed		if (fcnp->cn_flags & DOWHITEOUT)
1274211598Sed			tmpfs_dir_whiteout_add(fdvp, fcnp);
1275211598Sed		if (tcnp->cn_flags & ISWHITEOUT)
1276211598Sed			tmpfs_dir_whiteout_remove(tdvp, tcnp);
1277170808Sdelphij		tmpfs_dir_attach(tdvp, de);
1278170808Sdelphij	}
1279170808Sdelphij
1280170808Sdelphij	/* If the name has changed, we need to make it effective by changing
1281170808Sdelphij	 * it in the directory entry. */
1282170808Sdelphij	if (newname != NULL) {
1283170808Sdelphij		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1284170808Sdelphij
1285171087Sdelphij		free(de->td_name, M_TMPFSNAME);
1286170808Sdelphij		de->td_namelen = (uint16_t)tcnp->cn_namelen;
1287170808Sdelphij		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1288170808Sdelphij		de->td_name = newname;
1289170808Sdelphij
1290170808Sdelphij		fnode->tn_status |= TMPFS_NODE_CHANGED;
1291170808Sdelphij		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1292170808Sdelphij	}
1293170808Sdelphij
1294170808Sdelphij	/* If we are overwriting an entry, we have to remove the old one
1295170808Sdelphij	 * from the target directory. */
1296170808Sdelphij	if (tvp != NULL) {
1297170808Sdelphij		/* Remove the old entry from the target directory. */
1298188318Skib		de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1299170808Sdelphij		tmpfs_dir_detach(tdvp, de);
1300170808Sdelphij
1301170808Sdelphij		/* Free the directory entry we just deleted.  Note that the
1302170808Sdelphij		 * node referred by it will not be removed until the vnode is
1303170808Sdelphij		 * really reclaimed. */
1304170808Sdelphij		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1305170808Sdelphij	}
1306229130Spho	cache_purge(fvp);
1307233385Sjhb	if (tvp != NULL)
1308233385Sjhb		cache_purge(tvp);
1309248678Skib	cache_purge_negative(tdvp);
1310170808Sdelphij
1311170808Sdelphij	error = 0;
1312170808Sdelphij
1313170808Sdelphijout_locked:
1314229855Sivoras	if (fdvp != tdvp && fdvp != tvp)
1315175294Sattilio		VOP_UNLOCK(fdvp, 0);
1316170808Sdelphij
1317170808Sdelphijout:
1318170808Sdelphij	/* Release target nodes. */
1319170808Sdelphij	/* XXX: I don't understand when tdvp can be the same as tvp, but
1320170808Sdelphij	 * other code takes care of this... */
1321170808Sdelphij	if (tdvp == tvp)
1322170808Sdelphij		vrele(tdvp);
1323170808Sdelphij	else
1324170808Sdelphij		vput(tdvp);
1325170808Sdelphij	if (tvp != NULL)
1326170808Sdelphij		vput(tvp);
1327170808Sdelphij
1328170808Sdelphij	/* Release source nodes. */
1329170808Sdelphij	vrele(fdvp);
1330170808Sdelphij	vrele(fvp);
1331170808Sdelphij
1332233851Sgleb	if (mp != NULL)
1333233851Sgleb		vfs_unbusy(mp);
1334233851Sgleb
1335170808Sdelphij	return error;
1336170808Sdelphij}
1337170808Sdelphij
1338170808Sdelphij/* --------------------------------------------------------------------- */
1339170808Sdelphij
1340171069Sdelphijstatic int
1341170808Sdelphijtmpfs_mkdir(struct vop_mkdir_args *v)
1342170808Sdelphij{
1343170808Sdelphij	struct vnode *dvp = v->a_dvp;
1344170808Sdelphij	struct vnode **vpp = v->a_vpp;
1345170808Sdelphij	struct componentname *cnp = v->a_cnp;
1346170808Sdelphij	struct vattr *vap = v->a_vap;
1347170808Sdelphij
1348170808Sdelphij	MPASS(vap->va_type == VDIR);
1349170808Sdelphij
1350170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1351170808Sdelphij}
1352170808Sdelphij
1353170808Sdelphij/* --------------------------------------------------------------------- */
1354170808Sdelphij
1355171069Sdelphijstatic int
1356170808Sdelphijtmpfs_rmdir(struct vop_rmdir_args *v)
1357170808Sdelphij{
1358170808Sdelphij	struct vnode *dvp = v->a_dvp;
1359170808Sdelphij	struct vnode *vp = v->a_vp;
1360170808Sdelphij
1361170808Sdelphij	int error;
1362170808Sdelphij	struct tmpfs_dirent *de;
1363170808Sdelphij	struct tmpfs_mount *tmp;
1364170808Sdelphij	struct tmpfs_node *dnode;
1365170808Sdelphij	struct tmpfs_node *node;
1366170808Sdelphij
1367176559Sattilio	MPASS(VOP_ISLOCKED(dvp));
1368176559Sattilio	MPASS(VOP_ISLOCKED(vp));
1369170808Sdelphij
1370170808Sdelphij	tmp = VFS_TO_TMPFS(dvp->v_mount);
1371170808Sdelphij	dnode = VP_TO_TMPFS_DIR(dvp);
1372170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1373170808Sdelphij
1374171070Sdelphij	/* Directories with more than two entries ('.' and '..') cannot be
1375171070Sdelphij	 * removed. */
1376171070Sdelphij	 if (node->tn_size > 0) {
1377171070Sdelphij		 error = ENOTEMPTY;
1378171070Sdelphij		 goto out;
1379171070Sdelphij	 }
1380170808Sdelphij
1381170808Sdelphij	if ((dnode->tn_flags & APPEND)
1382170808Sdelphij	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1383170808Sdelphij		error = EPERM;
1384170808Sdelphij		goto out;
1385170808Sdelphij	}
1386170808Sdelphij
1387171070Sdelphij	/* This invariant holds only if we are not trying to remove "..".
1388171070Sdelphij	  * We checked for that above so this is safe now. */
1389170808Sdelphij	MPASS(node->tn_dir.tn_parent == dnode);
1390170808Sdelphij
1391170808Sdelphij	/* Get the directory entry associated with node (vp).  This was
1392170808Sdelphij	 * filled by tmpfs_lookup while looking up the entry. */
1393188318Skib	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1394170808Sdelphij	MPASS(TMPFS_DIRENT_MATCHES(de,
1395170808Sdelphij	    v->a_cnp->cn_nameptr,
1396170808Sdelphij	    v->a_cnp->cn_namelen));
1397170808Sdelphij
1398170808Sdelphij	/* Check flags to see if we are allowed to remove the directory. */
1399170808Sdelphij	if (dnode->tn_flags & APPEND
1400170808Sdelphij		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1401170808Sdelphij		error = EPERM;
1402170808Sdelphij		goto out;
1403170808Sdelphij	}
1404170808Sdelphij
1405197953Sdelphij
1406170808Sdelphij	/* Detach the directory entry from the directory (dnode). */
1407170808Sdelphij	tmpfs_dir_detach(dvp, de);
1408211598Sed	if (v->a_cnp->cn_flags & DOWHITEOUT)
1409211598Sed		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1410170808Sdelphij
1411197953Sdelphij	/* No vnode should be allocated for this entry from this point */
1412197953Sdelphij	TMPFS_NODE_LOCK(node);
1413197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1414170808Sdelphij	node->tn_links--;
1415197953Sdelphij	node->tn_dir.tn_parent = NULL;
1416170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1417170808Sdelphij	    TMPFS_NODE_MODIFIED;
1418197953Sdelphij
1419197953Sdelphij	TMPFS_NODE_UNLOCK(node);
1420197953Sdelphij
1421197953Sdelphij	TMPFS_NODE_LOCK(dnode);
1422197953Sdelphij	TMPFS_ASSERT_ELOCKED(dnode);
1423197953Sdelphij	dnode->tn_links--;
1424197953Sdelphij	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1425170808Sdelphij	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1426197953Sdelphij	TMPFS_NODE_UNLOCK(dnode);
1427170808Sdelphij
1428171070Sdelphij	cache_purge(dvp);
1429170808Sdelphij	cache_purge(vp);
1430170808Sdelphij
1431170808Sdelphij	/* Free the directory entry we just deleted.  Note that the node
1432170808Sdelphij	 * referred by it will not be removed until the vnode is really
1433170808Sdelphij	 * reclaimed. */
1434170808Sdelphij	tmpfs_free_dirent(tmp, de, TRUE);
1435170808Sdelphij
1436170808Sdelphij	/* Release the deleted vnode (will destroy the node, notify
1437170808Sdelphij	 * interested parties and clean it from the cache). */
1438170808Sdelphij
1439170808Sdelphij	dnode->tn_status |= TMPFS_NODE_CHANGED;
1440170808Sdelphij	tmpfs_update(dvp);
1441170808Sdelphij
1442170808Sdelphij	error = 0;
1443170808Sdelphij
1444170808Sdelphijout:
1445170808Sdelphij	return error;
1446170808Sdelphij}
1447170808Sdelphij
1448170808Sdelphij/* --------------------------------------------------------------------- */
1449170808Sdelphij
1450171069Sdelphijstatic int
1451170808Sdelphijtmpfs_symlink(struct vop_symlink_args *v)
1452170808Sdelphij{
1453170808Sdelphij	struct vnode *dvp = v->a_dvp;
1454170808Sdelphij	struct vnode **vpp = v->a_vpp;
1455170808Sdelphij	struct componentname *cnp = v->a_cnp;
1456170808Sdelphij	struct vattr *vap = v->a_vap;
1457170808Sdelphij	char *target = v->a_target;
1458170808Sdelphij
1459170808Sdelphij#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1460170808Sdelphij	MPASS(vap->va_type == VLNK);
1461170808Sdelphij#else
1462170808Sdelphij	vap->va_type = VLNK;
1463170808Sdelphij#endif
1464170808Sdelphij
1465170808Sdelphij	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1466170808Sdelphij}
1467170808Sdelphij
1468170808Sdelphij/* --------------------------------------------------------------------- */
1469170808Sdelphij
1470171069Sdelphijstatic int
1471170808Sdelphijtmpfs_readdir(struct vop_readdir_args *v)
1472170808Sdelphij{
1473170808Sdelphij	struct vnode *vp = v->a_vp;
1474170808Sdelphij	struct uio *uio = v->a_uio;
1475170808Sdelphij	int *eofflag = v->a_eofflag;
1476170808Sdelphij	u_long **cookies = v->a_cookies;
1477170808Sdelphij	int *ncookies = v->a_ncookies;
1478170808Sdelphij
1479170808Sdelphij	int error;
1480170808Sdelphij	off_t startoff;
1481171802Sdelphij	off_t cnt = 0;
1482170808Sdelphij	struct tmpfs_node *node;
1483170808Sdelphij
1484170808Sdelphij	/* This operation only makes sense on directory nodes. */
1485171802Sdelphij	if (vp->v_type != VDIR)
1486171802Sdelphij		return ENOTDIR;
1487170808Sdelphij
1488170808Sdelphij	node = VP_TO_TMPFS_DIR(vp);
1489170808Sdelphij
1490170808Sdelphij	startoff = uio->uio_offset;
1491170808Sdelphij
1492171862Sdelphij	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1493170808Sdelphij		error = tmpfs_dir_getdotdent(node, uio);
1494171862Sdelphij		if (error != 0)
1495171862Sdelphij			goto outok;
1496171862Sdelphij		cnt++;
1497171862Sdelphij	}
1498171862Sdelphij
1499171862Sdelphij	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1500170808Sdelphij		error = tmpfs_dir_getdotdotdent(node, uio);
1501171862Sdelphij		if (error != 0)
1502171862Sdelphij			goto outok;
1503171862Sdelphij		cnt++;
1504170808Sdelphij	}
1505170808Sdelphij
1506171862Sdelphij	error = tmpfs_dir_getdents(node, uio, &cnt);
1507171862Sdelphij
1508171862Sdelphijoutok:
1509171862Sdelphij	MPASS(error >= -1);
1510171862Sdelphij
1511170808Sdelphij	if (error == -1)
1512217633Skib		error = (cnt != 0) ? 0 : EINVAL;
1513170808Sdelphij
1514170808Sdelphij	if (eofflag != NULL)
1515170808Sdelphij		*eofflag =
1516170808Sdelphij		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1517170808Sdelphij
1518170808Sdelphij	/* Update NFS-related variables. */
1519170808Sdelphij	if (error == 0 && cookies != NULL && ncookies != NULL) {
1520170808Sdelphij		off_t i;
1521170808Sdelphij		off_t off = startoff;
1522170808Sdelphij		struct tmpfs_dirent *de = NULL;
1523170808Sdelphij
1524170808Sdelphij		*ncookies = cnt;
1525170808Sdelphij		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1526170808Sdelphij
1527170808Sdelphij		for (i = 0; i < cnt; i++) {
1528170808Sdelphij			MPASS(off != TMPFS_DIRCOOKIE_EOF);
1529170808Sdelphij			if (off == TMPFS_DIRCOOKIE_DOT) {
1530170808Sdelphij				off = TMPFS_DIRCOOKIE_DOTDOT;
1531170808Sdelphij			} else {
1532170808Sdelphij				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1533170808Sdelphij					de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1534170808Sdelphij				} else if (de != NULL) {
1535170808Sdelphij					de = TAILQ_NEXT(de, td_entries);
1536170808Sdelphij				} else {
1537170808Sdelphij					de = tmpfs_dir_lookupbycookie(node,
1538170808Sdelphij					    off);
1539170808Sdelphij					MPASS(de != NULL);
1540170808Sdelphij					de = TAILQ_NEXT(de, td_entries);
1541170808Sdelphij				}
1542171802Sdelphij				if (de == NULL)
1543170808Sdelphij					off = TMPFS_DIRCOOKIE_EOF;
1544171802Sdelphij				else
1545171802Sdelphij					off = tmpfs_dircookie(de);
1546170808Sdelphij			}
1547170808Sdelphij
1548170808Sdelphij			(*cookies)[i] = off;
1549170808Sdelphij		}
1550170808Sdelphij		MPASS(uio->uio_offset == off);
1551170808Sdelphij	}
1552170808Sdelphij
1553170808Sdelphij	return error;
1554170808Sdelphij}
1555170808Sdelphij
1556170808Sdelphij/* --------------------------------------------------------------------- */
1557170808Sdelphij
1558171069Sdelphijstatic int
1559170808Sdelphijtmpfs_readlink(struct vop_readlink_args *v)
1560170808Sdelphij{
1561170808Sdelphij	struct vnode *vp = v->a_vp;
1562170808Sdelphij	struct uio *uio = v->a_uio;
1563170808Sdelphij
1564170808Sdelphij	int error;
1565170808Sdelphij	struct tmpfs_node *node;
1566170808Sdelphij
1567170808Sdelphij	MPASS(uio->uio_offset == 0);
1568170808Sdelphij	MPASS(vp->v_type == VLNK);
1569170808Sdelphij
1570170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1571170808Sdelphij
1572170808Sdelphij	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1573170808Sdelphij	    uio);
1574170808Sdelphij	node->tn_status |= TMPFS_NODE_ACCESSED;
1575170808Sdelphij
1576170808Sdelphij	return error;
1577170808Sdelphij}
1578170808Sdelphij
1579170808Sdelphij/* --------------------------------------------------------------------- */
1580170808Sdelphij
1581171069Sdelphijstatic int
1582170808Sdelphijtmpfs_inactive(struct vop_inactive_args *v)
1583170808Sdelphij{
1584170808Sdelphij	struct vnode *vp = v->a_vp;
1585170808Sdelphij	struct thread *l = v->a_td;
1586170808Sdelphij
1587170808Sdelphij	struct tmpfs_node *node;
1588170808Sdelphij
1589176559Sattilio	MPASS(VOP_ISLOCKED(vp));
1590170808Sdelphij
1591170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1592170808Sdelphij
1593170808Sdelphij	if (node->tn_links == 0)
1594170808Sdelphij		vrecycle(vp, l);
1595170808Sdelphij
1596170808Sdelphij	return 0;
1597170808Sdelphij}
1598170808Sdelphij
1599170808Sdelphij/* --------------------------------------------------------------------- */
1600170808Sdelphij
1601170808Sdelphijint
1602170808Sdelphijtmpfs_reclaim(struct vop_reclaim_args *v)
1603170808Sdelphij{
1604170808Sdelphij	struct vnode *vp = v->a_vp;
1605170808Sdelphij
1606170808Sdelphij	struct tmpfs_mount *tmp;
1607170808Sdelphij	struct tmpfs_node *node;
1608170808Sdelphij
1609170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1610170808Sdelphij	tmp = VFS_TO_TMPFS(vp->v_mount);
1611171070Sdelphij
1612170808Sdelphij	vnode_destroy_vobject(vp);
1613170808Sdelphij	cache_purge(vp);
1614197953Sdelphij
1615197953Sdelphij	TMPFS_NODE_LOCK(node);
1616197953Sdelphij	TMPFS_ASSERT_ELOCKED(node);
1617170808Sdelphij	tmpfs_free_vp(vp);
1618170808Sdelphij
1619170808Sdelphij	/* If the node referenced by this vnode was deleted by the user,
1620170808Sdelphij	 * we must free its associated data structures (now that the vnode
1621170808Sdelphij	 * is being reclaimed). */
1622197953Sdelphij	if (node->tn_links == 0 &&
1623197953Sdelphij	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1624197953Sdelphij		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1625197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1626170808Sdelphij		tmpfs_free_node(tmp, node);
1627197953Sdelphij	} else
1628197953Sdelphij		TMPFS_NODE_UNLOCK(node);
1629170808Sdelphij
1630170808Sdelphij	MPASS(vp->v_data == NULL);
1631170808Sdelphij	return 0;
1632170808Sdelphij}
1633170808Sdelphij
1634170808Sdelphij/* --------------------------------------------------------------------- */
1635170808Sdelphij
1636171069Sdelphijstatic int
1637170808Sdelphijtmpfs_print(struct vop_print_args *v)
1638170808Sdelphij{
1639170808Sdelphij	struct vnode *vp = v->a_vp;
1640170808Sdelphij
1641170808Sdelphij	struct tmpfs_node *node;
1642170808Sdelphij
1643170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
1644170808Sdelphij
1645170808Sdelphij	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1646170808Sdelphij	    node, node->tn_flags, node->tn_links);
1647170808Sdelphij	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1648170808Sdelphij	    ", status 0x%x\n",
1649170808Sdelphij	    node->tn_mode, node->tn_uid, node->tn_gid,
1650170808Sdelphij	    (uintmax_t)node->tn_size, node->tn_status);
1651170808Sdelphij
1652170808Sdelphij	if (vp->v_type == VFIFO)
1653170808Sdelphij		fifo_printinfo(vp);
1654170808Sdelphij
1655170808Sdelphij	printf("\n");
1656170808Sdelphij
1657170808Sdelphij	return 0;
1658170808Sdelphij}
1659170808Sdelphij
1660170808Sdelphij/* --------------------------------------------------------------------- */
1661170808Sdelphij
1662171069Sdelphijstatic int
1663170808Sdelphijtmpfs_pathconf(struct vop_pathconf_args *v)
1664170808Sdelphij{
1665170808Sdelphij	int name = v->a_name;
1666170808Sdelphij	register_t *retval = v->a_retval;
1667170808Sdelphij
1668170808Sdelphij	int error;
1669170808Sdelphij
1670170808Sdelphij	error = 0;
1671170808Sdelphij
1672170808Sdelphij	switch (name) {
1673170808Sdelphij	case _PC_LINK_MAX:
1674170808Sdelphij		*retval = LINK_MAX;
1675170808Sdelphij		break;
1676170808Sdelphij
1677170808Sdelphij	case _PC_NAME_MAX:
1678170808Sdelphij		*retval = NAME_MAX;
1679170808Sdelphij		break;
1680170808Sdelphij
1681170808Sdelphij	case _PC_PATH_MAX:
1682170808Sdelphij		*retval = PATH_MAX;
1683170808Sdelphij		break;
1684170808Sdelphij
1685170808Sdelphij	case _PC_PIPE_BUF:
1686170808Sdelphij		*retval = PIPE_BUF;
1687170808Sdelphij		break;
1688170808Sdelphij
1689170808Sdelphij	case _PC_CHOWN_RESTRICTED:
1690170808Sdelphij		*retval = 1;
1691170808Sdelphij		break;
1692170808Sdelphij
1693170808Sdelphij	case _PC_NO_TRUNC:
1694170808Sdelphij		*retval = 1;
1695170808Sdelphij		break;
1696170808Sdelphij
1697170808Sdelphij	case _PC_SYNC_IO:
1698170808Sdelphij		*retval = 1;
1699170808Sdelphij		break;
1700170808Sdelphij
1701170808Sdelphij	case _PC_FILESIZEBITS:
1702170808Sdelphij		*retval = 0; /* XXX Don't know which value should I return. */
1703170808Sdelphij		break;
1704170808Sdelphij
1705170808Sdelphij	default:
1706170808Sdelphij		error = EINVAL;
1707170808Sdelphij	}
1708170808Sdelphij
1709170808Sdelphij	return error;
1710170808Sdelphij}
1711170808Sdelphij
1712171069Sdelphijstatic int
1713170808Sdelphijtmpfs_vptofh(struct vop_vptofh_args *ap)
1714170808Sdelphij{
1715170808Sdelphij	struct tmpfs_fid *tfhp;
1716170808Sdelphij	struct tmpfs_node *node;
1717170808Sdelphij
1718170808Sdelphij	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1719170808Sdelphij	node = VP_TO_TMPFS_NODE(ap->a_vp);
1720170808Sdelphij
1721170808Sdelphij	tfhp->tf_len = sizeof(struct tmpfs_fid);
1722170808Sdelphij	tfhp->tf_id = node->tn_id;
1723170808Sdelphij	tfhp->tf_gen = node->tn_gen;
1724171070Sdelphij
1725170808Sdelphij	return (0);
1726170808Sdelphij}
1727171069Sdelphij
1728211598Sedstatic int
1729211598Sedtmpfs_whiteout(struct vop_whiteout_args *ap)
1730211598Sed{
1731211598Sed	struct vnode *dvp = ap->a_dvp;
1732211598Sed	struct componentname *cnp = ap->a_cnp;
1733211598Sed	struct tmpfs_dirent *de;
1734211598Sed
1735211598Sed	switch (ap->a_flags) {
1736211598Sed	case LOOKUP:
1737211598Sed		return (0);
1738211598Sed	case CREATE:
1739211598Sed		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1740211598Sed		if (de != NULL)
1741211598Sed			return (de->td_node == NULL ? 0 : EEXIST);
1742211598Sed		return (tmpfs_dir_whiteout_add(dvp, cnp));
1743211598Sed	case DELETE:
1744211598Sed		tmpfs_dir_whiteout_remove(dvp, cnp);
1745211598Sed		return (0);
1746211598Sed	default:
1747211598Sed		panic("tmpfs_whiteout: unknown op");
1748211598Sed	}
1749211598Sed}
1750211598Sed
1751171069Sdelphij/* --------------------------------------------------------------------- */
1752171069Sdelphij
1753171069Sdelphij/*
1754171069Sdelphij * vnode operations vector used for files stored in a tmpfs file system.
1755171069Sdelphij */
1756171069Sdelphijstruct vop_vector tmpfs_vnodeop_entries = {
1757171069Sdelphij	.vop_default =			&default_vnodeops,
1758171069Sdelphij	.vop_lookup =			vfs_cache_lookup,
1759171069Sdelphij	.vop_cachedlookup =		tmpfs_lookup,
1760171069Sdelphij	.vop_create =			tmpfs_create,
1761171069Sdelphij	.vop_mknod =			tmpfs_mknod,
1762171069Sdelphij	.vop_open =			tmpfs_open,
1763171069Sdelphij	.vop_close =			tmpfs_close,
1764171069Sdelphij	.vop_access =			tmpfs_access,
1765171069Sdelphij	.vop_getattr =			tmpfs_getattr,
1766171069Sdelphij	.vop_setattr =			tmpfs_setattr,
1767171069Sdelphij	.vop_read =			tmpfs_read,
1768171069Sdelphij	.vop_write =			tmpfs_write,
1769171069Sdelphij	.vop_fsync =			tmpfs_fsync,
1770171069Sdelphij	.vop_remove =			tmpfs_remove,
1771171069Sdelphij	.vop_link =			tmpfs_link,
1772171069Sdelphij	.vop_rename =			tmpfs_rename,
1773171069Sdelphij	.vop_mkdir =			tmpfs_mkdir,
1774171069Sdelphij	.vop_rmdir =			tmpfs_rmdir,
1775171069Sdelphij	.vop_symlink =			tmpfs_symlink,
1776171069Sdelphij	.vop_readdir =			tmpfs_readdir,
1777171069Sdelphij	.vop_readlink =			tmpfs_readlink,
1778171069Sdelphij	.vop_inactive =			tmpfs_inactive,
1779171069Sdelphij	.vop_reclaim =			tmpfs_reclaim,
1780171069Sdelphij	.vop_print =			tmpfs_print,
1781171069Sdelphij	.vop_pathconf =			tmpfs_pathconf,
1782171069Sdelphij	.vop_vptofh =			tmpfs_vptofh,
1783211598Sed	.vop_whiteout =			tmpfs_whiteout,
1784171069Sdelphij	.vop_bmap =			VOP_EOPNOTSUPP,
1785171069Sdelphij};
1786171069Sdelphij
1787