ext4.h 18 KB

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