VirtualBox

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

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

linux/vboxsf: Set st_blksize to 1MB for files and 16KB for directories, rather than the 4KB we used to. With 1MB I see close to optimal sequential read performance here, 15x better than with 4KB. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.6 KB
Line 
1/* $Id: utils.c 77140 2019-02-01 19:38:26Z 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 reads:
360 * 4096 KB = 2254 MB/s
361 * 2048 KB = 2368 MB/s
362 * 1024 KB = 2208 MB/s
363 * 512 KB = 1908 MB/s
364 * 256 KB = 1625 MB/s
365 * 128 KB = 1413 MB/s
366 * 64 KB = 1152 MB/s
367 * 32 KB = 726 MB/s
368 * 4 KB = 145 MB/s
369 */
370 if (S_ISREG(kstat->mode))
371 kstat->blksize = _1M;
372 else if (S_ISDIR(kstat->mode))
373 /** @todo this may need more tuning after we rewrite the directory handling. */
374 kstat->blksize = _16K;
375 return 0;
376}
377
378int sf_setattr(struct dentry *dentry, struct iattr *iattr)
379{
380 struct sf_glob_info *sf_g;
381 struct sf_inode_info *sf_i;
382# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
383 SHFLCREATEPARMS CreateParms;
384 SHFLCREATEPARMS *pCreateParms = &CreateParms;
385 SHFLFSOBJINFO InfoBuf;
386 SHFLFSOBJINFO *pInfo = &InfoBuf;
387 uint32_t cbBuffer;
388 int rc, err;
389# else
390 union SetAttrReqs
391 {
392 VBOXSFCREATEREQ Create;
393 VBOXSFOBJINFOREQ Info;
394 VBOXSFSETFILESIZEREQ SetSize;
395 VBOXSFCLOSEREQ Close;
396 } *pReq;
397 size_t cbReq;
398 SHFLCREATEPARMS *pCreateParms;
399 SHFLFSOBJINFO *pInfo;
400 SHFLHANDLE hHostFile;
401 int vrc;
402 int err = 0;
403# endif
404
405 TRACE();
406
407 sf_g = GET_GLOB_INFO(dentry->d_inode->i_sb);
408 sf_i = GET_INODE_INFO(dentry->d_inode);
409# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
410 err = 0;
411# else
412 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
413 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
414 if (!pReq) {
415 LogFunc(("Failed to allocate %#x byte request buffer!\n", cbReq));
416 return -ENOMEM;
417 }
418 pCreateParms = &pReq->Create.CreateParms;
419 pInfo = &pReq->Info.ObjInfo;
420# endif
421
422 RT_ZERO(*pCreateParms);
423 pCreateParms->Handle = SHFL_HANDLE_NIL;
424 pCreateParms->CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
425 | SHFL_CF_ACT_FAIL_IF_NEW
426 | SHFL_CF_ACCESS_ATTR_WRITE;
427
428 /* this is at least required for Posix hosts */
429 if (iattr->ia_valid & ATTR_SIZE)
430 pCreateParms->CreateFlags |= SHFL_CF_ACCESS_WRITE;
431
432# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
433 rc = VbglR0SfCreate(&client_handle, &sf_g->map, sf_i->path, pCreateParms);
434 if (RT_FAILURE(rc)) {
435 LogFunc(("VbglR0SfCreate(%s) failed rc=%Rrc\n",
436 sf_i->path->String.utf8, rc));
437 err = -RTErrConvertToErrno(rc);
438 goto fail2;
439 }
440# else
441 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
442 vrc = VbglR0SfHostReqCreate(sf_g->map.root, &pReq->Create);
443 if (RT_SUCCESS(vrc)) {
444 hHostFile = pCreateParms->Handle;
445 } else {
446 err = -RTErrConvertToErrno(vrc);
447 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
448 goto fail2;
449 }
450# endif
451 if (pCreateParms->Result != SHFL_FILE_EXISTS) {
452 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
453 err = -ENOENT;
454 goto fail1;
455 }
456
457 /* Setting the file size and setting the other attributes has to be
458 * handled separately, see implementation of vbsfSetFSInfo() in
459 * vbsf.cpp */
460 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME)) {
461# define mode_set(r) ((iattr->ia_mode & (S_##r)) ? RTFS_UNIX_##r : 0)
462
463 RT_ZERO(*pInfo);
464 if (iattr->ia_valid & ATTR_MODE) {
465 pInfo->Attr.fMode = mode_set(IRUSR);
466 pInfo->Attr.fMode |= mode_set(IWUSR);
467 pInfo->Attr.fMode |= mode_set(IXUSR);
468 pInfo->Attr.fMode |= mode_set(IRGRP);
469 pInfo->Attr.fMode |= mode_set(IWGRP);
470 pInfo->Attr.fMode |= mode_set(IXGRP);
471 pInfo->Attr.fMode |= mode_set(IROTH);
472 pInfo->Attr.fMode |= mode_set(IWOTH);
473 pInfo->Attr.fMode |= mode_set(IXOTH);
474
475 if (iattr->ia_mode & S_IFDIR)
476 pInfo->Attr.fMode |= RTFS_TYPE_DIRECTORY;
477 else
478 pInfo->Attr.fMode |= RTFS_TYPE_FILE;
479 }
480
481 if (iattr->ia_valid & ATTR_ATIME)
482 sf_timespec_from_ftime(&pInfo->AccessTime,
483 &iattr->ia_atime);
484 if (iattr->ia_valid & ATTR_MTIME)
485 sf_timespec_from_ftime(&pInfo->ModificationTime,
486 &iattr->ia_mtime);
487 /* ignore ctime (inode change time) as it can't be set from userland anyway */
488
489# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
490 cbBuffer = sizeof(*pInfo);
491 rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, pCreateParms->Handle,
492 SHFL_INFO_SET | SHFL_INFO_FILE, &cbBuffer,
493 (PSHFLDIRINFO)pInfo);
494 if (RT_FAILURE(rc)) {
495 LogFunc(("VbglR0SfFsInfo(%s, FILE) failed rc=%Rrc\n",
496 sf_i->path->String.utf8, rc));
497 err = -RTErrConvertToErrno(rc);
498 goto fail1;
499 }
500# else
501 vrc = VbglR0SfHostReqSetObjInfo(sf_g->map.root, &pReq->Info, hHostFile);
502 if (RT_FAILURE(vrc)) {
503 err = -RTErrConvertToErrno(vrc);
504 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
505 goto fail1;
506 }
507# endif
508 }
509
510 if (iattr->ia_valid & ATTR_SIZE) {
511# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
512 RT_ZERO(*pInfo);
513 pInfo->cbObject = iattr->ia_size;
514 cbBuffer = sizeof(*pInfo);
515 rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, pCreateParms->Handle,
516 SHFL_INFO_SET | SHFL_INFO_SIZE, &cbBuffer,
517 (PSHFLDIRINFO)pInfo);
518 if (RT_FAILURE(rc)) {
519 LogFunc(("VbglR0SfFsInfo(%s, SIZE) failed rc=%Rrc\n",
520 sf_i->path->String.utf8, rc));
521 err = -RTErrConvertToErrno(rc);
522 goto fail1;
523 }
524# else
525 vrc = VbglR0SfHostReqSetFileSize(sf_g->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
526 /** @todo Implement fallback if host is < 6.0? */
527 if (RT_FAILURE(vrc)) {
528 err = -RTErrConvertToErrno(vrc);
529 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc err=%d\n",
530 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, err));
531 goto fail1;
532 }
533# endif
534 }
535
536# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
537 rc = VbglR0SfClose(&client_handle, &sf_g->map, pCreateParms->Handle);
538 if (RT_FAILURE(rc))
539 LogFunc(("VbglR0SfClose(%s) failed rc=%Rrc\n",
540 sf_i->path->String.utf8, rc));
541# else
542 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
543 if (RT_FAILURE(vrc))
544 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
545 VbglR0PhysHeapFree(pReq);
546# endif
547
548 /** @todo r=bird: I guess we're calling revalidate here to update the inode
549 * info. However, due to the TTL optimization this is not guarenteed to happen.
550 *
551 * Also, we already have accurate stat information on the file, either from the
552 * SHFL_FN_CREATE call or from SHFL_FN_INFORMATION, so there is no need to do a
553 * slow stat()-like operation to retrieve the information again.
554 *
555 * What's more, given that the SHFL_FN_CREATE call succeeded, we know that the
556 * dentry and all its parent entries are valid and could touch their timestamps
557 * extending their TTL (CIFS does that). */
558 return sf_inode_revalidate(dentry);
559
560 fail1:
561# ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
562 rc = VbglR0SfClose(&client_handle, &sf_g->map, pCreateParms->Handle);
563 if (RT_FAILURE(rc))
564 LogFunc(("VbglR0SfClose(%s) failed rc=%Rrc\n",
565 sf_i->path->String.utf8, rc));
566# else
567 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
568 if (RT_FAILURE(vrc))
569 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc; err=%d\n", sf_i->path->String.utf8, hHostFile, vrc, err));
570# endif
571
572 fail2:
573# ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
574 VbglR0PhysHeapFree(pReq);
575# endif
576 return err;
577}
578
579#endif /* >= 2.6.0 */
580
581static int sf_make_path(const char *caller, struct sf_inode_info *sf_i,
582 const char *d_name, size_t d_len, SHFLSTRING ** result)
583{
584 size_t path_len, shflstring_len;
585 SHFLSTRING *tmp;
586 uint16_t p_len;
587 uint8_t *p_name;
588 int fRoot = 0;
589
590 TRACE();
591 p_len = sf_i->path->u16Length;
592 p_name = sf_i->path->String.utf8;
593
594 if (p_len == 1 && *p_name == '/') {
595 path_len = d_len + 1;
596 fRoot = 1;
597 } else {
598 /* lengths of constituents plus terminating zero plus slash */
599 path_len = p_len + d_len + 2;
600 if (path_len > 0xffff) {
601 LogFunc(("path too long. caller=%s, path_len=%zu\n",
602 caller, path_len));
603 return -ENAMETOOLONG;
604 }
605 }
606
607 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
608 tmp = kmalloc(shflstring_len, GFP_KERNEL);
609 if (!tmp) {
610 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
611 return -ENOMEM;
612 }
613 tmp->u16Length = path_len - 1;
614 tmp->u16Size = path_len;
615
616 if (fRoot)
617 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
618 else {
619 memcpy(&tmp->String.utf8[0], p_name, p_len);
620 tmp->String.utf8[p_len] = '/';
621 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
622 tmp->String.utf8[p_len + 1 + d_len] = '\0';
623 }
624
625 *result = tmp;
626 return 0;
627}
628
629/**
630 * [dentry] contains string encoded in coding system that corresponds
631 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
632 * [sf_make_path] which will allocate SHFLSTRING and fill it in
633 */
634int sf_path_from_dentry(const char *caller, struct sf_glob_info *sf_g,
635 struct sf_inode_info *sf_i, struct dentry *dentry,
636 SHFLSTRING ** result)
637{
638 int err;
639 const char *d_name;
640 size_t d_len;
641 const char *name;
642 size_t len = 0;
643
644 TRACE();
645 d_name = dentry->d_name.name;
646 d_len = dentry->d_name.len;
647
648 if (sf_g->nls) {
649 size_t in_len, i, out_bound_len;
650 const char *in;
651 char *out;
652
653 in = d_name;
654 in_len = d_len;
655
656 out_bound_len = PATH_MAX;
657 out = kmalloc(out_bound_len, GFP_KERNEL);
658 name = out;
659
660 for (i = 0; i < d_len; ++i) {
661 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
662 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
663 linux_wchar_t uni;
664 int nb;
665
666 nb = sf_g->nls->char2uni(in, in_len, &uni);
667 if (nb < 0) {
668 LogFunc(("nls->char2uni failed %x %d\n",
669 *in, in_len));
670 err = -EINVAL;
671 goto fail1;
672 }
673 in_len -= nb;
674 in += nb;
675
676#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
677 nb = utf32_to_utf8(uni, out, out_bound_len);
678#else
679 nb = utf8_wctomb(out, uni, out_bound_len);
680#endif
681 if (nb < 0) {
682 LogFunc(("nls->uni2char failed %x %d\n",
683 uni, out_bound_len));
684 err = -EINVAL;
685 goto fail1;
686 }
687 out_bound_len -= nb;
688 out += nb;
689 len += nb;
690 }
691 if (len >= PATH_MAX - 1) {
692 err = -ENAMETOOLONG;
693 goto fail1;
694 }
695
696 LogFunc(("result(%d) = %.*s\n", len, len, name));
697 *out = 0;
698 } else {
699 name = d_name;
700 len = d_len;
701 }
702
703 err = sf_make_path(caller, sf_i, name, len, result);
704 if (name != d_name)
705 kfree(name);
706
707 return err;
708
709 fail1:
710 kfree(name);
711 return err;
712}
713
714int sf_nlscpy(struct sf_glob_info *sf_g,
715 char *name, size_t name_bound_len,
716 const unsigned char *utf8_name, size_t utf8_len)
717{
718 if (sf_g->nls) {
719 const char *in;
720 char *out;
721 size_t out_len;
722 size_t out_bound_len;
723 size_t in_bound_len;
724
725 in = utf8_name;
726 in_bound_len = utf8_len;
727
728 out = name;
729 out_len = 0;
730 out_bound_len = name_bound_len;
731
732 while (in_bound_len) {
733 int nb;
734#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
735 unicode_t uni;
736
737 nb = utf8_to_utf32(in, in_bound_len, &uni);
738#else
739 linux_wchar_t uni;
740
741 nb = utf8_mbtowc(&uni, in, in_bound_len);
742#endif
743 if (nb < 0) {
744 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n",
745 (const char *)utf8_name, *in,
746 in_bound_len));
747 return -EINVAL;
748 }
749 in += nb;
750 in_bound_len -= nb;
751
752 nb = sf_g->nls->uni2char(uni, out, out_bound_len);
753 if (nb < 0) {
754 LogFunc(("nls->uni2char failed(%s) %x:%d\n",
755 utf8_name, uni, out_bound_len));
756 return nb;
757 }
758 out += nb;
759 out_bound_len -= nb;
760 out_len += nb;
761 }
762
763 *out = 0;
764 } else {
765 if (utf8_len + 1 > name_bound_len)
766 return -ENAMETOOLONG;
767
768 memcpy(name, utf8_name, utf8_len + 1);
769 }
770 return 0;
771}
772
773static struct sf_dir_buf *sf_dir_buf_alloc(void)
774{
775 struct sf_dir_buf *b;
776
777 TRACE();
778 b = kmalloc(sizeof(*b), GFP_KERNEL);
779 if (!b) {
780 LogRelFunc(("could not alloc directory buffer\n"));
781 return NULL;
782 }
783 b->buf = kmalloc(DIR_BUFFER_SIZE, GFP_KERNEL);
784 if (!b->buf) {
785 kfree(b);
786 LogRelFunc(("could not alloc directory buffer storage\n"));
787 return NULL;
788 }
789
790 INIT_LIST_HEAD(&b->head);
791 b->cEntries = 0;
792 b->cbUsed = 0;
793 b->cbFree = DIR_BUFFER_SIZE;
794 return b;
795}
796
797static void sf_dir_buf_free(struct sf_dir_buf *b)
798{
799 BUG_ON(!b || !b->buf);
800
801 TRACE();
802 list_del(&b->head);
803 kfree(b->buf);
804 kfree(b);
805}
806
807/**
808 * Free the directory buffer.
809 */
810void sf_dir_info_free(struct sf_dir_info *p)
811{
812 struct list_head *list, *pos, *tmp;
813
814 TRACE();
815 list = &p->info_list;
816 list_for_each_safe(pos, tmp, list) {
817 struct sf_dir_buf *b;
818
819 b = list_entry(pos, struct sf_dir_buf, head);
820 sf_dir_buf_free(b);
821 }
822 kfree(p);
823}
824
825/**
826 * Empty (but not free) the directory buffer.
827 */
828void sf_dir_info_empty(struct sf_dir_info *p)
829{
830 struct list_head *list, *pos, *tmp;
831 TRACE();
832 list = &p->info_list;
833 list_for_each_safe(pos, tmp, list) {
834 struct sf_dir_buf *b;
835 b = list_entry(pos, struct sf_dir_buf, head);
836 b->cEntries = 0;
837 b->cbUsed = 0;
838 b->cbFree = DIR_BUFFER_SIZE;
839 }
840}
841
842/**
843 * Create a new directory buffer descriptor.
844 */
845struct sf_dir_info *sf_dir_info_alloc(void)
846{
847 struct sf_dir_info *p;
848
849 TRACE();
850 p = kmalloc(sizeof(*p), GFP_KERNEL);
851 if (!p) {
852 LogRelFunc(("could not alloc directory info\n"));
853 return NULL;
854 }
855
856 INIT_LIST_HEAD(&p->info_list);
857 return p;
858}
859
860/**
861 * Search for an empty directory content buffer.
862 */
863static struct sf_dir_buf *sf_get_empty_dir_buf(struct sf_dir_info *sf_d)
864{
865 struct list_head *list, *pos;
866
867 list = &sf_d->info_list;
868 list_for_each(pos, list) {
869 struct sf_dir_buf *b;
870
871 b = list_entry(pos, struct sf_dir_buf, head);
872 if (!b)
873 return NULL;
874 else {
875 if (b->cbUsed == 0)
876 return b;
877 }
878 }
879
880 return NULL;
881}
882
883/** @todo r=bird: Why on earth do we read in the entire directory??? This
884 * cannot be healthy for like big directory and such... */
885int sf_dir_read_all(struct sf_glob_info *sf_g, struct sf_inode_info *sf_i,
886 struct sf_dir_info *sf_d, SHFLHANDLE handle)
887{
888 int err;
889 SHFLSTRING *mask;
890#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
891 VBOXSFLISTDIRREQ *pReq = NULL;
892#endif
893
894 TRACE();
895 err = sf_make_path(__func__, sf_i, "*", 1, &mask);
896 if (err)
897 goto fail0;
898#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
899 pReq = (VBOXSFLISTDIRREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
900 if (!pReq)
901 goto fail1;
902#endif
903
904 for (;;) {
905 int rc;
906 struct sf_dir_buf *b;
907#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
908 void *buf;
909 uint32_t cbSize;
910 uint32_t cEntries;
911#endif
912
913 b = sf_get_empty_dir_buf(sf_d);
914 if (!b) {
915 b = sf_dir_buf_alloc();
916 if (!b) {
917 err = -ENOMEM;
918 LogRelFunc(("could not alloc directory buffer\n"));
919 goto fail2;
920 }
921 list_add(&b->head, &sf_d->info_list);
922 }
923
924#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
925 buf = b->buf;
926 cbSize = b->cbFree;
927
928 rc = VbglR0SfDirInfo(&client_handle, &sf_g->map, handle, mask,
929 0, 0, &cbSize, buf, &cEntries);
930 switch (rc) {
931 case VINF_SUCCESS:
932 RT_FALL_THRU();
933 case VERR_NO_MORE_FILES:
934 break;
935 case VERR_NO_TRANSLATION:
936 LogFunc(("host could not translate entry\n"));
937 /* XXX */
938 break;
939 default:
940 err = -RTErrConvertToErrno(rc);
941 LogFunc(("VbglR0SfDirInfo failed rc=%Rrc\n", rc));
942 goto fail2;
943 }
944
945 b->cEntries += cEntries;
946 b->cbFree -= cbSize;
947 b->cbUsed += cbSize;
948
949 if (RT_FAILURE(rc))
950 break;
951#else
952 rc = VbglR0SfHostReqListDirContig2x(sf_g->map.root, pReq, handle, mask, virt_to_phys(mask),
953 0 /*fFlags*/, b->buf, virt_to_phys(b->buf), b->cbFree);
954 if (RT_SUCCESS(rc)) {
955 b->cEntries += pReq->Parms.c32Entries.u.value32;
956 b->cbFree -= pReq->Parms.cb32Buffer.u.value32;
957 b->cbUsed += pReq->Parms.cb32Buffer.u.value32;
958 } else if (rc == VERR_NO_MORE_FILES) {
959 break;
960 } else {
961 err = -RTErrConvertToErrno(rc);
962 LogFunc(("VbglR0SfHostReqListDirContig2x failed rc=%Rrc err=%d\n", rc, err));
963 goto fail2;
964 }
965#endif
966 }
967 err = 0;
968
969 fail2:
970#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
971 VbglR0PhysHeapFree(pReq);
972#endif
973 fail1:
974 kfree(mask);
975
976 fail0:
977 return err;
978}
979
980int sf_get_volume_info(struct super_block *sb, STRUCT_STATFS * stat)
981{
982 struct sf_glob_info *sf_g;
983#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
984 SHFLVOLINFO SHFLVolumeInfo;
985 SHFLVOLINFO *pVolInfo = &SHFLVolumeInfo;
986 uint32_t cbBuffer;
987#else
988 VBOXSFVOLINFOREQ *pReq;
989 SHFLVOLINFO *pVolInfo;
990#endif
991 int rc;
992
993 sf_g = GET_GLOB_INFO(sb);
994#ifdef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
995 cbBuffer = sizeof(SHFLVolumeInfo);
996 rc = VbglR0SfFsInfo(&client_handle, &sf_g->map, 0,
997 SHFL_INFO_GET | SHFL_INFO_VOLUME, &cbBuffer,
998 (PSHFLDIRINFO) & SHFLVolumeInfo);
999#else
1000 pReq = VbglR0PhysHeapAlloc(sizeof(*pReq));
1001 if (pReq) {
1002 pVolInfo = &pReq->VolInfo;
1003 rc = VbglR0SfHostReqQueryVolInfo(sf_g->map.root, pReq, SHFL_HANDLE_ROOT);
1004#endif
1005 if (RT_SUCCESS(rc)) {
1006 stat->f_type = NFS_SUPER_MAGIC; /* XXX vboxsf type? */
1007 stat->f_bsize = pVolInfo->ulBytesPerAllocationUnit;
1008#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 73)
1009 stat->f_frsize = pVolInfo->ulBytesPerAllocationUnit;
1010#endif
1011 stat->f_blocks = pVolInfo->ullTotalAllocationBytes
1012 / pVolInfo->ulBytesPerAllocationUnit;
1013 stat->f_bfree = pVolInfo->ullAvailableAllocationBytes
1014 / pVolInfo->ulBytesPerAllocationUnit;
1015 stat->f_bavail = pVolInfo->ullAvailableAllocationBytes
1016 / pVolInfo->ulBytesPerAllocationUnit;
1017 stat->f_files = 1000;
1018 stat->f_ffree = 1000; /* don't return 0 here since the guest may think
1019 * that it is not possible to create any more files */
1020 stat->f_fsid.val[0] = 0;
1021 stat->f_fsid.val[1] = 0;
1022 stat->f_namelen = 255;
1023#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
1024 stat->f_flags = 0; /* not valid */
1025#endif
1026 RT_ZERO(stat->f_spare);
1027 rc = 0;
1028 } else
1029 rc = -RTErrConvertToErrno(rc);
1030#ifndef VBOXSF_USE_DEPRECATED_VBGL_INTERFACE
1031 VbglR0PhysHeapFree(pReq);
1032 } else
1033 rc = -ENOMEM;
1034#endif
1035 return rc;
1036}
1037
1038struct dentry_operations sf_dentry_ops = {
1039 .d_revalidate = sf_dentry_revalidate
1040};
1041
1042int sf_init_backing_dev(struct sf_glob_info *sf_g)
1043{
1044 int rc = 0;
1045#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
1046 /* Each new shared folder map gets a new uint64_t identifier,
1047 * allocated in sequence. We ASSUME the sequence will not wrap. */
1048 static uint64_t s_u64Sequence = 0;
1049 uint64_t u64CurrentSequence = ASMAtomicIncU64(&s_u64Sequence);
1050
1051 sf_g->bdi.ra_pages = 0; /* No readahead */
1052# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 12)
1053 sf_g->bdi.capabilities = BDI_CAP_MAP_DIRECT /* MAP_SHARED */
1054 | BDI_CAP_MAP_COPY /* MAP_PRIVATE */
1055 | BDI_CAP_READ_MAP /* can be mapped for reading */
1056 | BDI_CAP_WRITE_MAP /* can be mapped for writing */
1057 | BDI_CAP_EXEC_MAP; /* can be mapped for execution */
1058# endif /* >= 2.6.12 */
1059# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1060 rc = bdi_init(&sf_g->bdi);
1061# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
1062 if (!rc)
1063 rc = bdi_register(&sf_g->bdi, NULL, "vboxsf-%llu",
1064 (unsigned long long)u64CurrentSequence);
1065# endif /* >= 2.6.26 */
1066# endif /* >= 2.6.24 */
1067#endif /* >= 2.6.0 && <= 3.19.0 */
1068 return rc;
1069}
1070
1071void sf_done_backing_dev(struct sf_glob_info *sf_g)
1072{
1073#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) && LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
1074 bdi_destroy(&sf_g->bdi); /* includes bdi_unregister() */
1075#endif
1076}
1077
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