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