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 /********************************FILE OPEN FLAGS*****************************/
50 #if CONFIG_HAVE_OWN_OFLAGS
77 #define O_APPEND 02000
80 /********************************FILE SEEK FLAGS*****************************/
99 #include "ext4_config.h"
100 #include "ext4_types.h"
101 #include "ext4_blockdev.h"
103 /********************************OS LOCK INFERFACE***************************/
105 /**@brief OS dependent lock interface.*/
108 /**@brief Lock access to mount point*/
111 /**@brief Unlock access to mount point*/
112 void (*unlock)(void);
115 /********************************FILE DESCRIPTOR*****************************/
117 /**@brief File descriptor*/
118 typedef struct ext4_file {
120 /**@brief Mount point handle.*/
121 struct ext4_mountpoint *mp;
123 /**@brief File inode id*/
126 /**@brief Open flags.*/
129 /**@brief File size.*/
132 /**@brief File position*/
136 /*****************************DIRECTORY DESCRIPTOR***************************/
138 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
141 uint16_t entry_length;
148 /**@brief File descriptor*/
150 /**@brief Current directory entry.*/
152 /**@brief Next entry offset*/
156 /********************************MOUNT OPERATIONS****************************/
158 /**@brief Register a block device to a name.
159 * @warning Block device has to be filled by
160 * @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created
161 * @ref EXT4_BCACHE_STATIC_INSTANCE.
162 * Block cache may by created automatically when bc parameter is 0.
163 * @param bd block device
164 * @param bd block device cache (0 = automatic cache mode)
165 * @param dev_name register name
166 * @param standard error code*/
167 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
168 const char *dev_name);
170 /**@brief Mount a block device with EXT4 partition to the mount point.
171 * @param dev_name block device name (@ref ext4_device_register)
172 * @param mount_point mount point, for example
175 * - /my_second_partition/
177 * @return standard error code */
178 int ext4_mount(const char *dev_name, const char *mount_point);
180 /**@brief Umount operation.
181 * @param mount_point mount name
182 * @return standard error code */
183 int ext4_umount(const char *mount_point);
186 /**@brief Journal recovery.
187 * @param mount_point mount point
188 * @warning Must be called after @ref ext4_mount
189 * @return standard error code */
190 int ext4_recover(const char *mount_point);
192 /**@brief Some of the filesystem stats.*/
193 struct ext4_mount_stats {
194 uint32_t inodes_count;
195 uint32_t free_inodes_count;
196 uint64_t blocks_count;
197 uint64_t free_blocks_count;
200 uint32_t block_group_count;
201 uint32_t blocks_per_group;
202 uint32_t inodes_per_group;
204 char volume_name[16];
207 /**@brief Get file system params.
208 * @param mount_point mount path
209 * @param stats ext fs stats
210 * @return standard error code */
211 int ext4_mount_point_stats(const char *mount_point,
212 struct ext4_mount_stats *stats);
214 /**@brief Setup OS lock routines.
215 * @param mount_point mount path
216 * @param locks - lock and unlock functions
217 * @return standard error code */
218 int ext4_mount_setup_locks(const char *mount_point,
219 const struct ext4_lock *locks);
221 /**@brief Acquire the filesystem superblock pointer of a mp.
222 * @param mount_point mount path
223 * @param superblock pointer
224 * @return standard error code */
225 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
227 /**@brief Enable/disable write back cache mode.
228 * @warning Default model of cache is write trough. It means that when You do:
232 * < --- data is flushed to physical drive
235 * ext4_cache_write_back(..., 1);
238 * < --- data is NOT flushed to physical drive
239 * ext4_cache_write_back(..., 0);
240 * < --- when write back mode is disabled all
241 * cache data will be flushed
242 * To enable write back mode permanently just call this function
243 * once after ext4_mount (and disable before ext4_umount).
245 * Some of the function use write back cache mode internally.
246 * If you enable write back mode twice you have to disable it twice
249 * ext4_cache_write_back(..., 1);
250 * ext4_cache_write_back(..., 1);
252 * ext4_cache_write_back(..., 0);
253 * ext4_cache_write_back(..., 0);
255 * Write back mode is useful when you want to create a lot of empty
258 * @param path mount point path
259 * @param on enable/disable
261 * @return standard error code */
262 int ext4_cache_write_back(const char *path, bool on);
264 /********************************FILE OPERATIONS*****************************/
266 /**@brief Remove file by path.
267 * @param path path to file
268 * @return standard error code */
269 int ext4_fremove(const char *path);
271 /**@brief create a hardlink for a file.
272 * @param path path to file
273 * @param hardlink_path path of hardlink
274 * @return standard error code */
275 int ext4_flink(const char *path, const char *hardlink_path);
277 /**@brief Rename file
279 * @param new_path destination
280 * @return standard error code */
281 int ext4_frename(const char *path, const char *new_path);
283 /**@brief File open function.
284 * @param filename, (has to start from mount point)
285 * /my_partition/my_file
286 * @param flags open file flags
287 * |---------------------------------------------------------------|
288 * | r or rb O_RDONLY |
289 * |---------------------------------------------------------------|
290 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
291 * |---------------------------------------------------------------|
292 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
293 * |---------------------------------------------------------------|
294 * | r+ or rb+ or r+b O_RDWR |
295 * |---------------------------------------------------------------|
296 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
297 * |---------------------------------------------------------------|
298 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
299 * |---------------------------------------------------------------|
301 * @return standard error code*/
302 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
304 /**@brief Alternate file open function.
305 * @param filename, (has to start from mount point)
306 * /my_partition/my_file
307 * @param flags open file flags
308 * @return standard error code*/
309 int ext4_fopen2(ext4_file *f, const char *path, int flags);
311 /**@brief File close function.
312 * @param f file handle
313 * @return standard error code*/
314 int ext4_fclose(ext4_file *f);
316 /**@brief Fill in the ext4_inode buffer.
317 * @param path fetch inode data of the path
318 * @param ret_ino the inode id of the path
319 * @param ext4_inode buffer
320 * @return standard error code*/
321 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
322 struct ext4_inode *inode);
324 /**@brief File truncate function.
325 * @param f file handle
326 * @param new file size
327 * @return standard error code*/
328 int ext4_ftruncate(ext4_file *f, uint64_t size);
330 /**@brief Read data from file.
331 * @param f file handle
332 * @param buf output buffer
333 * @param size bytes to read
334 * @param rcnt bytes read (may be NULL)
335 * @return standard error code*/
336 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
338 /**@brief Write data to file.
339 * @param f file handle
340 * @param buf data to write
341 * @param size write length
342 * @param wcnt bytes written (may be NULL)
343 * @return standard error code*/
344 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
346 /**@brief File seek operation.
347 * @param f file handle
348 * @param offset offset to seek
349 * @param origin seek type:
353 * @return standard error code*/
354 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
356 /**@brief Get file position.
357 * @param f file handle
358 * @return actual file position */
359 uint64_t ext4_ftell(ext4_file *f);
361 /**@brief Get file size.
362 * @param f file handle
363 * @return file size */
364 uint64_t ext4_fsize(ext4_file *f);
366 int ext4_chmod(const char *path, uint32_t mode);
367 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
368 int ext4_file_set_atime(const char *path, uint32_t atime);
369 int ext4_file_set_mtime(const char *path, uint32_t mtime);
370 int ext4_file_set_ctime(const char *path, uint32_t ctime);
372 int ext4_fsymlink(const char *target, const char *path);
374 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
376 int ext4_setxattr(const char *path, const char *name, size_t name_len,
377 const void *data, size_t data_size, bool replace);
378 int ext4_getxattr(const char *path, const char *name, size_t name_len,
379 void *buf, size_t buf_size, size_t *data_size);
380 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
381 int ext4_removexattr(const char *path, const char *name, size_t name_len);
384 /*********************************DIRECTORY OPERATION***********************/
386 /**@brief Recursive directory remove.
387 * @param path directory path to remove
388 * @return standard error code*/
389 int ext4_dir_rm(const char *path);
391 /**@brief Create new directory.
392 * @param name new directory name
393 * @return standard error code*/
394 int ext4_dir_mk(const char *path);
396 /**@brief Directory open.
397 * @param d directory handle
398 * @param path directory path
399 * @return standard error code*/
400 int ext4_dir_open(ext4_dir *d, const char *path);
402 /**@brief Directory close.
403 * @param d directory handle
404 * @return standard error code*/
405 int ext4_dir_close(ext4_dir *d);
407 /**@brief Return next directory entry.
408 * @param d directory handle
410 * @return directory entry id (NULL if no entry)*/
411 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
413 /**@brief Rewine directory entry offset.
414 * @param d directory handle*/
415 void ext4_dir_entry_rewind(ext4_dir *d);