fs.h 36 KB

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