fs.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_FS_H
  3. #define BTRFS_FS_H
  4. #include <linux/blkdev.h>
  5. #include <linux/sizes.h>
  6. #include <linux/time64.h>
  7. #include <linux/compiler.h>
  8. #include <linux/math.h>
  9. #include <linux/atomic.h>
  10. #include <linux/percpu_counter.h>
  11. #include <linux/completion.h>
  12. #include <linux/lockdep.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/mutex.h>
  15. #include <linux/rwlock_types.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/semaphore.h>
  18. #include <linux/list.h>
  19. #include <linux/radix-tree.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/wait.h>
  22. #include <linux/wait_bit.h>
  23. #include <linux/sched.h>
  24. #include <linux/rbtree.h>
  25. #include <uapi/linux/btrfs.h>
  26. #include <uapi/linux/btrfs_tree.h>
  27. #include "extent-io-tree.h"
  28. #include "async-thread.h"
  29. #include "block-rsv.h"
  30. struct inode;
  31. struct super_block;
  32. struct kobject;
  33. struct reloc_control;
  34. struct crypto_shash;
  35. struct ulist;
  36. struct btrfs_device;
  37. struct btrfs_block_group;
  38. struct btrfs_root;
  39. struct btrfs_fs_devices;
  40. struct btrfs_transaction;
  41. struct btrfs_delayed_root;
  42. struct btrfs_balance_control;
  43. struct btrfs_subpage_info;
  44. struct btrfs_stripe_hash_table;
  45. struct btrfs_space_info;
  46. #define BTRFS_MAX_EXTENT_SIZE SZ_128M
  47. #define BTRFS_OLDEST_GENERATION 0ULL
  48. #define BTRFS_EMPTY_DIR_SIZE 0
  49. #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
  50. #define BTRFS_SUPER_INFO_OFFSET SZ_64K
  51. #define BTRFS_SUPER_INFO_SIZE 4096
  52. static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
  53. /*
  54. * Number of metadata items necessary for an unlink operation:
  55. *
  56. * 1 for the possible orphan item
  57. * 1 for the dir item
  58. * 1 for the dir index
  59. * 1 for the inode ref
  60. * 1 for the inode
  61. * 1 for the parent inode
  62. */
  63. #define BTRFS_UNLINK_METADATA_UNITS 6
  64. /*
  65. * The reserved space at the beginning of each device. It covers the primary
  66. * super block and leaves space for potential use by other tools like
  67. * bootloaders or to lower potential damage of accidental overwrite.
  68. */
  69. #define BTRFS_DEVICE_RANGE_RESERVED (SZ_1M)
  70. /*
  71. * Runtime (in-memory) states of filesystem
  72. */
  73. enum {
  74. /*
  75. * Filesystem is being remounted, allow to skip some operations, like
  76. * defrag
  77. */
  78. BTRFS_FS_STATE_REMOUNTING,
  79. /* Filesystem in RO mode */
  80. BTRFS_FS_STATE_RO,
  81. /* Track if a transaction abort has been reported on this filesystem */
  82. BTRFS_FS_STATE_TRANS_ABORTED,
  83. /*
  84. * Bio operations should be blocked on this filesystem because a source
  85. * or target device is being destroyed as part of a device replace
  86. */
  87. BTRFS_FS_STATE_DEV_REPLACING,
  88. /* The btrfs_fs_info created for self-tests */
  89. BTRFS_FS_STATE_DUMMY_FS_INFO,
  90. /* Checksum errors are ignored. */
  91. BTRFS_FS_STATE_NO_DATA_CSUMS,
  92. BTRFS_FS_STATE_SKIP_META_CSUMS,
  93. /* Indicates there was an error cleaning up a log tree. */
  94. BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
  95. BTRFS_FS_STATE_COUNT
  96. };
  97. enum {
  98. BTRFS_FS_CLOSING_START,
  99. BTRFS_FS_CLOSING_DONE,
  100. BTRFS_FS_LOG_RECOVERING,
  101. BTRFS_FS_OPEN,
  102. BTRFS_FS_QUOTA_ENABLED,
  103. BTRFS_FS_UPDATE_UUID_TREE_GEN,
  104. BTRFS_FS_CREATING_FREE_SPACE_TREE,
  105. BTRFS_FS_BTREE_ERR,
  106. BTRFS_FS_LOG1_ERR,
  107. BTRFS_FS_LOG2_ERR,
  108. BTRFS_FS_QUOTA_OVERRIDE,
  109. /* Used to record internally whether fs has been frozen */
  110. BTRFS_FS_FROZEN,
  111. /*
  112. * Indicate that balance has been set up from the ioctl and is in the
  113. * main phase. The fs_info::balance_ctl is initialized.
  114. */
  115. BTRFS_FS_BALANCE_RUNNING,
  116. /*
  117. * Indicate that relocation of a chunk has started, it's set per chunk
  118. * and is toggled between chunks.
  119. */
  120. BTRFS_FS_RELOC_RUNNING,
  121. /* Indicate that the cleaner thread is awake and doing something. */
  122. BTRFS_FS_CLEANER_RUNNING,
  123. /*
  124. * The checksumming has an optimized version and is considered fast,
  125. * so we don't need to offload checksums to workqueues.
  126. */
  127. BTRFS_FS_CSUM_IMPL_FAST,
  128. /* Indicate that the discard workqueue can service discards. */
  129. BTRFS_FS_DISCARD_RUNNING,
  130. /* Indicate that we need to cleanup space cache v1 */
  131. BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
  132. /* Indicate that we can't trust the free space tree for caching yet */
  133. BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
  134. /* Indicate whether there are any tree modification log users */
  135. BTRFS_FS_TREE_MOD_LOG_USERS,
  136. /* Indicate that we want the transaction kthread to commit right now. */
  137. BTRFS_FS_COMMIT_TRANS,
  138. /* Indicate we have half completed snapshot deletions pending. */
  139. BTRFS_FS_UNFINISHED_DROPS,
  140. /* Indicate we have to finish a zone to do next allocation. */
  141. BTRFS_FS_NEED_ZONE_FINISH,
  142. /* Indicate that we want to commit the transaction. */
  143. BTRFS_FS_NEED_TRANS_COMMIT,
  144. /* This is set when active zone tracking is needed. */
  145. BTRFS_FS_ACTIVE_ZONE_TRACKING,
  146. /*
  147. * Indicate if we have some features changed, this is mostly for
  148. * cleaner thread to update the sysfs interface.
  149. */
  150. BTRFS_FS_FEATURE_CHANGED,
  151. /*
  152. * Indicate that we have found a tree block which is only aligned to
  153. * sectorsize, but not to nodesize. This should be rare nowadays.
  154. */
  155. BTRFS_FS_UNALIGNED_TREE_BLOCK,
  156. #if BITS_PER_LONG == 32
  157. /* Indicate if we have error/warn message printed on 32bit systems */
  158. BTRFS_FS_32BIT_ERROR,
  159. BTRFS_FS_32BIT_WARN,
  160. #endif
  161. };
  162. /*
  163. * Flags for mount options.
  164. *
  165. * Note: don't forget to add new options to btrfs_show_options()
  166. */
  167. enum {
  168. BTRFS_MOUNT_NODATASUM = (1ULL << 0),
  169. BTRFS_MOUNT_NODATACOW = (1ULL << 1),
  170. BTRFS_MOUNT_NOBARRIER = (1ULL << 2),
  171. BTRFS_MOUNT_SSD = (1ULL << 3),
  172. BTRFS_MOUNT_DEGRADED = (1ULL << 4),
  173. BTRFS_MOUNT_COMPRESS = (1ULL << 5),
  174. BTRFS_MOUNT_NOTREELOG = (1ULL << 6),
  175. BTRFS_MOUNT_FLUSHONCOMMIT = (1ULL << 7),
  176. BTRFS_MOUNT_SSD_SPREAD = (1ULL << 8),
  177. BTRFS_MOUNT_NOSSD = (1ULL << 9),
  178. BTRFS_MOUNT_DISCARD_SYNC = (1ULL << 10),
  179. BTRFS_MOUNT_FORCE_COMPRESS = (1ULL << 11),
  180. BTRFS_MOUNT_SPACE_CACHE = (1ULL << 12),
  181. BTRFS_MOUNT_CLEAR_CACHE = (1ULL << 13),
  182. BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED = (1ULL << 14),
  183. BTRFS_MOUNT_ENOSPC_DEBUG = (1ULL << 15),
  184. BTRFS_MOUNT_AUTO_DEFRAG = (1ULL << 16),
  185. BTRFS_MOUNT_USEBACKUPROOT = (1ULL << 17),
  186. BTRFS_MOUNT_SKIP_BALANCE = (1ULL << 18),
  187. BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1ULL << 19),
  188. BTRFS_MOUNT_RESCAN_UUID_TREE = (1ULL << 20),
  189. BTRFS_MOUNT_FRAGMENT_DATA = (1ULL << 21),
  190. BTRFS_MOUNT_FRAGMENT_METADATA = (1ULL << 22),
  191. BTRFS_MOUNT_FREE_SPACE_TREE = (1ULL << 23),
  192. BTRFS_MOUNT_NOLOGREPLAY = (1ULL << 24),
  193. BTRFS_MOUNT_REF_VERIFY = (1ULL << 25),
  194. BTRFS_MOUNT_DISCARD_ASYNC = (1ULL << 26),
  195. BTRFS_MOUNT_IGNOREBADROOTS = (1ULL << 27),
  196. BTRFS_MOUNT_IGNOREDATACSUMS = (1ULL << 28),
  197. BTRFS_MOUNT_NODISCARD = (1ULL << 29),
  198. BTRFS_MOUNT_NOSPACECACHE = (1ULL << 30),
  199. BTRFS_MOUNT_IGNOREMETACSUMS = (1ULL << 31),
  200. BTRFS_MOUNT_IGNORESUPERFLAGS = (1ULL << 32),
  201. };
  202. /*
  203. * Compat flags that we support. If any incompat flags are set other than the
  204. * ones specified below then we will fail to mount
  205. */
  206. #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
  207. #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
  208. #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
  209. #define BTRFS_FEATURE_COMPAT_RO_SUPP \
  210. (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
  211. BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
  212. BTRFS_FEATURE_COMPAT_RO_VERITY | \
  213. BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
  214. #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
  215. #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
  216. #define BTRFS_FEATURE_INCOMPAT_SUPP_STABLE \
  217. (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
  218. BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
  219. BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
  220. BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
  221. BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
  222. BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
  223. BTRFS_FEATURE_INCOMPAT_RAID56 | \
  224. BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
  225. BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
  226. BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
  227. BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
  228. BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
  229. BTRFS_FEATURE_INCOMPAT_ZONED | \
  230. BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA)
  231. #ifdef CONFIG_BTRFS_DEBUG
  232. /*
  233. * Features under developmen like Extent tree v2 support is enabled
  234. * only under CONFIG_BTRFS_DEBUG.
  235. */
  236. #define BTRFS_FEATURE_INCOMPAT_SUPP \
  237. (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE | \
  238. BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE | \
  239. BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
  240. #else
  241. #define BTRFS_FEATURE_INCOMPAT_SUPP \
  242. (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE)
  243. #endif
  244. #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
  245. (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
  246. #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
  247. #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
  248. #define BTRFS_WARNING_COMMIT_INTERVAL (300)
  249. #define BTRFS_DEFAULT_MAX_INLINE (2048)
  250. struct btrfs_dev_replace {
  251. /* See #define above */
  252. u64 replace_state;
  253. /* Seconds since 1-Jan-1970 */
  254. time64_t time_started;
  255. /* Seconds since 1-Jan-1970 */
  256. time64_t time_stopped;
  257. atomic64_t num_write_errors;
  258. atomic64_t num_uncorrectable_read_errors;
  259. u64 cursor_left;
  260. u64 committed_cursor_left;
  261. u64 cursor_left_last_write_of_item;
  262. u64 cursor_right;
  263. /* See #define above */
  264. u64 cont_reading_from_srcdev_mode;
  265. int is_valid;
  266. int item_needs_writeback;
  267. struct btrfs_device *srcdev;
  268. struct btrfs_device *tgtdev;
  269. struct mutex lock_finishing_cancel_unmount;
  270. struct rw_semaphore rwsem;
  271. struct btrfs_scrub_progress scrub_progress;
  272. struct percpu_counter bio_counter;
  273. wait_queue_head_t replace_wait;
  274. struct task_struct *replace_task;
  275. };
  276. /*
  277. * Free clusters are used to claim free space in relatively large chunks,
  278. * allowing us to do less seeky writes. They are used for all metadata
  279. * allocations. In ssd_spread mode they are also used for data allocations.
  280. */
  281. struct btrfs_free_cluster {
  282. spinlock_t lock;
  283. spinlock_t refill_lock;
  284. struct rb_root root;
  285. /* Largest extent in this cluster */
  286. u64 max_size;
  287. /* First extent starting offset */
  288. u64 window_start;
  289. /* We did a full search and couldn't create a cluster */
  290. bool fragmented;
  291. struct btrfs_block_group *block_group;
  292. /*
  293. * When a cluster is allocated from a block group, we put the cluster
  294. * onto a list in the block group so that it can be freed before the
  295. * block group is freed.
  296. */
  297. struct list_head block_group_list;
  298. };
  299. /* Discard control. */
  300. /*
  301. * Async discard uses multiple lists to differentiate the discard filter
  302. * parameters. Index 0 is for completely free block groups where we need to
  303. * ensure the entire block group is trimmed without being lossy. Indices
  304. * afterwards represent monotonically decreasing discard filter sizes to
  305. * prioritize what should be discarded next.
  306. */
  307. #define BTRFS_NR_DISCARD_LISTS 3
  308. #define BTRFS_DISCARD_INDEX_UNUSED 0
  309. #define BTRFS_DISCARD_INDEX_START 1
  310. struct btrfs_discard_ctl {
  311. struct workqueue_struct *discard_workers;
  312. struct delayed_work work;
  313. spinlock_t lock;
  314. struct btrfs_block_group *block_group;
  315. struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
  316. u64 prev_discard;
  317. u64 prev_discard_time;
  318. atomic_t discardable_extents;
  319. atomic64_t discardable_bytes;
  320. u64 max_discard_size;
  321. u64 delay_ms;
  322. u32 iops_limit;
  323. u32 kbps_limit;
  324. u64 discard_extent_bytes;
  325. u64 discard_bitmap_bytes;
  326. atomic64_t discard_bytes_saved;
  327. };
  328. /*
  329. * Exclusive operations (device replace, resize, device add/remove, balance)
  330. */
  331. enum btrfs_exclusive_operation {
  332. BTRFS_EXCLOP_NONE,
  333. BTRFS_EXCLOP_BALANCE_PAUSED,
  334. BTRFS_EXCLOP_BALANCE,
  335. BTRFS_EXCLOP_DEV_ADD,
  336. BTRFS_EXCLOP_DEV_REMOVE,
  337. BTRFS_EXCLOP_DEV_REPLACE,
  338. BTRFS_EXCLOP_RESIZE,
  339. BTRFS_EXCLOP_SWAP_ACTIVATE,
  340. };
  341. /* Store data about transaction commits, exported via sysfs. */
  342. struct btrfs_commit_stats {
  343. /* Total number of commits */
  344. u64 commit_count;
  345. /* The maximum commit duration so far in ns */
  346. u64 max_commit_dur;
  347. /* The last commit duration in ns */
  348. u64 last_commit_dur;
  349. /* The total commit duration in ns */
  350. u64 total_commit_dur;
  351. };
  352. struct btrfs_fs_info {
  353. u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
  354. unsigned long flags;
  355. struct btrfs_root *tree_root;
  356. struct btrfs_root *chunk_root;
  357. struct btrfs_root *dev_root;
  358. struct btrfs_root *fs_root;
  359. struct btrfs_root *quota_root;
  360. struct btrfs_root *uuid_root;
  361. struct btrfs_root *data_reloc_root;
  362. struct btrfs_root *block_group_root;
  363. struct btrfs_root *stripe_root;
  364. /* The log root tree is a directory of all the other log roots */
  365. struct btrfs_root *log_root_tree;
  366. /* The tree that holds the global roots (csum, extent, etc) */
  367. rwlock_t global_root_lock;
  368. struct rb_root global_root_tree;
  369. spinlock_t fs_roots_radix_lock;
  370. struct radix_tree_root fs_roots_radix;
  371. /* Block group cache stuff */
  372. rwlock_t block_group_cache_lock;
  373. struct rb_root_cached block_group_cache_tree;
  374. /* Keep track of unallocated space */
  375. atomic64_t free_chunk_space;
  376. /* Track ranges which are used by log trees blocks/logged data extents */
  377. struct extent_io_tree excluded_extents;
  378. /* logical->physical extent mapping */
  379. struct rb_root_cached mapping_tree;
  380. rwlock_t mapping_tree_lock;
  381. /*
  382. * Block reservation for extent, checksum, root tree and delayed dir
  383. * index item.
  384. */
  385. struct btrfs_block_rsv global_block_rsv;
  386. /* Block reservation for metadata operations */
  387. struct btrfs_block_rsv trans_block_rsv;
  388. /* Block reservation for chunk tree */
  389. struct btrfs_block_rsv chunk_block_rsv;
  390. /* Block reservation for delayed operations */
  391. struct btrfs_block_rsv delayed_block_rsv;
  392. /* Block reservation for delayed refs */
  393. struct btrfs_block_rsv delayed_refs_rsv;
  394. struct btrfs_block_rsv empty_block_rsv;
  395. /*
  396. * Updated while holding the lock 'trans_lock'. Due to the life cycle of
  397. * a transaction, it can be directly read while holding a transaction
  398. * handle, everywhere else must be read with btrfs_get_fs_generation().
  399. * Should always be updated using btrfs_set_fs_generation().
  400. */
  401. u64 generation;
  402. /*
  403. * Always use btrfs_get_last_trans_committed() and
  404. * btrfs_set_last_trans_committed() to read and update this field.
  405. */
  406. u64 last_trans_committed;
  407. /*
  408. * Generation of the last transaction used for block group relocation
  409. * since the filesystem was last mounted (or 0 if none happened yet).
  410. * Must be written and read while holding btrfs_fs_info::commit_root_sem.
  411. */
  412. u64 last_reloc_trans;
  413. /*
  414. * This is updated to the current trans every time a full commit is
  415. * required instead of the faster short fsync log commits
  416. */
  417. u64 last_trans_log_full_commit;
  418. unsigned long long mount_opt;
  419. unsigned long compress_type:4;
  420. unsigned int compress_level;
  421. u32 commit_interval;
  422. /*
  423. * It is a suggestive number, the read side is safe even it gets a
  424. * wrong number because we will write out the data into a regular
  425. * extent. The write side(mount/remount) is under ->s_umount lock,
  426. * so it is also safe.
  427. */
  428. u64 max_inline;
  429. struct btrfs_transaction *running_transaction;
  430. wait_queue_head_t transaction_throttle;
  431. wait_queue_head_t transaction_wait;
  432. wait_queue_head_t transaction_blocked_wait;
  433. wait_queue_head_t async_submit_wait;
  434. /*
  435. * Used to protect the incompat_flags, compat_flags, compat_ro_flags
  436. * when they are updated.
  437. *
  438. * Because we do not clear the flags for ever, so we needn't use
  439. * the lock on the read side.
  440. *
  441. * We also needn't use the lock when we mount the fs, because
  442. * there is no other task which will update the flag.
  443. */
  444. spinlock_t super_lock;
  445. struct btrfs_super_block *super_copy;
  446. struct btrfs_super_block *super_for_commit;
  447. struct super_block *sb;
  448. struct inode *btree_inode;
  449. struct mutex tree_log_mutex;
  450. struct mutex transaction_kthread_mutex;
  451. struct mutex cleaner_mutex;
  452. struct mutex chunk_mutex;
  453. /*
  454. * This is taken to make sure we don't set block groups ro after the
  455. * free space cache has been allocated on them.
  456. */
  457. struct mutex ro_block_group_mutex;
  458. /*
  459. * This is used during read/modify/write to make sure no two ios are
  460. * trying to mod the same stripe at the same time.
  461. */
  462. struct btrfs_stripe_hash_table *stripe_hash_table;
  463. /*
  464. * This protects the ordered operations list only while we are
  465. * processing all of the entries on it. This way we make sure the
  466. * commit code doesn't find the list temporarily empty because another
  467. * function happens to be doing non-waiting preflush before jumping
  468. * into the main commit.
  469. */
  470. struct mutex ordered_operations_mutex;
  471. struct rw_semaphore commit_root_sem;
  472. struct rw_semaphore cleanup_work_sem;
  473. struct rw_semaphore subvol_sem;
  474. spinlock_t trans_lock;
  475. /*
  476. * The reloc mutex goes with the trans lock, it is taken during commit
  477. * to protect us from the relocation code.
  478. */
  479. struct mutex reloc_mutex;
  480. struct list_head trans_list;
  481. struct list_head dead_roots;
  482. struct list_head caching_block_groups;
  483. spinlock_t delayed_iput_lock;
  484. struct list_head delayed_iputs;
  485. atomic_t nr_delayed_iputs;
  486. wait_queue_head_t delayed_iputs_wait;
  487. atomic64_t tree_mod_seq;
  488. /* This protects tree_mod_log and tree_mod_seq_list */
  489. rwlock_t tree_mod_log_lock;
  490. struct rb_root tree_mod_log;
  491. struct list_head tree_mod_seq_list;
  492. atomic_t async_delalloc_pages;
  493. /* This is used to protect the following list -- ordered_roots. */
  494. spinlock_t ordered_root_lock;
  495. /*
  496. * All fs/file tree roots in which there are data=ordered extents
  497. * pending writeback are added into this list.
  498. *
  499. * These can span multiple transactions and basically include every
  500. * dirty data page that isn't from nodatacow.
  501. */
  502. struct list_head ordered_roots;
  503. struct mutex delalloc_root_mutex;
  504. spinlock_t delalloc_root_lock;
  505. /* All fs/file tree roots that have delalloc inodes. */
  506. struct list_head delalloc_roots;
  507. /*
  508. * There is a pool of worker threads for checksumming during writes and
  509. * a pool for checksumming after reads. This is because readers can
  510. * run with FS locks held, and the writers may be waiting for those
  511. * locks. We don't want ordering in the pending list to cause
  512. * deadlocks, and so the two are serviced separately.
  513. *
  514. * A third pool does submit_bio to avoid deadlocking with the other two.
  515. */
  516. struct btrfs_workqueue *workers;
  517. struct btrfs_workqueue *delalloc_workers;
  518. struct btrfs_workqueue *flush_workers;
  519. struct workqueue_struct *endio_workers;
  520. struct workqueue_struct *endio_meta_workers;
  521. struct workqueue_struct *rmw_workers;
  522. struct workqueue_struct *compressed_write_workers;
  523. struct btrfs_workqueue *endio_write_workers;
  524. struct btrfs_workqueue *endio_freespace_worker;
  525. struct btrfs_workqueue *caching_workers;
  526. /*
  527. * Fixup workers take dirty pages that didn't properly go through the
  528. * cow mechanism and make them safe to write. It happens for the
  529. * sys_munmap function call path.
  530. */
  531. struct btrfs_workqueue *fixup_workers;
  532. struct btrfs_workqueue *delayed_workers;
  533. struct task_struct *transaction_kthread;
  534. struct task_struct *cleaner_kthread;
  535. u32 thread_pool_size;
  536. struct kobject *space_info_kobj;
  537. struct kobject *qgroups_kobj;
  538. struct kobject *discard_kobj;
  539. /* Used to keep from writing metadata until there is a nice batch */
  540. struct percpu_counter dirty_metadata_bytes;
  541. struct percpu_counter delalloc_bytes;
  542. struct percpu_counter ordered_bytes;
  543. s32 dirty_metadata_batch;
  544. s32 delalloc_batch;
  545. struct percpu_counter evictable_extent_maps;
  546. spinlock_t extent_map_shrinker_lock;
  547. u64 extent_map_shrinker_last_root;
  548. u64 extent_map_shrinker_last_ino;
  549. atomic64_t extent_map_shrinker_nr_to_scan;
  550. struct work_struct extent_map_shrinker_work;
  551. /* Protected by 'trans_lock'. */
  552. struct list_head dirty_cowonly_roots;
  553. struct btrfs_fs_devices *fs_devices;
  554. /*
  555. * The space_info list is effectively read only after initial setup.
  556. * It is populated at mount time and cleaned up after all block groups
  557. * are removed. RCU is used to protect it.
  558. */
  559. struct list_head space_info;
  560. struct btrfs_space_info *data_sinfo;
  561. struct reloc_control *reloc_ctl;
  562. /* data_alloc_cluster is only used in ssd_spread mode */
  563. struct btrfs_free_cluster data_alloc_cluster;
  564. /* All metadata allocations go through this cluster. */
  565. struct btrfs_free_cluster meta_alloc_cluster;
  566. /* Auto defrag inodes go here. */
  567. spinlock_t defrag_inodes_lock;
  568. struct rb_root defrag_inodes;
  569. atomic_t defrag_running;
  570. /* Used to protect avail_{data, metadata, system}_alloc_bits */
  571. seqlock_t profiles_lock;
  572. /*
  573. * These three are in extended format (availability of single chunks is
  574. * denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other types are denoted
  575. * by corresponding BTRFS_BLOCK_GROUP_* bits)
  576. */
  577. u64 avail_data_alloc_bits;
  578. u64 avail_metadata_alloc_bits;
  579. u64 avail_system_alloc_bits;
  580. /* Balance state */
  581. spinlock_t balance_lock;
  582. struct mutex balance_mutex;
  583. atomic_t balance_pause_req;
  584. atomic_t balance_cancel_req;
  585. struct btrfs_balance_control *balance_ctl;
  586. wait_queue_head_t balance_wait_q;
  587. /* Cancellation requests for chunk relocation */
  588. atomic_t reloc_cancel_req;
  589. u32 data_chunk_allocations;
  590. u32 metadata_ratio;
  591. void *bdev_holder;
  592. /* Private scrub information */
  593. struct mutex scrub_lock;
  594. atomic_t scrubs_running;
  595. atomic_t scrub_pause_req;
  596. atomic_t scrubs_paused;
  597. atomic_t scrub_cancel_req;
  598. wait_queue_head_t scrub_pause_wait;
  599. /*
  600. * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
  601. * running.
  602. */
  603. refcount_t scrub_workers_refcnt;
  604. u32 sectors_per_page;
  605. struct workqueue_struct *scrub_workers;
  606. struct btrfs_discard_ctl discard_ctl;
  607. /* Is qgroup tracking in a consistent state? */
  608. u64 qgroup_flags;
  609. /* Holds configuration and tracking. Protected by qgroup_lock. */
  610. struct rb_root qgroup_tree;
  611. spinlock_t qgroup_lock;
  612. /*
  613. * Used to avoid frequently calling ulist_alloc()/ulist_free()
  614. * when doing qgroup accounting, it must be protected by qgroup_lock.
  615. */
  616. struct ulist *qgroup_ulist;
  617. /*
  618. * Protect user change for quota operations. If a transaction is needed,
  619. * it must be started before locking this lock.
  620. */
  621. struct mutex qgroup_ioctl_lock;
  622. /* List of dirty qgroups to be written at next commit. */
  623. struct list_head dirty_qgroups;
  624. /* Used by qgroup for an efficient tree traversal. */
  625. u64 qgroup_seq;
  626. /* Qgroup rescan items. */
  627. /* Protects the progress item */
  628. struct mutex qgroup_rescan_lock;
  629. struct btrfs_key qgroup_rescan_progress;
  630. struct btrfs_workqueue *qgroup_rescan_workers;
  631. struct completion qgroup_rescan_completion;
  632. struct btrfs_work qgroup_rescan_work;
  633. /* Protected by qgroup_rescan_lock */
  634. bool qgroup_rescan_running;
  635. u8 qgroup_drop_subtree_thres;
  636. u64 qgroup_enable_gen;
  637. /*
  638. * If this is not 0, then it indicates a serious filesystem error has
  639. * happened and it contains that error (negative errno value).
  640. */
  641. int fs_error;
  642. /* Filesystem state */
  643. unsigned long fs_state;
  644. struct btrfs_delayed_root *delayed_root;
  645. /* Extent buffer radix tree */
  646. spinlock_t buffer_lock;
  647. /* Entries are eb->start / sectorsize */
  648. struct radix_tree_root buffer_radix;
  649. /* Next backup root to be overwritten */
  650. int backup_root_index;
  651. /* Device replace state */
  652. struct btrfs_dev_replace dev_replace;
  653. struct semaphore uuid_tree_rescan_sem;
  654. /* Used to reclaim the metadata space in the background. */
  655. struct work_struct async_reclaim_work;
  656. struct work_struct async_data_reclaim_work;
  657. struct work_struct preempt_reclaim_work;
  658. /* Reclaim partially filled block groups in the background */
  659. struct work_struct reclaim_bgs_work;
  660. /* Protected by unused_bgs_lock. */
  661. struct list_head reclaim_bgs;
  662. int bg_reclaim_threshold;
  663. /* Protects the lists unused_bgs and reclaim_bgs. */
  664. spinlock_t unused_bgs_lock;
  665. /* Protected by unused_bgs_lock. */
  666. struct list_head unused_bgs;
  667. struct mutex unused_bg_unpin_mutex;
  668. /* Protect block groups that are going to be deleted */
  669. struct mutex reclaim_bgs_lock;
  670. /* Cached block sizes */
  671. u32 nodesize;
  672. u32 sectorsize;
  673. /* ilog2 of sectorsize, use to avoid 64bit division */
  674. u32 sectorsize_bits;
  675. u32 csum_size;
  676. u32 csums_per_leaf;
  677. u32 stripesize;
  678. /*
  679. * Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular
  680. * filesystem, on zoned it depends on the device constraints.
  681. */
  682. u64 max_extent_size;
  683. /* Block groups and devices containing active swapfiles. */
  684. spinlock_t swapfile_pins_lock;
  685. struct rb_root swapfile_pins;
  686. struct crypto_shash *csum_shash;
  687. /* Type of exclusive operation running, protected by super_lock */
  688. enum btrfs_exclusive_operation exclusive_operation;
  689. /*
  690. * Zone size > 0 when in ZONED mode, otherwise it's used for a check
  691. * if the mode is enabled
  692. */
  693. u64 zone_size;
  694. /* Constraints for ZONE_APPEND commands: */
  695. struct queue_limits limits;
  696. u64 max_zone_append_size;
  697. struct mutex zoned_meta_io_lock;
  698. spinlock_t treelog_bg_lock;
  699. u64 treelog_bg;
  700. /*
  701. * Start of the dedicated data relocation block group, protected by
  702. * relocation_bg_lock.
  703. */
  704. spinlock_t relocation_bg_lock;
  705. u64 data_reloc_bg;
  706. struct mutex zoned_data_reloc_io_lock;
  707. struct btrfs_block_group *active_meta_bg;
  708. struct btrfs_block_group *active_system_bg;
  709. u64 nr_global_roots;
  710. spinlock_t zone_active_bgs_lock;
  711. struct list_head zone_active_bgs;
  712. /* Updates are not protected by any lock */
  713. struct btrfs_commit_stats commit_stats;
  714. /*
  715. * Last generation where we dropped a non-relocation root.
  716. * Use btrfs_set_last_root_drop_gen() and btrfs_get_last_root_drop_gen()
  717. * to change it and to read it, respectively.
  718. */
  719. u64 last_root_drop_gen;
  720. /*
  721. * Annotations for transaction events (structures are empty when
  722. * compiled without lockdep).
  723. */
  724. struct lockdep_map btrfs_trans_num_writers_map;
  725. struct lockdep_map btrfs_trans_num_extwriters_map;
  726. struct lockdep_map btrfs_state_change_map[4];
  727. struct lockdep_map btrfs_trans_pending_ordered_map;
  728. struct lockdep_map btrfs_ordered_extent_map;
  729. #ifdef CONFIG_BTRFS_FS_REF_VERIFY
  730. spinlock_t ref_verify_lock;
  731. struct rb_root block_tree;
  732. #endif
  733. #ifdef CONFIG_BTRFS_DEBUG
  734. struct kobject *debug_kobj;
  735. struct list_head allocated_roots;
  736. spinlock_t eb_leak_lock;
  737. struct list_head allocated_ebs;
  738. #endif
  739. };
  740. #define page_to_inode(_page) (BTRFS_I(_Generic((_page), \
  741. struct page *: (_page))->mapping->host))
  742. #define folio_to_inode(_folio) (BTRFS_I(_Generic((_folio), \
  743. struct folio *: (_folio))->mapping->host))
  744. #define page_to_fs_info(_page) (page_to_inode(_page)->root->fs_info)
  745. #define folio_to_fs_info(_folio) (folio_to_inode(_folio)->root->fs_info)
  746. #define inode_to_fs_info(_inode) (BTRFS_I(_Generic((_inode), \
  747. struct inode *: (_inode)))->root->fs_info)
  748. static inline u64 btrfs_get_fs_generation(const struct btrfs_fs_info *fs_info)
  749. {
  750. return READ_ONCE(fs_info->generation);
  751. }
  752. static inline void btrfs_set_fs_generation(struct btrfs_fs_info *fs_info, u64 gen)
  753. {
  754. WRITE_ONCE(fs_info->generation, gen);
  755. }
  756. static inline u64 btrfs_get_last_trans_committed(const struct btrfs_fs_info *fs_info)
  757. {
  758. return READ_ONCE(fs_info->last_trans_committed);
  759. }
  760. static inline void btrfs_set_last_trans_committed(struct btrfs_fs_info *fs_info, u64 gen)
  761. {
  762. WRITE_ONCE(fs_info->last_trans_committed, gen);
  763. }
  764. static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
  765. u64 gen)
  766. {
  767. WRITE_ONCE(fs_info->last_root_drop_gen, gen);
  768. }
  769. static inline u64 btrfs_get_last_root_drop_gen(const struct btrfs_fs_info *fs_info)
  770. {
  771. return READ_ONCE(fs_info->last_root_drop_gen);
  772. }
  773. /*
  774. * Take the number of bytes to be checksummed and figure out how many leaves
  775. * it would require to store the csums for that many bytes.
  776. */
  777. static inline u64 btrfs_csum_bytes_to_leaves(
  778. const struct btrfs_fs_info *fs_info, u64 csum_bytes)
  779. {
  780. const u64 num_csums = csum_bytes >> fs_info->sectorsize_bits;
  781. return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
  782. }
  783. /*
  784. * Use this if we would be adding new items, as we could split nodes as we cow
  785. * down the tree.
  786. */
  787. static inline u64 btrfs_calc_insert_metadata_size(const struct btrfs_fs_info *fs_info,
  788. unsigned num_items)
  789. {
  790. return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
  791. }
  792. /*
  793. * Doing a truncate or a modification won't result in new nodes or leaves, just
  794. * what we need for COW.
  795. */
  796. static inline u64 btrfs_calc_metadata_size(const struct btrfs_fs_info *fs_info,
  797. unsigned num_items)
  798. {
  799. return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
  800. }
  801. #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
  802. sizeof(struct btrfs_item))
  803. static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info)
  804. {
  805. return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && fs_info->zone_size > 0;
  806. }
  807. /*
  808. * Count how many fs_info->max_extent_size cover the @size
  809. */
  810. static inline u32 count_max_extents(const struct btrfs_fs_info *fs_info, u64 size)
  811. {
  812. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  813. if (!fs_info)
  814. return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
  815. #endif
  816. return div_u64(size + fs_info->max_extent_size - 1, fs_info->max_extent_size);
  817. }
  818. bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
  819. enum btrfs_exclusive_operation type);
  820. bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
  821. enum btrfs_exclusive_operation type);
  822. void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info);
  823. void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
  824. void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
  825. enum btrfs_exclusive_operation op);
  826. int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args);
  827. /* Compatibility and incompatibility defines */
  828. void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
  829. const char *name);
  830. void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
  831. const char *name);
  832. void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
  833. const char *name);
  834. void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
  835. const char *name);
  836. #define __btrfs_fs_incompat(fs_info, flags) \
  837. (!!(btrfs_super_incompat_flags((fs_info)->super_copy) & (flags)))
  838. #define __btrfs_fs_compat_ro(fs_info, flags) \
  839. (!!(btrfs_super_compat_ro_flags((fs_info)->super_copy) & (flags)))
  840. #define btrfs_set_fs_incompat(__fs_info, opt) \
  841. __btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
  842. #define btrfs_clear_fs_incompat(__fs_info, opt) \
  843. __btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
  844. #define btrfs_fs_incompat(fs_info, opt) \
  845. __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
  846. #define btrfs_set_fs_compat_ro(__fs_info, opt) \
  847. __btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
  848. #define btrfs_clear_fs_compat_ro(__fs_info, opt) \
  849. __btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
  850. #define btrfs_fs_compat_ro(fs_info, opt) \
  851. __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
  852. #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
  853. #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
  854. #define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
  855. #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
  856. BTRFS_MOUNT_##opt)
  857. static inline int btrfs_fs_closing(const struct btrfs_fs_info *fs_info)
  858. {
  859. /* Do it this way so we only ever do one test_bit in the normal case. */
  860. if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
  861. if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
  862. return 2;
  863. return 1;
  864. }
  865. return 0;
  866. }
  867. /*
  868. * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
  869. * anything except sleeping. This function is used to check the status of
  870. * the fs.
  871. * We check for BTRFS_FS_STATE_RO to avoid races with a concurrent remount,
  872. * since setting and checking for SB_RDONLY in the superblock's flags is not
  873. * atomic.
  874. */
  875. static inline int btrfs_need_cleaner_sleep(const struct btrfs_fs_info *fs_info)
  876. {
  877. return test_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state) ||
  878. btrfs_fs_closing(fs_info);
  879. }
  880. static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
  881. {
  882. clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
  883. }
  884. #define BTRFS_FS_ERROR(fs_info) (READ_ONCE((fs_info)->fs_error))
  885. #define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info) \
  886. (unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, \
  887. &(fs_info)->fs_state)))
  888. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  889. #define EXPORT_FOR_TESTS
  890. static inline int btrfs_is_testing(const struct btrfs_fs_info *fs_info)
  891. {
  892. return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  893. }
  894. void btrfs_test_destroy_inode(struct inode *inode);
  895. #else
  896. #define EXPORT_FOR_TESTS static
  897. static inline int btrfs_is_testing(const struct btrfs_fs_info *fs_info)
  898. {
  899. return 0;
  900. }
  901. #endif
  902. #endif