VirtualBox

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

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

add/linux/vboxsf: ticketref:17299 inode_change_ok() gone in 4.1.37-4.2.0

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