btrfs_inode.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #ifndef __BTRFS_I__
  19. #define __BTRFS_I__
  20. #include <linux/hash.h>
  21. #include "extent_map.h"
  22. #include "extent_io.h"
  23. #include "ordered-data.h"
  24. #include "delayed-inode.h"
  25. /*
  26. * ordered_data_close is set by truncate when a file that used
  27. * to have good data has been truncated to zero. When it is set
  28. * the btrfs file release call will add this inode to the
  29. * ordered operations list so that we make sure to flush out any
  30. * new data the application may have written before commit.
  31. */
  32. #define BTRFS_INODE_ORDERED_DATA_CLOSE 0
  33. #define BTRFS_INODE_ORPHAN_META_RESERVED 1
  34. #define BTRFS_INODE_DUMMY 2
  35. #define BTRFS_INODE_IN_DEFRAG 3
  36. #define BTRFS_INODE_DELALLOC_META_RESERVED 4
  37. #define BTRFS_INODE_HAS_ORPHAN_ITEM 5
  38. #define BTRFS_INODE_HAS_ASYNC_EXTENT 6
  39. #define BTRFS_INODE_NEEDS_FULL_SYNC 7
  40. #define BTRFS_INODE_COPY_EVERYTHING 8
  41. #define BTRFS_INODE_IN_DELALLOC_LIST 9
  42. #define BTRFS_INODE_READDIO_NEED_LOCK 10
  43. #define BTRFS_INODE_HAS_PROPS 11
  44. /* in memory btrfs inode */
  45. struct btrfs_inode {
  46. /* which subvolume this inode belongs to */
  47. struct btrfs_root *root;
  48. /* key used to find this inode on disk. This is used by the code
  49. * to read in roots of subvolumes
  50. */
  51. struct btrfs_key location;
  52. /* Lock for counters */
  53. spinlock_t lock;
  54. /* the extent_tree has caches of all the extent mappings to disk */
  55. struct extent_map_tree extent_tree;
  56. /* the io_tree does range state (DIRTY, LOCKED etc) */
  57. struct extent_io_tree io_tree;
  58. /* special utility tree used to record which mirrors have already been
  59. * tried when checksums fail for a given block
  60. */
  61. struct extent_io_tree io_failure_tree;
  62. /* held while logging the inode in tree-log.c */
  63. struct mutex log_mutex;
  64. /* held while doing delalloc reservations */
  65. struct mutex delalloc_mutex;
  66. /* used to order data wrt metadata */
  67. struct btrfs_ordered_inode_tree ordered_tree;
  68. /* list of all the delalloc inodes in the FS. There are times we need
  69. * to write all the delalloc pages to disk, and this list is used
  70. * to walk them all.
  71. */
  72. struct list_head delalloc_inodes;
  73. /*
  74. * list for tracking inodes that must be sent to disk before a
  75. * rename or truncate commit
  76. */
  77. struct list_head ordered_operations;
  78. /* node for the red-black tree that links inodes in subvolume root */
  79. struct rb_node rb_node;
  80. unsigned long runtime_flags;
  81. /* Keep track of who's O_SYNC/fsyncing currently */
  82. atomic_t sync_writers;
  83. /* full 64 bit generation number, struct vfs_inode doesn't have a big
  84. * enough field for this.
  85. */
  86. u64 generation;
  87. /*
  88. * transid of the trans_handle that last modified this inode
  89. */
  90. u64 last_trans;
  91. /*
  92. * transid that last logged this inode
  93. */
  94. u64 logged_trans;
  95. /*
  96. * log transid when this inode was last modified
  97. */
  98. int last_sub_trans;
  99. /* a local copy of root's last_log_commit */
  100. int last_log_commit;
  101. /* total number of bytes pending delalloc, used by stat to calc the
  102. * real block usage of the file
  103. */
  104. u64 delalloc_bytes;
  105. /*
  106. * the size of the file stored in the metadata on disk. data=ordered
  107. * means the in-memory i_size might be larger than the size on disk
  108. * because not all the blocks are written yet.
  109. */
  110. u64 disk_i_size;
  111. /*
  112. * if this is a directory then index_cnt is the counter for the index
  113. * number for new files that are created
  114. */
  115. u64 index_cnt;
  116. /* Cache the directory index number to speed the dir/file remove */
  117. u64 dir_index;
  118. /* the fsync log has some corner cases that mean we have to check
  119. * directories to see if any unlinks have been done before
  120. * the directory was logged. See tree-log.c for all the
  121. * details
  122. */
  123. u64 last_unlink_trans;
  124. /*
  125. * Number of bytes outstanding that are going to need csums. This is
  126. * used in ENOSPC accounting.
  127. */
  128. u64 csum_bytes;
  129. /* flags field from the on disk inode */
  130. u32 flags;
  131. /*
  132. * Counters to keep track of the number of extent item's we may use due
  133. * to delalloc and such. outstanding_extents is the number of extent
  134. * items we think we'll end up using, and reserved_extents is the number
  135. * of extent items we've reserved metadata for.
  136. */
  137. unsigned outstanding_extents;
  138. unsigned reserved_extents;
  139. /*
  140. * always compress this one file
  141. */
  142. unsigned force_compress;
  143. struct btrfs_delayed_node *delayed_node;
  144. struct inode vfs_inode;
  145. };
  146. extern unsigned char btrfs_filetype_table[];
  147. static inline struct btrfs_inode *BTRFS_I(struct inode *inode)
  148. {
  149. return container_of(inode, struct btrfs_inode, vfs_inode);
  150. }
  151. static inline unsigned long btrfs_inode_hash(u64 objectid,
  152. const struct btrfs_root *root)
  153. {
  154. u64 h = objectid ^ (root->objectid * GOLDEN_RATIO_PRIME);
  155. #if BITS_PER_LONG == 32
  156. h = (h >> 32) ^ (h & 0xffffffff);
  157. #endif
  158. return (unsigned long)h;
  159. }
  160. static inline void btrfs_insert_inode_hash(struct inode *inode)
  161. {
  162. unsigned long h = btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root);
  163. __insert_inode_hash(inode, h);
  164. }
  165. static inline u64 btrfs_ino(struct inode *inode)
  166. {
  167. u64 ino = BTRFS_I(inode)->location.objectid;
  168. /*
  169. * !ino: btree_inode
  170. * type == BTRFS_ROOT_ITEM_KEY: subvol dir
  171. */
  172. if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
  173. ino = inode->i_ino;
  174. return ino;
  175. }
  176. static inline void btrfs_i_size_write(struct inode *inode, u64 size)
  177. {
  178. i_size_write(inode, size);
  179. BTRFS_I(inode)->disk_i_size = size;
  180. }
  181. static inline bool btrfs_is_free_space_inode(struct inode *inode)
  182. {
  183. struct btrfs_root *root = BTRFS_I(inode)->root;
  184. if (root == root->fs_info->tree_root &&
  185. btrfs_ino(inode) != BTRFS_BTREE_INODE_OBJECTID)
  186. return true;
  187. if (BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
  188. return true;
  189. return false;
  190. }
  191. static inline int btrfs_inode_in_log(struct inode *inode, u64 generation)
  192. {
  193. if (BTRFS_I(inode)->logged_trans == generation &&
  194. BTRFS_I(inode)->last_sub_trans <=
  195. BTRFS_I(inode)->last_log_commit &&
  196. BTRFS_I(inode)->last_sub_trans <=
  197. BTRFS_I(inode)->root->last_log_commit)
  198. return 1;
  199. return 0;
  200. }
  201. struct btrfs_dio_private {
  202. struct inode *inode;
  203. u64 logical_offset;
  204. u64 disk_bytenr;
  205. u64 bytes;
  206. void *private;
  207. /* number of bios pending for this dio */
  208. atomic_t pending_bios;
  209. /* IO errors */
  210. int errors;
  211. /* orig_bio is our btrfs_io_bio */
  212. struct bio *orig_bio;
  213. /* dio_bio came from fs/direct-io.c */
  214. struct bio *dio_bio;
  215. u8 csum[0];
  216. };
  217. /*
  218. * Disable DIO read nolock optimization, so new dio readers will be forced
  219. * to grab i_mutex. It is used to avoid the endless truncate due to
  220. * nonlocked dio read.
  221. */
  222. static inline void btrfs_inode_block_unlocked_dio(struct inode *inode)
  223. {
  224. set_bit(BTRFS_INODE_READDIO_NEED_LOCK, &BTRFS_I(inode)->runtime_flags);
  225. smp_mb();
  226. }
  227. static inline void btrfs_inode_resume_unlocked_dio(struct inode *inode)
  228. {
  229. smp_mb__before_clear_bit();
  230. clear_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  231. &BTRFS_I(inode)->runtime_flags);
  232. }
  233. #endif