xfs_bmap.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #ifndef __XFS_BMAP_H__
  7. #define __XFS_BMAP_H__
  8. struct getbmap;
  9. struct xfs_bmbt_irec;
  10. struct xfs_ifork;
  11. struct xfs_inode;
  12. struct xfs_mount;
  13. struct xfs_trans;
  14. struct xfs_alloc_arg;
  15. /*
  16. * Argument structure for xfs_bmap_alloc.
  17. */
  18. struct xfs_bmalloca {
  19. struct xfs_trans *tp; /* transaction pointer */
  20. struct xfs_inode *ip; /* incore inode pointer */
  21. struct xfs_bmbt_irec prev; /* extent before the new one */
  22. struct xfs_bmbt_irec got; /* extent after, or delayed */
  23. xfs_fileoff_t offset; /* offset in file filling in */
  24. xfs_extlen_t length; /* i/o length asked/allocated */
  25. xfs_fsblock_t blkno; /* starting block of new extent */
  26. struct xfs_btree_cur *cur; /* btree cursor */
  27. struct xfs_iext_cursor icur; /* incore extent cursor */
  28. int nallocs;/* number of extents alloc'd */
  29. int logflags;/* flags for transaction logging */
  30. xfs_extlen_t total; /* total blocks needed for xaction */
  31. xfs_extlen_t minlen; /* minimum allocation size (blocks) */
  32. xfs_extlen_t minleft; /* amount must be left after alloc */
  33. bool eof; /* set if allocating past last extent */
  34. bool wasdel; /* replacing a delayed allocation */
  35. bool aeof; /* allocated space at eof */
  36. bool conv; /* overwriting unwritten extents */
  37. int datatype;/* data type being allocated */
  38. uint32_t flags;
  39. };
  40. #define XFS_BMAP_MAX_NMAP 4
  41. /*
  42. * Flags for xfs_bmapi_*
  43. */
  44. #define XFS_BMAPI_ENTIRE (1u << 0) /* return entire extent untrimmed */
  45. #define XFS_BMAPI_METADATA (1u << 1) /* mapping metadata not user data */
  46. #define XFS_BMAPI_ATTRFORK (1u << 2) /* use attribute fork not data */
  47. #define XFS_BMAPI_PREALLOC (1u << 3) /* preallocating unwritten space */
  48. #define XFS_BMAPI_CONTIG (1u << 4) /* must allocate only one extent */
  49. /*
  50. * unwritten extent conversion - this needs write cache flushing and no additional
  51. * allocation alignments. When specified with XFS_BMAPI_PREALLOC it converts
  52. * from written to unwritten, otherwise convert from unwritten to written.
  53. */
  54. #define XFS_BMAPI_CONVERT (1u << 5)
  55. /*
  56. * allocate zeroed extents - this requires all newly allocated user data extents
  57. * to be initialised to zero. It will be ignored if XFS_BMAPI_METADATA is set.
  58. * Use in conjunction with XFS_BMAPI_CONVERT to convert unwritten extents found
  59. * during the allocation range to zeroed written extents.
  60. */
  61. #define XFS_BMAPI_ZERO (1u << 6)
  62. /*
  63. * Map the inode offset to the block given in ap->firstblock. Primarily
  64. * used for reflink. The range must be in a hole, and this flag cannot be
  65. * turned on with PREALLOC or CONVERT, and cannot be used on the attr fork.
  66. *
  67. * For bunmapi, this flag unmaps the range without adjusting quota, reducing
  68. * refcount, or freeing the blocks.
  69. */
  70. #define XFS_BMAPI_REMAP (1u << 7)
  71. /* Map something in the CoW fork. */
  72. #define XFS_BMAPI_COWFORK (1u << 8)
  73. /* Skip online discard of freed extents */
  74. #define XFS_BMAPI_NODISCARD (1u << 9)
  75. /* Do not update the rmap btree. Used for reconstructing bmbt from rmapbt. */
  76. #define XFS_BMAPI_NORMAP (1u << 10)
  77. #define XFS_BMAPI_FLAGS \
  78. { XFS_BMAPI_ENTIRE, "ENTIRE" }, \
  79. { XFS_BMAPI_METADATA, "METADATA" }, \
  80. { XFS_BMAPI_ATTRFORK, "ATTRFORK" }, \
  81. { XFS_BMAPI_PREALLOC, "PREALLOC" }, \
  82. { XFS_BMAPI_CONTIG, "CONTIG" }, \
  83. { XFS_BMAPI_CONVERT, "CONVERT" }, \
  84. { XFS_BMAPI_ZERO, "ZERO" }, \
  85. { XFS_BMAPI_REMAP, "REMAP" }, \
  86. { XFS_BMAPI_COWFORK, "COWFORK" }, \
  87. { XFS_BMAPI_NODISCARD, "NODISCARD" }, \
  88. { XFS_BMAPI_NORMAP, "NORMAP" }
  89. static inline int xfs_bmapi_aflag(int w)
  90. {
  91. return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK :
  92. (w == XFS_COW_FORK ? XFS_BMAPI_COWFORK : 0));
  93. }
  94. static inline int xfs_bmapi_whichfork(uint32_t bmapi_flags)
  95. {
  96. if (bmapi_flags & XFS_BMAPI_COWFORK)
  97. return XFS_COW_FORK;
  98. else if (bmapi_flags & XFS_BMAPI_ATTRFORK)
  99. return XFS_ATTR_FORK;
  100. return XFS_DATA_FORK;
  101. }
  102. void xfs_bmap_alloc_account(struct xfs_bmalloca *ap);
  103. /*
  104. * Special values for xfs_bmbt_irec_t br_startblock field.
  105. */
  106. #define DELAYSTARTBLOCK ((xfs_fsblock_t)-1LL)
  107. #define HOLESTARTBLOCK ((xfs_fsblock_t)-2LL)
  108. /*
  109. * Flags for xfs_bmap_add_extent*.
  110. */
  111. #define BMAP_LEFT_CONTIG (1u << 0)
  112. #define BMAP_RIGHT_CONTIG (1u << 1)
  113. #define BMAP_LEFT_FILLING (1u << 2)
  114. #define BMAP_RIGHT_FILLING (1u << 3)
  115. #define BMAP_LEFT_DELAY (1u << 4)
  116. #define BMAP_RIGHT_DELAY (1u << 5)
  117. #define BMAP_LEFT_VALID (1u << 6)
  118. #define BMAP_RIGHT_VALID (1u << 7)
  119. #define BMAP_ATTRFORK (1u << 8)
  120. #define BMAP_COWFORK (1u << 9)
  121. #define XFS_BMAP_EXT_FLAGS \
  122. { BMAP_LEFT_CONTIG, "LC" }, \
  123. { BMAP_RIGHT_CONTIG, "RC" }, \
  124. { BMAP_LEFT_FILLING, "LF" }, \
  125. { BMAP_RIGHT_FILLING, "RF" }, \
  126. { BMAP_ATTRFORK, "ATTR" }, \
  127. { BMAP_COWFORK, "COW" }
  128. /* Return true if the extent is an allocated extent, written or not. */
  129. static inline bool xfs_bmap_is_real_extent(const struct xfs_bmbt_irec *irec)
  130. {
  131. return irec->br_startblock != HOLESTARTBLOCK &&
  132. irec->br_startblock != DELAYSTARTBLOCK &&
  133. !isnullstartblock(irec->br_startblock);
  134. }
  135. /*
  136. * Return true if the extent is a real, allocated extent, or false if it is a
  137. * delayed allocation, and unwritten extent or a hole.
  138. */
  139. static inline bool xfs_bmap_is_written_extent(const struct xfs_bmbt_irec *irec)
  140. {
  141. return xfs_bmap_is_real_extent(irec) &&
  142. irec->br_state != XFS_EXT_UNWRITTEN;
  143. }
  144. /*
  145. * Check the mapping for obviously garbage allocations that could trash the
  146. * filesystem immediately.
  147. */
  148. #define xfs_valid_startblock(ip, startblock) \
  149. ((startblock) != 0 || XFS_IS_REALTIME_INODE(ip))
  150. int xfs_bmap_longest_free_extent(struct xfs_perag *pag,
  151. struct xfs_trans *tp, xfs_extlen_t *blen);
  152. void xfs_trim_extent(struct xfs_bmbt_irec *irec, xfs_fileoff_t bno,
  153. xfs_filblks_t len);
  154. unsigned int xfs_bmap_compute_attr_offset(struct xfs_mount *mp);
  155. int xfs_bmap_add_attrfork(struct xfs_trans *tp, struct xfs_inode *ip,
  156. int size, int rsvd);
  157. void xfs_bmap_local_to_extents_empty(struct xfs_trans *tp,
  158. struct xfs_inode *ip, int whichfork);
  159. int xfs_bmap_local_to_extents(struct xfs_trans *tp, struct xfs_inode *ip,
  160. xfs_extlen_t total, int *logflagsp, int whichfork,
  161. void (*init_fn)(struct xfs_trans *tp, struct xfs_buf *bp,
  162. struct xfs_inode *ip, struct xfs_ifork *ifp,
  163. void *priv),
  164. void *priv);
  165. void xfs_bmap_compute_maxlevels(struct xfs_mount *mp, int whichfork);
  166. int xfs_bmap_first_unused(struct xfs_trans *tp, struct xfs_inode *ip,
  167. xfs_extlen_t len, xfs_fileoff_t *unused, int whichfork);
  168. int xfs_bmap_last_before(struct xfs_trans *tp, struct xfs_inode *ip,
  169. xfs_fileoff_t *last_block, int whichfork);
  170. int xfs_bmap_last_offset(struct xfs_inode *ip, xfs_fileoff_t *unused,
  171. int whichfork);
  172. int xfs_bmapi_read(struct xfs_inode *ip, xfs_fileoff_t bno,
  173. xfs_filblks_t len, struct xfs_bmbt_irec *mval,
  174. int *nmap, uint32_t flags);
  175. int xfs_bmapi_write(struct xfs_trans *tp, struct xfs_inode *ip,
  176. xfs_fileoff_t bno, xfs_filblks_t len, uint32_t flags,
  177. xfs_extlen_t total, struct xfs_bmbt_irec *mval, int *nmap);
  178. int xfs_bunmapi(struct xfs_trans *tp, struct xfs_inode *ip,
  179. xfs_fileoff_t bno, xfs_filblks_t len, uint32_t flags,
  180. xfs_extnum_t nexts, int *done);
  181. void xfs_bmap_del_extent_delay(struct xfs_inode *ip, int whichfork,
  182. struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
  183. struct xfs_bmbt_irec *del);
  184. void xfs_bmap_del_extent_cow(struct xfs_inode *ip,
  185. struct xfs_iext_cursor *cur, struct xfs_bmbt_irec *got,
  186. struct xfs_bmbt_irec *del);
  187. uint xfs_default_attroffset(struct xfs_inode *ip);
  188. int xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
  189. xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
  190. bool *done);
  191. int xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
  192. xfs_fileoff_t shift);
  193. int xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
  194. xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
  195. bool *done, xfs_fileoff_t stop_fsb);
  196. int xfs_bmap_split_extent(struct xfs_trans *tp, struct xfs_inode *ip,
  197. xfs_fileoff_t split_offset);
  198. int xfs_bmapi_reserve_delalloc(struct xfs_inode *ip, int whichfork,
  199. xfs_fileoff_t off, xfs_filblks_t len, xfs_filblks_t prealloc,
  200. struct xfs_bmbt_irec *got, struct xfs_iext_cursor *cur,
  201. int eof);
  202. int xfs_bmapi_convert_delalloc(struct xfs_inode *ip, int whichfork,
  203. xfs_off_t offset, struct iomap *iomap, unsigned int *seq);
  204. int xfs_bmap_add_extent_unwritten_real(struct xfs_trans *tp,
  205. struct xfs_inode *ip, int whichfork,
  206. struct xfs_iext_cursor *icur, struct xfs_btree_cur **curp,
  207. struct xfs_bmbt_irec *new, int *logflagsp);
  208. xfs_extlen_t xfs_bmapi_minleft(struct xfs_trans *tp, struct xfs_inode *ip,
  209. int fork);
  210. int xfs_bmap_btalloc_low_space(struct xfs_bmalloca *ap,
  211. struct xfs_alloc_arg *args);
  212. enum xfs_bmap_intent_type {
  213. XFS_BMAP_MAP = 1,
  214. XFS_BMAP_UNMAP,
  215. };
  216. #define XFS_BMAP_INTENT_STRINGS \
  217. { XFS_BMAP_MAP, "map" }, \
  218. { XFS_BMAP_UNMAP, "unmap" }
  219. struct xfs_bmap_intent {
  220. struct list_head bi_list;
  221. enum xfs_bmap_intent_type bi_type;
  222. int bi_whichfork;
  223. struct xfs_inode *bi_owner;
  224. struct xfs_perag *bi_pag;
  225. struct xfs_bmbt_irec bi_bmap;
  226. };
  227. int xfs_bmap_finish_one(struct xfs_trans *tp, struct xfs_bmap_intent *bi);
  228. void xfs_bmap_map_extent(struct xfs_trans *tp, struct xfs_inode *ip,
  229. int whichfork, struct xfs_bmbt_irec *imap);
  230. void xfs_bmap_unmap_extent(struct xfs_trans *tp, struct xfs_inode *ip,
  231. int whichfork, struct xfs_bmbt_irec *imap);
  232. static inline uint32_t xfs_bmap_fork_to_state(int whichfork)
  233. {
  234. switch (whichfork) {
  235. case XFS_ATTR_FORK:
  236. return BMAP_ATTRFORK;
  237. case XFS_COW_FORK:
  238. return BMAP_COWFORK;
  239. default:
  240. return 0;
  241. }
  242. }
  243. xfs_failaddr_t xfs_bmap_validate_extent_raw(struct xfs_mount *mp, bool rtfile,
  244. int whichfork, struct xfs_bmbt_irec *irec);
  245. xfs_failaddr_t xfs_bmap_validate_extent(struct xfs_inode *ip, int whichfork,
  246. struct xfs_bmbt_irec *irec);
  247. int xfs_bmap_complain_bad_rec(struct xfs_inode *ip, int whichfork,
  248. xfs_failaddr_t fa, const struct xfs_bmbt_irec *irec);
  249. int xfs_bmapi_remap(struct xfs_trans *tp, struct xfs_inode *ip,
  250. xfs_fileoff_t bno, xfs_filblks_t len, xfs_fsblock_t startblock,
  251. uint32_t flags);
  252. int xfs_bunmapi_range(struct xfs_trans **tpp, struct xfs_inode *ip,
  253. uint32_t flags, xfs_fileoff_t startoff, xfs_fileoff_t endoff);
  254. extern struct kmem_cache *xfs_bmap_intent_cache;
  255. int __init xfs_bmap_intent_init_cache(void);
  256. void xfs_bmap_intent_destroy_cache(void);
  257. typedef int (*xfs_bmap_query_range_fn)(
  258. struct xfs_btree_cur *cur,
  259. struct xfs_bmbt_irec *rec,
  260. void *priv);
  261. int xfs_bmap_query_all(struct xfs_btree_cur *cur, xfs_bmap_query_range_fn fn,
  262. void *priv);
  263. xfs_extlen_t xfs_get_extsz_hint(struct xfs_inode *ip);
  264. xfs_extlen_t xfs_get_cowextsz_hint(struct xfs_inode *ip);
  265. #endif /* __XFS_BMAP_H__ */