VirtualBox

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

Last change on this file since 39548 was 39224, checked in by vboxsync, 13 years ago

Linux 3.2-rc1 compile fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.5 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 if (sf_inode_revalidate(dentry))
262 return 0;
263
264 return 1;
265}
266
267/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
268 effect) updates inode attributes for [dentry] (given that [dentry]
269 has inode at all) from these new attributes we derive [kstat] via
270 [generic_fillattr] */
271#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
272int sf_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
273{
274 int err;
275
276 TRACE();
277 err = sf_inode_revalidate(dentry);
278 if (err)
279 return err;
280
281 generic_fillattr(dentry->d_inode, kstat);
282 return 0;
283}
284
285int sf_setattr(struct dentry *dentry, struct iattr *iattr)
286{
287 struct sf_glob_info *sf_g;
288 struct sf_inode_info *sf_i;
289 SHFLCREATEPARMS params;
290 SHFLFSOBJINFO info;
291 uint32_t cbBuffer;
292 int rc, err;
293
294 TRACE();
295
296 sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
297 sf_i = GET_INODE_INFO(dentry->d_inode);
298 err = 0;
299
300 RT_ZERO(params);
301 params.Handle = SHFL_HANDLE_NIL;
302 params.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
303 | SHFL_CF_ACT_FAIL_IF_NEW
304 | SHFL_CF_ACCESS_ATTR_WRITE;
305
306 /* this is at least required for Posix hosts */
307 if (iattr->ia_valid & ATTR_SIZE)
308 params.CreateFlags |= SHFL_CF_ACCESS_WRITE;
309
310 rc = vboxCallCreate(&client_handle, &sf_g->map, sf_i->path, &params);
311 if (RT_FAILURE(rc))
312 {
313 LogFunc(("vboxCallCreate(%s) failed rc=%Rrc\n",
314 sf_i->path->String.utf8, rc));
315 err = -RTErrConvertToErrno(rc);
316 goto fail2;
317 }
318 if (params.Result != SHFL_FILE_EXISTS)
319 {
320 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
321 err = -ENOENT;
322 goto fail1;
323 }
324
325 /* Setting the file size and setting the other attributes has to be
326 * handled separately, see implementation of vbsfSetFSInfo() in
327 * vbsf.cpp */
328 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME))
329 {
330#define mode_set(r) ((iattr->ia_mode & (S_##r)) ? RTFS_UNIX_##r : 0)
331
332 RT_ZERO(info);
333 if (iattr->ia_valid & ATTR_MODE)
334 {
335 info.Attr.fMode = mode_set(ISUID);
336 info.Attr.fMode |= mode_set(ISGID);
337 info.Attr.fMode |= mode_set(IRUSR);
338 info.Attr.fMode |= mode_set(IWUSR);
339 info.Attr.fMode |= mode_set(IXUSR);
340 info.Attr.fMode |= mode_set(IRGRP);
341 info.Attr.fMode |= mode_set(IWGRP);
342 info.Attr.fMode |= mode_set(IXGRP);
343 info.Attr.fMode |= mode_set(IROTH);
344 info.Attr.fMode |= mode_set(IWOTH);
345 info.Attr.fMode |= mode_set(IXOTH);
346
347 if (iattr->ia_mode & S_IFDIR)
348 info.Attr.fMode |= RTFS_TYPE_DIRECTORY;
349 else
350 info.Attr.fMode |= RTFS_TYPE_FILE;
351 }
352
353 if (iattr->ia_valid & ATTR_ATIME)
354 sf_timespec_from_ftime(&info.AccessTime, &iattr->ia_atime);
355 if (iattr->ia_valid & ATTR_MTIME)
356 sf_timespec_from_ftime(&info.ModificationTime, &iattr->ia_mtime);
357 /* ignore ctime (inode change time) as it can't be set from userland anyway */
358
359 cbBuffer = sizeof(info);
360 rc = vboxCallFSInfo(&client_handle, &sf_g->map, params.Handle,
361 SHFL_INFO_SET | SHFL_INFO_FILE, &cbBuffer,
362 (PSHFLDIRINFO)&info);
363 if (RT_FAILURE(rc))
364 {
365 LogFunc(("vboxCallFSInfo(%s, FILE) failed rc=%Rrc\n",
366 sf_i->path->String.utf8, rc));
367 err = -RTErrConvertToErrno(rc);
368 goto fail1;
369 }
370 }
371
372 if (iattr->ia_valid & ATTR_SIZE)
373 {
374 RT_ZERO(info);
375 info.cbObject = iattr->ia_size;
376 cbBuffer = sizeof(info);
377 rc = vboxCallFSInfo(&client_handle, &sf_g->map, params.Handle,
378 SHFL_INFO_SET | SHFL_INFO_SIZE, &cbBuffer,
379 (PSHFLDIRINFO)&info);
380 if (RT_FAILURE(rc))
381 {
382 LogFunc(("vboxCallFSInfo(%s, SIZE) failed rc=%Rrc\n",
383 sf_i->path->String.utf8, rc));
384 err = -RTErrConvertToErrno(rc);
385 goto fail1;
386 }
387 }
388
389 rc = vboxCallClose(&client_handle, &sf_g->map, params.Handle);
390 if (RT_FAILURE(rc))
391 LogFunc(("vboxCallClose(%s) failed rc=%Rrc\n", sf_i->path->String.utf8, rc));
392
393 return sf_inode_revalidate(dentry);
394
395fail1:
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
400fail2:
401 return err;
402}
403#endif /* >= 2.6.0 */
404
405static int sf_make_path(const char *caller, struct sf_inode_info *sf_i,
406 const char *d_name, size_t d_len, SHFLSTRING **result)
407{
408 size_t path_len, shflstring_len;
409 SHFLSTRING *tmp;
410 uint16_t p_len;
411 uint8_t *p_name;
412 int fRoot = 0;
413
414 TRACE();
415 p_len = sf_i->path->u16Length;
416 p_name = sf_i->path->String.utf8;
417
418 if (p_len == 1 && *p_name == '/')
419 {
420 path_len = d_len + 1;
421 fRoot = 1;
422 }
423 else
424 {
425 /* lengths of constituents plus terminating zero plus slash */
426 path_len = p_len + d_len + 2;
427 if (path_len > 0xffff)
428 {
429 LogFunc(("path too long. caller=%s, path_len=%zu\n", caller, path_len));
430 return -ENAMETOOLONG;
431 }
432 }
433
434 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
435 tmp = kmalloc(shflstring_len, GFP_KERNEL);
436 if (!tmp)
437 {
438 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
439 return -ENOMEM;
440 }
441 tmp->u16Length = path_len - 1;
442 tmp->u16Size = path_len;
443
444 if (fRoot)
445 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
446 else
447 {
448 memcpy(&tmp->String.utf8[0], p_name, p_len);
449 tmp->String.utf8[p_len] = '/';
450 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
451 tmp->String.utf8[p_len + 1 + d_len] = '\0';
452 }
453
454 *result = tmp;
455 return 0;
456}
457
458/**
459 * [dentry] contains string encoded in coding system that corresponds
460 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
461 * [sf_make_path] which will allocate SHFLSTRING and fill it in
462 */
463int sf_path_from_dentry(const char *caller, struct sf_glob_info *sf_g,
464 struct sf_inode_info *sf_i, struct dentry *dentry,
465 SHFLSTRING **result)
466{
467 int err;
468 const char *d_name;
469 size_t d_len;
470 const char *name;
471 size_t len = 0;
472
473 TRACE();
474 d_name = dentry->d_name.name;
475 d_len = dentry->d_name.len;
476
477 if (sf_g->nls)
478 {
479 size_t in_len, i, out_bound_len;
480 const char *in;
481 char *out;
482
483 in = d_name;
484 in_len = d_len;
485
486 out_bound_len = PATH_MAX;
487 out = kmalloc(out_bound_len, GFP_KERNEL);
488 name = out;
489
490 for (i = 0; i < d_len; ++i)
491 {
492 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
493 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
494 linux_wchar_t uni;
495 int nb;
496
497 nb = sf_g->nls->char2uni(in, in_len, &uni);
498 if (nb < 0)
499 {
500 LogFunc(("nls->char2uni failed %x %d\n",
501 *in, in_len));
502 err = -EINVAL;
503 goto fail1;
504 }
505 in_len -= nb;
506 in += nb;
507
508#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
509 nb = utf32_to_utf8(uni, out, out_bound_len);
510#else
511 nb = utf8_wctomb(out, uni, out_bound_len);
512#endif
513 if (nb < 0)
514 {
515 LogFunc(("nls->uni2char failed %x %d\n",
516 uni, out_bound_len));
517 err = -EINVAL;
518 goto fail1;
519 }
520 out_bound_len -= nb;
521 out += nb;
522 len += nb;
523 }
524 if (len >= PATH_MAX - 1)
525 {
526 err = -ENAMETOOLONG;
527 goto fail1;
528 }
529
530 LogFunc(("result(%d) = %.*s\n", len, len, name));
531 *out = 0;
532 }
533 else
534 {
535 name = d_name;
536 len = d_len;
537 }
538
539 err = sf_make_path(caller, sf_i, name, len, result);
540 if (name != d_name)
541 kfree(name);
542
543 return err;
544
545fail1:
546 kfree(name);
547 return err;
548}
549
550int sf_nlscpy(struct sf_glob_info *sf_g,
551 char *name, size_t name_bound_len,
552 const unsigned char *utf8_name, size_t utf8_len)
553{
554 if (sf_g->nls)
555 {
556 const char *in;
557 char *out;
558 size_t out_len;
559 size_t out_bound_len;
560 size_t in_bound_len;
561
562 in = utf8_name;
563 in_bound_len = utf8_len;
564
565 out = name;
566 out_len = 0;
567 out_bound_len = name_bound_len;
568
569 while (in_bound_len)
570 {
571 int nb;
572#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
573 unicode_t uni;
574
575 nb = utf8_to_utf32(in, in_bound_len, &uni);
576#else
577 linux_wchar_t uni;
578
579 nb = utf8_mbtowc(&uni, in, in_bound_len);
580#endif
581 if (nb < 0)
582 {
583 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
584 (const char *) utf8_name, *in, in_bound_len));
585 return -EINVAL;
586 }
587 in += nb;
588 in_bound_len -= nb;
589
590 nb = sf_g->nls->uni2char(uni, out, out_bound_len);
591 if (nb < 0)
592 {
593 LogFunc(("nls->uni2char failed(%s) %x:%d\n",
594 utf8_name, uni, out_bound_len));
595 return nb;
596 }
597 out += nb;
598 out_bound_len -= nb;
599 out_len += nb;
600 }
601
602 *out = 0;
603 }
604 else
605 {
606 if (utf8_len + 1 > name_bound_len)
607 return -ENAMETOOLONG;
608
609 memcpy(name, utf8_name, utf8_len + 1);
610 }
611 return 0;
612}
613
614static struct sf_dir_buf *sf_dir_buf_alloc(void)
615{
616 struct sf_dir_buf *b;
617
618 TRACE();
619 b = kmalloc(sizeof(*b), GFP_KERNEL);
620 if (!b)
621 {
622 LogRelFunc(("could not alloc directory buffer\n"));
623 return NULL;
624 }
625
626#ifdef USE_VMALLOC
627 b->buf = vmalloc(DIR_BUFFER_SIZE);
628#else
629 b->buf = kmalloc(DIR_BUFFER_SIZE, GFP_KERNEL);
630#endif
631 if (!b->buf)
632 {
633 kfree(b);
634 LogRelFunc(("could not alloc directory buffer storage\n"));
635 return NULL;
636 }
637
638 INIT_LIST_HEAD(&b->head);
639 b->cEntries = 0;
640 b->cbUsed = 0;
641 b->cbFree = DIR_BUFFER_SIZE;
642 return b;
643}
644
645static void sf_dir_buf_free(struct sf_dir_buf *b)
646{
647 BUG_ON(!b || !b->buf);
648
649 TRACE();
650 list_del(&b->head);
651#ifdef USE_VMALLOC
652 vfree(b->buf);
653#else
654 kfree(b->buf);
655#endif
656 kfree(b);
657}
658
659/**
660 * Free the directory buffer.
661 */
662void sf_dir_info_free(struct sf_dir_info *p)
663{
664 struct list_head *list, *pos, *tmp;
665
666 TRACE();
667 list = &p->info_list;
668 list_for_each_safe(pos, tmp, list)
669 {
670 struct sf_dir_buf *b;
671
672 b = list_entry(pos, struct sf_dir_buf, head);
673 sf_dir_buf_free(b);
674 }
675 kfree(p);
676}
677
678/**
679 * Empty (but not free) the directory buffer.
680 */
681void sf_dir_info_empty(struct sf_dir_info *p)
682{
683 struct list_head *list, *pos, *tmp;
684 TRACE();
685 list = &p->info_list;
686 list_for_each_safe(pos, tmp, list)
687 {
688 struct sf_dir_buf *b;
689 b = list_entry(pos, struct sf_dir_buf, head);
690 b->cEntries = 0;
691 b->cbUsed = 0;
692 b->cbFree = DIR_BUFFER_SIZE;
693 }
694}
695
696/**
697 * Create a new directory buffer descriptor.
698 */
699struct sf_dir_info *sf_dir_info_alloc(void)
700{
701 struct sf_dir_info *p;
702
703 TRACE();
704 p = kmalloc(sizeof(*p), GFP_KERNEL);
705 if (!p)
706 {
707 LogRelFunc(("could not alloc directory info\n"));
708 return NULL;
709 }
710
711 INIT_LIST_HEAD(&p->info_list);
712 return p;
713}
714
715/**
716 * Search for an empty directory content buffer.
717 */
718static struct sf_dir_buf *sf_get_empty_dir_buf(struct sf_dir_info *sf_d)
719{
720 struct list_head *list, *pos;
721
722 list = &sf_d->info_list;
723 list_for_each(pos, list)
724 {
725 struct sf_dir_buf *b;
726
727 b = list_entry(pos, struct sf_dir_buf, head);
728 if (!b)
729 return NULL;
730 else
731 {
732 if (b->cbUsed == 0)
733 return b;
734 }
735 }
736
737 return NULL;
738}
739
740int sf_dir_read_all(struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
741 struct sf_dir_info *sf_d, SHFLHANDLE handle)
742{
743 int err;
744 SHFLSTRING *mask;
745 struct sf_dir_buf *b;
746
747 TRACE();
748 err = sf_make_path(__func__, sf_i, "*", 1, &mask);
749 if (err)
750 goto fail0;
751
752 for (;;)
753 {
754 int rc;
755 void *buf;
756 uint32_t cbSize;
757 uint32_t cEntries;
758
759 b = sf_get_empty_dir_buf(sf_d);
760 if (!b)
761 {
762 b = sf_dir_buf_alloc();
763 if (!b)
764 {
765 err = -ENOMEM;
766 LogRelFunc(("could not alloc directory buffer\n"));
767 goto fail1;
768 }
769 list_add(&b->head, &sf_d->info_list);
770 }
771
772 buf = b->buf;
773 cbSize = b->cbFree;
774
775 rc = vboxCallDirInfo(&client_handle, &sf_g->map, handle, mask,
776 0, 0, &cbSize, buf, &cEntries);
777 switch (rc)
778 {
779 case VINF_SUCCESS:
780 /* fallthrough */
781 case VERR_NO_MORE_FILES:
782 break;
783 case VERR_NO_TRANSLATION:
784 LogFunc(("host could not translate entry\n"));
785 /* XXX */
786 break;
787 default:
788 err = -RTErrConvertToErrno(rc);
789 LogFunc(("vboxCallDirInfo failed rc=%Rrc\n", rc));
790 goto fail1;
791 }
792
793 b->cEntries += cEntries;
794 b->cbFree -= cbSize;
795 b->cbUsed += cbSize;
796
797 if (RT_FAILURE(rc))
798 break;
799 }
800 err = 0;
801
802fail1:
803 kfree(mask);
804
805fail0:
806 return err;
807}
808
809int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS *stat)
810{
811 struct sf_glob_info *sf_g;
812 SHFLVOLINFO SHFLVolumeInfo;
813 uint32_t cbBuffer;
814 int rc;
815
816 sf_g = GET_GLOB_INFO(sb);
817 cbBuffer = sizeof(SHFLVolumeInfo);
818 rc = vboxCallFSInfo(&client_handle, &sf_g->map, 0, SHFL_INFO_GET | SHFL_INFO_VOLUME,
819 &cbBuffer, (PSHFLDIRINFO)&SHFLVolumeInfo);
820 if (RT_FAILURE(rc))
821 return -RTErrConvertToErrno(rc);
822
823 stat->f_type = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
824 stat->f_bsize = SHFLVolumeInfo.ulBytesPerAllocationUnit;
825 stat->f_blocks = SHFLVolumeInfo.ullTotalAllocationBytes
826 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
827 stat->f_bfree = SHFLVolumeInfo.ullAvailableAllocationBytes
828 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
829 stat->f_bavail = SHFLVolumeInfo.ullAvailableAllocationBytes
830 / SHFLVolumeInfo.ulBytesPerAllocationUnit;
831 stat->f_files = 1000;
832 stat->f_ffree = 1000; /* don't return 0 here since the guest may think
833 * that it is not possible to create any more files */
834 stat->f_fsid.val[0] = 0;
835 stat->f_fsid.val[1] = 0;
836 stat->f_namelen = 255;
837 return 0;
838}
839
840struct dentry_operations sf_dentry_ops =
841{
842 .d_revalidate = sf_dentry_revalidate
843};
844
845int sf_init_backing_dev(struct sf_glob_info *sf_g)
846{
847 int rc = 0;
848 /* Each new shared folder map gets a new uint64_t identifier,
849 * allocated in sequence. We ASSUME the sequence will not wrap. */
850 static uint64_t s_u64Sequence = 0;
851 uint64_t u64CurrentSequence = ASMAtomicIncU64(&s_u64Sequence);
852
853#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
854 sf_g->bdi.ra_pages = 0; /* No readahead */
855# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 12)
856 sf_g->bdi.capabilities = BDI_CAP_MAP_DIRECT /* MAP_SHARED */
857 | BDI_CAP_MAP_COPY /* MAP_PRIVATE */
858 | BDI_CAP_READ_MAP /* can be mapped for reading */
859 | BDI_CAP_WRITE_MAP /* can be mapped for writing */
860 | BDI_CAP_EXEC_MAP; /* can be mapped for execution */
861# endif /* >= 2.6.12 */
862# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
863 rc = bdi_init(&sf_g->bdi);
864# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
865 if (!rc)
866 rc = bdi_register(&sf_g->bdi, NULL, "vboxsf-%llu",
867 (unsigned long long)u64CurrentSequence);
868# endif /* >= 2.6.26 */
869# endif /* >= 2.6.24 */
870#endif /* >= 2.6.0 */
871 return rc;
872}
873
874void sf_done_backing_dev(struct sf_glob_info *sf_g)
875{
876#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
877 bdi_destroy(&sf_g->bdi); /* includes bdi_unregister() */
878#endif
879}
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