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 <linux/kobject.h>
20 #include <trace/events/btrfs.h>
21 #include <asm/kmap_types.h>
22 #include <asm/unaligned.h>
23 #include <linux/pagemap.h>
24 #include <linux/btrfs.h>
25 #include <linux/btrfs_tree.h>
26 #include <linux/workqueue.h>
27 #include <linux/security.h>
28 #include <linux/sizes.h>
29 #include <linux/dynamic_debug.h>
30 #include <linux/refcount.h>
31 #include <linux/crc32c.h>
32 #include "extent_io.h"
33 #include "extent_map.h"
34 #include "async-thread.h"
36 struct btrfs_trans_handle;
37 struct btrfs_transaction;
38 struct btrfs_pending_snapshot;
39 struct btrfs_delayed_ref_root;
40 extern struct kmem_cache *btrfs_trans_handle_cachep;
41 extern struct kmem_cache *btrfs_bit_radix_cachep;
42 extern struct kmem_cache *btrfs_path_cachep;
43 extern struct kmem_cache *btrfs_free_space_cachep;
44 struct btrfs_ordered_sum;
47 #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
49 #define BTRFS_MAX_MIRRORS 3
51 #define BTRFS_MAX_LEVEL 8
53 #define BTRFS_OLDEST_GENERATION 0ULL
56 * the max metadata block size. This limit is somewhat artificial,
57 * but the memmove costs go through the roof for larger blocks.
59 #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
62 * we can actually store much bigger names, but lets not confuse the rest
65 #define BTRFS_NAME_LEN 255
68 * Theoretical limit is larger, but we keep this down to a sane
69 * value. That should limit greatly the possibility of collisions on
72 #define BTRFS_LINK_MAX 65535U
74 /* four bytes for CRC32 */
75 static const int btrfs_csum_sizes[] = { 4 };
76 static const char *btrfs_csum_names[] = { "crc32c" };
78 #define BTRFS_EMPTY_DIR_SIZE 0
80 /* ioprio of readahead is set to idle */
81 #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
83 #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
86 * Use large batch size to reduce overhead of metadata updates. On the reader
87 * side, we only read it when we are close to ENOSPC and the read overhead is
88 * mostly related to the number of CPUs, so it is OK to use arbitrary large
91 #define BTRFS_TOTAL_BYTES_PINNED_BATCH SZ_128M
93 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
97 * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
99 static inline u32 count_max_extents(u64 size)
101 return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
104 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
106 BUG_ON(num_stripes == 0);
107 return sizeof(struct btrfs_chunk) +
108 sizeof(struct btrfs_stripe) * (num_stripes - 1);
112 * Runtime (in-memory) states of filesystem
115 /* Global indicator of serious filesystem errors */
116 BTRFS_FS_STATE_ERROR,
118 * Filesystem is being remounted, allow to skip some operations, like
121 BTRFS_FS_STATE_REMOUNTING,
122 /* Track if a transaction abort has been reported on this filesystem */
123 BTRFS_FS_STATE_TRANS_ABORTED,
125 * Bio operations should be blocked on this filesystem because a source
126 * or target device is being destroyed as part of a device replace
128 BTRFS_FS_STATE_DEV_REPLACING,
129 /* The btrfs_fs_info created for self-tests */
130 BTRFS_FS_STATE_DUMMY_FS_INFO,
133 #define BTRFS_BACKREF_REV_MAX 256
134 #define BTRFS_BACKREF_REV_SHIFT 56
135 #define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
136 BTRFS_BACKREF_REV_SHIFT)
138 #define BTRFS_OLD_BACKREF_REV 0
139 #define BTRFS_MIXED_BACKREF_REV 1
142 * every tree block (leaf or node) starts with this header.
144 struct btrfs_header {
145 /* these first four must match the super block */
146 u8 csum[BTRFS_CSUM_SIZE];
147 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
148 __le64 bytenr; /* which block this node is supposed to live in */
151 /* allowed to be different from the super from here on down */
152 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
157 } __attribute__ ((__packed__));
160 * this is a very generous portion of the super block, giving us
161 * room to translate 14 chunks with 3 stripes each.
163 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
166 * just in case we somehow lose the roots and are not able to mount,
167 * we store an array of the roots from previous transactions
170 #define BTRFS_NUM_BACKUP_ROOTS 4
171 struct btrfs_root_backup {
173 __le64 tree_root_gen;
176 __le64 chunk_root_gen;
179 __le64 extent_root_gen;
188 __le64 csum_root_gen;
198 u8 extent_root_level;
202 /* future and to align */
204 } __attribute__ ((__packed__));
207 * the super block basically lists the main trees of the FS
208 * it currently lacks any block count etc etc
210 struct btrfs_super_block {
211 /* the first 4 fields must match struct btrfs_header */
212 u8 csum[BTRFS_CSUM_SIZE];
213 /* FS specific UUID, visible to user */
214 u8 fsid[BTRFS_FSID_SIZE];
215 __le64 bytenr; /* this block number */
218 /* allowed to be different from the btrfs_header from here own down */
225 /* this will help find the new super based on the log root */
226 __le64 log_root_transid;
229 __le64 root_dir_objectid;
233 __le32 __unused_leafsize;
235 __le32 sys_chunk_array_size;
236 __le64 chunk_root_generation;
238 __le64 compat_ro_flags;
239 __le64 incompat_flags;
244 struct btrfs_dev_item dev_item;
246 char label[BTRFS_LABEL_SIZE];
248 __le64 cache_generation;
249 __le64 uuid_tree_generation;
251 /* the UUID written into btree blocks */
252 u8 metadata_uuid[BTRFS_FSID_SIZE];
254 /* future expansion */
256 u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
257 struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
258 } __attribute__ ((__packed__));
261 * Compat flags that we support. If any incompat flags are set other than the
262 * ones specified below then we will fail to mount
264 #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
265 #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
266 #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
268 #define BTRFS_FEATURE_COMPAT_RO_SUPP \
269 (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
270 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID)
272 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
273 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
275 #define BTRFS_FEATURE_INCOMPAT_SUPP \
276 (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
277 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
278 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
279 BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
280 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
281 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
282 BTRFS_FEATURE_INCOMPAT_RAID56 | \
283 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
284 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
285 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
286 BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
288 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
289 (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
290 #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
293 * A leaf is full of items. offset and size tell us where to find
294 * the item in the leaf (relative to the start of the data area)
297 struct btrfs_disk_key key;
300 } __attribute__ ((__packed__));
303 * leaves have an item area and a data area:
304 * [item0, item1....itemN] [free space] [dataN...data1, data0]
306 * The data is separate from the items to get the keys closer together
310 struct btrfs_header header;
311 struct btrfs_item items[];
312 } __attribute__ ((__packed__));
315 * all non-leaf blocks are nodes, they hold only keys and pointers to
318 struct btrfs_key_ptr {
319 struct btrfs_disk_key key;
322 } __attribute__ ((__packed__));
325 struct btrfs_header header;
326 struct btrfs_key_ptr ptrs[];
327 } __attribute__ ((__packed__));
330 * btrfs_paths remember the path taken from the root down to the leaf.
331 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
332 * to any other levels that are present.
334 * The slots array records the index of the item or block pointer
335 * used while walking the tree.
337 enum { READA_NONE, READA_BACK, READA_FORWARD };
339 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
340 int slots[BTRFS_MAX_LEVEL];
341 /* if there is real range locking, this locks field will change */
342 u8 locks[BTRFS_MAX_LEVEL];
344 /* keep some upper locks as we walk down */
348 * set by btrfs_split_item, tells search_slot to keep all locks
349 * and to force calls to keep space in the nodes
351 unsigned int search_for_split:1;
352 unsigned int keep_locks:1;
353 unsigned int skip_locking:1;
354 unsigned int leave_spinning:1;
355 unsigned int search_commit_root:1;
356 unsigned int need_commit_sem:1;
357 unsigned int skip_release_on_error:1;
359 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
360 sizeof(struct btrfs_item))
361 struct btrfs_dev_replace {
362 u64 replace_state; /* see #define above */
363 time64_t time_started; /* seconds since 1-Jan-1970 */
364 time64_t time_stopped; /* seconds since 1-Jan-1970 */
365 atomic64_t num_write_errors;
366 atomic64_t num_uncorrectable_read_errors;
369 u64 committed_cursor_left;
370 u64 cursor_left_last_write_of_item;
373 u64 cont_reading_from_srcdev_mode; /* see #define above */
376 int item_needs_writeback;
377 struct btrfs_device *srcdev;
378 struct btrfs_device *tgtdev;
380 struct mutex lock_finishing_cancel_unmount;
381 struct rw_semaphore rwsem;
383 struct btrfs_scrub_progress scrub_progress;
385 struct percpu_counter bio_counter;
386 wait_queue_head_t replace_wait;
389 /* For raid type sysfs entries */
390 struct raid_kobject {
393 struct list_head list;
396 struct btrfs_space_info {
399 u64 total_bytes; /* total bytes in the space,
400 this doesn't take mirrors into account */
401 u64 bytes_used; /* total bytes used,
402 this doesn't take mirrors into account */
403 u64 bytes_pinned; /* total bytes pinned, will be freed when the
404 transaction finishes */
405 u64 bytes_reserved; /* total bytes the allocator has reserved for
406 current allocations */
407 u64 bytes_may_use; /* number of bytes that may be used for
408 delalloc/allocations */
409 u64 bytes_readonly; /* total bytes that are read only */
411 u64 max_extent_size; /* This will hold the maximum extent size of
412 the space info if we had an ENOSPC in the
415 unsigned int full:1; /* indicates that we cannot allocate any more
416 chunks for this space */
417 unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
419 unsigned int flush:1; /* set if we are trying to make space */
421 unsigned int force_alloc; /* set if we need to force a chunk
422 alloc for this space */
424 u64 disk_used; /* total bytes used on disk */
425 u64 disk_total; /* total bytes on disk, takes mirrors into
431 * bytes_pinned is kept in line with what is actually pinned, as in
432 * we've called update_block_group and dropped the bytes_used counter
433 * and increased the bytes_pinned counter. However this means that
434 * bytes_pinned does not reflect the bytes that will be pinned once the
435 * delayed refs are flushed, so this counter is inc'ed every time we
436 * call btrfs_free_extent so it is a realtime count of what will be
437 * freed once the transaction is committed. It will be zeroed every
438 * time the transaction commits.
440 struct percpu_counter total_bytes_pinned;
442 struct list_head list;
443 /* Protected by the spinlock 'lock'. */
444 struct list_head ro_bgs;
445 struct list_head priority_tickets;
446 struct list_head tickets;
448 * tickets_id just indicates the next ticket will be handled, so note
449 * it's not stored per ticket.
453 struct rw_semaphore groups_sem;
454 /* for block groups in our same type */
455 struct list_head block_groups[BTRFS_NR_RAID_TYPES];
456 wait_queue_head_t wait;
459 struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
463 * Types of block reserves
466 BTRFS_BLOCK_RSV_GLOBAL,
467 BTRFS_BLOCK_RSV_DELALLOC,
468 BTRFS_BLOCK_RSV_TRANS,
469 BTRFS_BLOCK_RSV_CHUNK,
470 BTRFS_BLOCK_RSV_DELOPS,
471 BTRFS_BLOCK_RSV_DELREFS,
472 BTRFS_BLOCK_RSV_EMPTY,
473 BTRFS_BLOCK_RSV_TEMP,
476 struct btrfs_block_rsv {
479 struct btrfs_space_info *space_info;
483 unsigned short failfast;
486 * Qgroup equivalent for @size @reserved
488 * Unlike normal @size/@reserved for inode rsv, qgroup doesn't care
489 * about things like csum size nor how many tree blocks it will need to
492 * Qgroup cares more about net change of the extent usage.
494 * So for one newly inserted file extent, in worst case it will cause
495 * leaf split and level increase, nodesize for each file extent is
498 * In short, qgroup_size/reserved is the upper limit of possible needed
499 * qgroup metadata reservation.
502 u64 qgroup_rsv_reserved;
506 * free clusters are used to claim free space in relatively large chunks,
507 * allowing us to do less seeky writes. They are used for all metadata
508 * allocations. In ssd_spread mode they are also used for data allocations.
510 struct btrfs_free_cluster {
512 spinlock_t refill_lock;
515 /* largest extent in this cluster */
518 /* first extent starting offset */
521 /* We did a full search and couldn't create a cluster */
524 struct btrfs_block_group_cache *block_group;
526 * when a cluster is allocated from a block group, we put the
527 * cluster onto a list in the block group so that it can
528 * be freed before the block group is freed.
530 struct list_head block_group_list;
533 enum btrfs_caching_type {
537 BTRFS_CACHE_FINISHED,
541 enum btrfs_disk_cache_state {
548 struct btrfs_caching_control {
549 struct list_head list;
551 wait_queue_head_t wait;
552 struct btrfs_work work;
553 struct btrfs_block_group_cache *block_group;
558 /* Once caching_thread() finds this much free space, it will wake up waiters. */
559 #define CACHING_CTL_WAKE_UP SZ_2M
561 struct btrfs_io_ctl {
565 struct btrfs_fs_info *fs_info;
572 unsigned check_crcs:1;
576 * Tree to record all locked full stripes of a RAID5/6 block group
578 struct btrfs_full_stripe_locks_tree {
583 struct btrfs_block_group_cache {
584 struct btrfs_key key;
585 struct btrfs_block_group_item item;
586 struct btrfs_fs_info *fs_info;
594 u64 cache_generation;
597 * If the free space extent count exceeds this number, convert the block
600 u32 bitmap_high_thresh;
603 * If the free space extent count drops below this number, convert the
604 * block group back to extents.
606 u32 bitmap_low_thresh;
609 * It is just used for the delayed data space allocation because
610 * only the data space allocation and the relative metadata update
611 * can be done cross the transaction.
613 struct rw_semaphore data_rwsem;
615 /* for raid56, this is a full stripe, without parity */
616 unsigned long full_stripe_len;
620 unsigned int has_caching_ctl:1;
621 unsigned int removed:1;
623 int disk_cache_state;
625 /* cache tracking stuff */
627 struct btrfs_caching_control *caching_ctl;
628 u64 last_byte_to_unpin;
630 struct btrfs_space_info *space_info;
632 /* free space cache stuff */
633 struct btrfs_free_space_ctl *free_space_ctl;
635 /* block group cache stuff */
636 struct rb_node cache_node;
638 /* for block groups in the same raid type */
639 struct list_head list;
644 /* List of struct btrfs_free_clusters for this block group.
645 * Today it will only have one thing on it, but that may change
647 struct list_head cluster_list;
649 /* For delayed block group creation or deletion of empty block groups */
650 struct list_head bg_list;
652 /* For read-only block groups */
653 struct list_head ro_list;
657 /* For dirty block groups */
658 struct list_head dirty_list;
659 struct list_head io_list;
661 struct btrfs_io_ctl io_ctl;
664 * Incremented when doing extent allocations and holding a read lock
665 * on the space_info's groups_sem semaphore.
666 * Decremented when an ordered extent that represents an IO against this
667 * block group's range is created (after it's added to its inode's
668 * root's list of ordered extents) or immediately after the allocation
669 * if it's a metadata extent or fallocate extent (for these cases we
670 * don't create ordered extents).
672 atomic_t reservations;
675 * Incremented while holding the spinlock *lock* by a task checking if
676 * it can perform a nocow write (incremented if the value for the *ro*
677 * field is 0). Decremented by such tasks once they create an ordered
678 * extent or before that if some error happens before reaching that step.
679 * This is to prevent races between block group relocation and nocow
680 * writes through direct IO.
682 atomic_t nocow_writers;
684 /* Lock for free space tree operations. */
685 struct mutex free_space_lock;
688 * Does the block group need to be added to the free space tree?
689 * Protected by free_space_lock.
691 int needs_free_space;
693 /* Record locked full stripes for RAID5/6 block group */
694 struct btrfs_full_stripe_locks_tree full_stripe_locks_root;
697 /* delayed seq elem */
699 struct list_head list;
703 #define SEQ_LIST_INIT(name) { .list = LIST_HEAD_INIT((name).list), .seq = 0 }
705 #define SEQ_LAST ((u64)-1)
707 enum btrfs_orphan_cleanup_state {
708 ORPHAN_CLEANUP_STARTED = 1,
709 ORPHAN_CLEANUP_DONE = 2,
712 /* used by the raid56 code to lock stripes for read/modify/write */
713 struct btrfs_stripe_hash {
714 struct list_head hash_list;
718 /* used by the raid56 code to lock stripes for read/modify/write */
719 struct btrfs_stripe_hash_table {
720 struct list_head stripe_cache;
721 spinlock_t cache_lock;
723 struct btrfs_stripe_hash table[];
726 #define BTRFS_STRIPE_HASH_TABLE_BITS 11
728 void btrfs_init_async_reclaim_work(struct work_struct *work);
731 struct reloc_control;
733 struct btrfs_fs_devices;
734 struct btrfs_balance_control;
735 struct btrfs_delayed_root;
738 * Block group or device which contains an active swapfile. Used for preventing
739 * unsafe operations while a swapfile is active.
741 * These are sorted on (ptr, inode) (note that a block group or device can
742 * contain more than one swapfile). We compare the pointer values because we
743 * don't actually care what the object is, we just need a quick check whether
744 * the object exists in the rbtree.
746 struct btrfs_swapfile_pin {
751 * If true, ptr points to a struct btrfs_block_group_cache. Otherwise,
752 * ptr points to a struct btrfs_device.
757 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
761 BTRFS_FS_CLOSING_START,
762 BTRFS_FS_CLOSING_DONE,
763 BTRFS_FS_LOG_RECOVERING,
765 BTRFS_FS_QUOTA_ENABLED,
766 BTRFS_FS_UPDATE_UUID_TREE_GEN,
767 BTRFS_FS_CREATING_FREE_SPACE_TREE,
771 BTRFS_FS_QUOTA_OVERRIDE,
772 /* Used to record internally whether fs has been frozen */
775 * Indicate that a whole-filesystem exclusive operation is running
776 * (device replace, resize, device add/delete, balance)
780 * To info transaction_kthread we need an immediate commit so it
781 * doesn't need to wait for commit_interval
783 BTRFS_FS_NEED_ASYNC_COMMIT,
785 * Indicate that balance has been set up from the ioctl and is in the
786 * main phase. The fs_info::balance_ctl is initialized.
788 BTRFS_FS_BALANCE_RUNNING,
790 /* Indicate that the cleaner thread is awake and doing something. */
791 BTRFS_FS_CLEANER_RUNNING,
794 * The checksumming has an optimized version and is considered fast,
795 * so we don't need to offload checksums to workqueues.
797 BTRFS_FS_CSUM_IMPL_FAST,
800 struct btrfs_fs_info {
801 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
803 struct btrfs_root *extent_root;
804 struct btrfs_root *tree_root;
805 struct btrfs_root *chunk_root;
806 struct btrfs_root *dev_root;
807 struct btrfs_root *fs_root;
808 struct btrfs_root *csum_root;
809 struct btrfs_root *quota_root;
810 struct btrfs_root *uuid_root;
811 struct btrfs_root *free_space_root;
813 /* the log root tree is a directory of all the other log roots */
814 struct btrfs_root *log_root_tree;
816 spinlock_t fs_roots_radix_lock;
817 struct radix_tree_root fs_roots_radix;
819 /* block group cache stuff */
820 spinlock_t block_group_cache_lock;
821 u64 first_logical_byte;
822 struct rb_root block_group_cache_tree;
824 /* keep track of unallocated space */
825 atomic64_t free_chunk_space;
827 struct extent_io_tree freed_extents[2];
828 struct extent_io_tree *pinned_extents;
830 /* logical->physical extent mapping */
831 struct extent_map_tree mapping_tree;
834 * block reservation for extent, checksum, root tree and
835 * delayed dir index item
837 struct btrfs_block_rsv global_block_rsv;
838 /* block reservation for metadata operations */
839 struct btrfs_block_rsv trans_block_rsv;
840 /* block reservation for chunk tree */
841 struct btrfs_block_rsv chunk_block_rsv;
842 /* block reservation for delayed operations */
843 struct btrfs_block_rsv delayed_block_rsv;
844 /* block reservation for delayed refs */
845 struct btrfs_block_rsv delayed_refs_rsv;
847 struct btrfs_block_rsv empty_block_rsv;
850 u64 last_trans_committed;
851 u64 avg_delayed_ref_runtime;
854 * this is updated to the current trans every time a full commit
855 * is required instead of the faster short fsync log commits
857 u64 last_trans_log_full_commit;
858 unsigned long mount_opt;
860 * Track requests for actions that need to be done during transaction
861 * commit (like for some mount options).
863 unsigned long pending_changes;
864 unsigned long compress_type:4;
865 unsigned int compress_level;
868 * It is a suggestive number, the read side is safe even it gets a
869 * wrong number because we will write out the data into a regular
870 * extent. The write side(mount/remount) is under ->s_umount lock,
871 * so it is also safe.
875 struct btrfs_transaction *running_transaction;
876 wait_queue_head_t transaction_throttle;
877 wait_queue_head_t transaction_wait;
878 wait_queue_head_t transaction_blocked_wait;
879 wait_queue_head_t async_submit_wait;
882 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
883 * when they are updated.
885 * Because we do not clear the flags for ever, so we needn't use
886 * the lock on the read side.
888 * We also needn't use the lock when we mount the fs, because
889 * there is no other task which will update the flag.
891 spinlock_t super_lock;
892 struct btrfs_super_block *super_copy;
893 struct btrfs_super_block *super_for_commit;
894 struct super_block *sb;
895 struct inode *btree_inode;
896 struct mutex tree_log_mutex;
897 struct mutex transaction_kthread_mutex;
898 struct mutex cleaner_mutex;
899 struct mutex chunk_mutex;
902 * this is taken to make sure we don't set block groups ro after
903 * the free space cache has been allocated on them
905 struct mutex ro_block_group_mutex;
907 /* this is used during read/modify/write to make sure
908 * no two ios are trying to mod the same stripe at the same
911 struct btrfs_stripe_hash_table *stripe_hash_table;
914 * this protects the ordered operations list only while we are
915 * processing all of the entries on it. This way we make
916 * sure the commit code doesn't find the list temporarily empty
917 * because another function happens to be doing non-waiting preflush
918 * before jumping into the main commit.
920 struct mutex ordered_operations_mutex;
922 struct rw_semaphore commit_root_sem;
924 struct rw_semaphore cleanup_work_sem;
926 struct rw_semaphore subvol_sem;
927 struct srcu_struct subvol_srcu;
929 spinlock_t trans_lock;
931 * the reloc mutex goes with the trans lock, it is taken
932 * during commit to protect us from the relocation code
934 struct mutex reloc_mutex;
936 struct list_head trans_list;
937 struct list_head dead_roots;
938 struct list_head caching_block_groups;
940 spinlock_t delayed_iput_lock;
941 struct list_head delayed_iputs;
942 atomic_t nr_delayed_iputs;
943 wait_queue_head_t delayed_iputs_wait;
945 /* this protects tree_mod_seq_list */
946 spinlock_t tree_mod_seq_lock;
947 atomic64_t tree_mod_seq;
948 struct list_head tree_mod_seq_list;
950 /* this protects tree_mod_log */
951 rwlock_t tree_mod_log_lock;
952 struct rb_root tree_mod_log;
954 atomic_t async_delalloc_pages;
957 * this is used to protect the following list -- ordered_roots.
959 spinlock_t ordered_root_lock;
962 * all fs/file tree roots in which there are data=ordered extents
963 * pending writeback are added into this list.
965 * these can span multiple transactions and basically include
966 * every dirty data page that isn't from nodatacow
968 struct list_head ordered_roots;
970 struct mutex delalloc_root_mutex;
971 spinlock_t delalloc_root_lock;
972 /* all fs/file tree roots that have delalloc inodes. */
973 struct list_head delalloc_roots;
976 * there is a pool of worker threads for checksumming during writes
977 * and a pool for checksumming after reads. This is because readers
978 * can run with FS locks held, and the writers may be waiting for
979 * those locks. We don't want ordering in the pending list to cause
980 * deadlocks, and so the two are serviced separately.
982 * A third pool does submit_bio to avoid deadlocking with the other
985 struct btrfs_workqueue *workers;
986 struct btrfs_workqueue *delalloc_workers;
987 struct btrfs_workqueue *flush_workers;
988 struct btrfs_workqueue *endio_workers;
989 struct btrfs_workqueue *endio_meta_workers;
990 struct btrfs_workqueue *endio_raid56_workers;
991 struct btrfs_workqueue *endio_repair_workers;
992 struct btrfs_workqueue *rmw_workers;
993 struct btrfs_workqueue *endio_meta_write_workers;
994 struct btrfs_workqueue *endio_write_workers;
995 struct btrfs_workqueue *endio_freespace_worker;
996 struct btrfs_workqueue *submit_workers;
997 struct btrfs_workqueue *caching_workers;
998 struct btrfs_workqueue *readahead_workers;
1001 * fixup workers take dirty pages that didn't properly go through
1002 * the cow mechanism and make them safe to write. It happens
1003 * for the sys_munmap function call path
1005 struct btrfs_workqueue *fixup_workers;
1006 struct btrfs_workqueue *delayed_workers;
1008 /* the extent workers do delayed refs on the extent allocation tree */
1009 struct btrfs_workqueue *extent_workers;
1010 struct task_struct *transaction_kthread;
1011 struct task_struct *cleaner_kthread;
1012 u32 thread_pool_size;
1014 struct kobject *space_info_kobj;
1015 struct list_head pending_raid_kobjs;
1016 spinlock_t pending_raid_kobjs_lock; /* uncontended */
1020 /* used to keep from writing metadata until there is a nice batch */
1021 struct percpu_counter dirty_metadata_bytes;
1022 struct percpu_counter delalloc_bytes;
1023 struct percpu_counter dio_bytes;
1024 s32 dirty_metadata_batch;
1027 struct list_head dirty_cowonly_roots;
1029 struct btrfs_fs_devices *fs_devices;
1032 * The space_info list is effectively read only after initial
1033 * setup. It is populated at mount time and cleaned up after
1034 * all block groups are removed. RCU is used to protect it.
1036 struct list_head space_info;
1038 struct btrfs_space_info *data_sinfo;
1040 struct reloc_control *reloc_ctl;
1042 /* data_alloc_cluster is only used in ssd_spread mode */
1043 struct btrfs_free_cluster data_alloc_cluster;
1045 /* all metadata allocations go through this cluster */
1046 struct btrfs_free_cluster meta_alloc_cluster;
1048 /* auto defrag inodes go here */
1049 spinlock_t defrag_inodes_lock;
1050 struct rb_root defrag_inodes;
1051 atomic_t defrag_running;
1053 /* Used to protect avail_{data, metadata, system}_alloc_bits */
1054 seqlock_t profiles_lock;
1056 * these three are in extended format (availability of single
1057 * chunks is denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other
1058 * types are denoted by corresponding BTRFS_BLOCK_GROUP_* bits)
1060 u64 avail_data_alloc_bits;
1061 u64 avail_metadata_alloc_bits;
1062 u64 avail_system_alloc_bits;
1064 /* restriper state */
1065 spinlock_t balance_lock;
1066 struct mutex balance_mutex;
1067 atomic_t balance_pause_req;
1068 atomic_t balance_cancel_req;
1069 struct btrfs_balance_control *balance_ctl;
1070 wait_queue_head_t balance_wait_q;
1072 u32 data_chunk_allocations;
1077 /* private scrub information */
1078 struct mutex scrub_lock;
1079 atomic_t scrubs_running;
1080 atomic_t scrub_pause_req;
1081 atomic_t scrubs_paused;
1082 atomic_t scrub_cancel_req;
1083 wait_queue_head_t scrub_pause_wait;
1085 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
1088 refcount_t scrub_workers_refcnt;
1089 struct btrfs_workqueue *scrub_workers;
1090 struct btrfs_workqueue *scrub_wr_completion_workers;
1091 struct btrfs_workqueue *scrub_parity_workers;
1093 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1094 u32 check_integrity_print_mask;
1096 /* is qgroup tracking in a consistent state? */
1099 /* holds configuration and tracking. Protected by qgroup_lock */
1100 struct rb_root qgroup_tree;
1101 spinlock_t qgroup_lock;
1104 * used to avoid frequently calling ulist_alloc()/ulist_free()
1105 * when doing qgroup accounting, it must be protected by qgroup_lock.
1107 struct ulist *qgroup_ulist;
1109 /* protect user change for quota operations */
1110 struct mutex qgroup_ioctl_lock;
1112 /* list of dirty qgroups to be written at next commit */
1113 struct list_head dirty_qgroups;
1115 /* used by qgroup for an efficient tree traversal */
1118 /* qgroup rescan items */
1119 struct mutex qgroup_rescan_lock; /* protects the progress item */
1120 struct btrfs_key qgroup_rescan_progress;
1121 struct btrfs_workqueue *qgroup_rescan_workers;
1122 struct completion qgroup_rescan_completion;
1123 struct btrfs_work qgroup_rescan_work;
1124 bool qgroup_rescan_running; /* protected by qgroup_rescan_lock */
1126 /* filesystem state */
1127 unsigned long fs_state;
1129 struct btrfs_delayed_root *delayed_root;
1131 /* readahead tree */
1132 spinlock_t reada_lock;
1133 struct radix_tree_root reada_tree;
1135 /* readahead works cnt */
1136 atomic_t reada_works_cnt;
1138 /* Extent buffer radix tree */
1139 spinlock_t buffer_lock;
1140 struct radix_tree_root buffer_radix;
1142 /* next backup root to be overwritten */
1143 int backup_root_index;
1145 /* device replace state */
1146 struct btrfs_dev_replace dev_replace;
1148 struct semaphore uuid_tree_rescan_sem;
1150 /* Used to reclaim the metadata space in the background. */
1151 struct work_struct async_reclaim_work;
1153 spinlock_t unused_bgs_lock;
1154 struct list_head unused_bgs;
1155 struct mutex unused_bg_unpin_mutex;
1156 struct mutex delete_unused_bgs_mutex;
1158 /* Cached block sizes */
1163 /* Block groups and devices containing active swapfiles. */
1164 spinlock_t swapfile_pins_lock;
1165 struct rb_root swapfile_pins;
1167 struct crypto_shash *csum_shash;
1169 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1170 spinlock_t ref_verify_lock;
1171 struct rb_root block_tree;
1175 static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
1177 return sb->s_fs_info;
1180 struct btrfs_subvolume_writers {
1181 struct percpu_counter counter;
1182 wait_queue_head_t wait;
1186 * The state of btrfs root
1190 * btrfs_record_root_in_trans is a multi-step process, and it can race
1191 * with the balancing code. But the race is very small, and only the
1192 * first time the root is added to each transaction. So IN_TRANS_SETUP
1193 * is used to tell us when more checks are required
1195 BTRFS_ROOT_IN_TRANS_SETUP,
1196 BTRFS_ROOT_REF_COWS,
1197 BTRFS_ROOT_TRACK_DIRTY,
1198 BTRFS_ROOT_IN_RADIX,
1199 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
1200 BTRFS_ROOT_DEFRAG_RUNNING,
1201 BTRFS_ROOT_FORCE_COW,
1202 BTRFS_ROOT_MULTI_LOG_TASKS,
1204 BTRFS_ROOT_DELETING,
1207 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
1209 * Set for the subvolume tree owning the reloc tree.
1211 BTRFS_ROOT_DEAD_RELOC_TREE,
1212 /* Mark dead root stored on device whose cleanup needs to be resumed */
1213 BTRFS_ROOT_DEAD_TREE,
1217 * Record swapped tree blocks of a subvolume tree for delayed subtree trace
1218 * code. For detail check comment in fs/btrfs/qgroup.c.
1220 struct btrfs_qgroup_swapped_blocks {
1222 /* RM_EMPTY_ROOT() of above blocks[] */
1224 struct rb_root blocks[BTRFS_MAX_LEVEL];
1228 * in ram representation of the tree. extent_root is used for all allocations
1229 * and for the extent tree extent_root root.
1232 struct extent_buffer *node;
1234 struct extent_buffer *commit_root;
1235 struct btrfs_root *log_root;
1236 struct btrfs_root *reloc_root;
1238 unsigned long state;
1239 struct btrfs_root_item root_item;
1240 struct btrfs_key root_key;
1241 struct btrfs_fs_info *fs_info;
1242 struct extent_io_tree dirty_log_pages;
1244 struct mutex objectid_mutex;
1246 spinlock_t accounting_lock;
1247 struct btrfs_block_rsv *block_rsv;
1249 /* free ino cache stuff */
1250 struct btrfs_free_space_ctl *free_ino_ctl;
1251 enum btrfs_caching_type ino_cache_state;
1252 spinlock_t ino_cache_lock;
1253 wait_queue_head_t ino_cache_wait;
1254 struct btrfs_free_space_ctl *free_ino_pinned;
1255 u64 ino_cache_progress;
1256 struct inode *ino_cache_inode;
1258 struct mutex log_mutex;
1259 wait_queue_head_t log_writer_wait;
1260 wait_queue_head_t log_commit_wait[2];
1261 struct list_head log_ctxs[2];
1262 atomic_t log_writers;
1263 atomic_t log_commit[2];
1266 /* No matter the commit succeeds or not*/
1267 int log_transid_committed;
1268 /* Just be updated when the commit succeeds. */
1269 int last_log_commit;
1270 pid_t log_start_pid;
1276 u64 highest_objectid;
1278 u64 defrag_trans_start;
1279 struct btrfs_key defrag_progress;
1280 struct btrfs_key defrag_max;
1282 /* the dirty list is only used by non-reference counted roots */
1283 struct list_head dirty_list;
1285 struct list_head root_list;
1287 spinlock_t log_extents_lock[2];
1288 struct list_head logged_list[2];
1290 int orphan_cleanup_state;
1292 spinlock_t inode_lock;
1293 /* red-black tree that keeps track of in-memory inodes */
1294 struct rb_root inode_tree;
1297 * radix tree that keeps track of delayed nodes of every inode,
1298 * protected by inode_lock
1300 struct radix_tree_root delayed_nodes_tree;
1302 * right now this just gets used so that a root has its own devid
1303 * for stat. It may be used for more later
1307 spinlock_t root_item_lock;
1310 struct mutex delalloc_mutex;
1311 spinlock_t delalloc_lock;
1313 * all of the inodes that have delalloc bytes. It is possible for
1314 * this list to be empty even when there is still dirty data=ordered
1315 * extents waiting to finish IO.
1317 struct list_head delalloc_inodes;
1318 struct list_head delalloc_root;
1319 u64 nr_delalloc_inodes;
1321 struct mutex ordered_extent_mutex;
1323 * this is used by the balancing code to wait for all the pending
1326 spinlock_t ordered_extent_lock;
1329 * all of the data=ordered extents pending writeback
1330 * these can span multiple transactions and basically include
1331 * every dirty data page that isn't from nodatacow
1333 struct list_head ordered_extents;
1334 struct list_head ordered_root;
1335 u64 nr_ordered_extents;
1338 * Not empty if this subvolume root has gone through tree block swap
1341 * Will be used by reloc_control::dirty_subvol_roots.
1343 struct list_head reloc_dirty_list;
1346 * Number of currently running SEND ioctls to prevent
1347 * manipulation with the read-only status via SUBVOL_SETFLAGS
1349 int send_in_progress;
1351 * Number of currently running deduplication operations that have a
1352 * destination inode belonging to this root. Protected by the lock
1355 int dedupe_in_progress;
1356 struct btrfs_subvolume_writers *subv_writers;
1357 atomic_t will_be_snapshotted;
1358 atomic_t snapshot_force_cow;
1360 /* For qgroup metadata reserved space */
1361 spinlock_t qgroup_meta_rsv_lock;
1362 u64 qgroup_meta_rsv_pertrans;
1363 u64 qgroup_meta_rsv_prealloc;
1365 /* Number of active swapfiles */
1366 atomic_t nr_swapfiles;
1368 /* Record pairs of swapped blocks for qgroup */
1369 struct btrfs_qgroup_swapped_blocks swapped_blocks;
1371 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1376 struct btrfs_file_private {
1380 static inline u32 btrfs_inode_sectorsize(const struct inode *inode)
1382 return btrfs_sb(inode->i_sb)->sectorsize;
1385 static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
1388 return info->nodesize - sizeof(struct btrfs_header);
1391 #define BTRFS_LEAF_DATA_OFFSET offsetof(struct btrfs_leaf, items)
1393 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
1395 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
1398 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
1400 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
1403 #define BTRFS_FILE_EXTENT_INLINE_DATA_START \
1404 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
1405 static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
1407 return BTRFS_MAX_ITEM_SIZE(info) -
1408 BTRFS_FILE_EXTENT_INLINE_DATA_START;
1411 static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1413 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
1417 * Flags for mount options.
1419 * Note: don't forget to add new options to btrfs_show_options()
1421 #define BTRFS_MOUNT_NODATASUM (1 << 0)
1422 #define BTRFS_MOUNT_NODATACOW (1 << 1)
1423 #define BTRFS_MOUNT_NOBARRIER (1 << 2)
1424 #define BTRFS_MOUNT_SSD (1 << 3)
1425 #define BTRFS_MOUNT_DEGRADED (1 << 4)
1426 #define BTRFS_MOUNT_COMPRESS (1 << 5)
1427 #define BTRFS_MOUNT_NOTREELOG (1 << 6)
1428 #define BTRFS_MOUNT_FLUSHONCOMMIT (1 << 7)
1429 #define BTRFS_MOUNT_SSD_SPREAD (1 << 8)
1430 #define BTRFS_MOUNT_NOSSD (1 << 9)
1431 #define BTRFS_MOUNT_DISCARD (1 << 10)
1432 #define BTRFS_MOUNT_FORCE_COMPRESS (1 << 11)
1433 #define BTRFS_MOUNT_SPACE_CACHE (1 << 12)
1434 #define BTRFS_MOUNT_CLEAR_CACHE (1 << 13)
1435 #define BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED (1 << 14)
1436 #define BTRFS_MOUNT_ENOSPC_DEBUG (1 << 15)
1437 #define BTRFS_MOUNT_AUTO_DEFRAG (1 << 16)
1438 #define BTRFS_MOUNT_INODE_MAP_CACHE (1 << 17)
1439 #define BTRFS_MOUNT_USEBACKUPROOT (1 << 18)
1440 #define BTRFS_MOUNT_SKIP_BALANCE (1 << 19)
1441 #define BTRFS_MOUNT_CHECK_INTEGRITY (1 << 20)
1442 #define BTRFS_MOUNT_CHECK_INTEGRITY_INCLUDING_EXTENT_DATA (1 << 21)
1443 #define BTRFS_MOUNT_PANIC_ON_FATAL_ERROR (1 << 22)
1444 #define BTRFS_MOUNT_RESCAN_UUID_TREE (1 << 23)
1445 #define BTRFS_MOUNT_FRAGMENT_DATA (1 << 24)
1446 #define BTRFS_MOUNT_FRAGMENT_METADATA (1 << 25)
1447 #define BTRFS_MOUNT_FREE_SPACE_TREE (1 << 26)
1448 #define BTRFS_MOUNT_NOLOGREPLAY (1 << 27)
1449 #define BTRFS_MOUNT_REF_VERIFY (1 << 28)
1451 #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
1452 #define BTRFS_DEFAULT_MAX_INLINE (2048)
1454 #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
1455 #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
1456 #define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
1457 #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
1460 #define btrfs_set_and_info(fs_info, opt, fmt, args...) \
1462 if (!btrfs_test_opt(fs_info, opt)) \
1463 btrfs_info(fs_info, fmt, ##args); \
1464 btrfs_set_opt(fs_info->mount_opt, opt); \
1467 #define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
1469 if (btrfs_test_opt(fs_info, opt)) \
1470 btrfs_info(fs_info, fmt, ##args); \
1471 btrfs_clear_opt(fs_info->mount_opt, opt); \
1474 #ifdef CONFIG_BTRFS_DEBUG
1476 btrfs_should_fragment_free_space(struct btrfs_block_group_cache *block_group)
1478 struct btrfs_fs_info *fs_info = block_group->fs_info;
1480 return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
1481 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
1482 (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
1483 block_group->flags & BTRFS_BLOCK_GROUP_DATA);
1488 * Requests for changes that need to be done during transaction commit.
1490 * Internal mount options that are used for special handling of the real
1491 * mount options (eg. cannot be set during remount and have to be set during
1492 * transaction commit)
1495 #define BTRFS_PENDING_SET_INODE_MAP_CACHE (0)
1496 #define BTRFS_PENDING_CLEAR_INODE_MAP_CACHE (1)
1497 #define BTRFS_PENDING_COMMIT (2)
1499 #define btrfs_test_pending(info, opt) \
1500 test_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1501 #define btrfs_set_pending(info, opt) \
1502 set_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1503 #define btrfs_clear_pending(info, opt) \
1504 clear_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1507 * Helpers for setting pending mount option changes.
1509 * Expects corresponding macros
1510 * BTRFS_PENDING_SET_ and CLEAR_ + short mount option name
1512 #define btrfs_set_pending_and_info(info, opt, fmt, args...) \
1514 if (!btrfs_raw_test_opt((info)->mount_opt, opt)) { \
1515 btrfs_info((info), fmt, ##args); \
1516 btrfs_set_pending((info), SET_##opt); \
1517 btrfs_clear_pending((info), CLEAR_##opt); \
1521 #define btrfs_clear_pending_and_info(info, opt, fmt, args...) \
1523 if (btrfs_raw_test_opt((info)->mount_opt, opt)) { \
1524 btrfs_info((info), fmt, ##args); \
1525 btrfs_set_pending((info), CLEAR_##opt); \
1526 btrfs_clear_pending((info), SET_##opt); \
1533 #define BTRFS_INODE_NODATASUM (1 << 0)
1534 #define BTRFS_INODE_NODATACOW (1 << 1)
1535 #define BTRFS_INODE_READONLY (1 << 2)
1536 #define BTRFS_INODE_NOCOMPRESS (1 << 3)
1537 #define BTRFS_INODE_PREALLOC (1 << 4)
1538 #define BTRFS_INODE_SYNC (1 << 5)
1539 #define BTRFS_INODE_IMMUTABLE (1 << 6)
1540 #define BTRFS_INODE_APPEND (1 << 7)
1541 #define BTRFS_INODE_NODUMP (1 << 8)
1542 #define BTRFS_INODE_NOATIME (1 << 9)
1543 #define BTRFS_INODE_DIRSYNC (1 << 10)
1544 #define BTRFS_INODE_COMPRESS (1 << 11)
1546 #define BTRFS_INODE_ROOT_ITEM_INIT (1 << 31)
1548 #define BTRFS_INODE_FLAG_MASK \
1549 (BTRFS_INODE_NODATASUM | \
1550 BTRFS_INODE_NODATACOW | \
1551 BTRFS_INODE_READONLY | \
1552 BTRFS_INODE_NOCOMPRESS | \
1553 BTRFS_INODE_PREALLOC | \
1554 BTRFS_INODE_SYNC | \
1555 BTRFS_INODE_IMMUTABLE | \
1556 BTRFS_INODE_APPEND | \
1557 BTRFS_INODE_NODUMP | \
1558 BTRFS_INODE_NOATIME | \
1559 BTRFS_INODE_DIRSYNC | \
1560 BTRFS_INODE_COMPRESS | \
1561 BTRFS_INODE_ROOT_ITEM_INIT)
1563 struct btrfs_map_token {
1564 const struct extent_buffer *eb;
1566 unsigned long offset;
1569 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
1570 ((bytes) >> (fs_info)->sb->s_blocksize_bits)
1572 static inline void btrfs_init_map_token (struct btrfs_map_token *token)
1574 token->kaddr = NULL;
1577 /* some macros to generate set/get functions for the struct fields. This
1578 * assumes there is a lefoo_to_cpu for every type, so lets make a simple
1581 #define le8_to_cpu(v) (v)
1582 #define cpu_to_le8(v) (v)
1585 #define read_eb_member(eb, ptr, type, member, result) (\
1586 read_extent_buffer(eb, (char *)(result), \
1587 ((unsigned long)(ptr)) + \
1588 offsetof(type, member), \
1589 sizeof(((type *)0)->member)))
1591 #define write_eb_member(eb, ptr, type, member, result) (\
1592 write_extent_buffer(eb, (char *)(result), \
1593 ((unsigned long)(ptr)) + \
1594 offsetof(type, member), \
1595 sizeof(((type *)0)->member)))
1597 #define DECLARE_BTRFS_SETGET_BITS(bits) \
1598 u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
1599 const void *ptr, unsigned long off, \
1600 struct btrfs_map_token *token); \
1601 void btrfs_set_token_##bits(struct extent_buffer *eb, const void *ptr, \
1602 unsigned long off, u##bits val, \
1603 struct btrfs_map_token *token); \
1604 static inline u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
1606 unsigned long off) \
1608 return btrfs_get_token_##bits(eb, ptr, off, NULL); \
1610 static inline void btrfs_set_##bits(struct extent_buffer *eb, void *ptr,\
1611 unsigned long off, u##bits val) \
1613 btrfs_set_token_##bits(eb, ptr, off, val, NULL); \
1616 DECLARE_BTRFS_SETGET_BITS(8)
1617 DECLARE_BTRFS_SETGET_BITS(16)
1618 DECLARE_BTRFS_SETGET_BITS(32)
1619 DECLARE_BTRFS_SETGET_BITS(64)
1621 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
1622 static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
1625 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1626 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
1628 static inline void btrfs_set_##name(struct extent_buffer *eb, type *s, \
1631 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1632 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
1634 static inline u##bits btrfs_token_##name(const struct extent_buffer *eb,\
1636 struct btrfs_map_token *token) \
1638 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1639 return btrfs_get_token_##bits(eb, s, offsetof(type, member), token); \
1641 static inline void btrfs_set_token_##name(struct extent_buffer *eb, \
1642 type *s, u##bits val, \
1643 struct btrfs_map_token *token) \
1645 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1646 btrfs_set_token_##bits(eb, s, offsetof(type, member), val, token); \
1649 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
1650 static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
1652 const type *p = page_address(eb->pages[0]); \
1653 u##bits res = le##bits##_to_cpu(p->member); \
1656 static inline void btrfs_set_##name(struct extent_buffer *eb, \
1659 type *p = page_address(eb->pages[0]); \
1660 p->member = cpu_to_le##bits(val); \
1663 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
1664 static inline u##bits btrfs_##name(const type *s) \
1666 return le##bits##_to_cpu(s->member); \
1668 static inline void btrfs_set_##name(type *s, u##bits val) \
1670 s->member = cpu_to_le##bits(val); \
1674 static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb,
1675 struct btrfs_dev_item *s)
1677 BUILD_BUG_ON(sizeof(u64) !=
1678 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1679 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
1682 static inline void btrfs_set_device_total_bytes(struct extent_buffer *eb,
1683 struct btrfs_dev_item *s,
1686 BUILD_BUG_ON(sizeof(u64) !=
1687 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1688 WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
1689 btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
1693 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
1694 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
1695 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
1696 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
1697 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
1699 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
1700 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
1701 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
1702 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
1703 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
1704 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
1706 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
1707 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
1709 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
1711 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
1713 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
1715 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
1717 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
1718 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
1720 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
1722 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
1724 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
1727 static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
1729 return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
1732 static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
1734 return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
1737 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
1738 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
1739 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
1740 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
1741 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
1742 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
1743 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
1744 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
1745 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
1746 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
1747 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
1749 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
1751 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
1754 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
1755 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
1756 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
1758 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
1760 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
1762 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
1764 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
1765 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
1767 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
1769 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
1770 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
1772 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
1775 unsigned long offset = (unsigned long)c;
1776 offset += offsetof(struct btrfs_chunk, stripe);
1777 offset += nr * sizeof(struct btrfs_stripe);
1778 return (struct btrfs_stripe *)offset;
1781 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
1783 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1786 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
1787 struct btrfs_chunk *c, int nr)
1789 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1792 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
1793 struct btrfs_chunk *c, int nr)
1795 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1798 /* struct btrfs_block_group_item */
1799 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
1801 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
1803 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
1804 struct btrfs_block_group_item, chunk_objectid, 64);
1806 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
1807 struct btrfs_block_group_item, chunk_objectid, 64);
1808 BTRFS_SETGET_FUNCS(disk_block_group_flags,
1809 struct btrfs_block_group_item, flags, 64);
1810 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
1811 struct btrfs_block_group_item, flags, 64);
1813 /* struct btrfs_free_space_info */
1814 BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
1816 BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
1818 /* struct btrfs_inode_ref */
1819 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1820 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
1822 /* struct btrfs_inode_extref */
1823 BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
1824 parent_objectid, 64);
1825 BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
1827 BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
1829 /* struct btrfs_inode_item */
1830 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
1831 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
1832 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
1833 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
1834 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
1835 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1836 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1837 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1838 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1839 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
1840 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
1841 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
1842 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1844 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1846 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1848 BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1849 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1851 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1853 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1854 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1855 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1856 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1857 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1858 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
1859 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1860 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1861 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1862 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
1864 /* struct btrfs_dev_extent */
1865 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1867 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1868 chunk_objectid, 64);
1869 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1871 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1873 static inline unsigned long btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1875 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1876 return (unsigned long)dev + ptr;
1879 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1880 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1882 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
1884 BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
1887 BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1889 static inline void btrfs_tree_block_key(struct extent_buffer *eb,
1890 struct btrfs_tree_block_info *item,
1891 struct btrfs_disk_key *key)
1893 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1896 static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
1897 struct btrfs_tree_block_info *item,
1898 struct btrfs_disk_key *key)
1900 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1903 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
1905 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
1907 BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
1909 BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
1912 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
1915 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
1917 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
1920 static inline u32 btrfs_extent_inline_ref_size(int type)
1922 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1923 type == BTRFS_SHARED_BLOCK_REF_KEY)
1924 return sizeof(struct btrfs_extent_inline_ref);
1925 if (type == BTRFS_SHARED_DATA_REF_KEY)
1926 return sizeof(struct btrfs_shared_data_ref) +
1927 sizeof(struct btrfs_extent_inline_ref);
1928 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1929 return sizeof(struct btrfs_extent_data_ref) +
1930 offsetof(struct btrfs_extent_inline_ref, offset);
1934 BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
1935 BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
1937 BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
1938 BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
1940 /* struct btrfs_node */
1941 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1942 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1943 BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr,
1945 BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
1948 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
1951 ptr = offsetof(struct btrfs_node, ptrs) +
1952 sizeof(struct btrfs_key_ptr) * nr;
1953 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1956 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
1960 ptr = offsetof(struct btrfs_node, ptrs) +
1961 sizeof(struct btrfs_key_ptr) * nr;
1962 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1965 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
1968 ptr = offsetof(struct btrfs_node, ptrs) +
1969 sizeof(struct btrfs_key_ptr) * nr;
1970 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1973 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1977 ptr = offsetof(struct btrfs_node, ptrs) +
1978 sizeof(struct btrfs_key_ptr) * nr;
1979 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1982 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1984 return offsetof(struct btrfs_node, ptrs) +
1985 sizeof(struct btrfs_key_ptr) * nr;
1988 void btrfs_node_key(const struct extent_buffer *eb,
1989 struct btrfs_disk_key *disk_key, int nr);
1991 static inline void btrfs_set_node_key(struct extent_buffer *eb,
1992 struct btrfs_disk_key *disk_key, int nr)
1995 ptr = btrfs_node_key_ptr_offset(nr);
1996 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1997 struct btrfs_key_ptr, key, disk_key);
2000 /* struct btrfs_item */
2001 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
2002 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
2003 BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
2004 BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
2006 static inline unsigned long btrfs_item_nr_offset(int nr)
2008 return offsetof(struct btrfs_leaf, items) +
2009 sizeof(struct btrfs_item) * nr;
2012 static inline struct btrfs_item *btrfs_item_nr(int nr)
2014 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
2017 static inline u32 btrfs_item_end(const struct extent_buffer *eb,
2018 struct btrfs_item *item)
2020 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
2023 static inline u32 btrfs_item_end_nr(const struct extent_buffer *eb, int nr)
2025 return btrfs_item_end(eb, btrfs_item_nr(nr));
2028 static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
2030 return btrfs_item_offset(eb, btrfs_item_nr(nr));
2033 static inline u32 btrfs_item_size_nr(const struct extent_buffer *eb, int nr)
2035 return btrfs_item_size(eb, btrfs_item_nr(nr));
2038 static inline void btrfs_item_key(const struct extent_buffer *eb,
2039 struct btrfs_disk_key *disk_key, int nr)
2041 struct btrfs_item *item = btrfs_item_nr(nr);
2042 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
2045 static inline void btrfs_set_item_key(struct extent_buffer *eb,
2046 struct btrfs_disk_key *disk_key, int nr)
2048 struct btrfs_item *item = btrfs_item_nr(nr);
2049 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
2052 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
2055 * struct btrfs_root_ref
2057 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
2058 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
2059 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
2061 /* struct btrfs_dir_item */
2062 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
2063 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
2064 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
2065 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
2066 BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
2067 BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item,
2069 BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item,
2071 BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item,
2074 static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
2075 const struct btrfs_dir_item *item,
2076 struct btrfs_disk_key *key)
2078 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
2081 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
2082 struct btrfs_dir_item *item,
2083 const struct btrfs_disk_key *key)
2085 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
2088 BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
2090 BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
2092 BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
2095 static inline void btrfs_free_space_key(const struct extent_buffer *eb,
2096 const struct btrfs_free_space_header *h,
2097 struct btrfs_disk_key *key)
2099 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2102 static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
2103 struct btrfs_free_space_header *h,
2104 const struct btrfs_disk_key *key)
2106 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2109 /* struct btrfs_disk_key */
2110 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
2112 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
2113 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
2115 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
2116 const struct btrfs_disk_key *disk)
2118 cpu->offset = le64_to_cpu(disk->offset);
2119 cpu->type = disk->type;
2120 cpu->objectid = le64_to_cpu(disk->objectid);
2123 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
2124 const struct btrfs_key *cpu)
2126 disk->offset = cpu_to_le64(cpu->offset);
2127 disk->type = cpu->type;
2128 disk->objectid = cpu_to_le64(cpu->objectid);
2131 static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2132 struct btrfs_key *key, int nr)
2134 struct btrfs_disk_key disk_key;
2135 btrfs_node_key(eb, &disk_key, nr);
2136 btrfs_disk_key_to_cpu(key, &disk_key);
2139 static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2140 struct btrfs_key *key, int nr)
2142 struct btrfs_disk_key disk_key;
2143 btrfs_item_key(eb, &disk_key, nr);
2144 btrfs_disk_key_to_cpu(key, &disk_key);
2147 static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2148 const struct btrfs_dir_item *item,
2149 struct btrfs_key *key)
2151 struct btrfs_disk_key disk_key;
2152 btrfs_dir_item_key(eb, item, &disk_key);
2153 btrfs_disk_key_to_cpu(key, &disk_key);
2156 static inline u8 btrfs_key_type(const struct btrfs_key *key)
2161 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
2166 /* struct btrfs_header */
2167 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
2168 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
2170 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
2171 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
2172 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
2173 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
2174 BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
2176 BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
2177 BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header,
2179 BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
2181 static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
2183 return (btrfs_header_flags(eb) & flag) == flag;
2186 static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
2188 u64 flags = btrfs_header_flags(eb);
2189 btrfs_set_header_flags(eb, flags | flag);
2192 static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
2194 u64 flags = btrfs_header_flags(eb);
2195 btrfs_set_header_flags(eb, flags & ~flag);
2198 static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
2200 u64 flags = btrfs_header_flags(eb);
2201 return flags >> BTRFS_BACKREF_REV_SHIFT;
2204 static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
2207 u64 flags = btrfs_header_flags(eb);
2208 flags &= ~BTRFS_BACKREF_REV_MASK;
2209 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
2210 btrfs_set_header_flags(eb, flags);
2213 static inline unsigned long btrfs_header_fsid(void)
2215 return offsetof(struct btrfs_header, fsid);
2218 static inline unsigned long btrfs_header_chunk_tree_uuid(const struct extent_buffer *eb)
2220 return offsetof(struct btrfs_header, chunk_tree_uuid);
2223 static inline int btrfs_is_leaf(const struct extent_buffer *eb)
2225 return btrfs_header_level(eb) == 0;
2228 /* struct btrfs_root_item */
2229 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
2231 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
2232 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
2233 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
2235 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
2237 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
2238 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
2239 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
2240 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
2241 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
2242 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
2243 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
2244 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
2246 BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
2248 BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
2250 BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
2252 BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
2254 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
2257 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
2259 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
2262 static inline bool btrfs_root_dead(const struct btrfs_root *root)
2264 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
2267 /* struct btrfs_root_backup */
2268 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
2270 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
2272 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
2273 tree_root_level, 8);
2275 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
2277 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
2278 chunk_root_gen, 64);
2279 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
2280 chunk_root_level, 8);
2282 BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
2284 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
2285 extent_root_gen, 64);
2286 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
2287 extent_root_level, 8);
2289 BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
2291 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
2293 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
2296 BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
2298 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
2300 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
2303 BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
2305 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
2307 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
2308 csum_root_level, 8);
2309 BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
2311 BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
2313 BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
2316 /* struct btrfs_balance_item */
2317 BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
2319 static inline void btrfs_balance_data(const struct extent_buffer *eb,
2320 const struct btrfs_balance_item *bi,
2321 struct btrfs_disk_balance_args *ba)
2323 read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2326 static inline void btrfs_set_balance_data(struct extent_buffer *eb,
2327 struct btrfs_balance_item *bi,
2328 const struct btrfs_disk_balance_args *ba)
2330 write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2333 static inline void btrfs_balance_meta(const struct extent_buffer *eb,
2334 const struct btrfs_balance_item *bi,
2335 struct btrfs_disk_balance_args *ba)
2337 read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2340 static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
2341 struct btrfs_balance_item *bi,
2342 const struct btrfs_disk_balance_args *ba)
2344 write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2347 static inline void btrfs_balance_sys(const struct extent_buffer *eb,
2348 const struct btrfs_balance_item *bi,
2349 struct btrfs_disk_balance_args *ba)
2351 read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2354 static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
2355 struct btrfs_balance_item *bi,
2356 const struct btrfs_disk_balance_args *ba)
2358 write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2362 btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
2363 const struct btrfs_disk_balance_args *disk)
2365 memset(cpu, 0, sizeof(*cpu));
2367 cpu->profiles = le64_to_cpu(disk->profiles);
2368 cpu->usage = le64_to_cpu(disk->usage);
2369 cpu->devid = le64_to_cpu(disk->devid);
2370 cpu->pstart = le64_to_cpu(disk->pstart);
2371 cpu->pend = le64_to_cpu(disk->pend);
2372 cpu->vstart = le64_to_cpu(disk->vstart);
2373 cpu->vend = le64_to_cpu(disk->vend);
2374 cpu->target = le64_to_cpu(disk->target);
2375 cpu->flags = le64_to_cpu(disk->flags);
2376 cpu->limit = le64_to_cpu(disk->limit);
2377 cpu->stripes_min = le32_to_cpu(disk->stripes_min);
2378 cpu->stripes_max = le32_to_cpu(disk->stripes_max);
2382 btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
2383 const struct btrfs_balance_args *cpu)
2385 memset(disk, 0, sizeof(*disk));
2387 disk->profiles = cpu_to_le64(cpu->profiles);
2388 disk->usage = cpu_to_le64(cpu->usage);
2389 disk->devid = cpu_to_le64(cpu->devid);
2390 disk->pstart = cpu_to_le64(cpu->pstart);
2391 disk->pend = cpu_to_le64(cpu->pend);
2392 disk->vstart = cpu_to_le64(cpu->vstart);
2393 disk->vend = cpu_to_le64(cpu->vend);
2394 disk->target = cpu_to_le64(cpu->target);
2395 disk->flags = cpu_to_le64(cpu->flags);
2396 disk->limit = cpu_to_le64(cpu->limit);
2397 disk->stripes_min = cpu_to_le32(cpu->stripes_min);
2398 disk->stripes_max = cpu_to_le32(cpu->stripes_max);
2401 /* struct btrfs_super_block */
2402 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
2403 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
2404 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
2406 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
2407 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
2408 struct btrfs_super_block, sys_chunk_array_size, 32);
2409 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
2410 struct btrfs_super_block, chunk_root_generation, 64);
2411 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
2413 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
2415 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
2416 chunk_root_level, 8);
2417 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
2419 BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
2420 log_root_transid, 64);
2421 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
2423 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
2425 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
2427 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
2429 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
2431 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
2433 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
2434 root_dir_objectid, 64);
2435 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
2437 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
2439 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
2440 compat_ro_flags, 64);
2441 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
2442 incompat_flags, 64);
2443 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
2445 BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
2446 cache_generation, 64);
2447 BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
2448 BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
2449 uuid_tree_generation, 64);
2451 static inline int btrfs_super_csum_size(const struct btrfs_super_block *s)
2453 u16 t = btrfs_super_csum_type(s);
2455 * csum type is validated at mount time
2457 return btrfs_csum_sizes[t];
2460 static inline const char *btrfs_super_csum_name(u16 csum_type)
2462 /* csum type is validated at mount time */
2463 return btrfs_csum_names[csum_type];
2467 * The leaf data grows from end-to-front in the node.
2468 * this returns the address of the start of the last item,
2469 * which is the stop of the leaf data stack
2471 static inline unsigned int leaf_data_end(const struct extent_buffer *leaf)
2473 u32 nr = btrfs_header_nritems(leaf);
2476 return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
2477 return btrfs_item_offset_nr(leaf, nr - 1);
2480 /* struct btrfs_file_extent_item */
2481 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
2482 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
2483 struct btrfs_file_extent_item, disk_bytenr, 64);
2484 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
2485 struct btrfs_file_extent_item, offset, 64);
2486 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
2487 struct btrfs_file_extent_item, generation, 64);
2488 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
2489 struct btrfs_file_extent_item, num_bytes, 64);
2490 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
2491 struct btrfs_file_extent_item, disk_num_bytes, 64);
2492 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
2493 struct btrfs_file_extent_item, compression, 8);
2495 static inline unsigned long
2496 btrfs_file_extent_inline_start(const struct btrfs_file_extent_item *e)
2498 return (unsigned long)e + BTRFS_FILE_EXTENT_INLINE_DATA_START;
2501 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2503 return BTRFS_FILE_EXTENT_INLINE_DATA_START + datasize;
2506 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
2508 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
2510 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
2511 disk_num_bytes, 64);
2512 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
2514 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
2516 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
2518 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
2520 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
2522 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
2523 other_encoding, 16);
2526 * this returns the number of bytes used by the item on disk, minus the
2527 * size of any extent headers. If a file is compressed on disk, this is
2528 * the compressed size
2530 static inline u32 btrfs_file_extent_inline_item_len(
2531 const struct extent_buffer *eb,
2532 struct btrfs_item *e)
2534 return btrfs_item_size(eb, e) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
2537 /* btrfs_dev_stats_item */
2538 static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
2539 const struct btrfs_dev_stats_item *ptr,
2544 read_extent_buffer(eb, &val,
2545 offsetof(struct btrfs_dev_stats_item, values) +
2546 ((unsigned long)ptr) + (index * sizeof(u64)),
2551 static inline void btrfs_set_dev_stats_value(struct extent_buffer *eb,
2552 struct btrfs_dev_stats_item *ptr,
2555 write_extent_buffer(eb, &val,
2556 offsetof(struct btrfs_dev_stats_item, values) +
2557 ((unsigned long)ptr) + (index * sizeof(u64)),
2561 /* btrfs_qgroup_status_item */
2562 BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
2564 BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
2566 BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
2568 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
2571 /* btrfs_qgroup_info_item */
2572 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
2574 BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
2575 BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
2577 BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
2578 BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
2581 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
2582 struct btrfs_qgroup_info_item, generation, 64);
2583 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
2585 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
2586 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
2587 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
2589 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
2590 struct btrfs_qgroup_info_item, excl_cmpr, 64);
2592 /* btrfs_qgroup_limit_item */
2593 BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
2595 BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
2597 BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
2599 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
2601 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
2604 /* btrfs_dev_replace_item */
2605 BTRFS_SETGET_FUNCS(dev_replace_src_devid,
2606 struct btrfs_dev_replace_item, src_devid, 64);
2607 BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
2608 struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
2610 BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
2612 BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
2614 BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
2616 BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
2617 num_write_errors, 64);
2618 BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
2619 struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
2621 BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
2623 BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
2626 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
2627 struct btrfs_dev_replace_item, src_devid, 64);
2628 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
2629 struct btrfs_dev_replace_item,
2630 cont_reading_from_srcdev_mode, 64);
2631 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
2632 struct btrfs_dev_replace_item, replace_state, 64);
2633 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
2634 struct btrfs_dev_replace_item, time_started, 64);
2635 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
2636 struct btrfs_dev_replace_item, time_stopped, 64);
2637 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
2638 struct btrfs_dev_replace_item, num_write_errors, 64);
2639 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
2640 struct btrfs_dev_replace_item,
2641 num_uncorrectable_read_errors, 64);
2642 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
2643 struct btrfs_dev_replace_item, cursor_left, 64);
2644 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
2645 struct btrfs_dev_replace_item, cursor_right, 64);
2647 /* helper function to cast into the data area of the leaf. */
2648 #define btrfs_item_ptr(leaf, slot, type) \
2649 ((type *)(BTRFS_LEAF_DATA_OFFSET + \
2650 btrfs_item_offset_nr(leaf, slot)))
2652 #define btrfs_item_ptr_offset(leaf, slot) \
2653 ((unsigned long)(BTRFS_LEAF_DATA_OFFSET + \
2654 btrfs_item_offset_nr(leaf, slot)))
2656 static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
2658 return crc32c(crc, address, length);
2661 static inline void btrfs_crc32c_final(u32 crc, u8 *result)
2663 put_unaligned_le32(~crc, result);
2666 static inline u64 btrfs_name_hash(const char *name, int len)
2668 return crc32c((u32)~1, name, len);
2672 * Figure the key offset of an extended inode ref
2674 static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
2677 return (u64) crc32c(parent_objectid, name, len);
2680 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
2682 return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2683 (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
2686 static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
2688 return mapping_gfp_constraint(mapping, ~__GFP_FS);
2693 enum btrfs_inline_ref_type {
2694 BTRFS_REF_TYPE_INVALID,
2695 BTRFS_REF_TYPE_BLOCK,
2696 BTRFS_REF_TYPE_DATA,
2700 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
2701 struct btrfs_extent_inline_ref *iref,
2702 enum btrfs_inline_ref_type is_data);
2704 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes);
2706 static inline u64 btrfs_calc_trans_metadata_size(struct btrfs_fs_info *fs_info,
2709 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
2713 * Doing a truncate won't result in new nodes or leaves, just what we need for
2716 static inline u64 btrfs_calc_trunc_metadata_size(struct btrfs_fs_info *fs_info,
2719 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
2722 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
2723 bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
2724 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
2726 void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg);
2727 bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2728 void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2729 void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg);
2730 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2731 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2732 unsigned long count);
2733 void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
2734 struct btrfs_delayed_ref_root *delayed_refs,
2735 struct btrfs_delayed_ref_head *head);
2736 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
2737 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2738 struct btrfs_fs_info *fs_info, u64 bytenr,
2739 u64 offset, int metadata, u64 *refs, u64 *flags);
2740 int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2741 u64 bytenr, u64 num, int reserved);
2742 int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
2743 u64 bytenr, u64 num_bytes);
2744 int btrfs_exclude_logged_extents(struct extent_buffer *eb);
2745 int btrfs_cross_ref_exist(struct btrfs_root *root,
2746 u64 objectid, u64 offset, u64 bytenr);
2747 struct btrfs_block_group_cache *btrfs_lookup_block_group(
2748 struct btrfs_fs_info *info,
2750 void btrfs_get_block_group(struct btrfs_block_group_cache *cache);
2751 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2752 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
2753 struct btrfs_root *root,
2754 u64 parent, u64 root_objectid,
2755 const struct btrfs_disk_key *key,
2756 int level, u64 hint,
2758 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2759 struct btrfs_root *root,
2760 struct extent_buffer *buf,
2761 u64 parent, int last_ref);
2762 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
2763 struct btrfs_root *root, u64 owner,
2764 u64 offset, u64 ram_bytes,
2765 struct btrfs_key *ins);
2766 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
2767 u64 root_objectid, u64 owner, u64 offset,
2768 struct btrfs_key *ins);
2769 int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
2770 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
2771 struct btrfs_key *ins, int is_data, int delalloc);
2772 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2773 struct extent_buffer *buf, int full_backref);
2774 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2775 struct extent_buffer *buf, int full_backref);
2776 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2777 u64 bytenr, u64 num_bytes, u64 flags,
2778 int level, int is_data);
2779 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
2781 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
2782 u64 start, u64 len, int delalloc);
2783 int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
2784 u64 start, u64 len);
2785 void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info);
2786 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
2787 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2788 struct btrfs_ref *generic_ref);
2790 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
2791 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
2792 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);
2793 int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);
2794 int btrfs_free_block_groups(struct btrfs_fs_info *info);
2795 int btrfs_read_block_groups(struct btrfs_fs_info *info);
2796 int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr);
2797 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2798 u64 bytes_used, u64 type, u64 chunk_offset,
2800 void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
2801 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
2802 struct btrfs_fs_info *fs_info,
2803 const u64 chunk_offset);
2804 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
2805 u64 group_start, struct extent_map *em);
2806 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
2807 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache);
2808 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *cache);
2809 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans);
2810 u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info);
2811 u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info);
2812 u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info);
2813 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2815 enum btrfs_reserve_flush_enum {
2816 /* If we are in the transaction, we can't flush anything.*/
2817 BTRFS_RESERVE_NO_FLUSH,
2819 * Flushing delalloc may cause deadlock somewhere, in this
2820 * case, use FLUSH LIMIT
2822 BTRFS_RESERVE_FLUSH_LIMIT,
2823 BTRFS_RESERVE_FLUSH_ALL,
2826 enum btrfs_flush_state {
2827 FLUSH_DELAYED_ITEMS_NR = 1,
2828 FLUSH_DELAYED_ITEMS = 2,
2829 FLUSH_DELAYED_REFS_NR = 3,
2830 FLUSH_DELAYED_REFS = 4,
2832 FLUSH_DELALLOC_WAIT = 6,
2834 ALLOC_CHUNK_FORCE = 8,
2838 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes);
2839 int btrfs_check_data_free_space(struct inode *inode,
2840 struct extent_changeset **reserved, u64 start, u64 len);
2841 void btrfs_free_reserved_data_space(struct inode *inode,
2842 struct extent_changeset *reserved, u64 start, u64 len);
2843 void btrfs_delalloc_release_space(struct inode *inode,
2844 struct extent_changeset *reserved,
2845 u64 start, u64 len, bool qgroup_free);
2846 void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
2848 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
2849 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
2850 struct btrfs_block_rsv *rsv,
2851 int nitems, bool use_global_rsv);
2852 void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
2853 struct btrfs_block_rsv *rsv);
2854 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
2857 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes);
2858 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
2860 int btrfs_delalloc_reserve_space(struct inode *inode,
2861 struct extent_changeset **reserved, u64 start, u64 len);
2862 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type);
2863 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
2864 unsigned short type);
2865 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
2866 struct btrfs_block_rsv *rsv,
2867 unsigned short type);
2868 void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
2869 struct btrfs_block_rsv *rsv);
2870 int btrfs_block_rsv_add(struct btrfs_root *root,
2871 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
2872 enum btrfs_reserve_flush_enum flush);
2873 int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor);
2874 int btrfs_block_rsv_refill(struct btrfs_root *root,
2875 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
2876 enum btrfs_reserve_flush_enum flush);
2877 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
2878 struct btrfs_block_rsv *dst_rsv, u64 num_bytes,
2880 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
2881 struct btrfs_block_rsv *dest, u64 num_bytes,
2883 void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
2884 struct btrfs_block_rsv *block_rsv,
2886 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
2887 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
2888 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
2889 enum btrfs_reserve_flush_enum flush);
2890 void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
2891 struct btrfs_block_rsv *src,
2893 int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache);
2894 void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache);
2895 void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
2896 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2897 int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
2898 u64 start, u64 end);
2899 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2900 u64 num_bytes, u64 *actual_bytes);
2901 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type);
2902 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
2904 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
2905 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2906 struct btrfs_fs_info *fs_info);
2907 int btrfs_start_write_no_snapshotting(struct btrfs_root *root);
2908 void btrfs_end_write_no_snapshotting(struct btrfs_root *root);
2909 void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
2910 void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type);
2911 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
2912 u64 start, u64 end);
2913 void btrfs_mark_bg_unused(struct btrfs_block_group_cache *bg);
2916 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
2917 int level, int *slot);
2918 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
2919 int btrfs_previous_item(struct btrfs_root *root,
2920 struct btrfs_path *path, u64 min_objectid,
2922 int btrfs_previous_extent_item(struct btrfs_root *root,
2923 struct btrfs_path *path, u64 min_objectid);
2924 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2925 struct btrfs_path *path,
2926 const struct btrfs_key *new_key);
2927 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
2928 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
2929 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
2930 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
2931 struct btrfs_key *key, int lowest_level,
2933 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
2934 struct btrfs_path *path,
2936 enum btrfs_compare_tree_result {
2937 BTRFS_COMPARE_TREE_NEW,
2938 BTRFS_COMPARE_TREE_DELETED,
2939 BTRFS_COMPARE_TREE_CHANGED,
2940 BTRFS_COMPARE_TREE_SAME,
2942 typedef int (*btrfs_changed_cb_t)(struct btrfs_path *left_path,
2943 struct btrfs_path *right_path,
2944 struct btrfs_key *key,
2945 enum btrfs_compare_tree_result result,
2947 int btrfs_compare_trees(struct btrfs_root *left_root,
2948 struct btrfs_root *right_root,
2949 btrfs_changed_cb_t cb, void *ctx);
2950 int btrfs_cow_block(struct btrfs_trans_handle *trans,
2951 struct btrfs_root *root, struct extent_buffer *buf,
2952 struct extent_buffer *parent, int parent_slot,
2953 struct extent_buffer **cow_ret);
2954 int btrfs_copy_root(struct btrfs_trans_handle *trans,
2955 struct btrfs_root *root,
2956 struct extent_buffer *buf,
2957 struct extent_buffer **cow_ret, u64 new_root_objectid);
2958 int btrfs_block_can_be_shared(struct btrfs_root *root,
2959 struct extent_buffer *buf);
2960 void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
2961 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
2962 int btrfs_split_item(struct btrfs_trans_handle *trans,
2963 struct btrfs_root *root,
2964 struct btrfs_path *path,
2965 const struct btrfs_key *new_key,
2966 unsigned long split_offset);
2967 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
2968 struct btrfs_root *root,
2969 struct btrfs_path *path,
2970 const struct btrfs_key *new_key);
2971 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2972 u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
2973 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2974 const struct btrfs_key *key, struct btrfs_path *p,
2975 int ins_len, int cow);
2976 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2977 struct btrfs_path *p, u64 time_seq);
2978 int btrfs_search_slot_for_read(struct btrfs_root *root,
2979 const struct btrfs_key *key,
2980 struct btrfs_path *p, int find_higher,
2982 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
2983 struct btrfs_root *root, struct extent_buffer *parent,
2984 int start_slot, u64 *last_ret,
2985 struct btrfs_key *progress);
2986 void btrfs_release_path(struct btrfs_path *p);
2987 struct btrfs_path *btrfs_alloc_path(void);
2988 void btrfs_free_path(struct btrfs_path *p);
2989 void btrfs_set_path_blocking(struct btrfs_path *p);
2990 void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
2992 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2993 struct btrfs_path *path, int slot, int nr);
2994 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
2995 struct btrfs_root *root,
2996 struct btrfs_path *path)
2998 return btrfs_del_items(trans, root, path, path->slots[0], 1);
3001 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
3002 const struct btrfs_key *cpu_key, u32 *data_size,
3003 u32 total_data, u32 total_size, int nr);
3004 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3005 const struct btrfs_key *key, void *data, u32 data_size);
3006 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3007 struct btrfs_root *root,
3008 struct btrfs_path *path,
3009 const struct btrfs_key *cpu_key, u32 *data_size,
3012 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
3013 struct btrfs_root *root,
3014 struct btrfs_path *path,
3015 const struct btrfs_key *key,
3018 return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
3021 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
3022 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
3023 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
3025 static inline int btrfs_next_old_item(struct btrfs_root *root,
3026 struct btrfs_path *p, u64 time_seq)
3029 if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
3030 return btrfs_next_old_leaf(root, p, time_seq);
3033 static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
3035 return btrfs_next_old_item(root, p, 0);
3037 int btrfs_leaf_free_space(struct extent_buffer *leaf);
3038 int __must_check btrfs_drop_snapshot(struct btrfs_root *root,
3039 struct btrfs_block_rsv *block_rsv,
3040 int update_ref, int for_reloc);
3041 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3042 struct btrfs_root *root,
3043 struct extent_buffer *node,
3044 struct extent_buffer *parent);
3045 static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
3048 * Do it this way so we only ever do one test_bit in the normal case.
3050 if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
3051 if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
3059 * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
3060 * anything except sleeping. This function is used to check the status of
3063 static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
3065 return fs_info->sb->s_flags & SB_RDONLY || btrfs_fs_closing(fs_info);
3068 static inline void free_fs_info(struct btrfs_fs_info *fs_info)
3070 kfree(fs_info->balance_ctl);
3071 kfree(fs_info->delayed_root);
3072 kfree(fs_info->extent_root);
3073 kfree(fs_info->tree_root);
3074 kfree(fs_info->chunk_root);
3075 kfree(fs_info->dev_root);
3076 kfree(fs_info->csum_root);
3077 kfree(fs_info->quota_root);
3078 kfree(fs_info->uuid_root);
3079 kfree(fs_info->free_space_root);
3080 kfree(fs_info->super_copy);
3081 kfree(fs_info->super_for_commit);
3085 /* tree mod log functions from ctree.c */
3086 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
3087 struct seq_list *elem);
3088 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
3089 struct seq_list *elem);
3090 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq);
3093 int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3094 u64 ref_id, u64 dirid, u64 sequence, const char *name,
3096 int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3097 u64 ref_id, u64 dirid, u64 *sequence, const char *name,
3099 int btrfs_del_root(struct btrfs_trans_handle *trans,
3100 const struct btrfs_key *key);
3101 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3102 const struct btrfs_key *key,
3103 struct btrfs_root_item *item);
3104 int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
3105 struct btrfs_root *root,
3106 struct btrfs_key *key,
3107 struct btrfs_root_item *item);
3108 int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
3109 struct btrfs_path *path, struct btrfs_root_item *root_item,
3110 struct btrfs_key *root_key);
3111 int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info);
3112 void btrfs_set_root_node(struct btrfs_root_item *item,
3113 struct extent_buffer *node);
3114 void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
3115 void btrfs_update_root_times(struct btrfs_trans_handle *trans,
3116 struct btrfs_root *root);
3119 int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
3121 int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
3123 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
3124 int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
3128 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
3129 const char *name, int name_len);
3130 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
3131 int name_len, struct btrfs_inode *dir,
3132 struct btrfs_key *location, u8 type, u64 index);
3133 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
3134 struct btrfs_root *root,
3135 struct btrfs_path *path, u64 dir,
3136 const char *name, int name_len,
3138 struct btrfs_dir_item *
3139 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
3140 struct btrfs_root *root,
3141 struct btrfs_path *path, u64 dir,
3142 u64 objectid, const char *name, int name_len,
3144 struct btrfs_dir_item *
3145 btrfs_search_dir_index_item(struct btrfs_root *root,
3146 struct btrfs_path *path, u64 dirid,
3147 const char *name, int name_len);
3148 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
3149 struct btrfs_root *root,
3150 struct btrfs_path *path,
3151 struct btrfs_dir_item *di);
3152 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root,
3154 struct btrfs_path *path, u64 objectid,
3155 const char *name, u16 name_len,
3156 const void *data, u16 data_len);
3157 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
3158 struct btrfs_root *root,
3159 struct btrfs_path *path, u64 dir,
3160 const char *name, u16 name_len,
3162 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
3163 struct btrfs_path *path,
3168 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
3169 struct btrfs_root *root, u64 offset);
3170 int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
3171 struct btrfs_root *root, u64 offset);
3172 int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
3175 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
3176 struct btrfs_root *root,
3177 const char *name, int name_len,
3178 u64 inode_objectid, u64 ref_objectid, u64 index);
3179 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
3180 struct btrfs_root *root,
3181 const char *name, int name_len,
3182 u64 inode_objectid, u64 ref_objectid, u64 *index);
3183 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
3184 struct btrfs_root *root,
3185 struct btrfs_path *path, u64 objectid);
3186 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
3187 *root, struct btrfs_path *path,
3188 struct btrfs_key *location, int mod);
3190 struct btrfs_inode_extref *
3191 btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 struct btrfs_path *path,
3194 const char *name, int name_len,
3195 u64 inode_objectid, u64 ref_objectid, int ins_len,
3198 int btrfs_find_name_in_backref(struct extent_buffer *leaf, int slot,
3200 int name_len, struct btrfs_inode_ref **ref_ret);
3201 int btrfs_find_name_in_ext_backref(struct extent_buffer *leaf, int slot,
3202 u64 ref_objectid, const char *name,
3204 struct btrfs_inode_extref **extref_ret);
3207 struct btrfs_dio_private;
3208 int btrfs_del_csums(struct btrfs_trans_handle *trans,
3209 struct btrfs_fs_info *fs_info, u64 bytenr, u64 len);
3210 blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio,
3212 blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio,
3213 u64 logical_offset);
3214 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
3215 struct btrfs_root *root,
3216 u64 objectid, u64 pos,
3217 u64 disk_offset, u64 disk_num_bytes,
3218 u64 num_bytes, u64 offset, u64 ram_bytes,
3219 u8 compression, u8 encryption, u16 other_encoding);
3220 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
3221 struct btrfs_root *root,
3222 struct btrfs_path *path, u64 objectid,
3223 u64 bytenr, int mod);
3224 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
3225 struct btrfs_root *root,
3226 struct btrfs_ordered_sum *sums);
3227 blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
3228 u64 file_start, int contig);
3229 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
3230 struct list_head *list, int search_commit);
3231 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
3232 const struct btrfs_path *path,
3233 struct btrfs_file_extent_item *fi,
3234 const bool new_inline,
3235 struct extent_map *em);
3238 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
3239 u64 start, u64 len);
3240 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
3241 u64 *orig_start, u64 *orig_block_len,
3244 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
3245 struct btrfs_inode *inode);
3246 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
3247 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
3248 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3249 struct btrfs_root *root,
3250 struct btrfs_inode *dir, struct btrfs_inode *inode,
3251 const char *name, int name_len);
3252 int btrfs_add_link(struct btrfs_trans_handle *trans,
3253 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
3254 const char *name, int name_len, int add_backref, u64 index);
3255 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
3256 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
3258 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3259 struct btrfs_root *root,
3260 struct inode *inode, u64 new_size,
3263 int btrfs_start_delalloc_snapshot(struct btrfs_root *root);
3264 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr);
3265 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
3266 unsigned int extra_bits,
3267 struct extent_state **cached_state, int dedupe);
3268 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
3269 struct btrfs_root *new_root,
3270 struct btrfs_root *parent_root,
3272 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
3274 void btrfs_clear_delalloc_extent(struct inode *inode,
3275 struct extent_state *state, unsigned *bits);
3276 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
3277 struct extent_state *other);
3278 void btrfs_split_delalloc_extent(struct inode *inode,