VirtualBox

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

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

Restructured the Linux shared folders guest module and made it use backdoor logging

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