btrfs: improve messages when updating feature flags
[linux/fpc-iii.git] / fs / btrfs / ctree.h
blob5e89e4d5b06534f4be70d580f4581876bf8cba4a
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
6 #ifndef BTRFS_CTREE_H
7 #define BTRFS_CTREE_H
9 #include <linux/mm.h>
10 #include <linux/sched/signal.h>
11 #include <linux/highmem.h>
12 #include <linux/fs.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;
45 struct btrfs_ref;
47 #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
50 * Maximum number of mirrors that can be available for all profiles counting
51 * the target device of dev-replace as one. During an active device replace
52 * procedure, the target device of the copy operation is a mirror for the
53 * filesystem data as well that can be used to read data in order to repair
54 * read errors on other disks.
56 * Current value is derived from RAID1 with 2 copies.
58 #define BTRFS_MAX_MIRRORS (2 + 1)
60 #define BTRFS_MAX_LEVEL 8
62 #define BTRFS_OLDEST_GENERATION 0ULL
65 * the max metadata block size. This limit is somewhat artificial,
66 * but the memmove costs go through the roof for larger blocks.
68 #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
71 * we can actually store much bigger names, but lets not confuse the rest
72 * of linux
74 #define BTRFS_NAME_LEN 255
77 * Theoretical limit is larger, but we keep this down to a sane
78 * value. That should limit greatly the possibility of collisions on
79 * inode ref items.
81 #define BTRFS_LINK_MAX 65535U
83 /* four bytes for CRC32 */
84 static const int btrfs_csum_sizes[] = { 4 };
85 static const char *btrfs_csum_names[] = { "crc32c" };
87 #define BTRFS_EMPTY_DIR_SIZE 0
89 /* ioprio of readahead is set to idle */
90 #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
92 #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
95 * Use large batch size to reduce overhead of metadata updates. On the reader
96 * side, we only read it when we are close to ENOSPC and the read overhead is
97 * mostly related to the number of CPUs, so it is OK to use arbitrary large
98 * value here.
100 #define BTRFS_TOTAL_BYTES_PINNED_BATCH SZ_128M
102 #define BTRFS_MAX_EXTENT_SIZE SZ_128M
106 * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
108 static inline u32 count_max_extents(u64 size)
110 return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
113 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
115 BUG_ON(num_stripes == 0);
116 return sizeof(struct btrfs_chunk) +
117 sizeof(struct btrfs_stripe) * (num_stripes - 1);
121 * Runtime (in-memory) states of filesystem
123 enum {
124 /* Global indicator of serious filesystem errors */
125 BTRFS_FS_STATE_ERROR,
127 * Filesystem is being remounted, allow to skip some operations, like
128 * defrag
130 BTRFS_FS_STATE_REMOUNTING,
131 /* Track if a transaction abort has been reported on this filesystem */
132 BTRFS_FS_STATE_TRANS_ABORTED,
134 * Bio operations should be blocked on this filesystem because a source
135 * or target device is being destroyed as part of a device replace
137 BTRFS_FS_STATE_DEV_REPLACING,
138 /* The btrfs_fs_info created for self-tests */
139 BTRFS_FS_STATE_DUMMY_FS_INFO,
142 #define BTRFS_BACKREF_REV_MAX 256
143 #define BTRFS_BACKREF_REV_SHIFT 56
144 #define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
145 BTRFS_BACKREF_REV_SHIFT)
147 #define BTRFS_OLD_BACKREF_REV 0
148 #define BTRFS_MIXED_BACKREF_REV 1
151 * every tree block (leaf or node) starts with this header.
153 struct btrfs_header {
154 /* these first four must match the super block */
155 u8 csum[BTRFS_CSUM_SIZE];
156 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
157 __le64 bytenr; /* which block this node is supposed to live in */
158 __le64 flags;
160 /* allowed to be different from the super from here on down */
161 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
162 __le64 generation;
163 __le64 owner;
164 __le32 nritems;
165 u8 level;
166 } __attribute__ ((__packed__));
169 * this is a very generous portion of the super block, giving us
170 * room to translate 14 chunks with 3 stripes each.
172 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
175 * just in case we somehow lose the roots and are not able to mount,
176 * we store an array of the roots from previous transactions
177 * in the super.
179 #define BTRFS_NUM_BACKUP_ROOTS 4
180 struct btrfs_root_backup {
181 __le64 tree_root;
182 __le64 tree_root_gen;
184 __le64 chunk_root;
185 __le64 chunk_root_gen;
187 __le64 extent_root;
188 __le64 extent_root_gen;
190 __le64 fs_root;
191 __le64 fs_root_gen;
193 __le64 dev_root;
194 __le64 dev_root_gen;
196 __le64 csum_root;
197 __le64 csum_root_gen;
199 __le64 total_bytes;
200 __le64 bytes_used;
201 __le64 num_devices;
202 /* future */
203 __le64 unused_64[4];
205 u8 tree_root_level;
206 u8 chunk_root_level;
207 u8 extent_root_level;
208 u8 fs_root_level;
209 u8 dev_root_level;
210 u8 csum_root_level;
211 /* future and to align */
212 u8 unused_8[10];
213 } __attribute__ ((__packed__));
216 * the super block basically lists the main trees of the FS
217 * it currently lacks any block count etc etc
219 struct btrfs_super_block {
220 /* the first 4 fields must match struct btrfs_header */
221 u8 csum[BTRFS_CSUM_SIZE];
222 /* FS specific UUID, visible to user */
223 u8 fsid[BTRFS_FSID_SIZE];
224 __le64 bytenr; /* this block number */
225 __le64 flags;
227 /* allowed to be different from the btrfs_header from here own down */
228 __le64 magic;
229 __le64 generation;
230 __le64 root;
231 __le64 chunk_root;
232 __le64 log_root;
234 /* this will help find the new super based on the log root */
235 __le64 log_root_transid;
236 __le64 total_bytes;
237 __le64 bytes_used;
238 __le64 root_dir_objectid;
239 __le64 num_devices;
240 __le32 sectorsize;
241 __le32 nodesize;
242 __le32 __unused_leafsize;
243 __le32 stripesize;
244 __le32 sys_chunk_array_size;
245 __le64 chunk_root_generation;
246 __le64 compat_flags;
247 __le64 compat_ro_flags;
248 __le64 incompat_flags;
249 __le16 csum_type;
250 u8 root_level;
251 u8 chunk_root_level;
252 u8 log_root_level;
253 struct btrfs_dev_item dev_item;
255 char label[BTRFS_LABEL_SIZE];
257 __le64 cache_generation;
258 __le64 uuid_tree_generation;
260 /* the UUID written into btree blocks */
261 u8 metadata_uuid[BTRFS_FSID_SIZE];
263 /* future expansion */
264 __le64 reserved[28];
265 u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
266 struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
267 } __attribute__ ((__packed__));
270 * Compat flags that we support. If any incompat flags are set other than the
271 * ones specified below then we will fail to mount
273 #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
274 #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
275 #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
277 #define BTRFS_FEATURE_COMPAT_RO_SUPP \
278 (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
279 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID)
281 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
282 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
284 #define BTRFS_FEATURE_INCOMPAT_SUPP \
285 (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
286 BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
287 BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
288 BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
289 BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
290 BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
291 BTRFS_FEATURE_INCOMPAT_RAID56 | \
292 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
293 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
294 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
295 BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
297 #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
298 (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
299 #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
302 * A leaf is full of items. offset and size tell us where to find
303 * the item in the leaf (relative to the start of the data area)
305 struct btrfs_item {
306 struct btrfs_disk_key key;
307 __le32 offset;
308 __le32 size;
309 } __attribute__ ((__packed__));
312 * leaves have an item area and a data area:
313 * [item0, item1....itemN] [free space] [dataN...data1, data0]
315 * The data is separate from the items to get the keys closer together
316 * during searches.
318 struct btrfs_leaf {
319 struct btrfs_header header;
320 struct btrfs_item items[];
321 } __attribute__ ((__packed__));
324 * all non-leaf blocks are nodes, they hold only keys and pointers to
325 * other blocks
327 struct btrfs_key_ptr {
328 struct btrfs_disk_key key;
329 __le64 blockptr;
330 __le64 generation;
331 } __attribute__ ((__packed__));
333 struct btrfs_node {
334 struct btrfs_header header;
335 struct btrfs_key_ptr ptrs[];
336 } __attribute__ ((__packed__));
339 * btrfs_paths remember the path taken from the root down to the leaf.
340 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
341 * to any other levels that are present.
343 * The slots array records the index of the item or block pointer
344 * used while walking the tree.
346 enum { READA_NONE, READA_BACK, READA_FORWARD };
347 struct btrfs_path {
348 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
349 int slots[BTRFS_MAX_LEVEL];
350 /* if there is real range locking, this locks field will change */
351 u8 locks[BTRFS_MAX_LEVEL];
352 u8 reada;
353 /* keep some upper locks as we walk down */
354 u8 lowest_level;
357 * set by btrfs_split_item, tells search_slot to keep all locks
358 * and to force calls to keep space in the nodes
360 unsigned int search_for_split:1;
361 unsigned int keep_locks:1;
362 unsigned int skip_locking:1;
363 unsigned int leave_spinning:1;
364 unsigned int search_commit_root:1;
365 unsigned int need_commit_sem:1;
366 unsigned int skip_release_on_error:1;
368 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
369 sizeof(struct btrfs_item))
370 struct btrfs_dev_replace {
371 u64 replace_state; /* see #define above */
372 time64_t time_started; /* seconds since 1-Jan-1970 */
373 time64_t time_stopped; /* seconds since 1-Jan-1970 */
374 atomic64_t num_write_errors;
375 atomic64_t num_uncorrectable_read_errors;
377 u64 cursor_left;
378 u64 committed_cursor_left;
379 u64 cursor_left_last_write_of_item;
380 u64 cursor_right;
382 u64 cont_reading_from_srcdev_mode; /* see #define above */
384 int is_valid;
385 int item_needs_writeback;
386 struct btrfs_device *srcdev;
387 struct btrfs_device *tgtdev;
389 struct mutex lock_finishing_cancel_unmount;
390 struct rw_semaphore rwsem;
392 struct btrfs_scrub_progress scrub_progress;
394 struct percpu_counter bio_counter;
395 wait_queue_head_t replace_wait;
398 /* For raid type sysfs entries */
399 struct raid_kobject {
400 u64 flags;
401 struct kobject kobj;
402 struct list_head list;
405 struct btrfs_space_info {
406 spinlock_t lock;
408 u64 total_bytes; /* total bytes in the space,
409 this doesn't take mirrors into account */
410 u64 bytes_used; /* total bytes used,
411 this doesn't take mirrors into account */
412 u64 bytes_pinned; /* total bytes pinned, will be freed when the
413 transaction finishes */
414 u64 bytes_reserved; /* total bytes the allocator has reserved for
415 current allocations */
416 u64 bytes_may_use; /* number of bytes that may be used for
417 delalloc/allocations */
418 u64 bytes_readonly; /* total bytes that are read only */
420 u64 max_extent_size; /* This will hold the maximum extent size of
421 the space info if we had an ENOSPC in the
422 allocator. */
424 unsigned int full:1; /* indicates that we cannot allocate any more
425 chunks for this space */
426 unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
428 unsigned int flush:1; /* set if we are trying to make space */
430 unsigned int force_alloc; /* set if we need to force a chunk
431 alloc for this space */
433 u64 disk_used; /* total bytes used on disk */
434 u64 disk_total; /* total bytes on disk, takes mirrors into
435 account */
437 u64 flags;
440 * bytes_pinned is kept in line with what is actually pinned, as in
441 * we've called update_block_group and dropped the bytes_used counter
442 * and increased the bytes_pinned counter. However this means that
443 * bytes_pinned does not reflect the bytes that will be pinned once the
444 * delayed refs are flushed, so this counter is inc'ed every time we
445 * call btrfs_free_extent so it is a realtime count of what will be
446 * freed once the transaction is committed. It will be zeroed every
447 * time the transaction commits.
449 struct percpu_counter total_bytes_pinned;
451 struct list_head list;
452 /* Protected by the spinlock 'lock'. */
453 struct list_head ro_bgs;
454 struct list_head priority_tickets;
455 struct list_head tickets;
457 * tickets_id just indicates the next ticket will be handled, so note
458 * it's not stored per ticket.
460 u64 tickets_id;
462 struct rw_semaphore groups_sem;
463 /* for block groups in our same type */
464 struct list_head block_groups[BTRFS_NR_RAID_TYPES];
465 wait_queue_head_t wait;
467 struct kobject kobj;
468 struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
472 * Types of block reserves
474 enum {
475 BTRFS_BLOCK_RSV_GLOBAL,
476 BTRFS_BLOCK_RSV_DELALLOC,
477 BTRFS_BLOCK_RSV_TRANS,
478 BTRFS_BLOCK_RSV_CHUNK,
479 BTRFS_BLOCK_RSV_DELOPS,
480 BTRFS_BLOCK_RSV_DELREFS,
481 BTRFS_BLOCK_RSV_EMPTY,
482 BTRFS_BLOCK_RSV_TEMP,
485 struct btrfs_block_rsv {
486 u64 size;
487 u64 reserved;
488 struct btrfs_space_info *space_info;
489 spinlock_t lock;
490 unsigned short full;
491 unsigned short type;
492 unsigned short failfast;
495 * Qgroup equivalent for @size @reserved
497 * Unlike normal @size/@reserved for inode rsv, qgroup doesn't care
498 * about things like csum size nor how many tree blocks it will need to
499 * reserve.
501 * Qgroup cares more about net change of the extent usage.
503 * So for one newly inserted file extent, in worst case it will cause
504 * leaf split and level increase, nodesize for each file extent is
505 * already too much.
507 * In short, qgroup_size/reserved is the upper limit of possible needed
508 * qgroup metadata reservation.
510 u64 qgroup_rsv_size;
511 u64 qgroup_rsv_reserved;
515 * free clusters are used to claim free space in relatively large chunks,
516 * allowing us to do less seeky writes. They are used for all metadata
517 * allocations. In ssd_spread mode they are also used for data allocations.
519 struct btrfs_free_cluster {
520 spinlock_t lock;
521 spinlock_t refill_lock;
522 struct rb_root root;
524 /* largest extent in this cluster */
525 u64 max_size;
527 /* first extent starting offset */
528 u64 window_start;
530 /* We did a full search and couldn't create a cluster */
531 bool fragmented;
533 struct btrfs_block_group_cache *block_group;
535 * when a cluster is allocated from a block group, we put the
536 * cluster onto a list in the block group so that it can
537 * be freed before the block group is freed.
539 struct list_head block_group_list;
542 enum btrfs_caching_type {
543 BTRFS_CACHE_NO,
544 BTRFS_CACHE_STARTED,
545 BTRFS_CACHE_FAST,
546 BTRFS_CACHE_FINISHED,
547 BTRFS_CACHE_ERROR,
550 enum btrfs_disk_cache_state {
551 BTRFS_DC_WRITTEN,
552 BTRFS_DC_ERROR,
553 BTRFS_DC_CLEAR,
554 BTRFS_DC_SETUP,
557 struct btrfs_caching_control {
558 struct list_head list;
559 struct mutex mutex;
560 wait_queue_head_t wait;
561 struct btrfs_work work;
562 struct btrfs_block_group_cache *block_group;
563 u64 progress;
564 refcount_t count;
567 /* Once caching_thread() finds this much free space, it will wake up waiters. */
568 #define CACHING_CTL_WAKE_UP SZ_2M
570 struct btrfs_io_ctl {
571 void *cur, *orig;
572 struct page *page;
573 struct page **pages;
574 struct btrfs_fs_info *fs_info;
575 struct inode *inode;
576 unsigned long size;
577 int index;
578 int num_pages;
579 int entries;
580 int bitmaps;
581 unsigned check_crcs:1;
585 * Tree to record all locked full stripes of a RAID5/6 block group
587 struct btrfs_full_stripe_locks_tree {
588 struct rb_root root;
589 struct mutex lock;
592 struct btrfs_block_group_cache {
593 struct btrfs_key key;
594 struct btrfs_block_group_item item;
595 struct btrfs_fs_info *fs_info;
596 struct inode *inode;
597 spinlock_t lock;
598 u64 pinned;
599 u64 reserved;
600 u64 delalloc_bytes;
601 u64 bytes_super;
602 u64 flags;
603 u64 cache_generation;
606 * If the free space extent count exceeds this number, convert the block
607 * group to bitmaps.
609 u32 bitmap_high_thresh;
612 * If the free space extent count drops below this number, convert the
613 * block group back to extents.
615 u32 bitmap_low_thresh;
618 * It is just used for the delayed data space allocation because
619 * only the data space allocation and the relative metadata update
620 * can be done cross the transaction.
622 struct rw_semaphore data_rwsem;
624 /* for raid56, this is a full stripe, without parity */
625 unsigned long full_stripe_len;
627 unsigned int ro;
628 unsigned int iref:1;
629 unsigned int has_caching_ctl:1;
630 unsigned int removed:1;
632 int disk_cache_state;
634 /* cache tracking stuff */
635 int cached;
636 struct btrfs_caching_control *caching_ctl;
637 u64 last_byte_to_unpin;
639 struct btrfs_space_info *space_info;
641 /* free space cache stuff */
642 struct btrfs_free_space_ctl *free_space_ctl;
644 /* block group cache stuff */
645 struct rb_node cache_node;
647 /* for block groups in the same raid type */
648 struct list_head list;
650 /* usage count */
651 atomic_t count;
653 /* List of struct btrfs_free_clusters for this block group.
654 * Today it will only have one thing on it, but that may change
656 struct list_head cluster_list;
658 /* For delayed block group creation or deletion of empty block groups */
659 struct list_head bg_list;
661 /* For read-only block groups */
662 struct list_head ro_list;
664 atomic_t trimming;
666 /* For dirty block groups */
667 struct list_head dirty_list;
668 struct list_head io_list;
670 struct btrfs_io_ctl io_ctl;
673 * Incremented when doing extent allocations and holding a read lock
674 * on the space_info's groups_sem semaphore.
675 * Decremented when an ordered extent that represents an IO against this
676 * block group's range is created (after it's added to its inode's
677 * root's list of ordered extents) or immediately after the allocation
678 * if it's a metadata extent or fallocate extent (for these cases we
679 * don't create ordered extents).
681 atomic_t reservations;
684 * Incremented while holding the spinlock *lock* by a task checking if
685 * it can perform a nocow write (incremented if the value for the *ro*
686 * field is 0). Decremented by such tasks once they create an ordered
687 * extent or before that if some error happens before reaching that step.
688 * This is to prevent races between block group relocation and nocow
689 * writes through direct IO.
691 atomic_t nocow_writers;
693 /* Lock for free space tree operations. */
694 struct mutex free_space_lock;
697 * Does the block group need to be added to the free space tree?
698 * Protected by free_space_lock.
700 int needs_free_space;
702 /* Record locked full stripes for RAID5/6 block group */
703 struct btrfs_full_stripe_locks_tree full_stripe_locks_root;
706 /* delayed seq elem */
707 struct seq_list {
708 struct list_head list;
709 u64 seq;
712 #define SEQ_LIST_INIT(name) { .list = LIST_HEAD_INIT((name).list), .seq = 0 }
714 #define SEQ_LAST ((u64)-1)
716 enum btrfs_orphan_cleanup_state {
717 ORPHAN_CLEANUP_STARTED = 1,
718 ORPHAN_CLEANUP_DONE = 2,
721 /* used by the raid56 code to lock stripes for read/modify/write */
722 struct btrfs_stripe_hash {
723 struct list_head hash_list;
724 spinlock_t lock;
727 /* used by the raid56 code to lock stripes for read/modify/write */
728 struct btrfs_stripe_hash_table {
729 struct list_head stripe_cache;
730 spinlock_t cache_lock;
731 int cache_size;
732 struct btrfs_stripe_hash table[];
735 #define BTRFS_STRIPE_HASH_TABLE_BITS 11
737 void btrfs_init_async_reclaim_work(struct work_struct *work);
739 /* fs_info */
740 struct reloc_control;
741 struct btrfs_device;
742 struct btrfs_fs_devices;
743 struct btrfs_balance_control;
744 struct btrfs_delayed_root;
747 * Block group or device which contains an active swapfile. Used for preventing
748 * unsafe operations while a swapfile is active.
750 * These are sorted on (ptr, inode) (note that a block group or device can
751 * contain more than one swapfile). We compare the pointer values because we
752 * don't actually care what the object is, we just need a quick check whether
753 * the object exists in the rbtree.
755 struct btrfs_swapfile_pin {
756 struct rb_node node;
757 void *ptr;
758 struct inode *inode;
760 * If true, ptr points to a struct btrfs_block_group_cache. Otherwise,
761 * ptr points to a struct btrfs_device.
763 bool is_block_group;
766 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
768 enum {
769 BTRFS_FS_BARRIER,
770 BTRFS_FS_CLOSING_START,
771 BTRFS_FS_CLOSING_DONE,
772 BTRFS_FS_LOG_RECOVERING,
773 BTRFS_FS_OPEN,
774 BTRFS_FS_QUOTA_ENABLED,
775 BTRFS_FS_UPDATE_UUID_TREE_GEN,
776 BTRFS_FS_CREATING_FREE_SPACE_TREE,
777 BTRFS_FS_BTREE_ERR,
778 BTRFS_FS_LOG1_ERR,
779 BTRFS_FS_LOG2_ERR,
780 BTRFS_FS_QUOTA_OVERRIDE,
781 /* Used to record internally whether fs has been frozen */
782 BTRFS_FS_FROZEN,
784 * Indicate that a whole-filesystem exclusive operation is running
785 * (device replace, resize, device add/delete, balance)
787 BTRFS_FS_EXCL_OP,
789 * To info transaction_kthread we need an immediate commit so it
790 * doesn't need to wait for commit_interval
792 BTRFS_FS_NEED_ASYNC_COMMIT,
794 * Indicate that balance has been set up from the ioctl and is in the
795 * main phase. The fs_info::balance_ctl is initialized.
796 * Set and cleared while holding fs_info::balance_mutex.
798 BTRFS_FS_BALANCE_RUNNING,
800 /* Indicate that the cleaner thread is awake and doing something. */
801 BTRFS_FS_CLEANER_RUNNING,
804 * The checksumming has an optimized version and is considered fast,
805 * so we don't need to offload checksums to workqueues.
807 BTRFS_FS_CSUM_IMPL_FAST,
810 struct btrfs_fs_info {
811 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
812 unsigned long flags;
813 struct btrfs_root *extent_root;
814 struct btrfs_root *tree_root;
815 struct btrfs_root *chunk_root;
816 struct btrfs_root *dev_root;
817 struct btrfs_root *fs_root;
818 struct btrfs_root *csum_root;
819 struct btrfs_root *quota_root;
820 struct btrfs_root *uuid_root;
821 struct btrfs_root *free_space_root;
823 /* the log root tree is a directory of all the other log roots */
824 struct btrfs_root *log_root_tree;
826 spinlock_t fs_roots_radix_lock;
827 struct radix_tree_root fs_roots_radix;
829 /* block group cache stuff */
830 spinlock_t block_group_cache_lock;
831 u64 first_logical_byte;
832 struct rb_root block_group_cache_tree;
834 /* keep track of unallocated space */
835 atomic64_t free_chunk_space;
837 struct extent_io_tree freed_extents[2];
838 struct extent_io_tree *pinned_extents;
840 /* logical->physical extent mapping */
841 struct extent_map_tree mapping_tree;
844 * block reservation for extent, checksum, root tree and
845 * delayed dir index item
847 struct btrfs_block_rsv global_block_rsv;
848 /* block reservation for metadata operations */
849 struct btrfs_block_rsv trans_block_rsv;
850 /* block reservation for chunk tree */
851 struct btrfs_block_rsv chunk_block_rsv;
852 /* block reservation for delayed operations */
853 struct btrfs_block_rsv delayed_block_rsv;
854 /* block reservation for delayed refs */
855 struct btrfs_block_rsv delayed_refs_rsv;
857 struct btrfs_block_rsv empty_block_rsv;
859 u64 generation;
860 u64 last_trans_committed;
861 u64 avg_delayed_ref_runtime;
864 * this is updated to the current trans every time a full commit
865 * is required instead of the faster short fsync log commits
867 u64 last_trans_log_full_commit;
868 unsigned long mount_opt;
870 * Track requests for actions that need to be done during transaction
871 * commit (like for some mount options).
873 unsigned long pending_changes;
874 unsigned long compress_type:4;
875 unsigned int compress_level;
876 u32 commit_interval;
878 * It is a suggestive number, the read side is safe even it gets a
879 * wrong number because we will write out the data into a regular
880 * extent. The write side(mount/remount) is under ->s_umount lock,
881 * so it is also safe.
883 u64 max_inline;
885 struct btrfs_transaction *running_transaction;
886 wait_queue_head_t transaction_throttle;
887 wait_queue_head_t transaction_wait;
888 wait_queue_head_t transaction_blocked_wait;
889 wait_queue_head_t async_submit_wait;
892 * Used to protect the incompat_flags, compat_flags, compat_ro_flags
893 * when they are updated.
895 * Because we do not clear the flags for ever, so we needn't use
896 * the lock on the read side.
898 * We also needn't use the lock when we mount the fs, because
899 * there is no other task which will update the flag.
901 spinlock_t super_lock;
902 struct btrfs_super_block *super_copy;
903 struct btrfs_super_block *super_for_commit;
904 struct super_block *sb;
905 struct inode *btree_inode;
906 struct mutex tree_log_mutex;
907 struct mutex transaction_kthread_mutex;
908 struct mutex cleaner_mutex;
909 struct mutex chunk_mutex;
912 * this is taken to make sure we don't set block groups ro after
913 * the free space cache has been allocated on them
915 struct mutex ro_block_group_mutex;
917 /* this is used during read/modify/write to make sure
918 * no two ios are trying to mod the same stripe at the same
919 * time
921 struct btrfs_stripe_hash_table *stripe_hash_table;
924 * this protects the ordered operations list only while we are
925 * processing all of the entries on it. This way we make
926 * sure the commit code doesn't find the list temporarily empty
927 * because another function happens to be doing non-waiting preflush
928 * before jumping into the main commit.
930 struct mutex ordered_operations_mutex;
932 struct rw_semaphore commit_root_sem;
934 struct rw_semaphore cleanup_work_sem;
936 struct rw_semaphore subvol_sem;
937 struct srcu_struct subvol_srcu;
939 spinlock_t trans_lock;
941 * the reloc mutex goes with the trans lock, it is taken
942 * during commit to protect us from the relocation code
944 struct mutex reloc_mutex;
946 struct list_head trans_list;
947 struct list_head dead_roots;
948 struct list_head caching_block_groups;
950 spinlock_t delayed_iput_lock;
951 struct list_head delayed_iputs;
952 atomic_t nr_delayed_iputs;
953 wait_queue_head_t delayed_iputs_wait;
955 /* this protects tree_mod_seq_list */
956 spinlock_t tree_mod_seq_lock;
957 atomic64_t tree_mod_seq;
958 struct list_head tree_mod_seq_list;
960 /* this protects tree_mod_log */
961 rwlock_t tree_mod_log_lock;
962 struct rb_root tree_mod_log;
964 atomic_t async_delalloc_pages;
967 * this is used to protect the following list -- ordered_roots.
969 spinlock_t ordered_root_lock;
972 * all fs/file tree roots in which there are data=ordered extents
973 * pending writeback are added into this list.
975 * these can span multiple transactions and basically include
976 * every dirty data page that isn't from nodatacow
978 struct list_head ordered_roots;
980 struct mutex delalloc_root_mutex;
981 spinlock_t delalloc_root_lock;
982 /* all fs/file tree roots that have delalloc inodes. */
983 struct list_head delalloc_roots;
986 * there is a pool of worker threads for checksumming during writes
987 * and a pool for checksumming after reads. This is because readers
988 * can run with FS locks held, and the writers may be waiting for
989 * those locks. We don't want ordering in the pending list to cause
990 * deadlocks, and so the two are serviced separately.
992 * A third pool does submit_bio to avoid deadlocking with the other
993 * two
995 struct btrfs_workqueue *workers;
996 struct btrfs_workqueue *delalloc_workers;
997 struct btrfs_workqueue *flush_workers;
998 struct btrfs_workqueue *endio_workers;
999 struct btrfs_workqueue *endio_meta_workers;
1000 struct btrfs_workqueue *endio_raid56_workers;
1001 struct btrfs_workqueue *endio_repair_workers;
1002 struct btrfs_workqueue *rmw_workers;
1003 struct btrfs_workqueue *endio_meta_write_workers;
1004 struct btrfs_workqueue *endio_write_workers;
1005 struct btrfs_workqueue *endio_freespace_worker;
1006 struct btrfs_workqueue *submit_workers;
1007 struct btrfs_workqueue *caching_workers;
1008 struct btrfs_workqueue *readahead_workers;
1011 * fixup workers take dirty pages that didn't properly go through
1012 * the cow mechanism and make them safe to write. It happens
1013 * for the sys_munmap function call path
1015 struct btrfs_workqueue *fixup_workers;
1016 struct btrfs_workqueue *delayed_workers;
1018 /* the extent workers do delayed refs on the extent allocation tree */
1019 struct btrfs_workqueue *extent_workers;
1020 struct task_struct *transaction_kthread;
1021 struct task_struct *cleaner_kthread;
1022 u32 thread_pool_size;
1024 struct kobject *space_info_kobj;
1025 struct list_head pending_raid_kobjs;
1026 spinlock_t pending_raid_kobjs_lock; /* uncontended */
1028 u64 total_pinned;
1030 /* used to keep from writing metadata until there is a nice batch */
1031 struct percpu_counter dirty_metadata_bytes;
1032 struct percpu_counter delalloc_bytes;
1033 struct percpu_counter dio_bytes;
1034 s32 dirty_metadata_batch;
1035 s32 delalloc_batch;
1037 struct list_head dirty_cowonly_roots;
1039 struct btrfs_fs_devices *fs_devices;
1042 * The space_info list is effectively read only after initial
1043 * setup. It is populated at mount time and cleaned up after
1044 * all block groups are removed. RCU is used to protect it.
1046 struct list_head space_info;
1048 struct btrfs_space_info *data_sinfo;
1050 struct reloc_control *reloc_ctl;
1052 /* data_alloc_cluster is only used in ssd_spread mode */
1053 struct btrfs_free_cluster data_alloc_cluster;
1055 /* all metadata allocations go through this cluster */
1056 struct btrfs_free_cluster meta_alloc_cluster;
1058 /* auto defrag inodes go here */
1059 spinlock_t defrag_inodes_lock;
1060 struct rb_root defrag_inodes;
1061 atomic_t defrag_running;
1063 /* Used to protect avail_{data, metadata, system}_alloc_bits */
1064 seqlock_t profiles_lock;
1066 * these three are in extended format (availability of single
1067 * chunks is denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other
1068 * types are denoted by corresponding BTRFS_BLOCK_GROUP_* bits)
1070 u64 avail_data_alloc_bits;
1071 u64 avail_metadata_alloc_bits;
1072 u64 avail_system_alloc_bits;
1074 /* restriper state */
1075 spinlock_t balance_lock;
1076 struct mutex balance_mutex;
1077 atomic_t balance_pause_req;
1078 atomic_t balance_cancel_req;
1079 struct btrfs_balance_control *balance_ctl;
1080 wait_queue_head_t balance_wait_q;
1082 u32 data_chunk_allocations;
1083 u32 metadata_ratio;
1085 void *bdev_holder;
1087 /* private scrub information */
1088 struct mutex scrub_lock;
1089 atomic_t scrubs_running;
1090 atomic_t scrub_pause_req;
1091 atomic_t scrubs_paused;
1092 atomic_t scrub_cancel_req;
1093 wait_queue_head_t scrub_pause_wait;
1095 * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
1096 * running.
1098 refcount_t scrub_workers_refcnt;
1099 struct btrfs_workqueue *scrub_workers;
1100 struct btrfs_workqueue *scrub_wr_completion_workers;
1101 struct btrfs_workqueue *scrub_parity_workers;
1103 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1104 u32 check_integrity_print_mask;
1105 #endif
1106 /* is qgroup tracking in a consistent state? */
1107 u64 qgroup_flags;
1109 /* holds configuration and tracking. Protected by qgroup_lock */
1110 struct rb_root qgroup_tree;
1111 spinlock_t qgroup_lock;
1114 * used to avoid frequently calling ulist_alloc()/ulist_free()
1115 * when doing qgroup accounting, it must be protected by qgroup_lock.
1117 struct ulist *qgroup_ulist;
1119 /* protect user change for quota operations */
1120 struct mutex qgroup_ioctl_lock;
1122 /* list of dirty qgroups to be written at next commit */
1123 struct list_head dirty_qgroups;
1125 /* used by qgroup for an efficient tree traversal */
1126 u64 qgroup_seq;
1128 /* qgroup rescan items */
1129 struct mutex qgroup_rescan_lock; /* protects the progress item */
1130 struct btrfs_key qgroup_rescan_progress;
1131 struct btrfs_workqueue *qgroup_rescan_workers;
1132 struct completion qgroup_rescan_completion;
1133 struct btrfs_work qgroup_rescan_work;
1134 bool qgroup_rescan_running; /* protected by qgroup_rescan_lock */
1136 /* filesystem state */
1137 unsigned long fs_state;
1139 struct btrfs_delayed_root *delayed_root;
1141 /* readahead tree */
1142 spinlock_t reada_lock;
1143 struct radix_tree_root reada_tree;
1145 /* readahead works cnt */
1146 atomic_t reada_works_cnt;
1148 /* Extent buffer radix tree */
1149 spinlock_t buffer_lock;
1150 struct radix_tree_root buffer_radix;
1152 /* next backup root to be overwritten */
1153 int backup_root_index;
1155 /* device replace state */
1156 struct btrfs_dev_replace dev_replace;
1158 struct semaphore uuid_tree_rescan_sem;
1160 /* Used to reclaim the metadata space in the background. */
1161 struct work_struct async_reclaim_work;
1163 spinlock_t unused_bgs_lock;
1164 struct list_head unused_bgs;
1165 struct mutex unused_bg_unpin_mutex;
1166 struct mutex delete_unused_bgs_mutex;
1168 /* Cached block sizes */
1169 u32 nodesize;
1170 u32 sectorsize;
1171 u32 stripesize;
1173 /* Block groups and devices containing active swapfiles. */
1174 spinlock_t swapfile_pins_lock;
1175 struct rb_root swapfile_pins;
1177 struct crypto_shash *csum_shash;
1180 * Number of send operations in progress.
1181 * Updated while holding fs_info::balance_mutex.
1183 int send_in_progress;
1185 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1186 spinlock_t ref_verify_lock;
1187 struct rb_root block_tree;
1188 #endif
1191 static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
1193 return sb->s_fs_info;
1196 struct btrfs_subvolume_writers {
1197 struct percpu_counter counter;
1198 wait_queue_head_t wait;
1202 * The state of btrfs root
1204 enum {
1206 * btrfs_record_root_in_trans is a multi-step process, and it can race
1207 * with the balancing code. But the race is very small, and only the
1208 * first time the root is added to each transaction. So IN_TRANS_SETUP
1209 * is used to tell us when more checks are required
1211 BTRFS_ROOT_IN_TRANS_SETUP,
1212 BTRFS_ROOT_REF_COWS,
1213 BTRFS_ROOT_TRACK_DIRTY,
1214 BTRFS_ROOT_IN_RADIX,
1215 BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
1216 BTRFS_ROOT_DEFRAG_RUNNING,
1217 BTRFS_ROOT_FORCE_COW,
1218 BTRFS_ROOT_MULTI_LOG_TASKS,
1219 BTRFS_ROOT_DIRTY,
1220 BTRFS_ROOT_DELETING,
1223 * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
1225 * Set for the subvolume tree owning the reloc tree.
1227 BTRFS_ROOT_DEAD_RELOC_TREE,
1228 /* Mark dead root stored on device whose cleanup needs to be resumed */
1229 BTRFS_ROOT_DEAD_TREE,
1233 * Record swapped tree blocks of a subvolume tree for delayed subtree trace
1234 * code. For detail check comment in fs/btrfs/qgroup.c.
1236 struct btrfs_qgroup_swapped_blocks {
1237 spinlock_t lock;
1238 /* RM_EMPTY_ROOT() of above blocks[] */
1239 bool swapped;
1240 struct rb_root blocks[BTRFS_MAX_LEVEL];
1244 * in ram representation of the tree. extent_root is used for all allocations
1245 * and for the extent tree extent_root root.
1247 struct btrfs_root {
1248 struct extent_buffer *node;
1250 struct extent_buffer *commit_root;
1251 struct btrfs_root *log_root;
1252 struct btrfs_root *reloc_root;
1254 unsigned long state;
1255 struct btrfs_root_item root_item;
1256 struct btrfs_key root_key;
1257 struct btrfs_fs_info *fs_info;
1258 struct extent_io_tree dirty_log_pages;
1260 struct mutex objectid_mutex;
1262 spinlock_t accounting_lock;
1263 struct btrfs_block_rsv *block_rsv;
1265 /* free ino cache stuff */
1266 struct btrfs_free_space_ctl *free_ino_ctl;
1267 enum btrfs_caching_type ino_cache_state;
1268 spinlock_t ino_cache_lock;
1269 wait_queue_head_t ino_cache_wait;
1270 struct btrfs_free_space_ctl *free_ino_pinned;
1271 u64 ino_cache_progress;
1272 struct inode *ino_cache_inode;
1274 struct mutex log_mutex;
1275 wait_queue_head_t log_writer_wait;
1276 wait_queue_head_t log_commit_wait[2];
1277 struct list_head log_ctxs[2];
1278 atomic_t log_writers;
1279 atomic_t log_commit[2];
1280 atomic_t log_batch;
1281 int log_transid;
1282 /* No matter the commit succeeds or not*/
1283 int log_transid_committed;
1284 /* Just be updated when the commit succeeds. */
1285 int last_log_commit;
1286 pid_t log_start_pid;
1288 u64 last_trans;
1290 u32 type;
1292 u64 highest_objectid;
1294 u64 defrag_trans_start;
1295 struct btrfs_key defrag_progress;
1296 struct btrfs_key defrag_max;
1298 /* the dirty list is only used by non-reference counted roots */
1299 struct list_head dirty_list;
1301 struct list_head root_list;
1303 spinlock_t log_extents_lock[2];
1304 struct list_head logged_list[2];
1306 int orphan_cleanup_state;
1308 spinlock_t inode_lock;
1309 /* red-black tree that keeps track of in-memory inodes */
1310 struct rb_root inode_tree;
1313 * radix tree that keeps track of delayed nodes of every inode,
1314 * protected by inode_lock
1316 struct radix_tree_root delayed_nodes_tree;
1318 * right now this just gets used so that a root has its own devid
1319 * for stat. It may be used for more later
1321 dev_t anon_dev;
1323 spinlock_t root_item_lock;
1324 refcount_t refs;
1326 struct mutex delalloc_mutex;
1327 spinlock_t delalloc_lock;
1329 * all of the inodes that have delalloc bytes. It is possible for
1330 * this list to be empty even when there is still dirty data=ordered
1331 * extents waiting to finish IO.
1333 struct list_head delalloc_inodes;
1334 struct list_head delalloc_root;
1335 u64 nr_delalloc_inodes;
1337 struct mutex ordered_extent_mutex;
1339 * this is used by the balancing code to wait for all the pending
1340 * ordered extents
1342 spinlock_t ordered_extent_lock;
1345 * all of the data=ordered extents pending writeback
1346 * these can span multiple transactions and basically include
1347 * every dirty data page that isn't from nodatacow
1349 struct list_head ordered_extents;
1350 struct list_head ordered_root;
1351 u64 nr_ordered_extents;
1354 * Not empty if this subvolume root has gone through tree block swap
1355 * (relocation)
1357 * Will be used by reloc_control::dirty_subvol_roots.
1359 struct list_head reloc_dirty_list;
1362 * Number of currently running SEND ioctls to prevent
1363 * manipulation with the read-only status via SUBVOL_SETFLAGS
1365 int send_in_progress;
1367 * Number of currently running deduplication operations that have a
1368 * destination inode belonging to this root. Protected by the lock
1369 * root_item_lock.
1371 int dedupe_in_progress;
1372 struct btrfs_subvolume_writers *subv_writers;
1373 atomic_t will_be_snapshotted;
1374 atomic_t snapshot_force_cow;
1376 /* For qgroup metadata reserved space */
1377 spinlock_t qgroup_meta_rsv_lock;
1378 u64 qgroup_meta_rsv_pertrans;
1379 u64 qgroup_meta_rsv_prealloc;
1381 /* Number of active swapfiles */
1382 atomic_t nr_swapfiles;
1384 /* Record pairs of swapped blocks for qgroup */
1385 struct btrfs_qgroup_swapped_blocks swapped_blocks;
1387 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1388 u64 alloc_bytenr;
1389 #endif
1392 struct btrfs_file_private {
1393 void *filldir_buf;
1396 static inline u32 btrfs_inode_sectorsize(const struct inode *inode)
1398 return btrfs_sb(inode->i_sb)->sectorsize;
1401 static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
1404 return info->nodesize - sizeof(struct btrfs_header);
1407 #define BTRFS_LEAF_DATA_OFFSET offsetof(struct btrfs_leaf, items)
1409 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
1411 return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
1414 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
1416 return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
1419 #define BTRFS_FILE_EXTENT_INLINE_DATA_START \
1420 (offsetof(struct btrfs_file_extent_item, disk_bytenr))
1421 static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
1423 return BTRFS_MAX_ITEM_SIZE(info) -
1424 BTRFS_FILE_EXTENT_INLINE_DATA_START;
1427 static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1429 return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
1433 * Flags for mount options.
1435 * Note: don't forget to add new options to btrfs_show_options()
1437 #define BTRFS_MOUNT_NODATASUM (1 << 0)
1438 #define BTRFS_MOUNT_NODATACOW (1 << 1)
1439 #define BTRFS_MOUNT_NOBARRIER (1 << 2)
1440 #define BTRFS_MOUNT_SSD (1 << 3)
1441 #define BTRFS_MOUNT_DEGRADED (1 << 4)
1442 #define BTRFS_MOUNT_COMPRESS (1 << 5)
1443 #define BTRFS_MOUNT_NOTREELOG (1 << 6)
1444 #define BTRFS_MOUNT_FLUSHONCOMMIT (1 << 7)
1445 #define BTRFS_MOUNT_SSD_SPREAD (1 << 8)
1446 #define BTRFS_MOUNT_NOSSD (1 << 9)
1447 #define BTRFS_MOUNT_DISCARD (1 << 10)
1448 #define BTRFS_MOUNT_FORCE_COMPRESS (1 << 11)
1449 #define BTRFS_MOUNT_SPACE_CACHE (1 << 12)
1450 #define BTRFS_MOUNT_CLEAR_CACHE (1 << 13)
1451 #define BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED (1 << 14)
1452 #define BTRFS_MOUNT_ENOSPC_DEBUG (1 << 15)
1453 #define BTRFS_MOUNT_AUTO_DEFRAG (1 << 16)
1454 #define BTRFS_MOUNT_INODE_MAP_CACHE (1 << 17)
1455 #define BTRFS_MOUNT_USEBACKUPROOT (1 << 18)
1456 #define BTRFS_MOUNT_SKIP_BALANCE (1 << 19)
1457 #define BTRFS_MOUNT_CHECK_INTEGRITY (1 << 20)
1458 #define BTRFS_MOUNT_CHECK_INTEGRITY_INCLUDING_EXTENT_DATA (1 << 21)
1459 #define BTRFS_MOUNT_PANIC_ON_FATAL_ERROR (1 << 22)
1460 #define BTRFS_MOUNT_RESCAN_UUID_TREE (1 << 23)
1461 #define BTRFS_MOUNT_FRAGMENT_DATA (1 << 24)
1462 #define BTRFS_MOUNT_FRAGMENT_METADATA (1 << 25)
1463 #define BTRFS_MOUNT_FREE_SPACE_TREE (1 << 26)
1464 #define BTRFS_MOUNT_NOLOGREPLAY (1 << 27)
1465 #define BTRFS_MOUNT_REF_VERIFY (1 << 28)
1467 #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
1468 #define BTRFS_DEFAULT_MAX_INLINE (2048)
1470 #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
1471 #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
1472 #define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
1473 #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
1474 BTRFS_MOUNT_##opt)
1476 #define btrfs_set_and_info(fs_info, opt, fmt, args...) \
1478 if (!btrfs_test_opt(fs_info, opt)) \
1479 btrfs_info(fs_info, fmt, ##args); \
1480 btrfs_set_opt(fs_info->mount_opt, opt); \
1483 #define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
1485 if (btrfs_test_opt(fs_info, opt)) \
1486 btrfs_info(fs_info, fmt, ##args); \
1487 btrfs_clear_opt(fs_info->mount_opt, opt); \
1490 #ifdef CONFIG_BTRFS_DEBUG
1491 static inline int
1492 btrfs_should_fragment_free_space(struct btrfs_block_group_cache *block_group)
1494 struct btrfs_fs_info *fs_info = block_group->fs_info;
1496 return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
1497 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
1498 (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
1499 block_group->flags & BTRFS_BLOCK_GROUP_DATA);
1501 #endif
1504 * Requests for changes that need to be done during transaction commit.
1506 * Internal mount options that are used for special handling of the real
1507 * mount options (eg. cannot be set during remount and have to be set during
1508 * transaction commit)
1511 #define BTRFS_PENDING_SET_INODE_MAP_CACHE (0)
1512 #define BTRFS_PENDING_CLEAR_INODE_MAP_CACHE (1)
1513 #define BTRFS_PENDING_COMMIT (2)
1515 #define btrfs_test_pending(info, opt) \
1516 test_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1517 #define btrfs_set_pending(info, opt) \
1518 set_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1519 #define btrfs_clear_pending(info, opt) \
1520 clear_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
1523 * Helpers for setting pending mount option changes.
1525 * Expects corresponding macros
1526 * BTRFS_PENDING_SET_ and CLEAR_ + short mount option name
1528 #define btrfs_set_pending_and_info(info, opt, fmt, args...) \
1529 do { \
1530 if (!btrfs_raw_test_opt((info)->mount_opt, opt)) { \
1531 btrfs_info((info), fmt, ##args); \
1532 btrfs_set_pending((info), SET_##opt); \
1533 btrfs_clear_pending((info), CLEAR_##opt); \
1535 } while(0)
1537 #define btrfs_clear_pending_and_info(info, opt, fmt, args...) \
1538 do { \
1539 if (btrfs_raw_test_opt((info)->mount_opt, opt)) { \
1540 btrfs_info((info), fmt, ##args); \
1541 btrfs_set_pending((info), CLEAR_##opt); \
1542 btrfs_clear_pending((info), SET_##opt); \
1544 } while(0)
1547 * Inode flags
1549 #define BTRFS_INODE_NODATASUM (1 << 0)
1550 #define BTRFS_INODE_NODATACOW (1 << 1)
1551 #define BTRFS_INODE_READONLY (1 << 2)
1552 #define BTRFS_INODE_NOCOMPRESS (1 << 3)
1553 #define BTRFS_INODE_PREALLOC (1 << 4)
1554 #define BTRFS_INODE_SYNC (1 << 5)
1555 #define BTRFS_INODE_IMMUTABLE (1 << 6)
1556 #define BTRFS_INODE_APPEND (1 << 7)
1557 #define BTRFS_INODE_NODUMP (1 << 8)
1558 #define BTRFS_INODE_NOATIME (1 << 9)
1559 #define BTRFS_INODE_DIRSYNC (1 << 10)
1560 #define BTRFS_INODE_COMPRESS (1 << 11)
1562 #define BTRFS_INODE_ROOT_ITEM_INIT (1 << 31)
1564 #define BTRFS_INODE_FLAG_MASK \
1565 (BTRFS_INODE_NODATASUM | \
1566 BTRFS_INODE_NODATACOW | \
1567 BTRFS_INODE_READONLY | \
1568 BTRFS_INODE_NOCOMPRESS | \
1569 BTRFS_INODE_PREALLOC | \
1570 BTRFS_INODE_SYNC | \
1571 BTRFS_INODE_IMMUTABLE | \
1572 BTRFS_INODE_APPEND | \
1573 BTRFS_INODE_NODUMP | \
1574 BTRFS_INODE_NOATIME | \
1575 BTRFS_INODE_DIRSYNC | \
1576 BTRFS_INODE_COMPRESS | \
1577 BTRFS_INODE_ROOT_ITEM_INIT)
1579 struct btrfs_map_token {
1580 const struct extent_buffer *eb;
1581 char *kaddr;
1582 unsigned long offset;
1585 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
1586 ((bytes) >> (fs_info)->sb->s_blocksize_bits)
1588 static inline void btrfs_init_map_token (struct btrfs_map_token *token)
1590 token->kaddr = NULL;
1593 /* some macros to generate set/get functions for the struct fields. This
1594 * assumes there is a lefoo_to_cpu for every type, so lets make a simple
1595 * one for u8:
1597 #define le8_to_cpu(v) (v)
1598 #define cpu_to_le8(v) (v)
1599 #define __le8 u8
1601 #define read_eb_member(eb, ptr, type, member, result) (\
1602 read_extent_buffer(eb, (char *)(result), \
1603 ((unsigned long)(ptr)) + \
1604 offsetof(type, member), \
1605 sizeof(((type *)0)->member)))
1607 #define write_eb_member(eb, ptr, type, member, result) (\
1608 write_extent_buffer(eb, (char *)(result), \
1609 ((unsigned long)(ptr)) + \
1610 offsetof(type, member), \
1611 sizeof(((type *)0)->member)))
1613 #define DECLARE_BTRFS_SETGET_BITS(bits) \
1614 u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
1615 const void *ptr, unsigned long off, \
1616 struct btrfs_map_token *token); \
1617 void btrfs_set_token_##bits(struct extent_buffer *eb, const void *ptr, \
1618 unsigned long off, u##bits val, \
1619 struct btrfs_map_token *token); \
1620 static inline u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
1621 const void *ptr, \
1622 unsigned long off) \
1624 return btrfs_get_token_##bits(eb, ptr, off, NULL); \
1626 static inline void btrfs_set_##bits(struct extent_buffer *eb, void *ptr,\
1627 unsigned long off, u##bits val) \
1629 btrfs_set_token_##bits(eb, ptr, off, val, NULL); \
1632 DECLARE_BTRFS_SETGET_BITS(8)
1633 DECLARE_BTRFS_SETGET_BITS(16)
1634 DECLARE_BTRFS_SETGET_BITS(32)
1635 DECLARE_BTRFS_SETGET_BITS(64)
1637 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
1638 static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
1639 const type *s) \
1641 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1642 return btrfs_get_##bits(eb, s, offsetof(type, member)); \
1644 static inline void btrfs_set_##name(struct extent_buffer *eb, type *s, \
1645 u##bits val) \
1647 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1648 btrfs_set_##bits(eb, s, offsetof(type, member), val); \
1650 static inline u##bits btrfs_token_##name(const struct extent_buffer *eb,\
1651 const type *s, \
1652 struct btrfs_map_token *token) \
1654 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1655 return btrfs_get_token_##bits(eb, s, offsetof(type, member), token); \
1657 static inline void btrfs_set_token_##name(struct extent_buffer *eb, \
1658 type *s, u##bits val, \
1659 struct btrfs_map_token *token) \
1661 BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
1662 btrfs_set_token_##bits(eb, s, offsetof(type, member), val, token); \
1665 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
1666 static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
1668 const type *p = page_address(eb->pages[0]); \
1669 u##bits res = le##bits##_to_cpu(p->member); \
1670 return res; \
1672 static inline void btrfs_set_##name(struct extent_buffer *eb, \
1673 u##bits val) \
1675 type *p = page_address(eb->pages[0]); \
1676 p->member = cpu_to_le##bits(val); \
1679 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
1680 static inline u##bits btrfs_##name(const type *s) \
1682 return le##bits##_to_cpu(s->member); \
1684 static inline void btrfs_set_##name(type *s, u##bits val) \
1686 s->member = cpu_to_le##bits(val); \
1690 static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb,
1691 struct btrfs_dev_item *s)
1693 BUILD_BUG_ON(sizeof(u64) !=
1694 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1695 return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
1696 total_bytes));
1698 static inline void btrfs_set_device_total_bytes(struct extent_buffer *eb,
1699 struct btrfs_dev_item *s,
1700 u64 val)
1702 BUILD_BUG_ON(sizeof(u64) !=
1703 sizeof(((struct btrfs_dev_item *)0))->total_bytes);
1704 WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
1705 btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
1709 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
1710 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
1711 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
1712 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
1713 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
1714 start_offset, 64);
1715 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
1716 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
1717 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
1718 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
1719 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
1720 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
1722 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
1723 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
1724 total_bytes, 64);
1725 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
1726 bytes_used, 64);
1727 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
1728 io_align, 32);
1729 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
1730 io_width, 32);
1731 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
1732 sector_size, 32);
1733 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
1734 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
1735 dev_group, 32);
1736 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
1737 seek_speed, 8);
1738 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
1739 bandwidth, 8);
1740 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
1741 generation, 64);
1743 static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
1745 return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
1748 static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
1750 return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
1753 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
1754 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
1755 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
1756 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
1757 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
1758 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
1759 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
1760 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
1761 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
1762 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
1763 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
1765 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
1767 return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
1770 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
1771 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
1772 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
1773 stripe_len, 64);
1774 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
1775 io_align, 32);
1776 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
1777 io_width, 32);
1778 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
1779 sector_size, 32);
1780 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
1781 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
1782 num_stripes, 16);
1783 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
1784 sub_stripes, 16);
1785 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
1786 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
1788 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
1789 int nr)
1791 unsigned long offset = (unsigned long)c;
1792 offset += offsetof(struct btrfs_chunk, stripe);
1793 offset += nr * sizeof(struct btrfs_stripe);
1794 return (struct btrfs_stripe *)offset;
1797 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
1799 return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1802 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
1803 struct btrfs_chunk *c, int nr)
1805 return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1808 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
1809 struct btrfs_chunk *c, int nr)
1811 return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1814 /* struct btrfs_block_group_item */
1815 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
1816 used, 64);
1817 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
1818 used, 64);
1819 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
1820 struct btrfs_block_group_item, chunk_objectid, 64);
1822 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
1823 struct btrfs_block_group_item, chunk_objectid, 64);
1824 BTRFS_SETGET_FUNCS(disk_block_group_flags,
1825 struct btrfs_block_group_item, flags, 64);
1826 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
1827 struct btrfs_block_group_item, flags, 64);
1829 /* struct btrfs_free_space_info */
1830 BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
1831 extent_count, 32);
1832 BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
1834 /* struct btrfs_inode_ref */
1835 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1836 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
1838 /* struct btrfs_inode_extref */
1839 BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
1840 parent_objectid, 64);
1841 BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
1842 name_len, 16);
1843 BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
1845 /* struct btrfs_inode_item */
1846 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
1847 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
1848 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
1849 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
1850 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
1851 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1852 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1853 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1854 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1855 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
1856 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
1857 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
1858 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1859 generation, 64);
1860 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1861 sequence, 64);
1862 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1863 transid, 64);
1864 BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1865 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1866 nbytes, 64);
1867 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1868 block_group, 64);
1869 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1870 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1871 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1872 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1873 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1874 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
1875 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1876 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1877 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1878 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
1880 /* struct btrfs_dev_extent */
1881 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1882 chunk_tree, 64);
1883 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1884 chunk_objectid, 64);
1885 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1886 chunk_offset, 64);
1887 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1889 static inline unsigned long btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1891 unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1892 return (unsigned long)dev + ptr;
1895 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1896 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1897 generation, 64);
1898 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
1900 BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
1903 BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1905 static inline void btrfs_tree_block_key(struct extent_buffer *eb,
1906 struct btrfs_tree_block_info *item,
1907 struct btrfs_disk_key *key)
1909 read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1912 static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
1913 struct btrfs_tree_block_info *item,
1914 struct btrfs_disk_key *key)
1916 write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1919 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
1920 root, 64);
1921 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
1922 objectid, 64);
1923 BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
1924 offset, 64);
1925 BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
1926 count, 32);
1928 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
1929 count, 32);
1931 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
1932 type, 8);
1933 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
1934 offset, 64);
1936 static inline u32 btrfs_extent_inline_ref_size(int type)
1938 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1939 type == BTRFS_SHARED_BLOCK_REF_KEY)
1940 return sizeof(struct btrfs_extent_inline_ref);
1941 if (type == BTRFS_SHARED_DATA_REF_KEY)
1942 return sizeof(struct btrfs_shared_data_ref) +
1943 sizeof(struct btrfs_extent_inline_ref);
1944 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1945 return sizeof(struct btrfs_extent_data_ref) +
1946 offsetof(struct btrfs_extent_inline_ref, offset);
1947 return 0;
1950 BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
1951 BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
1952 generation, 64);
1953 BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
1954 BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
1956 /* struct btrfs_node */
1957 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1958 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1959 BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr,
1960 blockptr, 64);
1961 BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
1962 generation, 64);
1964 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
1966 unsigned long ptr;
1967 ptr = offsetof(struct btrfs_node, ptrs) +
1968 sizeof(struct btrfs_key_ptr) * nr;
1969 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1972 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
1973 int nr, u64 val)
1975 unsigned long ptr;
1976 ptr = offsetof(struct btrfs_node, ptrs) +
1977 sizeof(struct btrfs_key_ptr) * nr;
1978 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1981 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
1983 unsigned long ptr;
1984 ptr = offsetof(struct btrfs_node, ptrs) +
1985 sizeof(struct btrfs_key_ptr) * nr;
1986 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1989 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1990 int nr, u64 val)
1992 unsigned long ptr;
1993 ptr = offsetof(struct btrfs_node, ptrs) +
1994 sizeof(struct btrfs_key_ptr) * nr;
1995 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1998 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
2000 return offsetof(struct btrfs_node, ptrs) +
2001 sizeof(struct btrfs_key_ptr) * nr;
2004 void btrfs_node_key(const struct extent_buffer *eb,
2005 struct btrfs_disk_key *disk_key, int nr);
2007 static inline void btrfs_set_node_key(struct extent_buffer *eb,
2008 struct btrfs_disk_key *disk_key, int nr)
2010 unsigned long ptr;
2011 ptr = btrfs_node_key_ptr_offset(nr);
2012 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
2013 struct btrfs_key_ptr, key, disk_key);
2016 /* struct btrfs_item */
2017 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
2018 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
2019 BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
2020 BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
2022 static inline unsigned long btrfs_item_nr_offset(int nr)
2024 return offsetof(struct btrfs_leaf, items) +
2025 sizeof(struct btrfs_item) * nr;
2028 static inline struct btrfs_item *btrfs_item_nr(int nr)
2030 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
2033 static inline u32 btrfs_item_end(const struct extent_buffer *eb,
2034 struct btrfs_item *item)
2036 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
2039 static inline u32 btrfs_item_end_nr(const struct extent_buffer *eb, int nr)
2041 return btrfs_item_end(eb, btrfs_item_nr(nr));
2044 static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
2046 return btrfs_item_offset(eb, btrfs_item_nr(nr));
2049 static inline u32 btrfs_item_size_nr(const struct extent_buffer *eb, int nr)
2051 return btrfs_item_size(eb, btrfs_item_nr(nr));
2054 static inline void btrfs_item_key(const struct extent_buffer *eb,
2055 struct btrfs_disk_key *disk_key, int nr)
2057 struct btrfs_item *item = btrfs_item_nr(nr);
2058 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
2061 static inline void btrfs_set_item_key(struct extent_buffer *eb,
2062 struct btrfs_disk_key *disk_key, int nr)
2064 struct btrfs_item *item = btrfs_item_nr(nr);
2065 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
2068 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
2071 * struct btrfs_root_ref
2073 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
2074 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
2075 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
2077 /* struct btrfs_dir_item */
2078 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
2079 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
2080 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
2081 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
2082 BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
2083 BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item,
2084 data_len, 16);
2085 BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item,
2086 name_len, 16);
2087 BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item,
2088 transid, 64);
2090 static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
2091 const struct btrfs_dir_item *item,
2092 struct btrfs_disk_key *key)
2094 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
2097 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
2098 struct btrfs_dir_item *item,
2099 const struct btrfs_disk_key *key)
2101 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
2104 BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
2105 num_entries, 64);
2106 BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
2107 num_bitmaps, 64);
2108 BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
2109 generation, 64);
2111 static inline void btrfs_free_space_key(const struct extent_buffer *eb,
2112 const struct btrfs_free_space_header *h,
2113 struct btrfs_disk_key *key)
2115 read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2118 static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
2119 struct btrfs_free_space_header *h,
2120 const struct btrfs_disk_key *key)
2122 write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
2125 /* struct btrfs_disk_key */
2126 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
2127 objectid, 64);
2128 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
2129 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
2131 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
2132 const struct btrfs_disk_key *disk)
2134 cpu->offset = le64_to_cpu(disk->offset);
2135 cpu->type = disk->type;
2136 cpu->objectid = le64_to_cpu(disk->objectid);
2139 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
2140 const struct btrfs_key *cpu)
2142 disk->offset = cpu_to_le64(cpu->offset);
2143 disk->type = cpu->type;
2144 disk->objectid = cpu_to_le64(cpu->objectid);
2147 static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
2148 struct btrfs_key *key, int nr)
2150 struct btrfs_disk_key disk_key;
2151 btrfs_node_key(eb, &disk_key, nr);
2152 btrfs_disk_key_to_cpu(key, &disk_key);
2155 static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
2156 struct btrfs_key *key, int nr)
2158 struct btrfs_disk_key disk_key;
2159 btrfs_item_key(eb, &disk_key, nr);
2160 btrfs_disk_key_to_cpu(key, &disk_key);
2163 static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
2164 const struct btrfs_dir_item *item,
2165 struct btrfs_key *key)
2167 struct btrfs_disk_key disk_key;
2168 btrfs_dir_item_key(eb, item, &disk_key);
2169 btrfs_disk_key_to_cpu(key, &disk_key);
2172 static inline u8 btrfs_key_type(const struct btrfs_key *key)
2174 return key->type;
2177 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
2179 key->type = val;
2182 /* struct btrfs_header */
2183 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
2184 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
2185 generation, 64);
2186 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
2187 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
2188 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
2189 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
2190 BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
2191 generation, 64);
2192 BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
2193 BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header,
2194 nritems, 32);
2195 BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
2197 static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
2199 return (btrfs_header_flags(eb) & flag) == flag;
2202 static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
2204 u64 flags = btrfs_header_flags(eb);
2205 btrfs_set_header_flags(eb, flags | flag);
2208 static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
2210 u64 flags = btrfs_header_flags(eb);
2211 btrfs_set_header_flags(eb, flags & ~flag);
2214 static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
2216 u64 flags = btrfs_header_flags(eb);
2217 return flags >> BTRFS_BACKREF_REV_SHIFT;
2220 static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
2221 int rev)
2223 u64 flags = btrfs_header_flags(eb);
2224 flags &= ~BTRFS_BACKREF_REV_MASK;
2225 flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
2226 btrfs_set_header_flags(eb, flags);
2229 static inline unsigned long btrfs_header_fsid(void)
2231 return offsetof(struct btrfs_header, fsid);
2234 static inline unsigned long btrfs_header_chunk_tree_uuid(const struct extent_buffer *eb)
2236 return offsetof(struct btrfs_header, chunk_tree_uuid);
2239 static inline int btrfs_is_leaf(const struct extent_buffer *eb)
2241 return btrfs_header_level(eb) == 0;
2244 /* struct btrfs_root_item */
2245 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
2246 generation, 64);
2247 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
2248 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
2249 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
2251 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
2252 generation, 64);
2253 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
2254 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
2255 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
2256 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
2257 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
2258 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
2259 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
2260 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
2261 last_snapshot, 64);
2262 BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
2263 generation_v2, 64);
2264 BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
2265 ctransid, 64);
2266 BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
2267 otransid, 64);
2268 BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
2269 stransid, 64);
2270 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
2271 rtransid, 64);
2273 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
2275 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
2278 static inline bool btrfs_root_dead(const struct btrfs_root *root)
2280 return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
2283 /* struct btrfs_root_backup */
2284 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
2285 tree_root, 64);
2286 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
2287 tree_root_gen, 64);
2288 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
2289 tree_root_level, 8);
2291 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
2292 chunk_root, 64);
2293 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
2294 chunk_root_gen, 64);
2295 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
2296 chunk_root_level, 8);
2298 BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
2299 extent_root, 64);
2300 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
2301 extent_root_gen, 64);
2302 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
2303 extent_root_level, 8);
2305 BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
2306 fs_root, 64);
2307 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
2308 fs_root_gen, 64);
2309 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
2310 fs_root_level, 8);
2312 BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
2313 dev_root, 64);
2314 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
2315 dev_root_gen, 64);
2316 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
2317 dev_root_level, 8);
2319 BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
2320 csum_root, 64);
2321 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
2322 csum_root_gen, 64);
2323 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
2324 csum_root_level, 8);
2325 BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
2326 total_bytes, 64);
2327 BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
2328 bytes_used, 64);
2329 BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
2330 num_devices, 64);
2332 /* struct btrfs_balance_item */
2333 BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
2335 static inline void btrfs_balance_data(const struct extent_buffer *eb,
2336 const struct btrfs_balance_item *bi,
2337 struct btrfs_disk_balance_args *ba)
2339 read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2342 static inline void btrfs_set_balance_data(struct extent_buffer *eb,
2343 struct btrfs_balance_item *bi,
2344 const struct btrfs_disk_balance_args *ba)
2346 write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
2349 static inline void btrfs_balance_meta(const struct extent_buffer *eb,
2350 const struct btrfs_balance_item *bi,
2351 struct btrfs_disk_balance_args *ba)
2353 read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2356 static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
2357 struct btrfs_balance_item *bi,
2358 const struct btrfs_disk_balance_args *ba)
2360 write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
2363 static inline void btrfs_balance_sys(const struct extent_buffer *eb,
2364 const struct btrfs_balance_item *bi,
2365 struct btrfs_disk_balance_args *ba)
2367 read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2370 static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
2371 struct btrfs_balance_item *bi,
2372 const struct btrfs_disk_balance_args *ba)
2374 write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
2377 static inline void
2378 btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
2379 const struct btrfs_disk_balance_args *disk)
2381 memset(cpu, 0, sizeof(*cpu));
2383 cpu->profiles = le64_to_cpu(disk->profiles);
2384 cpu->usage = le64_to_cpu(disk->usage);
2385 cpu->devid = le64_to_cpu(disk->devid);
2386 cpu->pstart = le64_to_cpu(disk->pstart);
2387 cpu->pend = le64_to_cpu(disk->pend);
2388 cpu->vstart = le64_to_cpu(disk->vstart);
2389 cpu->vend = le64_to_cpu(disk->vend);
2390 cpu->target = le64_to_cpu(disk->target);
2391 cpu->flags = le64_to_cpu(disk->flags);
2392 cpu->limit = le64_to_cpu(disk->limit);
2393 cpu->stripes_min = le32_to_cpu(disk->stripes_min);
2394 cpu->stripes_max = le32_to_cpu(disk->stripes_max);
2397 static inline void
2398 btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
2399 const struct btrfs_balance_args *cpu)
2401 memset(disk, 0, sizeof(*disk));
2403 disk->profiles = cpu_to_le64(cpu->profiles);
2404 disk->usage = cpu_to_le64(cpu->usage);
2405 disk->devid = cpu_to_le64(cpu->devid);
2406 disk->pstart = cpu_to_le64(cpu->pstart);
2407 disk->pend = cpu_to_le64(cpu->pend);
2408 disk->vstart = cpu_to_le64(cpu->vstart);
2409 disk->vend = cpu_to_le64(cpu->vend);
2410 disk->target = cpu_to_le64(cpu->target);
2411 disk->flags = cpu_to_le64(cpu->flags);
2412 disk->limit = cpu_to_le64(cpu->limit);
2413 disk->stripes_min = cpu_to_le32(cpu->stripes_min);
2414 disk->stripes_max = cpu_to_le32(cpu->stripes_max);
2417 /* struct btrfs_super_block */
2418 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
2419 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
2420 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
2421 generation, 64);
2422 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
2423 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
2424 struct btrfs_super_block, sys_chunk_array_size, 32);
2425 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
2426 struct btrfs_super_block, chunk_root_generation, 64);
2427 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
2428 root_level, 8);
2429 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
2430 chunk_root, 64);
2431 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
2432 chunk_root_level, 8);
2433 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
2434 log_root, 64);
2435 BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
2436 log_root_transid, 64);
2437 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
2438 log_root_level, 8);
2439 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
2440 total_bytes, 64);
2441 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
2442 bytes_used, 64);
2443 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
2444 sectorsize, 32);
2445 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
2446 nodesize, 32);
2447 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
2448 stripesize, 32);
2449 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
2450 root_dir_objectid, 64);
2451 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
2452 num_devices, 64);
2453 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
2454 compat_flags, 64);
2455 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
2456 compat_ro_flags, 64);
2457 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
2458 incompat_flags, 64);
2459 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
2460 csum_type, 16);
2461 BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
2462 cache_generation, 64);
2463 BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
2464 BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
2465 uuid_tree_generation, 64);
2467 static inline int btrfs_super_csum_size(const struct btrfs_super_block *s)
2469 u16 t = btrfs_super_csum_type(s);
2471 * csum type is validated at mount time
2473 return btrfs_csum_sizes[t];
2476 static inline const char *btrfs_super_csum_name(u16 csum_type)
2478 /* csum type is validated at mount time */
2479 return btrfs_csum_names[csum_type];
2483 * The leaf data grows from end-to-front in the node.
2484 * this returns the address of the start of the last item,
2485 * which is the stop of the leaf data stack
2487 static inline unsigned int leaf_data_end(const struct extent_buffer *leaf)
2489 u32 nr = btrfs_header_nritems(leaf);
2491 if (nr == 0)
2492 return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
2493 return btrfs_item_offset_nr(leaf, nr - 1);
2496 /* struct btrfs_file_extent_item */
2497 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
2498 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
2499 struct btrfs_file_extent_item, disk_bytenr, 64);
2500 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
2501 struct btrfs_file_extent_item, offset, 64);
2502 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
2503 struct btrfs_file_extent_item, generation, 64);
2504 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
2505 struct btrfs_file_extent_item, num_bytes, 64);
2506 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
2507 struct btrfs_file_extent_item, disk_num_bytes, 64);
2508 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
2509 struct btrfs_file_extent_item, compression, 8);
2511 static inline unsigned long
2512 btrfs_file_extent_inline_start(const struct btrfs_file_extent_item *e)
2514 return (unsigned long)e + BTRFS_FILE_EXTENT_INLINE_DATA_START;
2517 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2519 return BTRFS_FILE_EXTENT_INLINE_DATA_START + datasize;
2522 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
2523 disk_bytenr, 64);
2524 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
2525 generation, 64);
2526 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
2527 disk_num_bytes, 64);
2528 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
2529 offset, 64);
2530 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
2531 num_bytes, 64);
2532 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
2533 ram_bytes, 64);
2534 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
2535 compression, 8);
2536 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
2537 encryption, 8);
2538 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
2539 other_encoding, 16);
2542 * this returns the number of bytes used by the item on disk, minus the
2543 * size of any extent headers. If a file is compressed on disk, this is
2544 * the compressed size
2546 static inline u32 btrfs_file_extent_inline_item_len(
2547 const struct extent_buffer *eb,
2548 struct btrfs_item *e)
2550 return btrfs_item_size(eb, e) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
2553 /* btrfs_dev_stats_item */
2554 static inline u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
2555 const struct btrfs_dev_stats_item *ptr,
2556 int index)
2558 u64 val;
2560 read_extent_buffer(eb, &val,
2561 offsetof(struct btrfs_dev_stats_item, values) +
2562 ((unsigned long)ptr) + (index * sizeof(u64)),
2563 sizeof(val));
2564 return val;
2567 static inline void btrfs_set_dev_stats_value(struct extent_buffer *eb,
2568 struct btrfs_dev_stats_item *ptr,
2569 int index, u64 val)
2571 write_extent_buffer(eb, &val,
2572 offsetof(struct btrfs_dev_stats_item, values) +
2573 ((unsigned long)ptr) + (index * sizeof(u64)),
2574 sizeof(val));
2577 /* btrfs_qgroup_status_item */
2578 BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
2579 generation, 64);
2580 BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
2581 version, 64);
2582 BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
2583 flags, 64);
2584 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
2585 rescan, 64);
2587 /* btrfs_qgroup_info_item */
2588 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
2589 generation, 64);
2590 BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
2591 BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
2592 rfer_cmpr, 64);
2593 BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
2594 BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
2595 excl_cmpr, 64);
2597 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
2598 struct btrfs_qgroup_info_item, generation, 64);
2599 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
2600 rfer, 64);
2601 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
2602 struct btrfs_qgroup_info_item, rfer_cmpr, 64);
2603 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
2604 excl, 64);
2605 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
2606 struct btrfs_qgroup_info_item, excl_cmpr, 64);
2608 /* btrfs_qgroup_limit_item */
2609 BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
2610 flags, 64);
2611 BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
2612 max_rfer, 64);
2613 BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
2614 max_excl, 64);
2615 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
2616 rsv_rfer, 64);
2617 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
2618 rsv_excl, 64);
2620 /* btrfs_dev_replace_item */
2621 BTRFS_SETGET_FUNCS(dev_replace_src_devid,
2622 struct btrfs_dev_replace_item, src_devid, 64);
2623 BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
2624 struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
2625 64);
2626 BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
2627 replace_state, 64);
2628 BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
2629 time_started, 64);
2630 BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
2631 time_stopped, 64);
2632 BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
2633 num_write_errors, 64);
2634 BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
2635 struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
2636 64);
2637 BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
2638 cursor_left, 64);
2639 BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
2640 cursor_right, 64);
2642 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
2643 struct btrfs_dev_replace_item, src_devid, 64);
2644 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
2645 struct btrfs_dev_replace_item,
2646 cont_reading_from_srcdev_mode, 64);
2647 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
2648 struct btrfs_dev_replace_item, replace_state, 64);
2649 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
2650 struct btrfs_dev_replace_item, time_started, 64);
2651 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
2652 struct btrfs_dev_replace_item, time_stopped, 64);
2653 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
2654 struct btrfs_dev_replace_item, num_write_errors, 64);
2655 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
2656 struct btrfs_dev_replace_item,
2657 num_uncorrectable_read_errors, 64);
2658 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
2659 struct btrfs_dev_replace_item, cursor_left, 64);
2660 BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
2661 struct btrfs_dev_replace_item, cursor_right, 64);
2663 /* helper function to cast into the data area of the leaf. */
2664 #define btrfs_item_ptr(leaf, slot, type) \
2665 ((type *)(BTRFS_LEAF_DATA_OFFSET + \
2666 btrfs_item_offset_nr(leaf, slot)))
2668 #define btrfs_item_ptr_offset(leaf, slot) \
2669 ((unsigned long)(BTRFS_LEAF_DATA_OFFSET + \
2670 btrfs_item_offset_nr(leaf, slot)))
2672 static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
2674 return crc32c(crc, address, length);
2677 static inline void btrfs_crc32c_final(u32 crc, u8 *result)
2679 put_unaligned_le32(~crc, result);
2682 static inline u64 btrfs_name_hash(const char *name, int len)
2684 return crc32c((u32)~1, name, len);
2688 * Figure the key offset of an extended inode ref
2690 static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
2691 int len)
2693 return (u64) crc32c(parent_objectid, name, len);
2696 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
2698 return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
2699 (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
2702 static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
2704 return mapping_gfp_constraint(mapping, ~__GFP_FS);
2707 /* extent-tree.c */
2709 enum btrfs_inline_ref_type {
2710 BTRFS_REF_TYPE_INVALID,
2711 BTRFS_REF_TYPE_BLOCK,
2712 BTRFS_REF_TYPE_DATA,
2713 BTRFS_REF_TYPE_ANY,
2716 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
2717 struct btrfs_extent_inline_ref *iref,
2718 enum btrfs_inline_ref_type is_data);
2720 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes);
2722 static inline u64 btrfs_calc_trans_metadata_size(struct btrfs_fs_info *fs_info,
2723 unsigned num_items)
2725 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
2729 * Doing a truncate won't result in new nodes or leaves, just what we need for
2730 * COW.
2732 static inline u64 btrfs_calc_trunc_metadata_size(struct btrfs_fs_info *fs_info,
2733 unsigned num_items)
2735 return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
2738 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
2739 bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
2740 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
2741 const u64 start);
2742 void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg);
2743 bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2744 void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2745 void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg);
2746 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2747 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2748 unsigned long count);
2749 void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
2750 struct btrfs_delayed_ref_root *delayed_refs,
2751 struct btrfs_delayed_ref_head *head);
2752 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
2753 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2754 struct btrfs_fs_info *fs_info, u64 bytenr,
2755 u64 offset, int metadata, u64 *refs, u64 *flags);
2756 int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2757 u64 bytenr, u64 num, int reserved);
2758 int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
2759 u64 bytenr, u64 num_bytes);
2760 int btrfs_exclude_logged_extents(struct extent_buffer *eb);
2761 int btrfs_cross_ref_exist(struct btrfs_root *root,
2762 u64 objectid, u64 offset, u64 bytenr);
2763 struct btrfs_block_group_cache *btrfs_lookup_block_group(
2764 struct btrfs_fs_info *info,
2765 u64 bytenr);
2766 void btrfs_get_block_group(struct btrfs_block_group_cache *cache);
2767 void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2768 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
2769 struct btrfs_root *root,
2770 u64 parent, u64 root_objectid,
2771 const struct btrfs_disk_key *key,
2772 int level, u64 hint,
2773 u64 empty_size);
2774 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2775 struct btrfs_root *root,
2776 struct extent_buffer *buf,
2777 u64 parent, int last_ref);
2778 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root, u64 owner,
2780 u64 offset, u64 ram_bytes,
2781 struct btrfs_key *ins);
2782 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
2783 u64 root_objectid, u64 owner, u64 offset,
2784 struct btrfs_key *ins);
2785 int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
2786 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
2787 struct btrfs_key *ins, int is_data, int delalloc);
2788 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2789 struct extent_buffer *buf, int full_backref);
2790 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2791 struct extent_buffer *buf, int full_backref);
2792 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2793 u64 bytenr, u64 num_bytes, u64 flags,
2794 int level, int is_data);
2795 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
2797 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
2798 u64 start, u64 len, int delalloc);
2799 int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
2800 u64 start, u64 len);
2801 void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info);
2802 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
2803 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2804 struct btrfs_ref *generic_ref);
2806 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
2807 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
2808 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);
2809 int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);
2810 int btrfs_free_block_groups(struct btrfs_fs_info *info);
2811 int btrfs_read_block_groups(struct btrfs_fs_info *info);
2812 int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr);
2813 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2814 u64 bytes_used, u64 type, u64 chunk_offset,
2815 u64 size);
2816 void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
2817 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
2818 struct btrfs_fs_info *fs_info,
2819 const u64 chunk_offset);
2820 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
2821 u64 group_start, struct extent_map *em);
2822 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
2823 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache);
2824 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *cache);
2825 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans);
2826 u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info);
2827 u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info);
2828 u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info);
2829 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2831 enum btrfs_reserve_flush_enum {
2832 /* If we are in the transaction, we can't flush anything.*/
2833 BTRFS_RESERVE_NO_FLUSH,
2835 * Flushing delalloc may cause deadlock somewhere, in this
2836 * case, use FLUSH LIMIT
2838 BTRFS_RESERVE_FLUSH_LIMIT,
2839 BTRFS_RESERVE_FLUSH_ALL,
2842 enum btrfs_flush_state {
2843 FLUSH_DELAYED_ITEMS_NR = 1,
2844 FLUSH_DELAYED_ITEMS = 2,
2845 FLUSH_DELAYED_REFS_NR = 3,
2846 FLUSH_DELAYED_REFS = 4,
2847 FLUSH_DELALLOC = 5,
2848 FLUSH_DELALLOC_WAIT = 6,
2849 ALLOC_CHUNK = 7,
2850 ALLOC_CHUNK_FORCE = 8,
2851 COMMIT_TRANS = 9,
2854 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes);
2855 int btrfs_check_data_free_space(struct inode *inode,
2856 struct extent_changeset **reserved, u64 start, u64 len);
2857 void btrfs_free_reserved_data_space(struct inode *inode,
2858 struct extent_changeset *reserved, u64 start, u64 len);
2859 void btrfs_delalloc_release_space(struct inode *inode,
2860 struct extent_changeset *reserved,
2861 u64 start, u64 len, bool qgroup_free);
2862 void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
2863 u64 len);
2864 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
2865 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
2866 struct btrfs_block_rsv *rsv,
2867 int nitems, bool use_global_rsv);
2868 void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
2869 struct btrfs_block_rsv *rsv);
2870 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
2871 bool qgroup_free);
2873 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes);
2874 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
2875 bool qgroup_free);
2876 int btrfs_delalloc_reserve_space(struct inode *inode,
2877 struct extent_changeset **reserved, u64 start, u64 len);
2878 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type);
2879 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
2880 unsigned short type);
2881 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
2882 struct btrfs_block_rsv *rsv,
2883 unsigned short type);
2884 void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
2885 struct btrfs_block_rsv *rsv);
2886 int btrfs_block_rsv_add(struct btrfs_root *root,
2887 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
2888 enum btrfs_reserve_flush_enum flush);
2889 int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor);
2890 int btrfs_block_rsv_refill(struct btrfs_root *root,
2891 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
2892 enum btrfs_reserve_flush_enum flush);
2893 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
2894 struct btrfs_block_rsv *dst_rsv, u64 num_bytes,
2895 bool update_size);
2896 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
2897 struct btrfs_block_rsv *dest, u64 num_bytes,
2898 int min_factor);
2899 void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
2900 struct btrfs_block_rsv *block_rsv,
2901 u64 num_bytes);
2902 void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
2903 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
2904 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
2905 enum btrfs_reserve_flush_enum flush);
2906 void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
2907 struct btrfs_block_rsv *src,
2908 u64 num_bytes);
2909 int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache);
2910 void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache);
2911 void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
2912 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2913 int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
2914 u64 start, u64 end);
2915 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2916 u64 num_bytes, u64 *actual_bytes);
2917 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type);
2918 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
2920 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
2921 int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2922 struct btrfs_fs_info *fs_info);
2923 int btrfs_start_write_no_snapshotting(struct btrfs_root *root);
2924 void btrfs_end_write_no_snapshotting(struct btrfs_root *root);
2925 void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
2926 void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type);
2927 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
2928 u64 start, u64 end);
2929 void btrfs_mark_bg_unused(struct btrfs_block_group_cache *bg);
2931 /* ctree.c */
2932 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
2933 int level, int *slot);
2934 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
2935 int btrfs_previous_item(struct btrfs_root *root,
2936 struct btrfs_path *path, u64 min_objectid,
2937 int type);
2938 int btrfs_previous_extent_item(struct btrfs_root *root,
2939 struct btrfs_path *path, u64 min_objectid);
2940 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2941 struct btrfs_path *path,
2942 const struct btrfs_key *new_key);
2943 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
2944 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
2945 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
2946 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
2947 struct btrfs_key *key, int lowest_level,
2948 u64 min_trans);
2949 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
2950 struct btrfs_path *path,
2951 u64 min_trans);
2952 enum btrfs_compare_tree_result {
2953 BTRFS_COMPARE_TREE_NEW,
2954 BTRFS_COMPARE_TREE_DELETED,
2955 BTRFS_COMPARE_TREE_CHANGED,
2956 BTRFS_COMPARE_TREE_SAME,
2958 typedef int (*btrfs_changed_cb_t)(struct btrfs_path *left_path,
2959 struct btrfs_path *right_path,
2960 struct btrfs_key *key,
2961 enum btrfs_compare_tree_result result,
2962 void *ctx);
2963 int btrfs_compare_trees(struct btrfs_root *left_root,
2964 struct btrfs_root *right_root,
2965 btrfs_changed_cb_t cb, void *ctx);
2966 int btrfs_cow_block(struct btrfs_trans_handle *trans,
2967 struct btrfs_root *root, struct extent_buffer *buf,
2968 struct extent_buffer *parent, int parent_slot,
2969 struct extent_buffer **cow_ret);
2970 int btrfs_copy_root(struct btrfs_trans_handle *trans,
2971 struct btrfs_root *root,
2972 struct extent_buffer *buf,
2973 struct extent_buffer **cow_ret, u64 new_root_objectid);
2974 int btrfs_block_can_be_shared(struct btrfs_root *root,
2975 struct extent_buffer *buf);
2976 void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
2977 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
2978 int btrfs_split_item(struct btrfs_trans_handle *trans,
2979 struct btrfs_root *root,
2980 struct btrfs_path *path,
2981 const struct btrfs_key *new_key,
2982 unsigned long split_offset);
2983 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
2984 struct btrfs_root *root,
2985 struct btrfs_path *path,
2986 const struct btrfs_key *new_key);
2987 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2988 u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
2989 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2990 const struct btrfs_key *key, struct btrfs_path *p,
2991 int ins_len, int cow);
2992 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2993 struct btrfs_path *p, u64 time_seq);
2994 int btrfs_search_slot_for_read(struct btrfs_root *root,
2995 const struct btrfs_key *key,
2996 struct btrfs_path *p, int find_higher,
2997 int return_any);
2998 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
2999 struct btrfs_root *root, struct extent_buffer *parent,
3000 int start_slot, u64 *last_ret,
3001 struct btrfs_key *progress);
3002 void btrfs_release_path(struct btrfs_path *p);
3003 struct btrfs_path *btrfs_alloc_path(void);
3004 void btrfs_free_path(struct btrfs_path *p);
3005 void btrfs_set_path_blocking(struct btrfs_path *p);
3006 void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
3008 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3009 struct btrfs_path *path, int slot, int nr);
3010 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
3011 struct btrfs_root *root,
3012 struct btrfs_path *path)
3014 return btrfs_del_items(trans, root, path, path->slots[0], 1);
3017 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
3018 const struct btrfs_key *cpu_key, u32 *data_size,
3019 u32 total_data, u32 total_size, int nr);
3020 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3021 const struct btrfs_key *key, void *data, u32 data_size);
3022 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3023 struct btrfs_root *root,
3024 struct btrfs_path *path,
3025 const struct btrfs_key *cpu_key, u32 *data_size,
3026 int nr);
3028 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
3029 struct btrfs_root *root,
3030 struct btrfs_path *path,
3031 const struct btrfs_key *key,
3032 u32 data_size)
3034 return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
3037 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
3038 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
3039 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
3040 u64 time_seq);
3041 static inline int btrfs_next_old_item(struct btrfs_root *root,
3042 struct btrfs_path *p, u64 time_seq)
3044 ++p->slots[0];
3045 if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
3046 return btrfs_next_old_leaf(root, p, time_seq);
3047 return 0;
3049 static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
3051 return btrfs_next_old_item(root, p, 0);
3053 int btrfs_leaf_free_space(struct extent_buffer *leaf);
3054 int __must_check btrfs_drop_snapshot(struct btrfs_root *root,
3055 struct btrfs_block_rsv *block_rsv,
3056 int update_ref, int for_reloc);
3057 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3058 struct btrfs_root *root,
3059 struct extent_buffer *node,
3060 struct extent_buffer *parent);
3061 static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
3064 * Do it this way so we only ever do one test_bit in the normal case.
3066 if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
3067 if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
3068 return 2;
3069 return 1;
3071 return 0;
3075 * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
3076 * anything except sleeping. This function is used to check the status of
3077 * the fs.
3079 static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
3081 return fs_info->sb->s_flags & SB_RDONLY || btrfs_fs_closing(fs_info);
3084 static inline void free_fs_info(struct btrfs_fs_info *fs_info)
3086 kfree(fs_info->balance_ctl);
3087 kfree(fs_info->delayed_root);
3088 kfree(fs_info->extent_root);
3089 kfree(fs_info->tree_root);
3090 kfree(fs_info->chunk_root);
3091 kfree(fs_info->dev_root);
3092 kfree(fs_info->csum_root);
3093 kfree(fs_info->quota_root);
3094 kfree(fs_info->uuid_root);
3095 kfree(fs_info->free_space_root);
3096 kfree(fs_info->super_copy);
3097 kfree(fs_info->super_for_commit);
3098 kvfree(fs_info);
3101 /* tree mod log functions from ctree.c */
3102 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
3103 struct seq_list *elem);
3104 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
3105 struct seq_list *elem);
3106 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq);
3108 /* root-item.c */
3109 int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3110 u64 ref_id, u64 dirid, u64 sequence, const char *name,
3111 int name_len);
3112 int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
3113 u64 ref_id, u64 dirid, u64 *sequence, const char *name,
3114 int name_len);
3115 int btrfs_del_root(struct btrfs_trans_handle *trans,
3116 const struct btrfs_key *key);
3117 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3118 const struct btrfs_key *key,
3119 struct btrfs_root_item *item);
3120 int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root,
3122 struct btrfs_key *key,
3123 struct btrfs_root_item *item);
3124 int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
3125 struct btrfs_path *path, struct btrfs_root_item *root_item,
3126 struct btrfs_key *root_key);
3127 int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info);
3128 void btrfs_set_root_node(struct btrfs_root_item *item,
3129 struct extent_buffer *node);
3130 void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
3131 void btrfs_update_root_times(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root);
3134 /* uuid-tree.c */
3135 int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
3136 u64 subid);
3137 int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
3138 u64 subid);
3139 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
3140 int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
3141 u64));
3143 /* dir-item.c */
3144 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
3145 const char *name, int name_len);
3146 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
3147 int name_len, struct btrfs_inode *dir,
3148 struct btrfs_key *location, u8 type, u64 index);
3149 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
3150 struct btrfs_root *root,
3151 struct btrfs_path *path, u64 dir,
3152 const char *name, int name_len,
3153 int mod);
3154 struct btrfs_dir_item *
3155 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
3156 struct btrfs_root *root,
3157 struct btrfs_path *path, u64 dir,
3158 u64 objectid, const char *name, int name_len,
3159 int mod);
3160 struct btrfs_dir_item *
3161 btrfs_search_dir_index_item(struct btrfs_root *root,
3162 struct btrfs_path *path, u64 dirid,
3163 const char *name, int name_len);
3164 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
3165 struct btrfs_root *root,
3166 struct btrfs_path *path,
3167 struct btrfs_dir_item *di);
3168 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
3169 struct btrfs_root *root,
3170 struct btrfs_path *path, u64 objectid,
3171 const char *name, u16 name_len,
3172 const void *data, u16 data_len);
3173 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root,
3175 struct btrfs_path *path, u64 dir,
3176 const char *name, u16 name_len,
3177 int mod);
3178 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
3179 struct btrfs_path *path,
3180 const char *name,
3181 int name_len);
3183 /* orphan.c */
3184 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
3185 struct btrfs_root *root, u64 offset);
3186 int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
3187 struct btrfs_root *root, u64 offset);
3188 int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
3190 /* inode-item.c */
3191 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 const char *name, int name_len,
3194 u64 inode_objectid, u64 ref_objectid, u64 index);
3195 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
3196 struct btrfs_root *root,
3197 const char *name, int name_len,
3198 u64 inode_objectid, u64 ref_objectid, u64 *index);
3199 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root,
3201 struct btrfs_path *path, u64 objectid);
3202 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
3203 *root, struct btrfs_path *path,
3204 struct btrfs_key *location, int mod);
3206 struct btrfs_inode_extref *
3207 btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
3208 struct btrfs_root *root,
3209 struct btrfs_path *path,
3210 const char *name, int name_len,
3211 u64 inode_objectid, u64 ref_objectid, int ins_len,
3212 int cow);
3214 int btrfs_find_name_in_backref(struct extent_buffer *leaf, int slot,
3215 const char *name,
3216 int name_len, struct btrfs_inode_ref **ref_ret);
3217 int btrfs_find_name_in_ext_backref(struct extent_buffer *leaf, int slot,
3218 u64 ref_objectid, const char *name,
3219 int name_len,
3220 struct btrfs_inode_extref **extref_ret);
3222 /* file-item.c */
3223 struct btrfs_dio_private;
3224 int btrfs_del_csums(struct btrfs_trans_handle *trans,
3225 struct btrfs_fs_info *fs_info, u64 bytenr, u64 len);
3226 blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio,
3227 u8 *dst);
3228 blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio,
3229 u64 logical_offset);
3230 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
3231 struct btrfs_root *root,
3232 u64 objectid, u64 pos,
3233 u64 disk_offset, u64 disk_num_bytes,
3234 u64 num_bytes, u64 offset, u64 ram_bytes,
3235 u8 compression, u8 encryption, u16 other_encoding);
3236 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
3237 struct btrfs_root *root,
3238 struct btrfs_path *path, u64 objectid,
3239 u64 bytenr, int mod);
3240 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
3241 struct btrfs_root *root,
3242 struct btrfs_ordered_sum *sums);
3243 blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
3244 u64 file_start, int contig);
3245 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
3246 struct list_head *list, int search_commit);
3247 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
3248 const struct btrfs_path *path,
3249 struct btrfs_file_extent_item *fi,
3250 const bool new_inline,
3251 struct extent_map *em);
3253 /* inode.c */
3254 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
3255 u64 start, u64 len);
3256 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
3257 u64 *orig_start, u64 *orig_block_len,
3258 u64 *ram_bytes);
3260 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
3261 struct btrfs_inode *inode);
3262 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
3263 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
3264 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3265 struct btrfs_root *root,
3266 struct btrfs_inode *dir, struct btrfs_inode *inode,
3267 const char *name, int name_len);
3268 int btrfs_add_link(struct btrfs_trans_handle *trans,
3269 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
3270 const char *name, int name_len, int add_backref, u64 index);
3271 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
3272 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
3273 int front);
3274 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3275 struct btrfs_root *root,
3276 struct inode *inode, u64 new_size,
3277 u32 min_type);
3279 int btrfs_start_delalloc_snapshot(struct btrfs_root *root);
3280 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr);
3281 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
3282 unsigned int extra_bits,
3283 struct extent_state **cached_state, int dedupe);
3284 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
3285 struct btrfs_root *new_root,
3286 struct btrfs_root *parent_root,
3287 u64 new_dirid);
3288 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
3289 unsigned *bits);
3290 void btrfs_clear_delalloc_extent(struct inode *inode,
3291 struct extent_state *state, unsigned *bits);
3292 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
3293 struct extent_state *other);
3294 void btrfs_split_delalloc_extent(struct inode *inode,
3295 struct extent_state *orig, u64 split);
3296 int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
3297 unsigned long bio_flags);
3298 void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end);
3299 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
3300 int btrfs_readpage(struct file *file, struct page *page);
3301 void btrfs_evict_inode(struct inode *inode);
3302 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc);
3303 struct inode *btrfs_alloc_inode(struct super_block *sb);
3304 void btrfs_destroy_inode(struct inode *inode);
3305 void btrfs_free_inode(struct inode *inode);
3306 int btrfs_drop_inode(struct inode *inode);
3307 int __init btrfs_init_cachep(void);
3308 void __cold btrfs_destroy_cachep(void);
3309 struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
3310 struct btrfs_root *root, int *new,
3311 struct btrfs_path *path);
3312 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3313 struct btrfs_root *root, int *was_new);
3314 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
3315 struct page *page, size_t pg_offset,
3316 u64 start, u64 end, int create);
3317 int btrfs_update_inode(struct btrfs_trans_handle *trans,
3318 struct btrfs_root *root,
3319 struct inode *inode);
3320 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3321 struct btrfs_root *root, struct inode *inode);
3322 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3323 struct btrfs_inode *inode);
3324 int btrfs_orphan_cleanup(struct btrfs_root *root);
3325 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size);
3326 void btrfs_add_delayed_iput(struct inode *inode);
3327 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
3328 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
3329 int btrfs_prealloc_file_range(struct inode *inode, int mode,
3330 u64 start, u64 num_bytes, u64 min_size,
3331 loff_t actual_len, u64 *alloc_hint);
3332 int btrfs_prealloc_file_range_trans(struct inode *inode,
3333 struct btrfs_trans_handle *trans, int mode,
3334 u64 start, u64 num_bytes, u64 min_size,
3335 loff_t actual_len, u64 *alloc_hint);
3336 int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
3337 u64 start, u64 end, int *page_started, unsigned long *nr_written,
3338 struct writeback_control *wbc);
3339 int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end);
3340 void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3341 u64 end, int uptodate);
3342 extern const struct dentry_operations btrfs_dentry_operations;
3344 /* ioctl.c */
3345 long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3346 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3347 int btrfs_ioctl_get_supported_features(void __user *arg);
3348 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
3349 int btrfs_is_empty_uuid(u8 *uuid);
3350 int btrfs_defrag_file(struct inode *inode, struct file *file,
3351 struct btrfs_ioctl_defrag_range_args *range,
3352 u64 newer_than, unsigned long max_pages);
3353 void btrfs_get_block_group_info(struct list_head *groups_list,
3354 struct btrfs_ioctl_space_info *space);
3355 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3356 struct btrfs_ioctl_balance_args *bargs);
3358 /* file.c */
3359 int __init btrfs_auto_defrag_init(void);
3360 void __cold btrfs_auto_defrag_exit(void);
3361 int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
3362 struct btrfs_inode *inode);
3363 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
3364 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
3365 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
3366 void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
3367 int skip_pinned);
3368 extern const struct file_operations btrfs_file_operations;
3369 int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
3370 struct btrfs_root *root, struct inode *inode,
3371 struct btrfs_path *path, u64 start, u64 end,
3372 u64 *drop_end, int drop_cache,
3373 int replace_extent,
3374 u32 extent_item_size,
3375 int *key_inserted);
3376 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
3377 struct btrfs_root *root, struct inode *inode, u64 start,
3378 u64 end, int drop_cache);
3379 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
3380 struct btrfs_inode *inode, u64 start, u64 end);
3381 int btrfs_release_file(struct inode *inode, struct file *file);
3382 int btrfs_dirty_pages(struct inode *inode, struct page **pages,
3383 size_t num_pages, loff_t pos, size_t write_bytes,
3384 struct extent_state **cached);
3385 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
3386 loff_t btrfs_remap_file_range(struct file *file_in, loff_t pos_in,
3387 struct file *file_out, loff_t pos_out,
3388 loff_t len, unsigned int remap_flags);
3390 /* tree-defrag.c */
3391 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
3392 struct btrfs_root *root);
3394 /* sysfs.c */
3395 int __init btrfs_init_sysfs(void);
3396 void __cold btrfs_exit_sysfs(void);
3397 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
3398 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
3400 /* super.c */
3401 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
3402 unsigned long new_flags);
3403 int btrfs_sync_fs(struct super_block *sb, int wait);
3405 static inline __printf(2, 3) __cold
3406 void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
3410 #ifdef CONFIG_PRINTK
3411 __printf(2, 3)
3412 __cold
3413 void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
3414 #else
3415 #define btrfs_printk(fs_info, fmt, args...) \
3416 btrfs_no_printk(fs_info, fmt, ##args)
3417 #endif
3419 #define btrfs_emerg(fs_info, fmt, args...) \
3420 btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
3421 #define btrfs_alert(fs_info, fmt, args...) \
3422 btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
3423 #define btrfs_crit(fs_info, fmt, args...) \
3424 btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
3425 #define btrfs_err(fs_info, fmt, args...) \
3426 btrfs_printk(fs_info, KERN_ERR fmt, ##args)
3427 #define btrfs_warn(fs_info, fmt, args...) \
3428 btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
3429 #define btrfs_notice(fs_info, fmt, args...) \
3430 btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
3431 #define btrfs_info(fs_info, fmt, args...) \
3432 btrfs_printk(fs_info, KERN_INFO fmt, ##args)
3435 * Wrappers that use printk_in_rcu
3437 #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
3438 btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3439 #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
3440 btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3441 #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
3442 btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3443 #define btrfs_err_in_rcu(fs_info, fmt, args...) \
3444 btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
3445 #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
3446 btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3447 #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
3448 btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3449 #define btrfs_info_in_rcu(fs_info, fmt, args...) \
3450 btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
3453 * Wrappers that use a ratelimited printk_in_rcu
3455 #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
3456 btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
3457 #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
3458 btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
3459 #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
3460 btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
3461 #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
3462 btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
3463 #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
3464 btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
3465 #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
3466 btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
3467 #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
3468 btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
3471 * Wrappers that use a ratelimited printk
3473 #define btrfs_emerg_rl(fs_info, fmt, args...) \
3474 btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
3475 #define btrfs_alert_rl(fs_info, fmt, args...) \
3476 btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
3477 #define btrfs_crit_rl(fs_info, fmt, args...) \
3478 btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
3479 #define btrfs_err_rl(fs_info, fmt, args...) \
3480 btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
3481 #define btrfs_warn_rl(fs_info, fmt, args...) \
3482 btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
3483 #define btrfs_notice_rl(fs_info, fmt, args...) \
3484 btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
3485 #define btrfs_info_rl(fs_info, fmt, args...) \
3486 btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
3488 #if defined(CONFIG_DYNAMIC_DEBUG)
3489 #define btrfs_debug(fs_info, fmt, args...) \
3490 _dynamic_func_call_no_desc(fmt, btrfs_printk, \
3491 fs_info, KERN_DEBUG fmt, ##args)
3492 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3493 _dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu, \
3494 fs_info, KERN_DEBUG fmt, ##args)
3495 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3496 _dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu, \
3497 fs_info, KERN_DEBUG fmt, ##args)
3498 #define btrfs_debug_rl(fs_info, fmt, args...) \
3499 _dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited, \
3500 fs_info, KERN_DEBUG fmt, ##args)
3501 #elif defined(DEBUG)
3502 #define btrfs_debug(fs_info, fmt, args...) \
3503 btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
3504 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3505 btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3506 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3507 btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3508 #define btrfs_debug_rl(fs_info, fmt, args...) \
3509 btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
3510 #else
3511 #define btrfs_debug(fs_info, fmt, args...) \
3512 btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
3513 #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
3514 btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3515 #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
3516 btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
3517 #define btrfs_debug_rl(fs_info, fmt, args...) \
3518 btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
3519 #endif
3521 #define btrfs_printk_in_rcu(fs_info, fmt, args...) \
3522 do { \
3523 rcu_read_lock(); \
3524 btrfs_printk(fs_info, fmt, ##args); \
3525 rcu_read_unlock(); \
3526 } while (0)
3528 #define btrfs_no_printk_in_rcu(fs_info, fmt, args...) \
3529 do { \
3530 rcu_read_lock(); \
3531 btrfs_no_printk(fs_info, fmt, ##args); \
3532 rcu_read_unlock(); \
3533 } while (0)
3535 #define btrfs_printk_ratelimited(fs_info, fmt, args...) \
3536 do { \
3537 static DEFINE_RATELIMIT_STATE(_rs, \
3538 DEFAULT_RATELIMIT_INTERVAL, \
3539 DEFAULT_RATELIMIT_BURST); \
3540 if (__ratelimit(&_rs)) \
3541 btrfs_printk(fs_info, fmt, ##args); \
3542 } while (0)
3544 #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
3545 do { \
3546 rcu_read_lock(); \
3547 btrfs_printk_ratelimited(fs_info, fmt, ##args); \
3548 rcu_read_unlock(); \
3549 } while (0)
3551 __cold
3552 static inline void assfail(const char *expr, const char *file, int line)
3554 if (IS_ENABLED(CONFIG_BTRFS_ASSERT)) {
3555 pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
3556 BUG();
3560 #define ASSERT(expr) \
3561 (likely(expr) ? (void)0 : assfail(#expr, __FILE__, __LINE__))
3564 * Use that for functions that are conditionally exported for sanity tests but
3565 * otherwise static
3567 #ifndef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3568 #define EXPORT_FOR_TESTS static
3569 #else
3570 #define EXPORT_FOR_TESTS
3571 #endif
3573 __cold
3574 static inline void btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
3576 btrfs_err(fs_info,
3577 "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
3580 __printf(5, 6)
3581 __cold
3582 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
3583 unsigned int line, int errno, const char *fmt, ...);
3585 const char *btrfs_decode_error(int errno);
3587 __cold
3588 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
3589 const char *function,
3590 unsigned int line, int errno);
3593 * Call btrfs_abort_transaction as early as possible when an error condition is
3594 * detected, that way the exact line number is reported.
3596 #define btrfs_abort_transaction(trans, errno) \
3597 do { \
3598 /* Report first abort since mount */ \
3599 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
3600 &((trans)->fs_info->fs_state))) { \
3601 if ((errno) != -EIO) { \
3602 WARN(1, KERN_DEBUG \
3603 "BTRFS: Transaction aborted (error %d)\n", \
3604 (errno)); \
3605 } else { \
3606 btrfs_debug((trans)->fs_info, \
3607 "Transaction aborted (error %d)", \
3608 (errno)); \
3611 __btrfs_abort_transaction((trans), __func__, \
3612 __LINE__, (errno)); \
3613 } while (0)
3615 #define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \
3616 do { \
3617 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
3618 (errno), fmt, ##args); \
3619 } while (0)
3621 __printf(5, 6)
3622 __cold
3623 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
3624 unsigned int line, int errno, const char *fmt, ...);
3626 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
3627 * will panic(). Otherwise we BUG() here.
3629 #define btrfs_panic(fs_info, errno, fmt, args...) \
3630 do { \
3631 __btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \
3632 BUG(); \
3633 } while (0)
3636 /* compatibility and incompatibility defines */
3638 #define btrfs_set_fs_incompat(__fs_info, opt) \
3639 __btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
3640 #opt)
3642 static inline void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info,
3643 u64 flag, const char* name)
3645 struct btrfs_super_block *disk_super;
3646 u64 features;
3648 disk_super = fs_info->super_copy;
3649 features = btrfs_super_incompat_flags(disk_super);
3650 if (!(features & flag)) {
3651 spin_lock(&fs_info->super_lock);
3652 features = btrfs_super_incompat_flags(disk_super);
3653 if (!(features & flag)) {
3654 features |= flag;
3655 btrfs_set_super_incompat_flags(disk_super, features);
3656 btrfs_info(fs_info,
3657 "setting incompat feature flag for %s (0x%llx)",
3658 name, flag);
3660 spin_unlock(&fs_info->super_lock);
3664 #define btrfs_clear_fs_incompat(__fs_info, opt) \
3665 __btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
3666 #opt)
3668 static inline void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info,
3669 u64 flag, const char* name)
3671 struct btrfs_super_block *disk_super;
3672 u64 features;
3674 disk_super = fs_info->super_copy;
3675 features = btrfs_super_incompat_flags(disk_super);
3676 if (features & flag) {
3677 spin_lock(&fs_info->super_lock);
3678 features = btrfs_super_incompat_flags(disk_super);
3679 if (features & flag) {
3680 features &= ~flag;
3681 btrfs_set_super_incompat_flags(disk_super, features);
3682 btrfs_info(fs_info,
3683 "clearing incompat feature flag for %s (0x%llx)",
3684 name, flag);
3686 spin_unlock(&fs_info->super_lock);
3690 #define btrfs_fs_incompat(fs_info, opt) \
3691 __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
3693 static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
3695 struct btrfs_super_block *disk_super;
3696 disk_super = fs_info->super_copy;
3697 return !!(btrfs_super_incompat_flags(disk_super) & flag);
3700 #define btrfs_set_fs_compat_ro(__fs_info, opt) \
3701 __btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
3702 #opt)
3704 static inline void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info,
3705 u64 flag, const char *name)
3707 struct btrfs_super_block *disk_super;
3708 u64 features;
3710 disk_super = fs_info->super_copy;
3711 features = btrfs_super_compat_ro_flags(disk_super);
3712 if (!(features & flag)) {
3713 spin_lock(&fs_info->super_lock);
3714 features = btrfs_super_compat_ro_flags(disk_super);
3715 if (!(features & flag)) {
3716 features |= flag;
3717 btrfs_set_super_compat_ro_flags(disk_super, features);
3718 btrfs_info(fs_info,
3719 "setting compat-ro feature flag for %s (0x%llx)",
3720 name, flag);
3722 spin_unlock(&fs_info->super_lock);
3726 #define btrfs_clear_fs_compat_ro(__fs_info, opt) \
3727 __btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
3728 #opt)
3730 static inline void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info,
3731 u64 flag, const char *name)
3733 struct btrfs_super_block *disk_super;
3734 u64 features;
3736 disk_super = fs_info->super_copy;
3737 features = btrfs_super_compat_ro_flags(disk_super);
3738 if (features & flag) {
3739 spin_lock(&fs_info->super_lock);
3740 features = btrfs_super_compat_ro_flags(disk_super);
3741 if (features & flag) {
3742 features &= ~flag;
3743 btrfs_set_super_compat_ro_flags(disk_super, features);
3744 btrfs_info(fs_info,
3745 "clearing compat-ro feature flag for %s (0x%llx)",
3746 name, flag);
3748 spin_unlock(&fs_info->super_lock);
3752 #define btrfs_fs_compat_ro(fs_info, opt) \
3753 __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
3755 static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
3757 struct btrfs_super_block *disk_super;
3758 disk_super = fs_info->super_copy;
3759 return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
3762 /* acl.c */
3763 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
3764 struct posix_acl *btrfs_get_acl(struct inode *inode, int type);
3765 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
3766 int btrfs_init_acl(struct btrfs_trans_handle *trans,
3767 struct inode *inode, struct inode *dir);
3768 #else
3769 #define btrfs_get_acl NULL
3770 #define btrfs_set_acl NULL
3771 static inline int btrfs_init_acl(struct btrfs_trans_handle *trans,
3772 struct inode *inode, struct inode *dir)
3774 return 0;
3776 #endif
3778 /* relocation.c */
3779 int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start);
3780 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
3781 struct btrfs_root *root);
3782 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
3783 struct btrfs_root *root);
3784 int btrfs_recover_relocation(struct btrfs_root *root);
3785 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len);
3786 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
3787 struct btrfs_root *root, struct extent_buffer *buf,
3788 struct extent_buffer *cow);
3789 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
3790 u64 *bytes_to_reserve);
3791 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
3792 struct btrfs_pending_snapshot *pending);
3794 /* scrub.c */
3795 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3796 u64 end, struct btrfs_scrub_progress *progress,
3797 int readonly, int is_dev_replace);
3798 void btrfs_scrub_pause(struct btrfs_fs_info *fs_info);
3799 void btrfs_scrub_continue(struct btrfs_fs_info *fs_info);
3800 int btrfs_scrub_cancel(struct btrfs_fs_info *info);
3801 int btrfs_scrub_cancel_dev(struct btrfs_device *dev);
3802 int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
3803 struct btrfs_scrub_progress *progress);
3804 static inline void btrfs_init_full_stripe_locks_tree(
3805 struct btrfs_full_stripe_locks_tree *locks_root)
3807 locks_root->root = RB_ROOT;
3808 mutex_init(&locks_root->lock);
3811 /* dev-replace.c */
3812 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info);
3813 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info);
3814 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount);
3816 static inline void btrfs_bio_counter_dec(struct btrfs_fs_info *fs_info)
3818 btrfs_bio_counter_sub(fs_info, 1);
3821 /* reada.c */
3822 struct reada_control {
3823 struct btrfs_fs_info *fs_info; /* tree to prefetch */
3824 struct btrfs_key key_start;
3825 struct btrfs_key key_end; /* exclusive */
3826 atomic_t elems;
3827 struct kref refcnt;
3828 wait_queue_head_t wait;
3830 struct reada_control *btrfs_reada_add(struct btrfs_root *root,
3831 struct btrfs_key *start, struct btrfs_key *end);
3832 int btrfs_reada_wait(void *handle);
3833 void btrfs_reada_detach(void *handle);
3834 int btree_readahead_hook(struct extent_buffer *eb, int err);
3836 static inline int is_fstree(u64 rootid)
3838 if (rootid == BTRFS_FS_TREE_OBJECTID ||
3839 ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
3840 !btrfs_qgroup_level(rootid)))
3841 return 1;
3842 return 0;
3845 static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
3847 return signal_pending(current);
3850 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
3852 /* Sanity test specific functions */
3853 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3854 void btrfs_test_inode_set_ops(struct inode *inode);
3855 void btrfs_test_destroy_inode(struct inode *inode);
3857 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3859 return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
3861 #else
3862 static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
3864 return 0;
3866 #endif
3868 static inline void cond_wake_up(struct wait_queue_head *wq)
3871 * This implies a full smp_mb barrier, see comments for
3872 * waitqueue_active why.
3874 if (wq_has_sleeper(wq))
3875 wake_up(wq);
3878 static inline void cond_wake_up_nomb(struct wait_queue_head *wq)
3881 * Special case for conditional wakeup where the barrier required for
3882 * waitqueue_active is implied by some of the preceding code. Eg. one
3883 * of such atomic operations (atomic_dec_and_return, ...), or a
3884 * unlock/lock sequence, etc.
3886 if (waitqueue_active(wq))
3887 wake_up(wq);
3890 #endif