ext4: add ext4_cache_flush for explicit cache flush
[lwext4.git] / include / ext4.h
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
16  *
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.
27  */
28
29 /** @addtogroup lwext4
30  * @{
31  */
32 /**
33  * @file  ext4.h
34  * @brief Ext4 high level operations (files, directories, mount points...).
35  *        Client has to include only this file.
36  */
37
38 #ifndef EXT4_H_
39 #define EXT4_H_
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 #include <stdint.h>
46 #include <stddef.h>
47
48 #include "ext4_config.h"
49 #include "ext4_types.h"
50 #include "ext4_errno.h"
51 #include "ext4_oflags.h"
52 #include "ext4_debug.h"
53
54 #include "ext4_blockdev.h"
55
56 /********************************OS LOCK INFERFACE***************************/
57
58 /**@brief   OS dependent lock interface.*/
59 struct ext4_lock {
60
61         /**@brief   Lock access to mount point*/
62         void (*lock)(void);
63
64         /**@brief   Unlock access to mount point*/
65         void (*unlock)(void);
66 };
67
68 /********************************FILE DESCRIPTOR*****************************/
69
70 /**@brief   File descriptor*/
71 typedef struct ext4_file {
72
73         /**@brief   Mount point handle.*/
74         struct ext4_mountpoint *mp;
75
76         /**@brief   File inode id*/
77         uint32_t inode;
78
79         /**@brief   Open flags.*/
80         uint32_t flags;
81
82         /**@brief   File size.*/
83         uint64_t fsize;
84
85         /**@brief   File position*/
86         uint64_t fpos;
87 } ext4_file;
88
89 /*****************************DIRECTORY DESCRIPTOR***************************/
90
91 /**@brief   Directory entry descriptor. Copy from ext4_types.h*/
92 typedef struct ext4_direntry {
93         uint32_t inode;
94         uint16_t entry_length;
95         uint8_t name_length;
96         uint8_t inode_type;
97         uint8_t name[255];
98 } ext4_direntry;
99
100 typedef struct ext4_dir {
101         /**@brief   File descriptor*/
102         ext4_file f;
103         /**@brief   Current directory entry.*/
104         ext4_direntry de;
105         /**@brief   Next entry offset*/
106         uint64_t next_off;
107 } ext4_dir;
108
109 /********************************MOUNT OPERATIONS****************************/
110
111 /**@brief   Register a block device to a name.
112  *          @warning Block device has to be filled by
113  *          Block cache may by created automatically when bc parameter is NULL.
114  * @param   bd block device
115  * @param   bd block device cache
116  * @param   dev_name register name
117  * @param   standard error code*/
118 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
119                          const char *dev_name);
120
121 /**@brief   Mount a block device with EXT4 partition to the mount point.
122  * @param   dev_name block device name (@ref ext4_device_register)
123  * @param   mount_point mount point, for example
124  *          -   /
125  *          -   /my_partition/
126  *          -   /my_second_partition/
127  * @param   read_only mount as read-only mode.
128  *
129  * @return standard error code */
130 int ext4_mount(const char *dev_name,
131                const char *mount_point,
132                bool read_only);
133
134 /**@brief   Umount operation.
135  * @param   mount_point mount name
136  * @return  standard error code */
137 int ext4_umount(const char *mount_point);
138
139 /**@brief   Start journaling. Journaling start/stop functions are transparent
140  *          and might be used on filesystems without journaling support.
141  * @warning Usage:
142  *              ext4_mount("sda1", "/");
143  *              ext4_journal_start("/");
144  *
145  *              //File operations here...
146  *
147  *              ext4_journal_stop("/");
148  *              ext4_umount("/");
149  * @param   mount_point mount name
150  * @return  standard error code */
151 int ext4_journal_start(const char *mount_point);
152
153 /**@brief   Stop journaling. Journaling start/stop functions are transparent
154  *          and might be used on filesystems without journaling support.
155  * @param   mount_point mount name
156  * @return  standard error code */
157 int ext4_journal_stop(const char *mount_point);
158
159 /**@brief   Journal recovery.
160  * @param   mount_point mount point
161  * @warning Must be called after @ref ext4_mount
162  * @return standard error code */
163 int ext4_recover(const char *mount_point);
164
165 /**@brief   Some of the filesystem stats.*/
166 struct ext4_mount_stats {
167         uint32_t inodes_count;
168         uint32_t free_inodes_count;
169         uint64_t blocks_count;
170         uint64_t free_blocks_count;
171
172         uint32_t block_size;
173         uint32_t block_group_count;
174         uint32_t blocks_per_group;
175         uint32_t inodes_per_group;
176
177         char volume_name[16];
178 };
179
180 /**@brief   Get file system params.
181  * @param   mount_point mount path
182  * @param   stats ext fs stats
183  * @return  standard error code */
184 int ext4_mount_point_stats(const char *mount_point,
185                            struct ext4_mount_stats *stats);
186
187 /**@brief   Setup OS lock routines.
188  * @param   mount_point mount path
189  * @param   locks - lock and unlock functions
190  * @return  standard error code */
191 int ext4_mount_setup_locks(const char *mount_point,
192                            const struct ext4_lock *locks);
193
194 /**@brief   Acquire the filesystem superblock pointer of a mp.
195  * @param   mount_point mount path
196  * @param   superblock pointer
197  * @return  standard error code */
198 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
199
200 /**@brief   Enable/disable write back cache mode.
201  * @warning Default model of cache is write trough. It means that when You do:
202  *
203  *          ext4_fopen(...);
204  *          ext4_fwrie(...);
205  *                           < --- data is flushed to physical drive
206  *
207  *          When you do:
208  *          ext4_cache_write_back(..., 1);
209  *          ext4_fopen(...);
210  *          ext4_fwrie(...);
211  *                           < --- data is NOT flushed to physical drive
212  *          ext4_cache_write_back(..., 0);
213  *                           < --- when write back mode is disabled all
214  *                                 cache data will be flushed
215  * To enable write back mode permanently just call this function
216  * once after ext4_mount (and disable before ext4_umount).
217  *
218  * Some of the function use write back cache mode internally.
219  * If you enable write back mode twice you have to disable it twice
220  * to flush all data:
221  *
222  *      ext4_cache_write_back(..., 1);
223  *      ext4_cache_write_back(..., 1);
224  *
225  *      ext4_cache_write_back(..., 0);
226  *      ext4_cache_write_back(..., 0);
227  *
228  * Write back mode is useful when you want to create a lot of empty
229  * files/directories.
230  *
231  * @param   path mount point path
232  * @param   on enable/disable
233  *
234  * @return  standard error code */
235 int ext4_cache_write_back(const char *path, bool on);
236
237
238 /**@brief   Force cache flush.
239  *
240  * @param   path mount point path
241  *
242  * @return  standard error code */
243 int ext4_cache_flush(const char *path);
244
245 /********************************FILE OPERATIONS*****************************/
246
247 /**@brief   Remove file by path.
248  * @param   path path to file
249  * @return  standard error code */
250 int ext4_fremove(const char *path);
251
252 /**@brief   create a hardlink for a file.
253  * @param   path path to file
254  * @param   hardlink_path path of hardlink
255  * @return  standard error code */
256 int ext4_flink(const char *path, const char *hardlink_path);
257
258 /**@brief Rename file
259  * @param path source
260  * @param new_path destination
261  * @return  standard error code */
262 int ext4_frename(const char *path, const char *new_path);
263
264 /**@brief   File open function.
265  * @param   path filename (has to start from mount point)
266  *          /my_partition/my_file
267  * @param   flags open file flags
268  *  |---------------------------------------------------------------|
269  *  |   r or rb                 O_RDONLY                            |
270  *  |---------------------------------------------------------------|
271  *  |   w or wb                 O_WRONLY|O_CREAT|O_TRUNC            |
272  *  |---------------------------------------------------------------|
273  *  |   a or ab                 O_WRONLY|O_CREAT|O_APPEND           |
274  *  |---------------------------------------------------------------|
275  *  |   r+ or rb+ or r+b        O_RDWR                              |
276  *  |---------------------------------------------------------------|
277  *  |   w+ or wb+ or w+b        O_RDWR|O_CREAT|O_TRUNC              |
278  *  |---------------------------------------------------------------|
279  *  |   a+ or ab+ or a+b        O_RDWR|O_CREAT|O_APPEND             |
280  *  |---------------------------------------------------------------|
281  *
282  * @return  standard error code*/
283 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
284
285 /**@brief   Alternate file open function.
286  * @param   filename, (has to start from mount point)
287  *          /my_partition/my_file
288  * @param   flags open file flags
289  * @return  standard error code*/
290 int ext4_fopen2(ext4_file *f, const char *path, int flags);
291
292 /**@brief   File close function.
293  * @param   f file handle
294  * @return  standard error code*/
295 int ext4_fclose(ext4_file *f);
296
297 /**@brief   Fill in the ext4_inode buffer.
298  * @param   path fetch inode data of the path
299  * @param   ret_ino the inode id of the path
300  * @param   ext4_inode buffer
301  * @return  standard error code*/
302 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
303                         struct ext4_inode *inode);
304
305 /**@brief   File truncate function.
306  * @param   f file handle
307  * @param   new file size
308  * @return  standard error code*/
309 int ext4_ftruncate(ext4_file *f, uint64_t size);
310
311 /**@brief   Read data from file.
312  * @param   f file handle
313  * @param   buf output buffer
314  * @param   size bytes to read
315  * @param   rcnt bytes read (may be NULL)
316  * @return  standard error code*/
317 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
318
319 /**@brief   Write data to file.
320  * @param   f file handle
321  * @param   buf data to write
322  * @param   size write length
323  * @param   wcnt bytes written (may be NULL)
324  * @return  standard error code*/
325 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
326
327 /**@brief   File seek operation.
328  * @param   f file handle
329  * @param   offset offset to seek
330  * @param   origin seek type:
331  *              @ref SEEK_SET
332  *              @ref SEEK_CUR
333  *              @ref SEEK_END
334  * @return  standard error code*/
335 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
336
337 /**@brief   Get file position.
338  * @param   f file handle
339  * @return  actual file position */
340 uint64_t ext4_ftell(ext4_file *f);
341
342 /**@brief   Get file size.
343  * @param   f file handle
344  * @return  file size */
345 uint64_t ext4_fsize(ext4_file *f);
346
347 /**@brief Change file/directory/link mode bits
348  * @param path to file/dir/link
349  * @param mode new mode bits (for example 0777)
350  * @return  standard error code*/
351 int ext4_chmod(const char *path, uint32_t mode);
352
353 /**@brief Change file owner and group
354  * @param path to file/dir/link
355  * @param uid user id
356  * @param gid group id
357  * @return  standard error code*/
358 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
359
360 /**@brief Set file access time
361  * @param path to file/dir/link
362  * @param atime access timestamp
363  * @return  standard error code*/
364 int ext4_file_set_atime(const char *path, uint32_t atime);
365
366 /**@brief Set file modify time
367  * @param path to file/dir/link
368  * @param mtime modify timestamp
369  * @return  standard error code*/
370 int ext4_file_set_mtime(const char *path, uint32_t mtime);
371
372 /**@brief Set file change time
373  * @param path to file/dir/link
374  * @param ctime change timestamp
375  * @return  standard error code*/
376 int ext4_file_set_ctime(const char *path, uint32_t ctime);
377
378 /**@brief Create symbolic link
379  * @param target destination path
380  * @param path source entry
381  * @return standard error code*/
382 int ext4_fsymlink(const char *target, const char *path);
383
384 /**@brief Create special file
385  * @param path path to new file
386  * @param filetype The filetype of the new special file
387  *        (that must not be regular file, directory, or unknown type)
388  * @param dev if filetype is char device or block device,
389  *        the device number will become the payload in the inode
390  * @return standard error code*/
391 int ext4_mknod(const char *path, int filetype, uint32_t dev);
392
393 /**@brief Read symbolic link payload
394  * @param path to symlink
395  * @param buf output buffer
396  * @param bufsize output buffer max size
397  * @param rcnt bytes read
398  * @return standard error code*/
399 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
400
401 /**@brief Set extended attribute
402  * @param path to file/directory
403  * @param name name of the entry to add
404  * @param name_len length of @name in bytes
405  * @param data data of the entry to add
406  * @param data_size size of data to add
407  * @param replace this boolean is deprecated.
408  * @return standard error code*/
409 int ext4_setxattr(const char *path, const char *name, size_t name_len,
410                   const void *data, size_t data_size, bool replace);
411
412 /**@brief Get extended attribute
413  * @param path to file/directory
414  * @param name name of the entry to get
415  * @param name_len length of @name in bytes
416  * @param data data of the entry to get
417  * @param data_size size of data to get
418  * @return standard error code*/
419 int ext4_getxattr(const char *path, const char *name, size_t name_len,
420                   void *buf, size_t buf_size, size_t *data_size);
421
422 /**@brief List extended attributes
423  * @param path to file/directory
424  * @param list list to hold the name of entries
425  * @param size size of @list in bytes
426  * @param ret_size used bytes of @list
427  * @return standard error code*/
428 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
429
430 /**@brief Remove extended attribute
431  * @param path to file/directory
432  * @param name name of the entry to remove
433  * @param name_len length of @name in bytes
434  * @return standard error code*/
435 int ext4_removexattr(const char *path, const char *name, size_t name_len);
436
437
438 /*********************************DIRECTORY OPERATION***********************/
439
440 /**@brief   Recursive directory remove.
441  * @param   path directory path to remove
442  * @return  standard error code*/
443 int ext4_dir_rm(const char *path);
444
445 /**@brief Rename/move directory
446  * @param path source
447  * @param new_path destination
448  * @return  standard error code */
449 int ext4_dir_mv(const char *path, const char *new_path);
450
451 /**@brief   Create new directory.
452  * @param   name new directory name
453  * @return  standard error code*/
454 int ext4_dir_mk(const char *path);
455
456 /**@brief   Directory open.
457  * @param   d directory handle
458  * @param   path directory path
459  * @return  standard error code*/
460 int ext4_dir_open(ext4_dir *d, const char *path);
461
462 /**@brief   Directory close.
463  * @param   d directory handle
464  * @return  standard error code*/
465 int ext4_dir_close(ext4_dir *d);
466
467 /**@brief   Return next directory entry.
468  * @param   d directory handle
469  * @param   id entry id
470  * @return  directory entry id (NULL if no entry)*/
471 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
472
473 /**@brief   Rewine directory entry offset.
474  * @param   d directory handle*/
475 void ext4_dir_entry_rewind(ext4_dir *d);
476
477
478 #ifdef __cplusplus
479 }
480 #endif
481
482 #endif /* EXT4_H_ */
483
484 /**
485  * @}
486  */