delayed-inode.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2011 Fujitsu. All rights reserved.
  4. * Written by Miao Xie <miaox@cn.fujitsu.com>
  5. */
  6. #ifndef BTRFS_DELAYED_INODE_H
  7. #define BTRFS_DELAYED_INODE_H
  8. #include <linux/types.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mutex.h>
  12. #include <linux/list.h>
  13. #include <linux/wait.h>
  14. #include <linux/fs.h>
  15. #include <linux/atomic.h>
  16. #include <linux/refcount.h>
  17. #include "ctree.h"
  18. struct btrfs_disk_key;
  19. struct btrfs_fs_info;
  20. struct btrfs_inode;
  21. struct btrfs_root;
  22. struct btrfs_trans_handle;
  23. enum btrfs_delayed_item_type {
  24. BTRFS_DELAYED_INSERTION_ITEM,
  25. BTRFS_DELAYED_DELETION_ITEM
  26. };
  27. struct btrfs_delayed_root {
  28. spinlock_t lock;
  29. struct list_head node_list;
  30. /*
  31. * Used for delayed nodes which is waiting to be dealt with by the
  32. * worker. If the delayed node is inserted into the work queue, we
  33. * drop it from this list.
  34. */
  35. struct list_head prepare_list;
  36. atomic_t items; /* for delayed items */
  37. atomic_t items_seq; /* for delayed items */
  38. int nodes; /* for delayed nodes */
  39. wait_queue_head_t wait;
  40. };
  41. #define BTRFS_DELAYED_NODE_IN_LIST 0
  42. #define BTRFS_DELAYED_NODE_INODE_DIRTY 1
  43. #define BTRFS_DELAYED_NODE_DEL_IREF 2
  44. struct btrfs_delayed_node {
  45. u64 inode_id;
  46. u64 bytes_reserved;
  47. struct btrfs_root *root;
  48. /* Used to add the node into the delayed root's node list. */
  49. struct list_head n_list;
  50. /*
  51. * Used to add the node into the prepare list, the nodes in this list
  52. * is waiting to be dealt with by the async worker.
  53. */
  54. struct list_head p_list;
  55. struct rb_root_cached ins_root;
  56. struct rb_root_cached del_root;
  57. struct mutex mutex;
  58. struct btrfs_inode_item inode_item;
  59. refcount_t refs;
  60. u64 index_cnt;
  61. unsigned long flags;
  62. int count;
  63. /*
  64. * The size of the next batch of dir index items to insert (if this
  65. * node is from a directory inode). Protected by @mutex.
  66. */
  67. u32 curr_index_batch_size;
  68. /*
  69. * Number of leaves reserved for inserting dir index items (if this
  70. * node belongs to a directory inode). This may be larger then the
  71. * actual number of leaves we end up using. Protected by @mutex.
  72. */
  73. u32 index_item_leaves;
  74. };
  75. struct btrfs_delayed_item {
  76. struct rb_node rb_node;
  77. /* Offset value of the corresponding dir index key. */
  78. u64 index;
  79. struct list_head tree_list; /* used for batch insert/delete items */
  80. struct list_head readdir_list; /* used for readdir items */
  81. /*
  82. * Used when logging a directory.
  83. * Insertions and deletions to this list are protected by the parent
  84. * delayed node's mutex.
  85. */
  86. struct list_head log_list;
  87. u64 bytes_reserved;
  88. struct btrfs_delayed_node *delayed_node;
  89. refcount_t refs;
  90. enum btrfs_delayed_item_type type:8;
  91. /*
  92. * Track if this delayed item was already logged.
  93. * Protected by the mutex of the parent delayed inode.
  94. */
  95. bool logged;
  96. /* The maximum leaf size is 64K, so u16 is more than enough. */
  97. u16 data_len;
  98. char data[] __counted_by(data_len);
  99. };
  100. void btrfs_init_delayed_root(struct btrfs_delayed_root *delayed_root);
  101. int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
  102. const char *name, int name_len,
  103. struct btrfs_inode *dir,
  104. const struct btrfs_disk_key *disk_key, u8 flags,
  105. u64 index);
  106. int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
  107. struct btrfs_inode *dir, u64 index);
  108. int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
  109. int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
  110. int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
  111. void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
  112. int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
  113. struct btrfs_inode *inode);
  114. /* Used for evicting the inode. */
  115. void btrfs_remove_delayed_node(struct btrfs_inode *inode);
  116. void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
  117. int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
  118. int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
  119. struct btrfs_inode *inode);
  120. int btrfs_fill_inode(struct inode *inode, u32 *rdev);
  121. int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
  122. /* Used for drop dead root */
  123. void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
  124. /* Used for clean the transaction */
  125. void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
  126. /* Used for readdir() */
  127. bool btrfs_readdir_get_delayed_items(struct btrfs_inode *inode,
  128. u64 last_index,
  129. struct list_head *ins_list,
  130. struct list_head *del_list);
  131. void btrfs_readdir_put_delayed_items(struct btrfs_inode *inode,
  132. struct list_head *ins_list,
  133. struct list_head *del_list);
  134. int btrfs_should_delete_dir_index(const struct list_head *del_list,
  135. u64 index);
  136. int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
  137. const struct list_head *ins_list);
  138. /* Used during directory logging. */
  139. void btrfs_log_get_delayed_items(struct btrfs_inode *inode,
  140. struct list_head *ins_list,
  141. struct list_head *del_list);
  142. void btrfs_log_put_delayed_items(struct btrfs_inode *inode,
  143. struct list_head *ins_list,
  144. struct list_head *del_list);
  145. /* for init */
  146. int __init btrfs_delayed_inode_init(void);
  147. void __cold btrfs_delayed_inode_exit(void);
  148. /* for debugging */
  149. void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
  150. #endif