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.
41 #include "ext4_config.h"
42 #include "ext4_types.h"
43 #include "ext4_blockdev.h"
47 /********************************FILE OPEN FLAGS*****************************/
74 #define O_APPEND 02000
77 /********************************FILE SEEK FLAGS*****************************/
91 /********************************OS LOCK INFERFACE***************************/
93 /**@brief OS dependent lock interface.*/
96 /**@brief Lock access to mount point*/
99 /**@brief Unlock access to mount point*/
100 void (*unlock)(void);
103 /********************************FILE DESCRIPTOR*****************************/
105 /**@brief File descriptor*/
106 typedef struct ext4_file {
108 /**@brief Mount point handle.*/
109 struct ext4_mountpoint *mp;
111 /**@brief File inode id*/
114 /**@brief Open flags.*/
117 /**@brief File size.*/
120 /**@brief File position*/
124 /*****************************DIRECTORY DESCRIPTOR***************************/
125 /**@brief Directory entry types. Copy from ext4_types.h*/
126 enum { EXT4_DIRENTRY_UNKNOWN = 0,
127 EXT4_DIRENTRY_REG_FILE,
129 EXT4_DIRENTRY_CHRDEV,
130 EXT4_DIRENTRY_BLKDEV,
133 EXT4_DIRENTRY_SYMLINK };
135 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
138 uint16_t entry_length;
145 /**@brief File descriptor*/
147 /**@brief Current directory entry.*/
149 /**@brief Next entry offset*/
153 /********************************MOUNT OPERATIONS****************************/
155 /**@brief Register a block device to a name.
156 * @warning Block device has to be filled by
157 * @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created
158 * @ref EXT4_BCACHE_STATIC_INSTANCE.
159 * Block cache may by created automatically when bc parameter is 0.
160 * @param bd block device
161 * @param bd block device cache (0 = automatic cache mode)
162 * @param dev_name register name
163 * @param standard error code*/
164 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
165 const char *dev_name);
167 /**@brief Mount a block device with EXT4 partition to the mount point.
168 * @param dev_name block device name (@ref ext4_device_register)
169 * @param mount_point mount point, for example
172 * - /my_second_partition/
174 * @return standard error code */
175 int ext4_mount(const char *dev_name, const char *mount_point);
177 /**@brief Umount operation.
178 * @param mount_point mount name
179 * @return standard error code */
180 int ext4_umount(const char *mount_point);
182 /**@brief Some of the filesystem stats.*/
183 struct ext4_mount_stats {
184 uint32_t inodes_count;
185 uint32_t free_inodes_count;
186 uint64_t blocks_count;
187 uint64_t free_blocks_count;
190 uint32_t block_group_count;
191 uint32_t blocks_per_group;
192 uint32_t inodes_per_group;
194 char volume_name[16];
197 /**@brief Get file system params.
198 * @param mount_point mount path
199 * @param stats ext fs stats
200 * @return standard error code */
201 int ext4_mount_point_stats(const char *mount_point,
202 struct ext4_mount_stats *stats);
204 /**@brief Setup OS lock routines.
205 * @param mount_point mount path
206 * @param locks - lock and unlock functions
207 * @return standard error code */
208 int ext4_mount_setup_locks(const char *mount_point,
209 const struct ext4_lock *locks);
211 /**@brief Acquire the filesystem superblock pointer of a mp.
212 * @param mount_point mount path
213 * @param superblock pointer
214 * @return standard error code */
215 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
217 /**@brief Enable/disable write back cache mode.
218 * @warning Default model of cache is write trough. It means that when You do:
222 * < --- data is flushed to physical drive
225 * ext4_cache_write_back(..., 1);
228 * < --- data is NOT flushed to physical drive
229 * ext4_cache_write_back(..., 0);
230 * < --- when write back mode is disabled all
231 * cache data will be flushed
232 * To enable write back mode permanently just call this function
233 * once after ext4_mount (and disable before ext4_umount).
235 * Some of the function use write back cache mode internally.
236 * If you enable write back mode twice you have to disable it twice
239 * ext4_cache_write_back(..., 1);
240 * ext4_cache_write_back(..., 1);
242 * ext4_cache_write_back(..., 0);
243 * ext4_cache_write_back(..., 0);
245 * Write back mode is useful when you want to create a lot of empty
248 * @param path mount point path
249 * @param on enable/disable
251 * @return standard error code */
252 int ext4_cache_write_back(const char *path, bool on);
254 /********************************FILE OPERATIONS*****************************/
256 /**@brief Remove file by path.
257 * @param path path to file
258 * @return standard error code */
259 int ext4_fremove(const char *path);
261 /**@brief create a hardlink for a file.
262 * @param path path to file
263 * @param hardlink_path path of hardlink
264 * @return standard error code */
265 int ext4_flink(const char *path, const char *hardlink_path);
267 /**@brief Rename file
269 * @param new_path destination
270 * @return standard error code */
271 int ext4_frename(const char *path, const char *new_path);
273 /**@brief File open function.
274 * @param filename, (has to start from mount point)
275 * /my_partition/my_file
276 * @param flags open file flags
277 * |---------------------------------------------------------------|
278 * | r or rb O_RDONLY |
279 * |---------------------------------------------------------------|
280 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
281 * |---------------------------------------------------------------|
282 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
283 * |---------------------------------------------------------------|
284 * | r+ or rb+ or r+b O_RDWR |
285 * |---------------------------------------------------------------|
286 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
287 * |---------------------------------------------------------------|
288 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
289 * |---------------------------------------------------------------|
291 * @return standard error code*/
292 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
294 /**@brief Alternate file open function.
295 * @param filename, (has to start from mount point)
296 * /my_partition/my_file
297 * @param flags open file flags
298 * @return standard error code*/
299 int ext4_fopen2(ext4_file *f, const char *path, int flags, bool file_expect);
301 /**@brief File close function.
302 * @param f file handle
303 * @return standard error code*/
304 int ext4_fclose(ext4_file *f);
306 /**@brief Fill in the ext4_inode buffer.
309 * @param ext4_inode buffer
310 * @return standard error code*/
311 int ext4_fill_raw_inode(const char *mount_point, uint32_t ino,
312 struct ext4_inode *inode);
314 /**@brief File truncate function.
315 * @param f file handle
316 * @param new file size
317 * @return standard error code*/
318 int ext4_ftruncate(ext4_file *f, uint64_t size);
320 /**@brief Read data from file.
321 * @param f file handle
322 * @param buf output buffer
323 * @param size bytes to read
324 * @param rcnt bytes read (may be NULL)
325 * @return standard error code*/
326 int ext4_fread(ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);
328 /**@brief Write data to file.
329 * @param f file handle
330 * @param buf data to write
331 * @param size write length
332 * @param wcnt bytes written (may be NULL)
333 * @return standard error code*/
334 int ext4_fwrite(ext4_file *f, const void *buf, uint32_t size, uint32_t *wcnt);
336 /**@brief File seek operation.
337 * @param f file handle
338 * @param offset offset to seek
339 * @param origin seek type:
343 * @return standard error code*/
344 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
346 /**@brief Get file position.
347 * @param f file handle
348 * @return actual file position */
349 uint64_t ext4_ftell(ext4_file *f);
351 /**@brief Get file size.
352 * @param f file handle
353 * @return file size */
354 uint64_t ext4_fsize(ext4_file *f);
356 int ext4_fchmod(ext4_file *f, uint32_t mode);
357 int ext4_fchown(ext4_file *f, uint32_t uid, uint32_t gid);
358 int ext4_file_set_atime(ext4_file *f, uint32_t atime);
359 int ext4_file_set_mtime(ext4_file *f, uint32_t mtime);
360 int ext4_file_set_ctime(ext4_file *f, uint32_t ctime);
362 /*********************************DIRECTORY OPERATION***********************/
364 /**@brief Recursive directory remove.
365 * @param path directory path to remove
366 * @return standard error code*/
367 int ext4_dir_rm(const char *path);
369 /**@brief Create new directory.
370 * @param name new directory name
371 * @return standard error code*/
372 int ext4_dir_mk(const char *path);
374 /**@brief Directory open.
375 * @param d directory handle
376 * @param path directory path
377 * @return standard error code*/
378 int ext4_dir_open(ext4_dir *d, const char *path);
380 /**@brief Directory close.
381 * @param d directory handle
382 * @return standard error code*/
383 int ext4_dir_close(ext4_dir *d);
385 /**@brief Return next directory entry.
386 * @param d directory handle
388 * @return directory entry id (NULL if no entry)*/
389 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);