VirtualBox

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

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

linux/vboxsf: Workaround for kernel_read/write calls (get_user_pages fails), fixing issue with sporadic ENOEXEC from execve(). The workaround requires rtR0MemObjLinuxVirtToPage() from memobj-r0drv-linux.c. Various cleanups and adjustments. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.4 KB
Line 
1/* $Id: utils.c 77549 2019-03-04 10:00:34Z 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/vfs.h>
37
38
39int vbsf_nlscpy(struct vbsf_super_info *sf_g, char *name, size_t name_bound_len, const unsigned char *utf8_name, size_t utf8_len)
40{
41 if (sf_g->nls) {
42 const char *in;
43 char *out;
44 size_t out_len;
45 size_t out_bound_len;
46 size_t in_bound_len;
47
48 in = utf8_name;
49 in_bound_len = utf8_len;
50
51 out = name;
52 out_len = 0;
53 out_bound_len = name_bound_len;
54
55 while (in_bound_len) {
56 int nb;
57#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
58 unicode_t uni;
59
60 nb = utf8_to_utf32(in, in_bound_len, &uni);
61#else
62 linux_wchar_t uni;
63
64 nb = utf8_mbtowc(&uni, in, in_bound_len);
65#endif
66 if (nb < 0) {
67 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n", (const char *)utf8_name, *in, in_bound_len));
68 return -EINVAL;
69 }
70 in += nb;
71 in_bound_len -= nb;
72
73 nb = sf_g->nls->uni2char(uni, out, out_bound_len);
74 if (nb < 0) {
75 LogFunc(("nls->uni2char failed(%s) %x:%d\n", utf8_name, uni, out_bound_len));
76 return nb;
77 }
78 out += nb;
79 out_bound_len -= nb;
80 out_len += nb;
81 }
82
83 *out = 0;
84 } else {
85 if (utf8_len + 1 > name_bound_len)
86 return -ENAMETOOLONG;
87
88 memcpy(name, utf8_name, utf8_len + 1);
89 }
90 return 0;
91}
92
93
94/**
95 * Convert from VBox to linux time.
96 */
97#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
98DECLINLINE(void) vbsf_time_to_linux(time_t *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
99{
100 int64_t t = RTTimeSpecGetNano(pVBoxSrc);
101 do_div(t, RT_NS_1SEC);
102 *pLinuxDst = t;
103}
104#else /* >= 2.6.0 */
105# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
106DECLINLINE(void) vbsf_time_to_linux(struct timespec *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
107# else
108DECLINLINE(void) vbsf_time_to_linux(struct timespec64 *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
109# endif
110{
111 int64_t t = RTTimeSpecGetNano(pVBoxSrc);
112 pLinuxDst->tv_nsec = do_div(t, RT_NS_1SEC);
113 pLinuxDst->tv_sec = t;
114}
115#endif /* >= 2.6.0 */
116
117
118/**
119 * Convert from linux to VBox time.
120 */
121#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
122DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, time_t *pLinuxSrc)
123{
124 RTTimeSpecSetNano(pVBoxDst, RT_NS_1SEC_64 * *pLinuxSrc);
125}
126#else /* >= 2.6.0 */
127# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
128DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, struct timespec const *pLinuxSrc)
129# else
130DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, struct timespec64 const *pLinuxSrc)
131# endif
132{
133 RTTimeSpecSetNano(pVBoxDst, pLinuxSrc->tv_nsec + pLinuxSrc->tv_sec * (int64_t)RT_NS_1SEC);
134}
135#endif /* >= 2.6.0 */
136
137
138/**
139 * Converts VBox access permissions to Linux ones (mode & 0777).
140 *
141 * @note Currently identical.
142 * @sa sf_access_permissions_to_vbox
143 */
144DECLINLINE(int) sf_access_permissions_to_linux(uint32_t fAttr)
145{
146 /* Access bits should be the same: */
147 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
148 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
149 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
150 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
151 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
152 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
153 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
154 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
155 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
156
157 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
158}
159
160
161/**
162 * Produce the Linux mode mask, given VBox, mount options and file type.
163 */
164DECLINLINE(int) sf_file_mode_to_linux(uint32_t fVBoxMode, int fFixedMode, int fClearMask, int fType)
165{
166 int fLnxMode = sf_access_permissions_to_linux(fVBoxMode);
167 if (fFixedMode != ~0)
168 fLnxMode = fFixedMode & 0777;
169 fLnxMode &= ~fClearMask;
170 fLnxMode |= fType;
171 return fLnxMode;
172}
173
174
175/**
176 * Initializes the @a inode attributes based on @a pObjInfo and @a sf_g options.
177 */
178void vbsf_init_inode(struct inode *inode, struct vbsf_inode_info *sf_i, PSHFLFSOBJINFO pObjInfo, struct vbsf_super_info *sf_g)
179{
180 PCSHFLFSOBJATTR pAttr = &pObjInfo->Attr;
181
182 TRACE();
183
184 sf_i->ts_up_to_date = jiffies;
185 sf_i->force_restat = 0;
186
187#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
188 inode->i_mapping->a_ops = &vbsf_reg_aops;
189# if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
190 inode->i_mapping->backing_dev_info = &sf_g->bdi; /* This is needed for mmap. */
191# endif
192#endif
193 if (RTFS_IS_DIRECTORY(pAttr->fMode)) {
194 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, sf_g->dmode, sf_g->dmask, S_IFDIR);
195 inode->i_op = &vbsf_dir_iops;
196 inode->i_fop = &vbsf_dir_fops;
197
198 /* XXX: this probably should be set to the number of entries
199 in the directory plus two (. ..) */
200 set_nlink(inode, 1);
201 }
202#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
203 else if (RTFS_IS_SYMLINK(pAttr->fMode)) {
204 /** @todo r=bird: Aren't System V symlinks w/o any mode mask? IIRC there is
205 * no lchmod on Linux. */
206 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFLNK);
207 inode->i_op = &vbsf_lnk_iops;
208 set_nlink(inode, 1);
209 }
210#endif
211 else {
212 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFREG);
213 inode->i_op = &vbsf_reg_iops;
214 inode->i_fop = &vbsf_reg_fops;
215 set_nlink(inode, 1);
216 }
217
218#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
219 inode->i_uid = make_kuid(current_user_ns(), sf_g->uid);
220 inode->i_gid = make_kgid(current_user_ns(), sf_g->gid);
221#else
222 inode->i_uid = sf_g->uid;
223 inode->i_gid = sf_g->gid;
224#endif
225
226 inode->i_size = pObjInfo->cbObject;
227#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
228 inode->i_blksize = 4096;
229#endif
230#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
231 inode->i_blkbits = 12;
232#endif
233 /* i_blocks always in units of 512 bytes! */
234 inode->i_blocks = (pObjInfo->cbAllocated + 511) / 512;
235
236 vbsf_time_to_linux(&inode->i_atime, &pObjInfo->AccessTime);
237 vbsf_time_to_linux(&inode->i_ctime, &pObjInfo->ChangeTime);
238 vbsf_time_to_linux(&inode->i_mtime, &pObjInfo->ModificationTime);
239 sf_i->BirthTime = pObjInfo->BirthTime;
240}
241
242
243/**
244 * Update the inode with new object info from the host.
245 *
246 * Called by sf_inode_revalidate() and sf_inode_revalidate_with_handle(), the
247 * inode is probably locked...
248 *
249 * @todo sort out the inode locking situation.
250 */
251void vbsf_update_inode(struct inode *pInode, struct vbsf_inode_info *pInodeInfo, PSHFLFSOBJINFO pObjInfo,
252 struct vbsf_super_info *sf_g, bool fInodeLocked)
253{
254 PCSHFLFSOBJATTR pAttr = &pObjInfo->Attr;
255 int fMode;
256
257 TRACE();
258
259#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
260 if (!fInodeLocked)
261 inode_lock(pInode);
262#endif
263
264 /*
265 * Calc new mode mask and update it if it changed.
266 */
267 if (RTFS_IS_DIRECTORY(pAttr->fMode))
268 fMode = sf_file_mode_to_linux(pAttr->fMode, sf_g->dmode, sf_g->dmask, S_IFDIR);
269#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
270 else if (RTFS_IS_SYMLINK(pAttr->fMode))
271 /** @todo r=bird: Aren't System V symlinks w/o any mode mask? IIRC there is
272 * no lchmod on Linux. */
273 fMode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFLNK);
274#endif
275 else
276 fMode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFREG);
277
278 if (fMode == pInode->i_mode) {
279 /* likely */
280 } else {
281 if ((fMode & S_IFMT) == (pInode->i_mode & S_IFMT))
282 pInode->i_mode = fMode;
283 else {
284 SFLOGFLOW(("vbsf_update_inode: Changed from %o to %o (%s)\n",
285 pInode->i_mode & S_IFMT, fMode & S_IFMT, pInodeInfo->path->String.ach));
286 /** @todo we probably need to be more drastic... */
287 vbsf_init_inode(pInode, pInodeInfo, pObjInfo, sf_g);
288
289#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
290 if (!fInodeLocked)
291 inode_unlock(pInode);
292#endif
293 return;
294 }
295 }
296
297 /*
298 * Update the sizes.
299 * Note! i_blocks is always in units of 512 bytes!
300 */
301 pInode->i_blocks = (pObjInfo->cbAllocated + 511) / 512;
302 i_size_write(pInode, pObjInfo->cbObject);
303
304 /*
305 * Update the timestamps.
306 */
307 vbsf_time_to_linux(&pInode->i_atime, &pObjInfo->AccessTime);
308 vbsf_time_to_linux(&pInode->i_ctime, &pObjInfo->ChangeTime);
309 vbsf_time_to_linux(&pInode->i_mtime, &pObjInfo->ModificationTime);
310 pInodeInfo->BirthTime = pObjInfo->BirthTime;
311
312 /*
313 * Mark it as up to date.
314 */
315 pInodeInfo->ts_up_to_date = jiffies;
316 pInodeInfo->force_restat = 0;
317
318#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
319 if (!fInodeLocked)
320 inode_unlock(pInode);
321#endif
322}
323
324
325/** @note Currently only used for the root directory during (re-)mount. */
326int vbsf_stat(const char *caller, struct vbsf_super_info *sf_g, SHFLSTRING *path, PSHFLFSOBJINFO result, int ok_to_fail)
327{
328 int rc;
329 VBOXSFCREATEREQ *pReq;
330 NOREF(caller);
331
332 TRACE();
333
334 pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + path->u16Size);
335 if (pReq) {
336 RT_ZERO(*pReq);
337 memcpy(&pReq->StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
338 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
339 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
340
341 LogFunc(("Calling VbglR0SfHostReqCreate on %s\n", path->String.utf8));
342 rc = VbglR0SfHostReqCreate(sf_g->map.root, pReq);
343 if (RT_SUCCESS(rc)) {
344 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
345 *result = pReq->CreateParms.Info;
346 rc = 0;
347 } else {
348 if (!ok_to_fail)
349 LogFunc(("VbglR0SfHostReqCreate on %s: file does not exist: %d (caller=%s)\n",
350 path->String.utf8, pReq->CreateParms.Result, caller));
351 rc = -ENOENT;
352 }
353 } else if (rc == VERR_INVALID_NAME) {
354 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
355 } else {
356 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc (caller=%s)\n", path->String.utf8, rc, caller));
357 rc = -EPROTO;
358 }
359 VbglR0PhysHeapFree(pReq);
360 }
361 else
362 rc = -ENOMEM;
363 return rc;
364}
365
366
367/**
368 * Revalidate an inode, inner worker.
369 *
370 * @sa sf_inode_revalidate()
371 */
372int vbsf_inode_revalidate_worker(struct dentry *dentry, bool fForced)
373{
374 int rc;
375 struct inode *pInode = dentry ? dentry->d_inode : NULL;
376 if (pInode) {
377 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
378 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(pInode->i_sb);
379 AssertReturn(sf_i, -EINVAL);
380 AssertReturn(sf_g, -EINVAL);
381
382 /*
383 * Can we get away without any action here?
384 */
385 if ( !fForced
386 && !sf_i->force_restat
387 && jiffies - sf_i->ts_up_to_date < sf_g->ttl)
388 rc = 0;
389 else {
390 /*
391 * No, we have to query the file info from the host.
392 * Try get a handle we can query, any kind of handle will do here.
393 */
394 struct vbsf_handle *pHandle = vbsf_handle_find(sf_i, 0, 0);
395 if (pHandle) {
396 /* Query thru pHandle. */
397 VBOXSFOBJINFOREQ *pReq = (VBOXSFOBJINFOREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
398 if (pReq) {
399 RT_ZERO(*pReq);
400 rc = VbglR0SfHostReqQueryObjInfo(sf_g->map.root, pReq, pHandle->hHost);
401 if (RT_SUCCESS(rc)) {
402 /*
403 * Reset the TTL and copy the info over into the inode structure.
404 */
405 vbsf_update_inode(pInode, sf_i, &pReq->ObjInfo, sf_g, true /*fInodeLocked??*/);
406 } else if (rc == VERR_INVALID_HANDLE) {
407 rc = -ENOENT; /* Restore.*/
408 } else {
409 LogFunc(("VbglR0SfHostReqQueryObjInfo failed on %#RX64: %Rrc\n", pHandle->hHost, rc));
410 rc = -RTErrConvertToErrno(rc);
411 }
412 VbglR0PhysHeapFree(pReq);
413 } else
414 rc = -ENOMEM;
415 vbsf_handle_release(pHandle, sf_g, "vbsf_inode_revalidate_worker");
416
417 } else {
418 /* Query via path. */
419 SHFLSTRING *pPath = sf_i->path;
420 VBOXSFCREATEREQ *pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + pPath->u16Size);
421 if (pReq) {
422 RT_ZERO(*pReq);
423 memcpy(&pReq->StrPath, pPath, SHFLSTRING_HEADER_SIZE + pPath->u16Size);
424 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
425 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
426
427 rc = VbglR0SfHostReqCreate(sf_g->map.root, pReq);
428 if (RT_SUCCESS(rc)) {
429 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
430 /*
431 * Reset the TTL and copy the info over into the inode structure.
432 */
433 vbsf_update_inode(pInode, sf_i, &pReq->CreateParms.Info, sf_g, true /*fInodeLocked??*/);
434 rc = 0;
435 } else {
436 rc = -ENOENT;
437 }
438 } else if (rc == VERR_INVALID_NAME) {
439 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
440 } else {
441 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc\n", pPath->String.ach, rc));
442 rc = -EPROTO;
443 }
444 VbglR0PhysHeapFree(pReq);
445 }
446 else
447 rc = -ENOMEM;
448 }
449 }
450 } else {
451 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, pInode));
452 rc = -EINVAL;
453 }
454 return rc;
455}
456
457
458/**
459 * Revalidate an inode.
460 *
461 * This is called directly as inode-op on 2.4, indirectly as dir-op
462 * vbsf_dentry_revalidate() on 2.4/2.6. The job is to find out whether
463 * dentry/inode is still valid. The test fails if @a dentry does not have an
464 * inode or vbsf_stat() is unsuccessful, otherwise we return success and update
465 * inode attributes.
466 */
467int vbsf_inode_revalidate(struct dentry *dentry)
468{
469 return vbsf_inode_revalidate_worker(dentry, false /*fForced*/);
470}
471
472
473/**
474 * Similar to sf_inode_revalidate, but uses associated host file handle as that
475 * is quite a bit faster.
476 */
477int vbsf_inode_revalidate_with_handle(struct dentry *dentry, SHFLHANDLE hHostFile, bool fForced, bool fInodeLocked)
478{
479 int err;
480 struct inode *pInode = dentry ? dentry->d_inode : NULL;
481 if (!pInode) {
482 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, pInode));
483 err = -EINVAL;
484 } else {
485 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
486 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(pInode->i_sb);
487 AssertReturn(sf_i, -EINVAL);
488 AssertReturn(sf_g, -EINVAL);
489
490 /*
491 * Can we get away without any action here?
492 */
493 if ( !fForced
494 && !sf_i->force_restat
495 && jiffies - sf_i->ts_up_to_date < sf_g->ttl)
496 err = 0;
497 else {
498 /*
499 * No, we have to query the file info from the host.
500 */
501 VBOXSFOBJINFOREQ *pReq = (VBOXSFOBJINFOREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
502 if (pReq) {
503 RT_ZERO(*pReq);
504 err = VbglR0SfHostReqQueryObjInfo(sf_g->map.root, pReq, hHostFile);
505 if (RT_SUCCESS(err)) {
506 /*
507 * Reset the TTL and copy the info over into the inode structure.
508 */
509 vbsf_update_inode(pInode, sf_i, &pReq->ObjInfo, sf_g, fInodeLocked);
510 } else {
511 LogFunc(("VbglR0SfHostReqQueryObjInfo failed on %#RX64: %Rrc\n", hHostFile, err));
512 err = -RTErrConvertToErrno(err);
513 }
514 VbglR0PhysHeapFree(pReq);
515 } else
516 err = -ENOMEM;
517 }
518 }
519 return err;
520}
521
522
523/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
524 effect) updates inode attributes for [dentry] (given that [dentry]
525 has inode at all) from these new attributes we derive [kstat] via
526 [generic_fillattr] */
527#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
528
529# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
530int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int flags)
531# else
532int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
533# endif
534{
535 int rc;
536# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
537 struct dentry *dentry = path->dentry;
538# endif
539
540# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
541 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p request_mask=%#x flags=%#x\n", dentry, request_mask, flags));
542# else
543 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p\n", dentry));
544# endif
545
546# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
547 /*
548 * With the introduction of statx() userland can control whether we
549 * update the inode information or not.
550 */
551 switch (flags & AT_STATX_SYNC_TYPE) {
552 default:
553 rc = vbsf_inode_revalidate_worker(dentry, false /*fForced*/);
554 break;
555
556 case AT_STATX_FORCE_SYNC:
557 rc = vbsf_inode_revalidate_worker(dentry, true /*fForced*/);
558 break;
559
560 case AT_STATX_DONT_SYNC:
561 rc = 0;
562 break;
563 }
564# else
565 rc = vbsf_inode_revalidate_worker(dentry, false /*fForced*/);
566# endif
567 if (rc == 0) {
568 /* Do generic filling in of info. */
569 generic_fillattr(dentry->d_inode, kstat);
570
571 /* Add birth time. */
572#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
573 if (dentry->d_inode) {
574 struct vbsf_inode_info *pInodeInfo = VBSF_GET_INODE_INFO(dentry->d_inode);
575 if (pInodeInfo) {
576 vbsf_time_to_linux(&kstat->btime, &pInodeInfo->BirthTime);
577 kstat->result_mask |= STATX_BTIME;
578 }
579 }
580#endif
581
582 /*
583 * FsPerf shows the following numbers for sequential file access against
584 * a tmpfs folder on an AMD 1950X host running debian buster/sid:
585 *
586 * block size = r128600 ----- r128755 -----
587 * reads reads writes
588 * 4096 KB = 2254 MB/s 4953 MB/s 3668 MB/s
589 * 2048 KB = 2368 MB/s 4908 MB/s 3541 MB/s
590 * 1024 KB = 2208 MB/s 4011 MB/s 3291 MB/s
591 * 512 KB = 1908 MB/s 3399 MB/s 2721 MB/s
592 * 256 KB = 1625 MB/s 2679 MB/s 2251 MB/s
593 * 128 KB = 1413 MB/s 1967 MB/s 1684 MB/s
594 * 64 KB = 1152 MB/s 1409 MB/s 1265 MB/s
595 * 32 KB = 726 MB/s 815 MB/s 783 MB/s
596 * 16 KB = 683 MB/s 475 MB/s
597 * 8 KB = 294 MB/s 286 MB/s
598 * 4 KB = 145 MB/s 156 MB/s 149 MB/s
599 *
600 */
601 if (S_ISREG(kstat->mode))
602 kstat->blksize = _1M;
603 else if (S_ISDIR(kstat->mode))
604 /** @todo this may need more tuning after we rewrite the directory handling. */
605 kstat->blksize = _16K;
606 }
607 return rc;
608}
609
610
611int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr)
612{
613 struct vbsf_super_info *sf_g;
614 struct vbsf_inode_info *sf_i;
615 union SetAttrReqs
616 {
617 VBOXSFCREATEREQ Create;
618 VBOXSFOBJINFOREQ Info;
619 VBOXSFSETFILESIZEREQ SetSize;
620 VBOXSFCLOSEREQ Close;
621 } *pReq;
622 size_t cbReq;
623 SHFLHANDLE hHostFile;
624 int vrc;
625 int err = 0;
626
627 TRACE();
628
629 sf_g = VBSF_GET_SUPER_INFO(dentry->d_inode->i_sb);
630 sf_i = VBSF_GET_INODE_INFO(dentry->d_inode);
631 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
632 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
633 if (!pReq) {
634 LogFunc(("Failed to allocate %#x byte request buffer!\n", cbReq));
635 return -ENOMEM;
636 }
637
638 RT_ZERO(pReq->Create.CreateParms);
639 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
640 pReq->Create.CreateParms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
641 | SHFL_CF_ACT_FAIL_IF_NEW
642 | SHFL_CF_ACCESS_ATTR_WRITE;
643
644 /* this is at least required for Posix hosts */
645 if (iattr->ia_valid & ATTR_SIZE)
646 pReq->Create.CreateParms.CreateFlags |= SHFL_CF_ACCESS_WRITE;
647
648 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
649 vrc = VbglR0SfHostReqCreate(sf_g->map.root, &pReq->Create);
650 if (RT_SUCCESS(vrc)) {
651 hHostFile = pReq->Create.CreateParms.Handle;
652 } else {
653 err = -RTErrConvertToErrno(vrc);
654 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
655 goto fail2;
656 }
657 if (pReq->Create.CreateParms.Result != SHFL_FILE_EXISTS) {
658 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
659 err = -ENOENT;
660 goto fail1;
661 }
662
663 /* Setting the file size and setting the other attributes has to be
664 * handled separately, see implementation of vbsfSetFSInfo() in
665 * vbsf.cpp */
666 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME)) {
667 RT_ZERO(pReq->Info.ObjInfo);
668
669 if (iattr->ia_valid & ATTR_MODE) {
670 pReq->Info.ObjInfo.Attr.fMode = sf_access_permissions_to_vbox(iattr->ia_mode);
671 if (iattr->ia_mode & S_IFDIR)
672 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_DIRECTORY;
673 else if (iattr->ia_mode & S_IFLNK)
674 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_SYMLINK;
675 else
676 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_FILE;
677 }
678
679 if (iattr->ia_valid & ATTR_ATIME)
680 vbsf_time_to_vbox(&pReq->Info.ObjInfo.AccessTime, &iattr->ia_atime);
681 if (iattr->ia_valid & ATTR_MTIME)
682 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ModificationTime, &iattr->ia_mtime);
683 /* ignore ctime (inode change time) as it can't be set from userland anyway */
684
685 vrc = VbglR0SfHostReqSetObjInfo(sf_g->map.root, &pReq->Info, hHostFile);
686 if (RT_FAILURE(vrc)) {
687 err = -RTErrConvertToErrno(vrc);
688 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
689 goto fail1;
690 }
691 }
692
693 if (iattr->ia_valid & ATTR_SIZE) {
694 vrc = VbglR0SfHostReqSetFileSize(sf_g->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
695 /** @todo Implement fallback if host is < 6.0? */
696 if (RT_FAILURE(vrc)) {
697 err = -RTErrConvertToErrno(vrc);
698 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc err=%d\n",
699 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, err));
700 goto fail1;
701 }
702 }
703
704 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
705 if (RT_FAILURE(vrc))
706 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
707 VbglR0PhysHeapFree(pReq);
708
709 /** @todo r=bird: I guess we're calling revalidate here to update the inode
710 * info. However, due to the TTL optimization this is not guarenteed to happen.
711 *
712 * Also, we already have accurate stat information on the file, either from the
713 * SHFL_FN_CREATE call or from SHFL_FN_INFORMATION, so there is no need to do a
714 * slow stat()-like operation to retrieve the information again.
715 *
716 * What's more, given that the SHFL_FN_CREATE call succeeded, we know that the
717 * dentry and all its parent entries are valid and could touch their timestamps
718 * extending their TTL (CIFS does that). */
719 return vbsf_inode_revalidate_worker(dentry, true /*fForced*/);
720
721 fail1:
722 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
723 if (RT_FAILURE(vrc))
724 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc; err=%d\n", sf_i->path->String.utf8, hHostFile, vrc, err));
725
726 fail2:
727 VbglR0PhysHeapFree(pReq);
728 return err;
729}
730
731#endif /* >= 2.6.0 */
732
733static int vbsf_make_path(const char *caller, struct vbsf_inode_info *sf_i,
734 const char *d_name, size_t d_len, SHFLSTRING **result)
735{
736 size_t path_len, shflstring_len;
737 SHFLSTRING *tmp;
738 uint16_t p_len;
739 uint8_t *p_name;
740 int fRoot = 0;
741
742 TRACE();
743 p_len = sf_i->path->u16Length;
744 p_name = sf_i->path->String.utf8;
745
746 if (p_len == 1 && *p_name == '/') {
747 path_len = d_len + 1;
748 fRoot = 1;
749 } else {
750 /* lengths of constituents plus terminating zero plus slash */
751 path_len = p_len + d_len + 2;
752 if (path_len > 0xffff) {
753 LogFunc(("path too long. caller=%s, path_len=%zu\n",
754 caller, path_len));
755 return -ENAMETOOLONG;
756 }
757 }
758
759 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
760 tmp = kmalloc(shflstring_len, GFP_KERNEL);
761 if (!tmp) {
762 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
763 return -ENOMEM;
764 }
765 tmp->u16Length = path_len - 1;
766 tmp->u16Size = path_len;
767
768 if (fRoot)
769 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
770 else {
771 memcpy(&tmp->String.utf8[0], p_name, p_len);
772 tmp->String.utf8[p_len] = '/';
773 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
774 tmp->String.utf8[p_len + 1 + d_len] = '\0';
775 }
776
777 *result = tmp;
778 return 0;
779}
780
781
782/**
783 * [dentry] contains string encoded in coding system that corresponds
784 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
785 * [vbsf_make_path] which will allocate SHFLSTRING and fill it in
786 */
787int vbsf_path_from_dentry(const char *caller, struct vbsf_super_info *sf_g, struct vbsf_inode_info *sf_i,
788 struct dentry *dentry, SHFLSTRING **result)
789{
790 int err;
791 const char *d_name;
792 size_t d_len;
793 const char *name;
794 size_t len = 0;
795
796 TRACE();
797 d_name = dentry->d_name.name;
798 d_len = dentry->d_name.len;
799
800 if (sf_g->nls) {
801 size_t in_len, i, out_bound_len;
802 const char *in;
803 char *out;
804
805 in = d_name;
806 in_len = d_len;
807
808 out_bound_len = PATH_MAX;
809 out = kmalloc(out_bound_len, GFP_KERNEL);
810 name = out;
811
812 for (i = 0; i < d_len; ++i) {
813 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
814 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
815 linux_wchar_t uni;
816 int nb;
817
818 nb = sf_g->nls->char2uni(in, in_len, &uni);
819 if (nb < 0) {
820 LogFunc(("nls->char2uni failed %x %d\n",
821 *in, in_len));
822 err = -EINVAL;
823 goto fail1;
824 }
825 in_len -= nb;
826 in += nb;
827
828#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
829 nb = utf32_to_utf8(uni, out, out_bound_len);
830#else
831 nb = utf8_wctomb(out, uni, out_bound_len);
832#endif
833 if (nb < 0) {
834 LogFunc(("nls->uni2char failed %x %d\n",
835 uni, out_bound_len));
836 err = -EINVAL;
837 goto fail1;
838 }
839 out_bound_len -= nb;
840 out += nb;
841 len += nb;
842 }
843 if (len >= PATH_MAX - 1) {
844 err = -ENAMETOOLONG;
845 goto fail1;
846 }
847
848 LogFunc(("result(%d) = %.*s\n", len, len, name));
849 *out = 0;
850 } else {
851 name = d_name;
852 len = d_len;
853 }
854
855 err = vbsf_make_path(caller, sf_i, name, len, result);
856 if (name != d_name)
857 kfree(name);
858
859 return err;
860
861 fail1:
862 kfree(name);
863 return err;
864}
865
866
867/**
868 * This is called during name resolution/lookup to check if the @a dentry in the
869 * cache is still valid. The actual validation is job is handled by
870 * vbsf_inode_revalidate_worker().
871 */
872#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
873static int vbsf_dentry_revalidate(struct dentry *dentry, unsigned flags)
874#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
875static int vbsf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
876#else
877static int vbsf_dentry_revalidate(struct dentry *dentry, int flags)
878#endif
879{
880#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
881 int const flags = nd ? nd->flags : 0;
882#endif
883
884 int rc;
885
886 Assert(dentry);
887 SFLOGFLOW(("vbsf_dentry_revalidate: %p %#x %s\n", dentry, flags,
888 dentry->d_inode ? VBSF_GET_INODE_INFO(dentry->d_inode)->path->String.ach : "<negative>"));
889
890 /*
891 * See Documentation/filesystems/vfs.txt why we skip LOOKUP_RCU.
892 *
893 * Also recommended: https://lwn.net/Articles/649115/
894 * https://lwn.net/Articles/649729/
895 * https://lwn.net/Articles/650786/
896 *
897 */
898#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
899 if (flags & LOOKUP_RCU) {
900 rc = -ECHILD;
901 SFLOGFLOW(("vbsf_dentry_revalidate: RCU -> -ECHILD\n"));
902 } else
903#endif
904 {
905 /*
906 * Do we have an inode or not? If not it's probably a negative cache
907 * entry, otherwise most likely a positive one.
908 */
909 struct inode *pInode = dentry->d_inode;
910 if (pInode) {
911 /*
912 * Positive entry.
913 *
914 * Note! We're more aggressive here than other remote file systems,
915 * current (4.19) CIFS will for instance revalidate the inode
916 * and ignore the dentry timestamp for positive entries.
917 */
918 //struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
919 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
920 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(dentry->d_sb);
921 if (cJiffiesAge < sf_g->ttl) {
922 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, sf_g->ttl));
923 rc = 1;
924 } else if (!vbsf_inode_revalidate_worker(dentry, true /*fForced*/)) {
925 vbsf_dentry_set_update_jiffies(dentry, jiffies); /** @todo get jiffies from inode. */
926 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 1\n", cJiffiesAge, sf_g->ttl));
927 rc = 1;
928 } else {
929 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 0\n", cJiffiesAge, sf_g->ttl));
930 rc = 0;
931 }
932 } else {
933 /*
934 * Negative entry.
935 *
936 * Invalidate dentries for open and renames here as we'll revalidate
937 * these when taking the actual action (also good for case preservation
938 * if we do case-insensitive mounts against windows + mac hosts at some
939 * later point).
940 */
941#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
942 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
943#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 75)
944 if (flags & LOOKUP_CREATE)
945#else
946 if (0)
947#endif
948 {
949 SFLOGFLOW(("vbsf_dentry_revalidate: negative: create or rename target -> 0\n"));
950 rc = 0;
951 } else {
952 /* Can we skip revalidation based on TTL? */
953 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
954 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(dentry->d_sb);
955 if (cJiffiesAge < sf_g->ttl) {
956 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, sf_g->ttl));
957 rc = 1;
958 } else {
959 /* We could revalidate it here, but we could instead just
960 have the caller kick it out. */
961 /** @todo stat the direntry and see if it exists now. */
962 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 0\n", cJiffiesAge, sf_g->ttl));
963 rc = 0;
964 }
965 }
966 }
967 }
968 return rc;
969}
970
971#ifdef SFLOG_ENABLED
972
973/** For logging purposes only. */
974# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
975static int vbsf_dentry_delete(const struct dentry *pDirEntry)
976# else
977static int vbsf_dentry_delete(struct dentry *pDirEntry)
978# endif
979{
980 SFLOGFLOW(("vbsf_dentry_delete: %p\n", pDirEntry));
981 return 0;
982}
983
984# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
985/** For logging purposes only. */
986static int vbsf_dentry_init(struct dentry *pDirEntry)
987{
988 SFLOGFLOW(("vbsf_dentry_init: %p\n", pDirEntry));
989 return 0;
990}
991# endif
992
993#endif /* SFLOG_ENABLED */
994
995/**
996 * Directory entry operations.
997 *
998 * Since 2.6.38 this is used via the super_block::s_d_op member.
999 */
1000struct dentry_operations vbsf_dentry_ops = {
1001 .d_revalidate = vbsf_dentry_revalidate,
1002#ifdef SFLOG_ENABLED
1003 .d_delete = vbsf_dentry_delete,
1004# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
1005 .d_init = vbsf_dentry_init,
1006# endif
1007#endif
1008};
1009
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