internal.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef __EROFS_INTERNAL_H
  3. #define __EROFS_INTERNAL_H
  4. #include "linux/compat.h"
  5. #define __packed __attribute__((__packed__))
  6. #include <linux/stat.h>
  7. #include <linux/bug.h>
  8. #include <linux/err.h>
  9. #include <linux/printk.h>
  10. #include <linux/log2.h>
  11. #include <inttypes.h>
  12. #include "erofs_fs.h"
  13. #define erofs_err(fmt, ...) \
  14. pr_err(fmt "\n", ##__VA_ARGS__)
  15. #define erofs_info(fmt, ...) \
  16. pr_info(fmt "\n", ##__VA_ARGS__)
  17. #define erofs_dbg(fmt, ...) \
  18. pr_debug(fmt "\n", ##__VA_ARGS__)
  19. #define DBG_BUGON(condition) BUG_ON(condition)
  20. /* no obvious reason to support explicit PAGE_SIZE != 4096 for now */
  21. #if PAGE_SIZE != 4096
  22. #error incompatible PAGE_SIZE is already defined
  23. #endif
  24. #define PAGE_MASK (~(PAGE_SIZE - 1))
  25. #ifndef EROFS_MAX_BLOCK_SIZE
  26. #define EROFS_MAX_BLOCK_SIZE PAGE_SIZE
  27. #endif
  28. #define EROFS_ISLOTBITS 5
  29. #define EROFS_SLOTSIZE (1U << EROFS_ISLOTBITS)
  30. typedef u64 erofs_off_t;
  31. typedef u64 erofs_nid_t;
  32. /* data type for filesystem-wide blocks number */
  33. typedef u32 erofs_blk_t;
  34. #define NULL_ADDR ((unsigned int)-1)
  35. #define NULL_ADDR_UL ((unsigned long)-1)
  36. /* global sbi */
  37. extern struct erofs_sb_info sbi;
  38. #define erofs_blksiz() (1u << sbi.blkszbits)
  39. #define erofs_blknr(addr) ((addr) >> sbi.blkszbits)
  40. #define erofs_blkoff(addr) ((addr) & (erofs_blksiz() - 1))
  41. #define erofs_pos(nr) ((erofs_off_t)(nr) << sbi.blkszbits)
  42. #define BLK_ROUND_UP(addr) DIV_ROUND_UP(addr, 1u << sbi.blkszbits)
  43. struct erofs_buffer_head;
  44. struct erofs_device_info {
  45. u32 blocks;
  46. u32 mapped_blkaddr;
  47. };
  48. #define EROFS_PACKED_NID_UNALLOCATED -1
  49. struct erofs_sb_info {
  50. struct erofs_device_info *devs;
  51. u64 total_blocks;
  52. u64 primarydevice_blocks;
  53. erofs_blk_t meta_blkaddr;
  54. erofs_blk_t xattr_blkaddr;
  55. u32 feature_compat;
  56. u32 feature_incompat;
  57. u64 build_time;
  58. u32 build_time_nsec;
  59. unsigned char islotbits;
  60. unsigned char blkszbits;
  61. /* what we really care is nid, rather than ino.. */
  62. erofs_nid_t root_nid;
  63. /* used for statfs, f_files - f_favail */
  64. u64 inos;
  65. u8 uuid[16];
  66. char volume_name[16];
  67. u16 available_compr_algs;
  68. u16 lz4_max_distance;
  69. u32 checksum;
  70. u16 extra_devices;
  71. union {
  72. u16 devt_slotoff; /* used for mkfs */
  73. u16 device_id_mask; /* used for others */
  74. };
  75. erofs_nid_t packed_nid;
  76. u32 xattr_prefix_start;
  77. u8 xattr_prefix_count;
  78. };
  79. static inline erofs_off_t iloc(erofs_nid_t nid)
  80. {
  81. return erofs_pos(sbi.meta_blkaddr) + (nid << sbi.islotbits);
  82. }
  83. #define EROFS_FEATURE_FUNCS(name, compat, feature) \
  84. static inline bool erofs_sb_has_##name(void) \
  85. { \
  86. return sbi.feature_##compat & EROFS_FEATURE_##feature; \
  87. } \
  88. static inline void erofs_sb_set_##name(void) \
  89. { \
  90. sbi.feature_##compat |= EROFS_FEATURE_##feature; \
  91. } \
  92. static inline void erofs_sb_clear_##name(void) \
  93. { \
  94. sbi.feature_##compat &= ~EROFS_FEATURE_##feature; \
  95. }
  96. EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_ZERO_PADDING)
  97. EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
  98. EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
  99. EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
  100. EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
  101. EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
  102. EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
  103. EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
  104. EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
  105. EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
  106. #define EROFS_I_EA_INITED (1 << 0)
  107. #define EROFS_I_Z_INITED (1 << 1)
  108. struct erofs_inode {
  109. struct list_head i_hash, i_subdirs, i_xattrs;
  110. union {
  111. /* (erofsfuse) runtime flags */
  112. unsigned int flags;
  113. /* (mkfs.erofs) device ID containing source file */
  114. u32 dev;
  115. /* (mkfs.erofs) queued sub-directories blocking dump */
  116. u32 subdirs_queued;
  117. };
  118. unsigned int i_count;
  119. struct erofs_inode *i_parent;
  120. umode_t i_mode;
  121. erofs_off_t i_size;
  122. u64 i_ino[2];
  123. u32 i_uid;
  124. u32 i_gid;
  125. u64 i_mtime;
  126. u32 i_mtime_nsec;
  127. u32 i_nlink;
  128. union {
  129. u32 i_blkaddr;
  130. u32 i_blocks;
  131. u32 i_rdev;
  132. struct {
  133. unsigned short chunkformat;
  134. unsigned char chunkbits;
  135. };
  136. } u;
  137. char *i_srcpath;
  138. unsigned char datalayout;
  139. unsigned char inode_isize;
  140. /* inline tail-end packing size */
  141. unsigned short idata_size;
  142. bool compressed_idata;
  143. bool lazy_tailblock;
  144. unsigned int xattr_isize;
  145. unsigned int extent_isize;
  146. unsigned int xattr_shared_count;
  147. unsigned int *xattr_shared_xattrs;
  148. erofs_nid_t nid;
  149. struct erofs_buffer_head *bh;
  150. struct erofs_buffer_head *bh_inline, *bh_data;
  151. void *idata;
  152. /* (ztailpacking) in order to recover uncompressed EOF data */
  153. void *eof_tailraw;
  154. unsigned int eof_tailrawsize;
  155. union {
  156. void *compressmeta;
  157. void *chunkindexes;
  158. struct {
  159. uint16_t z_advise;
  160. uint8_t z_algorithmtype[2];
  161. uint8_t z_logical_clusterbits;
  162. uint8_t z_physical_clusterblks;
  163. uint64_t z_tailextent_headlcn;
  164. unsigned int z_idataoff;
  165. #define z_idata_size idata_size
  166. };
  167. };
  168. uint64_t capabilities;
  169. erofs_off_t fragmentoff;
  170. unsigned int fragment_size;
  171. };
  172. static inline bool is_inode_layout_compression(struct erofs_inode *inode)
  173. {
  174. return erofs_inode_is_data_compressed(inode->datalayout);
  175. }
  176. static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
  177. unsigned int bits)
  178. {
  179. return (value >> bit) & ((1 << bits) - 1);
  180. }
  181. static inline unsigned int erofs_inode_version(unsigned int value)
  182. {
  183. return erofs_bitrange(value, EROFS_I_VERSION_BIT,
  184. EROFS_I_VERSION_BITS);
  185. }
  186. static inline unsigned int erofs_inode_datalayout(unsigned int value)
  187. {
  188. return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
  189. EROFS_I_DATALAYOUT_BITS);
  190. }
  191. #define IS_ROOT(x) ((x) == (x)->i_parent)
  192. struct erofs_dentry {
  193. struct list_head d_child; /* child of parent list */
  194. unsigned int type;
  195. char name[EROFS_NAME_LEN];
  196. union {
  197. struct erofs_inode *inode;
  198. erofs_nid_t nid;
  199. };
  200. };
  201. static inline bool is_dot_dotdot_len(const char *name, unsigned int len)
  202. {
  203. if (len >= 1 && name[0] != '.')
  204. return false;
  205. return len == 1 || (len == 2 && name[1] == '.');
  206. }
  207. static inline bool is_dot_dotdot(const char *name)
  208. {
  209. if (name[0] != '.')
  210. return false;
  211. return name[1] == '\0' || (name[1] == '.' && name[2] == '\0');
  212. }
  213. enum {
  214. BH_Meta,
  215. BH_Mapped,
  216. BH_Encoded,
  217. BH_FullMapped,
  218. BH_Fragment,
  219. BH_Partialref,
  220. };
  221. /* Has a disk mapping */
  222. #define EROFS_MAP_MAPPED (1 << BH_Mapped)
  223. /* Located in metadata (could be copied from bd_inode) */
  224. #define EROFS_MAP_META (1 << BH_Meta)
  225. /* The extent is encoded */
  226. #define EROFS_MAP_ENCODED (1 << BH_Encoded)
  227. /* The length of extent is full */
  228. #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
  229. /* Located in the special packed inode */
  230. #define EROFS_MAP_FRAGMENT (1 << BH_Fragment)
  231. /* The extent refers to partial decompressed data */
  232. #define EROFS_MAP_PARTIAL_REF (1 << BH_Partialref)
  233. struct erofs_map_blocks {
  234. char mpage[EROFS_MAX_BLOCK_SIZE];
  235. erofs_off_t m_pa, m_la;
  236. u64 m_plen, m_llen;
  237. unsigned short m_deviceid;
  238. char m_algorithmformat;
  239. unsigned int m_flags;
  240. erofs_blk_t index;
  241. };
  242. /*
  243. * Used to get the exact decompressed length, e.g. fiemap (consider lookback
  244. * approach instead if possible since it's more metadata lightweight.)
  245. */
  246. #define EROFS_GET_BLOCKS_FIEMAP 0x0002
  247. /* Used to map tail extent for tailpacking inline or fragment pcluster */
  248. #define EROFS_GET_BLOCKS_FINDTAIL 0x0008
  249. enum {
  250. Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
  251. Z_EROFS_COMPRESSION_INTERLACED,
  252. Z_EROFS_COMPRESSION_RUNTIME_MAX
  253. };
  254. struct erofs_map_dev {
  255. erofs_off_t m_pa;
  256. unsigned int m_deviceid;
  257. };
  258. /* fs.c */
  259. int erofs_blk_read(void *buf, erofs_blk_t start, u32 nblocks);
  260. int erofs_dev_read(int device_id, void *buf, u64 offset, size_t len);
  261. /* super.c */
  262. int erofs_read_superblock(void);
  263. void erofs_put_super(void);
  264. /* namei.c */
  265. int erofs_read_inode_from_disk(struct erofs_inode *vi);
  266. int erofs_ilookup(const char *path, struct erofs_inode *vi);
  267. int erofs_read_inode_from_disk(struct erofs_inode *vi);
  268. /* data.c */
  269. int erofs_pread(struct erofs_inode *inode, char *buf,
  270. erofs_off_t count, erofs_off_t offset);
  271. int erofs_map_blocks(struct erofs_inode *inode, struct erofs_map_blocks *map,
  272. int flags);
  273. int erofs_map_dev(struct erofs_map_dev *map);
  274. int erofs_read_one_data(struct erofs_map_blocks *map, char *buffer, u64 offset,
  275. size_t len);
  276. int z_erofs_read_one_data(struct erofs_inode *inode,
  277. struct erofs_map_blocks *map, char *raw, char *buffer,
  278. erofs_off_t skip, erofs_off_t length, bool trimmed);
  279. static inline int erofs_get_occupied_size(const struct erofs_inode *inode,
  280. erofs_off_t *size)
  281. {
  282. *size = 0;
  283. switch (inode->datalayout) {
  284. case EROFS_INODE_FLAT_INLINE:
  285. case EROFS_INODE_FLAT_PLAIN:
  286. case EROFS_INODE_CHUNK_BASED:
  287. *size = inode->i_size;
  288. break;
  289. case EROFS_INODE_COMPRESSED_FULL:
  290. case EROFS_INODE_COMPRESSED_COMPACT:
  291. *size = inode->u.i_blocks * erofs_blksiz();
  292. break;
  293. default:
  294. return -EOPNOTSUPP;
  295. }
  296. return 0;
  297. }
  298. /* data.c */
  299. int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,
  300. size_t buffer_size);
  301. int erofs_listxattr(struct erofs_inode *vi, char *buffer, size_t buffer_size);
  302. /* zmap.c */
  303. int z_erofs_fill_inode(struct erofs_inode *vi);
  304. int z_erofs_map_blocks_iter(struct erofs_inode *vi,
  305. struct erofs_map_blocks *map, int flags);
  306. #ifdef EUCLEAN
  307. #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
  308. #else
  309. #define EFSCORRUPTED EIO
  310. #endif
  311. #define CRC32C_POLY_LE 0x82F63B78
  312. static inline u32 erofs_crc32c(u32 crc, const u8 *in, size_t len)
  313. {
  314. int i;
  315. while (len--) {
  316. crc ^= *in++;
  317. for (i = 0; i < 8; i++)
  318. crc = (crc >> 1) ^ ((crc & 1) ? CRC32C_POLY_LE : 0);
  319. }
  320. return crc;
  321. }
  322. #endif