VirtualBox

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

Last change on this file since 77247 was 77247, checked in by vboxsync, 6 years ago

linux/vboxsf: Updated kstat->blksize comment with current performance numbers. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.0 KB
Line 
1/* $Id: utils.c 77247 2019-02-11 01:29:32Z vboxsync $ */
2/** @file
3 * vboxsf - VBox Linux Shared Folders VFS, utility functions.
4 *
5 * Utility functions (mainly conversion from/to VirtualBox/Linux data structures).
6 */
7
8/*
9 * Copyright (C) 2006-2019 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 <iprt/err.h>
36#include <linux/nfs_fs.h>
37#include <linux/vfs.h>
38
39/*
40 * sf_reg_aops and sf_backing_dev_info are just quick implementations to make
41 * sendfile work. For more information have a look at
42 *
43 * http://us1.samba.org/samba/ftp/cifs-cvs/ols2006-fs-tutorial-smf.odp
44 *
45 * and the sample implementation
46 *
47 * http://pserver.samba.org/samba/ftp/cifs-cvs/samplefs.tar.gz
48 */
49
50#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
51static void sf_ftime_from_timespec(time_t * time, RTTIMESPEC * ts)
52{
53 int64_t t = RTTimeSpecGetNano(ts);
54
55 do_div(t, 1000000000);
56 *time = t;
57}
58
59static void sf_timespec_from_ftime(RTTIMESPEC * ts, time_t * time)
60{
61 int64_t t = 1000000000 * *time;
62 RTTimeSpecSetNano(ts, t);
63}
64#else /* >= 2.6.0 */
65#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
66static void sf_ftime_from_timespec(struct timespec *tv, RTTIMESPEC *ts)
67#else
68static void sf_ftime_from_timespec(struct timespec64 *tv, RTTIMESPEC *ts)
69#endif
70{
71 int64_t t = RTTimeSpecGetNano(ts);
72 int64_t nsec;
73
74 nsec = do_div(t, 1000000000);
75 tv->tv_sec = t;
76 tv->tv_nsec = nsec;
77}
78
79#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
80static void sf_timespec_from_ftime(RTTIMESPEC *ts, struct timespec *tv)
81#else
82static void sf_timespec_from_ftime(RTTIMESPEC *ts, struct timespec64 *tv)
83#endif
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 sf_init_inode(struct sf_glob_info *sf_g, struct inode *inode,
92 PSHFLFSOBJINFO info)
93{
94 PSHFLFSOBJATTR attr;
95 int mode;
96
97 TRACE();
98
99 attr = &info->Attr;
100
101#define mode_set(r) attr->fMode & (RTFS_UNIX_##r) ? (S_##r) : 0;
102 mode = mode_set(IRUSR);
103 mode |= mode_set(IWUSR);
104 mode |= mode_set(IXUSR);
105
106 mode |= mode_set(IRGRP);
107 mode |= mode_set(IWGRP);
108 mode |= mode_set(IXGRP);
109
110 mode |= mode_set(IROTH);
111 mode |= mode_set(IWOTH);
112 mode |= mode_set(IXOTH);
113
114#undef mode_set
115
116#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
117 inode->i_mapping->a_ops = &sf_reg_aops;
118# if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
119 /* XXX Was this ever necessary? */
120 inode->i_mapping->backing_dev_info = &sf_g->bdi;
121# endif
122#endif
123
124 if (RTFS_IS_DIRECTORY(attr->fMode)) {
125 inode->i_mode = sf_g->dmode != ~0 ? (sf_g->dmode & 0777) : mode;
126 inode->i_mode &= ~sf_g->dmask;
127 inode->i_mode |= S_IFDIR;
128 inode->i_op = &sf_dir_iops;
129 inode->i_fop = &sf_dir_fops;
130 /* XXX: this probably should be set to the number of entries
131 in the directory plus two (. ..) */
132#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
133 set_nlink(inode, 1);
134#else
135 inode->i_nlink = 1;
136#endif
137 }
138#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
139 else if (RTFS_IS_SYMLINK(attr->fMode)) {
140 inode->i_mode = sf_g->fmode != ~0 ? (sf_g->fmode & 0777) : mode;
141 inode->i_mode &= ~sf_g->fmask;
142 inode->i_mode |= S_IFLNK;
143 inode->i_op = &sf_lnk_iops;
144# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
145 set_nlink(inode, 1);
146# else
147 inode->i_nlink = 1;
148# endif
149 }
150#endif
151 else {
152 inode->i_mode = sf_g->fmode != ~0 ? (sf_g->fmode & 0777) : mode;
153 inode->i_mode &= ~sf_g->fmask;
154 inode->i_mode |= S_IFREG;
155 inode->i_op = &sf_reg_iops;
156 inode->i_fop = &sf_reg_fops;
157#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
158 set_nlink(inode, 1);
159#else
160 inode->i_nlink = 1;
161#endif
162 }
163
164#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
165 inode->i_uid = make_kuid(current_user_ns(), sf_g->uid);
166 inode->i_gid = make_kgid(current_user_ns(), sf_g->gid);
167#else
168 inode->i_uid = sf_g->uid;
169 inode->i_gid = sf_g->gid;
170#endif
171
172 inode->i_size = info->cbObject;
173#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
174 inode->i_blksize = 4096;
175#endif
176#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
177 inode->i_blkbits = 12;
178#endif
179 /* i_blocks always in units of 512 bytes! */
180 inode->i_blocks = (info->cbAllocated + 511) / 512;
181
182 sf_ftime_from_timespec(&inode->i_atime, &info->AccessTime);
183 sf_ftime_from_timespec(&inode->i_ctime, &info->ChangeTime);
184 sf_ftime_from_timespec(&inode->i_mtime, &info->ModificationTime);
185}
186
187int sf_stat(const char *caller, struct sf_glob_info *sf_g,
188 SHFLSTRING *path, PSHFLFSOBJINFO result, int ok_to_fail)
189{
190 int rc;
191#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
192 SHFLCREATEPARMS params;
193#else
194 VBOXSFCREATEREQ *pReq;
195#endif
196 NOREF(caller);
197
198 TRACE();
199
200#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
201 RT_ZERO(params);
202 params.Handle = SHFL_HANDLE_NIL;
203 params.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
204 LogFunc(("sf_stat: calling VbglR0SfCreate, file %s, flags %#x\n",
205 path->String.utf8, params.CreateFlags));
206 rc = VbglR0SfCreate(&client_handle, &sf_g->map, path, &params);
207 if (rc == VERR_INVALID_NAME) {
208 /* this can happen for names like 'foo*' on a Windows host */
209 return -ENOENT;
210 }
211 if (RT_FAILURE(rc)) {
212 LogFunc(("VbglR0SfCreate(%s) failed. caller=%s, rc=%Rrc\n",
213 path->String.utf8, rc, caller));
214 return -EPROTO;
215 }
216 if (params.Result != SHFL_FILE_EXISTS) {
217 if (!ok_to_fail)
218 LogFunc(("VbglR0SfCreate(%s) file does not exist. caller=%s, result=%d\n", path->String.utf8, params.Result, caller));
219 return -ENOENT;
220 }
221
222 *result = params.Info;
223 return 0;
224#else
225 pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + path->u16Size);
226 if (pReq) {
227 RT_ZERO(*pReq);
228 memcpy(&pReq->StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
229 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
230 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
231
232 LogFunc(("Calling VbglR0SfHostReqCreate on %s\n", path->String.utf8));
233 rc = VbglR0SfHostReqCreate(sf_g->map.root, pReq);
234 if (RT_SUCCESS(rc)) {
235 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
236 *result = pReq->CreateParms.Info;
237 rc = 0;
238 } else {
239 if (!ok_to_fail)
240 LogFunc(("VbglR0SfHostReqCreate on %s: file does not exist: %d (caller=%s)\n",
241 path->String.utf8, pReq->CreateParms.Result, caller));
242 rc = -ENOENT;
243 }
244 } else if (rc == VERR_INVALID_NAME) {
245 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
246 } else {
247 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc (caller=%s)\n", path->String.utf8, rc, caller));
248 rc = -EPROTO;
249 }
250 VbglR0PhysHeapFree(pReq);
251 }
252 else
253 rc = -ENOMEM;
254 return rc;
255#endif
256}
257
258/* this is called directly as iop on 2.4, indirectly as dop
259 [sf_dentry_revalidate] on 2.4/2.6, indirectly as iop through
260 [sf_getattr] on 2.6. the job is to find out whether dentry/inode is
261 still valid. the test is failed if [dentry] does not have an inode
262 or [sf_stat] is unsuccessful, otherwise we return success and
263 update inode attributes */
264int sf_inode_revalidate(struct dentry *dentry)
265{
266 int err;
267 struct sf_glob_info *sf_g;
268 struct sf_inode_info *sf_i;
269 SHFLFSOBJINFO info;
270
271 TRACE();
272 if (!dentry || !dentry->d_inode) {
273 LogFunc(("no dentry(%p) or inode(%p)\n", dentry,
274 dentry->d_inode));
275 return -EINVAL;
276 }
277
278 sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
279 sf_i = GET_INODE_INFO(dentry->d_inode);
280
281#if 0
282 printk("%s called by %p:%p\n",
283 sf_i->path->String.utf8,
284 __builtin_return_address(0), __builtin_return_address(1));
285#endif
286
287 BUG_ON(!sf_g);
288 BUG_ON(!sf_i);
289
290 if (!sf_i->force_restat) {
291 if (jiffies - dentry->d_time < sf_g->ttl)
292 return 0;
293 }
294
295 err = sf_stat(__func__, sf_g, sf_i->path, &info, 1);
296 if (err)
297 return err;
298
299 dentry->d_time = jiffies;
300 sf_init_inode(sf_g, dentry->d_inode, &info);
301 return 0;
302}
303
304/* this is called during name resolution/lookup to check if the
305 [dentry] in the cache is still valid. the job is handled by
306 [sf_inode_revalidate] */
307static int
308#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
309sf_dentry_revalidate(struct dentry *dentry, unsigned flags)
310#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
311sf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
312#else
313sf_dentry_revalidate(struct dentry *dentry, int flags)
314#endif
315{
316 TRACE();
317
318#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
319 if (flags & LOOKUP_RCU)
320 return -ECHILD;
321#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
322 /* see Documentation/filesystems/vfs.txt */
323 if (nd && nd->flags & LOOKUP_RCU)
324 return -ECHILD;
325#endif
326
327 if (sf_inode_revalidate(dentry))
328 return 0;
329
330 return 1;
331}
332
333/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
334 effect) updates inode attributes for [dentry] (given that [dentry]
335 has inode at all) from these new attributes we derive [kstat] via
336 [generic_fillattr] */
337#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
338
339# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
340int sf_getattr(const struct path *path, struct kstat *kstat, u32 request_mask,
341 unsigned int flags)
342# else
343int sf_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
344# endif
345{
346 int err;
347# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
348 struct dentry *dentry = path->dentry;
349# endif
350
351 TRACE();
352 err = sf_inode_revalidate(dentry);
353 if (err)
354 return err;
355
356 generic_fillattr(dentry->d_inode, kstat);
357
358 /*
359 * FsPerf shows the following numbers for sequential file access against
360 * a tmpfs folder on an AMD 1950X host running debian buster/sid:
361 *
362 * block size = r128600 ----- r128753 -----
363 * reads reads writes
364 * 4096 KB = 2254 MB/s 4953 MB/s 3668 MB/s
365 * 2048 KB = 2368 MB/s 4908 MB/s 3541 MB/s
366 * 1024 KB = 2208 MB/s 4011 MB/s 3291 MB/s
367 * 512 KB = 1908 MB/s 3399 MB/s 2721 MB/s
368 * 256 KB = 1625 MB/s 2679 MB/s 2251 MB/s
369 * 128 KB = 1413 MB/s 1967 MB/s 1684 MB/s
370 * 64 KB = 1152 MB/s 1409 MB/s 1265 MB/s
371 * 32 KB = 726 MB/s 815 MB/s 783 MB/s
372 * 16 KB = 683 MB/s 475 MB/s
373 * 8 KB = 294 MB/s 286 MB/s
374 * 4 KB = 145 MB/s 156 MB/s 149 MB/s
375 *
376 */
377 if (S_ISREG(kstat->mode))
378 kstat->blksize = _1M;
379 else if (S_ISDIR(kstat->mode))
380 /** @todo this may need more tuning after we rewrite the directory handling. */
381 kstat->blksize = _16K;
382 return 0;
383}
384
385int sf_setattr(struct dentry *dentry, struct iattr *iattr)
386{
387 struct sf_glob_info *sf_g;
388 struct sf_inode_info *sf_i;
389# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
390 SHFLCREATEPARMS CreateParms;
391 SHFLCREATEPARMS *pCreateParms = &CreateParms;
392 SHFLFSOBJINFO InfoBuf;
393 SHFLFSOBJINFO *pInfo = &InfoBuf;
394 uint32_t cbBuffer;
395 int rc, err;
396# else
397 union SetAttrReqs
398 {
399 VBOXSFCREATEREQ Create;
400 VBOXSFOBJINFOREQ Info;
401 VBOXSFSETFILESIZEREQ SetSize;
402 VBOXSFCLOSEREQ Close;
403 } *pReq;
404 size_t cbReq;
405 SHFLCREATEPARMS *pCreateParms;
406 SHFLFSOBJINFO *pInfo;
407 SHFLHANDLE hHostFile;
408 int vrc;
409 int err = 0;
410# endif
411
412 TRACE();
413
414 sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
415 sf_i = GET_INODE_INFO(dentry->d_inode);
416# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
417 err = 0;
418# else
419 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
420 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
421 if (!pReq) {
422 LogFunc(("Failed to allocate %#x byte request buffer!\n", cbReq));
423 return -ENOMEM;
424 }
425 pCreateParms = &pReq->Create.CreateParms;
426 pInfo = &pReq->Info.ObjInfo;
427# endif
428
429 RT_ZERO(*pCreateParms);
430 pCreateParms->Handle = SHFL_HANDLE_NIL;
431 pCreateParms->CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
432 | SHFL_CF_ACT_FAIL_IF_NEW
433 | SHFL_CF_ACCESS_ATTR_WRITE;
434
435 /* this is at least required for Posix hosts */
436 if (iattr->ia_valid & ATTR_SIZE)
437 pCreateParms->CreateFlags |= SHFL_CF_ACCESS_WRITE;
438
439# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
440 rc = VbglR0SfCreate(&client_handle, &sf_g->map, sf_i->path, pCreateParms);
441 if (RT_FAILURE(rc)) {
442 LogFunc(("VbglR0SfCreate(%s) failed rc=%Rrc\n",
443 sf_i->path->String.utf8, rc));
444 err = -RTErrConvertToErrno(rc);
445 goto fail2;
446 }
447# else
448 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
449 vrc = VbglR0SfHostReqCreate(sf_g->map.root, &pReq->Create);
450 if (RT_SUCCESS(vrc)) {
451 hHostFile = pCreateParms->Handle;
452 } else {
453 err = -RTErrConvertToErrno(vrc);
454 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
455 goto fail2;
456 }
457# endif
458 if (pCreateParms->Result != SHFL_FILE_EXISTS) {
459 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
460 err = -ENOENT;
461 goto fail1;
462 }
463
464 /* Setting the file size and setting the other attributes has to be
465 * handled separately, see implementation of vbsfSetFSInfo() in
466 * vbsf.cpp */
467 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME)) {
468# define mode_set(r) ((iattr->ia_mode & (S_##r)) ? RTFS_UNIX_##r : 0)
469
470 RT_ZERO(*pInfo);
471 if (iattr->ia_valid & ATTR_MODE) {
472 pInfo->Attr.fMode = mode_set(IRUSR);
473 pInfo->Attr.fMode |= mode_set(IWUSR);
474 pInfo->Attr.fMode |= mode_set(IXUSR);
475 pInfo->Attr.fMode |= mode_set(IRGRP);
476 pInfo->Attr.fMode |= mode_set(IWGRP);
477 pInfo->Attr.fMode |= mode_set(IXGRP);
478 pInfo->Attr.fMode |= mode_set(IROTH);
479 pInfo->Attr.fMode |= mode_set(IWOTH);
480 pInfo->Attr.fMode |= mode_set(IXOTH);
481
482 if (iattr->ia_mode & S_IFDIR)
483 pInfo->Attr.fMode |= RTFS_TYPE_DIRECTORY;
484 else
485 pInfo->Attr.fMode |= RTFS_TYPE_FILE;
486 }
487
488 if (iattr->ia_valid & ATTR_ATIME)
489 sf_timespec_from_ftime(&pInfo->AccessTime,
490 &iattr->ia_atime);
491 if (iattr->ia_valid & ATTR_MTIME)
492 sf_timespec_from_ftime(&pInfo->ModificationTime,
493 &iattr->ia_mtime);
494 /* ignore ctime (inode change time) as it can't be set from userland anyway */
495
496# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
497 cbBuffer = sizeof(*pInfo);
498 rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, pCreateParms->Handle,
499 SHFL_INFO_SET | SHFL_INFO_FILE, &cbBuffer,
500 (PSHFLDIRINFO)pInfo);
501 if (RT_FAILURE(rc)) {
502 LogFunc(("VbglR0SfFsInfo(%s, FILE) failed rc=%Rrc\n",
503 sf_i->path->String.utf8, rc));
504 err = -RTErrConvertToErrno(rc);
505 goto fail1;
506 }
507# else
508 vrc = VbglR0SfHostReqSetObjInfo(sf_g->map.root, &pReq->Info, hHostFile);
509 if (RT_FAILURE(vrc)) {
510 err = -RTErrConvertToErrno(vrc);
511 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
512 goto fail1;
513 }
514# endif
515 }
516
517 if (iattr->ia_valid & ATTR_SIZE) {
518# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
519 RT_ZERO(*pInfo);
520 pInfo->cbObject = iattr->ia_size;
521 cbBuffer = sizeof(*pInfo);
522 rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, pCreateParms->Handle,
523 SHFL_INFO_SET | SHFL_INFO_SIZE, &cbBuffer,
524 (PSHFLDIRINFO)pInfo);
525 if (RT_FAILURE(rc)) {
526 LogFunc(("VbglR0SfFsInfo(%s, SIZE) failed rc=%Rrc\n",
527 sf_i->path->String.utf8, rc));
528 err = -RTErrConvertToErrno(rc);
529 goto fail1;
530 }
531# else
532 vrc = VbglR0SfHostReqSetFileSize(sf_g->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
533 /** @todo Implement fallback if host is < 6.0? */
534 if (RT_FAILURE(vrc)) {
535 err = -RTErrConvertToErrno(vrc);
536 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc err=%d\n",
537 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, err));
538 goto fail1;
539 }
540# endif
541 }
542
543# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
544 rc = VbglR0SfClose(&client_handle, &sf_g->map, pCreateParms->Handle);
545 if (RT_FAILURE(rc))
546 LogFunc(("VbglR0SfClose(%s) failed rc=%Rrc\n",
547 sf_i->path->String.utf8, rc));
548# else
549 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
550 if (RT_FAILURE(vrc))
551 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
552 VbglR0PhysHeapFree(pReq);
553# endif
554
555 /** @todo r=bird: I guess we're calling revalidate here to update the inode
556 * info. However, due to the TTL optimization this is not guarenteed to happen.
557 *
558 * Also, we already have accurate stat information on the file, either from the
559 * SHFL_FN_CREATE call or from SHFL_FN_INFORMATION, so there is no need to do a
560 * slow stat()-like operation to retrieve the information again.
561 *
562 * What's more, given that the SHFL_FN_CREATE call succeeded, we know that the
563 * dentry and all its parent entries are valid and could touch their timestamps
564 * extending their TTL (CIFS does that). */
565 return sf_inode_revalidate(dentry);
566
567 fail1:
568# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
569 rc = VbglR0SfClose(&client_handle, &sf_g->map, pCreateParms->Handle);
570 if (RT_FAILURE(rc))
571 LogFunc(("VbglR0SfClose(%s) failed rc=%Rrc\n",
572 sf_i->path->String.utf8, rc));
573# else
574 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
575 if (RT_FAILURE(vrc))
576 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc; err=%d\n", sf_i->path->String.utf8, hHostFile, vrc, err));
577# endif
578
579 fail2:
580# ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
581 VbglR0PhysHeapFree(pReq);
582# endif
583 return err;
584}
585
586#endif /* >= 2.6.0 */
587
588static int sf_make_path(const char *caller, struct sf_inode_info *sf_i,
589 const char *d_name, size_t d_len, SHFLSTRING ** result)
590{
591 size_t path_len, shflstring_len;
592 SHFLSTRING *tmp;
593 uint16_t p_len;
594 uint8_t *p_name;
595 int fRoot = 0;
596
597 TRACE();
598 p_len = sf_i->path->u16Length;
599 p_name = sf_i->path->String.utf8;
600
601 if (p_len == 1 && *p_name == '/') {
602 path_len = d_len + 1;
603 fRoot = 1;
604 } else {
605 /* lengths of constituents plus terminating zero plus slash */
606 path_len = p_len + d_len + 2;
607 if (path_len > 0xffff) {
608 LogFunc(("path too long. caller=%s, path_len=%zu\n",
609 caller, path_len));
610 return -ENAMETOOLONG;
611 }
612 }
613
614 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
615 tmp = kmalloc(shflstring_len, GFP_KERNEL);
616 if (!tmp) {
617 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
618 return -ENOMEM;
619 }
620 tmp->u16Length = path_len - 1;
621 tmp->u16Size = path_len;
622
623 if (fRoot)
624 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
625 else {
626 memcpy(&tmp->String.utf8[0], p_name, p_len);
627 tmp->String.utf8[p_len] = '/';
628 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
629 tmp->String.utf8[p_len + 1 + d_len] = '\0';
630 }
631
632 *result = tmp;
633 return 0;
634}
635
636/**
637 * [dentry] contains string encoded in coding system that corresponds
638 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
639 * [sf_make_path] which will allocate SHFLSTRING and fill it in
640 */
641int sf_path_from_dentry(const char *caller, struct sf_glob_info *sf_g,
642 struct sf_inode_info *sf_i, struct dentry *dentry,
643 SHFLSTRING ** result)
644{
645 int err;
646 const char *d_name;
647 size_t d_len;
648 const char *name;
649 size_t len = 0;
650
651 TRACE();
652 d_name = dentry->d_name.name;
653 d_len = dentry->d_name.len;
654
655 if (sf_g->nls) {
656 size_t in_len, i, out_bound_len;
657 const char *in;
658 char *out;
659
660 in = d_name;
661 in_len = d_len;
662
663 out_bound_len = PATH_MAX;
664 out = kmalloc(out_bound_len, GFP_KERNEL);
665 name = out;
666
667 for (i = 0; i < d_len; ++i) {
668 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
669 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
670 linux_wchar_t uni;
671 int nb;
672
673 nb = sf_g->nls->char2uni(in, in_len, &uni);
674 if (nb < 0) {
675 LogFunc(("nls->char2uni failed %x %d\n",
676 *in, in_len));
677 err = -EINVAL;
678 goto fail1;
679 }
680 in_len -= nb;
681 in += nb;
682
683#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
684 nb = utf32_to_utf8(uni, out, out_bound_len);
685#else
686 nb = utf8_wctomb(out, uni, out_bound_len);
687#endif
688 if (nb < 0) {
689 LogFunc(("nls->uni2char failed %x %d\n",
690 uni, out_bound_len));
691 err = -EINVAL;
692 goto fail1;
693 }
694 out_bound_len -= nb;
695 out += nb;
696 len += nb;
697 }
698 if (len >= PATH_MAX - 1) {
699 err = -ENAMETOOLONG;
700 goto fail1;
701 }
702
703 LogFunc(("result(%d) = %.*s\n", len, len, name));
704 *out = 0;
705 } else {
706 name = d_name;
707 len = d_len;
708 }
709
710 err = sf_make_path(caller, sf_i, name, len, result);
711 if (name != d_name)
712 kfree(name);
713
714 return err;
715
716 fail1:
717 kfree(name);
718 return err;
719}
720
721int sf_nlscpy(struct sf_glob_info *sf_g,
722 char *name, size_t name_bound_len,
723 const unsigned char *utf8_name, size_t utf8_len)
724{
725 if (sf_g->nls) {
726 const char *in;
727 char *out;
728 size_t out_len;
729 size_t out_bound_len;
730 size_t in_bound_len;
731
732 in = utf8_name;
733 in_bound_len = utf8_len;
734
735 out = name;
736 out_len = 0;
737 out_bound_len = name_bound_len;
738
739 while (in_bound_len) {
740 int nb;
741#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
742 unicode_t uni;
743
744 nb = utf8_to_utf32(in, in_bound_len, &uni);
745#else
746 linux_wchar_t uni;
747
748 nb = utf8_mbtowc(&uni, in, in_bound_len);
749#endif
750 if (nb < 0) {
751 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
752 (const char *)utf8_name, *in,
753 in_bound_len));
754 return -EINVAL;
755 }
756 in += nb;
757 in_bound_len -= nb;
758
759 nb = sf_g->nls->uni2char(uni, out, out_bound_len);
760 if (nb < 0) {
761 LogFunc(("nls->uni2char failed(%s) %x:%d\n",
762 utf8_name, uni, out_bound_len));
763 return nb;
764 }
765 out += nb;
766 out_bound_len -= nb;
767 out_len += nb;
768 }
769
770 *out = 0;
771 } else {
772 if (utf8_len + 1 > name_bound_len)
773 return -ENAMETOOLONG;
774
775 memcpy(name, utf8_name, utf8_len + 1);
776 }
777 return 0;
778}
779
780static struct sf_dir_buf *sf_dir_buf_alloc(void)
781{
782 struct sf_dir_buf *b;
783
784 TRACE();
785 b = kmalloc(sizeof(*b), GFP_KERNEL);
786 if (!b) {
787 LogRelFunc(("could not alloc directory buffer\n"));
788 return NULL;
789 }
790 b->buf = kmalloc(DIR_BUFFER_SIZE, GFP_KERNEL);
791 if (!b->buf) {
792 kfree(b);
793 LogRelFunc(("could not alloc directory buffer storage\n"));
794 return NULL;
795 }
796
797 INIT_LIST_HEAD(&b->head);
798 b->cEntries = 0;
799 b->cbUsed = 0;
800 b->cbFree = DIR_BUFFER_SIZE;
801 return b;
802}
803
804static void sf_dir_buf_free(struct sf_dir_buf *b)
805{
806 BUG_ON(!b || !b->buf);
807
808 TRACE();
809 list_del(&b->head);
810 kfree(b->buf);
811 kfree(b);
812}
813
814/**
815 * Free the directory buffer.
816 */
817void sf_dir_info_free(struct sf_dir_info *p)
818{
819 struct list_head *list, *pos, *tmp;
820
821 TRACE();
822 list = &p->info_list;
823 list_for_each_safe(pos, tmp, list) {
824 struct sf_dir_buf *b;
825
826 b = list_entry(pos, struct sf_dir_buf, head);
827 sf_dir_buf_free(b);
828 }
829 kfree(p);
830}
831
832/**
833 * Empty (but not free) the directory buffer.
834 */
835void sf_dir_info_empty(struct sf_dir_info *p)
836{
837 struct list_head *list, *pos, *tmp;
838 TRACE();
839 list = &p->info_list;
840 list_for_each_safe(pos, tmp, list) {
841 struct sf_dir_buf *b;
842 b = list_entry(pos, struct sf_dir_buf, head);
843 b->cEntries = 0;
844 b->cbUsed = 0;
845 b->cbFree = DIR_BUFFER_SIZE;
846 }
847}
848
849/**
850 * Create a new directory buffer descriptor.
851 */
852struct sf_dir_info *sf_dir_info_alloc(void)
853{
854 struct sf_dir_info *p;
855
856 TRACE();
857 p = kmalloc(sizeof(*p), GFP_KERNEL);
858 if (!p) {
859 LogRelFunc(("could not alloc directory info\n"));
860 return NULL;
861 }
862
863 INIT_LIST_HEAD(&p->info_list);
864 return p;
865}
866
867/**
868 * Search for an empty directory content buffer.
869 */
870static struct sf_dir_buf *sf_get_empty_dir_buf(struct sf_dir_info *sf_d)
871{
872 struct list_head *list, *pos;
873
874 list = &sf_d->info_list;
875 list_for_each(pos, list) {
876 struct sf_dir_buf *b;
877
878 b = list_entry(pos, struct sf_dir_buf, head);
879 if (!b)
880 return NULL;
881 else {
882 if (b->cbUsed == 0)
883 return b;
884 }
885 }
886
887 return NULL;
888}
889
890/** @todo r=bird: Why on earth do we read in the entire directory??? This
891 * cannot be healthy for like big directory and such... */
892int sf_dir_read_all(struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
893 struct sf_dir_info *sf_d, SHFLHANDLE handle)
894{
895 int err;
896 SHFLSTRING *mask;
897#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
898 VBOXSFLISTDIRREQ *pReq = NULL;
899#endif
900
901 TRACE();
902 err = sf_make_path(__func__, sf_i, "*", 1, &mask);
903 if (err)
904 goto fail0;
905#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
906 pReq = (VBOXSFLISTDIRREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
907 if (!pReq)
908 goto fail1;
909#endif
910
911 for (;;) {
912 int rc;
913 struct sf_dir_buf *b;
914#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
915 void *buf;
916 uint32_t cbSize;
917 uint32_t cEntries;
918#endif
919
920 b = sf_get_empty_dir_buf(sf_d);
921 if (!b) {
922 b = sf_dir_buf_alloc();
923 if (!b) {
924 err = -ENOMEM;
925 LogRelFunc(("could not alloc directory buffer\n"));
926 goto fail2;
927 }
928 list_add(&b->head, &sf_d->info_list);
929 }
930
931#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
932 buf = b->buf;
933 cbSize = b->cbFree;
934
935 rc = VbglR0SfDirInfo(&client_handle, &sf_g->map, handle, mask,
936 0, 0, &cbSize, buf, &cEntries);
937 switch (rc) {
938 case VINF_SUCCESS:
939 RT_FALL_THRU();
940 case VERR_NO_MORE_FILES:
941 break;
942 case VERR_NO_TRANSLATION:
943 LogFunc(("host could not translate entry\n"));
944 /* XXX */
945 break;
946 default:
947 err = -RTErrConvertToErrno(rc);
948 LogFunc(("VbglR0SfDirInfo failed rc=%Rrc\n", rc));
949 goto fail2;
950 }
951
952 b->cEntries += cEntries;
953 b->cbFree -= cbSize;
954 b->cbUsed += cbSize;
955
956 if (RT_FAILURE(rc))
957 break;
958#else
959 rc = VbglR0SfHostReqListDirContig2x(sf_g->map.root, pReq, handle, mask, virt_to_phys(mask),
960 0 /*fFlags*/, b->buf, virt_to_phys(b->buf), b->cbFree);
961 if (RT_SUCCESS(rc)) {
962 b->cEntries += pReq->Parms.c32Entries.u.value32;
963 b->cbFree -= pReq->Parms.cb32Buffer.u.value32;
964 b->cbUsed += pReq->Parms.cb32Buffer.u.value32;
965 } else if (rc == VERR_NO_MORE_FILES) {
966 break;
967 } else {
968 err = -RTErrConvertToErrno(rc);
969 LogFunc(("VbglR0SfHostReqListDirContig2x failed rc=%Rrc err=%d\n", rc, err));
970 goto fail2;
971 }
972#endif
973 }
974 err = 0;
975
976 fail2:
977#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
978 VbglR0PhysHeapFree(pReq);
979#endif
980 fail1:
981 kfree(mask);
982
983 fail0:
984 return err;
985}
986
987int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS * stat)
988{
989 struct sf_glob_info *sf_g;
990#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
991 SHFLVOLINFO SHFLVolumeInfo;
992 SHFLVOLINFO *pVolInfo = &SHFLVolumeInfo;
993 uint32_t cbBuffer;
994#else
995 VBOXSFVOLINFOREQ *pReq;
996 SHFLVOLINFO *pVolInfo;
997#endif
998 int rc;
999
1000 sf_g = GET_GLOB_INFO(sb);
1001#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
1002 cbBuffer = sizeof(SHFLVolumeInfo);
1003 rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, 0,
1004 SHFL_INFO_GET | SHFL_INFO_VOLUME, &cbBuffer,
1005 (PSHFLDIRINFO) & SHFLVolumeInfo);
1006#else
1007 pReq = VbglR0PhysHeapAlloc(sizeof(*pReq));
1008 if (pReq) {
1009 pVolInfo = &pReq->VolInfo;
1010 rc = VbglR0SfHostReqQueryVolInfo(sf_g->map.root, pReq, SHFL_HANDLE_ROOT);
1011#endif
1012 if (RT_SUCCESS(rc)) {
1013 stat->f_type = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
1014 stat->f_bsize = pVolInfo->ulBytesPerAllocationUnit;
1015#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 73)
1016 stat->f_frsize = pVolInfo->ulBytesPerAllocationUnit;
1017#endif
1018 stat->f_blocks = pVolInfo->ullTotalAllocationBytes
1019 / pVolInfo->ulBytesPerAllocationUnit;
1020 stat->f_bfree = pVolInfo->ullAvailableAllocationBytes
1021 / pVolInfo->ulBytesPerAllocationUnit;
1022 stat->f_bavail = pVolInfo->ullAvailableAllocationBytes
1023 / pVolInfo->ulBytesPerAllocationUnit;
1024 stat->f_files = 1000;
1025 stat->f_ffree = 1000; /* don't return 0 here since the guest may think
1026 * that it is not possible to create any more files */
1027 stat->f_fsid.val[0] = 0;
1028 stat->f_fsid.val[1] = 0;
1029 stat->f_namelen = 255;
1030#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
1031 stat->f_flags = 0; /* not valid */
1032#endif
1033 RT_ZERO(stat->f_spare);
1034 rc = 0;
1035 } else
1036 rc = -RTErrConvertToErrno(rc);
1037#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
1038 VbglR0PhysHeapFree(pReq);
1039 } else
1040 rc = -ENOMEM;
1041#endif
1042 return rc;
1043}
1044
1045struct dentry_operations sf_dentry_ops = {
1046 .d_revalidate = sf_dentry_revalidate
1047};
1048
1049int sf_init_backing_dev(struct sf_glob_info *sf_g)
1050{
1051 int rc = 0;
1052#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
1053 /* Each new shared folder map gets a new uint64_t identifier,
1054 * allocated in sequence. We ASSUME the sequence will not wrap. */
1055 static uint64_t s_u64Sequence = 0;
1056 uint64_t u64CurrentSequence = ASMAtomicIncU64(&s_u64Sequence);
1057
1058 sf_g->bdi.ra_pages = 0; /* No readahead */
1059# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 12)
1060 sf_g->bdi.capabilities = BDI_CAP_MAP_DIRECT /* MAP_SHARED */
1061 | BDI_CAP_MAP_COPY /* MAP_PRIVATE */
1062 | BDI_CAP_READ_MAP /* can be mapped for reading */
1063 | BDI_CAP_WRITE_MAP /* can be mapped for writing */
1064 | BDI_CAP_EXEC_MAP; /* can be mapped for execution */
1065# endif /* >= 2.6.12 */
1066# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1067 rc = bdi_init(&sf_g->bdi);
1068# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
1069 if (!rc)
1070 rc = bdi_register(&sf_g->bdi, NULL, "vboxsf-%llu",
1071 (unsigned long long)u64CurrentSequence);
1072# endif /* >= 2.6.26 */
1073# endif /* >= 2.6.24 */
1074#endif /* >= 2.6.0 && <= 3.19.0 */
1075 return rc;
1076}
1077
1078void sf_done_backing_dev(struct sf_glob_info *sf_g)
1079{
1080#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) && LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
1081 bdi_destroy(&sf_g->bdi); /* includes bdi_unregister() */
1082#endif
1083}
1084
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