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