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