This file is indexed.

/usr/src/virtualbox-guest-4.1.42/vboxsf/utils.c is in virtualbox-guest-dkms 4.1.42-dfsg-1+deb7u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/** @file
 *
 * vboxsf -- VirtualBox Guest Additions for Linux:
 * Utility functions.
 * Mainly conversion from/to VirtualBox/Linux data structures
 */

/*
 * Copyright (C) 2006-2012 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#include "vfsmod.h"
#include <iprt/asm.h>
#include <linux/nfs_fs.h>
#include <linux/vfs.h>

/* #define USE_VMALLOC */

/*
 * sf_reg_aops and sf_backing_dev_info are just quick implementations to make
 * sendfile work. For more information have a look at
 *
 *   http://us1.samba.org/samba/ftp/cifs-cvs/ols2006-fs-tutorial-smf.odp
 *
 * and the sample implementation
 *
 *   http://pserver.samba.org/samba/ftp/cifs-cvs/samplefs.tar.gz
 */

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
static void sf_ftime_from_timespec(time_t *time, RTTIMESPEC *ts)
{
    int64_t t = RTTimeSpecGetNano(ts);

    do_div(t, 1000000000);
    *time = t;
}

static void sf_timespec_from_ftime(RTTIMESPEC *ts, time_t *time)
{
    int64_t t = 1000000000 * *time;
    RTTimeSpecSetNano(ts, t);
}
#else /* >= 2.6.0 */
static void sf_ftime_from_timespec(struct timespec *tv, RTTIMESPEC *ts)
{
    int64_t t = RTTimeSpecGetNano(ts);
    int64_t nsec;

    nsec = do_div(t, 1000000000);
    tv->tv_sec = t;
    tv->tv_nsec = nsec;
}

static void sf_timespec_from_ftime(RTTIMESPEC *ts, struct timespec *tv)
{
    int64_t t = (int64_t)tv->tv_nsec + (int64_t)tv->tv_sec * 1000000000;
    RTTimeSpecSetNano(ts, t);
}
#endif /* >= 2.6.0 */

/* set [inode] attributes based on [info], uid/gid based on [sf_g] */
void sf_init_inode(struct sf_glob_info *sf_g, struct inode *inode,
                   PSHFLFSOBJINFO info)
{
    PSHFLFSOBJATTR attr;
    int mode;

    TRACE();

    attr = &info->Attr;

#define mode_set(r) attr->fMode & (RTFS_UNIX_##r) ? (S_##r) : 0;
    mode  = mode_set(ISUID);
    mode |= mode_set(ISGID);

    mode |= mode_set(IRUSR);
    mode |= mode_set(IWUSR);
    mode |= mode_set(IXUSR);

    mode |= mode_set(IRGRP);
    mode |= mode_set(IWGRP);
    mode |= mode_set(IXGRP);

    mode |= mode_set(IROTH);
    mode |= mode_set(IWOTH);
    mode |= mode_set(IXOTH);

#undef mode_set

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
    inode->i_mapping->a_ops = &sf_reg_aops;
    inode->i_mapping->backing_dev_info = &sf_g->bdi;
#endif

    if (RTFS_IS_DIRECTORY(attr->fMode))
    {
        inode->i_mode  = sf_g->dmode != ~0 ? (sf_g->dmode & 0777) : mode;
        inode->i_mode &= ~sf_g->dmask;
        inode->i_mode |= S_IFDIR;
        inode->i_op    = &sf_dir_iops;
        inode->i_fop   = &sf_dir_fops;
        /* XXX: this probably should be set to the number of entries
           in the directory plus two (. ..) */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
        set_nlink(inode, 1);
#else
        inode->i_nlink = 1;
#endif
    }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
    else if (RTFS_IS_SYMLINK(attr->fMode))
    {
        inode->i_mode  = sf_g->fmode != ~0 ? (sf_g->fmode & 0777): mode;
        inode->i_mode &= ~sf_g->fmask;
        inode->i_mode |= S_IFLNK;
        inode->i_op    = &sf_lnk_iops;
# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
        set_nlink(inode, 1);
# else
        inode->i_nlink = 1;
# endif
    }
#endif
    else
    {
        inode->i_mode  = sf_g->fmode != ~0 ? (sf_g->fmode & 0777): mode;
        inode->i_mode &= ~sf_g->fmask;
        inode->i_mode |= S_IFREG;
        inode->i_op    = &sf_reg_iops;
        inode->i_fop   = &sf_reg_fops;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
        set_nlink(inode, 1);
#else
        inode->i_nlink = 1;
#endif
    }

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
    inode->i_uid = make_kuid(current_user_ns(), sf_g->uid);
    inode->i_gid = make_kgid(current_user_ns(), sf_g->gid);
#else
    inode->i_uid = sf_g->uid;
    inode->i_gid = sf_g->gid;
#endif

    inode->i_size = info->cbObject;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
    inode->i_blksize = 4096;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
    inode->i_blkbits = 12;
#endif
    /* i_blocks always in units of 512 bytes! */
    inode->i_blocks = (info->cbAllocated + 511) / 512;

    sf_ftime_from_timespec(&inode->i_atime, &info->AccessTime);
    sf_ftime_from_timespec(&inode->i_ctime, &info->ChangeTime);
    sf_ftime_from_timespec(&inode->i_mtime, &info->ModificationTime);
}

