VirtualBox

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

Last change on this file since 23196 was 23196, checked in by vboxsync, 16 years ago

Linux shared folders: missing bit from r52613 (need to open the file writable when attempting to changing it's size)

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette