VirtualBox

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

Last change on this file since 96588 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.3 KB
Line 
1/* $Id: utils.c 96407 2022-08-22 17:43:14Z 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-2022 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 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 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 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
701# if RTLNX_VER_MIN(5,12,0)
702int vbsf_inode_getattr(struct user_namespace *ns, const struct path *path,
703 struct kstat *kstat, u32 request_mask, unsigned int flags)
704# elif RTLNX_VER_MIN(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 RTLNX_VER_MIN(4,11,0)
712 struct dentry *dentry = path->dentry;
713# endif
714
715# if RTLNX_VER_MIN(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 RTLNX_VER_MIN(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# if RTLNX_VER_MIN(5,12,0)
745 generic_fillattr(ns, dentry->d_inode, kstat);
746# else
747 generic_fillattr(dentry->d_inode, kstat);
748# endif
749
750 /* Add birth time. */
751# if RTLNX_VER_MIN(4,11,0)
752 if (dentry->d_inode) {
753 struct vbsf_inode_info *pInodeInfo = VBSF_GET_INODE_INFO(dentry->d_inode);
754 if (pInodeInfo) {
755 vbsf_time_to_linux(&kstat->btime, &pInodeInfo->BirthTime);
756 kstat->result_mask |= STATX_BTIME;
757 }
758 }
759# endif
760
761 /*
762 * FsPerf shows the following numbers for sequential file access against
763 * a tmpfs folder on an AMD 1950X host running debian buster/sid:
764 *
765 * block size = r128600 ----- r128755 -----
766 * reads reads writes
767 * 4096 KB = 2254 MB/s 4953 MB/s 3668 MB/s
768 * 2048 KB = 2368 MB/s 4908 MB/s 3541 MB/s
769 * 1024 KB = 2208 MB/s 4011 MB/s 3291 MB/s
770 * 512 KB = 1908 MB/s 3399 MB/s 2721 MB/s
771 * 256 KB = 1625 MB/s 2679 MB/s 2251 MB/s
772 * 128 KB = 1413 MB/s 1967 MB/s 1684 MB/s
773 * 64 KB = 1152 MB/s 1409 MB/s 1265 MB/s
774 * 32 KB = 726 MB/s 815 MB/s 783 MB/s
775 * 16 KB = 683 MB/s 475 MB/s
776 * 8 KB = 294 MB/s 286 MB/s
777 * 4 KB = 145 MB/s 156 MB/s 149 MB/s
778 *
779 */
780 if (S_ISREG(kstat->mode))
781 kstat->blksize = _1M;
782 else if (S_ISDIR(kstat->mode))
783 /** @todo this may need more tuning after we rewrite the directory handling. */
784 kstat->blksize = _16K;
785 }
786 return rc;
787}
788#endif /* >= 2.5.18 */
789
790
791/**
792 * Modify inode attributes.
793 */
794#if RTLNX_VER_MIN(5,12,0)
795int vbsf_inode_setattr(struct user_namespace *ns, struct dentry *dentry, struct iattr *iattr)
796#else
797int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr)
798#endif
799{
800 struct inode *pInode = dentry->d_inode;
801 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(pInode->i_sb);
802 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
803 int vrc;
804 int rc;
805
806 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p inode=%p ia_valid=%#x %s\n",
807 dentry, pInode, iattr->ia_valid, sf_i ? sf_i->path->String.ach : NULL));
808 AssertReturn(sf_i, -EINVAL);
809
810 /*
811 * Do minimal attribute permission checks. We set ATTR_FORCE since we cannot
812 * preserve ownership and such and would end up with EPERM here more often than
813 * we would like. For instance it would cause 'cp' to complain about EPERM
814 * from futimes() when asked to preserve times, see ticketref:18569.
815 */
816 iattr->ia_valid |= ATTR_FORCE;
817#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)
818# if RTLNX_VER_MIN(5,12,0)
819 rc = setattr_prepare(ns, dentry, iattr);
820# else
821 rc = setattr_prepare(dentry, iattr);
822# endif
823#else
824 rc = inode_change_ok(pInode, iattr);
825#endif
826 if (rc == 0) {
827 /*
828 * Don't modify MTIME and CTIME for open(O_TRUNC) and ftruncate, those
829 * operations will set those timestamps automatically. Saves a host call.
830 */
831 unsigned fAttrs = iattr->ia_valid;
832#if RTLNX_VER_MIN(2,6,15)
833 fAttrs &= ~ATTR_FILE;
834#endif
835 if ( fAttrs == (ATTR_SIZE | ATTR_MTIME | ATTR_CTIME)
836#if RTLNX_VER_MIN(2,6,24)
837 || (fAttrs & (ATTR_OPEN | ATTR_SIZE)) == (ATTR_OPEN | ATTR_SIZE)
838#endif
839 )
840 fAttrs &= ~(ATTR_MTIME | ATTR_CTIME);
841
842 /*
843 * We only implement a handful of attributes, so ignore any attempts
844 * at setting bits we don't support.
845 */
846 if (fAttrs & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE)) {
847 /*
848 * Try find a handle which allows us to modify the attributes, otherwise
849 * open the file/dir/whatever.
850 */
851 union SetAttrReqs
852 {
853 VBOXSFCREATEREQ Create;
854 VBOXSFOBJINFOREQ Info;
855 VBOXSFSETFILESIZEREQ SetSize;
856 VBOXSFCLOSEREQ Close;
857 } *pReq;
858 size_t cbReq;
859 SHFLHANDLE hHostFile;
860 /** @todo ATTR_FILE (2.6.15+) could be helpful here if we like. */
861 struct vbsf_handle *pHandle = fAttrs & ATTR_SIZE
862 ? vbsf_handle_find(sf_i, VBSF_HANDLE_F_WRITE, 0)
863 : vbsf_handle_find(sf_i, 0, 0);
864 if (pHandle) {
865 hHostFile = pHandle->hHost;
866 cbReq = RT_MAX(sizeof(VBOXSFOBJINFOREQ), sizeof(VBOXSFSETFILESIZEREQ));
867 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
868 if (pReq) {
869 /* likely */
870 } else
871 rc = -ENOMEM;
872 } else {
873 hHostFile = SHFL_HANDLE_NIL;
874 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
875 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
876 if (pReq) {
877 RT_ZERO(pReq->Create.CreateParms);
878 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
879 pReq->Create.CreateParms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
880 | SHFL_CF_ACT_FAIL_IF_NEW
881 | SHFL_CF_ACCESS_ATTR_WRITE;
882 if (fAttrs & ATTR_SIZE)
883 pReq->Create.CreateParms.CreateFlags |= SHFL_CF_ACCESS_WRITE;
884 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
885 vrc = VbglR0SfHostReqCreate(pSuperInfo->map.root, &pReq->Create);
886 if (RT_SUCCESS(vrc)) {
887 if (pReq->Create.CreateParms.Result == SHFL_FILE_EXISTS) {
888 hHostFile = pReq->Create.CreateParms.Handle;
889 Assert(hHostFile != SHFL_HANDLE_NIL);
890 vbsf_dentry_chain_increase_ttl(dentry);
891 } else {
892 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
893 vbsf_dentry_invalidate_ttl(dentry);
894 sf_i->force_restat = true;
895 rc = -ENOENT;
896 }
897 } else {
898 rc = -RTErrConvertToErrno(vrc);
899 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc rc=%d\n", sf_i->path->String.ach, vrc, rc));
900 }
901 } else
902 rc = -ENOMEM;
903 }
904 if (rc == 0) {
905 /*
906 * Set mode and/or timestamps.
907 */
908 if (fAttrs & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) {
909 /* Fill in the attributes. Start by setting all to zero
910 since the host will ignore zeroed fields. */
911 RT_ZERO(pReq->Info.ObjInfo);
912
913 if (fAttrs & ATTR_MODE) {
914 pReq->Info.ObjInfo.Attr.fMode = sf_access_permissions_to_vbox(iattr->ia_mode);
915 if (iattr->ia_mode & S_IFDIR)
916 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_DIRECTORY;
917 else if (iattr->ia_mode & S_IFLNK)
918 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_SYMLINK;
919 else
920 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_FILE;
921 }
922 if (fAttrs & ATTR_ATIME)
923 vbsf_time_to_vbox(&pReq->Info.ObjInfo.AccessTime, &iattr->ia_atime);
924 if (fAttrs & ATTR_MTIME)
925 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ModificationTime, &iattr->ia_mtime);
926 if (fAttrs & ATTR_CTIME)
927 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ChangeTime, &iattr->ia_ctime);
928
929 /* Make the change. */
930 vrc = VbglR0SfHostReqSetObjInfo(pSuperInfo->map.root, &pReq->Info, hHostFile);
931 if (RT_SUCCESS(vrc)) {
932 vbsf_update_inode(pInode, sf_i, &pReq->Info.ObjInfo, pSuperInfo, true /*fLocked*/, fAttrs);
933 } else {
934 rc = -RTErrConvertToErrno(vrc);
935 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc rc=%d\n", sf_i->path->String.ach, vrc, rc));
936 }
937 }
938
939 /*
940 * Change the file size.
941 * Note! Old API is more convenient here as it gives us up to date
942 * inode info back.
943 */
944 if ((fAttrs & ATTR_SIZE) && rc == 0) {
945 /*vrc = VbglR0SfHostReqSetFileSize(pSuperInfo->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
946 if (RT_SUCCESS(vrc)) {
947 i_size_write(pInode, iattr->ia_size);
948 } else if (vrc == VERR_NOT_IMPLEMENTED)*/ {
949 /* Fallback for pre 6.0 hosts: */
950 RT_ZERO(pReq->Info.ObjInfo);
951 pReq->Info.ObjInfo.cbObject = iattr->ia_size;
952 vrc = VbglR0SfHostReqSetFileSizeOld(pSuperInfo->map.root, &pReq->Info, hHostFile);
953 if (RT_SUCCESS(vrc))
954 vbsf_update_inode(pInode, sf_i, &pReq->Info.ObjInfo, pSuperInfo, true /*fLocked*/, fAttrs);
955 }
956 if (RT_SUCCESS(vrc)) {
957 /** @todo there is potentially more to be done here if there are mappings of
958 * the lovely file. */
959 } else {
960 rc = -RTErrConvertToErrno(vrc);
961 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc rc=%d\n",
962 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, rc));
963 }
964 }
965
966 /*
967 * Clean up.
968 */
969 if (!pHandle) {
970 vrc = VbglR0SfHostReqClose(pSuperInfo->map.root, &pReq->Close, hHostFile);
971 if (RT_FAILURE(vrc))
972 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
973 }
974 }
975 if (pReq)
976 VbglR0PhysHeapFree(pReq);
977 if (pHandle)
978 vbsf_handle_release(pHandle, pSuperInfo, "vbsf_inode_setattr");
979 } else
980 SFLOGFLOW(("vbsf_inode_setattr: Nothing to do here: %#x (was %#x).\n", fAttrs, iattr->ia_valid));
981 }
982 return rc;
983}
984
985
986static int vbsf_make_path(const char *caller, struct vbsf_inode_info *sf_i,
987 const char *d_name, size_t d_len, SHFLSTRING **result)
988{
989 size_t path_len, shflstring_len;
990 SHFLSTRING *tmp;
991 uint16_t p_len;
992 uint8_t *p_name;
993 int fRoot = 0;
994
995 TRACE();
996 p_len = sf_i->path->u16Length;
997 p_name = sf_i->path->String.utf8;
998
999 if (p_len == 1 && *p_name == '/') {
1000 path_len = d_len + 1;
1001 fRoot = 1;
1002 } else {
1003 /* lengths of constituents plus terminating zero plus slash */
1004 path_len = p_len + d_len + 2;
1005 if (path_len > 0xffff) {
1006 LogFunc(("path too long. caller=%s, path_len=%zu\n",
1007 caller, path_len));
1008 return -ENAMETOOLONG;
1009 }
1010 }
1011
1012 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
1013 tmp = kmalloc(shflstring_len, GFP_KERNEL);
1014 if (!tmp) {
1015 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
1016 return -ENOMEM;
1017 }
1018 tmp->u16Length = path_len - 1;
1019 tmp->u16Size = path_len;
1020
1021 if (fRoot)
1022 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
1023 else {
1024 memcpy(&tmp->String.utf8[0], p_name, p_len);
1025 tmp->String.utf8[p_len] = '/';
1026 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
1027 tmp->String.utf8[p_len + 1 + d_len] = '\0';
1028 }
1029
1030 *result = tmp;
1031 return 0;
1032}
1033
1034
1035/**
1036 * [dentry] contains string encoded in coding system that corresponds
1037 * to [pSuperInfo]->nls, we must convert it to UTF8 here and pass down to
1038 * [vbsf_make_path] which will allocate SHFLSTRING and fill it in
1039 */
1040int vbsf_path_from_dentry(struct vbsf_super_info *pSuperInfo, struct vbsf_inode_info *sf_i, struct dentry *dentry,
1041 SHFLSTRING **result, const char *caller)
1042{
1043 int err;
1044 const char *d_name;
1045 size_t d_len;
1046 const char *name;
1047 size_t len = 0;
1048
1049 TRACE();
1050 d_name = dentry->d_name.name;
1051 d_len = dentry->d_name.len;
1052
1053 if (pSuperInfo->nls) {
1054 size_t in_len, i, out_bound_len;
1055 const char *in;
1056 char *out;
1057
1058 in = d_name;
1059 in_len = d_len;
1060
1061 out_bound_len = PATH_MAX;
1062 out = kmalloc(out_bound_len, GFP_KERNEL);
1063 name = out;
1064
1065 for (i = 0; i < d_len; ++i) {
1066 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
1067 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
1068 linux_wchar_t uni;
1069 int nb;
1070
1071 nb = pSuperInfo->nls->char2uni(in, in_len, &uni);
1072 if (nb < 0) {
1073 LogFunc(("nls->char2uni failed %x %d\n",
1074 *in, in_len));
1075 err = -EINVAL;
1076 goto fail1;
1077 }
1078 in_len -= nb;
1079 in += nb;
1080
1081#if RTLNX_VER_MIN(2,6,31)
1082 nb = utf32_to_utf8(uni, out, out_bound_len);
1083#else
1084 nb = utf8_wctomb(out, uni, out_bound_len);
1085#endif
1086 if (nb < 0) {
1087 LogFunc(("nls->uni2char failed %x %d\n",
1088 uni, out_bound_len));
1089 err = -EINVAL;
1090 goto fail1;
1091 }
1092 out_bound_len -= nb;
1093 out += nb;
1094 len += nb;
1095 }
1096 if (len >= PATH_MAX - 1) {
1097 err = -ENAMETOOLONG;
1098 goto fail1;
1099 }
1100
1101 LogFunc(("result(%d) = %.*s\n", len, len, name));
1102 *out = 0;
1103 } else {
1104 name = d_name;
1105 len = d_len;
1106 }
1107
1108 err = vbsf_make_path(caller, sf_i, name, len, result);
1109 if (name != d_name)
1110 kfree(name);
1111
1112 return err;
1113
1114 fail1:
1115 kfree(name);
1116 return err;
1117}
1118
1119
1120/**
1121 * This is called during name resolution/lookup to check if the @a dentry in the
1122 * cache is still valid. The actual validation is job is handled by
1123 * vbsf_inode_revalidate_worker().
1124 *
1125 * @note Caller holds no relevant locks, just a dentry reference.
1126 */
1127#if RTLNX_VER_MIN(3,6,0)
1128static int vbsf_dentry_revalidate(struct dentry *dentry, unsigned flags)
1129#elif RTLNX_VER_MIN(2,6,0)
1130static int vbsf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
1131#else
1132static int vbsf_dentry_revalidate(struct dentry *dentry, int flags)
1133#endif
1134{
1135#if RTLNX_VER_RANGE(2,6,0, 3,6,0)
1136 int const flags = nd ? nd->flags : 0;
1137#endif
1138
1139 int rc;
1140
1141 Assert(dentry);
1142 SFLOGFLOW(("vbsf_dentry_revalidate: %p %#x %s\n", dentry, flags,
1143 dentry->d_inode ? VBSF_GET_INODE_INFO(dentry->d_inode)->path->String.ach : "<negative>"));
1144
1145 /*
1146 * See Documentation/filesystems/vfs.txt why we skip LOOKUP_RCU.
1147 *
1148 * Also recommended: https://lwn.net/Articles/649115/
1149 * https://lwn.net/Articles/649729/
1150 * https://lwn.net/Articles/650786/
1151 *
1152 */
1153#if RTLNX_VER_MIN(2,6,38)
1154 if (flags & LOOKUP_RCU) {
1155 rc = -ECHILD;
1156 SFLOGFLOW(("vbsf_dentry_revalidate: RCU -> -ECHILD\n"));
1157 } else
1158#endif
1159 {
1160 /*
1161 * Do we have an inode or not? If not it's probably a negative cache
1162 * entry, otherwise most likely a positive one.
1163 */
1164 struct inode *pInode = dentry->d_inode;
1165 if (pInode) {
1166 /*
1167 * Positive entry.
1168 *
1169 * Note! We're more aggressive here than other remote file systems,
1170 * current (4.19) CIFS will for instance revalidate the inode
1171 * and ignore the dentry timestamp for positive entries.
1172 */
1173 unsigned long const cJiffiesAge = jiffies - vbsf_dentry_get_update_jiffies(dentry);
1174 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(dentry->d_sb);
1175 if (cJiffiesAge < pSuperInfo->cJiffiesDirCacheTTL) {
1176 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1177 rc = 1;
1178 } else if (!vbsf_inode_revalidate_worker(dentry, true /*fForced*/, false /*fInodeLocked*/)) {
1179 vbsf_dentry_set_update_jiffies(dentry, jiffies);
1180 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 1\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1181 rc = 1;
1182 } else {
1183 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 0\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1184 rc = 0;
1185 }
1186 } else {
1187 /*
1188 * Negative entry.
1189 *
1190 * Invalidate dentries for open and renames here as we'll revalidate
1191 * these when taking the actual action (also good for case preservation
1192 * if we do case-insensitive mounts against windows + mac hosts at some
1193 * later point).
1194 */
1195#if RTLNX_VER_MIN(2,6,28)
1196 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1197#elif RTLNX_VER_MIN(2,5,75)
1198 if (flags & LOOKUP_CREATE)
1199#else
1200 if (0)
1201#endif
1202 {
1203 SFLOGFLOW(("vbsf_dentry_revalidate: negative: create or rename target -> 0\n"));
1204 rc = 0;
1205 } else {
1206 /* Can we skip revalidation based on TTL? */
1207 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
1208 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(dentry->d_sb);
1209 if (cJiffiesAge < pSuperInfo->cJiffiesDirCacheTTL) {
1210 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1211 rc = 1;
1212 } else {
1213 /* We could revalidate it here, but we could instead just
1214 have the caller kick it out. */
1215 /** @todo stat the direntry and see if it exists now. */
1216 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 0\n", cJiffiesAge, pSuperInfo->cJiffiesDirCacheTTL));
1217 rc = 0;
1218 }
1219 }
1220 }
1221 }
1222 return rc;
1223}
1224
1225#ifdef SFLOG_ENABLED
1226
1227/** For logging purposes only. */
1228# if RTLNX_VER_MIN(2,6,38)
1229static int vbsf_dentry_delete(const struct dentry *pDirEntry)
1230# else
1231static int vbsf_dentry_delete(struct dentry *pDirEntry)
1232# endif
1233{
1234 SFLOGFLOW(("vbsf_dentry_delete: %p\n", pDirEntry));
1235 return 0;
1236}
1237
1238# if RTLNX_VER_MIN(4,8,0)
1239/** For logging purposes only. */
1240static int vbsf_dentry_init(struct dentry *pDirEntry)
1241{
1242 SFLOGFLOW(("vbsf_dentry_init: %p\n", pDirEntry));
1243 return 0;
1244}
1245# endif
1246
1247#endif /* SFLOG_ENABLED */
1248
1249/**
1250 * Directory entry operations.
1251 *
1252 * Since 2.6.38 this is used via the super_block::s_d_op member.
1253 */
1254struct dentry_operations vbsf_dentry_ops = {
1255 .d_revalidate = vbsf_dentry_revalidate,
1256#ifdef SFLOG_ENABLED
1257 .d_delete = vbsf_dentry_delete,
1258# if RTLNX_VER_MIN(4,8,0)
1259 .d_init = vbsf_dentry_init,
1260# endif
1261#endif
1262};
1263
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