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*****************************/
50 #if CONFIG_HAVE_OWN_OFLAGS
77 #define O_APPEND 02000
80 /********************************FILE SEEK FLAGS*****************************/
99 /********************************OS LOCK INFERFACE***************************/
101 /**@brief OS dependent lock interface.*/
104 /**@brief Lock access to mount point*/
107 /**@brief Unlock access to mount point*/
108 void (*unlock)(void);
111 /********************************FILE DESCRIPTOR*****************************/
113 /**@brief File descriptor*/
114 typedef struct ext4_file {
116 /**@brief Mount point handle.*/
117 struct ext4_mountpoint *mp;
119 /**@brief File inode id*/
122 /**@brief Open flags.*/
125 /**@brief File size.*/
128 /**@brief File position*/
132 /*****************************DIRECTORY DESCRIPTOR***************************/
134 /**@brief Directory entry descriptor. Copy from ext4_types.h*/
137 uint16_t entry_length;
144 /**@brief File descriptor*/
146 /**@brief Current directory entry.*/
148 /**@brief Next entry offset*/
152 /********************************MOUNT OPERATIONS****************************/
154 /**@brief Register a block device to a name.
155 * @warning Block device has to be filled by
156 * @ref EXT4_BLOCKDEV_STATIC_INSTANCE. Block cache may be created
157 * @ref EXT4_BCACHE_STATIC_INSTANCE.
158 * Block cache may by created automatically when bc parameter is 0.
159 * @param bd block device
160 * @param bd block device cache (0 = automatic cache mode)
161 * @param dev_name register name
162 * @param standard error code*/
163 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
164 const char *dev_name);
166 /**@brief Mount a block device with EXT4 partition to the mount point.
167 * @param dev_name block device name (@ref ext4_device_register)
168 * @param mount_point mount point, for example
171 * - /my_second_partition/
173 * @return standard error code */
174 int ext4_mount(const char *dev_name, const char *mount_point);
176 /**@brief Umount operation.
177 * @param mount_point mount name
178 * @return standard error code */
179 int ext4_umount(const char *mount_point);
181 /**@brief Some of the filesystem stats.*/
182 struct ext4_mount_stats {
183 uint32_t inodes_count;
184 uint32_t free_inodes_count;
185 uint64_t blocks_count;
186 uint64_t free_blocks_count;
189 uint32_t block_group_count;
190 uint32_t blocks_per_group;
191 uint32_t inodes_per_group;
193 char volume_name[16];
196 /**@brief Get file system params.
197 * @param mount_point mount path
198 * @param stats ext fs stats
199 * @return standard error code */
200 int ext4_mount_point_stats(const char *mount_point,
201 struct ext4_mount_stats *stats);
203 /**@brief Setup OS lock routines.
204 * @param mount_point mount path
205 * @param locks - lock and unlock functions
206 * @return standard error code */
207 int ext4_mount_setup_locks(const char *mount_point,
208 const struct ext4_lock *locks);
210 /**@brief Acquire the filesystem superblock pointer of a mp.
211 * @param mount_point mount path
212 * @param superblock pointer
213 * @return standard error code */
214 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
216 /**@brief Enable/disable write back cache mode.
217 * @warning Default model of cache is write trough. It means that when You do:
221 * < --- data is flushed to physical drive
224 * ext4_cache_write_back(..., 1);
227 * < --- data is NOT flushed to physical drive
228 * ext4_cache_write_back(..., 0);
229 * < --- when write back mode is disabled all
230 * cache data will be flushed
231 * To enable write back mode permanently just call this function
232 * once after ext4_mount (and disable before ext4_umount).
234 * Some of the function use write back cache mode internally.
235 * If you enable write back mode twice you have to disable it twice
238 * ext4_cache_write_back(..., 1);
239 * ext4_cache_write_back(..., 1);
241 * ext4_cache_write_back(..., 0);
242 * ext4_cache_write_back(..., 0);
244 * Write back mode is useful when you want to create a lot of empty
247 * @param path mount point path
248 * @param on enable/disable
250 * @return standard error code */
251 int ext4_cache_write_back(const char *path, bool on);
253 /********************************FILE OPERATIONS*****************************/
255 /**@brief Remove file by path.
256 * @param path path to file
257 * @return standard error code */
258 int ext4_fremove(const char *path);
260 /**@brief create a hardlink for a file.
261 * @param path path to file
262 * @param hardlink_path path of hardlink
263 * @return standard error code */
264 int ext4_flink(const char *path, const char *hardlink_path);
266 /**@brief Rename file
268 * @param new_path destination
269 * @return standard error code */
270 int ext4_frename(const char *path, const char *new_path);
272 /**@brief File open function.
273 * @param filename, (has to start from mount point)
274 * /my_partition/my_file
275 * @param flags open file flags
276 * |---------------------------------------------------------------|
277 * | r or rb O_RDONLY |
278 * |---------------------------------------------------------------|
279 * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
280 * |---------------------------------------------------------------|
281 * | a or ab O_WRONLY|O_CREAT|O_APPEND |
282 * |---------------------------------------------------------------|
283 * | r+ or rb+ or r+b O_RDWR |
284 * |---------------------------------------------------------------|
285 * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
286 * |---------------------------------------------------------------|
287 * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
288 * |---------------------------------------------------------------|
290 * @return standard error code*/
291 int ext4_fopen(ext4_file *f, const char *path, const char *flags);
293 /**@brief Alternate file open function.
294 * @param filename, (has to start from mount point)
295 * /my_partition/my_file
296 * @param flags open file flags
297 * @return standard error code*/
298 int ext4_fopen2(ext4_file *f, const char *path, int flags);
300 /**@brief File close function.
301 * @param f file handle
302 * @return standard error code*/
303 int ext4_fclose(ext4_file *f);
305 /**@brief Fill in the ext4_inode buffer.
306 * @param path fetch inode data of the path
307 * @param ret_ino the inode id of the path
308 * @param ext4_inode buffer
309 * @return standard error code*/
310 int ext4_fill_raw_inode(const char *path,
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, size_t size, size_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, size_t size, size_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_chmod(const char *path, uint32_t mode);
357 int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
358 int ext4_file_set_atime(const char *path, uint32_t atime);
359 int ext4_file_set_mtime(const char *path, uint32_t mtime);
360 int ext4_file_set_ctime(const char *path, uint32_t ctime);
362 int ext4_fsymlink(const char *target, const char *path);
364 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
366 /*********************************DIRECTORY OPERATION***********************/
368 /**@brief Recursive directory remove.
369 * @param path directory path to remove
370 * @return standard error code*/
371 int ext4_dir_rm(const char *path);
373 /**@brief Create new directory.
374 * @param name new directory name
375 * @return standard error code*/
376 int ext4_dir_mk(const char *path);
378 /**@brief Directory open.
379 * @param d directory handle
380 * @param path directory path
381 * @return standard error code*/
382 int ext4_dir_open(ext4_dir *d, const char *path);
384 /**@brief Directory close.
385 * @param d directory handle
386 * @return standard error code*/
387 int ext4_dir_close(ext4_dir *d);
389 /**@brief Return next directory entry.
390 * @param d directory handle
392 * @return directory entry id (NULL if no entry)*/
393 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
395 /**@brief Rewine directory entry offset.
396 * @param d directory handle*/
397 void ext4_dir_entry_rewind(ext4_dir *d);