btrfs_inode.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_INODE_H
  6. #define BTRFS_INODE_H
  7. #include <linux/hash.h>
  8. #include <linux/refcount.h>
  9. #include "extent_map.h"
  10. #include "extent_io.h"
  11. #include "ordered-data.h"
  12. #include "delayed-inode.h"
  13. /*
  14. * Since we search a directory based on f_pos (struct dir_context::pos) we have
  15. * to start at 2 since '.' and '..' have f_pos of 0 and 1 respectively, so
  16. * everybody else has to start at 2 (see btrfs_real_readdir() and dir_emit_dots()).
  17. */
  18. #define BTRFS_DIR_START_INDEX 2
  19. /*
  20. * ordered_data_close is set by truncate when a file that used
  21. * to have good data has been truncated to zero. When it is set
  22. * the btrfs file release call will add this inode to the
  23. * ordered operations list so that we make sure to flush out any
  24. * new data the application may have written before commit.
  25. */
  26. enum {
  27. BTRFS_INODE_FLUSH_ON_CLOSE,
  28. BTRFS_INODE_DUMMY,
  29. BTRFS_INODE_IN_DEFRAG,
  30. BTRFS_INODE_HAS_ASYNC_EXTENT,
  31. /*
  32. * Always set under the VFS' inode lock, otherwise it can cause races
  33. * during fsync (we start as a fast fsync and then end up in a full
  34. * fsync racing with ordered extent completion).
  35. */
  36. BTRFS_INODE_NEEDS_FULL_SYNC,
  37. BTRFS_INODE_COPY_EVERYTHING,
  38. BTRFS_INODE_IN_DELALLOC_LIST,
  39. BTRFS_INODE_HAS_PROPS,
  40. BTRFS_INODE_SNAPSHOT_FLUSH,
  41. /*
  42. * Set and used when logging an inode and it serves to signal that an
  43. * inode does not have xattrs, so subsequent fsyncs can avoid searching
  44. * for xattrs to log. This bit must be cleared whenever a xattr is added
  45. * to an inode.
  46. */
  47. BTRFS_INODE_NO_XATTRS,
  48. /*
  49. * Set when we are in a context where we need to start a transaction and
  50. * have dirty pages with the respective file range locked. This is to
  51. * ensure that when reserving space for the transaction, if we are low
  52. * on available space and need to flush delalloc, we will not flush
  53. * delalloc for this inode, because that could result in a deadlock (on
  54. * the file range, inode's io_tree).
  55. */
  56. BTRFS_INODE_NO_DELALLOC_FLUSH,
  57. /*
  58. * Set when we are working on enabling verity for a file. Computing and
  59. * writing the whole Merkle tree can take a while so we want to prevent
  60. * races where two separate tasks attempt to simultaneously start verity
  61. * on the same file.
  62. */
  63. BTRFS_INODE_VERITY_IN_PROGRESS,
  64. /* Set when this inode is a free space inode. */
  65. BTRFS_INODE_FREE_SPACE_INODE,
  66. };
  67. /* in memory btrfs inode */
  68. struct btrfs_inode {
  69. /* which subvolume this inode belongs to */
  70. struct btrfs_root *root;
  71. /* key used to find this inode on disk. This is used by the code
  72. * to read in roots of subvolumes
  73. */
  74. struct btrfs_key location;
  75. /*
  76. * Lock for counters and all fields used to determine if the inode is in
  77. * the log or not (last_trans, last_sub_trans, last_log_commit,
  78. * logged_trans), to access/update new_delalloc_bytes and to update the
  79. * VFS' inode number of bytes used.
  80. */
  81. spinlock_t lock;
  82. /* the extent_tree has caches of all the extent mappings to disk */
  83. struct extent_map_tree extent_tree;
  84. /* the io_tree does range state (DIRTY, LOCKED etc) */
  85. struct extent_io_tree io_tree;
  86. /* special utility tree used to record which mirrors have already been
  87. * tried when checksums fail for a given block
  88. */
  89. struct rb_root io_failure_tree;
  90. spinlock_t io_failure_lock;
  91. /*
  92. * Keep track of where the inode has extent items mapped in order to
  93. * make sure the i_size adjustments are accurate
  94. */
  95. struct extent_io_tree file_extent_tree;
  96. /* held while logging the inode in tree-log.c */
  97. struct mutex log_mutex;
  98. /* used to order data wrt metadata */
  99. struct btrfs_ordered_inode_tree ordered_tree;
  100. /* list of all the delalloc inodes in the FS. There are times we need
  101. * to write all the delalloc pages to disk, and this list is used
  102. * to walk them all.
  103. */
  104. struct list_head delalloc_inodes;
  105. /* node for the red-black tree that links inodes in subvolume root */
  106. struct rb_node rb_node;
  107. unsigned long runtime_flags;
  108. /* Keep track of who's O_SYNC/fsyncing currently */
  109. atomic_t sync_writers;
  110. /* full 64 bit generation number, struct vfs_inode doesn't have a big
  111. * enough field for this.
  112. */
  113. u64 generation;
  114. /*
  115. * transid of the trans_handle that last modified this inode
  116. */
  117. u64 last_trans;
  118. /*
  119. * transid that last logged this inode
  120. */
  121. u64 logged_trans;
  122. /*
  123. * log transid when this inode was last modified
  124. */
  125. int last_sub_trans;
  126. /* a local copy of root's last_log_commit */
  127. int last_log_commit;
  128. /*
  129. * Total number of bytes pending delalloc, used by stat to calculate the
  130. * real block usage of the file. This is used only for files.
  131. */
  132. u64 delalloc_bytes;
  133. union {
  134. /*
  135. * Total number of bytes pending delalloc that fall within a file
  136. * range that is either a hole or beyond EOF (and no prealloc extent
  137. * exists in the range). This is always <= delalloc_bytes and this
  138. * is used only for files.
  139. */
  140. u64 new_delalloc_bytes;
  141. /*
  142. * The offset of the last dir index key that was logged.
  143. * This is used only for directories.
  144. */
  145. u64 last_dir_index_offset;
  146. };
  147. /*
  148. * total number of bytes pending defrag, used by stat to check whether
  149. * it needs COW.
  150. */
  151. u64 defrag_bytes;
  152. /*
  153. * the size of the file stored in the metadata on disk. data=ordered
  154. * means the in-memory i_size might be larger than the size on disk
  155. * because not all the blocks are written yet.
  156. */
  157. u64 disk_i_size;
  158. /*
  159. * If this is a directory then index_cnt is the counter for the index
  160. * number for new files that are created. For an empty directory, this
  161. * must be initialized to BTRFS_DIR_START_INDEX.
  162. */
  163. u64 index_cnt;
  164. /* Cache the directory index number to speed the dir/file remove */
  165. u64 dir_index;
  166. /* the fsync log has some corner cases that mean we have to check
  167. * directories to see if any unlinks have been done before
  168. * the directory was logged. See tree-log.c for all the
  169. * details
  170. */
  171. u64 last_unlink_trans;
  172. /*
  173. * The id/generation of the last transaction where this inode was
  174. * either the source or the destination of a clone/dedupe operation.
  175. * Used when logging an inode to know if there are shared extents that
  176. * need special care when logging checksum items, to avoid duplicate
  177. * checksum items in a log (which can lead to a corruption where we end
  178. * up with missing checksum ranges after log replay).
  179. * Protected by the vfs inode lock.
  180. */
  181. u64 last_reflink_trans;
  182. /*
  183. * Number of bytes outstanding that are going to need csums. This is
  184. * used in ENOSPC accounting.
  185. */
  186. u64 csum_bytes;
  187. /* Backwards incompatible flags, lower half of inode_item::flags */
  188. u32 flags;
  189. /* Read-only compatibility flags, upper half of inode_item::flags */
  190. u32 ro_flags;
  191. /*
  192. * Counters to keep track of the number of extent item's we may use due
  193. * to delalloc and such. outstanding_extents is the number of extent
  194. * items we think we'll end up using, and reserved_extents is the number
  195. * of extent items we've reserved metadata for.
  196. */
  197. unsigned outstanding_extents;
  198. struct btrfs_block_rsv block_rsv;
  199. /*
  200. * Cached values of inode properties
  201. */
  202. unsigned prop_compress; /* per-file compression algorithm */
  203. /*
  204. * Force compression on the file using the defrag ioctl, could be
  205. * different from prop_compress and takes precedence if set
  206. */
  207. unsigned defrag_compress;
  208. struct btrfs_delayed_node *delayed_node;
  209. /* File creation time. */
  210. struct timespec64 i_otime;
  211. /* Hook into fs_info->delayed_iputs */
  212. struct list_head delayed_iput;
  213. struct rw_semaphore i_mmap_lock;
  214. struct inode vfs_inode;
  215. };
  216. static inline struct btrfs_inode *BTRFS_I(const struct inode *inode)
  217. {
  218. return container_of(inode, struct btrfs_inode, vfs_inode);
  219. }
  220. static inline unsigned long btrfs_inode_hash(u64 objectid,
  221. const struct btrfs_root *root)
  222. {
  223. u64 h = objectid ^ (root->root_key.objectid * GOLDEN_RATIO_PRIME);
  224. #if BITS_PER_LONG == 32
  225. h = (h >> 32) ^ (h & 0xffffffff);
  226. #endif
  227. return (unsigned long)h;
  228. }
  229. #if BITS_PER_LONG == 32
  230. /*
  231. * On 32 bit systems the i_ino of struct inode is 32 bits (unsigned long), so
  232. * we use the inode's location objectid which is a u64 to avoid truncation.
  233. */
  234. static inline u64 btrfs_ino(const struct btrfs_inode *inode)
  235. {
  236. u64 ino = inode->location.objectid;
  237. /* type == BTRFS_ROOT_ITEM_KEY: subvol dir */
  238. if (inode->location.type == BTRFS_ROOT_ITEM_KEY)
  239. ino = inode->vfs_inode.i_ino;
  240. return ino;
  241. }
  242. #else
  243. static inline u64 btrfs_ino(const struct btrfs_inode *inode)
  244. {
  245. return inode->vfs_inode.i_ino;
  246. }
  247. #endif
  248. static inline void btrfs_i_size_write(struct btrfs_inode *inode, u64 size)
  249. {
  250. i_size_write(&inode->vfs_inode, size);
  251. inode->disk_i_size = size;
  252. }
  253. static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
  254. {
  255. return test_bit(BTRFS_INODE_FREE_SPACE_INODE, &inode->runtime_flags);
  256. }
  257. static inline bool is_data_inode(struct inode *inode)
  258. {
  259. return btrfs_ino(BTRFS_I(inode)) != BTRFS_BTREE_INODE_OBJECTID;
  260. }
  261. static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
  262. int mod)
  263. {
  264. lockdep_assert_held(&inode->lock);
  265. inode->outstanding_extents += mod;
  266. if (btrfs_is_free_space_inode(inode))
  267. return;
  268. trace_btrfs_inode_mod_outstanding_extents(inode->root, btrfs_ino(inode),
  269. mod);
  270. }
  271. /*
  272. * Called every time after doing a buffered, direct IO or memory mapped write.
  273. *
  274. * This is to ensure that if we write to a file that was previously fsynced in
  275. * the current transaction, then try to fsync it again in the same transaction,
  276. * we will know that there were changes in the file and that it needs to be
  277. * logged.
  278. */
  279. static inline void btrfs_set_inode_last_sub_trans(struct btrfs_inode *inode)
  280. {
  281. spin_lock(&inode->lock);
  282. inode->last_sub_trans = inode->root->log_transid;
  283. spin_unlock(&inode->lock);
  284. }
  285. /*
  286. * Should be called while holding the inode's VFS lock in exclusive mode or in a
  287. * context where no one else can access the inode concurrently (during inode
  288. * creation or when loading an inode from disk).
  289. */
  290. static inline void btrfs_set_inode_full_sync(struct btrfs_inode *inode)
  291. {
  292. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
  293. /*
  294. * The inode may have been part of a reflink operation in the last
  295. * transaction that modified it, and then a fsync has reset the
  296. * last_reflink_trans to avoid subsequent fsyncs in the same
  297. * transaction to do unnecessary work. So update last_reflink_trans
  298. * to the last_trans value (we have to be pessimistic and assume a
  299. * reflink happened).
  300. *
  301. * The ->last_trans is protected by the inode's spinlock and we can
  302. * have a concurrent ordered extent completion update it. Also set
  303. * last_reflink_trans to ->last_trans only if the former is less than
  304. * the later, because we can be called in a context where
  305. * last_reflink_trans was set to the current transaction generation
  306. * while ->last_trans was not yet updated in the current transaction,
  307. * and therefore has a lower value.
  308. */
  309. spin_lock(&inode->lock);
  310. if (inode->last_reflink_trans < inode->last_trans)
  311. inode->last_reflink_trans = inode->last_trans;
  312. spin_unlock(&inode->lock);
  313. }
  314. static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
  315. {
  316. bool ret = false;
  317. spin_lock(&inode->lock);
  318. if (inode->logged_trans == generation &&
  319. inode->last_sub_trans <= inode->last_log_commit &&
  320. inode->last_sub_trans <= inode->root->last_log_commit)
  321. ret = true;
  322. spin_unlock(&inode->lock);
  323. return ret;
  324. }
  325. /*
  326. * Check if the inode has flags compatible with compression
  327. */
  328. static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
  329. {
  330. if (inode->flags & BTRFS_INODE_NODATACOW ||
  331. inode->flags & BTRFS_INODE_NODATASUM)
  332. return false;
  333. return true;
  334. }
  335. /*
  336. * btrfs_inode_item stores flags in a u64, btrfs_inode stores them in two
  337. * separate u32s. These two functions convert between the two representations.
  338. */
  339. static inline u64 btrfs_inode_combine_flags(u32 flags, u32 ro_flags)
  340. {
  341. return (flags | ((u64)ro_flags << 32));
  342. }
  343. static inline void btrfs_inode_split_flags(u64 inode_item_flags,
  344. u32 *flags, u32 *ro_flags)
  345. {
  346. *flags = (u32)inode_item_flags;
  347. *ro_flags = (u32)(inode_item_flags >> 32);
  348. }
  349. /* Array of bytes with variable length, hexadecimal format 0x1234 */
  350. #define CSUM_FMT "0x%*phN"
  351. #define CSUM_FMT_VALUE(size, bytes) size, bytes
  352. static inline void btrfs_print_data_csum_error(struct btrfs_inode *inode,
  353. u64 logical_start, u8 *csum, u8 *csum_expected, int mirror_num)
  354. {
  355. struct btrfs_root *root = inode->root;
  356. const u32 csum_size = root->fs_info->csum_size;
  357. /* Output minus objectid, which is more meaningful */
  358. if (root->root_key.objectid >= BTRFS_LAST_FREE_OBJECTID)
  359. btrfs_warn_rl(root->fs_info,
  360. "csum failed root %lld ino %lld off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
  361. root->root_key.objectid, btrfs_ino(inode),
  362. logical_start,
  363. CSUM_FMT_VALUE(csum_size, csum),
  364. CSUM_FMT_VALUE(csum_size, csum_expected),
  365. mirror_num);
  366. else
  367. btrfs_warn_rl(root->fs_info,
  368. "csum failed root %llu ino %llu off %llu csum " CSUM_FMT " expected csum " CSUM_FMT " mirror %d",
  369. root->root_key.objectid, btrfs_ino(inode),
  370. logical_start,
  371. CSUM_FMT_VALUE(csum_size, csum),
  372. CSUM_FMT_VALUE(csum_size, csum_expected),
  373. mirror_num);
  374. }
  375. #endif