VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/utils.c@ 8061

Last change on this file since 8061 was 6054, checked in by vboxsync, 17 years ago

vboxvfs: implemented readfile()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 KB
Line 
1/** @file
2 *
3 * vboxvfs -- VirtualBox Guest Additions for Linux:
4 * Utility functions.
5 * Mainly conversion from/to VirtualBox/Linux data structures
6 */
7
8/*
9 * Copyright (C) 2006-2007 innotek GmbH
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#include "vfsmod.h"
21#include <linux/nfs_fs.h>
22#include <linux/vfs.h>
23
24/* #define USE_VMALLOC */
25
26#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
27/*
28 * sf_reg_aops and sf_backing_dev_info are just quick implementations to make
29 * sendfile work. For more information have a look at
30 *
31 * http://us1.samba.org/samba/ftp/cifs-cvs/ols2006-fs-tutorial-smf.odp
32 *
33 * and the sample implementation
34 *
35 * http://pserver.samba.org/samba/ftp/cifs-cvs/samplefs.tar.gz
36 */
37
38static struct backing_dev_info sf_backing_dev_info = {
39 .ra_pages = 0, /* No readahead */
40# if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 12)
41 .capabilities = BDI_CAP_MAP_DIRECT /* MAP_SHARED */
42 | BDI_CAP_MAP_COPY /* MAP_PRIVATE */
43 | BDI_CAP_READ_MAP /* can be mapped for reading */
44 | BDI_CAP_WRITE_MAP /* can be mapped for writing */
45 | BDI_CAP_EXEC_MAP, /* can be mapped for execution */
46# endif
47};
48#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0) */
49
50#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
51static void
52sf_ftime_from_timespec (time_t *time, RTTIMESPEC *ts)
53{
54 int64_t t = RTTimeSpecGetNano (ts);
55
56 do_div (t, 1000000000);
57 *time = t;
58}
59#else
60static void
61sf_ftime_from_timespec (struct timespec *tv, RTTIMESPEC *ts)
62{
63 int64_t t = RTTimeSpecGetNano (ts);
64 int64_t nsec;
65
66 nsec = do_div (t, 1000000000);
67 tv->tv_sec = t;
68 tv->tv_nsec = nsec;
69}
70#endif
71
72/* set [inode] attributes based on [info], uid/gid based on [sf_g] */
73void
74sf_init_inode (struct sf_glob_info *sf_g, struct inode *inode,
75 RTFSOBJINFO *info)
76{
77 int is_dir;
78 RTFSOBJATTR *attr;
79 int mode;
80
81 TRACE ();
82
83 attr = &info->Attr;
84 is_dir = RTFS_IS_DIRECTORY (attr->fMode);
85
86#define mode_set(r) attr->fMode & (RTFS_UNIX_##r) ? (S_##r) : 0;
87 mode = mode_set (ISUID);
88 mode |= mode_set (ISGID);
89
90 mode |= mode_set (IRUSR);
91 mode |= mode_set (IWUSR);
92 mode |= mode_set (IXUSR);
93
94 mode |= mode_set (IRGRP);
95 mode |= mode_set (IWGRP);
96 mode |= mode_set (IXGRP);
97
98 mode |= mode_set (IROTH);
99 mode |= mode_set (IWOTH);
100 mode |= mode_set (IXOTH);
101#undef mode_set
102
103#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
104 inode->i_mapping->a_ops = &sf_reg_aops;
105 inode->i_mapping->backing_dev_info = &sf_backing_dev_info;
106#endif
107
108 if (is_dir) {
109 inode->i_mode = S_IFDIR | mode;
110 inode->i_op = &sf_dir_iops;
111 inode->i_fop = &sf_dir_fops;
112 /* XXX: this probably should be set to the number of entries
113 in the directory plus two (. ..) */
114 inode->i_nlink = 1;
115 }
116 else {
117 inode->i_mode = S_IFREG | mode;
118 inode->i_op = &sf_reg_iops;
119 inode->i_fop = &sf_reg_fops;
120 inode->i_nlink = 1;
121 }
122
123 inode->i_uid = sf_g->uid;
124 inode->i_gid = sf_g->gid;
125 inode->i_size = info->cbObject;
126#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 19) && !defined(KERNEL_FC6)
127 inode->i_blksize = 4096;
128#endif
129#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 4, 11)
130 inode->i_blkbits = 12;
131#endif
132 inode->i_blocks = (info->cbObject + 4095) / 4096;
133
134 sf_ftime_from_timespec (&inode->i_atime, &info->AccessTime);
135 sf_ftime_from_timespec (&inode->i_ctime, &info->ChangeTime);
136 sf_ftime_from_timespec (&inode->i_mtime, &info->ModificationTime);
137}
138
139int
140sf_stat (const char *caller, struct sf_glob_info *sf_g,
141 SHFLSTRING *path, RTFSOBJINFO *result, int ok_to_fail)
142{
143 int rc;
144 SHFLCREATEPARMS params;
145
146 TRACE ();
147 params.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
148 LogFunc(("calling vboxCallCreate, file %s, flags %#x\n",
149 path->String.utf8, params.CreateFlags));
150 rc = vboxCallCreate (&client_handle, &sf_g->map, path, &params);
151 if (VBOX_FAILURE (rc)) {
152 LogFunc(("vboxCallCreate(%s) failed. caller=%s, rc=%Vrc\n",
153 path->String.utf8, rc, caller));
154 return -EPROTO;
155 }
156
157 if (params.Result != SHFL_FILE_EXISTS) {
158 if (!ok_to_fail) {
159 LogFunc(("vboxCallCreate(%s) file does not exist. caller=%s, result=%d\n",
160 path->String.utf8, params.Result, caller));
161 }
162 return -ENOENT;
163 }
164
165 *result = params.Info;
166 return 0;
167}
168
169/* this is called directly as iop on 2.4, indirectly as dop
170 [sf_dentry_revalidate] on 2.4/2.6, indirectly as iop through
171 [sf_getattr] on 2.6. the job is to find out whether dentry/inode is
172 still valid. the test is failed if [dentry] does not have an inode
173 or [sf_stat] is unsuccessful, otherwise we return success and
174 update inode attributes */
175int
176sf_inode_revalidate (struct dentry *dentry)
177{
178 int err;
179 struct sf_glob_info *sf_g;
180 struct sf_inode_info *sf_i;
181 RTFSOBJINFO info;
182
183 TRACE ();
184 if (!dentry || !dentry->d_inode) {
185 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, dentry->d_inode));
186 return -EINVAL;
187 }
188
189 sf_g = GET_GLOB_INFO (dentry->d_inode->i_sb);
190 sf_i = GET_INODE_INFO (dentry->d_inode);
191
192#if 0
193 printk ("%s called by %p:%p\n",
194 sf_i->path->String.utf8,
195 __builtin_return_address (0),
196 __builtin_return_address (1));
197#endif
198
199 BUG_ON (!sf_g);
200 BUG_ON (!sf_i);
201
202 if (!sf_i->force_restat) {
203 if (jiffies - dentry->d_time < sf_g->ttl) {
204 return 0;
205 }
206 }
207
208 err = sf_stat (__func__, sf_g, sf_i->path, &info, 1);
209 if (err) {
210 return err;
211 }
212
213 dentry->d_time = jiffies;
214 sf_init_inode (sf_g, dentry->d_inode, &info);
215 return 0;
216}
217
218/* this is called during name resolution/lookup to check if the
219 [dentry] in the cache is still valid. the job is handled by
220 [sf_inode_revalidate] */
221static int
222#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
223sf_dentry_revalidate (struct dentry *dentry, int flags)
224#else
225 sf_dentry_revalidate (struct dentry *dentry, struct nameidata *nd)
226#endif
227{
228 TRACE ();
229 if (sf_inode_revalidate (dentry)) {
230 return 0;
231 }
232 return 1;
233}
234
235/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
236 effect) updates inode attributes for [dentry] (given that [dentry]
237 has inode at all) from these new attributes we derive [kstat] via
238 [generic_fillattr] */
239#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
240int
241sf_getattr (struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
242{
243 int err;
244
245 TRACE ();
246 err = sf_inode_revalidate (dentry);
247 if (err) {
248 return err;
249 }
250
251 generic_fillattr (dentry->d_inode, kstat);
252 return 0;
253}
254#endif
255
256static int
257sf_make_path (const char *caller, struct sf_inode_info *sf_i,
258 const char *d_name, size_t d_len, SHFLSTRING **result)
259{
260 size_t path_len, shflstring_len;
261 SHFLSTRING *tmp;
262 uint16_t p_len;
263 uint8_t *p_name;
264 uint8_t *dst;
265 int is_root = 0;
266
267 TRACE ();
268 p_len = sf_i->path->u16Length;
269 p_name = sf_i->path->String.utf8;
270
271 if (p_len == 1 && *p_name == '/') {
272 path_len = d_len + 1;
273 is_root = 1;
274 }
275 else {
276 /* lengths of constituents plus terminating zero plus slash */
277 path_len = p_len + d_len + 2;
278 if (path_len > 0xffff) {
279 LogFunc(("path too long. caller=%s, path_len=%zu\n", caller, path_len));
280 return -ENAMETOOLONG;
281 }
282 }
283
284 shflstring_len = offsetof (SHFLSTRING, String.utf8) + path_len;
285 tmp = kmalloc (shflstring_len, GFP_KERNEL);
286 if (!tmp) {
287 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
288 return -ENOMEM;
289 }
290 tmp->u16Length = path_len - 1;
291 tmp->u16Size = path_len;
292
293 if (is_root) {
294 memcpy (tmp->String.utf8, d_name, d_len + 1);
295 }
296 else {
297 dst = tmp->String.utf8;
298 memcpy (dst, p_name, p_len);
299 dst += p_len; *dst++ = '/';
300 memcpy (dst, d_name, d_len);
301 dst[d_len] = 0;
302 }
303
304 *result = tmp;
305 return 0;
306}
307
308/* [dentry] contains string encoded in coding system that corresponds
309 to [sf_g]->nls, we must convert it to UTF8 here and pass down to
310 [sf_make_path] which will allocate SHFLSTRING and fill it in */
311int
312sf_path_from_dentry (const char *caller, struct sf_glob_info *sf_g,
313 struct sf_inode_info *sf_i, struct dentry *dentry,
314 SHFLSTRING **result)
315{
316 int err;
317 const char *d_name;
318 size_t d_len;
319 const char *name;
320 size_t len = 0;
321
322 TRACE ();
323 d_name = dentry->d_name.name;
324 d_len = dentry->d_name.len;
325
326 if (sf_g->nls) {
327 size_t in_len, i, out_bound_len;
328 const char *in;
329 char *out;
330
331 in = d_name;
332 in_len = d_len;
333
334 out_bound_len = PATH_MAX;
335 out = kmalloc (out_bound_len, GFP_KERNEL);
336 name = out;
337
338 for (i = 0; i < d_len; ++i) {
339 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
340 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
341 linux_wchar_t uni;
342 int nb;
343
344 nb = sf_g->nls->char2uni (in, in_len, &uni);
345 if (nb < 0) {
346 LogFunc(("nls->char2uni failed %x %d\n",
347 *in, in_len));
348 err = -EINVAL;
349 goto fail1;
350 }
351 in_len -= nb;
352 in += nb;
353
354 nb = utf8_wctomb (out, uni, out_bound_len);
355 if (nb < 0) {
356 LogFunc(("nls->uni2char failed %x %d\n",
357 uni, out_bound_len));
358 err = -EINVAL;
359 goto fail1;
360 }
361 out_bound_len -= nb;
362 out += nb;
363 len += nb;
364 }
365 if (len >= PATH_MAX - 1) {
366 err = -ENAMETOOLONG;
367 goto fail1;
368 }
369
370 LogFunc(("result(%d) = %.*s\n", len, len, name));
371 *out = 0;
372 }
373 else {
374 name = d_name;
375 len = d_len;
376 }
377
378 err = sf_make_path (caller, sf_i, name, len, result);
379 if (name != d_name) {
380 kfree (name);
381 }
382 return err;
383
384 fail1:
385 kfree (name);
386 return err;
387}
388
389int
390sf_nlscpy (struct sf_glob_info *sf_g,
391 char *name, size_t name_bound_len,
392 const unsigned char *utf8_name, size_t utf8_len)
393{
394 if (sf_g->nls) {
395 const char *in;
396 char *out;
397 size_t out_len;
398 size_t out_bound_len;
399 size_t in_bound_len;
400
401 in = utf8_name;
402 in_bound_len = utf8_len;
403
404 out = name;
405 out_len = 0;
406 out_bound_len = name_bound_len;
407
408 while (in_bound_len) {
409 int nb;
410 wchar_t uni;
411
412 nb = utf8_mbtowc (&uni, in, in_bound_len);
413 if (nb < 0) {
414 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
415 (const char *) utf8_name, *in, in_bound_len));
416 return -EINVAL;
417 }
418 in += nb;
419 in_bound_len -= nb;
420
421 nb = sf_g->nls->uni2char (uni, out, out_bound_len);
422 if (nb < 0) {
423 LogFunc(("nls->uni2char failed(%s) %x:%d\n",
424 utf8_name, uni, out_bound_len));
425 return nb;
426 }
427 out += nb;
428 out_bound_len -= nb;
429 out_len += nb;
430 }
431
432 *out = 0;
433 return 0;
434 }
435 else {
436 if (utf8_len + 1 > name_bound_len) {
437 return -ENAMETOOLONG;
438 }
439 else {
440 memcpy (name, utf8_name, utf8_len + 1);
441 }
442 return 0;
443 }
444}
445
446static struct sf_dir_buf *
447sf_dir_buf_alloc (void)
448{
449 struct sf_dir_buf *b;
450
451 TRACE ();
452 b = kmalloc (sizeof (*b), GFP_KERNEL);
453 if (!b) {
454 LogRelFunc(("could not alloc directory buffer\n"));
455 return NULL;
456 }
457
458#ifdef USE_VMALLOC
459 b->buf = vmalloc (16384);
460#else
461 b->buf = kmalloc (16384, GFP_KERNEL);
462#endif
463 if (!b->buf) {
464 kfree (b);
465 LogRelFunc(("could not alloc directory buffer storage\n"));
466 return NULL;
467 }
468
469 INIT_LIST_HEAD (&b->head);
470 b->nb_entries = 0;
471 b->used_bytes = 0;
472 b->free_bytes = 16384;
473 return b;
474}
475
476static void
477sf_dir_buf_free (struct sf_dir_buf *b)
478{
479 BUG_ON (!b || !b->buf);
480
481 TRACE ();
482 list_del (&b->head);
483#ifdef USE_VMALLOC
484 vfree (b->buf);
485#else
486 kfree (b->buf);
487#endif
488 kfree (b);
489}
490
491void
492sf_dir_info_free (struct sf_dir_info *p)
493{
494 struct list_head *list, *pos, *tmp;
495
496 TRACE ();
497 list = &p->info_list;
498 list_for_each_safe (pos, tmp, list) {
499 struct sf_dir_buf *b;
500
501 b = list_entry (pos, struct sf_dir_buf, head);
502 sf_dir_buf_free (b);
503 }
504 kfree (p);
505}
506
507struct sf_dir_info *
508sf_dir_info_alloc (void)
509{
510 struct sf_dir_info *p;
511
512 TRACE ();
513 p = kmalloc (sizeof (*p), GFP_KERNEL);
514 if (!p) {
515 LogRelFunc(("could not alloc directory info\n"));
516 return NULL;
517 }
518
519 INIT_LIST_HEAD (&p->info_list);
520 return p;
521}
522
523static struct sf_dir_buf *
524sf_get_non_empty_dir_buf (struct sf_dir_info *sf_d)
525{
526 struct list_head *list, *pos;
527
528 list = &sf_d->info_list;
529 list_for_each (pos, list) {
530 struct sf_dir_buf *b;
531
532 b = list_entry (pos, struct sf_dir_buf, head);
533 if (!b) {
534 return NULL;
535 }
536 else {
537 if (b->free_bytes > 0) {
538 return b;
539 }
540 }
541 }
542
543 return NULL;
544}
545
546int
547sf_dir_read_all (struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
548 struct sf_dir_info *sf_d, SHFLHANDLE handle)
549{
550 int err;
551 SHFLSTRING *mask;
552 struct sf_dir_buf *b;
553
554 TRACE ();
555 err = sf_make_path (__func__, sf_i, "*", 1, &mask);
556 if (err) {
557 goto fail0;
558 }
559
560 b = sf_get_non_empty_dir_buf (sf_d);
561 for (;;) {
562 int rc;
563 void *buf;
564 uint32_t buf_size;
565 uint32_t nb_ents;
566
567 if (!b) {
568 b = sf_dir_buf_alloc ();
569 if (!b) {
570 err = -ENOMEM;
571 LogRelFunc(("could not alloc directory buffer\n"));
572 goto fail1;
573 }
574 }
575
576 list_add (&b->head, &sf_d->info_list);
577
578 buf = b->buf;
579 buf_size = b->free_bytes;
580
581 rc = vboxCallDirInfo (
582 &client_handle,
583 &sf_g->map,
584 handle,
585 mask,
586 0,
587 0,
588 &buf_size,
589 buf,
590 &nb_ents
591 );
592 switch (rc) {
593 case VINF_SUCCESS:
594 /* fallthrough */
595 case VERR_NO_MORE_FILES:
596 break;
597
598 case VERR_NO_TRANSLATION:
599 LogFunc(("host could not translate entry\n"));
600 /* XXX */
601 break;
602
603 default:
604 err = -RTErrConvertToErrno (rc);
605 LogFunc(("vboxCallDirInfo failed rc=%Vrc\n", rc));
606 goto fail1;
607 }
608
609 b->nb_entries += nb_ents;
610 b->free_bytes -= buf_size;
611 b->used_bytes += buf_size;
612 b = NULL;
613
614 if (VBOX_FAILURE (rc)) {
615 break;
616 }
617 }
618 return 0;
619
620 fail1:
621 kfree (mask);
622 fail0:
623 return err;
624}
625
626int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS *stat)
627{
628 struct sf_glob_info *sf_g;
629 SHFLVOLINFO SHFLVolumeInfo;
630 uint32_t cbBuffer;
631 int rc;
632
633 sf_g = GET_GLOB_INFO (sb);
634 cbBuffer = sizeof(SHFLVolumeInfo);
635 rc = vboxCallFSInfo(&client_handle, &sf_g->map, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
636 &cbBuffer, (PSHFLDIRINFO)&SHFLVolumeInfo);
637 if (VBOX_FAILURE(rc))
638 return -RTErrConvertToErrno(rc);
639
640 stat->f_type = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
641 stat->f_bsize = SHFLVolumeInfo.ulBytesPerAllocationUnit;
642 stat->f_blocks = SHFLVolumeInfo.ullTotalAllocationBytes
643 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
644 stat->f_bfree = SHFLVolumeInfo.ullAvailableAllocationBytes
645 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
646 stat->f_bavail = SHFLVolumeInfo.ullAvailableAllocationBytes
647 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
648 stat->f_files = 1000;
649 stat->f_ffree = 1000; /* don't return 0 here since the guest may think
650 * that it is not possible to create any more files */
651 stat->f_fsid.val[0] = 0;
652 stat->f_fsid.val[1] = 0;
653 stat->f_namelen = 255;
654 return 0;
655}
656
657struct dentry_operations sf_dentry_ops = {
658 .d_revalidate = sf_dentry_revalidate
659};
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