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