VirtualBox

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

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

Additions: relicence components needed for Linux shared folders to MIT.
bugref:9109: Shared folders: update to match in-kernel code more closely
This change makes the code on which the Linux kernel shared folder patch is
based MIT-licenced, so that the version in the Linux kernel can be too. This
would make it easier to move code back and forth.

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