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