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