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_errno.h"
50 #include "ext4_oflags.h"
51 #include "ext4_types.h"
52 #include "ext4_blockdev.h"
54 /********************************OS LOCK INFERFACE***************************/
56 /**@brief OS dependent lock interface.*/
59 /**@brief Lock access to mount point*/
62 /**@brief Unlock access to mount point*/
66 /********************************FILE DESCRIPTOR*****************************/
68 /**@brief File descriptor*/
69 typedef struct ext4_file {
71 /**@brief Mount point handle.*/
72 struct ext4_mountpoint *mp;
74 /**@brief File inode id*/
77 /**@brief Open flags.*/
80 /**@brief File size.*/
83 /**@brief File position*/
87 /*****************************DIRECTORY DESCRIPTOR***************************/
89 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
90 typedef struct ext4_direntry {
92 uint16_t entry_length;
98 typedef struct ext4_dir {
99 /**@brief File descriptor*/
101 /**@brief Current directory entry.*/
103 /**@brief Next entry offset*/
107 /********************************MOUNT OPERATIONS****************************/
109 /**@brief Register a block device to a name.
110 * @warning Block device has to be filled by
111 * Block cache may by created automatically when bc parameter is NULL.
112 * @param bd block device
113 * @param bd block device cache
114 * @param dev_name register name
115 * @param standard error code*/
116 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
117 const char *dev_name);
119 /**@brief Mount a block device with EXT4 partition to the mount point.
120 * @param dev_name block device name (@ref ext4_device_register)
121 * @param mount_point mount point, for example
124 * - /my_second_partition/
126 * @return standard error code */
127 int ext4_mount(const char *dev_name, const char *mount_point);
129 /**@brief Umount operation.
130 * @param mount_point mount name
131 * @return standard error code */
132 int ext4_umount(const char *mount_point);
134 /**@brief Start journaling. Journaling start/stop functions are transparent
135 * and might be used on filesystems without journaling support.
137 * ext4_mount("sda1", "/");
138 * ext4_journal_start("/");
140 * //File operations here...
142 * ext4_journal_stop("/");
144 * @param mount_point mount name
145 * @return standard error code */
146 int ext4_journal_start(const char *mount_point);
148 /**@brief Stop journaling. Journaling start/stop functions are transparent
149 * and might be used on filesystems without journaling support.
150 * @param mount_point mount name
151 * @return standard error code */
152 int ext4_journal_stop(const char *mount_point);
154 /**@brief Journal recovery.
155 * @param mount_point mount point
156 * @warning Must be called after @ref ext4_mount
157 * @return standard error code */
158 int ext4_recover(const char *mount_point);
160 /**@brief Some of the filesystem stats.*/
161 struct ext4_mount_stats {
162 uint32_t inodes_count;
163 uint32_t free_inodes_count;
164 uint64_t blocks_count;
165 uint64_t free_blocks_count;
168 uint32_t block_group_count;
169 uint32_t blocks_per_group;
170 uint32_t inodes_per_group;
172 char volume_name[16];
175 /**@brief Get file system params.
176 * @param mount_point mount path
177 * @param stats ext fs stats
178 * @return standard error code */
179 int ext4_mount_point_stats(const char *mount_point,
180 struct ext4_mount_stats *stats);
182 /**@brief Setup OS lock routines.
183 * @param mount_point mount path
184 * @param locks - lock and unlock functions
185 * @return standard error code */
186 int ext4_mount_setup_locks(const char *mount_point,
187 const struct ext4_lock *locks);
189 /**@brief Acquire the filesystem superblock pointer of a mp.
190 * @param mount_point mount path
191 * @param superblock pointer
192 * @return standard error code */
193 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
195 /**@brief Enable/disable write back cache mode.
196 * @warning Default model of cache is write trough. It means that when You do:
200 * < --- data is flushed to physical drive
203 * ext4_cache_write_back(..., 1);
206 * < --- data is NOT flushed to physical drive
207 * ext4_cache_write_back(..., 0);
208 * < --- when write back mode is disabled all
209 * cache data will be flushed
210 * To enable write back mode permanently just call this function
211 * once after ext4_mount (and disable before ext4_umount).
213 * Some of the function use write back cache mode internally.
214 * If you enable write back mode twice you have to disable it twice
217 * ext4_cache_write_back(..., 1);
218 * ext4_cache_write_back(..., 1);
220 * ext4_cache_write_back(..., 0);
221 * ext4_cache_write_back(..., 0);
223 * Write back mode is useful when you want to create a lot of empty
226 * @param path mount point path
227 * @param on enable/disable
229 * @return standard error code */
230 int ext4_cache_write_back(const char *path, bool on);
232 /********************************FILE OPERATIONS*****************************/
234 /**@brief Remove file by path.
235 * @param path path to file
236 * @return standard error code */
237 int ext4_fremove(const char *path);
239 /**@brief create a hardlink for a file.
240 * @param path path to file
241 * @param hardlink_path path of hardlink
242 * @return standard error code */
243 int ext4_flink(const char *path, const char *hardlink_path);
245 /**@brief Rename file
247 * @param new_path destination
248 * @return standard error code */
249 int ext4_frename(const char *path, const char *new_path);
251 /**@brief File open function.
252 * @param path filename (has to start from mount point)
253 * /my_partition/my_file
254 * @param flags open file flags
255 * |---------------------------------------------------------------|
256 * | r or rb O_RDONLY |
257 * |---------------------------------------------------------------|
258 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
259 * |---------------------------------------------------------------|
260 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
261 * |---------------------------------------------------------------|
262 * | r+ or rb+ or r+b O_RDWR |
263 * |---------------------------------------------------------------|
264 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
265 * |---------------------------------------------------------------|
266 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
267 * |---------------------------------------------------------------|
269 * @return standard error code*/
270 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
272 /**@brief Alternate file open function.
273 * @param filename, (has to start from mount point)
274 * /my_partition/my_file
275 * @param flags open file flags
276 * @return standard error code*/
277 int ext4_fopen2(ext4_file *f, const char *path, int flags);
279 /**@brief File close function.
280 * @param f file handle
281 * @return standard error code*/
282 int ext4_fclose(ext4_file *f);
284 /**@brief Fill in the ext4_inode buffer.
285 * @param path fetch inode data of the path
286 * @param ret_ino the inode id of the path
287 * @param ext4_inode buffer
288 * @return standard error code*/
289 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
290 struct ext4_inode *inode);
292 /**@brief File truncate function.
293 * @param f file handle
294 * @param new file size
295 * @return standard error code*/
296 int ext4_ftruncate(ext4_file *f, uint64_t size);
298 /**@brief Read data from file.
299 * @param f file handle
300 * @param buf output buffer
301 * @param size bytes to read
302 * @param rcnt bytes read (may be NULL)
303 * @return standard error code*/
304 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
306 /**@brief Write data to file.
307 * @param f file handle
308 * @param buf data to write
309 * @param size write length
310 * @param wcnt bytes written (may be NULL)
311 * @return standard error code*/
312 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
314 /**@brief File seek operation.
315 * @param f file handle
316 * @param offset offset to seek
317 * @param origin seek type:
321 * @return standard error code*/
322 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
324 /**@brief Get file position.
325 * @param f file handle
326 * @return actual file position */
327 uint64_t ext4_ftell(ext4_file *f);
329 /**@brief Get file size.
330 * @param f file handle
331 * @return file size */
332 uint64_t ext4_fsize(ext4_file *f);
334 /**@brief Change file/directory/link mode bits
335 * @param path to file/dir/link
336 * @param mode new mode bits (for example 0777)
337 * @return standard error code*/
338 int ext4_chmod(const char *path, uint32_t mode);
340 /**@brief Change file owner and group
341 * @param path to file/dir/link
343 * @param gid group id
344 * @return standard error code*/
345 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
347 /**@brief Set file access time
348 * @param path to file/dir/link
349 * @param atime access timestamp
350 * @return standard error code*/
351 int ext4_file_set_atime(const char *path, uint32_t atime);
353 /**@brief Set file modify time
354 * @param path to file/dir/link
355 * @param mtime modify timestamp
356 * @return standard error code*/
357 int ext4_file_set_mtime(const char *path, uint32_t mtime);
359 /**@brief Set file change time
360 * @param path to file/dir/link
361 * @param ctime change timestamp
362 * @return standard error code*/
363 int ext4_file_set_ctime(const char *path, uint32_t ctime);
365 /**@brief Create symbolic link
366 * @param target destination path
367 * @param path source entry
368 * @return standard error code*/
369 int ext4_fsymlink(const char *target, const char *path);
372 /**@brief Read symbolic link payload
373 * @param path to symlink
374 * @param buf output buffer
375 * @param bufsize output buffer max size
376 * @param rcnt bytes read
377 * @return standard error code*/
378 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
380 /**@brief Set extended attribute
381 * @param path to file/directory
382 * @param name name of the entry to add
383 * @param name_len length of @name in bytes
384 * @param data data of the entry to add
385 * @param data_size size of data to add
386 * @param replace whether existing entries should be replaced
387 * @return standard error code*/
388 int ext4_setxattr(const char *path, const char *name, size_t name_len,
389 const void *data, size_t data_size, bool replace);
391 /**@brief Get extended attribute
392 * @param path to file/directory
393 * @param name name of the entry to get
394 * @param name_len length of @name in bytes
395 * @param data data of the entry to get
396 * @param data_size size of data to get
397 * @return standard error code*/
398 int ext4_getxattr(const char *path, const char *name, size_t name_len,
399 void *buf, size_t buf_size, size_t *data_size);
401 /**@brief List extended attributes
402 * @param path to file/directory
403 * @param list list to hold the name of entries
404 * @param size size of @list in bytes
405 * @param ret_size used bytes of @list
406 * @return standard error code*/
407 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
409 /**@brief Remove extended attribute
410 * @param path to file/directory
411 * @param name name of the entry to remove
412 * @param name_len length of @name in bytes
413 * @return standard error code*/
414 int ext4_removexattr(const char *path, const char *name, size_t name_len);
417 /*********************************DIRECTORY OPERATION***********************/
419 /**@brief Recursive directory remove.
420 * @param path directory path to remove
421 * @return standard error code*/
422 int ext4_dir_rm(const char *path);
424 /**@brief Create new directory.
425 * @param name new directory name
426 * @return standard error code*/
427 int ext4_dir_mk(const char *path);
429 /**@brief Directory open.
430 * @param d directory handle
431 * @param path directory path
432 * @return standard error code*/
433 int ext4_dir_open(ext4_dir *d, const char *path);
435 /**@brief Directory close.
436 * @param d directory handle
437 * @return standard error code*/
438 int ext4_dir_close(ext4_dir *d);
440 /**@brief Return next directory entry.
441 * @param d directory handle
443 * @return directory entry id (NULL if no entry)*/
444 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
446 /**@brief Rewine directory entry offset.
447 * @param d directory handle*/
448 void ext4_dir_entry_rewind(ext4_dir *d);