VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/fileio-sg-at-posix.cpp@ 77632

Last change on this file since 77632 was 77632, checked in by vboxsync, 6 years ago

IPRT: Adding RTFileSgRead and RTFileSgWrite (for FsPerf). bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: fileio-sg-at-posix.cpp 77632 2019-03-10 13:33:52Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, RTFileSgReadAt & RTFileSgWriteAt, posixy.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31/*
32 * Determin whether we've got preadv and pwritev.
33 */
34#include <iprt/cdefs.h>
35#ifdef RT_OS_LINUX
36/* Linux has these since glibc 2.10 and Linux 2.6.30: */
37# include <features.h>
38# ifdef __GLIBC_PREREQ
39# if __GLIBC_PREREQ(2,10)
40# define HAVE_PREADV_AND_PWRITEV 1
41#else
42# endif
43# endif
44
45#elif defined(RT_OS_FREEBSD)
46/* FreeBSD has these since 6.0: */
47# include <osreldate.h>
48# ifdef __FreeBSD_version
49# if __FreeBSD_version >= 600000
50# define HAVE_PREADV_AND_PWRITEV 1
51# endif
52# endif
53
54#endif
55
56#ifndef HAVE_PREADV_AND_PWRITEV
57
58# include "../../generic/fileio-sg-generic.cpp"
59
60#else /* HAVE_PREADV_AND_PWRITEV - rest of the file */
61
62# include <errno.h>
63# include <sys/types.h>
64# include <sys/uio.h>
65# include <unistd.h>
66
67# include "internal/iprt.h"
68# include <iprt/file.h>
69
70# include <iprt/assert.h>
71# include <iprt/err.h>
72# include <iprt/log.h>
73
74# ifndef UIO_MAXIOV
75# error "UIO_MAXIOV is undefined"
76# endif
77
78
79/* These assumptions simplifies things a lot here. */
80AssertCompileMembersSameSizeAndOffset(struct iovec, iov_base, RTSGSEG, pvSeg);
81AssertCompileMembersSameSizeAndOffset(struct iovec, iov_len, RTSGSEG, cbSeg);
82
83
84RTDECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
85{
86 /*
87 * Make sure we set pcbRead.
88 */
89 if (pcbRead)
90 *pcbRead = 0;
91
92 /*
93 * Special case: Zero read == seek.
94 */
95 if (cbToRead == 0)
96 return RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
97
98 /*
99 * We can use the segment array directly if we're at the start of the
100 * current S/G segment and cbToRead matches the remainder exactly.
101 */
102 size_t cbTotalRead = 0;
103
104 size_t const cbSgBufLeft = RTSgBufCalcLengthLeft(pSgBuf);
105 AssertMsgReturn(cbSgBufLeft >= cbToRead, ("%#zx vs %#zx\n", cbSgBufLeft, cbToRead), VERR_INVALID_PARAMETER);
106
107 if (cbToRead == cbSgBufLeft)
108 while (RTSgBufIsAtStartOfSegment(pSgBuf))
109 {
110 size_t const cSegsLeft = pSgBuf->cSegs - pSgBuf->idxSeg;
111 ssize_t cbThisRead = preadv(RTFileToNative(hFile), (const struct iovec *)&pSgBuf->paSegs[pSgBuf->idxSeg],
112 RT_MIN(cSegsLeft, UIO_MAXIOV), off);
113 if (cbThisRead >= 0)
114 {
115 AssertStmt((size_t)cbThisRead <= cbToRead, cbThisRead = cbToRead);
116
117 RTSgBufAdvance(pSgBuf, cbThisRead);
118 cbTotalRead += cbThisRead;
119 cbToRead -= cbThisRead;
120 if (cbToRead == 0)
121 {
122 if (pcbRead)
123 *pcbRead = cbTotalRead;
124 return VINF_SUCCESS;
125 }
126
127 if ( pcbRead
128 && ( cSegsLeft <= UIO_MAXIOV
129 || cbThisRead == 0 /* typically EOF */ ))
130 {
131 *pcbRead = cbTotalRead;
132 return VINF_SUCCESS;
133 }
134 if (cbThisRead == 0)
135 return VERR_EOF;
136
137 off += cbThisRead;
138 }
139 else if (cbTotalRead > 0 && pcbRead)
140 {
141 *pcbRead = cbTotalRead;
142 return VINF_SUCCESS;
143 }
144 else
145 return RTErrConvertFromErrno(errno);
146 }
147
148 /*
149 * Unaligned start or not reading the whole buffer. For reasons of
150 * simplicity, we work the input segment by segment like the generic code.
151 */
152 int rc = VINF_SUCCESS;
153 while (cbToRead > 0)
154 {
155 size_t cbSeg;
156 void *pvSeg = RTSgBufGetCurrentSegment(pSgBuf, cbToRead, &cbSeg);
157 size_t cbThisRead = cbSeg;
158 rc = RTFileReadAt(hFile, off, pvSeg, cbSeg, pcbRead ? &cbThisRead : NULL);
159 if (RT_SUCCESS(rc))
160 {
161 RTSgBufAdvance(pSgBuf, cbThisRead);
162 cbTotalRead += cbThisRead;
163 }
164 else
165 break;
166 if ((size_t)cbThisRead < cbSeg)
167 {
168 AssertStmt(pcbRead, rc = VERR_INTERNAL_ERROR_2);
169 break;
170 }
171
172 Assert(cbSeg == cbThisRead);
173 cbToRead -= cbSeg;
174 off += cbSeg;
175 }
176 if (pcbRead)
177 *pcbRead = cbTotalRead;
178 return rc;
179}
180
181
182RTDECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
183{
184 /*
185 * Make sure we set pcbWritten.
186 */
187 if (pcbWritten)
188 *pcbWritten = 0;
189
190 /*
191 * Special case: Zero write == seek.
192 */
193 if (cbToWrite == 0)
194 return RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, NULL);
195
196 /*
197 * We can use the segment array directly if we're at the start of the
198 * current S/G segment and cbToWrite matches the remainder exactly.
199 */
200 size_t cbTotalWritten = 0;
201
202 size_t const cbSgBufLeft = RTSgBufCalcLengthLeft(pSgBuf);
203 AssertMsgReturn(cbSgBufLeft >= cbToWrite, ("%#zx vs %#zx\n", cbSgBufLeft, cbToWrite), VERR_INVALID_PARAMETER);
204
205 if (cbToWrite == cbSgBufLeft)
206 while (RTSgBufIsAtStartOfSegment(pSgBuf))
207 {
208 size_t const cSegsLeft = pSgBuf->cSegs - pSgBuf->idxSeg;
209 ssize_t cbThisWritten = pwritev(RTFileToNative(hFile), (const struct iovec *)&pSgBuf->paSegs[pSgBuf->idxSeg],
210 RT_MIN(cSegsLeft, UIO_MAXIOV), off);
211 if (cbThisWritten >= 0)
212 {
213 AssertStmt((size_t)cbThisWritten <= cbToWrite, cbThisWritten = cbToWrite);
214
215 RTSgBufAdvance(pSgBuf, cbThisWritten);
216 cbTotalWritten += cbThisWritten;
217 cbToWrite -= cbThisWritten;
218 if (cbToWrite == 0)
219 {
220 if (pcbWritten)
221 *pcbWritten = cbTotalWritten;
222 return VINF_SUCCESS;
223 }
224
225 if ( pcbWritten
226 && ( cSegsLeft <= UIO_MAXIOV
227 || cbThisWritten == 0 /* non-file, full buffer/whatever */ ))
228 {
229 *pcbWritten = cbTotalWritten;
230 return VINF_SUCCESS;
231 }
232 if (cbThisWritten == 0)
233 return VERR_TRY_AGAIN;
234
235 off += cbThisWritten;
236 }
237 else if (cbTotalWritten > 0 && pcbWritten)
238 {
239 *pcbWritten = cbTotalWritten;
240 return VINF_SUCCESS;
241 }
242 else
243 return RTErrConvertFromErrno(errno);
244 }
245
246 /*
247 * Unaligned start or not writing the whole buffer. For reasons of
248 * simplicity, we work the input segment by segment like the generic code.
249 */
250 int rc = VINF_SUCCESS;
251 while (cbToWrite > 0)
252 {
253 size_t cbSeg;
254 void *pvSeg = RTSgBufGetCurrentSegment(pSgBuf, cbToWrite, &cbSeg);
255 size_t cbThisWritten = cbSeg;
256 rc = RTFileWriteAt(hFile, off, pvSeg, cbSeg, pcbWritten ? &cbThisWritten : NULL);
257 if (RT_SUCCESS(rc))
258 {
259 RTSgBufAdvance(pSgBuf, cbThisWritten);
260 cbTotalWritten += cbThisWritten;
261 }
262 else
263 break;
264 if ((size_t)cbThisWritten < cbSeg)
265 {
266 AssertStmt(pcbWritten, rc = VERR_INTERNAL_ERROR_2);
267 break;
268 }
269
270 Assert(cbSeg == cbThisWritten);
271 cbToWrite -= cbSeg;
272 off += cbSeg;
273 }
274 if (pcbWritten)
275 *pcbWritten = cbTotalWritten;
276 return rc;
277}
278
279#endif /* HAVE_PREADV_AND_PWRITEV */
280
Note: See TracBrowser for help on using the repository browser.

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