tmpfs_vnops.c revision 250189
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 250189 2013-05-02 18:44:31Z 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	if (m->queue != PQ_NONE)
498		vm_page_requeue(m);
499	vm_page_unlock(m);
500	VM_OBJECT_WUNLOCK(tobj);
501
502	return (error);
503}
504
505static int
506tmpfs_read(struct vop_read_args *v)
507{
508	struct vnode *vp = v->a_vp;
509	struct uio *uio = v->a_uio;
510	struct tmpfs_node *node;
511	vm_object_t uobj;
512	size_t len;
513	int resid;
514	int error = 0;
515	vm_pindex_t	idx;
516	vm_offset_t	offset;
517	off_t		addr;
518	size_t		tlen;
519
520	node = VP_TO_TMPFS_NODE(vp);
521
522	if (vp->v_type != VREG) {
523		error = EISDIR;
524		goto out;
525	}
526
527	if (uio->uio_offset < 0) {
528		error = EINVAL;
529		goto out;
530	}
531
532	node->tn_status |= TMPFS_NODE_ACCESSED;
533
534	uobj = node->tn_reg.tn_aobj;
535	while ((resid = uio->uio_resid) > 0) {
536		error = 0;
537		if (node->tn_size <= uio->uio_offset)
538			break;
539		len = MIN(node->tn_size - uio->uio_offset, resid);
540		if (len == 0)
541			break;
542		addr = uio->uio_offset;
543		idx = OFF_TO_IDX(addr);
544		offset = addr & PAGE_MASK;
545		tlen = MIN(PAGE_SIZE - offset, len);
546		error = tmpfs_nocacheread(uobj, idx, offset, tlen, uio);
547		if ((error != 0) || (resid == uio->uio_resid))
548			break;
549	}
550
551out:
552
553	return error;
554}
555
556/* --------------------------------------------------------------------- */
557
558static int
559tmpfs_mappedwrite(vm_object_t tobj, size_t len, struct uio *uio)
560{
561	vm_pindex_t	idx;
562	vm_page_t	tpg;
563	vm_offset_t	offset;
564	off_t		addr;
565	size_t		tlen;
566	int		error, rv;
567
568	error = 0;
569
570	addr = uio->uio_offset;
571	idx = OFF_TO_IDX(addr);
572	offset = addr & PAGE_MASK;
573	tlen = MIN(PAGE_SIZE - offset, len);
574
575	VM_OBJECT_WLOCK(tobj);
576	tpg = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
577	if (tpg->valid != VM_PAGE_BITS_ALL) {
578		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
579			rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
580			tpg = vm_page_lookup(tobj, idx);
581			if (tpg == NULL) {
582				printf(
583		    "tmpfs: vm_obj %p idx %jd null lookup rv %d\n",
584				    tobj, idx, rv);
585				return (EIO);
586			}
587			if (rv != VM_PAGER_OK) {
588				printf(
589		    "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n",
590				    tobj, idx, tpg->valid, rv);
591				vm_page_lock(tpg);
592				vm_page_free(tpg);
593				vm_page_unlock(tpg);
594				VM_OBJECT_WUNLOCK(tobj);
595				return (EIO);
596			}
597		} else
598			vm_page_zero_invalid(tpg, TRUE);
599	}
600	vm_page_lock(tpg);
601	vm_page_hold(tpg);
602	vm_page_wakeup(tpg);
603	vm_page_unlock(tpg);
604	VM_OBJECT_WUNLOCK(tobj);
605	error = uiomove_fromphys(&tpg, offset, tlen, uio);
606	VM_OBJECT_WLOCK(tobj);
607	if (error == 0)
608		vm_page_dirty(tpg);
609	vm_page_lock(tpg);
610	vm_page_unhold(tpg);
611	vm_page_deactivate(tpg);
612	/* Requeue to maintain LRU ordering. */
613	if (tpg->queue != PQ_NONE)
614		vm_page_requeue(tpg);
615	vm_page_unlock(tpg);
616	VM_OBJECT_WUNLOCK(tobj);
617
618	return	(error);
619}
620
621static int
622tmpfs_write(struct vop_write_args *v)
623{
624	struct vnode *vp = v->a_vp;
625	struct uio *uio = v->a_uio;
626	int ioflag = v->a_ioflag;
627
628	boolean_t extended;
629	int error = 0;
630	off_t oldsize;
631	struct tmpfs_node *node;
632	vm_object_t uobj;
633	size_t len;
634	int resid;
635
636	node = VP_TO_TMPFS_NODE(vp);
637	oldsize = node->tn_size;
638
639	if (uio->uio_offset < 0 || vp->v_type != VREG) {
640		error = EINVAL;
641		goto out;
642	}
643
644	if (uio->uio_resid == 0) {
645		error = 0;
646		goto out;
647	}
648
649	if (ioflag & IO_APPEND)
650		uio->uio_offset = node->tn_size;
651
652	if (uio->uio_offset + uio->uio_resid >
653	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
654		return (EFBIG);
655
656	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
657		return (EFBIG);
658
659	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
660	if (extended) {
661		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
662		    FALSE);
663		if (error != 0)
664			goto out;
665	}
666
667	uobj = node->tn_reg.tn_aobj;
668	while ((resid = uio->uio_resid) > 0) {
669		if (node->tn_size <= uio->uio_offset)
670			break;
671		len = MIN(node->tn_size - uio->uio_offset, resid);
672		if (len == 0)
673			break;
674		error = tmpfs_mappedwrite(uobj, len, uio);
675		if ((error != 0) || (resid == uio->uio_resid))
676			break;
677	}
678
679	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
680	    (extended ? TMPFS_NODE_CHANGED : 0);
681
682	if (node->tn_mode & (S_ISUID | S_ISGID)) {
683		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
684			node->tn_mode &= ~(S_ISUID | S_ISGID);
685	}
686
687	if (error != 0)
688		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
689
690out:
691	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
692	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
693
694	return error;
695}
696
697/* --------------------------------------------------------------------- */
698
699static int
700tmpfs_fsync(struct vop_fsync_args *v)
701{
702	struct vnode *vp = v->a_vp;
703
704	MPASS(VOP_ISLOCKED(vp));
705
706	tmpfs_update(vp);
707
708	return 0;
709}
710
711/* --------------------------------------------------------------------- */
712
713static int
714tmpfs_remove(struct vop_remove_args *v)
715{
716	struct vnode *dvp = v->a_dvp;
717	struct vnode *vp = v->a_vp;
718
719	int error;
720	struct tmpfs_dirent *de;
721	struct tmpfs_mount *tmp;
722	struct tmpfs_node *dnode;
723	struct tmpfs_node *node;
724
725	MPASS(VOP_ISLOCKED(dvp));
726	MPASS(VOP_ISLOCKED(vp));
727
728	if (vp->v_type == VDIR) {
729		error = EISDIR;
730		goto out;
731	}
732
733	dnode = VP_TO_TMPFS_DIR(dvp);
734	node = VP_TO_TMPFS_NODE(vp);
735	tmp = VFS_TO_TMPFS(vp->v_mount);
736	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
737	MPASS(de != NULL);
738
739	/* Files marked as immutable or append-only cannot be deleted. */
740	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
741	    (dnode->tn_flags & APPEND)) {
742		error = EPERM;
743		goto out;
744	}
745
746	/* Remove the entry from the directory; as it is a file, we do not
747	 * have to change the number of hard links of the directory. */
748	tmpfs_dir_detach(dvp, de);
749	if (v->a_cnp->cn_flags & DOWHITEOUT)
750		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
751
752	/* Free the directory entry we just deleted.  Note that the node
753	 * referred by it will not be removed until the vnode is really
754	 * reclaimed. */
755	tmpfs_free_dirent(tmp, de);
756
757	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
758	error = 0;
759
760out:
761
762	return error;
763}
764
765/* --------------------------------------------------------------------- */
766
767static int
768tmpfs_link(struct vop_link_args *v)
769{
770	struct vnode *dvp = v->a_tdvp;
771	struct vnode *vp = v->a_vp;
772	struct componentname *cnp = v->a_cnp;
773
774	int error;
775	struct tmpfs_dirent *de;
776	struct tmpfs_node *node;
777
778	MPASS(VOP_ISLOCKED(dvp));
779	MPASS(cnp->cn_flags & HASBUF);
780	MPASS(dvp != vp); /* XXX When can this be false? */
781
782	node = VP_TO_TMPFS_NODE(vp);
783
784	/* XXX: Why aren't the following two tests done by the caller? */
785
786	/* Hard links of directories are forbidden. */
787	if (vp->v_type == VDIR) {
788		error = EPERM;
789		goto out;
790	}
791
792	/* Cannot create cross-device links. */
793	if (dvp->v_mount != vp->v_mount) {
794		error = EXDEV;
795		goto out;
796	}
797
798	/* Ensure that we do not overflow the maximum number of links imposed
799	 * by the system. */
800	MPASS(node->tn_links <= LINK_MAX);
801	if (node->tn_links == LINK_MAX) {
802		error = EMLINK;
803		goto out;
804	}
805
806	/* We cannot create links of files marked immutable or append-only. */
807	if (node->tn_flags & (IMMUTABLE | APPEND)) {
808		error = EPERM;
809		goto out;
810	}
811
812	/* Allocate a new directory entry to represent the node. */
813	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
814	    cnp->cn_nameptr, cnp->cn_namelen, &de);
815	if (error != 0)
816		goto out;
817
818	/* Insert the new directory entry into the appropriate directory. */
819	if (cnp->cn_flags & ISWHITEOUT)
820		tmpfs_dir_whiteout_remove(dvp, cnp);
821	tmpfs_dir_attach(dvp, de);
822
823	/* vp link count has changed, so update node times. */
824	node->tn_status |= TMPFS_NODE_CHANGED;
825	tmpfs_update(vp);
826
827	error = 0;
828
829out:
830	return error;
831}
832
833/* --------------------------------------------------------------------- */
834
835/*
836 * We acquire all but fdvp locks using non-blocking acquisitions.  If we
837 * fail to acquire any lock in the path we will drop all held locks,
838 * acquire the new lock in a blocking fashion, and then release it and
839 * restart the rename.  This acquire/release step ensures that we do not
840 * spin on a lock waiting for release.  On error release all vnode locks
841 * and decrement references the way tmpfs_rename() would do.
842 */
843static int
844tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
845    struct vnode *tdvp, struct vnode **tvpp,
846    struct componentname *fcnp, struct componentname *tcnp)
847{
848	struct vnode *nvp;
849	struct mount *mp;
850	struct tmpfs_dirent *de;
851	int error, restarts = 0;
852
853	VOP_UNLOCK(tdvp, 0);
854	if (*tvpp != NULL && *tvpp != tdvp)
855		VOP_UNLOCK(*tvpp, 0);
856	mp = fdvp->v_mount;
857
858relock:
859	restarts += 1;
860	error = vn_lock(fdvp, LK_EXCLUSIVE);
861	if (error)
862		goto releout;
863	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
864		VOP_UNLOCK(fdvp, 0);
865		error = vn_lock(tdvp, LK_EXCLUSIVE);
866		if (error)
867			goto releout;
868		VOP_UNLOCK(tdvp, 0);
869		goto relock;
870	}
871	/*
872	 * Re-resolve fvp to be certain it still exists and fetch the
873	 * correct vnode.
874	 */
875	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
876	if (de == NULL) {
877		VOP_UNLOCK(fdvp, 0);
878		VOP_UNLOCK(tdvp, 0);
879		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
880		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
881			error = EINVAL;
882		else
883			error = ENOENT;
884		goto releout;
885	}
886	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
887	if (error != 0) {
888		VOP_UNLOCK(fdvp, 0);
889		VOP_UNLOCK(tdvp, 0);
890		if (error != EBUSY)
891			goto releout;
892		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
893		if (error != 0)
894			goto releout;
895		VOP_UNLOCK(nvp, 0);
896		/*
897		 * Concurrent rename race.
898		 */
899		if (nvp == tdvp) {
900			vrele(nvp);
901			error = EINVAL;
902			goto releout;
903		}
904		vrele(*fvpp);
905		*fvpp = nvp;
906		goto relock;
907	}
908	vrele(*fvpp);
909	*fvpp = nvp;
910	VOP_UNLOCK(*fvpp, 0);
911	/*
912	 * Re-resolve tvp and acquire the vnode lock if present.
913	 */
914	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
915	/*
916	 * If tvp disappeared we just carry on.
917	 */
918	if (de == NULL && *tvpp != NULL) {
919		vrele(*tvpp);
920		*tvpp = NULL;
921	}
922	/*
923	 * Get the tvp ino if the lookup succeeded.  We may have to restart
924	 * if the non-blocking acquire fails.
925	 */
926	if (de != NULL) {
927		nvp = NULL;
928		error = tmpfs_alloc_vp(mp, de->td_node,
929		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
930		if (*tvpp != NULL)
931			vrele(*tvpp);
932		*tvpp = nvp;
933		if (error != 0) {
934			VOP_UNLOCK(fdvp, 0);
935			VOP_UNLOCK(tdvp, 0);
936			if (error != EBUSY)
937				goto releout;
938			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
939			    &nvp);
940			if (error != 0)
941				goto releout;
942			VOP_UNLOCK(nvp, 0);
943			/*
944			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
945			 */
946			if (nvp == fdvp) {
947				error = ENOTEMPTY;
948				goto releout;
949			}
950			goto relock;
951		}
952	}
953	tmpfs_rename_restarts += restarts;
954
955	return (0);
956
957releout:
958	vrele(fdvp);
959	vrele(*fvpp);
960	vrele(tdvp);
961	if (*tvpp != NULL)
962		vrele(*tvpp);
963	tmpfs_rename_restarts += restarts;
964
965	return (error);
966}
967
968static int
969tmpfs_rename(struct vop_rename_args *v)
970{
971	struct vnode *fdvp = v->a_fdvp;
972	struct vnode *fvp = v->a_fvp;
973	struct componentname *fcnp = v->a_fcnp;
974	struct vnode *tdvp = v->a_tdvp;
975	struct vnode *tvp = v->a_tvp;
976	struct componentname *tcnp = v->a_tcnp;
977	struct mount *mp = NULL;
978
979	char *newname;
980	int error;
981	struct tmpfs_dirent *de;
982	struct tmpfs_mount *tmp;
983	struct tmpfs_node *fdnode;
984	struct tmpfs_node *fnode;
985	struct tmpfs_node *tnode;
986	struct tmpfs_node *tdnode;
987
988	MPASS(VOP_ISLOCKED(tdvp));
989	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
990	MPASS(fcnp->cn_flags & HASBUF);
991	MPASS(tcnp->cn_flags & HASBUF);
992
993	/* Disallow cross-device renames.
994	 * XXX Why isn't this done by the caller? */
995	if (fvp->v_mount != tdvp->v_mount ||
996	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
997		error = EXDEV;
998		goto out;
999	}
1000
1001	/* If source and target are the same file, there is nothing to do. */
1002	if (fvp == tvp) {
1003		error = 0;
1004		goto out;
1005	}
1006
1007	/* If we need to move the directory between entries, lock the
1008	 * source so that we can safely operate on it. */
1009	if (fdvp != tdvp && fdvp != tvp) {
1010		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1011			mp = tdvp->v_mount;
1012			error = vfs_busy(mp, 0);
1013			if (error != 0) {
1014				mp = NULL;
1015				goto out;
1016			}
1017			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1018			    fcnp, tcnp);
1019			if (error != 0) {
1020				vfs_unbusy(mp);
1021				return (error);
1022			}
1023			ASSERT_VOP_ELOCKED(fdvp,
1024			    "tmpfs_rename: fdvp not locked");
1025			ASSERT_VOP_ELOCKED(tdvp,
1026			    "tmpfs_rename: tdvp not locked");
1027			if (tvp != NULL)
1028				ASSERT_VOP_ELOCKED(tvp,
1029				    "tmpfs_rename: tvp not locked");
1030			if (fvp == tvp) {
1031				error = 0;
1032				goto out_locked;
1033			}
1034		}
1035	}
1036
1037	tmp = VFS_TO_TMPFS(tdvp->v_mount);
1038	tdnode = VP_TO_TMPFS_DIR(tdvp);
1039	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1040	fdnode = VP_TO_TMPFS_DIR(fdvp);
1041	fnode = VP_TO_TMPFS_NODE(fvp);
1042	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1043
1044	/* Entry can disappear before we lock fdvp,
1045	 * also avoid manipulating '.' and '..' entries. */
1046	if (de == NULL) {
1047		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1048		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1049			error = EINVAL;
1050		else
1051			error = ENOENT;
1052		goto out_locked;
1053	}
1054	MPASS(de->td_node == fnode);
1055
1056	/* If re-naming a directory to another preexisting directory
1057	 * ensure that the target directory is empty so that its
1058	 * removal causes no side effects.
1059	 * Kern_rename gurantees the destination to be a directory
1060	 * if the source is one. */
1061	if (tvp != NULL) {
1062		MPASS(tnode != NULL);
1063
1064		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1065		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1066			error = EPERM;
1067			goto out_locked;
1068		}
1069
1070		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1071			if (tnode->tn_size > 0) {
1072				error = ENOTEMPTY;
1073				goto out_locked;
1074			}
1075		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1076			error = ENOTDIR;
1077			goto out_locked;
1078		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1079			error = EISDIR;
1080			goto out_locked;
1081		} else {
1082			MPASS(fnode->tn_type != VDIR &&
1083				tnode->tn_type != VDIR);
1084		}
1085	}
1086
1087	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1088	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1089		error = EPERM;
1090		goto out_locked;
1091	}
1092
1093	/* Ensure that we have enough memory to hold the new name, if it
1094	 * has to be changed. */
1095	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1096	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1097		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1098	} else
1099		newname = NULL;
1100
1101	/* If the node is being moved to another directory, we have to do
1102	 * the move. */
1103	if (fdnode != tdnode) {
1104		/* In case we are moving a directory, we have to adjust its
1105		 * parent to point to the new parent. */
1106		if (de->td_node->tn_type == VDIR) {
1107			struct tmpfs_node *n;
1108
1109			/* Ensure the target directory is not a child of the
1110			 * directory being moved.  Otherwise, we'd end up
1111			 * with stale nodes. */
1112			n = tdnode;
1113			/* TMPFS_LOCK garanties that no nodes are freed while
1114			 * traversing the list. Nodes can only be marked as
1115			 * removed: tn_parent == NULL. */
1116			TMPFS_LOCK(tmp);
1117			TMPFS_NODE_LOCK(n);
1118			while (n != n->tn_dir.tn_parent) {
1119				struct tmpfs_node *parent;
1120
1121				if (n == fnode) {
1122					TMPFS_NODE_UNLOCK(n);
1123					TMPFS_UNLOCK(tmp);
1124					error = EINVAL;
1125					if (newname != NULL)
1126						    free(newname, M_TMPFSNAME);
1127					goto out_locked;
1128				}
1129				parent = n->tn_dir.tn_parent;
1130				TMPFS_NODE_UNLOCK(n);
1131				if (parent == NULL) {
1132					n = NULL;
1133					break;
1134				}
1135				TMPFS_NODE_LOCK(parent);
1136				if (parent->tn_dir.tn_parent == NULL) {
1137					TMPFS_NODE_UNLOCK(parent);
1138					n = NULL;
1139					break;
1140				}
1141				n = parent;
1142			}
1143			TMPFS_UNLOCK(tmp);
1144			if (n == NULL) {
1145				error = EINVAL;
1146				if (newname != NULL)
1147					    free(newname, M_TMPFSNAME);
1148				goto out_locked;
1149			}
1150			TMPFS_NODE_UNLOCK(n);
1151
1152			/* Adjust the parent pointer. */
1153			TMPFS_VALIDATE_DIR(fnode);
1154			TMPFS_NODE_LOCK(de->td_node);
1155			de->td_node->tn_dir.tn_parent = tdnode;
1156			TMPFS_NODE_UNLOCK(de->td_node);
1157
1158			/* As a result of changing the target of the '..'
1159			 * entry, the link count of the source and target
1160			 * directories has to be adjusted. */
1161			TMPFS_NODE_LOCK(tdnode);
1162			TMPFS_ASSERT_LOCKED(tdnode);
1163			tdnode->tn_links++;
1164			TMPFS_NODE_UNLOCK(tdnode);
1165
1166			TMPFS_NODE_LOCK(fdnode);
1167			TMPFS_ASSERT_LOCKED(fdnode);
1168			fdnode->tn_links--;
1169			TMPFS_NODE_UNLOCK(fdnode);
1170		}
1171	}
1172
1173	/* Do the move: just remove the entry from the source directory
1174	 * and insert it into the target one. */
1175	tmpfs_dir_detach(fdvp, de);
1176
1177	if (fcnp->cn_flags & DOWHITEOUT)
1178		tmpfs_dir_whiteout_add(fdvp, fcnp);
1179	if (tcnp->cn_flags & ISWHITEOUT)
1180		tmpfs_dir_whiteout_remove(tdvp, tcnp);
1181
1182	/* If the name has changed, we need to make it effective by changing
1183	 * it in the directory entry. */
1184	if (newname != NULL) {
1185		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1186
1187		free(de->ud.td_name, M_TMPFSNAME);
1188		de->ud.td_name = newname;
1189		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1190
1191		fnode->tn_status |= TMPFS_NODE_CHANGED;
1192		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1193	}
1194
1195	/* If we are overwriting an entry, we have to remove the old one
1196	 * from the target directory. */
1197	if (tvp != NULL) {
1198		struct tmpfs_dirent *tde;
1199
1200		/* Remove the old entry from the target directory. */
1201		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1202		tmpfs_dir_detach(tdvp, tde);
1203
1204		/* Free the directory entry we just deleted.  Note that the
1205		 * node referred by it will not be removed until the vnode is
1206		 * really reclaimed. */
1207		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1208	}
1209
1210	tmpfs_dir_attach(tdvp, de);
1211
1212	cache_purge(fvp);
1213	if (tvp != NULL)
1214		cache_purge(tvp);
1215	cache_purge_negative(tdvp);
1216
1217	error = 0;
1218
1219out_locked:
1220	if (fdvp != tdvp && fdvp != tvp)
1221		VOP_UNLOCK(fdvp, 0);
1222
1223out:
1224	/* Release target nodes. */
1225	/* XXX: I don't understand when tdvp can be the same as tvp, but
1226	 * other code takes care of this... */
1227	if (tdvp == tvp)
1228		vrele(tdvp);
1229	else
1230		vput(tdvp);
1231	if (tvp != NULL)
1232		vput(tvp);
1233
1234	/* Release source nodes. */
1235	vrele(fdvp);
1236	vrele(fvp);
1237
1238	if (mp != NULL)
1239		vfs_unbusy(mp);
1240
1241	return error;
1242}
1243
1244/* --------------------------------------------------------------------- */
1245
1246static int
1247tmpfs_mkdir(struct vop_mkdir_args *v)
1248{
1249	struct vnode *dvp = v->a_dvp;
1250	struct vnode **vpp = v->a_vpp;
1251	struct componentname *cnp = v->a_cnp;
1252	struct vattr *vap = v->a_vap;
1253
1254	MPASS(vap->va_type == VDIR);
1255
1256	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1257}
1258
1259/* --------------------------------------------------------------------- */
1260
1261static int
1262tmpfs_rmdir(struct vop_rmdir_args *v)
1263{
1264	struct vnode *dvp = v->a_dvp;
1265	struct vnode *vp = v->a_vp;
1266
1267	int error;
1268	struct tmpfs_dirent *de;
1269	struct tmpfs_mount *tmp;
1270	struct tmpfs_node *dnode;
1271	struct tmpfs_node *node;
1272
1273	MPASS(VOP_ISLOCKED(dvp));
1274	MPASS(VOP_ISLOCKED(vp));
1275
1276	tmp = VFS_TO_TMPFS(dvp->v_mount);
1277	dnode = VP_TO_TMPFS_DIR(dvp);
1278	node = VP_TO_TMPFS_DIR(vp);
1279
1280	/* Directories with more than two entries ('.' and '..') cannot be
1281	 * removed. */
1282	 if (node->tn_size > 0) {
1283		 error = ENOTEMPTY;
1284		 goto out;
1285	 }
1286
1287	if ((dnode->tn_flags & APPEND)
1288	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1289		error = EPERM;
1290		goto out;
1291	}
1292
1293	/* This invariant holds only if we are not trying to remove "..".
1294	  * We checked for that above so this is safe now. */
1295	MPASS(node->tn_dir.tn_parent == dnode);
1296
1297	/* Get the directory entry associated with node (vp).  This was
1298	 * filled by tmpfs_lookup while looking up the entry. */
1299	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1300	MPASS(TMPFS_DIRENT_MATCHES(de,
1301	    v->a_cnp->cn_nameptr,
1302	    v->a_cnp->cn_namelen));
1303
1304	/* Check flags to see if we are allowed to remove the directory. */
1305	if (dnode->tn_flags & APPEND
1306		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1307		error = EPERM;
1308		goto out;
1309	}
1310
1311
1312	/* Detach the directory entry from the directory (dnode). */
1313	tmpfs_dir_detach(dvp, de);
1314	if (v->a_cnp->cn_flags & DOWHITEOUT)
1315		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1316
1317	/* No vnode should be allocated for this entry from this point */
1318	TMPFS_NODE_LOCK(node);
1319	TMPFS_ASSERT_ELOCKED(node);
1320	node->tn_links--;
1321	node->tn_dir.tn_parent = NULL;
1322	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1323	    TMPFS_NODE_MODIFIED;
1324
1325	TMPFS_NODE_UNLOCK(node);
1326
1327	TMPFS_NODE_LOCK(dnode);
1328	TMPFS_ASSERT_ELOCKED(dnode);
1329	dnode->tn_links--;
1330	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1331	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1332	TMPFS_NODE_UNLOCK(dnode);
1333
1334	cache_purge(dvp);
1335	cache_purge(vp);
1336
1337	/* Free the directory entry we just deleted.  Note that the node
1338	 * referred by it will not be removed until the vnode is really
1339	 * reclaimed. */
1340	tmpfs_free_dirent(tmp, de);
1341
1342	/* Release the deleted vnode (will destroy the node, notify
1343	 * interested parties and clean it from the cache). */
1344
1345	dnode->tn_status |= TMPFS_NODE_CHANGED;
1346	tmpfs_update(dvp);
1347
1348	error = 0;
1349
1350out:
1351	return error;
1352}
1353
1354/* --------------------------------------------------------------------- */
1355
1356static int
1357tmpfs_symlink(struct vop_symlink_args *v)
1358{
1359	struct vnode *dvp = v->a_dvp;
1360	struct vnode **vpp = v->a_vpp;
1361	struct componentname *cnp = v->a_cnp;
1362	struct vattr *vap = v->a_vap;
1363	char *target = v->a_target;
1364
1365#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1366	MPASS(vap->va_type == VLNK);
1367#else
1368	vap->va_type = VLNK;
1369#endif
1370
1371	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1372}
1373
1374/* --------------------------------------------------------------------- */
1375
1376static int
1377tmpfs_readdir(struct vop_readdir_args *v)
1378{
1379	struct vnode *vp = v->a_vp;
1380	struct uio *uio = v->a_uio;
1381	int *eofflag = v->a_eofflag;
1382	u_long **cookies = v->a_cookies;
1383	int *ncookies = v->a_ncookies;
1384
1385	int error;
1386	ssize_t startresid;
1387	int cnt = 0;
1388	struct tmpfs_node *node;
1389
1390	/* This operation only makes sense on directory nodes. */
1391	if (vp->v_type != VDIR)
1392		return ENOTDIR;
1393
1394	node = VP_TO_TMPFS_DIR(vp);
1395
1396	startresid = uio->uio_resid;
1397
1398	if (cookies != NULL && ncookies != NULL) {
1399		cnt = howmany(node->tn_size, sizeof(struct tmpfs_dirent)) + 2;
1400		*cookies = malloc(cnt * sizeof(**cookies), M_TEMP, M_WAITOK);
1401		*ncookies = 0;
1402	}
1403
1404	if (cnt == 0)
1405		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1406	else
1407		error = tmpfs_dir_getdents(node, uio, cnt, *cookies, ncookies);
1408
1409	if (error == EJUSTRETURN)
1410		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1411
1412	if (error != 0 && cnt != 0)
1413		free(*cookies, M_TEMP);
1414
1415	if (eofflag != NULL)
1416		*eofflag =
1417		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1418
1419	return error;
1420}
1421
1422/* --------------------------------------------------------------------- */
1423
1424static int
1425tmpfs_readlink(struct vop_readlink_args *v)
1426{
1427	struct vnode *vp = v->a_vp;
1428	struct uio *uio = v->a_uio;
1429
1430	int error;
1431	struct tmpfs_node *node;
1432
1433	MPASS(uio->uio_offset == 0);
1434	MPASS(vp->v_type == VLNK);
1435
1436	node = VP_TO_TMPFS_NODE(vp);
1437
1438	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1439	    uio);
1440	node->tn_status |= TMPFS_NODE_ACCESSED;
1441
1442	return error;
1443}
1444
1445/* --------------------------------------------------------------------- */
1446
1447static int
1448tmpfs_inactive(struct vop_inactive_args *v)
1449{
1450	struct vnode *vp = v->a_vp;
1451
1452	struct tmpfs_node *node;
1453
1454	node = VP_TO_TMPFS_NODE(vp);
1455
1456	if (node->tn_links == 0)
1457		vrecycle(vp);
1458
1459	return 0;
1460}
1461
1462/* --------------------------------------------------------------------- */
1463
1464int
1465tmpfs_reclaim(struct vop_reclaim_args *v)
1466{
1467	struct vnode *vp = v->a_vp;
1468
1469	struct tmpfs_mount *tmp;
1470	struct tmpfs_node *node;
1471
1472	node = VP_TO_TMPFS_NODE(vp);
1473	tmp = VFS_TO_TMPFS(vp->v_mount);
1474
1475	if (vp->v_type == VREG)
1476		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1477	vp->v_object = NULL;
1478	cache_purge(vp);
1479
1480	TMPFS_NODE_LOCK(node);
1481	TMPFS_ASSERT_ELOCKED(node);
1482	tmpfs_free_vp(vp);
1483
1484	/* If the node referenced by this vnode was deleted by the user,
1485	 * we must free its associated data structures (now that the vnode
1486	 * is being reclaimed). */
1487	if (node->tn_links == 0 &&
1488	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1489		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1490		TMPFS_NODE_UNLOCK(node);
1491		tmpfs_free_node(tmp, node);
1492	} else
1493		TMPFS_NODE_UNLOCK(node);
1494
1495	MPASS(vp->v_data == NULL);
1496	return 0;
1497}
1498
1499/* --------------------------------------------------------------------- */
1500
1501static int
1502tmpfs_print(struct vop_print_args *v)
1503{
1504	struct vnode *vp = v->a_vp;
1505
1506	struct tmpfs_node *node;
1507
1508	node = VP_TO_TMPFS_NODE(vp);
1509
1510	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1511	    node, node->tn_flags, node->tn_links);
1512	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1513	    node->tn_mode, node->tn_uid, node->tn_gid,
1514	    (intmax_t)node->tn_size, node->tn_status);
1515
1516	if (vp->v_type == VFIFO)
1517		fifo_printinfo(vp);
1518
1519	printf("\n");
1520
1521	return 0;
1522}
1523
1524/* --------------------------------------------------------------------- */
1525
1526static int
1527tmpfs_pathconf(struct vop_pathconf_args *v)
1528{
1529	int name = v->a_name;
1530	register_t *retval = v->a_retval;
1531
1532	int error;
1533
1534	error = 0;
1535
1536	switch (name) {
1537	case _PC_LINK_MAX:
1538		*retval = LINK_MAX;
1539		break;
1540
1541	case _PC_NAME_MAX:
1542		*retval = NAME_MAX;
1543		break;
1544
1545	case _PC_PATH_MAX:
1546		*retval = PATH_MAX;
1547		break;
1548
1549	case _PC_PIPE_BUF:
1550		*retval = PIPE_BUF;
1551		break;
1552
1553	case _PC_CHOWN_RESTRICTED:
1554		*retval = 1;
1555		break;
1556
1557	case _PC_NO_TRUNC:
1558		*retval = 1;
1559		break;
1560
1561	case _PC_SYNC_IO:
1562		*retval = 1;
1563		break;
1564
1565	case _PC_FILESIZEBITS:
1566		*retval = 0; /* XXX Don't know which value should I return. */
1567		break;
1568
1569	default:
1570		error = EINVAL;
1571	}
1572
1573	return error;
1574}
1575
1576static int
1577tmpfs_vptofh(struct vop_vptofh_args *ap)
1578{
1579	struct tmpfs_fid *tfhp;
1580	struct tmpfs_node *node;
1581
1582	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1583	node = VP_TO_TMPFS_NODE(ap->a_vp);
1584
1585	tfhp->tf_len = sizeof(struct tmpfs_fid);
1586	tfhp->tf_id = node->tn_id;
1587	tfhp->tf_gen = node->tn_gen;
1588
1589	return (0);
1590}
1591
1592static int
1593tmpfs_whiteout(struct vop_whiteout_args *ap)
1594{
1595	struct vnode *dvp = ap->a_dvp;
1596	struct componentname *cnp = ap->a_cnp;
1597	struct tmpfs_dirent *de;
1598
1599	switch (ap->a_flags) {
1600	case LOOKUP:
1601		return (0);
1602	case CREATE:
1603		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1604		if (de != NULL)
1605			return (de->td_node == NULL ? 0 : EEXIST);
1606		return (tmpfs_dir_whiteout_add(dvp, cnp));
1607	case DELETE:
1608		tmpfs_dir_whiteout_remove(dvp, cnp);
1609		return (0);
1610	default:
1611		panic("tmpfs_whiteout: unknown op");
1612	}
1613}
1614
1615/* --------------------------------------------------------------------- */
1616
1617/*
1618 * vnode operations vector used for files stored in a tmpfs file system.
1619 */
1620struct vop_vector tmpfs_vnodeop_entries = {
1621	.vop_default =			&default_vnodeops,
1622	.vop_lookup =			vfs_cache_lookup,
1623	.vop_cachedlookup =		tmpfs_lookup,
1624	.vop_create =			tmpfs_create,
1625	.vop_mknod =			tmpfs_mknod,
1626	.vop_open =			tmpfs_open,
1627	.vop_close =			tmpfs_close,
1628	.vop_access =			tmpfs_access,
1629	.vop_getattr =			tmpfs_getattr,
1630	.vop_setattr =			tmpfs_setattr,
1631	.vop_read =			tmpfs_read,
1632	.vop_write =			tmpfs_write,
1633	.vop_fsync =			tmpfs_fsync,
1634	.vop_remove =			tmpfs_remove,
1635	.vop_link =			tmpfs_link,
1636	.vop_rename =			tmpfs_rename,
1637	.vop_mkdir =			tmpfs_mkdir,
1638	.vop_rmdir =			tmpfs_rmdir,
1639	.vop_symlink =			tmpfs_symlink,
1640	.vop_readdir =			tmpfs_readdir,
1641	.vop_readlink =			tmpfs_readlink,
1642	.vop_inactive =			tmpfs_inactive,
1643	.vop_reclaim =			tmpfs_reclaim,
1644	.vop_print =			tmpfs_print,
1645	.vop_pathconf =			tmpfs_pathconf,
1646	.vop_vptofh =			tmpfs_vptofh,
1647	.vop_whiteout =			tmpfs_whiteout,
1648	.vop_bmap =			VOP_EOPNOTSUPP,
1649};
1650
1651