int sf_stat(const char *caller, struct sf_glob_info *sf_g,
            SHFLSTRING *path, PSHFLFSOBJINFO result, int ok_to_fail)
{
    int rc;
    SHFLCREATEPARMS params;
    NOREF(caller);

    TRACE();

    RT_ZERO(params);
    params.Handle = SHFL_HANDLE_NIL;
    params.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
    LogFunc(("sf_stat: calling vboxCallCreate, file %s, flags %#x\n",
             path->String.utf8, params.CreateFlags));
    rc = vboxCallCreate(&client_handle, &sf_g->map, path, &params);
    if (rc == VERR_INVALID_NAME)
    {
        /* this can happen for names like 'foo*' on a Windows host */
        return -ENOENT;
    }
    if (RT_FAILURE(rc))
    {
        LogFunc(("vboxCallCreate(%s) failed.  caller=%s, rc=%Rrc\n",
                    path->String.utf8, rc, caller));
        return -EPROTO;
    }
    if (params.Result != SHFL_FILE_EXISTS)
    {
        if (!ok_to_fail)
            LogFunc(("vboxCallCreate(%s) file does not exist.  caller=%s, result=%d\n",
                        path->String.utf8, params.Result, caller));
        return -ENOENT;
    }

    *result = params.Info;
    return 0;
}

/* this is called directly as iop on 2.4, indirectly as dop
   [sf_dentry_revalidate] on 2.4/2.6, indirectly as iop through
   [sf_getattr] on 2.6. the job is to find out whether dentry/inode is
   still valid. the test is failed if [dentry] does not have an inode
   or [sf_stat] is unsuccessful, otherwise we return success and
   update inode attributes */
int sf_inode_revalidate(struct dentry *dentry)
{
    int err;
    struct sf_glob_info *sf_g;
    struct sf_inode_info *sf_i;
    SHFLFSOBJINFO info;

    TRACE();
    if (!dentry || !dentry->d_inode)
    {
        LogFunc(("no dentry(%p) or inode(%p)\n", dentry, dentry->d_inode));
        return -EINVAL;
    }

    sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
    sf_i = GET_INODE_INFO(dentry->d_inode);

#if 0
    printk("%s called by %p:%p\n",
            sf_i->path->String.utf8,
            __builtin_return_address (0),
            __builtin_return_address (1));
#endif

    BUG_ON(!sf_g);
    BUG_ON(!sf_i);

    if (!sf_i->force_restat)
    {
        if (jiffies - dentry->d_time < sf_g->ttl)
            return 0;
    }

    err = sf_stat(__func__, sf_g, sf_i->path, &info, 1);
    if (err)
        return err;

    dentry->d_time = jiffies;
    sf_init_inode(sf_g, dentry->d_inode, &info);
    return 0;
}

/* this is called during name resolution/lookup to check if the
   [dentry] in the cache is still valid. the job is handled by
   [sf_inode_revalidate] */
static int
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
sf_dentry_revalidate(struct dentry *dentry, unsigned flags)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
sf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
#else
sf_dentry_revalidate(struct dentry *dentry, int flags)
#endif
{
    TRACE();

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
    if (flags & LOOKUP_RCU)
        return -ECHILD;
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
    /* see Documentation/filesystems/vfs.txt */
    if (nd && nd->flags & LOOKUP_RCU)
        return -ECHILD;
#endif

    if (sf_inode_revalidate(dentry))
        return 0;

    return 1;
}

/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
   effect) updates inode attributes for [dentry] (given that [dentry]
   has inode at all) from these new attributes we derive [kstat] via
   [generic_fillattr] */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
int sf_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
{
    int err;

    TRACE();
    err = sf_inode_revalidate(dentry);
    if (err)
        return err;

    generic_fillattr(dentry->d_inode, kstat);
    return 0;
}

int sf_setattr(struct dentry *dentry, struct iattr *iattr)
{
    struct sf_glob_info *sf_g;
    struct sf_inode_info *sf_i;
    SHFLCREATEPARMS params;
    SHFLFSOBJINFO info;
    uint32_t cbBuffer;
    int rc, err;

    TRACE();

    sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
    sf_i = GET_INODE_INFO(dentry->d_inode);
    err  = 0;

    RT_ZERO(params);
    params.Handle = SHFL_HANDLE_NIL;
    params.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
                       | SHFL_CF_ACT_FAIL_IF_NEW
                       | SHFL_CF_ACCESS_ATTR_WRITE;

    /* this is at least required for Posix hosts */
    if (iattr->ia_valid & ATTR_SIZE)
        params.CreateFlags |= SHFL_CF_ACCESS_WRITE;

    rc = vboxCallCreate(&client_handle, &sf_g->map, sf_i->path, &params);
    if (RT_FAILURE(rc))
    {
        LogFunc(("vboxCallCreate(%s) failed rc=%Rrc\n",
                 sf_i->path->String.utf8, rc));
        err = -RTErrConvertToErrno(rc);
        goto fail2;
    }
    if (params.Result != SHFL_FILE_EXISTS)
    {
        LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
        err = -ENOENT;
        goto fail1;
    }

    /* Setting the file size and setting the other attributes has to be
     * handled separately, see implementation of vbsfSetFSInfo() in
     * vbsf.cpp */
    if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME))
    {
#define mode_set(r) ((iattr->ia_mode & (S_##r)) ? RTFS_UNIX_##r : 0)

        RT_ZERO(info);
        if (iattr->ia_valid & ATTR_MODE)
        {
            info.Attr.fMode  = mode_set(ISUID);
            info.Attr.fMode |= mode_set(ISGID);
            info.Attr.fMode |= mode_set(IRUSR);
            info.Attr.fMode |= mode_set(IWUSR);
            info.Attr.fMode |= mode_set(IXUSR);
            info.Attr.fMode |= mode_set(IRGRP);
            info.Attr.fMode |= mode_set(IWGRP);
            info.Attr.fMode |= mode_set(IXGRP);
            info.Attr.fMode |= mode_set(IROTH);
            info.Attr.fMode |= mode_set(IWOTH);
            info.Attr.fMode |= mode_set(IXOTH);

            if (iattr->ia_mode & S_IFDIR)
                info.Attr.fMode |= RTFS_TYPE_DIRECTORY;
            else
                info.Attr.fMode |= RTFS_TYPE_FILE;
        }

        if (iattr->ia_valid & ATTR_ATIME)
            sf_timespec_from_ftime(&info.AccessTime, &iattr->ia_atime);
        if (iattr->ia_valid & ATTR_MTIME)
            sf_timespec_from_ftime(&info.ModificationTime, &iattr->ia_mtime);
        /* ignore ctime (inode change time) as it can't be set from userland anyway */

        cbBuffer = sizeof(info);
        rc = vboxCallFSInfo(&client_handle, &sf_g->map, params.Handle,
                SHFL_INFO_SET | SHFL_INFO_FILE, &cbBuffer,
                (PSHFLDIRINFO)&info);
        if (RT_FAILURE(rc))
        {
            LogFunc(("vboxCallFSInfo(%s, FILE) failed rc=%Rrc\n",
                        sf_i->path->String.utf8, rc));
            err = -RTErrConvertToErrno(rc);
            goto fail1;
        }
    }

    if (iattr->ia_valid & ATTR_SIZE)
    {
        RT_ZERO(info);
        info.cbObject = iattr->ia_size;
        cbBuffer = sizeof(info);
        rc = vboxCallFSInfo(&client_handle, &sf_g->map, params.Handle,
                            SHFL_INFO_SET | SHFL_INFO_SIZE, &cbBuffer,
                            (PSHFLDIRINFO)&info);
        if (RT_FAILURE(rc))
        {
            LogFunc(("vboxCallFSInfo(%s, SIZE) failed rc=%Rrc\n",
                        sf_i->path->String.utf8, rc));
            err = -RTErrConvertToErrno(rc);
            goto fail1;
        }
    }

    rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
    if (RT_FAILURE(rc))
        LogFunc(("vboxCallClose(%s) failed rc=%Rrc\n", sf_i->path->String.utf8, rc));

    return sf_inode_revalidate(dentry);

fail1:
    rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
    if (RT_FAILURE(rc))
        LogFunc(("vboxCallClose(%s) failed rc=%Rrc\n", sf_i->path->String.utf8, rc));

fail2:
    return err;
}
#endif /* >= 2.6.0 */

static int sf_make_path(const char *caller, struct sf_inode_info *sf_i,
                        const char *d_name, size_t d_len, SHFLSTRING **result)
{
    size_t path_len, shflstring_len;
    SHFLSTRING *tmp;
    uint16_t p_len;
    uint8_t *p_name;
    int fRoot = 0;

    TRACE();
    p_len = sf_i->path->u16Length;
    p_name = sf_i->path->String.utf8;

    if (p_len == 1 && *p_name == '/')
    {
        path_len = d_len + 1;
        fRoot = 1;
    }
    else
    {
        /* lengths of constituents plus terminating zero plus slash  */
        path_len = p_len + d_len + 2;
        if (path_len > 0xffff)
        {
            LogFunc(("path too long.  caller=%s, path_len=%zu\n", caller, path_len));
            return -ENAMETOOLONG;
        }
    }

    shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
    tmp = kmalloc(shflstring_len, GFP_KERNEL);
    if (!tmp)
    {
        LogRelFunc(("kmalloc failed, caller=%s\n", caller));
        return -ENOMEM;
    }
    tmp->u16Length = path_len - 1;
    tmp->u16Size = path_len;

    if (fRoot)
        memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
    else
    {
        memcpy(&tmp->String.utf8[0], p_name, p_len);
        tmp->String.utf8[p_len] = '/';
        memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
        tmp->String.utf8[p_len + 1 + d_len] = '\0';
    }

    *result = tmp;
    return 0;
}

/**
 * [dentry] contains string encoded in coding system that corresponds
 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
 * [sf_make_path] which will allocate SHFLSTRING and fill it in
 */
int sf_path_from_dentry(const char *caller, struct sf_glob_info *sf_g,
                        struct sf_inode_info *sf_i, struct dentry *dentry,
                        SHFLSTRING **result)
{
    int err;
    const char *d_name;
    size_t d_len;
    const char *name;
    size_t len = 0;

    TRACE();
    d_name = dentry->d_name.name;
    d_len = dentry->d_name.len;

    if (sf_g->nls)
    {
        size_t in_len, i, out_bound_len;
        const char *in;
        char *out;

        in = d_name;
        in_len = d_len;

        out_bound_len = PATH_MAX;
        out = kmalloc(out_bound_len, GFP_KERNEL);
        name = out;

        for (i = 0; i < d_len; ++i)
        {
            /* We renamed the linux kernel wchar_t type to linux_wchar_t in
               the-linux-kernel.h, as it conflicts with the C++ type of that name. */
            linux_wchar_t uni;
            int nb;

            nb = sf_g->nls->char2uni(in, in_len, &uni);
            if (nb < 0)
            {
                LogFunc(("nls->char2uni failed %x %d\n",
                            *in, in_len));
                err = -EINVAL;
                goto fail1;
            }
            in_len -= nb;
            in += nb;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
            nb = utf32_to_utf8(uni, out, out_bound_len);
#else
            nb = utf8_wctomb(out, uni, out_bound_len);
#endif
            if (nb < 0)
            {
                LogFunc(("nls->uni2char failed %x %d\n",
                            uni, out_bound_len));
                err = -EINVAL;
                goto fail1;
            }
            out_bound_len -= nb;
            out += nb;
            len += nb;
        }
        if (len >= PATH_MAX - 1)
        {
            err = -ENAMETOOLONG;
            goto fail1;
        }

        LogFunc(("result(%d) = %.*s\n", len, len, name));
        *out = 0;
    }
    else
    {
        name = d_name;
        len = d_len;
    }

    err = sf_make_path(caller, sf_i, name, len, result);
    if (name != d_name)
        kfree(name);

    return err;

fail1:
    kfree(name);
    return err;
}

int sf_nlscpy(struct sf_glob_info *sf_g,
              char *name, size_t name_bound_len,
              const unsigned char *utf8_name, size_t utf8_len)
{
    if (sf_g->nls)
    {
        const char *in;
        char *out;
        size_t out_len;
        size_t out_bound_len;
        size_t in_bound_len;

        in = utf8_name;
        in_bound_len = utf8_len;

        out = name;
        out_len = 0;
        out_bound_len = name_bound_len;

        while (in_bound_len)
        {
            int nb;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
            unicode_t uni;

            nb = utf8_to_utf32(in, in_bound_len, &uni);
#else
            linux_wchar_t uni;

            nb = utf8_mbtowc(&uni, in, in_bound_len);
#endif
            if (nb < 0)
            {
                LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
                            (const char *) utf8_name, *in, in_bound_len));
                return -EINVAL;
            }
            in += nb;
            in_bound_len -= nb;

            nb = sf_g->nls->uni2char(uni, out, out_bound_len);
            if (nb < 0)
            {
                LogFunc(("nls->uni2char failed(%s) %x:%d\n",
                            utf8_name, uni, out_bound_len));
                return nb;
            }
            out += nb;
            out_bound_len -= nb;
            out_len += nb;
        }

        *out = 0;
    }
    else
    {
        if (utf8_len + 1 > name_bound_len)
            return -ENAMETOOLONG;

        memcpy(name, utf8_name, utf8_len + 1);
    }
    return 0;
}

static struct sf_dir_buf *sf_dir_buf_alloc(void)
{
    struct sf_dir_buf *b;

    TRACE();
    b = kmalloc(sizeof(*b), GFP_KERNEL);
    if (!b)
    {
        LogRelFunc(("could not alloc directory buffer\n"));
        return NULL;
    }

#ifdef USE_VMALLOC
    b->buf = vmalloc(DIR_BUFFER_SIZE);
#else
    b->buf = kmalloc(DIR_BUFFER_SIZE, GFP_KERNEL);
#endif
    if (!b->buf)
    {
        kfree(b);
        LogRelFunc(("could not alloc directory buffer storage\n"));
        return NULL;
    }

    INIT_LIST_HEAD(&b->head);
    b->cEntries = 0;
    b->cbUsed   = 0;
    b->cbFree   = DIR_BUFFER_SIZE;
    return b;
}

static void sf_dir_buf_free(struct sf_dir_buf *b)
{
    BUG_ON(!b || !b->buf);

    TRACE();
    list_del(&b->head);
#ifdef USE_VMALLOC
    vfree(b->buf);
#else
    kfree(b->buf);
#endif
    kfree(b);
}

/**
 * Free the directory buffer.
 */
void sf_dir_info_free(struct sf_dir_info *p)
{
    struct list_head *list, *pos, *tmp;

    TRACE();
    list = &p->info_list;
    list_for_each_safe(pos, tmp, list)
    {
        struct sf_dir_buf *b;

        b = list_entry(pos, struct sf_dir_buf, head);
        sf_dir_buf_free(b);
    }
    kfree(p);
}

/**
 * Empty (but not free) the directory buffer.
 */
void sf_dir_info_empty(struct sf_dir_info *p)
{
    struct list_head *list, *pos, *tmp;
    TRACE();
    list = &p->info_list;
    list_for_each_safe(pos, tmp, list)
    {
        struct sf_dir_buf *b;
        b = list_entry(pos, struct sf_dir_buf, head);
        b->cEntries = 0;
        b->cbUsed   = 0;
        b->cbFree = DIR_BUFFER_SIZE;
    }
}

/**
 * Create a new directory buffer descriptor.
 */
struct sf_dir_info *sf_dir_info_alloc(void)
{
    struct sf_dir_info *p;

    TRACE();
    p = kmalloc(sizeof(*p), GFP_KERNEL);
    if (!p)
    {
        LogRelFunc(("could not alloc directory info\n"));
        return NULL;
    }

    INIT_LIST_HEAD(&p->info_list);
    return p;
}

/**
 * Search for an empty directory content buffer.
 */
static struct sf_dir_buf *sf_get_empty_dir_buf(struct sf_dir_info *sf_d)
{
    struct list_head *list, *pos;

    list = &sf_d->info_list;
    list_for_each(pos, list)
    {
        struct sf_dir_buf *b;

        b = list_entry(pos, struct sf_dir_buf, head);
        if (!b)
            return NULL;
        else
        {
            if (b->cbUsed == 0)
                return b;
        }
    }

    return NULL;
}

int sf_dir_read_all(struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
                    struct sf_dir_info *sf_d, SHFLHANDLE handle)
{
    int err;
    SHFLSTRING *mask;
    struct sf_dir_buf *b;

    TRACE();
    err = sf_make_path(__func__, sf_i, "*", 1, &mask);
    if (err)
        goto fail0;

