delayed-inode.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/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. /* types of the delayed item */
  17. #define BTRFS_DELAYED_INSERTION_ITEM 1
  18. #define BTRFS_DELAYED_DELETION_ITEM 2
  19. struct btrfs_delayed_root {
  20. spinlock_t lock;
  21. struct list_head node_list;
  22. /*
  23. * Used for delayed nodes which is waiting to be dealt with by the
  24. * worker. If the delayed node is inserted into the work queue, we
  25. * drop it from this list.
  26. */
  27. struct list_head prepare_list;
  28. atomic_t items; /* for delayed items */
  29. atomic_t items_seq; /* for delayed items */
  30. int nodes; /* for delayed nodes */
  31. wait_queue_head_t wait;
  32. };
  33. #define BTRFS_DELAYED_NODE_IN_LIST 0
  34. #define BTRFS_DELAYED_NODE_INODE_DIRTY 1
  35. #define BTRFS_DELAYED_NODE_DEL_IREF 2
  36. struct btrfs_delayed_node {
  37. u64 inode_id;
  38. u64 bytes_reserved;
  39. struct btrfs_root *root;
  40. /* Used to add the node into the delayed root's node list. */
  41. struct list_head n_list;
  42. /*
  43. * Used to add the node into the prepare list, the nodes in this list
  44. * is waiting to be dealt with by the async worker.
  45. */
  46. struct list_head p_list;
  47. struct rb_root ins_root;
  48. struct rb_root del_root;
  49. struct mutex mutex;
  50. struct btrfs_inode_item inode_item;
  51. refcount_t refs;
  52. u64 index_cnt;
  53. unsigned long flags;
  54. int count;
  55. };
  56. struct btrfs_delayed_item {
  57. struct rb_node rb_node;
  58. struct btrfs_key key;
  59. struct list_head tree_list; /* used for batch insert/delete items */
  60. struct list_head readdir_list; /* used for readdir items */
  61. u64 bytes_reserved;
  62. struct btrfs_delayed_node *delayed_node;
  63. refcount_t refs;
  64. int ins_or_del;
  65. u32 data_len;
  66. char data[0];
  67. };
  68. static inline void btrfs_init_delayed_root(
  69. struct btrfs_delayed_root *delayed_root)
  70. {
  71. atomic_set(&delayed_root->items, 0);
  72. atomic_set(&delayed_root->items_seq, 0);
  73. delayed_root->nodes = 0;
  74. spin_lock_init(&delayed_root->lock);
  75. init_waitqueue_head(&delayed_root->wait);
  76. INIT_LIST_HEAD(&delayed_root->node_list);
  77. INIT_LIST_HEAD(&delayed_root->prepare_list);
  78. }
  79. int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
  80. const char *name, int name_len,
  81. struct btrfs_inode *dir,
  82. struct btrfs_disk_key *disk_key, u8 type,
  83. u64 index);
  84. int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
  85. struct btrfs_inode *dir, u64 index);
  86. int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode);
  87. int btrfs_run_delayed_items(struct btrfs_trans_handle *trans);
  88. int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr);
  89. void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
  90. int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
  91. struct btrfs_inode *inode);
  92. /* Used for evicting the inode. */
  93. void btrfs_remove_delayed_node(struct btrfs_inode *inode);
  94. void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode);
  95. int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode);
  96. int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
  97. struct btrfs_root *root, struct inode *inode);
  98. int btrfs_fill_inode(struct inode *inode, u32 *rdev);
  99. int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
  100. /* Used for drop dead root */
  101. void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
  102. /* Used for clean the transaction */
  103. void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info);
  104. /* Used for readdir() */
  105. bool btrfs_readdir_get_delayed_items(struct inode *inode,
  106. struct list_head *ins_list,
  107. struct list_head *del_list);
  108. void btrfs_readdir_put_delayed_items(struct inode *inode,
  109. struct list_head *ins_list,
  110. struct list_head *del_list);
  111. int btrfs_should_delete_dir_index(struct list_head *del_list,
  112. u64 index);
  113. int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
  114. struct list_head *ins_list);
  115. /* for init */
  116. int __init btrfs_delayed_inode_init(void);
  117. void __cold btrfs_delayed_inode_exit(void);
  118. /* for debugging */
  119. void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info);
  120. #endif