2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * - Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
11 * - Redistributions in binary form must reproduce the above copyright
\r
12 * notice, this list of conditions and the following disclaimer in the
\r
13 * documentation and/or other materials provided with the distribution.
\r
14 * - The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
29 /** @addtogroup lwext4
\r
34 * @brief Ext4 high level operations (files, directories, mount points...).
\r
35 * Client has to include only this file.
\r
41 #include <ext4_config.h>
\r
42 #include <ext4_blockdev.h>
\r
45 /********************************FILE OPEN FLAGS*****************************/
\r
60 #define O_CREAT 0100
\r
68 #define O_TRUNC 01000
\r
72 #define O_APPEND 02000
\r
75 /********************************FILE SEEK FLAGS*****************************/
\r
89 /********************************OS LOCK INFERFACE***************************/
\r
91 /**@brief OS dependent lock interface.*/
\r
95 /**@brief Lock access to mount point*/
\r
98 /**@brief Unlock access to mount point*/
\r
99 void (*unlock)(void);
\r
102 /********************************FILE DESCRIPTOR*****************************/
\r
104 /**@brief File descriptor*/
\r
105 typedef struct ext4_file
\r
108 /**@brief Mount point handle.*/
\r
109 struct ext4_mountpoint *mp;
\r
111 /**@brief File inode id*/
\r
114 /**@brief Open flags.*/
\r
117 /**@brief File size.*/
\r
120 /**@brief File position*/
\r
124 /*****************************DIRECTORY DESCRIPTOR***************************/
\r
125 /**@brief Directory entry types. Copy from ext4_types.h*/
\r
127 EXT4_DIRENTRY_UNKNOWN = 0,
\r
128 EXT4_DIRENTRY_REG_FILE,
\r
130 EXT4_DIRENTRY_CHRDEV,
\r
131 EXT4_DIRENTRY_BLKDEV,
\r
132 EXT4_DIRENTRY_FIFO,
\r
133 EXT4_DIRENTRY_SOCK,
\r
134 EXT4_DIRENTRY_SYMLINK
\r
137 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
\r
141 uint16_t entry_length;
\r
142 uint8_t name_length;
\r
143 uint8_t inode_type;
\r
149 /**@brief File descriptor*/
\r
151 /**@brief Current directory entry.*/
\r
153 /**@brief Next entry offset*/
\r
157 /********************************MOUNT OPERATIONS****************************/
\r
159 /**@brief Register a block device to a name.
\r
160 * @warning Block device has to be filled by
\r
161 * @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created
\r
162 * @ref EXT4_BCACHE_STATIC_INSTANCE.
\r
163 * Block cache may by created automatically when bc parameter is 0.
\r
164 * @param bd block device
\r
165 * @param bd block device cache (0 = automatic cache mode)
\r
166 * @param dev_name register name
\r
167 * @param standard error code*/
\r
168 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
\r
169 const char *dev_name);
\r
171 /**@brief Mount a block device with EXT4 partition to the mount point.
\r
172 * @param dev_name block device name (@ref ext4_device_register)
\r
173 * @param mount_point mount point, for example
\r
176 * - /my_second_partition/
\r
178 * @return standard error code */
\r
179 int ext4_mount(const char *dev_name, char *mount_point);
\r
181 /**@brief Umount operation.
\r
182 * @param mount_point mount name
\r
183 * @return standard error code */
\r
184 int ext4_umount(char *mount_point);
\r
186 /**@brief Some of the filesystem stats.*/
\r
187 struct ext4_mount_stats
\r
189 uint32_t inodes_count;
\r
190 uint32_t free_inodes_count;
\r
191 uint64_t blocks_count;
\r
192 uint64_t free_blocks_count;
\r
194 uint32_t block_size;
\r
195 uint32_t block_group_count;
\r
196 uint32_t blocks_per_group;
\r
197 uint32_t inodes_per_group;
\r
199 char volume_name[16];
\r
202 /**@brief Get file system params.
\r
203 * @param mount_point mount path
\r
204 * @param stats ext fs stats
\r
205 * @return standard error code */
\r
206 int ext4_mount_point_stats(const char *mount_point,
\r
207 struct ext4_mount_stats *stats);
\r
209 /**@brief Setup OS lock routines.
\r
210 * @param mount_point mount path
\r
211 * @param locks - lock and unlock functions
\r
212 * @return standard error code */
\r
213 int ext4_mount_setup_locks(const char *mount_point,
\r
214 const struct ext4_lock *locks);
\r
216 /**@brief Enable/disable write back cache mode.
\r
217 * @warning Default model of cache is write trough. It means that when You do:
\r
221 * < --- data is flushed to physical drive
\r
224 * ext4_cache_write_back(..., 1);
\r
227 * < --- data is NOT flushed to physical drive
\r
228 * ext4_cache_write_back(..., 0);
\r
229 * < --- when write back mode is disabled all
\r
230 * cache data will be flushed
\r
231 * To enable write back mode permanently just call this function
\r
232 * once after ext4_mount (and disable before ext4_umount).
\r
234 * Some of the function use write back cache mode internally.
\r
235 * If you enable write back mode twice you have to disable it twice
\r
236 * to flush all data:
\r
238 * ext4_cache_write_back(..., 1);
\r
239 * ext4_cache_write_back(..., 1);
\r
241 * ext4_cache_write_back(..., 0);
\r
242 * ext4_cache_write_back(..., 0);
\r
244 * Write back mode is useful when you want to create a lot of empty
\r
245 * files/directories.
\r
247 * @param path mount point path
\r
248 * @param on enable/disable
\r
250 * @return standard error code */
\r
251 int ext4_cache_write_back(const char *path, bool on);
\r
253 /********************************FILE OPERATIONS*****************************/
\r
255 /**@brief Remove file by path.
\r
256 * @param path path to file
\r
257 * @return standard error code */
\r
258 int ext4_fremove(const char *path);
\r
260 /**@brief File open function.
\r
261 * @param filename, (has to start from mount point)
\r
262 * /my_partition/my_file
\r
263 * @param flags open file flags
\r
264 * |---------------------------------------------------------------|
\r
265 * | r or rb O_RDONLY |
\r
266 * |---------------------------------------------------------------|
\r
267 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
\r
268 * |---------------------------------------------------------------|
\r
269 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
\r
270 * |---------------------------------------------------------------|
\r
271 * | r+ or rb+ or r+b O_RDWR |
\r
272 * |---------------------------------------------------------------|
\r
273 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
\r
274 * |---------------------------------------------------------------|
\r
275 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
\r
276 * |---------------------------------------------------------------|
\r
278 * @return standard error code*/
\r
279 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
\r
281 /**@brief File close function.
\r
282 * @param f file handle
\r
283 * @return standard error code*/
\r
284 int ext4_fclose(ext4_file *f);
\r
286 /**@brief Read data from file.
\r
287 * @param f file handle
\r
288 * @param buf output buffer
\r
289 * @param size bytes to read
\r
290 * @param rcnt bytes read (may be NULL)
\r
291 * @return standard error code*/
\r
292 int ext4_fread(ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);
\r
294 /**@brief Write data to file.
\r
295 * @param f file handle
\r
296 * @param buf data to write
\r
297 * @param size write length
\r
298 * @param wcnt bytes written (may be NULL)
\r
299 * @return standard error code*/
\r
300 int ext4_fwrite(ext4_file *f, const void *buf, uint32_t size, uint32_t *wcnt);
\r
302 /**@brief File seek operation.
\r
303 * @param f file handle
\r
304 * @param offset offset to seek
\r
305 * @param origin seek type:
\r
309 * @return standard error code*/
\r
310 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
\r
312 /**@brief Get file position.
\r
313 * @param f file handle
\r
314 * @return actual file position */
\r
315 uint64_t ext4_ftell(ext4_file *f);
\r
317 /**@brief Get file size.
\r
318 * @param f file handle
\r
319 * @return file size */
\r
320 uint64_t ext4_fsize(ext4_file *f);
\r
322 /*********************************DIRECTORY OPERATION***********************/
\r
324 /**@brief Recursive directory remove.
\r
325 * @param path directory path to remove
\r
326 * @return standard error code*/
\r
327 int ext4_dir_rm(const char *path);
\r
329 /**@brief Create new directory.
\r
330 * @param name new directory name
\r
331 * @return standard error code*/
\r
332 int ext4_dir_mk(const char *path);
\r
334 /**@brief Directory open.
\r
335 * @param d directory handle
\r
336 * @param path directory path
\r
337 * @return standard error code*/
\r
338 int ext4_dir_open(ext4_dir *d, const char *path);
\r
340 /**@brief Directory close.
\r
341 * @param d directory handle
\r
342 * @return standard error code*/
\r
343 int ext4_dir_close(ext4_dir *d);
\r
345 /**@brief Return next directory entry.
\r
346 * @param d directory handle
\r
347 * @param id entry id
\r
348 * @return directory entry id (NULL if no entry)*/
\r
349 ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
\r
351 #endif /* EXT4_H_ */
\r