tmpfs_vnops.c revision 207719
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 207719 2010-05-06 18:43:19Z 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
721	boolean_t extended;
722	int error = 0;
723	off_t oldsize;
724	struct tmpfs_node *node;
725	vm_object_t uobj;
726	size_t len;
727	int resid;
728
729	node = VP_TO_TMPFS_NODE(vp);
730	oldsize = node->tn_size;
731
732	if (uio->uio_offset < 0 || vp->v_type != VREG) {
733		error = EINVAL;
734		goto out;
735	}
736
737	if (uio->uio_resid == 0) {
738		error = 0;
739		goto out;
740	}
741
742	if (ioflag & IO_APPEND)
743		uio->uio_offset = node->tn_size;
744
745	if (uio->uio_offset + uio->uio_resid >
746	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
747		return (EFBIG);
748
749	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
750		return (EFBIG);
751
752	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
753	if (extended) {
754		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
755		if (error != 0)
756			goto out;
757	}
758
759	uobj = node->tn_reg.tn_aobj;
760	while ((resid = uio->uio_resid) > 0) {
761		if (node->tn_size <= uio->uio_offset)
762			break;
763		len = MIN(node->tn_size - uio->uio_offset, resid);
764		if (len == 0)
765			break;
766		error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
767		if ((error != 0) || (resid == uio->uio_resid))
768			break;
769	}
770
771	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
772	    (extended ? TMPFS_NODE_CHANGED : 0);
773
774	if (node->tn_mode & (S_ISUID | S_ISGID)) {
775		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
776			node->tn_mode &= ~(S_ISUID | S_ISGID);
777	}
778
779	if (error != 0)
780		(void)tmpfs_reg_resize(vp, oldsize);
781
782out:
783	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
784	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
785
786	return error;
787}
788
789/* --------------------------------------------------------------------- */
790
791static int
792tmpfs_fsync(struct vop_fsync_args *v)
793{
794	struct vnode *vp = v->a_vp;
795
796	MPASS(VOP_ISLOCKED(vp));
797
798	tmpfs_update(vp);
799
800	return 0;
801}
802
803/* --------------------------------------------------------------------- */
804
805static int
806tmpfs_remove(struct vop_remove_args *v)
807{
808	struct vnode *dvp = v->a_dvp;
809	struct vnode *vp = v->a_vp;
810
811	int error;
812	struct tmpfs_dirent *de;
813	struct tmpfs_mount *tmp;
814	struct tmpfs_node *dnode;
815	struct tmpfs_node *node;
816
817	MPASS(VOP_ISLOCKED(dvp));
818	MPASS(VOP_ISLOCKED(vp));
819
820	if (vp->v_type == VDIR) {
821		error = EISDIR;
822		goto out;
823	}
824
825	dnode = VP_TO_TMPFS_DIR(dvp);
826	node = VP_TO_TMPFS_NODE(vp);
827	tmp = VFS_TO_TMPFS(vp->v_mount);
828	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
829	MPASS(de != NULL);
830
831	/* Files marked as immutable or append-only cannot be deleted. */
832	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
833	    (dnode->tn_flags & APPEND)) {
834		error = EPERM;
835		goto out;
836	}
837
838	/* Remove the entry from the directory; as it is a file, we do not
839	 * have to change the number of hard links of the directory. */
840	tmpfs_dir_detach(dvp, de);
841
842	/* Free the directory entry we just deleted.  Note that the node
843	 * referred by it will not be removed until the vnode is really
844	 * reclaimed. */
845	tmpfs_free_dirent(tmp, de, TRUE);
846
847	if (node->tn_links > 0)
848		node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
849	    TMPFS_NODE_MODIFIED;
850	error = 0;
851
852out:
853
854	return error;
855}
856
857/* --------------------------------------------------------------------- */
858
859static int
860tmpfs_link(struct vop_link_args *v)
861{
862	struct vnode *dvp = v->a_tdvp;
863	struct vnode *vp = v->a_vp;
864	struct componentname *cnp = v->a_cnp;
865
866	int error;
867	struct tmpfs_dirent *de;
868	struct tmpfs_node *node;
869
870	MPASS(VOP_ISLOCKED(dvp));
871	MPASS(cnp->cn_flags & HASBUF);
872	MPASS(dvp != vp); /* XXX When can this be false? */
873
874	node = VP_TO_TMPFS_NODE(vp);
875
876	/* XXX: Why aren't the following two tests done by the caller? */
877
878	/* Hard links of directories are forbidden. */
879	if (vp->v_type == VDIR) {
880		error = EPERM;
881		goto out;
882	}
883
884	/* Cannot create cross-device links. */
885	if (dvp->v_mount != vp->v_mount) {
886		error = EXDEV;
887		goto out;
888	}
889
890	/* Ensure that we do not overflow the maximum number of links imposed
891	 * by the system. */
892	MPASS(node->tn_links <= LINK_MAX);
893	if (node->tn_links == LINK_MAX) {
894		error = EMLINK;
895		goto out;
896	}
897
898	/* We cannot create links of files marked immutable or append-only. */
899	if (node->tn_flags & (IMMUTABLE | APPEND)) {
900		error = EPERM;
901		goto out;
902	}
903
904	/* Allocate a new directory entry to represent the node. */
905	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
906	    cnp->cn_nameptr, cnp->cn_namelen, &de);
907	if (error != 0)
908		goto out;
909
910	/* Insert the new directory entry into the appropriate directory. */
911	tmpfs_dir_attach(dvp, de);
912
913	/* vp link count has changed, so update node times. */
914	node->tn_status |= TMPFS_NODE_CHANGED;
915	tmpfs_update(vp);
916
917	error = 0;
918
919out:
920	return error;
921}
922
923/* --------------------------------------------------------------------- */
924
925static int
926tmpfs_rename(struct vop_rename_args *v)
927{
928	struct vnode *fdvp = v->a_fdvp;
929	struct vnode *fvp = v->a_fvp;
930	struct componentname *fcnp = v->a_fcnp;
931	struct vnode *tdvp = v->a_tdvp;
932	struct vnode *tvp = v->a_tvp;
933	struct componentname *tcnp = v->a_tcnp;
934
935	char *newname;
936	int error;
937	struct tmpfs_dirent *de;
938	struct tmpfs_mount *tmp;
939	struct tmpfs_node *fdnode;
940	struct tmpfs_node *fnode;
941	struct tmpfs_node *tnode;
942	struct tmpfs_node *tdnode;
943
944	MPASS(VOP_ISLOCKED(tdvp));
945	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
946	MPASS(fcnp->cn_flags & HASBUF);
947	MPASS(tcnp->cn_flags & HASBUF);
948
949  	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
950
951	/* Disallow cross-device renames.
952	 * XXX Why isn't this done by the caller? */
953	if (fvp->v_mount != tdvp->v_mount ||
954	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
955		error = EXDEV;
956		goto out;
957	}
958
959	tmp = VFS_TO_TMPFS(tdvp->v_mount);
960	tdnode = VP_TO_TMPFS_DIR(tdvp);
961
962	/* If source and target are the same file, there is nothing to do. */
963	if (fvp == tvp) {
964		error = 0;
965		goto out;
966	}
967
968	/* If we need to move the directory between entries, lock the
969	 * source so that we can safely operate on it. */
970	if (tdvp != fdvp) {
971		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
972		if (error != 0)
973			goto out;
974	}
975	fdnode = VP_TO_TMPFS_DIR(fdvp);
976	fnode = VP_TO_TMPFS_NODE(fvp);
977	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
978
979	/* Avoid manipulating '.' and '..' entries. */
980	if (de == NULL) {
981		MPASS(fvp->v_type == VDIR);
982		error = EINVAL;
983		goto out_locked;
984	}
985	MPASS(de->td_node == fnode);
986
987	/* If re-naming a directory to another preexisting directory
988	 * ensure that the target directory is empty so that its
989	 * removal causes no side effects.
990	 * Kern_rename gurantees the destination to be a directory
991	 * if the source is one. */
992	if (tvp != NULL) {
993		MPASS(tnode != NULL);
994
995		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
996		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
997			error = EPERM;
998			goto out_locked;
999		}
1000
1001		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1002			if (tnode->tn_size > 0) {
1003				error = ENOTEMPTY;
1004				goto out_locked;
1005			}
1006		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1007			error = ENOTDIR;
1008			goto out_locked;
1009		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1010			error = EISDIR;
1011			goto out_locked;
1012		} else {
1013			MPASS(fnode->tn_type != VDIR &&
1014				tnode->tn_type != VDIR);
1015		}
1016	}
1017
1018	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1019	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1020		error = EPERM;
1021		goto out_locked;
1022	}
1023
1024	/* Ensure that we have enough memory to hold the new name, if it
1025	 * has to be changed. */
1026	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1027	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1028		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1029	} else
1030		newname = NULL;
1031
1032	/* If the node is being moved to another directory, we have to do
1033	 * the move. */
1034	if (fdnode != tdnode) {
1035		/* In case we are moving a directory, we have to adjust its
1036		 * parent to point to the new parent. */
1037		if (de->td_node->tn_type == VDIR) {
1038			struct tmpfs_node *n;
1039
1040			/* Ensure the target directory is not a child of the
1041			 * directory being moved.  Otherwise, we'd end up
1042			 * with stale nodes. */
1043			n = tdnode;
1044			/* TMPFS_LOCK garanties that no nodes are freed while
1045			 * traversing the list. Nodes can only be marked as
1046			 * removed: tn_parent == NULL. */
1047			TMPFS_LOCK(tmp);
1048			TMPFS_NODE_LOCK(n);
1049			while (n != n->tn_dir.tn_parent) {
1050				struct tmpfs_node *parent;
1051
1052				if (n == fnode) {
1053					TMPFS_NODE_UNLOCK(n);
1054					TMPFS_UNLOCK(tmp);
1055					error = EINVAL;
1056					if (newname != NULL)
1057						    free(newname, M_TMPFSNAME);
1058					goto out_locked;
1059				}
1060				parent = n->tn_dir.tn_parent;
1061				TMPFS_NODE_UNLOCK(n);
1062				if (parent == NULL) {
1063					n = NULL;
1064					break;
1065				}
1066				TMPFS_NODE_LOCK(parent);
1067				if (parent->tn_dir.tn_parent == NULL) {
1068					TMPFS_NODE_UNLOCK(parent);
1069					n = NULL;
1070					break;
1071				}
1072				n = parent;
1073			}
1074			TMPFS_UNLOCK(tmp);
1075			if (n == NULL) {
1076				error = EINVAL;
1077				if (newname != NULL)
1078					    free(newname, M_TMPFSNAME);
1079				goto out_locked;
1080			}
1081			TMPFS_NODE_UNLOCK(n);
1082
1083			/* Adjust the parent pointer. */
1084			TMPFS_VALIDATE_DIR(fnode);
1085			TMPFS_NODE_LOCK(de->td_node);
1086			de->td_node->tn_dir.tn_parent = tdnode;
1087			TMPFS_NODE_UNLOCK(de->td_node);
1088
1089			/* As a result of changing the target of the '..'
1090			 * entry, the link count of the source and target
1091			 * directories has to be adjusted. */
1092			TMPFS_NODE_LOCK(tdnode);
1093			TMPFS_ASSERT_LOCKED(tdnode);
1094			tdnode->tn_links++;
1095			TMPFS_NODE_UNLOCK(tdnode);
1096
1097			TMPFS_NODE_LOCK(fdnode);
1098			TMPFS_ASSERT_LOCKED(fdnode);
1099			fdnode->tn_links--;
1100			TMPFS_NODE_UNLOCK(fdnode);
1101		}
1102
1103		/* Do the move: just remove the entry from the source directory
1104		 * and insert it into the target one. */
1105		tmpfs_dir_detach(fdvp, de);
1106		tmpfs_dir_attach(tdvp, de);
1107	}
1108
1109	/* If the name has changed, we need to make it effective by changing
1110	 * it in the directory entry. */
1111	if (newname != NULL) {
1112		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1113
1114		free(de->td_name, M_TMPFSNAME);
1115		de->td_namelen = (uint16_t)tcnp->cn_namelen;
1116		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1117		de->td_name = newname;
1118
1119		fnode->tn_status |= TMPFS_NODE_CHANGED;
1120		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1121	}
1122
1123	/* If we are overwriting an entry, we have to remove the old one
1124	 * from the target directory. */
1125	if (tvp != NULL) {
1126		/* Remove the old entry from the target directory. */
1127		de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1128		tmpfs_dir_detach(tdvp, de);
1129
1130		/* Free the directory entry we just deleted.  Note that the
1131		 * node referred by it will not be removed until the vnode is
1132		 * really reclaimed. */
1133		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1134	}
1135
1136	error = 0;
1137
1138out_locked:
1139	if (fdnode != tdnode)
1140		VOP_UNLOCK(fdvp, 0);
1141
1142out:
1143	/* Release target nodes. */
1144	/* XXX: I don't understand when tdvp can be the same as tvp, but
1145	 * other code takes care of this... */
1146	if (tdvp == tvp)
1147		vrele(tdvp);
1148	else
1149		vput(tdvp);
1150	if (tvp != NULL)
1151		vput(tvp);
1152
1153	/* Release source nodes. */
1154	vrele(fdvp);
1155	vrele(fvp);
1156
1157	return error;
1158}
1159
1160/* --------------------------------------------------------------------- */
1161
1162static int
1163tmpfs_mkdir(struct vop_mkdir_args *v)
1164{
1165	struct vnode *dvp = v->a_dvp;
1166	struct vnode **vpp = v->a_vpp;
1167	struct componentname *cnp = v->a_cnp;
1168	struct vattr *vap = v->a_vap;
1169
1170	MPASS(vap->va_type == VDIR);
1171
1172	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1173}
1174
1175/* --------------------------------------------------------------------- */
1176
1177static int
1178tmpfs_rmdir(struct vop_rmdir_args *v)
1179{
1180	struct vnode *dvp = v->a_dvp;
1181	struct vnode *vp = v->a_vp;
1182
1183	int error;
1184	struct tmpfs_dirent *de;
1185	struct tmpfs_mount *tmp;
1186	struct tmpfs_node *dnode;
1187	struct tmpfs_node *node;
1188
1189	MPASS(VOP_ISLOCKED(dvp));
1190	MPASS(VOP_ISLOCKED(vp));
1191
1192	tmp = VFS_TO_TMPFS(dvp->v_mount);
1193	dnode = VP_TO_TMPFS_DIR(dvp);
1194	node = VP_TO_TMPFS_DIR(vp);
1195
1196	/* Directories with more than two entries ('.' and '..') cannot be
1197	 * removed. */
1198	 if (node->tn_size > 0) {
1199		 error = ENOTEMPTY;
1200		 goto out;
1201	 }
1202
1203	if ((dnode->tn_flags & APPEND)
1204	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1205		error = EPERM;
1206		goto out;
1207	}
1208
1209	/* This invariant holds only if we are not trying to remove "..".
1210	  * We checked for that above so this is safe now. */
1211	MPASS(node->tn_dir.tn_parent == dnode);
1212
1213	/* Get the directory entry associated with node (vp).  This was
1214	 * filled by tmpfs_lookup while looking up the entry. */
1215	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1216	MPASS(TMPFS_DIRENT_MATCHES(de,
1217	    v->a_cnp->cn_nameptr,
1218	    v->a_cnp->cn_namelen));
1219
1220	/* Check flags to see if we are allowed to remove the directory. */
1221	if (dnode->tn_flags & APPEND
1222		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1223		error = EPERM;
1224		goto out;
1225	}
1226
1227
1228	/* Detach the directory entry from the directory (dnode). */
1229	tmpfs_dir_detach(dvp, de);
1230
1231	/* No vnode should be allocated for this entry from this point */
1232	TMPFS_NODE_LOCK(node);
1233	TMPFS_ASSERT_ELOCKED(node);
1234	node->tn_links--;
1235	node->tn_dir.tn_parent = NULL;
1236	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1237	    TMPFS_NODE_MODIFIED;
1238
1239	TMPFS_NODE_UNLOCK(node);
1240
1241	TMPFS_NODE_LOCK(dnode);
1242	TMPFS_ASSERT_ELOCKED(dnode);
1243	dnode->tn_links--;
1244	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1245	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1246	TMPFS_NODE_UNLOCK(dnode);
1247
1248	cache_purge(dvp);
1249	cache_purge(vp);
1250
1251	/* Free the directory entry we just deleted.  Note that the node
1252	 * referred by it will not be removed until the vnode is really
1253	 * reclaimed. */
1254	tmpfs_free_dirent(tmp, de, TRUE);
1255
1256	/* Release the deleted vnode (will destroy the node, notify
1257	 * interested parties and clean it from the cache). */
1258
1259	dnode->tn_status |= TMPFS_NODE_CHANGED;
1260	tmpfs_update(dvp);
1261
1262	error = 0;
1263
1264out:
1265	return error;
1266}
1267
1268/* --------------------------------------------------------------------- */
1269
1270static int
1271tmpfs_symlink(struct vop_symlink_args *v)
1272{
1273	struct vnode *dvp = v->a_dvp;
1274	struct vnode **vpp = v->a_vpp;
1275	struct componentname *cnp = v->a_cnp;
1276	struct vattr *vap = v->a_vap;
1277	char *target = v->a_target;
1278
1279#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1280	MPASS(vap->va_type == VLNK);
1281#else
1282	vap->va_type = VLNK;
1283#endif
1284
1285	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1286}
1287
1288/* --------------------------------------------------------------------- */
1289
1290static int
1291tmpfs_readdir(struct vop_readdir_args *v)
1292{
1293	struct vnode *vp = v->a_vp;
1294	struct uio *uio = v->a_uio;
1295	int *eofflag = v->a_eofflag;
1296	u_long **cookies = v->a_cookies;
1297	int *ncookies = v->a_ncookies;
1298
1299	int error;
1300	off_t startoff;
1301	off_t cnt = 0;
1302	struct tmpfs_node *node;
1303
1304	/* This operation only makes sense on directory nodes. */
1305	if (vp->v_type != VDIR)
1306		return ENOTDIR;
1307
1308	node = VP_TO_TMPFS_DIR(vp);
1309
1310	startoff = uio->uio_offset;
1311
1312	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1313		error = tmpfs_dir_getdotdent(node, uio);
1314		if (error != 0)
1315			goto outok;
1316		cnt++;
1317	}
1318
1319	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1320		error = tmpfs_dir_getdotdotdent(node, uio);
1321		if (error != 0)
1322			goto outok;
1323		cnt++;
1324	}
1325
1326	error = tmpfs_dir_getdents(node, uio, &cnt);
1327
1328outok:
1329	MPASS(error >= -1);
1330
1331	if (error == -1)
1332		error = 0;
1333
1334	if (eofflag != NULL)
1335		*eofflag =
1336		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1337
1338	/* Update NFS-related variables. */
1339	if (error == 0 && cookies != NULL && ncookies != NULL) {
1340		off_t i;
1341		off_t off = startoff;
1342		struct tmpfs_dirent *de = NULL;
1343
1344		*ncookies = cnt;
1345		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1346
1347		for (i = 0; i < cnt; i++) {
1348			MPASS(off != TMPFS_DIRCOOKIE_EOF);
1349			if (off == TMPFS_DIRCOOKIE_DOT) {
1350				off = TMPFS_DIRCOOKIE_DOTDOT;
1351			} else {
1352				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1353					de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1354				} else if (de != NULL) {
1355					de = TAILQ_NEXT(de, td_entries);
1356				} else {
1357					de = tmpfs_dir_lookupbycookie(node,
1358					    off);
1359					MPASS(de != NULL);
1360					de = TAILQ_NEXT(de, td_entries);
1361				}
1362				if (de == NULL)
1363					off = TMPFS_DIRCOOKIE_EOF;
1364				else
1365					off = tmpfs_dircookie(de);
1366			}
1367
1368			(*cookies)[i] = off;
1369		}
1370		MPASS(uio->uio_offset == off);
1371	}
1372
1373	return error;
1374}
1375
1376/* --------------------------------------------------------------------- */
1377
1378static int
1379tmpfs_readlink(struct vop_readlink_args *v)
1380{
1381	struct vnode *vp = v->a_vp;
1382	struct uio *uio = v->a_uio;
1383
1384	int error;
1385	struct tmpfs_node *node;
1386
1387	MPASS(uio->uio_offset == 0);
1388	MPASS(vp->v_type == VLNK);
1389
1390	node = VP_TO_TMPFS_NODE(vp);
1391
1392	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1393	    uio);
1394	node->tn_status |= TMPFS_NODE_ACCESSED;
1395
1396	return error;
1397}
1398
1399/* --------------------------------------------------------------------- */
1400
1401static int
1402tmpfs_inactive(struct vop_inactive_args *v)
1403{
1404	struct vnode *vp = v->a_vp;
1405	struct thread *l = v->a_td;
1406
1407	struct tmpfs_node *node;
1408
1409	MPASS(VOP_ISLOCKED(vp));
1410
1411	node = VP_TO_TMPFS_NODE(vp);
1412
1413	if (node->tn_links == 0)
1414		vrecycle(vp, l);
1415
1416	return 0;
1417}
1418
1419/* --------------------------------------------------------------------- */
1420
1421int
1422tmpfs_reclaim(struct vop_reclaim_args *v)
1423{
1424	struct vnode *vp = v->a_vp;
1425
1426	struct tmpfs_mount *tmp;
1427	struct tmpfs_node *node;
1428
1429	node = VP_TO_TMPFS_NODE(vp);
1430	tmp = VFS_TO_TMPFS(vp->v_mount);
1431
1432	vnode_destroy_vobject(vp);
1433	cache_purge(vp);
1434
1435	TMPFS_NODE_LOCK(node);
1436	TMPFS_ASSERT_ELOCKED(node);
1437	tmpfs_free_vp(vp);
1438
1439	/* If the node referenced by this vnode was deleted by the user,
1440	 * we must free its associated data structures (now that the vnode
1441	 * is being reclaimed). */
1442	if (node->tn_links == 0 &&
1443	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1444		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1445		TMPFS_NODE_UNLOCK(node);
1446		tmpfs_free_node(tmp, node);
1447	} else
1448		TMPFS_NODE_UNLOCK(node);
1449
1450	MPASS(vp->v_data == NULL);
1451	return 0;
1452}
1453
1454/* --------------------------------------------------------------------- */
1455
1456static int
1457tmpfs_print(struct vop_print_args *v)
1458{
1459	struct vnode *vp = v->a_vp;
1460
1461	struct tmpfs_node *node;
1462
1463	node = VP_TO_TMPFS_NODE(vp);
1464
1465	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1466	    node, node->tn_flags, node->tn_links);
1467	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1468	    ", status 0x%x\n",
1469	    node->tn_mode, node->tn_uid, node->tn_gid,
1470	    (uintmax_t)node->tn_size, node->tn_status);
1471
1472	if (vp->v_type == VFIFO)
1473		fifo_printinfo(vp);
1474
1475	printf("\n");
1476
1477	return 0;
1478}
1479
1480/* --------------------------------------------------------------------- */
1481
1482static int
1483tmpfs_pathconf(struct vop_pathconf_args *v)
1484{
1485	int name = v->a_name;
1486	register_t *retval = v->a_retval;
1487
1488	int error;
1489
1490	error = 0;
1491
1492	switch (name) {
1493	case _PC_LINK_MAX:
1494		*retval = LINK_MAX;
1495		break;
1496
1497	case _PC_NAME_MAX:
1498		*retval = NAME_MAX;
1499		break;
1500
1501	case _PC_PATH_MAX:
1502		*retval = PATH_MAX;
1503		break;
1504
1505	case _PC_PIPE_BUF:
1506		*retval = PIPE_BUF;
1507		break;
1508
1509	case _PC_CHOWN_RESTRICTED:
1510		*retval = 1;
1511		break;
1512
1513	case _PC_NO_TRUNC:
1514		*retval = 1;
1515		break;
1516
1517	case _PC_SYNC_IO:
1518		*retval = 1;
1519		break;
1520
1521	case _PC_FILESIZEBITS:
1522		*retval = 0; /* XXX Don't know which value should I return. */
1523		break;
1524
1525	default:
1526		error = EINVAL;
1527	}
1528
1529	return error;
1530}
1531
1532static int
1533tmpfs_vptofh(struct vop_vptofh_args *ap)
1534{
1535	struct tmpfs_fid *tfhp;
1536	struct tmpfs_node *node;
1537
1538	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1539	node = VP_TO_TMPFS_NODE(ap->a_vp);
1540
1541	tfhp->tf_len = sizeof(struct tmpfs_fid);
1542	tfhp->tf_id = node->tn_id;
1543	tfhp->tf_gen = node->tn_gen;
1544
1545	return (0);
1546}
1547
1548/* --------------------------------------------------------------------- */
1549
1550/*
1551 * vnode operations vector used for files stored in a tmpfs file system.
1552 */
1553struct vop_vector tmpfs_vnodeop_entries = {
1554	.vop_default =			&default_vnodeops,
1555	.vop_lookup =			vfs_cache_lookup,
1556	.vop_cachedlookup =		tmpfs_lookup,
1557	.vop_create =			tmpfs_create,
1558	.vop_mknod =			tmpfs_mknod,
1559	.vop_open =			tmpfs_open,
1560	.vop_close =			tmpfs_close,
1561	.vop_access =			tmpfs_access,
1562	.vop_getattr =			tmpfs_getattr,
1563	.vop_setattr =			tmpfs_setattr,
1564	.vop_read =			tmpfs_read,
1565	.vop_write =			tmpfs_write,
1566	.vop_fsync =			tmpfs_fsync,
1567	.vop_remove =			tmpfs_remove,
1568	.vop_link =			tmpfs_link,
1569	.vop_rename =			tmpfs_rename,
1570	.vop_mkdir =			tmpfs_mkdir,
1571	.vop_rmdir =			tmpfs_rmdir,
1572	.vop_symlink =			tmpfs_symlink,
1573	.vop_readdir =			tmpfs_readdir,
1574	.vop_readlink =			tmpfs_readlink,
1575	.vop_inactive =			tmpfs_inactive,
1576	.vop_reclaim =			tmpfs_reclaim,
1577	.vop_print =			tmpfs_print,
1578	.vop_pathconf =			tmpfs_pathconf,
1579	.vop_vptofh =			tmpfs_vptofh,
1580	.vop_bmap =			VOP_EOPNOTSUPP,
1581};
1582
1583