• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "limits.h"
40 
iov_trans_to_buf(char * buf,ssize_t totallen,const struct iovec * iov,int iovcnt)41 static int iov_trans_to_buf(char *buf, ssize_t totallen, const struct iovec *iov, int iovcnt)
42 {
43     int i;
44     size_t ret, writepart;
45     size_t bytestowrite;
46     char *writebuf = NULL;
47     char *curbuf = buf;
48 
49     for (i = 0; i < iovcnt; ++i) {
50         writebuf = (char *)iov[i].iov_base;
51         bytestowrite = iov[i].iov_len;
52         if (bytestowrite == 0) {
53             continue;
54         }
55 
56         if (totallen == 0) {
57             break;
58         }
59 
60         bytestowrite = (totallen < bytestowrite) ? totallen : bytestowrite;
61         ret = LOS_CopyToKernel(curbuf, bytestowrite, writebuf, bytestowrite);
62         if (ret != 0) {
63             if (ret == bytestowrite) {
64                 set_errno(EFAULT);
65                 return VFS_ERROR;
66             } else {
67                 writepart = bytestowrite - ret;
68                 curbuf += writepart;
69                 totallen -= writepart;
70                 break;
71             }
72         }
73         curbuf += bytestowrite;
74         totallen -= bytestowrite;
75     }
76 
77     return (int)((intptr_t)curbuf - (intptr_t)buf);
78 }
79 
vfs_writev(int fd,const struct iovec * iov,int iovcnt,off_t * offset)80 ssize_t vfs_writev(int fd, const struct iovec *iov, int iovcnt, off_t *offset)
81 {
82     int i, ret;
83     char *buf = NULL;
84     size_t buflen = 0;
85     size_t bytestowrite;
86     ssize_t totalbyteswritten;
87     size_t totallen;
88 
89     if ((iov == NULL) || (iovcnt > IOV_MAX)) {
90         return VFS_ERROR;
91     }
92 
93     for (i = 0; i < iovcnt; ++i) {
94         if (SSIZE_MAX - buflen < iov[i].iov_len) {
95             set_errno(EINVAL);
96             return VFS_ERROR;
97         }
98         buflen += iov[i].iov_len;
99     }
100 
101     if (buflen == 0) {
102         return 0;
103     }
104 
105     totallen = buflen * sizeof(char);
106 #ifdef LOSCFG_KERNEL_VM
107     buf = (char *)LOS_VMalloc(totallen);
108 #else
109     buf = (char *)malloc(totallen);
110 #endif
111     if (buf == NULL) {
112         return VFS_ERROR;
113     }
114 
115     ret = iov_trans_to_buf(buf, totallen, iov, iovcnt);
116     if (ret <= 0) {
117 #ifdef LOSCFG_KERNEL_VM
118         LOS_VFree(buf);
119 #else
120         free(buf);
121 #endif
122         return VFS_ERROR;
123     }
124 
125     bytestowrite = (ssize_t)ret;
126     totalbyteswritten = (offset == NULL) ? write(fd, buf, bytestowrite)
127                                          : pwrite(fd, buf, bytestowrite, *offset);
128 #ifdef LOSCFG_KERNEL_VM
129     LOS_VFree(buf);
130 #else
131     free(buf);
132 #endif
133     return totalbyteswritten;
134 }
135 
writev(int fd,const struct iovec * iov,int iovcnt)136 ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
137 {
138     return vfs_writev(fd, iov, iovcnt, NULL);
139 }
140