ext4.h 17 KB

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