• 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                 break;
70             }
71         }
72         curbuf += bytestowrite;
73         totallen -= bytestowrite;
74     }
75 
76     return (int)((intptr_t)curbuf - (intptr_t)buf);
77 }
78 
vfs_writev(int fd,const struct iovec * iov,int iovcnt,off_t * offset)79 ssize_t vfs_writev(int fd, const struct iovec *iov, int iovcnt, off_t *offset)
80 {
81     int i, ret;
82     char *buf = NULL;
83     size_t buflen = 0;
84     size_t bytestowrite;
85     ssize_t totalbyteswritten;
86     size_t totallen;
87 
88     if ((iov == NULL) || (iovcnt > IOV_MAX)) {
89         return VFS_ERROR;
90     }
91 
92     for (i = 0; i < iovcnt; ++i) {
93         if (SSIZE_MAX - buflen < iov[i].iov_len) {
94             set_errno(EINVAL);
95             return VFS_ERROR;
96         }
97         buflen += iov[i].iov_len;
98     }
99 
100     if (buflen == 0) {
101         return 0;
102     }
103 
104     totallen = buflen * sizeof(char);
105 #ifdef LOSCFG_KERNEL_VM
106     buf = (char *)LOS_VMalloc(totallen);
107 #else
108     buf = (char *)malloc(totallen);
109 #endif
110     if (buf == NULL) {
111         return VFS_ERROR;
112     }
113 
114     ret = iov_trans_to_buf(buf, totallen, iov, iovcnt);
115     if (ret <= 0) {
116 #ifdef LOSCFG_KERNEL_VM
117         LOS_VFree(buf);
118 #else
119         free(buf);
120 #endif
121         return VFS_ERROR;
122     }
123 
124     bytestowrite = (ssize_t)ret;
125     totalbyteswritten = (offset == NULL) ? write(fd, buf, bytestowrite)
126                                          : pwrite(fd, buf, bytestowrite, *offset);
127 #ifdef LOSCFG_KERNEL_VM
128     LOS_VFree(buf);
129 #else
130     free(buf);
131 #endif
132     return totalbyteswritten;
133 }
134 
writev(int fd,const struct iovec * iov,int iovcnt)135 ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
136 {
137     return vfs_writev(fd, iov, iovcnt, NULL);
138 }
139