VirtualBox

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

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

InnoTek -> innotek: all the headers and comments.

  • 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 * If you received this file as part of a commercial VirtualBox
20 * distribution, then only the terms of your commercial VirtualBox
21 * license agreement apply instead of the previous paragraph.
22 */
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] */
49static void
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
107static int
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 rc = vboxCallCreate (&client_handle, &sf_g->map, path, &params);
117 if (VBOX_FAILURE (rc)) {
118 elog3 ("%s: %s: vboxCallCreate(%s) failed rc=%d\n",
119 caller, __func__, path->String.utf8, rc);
120 return -EPROTO;
121 }
122
123 if (params.Result != SHFL_FILE_EXISTS) {
124 if (!ok_to_fail) {
125 elog3 ("%s: %s: vboxCallCreate(%s)"
126 " file does not exist result=%d\n",
127 caller, __func__, path->String.utf8, params.Result);
128 }
129 return -ENOENT;
130 }
131
132 *result = params.Info;
133 return 0;
134}
135
136/* this is called directly as iop on 2.4, indirectly as dop
137 [sf_dentry_revalidate] on 2.4/2.6, indirectly as iop through
138 [sf_getattr] on 2.6. the job is to find out whether dentry/inode is
139 still valid. the test is failed if [dentry] does not have an inode
140 or [sf_stat] is unsuccessful, otherwise we return success and
141 update inode attributes */
142static int
143sf_inode_revalidate (struct dentry *dentry)
144{
145 int err;
146 struct sf_glob_info *sf_g;
147 struct sf_inode_info *sf_i;
148 RTFSOBJINFO info;
149
150 TRACE ();
151 if (!dentry || !dentry->d_inode) {
152 DBGC elog ("no dentry(%p) or inode(%p)\n", dentry, dentry->d_inode);
153 return -EINVAL;
154 }
155
156 sf_g = GET_GLOB_INFO (dentry->d_inode->i_sb);
157 sf_i = GET_INODE_INFO (dentry->d_inode);
158
159#if 0
160 printk ("%s called by %p:%p\n",
161 sf_i->path->String.utf8,
162 __builtin_return_address (0),
163 __builtin_return_address (1));
164#endif
165
166 BUG_ON (!sf_g);
167 BUG_ON (!sf_i);
168
169 if (!sf_i->force_restat) {
170 if (jiffies - dentry->d_time < sf_g->ttl) {
171 return 0;
172 }
173 }
174
175 err = sf_stat (__func__, sf_g, sf_i->path, &info, 1);
176 if (err) {
177 return err;
178 }
179
180 dentry->d_time = jiffies;
181 sf_init_inode (sf_g, dentry->d_inode, &info);
182 return 0;
183}
184
185/* this is called during name resolution/lookup to check if the
186 [dentry] in the cache is still valid. the job is handled by
187 [sf_inode_revalidate] */
188static int
189#if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
190sf_dentry_revalidate (struct dentry *dentry, int flags)
191#else
192 sf_dentry_revalidate (struct dentry *dentry, struct nameidata *nd)
193#endif
194{
195 TRACE ();
196 if (sf_inode_revalidate (dentry)) {
197 return 0;
198 }
199 return 1;
200}
201
202/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
203 effect) updates inode attributes for [dentry] (given that [dentry]
204 has inode at all) from these new attributes we derive [kstat] via
205 [generic_fillattr] */
206#if LINUX_VERSION_CODE >= KERNEL_VERSION (2, 6, 0)
207static int
208sf_getattr (struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
209{
210 int err;
211
212 TRACE ();
213 err = sf_inode_revalidate (dentry);
214 if (err) {
215 return err;
216 }
217
218 generic_fillattr (dentry->d_inode, kstat);
219 return 0;
220}
221#endif
222
223static int
224sf_make_path (const char *caller, struct sf_inode_info *sf_i,
225 const char *d_name, size_t d_len, SHFLSTRING **result)
226{
227 size_t path_len, shflstring_len;
228 SHFLSTRING *tmp;
229 uint16_t p_len;
230 uint8_t *p_name;
231 uint8_t *dst;
232 int is_root = 0;
233
234 TRACE ();
235 p_len = sf_i->path->u16Length;
236 p_name = sf_i->path->String.utf8;
237
238 if (p_len == 1 && *p_name == '/') {
239 path_len = d_len + 1;
240 is_root = 1;
241 }
242 else {
243 /* lengths of constituents plus terminating zero plus slash */
244 path_len = p_len + d_len + 2;
245 if (path_len > 0xffff) {
246 elog ("%s: path to big %zu\n", caller, path_len);
247 return -ENAMETOOLONG;
248 }
249 }
250
251 shflstring_len = offsetof (SHFLSTRING, String.utf8) + path_len;
252 tmp = kmalloc (shflstring_len, GFP_KERNEL);
253 if (!tmp) {
254 elog ("%s: kmalloc failed\n", 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 */
278static int
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 elog ("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 elog ("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 DBGC printk (KERN_DEBUG "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
356static int
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 elog ("utf8_mbtowc failed(%s) %x:%d\n",
382 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 elog ("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 elog2 ("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 elog2 ("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
458static void
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
474static struct 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 elog2 ("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
513static int
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 elog2 ("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 elog2 ("host could not translte entry\n");
567 /* XXX */
568 break;
569
570 default:
571 err = -EPROTO;
572 elog ("vboxCallDirInfo failed rc=%d\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
593static struct 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