2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
34 * @brief Ext4 high level operations (files, directories, mount points...).
35 * Client has to include only this file.
41 #include "ext4_config.h"
42 #include "ext4_types.h"
43 #include "ext4_blockdev.h"
48 /********************************FILE OPEN FLAGS*****************************/
50 #if CONFIG_HAVE_OWN_OFLAGS
77 #define O_APPEND 02000
80 /********************************FILE SEEK FLAGS*****************************/
98 /********************************OS LOCK INFERFACE***************************/
100 /**@brief OS dependent lock interface.*/
103 /**@brief Lock access to mount point*/
106 /**@brief Unlock access to mount point*/
107 void (*unlock)(void);
110 /********************************FILE DESCRIPTOR*****************************/
112 /**@brief File descriptor*/
113 typedef struct ext4_file {
115 /**@brief Mount point handle.*/
116 struct ext4_mountpoint *mp;
118 /**@brief File inode id*/
121 /**@brief Open flags.*/
124 /**@brief File size.*/
127 /**@brief File position*/
131 /*****************************DIRECTORY DESCRIPTOR***************************/
133 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
136 uint16_t entry_length;
143 /**@brief File descriptor*/
145 /**@brief Current directory entry.*/
147 /**@brief Next entry offset*/
151 /********************************MOUNT OPERATIONS****************************/
153 /**@brief Register a block device to a name.
154 * @warning Block device has to be filled by
155 * @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created
156 * @ref EXT4_BCACHE_STATIC_INSTANCE.
157 * Block cache may by created automatically when bc parameter is 0.
158 * @param bd block device
159 * @param bd block device cache (0 = automatic cache mode)
160 * @param dev_name register name
161 * @param standard error code*/
162 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
163 const char *dev_name);
165 /**@brief Mount a block device with EXT4 partition to the mount point.
166 * @param dev_name block device name (@ref ext4_device_register)
167 * @param mount_point mount point, for example
170 * - /my_second_partition/
172 * @return standard error code */
173 int ext4_mount(const char *dev_name, const char *mount_point);
175 /**@brief Umount operation.
176 * @param mount_point mount name
177 * @return standard error code */
178 int ext4_umount(const char *mount_point);
180 /**@brief Some of the filesystem stats.*/
181 struct ext4_mount_stats {
182 uint32_t inodes_count;
183 uint32_t free_inodes_count;
184 uint64_t blocks_count;
185 uint64_t free_blocks_count;
188 uint32_t block_group_count;
189 uint32_t blocks_per_group;
190 uint32_t inodes_per_group;
192 char volume_name[16];
195 /**@brief Get file system params.
196 * @param mount_point mount path
197 * @param stats ext fs stats
198 * @return standard error code */
199 int ext4_mount_point_stats(const char *mount_point,
200 struct ext4_mount_stats *stats);
202 /**@brief Setup OS lock routines.
203 * @param mount_point mount path
204 * @param locks - lock and unlock functions
205 * @return standard error code */
206 int ext4_mount_setup_locks(const char *mount_point,
207 const struct ext4_lock *locks);
209 /**@brief Acquire the filesystem superblock pointer of a mp.
210 * @param mount_point mount path
211 * @param superblock pointer
212 * @return standard error code */
213 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
215 /**@brief Enable/disable write back cache mode.
216 * @warning Default model of cache is write trough. It means that when You do:
220 * < --- data is flushed to physical drive
223 * ext4_cache_write_back(..., 1);
226 * < --- data is NOT flushed to physical drive
227 * ext4_cache_write_back(..., 0);
228 * < --- when write back mode is disabled all
229 * cache data will be flushed
230 * To enable write back mode permanently just call this function
231 * once after ext4_mount (and disable before ext4_umount).
233 * Some of the function use write back cache mode internally.
234 * If you enable write back mode twice you have to disable it twice
237 * ext4_cache_write_back(..., 1);
238 * ext4_cache_write_back(..., 1);
240 * ext4_cache_write_back(..., 0);
241 * ext4_cache_write_back(..., 0);
243 * Write back mode is useful when you want to create a lot of empty
246 * @param path mount point path
247 * @param on enable/disable
249 * @return standard error code */
250 int ext4_cache_write_back(const char *path, bool on);
252 /********************************FILE OPERATIONS*****************************/
254 /**@brief Remove file by path.
255 * @param path path to file
256 * @return standard error code */
257 int ext4_fremove(const char *path);
259 /**@brief create a hardlink for a file.
260 * @param path path to file
261 * @param hardlink_path path of hardlink
262 * @return standard error code */
263 int ext4_flink(const char *path, const char *hardlink_path);
265 /**@brief Rename file
267 * @param new_path destination
268 * @return standard error code */
269 int ext4_frename(const char *path, const char *new_path);
271 /**@brief File open function.
272 * @param filename, (has to start from mount point)
273 * /my_partition/my_file
274 * @param flags open file flags
275 * |---------------------------------------------------------------|
276 * | r or rb O_RDONLY |
277 * |---------------------------------------------------------------|
278 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
279 * |---------------------------------------------------------------|
280 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
281 * |---------------------------------------------------------------|
282 * | r+ or rb+ or r+b O_RDWR |
283 * |---------------------------------------------------------------|
284 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
285 * |---------------------------------------------------------------|
286 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
287 * |---------------------------------------------------------------|
289 * @return standard error code*/
290 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
292 /**@brief Alternate file open function.
293 * @param filename, (has to start from mount point)
294 * /my_partition/my_file
295 * @param flags open file flags
296 * @return standard error code*/
297 int ext4_fopen2(ext4_file *f, const char *path, int flags);
299 /**@brief File close function.
300 * @param f file handle
301 * @return standard error code*/
302 int ext4_fclose(ext4_file *f);
304 /**@brief Fill in the ext4_inode buffer.
305 * @param path fetch inode data of the path
306 * @param ret_ino the inode id of the path
307 * @param ext4_inode buffer
308 * @return standard error code*/
309 int ext4_fill_raw_inode(const char *path,
311 struct ext4_inode *inode);
313 /**@brief File truncate function.
314 * @param f file handle
315 * @param new file size
316 * @return standard error code*/
317 int ext4_ftruncate(ext4_file *f, uint64_t size);
319 /**@brief Read data from file.
320 * @param f file handle
321 * @param buf output buffer
322 * @param size bytes to read
323 * @param rcnt bytes read (may be NULL)
324 * @return standard error code*/
325 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
327 /**@brief Write data to file.
328 * @param f file handle
329 * @param buf data to write
330 * @param size write length
331 * @param wcnt bytes written (may be NULL)
332 * @return standard error code*/
333 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
335 /**@brief File seek operation.
336 * @param f file handle
337 * @param offset offset to seek
338 * @param origin seek type:
342 * @return standard error code*/
343 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
345 /**@brief Get file position.
346 * @param f file handle
347 * @return actual file position */
348 uint64_t ext4_ftell(ext4_file *f);
350 /**@brief Get file size.
351 * @param f file handle
352 * @return file size */
353 uint64_t ext4_fsize(ext4_file *f);
355 int ext4_chmod(const char *path, uint32_t mode);
356 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
357 int ext4_file_set_atime(const char *path, uint32_t atime);
358 int ext4_file_set_mtime(const char *path, uint32_t mtime);
359 int ext4_file_set_ctime(const char *path, uint32_t ctime);
361 int ext4_fsymlink(const char *target, const char *path);
363 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
365 /*********************************DIRECTORY OPERATION***********************/
367 /**@brief Recursive directory remove.
368 * @param path directory path to remove
369 * @return standard error code*/
370 int ext4_dir_rm(const char *path);
372 /**@brief Create new directory.
373 * @param name new directory name
374 * @return standard error code*/
375 int ext4_dir_mk(const char *path);
377 /**@brief Directory open.
378 * @param d directory handle
379 * @param path directory path
380 * @return standard error code*/
381 int ext4_dir_open(ext4_dir *d, const char *path);
383 /**@brief Directory close.
384 * @param d directory handle
385 * @return standard error code*/
386 int ext4_dir_close(ext4_dir *d);
388 /**@brief Return next directory entry.
389 * @param d directory handle
391 * @return directory entry id (NULL if no entry)*/
392 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
394 /**@brief Rewine directory entry offset.
395 * @param d directory handle*/
396 void ext4_dir_entry_rewind(ext4_dir *d);