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