tmpfs_vnops.c revision 312803
143412Snewton/*	$NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $	*/
243412Snewton
343412Snewton/*-
443412Snewton * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
552333Snewton * All rights reserved.
6302097Sbrooks *
743412Snewton * This code is derived from software contributed to The NetBSD Foundation
843412Snewton * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
943412Snewton * 2005 program.
1043412Snewton *
1143412Snewton * Redistribution and use in source and binary forms, with or without
1243412Snewton * modification, are permitted provided that the following conditions
1365302Sobrien * are met:
1465302Sobrien * 1. Redistributions of source code must retain the above copyright
1565302Sobrien *    notice, this list of conditions and the following disclaimer.
1665302Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1743412Snewton *    notice, this list of conditions and the following disclaimer in the
1860290Sbde *    documentation and/or other materials provided with the distribution.
1960290Sbde *
2043412Snewton * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2143412Snewton * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22209581Skib * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23225618Skmacy * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24225618Skmacy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25225618Skmacy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26225618Skmacy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27209581Skib * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28225618Skmacy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29209581Skib * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30209581Skib * POSSIBILITY OF SUCH DAMAGE.
31225618Skmacy */
32225618Skmacy
33209581Skib/*
34225618Skmacy * tmpfs vnode interface.
35209581Skib */
36209581Skib#include <sys/cdefs.h>
37225618Skmacy__FBSDID("$FreeBSD: stable/10/sys/fs/tmpfs/tmpfs_vnops.c 312803 2017-01-26 10:49:45Z kib $");
38225618Skmacy
39209581Skib#include <sys/param.h>
40209581Skib#include <sys/fcntl.h>
41225618Skmacy#include <sys/lockf.h>
42225618Skmacy#include <sys/lock.h>
43209581Skib#include <sys/namei.h>
44209581Skib#include <sys/priv.h>
45225618Skmacy#include <sys/proc.h>
46225618Skmacy#include <sys/rwlock.h>
47209581Skib#include <sys/sched.h>
48209581Skib#include <sys/stat.h>
49209581Skib#include <sys/systm.h>
50209581Skib#include <sys/sysctl.h>
51209581Skib#include <sys/unistd.h>
52209581Skib#include <sys/vnode.h>
53209581Skib
54209581Skib#include <vm/vm.h>
55209581Skib#include <vm/vm_param.h>
56209581Skib#include <vm/vm_object.h>
57209581Skib#include <vm/vm_page.h>
58225618Skmacy#include <vm/vm_pager.h>
59209581Skib
60209581Skib#include <fs/tmpfs/tmpfs_vnops.h>
61209581Skib#include <fs/tmpfs/tmpfs.h>
62209581Skib
63225618SkmacySYSCTL_DECL(_vfs_tmpfs);
64302097Sbrooks
65209581Skibstatic volatile int tmpfs_rename_restarts;
66209581SkibSYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
67209581Skib    __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
68225618Skmacy    "Times rename had to restart due to lock contention");
69225618Skmacy
70209581Skibstatic int
71209581Skibtmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
72209581Skib    struct vnode **rvp)
73209581Skib{
74209581Skib
75209581Skib	return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
76209581Skib}
77209581Skib
78209581Skibstatic int
79209581Skibtmpfs_lookup(struct vop_cachedlookup_args *v)
80225618Skmacy{
81209581Skib	struct vnode *dvp = v->a_dvp;
82225618Skmacy	struct vnode **vpp = v->a_vpp;
83225618Skmacy	struct componentname *cnp = v->a_cnp;
84209581Skib	struct tmpfs_dirent *de;
85209581Skib	struct tmpfs_node *dnode;
86209581Skib	int error;
87209581Skib
88209581Skib	dnode = VP_TO_TMPFS_DIR(dvp);
89209581Skib	*vpp = NULLVP;
90209581Skib
91209581Skib	/* Check accessibility of requested node as a first step. */
92209581Skib	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
93209581Skib	if (error != 0)
94209581Skib		goto out;
95209581Skib
96209581Skib	/* We cannot be requesting the parent directory of the root node. */
97209581Skib	MPASS(IMPLIES(dnode->tn_type == VDIR &&
98209581Skib	    dnode->tn_dir.tn_parent == dnode,
99209581Skib	    !(cnp->cn_flags & ISDOTDOT)));
100209581Skib
101225618Skmacy	TMPFS_ASSERT_LOCKED(dnode);
102225618Skmacy	if (dnode->tn_dir.tn_parent == NULL) {
103209581Skib		error = ENOENT;
104209581Skib		goto out;
105209581Skib	}
106209581Skib	if (cnp->cn_flags & ISDOTDOT) {
107209581Skib		error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
108209581Skib		    dnode->tn_dir.tn_parent, cnp->cn_lkflags, vpp);
109209581Skib		if (error != 0)
110209581Skib			goto out;
111225618Skmacy	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
112225618Skmacy		VREF(dvp);
113225618Skmacy		*vpp = dvp;
114225618Skmacy		error = 0;
115225618Skmacy	} else {
116225618Skmacy		de = tmpfs_dir_lookup(dnode, NULL, cnp);
117209581Skib		if (de != NULL && de->td_node == NULL)
118209581Skib			cnp->cn_flags |= ISWHITEOUT;
119209581Skib		if (de == NULL || de->td_node == NULL) {
120209581Skib			/*
121209581Skib			 * The entry was not found in the directory.
122209581Skib			 * This is OK if we are creating or renaming an
123209581Skib			 * entry and are working on the last component of
124209581Skib			 * the path name.
125209581Skib			 */
126209581Skib			if ((cnp->cn_flags & ISLASTCN) &&
127209581Skib			    (cnp->cn_nameiop == CREATE || \
128209581Skib			    cnp->cn_nameiop == RENAME ||
129209581Skib			    (cnp->cn_nameiop == DELETE &&
130209581Skib			    cnp->cn_flags & DOWHITEOUT &&
131209581Skib			    cnp->cn_flags & ISWHITEOUT))) {
132209581Skib				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
133209581Skib				    cnp->cn_thread);
134209581Skib				if (error != 0)
135209581Skib					goto out;
136209581Skib
137209581Skib				/*
138225618Skmacy				 * Keep the component name in the buffer for
139225618Skmacy				 * future uses.
140209581Skib				 */
141225618Skmacy				cnp->cn_flags |= SAVENAME;
142225618Skmacy
143225618Skmacy				error = EJUSTRETURN;
144225618Skmacy			} else
145209581Skib				error = ENOENT;
146209581Skib		} else {
147209581Skib			struct tmpfs_node *tnode;
148209581Skib
149209581Skib			/*
150209581Skib			 * The entry was found, so get its associated
151209581Skib			 * tmpfs_node.
152225618Skmacy			 */
153209581Skib			tnode = de->td_node;
154209581Skib
155209581Skib			/*
156225618Skmacy			 * If we are not at the last path component and
157209581Skib			 * found a non-directory or non-link entry (which
158225618Skmacy			 * may itself be pointing to a directory), raise
159209581Skib			 * an error.
160225618Skmacy			 */
161209581Skib			if ((tnode->tn_type != VDIR &&
162209581Skib			    tnode->tn_type != VLNK) &&
163225618Skmacy			    !(cnp->cn_flags & ISLASTCN)) {
164209581Skib				error = ENOTDIR;
165209581Skib				goto out;
166209581Skib			}
167209581Skib
168209581Skib			/*
169209581Skib			 * If we are deleting or renaming the entry, keep
170209581Skib			 * track of its tmpfs_dirent so that it can be
171209581Skib			 * easily deleted later.
172209581Skib			 */
173209581Skib			if ((cnp->cn_flags & ISLASTCN) &&
174209581Skib			    (cnp->cn_nameiop == DELETE ||
175209581Skib			    cnp->cn_nameiop == RENAME)) {
176209581Skib				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
177209581Skib				    cnp->cn_thread);
178209581Skib				if (error != 0)
179225618Skmacy					goto out;
180225618Skmacy
181209581Skib				/* Allocate a new vnode on the matching entry. */
182209581Skib				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
183209581Skib				    cnp->cn_lkflags, vpp);
184209581Skib				if (error != 0)
185209581Skib					goto out;
186209581Skib
187209581Skib				if ((dnode->tn_mode & S_ISTXT) &&
188209581Skib				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
189209581Skib				  cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
190209581Skib				  cnp->cn_cred, cnp->cn_thread)) {
191209581Skib					error = EPERM;
192209581Skib					vput(*vpp);
193209581Skib					*vpp = NULL;
194209581Skib					goto out;
195209581Skib				}
196209581Skib				cnp->cn_flags |= SAVENAME;
197209581Skib			} else {
198209581Skib				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
199209581Skib				    cnp->cn_lkflags, vpp);
200209581Skib				if (error != 0)
201209581Skib					goto out;
202209581Skib			}
203209581Skib		}
204209581Skib	}
205209581Skib
206209581Skib	/*
207209581Skib	 * Store the result of this lookup in the cache.  Avoid this if the
208209581Skib	 * request was for creation, as it does not improve timings on
209209581Skib	 * emprical tests.
210209581Skib	 */
211209581Skib	if ((cnp->cn_flags & MAKEENTRY) != 0)
212209581Skib		cache_enter(dvp, *vpp, cnp);
213209581Skib
214209581Skibout:
215209581Skib	/*
216209581Skib	 * If there were no errors, *vpp cannot be null and it must be
217209581Skib	 * locked.
218209581Skib	 */
219209581Skib	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
220209581Skib
221225618Skmacy	return (error);
222209581Skib}
223209581Skib
224225618Skmacystatic int
225225618Skmacytmpfs_create(struct vop_create_args *v)
226209581Skib{
227209581Skib	struct vnode *dvp = v->a_dvp;
228209581Skib	struct vnode **vpp = v->a_vpp;
229209581Skib	struct componentname *cnp = v->a_cnp;
230209581Skib	struct vattr *vap = v->a_vap;
231209581Skib	int error;
232209581Skib
233209581Skib	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
234209581Skib
235209581Skib	error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
236209581Skib	if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
237209581Skib		cache_enter(dvp, *vpp, cnp);
238209581Skib	return (error);
239209581Skib}
240209581Skib
241209581Skibstatic int
242209581Skibtmpfs_mknod(struct vop_mknod_args *v)
243209581Skib{
244209581Skib	struct vnode *dvp = v->a_dvp;
245209581Skib	struct vnode **vpp = v->a_vpp;
246209581Skib	struct componentname *cnp = v->a_cnp;
247209581Skib	struct vattr *vap = v->a_vap;
248209581Skib
249209581Skib	if (vap->va_type != VBLK && vap->va_type != VCHR &&
250209581Skib	    vap->va_type != VFIFO)
251209581Skib		return EINVAL;
252209581Skib
253225618Skmacy	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
254225618Skmacy}
255225618Skmacy
256225618Skmacystatic int
257225618Skmacytmpfs_open(struct vop_open_args *v)
258225618Skmacy{
259209581Skib	struct vnode *vp = v->a_vp;
260225618Skmacy	int mode = v->a_mode;
261225618Skmacy
262209581Skib	int error;
263225618Skmacy	struct tmpfs_node *node;
264209581Skib
265225618Skmacy	MPASS(VOP_ISLOCKED(vp));
266225618Skmacy
267225618Skmacy	node = VP_TO_TMPFS_NODE(vp);
268225618Skmacy
269209581Skib	/* The file is still active but all its names have been removed
270209581Skib	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
271209581Skib	 * it is about to die. */
27243412Snewton	if (node->tn_links < 1)
273		return (ENOENT);
274
275	/* If the file is marked append-only, deny write requests. */
276	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
277		error = EPERM;
278	else {
279		error = 0;
280		/* For regular files, the call below is nop. */
281		KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
282		    OBJ_DEAD) == 0, ("dead object"));
283		vnode_create_vobject(vp, node->tn_size, v->a_td);
284	}
285
286	MPASS(VOP_ISLOCKED(vp));
287	return error;
288}
289
290static int
291tmpfs_close(struct vop_close_args *v)
292{
293	struct vnode *vp = v->a_vp;
294
295	/* Update node times. */
296	tmpfs_update(vp);
297
298	return (0);
299}
300
301int
302tmpfs_access(struct vop_access_args *v)
303{
304	struct vnode *vp = v->a_vp;
305	accmode_t accmode = v->a_accmode;
306	struct ucred *cred = v->a_cred;
307
308	int error;
309	struct tmpfs_node *node;
310
311	MPASS(VOP_ISLOCKED(vp));
312
313	node = VP_TO_TMPFS_NODE(vp);
314
315	switch (vp->v_type) {
316	case VDIR:
317		/* FALLTHROUGH */
318	case VLNK:
319		/* FALLTHROUGH */
320	case VREG:
321		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
322			error = EROFS;
323			goto out;
324		}
325		break;
326
327	case VBLK:
328		/* FALLTHROUGH */
329	case VCHR:
330		/* FALLTHROUGH */
331	case VSOCK:
332		/* FALLTHROUGH */
333	case VFIFO:
334		break;
335
336	default:
337		error = EINVAL;
338		goto out;
339	}
340
341	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
342		error = EPERM;
343		goto out;
344	}
345
346	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
347	    node->tn_gid, accmode, cred, NULL);
348
349out:
350	MPASS(VOP_ISLOCKED(vp));
351
352	return error;
353}
354
355int
356tmpfs_getattr(struct vop_getattr_args *v)
357{
358	struct vnode *vp = v->a_vp;
359	struct vattr *vap = v->a_vap;
360
361	struct tmpfs_node *node;
362
363	node = VP_TO_TMPFS_NODE(vp);
364
365	tmpfs_update(vp);
366
367	vap->va_type = vp->v_type;
368	vap->va_mode = node->tn_mode;
369	vap->va_nlink = node->tn_links;
370	vap->va_uid = node->tn_uid;
371	vap->va_gid = node->tn_gid;
372	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
373	vap->va_fileid = node->tn_id;
374	vap->va_size = node->tn_size;
375	vap->va_blocksize = PAGE_SIZE;
376	vap->va_atime = node->tn_atime;
377	vap->va_mtime = node->tn_mtime;
378	vap->va_ctime = node->tn_ctime;
379	vap->va_birthtime = node->tn_birthtime;
380	vap->va_gen = node->tn_gen;
381	vap->va_flags = node->tn_flags;
382	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
383		node->tn_rdev : NODEV;
384	vap->va_bytes = round_page(node->tn_size);
385	vap->va_filerev = 0;
386
387	return 0;
388}
389
390int
391tmpfs_setattr(struct vop_setattr_args *v)
392{
393	struct vnode *vp = v->a_vp;
394	struct vattr *vap = v->a_vap;
395	struct ucred *cred = v->a_cred;
396	struct thread *td = curthread;
397
398	int error;
399
400	MPASS(VOP_ISLOCKED(vp));
401
402	error = 0;
403
404	/* Abort if any unsettable attribute is given. */
405	if (vap->va_type != VNON ||
406	    vap->va_nlink != VNOVAL ||
407	    vap->va_fsid != VNOVAL ||
408	    vap->va_fileid != VNOVAL ||
409	    vap->va_blocksize != VNOVAL ||
410	    vap->va_gen != VNOVAL ||
411	    vap->va_rdev != VNOVAL ||
412	    vap->va_bytes != VNOVAL)
413		error = EINVAL;
414
415	if (error == 0 && (vap->va_flags != VNOVAL))
416		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
417
418	if (error == 0 && (vap->va_size != VNOVAL))
419		error = tmpfs_chsize(vp, vap->va_size, cred, td);
420
421	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
422		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
423
424	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
425		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
426
427	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
428	    vap->va_atime.tv_nsec != VNOVAL) ||
429	    (vap->va_mtime.tv_sec != VNOVAL &&
430	    vap->va_mtime.tv_nsec != VNOVAL) ||
431	    (vap->va_birthtime.tv_sec != VNOVAL &&
432	    vap->va_birthtime.tv_nsec != VNOVAL)))
433		error = tmpfs_chtimes(vp, vap, cred, td);
434
435	/* Update the node times.  We give preference to the error codes
436	 * generated by this function rather than the ones that may arise
437	 * from tmpfs_update. */
438	tmpfs_update(vp);
439
440	MPASS(VOP_ISLOCKED(vp));
441
442	return error;
443}
444
445static int
446tmpfs_read(struct vop_read_args *v)
447{
448	struct vnode *vp;
449	struct uio *uio;
450	struct tmpfs_node *node;
451
452	vp = v->a_vp;
453	if (vp->v_type != VREG)
454		return (EISDIR);
455	uio = v->a_uio;
456	if (uio->uio_offset < 0)
457		return (EINVAL);
458	node = VP_TO_TMPFS_NODE(vp);
459	tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
460	return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
461}
462
463static int
464tmpfs_write(struct vop_write_args *v)
465{
466	struct vnode *vp;
467	struct uio *uio;
468	struct tmpfs_node *node;
469	off_t oldsize;
470	int error, ioflag;
471
472	vp = v->a_vp;
473	uio = v->a_uio;
474	ioflag = v->a_ioflag;
475	error = 0;
476	node = VP_TO_TMPFS_NODE(vp);
477	oldsize = node->tn_size;
478
479	if (uio->uio_offset < 0 || vp->v_type != VREG)
480		return (EINVAL);
481	if (uio->uio_resid == 0)
482		return (0);
483	if (ioflag & IO_APPEND)
484		uio->uio_offset = node->tn_size;
485	if (uio->uio_offset + uio->uio_resid >
486	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
487		return (EFBIG);
488	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
489		return (EFBIG);
490	if (uio->uio_offset + uio->uio_resid > node->tn_size) {
491		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
492		    FALSE);
493		if (error != 0)
494			goto out;
495	}
496
497	error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
498	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
499	    TMPFS_NODE_CHANGED;
500	if (node->tn_mode & (S_ISUID | S_ISGID)) {
501		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
502			node->tn_mode &= ~(S_ISUID | S_ISGID);
503	}
504	if (error != 0)
505		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
506
507out:
508	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
509	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
510
511	return (error);
512}
513
514static int
515tmpfs_fsync(struct vop_fsync_args *v)
516{
517	struct vnode *vp = v->a_vp;
518
519	MPASS(VOP_ISLOCKED(vp));
520
521	tmpfs_check_mtime(vp);
522	tmpfs_update(vp);
523
524	return 0;
525}
526
527static int
528tmpfs_remove(struct vop_remove_args *v)
529{
530	struct vnode *dvp = v->a_dvp;
531	struct vnode *vp = v->a_vp;
532
533	int error;
534	struct tmpfs_dirent *de;
535	struct tmpfs_mount *tmp;
536	struct tmpfs_node *dnode;
537	struct tmpfs_node *node;
538
539	MPASS(VOP_ISLOCKED(dvp));
540	MPASS(VOP_ISLOCKED(vp));
541
542	if (vp->v_type == VDIR) {
543		error = EISDIR;
544		goto out;
545	}
546
547	dnode = VP_TO_TMPFS_DIR(dvp);
548	node = VP_TO_TMPFS_NODE(vp);
549	tmp = VFS_TO_TMPFS(vp->v_mount);
550	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
551	MPASS(de != NULL);
552
553	/* Files marked as immutable or append-only cannot be deleted. */
554	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
555	    (dnode->tn_flags & APPEND)) {
556		error = EPERM;
557		goto out;
558	}
559
560	/* Remove the entry from the directory; as it is a file, we do not
561	 * have to change the number of hard links of the directory. */
562	tmpfs_dir_detach(dvp, de);
563	if (v->a_cnp->cn_flags & DOWHITEOUT)
564		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
565
566	/* Free the directory entry we just deleted.  Note that the node
567	 * referred by it will not be removed until the vnode is really
568	 * reclaimed. */
569	tmpfs_free_dirent(tmp, de);
570
571	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
572	error = 0;
573
574out:
575
576	return error;
577}
578
579static int
580tmpfs_link(struct vop_link_args *v)
581{
582	struct vnode *dvp = v->a_tdvp;
583	struct vnode *vp = v->a_vp;
584	struct componentname *cnp = v->a_cnp;
585
586	int error;
587	struct tmpfs_dirent *de;
588	struct tmpfs_node *node;
589
590	MPASS(VOP_ISLOCKED(dvp));
591	MPASS(cnp->cn_flags & HASBUF);
592	MPASS(dvp != vp); /* XXX When can this be false? */
593	node = VP_TO_TMPFS_NODE(vp);
594
595	/* Ensure that we do not overflow the maximum number of links imposed
596	 * by the system. */
597	MPASS(node->tn_links <= LINK_MAX);
598	if (node->tn_links == LINK_MAX) {
599		error = EMLINK;
600		goto out;
601	}
602
603	/* We cannot create links of files marked immutable or append-only. */
604	if (node->tn_flags & (IMMUTABLE | APPEND)) {
605		error = EPERM;
606		goto out;
607	}
608
609	/* Allocate a new directory entry to represent the node. */
610	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
611	    cnp->cn_nameptr, cnp->cn_namelen, &de);
612	if (error != 0)
613		goto out;
614
615	/* Insert the new directory entry into the appropriate directory. */
616	if (cnp->cn_flags & ISWHITEOUT)
617		tmpfs_dir_whiteout_remove(dvp, cnp);
618	tmpfs_dir_attach(dvp, de);
619
620	/* vp link count has changed, so update node times. */
621	node->tn_status |= TMPFS_NODE_CHANGED;
622	tmpfs_update(vp);
623
624	error = 0;
625
626out:
627	return error;
628}
629
630/*
631 * We acquire all but fdvp locks using non-blocking acquisitions.  If we
632 * fail to acquire any lock in the path we will drop all held locks,
633 * acquire the new lock in a blocking fashion, and then release it and
634 * restart the rename.  This acquire/release step ensures that we do not
635 * spin on a lock waiting for release.  On error release all vnode locks
636 * and decrement references the way tmpfs_rename() would do.
637 */
638static int
639tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
640    struct vnode *tdvp, struct vnode **tvpp,
641    struct componentname *fcnp, struct componentname *tcnp)
642{
643	struct vnode *nvp;
644	struct mount *mp;
645	struct tmpfs_dirent *de;
646	int error, restarts = 0;
647
648	VOP_UNLOCK(tdvp, 0);
649	if (*tvpp != NULL && *tvpp != tdvp)
650		VOP_UNLOCK(*tvpp, 0);
651	mp = fdvp->v_mount;
652
653relock:
654	restarts += 1;
655	error = vn_lock(fdvp, LK_EXCLUSIVE);
656	if (error)
657		goto releout;
658	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
659		VOP_UNLOCK(fdvp, 0);
660		error = vn_lock(tdvp, LK_EXCLUSIVE);
661		if (error)
662			goto releout;
663		VOP_UNLOCK(tdvp, 0);
664		goto relock;
665	}
666	/*
667	 * Re-resolve fvp to be certain it still exists and fetch the
668	 * correct vnode.
669	 */
670	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
671	if (de == NULL) {
672		VOP_UNLOCK(fdvp, 0);
673		VOP_UNLOCK(tdvp, 0);
674		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
675		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
676			error = EINVAL;
677		else
678			error = ENOENT;
679		goto releout;
680	}
681	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
682	if (error != 0) {
683		VOP_UNLOCK(fdvp, 0);
684		VOP_UNLOCK(tdvp, 0);
685		if (error != EBUSY)
686			goto releout;
687		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
688		if (error != 0)
689			goto releout;
690		VOP_UNLOCK(nvp, 0);
691		/*
692		 * Concurrent rename race.
693		 */
694		if (nvp == tdvp) {
695			vrele(nvp);
696			error = EINVAL;
697			goto releout;
698		}
699		vrele(*fvpp);
700		*fvpp = nvp;
701		goto relock;
702	}
703	vrele(*fvpp);
704	*fvpp = nvp;
705	VOP_UNLOCK(*fvpp, 0);
706	/*
707	 * Re-resolve tvp and acquire the vnode lock if present.
708	 */
709	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
710	/*
711	 * If tvp disappeared we just carry on.
712	 */
713	if (de == NULL && *tvpp != NULL) {
714		vrele(*tvpp);
715		*tvpp = NULL;
716	}
717	/*
718	 * Get the tvp ino if the lookup succeeded.  We may have to restart
719	 * if the non-blocking acquire fails.
720	 */
721	if (de != NULL) {
722		nvp = NULL;
723		error = tmpfs_alloc_vp(mp, de->td_node,
724		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
725		if (*tvpp != NULL)
726			vrele(*tvpp);
727		*tvpp = nvp;
728		if (error != 0) {
729			VOP_UNLOCK(fdvp, 0);
730			VOP_UNLOCK(tdvp, 0);
731			if (error != EBUSY)
732				goto releout;
733			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
734			    &nvp);
735			if (error != 0)
736				goto releout;
737			VOP_UNLOCK(nvp, 0);
738			/*
739			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
740			 */
741			if (nvp == fdvp) {
742				error = ENOTEMPTY;
743				goto releout;
744			}
745			goto relock;
746		}
747	}
748	tmpfs_rename_restarts += restarts;
749
750	return (0);
751
752releout:
753	vrele(fdvp);
754	vrele(*fvpp);
755	vrele(tdvp);
756	if (*tvpp != NULL)
757		vrele(*tvpp);
758	tmpfs_rename_restarts += restarts;
759
760	return (error);
761}
762
763static int
764tmpfs_rename(struct vop_rename_args *v)
765{
766	struct vnode *fdvp = v->a_fdvp;
767	struct vnode *fvp = v->a_fvp;
768	struct componentname *fcnp = v->a_fcnp;
769	struct vnode *tdvp = v->a_tdvp;
770	struct vnode *tvp = v->a_tvp;
771	struct componentname *tcnp = v->a_tcnp;
772	struct mount *mp = NULL;
773
774	char *newname;
775	int error;
776	struct tmpfs_dirent *de;
777	struct tmpfs_mount *tmp;
778	struct tmpfs_node *fdnode;
779	struct tmpfs_node *fnode;
780	struct tmpfs_node *tnode;
781	struct tmpfs_node *tdnode;
782
783	MPASS(VOP_ISLOCKED(tdvp));
784	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
785	MPASS(fcnp->cn_flags & HASBUF);
786	MPASS(tcnp->cn_flags & HASBUF);
787
788	/* Disallow cross-device renames.
789	 * XXX Why isn't this done by the caller? */
790	if (fvp->v_mount != tdvp->v_mount ||
791	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
792		error = EXDEV;
793		goto out;
794	}
795
796	/* If source and target are the same file, there is nothing to do. */
797	if (fvp == tvp) {
798		error = 0;
799		goto out;
800	}
801
802	/* If we need to move the directory between entries, lock the
803	 * source so that we can safely operate on it. */
804	if (fdvp != tdvp && fdvp != tvp) {
805		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
806			mp = tdvp->v_mount;
807			error = vfs_busy(mp, 0);
808			if (error != 0) {
809				mp = NULL;
810				goto out;
811			}
812			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
813			    fcnp, tcnp);
814			if (error != 0) {
815				vfs_unbusy(mp);
816				return (error);
817			}
818			ASSERT_VOP_ELOCKED(fdvp,
819			    "tmpfs_rename: fdvp not locked");
820			ASSERT_VOP_ELOCKED(tdvp,
821			    "tmpfs_rename: tdvp not locked");
822			if (tvp != NULL)
823				ASSERT_VOP_ELOCKED(tvp,
824				    "tmpfs_rename: tvp not locked");
825			if (fvp == tvp) {
826				error = 0;
827				goto out_locked;
828			}
829		}
830	}
831
832	tmp = VFS_TO_TMPFS(tdvp->v_mount);
833	tdnode = VP_TO_TMPFS_DIR(tdvp);
834	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
835	fdnode = VP_TO_TMPFS_DIR(fdvp);
836	fnode = VP_TO_TMPFS_NODE(fvp);
837	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
838
839	/* Entry can disappear before we lock fdvp,
840	 * also avoid manipulating '.' and '..' entries. */
841	if (de == NULL) {
842		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
843		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
844			error = EINVAL;
845		else
846			error = ENOENT;
847		goto out_locked;
848	}
849	MPASS(de->td_node == fnode);
850
851	/* If re-naming a directory to another preexisting directory
852	 * ensure that the target directory is empty so that its
853	 * removal causes no side effects.
854	 * Kern_rename gurantees the destination to be a directory
855	 * if the source is one. */
856	if (tvp != NULL) {
857		MPASS(tnode != NULL);
858
859		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
860		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
861			error = EPERM;
862			goto out_locked;
863		}
864
865		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
866			if (tnode->tn_size > 0) {
867				error = ENOTEMPTY;
868				goto out_locked;
869			}
870		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
871			error = ENOTDIR;
872			goto out_locked;
873		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
874			error = EISDIR;
875			goto out_locked;
876		} else {
877			MPASS(fnode->tn_type != VDIR &&
878				tnode->tn_type != VDIR);
879		}
880	}
881
882	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
883	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
884		error = EPERM;
885		goto out_locked;
886	}
887
888	/* Ensure that we have enough memory to hold the new name, if it
889	 * has to be changed. */
890	if (fcnp->cn_namelen != tcnp->cn_namelen ||
891	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
892		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
893	} else
894		newname = NULL;
895
896	/* If the node is being moved to another directory, we have to do
897	 * the move. */
898	if (fdnode != tdnode) {
899		/* In case we are moving a directory, we have to adjust its
900		 * parent to point to the new parent. */
901		if (de->td_node->tn_type == VDIR) {
902			struct tmpfs_node *n;
903
904			/* Ensure the target directory is not a child of the
905			 * directory being moved.  Otherwise, we'd end up
906			 * with stale nodes. */
907			n = tdnode;
908			/* TMPFS_LOCK garanties that no nodes are freed while
909			 * traversing the list. Nodes can only be marked as
910			 * removed: tn_parent == NULL. */
911			TMPFS_LOCK(tmp);
912			TMPFS_NODE_LOCK(n);
913			while (n != n->tn_dir.tn_parent) {
914				struct tmpfs_node *parent;
915
916				if (n == fnode) {
917					TMPFS_NODE_UNLOCK(n);
918					TMPFS_UNLOCK(tmp);
919					error = EINVAL;
920					if (newname != NULL)
921						    free(newname, M_TMPFSNAME);
922					goto out_locked;
923				}
924				parent = n->tn_dir.tn_parent;
925				TMPFS_NODE_UNLOCK(n);
926				if (parent == NULL) {
927					n = NULL;
928					break;
929				}
930				TMPFS_NODE_LOCK(parent);
931				if (parent->tn_dir.tn_parent == NULL) {
932					TMPFS_NODE_UNLOCK(parent);
933					n = NULL;
934					break;
935				}
936				n = parent;
937			}
938			TMPFS_UNLOCK(tmp);
939			if (n == NULL) {
940				error = EINVAL;
941				if (newname != NULL)
942					    free(newname, M_TMPFSNAME);
943				goto out_locked;
944			}
945			TMPFS_NODE_UNLOCK(n);
946
947			/* Adjust the parent pointer. */
948			TMPFS_VALIDATE_DIR(fnode);
949			TMPFS_NODE_LOCK(de->td_node);
950			de->td_node->tn_dir.tn_parent = tdnode;
951			TMPFS_NODE_UNLOCK(de->td_node);
952
953			/* As a result of changing the target of the '..'
954			 * entry, the link count of the source and target
955			 * directories has to be adjusted. */
956			TMPFS_NODE_LOCK(tdnode);
957			TMPFS_ASSERT_LOCKED(tdnode);
958			tdnode->tn_links++;
959			TMPFS_NODE_UNLOCK(tdnode);
960
961			TMPFS_NODE_LOCK(fdnode);
962			TMPFS_ASSERT_LOCKED(fdnode);
963			fdnode->tn_links--;
964			TMPFS_NODE_UNLOCK(fdnode);
965		}
966	}
967
968	/* Do the move: just remove the entry from the source directory
969	 * and insert it into the target one. */
970	tmpfs_dir_detach(fdvp, de);
971
972	if (fcnp->cn_flags & DOWHITEOUT)
973		tmpfs_dir_whiteout_add(fdvp, fcnp);
974	if (tcnp->cn_flags & ISWHITEOUT)
975		tmpfs_dir_whiteout_remove(tdvp, tcnp);
976
977	/* If the name has changed, we need to make it effective by changing
978	 * it in the directory entry. */
979	if (newname != NULL) {
980		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
981
982		free(de->ud.td_name, M_TMPFSNAME);
983		de->ud.td_name = newname;
984		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
985
986		fnode->tn_status |= TMPFS_NODE_CHANGED;
987		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
988	}
989
990	/* If we are overwriting an entry, we have to remove the old one
991	 * from the target directory. */
992	if (tvp != NULL) {
993		struct tmpfs_dirent *tde;
994
995		/* Remove the old entry from the target directory. */
996		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
997		tmpfs_dir_detach(tdvp, tde);
998
999		/* Free the directory entry we just deleted.  Note that the
1000		 * node referred by it will not be removed until the vnode is
1001		 * really reclaimed. */
1002		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1003	}
1004
1005	tmpfs_dir_attach(tdvp, de);
1006
1007	cache_purge(fvp);
1008	if (tvp != NULL)
1009		cache_purge(tvp);
1010	cache_purge_negative(tdvp);
1011
1012	error = 0;
1013
1014out_locked:
1015	if (fdvp != tdvp && fdvp != tvp)
1016		VOP_UNLOCK(fdvp, 0);
1017
1018out:
1019	/* Release target nodes. */
1020	/* XXX: I don't understand when tdvp can be the same as tvp, but
1021	 * other code takes care of this... */
1022	if (tdvp == tvp)
1023		vrele(tdvp);
1024	else
1025		vput(tdvp);
1026	if (tvp != NULL)
1027		vput(tvp);
1028
1029	/* Release source nodes. */
1030	vrele(fdvp);
1031	vrele(fvp);
1032
1033	if (mp != NULL)
1034		vfs_unbusy(mp);
1035
1036	return error;
1037}
1038
1039static int
1040tmpfs_mkdir(struct vop_mkdir_args *v)
1041{
1042	struct vnode *dvp = v->a_dvp;
1043	struct vnode **vpp = v->a_vpp;
1044	struct componentname *cnp = v->a_cnp;
1045	struct vattr *vap = v->a_vap;
1046
1047	MPASS(vap->va_type == VDIR);
1048
1049	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1050}
1051
1052static int
1053tmpfs_rmdir(struct vop_rmdir_args *v)
1054{
1055	struct vnode *dvp = v->a_dvp;
1056	struct vnode *vp = v->a_vp;
1057
1058	int error;
1059	struct tmpfs_dirent *de;
1060	struct tmpfs_mount *tmp;
1061	struct tmpfs_node *dnode;
1062	struct tmpfs_node *node;
1063
1064	MPASS(VOP_ISLOCKED(dvp));
1065	MPASS(VOP_ISLOCKED(vp));
1066
1067	tmp = VFS_TO_TMPFS(dvp->v_mount);
1068	dnode = VP_TO_TMPFS_DIR(dvp);
1069	node = VP_TO_TMPFS_DIR(vp);
1070
1071	/* Directories with more than two entries ('.' and '..') cannot be
1072	 * removed. */
1073	 if (node->tn_size > 0) {
1074		 error = ENOTEMPTY;
1075		 goto out;
1076	 }
1077
1078	if ((dnode->tn_flags & APPEND)
1079	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1080		error = EPERM;
1081		goto out;
1082	}
1083
1084	/* This invariant holds only if we are not trying to remove "..".
1085	  * We checked for that above so this is safe now. */
1086	MPASS(node->tn_dir.tn_parent == dnode);
1087
1088	/* Get the directory entry associated with node (vp).  This was
1089	 * filled by tmpfs_lookup while looking up the entry. */
1090	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1091	MPASS(TMPFS_DIRENT_MATCHES(de,
1092	    v->a_cnp->cn_nameptr,
1093	    v->a_cnp->cn_namelen));
1094
1095	/* Check flags to see if we are allowed to remove the directory. */
1096	if ((dnode->tn_flags & APPEND) != 0 ||
1097	    (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1098		error = EPERM;
1099		goto out;
1100	}
1101
1102
1103	/* Detach the directory entry from the directory (dnode). */
1104	tmpfs_dir_detach(dvp, de);
1105	if (v->a_cnp->cn_flags & DOWHITEOUT)
1106		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1107
1108	/* No vnode should be allocated for this entry from this point */
1109	TMPFS_NODE_LOCK(node);
1110	TMPFS_ASSERT_ELOCKED(node);
1111	node->tn_links--;
1112	node->tn_dir.tn_parent = NULL;
1113	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1114	    TMPFS_NODE_MODIFIED;
1115
1116	TMPFS_NODE_UNLOCK(node);
1117
1118	TMPFS_NODE_LOCK(dnode);
1119	TMPFS_ASSERT_ELOCKED(dnode);
1120	dnode->tn_links--;
1121	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1122	    TMPFS_NODE_MODIFIED;
1123	TMPFS_NODE_UNLOCK(dnode);
1124
1125	cache_purge(dvp);
1126	cache_purge(vp);
1127
1128	/* Free the directory entry we just deleted.  Note that the node
1129	 * referred by it will not be removed until the vnode is really
1130	 * reclaimed. */
1131	tmpfs_free_dirent(tmp, de);
1132
1133	/* Release the deleted vnode (will destroy the node, notify
1134	 * interested parties and clean it from the cache). */
1135
1136	dnode->tn_status |= TMPFS_NODE_CHANGED;
1137	tmpfs_update(dvp);
1138
1139	error = 0;
1140
1141out:
1142	return error;
1143}
1144
1145static int
1146tmpfs_symlink(struct vop_symlink_args *v)
1147{
1148	struct vnode *dvp = v->a_dvp;
1149	struct vnode **vpp = v->a_vpp;
1150	struct componentname *cnp = v->a_cnp;
1151	struct vattr *vap = v->a_vap;
1152	char *target = v->a_target;
1153
1154#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1155	MPASS(vap->va_type == VLNK);
1156#else
1157	vap->va_type = VLNK;
1158#endif
1159
1160	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1161}
1162
1163static int
1164tmpfs_readdir(struct vop_readdir_args *v)
1165{
1166	struct vnode *vp = v->a_vp;
1167	struct uio *uio = v->a_uio;
1168	int *eofflag = v->a_eofflag;
1169	u_long **cookies = v->a_cookies;
1170	int *ncookies = v->a_ncookies;
1171
1172	int error;
1173	ssize_t startresid;
1174	int maxcookies;
1175	struct tmpfs_node *node;
1176
1177	/* This operation only makes sense on directory nodes. */
1178	if (vp->v_type != VDIR)
1179		return ENOTDIR;
1180
1181	maxcookies = 0;
1182	node = VP_TO_TMPFS_DIR(vp);
1183
1184	startresid = uio->uio_resid;
1185
1186	/* Allocate cookies for NFS and compat modules. */
1187	if (cookies != NULL && ncookies != NULL) {
1188		maxcookies = howmany(node->tn_size,
1189		    sizeof(struct tmpfs_dirent)) + 2;
1190		*cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1191		    M_WAITOK);
1192		*ncookies = 0;
1193	}
1194
1195	if (cookies == NULL)
1196		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1197	else
1198		error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1199		    ncookies);
1200
1201	/* Buffer was filled without hitting EOF. */
1202	if (error == EJUSTRETURN)
1203		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1204
1205	if (error != 0 && cookies != NULL && ncookies != NULL) {
1206		free(*cookies, M_TEMP);
1207		*cookies = NULL;
1208		*ncookies = 0;
1209	}
1210
1211	if (eofflag != NULL)
1212		*eofflag =
1213		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1214
1215	return error;
1216}
1217
1218static int
1219tmpfs_readlink(struct vop_readlink_args *v)
1220{
1221	struct vnode *vp = v->a_vp;
1222	struct uio *uio = v->a_uio;
1223
1224	int error;
1225	struct tmpfs_node *node;
1226
1227	MPASS(uio->uio_offset == 0);
1228	MPASS(vp->v_type == VLNK);
1229
1230	node = VP_TO_TMPFS_NODE(vp);
1231
1232	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1233	    uio);
1234	tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
1235
1236	return (error);
1237}
1238
1239static int
1240tmpfs_inactive(struct vop_inactive_args *v)
1241{
1242	struct vnode *vp;
1243	struct tmpfs_node *node;
1244
1245	vp = v->a_vp;
1246	node = VP_TO_TMPFS_NODE(vp);
1247	if (node->tn_links == 0)
1248		vrecycle(vp);
1249	else
1250		tmpfs_check_mtime(vp);
1251	return (0);
1252}
1253
1254int
1255tmpfs_reclaim(struct vop_reclaim_args *v)
1256{
1257	struct vnode *vp = v->a_vp;
1258
1259	struct tmpfs_mount *tmp;
1260	struct tmpfs_node *node;
1261
1262	node = VP_TO_TMPFS_NODE(vp);
1263	tmp = VFS_TO_TMPFS(vp->v_mount);
1264
1265	if (vp->v_type == VREG)
1266		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1267	else
1268		vnode_destroy_vobject(vp);
1269	vp->v_object = NULL;
1270	cache_purge(vp);
1271
1272	TMPFS_NODE_LOCK(node);
1273	TMPFS_ASSERT_ELOCKED(node);
1274	tmpfs_free_vp(vp);
1275
1276	/* If the node referenced by this vnode was deleted by the user,
1277	 * we must free its associated data structures (now that the vnode
1278	 * is being reclaimed). */
1279	if (node->tn_links == 0 &&
1280	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1281		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1282		TMPFS_NODE_UNLOCK(node);
1283		tmpfs_free_node(tmp, node);
1284	} else
1285		TMPFS_NODE_UNLOCK(node);
1286
1287	MPASS(vp->v_data == NULL);
1288	return 0;
1289}
1290
1291static int
1292tmpfs_print(struct vop_print_args *v)
1293{
1294	struct vnode *vp = v->a_vp;
1295
1296	struct tmpfs_node *node;
1297
1298	node = VP_TO_TMPFS_NODE(vp);
1299
1300	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1301	    node, node->tn_flags, node->tn_links);
1302	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1303	    node->tn_mode, node->tn_uid, node->tn_gid,
1304	    (intmax_t)node->tn_size, node->tn_status);
1305
1306	if (vp->v_type == VFIFO)
1307		fifo_printinfo(vp);
1308
1309	printf("\n");
1310
1311	return 0;
1312}
1313
1314static int
1315tmpfs_pathconf(struct vop_pathconf_args *v)
1316{
1317	int name = v->a_name;
1318	register_t *retval = v->a_retval;
1319
1320	int error;
1321
1322	error = 0;
1323
1324	switch (name) {
1325	case _PC_LINK_MAX:
1326		*retval = LINK_MAX;
1327		break;
1328
1329	case _PC_NAME_MAX:
1330		*retval = NAME_MAX;
1331		break;
1332
1333	case _PC_PATH_MAX:
1334		*retval = PATH_MAX;
1335		break;
1336
1337	case _PC_PIPE_BUF:
1338		*retval = PIPE_BUF;
1339		break;
1340
1341	case _PC_CHOWN_RESTRICTED:
1342		*retval = 1;
1343		break;
1344
1345	case _PC_NO_TRUNC:
1346		*retval = 1;
1347		break;
1348
1349	case _PC_SYNC_IO:
1350		*retval = 1;
1351		break;
1352
1353	case _PC_FILESIZEBITS:
1354		*retval = 0; /* XXX Don't know which value should I return. */
1355		break;
1356
1357	default:
1358		error = EINVAL;
1359	}
1360
1361	return error;
1362}
1363
1364static int
1365tmpfs_vptofh(struct vop_vptofh_args *ap)
1366{
1367	struct tmpfs_fid *tfhp;
1368	struct tmpfs_node *node;
1369
1370	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1371	node = VP_TO_TMPFS_NODE(ap->a_vp);
1372
1373	tfhp->tf_len = sizeof(struct tmpfs_fid);
1374	tfhp->tf_id = node->tn_id;
1375	tfhp->tf_gen = node->tn_gen;
1376
1377	return (0);
1378}
1379
1380static int
1381tmpfs_whiteout(struct vop_whiteout_args *ap)
1382{
1383	struct vnode *dvp = ap->a_dvp;
1384	struct componentname *cnp = ap->a_cnp;
1385	struct tmpfs_dirent *de;
1386
1387	switch (ap->a_flags) {
1388	case LOOKUP:
1389		return (0);
1390	case CREATE:
1391		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1392		if (de != NULL)
1393			return (de->td_node == NULL ? 0 : EEXIST);
1394		return (tmpfs_dir_whiteout_add(dvp, cnp));
1395	case DELETE:
1396		tmpfs_dir_whiteout_remove(dvp, cnp);
1397		return (0);
1398	default:
1399		panic("tmpfs_whiteout: unknown op");
1400	}
1401}
1402
1403/*
1404 * Vnode operations vector used for files stored in a tmpfs file system.
1405 */
1406struct vop_vector tmpfs_vnodeop_entries = {
1407	.vop_default =			&default_vnodeops,
1408	.vop_lookup =			vfs_cache_lookup,
1409	.vop_cachedlookup =		tmpfs_lookup,
1410	.vop_create =			tmpfs_create,
1411	.vop_mknod =			tmpfs_mknod,
1412	.vop_open =			tmpfs_open,
1413	.vop_close =			tmpfs_close,
1414	.vop_access =			tmpfs_access,
1415	.vop_getattr =			tmpfs_getattr,
1416	.vop_setattr =			tmpfs_setattr,
1417	.vop_read =			tmpfs_read,
1418	.vop_write =			tmpfs_write,
1419	.vop_fsync =			tmpfs_fsync,
1420	.vop_remove =			tmpfs_remove,
1421	.vop_link =			tmpfs_link,
1422	.vop_rename =			tmpfs_rename,
1423	.vop_mkdir =			tmpfs_mkdir,
1424	.vop_rmdir =			tmpfs_rmdir,
1425	.vop_symlink =			tmpfs_symlink,
1426	.vop_readdir =			tmpfs_readdir,
1427	.vop_readlink =			tmpfs_readlink,
1428	.vop_inactive =			tmpfs_inactive,
1429	.vop_reclaim =			tmpfs_reclaim,
1430	.vop_print =			tmpfs_print,
1431	.vop_pathconf =			tmpfs_pathconf,
1432	.vop_vptofh =			tmpfs_vptofh,
1433	.vop_whiteout =			tmpfs_whiteout,
1434	.vop_bmap =			VOP_EOPNOTSUPP,
1435};
1436
1437