xfs_bmap.h 9.3 KB

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