xref: /freebsd/sys/fs/fuse/fuse_node.c (revision 42249ef2)
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/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include <sys/types.h>
67 #include <sys/systm.h>
68 #include <sys/counter.h>
69 #include <sys/module.h>
70 #include <sys/errno.h>
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/conf.h>
74 #include <sys/uio.h>
75 #include <sys/malloc.h>
76 #include <sys/queue.h>
77 #include <sys/lock.h>
78 #include <sys/sx.h>
79 #include <sys/mutex.h>
80 #include <sys/proc.h>
81 #include <sys/vnode.h>
82 #include <sys/namei.h>
83 #include <sys/mount.h>
84 #include <sys/sysctl.h>
85 #include <sys/fcntl.h>
86 #include <sys/priv.h>
87 #include <sys/buf.h>
88 #include <security/mac/mac_framework.h>
89 #include <vm/vm.h>
90 #include <vm/vm_extern.h>
91 
92 #include "fuse.h"
93 #include "fuse_node.h"
94 #include "fuse_internal.h"
95 #include "fuse_io.h"
96 #include "fuse_ipc.h"
97 
98 SDT_PROVIDER_DECLARE(fusefs);
99 /*
100  * Fuse trace probe:
101  * arg0: verbosity.  Higher numbers give more verbose messages
102  * arg1: Textual message
103  */
104 SDT_PROBE_DEFINE2(fusefs, , node, trace, "int", "char*");
105 
106 MALLOC_DEFINE(M_FUSEVN, "fuse_vnode", "fuse vnode private data");
107 
108 static int sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS);
109 
110 static counter_u64_t fuse_node_count;
111 
112 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, node_count, CTLFLAG_RD,
113     &fuse_node_count, "Count of FUSE vnodes");
114 
115 int	fuse_data_cache_mode = FUSE_CACHE_WT;
116 
117 /*
118  * DEPRECATED
119  * This sysctl is no longer needed as of fuse protocol 7.23.  Individual
120  * servers can select the cache behavior they need for each mountpoint:
121  * - writethrough: the default
122  * - writeback: set FUSE_WRITEBACK_CACHE in fuse_init_out.flags
123  * - uncached: set FOPEN_DIRECT_IO for every file
124  * The sysctl is retained primarily for use by jails supporting older FUSE
125  * protocols.  It may be removed entirely once FreeBSD 11.3 and 12.0 are EOL.
126  */
127 SYSCTL_PROC(_vfs_fusefs, OID_AUTO, data_cache_mode, CTLTYPE_INT|CTLFLAG_RW,
128     &fuse_data_cache_mode, 0, sysctl_fuse_cache_mode, "I",
129     "Zero: disable caching of FUSE file data; One: write-through caching "
130     "(default); Two: write-back caching (generally unsafe)");
131 
132 static int
133 sysctl_fuse_cache_mode(SYSCTL_HANDLER_ARGS)
134 {
135 	int val, error;
136 
137 	val = *(int *)arg1;
138 	error = sysctl_handle_int(oidp, &val, 0, req);
139 	if (error || !req->newptr)
140 		return (error);
141 
142 	switch (val) {
143 	case FUSE_CACHE_UC:
144 	case FUSE_CACHE_WT:
145 	case FUSE_CACHE_WB:
146 		*(int *)arg1 = val;
147 		break;
148 	default:
149 		return (EDOM);
150 	}
151 	return (0);
152 }
153 
154 static void
155 fuse_vnode_init(struct vnode *vp, struct fuse_vnode_data *fvdat,
156     uint64_t nodeid, enum vtype vtyp)
157 {
158 	fvdat->nid = nodeid;
159 	LIST_INIT(&fvdat->handles);
160 	vattr_null(&fvdat->cached_attrs);
161 	if (nodeid == FUSE_ROOT_ID) {
162 		vp->v_vflag |= VV_ROOT;
163 	}
164 	vp->v_type = vtyp;
165 	vp->v_data = fvdat;
166 
167 	counter_u64_add(fuse_node_count, 1);
168 }
169 
170 void
171 fuse_vnode_destroy(struct vnode *vp)
172 {
173 	struct fuse_vnode_data *fvdat = vp->v_data;
174 
175 	vp->v_data = NULL;
176 	KASSERT(LIST_EMPTY(&fvdat->handles),
177 		("Destroying fuse vnode with open files!"));
178 	free(fvdat, M_FUSEVN);
179 
180 	counter_u64_add(fuse_node_count, -1);
181 }
182 
183 int
184 fuse_vnode_cmp(struct vnode *vp, void *nidp)
185 {
186 	return (VTOI(vp) != *((uint64_t *)nidp));
187 }
188 
189 SDT_PROBE_DEFINE3(fusefs, , node, stale_vnode, "struct vnode*", "enum vtype",
190 		"uint64_t");
191 static int
192 fuse_vnode_alloc(struct mount *mp,
193     struct thread *td,
194     uint64_t nodeid,
195     enum vtype vtyp,
196     struct vnode **vpp)
197 {
198 	struct fuse_data *data;
199 	struct fuse_vnode_data *fvdat;
200 	struct vnode *vp2;
201 	int err = 0;
202 
203 	data = fuse_get_mpdata(mp);
204 	if (vtyp == VNON) {
205 		return EINVAL;
206 	}
207 	*vpp = NULL;
208 	err = vfs_hash_get(mp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE, td, vpp,
209 	    fuse_vnode_cmp, &nodeid);
210 	if (err)
211 		return (err);
212 
213 	if (*vpp) {
214 		if ((*vpp)->v_type != vtyp) {
215 			/*
216 			 * STALE vnode!  This probably indicates a buggy
217 			 * server, but it could also be the result of a race
218 			 * between FUSE_LOOKUP and another client's
219 			 * FUSE_UNLINK/FUSE_CREATE
220 			 */
221 			SDT_PROBE3(fusefs, , node, stale_vnode, *vpp, vtyp,
222 				nodeid);
223 			fuse_internal_vnode_disappear(*vpp);
224 			lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
225 			*vpp = NULL;
226 			return (EAGAIN);
227 		}
228 		MPASS((*vpp)->v_data != NULL);
229 		MPASS(VTOFUD(*vpp)->nid == nodeid);
230 		SDT_PROBE2(fusefs, , node, trace, 1, "vnode taken from hash");
231 		return (0);
232 	}
233 	fvdat = malloc(sizeof(*fvdat), M_FUSEVN, M_WAITOK | M_ZERO);
234 	switch (vtyp) {
235 	case VFIFO:
236 		err = getnewvnode("fuse", mp, &fuse_fifoops, vpp);
237 		break;
238 	default:
239 		err = getnewvnode("fuse", mp, &fuse_vnops, vpp);
240 		break;
241 	}
242 	if (err) {
243 		free(fvdat, M_FUSEVN);
244 		return (err);
245 	}
246 	lockmgr((*vpp)->v_vnlock, LK_EXCLUSIVE, NULL);
247 	fuse_vnode_init(*vpp, fvdat, nodeid, vtyp);
248 	err = insmntque(*vpp, mp);
249 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
250 	if (err) {
251 		lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
252 		free(fvdat, M_FUSEVN);
253 		*vpp = NULL;
254 		return (err);
255 	}
256 	/* Disallow async reads for fifos because UFS does.  I don't know why */
257 	if (data->dataflags & FSESS_ASYNC_READ && vtyp != VFIFO)
258 		VN_LOCK_ASHARE(*vpp);
259 
260 	err = vfs_hash_insert(*vpp, fuse_vnode_hash(nodeid), LK_EXCLUSIVE,
261 	    td, &vp2, fuse_vnode_cmp, &nodeid);
262 	if (err) {
263 		lockmgr((*vpp)->v_vnlock, LK_RELEASE, NULL);
264 		free(fvdat, M_FUSEVN);
265 		*vpp = NULL;
266 		return (err);
267 	}
268 	if (vp2 != NULL) {
269 		*vpp = vp2;
270 		return (0);
271 	}
272 
273 	ASSERT_VOP_ELOCKED(*vpp, "fuse_vnode_alloc");
274 
275 	return (0);
276 }
277 
278 int
279 fuse_vnode_get(struct mount *mp,
280     struct fuse_entry_out *feo,
281     uint64_t nodeid,
282     struct vnode *dvp,
283     struct vnode **vpp,
284     struct componentname *cnp,
285     enum vtype vtyp)
286 {
287 	struct thread *td = (cnp != NULL ? cnp->cn_thread : curthread);
288 	/*
289 	 * feo should only be NULL for the root directory, which (when libfuse
290 	 * is used) always has generation 0
291 	 */
292 	uint64_t generation = feo ? feo->generation : 0;
293 	int err = 0;
294 
295 	err = fuse_vnode_alloc(mp, td, nodeid, vtyp, vpp);
296 	if (err) {
297 		return err;
298 	}
299 	if (dvp != NULL) {
300 		MPASS(cnp && (cnp->cn_flags & ISDOTDOT) == 0);
301 		MPASS(cnp &&
302 			!(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'));
303 		fuse_vnode_setparent(*vpp, dvp);
304 	}
305 	if (dvp != NULL && cnp != NULL && (cnp->cn_flags & MAKEENTRY) != 0 &&
306 	    feo != NULL &&
307 	    (feo->entry_valid != 0 || feo->entry_valid_nsec != 0)) {
308 		struct timespec timeout;
309 
310 		ASSERT_VOP_LOCKED(*vpp, "fuse_vnode_get");
311 		ASSERT_VOP_LOCKED(dvp, "fuse_vnode_get");
312 
313 		fuse_validity_2_timespec(feo, &timeout);
314 		cache_enter_time(dvp, *vpp, cnp, &timeout, NULL);
315 	}
316 
317 	VTOFUD(*vpp)->generation = generation;
318 	/*
319 	 * In userland, libfuse uses cached lookups for dot and dotdot entries,
320 	 * thus it does not really bump the nlookup counter for forget.
321 	 * Follow the same semantic and avoid the bump in order to keep
322 	 * nlookup counters consistent.
323 	 */
324 	if (cnp == NULL || ((cnp->cn_flags & ISDOTDOT) == 0 &&
325 	    (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.')))
326 		VTOFUD(*vpp)->nlookup++;
327 
328 	return 0;
329 }
330 
331 /*
332  * Called for every fusefs vnode open to initialize the vnode (not
333  * fuse_filehandle) for use
334  */
335 void
336 fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags, struct thread *td)
337 {
338 	if (vnode_vtype(vp) == VREG)
339 		vnode_create_vobject(vp, 0, td);
340 }
341 
342 int
343 fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid)
344 {
345 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
346 	struct thread *td = curthread;
347 	struct fuse_filehandle *fufh = NULL;
348 	struct fuse_dispatcher fdi;
349 	struct fuse_setattr_in *fsai;
350 	int err = 0;
351 
352 	ASSERT_VOP_ELOCKED(vp, "fuse_io_extend");
353 
354 	if (fuse_isdeadfs(vp)) {
355 		return EBADF;
356 	}
357 	if (vnode_vtype(vp) == VDIR) {
358 		return EISDIR;
359 	}
360 	if (vfs_isrdonly(vnode_mount(vp))) {
361 		return EROFS;
362 	}
363 	if (cred == NULL) {
364 		cred = td->td_ucred;
365 	}
366 	fdisp_init(&fdi, sizeof(*fsai));
367 	fdisp_make_vp(&fdi, FUSE_SETATTR, vp, td, cred);
368 	fsai = fdi.indata;
369 	fsai->valid = 0;
370 
371 	/* Truncate to a new value. */
372 	MPASS((fvdat->flag & FN_SIZECHANGE) != 0);
373 	fsai->size = fvdat->cached_attrs.va_size;
374 	fsai->valid |= FATTR_SIZE;
375 
376 	fuse_filehandle_getrw(vp, FWRITE, &fufh, cred, pid);
377 	if (fufh) {
378 		fsai->fh = fufh->fh_id;
379 		fsai->valid |= FATTR_FH;
380 	}
381 	err = fdisp_wait_answ(&fdi);
382 	fdisp_destroy(&fdi);
383 	if (err == 0)
384 		fvdat->flag &= ~FN_SIZECHANGE;
385 
386 	return err;
387 }
388 
389 /*
390  * Adjust the vnode's size to a new value, such as that provided by
391  * FUSE_GETATTR.
392  */
393 int
394 fuse_vnode_setsize(struct vnode *vp, off_t newsize)
395 {
396 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
397 	struct vattr *attrs;
398 	off_t oldsize;
399 	size_t iosize;
400 	struct buf *bp = NULL;
401 	int err = 0;
402 
403 	ASSERT_VOP_ELOCKED(vp, "fuse_vnode_setsize");
404 
405 	iosize = fuse_iosize(vp);
406 	oldsize = fvdat->cached_attrs.va_size;
407 	fvdat->cached_attrs.va_size = newsize;
408 	if ((attrs = VTOVA(vp)) != NULL)
409 		attrs->va_size = newsize;
410 
411 	if (newsize < oldsize) {
412 		daddr_t lbn;
413 
414 		err = vtruncbuf(vp, newsize, fuse_iosize(vp));
415 		if (err)
416 			goto out;
417 		if (newsize % iosize == 0)
418 			goto out;
419 		/*
420 		 * Zero the contents of the last partial block.
421 		 * Sure seems like vtruncbuf should do this for us.
422 		 */
423 
424 		lbn = newsize / iosize;
425 		bp = getblk(vp, lbn, iosize, PCATCH, 0, 0);
426 		if (!bp) {
427 			err = EINTR;
428 			goto out;
429 		}
430 		if (!(bp->b_flags & B_CACHE))
431 			goto out;	/* Nothing to do */
432 		MPASS(bp->b_flags & B_VMIO);
433 		vfs_bio_clrbuf(bp);
434 		bp->b_dirtyend = MIN(bp->b_dirtyend, newsize - lbn * iosize);
435 	}
436 out:
437 	if (bp)
438 		brelse(bp);
439 	vnode_pager_setsize(vp, newsize);
440 	return err;
441 }
442 
443 /* Get the current, possibly dirty, size of the file */
444 int
445 fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred,
446 	struct thread *td)
447 {
448 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
449 	int error = 0;
450 
451 	if (!(fvdat->flag & FN_SIZECHANGE) &&
452 		(VTOVA(vp) == NULL || fvdat->cached_attrs.va_size == VNOVAL))
453 		error = fuse_internal_do_getattr(vp, NULL, cred, td);
454 
455 	if (!error)
456 		*filesize = fvdat->cached_attrs.va_size;
457 
458 	return error;
459 }
460 
461 void
462 fuse_vnode_undirty_cached_timestamps(struct vnode *vp)
463 {
464 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
465 
466 	fvdat->flag &= ~(FN_MTIMECHANGE | FN_CTIMECHANGE);
467 }
468 
469 /* Update a fuse file's cached timestamps */
470 void
471 fuse_vnode_update(struct vnode *vp, int flags)
472 {
473 	struct fuse_vnode_data *fvdat = VTOFUD(vp);
474 	struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
475 	struct timespec ts;
476 
477 	vfs_timestamp(&ts);
478 
479 	if (data->time_gran > 1)
480 		ts.tv_nsec = rounddown(ts.tv_nsec, data->time_gran);
481 
482 	if (flags & FN_MTIMECHANGE)
483 		fvdat->cached_attrs.va_mtime = ts;
484 	if (flags & FN_CTIMECHANGE)
485 		fvdat->cached_attrs.va_ctime = ts;
486 
487 	fvdat->flag |= flags;
488 }
489 
490 void
491 fuse_node_init(void)
492 {
493 	fuse_node_count = counter_u64_alloc(M_WAITOK);
494 }
495 
496 void
497 fuse_node_destroy(void)
498 {
499 	counter_u64_free(fuse_node_count);
500 }
501