VirtualBox

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

最後變更 在這個檔案從94081是 93115,由 vboxsync 提交於 3 年 前

scm --update-copyright-year

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

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