VirtualBox

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

Last change on this file since 99425 was 99420, checked in by vboxsync, 20 months ago

linux/vboxsf: s/VBOX_LINUX_MEMCPY/VBSF_UNFORTIFIED_MEMCPY/g + explanation why and when to use. bugref:10209 ticketref:21410

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