VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/vfsmod.c@ 12726

Last change on this file since 12726 was 11567, checked in by vboxsync, 17 years ago

Linux additions: shared folders memset fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/** @file
2 *
3 * vboxvfs -- VirtualBox Guest Additions for Linux:
4 * Virtual File System for VirtualBox Shared Folders
5 *
6 * Module initialization/finalization
7 * File system registration/deregistration
8 * Superblock reading
9 * Few utility functions
10 */
11
12/*
13 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
14 *
15 * This file is part of VirtualBox Open Source Edition (OSE), as
16 * available from http://www.virtualbox.org. This file is free software;
17 * you can redistribute it and/or modify it under the terms of the GNU
18 * General Public License (GPL) as published by the Free Software
19 * Foundation, in version 2 as it comes in the "COPYING" file of the
20 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22 *
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
24 * Clara, CA 95054 USA or visit http://www.sun.com if you need
25 * additional information or have any questions.
26 */
27
28/**
29 * @note Anyone wishing to make changes here might wish to take a look at
30 * http://www.atnf.csiro.au/people/rgooch/linux/vfs.txt
31 * which seems to be the closest there is to official documentation on
32 * writing filesystem drivers for Linux.
33 */
34
35/*
36 * Suppress the definition of wchar_t from stddef.h that occurs below.
37 * This makes (at least) RHEL3U5 happy.
38 */
39#if 0
40#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
41# define _WCHAR_T
42#endif
43#endif
44
45#include "vfsmod.h"
46
47// #define wchar_t linux_wchar_t
48
49#define xstr(s) str(s)
50#define str(s) #s
51
52MODULE_DESCRIPTION ("Host file system access VFS for VirtualBox");
53MODULE_AUTHOR ("Sun Microsystems, Inc.");
54MODULE_LICENSE ("GPL");
55#ifdef MODULE_VERSION
56MODULE_VERSION(VBOX_VERSION_STRING " (interface " xstr(VMMDEV_VERSION) ")");
57#endif
58
59/* globals */
60VBSFCLIENT client_handle;
61
62/* forward declarations */
63static struct super_operations sf_super_ops;
64
65// #include "utils.c"
66// #include "dirops.c"
67// #include "regops.c"
68
69/* allocate global info, try to map host share */
70static int
71sf_glob_alloc (struct vbsf_mount_info_new *info, struct sf_glob_info **sf_gp)
72{
73 int err, rc;
74 SHFLSTRING *str_name;
75 size_t name_len, str_len;
76 struct sf_glob_info *sf_g;
77
78 TRACE ();
79 sf_g = kmalloc (sizeof (*sf_g), GFP_KERNEL);
80 if (!sf_g) {
81 err = -ENOMEM;
82 LogRelFunc(("could not allocate memory for global info\n"));
83 goto fail0;
84 }
85
86 memset(sf_g, 0, sizeof(*sf_g));
87
88 if ( info->nullchar != '\0'
89 || info->signature[0] != VBSF_MOUNT_SIGNATURE_BYTE_0
90 || info->signature[1] != VBSF_MOUNT_SIGNATURE_BYTE_1
91 || info->signature[2] != VBSF_MOUNT_SIGNATURE_BYTE_2)
92 {
93 /* An old version of mount.vboxsf made the syscall. Translate the
94 * old parameters to the new structure. */
95 struct vbsf_mount_info_old *info_old = (struct vbsf_mount_info_old *)info;
96 static struct vbsf_mount_info_new info_compat;
97
98 info = &info_compat;
99 memset(info, 0, sizeof(*info));
100 memcpy(&info->name, &info_old->name, MAX_HOST_NAME);
101 memcpy(&info->nls_name, &info_old->nls_name, MAX_NLS_NAME);
102 info->length = offsetof(struct vbsf_mount_info_new, dmode);
103 info->uid = info_old->uid;
104 info->gid = info_old->gid;
105 info->ttl = info_old->ttl;
106 }
107
108 info->name[sizeof (info->name) - 1] = 0;
109 info->nls_name[sizeof (info->nls_name) - 1] = 0;
110
111 name_len = strlen (info->name);
112 if (name_len > 0xfffe) {
113 err = -ENAMETOOLONG;
114 LogFunc(("map name too big\n"));
115 goto fail1;
116 }
117
118 str_len = offsetof (SHFLSTRING, String.utf8) + name_len + 1;
119 str_name = kmalloc (str_len, GFP_KERNEL);
120 if (!str_name) {
121 err = -ENOMEM;
122 LogRelFunc(("could not allocate memory for host name\n"));
123 goto fail1;
124 }
125
126 str_name->u16Length = name_len;
127 str_name->u16Size = name_len + 1;
128 memcpy (str_name->String.utf8, info->name, name_len + 1);
129
130 if (info->nls_name[0] && strcmp (info->nls_name, "utf8")) {
131 sf_g->nls = load_nls (info->nls_name);
132 if (!sf_g->nls) {
133 err = -EINVAL;
134 LogFunc(("failed to load nls %s\n", info->nls_name));
135 goto fail1;
136 }
137 }
138 else {
139 sf_g->nls = NULL;
140 }
141
142 rc = vboxCallMapFolder (&client_handle, str_name, &sf_g->map);
143 kfree (str_name);
144
145 if (VBOX_FAILURE (rc)) {
146 err = -EPROTO;
147 LogFunc(("vboxCallMapFolder failed rc=%d\n", rc));
148 goto fail2;
149 }
150
151 sf_g->ttl = info->ttl;
152 sf_g->uid = info->uid;
153 sf_g->gid = info->gid;
154
155 if (info->length >= sizeof(struct vbsf_mount_info_new))
156 {
157 /* new fields */
158 sf_g->dmode = info->dmode;
159 sf_g->fmode = info->fmode;
160 sf_g->dmask = info->dmask;
161 sf_g->fmask = info->fmask;
162 }
163 else
164 {
165 sf_g->dmode = ~0;
166 sf_g->fmode = ~0;
167 }
168
169 *sf_gp = sf_g;
170 return 0;
171
172 fail2:
173 if (sf_g->nls) {
174 unload_nls (sf_g->nls);
175 }
176 fail1:
177 kfree (sf_g);
178 fail0:
179 return err;
180}
181
182/* unmap the share and free global info [sf_g] */
183static void
184sf_glob_free (struct sf_glob_info *sf_g)
185{
186 int rc;
187
188 TRACE ();
189 rc = vboxCallUnmapFolder (&client_handle, &sf_g->map);
190 if (VBOX_FAILURE (rc)) {
191 LogFunc(("vboxCallUnmapFolder failed rc=%d\n", rc));
192 }
193
194 if (sf_g->nls) {
195 unload_nls (sf_g->nls);
196 }
197 kfree (sf_g);
198}
199
200/* this is called (by sf_read_super_[24|26] when vfs mounts the fs and
201 wants to read super_block.
202
203 calls [sf_glob_alloc] to map the folder and allocate global
204 information structure.
205
206 initializes [sb], initializes root inode and dentry.
207
208 should respect [flags] */
209static int
210sf_read_super_aux (struct super_block *sb, void *data, int flags)
211{
212 int err;
213 struct dentry *droot;
214 struct inode *iroot;
215 struct sf_inode_info *sf_i;
216 struct sf_glob_info *sf_g;
217 RTFSOBJINFO fsinfo;
218 struct vbsf_mount_info_new *info;
219
220 TRACE ();
221 if (!data) {
222 LogFunc(("no mount info specified\n"));
223 return -EINVAL;
224 }
225
226 info = data;
227
228 if (flags & MS_REMOUNT) {
229 LogFunc(("remounting is not supported\n"));
230 return -ENOSYS;
231 }
232
233 err = sf_glob_alloc (info, &sf_g);
234 if (err) {
235 goto fail0;
236 }
237
238 sf_i = kmalloc (sizeof (*sf_i), GFP_KERNEL);
239 if (!sf_i) {
240 err = -ENOMEM;
241 LogRelFunc (("could not allocate memory for root inode info\n"));
242 goto fail1;
243 }
244
245 sf_i->path = kmalloc (sizeof (SHFLSTRING) + 1, GFP_KERNEL);
246 if (!sf_i->path) {
247 err = -ENOMEM;
248 LogRelFunc (("could not allocate memory for root inode path\n"));
249 goto fail2;
250 }
251
252 sf_i->path->u16Length = 1;
253 sf_i->path->u16Size = 2;
254 sf_i->path->String.utf8[0] = '/';
255 sf_i->path->String.utf8[1] = 0;
256
257 err = sf_stat (__func__, sf_g, sf_i->path, &fsinfo, 0);
258 if (err) {
259 LogFunc(("could not stat root of share\n"));
260 goto fail3;
261 }
262
263 sb->s_magic = 0xface;
264 sb->s_blocksize = 1024;
265 sb->s_op = &sf_super_ops;
266
267#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 4, 25)
268 iroot = iget_locked (sb, 0);
269#else
270 iroot = iget (sb, 0);
271#endif
272 if (!iroot) {
273 err = -ENOMEM; /* XXX */
274 LogFunc(("could not get root inode\n"));
275 goto fail3;
276 }
277
278 sf_init_inode (sf_g, iroot, &fsinfo);
279 SET_INODE_INFO (iroot, sf_i);
280
281#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 4, 25)
282 unlock_new_inode(iroot);
283#endif
284
285 droot = d_alloc_root (iroot);
286 if (!droot) {
287 err = -ENOMEM; /* XXX */
288 LogFunc(("d_alloc_root failed\n"));
289 goto fail4;
290 }
291
292 sb->s_root = droot;
293 SET_GLOB_INFO (sb, sf_g);
294 return 0;
295
296 fail4:
297 iput (iroot);
298 fail3:
299 kfree (sf_i->path);
300 fail2:
301 kfree (sf_i);
302 fail1:
303 sf_glob_free (sf_g);
304 fail0:
305 return err;
306}
307
308#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
309static struct super_block *
310sf_read_super_24 (struct super_block *sb, void *data, int flags)
311{
312 int err;
313
314 TRACE ();
315 err = sf_read_super_aux (sb, data, flags);
316 if (err) {
317 return NULL;
318 }
319
320 return sb;
321}
322#endif
323
324/* this is called when vfs is about to destroy the [inode]. all
325 resources associated with this [inode] must be cleared here */
326static void
327sf_clear_inode (struct inode *inode)
328{
329 struct sf_inode_info *sf_i;
330
331 TRACE ();
332 sf_i = GET_INODE_INFO (inode);
333 if (!sf_i) {
334 return;
335 }
336
337 BUG_ON (!sf_i->path);
338 kfree (sf_i->path);
339 kfree (sf_i);
340 SET_INODE_INFO (inode, NULL);
341}
342
343/* this is called by vfs when it wants to populate [inode] with data.
344 the only thing that is known about inode at this point is its index
345 hence we can't do anything here, and let lookup/whatever with the
346 job to properly fill then [inode] */
347#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 25)
348static void
349sf_read_inode (struct inode *inode)
350{
351}
352#endif
353
354/* vfs is done with [sb] (umount called) call [sf_glob_free] to unmap
355 the folder and free [sf_g] */
356static void
357sf_put_super (struct super_block *sb)
358{
359 struct sf_glob_info *sf_g;
360
361 sf_g = GET_GLOB_INFO (sb);
362 BUG_ON (!sf_g);
363 sf_glob_free (sf_g);
364}
365
366#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 18)
367static int
368sf_statfs (struct super_block *sb, STRUCT_STATFS *stat)
369{
370 return sf_get_volume_info(sb, stat);
371}
372#else
373static int
374sf_statfs (struct dentry *dentry, STRUCT_STATFS *stat)
375{
376 struct super_block *sb = dentry->d_inode->i_sb;
377 return sf_get_volume_info(sb, stat);
378}
379#endif
380
381static int
382sf_remount_fs (struct super_block *sb, int *flags, char *data)
383{
384 TRACE ();
385 return -ENOSYS;
386}
387
388static struct super_operations sf_super_ops = {
389 .clear_inode = sf_clear_inode,
390#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 25)
391 .read_inode = sf_read_inode,
392#endif
393 .put_super = sf_put_super,
394 .statfs = sf_statfs,
395 .remount_fs = sf_remount_fs
396};
397
398#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
399static DECLARE_FSTYPE (vboxsf_fs_type, "vboxsf", sf_read_super_24, 0);
400#else
401static int
402sf_read_super_26 (struct super_block *sb, void *data, int flags)
403{
404 int err;
405
406 TRACE ();
407 err = sf_read_super_aux (sb, data, flags);
408 if (err) {
409 printk (KERN_DEBUG "sf_read_super_aux err=%d\n", err);
410 }
411 return err;
412}
413
414#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 18)
415static struct super_block *
416sf_get_sb (struct file_system_type *fs_type, int flags,
417 const char *dev_name, void *data)
418{
419 TRACE ();
420 return get_sb_nodev (fs_type, flags, data, sf_read_super_26);
421}
422#else
423static int
424sf_get_sb (struct file_system_type *fs_type, int flags,
425 const char *dev_name, void *data, struct vfsmount *mnt)
426{
427 TRACE ();
428 return get_sb_nodev (fs_type, flags, data, sf_read_super_26, mnt);
429}
430#endif
431
432static struct file_system_type vboxsf_fs_type = {
433 .owner = THIS_MODULE,
434 .name = "vboxsf",
435 .get_sb = sf_get_sb,
436 .kill_sb = kill_anon_super
437};
438#endif
439
440extern int CMC_API
441vboxadd_cmc_ctl_guest_filter_mask (uint32_t or_mask, uint32_t not_mask);
442
443/* Module initialization/finalization handlers */
444static int __init
445init (void)
446{
447 int rcVBox;
448 int rcRet = 0;
449 int err;
450
451 TRACE ();
452
453 if (sizeof (struct vbsf_mount_info_new) > PAGE_SIZE) {
454 printk (KERN_ERR
455 "Mount information structure is too large %lu\n"
456 "Must be less than or equal to %lu\n",
457 (unsigned long)sizeof (struct vbsf_mount_info_new),
458 PAGE_SIZE);
459 return -EINVAL;
460 }
461
462 err = register_filesystem (&vboxsf_fs_type);
463 if (err) {
464 LogFunc(("register_filesystem err=%d\n", err));
465 return err;
466 }
467
468 if (vboxadd_cmc_ctl_guest_filter_mask (VMMDEV_EVENT_HGCM, 0)) {
469 rcRet = -EINVAL;
470 goto fail0;
471 }
472
473 rcVBox = vboxInit ();
474 if (VBOX_FAILURE (rcVBox)) {
475 LogRelFunc (("vboxInit failed, rc=%d\n", rcVBox));
476 rcRet = -EPROTO;
477 goto fail0;
478 }
479
480 rcVBox = vboxConnect (&client_handle);
481 if (VBOX_FAILURE (rcVBox)) {
482 LogRelFunc (("vboxConnect failed, rc=%d\n", rcVBox));
483 rcRet = -EPROTO;
484 goto fail1;
485 }
486
487 rcVBox = vboxCallSetUtf8 (&client_handle);
488 if (VBOX_FAILURE (rcVBox)) {
489 LogRelFunc (("vboxCallSetUtf8 failed, rc=%d\n", rcVBox));
490 rcRet = -EPROTO;
491 goto fail2;
492 }
493
494 printk(KERN_DEBUG
495 "vboxvfs: Successfully loaded version " VBOX_VERSION_STRING
496 " (interface " xstr(VMMDEV_VERSION) ")\n");
497
498 return 0;
499
500 fail2:
501 vboxDisconnect (&client_handle);
502 fail1:
503 vboxUninit ();
504 fail0:
505 vboxadd_cmc_ctl_guest_filter_mask (0, VMMDEV_EVENT_HGCM);
506 unregister_filesystem (&vboxsf_fs_type);
507 return rcRet;
508}
509
510static void __exit
511fini (void)
512{
513 TRACE ();
514
515 vboxDisconnect (&client_handle);
516 vboxUninit ();
517 vboxadd_cmc_ctl_guest_filter_mask (0, VMMDEV_EVENT_HGCM);
518 unregister_filesystem (&vboxsf_fs_type);
519}
520
521module_init (init);
522module_exit (fini);
523
524/* C++ hack */
525int __gxx_personality_v0 = 0xdeadbeef;
526
527#if 0
528/* long long hacks (as far as i can see, gcc emits the refs to those
529 symbols, notwithstanding the fact that those aren't referenced
530 anywhere in the module) */
531void __divdi3 (void)
532{
533 elog ("called from %p\n", __builtin_return_address (0));
534 BUG ();
535}
536
537void __moddi3 (void)
538{
539 elog ("called from %p\n", __builtin_return_address (0));
540 BUG ();
541}
542#endif /* 0 */
543
544/*
545 * Local Variables:
546 * c-mode: linux
547 * indent-tabs-mode: nil
548 * c-basic-offset: 8
549 * End:
550 */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette