VirtualBox

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

Last change on this file since 69564 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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

© 2023 Oracle
ContactPrivacy policyTerms of Use