1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2007 Oracle. All rights reserved.
10 #include <linux/sched/signal.h>
11 #include <linux/highmem.h>
13 #include <linux/rwsem.h>
14 #include <linux/semaphore.h>
15 #include <linux/completion.h>
16 #include <linux/backing-dev.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <trace/events/btrfs.h>
20 #include <asm/unaligned.h>
21 #include <linux/pagemap.h>
22 #include <linux/btrfs.h>
23 #include <linux/btrfs_tree.h>
24 #include <linux/workqueue.h>
25 #include <linux/security.h>
26 #include <linux/sizes.h>
27 #include <linux/dynamic_debug.h>
28 #include <linux/refcount.h>
29 #include <linux/crc32c.h>
30 #include <linux/iomap.h>
31 #include "extent-io-tree.h"
32 #include "extent_io.h"
33 #include "extent_map.h"
34 #include "async-thread.h"
35 #include "block-rsv.h"
38 struct btrfs_trans_handle;
39 struct btrfs_transaction;
40 struct btrfs_pending_snapshot;
41 struct btrfs_delayed_ref_root;
42 struct btrfs_space_info;
43 struct btrfs_block_group;
44 extern struct kmem_cache *btrfs_trans_handle_cachep;
45 extern struct kmem_cache *btrfs_path_cachep;
46 extern struct kmem_cache *btrfs_free_space_cachep;
47 extern struct kmem_cache *btrfs_free_space_bitmap_cachep;
48 struct btrfs_ordered_sum;
51 struct btrfs_ioctl_encoded_io_args;
53 struct btrfs_fs_devices;
54 struct btrfs_balance_control;
55 struct btrfs_delayed_root;
58 #define BTRFS_OLDEST_GENERATION 0ULL
60 #define BTRFS_EMPTY_DIR_SIZE 0
62 #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
64 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
66 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
68 BUG_ON(num_stripes == 0);
69 return sizeof(struct btrfs_chunk) +
70 sizeof(struct btrfs_stripe) * (num_stripes - 1);
74 * Runtime (in-memory) states of filesystem
77 /* Global indicator of serious filesystem errors */
80 * Filesystem is being remounted, allow to skip some operations, like
83 BTRFS_FS_STATE_REMOUNTING,
84 /* Filesystem in RO mode */
86 /* Track if a transaction abort has been reported on this filesystem */
87 BTRFS_FS_STATE_TRANS_ABORTED,
89 * Bio operations should be blocked on this filesystem because a source
90 * or target device is being destroyed as part of a device replace
92 BTRFS_FS_STATE_DEV_REPLACING,
93 /* The btrfs_fs_info created for self-tests */
94 BTRFS_FS_STATE_DUMMY_FS_INFO,
96 BTRFS_FS_STATE_NO_CSUMS,
98 /* Indicates there was an error cleaning up a log tree. */
99 BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
104 #define BTRFS_SUPER_INFO_OFFSET SZ_64K
105 #define BTRFS_SUPER_INFO_SIZE 4096
106 static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
109 * The reserved space at the beginning of each device.
110 * It covers the primary super block and leaves space for potential use by other
111 * tools like bootloaders or to lower potential damage of accidental overwrite.
113 #define BTRFS_DEVICE_RANGE_RESERVED (SZ_1M)
116 * Compat flags that we support. If any incompat flags are set other than the
117 * ones specified below then we will fail to mount
119 #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
120 #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
121 #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
123 #define BTRFS_FEATURE_COMPAT_RO_SUPP \
124 (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
125 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
126 BTRFS_FEATURE_COMPAT_RO_VERITY | \
127 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
129 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
130 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
132 #ifdef CONFIG_BTRFS_DEBUG
134 * Extent tree v2 supported only with CONFIG_BTRFS_DEBUG
136 #define BTRFS_FEATURE_INCOMPAT_SUPP \
137 (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
138 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
139 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
140 BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
141 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
142 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
143 BTRFS_FEATURE_INCOMPAT_RAID56 | \
144 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
145 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
146 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
147 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
148 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
149 BTRFS_FEATURE_INCOMPAT_ZONED | \
150 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
152 #define BTRFS_FEATURE_INCOMPAT_SUPP \
153 (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
154 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
155 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
156 BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
157 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
158 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
159 BTRFS_FEATURE_INCOMPAT_RAID56 | \
160 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
161 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
162 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
163 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
164 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
165 BTRFS_FEATURE_INCOMPAT_ZONED)
168 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
169 (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
170 #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
172 /* Read ahead values for struct btrfs_path.reada */
178 * Similar to READA_FORWARD but unlike it:
180 * 1) It will trigger readahead even for leaves that are not close to
181 * each other on disk;
182 * 2) It also triggers readahead for nodes;
183 * 3) During a search, even when a node or leaf is already in memory, it
184 * will still trigger readahead for other nodes and leaves that follow
187 * This is meant to be used only when we know we are iterating over the
188 * entire tree or a very large part of it.
190 READA_FORWARD_ALWAYS,
194 * btrfs_paths remember the path taken from the root down to the leaf.
195 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
196 * to any other levels that are present.
198 * The slots array records the index of the item or block pointer
199 * used while walking the tree.
202 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
203 int slots[BTRFS_MAX_LEVEL];
204 /* if there is real range locking, this locks field will change */
205 u8 locks[BTRFS_MAX_LEVEL];
207 /* keep some upper locks as we walk down */
211 * set by btrfs_split_item, tells search_slot to keep all locks
212 * and to force calls to keep space in the nodes
214 unsigned int search_for_split:1;
215 unsigned int keep_locks:1;
216 unsigned int skip_locking:1;
217 unsigned int search_commit_root:1;
218 unsigned int need_commit_sem:1;
219 unsigned int skip_release_on_error:1;
221 * Indicate that new item (btrfs_search_slot) is extending already
222 * existing item and ins_len contains only the data size and not item
223 * header (ie. sizeof(struct btrfs_item) is not included).
225 unsigned int search_for_extension:1;
226 /* Stop search if any locks need to be taken (for read) */
227 unsigned int nowait:1;
230 struct btrfs_dev_replace {
231 u64 replace_state; /* see #define above */
232 time64_t time_started; /* seconds since 1-Jan-1970 */
233 time64_t time_stopped; /* seconds since 1-Jan-1970 */
234 atomic64_t num_write_errors;
235 atomic64_t num_uncorrectable_read_errors;
238 u64 committed_cursor_left;
239 u64 cursor_left_last_write_of_item;
242 u64 cont_reading_from_srcdev_mode; /* see #define above */
245 int item_needs_writeback;
246 struct btrfs_device *srcdev;
247 struct btrfs_device *tgtdev;
249 struct mutex lock_finishing_cancel_unmount;
250 struct rw_semaphore rwsem;
252 struct btrfs_scrub_progress scrub_progress;
254 struct percpu_counter bio_counter;
255 wait_queue_head_t replace_wait;
259 * free clusters are used to claim free space in relatively large chunks,
260 * allowing us to do less seeky writes. They are used for all metadata
261 * allocations. In ssd_spread mode they are also used for data allocations.
263 struct btrfs_free_cluster {
265 spinlock_t refill_lock;
268 /* largest extent in this cluster */
271 /* first extent starting offset */
274 /* We did a full search and couldn't create a cluster */
277 struct btrfs_block_group *block_group;
279 * when a cluster is allocated from a block group, we put the
280 * cluster onto a list in the block group so that it can
281 * be freed before the block group is freed.
283 struct list_head block_group_list;
286 /* Discard control. */
288 * Async discard uses multiple lists to differentiate the discard filter
289 * parameters. Index 0 is for completely free block groups where we need to
290 * ensure the entire block group is trimmed without being lossy. Indices
291 * afterwards represent monotonically decreasing discard filter sizes to
292 * prioritize what should be discarded next.
294 #define BTRFS_NR_DISCARD_LISTS 3
295 #define BTRFS_DISCARD_INDEX_UNUSED 0
296 #define BTRFS_DISCARD_INDEX_START 1
298 struct btrfs_discard_ctl {
299 struct workqueue_struct *discard_workers;
300 struct delayed_work work;
302 struct btrfs_block_group *block_group;
303 struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
305 u64 prev_discard_time;
306 atomic_t discardable_extents;
307 atomic64_t discardable_bytes;
308 u64 max_discard_size;
312 u64 discard_extent_bytes;
313 u64 discard_bitmap_bytes;
314 atomic64_t discard_bytes_saved;
318 BTRFS_FS_CLOSING_START,
319 BTRFS_FS_CLOSING_DONE,
320 BTRFS_FS_LOG_RECOVERING,
322 BTRFS_FS_QUOTA_ENABLED,
323 BTRFS_FS_UPDATE_UUID_TREE_GEN,
324 BTRFS_FS_CREATING_FREE_SPACE_TREE,
328 BTRFS_FS_QUOTA_OVERRIDE,
329 /* Used to record internally whether fs has been frozen */
332 * Indicate that balance has been set up from the ioctl and is in the
333 * main phase. The fs_info::balance_ctl is initialized.
335 BTRFS_FS_BALANCE_RUNNING,
338 * Indicate that relocation of a chunk has started, it's set per chunk
339 * and is toggled between chunks.
341 BTRFS_FS_RELOC_RUNNING,
343 /* Indicate that the cleaner thread is awake and doing something. */
344 BTRFS_FS_CLEANER_RUNNING,
347 * The checksumming has an optimized version and is considered fast,
348 * so we don't need to offload checksums to workqueues.
350 BTRFS_FS_CSUM_IMPL_FAST,
352 /* Indicate that the discard workqueue can service discards. */
353 BTRFS_FS_DISCARD_RUNNING,
355 /* Indicate that we need to cleanup space cache v1 */
356 BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
358 /* Indicate that we can't trust the free space tree for caching yet */
359 BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
361 /* Indicate whether there are any tree modification log users */
362 BTRFS_FS_TREE_MOD_LOG_USERS,
364 /* Indicate that we want the transaction kthread to commit right now. */
365 BTRFS_FS_COMMIT_TRANS,
367 /* Indicate we have half completed snapshot deletions pending. */
368 BTRFS_FS_UNFINISHED_DROPS,
370 /* Indicate we have to finish a zone to do next allocation. */
371 BTRFS_FS_NEED_ZONE_FINISH,
373 #if BITS_PER_LONG == 32
374 /* Indicate if we have error/warn message printed on 32bit systems */
375 BTRFS_FS_32BIT_ERROR,
381 * Exclusive operations (device replace, resize, device add/remove, balance)
383 enum btrfs_exclusive_operation {
385 BTRFS_EXCLOP_BALANCE_PAUSED,
386 BTRFS_EXCLOP_BALANCE,
387 BTRFS_EXCLOP_DEV_ADD,
388 BTRFS_EXCLOP_DEV_REMOVE,
389 BTRFS_EXCLOP_DEV_REPLACE,
391 BTRFS_EXCLOP_SWAP_ACTIVATE,
394 /* Store data about transaction commits, exported via sysfs. */
395 struct btrfs_commit_stats {
396 /* Total number of commits */
398 /* The maximum commit duration so far in ns */
400 /* The last commit duration in ns */
402 /* The total commit duration in ns */
403 u64 total_commit_dur;
406 struct btrfs_fs_info {
407 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
409 struct btrfs_root *tree_root;
410 struct btrfs_root *chunk_root;
411 struct btrfs_root *dev_root;
412 struct btrfs_root *fs_root;
413 struct btrfs_root *quota_root;
414 struct btrfs_root *uuid_root;
415 struct btrfs_root *data_reloc_root;
416 struct btrfs_root *block_group_root;
418 /* the log root tree is a directory of all the other log roots */
419 struct btrfs_root *log_root_tree;
421 /* The tree that holds the global roots (csum, extent, etc) */
422 rwlock_t global_root_lock;
423 struct rb_root global_root_tree;
425 spinlock_t fs_roots_radix_lock;
426 struct radix_tree_root fs_roots_radix;
428 /* block group cache stuff */
429 rwlock_t block_group_cache_lock;
430 struct rb_root_cached block_group_cache_tree;
432 /* keep track of unallocated space */
433 atomic64_t free_chunk_space;
435 /* Track ranges which are used by log trees blocks/logged data extents */
436 struct extent_io_tree excluded_extents;
438 /* logical->physical extent mapping */
439 struct extent_map_tree mapping_tree;
442 * block reservation for extent, checksum, root tree and
443 * delayed dir index item
445 struct btrfs_block_rsv global_block_rsv;
446 /* block reservation for metadata operations */
447 struct btrfs_block_rsv trans_block_rsv;
448 /* block reservation for chunk tree */
449 struct btrfs_block_rsv chunk_block_rsv;
450 /* block reservation for delayed operations */
451 struct btrfs_block_rsv delayed_block_rsv;
452 /* block reservation for delayed refs */
453 struct btrfs_block_rsv delayed_refs_rsv;
455 struct btrfs_block_rsv empty_block_rsv;
458 u64 last_trans_committed;
460 * Generation of the last transaction used for block group relocation
461 * since the filesystem was last mounted (or 0 if none happened yet).
462 * Must be written and read while holding btrfs_fs_info::commit_root_sem.
464 u64 last_reloc_trans;
465 u64 avg_delayed_ref_runtime;
468 * this is updated to the current trans every time a full commit
469 * is required instead of the faster short fsync log commits
471 u64 last_trans_log_full_commit;
472 unsigned long mount_opt;
474 * Track requests for actions that need to be done during transaction
475 * commit (like for some mount options).
477 unsigned long pending_changes;
478 unsigned long compress_type:4;
479 unsigned int compress_level;
482 * It is a suggestive number, the read side is safe even it gets a
483 * wrong number because we will write out the data into a regular
484 * extent. The write side(mount/remount) is under ->s_umount lock,
485 * so it is also safe.
489 struct btrfs_transaction *running_transaction;
490 wait_queue_head_t transaction_throttle;
491 wait_queue_head_t transaction_wait;
492 wait_queue_head_t transaction_blocked_wait;
493 wait_queue_head_t async_submit_wait;
496 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
497 * when they are updated.
499 * Because we do not clear the flags for ever, so we needn't use
500 * the lock on the read side.
502 * We also needn't use the lock when we mount the fs, because
503 * there is no other task which will update the flag.
505 spinlock_t super_lock;
506 struct btrfs_super_block *super_copy;
507 struct btrfs_super_block *super_for_commit;
508 struct super_block *sb;
509 struct inode *btree_inode;
510 struct mutex tree_log_mutex;
511 struct mutex transaction_kthread_mutex;
512 struct mutex cleaner_mutex;
513 struct mutex chunk_mutex;
516 * this is taken to make sure we don't set block groups ro after
517 * the free space cache has been allocated on them
519 struct mutex ro_block_group_mutex;
521 /* this is used during read/modify/write to make sure
522 * no two ios are trying to mod the same stripe at the same
525 struct btrfs_stripe_hash_table *stripe_hash_table;
528 * this protects the ordered operations list only while we are
529 * processing all of the entries on it. This way we make
530 * sure the commit code doesn't find the list temporarily empty
531 * because another function happens to be doing non-waiting preflush
532 * before jumping into the main commit.
534 struct mutex ordered_operations_mutex;
536 struct rw_semaphore commit_root_sem;
538 struct rw_semaphore cleanup_work_sem;
540 struct rw_semaphore subvol_sem;
542 spinlock_t trans_lock;
544 * the reloc mutex goes with the trans lock, it is taken
545 * during commit to protect us from the relocation code
547 struct mutex reloc_mutex;
549 struct list_head trans_list;
550 struct list_head dead_roots;
551 struct list_head caching_block_groups;
553 spinlock_t delayed_iput_lock;
554 struct list_head delayed_iputs;
555 atomic_t nr_delayed_iputs;
556 wait_queue_head_t delayed_iputs_wait;
558 atomic64_t tree_mod_seq;
560 /* this protects tree_mod_log and tree_mod_seq_list */
561 rwlock_t tree_mod_log_lock;
562 struct rb_root tree_mod_log;
563 struct list_head tree_mod_seq_list;
565 atomic_t async_delalloc_pages;
568 * this is used to protect the following list -- ordered_roots.
570 spinlock_t ordered_root_lock;
573 * all fs/file tree roots in which there are data=ordered extents
574 * pending writeback are added into this list.
576 * these can span multiple transactions and basically include
577 * every dirty data page that isn't from nodatacow
579 struct list_head ordered_roots;
581 struct mutex delalloc_root_mutex;
582 spinlock_t delalloc_root_lock;
583 /* all fs/file tree roots that have delalloc inodes. */
584 struct list_head delalloc_roots;
587 * there is a pool of worker threads for checksumming during writes
588 * and a pool for checksumming after reads. This is because readers
589 * can run with FS locks held, and the writers may be waiting for
590 * those locks. We don't want ordering in the pending list to cause
591 * deadlocks, and so the two are serviced separately.
593 * A third pool does submit_bio to avoid deadlocking with the other
596 struct btrfs_workqueue *workers;
597 struct btrfs_workqueue *hipri_workers;
598 struct btrfs_workqueue *delalloc_workers;
599 struct btrfs_workqueue *flush_workers;
600 struct workqueue_struct *endio_workers;
601 struct workqueue_struct *endio_meta_workers;
602 struct workqueue_struct *endio_raid56_workers;
603 struct workqueue_struct *rmw_workers;
604 struct workqueue_struct *compressed_write_workers;
605 struct btrfs_workqueue *endio_write_workers;
606 struct btrfs_workqueue *endio_freespace_worker;
607 struct btrfs_workqueue *caching_workers;
610 * fixup workers take dirty pages that didn't properly go through
611 * the cow mechanism and make them safe to write. It happens
612 * for the sys_munmap function call path
614 struct btrfs_workqueue *fixup_workers;
615 struct btrfs_workqueue *delayed_workers;
617 struct task_struct *transaction_kthread;
618 struct task_struct *cleaner_kthread;
619 u32 thread_pool_size;
621 struct kobject *space_info_kobj;
622 struct kobject *qgroups_kobj;
623 struct kobject *discard_kobj;
625 /* used to keep from writing metadata until there is a nice batch */
626 struct percpu_counter dirty_metadata_bytes;
627 struct percpu_counter delalloc_bytes;
628 struct percpu_counter ordered_bytes;
629 s32 dirty_metadata_batch;
632 struct list_head dirty_cowonly_roots;
634 struct btrfs_fs_devices *fs_devices;
637 * The space_info list is effectively read only after initial
638 * setup. It is populated at mount time and cleaned up after
639 * all block groups are removed. RCU is used to protect it.
641 struct list_head space_info;
643 struct btrfs_space_info *data_sinfo;
645 struct reloc_control *reloc_ctl;
647 /* data_alloc_cluster is only used in ssd_spread mode */
648 struct btrfs_free_cluster data_alloc_cluster;
650 /* all metadata allocations go through this cluster */
651 struct btrfs_free_cluster meta_alloc_cluster;
653 /* auto defrag inodes go here */
654 spinlock_t defrag_inodes_lock;
655 struct rb_root defrag_inodes;
656 atomic_t defrag_running;
658 /* Used to protect avail_{data, metadata, system}_alloc_bits */
659 seqlock_t profiles_lock;
661 * these three are in extended format (availability of single
662 * chunks is denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other
663 * types are denoted by corresponding BTRFS_BLOCK_GROUP_* bits)
665 u64 avail_data_alloc_bits;
666 u64 avail_metadata_alloc_bits;
667 u64 avail_system_alloc_bits;
669 /* restriper state */
670 spinlock_t balance_lock;
671 struct mutex balance_mutex;
672 atomic_t balance_pause_req;
673 atomic_t balance_cancel_req;
674 struct btrfs_balance_control *balance_ctl;
675 wait_queue_head_t balance_wait_q;
677 /* Cancellation requests for chunk relocation */
678 atomic_t reloc_cancel_req;
680 u32 data_chunk_allocations;
685 /* private scrub information */
686 struct mutex scrub_lock;
687 atomic_t scrubs_running;
688 atomic_t scrub_pause_req;
689 atomic_t scrubs_paused;
690 atomic_t scrub_cancel_req;
691 wait_queue_head_t scrub_pause_wait;
693 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
696 refcount_t scrub_workers_refcnt;
697 struct workqueue_struct *scrub_workers;
698 struct workqueue_struct *scrub_wr_completion_workers;
699 struct workqueue_struct *scrub_parity_workers;
700 struct btrfs_subpage_info *subpage_info;
702 struct btrfs_discard_ctl discard_ctl;
704 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
705 u32 check_integrity_print_mask;
707 /* is qgroup tracking in a consistent state? */
710 /* holds configuration and tracking. Protected by qgroup_lock */
711 struct rb_root qgroup_tree;
712 spinlock_t qgroup_lock;
715 * used to avoid frequently calling ulist_alloc()/ulist_free()
716 * when doing qgroup accounting, it must be protected by qgroup_lock.
718 struct ulist *qgroup_ulist;
721 * Protect user change for quota operations. If a transaction is needed,
722 * it must be started before locking this lock.
724 struct mutex qgroup_ioctl_lock;
726 /* list of dirty qgroups to be written at next commit */
727 struct list_head dirty_qgroups;
729 /* used by qgroup for an efficient tree traversal */
732 /* qgroup rescan items */
733 struct mutex qgroup_rescan_lock; /* protects the progress item */
734 struct btrfs_key qgroup_rescan_progress;
735 struct btrfs_workqueue *qgroup_rescan_workers;
736 struct completion qgroup_rescan_completion;
737 struct btrfs_work qgroup_rescan_work;
738 bool qgroup_rescan_running; /* protected by qgroup_rescan_lock */
739 u8 qgroup_drop_subtree_thres;
741 /* filesystem state */
742 unsigned long fs_state;
744 struct btrfs_delayed_root *delayed_root;
746 /* Extent buffer radix tree */
747 spinlock_t buffer_lock;
748 /* Entries are eb->start / sectorsize */
749 struct radix_tree_root buffer_radix;
751 /* next backup root to be overwritten */
752 int backup_root_index;
754 /* device replace state */
755 struct btrfs_dev_replace dev_replace;
757 struct semaphore uuid_tree_rescan_sem;
759 /* Used to reclaim the metadata space in the background. */
760 struct work_struct async_reclaim_work;
761 struct work_struct async_data_reclaim_work;
762 struct work_struct preempt_reclaim_work;
764 /* Reclaim partially filled block groups in the background */
765 struct work_struct reclaim_bgs_work;
766 struct list_head reclaim_bgs;
767 int bg_reclaim_threshold;
769 spinlock_t unused_bgs_lock;
770 struct list_head unused_bgs;
771 struct mutex unused_bg_unpin_mutex;
772 /* Protect block groups that are going to be deleted */
773 struct mutex reclaim_bgs_lock;
775 /* Cached block sizes */
778 /* ilog2 of sectorsize, use to avoid 64bit division */
785 * Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular
786 * filesystem, on zoned it depends on the device constraints.
790 /* Block groups and devices containing active swapfiles. */
791 spinlock_t swapfile_pins_lock;
792 struct rb_root swapfile_pins;
794 struct crypto_shash *csum_shash;
796 /* Type of exclusive operation running, protected by super_lock */
797 enum btrfs_exclusive_operation exclusive_operation;
800 * Zone size > 0 when in ZONED mode, otherwise it's used for a check
801 * if the mode is enabled
805 /* Max size to emit ZONE_APPEND write command */
806 u64 max_zone_append_size;
807 struct mutex zoned_meta_io_lock;
808 spinlock_t treelog_bg_lock;
812 * Start of the dedicated data relocation block group, protected by
813 * relocation_bg_lock.
815 spinlock_t relocation_bg_lock;
817 struct mutex zoned_data_reloc_io_lock;
821 spinlock_t zone_active_bgs_lock;
822 struct list_head zone_active_bgs;
824 /* Updates are not protected by any lock */
825 struct btrfs_commit_stats commit_stats;
828 * Last generation where we dropped a non-relocation root.
829 * Use btrfs_set_last_root_drop_gen() and btrfs_get_last_root_drop_gen()
830 * to change it and to read it, respectively.
832 u64 last_root_drop_gen;
835 * Annotations for transaction events (structures are empty when
836 * compiled without lockdep).
838 struct lockdep_map btrfs_trans_num_writers_map;
839 struct lockdep_map btrfs_trans_num_extwriters_map;
840 struct lockdep_map btrfs_state_change_map[4];
841 struct lockdep_map btrfs_trans_pending_ordered_map;
842 struct lockdep_map btrfs_ordered_extent_map;
844 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
845 spinlock_t ref_verify_lock;
846 struct rb_root block_tree;
849 #ifdef CONFIG_BTRFS_DEBUG
850 struct kobject *debug_kobj;
851 struct list_head allocated_roots;
853 spinlock_t eb_leak_lock;
854 struct list_head allocated_ebs;
858 static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
861 WRITE_ONCE(fs_info->last_root_drop_gen, gen);
864 static inline u64 btrfs_get_last_root_drop_gen(const struct btrfs_fs_info *fs_info)
866 return READ_ONCE(fs_info->last_root_drop_gen);
869 static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
871 return sb->s_fs_info;
875 * Take the number of bytes to be checksummed and figure out how many leaves
876 * it would require to store the csums for that many bytes.
878 static inline u64 btrfs_csum_bytes_to_leaves(
879 const struct btrfs_fs_info *fs_info, u64 csum_bytes)
881 const u64 num_csums = csum_bytes >> fs_info->sectorsize_bits;
883 return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
887 * Use this if we would be adding new items, as we could split nodes as we cow
890 static inline u64 btrfs_calc_insert_metadata_size(struct btrfs_fs_info *fs_info,
893 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
897 * Doing a truncate or a modification won't result in new nodes or leaves, just
898 * what we need for COW.
900 static inline u64 btrfs_calc_metadata_size(struct btrfs_fs_info *fs_info,
903 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
906 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
907 sizeof(struct btrfs_item))
909 static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info)
911 return fs_info->zone_size > 0;
915 * Count how many fs_info->max_extent_size cover the @size
917 static inline u32 count_max_extents(struct btrfs_fs_info *fs_info, u64 size)
919 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
921 return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
924 return div_u64(size + fs_info->max_extent_size - 1, fs_info->max_extent_size);
927 bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
928 enum btrfs_exclusive_operation type);
929 bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
930 enum btrfs_exclusive_operation type);
931 void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info);
932 void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
933 void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
934 enum btrfs_exclusive_operation op);
937 * The state of btrfs root
941 * btrfs_record_root_in_trans is a multi-step process, and it can race
942 * with the balancing code. But the race is very small, and only the
943 * first time the root is added to each transaction. So IN_TRANS_SETUP
944 * is used to tell us when more checks are required
946 BTRFS_ROOT_IN_TRANS_SETUP,
949 * Set if tree blocks of this root can be shared by other roots.
950 * Only subvolume trees and their reloc trees have this bit set.
951 * Conflicts with TRACK_DIRTY bit.
953 * This affects two things:
955 * - How balance works
956 * For shareable roots, we need to use reloc tree and do path
957 * replacement for balance, and need various pre/post hooks for
958 * snapshot creation to handle them.
960 * While for non-shareable trees, we just simply do a tree search
963 * - How dirty roots are tracked
964 * For shareable roots, btrfs_record_root_in_trans() is needed to
965 * track them, while non-subvolume roots have TRACK_DIRTY bit, they
966 * don't need to set this manually.
968 BTRFS_ROOT_SHAREABLE,
969 BTRFS_ROOT_TRACK_DIRTY,
971 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
972 BTRFS_ROOT_DEFRAG_RUNNING,
973 BTRFS_ROOT_FORCE_COW,
974 BTRFS_ROOT_MULTI_LOG_TASKS,
979 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
981 * Set for the subvolume tree owning the reloc tree.
983 BTRFS_ROOT_DEAD_RELOC_TREE,
984 /* Mark dead root stored on device whose cleanup needs to be resumed */
985 BTRFS_ROOT_DEAD_TREE,
986 /* The root has a log tree. Used for subvolume roots and the tree root. */
987 BTRFS_ROOT_HAS_LOG_TREE,
988 /* Qgroup flushing is in progress */
989 BTRFS_ROOT_QGROUP_FLUSHING,
990 /* We started the orphan cleanup for this root. */
991 BTRFS_ROOT_ORPHAN_CLEANUP,
992 /* This root has a drop operation that was started previously. */
993 BTRFS_ROOT_UNFINISHED_DROP,
994 /* This reloc root needs to have its buffers lockdep class reset. */
995 BTRFS_ROOT_RESET_LOCKDEP_CLASS,
998 enum btrfs_lockdep_trans_states {
999 BTRFS_LOCKDEP_TRANS_COMMIT_START,
1000 BTRFS_LOCKDEP_TRANS_UNBLOCKED,
1001 BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED,
1002 BTRFS_LOCKDEP_TRANS_COMPLETED,
1006 * Lockdep annotation for wait events.
1008 * @owner: The struct where the lockdep map is defined
1009 * @lock: The lockdep map corresponding to a wait event
1011 * This macro is used to annotate a wait event. In this case a thread acquires
1012 * the lockdep map as writer (exclusive lock) because it has to block until all
1013 * the threads that hold the lock as readers signal the condition for the wait
1014 * event and release their locks.
1016 #define btrfs_might_wait_for_event(owner, lock) \
1018 rwsem_acquire(&owner->lock##_map, 0, 0, _THIS_IP_); \
1019 rwsem_release(&owner->lock##_map, _THIS_IP_); \
1023 * Protection for the resource/condition of a wait event.
1025 * @owner: The struct where the lockdep map is defined
1026 * @lock: The lockdep map corresponding to a wait event
1028 * Many threads can modify the condition for the wait event at the same time
1029 * and signal the threads that block on the wait event. The threads that modify
1030 * the condition and do the signaling acquire the lock as readers (shared
1033 #define btrfs_lockdep_acquire(owner, lock) \
1034 rwsem_acquire_read(&owner->lock##_map, 0, 0, _THIS_IP_)
1037 * Used after signaling the condition for a wait event to release the lockdep
1038 * map held by a reader thread.
1040 #define btrfs_lockdep_release(owner, lock) \
1041 rwsem_release(&owner->lock##_map, _THIS_IP_)
1044 * Macros for the transaction states wait events, similar to the generic wait
1047 #define btrfs_might_wait_for_state(owner, i) \
1049 rwsem_acquire(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_); \
1050 rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_); \
1053 #define btrfs_trans_state_lockdep_acquire(owner, i) \
1054 rwsem_acquire_read(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_)
1056 #define btrfs_trans_state_lockdep_release(owner, i) \
1057 rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_)
1059 /* Initialization of the lockdep map */
1060 #define btrfs_lockdep_init_map(owner, lock) \
1062 static struct lock_class_key lock##_key; \
1063 lockdep_init_map(&owner->lock##_map, #lock, &lock##_key, 0); \
1066 /* Initialization of the transaction states lockdep maps. */
1067 #define btrfs_state_lockdep_init_map(owner, lock, state) \
1069 static struct lock_class_key lock##_key; \
1070 lockdep_init_map(&owner->btrfs_state_change_map[state], #lock, \
1074 static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
1076 clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
1080 * Record swapped tree blocks of a subvolume tree for delayed subtree trace
1081 * code. For detail check comment in fs/btrfs/qgroup.c.
1083 struct btrfs_qgroup_swapped_blocks {
1085 /* RM_EMPTY_ROOT() of above blocks[] */
1087 struct rb_root blocks[BTRFS_MAX_LEVEL];
1091 * in ram representation of the tree. extent_root is used for all allocations
1092 * and for the extent tree extent_root root.
1095 struct rb_node rb_node;
1097 struct extent_buffer *node;
1099 struct extent_buffer *commit_root;
1100 struct btrfs_root *log_root;
1101 struct btrfs_root *reloc_root;
1103 unsigned long state;
1104 struct btrfs_root_item root_item;
1105 struct btrfs_key root_key;
1106 struct btrfs_fs_info *fs_info;
1107 struct extent_io_tree dirty_log_pages;
1109 struct mutex objectid_mutex;
1111 spinlock_t accounting_lock;
1112 struct btrfs_block_rsv *block_rsv;
1114 struct mutex log_mutex;
1115 wait_queue_head_t log_writer_wait;
1116 wait_queue_head_t log_commit_wait[2];
1117 struct list_head log_ctxs[2];
1118 /* Used only for log trees of subvolumes, not for the log root tree */
1119 atomic_t log_writers;
1120 atomic_t log_commit[2];
1121 /* Used only for log trees of subvolumes, not for the log root tree */
1124 /* No matter the commit succeeds or not*/
1125 int log_transid_committed;
1126 /* Just be updated when the commit succeeds. */
1127 int last_log_commit;
1128 pid_t log_start_pid;
1136 struct btrfs_key defrag_progress;
1137 struct btrfs_key defrag_max;
1139 /* The dirty list is only used by non-shareable roots */
1140 struct list_head dirty_list;
1142 struct list_head root_list;
1144 spinlock_t log_extents_lock[2];
1145 struct list_head logged_list[2];
1147 spinlock_t inode_lock;
1148 /* red-black tree that keeps track of in-memory inodes */
1149 struct rb_root inode_tree;
1152 * radix tree that keeps track of delayed nodes of every inode,
1153 * protected by inode_lock
1155 struct radix_tree_root delayed_nodes_tree;
1157 * right now this just gets used so that a root has its own devid
1158 * for stat. It may be used for more later
1162 spinlock_t root_item_lock;
1165 struct mutex delalloc_mutex;
1166 spinlock_t delalloc_lock;
1168 * all of the inodes that have delalloc bytes. It is possible for
1169 * this list to be empty even when there is still dirty data=ordered
1170 * extents waiting to finish IO.
1172 struct list_head delalloc_inodes;
1173 struct list_head delalloc_root;
1174 u64 nr_delalloc_inodes;
1176 struct mutex ordered_extent_mutex;
1178 * this is used by the balancing code to wait for all the pending
1181 spinlock_t ordered_extent_lock;
1184 * all of the data=ordered extents pending writeback
1185 * these can span multiple transactions and basically include
1186 * every dirty data page that isn't from nodatacow
1188 struct list_head ordered_extents;
1189 struct list_head ordered_root;
1190 u64 nr_ordered_extents;
1193 * Not empty if this subvolume root has gone through tree block swap
1196 * Will be used by reloc_control::dirty_subvol_roots.
1198 struct list_head reloc_dirty_list;
1201 * Number of currently running SEND ioctls to prevent
1202 * manipulation with the read-only status via SUBVOL_SETFLAGS
1204 int send_in_progress;
1206 * Number of currently running deduplication operations that have a
1207 * destination inode belonging to this root. Protected by the lock
1210 int dedupe_in_progress;
1211 /* For exclusion of snapshot creation and nocow writes */
1212 struct btrfs_drew_lock snapshot_lock;
1214 atomic_t snapshot_force_cow;
1216 /* For qgroup metadata reserved space */
1217 spinlock_t qgroup_meta_rsv_lock;
1218 u64 qgroup_meta_rsv_pertrans;
1219 u64 qgroup_meta_rsv_prealloc;
1220 wait_queue_head_t qgroup_flush_wait;
1222 /* Number of active swapfiles */
1223 atomic_t nr_swapfiles;
1225 /* Record pairs of swapped blocks for qgroup */
1226 struct btrfs_qgroup_swapped_blocks swapped_blocks;
1228 /* Used only by log trees, when logging csum items */
1229 struct extent_io_tree log_csum_range;
1231 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1235 #ifdef CONFIG_BTRFS_DEBUG
1236 struct list_head leak_list;
1241 * Structure that conveys information about an extent that is going to replace
1242 * all the extents in a file range.
1244 struct btrfs_replace_extent_info {
1250 /* Pointer to a file extent item of type regular or prealloc. */
1253 * Set to true when attempting to replace a file range with a new extent
1254 * described by this structure, set to false when attempting to clone an
1255 * existing extent into a file range.
1258 /* Indicate if we should update the inode's mtime and ctime. */
1260 /* Meaningful only if is_new_extent is true. */
1261 int qgroup_reserved;
1263 * Meaningful only if is_new_extent is true.
1264 * Used to track how many extent items we have already inserted in a
1265 * subvolume tree that refer to the extent described by this structure,
1266 * so that we know when to create a new delayed ref or update an existing
1272 /* Arguments for btrfs_drop_extents() */
1273 struct btrfs_drop_extents_args {
1274 /* Input parameters */
1277 * If NULL, btrfs_drop_extents() will allocate and free its own path.
1278 * If 'replace_extent' is true, this must not be NULL. Also the path
1279 * is always released except if 'replace_extent' is true and
1280 * btrfs_drop_extents() sets 'extent_inserted' to true, in which case
1281 * the path is kept locked.
1283 struct btrfs_path *path;
1284 /* Start offset of the range to drop extents from */
1286 /* End (exclusive, last byte + 1) of the range to drop extents from */
1288 /* If true drop all the extent maps in the range */
1291 * If true it means we want to insert a new extent after dropping all
1292 * the extents in the range. If this is true, the 'extent_item_size'
1293 * parameter must be set as well and the 'extent_inserted' field will
1294 * be set to true by btrfs_drop_extents() if it could insert the new
1296 * Note: when this is set to true the path must not be NULL.
1298 bool replace_extent;
1300 * Used if 'replace_extent' is true. Size of the file extent item to
1301 * insert after dropping all existing extents in the range
1303 u32 extent_item_size;
1305 /* Output parameters */
1308 * Set to the minimum between the input parameter 'end' and the end
1309 * (exclusive, last byte + 1) of the last dropped extent. This is always
1310 * set even if btrfs_drop_extents() returns an error.
1314 * The number of allocated bytes found in the range. This can be smaller
1315 * than the range's length when there are holes in the range.
1319 * Only set if 'replace_extent' is true. Set to true if we were able
1320 * to insert a replacement extent after dropping all extents in the
1321 * range, otherwise set to false by btrfs_drop_extents().
1322 * Also, if btrfs_drop_extents() has set this to true it means it
1323 * returned with the path locked, otherwise if it has set this to
1324 * false it has returned with the path released.
1326 bool extent_inserted;
1329 struct btrfs_file_private {
1334 static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
1337 return info->nodesize - sizeof(struct btrfs_header);
1340 #define BTRFS_LEAF_DATA_OFFSET offsetof(struct btrfs_leaf, items)
1342 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
1344 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
1347 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
1349 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
1352 #define BTRFS_FILE_EXTENT_INLINE_DATA_START \
1353 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
1354 static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
1356 return BTRFS_MAX_ITEM_SIZE(info) -
1357 BTRFS_FILE_EXTENT_INLINE_DATA_START;
1360 static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1362 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
1366 * Flags for mount options.
1368 * Note: don't forget to add new options to btrfs_show_options()
1371 BTRFS_MOUNT_NODATASUM = (1UL << 0),
1372 BTRFS_MOUNT_NODATACOW = (1UL << 1),
1373 BTRFS_MOUNT_NOBARRIER = (1UL << 2),
1374 BTRFS_MOUNT_SSD = (1UL << 3),
1375 BTRFS_MOUNT_DEGRADED = (1UL << 4),
1376 BTRFS_MOUNT_COMPRESS = (1UL << 5),
1377 BTRFS_MOUNT_NOTREELOG = (1UL << 6),
1378 BTRFS_MOUNT_FLUSHONCOMMIT = (1UL << 7),
1379 BTRFS_MOUNT_SSD_SPREAD = (1UL << 8),
1380 BTRFS_MOUNT_NOSSD = (1UL << 9),
1381 BTRFS_MOUNT_DISCARD_SYNC = (1UL << 10),
1382 BTRFS_MOUNT_FORCE_COMPRESS = (1UL << 11),
1383 BTRFS_MOUNT_SPACE_CACHE = (1UL << 12),
1384 BTRFS_MOUNT_CLEAR_CACHE = (1UL << 13),
1385 BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED = (1UL << 14),
1386 BTRFS_MOUNT_ENOSPC_DEBUG = (1UL << 15),
1387 BTRFS_MOUNT_AUTO_DEFRAG = (1UL << 16),
1388 BTRFS_MOUNT_USEBACKUPROOT = (1UL << 17),
1389 BTRFS_MOUNT_SKIP_BALANCE = (1UL << 18),
1390 BTRFS_MOUNT_CHECK_INTEGRITY = (1UL << 19),
1391 BTRFS_MOUNT_CHECK_INTEGRITY_DATA = (1UL << 20),
1392 BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 21),
1393 BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 22),
1394 BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 23),
1395 BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 24),
1396 BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 25),
1397 BTRFS_MOUNT_NOLOGREPLAY = (1UL << 26),
1398 BTRFS_MOUNT_REF_VERIFY = (1UL << 27),
1399 BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),
1400 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),
1401 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
1404 #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
1405 #define BTRFS_DEFAULT_MAX_INLINE (2048)
1407 #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
1408 #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
1409 #define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
1410 #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
1413 #define btrfs_set_and_info(fs_info, opt, fmt, args...) \
1415 if (!btrfs_test_opt(fs_info, opt)) \
1416 btrfs_info(fs_info, fmt, ##args); \
1417 btrfs_set_opt(fs_info->mount_opt, opt); \
1420 #define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
1422 if (btrfs_test_opt(fs_info, opt)) \
1423 btrfs_info(fs_info, fmt, ##args); \
1424 btrfs_clear_opt(fs_info->mount_opt, opt); \
1428 * Requests for changes that need to be done during transaction commit.
1430 * Internal mount options that are used for special handling of the real
1431 * mount options (eg. cannot be set during remount and have to be set during
1432 * transaction commit)
1435 #define BTRFS_PENDING_COMMIT (0)
1437 #define btrfs_test_pending(info, opt) \
1438 test_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1439 #define btrfs_set_pending(info, opt) \
1440 set_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1441 #define btrfs_clear_pending(info, opt) \
1442 clear_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1444 struct btrfs_map_token {
1445 struct extent_buffer *eb;
1447 unsigned long offset;
1450 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
1451 ((bytes) >> (fs_info)->sectorsize_bits)
1453 static inline void btrfs_init_map_token(struct btrfs_map_token *token,
1454 struct extent_buffer *eb)
1457 token->kaddr = page_address(eb->pages[0]);
1461 /* some macros to generate set/get functions for the struct fields. This
1462 * assumes there is a lefoo_to_cpu for every type, so lets make a simple
1465 #define le8_to_cpu(v) (v)
1466 #define cpu_to_le8(v) (v)
1469 static inline u8 get_unaligned_le8(const void *p)
1474 static inline void put_unaligned_le8(u8 val, void *p)
1479 #define read_eb_member(eb, ptr, type, member, result) (\
1480 read_extent_buffer(eb, (char *)(result), \
1481 ((unsigned long)(ptr)) + \
1482 offsetof(type, member), \
1483 sizeof(((type *)0)->member)))
1485 #define write_eb_member(eb, ptr, type, member, result) (\
1486 write_extent_buffer(eb, (char *)(result), \
1487 ((unsigned long)(ptr)) + \
1488 offsetof(type, member), \
1489 sizeof(((type *)0)->member)))
1491 #define DECLARE_BTRFS_SETGET_BITS(bits) \
1492 u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \
1493 const void *ptr, unsigned long off); \
1494 void btrfs_set_token_##bits(struct btrfs_map_token *token, \
1495 const void *ptr, unsigned long off, \
1497 u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
1498 const void *ptr, unsigned long off); \
1499 void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
1500 unsigned long off, u##bits val);
1502 DECLARE_BTRFS_SETGET_BITS(8)
1503 DECLARE_BTRFS_SETGET_BITS(16)
1504 DECLARE_BTRFS_SETGET_BITS(32)
1505 DECLARE_BTRFS_SETGET_BITS(64)
1507 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
1508 static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
1511 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
1512 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
1514 static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
1517 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
1518 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
1520 static inline u##bits btrfs_token_##name(struct btrfs_map_token *token, \
1523 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
1524 return btrfs_get_token_##bits(token, s, offsetof(type, member));\
1526 static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
1527 type *s, u##bits val) \
1529 static_assert(sizeof(u##bits) == sizeof(((type *)0))->member); \
1530 btrfs_set_token_##bits(token, s, offsetof(type, member), val); \
1533 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
1534 static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
1536 const type *p = page_address(eb->pages[0]) + \
1537 offset_in_page(eb->start); \
1538 return get_unaligned_le##bits(&p->member); \
1540 static inline void btrfs_set_##name(const struct extent_buffer *eb, \
1543 type *p = page_address(eb->pages[0]) + offset_in_page(eb->start); \
1544 put_unaligned_le##bits(val, &p->member); \
1547 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
1548 static inline u##bits btrfs_##name(const type *s) \
1550 return get_unaligned_le##bits(&s->member); \
1552 static inline void btrfs_set_##name(type *s, u##bits val) \
1554 put_unaligned_le##bits(val, &s->member); \
1557 static inline u64 btrfs_device_total_bytes(const struct extent_buffer *eb,
1558 struct btrfs_dev_item *s)
1560 static_assert(sizeof(u64) ==
1561 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1562 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
1565 static inline void btrfs_set_device_total_bytes(const struct extent_buffer *eb,
1566 struct btrfs_dev_item *s,
1569 static_assert(sizeof(u64) ==
1570 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1571 WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
1572 btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
1576 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
1577 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
1578 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
1579 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
1580 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
1582 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
1583 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
1584 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
1585 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
1586 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
1587 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
1589 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
1590 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
1592 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
1594 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
1596 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
1598 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
1600 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
1601 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
1603 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
1605 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
1607 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
1610 static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
1612 return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
1615 static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
1617 return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
1620 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
1621 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
1622 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
1623 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
1624 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
1625 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
1626 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
1627 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
1628 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
1629 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
1630 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
1632 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
1634 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
1637 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
1638 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
1639 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
1641 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
1643 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
1645 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
1647 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
1648 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
1650 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
1652 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
1653 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
1655 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
1658 unsigned long offset = (unsigned long)c;
1659 offset += offsetof(struct btrfs_chunk, stripe);
1660 offset += nr * sizeof(struct btrfs_stripe);
1661 return (struct btrfs_stripe *)offset;
1664 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
1666 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1669 static inline u64 btrfs_stripe_offset_nr(const struct extent_buffer *eb,
1670 struct btrfs_chunk *c, int nr)
1672 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1675 static inline u64 btrfs_stripe_devid_nr(const struct extent_buffer *eb,
1676 struct btrfs_chunk *c, int nr)
1678 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1681 /* struct btrfs_block_group_item */
1682 BTRFS_SETGET_STACK_FUNCS(stack_block_group_used, struct btrfs_block_group_item,
1684 BTRFS_SETGET_FUNCS(block_group_used, struct btrfs_block_group_item,
1686 BTRFS_SETGET_STACK_FUNCS(stack_block_group_chunk_objectid,
1687 struct btrfs_block_group_item, chunk_objectid, 64);
1689 BTRFS_SETGET_FUNCS(block_group_chunk_objectid,
1690 struct btrfs_block_group_item, chunk_objectid, 64);
1691 BTRFS_SETGET_FUNCS(block_group_flags,
1692 struct btrfs_block_group_item, flags, 64);
1693 BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
1694 struct btrfs_block_group_item, flags, 64);
1696 /* struct btrfs_free_space_info */
1697 BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
1699 BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
1701 /* struct btrfs_inode_ref */
1702 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1703 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
1705 /* struct btrfs_inode_extref */
1706 BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
1707 parent_objectid, 64);
1708 BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
1710 BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
1712 /* struct btrfs_inode_item */
1713 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
1714 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
1715 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
1716 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
1717 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
1718 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1719 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1720 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1721 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1722 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
1723 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
1724 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
1725 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1727 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1729 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1731 BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1732 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1734 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1736 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1737 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1738 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1739 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1740 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1741 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
1742 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1743 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1744 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1745 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
1747 /* struct btrfs_dev_extent */
1748 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1750 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1751 chunk_objectid, 64);
1752 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1754 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1755 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1756 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1758 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
1760 BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1762 static inline void btrfs_tree_block_key(const struct extent_buffer *eb,
1763 struct btrfs_tree_block_info *item,
1764 struct btrfs_disk_key *key)
1766 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1769 static inline void btrfs_set_tree_block_key(const struct extent_buffer *eb,
1770 struct btrfs_tree_block_info *item,
1771 struct btrfs_disk_key *key)
1773 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1776 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
1778 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
1780 BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
1782 BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
1785 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
1788 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
1790 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
1793 static inline u32 btrfs_extent_inline_ref_size(int type)
1795 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1796 type == BTRFS_SHARED_BLOCK_REF_KEY)
1797 return sizeof(struct btrfs_extent_inline_ref);
1798 if (type == BTRFS_SHARED_DATA_REF_KEY)
1799 return sizeof(struct btrfs_shared_data_ref) +
1800 sizeof(struct btrfs_extent_inline_ref);
1801 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1802 return sizeof(struct btrfs_extent_data_ref) +
1803 offsetof(struct btrfs_extent_inline_ref, offset);
1807 /* struct btrfs_node */
1808 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1809 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1810 BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr,
1812 BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
1815 static inline u64 btrfs_node_blockptr(const struct extent_buffer *eb, int nr)
1818 ptr = offsetof(struct btrfs_node, ptrs) +
1819 sizeof(struct btrfs_key_ptr) * nr;
1820 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1823 static inline void btrfs_set_node_blockptr(const struct extent_buffer *eb,
1827 ptr = offsetof(struct btrfs_node, ptrs) +
1828 sizeof(struct btrfs_key_ptr) * nr;
1829 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1832 static inline u64 btrfs_node_ptr_generation(const struct extent_buffer *eb, int nr)
1835 ptr = offsetof(struct btrfs_node, ptrs) +
1836 sizeof(struct btrfs_key_ptr) * nr;
1837 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1840 static inline void btrfs_set_node_ptr_generation(const struct extent_buffer *eb,
1844 ptr = offsetof(struct btrfs_node, ptrs) +
1845 sizeof(struct btrfs_key_ptr) * nr;
1846 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1849 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1851 return offsetof(struct btrfs_node, ptrs) +
1852 sizeof(struct btrfs_key_ptr) * nr;
1855 void btrfs_node_key(const struct extent_buffer *eb,
1856 struct btrfs_disk_key *disk_key, int nr);
1858 static inline void btrfs_set_node_key(const struct extent_buffer *eb,
1859 struct btrfs_disk_key *disk_key, int nr)
1862 ptr = btrfs_node_key_ptr_offset(nr);
1863 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1864 struct btrfs_key_ptr, key, disk_key);
1867 /* struct btrfs_item */
1868 BTRFS_SETGET_FUNCS(raw_item_offset, struct btrfs_item, offset, 32);
1869 BTRFS_SETGET_FUNCS(raw_item_size, struct btrfs_item, size, 32);
1870 BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
1871 BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
1873 static inline unsigned long btrfs_item_nr_offset(int nr)
1875 return offsetof(struct btrfs_leaf, items) +
1876 sizeof(struct btrfs_item) * nr;
1879 static inline struct btrfs_item *btrfs_item_nr(int nr)
1881 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
1884 #define BTRFS_ITEM_SETGET_FUNCS(member) \
1885 static inline u32 btrfs_item_##member(const struct extent_buffer *eb, \
1888 return btrfs_raw_item_##member(eb, btrfs_item_nr(slot)); \
1890 static inline void btrfs_set_item_##member(const struct extent_buffer *eb, \
1891 int slot, u32 val) \
1893 btrfs_set_raw_item_##member(eb, btrfs_item_nr(slot), val); \
1895 static inline u32 btrfs_token_item_##member(struct btrfs_map_token *token, \
1898 struct btrfs_item *item = btrfs_item_nr(slot); \
1899 return btrfs_token_raw_item_##member(token, item); \
1901 static inline void btrfs_set_token_item_##member(struct btrfs_map_token *token, \
1902 int slot, u32 val) \
1904 struct btrfs_item *item = btrfs_item_nr(slot); \
1905 btrfs_set_token_raw_item_##member(token, item, val); \
1908 BTRFS_ITEM_SETGET_FUNCS(offset)
1909 BTRFS_ITEM_SETGET_FUNCS(size);
1911 static inline u32 btrfs_item_data_end(const struct extent_buffer *eb, int nr)
1913 return btrfs_item_offset(eb, nr) + btrfs_item_size(eb, nr);
1916 static inline void btrfs_item_key(const struct extent_buffer *eb,
1917 struct btrfs_disk_key *disk_key, int nr)
1919 struct btrfs_item *item = btrfs_item_nr(nr);
1920 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1923 static inline void btrfs_set_item_key(struct extent_buffer *eb,
1924 struct btrfs_disk_key *disk_key, int nr)
1926 struct btrfs_item *item = btrfs_item_nr(nr);
1927 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1930 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
1933 * struct btrfs_root_ref
1935 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
1936 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
1937 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
1939 /* struct btrfs_dir_item */
1940 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
1941 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
1942 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
1943 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
1944 BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
1945 BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item,
1947 BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item,
1949 BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item,
1952 static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
1953 const struct btrfs_dir_item *item,
1954 struct btrfs_disk_key *key)
1956 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1959 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
1960 struct btrfs_dir_item *item,
1961 const struct btrfs_disk_key *key)
1963 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
1966 BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
1968 BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
1970 BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
1973 static inline void btrfs_free_space_key(const struct extent_buffer *eb,
1974 const struct btrfs_free_space_header *h,
1975 struct btrfs_disk_key *key)
1977 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
1980 static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
1981 struct btrfs_free_space_header *h,
1982 const struct btrfs_disk_key *key)
1984 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
1987 /* struct btrfs_disk_key */
1988 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
1990 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
1991 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1993 #ifdef __LITTLE_ENDIAN
1996 * Optimized helpers for little-endian architectures where CPU and on-disk
1997 * structures have the same endianness and we can skip conversions.
2000 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu_key,
2001 const struct btrfs_disk_key *disk_key)
2003 memcpy(cpu_key, disk_key, sizeof(struct btrfs_key));
2006 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk_key,
2007 const struct btrfs_key *cpu_key)
2009 memcpy(disk_key, cpu_key, sizeof(struct btrfs_key));
2012 static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2013 struct btrfs_key *cpu_key, int nr)
2015 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
2017 btrfs_node_key(eb, disk_key, nr);
2020 static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2021 struct btrfs_key *cpu_key, int nr)
2023 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
2025 btrfs_item_key(eb, disk_key, nr);
2028 static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2029 const struct btrfs_dir_item *item,
2030 struct btrfs_key *cpu_key)
2032 struct btrfs_disk_key *disk_key = (struct btrfs_disk_key *)cpu_key;
2034 btrfs_dir_item_key(eb, item, disk_key);
2039 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
2040 const struct btrfs_disk_key *disk)
2042 cpu->offset = le64_to_cpu(disk->offset);
2043 cpu->type = disk->type;
2044 cpu->objectid = le64_to_cpu(disk->objectid);
2047 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
2048 const struct btrfs_key *cpu)
2050 disk->offset = cpu_to_le64(cpu->offset);
2051 disk->type = cpu->type;
2052 disk->objectid = cpu_to_le64(cpu->objectid);
2055 static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2056 struct btrfs_key *key, int nr)
2058 struct btrfs_disk_key disk_key;
2059 btrfs_node_key(eb, &disk_key, nr);
2060 btrfs_disk_key_to_cpu(key, &disk_key);
2063 static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2064 struct btrfs_key *key, int nr)
2066 struct btrfs_disk_key disk_key;
2067 btrfs_item_key(eb, &disk_key, nr);
2068 btrfs_disk_key_to_cpu(key, &disk_key);
2071 static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2072 const struct btrfs_dir_item *item,
2073 struct btrfs_key *key)
2075 struct btrfs_disk_key disk_key;
2076 btrfs_dir_item_key(eb, item, &disk_key);
2077 btrfs_disk_key_to_cpu(key, &disk_key);
2082 /* struct btrfs_header */
2083 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
2084 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
2086 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
2087 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
2088 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
2089 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
2090 BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
2092 BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
2093 BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header,
2095 BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
2097 static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
2099 return (btrfs_header_flags(eb) & flag) == flag;
2102 static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
2104 u64 flags = btrfs_header_flags(eb);
2105 btrfs_set_header_flags(eb, flags | flag);
2108 static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
2110 u64 flags = btrfs_header_flags(eb);
2111 btrfs_set_header_flags(eb, flags & ~flag);
2114 static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
2116 u64 flags = btrfs_header_flags(eb);
2117 return flags >> BTRFS_BACKREF_REV_SHIFT;
2120 static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
2123 u64 flags = btrfs_header_flags(eb);
2124 flags &= ~BTRFS_BACKREF_REV_MASK;
2125 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
2126 btrfs_set_header_flags(eb, flags);
2129 static inline int btrfs_is_leaf(const struct extent_buffer *eb)
2131 return btrfs_header_level(eb) == 0;
2134 /* struct btrfs_root_item */
2135 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
2137 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
2138 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
2139 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
2141 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
2143 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
2144 BTRFS_SETGET_STACK_FUNCS(root_drop_level, struct btrfs_root_item, drop_level, 8);
2145 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
2146 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
2147 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
2148 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
2149 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
2150 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
2151 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
2153 BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
2155 BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
2157 BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
2159 BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
2161 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
2164 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
2166 /* Byte-swap the constant at compile time, root_item::flags is LE */
2167 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
2170 static inline bool btrfs_root_dead(const struct btrfs_root *root)
2172 /* Byte-swap the constant at compile time, root_item::flags is LE */
2173 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
2176 static inline u64 btrfs_root_id(const struct btrfs_root *root)
2178 return root->root_key.objectid;
2181 /* struct btrfs_root_backup */
2182 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
2184 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
2186 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
2187 tree_root_level, 8);
2189 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
2191 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
2192 chunk_root_gen, 64);
2193 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
2194 chunk_root_level, 8);
2196 BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
2198 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
2199 extent_root_gen, 64);
2200 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
2201 extent_root_level, 8);
2203 BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
2205 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
2207 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
2210 BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
2212 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
2214 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
2217 BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
2219 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
2221 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
2222 csum_root_level, 8);
2223 BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
2225 BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
2227 BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
2230 /* struct btrfs_balance_item */
2231 BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
2233 static inline void btrfs_balance_data(const struct extent_buffer *eb,
2234 const struct btrfs_balance_item *bi,
2235 struct btrfs_disk_balance_args *ba)
2237 read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2240 static inline void btrfs_set_balance_data(struct extent_buffer *eb,
2241 struct btrfs_balance_item *bi,
2242 const struct btrfs_disk_balance_args *ba)
2244 write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2247 static inline void btrfs_balance_meta(const struct extent_buffer *eb,
2248 const struct btrfs_balance_item *bi,
2249 struct btrfs_disk_balance_args *ba)
2251 read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2254 static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
2255 struct btrfs_balance_item *bi,
2256 const struct btrfs_disk_balance_args *ba)
2258 write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2261 static inline void btrfs_balance_sys(const struct extent_buffer *eb,
2262 const struct btrfs_balance_item *bi,
2263 struct btrfs_disk_balance_args *ba)
2265 read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2268 static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
2269 struct btrfs_balance_item *bi,
2270 const struct btrfs_disk_balance_args *ba)
2272 write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2276 btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
2277 const struct btrfs_disk_balance_args *disk)
2279 memset(cpu, 0, sizeof(*cpu));
2281 cpu->profiles = le64_to_cpu(disk->profiles);
2282 cpu->usage = le64_to_cpu(disk->usage);
2283 cpu->devid = le64_to_cpu(disk->devid);
2284 cpu->pstart = le64_to_cpu(disk->pstart);
2285 cpu->pend = le64_to_cpu(disk->pend);
2286 cpu->vstart = le64_to_cpu(disk->vstart);
2287 cpu->vend = le64_to_cpu(disk->vend);
2288 cpu->target = le64_to_cpu(disk->target);
2289 cpu->flags = le64_to_cpu(disk->flags);
2290 cpu->limit = le64_to_cpu(disk->limit);
2291 cpu->stripes_min = le32_to_cpu(disk->stripes_min);
2292 cpu->stripes_max = le32_to_cpu(disk->stripes_max);
2296 btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
2297 const struct btrfs_balance_args *cpu)
2299 memset(disk, 0, sizeof(*disk));
2301 disk->profiles = cpu_to_le64(cpu->profiles);
2302 disk->usage = cpu_to_le64(cpu->usage);
2303 disk->devid = cpu_to_le64(cpu->devid);
2304 disk->pstart = cpu_to_le64(cpu->pstart);
2305 disk->pend = cpu_to_le64(cpu->pend);
2306 disk->vstart = cpu_to_le64(cpu->vstart);
2307 disk->vend = cpu_to_le64(cpu->vend);
2308 disk->target = cpu_to_le64(cpu->target);
2309 disk->flags = cpu_to_le64(cpu->flags);
2310 disk->limit = cpu_to_le64(cpu->limit);
2311 disk->stripes_min = cpu_to_le32(cpu->stripes_min);
2312 disk->stripes_max = cpu_to_le32(cpu->stripes_max);
2315 /* struct btrfs_super_block */
2316 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
2317 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
2318 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
2320 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
2321 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
2322 struct btrfs_super_block, sys_chunk_array_size, 32);
2323 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
2324 struct btrfs_super_block, chunk_root_generation, 64);
2325 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
2327 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
2329 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
2330 chunk_root_level, 8);
2331 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
2333 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
2335 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
2337 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
2339 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
2341 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
2343 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
2345 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
2346 root_dir_objectid, 64);
2347 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
2349 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
2351 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
2352 compat_ro_flags, 64);
2353 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
2354 incompat_flags, 64);
2355 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
2357 BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
2358 cache_generation, 64);
2359 BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
2360 BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
2361 uuid_tree_generation, 64);
2363 int btrfs_super_csum_size(const struct btrfs_super_block *s);
2364 const char *btrfs_super_csum_name(u16 csum_type);
2365 const char *btrfs_super_csum_driver(u16 csum_type);
2366 size_t __attribute_const__ btrfs_get_num_csums(void);
2370 * The leaf data grows from end-to-front in the node.
2371 * this returns the address of the start of the last item,
2372 * which is the stop of the leaf data stack
2374 static inline unsigned int leaf_data_end(const struct extent_buffer *leaf)
2376 u32 nr = btrfs_header_nritems(leaf);
2379 return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
2380 return btrfs_item_offset(leaf, nr - 1);
2383 /* struct btrfs_file_extent_item */
2384 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item,
2386 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
2387 struct btrfs_file_extent_item, disk_bytenr, 64);
2388 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
2389 struct btrfs_file_extent_item, offset, 64);
2390 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
2391 struct btrfs_file_extent_item, generation, 64);
2392 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
2393 struct btrfs_file_extent_item, num_bytes, 64);
2394 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes,
2395 struct btrfs_file_extent_item, ram_bytes, 64);
2396 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
2397 struct btrfs_file_extent_item, disk_num_bytes, 64);
2398 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
2399 struct btrfs_file_extent_item, compression, 8);
2401 static inline unsigned long
2402 btrfs_file_extent_inline_start(const struct btrfs_file_extent_item *e)
2404 return (unsigned long)e + BTRFS_FILE_EXTENT_INLINE_DATA_START;
2407 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2409 return BTRFS_FILE_EXTENT_INLINE_DATA_START + datasize;
2412 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
2413 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
2415 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
2417 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
2418 disk_num_bytes, 64);
2419 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
2421 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
2423 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
2425 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
2427 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
2429 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
2430 other_encoding, 16);
2433 * this returns the number of bytes used by the item on disk, minus the
2434 * size of any extent headers. If a file is compressed on disk, this is
2435 * the compressed size
2437 static inline u32 btrfs_file_extent_inline_item_len(
2438 const struct extent_buffer *eb,
2441 return btrfs_item_size(eb, nr) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
2444 /* btrfs_qgroup_status_item */
2445 BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
2447 BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
2449 BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
2451 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
2454 /* btrfs_qgroup_info_item */
2455 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
2457 BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
2458 BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
2460 BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
2461 BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
2464 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
2465 struct btrfs_qgroup_info_item, generation, 64);
2466 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
2468 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
2469 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
2470 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
2472 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
2473 struct btrfs_qgroup_info_item, excl_cmpr, 64);
2475 /* btrfs_qgroup_limit_item */
2476 BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
2478 BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
2480 BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
2482 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
2484 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
2487 /* btrfs_dev_replace_item */
2488 BTRFS_SETGET_FUNCS(dev_replace_src_devid,
2489 struct btrfs_dev_replace_item, src_devid, 64);
2490 BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
2491 struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
2493 BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
2495 BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
2497 BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
2499 BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
2500 num_write_errors, 64);
2501 BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
2502 struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
2504 BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
2506 BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
2509 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
2510 struct btrfs_dev_replace_item, src_devid, 64);
2511 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
2512 struct btrfs_dev_replace_item,
2513 cont_reading_from_srcdev_mode, 64);
2514 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
2515 struct btrfs_dev_replace_item, replace_state, 64);
2516 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
2517 struct btrfs_dev_replace_item, time_started, 64);
2518 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
2519 struct btrfs_dev_replace_item, time_stopped, 64);
2520 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
2521 struct btrfs_dev_replace_item, num_write_errors, 64);
2522 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
2523 struct btrfs_dev_replace_item,
2524 num_uncorrectable_read_errors, 64);
2525 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
2526 struct btrfs_dev_replace_item, cursor_left, 64);
2527 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
2528 struct btrfs_dev_replace_item, cursor_right, 64);
2530 /* helper function to cast into the data area of the leaf. */
2531 #define btrfs_item_ptr(leaf, slot, type) \
2532 ((type *)(BTRFS_LEAF_DATA_OFFSET + \
2533 btrfs_item_offset(leaf, slot)))
2535 #define btrfs_item_ptr_offset(leaf, slot) \
2536 ((unsigned long)(BTRFS_LEAF_DATA_OFFSET + \
2537 btrfs_item_offset(leaf, slot)))
2539 static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
2541 return crc32c(crc, address, length);
2544 static inline void btrfs_crc32c_final(u32 crc, u8 *result)
2546 put_unaligned_le32(~crc, result);
2549 static inline u64 btrfs_name_hash(const char *name, int len)
2551 return crc32c((u32)~1, name, len);
2555 * Figure the key offset of an extended inode ref
2557 static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
2560 return (u64) crc32c(parent_objectid, name, len);
2563 static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
2565 return mapping_gfp_constraint(mapping, ~__GFP_FS);
2570 enum btrfs_inline_ref_type {
2571 BTRFS_REF_TYPE_INVALID,
2572 BTRFS_REF_TYPE_BLOCK,
2573 BTRFS_REF_TYPE_DATA,
2577 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
2578 struct btrfs_extent_inline_ref *iref,
2579 enum btrfs_inline_ref_type is_data);
2580 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
2583 int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
2584 u64 start, u64 num_bytes);
2585 void btrfs_free_excluded_extents(struct btrfs_block_group *cache);
2586 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2587 unsigned long count);
2588 void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
2589 struct btrfs_delayed_ref_root *delayed_refs,
2590 struct btrfs_delayed_ref_head *head);
2591 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
2592 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2593 struct btrfs_fs_info *fs_info, u64 bytenr,
2594 u64 offset, int metadata, u64 *refs, u64 *flags);
2595 int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num,
2597 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
2598 u64 bytenr, u64 num_bytes);
2599 int btrfs_exclude_logged_extents(struct extent_buffer *eb);
2600 int btrfs_cross_ref_exist(struct btrfs_root *root,
2601 u64 objectid, u64 offset, u64 bytenr, bool strict,
2602 struct btrfs_path *path);
2603 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
2604 struct btrfs_root *root,
2605 u64 parent, u64 root_objectid,
2606 const struct btrfs_disk_key *key,
2607 int level, u64 hint,
2609 enum btrfs_lock_nesting nest);
2610 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2612 struct extent_buffer *buf,
2613 u64 parent, int last_ref);
2614 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
2615 struct btrfs_root *root, u64 owner,
2616 u64 offset, u64 ram_bytes,
2617 struct btrfs_key *ins);
2618 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
2619 u64 root_objectid, u64 owner, u64 offset,
2620 struct btrfs_key *ins);
2621 int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
2622 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
2623 struct btrfs_key *ins, int is_data, int delalloc);
2624 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2625 struct extent_buffer *buf, int full_backref);
2626 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2627 struct extent_buffer *buf, int full_backref);
2628 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2629 struct extent_buffer *eb, u64 flags, int level);
2630 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
2632 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
2633 u64 start, u64 len, int delalloc);
2634 int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
2636 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
2637 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2638 struct btrfs_ref *generic_ref);
2640 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2643 * Different levels for to flush space when doing space reservations.
2645 * The higher the level, the more methods we try to reclaim space.
2647 enum btrfs_reserve_flush_enum {
2648 /* If we are in the transaction, we can't flush anything.*/
2649 BTRFS_RESERVE_NO_FLUSH,
2653 * - Running delayed inode items
2654 * - Allocating a new chunk
2656 BTRFS_RESERVE_FLUSH_LIMIT,
2660 * - Running delayed inode items
2661 * - Running delayed refs
2662 * - Running delalloc and waiting for ordered extents
2663 * - Allocating a new chunk
2665 BTRFS_RESERVE_FLUSH_EVICT,
2668 * Flush space by above mentioned methods and by:
2669 * - Running delayed iputs
2670 * - Committing transaction
2672 * Can be interrupted by a fatal signal.
2674 BTRFS_RESERVE_FLUSH_DATA,
2675 BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
2676 BTRFS_RESERVE_FLUSH_ALL,
2679 * Pretty much the same as FLUSH_ALL, but can also steal space from
2682 * Can be interrupted by a fatal signal.
2684 BTRFS_RESERVE_FLUSH_ALL_STEAL,
2687 enum btrfs_flush_state {
2688 FLUSH_DELAYED_ITEMS_NR = 1,
2689 FLUSH_DELAYED_ITEMS = 2,
2690 FLUSH_DELAYED_REFS_NR = 3,
2691 FLUSH_DELAYED_REFS = 4,
2693 FLUSH_DELALLOC_WAIT = 6,
2694 FLUSH_DELALLOC_FULL = 7,
2696 ALLOC_CHUNK_FORCE = 9,
2697 RUN_DELAYED_IPUTS = 10,
2701 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
2702 struct btrfs_block_rsv *rsv,
2703 int nitems, bool use_global_rsv);
2704 void btrfs_subvolume_release_metadata(struct btrfs_root *root,
2705 struct btrfs_block_rsv *rsv);
2706 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes);
2708 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
2709 u64 disk_num_bytes, bool noflush);
2710 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2711 int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
2712 u64 start, u64 end);
2713 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2714 u64 num_bytes, u64 *actual_bytes);
2715 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
2717 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
2718 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2719 struct btrfs_fs_info *fs_info);
2720 int btrfs_start_write_no_snapshotting(struct btrfs_root *root);
2721 void btrfs_end_write_no_snapshotting(struct btrfs_root *root);
2722 void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
2725 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
2727 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
2728 int btrfs_previous_item(struct btrfs_root *root,
2729 struct btrfs_path *path, u64 min_objectid,
2731 int btrfs_previous_extent_item(struct btrfs_root *root,
2732 struct btrfs_path *path, u64 min_objectid);
2733 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2734 struct btrfs_path *path,
2735 const struct btrfs_key *new_key);
2736 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
2737 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
2738 struct btrfs_key *key, int lowest_level,
2740 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
2741 struct btrfs_path *path,
2743 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
2746 int btrfs_cow_block(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *root, struct extent_buffer *buf,
2748 struct extent_buffer *parent, int parent_slot,
2749 struct extent_buffer **cow_ret,
2750 enum btrfs_lock_nesting nest);
2751 int btrfs_copy_root(struct btrfs_trans_handle *trans,
2752 struct btrfs_root *root,
2753 struct extent_buffer *buf,
2754 struct extent_buffer **cow_ret, u64 new_root_objectid);
2755 int btrfs_block_can_be_shared(struct btrfs_root *root,
2756 struct extent_buffer *buf);
2757 void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
2758 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
2759 int btrfs_split_item(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root,
2761 struct btrfs_path *path,
2762 const struct btrfs_key *new_key,
2763 unsigned long split_offset);
2764 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
2765 struct btrfs_root *root,
2766 struct btrfs_path *path,
2767 const struct btrfs_key *new_key);
2768 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2769 u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
2770 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2771 const struct btrfs_key *key, struct btrfs_path *p,
2772 int ins_len, int cow);
2773 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2774 struct btrfs_path *p, u64 time_seq);
2775 int btrfs_search_slot_for_read(struct btrfs_root *root,
2776 const struct btrfs_key *key,
2777 struct btrfs_path *p, int find_higher,
2779 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
2780 struct btrfs_root *root, struct extent_buffer *parent,
2781 int start_slot, u64 *last_ret,
2782 struct btrfs_key *progress);
2783 void btrfs_release_path(struct btrfs_path *p);
2784 struct btrfs_path *btrfs_alloc_path(void);
2785 void btrfs_free_path(struct btrfs_path *p);
2787 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2788 struct btrfs_path *path, int slot, int nr);
2789 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
2790 struct btrfs_root *root,
2791 struct btrfs_path *path)
2793 return btrfs_del_items(trans, root, path, path->slots[0], 1);
2797 * Describes a batch of items to insert in a btree. This is used by
2798 * btrfs_insert_empty_items().
2800 struct btrfs_item_batch {
2802 * Pointer to an array containing the keys of the items to insert (in
2805 const struct btrfs_key *keys;
2806 /* Pointer to an array containing the data size for each item to insert. */
2807 const u32 *data_sizes;
2809 * The sum of data sizes for all items. The caller can compute this while
2810 * setting up the data_sizes array, so it ends up being more efficient
2811 * than having btrfs_insert_empty_items() or setup_item_for_insert()
2812 * doing it, as it would avoid an extra loop over a potentially large
2813 * array, and in the case of setup_item_for_insert(), we would be doing
2814 * it while holding a write lock on a leaf and often on upper level nodes
2815 * too, unnecessarily increasing the size of a critical section.
2817 u32 total_data_size;
2818 /* Size of the keys and data_sizes arrays (number of items in the batch). */
2822 void btrfs_setup_item_for_insert(struct btrfs_root *root,
2823 struct btrfs_path *path,
2824 const struct btrfs_key *key,
2826 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2827 const struct btrfs_key *key, void *data, u32 data_size);
2828 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2829 struct btrfs_root *root,
2830 struct btrfs_path *path,
2831 const struct btrfs_item_batch *batch);
2833 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2834 struct btrfs_root *root,
2835 struct btrfs_path *path,
2836 const struct btrfs_key *key,
2839 struct btrfs_item_batch batch;
2842 batch.data_sizes = &data_size;
2843 batch.total_data_size = data_size;
2846 return btrfs_insert_empty_items(trans, root, path, &batch);
2849 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
2850 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
2853 int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
2854 struct btrfs_path *path);
2856 int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
2857 struct btrfs_path *path);
2860 * Search in @root for a given @key, and store the slot found in @found_key.
2862 * @root: The root node of the tree.
2863 * @key: The key we are looking for.
2864 * @found_key: Will hold the found item.
2865 * @path: Holds the current slot/leaf.
2866 * @iter_ret: Contains the value returned from btrfs_search_slot or
2867 * btrfs_get_next_valid_item, whichever was executed last.
2869 * The @iter_ret is an output variable that will contain the return value of
2870 * btrfs_search_slot, if it encountered an error, or the value returned from
2871 * btrfs_get_next_valid_item otherwise. That return value can be 0, if a valid
2872 * slot was found, 1 if there were no more leaves, and <0 if there was an error.
2874 * It's recommended to use a separate variable for iter_ret and then use it to
2875 * set the function return value so there's no confusion of the 0/1/errno
2876 * values stemming from btrfs_search_slot.
2878 #define btrfs_for_each_slot(root, key, found_key, path, iter_ret) \
2879 for (iter_ret = btrfs_search_slot(NULL, (root), (key), (path), 0, 0); \
2880 (iter_ret) >= 0 && \
2881 (iter_ret = btrfs_get_next_valid_item((root), (found_key), (path))) == 0; \
2882 (path)->slots[0]++ \
2885 static inline int btrfs_next_old_item(struct btrfs_root *root,
2886 struct btrfs_path *p, u64 time_seq)
2889 if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
2890 return btrfs_next_old_leaf(root, p, time_seq);
2895 * Search the tree again to find a leaf with greater keys.
2897 * Returns 0 if it found something or 1 if there are no greater leaves.
2898 * Returns < 0 on error.
2900 static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2902 return btrfs_next_old_leaf(root, path, 0);
2905 static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
2907 return btrfs_next_old_item(root, p, 0);
2909 int btrfs_leaf_free_space(struct extent_buffer *leaf);
2910 int __must_check btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,
2912 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
2913 struct btrfs_root *root,
2914 struct extent_buffer *node,
2915 struct extent_buffer *parent);
2916 static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
2919 * Do it this way so we only ever do one test_bit in the normal case.
2921 if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
2922 if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
2930 * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
2931 * anything except sleeping. This function is used to check the status of
2933 * We check for BTRFS_FS_STATE_RO to avoid races with a concurrent remount,
2934 * since setting and checking for SB_RDONLY in the superblock's flags is not
2937 static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
2939 return test_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state) ||
2940 btrfs_fs_closing(fs_info);
2943 static inline void btrfs_set_sb_rdonly(struct super_block *sb)
2945 sb->s_flags |= SB_RDONLY;
2946 set_bit(BTRFS_FS_STATE_RO, &btrfs_sb(sb)->fs_state);
2949 static inline void btrfs_clear_sb_rdonly(struct super_block *sb)
2951 sb->s_flags &= ~SB_RDONLY;
2952 clear_bit(BTRFS_FS_STATE_RO, &btrfs_sb(sb)->fs_state);
2956 int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
2957 u64 ref_id, u64 dirid, u64 sequence, const char *name,
2959 int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
2960 u64 ref_id, u64 dirid, u64 *sequence, const char *name,
2962 int btrfs_del_root(struct btrfs_trans_handle *trans,
2963 const struct btrfs_key *key);
2964 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2965 const struct btrfs_key *key,
2966 struct btrfs_root_item *item);
2967 int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
2968 struct btrfs_root *root,
2969 struct btrfs_key *key,
2970 struct btrfs_root_item *item);
2971 int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
2972 struct btrfs_path *path, struct btrfs_root_item *root_item,
2973 struct btrfs_key *root_key);
2974 int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info);
2975 void btrfs_set_root_node(struct btrfs_root_item *item,
2976 struct extent_buffer *node);
2977 void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
2978 void btrfs_update_root_times(struct btrfs_trans_handle *trans,
2979 struct btrfs_root *root);
2982 int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
2984 int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
2986 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info);
2989 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
2990 const char *name, int name_len);
2991 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
2992 int name_len, struct btrfs_inode *dir,
2993 struct btrfs_key *location, u8 type, u64 index);
2994 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
2995 struct btrfs_root *root,
2996 struct btrfs_path *path, u64 dir,
2997 const char *name, int name_len,
2999 struct btrfs_dir_item *
3000 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
3001 struct btrfs_root *root,
3002 struct btrfs_path *path, u64 dir,
3003 u64 index, const char *name, int name_len,
3005 struct btrfs_dir_item *
3006 btrfs_search_dir_index_item(struct btrfs_root *root,
3007 struct btrfs_path *path, u64 dirid,
3008 const char *name, int name_len);
3009 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
3010 struct btrfs_root *root,
3011 struct btrfs_path *path,
3012 struct btrfs_dir_item *di);
3013 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
3014 struct btrfs_root *root,
3015 struct btrfs_path *path, u64 objectid,
3016 const char *name, u16 name_len,
3017 const void *data, u16 data_len);
3018 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
3019 struct btrfs_root *root,
3020 struct btrfs_path *path, u64 dir,
3021 const char *name, u16 name_len,
3023 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
3024 struct btrfs_path *path,
3029 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
3030 struct btrfs_root *root, u64 offset);
3031 int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
3032 struct btrfs_root *root, u64 offset);
3033 int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
3036 int btrfs_del_csums(struct btrfs_trans_handle *trans,
3037 struct btrfs_root *root, u64 bytenr, u64 len);
3038 blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst);
3039 int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
3040 struct btrfs_root *root, u64 objectid, u64 pos,
3042 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
3043 struct btrfs_root *root,
3044 struct btrfs_path *path, u64 objectid,
3045 u64 bytenr, int mod);
3046 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
3047 struct btrfs_root *root,
3048 struct btrfs_ordered_sum *sums);
3049 blk_status_t btrfs_csum_one_bio(struct btrfs_inode *inode, struct bio *bio,
3050 u64 offset, bool one_ordered);
3051 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
3052 struct list_head *list, int search_commit,
3054 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
3055 const struct btrfs_path *path,
3056 struct btrfs_file_extent_item *fi,
3057 const bool new_inline,
3058 struct extent_map *em);
3059 int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
3061 int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
3063 void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size);
3064 u64 btrfs_file_extent_end(const struct btrfs_path *path);
3067 void btrfs_submit_data_write_bio(struct inode *inode, struct bio *bio, int mirror_num);
3068 void btrfs_submit_data_read_bio(struct inode *inode, struct bio *bio,
3069 int mirror_num, enum btrfs_compression_type compress_type);
3070 int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
3071 u32 pgoff, u8 *csum, const u8 * const csum_expected);
3072 int btrfs_check_data_csum(struct inode *inode, struct btrfs_bio *bbio,
3073 u32 bio_offset, struct page *page, u32 pgoff);
3074 unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio,
3075 u32 bio_offset, struct page *page,
3076 u64 start, u64 end);
3077 int btrfs_check_data_csum(struct inode *inode, struct btrfs_bio *bbio,
3078 u32 bio_offset, struct page *page, u32 pgoff);
3079 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
3080 u64 *orig_start, u64 *orig_block_len,
3081 u64 *ram_bytes, bool nowait, bool strict);
3083 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
3084 struct btrfs_inode *inode);
3085 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
3086 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
3087 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3088 struct btrfs_inode *dir, struct btrfs_inode *inode,
3089 const char *name, int name_len);
3090 int btrfs_add_link(struct btrfs_trans_handle *trans,
3091 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
3092 const char *name, int name_len, int add_backref, u64 index);
3093 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
3094 int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
3097 int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context);
3098 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
3099 bool in_reclaim_context);
3100 int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
3101 unsigned int extra_bits,
3102 struct extent_state **cached_state);
3103 struct btrfs_new_inode_args {
3106 struct dentry *dentry;
3107 struct inode *inode;
3112 * Output from btrfs_new_inode_prepare(), input to
3113 * btrfs_create_new_inode().
3115 struct posix_acl *default_acl;
3116 struct posix_acl *acl;
3118 int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
3119 unsigned int *trans_num_items);
3120 int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
3121 struct btrfs_new_inode_args *args);
3122 void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args);
3123 struct inode *btrfs_new_subvol_inode(struct user_namespace *mnt_userns,
3125 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
3127 void btrfs_clear_delalloc_extent(struct inode *inode,
3128 struct extent_state *state, u32 bits);
3129 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
3130 struct extent_state *other);
3131 void btrfs_split_delalloc_extent(struct inode *inode,
3132 struct extent_state *orig, u64 split);
3133 void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end);
3134 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
3135 void btrfs_evict_inode(struct inode *inode);
3136 struct inode *btrfs_alloc_inode(struct super_block *sb);
3137 void btrfs_destroy_inode(struct inode *inode);
3138 void btrfs_free_inode(struct inode *inode);
3139 int btrfs_drop_inode(struct inode *inode);
3140 int __init btrfs_init_cachep(void);
3141 void __cold btrfs_destroy_cachep(void);
3142 struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
3143 struct btrfs_root *root, struct btrfs_path *path);
3144 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
3145 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
3146 struct page *page, size_t pg_offset,
3147 u64 start, u64 end);
3148 int btrfs_update_inode(struct btrfs_trans_handle *trans,
3149 struct btrfs_root *root, struct btrfs_inode *inode);
3150 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3151 struct btrfs_root *root, struct btrfs_inode *inode);
3152 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3153 struct btrfs_inode *inode);
3154 int btrfs_orphan_cleanup(struct btrfs_root *root);
3155 int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
3156 void btrfs_add_delayed_iput(struct inode *inode);
3157 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
3158 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
3159 int btrfs_prealloc_file_range(struct inode *inode, int mode,
3160 u64 start, u64 num_bytes, u64 min_size,
3161 loff_t actual_len, u64 *alloc_hint);
3162 int btrfs_prealloc_file_range_trans(struct inode *inode,
3163 struct btrfs_trans_handle *trans, int mode,
3164 u64 start, u64 num_bytes, u64 min_size,
3165 loff_t actual_len, u64 *alloc_hint);
3166 int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
3167 u64 start, u64 end, int *page_started, unsigned long *nr_written,
3168 struct writeback_control *wbc);
3169 int btrfs_writepage_cow_fixup(struct page *page);
3170 void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3171 struct page *page, u64 start,
3172 u64 end, bool uptodate);
3173 int btrfs_encoded_io_compression_from_extent(struct btrfs_fs_info *fs_info,
3175 int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
3176 u64 file_offset, u64 disk_bytenr,
3178 struct page **pages);
3179 ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
3180 struct btrfs_ioctl_encoded_io_args *encoded);
3181 ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
3182 const struct btrfs_ioctl_encoded_io_args *encoded);
3184 ssize_t btrfs_dio_read(struct kiocb *iocb, struct iov_iter *iter,
3185 size_t done_before);
3186 struct iomap_dio *btrfs_dio_write(struct kiocb *iocb, struct iov_iter *iter,
3187 size_t done_before);
3189 extern const struct dentry_operations btrfs_dentry_operations;
3191 /* Inode locking type flags, by default the exclusive lock is taken */
3192 #define BTRFS_ILOCK_SHARED (1U << 0)
3193 #define BTRFS_ILOCK_TRY (1U << 1)
3194 #define BTRFS_ILOCK_MMAP (1U << 2)
3196 int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags);
3197 void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags);
3198 void btrfs_update_inode_bytes(struct btrfs_inode *inode,
3199 const u64 add_bytes,
3200 const u64 del_bytes);
3201 void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end);
3204 long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3205 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3206 int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
3207 int btrfs_fileattr_set(struct user_namespace *mnt_userns,
3208 struct dentry *dentry, struct fileattr *fa);
3209 int btrfs_ioctl_get_supported_features(void __user *arg);
3210 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
3211 int __pure btrfs_is_empty_uuid(u8 *uuid);
3212 int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
3213 struct btrfs_ioctl_defrag_range_args *range,
3214 u64 newer_than, unsigned long max_to_defrag);
3215 void btrfs_get_block_group_info(struct list_head *groups_list,
3216 struct btrfs_ioctl_space_info *space);
3217 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3218 struct btrfs_ioctl_balance_args *bargs);
3221 int __init btrfs_auto_defrag_init(void);
3222 void __cold btrfs_auto_defrag_exit(void);
3223 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
3224 struct btrfs_inode *inode, u32 extent_thresh);
3225 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
3226 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
3227 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
3228 extern const struct file_operations btrfs_file_operations;
3229 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
3230 struct btrfs_root *root, struct btrfs_inode *inode,
3231 struct btrfs_drop_extents_args *args);
3232 int btrfs_replace_file_extents(struct btrfs_inode *inode,
3233 struct btrfs_path *path, const u64 start,
3235 struct btrfs_replace_extent_info *extent_info,
3236 struct btrfs_trans_handle **trans_out);
3237 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
3238 struct btrfs_inode *inode, u64 start, u64 end);
3239 ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from,
3240 const struct btrfs_ioctl_encoded_io_args *encoded);
3241 int btrfs_release_file(struct inode *inode, struct file *file);
3242 int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
3243 size_t num_pages, loff_t pos, size_t write_bytes,
3244 struct extent_state **cached, bool noreserve);
3245 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
3246 int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
3247 size_t *write_bytes, bool nowait);
3248 void btrfs_check_nocow_unlock(struct btrfs_inode *inode);
3249 bool btrfs_find_delalloc_in_range(struct btrfs_inode *inode, u64 start, u64 end,
3250 u64 *delalloc_start_ret, u64 *delalloc_end_ret);
3253 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
3254 struct btrfs_root *root);
3257 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
3258 unsigned long new_flags);
3259 int btrfs_sync_fs(struct super_block *sb, int wait);
3260 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
3261 u64 subvol_objectid);
3263 static inline __printf(2, 3) __cold
3264 void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
3268 #ifdef CONFIG_PRINTK_INDEX
3270 #define btrfs_printk(fs_info, fmt, args...) \
3272 printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt); \
3273 _btrfs_printk(fs_info, fmt, ##args); \
3278 void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
3280 #elif defined(CONFIG_PRINTK)
3282 #define btrfs_printk(fs_info, fmt, args...) \
3283 _btrfs_printk(fs_info, fmt, ##args)
3287 void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
3291 #define btrfs_printk(fs_info, fmt, args...) \
3292 btrfs_no_printk(fs_info, fmt, ##args)
3295 #define btrfs_emerg(fs_info, fmt, args...) \
3296 btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
3297 #define btrfs_alert(fs_info, fmt, args...) \
3298 btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
3299 #define btrfs_crit(fs_info, fmt, args...) \
3300 btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
3301 #define btrfs_err(fs_info, fmt, args...) \
3302 btrfs_printk(fs_info, KERN_ERR fmt, ##args)
3303 #define btrfs_warn(fs_info, fmt, args...) \
3304 btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
3305 #define btrfs_notice(fs_info, fmt, args...) \
3306 btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
3307 #define btrfs_info(fs_info, fmt, args...) \
3308 btrfs_printk(fs_info, KERN_INFO fmt, ##args)
3311 * Wrappers that use printk_in_rcu
3313 #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
3314 btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3315 #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
3316 btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3317 #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
3318 btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3319 #define btrfs_err_in_rcu(fs_info, fmt, args...) \
3320 btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
3321 #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
3322 btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3323 #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
3324 btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3325 #define btrfs_info_in_rcu(fs_info, fmt, args...) \
3326 btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
3329 * Wrappers that use a ratelimited printk_in_rcu
3331 #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
3332 btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3333 #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
3334 btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3335 #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
3336 btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3337 #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
3338 btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
3339 #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
3340 btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3341 #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
3342 btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3343 #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
3344 btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
3347 * Wrappers that use a ratelimited printk
3349 #define btrfs_emerg_rl(fs_info, fmt, args...) \
3350 btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
3351 #define btrfs_alert_rl(fs_info, fmt, args...) \
3352 btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
3353 #define btrfs_crit_rl(fs_info, fmt, args...) \
3354 btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
3355 #define btrfs_err_rl(fs_info, fmt, args...) \
3356 btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
3357 #define btrfs_warn_rl(fs_info, fmt, args...) \
3358 btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
3359 #define btrfs_notice_rl(fs_info, fmt, args...) \
3360 btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
3361 #define btrfs_info_rl(fs_info, fmt, args...) \
3362 btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
3364 #if defined(CONFIG_DYNAMIC_DEBUG)
3365 #define btrfs_debug(fs_info, fmt, args...) \
3366 _dynamic_func_call_no_desc(fmt, btrfs_printk, \
3367 fs_info, KERN_DEBUG fmt, ##args)
3368 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3369 _dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu, \
3370 fs_info, KERN_DEBUG fmt, ##args)
3371 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3372 _dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu, \
3373 fs_info, KERN_DEBUG fmt, ##args)
3374 #define btrfs_debug_rl(fs_info, fmt, args...) \
3375 _dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited, \
3376 fs_info, KERN_DEBUG fmt, ##args)
3377 #elif defined(DEBUG)
3378 #define btrfs_debug(fs_info, fmt, args...) \
3379 btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
3380 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3381 btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3382 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3383 btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3384 #define btrfs_debug_rl(fs_info, fmt, args...) \
3385 btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
3387 #define btrfs_debug(fs_info, fmt, args...) \
3388 btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
3389 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3390 btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3391 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3392 btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3393 #define btrfs_debug_rl(fs_info, fmt, args...) \
3394 btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
3397 #define btrfs_printk_in_rcu(fs_info, fmt, args...) \
3400 btrfs_printk(fs_info, fmt, ##args); \
3401 rcu_read_unlock(); \
3404 #define btrfs_no_printk_in_rcu(fs_info, fmt, args...) \
3407 btrfs_no_printk(fs_info, fmt, ##args); \
3408 rcu_read_unlock(); \
3411 #define btrfs_printk_ratelimited(fs_info, fmt, args...) \
3413 static DEFINE_RATELIMIT_STATE(_rs, \
3414 DEFAULT_RATELIMIT_INTERVAL, \
3415 DEFAULT_RATELIMIT_BURST); \
3416 if (__ratelimit(&_rs)) \
3417 btrfs_printk(fs_info, fmt, ##args); \
3420 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
3423 btrfs_printk_ratelimited(fs_info, fmt, ##args); \
3424 rcu_read_unlock(); \
3427 #ifdef CONFIG_BTRFS_ASSERT
3429 static inline void assertfail(const char *expr, const char *file, int line)
3431 pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
3435 #define ASSERT(expr) \
3436 (likely(expr) ? (void)0 : assertfail(#expr, __FILE__, __LINE__))
3439 static inline void assertfail(const char *expr, const char* file, int line) { }
3440 #define ASSERT(expr) (void)(expr)
3443 #if BITS_PER_LONG == 32
3444 #define BTRFS_32BIT_MAX_FILE_SIZE (((u64)ULONG_MAX + 1) << PAGE_SHIFT)
3446 * The warning threshold is 5/8th of the MAX_LFS_FILESIZE that limits the logical
3447 * addresses of extents.
3449 * For 4K page size it's about 10T, for 64K it's 160T.
3451 #define BTRFS_32BIT_EARLY_WARN_THRESHOLD (BTRFS_32BIT_MAX_FILE_SIZE * 5 / 8)
3452 void btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info);
3453 void btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info);
3457 * Get the correct offset inside the page of extent buffer.
3459 * @eb: target extent buffer
3460 * @start: offset inside the extent buffer
3462 * Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.
3464 static inline size_t get_eb_offset_in_page(const struct extent_buffer *eb,
3465 unsigned long offset)
3468 * For sectorsize == PAGE_SIZE case, eb->start will always be aligned
3469 * to PAGE_SIZE, thus adding it won't cause any difference.
3471 * For sectorsize < PAGE_SIZE, we must only read the data that belongs
3472 * to the eb, thus we have to take the eb->start into consideration.
3474 return offset_in_page(offset + eb->start);
3477 static inline unsigned long get_eb_page_index(unsigned long offset)
3480 * For sectorsize == PAGE_SIZE case, plain >> PAGE_SHIFT is enough.
3482 * For sectorsize < PAGE_SIZE case, we only support 64K PAGE_SIZE,
3483 * and have ensured that all tree blocks are contained in one page,
3484 * thus we always get index == 0.
3486 return offset >> PAGE_SHIFT;
3490 * Use that for functions that are conditionally exported for sanity tests but
3493 #ifndef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3494 #define EXPORT_FOR_TESTS static
3496 #define EXPORT_FOR_TESTS
3500 static inline void btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
3503 "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
3508 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
3509 unsigned int line, int errno, const char *fmt, ...);
3511 const char * __attribute_const__ btrfs_decode_error(int errno);
3514 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
3515 const char *function,
3516 unsigned int line, int errno, bool first_hit);
3518 bool __cold abort_should_print_stack(int errno);
3521 * Call btrfs_abort_transaction as early as possible when an error condition is
3522 * detected, that way the exact stack trace is reported for some errors.
3524 #define btrfs_abort_transaction(trans, errno) \
3526 bool first = false; \
3527 /* Report first abort since mount */ \
3528 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
3529 &((trans)->fs_info->fs_state))) { \
3531 if (WARN(abort_should_print_stack(errno), \
3533 "BTRFS: Transaction aborted (error %d)\n", \
3535 /* Stack trace printed. */ \
3537 btrfs_debug((trans)->fs_info, \
3538 "Transaction aborted (error %d)", \
3542 __btrfs_abort_transaction((trans), __func__, \
3543 __LINE__, (errno), first); \
3546 #ifdef CONFIG_PRINTK_INDEX
3548 #define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \
3550 printk_index_subsys_emit( \
3551 "BTRFS: error (device %s%s) in %s:%d: errno=%d %s", \
3553 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
3554 (errno), fmt, ##args); \
3559 #define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \
3560 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
3561 (errno), fmt, ##args)
3565 #define BTRFS_FS_ERROR(fs_info) (unlikely(test_bit(BTRFS_FS_STATE_ERROR, \
3566 &(fs_info)->fs_state)))
3567 #define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info) \
3568 (unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, \
3569 &(fs_info)->fs_state)))
3573 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3574 unsigned int line, int errno, const char *fmt, ...);
3576 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
3577 * will panic(). Otherwise we BUG() here.
3579 #define btrfs_panic(fs_info, errno, fmt, args...) \
3581 __btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \
3586 /* compatibility and incompatibility defines */
3588 #define btrfs_set_fs_incompat(__fs_info, opt) \
3589 __btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
3592 static inline void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info,
3593 u64 flag, const char* name)
3595 struct btrfs_super_block *disk_super;
3598 disk_super = fs_info->super_copy;
3599 features = btrfs_super_incompat_flags(disk_super);
3600 if (!(features & flag)) {
3601 spin_lock(&fs_info->super_lock);
3602 features = btrfs_super_incompat_flags(disk_super);
3603 if (!(features & flag)) {
3605 btrfs_set_super_incompat_flags(disk_super, features);
3607 "setting incompat feature flag for %s (0x%llx)",
3610 spin_unlock(&fs_info->super_lock);
3614 #define btrfs_clear_fs_incompat(__fs_info, opt) \
3615 __btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
3618 static inline void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info,
3619 u64 flag, const char* name)
3621 struct btrfs_super_block *disk_super;
3624 disk_super = fs_info->super_copy;
3625 features = btrfs_super_incompat_flags(disk_super);
3626 if (features & flag) {
3627 spin_lock(&fs_info->super_lock);
3628 features = btrfs_super_incompat_flags(disk_super);
3629 if (features & flag) {
3631 btrfs_set_super_incompat_flags(disk_super, features);
3633 "clearing incompat feature flag for %s (0x%llx)",
3636 spin_unlock(&fs_info->super_lock);
3640 #define btrfs_fs_incompat(fs_info, opt) \
3641 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
3643 static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
3645 struct btrfs_super_block *disk_super;
3646 disk_super = fs_info->super_copy;
3647 return !!(btrfs_super_incompat_flags(disk_super) & flag);
3650 #define btrfs_set_fs_compat_ro(__fs_info, opt) \
3651 __btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
3654 static inline void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info,
3655 u64 flag, const char *name)
3657 struct btrfs_super_block *disk_super;
3660 disk_super = fs_info->super_copy;
3661 features = btrfs_super_compat_ro_flags(disk_super);
3662 if (!(features & flag)) {
3663 spin_lock(&fs_info->super_lock);
3664 features = btrfs_super_compat_ro_flags(disk_super);
3665 if (!(features & flag)) {
3667 btrfs_set_super_compat_ro_flags(disk_super, features);
3669 "setting compat-ro feature flag for %s (0x%llx)",
3672 spin_unlock(&fs_info->super_lock);
3676 #define btrfs_clear_fs_compat_ro(__fs_info, opt) \
3677 __btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
3680 static inline void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info,
3681 u64 flag, const char *name)
3683 struct btrfs_super_block *disk_super;
3686 disk_super = fs_info->super_copy;
3687 features = btrfs_super_compat_ro_flags(disk_super);
3688 if (features & flag) {
3689 spin_lock(&fs_info->super_lock);
3690 features = btrfs_super_compat_ro_flags(disk_super);
3691 if (features & flag) {
3693 btrfs_set_super_compat_ro_flags(disk_super, features);
3695 "clearing compat-ro feature flag for %s (0x%llx)",
3698 spin_unlock(&fs_info->super_lock);
3702 #define btrfs_fs_compat_ro(fs_info, opt) \
3703 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
3705 static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
3707 struct btrfs_super_block *disk_super;
3708 disk_super = fs_info->super_copy;
3709 return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
3713 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
3714 struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu);
3715 int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
3716 struct posix_acl *acl, int type);
3717 int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
3718 struct posix_acl *acl, int type);
3720 #define btrfs_get_acl NULL
3721 #define btrfs_set_acl NULL
3722 static inline int __btrfs_set_acl(struct btrfs_trans_handle *trans,
3723 struct inode *inode, struct posix_acl *acl,
3731 int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start);
3732 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
3733 struct btrfs_root *root);
3734 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
3735 struct btrfs_root *root);
3736 int btrfs_recover_relocation(struct btrfs_fs_info *fs_info);
3737 int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len);
3738 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
3739 struct btrfs_root *root, struct extent_buffer *buf,
3740 struct extent_buffer *cow);
3741 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
3742 u64 *bytes_to_reserve);
3743 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
3744 struct btrfs_pending_snapshot *pending);
3745 int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info);
3746 struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info,
3748 int btrfs_should_ignore_reloc_root(struct btrfs_root *root);
3751 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3752 u64 end, struct btrfs_scrub_progress *progress,
3753 int readonly, int is_dev_replace);
3754 void btrfs_scrub_pause(struct btrfs_fs_info *fs_info);
3755 void btrfs_scrub_continue(struct btrfs_fs_info *fs_info);
3756 int btrfs_scrub_cancel(struct btrfs_fs_info *info);
3757 int btrfs_scrub_cancel_dev(struct btrfs_device *dev);
3758 int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
3759 struct btrfs_scrub_progress *progress);
3762 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info);
3763 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount);
3765 static inline void btrfs_bio_counter_dec(struct btrfs_fs_info *fs_info)
3767 btrfs_bio_counter_sub(fs_info, 1);
3770 static inline int is_fstree(u64 rootid)
3772 if (rootid == BTRFS_FS_TREE_OBJECTID ||
3773 ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
3774 !btrfs_qgroup_level(rootid)))
3779 static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
3781 return signal_pending(current);
3785 #ifdef CONFIG_FS_VERITY
3787 extern const struct fsverity_operations btrfs_verityops;
3788 int btrfs_drop_verity_items(struct btrfs_inode *inode);
3789 int btrfs_get_verity_descriptor(struct inode *inode, void *buf, size_t buf_size);
3791 BTRFS_SETGET_FUNCS(verity_descriptor_encryption, struct btrfs_verity_descriptor_item,
3793 BTRFS_SETGET_FUNCS(verity_descriptor_size, struct btrfs_verity_descriptor_item,
3795 BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_encryption,
3796 struct btrfs_verity_descriptor_item, encryption, 8);
3797 BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_size,
3798 struct btrfs_verity_descriptor_item, size, 64);
3802 static inline int btrfs_drop_verity_items(struct btrfs_inode *inode)
3807 static inline int btrfs_get_verity_descriptor(struct inode *inode, void *buf,
3815 /* Sanity test specific functions */
3816 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3817 void btrfs_test_destroy_inode(struct inode *inode);
3818 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3820 return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
3823 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3829 static inline bool btrfs_is_data_reloc_root(const struct btrfs_root *root)
3831 return root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID;
3835 * We use page status Private2 to indicate there is an ordered extent with
3838 * Rename the Private2 accessors to Ordered, to improve readability.
3840 #define PageOrdered(page) PagePrivate2(page)
3841 #define SetPageOrdered(page) SetPagePrivate2(page)
3842 #define ClearPageOrdered(page) ClearPagePrivate2(page)
3843 #define folio_test_ordered(folio) folio_test_private_2(folio)
3844 #define folio_set_ordered(folio) folio_set_private_2(folio)
3845 #define folio_clear_ordered(folio) folio_clear_private_2(folio)