ext4.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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_inode.h>
  49. #include <ext4_fs.h>
  50. #include <ext4_blockdev.h>
  51. /********************************OS LOCK INFERFACE***************************/
  52. /**@brief OS dependent lock interface.*/
  53. struct ext4_lock {
  54. /**@brief Lock access to mount point.*/
  55. void (*lock)(void);
  56. /**@brief Unlock access to mount point.*/
  57. void (*unlock)(void);
  58. };
  59. /********************************FILE DESCRIPTOR*****************************/
  60. /**@brief File descriptor. */
  61. typedef struct ext4_file {
  62. /**@brief Mount point handle.*/
  63. struct ext4_mountpoint *mp;
  64. /**@brief File inode id.*/
  65. uint32_t inode;
  66. /**@brief Open flags.*/
  67. uint32_t flags;
  68. /**@brief File size.*/
  69. uint64_t fsize;
  70. /**@brief Actual file position.*/
  71. uint64_t fpos;
  72. } ext4_file;
  73. /*****************************DIRECTORY DESCRIPTOR***************************/
  74. /**@brief Directory entry descriptor. */
  75. typedef struct ext4_direntry {
  76. uint32_t inode;
  77. uint16_t entry_length;
  78. uint8_t name_length;
  79. uint8_t inode_type;
  80. uint8_t name[255];
  81. } ext4_direntry;
  82. /**@brief Directory descriptor. */
  83. typedef struct ext4_dir {
  84. /**@brief File descriptor.*/
  85. ext4_file f;
  86. /**@brief Current directory entry.*/
  87. ext4_direntry de;
  88. /**@brief Next entry offset.*/
  89. uint64_t next_off;
  90. } ext4_dir;
  91. /********************************MOUNT OPERATIONS****************************/
  92. /**@brief Mount a block device with EXT4 partition to the mount point.
  93. *
  94. * @param dev_name Block device name (@ref ext4_device_register).
  95. * @param mount_point Mount point, for example:
  96. * - /
  97. * - /my_partition/
  98. * - /my_second_partition/
  99. * @param read_only mount as read-only mode.
  100. *
  101. * @return Standard error code */
  102. int ext4_mount(struct ext4_blockdev *bd,
  103. const char *mount_point,
  104. bool read_only);
  105. /**@brief Umount operation.
  106. *
  107. * @param mount_point Mount point.
  108. *
  109. * @return Standard error code */
  110. int ext4_umount_mp(struct ext4_mountpoint *mp);
  111. /**@brief Umount operation.
  112. *
  113. * @param mount_point Mount point.
  114. *
  115. * @return Standard error code */
  116. int ext4_umount(const char *mount_point);
  117. /**@brief Starts journaling. Journaling start/stop functions are transparent
  118. * and might be used on filesystems without journaling support.
  119. * @warning Usage:
  120. * ext4_mount("sda1", "/");
  121. * ext4_journal_start("/");
  122. *
  123. * //File operations here...
  124. *
  125. * ext4_journal_stop("/");
  126. * ext4_umount("/");
  127. * @param mount_point Mount point.
  128. *
  129. * @return Standard error code. */
  130. int ext4_journal_start(const char *mount_point);
  131. /**@brief Stops journaling. Journaling start/stop functions are transparent
  132. * and might be used on filesystems without journaling support.
  133. *
  134. * @param mount_point Mount point name.
  135. *
  136. * @return Standard error code. */
  137. int ext4_journal_stop(const char *mount_point);
  138. /**@brief Journal recovery.
  139. * @warning Must be called after @ref ext4_mount.
  140. *
  141. * @param mount_point Mount point.
  142. *
  143. * @return Standard error code. */
  144. int ext4_recover(const char *mount_point);
  145. /**@brief Some of the filesystem stats. */
  146. struct ext4_mount_stats {
  147. uint32_t inodes_count;
  148. uint32_t free_inodes_count;
  149. uint64_t blocks_count;
  150. uint64_t free_blocks_count;
  151. uint32_t block_size;
  152. uint32_t block_group_count;
  153. uint32_t blocks_per_group;
  154. uint32_t inodes_per_group;
  155. char volume_name[16];
  156. };
  157. /**@brief Get file mount point stats.
  158. *
  159. * @param mount_point Mount point.
  160. * @param stats Filesystem stats.
  161. *
  162. * @return Standard error code. */
  163. int ext4_mount_point_stats(const char *mount_point,
  164. struct ext4_mount_stats *stats);
  165. /**@brief Setup OS lock routines.
  166. *
  167. * @param mount_point Mount point.
  168. * @param locks Lock and unlock functions
  169. *
  170. * @return Standard error code. */
  171. int ext4_mount_setup_locks(const char *mount_point,
  172. const struct ext4_lock *locks);
  173. /**@brief Acquire the filesystem superblock pointer of a mp.
  174. *
  175. * @param mount_point Mount point.
  176. * @param sb Superblock handle
  177. *
  178. * @return Standard error code. */
  179. int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb);
  180. /**@brief Enable/disable write back cache mode.
  181. * @warning Default model of cache is write trough. It means that when You do:
  182. *
  183. * ext4_fopen(...);
  184. * ext4_fwrite(...);
  185. * < --- data is flushed to physical drive
  186. *
  187. * When you do:
  188. * ext4_cache_write_back(..., 1);
  189. * ext4_fopen(...);
  190. * ext4_fwrite(...);
  191. * < --- data is NOT flushed to physical drive
  192. * ext4_cache_write_back(..., 0);
  193. * < --- when write back mode is disabled all
  194. * cache data will be flushed
  195. * To enable write back mode permanently just call this function
  196. * once after ext4_mount (and disable before ext4_umount).
  197. *
  198. * Some of the function use write back cache mode internally.
  199. * If you enable write back mode twice you have to disable it twice
  200. * to flush all data:
  201. *
  202. * ext4_cache_write_back(..., 1);
  203. * ext4_cache_write_back(..., 1);
  204. *
  205. * ext4_cache_write_back(..., 0);
  206. * ext4_cache_write_back(..., 0);
  207. *
  208. * Write back mode is useful when you want to create a lot of empty
  209. * files/directories.
  210. *
  211. * @param path Mount point.
  212. * @param on Enable/disable cache writeback mode.
  213. *
  214. * @return Standard error code. */
  215. int ext4_cache_write_back(const char *path, bool on);
  216. /**@brief Force cache flush.
  217. *
  218. * @param path Mount point.
  219. *
  220. * @return Standard error code. */
  221. int ext4_cache_flush(const char *path);
  222. /********************************FILE OPERATIONS*****************************/
  223. /**@brief Remove file by path.
  224. *
  225. * @param path Path to file.
  226. *
  227. * @return Standard error code. */
  228. int ext4_fremove(const char *path);
  229. /**@brief Create a hardlink for a file.
  230. *
  231. * @param path Path to file.
  232. * @param hardlink_path Path of hardlink.
  233. *
  234. * @return Standard error code. */
  235. int ext4_flink(const char *path, const char *hardlink_path);
  236. /**@brief Rename file.
  237. * @param path Source.
  238. * @param new_path Destination.
  239. * @return Standard error code. */
  240. int ext4_frename(const char *path, const char *new_path);
  241. /**@brief File open function.
  242. *
  243. * @param file File handle.
  244. * @param path File path, has to start from mount point:/my_partition/file.
  245. * @param flags File open flags.
  246. * |---------------------------------------------------------------|
  247. * | r or rb O_RDONLY |
  248. * |---------------------------------------------------------------|
  249. * | w or wb O_WRONLY|O_CREAT|O_TRUNC |
  250. * |---------------------------------------------------------------|
  251. * | a or ab O_WRONLY|O_CREAT|O_APPEND |
  252. * |---------------------------------------------------------------|
  253. * | r+ or rb+ or r+b O_RDWR |
  254. * |---------------------------------------------------------------|
  255. * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC |
  256. * |---------------------------------------------------------------|
  257. * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND |
  258. * |---------------------------------------------------------------|
  259. *
  260. * @return Standard error code.*/
  261. int ext4_fopen(ext4_file *file, const char *path, const char *flags);
  262. /**@brief Alternate file open function.
  263. *
  264. * @param file File handle.
  265. * @param path File path, has to start from mount point:/my_partition/file.
  266. * @param flags File open flags.
  267. *
  268. * @return Standard error code.*/
  269. int ext4_fopen2(ext4_file *file, const char *path, int flags);
  270. /**@brief File close function.
  271. *
  272. * @param file File handle.
  273. *
  274. * @return Standard error code.*/
  275. int ext4_fclose(ext4_file *file);
  276. /**@brief File truncate function.
  277. *
  278. * @param file File handle.
  279. * @param size New file size.
  280. *
  281. * @return Standard error code.*/
  282. int ext4_ftruncate(ext4_file *file, uint64_t size);
  283. /**@brief Read data from file.
  284. *
  285. * @param file File handle.
  286. * @param buf Output buffer.
  287. * @param size Bytes to read.
  288. * @param rcnt Bytes read (NULL allowed).
  289. *
  290. * @return Standard error code.*/
  291. int ext4_fread(ext4_file *file, void *buf, size_t size, size_t *rcnt);
  292. /**@brief Write data to file.
  293. *
  294. * @param file File handle.
  295. * @param buf Data to write
  296. * @param size Write length..
  297. * @param wcnt Bytes written (NULL allowed).
  298. *
  299. * @return Standard error code.*/
  300. int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt);
  301. /**@brief File seek operation.
  302. *
  303. * @param file File handle.
  304. * @param offset Offset to seek.
  305. * @param origin Seek type:
  306. * @ref SEEK_SET
  307. * @ref SEEK_CUR
  308. * @ref SEEK_END
  309. *
  310. * @return Standard error code.*/
  311. int ext4_fseek(ext4_file *file, int64_t offset, uint32_t origin);
  312. /**@brief Get file position.
  313. *
  314. * @param file File handle.
  315. *
  316. * @return Actual file position */
  317. uint64_t ext4_ftell(ext4_file *file);
  318. /**@brief Get file size.
  319. *
  320. * @param file File handle.
  321. *
  322. * @return File size. */
  323. uint64_t ext4_fsize(ext4_file *file);
  324. /**@brief Get inode of file/directory/link.
  325. *
  326. * @param path Parh to file/dir/link.
  327. * @param ret_ino Inode number.
  328. * @param inode Inode internals.
  329. *
  330. * @return Standard error code.*/
  331. int ext4_raw_inode_fill(const char *path, uint32_t *ret_ino,
  332. struct ext4_inode *inode);
  333. /**@brief Check if inode exists.
  334. *
  335. * @param path Parh to file/dir/link.
  336. * @param type Inode type.
  337. * @ref EXT4_DIRENTRY_UNKNOWN
  338. * @ref EXT4_DE_REG_FILE
  339. * @ref EXT4_DE_DIR
  340. * @ref EXT4_DE_CHRDEV
  341. * @ref EXT4_DE_BLKDEV
  342. * @ref EXT4_DE_FIFO
  343. * @ref EXT4_DE_SOCK
  344. * @ref EXT4_DE_SYMLINK
  345. *
  346. * @return Standard error code.*/
  347. int ext4_inode_exist(const char *path, int type);
  348. void* ext4_get_inode_ref(const char *path, struct ext4_inode_ref *ref);
  349. int ext4_put_inode_ref(struct ext4_mountpoint *mp, struct ext4_inode_ref *ref);
  350. /**@brief Change file/directory/link mode bits.
  351. *
  352. * @param path Path to file/dir/link.
  353. * @param mode New mode bits (for example 0777).
  354. *
  355. * @return Standard error code.*/
  356. int ext4_mode_set(const char *path, uint32_t mode);
  357. /**@brief Get file/directory/link mode bits.
  358. *
  359. * @param path Path to file/dir/link.
  360. * @param mode New mode bits (for example 0777).
  361. *
  362. * @return Standard error code.*/
  363. int ext4_mode_get(const char *path, uint32_t *mode);
  364. /**@brief Change file owner and group.
  365. *
  366. * @param path Path to file/dir/link.
  367. * @param uid User id.
  368. * @param gid Group id.
  369. *
  370. * @return Standard error code.*/
  371. int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid);
  372. /**@brief Get file/directory/link owner and group.
  373. *
  374. * @param path Path to file/dir/link.
  375. * @param uid User id.
  376. * @param gid Group id.
  377. *
  378. * @return Standard error code.*/
  379. int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid);
  380. /**@brief Set file/directory/link access time.
  381. *
  382. * @param path Path to file/dir/link.
  383. * @param atime Access timestamp.
  384. *
  385. * @return Standard error code.*/
  386. int ext4_atime_set(const char *path, uint32_t atime);
  387. /**@brief Set file/directory/link modify time.
  388. *
  389. * @param path Path to file/dir/link.
  390. * @param mtime Modify timestamp.
  391. *
  392. * @return Standard error code.*/
  393. int ext4_mtime_set(const char *path, uint32_t mtime);
  394. /**@brief Set file/directory/link change time.
  395. *
  396. * @param path Path to file/dir/link.
  397. * @param ctime Change timestamp.
  398. *
  399. * @return Standard error code.*/
  400. int ext4_ctime_set(const char *path, uint32_t ctime);
  401. /**@brief Get file/directory/link access time.
  402. *
  403. * @param path Path to file/dir/link.
  404. * @param atime Access timestamp.
  405. *
  406. * @return Standard error code.*/
  407. int ext4_atime_get(const char *path, uint32_t *atime);
  408. /**@brief Get file/directory/link modify time.
  409. *
  410. * @param path Path to file/dir/link.
  411. * @param mtime Modify timestamp.
  412. *
  413. * @return Standard error code.*/
  414. int ext4_mtime_get(const char *path, uint32_t *mtime);
  415. /**@brief Get file/directory/link change time.
  416. *
  417. * @param path Pathto file/dir/link.
  418. * @param ctime Change timestamp.
  419. *
  420. * @return standard error code*/
  421. int ext4_ctime_get(const char *path, uint32_t *ctime);
  422. /**@brief Create symbolic link.
  423. *
  424. * @param target Destination entry path.
  425. * @param path Source entry path.
  426. *
  427. * @return Standard error code.*/
  428. int ext4_fsymlink(const char *target, const char *path);
  429. /**@brief Create special file.
  430. * @param path Path to new special file.
  431. * @param filetype Filetype of the new special file.
  432. * (that must not be regular file, directory, or unknown type)
  433. * @param dev If filetype is char device or block device,
  434. * the device number will become the payload in the inode.
  435. * @return Standard error code.*/
  436. int ext4_mknod(const char *path, int filetype, uint32_t dev);
  437. /**@brief Read symbolic link payload.
  438. *
  439. * @param path Path to symlink.
  440. * @param buf Output buffer.
  441. * @param bufsize Output buffer max size.
  442. * @param rcnt Bytes read.
  443. *
  444. * @return Standard error code.*/
  445. int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
  446. /**@brief Set extended attribute.
  447. *
  448. * @param path Path to file/directory
  449. * @param name Name of the entry to add.
  450. * @param name_len Length of @name in bytes.
  451. * @param data Data of the entry to add.
  452. * @param data_size Size of data to add.
  453. *
  454. * @return Standard error code.*/
  455. int ext4_setxattr(const char *path, const char *name, size_t name_len,
  456. const void *data, size_t data_size);
  457. /**@brief Get extended attribute.
  458. *
  459. * @param path Path to file/directory.
  460. * @param name Name of the entry to get.
  461. * @param name_len Length of @name in bytes.
  462. * @param buf Data of the entry to get.
  463. * @param buf_size Size of data to get.
  464. *
  465. * @return Standard error code.*/
  466. int ext4_getxattr(const char *path, const char *name, size_t name_len,
  467. void *buf, size_t buf_size, size_t *data_size);
  468. /**@brief List extended attributes.
  469. *
  470. * @param path Path to file/directory.
  471. * @param list List to hold the name of entries.
  472. * @param size Size of @list in bytes.
  473. * @param ret_size Used bytes of @list.
  474. *
  475. * @return Standard error code.*/
  476. int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size);
  477. /**@brief Remove extended attribute.
  478. *
  479. * @param path Path to file/directory.
  480. * @param name Name of the entry to remove.
  481. * @param name_len Length of @name in bytes.
  482. *
  483. * @return Standard error code.*/
  484. int ext4_removexattr(const char *path, const char *name, size_t name_len);
  485. /*********************************DIRECTORY OPERATION***********************/
  486. /**@brief Recursive directory remove.
  487. *
  488. * @param path Directory path to remove
  489. *
  490. * @return Standard error code.*/
  491. int ext4_dir_rm(const char *path);
  492. /**@brief Rename/move directory.
  493. *
  494. * @param path Source path.
  495. * @param new_path Destination path.
  496. *
  497. * @return Standard error code. */
  498. int ext4_dir_mv(const char *path, const char *new_path);
  499. /**@brief Create new directory.
  500. *
  501. * @param path Directory name.
  502. *
  503. * @return Standard error code.*/
  504. int ext4_dir_mk(const char *path);
  505. /**@brief Directory open.
  506. *
  507. * @param dir Directory handle.
  508. * @param path Directory path.
  509. *
  510. * @return Standard error code.*/
  511. int ext4_dir_open(ext4_dir *dir, const char *path);
  512. /**@brief Directory close.
  513. *
  514. * @param dir directory handle.
  515. *
  516. * @return Standard error code.*/
  517. int ext4_dir_close(ext4_dir *dir);
  518. /**@brief Return next directory entry.
  519. *
  520. * @param dir Directory handle.
  521. *
  522. * @return Directory entry id (NULL if no entry)*/
  523. const ext4_direntry *ext4_dir_entry_next(ext4_dir *dir);
  524. /**@brief Rewine directory entry offset.
  525. *
  526. * @param dir Directory handle.*/
  527. void ext4_dir_entry_rewind(ext4_dir *dir);
  528. #ifdef __cplusplus
  529. }
  530. #endif
  531. #endif /* EXT4_H_ */
  532. /**
  533. * @}
  534. */