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"
48 /********************************FILE OPEN FLAGS*****************************/
75 #define O_APPEND 02000
78 /********************************FILE SEEK FLAGS*****************************/
92 /********************************OS LOCK INFERFACE***************************/
94 /**@brief OS dependent lock interface.*/
97 /**@brief Lock access to mount point*/
100 /**@brief Unlock access to mount point*/
101 void (*unlock)(void);
104 /********************************FILE DESCRIPTOR*****************************/
106 /**@brief File descriptor*/
107 typedef struct ext4_file {
109 /**@brief Mount point handle.*/
110 struct ext4_mountpoint *mp;
112 /**@brief File inode id*/
115 /**@brief Open flags.*/
118 /**@brief File size.*/
121 /**@brief File position*/
125 /*****************************DIRECTORY DESCRIPTOR***************************/
126 /**@brief Directory entry types. Copy from ext4_types.h*/
127 enum { EXT4_DIRENTRY_UNKNOWN = 0,
128 EXT4_DIRENTRY_REG_FILE,
130 EXT4_DIRENTRY_CHRDEV,
131 EXT4_DIRENTRY_BLKDEV,
134 EXT4_DIRENTRY_SYMLINK };
136 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
139 uint16_t entry_length;
146 /**@brief File descriptor*/
148 /**@brief Current directory entry.*/
150 /**@brief Next entry offset*/
154 /********************************MOUNT OPERATIONS****************************/
156 /**@brief Register a block device to a name.
157 * @warning Block device has to be filled by
158 * @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created
159 * @ref EXT4_BCACHE_STATIC_INSTANCE.
160 * Block cache may by created automatically when bc parameter is 0.
161 * @param bd block device
162 * @param bd block device cache (0 = automatic cache mode)
163 * @param dev_name register name
164 * @param standard error code*/
165 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
166 const char *dev_name);
168 /**@brief Mount a block device with EXT4 partition to the mount point.
169 * @param dev_name block device name (@ref ext4_device_register)
170 * @param mount_point mount point, for example
173 * - /my_second_partition/
175 * @return standard error code */
176 int ext4_mount(const char *dev_name, const char *mount_point);
178 /**@brief Umount operation.
179 * @param mount_point mount name
180 * @return standard error code */
181 int ext4_umount(const char *mount_point);
183 /**@brief Some of the filesystem stats.*/
184 struct ext4_mount_stats {
185 uint32_t inodes_count;
186 uint32_t free_inodes_count;
187 uint64_t blocks_count;
188 uint64_t free_blocks_count;
191 uint32_t block_group_count;
192 uint32_t blocks_per_group;
193 uint32_t inodes_per_group;
195 char volume_name[16];
198 /**@brief Get file system params.
199 * @param mount_point mount path
200 * @param stats ext fs stats
201 * @return standard error code */
202 int ext4_mount_point_stats(const char *mount_point,
203 struct ext4_mount_stats *stats);
205 /**@brief Setup OS lock routines.
206 * @param mount_point mount path
207 * @param locks - lock and unlock functions
208 * @return standard error code */
209 int ext4_mount_setup_locks(const char *mount_point,
210 const struct ext4_lock *locks);
212 /**@brief Acquire the filesystem superblock pointer of a mp.
213 * @param mount_point mount path
214 * @param superblock pointer
215 * @return standard error code */
216 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
218 /**@brief Enable/disable write back cache mode.
219 * @warning Default model of cache is write trough. It means that when You do:
223 * < --- data is flushed to physical drive
226 * ext4_cache_write_back(..., 1);
229 * < --- data is NOT flushed to physical drive
230 * ext4_cache_write_back(..., 0);
231 * < --- when write back mode is disabled all
232 * cache data will be flushed
233 * To enable write back mode permanently just call this function
234 * once after ext4_mount (and disable before ext4_umount).
236 * Some of the function use write back cache mode internally.
237 * If you enable write back mode twice you have to disable it twice
240 * ext4_cache_write_back(..., 1);
241 * ext4_cache_write_back(..., 1);
243 * ext4_cache_write_back(..., 0);
244 * ext4_cache_write_back(..., 0);
246 * Write back mode is useful when you want to create a lot of empty
249 * @param path mount point path
250 * @param on enable/disable
252 * @return standard error code */
253 int ext4_cache_write_back(const char *path, bool on);
255 /********************************FILE OPERATIONS*****************************/
257 /**@brief Remove file by path.
258 * @param path path to file
259 * @return standard error code */
260 int ext4_fremove(const char *path);
262 /**@brief create a hardlink for a file.
263 * @param path path to file
264 * @param hardlink_path path of hardlink
265 * @return standard error code */
266 int ext4_flink(const char *path, const char *hardlink_path);
268 /**@brief Rename file
270 * @param new_path destination
271 * @return standard error code */
272 int ext4_frename(const char *path, const char *new_path);
274 /**@brief File open function.
275 * @param filename, (has to start from mount point)
276 * /my_partition/my_file
277 * @param flags open file flags
278 * |---------------------------------------------------------------|
279 * | r or rb O_RDONLY |
280 * |---------------------------------------------------------------|
281 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
282 * |---------------------------------------------------------------|
283 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
284 * |---------------------------------------------------------------|
285 * | r+ or rb+ or r+b O_RDWR |
286 * |---------------------------------------------------------------|
287 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
288 * |---------------------------------------------------------------|
289 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
290 * |---------------------------------------------------------------|
292 * @return standard error code*/
293 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
295 /**@brief Alternate file open function.
296 * @param filename, (has to start from mount point)
297 * /my_partition/my_file
298 * @param flags open file flags
299 * @return standard error code*/
300 int ext4_fopen2(ext4_file *f, const char *path, int flags, bool file_expect);
302 /**@brief File close function.
303 * @param f file handle
304 * @return standard error code*/
305 int ext4_fclose(ext4_file *f);
307 /**@brief Fill in the ext4_inode buffer.
310 * @param ext4_inode buffer
311 * @return standard error code*/
312 int ext4_fill_raw_inode(const char *mount_point, uint32_t ino,
313 struct ext4_inode *inode);
315 /**@brief File truncate function.
316 * @param f file handle
317 * @param new file size
318 * @return standard error code*/
319 int ext4_ftruncate(ext4_file *f, uint64_t size);
321 /**@brief Read data from file.
322 * @param f file handle
323 * @param buf output buffer
324 * @param size bytes to read
325 * @param rcnt bytes read (may be NULL)
326 * @return standard error code*/
327 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
329 /**@brief Write data to file.
330 * @param f file handle
331 * @param buf data to write
332 * @param size write length
333 * @param wcnt bytes written (may be NULL)
334 * @return standard error code*/
335 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
337 /**@brief File seek operation.
338 * @param f file handle
339 * @param offset offset to seek
340 * @param origin seek type:
344 * @return standard error code*/
345 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
347 /**@brief Get file position.
348 * @param f file handle
349 * @return actual file position */
350 uint64_t ext4_ftell(ext4_file *f);
352 /**@brief Get file size.
353 * @param f file handle
354 * @return file size */
355 uint64_t ext4_fsize(ext4_file *f);
357 int ext4_fchmod(ext4_file *f, uint32_t mode);
358 int ext4_fchown(ext4_file *f, uint32_t uid, uint32_t gid);
359 int ext4_file_set_atime(ext4_file *f, uint32_t atime);
360 int ext4_file_set_mtime(ext4_file *f, uint32_t mtime);
361 int ext4_file_set_ctime(ext4_file *f, uint32_t ctime);
363 int ext4_fsymlink(const char *target, const char *path);
365 /*********************************DIRECTORY OPERATION***********************/
367 /**@brief Recursive directory remove.
368 * @param path directory path to remove
369 * @return standard error code*/
370 int ext4_dir_rm(const char *path);
372 /**@brief Create new directory.
373 * @param name new directory name
374 * @return standard error code*/
375 int ext4_dir_mk(const char *path);
377 /**@brief Directory open.
378 * @param d directory handle
379 * @param path directory path
380 * @return standard error code*/
381 int ext4_dir_open(ext4_dir *d, const char *path);
383 /**@brief Directory close.
384 * @param d directory handle
385 * @return standard error code*/
386 int ext4_dir_close(ext4_dir *d);
388 /**@brief Return next directory entry.
389 * @param d directory handle
391 * @return directory entry id (NULL if no entry)*/
392 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);