VirtualBox

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

最後變更 在這個檔案從102795是 101359,由 vboxsync 提交於 16 月 前

Additions: Linux: Replace VBSF_UNFORTIFIED_MEMCPY with RT_BCOPY_UNFORTIFIED, bugref:10209.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette