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