VirtualBox

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

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

linux/vboxsf: Adjusted ftruncate trick for skipping MTIME+CTIME updating, now we're really skipping the unnecessary VbglR0SfHostReqSetObjInfo call. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.4 KB
Line 
1/* $Id: utils.c 77964 2019-03-30 11:37:59Z 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 *pSuperInfo, char *name, size_t name_bound_len,
40 const unsigned char *utf8_name, size_t utf8_len)
41{
42 Assert(name_bound_len > 1);
43 Assert(RTStrNLen(utf8_name, utf8_len) == utf8_len);
44
45 if (pSuperInfo->nls) {
46 const char *in = utf8_name;
47 size_t in_bound_len = utf8_len;
48 char *out = name;
49 size_t out_bound_len = name_bound_len - 1;
50 size_t out_len = 0;
51
52 while (in_bound_len) {
53#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
54 unicode_t uni;
55 int cbInEnc = utf8_to_utf32(in, in_bound_len, &uni);
56#else
57 linux_wchar_t uni;
58 int cbInEnc = utf8_mbtowc(&uni, in, in_bound_len);
59#endif
60 if (cbInEnc >= 0) {
61 int cbOutEnc = pSuperInfo->nls->uni2char(uni, out, out_bound_len);
62 if (cbOutEnc >= 0) {
63 /*SFLOG3(("vbsf_nlscpy: cbOutEnc=%d cbInEnc=%d uni=%#x in_bound_len=%u\n", cbOutEnc, cbInEnc, uni, in_bound_len));*/
64 out += cbOutEnc;
65 out_bound_len -= cbOutEnc;
66 out_len += cbOutEnc;
67
68 in += cbInEnc;
69 in_bound_len -= cbInEnc;
70 } else {
71 SFLOG(("vbsf_nlscpy: nls->uni2char failed with %d on %#x (pos %u in '%s'), out_bound_len=%u\n",
72 cbOutEnc, uni, in - (const char *)utf8_name, (const char *)utf8_name, (unsigned)out_bound_len));
73 return cbOutEnc;
74 }
75 } else {
76 SFLOG(("vbsf_nlscpy: utf8_to_utf32/utf8_mbtowc failed with %d on %x (pos %u in '%s'), in_bound_len=%u!\n",
77 cbInEnc, *in, in - (const char *)utf8_name, (const char *)utf8_name, (unsigned)in_bound_len));
78 return -EINVAL;
79 }
80 }
81
82 *out = '\0';
83 } else {
84 if (utf8_len + 1 > name_bound_len)
85 return -ENAMETOOLONG;
86
87 memcpy(name, utf8_name, utf8_len + 1);
88 }
89 return 0;
90}
91
92
93/**
94 * Converts the given NLS string to a host one, kmalloc'ing
95 * the output buffer (use kfree on result).
96 */
97int vbsf_nls_to_shflstring(struct vbsf_super_info *pSuperInfo, const char *pszNls, PSHFLSTRING *ppString)
98{
99 int rc;
100 size_t const cchNls = strlen(pszNls);
101 PSHFLSTRING pString = NULL;
102 if (pSuperInfo->nls) {
103 /*
104 * NLS -> UTF-8 w/ SHLF string header.
105 */
106 /* Calc length first: */
107 size_t cchUtf8 = 0;
108 size_t offNls = 0;
109 while (offNls < cchNls) {
110 linux_wchar_t uc; /* Note! We renamed the type due to clashes. */
111 int const cbNlsCodepoint = pSuperInfo->nls->char2uni(&pszNls[offNls], cchNls - offNls, &uc);
112 if (cbNlsCodepoint >= 0) {
113 char achTmp[16];
114#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
115 int cbUtf8Codepoint = utf32_to_utf8(uc, achTmp, sizeof(achTmp));
116#else
117 int cbUtf8Codepoint = utf8_wctomb(achTmp, uc, sizeof(achTmp));
118#endif
119 if (cbUtf8Codepoint > 0) {
120 cchUtf8 += cbUtf8Codepoint;
121 offNls += cbNlsCodepoint;
122 } else {
123 Log(("vbsf_nls_to_shflstring: nls->uni2char(%#x) failed: %d\n", uc, cbUtf8Codepoint));
124 return -EINVAL;
125 }
126 } else {
127 Log(("vbsf_nls_to_shflstring: nls->char2uni(%.*Rhxs) failed: %d\n",
128 RT_MIN(8, cchNls - offNls), &pszNls[offNls], cbNlsCodepoint));
129 return -EINVAL;
130 }
131 }
132 if (cchUtf8 + 1 < _64K) {
133 /* Allocate: */
134 pString = (PSHFLSTRING)kmalloc(SHFLSTRING_HEADER_SIZE + cchUtf8 + 1, GFP_KERNEL);
135 if (pString) {
136 char *pchDst = pString->String.ach;
137 pString->u16Length = (uint16_t)cchUtf8;
138 pString->u16Size = (uint16_t)(cchUtf8 + 1);
139
140 /* Do the conversion (cchUtf8 is counted down): */
141 rc = 0;
142 offNls = 0;
143 while (offNls < cchNls) {
144 linux_wchar_t uc; /* Note! We renamed the type due to clashes. */
145 int const cbNlsCodepoint = pSuperInfo->nls->char2uni(&pszNls[offNls], cchNls - offNls, &uc);
146 if (cbNlsCodepoint >= 0) {
147#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
148 int cbUtf8Codepoint = utf32_to_utf8(uc, pchDst, cchUtf8);
149#else
150 int cbUtf8Codepoint = utf8_wctomb(pchDst, uc, cchUtf8);
151#endif
152 if (cbUtf8Codepoint > 0) {
153 AssertBreakStmt(cbUtf8Codepoint <= cchUtf8, rc = -EINVAL);
154 cchUtf8 -= cbUtf8Codepoint;
155 pchDst += cbUtf8Codepoint;
156 offNls += cbNlsCodepoint;
157 } else {
158 Log(("vbsf_nls_to_shflstring: nls->uni2char(%#x) failed! %d, cchUtf8=%zu\n",
159 uc, cbUtf8Codepoint, cchUtf8));
160 rc = -EINVAL;
161 break;
162 }
163 } else {
164 Log(("vbsf_nls_to_shflstring: nls->char2uni(%.*Rhxs) failed! %d\n",
165 RT_MIN(8, cchNls - offNls), &pszNls[offNls], cbNlsCodepoint));
166 rc = -EINVAL;
167 break;
168 }
169 }
170 if (rc == 0) {
171 /*
172 * Succeeded. Just terminate the string and we're good.
173 */
174 Assert(pchDst - pString->String.ach == pString->u16Length);
175 *pchDst = '\0';
176 } else {
177 kfree(pString);
178 pString = NULL;
179 }
180 } else {
181 Log(("vbsf_nls_to_shflstring: failed to allocate %u bytes\n", SHFLSTRING_HEADER_SIZE + cchUtf8 + 1));
182 rc = -ENOMEM;
183 }
184 } else {
185 Log(("vbsf_nls_to_shflstring: too long: %zu bytes (%zu nls bytes)\n", cchUtf8, cchNls));
186 rc = -ENAMETOOLONG;
187 }
188 } else {
189 /*
190 * UTF-8 -> UTF-8 w/ SHLF string header.
191 */
192 if (cchNls + 1 < _64K) {
193 pString = (PSHFLSTRING)kmalloc(SHFLSTRING_HEADER_SIZE + cchNls + 1, GFP_KERNEL);
194 if (pString) {
195 pString->u16Length = (uint16_t)cchNls;
196 pString->u16Size = (uint16_t)(cchNls + 1);
197 memcpy(pString->String.ach, pszNls, cchNls);
198 pString->String.ach[cchNls] = '\0';
199 rc = 0;
200 } else {
201 Log(("vbsf_nls_to_shflstring: failed to allocate %u bytes\n", SHFLSTRING_HEADER_SIZE + cchNls + 1));
202 rc = -ENOMEM;
203 }
204 } else {
205 Log(("vbsf_nls_to_shflstring: too long: %zu bytes\n", cchNls));
206 rc = -ENAMETOOLONG;
207 }
208 }
209 *ppString = pString;
210 return rc;
211}
212
213
214/**
215 * Convert from VBox to linux time.
216 */
217#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
218DECLINLINE(void) vbsf_time_to_linux(time_t *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
219{
220 int64_t t = RTTimeSpecGetNano(pVBoxSrc);
221 do_div(t, RT_NS_1SEC);
222 *pLinuxDst = t;
223}
224#else /* >= 2.6.0 */
225# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
226DECLINLINE(void) vbsf_time_to_linux(struct timespec *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
227# else
228DECLINLINE(void) vbsf_time_to_linux(struct timespec64 *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
229# endif
230{
231 int64_t t = RTTimeSpecGetNano(pVBoxSrc);
232 pLinuxDst->tv_nsec = do_div(t, RT_NS_1SEC);
233 pLinuxDst->tv_sec = t;
234}
235#endif /* >= 2.6.0 */
236
237
238/**
239 * Convert from linux to VBox time.
240 */
241#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
242DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, time_t *pLinuxSrc)
243{
244 RTTimeSpecSetNano(pVBoxDst, RT_NS_1SEC_64 * *pLinuxSrc);
245}
246#else /* >= 2.6.0 */
247# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
248DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, struct timespec const *pLinuxSrc)
249# else
250DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, struct timespec64 const *pLinuxSrc)
251# endif
252{
253 RTTimeSpecSetNano(pVBoxDst, pLinuxSrc->tv_nsec + pLinuxSrc->tv_sec * (int64_t)RT_NS_1SEC);
254}
255#endif /* >= 2.6.0 */
256
257
258/**
259 * Converts VBox access permissions to Linux ones (mode & 0777).
260 *
261 * @note Currently identical.
262 * @sa sf_access_permissions_to_vbox
263 */
264DECLINLINE(int) sf_access_permissions_to_linux(uint32_t fAttr)
265{
266 /* Access bits should be the same: */
267 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
268 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
269 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
270 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
271 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
272 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
273 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
274 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
275 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
276
277 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
278}
279
280
281/**
282 * Produce the Linux mode mask, given VBox, mount options and file type.
283 */
284DECLINLINE(int) sf_file_mode_to_linux(uint32_t fVBoxMode, int fFixedMode, int fClearMask, int fType)
285{
286 int fLnxMode = sf_access_permissions_to_linux(fVBoxMode);
287 if (fFixedMode != ~0)
288 fLnxMode = fFixedMode & 0777;
289 fLnxMode &= ~fClearMask;
290 fLnxMode |= fType;
291 return fLnxMode;
292}
293
294
295/**
296 * Initializes the @a inode attributes based on @a pObjInfo and @a pSuperInfo
297 * options.
298 */
299void vbsf_init_inode(struct inode *inode, struct vbsf_inode_info *sf_i, PSHFLFSOBJINFO pObjInfo,
300 struct vbsf_super_info *pSuperInfo)
301{
302 PCSHFLFSOBJATTR pAttr = &pObjInfo->Attr;
303
304 TRACE();
305
306 sf_i->ts_up_to_date = jiffies;
307 sf_i->force_restat = 0;
308
309#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
310 inode->i_mapping->a_ops = &vbsf_reg_aops;
311# if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
312 inode->i_mapping->backing_dev_info = &pSuperInfo->bdi; /* This is needed for mmap. */
313# endif
314#endif
315 if (RTFS_IS_DIRECTORY(pAttr->fMode)) {
316 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, pSuperInfo->dmode, pSuperInfo->dmask, S_IFDIR);
317 inode->i_op = &vbsf_dir_iops;
318 inode->i_fop = &vbsf_dir_fops;
319
320 /* XXX: this probably should be set to the number of entries
321 in the directory plus two (. ..) */
322 set_nlink(inode, 1);
323 }
324 else if (RTFS_IS_SYMLINK(pAttr->fMode)) {
325 /** @todo r=bird: Aren't System V symlinks w/o any mode mask? IIRC there is
326 * no lchmod on Linux. */
327 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, pSuperInfo->fmode, pSuperInfo->fmask, S_IFLNK);
328 inode->i_op = &vbsf_lnk_iops;
329 set_nlink(inode, 1);
330 } else {
331 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, pSuperInfo->fmode, pSuperInfo->fmask, S_IFREG);
332 inode->i_op = &vbsf_reg_iops;
333 inode->i_fop = &vbsf_reg_fops;
334 set_nlink(inode, 1);
335 }
336
337#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
338 inode->i_uid = make_kuid(current_user_ns(), pSuperInfo->uid);
339 inode->i_gid = make_kgid(current_user_ns(), pSuperInfo->gid);
340#else
341 inode->i_uid = pSuperInfo->uid;
342 inode->i_gid = pSuperInfo->gid;
343#endif
344
345 inode->i_size = pObjInfo->cbObject;
346#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
347 inode->i_blksize = 4096;
348#endif
349#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
350 inode->i_blkbits = 12;
351#endif
352 /* i_blocks always in units of 512 bytes! */
353 inode->i_blocks = (pObjInfo->cbAllocated + 511) / 512;
354
355 vbsf_time_to_linux(&inode->i_atime, &pObjInfo->AccessTime);
356 vbsf_time_to_linux(&inode->i_ctime, &pObjInfo->ChangeTime);
357 vbsf_time_to_linux(&inode->i_mtime, &pObjInfo->ModificationTime);
358 sf_i->BirthTime = pObjInfo->BirthTime;
359 sf_i->ModificationTime = pObjInfo->ModificationTime;
360 RTTimeSpecSetSeconds(&sf_i->ModificationTimeAtOurLastWrite, 0);
361}
362
363
364/**
365 * Update the inode with new object info from the host.
366 *
367 * Called by sf_inode_revalidate() and sf_inode_revalidate_with_handle().
368 */
369void vbsf_update_inode(struct inode *pInode, struct vbsf_inode_info *pInodeInfo, PSHFLFSOBJINFO pObjInfo,
370 struct vbsf_super_info *pSuperInfo, bool fInodeLocked, unsigned fSetAttrs)
371{
372 PCSHFLFSOBJATTR pAttr = &pObjInfo->Attr;
373 int fMode;
374
375 TRACE();
376
377#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
378 if (!fInodeLocked)
379 inode_lock(pInode);
380#endif
381
382 /*
383 * Calc new mode mask and update it if it changed.
384 */
385 if (RTFS_IS_DIRECTORY(pAttr->fMode))
386 fMode = sf_file_mode_to_linux(pAttr->fMode, pSuperInfo->dmode, pSuperInfo->dmask, S_IFDIR);
387 else if (RTFS_IS_SYMLINK(pAttr->fMode))
388 /** @todo r=bird: Aren't System V symlinks w/o any mode mask? IIRC there is
389 * no lchmod on Linux. */
390 fMode = sf_file_mode_to_linux(pAttr->fMode, pSuperInfo->fmode, pSuperInfo->fmask, S_IFLNK);
391 else
392 fMode = sf_file_mode_to_linux(pAttr->fMode, pSuperInfo->fmode, pSuperInfo->fmask, S_IFREG);
393
394 if (fMode == pInode->i_mode) {
395 /* likely */
396 } else {
397 if ((fMode & S_IFMT) == (pInode->i_mode & S_IFMT))
398 pInode->i_mode = fMode;
399 else {
400 SFLOGFLOW(("vbsf_update_inode: Changed from %o to %o (%s)\n",
401 pInode->i_mode & S_IFMT, fMode & S_IFMT, pInodeInfo->path->String.ach));
402 /** @todo we probably need to be more drastic... */
403 vbsf_init_inode(pInode, pInodeInfo, pObjInfo, pSuperInfo);
404
405#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
406 if (!fInodeLocked)
407 inode_unlock(pInode);
408#endif
409 return;
410 }
411 }
412
413 /*
414 * Update the sizes.
415 * Note! i_blocks is always in units of 512 bytes!
416 */
417 pInode->i_blocks = (pObjInfo->cbAllocated + 511) / 512;
418 i_size_write(pInode, pObjInfo->cbObject);
419
420 /*
421 * Update the timestamps.
422 */
423 vbsf_time_to_linux(&pInode->i_atime, &pObjInfo->AccessTime);
424 vbsf_time_to_linux(&pInode->i_ctime, &pObjInfo->ChangeTime);
425 vbsf_time_to_linux(&pInode->i_mtime, &pObjInfo->ModificationTime);
426 pInodeInfo->BirthTime = pObjInfo->BirthTime;
427
428 /*
429 * Mark it as up to date.
430 * Best to do this before we start with any expensive map invalidation.
431 */
432 pInodeInfo->ts_up_to_date = jiffies;
433 pInodeInfo->force_restat = 0;
434
435 /*
436 * If the modification time changed, we may have to invalidate the page
437 * cache pages associated with this inode if we suspect the change was
438 * made by the host. How supicious we are depends on the cache mode.
439 *
440 * Note! The invalidate_inode_pages() call is pretty weak. It will _not_
441 * touch pages that are already mapped into an address space, but it
442 * will help if the file isn't currently mmap'ed or if we're in read
443 * or read/write caching mode.
444 */
445 if (!RTTimeSpecIsEqual(&pInodeInfo->ModificationTime, &pObjInfo->ModificationTime)) {
446 if (RTFS_IS_FILE(pAttr->fMode)) {
447 if (!(fSetAttrs & (ATTR_MTIME | ATTR_SIZE))) {
448 bool fInvalidate;
449 if (pSuperInfo->enmCacheMode == kVbsfCacheMode_None) {
450 fInvalidate = true; /* No-caching: always invalidate. */
451 } else {
452 if (RTTimeSpecIsEqual(&pInodeInfo->ModificationTimeAtOurLastWrite, &pInodeInfo->ModificationTime)) {
453 fInvalidate = false; /* Could be our write, so don't invalidate anything */
454 RTTimeSpecSetSeconds(&pInodeInfo->ModificationTimeAtOurLastWrite, 0);
455 } else {
456 /*RTLogBackdoorPrintf("vbsf_update_inode: Invalidating the mapping %s - %RU64 vs %RU64 vs %RU64 - %#x\n",
457 pInodeInfo->path->String.ach,
458 RTTimeSpecGetNano(&pInodeInfo->ModificationTimeAtOurLastWrite),
459 RTTimeSpecGetNano(&pInodeInfo->ModificationTime),
460 RTTimeSpecGetNano(&pObjInfo->ModificationTime), fSetAttrs);*/
461 fInvalidate = true; /* We haven't modified the file recently, so probably a host update. */
462 }
463 }
464 pInodeInfo->ModificationTime = pObjInfo->ModificationTime;
465
466 if (fInvalidate) {
467 struct address_space *mapping = pInode->i_mapping;
468 if (mapping && mapping->nrpages > 0) {
469 SFLOGFLOW(("vbsf_update_inode: Invalidating the mapping %s (%#x)\n", pInodeInfo->path->String.ach, fSetAttrs));
470#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
471 invalidate_mapping_pages(mapping, 0, ~(pgoff_t)0);
472#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 41)
473 invalidate_inode_pages(mapping);
474#else
475 invalidate_inode_pages(pInode);
476#endif
477 }
478 }
479 } else {
480 RTTimeSpecSetSeconds(&pInodeInfo->ModificationTimeAtOurLastWrite, 0);
481 pInodeInfo->ModificationTime = pObjInfo->ModificationTime;
482 }
483 } else
484 pInodeInfo->ModificationTime = pObjInfo->ModificationTime;
485 }
486
487 /*
488 * Done.
489 */
490#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
491 if (!fInodeLocked)
492 inode_unlock(pInode);
493#endif
494}
495
496
497/** @note Currently only used for the root directory during (re-)mount. */
498int vbsf_stat(const char *caller, struct vbsf_super_info *pSuperInfo, SHFLSTRING *path, PSHFLFSOBJINFO result, int ok_to_fail)
499{
500 int rc;
501 VBOXSFCREATEREQ *pReq;
502 NOREF(caller);
503
504 TRACE();
505
506 pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + path->u16Size);
507 if (pReq) {
508 RT_ZERO(*pReq);
509 memcpy(&pReq->StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
510 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
511 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
512
513 LogFunc(("Calling VbglR0SfHostReqCreate on %s\n", path->String.utf8));
514 rc = VbglR0SfHostReqCreate(pSuperInfo->map.root, pReq);
515 if (RT_SUCCESS(rc)) {
516 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
517 *result = pReq->CreateParms.Info;
518 rc = 0;
519 } else {
520 if (!ok_to_fail)
521 LogFunc(("VbglR0SfHostReqCreate on %s: file does not exist: %d (caller=%s)\n",
522 path->String.utf8, pReq->CreateParms.Result, caller));
523 rc = -ENOENT;
524 }
525 } else if (rc == VERR_INVALID_NAME) {
526 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
527 } else {
528 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc (caller=%s)\n", path->String.utf8, rc, caller));
529 rc = -EPROTO;
530 }
531 VbglR0PhysHeapFree(pReq);
532 }
533 else
534 rc = -ENOMEM;
535 return rc;
536}
537
538
539/**
540 * Revalidate an inode, inner worker.
541 *
542 * @sa sf_inode_revalidate()
543 */
544int vbsf_inode_revalidate_worker(struct dentry *dentry, bool fForced, bool fInodeLocked)
545{
546 int rc;
547 struct inode *pInode = dentry ? dentry->d_inode : NULL;
548 if (pInode) {
549 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
550 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(pInode->i_sb);
551 AssertReturn(sf_i, -EINVAL);
552 AssertReturn(pSuperInfo, -EINVAL);
553
554 /*
555 * Can we get away without any action here?
556 */
557 if ( !fForced
558 && !sf_i->force_restat
559 && jiffies - sf_i->ts_up_to_date < pSuperInfo->cJiffiesInodeTTL)
560 rc = 0;
561 else {
562 /*
563 * No, we have to query the file info from the host.
564 * Try get a handle we can query, any kind of handle will do here.
565 */
566 struct vbsf_handle *pHandle = vbsf_handle_find(sf_i, 0, 0);
567 if (pHandle) {
568 /* Query thru pHandle. */
569 VBOXSFOBJINFOREQ *pReq = (VBOXSFOBJINFOREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
570 if (pReq) {
571 RT_ZERO(*pReq);
572 rc = VbglR0SfHostReqQueryObjInfo(pSuperInfo->map.root, pReq, pHandle->hHost);
573 if (RT_SUCCESS(rc)) {
574 /*
575 * Reset the TTL and copy the info over into the inode structure.
576 */
577 vbsf_update_inode(pInode, sf_i, &pReq->ObjInfo, pSuperInfo, fInodeLocked, 0 /*fSetAttrs*/);
578 } else if (rc == VERR_INVALID_HANDLE) {
579 rc = -ENOENT; /* Restore.*/
580 } else {
581 LogFunc(("VbglR0SfHostReqQueryObjInfo failed on %#RX64: %Rrc\n", pHandle->hHost, rc));
582 rc = -RTErrConvertToErrno(rc);
583 }
584 VbglR0PhysHeapFree(pReq);
585 } else
586 rc = -ENOMEM;
587 vbsf_handle_release(pHandle, pSuperInfo, "vbsf_inode_revalidate_worker");
588
589 } else {
590 /* Query via path. */
591 SHFLSTRING *pPath = sf_i->path;
592 VBOXSFCREATEREQ *pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + pPath->u16Size);
593 if (pReq) {
594 RT_ZERO(*pReq);
595 memcpy(&pReq->StrPath, pPath, SHFLSTRING_HEADER_SIZE + pPath->u16Size);
596 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
597 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
598
599 rc = VbglR0SfHostReqCreate(pSuperInfo->map.root, pReq);
600 if (RT_SUCCESS(rc)) {
601 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
602 /*
603 * Reset the TTL and copy the info over into the inode structure.
604 */
605 vbsf_update_inode(pInode, sf_i, &pReq->CreateParms.Info, pSuperInfo, fInodeLocked, 0 /*fSetAttrs*/);
606 rc = 0;
607 } else {
608 rc = -ENOENT;
609 }
610 } else if (rc == VERR_INVALID_NAME) {
611 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
612 } else {
613 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc\n", pPath->String.ach, rc));
614 rc = -EPROTO;
615 }
616 VbglR0PhysHeapFree(pReq);
617 }
618 else
619 rc = -ENOMEM;
620 }
621 }
622 } else {
623 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, pInode));
624 rc = -EINVAL;
625 }
626 return rc;
627}
628
629
630#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 18)
631/**
632 * Revalidate an inode for 2.4.
633 *
634 * This is called in the stat(), lstat() and readlink() code paths. In the stat
635 * cases the caller will use the result afterwards to produce the stat data.
636 *
637 * @note 2.4.x has a getattr() inode operation too, but it is not used.
638 */
639int vbsf_inode_revalidate(struct dentry *dentry)
640{
641 /*
642 * We pretend the inode is locked here, as 2.4.x does not have inode level locking.
643 */
644 return vbsf_inode_revalidate_worker(dentry, false /*fForced*/, true /*fInodeLocked*/);
645}
646#endif /* < 2.5.18 */
647
648
649/**
650 * Similar to sf_inode_revalidate, but uses associated host file handle as that
651 * is quite a bit faster.
652 */
653int vbsf_inode_revalidate_with_handle(struct dentry *dentry, SHFLHANDLE hHostFile, bool fForced, bool fInodeLocked)
654{
655 int err;
656 struct inode *pInode = dentry ? dentry->d_inode : NULL;
657 if (!pInode) {
658 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, pInode));
659 err = -EINVAL;
660 } else {
661 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
662 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(pInode->i_sb);
663 AssertReturn(sf_i, -EINVAL);
664 AssertReturn(pSuperInfo, -EINVAL);
665
666 /*
667 * Can we get away without any action here?
668 */
669 if ( !fForced
670 && !sf_i->force_restat
671 && jiffies - sf_i->ts_up_to_date < pSuperInfo->cJiffiesInodeTTL)
672 err = 0;
673 else {
674 /*
675 * No, we have to query the file info from the host.
676 */
677 VBOXSFOBJINFOREQ *pReq = (VBOXSFOBJINFOREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
678 if (pReq) {
679 RT_ZERO(*pReq);
680 err = VbglR0SfHostReqQueryObjInfo(pSuperInfo->map.root, pReq, hHostFile);
681 if (RT_SUCCESS(err)) {
682 /*
683 * Reset the TTL and copy the info over into the inode structure.
684 */
685 vbsf_update_inode(pInode, sf_i, &pReq->ObjInfo, pSuperInfo, fInodeLocked, 0 /*fSetAttrs*/);
686 } else {
687 LogFunc(("VbglR0SfHostReqQueryObjInfo failed on %#RX64: %Rrc\n", hHostFile, err));
688 err = -RTErrConvertToErrno(err);
689 }
690 VbglR0PhysHeapFree(pReq);
691 } else
692 err = -ENOMEM;
693 }
694 }
695 return err;
696}
697
698
699/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
700 effect) updates inode attributes for [dentry] (given that [dentry]
701 has inode at all) from these new attributes we derive [kstat] via
702 [generic_fillattr] */
703#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 18)
704
705# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
706int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int flags)
707# else
708int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
709# endif
710{
711 int rc;
712# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
713 struct dentry *dentry = path->dentry;
714# endif
715
716# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
717 SFLOGFLOW(("vbsf_inode_getattr: dentry=%p request_mask=%#x flags=%#x\n", dentry, request_mask, flags));
718# else
719 SFLOGFLOW(("vbsf_inode_getattr: dentry=%p\n", dentry));
720# endif
721
722# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
723 /*
724 * With the introduction of statx() userland can control whether we
725 * update the inode information or not.
726 */
727 switch (flags & AT_STATX_SYNC_TYPE) {
728 default:
729 rc = vbsf_inode_revalidate_worker(dentry, false /*fForced*/, false /*fInodeLocked*/);
730 break;
731
732 case AT_STATX_FORCE_SYNC:
733 rc = vbsf_inode_revalidate_worker(dentry, true /*fForced*/, false /*fInodeLocked*/);
734 break;
735
736 case AT_STATX_DONT_SYNC:
737 rc = 0;
738 break;
739 }
740# else
741 rc = vbsf_inode_revalidate_worker(dentry, false /*fForced*/, false /*fInodeLocked*/);
742# endif
743 if (rc == 0) {
744 /* Do generic filling in of info. */
745 generic_fillattr(dentry->d_inode, kstat);
746
747 /* Add birth time. */
748# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
749 if (dentry->d_inode) {
750 struct vbsf_inode_info *pInodeInfo = VBSF_GET_INODE_INFO(dentry->d_inode);
751 if (pInodeInfo) {
752 vbsf_time_to_linux(&kstat->btime, &pInodeInfo->BirthTime);
753 kstat->result_mask |= STATX_BTIME;
754 }
755 }
756# endif
757
758 /*
759 * FsPerf shows the following numbers for sequential file access against
760 * a tmpfs folder on an AMD 1950X host running debian buster/sid:
761 *
762 * block size = r128600 ----- r128755 -----
763 * reads reads writes
764 * 4096 KB = 2254 MB/s 4953 MB/s 3668 MB/s
765 * 2048 KB = 2368 MB/s 4908 MB/s 3541 MB/s
766 * 1024 KB = 2208 MB/s 4011 MB/s 3291 MB/s
767 * 512 KB = 1908 MB/s 3399 MB/s 2721 MB/s
768 * 256 KB = 1625 MB/s 2679 MB/s 2251 MB/s
769 * 128 KB = 1413 MB/s 1967 MB/s 1684 MB/s
770 * 64 KB = 1152 MB/s 1409 MB/s 1265 MB/s
771 * 32 KB = 726 MB/s 815 MB/s 783 MB/s
772 * 16 KB = 683 MB/s 475 MB/s
773 * 8 KB = 294 MB/s 286 MB/s
774 * 4 KB = 145 MB/s 156 MB/s 149 MB/s
775 *
776 */
777 if (S_ISREG(kstat->mode))
778 kstat->blksize = _1M;
779 else if (S_ISDIR(kstat->mode))
780 /** @todo this may need more tuning after we rewrite the directory handling. */
781 kstat->blksize = _16K;
782 }
783 return rc;
784}
785#endif /* >= 2.5.18 */
786
787
788/**
789 * Modify inode attributes.
790 */
791int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr)
792{
793 struct inode *pInode = dentry->d_inode;
794 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(pInode->i_sb);
795 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
796 int vrc;
797 int rc;
798
799 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p inode=%p ia_valid=%#x %s\n",
800 dentry, pInode, iattr->ia_valid, sf_i ? sf_i->path->String.ach : NULL));
801 AssertReturn(sf_i, -EINVAL);
802
803 /*
804 * Need to check whether the caller is allowed to modify the attributes or not.
805 */
806#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
807 rc = setattr_prepare(dentry, iattr);
808#else
809 rc = inode_change_ok(pInode, iattr);
810#endif
811 if (rc == 0) {
812 /*
813 * Don't modify MTIME and CTIME for open(O_TRUNC) and ftruncate, those
814 * operations will set those timestamps automatically. Saves a host call.
815 */
816 unsigned fAttrs = iattr->ia_valid;
817 if ( (fAttrs & ~ATTR_FILE) == (ATTR_SIZE | ATTR_MTIME | ATTR_CTIME)
818#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
819 || (fAttrs & (ATTR_OPEN | ATTR_SIZE)) == (ATTR_OPEN | ATTR_SIZE)
820#endif
821 )
822 fAttrs &= ~(ATTR_MTIME | ATTR_CTIME);
823
824 /*
825 * We only implement a handful of attributes, so ignore any attempts
826 * at setting bits we don't support.
827 */
828 if (fAttrs & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE)) {
829 /*
830 * Try find a handle which allows us to modify the attributes, otherwise
831 * open the file/dir/whatever.
832 */
833 union SetAttrReqs
834 {
835 VBOXSFCREATEREQ Create;
836 VBOXSFOBJINFOREQ Info;
837 VBOXSFSETFILESIZEREQ SetSize;
838 VBOXSFCLOSEREQ Close;
839 } *pReq;
840 size_t cbReq;
841 SHFLHANDLE hHostFile;
842 /** @todo ATTR_FILE (2.6.15+) could be helpful here if we like. */
843 struct vbsf_handle *pHandle = fAttrs & ATTR_SIZE
844 ? vbsf_handle_find(sf_i, VBSF_HANDLE_F_WRITE, 0)
845 : vbsf_handle_find(sf_i, 0, 0);
846 if (pHandle) {
847 hHostFile = pHandle->hHost;
848 cbReq = RT_MAX(sizeof(VBOXSFOBJINFOREQ), sizeof(VBOXSFSETFILESIZEREQ));
849 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
850 if (pReq) {
851 /* likely */
852 } else
853 rc = -ENOMEM;
854 } else {
855 hHostFile = SHFL_HANDLE_NIL;
856 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
857 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
858 if (pReq) {
859 RT_ZERO(pReq->Create.CreateParms);
860 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
861 pReq->Create.CreateParms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
862 | SHFL_CF_ACT_FAIL_IF_NEW
863 | SHFL_CF_ACCESS_ATTR_WRITE;
864 if (fAttrs & ATTR_SIZE)
865 pReq->Create.CreateParms.CreateFlags |= SHFL_CF_ACCESS_WRITE;
866 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
867 vrc = VbglR0SfHostReqCreate(pSuperInfo->map.root, &pReq->Create);
868 if (RT_SUCCESS(vrc)) {
869 if (pReq->Create.CreateParms.Result == SHFL_FILE_EXISTS) {
870 hHostFile = pReq->Create.CreateParms.Handle;
871 Assert(hHostFile != SHFL_HANDLE_NIL);
872 vbsf_dentry_chain_increase_ttl(dentry);
873 } else {
874 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
875 vbsf_dentry_invalidate_ttl(dentry);
876 sf_i->force_restat = true;
877 rc = -ENOENT;
878 }
879 } else {
880 rc = -RTErrConvertToErrno(vrc);
881 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc rc=%d\n", sf_i->path->String.ach, vrc, rc));
882 }
883 } else
884 rc = -ENOMEM;
885 }
886 if (rc == 0) {
887 /*
888 * Set mode and/or timestamps.
889 */
890 if (fAttrs & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) {
891 /* Fill in the attributes. Start by setting all to zero
892 since the host will ignore zeroed fields. */
893 RT_ZERO(pReq->Info.ObjInfo);
894
895 if (fAttrs & ATTR_MODE) {
896 pReq->Info.ObjInfo.Attr.fMode = sf_access_permissions_to_vbox(iattr->ia_mode);
897 if (iattr->ia_mode & S_IFDIR)
898 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_DIRECTORY;
899 else if (iattr->ia_mode & S_IFLNK)
900 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_SYMLINK;
901 else
902 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_FILE;
903 }
904 if (fAttrs & ATTR_ATIME)
905 vbsf_time_to_vbox(&pReq->Info.ObjInfo.AccessTime, &iattr->ia_atime);
906 if (fAttrs & ATTR_MTIME)
907 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ModificationTime, &iattr->ia_mtime);
908 if (fAttrs & ATTR_CTIME)
909 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ChangeTime, &iattr->ia_ctime);
910
911 /* Make the change. */
912 vrc = VbglR0SfHostReqSetObjInfo(pSuperInfo->map.root, &pReq->Info, hHostFile);
913 if (RT_SUCCESS(vrc)) {
914 vbsf_update_inode(pInode, sf_i, &pReq->Info.ObjInfo, pSuperInfo, true /*fLocked*/, fAttrs);
915 } else {
916 rc = -RTErrConvertToErrno(vrc);
917 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc rc=%d\n", sf_i->path->String.ach, vrc, rc));
918 }
919 }
920
921 /*
922 * Change the file size.
923 * Note! Old API is more convenient here as it gives us up to date
924 * inode info back.
925 */
926 if ((fAttrs & ATTR_SIZE) && rc == 0) {
927 /*vrc = VbglR0SfHostReqSetFileSize(pSuperInfo->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
928 if (RT_SUCCESS(vrc)) {
929 i_size_write(pInode, iattr->ia_size);
930 } else if (vrc == VERR_NOT_IMPLEMENTED)*/ {
931 /* Fallback for pre 6.0 hosts: */
932 RT_ZERO(pReq->Info.ObjInfo);
933 pReq->Info.ObjInfo.cbObject = iattr->ia_size;
934 vrc = VbglR0SfHostReqSetFileSizeOld(pSuperInfo->map.root, &pReq->Info, hHostFile);
935 if (RT_SUCCESS(vrc))
936 vbsf_update_inode(pInode, sf_i, &pReq->Info.ObjInfo, pSuperInfo, true /*fLocked*/, fAttrs);
937 }
938 if (RT_SUCCESS(vrc)) {
939 /** @todo there is potentially more to be done here if there are mappings of
940 * the lovely file. */
941 } else {
942 rc = -RTErrConvertToErrno(vrc);
943 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc rc=%d\n",
944 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, rc));
945 }
946 }
947
948 /*
949 * Clean up.
950 */
951 if (!pHandle) {
952 vrc = VbglR0SfHostReqClose(pSuperInfo->map.root, &pReq->Close, hHostFile);
953 if (RT_FAILURE(vrc))
954 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
955 }
956 }
957 if (pReq)
958 VbglR0PhysHeapFree(pReq);
959 if (pHandle)
960 vbsf_handle_release(pHandle, pSuperInfo, "vbsf_inode_setattr");
961 } else
962 SFLOGFLOW(("vbsf_inode_setattr: Nothing to do here: %#x (was %#x).\n", fAttrs, iattr->ia_valid));
963 }
964 return rc;
965}
966
967
968static int vbsf_make_path(const char *caller, struct vbsf_inode_info *sf_i,
969 const char *d_name, size_t d_len, SHFLSTRING **result)
970{
971 size_t path_len, shflstring_len;
972 SHFLSTRING *tmp;
973 uint16_t p_len;
974 uint8_t *p_name;
975 int fRoot = 0;
976
977 TRACE();
978 p_len = sf_i->path->u16Length;
979 p_name = sf_i->path->String.utf8;
980
981 if (p_len == 1 && *p_name == '/') {
982 path_len = d_len + 1;
983 fRoot = 1;
984 } else {
985 /* lengths of constituents plus terminating zero plus slash */
986 path_len = p_len + d_len + 2;
987 if (path_len > 0xffff) {
988 LogFunc(("path too long. caller=%s, path_len=%zu\n",
989 caller, path_len));
990 return -ENAMETOOLONG;
991 }
992 }
993
994 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
995 tmp = kmalloc(shflstring_len, GFP_KERNEL);
996 if (!tmp) {
997 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
998 return -ENOMEM;
999 }
1000 tmp->u16Length = path_len - 1;
1001 tmp->u16Size = path_len;
1002
1003 if (fRoot)
1004 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
1005 else {
1006 memcpy(&tmp->String.utf8[0], p_name, p_len);
1007 tmp->String.utf8[p_len] = '/';
1008 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
1009 tmp->String.utf8[p_len + 1 + d_len] = '\0';
1010 }
1011
1012 *result = tmp;
1013 return 0;
1014}
1015
1016
1017/**
1018 * [dentry] contains string encoded in coding system that corresponds
1019 * to [pSuperInfo]->nls, we must convert it to UTF8 here and pass down to
1020 * [vbsf_make_path] which will allocate SHFLSTRING and fill it in
1021 */
1022int vbsf_path_from_dentry(struct vbsf_super_info *pSuperInfo, struct vbsf_inode_info *sf_i, struct dentry *dentry,
1023 SHFLSTRING **result, const char *caller)
1024{
1025 int err;
1026 const char *d_name;
1027 size_t d_len;
1028 const char *name;
1029 size_t len = 0;
1030
1031 TRACE();
1032 d_name = dentry->d_name.name;
1033 d_len = dentry->d_name.len;
1034
1035 if (pSuperInfo->nls) {
1036 size_t in_len, i, out_bound_len;
1037 const char *in;
1038 char *out;
1039
1040 in = d_name;
1041 in_len = d_len;
1042
1043 out_bound_len = PATH_MAX;
1044 out = kmalloc(out_bound_len, GFP_KERNEL);
1045 name = out;
1046
1047 for (i = 0; i < d_len; ++i) {
1048 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
1049 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
1050 linux_wchar_t uni;
1051 int nb;
1052
1053 nb = pSuperInfo->nls->char2uni(in, in_len, &uni);
1054 if (nb < 0) {
1055 LogFunc(("nls->char2uni failed %x %d\n",
1056 *in, in_len));
1057 err = -EINVAL;
1058 goto fail1;
1059 }
1060 in_len -= nb;
1061 in += nb;
1062
1063#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
1064 nb = utf32_to_utf8(uni, out, out_bound_len);
1065#else
1066 nb = utf8_wctomb(out, uni, out_bound_len);
1067#endif
1068 if (nb < 0) {
1069 LogFunc(("nls->uni2char failed %x %d\n",
1070 uni, out_bound_len));
1071 err = -EINVAL;
1072 goto fail1;
1073 }
1074 out_bound_len -= nb;
1075 out += nb;
1076 len += nb;
1077 }
1078 if (len >= PATH_MAX - 1) {
1079 err = -ENAMETOOLONG;
1080 goto fail1;
1081 }
1082
1083 LogFunc(("result(%d) = %.*s\n", len, len, name));
1084 *out = 0;
1085 } else {
1086 name = d_name;
1087 len = d_len;
1088 }
1089
1090 err = vbsf_make_path(caller, sf_i, name, len, result);
1091 if (name != d_name)
1092 kfree(name);
1093
1094 return err;
1095
1096 fail1:
1097 kfree(name);
1098 return err;
1099}
1100
1101
1102/**
1103 * This is called during name resolution/lookup to check if the @a dentry in the
1104 * cache is still valid. The actual validation is job is handled by
1105 * vbsf_inode_revalidate_worker().
1106 *
1107 * @note Caller holds no relevant locks, just a dentry reference.
1108 */
1109#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
1110static int vbsf_dentry_revalidate(struct dentry *dentry, unsigned flags)
1111#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1112static int vbsf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
1113#else
1114static int vbsf_dentry_revalidate(struct dentry *dentry, int flags)
1115#endif
1116{
1117#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
1118 int const flags = nd ? nd->flags : 0;
1119#endif
1120
1121 int rc;
1122
1123 Assert(dentry);
1124 SFLOGFLOW(("vbsf_dentry_revalidate: %p %#x %s\n", dentry, flags,
1125 dentry->d_inode ? VBSF_GET_INODE_INFO(dentry->d_inode)->path->String.ach : "<negative>"));
1126
1127 /*
1128 * See Documentation/filesystems/vfs.txt why we skip LOOKUP_RCU.
1129 *
1130 * Also recommended: https://lwn.net/Articles/649115/
1131 * https://lwn.net/Articles/649729/
1132 * https://lwn.net/Articles/650786/
1133 *
1134 */
1135#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
1136 if (flags & LOOKUP_RCU) {
1137 rc = -ECHILD;
1138 SFLOGFLOW(("vbsf_dentry_revalidate: RCU -> -ECHILD\n"));
1139 } else
1140#endif
1141 {
1142 /*
1143 * Do we have an inode or not? If not it's probably a negative cache
1144 * entry, otherwise most likely a positive one.
1145 */
1146 struct inode *pInode = dentry->d_inode;
1147 if (pInode) {
1148 /*
1149 * Positive entry.
1150 *
1151 * Note! We're more aggressive here than other remote file systems,
1152 * current (4.19) CIFS will for instance revalidate the inode
1153 * and ignore the dentry timestamp for positive entries.
1154 */
1155 unsigned long const cJiffiesAge = jiffies - vbsf_dentry_get_update_jiffies(dentry);
1156 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(dentry->d_sb);
1157 if (cJiffiesAge < pSuperInfo->cJiffiesDirCacheTTL) {
1158 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1159 rc = 1;
1160 } else if (!vbsf_inode_revalidate_worker(dentry, true /*fForced*/, false /*fInodeLocked*/)) {
1161 vbsf_dentry_set_update_jiffies(dentry, jiffies);
1162 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 1\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1163 rc = 1;
1164 } else {
1165 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 0\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1166 rc = 0;
1167 }
1168 } else {
1169 /*
1170 * Negative entry.
1171 *
1172 * Invalidate dentries for open and renames here as we'll revalidate
1173 * these when taking the actual action (also good for case preservation
1174 * if we do case-insensitive mounts against windows + mac hosts at some
1175 * later point).
1176 */
1177#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1178 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1179#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 75)
1180 if (flags & LOOKUP_CREATE)
1181#else
1182 if (0)
1183#endif
1184 {
1185 SFLOGFLOW(("vbsf_dentry_revalidate: negative: create or rename target -> 0\n"));
1186 rc = 0;
1187 } else {
1188 /* Can we skip revalidation based on TTL? */
1189 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
1190 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(dentry->d_sb);
1191 if (cJiffiesAge < pSuperInfo->cJiffiesDirCacheTTL) {
1192 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1193 rc = 1;
1194 } else {
1195 /* We could revalidate it here, but we could instead just
1196 have the caller kick it out. */
1197 /** @todo stat the direntry and see if it exists now. */
1198 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 0\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1199 rc = 0;
1200 }
1201 }
1202 }
1203 }
1204 return rc;
1205}
1206
1207#ifdef SFLOG_ENABLED
1208
1209/** For logging purposes only. */
1210# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
1211static int vbsf_dentry_delete(const struct dentry *pDirEntry)
1212# else
1213static int vbsf_dentry_delete(struct dentry *pDirEntry)
1214# endif
1215{
1216 SFLOGFLOW(("vbsf_dentry_delete: %p\n", pDirEntry));
1217 return 0;
1218}
1219
1220# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
1221/** For logging purposes only. */
1222static int vbsf_dentry_init(struct dentry *pDirEntry)
1223{
1224 SFLOGFLOW(("vbsf_dentry_init: %p\n", pDirEntry));
1225 return 0;
1226}
1227# endif
1228
1229#endif /* SFLOG_ENABLED */
1230
1231/**
1232 * Directory entry operations.
1233 *
1234 * Since 2.6.38 this is used via the super_block::s_d_op member.
1235 */
1236struct dentry_operations vbsf_dentry_ops = {
1237 .d_revalidate = vbsf_dentry_revalidate,
1238#ifdef SFLOG_ENABLED
1239 .d_delete = vbsf_dentry_delete,
1240# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
1241 .d_init = vbsf_dentry_init,
1242# endif
1243#endif
1244};
1245
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