VirtualBox

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

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

Implement statfs call for Linux guests. Makes volume info (e.g. df) work.

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