ext4.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  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.
  16. *
  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.
  27. */
  28. /** @addtogroup lwext4
  29. * @{
  30. */
  31. /**
  32. * @file ext4.h
  33. * @brief Ext4 high level operations (files, directories, mount points...).
  34. * Client has to include only this file.
  35. */
  36. #ifndef EXT4_H_
  37. #define EXT4_H_
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #include <stdint.h>
  42. #include <stddef.h>
  43. #include "ext4_config.h"
  44. #include "ext4_types.h"
  45. #include "ext4_errno.h"
  46. #include "ext4_oflags.h"
  47. #include "ext4_debug.h"
  48. #include "ext4_blockdev.h"
  49. /********************************OS LOCK INFERFACE***************************/
  50. /**@brief OS dependent lock interface.*/
  51. struct ext4_lock {
  52. /**@brief Lock access to mount point*/
  53. void (*lock)(void);
  54. /**@brief Unlock access to mount point*/
  55. void (*unlock)(void);
  56. };
  57. /********************************FILE DESCRIPTOR*****************************/
  58. /**@brief File descriptor*/
  59. typedef struct ext4_file {
  60. /**@brief Mount point handle.*/
  61. struct ext4_mountpoint *mp;
  62. /**@brief File inode id*/
  63. uint32_t inode;
  64. /**@brief Open flags.*/
  65. uint32_t flags;
  66. /**@brief File size.*/
  67. uint64_t fsize;
  68. /**@brief File position*/
  69. uint64_t fpos;
  70. } ext4_file;
  71. /*****************************DIRECTORY DESCRIPTOR***************************/
  72. /**@brief Directory entry descriptor. Copy from ext4_types.h*/
  73. typedef struct ext4_direntry {
  74. uint32_t inode;
  75. uint16_t entry_length;
  76. uint8_t name_length;
  77. uint8_t inode_type;
  78. uint8_t name[255];
  79. } ext4_direntry;
  80. typedef struct ext4_dir {
  81. /**@brief File descriptor*/
  82. ext4_file f;
  83. /**@brief Current directory entry.*/
  84. ext4_direntry de;
  85. /**@brief Next entry offset*/
  86. uint64_t next_off;
  87. } ext4_dir;
  88. /********************************MOUNT OPERATIONS****************************/
  89. /**@brief Register a block device to a name.
  90. * @warning Block device has to be filled by
  91. * Block cache may by created automatically when bc parameter is NULL.
  92. * @param bd block device
  93. * @param bd block device cache
  94. * @param dev_name register name
  95. * @param standard error code*/
  96. int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
  97. const char *dev_name);
  98. /**@brief Mount a block device with EXT4 partition to the mount point.
  99. * @param dev_name block device name (@ref ext4_device_register)
  100. * @param mount_point mount point, for example
  101. * - /
  102. * - /my_partition/
  103. * - /my_second_partition/
  104. * @param read_only mount as read-only mode.
  105. *
  106. * @return standard error code */
  107. int ext4_mount(const char *dev_name,
  108. const char *mount_point,
  109. bool read_only);
  110. /**@brief Umount operation.
  111. * @param mount_point mount name
  112. * @return standard error code */
  113. int ext4_umount(const char *mount_point);
  114. /**@brief Start journaling. Journaling start/stop functions are transparent
  115. * and might be used on filesystems without journaling support.
  116. * @warning Usage:
  117. * ext4_mount("sda1", "/");
  118. * ext4_journal_start("/");
  119. *
  120. * //File operations here...
  121. *
  122. * ext4_journal_stop("/");
  123. * ext4_umount("/");
  124. * @param mount_point mount name
  125. * @return standard error code */
  126. int ext4_journal_start(const char *mount_point);
  127. /**@brief Stop journaling. Journaling start/stop functions are transparent
  128. * and might be used on filesystems without journaling support.
  129. * @param mount_point mount name
  130. * @return standard error code */
  131. int ext4_journal_stop(const char *mount_point);
  132. /**@brief Journal recovery.
  133. * @param mount_point mount point
  134. * @warning Must be called after @ref ext4_mount
  135. * @return standard error code */
  136. int ext4_recover(const char *mount_point);
  137. /**@brief Some of the filesystem stats.*/
  138. struct ext4_mount_stats {
  139. uint32_t inodes_count;
  140. uint32_t free_inodes_count;
  141. uint64_t blocks_count;
  142. uint64_t free_blocks_count;
  143. uint32_t block_size;
  144. uint32_t block_group_count;
  145. uint32_t blocks_per_group;
  146. uint32_t inodes_per_group;
  147. char volume_name[16];
  148. };
  149. /**@brief Get file system params.
  150. * @param mount_point mount path
  151. * @param stats ext fs stats
  152. * @return standard error code */
  153. int ext4_mount_point_stats(const char *mount_point,
  154. struct ext4_mount_stats *stats);
  155. /**@brief Setup OS lock routines.
  156. * @param mount_point mount path
  157. * @param locks - lock and unlock functions
  158. * @return standard error code */
  159. int ext4_mount_setup_locks(const char *mount_point,
  160. const struct ext4_lock *locks);
  161. /**@brief Acquire the filesystem superblock pointer of a mp.
  162. * @param mount_point mount path
  163. * @param superblock pointer
  164. * @return standard error code */
  165. int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
  166. /**@brief Enable/disable write back cache mode.
  167. * @warning Default model of cache is write trough. It means that when You do:
  168. *
  169. * ext4_fopen(...);
  170. * ext4_fwrie(...);
  171. * < --- data is flushed to physical drive
  172. *
  173. * When you do:
  174. * ext4_cache_write_back(..., 1);
  175. * ext4_fopen(...);
  176. * ext4_fwrie(...);
  177. * < --- data is NOT flushed to physical drive
  178. * ext4_cache_write_back(..., 0);
  179. * < --- when write back mode is disabled all
  180. * cache data will be flushed
  181. * To enable write back mode permanently just call this function
  182. * once after ext4_mount (and disable before ext4_umount).
  183. *
  184. * Some of the function use write back cache mode internally.
  185. * If you enable write back mode twice you have to disable it twice
  186. * to flush all data:
  187. *
  188. * ext4_cache_write_back(..., 1);
  189. * ext4_cache_write_back(..., 1);
  190. *
  191. * ext4_cache_write_back(..., 0);
  192. * ext4_cache_write_back(..., 0);
  193. *
  194. * Write back mode is useful when you want to create a lot of empty
  195. * files/directories.
  196. *
  197. * @param path mount point path
  198. * @param on enable/disable
  199. *
  200. * @return standard error code */
  201. int ext4_cache_write_back(const char *path, bool on);
  202. /********************************FILE OPERATIONS*****************************/
  203. /**@brief Remove file by path.
  204. * @param path path to file
  205. * @return standard error code */
  206. int ext4_fremove(const char *path);
  207. /**@brief create a hardlink for a file.
  208. * @param path path to file
  209. * @param hardlink_path path of hardlink
  210. * @return standard error code */
  211. int ext4_flink(const char *path, const char *hardlink_path);
  212. /**@brief Rename file
  213. * @param path source
  214. * @param new_path destination
  215. * @return standard error code */
  216. int ext4_frename(const char *path, const char *new_path);
  217. /**@brief File open function.
  218. * @param path filename (has to start from mount point)
  219. * /my_partition/my_file
  220. * @param flags open file flags
  221. * |---------------------------------------------------------------|
  222. * | r or rb O_RDONLY |
  223. * |---------------------------------------------------------------|
  224. * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
  225. * |---------------------------------------------------------------|
  226. * | a or ab O_WRONLY|O_CREAT|O_APPEND |
  227. * |---------------------------------------------------------------|
  228. * | r+ or rb+ or r+b O_RDWR |
  229. * |---------------------------------------------------------------|
  230. * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
  231. * |---------------------------------------------------------------|
  232. * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
  233. * |---------------------------------------------------------------|
  234. *
  235. * @return standard error code*/
  236. int ext4_fopen(ext4_file *f, const char *path, const char *flags);
  237. /**@brief Alternate file open function.
  238. * @param filename, (has to start from mount point)
  239. * /my_partition/my_file
  240. * @param flags open file flags
  241. * @return standard error code*/
  242. int ext4_fopen2(ext4_file *f, const char *path, int flags);
  243. /**@brief File close function.
  244. * @param f file handle
  245. * @return standard error code*/
  246. int ext4_fclose(ext4_file *f);
  247. /**@brief Fill in the ext4_inode buffer.
  248. * @param path fetch inode data of the path
  249. * @param ret_ino the inode id of the path
  250. * @param ext4_inode buffer
  251. * @return standard error code*/
  252. int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
  253. struct ext4_inode *inode);
  254. /**@brief File truncate function.
  255. * @param f file handle
  256. * @param new file size
  257. * @return standard error code*/
  258. int ext4_ftruncate(ext4_file *f, uint64_t size);
  259. /**@brief Read data from file.
  260. * @param f file handle
  261. * @param buf output buffer
  262. * @param size bytes to read
  263. * @param rcnt bytes read (may be NULL)
  264. * @return standard error code*/
  265. int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt);
  266. /**@brief Write data to file.
  267. * @param f file handle
  268. * @param buf data to write
  269. * @param size write length
  270. * @param wcnt bytes written (may be NULL)
  271. * @return standard error code*/
  272. int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt);
  273. /**@brief File seek operation.
  274. * @param f file handle
  275. * @param offset offset to seek
  276. * @param origin seek type:
  277. * @ref SEEK_SET
  278. * @ref SEEK_CUR
  279. * @ref SEEK_END
  280. * @return standard error code*/
  281. int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
  282. /**@brief Get file position.
  283. * @param f file handle
  284. * @return actual file position */
  285. uint64_t ext4_ftell(ext4_file *f);
  286. /**@brief Get file size.
  287. * @param f file handle
  288. * @return file size */
  289. uint64_t ext4_fsize(ext4_file *f);
  290. /**@brief Change file/directory/link mode bits
  291. * @param path to file/dir/link
  292. * @param mode new mode bits (for example 0777)
  293. * @return standard error code*/
  294. int ext4_chmod(const char *path, uint32_t mode);
  295. /**@brief Change file owner and group
  296. * @param path to file/dir/link
  297. * @param uid user id
  298. * @param gid group id
  299. * @return standard error code*/
  300. int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
  301. /**@brief Set file access time
  302. * @param path to file/dir/link
  303. * @param atime access timestamp
  304. * @return standard error code*/
  305. int ext4_file_set_atime(const char *path, uint32_t atime);
  306. /**@brief Set file modify time
  307. * @param path to file/dir/link
  308. * @param mtime modify timestamp
  309. * @return standard error code*/
  310. int ext4_file_set_mtime(const char *path, uint32_t mtime);
  311. /**@brief Set file change time
  312. * @param path to file/dir/link
  313. * @param ctime change timestamp
  314. * @return standard error code*/
  315. int ext4_file_set_ctime(const char *path, uint32_t ctime);
  316. /**@brief Create symbolic link
  317. * @param target destination path
  318. * @param path source entry
  319. * @return standard error code*/
  320. int ext4_fsymlink(const char *target, const char *path);
  321. /**@brief Read symbolic link payload
  322. * @param path to symlink
  323. * @param buf output buffer
  324. * @param bufsize output buffer max size
  325. * @param rcnt bytes read
  326. * @return standard error code*/
  327. int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
  328. /**@brief Set extended attribute
  329. * @param path to file/directory
  330. * @param name name of the entry to add
  331. * @param name_len length of @name in bytes
  332. * @param data data of the entry to add
  333. * @param data_size size of data to add
  334. * @param replace whether existing entries should be replaced
  335. * @return standard error code*/
  336. int ext4_setxattr(const char *path, const char *name, size_t name_len,
  337. const void *data, size_t data_size, bool replace);
  338. /**@brief Get extended attribute
  339. * @param path to file/directory
  340. * @param name name of the entry to get
  341. * @param name_len length of @name in bytes
  342. * @param data data of the entry to get
  343. * @param data_size size of data to get
  344. * @return standard error code*/
  345. int ext4_getxattr(const char *path, const char *name, size_t name_len,
  346. void *buf, size_t buf_size, size_t *data_size);
  347. /**@brief List extended attributes
  348. * @param path to file/directory
  349. * @param list list to hold the name of entries
  350. * @param size size of @list in bytes
  351. * @param ret_size used bytes of @list
  352. * @return standard error code*/
  353. int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
  354. /**@brief Remove extended attribute
  355. * @param path to file/directory
  356. * @param name name of the entry to remove
  357. * @param name_len length of @name in bytes
  358. * @return standard error code*/
  359. int ext4_removexattr(const char *path, const char *name, size_t name_len);
  360. /*********************************DIRECTORY OPERATION***********************/
  361. /**@brief Recursive directory remove.
  362. * @param path directory path to remove
  363. * @return standard error code*/
  364. int ext4_dir_rm(const char *path);
  365. /**@brief Create new directory.
  366. * @param name new directory name
  367. * @return standard error code*/
  368. int ext4_dir_mk(const char *path);
  369. /**@brief Directory open.
  370. * @param d directory handle
  371. * @param path directory path
  372. * @return standard error code*/
  373. int ext4_dir_open(ext4_dir *d, const char *path);
  374. /**@brief Directory close.
  375. * @param d directory handle
  376. * @return standard error code*/
  377. int ext4_dir_close(ext4_dir *d);
  378. /**@brief Return next directory entry.
  379. * @param d directory handle
  380. * @param id entry id
  381. * @return directory entry id (NULL if no entry)*/
  382. const ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
  383. /**@brief Rewine directory entry offset.
  384. * @param d directory handle*/
  385. void ext4_dir_entry_rewind(ext4_dir *d);
  386. #ifdef __cplusplus
  387. }
  388. #endif
  389. #endif /* EXT4_H_ */
  390. /**
  391. * @}
  392. */