VirtualBox

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

最後變更 在這個檔案從77559是 77559,由 vboxsync 提交於 6 年 前

linux/vboxsf: Reviewing revalidate and d_revalidate locking. bugref:9172

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 34.6 KB
 
1/* $Id: utils.c 77559 2019-03-04 16:19:23Z 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 *sf_g, char *name, size_t name_bound_len, const unsigned char *utf8_name, size_t utf8_len)
40{
41 if (sf_g->nls) {
42 const char *in;
43 char *out;
44 size_t out_len;
45 size_t out_bound_len;
46 size_t in_bound_len;
47
48 in = utf8_name;
49 in_bound_len = utf8_len;
50
51 out = name;
52 out_len = 0;
53 out_bound_len = name_bound_len;
54
55 while (in_bound_len) {
56 int nb;
57#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
58 unicode_t uni;
59
60 nb = utf8_to_utf32(in, in_bound_len, &uni);
61#else
62 linux_wchar_t uni;
63
64 nb = utf8_mbtowc(&uni, in, in_bound_len);
65#endif
66 if (nb < 0) {
67 LogFunc(("utf8_mbtowc failed(%s) %x:%d\n", (const char *)utf8_name, *in, in_bound_len));
68 return -EINVAL;
69 }
70 in += nb;
71 in_bound_len -= nb;
72
73 nb = sf_g->nls->uni2char(uni, out, out_bound_len);
74 if (nb < 0) {
75 LogFunc(("nls->uni2char failed(%s) %x:%d\n", utf8_name, uni, out_bound_len));
76 return nb;
77 }
78 out += nb;
79 out_bound_len -= nb;
80 out_len += nb;
81 }
82
83 *out = 0;
84 } else {
85 if (utf8_len + 1 > name_bound_len)
86 return -ENAMETOOLONG;
87
88 memcpy(name, utf8_name, utf8_len + 1);
89 }
90 return 0;
91}
92
93
94/**
95 * Convert from VBox to linux time.
96 */
97#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
98DECLINLINE(void) vbsf_time_to_linux(time_t *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
99{
100 int64_t t = RTTimeSpecGetNano(pVBoxSrc);
101 do_div(t, RT_NS_1SEC);
102 *pLinuxDst = t;
103}
104#else /* >= 2.6.0 */
105# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
106DECLINLINE(void) vbsf_time_to_linux(struct timespec *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
107# else
108DECLINLINE(void) vbsf_time_to_linux(struct timespec64 *pLinuxDst, PCRTTIMESPEC pVBoxSrc)
109# endif
110{
111 int64_t t = RTTimeSpecGetNano(pVBoxSrc);
112 pLinuxDst->tv_nsec = do_div(t, RT_NS_1SEC);
113 pLinuxDst->tv_sec = t;
114}
115#endif /* >= 2.6.0 */
116
117
118/**
119 * Convert from linux to VBox time.
120 */
121#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
122DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, time_t *pLinuxSrc)
123{
124 RTTimeSpecSetNano(pVBoxDst, RT_NS_1SEC_64 * *pLinuxSrc);
125}
126#else /* >= 2.6.0 */
127# if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
128DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, struct timespec const *pLinuxSrc)
129# else
130DECLINLINE(void) vbsf_time_to_vbox(PRTTIMESPEC pVBoxDst, struct timespec64 const *pLinuxSrc)
131# endif
132{
133 RTTimeSpecSetNano(pVBoxDst, pLinuxSrc->tv_nsec + pLinuxSrc->tv_sec * (int64_t)RT_NS_1SEC);
134}
135#endif /* >= 2.6.0 */
136
137
138/**
139 * Converts VBox access permissions to Linux ones (mode & 0777).
140 *
141 * @note Currently identical.
142 * @sa sf_access_permissions_to_vbox
143 */
144DECLINLINE(int) sf_access_permissions_to_linux(uint32_t fAttr)
145{
146 /* Access bits should be the same: */
147 AssertCompile(RTFS_UNIX_IRUSR == S_IRUSR);
148 AssertCompile(RTFS_UNIX_IWUSR == S_IWUSR);
149 AssertCompile(RTFS_UNIX_IXUSR == S_IXUSR);
150 AssertCompile(RTFS_UNIX_IRGRP == S_IRGRP);
151 AssertCompile(RTFS_UNIX_IWGRP == S_IWGRP);
152 AssertCompile(RTFS_UNIX_IXGRP == S_IXGRP);
153 AssertCompile(RTFS_UNIX_IROTH == S_IROTH);
154 AssertCompile(RTFS_UNIX_IWOTH == S_IWOTH);
155 AssertCompile(RTFS_UNIX_IXOTH == S_IXOTH);
156
157 return fAttr & RTFS_UNIX_ALL_ACCESS_PERMS;
158}
159
160
161/**
162 * Produce the Linux mode mask, given VBox, mount options and file type.
163 */
164DECLINLINE(int) sf_file_mode_to_linux(uint32_t fVBoxMode, int fFixedMode, int fClearMask, int fType)
165{
166 int fLnxMode = sf_access_permissions_to_linux(fVBoxMode);
167 if (fFixedMode != ~0)
168 fLnxMode = fFixedMode & 0777;
169 fLnxMode &= ~fClearMask;
170 fLnxMode |= fType;
171 return fLnxMode;
172}
173
174
175/**
176 * Initializes the @a inode attributes based on @a pObjInfo and @a sf_g options.
177 */
178void vbsf_init_inode(struct inode *inode, struct vbsf_inode_info *sf_i, PSHFLFSOBJINFO pObjInfo, struct vbsf_super_info *sf_g)
179{
180 PCSHFLFSOBJATTR pAttr = &pObjInfo->Attr;
181
182 TRACE();
183
184 sf_i->ts_up_to_date = jiffies;
185 sf_i->force_restat = 0;
186
187#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
188 inode->i_mapping->a_ops = &vbsf_reg_aops;
189# if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 19, 0)
190 inode->i_mapping->backing_dev_info = &sf_g->bdi; /* This is needed for mmap. */
191# endif
192#endif
193 if (RTFS_IS_DIRECTORY(pAttr->fMode)) {
194 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, sf_g->dmode, sf_g->dmask, S_IFDIR);
195 inode->i_op = &vbsf_dir_iops;
196 inode->i_fop = &vbsf_dir_fops;
197
198 /* XXX: this probably should be set to the number of entries
199 in the directory plus two (. ..) */
200 set_nlink(inode, 1);
201 }
202#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
203 else if (RTFS_IS_SYMLINK(pAttr->fMode)) {
204 /** @todo r=bird: Aren't System V symlinks w/o any mode mask? IIRC there is
205 * no lchmod on Linux. */
206 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFLNK);
207 inode->i_op = &vbsf_lnk_iops;
208 set_nlink(inode, 1);
209 }
210#endif
211 else {
212 inode->i_mode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFREG);
213 inode->i_op = &vbsf_reg_iops;
214 inode->i_fop = &vbsf_reg_fops;
215 set_nlink(inode, 1);
216 }
217
218#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
219 inode->i_uid = make_kuid(current_user_ns(), sf_g->uid);
220 inode->i_gid = make_kgid(current_user_ns(), sf_g->gid);
221#else
222 inode->i_uid = sf_g->uid;
223 inode->i_gid = sf_g->gid;
224#endif
225
226 inode->i_size = pObjInfo->cbObject;
227#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) && !defined(KERNEL_FC6)
228 inode->i_blksize = 4096;
229#endif
230#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 11)
231 inode->i_blkbits = 12;
232#endif
233 /* i_blocks always in units of 512 bytes! */
234 inode->i_blocks = (pObjInfo->cbAllocated + 511) / 512;
235
236 vbsf_time_to_linux(&inode->i_atime, &pObjInfo->AccessTime);
237 vbsf_time_to_linux(&inode->i_ctime, &pObjInfo->ChangeTime);
238 vbsf_time_to_linux(&inode->i_mtime, &pObjInfo->ModificationTime);
239 sf_i->BirthTime = pObjInfo->BirthTime;
240}
241
242
243/**
244 * Update the inode with new object info from the host.
245 *
246 * Called by sf_inode_revalidate() and sf_inode_revalidate_with_handle().
247 */
248void vbsf_update_inode(struct inode *pInode, struct vbsf_inode_info *pInodeInfo, PSHFLFSOBJINFO pObjInfo,
249 struct vbsf_super_info *sf_g, bool fInodeLocked)
250{
251 PCSHFLFSOBJATTR pAttr = &pObjInfo->Attr;
252 int fMode;
253
254 TRACE();
255
256#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
257 if (!fInodeLocked)
258 inode_lock(pInode);
259#endif
260
261 /*
262 * Calc new mode mask and update it if it changed.
263 */
264 if (RTFS_IS_DIRECTORY(pAttr->fMode))
265 fMode = sf_file_mode_to_linux(pAttr->fMode, sf_g->dmode, sf_g->dmask, S_IFDIR);
266#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
267 else if (RTFS_IS_SYMLINK(pAttr->fMode))
268 /** @todo r=bird: Aren't System V symlinks w/o any mode mask? IIRC there is
269 * no lchmod on Linux. */
270 fMode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFLNK);
271#endif
272 else
273 fMode = sf_file_mode_to_linux(pAttr->fMode, sf_g->fmode, sf_g->fmask, S_IFREG);
274
275 if (fMode == pInode->i_mode) {
276 /* likely */
277 } else {
278 if ((fMode & S_IFMT) == (pInode->i_mode & S_IFMT))
279 pInode->i_mode = fMode;
280 else {
281 SFLOGFLOW(("vbsf_update_inode: Changed from %o to %o (%s)\n",
282 pInode->i_mode & S_IFMT, fMode & S_IFMT, pInodeInfo->path->String.ach));
283 /** @todo we probably need to be more drastic... */
284 vbsf_init_inode(pInode, pInodeInfo, pObjInfo, sf_g);
285
286#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
287 if (!fInodeLocked)
288 inode_unlock(pInode);
289#endif
290 return;
291 }
292 }
293
294 /*
295 * Update the sizes.
296 * Note! i_blocks is always in units of 512 bytes!
297 */
298 pInode->i_blocks = (pObjInfo->cbAllocated + 511) / 512;
299 i_size_write(pInode, pObjInfo->cbObject);
300
301 /*
302 * Update the timestamps.
303 */
304 vbsf_time_to_linux(&pInode->i_atime, &pObjInfo->AccessTime);
305 vbsf_time_to_linux(&pInode->i_ctime, &pObjInfo->ChangeTime);
306 vbsf_time_to_linux(&pInode->i_mtime, &pObjInfo->ModificationTime);
307 pInodeInfo->BirthTime = pObjInfo->BirthTime;
308
309 /*
310 * Mark it as up to date.
311 */
312 pInodeInfo->ts_up_to_date = jiffies;
313 pInodeInfo->force_restat = 0;
314
315#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
316 if (!fInodeLocked)
317 inode_unlock(pInode);
318#endif
319}
320
321
322/** @note Currently only used for the root directory during (re-)mount. */
323int vbsf_stat(const char *caller, struct vbsf_super_info *sf_g, SHFLSTRING *path, PSHFLFSOBJINFO result, int ok_to_fail)
324{
325 int rc;
326 VBOXSFCREATEREQ *pReq;
327 NOREF(caller);
328
329 TRACE();
330
331 pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + path->u16Size);
332 if (pReq) {
333 RT_ZERO(*pReq);
334 memcpy(&pReq->StrPath, path, SHFLSTRING_HEADER_SIZE + path->u16Size);
335 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
336 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
337
338 LogFunc(("Calling VbglR0SfHostReqCreate on %s\n", path->String.utf8));
339 rc = VbglR0SfHostReqCreate(sf_g->map.root, pReq);
340 if (RT_SUCCESS(rc)) {
341 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
342 *result = pReq->CreateParms.Info;
343 rc = 0;
344 } else {
345 if (!ok_to_fail)
346 LogFunc(("VbglR0SfHostReqCreate on %s: file does not exist: %d (caller=%s)\n",
347 path->String.utf8, pReq->CreateParms.Result, caller));
348 rc = -ENOENT;
349 }
350 } else if (rc == VERR_INVALID_NAME) {
351 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
352 } else {
353 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc (caller=%s)\n", path->String.utf8, rc, caller));
354 rc = -EPROTO;
355 }
356 VbglR0PhysHeapFree(pReq);
357 }
358 else
359 rc = -ENOMEM;
360 return rc;
361}
362
363
364/**
365 * Revalidate an inode, inner worker.
366 *
367 * @sa sf_inode_revalidate()
368 */
369int vbsf_inode_revalidate_worker(struct dentry *dentry, bool fForced, bool fInodeLocked)
370{
371 int rc;
372 struct inode *pInode = dentry ? dentry->d_inode : NULL;
373 if (pInode) {
374 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
375 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(pInode->i_sb);
376 AssertReturn(sf_i, -EINVAL);
377 AssertReturn(sf_g, -EINVAL);
378
379 /*
380 * Can we get away without any action here?
381 */
382 if ( !fForced
383 && !sf_i->force_restat
384 && jiffies - sf_i->ts_up_to_date < sf_g->ttl)
385 rc = 0;
386 else {
387 /*
388 * No, we have to query the file info from the host.
389 * Try get a handle we can query, any kind of handle will do here.
390 */
391 struct vbsf_handle *pHandle = vbsf_handle_find(sf_i, 0, 0);
392 if (pHandle) {
393 /* Query thru pHandle. */
394 VBOXSFOBJINFOREQ *pReq = (VBOXSFOBJINFOREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
395 if (pReq) {
396 RT_ZERO(*pReq);
397 rc = VbglR0SfHostReqQueryObjInfo(sf_g->map.root, pReq, pHandle->hHost);
398 if (RT_SUCCESS(rc)) {
399 /*
400 * Reset the TTL and copy the info over into the inode structure.
401 */
402 vbsf_update_inode(pInode, sf_i, &pReq->ObjInfo, sf_g, fInodeLocked);
403 } else if (rc == VERR_INVALID_HANDLE) {
404 rc = -ENOENT; /* Restore.*/
405 } else {
406 LogFunc(("VbglR0SfHostReqQueryObjInfo failed on %#RX64: %Rrc\n", pHandle->hHost, rc));
407 rc = -RTErrConvertToErrno(rc);
408 }
409 VbglR0PhysHeapFree(pReq);
410 } else
411 rc = -ENOMEM;
412 vbsf_handle_release(pHandle, sf_g, "vbsf_inode_revalidate_worker");
413
414 } else {
415 /* Query via path. */
416 SHFLSTRING *pPath = sf_i->path;
417 VBOXSFCREATEREQ *pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + pPath->u16Size);
418 if (pReq) {
419 RT_ZERO(*pReq);
420 memcpy(&pReq->StrPath, pPath, SHFLSTRING_HEADER_SIZE + pPath->u16Size);
421 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
422 pReq->CreateParms.CreateFlags = SHFL_CF_LOOKUP | SHFL_CF_ACT_FAIL_IF_NEW;
423
424 rc = VbglR0SfHostReqCreate(sf_g->map.root, pReq);
425 if (RT_SUCCESS(rc)) {
426 if (pReq->CreateParms.Result == SHFL_FILE_EXISTS) {
427 /*
428 * Reset the TTL and copy the info over into the inode structure.
429 */
430 vbsf_update_inode(pInode, sf_i, &pReq->CreateParms.Info, sf_g, fInodeLocked);
431 rc = 0;
432 } else {
433 rc = -ENOENT;
434 }
435 } else if (rc == VERR_INVALID_NAME) {
436 rc = -ENOENT; /* this can happen for names like 'foo*' on a Windows host */
437 } else {
438 LogFunc(("VbglR0SfHostReqCreate failed on %s: %Rrc\n", pPath->String.ach, rc));
439 rc = -EPROTO;
440 }
441 VbglR0PhysHeapFree(pReq);
442 }
443 else
444 rc = -ENOMEM;
445 }
446 }
447 } else {
448 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, pInode));
449 rc = -EINVAL;
450 }
451 return rc;
452}
453
454
455#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 18)
456/**
457 * Revalidate an inode for 2.4.
458 *
459 * This is called in the stat(), lstat() and readlink() code paths. In the stat
460 * cases the caller will use the result afterwards to produce the stat data.
461 *
462 * @note 2.4.x has a getattr() inode operation too, but it is not used.
463 */
464int vbsf_inode_revalidate(struct dentry *dentry)
465{
466 /*
467 * We pretend the inode is locked here, as 2.4.x does not have inode level locking.
468 */
469 return vbsf_inode_revalidate_worker(dentry, false /*fForced*/, true /*fInodeLocked*/);
470}
471#endif /* < 2.5.18 */
472
473
474/**
475 * Similar to sf_inode_revalidate, but uses associated host file handle as that
476 * is quite a bit faster.
477 */
478int vbsf_inode_revalidate_with_handle(struct dentry *dentry, SHFLHANDLE hHostFile, bool fForced, bool fInodeLocked)
479{
480 int err;
481 struct inode *pInode = dentry ? dentry->d_inode : NULL;
482 if (!pInode) {
483 LogFunc(("no dentry(%p) or inode(%p)\n", dentry, pInode));
484 err = -EINVAL;
485 } else {
486 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
487 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(pInode->i_sb);
488 AssertReturn(sf_i, -EINVAL);
489 AssertReturn(sf_g, -EINVAL);
490
491 /*
492 * Can we get away without any action here?
493 */
494 if ( !fForced
495 && !sf_i->force_restat
496 && jiffies - sf_i->ts_up_to_date < sf_g->ttl)
497 err = 0;
498 else {
499 /*
500 * No, we have to query the file info from the host.
501 */
502 VBOXSFOBJINFOREQ *pReq = (VBOXSFOBJINFOREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
503 if (pReq) {
504 RT_ZERO(*pReq);
505 err = VbglR0SfHostReqQueryObjInfo(sf_g->map.root, pReq, hHostFile);
506 if (RT_SUCCESS(err)) {
507 /*
508 * Reset the TTL and copy the info over into the inode structure.
509 */
510 vbsf_update_inode(pInode, sf_i, &pReq->ObjInfo, sf_g, fInodeLocked);
511 } else {
512 LogFunc(("VbglR0SfHostReqQueryObjInfo failed on %#RX64: %Rrc\n", hHostFile, err));
513 err = -RTErrConvertToErrno(err);
514 }
515 VbglR0PhysHeapFree(pReq);
516 } else
517 err = -ENOMEM;
518 }
519 }
520 return err;
521}
522
523
524/* on 2.6 this is a proxy for [sf_inode_revalidate] which (as a side
525 effect) updates inode attributes for [dentry] (given that [dentry]
526 has inode at all) from these new attributes we derive [kstat] via
527 [generic_fillattr] */
528#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 18)
529
530# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
531int vbsf_inode_getattr(const struct path *path, struct kstat *kstat, u32 request_mask, unsigned int flags)
532# else
533int vbsf_inode_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *kstat)
534# endif
535{
536 int rc;
537# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
538 struct dentry *dentry = path->dentry;
539# endif
540
541# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
542 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p request_mask=%#x flags=%#x\n", dentry, request_mask, flags));
543# else
544 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p\n", dentry));
545# endif
546
547# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
548 /*
549 * With the introduction of statx() userland can control whether we
550 * update the inode information or not.
551 */
552 switch (flags & AT_STATX_SYNC_TYPE) {
553 default:
554 rc = vbsf_inode_revalidate_worker(dentry, false /*fForced*/, false /*fInodeLocked*/);
555 break;
556
557 case AT_STATX_FORCE_SYNC:
558 rc = vbsf_inode_revalidate_worker(dentry, true /*fForced*/, false /*fInodeLocked*/);
559 break;
560
561 case AT_STATX_DONT_SYNC:
562 rc = 0;
563 break;
564 }
565# else
566 rc = vbsf_inode_revalidate_worker(dentry, false /*fForced*/, false /*fInodeLocked*/);
567# endif
568 if (rc == 0) {
569 /* Do generic filling in of info. */
570 generic_fillattr(dentry->d_inode, kstat);
571
572 /* Add birth time. */
573#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
574 if (dentry->d_inode) {
575 struct vbsf_inode_info *pInodeInfo = VBSF_GET_INODE_INFO(dentry->d_inode);
576 if (pInodeInfo) {
577 vbsf_time_to_linux(&kstat->btime, &pInodeInfo->BirthTime);
578 kstat->result_mask |= STATX_BTIME;
579 }
580 }
581#endif
582
583 /*
584 * FsPerf shows the following numbers for sequential file access against
585 * a tmpfs folder on an AMD 1950X host running debian buster/sid:
586 *
587 * block size = r128600 ----- r128755 -----
588 * reads reads writes
589 * 4096 KB = 2254 MB/s 4953 MB/s 3668 MB/s
590 * 2048 KB = 2368 MB/s 4908 MB/s 3541 MB/s
591 * 1024 KB = 2208 MB/s 4011 MB/s 3291 MB/s
592 * 512 KB = 1908 MB/s 3399 MB/s 2721 MB/s
593 * 256 KB = 1625 MB/s 2679 MB/s 2251 MB/s
594 * 128 KB = 1413 MB/s 1967 MB/s 1684 MB/s
595 * 64 KB = 1152 MB/s 1409 MB/s 1265 MB/s
596 * 32 KB = 726 MB/s 815 MB/s 783 MB/s
597 * 16 KB = 683 MB/s 475 MB/s
598 * 8 KB = 294 MB/s 286 MB/s
599 * 4 KB = 145 MB/s 156 MB/s 149 MB/s
600 *
601 */
602 if (S_ISREG(kstat->mode))
603 kstat->blksize = _1M;
604 else if (S_ISDIR(kstat->mode))
605 /** @todo this may need more tuning after we rewrite the directory handling. */
606 kstat->blksize = _16K;
607 }
608 return rc;
609}
610
611
612int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr)
613{
614 struct vbsf_super_info *sf_g;
615 struct vbsf_inode_info *sf_i;
616 union SetAttrReqs
617 {
618 VBOXSFCREATEREQ Create;
619 VBOXSFOBJINFOREQ Info;
620 VBOXSFSETFILESIZEREQ SetSize;
621 VBOXSFCLOSEREQ Close;
622 } *pReq;
623 size_t cbReq;
624 SHFLHANDLE hHostFile;
625 int vrc;
626 int err = 0;
627
628 TRACE();
629
630 sf_g = VBSF_GET_SUPER_INFO(dentry->d_inode->i_sb);
631 sf_i = VBSF_GET_INODE_INFO(dentry->d_inode);
632 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
633 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
634 if (!pReq) {
635 LogFunc(("Failed to allocate %#x byte request buffer!\n", cbReq));
636 return -ENOMEM;
637 }
638
639 RT_ZERO(pReq->Create.CreateParms);
640 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
641 pReq->Create.CreateParms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
642 | SHFL_CF_ACT_FAIL_IF_NEW
643 | SHFL_CF_ACCESS_ATTR_WRITE;
644
645 /* this is at least required for Posix hosts */
646 if (iattr->ia_valid & ATTR_SIZE)
647 pReq->Create.CreateParms.CreateFlags |= SHFL_CF_ACCESS_WRITE;
648
649 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
650 vrc = VbglR0SfHostReqCreate(sf_g->map.root, &pReq->Create);
651 if (RT_SUCCESS(vrc)) {
652 hHostFile = pReq->Create.CreateParms.Handle;
653 } else {
654 err = -RTErrConvertToErrno(vrc);
655 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
656 goto fail2;
657 }
658 if (pReq->Create.CreateParms.Result != SHFL_FILE_EXISTS) {
659 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
660 err = -ENOENT;
661 goto fail1;
662 }
663
664 /* Setting the file size and setting the other attributes has to be
665 * handled separately, see implementation of vbsfSetFSInfo() in
666 * vbsf.cpp */
667 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME)) {
668 RT_ZERO(pReq->Info.ObjInfo);
669
670 if (iattr->ia_valid & ATTR_MODE) {
671 pReq->Info.ObjInfo.Attr.fMode = sf_access_permissions_to_vbox(iattr->ia_mode);
672 if (iattr->ia_mode & S_IFDIR)
673 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_DIRECTORY;
674 else if (iattr->ia_mode & S_IFLNK)
675 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_SYMLINK;
676 else
677 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_FILE;
678 }
679
680 if (iattr->ia_valid & ATTR_ATIME)
681 vbsf_time_to_vbox(&pReq->Info.ObjInfo.AccessTime, &iattr->ia_atime);
682 if (iattr->ia_valid & ATTR_MTIME)
683 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ModificationTime, &iattr->ia_mtime);
684 /* ignore ctime (inode change time) as it can't be set from userland anyway */
685
686 vrc = VbglR0SfHostReqSetObjInfo(sf_g->map.root, &pReq->Info, hHostFile);
687 if (RT_FAILURE(vrc)) {
688 err = -RTErrConvertToErrno(vrc);
689 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc err=%d\n", sf_i->path->String.ach, vrc, err));
690 goto fail1;
691 }
692 }
693
694 if (iattr->ia_valid & ATTR_SIZE) {
695 vrc = VbglR0SfHostReqSetFileSize(sf_g->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
696 /** @todo Implement fallback if host is < 6.0? */
697 if (RT_FAILURE(vrc)) {
698 err = -RTErrConvertToErrno(vrc);
699 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc err=%d\n",
700 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, err));
701 goto fail1;
702 }
703 }
704
705 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
706 if (RT_FAILURE(vrc))
707 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
708 VbglR0PhysHeapFree(pReq);
709
710 /** @todo r=bird: I guess we're calling revalidate here to update the inode
711 * info. However, due to the TTL optimization this is not guarenteed to happen.
712 *
713 * Also, we already have accurate stat information on the file, either from the
714 * SHFL_FN_CREATE call or from SHFL_FN_INFORMATION, so there is no need to do a
715 * slow stat()-like operation to retrieve the information again.
716 *
717 * What's more, given that the SHFL_FN_CREATE call succeeded, we know that the
718 * dentry and all its parent entries are valid and could touch their timestamps
719 * extending their TTL (CIFS does that). */
720 return vbsf_inode_revalidate_worker(dentry, true /*fForced*/, true /*fInodeLocked*/);
721
722 fail1:
723 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
724 if (RT_FAILURE(vrc))
725 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc; err=%d\n", sf_i->path->String.utf8, hHostFile, vrc, err));
726
727 fail2:
728 VbglR0PhysHeapFree(pReq);
729 return err;
730}
731
732#endif /* >= 2.5.18 */
733
734static int vbsf_make_path(const char *caller, struct vbsf_inode_info *sf_i,
735 const char *d_name, size_t d_len, SHFLSTRING **result)
736{
737 size_t path_len, shflstring_len;
738 SHFLSTRING *tmp;
739 uint16_t p_len;
740 uint8_t *p_name;
741 int fRoot = 0;
742
743 TRACE();
744 p_len = sf_i->path->u16Length;
745 p_name = sf_i->path->String.utf8;
746
747 if (p_len == 1 && *p_name == '/') {
748 path_len = d_len + 1;
749 fRoot = 1;
750 } else {
751 /* lengths of constituents plus terminating zero plus slash */
752 path_len = p_len + d_len + 2;
753 if (path_len > 0xffff) {
754 LogFunc(("path too long. caller=%s, path_len=%zu\n",
755 caller, path_len));
756 return -ENAMETOOLONG;
757 }
758 }
759
760 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
761 tmp = kmalloc(shflstring_len, GFP_KERNEL);
762 if (!tmp) {
763 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
764 return -ENOMEM;
765 }
766 tmp->u16Length = path_len - 1;
767 tmp->u16Size = path_len;
768
769 if (fRoot)
770 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
771 else {
772 memcpy(&tmp->String.utf8[0], p_name, p_len);
773 tmp->String.utf8[p_len] = '/';
774 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
775 tmp->String.utf8[p_len + 1 + d_len] = '\0';
776 }
777
778 *result = tmp;
779 return 0;
780}
781
782
783/**
784 * [dentry] contains string encoded in coding system that corresponds
785 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
786 * [vbsf_make_path] which will allocate SHFLSTRING and fill it in
787 */
788int vbsf_path_from_dentry(const char *caller, struct vbsf_super_info *sf_g, struct vbsf_inode_info *sf_i,
789 struct dentry *dentry, SHFLSTRING **result)
790{
791 int err;
792 const char *d_name;
793 size_t d_len;
794 const char *name;
795 size_t len = 0;
796
797 TRACE();
798 d_name = dentry->d_name.name;
799 d_len = dentry->d_name.len;
800
801 if (sf_g->nls) {
802 size_t in_len, i, out_bound_len;
803 const char *in;
804 char *out;
805
806 in = d_name;
807 in_len = d_len;
808
809 out_bound_len = PATH_MAX;
810 out = kmalloc(out_bound_len, GFP_KERNEL);
811 name = out;
812
813 for (i = 0; i < d_len; ++i) {
814 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
815 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
816 linux_wchar_t uni;
817 int nb;
818
819 nb = sf_g->nls->char2uni(in, in_len, &uni);
820 if (nb < 0) {
821 LogFunc(("nls->char2uni failed %x %d\n",
822 *in, in_len));
823 err = -EINVAL;
824 goto fail1;
825 }
826 in_len -= nb;
827 in += nb;
828
829#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
830 nb = utf32_to_utf8(uni, out, out_bound_len);
831#else
832 nb = utf8_wctomb(out, uni, out_bound_len);
833#endif
834 if (nb < 0) {
835 LogFunc(("nls->uni2char failed %x %d\n",
836 uni, out_bound_len));
837 err = -EINVAL;
838 goto fail1;
839 }
840 out_bound_len -= nb;
841 out += nb;
842 len += nb;
843 }
844 if (len >= PATH_MAX - 1) {
845 err = -ENAMETOOLONG;
846 goto fail1;
847 }
848
849 LogFunc(("result(%d) = %.*s\n", len, len, name));
850 *out = 0;
851 } else {
852 name = d_name;
853 len = d_len;
854 }
855
856 err = vbsf_make_path(caller, sf_i, name, len, result);
857 if (name != d_name)
858 kfree(name);
859
860 return err;
861
862 fail1:
863 kfree(name);
864 return err;
865}
866
867
868/**
869 * This is called during name resolution/lookup to check if the @a dentry in the
870 * cache is still valid. The actual validation is job is handled by
871 * vbsf_inode_revalidate_worker().
872 *
873 * @note Caller holds no relevant locks, just a dentry reference.
874 */
875#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
876static int vbsf_dentry_revalidate(struct dentry *dentry, unsigned flags)
877#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
878static int vbsf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
879#else
880static int vbsf_dentry_revalidate(struct dentry *dentry, int flags)
881#endif
882{
883#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
884 int const flags = nd ? nd->flags : 0;
885#endif
886
887 int rc;
888
889 Assert(dentry);
890 SFLOGFLOW(("vbsf_dentry_revalidate: %p %#x %s\n", dentry, flags,
891 dentry->d_inode ? VBSF_GET_INODE_INFO(dentry->d_inode)->path->String.ach : "<negative>"));
892
893 /*
894 * See Documentation/filesystems/vfs.txt why we skip LOOKUP_RCU.
895 *
896 * Also recommended: https://lwn.net/Articles/649115/
897 * https://lwn.net/Articles/649729/
898 * https://lwn.net/Articles/650786/
899 *
900 */
901#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
902 if (flags & LOOKUP_RCU) {
903 rc = -ECHILD;
904 SFLOGFLOW(("vbsf_dentry_revalidate: RCU -> -ECHILD\n"));
905 } else
906#endif
907 {
908 /*
909 * Do we have an inode or not? If not it's probably a negative cache
910 * entry, otherwise most likely a positive one.
911 */
912 struct inode *pInode = dentry->d_inode;
913 if (pInode) {
914 /*
915 * Positive entry.
916 *
917 * Note! We're more aggressive here than other remote file systems,
918 * current (4.19) CIFS will for instance revalidate the inode
919 * and ignore the dentry timestamp for positive entries.
920 */
921 //struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
922 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
923 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(dentry->d_sb);
924 if (cJiffiesAge < sf_g->ttl) {
925 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, sf_g->ttl));
926 rc = 1;
927 } else if (!vbsf_inode_revalidate_worker(dentry, true /*fForced*/, false /*fInodeLocked*/)) {
928 vbsf_dentry_set_update_jiffies(dentry, jiffies); /** @todo get jiffies from inode. */
929 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 1\n", cJiffiesAge, sf_g->ttl));
930 rc = 1;
931 } else {
932 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 0\n", cJiffiesAge, sf_g->ttl));
933 rc = 0;
934 }
935 } else {
936 /*
937 * Negative entry.
938 *
939 * Invalidate dentries for open and renames here as we'll revalidate
940 * these when taking the actual action (also good for case preservation
941 * if we do case-insensitive mounts against windows + mac hosts at some
942 * later point).
943 */
944#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
945 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
946#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 75)
947 if (flags & LOOKUP_CREATE)
948#else
949 if (0)
950#endif
951 {
952 SFLOGFLOW(("vbsf_dentry_revalidate: negative: create or rename target -> 0\n"));
953 rc = 0;
954 } else {
955 /* Can we skip revalidation based on TTL? */
956 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
957 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(dentry->d_sb);
958 if (cJiffiesAge < sf_g->ttl) {
959 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, sf_g->ttl));
960 rc = 1;
961 } else {
962 /* We could revalidate it here, but we could instead just
963 have the caller kick it out. */
964 /** @todo stat the direntry and see if it exists now. */
965 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 0\n", cJiffiesAge, sf_g->ttl));
966 rc = 0;
967 }
968 }
969 }
970 }
971 return rc;
972}
973
974#ifdef SFLOG_ENABLED
975
976/** For logging purposes only. */
977# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
978static int vbsf_dentry_delete(const struct dentry *pDirEntry)
979# else
980static int vbsf_dentry_delete(struct dentry *pDirEntry)
981# endif
982{
983 SFLOGFLOW(("vbsf_dentry_delete: %p\n", pDirEntry));
984 return 0;
985}
986
987# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
988/** For logging purposes only. */
989static int vbsf_dentry_init(struct dentry *pDirEntry)
990{
991 SFLOGFLOW(("vbsf_dentry_init: %p\n", pDirEntry));
992 return 0;
993}
994# endif
995
996#endif /* SFLOG_ENABLED */
997
998/**
999 * Directory entry operations.
1000 *
1001 * Since 2.6.38 this is used via the super_block::s_d_op member.
1002 */
1003struct dentry_operations vbsf_dentry_ops = {
1004 .d_revalidate = vbsf_dentry_revalidate,
1005#ifdef SFLOG_ENABLED
1006 .d_delete = vbsf_dentry_delete,
1007# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
1008 .d_init = vbsf_dentry_init,
1009# endif
1010#endif
1011};
1012
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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