xref: /netbsd-src/sys/fs/tmpfs/tmpfs_vnops.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.143 2020/06/27 17:29:18 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006, 2007, 2020 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 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.143 2020/06/27 17:29:18 christos Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/fcntl.h>
43 #include <sys/event.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/stat.h>
47 #include <sys/uio.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50 #include <sys/lockf.h>
51 #include <sys/kauth.h>
52 #include <sys/atomic.h>
53 
54 #include <uvm/uvm.h>
55 
56 #include <miscfs/fifofs/fifo.h>
57 #include <miscfs/genfs/genfs.h>
58 #include <fs/tmpfs/tmpfs_vnops.h>
59 #include <fs/tmpfs/tmpfs.h>
60 
61 /*
62  * vnode operations vector used for files stored in a tmpfs file system.
63  */
64 int (**tmpfs_vnodeop_p)(void *);
65 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
66 	{ &vop_default_desc,		vn_default_error },
67 	{ &vop_lookup_desc,		tmpfs_lookup },
68 	{ &vop_create_desc,		tmpfs_create },
69 	{ &vop_mknod_desc,		tmpfs_mknod },
70 	{ &vop_open_desc,		tmpfs_open },
71 	{ &vop_close_desc,		tmpfs_close },
72 	{ &vop_access_desc,		tmpfs_access },
73 	{ &vop_accessx_desc,		genfs_accessx },
74 	{ &vop_getattr_desc,		tmpfs_getattr },
75 	{ &vop_setattr_desc,		tmpfs_setattr },
76 	{ &vop_read_desc,		tmpfs_read },
77 	{ &vop_write_desc,		tmpfs_write },
78 	{ &vop_fallocate_desc,		genfs_eopnotsupp },
79 	{ &vop_fdiscard_desc,		genfs_eopnotsupp },
80 	{ &vop_ioctl_desc,		tmpfs_ioctl },
81 	{ &vop_fcntl_desc,		tmpfs_fcntl },
82 	{ &vop_poll_desc,		tmpfs_poll },
83 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
84 	{ &vop_revoke_desc,		tmpfs_revoke },
85 	{ &vop_mmap_desc,		tmpfs_mmap },
86 	{ &vop_fsync_desc,		tmpfs_fsync },
87 	{ &vop_seek_desc,		tmpfs_seek },
88 	{ &vop_remove_desc,		tmpfs_remove },
89 	{ &vop_link_desc,		tmpfs_link },
90 	{ &vop_rename_desc,		tmpfs_rename },
91 	{ &vop_mkdir_desc,		tmpfs_mkdir },
92 	{ &vop_rmdir_desc,		tmpfs_rmdir },
93 	{ &vop_symlink_desc,		tmpfs_symlink },
94 	{ &vop_readdir_desc,		tmpfs_readdir },
95 	{ &vop_readlink_desc,		tmpfs_readlink },
96 	{ &vop_abortop_desc,		tmpfs_abortop },
97 	{ &vop_inactive_desc,		tmpfs_inactive },
98 	{ &vop_reclaim_desc,		tmpfs_reclaim },
99 	{ &vop_lock_desc,		tmpfs_lock },
100 	{ &vop_unlock_desc,		tmpfs_unlock },
101 	{ &vop_bmap_desc,		tmpfs_bmap },
102 	{ &vop_strategy_desc,		tmpfs_strategy },
103 	{ &vop_print_desc,		tmpfs_print },
104 	{ &vop_pathconf_desc,		tmpfs_pathconf },
105 	{ &vop_islocked_desc,		tmpfs_islocked },
106 	{ &vop_advlock_desc,		tmpfs_advlock },
107 	{ &vop_bwrite_desc,		tmpfs_bwrite },
108 	{ &vop_getpages_desc,		tmpfs_getpages },
109 	{ &vop_putpages_desc,		tmpfs_putpages },
110 	{ &vop_whiteout_desc,		tmpfs_whiteout },
111 	{ NULL, NULL }
112 };
113 
114 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
115 	&tmpfs_vnodeop_p, tmpfs_vnodeop_entries
116 };
117 
118 /*
119  * tmpfs_lookup: path name traversal routine.
120  *
121  * Arguments: dvp (directory being searched), vpp (result),
122  * cnp (component name - path).
123  *
124  * => Caller holds a reference and lock on dvp.
125  * => We return looked-up vnode (vpp) locked, with a reference held.
126  */
127 int
128 tmpfs_lookup(void *v)
129 {
130 	struct vop_lookup_v2_args /* {
131 		struct vnode *a_dvp;
132 		struct vnode **a_vpp;
133 		struct componentname *a_cnp;
134 	} */ *ap = v;
135 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
136 	struct componentname *cnp = ap->a_cnp;
137 	const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
138 	tmpfs_node_t *dnode, *tnode;
139 	tmpfs_dirent_t *de;
140 	int cachefound, iswhiteout;
141 	int error;
142 
143 	KASSERT(VOP_ISLOCKED(dvp));
144 
145 	dnode = VP_TO_TMPFS_DIR(dvp);
146 	*vpp = NULL;
147 
148 	/* Check accessibility of directory. */
149 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
150 	if (error) {
151 		goto out;
152 	}
153 
154 	/*
155 	 * If requesting the last path component on a read-only file system
156 	 * with a write operation, deny it.
157 	 */
158 	if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
159 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
160 		error = EROFS;
161 		goto out;
162 	}
163 
164 	/*
165 	 * Avoid doing a linear scan of the directory if the requested
166 	 * directory/name couple is already in the cache.
167 	 */
168 	cachefound = cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
169 				  cnp->cn_nameiop, cnp->cn_flags,
170 				  &iswhiteout, vpp);
171 	if (iswhiteout) {
172 		cnp->cn_flags |= ISWHITEOUT;
173 	}
174 	if (cachefound && *vpp == NULLVP) {
175 		/* Negative cache hit. */
176 		error = ENOENT;
177 		goto out;
178 	} else if (cachefound) {
179 		error = 0;
180 		goto out;
181 	}
182 
183 	/*
184 	 * Treat an unlinked directory as empty (no "." or "..")
185 	 */
186 	if (dnode->tn_links == 0) {
187 		KASSERT(dnode->tn_size == 0);
188 		error = ENOENT;
189 		goto out;
190 	}
191 
192 	if (cnp->cn_flags & ISDOTDOT) {
193 		tmpfs_node_t *pnode;
194 
195 		/*
196 		 * Lookup of ".." case.
197 		 */
198 		if (lastcn && cnp->cn_nameiop == RENAME) {
199 			error = EINVAL;
200 			goto out;
201 		}
202 		KASSERT(dnode->tn_type == VDIR);
203 		pnode = dnode->tn_spec.tn_dir.tn_parent;
204 		if (pnode == NULL) {
205 			error = ENOENT;
206 			goto done;
207 		}
208 
209 		error = vcache_get(dvp->v_mount, &pnode, sizeof(pnode), vpp);
210 		goto done;
211 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
212 		/*
213 		 * Lookup of "." case.
214 		 */
215 		if (lastcn && cnp->cn_nameiop == RENAME) {
216 			error = EISDIR;
217 			goto out;
218 		}
219 		vref(dvp);
220 		*vpp = dvp;
221 		error = 0;
222 		goto done;
223 	}
224 
225 	/*
226 	 * Other lookup cases: perform directory scan.
227 	 */
228 	de = tmpfs_dir_lookup(dnode, cnp);
229 	if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
230 		/*
231 		 * The entry was not found in the directory.  This is valid
232 		 * if we are creating or renaming an entry and are working
233 		 * on the last component of the path name.
234 		 */
235 		if (lastcn && (cnp->cn_nameiop == CREATE ||
236 		    cnp->cn_nameiop == RENAME)) {
237 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
238 			if (error) {
239 				goto out;
240 			}
241 			error = EJUSTRETURN;
242 		} else {
243 			error = ENOENT;
244 		}
245 		if (de) {
246 			KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
247 			cnp->cn_flags |= ISWHITEOUT;
248 		}
249 		goto done;
250 	}
251 
252 	tnode = de->td_node;
253 
254 	/*
255 	 * If it is not the last path component and found a non-directory
256 	 * or non-link entry (which may itself be pointing to a directory),
257 	 * raise an error.
258 	 */
259 	if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
260 		error = ENOTDIR;
261 		goto out;
262 	}
263 
264 	/* Check the permissions. */
265 	if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
266 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
267 		if (error)
268 			goto out;
269 
270 		if ((dnode->tn_mode & S_ISTXT) != 0) {
271 			error = kauth_authorize_vnode(cnp->cn_cred,
272 			    KAUTH_VNODE_DELETE, tnode->tn_vnode,
273 			    dnode->tn_vnode, genfs_can_sticky(dvp, cnp->cn_cred,
274 			    dnode->tn_uid, tnode->tn_uid));
275 			if (error) {
276 				error = EPERM;
277 				goto out;
278 			}
279 		}
280 	}
281 
282 	/* Get a vnode for the matching entry. */
283 	error = vcache_get(dvp->v_mount, &tnode, sizeof(tnode), vpp);
284 done:
285 	/*
286 	 * Cache the result, unless request was for creation (as it does
287 	 * not improve the performance).
288 	 */
289 	if (cnp->cn_nameiop != CREATE) {
290 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
291 			    cnp->cn_flags);
292 	}
293 out:
294 	KASSERT(VOP_ISLOCKED(dvp));
295 
296 	return error;
297 }
298 
299 int
300 tmpfs_create(void *v)
301 {
302 	struct vop_create_v3_args /* {
303 		struct vnode		*a_dvp;
304 		struct vnode		**a_vpp;
305 		struct componentname	*a_cnp;
306 		struct vattr		*a_vap;
307 	} */ *ap = v;
308 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
309 	struct componentname *cnp = ap->a_cnp;
310 	struct vattr *vap = ap->a_vap;
311 
312 	KASSERT(VOP_ISLOCKED(dvp));
313 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
314 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
315 }
316 
317 int
318 tmpfs_mknod(void *v)
319 {
320 	struct vop_mknod_v3_args /* {
321 		struct vnode		*a_dvp;
322 		struct vnode		**a_vpp;
323 		struct componentname	*a_cnp;
324 		struct vattr		*a_vap;
325 	} */ *ap = v;
326 	vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
327 	struct componentname *cnp = ap->a_cnp;
328 	struct vattr *vap = ap->a_vap;
329 	enum vtype vt = vap->va_type;
330 
331 	if (vt != VBLK && vt != VCHR && vt != VFIFO) {
332 		*vpp = NULL;
333 		return EINVAL;
334 	}
335 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
336 }
337 
338 int
339 tmpfs_open(void *v)
340 {
341 	struct vop_open_args /* {
342 		struct vnode	*a_vp;
343 		int		a_mode;
344 		kauth_cred_t	a_cred;
345 	} */ *ap = v;
346 	vnode_t *vp = ap->a_vp;
347 	mode_t mode = ap->a_mode;
348 	tmpfs_node_t *node;
349 
350 	KASSERT(VOP_ISLOCKED(vp));
351 
352 	node = VP_TO_TMPFS_NODE(vp);
353 
354 	/* If the file is marked append-only, deny write requests. */
355 	if ((node->tn_flags & APPEND) != 0 &&
356 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
357 		return EPERM;
358 	}
359 	return 0;
360 }
361 
362 int
363 tmpfs_close(void *v)
364 {
365 	struct vop_close_args /* {
366 		struct vnode	*a_vp;
367 		int		a_fflag;
368 		kauth_cred_t	a_cred;
369 	} */ *ap = v;
370 	vnode_t *vp __diagused = ap->a_vp;
371 
372 	KASSERT(VOP_ISLOCKED(vp));
373 	return 0;
374 }
375 
376 int
377 tmpfs_access(void *v)
378 {
379 	struct vop_access_args /* {
380 		struct vnode	*a_vp;
381 		accmode_t	a_accmode;
382 		kauth_cred_t	a_cred;
383 	} */ *ap = v;
384 	vnode_t *vp = ap->a_vp;
385 	accmode_t accmode = ap->a_accmode;
386 	kauth_cred_t cred = ap->a_cred;
387 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
388 	const bool writing = (accmode & VWRITE) != 0;
389 
390 	KASSERT(VOP_ISLOCKED(vp));
391 
392 	/* Possible? */
393 	switch (vp->v_type) {
394 	case VDIR:
395 	case VLNK:
396 	case VREG:
397 		if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
398 			return EROFS;
399 		}
400 		break;
401 	case VBLK:
402 	case VCHR:
403 	case VSOCK:
404 	case VFIFO:
405 		break;
406 	default:
407 		return EINVAL;
408 	}
409 	if (writing && (node->tn_flags & IMMUTABLE) != 0) {
410 		return EPERM;
411 	}
412 
413 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
414 	    vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp, cred,
415 	    node->tn_uid, node->tn_gid, node->tn_mode, NULL, accmode));
416 }
417 
418 int
419 tmpfs_getattr(void *v)
420 {
421 	struct vop_getattr_args /* {
422 		struct vnode	*a_vp;
423 		struct vattr	*a_vap;
424 		kauth_cred_t	a_cred;
425 	} */ *ap = v;
426 	vnode_t *vp = ap->a_vp;
427 	struct vattr *vap = ap->a_vap;
428 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
429 
430 	vattr_null(vap);
431 
432 	vap->va_type = vp->v_type;
433 	vap->va_mode = node->tn_mode;
434 	vap->va_nlink = node->tn_links;
435 	vap->va_uid = node->tn_uid;
436 	vap->va_gid = node->tn_gid;
437 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
438 	vap->va_fileid = node->tn_id;
439 	vap->va_size = node->tn_size;
440 	vap->va_blocksize = PAGE_SIZE;
441 	vap->va_gen = TMPFS_NODE_GEN(node);
442 	vap->va_flags = node->tn_flags;
443 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
444 	    node->tn_spec.tn_dev.tn_rdev : VNOVAL;
445 	vap->va_bytes = round_page(node->tn_size);
446 	vap->va_filerev = VNOVAL;
447 	vap->va_vaflags = 0;
448 	vap->va_spare = VNOVAL; /* XXX */
449 
450 	mutex_enter(&node->tn_timelock);
451 	tmpfs_update_locked(vp, 0);
452 	vap->va_atime = node->tn_atime;
453 	vap->va_mtime = node->tn_mtime;
454 	vap->va_ctime = node->tn_ctime;
455 	vap->va_birthtime = node->tn_birthtime;
456 	mutex_exit(&node->tn_timelock);
457 
458 	return 0;
459 }
460 
461 int
462 tmpfs_setattr(void *v)
463 {
464 	struct vop_setattr_args /* {
465 		struct vnode	*a_vp;
466 		struct vattr	*a_vap;
467 		kauth_cred_t	a_cred;
468 	} */ *ap = v;
469 	vnode_t *vp = ap->a_vp;
470 	struct vattr *vap = ap->a_vap;
471 	kauth_cred_t cred = ap->a_cred;
472 	lwp_t *l = curlwp;
473 	int error = 0;
474 
475 	KASSERT(VOP_ISLOCKED(vp));
476 
477 	/* Abort if any unsettable attribute is given. */
478 	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
479 	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
480 	    vap->va_blocksize != VNOVAL || vap->va_ctime.tv_sec != VNOVAL ||
481 	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
482 	    vap->va_bytes != VNOVAL) {
483 		return EINVAL;
484 	}
485 
486 	if (error == 0 && vap->va_flags != VNOVAL)
487 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
488 
489 	if (error == 0 && vap->va_size != VNOVAL)
490 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
491 
492 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
493 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
494 
495 	if (error == 0 && vap->va_mode != VNOVAL)
496 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
497 
498 	const bool chsometime =
499 	    vap->va_atime.tv_sec != VNOVAL ||
500 	    vap->va_mtime.tv_sec != VNOVAL ||
501 	    vap->va_birthtime.tv_sec != VNOVAL;
502 	if (error == 0 && chsometime) {
503 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
504 		    &vap->va_birthtime, vap->va_vaflags, cred, l);
505 	}
506 	return error;
507 }
508 
509 int
510 tmpfs_read(void *v)
511 {
512 	struct vop_read_args /* {
513 		struct vnode *a_vp;
514 		struct uio *a_uio;
515 		int a_ioflag;
516 		kauth_cred_t a_cred;
517 	} */ *ap = v;
518 	vnode_t *vp = ap->a_vp;
519 	struct uio *uio = ap->a_uio;
520 	const int ioflag = ap->a_ioflag;
521 	tmpfs_node_t *node;
522 	struct uvm_object *uobj;
523 	int error;
524 
525 	KASSERT(VOP_ISLOCKED(vp));
526 
527 	if (vp->v_type == VDIR) {
528 		return EISDIR;
529 	}
530 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
531 		return EINVAL;
532 	}
533 
534 	/* Note: reading zero bytes should not update atime. */
535 	if (uio->uio_resid == 0) {
536 		return 0;
537 	}
538 
539 	node = VP_TO_TMPFS_NODE(vp);
540 	uobj = node->tn_spec.tn_reg.tn_aobj;
541 	error = 0;
542 
543 	while (error == 0 && uio->uio_resid > 0) {
544 		vsize_t len;
545 
546 		if (node->tn_size <= uio->uio_offset) {
547 			break;
548 		}
549 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
550 		if (len == 0) {
551 			break;
552 		}
553 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
554 		    UBC_READ | UBC_PARTIALOK | UBC_VNODE_FLAGS(vp));
555 	}
556 
557 	tmpfs_update(vp, TMPFS_UPDATE_ATIME);
558 	return error;
559 }
560 
561 int
562 tmpfs_write(void *v)
563 {
564 	struct vop_write_args /* {
565 		struct vnode	*a_vp;
566 		struct uio	*a_uio;
567 		int		a_ioflag;
568 		kauth_cred_t	a_cred;
569 	} */ *ap = v;
570 	vnode_t *vp = ap->a_vp;
571 	struct uio *uio = ap->a_uio;
572 	const int ioflag = ap->a_ioflag;
573 	tmpfs_node_t *node;
574 	struct uvm_object *uobj;
575 	off_t oldsize;
576 	int error, ubc_flags;
577 
578 	KASSERT(VOP_ISLOCKED(vp));
579 
580 	node = VP_TO_TMPFS_NODE(vp);
581 	oldsize = node->tn_size;
582 
583 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
584 		error = EROFS;
585 		goto out;
586 	}
587 
588 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
589 		error = EINVAL;
590 		goto out;
591 	}
592 	if (uio->uio_resid == 0) {
593 		error = 0;
594 		goto out;
595 	}
596 	if (ioflag & IO_APPEND) {
597 		uio->uio_offset = node->tn_size;
598 	}
599 
600 	if (uio->uio_offset + uio->uio_resid > node->tn_size) {
601 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
602 		if (error)
603 			goto out;
604 	}
605 
606 	/*
607 	 * If we're extending the file and have data to write that would
608 	 * not leave an un-zeroed hole, we can avoid fault processing and
609 	 * zeroing of pages on allocation.
610 	 *
611 	 * Don't do this if the file is mapped and we need to touch an
612 	 * existing page, because writing a mapping of the file into itself
613 	 * could cause a deadlock on PG_BUSY.
614 	 *
615 	 * New pages will not become visible until finished here (because
616 	 * of PG_BUSY and the vnode lock).
617 	 */
618 	ubc_flags = UBC_WRITE | UBC_VNODE_FLAGS(vp);
619 	if (uio->uio_offset >= oldsize &&
620 	    ((uio->uio_offset & (PAGE_SIZE - 1)) == 0 ||
621 	    ((vp->v_vflag & VV_MAPPED) == 0 &&
622 	    trunc_page(uio->uio_offset) == trunc_page(oldsize)))) {
623 		ubc_flags |= UBC_FAULTBUSY;
624 	}
625 
626 	uobj = node->tn_spec.tn_reg.tn_aobj;
627 	error = 0;
628 	while (error == 0 && uio->uio_resid > 0) {
629 		vsize_t len;
630 
631 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
632 		if (len == 0) {
633 			break;
634 		}
635 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
636 		    ubc_flags);
637 	}
638 	if (error) {
639 		(void)tmpfs_reg_resize(vp, oldsize);
640 	}
641 
642 	tmpfs_update(vp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
643 	VN_KNOTE(vp, NOTE_WRITE);
644 out:
645 	if (error) {
646 		KASSERT(oldsize == node->tn_size);
647 	} else {
648 		KASSERT(uio->uio_resid == 0);
649 	}
650 	return error;
651 }
652 
653 int
654 tmpfs_fsync(void *v)
655 {
656 	struct vop_fsync_args /* {
657 		struct vnode *a_vp;
658 		kauth_cred_t a_cred;
659 		int a_flags;
660 		off_t a_offlo;
661 		off_t a_offhi;
662 		struct lwp *a_l;
663 	} */ *ap = v;
664 	vnode_t *vp __diagused = ap->a_vp;
665 
666 	/* Nothing to do.  Should be up to date. */
667 	KASSERT(VOP_ISLOCKED(vp));
668 	return 0;
669 }
670 
671 /*
672  * tmpfs_remove: unlink a file.
673  *
674  * => Both directory (dvp) and file (vp) are locked.
675  * => We unlock and drop the reference on both.
676  */
677 int
678 tmpfs_remove(void *v)
679 {
680 	struct vop_remove_v2_args /* {
681 		struct vnode *a_dvp;
682 		struct vnode *a_vp;
683 		struct componentname *a_cnp;
684 	} */ *ap = v;
685 	vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
686 	tmpfs_node_t *dnode, *node;
687 	tmpfs_dirent_t *de;
688 	int error, tflags;
689 
690 	KASSERT(VOP_ISLOCKED(dvp));
691 	KASSERT(VOP_ISLOCKED(vp));
692 
693 	if (vp->v_type == VDIR) {
694 		error = EPERM;
695 		goto out;
696 	}
697 	dnode = VP_TO_TMPFS_DIR(dvp);
698 	node = VP_TO_TMPFS_NODE(vp);
699 
700 	/*
701 	 * Files marked as immutable or append-only cannot be deleted.
702 	 * Likewise, files residing on directories marked as append-only
703 	 * cannot be deleted.
704 	 */
705 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
706 		error = EPERM;
707 		goto out;
708 	}
709 	if (dnode->tn_flags & APPEND) {
710 		error = EPERM;
711 		goto out;
712 	}
713 
714 	/* Lookup the directory entry (check the cached hint first). */
715 	de = tmpfs_dir_cached(node);
716 	if (de == NULL) {
717 		struct componentname *cnp = ap->a_cnp;
718 		de = tmpfs_dir_lookup(dnode, cnp);
719 	}
720 	KASSERT(de && de->td_node == node);
721 
722 	/*
723 	 * Remove the entry from the directory (drops the link count) and
724 	 * destroy it or replace with a whiteout.
725 	 *
726 	 * Note: the inode referred by it will not be destroyed until the
727 	 * vnode is reclaimed/recycled.
728 	 */
729 
730 	tmpfs_dir_detach(dnode, de);
731 
732 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
733 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
734 	else
735 		tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
736 
737 	tflags = TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME;
738 	if (node->tn_links > 0) {
739 		/* We removed a hard link. */
740 		tflags |= TMPFS_UPDATE_CTIME;
741 	}
742 	tmpfs_update(dvp, tflags);
743 	error = 0;
744 out:
745 	/* Drop the reference and unlock the node. */
746 	if (dvp == vp) {
747 		vrele(vp);
748 	} else {
749 		vput(vp);
750 	}
751 	return error;
752 }
753 
754 /*
755  * tmpfs_link: create a hard link.
756  */
757 int
758 tmpfs_link(void *v)
759 {
760 	struct vop_link_v2_args /* {
761 		struct vnode *a_dvp;
762 		struct vnode *a_vp;
763 		struct componentname *a_cnp;
764 	} */ *ap = v;
765 	vnode_t *dvp = ap->a_dvp;
766 	vnode_t *vp = ap->a_vp;
767 	struct componentname *cnp = ap->a_cnp;
768 	tmpfs_node_t *dnode, *node;
769 	tmpfs_dirent_t *de;
770 	int error;
771 
772 	KASSERT(dvp != vp);
773 	KASSERT(VOP_ISLOCKED(dvp));
774 	KASSERT(vp->v_type != VDIR);
775 	KASSERT(dvp->v_mount == vp->v_mount);
776 
777 	dnode = VP_TO_TMPFS_DIR(dvp);
778 	node = VP_TO_TMPFS_NODE(vp);
779 
780 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
781 
782 	/* Check for maximum number of links limit. */
783 	if (node->tn_links == LINK_MAX) {
784 		error = EMLINK;
785 		goto out;
786 	}
787 	KASSERT(node->tn_links < LINK_MAX);
788 
789 	/* We cannot create links of files marked immutable or append-only. */
790 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
791 		error = EPERM;
792 		goto out;
793 	}
794 
795 	/* Allocate a new directory entry to represent the inode. */
796 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
797 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
798 	if (error) {
799 		goto out;
800 	}
801 
802 	/*
803 	 * Insert the entry into the directory.
804 	 * It will increase the inode link count.
805 	 */
806 	tmpfs_dir_attach(dnode, de, node);
807 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
808 
809 	/* Update the timestamps and trigger the event. */
810 	if (node->tn_vnode) {
811 		VN_KNOTE(node->tn_vnode, NOTE_LINK);
812 	}
813 	tmpfs_update(vp, TMPFS_UPDATE_CTIME);
814 	error = 0;
815 out:
816 	VOP_UNLOCK(vp);
817 	return error;
818 }
819 
820 int
821 tmpfs_mkdir(void *v)
822 {
823 	struct vop_mkdir_v3_args /* {
824 		struct vnode		*a_dvp;
825 		struct vnode		**a_vpp;
826 		struct componentname	*a_cnp;
827 		struct vattr		*a_vap;
828 	} */ *ap = v;
829 	vnode_t *dvp = ap->a_dvp;
830 	vnode_t **vpp = ap->a_vpp;
831 	struct componentname *cnp = ap->a_cnp;
832 	struct vattr *vap = ap->a_vap;
833 
834 	KASSERT(vap->va_type == VDIR);
835 	return tmpfs_construct_node(dvp, vpp, vap, cnp, NULL);
836 }
837 
838 int
839 tmpfs_rmdir(void *v)
840 {
841 	struct vop_rmdir_v2_args /* {
842 		struct vnode		*a_dvp;
843 		struct vnode		*a_vp;
844 		struct componentname	*a_cnp;
845 	} */ *ap = v;
846 	vnode_t *dvp = ap->a_dvp;
847 	vnode_t *vp = ap->a_vp;
848 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
849 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
850 	tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
851 	tmpfs_dirent_t *de;
852 	int error = 0;
853 
854 	KASSERT(VOP_ISLOCKED(dvp));
855 	KASSERT(VOP_ISLOCKED(vp));
856 
857 	/*
858 	 * Directories with more than two entries ('.' and '..') cannot be
859 	 * removed.  There may be whiteout entries, which we will destroy.
860 	 */
861 	if (node->tn_size > 0) {
862 		/*
863 		 * If never had whiteout entries, the directory is certainly
864 		 * not empty.  Otherwise, scan for any non-whiteout entry.
865 		 */
866 		if ((node->tn_gen & TMPFS_WHITEOUT_BIT) == 0) {
867 			error = ENOTEMPTY;
868 			goto out;
869 		}
870 		TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
871 			if (de->td_node != TMPFS_NODE_WHITEOUT) {
872 				error = ENOTEMPTY;
873 				goto out;
874 			}
875 		}
876 		KASSERT(error == 0);
877 	}
878 
879 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
880 
881 	/* Lookup the directory entry (check the cached hint first). */
882 	de = tmpfs_dir_cached(node);
883 	if (de == NULL) {
884 		struct componentname *cnp = ap->a_cnp;
885 		de = tmpfs_dir_lookup(dnode, cnp);
886 	}
887 	KASSERT(de && de->td_node == node);
888 
889 	/* Check flags to see if we are allowed to remove the directory. */
890 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
891 		error = EPERM;
892 		goto out;
893 	}
894 
895 	/* Decrement the link count for the virtual '.' entry. */
896 	node->tn_links--;
897 
898 	/* Detach the directory entry from the directory. */
899 	tmpfs_dir_detach(dnode, de);
900 
901 	/* Purge the cache for parent. */
902 	cache_purge(dvp);
903 
904 	/*
905 	 * Destroy the directory entry or replace it with a whiteout.
906 	 *
907 	 * Note: the inode referred by it will not be destroyed until the
908 	 * vnode is reclaimed.
909 	 */
910 	if (ap->a_cnp->cn_flags & DOWHITEOUT)
911 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
912 	else
913 		tmpfs_free_dirent(tmp, de);
914 
915 	/* Destroy the whiteout entries from the node. */
916 	while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
917 		KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
918 		tmpfs_dir_detach(node, de);
919 		tmpfs_free_dirent(tmp, de);
920 	}
921 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
922 
923 	KASSERT(node->tn_size == 0);
924 	KASSERT(node->tn_links == 0);
925 out:
926 	/* Release the node. */
927 	KASSERT(dvp != vp);
928 	vput(vp);
929 	return error;
930 }
931 
932 int
933 tmpfs_symlink(void *v)
934 {
935 	struct vop_symlink_v3_args /* {
936 		struct vnode		*a_dvp;
937 		struct vnode		**a_vpp;
938 		struct componentname	*a_cnp;
939 		struct vattr		*a_vap;
940 		char			*a_target;
941 	} */ *ap = v;
942 	vnode_t *dvp = ap->a_dvp;
943 	vnode_t **vpp = ap->a_vpp;
944 	struct componentname *cnp = ap->a_cnp;
945 	struct vattr *vap = ap->a_vap;
946 	char *target = ap->a_target;
947 
948 	KASSERT(vap->va_type == VLNK);
949 	return tmpfs_construct_node(dvp, vpp, vap, cnp, target);
950 }
951 
952 int
953 tmpfs_readdir(void *v)
954 {
955 	struct vop_readdir_args /* {
956 		struct vnode	*a_vp;
957 		struct uio	*a_uio;
958 		kauth_cred_t	a_cred;
959 		int		*a_eofflag;
960 		off_t		**a_cookies;
961 		int		*ncookies;
962 	} */ *ap = v;
963 	vnode_t *vp = ap->a_vp;
964 	struct uio *uio = ap->a_uio;
965 	int *eofflag = ap->a_eofflag;
966 	off_t **cookies = ap->a_cookies;
967 	int *ncookies = ap->a_ncookies;
968 	off_t startoff, cnt;
969 	tmpfs_node_t *node;
970 	int error;
971 
972 	KASSERT(VOP_ISLOCKED(vp));
973 
974 	/* This operation only makes sense on directory nodes. */
975 	if (vp->v_type != VDIR) {
976 		return ENOTDIR;
977 	}
978 	node = VP_TO_TMPFS_DIR(vp);
979 	startoff = uio->uio_offset;
980 	cnt = 0;
981 
982 	/*
983 	 * Retrieve the directory entries, unless it is being destroyed.
984 	 */
985 	if (node->tn_links) {
986 		error = tmpfs_dir_getdents(node, uio, &cnt);
987 	} else {
988 		error = 0;
989 	}
990 
991 	if (eofflag != NULL) {
992 		*eofflag = !error && uio->uio_offset == TMPFS_DIRSEQ_EOF;
993 	}
994 	if (error || cookies == NULL || ncookies == NULL) {
995 		return error;
996 	}
997 
998 	/* Update NFS-related variables, if any. */
999 	tmpfs_dirent_t *de = NULL;
1000 	off_t i, off = startoff;
1001 
1002 	*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1003 	*ncookies = cnt;
1004 
1005 	for (i = 0; i < cnt; i++) {
1006 		KASSERT(off != TMPFS_DIRSEQ_EOF);
1007 		if (off != TMPFS_DIRSEQ_DOT) {
1008 			if (off == TMPFS_DIRSEQ_DOTDOT) {
1009 				de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
1010 			} else if (de != NULL) {
1011 				de = TAILQ_NEXT(de, td_entries);
1012 			} else {
1013 				de = tmpfs_dir_lookupbyseq(node, off);
1014 				KASSERT(de != NULL);
1015 				de = TAILQ_NEXT(de, td_entries);
1016 			}
1017 			if (de == NULL) {
1018 				off = TMPFS_DIRSEQ_EOF;
1019 			} else {
1020 				off = tmpfs_dir_getseq(node, de);
1021 			}
1022 		} else {
1023 			off = TMPFS_DIRSEQ_DOTDOT;
1024 		}
1025 		(*cookies)[i] = off;
1026 	}
1027 	KASSERT(uio->uio_offset == off);
1028 	return error;
1029 }
1030 
1031 int
1032 tmpfs_readlink(void *v)
1033 {
1034 	struct vop_readlink_args /* {
1035 		struct vnode	*a_vp;
1036 		struct uio	*a_uio;
1037 		kauth_cred_t	a_cred;
1038 	} */ *ap = v;
1039 	vnode_t *vp = ap->a_vp;
1040 	struct uio *uio = ap->a_uio;
1041 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1042 	int error;
1043 
1044 	KASSERT(VOP_ISLOCKED(vp));
1045 	KASSERT(uio->uio_offset == 0);
1046 	KASSERT(vp->v_type == VLNK);
1047 
1048 	/* Note: readlink(2) returns the path without NUL terminator. */
1049 	if (node->tn_size > 0) {
1050 		error = uiomove(node->tn_spec.tn_lnk.tn_link,
1051 		    MIN(node->tn_size, uio->uio_resid), uio);
1052 	} else {
1053 		error = 0;
1054 	}
1055 	tmpfs_update(vp, TMPFS_UPDATE_ATIME);
1056 
1057 	return error;
1058 }
1059 
1060 int
1061 tmpfs_inactive(void *v)
1062 {
1063 	struct vop_inactive_v2_args /* {
1064 		struct vnode *a_vp;
1065 		bool *a_recycle;
1066 	} */ *ap = v;
1067 	vnode_t *vp = ap->a_vp;
1068 	tmpfs_node_t *node;
1069 	int error = 0;
1070 
1071 	KASSERT(VOP_ISLOCKED(vp));
1072 
1073 	node = VP_TO_TMPFS_NODE(vp);
1074 	if (node->tn_links == 0) {
1075 		/*
1076 		 * Mark node as dead by setting its generation to zero.
1077 		 */
1078 		atomic_and_32(&node->tn_gen, ~TMPFS_NODE_GEN_MASK);
1079 
1080 		/*
1081 		 * If the file has been deleted, truncate it, otherwise VFS
1082 		 * will quite rightly try to write back dirty data, which in
1083 		 * the case of tmpfs/UAO means needless page deactivations.
1084 		 */
1085 		if (vp->v_type == VREG) {
1086 			error = tmpfs_reg_resize(vp, 0);
1087 		}
1088 		*ap->a_recycle = true;
1089 	} else {
1090 		tmpfs_update(vp, 0);
1091 		*ap->a_recycle = false;
1092 	}
1093 
1094 	return error;
1095 }
1096 
1097 int
1098 tmpfs_reclaim(void *v)
1099 {
1100 	struct vop_reclaim_v2_args /* {
1101 		struct vnode *a_vp;
1102 	} */ *ap = v;
1103 	vnode_t *vp = ap->a_vp;
1104 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
1105 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1106 
1107 	/* Unlock vnode.  We still have exclusive access to it. */
1108 	VOP_UNLOCK(vp);
1109 
1110 	/* Disassociate inode from vnode. */
1111 	node->tn_vnode = NULL;
1112 	vp->v_data = NULL;
1113 
1114 	/* If inode is not referenced, i.e. no links, then destroy it. */
1115 	if (node->tn_links == 0)
1116 		tmpfs_free_node(tmp, node);
1117 	return 0;
1118 }
1119 
1120 int
1121 tmpfs_pathconf(void *v)
1122 {
1123 	struct vop_pathconf_args /* {
1124 		struct vnode	*a_vp;
1125 		int		a_name;
1126 		register_t	*a_retval;
1127 	} */ *ap = v;
1128 	register_t *retval = ap->a_retval;
1129 
1130 	switch (ap->a_name) {
1131 	case _PC_LINK_MAX:
1132 		*retval = LINK_MAX;
1133 		return 0;
1134 	case _PC_NAME_MAX:
1135 		*retval = TMPFS_MAXNAMLEN;
1136 		return 0;
1137 	case _PC_PATH_MAX:
1138 		*retval = PATH_MAX;
1139 		return 0;
1140 	case _PC_PIPE_BUF:
1141 		*retval = PIPE_BUF;
1142 		return 0;
1143 	case _PC_CHOWN_RESTRICTED:
1144 		*retval = 1;
1145 		return 0;
1146 	case _PC_NO_TRUNC:
1147 		*retval = 1;
1148 		return 0;
1149 	case _PC_SYNC_IO:
1150 		*retval = 1;
1151 		return 0;
1152 	case _PC_FILESIZEBITS:
1153 		*retval = sizeof(off_t) * CHAR_BIT;
1154 		return 0;
1155 	default:
1156 		return genfs_pathconf(ap);
1157 	}
1158 }
1159 
1160 int
1161 tmpfs_advlock(void *v)
1162 {
1163 	struct vop_advlock_args /* {
1164 		struct vnode	*a_vp;
1165 		void *		a_id;
1166 		int		a_op;
1167 		struct flock	*a_fl;
1168 		int		a_flags;
1169 	} */ *ap = v;
1170 	vnode_t *vp = ap->a_vp;
1171 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1172 
1173 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
1174 }
1175 
1176 int
1177 tmpfs_getpages(void *v)
1178 {
1179 	struct vop_getpages_args /* {
1180 		struct vnode *a_vp;
1181 		voff_t a_offset;
1182 		struct vm_page **a_m;
1183 		int *a_count;
1184 		int a_centeridx;
1185 		vm_prot_t a_access_type;
1186 		int a_advice;
1187 		int a_flags;
1188 	} */ * const ap = v;
1189 	vnode_t *vp = ap->a_vp;
1190 	const voff_t offset = ap->a_offset;
1191 	struct vm_page **pgs = ap->a_m;
1192 	const int centeridx = ap->a_centeridx;
1193 	const vm_prot_t access_type = ap->a_access_type;
1194 	const int advice = ap->a_advice;
1195 	const int flags = ap->a_flags;
1196 	int error, iflag, npages = *ap->a_count;
1197 	tmpfs_node_t *node;
1198 	struct uvm_object *uobj;
1199 
1200 	KASSERT(vp->v_type == VREG);
1201 	KASSERT(rw_lock_held(vp->v_uobj.vmobjlock));
1202 
1203 	/*
1204 	 * Currently, PGO_PASTEOF is not supported.
1205 	 */
1206 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1207 		if ((flags & PGO_LOCKED) == 0)
1208 			rw_exit(vp->v_uobj.vmobjlock);
1209 		return EINVAL;
1210 	}
1211 
1212 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1213 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1214 	}
1215 
1216 	/*
1217 	 * Check for reclaimed vnode.  v_interlock is not held here, but
1218 	 * VI_DEADCHECK is set with vmobjlock held.
1219 	 */
1220 	iflag = atomic_load_relaxed(&vp->v_iflag);
1221 	if (__predict_false((iflag & VI_DEADCHECK) != 0)) {
1222 		mutex_enter(vp->v_interlock);
1223 		error = vdead_check(vp, VDEAD_NOWAIT);
1224 		mutex_exit(vp->v_interlock);
1225 		if (error) {
1226 			if ((flags & PGO_LOCKED) == 0)
1227 				rw_exit(vp->v_uobj.vmobjlock);
1228 			return error;
1229 		}
1230 	}
1231 
1232 	node = VP_TO_TMPFS_NODE(vp);
1233 	uobj = node->tn_spec.tn_reg.tn_aobj;
1234 
1235 	/*
1236 	 * Update timestamp lazily.  The update will be made real when
1237 	 * a synchronous update is next made -- or by tmpfs_getattr,
1238 	 * tmpfs_putpages, and tmpfs_inactive.
1239 	 */
1240 	if ((flags & PGO_NOTIMESTAMP) == 0) {
1241 		u_int tflags = 0;
1242 
1243 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1244 			tflags |= TMPFS_UPDATE_ATIME;
1245 
1246 		if ((access_type & VM_PROT_WRITE) != 0) {
1247 			tflags |= TMPFS_UPDATE_MTIME;
1248 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
1249 				tflags |= TMPFS_UPDATE_ATIME;
1250 		}
1251 		tmpfs_update_lazily(vp, tflags);
1252 	}
1253 
1254 	/* Invoke the pager.  The vnode vmobjlock is shared with the UAO. */
1255 	KASSERT(vp->v_uobj.vmobjlock == uobj->vmobjlock);
1256 	error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
1257 	    access_type, advice, flags);
1258 #if defined(DEBUG)
1259 	if (!error && pgs) {
1260 		KASSERT(pgs[centeridx] != NULL);
1261 	}
1262 #endif
1263 	return error;
1264 }
1265 
1266 int
1267 tmpfs_putpages(void *v)
1268 {
1269 	struct vop_putpages_args /* {
1270 		struct vnode *a_vp;
1271 		voff_t a_offlo;
1272 		voff_t a_offhi;
1273 		int a_flags;
1274 	} */ * const ap = v;
1275 	vnode_t *vp = ap->a_vp;
1276 	const voff_t offlo = ap->a_offlo;
1277 	const voff_t offhi = ap->a_offhi;
1278 	const int flags = ap->a_flags;
1279 	tmpfs_node_t *node;
1280 	struct uvm_object *uobj;
1281 	int error;
1282 
1283 	KASSERT(rw_write_held(vp->v_uobj.vmobjlock));
1284 
1285 	if (vp->v_type != VREG) {
1286 		rw_exit(vp->v_uobj.vmobjlock);
1287 		return 0;
1288 	}
1289 
1290 	node = VP_TO_TMPFS_NODE(vp);
1291 	uobj = node->tn_spec.tn_reg.tn_aobj;
1292 
1293 	KASSERT(vp->v_uobj.vmobjlock == uobj->vmobjlock);
1294 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1295 
1296 	/* XXX mtime */
1297 
1298 	/* Process deferred updates. */
1299 	tmpfs_update(vp, 0);
1300 	return error;
1301 }
1302 
1303 int
1304 tmpfs_whiteout(void *v)
1305 {
1306 	struct vop_whiteout_args /* {
1307 		struct vnode		*a_dvp;
1308 		struct componentname	*a_cnp;
1309 		int			a_flags;
1310 	} */ *ap = v;
1311 	vnode_t *dvp = ap->a_dvp;
1312 	struct componentname *cnp = ap->a_cnp;
1313 	const int flags = ap->a_flags;
1314 	tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
1315 	tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
1316 	tmpfs_dirent_t *de;
1317 	int error;
1318 
1319 	switch (flags) {
1320 	case LOOKUP:
1321 		break;
1322 	case CREATE:
1323 		error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
1324 		    cnp->cn_namelen, &de);
1325 		if (error)
1326 			return error;
1327 		tmpfs_dir_attach(dnode, de, TMPFS_NODE_WHITEOUT);
1328 		break;
1329 	case DELETE:
1330 		cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
1331 		de = tmpfs_dir_lookup(dnode, cnp);
1332 		if (de == NULL)
1333 			return ENOENT;
1334 		tmpfs_dir_detach(dnode, de);
1335 		tmpfs_free_dirent(tmp, de);
1336 		break;
1337 	}
1338 	tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
1339 	return 0;
1340 }
1341 
1342 int
1343 tmpfs_print(void *v)
1344 {
1345 	struct vop_print_args /* {
1346 		struct vnode	*a_vp;
1347 	} */ *ap = v;
1348 	vnode_t *vp = ap->a_vp;
1349 	tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1350 
1351 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
1352 	    "\tmode 0%o, owner %d, group %d, size %" PRIdMAX,
1353 	    node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
1354 	    node->tn_gid, (uintmax_t)node->tn_size);
1355 	if (vp->v_type == VFIFO) {
1356 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
1357 	}
1358 	printf("\n");
1359 	return 0;
1360 }
1361