tmpfs_vnops.c revision 250030
1/*	$NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $	*/
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 250030 2013-04-28 19:38:59Z kib $");
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/lock.h>
43#include <sys/namei.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/rwlock.h>
47#include <sys/sched.h>
48#include <sys/sf_buf.h>
49#include <sys/stat.h>
50#include <sys/systm.h>
51#include <sys/sysctl.h>
52#include <sys/unistd.h>
53#include <sys/vnode.h>
54
55#include <vm/vm.h>
56#include <vm/vm_param.h>
57#include <vm/vm_object.h>
58#include <vm/vm_page.h>
59#include <vm/vm_pager.h>
60
61#include <fs/tmpfs/tmpfs_vnops.h>
62#include <fs/tmpfs/tmpfs.h>
63
64SYSCTL_DECL(_vfs_tmpfs);
65
66static volatile int tmpfs_rename_restarts;
67SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
68    __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
69    "Times rename had to restart due to lock contention");
70
71/* --------------------------------------------------------------------- */
72
73static int
74tmpfs_lookup(struct vop_cachedlookup_args *v)
75{
76	struct vnode *dvp = v->a_dvp;
77	struct vnode **vpp = v->a_vpp;
78	struct componentname *cnp = v->a_cnp;
79
80	int error;
81	struct tmpfs_dirent *de;
82	struct tmpfs_node *dnode;
83
84	dnode = VP_TO_TMPFS_DIR(dvp);
85	*vpp = NULLVP;
86
87	/* Check accessibility of requested node as a first step. */
88	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
89	if (error != 0)
90		goto out;
91
92	/* We cannot be requesting the parent directory of the root node. */
93	MPASS(IMPLIES(dnode->tn_type == VDIR &&
94	    dnode->tn_dir.tn_parent == dnode,
95	    !(cnp->cn_flags & ISDOTDOT)));
96
97	TMPFS_ASSERT_LOCKED(dnode);
98	if (dnode->tn_dir.tn_parent == NULL) {
99		error = ENOENT;
100		goto out;
101	}
102	if (cnp->cn_flags & ISDOTDOT) {
103		int ltype = 0;
104
105		ltype = VOP_ISLOCKED(dvp);
106		vhold(dvp);
107		VOP_UNLOCK(dvp, 0);
108		/* Allocate a new vnode on the matching entry. */
109		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
110		    cnp->cn_lkflags, vpp);
111
112		vn_lock(dvp, ltype | LK_RETRY);
113		vdrop(dvp);
114	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
115		VREF(dvp);
116		*vpp = dvp;
117		error = 0;
118	} else {
119		de = tmpfs_dir_lookup(dnode, NULL, cnp);
120		if (de != NULL && de->td_node == NULL)
121			cnp->cn_flags |= ISWHITEOUT;
122		if (de == NULL || de->td_node == NULL) {
123			/* The entry was not found in the directory.
124			 * This is OK if we are creating or renaming an
125			 * entry and are working on the last component of
126			 * the path name. */
127			if ((cnp->cn_flags & ISLASTCN) &&
128			    (cnp->cn_nameiop == CREATE || \
129			    cnp->cn_nameiop == RENAME ||
130			    (cnp->cn_nameiop == DELETE &&
131			    cnp->cn_flags & DOWHITEOUT &&
132			    cnp->cn_flags & ISWHITEOUT))) {
133				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
134				    cnp->cn_thread);
135				if (error != 0)
136					goto out;
137
138				/* Keep the component name in the buffer for
139				 * future uses. */
140				cnp->cn_flags |= SAVENAME;
141
142				error = EJUSTRETURN;
143			} else
144				error = ENOENT;
145		} else {
146			struct tmpfs_node *tnode;
147
148			/* The entry was found, so get its associated
149			 * tmpfs_node. */
150			tnode = de->td_node;
151
152			/* If we are not at the last path component and
153			 * found a non-directory or non-link entry (which
154			 * may itself be pointing to a directory), raise
155			 * an error. */
156			if ((tnode->tn_type != VDIR &&
157			    tnode->tn_type != VLNK) &&
158			    !(cnp->cn_flags & ISLASTCN)) {
159				error = ENOTDIR;
160				goto out;
161			}
162
163			/* If we are deleting or renaming the entry, keep
164			 * track of its tmpfs_dirent so that it can be
165			 * easily deleted later. */
166			if ((cnp->cn_flags & ISLASTCN) &&
167			    (cnp->cn_nameiop == DELETE ||
168			    cnp->cn_nameiop == RENAME)) {
169				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
170				    cnp->cn_thread);
171				if (error != 0)
172					goto out;
173
174				/* Allocate a new vnode on the matching entry. */
175				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
176						cnp->cn_lkflags, vpp);
177				if (error != 0)
178					goto out;
179
180				if ((dnode->tn_mode & S_ISTXT) &&
181				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
182				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
183					error = EPERM;
184					vput(*vpp);
185					*vpp = NULL;
186					goto out;
187				}
188				cnp->cn_flags |= SAVENAME;
189			} else {
190				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
191						cnp->cn_lkflags, vpp);
192			}
193		}
194	}
195
196	/* Store the result of this lookup in the cache.  Avoid this if the
197	 * request was for creation, as it does not improve timings on
198	 * emprical tests. */
199	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
200		cache_enter(dvp, *vpp, cnp);
201
202out:
203	/* If there were no errors, *vpp cannot be null and it must be
204	 * locked. */
205	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
206
207	return error;
208}
209
210/* --------------------------------------------------------------------- */
211
212static int
213tmpfs_create(struct vop_create_args *v)
214{
215	struct vnode *dvp = v->a_dvp;
216	struct vnode **vpp = v->a_vpp;
217	struct componentname *cnp = v->a_cnp;
218	struct vattr *vap = v->a_vap;
219
220	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
221
222	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
223}
224/* --------------------------------------------------------------------- */
225
226static int
227tmpfs_mknod(struct vop_mknod_args *v)
228{
229	struct vnode *dvp = v->a_dvp;
230	struct vnode **vpp = v->a_vpp;
231	struct componentname *cnp = v->a_cnp;
232	struct vattr *vap = v->a_vap;
233
234	if (vap->va_type != VBLK && vap->va_type != VCHR &&
235	    vap->va_type != VFIFO)
236		return EINVAL;
237
238	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
239}
240
241/* --------------------------------------------------------------------- */
242
243static int
244tmpfs_open(struct vop_open_args *v)
245{
246	struct vnode *vp = v->a_vp;
247	int mode = v->a_mode;
248
249	int error;
250	struct tmpfs_node *node;
251
252	MPASS(VOP_ISLOCKED(vp));
253
254	node = VP_TO_TMPFS_NODE(vp);
255
256	/* The file is still active but all its names have been removed
257	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
258	 * it is about to die. */
259	if (node->tn_links < 1)
260		return (ENOENT);
261
262	/* If the file is marked append-only, deny write requests. */
263	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
264		error = EPERM;
265	else {
266		error = 0;
267		vnode_create_vobject(vp, node->tn_size, v->a_td);
268	}
269
270	MPASS(VOP_ISLOCKED(vp));
271	return error;
272}
273
274/* --------------------------------------------------------------------- */
275
276static int
277tmpfs_close(struct vop_close_args *v)
278{
279	struct vnode *vp = v->a_vp;
280
281	/* Update node times. */
282	tmpfs_update(vp);
283
284	return (0);
285}
286
287/* --------------------------------------------------------------------- */
288
289int
290tmpfs_access(struct vop_access_args *v)
291{
292	struct vnode *vp = v->a_vp;
293	accmode_t accmode = v->a_accmode;
294	struct ucred *cred = v->a_cred;
295
296	int error;
297	struct tmpfs_node *node;
298
299	MPASS(VOP_ISLOCKED(vp));
300
301	node = VP_TO_TMPFS_NODE(vp);
302
303	switch (vp->v_type) {
304	case VDIR:
305		/* FALLTHROUGH */
306	case VLNK:
307		/* FALLTHROUGH */
308	case VREG:
309		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
310			error = EROFS;
311			goto out;
312		}
313		break;
314
315	case VBLK:
316		/* FALLTHROUGH */
317	case VCHR:
318		/* FALLTHROUGH */
319	case VSOCK:
320		/* FALLTHROUGH */
321	case VFIFO:
322		break;
323
324	default:
325		error = EINVAL;
326		goto out;
327	}
328
329	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
330		error = EPERM;
331		goto out;
332	}
333
334	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
335	    node->tn_gid, accmode, cred, NULL);
336
337out:
338	MPASS(VOP_ISLOCKED(vp));
339
340	return error;
341}
342
343/* --------------------------------------------------------------------- */
344
345int
346tmpfs_getattr(struct vop_getattr_args *v)
347{
348	struct vnode *vp = v->a_vp;
349	struct vattr *vap = v->a_vap;
350
351	struct tmpfs_node *node;
352
353	node = VP_TO_TMPFS_NODE(vp);
354
355	tmpfs_update(vp);
356
357	vap->va_type = vp->v_type;
358	vap->va_mode = node->tn_mode;
359	vap->va_nlink = node->tn_links;
360	vap->va_uid = node->tn_uid;
361	vap->va_gid = node->tn_gid;
362	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
363	vap->va_fileid = node->tn_id;
364	vap->va_size = node->tn_size;
365	vap->va_blocksize = PAGE_SIZE;
366	vap->va_atime = node->tn_atime;
367	vap->va_mtime = node->tn_mtime;
368	vap->va_ctime = node->tn_ctime;
369	vap->va_birthtime = node->tn_birthtime;
370	vap->va_gen = node->tn_gen;
371	vap->va_flags = node->tn_flags;
372	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
373		node->tn_rdev : NODEV;
374	vap->va_bytes = round_page(node->tn_size);
375	vap->va_filerev = 0;
376
377	return 0;
378}
379
380/* --------------------------------------------------------------------- */
381
382/* XXX Should this operation be atomic?  I think it should, but code in
383 * XXX other places (e.g., ufs) doesn't seem to be... */
384int
385tmpfs_setattr(struct vop_setattr_args *v)
386{
387	struct vnode *vp = v->a_vp;
388	struct vattr *vap = v->a_vap;
389	struct ucred *cred = v->a_cred;
390	struct thread *td = curthread;
391
392	int error;
393
394	MPASS(VOP_ISLOCKED(vp));
395
396	error = 0;
397
398	/* Abort if any unsettable attribute is given. */
399	if (vap->va_type != VNON ||
400	    vap->va_nlink != VNOVAL ||
401	    vap->va_fsid != VNOVAL ||
402	    vap->va_fileid != VNOVAL ||
403	    vap->va_blocksize != VNOVAL ||
404	    vap->va_gen != VNOVAL ||
405	    vap->va_rdev != VNOVAL ||
406	    vap->va_bytes != VNOVAL)
407		error = EINVAL;
408
409	if (error == 0 && (vap->va_flags != VNOVAL))
410		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
411
412	if (error == 0 && (vap->va_size != VNOVAL))
413		error = tmpfs_chsize(vp, vap->va_size, cred, td);
414
415	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
416		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
417
418	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
419		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
420
421	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
422	    vap->va_atime.tv_nsec != VNOVAL) ||
423	    (vap->va_mtime.tv_sec != VNOVAL &&
424	    vap->va_mtime.tv_nsec != VNOVAL) ||
425	    (vap->va_birthtime.tv_sec != VNOVAL &&
426	    vap->va_birthtime.tv_nsec != VNOVAL)))
427		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
428			&vap->va_birthtime, vap->va_vaflags, cred, td);
429
430	/* Update the node times.  We give preference to the error codes
431	 * generated by this function rather than the ones that may arise
432	 * from tmpfs_update. */
433	tmpfs_update(vp);
434
435	MPASS(VOP_ISLOCKED(vp));
436
437	return error;
438}
439
440static int
441tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
442    vm_offset_t offset, size_t tlen, struct uio *uio)
443{
444	vm_page_t	m;
445	int		error, rv;
446
447	VM_OBJECT_WLOCK(tobj);
448
449	/*
450	 * The kern_sendfile() code calls vn_rdwr() with the page
451	 * soft-busied.  Ignore the soft-busy state here. Parallel
452	 * reads of the page content from disk are prevented by
453	 * VPO_BUSY.
454	 *
455	 * Although the tmpfs vnode lock is held here, it is
456	 * nonetheless safe to sleep waiting for a free page.  The
457	 * pageout daemon does not need to acquire the tmpfs vnode
458	 * lock to page out tobj's pages because tobj is a OBJT_SWAP
459	 * type object.
460	 */
461	m = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
462	    VM_ALLOC_IGN_SBUSY);
463	if (m->valid != VM_PAGE_BITS_ALL) {
464		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
465			rv = vm_pager_get_pages(tobj, &m, 1, 0);
466			m = vm_page_lookup(tobj, idx);
467			if (m == NULL) {
468				printf(
469		    "tmpfs: vm_obj %p idx %jd null lookup rv %d\n",
470				    tobj, idx, rv);
471				return (EIO);
472			}
473			if (rv != VM_PAGER_OK) {
474				printf(
475		    "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n",
476				    tobj, idx, m->valid, rv);
477				vm_page_lock(m);
478				vm_page_free(m);
479				vm_page_unlock(m);
480				VM_OBJECT_WUNLOCK(tobj);
481				return (EIO);
482			}
483		} else
484			vm_page_zero_invalid(m, TRUE);
485	}
486	vm_page_lock(m);
487	vm_page_hold(m);
488	vm_page_wakeup(m);
489	vm_page_unlock(m);
490	VM_OBJECT_WUNLOCK(tobj);
491	error = uiomove_fromphys(&m, offset, tlen, uio);
492	VM_OBJECT_WLOCK(tobj);
493	vm_page_lock(m);
494	vm_page_unhold(m);
495	vm_page_deactivate(m);
496	/* Requeue to maintain LRU ordering. */
497	vm_page_requeue(m);
498	vm_page_unlock(m);
499	VM_OBJECT_WUNLOCK(tobj);
500
501	return (error);
502}
503
504static int
505tmpfs_read(struct vop_read_args *v)
506{
507	struct vnode *vp = v->a_vp;
508	struct uio *uio = v->a_uio;
509	struct tmpfs_node *node;
510	vm_object_t uobj;
511	size_t len;
512	int resid;
513	int error = 0;
514	vm_pindex_t	idx;
515	vm_offset_t	offset;
516	off_t		addr;
517	size_t		tlen;
518
519	node = VP_TO_TMPFS_NODE(vp);
520
521	if (vp->v_type != VREG) {
522		error = EISDIR;
523		goto out;
524	}
525
526	if (uio->uio_offset < 0) {
527		error = EINVAL;
528		goto out;
529	}
530
531	node->tn_status |= TMPFS_NODE_ACCESSED;
532
533	uobj = node->tn_reg.tn_aobj;
534	while ((resid = uio->uio_resid) > 0) {
535		error = 0;
536		if (node->tn_size <= uio->uio_offset)
537			break;
538		len = MIN(node->tn_size - uio->uio_offset, resid);
539		if (len == 0)
540			break;
541		addr = uio->uio_offset;
542		idx = OFF_TO_IDX(addr);
543		offset = addr & PAGE_MASK;
544		tlen = MIN(PAGE_SIZE - offset, len);
545		error = tmpfs_nocacheread(uobj, idx, offset, tlen, uio);
546		if ((error != 0) || (resid == uio->uio_resid))
547			break;
548	}
549
550out:
551
552	return error;
553}
554
555/* --------------------------------------------------------------------- */
556
557static int
558tmpfs_mappedwrite(vm_object_t tobj, size_t len, struct uio *uio)
559{
560	vm_pindex_t	idx;
561	vm_page_t	tpg;
562	vm_offset_t	offset;
563	off_t		addr;
564	size_t		tlen;
565	int		error, rv;
566
567	error = 0;
568
569	addr = uio->uio_offset;
570	idx = OFF_TO_IDX(addr);
571	offset = addr & PAGE_MASK;
572	tlen = MIN(PAGE_SIZE - offset, len);
573
574	VM_OBJECT_WLOCK(tobj);
575	tpg = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
576	if (tpg->valid != VM_PAGE_BITS_ALL) {
577		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
578			rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
579			tpg = vm_page_lookup(tobj, idx);
580			if (tpg == NULL) {
581				printf(
582		    "tmpfs: vm_obj %p idx %jd null lookup rv %d\n",
583				    tobj, idx, rv);
584				return (EIO);
585			}
586			if (rv != VM_PAGER_OK) {
587				printf(
588		    "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n",
589				    tobj, idx, tpg->valid, rv);
590				vm_page_lock(tpg);
591				vm_page_free(tpg);
592				vm_page_unlock(tpg);
593				VM_OBJECT_WUNLOCK(tobj);
594				return (EIO);
595			}
596		} else
597			vm_page_zero_invalid(tpg, TRUE);
598	}
599	vm_page_lock(tpg);
600	vm_page_hold(tpg);
601	vm_page_wakeup(tpg);
602	vm_page_unlock(tpg);
603	VM_OBJECT_WUNLOCK(tobj);
604	error = uiomove_fromphys(&tpg, offset, tlen, uio);
605	VM_OBJECT_WLOCK(tobj);
606	if (error == 0)
607		vm_page_dirty(tpg);
608	vm_page_lock(tpg);
609	vm_page_unhold(tpg);
610	vm_page_deactivate(tpg);
611	/* Requeue to maintain LRU ordering. */
612	vm_page_requeue(tpg);
613	vm_page_unlock(tpg);
614	VM_OBJECT_WUNLOCK(tobj);
615
616	return	(error);
617}
618
619static int
620tmpfs_write(struct vop_write_args *v)
621{
622	struct vnode *vp = v->a_vp;
623	struct uio *uio = v->a_uio;
624	int ioflag = v->a_ioflag;
625
626	boolean_t extended;
627	int error = 0;
628	off_t oldsize;
629	struct tmpfs_node *node;
630	vm_object_t uobj;
631	size_t len;
632	int resid;
633
634	node = VP_TO_TMPFS_NODE(vp);
635	oldsize = node->tn_size;
636
637	if (uio->uio_offset < 0 || vp->v_type != VREG) {
638		error = EINVAL;
639		goto out;
640	}
641
642	if (uio->uio_resid == 0) {
643		error = 0;
644		goto out;
645	}
646
647	if (ioflag & IO_APPEND)
648		uio->uio_offset = node->tn_size;
649
650	if (uio->uio_offset + uio->uio_resid >
651	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
652		return (EFBIG);
653
654	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
655		return (EFBIG);
656
657	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
658	if (extended) {
659		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
660		    FALSE);
661		if (error != 0)
662			goto out;
663	}
664
665	uobj = node->tn_reg.tn_aobj;
666	while ((resid = uio->uio_resid) > 0) {
667		if (node->tn_size <= uio->uio_offset)
668			break;
669		len = MIN(node->tn_size - uio->uio_offset, resid);
670		if (len == 0)
671			break;
672		error = tmpfs_mappedwrite(uobj, len, uio);
673		if ((error != 0) || (resid == uio->uio_resid))
674			break;
675	}
676
677	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
678	    (extended ? TMPFS_NODE_CHANGED : 0);
679
680	if (node->tn_mode & (S_ISUID | S_ISGID)) {
681		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
682			node->tn_mode &= ~(S_ISUID | S_ISGID);
683	}
684
685	if (error != 0)
686		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
687
688out:
689	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
690	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
691
692	return error;
693}
694
695/* --------------------------------------------------------------------- */
696
697static int
698tmpfs_fsync(struct vop_fsync_args *v)
699{
700	struct vnode *vp = v->a_vp;
701
702	MPASS(VOP_ISLOCKED(vp));
703
704	tmpfs_update(vp);
705
706	return 0;
707}
708
709/* --------------------------------------------------------------------- */
710
711static int
712tmpfs_remove(struct vop_remove_args *v)
713{
714	struct vnode *dvp = v->a_dvp;
715	struct vnode *vp = v->a_vp;
716
717	int error;
718	struct tmpfs_dirent *de;
719	struct tmpfs_mount *tmp;
720	struct tmpfs_node *dnode;
721	struct tmpfs_node *node;
722
723	MPASS(VOP_ISLOCKED(dvp));
724	MPASS(VOP_ISLOCKED(vp));
725
726	if (vp->v_type == VDIR) {
727		error = EISDIR;
728		goto out;
729	}
730
731	dnode = VP_TO_TMPFS_DIR(dvp);
732	node = VP_TO_TMPFS_NODE(vp);
733	tmp = VFS_TO_TMPFS(vp->v_mount);
734	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
735	MPASS(de != NULL);
736
737	/* Files marked as immutable or append-only cannot be deleted. */
738	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
739	    (dnode->tn_flags & APPEND)) {
740		error = EPERM;
741		goto out;
742	}
743
744	/* Remove the entry from the directory; as it is a file, we do not
745	 * have to change the number of hard links of the directory. */
746	tmpfs_dir_detach(dvp, de);
747	if (v->a_cnp->cn_flags & DOWHITEOUT)
748		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
749
750	/* Free the directory entry we just deleted.  Note that the node
751	 * referred by it will not be removed until the vnode is really
752	 * reclaimed. */
753	tmpfs_free_dirent(tmp, de);
754
755	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
756	error = 0;
757
758out:
759
760	return error;
761}
762
763/* --------------------------------------------------------------------- */
764
765static int
766tmpfs_link(struct vop_link_args *v)
767{
768	struct vnode *dvp = v->a_tdvp;
769	struct vnode *vp = v->a_vp;
770	struct componentname *cnp = v->a_cnp;
771
772	int error;
773	struct tmpfs_dirent *de;
774	struct tmpfs_node *node;
775
776	MPASS(VOP_ISLOCKED(dvp));
777	MPASS(cnp->cn_flags & HASBUF);
778	MPASS(dvp != vp); /* XXX When can this be false? */
779
780	node = VP_TO_TMPFS_NODE(vp);
781
782	/* XXX: Why aren't the following two tests done by the caller? */
783
784	/* Hard links of directories are forbidden. */
785	if (vp->v_type == VDIR) {
786		error = EPERM;
787		goto out;
788	}
789
790	/* Cannot create cross-device links. */
791	if (dvp->v_mount != vp->v_mount) {
792		error = EXDEV;
793		goto out;
794	}
795
796	/* Ensure that we do not overflow the maximum number of links imposed
797	 * by the system. */
798	MPASS(node->tn_links <= LINK_MAX);
799	if (node->tn_links == LINK_MAX) {
800		error = EMLINK;
801		goto out;
802	}
803
804	/* We cannot create links of files marked immutable or append-only. */
805	if (node->tn_flags & (IMMUTABLE | APPEND)) {
806		error = EPERM;
807		goto out;
808	}
809
810	/* Allocate a new directory entry to represent the node. */
811	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
812	    cnp->cn_nameptr, cnp->cn_namelen, &de);
813	if (error != 0)
814		goto out;
815
816	/* Insert the new directory entry into the appropriate directory. */
817	if (cnp->cn_flags & ISWHITEOUT)
818		tmpfs_dir_whiteout_remove(dvp, cnp);
819	tmpfs_dir_attach(dvp, de);
820
821	/* vp link count has changed, so update node times. */
822	node->tn_status |= TMPFS_NODE_CHANGED;
823	tmpfs_update(vp);
824
825	error = 0;
826
827out:
828	return error;
829}
830
831/* --------------------------------------------------------------------- */
832
833/*
834 * We acquire all but fdvp locks using non-blocking acquisitions.  If we
835 * fail to acquire any lock in the path we will drop all held locks,
836 * acquire the new lock in a blocking fashion, and then release it and
837 * restart the rename.  This acquire/release step ensures that we do not
838 * spin on a lock waiting for release.  On error release all vnode locks
839 * and decrement references the way tmpfs_rename() would do.
840 */
841static int
842tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
843    struct vnode *tdvp, struct vnode **tvpp,
844    struct componentname *fcnp, struct componentname *tcnp)
845{
846	struct vnode *nvp;
847	struct mount *mp;
848	struct tmpfs_dirent *de;
849	int error, restarts = 0;
850
851	VOP_UNLOCK(tdvp, 0);
852	if (*tvpp != NULL && *tvpp != tdvp)
853		VOP_UNLOCK(*tvpp, 0);
854	mp = fdvp->v_mount;
855
856relock:
857	restarts += 1;
858	error = vn_lock(fdvp, LK_EXCLUSIVE);
859	if (error)
860		goto releout;
861	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
862		VOP_UNLOCK(fdvp, 0);
863		error = vn_lock(tdvp, LK_EXCLUSIVE);
864		if (error)
865			goto releout;
866		VOP_UNLOCK(tdvp, 0);
867		goto relock;
868	}
869	/*
870	 * Re-resolve fvp to be certain it still exists and fetch the
871	 * correct vnode.
872	 */
873	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
874	if (de == NULL) {
875		VOP_UNLOCK(fdvp, 0);
876		VOP_UNLOCK(tdvp, 0);
877		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
878		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
879			error = EINVAL;
880		else
881			error = ENOENT;
882		goto releout;
883	}
884	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
885	if (error != 0) {
886		VOP_UNLOCK(fdvp, 0);
887		VOP_UNLOCK(tdvp, 0);
888		if (error != EBUSY)
889			goto releout;
890		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
891		if (error != 0)
892			goto releout;
893		VOP_UNLOCK(nvp, 0);
894		/*
895		 * Concurrent rename race.
896		 */
897		if (nvp == tdvp) {
898			vrele(nvp);
899			error = EINVAL;
900			goto releout;
901		}
902		vrele(*fvpp);
903		*fvpp = nvp;
904		goto relock;
905	}
906	vrele(*fvpp);
907	*fvpp = nvp;
908	VOP_UNLOCK(*fvpp, 0);
909	/*
910	 * Re-resolve tvp and acquire the vnode lock if present.
911	 */
912	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
913	/*
914	 * If tvp disappeared we just carry on.
915	 */
916	if (de == NULL && *tvpp != NULL) {
917		vrele(*tvpp);
918		*tvpp = NULL;
919	}
920	/*
921	 * Get the tvp ino if the lookup succeeded.  We may have to restart
922	 * if the non-blocking acquire fails.
923	 */
924	if (de != NULL) {
925		nvp = NULL;
926		error = tmpfs_alloc_vp(mp, de->td_node,
927		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
928		if (*tvpp != NULL)
929			vrele(*tvpp);
930		*tvpp = nvp;
931		if (error != 0) {
932			VOP_UNLOCK(fdvp, 0);
933			VOP_UNLOCK(tdvp, 0);
934			if (error != EBUSY)
935				goto releout;
936			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
937			    &nvp);
938			if (error != 0)
939				goto releout;
940			VOP_UNLOCK(nvp, 0);
941			/*
942			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
943			 */
944			if (nvp == fdvp) {
945				error = ENOTEMPTY;
946				goto releout;
947			}
948			goto relock;
949		}
950	}
951	tmpfs_rename_restarts += restarts;
952
953	return (0);
954
955releout:
956	vrele(fdvp);
957	vrele(*fvpp);
958	vrele(tdvp);
959	if (*tvpp != NULL)
960		vrele(*tvpp);
961	tmpfs_rename_restarts += restarts;
962
963	return (error);
964}
965
966static int
967tmpfs_rename(struct vop_rename_args *v)
968{
969	struct vnode *fdvp = v->a_fdvp;
970	struct vnode *fvp = v->a_fvp;
971	struct componentname *fcnp = v->a_fcnp;
972	struct vnode *tdvp = v->a_tdvp;
973	struct vnode *tvp = v->a_tvp;
974	struct componentname *tcnp = v->a_tcnp;
975	struct mount *mp = NULL;
976
977	char *newname;
978	int error;
979	struct tmpfs_dirent *de;
980	struct tmpfs_mount *tmp;
981	struct tmpfs_node *fdnode;
982	struct tmpfs_node *fnode;
983	struct tmpfs_node *tnode;
984	struct tmpfs_node *tdnode;
985
986	MPASS(VOP_ISLOCKED(tdvp));
987	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
988	MPASS(fcnp->cn_flags & HASBUF);
989	MPASS(tcnp->cn_flags & HASBUF);
990
991	/* Disallow cross-device renames.
992	 * XXX Why isn't this done by the caller? */
993	if (fvp->v_mount != tdvp->v_mount ||
994	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
995		error = EXDEV;
996		goto out;
997	}
998
999	/* If source and target are the same file, there is nothing to do. */
1000	if (fvp == tvp) {
1001		error = 0;
1002		goto out;
1003	}
1004
1005	/* If we need to move the directory between entries, lock the
1006	 * source so that we can safely operate on it. */
1007	if (fdvp != tdvp && fdvp != tvp) {
1008		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1009			mp = tdvp->v_mount;
1010			error = vfs_busy(mp, 0);
1011			if (error != 0) {
1012				mp = NULL;
1013				goto out;
1014			}
1015			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1016			    fcnp, tcnp);
1017			if (error != 0) {
1018				vfs_unbusy(mp);
1019				return (error);
1020			}
1021			ASSERT_VOP_ELOCKED(fdvp,
1022			    "tmpfs_rename: fdvp not locked");
1023			ASSERT_VOP_ELOCKED(tdvp,
1024			    "tmpfs_rename: tdvp not locked");
1025			if (tvp != NULL)
1026				ASSERT_VOP_ELOCKED(tvp,
1027				    "tmpfs_rename: tvp not locked");
1028			if (fvp == tvp) {
1029				error = 0;
1030				goto out_locked;
1031			}
1032		}
1033	}
1034
1035	tmp = VFS_TO_TMPFS(tdvp->v_mount);
1036	tdnode = VP_TO_TMPFS_DIR(tdvp);
1037	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1038	fdnode = VP_TO_TMPFS_DIR(fdvp);
1039	fnode = VP_TO_TMPFS_NODE(fvp);
1040	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1041
1042	/* Entry can disappear before we lock fdvp,
1043	 * also avoid manipulating '.' and '..' entries. */
1044	if (de == NULL) {
1045		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1046		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1047			error = EINVAL;
1048		else
1049			error = ENOENT;
1050		goto out_locked;
1051	}
1052	MPASS(de->td_node == fnode);
1053
1054	/* If re-naming a directory to another preexisting directory
1055	 * ensure that the target directory is empty so that its
1056	 * removal causes no side effects.
1057	 * Kern_rename gurantees the destination to be a directory
1058	 * if the source is one. */
1059	if (tvp != NULL) {
1060		MPASS(tnode != NULL);
1061
1062		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1063		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1064			error = EPERM;
1065			goto out_locked;
1066		}
1067
1068		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1069			if (tnode->tn_size > 0) {
1070				error = ENOTEMPTY;
1071				goto out_locked;
1072			}
1073		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1074			error = ENOTDIR;
1075			goto out_locked;
1076		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1077			error = EISDIR;
1078			goto out_locked;
1079		} else {
1080			MPASS(fnode->tn_type != VDIR &&
1081				tnode->tn_type != VDIR);
1082		}
1083	}
1084
1085	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1086	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1087		error = EPERM;
1088		goto out_locked;
1089	}
1090
1091	/* Ensure that we have enough memory to hold the new name, if it
1092	 * has to be changed. */
1093	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1094	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1095		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1096	} else
1097		newname = NULL;
1098
1099	/* If the node is being moved to another directory, we have to do
1100	 * the move. */
1101	if (fdnode != tdnode) {
1102		/* In case we are moving a directory, we have to adjust its
1103		 * parent to point to the new parent. */
1104		if (de->td_node->tn_type == VDIR) {
1105			struct tmpfs_node *n;
1106
1107			/* Ensure the target directory is not a child of the
1108			 * directory being moved.  Otherwise, we'd end up
1109			 * with stale nodes. */
1110			n = tdnode;
1111			/* TMPFS_LOCK garanties that no nodes are freed while
1112			 * traversing the list. Nodes can only be marked as
1113			 * removed: tn_parent == NULL. */
1114			TMPFS_LOCK(tmp);
1115			TMPFS_NODE_LOCK(n);
1116			while (n != n->tn_dir.tn_parent) {
1117				struct tmpfs_node *parent;
1118
1119				if (n == fnode) {
1120					TMPFS_NODE_UNLOCK(n);
1121					TMPFS_UNLOCK(tmp);
1122					error = EINVAL;
1123					if (newname != NULL)
1124						    free(newname, M_TMPFSNAME);
1125					goto out_locked;
1126				}
1127				parent = n->tn_dir.tn_parent;
1128				TMPFS_NODE_UNLOCK(n);
1129				if (parent == NULL) {
1130					n = NULL;
1131					break;
1132				}
1133				TMPFS_NODE_LOCK(parent);
1134				if (parent->tn_dir.tn_parent == NULL) {
1135					TMPFS_NODE_UNLOCK(parent);
1136					n = NULL;
1137					break;
1138				}
1139				n = parent;
1140			}
1141			TMPFS_UNLOCK(tmp);
1142			if (n == NULL) {
1143				error = EINVAL;
1144				if (newname != NULL)
1145					    free(newname, M_TMPFSNAME);
1146				goto out_locked;
1147			}
1148			TMPFS_NODE_UNLOCK(n);
1149
1150			/* Adjust the parent pointer. */
1151			TMPFS_VALIDATE_DIR(fnode);
1152			TMPFS_NODE_LOCK(de->td_node);
1153			de->td_node->tn_dir.tn_parent = tdnode;
1154			TMPFS_NODE_UNLOCK(de->td_node);
1155
1156			/* As a result of changing the target of the '..'
1157			 * entry, the link count of the source and target
1158			 * directories has to be adjusted. */
1159			TMPFS_NODE_LOCK(tdnode);
1160			TMPFS_ASSERT_LOCKED(tdnode);
1161			tdnode->tn_links++;
1162			TMPFS_NODE_UNLOCK(tdnode);
1163
1164			TMPFS_NODE_LOCK(fdnode);
1165			TMPFS_ASSERT_LOCKED(fdnode);
1166			fdnode->tn_links--;
1167			TMPFS_NODE_UNLOCK(fdnode);
1168		}
1169	}
1170
1171	/* Do the move: just remove the entry from the source directory
1172	 * and insert it into the target one. */
1173	tmpfs_dir_detach(fdvp, de);
1174
1175	if (fcnp->cn_flags & DOWHITEOUT)
1176		tmpfs_dir_whiteout_add(fdvp, fcnp);
1177	if (tcnp->cn_flags & ISWHITEOUT)
1178		tmpfs_dir_whiteout_remove(tdvp, tcnp);
1179
1180	/* If the name has changed, we need to make it effective by changing
1181	 * it in the directory entry. */
1182	if (newname != NULL) {
1183		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1184
1185		free(de->ud.td_name, M_TMPFSNAME);
1186		de->ud.td_name = newname;
1187		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1188
1189		fnode->tn_status |= TMPFS_NODE_CHANGED;
1190		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1191	}
1192
1193	/* If we are overwriting an entry, we have to remove the old one
1194	 * from the target directory. */
1195	if (tvp != NULL) {
1196		struct tmpfs_dirent *tde;
1197
1198		/* Remove the old entry from the target directory. */
1199		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1200		tmpfs_dir_detach(tdvp, tde);
1201
1202		/* Free the directory entry we just deleted.  Note that the
1203		 * node referred by it will not be removed until the vnode is
1204		 * really reclaimed. */
1205		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1206	}
1207
1208	tmpfs_dir_attach(tdvp, de);
1209
1210	cache_purge(fvp);
1211	if (tvp != NULL)
1212		cache_purge(tvp);
1213	cache_purge_negative(tdvp);
1214
1215	error = 0;
1216
1217out_locked:
1218	if (fdvp != tdvp && fdvp != tvp)
1219		VOP_UNLOCK(fdvp, 0);
1220
1221out:
1222	/* Release target nodes. */
1223	/* XXX: I don't understand when tdvp can be the same as tvp, but
1224	 * other code takes care of this... */
1225	if (tdvp == tvp)
1226		vrele(tdvp);
1227	else
1228		vput(tdvp);
1229	if (tvp != NULL)
1230		vput(tvp);
1231
1232	/* Release source nodes. */
1233	vrele(fdvp);
1234	vrele(fvp);
1235
1236	if (mp != NULL)
1237		vfs_unbusy(mp);
1238
1239	return error;
1240}
1241
1242/* --------------------------------------------------------------------- */
1243
1244static int
1245tmpfs_mkdir(struct vop_mkdir_args *v)
1246{
1247	struct vnode *dvp = v->a_dvp;
1248	struct vnode **vpp = v->a_vpp;
1249	struct componentname *cnp = v->a_cnp;
1250	struct vattr *vap = v->a_vap;
1251
1252	MPASS(vap->va_type == VDIR);
1253
1254	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1255}
1256
1257/* --------------------------------------------------------------------- */
1258
1259static int
1260tmpfs_rmdir(struct vop_rmdir_args *v)
1261{
1262	struct vnode *dvp = v->a_dvp;
1263	struct vnode *vp = v->a_vp;
1264
1265	int error;
1266	struct tmpfs_dirent *de;
1267	struct tmpfs_mount *tmp;
1268	struct tmpfs_node *dnode;
1269	struct tmpfs_node *node;
1270
1271	MPASS(VOP_ISLOCKED(dvp));
1272	MPASS(VOP_ISLOCKED(vp));
1273
1274	tmp = VFS_TO_TMPFS(dvp->v_mount);
1275	dnode = VP_TO_TMPFS_DIR(dvp);
1276	node = VP_TO_TMPFS_DIR(vp);
1277
1278	/* Directories with more than two entries ('.' and '..') cannot be
1279	 * removed. */
1280	 if (node->tn_size > 0) {
1281		 error = ENOTEMPTY;
1282		 goto out;
1283	 }
1284
1285	if ((dnode->tn_flags & APPEND)
1286	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1287		error = EPERM;
1288		goto out;
1289	}
1290
1291	/* This invariant holds only if we are not trying to remove "..".
1292	  * We checked for that above so this is safe now. */
1293	MPASS(node->tn_dir.tn_parent == dnode);
1294
1295	/* Get the directory entry associated with node (vp).  This was
1296	 * filled by tmpfs_lookup while looking up the entry. */
1297	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1298	MPASS(TMPFS_DIRENT_MATCHES(de,
1299	    v->a_cnp->cn_nameptr,
1300	    v->a_cnp->cn_namelen));
1301
1302	/* Check flags to see if we are allowed to remove the directory. */
1303	if (dnode->tn_flags & APPEND
1304		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1305		error = EPERM;
1306		goto out;
1307	}
1308
1309
1310	/* Detach the directory entry from the directory (dnode). */
1311	tmpfs_dir_detach(dvp, de);
1312	if (v->a_cnp->cn_flags & DOWHITEOUT)
1313		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1314
1315	/* No vnode should be allocated for this entry from this point */
1316	TMPFS_NODE_LOCK(node);
1317	TMPFS_ASSERT_ELOCKED(node);
1318	node->tn_links--;
1319	node->tn_dir.tn_parent = NULL;
1320	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1321	    TMPFS_NODE_MODIFIED;
1322
1323	TMPFS_NODE_UNLOCK(node);
1324
1325	TMPFS_NODE_LOCK(dnode);
1326	TMPFS_ASSERT_ELOCKED(dnode);
1327	dnode->tn_links--;
1328	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1329	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1330	TMPFS_NODE_UNLOCK(dnode);
1331
1332	cache_purge(dvp);
1333	cache_purge(vp);
1334
1335	/* Free the directory entry we just deleted.  Note that the node
1336	 * referred by it will not be removed until the vnode is really
1337	 * reclaimed. */
1338	tmpfs_free_dirent(tmp, de);
1339
1340	/* Release the deleted vnode (will destroy the node, notify
1341	 * interested parties and clean it from the cache). */
1342
1343	dnode->tn_status |= TMPFS_NODE_CHANGED;
1344	tmpfs_update(dvp);
1345
1346	error = 0;
1347
1348out:
1349	return error;
1350}
1351
1352/* --------------------------------------------------------------------- */
1353
1354static int
1355tmpfs_symlink(struct vop_symlink_args *v)
1356{
1357	struct vnode *dvp = v->a_dvp;
1358	struct vnode **vpp = v->a_vpp;
1359	struct componentname *cnp = v->a_cnp;
1360	struct vattr *vap = v->a_vap;
1361	char *target = v->a_target;
1362
1363#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1364	MPASS(vap->va_type == VLNK);
1365#else
1366	vap->va_type = VLNK;
1367#endif
1368
1369	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1370}
1371
1372/* --------------------------------------------------------------------- */
1373
1374static int
1375tmpfs_readdir(struct vop_readdir_args *v)
1376{
1377	struct vnode *vp = v->a_vp;
1378	struct uio *uio = v->a_uio;
1379	int *eofflag = v->a_eofflag;
1380	u_long **cookies = v->a_cookies;
1381	int *ncookies = v->a_ncookies;
1382
1383	int error;
1384	ssize_t startresid;
1385	int cnt = 0;
1386	struct tmpfs_node *node;
1387
1388	/* This operation only makes sense on directory nodes. */
1389	if (vp->v_type != VDIR)
1390		return ENOTDIR;
1391
1392	node = VP_TO_TMPFS_DIR(vp);
1393
1394	startresid = uio->uio_resid;
1395
1396	if (cookies != NULL && ncookies != NULL) {
1397		cnt = howmany(node->tn_size, sizeof(struct tmpfs_dirent)) + 2;
1398		*cookies = malloc(cnt * sizeof(**cookies), M_TEMP, M_WAITOK);
1399		*ncookies = 0;
1400	}
1401
1402	if (cnt == 0)
1403		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1404	else
1405		error = tmpfs_dir_getdents(node, uio, cnt, *cookies, ncookies);
1406
1407	if (error == EJUSTRETURN)
1408		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1409
1410	if (error != 0 && cnt != 0)
1411		free(*cookies, M_TEMP);
1412
1413	if (eofflag != NULL)
1414		*eofflag =
1415		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1416
1417	return error;
1418}
1419
1420/* --------------------------------------------------------------------- */
1421
1422static int
1423tmpfs_readlink(struct vop_readlink_args *v)
1424{
1425	struct vnode *vp = v->a_vp;
1426	struct uio *uio = v->a_uio;
1427
1428	int error;
1429	struct tmpfs_node *node;
1430
1431	MPASS(uio->uio_offset == 0);
1432	MPASS(vp->v_type == VLNK);
1433
1434	node = VP_TO_TMPFS_NODE(vp);
1435
1436	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1437	    uio);
1438	node->tn_status |= TMPFS_NODE_ACCESSED;
1439
1440	return error;
1441}
1442
1443/* --------------------------------------------------------------------- */
1444
1445static int
1446tmpfs_inactive(struct vop_inactive_args *v)
1447{
1448	struct vnode *vp = v->a_vp;
1449
1450	struct tmpfs_node *node;
1451
1452	node = VP_TO_TMPFS_NODE(vp);
1453
1454	if (node->tn_links == 0)
1455		vrecycle(vp);
1456
1457	return 0;
1458}
1459
1460/* --------------------------------------------------------------------- */
1461
1462int
1463tmpfs_reclaim(struct vop_reclaim_args *v)
1464{
1465	struct vnode *vp = v->a_vp;
1466
1467	struct tmpfs_mount *tmp;
1468	struct tmpfs_node *node;
1469	vm_object_t obj;
1470
1471	node = VP_TO_TMPFS_NODE(vp);
1472	tmp = VFS_TO_TMPFS(vp->v_mount);
1473
1474	if (node->tn_type == VREG) {
1475		obj = node->tn_reg.tn_aobj;
1476		if (obj != NULL) {
1477			/* Instead of vnode_destroy_vobject() */
1478			VM_OBJECT_WLOCK(obj);
1479			VI_LOCK(vp);
1480			vm_object_clear_flag(obj, OBJ_TMPFS);
1481			obj->un_pager.swp.swp_tmpfs = NULL;
1482			VI_UNLOCK(vp);
1483			VM_OBJECT_WUNLOCK(obj);
1484		}
1485	}
1486	vp->v_object = NULL;
1487	cache_purge(vp);
1488
1489	TMPFS_NODE_LOCK(node);
1490	TMPFS_ASSERT_ELOCKED(node);
1491	tmpfs_free_vp(vp);
1492
1493	/* If the node referenced by this vnode was deleted by the user,
1494	 * we must free its associated data structures (now that the vnode
1495	 * is being reclaimed). */
1496	if (node->tn_links == 0 &&
1497	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1498		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1499		TMPFS_NODE_UNLOCK(node);
1500		tmpfs_free_node(tmp, node);
1501	} else
1502		TMPFS_NODE_UNLOCK(node);
1503
1504	MPASS(vp->v_data == NULL);
1505	return 0;
1506}
1507
1508/* --------------------------------------------------------------------- */
1509
1510static int
1511tmpfs_print(struct vop_print_args *v)
1512{
1513	struct vnode *vp = v->a_vp;
1514
1515	struct tmpfs_node *node;
1516
1517	node = VP_TO_TMPFS_NODE(vp);
1518
1519	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1520	    node, node->tn_flags, node->tn_links);
1521	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1522	    node->tn_mode, node->tn_uid, node->tn_gid,
1523	    (intmax_t)node->tn_size, node->tn_status);
1524
1525	if (vp->v_type == VFIFO)
1526		fifo_printinfo(vp);
1527
1528	printf("\n");
1529
1530	return 0;
1531}
1532
1533/* --------------------------------------------------------------------- */
1534
1535static int
1536tmpfs_pathconf(struct vop_pathconf_args *v)
1537{
1538	int name = v->a_name;
1539	register_t *retval = v->a_retval;
1540
1541	int error;
1542
1543	error = 0;
1544
1545	switch (name) {
1546	case _PC_LINK_MAX:
1547		*retval = LINK_MAX;
1548		break;
1549
1550	case _PC_NAME_MAX:
1551		*retval = NAME_MAX;
1552		break;
1553
1554	case _PC_PATH_MAX:
1555		*retval = PATH_MAX;
1556		break;
1557
1558	case _PC_PIPE_BUF:
1559		*retval = PIPE_BUF;
1560		break;
1561
1562	case _PC_CHOWN_RESTRICTED:
1563		*retval = 1;
1564		break;
1565
1566	case _PC_NO_TRUNC:
1567		*retval = 1;
1568		break;
1569
1570	case _PC_SYNC_IO:
1571		*retval = 1;
1572		break;
1573
1574	case _PC_FILESIZEBITS:
1575		*retval = 0; /* XXX Don't know which value should I return. */
1576		break;
1577
1578	default:
1579		error = EINVAL;
1580	}
1581
1582	return error;
1583}
1584
1585static int
1586tmpfs_vptofh(struct vop_vptofh_args *ap)
1587{
1588	struct tmpfs_fid *tfhp;
1589	struct tmpfs_node *node;
1590
1591	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1592	node = VP_TO_TMPFS_NODE(ap->a_vp);
1593
1594	tfhp->tf_len = sizeof(struct tmpfs_fid);
1595	tfhp->tf_id = node->tn_id;
1596	tfhp->tf_gen = node->tn_gen;
1597
1598	return (0);
1599}
1600
1601static int
1602tmpfs_whiteout(struct vop_whiteout_args *ap)
1603{
1604	struct vnode *dvp = ap->a_dvp;
1605	struct componentname *cnp = ap->a_cnp;
1606	struct tmpfs_dirent *de;
1607
1608	switch (ap->a_flags) {
1609	case LOOKUP:
1610		return (0);
1611	case CREATE:
1612		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1613		if (de != NULL)
1614			return (de->td_node == NULL ? 0 : EEXIST);
1615		return (tmpfs_dir_whiteout_add(dvp, cnp));
1616	case DELETE:
1617		tmpfs_dir_whiteout_remove(dvp, cnp);
1618		return (0);
1619	default:
1620		panic("tmpfs_whiteout: unknown op");
1621	}
1622}
1623
1624/* --------------------------------------------------------------------- */
1625
1626/*
1627 * vnode operations vector used for files stored in a tmpfs file system.
1628 */
1629struct vop_vector tmpfs_vnodeop_entries = {
1630	.vop_default =			&default_vnodeops,
1631	.vop_lookup =			vfs_cache_lookup,
1632	.vop_cachedlookup =		tmpfs_lookup,
1633	.vop_create =			tmpfs_create,
1634	.vop_mknod =			tmpfs_mknod,
1635	.vop_open =			tmpfs_open,
1636	.vop_close =			tmpfs_close,
1637	.vop_access =			tmpfs_access,
1638	.vop_getattr =			tmpfs_getattr,
1639	.vop_setattr =			tmpfs_setattr,
1640	.vop_read =			tmpfs_read,
1641	.vop_write =			tmpfs_write,
1642	.vop_fsync =			tmpfs_fsync,
1643	.vop_remove =			tmpfs_remove,
1644	.vop_link =			tmpfs_link,
1645	.vop_rename =			tmpfs_rename,
1646	.vop_mkdir =			tmpfs_mkdir,
1647	.vop_rmdir =			tmpfs_rmdir,
1648	.vop_symlink =			tmpfs_symlink,
1649	.vop_readdir =			tmpfs_readdir,
1650	.vop_readlink =			tmpfs_readlink,
1651	.vop_inactive =			tmpfs_inactive,
1652	.vop_reclaim =			tmpfs_reclaim,
1653	.vop_print =			tmpfs_print,
1654	.vop_pathconf =			tmpfs_pathconf,
1655	.vop_vptofh =			tmpfs_vptofh,
1656	.vop_whiteout =			tmpfs_whiteout,
1657	.vop_bmap =			VOP_EOPNOTSUPP,
1658};
1659
1660