xref: /src/sys/fs/fuse/fuse_node.c (revision 7e68af7ce2c1b892954df415774fe59fd2f1b62f) !
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following disclaimer
15  *   in the documentation and/or other materials provided with the
16  *   distribution.
17  * * Neither the name of Google Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Copyright (c) 2019 The FreeBSD Foundation
37  *
38  * Portions of this software were developed by BFF Storage Systems, LLC under
39  * sponsorship from the FreeBSD Foundation.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/types.h>
64 #include <sys/systm.h>
65 #include <sys/counter.h>
66 #include <sys/module.h>
67 #include <sys/errno.h>
68 #include <sys/param.h>
69 #include <sys/kernel.h>
70 #include <sys/conf.h>
71 #include <sys/uio.h>
72 #include <sys/malloc.h>
73 #include <sys/queue.h>
74 #include <sys/lock.h>
75 #include <sys/sx.h>
76 #include <sys/mutex.h>
77 #include <sys/proc.h>
78 #include <sys/vnode.h>
79 #include <sys/namei.h>
80 #include <sys/mount.h>
81 #include <sys/sysctl.h>
82 #include <sys/fcntl.h>
83 #include <sys/priv.h>
84 #include <sys/buf.h>
85 #include <security/mac/mac_framework.h>
86 #include <vm/vm.h>
87 #include <vm/vm_extern.h>
88 
89 #include "fuse.h"
90 #include "fuse_node.h"
91 #include "fuse_internal.h"
92 #include "fuse_io.h"
93 #include "fuse_ipc.h"
94 
95 SDT_PROVIDER_DECLARE(fusefs);
96 /*
97  * Fuse trace probe:
98  * arg0: verbosity.  Higher numbers give more verbose messages
99  * arg1: Textual message
100  */
101 SDT_PROBE_DEFINE2(fusefs, , node, trace, "int", "char*");
102 
103 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data");
104 
105 static int sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS);
106 
107 static counter_u64_t fuse_node_count;
108 
109 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, node_count, CTLFLAG_RD,
110     &fuse_node_count, "Count of FUSE vnodes");
111 
112 int	fuse_data_cache_mode = FUSE_CACHE_WT;
113 
114 /*
115  * OBSOLETE
116  * This sysctl is no longer needed as of fuse protocol 7.23.  Now, individual
117  * servers can select the cache behavior they need for each mountpoint:
118  * - writethrough: the default
119  * - writeback: set FUSE_WRITEBACK_CACHE in fuse_init_out.flags
120  * - uncached: set FOPEN_DIRECT_IO for every file
121  * The sysctl is retained primarily due to the enduring popularity of libfuse2,
122  * which is frozen at protocol version 7.19.  As of 4-April-2024, 90% of
123  * FreeBSD ports that use libfuse still bind to libfuse2.
124  */
125 SYSCTL_PROC(_vfs_fusefs, OID_AUTO, data_cache_mode,
126     CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW,
127     &fuse_data_cache_mode, 0, sysctl_fuse_cache_mode, "I",
128     "Zero: disable caching of FUSE file data; One: write-through caching "
129     "(default); Two: write-back caching (generally unsafe)");
130 
131 static int
sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS)132 sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS)
133 {
134 	int val, error;
135 
136 	val = *(int *)arg1;
137 	error = sysctl_handle_int(oidp, &val, 0, req);
138 	if (error || !req->newptr)
139 		return (error);
140 
141 	switch (val) {
142 	case FUSE_CACHE_UC:
143 	case FUSE_CACHE_WT:
144 	case FUSE_CACHE_WB:
145 		*(int *)arg1 = val;
146 		break;
147 	default:
148 		return (EDOM);
149 	}
150 	return (0);
151 }
152 
153 static void
fuse_vnode_init(struct vnode * vp,struct fuse_vnode_data * fvdat,uint64_t nodeid,__enum_uint8 (vtype)vtyp)154 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat,
155     uint64_t nodeid, __enum_uint8(vtype) vtyp)
156 {
157 	fvdat->nid = nodeid;
158 	LIST_INIT(&fvdat->handles);
159 
160 	mtx_init(&fvdat->cached_attr_mtx, "fuse attr cache mutex", NULL,
161 	    MTX_DEF);
162 	vattr_null(&fvdat->cached_attrs);
163 	fvdat->cached_attrs.va_birthtime.tv_sec = -1;
164 	fvdat->cached_attrs.va_birthtime.tv_nsec = 0;
165 	fvdat->cached_attrs.va_fsid = VNOVAL;
166 	fvdat->cached_attrs.va_gen = 0;
167 	fvdat->cached_attrs.va_rdev = NODEV;
168 
169 	if (nodeid == FUSE_ROOT_ID) {
170 		vp->v_vflag |= VV_ROOT;
171 	}
172 	vp->v_type = vtyp;
173 	vp->v_data = fvdat;
174 	cluster_init_vn(&fvdat->clusterw);
175 	timespecclear(&fvdat->last_local_modify);
176 
177 	counter_u64_add(fuse_node_count, 1);
178 }
179 
180 void
fuse_vnode_destroy(struct vnode * vp)181 fuse_vnode_destroy(struct vnode *vp)
182 {
183 	struct fuse_vnode_data *fvdat = vp->v_data;
184 
185 	vp->v_data = NULL;
186 	mtx_destroy(&fvdat->cached_attr_mtx);
187 	KASSERT(LIST_EMPTY(&fvdat->handles),
188 		("Destroying fuse vnode with open files!"));
189 	free(fvdat, M_FUSEVN);
190 
191 	counter_u64_add(fuse_node_count, -1);
192 }
193 
194 int
fuse_vnode_cmp(struct vnode * vp,void * nidp)195 fuse_vnode_cmp(struct vnode *vp, void *nidp)
196 {
197 	return (VTOI(vp) != *((uint64_t *)nidp));
198 }
199 
200 SDT_PROBE_DEFINE3(fusefs, , node, stale_vnode, "struct vnode*", "uint8_t",
201 		"uint64_t");
202 static int
fuse_vnode_alloc(struct mount * mp,struct thread * td,uint64_t nodeid,__enum_uint8 (vtype)vtyp,struct vnode ** vpp)203 fuse_vnode_alloc(struct mount *mp,
204     struct thread *td,
205     uint64_t nodeid,
206     __enum_uint8(vtype) vtyp,
207     struct vnode **vpp)
208 {
209 	struct fuse_data *data;
210 	struct fuse_vnode_data *fvdat;
211 	struct vnode *vp2;
212 	int err = 0;
213 
214 	data = fuse_get_mpdata(mp);
215 	if (vtyp == VNON) {
216 		return EINVAL;
217 	}
218 	*vpp = NULL;
219 	err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp,
220 	    fuse_vnode_cmp, &nodeid);
221 	if (err)
222 		return (err);
223 
224 	if (*vpp) {
225 		if ((*vpp)->v_type == vtyp) {
226 			/* Reuse a vnode that hasn't yet been reclaimed */
227 			MPASS((*vpp)->v_data != NULL);
228 			MPASS(VTOFUD(*vpp)->nid == nodeid);
229 			SDT_PROBE2(fusefs, , node, trace, 1,
230 				"vnode taken from hash");
231 			return (0);
232 		} else {
233 			/*
234 			 * The inode changed types!  If we get here, we can't
235 			 * tell whether the inode's entry cache had expired
236 			 * yet.  So this could be the result of a buggy server,
237 			 * but more likely the server just reused an inode
238 			 * number following an entry cache expiration.
239 			 */
240 			SDT_PROBE3(fusefs, , node, stale_vnode, *vpp, vtyp,
241 				nodeid);
242 			fuse_internal_vnode_disappear(*vpp);
243 			vgone(*vpp);
244 			lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
245 		}
246 	}
247 	fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO);
248 	switch (vtyp) {
249 	case VFIFO:
250 		err = getnewvnode("fuse", mp, &fuse_fifoops, vpp);
251 		break;
252 	default:
253 		err = getnewvnode("fuse", mp, &fuse_vnops, vpp);
254 		break;
255 	}
256 	if (err) {
257 		free(fvdat, M_FUSEVN);
258 		return (err);
259 	}
260 	lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL);
261 	fuse_vnode_init(*vpp, fvdat, nodeid, vtyp);
262 	err = insmntque(*vpp, mp);
263 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
264 	if (err) {
265 		lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
266 		free(fvdat, M_FUSEVN);
267 		*vpp = NULL;
268 		return (err);
269 	}
270 	/* Disallow async reads for fifos because UFS does.  I don't know why */
271 	if (data->dataflags & FSESS_ASYNC_READ && vtyp != VFIFO)
272 		VN_LOCK_ASHARE(*vpp);
273 
274 	vn_set_state(*vpp, VSTATE_CONSTRUCTED);
275 	err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE,
276 	    td, &vp2, fuse_vnode_cmp, &nodeid);
277 	if (err) {
278 		lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
279 		free(fvdat, M_FUSEVN);
280 		*vpp = NULL;
281 		return (err);
282 	}
283 	if (vp2 != NULL) {
284 		*vpp = vp2;
285 		return (0);
286 	}
287 
288 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
289 
290 	return (0);
291 }
292 
293 int
fuse_vnode_get(struct mount * mp,struct fuse_entry_out * feo,uint64_t nodeid,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,__enum_uint8 (vtype)vtyp)294 fuse_vnode_get(struct mount *mp,
295     struct fuse_entry_out *feo,
296     uint64_t nodeid,
297     struct vnode *dvp,
298     struct vnode **vpp,
299     struct componentname *cnp,
300     __enum_uint8(vtype) vtyp)
301 {
302 	struct thread *td = curthread;
303 	bool exportable = fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT;
304 
305 	/*
306 	 * feo should only be NULL for the root directory, which (when libfuse
307 	 * is used) always has generation 0
308 	 */
309 	uint64_t generation = feo ? feo->generation : 0;
310 	int err = 0;
311 
312 	if (dvp != NULL && VTOFUD(dvp)->nid == nodeid) {
313 		fuse_warn(fuse_get_mpdata(mp), FSESS_WARN_ILLEGAL_INODE,
314 			"Assigned same inode to both parent and child.");
315 		return EIO;
316 	}
317 	if (feo && feo->nodeid != feo->attr.ino && exportable) {
318 		/*
319 		 * NFS servers (both kernelspace and userspace) rely on
320 		 * VFS_VGET to lookup inodes.  But that's only possible if the
321 		 * file's inode number matches its nodeid, which isn't
322 		 * necessarily the case for FUSE.  If they don't match, then we
323 		 * can complete the current operation, but future VFS_VGET
324 		 * operations will almost certainly return spurious results.
325 		 * Warn the operator.
326 		 *
327 		 * But only warn the operator if the file system reports
328 		 * NFS-compatibility, because that's the only time that this
329 		 * matters, and dumb fuse servers abound.
330 		 */
331 		fuse_warn(fuse_get_mpdata(mp), FSESS_WARN_INODE_MISMATCH,
332 		    "file has different inode number and nodeid.");
333 	}
334 
335 	err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp);
336 	if (err) {
337 		return err;
338 	}
339 	if (dvp != NULL) {
340 		MPASS(cnp && (cnp->cn_flags & ISDOTDOT) == 0);
341 		MPASS(cnp &&
342 			!(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'));
343 		fuse_vnode_setparent(*vpp, dvp);
344 	}
345 	if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0 &&
346 	    feo != NULL &&
347 	    (feo->entry_valid != 0 || feo->entry_valid_nsec != 0)) {
348 		struct timespec timeout;
349 
350 		ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get");
351 		ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get");
352 
353 		fuse_validity_2_timespec(feo, &timeout);
354 		cache_enter_time(dvp, *vpp, cnp, &timeout, NULL);
355 	}
356 
357 	VTOFUD(*vpp)->generation = generation;
358 	/*
359 	 * In userland, libfuse uses cached lookups for dot and dotdot entries,
360 	 * thus it does not really bump the nlookup counter for forget.
361 	 * Follow the same semantic and avoid the bump in order to keep
362 	 * nlookup counters consistent.
363 	 */
364 	if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 &&
365 	    (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.')))
366 		VTOFUD(*vpp)->nlookup++;
367 
368 	return 0;
369 }
370 
371 /*
372  * Called for every fusefs vnode open to initialize the vnode (not
373  * fuse_filehandle) for use
374  */
375 void
fuse_vnode_open(struct vnode * vp,int32_t fuse_open_flags,struct thread * td)376 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td)
377 {
378 	if (vnode_vtype(vp) == VREG)
379 		vnode_create_vobject(vp, VNODE_NO_SIZE, td);
380 }
381 
382 int
fuse_vnode_savesize(struct vnode * vp,struct ucred * cred,pid_t pid)383 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid)
384 {
385 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
386 	struct thread *td = curthread;
387 	struct fuse_filehandle *fufh = NULL;
388 	struct fuse_dispatcher fdi;
389 	struct fuse_setattr_in *fsai;
390 	int err = 0;
391 
392 	ASSERT_VOP_ELOCKED(vp, __func__); /* For flag and last_local_modify */
393 	ASSERT_CACHED_ATTRS_LOCKED(vp);
394 
395 	if (fuse_isdeadfs(vp)) {
396 		return EBADF;
397 	}
398 	if (vnode_vtype(vp) == VDIR) {
399 		return EISDIR;
400 	}
401 	if (vfs_isrdonly(vnode_mount(vp))) {
402 		return EROFS;
403 	}
404 	if (cred == NULL) {
405 		cred = td->td_ucred;
406 	}
407 	fdisp_init(&fdi, sizeof(*fsai));
408 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
409 	fsai = fdi.indata;
410 	fsai->valid = 0;
411 
412 	/* Truncate to a new value. */
413 	MPASS((fvdat->flag & FN_SIZECHANGE) != 0);
414 	fsai->size = fvdat->cached_attrs.va_size;
415 	fsai->valid |= FATTR_SIZE;
416 
417 	fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
418 	if (fufh) {
419 		fsai->fh = fufh->fh_id;
420 		fsai->valid |= FATTR_FH;
421 	}
422 	err = fdisp_wait_answ(&fdi);
423 	fdisp_destroy(&fdi);
424 	if (err == 0) {
425 		getnanouptime(&fvdat->last_local_modify);
426 		fvdat->flag &= ~FN_SIZECHANGE;
427 	}
428 
429 	return err;
430 }
431 
432 /*
433  * Adjust the vnode's size to a new value.
434  *
435  * If the new value came from the server, such as from a FUSE_GETATTR
436  * operation, set `from_server` true.  But if it came from a local operation,
437  * such as write(2) or truncate(2), set `from_server` false.
438  */
439 int
fuse_vnode_setsize(struct vnode * vp,off_t newsize,bool from_server)440 fuse_vnode_setsize(struct vnode *vp, off_t newsize, bool from_server)
441 {
442 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
443 	struct vattr *attrs;
444 	off_t oldsize;
445 	size_t iosize;
446 	int err = 0;
447 
448 	ASSERT_VOP_LOCKED(vp, __func__);
449 	ASSERT_CACHED_ATTRS_LOCKED(vp);
450 
451 	iosize = fuse_iosize(vp);
452 	oldsize = fvdat->cached_attrs.va_size;
453 	fvdat->cached_attrs.va_size = newsize;
454 	if ((attrs = VTOVA(vp)) != NULL)
455 		attrs->va_size = newsize;
456 
457 	if (from_server && newsize > oldsize && oldsize != VNOVAL) {
458 		/*
459 		 * The FUSE server changed the file size behind our back.  We
460 		 * should invalidate the entire cache.
461 		 */
462 		daddr_t end_lbn;
463 
464 		end_lbn = howmany(newsize, iosize);
465 		v_inval_buf_range(vp, 0, end_lbn, iosize);
466 	}
467 
468 	if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
469 		err = fuse_vnode_setsize_immediate(vp, newsize < oldsize);
470 	} else {
471 		/* Without an exclusive vnode lock, we must defer the operation */
472 		fvdat->flag |= FN_DELAYED_TRUNCATE;
473 		vn_delayed_setsize(vp);
474 	}
475 
476 	return err;
477 }
478 
479 /* Immediately set the vnode's size in the pager */
480 int
fuse_vnode_setsize_immediate(struct vnode * vp,bool shrink)481 fuse_vnode_setsize_immediate(struct vnode *vp, bool shrink)
482 {
483 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
484 	struct buf *bp = NULL;
485 	size_t iosize;
486 	off_t newsize;
487 	int err = 0;
488 
489 	MPASS(fvdat);
490 	ASSERT_VOP_ELOCKED(vp, __func__);
491 
492 	iosize = fuse_iosize(vp);
493 	newsize = fvdat->cached_attrs.va_size;
494 
495 	if (shrink) {
496 		daddr_t lbn;
497 
498 		err = vtruncbuf(vp, newsize, fuse_iosize(vp));
499 		if (err)
500 			goto out;
501 		if (newsize % iosize == 0)
502 			goto out;
503 		/*
504 		 * Zero the contents of the last partial block.
505 		 * Sure seems like vtruncbuf should do this for us.
506 		 */
507 
508 		lbn = newsize / iosize;
509 		bp = getblk(vp, lbn, iosize, PCATCH, 0, 0);
510 		if (!bp) {
511 			err = EINTR;
512 			goto out;
513 		}
514 		if (!(bp->b_flags & B_CACHE))
515 			goto out;	/* Nothing to do */
516 		MPASS(bp->b_flags & B_VMIO);
517 		vfs_bio_clrbuf(bp);
518 		bp->b_dirtyend = MIN(bp->b_dirtyend, newsize - lbn * iosize);
519 	}
520 out:
521 	if (bp)
522 		brelse(bp);
523 	vnode_pager_setsize(vp, newsize);
524 
525 	return (err);
526 }
527 
528 /* Get the current, possibly dirty, size of the file */
529 int
fuse_vnode_size(struct vnode * vp,off_t * filesize,struct ucred * cred,struct thread * td)530 fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred,
531 	struct thread *td)
532 {
533 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
534 	struct vattr va;
535 	int error = 0;
536 
537 	ASSERT_VOP_LOCKED(vp, __func__);
538 
539 	CACHED_ATTR_LOCK(vp);
540 	if (!(fvdat->flag & FN_SIZECHANGE) &&
541 		(!fuse_vnode_attr_cache_valid(vp) ||
542 		  fvdat->cached_attrs.va_size == VNOVAL)) {
543 		CACHED_ATTR_UNLOCK(vp);
544 		/*
545 		 * It incurs a large struct copy, but we supply &va so we don't
546 		 * have to acquire the lock a second time after
547 		 * fuse_internal_do_getattr returns.
548 		 */
549 		error = fuse_internal_do_getattr(vp, &va, cred, td);
550 		if (!error)
551 			*filesize = va.va_size;
552 	} else {
553 		*filesize = fvdat->cached_attrs.va_size;
554 		CACHED_ATTR_UNLOCK(vp);
555 	}
556 
557 	return error;
558 }
559 
560 void
fuse_vnode_undirty_cached_timestamps(struct vnode * vp,bool atime)561 fuse_vnode_undirty_cached_timestamps(struct vnode *vp, bool atime)
562 {
563 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
564 
565 	ASSERT_CACHED_ATTRS_LOCKED(vp);
566 
567 	fvdat->flag &= ~(FN_MTIMECHANGE | FN_CTIMECHANGE);
568 	if (atime)
569 		fvdat->flag &= ~FN_ATIMECHANGE;
570 }
571 
572 /* Update a fuse file's cached timestamps */
573 void
fuse_vnode_update(struct vnode * vp,int flags)574 fuse_vnode_update(struct vnode *vp, int flags)
575 {
576 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
577 	struct mount *mp = vnode_mount(vp);
578 	struct fuse_data *data = fuse_get_mpdata(mp);
579 	struct timespec ts;
580 
581 	vfs_timestamp(&ts);
582 
583 	if (data->time_gran > 1)
584 		ts.tv_nsec = rounddown(ts.tv_nsec, data->time_gran);
585 
586 	if (mp->mnt_flag & MNT_NOATIME)
587 		flags &= ~FN_ATIMECHANGE;
588 
589 	CACHED_ATTR_LOCK(vp);
590 
591 	if (flags & FN_ATIMECHANGE)
592 		fvdat->cached_attrs.va_atime = ts;
593 	if (flags & FN_MTIMECHANGE)
594 		fvdat->cached_attrs.va_mtime = ts;
595 	if (flags & FN_CTIMECHANGE)
596 		fvdat->cached_attrs.va_ctime = ts;
597 
598 	fvdat->flag |= flags;
599 
600 	CACHED_ATTR_UNLOCK(vp);
601 }
602 
603 void
fuse_node_init(void)604 fuse_node_init(void)
605 {
606 	fuse_node_count = counter_u64_alloc(M_WAITOK);
607 }
608 
609 void
fuse_node_destroy(void)610 fuse_node_destroy(void)
611 {
612 	counter_u64_free(fuse_node_count);
613 }
614