VirtualBox

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

Last change on this file since 13053 was 13053, checked in by vboxsync, 16 years ago

Linux guest additions: allow seek for files >= 2GB

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 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 ((unsigned)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_maxbytes = ~0ULL; /* seek */
266 sb->s_op = &sf_super_ops;
267
268#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 4, 25)
269 iroot = iget_locked (sb, 0);
270#else
271 iroot = iget (sb, 0);
272#endif
273 if (!iroot) {
274 err = -ENOMEM; /* XXX */
275 LogFunc(("could not get root inode\n"));
276 goto fail3;
277 }
278
279 sf_init_inode (sf_g, iroot, &fsinfo);
280 SET_INODE_INFO (iroot, sf_i);
281
282#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 4, 25)
283 unlock_new_inode(iroot);
284#endif
285
286 droot = d_alloc_root (iroot);
287 if (!droot) {
288 err = -ENOMEM; /* XXX */
289 LogFunc(("d_alloc_root failed\n"));
290 goto fail4;
291 }
292
293 sb->s_root = droot;
294 SET_GLOB_INFO (sb, sf_g);
295 return 0;
296
297 fail4:
298 iput (iroot);
299 fail3:
300 kfree (sf_i->path);
301 fail2:
302 kfree (sf_i);
303 fail1:
304 sf_glob_free (sf_g);
305 fail0:
306 return err;
307}
308
309#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
310static struct super_block *
311sf_read_super_24 (struct super_block *sb, void *data, int flags)
312{
313 int err;
314
315 TRACE ();
316 err = sf_read_super_aux (sb, data, flags);
317 if (err) {
318 return NULL;
319 }
320
321 return sb;
322}
323#endif
324
325/* this is called when vfs is about to destroy the [inode]. all
326 resources associated with this [inode] must be cleared here */
327static void
328sf_clear_inode (struct inode *inode)
329{
330 struct sf_inode_info *sf_i;
331
332 TRACE ();
333 sf_i = GET_INODE_INFO (inode);
334 if (!sf_i) {
335 return;
336 }
337
338 BUG_ON (!sf_i->path);
339 kfree (sf_i->path);
340 kfree (sf_i);
341 SET_INODE_INFO (inode, NULL);
342}
343
344/* this is called by vfs when it wants to populate [inode] with data.
345 the only thing that is known about inode at this point is its index
346 hence we can't do anything here, and let lookup/whatever with the
347 job to properly fill then [inode] */
348#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 25)
349static void
350sf_read_inode (struct inode *inode)
351{
352}
353#endif
354
355/* vfs is done with [sb] (umount called) call [sf_glob_free] to unmap
356 the folder and free [sf_g] */
357static void
358sf_put_super (struct super_block *sb)
359{
360 struct sf_glob_info *sf_g;
361
362 sf_g = GET_GLOB_INFO (sb);
363 BUG_ON (!sf_g);
364 sf_glob_free (sf_g);
365}
366
367#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 18)
368static int
369sf_statfs (struct super_block *sb, STRUCT_STATFS *stat)
370{
371 return sf_get_volume_info(sb, stat);
372}
373#else
374static int
375sf_statfs (struct dentry *dentry, STRUCT_STATFS *stat)
376{
377 struct super_block *sb = dentry->d_inode->i_sb;
378 return sf_get_volume_info(sb, stat);
379}
380#endif
381
382static int
383sf_remount_fs (struct super_block *sb, int *flags, char *data)
384{
385 TRACE ();
386 return -ENOSYS;
387}
388
389static struct super_operations sf_super_ops = {
390 .clear_inode = sf_clear_inode,
391#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 25)
392 .read_inode = sf_read_inode,
393#endif
394 .put_super = sf_put_super,
395 .statfs = sf_statfs,
396 .remount_fs = sf_remount_fs
397};
398
399#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
400static DECLARE_FSTYPE (vboxsf_fs_type, "vboxsf", sf_read_super_24, 0);
401#else
402static int
403sf_read_super_26 (struct super_block *sb, void *data, int flags)
404{
405 int err;
406
407 TRACE ();
408 err = sf_read_super_aux (sb, data, flags);
409 if (err) {
410 printk (KERN_DEBUG "sf_read_super_aux err=%d\n", err);
411 }
412 return err;
413}
414
415#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 18)
416static struct super_block *
417sf_get_sb (struct file_system_type *fs_type, int flags,
418 const char *dev_name, void *data)
419{
420 TRACE ();
421 return get_sb_nodev (fs_type, flags, data, sf_read_super_26);
422}
423#else
424static int
425sf_get_sb (struct file_system_type *fs_type, int flags,
426 const char *dev_name, void *data, struct vfsmount *mnt)
427{
428 TRACE ();
429 return get_sb_nodev (fs_type, flags, data, sf_read_super_26, mnt);
430}
431#endif
432
433static struct file_system_type vboxsf_fs_type = {
434 .owner = THIS_MODULE,
435 .name = "vboxsf",
436 .get_sb = sf_get_sb,
437 .kill_sb = kill_anon_super
438};
439#endif
440
441extern int CMC_API
442vboxadd_cmc_ctl_guest_filter_mask (uint32_t or_mask, uint32_t not_mask);
443
444/* Module initialization/finalization handlers */
445static int __init
446init (void)
447{
448 int rcVBox;
449 int rcRet = 0;
450 int err;
451
452 TRACE ();
453
454 if (sizeof (struct vbsf_mount_info_new) > PAGE_SIZE) {
455 printk (KERN_ERR
456 "Mount information structure is too large %lu\n"
457 "Must be less than or equal to %lu\n",
458 (unsigned long)sizeof (struct vbsf_mount_info_new),
459 PAGE_SIZE);
460 return -EINVAL;
461 }
462
463 err = register_filesystem (&vboxsf_fs_type);
464 if (err) {
465 LogFunc(("register_filesystem err=%d\n", err));
466 return err;
467 }
468
469 if (vboxadd_cmc_ctl_guest_filter_mask (VMMDEV_EVENT_HGCM, 0)) {
470 rcRet = -EINVAL;
471 goto fail0;
472 }
473
474 rcVBox = vboxInit ();
475 if (VBOX_FAILURE (rcVBox)) {
476 LogRelFunc (("vboxInit failed, rc=%d\n", rcVBox));
477 rcRet = -EPROTO;
478 goto fail0;
479 }
480
481 rcVBox = vboxConnect (&client_handle);
482 if (VBOX_FAILURE (rcVBox)) {
483 LogRelFunc (("vboxConnect failed, rc=%d\n", rcVBox));
484 rcRet = -EPROTO;
485 goto fail1;
486 }
487
488 rcVBox = vboxCallSetUtf8 (&client_handle);
489 if (VBOX_FAILURE (rcVBox)) {
490 LogRelFunc (("vboxCallSetUtf8 failed, rc=%d\n", rcVBox));
491 rcRet = -EPROTO;
492 goto fail2;
493 }
494
495 printk(KERN_DEBUG
496 "vboxvfs: Successfully loaded version " VBOX_VERSION_STRING
497 " (interface " xstr(VMMDEV_VERSION) ")\n");
498
499 return 0;
500
501 fail2:
502 vboxDisconnect (&client_handle);
503 fail1:
504 vboxUninit ();
505 fail0:
506 vboxadd_cmc_ctl_guest_filter_mask (0, VMMDEV_EVENT_HGCM);
507 unregister_filesystem (&vboxsf_fs_type);
508 return rcRet;
509}
510
511static void __exit
512fini (void)
513{
514 TRACE ();
515
516 vboxDisconnect (&client_handle);
517 vboxUninit ();
518 vboxadd_cmc_ctl_guest_filter_mask (0, VMMDEV_EVENT_HGCM);
519 unregister_filesystem (&vboxsf_fs_type);
520}
521
522module_init (init);
523module_exit (fini);
524
525/* C++ hack */
526int __gxx_personality_v0 = 0xdeadbeef;
527
528#if 0
529/* long long hacks (as far as i can see, gcc emits the refs to those
530 symbols, notwithstanding the fact that those aren't referenced
531 anywhere in the module) */
532void __divdi3 (void)
533{
534 elog ("called from %p\n", __builtin_return_address (0));
535 BUG ();
536}
537
538void __moddi3 (void)
539{
540 elog ("called from %p\n", __builtin_return_address (0));
541 BUG ();
542}
543#endif /* 0 */
544
545/*
546 * Local Variables:
547 * c-mode: linux
548 * indent-tabs-mode: nil
549 * c-basic-offset: 8
550 * End:
551 */
Note: See TracBrowser for help on using the repository browser.

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