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