VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/regops.c@ 86708

Last change on this file since 86708 was 85703, checked in by vboxsync, 4 years ago

IPRT,lnx-kmods: s/RTLNX_RHEL_PREREQ/RTLNX_RHEL_MIN/; added RTLNX_RHEL_MAX and RTLNX_RHEL_RANGE. Use them instead of the RHEL_XXXX defines everywhere.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 145.7 KB
Line 
1/* $Id: regops.c 85703 2020-08-11 18:54:01Z vboxsync $ */
2/** @file
3 * vboxsf - VBox Linux Shared Folders VFS, regular file inode and file operations.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#include "vfsmod.h"
36#include <linux/uio.h>
37#if RTLNX_VER_MIN(2,5,32)
38# include <linux/aio.h> /* struct kiocb before 4.1 */
39#endif
40#if RTLNX_VER_MIN(2,5,12)
41# include <linux/buffer_head.h>
42#endif
43#if RTLNX_VER_RANGE(2,5,12, 2,6,31)
44# include <linux/writeback.h>
45#endif
46#if RTLNX_VER_RANGE(2,6,23, 3,16,0)
47# include <linux/splice.h>
48#endif
49#if RTLNX_VER_RANGE(2,6,17, 2,6,23)
50# include <linux/pipe_fs_i.h>
51#endif
52#if RTLNX_VER_MIN(2,4,10)
53# include <linux/swap.h> /* for mark_page_accessed */
54#endif
55#include <iprt/err.h>
56
57#if RTLNX_VER_MAX(2,6,18)
58# define SEEK_END 2
59#endif
60
61#if RTLNX_VER_MAX(3,16,0)
62# define iter_is_iovec(a_pIter) ( !((a_pIter)->type & ITER_KVEC) )
63#elif RTLNX_VER_MAX(3,19,0)
64# define iter_is_iovec(a_pIter) ( !((a_pIter)->type & (ITER_KVEC | ITER_BVEC)) )
65#endif
66
67#if RTLNX_VER_MAX(4,17,0)
68# define vm_fault_t int
69#endif
70
71#if RTLNX_VER_MAX(2,5,20)
72# define pgoff_t unsigned long
73#endif
74
75#if RTLNX_VER_MAX(2,5,12)
76# define PageUptodate(a_pPage) Page_Uptodate(a_pPage)
77#endif
78
79
80/*********************************************************************************************************************************
81* Structures and Typedefs *
82*********************************************************************************************************************************/
83#if RTLNX_VER_MAX(3,16,0)
84struct vbsf_iov_iter {
85 unsigned int type;
86 unsigned int v_write : 1;
87 size_t iov_offset;
88 size_t nr_segs;
89 struct iovec const *iov;
90# ifdef VBOX_STRICT
91 struct iovec const *iov_org;
92 size_t nr_segs_org;
93# endif
94};
95# ifdef VBOX_STRICT
96# define VBSF_IOV_ITER_INITIALIZER(a_cSegs, a_pIov, a_fWrite) \
97 { vbsf_iov_iter_detect_type(a_pIov, a_cSegs), a_fWrite, 0, a_cSegs, a_pIov, a_pIov, a_cSegs }
98# else
99# define VBSF_IOV_ITER_INITIALIZER(a_cSegs, a_pIov, a_fWrite) \
100 { vbsf_iov_iter_detect_type(a_pIov, a_cSegs), a_fWrite, 0, a_cSegs, a_pIov }
101# endif
102# define ITER_KVEC 1
103# define iov_iter vbsf_iov_iter
104#endif
105
106#if RTLNX_VER_MIN(2,6,19)
107/** Used by vbsf_iter_lock_pages() to keep the first page of the next segment. */
108struct vbsf_iter_stash {
109 struct page *pPage;
110 size_t off;
111 size_t cb;
112# if RTLNX_VER_MAX(4,11,0)
113 size_t offFromEnd;
114 struct iov_iter Copy;
115# endif
116};
117#endif /* >= 3.16.0 */
118/** Initializer for struct vbsf_iter_stash. */
119#if RTLNX_VER_MIN(4,11,0)
120# define VBSF_ITER_STASH_INITIALIZER { NULL, 0 }
121#else
122# define VBSF_ITER_STASH_INITIALIZER { NULL, 0, ~(size_t)0 }
123#endif
124
125
126/*********************************************************************************************************************************
127* Internal Functions *
128*********************************************************************************************************************************/
129DECLINLINE(void) vbsf_put_page(struct page *pPage);
130static void vbsf_unlock_user_pages(struct page **papPages, size_t cPages, bool fSetDirty, bool fLockPgHack);
131static void vbsf_reg_write_sync_page_cache(struct address_space *mapping, loff_t offFile, uint32_t cbRange,
132 uint8_t const *pbSrcBuf, struct page **papSrcPages,
133 uint32_t offSrcPage, size_t cSrcPages);
134
135
136/*********************************************************************************************************************************
137* Provide more recent uio.h functionality to older kernels. *
138*********************************************************************************************************************************/
139#if RTLNX_VER_RANGE(2,6,19, 3,16,0)
140
141/**
142 * Detects the vector type.
143 */
144static int vbsf_iov_iter_detect_type(struct iovec const *paIov, size_t cSegs)
145{
146 /* Check the first segment with a non-zero length. */
147 while (cSegs-- > 0) {
148 if (paIov->iov_len > 0) {
149 if (access_ok(VERIFY_READ, paIov->iov_base, paIov->iov_len))
150 return (uintptr_t)paIov->iov_base >= USER_DS.seg ? ITER_KVEC : 0;
151 AssertMsgFailed(("%p LB %#zx\n", paIov->iov_base, paIov->iov_len));
152 break;
153 }
154 paIov++;
155 }
156 return 0;
157}
158
159
160# undef iov_iter_count
161# define iov_iter_count(a_pIter) vbsf_iov_iter_count(a_pIter)
162static size_t vbsf_iov_iter_count(struct vbsf_iov_iter const *iter)
163{
164 size_t cbRet = 0;
165 size_t cLeft = iter->nr_segs;
166 struct iovec const *iov = iter->iov;
167 while (cLeft-- > 0) {
168 cbRet += iov->iov_len;
169 iov++;
170 }
171 return cbRet - iter->iov_offset;
172}
173
174
175# undef iov_iter_single_seg_count
176# define iov_iter_single_seg_count(a_pIter) vbsf_iov_iter_single_seg_count(a_pIter)
177static size_t vbsf_iov_iter_single_seg_count(struct vbsf_iov_iter const *iter)
178{
179 if (iter->nr_segs > 0)
180 return iter->iov->iov_len - iter->iov_offset;
181 return 0;
182}
183
184
185# undef iov_iter_advance
186# define iov_iter_advance(a_pIter, a_cbSkip) vbsf_iov_iter_advance(a_pIter, a_cbSkip)
187static void vbsf_iov_iter_advance(struct vbsf_iov_iter *iter, size_t cbSkip)
188{
189 SFLOG2(("vbsf_iov_iter_advance: cbSkip=%#zx\n", cbSkip));
190 if (iter->nr_segs > 0) {
191 size_t const cbLeftCur = iter->iov->iov_len - iter->iov_offset;
192 Assert(iter->iov_offset <= iter->iov->iov_len);
193 if (cbLeftCur > cbSkip) {
194 iter->iov_offset += cbSkip;
195 } else {
196 cbSkip -= cbLeftCur;
197 iter->iov_offset = 0;
198 iter->iov++;
199 iter->nr_segs--;
200 while (iter->nr_segs > 0) {
201 size_t const cbSeg = iter->iov->iov_len;
202 if (cbSeg > cbSkip) {
203 iter->iov_offset = cbSkip;
204 break;
205 }
206 cbSkip -= cbSeg;
207 iter->iov++;
208 iter->nr_segs--;
209 }
210 }
211 }
212}
213
214
215# undef iov_iter_get_pages
216# define iov_iter_get_pages(a_pIter, a_papPages, a_cbMax, a_cMaxPages, a_poffPg0) \
217 vbsf_iov_iter_get_pages(a_pIter, a_papPages, a_cbMax, a_cMaxPages, a_poffPg0)
218static ssize_t vbsf_iov_iter_get_pages(struct vbsf_iov_iter *iter, struct page **papPages,
219 size_t cbMax, unsigned cMaxPages, size_t *poffPg0)
220{
221 while (iter->nr_segs > 0) {
222 size_t const cbLeft = iter->iov->iov_len - iter->iov_offset;
223 Assert(iter->iov->iov_len >= iter->iov_offset);
224 if (cbLeft > 0) {
225 uintptr_t uPtrFrom = (uintptr_t)iter->iov->iov_base + iter->iov_offset;
226 size_t offPg0 = *poffPg0 = uPtrFrom & PAGE_OFFSET_MASK;
227 size_t cPagesLeft = RT_ALIGN_Z(offPg0 + cbLeft, PAGE_SIZE) >> PAGE_SHIFT;
228 size_t cPages = RT_MIN(cPagesLeft, cMaxPages);
229 struct task_struct *pTask = current;
230 size_t cPagesLocked;
231
232 down_read(&pTask->mm->mmap_sem);
233 cPagesLocked = get_user_pages(pTask, pTask->mm, uPtrFrom, cPages, iter->v_write, 1 /*force*/, papPages, NULL);
234 up_read(&pTask->mm->mmap_sem);
235 if (cPagesLocked == cPages) {
236 size_t cbRet = (cPages << PAGE_SHIFT) - offPg0;
237 if (cPages == cPagesLeft) {
238 size_t offLastPg = (uPtrFrom + cbLeft) & PAGE_OFFSET_MASK;
239 if (offLastPg)
240 cbRet -= PAGE_SIZE - offLastPg;
241 }
242 Assert(cbRet <= cbLeft);
243 return cbRet;
244 }
245 if (cPagesLocked > 0)
246 vbsf_unlock_user_pages(papPages, cPagesLocked, false /*fSetDirty*/, false /*fLockPgHack*/);
247 return -EFAULT;
248 }
249 iter->iov_offset = 0;
250 iter->iov++;
251 iter->nr_segs--;
252 }
253 AssertFailed();
254 return 0;
255}
256
257
258# undef iov_iter_truncate
259# define iov_iter_truncate(iter, cbNew) vbsf_iov_iter_truncate(iter, cbNew)
260static void vbsf_iov_iter_truncate(struct vbsf_iov_iter *iter, size_t cbNew)
261{
262 /* we have no counter or stuff, so it's a no-op. */
263 RT_NOREF(iter, cbNew);
264}
265
266
267# undef iov_iter_revert
268# define iov_iter_revert(a_pIter, a_cbRewind) vbsf_iov_iter_revert(a_pIter, a_cbRewind)
269void vbsf_iov_iter_revert(struct vbsf_iov_iter *iter, size_t cbRewind)
270{
271 SFLOG2(("vbsf_iov_iter_revert: cbRewind=%#zx\n", cbRewind));
272 if (iter->iov_offset > 0) {
273 if (cbRewind <= iter->iov_offset) {
274 iter->iov_offset -= cbRewind;
275 return;
276 }
277 cbRewind -= iter->iov_offset;
278 iter->iov_offset = 0;
279 }
280
281 while (cbRewind > 0) {
282 struct iovec const *pIov = --iter->iov;
283 size_t const cbSeg = pIov->iov_len;
284 iter->nr_segs++;
285
286 Assert((uintptr_t)pIov >= (uintptr_t)iter->iov_org);
287 Assert(iter->nr_segs <= iter->nr_segs_org);
288
289 if (cbRewind <= cbSeg) {
290 iter->iov_offset = cbSeg - cbRewind;
291 break;
292 }
293 cbRewind -= cbSeg;
294 }
295}
296
297#endif /* 2.6.19 <= linux < 3.16.0 */
298#if RTLNX_VER_RANGE(3,16,0, 3,16,35)
299
300/** This is for implementing cMaxPage on 3.16 which doesn't have it. */
301static ssize_t vbsf_iov_iter_get_pages_3_16(struct iov_iter *iter, struct page **papPages,
302 size_t cbMax, unsigned cMaxPages, size_t *poffPg0)
303{
304 if (!(iter->type & ITER_BVEC)) {
305 size_t const offPg0 = iter->iov_offset & PAGE_OFFSET_MASK;
306 size_t const cbMaxPages = ((size_t)cMaxPages << PAGE_SHIFT) - offPg0;
307 if (cbMax > cbMaxPages)
308 cbMax = cbMaxPages;
309 }
310 /* else: BVEC works a page at a time and shouldn't have much of a problem here. */
311 return iov_iter_get_pages(iter, papPages, cbMax, poffPg0);
312}
313# undef iov_iter_get_pages
314# define iov_iter_get_pages(a_pIter, a_papPages, a_cbMax, a_cMaxPages, a_poffPg0) \
315 vbsf_iov_iter_get_pages_3_16(a_pIter, a_papPages, a_cbMax, a_cMaxPages, a_poffPg0)
316
317#endif /* 3.16.0-3.16.34 */
318#if RTLNX_VER_RANGE(2,6,19, 3,18,0)
319
320static size_t copy_from_iter(uint8_t *pbDst, size_t cbToCopy, struct iov_iter *pSrcIter)
321{
322 size_t const cbTotal = cbToCopy;
323 Assert(iov_iter_count(pSrcIter) >= cbToCopy);
324# if RTLNX_VER_MIN(3,16,0)
325 if (pSrcIter->type & ITER_BVEC) {
326 while (cbToCopy > 0) {
327 size_t const offPage = (uintptr_t)pbDst & PAGE_OFFSET_MASK;
328 size_t const cbThisCopy = RT_MIN(PAGE_SIZE - offPage, cbToCopy);
329 struct page *pPage = rtR0MemObjLinuxVirtToPage(pbDst);
330 size_t cbCopied = copy_page_from_iter(pPage, offPage, cbThisCopy, pSrcIter);
331 AssertStmt(cbCopied <= cbThisCopy, cbCopied = cbThisCopy);
332 pbDst += cbCopied;
333 cbToCopy -= cbCopied;
334 if (cbCopied != cbToCopy)
335 break;
336 }
337 } else
338# endif
339 {
340 while (cbToCopy > 0) {
341 size_t cbThisCopy = iov_iter_single_seg_count(pSrcIter);
342 if (cbThisCopy > 0) {
343 if (cbThisCopy > cbToCopy)
344 cbThisCopy = cbToCopy;
345 if (pSrcIter->type & ITER_KVEC)
346 memcpy(pbDst, (void *)pSrcIter->iov->iov_base + pSrcIter->iov_offset, cbThisCopy);
347 else if (copy_from_user(pbDst, pSrcIter->iov->iov_base + pSrcIter->iov_offset, cbThisCopy) != 0)
348 break;
349 pbDst += cbThisCopy;
350 cbToCopy -= cbThisCopy;
351 }
352 iov_iter_advance(pSrcIter, cbThisCopy);
353 }
354 }
355 return cbTotal - cbToCopy;
356}
357
358
359static size_t copy_to_iter(uint8_t const *pbSrc, size_t cbToCopy, struct iov_iter *pDstIter)
360{
361 size_t const cbTotal = cbToCopy;
362 Assert(iov_iter_count(pDstIter) >= cbToCopy);
363# if RTLNX_VER_MIN(3,16,0)
364 if (pDstIter->type & ITER_BVEC) {
365 while (cbToCopy > 0) {
366 size_t const offPage = (uintptr_t)pbSrc & PAGE_OFFSET_MASK;
367 size_t const cbThisCopy = RT_MIN(PAGE_SIZE - offPage, cbToCopy);
368 struct page *pPage = rtR0MemObjLinuxVirtToPage((void *)pbSrc);
369 size_t cbCopied = copy_page_to_iter(pPage, offPage, cbThisCopy, pDstIter);
370 AssertStmt(cbCopied <= cbThisCopy, cbCopied = cbThisCopy);
371 pbSrc += cbCopied;
372 cbToCopy -= cbCopied;
373 if (cbCopied != cbToCopy)
374 break;
375 }
376 } else
377# endif
378 {
379 while (cbToCopy > 0) {
380 size_t cbThisCopy = iov_iter_single_seg_count(pDstIter);
381 if (cbThisCopy > 0) {
382 if (cbThisCopy > cbToCopy)
383 cbThisCopy = cbToCopy;
384 if (pDstIter->type & ITER_KVEC)
385 memcpy((void *)pDstIter->iov->iov_base + pDstIter->iov_offset, pbSrc, cbThisCopy);
386 else if (copy_to_user(pDstIter->iov->iov_base + pDstIter->iov_offset, pbSrc, cbThisCopy) != 0) {
387 break;
388 }
389 pbSrc += cbThisCopy;
390 cbToCopy -= cbThisCopy;
391 }
392 iov_iter_advance(pDstIter, cbThisCopy);
393 }
394 }
395 return cbTotal - cbToCopy;
396}
397
398#endif /* 3.16.0 <= linux < 3.18.0 */
399
400
401
402/*********************************************************************************************************************************
403* Handle management *
404*********************************************************************************************************************************/
405
406/**
407 * Called when an inode is released to unlink all handles that might impossibly
408 * still be associated with it.
409 *
410 * @param pInodeInfo The inode which handles to drop.
411 */
412void vbsf_handle_drop_chain(struct vbsf_inode_info *pInodeInfo)
413{
414 struct vbsf_handle *pCur, *pNext;
415 unsigned long fSavedFlags;
416 SFLOGFLOW(("vbsf_handle_drop_chain: %p\n", pInodeInfo));
417 spin_lock_irqsave(&g_SfHandleLock, fSavedFlags);
418
419 RTListForEachSafe(&pInodeInfo->HandleList, pCur, pNext, struct vbsf_handle, Entry) {
420 AssertMsg( (pCur->fFlags & (VBSF_HANDLE_F_MAGIC_MASK | VBSF_HANDLE_F_ON_LIST))
421 == (VBSF_HANDLE_F_MAGIC | VBSF_HANDLE_F_ON_LIST), ("%p %#x\n", pCur, pCur->fFlags));
422 pCur->fFlags |= VBSF_HANDLE_F_ON_LIST;
423 RTListNodeRemove(&pCur->Entry);
424 }
425
426 spin_unlock_irqrestore(&g_SfHandleLock, fSavedFlags);
427}
428
429
430/**
431 * Locates a handle that matches all the flags in @a fFlags.
432 *
433 * @returns Pointer to handle on success (retained), use vbsf_handle_release() to
434 * release it. NULL if no suitable handle was found.
435 * @param pInodeInfo The inode info to search.
436 * @param fFlagsSet The flags that must be set.
437 * @param fFlagsClear The flags that must be clear.
438 */
439struct vbsf_handle *vbsf_handle_find(struct vbsf_inode_info *pInodeInfo, uint32_t fFlagsSet, uint32_t fFlagsClear)
440{
441 struct vbsf_handle *pCur;
442 unsigned long fSavedFlags;
443 spin_lock_irqsave(&g_SfHandleLock, fSavedFlags);
444
445 RTListForEach(&pInodeInfo->HandleList, pCur, struct vbsf_handle, Entry) {
446 AssertMsg( (pCur->fFlags & (VBSF_HANDLE_F_MAGIC_MASK | VBSF_HANDLE_F_ON_LIST))
447 == (VBSF_HANDLE_F_MAGIC | VBSF_HANDLE_F_ON_LIST), ("%p %#x\n", pCur, pCur->fFlags));
448 if ((pCur->fFlags & (fFlagsSet | fFlagsClear)) == fFlagsSet) {
449 uint32_t cRefs = ASMAtomicIncU32(&pCur->cRefs);
450 if (cRefs > 1) {
451 spin_unlock_irqrestore(&g_SfHandleLock, fSavedFlags);
452 SFLOGFLOW(("vbsf_handle_find: returns %p\n", pCur));
453 return pCur;
454 }
455 /* Oops, already being closed (safe as it's only ever increased here). */
456 ASMAtomicDecU32(&pCur->cRefs);
457 }
458 }
459
460 spin_unlock_irqrestore(&g_SfHandleLock, fSavedFlags);
461 SFLOGFLOW(("vbsf_handle_find: returns NULL!\n"));
462 return NULL;
463}
464
465
466/**
467 * Slow worker for vbsf_handle_release() that does the freeing.
468 *
469 * @returns 0 (ref count).
470 * @param pHandle The handle to release.
471 * @param pSuperInfo The info structure for the shared folder associated with
472 * the handle.
473 * @param pszCaller The caller name (for logging failures).
474 */
475uint32_t vbsf_handle_release_slow(struct vbsf_handle *pHandle, struct vbsf_super_info *pSuperInfo, const char *pszCaller)
476{
477 int rc;
478 unsigned long fSavedFlags;
479
480 SFLOGFLOW(("vbsf_handle_release_slow: %p (%s)\n", pHandle, pszCaller));
481
482 /*
483 * Remove from the list.
484 */
485 spin_lock_irqsave(&g_SfHandleLock, fSavedFlags);
486
487 AssertMsg((pHandle->fFlags & VBSF_HANDLE_F_MAGIC_MASK) == VBSF_HANDLE_F_MAGIC, ("%p %#x\n", pHandle, pHandle->fFlags));
488 Assert(pHandle->pInodeInfo);
489 Assert(pHandle->pInodeInfo && pHandle->pInodeInfo->u32Magic == SF_INODE_INFO_MAGIC);
490
491 if (pHandle->fFlags & VBSF_HANDLE_F_ON_LIST) {
492 pHandle->fFlags &= ~VBSF_HANDLE_F_ON_LIST;
493 RTListNodeRemove(&pHandle->Entry);
494 }
495
496 spin_unlock_irqrestore(&g_SfHandleLock, fSavedFlags);
497
498 /*
499 * Actually destroy it.
500 */
501 rc = VbglR0SfHostReqCloseSimple(pSuperInfo->map.root, pHandle->hHost);
502 if (RT_FAILURE(rc))
503 LogFunc(("Caller %s: VbglR0SfHostReqCloseSimple %#RX64 failed with rc=%Rrc\n", pszCaller, pHandle->hHost, rc));
504 pHandle->hHost = SHFL_HANDLE_NIL;
505 pHandle->fFlags = VBSF_HANDLE_F_MAGIC_DEAD;
506 kfree(pHandle);
507 return 0;
508}
509
510
511/**
512 * Appends a handle to a handle list.
513 *
514 * @param pInodeInfo The inode to add it to.
515 * @param pHandle The handle to add.
516 */
517void vbsf_handle_append(struct vbsf_inode_info *pInodeInfo, struct vbsf_handle *pHandle)
518{
519#ifdef VBOX_STRICT
520 struct vbsf_handle *pCur;
521#endif
522 unsigned long fSavedFlags;
523
524 SFLOGFLOW(("vbsf_handle_append: %p (to %p)\n", pHandle, pInodeInfo));
525 AssertMsg((pHandle->fFlags & (VBSF_HANDLE_F_MAGIC_MASK | VBSF_HANDLE_F_ON_LIST)) == VBSF_HANDLE_F_MAGIC,
526 ("%p %#x\n", pHandle, pHandle->fFlags));
527 Assert(pInodeInfo->u32Magic == SF_INODE_INFO_MAGIC);
528
529 spin_lock_irqsave(&g_SfHandleLock, fSavedFlags);
530
531 AssertMsg((pHandle->fFlags & (VBSF_HANDLE_F_MAGIC_MASK | VBSF_HANDLE_F_ON_LIST)) == VBSF_HANDLE_F_MAGIC,
532 ("%p %#x\n", pHandle, pHandle->fFlags));
533#ifdef VBOX_STRICT
534 RTListForEach(&pInodeInfo->HandleList, pCur, struct vbsf_handle, Entry) {
535 Assert(pCur != pHandle);
536 AssertMsg( (pCur->fFlags & (VBSF_HANDLE_F_MAGIC_MASK | VBSF_HANDLE_F_ON_LIST))
537 == (VBSF_HANDLE_F_MAGIC | VBSF_HANDLE_F_ON_LIST), ("%p %#x\n", pCur, pCur->fFlags));
538 }
539 pHandle->pInodeInfo = pInodeInfo;
540#endif
541
542 pHandle->fFlags |= VBSF_HANDLE_F_ON_LIST;
543 RTListAppend(&pInodeInfo->HandleList, &pHandle->Entry);
544
545 spin_unlock_irqrestore(&g_SfHandleLock, fSavedFlags);
546}
547
548
549
550/*********************************************************************************************************************************
551* Misc *
552*********************************************************************************************************************************/
553
554#if RTLNX_VER_MAX(2,6,6)
555/** Any writable mappings? */
556DECLINLINE(bool) mapping_writably_mapped(struct address_space const *mapping)
557{
558# if RTLNX_VER_MIN(2,5,6)
559 return !list_empty(&mapping->i_mmap_shared);
560# else
561 return mapping->i_mmap_shared != NULL;
562# endif
563}
564#endif
565
566
567#if RTLNX_VER_MAX(2,5,12)
568/** Missing in 2.4.x, so just stub it for now. */
569DECLINLINE(bool) PageWriteback(struct page const *page)
570{
571 return false;
572}
573#endif
574
575
576/**
577 * Helper for deciding wheter we should do a read via the page cache or not.
578 *
579 * By default we will only use the page cache if there is a writable memory
580 * mapping of the file with a chance that it may have modified any of the pages
581 * already.
582 */
583DECLINLINE(bool) vbsf_should_use_cached_read(struct file *file, struct address_space *mapping, struct vbsf_super_info *pSuperInfo)
584{
585 if ( (file->f_flags & O_DIRECT)
586 || pSuperInfo->enmCacheMode == kVbsfCacheMode_None)
587 return false;
588 if ( pSuperInfo->enmCacheMode == kVbsfCacheMode_Read
589 || pSuperInfo->enmCacheMode == kVbsfCacheMode_ReadWrite)
590 return true;
591 Assert(pSuperInfo->enmCacheMode == kVbsfCacheMode_Strict);
592 return mapping
593 && mapping->nrpages > 0
594 && mapping_writably_mapped(mapping);
595}
596
597
598
599/*********************************************************************************************************************************
600* Pipe / splice stuff mainly for 2.6.17 >= linux < 2.6.31 (where no fallbacks were available) *
601*********************************************************************************************************************************/
602
603#if RTLNX_VER_RANGE(2,6,17, 3,16,0)
604
605# if RTLNX_VER_MAX(2,6,30)
606# define LOCK_PIPE(a_pPipe) do { if ((a_pPipe)->inode) mutex_lock(&(a_pPipe)->inode->i_mutex); } while (0)
607# define UNLOCK_PIPE(a_pPipe) do { if ((a_pPipe)->inode) mutex_unlock(&(a_pPipe)->inode->i_mutex); } while (0)
608# else
609# define LOCK_PIPE(a_pPipe) pipe_lock(a_pPipe)
610# define UNLOCK_PIPE(a_pPipe) pipe_unlock(a_pPipe)
611# endif
612
613
614/** Waits for the pipe buffer status to change. */
615static void vbsf_wait_pipe(struct pipe_inode_info *pPipe)
616{
617 DEFINE_WAIT(WaitStuff);
618# ifdef TASK_NONINTERACTIVE
619 prepare_to_wait(&pPipe->wait, &WaitStuff, TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE);
620# else
621 prepare_to_wait(&pPipe->wait, &WaitStuff, TASK_INTERRUPTIBLE);
622# endif
623 UNLOCK_PIPE(pPipe);
624
625 schedule();
626
627 finish_wait(&pPipe->wait, &WaitStuff);
628 LOCK_PIPE(pPipe);
629}
630
631
632/** Worker for vbsf_feed_pages_to_pipe that wakes up readers. */
633static void vbsf_wake_up_pipe(struct pipe_inode_info *pPipe, bool fReaders)
634{
635 smp_mb();
636 if (waitqueue_active(&pPipe->wait))
637 wake_up_interruptible_sync(&pPipe->wait);
638 if (fReaders)
639 kill_fasync(&pPipe->fasync_readers, SIGIO, POLL_IN);
640 else
641 kill_fasync(&pPipe->fasync_writers, SIGIO, POLL_OUT);
642}
643
644#endif
645#if RTLNX_VER_RANGE(2,6,17, 2,6,31)
646
647/** Verify pipe buffer content (needed for page-cache to ensure idle page). */
648static int vbsf_pipe_buf_confirm(struct pipe_inode_info *pPipe, struct pipe_buffer *pPipeBuf)
649{
650 /*SFLOG3(("vbsf_pipe_buf_confirm: %p\n", pPipeBuf));*/
651 return 0;
652}
653
654
655/** Maps the buffer page. */
656static void *vbsf_pipe_buf_map(struct pipe_inode_info *pPipe, struct pipe_buffer *pPipeBuf, int atomic)
657{
658 void *pvRet;
659 if (!atomic)
660 pvRet = kmap(pPipeBuf->page);
661 else {
662 pPipeBuf->flags |= PIPE_BUF_FLAG_ATOMIC;
663 pvRet = kmap_atomic(pPipeBuf->page, KM_USER0);
664 }
665 /*SFLOG3(("vbsf_pipe_buf_map: %p -> %p\n", pPipeBuf, pvRet));*/
666 return pvRet;
667}
668
669
670/** Unmaps the buffer page. */
671static void vbsf_pipe_buf_unmap(struct pipe_inode_info *pPipe, struct pipe_buffer *pPipeBuf, void *pvMapping)
672{
673 /*SFLOG3(("vbsf_pipe_buf_unmap: %p/%p\n", pPipeBuf, pvMapping)); */
674 if (!(pPipeBuf->flags & PIPE_BUF_FLAG_ATOMIC))
675 kunmap(pPipeBuf->page);
676 else {
677 pPipeBuf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
678 kunmap_atomic(pvMapping, KM_USER0);
679 }
680}
681
682
683/** Gets a reference to the page. */
684static void vbsf_pipe_buf_get(struct pipe_inode_info *pPipe, struct pipe_buffer *pPipeBuf)
685{
686 page_cache_get(pPipeBuf->page);
687 /*SFLOG3(("vbsf_pipe_buf_get: %p (return count=%d)\n", pPipeBuf, page_count(pPipeBuf->page)));*/
688}
689
690
691/** Release the buffer page (counter to vbsf_pipe_buf_get). */
692static void vbsf_pipe_buf_release(struct pipe_inode_info *pPipe, struct pipe_buffer *pPipeBuf)
693{
694 /*SFLOG3(("vbsf_pipe_buf_release: %p (incoming count=%d)\n", pPipeBuf, page_count(pPipeBuf->page)));*/
695 page_cache_release(pPipeBuf->page);
696}
697
698
699/** Attempt to steal the page.
700 * @returns 0 success, 1 on failure. */
701static int vbsf_pipe_buf_steal(struct pipe_inode_info *pPipe, struct pipe_buffer *pPipeBuf)
702{
703 if (page_count(pPipeBuf->page) == 1) {
704 lock_page(pPipeBuf->page);
705 SFLOG3(("vbsf_pipe_buf_steal: %p -> 0\n", pPipeBuf));
706 return 0;
707 }
708 SFLOG3(("vbsf_pipe_buf_steal: %p -> 1\n", pPipeBuf));
709 return 1;
710}
711
712
713/**
714 * Pipe buffer operations for used by vbsf_feed_pages_to_pipe.
715 */
716static struct pipe_buf_operations vbsf_pipe_buf_ops = {
717 .can_merge = 0,
718# if RTLNX_VER_MIN(2,6,23)
719 .confirm = vbsf_pipe_buf_confirm,
720# else
721 .pin = vbsf_pipe_buf_confirm,
722# endif
723 .map = vbsf_pipe_buf_map,
724 .unmap = vbsf_pipe_buf_unmap,
725 .get = vbsf_pipe_buf_get,
726 .release = vbsf_pipe_buf_release,
727 .steal = vbsf_pipe_buf_steal,
728};
729
730
731/**
732 * Feeds the pages to the pipe.
733 *
734 * Pages given to the pipe are set to NULL in papPages.
735 */
736static ssize_t vbsf_feed_pages_to_pipe(struct pipe_inode_info *pPipe, struct page **papPages, size_t cPages, uint32_t offPg0,
737 uint32_t cbActual, unsigned fFlags)
738{
739 ssize_t cbRet = 0;
740 size_t iPage = 0;
741 bool fNeedWakeUp = false;
742
743 LOCK_PIPE(pPipe);
744 for (;;) {
745 if ( pPipe->readers > 0
746 && pPipe->nrbufs < PIPE_BUFFERS) {
747 struct pipe_buffer *pPipeBuf = &pPipe->bufs[(pPipe->curbuf + pPipe->nrbufs) % PIPE_BUFFERS];
748 uint32_t const cbThisPage = RT_MIN(cbActual, PAGE_SIZE - offPg0);
749 pPipeBuf->len = cbThisPage;
750 pPipeBuf->offset = offPg0;
751# if RTLNX_VER_MIN(2,6,23)
752 pPipeBuf->private = 0;
753# endif
754 pPipeBuf->ops = &vbsf_pipe_buf_ops;
755 pPipeBuf->flags = fFlags & SPLICE_F_GIFT ? PIPE_BUF_FLAG_GIFT : 0;
756 pPipeBuf->page = papPages[iPage];
757
758 papPages[iPage++] = NULL;
759 pPipe->nrbufs++;
760 fNeedWakeUp |= pPipe->inode != NULL;
761 offPg0 = 0;
762 cbRet += cbThisPage;
763
764 /* done? */
765 cbActual -= cbThisPage;
766 if (!cbActual)
767 break;
768 } else if (pPipe->readers == 0) {
769 SFLOGFLOW(("vbsf_feed_pages_to_pipe: no readers!\n"));
770 send_sig(SIGPIPE, current, 0);
771 if (cbRet == 0)
772 cbRet = -EPIPE;
773 break;
774 } else if (fFlags & SPLICE_F_NONBLOCK) {
775 if (cbRet == 0)
776 cbRet = -EAGAIN;
777 break;
778 } else if (signal_pending(current)) {
779 if (cbRet == 0)
780 cbRet = -ERESTARTSYS;
781 SFLOGFLOW(("vbsf_feed_pages_to_pipe: pending signal! (%zd)\n", cbRet));
782 break;
783 } else {
784 if (fNeedWakeUp) {
785 vbsf_wake_up_pipe(pPipe, true /*fReaders*/);
786 fNeedWakeUp = 0;
787 }
788 pPipe->waiting_writers++;
789 vbsf_wait_pipe(pPipe);
790 pPipe->waiting_writers--;
791 }
792 }
793 UNLOCK_PIPE(pPipe);
794
795 if (fNeedWakeUp)
796 vbsf_wake_up_pipe(pPipe, true /*fReaders*/);
797
798 return cbRet;
799}
800
801
802/**
803 * For splicing from a file to a pipe.
804 */
805static ssize_t vbsf_splice_read(struct file *file, loff_t *poffset, struct pipe_inode_info *pipe, size_t len, unsigned int flags)
806{
807 struct inode *inode = VBSF_GET_F_DENTRY(file)->d_inode;
808 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
809 ssize_t cbRet;
810
811 SFLOGFLOW(("vbsf_splice_read: file=%p poffset=%p{%#RX64} pipe=%p len=%#zx flags=%#x\n", file, poffset, *poffset, pipe, len, flags));
812 if (vbsf_should_use_cached_read(file, inode->i_mapping, pSuperInfo)) {
813 cbRet = generic_file_splice_read(file, poffset, pipe, len, flags);
814 } else {
815 /*
816 * Create a read request.
817 */
818 loff_t offFile = *poffset;
819 size_t cPages = RT_MIN(RT_ALIGN_Z((offFile & ~PAGE_CACHE_MASK) + len, PAGE_CACHE_SIZE) >> PAGE_CACHE_SHIFT,
820 PIPE_BUFFERS);
821 VBOXSFREADPGLSTREQ *pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ,
822 PgLst.aPages[cPages]));
823 if (pReq) {
824 /*
825 * Allocate pages.
826 */
827 struct page *apPages[PIPE_BUFFERS];
828 size_t i;
829 pReq->PgLst.offFirstPage = (uint16_t)offFile & (uint16_t)PAGE_OFFSET_MASK;
830 cbRet = 0;
831 for (i = 0; i < cPages; i++) {
832 struct page *pPage;
833 apPages[i] = pPage = alloc_page(GFP_USER);
834 if (pPage) {
835 pReq->PgLst.aPages[i] = page_to_phys(pPage);
836# ifdef VBOX_STRICT
837 ASMMemFill32(kmap(pPage), PAGE_SIZE, UINT32_C(0xdeadbeef));
838 kunmap(pPage);
839# endif
840 } else {
841 cbRet = -ENOMEM;
842 break;
843 }
844 }
845 if (cbRet == 0) {
846 /*
847 * Do the reading.
848 */
849 uint32_t const cbToRead = RT_MIN((cPages << PAGE_SHIFT) - (offFile & PAGE_OFFSET_MASK), len);
850 struct vbsf_reg_info *sf_r = (struct vbsf_reg_info *)file->private_data;
851 int vrc = VbglR0SfHostReqReadPgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, offFile, cbToRead, cPages);
852 if (RT_SUCCESS(vrc)) {
853 /*
854 * Get the number of bytes read, jettison the request
855 * and, in case of EOF, any unnecessary pages.
856 */
857 uint32_t cbActual = pReq->Parms.cb32Read.u.value32;
858 AssertStmt(cbActual <= cbToRead, cbActual = cbToRead);
859 SFLOG2(("vbsf_splice_read: read -> %#x bytes @ %#RX64\n", cbActual, offFile));
860
861 VbglR0PhysHeapFree(pReq);
862 pReq = NULL;
863
864 /*
865 * Now, feed it to the pipe thingy.
866 * This will take ownership of the all pages no matter what happens.
867 */
868 cbRet = vbsf_feed_pages_to_pipe(pipe, apPages, cPages, offFile & PAGE_OFFSET_MASK, cbActual, flags);
869 if (cbRet > 0)
870 *poffset = offFile + cbRet;
871 } else {
872 cbRet = -RTErrConvertToErrno(vrc);
873 SFLOGFLOW(("vbsf_splice_read: Read failed: %Rrc -> %zd\n", vrc, cbRet));
874 }
875 i = cPages;
876 }
877
878 while (i-- > 0)
879 if (apPages[i])
880 __free_pages(apPages[i], 0);
881 if (pReq)
882 VbglR0PhysHeapFree(pReq);
883 } else {
884 cbRet = -ENOMEM;
885 }
886 }
887 SFLOGFLOW(("vbsf_splice_read: returns %zd (%#zx), *poffset=%#RX64\n", cbRet, cbRet, *poffset));
888 return cbRet;
889}
890
891#endif /* 2.6.17 <= LINUX_VERSION_CODE < 2.6.31 */
892#if RTLNX_VER_RANGE(2,6,17, 3,16,0)
893
894/**
895 * For splicing from a pipe to a file.
896 *
897 * Since we can combine buffers and request allocations, this should be faster
898 * than the default implementation.
899 */
900static ssize_t vbsf_splice_write(struct pipe_inode_info *pPipe, struct file *file, loff_t *poffset, size_t len, unsigned int flags)
901{
902 struct inode *inode = VBSF_GET_F_DENTRY(file)->d_inode;
903 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
904 ssize_t cbRet;
905
906 SFLOGFLOW(("vbsf_splice_write: pPipe=%p file=%p poffset=%p{%#RX64} len=%#zx flags=%#x\n", pPipe, file, poffset, *poffset, len, flags));
907 /** @todo later if (false) {
908 cbRet = generic_file_splice_write(pPipe, file, poffset, len, flags);
909 } else */ {
910 /*
911 * Prepare a write request.
912 */
913# ifdef PIPE_BUFFERS
914 uint32_t const cMaxPages = RT_MIN(PIPE_BUFFERS, RT_ALIGN_Z(len, PAGE_SIZE) >> PAGE_SHIFT);
915# else
916 uint32_t const cMaxPages = RT_MIN(RT_MAX(RT_MIN(pPipe->buffers, 256), PIPE_DEF_BUFFERS),
917 RT_ALIGN_Z(len, PAGE_SIZE) >> PAGE_SHIFT);
918# endif
919 VBOXSFWRITEPGLSTREQ *pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ,
920 PgLst.aPages[cMaxPages]));
921 if (pReq) {
922 /*
923 * Feed from the pipe.
924 */
925 struct vbsf_reg_info *sf_r = (struct vbsf_reg_info *)file->private_data;
926 struct address_space *mapping = inode->i_mapping;
927 loff_t offFile = *poffset;
928 bool fNeedWakeUp = false;
929 cbRet = 0;
930
931 LOCK_PIPE(pPipe);
932
933 for (;;) {
934 unsigned cBufs = pPipe->nrbufs;
935 /*SFLOG2(("vbsf_splice_write: nrbufs=%#x curbuf=%#x\n", cBufs, pPipe->curbuf));*/
936 if (cBufs) {
937 /*
938 * There is data available. Write it to the file.
939 */
940 int vrc;
941 struct pipe_buffer *pPipeBuf = &pPipe->bufs[pPipe->curbuf];
942 uint32_t cPagesToWrite = 1;
943 uint32_t cbToWrite = pPipeBuf->len;
944
945 Assert(pPipeBuf->offset < PAGE_SIZE);
946 Assert(pPipeBuf->offset + pPipeBuf->len <= PAGE_SIZE);
947
948 pReq->PgLst.offFirstPage = pPipeBuf->offset & PAGE_OFFSET;
949 pReq->PgLst.aPages[0] = page_to_phys(pPipeBuf->page);
950
951 /* Add any adjacent page buffers: */
952 while ( cPagesToWrite < cBufs
953 && cPagesToWrite < cMaxPages
954 && ((pReq->PgLst.offFirstPage + cbToWrite) & PAGE_OFFSET_MASK) == 0) {
955# ifdef PIPE_BUFFERS
956 struct pipe_buffer *pPipeBuf2 = &pPipe->bufs[(pPipe->curbuf + cPagesToWrite) % PIPE_BUFFERS];
957# else
958 struct pipe_buffer *pPipeBuf2 = &pPipe->bufs[(pPipe->curbuf + cPagesToWrite) % pPipe->buffers];
959# endif
960 Assert(pPipeBuf2->len <= PAGE_SIZE);
961 Assert(pPipeBuf2->offset < PAGE_SIZE);
962 if (pPipeBuf2->offset != 0)
963 break;
964 pReq->PgLst.aPages[cPagesToWrite] = page_to_phys(pPipeBuf2->page);
965 cbToWrite += pPipeBuf2->len;
966 cPagesToWrite += 1;
967 }
968
969 /* Check that we don't have signals pending before we issue the write, as
970 we'll only end up having to cancel the HGCM request 99% of the time: */
971 if (!signal_pending(current)) {
972 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
973 vrc = VbglR0SfHostReqWritePgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, offFile,
974 cbToWrite, cPagesToWrite);
975 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
976 } else
977 vrc = VERR_INTERRUPTED;
978 if (RT_SUCCESS(vrc)) {
979 /*
980 * Get the number of bytes actually written, update file position
981 * and return value, and advance the pipe buffer.
982 */
983 uint32_t cbActual = pReq->Parms.cb32Write.u.value32;
984 AssertStmt(cbActual <= cbToWrite, cbActual = cbToWrite);
985 SFLOG2(("vbsf_splice_write: write -> %#x bytes @ %#RX64\n", cbActual, offFile));
986
987 cbRet += cbActual;
988
989 while (cbActual > 0) {
990 uint32_t cbAdvance = RT_MIN(pPipeBuf->len, cbActual);
991
992 vbsf_reg_write_sync_page_cache(mapping, offFile, cbAdvance, NULL,
993 &pPipeBuf->page, pPipeBuf->offset, 1);
994
995 offFile += cbAdvance;
996 cbActual -= cbAdvance;
997 pPipeBuf->offset += cbAdvance;
998 pPipeBuf->len -= cbAdvance;
999
1000 if (!pPipeBuf->len) {
1001 struct pipe_buf_operations const *pOps = pPipeBuf->ops;
1002 pPipeBuf->ops = NULL;
1003 pOps->release(pPipe, pPipeBuf);
1004
1005# ifdef PIPE_BUFFERS
1006 pPipe->curbuf = (pPipe->curbuf + 1) % PIPE_BUFFERS;
1007# else
1008 pPipe->curbuf = (pPipe->curbuf + 1) % pPipe->buffers;
1009# endif
1010 pPipe->nrbufs -= 1;
1011 pPipeBuf = &pPipe->bufs[pPipe->curbuf];
1012
1013# if RTLNX_VER_MAX(2,6,30)
1014 fNeedWakeUp |= pPipe->inode != NULL;
1015# else
1016 fNeedWakeUp = true;
1017# endif
1018 } else {
1019 Assert(cbActual == 0);
1020 break;
1021 }
1022 }
1023
1024 *poffset = offFile;
1025 } else {
1026 if (cbRet == 0)
1027 cbRet = vrc == VERR_INTERRUPTED ? -ERESTARTSYS : -RTErrConvertToErrno(vrc);
1028 SFLOGFLOW(("vbsf_splice_write: Write failed: %Rrc -> %zd (cbRet=%#zx)\n",
1029 vrc, -RTErrConvertToErrno(vrc), cbRet));
1030 break;
1031 }
1032 } else {
1033 /*
1034 * Wait for data to become available, if there is chance that'll happen.
1035 */
1036 /* Quit if there are no writers (think EOF): */
1037 if (pPipe->writers == 0) {
1038 SFLOGFLOW(("vbsf_splice_write: No buffers. No writers. The show is done!\n"));
1039 break;
1040 }
1041
1042 /* Quit if if we've written some and no writers waiting on the lock: */
1043 if (cbRet > 0 && pPipe->waiting_writers == 0) {
1044 SFLOGFLOW(("vbsf_splice_write: No waiting writers, returning what we've got.\n"));
1045 break;
1046 }
1047
1048 /* Quit with EAGAIN if non-blocking: */
1049 if (flags & SPLICE_F_NONBLOCK) {
1050 if (cbRet == 0)
1051 cbRet = -EAGAIN;
1052 break;
1053 }
1054
1055 /* Quit if we've got pending signals: */
1056 if (signal_pending(current)) {
1057 if (cbRet == 0)
1058 cbRet = -ERESTARTSYS;
1059 SFLOGFLOW(("vbsf_splice_write: pending signal! (%zd)\n", cbRet));
1060 break;
1061 }
1062
1063 /* Wake up writers before we start waiting: */
1064 if (fNeedWakeUp) {
1065 vbsf_wake_up_pipe(pPipe, false /*fReaders*/);
1066 fNeedWakeUp = false;
1067 }
1068 vbsf_wait_pipe(pPipe);
1069 }
1070 } /* feed loop */
1071
1072 if (fNeedWakeUp)
1073 vbsf_wake_up_pipe(pPipe, false /*fReaders*/);
1074
1075 UNLOCK_PIPE(pPipe);
1076
1077 VbglR0PhysHeapFree(pReq);
1078 } else {
1079 cbRet = -ENOMEM;
1080 }
1081 }
1082 SFLOGFLOW(("vbsf_splice_write: returns %zd (%#zx), *poffset=%#RX64\n", cbRet, cbRet, *poffset));
1083 return cbRet;
1084}
1085
1086#endif /* 2.6.17 <= LINUX_VERSION_CODE < 3.16.0 */
1087
1088#if RTLNX_VER_RANGE(2,5,30, 2,6,23)
1089/**
1090 * Our own senfile implementation that does not go via the page cache like
1091 * generic_file_sendfile() does.
1092 */
1093static ssize_t vbsf_reg_sendfile(struct file *pFile, loff_t *poffFile, size_t cbToSend, read_actor_t pfnActor,
1094# if RTLNX_VER_MIN(2,6,8)
1095 void *pvUser
1096# else
1097 void __user *pvUser
1098# endif
1099 )
1100{
1101 struct inode *inode = VBSF_GET_F_DENTRY(pFile)->d_inode;
1102 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
1103 ssize_t cbRet;
1104 SFLOGFLOW(("vbsf_reg_sendfile: pFile=%p poffFile=%p{%#RX64} cbToSend=%#zx pfnActor=%p pvUser=%p\n",
1105 pFile, poffFile, poffFile ? *poffFile : 0, cbToSend, pfnActor, pvUser));
1106 Assert(pSuperInfo);
1107
1108 /*
1109 * Return immediately if asked to send nothing.
1110 */
1111 if (cbToSend == 0)
1112 return 0;
1113
1114 /*
1115 * Like for vbsf_reg_read() and vbsf_reg_read_iter(), we allow going via
1116 * the page cache in some cases or configs.
1117 */
1118 if (vbsf_should_use_cached_read(pFile, inode->i_mapping, pSuperInfo)) {
1119 cbRet = generic_file_sendfile(pFile, poffFile, cbToSend, pfnActor, pvUser);
1120 SFLOGFLOW(("vbsf_reg_sendfile: returns %#zx *poffFile=%#RX64 [generic_file_sendfile]\n", cbRet, poffFile ? *poffFile : UINT64_MAX));
1121 } else {
1122 /*
1123 * Allocate a request and a bunch of pages for reading from the file.
1124 */
1125 struct page *apPages[16];
1126 loff_t offFile = poffFile ? *poffFile : 0;
1127 size_t const cPages = cbToSend + ((size_t)offFile & PAGE_OFFSET_MASK) >= RT_ELEMENTS(apPages) * PAGE_SIZE
1128 ? RT_ELEMENTS(apPages)
1129 : RT_ALIGN_Z(cbToSend + ((size_t)offFile & PAGE_OFFSET_MASK), PAGE_SIZE) >> PAGE_SHIFT;
1130 size_t iPage;
1131 VBOXSFREADPGLSTREQ *pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ,
1132 PgLst.aPages[cPages]));
1133 if (pReq) {
1134 Assert(cPages > 0);
1135 cbRet = 0;
1136 for (iPage = 0; iPage < cPages; iPage++) {
1137 struct page *pPage;
1138 apPages[iPage] = pPage = alloc_page(GFP_USER);
1139 if (pPage) {
1140 Assert(page_count(pPage) == 1);
1141 pReq->PgLst.aPages[iPage] = page_to_phys(pPage);
1142 } else {
1143 while (iPage-- > 0)
1144 vbsf_put_page(apPages[iPage]);
1145 cbRet = -ENOMEM;
1146 break;
1147 }
1148 }
1149 if (cbRet == 0) {
1150 /*
1151 * Do the job.
1152 */
1153 struct vbsf_reg_info *sf_r = (struct vbsf_reg_info *)pFile->private_data;
1154 read_descriptor_t RdDesc;
1155 RdDesc.count = cbToSend;
1156# if RTLNX_VER_MIN(2,6,8)
1157 RdDesc.arg.data = pvUser;
1158# else
1159 RdDesc.buf = pvUser;
1160# endif
1161 RdDesc.written = 0;
1162 RdDesc.error = 0;
1163
1164 Assert(sf_r);
1165 Assert((sf_r->Handle.fFlags & VBSF_HANDLE_F_MAGIC_MASK) == VBSF_HANDLE_F_MAGIC);
1166
1167 while (cbToSend > 0) {
1168 /*
1169 * Read another chunk. For paranoid reasons, we keep data where the page cache
1170 * would keep it, i.e. page offset bits corresponds to the file offset bits.
1171 */
1172 uint32_t const offPg0 = (uint32_t)offFile & (uint32_t)PAGE_OFFSET_MASK;
1173 uint32_t const cbToRead = RT_MIN((cPages << PAGE_SHIFT) - offPg0, cbToSend);
1174 uint32_t const cPagesToRead = RT_ALIGN_Z(cbToRead + offPg0, PAGE_SIZE) >> PAGE_SHIFT;
1175 int vrc;
1176 pReq->PgLst.offFirstPage = (uint16_t)offPg0;
1177 if (!signal_pending(current))
1178 vrc = VbglR0SfHostReqReadPgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, offFile,
1179 cbToRead, cPagesToRead);
1180 else
1181 vrc = VERR_INTERRUPTED;
1182 if (RT_SUCCESS(vrc)) {
1183 /*
1184 * Pass what we read to the actor.
1185 */
1186 uint32_t off = offPg0;
1187 uint32_t cbActual = pReq->Parms.cb32Read.u.value32;
1188 bool const fIsEof = cbActual < cbToRead;
1189 AssertStmt(cbActual <= cbToRead, cbActual = cbToRead);
1190 SFLOG3(("vbsf_reg_sendfile: Read %#x bytes (offPg0=%#x), wanted %#x ...\n", cbActual, offPg0, cbToRead));
1191
1192 iPage = 0;
1193 while (cbActual > 0) {
1194 uint32_t const cbPage = RT_MIN(cbActual, PAGE_SIZE - off);
1195 int const cbRetActor = pfnActor(&RdDesc, apPages[iPage], off, cbPage);
1196 Assert(cbRetActor >= 0); /* Returns zero on failure, with RdDesc.error holding the status code. */
1197
1198 AssertMsg(iPage < cPages && iPage < cPagesToRead, ("iPage=%#x cPages=%#x cPagesToRead=%#x\n", iPage, cPages, cPagesToRead));
1199
1200 offFile += cbRetActor;
1201 if ((uint32_t)cbRetActor == cbPage && RdDesc.count > 0) {
1202 cbActual -= cbPage;
1203 cbToSend -= cbPage;
1204 iPage++;
1205 } else {
1206 SFLOG3(("vbsf_reg_sendfile: cbRetActor=%#x (%d) cbPage=%#x RdDesc{count=%#lx error=%d} iPage=%#x/%#x/%#x cbToSend=%#zx\n",
1207 cbRetActor, cbRetActor, cbPage, RdDesc.count, RdDesc.error, iPage, cPagesToRead, cPages, cbToSend));
1208 vrc = VERR_CALLBACK_RETURN;
1209 break;
1210 }
1211 off = 0;
1212 }
1213
1214 /*
1215 * Are we done yet?
1216 */
1217 if (RT_FAILURE_NP(vrc) || cbToSend == 0 || RdDesc.error != 0 || fIsEof) {
1218 break;
1219 }
1220
1221 /*
1222 * Replace pages held by the actor.
1223 */
1224 vrc = VINF_SUCCESS;
1225 for (iPage = 0; iPage < cPages; iPage++) {
1226 struct page *pPage = apPages[iPage];
1227 if (page_count(pPage) != 1) {
1228 struct page *pNewPage = alloc_page(GFP_USER);
1229 if (pNewPage) {
1230 SFLOGFLOW(("vbsf_reg_sendfile: Replacing page #%x: %p -> %p\n", iPage, pPage, pNewPage));
1231 vbsf_put_page(pPage);
1232 apPages[iPage] = pNewPage;
1233 } else {
1234 SFLOGFLOW(("vbsf_reg_sendfile: Failed to allocate a replacement page.\n"));
1235 vrc = VERR_NO_MEMORY;
1236 break;
1237 }
1238 }
1239 }
1240 if (RT_FAILURE(vrc))
1241 break; /* RdDesc.written should be non-zero, so don't bother with setting error. */
1242 } else {
1243 RdDesc.error = vrc == VERR_INTERRUPTED ? -ERESTARTSYS : -RTErrConvertToErrno(vrc);
1244 SFLOGFLOW(("vbsf_reg_sendfile: Read failed: %Rrc -> %zd (RdDesc.error=%#d)\n",
1245 vrc, -RTErrConvertToErrno(vrc), RdDesc.error));
1246 break;
1247 }
1248 }
1249
1250 /*
1251 * Free memory.
1252 */
1253 for (iPage = 0; iPage < cPages; iPage++)
1254 vbsf_put_page(apPages[iPage]);
1255
1256 /*
1257 * Set the return values.
1258 */
1259 if (RdDesc.written) {
1260 cbRet = RdDesc.written;
1261 if (poffFile)
1262 *poffFile = offFile;
1263 } else {
1264 cbRet = RdDesc.error;
1265 }
1266 }
1267 VbglR0PhysHeapFree(pReq);
1268 } else {
1269 cbRet = -ENOMEM;
1270 }
1271 SFLOGFLOW(("vbsf_reg_sendfile: returns %#zx offFile=%#RX64\n", cbRet, offFile));
1272 }
1273 return cbRet;
1274}
1275#endif /* 2.5.30 <= LINUX_VERSION_CODE < 2.6.23 */
1276
1277
1278/*********************************************************************************************************************************
1279* File operations on regular files *
1280*********************************************************************************************************************************/
1281
1282/** Wrapper around put_page / page_cache_release. */
1283DECLINLINE(void) vbsf_put_page(struct page *pPage)
1284{
1285#if RTLNX_VER_MIN(4,6,0)
1286 put_page(pPage);
1287#else
1288 page_cache_release(pPage);
1289#endif
1290}
1291
1292
1293/** Wrapper around get_page / page_cache_get. */
1294DECLINLINE(void) vbsf_get_page(struct page *pPage)
1295{
1296#if RTLNX_VER_MIN(4,6,0)
1297 get_page(pPage);
1298#else
1299 page_cache_get(pPage);
1300#endif
1301}
1302
1303
1304/** Companion to vbsf_lock_user_pages(). */
1305static void vbsf_unlock_user_pages(struct page **papPages, size_t cPages, bool fSetDirty, bool fLockPgHack)
1306{
1307 /* We don't mark kernel pages dirty: */
1308 if (fLockPgHack)
1309 fSetDirty = false;
1310
1311 while (cPages-- > 0)
1312 {
1313 struct page *pPage = papPages[cPages];
1314 Assert((ssize_t)cPages >= 0);
1315 if (fSetDirty && !PageReserved(pPage))
1316 set_page_dirty(pPage);
1317 vbsf_put_page(pPage);
1318 }
1319}
1320
1321
1322/**
1323 * Worker for vbsf_lock_user_pages_failed_check_kernel() and
1324 * vbsf_iter_lock_pages().
1325 */
1326static int vbsf_lock_kernel_pages(uint8_t *pbStart, bool fWrite, size_t cPages, struct page **papPages)
1327{
1328 uintptr_t const uPtrFrom = (uintptr_t)pbStart;
1329 uintptr_t const uPtrLast = (uPtrFrom & ~(uintptr_t)PAGE_OFFSET_MASK) + (cPages << PAGE_SHIFT) - 1;
1330 uint8_t *pbPage = (uint8_t *)uPtrLast;
1331 size_t iPage = cPages;
1332
1333 /*
1334 * Touch the pages first (paranoia^2).
1335 */
1336 if (fWrite) {
1337 uint8_t volatile *pbProbe = (uint8_t volatile *)uPtrFrom;
1338 while (iPage-- > 0) {
1339 *pbProbe = *pbProbe;
1340 pbProbe += PAGE_SIZE;
1341 }
1342 } else {
1343 uint8_t const *pbProbe = (uint8_t const *)uPtrFrom;
1344 while (iPage-- > 0) {
1345 ASMProbeReadByte(pbProbe);
1346 pbProbe += PAGE_SIZE;
1347 }
1348 }
1349
1350 /*
1351 * Get the pages.
1352 * Note! Fixes here probably applies to rtR0MemObjNativeLockKernel as well.
1353 */
1354 iPage = cPages;
1355 if ( uPtrFrom >= (unsigned long)__va(0)
1356 && uPtrLast < (unsigned long)high_memory) {
1357 /* The physical page mapping area: */
1358 while (iPage-- > 0) {
1359 struct page *pPage = papPages[iPage] = virt_to_page(pbPage);
1360 vbsf_get_page(pPage);
1361 pbPage -= PAGE_SIZE;
1362 }
1363 } else {
1364 /* This is vmalloc or some such thing, so go thru page tables: */
1365 while (iPage-- > 0) {
1366 struct page *pPage = rtR0MemObjLinuxVirtToPage(pbPage);
1367 if (pPage) {
1368 papPages[iPage] = pPage;
1369 vbsf_get_page(pPage);
1370 pbPage -= PAGE_SIZE;
1371 } else {
1372 while (++iPage < cPages) {
1373 pPage = papPages[iPage];
1374 vbsf_put_page(pPage);
1375 }
1376 return -EFAULT;
1377 }
1378 }
1379 }
1380 return 0;
1381}
1382
1383
1384/**
1385 * Catches kernel_read() and kernel_write() calls and works around them.
1386 *
1387 * The file_operations::read and file_operations::write callbacks supposedly
1388 * hands us the user buffers to read into and write out of. To allow the kernel
1389 * to read and write without allocating buffers in userland, they kernel_read()
1390 * and kernel_write() increases the user space address limit before calling us
1391 * so that copyin/copyout won't reject it. Our problem is that get_user_pages()
1392 * works on the userspace address space structures and will not be fooled by an
1393 * increased addr_limit.
1394 *
1395 * This code tries to detect this situation and fake get_user_lock() for the
1396 * kernel buffer.
1397 */
1398static int vbsf_lock_user_pages_failed_check_kernel(uintptr_t uPtrFrom, size_t cPages, bool fWrite, int rcFailed,
1399 struct page **papPages, bool *pfLockPgHack)
1400{
1401 /*
1402 * Check that this is valid user memory that is actually in the kernel range.
1403 */
1404#if RTLNX_VER_MIN(5,0,0) || RTLNX_RHEL_MIN(8,1)
1405 if ( access_ok((void *)uPtrFrom, cPages << PAGE_SHIFT)
1406 && uPtrFrom >= USER_DS.seg)
1407#else
1408 if ( access_ok(fWrite ? VERIFY_WRITE : VERIFY_READ, (void *)uPtrFrom, cPages << PAGE_SHIFT)
1409 && uPtrFrom >= USER_DS.seg)
1410#endif
1411 {
1412 int rc = vbsf_lock_kernel_pages((uint8_t *)uPtrFrom, fWrite, cPages, papPages);
1413 if (rc == 0) {
1414 *pfLockPgHack = true;
1415 return 0;
1416 }
1417 }
1418
1419 return rcFailed;
1420}
1421
1422
1423/** Wrapper around get_user_pages. */
1424DECLINLINE(int) vbsf_lock_user_pages(uintptr_t uPtrFrom, size_t cPages, bool fWrite, struct page **papPages, bool *pfLockPgHack)
1425{
1426# if RTLNX_VER_MIN(4,9,0) \
1427 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_RANGE(4,4,73, 4,4,74) /** @todo Figure out when & what exactly. */) \
1428 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_RANGE(4,4,75, 4,4,90) /** @todo Figure out when & what exactly. */) \
1429 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_RANGE(4,4,92, 4,5,0) /** @todo Figure out when & what exactly. */)
1430 ssize_t cPagesLocked = get_user_pages_unlocked(uPtrFrom, cPages, papPages,
1431 fWrite ? FOLL_WRITE | FOLL_FORCE : FOLL_FORCE);
1432# elif RTLNX_VER_MIN(4,6,0)
1433 ssize_t cPagesLocked = get_user_pages_unlocked(uPtrFrom, cPages, fWrite, 1 /*force*/, papPages);
1434# elif RTLNX_VER_RANGE(4,4,168, 4,5,0)
1435 ssize_t cPagesLocked = get_user_pages_unlocked(current, current->mm, uPtrFrom, cPages, papPages,
1436 fWrite ? FOLL_WRITE | FOLL_FORCE : FOLL_FORCE);
1437# elif RTLNX_VER_MIN(4,0,0)
1438 ssize_t cPagesLocked = get_user_pages_unlocked(current, current->mm, uPtrFrom, cPages, fWrite, 1 /*force*/, papPages);
1439# else
1440 struct task_struct *pTask = current;
1441 ssize_t cPagesLocked;
1442 down_read(&pTask->mm->mmap_sem);
1443 cPagesLocked = get_user_pages(pTask, pTask->mm, uPtrFrom, cPages, fWrite, 1 /*force*/, papPages, NULL);
1444 up_read(&pTask->mm->mmap_sem);
1445# endif
1446 *pfLockPgHack = false;
1447 if (cPagesLocked == cPages)
1448 return 0;
1449
1450 /*
1451 * It failed.
1452 */
1453 if (cPagesLocked < 0)
1454 return vbsf_lock_user_pages_failed_check_kernel(uPtrFrom, cPages, fWrite, (int)cPagesLocked, papPages, pfLockPgHack);
1455
1456 vbsf_unlock_user_pages(papPages, cPagesLocked, false /*fSetDirty*/, false /*fLockPgHack*/);
1457
1458 /* We could use uPtrFrom + cPagesLocked to get the correct status here... */
1459 return -EFAULT;
1460}
1461
1462
1463/**
1464 * Read function used when accessing files that are memory mapped.
1465 *
1466 * We read from the page cache here to present the a cohertent picture of the
1467 * the file content.
1468 */
1469static ssize_t vbsf_reg_read_mapped(struct file *file, char /*__user*/ *buf, size_t size, loff_t *off)
1470{
1471#if RTLNX_VER_MIN(3,16,0)
1472 struct iovec iov = { .iov_base = buf, .iov_len = size };
1473 struct iov_iter iter;
1474 struct kiocb kiocb;
1475 ssize_t cbRet;
1476
1477 init_sync_kiocb(&kiocb, file);
1478 kiocb.ki_pos = *off;
1479 iov_iter_init(&iter, READ, &iov, 1, size);
1480
1481 cbRet = generic_file_read_iter(&kiocb, &iter);
1482
1483 *off = kiocb.ki_pos;
1484 return cbRet;
1485
1486#elif RTLNX_VER_MIN(2,6,19)
1487 struct iovec iov = { .iov_base = buf, .iov_len = size };
1488 struct kiocb kiocb;
1489 ssize_t cbRet;
1490
1491 init_sync_kiocb(&kiocb, file);
1492 kiocb.ki_pos = *off;
1493
1494 cbRet = generic_file_aio_read(&kiocb, &iov, 1, *off);
1495 if (cbRet == -EIOCBQUEUED)
1496 cbRet = wait_on_sync_kiocb(&kiocb);
1497
1498 *off = kiocb.ki_pos;
1499 return cbRet;
1500
1501#else /* 2.6.18 or earlier: */
1502 return generic_file_read(file, buf, size, off);
1503#endif
1504}
1505
1506
1507/**
1508 * Fallback case of vbsf_reg_read() that locks the user buffers and let the host
1509 * write directly to them.
1510 */
1511static ssize_t vbsf_reg_read_locking(struct file *file, char /*__user*/ *buf, size_t size, loff_t *off,
1512 struct vbsf_super_info *pSuperInfo, struct vbsf_reg_info *sf_r)
1513{
1514 /*
1515 * Lock pages and execute the read, taking care not to pass the host
1516 * more than it can handle in one go or more than we care to allocate
1517 * page arrays for. The latter limit is set at just short of 32KB due
1518 * to how the physical heap works.
1519 */
1520 struct page *apPagesStack[16];
1521 struct page **papPages = &apPagesStack[0];
1522 struct page **papPagesFree = NULL;
1523 VBOXSFREADPGLSTREQ *pReq;
1524 loff_t offFile = *off;
1525 ssize_t cbRet = -ENOMEM;
1526 size_t cPages = (((uintptr_t)buf & PAGE_OFFSET_MASK) + size + PAGE_OFFSET_MASK) >> PAGE_SHIFT;
1527 size_t cMaxPages = RT_MIN(RT_MAX(pSuperInfo->cMaxIoPages, 1), cPages);
1528 bool fLockPgHack;
1529
1530 pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ, PgLst.aPages[cMaxPages]));
1531 while (!pReq && cMaxPages > 4) {
1532 cMaxPages /= 2;
1533 pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ, PgLst.aPages[cMaxPages]));
1534 }
1535 if (pReq && cMaxPages > RT_ELEMENTS(apPagesStack))
1536 papPagesFree = papPages = kmalloc(cMaxPages * sizeof(sizeof(papPages[0])), GFP_KERNEL);
1537 if (pReq && papPages) {
1538 cbRet = 0;
1539 for (;;) {
1540 /*
1541 * Figure out how much to process now and lock the user pages.
1542 */
1543 int rc;
1544 size_t cbChunk = (uintptr_t)buf & PAGE_OFFSET_MASK;
1545 pReq->PgLst.offFirstPage = (uint16_t)cbChunk;
1546 cPages = RT_ALIGN_Z(cbChunk + size, PAGE_SIZE) >> PAGE_SHIFT;
1547 if (cPages <= cMaxPages)
1548 cbChunk = size;
1549 else {
1550 cPages = cMaxPages;
1551 cbChunk = (cMaxPages << PAGE_SHIFT) - cbChunk;
1552 }
1553
1554 rc = vbsf_lock_user_pages((uintptr_t)buf, cPages, true /*fWrite*/, papPages, &fLockPgHack);
1555 if (rc == 0) {
1556 size_t iPage = cPages;
1557 while (iPage-- > 0)
1558 pReq->PgLst.aPages[iPage] = page_to_phys(papPages[iPage]);
1559 } else {
1560 cbRet = rc;
1561 break;
1562 }
1563
1564 /*
1565 * Issue the request and unlock the pages.
1566 */
1567 rc = VbglR0SfHostReqReadPgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, offFile, cbChunk, cPages);
1568
1569 Assert(cPages <= cMaxPages);
1570 vbsf_unlock_user_pages(papPages, cPages, true /*fSetDirty*/, fLockPgHack);
1571
1572 if (RT_SUCCESS(rc)) {
1573 /*
1574 * Success, advance position and buffer.
1575 */
1576 uint32_t cbActual = pReq->Parms.cb32Read.u.value32;
1577 AssertStmt(cbActual <= cbChunk, cbActual = cbChunk);
1578 cbRet += cbActual;
1579 offFile += cbActual;
1580 buf = (uint8_t *)buf + cbActual;
1581 size -= cbActual;
1582
1583 /*
1584 * Are we done already? If so commit the new file offset.
1585 */
1586 if (!size || cbActual < cbChunk) {
1587 *off = offFile;
1588 break;
1589 }
1590 } else if (rc == VERR_NO_MEMORY && cMaxPages > 4) {
1591 /*
1592 * The host probably doesn't have enough heap to handle the
1593 * request, reduce the page count and retry.
1594 */
1595 cMaxPages /= 4;
1596 Assert(cMaxPages > 0);
1597 } else {
1598 /*
1599 * If we've successfully read stuff, return it rather than
1600 * the error. (Not sure if this is such a great idea...)
1601 */
1602 if (cbRet > 0) {
1603 SFLOGFLOW(("vbsf_reg_read: read at %#RX64 -> %Rrc; got cbRet=%#zx already\n", offFile, rc, cbRet));
1604 *off = offFile;
1605 } else {
1606 SFLOGFLOW(("vbsf_reg_read: read at %#RX64 -> %Rrc\n", offFile, rc));
1607 cbRet = -EPROTO;
1608 }
1609 break;
1610 }
1611 }
1612 }
1613 if (papPagesFree)
1614 kfree(papPages);
1615 if (pReq)
1616 VbglR0PhysHeapFree(pReq);
1617 SFLOGFLOW(("vbsf_reg_read: returns %zd (%#zx), *off=%RX64 [lock]\n", cbRet, cbRet, *off));
1618 return cbRet;
1619}
1620
1621
1622/**
1623 * Read from a regular file.
1624 *
1625 * @param file the file
1626 * @param buf the buffer
1627 * @param size length of the buffer
1628 * @param off offset within the file (in/out).
1629 * @returns the number of read bytes on success, Linux error code otherwise
1630 */
1631static ssize_t vbsf_reg_read(struct file *file, char /*__user*/ *buf, size_t size, loff_t *off)
1632{
1633 struct inode *inode = VBSF_GET_F_DENTRY(file)->d_inode;
1634 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
1635 struct vbsf_reg_info *sf_r = file->private_data;
1636 struct address_space *mapping = inode->i_mapping;
1637
1638 SFLOGFLOW(("vbsf_reg_read: inode=%p file=%p buf=%p size=%#zx off=%#llx\n", inode, file, buf, size, *off));
1639
1640 if (!S_ISREG(inode->i_mode)) {
1641 LogFunc(("read from non regular file %d\n", inode->i_mode));
1642 return -EINVAL;
1643 }
1644
1645 /** @todo XXX Check read permission according to inode->i_mode! */
1646
1647 if (!size)
1648 return 0;
1649
1650 /*
1651 * If there is a mapping and O_DIRECT isn't in effect, we must at a
1652 * heed dirty pages in the mapping and read from them. For simplicity
1653 * though, we just do page cache reading when there are writable
1654 * mappings around with any kind of pages loaded.
1655 */
1656 if (vbsf_should_use_cached_read(file, mapping, pSuperInfo))
1657 return vbsf_reg_read_mapped(file, buf, size, off);
1658
1659 /*
1660 * For small requests, try use an embedded buffer provided we get a heap block
1661 * that does not cross page boundraries (see host code).
1662 */
1663 if (size <= PAGE_SIZE / 4 * 3 - RT_UOFFSETOF(VBOXSFREADEMBEDDEDREQ, abData[0]) /* see allocator */) {
1664 uint32_t const cbReq = RT_UOFFSETOF(VBOXSFREADEMBEDDEDREQ, abData[0]) + size;
1665 VBOXSFREADEMBEDDEDREQ *pReq = (VBOXSFREADEMBEDDEDREQ *)VbglR0PhysHeapAlloc(cbReq);
1666 if (pReq) {
1667 if ((PAGE_SIZE - ((uintptr_t)pReq & PAGE_OFFSET_MASK)) >= cbReq) {
1668 ssize_t cbRet;
1669 int vrc = VbglR0SfHostReqReadEmbedded(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, *off, (uint32_t)size);
1670 if (RT_SUCCESS(vrc)) {
1671 cbRet = pReq->Parms.cb32Read.u.value32;
1672 AssertStmt(cbRet <= (ssize_t)size, cbRet = size);
1673 if (copy_to_user(buf, pReq->abData, cbRet) == 0)
1674 *off += cbRet;
1675 else
1676 cbRet = -EFAULT;
1677 } else
1678 cbRet = -EPROTO;
1679 VbglR0PhysHeapFree(pReq);
1680 SFLOGFLOW(("vbsf_reg_read: returns %zd (%#zx), *off=%RX64 [embed]\n", cbRet, cbRet, *off));
1681 return cbRet;
1682 }
1683 VbglR0PhysHeapFree(pReq);
1684 }
1685 }
1686
1687#if 0 /* Turns out this is slightly slower than locking the pages even for 4KB reads (4.19/amd64). */
1688 /*
1689 * For medium sized requests try use a bounce buffer.
1690 */
1691 if (size <= _64K /** @todo make this configurable? */) {
1692 void *pvBounce = kmalloc(size, GFP_KERNEL);
1693 if (pvBounce) {
1694 VBOXSFREADPGLSTREQ *pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
1695 if (pReq) {
1696 ssize_t cbRet;
1697 int vrc = VbglR0SfHostReqReadContig(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, *off,
1698 (uint32_t)size, pvBounce, virt_to_phys(pvBounce));
1699 if (RT_SUCCESS(vrc)) {
1700 cbRet = pReq->Parms.cb32Read.u.value32;
1701 AssertStmt(cbRet <= (ssize_t)size, cbRet = size);
1702 if (copy_to_user(buf, pvBounce, cbRet) == 0)
1703 *off += cbRet;
1704 else
1705 cbRet = -EFAULT;
1706 } else
1707 cbRet = -EPROTO;
1708 VbglR0PhysHeapFree(pReq);
1709 kfree(pvBounce);
1710 SFLOGFLOW(("vbsf_reg_read: returns %zd (%#zx), *off=%RX64 [bounce]\n", cbRet, cbRet, *off));
1711 return cbRet;
1712 }
1713 kfree(pvBounce);
1714 }
1715 }
1716#endif
1717
1718 return vbsf_reg_read_locking(file, buf, size, off, pSuperInfo, sf_r);
1719}
1720
1721
1722/**
1723 * Helper the synchronizes the page cache content with something we just wrote
1724 * to the host.
1725 */
1726static void vbsf_reg_write_sync_page_cache(struct address_space *mapping, loff_t offFile, uint32_t cbRange,
1727 uint8_t const *pbSrcBuf, struct page **papSrcPages,
1728 uint32_t offSrcPage, size_t cSrcPages)
1729{
1730 Assert(offSrcPage < PAGE_SIZE);
1731 if (mapping && mapping->nrpages > 0) {
1732 /*
1733 * Work the pages in the write range.
1734 */
1735 while (cbRange > 0) {
1736 /*
1737 * Lookup the page at offFile. We're fine if there aren't
1738 * any there. We're skip if it's dirty or is being written
1739 * back, at least for now.
1740 */
1741 size_t const offDstPage = offFile & PAGE_OFFSET_MASK;
1742 size_t const cbToCopy = RT_MIN(PAGE_SIZE - offDstPage, cbRange);
1743 pgoff_t const idxPage = offFile >> PAGE_SHIFT;
1744 struct page *pDstPage = find_lock_page(mapping, idxPage);
1745 if (pDstPage) {
1746 if ( pDstPage->mapping == mapping /* ignore if re-purposed (paranoia) */
1747 && pDstPage->index == idxPage
1748 && !PageDirty(pDstPage) /* ignore if dirty */
1749 && !PageWriteback(pDstPage) /* ignore if being written back */ ) {
1750 /*
1751 * Map the page and do the copying.
1752 */
1753 uint8_t *pbDst = (uint8_t *)kmap(pDstPage);
1754 if (pbSrcBuf)
1755 memcpy(&pbDst[offDstPage], pbSrcBuf, cbToCopy);
1756 else {
1757 uint32_t const cbSrc0 = PAGE_SIZE - offSrcPage;
1758 uint8_t const *pbSrc = (uint8_t const *)kmap(papSrcPages[0]);
1759 AssertMsg(cSrcPages >= 1, ("offFile=%#llx cbRange=%#zx cbToCopy=%#zx\n", offFile, cbRange, cbToCopy));
1760 memcpy(&pbDst[offDstPage], &pbSrc[offSrcPage], RT_MIN(cbToCopy, cbSrc0));
1761 kunmap(papSrcPages[0]);
1762 if (cbToCopy > cbSrc0) {
1763 AssertMsg(cSrcPages >= 2, ("offFile=%#llx cbRange=%#zx cbToCopy=%#zx\n", offFile, cbRange, cbToCopy));
1764 pbSrc = (uint8_t const *)kmap(papSrcPages[1]);
1765 memcpy(&pbDst[offDstPage + cbSrc0], pbSrc, cbToCopy - cbSrc0);
1766 kunmap(papSrcPages[1]);
1767 }
1768 }
1769 kunmap(pDstPage);
1770 flush_dcache_page(pDstPage);
1771 if (cbToCopy == PAGE_SIZE)
1772 SetPageUptodate(pDstPage);
1773# if RTLNX_VER_MIN(2,4,10)
1774 mark_page_accessed(pDstPage);
1775# endif
1776 } else
1777 SFLOGFLOW(("vbsf_reg_write_sync_page_cache: Skipping page %p: mapping=%p (vs %p) writeback=%d offset=%#lx (vs%#lx)\n",
1778 pDstPage, pDstPage->mapping, mapping, PageWriteback(pDstPage), pDstPage->index, idxPage));
1779 unlock_page(pDstPage);
1780 vbsf_put_page(pDstPage);
1781 }
1782
1783 /*
1784 * Advance.
1785 */
1786 if (pbSrcBuf)
1787 pbSrcBuf += cbToCopy;
1788 else
1789 {
1790 offSrcPage += cbToCopy;
1791 Assert(offSrcPage < PAGE_SIZE * 2);
1792 if (offSrcPage >= PAGE_SIZE) {
1793 offSrcPage &= PAGE_OFFSET_MASK;
1794 papSrcPages++;
1795# ifdef VBOX_STRICT
1796 Assert(cSrcPages > 0);
1797 cSrcPages--;
1798# endif
1799 }
1800 }
1801 offFile += cbToCopy;
1802 cbRange -= cbToCopy;
1803 }
1804 }
1805 RT_NOREF(cSrcPages);
1806}
1807
1808
1809/**
1810 * Fallback case of vbsf_reg_write() that locks the user buffers and let the host
1811 * write directly to them.
1812 */
1813static ssize_t vbsf_reg_write_locking(struct file *file, const char /*__user*/ *buf, size_t size, loff_t *off, loff_t offFile,
1814 struct inode *inode, struct vbsf_inode_info *sf_i,
1815 struct vbsf_super_info *pSuperInfo, struct vbsf_reg_info *sf_r)
1816{
1817 /*
1818 * Lock pages and execute the write, taking care not to pass the host
1819 * more than it can handle in one go or more than we care to allocate
1820 * page arrays for. The latter limit is set at just short of 32KB due
1821 * to how the physical heap works.
1822 */
1823 struct page *apPagesStack[16];
1824 struct page **papPages = &apPagesStack[0];
1825 struct page **papPagesFree = NULL;
1826 VBOXSFWRITEPGLSTREQ *pReq;
1827 ssize_t cbRet = -ENOMEM;
1828 size_t cPages = (((uintptr_t)buf & PAGE_OFFSET_MASK) + size + PAGE_OFFSET_MASK) >> PAGE_SHIFT;
1829 size_t cMaxPages = RT_MIN(RT_MAX(pSuperInfo->cMaxIoPages, 1), cPages);
1830 bool fLockPgHack;
1831
1832 pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFWRITEPGLSTREQ, PgLst.aPages[cMaxPages]));
1833 while (!pReq && cMaxPages > 4) {
1834 cMaxPages /= 2;
1835 pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFWRITEPGLSTREQ, PgLst.aPages[cMaxPages]));
1836 }
1837 if (pReq && cMaxPages > RT_ELEMENTS(apPagesStack))
1838 papPagesFree = papPages = kmalloc(cMaxPages * sizeof(sizeof(papPages[0])), GFP_KERNEL);
1839 if (pReq && papPages) {
1840 cbRet = 0;
1841 for (;;) {
1842 /*
1843 * Figure out how much to process now and lock the user pages.
1844 */
1845 int rc;
1846 size_t cbChunk = (uintptr_t)buf & PAGE_OFFSET_MASK;
1847 pReq->PgLst.offFirstPage = (uint16_t)cbChunk;
1848 cPages = RT_ALIGN_Z(cbChunk + size, PAGE_SIZE) >> PAGE_SHIFT;
1849 if (cPages <= cMaxPages)
1850 cbChunk = size;
1851 else {
1852 cPages = cMaxPages;
1853 cbChunk = (cMaxPages << PAGE_SHIFT) - cbChunk;
1854 }
1855
1856 rc = vbsf_lock_user_pages((uintptr_t)buf, cPages, false /*fWrite*/, papPages, &fLockPgHack);
1857 if (rc == 0) {
1858 size_t iPage = cPages;
1859 while (iPage-- > 0)
1860 pReq->PgLst.aPages[iPage] = page_to_phys(papPages[iPage]);
1861 } else {
1862 cbRet = rc;
1863 break;
1864 }
1865
1866 /*
1867 * Issue the request and unlock the pages.
1868 */
1869 rc = VbglR0SfHostReqWritePgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, offFile, cbChunk, cPages);
1870 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
1871 if (RT_SUCCESS(rc)) {
1872 /*
1873 * Success, advance position and buffer.
1874 */
1875 uint32_t cbActual = pReq->Parms.cb32Write.u.value32;
1876 AssertStmt(cbActual <= cbChunk, cbActual = cbChunk);
1877
1878 vbsf_reg_write_sync_page_cache(inode->i_mapping, offFile, cbActual, NULL /*pbKrnlBuf*/,
1879 papPages, (uintptr_t)buf & PAGE_OFFSET_MASK, cPages);
1880 Assert(cPages <= cMaxPages);
1881 vbsf_unlock_user_pages(papPages, cPages, false /*fSetDirty*/, fLockPgHack);
1882
1883 cbRet += cbActual;
1884 buf = (uint8_t *)buf + cbActual;
1885 size -= cbActual;
1886
1887 offFile += cbActual;
1888 if ((file->f_flags & O_APPEND) && (g_fSfFeatures & SHFL_FEATURE_WRITE_UPDATES_OFFSET))
1889 offFile = pReq->Parms.off64Write.u.value64;
1890 if (offFile > i_size_read(inode))
1891 i_size_write(inode, offFile);
1892
1893 sf_i->force_restat = 1; /* mtime (and size) may have changed */
1894
1895 /*
1896 * Are we done already? If so commit the new file offset.
1897 */
1898 if (!size || cbActual < cbChunk) {
1899 *off = offFile;
1900 break;
1901 }
1902 } else {
1903 vbsf_unlock_user_pages(papPages, cPages, false /*fSetDirty*/, fLockPgHack);
1904 if (rc == VERR_NO_MEMORY && cMaxPages > 4) {
1905 /*
1906 * The host probably doesn't have enough heap to handle the
1907 * request, reduce the page count and retry.
1908 */
1909 cMaxPages /= 4;
1910 Assert(cMaxPages > 0);
1911 } else {
1912 /*
1913 * If we've successfully written stuff, return it rather than
1914 * the error. (Not sure if this is such a great idea...)
1915 */
1916 if (cbRet > 0) {
1917 SFLOGFLOW(("vbsf_reg_write: write at %#RX64 -> %Rrc; got cbRet=%#zx already\n", offFile, rc, cbRet));
1918 *off = offFile;
1919 } else {
1920 SFLOGFLOW(("vbsf_reg_write: write at %#RX64 -> %Rrc\n", offFile, rc));
1921 cbRet = -EPROTO;
1922 }
1923 break;
1924 }
1925 }
1926 }
1927 }
1928 if (papPagesFree)
1929 kfree(papPages);
1930 if (pReq)
1931 VbglR0PhysHeapFree(pReq);
1932 SFLOGFLOW(("vbsf_reg_write: returns %zd (%#zx), *off=%RX64 [lock]\n", cbRet, cbRet, *off));
1933 return cbRet;
1934}
1935
1936
1937/**
1938 * Write to a regular file.
1939 *
1940 * @param file the file
1941 * @param buf the buffer
1942 * @param size length of the buffer
1943 * @param off offset within the file
1944 * @returns the number of written bytes on success, Linux error code otherwise
1945 */
1946static ssize_t vbsf_reg_write(struct file *file, const char *buf, size_t size, loff_t * off)
1947{
1948 struct inode *inode = VBSF_GET_F_DENTRY(file)->d_inode;
1949 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
1950 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
1951 struct vbsf_reg_info *sf_r = file->private_data;
1952 struct address_space *mapping = inode->i_mapping;
1953 loff_t pos;
1954
1955 SFLOGFLOW(("vbsf_reg_write: inode=%p file=%p buf=%p size=%#zx off=%#llx\n", inode, file, buf, size, *off));
1956 Assert(sf_i);
1957 Assert(pSuperInfo);
1958 Assert(sf_r);
1959 AssertReturn(S_ISREG(inode->i_mode), -EINVAL);
1960
1961 pos = *off;
1962 if (file->f_flags & O_APPEND)
1963 pos = i_size_read(inode);
1964
1965 /** @todo XXX Check write permission according to inode->i_mode! */
1966
1967 if (!size) {
1968 if (file->f_flags & O_APPEND) /** @todo check if this is the consensus behavior... */
1969 *off = pos;
1970 return 0;
1971 }
1972
1973 /** @todo Implement the read-write caching mode. */
1974
1975 /*
1976 * If there are active writable mappings, coordinate with any
1977 * pending writes via those.
1978 */
1979 if ( mapping
1980 && mapping->nrpages > 0
1981 && mapping_writably_mapped(mapping)) {
1982#if RTLNX_VER_MIN(2,6,32)
1983 int err = filemap_fdatawait_range(mapping, pos, pos + size - 1);
1984 if (err)
1985 return err;
1986#else
1987 /** @todo ... */
1988#endif
1989 }
1990
1991 /*
1992 * For small requests, try use an embedded buffer provided we get a heap block
1993 * that does not cross page boundraries (see host code).
1994 */
1995 if (size <= PAGE_SIZE / 4 * 3 - RT_UOFFSETOF(VBOXSFWRITEEMBEDDEDREQ, abData[0]) /* see allocator */) {
1996 uint32_t const cbReq = RT_UOFFSETOF(VBOXSFWRITEEMBEDDEDREQ, abData[0]) + size;
1997 VBOXSFWRITEEMBEDDEDREQ *pReq = (VBOXSFWRITEEMBEDDEDREQ *)VbglR0PhysHeapAlloc(cbReq);
1998 if ( pReq
1999 && (PAGE_SIZE - ((uintptr_t)pReq & PAGE_OFFSET_MASK)) >= cbReq) {
2000 ssize_t cbRet;
2001 if (copy_from_user(pReq->abData, buf, size) == 0) {
2002 int vrc = VbglR0SfHostReqWriteEmbedded(pSuperInfo->map.root, pReq, sf_r->Handle.hHost,
2003 pos, (uint32_t)size);
2004 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
2005 if (RT_SUCCESS(vrc)) {
2006 cbRet = pReq->Parms.cb32Write.u.value32;
2007 AssertStmt(cbRet <= (ssize_t)size, cbRet = size);
2008 vbsf_reg_write_sync_page_cache(mapping, pos, (uint32_t)cbRet, pReq->abData,
2009 NULL /*papSrcPages*/, 0 /*offSrcPage0*/, 0 /*cSrcPages*/);
2010 pos += cbRet;
2011 if ((file->f_flags & O_APPEND) && (g_fSfFeatures & SHFL_FEATURE_WRITE_UPDATES_OFFSET))
2012 pos = pReq->Parms.off64Write.u.value64;
2013 *off = pos;
2014 if (pos > i_size_read(inode))
2015 i_size_write(inode, pos);
2016 } else
2017 cbRet = -EPROTO;
2018 sf_i->force_restat = 1; /* mtime (and size) may have changed */
2019 } else
2020 cbRet = -EFAULT;
2021
2022 VbglR0PhysHeapFree(pReq);
2023 SFLOGFLOW(("vbsf_reg_write: returns %zd (%#zx), *off=%RX64 [embed]\n", cbRet, cbRet, *off));
2024 return cbRet;
2025 }
2026 if (pReq)
2027 VbglR0PhysHeapFree(pReq);
2028 }
2029
2030#if 0 /* Turns out this is slightly slower than locking the pages even for 4KB reads (4.19/amd64). */
2031 /*
2032 * For medium sized requests try use a bounce buffer.
2033 */
2034 if (size <= _64K /** @todo make this configurable? */) {
2035 void *pvBounce = kmalloc(size, GFP_KERNEL);
2036 if (pvBounce) {
2037 if (copy_from_user(pvBounce, buf, size) == 0) {
2038 VBOXSFWRITEPGLSTREQ *pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
2039 if (pReq) {
2040 ssize_t cbRet;
2041 int vrc = VbglR0SfHostReqWriteContig(pSuperInfo->map.root, pReq, sf_r->handle, pos,
2042 (uint32_t)size, pvBounce, virt_to_phys(pvBounce));
2043 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
2044 if (RT_SUCCESS(vrc)) {
2045 cbRet = pReq->Parms.cb32Write.u.value32;
2046 AssertStmt(cbRet <= (ssize_t)size, cbRet = size);
2047 vbsf_reg_write_sync_page_cache(mapping, pos, (uint32_t)cbRet, (uint8_t const *)pvBounce,
2048 NULL /*papSrcPages*/, 0 /*offSrcPage0*/, 0 /*cSrcPages*/);
2049 pos += cbRet;
2050 *off = pos;
2051 if (pos > i_size_read(inode))
2052 i_size_write(inode, pos);
2053 } else
2054 cbRet = -EPROTO;
2055 sf_i->force_restat = 1; /* mtime (and size) may have changed */
2056 VbglR0PhysHeapFree(pReq);
2057 kfree(pvBounce);
2058 SFLOGFLOW(("vbsf_reg_write: returns %zd (%#zx), *off=%RX64 [bounce]\n", cbRet, cbRet, *off));
2059 return cbRet;
2060 }
2061 kfree(pvBounce);
2062 } else {
2063 kfree(pvBounce);
2064 SFLOGFLOW(("vbsf_reg_write: returns -EFAULT, *off=%RX64 [bounce]\n", *off));
2065 return -EFAULT;
2066 }
2067 }
2068 }
2069#endif
2070
2071 return vbsf_reg_write_locking(file, buf, size, off, pos, inode, sf_i, pSuperInfo, sf_r);
2072}
2073
2074#if RTLNX_VER_MIN(2,6,19)
2075
2076/**
2077 * Companion to vbsf_iter_lock_pages().
2078 */
2079DECLINLINE(void) vbsf_iter_unlock_pages(struct iov_iter *iter, struct page **papPages, size_t cPages, bool fSetDirty)
2080{
2081 /* We don't mark kernel pages dirty (KVECs, BVECs, PIPEs): */
2082 if (!iter_is_iovec(iter))
2083 fSetDirty = false;
2084
2085 while (cPages-- > 0)
2086 {
2087 struct page *pPage = papPages[cPages];
2088 if (fSetDirty && !PageReserved(pPage))
2089 set_page_dirty(pPage);
2090 vbsf_put_page(pPage);
2091 }
2092}
2093
2094
2095/**
2096 * Locks up to @a cMaxPages from the I/O vector iterator, advancing the
2097 * iterator.
2098 *
2099 * @returns 0 on success, negative errno value on failure.
2100 * @param iter The iterator to lock pages from.
2101 * @param fWrite Whether to write (true) or read (false) lock the pages.
2102 * @param pStash Where we stash peek results.
2103 * @param cMaxPages The maximum number of pages to get.
2104 * @param papPages Where to return the locked pages.
2105 * @param pcPages Where to return the number of pages.
2106 * @param poffPage0 Where to return the offset into the first page.
2107 * @param pcbChunk Where to return the number of bytes covered.
2108 */
2109static int vbsf_iter_lock_pages(struct iov_iter *iter, bool fWrite, struct vbsf_iter_stash *pStash, size_t cMaxPages,
2110 struct page **papPages, size_t *pcPages, size_t *poffPage0, size_t *pcbChunk)
2111{
2112 size_t cbChunk = 0;
2113 size_t cPages = 0;
2114 size_t offPage0 = 0;
2115 int rc = 0;
2116
2117 Assert(iov_iter_count(iter) + pStash->cb > 0);
2118 if (!(iter->type & ITER_KVEC)) {
2119 /*
2120 * Do we have a stashed page?
2121 */
2122 if (pStash->pPage) {
2123 papPages[0] = pStash->pPage;
2124 offPage0 = pStash->off;
2125 cbChunk = pStash->cb;
2126 cPages = 1;
2127 pStash->pPage = NULL;
2128 pStash->off = 0;
2129 pStash->cb = 0;
2130 if ( offPage0 + cbChunk < PAGE_SIZE
2131 || iov_iter_count(iter) == 0) {
2132 *poffPage0 = offPage0;
2133 *pcbChunk = cbChunk;
2134 *pcPages = cPages;
2135 SFLOGFLOW(("vbsf_iter_lock_pages: returns %d - cPages=%#zx offPage0=%#zx cbChunk=%zx (stashed)\n",
2136 rc, cPages, offPage0, cbChunk));
2137 return 0;
2138 }
2139 cMaxPages -= 1;
2140 SFLOG3(("vbsf_iter_lock_pages: Picked up stashed page: %#zx LB %#zx\n", offPage0, cbChunk));
2141 } else {
2142# if RTLNX_VER_MAX(4,11,0)
2143 /*
2144 * Copy out our starting point to assist rewinding.
2145 */
2146 pStash->offFromEnd = iov_iter_count(iter);
2147 pStash->Copy = *iter;
2148# endif
2149 }
2150
2151 /*
2152 * Get pages segment by segment.
2153 */
2154 do {
2155 /*
2156 * Make a special case of the first time thru here, since that's
2157 * the most typical scenario.
2158 */
2159 ssize_t cbSegRet;
2160 if (cPages == 0) {
2161# if RTLNX_VER_MAX(3,19,0)
2162 while (!iov_iter_single_seg_count(iter)) /* Old code didn't skip empty segments which caused EFAULTs. */
2163 iov_iter_advance(iter, 0);
2164# endif
2165 cbSegRet = iov_iter_get_pages(iter, papPages, iov_iter_count(iter), cMaxPages, &offPage0);
2166 if (cbSegRet > 0) {
2167 iov_iter_advance(iter, cbSegRet);
2168 cbChunk = (size_t)cbSegRet;
2169 cPages = RT_ALIGN_Z(offPage0 + cbSegRet, PAGE_SIZE) >> PAGE_SHIFT;
2170 cMaxPages -= cPages;
2171 SFLOG3(("vbsf_iter_lock_pages: iov_iter_get_pages -> %#zx @ %#zx; %#zx pages [first]\n", cbSegRet, offPage0, cPages));
2172 if ( cMaxPages == 0
2173 || ((offPage0 + (size_t)cbSegRet) & PAGE_OFFSET_MASK))
2174 break;
2175 } else {
2176 AssertStmt(cbSegRet < 0, cbSegRet = -EFAULT);
2177 rc = (int)cbSegRet;
2178 break;
2179 }
2180 } else {
2181 /*
2182 * Probe first page of new segment to check that we've got a zero offset and
2183 * can continue on the current chunk. Stash the page if the offset isn't zero.
2184 */
2185 size_t offPgProbe;
2186 size_t cbSeg = iov_iter_single_seg_count(iter);
2187 while (!cbSeg) {
2188 iov_iter_advance(iter, 0);
2189 cbSeg = iov_iter_single_seg_count(iter);
2190 }
2191 cbSegRet = iov_iter_get_pages(iter, &papPages[cPages], iov_iter_count(iter), 1, &offPgProbe);
2192 if (cbSegRet > 0) {
2193 iov_iter_advance(iter, cbSegRet); /** @todo maybe not do this if we stash the page? */
2194 Assert(offPgProbe + cbSegRet <= PAGE_SIZE);
2195 if (offPgProbe == 0) {
2196 cbChunk += cbSegRet;
2197 cPages += 1;
2198 cMaxPages -= 1;
2199 SFLOG3(("vbsf_iter_lock_pages: iov_iter_get_pages(1) -> %#zx @ %#zx\n", cbSegRet, offPgProbe));
2200 if ( cMaxPages == 0
2201 || cbSegRet != PAGE_SIZE)
2202 break;
2203
2204 /*
2205 * Get the rest of the segment (if anything remaining).
2206 */
2207 cbSeg -= cbSegRet;
2208 if (cbSeg > 0) {
2209 cbSegRet = iov_iter_get_pages(iter, &papPages[cPages], iov_iter_count(iter), cMaxPages, &offPgProbe);
2210 if (cbSegRet > 0) {
2211 size_t const cPgRet = RT_ALIGN_Z((size_t)cbSegRet, PAGE_SIZE) >> PAGE_SHIFT;
2212 Assert(offPgProbe == 0);
2213 iov_iter_advance(iter, cbSegRet);
2214 SFLOG3(("vbsf_iter_lock_pages: iov_iter_get_pages() -> %#zx; %#zx pages\n", cbSegRet, cPgRet));
2215 cPages += cPgRet;
2216 cMaxPages -= cPgRet;
2217 cbChunk += cbSegRet;
2218 if ( cMaxPages == 0
2219 || ((size_t)cbSegRet & PAGE_OFFSET_MASK))
2220 break;
2221 } else {
2222 AssertStmt(cbSegRet < 0, cbSegRet = -EFAULT);
2223 rc = (int)cbSegRet;
2224 break;
2225 }
2226 }
2227 } else {
2228 /* The segment didn't start at a page boundrary, so stash it for
2229 the next round: */
2230 SFLOGFLOW(("vbsf_iter_lock_pages: iov_iter_get_pages(1) -> %#zx @ %#zx; stashed\n", cbSegRet, offPgProbe));
2231 Assert(papPages[cPages]);
2232 pStash->pPage = papPages[cPages];
2233 pStash->off = offPgProbe;
2234 pStash->cb = cbSegRet;
2235 break;
2236 }
2237 } else {
2238 AssertStmt(cbSegRet < 0, cbSegRet = -EFAULT);
2239 rc = (int)cbSegRet;
2240 break;
2241 }
2242 }
2243 Assert(cMaxPages > 0);
2244 } while (iov_iter_count(iter) > 0);
2245
2246 } else {
2247 /*
2248 * The silly iov_iter_get_pages_alloc() function doesn't handle KVECs,
2249 * so everyone needs to do that by themselves.
2250 *
2251 * Note! Fixes here may apply to rtR0MemObjNativeLockKernel()
2252 * and vbsf_lock_user_pages_failed_check_kernel() as well.
2253 */
2254# if RTLNX_VER_MAX(4,11,0)
2255 pStash->offFromEnd = iov_iter_count(iter);
2256 pStash->Copy = *iter;
2257# endif
2258 do {
2259 uint8_t *pbBuf;
2260 size_t offStart;
2261 size_t cPgSeg;
2262
2263 size_t cbSeg = iov_iter_single_seg_count(iter);
2264 while (!cbSeg) {
2265 iov_iter_advance(iter, 0);
2266 cbSeg = iov_iter_single_seg_count(iter);
2267 }
2268
2269# if RTLNX_VER_MIN(3,19,0)
2270 pbBuf = iter->kvec->iov_base + iter->iov_offset;
2271# else
2272 pbBuf = iter->iov->iov_base + iter->iov_offset;
2273# endif
2274 offStart = (uintptr_t)pbBuf & PAGE_OFFSET_MASK;
2275 if (!cPages)
2276 offPage0 = offStart;
2277 else if (offStart)
2278 break;
2279
2280 cPgSeg = RT_ALIGN_Z(cbSeg, PAGE_SIZE) >> PAGE_SHIFT;
2281 if (cPgSeg > cMaxPages) {
2282 cPgSeg = cMaxPages;
2283 cbSeg = (cPgSeg << PAGE_SHIFT) - offStart;
2284 }
2285
2286 rc = vbsf_lock_kernel_pages(pbBuf, fWrite, cPgSeg, &papPages[cPages]);
2287 if (rc == 0) {
2288 iov_iter_advance(iter, cbSeg);
2289 cbChunk += cbSeg;
2290 cPages += cPgSeg;
2291 cMaxPages -= cPgSeg;
2292 if ( cMaxPages == 0
2293 || ((offStart + cbSeg) & PAGE_OFFSET_MASK) != 0)
2294 break;
2295 } else
2296 break;
2297 } while (iov_iter_count(iter) > 0);
2298 }
2299
2300 /*
2301 * Clean up if we failed; set return values.
2302 */
2303 if (rc == 0) {
2304 /* likely */
2305 } else {
2306 if (cPages > 0)
2307 vbsf_iter_unlock_pages(iter, papPages, cPages, false /*fSetDirty*/);
2308 offPage0 = cbChunk = cPages = 0;
2309 }
2310 *poffPage0 = offPage0;
2311 *pcbChunk = cbChunk;
2312 *pcPages = cPages;
2313 SFLOGFLOW(("vbsf_iter_lock_pages: returns %d - cPages=%#zx offPage0=%#zx cbChunk=%zx\n", rc, cPages, offPage0, cbChunk));
2314 return rc;
2315}
2316
2317
2318/**
2319 * Rewinds the I/O vector.
2320 */
2321static bool vbsf_iter_rewind(struct iov_iter *iter, struct vbsf_iter_stash *pStash, size_t cbToRewind, size_t cbChunk)
2322{
2323 size_t cbExtra;
2324 if (!pStash->pPage) {
2325 cbExtra = 0;
2326 } else {
2327 cbExtra = pStash->cb;
2328 vbsf_put_page(pStash->pPage);
2329 pStash->pPage = NULL;
2330 pStash->cb = 0;
2331 pStash->off = 0;
2332 }
2333
2334# if RTLNX_VER_MIN(4,11,0) || RTLNX_VER_MAX(3,16,0)
2335 iov_iter_revert(iter, cbToRewind + cbExtra);
2336 return true;
2337# else
2338 /** @todo impl this */
2339 return false;
2340# endif
2341}
2342
2343
2344/**
2345 * Cleans up the page locking stash.
2346 */
2347DECLINLINE(void) vbsf_iter_cleanup_stash(struct iov_iter *iter, struct vbsf_iter_stash *pStash)
2348{
2349 if (pStash->pPage)
2350 vbsf_iter_rewind(iter, pStash, 0, 0);
2351}
2352
2353
2354/**
2355 * Calculates the longest span of pages we could transfer to the host in a
2356 * single request.
2357 *
2358 * @returns Page count, non-zero.
2359 * @param iter The I/O vector iterator to inspect.
2360 */
2361static size_t vbsf_iter_max_span_of_pages(struct iov_iter *iter)
2362{
2363 size_t cPages;
2364# if RTLNX_VER_MIN(3,16,0)
2365 if (iter_is_iovec(iter) || (iter->type & ITER_KVEC)) {
2366#endif
2367 const struct iovec *pCurIov = iter->iov;
2368 size_t cLeft = iter->nr_segs;
2369 size_t cPagesSpan = 0;
2370
2371 /* iovect and kvec are identical, except for the __user tagging of iov_base. */
2372 AssertCompileMembersSameSizeAndOffset(struct iovec, iov_base, struct kvec, iov_base);
2373 AssertCompileMembersSameSizeAndOffset(struct iovec, iov_len, struct kvec, iov_len);
2374 AssertCompile(sizeof(struct iovec) == sizeof(struct kvec));
2375
2376 cPages = 1;
2377 AssertReturn(cLeft > 0, cPages);
2378
2379 /* Special case: segment offset. */
2380 if (iter->iov_offset > 0) {
2381 if (iter->iov_offset < pCurIov->iov_len) {
2382 size_t const cbSegLeft = pCurIov->iov_len - iter->iov_offset;
2383 size_t const offPage0 = ((uintptr_t)pCurIov->iov_base + iter->iov_offset) & PAGE_OFFSET_MASK;
2384 cPages = cPagesSpan = RT_ALIGN_Z(offPage0 + cbSegLeft, PAGE_SIZE) >> PAGE_SHIFT;
2385 if ((offPage0 + cbSegLeft) & PAGE_OFFSET_MASK)
2386 cPagesSpan = 0;
2387 }
2388 SFLOGFLOW(("vbsf_iter: seg[0]= %p LB %#zx\n", pCurIov->iov_base, pCurIov->iov_len));
2389 pCurIov++;
2390 cLeft--;
2391 }
2392
2393 /* Full segments. */
2394 while (cLeft-- > 0) {
2395 if (pCurIov->iov_len > 0) {
2396 size_t const offPage0 = (uintptr_t)pCurIov->iov_base & PAGE_OFFSET_MASK;
2397 if (offPage0 == 0) {
2398 if (!(pCurIov->iov_len & PAGE_OFFSET_MASK)) {
2399 cPagesSpan += pCurIov->iov_len >> PAGE_SHIFT;
2400 } else {
2401 cPagesSpan += RT_ALIGN_Z(pCurIov->iov_len, PAGE_SIZE) >> PAGE_SHIFT;
2402 if (cPagesSpan > cPages)
2403 cPages = cPagesSpan;
2404 cPagesSpan = 0;
2405 }
2406 } else {
2407 if (cPagesSpan > cPages)
2408 cPages = cPagesSpan;
2409 if (!((offPage0 + pCurIov->iov_len) & PAGE_OFFSET_MASK)) {
2410 cPagesSpan = pCurIov->iov_len >> PAGE_SHIFT;
2411 } else {
2412 cPagesSpan += RT_ALIGN_Z(offPage0 + pCurIov->iov_len, PAGE_SIZE) >> PAGE_SHIFT;
2413 if (cPagesSpan > cPages)
2414 cPages = cPagesSpan;
2415 cPagesSpan = 0;
2416 }
2417 }
2418 }
2419 SFLOGFLOW(("vbsf_iter: seg[%u]= %p LB %#zx\n", iter->nr_segs - cLeft, pCurIov->iov_base, pCurIov->iov_len));
2420 pCurIov++;
2421 }
2422 if (cPagesSpan > cPages)
2423 cPages = cPagesSpan;
2424# if RTLNX_VER_MIN(3,16,0)
2425 } else {
2426 /* Won't bother with accurate counts for the next two types, just make
2427 some rough estimates (does pipes have segments?): */
2428 size_t cSegs = iter->type & ITER_BVEC ? RT_MAX(1, iter->nr_segs) : 1;
2429 cPages = (iov_iter_count(iter) + (PAGE_SIZE * 2 - 2) * cSegs) >> PAGE_SHIFT;
2430 }
2431# endif
2432 SFLOGFLOW(("vbsf_iter_max_span_of_pages: returns %#zx\n", cPages));
2433 return cPages;
2434}
2435
2436
2437/**
2438 * Worker for vbsf_reg_read_iter() that deals with larger reads using page
2439 * locking.
2440 */
2441static ssize_t vbsf_reg_read_iter_locking(struct kiocb *kio, struct iov_iter *iter, size_t cbToRead,
2442 struct vbsf_super_info *pSuperInfo, struct vbsf_reg_info *sf_r)
2443{
2444 /*
2445 * Estimate how many pages we may possible submit in a single request so
2446 * that we can allocate matching request buffer and page array.
2447 */
2448 struct page *apPagesStack[16];
2449 struct page **papPages = &apPagesStack[0];
2450 struct page **papPagesFree = NULL;
2451 VBOXSFREADPGLSTREQ *pReq;
2452 ssize_t cbRet = 0;
2453 size_t cMaxPages = vbsf_iter_max_span_of_pages(iter);
2454 cMaxPages = RT_MIN(RT_MAX(pSuperInfo->cMaxIoPages, 2), cMaxPages);
2455
2456 pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ, PgLst.aPages[cMaxPages]));
2457 while (!pReq && cMaxPages > 4) {
2458 cMaxPages /= 2;
2459 pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFREADPGLSTREQ, PgLst.aPages[cMaxPages]));
2460 }
2461 if (pReq && cMaxPages > RT_ELEMENTS(apPagesStack))
2462 papPagesFree = papPages = kmalloc(cMaxPages * sizeof(sizeof(papPages[0])), GFP_KERNEL);
2463 if (pReq && papPages) {
2464
2465 /*
2466 * The read loop.
2467 */
2468 struct vbsf_iter_stash Stash = VBSF_ITER_STASH_INITIALIZER;
2469 do {
2470 /*
2471 * Grab as many pages as we can. This means that if adjacent
2472 * segments both starts and ends at a page boundrary, we can
2473 * do them both in the same transfer from the host.
2474 */
2475 size_t cPages = 0;
2476 size_t cbChunk = 0;
2477 size_t offPage0 = 0;
2478 int rc = vbsf_iter_lock_pages(iter, true /*fWrite*/, &Stash, cMaxPages, papPages, &cPages, &offPage0, &cbChunk);
2479 if (rc == 0) {
2480 size_t iPage = cPages;
2481 while (iPage-- > 0)
2482 pReq->PgLst.aPages[iPage] = page_to_phys(papPages[iPage]);
2483 pReq->PgLst.offFirstPage = (uint16_t)offPage0;
2484 AssertStmt(cbChunk <= cbToRead, cbChunk = cbToRead);
2485 } else {
2486 cbRet = rc;
2487 break;
2488 }
2489
2490 /*
2491 * Issue the request and unlock the pages.
2492 */
2493 rc = VbglR0SfHostReqReadPgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, kio->ki_pos, cbChunk, cPages);
2494 SFLOGFLOW(("vbsf_reg_read_iter_locking: VbglR0SfHostReqReadPgLst -> %d (cbActual=%#x cbChunk=%#zx of %#zx cPages=%#zx offPage0=%#x\n",
2495 rc, pReq->Parms.cb32Read.u.value32, cbChunk, cbToRead, cPages, offPage0));
2496
2497 vbsf_iter_unlock_pages(iter, papPages, cPages, true /*fSetDirty*/);
2498
2499 if (RT_SUCCESS(rc)) {
2500 /*
2501 * Success, advance position and buffer.
2502 */
2503 uint32_t cbActual = pReq->Parms.cb32Read.u.value32;
2504 AssertStmt(cbActual <= cbChunk, cbActual = cbChunk);
2505 cbRet += cbActual;
2506 kio->ki_pos += cbActual;
2507 cbToRead -= cbActual;
2508
2509 /*
2510 * Are we done already?
2511 */
2512 if (!cbToRead)
2513 break;
2514 if (cbActual < cbChunk) { /* We ASSUME end-of-file here. */
2515 if (vbsf_iter_rewind(iter, &Stash, cbChunk - cbActual, cbActual))
2516 iov_iter_truncate(iter, 0);
2517 break;
2518 }
2519 } else {
2520 /*
2521 * Try rewind the iter structure.
2522 */
2523 bool const fRewindOkay = vbsf_iter_rewind(iter, &Stash, cbChunk, cbChunk);
2524 if (rc == VERR_NO_MEMORY && cMaxPages > 4 && fRewindOkay) {
2525 /*
2526 * The host probably doesn't have enough heap to handle the
2527 * request, reduce the page count and retry.
2528 */
2529 cMaxPages /= 4;
2530 Assert(cMaxPages > 0);
2531 } else {
2532 /*
2533 * If we've successfully read stuff, return it rather than
2534 * the error. (Not sure if this is such a great idea...)
2535 */
2536 if (cbRet <= 0)
2537 cbRet = -EPROTO;
2538 break;
2539 }
2540 }
2541 } while (cbToRead > 0);
2542
2543 vbsf_iter_cleanup_stash(iter, &Stash);
2544 }
2545 else
2546 cbRet = -ENOMEM;
2547 if (papPagesFree)
2548 kfree(papPages);
2549 if (pReq)
2550 VbglR0PhysHeapFree(pReq);
2551 SFLOGFLOW(("vbsf_reg_read_iter_locking: returns %#zx (%zd)\n", cbRet, cbRet));
2552 return cbRet;
2553}
2554
2555
2556/**
2557 * Read into I/O vector iterator.
2558 *
2559 * @returns Number of bytes read on success, negative errno on error.
2560 * @param kio The kernel I/O control block (or something like that).
2561 * @param iter The I/O vector iterator describing the buffer.
2562 */
2563# if RTLNX_VER_MIN(3,16,0)
2564static ssize_t vbsf_reg_read_iter(struct kiocb *kio, struct iov_iter *iter)
2565# else
2566static ssize_t vbsf_reg_aio_read(struct kiocb *kio, const struct iovec *iov, unsigned long cSegs, loff_t offFile)
2567# endif
2568{
2569# if RTLNX_VER_MAX(3,16,0)
2570 struct vbsf_iov_iter fake_iter = VBSF_IOV_ITER_INITIALIZER(cSegs, iov, 0 /*write*/);
2571 struct vbsf_iov_iter *iter = &fake_iter;
2572# endif
2573 size_t cbToRead = iov_iter_count(iter);
2574 struct inode *inode = VBSF_GET_F_DENTRY(kio->ki_filp)->d_inode;
2575 struct address_space *mapping = inode->i_mapping;
2576
2577 struct vbsf_reg_info *sf_r = kio->ki_filp->private_data;
2578 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
2579
2580 SFLOGFLOW(("vbsf_reg_read_iter: inode=%p file=%p size=%#zx off=%#llx type=%#x\n",
2581 inode, kio->ki_filp, cbToRead, kio->ki_pos, iter->type));
2582 AssertReturn(S_ISREG(inode->i_mode), -EINVAL);
2583
2584 /*
2585 * Do we have anything at all to do here?
2586 */
2587 if (!cbToRead)
2588 return 0;
2589
2590 /*
2591 * If there is a mapping and O_DIRECT isn't in effect, we must at a
2592 * heed dirty pages in the mapping and read from them. For simplicity
2593 * though, we just do page cache reading when there are writable
2594 * mappings around with any kind of pages loaded.
2595 */
2596 if (vbsf_should_use_cached_read(kio->ki_filp, mapping, pSuperInfo)) {
2597# if RTLNX_VER_MIN(3,16,0)
2598 return generic_file_read_iter(kio, iter);
2599# else
2600 return generic_file_aio_read(kio, iov, cSegs, offFile);
2601# endif
2602 }
2603
2604 /*
2605 * Now now we reject async I/O requests.
2606 */
2607 if (!is_sync_kiocb(kio)) {
2608 SFLOGFLOW(("vbsf_reg_read_iter: async I/O not yet supported\n")); /** @todo extend FsPerf with AIO tests. */
2609 return -EOPNOTSUPP;
2610 }
2611
2612 /*
2613 * For small requests, try use an embedded buffer provided we get a heap block
2614 * that does not cross page boundraries (see host code).
2615 */
2616 if (cbToRead <= PAGE_SIZE / 4 * 3 - RT_UOFFSETOF(VBOXSFREADEMBEDDEDREQ, abData[0]) /* see allocator */) {
2617 uint32_t const cbReq = RT_UOFFSETOF(VBOXSFREADEMBEDDEDREQ, abData[0]) + cbToRead;
2618 VBOXSFREADEMBEDDEDREQ *pReq = (VBOXSFREADEMBEDDEDREQ *)VbglR0PhysHeapAlloc(cbReq);
2619 if (pReq) {
2620 if ((PAGE_SIZE - ((uintptr_t)pReq & PAGE_OFFSET_MASK)) >= cbReq) {
2621 ssize_t cbRet;
2622 int vrc = VbglR0SfHostReqReadEmbedded(pSuperInfo->map.root, pReq, sf_r->Handle.hHost,
2623 kio->ki_pos, (uint32_t)cbToRead);
2624 if (RT_SUCCESS(vrc)) {
2625 cbRet = pReq->Parms.cb32Read.u.value32;
2626 AssertStmt(cbRet <= (ssize_t)cbToRead, cbRet = cbToRead);
2627 if (copy_to_iter(pReq->abData, cbRet, iter) == cbRet) {
2628 kio->ki_pos += cbRet;
2629 if (cbRet < cbToRead)
2630 iov_iter_truncate(iter, 0);
2631 } else
2632 cbRet = -EFAULT;
2633 } else
2634 cbRet = -EPROTO;
2635 VbglR0PhysHeapFree(pReq);
2636 SFLOGFLOW(("vbsf_reg_read_iter: returns %#zx (%zd)\n", cbRet, cbRet));
2637 return cbRet;
2638 }
2639 VbglR0PhysHeapFree(pReq);
2640 }
2641 }
2642
2643 /*
2644 * Otherwise do the page locking thing.
2645 */
2646 return vbsf_reg_read_iter_locking(kio, iter, cbToRead, pSuperInfo, sf_r);
2647}
2648
2649
2650/**
2651 * Worker for vbsf_reg_write_iter() that deals with larger writes using page
2652 * locking.
2653 */
2654static ssize_t vbsf_reg_write_iter_locking(struct kiocb *kio, struct iov_iter *iter, size_t cbToWrite, loff_t offFile,
2655 struct vbsf_super_info *pSuperInfo, struct vbsf_reg_info *sf_r, struct inode *inode,
2656 struct vbsf_inode_info *sf_i, struct address_space *mapping, bool fAppend)
2657{
2658 /*
2659 * Estimate how many pages we may possible submit in a single request so
2660 * that we can allocate matching request buffer and page array.
2661 */
2662 struct page *apPagesStack[16];
2663 struct page **papPages = &apPagesStack[0];
2664 struct page **papPagesFree = NULL;
2665 VBOXSFWRITEPGLSTREQ *pReq;
2666 ssize_t cbRet = 0;
2667 size_t cMaxPages = vbsf_iter_max_span_of_pages(iter);
2668 cMaxPages = RT_MIN(RT_MAX(pSuperInfo->cMaxIoPages, 2), cMaxPages);
2669
2670 pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFWRITEPGLSTREQ, PgLst.aPages[cMaxPages]));
2671 while (!pReq && cMaxPages > 4) {
2672 cMaxPages /= 2;
2673 pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(RT_UOFFSETOF_DYN(VBOXSFWRITEPGLSTREQ, PgLst.aPages[cMaxPages]));
2674 }
2675 if (pReq && cMaxPages > RT_ELEMENTS(apPagesStack))
2676 papPagesFree = papPages = kmalloc(cMaxPages * sizeof(sizeof(papPages[0])), GFP_KERNEL);
2677 if (pReq && papPages) {
2678
2679 /*
2680 * The write loop.
2681 */
2682 struct vbsf_iter_stash Stash = VBSF_ITER_STASH_INITIALIZER;
2683 do {
2684 /*
2685 * Grab as many pages as we can. This means that if adjacent
2686 * segments both starts and ends at a page boundrary, we can
2687 * do them both in the same transfer from the host.
2688 */
2689 size_t cPages = 0;
2690 size_t cbChunk = 0;
2691 size_t offPage0 = 0;
2692 int rc = vbsf_iter_lock_pages(iter, false /*fWrite*/, &Stash, cMaxPages, papPages, &cPages, &offPage0, &cbChunk);
2693 if (rc == 0) {
2694 size_t iPage = cPages;
2695 while (iPage-- > 0)
2696 pReq->PgLst.aPages[iPage] = page_to_phys(papPages[iPage]);
2697 pReq->PgLst.offFirstPage = (uint16_t)offPage0;
2698 AssertStmt(cbChunk <= cbToWrite, cbChunk = cbToWrite);
2699 } else {
2700 cbRet = rc;
2701 break;
2702 }
2703
2704 /*
2705 * Issue the request and unlock the pages.
2706 */
2707 rc = VbglR0SfHostReqWritePgLst(pSuperInfo->map.root, pReq, sf_r->Handle.hHost, offFile, cbChunk, cPages);
2708 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
2709 SFLOGFLOW(("vbsf_reg_write_iter_locking: VbglR0SfHostReqWritePgLst -> %d (cbActual=%#x cbChunk=%#zx of %#zx cPages=%#zx offPage0=%#x\n",
2710 rc, pReq->Parms.cb32Write.u.value32, cbChunk, cbToWrite, cPages, offPage0));
2711 if (RT_SUCCESS(rc)) {
2712 /*
2713 * Success, advance position and buffer.
2714 */
2715 uint32_t cbActual = pReq->Parms.cb32Write.u.value32;
2716 AssertStmt(cbActual <= cbChunk, cbActual = cbChunk);
2717
2718 vbsf_reg_write_sync_page_cache(mapping, offFile, cbActual, NULL /*pbSrcBuf*/, papPages, offPage0, cPages);
2719 vbsf_iter_unlock_pages(iter, papPages, cPages, false /*fSetDirty*/);
2720
2721 cbRet += cbActual;
2722 cbToWrite -= cbActual;
2723
2724 offFile += cbActual;
2725 if (fAppend && (g_fSfFeatures & SHFL_FEATURE_WRITE_UPDATES_OFFSET))
2726 offFile = pReq->Parms.off64Write.u.value64;
2727 kio->ki_pos = offFile;
2728 if (offFile > i_size_read(inode))
2729 i_size_write(inode, offFile);
2730
2731 sf_i->force_restat = 1; /* mtime (and size) may have changed */
2732
2733 /*
2734 * Are we done already?
2735 */
2736 if (!cbToWrite)
2737 break;
2738 if (cbActual < cbChunk) { /* We ASSUME end-of-file here. */
2739 if (vbsf_iter_rewind(iter, &Stash, cbChunk - cbActual, cbActual))
2740 iov_iter_truncate(iter, 0);
2741 break;
2742 }
2743 } else {
2744 /*
2745 * Try rewind the iter structure.
2746 */
2747 bool fRewindOkay;
2748 vbsf_iter_unlock_pages(iter, papPages, cPages, false /*fSetDirty*/);
2749 fRewindOkay = vbsf_iter_rewind(iter, &Stash, cbChunk, cbChunk);
2750 if (rc == VERR_NO_MEMORY && cMaxPages > 4 && fRewindOkay) {
2751 /*
2752 * The host probably doesn't have enough heap to handle the
2753 * request, reduce the page count and retry.
2754 */
2755 cMaxPages /= 4;
2756 Assert(cMaxPages > 0);
2757 } else {
2758 /*
2759 * If we've successfully written stuff, return it rather than
2760 * the error. (Not sure if this is such a great idea...)
2761 */
2762 if (cbRet <= 0)
2763 cbRet = -EPROTO;
2764 break;
2765 }
2766 }
2767 } while (cbToWrite > 0);
2768
2769 vbsf_iter_cleanup_stash(iter, &Stash);
2770 }
2771 else
2772 cbRet = -ENOMEM;
2773 if (papPagesFree)
2774 kfree(papPages);
2775 if (pReq)
2776 VbglR0PhysHeapFree(pReq);
2777 SFLOGFLOW(("vbsf_reg_write_iter_locking: returns %#zx (%zd)\n", cbRet, cbRet));
2778 return cbRet;
2779}
2780
2781
2782/**
2783 * Write from I/O vector iterator.
2784 *
2785 * @returns Number of bytes written on success, negative errno on error.
2786 * @param kio The kernel I/O control block (or something like that).
2787 * @param iter The I/O vector iterator describing the buffer.
2788 */
2789# if RTLNX_VER_MIN(3,16,0)
2790static ssize_t vbsf_reg_write_iter(struct kiocb *kio, struct iov_iter *iter)
2791# else
2792static ssize_t vbsf_reg_aio_write(struct kiocb *kio, const struct iovec *iov, unsigned long cSegs, loff_t offFile)
2793# endif
2794{
2795# if RTLNX_VER_MAX(3,16,0)
2796 struct vbsf_iov_iter fake_iter = VBSF_IOV_ITER_INITIALIZER(cSegs, iov, 1 /*write*/);
2797 struct vbsf_iov_iter *iter = &fake_iter;
2798# endif
2799 size_t cbToWrite = iov_iter_count(iter);
2800 struct inode *inode = VBSF_GET_F_DENTRY(kio->ki_filp)->d_inode;
2801 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
2802 struct address_space *mapping = inode->i_mapping;
2803
2804 struct vbsf_reg_info *sf_r = kio->ki_filp->private_data;
2805 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
2806# if RTLNX_VER_MIN(3,16,0)
2807 loff_t offFile = kio->ki_pos;
2808# endif
2809# if RTLNX_VER_MIN(4,1,0)
2810 bool const fAppend = RT_BOOL(kio->ki_flags & IOCB_APPEND);
2811# else
2812 bool const fAppend = RT_BOOL(kio->ki_filp->f_flags & O_APPEND);
2813# endif
2814
2815
2816 SFLOGFLOW(("vbsf_reg_write_iter: inode=%p file=%p size=%#zx off=%#llx type=%#x\n",
2817 inode, kio->ki_filp, cbToWrite, offFile, iter->type));
2818 AssertReturn(S_ISREG(inode->i_mode), -EINVAL);
2819
2820 /*
2821 * Enforce APPEND flag (more later).
2822 */
2823 if (fAppend)
2824 kio->ki_pos = offFile = i_size_read(inode);
2825
2826 /*
2827 * Do we have anything at all to do here?
2828 */
2829 if (!cbToWrite)
2830 return 0;
2831
2832 /** @todo Implement the read-write caching mode. */
2833
2834 /*
2835 * Now now we reject async I/O requests.
2836 */
2837 if (!is_sync_kiocb(kio)) {
2838 SFLOGFLOW(("vbsf_reg_write_iter: async I/O not yet supported\n")); /** @todo extend FsPerf with AIO tests. */
2839 return -EOPNOTSUPP;
2840 }
2841
2842 /*
2843 * If there are active writable mappings, coordinate with any
2844 * pending writes via those.
2845 */
2846 if ( mapping
2847 && mapping->nrpages > 0
2848 && mapping_writably_mapped(mapping)) {
2849# if RTLNX_VER_MIN(2,6,32)
2850 int err = filemap_fdatawait_range(mapping, offFile, offFile + cbToWrite - 1);
2851 if (err)
2852 return err;
2853# else
2854 /** @todo ... */
2855# endif
2856 }
2857
2858 /*
2859 * For small requests, try use an embedded buffer provided we get a heap block
2860 * that does not cross page boundraries (see host code).
2861 */
2862 if (cbToWrite <= PAGE_SIZE / 4 * 3 - RT_UOFFSETOF(VBOXSFWRITEEMBEDDEDREQ, abData[0]) /* see allocator */) {
2863 uint32_t const cbReq = RT_UOFFSETOF(VBOXSFWRITEEMBEDDEDREQ, abData[0]) + cbToWrite;
2864 VBOXSFWRITEEMBEDDEDREQ *pReq = (VBOXSFWRITEEMBEDDEDREQ *)VbglR0PhysHeapAlloc(cbReq);
2865 if (pReq) {
2866 if ((PAGE_SIZE - ((uintptr_t)pReq & PAGE_OFFSET_MASK)) >= cbReq) {
2867 ssize_t cbRet;
2868 if (copy_from_iter(pReq->abData, cbToWrite, iter) == cbToWrite) {
2869 int vrc = VbglR0SfHostReqWriteEmbedded(pSuperInfo->map.root, pReq, sf_r->Handle.hHost,
2870 offFile, (uint32_t)cbToWrite);
2871 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
2872 if (RT_SUCCESS(vrc)) {
2873 cbRet = pReq->Parms.cb32Write.u.value32;
2874 AssertStmt(cbRet <= (ssize_t)cbToWrite, cbRet = cbToWrite);
2875 vbsf_reg_write_sync_page_cache(mapping, offFile, (uint32_t)cbRet, pReq->abData,
2876 NULL /*papSrcPages*/, 0 /*offSrcPage0*/, 0 /*cSrcPages*/);
2877
2878 offFile += cbRet;
2879 if (fAppend && (g_fSfFeatures & SHFL_FEATURE_WRITE_UPDATES_OFFSET))
2880 offFile = pReq->Parms.off64Write.u.value64;
2881 kio->ki_pos = offFile;
2882 if (offFile > i_size_read(inode))
2883 i_size_write(inode, offFile);
2884
2885# if RTLNX_VER_MIN(4,11,0)
2886 if ((size_t)cbRet < cbToWrite)
2887 iov_iter_revert(iter, cbToWrite - cbRet);
2888# endif
2889 } else
2890 cbRet = -EPROTO;
2891 sf_i->force_restat = 1; /* mtime (and size) may have changed */
2892 } else
2893 cbRet = -EFAULT;
2894 VbglR0PhysHeapFree(pReq);
2895 SFLOGFLOW(("vbsf_reg_write_iter: returns %#zx (%zd)\n", cbRet, cbRet));
2896 return cbRet;
2897 }
2898 VbglR0PhysHeapFree(pReq);
2899 }
2900 }
2901
2902 /*
2903 * Otherwise do the page locking thing.
2904 */
2905 return vbsf_reg_write_iter_locking(kio, iter, cbToWrite, offFile, pSuperInfo, sf_r, inode, sf_i, mapping, fAppend);
2906}
2907
2908#endif /* >= 2.6.19 */
2909
2910/**
2911 * Used by vbsf_reg_open() and vbsf_inode_atomic_open() to
2912 *
2913 * @returns shared folders create flags.
2914 * @param fLnxOpen The linux O_XXX flags to convert.
2915 * @param pfHandle Pointer to vbsf_handle::fFlags.
2916 * @param pszCaller Caller, for logging purposes.
2917 */
2918uint32_t vbsf_linux_oflags_to_vbox(unsigned fLnxOpen, uint32_t *pfHandle, const char *pszCaller)
2919{
2920 uint32_t fVBoxFlags = SHFL_CF_ACCESS_DENYNONE;
2921
2922 /*
2923 * Disposition.
2924 */
2925 if (fLnxOpen & O_CREAT) {
2926 Log(("%s: O_CREAT set\n", pszCaller));
2927 fVBoxFlags |= SHFL_CF_ACT_CREATE_IF_NEW;
2928 if (fLnxOpen & O_EXCL) {
2929 Log(("%s: O_EXCL set\n", pszCaller));
2930 fVBoxFlags |= SHFL_CF_ACT_FAIL_IF_EXISTS;
2931 } else if (fLnxOpen & O_TRUNC) {
2932 Log(("%s: O_TRUNC set\n", pszCaller));
2933 fVBoxFlags |= SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
2934 } else
2935 fVBoxFlags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
2936 } else {
2937 fVBoxFlags |= SHFL_CF_ACT_FAIL_IF_NEW;
2938 if (fLnxOpen & O_TRUNC) {
2939 Log(("%s: O_TRUNC set\n", pszCaller));
2940 fVBoxFlags |= SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
2941 }
2942 }
2943
2944 /*
2945 * Access.
2946 */
2947 switch (fLnxOpen & O_ACCMODE) {
2948 case O_RDONLY:
2949 fVBoxFlags |= SHFL_CF_ACCESS_READ;
2950 *pfHandle |= VBSF_HANDLE_F_READ;
2951 break;
2952
2953 case O_WRONLY:
2954 fVBoxFlags |= SHFL_CF_ACCESS_WRITE;
2955 *pfHandle |= VBSF_HANDLE_F_WRITE;
2956 break;
2957
2958 case O_RDWR:
2959 fVBoxFlags |= SHFL_CF_ACCESS_READWRITE;
2960 *pfHandle |= VBSF_HANDLE_F_READ | VBSF_HANDLE_F_WRITE;
2961 break;
2962
2963 default:
2964 BUG();
2965 }
2966
2967 if (fLnxOpen & O_APPEND) {
2968 Log(("%s: O_APPEND set\n", pszCaller));
2969 fVBoxFlags |= SHFL_CF_ACCESS_APPEND;
2970 *pfHandle |= VBSF_HANDLE_F_APPEND;
2971 }
2972
2973 /*
2974 * Only directories?
2975 */
2976 if (fLnxOpen & O_DIRECTORY) {
2977 Log(("%s: O_DIRECTORY set\n", pszCaller));
2978 fVBoxFlags |= SHFL_CF_DIRECTORY;
2979 }
2980
2981 return fVBoxFlags;
2982}
2983
2984
2985/**
2986 * Open a regular file.
2987 *
2988 * @param inode the inode
2989 * @param file the file
2990 * @returns 0 on success, Linux error code otherwise
2991 */
2992static int vbsf_reg_open(struct inode *inode, struct file *file)
2993{
2994 int rc, rc_linux = 0;
2995 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
2996 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
2997 struct dentry *dentry = VBSF_GET_F_DENTRY(file);
2998 struct vbsf_reg_info *sf_r;
2999 VBOXSFCREATEREQ *pReq;
3000
3001 SFLOGFLOW(("vbsf_reg_open: inode=%p file=%p flags=%#x %s\n", inode, file, file->f_flags, sf_i ? sf_i->path->String.ach : NULL));
3002 Assert(pSuperInfo);
3003 Assert(sf_i);
3004
3005 sf_r = kmalloc(sizeof(*sf_r), GFP_KERNEL);
3006 if (!sf_r) {
3007 LogRelFunc(("could not allocate reg info\n"));
3008 return -ENOMEM;
3009 }
3010
3011 RTListInit(&sf_r->Handle.Entry);
3012 sf_r->Handle.cRefs = 1;
3013 sf_r->Handle.fFlags = VBSF_HANDLE_F_FILE | VBSF_HANDLE_F_MAGIC;
3014 sf_r->Handle.hHost = SHFL_HANDLE_NIL;
3015
3016 /* Already open? */
3017 if (sf_i->handle != SHFL_HANDLE_NIL) {
3018 /*
3019 * This inode was created with vbsf_create_worker(). Check the CreateFlags:
3020 * O_CREAT, O_TRUNC: inherent true (file was just created). Not sure
3021 * about the access flags (SHFL_CF_ACCESS_*).
3022 */
3023 sf_i->force_restat = 1;
3024 sf_r->Handle.hHost = sf_i->handle;
3025 sf_i->handle = SHFL_HANDLE_NIL;
3026 file->private_data = sf_r;
3027
3028 sf_r->Handle.fFlags |= VBSF_HANDLE_F_READ | VBSF_HANDLE_F_WRITE; /** @todo fix */
3029 vbsf_handle_append(sf_i, &sf_r->Handle);
3030 SFLOGFLOW(("vbsf_reg_open: returns 0 (#1) - sf_i=%p hHost=%#llx\n", sf_i, sf_r->Handle.hHost));
3031 return 0;
3032 }
3033
3034 pReq = (VBOXSFCREATEREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq) + sf_i->path->u16Size);
3035 if (!pReq) {
3036 kfree(sf_r);
3037 LogRelFunc(("Failed to allocate a VBOXSFCREATEREQ buffer!\n"));
3038 return -ENOMEM;
3039 }
3040 memcpy(&pReq->StrPath, sf_i->path, SHFLSTRING_HEADER_SIZE + sf_i->path->u16Size);
3041 RT_ZERO(pReq->CreateParms);
3042 pReq->CreateParms.Handle = SHFL_HANDLE_NIL;
3043
3044 /* We check the value of pReq->CreateParms.Handle afterwards to
3045 * find out if the call succeeded or failed, as the API does not seem
3046 * to cleanly distinguish error and informational messages.
3047 *
3048 * Furthermore, we must set pReq->CreateParms.Handle to SHFL_HANDLE_NIL
3049 * to make the shared folders host service use our fMode parameter */
3050
3051 /* We ignore O_EXCL, as the Linux kernel seems to call create
3052 beforehand itself, so O_EXCL should always fail. */
3053 pReq->CreateParms.CreateFlags = vbsf_linux_oflags_to_vbox(file->f_flags & ~O_EXCL, &sf_r->Handle.fFlags, __FUNCTION__);
3054 pReq->CreateParms.Info.Attr.fMode = inode->i_mode;
3055 LogFunc(("vbsf_reg_open: calling VbglR0SfHostReqCreate, file %s, flags=%#x, %#x\n",
3056 sf_i->path->String.utf8, file->f_flags, pReq->CreateParms.CreateFlags));
3057 rc = VbglR0SfHostReqCreate(pSuperInfo->map.root, pReq);
3058 if (RT_FAILURE(rc)) {
3059 LogFunc(("VbglR0SfHostReqCreate failed flags=%d,%#x rc=%Rrc\n", file->f_flags, pReq->CreateParms.CreateFlags, rc));
3060 kfree(sf_r);
3061 VbglR0PhysHeapFree(pReq);
3062 return -RTErrConvertToErrno(rc);
3063 }
3064
3065 if (pReq->CreateParms.Handle != SHFL_HANDLE_NIL) {
3066 vbsf_dentry_chain_increase_ttl(dentry);
3067 vbsf_update_inode(inode, sf_i, &pReq->CreateParms.Info, pSuperInfo, false /*fInodeLocked*/, 0 /*fSetAttrs*/);
3068 rc_linux = 0;
3069 } else {
3070 switch (pReq->CreateParms.Result) {
3071 case SHFL_PATH_NOT_FOUND:
3072 vbsf_dentry_invalidate_ttl(dentry);
3073 rc_linux = -ENOENT;
3074 break;
3075 case SHFL_FILE_NOT_FOUND:
3076 vbsf_dentry_invalidate_ttl(dentry);
3077 /** @todo sf_dentry_increase_parent_ttl(file->f_dentry); if we can trust it. */
3078 rc_linux = -ENOENT;
3079 break;
3080 case SHFL_FILE_EXISTS:
3081 vbsf_dentry_chain_increase_ttl(dentry);
3082 vbsf_update_inode(inode, sf_i, &pReq->CreateParms.Info, pSuperInfo, false /*fInodeLocked*/, 0 /*fSetAttrs*/);
3083 rc_linux = -EEXIST;
3084 break;
3085 default:
3086 vbsf_dentry_chain_increase_parent_ttl(dentry);
3087 rc_linux = 0;
3088 break;
3089 }
3090 }
3091
3092 sf_r->Handle.hHost = pReq->CreateParms.Handle;
3093 file->private_data = sf_r;
3094 vbsf_handle_append(sf_i, &sf_r->Handle);
3095 VbglR0PhysHeapFree(pReq);
3096 SFLOGFLOW(("vbsf_reg_open: returns 0 (#2) - sf_i=%p hHost=%#llx\n", sf_i, sf_r->Handle.hHost));
3097 return rc_linux;
3098}
3099
3100
3101/**
3102 * Close a regular file.
3103 *
3104 * @param inode the inode
3105 * @param file the file
3106 * @returns 0 on success, Linux error code otherwise
3107 */
3108static int vbsf_reg_release(struct inode *inode, struct file *file)
3109{
3110 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
3111 struct vbsf_reg_info *sf_r = file->private_data;
3112
3113 SFLOGFLOW(("vbsf_reg_release: inode=%p file=%p\n", inode, file));
3114 if (sf_r) {
3115 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
3116 struct address_space *mapping = inode->i_mapping;
3117 Assert(pSuperInfo);
3118
3119 /* If we're closing the last handle for this inode, make sure the flush
3120 the mapping or we'll end up in vbsf_writepage without a handle. */
3121 if ( mapping
3122 && mapping->nrpages > 0
3123 /** @todo && last writable handle */ ) {
3124#if RTLNX_VER_MIN(2,4,25)
3125 if (filemap_fdatawrite(mapping) != -EIO)
3126#else
3127 if ( filemap_fdatasync(mapping) == 0
3128 && fsync_inode_data_buffers(inode) == 0)
3129#endif
3130 filemap_fdatawait(inode->i_mapping);
3131 }
3132
3133 /* Release sf_r, closing the handle if we're the last user. */
3134 file->private_data = NULL;
3135 vbsf_handle_release(&sf_r->Handle, pSuperInfo, "vbsf_reg_release");
3136
3137 sf_i->handle = SHFL_HANDLE_NIL;
3138 }
3139 return 0;
3140}
3141
3142
3143/**
3144 * Wrapper around generic/default seek function that ensures that we've got
3145 * the up-to-date file size when doing anything relative to EOF.
3146 *
3147 * The issue is that the host may extend the file while we weren't looking and
3148 * if the caller wishes to append data, it may end up overwriting existing data
3149 * if we operate with a stale size. So, we always retrieve the file size on EOF
3150 * relative seeks.
3151 */
3152static loff_t vbsf_reg_llseek(struct file *file, loff_t off, int whence)
3153{
3154 SFLOGFLOW(("vbsf_reg_llseek: file=%p off=%lld whence=%d\n", file, off, whence));
3155
3156 switch (whence) {
3157#ifdef SEEK_HOLE
3158 case SEEK_HOLE:
3159 case SEEK_DATA:
3160#endif
3161 case SEEK_END: {
3162 struct vbsf_reg_info *sf_r = file->private_data;
3163 int rc = vbsf_inode_revalidate_with_handle(VBSF_GET_F_DENTRY(file), sf_r->Handle.hHost,
3164 true /*fForce*/, false /*fInodeLocked*/);
3165 if (rc == 0)
3166 break;
3167 return rc;
3168 }
3169 }
3170
3171#if RTLNX_VER_MIN(2,4,8)
3172 return generic_file_llseek(file, off, whence);
3173#else
3174 return default_llseek(file, off, whence);
3175#endif
3176}
3177
3178
3179/**
3180 * Flush region of file - chiefly mmap/msync.
3181 *
3182 * We cannot use the noop_fsync / simple_sync_file here as that means
3183 * msync(,,MS_SYNC) will return before the data hits the host, thereby
3184 * causing coherency issues with O_DIRECT access to the same file as
3185 * well as any host interaction with the file.
3186 */
3187#if RTLNX_VER_MIN(3,1,0) \
3188 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_MIN(3,0,101) /** @todo figure when exactly */)
3189static int vbsf_reg_fsync(struct file *file, loff_t start, loff_t end, int datasync)
3190{
3191# if RTLNX_VER_MIN(3,16,0)
3192 return __generic_file_fsync(file, start, end, datasync);
3193# else
3194 return generic_file_fsync(file, start, end, datasync);
3195# endif
3196}
3197#elif RTLNX_VER_MIN(2,6,35)
3198static int vbsf_reg_fsync(struct file *file, int datasync)
3199{
3200 return generic_file_fsync(file, datasync);
3201}
3202#else /* < 2.6.35 */
3203static int vbsf_reg_fsync(struct file *file, struct dentry *dentry, int datasync)
3204{
3205# if RTLNX_VER_MIN(2,6,31)
3206 return simple_fsync(file, dentry, datasync);
3207# else
3208 int rc;
3209 struct inode *inode = dentry->d_inode;
3210 AssertReturn(inode, -EINVAL);
3211
3212 /** @todo What about file_fsync()? (<= 2.5.11) */
3213
3214# if RTLNX_VER_MIN(2,5,12)
3215 rc = sync_mapping_buffers(inode->i_mapping);
3216 if ( rc == 0
3217 && (inode->i_state & I_DIRTY)
3218 && ((inode->i_state & I_DIRTY_DATASYNC) || !datasync)
3219 ) {
3220 struct writeback_control wbc = {
3221 .sync_mode = WB_SYNC_ALL,
3222 .nr_to_write = 0
3223 };
3224 rc = sync_inode(inode, &wbc);
3225 }
3226# else /* < 2.5.12 */
3227 /** @todo
3228 * Somethings is buggy here or in the 2.4.21-27.EL kernel I'm testing on.
3229 *
3230 * In theory we shouldn't need to do anything here, since msync will call
3231 * writepage() on each dirty page and we write them out synchronously. So, the
3232 * problem is elsewhere... Doesn't happen all the time either. Sigh.
3233 */
3234 rc = fsync_inode_buffers(inode);
3235# if RTLNX_VER_MIN(2,4,10)
3236 if (rc == 0 && datasync)
3237 rc = fsync_inode_data_buffers(inode);
3238# endif
3239
3240# endif /* < 2.5.12 */
3241 return rc;
3242# endif
3243}
3244#endif /* < 2.6.35 */
3245
3246
3247#if RTLNX_VER_MIN(4,5,0)
3248/**
3249 * Copy a datablock from one file to another on the host side.
3250 */
3251static ssize_t vbsf_reg_copy_file_range(struct file *pFileSrc, loff_t offSrc, struct file *pFileDst, loff_t offDst,
3252 size_t cbRange, unsigned int fFlags)
3253{
3254 ssize_t cbRet;
3255 if (g_uSfLastFunction >= SHFL_FN_COPY_FILE_PART) {
3256 struct inode *pInodeSrc = pFileSrc->f_inode;
3257 struct vbsf_inode_info *pInodeInfoSrc = VBSF_GET_INODE_INFO(pInodeSrc);
3258 struct vbsf_super_info *pSuperInfoSrc = VBSF_GET_SUPER_INFO(pInodeSrc->i_sb);
3259 struct vbsf_reg_info *pFileInfoSrc = (struct vbsf_reg_info *)pFileSrc->private_data;
3260 struct inode *pInodeDst = pInodeSrc;
3261 struct vbsf_inode_info *pInodeInfoDst = VBSF_GET_INODE_INFO(pInodeDst);
3262 struct vbsf_super_info *pSuperInfoDst = VBSF_GET_SUPER_INFO(pInodeDst->i_sb);
3263 struct vbsf_reg_info *pFileInfoDst = (struct vbsf_reg_info *)pFileDst->private_data;
3264 VBOXSFCOPYFILEPARTREQ *pReq;
3265
3266 /*
3267 * Some extra validation.
3268 */
3269 AssertPtrReturn(pInodeInfoSrc, -EOPNOTSUPP);
3270 Assert(pInodeInfoSrc->u32Magic == SF_INODE_INFO_MAGIC);
3271 AssertPtrReturn(pInodeInfoDst, -EOPNOTSUPP);
3272 Assert(pInodeInfoDst->u32Magic == SF_INODE_INFO_MAGIC);
3273
3274# if RTLNX_VER_MAX(4,11,0)
3275 if (!S_ISREG(pInodeSrc->i_mode) || !S_ISREG(pInodeDst->i_mode))
3276 return S_ISDIR(pInodeSrc->i_mode) || S_ISDIR(pInodeDst->i_mode) ? -EISDIR : -EINVAL;
3277# endif
3278
3279 /*
3280 * Allocate the request and issue it.
3281 */
3282 pReq = (VBOXSFCOPYFILEPARTREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
3283 if (pReq) {
3284 int vrc = VbglR0SfHostReqCopyFilePart(pSuperInfoSrc->map.root, pFileInfoSrc->Handle.hHost, offSrc,
3285 pSuperInfoDst->map.root, pFileInfoDst->Handle.hHost, offDst,
3286 cbRange, 0 /*fFlags*/, pReq);
3287 if (RT_SUCCESS(vrc))
3288 cbRet = pReq->Parms.cb64ToCopy.u.value64;
3289 else if (vrc == VERR_NOT_IMPLEMENTED)
3290 cbRet = -EOPNOTSUPP;
3291 else
3292 cbRet = -RTErrConvertToErrno(vrc);
3293
3294 VbglR0PhysHeapFree(pReq);
3295 } else
3296 cbRet = -ENOMEM;
3297 } else {
3298 cbRet = -EOPNOTSUPP;
3299 }
3300 SFLOGFLOW(("vbsf_reg_copy_file_range: returns %zd\n", cbRet));
3301 return cbRet;
3302}
3303#endif /* > 4.5 */
3304
3305
3306#ifdef SFLOG_ENABLED
3307/*
3308 * This is just for logging page faults and such.
3309 */
3310
3311/** Pointer to the ops generic_file_mmap returns the first time it's called. */
3312static struct vm_operations_struct const *g_pGenericFileVmOps = NULL;
3313/** Merge of g_LoggingVmOpsTemplate and g_pGenericFileVmOps. */
3314static struct vm_operations_struct g_LoggingVmOps;
3315
3316
3317/* Generic page fault callback: */
3318# if RTLNX_VER_MIN(4,11,0)
3319static vm_fault_t vbsf_vmlog_fault(struct vm_fault *vmf)
3320{
3321 vm_fault_t rc;
3322 SFLOGFLOW(("vbsf_vmlog_fault: vmf=%p flags=%#x addr=%p\n", vmf, vmf->flags, vmf->address));
3323 rc = g_pGenericFileVmOps->fault(vmf);
3324 SFLOGFLOW(("vbsf_vmlog_fault: returns %d\n", rc));
3325 return rc;
3326}
3327# elif RTLNX_VER_MIN(2,6,23)
3328static int vbsf_vmlog_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3329{
3330 int rc;
3331# if RTLNX_VER_MIN(4,10,0)
3332 SFLOGFLOW(("vbsf_vmlog_fault: vma=%p vmf=%p flags=%#x addr=%p\n", vma, vmf, vmf->flags, vmf->address));
3333# else
3334 SFLOGFLOW(("vbsf_vmlog_fault: vma=%p vmf=%p flags=%#x addr=%p\n", vma, vmf, vmf->flags, vmf->virtual_address));
3335# endif
3336 rc = g_pGenericFileVmOps->fault(vma, vmf);
3337 SFLOGFLOW(("vbsf_vmlog_fault: returns %d\n", rc));
3338 return rc;
3339}
3340# endif
3341
3342
3343/* Special/generic page fault handler: */
3344# if RTLNX_VER_MIN(2,6,26)
3345# elif RTLNX_VER_MIN(2,6,1)
3346static struct page *vbsf_vmlog_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
3347{
3348 struct page *page;
3349 SFLOGFLOW(("vbsf_vmlog_nopage: vma=%p address=%p type=%p:{%#x}\n", vma, address, type, type ? *type : 0));
3350 page = g_pGenericFileVmOps->nopage(vma, address, type);
3351 SFLOGFLOW(("vbsf_vmlog_nopage: returns %p\n", page));
3352 return page;
3353}
3354# else
3355static struct page *vbsf_vmlog_nopage(struct vm_area_struct *vma, unsigned long address, int write_access_or_unused)
3356{
3357 struct page *page;
3358 SFLOGFLOW(("vbsf_vmlog_nopage: vma=%p address=%p wau=%d\n", vma, address, write_access_or_unused));
3359 page = g_pGenericFileVmOps->nopage(vma, address, write_access_or_unused);
3360 SFLOGFLOW(("vbsf_vmlog_nopage: returns %p\n", page));
3361 return page;
3362}
3363# endif /* < 2.6.26 */
3364
3365
3366/* Special page fault callback for making something writable: */
3367# if RTLNX_VER_MIN(4,11,0)
3368static vm_fault_t vbsf_vmlog_page_mkwrite(struct vm_fault *vmf)
3369{
3370 vm_fault_t rc;
3371 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: vmf=%p flags=%#x addr=%p\n", vmf, vmf->flags, vmf->address));
3372 rc = g_pGenericFileVmOps->page_mkwrite(vmf);
3373 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: returns %d\n", rc));
3374 return rc;
3375}
3376# elif RTLNX_VER_MIN(2,6,30)
3377static int vbsf_vmlog_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
3378{
3379 int rc;
3380# if RTLNX_VER_MIN(4,10,0)
3381 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: vma=%p vmf=%p flags=%#x addr=%p\n", vma, vmf, vmf->flags, vmf->address));
3382# else
3383 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: vma=%p vmf=%p flags=%#x addr=%p\n", vma, vmf, vmf->flags, vmf->virtual_address));
3384# endif
3385 rc = g_pGenericFileVmOps->page_mkwrite(vma, vmf);
3386 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: returns %d\n", rc));
3387 return rc;
3388}
3389# elif RTLNX_VER_MIN(2,6,18)
3390static int vbsf_vmlog_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3391{
3392 int rc;
3393 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: vma=%p page=%p\n", vma, page));
3394 rc = g_pGenericFileVmOps->page_mkwrite(vma, page);
3395 SFLOGFLOW(("vbsf_vmlog_page_mkwrite: returns %d\n", rc));
3396 return rc;
3397}
3398# endif
3399
3400
3401/* Special page fault callback for mapping pages: */
3402# if RTLNX_VER_MIN(4,10,0)
3403static void vbsf_vmlog_map_pages(struct vm_fault *vmf, pgoff_t start, pgoff_t end)
3404{
3405 SFLOGFLOW(("vbsf_vmlog_map_pages: vmf=%p (flags=%#x addr=%p) start=%p end=%p\n", vmf, vmf->flags, vmf->address, start, end));
3406 g_pGenericFileVmOps->map_pages(vmf, start, end);
3407 SFLOGFLOW(("vbsf_vmlog_map_pages: returns\n"));
3408}
3409# elif RTLNX_VER_MIN(4,8,0)
3410static void vbsf_vmlog_map_pages(struct fault_env *fenv, pgoff_t start, pgoff_t end)
3411{
3412 SFLOGFLOW(("vbsf_vmlog_map_pages: fenv=%p (flags=%#x addr=%p) start=%p end=%p\n", fenv, fenv->flags, fenv->address, start, end));
3413 g_pGenericFileVmOps->map_pages(fenv, start, end);
3414 SFLOGFLOW(("vbsf_vmlog_map_pages: returns\n"));
3415}
3416# elif RTLNX_VER_MIN(3,15,0)
3417static void vbsf_vmlog_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
3418{
3419 SFLOGFLOW(("vbsf_vmlog_map_pages: vma=%p vmf=%p (flags=%#x addr=%p)\n", vma, vmf, vmf->flags, vmf->virtual_address));
3420 g_pGenericFileVmOps->map_pages(vma, vmf);
3421 SFLOGFLOW(("vbsf_vmlog_map_pages: returns\n"));
3422}
3423# endif
3424
3425
3426/** Overload template. */
3427static struct vm_operations_struct const g_LoggingVmOpsTemplate = {
3428# if RTLNX_VER_MIN(2,6,23)
3429 .fault = vbsf_vmlog_fault,
3430# endif
3431# if RTLNX_VER_MAX(2,6,26)
3432 .nopage = vbsf_vmlog_nopage,
3433# endif
3434# if RTLNX_VER_MIN(2,6,18)
3435 .page_mkwrite = vbsf_vmlog_page_mkwrite,
3436# endif
3437# if RTLNX_VER_MIN(3,15,0)
3438 .map_pages = vbsf_vmlog_map_pages,
3439# endif
3440};
3441
3442/** file_operations::mmap wrapper for logging purposes. */
3443extern int vbsf_reg_mmap(struct file *file, struct vm_area_struct *vma)
3444{
3445 int rc;
3446 SFLOGFLOW(("vbsf_reg_mmap: file=%p vma=%p\n", file, vma));
3447 rc = generic_file_mmap(file, vma);
3448 if (rc == 0) {
3449 /* Merge the ops and template the first time thru (there's a race here). */
3450 if (g_pGenericFileVmOps == NULL) {
3451 uintptr_t const *puSrc1 = (uintptr_t *)vma->vm_ops;
3452 uintptr_t const *puSrc2 = (uintptr_t *)&g_LoggingVmOpsTemplate;
3453 uintptr_t volatile *puDst = (uintptr_t *)&g_LoggingVmOps;
3454 size_t cbLeft = sizeof(g_LoggingVmOps) / sizeof(*puDst);
3455 while (cbLeft-- > 0) {
3456 *puDst = *puSrc2 && *puSrc1 ? *puSrc2 : *puSrc1;
3457 puSrc1++;
3458 puSrc2++;
3459 puDst++;
3460 }
3461 g_pGenericFileVmOps = vma->vm_ops;
3462 vma->vm_ops = &g_LoggingVmOps;
3463 } else if (g_pGenericFileVmOps == vma->vm_ops)
3464 vma->vm_ops = &g_LoggingVmOps;
3465 else
3466 SFLOGFLOW(("vbsf_reg_mmap: Warning: vm_ops=%p, expected %p!\n", vma->vm_ops, g_pGenericFileVmOps));
3467 }
3468 SFLOGFLOW(("vbsf_reg_mmap: returns %d\n", rc));
3469 return rc;
3470}
3471
3472#endif /* SFLOG_ENABLED */
3473
3474
3475/**
3476 * File operations for regular files.
3477 *
3478 * Note on splice_read/splice_write/sendfile:
3479 * - Splice was introduced in 2.6.17. The generic_file_splice_read/write
3480 * methods go thru the page cache, which is undesirable and is why we
3481 * need to cook our own versions of the code as long as we cannot track
3482 * host-side writes and correctly invalidate the guest page-cache.
3483 * - Sendfile reimplemented using splice in 2.6.23.
3484 * - The default_file_splice_read/write no-page-cache fallback functions,
3485 * were introduced in 2.6.31. The write one work in page units.
3486 * - Since linux 3.16 there is iter_file_splice_write that uses iter_write.
3487 * - Since linux 4.9 the generic_file_splice_read function started using
3488 * read_iter.
3489 */
3490struct file_operations vbsf_reg_fops = {
3491 .open = vbsf_reg_open,
3492 .read = vbsf_reg_read,
3493 .write = vbsf_reg_write,
3494#if RTLNX_VER_MIN(3,16,0)
3495 .read_iter = vbsf_reg_read_iter,
3496 .write_iter = vbsf_reg_write_iter,
3497#elif RTLNX_VER_MIN(2,6,19)
3498 .aio_read = vbsf_reg_aio_read,
3499 .aio_write = vbsf_reg_aio_write,
3500#endif
3501 .release = vbsf_reg_release,
3502#ifdef SFLOG_ENABLED
3503 .mmap = vbsf_reg_mmap,
3504#else
3505 .mmap = generic_file_mmap,
3506#endif
3507#if RTLNX_VER_RANGE(2,6,17, 2,6,31)
3508 .splice_read = vbsf_splice_read,
3509#endif
3510#if RTLNX_VER_MIN(3,16,0)
3511 .splice_write = iter_file_splice_write,
3512#elif RTLNX_VER_MIN(2,6,17)
3513 .splice_write = vbsf_splice_write,
3514#endif
3515#if RTLNX_VER_RANGE(2,5,30, 2,6,23)
3516 .sendfile = vbsf_reg_sendfile,
3517#endif
3518 .llseek = vbsf_reg_llseek,
3519 .fsync = vbsf_reg_fsync,
3520#if RTLNX_VER_MIN(4,5,0)
3521 .copy_file_range = vbsf_reg_copy_file_range,
3522#endif
3523};
3524
3525
3526/**
3527 * Inodes operations for regular files.
3528 */
3529struct inode_operations vbsf_reg_iops = {
3530#if RTLNX_VER_MIN(2,5,18)
3531 .getattr = vbsf_inode_getattr,
3532#else
3533 .revalidate = vbsf_inode_revalidate,
3534#endif
3535 .setattr = vbsf_inode_setattr,
3536};
3537
3538
3539
3540/*********************************************************************************************************************************
3541* Address Space Operations on Regular Files (for mmap, sendfile, direct I/O) *
3542*********************************************************************************************************************************/
3543
3544/**
3545 * Used to read the content of a page into the page cache.
3546 *
3547 * Needed for mmap and reads+writes when the file is mmapped in a
3548 * shared+writeable fashion.
3549 */
3550static int vbsf_readpage(struct file *file, struct page *page)
3551{
3552 struct inode *inode = VBSF_GET_F_DENTRY(file)->d_inode;
3553 int err;
3554
3555 SFLOGFLOW(("vbsf_readpage: inode=%p file=%p page=%p off=%#llx\n", inode, file, page, (uint64_t)page->index << PAGE_SHIFT));
3556 Assert(PageLocked(page));
3557
3558 if (PageUptodate(page)) {
3559 unlock_page(page);
3560 return 0;
3561 }
3562
3563 if (!is_bad_inode(inode)) {
3564 VBOXSFREADPGLSTREQ *pReq = (VBOXSFREADPGLSTREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
3565 if (pReq) {
3566 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
3567 struct vbsf_reg_info *sf_r = file->private_data;
3568 uint32_t cbRead;
3569 int vrc;
3570
3571 pReq->PgLst.offFirstPage = 0;
3572 pReq->PgLst.aPages[0] = page_to_phys(page);
3573 vrc = VbglR0SfHostReqReadPgLst(pSuperInfo->map.root,
3574 pReq,
3575 sf_r->Handle.hHost,
3576 (uint64_t)page->index << PAGE_SHIFT,
3577 PAGE_SIZE,
3578 1 /*cPages*/);
3579
3580 cbRead = pReq->Parms.cb32Read.u.value32;
3581 AssertStmt(cbRead <= PAGE_SIZE, cbRead = PAGE_SIZE);
3582 VbglR0PhysHeapFree(pReq);
3583
3584 if (RT_SUCCESS(vrc)) {
3585 if (cbRead == PAGE_SIZE) {
3586 /* likely */
3587 } else {
3588 uint8_t *pbMapped = (uint8_t *)kmap(page);
3589 RT_BZERO(&pbMapped[cbRead], PAGE_SIZE - cbRead);
3590 kunmap(page);
3591 /** @todo truncate the inode file size? */
3592 }
3593
3594 flush_dcache_page(page);
3595 SetPageUptodate(page);
3596 unlock_page(page);
3597 return 0;
3598 }
3599 err = -RTErrConvertToErrno(vrc);
3600 } else
3601 err = -ENOMEM;
3602 } else
3603 err = -EIO;
3604 SetPageError(page);
3605 unlock_page(page);
3606 return err;
3607}
3608
3609
3610/**
3611 * Used to write out the content of a dirty page cache page to the host file.
3612 *
3613 * Needed for mmap and writes when the file is mmapped in a shared+writeable
3614 * fashion.
3615 */
3616#if RTLNX_VER_MIN(2,5,52)
3617static int vbsf_writepage(struct page *page, struct writeback_control *wbc)
3618#else
3619static int vbsf_writepage(struct page *page)
3620#endif
3621{
3622 struct address_space *mapping = page->mapping;
3623 struct inode *inode = mapping->host;
3624 struct vbsf_inode_info *sf_i = VBSF_GET_INODE_INFO(inode);
3625 struct vbsf_handle *pHandle = vbsf_handle_find(sf_i, VBSF_HANDLE_F_WRITE, VBSF_HANDLE_F_APPEND);
3626 int err;
3627
3628 SFLOGFLOW(("vbsf_writepage: inode=%p page=%p off=%#llx pHandle=%p (%#llx)\n",
3629 inode, page, (uint64_t)page->index << PAGE_SHIFT, pHandle, pHandle ? pHandle->hHost : 0));
3630
3631 if (pHandle) {
3632 struct vbsf_super_info *pSuperInfo = VBSF_GET_SUPER_INFO(inode->i_sb);
3633 VBOXSFWRITEPGLSTREQ *pReq = (VBOXSFWRITEPGLSTREQ *)VbglR0PhysHeapAlloc(sizeof(*pReq));
3634 if (pReq) {
3635 uint64_t const cbFile = i_size_read(inode);
3636 uint64_t const offInFile = (uint64_t)page->index << PAGE_SHIFT;
3637 uint32_t const cbToWrite = page->index != (cbFile >> PAGE_SHIFT) ? PAGE_SIZE
3638 : (uint32_t)cbFile & (uint32_t)PAGE_OFFSET_MASK;
3639 int vrc;
3640
3641 pReq->PgLst.offFirstPage = 0;
3642 pReq->PgLst.aPages[0] = page_to_phys(page);
3643 vrc = VbglR0SfHostReqWritePgLst(pSuperInfo->map.root,
3644 pReq,
3645 pHandle->hHost,
3646 offInFile,
3647 cbToWrite,
3648 1 /*cPages*/);
3649 sf_i->ModificationTimeAtOurLastWrite = sf_i->ModificationTime;
3650 AssertMsgStmt(pReq->Parms.cb32Write.u.value32 == cbToWrite || RT_FAILURE(vrc), /* lazy bird */
3651 ("%#x vs %#x\n", pReq->Parms.cb32Write, cbToWrite),
3652 vrc = VERR_WRITE_ERROR);
3653 VbglR0PhysHeapFree(pReq);
3654
3655 if (RT_SUCCESS(vrc)) {
3656 /* Update the inode if we've extended the file. */
3657 /** @todo is this necessary given the cbToWrite calc above? */
3658 uint64_t const offEndOfWrite = offInFile + cbToWrite;
3659 if ( offEndOfWrite > cbFile
3660 && offEndOfWrite > i_size_read(inode))
3661 i_size_write(inode, offEndOfWrite);
3662
3663 /* Update and unlock the page. */
3664 if (PageError(page))
3665 ClearPageError(page);
3666 SetPageUptodate(page);
3667 unlock_page(page);
3668
3669 vbsf_handle_release(pHandle, pSuperInfo, "vbsf_writepage");
3670 return 0;
3671 }
3672
3673 /*
3674 * We failed.
3675 */
3676 err = -EIO;
3677 } else
3678 err = -ENOMEM;
3679 vbsf_handle_release(pHandle, pSuperInfo, "vbsf_writepage");
3680 } else {
3681 /** @todo we could re-open the file here and deal with this... */
3682 static uint64_t volatile s_cCalls = 0;
3683 if (s_cCalls++ < 16)
3684 printk("vbsf_writepage: no writable handle for %s..\n", sf_i->path->String.ach);
3685 err = -EIO;
3686 }
3687 SetPageError(page);
3688 unlock_page(page);
3689 return err;
3690}
3691
3692
3693#if RTLNX_VER_MIN(2,6,24)
3694/**
3695 * Called when writing thru the page cache (which we shouldn't be doing).
3696 */
3697int vbsf_write_begin(struct file *file, struct address_space *mapping, loff_t pos,
3698 unsigned len, unsigned flags, struct page **pagep, void **fsdata)
3699{
3700 /** @todo r=bird: We shouldn't ever get here, should we? Because we don't use
3701 * the page cache for any writes AFAIK. We could just as well use
3702 * simple_write_begin & simple_write_end here if we think we really
3703 * need to have non-NULL function pointers in the table... */
3704 static uint64_t volatile s_cCalls = 0;
3705 if (s_cCalls++ < 16) {
3706 printk("vboxsf: Unexpected call to vbsf_write_begin(pos=%#llx len=%#x flags=%#x)! Please report.\n",
3707 (unsigned long long)pos, len, flags);
3708 RTLogBackdoorPrintf("vboxsf: Unexpected call to vbsf_write_begin(pos=%#llx len=%#x flags=%#x)! Please report.\n",
3709 (unsigned long long)pos, len, flags);
3710# ifdef WARN_ON
3711 WARN_ON(1);
3712# endif
3713 }
3714 return simple_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
3715}
3716#endif /* KERNEL_VERSION >= 2.6.24 */
3717
3718
3719#if RTLNX_VER_MIN(2,4,10)
3720
3721# ifdef VBOX_UEK
3722# undef iov_iter /* HACK ALERT! Don't put anything needing vbsf_iov_iter after this fun! */
3723# endif
3724
3725/**
3726 * This is needed to make open accept O_DIRECT as well as dealing with direct
3727 * I/O requests if we don't intercept them earlier.
3728 */
3729# if RTLNX_VER_MIN(4, 7, 0) \
3730 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_RANGE(4,4,73, 4,4,74) /** @todo Figure out when exactly. */) \
3731 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_RANGE(4,4,75, 4,4,90) /** @todo Figure out when exactly. */) \
3732 || (defined(CONFIG_SUSE_KERNEL) && RTLNX_VER_RANGE(4,4,92, 4,5,0) /** @todo Figure out when exactly. */)
3733static ssize_t vbsf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3734# elif RTLNX_VER_MIN(4, 1, 0)
3735static ssize_t vbsf_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
3736# elif RTLNX_VER_MIN(3, 16, 0) || defined(VBOX_UEK)
3737static ssize_t vbsf_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t offset)
3738# elif RTLNX_VER_MIN(2, 6, 6)
3739static ssize_t vbsf_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t offset, unsigned long nr_segs)
3740# elif RTLNX_VER_MIN(2, 5, 55)
3741static int vbsf_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t offset, unsigned long nr_segs)
3742# elif RTLNX_VER_MIN(2, 5, 41)
3743static int vbsf_direct_IO(int rw, struct file *file, const struct iovec *iov, loff_t offset, unsigned long nr_segs)
3744# elif RTLNX_VER_MIN(2, 5, 35)
3745static int vbsf_direct_IO(int rw, struct inode *inode, const struct iovec *iov, loff_t offset, unsigned long nr_segs)
3746# elif RTLNX_VER_MIN(2, 5, 26)
3747static int vbsf_direct_IO(int rw, struct inode *inode, char *buf, loff_t offset, size_t count)
3748# elif LINUX_VERSION_CODE == KERNEL_VERSION(2, 4, 21) && defined(I_NEW) /* RHEL3 Frankenkernel. */
3749static int vbsf_direct_IO(int rw, struct file *file, struct kiobuf *buf, unsigned long whatever1, int whatever2)
3750# else
3751static int vbsf_direct_IO(int rw, struct inode *inode, struct kiobuf *buf, unsigned long whatever1, int whatever2)
3752# endif
3753{
3754 TRACE();
3755 return -EINVAL;
3756}
3757
3758#endif
3759
3760/**
3761 * Address space (for the page cache) operations for regular files.
3762 *
3763 * @todo the FsPerf touch/flush (mmap) test fails on 4.4.0 (ubuntu 16.04 lts).
3764 */
3765struct address_space_operations vbsf_reg_aops = {
3766 .readpage = vbsf_readpage,
3767 .writepage = vbsf_writepage,
3768 /** @todo Need .writepages if we want msync performance... */
3769#if RTLNX_VER_MIN(2,5,12)
3770 .set_page_dirty = __set_page_dirty_buffers,
3771#endif
3772#if RTLNX_VER_MIN(2,6,24)
3773 .write_begin = vbsf_write_begin,
3774 .write_end = simple_write_end,
3775#elif RTLNX_VER_MIN(2,5,45)
3776 .prepare_write = simple_prepare_write,
3777 .commit_write = simple_commit_write,
3778#endif
3779#if RTLNX_VER_MIN(2,4,10)
3780 .direct_IO = vbsf_direct_IO,
3781#endif
3782};
3783
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