    for (;;)
    {
        int rc;
        void *buf;
        uint32_t cbSize;
        uint32_t cEntries;

        b = sf_get_empty_dir_buf(sf_d);
        if (!b)
        {
            b = sf_dir_buf_alloc();
            if (!b)
            {
                err = -ENOMEM;
                LogRelFunc(("could not alloc directory buffer\n"));
                goto fail1;
            }
            list_add(&b->head, &sf_d->info_list);
        }

        buf = b->buf;
        cbSize = b->cbFree;

        rc = vboxCallDirInfo(&client_handle, &sf_g->map, handle, mask,
                             0, 0, &cbSize, buf, &cEntries);
        switch (rc)
        {
            case VINF_SUCCESS:
                /* fallthrough */
            case VERR_NO_MORE_FILES:
                break;
            case VERR_NO_TRANSLATION:
                LogFunc(("host could not translate entry\n"));
                /* XXX */
                break;
            default:
                err = -RTErrConvertToErrno(rc);
                LogFunc(("vboxCallDirInfo failed rc=%Rrc\n", rc));
                goto fail1;
        }

        b->cEntries += cEntries;
        b->cbFree   -= cbSize;
        b->cbUsed   += cbSize;

        if (RT_FAILURE(rc))
            break;
    }
    err = 0;

fail1:
    kfree(mask);

fail0:
    return err;
}

int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS *stat)
{
    struct sf_glob_info *sf_g;
    SHFLVOLINFO SHFLVolumeInfo;
    uint32_t cbBuffer;
    int rc;

    sf_g = GET_GLOB_INFO(sb);
    cbBuffer = sizeof(SHFLVolumeInfo);
    rc = vboxCallFSInfo(&client_handle, &sf_g->map, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
                        &cbBuffer, (PSHFLDIRINFO)&SHFLVolumeInfo);
    if (RT_FAILURE(rc))
        return -RTErrConvertToErrno(rc);

    stat->f_type        = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
    stat->f_bsize       = SHFLVolumeInfo.ulBytesPerAllocationUnit;
    stat->f_blocks      = SHFLVolumeInfo.ullTotalAllocationBytes
                        / SHFLVolumeInfo.ulBytesPerAllocationUnit;
    stat->f_bfree       = SHFLVolumeInfo.ullAvailableAllocationBytes
                        / SHFLVolumeInfo.ulBytesPerAllocationUnit;
    stat->f_bavail      = SHFLVolumeInfo.ullAvailableAllocationBytes
                        / SHFLVolumeInfo.ulBytesPerAllocationUnit;
    stat->f_files       = 1000;
    stat->f_ffree       = 1000; /* don't return 0 here since the guest may think
                                 * that it is not possible to create any more files */
    stat->f_fsid.val[0] = 0;
    stat->f_fsid.val[1] = 0;
    stat->f_namelen     = 255;
    return 0;
}

struct dentry_operations sf_dentry_ops =
{
    .d_revalidate = sf_dentry_revalidate
};

int sf_init_backing_dev(struct sf_glob_info *sf_g)
{
    int rc = 0;
    /* Each new shared folder map gets a new uint64_t identifier,
     * allocated in sequence.  We ASSUME the sequence will not wrap. */
    static uint64_t s_u64Sequence = 0;
    uint64_t u64CurrentSequence = ASMAtomicIncU64(&s_u64Sequence);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
    sf_g->bdi.ra_pages = 0; /* No readahead */
# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 12)
    sf_g->bdi.capabilities  = BDI_CAP_MAP_DIRECT    /* MAP_SHARED */
                            | BDI_CAP_MAP_COPY      /* MAP_PRIVATE */
                            | BDI_CAP_READ_MAP      /* can be mapped for reading */
                            | BDI_CAP_WRITE_MAP     /* can be mapped for writing */
                            | BDI_CAP_EXEC_MAP;     /* can be mapped for execution */
# endif /* >= 2.6.12 */
# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
    rc = bdi_init(&sf_g->bdi);
#  if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
    if (!rc)
        rc = bdi_register(&sf_g->bdi, NULL, "vboxsf-%llu",
                          (unsigned long long)u64CurrentSequence);
#  endif /* >= 2.6.26 */
# endif /* >= 2.6.24 */
#endif /* >= 2.6.0 */
    return rc;
}

void sf_done_backing_dev(struct sf_glob_info *sf_g)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
    bdi_destroy(&sf_g->bdi); /* includes bdi_unregister() */
#endif
}