xfs_inode_fork.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_INODE_FORK_H__
  7. #define __XFS_INODE_FORK_H__
  8. struct xfs_inode_log_item;
  9. struct xfs_dinode;
  10. /*
  11. * File incore extent information, present for each of data & attr forks.
  12. */
  13. struct xfs_ifork {
  14. int64_t if_bytes; /* bytes in if_data */
  15. struct xfs_btree_block *if_broot; /* file's incore btree root */
  16. unsigned int if_seq; /* fork mod counter */
  17. int if_height; /* height of the extent tree */
  18. void *if_data; /* extent tree root or
  19. inline data */
  20. xfs_extnum_t if_nextents; /* # of extents in this fork */
  21. short if_broot_bytes; /* bytes allocated for root */
  22. int8_t if_format; /* format of this fork */
  23. uint8_t if_needextents; /* extents have not been read */
  24. };
  25. /*
  26. * Worst-case increase in the fork extent count when we're adding a single
  27. * extent to a fork and there's no possibility of splitting an existing mapping.
  28. */
  29. #define XFS_IEXT_ADD_NOSPLIT_CNT (1)
  30. /*
  31. * Punching out an extent from the middle of an existing extent can cause the
  32. * extent count to increase by 1.
  33. * i.e. | Old extent | Hole | Old extent |
  34. */
  35. #define XFS_IEXT_PUNCH_HOLE_CNT (1)
  36. /*
  37. * Adding/removing an xattr can cause XFS_DA_NODE_MAXDEPTH extents to
  38. * be added. One extra extent for dabtree in case a local attr is
  39. * large enough to cause a double split. It can also cause extent
  40. * count to increase proportional to the size of a remote xattr's
  41. * value.
  42. */
  43. #define XFS_IEXT_ATTR_MANIP_CNT(rmt_blks) \
  44. (XFS_DA_NODE_MAXDEPTH + max(1, rmt_blks))
  45. /*
  46. * A write to a sub-interval of an existing unwritten extent causes the original
  47. * extent to be split into 3 extents
  48. * i.e. | Unwritten | Real | Unwritten |
  49. * Hence extent count can increase by 2.
  50. */
  51. #define XFS_IEXT_WRITE_UNWRITTEN_CNT (2)
  52. /*
  53. * Moving an extent to data fork can cause a sub-interval of an existing extent
  54. * to be unmapped. This will increase extent count by 1. Mapping in the new
  55. * extent can increase the extent count by 1 again i.e.
  56. * | Old extent | New extent | Old extent |
  57. * Hence number of extents increases by 2.
  58. */
  59. #define XFS_IEXT_REFLINK_END_COW_CNT (2)
  60. /*
  61. * Removing an initial range of source/donor file's extent and adding a new
  62. * extent (from donor/source file) in its place will cause extent count to
  63. * increase by 1.
  64. */
  65. #define XFS_IEXT_SWAP_RMAP_CNT (1)
  66. /*
  67. * Fork handling.
  68. */
  69. #define XFS_IFORK_MAXEXT(ip, w) \
  70. (xfs_inode_fork_size(ip, w) / sizeof(xfs_bmbt_rec_t))
  71. static inline bool xfs_ifork_has_extents(struct xfs_ifork *ifp)
  72. {
  73. return ifp->if_format == XFS_DINODE_FMT_EXTENTS ||
  74. ifp->if_format == XFS_DINODE_FMT_BTREE;
  75. }
  76. static inline xfs_extnum_t xfs_ifork_nextents(struct xfs_ifork *ifp)
  77. {
  78. if (!ifp)
  79. return 0;
  80. return ifp->if_nextents;
  81. }
  82. static inline int8_t xfs_ifork_format(struct xfs_ifork *ifp)
  83. {
  84. if (!ifp)
  85. return XFS_DINODE_FMT_EXTENTS;
  86. return ifp->if_format;
  87. }
  88. static inline xfs_extnum_t xfs_iext_max_nextents(bool has_large_extent_counts,
  89. int whichfork)
  90. {
  91. switch (whichfork) {
  92. case XFS_DATA_FORK:
  93. case XFS_COW_FORK:
  94. if (has_large_extent_counts)
  95. return XFS_MAX_EXTCNT_DATA_FORK_LARGE;
  96. return XFS_MAX_EXTCNT_DATA_FORK_SMALL;
  97. case XFS_ATTR_FORK:
  98. if (has_large_extent_counts)
  99. return XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
  100. return XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
  101. default:
  102. ASSERT(0);
  103. return 0;
  104. }
  105. }
  106. static inline xfs_extnum_t
  107. xfs_dfork_data_extents(
  108. struct xfs_dinode *dip)
  109. {
  110. if (xfs_dinode_has_large_extent_counts(dip))
  111. return be64_to_cpu(dip->di_big_nextents);
  112. return be32_to_cpu(dip->di_nextents);
  113. }
  114. static inline xfs_extnum_t
  115. xfs_dfork_attr_extents(
  116. struct xfs_dinode *dip)
  117. {
  118. if (xfs_dinode_has_large_extent_counts(dip))
  119. return be32_to_cpu(dip->di_big_anextents);
  120. return be16_to_cpu(dip->di_anextents);
  121. }
  122. static inline xfs_extnum_t
  123. xfs_dfork_nextents(
  124. struct xfs_dinode *dip,
  125. int whichfork)
  126. {
  127. switch (whichfork) {
  128. case XFS_DATA_FORK:
  129. return xfs_dfork_data_extents(dip);
  130. case XFS_ATTR_FORK:
  131. return xfs_dfork_attr_extents(dip);
  132. default:
  133. ASSERT(0);
  134. break;
  135. }
  136. return 0;
  137. }
  138. void xfs_ifork_zap_attr(struct xfs_inode *ip);
  139. void xfs_ifork_init_attr(struct xfs_inode *ip, enum xfs_dinode_fmt format,
  140. xfs_extnum_t nextents);
  141. struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
  142. int xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);
  143. int xfs_iformat_attr_fork(struct xfs_inode *, struct xfs_dinode *);
  144. void xfs_iflush_fork(struct xfs_inode *, struct xfs_dinode *,
  145. struct xfs_inode_log_item *, int);
  146. void xfs_idestroy_fork(struct xfs_ifork *ifp);
  147. void * xfs_idata_realloc(struct xfs_inode *ip, int64_t byte_diff,
  148. int whichfork);
  149. void xfs_iroot_realloc(struct xfs_inode *, int, int);
  150. int xfs_iread_extents(struct xfs_trans *, struct xfs_inode *, int);
  151. int xfs_iextents_copy(struct xfs_inode *, struct xfs_bmbt_rec *,
  152. int);
  153. void xfs_init_local_fork(struct xfs_inode *ip, int whichfork,
  154. const void *data, int64_t size);
  155. xfs_extnum_t xfs_iext_count(struct xfs_ifork *ifp);
  156. void xfs_iext_insert_raw(struct xfs_ifork *ifp,
  157. struct xfs_iext_cursor *cur,
  158. struct xfs_bmbt_irec *irec);
  159. void xfs_iext_insert(struct xfs_inode *, struct xfs_iext_cursor *cur,
  160. struct xfs_bmbt_irec *, int);
  161. void xfs_iext_remove(struct xfs_inode *, struct xfs_iext_cursor *,
  162. int);
  163. void xfs_iext_destroy(struct xfs_ifork *);
  164. bool xfs_iext_lookup_extent(struct xfs_inode *ip,
  165. struct xfs_ifork *ifp, xfs_fileoff_t bno,
  166. struct xfs_iext_cursor *cur,
  167. struct xfs_bmbt_irec *gotp);
  168. bool xfs_iext_lookup_extent_before(struct xfs_inode *ip,
  169. struct xfs_ifork *ifp, xfs_fileoff_t *end,
  170. struct xfs_iext_cursor *cur,
  171. struct xfs_bmbt_irec *gotp);
  172. bool xfs_iext_get_extent(struct xfs_ifork *ifp,
  173. struct xfs_iext_cursor *cur,
  174. struct xfs_bmbt_irec *gotp);
  175. void xfs_iext_update_extent(struct xfs_inode *ip, int state,
  176. struct xfs_iext_cursor *cur,
  177. struct xfs_bmbt_irec *gotp);
  178. void xfs_iext_first(struct xfs_ifork *, struct xfs_iext_cursor *);
  179. void xfs_iext_last(struct xfs_ifork *, struct xfs_iext_cursor *);
  180. void xfs_iext_next(struct xfs_ifork *, struct xfs_iext_cursor *);
  181. void xfs_iext_prev(struct xfs_ifork *, struct xfs_iext_cursor *);
  182. static inline bool xfs_iext_next_extent(struct xfs_ifork *ifp,
  183. struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
  184. {
  185. xfs_iext_next(ifp, cur);
  186. return xfs_iext_get_extent(ifp, cur, gotp);
  187. }
  188. static inline bool xfs_iext_prev_extent(struct xfs_ifork *ifp,
  189. struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
  190. {
  191. xfs_iext_prev(ifp, cur);
  192. return xfs_iext_get_extent(ifp, cur, gotp);
  193. }
  194. /*
  195. * Return the extent after cur in gotp without updating the cursor.
  196. */
  197. static inline bool xfs_iext_peek_next_extent(struct xfs_ifork *ifp,
  198. struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
  199. {
  200. struct xfs_iext_cursor ncur = *cur;
  201. xfs_iext_next(ifp, &ncur);
  202. return xfs_iext_get_extent(ifp, &ncur, gotp);
  203. }
  204. /*
  205. * Return the extent before cur in gotp without updating the cursor.
  206. */
  207. static inline bool xfs_iext_peek_prev_extent(struct xfs_ifork *ifp,
  208. struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *gotp)
  209. {
  210. struct xfs_iext_cursor ncur = *cur;
  211. xfs_iext_prev(ifp, &ncur);
  212. return xfs_iext_get_extent(ifp, &ncur, gotp);
  213. }
  214. #define for_each_xfs_iext(ifp, ext, got) \
  215. for (xfs_iext_first((ifp), (ext)); \
  216. xfs_iext_get_extent((ifp), (ext), (got)); \
  217. xfs_iext_next((ifp), (ext)))
  218. extern struct kmem_cache *xfs_ifork_cache;
  219. extern void xfs_ifork_init_cow(struct xfs_inode *ip);
  220. int xfs_ifork_verify_local_data(struct xfs_inode *ip);
  221. int xfs_ifork_verify_local_attr(struct xfs_inode *ip);
  222. int xfs_iext_count_extend(struct xfs_trans *tp, struct xfs_inode *ip,
  223. int whichfork, uint nr_to_add);
  224. bool xfs_ifork_is_realtime(struct xfs_inode *ip, int whichfork);
  225. /* returns true if the fork has extents but they are not read in yet. */
  226. static inline bool xfs_need_iread_extents(const struct xfs_ifork *ifp)
  227. {
  228. /* see xfs_iformat_{data,attr}_fork() for needextents semantics */
  229. return smp_load_acquire(&ifp->if_needextents) != 0;
  230. }
  231. #endif /* __XFS_INODE_FORK_H__ */