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