#ifdef CONFIG_HAVE_OWN_OFLAGS should be #if. Also, if CONFIG_HAVE_OWN_OFLAGS == 1...
[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 #include "ext4_config.h"
42 #include "ext4_types.h"
43 #include "ext4_blockdev.h"
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 #endif
97
98 /********************************OS LOCK INFERFACE***************************/
99
100 /**@brief   OS dependent lock interface.*/
101 struct ext4_lock {
102
103         /**@brief   Lock access to mount point*/
104         void (*lock)(void);
105
106         /**@brief   Unlock access to mount point*/
107         void (*unlock)(void);
108 };
109
110 /********************************FILE DESCRIPTOR*****************************/
111
112 /**@brief   File descriptor*/
113 typedef struct ext4_file {
114
115         /**@brief   Mount point handle.*/
116         struct ext4_mountpoint *mp;
117
118         /**@brief   File inode id*/
119         uint32_t inode;
120
121         /**@brief   Open flags.*/
122         uint32_t flags;
123
124         /**@brief   File size.*/
125         uint64_t fsize;
126
127         /**@brief   File position*/
128         uint64_t fpos;
129 } ext4_file;
130
131 /*****************************DIRECTORY DESCRIPTOR***************************/
132
133 /**@brief   Directory entry descriptor. Copy from ext4_types.h*/
134 typedef struct {
135         uint32_t inode;
136         uint16_t entry_length;
137         uint8_t name_length;
138         uint8_t inode_type;
139         uint8_t name[255];
140 } ext4_direntry;
141
142 typedef struct {
143         /**@brief   File descriptor*/
144         ext4_file f;
145         /**@brief   Current directory entry.*/
146         ext4_direntry de;
147         /**@brief   Next entry offset*/
148         uint64_t next_off;
149 } ext4_dir;
150
151 /********************************MOUNT OPERATIONS****************************/
152
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);
164
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
168  *          -   /
169  *          -   /my_partition/
170  *          -   /my_second_partition/
171  *
172  * @return standard error code */
173 int ext4_mount(const char *dev_name, const char *mount_point);
174
175 /**@brief   Umount operation.
176  * @param   mount_point mount name
177  * @return  standard error code */
178 int ext4_umount(const char *mount_point);
179
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;
186
187         uint32_t block_size;
188         uint32_t block_group_count;
189         uint32_t blocks_per_group;
190         uint32_t inodes_per_group;
191
192         char volume_name[16];
193 };
194
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);
201
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);
208
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);
214
215 /**@brief   Enable/disable write back cache mode.
216  * @warning Default model of cache is write trough. It means that when You do:
217  *
218  *          ext4_fopen(...);
219  *          ext4_fwrie(...);
220  *                           < --- data is flushed to physical drive
221  *
222  *          When you do:
223  *          ext4_cache_write_back(..., 1);
224  *          ext4_fopen(...);
225  *          ext4_fwrie(...);
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).
232  *
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
235  * to flush all data:
236  *
237  *      ext4_cache_write_back(..., 1);
238  *      ext4_cache_write_back(..., 1);
239  *
240  *      ext4_cache_write_back(..., 0);
241  *      ext4_cache_write_back(..., 0);
242  *
243  * Write back mode is useful when you want to create a lot of empty
244  * files/directories.
245  *
246  * @param   path mount point path
247  * @param   on enable/disable
248  *
249  * @return  standard error code */
250 int ext4_cache_write_back(const char *path, bool on);
251
252 /********************************FILE OPERATIONS*****************************/
253
254 /**@brief   Remove file by path.
255  * @param   path path to file
256  * @return  standard error code */
257 int ext4_fremove(const char *path);
258
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);
264
265 /**@brief Rename file
266  * @param path source
267  * @param new_path destination
268  * @return  standard error code */
269 int ext4_frename(const char *path, const char *new_path);
270
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  *  |---------------------------------------------------------------|
288  *
289  * @return  standard error code*/
290 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
291
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);
298
299 /**@brief   File close function.
300  * @param   f file handle
301  * @return  standard error code*/
302 int ext4_fclose(ext4_file *f);
303
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,
310                         uint32_t *ret_ino,
311                         struct ext4_inode *inode);
312
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);
318
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);
326
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);
334
335 /**@brief   File seek operation.
336  * @param   f file handle
337  * @param   offset offset to seek
338  * @param   origin seek type:
339  *              @ref SEEK_SET
340  *              @ref SEEK_CUR
341  *              @ref SEEK_END
342  * @return  standard error code*/
343 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
344
345 /**@brief   Get file position.
346  * @param   f file handle
347  * @return  actual file position */
348 uint64_t ext4_ftell(ext4_file *f);
349
350 /**@brief   Get file size.
351  * @param   f file handle
352  * @return  file size */
353 uint64_t ext4_fsize(ext4_file *f);
354
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);
360
361 int ext4_fsymlink(const char *target, const char *path);
362
363 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
364
365 /*********************************DIRECTORY OPERATION***********************/
366
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);
371
372 /**@brief   Create new directory.
373  * @param   name new directory name
374  * @return  standard error code*/
375 int ext4_dir_mk(const char *path);
376
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);
382
383 /**@brief   Directory close.
384  * @param   d directory handle
385  * @return  standard error code*/
386 int ext4_dir_close(ext4_dir *d);
387
388 /**@brief   Return next directory entry.
389  * @param   d directory handle
390  * @param   id entry id
391  * @return  directory entry id (NULL if no entry)*/
392 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
393
394 /**@brief   Rewine directory entry offset.
395  * @param   d directory handle*/
396 void ext4_dir_entry_rewind(ext4_dir *d);
397
398 #endif /* EXT4_H_ */
399
400 /**
401  * @}
402  */