1 | /* $Id: vboxvfs_vfsops.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Description.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "vboxvfs.h"
|
---|
29 | #include <sys/param.h>
|
---|
30 | #include <sys/systm.h>
|
---|
31 | #include <sys/proc.h>
|
---|
32 | #include <sys/bio.h>
|
---|
33 | #include <sys/buf.h>
|
---|
34 | #include <sys/kernel.h>
|
---|
35 | #include <sys/sysctl.h>
|
---|
36 | #include <sys/vnode.h>
|
---|
37 | #include <sys/mount.h>
|
---|
38 | #include <sys/stat.h>
|
---|
39 | #include <sys/malloc.h>
|
---|
40 | #include <sys/module.h>
|
---|
41 |
|
---|
42 | #include <iprt/mem.h>
|
---|
43 |
|
---|
44 | #define VFSMP2SFGLOBINFO(mp) ((struct sf_glob_info *)mp->mnt_data)
|
---|
45 |
|
---|
46 | static int vboxvfs_version = VBOXVFS_VERSION;
|
---|
47 |
|
---|
48 | SYSCTL_NODE(_vfs, OID_AUTO, vboxvfs, CTLFLAG_RW, 0, "VirtualBox shared filesystem");
|
---|
49 | SYSCTL_INT(_vfs_vboxvfs, OID_AUTO, version, CTLFLAG_RD, &vboxvfs_version, 0, "");
|
---|
50 |
|
---|
51 | /* global connection to the host service. */
|
---|
52 | static VBGLSFCLIENT g_vboxSFClient;
|
---|
53 |
|
---|
54 | static vfs_init_t vboxvfs_init;
|
---|
55 | static vfs_uninit_t vboxvfs_uninit;
|
---|
56 | static vfs_cmount_t vboxvfs_cmount;
|
---|
57 | static vfs_mount_t vboxvfs_mount;
|
---|
58 | static vfs_root_t vboxvfs_root;
|
---|
59 | static vfs_quotactl_t vboxvfs_quotactl;
|
---|
60 | static vfs_statfs_t vboxvfs_statfs;
|
---|
61 | static vfs_unmount_t vboxvfs_unmount;
|
---|
62 |
|
---|
63 | static struct vfsops vboxvfs_vfsops = {
|
---|
64 | .vfs_init = vboxvfs_init,
|
---|
65 | .vfs_cmount = vboxvfs_cmount,
|
---|
66 | .vfs_mount = vboxvfs_mount,
|
---|
67 | .vfs_quotactl = vboxvfs_quotactl,
|
---|
68 | .vfs_root = vboxvfs_root,
|
---|
69 | .vfs_statfs = vboxvfs_statfs,
|
---|
70 | .vfs_sync = vfs_stdsync,
|
---|
71 | .vfs_uninit = vboxvfs_uninit,
|
---|
72 | .vfs_unmount = vboxvfs_unmount,
|
---|
73 | };
|
---|
74 |
|
---|
75 |
|
---|
76 | VFS_SET(vboxvfs_vfsops, vboxvfs, VFCF_NETWORK);
|
---|
77 | MODULE_DEPEND(vboxvfs, vboxguest, 1, 1, 1);
|
---|
78 |
|
---|
79 | static int vboxvfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
|
---|
80 | {
|
---|
81 | struct vboxvfs_mount_info args;
|
---|
82 | int rc = 0;
|
---|
83 |
|
---|
84 | printf("%s: Enter\n", __FUNCTION__);
|
---|
85 |
|
---|
86 | rc = copyin(data, &args, sizeof(struct vboxvfs_mount_info));
|
---|
87 | if (rc)
|
---|
88 | return rc;
|
---|
89 |
|
---|
90 | ma = mount_argf(ma, "uid", "%d", args.uid);
|
---|
91 | ma = mount_argf(ma, "gid", "%d", args.gid);
|
---|
92 | ma = mount_arg(ma, "from", args.name, -1);
|
---|
93 |
|
---|
94 | rc = kernel_mount(ma, flags);
|
---|
95 |
|
---|
96 | printf("%s: Leave rc=%d\n", __FUNCTION__, rc);
|
---|
97 |
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static const char *vboxvfs_opts[] = {
|
---|
102 | "uid", "gid", "from", "fstype", "fspath", "errmsg", NULL
|
---|
103 | };
|
---|
104 |
|
---|
105 | static int vboxvfs_mount(struct mount *mp, struct thread *td)
|
---|
106 | {
|
---|
107 | int rc;
|
---|
108 | char *pszShare;
|
---|
109 | int cbShare, cbOption;
|
---|
110 | int uid = 0, gid = 0;
|
---|
111 | struct sf_glob_info *pShFlGlobalInfo;
|
---|
112 | SHFLSTRING *pShFlShareName = NULL;
|
---|
113 | int cbShFlShareName;
|
---|
114 |
|
---|
115 | printf("%s: Enter\n", __FUNCTION__);
|
---|
116 |
|
---|
117 | if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
|
---|
118 | return EOPNOTSUPP;
|
---|
119 |
|
---|
120 | if (vfs_filteropt(mp->mnt_optnew, vboxvfs_opts))
|
---|
121 | {
|
---|
122 | vfs_mount_error(mp, "%s", "Invalid option");
|
---|
123 | return EINVAL;
|
---|
124 | }
|
---|
125 |
|
---|
126 | rc = vfs_getopt(mp->mnt_optnew, "from", (void **)&pszShare, &cbShare);
|
---|
127 | if (rc || pszShare[cbShare-1] != '\0' || cbShare > 0xfffe)
|
---|
128 | return EINVAL;
|
---|
129 |
|
---|
130 | rc = vfs_getopt(mp->mnt_optnew, "gid", (void **)&gid, &cbOption);
|
---|
131 | if ((rc != ENOENT) && (rc || cbOption != sizeof(gid)))
|
---|
132 | return EINVAL;
|
---|
133 |
|
---|
134 | rc = vfs_getopt(mp->mnt_optnew, "uid", (void **)&uid, &cbOption);
|
---|
135 | if ((rc != ENOENT) && (rc || cbOption != sizeof(uid)))
|
---|
136 | return EINVAL;
|
---|
137 |
|
---|
138 | pShFlGlobalInfo = RTMemAllocZ(sizeof(struct sf_glob_info));
|
---|
139 | if (!pShFlGlobalInfo)
|
---|
140 | return ENOMEM;
|
---|
141 |
|
---|
142 | cbShFlShareName = offsetof (SHFLSTRING, String.utf8) + cbShare + 1;
|
---|
143 | pShFlShareName = RTMemAllocZ(cbShFlShareName);
|
---|
144 | if (!pShFlShareName)
|
---|
145 | return VERR_NO_MEMORY;
|
---|
146 |
|
---|
147 | pShFlShareName->u16Length = cbShare;
|
---|
148 | pShFlShareName->u16Size = cbShare + 1;
|
---|
149 | memcpy (pShFlShareName->String.utf8, pszShare, cbShare + 1);
|
---|
150 |
|
---|
151 | rc = VbglR0SfMapFolder (&g_vboxSFClient, pShFlShareName, &pShFlGlobalInfo->map);
|
---|
152 | RTMemFree(pShFlShareName);
|
---|
153 |
|
---|
154 | if (RT_FAILURE (rc))
|
---|
155 | {
|
---|
156 | RTMemFree(pShFlGlobalInfo);
|
---|
157 | printf("VbglR0SfMapFolder failed rc=%d\n", rc);
|
---|
158 | return EPROTO;
|
---|
159 | }
|
---|
160 |
|
---|
161 | pShFlGlobalInfo->uid = uid;
|
---|
162 | pShFlGlobalInfo->gid = gid;
|
---|
163 |
|
---|
164 | mp->mnt_data = pShFlGlobalInfo;
|
---|
165 |
|
---|
166 | /** @todo root vnode. */
|
---|
167 |
|
---|
168 | vfs_getnewfsid(mp);
|
---|
169 | vfs_mountedfrom(mp, pszShare);
|
---|
170 |
|
---|
171 | printf("%s: Leave rc=0\n", __FUNCTION__);
|
---|
172 |
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static int vboxvfs_unmount(struct mount *mp, int mntflags, struct thread *td)
|
---|
177 | {
|
---|
178 | struct sf_glob_info *pShFlGlobalInfo = VFSMP2SFGLOBINFO(mp);
|
---|
179 | int rc;
|
---|
180 | int flags = 0;
|
---|
181 |
|
---|
182 | rc = VbglR0SfUnmapFolder(&g_vboxSFClient, &pShFlGlobalInfo->map);
|
---|
183 | if (RT_FAILURE(rc))
|
---|
184 | printf("Failed to unmap shared folder\n");
|
---|
185 |
|
---|
186 | if (mntflags & MNT_FORCE)
|
---|
187 | flags |= FORCECLOSE;
|
---|
188 |
|
---|
189 | /* There is 1 extra root vnode reference (vnode_root). */
|
---|
190 | rc = vflush(mp, 1, flags, td);
|
---|
191 | if (rc)
|
---|
192 | return rc;
|
---|
193 |
|
---|
194 |
|
---|
195 | RTMemFree(pShFlGlobalInfo);
|
---|
196 | mp->mnt_data = NULL;
|
---|
197 |
|
---|
198 | return 0;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static int vboxvfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
|
---|
202 | {
|
---|
203 | int rc = 0;
|
---|
204 | struct sf_glob_info *pShFlGlobalInfo = VFSMP2SFGLOBINFO(mp);
|
---|
205 | struct vnode *vp;
|
---|
206 |
|
---|
207 | printf("%s: Enter\n", __FUNCTION__);
|
---|
208 |
|
---|
209 | vp = pShFlGlobalInfo->vnode_root;
|
---|
210 | VREF(vp);
|
---|
211 |
|
---|
212 | vn_lock(vp, flags | LK_RETRY, td);
|
---|
213 | *vpp = vp;
|
---|
214 |
|
---|
215 | printf("%s: Leave\n", __FUNCTION__);
|
---|
216 |
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | static int vboxvfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct thread *td)
|
---|
221 | {
|
---|
222 | return EOPNOTSUPP;
|
---|
223 | }
|
---|
224 |
|
---|
225 | int vboxvfs_init(struct vfsconf *vfsp)
|
---|
226 | {
|
---|
227 | int rc;
|
---|
228 |
|
---|
229 | /* Initialize the R0 guest library. */
|
---|
230 | rc = VbglR0SfInit();
|
---|
231 | if (RT_FAILURE(rc))
|
---|
232 | return ENXIO;
|
---|
233 |
|
---|
234 | /* Connect to the host service. */
|
---|
235 | rc = VbglR0SfConnect(&g_vboxSFClient);
|
---|
236 | if (RT_FAILURE(rc))
|
---|
237 | {
|
---|
238 | printf("Failed to get connection to host! rc=%d\n", rc);
|
---|
239 | VbglR0SfTerm();
|
---|
240 | return ENXIO;
|
---|
241 | }
|
---|
242 |
|
---|
243 | rc = VbglR0SfSetUtf8(&g_vboxSFClient);
|
---|
244 | if (RT_FAILURE (rc))
|
---|
245 | {
|
---|
246 | printf("VbglR0SfSetUtf8 failed, rc=%d\n", rc);
|
---|
247 | VbglR0SfDisconnect(&g_vboxSFClient);
|
---|
248 | VbglR0SfTerm();
|
---|
249 | return EPROTO;
|
---|
250 | }
|
---|
251 |
|
---|
252 | printf("Successfully loaded shared folder module\n");
|
---|
253 |
|
---|
254 | return 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 | int vboxvfs_uninit(struct vfsconf *vfsp)
|
---|
258 | {
|
---|
259 | VbglR0SfDisconnect(&g_vboxSFClient);
|
---|
260 | VbglR0SfTerm();
|
---|
261 |
|
---|
262 | return 0;
|
---|
263 | }
|
---|
264 |
|
---|
265 | int vboxvfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
|
---|
266 | {
|
---|
267 | return 0;
|
---|
268 | }
|
---|