VirtualBox

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

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

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