10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "sys/types.h"
330d163575Sopenharmony_ci#include "sys/uio.h"
340d163575Sopenharmony_ci#include "unistd.h"
350d163575Sopenharmony_ci#include "string.h"
360d163575Sopenharmony_ci#include "stdlib.h"
370d163575Sopenharmony_ci#include "fs/file.h"
380d163575Sopenharmony_ci#include "user_copy.h"
390d163575Sopenharmony_ci#include "stdio.h"
400d163575Sopenharmony_ci#include "limits.h"
410d163575Sopenharmony_ci
420d163575Sopenharmony_cistatic char *pread_buf_and_check(int fd, const struct iovec *iov, int iovcnt, ssize_t *totalbytesread, off_t *offset)
430d163575Sopenharmony_ci{
440d163575Sopenharmony_ci    char *buf = NULL;
450d163575Sopenharmony_ci    size_t buflen = 0;
460d163575Sopenharmony_ci    int i;
470d163575Sopenharmony_ci
480d163575Sopenharmony_ci    if ((iov == NULL) || (iovcnt > IOV_MAX)) {
490d163575Sopenharmony_ci        *totalbytesread = VFS_ERROR;
500d163575Sopenharmony_ci        return NULL;
510d163575Sopenharmony_ci    }
520d163575Sopenharmony_ci
530d163575Sopenharmony_ci    for (i = 0; i < iovcnt; ++i) {
540d163575Sopenharmony_ci        if (SSIZE_MAX - buflen < iov[i].iov_len) {
550d163575Sopenharmony_ci            set_errno(EINVAL);
560d163575Sopenharmony_ci            return NULL;
570d163575Sopenharmony_ci        }
580d163575Sopenharmony_ci        buflen += iov[i].iov_len;
590d163575Sopenharmony_ci    }
600d163575Sopenharmony_ci
610d163575Sopenharmony_ci    if (buflen == 0) {
620d163575Sopenharmony_ci        *totalbytesread = 0;
630d163575Sopenharmony_ci        return NULL;
640d163575Sopenharmony_ci    }
650d163575Sopenharmony_ci
660d163575Sopenharmony_ci#ifdef LOSCFG_KERNEL_VM
670d163575Sopenharmony_ci    buf = (char *)LOS_VMalloc(buflen * sizeof(char));
680d163575Sopenharmony_ci#else
690d163575Sopenharmony_ci    buf = (char *)malloc(buflen * sizeof(char));
700d163575Sopenharmony_ci#endif
710d163575Sopenharmony_ci    if (buf == NULL) {
720d163575Sopenharmony_ci        set_errno(ENOMEM);
730d163575Sopenharmony_ci        *totalbytesread = VFS_ERROR;
740d163575Sopenharmony_ci        return buf;
750d163575Sopenharmony_ci    }
760d163575Sopenharmony_ci
770d163575Sopenharmony_ci    *totalbytesread = (offset == NULL) ? read(fd, buf, buflen)
780d163575Sopenharmony_ci                                       : pread(fd, buf, buflen, *offset);
790d163575Sopenharmony_ci    if ((*totalbytesread == VFS_ERROR) || (*totalbytesread == 0)) {
800d163575Sopenharmony_ci#ifdef LOSCFG_KERNEL_VM
810d163575Sopenharmony_ci        LOS_VFree(buf);
820d163575Sopenharmony_ci#else
830d163575Sopenharmony_ci        free(buf);
840d163575Sopenharmony_ci#endif
850d163575Sopenharmony_ci        return NULL;
860d163575Sopenharmony_ci    }
870d163575Sopenharmony_ci
880d163575Sopenharmony_ci    return buf;
890d163575Sopenharmony_ci}
900d163575Sopenharmony_ci
910d163575Sopenharmony_cissize_t vfs_readv(int fd, const struct iovec *iov, int iovcnt, off_t *offset)
920d163575Sopenharmony_ci{
930d163575Sopenharmony_ci    int i;
940d163575Sopenharmony_ci    int ret;
950d163575Sopenharmony_ci    char *buf = NULL;
960d163575Sopenharmony_ci    char *curbuf = NULL;
970d163575Sopenharmony_ci    ssize_t bytestoread;
980d163575Sopenharmony_ci    ssize_t totalbytesread = 0;
990d163575Sopenharmony_ci    ssize_t bytesleft;
1000d163575Sopenharmony_ci
1010d163575Sopenharmony_ci    buf = pread_buf_and_check(fd, iov, iovcnt, &totalbytesread, offset);
1020d163575Sopenharmony_ci    if (buf == NULL) {
1030d163575Sopenharmony_ci        return totalbytesread;
1040d163575Sopenharmony_ci    }
1050d163575Sopenharmony_ci
1060d163575Sopenharmony_ci    curbuf = buf;
1070d163575Sopenharmony_ci    bytesleft = totalbytesread;
1080d163575Sopenharmony_ci    for (i = 0; i < iovcnt; ++i) {
1090d163575Sopenharmony_ci        bytestoread = iov[i].iov_len;
1100d163575Sopenharmony_ci        if (bytestoread == 0) {
1110d163575Sopenharmony_ci            continue;
1120d163575Sopenharmony_ci        }
1130d163575Sopenharmony_ci
1140d163575Sopenharmony_ci        if (bytesleft <= bytestoread) {
1150d163575Sopenharmony_ci            ret = LOS_CopyFromKernel(iov[i].iov_base, bytesleft, curbuf, bytesleft);
1160d163575Sopenharmony_ci            bytesleft = ret;
1170d163575Sopenharmony_ci            goto out;
1180d163575Sopenharmony_ci        }
1190d163575Sopenharmony_ci
1200d163575Sopenharmony_ci        ret = LOS_CopyFromKernel(iov[i].iov_base, bytestoread, curbuf, bytestoread);
1210d163575Sopenharmony_ci        if (ret != 0) {
1220d163575Sopenharmony_ci            bytesleft = bytesleft - (bytestoread - ret);
1230d163575Sopenharmony_ci            goto out;
1240d163575Sopenharmony_ci        }
1250d163575Sopenharmony_ci        bytesleft -= bytestoread;
1260d163575Sopenharmony_ci        curbuf += bytestoread;
1270d163575Sopenharmony_ci    }
1280d163575Sopenharmony_ci
1290d163575Sopenharmony_ciout:
1300d163575Sopenharmony_ci#ifdef LOSCFG_KERNEL_VM
1310d163575Sopenharmony_ci    LOS_VFree(buf);
1320d163575Sopenharmony_ci#else
1330d163575Sopenharmony_ci    free(buf);
1340d163575Sopenharmony_ci#endif
1350d163575Sopenharmony_ci    if ((i == 0) && (ret == iov[i].iov_len)) {
1360d163575Sopenharmony_ci        /* failed in the first iovec copy, and 0 bytes copied */
1370d163575Sopenharmony_ci        set_errno(EFAULT);
1380d163575Sopenharmony_ci        return VFS_ERROR;
1390d163575Sopenharmony_ci    }
1400d163575Sopenharmony_ci
1410d163575Sopenharmony_ci    return totalbytesread - bytesleft;
1420d163575Sopenharmony_ci}
1430d163575Sopenharmony_ci
1440d163575Sopenharmony_cissize_t readv(int fd, const struct iovec *iov, int iovcnt)
1450d163575Sopenharmony_ci{
1460d163575Sopenharmony_ci    return vfs_readv(fd, iov, iovcnt, NULL);
1470d163575Sopenharmony_ci}
148