更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
vfs_readv.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "sys/types.h"
33#include "sys/uio.h"
34#include "unistd.h"
35#include "string.h"
36#include "stdlib.h"
37#include "fs/file.h"
38#include "user_copy.h"
39#include "stdio.h"
40#include "limits.h"
41
42static char *pread_buf_and_check(int fd, const struct iovec *iov, int iovcnt, ssize_t *totalbytesread, off_t *offset)
43{
44 char *buf = NULL;
45 size_t buflen = 0;
46 int i;
47
48 if ((iov == NULL) || (iovcnt > IOV_MAX)) {
49 *totalbytesread = VFS_ERROR;
50 return NULL;
51 }
52
53 for (i = 0; i < iovcnt; ++i) {
54 if (SSIZE_MAX - buflen < iov[i].iov_len) {
55 set_errno(EINVAL);
56 return NULL;
57 }
58 buflen += iov[i].iov_len;
59 }
60
61 if (buflen == 0) {
62 *totalbytesread = 0;
63 return NULL;
64 }
65
66#ifdef LOSCFG_KERNEL_VM
67 buf = (char *)LOS_VMalloc(buflen * sizeof(char));
68#else
69 buf = (char *)malloc(buflen * sizeof(char));
70#endif
71 if (buf == NULL) {
72 set_errno(ENOMEM);
73 *totalbytesread = VFS_ERROR;
74 return buf;
75 }
76
77 *totalbytesread = (offset == NULL) ? read(fd, buf, buflen)
78 : pread(fd, buf, buflen, *offset);
79 if ((*totalbytesread == VFS_ERROR) || (*totalbytesread == 0)) {
80#ifdef LOSCFG_KERNEL_VM
81 LOS_VFree(buf);
82#else
83 free(buf);
84#endif
85 return NULL;
86 }
87
88 return buf;
89}
90///供系统调用
91ssize_t vfs_readv(int fd, const struct iovec *iov, int iovcnt, off_t *offset)
92{
93 int i;
94 int ret;
95 char *buf = NULL;
96 char *curbuf = NULL;
97 ssize_t bytestoread;
98 ssize_t totalbytesread = 0;
99 ssize_t bytesleft;
100
101 buf = pread_buf_and_check(fd, iov, iovcnt, &totalbytesread, offset);
102 if (buf == NULL) {
103 return totalbytesread;
104 }
105
106 curbuf = buf;
107 bytesleft = totalbytesread;
108 for (i = 0; i < iovcnt; ++i) {
109 bytestoread = iov[i].iov_len;
110 if (bytestoread == 0) {
111 continue;
112 }
113
114 if (bytesleft <= bytestoread) {
115 ret = LOS_CopyFromKernel(iov[i].iov_base, bytesleft, curbuf, bytesleft);
116 bytesleft = ret;
117 goto out;
118 }
119
120 ret = LOS_CopyFromKernel(iov[i].iov_base, bytestoread, curbuf, bytestoread);
121 if (ret != 0) {
122 bytesleft = bytesleft - (bytestoread - ret);
123 goto out;
124 }
125 bytesleft -= bytestoread;
126 curbuf += bytestoread;
127 }
128
129out:
130#ifdef LOSCFG_KERNEL_VM
131 LOS_VFree(buf);
132#else
133 free(buf);
134#endif
135 if ((i == 0) && (ret == iov[i].iov_len)) {
136 /* failed in the first iovec copy, and 0 bytes copied */
137 set_errno(EFAULT);
138 return VFS_ERROR;
139 }
140
141 return totalbytesread - bytesleft;
142}
143
144ssize_t readv(int fd, const struct iovec *iov, int iovcnt)
145{
146 return vfs_readv(fd, iov, iovcnt, NULL);
147}
INT64 ssize_t
Definition: los_typedef.h:79
VOID LOS_VFree(const VOID *addr)
VOID * LOS_VMalloc(size_t size)
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
INT32 LOS_CopyFromKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
将内核数据拷贝到用户空间
Definition: user_copy.c:88
static char * pread_buf_and_check(int fd, const struct iovec *iov, int iovcnt, ssize_t *totalbytesread, off_t *offset)
Definition: vfs_readv.c:42
ssize_t readv(int fd, const struct iovec *iov, int iovcnt)
Definition: vfs_readv.c:144
ssize_t vfs_readv(int fd, const struct iovec *iov, int iovcnt, off_t *offset)
供系统调用
Definition: vfs_readv.c:91