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 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);
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
126 * - /my_second_partition/
127 * @param read_only mount as read-only mode.
129 * @return standard error code */
130 int ext4_mount(const char *dev_name,
131 const char *mount_point,
134 /**@brief Umount operation.
135 * @param mount_point mount name
136 * @return standard error code */
137 int ext4_umount(const char *mount_point);
139 /**@brief Start journaling. Journaling start/stop functions are transparent
140 * and might be used on filesystems without journaling support.
142 * ext4_mount("sda1", "/");
143 * ext4_journal_start("/");
145 * //File operations here...
147 * ext4_journal_stop("/");
149 * @param mount_point mount name
150 * @return standard error code */
151 int ext4_journal_start(const char *mount_point);
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);
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);
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;
173 uint32_t block_group_count;
174 uint32_t blocks_per_group;
175 uint32_t inodes_per_group;
177 char volume_name[16];
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);
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);
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);
200 /**@brief Enable/disable write back cache mode.
201 * @warning Default model of cache is write trough. It means that when You do:
205 * < --- data is flushed to physical drive
208 * ext4_cache_write_back(..., 1);
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).
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
222 * ext4_cache_write_back(..., 1);
223 * ext4_cache_write_back(..., 1);
225 * ext4_cache_write_back(..., 0);
226 * ext4_cache_write_back(..., 0);
228 * Write back mode is useful when you want to create a lot of empty
231 * @param path mount point path
232 * @param on enable/disable
234 * @return standard error code */
235 int ext4_cache_write_back(const char *path, bool on);
238 /**@brief Force cache flush.
240 * @param path mount point path
242 * @return standard error code */
243 int ext4_cache_flush(const char *path);
245 /********************************FILE OPERATIONS*****************************/
247 /**@brief Remove file by path.
248 * @param path path to file
249 * @return standard error code */
250 int ext4_fremove(const char *path);
252 /**@brief create a hardlink for a file.
253 * @param path path to file
254 * @param hardlink_path path of hardlink
255 * @return standard error code */
256 int ext4_flink(const char *path, const char *hardlink_path);
258 /**@brief Rename file
260 * @param new_path destination
261 * @return standard error code */
262 int ext4_frename(const char *path, const char *new_path);
264 /**@brief File open function.
265 * @param path filename (has to start from mount point)
266 * /my_partition/my_file
267 * @param flags open file flags
268 * |---------------------------------------------------------------|
269 * | r or rb O_RDONLY |
270 * |---------------------------------------------------------------|
271 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
272 * |---------------------------------------------------------------|
273 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
274 * |---------------------------------------------------------------|
275 * | r+ or rb+ or r+b O_RDWR |
276 * |---------------------------------------------------------------|
277 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
278 * |---------------------------------------------------------------|
279 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
280 * |---------------------------------------------------------------|
282 * @return standard error code*/
283 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
285 /**@brief Alternate file open function.
286 * @param filename, (has to start from mount point)
287 * /my_partition/my_file
288 * @param flags open file flags
289 * @return standard error code*/
290 int ext4_fopen2(ext4_file *f, const char *path, int flags);
292 /**@brief File close function.
293 * @param f file handle
294 * @return standard error code*/
295 int ext4_fclose(ext4_file *f);
297 /**@brief Fill in the ext4_inode buffer.
298 * @param path fetch inode data of the path
299 * @param ret_ino the inode id of the path
300 * @param ext4_inode buffer
301 * @return standard error code*/
302 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
303 struct ext4_inode *inode);
305 /**@brief File truncate function.
306 * @param f file handle
307 * @param new file size
308 * @return standard error code*/
309 int ext4_ftruncate(ext4_file *f, uint64_t size);
311 /**@brief Read data from file.
312 * @param f file handle
313 * @param buf output buffer
314 * @param size bytes to read
315 * @param rcnt bytes read (may be NULL)
316 * @return standard error code*/
317 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
319 /**@brief Write data to file.
320 * @param f file handle
321 * @param buf data to write
322 * @param size write length
323 * @param wcnt bytes written (may be NULL)
324 * @return standard error code*/
325 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
327 /**@brief File seek operation.
328 * @param f file handle
329 * @param offset offset to seek
330 * @param origin seek type:
334 * @return standard error code*/
335 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
337 /**@brief Get file position.
338 * @param f file handle
339 * @return actual file position */
340 uint64_t ext4_ftell(ext4_file *f);
342 /**@brief Get file size.
343 * @param f file handle
344 * @return file size */
345 uint64_t ext4_fsize(ext4_file *f);
347 /**@brief Change file/directory/link mode bits
348 * @param path to file/dir/link
349 * @param mode new mode bits (for example 0777)
350 * @return standard error code*/
351 int ext4_chmod(const char *path, uint32_t mode);
353 /**@brief Change file owner and group
354 * @param path to file/dir/link
356 * @param gid group id
357 * @return standard error code*/
358 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
360 /**@brief Set file access time
361 * @param path to file/dir/link
362 * @param atime access timestamp
363 * @return standard error code*/
364 int ext4_file_set_atime(const char *path, uint32_t atime);
366 /**@brief Set file modify time
367 * @param path to file/dir/link
368 * @param mtime modify timestamp
369 * @return standard error code*/
370 int ext4_file_set_mtime(const char *path, uint32_t mtime);
372 /**@brief Set file change time
373 * @param path to file/dir/link
374 * @param ctime change timestamp
375 * @return standard error code*/
376 int ext4_file_set_ctime(const char *path, uint32_t ctime);
378 /**@brief Create symbolic link
379 * @param target destination path
380 * @param path source entry
381 * @return standard error code*/
382 int ext4_fsymlink(const char *target, const char *path);
384 /**@brief Create special file
385 * @param path path to new file
386 * @param filetype The filetype of the new special file
387 * (that must not be regular file, directory, or unknown type)
388 * @param dev if filetype is char device or block device,
389 * the device number will become the payload in the inode
390 * @return standard error code*/
391 int ext4_mknod(const char *path, int filetype, uint32_t dev);
393 /**@brief Read symbolic link payload
394 * @param path to symlink
395 * @param buf output buffer
396 * @param bufsize output buffer max size
397 * @param rcnt bytes read
398 * @return standard error code*/
399 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
401 /**@brief Set extended attribute
402 * @param path to file/directory
403 * @param name name of the entry to add
404 * @param name_len length of @name in bytes
405 * @param data data of the entry to add
406 * @param data_size size of data to add
407 * @param replace this boolean is deprecated.
408 * @return standard error code*/
409 int ext4_setxattr(const char *path, const char *name, size_t name_len,
410 const void *data, size_t data_size, bool replace);
412 /**@brief Get extended attribute
413 * @param path to file/directory
414 * @param name name of the entry to get
415 * @param name_len length of @name in bytes
416 * @param data data of the entry to get
417 * @param data_size size of data to get
418 * @return standard error code*/
419 int ext4_getxattr(const char *path, const char *name, size_t name_len,
420 void *buf, size_t buf_size, size_t *data_size);
422 /**@brief List extended attributes
423 * @param path to file/directory
424 * @param list list to hold the name of entries
425 * @param size size of @list in bytes
426 * @param ret_size used bytes of @list
427 * @return standard error code*/
428 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
430 /**@brief Remove extended attribute
431 * @param path to file/directory
432 * @param name name of the entry to remove
433 * @param name_len length of @name in bytes
434 * @return standard error code*/
435 int ext4_removexattr(const char *path, const char *name, size_t name_len);
438 /*********************************DIRECTORY OPERATION***********************/
440 /**@brief Recursive directory remove.
441 * @param path directory path to remove
442 * @return standard error code*/
443 int ext4_dir_rm(const char *path);
445 /**@brief Rename/move directory
447 * @param new_path destination
448 * @return standard error code */
449 int ext4_dir_mv(const char *path, const char *new_path);
451 /**@brief Create new directory.
452 * @param name new directory name
453 * @return standard error code*/
454 int ext4_dir_mk(const char *path);
456 /**@brief Directory open.
457 * @param d directory handle
458 * @param path directory path
459 * @return standard error code*/
460 int ext4_dir_open(ext4_dir *d, const char *path);
462 /**@brief Directory close.
463 * @param d directory handle
464 * @return standard error code*/
465 int ext4_dir_close(ext4_dir *d);
467 /**@brief Return next directory entry.
468 * @param d directory handle
470 * @return directory entry id (NULL if no entry)*/
471 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
473 /**@brief Rewine directory entry offset.
474 * @param d directory handle*/
475 void ext4_dir_entry_rewind(ext4_dir *d);