xref: /freebsd/sys/fs/fuse/fuse_main.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc.
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
368aafc8c3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
378aafc8c3SAlan Somers  *
388aafc8c3SAlan Somers  * Portions of this software were developed by BFF Storage Systems, LLC under
398aafc8c3SAlan Somers  * sponsorship from the FreeBSD Foundation.
408aafc8c3SAlan Somers  *
415fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
425fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
435fe58019SAttilio Rao  * are met:
445fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
455fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
465fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
475fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
485fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
495fe58019SAttilio Rao  *
505fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
515fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
545fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
555fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
565fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
585fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
595fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
605fe58019SAttilio Rao  * SUCH DAMAGE.
615fe58019SAttilio Rao  */
625fe58019SAttilio Rao 
635fe58019SAttilio Rao #include <sys/types.h>
64*df38ada2SBjoern A. Zeeb #include <sys/param.h>
655fe58019SAttilio Rao #include <sys/module.h>
665fe58019SAttilio Rao #include <sys/systm.h>
675fe58019SAttilio Rao #include <sys/errno.h>
685fe58019SAttilio Rao #include <sys/param.h>
695fe58019SAttilio Rao #include <sys/kernel.h>
705fe58019SAttilio Rao #include <sys/conf.h>
715fe58019SAttilio Rao #include <sys/mutex.h>
725fe58019SAttilio Rao #include <sys/proc.h>
73cf169498SAlan Somers #include <sys/queue.h>
745fe58019SAttilio Rao #include <sys/mount.h>
755fe58019SAttilio Rao #include <sys/vnode.h>
765fe58019SAttilio Rao #include <sys/stat.h>
775fe58019SAttilio Rao #include <sys/file.h>
785fe58019SAttilio Rao #include <sys/buf.h>
79cf169498SAlan Somers #include <sys/sdt.h>
805fe58019SAttilio Rao #include <sys/sysctl.h>
815fe58019SAttilio Rao 
825fe58019SAttilio Rao #include "fuse.h"
83560a55d0SAlan Somers #include "fuse_file.h"
84560a55d0SAlan Somers #include "fuse_ipc.h"
85560a55d0SAlan Somers #include "fuse_internal.h"
86560a55d0SAlan Somers #include "fuse_node.h"
875fe58019SAttilio Rao 
885fe58019SAttilio Rao static void fuse_bringdown(eventhandler_tag eh_tag);
895fe58019SAttilio Rao static int fuse_loader(struct module *m, int what, void *arg);
905fe58019SAttilio Rao 
915fe58019SAttilio Rao struct mtx fuse_mtx;
925fe58019SAttilio Rao 
935fe58019SAttilio Rao extern struct vfsops fuse_vfsops;
945fe58019SAttilio Rao extern struct cdevsw fuse_cdevsw;
95f9b0e30bSAlan Somers extern struct vop_vector fuse_fifonops;
965fe58019SAttilio Rao 
975fe58019SAttilio Rao static struct vfsconf fuse_vfsconf = {
985fe58019SAttilio Rao 	.vfc_version = VFS_VERSION,
995fe58019SAttilio Rao 	.vfc_name = "fusefs",
1005fe58019SAttilio Rao 	.vfc_vfsops = &fuse_vfsops,
1015fe58019SAttilio Rao 	.vfc_typenum = -1,
1025717aa2dSAlan Somers 	.vfc_flags = VFCF_JAIL | VFCF_SYNTHETIC
1035fe58019SAttilio Rao };
1045fe58019SAttilio Rao 
105ef06a80cSPawel Biernacki SYSCTL_NODE(_vfs, OID_AUTO, fusefs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
106ef06a80cSPawel Biernacki     "FUSE tunables");
107ef06a80cSPawel Biernacki SYSCTL_NODE(_vfs_fusefs, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
108ef06a80cSPawel Biernacki     "FUSE statistics");
109123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, kernelabi_major, CTLFLAG_RD,
110f0188618SHans Petter Selasky     SYSCTL_NULL_INT_PTR, FUSE_KERNEL_VERSION, "FUSE kernel abi major version");
111123af6ecSAlan Somers SYSCTL_INT(_vfs_fusefs, OID_AUTO, kernelabi_minor, CTLFLAG_RD,
112f0188618SHans Petter Selasky     SYSCTL_NULL_INT_PTR, FUSE_KERNEL_MINOR_VERSION, "FUSE kernel abi minor version");
113419e7ff6SAlan Somers SDT_PROVIDER_DEFINE(fusefs);
1145fe58019SAttilio Rao 
1155fe58019SAttilio Rao /******************************
1165fe58019SAttilio Rao  *
1175fe58019SAttilio Rao  * >>> Module management stuff
1185fe58019SAttilio Rao  *
1195fe58019SAttilio Rao  ******************************/
1205fe58019SAttilio Rao 
1215fe58019SAttilio Rao static void
fuse_bringdown(eventhandler_tag eh_tag)1225fe58019SAttilio Rao fuse_bringdown(eventhandler_tag eh_tag)
1235fe58019SAttilio Rao {
124560a55d0SAlan Somers 	fuse_node_destroy();
125560a55d0SAlan Somers 	fuse_internal_destroy();
126560a55d0SAlan Somers 	fuse_file_destroy();
1275fe58019SAttilio Rao 	fuse_ipc_destroy();
1285fe58019SAttilio Rao 	fuse_device_destroy();
1295fe58019SAttilio Rao 	mtx_destroy(&fuse_mtx);
1305fe58019SAttilio Rao }
1315fe58019SAttilio Rao 
1325fe58019SAttilio Rao static int
fuse_loader(struct module * m,int what,void * arg)1335fe58019SAttilio Rao fuse_loader(struct module *m, int what, void *arg)
1345fe58019SAttilio Rao {
1355fe58019SAttilio Rao 	static eventhandler_tag eh_tag = NULL;
1365fe58019SAttilio Rao 	int err = 0;
1375fe58019SAttilio Rao 
1385fe58019SAttilio Rao 	switch (what) {
1395fe58019SAttilio Rao 	case MOD_LOAD:			/* kldload */
1405fe58019SAttilio Rao 		mtx_init(&fuse_mtx, "fuse_mtx", NULL, MTX_DEF);
1415fe58019SAttilio Rao 		err = fuse_device_init();
1425fe58019SAttilio Rao 		if (err) {
1435fe58019SAttilio Rao 			mtx_destroy(&fuse_mtx);
1445fe58019SAttilio Rao 			return (err);
1455fe58019SAttilio Rao 		}
1465fe58019SAttilio Rao 		fuse_ipc_init();
147560a55d0SAlan Somers 		fuse_file_init();
148560a55d0SAlan Somers 		fuse_internal_init();
149560a55d0SAlan Somers 		fuse_node_init();
1505fe58019SAttilio Rao 
1515fe58019SAttilio Rao 		/* vfs_modevent ignores its first arg */
1525fe58019SAttilio Rao 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
1535fe58019SAttilio Rao 			fuse_bringdown(eh_tag);
1545fe58019SAttilio Rao 		break;
1555fe58019SAttilio Rao 	case MOD_UNLOAD:
1565fe58019SAttilio Rao 		if ((err = vfs_modevent(NULL, what, &fuse_vfsconf)))
1575fe58019SAttilio Rao 			return (err);
1585fe58019SAttilio Rao 		fuse_bringdown(eh_tag);
1595fe58019SAttilio Rao 		break;
1605fe58019SAttilio Rao 	default:
1615fe58019SAttilio Rao 		return (EINVAL);
1625fe58019SAttilio Rao 	}
1635fe58019SAttilio Rao 
1645fe58019SAttilio Rao 	return (err);
1655fe58019SAttilio Rao }
1665fe58019SAttilio Rao 
1675fe58019SAttilio Rao /* Registering the module */
1685fe58019SAttilio Rao 
1695fe58019SAttilio Rao static moduledata_t fuse_moddata = {
170123af6ecSAlan Somers 	"fusefs",
1715fe58019SAttilio Rao 	fuse_loader,
1725fe58019SAttilio Rao 	&fuse_vfsconf
1735fe58019SAttilio Rao };
1745fe58019SAttilio Rao 
175123af6ecSAlan Somers DECLARE_MODULE(fusefs, fuse_moddata, SI_SUB_VFS, SI_ORDER_MIDDLE);
176123af6ecSAlan Somers MODULE_VERSION(fusefs, 1);
177