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