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