delayed-inode.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2011 Fujitsu. All rights reserved.
  3. * Written by Miao Xie <miaox@cn.fujitsu.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 021110-1307, USA.
  18. */
  19. #ifndef __DELAYED_TREE_OPERATION_H
  20. #define __DELAYED_TREE_OPERATION_H
  21. #include <linux/rbtree.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/mutex.h>
  24. #include <linux/list.h>
  25. #include <linux/wait.h>
  26. #include <linux/atomic.h>
  27. #include "ctree.h"
  28. /* types of the delayed item */
  29. #define BTRFS_DELAYED_INSERTION_ITEM 1
  30. #define BTRFS_DELAYED_DELETION_ITEM 2
  31. struct btrfs_delayed_root {
  32. spinlock_t lock;
  33. struct list_head node_list;
  34. /*
  35. * Used for delayed nodes which is waiting to be dealt with by the
  36. * worker. If the delayed node is inserted into the work queue, we
  37. * drop it from this list.
  38. */
  39. struct list_head prepare_list;
  40. atomic_t items; /* for delayed items */
  41. atomic_t items_seq; /* for delayed items */
  42. int nodes; /* for delayed nodes */
  43. wait_queue_head_t wait;
  44. };
  45. struct btrfs_delayed_node {
  46. u64 inode_id;
  47. u64 bytes_reserved;
  48. struct btrfs_root *root;
  49. /* Used to add the node into the delayed root's node list. */
  50. struct list_head n_list;
  51. /*
  52. * Used to add the node into the prepare list, the nodes in this list
  53. * is waiting to be dealt with by the async worker.
  54. */
  55. struct list_head p_list;
  56. struct rb_root ins_root;
  57. struct rb_root del_root;
  58. struct mutex mutex;
  59. struct btrfs_inode_item inode_item;
  60. atomic_t refs;
  61. u64 index_cnt;
  62. bool in_list;
  63. bool inode_dirty;
  64. int count;
  65. };
  66. struct btrfs_delayed_item {
  67. struct rb_node rb_node;
  68. struct btrfs_key key;
  69. struct list_head tree_list; /* used for batch insert/delete items */
  70. struct list_head readdir_list; /* used for readdir items */
  71. u64 bytes_reserved;
  72. struct btrfs_delayed_node *delayed_node;
  73. atomic_t refs;
  74. int ins_or_del;
  75. u32 data_len;
  76. char data[0];
  77. };
  78. static inline void btrfs_init_delayed_root(
  79. struct btrfs_delayed_root *delayed_root)
  80. {
  81. atomic_set(&delayed_root->items, 0);
  82. atomic_set(&delayed_root->items_seq, 0);
  83. delayed_root->nodes = 0;
  84. spin_lock_init(&delayed_root->lock);
  85. init_waitqueue_head(&delayed_root->wait);
  86. INIT_LIST_HEAD(&delayed_root->node_list);
  87. INIT_LIST_HEAD(&delayed_root->prepare_list);
  88. }
  89. int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
  90. struct btrfs_root *root, const char *name,
  91. int name_len, struct inode *dir,
  92. struct btrfs_disk_key *disk_key, u8 type,
  93. u64 index);
  94. int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
  95. struct btrfs_root *root, struct inode *dir,
  96. u64 index);
  97. int btrfs_inode_delayed_dir_index_count(struct inode *inode);
  98. int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root);
  100. int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
  101. struct btrfs_root *root, int nr);
  102. void btrfs_balance_delayed_items(struct btrfs_root *root);
  103. int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
  104. struct inode *inode);
  105. /* Used for evicting the inode. */
  106. void btrfs_remove_delayed_node(struct inode *inode);
  107. void btrfs_kill_delayed_inode_items(struct inode *inode);
  108. int btrfs_commit_inode_delayed_inode(struct inode *inode);
  109. int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
  110. struct btrfs_root *root, struct inode *inode);
  111. int btrfs_fill_inode(struct inode *inode, u32 *rdev);
  112. /* Used for drop dead root */
  113. void btrfs_kill_all_delayed_nodes(struct btrfs_root *root);
  114. /* Used for clean the transaction */
  115. void btrfs_destroy_delayed_inodes(struct btrfs_root *root);
  116. /* Used for readdir() */
  117. void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
  118. struct list_head *del_list);
  119. void btrfs_put_delayed_items(struct list_head *ins_list,
  120. struct list_head *del_list);
  121. int btrfs_should_delete_dir_index(struct list_head *del_list,
  122. u64 index);
  123. int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
  124. struct list_head *ins_list);
  125. /* for init */
  126. int __init btrfs_delayed_inode_init(void);
  127. void btrfs_delayed_inode_exit(void);
  128. /* for debugging */
  129. void btrfs_assert_delayed_root_empty(struct btrfs_root *root);
  130. #endif