delayed-inode.h 5.3 KB

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