VirtualBox

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

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

linux/vboxsf: Fixed TTL calc bug in vbsf_dentry_revalidate(). bugref:9172

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 37.6 KB
 
1/* $Id: utils.c 77629 2019-03-10 02:58:14Z vboxsync $ */
2/** @file
3 * vboxsf - VBox Linux Shared Folders VFS, utility functions.
4 *
5 * Utility functions (mainly conversion from/to VirtualBox/Linux data structures).
6 */
7
8/*
9 * Copyright (C) 2006-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#endif /* >= 2.5.18 */
611
612
613/**
614 * Modify inode attributes.
615 */
616int vbsf_inode_setattr(struct dentry *dentry, struct iattr *iattr)
617{
618 struct inode *pInode = dentry->d_inode;
619 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(pInode->i_sb);
620 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
621 int vrc;
622 int rc;
623
624 SFLOGFLOW(("vbsf_inode_setattr: dentry=%p inode=%p ia_valid=%#x %s\n",
625 dentry, pInode, iattr->ia_valid, sf_i ? sf_i->path->String.ach : NULL));
626 AssertReturn(sf_i, -EINVAL);
627
628 /*
629 * Need to check whether the caller is allowed to modify the attributes or not.
630 */
631#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
632 rc = setattr_prepare(dentry, iattr);
633#else
634 rc = inode_change_ok(pInode, iattr);
635#endif
636 if (rc == 0) {
637 /*
638 * We only implement a handful of attributes, so ignore any attempts
639 * at setting bits we don't support.
640 */
641 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE)) {
642 /*
643 * Try find a handle which allows us to modify the attributes, otherwise
644 * open the file/dir/whatever.
645 */
646 union SetAttrReqs
647 {
648 VBOXSFCREATEREQ Create;
649 VBOXSFOBJINFOREQ Info;
650 VBOXSFSETFILESIZEREQ SetSize;
651 VBOXSFCLOSEREQ Close;
652 } *pReq;
653 size_t cbReq;
654 SHFLHANDLE hHostFile;
655 struct vbsf_handle *pHandle = iattr->ia_valid & ATTR_SIZE
656 ? vbsf_handle_find(sf_i, VBSF_HANDLE_F_WRITE, 0)
657 : vbsf_handle_find(sf_i, 0, 0);
658 if (pHandle) {
659 hHostFile = pHandle->hHost;
660 cbReq = RT_MAX(sizeof(VBOXSFOBJINFOREQ), sizeof(VBOXSFSETFILESIZEREQ));
661 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
662 if (pReq) {
663 /* likely */
664 } else
665 rc = -ENOMEM;
666 } else {
667 hHostFile = SHFL_HANDLE_NIL;
668 cbReq = RT_MAX(sizeof(pReq->Info), sizeof(pReq->Create) + SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
669 pReq = (union SetAttrReqs *)VbglR0PhysHeapAlloc(cbReq);
670 if (pReq) {
671 RT_ZERO(pReq->Create.CreateParms);
672 pReq->Create.CreateParms.Handle = SHFL_HANDLE_NIL;
673 pReq->Create.CreateParms.CreateFlags = SHFL_CF_ACT_OPEN_IF_EXISTS
674 | SHFL_CF_ACT_FAIL_IF_NEW
675 | SHFL_CF_ACCESS_ATTR_WRITE;
676 if (iattr->ia_valid & ATTR_SIZE)
677 pReq->Create.CreateParms.CreateFlags |= SHFL_CF_ACCESS_WRITE;
678 memcpy(&pReq->Create.StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
679 vrc = VbglR0SfHostReqCreate(sf_g->map.root, &pReq->Create);
680 if (RT_SUCCESS(vrc)) {
681 if (pReq->Create.CreateParms.Result == SHFL_FILE_EXISTS) {
682 hHostFile = pReq->Create.CreateParms.Handle;
683 Assert(hHostFile != SHFL_HANDLE_NIL);
684 vbsf_dentry_chain_increase_ttl(dentry);
685 } else {
686 LogFunc(("file %s does not exist\n", sf_i->path->String.utf8));
687 /** @todo */
688 rc = -ENOENT;
689 }
690 } else {
691 rc = -RTErrConvertToErrno(vrc);
692 LogFunc(("VbglR0SfCreate(%s) failed vrc=%Rrc rc=%d\n", sf_i->path->String.ach, vrc, rc));
693 }
694 } else
695 rc = -ENOMEM;
696 }
697 if (rc == 0) {
698 /*
699 * Set mode and/or timestamps.
700 */
701 if (iattr->ia_valid & (ATTR_MODE | ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) {
702 /* Fill in the attributes. Start by setting all to zero
703 since the host will ignore zeroed fields. */
704 RT_ZERO(pReq->Info.ObjInfo);
705
706 if (iattr->ia_valid & ATTR_MODE) {
707 pReq->Info.ObjInfo.Attr.fMode = sf_access_permissions_to_vbox(iattr->ia_mode);
708 if (iattr->ia_mode & S_IFDIR)
709 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_DIRECTORY;
710 else if (iattr->ia_mode & S_IFLNK)
711 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_SYMLINK;
712 else
713 pReq->Info.ObjInfo.Attr.fMode |= RTFS_TYPE_FILE;
714 }
715 if (iattr->ia_valid & ATTR_ATIME)
716 vbsf_time_to_vbox(&pReq->Info.ObjInfo.AccessTime, &iattr->ia_atime);
717 if (iattr->ia_valid & ATTR_MTIME)
718 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ModificationTime, &iattr->ia_mtime);
719 if (iattr->ia_valid & ATTR_CTIME)
720 vbsf_time_to_vbox(&pReq->Info.ObjInfo.ChangeTime, &iattr->ia_ctime);
721
722 /* Make the change. */
723 vrc = VbglR0SfHostReqSetObjInfo(sf_g->map.root, &pReq->Info, hHostFile);
724 if (RT_SUCCESS(vrc)) {
725 vbsf_update_inode(pInode, sf_i, &pReq->Info.ObjInfo, sf_g, true /*fLocked*/);
726 } else {
727 rc = -RTErrConvertToErrno(vrc);
728 LogFunc(("VbglR0SfHostReqSetObjInfo(%s) failed vrc=%Rrc rc=%d\n", sf_i->path->String.ach, vrc, rc));
729 }
730 }
731
732 /*
733 * Change the file size.
734 * Note! Old API is more convenient here as it gives us up to date
735 * inode info back.
736 */
737 if ((iattr->ia_valid & ATTR_SIZE) && rc == 0) {
738 /*vrc = VbglR0SfHostReqSetFileSize(sf_g->map.root, &pReq->SetSize, hHostFile, iattr->ia_size);
739 if (RT_SUCCESS(vrc)) {
740 i_size_write(pInode, iattr->ia_size);
741 } else if (vrc == VERR_NOT_IMPLEMENTED)*/ {
742 /* Fallback for pre 6.0 hosts: */
743 RT_ZERO(pReq->Info.ObjInfo);
744 pReq->Info.ObjInfo.cbObject = iattr->ia_size;
745 vrc = VbglR0SfHostReqSetFileSizeOld(sf_g->map.root, &pReq->Info, hHostFile);
746 if (RT_SUCCESS(vrc))
747 vbsf_update_inode(pInode, sf_i, &pReq->Info.ObjInfo, sf_g, true /*fLocked*/);
748 }
749 if (RT_SUCCESS(vrc)) {
750 /** @todo there is potentially more to be done here if there are mappings of
751 * the lovely file. */
752 } else {
753 rc = -RTErrConvertToErrno(vrc);
754 LogFunc(("VbglR0SfHostReqSetFileSize(%s, %#llx) failed vrc=%Rrc rc=%d\n",
755 sf_i->path->String.ach, (unsigned long long)iattr->ia_size, vrc, rc));
756 }
757 }
758
759 /*
760 * Clean up.
761 */
762 if (!pHandle) {
763 vrc = VbglR0SfHostReqClose(sf_g->map.root, &pReq->Close, hHostFile);
764 if (RT_FAILURE(vrc))
765 LogFunc(("VbglR0SfHostReqClose(%s [%#llx]) failed vrc=%Rrc\n", sf_i->path->String.utf8, hHostFile, vrc));
766 }
767 }
768 if (pReq)
769 VbglR0PhysHeapFree(pReq);
770 if (pHandle)
771 vbsf_handle_release(pHandle, sf_g, "vbsf_inode_setattr");
772 } else
773 SFLOGFLOW(("vbsf_inode_setattr: Notthing to do here (%#x).\n", iattr->ia_valid));
774 }
775 return rc;
776}
777
778
779static int vbsf_make_path(const char *caller, struct vbsf_inode_info *sf_i,
780 const char *d_name, size_t d_len, SHFLSTRING **result)
781{
782 size_t path_len, shflstring_len;
783 SHFLSTRING *tmp;
784 uint16_t p_len;
785 uint8_t *p_name;
786 int fRoot = 0;
787
788 TRACE();
789 p_len = sf_i->path->u16Length;
790 p_name = sf_i->path->String.utf8;
791
792 if (p_len == 1 && *p_name == '/') {
793 path_len = d_len + 1;
794 fRoot = 1;
795 } else {
796 /* lengths of constituents plus terminating zero plus slash */
797 path_len = p_len + d_len + 2;
798 if (path_len > 0xffff) {
799 LogFunc(("path too long. caller=%s, path_len=%zu\n",
800 caller, path_len));
801 return -ENAMETOOLONG;
802 }
803 }
804
805 shflstring_len = offsetof(SHFLSTRING, String.utf8) + path_len;
806 tmp = kmalloc(shflstring_len, GFP_KERNEL);
807 if (!tmp) {
808 LogRelFunc(("kmalloc failed, caller=%s\n", caller));
809 return -ENOMEM;
810 }
811 tmp->u16Length = path_len - 1;
812 tmp->u16Size = path_len;
813
814 if (fRoot)
815 memcpy(&tmp->String.utf8[0], d_name, d_len + 1);
816 else {
817 memcpy(&tmp->String.utf8[0], p_name, p_len);
818 tmp->String.utf8[p_len] = '/';
819 memcpy(&tmp->String.utf8[p_len + 1], d_name, d_len);
820 tmp->String.utf8[p_len + 1 + d_len] = '\0';
821 }
822
823 *result = tmp;
824 return 0;
825}
826
827
828/**
829 * [dentry] contains string encoded in coding system that corresponds
830 * to [sf_g]->nls, we must convert it to UTF8 here and pass down to
831 * [vbsf_make_path] which will allocate SHFLSTRING and fill it in
832 */
833int vbsf_path_from_dentry(const char *caller, struct vbsf_super_info *sf_g, struct vbsf_inode_info *sf_i,
834 struct dentry *dentry, SHFLSTRING **result)
835{
836 int err;
837 const char *d_name;
838 size_t d_len;
839 const char *name;
840 size_t len = 0;
841
842 TRACE();
843 d_name = dentry->d_name.name;
844 d_len = dentry->d_name.len;
845
846 if (sf_g->nls) {
847 size_t in_len, i, out_bound_len;
848 const char *in;
849 char *out;
850
851 in = d_name;
852 in_len = d_len;
853
854 out_bound_len = PATH_MAX;
855 out = kmalloc(out_bound_len, GFP_KERNEL);
856 name = out;
857
858 for (i = 0; i < d_len; ++i) {
859 /* We renamed the linux kernel wchar_t type to linux_wchar_t in
860 the-linux-kernel.h, as it conflicts with the C++ type of that name. */
861 linux_wchar_t uni;
862 int nb;
863
864 nb = sf_g->nls->char2uni(in, in_len, &uni);
865 if (nb < 0) {
866 LogFunc(("nls->char2uni failed %x %d\n",
867 *in, in_len));
868 err = -EINVAL;
869 goto fail1;
870 }
871 in_len -= nb;
872 in += nb;
873
874#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
875 nb = utf32_to_utf8(uni, out, out_bound_len);
876#else
877 nb = utf8_wctomb(out, uni, out_bound_len);
878#endif
879 if (nb < 0) {
880 LogFunc(("nls->uni2char failed %x %d\n",
881 uni, out_bound_len));
882 err = -EINVAL;
883 goto fail1;
884 }
885 out_bound_len -= nb;
886 out += nb;
887 len += nb;
888 }
889 if (len >= PATH_MAX - 1) {
890 err = -ENAMETOOLONG;
891 goto fail1;
892 }
893
894 LogFunc(("result(%d) = %.*s\n", len, len, name));
895 *out = 0;
896 } else {
897 name = d_name;
898 len = d_len;
899 }
900
901 err = vbsf_make_path(caller, sf_i, name, len, result);
902 if (name != d_name)
903 kfree(name);
904
905 return err;
906
907 fail1:
908 kfree(name);
909 return err;
910}
911
912
913/**
914 * This is called during name resolution/lookup to check if the @a dentry in the
915 * cache is still valid. The actual validation is job is handled by
916 * vbsf_inode_revalidate_worker().
917 *
918 * @note Caller holds no relevant locks, just a dentry reference.
919 */
920#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
921static int vbsf_dentry_revalidate(struct dentry *dentry, unsigned flags)
922#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
923static int vbsf_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
924#else
925static int vbsf_dentry_revalidate(struct dentry *dentry, int flags)
926#endif
927{
928#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
929 int const flags = nd ? nd->flags : 0;
930#endif
931
932 int rc;
933
934 Assert(dentry);
935 SFLOGFLOW(("vbsf_dentry_revalidate: %p %#x %s\n", dentry, flags,
936 dentry->d_inode ? VBSF_GET_INODE_INFO(dentry->d_inode)->path->String.ach : "<negative>"));
937
938 /*
939 * See Documentation/filesystems/vfs.txt why we skip LOOKUP_RCU.
940 *
941 * Also recommended: https://lwn.net/Articles/649115/
942 * https://lwn.net/Articles/649729/
943 * https://lwn.net/Articles/650786/
944 *
945 */
946#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
947 if (flags & LOOKUP_RCU) {
948 rc = -ECHILD;
949 SFLOGFLOW(("vbsf_dentry_revalidate: RCU -> -ECHILD\n"));
950 } else
951#endif
952 {
953 /*
954 * Do we have an inode or not? If not it's probably a negative cache
955 * entry, otherwise most likely a positive one.
956 */
957 struct inode *pInode = dentry->d_inode;
958 if (pInode) {
959 /*
960 * Positive entry.
961 *
962 * Note! We're more aggressive here than other remote file systems,
963 * current (4.19) CIFS will for instance revalidate the inode
964 * and ignore the dentry timestamp for positive entries.
965 */
966 //struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(pInode);
967 unsigned long const cJiffiesAge = jiffies - vbsf_dentry_get_update_jiffies(dentry);
968 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(dentry->d_sb);
969 if (cJiffiesAge < sf_g->ttl) {
970 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, sf_g->ttl));
971 rc = 1;
972 } else if (!vbsf_inode_revalidate_worker(dentry, true /*fForced*/, false /*fInodeLocked*/)) {
973 vbsf_dentry_set_update_jiffies(dentry, jiffies); /** @todo get jiffies from inode. */
974 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 1\n", cJiffiesAge, sf_g->ttl));
975 rc = 1;
976 } else {
977 SFLOGFLOW(("vbsf_dentry_revalidate: age: %lu vs. TTL %lu -> reval -> 0\n", cJiffiesAge, sf_g->ttl));
978 rc = 0;
979 }
980 } else {
981 /*
982 * Negative entry.
983 *
984 * Invalidate dentries for open and renames here as we'll revalidate
985 * these when taking the actual action (also good for case preservation
986 * if we do case-insensitive mounts against windows + mac hosts at some
987 * later point).
988 */
989#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
990 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
991#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 75)
992 if (flags & LOOKUP_CREATE)
993#else
994 if (0)
995#endif
996 {
997 SFLOGFLOW(("vbsf_dentry_revalidate: negative: create or rename target -> 0\n"));
998 rc = 0;
999 } else {
1000 /* Can we skip revalidation based on TTL? */
1001 unsigned long const cJiffiesAge = vbsf_dentry_get_update_jiffies(dentry) - jiffies;
1002 struct vbsf_super_info *sf_g = VBSF_GET_SUPER_INFO(dentry->d_sb);
1003 if (cJiffiesAge < sf_g->ttl) {
1004 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 1\n", cJiffiesAge, sf_g->ttl));
1005 rc = 1;
1006 } else {
1007 /* We could revalidate it here, but we could instead just
1008 have the caller kick it out. */
1009 /** @todo stat the direntry and see if it exists now. */
1010 SFLOGFLOW(("vbsf_dentry_revalidate: negative: age: %lu vs. TTL %lu -> 0\n", cJiffiesAge, sf_g->ttl));
1011 rc = 0;
1012 }
1013 }
1014 }
1015 }
1016 return rc;
1017}
1018
1019#ifdef SFLOG_ENABLED
1020
1021/** For logging purposes only. */
1022# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
1023static int vbsf_dentry_delete(const struct dentry *pDirEntry)
1024# else
1025static int vbsf_dentry_delete(struct dentry *pDirEntry)
1026# endif
1027{
1028 SFLOGFLOW(("vbsf_dentry_delete: %p\n", pDirEntry));
1029 return 0;
1030}
1031
1032# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
1033/** For logging purposes only. */
1034static int vbsf_dentry_init(struct dentry *pDirEntry)
1035{
1036 SFLOGFLOW(("vbsf_dentry_init: %p\n", pDirEntry));
1037 return 0;
1038}
1039# endif
1040
1041#endif /* SFLOG_ENABLED */
1042
1043/**
1044 * Directory entry operations.
1045 *
1046 * Since 2.6.38 this is used via the super_block::s_d_op member.
1047 */
1048struct dentry_operations vbsf_dentry_ops = {
1049 .d_revalidate = vbsf_dentry_revalidate,
1050#ifdef SFLOG_ENABLED
1051 .d_delete = vbsf_dentry_delete,
1052# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
1053 .d_init = vbsf_dentry_init,
1054# endif
1055#endif
1056};
1057
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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