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);
237 /********************************FILE OPERATIONS*****************************/
239 /**@brief Remove file by path.
240 * @param path path to file
241 * @return standard error code */
242 int ext4_fremove(const char *path);
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);
250 /**@brief Rename file
252 * @param new_path destination
253 * @return standard error code */
254 int ext4_frename(const char *path, const char *new_path);
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 * |---------------------------------------------------------------|
274 * @return standard error code*/
275 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
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);
284 /**@brief File close function.
285 * @param f file handle
286 * @return standard error code*/
287 int ext4_fclose(ext4_file *f);
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);
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);
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);
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);
319 /**@brief File seek operation.
320 * @param f file handle
321 * @param offset offset to seek
322 * @param origin seek type:
326 * @return standard error code*/
327 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
329 /**@brief Get file position.
330 * @param f file handle
331 * @return actual file position */
332 uint64_t ext4_ftell(ext4_file *f);
334 /**@brief Get file size.
335 * @param f file handle
336 * @return file size */
337 uint64_t ext4_fsize(ext4_file *f);
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);
345 /**@brief Change file owner and group
346 * @param path to file/dir/link
348 * @param gid group id
349 * @return standard error code*/
350 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
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);
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);
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);
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);
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);
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 whether existing entries should be replaced
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);
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);
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);
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);
422 /*********************************DIRECTORY OPERATION***********************/
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);
429 /**@brief Rename/move directory
431 * @param new_path destination
432 * @return standard error code */
433 int ext4_dir_mv(const char *path, const char *new_path);
435 /**@brief Create new directory.
436 * @param name new directory name
437 * @return standard error code*/
438 int ext4_dir_mk(const char *path);
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);
446 /**@brief Directory close.
447 * @param d directory handle
448 * @return standard error code*/
449 int ext4_dir_close(ext4_dir *d);
451 /**@brief Return next directory entry.
452 * @param d directory handle
454 * @return directory entry id (NULL if no entry)*/
455 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
457 /**@brief Rewine directory entry offset.
458 * @param d directory handle*/
459 void ext4_dir_entry_rewind(ext4_dir *d);