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