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 /****************************************************************************
33  * Included Files
34  ****************************************************************************/
35 
36 #include "assert.h"
37 #include "errno.h"
38 #include "fcntl.h"
39 #include "fs/file.h"
40 #include "sys/types.h"
41 #include "sched.h"
42 #include "unistd.h"
43 #include "vfs_config.h"
44 
45 /****************************************************************************
46  * Name: file_fallocate
47  ****************************************************************************/
48 
file_fallocate(struct file *filep, int mode, off_t offset, off_t len)49 static ssize_t file_fallocate(struct file *filep, int mode, off_t offset, off_t len)
50 {
51     int ret;
52 
53     if (len <= 0) {
54         ret = EINVAL;
55         goto errout;
56     }
57 
58     /* Was this file opened for write access? */
59 
60     if (((unsigned int)(filep->f_oflags) & O_ACCMODE) == O_RDONLY) {
61         ret = -EACCES;
62         goto errout;
63     }
64 
65     if (!filep->ops || !filep->ops->fallocate) {
66         ret = -EBADF;
67         goto errout;
68     }
69 
70     /* Yes, then let the driver perform the fallocate */
71 
72     ret = filep->ops->fallocate(filep, mode, offset, len);
73     if (ret < 0) {
74         goto errout;
75     }
76 
77     return ret;
78 
79 errout:
80     set_errno(-ret);
81     return VFS_ERROR;
82 }
83 
84 /***************************************************************************
85  * Name: fallocate
86  *
87  * Description:
88  * The fallocate() function prepares or allocates a contiguous data area to the file.
89  * Thus the write file is guaranteed be contiguous and no allocation delay until the
90  * size reaches that size at least unless any other changes to the volume is performed.
91  *
92  * Parameters:
93  * fp Pointer to the open file object.
94  * mode Operation mode. only support FALLOC_FL_KEEP_SIZE.
95  * offset offset of the file to allocated.
96  * len The size to allocate for the file.
97  *
98  * Returned Value:
99  *  On success, allocate contiguous data area to the file . On error, -1 is returned, and errno is set appro-
100  *  priately:
101  *
102  *
103  ********************************************************************************************/
104 
fallocate(int fd, int mode, off_t offset, off_t len)105 int fallocate(int fd, int mode, off_t offset, off_t len)
106 {
107 #if CONFIG_NFILE_DESCRIPTORS > 0
108     struct file *filep = NULL;
109 #endif
110 
111 /* Did we get a valid file descriptor? */
112 
113 #if CONFIG_NFILE_DESCRIPTORS > 0
114     if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
115 #endif
116     {
117         set_errno(EBADF);
118         return VFS_ERROR;
119     }
120 
121 #if CONFIG_NFILE_DESCRIPTORS > 0
122 
123     /* The descriptor is in the right range to be a file descriptor... write to the file. */
124 
125     int ret = fs_getfilep(fd, &filep);
126     if (ret < 0) {
127         /* The errno value has already been set */
128         return VFS_ERROR;
129     }
130 
131     if (filep->f_oflags & O_DIRECTORY) {
132         set_errno(EBADF);
133         return VFS_ERROR;
134     }
135 
136     /* Perform the fallocate operation using the file descriptor as an index */
137     return file_fallocate(filep, mode, offset, len);
138 #endif
139 }
140