ext4: @replace in ext4_setxattr() is deprecated
[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 /********************************FILE OPERATIONS*****************************/
238
239 /**@brief   Remove file by path.
240  * @param   path path to file
241  * @return  standard error code */
242 int ext4_fremove(const char *path);
243
244 /**@brief   create a hardlink for a file.
245  * @param   path path to file
246  * @param   hardlink_path path of hardlink
247  * @return  standard error code */
248 int ext4_flink(const char *path, const char *hardlink_path);
249
250 /**@brief Rename file
251  * @param path source
252  * @param new_path destination
253  * @return  standard error code */
254 int ext4_frename(const char *path, const char *new_path);
255
256 /**@brief   File open function.
257  * @param   path filename (has to start from mount point)
258  *          /my_partition/my_file
259  * @param   flags open file flags
260  *  |---------------------------------------------------------------|
261  *  |   r or rb                 O_RDONLY                            |
262  *  |---------------------------------------------------------------|
263  *  |   w or wb                 O_WRONLY|O_CREAT|O_TRUNC            |
264  *  |---------------------------------------------------------------|
265  *  |   a or ab                 O_WRONLY|O_CREAT|O_APPEND           |
266  *  |---------------------------------------------------------------|
267  *  |   r+ or rb+ or r+b        O_RDWR                              |
268  *  |---------------------------------------------------------------|
269  *  |   w+ or wb+ or w+b        O_RDWR|O_CREAT|O_TRUNC              |
270  *  |---------------------------------------------------------------|
271  *  |   a+ or ab+ or a+b        O_RDWR|O_CREAT|O_APPEND             |
272  *  |---------------------------------------------------------------|
273  *
274  * @return  standard error code*/
275 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
276
277 /**@brief   Alternate file open function.
278  * @param   filename, (has to start from mount point)
279  *          /my_partition/my_file
280  * @param   flags open file flags
281  * @return  standard error code*/
282 int ext4_fopen2(ext4_file *f, const char *path, int flags);
283
284 /**@brief   File close function.
285  * @param   f file handle
286  * @return  standard error code*/
287 int ext4_fclose(ext4_file *f);
288
289 /**@brief   Fill in the ext4_inode buffer.
290  * @param   path fetch inode data of the path
291  * @param   ret_ino the inode id of the path
292  * @param   ext4_inode buffer
293  * @return  standard error code*/
294 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
295                         struct ext4_inode *inode);
296
297 /**@brief   File truncate function.
298  * @param   f file handle
299  * @param   new file size
300  * @return  standard error code*/
301 int ext4_ftruncate(ext4_file *f, uint64_t size);
302
303 /**@brief   Read data from file.
304  * @param   f file handle
305  * @param   buf output buffer
306  * @param   size bytes to read
307  * @param   rcnt bytes read (may be NULL)
308  * @return  standard error code*/
309 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
310
311 /**@brief   Write data to file.
312  * @param   f file handle
313  * @param   buf data to write
314  * @param   size write length
315  * @param   wcnt bytes written (may be NULL)
316  * @return  standard error code*/
317 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
318
319 /**@brief   File seek operation.
320  * @param   f file handle
321  * @param   offset offset to seek
322  * @param   origin seek type:
323  *              @ref SEEK_SET
324  *              @ref SEEK_CUR
325  *              @ref SEEK_END
326  * @return  standard error code*/
327 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
328
329 /**@brief   Get file position.
330  * @param   f file handle
331  * @return  actual file position */
332 uint64_t ext4_ftell(ext4_file *f);
333
334 /**@brief   Get file size.
335  * @param   f file handle
336  * @return  file size */
337 uint64_t ext4_fsize(ext4_file *f);
338
339 /**@brief Change file/directory/link mode bits
340  * @param path to file/dir/link
341  * @param mode new mode bits (for example 0777)
342  * @return  standard error code*/
343 int ext4_chmod(const char *path, uint32_t mode);
344
345 /**@brief Change file owner and group
346  * @param path to file/dir/link
347  * @param uid user id
348  * @param gid group id
349  * @return  standard error code*/
350 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
351
352 /**@brief Set file access time
353  * @param path to file/dir/link
354  * @param atime access timestamp
355  * @return  standard error code*/
356 int ext4_file_set_atime(const char *path, uint32_t atime);
357
358 /**@brief Set file modify time
359  * @param path to file/dir/link
360  * @param mtime modify timestamp
361  * @return  standard error code*/
362 int ext4_file_set_mtime(const char *path, uint32_t mtime);
363
364 /**@brief Set file change time
365  * @param path to file/dir/link
366  * @param ctime change timestamp
367  * @return  standard error code*/
368 int ext4_file_set_ctime(const char *path, uint32_t ctime);
369
370 /**@brief Create symbolic link
371  * @param target destination path
372  * @param path source entry
373  * @return standard error code*/
374 int ext4_fsymlink(const char *target, const char *path);
375
376
377 /**@brief Read symbolic link payload
378  * @param path to symlink
379  * @param buf output buffer
380  * @param bufsize output buffer max size
381  * @param rcnt bytes read
382  * @return standard error code*/
383 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
384
385 /**@brief Set extended attribute
386  * @param path to file/directory
387  * @param name name of the entry to add
388  * @param name_len length of @name in bytes
389  * @param data data of the entry to add
390  * @param data_size size of data to add
391  * @param replace this boolean is deprecated.
392  * @return standard error code*/
393 int ext4_setxattr(const char *path, const char *name, size_t name_len,
394                   const void *data, size_t data_size, bool replace);
395
396 /**@brief Get extended attribute
397  * @param path to file/directory
398  * @param name name of the entry to get
399  * @param name_len length of @name in bytes
400  * @param data data of the entry to get
401  * @param data_size size of data to get
402  * @return standard error code*/
403 int ext4_getxattr(const char *path, const char *name, size_t name_len,
404                   void *buf, size_t buf_size, size_t *data_size);
405
406 /**@brief List extended attributes
407  * @param path to file/directory
408  * @param list list to hold the name of entries
409  * @param size size of @list in bytes
410  * @param ret_size used bytes of @list
411  * @return standard error code*/
412 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
413
414 /**@brief Remove extended attribute
415  * @param path to file/directory
416  * @param name name of the entry to remove
417  * @param name_len length of @name in bytes
418  * @return standard error code*/
419 int ext4_removexattr(const char *path, const char *name, size_t name_len);
420
421
422 /*********************************DIRECTORY OPERATION***********************/
423
424 /**@brief   Recursive directory remove.
425  * @param   path directory path to remove
426  * @return  standard error code*/
427 int ext4_dir_rm(const char *path);
428
429 /**@brief Rename/move directory
430  * @param path source
431  * @param new_path destination
432  * @return  standard error code */
433 int ext4_dir_mv(const char *path, const char *new_path);
434
435 /**@brief   Create new directory.
436  * @param   name new directory name
437  * @return  standard error code*/
438 int ext4_dir_mk(const char *path);
439
440 /**@brief   Directory open.
441  * @param   d directory handle
442  * @param   path directory path
443  * @return  standard error code*/
444 int ext4_dir_open(ext4_dir *d, const char *path);
445
446 /**@brief   Directory close.
447  * @param   d directory handle
448  * @return  standard error code*/
449 int ext4_dir_close(ext4_dir *d);
450
451 /**@brief   Return next directory entry.
452  * @param   d directory handle
453  * @param   id entry id
454  * @return  directory entry id (NULL if no entry)*/
455 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
456
457 /**@brief   Rewine directory entry offset.
458  * @param   d directory handle*/
459 void ext4_dir_entry_rewind(ext4_dir *d);
460
461
462 #ifdef __cplusplus
463 }
464 #endif
465
466 #endif /* EXT4_H_ */
467
468 /**
469  * @}
470  */