alloc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * alloc.h
  4. *
  5. * Function prototypes
  6. *
  7. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  8. */
  9. #ifndef OCFS2_ALLOC_H
  10. #define OCFS2_ALLOC_H
  11. /*
  12. * For xattr tree leaf, we limit the leaf byte size to be 64K.
  13. */
  14. #define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
  15. /*
  16. * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
  17. * the b-tree operations in ocfs2. Now all the b-tree operations are not
  18. * limited to ocfs2_dinode only. Any data which need to allocate clusters
  19. * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
  20. * and operation.
  21. *
  22. * ocfs2_extent_tree becomes the first-class object for extent tree
  23. * manipulation. Callers of the alloc.c code need to fill it via one of
  24. * the ocfs2_init_*_extent_tree() operations below.
  25. *
  26. * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
  27. * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
  28. * functions. It needs the ocfs2_caching_info structure associated with
  29. * I/O on the tree. With metadata ecc, we now call different journal_access
  30. * functions for each type of metadata, so it must have the
  31. * root_journal_access function.
  32. * ocfs2_extent_tree_operations abstract the normal operations we do for
  33. * the root of extent b-tree.
  34. */
  35. struct ocfs2_extent_tree_operations;
  36. struct ocfs2_extent_tree {
  37. const struct ocfs2_extent_tree_operations *et_ops;
  38. struct buffer_head *et_root_bh;
  39. struct ocfs2_extent_list *et_root_el;
  40. struct ocfs2_caching_info *et_ci;
  41. ocfs2_journal_access_func et_root_journal_access;
  42. void *et_object;
  43. unsigned int et_max_leaf_clusters;
  44. struct ocfs2_cached_dealloc_ctxt *et_dealloc;
  45. };
  46. /*
  47. * ocfs2_init_*_extent_tree() will fill an ocfs2_extent_tree from the
  48. * specified object buffer.
  49. */
  50. void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
  51. struct ocfs2_caching_info *ci,
  52. struct buffer_head *bh);
  53. void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
  54. struct ocfs2_caching_info *ci,
  55. struct buffer_head *bh);
  56. struct ocfs2_xattr_value_buf;
  57. void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
  58. struct ocfs2_caching_info *ci,
  59. struct ocfs2_xattr_value_buf *vb);
  60. void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
  61. struct ocfs2_caching_info *ci,
  62. struct buffer_head *bh);
  63. void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
  64. struct ocfs2_caching_info *ci,
  65. struct buffer_head *bh);
  66. /*
  67. * Read an extent block into *bh. If *bh is NULL, a bh will be
  68. * allocated. This is a cached read. The extent block will be validated
  69. * with ocfs2_validate_extent_block().
  70. */
  71. int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
  72. struct buffer_head **bh);
  73. struct ocfs2_alloc_context;
  74. int ocfs2_insert_extent(handle_t *handle,
  75. struct ocfs2_extent_tree *et,
  76. u32 cpos,
  77. u64 start_blk,
  78. u32 new_clusters,
  79. u8 flags,
  80. struct ocfs2_alloc_context *meta_ac);
  81. enum ocfs2_alloc_restarted {
  82. RESTART_NONE = 0,
  83. RESTART_TRANS,
  84. RESTART_META
  85. };
  86. int ocfs2_add_clusters_in_btree(handle_t *handle,
  87. struct ocfs2_extent_tree *et,
  88. u32 *logical_offset,
  89. u32 clusters_to_add,
  90. int mark_unwritten,
  91. struct ocfs2_alloc_context *data_ac,
  92. struct ocfs2_alloc_context *meta_ac,
  93. enum ocfs2_alloc_restarted *reason_ret);
  94. struct ocfs2_cached_dealloc_ctxt;
  95. struct ocfs2_path;
  96. int ocfs2_split_extent(handle_t *handle,
  97. struct ocfs2_extent_tree *et,
  98. struct ocfs2_path *path,
  99. int split_index,
  100. struct ocfs2_extent_rec *split_rec,
  101. struct ocfs2_alloc_context *meta_ac,
  102. struct ocfs2_cached_dealloc_ctxt *dealloc);
  103. int ocfs2_mark_extent_written(struct inode *inode,
  104. struct ocfs2_extent_tree *et,
  105. handle_t *handle, u32 cpos, u32 len, u32 phys,
  106. struct ocfs2_alloc_context *meta_ac,
  107. struct ocfs2_cached_dealloc_ctxt *dealloc);
  108. int ocfs2_change_extent_flag(handle_t *handle,
  109. struct ocfs2_extent_tree *et,
  110. u32 cpos, u32 len, u32 phys,
  111. struct ocfs2_alloc_context *meta_ac,
  112. struct ocfs2_cached_dealloc_ctxt *dealloc,
  113. int new_flags, int clear_flags);
  114. int ocfs2_remove_extent(handle_t *handle, struct ocfs2_extent_tree *et,
  115. u32 cpos, u32 len,
  116. struct ocfs2_alloc_context *meta_ac,
  117. struct ocfs2_cached_dealloc_ctxt *dealloc);
  118. int ocfs2_remove_btree_range(struct inode *inode,
  119. struct ocfs2_extent_tree *et,
  120. u32 cpos, u32 phys_cpos, u32 len, int flags,
  121. struct ocfs2_cached_dealloc_ctxt *dealloc,
  122. u64 refcount_loc, bool refcount_tree_locked);
  123. int ocfs2_num_free_extents(struct ocfs2_extent_tree *et);
  124. /*
  125. * how many new metadata chunks would an allocation need at maximum?
  126. *
  127. * Please note that the caller must make sure that root_el is the root
  128. * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
  129. * the result may be wrong.
  130. */
  131. static inline int ocfs2_extend_meta_needed(struct ocfs2_extent_list *root_el)
  132. {
  133. /*
  134. * Rather than do all the work of determining how much we need
  135. * (involves a ton of reads and locks), just ask for the
  136. * maximal limit. That's a tree depth shift. So, one block for
  137. * level of the tree (current l_tree_depth), one block for the
  138. * new tree_depth==0 extent_block, and one block at the new
  139. * top-of-the tree.
  140. */
  141. return le16_to_cpu(root_el->l_tree_depth) + 2;
  142. }
  143. void ocfs2_dinode_new_extent_list(struct inode *inode, struct ocfs2_dinode *di);
  144. void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di);
  145. int ocfs2_convert_inline_data_to_extents(struct inode *inode,
  146. struct buffer_head *di_bh);
  147. int ocfs2_truncate_log_init(struct ocfs2_super *osb);
  148. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb);
  149. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  150. int cancel);
  151. int ocfs2_flush_truncate_log(struct ocfs2_super *osb);
  152. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  153. int slot_num,
  154. struct ocfs2_dinode **tl_copy);
  155. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  156. struct ocfs2_dinode *tl_copy);
  157. int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb);
  158. int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  159. handle_t *handle,
  160. u64 start_blk,
  161. unsigned int num_clusters);
  162. int __ocfs2_flush_truncate_log(struct ocfs2_super *osb);
  163. int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
  164. unsigned int needed);
  165. /*
  166. * Process local structure which describes the block unlinks done
  167. * during an operation. This is populated via
  168. * ocfs2_cache_block_dealloc().
  169. *
  170. * ocfs2_run_deallocs() should be called after the potentially
  171. * de-allocating routines. No journal handles should be open, and most
  172. * locks should have been dropped.
  173. */
  174. struct ocfs2_cached_dealloc_ctxt {
  175. struct ocfs2_per_slot_free_list *c_first_suballocator;
  176. struct ocfs2_cached_block_free *c_global_allocator;
  177. };
  178. static inline void ocfs2_init_dealloc_ctxt(struct ocfs2_cached_dealloc_ctxt *c)
  179. {
  180. c->c_first_suballocator = NULL;
  181. c->c_global_allocator = NULL;
  182. }
  183. int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  184. u64 blkno, unsigned int bit);
  185. int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  186. int type, int slot, u64 suballoc, u64 blkno,
  187. unsigned int bit);
  188. static inline int ocfs2_dealloc_has_cluster(struct ocfs2_cached_dealloc_ctxt *c)
  189. {
  190. return c->c_global_allocator != NULL;
  191. }
  192. int ocfs2_run_deallocs(struct ocfs2_super *osb,
  193. struct ocfs2_cached_dealloc_ctxt *ctxt);
  194. struct ocfs2_truncate_context {
  195. struct ocfs2_cached_dealloc_ctxt tc_dealloc;
  196. int tc_ext_alloc_locked; /* is it cluster locked? */
  197. /* these get destroyed once it's passed to ocfs2_commit_truncate. */
  198. struct buffer_head *tc_last_eb_bh;
  199. };
  200. int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
  201. u64 range_start, u64 range_end);
  202. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  203. struct inode *inode,
  204. struct buffer_head *di_bh);
  205. int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
  206. unsigned int start, unsigned int end, int trunc);
  207. int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
  208. struct ocfs2_extent_list *root_el, u32 cpos,
  209. struct buffer_head **leaf_bh);
  210. int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster);
  211. int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range);
  212. /*
  213. * Helper function to look at the # of clusters in an extent record.
  214. */
  215. static inline unsigned int ocfs2_rec_clusters(struct ocfs2_extent_list *el,
  216. struct ocfs2_extent_rec *rec)
  217. {
  218. /*
  219. * Cluster count in extent records is slightly different
  220. * between interior nodes and leaf nodes. This is to support
  221. * unwritten extents which need a flags field in leaf node
  222. * records, thus shrinking the available space for a clusters
  223. * field.
  224. */
  225. if (el->l_tree_depth)
  226. return le32_to_cpu(rec->e_int_clusters);
  227. else
  228. return le16_to_cpu(rec->e_leaf_clusters);
  229. }
  230. /*
  231. * This is only valid for leaf nodes, which are the only ones that can
  232. * have empty extents anyway.
  233. */
  234. static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
  235. {
  236. return !rec->e_leaf_clusters;
  237. }
  238. int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
  239. struct page **pages, int *num);
  240. void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
  241. unsigned int from, unsigned int to,
  242. struct page *page, int zero, u64 *phys);
  243. /*
  244. * Structures which describe a path through a btree, and functions to
  245. * manipulate them.
  246. *
  247. * The idea here is to be as generic as possible with the tree
  248. * manipulation code.
  249. */
  250. struct ocfs2_path_item {
  251. struct buffer_head *bh;
  252. struct ocfs2_extent_list *el;
  253. };
  254. #define OCFS2_MAX_PATH_DEPTH 5
  255. struct ocfs2_path {
  256. int p_tree_depth;
  257. ocfs2_journal_access_func p_root_access;
  258. struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
  259. };
  260. #define path_root_bh(_path) ((_path)->p_node[0].bh)
  261. #define path_root_el(_path) ((_path)->p_node[0].el)
  262. #define path_root_access(_path)((_path)->p_root_access)
  263. #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
  264. #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
  265. #define path_num_items(_path) ((_path)->p_tree_depth + 1)
  266. void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root);
  267. void ocfs2_free_path(struct ocfs2_path *path);
  268. int ocfs2_find_path(struct ocfs2_caching_info *ci,
  269. struct ocfs2_path *path,
  270. u32 cpos);
  271. struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path);
  272. struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et);
  273. int ocfs2_path_bh_journal_access(handle_t *handle,
  274. struct ocfs2_caching_info *ci,
  275. struct ocfs2_path *path,
  276. int idx);
  277. int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
  278. handle_t *handle,
  279. struct ocfs2_path *path);
  280. int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
  281. struct ocfs2_path *path, u32 *cpos);
  282. int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  283. struct ocfs2_path *path, u32 *cpos);
  284. int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
  285. struct ocfs2_path *left,
  286. struct ocfs2_path *right);
  287. #endif /* OCFS2_ALLOC_H */