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