VirtualBox

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

Last change on this file since 88034 was 85698, checked in by vboxsync, 5 years ago

IPRT,lnx-kmods: Use new linux kernel version checking macros. Moved them to separate wrapper header.

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