VirtualBox

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

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

Linux additions: properly initialize i_blkbits

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