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