2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup lwext4
34 * @brief Ext4 high level operations (files, directories, mount points...).
35 * Client has to include only this file.
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"
54 #include "ext4_blockdev.h"
56 /********************************OS LOCK INFERFACE***************************/
58 /**@brief OS dependent lock interface.*/
61 /**@brief Lock access to mount point*/
64 /**@brief Unlock access to mount point*/
68 /********************************FILE DESCRIPTOR*****************************/
70 /**@brief File descriptor*/
71 typedef struct ext4_file {
73 /**@brief Mount point handle.*/
74 struct ext4_mountpoint *mp;
76 /**@brief File inode id*/
79 /**@brief Open flags.*/
82 /**@brief File size.*/
85 /**@brief File position*/
89 /*****************************DIRECTORY DESCRIPTOR***************************/
91 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
92 typedef struct ext4_direntry {
94 uint16_t entry_length;
100 typedef struct ext4_dir {
101 /**@brief File descriptor*/
103 /**@brief Current directory entry.*/
105 /**@brief Next entry offset*/
109 /********************************MOUNT OPERATIONS****************************/
111 /**@brief Register block device.
113 * @param bd Block device.
114 * @param bd Block device cache.
115 * @param dev_name Block device name.
117 * @return Standard error code.*/
118 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
119 const char *dev_name);
121 /**@brief Un-register block device.
123 * @param dev_name Block device name.
125 * @return Standard error code.*/
126 int ext4_device_unregister(const char *dev_name);
128 /**@brief Un-register all block devices.
130 * @return Standard error code.*/
131 int ext4_device_unregister_all(void);
133 /**@brief Mount a block device with EXT4 partition to the mount point.
135 * @param dev_name block Device name (@ref ext4_device_register).
136 * @param mount_point Mount point, for example:
139 * - /my_second_partition/
140 * @param read_only mount as read-only mode.
142 * @return Standard error code */
143 int ext4_mount(const char *dev_name,
144 const char *mount_point,
147 /**@brief Umount operation.
148 * @param mount_point mount name
149 * @return standard error code */
150 int ext4_umount(const char *mount_point);
152 /**@brief Start journaling. Journaling start/stop functions are transparent
153 * and might be used on filesystems without journaling support.
155 * ext4_mount("sda1", "/");
156 * ext4_journal_start("/");
158 * //File operations here...
160 * ext4_journal_stop("/");
162 * @param mount_point mount name
163 * @return standard error code */
164 int ext4_journal_start(const char *mount_point);
166 /**@brief Stop journaling. Journaling start/stop functions are transparent
167 * and might be used on filesystems without journaling support.
168 * @param mount_point mount name
169 * @return standard error code */
170 int ext4_journal_stop(const char *mount_point);
172 /**@brief Journal recovery.
173 * @param mount_point mount point
174 * @warning Must be called after @ref ext4_mount
175 * @return standard error code */
176 int ext4_recover(const char *mount_point);
178 /**@brief Some of the filesystem stats.*/
179 struct ext4_mount_stats {
180 uint32_t inodes_count;
181 uint32_t free_inodes_count;
182 uint64_t blocks_count;
183 uint64_t free_blocks_count;
186 uint32_t block_group_count;
187 uint32_t blocks_per_group;
188 uint32_t inodes_per_group;
190 char volume_name[16];
193 /**@brief Get file system params.
194 * @param mount_point mount path
195 * @param stats ext fs stats
196 * @return standard error code */
197 int ext4_mount_point_stats(const char *mount_point,
198 struct ext4_mount_stats *stats);
200 /**@brief Setup OS lock routines.
201 * @param mount_point mount path
202 * @param locks - lock and unlock functions
203 * @return standard error code */
204 int ext4_mount_setup_locks(const char *mount_point,
205 const struct ext4_lock *locks);
207 /**@brief Acquire the filesystem superblock pointer of a mp.
208 * @param mount_point mount path
209 * @param superblock pointer
210 * @return standard error code */
211 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
213 /**@brief Enable/disable write back cache mode.
214 * @warning Default model of cache is write trough. It means that when You do:
218 * < --- data is flushed to physical drive
221 * ext4_cache_write_back(..., 1);
224 * < --- data is NOT flushed to physical drive
225 * ext4_cache_write_back(..., 0);
226 * < --- when write back mode is disabled all
227 * cache data will be flushed
228 * To enable write back mode permanently just call this function
229 * once after ext4_mount (and disable before ext4_umount).
231 * Some of the function use write back cache mode internally.
232 * If you enable write back mode twice you have to disable it twice
235 * ext4_cache_write_back(..., 1);
236 * ext4_cache_write_back(..., 1);
238 * ext4_cache_write_back(..., 0);
239 * ext4_cache_write_back(..., 0);
241 * Write back mode is useful when you want to create a lot of empty
244 * @param path mount point path
245 * @param on enable/disable
247 * @return standard error code */
248 int ext4_cache_write_back(const char *path, bool on);
251 /**@brief Force cache flush.
253 * @param path mount point path
255 * @return standard error code */
256 int ext4_cache_flush(const char *path);
258 /********************************FILE OPERATIONS*****************************/
260 /**@brief Remove file by path.
261 * @param path path to file
262 * @return standard error code */
263 int ext4_fremove(const char *path);
265 /**@brief create a hardlink for a file.
266 * @param path path to file
267 * @param hardlink_path path of hardlink
268 * @return standard error code */
269 int ext4_flink(const char *path, const char *hardlink_path);
271 /**@brief Rename file
273 * @param new_path destination
274 * @return standard error code */
275 int ext4_frename(const char *path, const char *new_path);
277 /**@brief File open function.
278 * @param path filename (has to start from mount point)
279 * /my_partition/my_file
280 * @param flags open file flags
281 * |---------------------------------------------------------------|
282 * | r or rb O_RDONLY |
283 * |---------------------------------------------------------------|
284 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
285 * |---------------------------------------------------------------|
286 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
287 * |---------------------------------------------------------------|
288 * | r+ or rb+ or r+b O_RDWR |
289 * |---------------------------------------------------------------|
290 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
291 * |---------------------------------------------------------------|
292 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
293 * |---------------------------------------------------------------|
295 * @return standard error code*/
296 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
298 /**@brief Alternate file open function.
299 * @param filename, (has to start from mount point)
300 * /my_partition/my_file
301 * @param flags open file flags
302 * @return standard error code*/
303 int ext4_fopen2(ext4_file *f, const char *path, int flags);
305 /**@brief File close function.
306 * @param f file handle
307 * @return standard error code*/
308 int ext4_fclose(ext4_file *f);
310 /**@brief Fill in the ext4_inode buffer.
311 * @param path fetch inode data of the path
312 * @param ret_ino the inode id of the path
313 * @param ext4_inode buffer
314 * @return standard error code*/
315 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
316 struct ext4_inode *inode);
318 /**@brief File truncate function.
319 * @param f file handle
320 * @param new file size
321 * @return standard error code*/
322 int ext4_ftruncate(ext4_file *f, uint64_t size);
324 /**@brief Read data from file.
325 * @param f file handle
326 * @param buf output buffer
327 * @param size bytes to read
328 * @param rcnt bytes read (may be NULL)
329 * @return standard error code*/
330 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
332 /**@brief Write data to file.
333 * @param f file handle
334 * @param buf data to write
335 * @param size write length
336 * @param wcnt bytes written (may be NULL)
337 * @return standard error code*/
338 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
340 /**@brief File seek operation.
341 * @param f file handle
342 * @param offset offset to seek
343 * @param origin seek type:
347 * @return standard error code*/
348 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
350 /**@brief Get file position.
351 * @param f file handle
352 * @return actual file position */
353 uint64_t ext4_ftell(ext4_file *f);
355 /**@brief Get file size.
356 * @param f file handle
357 * @return file size */
358 uint64_t ext4_fsize(ext4_file *f);
360 /**@brief Change file/directory/link mode bits
361 * @param path to file/dir/link
362 * @param mode new mode bits (for example 0777)
363 * @return standard error code*/
364 int ext4_mode_set(const char *path, uint32_t mode);
367 /**@brief Get file/directory/link mode bits
368 * @param path to file/dir/link
369 * @param mode new mode bits (for example 0777)
370 * @return standard error code*/
371 int ext4_mode_get(const char *path, uint32_t *mode);
373 /**@brief Change file owner and group
374 * @param path to file/dir/link
376 * @param gid group id
377 * @return standard error code*/
378 int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid);
380 /**@brief Get file/directory/link owner and group
381 * @param path to file/dir/link
383 * @param gid group id
384 * @return standard error code*/
385 int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid);
387 /**@brief Set file/directory/link access time
388 * @param path to file/dir/link
389 * @param atime access timestamp
390 * @return standard error code*/
391 int ext4_atime_set(const char *path, uint32_t atime);
393 /**@brief Set file/directory/link modify time
394 * @param path to file/dir/link
395 * @param mtime modify timestamp
396 * @return standard error code*/
397 int ext4_mtime_set(const char *path, uint32_t mtime);
399 /**@brief Set file/directory/link change time
400 * @param path to file/dir/link
401 * @param ctime change timestamp
402 * @return standard error code*/
403 int ext4_ctime_set(const char *path, uint32_t ctime);
405 /**@brief Get file/directory/link access time
406 * @param path to file/dir/link
407 * @param atime access timestamp
408 * @return standard error code*/
409 int ext4_atime_get(const char *path, uint32_t *atime);
411 /**@brief Get file/directory/link modify time
412 * @param path to file/dir/link
413 * @param mtime modify timestamp
414 * @return standard error code*/
415 int ext4_mtime_get(const char *path, uint32_t *mtime);
417 /**@brief Get file/directory/link change time
418 * @param path to file/dir/link
419 * @param ctime change timestamp
420 * @return standard error code*/
421 int ext4_ctime_get(const char *path, uint32_t *ctime);
423 /**@brief Create symbolic link
424 * @param target destination path
425 * @param path source entry
426 * @return standard error code*/
427 int ext4_fsymlink(const char *target, const char *path);
429 /**@brief Create special file
430 * @param path path to new file
431 * @param filetype The filetype of the new special file
432 * (that must not be regular file, directory, or unknown type)
433 * @param dev if filetype is char device or block device,
434 * the device number will become the payload in the inode
435 * @return standard error code*/
436 int ext4_mknod(const char *path, int filetype, uint32_t dev);
438 /**@brief Read symbolic link payload
439 * @param path to symlink
440 * @param buf output buffer
441 * @param bufsize output buffer max size
442 * @param rcnt bytes read
443 * @return standard error code*/
444 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
446 /**@brief Set extended attribute
447 * @param path to file/directory
448 * @param name name of the entry to add
449 * @param name_len length of @name in bytes
450 * @param data data of the entry to add
451 * @param data_size size of data to add
452 * @param replace this boolean is deprecated.
453 * @return standard error code*/
454 int ext4_setxattr(const char *path, const char *name, size_t name_len,
455 const void *data, size_t data_size, bool replace);
457 /**@brief Get extended attribute
458 * @param path to file/directory
459 * @param name name of the entry to get
460 * @param name_len length of @name in bytes
461 * @param data data of the entry to get
462 * @param data_size size of data to get
463 * @return standard error code*/
464 int ext4_getxattr(const char *path, const char *name, size_t name_len,
465 void *buf, size_t buf_size, size_t *data_size);
467 /**@brief List extended attributes
468 * @param path to file/directory
469 * @param list list to hold the name of entries
470 * @param size size of @list in bytes
471 * @param ret_size used bytes of @list
472 * @return standard error code*/
473 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
475 /**@brief Remove extended attribute
476 * @param path to file/directory
477 * @param name name of the entry to remove
478 * @param name_len length of @name in bytes
479 * @return standard error code*/
480 int ext4_removexattr(const char *path, const char *name, size_t name_len);
483 /*********************************DIRECTORY OPERATION***********************/
485 /**@brief Recursive directory remove.
486 * @param path directory path to remove
487 * @return standard error code*/
488 int ext4_dir_rm(const char *path);
490 /**@brief Rename/move directory
492 * @param new_path destination
493 * @return standard error code */
494 int ext4_dir_mv(const char *path, const char *new_path);
496 /**@brief Create new directory.
497 * @param name new directory name
498 * @return standard error code*/
499 int ext4_dir_mk(const char *path);
501 /**@brief Directory open.
502 * @param d directory handle
503 * @param path directory path
504 * @return standard error code*/
505 int ext4_dir_open(ext4_dir *d, const char *path);
507 /**@brief Directory close.
508 * @param d directory handle
509 * @return standard error code*/
510 int ext4_dir_close(ext4_dir *d);
512 /**@brief Return next directory entry.
513 * @param d directory handle
515 * @return directory entry id (NULL if no entry)*/
516 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
518 /**@brief Rewine directory entry offset.
519 * @param d directory handle*/
520 void ext4_dir_entry_rewind(ext4_dir *d);