xref: /netbsd-src/sys/fs/tmpfs/tmpfs_vnops.c (revision 27527e67bbdf8d9ec84fd58803048ed6d181ece2)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.20 2006/01/26 20:07:34 jmmv Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9  * 2005 program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * tmpfs vnode interface.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.20 2006/01/26 20:07:34 jmmv Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/dirent.h>
49 #include <sys/fcntl.h>
50 #include <sys/event.h>
51 #include <sys/malloc.h>
52 #include <sys/namei.h>
53 #include <sys/proc.h>
54 #include <sys/stat.h>
55 #include <sys/uio.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 #include <sys/lockf.h>
59 
60 #include <uvm/uvm.h>
61 
62 #include <miscfs/fifofs/fifo.h>
63 #include <fs/tmpfs/tmpfs_vnops.h>
64 #include <fs/tmpfs/tmpfs.h>
65 
66 /* --------------------------------------------------------------------- */
67 
68 /*
69  * vnode operations vector used for files stored in a tmpfs file system.
70  */
71 int (**tmpfs_vnodeop_p)(void *);
72 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
73 	{ &vop_default_desc,		vn_default_error },
74 	{ &vop_lookup_desc,		tmpfs_lookup },
75 	{ &vop_create_desc,		tmpfs_create },
76 	{ &vop_mknod_desc,		tmpfs_mknod },
77 	{ &vop_open_desc,		tmpfs_open },
78 	{ &vop_close_desc,		tmpfs_close },
79 	{ &vop_access_desc,		tmpfs_access },
80 	{ &vop_getattr_desc,		tmpfs_getattr },
81 	{ &vop_setattr_desc,		tmpfs_setattr },
82 	{ &vop_read_desc,		tmpfs_read },
83 	{ &vop_write_desc,		tmpfs_write },
84 	{ &vop_ioctl_desc,		tmpfs_ioctl },
85 	{ &vop_fcntl_desc,		tmpfs_fcntl },
86 	{ &vop_poll_desc,		tmpfs_poll },
87 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
88 	{ &vop_revoke_desc,		tmpfs_revoke },
89 	{ &vop_mmap_desc,		tmpfs_mmap },
90 	{ &vop_fsync_desc,		tmpfs_fsync },
91 	{ &vop_seek_desc,		tmpfs_seek },
92 	{ &vop_remove_desc,		tmpfs_remove },
93 	{ &vop_link_desc,		tmpfs_link },
94 	{ &vop_rename_desc,		tmpfs_rename },
95 	{ &vop_mkdir_desc,		tmpfs_mkdir },
96 	{ &vop_rmdir_desc,		tmpfs_rmdir },
97 	{ &vop_symlink_desc,		tmpfs_symlink },
98 	{ &vop_readdir_desc,		tmpfs_readdir },
99 	{ &vop_readlink_desc,		tmpfs_readlink },
100 	{ &vop_abortop_desc,		tmpfs_abortop },
101 	{ &vop_inactive_desc,		tmpfs_inactive },
102 	{ &vop_reclaim_desc,		tmpfs_reclaim },
103 	{ &vop_lock_desc,		tmpfs_lock },
104 	{ &vop_unlock_desc,		tmpfs_unlock },
105 	{ &vop_bmap_desc,		tmpfs_bmap },
106 	{ &vop_strategy_desc,		tmpfs_strategy },
107 	{ &vop_print_desc,		tmpfs_print },
108 	{ &vop_pathconf_desc,		tmpfs_pathconf },
109 	{ &vop_islocked_desc,		tmpfs_islocked },
110 	{ &vop_advlock_desc,		tmpfs_advlock },
111 	{ &vop_lease_desc,		tmpfs_lease },
112 	{ &vop_bwrite_desc,		tmpfs_bwrite },
113 	{ &vop_getpages_desc,		tmpfs_getpages },
114 	{ &vop_putpages_desc,		tmpfs_putpages },
115 	{ NULL, NULL }
116 };
117 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
118 	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
119 
120 /* --------------------------------------------------------------------- */
121 
122 int
123 tmpfs_lookup(void *v)
124 {
125 	struct vnode *dvp = ((struct vop_lookup_args *)v)->a_dvp;
126 	struct vnode **vpp = ((struct vop_lookup_args *)v)->a_vpp;
127 	struct componentname *cnp = ((struct vop_lookup_args *)v)->a_cnp;
128 
129 	int error;
130 	struct tmpfs_dirent *de;
131 	struct tmpfs_node *dnode;
132 
133 	KASSERT(VOP_ISLOCKED(dvp));
134 
135 	dnode = VP_TO_TMPFS_DIR(dvp);
136 	*vpp = NULL;
137 
138 	/* Check accessibility of requested node as a first step. */
139 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_lwp);
140 	if (error != 0)
141 		goto out;
142 
143 	/* If requesting the last path component on a read-only file system
144 	 * with a write operation, deny it. */
145 	if ((cnp->cn_flags & ISLASTCN) &&
146 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
147 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
148 		error = EROFS;
149 		goto out;
150 	}
151 
152 	/* Avoid doing a linear scan of the directory if the requested
153 	 * directory/name couple is already in the cache. */
154 	error = cache_lookup(dvp, vpp, cnp);
155 	if (error >= 0)
156 		goto out;
157 
158 	/* We cannot be requesting the parent directory of the root node. */
159 	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
160 	    dnode->tn_parent == dnode, !(cnp->cn_flags & ISDOTDOT)));
161 
162 	if (cnp->cn_flags & ISDOTDOT) {
163 		VOP_UNLOCK(dvp, 0);
164 
165 		/* Allocate a new vnode on the matching entry. */
166 		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_parent, vpp);
167 
168 		if (cnp->cn_flags & LOCKPARENT &&
169 		    cnp->cn_flags & ISLASTCN) {
170 			if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) != 0)
171 				cnp->cn_flags |= PDIRUNLOCK;
172 		}
173 		dnode->tn_parent->tn_lookup_dirent = NULL;
174 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
175 		VREF(dvp);
176 		*vpp = dvp;
177 		dnode->tn_lookup_dirent = NULL;
178 		error = 0;
179 	} else {
180 		de = tmpfs_dir_lookup(dnode, cnp);
181 		if (de == NULL) {
182 			/* The entry was not found in the directory.
183 			 * This is OK iff we are creating or renaming an
184 			 * entry and are working on the last component of
185 			 * the path name. */
186 			if ((cnp->cn_flags & ISLASTCN) &&
187 			    (cnp->cn_nameiop == CREATE || \
188 			    cnp->cn_nameiop == RENAME)) {
189 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
190 				    cnp->cn_lwp);
191 				if (error != 0)
192 					goto out;
193 
194 				/* Keep the component name in the buffer for
195 				 * future uses. */
196 				cnp->cn_flags |= SAVENAME;
197 
198 				error = EJUSTRETURN;
199 			} else
200 				error = ENOENT;
201 		} else {
202 			struct tmpfs_node *tnode;
203 
204 			/* The entry was found, so get its associated
205 			 * tmpfs_node. */
206 			tnode = de->td_node;
207 
208 			/* If we are not at the last path component and
209 			 * found a non-directory or non-link entry (which
210 			 * may itself be pointing to a directory), raise
211 			 * an error. */
212 			if ((tnode->tn_type != VDIR &&
213 			    tnode->tn_type != VLNK) &&
214 			    !(cnp->cn_flags & ISLASTCN)) {
215 				error = ENOTDIR;
216 				goto out;
217 			}
218 
219 			/* If we are deleting or renaming the entry, keep
220 			 * track of its tmpfs_dirent so that it can be
221 			 * easily deleted later. */
222 			if ((cnp->cn_flags & ISLASTCN) &&
223 			    (cnp->cn_nameiop == DELETE ||
224 			    cnp->cn_nameiop == RENAME)) {
225 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
226 				    cnp->cn_lwp);
227 				if (error != 0)
228 					goto out;
229 				/* TODO: Check sticky bit. */
230 				tnode->tn_lookup_dirent = de;
231 			}
232 
233 			/* Allocate a new vnode on the matching entry. */
234 			error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
235 
236 			if (error == 0 && (!(cnp->cn_flags & LOCKPARENT) ||
237 			    !(cnp->cn_flags & ISLASTCN)))
238 				VOP_UNLOCK(dvp, 0);
239 		}
240 	}
241 
242 	/* Store the result of this lookup in the cache.  Avoid this if the
243 	 * request was for creation, as it does not improve timings on
244 	 * emprical tests. */
245 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
246 		cache_enter(dvp, *vpp, cnp);
247 
248 out:
249 	/* If there were no errors, *vpp cannot be null and it must be
250 	 * locked. */
251 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
252 
253 	/* dvp has to be locked if:
254 	 * - There were errors and relocking of dvp did not fail.
255 	 * - We are doing a '..' lookup, relocking of dvp did not fail
256 	 *   (PDIRUNLOCK is unset) and LOCKPARENT or ISLASTCN are not set.
257 	 * - LOCKPARENT and ISLASTCN are set. */
258 	KASSERT(IMPLIES(
259 	    (error != 0 && !(cnp->cn_flags & PDIRUNLOCK)) ||
260 	    (cnp->cn_flags & ISDOTDOT && !(cnp->cn_flags & PDIRUNLOCK) &&
261 	     ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))) ||
262 	    (cnp->cn_flags & LOCKPARENT && cnp->cn_flags & ISLASTCN)
263 	    ,
264 	    VOP_ISLOCKED(dvp)));
265 
266 	return error;
267 }
268 
269 /* --------------------------------------------------------------------- */
270 
271 int
272 tmpfs_create(void *v)
273 {
274 	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
275 	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
276 	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
277 	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
278 
279 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
280 
281 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
282 }
283 /* --------------------------------------------------------------------- */
284 
285 int
286 tmpfs_mknod(void *v)
287 {
288 	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
289 	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
290 	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
291 	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
292 
293 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
294 	    vap->va_type != VFIFO)
295 		return EINVAL;
296 
297 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
298 }
299 
300 /* --------------------------------------------------------------------- */
301 
302 int
303 tmpfs_open(void *v)
304 {
305 	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
306 	int mode = ((struct vop_open_args *)v)->a_mode;
307 
308 	int error;
309 	struct tmpfs_node *node;
310 
311 	KASSERT(VOP_ISLOCKED(vp));
312 
313 	node = VP_TO_TMPFS_NODE(vp);
314 	KASSERT(node->tn_links > 0);
315 
316 	/* If the file is marked append-only, deny write requests. */
317 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
318 		error = EPERM;
319 	else
320 		error = 0;
321 
322 	KASSERT(VOP_ISLOCKED(vp));
323 
324 	return error;
325 }
326 
327 /* --------------------------------------------------------------------- */
328 
329 int
330 tmpfs_close(void *v)
331 {
332 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
333 
334 	struct tmpfs_node *node;
335 
336 	KASSERT(VOP_ISLOCKED(vp));
337 
338 	node = VP_TO_TMPFS_NODE(vp);
339 
340 	if (node->tn_links > 0) {
341 		/* Update node times.  No need to do it if the node has
342 		 * been deleted, because it will vanish after we return. */
343 		tmpfs_update(vp, NULL, NULL, UPDATE_CLOSE);
344 	}
345 
346 	return 0;
347 }
348 
349 /* --------------------------------------------------------------------- */
350 
351 int
352 tmpfs_access(void *v)
353 {
354 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
355 	int mode = ((struct vop_access_args *)v)->a_mode;
356 	struct ucred *cred = ((struct vop_access_args *)v)->a_cred;
357 
358 	int error;
359 	struct tmpfs_node *node;
360 
361 	KASSERT(VOP_ISLOCKED(vp));
362 
363 	node = VP_TO_TMPFS_NODE(vp);
364 
365 	switch (vp->v_type) {
366 	case VDIR:
367 		/* FALLTHROUGH */
368 	case VLNK:
369 		/* FALLTHROUGH */
370 	case VREG:
371 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
372 			error = EROFS;
373 			goto out;
374 		}
375 		break;
376 
377 	case VBLK:
378 		/* FALLTHROUGH */
379 	case VCHR:
380 		/* FALLTHROUGH */
381 	case VSOCK:
382 		/* FALLTHROUGH */
383 	case VFIFO:
384 		break;
385 
386 	default:
387 		error = EINVAL;
388 		goto out;
389 	}
390 
391 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
392 		error = EPERM;
393 		goto out;
394 	}
395 
396 	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
397 	    node->tn_gid, mode, cred);
398 
399 out:
400 	KASSERT(VOP_ISLOCKED(vp));
401 
402 	return error;
403 }
404 
405 /* --------------------------------------------------------------------- */
406 
407 int
408 tmpfs_getattr(void *v)
409 {
410 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
411 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
412 
413 	struct tmpfs_node *node;
414 
415 	node = VP_TO_TMPFS_NODE(vp);
416 
417 	VATTR_NULL(vap);
418 
419 	tmpfs_itimes(vp, NULL, NULL);
420 
421 	vap->va_type = vp->v_type;
422 	vap->va_mode = node->tn_mode;
423 	vap->va_nlink = node->tn_links;
424 	vap->va_uid = node->tn_uid;
425 	vap->va_gid = node->tn_gid;
426 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
427 	vap->va_fileid = node->tn_id;
428 	vap->va_size = node->tn_size;
429 	vap->va_blocksize = PAGE_SIZE;
430 	vap->va_atime = node->tn_atime;
431 	vap->va_mtime = node->tn_mtime;
432 	vap->va_ctime = node->tn_ctime;
433 	vap->va_birthtime = node->tn_birthtime;
434 	vap->va_gen = node->tn_gen;
435 	vap->va_flags = node->tn_flags;
436 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
437 		node->tn_rdev : VNOVAL;
438 	vap->va_bytes = round_page(node->tn_size);
439 	vap->va_filerev = VNOVAL;
440 	vap->va_vaflags = 0;
441 	vap->va_spare = VNOVAL; /* XXX */
442 
443 	return 0;
444 }
445 
446 /* --------------------------------------------------------------------- */
447 
448 /* XXX Should this operation be atomic?  I think it should, but code in
449  * XXX other places (e.g., ufs) doesn't seem to be... */
450 int
451 tmpfs_setattr(void *v)
452 {
453 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
454 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
455 	struct ucred *cred = ((struct vop_setattr_args *)v)->a_cred;
456 	struct lwp *l = ((struct vop_setattr_args *)v)->a_l;
457 
458 	int error;
459 
460 	KASSERT(VOP_ISLOCKED(vp));
461 
462 	error = 0;
463 
464 	/* Abort if any unsettable attribute is given. */
465 	if (vap->va_type != VNON ||
466 	    vap->va_nlink != VNOVAL ||
467 	    vap->va_fsid != VNOVAL ||
468 	    vap->va_fileid != VNOVAL ||
469 	    vap->va_blocksize != VNOVAL ||
470 	    vap->va_ctime.tv_sec != VNOVAL ||
471 	    vap->va_ctime.tv_nsec != VNOVAL ||
472 	    vap->va_birthtime.tv_sec != VNOVAL ||
473 	    vap->va_birthtime.tv_nsec != VNOVAL ||
474 	    vap->va_gen != VNOVAL ||
475 	    vap->va_rdev != VNOVAL ||
476 	    vap->va_bytes != VNOVAL)
477 		error = EINVAL;
478 
479 	if (error == 0 && (vap->va_flags != VNOVAL))
480 		error = tmpfs_chflags(vp, vap->va_flags, cred, l->l_proc);
481 
482 	if (error == 0 && (vap->va_size != VNOVAL))
483 		error = tmpfs_chsize(vp, vap->va_size, cred, l->l_proc);
484 
485 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
486 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred,
487 		    l->l_proc);
488 
489 	if (error == 0 && (vap->va_mode != VNOVAL))
490 		error = tmpfs_chmod(vp, vap->va_mode, cred, l->l_proc);
491 
492 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
493 	    vap->va_atime.tv_nsec != VNOVAL) ||
494 	    (vap->va_mtime.tv_sec != VNOVAL &&
495 	    vap->va_mtime.tv_nsec != VNOVAL)))
496 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
497 		    vap->va_vaflags, cred, l);
498 
499 	/* Update the node times.  We give preference to the error codes
500 	 * generated by this function rather than the ones that may arise
501 	 * from tmpfs_update. */
502 	tmpfs_update(vp, NULL, NULL, 0);
503 
504 	KASSERT(VOP_ISLOCKED(vp));
505 
506 	return error;
507 }
508 
509 /* --------------------------------------------------------------------- */
510 
511 int
512 tmpfs_read(void *v)
513 {
514 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
515 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
516 
517 	int error;
518 	int flags;
519 	struct tmpfs_node *node;
520 	struct uvm_object *uobj;
521 
522 	KASSERT(VOP_ISLOCKED(vp));
523 
524 	node = VP_TO_TMPFS_NODE(vp);
525 
526 	if (vp->v_type != VREG) {
527 		error = EISDIR;
528 		goto out;
529 	}
530 
531 	if (uio->uio_offset < 0) {
532 		error = EINVAL;
533 		goto out;
534 	}
535 
536 	node->tn_status |= TMPFS_NODE_ACCESSED;
537 
538 	uobj = node->tn_aobj;
539 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
540 	error = 0;
541 	while (error == 0 && uio->uio_resid > 0) {
542 		vsize_t len;
543 		void *win;
544 
545 		if (node->tn_size <= uio->uio_offset)
546 			break;
547 
548 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
549 		if (len == 0)
550 			break;
551 
552 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
553 		    UBC_READ);
554 		error = uiomove(win, len, uio);
555 		ubc_release(win, flags);
556 	}
557 
558 out:
559 	KASSERT(VOP_ISLOCKED(vp));
560 
561 	return error;
562 }
563 
564 /* --------------------------------------------------------------------- */
565 
566 int
567 tmpfs_write(void *v)
568 {
569 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
570 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
571 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
572 
573 	boolean_t extended;
574 	int error;
575 	int flags;
576 	off_t oldsize;
577 	struct tmpfs_node *node;
578 	struct uvm_object *uobj;
579 
580 	KASSERT(VOP_ISLOCKED(vp));
581 
582 	node = VP_TO_TMPFS_NODE(vp);
583 	oldsize = node->tn_size;
584 
585 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
586 		error = EINVAL;
587 		goto out;
588 	}
589 
590 	if (uio->uio_resid == 0) {
591 		error = 0;
592 		goto out;
593 	}
594 
595 	if (ioflag & IO_APPEND)
596 		uio->uio_offset = node->tn_size;
597 
598 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
599 	if (extended) {
600 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
601 		if (error != 0)
602 			goto out;
603 	}
604 
605 	uobj = node->tn_aobj;
606 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
607 	error = 0;
608 	while (error == 0 && uio->uio_resid > 0) {
609 		vsize_t len;
610 		void *win;
611 
612 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
613 		if (len == 0)
614 			break;
615 
616 		win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
617 		    UBC_WRITE);
618 		error = uiomove(win, len, uio);
619 		ubc_release(win, flags);
620 	}
621 
622 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
623 	    (extended ? TMPFS_NODE_CHANGED : 0);
624 
625 	if (error != 0)
626 		(void)tmpfs_reg_resize(vp, oldsize);
627 
628 out:
629 	KASSERT(VOP_ISLOCKED(vp));
630 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
631 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
632 
633 	return error;
634 }
635 
636 /* --------------------------------------------------------------------- */
637 
638 int
639 tmpfs_fsync(void *v)
640 {
641 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
642 
643 	KASSERT(VOP_ISLOCKED(vp));
644 
645 	tmpfs_update(vp, NULL, NULL, 0);
646 
647 	return 0;
648 }
649 
650 /* --------------------------------------------------------------------- */
651 
652 int
653 tmpfs_remove(void *v)
654 {
655 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
656 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
657 
658 	int error;
659 	struct tmpfs_dirent *de;
660 	struct tmpfs_mount *tmp;
661 	struct tmpfs_node *dnode;
662 	struct tmpfs_node *node;
663 
664 	KASSERT(VOP_ISLOCKED(dvp));
665 	KASSERT(VOP_ISLOCKED(vp));
666 
667 	dnode = VP_TO_TMPFS_DIR(dvp);
668 	node = VP_TO_TMPFS_NODE(vp);
669 	tmp = VFS_TO_TMPFS(vp->v_mount);
670 	de = node->tn_lookup_dirent;
671 	KASSERT(de != NULL);
672 
673 	/* XXX: Why isn't this done by the caller? */
674 	if (vp->v_type == VDIR) {
675 		error = EISDIR;
676 		goto out;
677 	}
678 
679 	/* Files marked as immutable or append-only cannot be deleted. */
680 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
681 		error = EPERM;
682 		goto out;
683 	}
684 
685 	/* Remove the entry from the directory; as it is a file, we do not
686 	 * have to change the number of hard links of the directory. */
687 	tmpfs_dir_detach(dvp, de);
688 
689 	/* Notify interested parties about the modification of dvp.
690 	 * The removal of vp is notified when it is reclaimed. */
691 	VN_KNOTE(dvp, NOTE_WRITE);
692 
693 	/* Free the directory entry we just deleted.  Note that the node
694 	 * referred by it will not be removed until the vnode is really
695 	 * reclaimed. */
696 	tmpfs_free_dirent(tmp, de, TRUE);
697 
698 	error = 0;
699 
700 out:
701 	vput(dvp);
702 	vput(vp);
703 
704 	KASSERT(!VOP_ISLOCKED(dvp));
705 
706 	return error;
707 }
708 
709 /* --------------------------------------------------------------------- */
710 
711 int
712 tmpfs_link(void *v)
713 {
714 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
715 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
716 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
717 
718 	int error;
719 	struct tmpfs_dirent *de;
720 	struct tmpfs_node *dnode;
721 	struct tmpfs_node *node;
722 
723 	KASSERT(VOP_ISLOCKED(dvp));
724 	KASSERT(!VOP_ISLOCKED(vp));
725 	KASSERT(cnp->cn_flags & HASBUF);
726 	KASSERT(dvp != vp); /* XXX When can this be false? */
727 
728 	dnode = VP_TO_TMPFS_DIR(dvp);
729 	node = VP_TO_TMPFS_NODE(vp);
730 
731 	/* Lock vp because we will need to run tmpfs_update over it, which
732 	 * needs the vnode to be locked. */
733 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
734 	if (error != 0)
735 		goto out;
736 
737 	/* XXX: Why aren't the following two tests done by the caller? */
738 
739 	/* Hard links of directories are forbidden. */
740 	if (vp->v_type == VDIR) {
741 		error = EPERM;
742 		goto out;
743 	}
744 
745 	/* Cannot create cross-device links. */
746 	if (dvp->v_mount != vp->v_mount) {
747 		error = EXDEV;
748 		goto out;
749 	}
750 
751 	/* Ensure that we do not overflow the maximum number of links imposed
752 	 * by the system. */
753 	KASSERT(node->tn_links <= LINK_MAX);
754 	if (node->tn_links == LINK_MAX) {
755 		error = EMLINK;
756 		goto out;
757 	}
758 
759 	/* We cannot create links of files marked immutable or append-only. */
760 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
761 		error = EPERM;
762 		goto out;
763 	}
764 
765 	/* Allocate a new directory entry to represent the node. */
766 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
767 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
768 	if (error != 0)
769 		goto out;
770 
771 	/* Insert the new directory entry into the appropriate directory. */
772 	tmpfs_dir_attach(dvp, de);
773 	VN_KNOTE(dvp, NOTE_WRITE);
774 
775 	/* vp link count has changed, so update node times. */
776 	node->tn_status |= TMPFS_NODE_CHANGED;
777 	tmpfs_update(vp, NULL, NULL, 0);
778 
779 	error = 0;
780 
781 out:
782 	if (VOP_ISLOCKED(vp))
783 		VOP_UNLOCK(vp, 0);
784 
785 	PNBUF_PUT(cnp->cn_pnbuf);
786 
787 	vput(dvp);
788 
789 	/* XXX Locking status of dvp does not match manual page. */
790 	KASSERT(!VOP_ISLOCKED(dvp));
791 	KASSERT(!VOP_ISLOCKED(vp));
792 
793 	return error;
794 }
795 
796 /* --------------------------------------------------------------------- */
797 
798 int
799 tmpfs_rename(void *v)
800 {
801 	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
802 	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
803 	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
804 	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
805 	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
806 	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
807 
808 	char *newname;
809 	int error;
810 	struct tmpfs_dirent *de;
811 	struct tmpfs_mount *tmp;
812 	struct tmpfs_node *fdnode;
813 	struct tmpfs_node *fnode;
814 	struct tmpfs_node *tdnode;
815 
816 	KASSERT(VOP_ISLOCKED(tdvp));
817 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
818 	KASSERT(fcnp->cn_flags & HASBUF);
819 	KASSERT(tcnp->cn_flags & HASBUF);
820 
821 	fdnode = VP_TO_TMPFS_DIR(fdvp);
822 	fnode = VP_TO_TMPFS_NODE(fvp);
823 	de = fnode->tn_lookup_dirent;
824 
825 	/* Disallow cross-device renames.
826 	 * XXX Why isn't this done by the caller? */
827 	if (fvp->v_mount != tdvp->v_mount ||
828 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
829 		error = EXDEV;
830 		goto out;
831 	}
832 
833 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
834 	tdnode = VP_TO_TMPFS_DIR(tdvp);
835 
836 	/* If source and target are the same file, there is nothing to do. */
837 	if (fvp == tvp) {
838 		error = 0;
839 		goto out;
840 	}
841 
842 	/* Avoid manipulating '.' and '..' entries. */
843 	if (de == NULL) {
844 		KASSERT(fvp->v_type == VDIR);
845 		error = EINVAL;
846 		goto out;
847 	}
848 	KASSERT(de->td_node == fnode);
849 
850 	/* If we need to move the directory between entries, lock the
851 	 * source so that we can safely operate on it. */
852 	if (fdnode != tdnode) {
853 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
854 		if (error != 0)
855 			goto out;
856 	}
857 
858 	/* Ensure that we have enough memory to hold the new name, if it
859 	 * has to be changed. */
860 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
861 	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
862 		newname = tmpfs_str_pool_get(&tmp->tm_str_pool,
863 		    tcnp->cn_namelen, 0);
864 		if (newname == NULL) {
865 			error = ENOSPC;
866 			goto out_locked;
867 		}
868 	} else
869 		newname = NULL;
870 
871 	/* If the node is being moved to another directory, we have to do
872 	 * the move. */
873 	if (fdnode != tdnode) {
874 		/* In case we are moving a directory, we have to adjust its
875 		 * parent to point to the new parent. */
876 		if (de->td_node->tn_type == VDIR) {
877 			struct tmpfs_node *n;
878 
879 			/* Ensure the target directory is not a child of the
880 			 * directory being moved.  Otherwise, we'd end up
881 			 * with stale nodes. */
882 			n = tdnode;
883 			while (n != n->tn_parent) {
884 				if (n == fnode) {
885 					error = EINVAL;
886 					goto out_locked;
887 				}
888 				n = n->tn_parent;
889 			}
890 
891 			/* Adjust the parent pointer. */
892 			TMPFS_VALIDATE_DIR(fnode);
893 			de->td_node->tn_parent = tdnode;
894 
895 			/* As a result of changing the target of the '..'
896 			 * entry, the link count of the source and target
897 			 * directories has to be adjusted. */
898 			fdnode->tn_links--;
899 			tdnode->tn_links++;
900 		}
901 
902 		/* Do the move: just remove the entry from the source directory
903 		 * and insert it into the target one. */
904 		tmpfs_dir_detach(fdvp, de);
905 		tmpfs_dir_attach(tdvp, de);
906 
907 		/* Notify listeners of fdvp about the change in the directory.
908 		 * We can do it at this point because we aren't touching fdvp
909 		 * any more below. */
910 		VN_KNOTE(fdvp, NOTE_WRITE);
911 	}
912 
913 	/* If the name has changed, we need to make it effective by changing
914 	 * it in the directory entry. */
915 	if (newname != NULL) {
916 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
917 		KASSERT(tcnp->cn_namelen < 0xffff);
918 
919 		tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
920 		    de->td_namelen);
921 		de->td_namelen = (uint16_t)tcnp->cn_namelen;
922 		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
923 		de->td_name = newname;
924 
925 		fnode->tn_status |= TMPFS_NODE_MODIFIED;
926 	}
927 
928 	/* If we are overwriting an entry, we have to remove the old one
929 	 * from the target directory. */
930 	if (tvp != NULL) {
931 		struct tmpfs_node *tnode;
932 
933 		tnode = VP_TO_TMPFS_NODE(tvp);
934 
935 		/* The source node cannot be a directory in this case. */
936 		KASSERT(fnode->tn_type != VDIR);
937 
938 		/* Remove the old entry from the target directory. */
939 		de = tnode->tn_lookup_dirent;
940 		tmpfs_dir_detach(tdvp, de);
941 
942 		/* Free the directory entry we just deleted.  Note that the
943 		 * node referred by it will not be removed until the vnode is
944 		 * really reclaimed. */
945 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
946 	}
947 
948 	/* Notify listeners of tdvp about the change in the directory (either
949 	 * because a new entry was added or because one was removed). */
950 	VN_KNOTE(tdvp, NOTE_WRITE);
951 
952 	error = 0;
953 
954 out_locked:
955 	if (fdnode != tdnode)
956 		VOP_UNLOCK(fdvp, 0);
957 
958 out:
959 	/* Release target nodes. */
960 	/* XXX: I don't understand when tdvp can be the same as tvp, but
961 	 * other code takes care of this... */
962 	if (tdvp == tvp)
963 		vrele(tdvp);
964 	else
965 		vput(tdvp);
966 	if (tvp != NULL)
967 		vput(tvp);
968 
969 	/* Release source nodes. */
970 	vrele(fdvp);
971 	vrele(fvp);
972 
973 	return error;
974 }
975 
976 /* --------------------------------------------------------------------- */
977 
978 int
979 tmpfs_mkdir(void *v)
980 {
981 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
982 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
983 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
984 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
985 
986 	KASSERT(vap->va_type == VDIR);
987 
988 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
989 }
990 
991 /* --------------------------------------------------------------------- */
992 
993 int
994 tmpfs_rmdir(void *v)
995 {
996 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
997 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
998 
999 	int error;
1000 	struct tmpfs_dirent *de;
1001 	struct tmpfs_mount *tmp;
1002 	struct tmpfs_node *dnode;
1003 	struct tmpfs_node *node;
1004 
1005 	KASSERT(VOP_ISLOCKED(dvp));
1006 	KASSERT(VOP_ISLOCKED(vp));
1007 
1008 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1009 	dnode = VP_TO_TMPFS_DIR(dvp);
1010 	node = VP_TO_TMPFS_DIR(vp);
1011 	KASSERT(node->tn_parent == dnode);
1012 
1013 	/* Get the directory entry associated with node (vp).  This was
1014 	 * filled by tmpfs_lookup while looking up the entry. */
1015 	de = node->tn_lookup_dirent;
1016 	KASSERT(TMPFS_DIRENT_MATCHES(de,
1017 	    ((struct vop_rmdir_args *)v)->a_cnp->cn_nameptr,
1018 	    ((struct vop_rmdir_args *)v)->a_cnp->cn_namelen));
1019 
1020 	/* Directories with more than two entries ('.' and '..') cannot be
1021 	 * removed. */
1022 	if (node->tn_size > 0) {
1023 		error = ENOTEMPTY;
1024 		goto out;
1025 	}
1026 
1027 	/* Check flags to see if we are allowed to remove the directory. */
1028 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1029 		error = EPERM;
1030 		goto out;
1031 	}
1032 
1033 	/* Detach the directory entry from the directory (dnode). */
1034 	tmpfs_dir_detach(dvp, de);
1035 
1036 	node->tn_links--;
1037 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1038 	    TMPFS_NODE_MODIFIED;
1039 	node->tn_parent->tn_links--;
1040 	node->tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1041 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1042 
1043 	/* Notify modification of parent directory and release it. */
1044 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1045 	cache_purge(dvp); /* XXX Is this needed? */
1046 	vput(dvp);
1047 
1048 	/* Free the directory entry we just deleted.  Note that the node
1049 	 * referred by it will not be removed until the vnode is really
1050 	 * reclaimed. */
1051 	tmpfs_free_dirent(tmp, de, TRUE);
1052 
1053 	/* Release the deleted vnode (will destroy the node, notify
1054 	 * interested parties and clean it from the cache). */
1055 	vput(vp);
1056 
1057 	error = 0;
1058 
1059 out:
1060 	if (error != 0) {
1061 		vput(dvp);
1062 		vput(vp);
1063 	}
1064 
1065 	return error;
1066 }
1067 
1068 /* --------------------------------------------------------------------- */
1069 
1070 int
1071 tmpfs_symlink(void *v)
1072 {
1073 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1074 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1075 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1076 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1077 	char *target = ((struct vop_symlink_args *)v)->a_target;
1078 
1079 	KASSERT(vap->va_type == VLNK);
1080 
1081 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1082 }
1083 
1084 /* --------------------------------------------------------------------- */
1085 
1086 int
1087 tmpfs_readdir(void *v)
1088 {
1089 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1090 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1091 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1092 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1093 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1094 
1095 	int error;
1096 	off_t startoff;
1097 	off_t cnt;
1098 	struct tmpfs_node *node;
1099 
1100 	KASSERT(VOP_ISLOCKED(vp));
1101 
1102 	/* This operation only makes sense on directory nodes. */
1103 	if (vp->v_type != VDIR) {
1104 		error = ENOTDIR;
1105 		goto out;
1106 	}
1107 
1108 	node = VP_TO_TMPFS_DIR(vp);
1109 
1110 	startoff = uio->uio_offset;
1111 
1112 	cnt = 0;
1113 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1114 		error = tmpfs_dir_getdotdent(node, uio);
1115 		if (error == -1) {
1116 			error = 0;
1117 			goto outok;
1118 		} else if (error != 0)
1119 			goto outok;
1120 		cnt++;
1121 	}
1122 
1123 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1124 		error = tmpfs_dir_getdotdotdent(node, uio);
1125 		if (error == -1) {
1126 			error = 0;
1127 			goto outok;
1128 		} else if (error != 0)
1129 			goto outok;
1130 		cnt++;
1131 	}
1132 
1133 	error = tmpfs_dir_getdents(node, uio, &cnt);
1134 	if (error == -1)
1135 		error = 0;
1136 	KASSERT(error >= 0);
1137 
1138 outok:
1139 	/* This label assumes that startoff has been
1140 	 * initialized.  If the compiler didn't spit out warnings, we'd
1141 	 * simply make this one be 'out' and drop 'outok'. */
1142 
1143 	if (eofflag != NULL)
1144 		*eofflag =
1145 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1146 
1147 	/* Update NFS-related variables. */
1148 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1149 		off_t i;
1150 		off_t off = startoff;
1151 		struct tmpfs_dirent *de = NULL;
1152 
1153 		*ncookies = cnt;
1154 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1155 
1156 		for (i = 0; i < cnt; i++) {
1157 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1158 			if (off == TMPFS_DIRCOOKIE_DOT) {
1159 				off = TMPFS_DIRCOOKIE_DOTDOT;
1160 			} else {
1161 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1162 					de = TAILQ_FIRST(&node->tn_dir);
1163 				} else if (de != NULL) {
1164 					de = TAILQ_NEXT(de, td_entries);
1165 				} else {
1166 					de = tmpfs_dir_lookupbycookie(node,
1167 					    off);
1168 					KASSERT(de != NULL);
1169 					de = TAILQ_NEXT(de, td_entries);
1170 				}
1171 				if (de == NULL) {
1172 					off = TMPFS_DIRCOOKIE_EOF;
1173 				} else {
1174 					off = TMPFS_DIRCOOKIE(de);
1175 				}
1176 			}
1177 
1178 			(*cookies)[i] = off;
1179 		}
1180 		KASSERT(uio->uio_offset == off);
1181 	}
1182 
1183 out:
1184 	KASSERT(VOP_ISLOCKED(vp));
1185 
1186 	return error;
1187 }
1188 
1189 /* --------------------------------------------------------------------- */
1190 
1191 int
1192 tmpfs_readlink(void *v)
1193 {
1194 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1195 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1196 
1197 	int error;
1198 	struct tmpfs_node *node;
1199 
1200 	KASSERT(VOP_ISLOCKED(vp));
1201 	KASSERT(uio->uio_offset == 0);
1202 	KASSERT(vp->v_type == VLNK);
1203 
1204 	node = VP_TO_TMPFS_NODE(vp);
1205 
1206 	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1207 	    uio);
1208 	node->tn_status |= TMPFS_NODE_ACCESSED;
1209 
1210 	KASSERT(VOP_ISLOCKED(vp));
1211 
1212 	return error;
1213 }
1214 
1215 /* --------------------------------------------------------------------- */
1216 
1217 int
1218 tmpfs_inactive(void *v)
1219 {
1220 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1221 	struct lwp *l = ((struct vop_inactive_args *)v)->a_l;
1222 
1223 	struct tmpfs_node *node;
1224 
1225 	KASSERT(VOP_ISLOCKED(vp));
1226 
1227 	node = VP_TO_TMPFS_NODE(vp);
1228 
1229 	VOP_UNLOCK(vp, 0);
1230 
1231 	if (node->tn_links == 0)
1232 		vrecycle(vp, NULL, l);
1233 
1234 	return 0;
1235 }
1236 
1237 /* --------------------------------------------------------------------- */
1238 
1239 int
1240 tmpfs_reclaim(void *v)
1241 {
1242 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1243 
1244 	struct tmpfs_mount *tmp;
1245 	struct tmpfs_node *node;
1246 
1247 	KASSERT(!VOP_ISLOCKED(vp));
1248 
1249 	node = VP_TO_TMPFS_NODE(vp);
1250 	tmp = VFS_TO_TMPFS(vp->v_mount);
1251 
1252 	if (node->tn_links == 0)
1253 		VN_KNOTE(vp, NOTE_DELETE);
1254 
1255 	cache_purge(vp);
1256 	tmpfs_free_vp(vp);
1257 
1258 	/* If the node referenced by this vnode was deleted by the user,
1259 	 * we must free its associated data structures (now that the vnode
1260 	 * is being reclaimed). */
1261 	if (node->tn_links == 0)
1262 		tmpfs_free_node(tmp, node);
1263 
1264 	KASSERT(!VOP_ISLOCKED(vp));
1265 	KASSERT(vp->v_data == NULL);
1266 
1267 	return 0;
1268 }
1269 
1270 /* --------------------------------------------------------------------- */
1271 
1272 int
1273 tmpfs_print(void *v)
1274 {
1275 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1276 
1277 	struct tmpfs_node *node;
1278 
1279 	node = VP_TO_TMPFS_NODE(vp);
1280 
1281 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1282 	    node, node->tn_flags, node->tn_links);
1283 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1284 	    ", status 0x%x\n",
1285 	    node->tn_mode, node->tn_uid, node->tn_gid,
1286 	    (uintmax_t)node->tn_size, node->tn_status);
1287 
1288 	if (vp->v_type == VFIFO)
1289 		fifo_printinfo(vp);
1290 	lockmgr_printinfo(&vp->v_lock);
1291 
1292 	printf("\n");
1293 
1294 	return 0;
1295 }
1296 
1297 /* --------------------------------------------------------------------- */
1298 
1299 int
1300 tmpfs_pathconf(void *v)
1301 {
1302 	int name = ((struct vop_pathconf_args *)v)->a_name;
1303 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1304 
1305 	int error;
1306 
1307 	error = 0;
1308 
1309 	switch (name) {
1310 	case _PC_LINK_MAX:
1311 		*retval = LINK_MAX;
1312 		break;
1313 
1314 	case _PC_NAME_MAX:
1315 		*retval = NAME_MAX;
1316 		break;
1317 
1318 	case _PC_PATH_MAX:
1319 		*retval = PATH_MAX;
1320 		break;
1321 
1322 	case _PC_PIPE_BUF:
1323 		*retval = PIPE_BUF;
1324 		break;
1325 
1326 	case _PC_CHOWN_RESTRICTED:
1327 		*retval = 1;
1328 		break;
1329 
1330 	case _PC_NO_TRUNC:
1331 		*retval = 1;
1332 		break;
1333 
1334 	case _PC_SYNC_IO:
1335 		*retval = 1;
1336 		break;
1337 
1338 	case _PC_FILESIZEBITS:
1339 		*retval = 0; /* XXX Don't know which value should I return. */
1340 		break;
1341 
1342 	default:
1343 		error = EINVAL;
1344 	}
1345 
1346 	return error;
1347 }
1348 
1349 /* --------------------------------------------------------------------- */
1350 
1351 int
1352 tmpfs_advlock(void *v)
1353 {
1354 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1355 
1356 	struct tmpfs_node *node;
1357 
1358 	node = VP_TO_TMPFS_NODE(vp);
1359 
1360 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
1361 }
1362 
1363 /* --------------------------------------------------------------------- */
1364 
1365 int
1366 tmpfs_getpages(void *v)
1367 {
1368 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1369 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1370 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1371 	int *count = ((struct vop_getpages_args *)v)->a_count;
1372 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1373 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1374 	int advice = ((struct vop_getpages_args *)v)->a_advice;
1375 	int flags = ((struct vop_getpages_args *)v)->a_flags;
1376 
1377 	int error;
1378 	struct tmpfs_node *node;
1379 	struct uvm_object *uobj;
1380 	int npages = *count;
1381 
1382 	KASSERT(vp->v_type == VREG);
1383 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
1384 
1385 	node = VP_TO_TMPFS_NODE(vp);
1386 	uobj = node->tn_aobj;
1387 
1388 	/* We currently don't rely on PGO_PASTEOF. */
1389 
1390 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1391 		if ((flags & PGO_LOCKED) == 0)
1392 			simple_unlock(&vp->v_interlock);
1393 		return EINVAL;
1394 	}
1395 
1396 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1397 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1398 	}
1399 
1400 	if ((flags & PGO_LOCKED) != 0)
1401 		return EBUSY;
1402 
1403 	if ((flags & PGO_NOTIMESTAMP) == 0) {
1404 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1405 			node->tn_status |= TMPFS_NODE_ACCESSED;
1406 
1407 		if ((access_type & VM_PROT_WRITE) != 0)
1408 			node->tn_status |= TMPFS_NODE_MODIFIED;
1409 	}
1410 
1411 	simple_unlock(&vp->v_interlock);
1412 
1413 	simple_lock(&uobj->vmobjlock);
1414 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1415 	    access_type, advice, flags);
1416 
1417 	return error;
1418 }
1419 
1420 /* --------------------------------------------------------------------- */
1421 
1422 int
1423 tmpfs_putpages(void *v)
1424 {
1425 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1426 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1427 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1428 	int flags = ((struct vop_putpages_args *)v)->a_flags;
1429 
1430 	int error;
1431 	struct tmpfs_node *node;
1432 	struct uvm_object *uobj;
1433 
1434 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
1435 
1436 	node = VP_TO_TMPFS_NODE(vp);
1437 
1438 	if (vp->v_type != VREG) {
1439 		simple_unlock(&vp->v_interlock);
1440 		return 0;
1441 	}
1442 
1443 	uobj = node->tn_aobj;
1444 	simple_unlock(&vp->v_interlock);
1445 
1446 	simple_lock(&uobj->vmobjlock);
1447 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1448 
1449 	/* XXX mtime */
1450 
1451 	return error;
1452 }
1453