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