VirtualBox

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

Last change on this file since 42809 was 41451, checked in by vboxsync, 13 years ago

Additions/linux/sharedfolders: we cannot work in RCU mode in d_revalidate

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.7 KB
Line 
1/** @file
2 *
3 * vboxsf -- VirtualBox Guest Additions for Linux:
4 * Utility functions.
5 * Mainly conversion from/to VirtualBox/Linux data structures
6 */
7
8/*
9 * Copyright (C) 2006-2010 Oracle Corporation
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 <iprt/asm.h>
22#include <linux/nfs_fs.h>
23#include <linux/vfs.h>
24
25/* #define USE_VMALLOC */
26
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
38#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
39static void sf_ftime_from_timespec(time_t *time, RTTIMESPEC *ts)
40{
41 int64_t t = RTTimeSpecGetNano(ts);
42
43 do_div(t, 1000000000);
44 *time = t;
45}
46
47static void sf_timespec_from_ftime(RTTIMESPEC *ts, time_t *time)
48{
49 int64_t t = 1000000000 * *time;
50 RTTimeSpecSetNano(ts, t);
51}
52#else /* >= 2.6.0 */
53static void sf_ftime_from_timespec(struct timespec *tv, RTTIMESPEC *ts)
54{
55 int64_t t = RTTimeSpecGetNano(ts);
56 int64_t nsec;
57
58 nsec = do_div(t, 1000000000);
59 tv->tv_sec = t;
60 tv->tv_nsec = nsec;
61}
62
63static void sf_timespec_from_ftime(RTTIMESPEC *ts, struct timespec *tv)
64{
65 int64_t t = (int64_t)tv->tv_nsec + (int64_t)tv->tv_sec * 1000000000;
66 RTTimeSpecSetNano(ts, t);
67}
68#endif /* >= 2.6.0 */
69
70/* set [inode] attributes based on [info], uid/gid based on [sf_g] */
71void sf_init_inode(struct sf_glob_info *sf_g, struct inode *inode,
72 PSHFLFSOBJINFO info)
73{
74 PSHFLFSOBJATTR attr;
75 int mode;
76
77 TRACE();
78
79 attr = &info->Attr;
80
81#define mode_set(r) attr->fMode & (RTFS_UNIX_##r) ? (S_##r) : 0;
82 mode = mode_set(ISUID);
83 mode |= mode_set(ISGID);
84
85 mode |= mode_set(IRUSR);
86 mode |= mode_set(IWUSR);
87 mode |= mode_set(IXUSR);
88
89 mode |= mode_set(IRGRP);
90 mode |= mode_set(IWGRP);
91 mode |= mode_set(IXGRP);
92
93 mode |= mode_set(IROTH);
94 mode |= mode_set(IWOTH);
95 mode |= mode_set(IXOTH);
96
97#undef mode_set
98
99#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
100 inode->i_mapping->a_ops = &sf_reg_aops;
101 inode->i_mapping->backing_dev_info = &sf_g->bdi;
102#endif
103
104 if (RTFS_IS_DIRECTORY(attr->fMode))
105 {
106 inode->i_mode = sf_g->dmode != ~0 ? (sf_g->dmode & 0777) : mode;
107 inode->i_mode &= ~sf_g->dmask;
108 inode->i_mode |= S_IFDIR;
109 inode->i_op = &sf_dir_iops;
110 inode->i_fop = &sf_dir_fops;
111 /* XXX: this probably should be set to the number of entries
112 in the directory plus two (. ..) */
113#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
114 set_nlink(inode, 1);
115#else
116 inode->i_nlink = 1;
117#endif
118 }
119#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
120 else if (RTFS_IS_SYMLINK(attr->fMode))
121 {
122 inode->i_mode = sf_g->fmode != ~0 ? (sf_g->fmode & 0777): mode;
123 inode->i_mode &= ~sf_g->fmask;
124 inode->i_mode |= S_IFLNK;
125 inode->i_op = &sf_lnk_iops;
126# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
127 set_nlink(inode, 1);
128# else
129 inode->i_nlink = 1;
130# endif
131 }
132#endif
133 else
134 {
135 inode->i_mode = sf_g->fmode != ~0 ? (sf_g->fmode & 0777): mode;
136 inode->i_mode &= ~sf_g->fmask;
137 inode->i_mode |= S_IFREG;
138 inode->i_op = &sf_reg_iops;
139 inode->i_fop = &sf_reg_fops;
140#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
141 set_nlink(inode, 1);
142#else
143 inode->i_nlink = 1;
144#endif
145 }
146
147 inode->i_uid = sf_g->uid;
148 inode->i_gid = sf_g->gid;
149 inode->i_size = info->cbObject;
150#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
151 inode->i_blksize = 4096;
152#endif
153#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
154 inode->i_blkbits = 12;
155#endif
156 /* i_blocks always in units of 512 bytes! */
157 inode->i_blocks = (info->cbAllocated + 511) / 512;
158
159 sf_ftime_from_timespec(&inode->i_atime, &info->AccessTime);
160 sf_ftime_from_timespec(&inode->i_ctime, &info->ChangeTime);
161 sf_ftime_from_timespec(&inode->i_mtime, &info->ModificationTime);
162}
163
164int sf_stat(const char *caller, struct sf_glob_info *sf_g,
165 SHFLSTRING *path, PSHFLFSOBJINFO result, int ok_to_fail)
166{
167 int rc;
168 SHFLCREATEPARMS params;
169 NOREF(caller);
170
171 TRACE();
172
173 RT_ZERO(params);
174 params.Handle = SHFL_HANDLE_NIL;
175 params.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
176 LogFunc(("sf_stat: calling vboxCallCreate, file %s, flags %#x\n",
177 path->String.utf8, params.CreateFlags));
178 rc = vboxCallCreate(&client_handle, &sf_g->map, path, &params);
179 if (rc == VERR_INVALID_NAME)
180 {
181 /* this can happen for names like 'foo*' on a Windows host */
182 return -ENOENT;
183 }
184 if (RT_FAILURE(rc))
185 {
186 LogFunc(("vboxCallCreate(%s) failed. caller=%s, rc=%Rrc\n",
187 path->String.utf8, rc, caller));
188 return -EPROTO;
189 }
190 if (params.Result != SHFL_FILE_EXISTS)
191 {
192 if (!ok_to_fail)
193 LogFunc(("vboxCallCreate(%s) file does not exist. caller=%s, result=%d\n",
194 path->String.utf8, params.Result, caller));
195 return -ENOENT;
196 }
197
198 *result = params.Info;
199 return 0;
200}
201
202/* this is called directly as iop on 2.4, indirectly as dop
203 [sf_dentry_revalidate] on 2.4/2.6, indirectly as iop through
204 [sf_getattr] on 2.6. the job is to find out whether dentry/inode is
205 still valid. the test is failed if [dentry] does not have an inode
206 or [sf_stat] is unsuccessful, otherwise we return success and
207 update inode attributes */
208int sf_inode_revalidate(struct dentry *dentry)
209{
210 int err;
211 struct sf_glob_info *sf_g;
212 struct sf_inode_info *sf_i;
213 SHFLFSOBJINFO info;
214
215 TRACE();
216 if (!dentry || !dentry->d_inode)
217 {
218 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, dentry->d_inode));
219 return -EINVAL;
220 }
221
222 sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
223 sf_i = GET_INODE_INFO(dentry->d_inode);
224
225#if 0
226 printk("%s called by %p:%p\n",
227 sf_i->path->String.utf8,
228 __builtin_return_address (0),
229 __builtin_return_address (1));
230#endif
231
232 BUG_ON(!sf_g);
233 BUG_ON(!sf_i);
234
235 if (!sf_i->force_restat)
236 {
237 if (jiffies - dentry->d_time < sf_g->ttl)
238 return 0;
239 }
240
241 err = sf_stat(__func__, sf_g, sf_i->path, &info, 1);
242 if (err)
243 return err;
244
245 dentry->d_time = jiffies;
246 sf_init_inode(sf_g, dentry->d_inode, &info);
247 return 0;
248}
249
250/* this is called during name resolution/lookup to check if the
251 [dentry] in the cache is still valid. the job is handled by
252 [sf_inode_revalidate] */
253static int
254#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
255sf_dentry_revalidate(struct dentry *dentry, int flags)
256#else
257sf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
258#endif
259{
260 TRACE();
261
262#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
263 /* see Documentation/filesystems/vfs.txt */
264 if (nd && nd->flags & LOOKUP_RCU)
265 return -ECHILD;
266#endif
267
268 if (sf_inode_revalidate(dentry))
269 return 0;
270
271 return 1;
272}
273
274/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
275 effect) updates inode attributes for [dentry] (given that [dentry]
276 has inode at all) from these new attributes we derive [kstat] via
277 [generic_fillattr] */
278#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
279int sf_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
280{
281 int err;
282
283 TRACE();
284 err = sf_inode_revalidate(dentry);
285 if (err)
286 return err;
287
288 generic_fillattr(dentry->d_inode, kstat);
289 return 0;
290}
291
292int sf_setattr(struct dentry *dentry, struct iattr *iattr)
293{
294 struct sf_glob_info *sf_g;
295 struct sf_inode_info *sf_i;
296 SHFLCREATEPARMS params;
297 SHFLFSOBJINFO info;
298 uint32_t cbBuffer;
299 int rc, err;
300
301 TRACE();
302
303 sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
304 sf_i = GET_INODE_INFO(dentry->d_inode);
305 err = 0;
306
307 RT_ZERO(params);
308 params.Handle = SHFL_HANDLE_NIL;
309 params.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
310 | SHFL_CF_ACT_FAIL_IF_NEW
311 | SHFL_CF_ACCESS_ATTR_WRITE;
312
313 /* this is at least required for Posix hosts */
314 if (iattr->ia_valid & ATTR_SIZE)
315 params.CreateFlags |= SHFL_CF_ACCESS_WRITE;
316
317 rc = vboxCallCreate(&client_handle, &sf_g->map, sf_i->path, &params);
318 if (RT_FAILURE(rc))
319 {
320 LogFunc(("vboxCallCreate(%s) failed rc=%Rrc\n",
321 sf_i->path->String.utf8, rc));
322 err = -RTErrConvertToErrno(rc);
323 goto fail2;
324 }
325 if (params.Result != SHFL_FILE_EXISTS)
326 {
327 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
328 err = -ENOENT;
329 goto fail1;
330 }
331
332 /* Setting the file size and setting the other attributes has to be
333 * handled separately, see implementation of vbsfSetFSInfo() in
334 * vbsf.cpp */
335 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME))
336 {
337#define mode_set(r) ((iattr->ia_mode & (S_##r)) ? RTFS_UNIX_##r : 0)
338
339 RT_ZERO(info);
340 if (iattr->ia_valid & ATTR_MODE)
341 {
342 info.Attr.fMode = mode_set(ISUID);
343 info.Attr.fMode |= mode_set(ISGID);
344 info.Attr.fMode |= mode_set(IRUSR);
345 info.Attr.fMode |= mode_set(IWUSR);
346 info.Attr.fMode |= mode_set(IXUSR);
347 info.Attr.fMode |= mode_set(IRGRP);
348 info.Attr.fMode |= mode_set(IWGRP);
349 info.Attr.fMode |= mode_set(IXGRP);
350 info.Attr.fMode |= mode_set(IROTH);
351 info.Attr.fMode |= mode_set(IWOTH);
352 info.Attr.fMode |= mode_set(IXOTH);
353
354 if (iattr->ia_mode & S_IFDIR)
355 info.Attr.fMode |= RTFS_TYPE_DIRECTORY;
356 else
357 info.Attr.fMode |= RTFS_TYPE_FILE;
358 }
359
360 if (iattr->ia_valid & ATTR_ATIME)
361 sf_timespec_from_ftime(&info.AccessTime, &iattr->ia_atime);
362 if (iattr->ia_valid & ATTR_MTIME)
363 sf_timespec_from_ftime(&info.ModificationTime, &iattr->ia_mtime);
364 /* ignore ctime (inode change time) as it can't be set from userland anyway */
365
366 cbBuffer = sizeof(info);
367 rc = vboxCallFSInfo(&client_handle, &sf_g->map, params.Handle,
368 SHFL_INFO_SET | SHFL_INFO_FILE, &cbBuffer,
369 (PSHFLDIRINFO)&info);
370 if (RT_FAILURE(rc))
371 {
372 LogFunc(("vboxCallFSInfo(%s, FILE) failed rc=%Rrc\n",
373 sf_i->path->String.utf8, rc));
374 err = -RTErrConvertToErrno(rc);
375 goto fail1;
376 }
377 }
378
379 if (iattr->ia_valid & ATTR_SIZE)
380 {
381 RT_ZERO(info);
382 info.cbObject = iattr->ia_size;
383 cbBuffer = sizeof(info);
384 rc = vboxCallFSInfo(&client_handle, &sf_g->map, params.Handle,
385 SHFL_INFO_SET | SHFL_INFO_SIZE, &cbBuffer,
386 (PSHFLDIRINFO)&info);
387 if (RT_FAILURE(rc))
388 {
389 LogFunc(("vboxCallFSInfo(%s, SIZE) failed rc=%Rrc\n",
390 sf_i->path->String.utf8, rc));
391 err = -RTErrConvertToErrno(rc);
392 goto fail1;
393 }
394 }
395
396 rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
397 if (RT_FAILURE(rc))
398 LogFunc(("vboxCallClose(%s) failed rc=%Rrc\n", sf_i->path->String.utf8, rc));
399
400 return sf_inode_revalidate(dentry);
401
402fail1:
403 rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
404 if (RT_FAILURE(rc))
405 LogFunc(("vboxCallClose(%s) failed rc=%Rrc\n", sf_i->path->String.utf8, rc));
406
407fail2:
408 return err;
409}
410#endif /* >= 2.6.0 */
411
412static int sf_make_path(const char *caller, struct sf_inode_info *sf_i,
413 const char *d_name, size_t d_len, SHFLSTRING **result)
414{
415 size_t path_len, shflstring_len;
416 SHFLSTRING *tmp;
417 uint16_t p_len;
418 uint8_t *p_name;
419 int fRoot = 0;
420
421 TRACE();
422 p_len = sf_i->path->u16Length;
423 p_name = sf_i->path->String.utf8;
424
425 if (p_len == 1 && *p_name == '/')
426 {
427 path_len = d_len + 1;
428 fRoot = 1;
429 }
430 else
431 {
432 /* lengths of constituents plus terminating zero plus slash */
433 path_len = p_len + d_len + 2;
434 if (path_len > 0xffff)
435 {
436 LogFunc(("path too long. caller=%s, path_len=%zu\n", caller, path_len));
437 return -ENAMETOOLONG;
438 }
439 }
440
441 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
442 tmp = kmalloc(shflstring_len, GFP_KERNEL);
443 if (!tmp)
444 {
445 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
446 return -ENOMEM;
447 }
448 tmp->u16Length = path_len - 1;
449 tmp->u16Size = path_len;
450
451 if (fRoot)
452 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
453 else
454 {
455 memcpy(&tmp->String.utf8[0], p_name, p_len);
456 tmp->String.utf8[p_len] = '/';
457 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
458 tmp->String.utf8[p_len + 1 + d_len] = '\0';
459 }
460
461 *result = tmp;
462 return 0;
463}
464
465/**
466 * [dentry] contains string encoded in coding system that corresponds
467 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
468 * [sf_make_path] which will allocate SHFLSTRING and fill it in
469 */
470int sf_path_from_dentry(const char *caller, struct sf_glob_info *sf_g,
471 struct sf_inode_info *sf_i, struct dentry *dentry,
472 SHFLSTRING **result)
473{
474 int err;
475 const char *d_name;
476 size_t d_len;
477 const char *name;
478 size_t len = 0;
479
480 TRACE();
481 d_name = dentry->d_name.name;
482 d_len = dentry->d_name.len;
483
484 if (sf_g->nls)
485 {
486 size_t in_len, i, out_bound_len;
487 const char *in;
488 char *out;
489
490 in = d_name;
491 in_len = d_len;
492
493 out_bound_len = PATH_MAX;
494 out = kmalloc(out_bound_len, GFP_KERNEL);
495 name = out;
496
497 for (i = 0; i < d_len; ++i)
498 {
499 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
500 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
501 linux_wchar_t uni;
502 int nb;
503
504 nb = sf_g->nls->char2uni(in, in_len, &uni);
505 if (nb < 0)
506 {
507 LogFunc(("nls->char2uni failed %x %d\n",
508 *in, in_len));
509 err = -EINVAL;
510 goto fail1;
511 }
512 in_len -= nb;
513 in += nb;
514
515#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
516 nb = utf32_to_utf8(uni, out, out_bound_len);
517#else
518 nb = utf8_wctomb(out, uni, out_bound_len);
519#endif
520 if (nb < 0)
521 {
522 LogFunc(("nls->uni2char failed %x %d\n",
523 uni, out_bound_len));
524 err = -EINVAL;
525 goto fail1;
526 }
527 out_bound_len -= nb;
528 out += nb;
529 len += nb;
530 }
531 if (len >= PATH_MAX - 1)
532 {
533 err = -ENAMETOOLONG;
534 goto fail1;
535 }
536
537 LogFunc(("result(%d) = %.*s\n", len, len, name));
538 *out = 0;
539 }
540 else
541 {
542 name = d_name;
543 len = d_len;
544 }
545
546 err = sf_make_path(caller, sf_i, name, len, result);
547 if (name != d_name)
548 kfree(name);
549
550 return err;
551
552fail1:
553 kfree(name);
554 return err;
555}
556
557int sf_nlscpy(struct sf_glob_info *sf_g,
558 char *name, size_t name_bound_len,
559 const unsigned char *utf8_name, size_t utf8_len)
560{
561 if (sf_g->nls)
562 {
563 const char *in;
564 char *out;
565 size_t out_len;
566 size_t out_bound_len;
567 size_t in_bound_len;
568
569 in = utf8_name;
570 in_bound_len = utf8_len;
571
572 out = name;
573 out_len = 0;
574 out_bound_len = name_bound_len;
575
576 while (in_bound_len)
577 {
578 int nb;
579#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
580 unicode_t uni;
581
582 nb = utf8_to_utf32(in, in_bound_len, &uni);
583#else
584 linux_wchar_t uni;
585
586 nb = utf8_mbtowc(&uni, in, in_bound_len);
587#endif
588 if (nb < 0)
589 {
590 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
591 (const char *) utf8_name, *in, in_bound_len));
592 return -EINVAL;
593 }
594 in += nb;
595 in_bound_len -= nb;
596
597 nb = sf_g->nls->uni2char(uni, out, out_bound_len);
598 if (nb < 0)
599 {
600 LogFunc(("nls->uni2char failed(%s) %x:%d\n",
601 utf8_name, uni, out_bound_len));
602 return nb;
603 }
604 out += nb;
605 out_bound_len -= nb;
606 out_len += nb;
607 }
608
609 *out = 0;
610 }
611 else
612 {
613 if (utf8_len + 1 > name_bound_len)
614 return -ENAMETOOLONG;
615
616 memcpy(name, utf8_name, utf8_len + 1);
617 }
618 return 0;
619}
620
621static struct sf_dir_buf *sf_dir_buf_alloc(void)
622{
623 struct sf_dir_buf *b;
624
625 TRACE();
626 b = kmalloc(sizeof(*b), GFP_KERNEL);
627 if (!b)
628 {
629 LogRelFunc(("could not alloc directory buffer\n"));
630 return NULL;
631 }
632
633#ifdef USE_VMALLOC
634 b->buf = vmalloc(DIR_BUFFER_SIZE);
635#else
636 b->buf = kmalloc(DIR_BUFFER_SIZE, GFP_KERNEL);
637#endif
638 if (!b->buf)
639 {
640 kfree(b);
641 LogRelFunc(("could not alloc directory buffer storage\n"));
642 return NULL;
643 }
644
645 INIT_LIST_HEAD(&b->head);
646 b->cEntries = 0;
647 b->cbUsed = 0;
648 b->cbFree = DIR_BUFFER_SIZE;
649 return b;
650}
651
652static void sf_dir_buf_free(struct sf_dir_buf *b)
653{
654 BUG_ON(!b || !b->buf);
655
656 TRACE();
657 list_del(&b->head);
658#ifdef USE_VMALLOC
659 vfree(b->buf);
660#else
661 kfree(b->buf);
662#endif
663 kfree(b);
664}
665
666/**
667 * Free the directory buffer.
668 */
669void sf_dir_info_free(struct sf_dir_info *p)
670{
671 struct list_head *list, *pos, *tmp;
672
673 TRACE();
674 list = &p->info_list;
675 list_for_each_safe(pos, tmp, list)
676 {
677 struct sf_dir_buf *b;
678
679 b = list_entry(pos, struct sf_dir_buf, head);
680 sf_dir_buf_free(b);
681 }
682 kfree(p);
683}
684
685/**
686 * Empty (but not free) the directory buffer.
687 */
688void sf_dir_info_empty(struct sf_dir_info *p)
689{
690 struct list_head *list, *pos, *tmp;
691 TRACE();
692 list = &p->info_list;
693 list_for_each_safe(pos, tmp, list)
694 {
695 struct sf_dir_buf *b;
696 b = list_entry(pos, struct sf_dir_buf, head);
697 b->cEntries = 0;
698 b->cbUsed = 0;
699 b->cbFree = DIR_BUFFER_SIZE;
700 }
701}
702
703/**
704 * Create a new directory buffer descriptor.
705 */
706struct sf_dir_info *sf_dir_info_alloc(void)
707{
708 struct sf_dir_info *p;
709
710 TRACE();
711 p = kmalloc(sizeof(*p), GFP_KERNEL);
712 if (!p)
713 {
714 LogRelFunc(("could not alloc directory info\n"));
715 return NULL;
716 }
717
718 INIT_LIST_HEAD(&p->info_list);
719 return p;
720}
721
722/**
723 * Search for an empty directory content buffer.
724 */
725static struct sf_dir_buf *sf_get_empty_dir_buf(struct sf_dir_info *sf_d)
726{
727 struct list_head *list, *pos;
728
729 list = &sf_d->info_list;
730 list_for_each(pos, list)
731 {
732 struct sf_dir_buf *b;
733
734 b = list_entry(pos, struct sf_dir_buf, head);
735 if (!b)
736 return NULL;
737 else
738 {
739 if (b->cbUsed == 0)
740 return b;
741 }
742 }
743
744 return NULL;
745}
746
747int sf_dir_read_all(struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
748 struct sf_dir_info *sf_d, SHFLHANDLE handle)
749{
750 int err;
751 SHFLSTRING *mask;
752 struct sf_dir_buf *b;
753
754 TRACE();
755 err = sf_make_path(__func__, sf_i, "*", 1, &mask);
756 if (err)
757 goto fail0;
758
759 for (;;)
760 {
761 int rc;
762 void *buf;
763 uint32_t cbSize;
764 uint32_t cEntries;
765
766 b = sf_get_empty_dir_buf(sf_d);
767 if (!b)
768 {
769 b = sf_dir_buf_alloc();
770 if (!b)
771 {
772 err = -ENOMEM;
773 LogRelFunc(("could not alloc directory buffer\n"));
774 goto fail1;
775 }
776 list_add(&b->head, &sf_d->info_list);
777 }
778
779 buf = b->buf;
780 cbSize = b->cbFree;
781
782 rc = vboxCallDirInfo(&client_handle, &sf_g->map, handle, mask,
783 0, 0, &cbSize, buf, &cEntries);
784 switch (rc)
785 {
786 case VINF_SUCCESS:
787 /* fallthrough */
788 case VERR_NO_MORE_FILES:
789 break;
790 case VERR_NO_TRANSLATION:
791 LogFunc(("host could not translate entry\n"));
792 /* XXX */
793 break;
794 default:
795 err = -RTErrConvertToErrno(rc);
796 LogFunc(("vboxCallDirInfo failed rc=%Rrc\n", rc));
797 goto fail1;
798 }
799
800 b->cEntries += cEntries;
801 b->cbFree -= cbSize;
802 b->cbUsed += cbSize;
803
804 if (RT_FAILURE(rc))
805 break;
806 }
807 err = 0;
808
809fail1:
810 kfree(mask);
811
812fail0:
813 return err;
814}
815
816int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS *stat)
817{
818 struct sf_glob_info *sf_g;
819 SHFLVOLINFO SHFLVolumeInfo;
820 uint32_t cbBuffer;
821 int rc;
822
823 sf_g = GET_GLOB_INFO(sb);
824 cbBuffer = sizeof(SHFLVolumeInfo);
825 rc = vboxCallFSInfo(&client_handle, &sf_g->map, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
826 &cbBuffer, (PSHFLDIRINFO)&SHFLVolumeInfo);
827 if (RT_FAILURE(rc))
828 return -RTErrConvertToErrno(rc);
829
830 stat->f_type = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
831 stat->f_bsize = SHFLVolumeInfo.ulBytesPerAllocationUnit;
832 stat->f_blocks = SHFLVolumeInfo.ullTotalAllocationBytes
833 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
834 stat->f_bfree = SHFLVolumeInfo.ullAvailableAllocationBytes
835 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
836 stat->f_bavail = SHFLVolumeInfo.ullAvailableAllocationBytes
837 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
838 stat->f_files = 1000;
839 stat->f_ffree = 1000; /* don't return 0 here since the guest may think
840 * that it is not possible to create any more files */
841 stat->f_fsid.val[0] = 0;
842 stat->f_fsid.val[1] = 0;
843 stat->f_namelen = 255;
844 return 0;
845}
846
847struct dentry_operations sf_dentry_ops =
848{
849 .d_revalidate = sf_dentry_revalidate
850};
851
852int sf_init_backing_dev(struct sf_glob_info *sf_g)
853{
854 int rc = 0;
855 /* Each new shared folder map gets a new uint64_t identifier,
856 * allocated in sequence. We ASSUME the sequence will not wrap. */
857 static uint64_t s_u64Sequence = 0;
858 uint64_t u64CurrentSequence = ASMAtomicIncU64(&s_u64Sequence);
859
860#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
861 sf_g->bdi.ra_pages = 0; /* No readahead */
862# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 12)
863 sf_g->bdi.capabilities = BDI_CAP_MAP_DIRECT /* MAP_SHARED */
864 | BDI_CAP_MAP_COPY /* MAP_PRIVATE */
865 | BDI_CAP_READ_MAP /* can be mapped for reading */
866 | BDI_CAP_WRITE_MAP /* can be mapped for writing */
867 | BDI_CAP_EXEC_MAP; /* can be mapped for execution */
868# endif /* >= 2.6.12 */
869# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
870 rc = bdi_init(&sf_g->bdi);
871# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
872 if (!rc)
873 rc = bdi_register(&sf_g->bdi, NULL, "vboxsf-%llu",
874 (unsigned long long)u64CurrentSequence);
875# endif /* >= 2.6.26 */
876# endif /* >= 2.6.24 */
877#endif /* >= 2.6.0 */
878 return rc;
879}
880
881void sf_done_backing_dev(struct sf_glob_info *sf_g)
882{
883#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
884 bdi_destroy(&sf_g->bdi); /* includes bdi_unregister() */
885#endif
886}
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