VirtualBox

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

Last change on this file since 72601 was 70786, checked in by vboxsync, 7 years ago

linux/shared folders: use indent to switch Linux-only files to kernel style.
bugref:9109: Shared folders: update to match in-kernel code more closely

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