History

Merge tag 'for-6.8-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux

Pull btrfs updates from David Sterba: "There are no exciting changes for users, it's been mostly API conversions and some fixes or refactoring. The mount API conversion is a base for future improvements that would come with VFS. Metadata processing has been converted to folios, not yet enabling the large folios but it's one patch away once everything gets tested enough. Core changes: - convert extent buffers to folios: - direct API conversion where possible - performance can drop by a few percent on metadata heavy workloads, the folio sizes are not constant and the calculations add up in the item helpers - both regular and subpage modes - data cannot be converted yet, we need to port that to iomap and there are some other generic changes required - convert mount to the new API, should not be user visible: - options deprecated long time ago have been removed: inode_cache, recovery - the new logic that splits mount to two phases slightly changes timing of device scanning for multi-device filesystems - LSM options will now work (like for selinux) - convert delayed nodes radix tree to xarray, preserving the preload-like logic that still allows to allocate with GFP_NOFS - more validation of sysfs value of scrub_speed_max - refactor chunk map structure, reduce size and improve performance - extent map refactoring, smaller data structures, improved performance - reduce size of struct extent_io_tree, embedded in several structures - temporary pages used for compression are cached and attached to a shrinker, this may slightly improve performance - in zoned mode, remove redirty extent buffer tracking, zeros are written in case an out-of-order is detected and proper data are written to the actual write pointer - cleanups, refactoring, error message improvements, updated tests - verify and update branch name or tag - remove unwanted text" * tag 'for-6.8-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: (89 commits) btrfs: pass btrfs_io_geometry into btrfs_max_io_len btrfs: pass struct btrfs_io_geometry to set_io_stripe btrfs: open code set_io_stripe for RAID56 btrfs: change block mapping to switch/case in btrfs_map_block btrfs: factor out block mapping for single profiles btrfs: factor out block mapping for RAID5/6 btrfs: reduce scope of data_stripes in btrfs_map_block btrfs: factor out block mapping for RAID10 btrfs: factor out block mapping for DUP profiles btrfs: factor out RAID1 block mapping btrfs: factor out block-mapping for RAID0 btrfs: re-introduce struct btrfs_io_geometry btrfs: factor out helper for single device IO check btrfs: migrate btrfs_repair_io_failure() to folio interfaces btrfs: migrate eb_bitmap_offset() to folio interfaces btrfs: migrate various end io functions to folios btrfs: migrate subpage code to folio interfaces btrfs: migrate get_eb_page_index() and get_eb_offset_in_page() to folios btrfs: don't double put our subpage reference in alloc_extent_buffer btrfs: cleanup metadata page pointer usage ...

Linus Torvalds committed 2 years ago

Commit affc5af2 parents 12958e9 e94dfb761 files · +3883 −3184Browse files

Unified · Split

61 files
  1. fs/btrfs/accessors.c +54 −44
  2. fs/btrfs/accessors.h +2 −2
  3. fs/btrfs/bio.c +12 −5
  4. fs/btrfs/bio.h +2 −2
  5. fs/btrfs/block-group.c +69 −100
  6. fs/btrfs/block-group.h +4 −2
  7. fs/btrfs/btrfs_inode.h +7 −3
  8. fs/btrfs/compression.c +128 −11
  9. fs/btrfs/compression.h +5 −0
  10. fs/btrfs/ctree.c +36 −27
  11. fs/btrfs/ctree.h +6 −11
  12. fs/btrfs/defrag.c +6 −7
  13. fs/btrfs/delayed-inode.c +59 −50
  14. fs/btrfs/dev-replace.c +12 −16
  15. fs/btrfs/disk-io.c +70 −85
  16. fs/btrfs/disk-io.h +0 −3
  17. fs/btrfs/extent-io-tree.c +72 −47
  18. fs/btrfs/extent-io-tree.h +15 −3
  19. fs/btrfs/extent-tree.c +51 −53
  20. fs/btrfs/extent_io.c +633 −418
  21. fs/btrfs/extent_io.h +61 −19
  22. fs/btrfs/extent_map.c +80 −115
  23. fs/btrfs/extent_map.h +52 −25
  24. fs/btrfs/file-item.c +6 −9
  25. fs/btrfs/file.c +14 −13
  26. fs/btrfs/free-space-cache.c +2 −2
  27. fs/btrfs/fs.h +3 −15
  28. fs/btrfs/inode.c +98 −55
  29. fs/btrfs/lru_cache.c +1 −1
  30. fs/btrfs/lzo.c +2 −2
  31. fs/btrfs/messages.c +1 −1
  32. fs/btrfs/messages.h +1 −1
  33. fs/btrfs/ordered-data.c +3 −2
  34. fs/btrfs/ordered-data.h +0 −7
  35. fs/btrfs/qgroup.c +1 −1
  36. fs/btrfs/raid56.c +3 −4
  37. fs/btrfs/raid56.h +1 −1
  38. fs/btrfs/reflink.c +3 −3
  39. fs/btrfs/relocation.c +4 −3
  40. fs/btrfs/scrub.c +26 −37
  41. fs/btrfs/subpage.c +192 −181
  42. fs/btrfs/subpage.h +39 −43
  43. fs/btrfs/super.c +1195 −1150
  44. fs/btrfs/super.h +3 −2
  45. fs/btrfs/sysfs.c +4 −0
  46. fs/btrfs/tests/btrfs-tests.c +3 −2
  47. fs/btrfs/tests/btrfs-tests.h +1 −0
  48. fs/btrfs/tests/extent-io-tests.c +2 −2
  49. fs/btrfs/tests/extent-map-tests.c +72 −71
  50. fs/btrfs/tests/inode-tests.c +30 −30
  51. fs/btrfs/tree-checker.h +1 −1
  52. fs/btrfs/tree-log.c +9 −8
  53. fs/btrfs/volumes.c +564 −370
  54. fs/btrfs/volumes.h +37 −10
  55. fs/btrfs/xattr.c +53 −2
  56. fs/btrfs/zlib.c +3 −3
  57. fs/btrfs/zoned.c +22 −44
  58. fs/btrfs/zoned.h +4 −8
  59. fs/btrfs/zstd.c +3 −4
  60. fs/namespace.c +11 −0
  61. include/trace/events/btrfs.h +30 −48
fs/btrfs/accessors.c+54−44
@@ -27,7 +27,7 @@ static bool check_setget_bounds(const struct extent_buffer *eb, · 26 unmodified lines
27void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)27void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)
28{28{
29 token->eb = eb;29 token->eb = eb;
30 token->kaddr = page_address(eb->pages[0]);30 token->kaddr = folio_address(eb->folios[0]);
31 token->offset = 0;31 token->offset = 0;
32}32}
33 33
@@ -50,7 +50,7 @@ void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *e · 16 unmodified lines
50 * an offset into the extent buffer page array, cast to a specific type. This50 * an offset into the extent buffer page array, cast to a specific type. This
51 * gives us all the type checking.51 * gives us all the type checking.
52 *52 *
53 * The extent buffer pages stored in the array pages do not form a contiguous53 * The extent buffer pages stored in the array folios may not form a contiguous
54 * phyusical range, but the API functions assume the linear offset to the range54 * phyusical range, but the API functions assume the linear offset to the range
55 * from 0 to metadata node size.55 * from 0 to metadata node size.
56 */56 */
@@ -60,28 +60,30 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ · 3 unmodified lines
60 const void *ptr, unsigned long off) \60 const void *ptr, unsigned long off) \
61{ \61{ \
62 const unsigned long member_offset = (unsigned long)ptr + off; \62 const unsigned long member_offset = (unsigned long)ptr + off; \
63 const unsigned long idx = get_eb_page_index(member_offset); \63 const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
64 const unsigned long oip = get_eb_offset_in_page(token->eb, \64 const unsigned long oil = get_eb_offset_in_folio(token->eb, \
65 member_offset); \65 member_offset);\
66 const int unit_size = folio_size(token->eb->folios[0]); \
67 const int unit_shift = folio_shift(token->eb->folios[0]); \
66 const int size = sizeof(u##bits); \68 const int size = sizeof(u##bits); \
67 u8 lebytes[sizeof(u##bits)]; \69 u8 lebytes[sizeof(u##bits)]; \
68 const int part = PAGE_SIZE - oip; \70 const int part = unit_size - oil; \
69 \71 \
70 ASSERT(token); \72 ASSERT(token); \
71 ASSERT(token->kaddr); \73 ASSERT(token->kaddr); \
72 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \74 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
73 if (token->offset <= member_offset && \75 if (token->offset <= member_offset && \
74 member_offset + size <= token->offset + PAGE_SIZE) { \76 member_offset + size <= token->offset + unit_size) { \
75 return get_unaligned_le##bits(token->kaddr + oip); \77 return get_unaligned_le##bits(token->kaddr + oil); \
76 } \78 } \
77 token->kaddr = page_address(token->eb->pages[idx]); \79 token->kaddr = folio_address(token->eb->folios[idx]); \
78 token->offset = idx << PAGE_SHIFT; \80 token->offset = idx << unit_shift; \
79 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE ) \81 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \
80 return get_unaligned_le##bits(token->kaddr + oip); \82 return get_unaligned_le##bits(token->kaddr + oil); \
81 \83 \
82 memcpy(lebytes, token->kaddr + oip, part); \84 memcpy(lebytes, token->kaddr + oil, part); \
83 token->kaddr = page_address(token->eb->pages[idx + 1]); \85 token->kaddr = folio_address(token->eb->folios[idx + 1]); \
84 token->offset = (idx + 1) << PAGE_SHIFT; \86 token->offset = (idx + 1) << unit_shift; \
85 memcpy(lebytes + part, token->kaddr, size - part); \87 memcpy(lebytes + part, token->kaddr, size - part); \
86 return get_unaligned_le##bits(lebytes); \88 return get_unaligned_le##bits(lebytes); \
87} \89} \
@@ -89,19 +91,21 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ · 1 unmodified line
89 const void *ptr, unsigned long off) \91 const void *ptr, unsigned long off) \
90{ \92{ \
91 const unsigned long member_offset = (unsigned long)ptr + off; \93 const unsigned long member_offset = (unsigned long)ptr + off; \
92 const unsigned long oip = get_eb_offset_in_page(eb, member_offset); \94 const unsigned long idx = get_eb_folio_index(eb, member_offset);\
93 const unsigned long idx = get_eb_page_index(member_offset); \95 const unsigned long oil = get_eb_offset_in_folio(eb, \
94 char *kaddr = page_address(eb->pages[idx]); \96 member_offset);\
97 const int unit_size = folio_size(eb->folios[0]); \
98 char *kaddr = folio_address(eb->folios[idx]); \
95 const int size = sizeof(u##bits); \99 const int size = sizeof(u##bits); \
96 const int part = PAGE_SIZE - oip; \100 const int part = unit_size - oil; \
97 u8 lebytes[sizeof(u##bits)]; \101 u8 lebytes[sizeof(u##bits)]; \
98 \102 \
99 ASSERT(check_setget_bounds(eb, ptr, off, size)); \103 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
100 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) \104 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \
101 return get_unaligned_le##bits(kaddr + oip); \105 return get_unaligned_le##bits(kaddr + oil); \
102 \106 \
103 memcpy(lebytes, kaddr + oip, part); \107 memcpy(lebytes, kaddr + oil, part); \
104 kaddr = page_address(eb->pages[idx + 1]); \108 kaddr = folio_address(eb->folios[idx + 1]); \
105 memcpy(lebytes + part, kaddr, size - part); \109 memcpy(lebytes + part, kaddr, size - part); \
106 return get_unaligned_le##bits(lebytes); \110 return get_unaligned_le##bits(lebytes); \
107} \111} \
@@ -110,53 +114,59 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ · 2 unmodified lines
110 u##bits val) \114 u##bits val) \
111{ \115{ \
112 const unsigned long member_offset = (unsigned long)ptr + off; \116 const unsigned long member_offset = (unsigned long)ptr + off; \
113 const unsigned long idx = get_eb_page_index(member_offset); \117 const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
114 const unsigned long oip = get_eb_offset_in_page(token->eb, \118 const unsigned long oil = get_eb_offset_in_folio(token->eb, \
115 member_offset); \119 member_offset);\
120 const int unit_size = folio_size(token->eb->folios[0]); \
121 const int unit_shift = folio_shift(token->eb->folios[0]); \
116 const int size = sizeof(u##bits); \122 const int size = sizeof(u##bits); \
117 u8 lebytes[sizeof(u##bits)]; \123 u8 lebytes[sizeof(u##bits)]; \
118 const int part = PAGE_SIZE - oip; \124 const int part = unit_size - oil; \
119 \125 \
120 ASSERT(token); \126 ASSERT(token); \
121 ASSERT(token->kaddr); \127 ASSERT(token->kaddr); \
122 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \128 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
123 if (token->offset <= member_offset && \129 if (token->offset <= member_offset && \
124 member_offset + size <= token->offset + PAGE_SIZE) { \130 member_offset + size <= token->offset + unit_size) { \
125 put_unaligned_le##bits(val, token->kaddr + oip); \131 put_unaligned_le##bits(val, token->kaddr + oil); \
126 return; \132 return; \
127 } \133 } \
128 token->kaddr = page_address(token->eb->pages[idx]); \134 token->kaddr = folio_address(token->eb->folios[idx]); \
129 token->offset = idx << PAGE_SHIFT; \135 token->offset = idx << unit_shift; \
130 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) { \136 if (INLINE_EXTENT_BUFFER_PAGES == 1 || \
131 put_unaligned_le##bits(val, token->kaddr + oip); \137 oil + size <= unit_size) { \
138 put_unaligned_le##bits(val, token->kaddr + oil); \
132 return; \139 return; \
133 } \140 } \
134 put_unaligned_le##bits(val, lebytes); \141 put_unaligned_le##bits(val, lebytes); \
135 memcpy(token->kaddr + oip, lebytes, part); \142 memcpy(token->kaddr + oil, lebytes, part); \
136 token->kaddr = page_address(token->eb->pages[idx + 1]); \143 token->kaddr = folio_address(token->eb->folios[idx + 1]); \
137 token->offset = (idx + 1) << PAGE_SHIFT; \144 token->offset = (idx + 1) << unit_shift; \
138 memcpy(token->kaddr, lebytes + part, size - part); \145 memcpy(token->kaddr, lebytes + part, size - part); \
139} \146} \
140void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \147void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
141 unsigned long off, u##bits val) \148 unsigned long off, u##bits val) \
142{ \149{ \
143 const unsigned long member_offset = (unsigned long)ptr + off; \150 const unsigned long member_offset = (unsigned long)ptr + off; \
144 const unsigned long oip = get_eb_offset_in_page(eb, member_offset); \151 const unsigned long idx = get_eb_folio_index(eb, member_offset);\
145 const unsigned long idx = get_eb_page_index(member_offset); \152 const unsigned long oil = get_eb_offset_in_folio(eb, \
146 char *kaddr = page_address(eb->pages[idx]); \153 member_offset);\
154 const int unit_size = folio_size(eb->folios[0]); \
155 char *kaddr = folio_address(eb->folios[idx]); \
147 const int size = sizeof(u##bits); \156 const int size = sizeof(u##bits); \
148 const int part = PAGE_SIZE - oip; \157 const int part = unit_size - oil; \
149 u8 lebytes[sizeof(u##bits)]; \158 u8 lebytes[sizeof(u##bits)]; \
150 \159 \
151 ASSERT(check_setget_bounds(eb, ptr, off, size)); \160 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
152 if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) { \161 if (INLINE_EXTENT_BUFFER_PAGES == 1 || \
153 put_unaligned_le##bits(val, kaddr + oip); \162 oil + size <= unit_size) { \
163 put_unaligned_le##bits(val, kaddr + oil); \
154 return; \164 return; \
155 } \165 } \
156 \166 \
157 put_unaligned_le##bits(val, lebytes); \167 put_unaligned_le##bits(val, lebytes); \
158 memcpy(kaddr + oip, lebytes, part); \168 memcpy(kaddr + oil, lebytes, part); \
159 kaddr = page_address(eb->pages[idx + 1]); \169 kaddr = folio_address(eb->folios[idx + 1]); \
160 memcpy(kaddr, lebytes + part, size - part); \170 memcpy(kaddr, lebytes + part, size - part); \
161}171}
162 172
fs/btrfs/accessors.h+2−2
@@ -90,14 +90,14 @@ static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\ · 89 unmodified lines
90#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \90#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
91static inline u##bits btrfs_##name(const struct extent_buffer *eb) \91static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
92{ \92{ \
93 const type *p = page_address(eb->pages[0]) + \93 const type *p = folio_address(eb->folios[0]) + \
94 offset_in_page(eb->start); \94 offset_in_page(eb->start); \
95 return get_unaligned_le##bits(&p->member); \95 return get_unaligned_le##bits(&p->member); \
96} \96} \
97static inline void btrfs_set_##name(const struct extent_buffer *eb, \97static inline void btrfs_set_##name(const struct extent_buffer *eb, \
98 u##bits val) \98 u##bits val) \
99{ \99{ \
100 type *p = page_address(eb->pages[0]) + offset_in_page(eb->start); \100 type *p = folio_address(eb->folios[0]) + offset_in_page(eb->start); \
101 put_unaligned_le##bits(val, &p->member); \101 put_unaligned_le##bits(val, &p->member); \
102}102}
103 103
fs/btrfs/bio.c+12−5
@@ -194,6 +194,12 @@ static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio, · 193 unmodified lines
194 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);194 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);
195 int mirror = repair_bbio->mirror_num;195 int mirror = repair_bbio->mirror_num;
196 196
197 /*
198 * We can only trigger this for data bio, which doesn't support larger
199 * folios yet.
200 */
201 ASSERT(folio_order(page_folio(bv->bv_page)) == 0);
202
197 if (repair_bbio->bio.bi_status ||203 if (repair_bbio->bio.bi_status ||
198 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {204 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {
199 bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ);205 bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ);
@@ -215,7 +221,7 @@ static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio, · 15 unmodified lines
215 btrfs_repair_io_failure(fs_info, btrfs_ino(inode),221 btrfs_repair_io_failure(fs_info, btrfs_ino(inode),
216 repair_bbio->file_offset, fs_info->sectorsize,222 repair_bbio->file_offset, fs_info->sectorsize,
217 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT,223 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT,
218 bv->bv_page, bv->bv_offset, mirror);224 page_folio(bv->bv_page), bv->bv_offset, mirror);
219 } while (mirror != fbio->bbio->mirror_num);225 } while (mirror != fbio->bbio->mirror_num);
220 226
221done:227done:
@@ -626,7 +632,7 @@ static bool should_async_write(struct btrfs_bio *bbio) · 404 unmodified lines
626/*632/*
627 * Submit bio to an async queue.633 * Submit bio to an async queue.
628 *634 *
629 * Return true if the work has been succesfuly submitted, else false.635 * Return true if the work has been successfully submitted, else false.
630 */636 */
631static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio,637static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio,
632 struct btrfs_io_context *bioc,638 struct btrfs_io_context *bioc,
@@ -767,8 +773,8 @@ void btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num) · 134 unmodified lines
767 * freeing the bio.773 * freeing the bio.
768 */774 */
769int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,775int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
770 u64 length, u64 logical, struct page *page,776 u64 length, u64 logical, struct folio *folio,
771 unsigned int pg_offset, int mirror_num)777 unsigned int folio_offset, int mirror_num)
772{778{
773 struct btrfs_io_stripe smap = { 0 };779 struct btrfs_io_stripe smap = { 0 };
774 struct bio_vec bvec;780 struct bio_vec bvec;
@@ -799,7 +805,8 @@ int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, · 24 unmodified lines
799 805
800 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);806 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
801 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;807 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
802 __bio_add_page(&bio, page, length, pg_offset);808 ret = bio_add_folio(&bio, folio, length, folio_offset);
809 ASSERT(ret);
803 ret = submit_bio_wait(&bio);810 ret = submit_bio_wait(&bio);
804 if (ret) {811 if (ret) {
805 /* try to remap that extent elsewhere? */812 /* try to remap that extent elsewhere? */
fs/btrfs/bio.h+2−2
@@ -105,7 +105,7 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status); · 104 unmodified lines
105void btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num);105void btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num);
106void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace);106void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace);
107int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,107int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
108 u64 length, u64 logical, struct page *page,108 u64 length, u64 logical, struct folio *folio,
109 unsigned int pg_offset, int mirror_num);109 unsigned int folio_offset, int mirror_num);
110 110
111#endif111#endif
fs/btrfs/block-group.c+69−100
@@ -168,7 +168,7 @@ void btrfs_put_block_group(struct btrfs_block_group *cache) · 167 unmodified lines
168 cache);168 cache);
169 169
170 kfree(cache->free_space_ctl);170 kfree(cache->free_space_ctl);
171 kfree(cache->physical_map);171 btrfs_free_chunk_map(cache->physical_map);
172 kfree(cache);172 kfree(cache);
173 }173 }
174}174}
@@ -1047,7 +1047,7 @@ static int remove_block_group_item(struct btrfs_trans_handle *trans, · 872 unmodified lines
1047}1047}
1048 1048
1049int btrfs_remove_block_group(struct btrfs_trans_handle *trans,1049int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1050 u64 group_start, struct extent_map *em)1050 struct btrfs_chunk_map *map)
1051{1051{
1052 struct btrfs_fs_info *fs_info = trans->fs_info;1052 struct btrfs_fs_info *fs_info = trans->fs_info;
1053 struct btrfs_path *path;1053 struct btrfs_path *path;
@@ -1059,10 +1059,10 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 5 unmodified lines
1059 int index;1059 int index;
1060 int factor;1060 int factor;
1061 struct btrfs_caching_control *caching_ctl = NULL;1061 struct btrfs_caching_control *caching_ctl = NULL;
1062 bool remove_em;1062 bool remove_map;
1063 bool remove_rsv = false;1063 bool remove_rsv = false;
1064 1064
1065 block_group = btrfs_lookup_block_group(fs_info, group_start);1065 block_group = btrfs_lookup_block_group(fs_info, map->start);
1066 BUG_ON(!block_group);1066 BUG_ON(!block_group);
1067 BUG_ON(!block_group->ro);1067 BUG_ON(!block_group->ro);
1068 1068
@@ -1252,7 +1252,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 183 unmodified lines
1252 * entries because we already removed them all when we called1252 * entries because we already removed them all when we called
1253 * btrfs_remove_free_space_cache().1253 * btrfs_remove_free_space_cache().
1254 *1254 *
1255 * And we must not remove the extent map from the fs_info->mapping_tree1255 * And we must not remove the chunk map from the fs_info->mapping_tree
1256 * to prevent the same logical address range and physical device space1256 * to prevent the same logical address range and physical device space
1257 * ranges from being reused for a new block group. This is needed to1257 * ranges from being reused for a new block group. This is needed to
1258 * avoid races with trimming and scrub.1258 * avoid races with trimming and scrub.
@@ -1268,19 +1268,11 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 9 unmodified lines
1268 * in place until the extents have been discarded completely when1268 * in place until the extents have been discarded completely when
1269 * the transaction commit has completed.1269 * the transaction commit has completed.
1270 */1270 */
1271 remove_em = (atomic_read(&block_group->frozen) == 0);1271 remove_map = (atomic_read(&block_group->frozen) == 0);
1272 spin_unlock(&block_group->lock);1272 spin_unlock(&block_group->lock);
1273 1273
1274 if (remove_em) {1274 if (remove_map)
1275 struct extent_map_tree *em_tree;1275 btrfs_remove_chunk_map(fs_info, map);
1276
1277 em_tree = &fs_info->mapping_tree;
1278 write_lock(&em_tree->lock);
1279 remove_extent_mapping(em_tree, em);
1280 write_unlock(&em_tree->lock);
1281 /* once for the tree */
1282 free_extent_map(em);
1283 }
1284 1276
1285out:1277out:
1286 /* Once for the lookup reference */1278 /* Once for the lookup reference */
@@ -1295,15 +1287,12 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( · 8 unmodified lines
1295 struct btrfs_fs_info *fs_info, const u64 chunk_offset)1287 struct btrfs_fs_info *fs_info, const u64 chunk_offset)
1296{1288{
1297 struct btrfs_root *root = btrfs_block_group_root(fs_info);1289 struct btrfs_root *root = btrfs_block_group_root(fs_info);
1298 struct extent_map_tree *em_tree = &fs_info->mapping_tree;1290 struct btrfs_chunk_map *map;
1299 struct extent_map *em;
1300 struct map_lookup *map;
1301 unsigned int num_items;1291 unsigned int num_items;
1302 1292
1303 read_lock(&em_tree->lock);1293 map = btrfs_find_chunk_map(fs_info, chunk_offset, 1);
1304 em = lookup_extent_mapping(em_tree, chunk_offset, 1);1294 ASSERT(map != NULL);
1305 read_unlock(&em_tree->lock);1295 ASSERT(map->start == chunk_offset);
1306 ASSERT(em && em->start == chunk_offset);
1307 1296
1308 /*1297 /*
1309 * We need to reserve 3 + N units from the metadata space info in order1298 * We need to reserve 3 + N units from the metadata space info in order
@@ -1324,9 +1313,8 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( · 14 unmodified lines
1324 * more device items and remove one chunk item), but this is done at1313 * more device items and remove one chunk item), but this is done at
1325 * btrfs_remove_chunk() through a call to check_system_chunk().1314 * btrfs_remove_chunk() through a call to check_system_chunk().
1326 */1315 */
1327 map = em->map_lookup;
1328 num_items = 3 + map->num_stripes;1316 num_items = 3 + map->num_stripes;
1329 free_extent_map(em);1317 btrfs_free_chunk_map(map);
1330 1318
1331 return btrfs_start_transaction_fallback_global_rsv(root, num_items);1319 return btrfs_start_transaction_fallback_global_rsv(root, num_items);
1332}1320}
@@ -1927,8 +1915,7 @@ void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg) · 594 unmodified lines
1927static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,1915static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
1928 struct btrfs_path *path)1916 struct btrfs_path *path)
1929{1917{
1930 struct extent_map_tree *em_tree;1918 struct btrfs_chunk_map *map;
1931 struct extent_map *em;
1932 struct btrfs_block_group_item bg;1919 struct btrfs_block_group_item bg;
1933 struct extent_buffer *leaf;1920 struct extent_buffer *leaf;
1934 int slot;1921 int slot;
@@ -1938,23 +1925,20 @@ static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key, · 3 unmodified lines
1938 slot = path->slots[0];1925 slot = path->slots[0];
1939 leaf = path->nodes[0];1926 leaf = path->nodes[0];
1940 1927
1941 em_tree = &fs_info->mapping_tree;1928 map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset);
1942 read_lock(&em_tree->lock);1929 if (!map) {
1943 em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1944 read_unlock(&em_tree->lock);
1945 if (!em) {
1946 btrfs_err(fs_info,1930 btrfs_err(fs_info,
1947 "logical %llu len %llu found bg but no related chunk",1931 "logical %llu len %llu found bg but no related chunk",
1948 key->objectid, key->offset);1932 key->objectid, key->offset);
1949 return -ENOENT;1933 return -ENOENT;
1950 }1934 }
1951 1935
1952 if (em->start != key->objectid || em->len != key->offset) {1936 if (map->start != key->objectid || map->chunk_len != key->offset) {
1953 btrfs_err(fs_info,1937 btrfs_err(fs_info,
1954 "block group %llu len %llu mismatch with chunk %llu len %llu",1938 "block group %llu len %llu mismatch with chunk %llu len %llu",
1955 key->objectid, key->offset, em->start, em->len);1939 key->objectid, key->offset, map->start, map->chunk_len);
1956 ret = -EUCLEAN;1940 ret = -EUCLEAN;
1957 goto out_free_em;1941 goto out_free_map;
1958 }1942 }
1959 1943
1960 read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),1944 read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot),
@@ -1962,16 +1946,16 @@ static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key, · 1 unmodified line
1962 flags = btrfs_stack_block_group_flags(&bg) &1946 flags = btrfs_stack_block_group_flags(&bg) &
1963 BTRFS_BLOCK_GROUP_TYPE_MASK;1947 BTRFS_BLOCK_GROUP_TYPE_MASK;
1964 1948
1965 if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {1949 if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1966 btrfs_err(fs_info,1950 btrfs_err(fs_info,
1967"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",1951"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
1968 key->objectid, key->offset, flags,1952 key->objectid, key->offset, flags,
1969 (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));1953 (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type));
1970 ret = -EUCLEAN;1954 ret = -EUCLEAN;
1971 }1955 }
1972 1956
1973out_free_em:1957out_free_map:
1974 free_extent_map(em);1958 btrfs_free_chunk_map(map);
1975 return ret;1959 return ret;
1976}1960}
1977 1961
@@ -2024,8 +2008,7 @@ static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) · 46 unmodified lines
2024int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,2008int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
2025 u64 physical, u64 **logical, int *naddrs, int *stripe_len)2009 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
2026{2010{
2027 struct extent_map *em;2011 struct btrfs_chunk_map *map;
2028 struct map_lookup *map;
2029 u64 *buf;2012 u64 *buf;
2030 u64 bytenr;2013 u64 bytenr;
2031 u64 data_stripe_length;2014 u64 data_stripe_length;
@@ -2033,14 +2016,13 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, · 1 unmodified line
2033 int i, nr = 0;2016 int i, nr = 0;
2034 int ret = 0;2017 int ret = 0;
2035 2018
2036 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);2019 map = btrfs_get_chunk_map(fs_info, chunk_start, 1);
2037 if (IS_ERR(em))2020 if (IS_ERR(map))
2038 return -EIO;2021 return -EIO;
2039 2022
2040 map = em->map_lookup;2023 data_stripe_length = map->stripe_size;
2041 data_stripe_length = em->orig_block_len;
2042 io_stripe_size = BTRFS_STRIPE_LEN;2024 io_stripe_size = BTRFS_STRIPE_LEN;
2043 chunk_start = em->start;2025 chunk_start = map->start;
2044 2026
2045 /* For RAID5/6 adjust to a full IO stripe length */2027 /* For RAID5/6 adjust to a full IO stripe length */
2046 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)2028 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
@@ -2094,7 +2076,7 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, · 47 unmodified lines
2094 *naddrs = nr;2076 *naddrs = nr;
2095 *stripe_len = io_stripe_size;2077 *stripe_len = io_stripe_size;
2096out:2078out:
2097 free_extent_map(em);2079 btrfs_free_chunk_map(map);
2098 return ret;2080 return ret;
2099}2081}
2100 2082
@@ -2199,49 +2181,47 @@ static struct btrfs_block_group *btrfs_create_block_group_cache( · 98 unmodified lines
2199 */2181 */
2200static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)2182static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
2201{2183{
2202 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
2203 struct extent_map *em;
2204 struct btrfs_block_group *bg;
2205 u64 start = 0;2184 u64 start = 0;
2206 int ret = 0;2185 int ret = 0;
2207 2186
2208 while (1) {2187 while (1) {
2209 read_lock(&map_tree->lock);2188 struct btrfs_chunk_map *map;
2189 struct btrfs_block_group *bg;
2190
2210 /*2191 /*
2211 * lookup_extent_mapping will return the first extent map2192 * btrfs_find_chunk_map() will return the first chunk map
2212 * intersecting the range, so setting @len to 1 is enough to2193 * intersecting the range, so setting @length to 1 is enough to
2213 * get the first chunk.2194 * get the first chunk.
2214 */2195 */
2215 em = lookup_extent_mapping(map_tree, start, 1);2196 map = btrfs_find_chunk_map(fs_info, start, 1);
2216 read_unlock(&map_tree->lock);2197 if (!map)
2217 if (!em)
2218 break;2198 break;
2219 2199
2220 bg = btrfs_lookup_block_group(fs_info, em->start);2200 bg = btrfs_lookup_block_group(fs_info, map->start);
2221 if (!bg) {2201 if (!bg) {
2222 btrfs_err(fs_info,2202 btrfs_err(fs_info,
2223 "chunk start=%llu len=%llu doesn't have corresponding block group",2203 "chunk start=%llu len=%llu doesn't have corresponding block group",
2224 em->start, em->len);2204 map->start, map->chunk_len);
2225 ret = -EUCLEAN;2205 ret = -EUCLEAN;
2226 free_extent_map(em);2206 btrfs_free_chunk_map(map);
2227 break;2207 break;
2228 }2208 }
2229 if (bg->start != em->start || bg->length != em->len ||2209 if (bg->start != map->start || bg->length != map->chunk_len ||
2230 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=2210 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
2231 (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {2211 (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
2232 btrfs_err(fs_info,2212 btrfs_err(fs_info,
2233"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",2213"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
2234 em->start, em->len,2214 map->start, map->chunk_len,
2235 em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,2215 map->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
2236 bg->start, bg->length,2216 bg->start, bg->length,
2237 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);2217 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
2238 ret = -EUCLEAN;2218 ret = -EUCLEAN;
2239 free_extent_map(em);2219 btrfs_free_chunk_map(map);
2240 btrfs_put_block_group(bg);2220 btrfs_put_block_group(bg);
2241 break;2221 break;
2242 }2222 }
2243 start = em->start + em->len;2223 start = map->start + map->chunk_len;
2244 free_extent_map(em);2224 btrfs_free_chunk_map(map);
2245 btrfs_put_block_group(bg);2225 btrfs_put_block_group(bg);
2246 }2226 }
2247 return ret;2227 return ret;
@@ -2369,28 +2349,25 @@ error: · 121 unmodified lines
2369 2349
2370static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)2350static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
2371{2351{
2372 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
2373 struct rb_node *node;2352 struct rb_node *node;
2374 int ret = 0;2353 int ret = 0;
2375 2354
2376 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {2355 for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) {
2377 struct extent_map *em;2356 struct btrfs_chunk_map *map;
2378 struct map_lookup *map;
2379 struct btrfs_block_group *bg;2357 struct btrfs_block_group *bg;
2380 2358
2381 em = rb_entry(node, struct extent_map, rb_node);2359 map = rb_entry(node, struct btrfs_chunk_map, rb_node);
2382 map = em->map_lookup;2360 bg = btrfs_create_block_group_cache(fs_info, map->start);
2383 bg = btrfs_create_block_group_cache(fs_info, em->start);
2384 if (!bg) {2361 if (!bg) {
2385 ret = -ENOMEM;2362 ret = -ENOMEM;
2386 break;2363 break;
2387 }2364 }
2388 2365
2389 /* Fill dummy cache as FULL */2366 /* Fill dummy cache as FULL */
2390 bg->length = em->len;2367 bg->length = map->chunk_len;
2391 bg->flags = map->type;2368 bg->flags = map->type;
2392 bg->cached = BTRFS_CACHE_FINISHED;2369 bg->cached = BTRFS_CACHE_FINISHED;
2393 bg->used = em->len;2370 bg->used = map->chunk_len;
2394 bg->flags = map->type;2371 bg->flags = map->type;
2395 ret = btrfs_add_block_group_cache(fs_info, bg);2372 ret = btrfs_add_block_group_cache(fs_info, bg);
2396 /*2373 /*
@@ -2618,19 +2595,14 @@ static int insert_dev_extents(struct btrfs_trans_handle *trans, · 221 unmodified lines
2618{2595{
2619 struct btrfs_fs_info *fs_info = trans->fs_info;2596 struct btrfs_fs_info *fs_info = trans->fs_info;
2620 struct btrfs_device *device;2597 struct btrfs_device *device;
2621 struct extent_map *em;2598 struct btrfs_chunk_map *map;
2622 struct map_lookup *map;
2623 u64 dev_offset;2599 u64 dev_offset;
2624 u64 stripe_size;
2625 int i;2600 int i;
2626 int ret = 0;2601 int ret = 0;
2627 2602
2628 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);2603 map = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
2629 if (IS_ERR(em))2604 if (IS_ERR(map))
2630 return PTR_ERR(em);2605 return PTR_ERR(map);
2631
2632 map = em->map_lookup;
2633 stripe_size = em->orig_block_len;
2634 2606
2635 /*2607 /*
2636 * Take the device list mutex to prevent races with the final phase of2608 * Take the device list mutex to prevent races with the final phase of
@@ -2647,13 +2619,13 @@ static int insert_dev_extents(struct btrfs_trans_handle *trans, · 10 unmodified lines
2647 dev_offset = map->stripes[i].physical;2619 dev_offset = map->stripes[i].physical;
2648 2620
2649 ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,2621 ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
2650 stripe_size);2622 map->stripe_size);
2651 if (ret)2623 if (ret)
2652 break;2624 break;
2653 }2625 }
2654 mutex_unlock(&fs_info->fs_devices->device_list_mutex);2626 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2655 2627
2656 free_extent_map(em);2628 btrfs_free_chunk_map(map);
2657 return ret;2629 return ret;
2658}2630}
2659 2631
@@ -2910,7 +2882,7 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, · 250 unmodified lines
2910 goto unlock_out;2882 goto unlock_out;
2911 2883
2912 /*2884 /*
2913 * Skip chunk alloction if the bg is SYSTEM, this is to avoid system2885 * Skip chunk allocation if the bg is SYSTEM, this is to avoid system
2914 * chunk allocation storm to exhaust the system chunk array. Otherwise2886 * chunk allocation storm to exhaust the system chunk array. Otherwise
2915 * we still want to try our best to mark the block group read-only.2887 * we still want to try our best to mark the block group read-only.
2916 */2888 */
@@ -4406,8 +4378,6 @@ void btrfs_freeze_block_group(struct btrfs_block_group *cache) · 1489 unmodified lines
4406void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)4378void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
4407{4379{
4408 struct btrfs_fs_info *fs_info = block_group->fs_info;4380 struct btrfs_fs_info *fs_info = block_group->fs_info;
4409 struct extent_map_tree *em_tree;
4410 struct extent_map *em;
4411 bool cleanup;4381 bool cleanup;
4412 4382
4413 spin_lock(&block_group->lock);4383 spin_lock(&block_group->lock);
@@ -4416,17 +4386,16 @@ void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group) · 2 unmodified lines
4416 spin_unlock(&block_group->lock);4386 spin_unlock(&block_group->lock);
4417 4387
4418 if (cleanup) {4388 if (cleanup) {
4419 em_tree = &fs_info->mapping_tree;4389 struct btrfs_chunk_map *map;
4420 write_lock(&em_tree->lock);4390
4421 em = lookup_extent_mapping(em_tree, block_group->start,4391 map = btrfs_find_chunk_map(fs_info, block_group->start, 1);
4422 1);4392 /* Logic error, can't happen. */
4423 BUG_ON(!em); /* logic error, can't happen */4393 ASSERT(map);
4424 remove_extent_mapping(em_tree, em);4394
4425 write_unlock(&em_tree->lock);4395 btrfs_remove_chunk_map(fs_info, map);
4426 4396
4427 /* once for us and once for the tree */4397 /* Once for our lookup reference. */
4428 free_extent_map(em);4398 btrfs_free_chunk_map(map);
4429 free_extent_map(em);
4430 4399
4431 /*4400 /*
4432 * We may have left one free space entry and other possible4401 * We may have left one free space entry and other possible
fs/btrfs/block-group.h+4−2
@@ -5,6 +5,8 @@ · 4 unmodified lines
5 5
6#include "free-space-cache.h"6#include "free-space-cache.h"
7 7
8struct btrfs_chunk_map;
9
8enum btrfs_disk_cache_state {10enum btrfs_disk_cache_state {
9 BTRFS_DC_WRITTEN,11 BTRFS_DC_WRITTEN,
10 BTRFS_DC_ERROR,12 BTRFS_DC_ERROR,
@@ -243,7 +245,7 @@ struct btrfs_block_group { · 232 unmodified lines
243 u64 zone_unusable;245 u64 zone_unusable;
244 u64 zone_capacity;246 u64 zone_capacity;
245 u64 meta_write_pointer;247 u64 meta_write_pointer;
246 struct map_lookup *physical_map;248 struct btrfs_chunk_map *physical_map;
247 struct list_head active_bg_list;249 struct list_head active_bg_list;
248 struct work_struct zone_finish_work;250 struct work_struct zone_finish_work;
249 struct extent_buffer *last_eb;251 struct extent_buffer *last_eb;
@@ -297,7 +299,7 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( · 47 unmodified lines
297 struct btrfs_fs_info *fs_info,299 struct btrfs_fs_info *fs_info,
298 const u64 chunk_offset);300 const u64 chunk_offset);
299int btrfs_remove_block_group(struct btrfs_trans_handle *trans,301int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
300 u64 group_start, struct extent_map *em);302 struct btrfs_chunk_map *map);
301void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);303void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
302void btrfs_mark_bg_unused(struct btrfs_block_group *bg);304void btrfs_mark_bg_unused(struct btrfs_block_group *bg);
303void btrfs_reclaim_bgs_work(struct work_struct *work);305void btrfs_reclaim_bgs_work(struct work_struct *work);
fs/btrfs/btrfs_inode.h+7−3
@@ -69,6 +69,8 @@ enum { · 68 unmodified lines
69 BTRFS_INODE_VERITY_IN_PROGRESS,69 BTRFS_INODE_VERITY_IN_PROGRESS,
70 /* Set when this inode is a free space inode. */70 /* Set when this inode is a free space inode. */
71 BTRFS_INODE_FREE_SPACE_INODE,71 BTRFS_INODE_FREE_SPACE_INODE,
72 /* Set when there are no capabilities in XATTs for the inode. */
73 BTRFS_INODE_NO_CAP_XATTR,
72};74};
73 75
74/* in memory btrfs inode */76/* in memory btrfs inode */
@@ -107,9 +109,11 @@ struct btrfs_inode { · 32 unmodified lines
107 109
108 /*110 /*
109 * Keep track of where the inode has extent items mapped in order to111 * Keep track of where the inode has extent items mapped in order to
110 * make sure the i_size adjustments are accurate112 * make sure the i_size adjustments are accurate. Not required when the
113 * filesystem is NO_HOLES, the status can't be set while mounted as
114 * it's a mkfs-time feature.
111 */115 */
112 struct extent_io_tree file_extent_tree;116 struct extent_io_tree *file_extent_tree;
113 117
114 /* held while logging the inode in tree-log.c */118 /* held while logging the inode in tree-log.c */
115 struct mutex log_mutex;119 struct mutex log_mutex;
@@ -487,7 +491,7 @@ struct inode *btrfs_iget_path(struct super_block *s, u64 ino, · 371 unmodified lines
487struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);491struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
488struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,492struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
489 struct page *page, size_t pg_offset,493 struct page *page, size_t pg_offset,
490 u64 start, u64 end);494 u64 start, u64 len);
491int btrfs_update_inode(struct btrfs_trans_handle *trans,495int btrfs_update_inode(struct btrfs_trans_handle *trans,
492 struct btrfs_inode *inode);496 struct btrfs_inode *inode);
493int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,497int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
fs/btrfs/compression.c+128−11
@@ -20,6 +20,7 @@ · 19 unmodified lines
20#include <linux/slab.h>20#include <linux/slab.h>
21#include <linux/sched/mm.h>21#include <linux/sched/mm.h>
22#include <linux/log2.h>22#include <linux/log2.h>
23#include <linux/shrinker.h>
23#include <crypto/hash.h>24#include <crypto/hash.h>
24#include "misc.h"25#include "misc.h"
25#include "ctree.h"26#include "ctree.h"
@@ -163,13 +164,107 @@ static int compression_decompress(int type, struct list_head *ws, · 137 unmodified lines
163static void btrfs_free_compressed_pages(struct compressed_bio *cb)164static void btrfs_free_compressed_pages(struct compressed_bio *cb)
164{165{
165 for (unsigned int i = 0; i < cb->nr_pages; i++)166 for (unsigned int i = 0; i < cb->nr_pages; i++)
166 put_page(cb->compressed_pages[i]);167 btrfs_free_compr_page(cb->compressed_pages[i]);
167 kfree(cb->compressed_pages);168 kfree(cb->compressed_pages);
168}169}
169 170
170static int btrfs_decompress_bio(struct compressed_bio *cb);171static int btrfs_decompress_bio(struct compressed_bio *cb);
171 172
172static void end_compressed_bio_read(struct btrfs_bio *bbio)173/*
174 * Global cache of last unused pages for compression/decompression.
175 */
176static struct btrfs_compr_pool {
177 struct shrinker *shrinker;
178 spinlock_t lock;
179 struct list_head list;
180 int count;
181 int thresh;
182} compr_pool;
183
184static unsigned long btrfs_compr_pool_count(struct shrinker *sh, struct shrink_control *sc)
185{
186 int ret;
187
188 /*
189 * We must not read the values more than once if 'ret' gets expanded in
190 * the return statement so we don't accidentally return a negative
191 * number, even if the first condition finds it positive.
192 */
193 ret = READ_ONCE(compr_pool.count) - READ_ONCE(compr_pool.thresh);
194
195 return ret > 0 ? ret : 0;
196}
197
198static unsigned long btrfs_compr_pool_scan(struct shrinker *sh, struct shrink_control *sc)
199{
200 struct list_head remove;
201 struct list_head *tmp, *next;
202 int freed;
203
204 if (compr_pool.count == 0)
205 return SHRINK_STOP;
206
207 INIT_LIST_HEAD(&remove);
208
209 /* For now, just simply drain the whole list. */
210 spin_lock(&compr_pool.lock);
211 list_splice_init(&compr_pool.list, &remove);
212 freed = compr_pool.count;
213 compr_pool.count = 0;
214 spin_unlock(&compr_pool.lock);
215
216 list_for_each_safe(tmp, next, &remove) {
217 struct page *page = list_entry(tmp, struct page, lru);
218
219 ASSERT(page_ref_count(page) == 1);
220 put_page(page);
221 }
222
223 return freed;
224}
225
226/*
227 * Common wrappers for page allocation from compression wrappers
228 */
229struct page *btrfs_alloc_compr_page(void)
230{
231 struct page *page = NULL;
232
233 spin_lock(&compr_pool.lock);
234 if (compr_pool.count > 0) {
235 page = list_first_entry(&compr_pool.list, struct page, lru);
236 list_del_init(&page->lru);
237 compr_pool.count--;
238 }
239 spin_unlock(&compr_pool.lock);
240
241 if (page)
242 return page;
243
244 return alloc_page(GFP_NOFS);
245}
246
247void btrfs_free_compr_page(struct page *page)
248{
249 bool do_free = false;
250
251 spin_lock(&compr_pool.lock);
252 if (compr_pool.count > compr_pool.thresh) {
253 do_free = true;
254 } else {
255 list_add(&page->lru, &compr_pool.list);
256 compr_pool.count++;
257 }
258 spin_unlock(&compr_pool.lock);
259
260 if (!do_free)
261 return;
262
263 ASSERT(page_ref_count(page) == 1);
264 put_page(page);
265}
266
267static void end_bbio_comprssed_read(struct btrfs_bio *bbio)
173{268{
174 struct compressed_bio *cb = to_compressed_bio(bbio);269 struct compressed_bio *cb = to_compressed_bio(bbio);
175 blk_status_t status = bbio->bio.bi_status;270 blk_status_t status = bbio->bio.bi_status;
@@ -211,8 +306,8 @@ static noinline void end_compressed_writeback(const struct compressed_bio *cb) · 35 unmodified lines
211 for (i = 0; i < ret; i++) {306 for (i = 0; i < ret; i++) {
212 struct folio *folio = fbatch.folios[i];307 struct folio *folio = fbatch.folios[i];
213 308
214 btrfs_page_clamp_clear_writeback(fs_info, &folio->page,309 btrfs_folio_clamp_clear_writeback(fs_info, folio,
215 cb->start, cb->len);310 cb->start, cb->len);
216 }311 }
217 folio_batch_release(&fbatch);312 folio_batch_release(&fbatch);
218 }313 }
@@ -242,7 +337,7 @@ static void btrfs_finish_compressed_write_work(struct work_struct *work) · 23 unmodified lines
242 * This also calls the writeback end hooks for the file pages so that metadata337 * This also calls the writeback end hooks for the file pages so that metadata
243 * and checksums can be updated in the file.338 * and checksums can be updated in the file.
244 */339 */
245static void end_compressed_bio_write(struct btrfs_bio *bbio)340static void end_bbio_comprssed_write(struct btrfs_bio *bbio)
246{341{
247 struct compressed_bio *cb = to_compressed_bio(bbio);342 struct compressed_bio *cb = to_compressed_bio(bbio);
248 struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;343 struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
@@ -289,7 +384,7 @@ void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered, · 40 unmodified lines
289 384
290 cb = alloc_compressed_bio(inode, ordered->file_offset,385 cb = alloc_compressed_bio(inode, ordered->file_offset,
291 REQ_OP_WRITE | write_flags,386 REQ_OP_WRITE | write_flags,
292 end_compressed_bio_write);387 end_bbio_comprssed_write);
293 cb->start = ordered->file_offset;388 cb->start = ordered->file_offset;
294 cb->len = ordered->num_bytes;389 cb->len = ordered->num_bytes;
295 cb->compressed_pages = compressed_pages;390 cb->compressed_pages = compressed_pages;
@@ -446,7 +541,8 @@ static noinline int add_ra_bio_pages(struct inode *inode, · 150 unmodified lines
446 * subpage::readers and to unlock the page.541 * subpage::readers and to unlock the page.
447 */542 */
448 if (fs_info->sectorsize < PAGE_SIZE)543 if (fs_info->sectorsize < PAGE_SIZE)
449 btrfs_subpage_start_reader(fs_info, page, cur, add_size);544 btrfs_subpage_start_reader(fs_info, page_folio(page),
545 cur, add_size);
450 put_page(page);546 put_page(page);
451 cur += add_size;547 cur += add_size;
452 }548 }
@@ -489,11 +585,11 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) · 36 unmodified lines
489 goto out;585 goto out;
490 }586 }
491 587
492 ASSERT(em->compress_type != BTRFS_COMPRESS_NONE);588 ASSERT(extent_map_is_compressed(em));
493 compressed_len = em->block_len;589 compressed_len = em->block_len;
494 590
495 cb = alloc_compressed_bio(inode, file_offset, REQ_OP_READ,591 cb = alloc_compressed_bio(inode, file_offset, REQ_OP_READ,
496 end_compressed_bio_read);592 end_bbio_comprssed_read);
497 593
498 cb->start = em->orig_start;594 cb->start = em->orig_start;
499 em_len = em->len;595 em_len = em->len;
@@ -501,7 +597,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) · 1 unmodified line
501 597
502 cb->len = bbio->bio.bi_iter.bi_size;598 cb->len = bbio->bio.bi_iter.bi_size;
503 cb->compressed_len = compressed_len;599 cb->compressed_len = compressed_len;
504 cb->compress_type = em->compress_type;600 cb->compress_type = extent_map_compression(em);
505 cb->orig_bbio = bbio;601 cb->orig_bbio = bbio;
506 602
507 free_extent_map(em);603 free_extent_map(em);
@@ -513,7 +609,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) · 5 unmodified lines
513 goto out_free_bio;609 goto out_free_bio;
514 }610 }
515 611
516 ret2 = btrfs_alloc_page_array(cb->nr_pages, cb->compressed_pages);612 ret2 = btrfs_alloc_page_array(cb->nr_pages, cb->compressed_pages, 0);
517 if (ret2) {613 if (ret2) {
518 ret = BLK_STS_RESOURCE;614 ret = BLK_STS_RESOURCE;
519 goto out_free_compressed_pages;615 goto out_free_compressed_pages;
@@ -960,15 +1056,36 @@ int __init btrfs_init_compress(void) · 440 unmodified lines
960 offsetof(struct compressed_bio, bbio.bio),1056 offsetof(struct compressed_bio, bbio.bio),
961 BIOSET_NEED_BVECS))1057 BIOSET_NEED_BVECS))
962 return -ENOMEM;1058 return -ENOMEM;
1059
1060 compr_pool.shrinker = shrinker_alloc(SHRINKER_NONSLAB, "btrfs-compr-pages");
1061 if (!compr_pool.shrinker)
1062 return -ENOMEM;
1063
963 btrfs_init_workspace_manager(BTRFS_COMPRESS_NONE);1064 btrfs_init_workspace_manager(BTRFS_COMPRESS_NONE);
964 btrfs_init_workspace_manager(BTRFS_COMPRESS_ZLIB);1065 btrfs_init_workspace_manager(BTRFS_COMPRESS_ZLIB);
965 btrfs_init_workspace_manager(BTRFS_COMPRESS_LZO);1066 btrfs_init_workspace_manager(BTRFS_COMPRESS_LZO);
966 zstd_init_workspace_manager();1067 zstd_init_workspace_manager();
1068
1069 spin_lock_init(&compr_pool.lock);
1070 INIT_LIST_HEAD(&compr_pool.list);
1071 compr_pool.count = 0;
1072 /* 128K / 4K = 32, for 8 threads is 256 pages. */
1073 compr_pool.thresh = BTRFS_MAX_COMPRESSED / PAGE_SIZE * 8;
1074 compr_pool.shrinker->count_objects = btrfs_compr_pool_count;
1075 compr_pool.shrinker->scan_objects = btrfs_compr_pool_scan;
1076 compr_pool.shrinker->batch = 32;
1077 compr_pool.shrinker->seeks = DEFAULT_SEEKS;
1078 shrinker_register(compr_pool.shrinker);
1079
967 return 0;1080 return 0;
968}1081}
969 1082
970void __cold btrfs_exit_compress(void)1083void __cold btrfs_exit_compress(void)
971{1084{
1085 /* For now scan drains all pages and does not touch the parameters. */
1086 btrfs_compr_pool_scan(NULL, NULL);
1087 shrinker_free(compr_pool.shrinker);
1088
972 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_NONE);1089 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_NONE);
973 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_ZLIB);1090 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_ZLIB);
974 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_LZO);1091 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_LZO);
fs/btrfs/compression.h+5−0
@@ -32,6 +32,8 @@ static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0); · 31 unmodified lines
32 32
33#define BTRFS_ZLIB_DEFAULT_LEVEL 333#define BTRFS_ZLIB_DEFAULT_LEVEL 3
34 34
35struct page;
36
35struct compressed_bio {37struct compressed_bio {
36 /* Number of compressed pages in the array */38 /* Number of compressed pages in the array */
37 unsigned int nr_pages;39 unsigned int nr_pages;
@@ -96,6 +98,9 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio); · 58 unmodified lines
96 98
97unsigned int btrfs_compress_str2level(unsigned int type, const char *str);99unsigned int btrfs_compress_str2level(unsigned int type, const char *str);
98 100
101struct page *btrfs_alloc_compr_page(void);
102void btrfs_free_compr_page(struct page *page);
103
99enum btrfs_compression_type {104enum btrfs_compression_type {
100 BTRFS_COMPRESS_NONE = 0,105 BTRFS_COMPRESS_NONE = 0,
101 BTRFS_COMPRESS_ZLIB = 1,106 BTRFS_COMPRESS_ZLIB = 1,
fs/btrfs/ctree.c+36−27
@@ -370,33 +370,41 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 369 unmodified lines
370/*370/*
371 * check if the tree block can be shared by multiple trees371 * check if the tree block can be shared by multiple trees
372 */372 */
373int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,373bool btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
374 struct btrfs_root *root,374 struct btrfs_root *root,
375 struct extent_buffer *buf)375 struct extent_buffer *buf)
376{376{
377 const u64 buf_gen = btrfs_header_generation(buf);
378
377 /*379 /*
378 * Tree blocks not in shareable trees and tree roots are never shared.380 * Tree blocks not in shareable trees and tree roots are never shared.
379 * If a block was allocated after the last snapshot and the block was381 * If a block was allocated after the last snapshot and the block was
380 * not allocated by tree relocation, we know the block is not shared.382 * not allocated by tree relocation, we know the block is not shared.
381 */383 */
382 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
383 buf != root->node &&
384 (btrfs_header_generation(buf) <=
385 btrfs_root_last_snapshot(&root->root_item) ||
386 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
387 if (buf != root->commit_root)
388 return 1;
389 /*
390 * An extent buffer that used to be the commit root may still be
391 * shared because the tree height may have increased and it
392 * became a child of a higher level root. This can happen when
393 * snapshotting a subvolume created in the current transaction.
394 */
395 if (btrfs_header_generation(buf) == trans->transid)
396 return 1;
397 }
398 384
399 return 0;385 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
386 return false;
387
388 if (buf == root->node)
389 return false;
390
391 if (buf_gen > btrfs_root_last_snapshot(&root->root_item) &&
392 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
393 return false;
394
395 if (buf != root->commit_root)
396 return true;
397
398 /*
399 * An extent buffer that used to be the commit root may still be shared
400 * because the tree height may have increased and it became a child of a
401 * higher level root. This can happen when snapshotting a subvolume
402 * created in the current transaction.
403 */
404 if (buf_gen == trans->transid)
405 return true;
406
407 return false;
400}408}
401 409
402static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,410static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
@@ -812,7 +820,8 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 409 unmodified lines
812 }820 }
813 821
814 while (low < high) {822 while (low < high) {
815 unsigned long oip;823 const int unit_size = folio_size(eb->folios[0]);
824 unsigned long oil;
816 unsigned long offset;825 unsigned long offset;
817 struct btrfs_disk_key *tmp;826 struct btrfs_disk_key *tmp;
818 struct btrfs_disk_key unaligned;827 struct btrfs_disk_key unaligned;
@@ -820,14 +829,14 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 1 unmodified line
820 829
821 mid = (low + high) / 2;830 mid = (low + high) / 2;
822 offset = p + mid * item_size;831 offset = p + mid * item_size;
823 oip = offset_in_page(offset);832 oil = get_eb_offset_in_folio(eb, offset);
824 833
825 if (oip + key_size <= PAGE_SIZE) {834 if (oil + key_size <= unit_size) {
826 const unsigned long idx = get_eb_page_index(offset);835 const unsigned long idx = get_eb_folio_index(eb, offset);
827 char *kaddr = page_address(eb->pages[idx]);836 char *kaddr = folio_address(eb->folios[idx]);
828 837
829 oip = get_eb_offset_in_page(eb, offset);838 oil = get_eb_offset_in_folio(eb, offset);
830 tmp = (struct btrfs_disk_key *)(kaddr + oip);839 tmp = (struct btrfs_disk_key *)(kaddr + oil);
831 } else {840 } else {
832 read_extent_buffer(eb, &unaligned, offset, key_size);841 read_extent_buffer(eb, &unaligned, offset, key_size);
833 tmp = &unaligned;842 tmp = &unaligned;
fs/btrfs/ctree.h+6−11
@@ -212,8 +212,6 @@ struct btrfs_root { · 211 unmodified lines
212 212
213 u64 last_trans;213 u64 last_trans;
214 214
215 u32 type;
216
217 u64 free_objectid;215 u64 free_objectid;
218 216
219 struct btrfs_key defrag_progress;217 struct btrfs_key defrag_progress;
@@ -224,18 +222,15 @@ struct btrfs_root { · 4 unmodified lines
224 222
225 struct list_head root_list;223 struct list_head root_list;
226 224
227 spinlock_t log_extents_lock[2];
228 struct list_head logged_list[2];
229
230 spinlock_t inode_lock;225 spinlock_t inode_lock;
231 /* red-black tree that keeps track of in-memory inodes */226 /* red-black tree that keeps track of in-memory inodes */
232 struct rb_root inode_tree;227 struct rb_root inode_tree;
233 228
234 /*229 /*
235 * radix tree that keeps track of delayed nodes of every inode,230 * Xarray that keeps track of delayed nodes of every inode, protected
236 * protected by inode_lock231 * by @inode_lock.
237 */232 */
238 struct radix_tree_root delayed_nodes_tree;233 struct xarray delayed_nodes;
239 /*234 /*
240 * right now this just gets used so that a root has its own devid235 * right now this just gets used so that a root has its own devid
241 * for stat. It may be used for more later236 * for stat. It may be used for more later
@@ -561,9 +556,9 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 319 unmodified lines
561 struct btrfs_root *root,556 struct btrfs_root *root,
562 struct extent_buffer *buf,557 struct extent_buffer *buf,
563 struct extent_buffer **cow_ret, u64 new_root_objectid);558 struct extent_buffer **cow_ret, u64 new_root_objectid);
564int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,559bool btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
565 struct btrfs_root *root,560 struct btrfs_root *root,
566 struct extent_buffer *buf);561 struct extent_buffer *buf);
567int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,562int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
568 struct btrfs_path *path, int level, int slot);563 struct btrfs_path *path, int level, int slot);
569void btrfs_extend_item(struct btrfs_trans_handle *trans,564void btrfs_extend_item(struct btrfs_trans_handle *trans,
fs/btrfs/defrag.c+6−7
@@ -775,7 +775,7 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, · 774 unmodified lines
775 * this em, as either we don't care about the generation, or the775 * this em, as either we don't care about the generation, or the
776 * merged extent map will be rejected anyway.776 * merged extent map will be rejected anyway.
777 */777 */
778 if (em && test_bit(EXTENT_FLAG_MERGED, &em->flags) &&778 if (em && (em->flags & EXTENT_FLAG_MERGED) &&
779 newer_than && em->generation >= newer_than) {779 newer_than && em->generation >= newer_than) {
780 free_extent_map(em);780 free_extent_map(em);
781 em = NULL;781 em = NULL;
@@ -802,7 +802,7 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, · 20 unmodified lines
802static u32 get_extent_max_capacity(const struct btrfs_fs_info *fs_info,802static u32 get_extent_max_capacity(const struct btrfs_fs_info *fs_info,
803 const struct extent_map *em)803 const struct extent_map *em)
804{804{
805 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))805 if (extent_map_is_compressed(em))
806 return BTRFS_MAX_COMPRESSED;806 return BTRFS_MAX_COMPRESSED;
807 return fs_info->max_extent_size;807 return fs_info->max_extent_size;
808}808}
@@ -828,7 +828,7 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em, · 19 unmodified lines
828 /* No more em or hole */828 /* No more em or hole */
829 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)829 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
830 goto out;830 goto out;
831 if (test_bit(EXTENT_FLAG_PREALLOC, &next->flags))831 if (next->flags & EXTENT_FLAG_PREALLOC)
832 goto out;832 goto out;
833 /*833 /*
834 * If the next extent is at its max capacity, defragging current extent834 * If the next extent is at its max capacity, defragging current extent
@@ -996,10 +996,9 @@ static int defrag_collect_targets(struct btrfs_inode *inode, · 161 unmodified lines
996 em->len <= inode->root->fs_info->max_inline)996 em->len <= inode->root->fs_info->max_inline)
997 goto next;997 goto next;
998 998
999 /* Skip hole/delalloc/preallocated extents */999 /* Skip holes and preallocated extents. */
1000 if (em->block_start == EXTENT_MAP_HOLE ||1000 if (em->block_start == EXTENT_MAP_HOLE ||
1001 em->block_start == EXTENT_MAP_DELALLOC ||1001 (em->flags & EXTENT_FLAG_PREALLOC))
1002 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1003 goto next;1002 goto next;
1004 1003
1005 /* Skip older extent */1004 /* Skip older extent */
@@ -1190,7 +1189,7 @@ static int defrag_one_locked_target(struct btrfs_inode *inode, · 184 unmodified lines
1190 /* Update the page status */1189 /* Update the page status */
1191 for (i = start_index - first_index; i <= last_index - first_index; i++) {1190 for (i = start_index - first_index; i <= last_index - first_index; i++) {
1192 ClearPageChecked(pages[i]);1191 ClearPageChecked(pages[i]);
1193 btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len);1192 btrfs_folio_clamp_set_dirty(fs_info, page_folio(pages[i]), start, len);
1194 }1193 }
1195 btrfs_delalloc_release_extents(inode, len);1194 btrfs_delalloc_release_extents(inode, len);
1196 extent_changeset_free(data_reserved);1195 extent_changeset_free(data_reserved);
fs/btrfs/delayed-inode.c+59−50
@@ -71,7 +71,7 @@ static struct btrfs_delayed_node *btrfs_get_delayed_node( · 70 unmodified lines
71 }71 }
72 72
73 spin_lock(&root->inode_lock);73 spin_lock(&root->inode_lock);
74 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);74 node = xa_load(&root->delayed_nodes, ino);
75 75
76 if (node) {76 if (node) {
77 if (btrfs_inode->delayed_node) {77 if (btrfs_inode->delayed_node) {
@@ -83,9 +83,9 @@ static struct btrfs_delayed_node *btrfs_get_delayed_node( · 5 unmodified lines
83 83
84 /*84 /*
85 * It's possible that we're racing into the middle of removing85 * It's possible that we're racing into the middle of removing
86 * this node from the radix tree. In this case, the refcount86 * this node from the xarray. In this case, the refcount
87 * was zero and it should never go back to one. Just return87 * was zero and it should never go back to one. Just return
88 * NULL like it was never in the radix at all; our release88 * NULL like it was never in the xarray at all; our release
89 * function is in the process of removing it.89 * function is in the process of removing it.
90 *90 *
91 * Some implementations of refcount_inc refuse to bump the91 * Some implementations of refcount_inc refuse to bump the
@@ -93,7 +93,7 @@ static struct btrfs_delayed_node *btrfs_get_delayed_node( · 1 unmodified line
93 * here, refcount_inc() may decide to just WARN_ONCE() instead93 * here, refcount_inc() may decide to just WARN_ONCE() instead
94 * of actually bumping the refcount.94 * of actually bumping the refcount.
95 *95 *
96 * If this node is properly in the radix, we want to bump the96 * If this node is properly in the xarray, we want to bump the
97 * refcount twice, once for the inode and once for this get97 * refcount twice, once for the inode and once for this get
98 * operation.98 * operation.
99 */99 */
@@ -120,6 +120,7 @@ static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( · 20 unmodified lines
120 struct btrfs_root *root = btrfs_inode->root;120 struct btrfs_root *root = btrfs_inode->root;
121 u64 ino = btrfs_ino(btrfs_inode);121 u64 ino = btrfs_ino(btrfs_inode);
122 int ret;122 int ret;
123 void *ptr;
123 124
124again:125again:
125 node = btrfs_get_delayed_node(btrfs_inode);126 node = btrfs_get_delayed_node(btrfs_inode);
@@ -131,26 +132,30 @@ again: · 5 unmodified lines
131 return ERR_PTR(-ENOMEM);132 return ERR_PTR(-ENOMEM);
132 btrfs_init_delayed_node(node, root, ino);133 btrfs_init_delayed_node(node, root, ino);
133 134
134 /* cached in the btrfs inode and can be accessed */135 /* Cached in the inode and can be accessed. */
135 refcount_set(&node->refs, 2);136 refcount_set(&node->refs, 2);
136 137
137 ret = radix_tree_preload(GFP_NOFS);138 /* Allocate and reserve the slot, from now it can return a NULL from xa_load(). */
138 if (ret) {139 ret = xa_reserve(&root->delayed_nodes, ino, GFP_NOFS);
140 if (ret == -ENOMEM) {
139 kmem_cache_free(delayed_node_cache, node);141 kmem_cache_free(delayed_node_cache, node);
140 return ERR_PTR(ret);142 return ERR_PTR(-ENOMEM);
141 }143 }
142
143 spin_lock(&root->inode_lock);144 spin_lock(&root->inode_lock);
144 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);145 ptr = xa_load(&root->delayed_nodes, ino);
145 if (ret == -EEXIST) {146 if (ptr) {
147 /* Somebody inserted it, go back and read it. */
146 spin_unlock(&root->inode_lock);148 spin_unlock(&root->inode_lock);
147 kmem_cache_free(delayed_node_cache, node);149 kmem_cache_free(delayed_node_cache, node);
148 radix_tree_preload_end();150 node = NULL;
149 goto again;151 goto again;
150 }152 }
153 ptr = xa_store(&root->delayed_nodes, ino, node, GFP_ATOMIC);
154 ASSERT(xa_err(ptr) != -EINVAL);
155 ASSERT(xa_err(ptr) != -ENOMEM);
156 ASSERT(ptr == NULL);
151 btrfs_inode->delayed_node = node;157 btrfs_inode->delayed_node = node;
152 spin_unlock(&root->inode_lock);158 spin_unlock(&root->inode_lock);
153 radix_tree_preload_end();
154 159
155 return node;160 return node;
156}161}
@@ -269,8 +274,7 @@ static void __btrfs_release_delayed_node( · 112 unmodified lines
269 * back up. We can delete it now.274 * back up. We can delete it now.
270 */275 */
271 ASSERT(refcount_read(&delayed_node->refs) == 0);276 ASSERT(refcount_read(&delayed_node->refs) == 0);
272 radix_tree_delete(&root->delayed_nodes_tree,277 xa_erase(&root->delayed_nodes, delayed_node->inode_id);
273 delayed_node->inode_id);
274 spin_unlock(&root->inode_lock);278 spin_unlock(&root->inode_lock);
275 kmem_cache_free(delayed_node_cache, delayed_node);279 kmem_cache_free(delayed_node_cache, delayed_node);
276 }280 }
@@ -1036,14 +1040,33 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, · 759 unmodified lines
1036 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))1040 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1037 goto out;1041 goto out;
1038 1042
1039 path->slots[0]++;1043 /*
1040 if (path->slots[0] >= btrfs_header_nritems(leaf))1044 * Now we're going to delete the INODE_REF/EXTREF, which should be the
1041 goto search;1045 * only one ref left. Check if the next item is an INODE_REF/EXTREF.
1042again:1046 *
1047 * But if we're the last item already, release and search for the last
1048 * INODE_REF/EXTREF.
1049 */
1050 if (path->slots[0] + 1 >= btrfs_header_nritems(leaf)) {
1051 key.objectid = node->inode_id;
1052 key.type = BTRFS_INODE_EXTREF_KEY;
1053 key.offset = (u64)-1;
1054
1055 btrfs_release_path(path);
1056 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1057 if (ret < 0)
1058 goto err_out;
1059 ASSERT(ret > 0);
1060 ASSERT(path->slots[0] > 0);
1061 ret = 0;
1062 path->slots[0]--;
1063 leaf = path->nodes[0];
1064 } else {
1065 path->slots[0]++;
1066 }
1043 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);1067 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1044 if (key.objectid != node->inode_id)1068 if (key.objectid != node->inode_id)
1045 goto out;1069 goto out;
1046
1047 if (key.type != BTRFS_INODE_REF_KEY &&1070 if (key.type != BTRFS_INODE_REF_KEY &&
1048 key.type != BTRFS_INODE_EXTREF_KEY)1071 key.type != BTRFS_INODE_EXTREF_KEY)
1049 goto out;1072 goto out;
@@ -1070,22 +1093,6 @@ err_out: · 20 unmodified lines
1070 btrfs_abort_transaction(trans, ret);1093 btrfs_abort_transaction(trans, ret);
1071 1094
1072 return ret;1095 return ret;
1073
1074search:
1075 btrfs_release_path(path);
1076
1077 key.type = BTRFS_INODE_EXTREF_KEY;
1078 key.offset = -1;
1079
1080 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1081 if (ret < 0)
1082 goto err_out;
1083 ASSERT(ret);
1084
1085 ret = 0;
1086 leaf = path->nodes[0];
1087 path->slots[0]--;
1088 goto again;
1089}1096}
1090 1097
1091static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,1098static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
@@ -2035,34 +2042,36 @@ void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode) · 943 unmodified lines
2035 2042
2036void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)2043void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
2037{2044{
2038 u64 inode_id = 0;2045 unsigned long index = 0;
2039 struct btrfs_delayed_node *delayed_nodes[8];2046 struct btrfs_delayed_node *delayed_nodes[8];
2040 int i, n;
2041 2047
2042 while (1) {2048 while (1) {
2049 struct btrfs_delayed_node *node;
2050 int count;
2051
2043 spin_lock(&root->inode_lock);2052 spin_lock(&root->inode_lock);
2044 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,2053 if (xa_empty(&root->delayed_nodes)) {
2045 (void **)delayed_nodes, inode_id,
2046 ARRAY_SIZE(delayed_nodes));
2047 if (!n) {
2048 spin_unlock(&root->inode_lock);2054 spin_unlock(&root->inode_lock);
2049 break;2055 return;
2050 }2056 }
2051 2057
2052 inode_id = delayed_nodes[n - 1]->inode_id + 1;2058 count = 0;
2053 for (i = 0; i < n; i++) {2059 xa_for_each_start(&root->delayed_nodes, index, node, index) {
2054 /*2060 /*
2055 * Don't increase refs in case the node is dead and2061 * Don't increase refs in case the node is dead and
2056 * about to be removed from the tree in the loop below2062 * about to be removed from the tree in the loop below
2057 */2063 */
2058 if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))2064 if (refcount_inc_not_zero(&node->refs)) {
2059 delayed_nodes[i] = NULL;2065 delayed_nodes[count] = node;
2066 count++;
2067 }
2068 if (count >= ARRAY_SIZE(delayed_nodes))
2069 break;
2060 }2070 }
2061 spin_unlock(&root->inode_lock);2071 spin_unlock(&root->inode_lock);
2072 index++;
2062 2073
2063 for (i = 0; i < n; i++) {2074 for (int i = 0; i < count; i++) {
2064 if (!delayed_nodes[i])
2065 continue;
2066 __btrfs_kill_delayed_node(delayed_nodes[i]);2075 __btrfs_kill_delayed_node(delayed_nodes[i]);
2067 btrfs_release_delayed_node(delayed_nodes[i]);2076 btrfs_release_delayed_node(delayed_nodes[i]);
2068 }2077 }
fs/btrfs/dev-replace.c+12−16
@@ -550,8 +550,7 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev, · 549 unmodified lines
550 u64 physical)550 u64 physical)
551{551{
552 struct btrfs_fs_info *fs_info = cache->fs_info;552 struct btrfs_fs_info *fs_info = cache->fs_info;
553 struct extent_map *em;553 struct btrfs_chunk_map *map;
554 struct map_lookup *map;
555 u64 chunk_offset = cache->start;554 u64 chunk_offset = cache->start;
556 int num_extents, cur_extent;555 int num_extents, cur_extent;
557 int i;556 int i;
@@ -567,9 +566,8 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev, · 9 unmodified lines
567 }566 }
568 spin_unlock(&cache->lock);567 spin_unlock(&cache->lock);
569 568
570 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);569 map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
571 ASSERT(!IS_ERR(em));570 ASSERT(!IS_ERR(map));
572 map = em->map_lookup;
573 571
574 num_extents = 0;572 num_extents = 0;
575 cur_extent = 0;573 cur_extent = 0;
@@ -583,7 +581,7 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev, · 7 unmodified lines
583 cur_extent = i;581 cur_extent = i;
584 }582 }
585 583
586 free_extent_map(em);584 btrfs_free_chunk_map(map);
587 585
588 if (num_extents > 1 && cur_extent < num_extents - 1) {586 if (num_extents > 1 && cur_extent < num_extents - 1) {
589 /*587 /*
@@ -812,25 +810,23 @@ static void btrfs_dev_replace_update_device_in_mapping_tree( · 222 unmodified lines
812 struct btrfs_device *srcdev,810 struct btrfs_device *srcdev,
813 struct btrfs_device *tgtdev)811 struct btrfs_device *tgtdev)
814{812{
815 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
816 struct extent_map *em;
817 struct map_lookup *map;
818 u64 start = 0;813 u64 start = 0;
819 int i;814 int i;
820 815
821 write_lock(&em_tree->lock);816 write_lock(&fs_info->mapping_tree_lock);
822 do {817 do {
823 em = lookup_extent_mapping(em_tree, start, (u64)-1);818 struct btrfs_chunk_map *map;
824 if (!em)819
820 map = btrfs_find_chunk_map_nolock(fs_info, start, U64_MAX);
821 if (!map)
825 break;822 break;
826 map = em->map_lookup;
827 for (i = 0; i < map->num_stripes; i++)823 for (i = 0; i < map->num_stripes; i++)
828 if (srcdev == map->stripes[i].dev)824 if (srcdev == map->stripes[i].dev)
829 map->stripes[i].dev = tgtdev;825 map->stripes[i].dev = tgtdev;
830 start = em->start + em->len;826 start = map->start + map->chunk_len;
831 free_extent_map(em);827 btrfs_free_chunk_map(map);
832 } while (start);828 } while (start);
833 write_unlock(&em_tree->lock);829 write_unlock(&fs_info->mapping_tree_lock);
834}830}
835 831
836static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,832static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
fs/btrfs/disk-io.c+70−85
@@ -74,20 +74,37 @@ static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info) · 73 unmodified lines
74static void csum_tree_block(struct extent_buffer *buf, u8 *result)74static void csum_tree_block(struct extent_buffer *buf, u8 *result)
75{75{
76 struct btrfs_fs_info *fs_info = buf->fs_info;76 struct btrfs_fs_info *fs_info = buf->fs_info;
77 const int num_pages = num_extent_pages(buf);77 int num_pages;
78 const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);78 u32 first_page_part;
79 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);79 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
80 char *kaddr;80 char *kaddr;
81 int i;81 int i;
82 82
83 shash->tfm = fs_info->csum_shash;83 shash->tfm = fs_info->csum_shash;
84 crypto_shash_init(shash);84 crypto_shash_init(shash);
85 kaddr = page_address(buf->pages[0]) + offset_in_page(buf->start);85
86 if (buf->addr) {
87 /* Pages are contiguous, handle them as a big one. */
88 kaddr = buf->addr;
89 first_page_part = fs_info->nodesize;
90 num_pages = 1;
91 } else {
92 kaddr = folio_address(buf->folios[0]);
93 first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
94 num_pages = num_extent_pages(buf);
95 }
96
86 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,97 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
87 first_page_part - BTRFS_CSUM_SIZE);98 first_page_part - BTRFS_CSUM_SIZE);
88 99
100 /*
101 * Multiple single-page folios case would reach here.
102 *
103 * nodesize <= PAGE_SIZE and large folio all handled by above
104 * crypto_shash_update() already.
105 */
89 for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {106 for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {
90 kaddr = page_address(buf->pages[i]);107 kaddr = folio_address(buf->folios[i]);
91 crypto_shash_update(shash, kaddr, PAGE_SIZE);108 crypto_shash_update(shash, kaddr, PAGE_SIZE);
92 }109 }
93 memset(result, 0, BTRFS_CSUM_SIZE);110 memset(result, 0, BTRFS_CSUM_SIZE);
@@ -166,20 +183,22 @@ static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, · 72 unmodified lines
166 int mirror_num)183 int mirror_num)
167{184{
168 struct btrfs_fs_info *fs_info = eb->fs_info;185 struct btrfs_fs_info *fs_info = eb->fs_info;
169 int i, num_pages = num_extent_pages(eb);186 int num_folios = num_extent_folios(eb);
170 int ret = 0;187 int ret = 0;
171 188
172 if (sb_rdonly(fs_info->sb))189 if (sb_rdonly(fs_info->sb))
173 return -EROFS;190 return -EROFS;
174 191
175 for (i = 0; i < num_pages; i++) {192 for (int i = 0; i < num_folios; i++) {
176 struct page *p = eb->pages[i];193 struct folio *folio = eb->folios[i];
177 u64 start = max_t(u64, eb->start, page_offset(p));194 u64 start = max_t(u64, eb->start, folio_pos(folio));
178 u64 end = min_t(u64, eb->start + eb->len, page_offset(p) + PAGE_SIZE);195 u64 end = min_t(u64, eb->start + eb->len,
196 folio_pos(folio) + folio_size(folio));
179 u32 len = end - start;197 u32 len = end - start;
180 198
181 ret = btrfs_repair_io_failure(fs_info, 0, start, len,199 ret = btrfs_repair_io_failure(fs_info, 0, start, len,
182 start, p, offset_in_page(start), mirror_num);200 start, folio, offset_in_folio(folio, start),
201 mirror_num);
183 if (ret)202 if (ret)
184 break;203 break;
185 }204 }
@@ -254,15 +273,20 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio) · 68 unmodified lines
254 if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))273 if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
255 return BLK_STS_IOERR;274 return BLK_STS_IOERR;
256 275
257 if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) {276 /*
258 WARN_ON_ONCE(found_start != 0);277 * If an extent_buffer is marked as EXTENT_BUFFER_ZONED_ZEROOUT, don't
278 * checksum it but zero-out its content. This is done to preserve
279 * ordering of I/O without unnecessarily writing out data.
280 */
281 if (test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags)) {
282 memzero_extent_buffer(eb, 0, eb->len);
259 return BLK_STS_OK;283 return BLK_STS_OK;
260 }284 }
261 285
262 if (WARN_ON_ONCE(found_start != eb->start))286 if (WARN_ON_ONCE(found_start != eb->start))
263 return BLK_STS_IOERR;287 return BLK_STS_IOERR;
264 if (WARN_ON(!btrfs_page_test_uptodate(fs_info, eb->pages[0], eb->start,288 if (WARN_ON(!btrfs_folio_test_uptodate(fs_info, eb->folios[0],
265 eb->len)))289 eb->start, eb->len)))
266 return BLK_STS_IOERR;290 return BLK_STS_IOERR;
267 291
268 ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,292 ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
@@ -371,8 +395,8 @@ int btrfs_validate_extent_buffer(struct extent_buffer *eb, · 102 unmodified lines
371 }395 }
372 396
373 csum_tree_block(eb, result);397 csum_tree_block(eb, result);
374 header_csum = page_address(eb->pages[0]) +398 header_csum = folio_address(eb->folios[0]) +
375 get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum));399 get_eb_offset_in_folio(eb, offsetof(struct btrfs_header, csum));
376 400
377 if (memcmp(result, header_csum, csum_size) != 0) {401 if (memcmp(result, header_csum, csum_size) != 0) {
378 btrfs_warn_rl(fs_info,402 btrfs_warn_rl(fs_info,
@@ -639,7 +663,8 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, · 260 unmodified lines
639 root->nr_delalloc_inodes = 0;663 root->nr_delalloc_inodes = 0;
640 root->nr_ordered_extents = 0;664 root->nr_ordered_extents = 0;
641 root->inode_tree = RB_ROOT;665 root->inode_tree = RB_ROOT;
642 INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);666 /* GFP flags are compatible with XA_FLAGS_*. */
667 xa_init_flags(&root->delayed_nodes, GFP_ATOMIC);
643 668
644 btrfs_init_root_block_rsv(root);669 btrfs_init_root_block_rsv(root);
645 670
@@ -650,14 +675,10 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, · 4 unmodified lines
650 INIT_LIST_HEAD(&root->ordered_extents);675 INIT_LIST_HEAD(&root->ordered_extents);
651 INIT_LIST_HEAD(&root->ordered_root);676 INIT_LIST_HEAD(&root->ordered_root);
652 INIT_LIST_HEAD(&root->reloc_dirty_list);677 INIT_LIST_HEAD(&root->reloc_dirty_list);
653 INIT_LIST_HEAD(&root->logged_list[0]);
654 INIT_LIST_HEAD(&root->logged_list[1]);
655 spin_lock_init(&root->inode_lock);678 spin_lock_init(&root->inode_lock);
656 spin_lock_init(&root->delalloc_lock);679 spin_lock_init(&root->delalloc_lock);
657 spin_lock_init(&root->ordered_extent_lock);680 spin_lock_init(&root->ordered_extent_lock);
658 spin_lock_init(&root->accounting_lock);681 spin_lock_init(&root->accounting_lock);
659 spin_lock_init(&root->log_extents_lock[0]);
660 spin_lock_init(&root->log_extents_lock[1]);
661 spin_lock_init(&root->qgroup_meta_rsv_lock);682 spin_lock_init(&root->qgroup_meta_rsv_lock);
662 mutex_init(&root->objectid_mutex);683 mutex_init(&root->objectid_mutex);
663 mutex_init(&root->log_mutex);684 mutex_init(&root->log_mutex);
@@ -2618,9 +2639,6 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) · 1954 unmodified lines
2618 */2639 */
2619 btrfs_set_super_log_root(sb, 0);2640 btrfs_set_super_log_root(sb, 0);
2620 2641
2621 /* We can't trust the free space cache either */
2622 btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
2623
2624 btrfs_warn(fs_info, "try to load backup roots slot %d", i);2642 btrfs_warn(fs_info, "try to load backup roots slot %d", i);
2625 ret = read_backup_root(fs_info, i);2643 ret = read_backup_root(fs_info, i);
2626 backup_index = ret;2644 backup_index = ret;
@@ -2724,7 +2742,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) · 97 unmodified lines
2724 INIT_LIST_HEAD(&fs_info->allocated_ebs);2742 INIT_LIST_HEAD(&fs_info->allocated_ebs);
2725 spin_lock_init(&fs_info->eb_leak_lock);2743 spin_lock_init(&fs_info->eb_leak_lock);
2726#endif2744#endif
2727 extent_map_tree_init(&fs_info->mapping_tree);2745 fs_info->mapping_tree = RB_ROOT_CACHED;
2746 rwlock_init(&fs_info->mapping_tree_lock);
2728 btrfs_init_block_rsv(&fs_info->global_block_rsv,2747 btrfs_init_block_rsv(&fs_info->global_block_rsv,
2729 BTRFS_BLOCK_RSV_GLOBAL);2748 BTRFS_BLOCK_RSV_GLOBAL);
2730 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);2749 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
@@ -2794,6 +2813,9 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) · 63 unmodified lines
2794 fs_info->sectorsize_bits = ilog2(4096);2813 fs_info->sectorsize_bits = ilog2(4096);
2795 fs_info->stripesize = 4096;2814 fs_info->stripesize = 4096;
2796 2815
2816 /* Default compress algorithm when user does -o compress */
2817 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2818
2797 fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;2819 fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
2798 2820
2799 spin_lock_init(&fs_info->swapfile_pins_lock);2821 spin_lock_init(&fs_info->swapfile_pins_lock);
@@ -2930,17 +2952,6 @@ out: · 130 unmodified lines
2930 return err;2952 return err;
2931}2953}
2932 2954
2933/*
2934 * Some options only have meaning at mount time and shouldn't persist across
2935 * remounts, or be displayed. Clear these at the end of mount and remount
2936 * code paths.
2937 */
2938void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
2939{
2940 btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
2941 btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
2942}
2943
2944/*2955/*
2945 * Mounting logic specific to read-write file systems. Shared by open_ctree2956 * Mounting logic specific to read-write file systems. Shared by open_ctree
2946 * and btrfs_remount when remounting from read-only to read-write.2957 * and btrfs_remount when remounting from read-only to read-write.
@@ -2953,7 +2964,11 @@ int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info) · 6 unmodified lines
2953 2964
2954 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&2965 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
2955 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {2966 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2956 rebuild_free_space_tree = true;2967 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
2968 btrfs_warn(fs_info,
2969 "'clear_cache' option is ignored with extent tree v2");
2970 else
2971 rebuild_free_space_tree = true;
2957 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&2972 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
2958 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {2973 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
2959 btrfs_warn(fs_info, "free space tree is invalid");2974 btrfs_warn(fs_info, "free space tree is invalid");
@@ -3276,13 +3291,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 316 unmodified lines
3276 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)3291 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
3277 WRITE_ONCE(fs_info->fs_error, -EUCLEAN);3292 WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
3278 3293
3279 /*
3280 * In the long term, we'll store the compression type in the super
3281 * block, and it'll be used for per file compression control.
3282 */
3283 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
3284
3285
3286 /* Set up fs_info before parsing mount options */3294 /* Set up fs_info before parsing mount options */
3287 nodesize = btrfs_super_nodesize(disk_super);3295 nodesize = btrfs_super_nodesize(disk_super);
3288 sectorsize = btrfs_super_sectorsize(disk_super);3296 sectorsize = btrfs_super_sectorsize(disk_super);
@@ -3296,28 +3304,30 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 7 unmodified lines
3296 fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;3304 fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
3297 fs_info->stripesize = stripesize;3305 fs_info->stripesize = stripesize;
3298 3306
3299 ret = btrfs_parse_options(fs_info, options, sb->s_flags);3307 /*
3300 if (ret)3308 * Handle the space caching options appropriately now that we have the
3309 * super block loaded and validated.
3310 */
3311 btrfs_set_free_space_cache_settings(fs_info);
3312
3313 if (!btrfs_check_options(fs_info, &fs_info->mount_opt, sb->s_flags)) {
3314 ret = -EINVAL;
3301 goto fail_alloc;3315 goto fail_alloc;
3316 }
3302 3317
3303 ret = btrfs_check_features(fs_info, !sb_rdonly(sb));3318 ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
3304 if (ret < 0)3319 if (ret < 0)
3305 goto fail_alloc;3320 goto fail_alloc;
3306 3321
3322 /*
3323 * At this point our mount options are validated, if we set ->max_inline
3324 * to something non-standard make sure we truncate it to sectorsize.
3325 */
3326 fs_info->max_inline = min_t(u64, fs_info->max_inline, fs_info->sectorsize);
3327
3307 if (sectorsize < PAGE_SIZE) {3328 if (sectorsize < PAGE_SIZE) {
3308 struct btrfs_subpage_info *subpage_info;3329 struct btrfs_subpage_info *subpage_info;
3309 3330
3310 /*
3311 * V1 space cache has some hardcoded PAGE_SIZE usage, and is
3312 * going to be deprecated.
3313 *
3314 * Force to use v2 cache for subpage case.
3315 */
3316 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
3317 btrfs_set_and_info(fs_info, FREE_SPACE_TREE,
3318 "forcing free space tree for sector size %u with page size %lu",
3319 sectorsize, PAGE_SIZE);
3320
3321 btrfs_warn(fs_info,3331 btrfs_warn(fs_info,
3322 "read-write for sector size %u with page size %lu is experimental",3332 "read-write for sector size %u with page size %lu is experimental",
3323 sectorsize, PAGE_SIZE);3333 sectorsize, PAGE_SIZE);
@@ -3494,29 +3504,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 170 unmodified lines
3494 goto fail_cleaner;3504 goto fail_cleaner;
3495 }3505 }
3496 3506
3497 if (!btrfs_test_opt(fs_info, NOSSD) &&
3498 !fs_info->fs_devices->rotating) {
3499 btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
3500 }
3501
3502 /*
3503 * For devices supporting discard turn on discard=async automatically,
3504 * unless it's already set or disabled. This could be turned off by
3505 * nodiscard for the same mount.
3506 *
3507 * The zoned mode piggy backs on the discard functionality for
3508 * resetting a zone. There is no reason to delay the zone reset as it is
3509 * fast enough. So, do not enable async discard for zoned mode.
3510 */
3511 if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
3512 btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
3513 btrfs_test_opt(fs_info, NODISCARD)) &&
3514 fs_info->fs_devices->discardable &&
3515 !btrfs_is_zoned(fs_info)) {
3516 btrfs_set_and_info(fs_info, DISCARD_ASYNC,
3517 "auto enabling async discard");
3518 }
3519
3520 ret = btrfs_read_qgroup_config(fs_info);3507 ret = btrfs_read_qgroup_config(fs_info);
3521 if (ret)3508 if (ret)
3522 goto fail_trans_kthread;3509 goto fail_trans_kthread;
@@ -3542,7 +3529,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 19 unmodified lines
3542 }3529 }
3543 3530
3544 if (sb_rdonly(sb))3531 if (sb_rdonly(sb))
3545 goto clear_oneshot;3532 return 0;
3546 3533
3547 ret = btrfs_start_pre_rw_mount(fs_info);3534 ret = btrfs_start_pre_rw_mount(fs_info);
3548 if (ret) {3535 if (ret) {
@@ -3570,8 +3557,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 21 unmodified lines
3570 if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))3557 if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
3571 wake_up_process(fs_info->cleaner_kthread);3558 wake_up_process(fs_info->cleaner_kthread);
3572 3559
3573clear_oneshot:
3574 btrfs_clear_oneshot_options(fs_info);
3575 return 0;3560 return 0;
3576 3561
3577fail_qgroup:3562fail_qgroup:
@@ -3608,7 +3593,7 @@ fail_sb_buffer: · 30 unmodified lines
3608 btrfs_stop_all_workers(fs_info);3593 btrfs_stop_all_workers(fs_info);
3609 btrfs_free_block_groups(fs_info);3594 btrfs_free_block_groups(fs_info);
3610fail_alloc:3595fail_alloc:
3611 btrfs_mapping_tree_free(&fs_info->mapping_tree);3596 btrfs_mapping_tree_free(fs_info);
3612 3597
3613 iput(fs_info->btree_inode);3598 iput(fs_info->btree_inode);
3614fail:3599fail:
@@ -4391,7 +4376,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) · 776 unmodified lines
4391 4376
4392 iput(fs_info->btree_inode);4377 iput(fs_info->btree_inode);
4393 4378
4394 btrfs_mapping_tree_free(&fs_info->mapping_tree);4379 btrfs_mapping_tree_free(fs_info);
4395 btrfs_close_devices(fs_info->fs_devices);4380 btrfs_close_devices(fs_info->fs_devices);
4396}4381}
4397 4382
fs/btrfs/disk-io.h+0−3
@@ -37,9 +37,6 @@ struct extent_buffer *btrfs_find_create_tree_block( · 36 unmodified lines
37 struct btrfs_fs_info *fs_info,37 struct btrfs_fs_info *fs_info,
38 u64 bytenr, u64 owner_root,38 u64 bytenr, u64 owner_root,
39 int level);39 int level);
40void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
41 struct extent_buffer *buf);
42void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info);
43int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info);40int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info);
44int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,41int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
45 const struct btrfs_super_block *disk_sb);42 const struct btrfs_super_block *disk_sb);
fs/btrfs/extent-io-tree.c+72−47
@@ -58,12 +58,13 @@ static inline void __btrfs_debug_check_extent_io_range(const char *caller, · 57 unmodified lines
58 struct extent_io_tree *tree,58 struct extent_io_tree *tree,
59 u64 start, u64 end)59 u64 start, u64 end)
60{60{
61 struct btrfs_inode *inode = tree->inode;61 const struct btrfs_inode *inode;
62 u64 isize;62 u64 isize;
63 63
64 if (!inode)64 if (tree->owner != IO_TREE_INODE_IO)
65 return;65 return;
66 66
67 inode = extent_io_tree_to_inode_const(tree);
67 isize = i_size_read(&inode->vfs_inode);68 isize = i_size_read(&inode->vfs_inode);
68 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {69 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
69 btrfs_debug_rl(inode->root->fs_info,70 btrfs_debug_rl(inode->root->fs_info,
@@ -78,31 +79,46 @@ static inline void __btrfs_debug_check_extent_io_range(const char *caller, · 8 unmodified lines
78#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)79#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
79#endif80#endif
80 81
82
81/*83/*
82 * For the file_extent_tree, we want to hold the inode lock when we lookup and84 * The only tree allowed to set the inode is IO_TREE_INODE_IO.
83 * update the disk_i_size, but lockdep will complain because our io_tree we hold
84 * the tree lock and get the inode lock when setting delalloc. These two things
85 * are unrelated, so make a class for the file_extent_tree so we don't get the
86 * two locking patterns mixed up.
87 */85 */
88static struct lock_class_key file_extent_tree_class;86static bool is_inode_io_tree(const struct extent_io_tree *tree)
87{
88 return tree->owner == IO_TREE_INODE_IO;
89}
90
91/* Return the inode if it's valid for the given tree, otherwise NULL. */
92struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree)
93{
94 if (tree->owner == IO_TREE_INODE_IO)
95 return tree->inode;
96 return NULL;
97}
98
99/* Read-only access to the inode. */
100const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree)
101{
102 if (tree->owner == IO_TREE_INODE_IO)
103 return tree->inode;
104 return NULL;
105}
89 106
90struct tree_entry {107/* For read-only access to fs_info. */
91 u64 start;108const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree)
92 u64 end;109{
93 struct rb_node rb_node;110 if (tree->owner == IO_TREE_INODE_IO)
94};111 return tree->inode->root->fs_info;
112 return tree->fs_info;
113}
95 114
96void extent_io_tree_init(struct btrfs_fs_info *fs_info,115void extent_io_tree_init(struct btrfs_fs_info *fs_info,
97 struct extent_io_tree *tree, unsigned int owner)116 struct extent_io_tree *tree, unsigned int owner)
98{117{
99 tree->fs_info = fs_info;
100 tree->state = RB_ROOT;118 tree->state = RB_ROOT;
101 spin_lock_init(&tree->lock);119 spin_lock_init(&tree->lock);
102 tree->inode = NULL;120 tree->fs_info = fs_info;
103 tree->owner = owner;121 tree->owner = owner;
104 if (owner == IO_TREE_INODE_FILE_EXTENT)
105 lockdep_set_class(&tree->lock, &file_extent_tree_class);
106}122}
107 123
108/*124/*
@@ -329,10 +345,14 @@ static inline struct extent_state *tree_search(struct extent_io_tree *tree, u64 · 220 unmodified lines
329 return tree_search_for_insert(tree, offset, NULL, NULL);345 return tree_search_for_insert(tree, offset, NULL, NULL);
330}346}
331 347
332static void extent_io_tree_panic(struct extent_io_tree *tree, int err)348static void extent_io_tree_panic(const struct extent_io_tree *tree,
349 const struct extent_state *state,
350 const char *opname,
351 int err)
333{352{
334 btrfs_panic(tree->fs_info, err,353 btrfs_panic(extent_io_tree_to_fs_info(tree), err,
335 "locking error: extent tree was modified by another thread while locked");354 "extent io tree error on %s state start %llu end %llu",
355 opname, state->start, state->end);
336}356}
337 357
338static void merge_prev_state(struct extent_io_tree *tree, struct extent_state *state)358static void merge_prev_state(struct extent_io_tree *tree, struct extent_state *state)
@@ -341,8 +361,9 @@ static void merge_prev_state(struct extent_io_tree *tree, struct extent_state *s · 2 unmodified lines
341 361
342 prev = prev_state(state);362 prev = prev_state(state);
343 if (prev && prev->end == state->start - 1 && prev->state == state->state) {363 if (prev && prev->end == state->start - 1 && prev->state == state->state) {
344 if (tree->inode)364 if (is_inode_io_tree(tree))
345 btrfs_merge_delalloc_extent(tree->inode, state, prev);365 btrfs_merge_delalloc_extent(extent_io_tree_to_inode(tree),
366 state, prev);
346 state->start = prev->start;367 state->start = prev->start;
347 rb_erase(&prev->rb_node, &tree->state);368 rb_erase(&prev->rb_node, &tree->state);
348 RB_CLEAR_NODE(&prev->rb_node);369 RB_CLEAR_NODE(&prev->rb_node);
@@ -356,8 +377,9 @@ static void merge_next_state(struct extent_io_tree *tree, struct extent_state *s · 7 unmodified lines
356 377
357 next = next_state(state);378 next = next_state(state);
358 if (next && next->start == state->end + 1 && next->state == state->state) {379 if (next && next->start == state->end + 1 && next->state == state->state) {
359 if (tree->inode)380 if (is_inode_io_tree(tree))
360 btrfs_merge_delalloc_extent(tree->inode, state, next);381 btrfs_merge_delalloc_extent(extent_io_tree_to_inode(tree),
382 state, next);
361 state->end = next->end;383 state->end = next->end;
362 rb_erase(&next->rb_node, &tree->state);384 rb_erase(&next->rb_node, &tree->state);
363 RB_CLEAR_NODE(&next->rb_node);385 RB_CLEAR_NODE(&next->rb_node);
@@ -390,8 +412,8 @@ static void set_state_bits(struct extent_io_tree *tree, · 26 unmodified lines
390 u32 bits_to_set = bits & ~EXTENT_CTLBITS;412 u32 bits_to_set = bits & ~EXTENT_CTLBITS;
391 int ret;413 int ret;
392 414
393 if (tree->inode)415 if (is_inode_io_tree(tree))
394 btrfs_set_delalloc_extent(tree->inode, state, bits);416 btrfs_set_delalloc_extent(extent_io_tree_to_inode(tree), state, bits);
395 417
396 ret = add_extent_changeset(state, bits_to_set, changeset, 1);418 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
397 BUG_ON(ret < 0);419 BUG_ON(ret < 0);
@@ -436,9 +458,10 @@ static struct extent_state *insert_state(struct extent_io_tree *tree, · 38 unmodified lines
436 if (state->end < entry->start) {458 if (state->end < entry->start) {
437 if (try_merge && end == entry->start &&459 if (try_merge && end == entry->start &&
438 state->state == entry->state) {460 state->state == entry->state) {
439 if (tree->inode)461 if (is_inode_io_tree(tree))
440 btrfs_merge_delalloc_extent(tree->inode,462 btrfs_merge_delalloc_extent(
441 state, entry);463 extent_io_tree_to_inode(tree),
464 state, entry);
442 entry->start = state->start;465 entry->start = state->start;
443 merge_prev_state(tree, entry);466 merge_prev_state(tree, entry);
444 state->state = 0;467 state->state = 0;
@@ -448,9 +471,10 @@ static struct extent_state *insert_state(struct extent_io_tree *tree, · 3 unmodified lines
448 } else if (state->end > entry->end) {471 } else if (state->end > entry->end) {
449 if (try_merge && entry->end == start &&472 if (try_merge && entry->end == start &&
450 state->state == entry->state) {473 state->state == entry->state) {
451 if (tree->inode)474 if (is_inode_io_tree(tree))
452 btrfs_merge_delalloc_extent(tree->inode,475 btrfs_merge_delalloc_extent(
453 state, entry);476 extent_io_tree_to_inode(tree),
477 state, entry);
454 entry->end = state->end;478 entry->end = state->end;
455 merge_next_state(tree, entry);479 merge_next_state(tree, entry);
456 state->state = 0;480 state->state = 0;
@@ -458,9 +482,6 @@ static struct extent_state *insert_state(struct extent_io_tree *tree, · 1 unmodified line
458 }482 }
459 node = &(*node)->rb_right;483 node = &(*node)->rb_right;
460 } else {484 } else {
461 btrfs_err(tree->fs_info,
462 "found node %llu %llu on insert of %llu %llu",
463 entry->start, entry->end, state->start, state->end);
464 return ERR_PTR(-EEXIST);485 return ERR_PTR(-EEXIST);
465 }486 }
466 }487 }
@@ -505,8 +526,9 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig, · 38 unmodified lines
505 struct rb_node *parent = NULL;526 struct rb_node *parent = NULL;
506 struct rb_node **node;527 struct rb_node **node;
507 528
508 if (tree->inode)529 if (is_inode_io_tree(tree))
509 btrfs_split_delalloc_extent(tree->inode, orig, split);530 btrfs_split_delalloc_extent(extent_io_tree_to_inode(tree), orig,
531 split);
510 532
511 prealloc->start = orig->start;533 prealloc->start = orig->start;
512 prealloc->end = split - 1;534 prealloc->end = split - 1;
@@ -553,8 +575,9 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree, · 40 unmodified lines
553 u32 bits_to_clear = bits & ~EXTENT_CTLBITS;575 u32 bits_to_clear = bits & ~EXTENT_CTLBITS;
554 int ret;576 int ret;
555 577
556 if (tree->inode)578 if (is_inode_io_tree(tree))
557 btrfs_clear_delalloc_extent(tree->inode, state, bits);579 btrfs_clear_delalloc_extent(extent_io_tree_to_inode(tree), state,
580 bits);
558 581
559 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);582 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
560 BUG_ON(ret < 0);583 BUG_ON(ret < 0);
@@ -695,7 +718,7 @@ hit_next: · 134 unmodified lines
695 goto search_again;718 goto search_again;
696 err = split_state(tree, state, prealloc, start);719 err = split_state(tree, state, prealloc, start);
697 if (err)720 if (err)
698 extent_io_tree_panic(tree, err);721 extent_io_tree_panic(tree, state, "split", err);
699 722
700 prealloc = NULL;723 prealloc = NULL;
701 if (err)724 if (err)
@@ -717,7 +740,7 @@ hit_next: · 15 unmodified lines
717 goto search_again;740 goto search_again;
718 err = split_state(tree, state, prealloc, end + 1);741 err = split_state(tree, state, prealloc, end + 1);
719 if (err)742 if (err)
720 extent_io_tree_panic(tree, err);743 extent_io_tree_panic(tree, state, "split", err);
721 744
722 if (wake)745 if (wake)
723 wake_up(&state->wq);746 wake_up(&state->wq);
@@ -939,6 +962,8 @@ int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, · 215 unmodified lines
939 struct extent_state *state;962 struct extent_state *state;
940 int ret = 1;963 int ret = 1;
941 964
965 ASSERT(!btrfs_fs_incompat(extent_io_tree_to_fs_info(tree), NO_HOLES));
966
942 spin_lock(&tree->lock);967 spin_lock(&tree->lock);
943 state = find_first_extent_bit_state(tree, start, bits);968 state = find_first_extent_bit_state(tree, start, bits);
944 if (state) {969 if (state) {
@@ -1152,7 +1177,7 @@ hit_next: · 207 unmodified lines
1152 goto search_again;1177 goto search_again;
1153 err = split_state(tree, state, prealloc, start);1178 err = split_state(tree, state, prealloc, start);
1154 if (err)1179 if (err)
1155 extent_io_tree_panic(tree, err);1180 extent_io_tree_panic(tree, state, "split", err);
1156 1181
1157 prealloc = NULL;1182 prealloc = NULL;
1158 if (err)1183 if (err)
@@ -1200,7 +1225,7 @@ hit_next: · 41 unmodified lines
1200 inserted_state = insert_state(tree, prealloc, bits, changeset);1225 inserted_state = insert_state(tree, prealloc, bits, changeset);
1201 if (IS_ERR(inserted_state)) {1226 if (IS_ERR(inserted_state)) {
1202 err = PTR_ERR(inserted_state);1227 err = PTR_ERR(inserted_state);
1203 extent_io_tree_panic(tree, err);1228 extent_io_tree_panic(tree, prealloc, "insert", err);
1204 }1229 }
1205 1230
1206 cache_state(inserted_state, cached_state);1231 cache_state(inserted_state, cached_state);
@@ -1228,7 +1253,7 @@ hit_next: · 21 unmodified lines
1228 goto search_again;1253 goto search_again;
1229 err = split_state(tree, state, prealloc, end + 1);1254 err = split_state(tree, state, prealloc, end + 1);
1230 if (err)1255 if (err)
1231 extent_io_tree_panic(tree, err);1256 extent_io_tree_panic(tree, state, "split", err);
1232 1257
1233 set_state_bits(tree, prealloc, bits, changeset);1258 set_state_bits(tree, prealloc, bits, changeset);
1234 cache_state(prealloc, cached_state);1259 cache_state(prealloc, cached_state);
@@ -1382,7 +1407,7 @@ hit_next: · 147 unmodified lines
1382 }1407 }
1383 err = split_state(tree, state, prealloc, start);1408 err = split_state(tree, state, prealloc, start);
1384 if (err)1409 if (err)
1385 extent_io_tree_panic(tree, err);1410 extent_io_tree_panic(tree, state, "split", err);
1386 prealloc = NULL;1411 prealloc = NULL;
1387 if (err)1412 if (err)
1388 goto out;1413 goto out;
@@ -1430,7 +1455,7 @@ hit_next: · 41 unmodified lines
1430 inserted_state = insert_state(tree, prealloc, bits, NULL);1455 inserted_state = insert_state(tree, prealloc, bits, NULL);
1431 if (IS_ERR(inserted_state)) {1456 if (IS_ERR(inserted_state)) {
1432 err = PTR_ERR(inserted_state);1457 err = PTR_ERR(inserted_state);
1433 extent_io_tree_panic(tree, err);1458 extent_io_tree_panic(tree, prealloc, "insert", err);
1434 }1459 }
1435 cache_state(inserted_state, cached_state);1460 cache_state(inserted_state, cached_state);
1436 if (inserted_state == prealloc)1461 if (inserted_state == prealloc)
@@ -1453,7 +1478,7 @@ hit_next: · 16 unmodified lines
1453 1478
1454 err = split_state(tree, state, prealloc, end + 1);1479 err = split_state(tree, state, prealloc, end + 1);
1455 if (err)1480 if (err)
1456 extent_io_tree_panic(tree, err);1481 extent_io_tree_panic(tree, state, "split", err);
1457 1482
1458 set_state_bits(tree, prealloc, bits, NULL);1483 set_state_bits(tree, prealloc, bits, NULL);
1459 cache_state(prealloc, cached_state);1484 cache_state(prealloc, cached_state);
fs/btrfs/extent-io-tree.h+15−3
@@ -87,9 +87,17 @@ enum { · 86 unmodified lines
87 87
88struct extent_io_tree {88struct extent_io_tree {
89 struct rb_root state;89 struct rb_root state;
90 struct btrfs_fs_info *fs_info;90 /*
91 /* Inode associated with this tree, or NULL. */91 * The fs_info is needed for trace points, a tree attached to an inode
92 struct btrfs_inode *inode;92 * needs the inode.
93 *
94 * owner == IO_TREE_INODE_IO - then inode is valid and fs_info can be
95 * accessed as inode->root->fs_info
96 */
97 union {
98 struct btrfs_fs_info *fs_info;
99 struct btrfs_inode *inode;
100 };
93 101
94 /* Who owns this io tree, should be one of IO_TREE_* */102 /* Who owns this io tree, should be one of IO_TREE_* */
95 u8 owner;103 u8 owner;
@@ -112,6 +120,10 @@ struct extent_state { · 16 unmodified lines
112#endif120#endif
113};121};
114 122
123struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree);
124const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree);
125const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
126
115void extent_io_tree_init(struct btrfs_fs_info *fs_info,127void extent_io_tree_init(struct btrfs_fs_info *fs_info,
116 struct extent_io_tree *tree, unsigned int owner);128 struct extent_io_tree *tree, unsigned int owner);
117void extent_io_tree_release(struct extent_io_tree *tree);129void extent_io_tree_release(struct extent_io_tree *tree);
fs/btrfs/extent-tree.c+51−53
@@ -3447,6 +3447,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, · 3446 unmodified lines
3447{3447{
3448 struct btrfs_fs_info *fs_info = trans->fs_info;3448 struct btrfs_fs_info *fs_info = trans->fs_info;
3449 struct btrfs_ref generic_ref = { 0 };3449 struct btrfs_ref generic_ref = { 0 };
3450 struct btrfs_block_group *bg;
3450 int ret;3451 int ret;
3451 3452
3452 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,3453 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
@@ -3460,67 +3461,64 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, · 7 unmodified lines
3460 BUG_ON(ret); /* -ENOMEM */3461 BUG_ON(ret); /* -ENOMEM */
3461 }3462 }
3462 3463
3463 if (last_ref && btrfs_header_generation(buf) == trans->transid) {3464 if (!last_ref)
3464 struct btrfs_block_group *cache;3465 return;
3465 bool must_pin = false;
3466
3467 if (root_id != BTRFS_TREE_LOG_OBJECTID) {
3468 ret = check_ref_cleanup(trans, buf->start);
3469 if (!ret) {
3470 btrfs_redirty_list_add(trans->transaction, buf);
3471 goto out;
3472 }
3473 }
3474 3466
3475 cache = btrfs_lookup_block_group(fs_info, buf->start);3467 if (btrfs_header_generation(buf) != trans->transid)
3468 goto out;
3476 3469
3477 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {3470 if (root_id != BTRFS_TREE_LOG_OBJECTID) {
3478 pin_down_extent(trans, cache, buf->start, buf->len, 1);3471 ret = check_ref_cleanup(trans, buf->start);
3479 btrfs_put_block_group(cache);3472 if (!ret)
3480 goto out;3473 goto out;
3481 }3474 }
3482 3475
3483 /*3476 bg = btrfs_lookup_block_group(fs_info, buf->start);
3484 * If there are tree mod log users we may have recorded mod log
3485 * operations for this node. If we re-allocate this node we
3486 * could replay operations on this node that happened when it
3487 * existed in a completely different root. For example if it
3488 * was part of root A, then was reallocated to root B, and we
3489 * are doing a btrfs_old_search_slot(root b), we could replay
3490 * operations that happened when the block was part of root A,
3491 * giving us an inconsistent view of the btree.
3492 *
3493 * We are safe from races here because at this point no other
3494 * node or root points to this extent buffer, so if after this
3495 * check a new tree mod log user joins we will not have an
3496 * existing log of operations on this node that we have to
3497 * contend with.
3498 */
3499 if (test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
3500 must_pin = true;
3501 3477
3502 if (must_pin || btrfs_is_zoned(fs_info)) {3478 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3503 btrfs_redirty_list_add(trans->transaction, buf);3479 pin_down_extent(trans, bg, buf->start, buf->len, 1);
3504 pin_down_extent(trans, cache, buf->start, buf->len, 1);3480 btrfs_put_block_group(bg);
3505 btrfs_put_block_group(cache);3481 goto out;
3506 goto out;3482 }
3507 }
3508 3483
3509 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));3484 /*
3485 * If there are tree mod log users we may have recorded mod log
3486 * operations for this node. If we re-allocate this node we
3487 * could replay operations on this node that happened when it
3488 * existed in a completely different root. For example if it
3489 * was part of root A, then was reallocated to root B, and we
3490 * are doing a btrfs_old_search_slot(root b), we could replay
3491 * operations that happened when the block was part of root A,
3492 * giving us an inconsistent view of the btree.
3493 *
3494 * We are safe from races here because at this point no other
3495 * node or root points to this extent buffer, so if after this
3496 * check a new tree mod log user joins we will not have an
3497 * existing log of operations on this node that we have to
3498 * contend with.
3499 */
3510 3500
3511 btrfs_add_free_space(cache, buf->start, buf->len);3501 if (test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags)
3512 btrfs_free_reserved_bytes(cache, buf->len, 0);3502 || btrfs_is_zoned(fs_info)) {
3513 btrfs_put_block_group(cache);3503 pin_down_extent(trans, bg, buf->start, buf->len, 1);
3514 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);3504 btrfs_put_block_group(bg);
3505 goto out;
3515 }3506 }
3507
3508 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3509
3510 btrfs_add_free_space(bg, buf->start, buf->len);
3511 btrfs_free_reserved_bytes(bg, buf->len, 0);
3512 btrfs_put_block_group(bg);
3513 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
3514
3516out:3515out:
3517 if (last_ref) {3516
3518 /*3517 /*
3519 * Deleting the buffer, clear the corrupt flag since it doesn't3518 * Deleting the buffer, clear the corrupt flag since it doesn't
3520 * matter anymore.3519 * matter anymore.
3521 */3520 */
3522 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);3521 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3523 }
3524}3522}
3525 3523
3526/* Can return -ENOMEM */3524/* Can return -ENOMEM */
@@ -5061,7 +5059,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 1534 unmodified lines
5061 __btrfs_tree_lock(buf, nest);5059 __btrfs_tree_lock(buf, nest);
5062 btrfs_clear_buffer_dirty(trans, buf);5060 btrfs_clear_buffer_dirty(trans, buf);
5063 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);5061 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
5064 clear_bit(EXTENT_BUFFER_NO_CHECK, &buf->bflags);5062 clear_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &buf->bflags);
5065 5063
5066 set_extent_buffer_uptodate(buf);5064 set_extent_buffer_uptodate(buf);
5067 5065
fs/btrfs/extent_io.c+633−418
@@ -184,22 +184,23 @@ static void process_one_page(struct btrfs_fs_info *fs_info, · 183 unmodified lines
184 struct page *page, struct page *locked_page,184 struct page *page, struct page *locked_page,
185 unsigned long page_ops, u64 start, u64 end)185 unsigned long page_ops, u64 start, u64 end)
186{186{
187 struct folio *folio = page_folio(page);
187 u32 len;188 u32 len;
188 189
189 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);190 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
190 len = end + 1 - start;191 len = end + 1 - start;
191 192
192 if (page_ops & PAGE_SET_ORDERED)193 if (page_ops & PAGE_SET_ORDERED)
193 btrfs_page_clamp_set_ordered(fs_info, page, start, len);194 btrfs_folio_clamp_set_ordered(fs_info, folio, start, len);
194 if (page_ops & PAGE_START_WRITEBACK) {195 if (page_ops & PAGE_START_WRITEBACK) {
195 btrfs_page_clamp_clear_dirty(fs_info, page, start, len);196 btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
196 btrfs_page_clamp_set_writeback(fs_info, page, start, len);197 btrfs_folio_clamp_set_writeback(fs_info, folio, start, len);
197 }198 }
198 if (page_ops & PAGE_END_WRITEBACK)199 if (page_ops & PAGE_END_WRITEBACK)
199 btrfs_page_clamp_clear_writeback(fs_info, page, start, len);200 btrfs_folio_clamp_clear_writeback(fs_info, folio, start, len);
200 201
201 if (page != locked_page && (page_ops & PAGE_UNLOCK))202 if (page != locked_page && (page_ops & PAGE_UNLOCK))
202 btrfs_page_end_writer_lock(fs_info, page, start, len);203 btrfs_folio_end_writer_lock(fs_info, folio, start, len);
203}204}
204 205
205static void __process_pages_contig(struct address_space *mapping,206static void __process_pages_contig(struct address_space *mapping,
@@ -271,19 +272,20 @@ static noinline int lock_delalloc_pages(struct inode *inode, · 65 unmodified lines
271 goto out;272 goto out;
272 273
273 for (i = 0; i < found_folios; i++) {274 for (i = 0; i < found_folios; i++) {
274 struct page *page = &fbatch.folios[i]->page;275 struct folio *folio = fbatch.folios[i];
276 struct page *page = folio_page(folio, 0);
275 u32 len = end + 1 - start;277 u32 len = end + 1 - start;
276 278
277 if (page == locked_page)279 if (page == locked_page)
278 continue;280 continue;
279 281
280 if (btrfs_page_start_writer_lock(fs_info, page, start,282 if (btrfs_folio_start_writer_lock(fs_info, folio, start,
281 len))283 len))
282 goto out;284 goto out;
283 285
284 if (!PageDirty(page) || page->mapping != mapping) {286 if (!PageDirty(page) || page->mapping != mapping) {
285 btrfs_page_end_writer_lock(fs_info, page, start,287 btrfs_folio_end_writer_lock(fs_info, folio, start,
286 len);288 len);
287 goto out;289 goto out;
288 }290 }
289 291
@@ -432,60 +434,65 @@ static bool btrfs_verify_page(struct page *page, u64 start) · 142 unmodified lines
432static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)434static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
433{435{
434 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);436 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
437 struct folio *folio = page_folio(page);
435 438
436 ASSERT(page_offset(page) <= start &&439 ASSERT(page_offset(page) <= start &&
437 start + len <= page_offset(page) + PAGE_SIZE);440 start + len <= page_offset(page) + PAGE_SIZE);
438 441
439 if (uptodate && btrfs_verify_page(page, start))442 if (uptodate && btrfs_verify_page(page, start))
440 btrfs_page_set_uptodate(fs_info, page, start, len);443 btrfs_folio_set_uptodate(fs_info, folio, start, len);
441 else444 else
442 btrfs_page_clear_uptodate(fs_info, page, start, len);445 btrfs_folio_clear_uptodate(fs_info, folio, start, len);
443 446
444 if (!btrfs_is_subpage(fs_info, page))447 if (!btrfs_is_subpage(fs_info, page->mapping))
445 unlock_page(page);448 unlock_page(page);
446 else449 else
447 btrfs_subpage_end_reader(fs_info, page, start, len);450 btrfs_subpage_end_reader(fs_info, folio, start, len);
448}451}
449 452
450/*453/*
451 * after a writepage IO is done, we need to:454 * After a write IO is done, we need to:
452 * clear the uptodate bits on error455 *
453 * clear the writeback bits in the extent tree for this IO456 * - clear the uptodate bits on error
454 * end_page_writeback if the page has no more pending IO457 * - clear the writeback bits in the extent tree for the range
458 * - filio_end_writeback() if there is no more pending io for the folio
455 *459 *
456 * Scheduling is not allowed, so the extent state tree is expected460 * Scheduling is not allowed, so the extent state tree is expected
457 * to have one and only one object corresponding to this IO.461 * to have one and only one object corresponding to this IO.
458 */462 */
459static void end_bio_extent_writepage(struct btrfs_bio *bbio)463static void end_bbio_data_write(struct btrfs_bio *bbio)
460{464{
461 struct bio *bio = &bbio->bio;465 struct bio *bio = &bbio->bio;
462 int error = blk_status_to_errno(bio->bi_status);466 int error = blk_status_to_errno(bio->bi_status);
463 struct bio_vec *bvec;467 struct folio_iter fi;
464 struct bvec_iter_all iter_all;
465 468
466 ASSERT(!bio_flagged(bio, BIO_CLONED));469 ASSERT(!bio_flagged(bio, BIO_CLONED));
467 bio_for_each_segment_all(bvec, bio, iter_all) {470 bio_for_each_folio_all(fi, bio) {
468 struct page *page = bvec->bv_page;471 struct folio *folio = fi.folio;
469 struct inode *inode = page->mapping->host;472 struct inode *inode = folio->mapping->host;
470 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
471 const u32 sectorsize = fs_info->sectorsize;474 const u32 sectorsize = fs_info->sectorsize;
472 u64 start = page_offset(page) + bvec->bv_offset;475 u64 start = folio_pos(folio) + fi.offset;
473 u32 len = bvec->bv_len;476 u32 len = fi.length;
477
478 /* Only order 0 (single page) folios are allowed for data. */
479 ASSERT(folio_order(folio) == 0);
474 480
475 /* Our read/write should always be sector aligned. */481 /* Our read/write should always be sector aligned. */
476 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))482 if (!IS_ALIGNED(fi.offset, sectorsize))
477 btrfs_err(fs_info,483 btrfs_err(fs_info,
478 "partial page write in btrfs with offset %u and length %u",484 "partial page write in btrfs with offset %zu and length %zu",
479 bvec->bv_offset, bvec->bv_len);485 fi.offset, fi.length);
480 else if (!IS_ALIGNED(bvec->bv_len, sectorsize))486 else if (!IS_ALIGNED(fi.length, sectorsize))
481 btrfs_info(fs_info,487 btrfs_info(fs_info,
482 "incomplete page write with offset %u and length %u",488 "incomplete page write with offset %zu and length %zu",
483 bvec->bv_offset, bvec->bv_len);489 fi.offset, fi.length);
484 490
485 btrfs_finish_ordered_extent(bbio->ordered, page, start, len, !error);491 btrfs_finish_ordered_extent(bbio->ordered,
492 folio_page(folio, 0), start, len, !error);
486 if (error)493 if (error)
487 mapping_set_error(page->mapping, error);494 mapping_set_error(folio->mapping, error);
488 btrfs_page_clear_writeback(fs_info, page, start, len);495 btrfs_folio_clear_writeback(fs_info, folio, start, len);
489 }496 }
490 497
491 bio_put(bio);498 bio_put(bio);
@@ -562,98 +569,102 @@ update: · 70 unmodified lines
562 569
563static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)570static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
564{571{
565 ASSERT(PageLocked(page));572 struct folio *folio = page_folio(page);
566 if (!btrfs_is_subpage(fs_info, page))573
574 ASSERT(folio_test_locked(folio));
575 if (!btrfs_is_subpage(fs_info, folio->mapping))
567 return;576 return;
568 577
569 ASSERT(PagePrivate(page));578 ASSERT(folio_test_private(folio));
570 btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE);579 btrfs_subpage_start_reader(fs_info, folio, page_offset(page), PAGE_SIZE);
571}580}
572 581
573/*582/*
574 * after a readpage IO is done, we need to:583 * After a data read IO is done, we need to:
575 * clear the uptodate bits on error584 *
576 * set the uptodate bits if things worked585 * - clear the uptodate bits on error
577 * set the page up to date if all extents in the tree are uptodate586 * - set the uptodate bits if things worked
578 * clear the lock bit in the extent tree587 * - set the folio up to date if all extents in the tree are uptodate
579 * unlock the page if there are no other extents locked for it588 * - clear the lock bit in the extent tree
589 * - unlock the folio if there are no other extents locked for it
580 *590 *
581 * Scheduling is not allowed, so the extent state tree is expected591 * Scheduling is not allowed, so the extent state tree is expected
582 * to have one and only one object corresponding to this IO.592 * to have one and only one object corresponding to this IO.
583 */593 */
584static void end_bio_extent_readpage(struct btrfs_bio *bbio)594static void end_bbio_data_read(struct btrfs_bio *bbio)
585{595{
586 struct bio *bio = &bbio->bio;596 struct bio *bio = &bbio->bio;
587 struct bio_vec *bvec;
588 struct processed_extent processed = { 0 };597 struct processed_extent processed = { 0 };
598 struct folio_iter fi;
589 /*599 /*
590 * The offset to the beginning of a bio, since one bio can never be600 * The offset to the beginning of a bio, since one bio can never be
591 * larger than UINT_MAX, u32 here is enough.601 * larger than UINT_MAX, u32 here is enough.
592 */602 */
593 u32 bio_offset = 0;603 u32 bio_offset = 0;
594 struct bvec_iter_all iter_all;
595 604
596 ASSERT(!bio_flagged(bio, BIO_CLONED));605 ASSERT(!bio_flagged(bio, BIO_CLONED));
597 bio_for_each_segment_all(bvec, bio, iter_all) {606 bio_for_each_folio_all(fi, &bbio->bio) {
598 bool uptodate = !bio->bi_status;607 bool uptodate = !bio->bi_status;
599 struct page *page = bvec->bv_page;608 struct folio *folio = fi.folio;
600 struct inode *inode = page->mapping->host;609 struct inode *inode = folio->mapping->host;
601 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
602 const u32 sectorsize = fs_info->sectorsize;611 const u32 sectorsize = fs_info->sectorsize;
603 u64 start;612 u64 start;
604 u64 end;613 u64 end;
605 u32 len;614 u32 len;
606 615
616 /* For now only order 0 folios are supported for data. */
617 ASSERT(folio_order(folio) == 0);
607 btrfs_debug(fs_info,618 btrfs_debug(fs_info,
608 "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",619 "%s: bi_sector=%llu, err=%d, mirror=%u",
609 bio->bi_iter.bi_sector, bio->bi_status,620 __func__, bio->bi_iter.bi_sector, bio->bi_status,
610 bbio->mirror_num);621 bbio->mirror_num);
611 622
612 /*623 /*
613 * We always issue full-sector reads, but if some block in a624 * We always issue full-sector reads, but if some block in a
614 * page fails to read, blk_update_request() will advance625 * folio fails to read, blk_update_request() will advance
615 * bv_offset and adjust bv_len to compensate. Print a warning626 * bv_offset and adjust bv_len to compensate. Print a warning
616 * for unaligned offsets, and an error if they don't add up to627 * for unaligned offsets, and an error if they don't add up to
617 * a full sector.628 * a full sector.
618 */629 */
619 if (!IS_ALIGNED(bvec->bv_offset, sectorsize))630 if (!IS_ALIGNED(fi.offset, sectorsize))
620 btrfs_err(fs_info,631 btrfs_err(fs_info,
621 "partial page read in btrfs with offset %u and length %u",632 "partial page read in btrfs with offset %zu and length %zu",
622 bvec->bv_offset, bvec->bv_len);633 fi.offset, fi.length);
623 else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,634 else if (!IS_ALIGNED(fi.offset + fi.length, sectorsize))
624 sectorsize))
625 btrfs_info(fs_info,635 btrfs_info(fs_info,
626 "incomplete page read with offset %u and length %u",636 "incomplete page read with offset %zu and length %zu",
627 bvec->bv_offset, bvec->bv_len);637 fi.offset, fi.length);
628 638
629 start = page_offset(page) + bvec->bv_offset;639 start = folio_pos(folio) + fi.offset;
630 end = start + bvec->bv_len - 1;640 end = start + fi.length - 1;
631 len = bvec->bv_len;641 len = fi.length;
632 642
633 if (likely(uptodate)) {643 if (likely(uptodate)) {
634 loff_t i_size = i_size_read(inode);644 loff_t i_size = i_size_read(inode);
635 pgoff_t end_index = i_size >> PAGE_SHIFT;645 pgoff_t end_index = i_size >> folio_shift(folio);
636 646
637 /*647 /*
638 * Zero out the remaining part if this range straddles648 * Zero out the remaining part if this range straddles
639 * i_size.649 * i_size.
640 *650 *
641 * Here we should only zero the range inside the bvec,651 * Here we should only zero the range inside the folio,
642 * not touch anything else.652 * not touch anything else.
643 *653 *
644 * NOTE: i_size is exclusive while end is inclusive.654 * NOTE: i_size is exclusive while end is inclusive.
645 */655 */
646 if (page->index == end_index && i_size <= end) {656 if (folio_index(folio) == end_index && i_size <= end) {
647 u32 zero_start = max(offset_in_page(i_size),657 u32 zero_start = max(offset_in_folio(folio, i_size),
648 offset_in_page(start));658 offset_in_folio(folio, start));
659 u32 zero_len = offset_in_folio(folio, end) + 1 -
660 zero_start;
649 661
650 zero_user_segment(page, zero_start,662 folio_zero_range(folio, zero_start, zero_len);
651 offset_in_page(end) + 1);
652 }663 }
653 }664 }
654 665
655 /* Update page status and unlock. */666 /* Update page status and unlock. */
656 end_page_read(page, uptodate, start, len);667 end_page_read(folio_page(folio, 0), uptodate, start, len);
657 endio_readpage_release_extent(&processed, BTRFS_I(inode),668 endio_readpage_release_extent(&processed, BTRFS_I(inode),
658 start, end, uptodate);669 start, end, uptodate);
659 670
@@ -672,19 +683,22 @@ static void end_bio_extent_readpage(struct btrfs_bio *bbio) · 12 unmodified lines
672 * @nr_pages: number of pages to allocate683 * @nr_pages: number of pages to allocate
673 * @page_array: the array to fill with pages; any existing non-null entries in684 * @page_array: the array to fill with pages; any existing non-null entries in
674 * the array will be skipped685 * the array will be skipped
686 * @extra_gfp: the extra GFP flags for the allocation.
675 *687 *
676 * Return: 0 if all pages were able to be allocated;688 * Return: 0 if all pages were able to be allocated;
677 * -ENOMEM otherwise, the partially allocated pages would be freed and689 * -ENOMEM otherwise, the partially allocated pages would be freed and
678 * the array slots zeroed690 * the array slots zeroed
679 */691 */
680int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array)692int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
693 gfp_t extra_gfp)
681{694{
682 unsigned int allocated;695 unsigned int allocated;
683 696
684 for (allocated = 0; allocated < nr_pages;) {697 for (allocated = 0; allocated < nr_pages;) {
685 unsigned int last = allocated;698 unsigned int last = allocated;
686 699
687 allocated = alloc_pages_bulk_array(GFP_NOFS, nr_pages, page_array);700 allocated = alloc_pages_bulk_array(GFP_NOFS | extra_gfp,
701 nr_pages, page_array);
688 702
689 if (allocated == nr_pages)703 if (allocated == nr_pages)
690 return 0;704 return 0;
@@ -707,6 +721,26 @@ int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array) · 16 unmodified lines
707 return 0;721 return 0;
708}722}
709 723
724/*
725 * Populate needed folios for the extent buffer.
726 *
727 * For now, the folios populated are always in order 0 (aka, single page).
728 */
729static int alloc_eb_folio_array(struct extent_buffer *eb, gfp_t extra_gfp)
730{
731 struct page *page_array[INLINE_EXTENT_BUFFER_PAGES] = { 0 };
732 int num_pages = num_extent_pages(eb);
733 int ret;
734
735 ret = btrfs_alloc_page_array(num_pages, page_array, extra_gfp);
736 if (ret < 0)
737 return ret;
738
739 for (int i = 0; i < num_pages; i++)
740 eb->folios[i] = page_folio(page_array[i]);
741 return 0;
742}
743
710static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,744static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,
711 struct page *page, u64 disk_bytenr,745 struct page *page, u64 disk_bytenr,
712 unsigned int pg_offset)746 unsigned int pg_offset)
@@ -861,9 +895,9 @@ static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl, · 148 unmodified lines
861 } while (size);895 } while (size);
862}896}
863 897
864static int attach_extent_buffer_page(struct extent_buffer *eb,898static int attach_extent_buffer_folio(struct extent_buffer *eb,
865 struct page *page,899 struct folio *folio,
866 struct btrfs_subpage *prealloc)900 struct btrfs_subpage *prealloc)
867{901{
868 struct btrfs_fs_info *fs_info = eb->fs_info;902 struct btrfs_fs_info *fs_info = eb->fs_info;
869 int ret = 0;903 int ret = 0;
@@ -874,65 +908,66 @@ static int attach_extent_buffer_page(struct extent_buffer *eb, · 4 unmodified lines
874 * For cloned or dummy extent buffers, their pages are not mapped and908 * For cloned or dummy extent buffers, their pages are not mapped and
875 * will not race with any other ebs.909 * will not race with any other ebs.
876 */910 */
877 if (page->mapping)911 if (folio->mapping)
878 lockdep_assert_held(&page->mapping->i_private_lock);912 lockdep_assert_held(&folio->mapping->i_private_lock);
879 913
880 if (fs_info->nodesize >= PAGE_SIZE) {914 if (fs_info->nodesize >= PAGE_SIZE) {
881 if (!PagePrivate(page))915 if (!folio_test_private(folio))
882 attach_page_private(page, eb);916 folio_attach_private(folio, eb);
883 else917 else
884 WARN_ON(page->private != (unsigned long)eb);918 WARN_ON(folio_get_private(folio) != eb);
885 return 0;919 return 0;
886 }920 }
887 921
888 /* Already mapped, just free prealloc */922 /* Already mapped, just free prealloc */
889 if (PagePrivate(page)) {923 if (folio_test_private(folio)) {
890 btrfs_free_subpage(prealloc);924 btrfs_free_subpage(prealloc);
891 return 0;925 return 0;
892 }926 }
893 927
894 if (prealloc)928 if (prealloc)
895 /* Has preallocated memory for subpage */929 /* Has preallocated memory for subpage */
896 attach_page_private(page, prealloc);930 folio_attach_private(folio, prealloc);
897 else931 else
898 /* Do new allocation to attach subpage */932 /* Do new allocation to attach subpage */
899 ret = btrfs_attach_subpage(fs_info, page,933 ret = btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_METADATA);
900 BTRFS_SUBPAGE_METADATA);
901 return ret;934 return ret;
902}935}
903 936
904int set_page_extent_mapped(struct page *page)937int set_page_extent_mapped(struct page *page)
905{938{
939 struct folio *folio = page_folio(page);
906 struct btrfs_fs_info *fs_info;940 struct btrfs_fs_info *fs_info;
907 941
908 ASSERT(page->mapping);942 ASSERT(page->mapping);
909 943
910 if (PagePrivate(page))944 if (folio_test_private(folio))
911 return 0;945 return 0;
912 946
913 fs_info = btrfs_sb(page->mapping->host->i_sb);947 fs_info = btrfs_sb(page->mapping->host->i_sb);
914 948
915 if (btrfs_is_subpage(fs_info, page))949 if (btrfs_is_subpage(fs_info, page->mapping))
916 return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA);950 return btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_DATA);
917 951
918 attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);952 folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
919 return 0;953 return 0;
920}954}
921 955
922void clear_page_extent_mapped(struct page *page)956void clear_page_extent_mapped(struct page *page)
923{957{
958 struct folio *folio = page_folio(page);
924 struct btrfs_fs_info *fs_info;959 struct btrfs_fs_info *fs_info;
925 960
926 ASSERT(page->mapping);961 ASSERT(page->mapping);
927 962
928 if (!PagePrivate(page))963 if (!folio_test_private(folio))
929 return;964 return;
930 965
931 fs_info = btrfs_sb(page->mapping->host->i_sb);966 fs_info = btrfs_sb(page->mapping->host->i_sb);
932 if (btrfs_is_subpage(fs_info, page))967 if (btrfs_is_subpage(fs_info, page->mapping))
933 return btrfs_detach_subpage(fs_info, page);968 return btrfs_detach_subpage(fs_info, folio);
934 969
935 detach_page_private(page);970 folio_detach_private(folio);
936}971}
937 972
938static struct extent_map *973static struct extent_map *
@@ -1001,7 +1036,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 62 unmodified lines
1001 memzero_page(page, zero_offset, iosize);1036 memzero_page(page, zero_offset, iosize);
1002 }1037 }
1003 }1038 }
1004 bio_ctrl->end_io_func = end_bio_extent_readpage;1039 bio_ctrl->end_io_func = end_bbio_data_read;
1005 begin_page_read(fs_info, page);1040 begin_page_read(fs_info, page);
1006 while (cur <= end) {1041 while (cur <= end) {
1007 enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE;1042 enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE;
@@ -1027,8 +1062,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 19 unmodified lines
1027 BUG_ON(extent_map_end(em) <= cur);1062 BUG_ON(extent_map_end(em) <= cur);
1028 BUG_ON(end < cur);1063 BUG_ON(end < cur);
1029 1064
1030 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))1065 compress_type = extent_map_compression(em);
1031 compress_type = em->compress_type;
1032 1066
1033 iosize = min(extent_map_end(em) - cur, end - cur + 1);1067 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1034 iosize = ALIGN(iosize, blocksize);1068 iosize = ALIGN(iosize, blocksize);
@@ -1037,7 +1071,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 2 unmodified lines
1037 else1071 else
1038 disk_bytenr = em->block_start + extent_offset;1072 disk_bytenr = em->block_start + extent_offset;
1039 block_start = em->block_start;1073 block_start = em->block_start;
1040 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))1074 if (em->flags & EXTENT_FLAG_PREALLOC)
1041 block_start = EXTENT_MAP_HOLE;1075 block_start = EXTENT_MAP_HOLE;
1042 1076
1043 /*1077 /*
@@ -1074,7 +1108,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 30 unmodified lines
1074 * is a corner case so we prioritize correctness over1108 * is a corner case so we prioritize correctness over
1075 * non-optimal behavior (submitting 2 bios for the same extent).1109 * non-optimal behavior (submitting 2 bios for the same extent).
1076 */1110 */
1077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&1111 if (compress_type != BTRFS_COMPRESS_NONE &&
1078 prev_em_start && *prev_em_start != (u64)-1 &&1112 prev_em_start && *prev_em_start != (u64)-1 &&
1079 *prev_em_start != em->start)1113 *prev_em_start != em->start)
1080 force_bio_submit = true;1114 force_bio_submit = true;
@@ -1240,7 +1274,8 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, · 159 unmodified lines
1240static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,1274static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
1241 struct page *page, u64 *start, u64 *end)1275 struct page *page, u64 *start, u64 *end)
1242{1276{
1243 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;1277 struct folio *folio = page_folio(page);
1278 struct btrfs_subpage *subpage = folio_get_private(folio);
1244 struct btrfs_subpage_info *spi = fs_info->subpage_info;1279 struct btrfs_subpage_info *spi = fs_info->subpage_info;
1245 u64 orig_start = *start;1280 u64 orig_start = *start;
1246 /* Declare as unsigned long so we can use bitmap ops */1281 /* Declare as unsigned long so we can use bitmap ops */
@@ -1252,7 +1287,7 @@ static void find_next_dirty_byte(struct btrfs_fs_info *fs_info, · 5 unmodified lines
1252 * For regular sector size == page size case, since one page only1287 * For regular sector size == page size case, since one page only
1253 * contains one sector, we return the page offset directly.1288 * contains one sector, we return the page offset directly.
1254 */1289 */
1255 if (!btrfs_is_subpage(fs_info, page)) {1290 if (!btrfs_is_subpage(fs_info, page->mapping)) {
1256 *start = page_offset(page);1291 *start = page_offset(page);
1257 *end = page_offset(page) + PAGE_SIZE;1292 *end = page_offset(page) + PAGE_SIZE;
1258 return;1293 return;
@@ -1305,7 +1340,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 46 unmodified lines
1305 return 1;1340 return 1;
1306 }1341 }
1307 1342
1308 bio_ctrl->end_io_func = end_bio_extent_writepage;1343 bio_ctrl->end_io_func = end_bbio_data_write;
1309 while (cur <= end) {1344 while (cur <= end) {
1310 u32 len = end - cur + 1;1345 u32 len = end - cur + 1;
1311 u64 disk_bytenr;1346 u64 disk_bytenr;
@@ -1325,7 +1360,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 13 unmodified lines
1325 * writeback the sectors with subpage dirty bits,1360 * writeback the sectors with subpage dirty bits,
1326 * causing writeback without ordered extent.1361 * causing writeback without ordered extent.
1327 */1362 */
1328 btrfs_page_clear_dirty(fs_info, page, cur, len);1363 btrfs_folio_clear_dirty(fs_info, page_folio(page), cur, len);
1329 break;1364 break;
1330 }1365 }
1331 1366
@@ -1352,7 +1387,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 20 unmodified lines
1352 block_start = em->block_start;1387 block_start = em->block_start;
1353 disk_bytenr = em->block_start + extent_offset;1388 disk_bytenr = em->block_start + extent_offset;
1354 1389
1355 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));1390 ASSERT(!extent_map_is_compressed(em));
1356 ASSERT(block_start != EXTENT_MAP_HOLE);1391 ASSERT(block_start != EXTENT_MAP_HOLE);
1357 ASSERT(block_start != EXTENT_MAP_INLINE);1392 ASSERT(block_start != EXTENT_MAP_INLINE);
1358 1393
@@ -1377,7 +1412,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 18 unmodified lines
1377 * So clear subpage dirty bit here so next time we won't submit1412 * So clear subpage dirty bit here so next time we won't submit
1378 * page for range already written to disk.1413 * page for range already written to disk.
1379 */1414 */
1380 btrfs_page_clear_dirty(fs_info, page, cur, iosize);1415 btrfs_folio_clear_dirty(fs_info, page_folio(page), cur, iosize);
1381 1416
1382 submit_extent_page(bio_ctrl, disk_bytenr, page, iosize,1417 submit_extent_page(bio_ctrl, disk_bytenr, page, iosize,
1383 cur - page_offset(page));1418 cur - page_offset(page));
@@ -1385,7 +1420,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 1 unmodified line
1385 nr++;1420 nr++;
1386 }1421 }
1387 1422
1388 btrfs_page_assert_not_dirty(fs_info, page);1423 btrfs_folio_assert_not_dirty(fs_info, page_folio(page));
1389 *nr_ret = nr;1424 *nr_ret = nr;
1390 return 0;1425 return 0;
1391 1426
@@ -1607,24 +1642,23 @@ static struct extent_buffer *find_extent_buffer_nolock( · 215 unmodified lines
1607 return NULL;1642 return NULL;
1608}1643}
1609 1644
1610static void extent_buffer_write_end_io(struct btrfs_bio *bbio)1645static void end_bbio_meta_write(struct btrfs_bio *bbio)
1611{1646{
1612 struct extent_buffer *eb = bbio->private;1647 struct extent_buffer *eb = bbio->private;
1613 struct btrfs_fs_info *fs_info = eb->fs_info;1648 struct btrfs_fs_info *fs_info = eb->fs_info;
1614 bool uptodate = !bbio->bio.bi_status;1649 bool uptodate = !bbio->bio.bi_status;
1615 struct bvec_iter_all iter_all;1650 struct folio_iter fi;
1616 struct bio_vec *bvec;
1617 u32 bio_offset = 0;1651 u32 bio_offset = 0;
1618 1652
1619 if (!uptodate)1653 if (!uptodate)
1620 set_btree_ioerr(eb);1654 set_btree_ioerr(eb);
1621 1655
1622 bio_for_each_segment_all(bvec, &bbio->bio, iter_all) {1656 bio_for_each_folio_all(fi, &bbio->bio) {
1623 u64 start = eb->start + bio_offset;1657 u64 start = eb->start + bio_offset;
1624 struct page *page = bvec->bv_page;1658 struct folio *folio = fi.folio;
1625 u32 len = bvec->bv_len;1659 u32 len = fi.length;
1626 1660
1627 btrfs_page_clear_writeback(fs_info, page, start, len);1661 btrfs_folio_clear_writeback(fs_info, folio, start, len);
1628 bio_offset += len;1662 bio_offset += len;
1629 }1663 }
1630 1664
@@ -1673,36 +1707,44 @@ static noinline_for_stack void write_one_eb(struct extent_buffer *eb, · 42 unmodified lines
1673 1707
1674 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,1708 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
1675 REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc),1709 REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc),
1676 eb->fs_info, extent_buffer_write_end_io, eb);1710 eb->fs_info, end_bbio_meta_write, eb);
1677 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;1711 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
1678 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);1712 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
1679 wbc_init_bio(wbc, &bbio->bio);1713 wbc_init_bio(wbc, &bbio->bio);
1680 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);1714 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);
1681 bbio->file_offset = eb->start;1715 bbio->file_offset = eb->start;
1682 if (fs_info->nodesize < PAGE_SIZE) {1716 if (fs_info->nodesize < PAGE_SIZE) {
1683 struct page *p = eb->pages[0];1717 struct folio *folio = eb->folios[0];
1718 bool ret;
1684 1719
1685 lock_page(p);1720 folio_lock(folio);
1686 btrfs_subpage_set_writeback(fs_info, p, eb->start, eb->len);1721 btrfs_subpage_set_writeback(fs_info, folio, eb->start, eb->len);
1687 if (btrfs_subpage_clear_and_test_dirty(fs_info, p, eb->start,1722 if (btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start,
1688 eb->len)) {1723 eb->len)) {
1689 clear_page_dirty_for_io(p);1724 folio_clear_dirty_for_io(folio);
1690 wbc->nr_to_write--;1725 wbc->nr_to_write--;
1691 }1726 }
1692 __bio_add_page(&bbio->bio, p, eb->len, eb->start - page_offset(p));1727 ret = bio_add_folio(&bbio->bio, folio, eb->len,
1693 wbc_account_cgroup_owner(wbc, p, eb->len);1728 eb->start - folio_pos(folio));
1694 unlock_page(p);1729 ASSERT(ret);
1730 wbc_account_cgroup_owner(wbc, folio_page(folio, 0), eb->len);
1731 folio_unlock(folio);
1695 } else {1732 } else {
1696 for (int i = 0; i < num_extent_pages(eb); i++) {1733 int num_folios = num_extent_folios(eb);
1697 struct page *p = eb->pages[i];1734
1698 1735 for (int i = 0; i < num_folios; i++) {
1699 lock_page(p);1736 struct folio *folio = eb->folios[i];
1700 clear_page_dirty_for_io(p);1737 bool ret;
1701 set_page_writeback(p);1738
1702 __bio_add_page(&bbio->bio, p, PAGE_SIZE, 0);1739 folio_lock(folio);
1703 wbc_account_cgroup_owner(wbc, p, PAGE_SIZE);1740 folio_clear_dirty_for_io(folio);
1704 wbc->nr_to_write--;1741 folio_start_writeback(folio);
1705 unlock_page(p);1742 ret = bio_add_folio(&bbio->bio, folio, folio_size(folio), 0);
1743 ASSERT(ret);
1744 wbc_account_cgroup_owner(wbc, folio_page(folio, 0),
1745 folio_size(folio));
1746 wbc->nr_to_write -= folio_nr_pages(folio);
1747 folio_unlock(folio);
1706 }1748 }
1707 }1749 }
1708 btrfs_submit_bio(bbio, 0);1750 btrfs_submit_bio(bbio, 0);
@@ -1725,6 +1767,7 @@ static noinline_for_stack void write_one_eb(struct extent_buffer *eb, · 16 unmodified lines
1725static int submit_eb_subpage(struct page *page, struct writeback_control *wbc)1767static int submit_eb_subpage(struct page *page, struct writeback_control *wbc)
1726{1768{
1727 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);1769 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
1770 struct folio *folio = page_folio(page);
1728 int submitted = 0;1771 int submitted = 0;
1729 u64 page_start = page_offset(page);1772 u64 page_start = page_offset(page);
1730 int bit_start = 0;1773 int bit_start = 0;
@@ -1732,7 +1775,7 @@ static int submit_eb_subpage(struct page *page, struct writeback_control *wbc) · 1 unmodified line
1732 1775
1733 /* Lock and write each dirty extent buffers in the range */1776 /* Lock and write each dirty extent buffers in the range */
1734 while (bit_start < fs_info->subpage_info->bitmap_nr_bits) {1777 while (bit_start < fs_info->subpage_info->bitmap_nr_bits) {
1735 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;1778 struct btrfs_subpage *subpage = folio_get_private(folio);
1736 struct extent_buffer *eb;1779 struct extent_buffer *eb;
1737 unsigned long flags;1780 unsigned long flags;
1738 u64 start;1781 u64 start;
@@ -1742,7 +1785,7 @@ static int submit_eb_subpage(struct page *page, struct writeback_control *wbc) · 3 unmodified lines
1742 * in the meantime.1785 * in the meantime.
1743 */1786 */
1744 spin_lock(&page->mapping->i_private_lock);1787 spin_lock(&page->mapping->i_private_lock);
1745 if (!PagePrivate(page)) {1788 if (!folio_test_private(folio)) {
1746 spin_unlock(&page->mapping->i_private_lock);1789 spin_unlock(&page->mapping->i_private_lock);
1747 break;1790 break;
1748 }1791 }
@@ -1807,22 +1850,23 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) · 58 unmodified lines
1807{1850{
1808 struct writeback_control *wbc = ctx->wbc;1851 struct writeback_control *wbc = ctx->wbc;
1809 struct address_space *mapping = page->mapping;1852 struct address_space *mapping = page->mapping;
1853 struct folio *folio = page_folio(page);
1810 struct extent_buffer *eb;1854 struct extent_buffer *eb;
1811 int ret;1855 int ret;
1812 1856
1813 if (!PagePrivate(page))1857 if (!folio_test_private(folio))
1814 return 0;1858 return 0;
1815 1859
1816 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)1860 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
1817 return submit_eb_subpage(page, wbc);1861 return submit_eb_subpage(page, wbc);
1818 1862
1819 spin_lock(&mapping->i_private_lock);1863 spin_lock(&mapping->i_private_lock);
1820 if (!PagePrivate(page)) {1864 if (!folio_test_private(folio)) {
1821 spin_unlock(&mapping->i_private_lock);1865 spin_unlock(&mapping->i_private_lock);
1822 return 0;1866 return 0;
1823 }1867 }
1824 1868
1825 eb = (struct extent_buffer *)page->private;1869 eb = folio_get_private(folio);
1826 1870
1827 /*1871 /*
1828 * Shouldn't happen and normally this would be a BUG_ON but no point1872 * Shouldn't happen and normally this would be a BUG_ON but no point
@@ -2201,7 +2245,7 @@ void extent_write_locked_range(struct inode *inode, struct page *locked_page, · 372 unmodified lines
2201 cur, cur_len, !ret);2245 cur, cur_len, !ret);
2202 mapping_set_error(page->mapping, ret);2246 mapping_set_error(page->mapping, ret);
2203 }2247 }
2204 btrfs_page_unlock_writer(fs_info, page, cur, cur_len);2248 btrfs_folio_unlock_writer(fs_info, page_folio(page), cur, cur_len);
2205 if (ret < 0)2249 if (ret < 0)
2206 found_error = true;2250 found_error = true;
2207next_page:2251next_page:
@@ -2352,7 +2396,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask) · 144 unmodified lines
2352 write_unlock(&map->lock);2396 write_unlock(&map->lock);
2353 break;2397 break;
2354 }2398 }
2355 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||2399 if ((em->flags & EXTENT_FLAG_PINNED) ||
2356 em->start != start) {2400 em->start != start) {
2357 write_unlock(&map->lock);2401 write_unlock(&map->lock);
2358 free_extent_map(em);2402 free_extent_map(em);
@@ -2369,7 +2413,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask) · 10 unmodified lines
2369 * extra reference on the em.2413 * extra reference on the em.
2370 */2414 */
2371 if (list_empty(&em->list) ||2415 if (list_empty(&em->list) ||
2372 test_bit(EXTENT_FLAG_LOGGING, &em->flags))2416 (em->flags & EXTENT_FLAG_LOGGING))
2373 goto remove_em;2417 goto remove_em;
2374 /*2418 /*
2375 * If it's in the list of modified extents, remove it2419 * If it's in the list of modified extents, remove it
@@ -3058,14 +3102,14 @@ static int extent_buffer_under_io(const struct extent_buffer *eb) · 682 unmodified lines
3058 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));3102 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3059}3103}
3060 3104
3061static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page)3105static bool folio_range_has_eb(struct btrfs_fs_info *fs_info, struct folio *folio)
3062{3106{
3063 struct btrfs_subpage *subpage;3107 struct btrfs_subpage *subpage;
3064 3108
3065 lockdep_assert_held(&page->mapping->i_private_lock);3109 lockdep_assert_held(&folio->mapping->i_private_lock);
3066 3110
3067 if (PagePrivate(page)) {3111 if (folio_test_private(folio)) {
3068 subpage = (struct btrfs_subpage *)page->private;3112 subpage = folio_get_private(folio);
3069 if (atomic_read(&subpage->eb_refs))3113 if (atomic_read(&subpage->eb_refs))
3070 return true;3114 return true;
3071 /*3115 /*
@@ -3078,21 +3122,21 @@ static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page) · 6 unmodified lines
3078 return false;3122 return false;
3079}3123}
3080 3124
3081static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page)3125static void detach_extent_buffer_folio(struct extent_buffer *eb, struct folio *folio)
3082{3126{
3083 struct btrfs_fs_info *fs_info = eb->fs_info;3127 struct btrfs_fs_info *fs_info = eb->fs_info;
3084 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);3128 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
3085 3129
3086 /*3130 /*
3087 * For mapped eb, we're going to change the page private, which should3131 * For mapped eb, we're going to change the folio private, which should
3088 * be done under the i_private_lock.3132 * be done under the i_private_lock.
3089 */3133 */
3090 if (mapped)3134 if (mapped)
3091 spin_lock(&page->mapping->i_private_lock);3135 spin_lock(&folio->mapping->i_private_lock);
3092 3136
3093 if (!PagePrivate(page)) {3137 if (!folio_test_private(folio)) {
3094 if (mapped)3138 if (mapped)
3095 spin_unlock(&page->mapping->i_private_lock);3139 spin_unlock(&folio->mapping->i_private_lock);
3096 return;3140 return;
3097 }3141 }
3098 3142
@@ -3101,66 +3145,58 @@ static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *pag · 2 unmodified lines
3101 * We do this since we'll remove the pages after we've3145 * We do this since we'll remove the pages after we've
3102 * removed the eb from the radix tree, so we could race3146 * removed the eb from the radix tree, so we could race
3103 * and have this page now attached to the new eb. So3147 * and have this page now attached to the new eb. So
3104 * only clear page_private if it's still connected to3148 * only clear folio if it's still connected to
3105 * this eb.3149 * this eb.
3106 */3150 */
3107 if (PagePrivate(page) &&3151 if (folio_test_private(folio) && folio_get_private(folio) == eb) {
3108 page->private == (unsigned long)eb) {
3109 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));3152 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3110 BUG_ON(PageDirty(page));3153 BUG_ON(folio_test_dirty(folio));
3111 BUG_ON(PageWriteback(page));3154 BUG_ON(folio_test_writeback(folio));
3112 /*3155 /* We need to make sure we haven't be attached to a new eb. */
3113 * We need to make sure we haven't be attached3156 folio_detach_private(folio);
3114 * to a new eb.
3115 */
3116 detach_page_private(page);
3117 }3157 }
3118 if (mapped)3158 if (mapped)
3119 spin_unlock(&page->mapping->i_private_lock);3159 spin_unlock(&folio->mapping->i_private_lock);
3120 return;3160 return;
3121 }3161 }
3122 3162
3123 /*3163 /*
3124 * For subpage, we can have dummy eb with page private. In this case,3164 * For subpage, we can have dummy eb with folio private attached. In
3125 * we can directly detach the private as such page is only attached to3165 * this case, we can directly detach the private as such folio is only
3126 * one dummy eb, no sharing.3166 * attached to one dummy eb, no sharing.
3127 */3167 */
3128 if (!mapped) {3168 if (!mapped) {
3129 btrfs_detach_subpage(fs_info, page);3169 btrfs_detach_subpage(fs_info, folio);
3130 return;3170 return;
3131 }3171 }
3132 3172
3133 btrfs_page_dec_eb_refs(fs_info, page);3173 btrfs_folio_dec_eb_refs(fs_info, folio);
3134 3174
3135 /*3175 /*
3136 * We can only detach the page private if there are no other ebs in the3176 * We can only detach the folio private if there are no other ebs in the
3137 * page range and no unfinished IO.3177 * page range and no unfinished IO.
3138 */3178 */
3139 if (!page_range_has_eb(fs_info, page))3179 if (!folio_range_has_eb(fs_info, folio))
3140 btrfs_detach_subpage(fs_info, page);3180 btrfs_detach_subpage(fs_info, folio);
3141 3181
3142 spin_unlock(&page->mapping->i_private_lock);3182 spin_unlock(&folio->mapping->i_private_lock);
3143}3183}
3144 3184
3145/* Release all pages attached to the extent buffer */3185/* Release all pages attached to the extent buffer */
3146static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)3186static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
3147{3187{
3148 int i;
3149 int num_pages;
3150
3151 ASSERT(!extent_buffer_under_io(eb));3188 ASSERT(!extent_buffer_under_io(eb));
3152 3189
3153 num_pages = num_extent_pages(eb);3190 for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) {
3154 for (i = 0; i < num_pages; i++) {3191 struct folio *folio = eb->folios[i];
3155 struct page *page = eb->pages[i];
3156 3192
3157 if (!page)3193 if (!folio)
3158 continue;3194 continue;
3159 3195
3160 detach_extent_buffer_page(eb, page);3196 detach_extent_buffer_folio(eb, folio);
3161 3197
3162 /* One for when we allocated the page */3198 /* One for when we allocated the folio. */
3163 put_page(page);3199 folio_put(folio);
3164 }3200 }
3165}3201}
3166 3202
@@ -3198,9 +3234,8 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, · 31 unmodified lines
3198 3234
3199struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)3235struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
3200{3236{
3201 int i;
3202 struct extent_buffer *new;3237 struct extent_buffer *new;
3203 int num_pages = num_extent_pages(src);3238 int num_folios = num_extent_folios(src);
3204 int ret;3239 int ret;
3205 3240
3206 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);3241 new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
@@ -3214,22 +3249,22 @@ struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) · 7 unmodified lines
3214 */3249 */
3215 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);3250 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
3216 3251
3217 ret = btrfs_alloc_page_array(num_pages, new->pages);3252 ret = alloc_eb_folio_array(new, 0);
3218 if (ret) {3253 if (ret) {
3219 btrfs_release_extent_buffer(new);3254 btrfs_release_extent_buffer(new);
3220 return NULL;3255 return NULL;
3221 }3256 }
3222 3257
3223 for (i = 0; i < num_pages; i++) {3258 for (int i = 0; i < num_folios; i++) {
3259 struct folio *folio = new->folios[i];
3224 int ret;3260 int ret;
3225 struct page *p = new->pages[i];
3226 3261
3227 ret = attach_extent_buffer_page(new, p, NULL);3262 ret = attach_extent_buffer_folio(new, folio, NULL);
3228 if (ret < 0) {3263 if (ret < 0) {
3229 btrfs_release_extent_buffer(new);3264 btrfs_release_extent_buffer(new);
3230 return NULL;3265 return NULL;
3231 }3266 }
3232 WARN_ON(PageDirty(p));3267 WARN_ON(folio_test_dirty(folio));
3233 }3268 }
3234 copy_extent_buffer_full(new, src);3269 copy_extent_buffer_full(new, src);
3235 set_extent_buffer_uptodate(new);3270 set_extent_buffer_uptodate(new);
@@ -3241,23 +3276,20 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, · 5 unmodified lines
3241 u64 start, unsigned long len)3276 u64 start, unsigned long len)
3242{3277{
3243 struct extent_buffer *eb;3278 struct extent_buffer *eb;
3244 int num_pages;3279 int num_folios = 0;
3245 int i;
3246 int ret;3280 int ret;
3247 3281
3248 eb = __alloc_extent_buffer(fs_info, start, len);3282 eb = __alloc_extent_buffer(fs_info, start, len);
3249 if (!eb)3283 if (!eb)
3250 return NULL;3284 return NULL;
3251 3285
3252 num_pages = num_extent_pages(eb);3286 ret = alloc_eb_folio_array(eb, 0);
3253 ret = btrfs_alloc_page_array(num_pages, eb->pages);
3254 if (ret)3287 if (ret)
3255 goto err;3288 goto err;
3256 3289
3257 for (i = 0; i < num_pages; i++) {3290 num_folios = num_extent_folios(eb);
3258 struct page *p = eb->pages[i];3291 for (int i = 0; i < num_folios; i++) {
3259 3292 ret = attach_extent_buffer_folio(eb, eb->folios[i], NULL);
3260 ret = attach_extent_buffer_page(eb, p, NULL);
3261 if (ret < 0)3293 if (ret < 0)
3262 goto err;3294 goto err;
3263 }3295 }
@@ -3268,10 +3300,10 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, · 4 unmodified lines
3268 3300
3269 return eb;3301 return eb;
3270err:3302err:
3271 for (i = 0; i < num_pages; i++) {3303 for (int i = 0; i < num_folios; i++) {
3272 if (eb->pages[i]) {3304 if (eb->folios[i]) {
3273 detach_extent_buffer_page(eb, eb->pages[i]);3305 detach_extent_buffer_folio(eb, eb->folios[i]);
3274 __free_page(eb->pages[i]);3306 __folio_put(eb->folios[i]);
3275 }3307 }
3276 }3308 }
3277 __free_extent_buffer(eb);3309 __free_extent_buffer(eb);
@@ -3320,20 +3352,14 @@ static void check_buffer_tree_ref(struct extent_buffer *eb) · 42 unmodified lines
3320 spin_unlock(&eb->refs_lock);3352 spin_unlock(&eb->refs_lock);
3321}3353}
3322 3354
3323static void mark_extent_buffer_accessed(struct extent_buffer *eb,3355static void mark_extent_buffer_accessed(struct extent_buffer *eb)
3324 struct page *accessed)
3325{3356{
3326 int num_pages, i;3357 int num_folios= num_extent_folios(eb);
3327 3358
3328 check_buffer_tree_ref(eb);3359 check_buffer_tree_ref(eb);
3329 3360
3330 num_pages = num_extent_pages(eb);3361 for (int i = 0; i < num_folios; i++)
3331 for (i = 0; i < num_pages; i++) {3362 folio_mark_accessed(eb->folios[i]);
3332 struct page *p = eb->pages[i];
3333
3334 if (p != accessed)
3335 mark_page_accessed(p);
3336 }
3337}3363}
3338 3364
3339struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,3365struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
@@ -3361,7 +3387,7 @@ struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, · 21 unmodified lines
3361 spin_lock(&eb->refs_lock);3387 spin_lock(&eb->refs_lock);
3362 spin_unlock(&eb->refs_lock);3388 spin_unlock(&eb->refs_lock);
3363 }3389 }
3364 mark_extent_buffer_accessed(eb, NULL);3390 mark_extent_buffer_accessed(eb);
3365 return eb;3391 return eb;
3366}3392}
3367 3393
@@ -3410,6 +3436,7 @@ free_eb: · 42 unmodified lines
3410static struct extent_buffer *grab_extent_buffer(3436static struct extent_buffer *grab_extent_buffer(
3411 struct btrfs_fs_info *fs_info, struct page *page)3437 struct btrfs_fs_info *fs_info, struct page *page)
3412{3438{
3439 struct folio *folio = page_folio(page);
3413 struct extent_buffer *exists;3440 struct extent_buffer *exists;
3414 3441
3415 /*3442 /*
@@ -3421,21 +3448,21 @@ static struct extent_buffer *grab_extent_buffer( · 5 unmodified lines
3421 return NULL;3448 return NULL;
3422 3449
3423 /* Page not yet attached to an extent buffer */3450 /* Page not yet attached to an extent buffer */
3424 if (!PagePrivate(page))3451 if (!folio_test_private(folio))
3425 return NULL;3452 return NULL;
3426 3453
3427 /*3454 /*
3428 * We could have already allocated an eb for this page and attached one3455 * We could have already allocated an eb for this page and attached one
3429 * so lets see if we can get a ref on the existing eb, and if we can we3456 * so lets see if we can get a ref on the existing eb, and if we can we
3430 * know it's good and we can just return that one, else we know we can3457 * know it's good and we can just return that one, else we know we can
3431 * just overwrite page->private.3458 * just overwrite folio private.
3432 */3459 */
3433 exists = (struct extent_buffer *)page->private;3460 exists = folio_get_private(folio);
3434 if (atomic_inc_not_zero(&exists->refs))3461 if (atomic_inc_not_zero(&exists->refs))
3435 return exists;3462 return exists;
3436 3463
3437 WARN_ON(PageDirty(page));3464 WARN_ON(PageDirty(page));
3438 detach_page_private(page);3465 folio_detach_private(folio);
3439 return NULL;3466 return NULL;
3440}3467}
3441 3468
@@ -3469,19 +3496,88 @@ static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) · 27 unmodified lines
3469 return 0;3496 return 0;
3470}3497}
3471 3498
3499
3500/*
3501 * Return 0 if eb->folios[i] is attached to btree inode successfully.
3502 * Return >0 if there is already another extent buffer for the range,
3503 * and @found_eb_ret would be updated.
3504 * Return -EAGAIN if the filemap has an existing folio but with different size
3505 * than @eb.
3506 * The caller needs to free the existing folios and retry using the same order.
3507 */
3508static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
3509 struct extent_buffer **found_eb_ret)
3510{
3511
3512 struct btrfs_fs_info *fs_info = eb->fs_info;
3513 struct address_space *mapping = fs_info->btree_inode->i_mapping;
3514 const unsigned long index = eb->start >> PAGE_SHIFT;
3515 struct folio *existing_folio;
3516 int ret;
3517
3518 ASSERT(found_eb_ret);
3519
3520 /* Caller should ensure the folio exists. */
3521 ASSERT(eb->folios[i]);
3522
3523retry:
3524 ret = filemap_add_folio(mapping, eb->folios[i], index + i,
3525 GFP_NOFS | __GFP_NOFAIL);
3526 if (!ret)
3527 return 0;
3528
3529 existing_folio = filemap_lock_folio(mapping, index + i);
3530 /* The page cache only exists for a very short time, just retry. */
3531 if (IS_ERR(existing_folio))
3532 goto retry;
3533
3534 /* For now, we should only have single-page folios for btree inode. */
3535 ASSERT(folio_nr_pages(existing_folio) == 1);
3536
3537 if (folio_size(existing_folio) != folio_size(eb->folios[0])) {
3538 folio_unlock(existing_folio);
3539 folio_put(existing_folio);
3540 return -EAGAIN;
3541 }
3542
3543 if (fs_info->nodesize < PAGE_SIZE) {
3544 /*
3545 * We're going to reuse the existing page, can drop our page
3546 * and subpage structure now.
3547 */
3548 __free_page(folio_page(eb->folios[i], 0));
3549 eb->folios[i] = existing_folio;
3550 } else {
3551 struct extent_buffer *existing_eb;
3552
3553 existing_eb = grab_extent_buffer(fs_info,
3554 folio_page(existing_folio, 0));
3555 if (existing_eb) {
3556 /* The extent buffer still exists, we can use it directly. */
3557 *found_eb_ret = existing_eb;
3558 folio_unlock(existing_folio);
3559 folio_put(existing_folio);
3560 return 1;
3561 }
3562 /* The extent buffer no longer exists, we can reuse the folio. */
3563 __free_page(folio_page(eb->folios[i], 0));
3564 eb->folios[i] = existing_folio;
3565 }
3566 return 0;
3567}
3568
3472struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,3569struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
3473 u64 start, u64 owner_root, int level)3570 u64 start, u64 owner_root, int level)
3474{3571{
3475 unsigned long len = fs_info->nodesize;3572 unsigned long len = fs_info->nodesize;
3476 int num_pages;3573 int num_folios;
3477 int i;3574 int attached = 0;
3478 unsigned long index = start >> PAGE_SHIFT;
3479 struct extent_buffer *eb;3575 struct extent_buffer *eb;
3480 struct extent_buffer *exists = NULL;3576 struct extent_buffer *existing_eb = NULL;
3481 struct page *p;
3482 struct address_space *mapping = fs_info->btree_inode->i_mapping;3577 struct address_space *mapping = fs_info->btree_inode->i_mapping;
3483 struct btrfs_subpage *prealloc = NULL;3578 struct btrfs_subpage *prealloc = NULL;
3484 u64 lockdep_owner = owner_root;3579 u64 lockdep_owner = owner_root;
3580 bool page_contig = true;
3485 int uptodate = 1;3581 int uptodate = 1;
3486 int ret;3582 int ret;
3487 3583
@@ -3516,10 +3612,8 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, · 28 unmodified lines
3516 3612
3517 btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level);3613 btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level);
3518 3614
3519 num_pages = num_extent_pages(eb);
3520
3521 /*3615 /*
3522 * Preallocate page->private for subpage case, so that we won't3616 * Preallocate folio private for subpage case, so that we won't
3523 * allocate memory with i_private_lock nor page lock hold.3617 * allocate memory with i_private_lock nor page lock hold.
3524 *3618 *
3525 * The memory will be freed by attach_extent_buffer_page() or freed3619 * The memory will be freed by attach_extent_buffer_page() or freed
@@ -3528,47 +3622,89 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, · 2 unmodified lines
3528 if (fs_info->nodesize < PAGE_SIZE) {3622 if (fs_info->nodesize < PAGE_SIZE) {
3529 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);3623 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);
3530 if (IS_ERR(prealloc)) {3624 if (IS_ERR(prealloc)) {
3531 exists = ERR_CAST(prealloc);3625 ret = PTR_ERR(prealloc);
3532 goto free_eb;3626 goto out;
3533 }3627 }
3534 }3628 }
3535 3629
3536 for (i = 0; i < num_pages; i++, index++) {3630reallocate:
3537 p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);3631 /* Allocate all pages first. */
3538 if (!p) {3632 ret = alloc_eb_folio_array(eb, __GFP_NOFAIL);
3539 exists = ERR_PTR(-ENOMEM);3633 if (ret < 0) {
3540 btrfs_free_subpage(prealloc);3634 btrfs_free_subpage(prealloc);
3541 goto free_eb;3635 goto out;
3636 }
3637
3638 num_folios = num_extent_folios(eb);
3639 /* Attach all pages to the filemap. */
3640 for (int i = 0; i < num_folios; i++) {
3641 struct folio *folio;
3642
3643 ret = attach_eb_folio_to_filemap(eb, i, &existing_eb);
3644 if (ret > 0) {
3645 ASSERT(existing_eb);
3646 goto out;
3542 }3647 }
3543 3648
3544 spin_lock(&mapping->i_private_lock);3649 /*
3545 exists = grab_extent_buffer(fs_info, p);3650 * TODO: Special handling for a corner case where the order of
3546 if (exists) {3651 * folios mismatch between the new eb and filemap.
3547 spin_unlock(&mapping->i_private_lock);3652 *
3548 unlock_page(p);3653 * This happens when:
3549 put_page(p);3654 *
3550 mark_extent_buffer_accessed(exists, p);3655 * - the new eb is using higher order folio
3551 btrfs_free_subpage(prealloc);3656 *
3552 goto free_eb;3657 * - the filemap is still using 0-order folios for the range
3658 * This can happen at the previous eb allocation, and we don't
3659 * have higher order folio for the call.
3660 *
3661 * - the existing eb has already been freed
3662 *
3663 * In this case, we have to free the existing folios first, and
3664 * re-allocate using the same order.
3665 * Thankfully this is not going to happen yet, as we're still
3666 * using 0-order folios.
3667 */
3668 if (unlikely(ret == -EAGAIN)) {
3669 ASSERT(0);
3670 goto reallocate;
3553 }3671 }
3672 attached++;
3673
3674 /*
3675 * Only after attach_eb_folio_to_filemap(), eb->folios[] is
3676 * reliable, as we may choose to reuse the existing page cache
3677 * and free the allocated page.
3678 */
3679 folio = eb->folios[i];
3680 spin_lock(&mapping->i_private_lock);
3554 /* Should not fail, as we have preallocated the memory */3681 /* Should not fail, as we have preallocated the memory */
3555 ret = attach_extent_buffer_page(eb, p, prealloc);3682 ret = attach_extent_buffer_folio(eb, folio, prealloc);
3556 ASSERT(!ret);3683 ASSERT(!ret);
3557 /*3684 /*
3558 * To inform we have extra eb under allocation, so that3685 * To inform we have extra eb under allocation, so that
3559 * detach_extent_buffer_page() won't release the page private3686 * detach_extent_buffer_page() won't release the folio private
3560 * when the eb hasn't yet been inserted into radix tree.3687 * when the eb hasn't yet been inserted into radix tree.
3561 *3688 *
3562 * The ref will be decreased when the eb released the page, in3689 * The ref will be decreased when the eb released the page, in
3563 * detach_extent_buffer_page().3690 * detach_extent_buffer_page().
3564 * Thus needs no special handling in error path.3691 * Thus needs no special handling in error path.
3565 */3692 */
3566 btrfs_page_inc_eb_refs(fs_info, p);3693 btrfs_folio_inc_eb_refs(fs_info, folio);
3567 spin_unlock(&mapping->i_private_lock);3694 spin_unlock(&mapping->i_private_lock);
3568 3695
3569 WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len));3696 WARN_ON(btrfs_folio_test_dirty(fs_info, folio, eb->start, eb->len));
3570 eb->pages[i] = p;3697
3571 if (!btrfs_page_test_uptodate(fs_info, p, eb->start, eb->len))3698 /*
3699 * Check if the current page is physically contiguous with previous eb
3700 * page.
3701 * At this stage, either we allocated a large folio, thus @i
3702 * would only be 0, or we fall back to per-page allocation.
3703 */
3704 if (i && folio_page(eb->folios[i - 1], 0) + 1 != folio_page(folio, 0))
3705 page_contig = false;
3706
3707 if (!btrfs_folio_test_uptodate(fs_info, folio, eb->start, eb->len))
3572 uptodate = 0;3708 uptodate = 0;
3573 3709
3574 /*3710 /*
@@ -3581,12 +3717,13 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, · 6 unmodified lines
3581 }3717 }
3582 if (uptodate)3718 if (uptodate)
3583 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);3719 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3720 /* All pages are physically contiguous, can skip cross page handling. */
3721 if (page_contig)
3722 eb->addr = folio_address(eb->folios[0]) + offset_in_page(eb->start);
3584again:3723again:
3585 ret = radix_tree_preload(GFP_NOFS);3724 ret = radix_tree_preload(GFP_NOFS);
3586 if (ret) {3725 if (ret)
3587 exists = ERR_PTR(ret);3726 goto out;
3588 goto free_eb;
3589 }
3590 3727
3591 spin_lock(&fs_info->buffer_lock);3728 spin_lock(&fs_info->buffer_lock);
3592 ret = radix_tree_insert(&fs_info->buffer_radix,3729 ret = radix_tree_insert(&fs_info->buffer_radix,
@@ -3594,9 +3731,10 @@ again: · 1 unmodified line
3594 spin_unlock(&fs_info->buffer_lock);3731 spin_unlock(&fs_info->buffer_lock);
3595 radix_tree_preload_end();3732 radix_tree_preload_end();
3596 if (ret == -EEXIST) {3733 if (ret == -EEXIST) {
3597 exists = find_extent_buffer(fs_info, start);3734 ret = 0;
3598 if (exists)3735 existing_eb = find_extent_buffer(fs_info, start);
3599 goto free_eb;3736 if (existing_eb)
3737 goto out;
3600 else3738 else
3601 goto again;3739 goto again;
3602 }3740 }
@@ -3609,19 +3747,46 @@ again: · 6 unmodified lines
3609 * btree_release_folio will correctly detect that a page belongs to a3747 * btree_release_folio will correctly detect that a page belongs to a
3610 * live buffer and won't free them prematurely.3748 * live buffer and won't free them prematurely.
3611 */3749 */
3612 for (i = 0; i < num_pages; i++)3750 for (int i = 0; i < num_folios; i++)
3613 unlock_page(eb->pages[i]);3751 unlock_page(folio_page(eb->folios[i], 0));
3614 return eb;3752 return eb;
3615 3753
3616free_eb:3754out:
3617 WARN_ON(!atomic_dec_and_test(&eb->refs));3755 WARN_ON(!atomic_dec_and_test(&eb->refs));
3618 for (i = 0; i < num_pages; i++) {3756
3619 if (eb->pages[i])3757 /*
3620 unlock_page(eb->pages[i]);3758 * Any attached folios need to be detached before we unlock them. This
3759 * is because when we're inserting our new folios into the mapping, and
3760 * then attaching our eb to that folio. If we fail to insert our folio
3761 * we'll lookup the folio for that index, and grab that EB. We do not
3762 * want that to grab this eb, as we're getting ready to free it. So we
3763 * have to detach it first and then unlock it.
3764 *
3765 * We have to drop our reference and NULL it out here because in the
3766 * subpage case detaching does a btrfs_folio_dec_eb_refs() for our eb.
3767 * Below when we call btrfs_release_extent_buffer() we will call
3768 * detach_extent_buffer_folio() on our remaining pages in the !subpage
3769 * case. If we left eb->folios[i] populated in the subpage case we'd
3770 * double put our reference and be super sad.
3771 */
3772 for (int i = 0; i < attached; i++) {
3773 ASSERT(eb->folios[i]);
3774 detach_extent_buffer_folio(eb, eb->folios[i]);
3775 unlock_page(folio_page(eb->folios[i], 0));
3776 folio_put(eb->folios[i]);
3777 eb->folios[i] = NULL;
3621 }3778 }
3779 /*
3780 * Now all pages of that extent buffer is unmapped, set UNMAPPED flag,
3781 * so it can be cleaned up without utlizing page->mapping.
3782 */
3783 set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
3622 3784
3623 btrfs_release_extent_buffer(eb);3785 btrfs_release_extent_buffer(eb);
3624 return exists;3786 if (ret < 0)
3787 return ERR_PTR(ret);
3788 ASSERT(existing_eb);
3789 return existing_eb;
3625}3790}
3626 3791
3627static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)3792static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
@@ -3713,31 +3878,30 @@ void free_extent_buffer_stale(struct extent_buffer *eb) · 85 unmodified lines
3713 release_extent_buffer(eb);3878 release_extent_buffer(eb);
3714}3879}
3715 3880
3716static void btree_clear_page_dirty(struct page *page)3881static void btree_clear_folio_dirty(struct folio *folio)
3717{3882{
3718 ASSERT(PageDirty(page));3883 ASSERT(folio_test_dirty(folio));
3719 ASSERT(PageLocked(page));3884 ASSERT(folio_test_locked(folio));
3720 clear_page_dirty_for_io(page);3885 folio_clear_dirty_for_io(folio);
3721 xa_lock_irq(&page->mapping->i_pages);3886 xa_lock_irq(&folio->mapping->i_pages);
3722 if (!PageDirty(page))3887 if (!folio_test_dirty(folio))
3723 __xa_clear_mark(&page->mapping->i_pages,3888 __xa_clear_mark(&folio->mapping->i_pages,
3724 page_index(page), PAGECACHE_TAG_DIRTY);3889 folio_index(folio), PAGECACHE_TAG_DIRTY);
3725 xa_unlock_irq(&page->mapping->i_pages);3890 xa_unlock_irq(&folio->mapping->i_pages);
3726}3891}
3727 3892
3728static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)3893static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)
3729{3894{
3730 struct btrfs_fs_info *fs_info = eb->fs_info;3895 struct btrfs_fs_info *fs_info = eb->fs_info;
3731 struct page *page = eb->pages[0];3896 struct folio *folio = eb->folios[0];
3732 bool last;3897 bool last;
3733 3898
3734 /* btree_clear_page_dirty() needs page locked */3899 /* btree_clear_folio_dirty() needs page locked. */
3735 lock_page(page);3900 folio_lock(folio);
3736 last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start,3901 last = btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start, eb->len);
3737 eb->len);
3738 if (last)3902 if (last)
3739 btree_clear_page_dirty(page);3903 btree_clear_folio_dirty(folio);
3740 unlock_page(page);3904 folio_unlock(folio);
3741 WARN_ON(atomic_read(&eb->refs) == 0);3905 WARN_ON(atomic_read(&eb->refs) == 0);
3742}3906}
3743 3907
@@ -3745,15 +3909,27 @@ void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, · 1 unmodified line
3745 struct extent_buffer *eb)3909 struct extent_buffer *eb)
3746{3910{
3747 struct btrfs_fs_info *fs_info = eb->fs_info;3911 struct btrfs_fs_info *fs_info = eb->fs_info;
3748 int i;3912 int num_folios;
3749 int num_pages;
3750 struct page *page;
3751 3913
3752 btrfs_assert_tree_write_locked(eb);3914 btrfs_assert_tree_write_locked(eb);
3753 3915
3754 if (trans && btrfs_header_generation(eb) != trans->transid)3916 if (trans && btrfs_header_generation(eb) != trans->transid)
3755 return;3917 return;
3756 3918
3919 /*
3920 * Instead of clearing the dirty flag off of the buffer, mark it as
3921 * EXTENT_BUFFER_ZONED_ZEROOUT. This allows us to preserve
3922 * write-ordering in zoned mode, without the need to later re-dirty
3923 * the extent_buffer.
3924 *
3925 * The actual zeroout of the buffer will happen later in
3926 * btree_csum_one_bio.
3927 */
3928 if (btrfs_is_zoned(fs_info)) {
3929 set_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags);
3930 return;
3931 }
3932
3757 if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags))3933 if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags))
3758 return;3934 return;
3759 3935
@@ -3763,30 +3939,29 @@ void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, · 3 unmodified lines
3763 if (eb->fs_info->nodesize < PAGE_SIZE)3939 if (eb->fs_info->nodesize < PAGE_SIZE)
3764 return clear_subpage_extent_buffer_dirty(eb);3940 return clear_subpage_extent_buffer_dirty(eb);
3765 3941
3766 num_pages = num_extent_pages(eb);3942 num_folios = num_extent_folios(eb);
3943 for (int i = 0; i < num_folios; i++) {
3944 struct folio *folio = eb->folios[i];
3767 3945
3768 for (i = 0; i < num_pages; i++) {3946 if (!folio_test_dirty(folio))
3769 page = eb->pages[i];
3770 if (!PageDirty(page))
3771 continue;3947 continue;
3772 lock_page(page);3948 folio_lock(folio);
3773 btree_clear_page_dirty(page);3949 btree_clear_folio_dirty(folio);
3774 unlock_page(page);3950 folio_unlock(folio);
3775 }3951 }
3776 WARN_ON(atomic_read(&eb->refs) == 0);3952 WARN_ON(atomic_read(&eb->refs) == 0);
3777}3953}
3778 3954
3779void set_extent_buffer_dirty(struct extent_buffer *eb)3955void set_extent_buffer_dirty(struct extent_buffer *eb)
3780{3956{
3781 int i;3957 int num_folios;
3782 int num_pages;
3783 bool was_dirty;3958 bool was_dirty;
3784 3959
3785 check_buffer_tree_ref(eb);3960 check_buffer_tree_ref(eb);
3786 3961
3787 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);3962 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3788 3963
3789 num_pages = num_extent_pages(eb);3964 num_folios = num_extent_folios(eb);
3790 WARN_ON(atomic_read(&eb->refs) == 0);3965 WARN_ON(atomic_read(&eb->refs) == 0);
3791 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));3966 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
3792 3967
@@ -3805,34 +3980,32 @@ void set_extent_buffer_dirty(struct extent_buffer *eb) · 12 unmodified lines
3805 * the above race.3980 * the above race.
3806 */3981 */
3807 if (subpage)3982 if (subpage)
3808 lock_page(eb->pages[0]);3983 lock_page(folio_page(eb->folios[0], 0));
3809 for (i = 0; i < num_pages; i++)3984 for (int i = 0; i < num_folios; i++)
3810 btrfs_page_set_dirty(eb->fs_info, eb->pages[i],3985 btrfs_folio_set_dirty(eb->fs_info, eb->folios[i],
3811 eb->start, eb->len);3986 eb->start, eb->len);
3812 if (subpage)3987 if (subpage)
3813 unlock_page(eb->pages[0]);3988 unlock_page(folio_page(eb->folios[0], 0));
3814 percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes,3989 percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes,
3815 eb->len,3990 eb->len,
3816 eb->fs_info->dirty_metadata_batch);3991 eb->fs_info->dirty_metadata_batch);
3817 }3992 }
3818#ifdef CONFIG_BTRFS_DEBUG3993#ifdef CONFIG_BTRFS_DEBUG
3819 for (i = 0; i < num_pages; i++)3994 for (int i = 0; i < num_folios; i++)
3820 ASSERT(PageDirty(eb->pages[i]));3995 ASSERT(folio_test_dirty(eb->folios[i]));
3821#endif3996#endif
3822}3997}
3823 3998
3824void clear_extent_buffer_uptodate(struct extent_buffer *eb)3999void clear_extent_buffer_uptodate(struct extent_buffer *eb)
3825{4000{
3826 struct btrfs_fs_info *fs_info = eb->fs_info;4001 struct btrfs_fs_info *fs_info = eb->fs_info;
3827 struct page *page;4002 int num_folios = num_extent_folios(eb);
3828 int num_pages;
3829 int i;
3830 4003
3831 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);4004 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3832 num_pages = num_extent_pages(eb);4005 for (int i = 0; i < num_folios; i++) {
3833 for (i = 0; i < num_pages; i++) {4006 struct folio *folio = eb->folios[i];
3834 page = eb->pages[i];4007
3835 if (!page)4008 if (!folio)
3836 continue;4009 continue;
3837 4010
3838 /*4011 /*
@@ -3840,44 +4013,40 @@ void clear_extent_buffer_uptodate(struct extent_buffer *eb) · 1 unmodified line
3840 * btrfs_is_subpage() can not handle cloned/dummy metadata.4013 * btrfs_is_subpage() can not handle cloned/dummy metadata.
3841 */4014 */
3842 if (fs_info->nodesize >= PAGE_SIZE)4015 if (fs_info->nodesize >= PAGE_SIZE)
3843 ClearPageUptodate(page);4016 folio_clear_uptodate(folio);
3844 else4017 else
3845 btrfs_subpage_clear_uptodate(fs_info, page, eb->start,4018 btrfs_subpage_clear_uptodate(fs_info, folio,
3846 eb->len);4019 eb->start, eb->len);
3847 }4020 }
3848}4021}
3849 4022
3850void set_extent_buffer_uptodate(struct extent_buffer *eb)4023void set_extent_buffer_uptodate(struct extent_buffer *eb)
3851{4024{
3852 struct btrfs_fs_info *fs_info = eb->fs_info;4025 struct btrfs_fs_info *fs_info = eb->fs_info;
3853 struct page *page;4026 int num_folios = num_extent_folios(eb);
3854 int num_pages;
3855 int i;
3856 4027
3857 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);4028 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3858 num_pages = num_extent_pages(eb);4029 for (int i = 0; i < num_folios; i++) {
3859 for (i = 0; i < num_pages; i++) {4030 struct folio *folio = eb->folios[i];
3860 page = eb->pages[i];
3861 4031
3862 /*4032 /*
3863 * This is special handling for metadata subpage, as regular4033 * This is special handling for metadata subpage, as regular
3864 * btrfs_is_subpage() can not handle cloned/dummy metadata.4034 * btrfs_is_subpage() can not handle cloned/dummy metadata.
3865 */4035 */
3866 if (fs_info->nodesize >= PAGE_SIZE)4036 if (fs_info->nodesize >= PAGE_SIZE)
3867 SetPageUptodate(page);4037 folio_mark_uptodate(folio);
3868 else4038 else
3869 btrfs_subpage_set_uptodate(fs_info, page, eb->start,4039 btrfs_subpage_set_uptodate(fs_info, folio,
3870 eb->len);4040 eb->start, eb->len);
3871 }4041 }
3872}4042}
3873 4043
3874static void extent_buffer_read_end_io(struct btrfs_bio *bbio)4044static void end_bbio_meta_read(struct btrfs_bio *bbio)
3875{4045{
3876 struct extent_buffer *eb = bbio->private;4046 struct extent_buffer *eb = bbio->private;
3877 struct btrfs_fs_info *fs_info = eb->fs_info;4047 struct btrfs_fs_info *fs_info = eb->fs_info;
3878 bool uptodate = !bbio->bio.bi_status;4048 bool uptodate = !bbio->bio.bi_status;
3879 struct bvec_iter_all iter_all;4049 struct folio_iter fi;
3880 struct bio_vec *bvec;
3881 u32 bio_offset = 0;4050 u32 bio_offset = 0;
3882 4051
3883 eb->read_mirror = bbio->mirror_num;4052 eb->read_mirror = bbio->mirror_num;
@@ -3893,15 +4062,15 @@ static void extent_buffer_read_end_io(struct btrfs_bio *bbio) · 9 unmodified lines
3893 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);4062 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
3894 }4063 }
3895 4064
3896 bio_for_each_segment_all(bvec, &bbio->bio, iter_all) {4065 bio_for_each_folio_all(fi, &bbio->bio) {
4066 struct folio *folio = fi.folio;
3897 u64 start = eb->start + bio_offset;4067 u64 start = eb->start + bio_offset;
3898 struct page *page = bvec->bv_page;4068 u32 len = fi.length;
3899 u32 len = bvec->bv_len;
3900 4069
3901 if (uptodate)4070 if (uptodate)
3902 btrfs_page_set_uptodate(fs_info, page, start, len);4071 btrfs_folio_set_uptodate(fs_info, folio, start, len);
3903 else4072 else
3904 btrfs_page_clear_uptodate(fs_info, page, start, len);4073 btrfs_folio_clear_uptodate(fs_info, folio, start, len);
3905 4074
3906 bio_offset += len;4075 bio_offset += len;
3907 }4076 }
@@ -3917,8 +4086,8 @@ static void extent_buffer_read_end_io(struct btrfs_bio *bbio) · 9 unmodified lines
3917int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num,4086int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num,
3918 struct btrfs_tree_parent_check *check)4087 struct btrfs_tree_parent_check *check)
3919{4088{
3920 int num_pages = num_extent_pages(eb), i;
3921 struct btrfs_bio *bbio;4089 struct btrfs_bio *bbio;
4090 bool ret;
3922 4091
3923 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))4092 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3924 return 0;4093 return 0;
@@ -3942,17 +4111,24 @@ int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, · 17 unmodified lines
3942 4111
3943 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,4112 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
3944 REQ_OP_READ | REQ_META, eb->fs_info,4113 REQ_OP_READ | REQ_META, eb->fs_info,
3945 extent_buffer_read_end_io, eb);4114 end_bbio_meta_read, eb);
3946 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;4115 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
3947 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);4116 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);
3948 bbio->file_offset = eb->start;4117 bbio->file_offset = eb->start;
3949 memcpy(&bbio->parent_check, check, sizeof(*check));4118 memcpy(&bbio->parent_check, check, sizeof(*check));
3950 if (eb->fs_info->nodesize < PAGE_SIZE) {4119 if (eb->fs_info->nodesize < PAGE_SIZE) {
3951 __bio_add_page(&bbio->bio, eb->pages[0], eb->len,4120 ret = bio_add_folio(&bbio->bio, eb->folios[0], eb->len,
3952 eb->start - page_offset(eb->pages[0]));4121 eb->start - folio_pos(eb->folios[0]));
4122 ASSERT(ret);
3953 } else {4123 } else {
3954 for (i = 0; i < num_pages; i++)4124 int num_folios = num_extent_folios(eb);
3955 __bio_add_page(&bbio->bio, eb->pages[i], PAGE_SIZE, 0);4125
4126 for (int i = 0; i < num_folios; i++) {
4127 struct folio *folio = eb->folios[i];
4128
4129 ret = bio_add_folio(&bbio->bio, folio, folio_size(folio), 0);
4130 ASSERT(ret);
4131 }
3956 }4132 }
3957 btrfs_submit_bio(bbio, mirror_num);4133 btrfs_submit_bio(bbio, mirror_num);
3958 4134
@@ -3999,29 +4175,33 @@ static inline int check_eb_range(const struct extent_buffer *eb, · 40 unmodified lines
3999void read_extent_buffer(const struct extent_buffer *eb, void *dstv,4175void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
4000 unsigned long start, unsigned long len)4176 unsigned long start, unsigned long len)
4001{4177{
4178 const int unit_size = folio_size(eb->folios[0]);
4002 size_t cur;4179 size_t cur;
4003 size_t offset;4180 size_t offset;
4004 struct page *page;
4005 char *kaddr;
4006 char *dst = (char *)dstv;4181 char *dst = (char *)dstv;
4007 unsigned long i = get_eb_page_index(start);4182 unsigned long i = get_eb_folio_index(eb, start);
4008 4183
4009 if (check_eb_range(eb, start, len)) {4184 if (check_eb_range(eb, start, len)) {
4010 /*4185 /*
4011 * Invalid range hit, reset the memory, so callers won't get4186 * Invalid range hit, reset the memory, so callers won't get
4012 * some random garbage for their uninitialzed memory.4187 * some random garbage for their uninitialized memory.
4013 */4188 */
4014 memset(dstv, 0, len);4189 memset(dstv, 0, len);
4015 return;4190 return;
4016 }4191 }
4017 4192
4018 offset = get_eb_offset_in_page(eb, start);4193 if (eb->addr) {
4194 memcpy(dstv, eb->addr + start, len);
4195 return;
4196 }
4197
4198 offset = get_eb_offset_in_folio(eb, start);
4019 4199
4020 while (len > 0) {4200 while (len > 0) {
4021 page = eb->pages[i];4201 char *kaddr;
4022 4202
4023 cur = min(len, (PAGE_SIZE - offset));4203 cur = min(len, unit_size - offset);
4024 kaddr = page_address(page);4204 kaddr = folio_address(eb->folios[i]);
4025 memcpy(dst, kaddr + offset, cur);4205 memcpy(dst, kaddr + offset, cur);
4026 4206
4027 dst += cur;4207 dst += cur;
@@ -4035,24 +4215,29 @@ int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, · 7 unmodified lines
4035 void __user *dstv,4215 void __user *dstv,
4036 unsigned long start, unsigned long len)4216 unsigned long start, unsigned long len)
4037{4217{
4218 const int unit_size = folio_size(eb->folios[0]);
4038 size_t cur;4219 size_t cur;
4039 size_t offset;4220 size_t offset;
4040 struct page *page;
4041 char *kaddr;
4042 char __user *dst = (char __user *)dstv;4221 char __user *dst = (char __user *)dstv;
4043 unsigned long i = get_eb_page_index(start);4222 unsigned long i = get_eb_folio_index(eb, start);
4044 int ret = 0;4223 int ret = 0;
4045 4224
4046 WARN_ON(start > eb->len);4225 WARN_ON(start > eb->len);
4047 WARN_ON(start + len > eb->start + eb->len);4226 WARN_ON(start + len > eb->start + eb->len);
4048 4227
4049 offset = get_eb_offset_in_page(eb, start);4228 if (eb->addr) {
4229 if (copy_to_user_nofault(dstv, eb->addr + start, len))
4230 ret = -EFAULT;
4231 return ret;
4232 }
4233
4234 offset = get_eb_offset_in_folio(eb, start);
4050 4235
4051 while (len > 0) {4236 while (len > 0) {
4052 page = eb->pages[i];4237 char *kaddr;
4053 4238
4054 cur = min(len, (PAGE_SIZE - offset));4239 cur = min(len, unit_size - offset);
4055 kaddr = page_address(page);4240 kaddr = folio_address(eb->folios[i]);
4056 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {4241 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
4057 ret = -EFAULT;4242 ret = -EFAULT;
4058 break;4243 break;
@@ -4070,25 +4255,25 @@ int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, · 11 unmodified lines
4070int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,4255int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
4071 unsigned long start, unsigned long len)4256 unsigned long start, unsigned long len)
4072{4257{
4258 const int unit_size = folio_size(eb->folios[0]);
4073 size_t cur;4259 size_t cur;
4074 size_t offset;4260 size_t offset;
4075 struct page *page;
4076 char *kaddr;4261 char *kaddr;
4077 char *ptr = (char *)ptrv;4262 char *ptr = (char *)ptrv;
4078 unsigned long i = get_eb_page_index(start);4263 unsigned long i = get_eb_folio_index(eb, start);
4079 int ret = 0;4264 int ret = 0;
4080 4265
4081 if (check_eb_range(eb, start, len))4266 if (check_eb_range(eb, start, len))
4082 return -EINVAL;4267 return -EINVAL;
4083 4268
4084 offset = get_eb_offset_in_page(eb, start);4269 if (eb->addr)
4270 return memcmp(ptrv, eb->addr + start, len);
4085 4271
4086 while (len > 0) {4272 offset = get_eb_offset_in_folio(eb, start);
4087 page = eb->pages[i];
4088 4273
4089 cur = min(len, (PAGE_SIZE - offset));4274 while (len > 0) {
4090 4275 cur = min(len, unit_size - offset);
4091 kaddr = page_address(page);4276 kaddr = folio_address(eb->folios[i]);
4092 ret = memcmp(ptr, kaddr + offset, cur);4277 ret = memcmp(ptr, kaddr + offset, cur);
4093 if (ret)4278 if (ret)
4094 break;4279 break;
@@ -4107,10 +4292,12 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, · 12 unmodified lines
4107 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.4292 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
4108 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.4293 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
4109 */4294 */
4110static void assert_eb_page_uptodate(const struct extent_buffer *eb,4295static void assert_eb_folio_uptodate(const struct extent_buffer *eb, int i)
4111 struct page *page)
4112{4296{
4113 struct btrfs_fs_info *fs_info = eb->fs_info;4297 struct btrfs_fs_info *fs_info = eb->fs_info;
4298 struct folio *folio = eb->folios[i];
4299
4300 ASSERT(folio);
4114 4301
4115 /*4302 /*
4116 * If we are using the commit root we could potentially clear a page4303 * If we are using the commit root we could potentially clear a page
@@ -4124,11 +4311,14 @@ static void assert_eb_page_uptodate(const struct extent_buffer *eb, · 7 unmodified lines
4124 return;4311 return;
4125 4312
4126 if (fs_info->nodesize < PAGE_SIZE) {4313 if (fs_info->nodesize < PAGE_SIZE) {
4127 if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, page,4314 struct folio *folio = eb->folios[0];
4315
4316 ASSERT(i == 0);
4317 if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, folio,
4128 eb->start, eb->len)))4318 eb->start, eb->len)))
4129 btrfs_subpage_dump_bitmap(fs_info, page, eb->start, eb->len);4319 btrfs_subpage_dump_bitmap(fs_info, folio, eb->start, eb->len);
4130 } else {4320 } else {
4131 WARN_ON(!PageUptodate(page));4321 WARN_ON(!folio_test_uptodate(folio));
4132 }4322 }
4133}4323}
4134 4324
@@ -4136,29 +4326,34 @@ static void __write_extent_buffer(const struct extent_buffer *eb, · 1 unmodified line
4136 const void *srcv, unsigned long start,4326 const void *srcv, unsigned long start,
4137 unsigned long len, bool use_memmove)4327 unsigned long len, bool use_memmove)
4138{4328{
4329 const int unit_size = folio_size(eb->folios[0]);
4139 size_t cur;4330 size_t cur;
4140 size_t offset;4331 size_t offset;
4141 struct page *page;
4142 char *kaddr;4332 char *kaddr;
4143 char *src = (char *)srcv;4333 char *src = (char *)srcv;
4144 unsigned long i = get_eb_page_index(start);4334 unsigned long i = get_eb_folio_index(eb, start);
4145 /* For unmapped (dummy) ebs, no need to check their uptodate status. */4335 /* For unmapped (dummy) ebs, no need to check their uptodate status. */
4146 const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);4336 const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
4147 4337
4148 WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags));
4149
4150 if (check_eb_range(eb, start, len))4338 if (check_eb_range(eb, start, len))
4151 return;4339 return;
4152 4340
4153 offset = get_eb_offset_in_page(eb, start);4341 if (eb->addr) {
4342 if (use_memmove)
4343 memmove(eb->addr + start, srcv, len);
4344 else
4345 memcpy(eb->addr + start, srcv, len);
4346 return;
4347 }
4348
4349 offset = get_eb_offset_in_folio(eb, start);
4154 4350
4155 while (len > 0) {4351 while (len > 0) {
4156 page = eb->pages[i];
4157 if (check_uptodate)4352 if (check_uptodate)
4158 assert_eb_page_uptodate(eb, page);4353 assert_eb_folio_uptodate(eb, i);
4159 4354
4160 cur = min(len, PAGE_SIZE - offset);4355 cur = min(len, unit_size - offset);
4161 kaddr = page_address(page);4356 kaddr = folio_address(eb->folios[i]);
4162 if (use_memmove)4357 if (use_memmove)
4163 memmove(kaddr + offset, src, cur);4358 memmove(kaddr + offset, src, cur);
4164 else4359 else
@@ -4180,16 +4375,21 @@ void write_extent_buffer(const struct extent_buffer *eb, const void *srcv, · 15 unmodified lines
4180static void memset_extent_buffer(const struct extent_buffer *eb, int c,4375static void memset_extent_buffer(const struct extent_buffer *eb, int c,
4181 unsigned long start, unsigned long len)4376 unsigned long start, unsigned long len)
4182{4377{
4378 const int unit_size = folio_size(eb->folios[0]);
4183 unsigned long cur = start;4379 unsigned long cur = start;
4184 4380
4381 if (eb->addr) {
4382 memset(eb->addr + start, c, len);
4383 return;
4384 }
4385
4185 while (cur < start + len) {4386 while (cur < start + len) {
4186 unsigned long index = get_eb_page_index(cur);4387 unsigned long index = get_eb_folio_index(eb, cur);
4187 unsigned int offset = get_eb_offset_in_page(eb, cur);4388 unsigned int offset = get_eb_offset_in_folio(eb, cur);
4188 unsigned int cur_len = min(start + len - cur, PAGE_SIZE - offset);4389 unsigned int cur_len = min(start + len - cur, unit_size - offset);
4189 struct page *page = eb->pages[index];
4190 4390
4191 assert_eb_page_uptodate(eb, page);4391 assert_eb_folio_uptodate(eb, index);
4192 memset(page_address(page) + offset, c, cur_len);4392 memset(folio_address(eb->folios[index]) + offset, c, cur_len);
4193 4393
4194 cur += cur_len;4394 cur += cur_len;
4195 }4395 }
@@ -4206,15 +4406,16 @@ void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, · 10 unmodified lines
4206void copy_extent_buffer_full(const struct extent_buffer *dst,4406void copy_extent_buffer_full(const struct extent_buffer *dst,
4207 const struct extent_buffer *src)4407 const struct extent_buffer *src)
4208{4408{
4409 const int unit_size = folio_size(src->folios[0]);
4209 unsigned long cur = 0;4410 unsigned long cur = 0;
4210 4411
4211 ASSERT(dst->len == src->len);4412 ASSERT(dst->len == src->len);
4212 4413
4213 while (cur < src->len) {4414 while (cur < src->len) {
4214 unsigned long index = get_eb_page_index(cur);4415 unsigned long index = get_eb_folio_index(src, cur);
4215 unsigned long offset = get_eb_offset_in_page(src, cur);4416 unsigned long offset = get_eb_offset_in_folio(src, cur);
4216 unsigned long cur_len = min(src->len, PAGE_SIZE - offset);4417 unsigned long cur_len = min(src->len, unit_size - offset);
4217 void *addr = page_address(src->pages[index]) + offset;4418 void *addr = folio_address(src->folios[index]) + offset;
4218 4419
4219 write_extent_buffer(dst, addr, cur, cur_len);4420 write_extent_buffer(dst, addr, cur, cur_len);
4220 4421
@@ -4227,12 +4428,12 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 6 unmodified lines
4227 unsigned long dst_offset, unsigned long src_offset,4428 unsigned long dst_offset, unsigned long src_offset,
4228 unsigned long len)4429 unsigned long len)
4229{4430{
4431 const int unit_size = folio_size(dst->folios[0]);
4230 u64 dst_len = dst->len;4432 u64 dst_len = dst->len;
4231 size_t cur;4433 size_t cur;
4232 size_t offset;4434 size_t offset;
4233 struct page *page;
4234 char *kaddr;4435 char *kaddr;
4235 unsigned long i = get_eb_page_index(dst_offset);4436 unsigned long i = get_eb_folio_index(dst, dst_offset);
4236 4437
4237 if (check_eb_range(dst, dst_offset, len) ||4438 if (check_eb_range(dst, dst_offset, len) ||
4238 check_eb_range(src, src_offset, len))4439 check_eb_range(src, src_offset, len))
@@ -4240,15 +4441,14 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 1 unmodified line
4240 4441
4241 WARN_ON(src->len != dst_len);4442 WARN_ON(src->len != dst_len);
4242 4443
4243 offset = get_eb_offset_in_page(dst, dst_offset);4444 offset = get_eb_offset_in_folio(dst, dst_offset);
4244 4445
4245 while (len > 0) {4446 while (len > 0) {
4246 page = dst->pages[i];4447 assert_eb_folio_uptodate(dst, i);
4247 assert_eb_page_uptodate(dst, page);
4248 4448
4249 cur = min(len, (unsigned long)(PAGE_SIZE - offset));4449 cur = min(len, (unsigned long)(unit_size - offset));
4250 4450
4251 kaddr = page_address(page);4451 kaddr = folio_address(dst->folios[i]);
4252 read_extent_buffer(src, kaddr + offset, src_offset, cur);4452 read_extent_buffer(src, kaddr + offset, src_offset, cur);
4253 4453
4254 src_offset += cur;4454 src_offset += cur;
@@ -4259,22 +4459,22 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 4 unmodified lines
4259}4459}
4260 4460
4261/*4461/*
4262 * Calculate the page and offset of the byte containing the given bit number.4462 * Calculate the folio and offset of the byte containing the given bit number.
4263 *4463 *
4264 * @eb: the extent buffer4464 * @eb: the extent buffer
4265 * @start: offset of the bitmap item in the extent buffer4465 * @start: offset of the bitmap item in the extent buffer
4266 * @nr: bit number4466 * @nr: bit number
4267 * @page_index: return index of the page in the extent buffer that contains4467 * @folio_index: return index of the folio in the extent buffer that contains
4268 * the given bit number4468 * the given bit number
4269 * @page_offset: return offset into the page given by page_index4469 * @folio_offset: return offset into the folio given by folio_index
4270 *4470 *
4271 * This helper hides the ugliness of finding the byte in an extent buffer which4471 * This helper hides the ugliness of finding the byte in an extent buffer which
4272 * contains a given bit.4472 * contains a given bit.
4273 */4473 */
4274static inline void eb_bitmap_offset(const struct extent_buffer *eb,4474static inline void eb_bitmap_offset(const struct extent_buffer *eb,
4275 unsigned long start, unsigned long nr,4475 unsigned long start, unsigned long nr,
4276 unsigned long *page_index,4476 unsigned long *folio_index,
4277 size_t *page_offset)4477 size_t *folio_offset)
4278{4478{
4279 size_t byte_offset = BIT_BYTE(nr);4479 size_t byte_offset = BIT_BYTE(nr);
4280 size_t offset;4480 size_t offset;
@@ -4284,10 +4484,10 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb, · 3 unmodified lines
4284 * the bitmap item in the extent buffer + the offset of the byte in the4484 * the bitmap item in the extent buffer + the offset of the byte in the
4285 * bitmap item.4485 * bitmap item.
4286 */4486 */
4287 offset = start + offset_in_page(eb->start) + byte_offset;4487 offset = start + offset_in_folio(eb->folios[0], eb->start) + byte_offset;
4288 4488
4289 *page_index = offset >> PAGE_SHIFT;4489 *folio_index = offset >> folio_shift(eb->folios[0]);
4290 *page_offset = offset_in_page(offset);4490 *folio_offset = offset_in_folio(eb->folios[0], offset);
4291}4491}
4292 4492
4293/*4493/*
@@ -4300,25 +4500,23 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb, · 6 unmodified lines
4300int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,4500int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
4301 unsigned long nr)4501 unsigned long nr)
4302{4502{
4303 u8 *kaddr;
4304 struct page *page;
4305 unsigned long i;4503 unsigned long i;
4306 size_t offset;4504 size_t offset;
4505 u8 *kaddr;
4307 4506
4308 eb_bitmap_offset(eb, start, nr, &i, &offset);4507 eb_bitmap_offset(eb, start, nr, &i, &offset);
4309 page = eb->pages[i];4508 assert_eb_folio_uptodate(eb, i);
4310 assert_eb_page_uptodate(eb, page);4509 kaddr = folio_address(eb->folios[i]);
4311 kaddr = page_address(page);
4312 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));4510 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
4313}4511}
4314 4512
4315static u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr)4513static u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr)
4316{4514{
4317 unsigned long index = get_eb_page_index(bytenr);4515 unsigned long index = get_eb_folio_index(eb, bytenr);
4318 4516
4319 if (check_eb_range(eb, bytenr, 1))4517 if (check_eb_range(eb, bytenr, 1))
4320 return NULL;4518 return NULL;
4321 return page_address(eb->pages[index]) + get_eb_offset_in_page(eb, bytenr);4519 return folio_address(eb->folios[index]) + get_eb_offset_in_folio(eb, bytenr);
4322}4520}
4323 4521
4324/*4522/*
@@ -4403,19 +4601,30 @@ void memcpy_extent_buffer(const struct extent_buffer *dst, · 78 unmodified lines
4403 unsigned long dst_offset, unsigned long src_offset,4601 unsigned long dst_offset, unsigned long src_offset,
4404 unsigned long len)4602 unsigned long len)
4405{4603{
4604 const int unit_size = folio_size(dst->folios[0]);
4406 unsigned long cur_off = 0;4605 unsigned long cur_off = 0;
4407 4606
4408 if (check_eb_range(dst, dst_offset, len) ||4607 if (check_eb_range(dst, dst_offset, len) ||
4409 check_eb_range(dst, src_offset, len))4608 check_eb_range(dst, src_offset, len))
4410 return;4609 return;
4411 4610
4611 if (dst->addr) {
4612 const bool use_memmove = areas_overlap(src_offset, dst_offset, len);
4613
4614 if (use_memmove)
4615 memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
4616 else
4617 memcpy(dst->addr + dst_offset, dst->addr + src_offset, len);
4618 return;
4619 }
4620
4412 while (cur_off < len) {4621 while (cur_off < len) {
4413 unsigned long cur_src = cur_off + src_offset;4622 unsigned long cur_src = cur_off + src_offset;
4414 unsigned long pg_index = get_eb_page_index(cur_src);4623 unsigned long folio_index = get_eb_folio_index(dst, cur_src);
4415 unsigned long pg_off = get_eb_offset_in_page(dst, cur_src);4624 unsigned long folio_off = get_eb_offset_in_folio(dst, cur_src);
4416 unsigned long cur_len = min(src_offset + len - cur_src,4625 unsigned long cur_len = min(src_offset + len - cur_src,
4417 PAGE_SIZE - pg_off);4626 unit_size - folio_off);
4418 void *src_addr = page_address(dst->pages[pg_index]) + pg_off;4627 void *src_addr = folio_address(dst->folios[folio_index]) + folio_off;
4419 const bool use_memmove = areas_overlap(src_offset + cur_off,4628 const bool use_memmove = areas_overlap(src_offset + cur_off,
4420 dst_offset + cur_off, cur_len);4629 dst_offset + cur_off, cur_len);
4421 4630
@@ -4441,24 +4650,29 @@ void memmove_extent_buffer(const struct extent_buffer *dst, · 19 unmodified lines
4441 return;4650 return;
4442 }4651 }
4443 4652
4653 if (dst->addr) {
4654 memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
4655 return;
4656 }
4657
4444 while (len > 0) {4658 while (len > 0) {
4445 unsigned long src_i;4659 unsigned long src_i;
4446 size_t cur;4660 size_t cur;
4447 size_t dst_off_in_page;4661 size_t dst_off_in_folio;
4448 size_t src_off_in_page;4662 size_t src_off_in_folio;
4449 void *src_addr;4663 void *src_addr;
4450 bool use_memmove;4664 bool use_memmove;
4451 4665
4452 src_i = get_eb_page_index(src_end);4666 src_i = get_eb_folio_index(dst, src_end);
4453 4667
4454 dst_off_in_page = get_eb_offset_in_page(dst, dst_end);4668 dst_off_in_folio = get_eb_offset_in_folio(dst, dst_end);
4455 src_off_in_page = get_eb_offset_in_page(dst, src_end);4669 src_off_in_folio = get_eb_offset_in_folio(dst, src_end);
4456 4670
4457 cur = min_t(unsigned long, len, src_off_in_page + 1);4671 cur = min_t(unsigned long, len, src_off_in_folio + 1);
4458 cur = min(cur, dst_off_in_page + 1);4672 cur = min(cur, dst_off_in_folio + 1);
4459 4673
4460 src_addr = page_address(dst->pages[src_i]) + src_off_in_page -4674 src_addr = folio_address(dst->folios[src_i]) + src_off_in_folio -
4461 cur + 1;4675 cur + 1;
4462 use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1,4676 use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1,
4463 cur);4677 cur);
4464 4678
@@ -4520,7 +4734,7 @@ static int try_release_subpage_extent_buffer(struct page *page) · 55 unmodified lines
4520 struct extent_buffer *eb = NULL;4734 struct extent_buffer *eb = NULL;
4521 4735
4522 /*4736 /*
4523 * Unlike try_release_extent_buffer() which uses page->private4737 * Unlike try_release_extent_buffer() which uses folio private
4524 * to grab buffer, for subpage case we rely on radix tree, thus4738 * to grab buffer, for subpage case we rely on radix tree, thus
4525 * we need to ensure radix tree consistency.4739 * we need to ensure radix tree consistency.
4526 *4740 *
@@ -4560,17 +4774,17 @@ static int try_release_subpage_extent_buffer(struct page *page) · 33 unmodified lines
4560 4774
4561 /*4775 /*
4562 * Here we don't care about the return value, we will always4776 * Here we don't care about the return value, we will always
4563 * check the page private at the end. And4777 * check the folio private at the end. And
4564 * release_extent_buffer() will release the refs_lock.4778 * release_extent_buffer() will release the refs_lock.
4565 */4779 */
4566 release_extent_buffer(eb);4780 release_extent_buffer(eb);
4567 }4781 }
4568 /*4782 /*
4569 * Finally to check if we have cleared page private, as if we have4783 * Finally to check if we have cleared folio private, as if we have
4570 * released all ebs in the page, the page private should be cleared now.4784 * released all ebs in the page, the folio private should be cleared now.
4571 */4785 */
4572 spin_lock(&page->mapping->i_private_lock);4786 spin_lock(&page->mapping->i_private_lock);
4573 if (!PagePrivate(page))4787 if (!folio_test_private(page_folio(page)))
4574 ret = 1;4788 ret = 1;
4575 else4789 else
4576 ret = 0;4790 ret = 0;
@@ -4581,22 +4795,23 @@ static int try_release_subpage_extent_buffer(struct page *page) · 4 unmodified lines
4581 4795
4582int try_release_extent_buffer(struct page *page)4796int try_release_extent_buffer(struct page *page)
4583{4797{
4798 struct folio *folio = page_folio(page);
4584 struct extent_buffer *eb;4799 struct extent_buffer *eb;
4585 4800
4586 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)4801 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
4587 return try_release_subpage_extent_buffer(page);4802 return try_release_subpage_extent_buffer(page);
4588 4803
4589 /*4804 /*
4590 * We need to make sure nobody is changing page->private, as we rely on4805 * We need to make sure nobody is changing folio private, as we rely on
4591 * page->private as the pointer to extent buffer.4806 * folio private as the pointer to extent buffer.
4592 */4807 */
4593 spin_lock(&page->mapping->i_private_lock);4808 spin_lock(&page->mapping->i_private_lock);
4594 if (!PagePrivate(page)) {4809 if (!folio_test_private(folio)) {
4595 spin_unlock(&page->mapping->i_private_lock);4810 spin_unlock(&page->mapping->i_private_lock);
4596 return 1;4811 return 1;
4597 }4812 }
4598 4813
4599 eb = (struct extent_buffer *)page->private;4814 eb = folio_get_private(folio);
4600 BUG_ON(!eb);4815 BUG_ON(!eb);
4601 4816
4602 /*4817 /*
fs/btrfs/extent_io.h+61−19
@@ -28,7 +28,8 @@ enum { · 27 unmodified lines
28 EXTENT_BUFFER_IN_TREE,28 EXTENT_BUFFER_IN_TREE,
29 /* write IO error */29 /* write IO error */
30 EXTENT_BUFFER_WRITE_ERR,30 EXTENT_BUFFER_WRITE_ERR,
31 EXTENT_BUFFER_NO_CHECK,31 /* Indicate the extent buffer is written zeroed out (for zoned) */
32 EXTENT_BUFFER_ZONED_ZEROOUT,
32 /* Indicate that extent buffer pages a being read */33 /* Indicate that extent buffer pages a being read */
33 EXTENT_BUFFER_READING,34 EXTENT_BUFFER_READING,
34};35};
@@ -43,10 +44,10 @@ enum { · 8 unmodified lines
43};44};
44 45
45/*46/*
46 * page->private values. Every page that is controlled by the extent47 * Folio private values. Every page that is controlled by the extent map has
47 * map has page->private set to one.48 * folio private set to this value.
48 */49 */
49#define EXTENT_PAGE_PRIVATE 150#define EXTENT_FOLIO_PRIVATE 1
50 51
51/*52/*
52 * The extent buffer bitmap operations are done with byte granularity instead of53 * The extent buffer bitmap operations are done with byte granularity instead of
@@ -77,6 +78,13 @@ struct extent_buffer { · 24 unmodified lines
77 unsigned long len;78 unsigned long len;
78 unsigned long bflags;79 unsigned long bflags;
79 struct btrfs_fs_info *fs_info;80 struct btrfs_fs_info *fs_info;
81
82 /*
83 * The address where the eb can be accessed without any cross-page handling.
84 * This can be NULL if not possible.
85 */
86 void *addr;
87
80 spinlock_t refs_lock;88 spinlock_t refs_lock;
81 atomic_t refs;89 atomic_t refs;
82 int read_mirror;90 int read_mirror;
@@ -86,7 +94,12 @@ struct extent_buffer { · 3 unmodified lines
86 94
87 struct rw_semaphore lock;95 struct rw_semaphore lock;
88 96
89 struct page *pages[INLINE_EXTENT_BUFFER_PAGES];97 /*
98 * Pointers to all the folios of the extent buffer.
99 *
100 * For now the folio is always order 0 (aka, a single page).
101 */
102 struct folio *folios[INLINE_EXTENT_BUFFER_PAGES];
90#ifdef CONFIG_BTRFS_DEBUG103#ifdef CONFIG_BTRFS_DEBUG
91 struct list_head leak_list;104 struct list_head leak_list;
92 pid_t lock_owner;105 pid_t lock_owner;
@@ -108,29 +121,43 @@ struct btrfs_eb_write_context { · 15 unmodified lines
108 *121 *
109 * Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.122 * Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.
110 */123 */
111static inline size_t get_eb_offset_in_page(const struct extent_buffer *eb,124static inline size_t get_eb_offset_in_folio(const struct extent_buffer *eb,
112 unsigned long offset)125 unsigned long offset)
113{126{
114 /*127 /*
115 * For sectorsize == PAGE_SIZE case, eb->start will always be aligned128 * 1) sectorsize == PAGE_SIZE and nodesize >= PAGE_SIZE case
116 * to PAGE_SIZE, thus adding it won't cause any difference.129 * 1.1) One large folio covering the whole eb
130 * The eb->start is aligned to folio size, thus adding it
131 * won't cause any difference.
132 * 1.2) Several page sized folios
133 * The eb->start is aligned to folio (page) size, thus
134 * adding it won't cause any difference.
117 *135 *
118 * For sectorsize < PAGE_SIZE, we must only read the data that belongs136 * 2) sectorsize < PAGE_SIZE and nodesize < PAGE_SIZE case
119 * to the eb, thus we have to take the eb->start into consideration.137 * In this case there would only be one page sized folio, and there
138 * may be several different extent buffers in the page/folio.
139 * We need to add eb->start to properly access the offset inside
140 * that eb.
120 */141 */
121 return offset_in_page(offset + eb->start);142 return offset_in_folio(eb->folios[0], offset + eb->start);
122}143}
123 144
124static inline unsigned long get_eb_page_index(unsigned long offset)145static inline unsigned long get_eb_folio_index(const struct extent_buffer *eb,
146 unsigned long offset)
125{147{
126 /*148 /*
127 * For sectorsize == PAGE_SIZE case, plain >> PAGE_SHIFT is enough.149 * 1) sectorsize == PAGE_SIZE and nodesize >= PAGE_SIZE case
150 * 1.1) One large folio covering the whole eb.
151 * the folio_shift would be large enough to always make us
152 * return 0 as index.
153 * 1.2) Several page sized folios
154 * The folio_shift() would be PAGE_SHIFT, giving us the correct
155 * index.
128 *156 *
129 * For sectorsize < PAGE_SIZE case, we only support 64K PAGE_SIZE,157 * 2) sectorsize < PAGE_SIZE and nodesize < PAGE_SIZE case
130 * and have ensured that all tree blocks are contained in one page,158 * The folio would only be page sized, and always give us 0 as index.
131 * thus we always get index == 0.
132 */159 */
133 return offset >> PAGE_SHIFT;160 return offset >> folio_shift(eb->folios[0]);
134}161}
135 162
136/*163/*
@@ -230,6 +257,20 @@ static inline int num_extent_pages(const struct extent_buffer *eb) · 93 unmodified lines
230 return (eb->len >> PAGE_SHIFT) ?: 1;257 return (eb->len >> PAGE_SHIFT) ?: 1;
231}258}
232 259
260/*
261 * This can only be determined at runtime by checking eb::folios[0].
262 *
263 * As we can have either one large folio covering the whole eb
264 * (either nodesize <= PAGE_SIZE, or high order folio), or multiple
265 * single-paged folios.
266 */
267static inline int num_extent_folios(const struct extent_buffer *eb)
268{
269 if (folio_order(eb->folios[0]))
270 return 1;
271 return num_extent_pages(eb);
272}
273
233static inline int extent_buffer_uptodate(const struct extent_buffer *eb)274static inline int extent_buffer_uptodate(const struct extent_buffer *eb)
234{275{
235 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);276 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
@@ -294,7 +335,8 @@ int extent_invalidate_folio(struct extent_io_tree *tree, · 58 unmodified lines
294void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,335void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
295 struct extent_buffer *buf);336 struct extent_buffer *buf);
296 337
297int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array);338int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
339 gfp_t extra_gfp);
298 340
299#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS341#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
300bool find_lock_delalloc_range(struct inode *inode,342bool find_lock_delalloc_range(struct inode *inode,
fs/btrfs/extent_map.c+80−115
@@ -50,7 +50,6 @@ struct extent_map *alloc_extent_map(void) · 49 unmodified lines
50 if (!em)50 if (!em)
51 return NULL;51 return NULL;
52 RB_CLEAR_NODE(&em->rb_node);52 RB_CLEAR_NODE(&em->rb_node);
53 em->compress_type = BTRFS_COMPRESS_NONE;
54 refcount_set(&em->refs, 1);53 refcount_set(&em->refs, 1);
55 INIT_LIST_HEAD(&em->list);54 INIT_LIST_HEAD(&em->list);
56 return em;55 return em;
@@ -67,8 +66,6 @@ void free_extent_map(struct extent_map *em) · 10 unmodified lines
67 if (refcount_dec_and_test(&em->refs)) {66 if (refcount_dec_and_test(&em->refs)) {
68 WARN_ON(extent_map_in_tree(em));67 WARN_ON(extent_map_in_tree(em));
69 WARN_ON(!list_empty(&em->list));68 WARN_ON(!list_empty(&em->list));
70 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
71 kfree(em->map_lookup);
72 kmem_cache_free(extent_map_cache, em);69 kmem_cache_free(extent_map_cache, em);
73 }70 }
74}71}
@@ -182,50 +179,50 @@ static struct rb_node *__tree_search(struct rb_root *root, u64 offset, · 107 unmodified lines
182 return NULL;179 return NULL;
183}180}
184 181
185/* Check to see if two extent_map structs are adjacent and safe to merge. */182static inline u64 extent_map_block_end(const struct extent_map *em)
186static int mergable_maps(struct extent_map *prev, struct extent_map *next)
187{183{
188 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))184 if (em->block_start + em->block_len < em->block_start)
189 return 0;185 return (u64)-1;
186 return em->block_start + em->block_len;
187}
190 188
191 /*189static bool can_merge_extent_map(const struct extent_map *em)
192 * don't merge compressed extents, we need to know their190{
193 * actual size191 if (em->flags & EXTENT_FLAG_PINNED)
194 */192 return false;
195 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))193
196 return 0;194 /* Don't merge compressed extents, we need to know their actual size. */
195 if (extent_map_is_compressed(em))
196 return false;
197 197
198 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||198 if (em->flags & EXTENT_FLAG_LOGGING)
199 test_bit(EXTENT_FLAG_LOGGING, &next->flags))199 return false;
200 return 0;
201 200
202 /*201 /*
203 * We don't want to merge stuff that hasn't been written to the log yet202 * We don't want to merge stuff that hasn't been written to the log yet
204 * since it may not reflect exactly what is on disk, and that would be203 * since it may not reflect exactly what is on disk, and that would be
205 * bad.204 * bad.
206 */205 */
207 if (!list_empty(&prev->list) || !list_empty(&next->list))206 if (!list_empty(&em->list))
208 return 0;207 return false;
209 208
210 ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&209 return true;
211 prev->block_start != EXTENT_MAP_DELALLOC);210}
212 211
213 if (prev->map_lookup || next->map_lookup)212/* Check to see if two extent_map structs are adjacent and safe to merge. */
214 ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&213static bool mergeable_maps(const struct extent_map *prev, const struct extent_map *next)
215 test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));214{
216 215 if (extent_map_end(prev) != next->start)
217 if (extent_map_end(prev) == next->start &&216 return false;
218 prev->flags == next->flags &&217
219 prev->map_lookup == next->map_lookup &&218 if (prev->flags != next->flags)
220 ((next->block_start == EXTENT_MAP_HOLE &&219 return false;
221 prev->block_start == EXTENT_MAP_HOLE) ||220
222 (next->block_start == EXTENT_MAP_INLINE &&221 if (next->block_start < EXTENT_MAP_LAST_BYTE - 1)
223 prev->block_start == EXTENT_MAP_INLINE) ||222 return next->block_start == extent_map_block_end(prev);
224 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&223
225 next->block_start == extent_map_block_end(prev)))) {224 /* HOLES and INLINE extents. */
226 return 1;225 return next->block_start == prev->block_start;
227 }
228 return 0;
229}226}
230 227
231static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)228static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
@@ -244,11 +241,14 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) · 12 unmodified lines
244 if (refcount_read(&em->refs) > 2)241 if (refcount_read(&em->refs) > 2)
245 return;242 return;
246 243
244 if (!can_merge_extent_map(em))
245 return;
246
247 if (em->start != 0) {247 if (em->start != 0) {
248 rb = rb_prev(&em->rb_node);248 rb = rb_prev(&em->rb_node);
249 if (rb)249 if (rb)
250 merge = rb_entry(rb, struct extent_map, rb_node);250 merge = rb_entry(rb, struct extent_map, rb_node);
251 if (rb && mergable_maps(merge, em)) {251 if (rb && can_merge_extent_map(merge) && mergeable_maps(merge, em)) {
252 em->start = merge->start;252 em->start = merge->start;
253 em->orig_start = merge->orig_start;253 em->orig_start = merge->orig_start;
254 em->len += merge->len;254 em->len += merge->len;
@@ -257,7 +257,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) · 2 unmodified lines
257 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;257 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
258 em->mod_start = merge->mod_start;258 em->mod_start = merge->mod_start;
259 em->generation = max(em->generation, merge->generation);259 em->generation = max(em->generation, merge->generation);
260 set_bit(EXTENT_FLAG_MERGED, &em->flags);260 em->flags |= EXTENT_FLAG_MERGED;
261 261
262 rb_erase_cached(&merge->rb_node, &tree->map);262 rb_erase_cached(&merge->rb_node, &tree->map);
263 RB_CLEAR_NODE(&merge->rb_node);263 RB_CLEAR_NODE(&merge->rb_node);
@@ -268,14 +268,14 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) · 4 unmodified lines
268 rb = rb_next(&em->rb_node);268 rb = rb_next(&em->rb_node);
269 if (rb)269 if (rb)
270 merge = rb_entry(rb, struct extent_map, rb_node);270 merge = rb_entry(rb, struct extent_map, rb_node);
271 if (rb && mergable_maps(em, merge)) {271 if (rb && can_merge_extent_map(merge) && mergeable_maps(em, merge)) {
272 em->len += merge->len;272 em->len += merge->len;
273 em->block_len += merge->block_len;273 em->block_len += merge->block_len;
274 rb_erase_cached(&merge->rb_node, &tree->map);274 rb_erase_cached(&merge->rb_node, &tree->map);
275 RB_CLEAR_NODE(&merge->rb_node);275 RB_CLEAR_NODE(&merge->rb_node);
276 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;276 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
277 em->generation = max(em->generation, merge->generation);277 em->generation = max(em->generation, merge->generation);
278 set_bit(EXTENT_FLAG_MERGED, &em->flags);278 em->flags |= EXTENT_FLAG_MERGED;
279 free_extent_map(merge);279 free_extent_map(merge);
280 }280 }
281}281}
@@ -283,7 +283,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) · 1 unmodified line
283/*283/*
284 * Unpin an extent from the cache.284 * Unpin an extent from the cache.
285 *285 *
286 * @tree: tree to unpin the extent in286 * @inode: the inode from which we are unpinning an extent range
287 * @start: logical offset in the file287 * @start: logical offset in the file
288 * @len: length of the extent288 * @len: length of the extent
289 * @gen: generation that this extent has been modified in289 * @gen: generation that this extent has been modified in
@@ -292,9 +292,10 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) · 2 unmodified lines
292 * to the generation that actually added the file item to the inode so we know292 * to the generation that actually added the file item to the inode so we know
293 * we need to sync this extent when we call fsync().293 * we need to sync this extent when we call fsync().
294 */294 */
295int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,295int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
296 u64 gen)
297{296{
297 struct btrfs_fs_info *fs_info = inode->root->fs_info;
298 struct extent_map_tree *tree = &inode->extent_tree;
298 int ret = 0;299 int ret = 0;
299 struct extent_map *em;300 struct extent_map *em;
300 bool prealloc = false;301 bool prealloc = false;
@@ -302,19 +303,28 @@ int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, · 1 unmodified line
302 write_lock(&tree->lock);303 write_lock(&tree->lock);
303 em = lookup_extent_mapping(tree, start, len);304 em = lookup_extent_mapping(tree, start, len);
304 305
305 WARN_ON(!em || em->start != start);306 if (WARN_ON(!em)) {
306 307 btrfs_warn(fs_info,
307 if (!em)308"no extent map found for inode %llu (root %lld) when unpinning extent range [%llu, %llu), generation %llu",
309 btrfs_ino(inode), btrfs_root_id(inode->root),
310 start, len, gen);
308 goto out;311 goto out;
312 }
313
314 if (WARN_ON(em->start != start))
315 btrfs_warn(fs_info,
316"found extent map for inode %llu (root %lld) with unexpected start offset %llu when unpinning extent range [%llu, %llu), generation %llu",
317 btrfs_ino(inode), btrfs_root_id(inode->root),
318 em->start, start, len, gen);
309 319
310 em->generation = gen;320 em->generation = gen;
311 clear_bit(EXTENT_FLAG_PINNED, &em->flags);321 em->flags &= ~EXTENT_FLAG_PINNED;
312 em->mod_start = em->start;322 em->mod_start = em->start;
313 em->mod_len = em->len;323 em->mod_len = em->len;
314 324
315 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {325 if (em->flags & EXTENT_FLAG_FILLING) {
316 prealloc = true;326 prealloc = true;
317 clear_bit(EXTENT_FLAG_FILLING, &em->flags);327 em->flags &= ~EXTENT_FLAG_FILLING;
318 }328 }
319 329
320 try_merge_map(tree, em);330 try_merge_map(tree, em);
@@ -335,7 +345,7 @@ void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) · 14 unmodified lines
335{345{
336 lockdep_assert_held_write(&tree->lock);346 lockdep_assert_held_write(&tree->lock);
337 347
338 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);348 em->flags &= ~EXTENT_FLAG_LOGGING;
339 if (extent_map_in_tree(em))349 if (extent_map_in_tree(em))
340 try_merge_map(tree, em);350 try_merge_map(tree, em);
341}351}
@@ -348,45 +358,14 @@ static inline void setup_extent_mapping(struct extent_map_tree *tree, · 6 unmodified lines
348 em->mod_start = em->start;358 em->mod_start = em->start;
349 em->mod_len = em->len;359 em->mod_len = em->len;
350 360
361 ASSERT(list_empty(&em->list));
362
351 if (modified)363 if (modified)
352 list_move(&em->list, &tree->modified_extents);364 list_add(&em->list, &tree->modified_extents);
353 else365 else
354 try_merge_map(tree, em);366 try_merge_map(tree, em);
355}367}
356 368
357static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
358{
359 struct map_lookup *map = em->map_lookup;
360 u64 stripe_size = em->orig_block_len;
361 int i;
362
363 for (i = 0; i < map->num_stripes; i++) {
364 struct btrfs_io_stripe *stripe = &map->stripes[i];
365 struct btrfs_device *device = stripe->dev;
366
367 set_extent_bit(&device->alloc_state, stripe->physical,
368 stripe->physical + stripe_size - 1,
369 bits | EXTENT_NOWAIT, NULL);
370 }
371}
372
373static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
374{
375 struct map_lookup *map = em->map_lookup;
376 u64 stripe_size = em->orig_block_len;
377 int i;
378
379 for (i = 0; i < map->num_stripes; i++) {
380 struct btrfs_io_stripe *stripe = &map->stripes[i];
381 struct btrfs_device *device = stripe->dev;
382
383 __clear_extent_bit(&device->alloc_state, stripe->physical,
384 stripe->physical + stripe_size - 1,
385 bits | EXTENT_NOWAIT,
386 NULL, NULL);
387 }
388}
389
390/*369/*
391 * Add new extent map to the extent tree370 * Add new extent map to the extent tree
392 *371 *
@@ -400,8 +379,8 @@ static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits) · 7 unmodified lines
400 * into the tree directly, with an additional reference taken, or a379 * into the tree directly, with an additional reference taken, or a
401 * reference dropped if the merge attempt was successful.380 * reference dropped if the merge attempt was successful.
402 */381 */
403int add_extent_mapping(struct extent_map_tree *tree,382static int add_extent_mapping(struct extent_map_tree *tree,
404 struct extent_map *em, int modified)383 struct extent_map *em, int modified)
405{384{
406 int ret = 0;385 int ret = 0;
407 386
@@ -412,10 +391,6 @@ int add_extent_mapping(struct extent_map_tree *tree, · 4 unmodified lines
412 goto out;391 goto out;
413 392
414 setup_extent_mapping(tree, em, modified);393 setup_extent_mapping(tree, em, modified);
415 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
416 extent_map_device_set_bits(em, CHUNK_ALLOCATED);
417 extent_map_device_clear_bits(em, CHUNK_TRIMMED);
418 }
419out:394out:
420 return ret;395 return ret;
421}396}
@@ -495,12 +470,10 @@ void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) · 73 unmodified lines
495{470{
496 lockdep_assert_held_write(&tree->lock);471 lockdep_assert_held_write(&tree->lock);
497 472
498 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));473 WARN_ON(em->flags & EXTENT_FLAG_PINNED);
499 rb_erase_cached(&em->rb_node, &tree->map);474 rb_erase_cached(&em->rb_node, &tree->map);
500 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))475 if (!(em->flags & EXTENT_FLAG_LOGGING))
501 list_del_init(&em->list);476 list_del_init(&em->list);
502 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
503 extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
504 RB_CLEAR_NODE(&em->rb_node);477 RB_CLEAR_NODE(&em->rb_node);
505}478}
506 479
@@ -511,9 +484,9 @@ static void replace_extent_mapping(struct extent_map_tree *tree, · 4 unmodified lines
511{484{
512 lockdep_assert_held_write(&tree->lock);485 lockdep_assert_held_write(&tree->lock);
513 486
514 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));487 WARN_ON(cur->flags & EXTENT_FLAG_PINNED);
515 ASSERT(extent_map_in_tree(cur));488 ASSERT(extent_map_in_tree(cur));
516 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))489 if (!(cur->flags & EXTENT_FLAG_LOGGING))
517 list_del_init(&cur->list);490 list_del_init(&cur->list);
518 rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);491 rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
519 RB_CLEAR_NODE(&cur->rb_node);492 RB_CLEAR_NODE(&cur->rb_node);
@@ -576,7 +549,7 @@ static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, · 56 unmodified lines
576 em->start = start;549 em->start = start;
577 em->len = end - start;550 em->len = end - start;
578 if (em->block_start < EXTENT_MAP_LAST_BYTE &&551 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
579 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {552 !extent_map_is_compressed(em)) {
580 em->block_start += start_diff;553 em->block_start += start_diff;
581 em->block_len = em->len;554 em->block_len = em->len;
582 }555 }
@@ -626,8 +599,6 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, · 43 unmodified lines
626 if (ret == -EEXIST) {599 if (ret == -EEXIST) {
627 struct extent_map *existing;600 struct extent_map *existing;
628 601
629 ret = 0;
630
631 existing = search_extent_mapping(em_tree, start, len);602 existing = search_extent_mapping(em_tree, start, len);
632 603
633 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);604 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
@@ -681,8 +652,7 @@ static void drop_all_extent_maps_fast(struct extent_map_tree *tree) · 47 unmodified lines
681 652
682 node = rb_first_cached(&tree->map);653 node = rb_first_cached(&tree->map);
683 em = rb_entry(node, struct extent_map, rb_node);654 em = rb_entry(node, struct extent_map, rb_node);
684 clear_bit(EXTENT_FLAG_PINNED, &em->flags);655 em->flags &= ~(EXTENT_FLAG_PINNED | EXTENT_FLAG_LOGGING);
685 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
686 remove_extent_mapping(tree, em);656 remove_extent_mapping(tree, em);
687 free_extent_map(em);657 free_extent_map(em);
688 cond_resched_rwlock_write(&tree->lock);658 cond_resched_rwlock_write(&tree->lock);
@@ -758,19 +728,18 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 69 unmodified lines
758 }728 }
759 }729 }
760 730
761 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {731 if (skip_pinned && (em->flags & EXTENT_FLAG_PINNED)) {
762 start = em_end;732 start = em_end;
763 goto next;733 goto next;
764 }734 }
765 735
766 flags = em->flags;736 flags = em->flags;
767 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
768 /*737 /*
769 * In case we split the extent map, we want to preserve the738 * In case we split the extent map, we want to preserve the
770 * EXTENT_FLAG_LOGGING flag on our extent map, but we don't want739 * EXTENT_FLAG_LOGGING flag on our extent map, but we don't want
771 * it on the new extent maps.740 * it on the new extent maps.
772 */741 */
773 clear_bit(EXTENT_FLAG_LOGGING, &flags);742 em->flags &= ~(EXTENT_FLAG_PINNED | EXTENT_FLAG_LOGGING);
774 modified = !list_empty(&em->list);743 modified = !list_empty(&em->list);
775 744
776 /*745 /*
@@ -781,7 +750,7 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 4 unmodified lines
781 goto remove_em;750 goto remove_em;
782 751
783 gen = em->generation;752 gen = em->generation;
784 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);753 compressed = extent_map_is_compressed(em);
785 754
786 if (em->start < start) {755 if (em->start < start) {
787 if (!split) {756 if (!split) {
@@ -814,7 +783,6 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 26 unmodified lines
814 783
815 split->generation = gen;784 split->generation = gen;
816 split->flags = flags;785 split->flags = flags;
817 split->compress_type = em->compress_type;
818 replace_extent_mapping(em_tree, em, split, modified);786 replace_extent_mapping(em_tree, em, split, modified);
819 free_extent_map(split);787 free_extent_map(split);
820 split = split2;788 split = split2;
@@ -831,7 +799,6 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 10 unmodified lines
831 split->len = em_end - end;799 split->len = em_end - end;
832 split->block_start = em->block_start;800 split->block_start = em->block_start;
833 split->flags = flags;801 split->flags = flags;
834 split->compress_type = em->compress_type;
835 split->generation = gen;802 split->generation = gen;
836 803
837 if (em->block_start < EXTENT_MAP_LAST_BYTE) {804 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
@@ -997,14 +964,14 @@ int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre, · 159 unmodified lines
997 }964 }
998 965
999 ASSERT(em->len == len);966 ASSERT(em->len == len);
1000 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));967 ASSERT(!extent_map_is_compressed(em));
1001 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);968 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
1002 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));969 ASSERT(em->flags & EXTENT_FLAG_PINNED);
1003 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));970 ASSERT(!(em->flags & EXTENT_FLAG_LOGGING));
1004 ASSERT(!list_empty(&em->list));971 ASSERT(!list_empty(&em->list));
1005 972
1006 flags = em->flags;973 flags = em->flags;
1007 clear_bit(EXTENT_FLAG_PINNED, &em->flags);974 em->flags &= ~EXTENT_FLAG_PINNED;
1008 975
1009 /* First, replace the em with a new extent_map starting from * em->start */976 /* First, replace the em with a new extent_map starting from * em->start */
1010 split_pre->start = em->start;977 split_pre->start = em->start;
@@ -1015,7 +982,6 @@ int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre, · 4 unmodified lines
1015 split_pre->orig_block_len = split_pre->block_len;982 split_pre->orig_block_len = split_pre->block_len;
1016 split_pre->ram_bytes = split_pre->len;983 split_pre->ram_bytes = split_pre->len;
1017 split_pre->flags = flags;984 split_pre->flags = flags;
1018 split_pre->compress_type = em->compress_type;
1019 split_pre->generation = em->generation;985 split_pre->generation = em->generation;
1020 986
1021 replace_extent_mapping(em_tree, em, split_pre, 1);987 replace_extent_mapping(em_tree, em, split_pre, 1);
@@ -1034,7 +1000,6 @@ int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre, · 12 unmodified lines
1034 split_mid->orig_block_len = split_mid->block_len;1000 split_mid->orig_block_len = split_mid->block_len;
1035 split_mid->ram_bytes = split_mid->len;1001 split_mid->ram_bytes = split_mid->len;
1036 split_mid->flags = flags;1002 split_mid->flags = flags;
1037 split_mid->compress_type = em->compress_type;
1038 split_mid->generation = em->generation;1003 split_mid->generation = em->generation;
1039 add_extent_mapping(em_tree, split_mid, 1);1004 add_extent_mapping(em_tree, split_mid, 1);
1040 1005
fs/btrfs/extent_map.h+52−25
@@ -5,30 +5,33 @@ · 4 unmodified lines
5 5
6#include <linux/rbtree.h>6#include <linux/rbtree.h>
7#include <linux/refcount.h>7#include <linux/refcount.h>
8#include "compression.h"
8 9
9#define EXTENT_MAP_LAST_BYTE ((u64)-4)10#define EXTENT_MAP_LAST_BYTE ((u64)-4)
10#define EXTENT_MAP_HOLE ((u64)-3)11#define EXTENT_MAP_HOLE ((u64)-3)
11#define EXTENT_MAP_INLINE ((u64)-2)12#define EXTENT_MAP_INLINE ((u64)-2)
12/* used only during fiemap calls */
13#define EXTENT_MAP_DELALLOC ((u64)-1)
14 13
15/* bits for the extent_map::flags field */14/* bits for the extent_map::flags field */
16enum {15enum {
17 /* this entry not yet on disk, don't free it */16 /* this entry not yet on disk, don't free it */
18 EXTENT_FLAG_PINNED,17 ENUM_BIT(EXTENT_FLAG_PINNED),
19 EXTENT_FLAG_COMPRESSED,18 ENUM_BIT(EXTENT_FLAG_COMPRESS_ZLIB),
19 ENUM_BIT(EXTENT_FLAG_COMPRESS_LZO),
20 ENUM_BIT(EXTENT_FLAG_COMPRESS_ZSTD),
20 /* pre-allocated extent */21 /* pre-allocated extent */
21 EXTENT_FLAG_PREALLOC,22 ENUM_BIT(EXTENT_FLAG_PREALLOC),
22 /* Logging this extent */23 /* Logging this extent */
23 EXTENT_FLAG_LOGGING,24 ENUM_BIT(EXTENT_FLAG_LOGGING),
24 /* Filling in a preallocated extent */25 /* Filling in a preallocated extent */
25 EXTENT_FLAG_FILLING,26 ENUM_BIT(EXTENT_FLAG_FILLING),
26 /* filesystem extent mapping type */
27 EXTENT_FLAG_FS_MAPPING,
28 /* This em is merged from two or more physically adjacent ems */27 /* This em is merged from two or more physically adjacent ems */
29 EXTENT_FLAG_MERGED,28 ENUM_BIT(EXTENT_FLAG_MERGED),
30};29};
31 30
31/*
32 * Keep this structure as compact as possible, as we can have really large
33 * amounts of allocated extent maps at any time.
34 */
32struct extent_map {35struct extent_map {
33 struct rb_node rb_node;36 struct rb_node rb_node;
34 37
@@ -49,11 +52,8 @@ struct extent_map { · 14 unmodified lines
49 * For non-merged extents, it's from btrfs_file_extent_item::generation.52 * For non-merged extents, it's from btrfs_file_extent_item::generation.
50 */53 */
51 u64 generation;54 u64 generation;
52 unsigned long flags;55 u32 flags;
53 /* Used for chunk mappings, flag EXTENT_FLAG_FS_MAPPING must be set */
54 struct map_lookup *map_lookup;
55 refcount_t refs;56 refcount_t refs;
56 unsigned int compress_type;
57 struct list_head list;57 struct list_head list;
58};58};
59 59
@@ -65,30 +65,57 @@ struct extent_map_tree { · 5 unmodified lines
65 65
66struct btrfs_inode;66struct btrfs_inode;
67 67
68static inline void extent_map_set_compression(struct extent_map *em,
69 enum btrfs_compression_type type)
70{
71 if (type == BTRFS_COMPRESS_ZLIB)
72 em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
73 else if (type == BTRFS_COMPRESS_LZO)
74 em->flags |= EXTENT_FLAG_COMPRESS_LZO;
75 else if (type == BTRFS_COMPRESS_ZSTD)
76 em->flags |= EXTENT_FLAG_COMPRESS_ZSTD;
77}
78
79static inline enum btrfs_compression_type extent_map_compression(const struct extent_map *em)
80{
81 if (em->flags & EXTENT_FLAG_COMPRESS_ZLIB)
82 return BTRFS_COMPRESS_ZLIB;
83
84 if (em->flags & EXTENT_FLAG_COMPRESS_LZO)
85 return BTRFS_COMPRESS_LZO;
86
87 if (em->flags & EXTENT_FLAG_COMPRESS_ZSTD)
88 return BTRFS_COMPRESS_ZSTD;
89
90 return BTRFS_COMPRESS_NONE;
91}
92
93/*
94 * More efficient way to determine if extent is compressed, instead of using
95 * 'extent_map_compression() != BTRFS_COMPRESS_NONE'.
96 */
97static inline bool extent_map_is_compressed(const struct extent_map *em)
98{
99 return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB |
100 EXTENT_FLAG_COMPRESS_LZO |
101 EXTENT_FLAG_COMPRESS_ZSTD)) != 0;
102}
103
68static inline int extent_map_in_tree(const struct extent_map *em)104static inline int extent_map_in_tree(const struct extent_map *em)
69{105{
70 return !RB_EMPTY_NODE(&em->rb_node);106 return !RB_EMPTY_NODE(&em->rb_node);
71}107}
72 108
73static inline u64 extent_map_end(struct extent_map *em)109static inline u64 extent_map_end(const struct extent_map *em)
74{110{
75 if (em->start + em->len < em->start)111 if (em->start + em->len < em->start)
76 return (u64)-1;112 return (u64)-1;
77 return em->start + em->len;113 return em->start + em->len;
78}114}
79 115
80static inline u64 extent_map_block_end(struct extent_map *em)
81{
82 if (em->block_start + em->block_len < em->block_start)
83 return (u64)-1;
84 return em->block_start + em->block_len;
85}
86
87void extent_map_tree_init(struct extent_map_tree *tree);116void extent_map_tree_init(struct extent_map_tree *tree);
88struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,117struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
89 u64 start, u64 len);118 u64 start, u64 len);
90int add_extent_mapping(struct extent_map_tree *tree,
91 struct extent_map *em, int modified);
92void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);119void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);
93int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,120int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,
94 u64 new_logical);121 u64 new_logical);
@@ -97,7 +124,7 @@ struct extent_map *alloc_extent_map(void); · 2 unmodified lines
97void free_extent_map(struct extent_map *em);124void free_extent_map(struct extent_map *em);
98int __init extent_map_init(void);125int __init extent_map_init(void);
99void __cold extent_map_exit(void);126void __cold extent_map_exit(void);
100int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);127int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
101void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);128void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
102struct extent_map *search_extent_mapping(struct extent_map_tree *tree,129struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
103 u64 start, u64 len);130 u64 start, u64 len);
fs/btrfs/file-item.c+6−9
@@ -59,7 +59,7 @@ void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_siz · 58 unmodified lines
59 goto out_unlock;59 goto out_unlock;
60 }60 }
61 61
62 ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start,62 ret = find_contiguous_extent_bit(inode->file_extent_tree, 0, &start,
63 &end, EXTENT_DIRTY);63 &end, EXTENT_DIRTY);
64 if (!ret && start == 0)64 if (!ret && start == 0)
65 i_size = min(i_size, end + 1);65 i_size = min(i_size, end + 1);
@@ -94,7 +94,7 @@ int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, · 28 unmodified lines
94 94
95 if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))95 if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
96 return 0;96 return 0;
97 return set_extent_bit(&inode->file_extent_tree, start, start + len - 1,97 return set_extent_bit(inode->file_extent_tree, start, start + len - 1,
98 EXTENT_DIRTY, NULL);98 EXTENT_DIRTY, NULL);
99}99}
100 100
@@ -123,7 +123,7 @@ int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, · 22 unmodified lines
123 123
124 if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))124 if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
125 return 0;125 return 0;
126 return clear_extent_bit(&inode->file_extent_tree, start,126 return clear_extent_bit(inode->file_extent_tree, start,
127 start + len - 1, EXTENT_DIRTY, NULL);127 start + len - 1, EXTENT_DIRTY, NULL);
128}128}
129 129
@@ -1294,8 +1294,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · 1164 unmodified lines
1294 return;1294 return;
1295 }1295 }
1296 if (compress_type != BTRFS_COMPRESS_NONE) {1296 if (compress_type != BTRFS_COMPRESS_NONE) {
1297 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);1297 extent_map_set_compression(em, compress_type);
1298 em->compress_type = compress_type;
1299 em->block_start = bytenr;1298 em->block_start = bytenr;
1300 em->block_len = em->orig_block_len;1299 em->block_len = em->orig_block_len;
1301 } else {1300 } else {
@@ -1303,7 +1302,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · 1 unmodified line
1303 em->block_start = bytenr;1302 em->block_start = bytenr;
1304 em->block_len = em->len;1303 em->block_len = em->len;
1305 if (type == BTRFS_FILE_EXTENT_PREALLOC)1304 if (type == BTRFS_FILE_EXTENT_PREALLOC)
1306 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);1305 em->flags |= EXTENT_FLAG_PREALLOC;
1307 }1306 }
1308 } else if (type == BTRFS_FILE_EXTENT_INLINE) {1307 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1309 em->block_start = EXTENT_MAP_INLINE;1308 em->block_start = EXTENT_MAP_INLINE;
@@ -1315,9 +1314,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · 5 unmodified lines
1315 */1314 */
1316 em->orig_start = EXTENT_MAP_HOLE;1315 em->orig_start = EXTENT_MAP_HOLE;
1317 em->block_len = (u64)-1;1316 em->block_len = (u64)-1;
1318 em->compress_type = compress_type;1317 extent_map_set_compression(em, compress_type);
1319 if (compress_type != BTRFS_COMPRESS_NONE)
1320 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1321 } else {1318 } else {
1322 btrfs_err(fs_info,1319 btrfs_err(fs_info,
1323 "unknown file extent item type %d, inode %llu, offset %llu, "1320 "unknown file extent item type %d, inode %llu, offset %llu, "
fs/btrfs/file.c+14−13
@@ -111,8 +111,8 @@ static void btrfs_drop_pages(struct btrfs_fs_info *fs_info, · 110 unmodified lines
111 * accessed as prepare_pages should have marked them accessed111 * accessed as prepare_pages should have marked them accessed
112 * in prepare_pages via find_or_create_page()112 * in prepare_pages via find_or_create_page()
113 */113 */
114 btrfs_page_clamp_clear_checked(fs_info, pages[i], block_start,114 btrfs_folio_clamp_clear_checked(fs_info, page_folio(pages[i]),
115 block_len);115 block_start, block_len);
116 unlock_page(pages[i]);116 unlock_page(pages[i]);
117 put_page(pages[i]);117 put_page(pages[i]);
118 }118 }
@@ -168,9 +168,12 @@ int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages, · 49 unmodified lines
168 for (i = 0; i < num_pages; i++) {168 for (i = 0; i < num_pages; i++) {
169 struct page *p = pages[i];169 struct page *p = pages[i];
170 170
171 btrfs_page_clamp_set_uptodate(fs_info, p, start_pos, num_bytes);171 btrfs_folio_clamp_set_uptodate(fs_info, page_folio(p),
172 btrfs_page_clamp_clear_checked(fs_info, p, start_pos, num_bytes);172 start_pos, num_bytes);
173 btrfs_page_clamp_set_dirty(fs_info, p, start_pos, num_bytes);173 btrfs_folio_clamp_clear_checked(fs_info, page_folio(p),
174 start_pos, num_bytes);
175 btrfs_folio_clamp_set_dirty(fs_info, page_folio(p),
176 start_pos, num_bytes);
174 }177 }
175 178
176 /*179 /*
@@ -869,9 +872,9 @@ static int prepare_uptodate_page(struct inode *inode, · 692 unmodified lines
869 * released.872 * released.
870 *873 *
871 * The private flag check is essential for subpage as we need874 * The private flag check is essential for subpage as we need
872 * to store extra bitmap using page->private.875 * to store extra bitmap using folio private.
873 */876 */
874 if (page->mapping != inode->i_mapping || !PagePrivate(page)) {877 if (page->mapping != inode->i_mapping || !folio_test_private(folio)) {
875 unlock_page(page);878 unlock_page(page);
876 return -EAGAIN;879 return -EAGAIN;
877 }880 }
@@ -2150,7 +2153,6 @@ out: · 1272 unmodified lines
2150 hole_em->block_start = EXTENT_MAP_HOLE;2153 hole_em->block_start = EXTENT_MAP_HOLE;
2151 hole_em->block_len = 0;2154 hole_em->block_len = 0;
2152 hole_em->orig_block_len = 0;2155 hole_em->orig_block_len = 0;
2153 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2154 hole_em->generation = trans->transid;2156 hole_em->generation = trans->transid;
2155 2157
2156 ret = btrfs_replace_extent_map_range(inode, hole_em, true);2158 ret = btrfs_replace_extent_map_range(inode, hole_em, true);
@@ -2839,7 +2841,7 @@ static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode, · 682 unmodified lines
2839 2841
2840 if (em->block_start == EXTENT_MAP_HOLE)2842 if (em->block_start == EXTENT_MAP_HOLE)
2841 ret = RANGE_BOUNDARY_HOLE;2843 ret = RANGE_BOUNDARY_HOLE;
2842 else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))2844 else if (em->flags & EXTENT_FLAG_PREALLOC)
2843 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;2845 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
2844 else2846 else
2845 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;2847 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
@@ -2879,8 +2881,7 @@ static int btrfs_zero_range(struct inode *inode, · 33 unmodified lines
2879 * extents and holes, we drop all the existing extents and allocate a2881 * extents and holes, we drop all the existing extents and allocate a
2880 * new prealloc extent, so that we get a larger contiguous disk extent.2882 * new prealloc extent, so that we get a larger contiguous disk extent.
2881 */2883 */
2882 if (em->start <= alloc_start &&2884 if (em->start <= alloc_start && (em->flags & EXTENT_FLAG_PREALLOC)) {
2883 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
2884 const u64 em_end = em->start + em->len;2885 const u64 em_end = em->start + em->len;
2885 2886
2886 if (em_end >= offset + len) {2887 if (em_end >= offset + len) {
@@ -2915,7 +2916,7 @@ static int btrfs_zero_range(struct inode *inode, · 28 unmodified lines
2915 goto out;2916 goto out;
2916 }2917 }
2917 2918
2918 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {2919 if (em->flags & EXTENT_FLAG_PREALLOC) {
2919 free_extent_map(em);2920 free_extent_map(em);
2920 ret = btrfs_fallocate_update_isize(inode, offset + len,2921 ret = btrfs_fallocate_update_isize(inode, offset + len,
2921 mode);2922 mode);
@@ -3136,7 +3137,7 @@ static long btrfs_fallocate(struct file *file, int mode, · 214 unmodified lines
3136 last_byte = ALIGN(last_byte, blocksize);3137 last_byte = ALIGN(last_byte, blocksize);
3137 if (em->block_start == EXTENT_MAP_HOLE ||3138 if (em->block_start == EXTENT_MAP_HOLE ||
3138 (cur_offset >= inode->i_size &&3139 (cur_offset >= inode->i_size &&
3139 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {3140 !(em->flags & EXTENT_FLAG_PREALLOC))) {
3140 const u64 range_len = last_byte - cur_offset;3141 const u64 range_len = last_byte - cur_offset;
3141 3142
3142 ret = add_falloc_range(&reserve_list, cur_offset, range_len);3143 ret = add_falloc_range(&reserve_list, cur_offset, range_len);
fs/btrfs/free-space-cache.c+2−2
@@ -439,8 +439,8 @@ static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl) · 438 unmodified lines
439 439
440 for (i = 0; i < io_ctl->num_pages; i++) {440 for (i = 0; i < io_ctl->num_pages; i++) {
441 if (io_ctl->pages[i]) {441 if (io_ctl->pages[i]) {
442 btrfs_page_clear_checked(io_ctl->fs_info,442 btrfs_folio_clear_checked(io_ctl->fs_info,
443 io_ctl->pages[i],443 page_folio(io_ctl->pages[i]),
444 page_offset(io_ctl->pages[i]),444 page_offset(io_ctl->pages[i]),
445 PAGE_SIZE);445 PAGE_SIZE);
446 unlock_page(io_ctl->pages[i]);446 unlock_page(io_ctl->pages[i]);
fs/btrfs/fs.h+3−15
@@ -188,6 +188,7 @@ enum { · 187 unmodified lines
188 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 27),188 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 27),
189 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 28),189 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 28),
190 BTRFS_MOUNT_NODISCARD = (1UL << 29),190 BTRFS_MOUNT_NODISCARD = (1UL << 29),
191 BTRFS_MOUNT_NOSPACECACHE = (1UL << 30),
191};192};
192 193
193/*194/*
@@ -398,7 +399,8 @@ struct btrfs_fs_info { · 204 unmodified lines
398 struct extent_io_tree excluded_extents;399 struct extent_io_tree excluded_extents;
399 400
400 /* logical->physical extent mapping */401 /* logical->physical extent mapping */
401 struct extent_map_tree mapping_tree;402 struct rb_root_cached mapping_tree;
403 rwlock_t mapping_tree_lock;
402 404
403 /*405 /*
404 * Block reservation for extent, checksum, root tree and delayed dir406 * Block reservation for extent, checksum, root tree and delayed dir
@@ -960,20 +962,6 @@ void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag, · 555 unmodified lines
960#define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \962#define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
961 BTRFS_MOUNT_##opt)963 BTRFS_MOUNT_##opt)
962 964
963#define btrfs_set_and_info(fs_info, opt, fmt, args...) \
964do { \
965 if (!btrfs_test_opt(fs_info, opt)) \
966 btrfs_info(fs_info, fmt, ##args); \
967 btrfs_set_opt(fs_info->mount_opt, opt); \
968} while (0)
969
970#define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
971do { \
972 if (btrfs_test_opt(fs_info, opt)) \
973 btrfs_info(fs_info, fmt, ##args); \
974 btrfs_clear_opt(fs_info->mount_opt, opt); \
975} while (0)
976
977static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)965static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
978{966{
979 /* Do it this way so we only ever do one test_bit in the normal case. */967 /* Do it this way so we only ever do one test_bit in the normal case. */
fs/btrfs/inode.c+98−55
@@ -114,6 +114,15 @@ struct data_reloc_warn { · 113 unmodified lines
114 int mirror_num;114 int mirror_num;
115};115};
116 116
117/*
118 * For the file_extent_tree, we want to hold the inode lock when we lookup and
119 * update the disk_i_size, but lockdep will complain because our io_tree we hold
120 * the tree lock and get the inode lock when setting delalloc. These two things
121 * are unrelated, so make a class for the file_extent_tree so we don't get the
122 * two locking patterns mixed up.
123 */
124static struct lock_class_key file_extent_tree_class;
125
117static const struct inode_operations btrfs_dir_inode_operations;126static const struct inode_operations btrfs_dir_inode_operations;
118static const struct inode_operations btrfs_symlink_inode_operations;127static const struct inode_operations btrfs_symlink_inode_operations;
119static const struct inode_operations btrfs_special_inode_operations;128static const struct inode_operations btrfs_special_inode_operations;
@@ -447,8 +456,8 @@ static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode, · 327 unmodified lines
447 * range, then btrfs_mark_ordered_io_finished() will handle456 * range, then btrfs_mark_ordered_io_finished() will handle
448 * the ordered extent accounting for the range.457 * the ordered extent accounting for the range.
449 */458 */
450 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,459 btrfs_folio_clamp_clear_ordered(inode->root->fs_info,
451 offset, bytes);460 page_folio(page), offset, bytes);
452 put_page(page);461 put_page(page);
453 }462 }
454 463
@@ -1037,7 +1046,7 @@ free_pages: · 582 unmodified lines
1037 if (pages) {1046 if (pages) {
1038 for (i = 0; i < nr_pages; i++) {1047 for (i = 0; i < nr_pages; i++) {
1039 WARN_ON(pages[i]->mapping);1048 WARN_ON(pages[i]->mapping);
1040 put_page(pages[i]);1049 btrfs_free_compr_page(pages[i]);
1041 }1050 }
1042 kfree(pages);1051 kfree(pages);
1043 }1052 }
@@ -1052,7 +1061,7 @@ static void free_async_extent_pages(struct async_extent *async_extent) · 8 unmodified lines
1052 1061
1053 for (i = 0; i < async_extent->nr_pages; i++) {1062 for (i = 0; i < async_extent->nr_pages; i++) {
1054 WARN_ON(async_extent->pages[i]->mapping);1063 WARN_ON(async_extent->pages[i]->mapping);
1055 put_page(async_extent->pages[i]);1064 btrfs_free_compr_page(async_extent->pages[i]);
1056 }1065 }
1057 kfree(async_extent->pages);1066 kfree(async_extent->pages);
1058 async_extent->nr_pages = 0;1067 async_extent->nr_pages = 0;
@@ -2793,7 +2802,7 @@ out_page: · 1734 unmodified lines
2793 PAGE_SIZE, !ret);2802 PAGE_SIZE, !ret);
2794 clear_page_dirty_for_io(page);2803 clear_page_dirty_for_io(page);
2795 }2804 }
2796 btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);2805 btrfs_folio_clear_checked(fs_info, page_folio(page), page_start, PAGE_SIZE);
2797 unlock_page(page);2806 unlock_page(page);
2798 put_page(page);2807 put_page(page);
2799 kfree(fixup);2808 kfree(fixup);
@@ -2848,7 +2857,7 @@ int btrfs_writepage_cow_fixup(struct page *page) · 48 unmodified lines
2848 * page->mapping outside of the page lock.2857 * page->mapping outside of the page lock.
2849 */2858 */
2850 ihold(inode);2859 ihold(inode);
2851 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);2860 btrfs_folio_set_checked(fs_info, page_folio(page), page_offset(page), PAGE_SIZE);
2852 get_page(page);2861 get_page(page);
2853 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL);2862 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL);
2854 fixup->page = page;2863 fixup->page = page;
@@ -3118,7 +3127,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 263 unmodified lines
3118 ordered_extent->disk_num_bytes);3127 ordered_extent->disk_num_bytes);
3119 }3128 }
3120 }3129 }
3121 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,3130 unpin_extent_cache(inode, ordered_extent->file_offset,
3122 ordered_extent->num_bytes, trans->transid);3131 ordered_extent->num_bytes, trans->transid);
3123 if (ret < 0) {3132 if (ret < 0) {
3124 btrfs_abort_transaction(trans, ret);3133 btrfs_abort_transaction(trans, ret);
@@ -3796,7 +3805,7 @@ cache_index: · 671 unmodified lines
3796 * cache.3805 * cache.
3797 *3806 *
3798 * This is required for both inode re-read from disk and delayed inode3807 * This is required for both inode re-read from disk and delayed inode
3799 * in delayed_nodes_tree.3808 * in the delayed_nodes xarray.
3800 */3809 */
3801 if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info))3810 if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info))
3802 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,3811 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
@@ -4725,7 +4734,7 @@ again: · 922 unmodified lines
4725 /*4734 /*
4726 * We unlock the page after the io is completed and then re-lock it4735 * We unlock the page after the io is completed and then re-lock it
4727 * above. release_folio() could have come in between that and cleared4736 * above. release_folio() could have come in between that and cleared
4728 * PagePrivate(), but left the page in the mapping. Set the page mapped4737 * folio private, but left the page in the mapping. Set the page mapped
4729 * here to make sure it's properly set for the subpage stuff.4738 * here to make sure it's properly set for the subpage stuff.
4730 */4739 */
4731 ret = set_page_extent_mapped(page);4740 ret = set_page_extent_mapped(page);
@@ -4767,9 +4776,10 @@ again: · 35 unmodified lines
4767 memzero_page(page, (block_start - page_offset(page)) + offset,4776 memzero_page(page, (block_start - page_offset(page)) + offset,
4768 len);4777 len);
4769 }4778 }
4770 btrfs_page_clear_checked(fs_info, page, block_start,4779 btrfs_folio_clear_checked(fs_info, page_folio(page), block_start,
4771 block_end + 1 - block_start);4780 block_end + 1 - block_start);
4772 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);4781 btrfs_folio_set_dirty(fs_info, page_folio(page), block_start,
4782 block_end + 1 - block_start);
4773 unlock_extent(io_tree, block_start, block_end, &cached_state);4783 unlock_extent(io_tree, block_start, block_end, &cached_state);
4774 4784
4775 if (only_release_metadata)4785 if (only_release_metadata)
@@ -4889,7 +4899,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) · 113 unmodified lines
4889 last_byte = ALIGN(last_byte, fs_info->sectorsize);4899 last_byte = ALIGN(last_byte, fs_info->sectorsize);
4890 hole_size = last_byte - cur_offset;4900 hole_size = last_byte - cur_offset;
4891 4901
4892 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {4902 if (!(em->flags & EXTENT_FLAG_PREALLOC)) {
4893 struct extent_map *hole_em;4903 struct extent_map *hole_em;
4894 4904
4895 err = maybe_insert_hole(inode, cur_offset, hole_size);4905 err = maybe_insert_hole(inode, cur_offset, hole_size);
@@ -4917,7 +4927,6 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) · 21 unmodified lines
4917 hole_em->block_len = 0;4927 hole_em->block_len = 0;
4918 hole_em->orig_block_len = 0;4928 hole_em->orig_block_len = 0;
4919 hole_em->ram_bytes = hole_size;4929 hole_em->ram_bytes = hole_size;
4920 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4921 hole_em->generation = btrfs_get_fs_generation(fs_info);4930 hole_em->generation = btrfs_get_fs_generation(fs_info);
4922 4931
4923 err = btrfs_replace_extent_map_range(inode, hole_em, true);4932 err = btrfs_replace_extent_map_range(inode, hole_em, true);
@@ -6216,6 +6225,13 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans, · 1292 unmodified lines
6216 BTRFS_I(inode)->generation = trans->transid;6225 BTRFS_I(inode)->generation = trans->transid;
6217 inode->i_generation = BTRFS_I(inode)->generation;6226 inode->i_generation = BTRFS_I(inode)->generation;
6218 6227
6228 /*
6229 * We don't have any capability xattrs set here yet, shortcut any
6230 * queries for the xattrs here. If we add them later via the inode
6231 * security init path or any other path this flag will be cleared.
6232 */
6233 set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
6234
6219 /*6235 /*
6220 * Subvolumes don't inherit flags from their parent directory.6236 * Subvolumes don't inherit flags from their parent directory.
6221 * Originally this was probably by accident, but we probably can't6237 * Originally this was probably by accident, but we probably can't
@@ -7258,13 +7274,11 @@ static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start, · 1036 unmodified lines
7258 em->orig_block_len = orig_block_len;7274 em->orig_block_len = orig_block_len;
7259 em->ram_bytes = ram_bytes;7275 em->ram_bytes = ram_bytes;
7260 em->generation = -1;7276 em->generation = -1;
7261 set_bit(EXTENT_FLAG_PINNED, &em->flags);7277 em->flags |= EXTENT_FLAG_PINNED;
7262 if (type == BTRFS_ORDERED_PREALLOC) {7278 if (type == BTRFS_ORDERED_PREALLOC)
7263 set_bit(EXTENT_FLAG_FILLING, &em->flags);7279 em->flags |= EXTENT_FLAG_FILLING;
7264 } else if (type == BTRFS_ORDERED_COMPRESSED) {7280 else if (type == BTRFS_ORDERED_COMPRESSED)
7265 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);7281 extent_map_set_compression(em, compress_type);
7266 em->compress_type = compress_type;
7267 }
7268 7282
7269 ret = btrfs_replace_extent_map_range(inode, em, true);7283 ret = btrfs_replace_extent_map_range(inode, em, true);
7270 if (ret) {7284 if (ret) {
@@ -7304,10 +7318,10 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, · 33 unmodified lines
7304 * just use the extent.7318 * just use the extent.
7305 *7319 *
7306 */7320 */
7307 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||7321 if ((em->flags & EXTENT_FLAG_PREALLOC) ||
7308 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&7322 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7309 em->block_start != EXTENT_MAP_HOLE)) {7323 em->block_start != EXTENT_MAP_HOLE)) {
7310 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))7324 if (em->flags & EXTENT_FLAG_PREALLOC)
7311 type = BTRFS_ORDERED_PREALLOC;7325 type = BTRFS_ORDERED_PREALLOC;
7312 else7326 else
7313 type = BTRFS_ORDERED_NOCOW;7327 type = BTRFS_ORDERED_NOCOW;
@@ -7542,7 +7556,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, · 228 unmodified lines
7542 * to buffered IO. Don't blame me, this is the price we pay for using7556 * to buffered IO. Don't blame me, this is the price we pay for using
7543 * the generic code.7557 * the generic code.
7544 */7558 */
7545 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||7559 if (extent_map_is_compressed(em) ||
7546 em->block_start == EXTENT_MAP_INLINE) {7560 em->block_start == EXTENT_MAP_INLINE) {
7547 free_extent_map(em);7561 free_extent_map(em);
7548 /*7562 /*
@@ -7638,7 +7652,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, · 89 unmodified lines
7638 * that, since we have locked only the parts we are performing I/O in.7652 * that, since we have locked only the parts we are performing I/O in.
7639 */7653 */
7640 if ((em->block_start == EXTENT_MAP_HOLE) ||7654 if ((em->block_start == EXTENT_MAP_HOLE) ||
7641 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {7655 ((em->flags & EXTENT_FLAG_PREALLOC) && !write)) {
7642 iomap->addr = IOMAP_NULL_ADDR;7656 iomap->addr = IOMAP_NULL_ADDR;
7643 iomap->type = IOMAP_HOLE;7657 iomap->type = IOMAP_HOLE;
7644 } else {7658 } else {
@@ -7851,13 +7865,14 @@ static void btrfs_readahead(struct readahead_control *rac) · 206 unmodified lines
7851static void wait_subpage_spinlock(struct page *page)7865static void wait_subpage_spinlock(struct page *page)
7852{7866{
7853 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);7867 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
7868 struct folio *folio = page_folio(page);
7854 struct btrfs_subpage *subpage;7869 struct btrfs_subpage *subpage;
7855 7870
7856 if (!btrfs_is_subpage(fs_info, page))7871 if (!btrfs_is_subpage(fs_info, page->mapping))
7857 return;7872 return;
7858 7873
7859 ASSERT(PagePrivate(page) && page->private);7874 ASSERT(folio_test_private(folio) && folio_get_private(folio));
7860 subpage = (struct btrfs_subpage *)page->private;7875 subpage = folio_get_private(folio);
7861 7876
7862 /*7877 /*
7863 * This may look insane as we just acquire the spinlock and release it,7878 * This may look insane as we just acquire the spinlock and release it,
@@ -7995,7 +8010,7 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset, · 131 unmodified lines
7995 page_end);8010 page_end);
7996 ASSERT(range_end + 1 - cur < U32_MAX);8011 ASSERT(range_end + 1 - cur < U32_MAX);
7997 range_len = range_end + 1 - cur;8012 range_len = range_end + 1 - cur;
7998 if (!btrfs_page_test_ordered(fs_info, &folio->page, cur, range_len)) {8013 if (!btrfs_folio_test_ordered(fs_info, folio, cur, range_len)) {
7999 /*8014 /*
8000 * If Ordered (Private2) is cleared, it means endio has8015 * If Ordered (Private2) is cleared, it means endio has
8001 * already been executed for the range.8016 * already been executed for the range.
@@ -8004,7 +8019,7 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset, · 2 unmodified lines
8004 */8019 */
8005 goto next;8020 goto next;
8006 }8021 }
8007 btrfs_page_clear_ordered(fs_info, &folio->page, cur, range_len);8022 btrfs_folio_clear_ordered(fs_info, folio, cur, range_len);
8008 8023
8009 /*8024 /*
8010 * IO on this page will never be started, so we need to account8025 * IO on this page will never be started, so we need to account
@@ -8074,7 +8089,7 @@ next: · 63 unmodified lines
8074 * did something wrong.8089 * did something wrong.
8075 */8090 */
8076 ASSERT(!folio_test_ordered(folio));8091 ASSERT(!folio_test_ordered(folio));
8077 btrfs_page_clear_checked(fs_info, &folio->page, folio_pos(folio), folio_size(folio));8092 btrfs_folio_clear_checked(fs_info, folio, folio_pos(folio), folio_size(folio));
8078 if (!inode_evicting)8093 if (!inode_evicting)
8079 __btrfs_release_folio(folio, GFP_NOFS);8094 __btrfs_release_folio(folio, GFP_NOFS);
8080 clear_page_extent_mapped(&folio->page);8095 clear_page_extent_mapped(&folio->page);
@@ -8098,6 +8113,7 @@ next: · 17 unmodified lines
8098vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)8113vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
8099{8114{
8100 struct page *page = vmf->page;8115 struct page *page = vmf->page;
8116 struct folio *folio = page_folio(page);
8101 struct inode *inode = file_inode(vmf->vma->vm_file);8117 struct inode *inode = file_inode(vmf->vma->vm_file);
8102 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);8118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8103 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;8119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
@@ -8114,6 +8130,8 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) · 10 unmodified lines
8114 u64 page_end;8130 u64 page_end;
8115 u64 end;8131 u64 end;
8116 8132
8133 ASSERT(folio_order(folio) == 0);
8134
8117 reserved_space = PAGE_SIZE;8135 reserved_space = PAGE_SIZE;
8118 8136
8119 sb_start_pagefault(inode->i_sb);8137 sb_start_pagefault(inode->i_sb);
@@ -8217,9 +8235,9 @@ again: · 97 unmodified lines
8217 if (zero_start != PAGE_SIZE)8235 if (zero_start != PAGE_SIZE)
8218 memzero_page(page, zero_start, PAGE_SIZE - zero_start);8236 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
8219 8237
8220 btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);8238 btrfs_folio_clear_checked(fs_info, folio, page_start, PAGE_SIZE);
8221 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);8239 btrfs_folio_set_dirty(fs_info, folio, page_start, end + 1 - page_start);
8222 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);8240 btrfs_folio_set_uptodate(fs_info, folio, page_start, end + 1 - page_start);
8223 8241
8224 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));8242 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
8225 8243
@@ -8462,10 +8480,20 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 236 unmodified lines
8462 struct btrfs_fs_info *fs_info = btrfs_sb(sb);8480 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
8463 struct btrfs_inode *ei;8481 struct btrfs_inode *ei;
8464 struct inode *inode;8482 struct inode *inode;
8483 struct extent_io_tree *file_extent_tree = NULL;
8484
8485 /* Self tests may pass a NULL fs_info. */
8486 if (fs_info && !btrfs_fs_incompat(fs_info, NO_HOLES)) {
8487 file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL);
8488 if (!file_extent_tree)
8489 return NULL;
8490 }
8465 8491
8466 ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);8492 ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);
8467 if (!ei)8493 if (!ei) {
8494 kfree(file_extent_tree);
8468 return NULL;8495 return NULL;
8496 }
8469 8497
8470 ei->root = NULL;8498 ei->root = NULL;
8471 ei->generation = 0;8499 ei->generation = 0;
@@ -8501,10 +8529,18 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 29 unmodified lines
8501 8529
8502 inode = &ei->vfs_inode;8530 inode = &ei->vfs_inode;
8503 extent_map_tree_init(&ei->extent_tree);8531 extent_map_tree_init(&ei->extent_tree);
8532
8533 /* This io tree sets the valid inode. */
8504 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO);8534 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO);
8505 ei->io_tree.inode = ei;8535 ei->io_tree.inode = ei;
8506 extent_io_tree_init(fs_info, &ei->file_extent_tree,8536
8507 IO_TREE_INODE_FILE_EXTENT);8537 ei->file_extent_tree = file_extent_tree;
8538 if (file_extent_tree) {
8539 extent_io_tree_init(fs_info, ei->file_extent_tree,
8540 IO_TREE_INODE_FILE_EXTENT);
8541 /* Lockdep class is set only for the file extent tree. */
8542 lockdep_set_class(&ei->file_extent_tree->lock, &file_extent_tree_class);
8543 }
8508 mutex_init(&ei->log_mutex);8544 mutex_init(&ei->log_mutex);
8509 spin_lock_init(&ei->ordered_tree_lock);8545 spin_lock_init(&ei->ordered_tree_lock);
8510 ei->ordered_tree = RB_ROOT;8546 ei->ordered_tree = RB_ROOT;
@@ -8521,12 +8557,14 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 10 unmodified lines
8521void btrfs_test_destroy_inode(struct inode *inode)8557void btrfs_test_destroy_inode(struct inode *inode)
8522{8558{
8523 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);8559 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
8560 kfree(BTRFS_I(inode)->file_extent_tree);
8524 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));8561 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8525}8562}
8526#endif8563#endif
8527 8564
8528void btrfs_free_inode(struct inode *inode)8565void btrfs_free_inode(struct inode *inode)
8529{8566{
8567 kfree(BTRFS_I(inode)->file_extent_tree);
8530 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));8568 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8531}8569}
8532 8570
@@ -9632,7 +9670,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, · 1099 unmodified lines
9632 em->block_len = ins.offset;9670 em->block_len = ins.offset;
9633 em->orig_block_len = ins.offset;9671 em->orig_block_len = ins.offset;
9634 em->ram_bytes = ins.offset;9672 em->ram_bytes = ins.offset;
9635 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);9673 em->flags |= EXTENT_FLAG_PREALLOC;
9636 em->generation = trans->transid;9674 em->generation = trans->transid;
9637 9675
9638 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true);9676 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true);
@@ -9785,7 +9823,9 @@ void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end) · 146 unmodified lines
9785 page = find_get_page(inode->vfs_inode.i_mapping, index);9823 page = find_get_page(inode->vfs_inode.i_mapping, index);
9786 ASSERT(page); /* Pages should be in the extent_io_tree */9824 ASSERT(page); /* Pages should be in the extent_io_tree */
9787 9825
9788 btrfs_page_set_writeback(fs_info, page, start, len);9826 /* This is for data, which doesn't yet support larger folio. */
9827 ASSERT(folio_order(page_folio(page)) == 0);
9828 btrfs_folio_set_writeback(fs_info, page_folio(page), start, len);
9789 put_page(page);9829 put_page(page);
9790 index++;9830 index++;
9791 }9831 }
@@ -9994,7 +10034,7 @@ static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb, · 202 unmodified lines
9994 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);10034 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
9995 if (!pages)10035 if (!pages)
9996 return -ENOMEM;10036 return -ENOMEM;
9997 ret = btrfs_alloc_page_array(nr_pages, pages);10037 ret = btrfs_alloc_page_array(nr_pages, pages, 0);
9998 if (ret) {10038 if (ret) {
9999 ret = -ENOMEM;10039 ret = -ENOMEM;
10000 goto out;10040 goto out;
@@ -10113,12 +10153,12 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, · 112 unmodified lines
10113 encoded->len = min_t(u64, extent_map_end(em),10153 encoded->len = min_t(u64, extent_map_end(em),
10114 inode->vfs_inode.i_size) - iocb->ki_pos;10154 inode->vfs_inode.i_size) - iocb->ki_pos;
10115 if (em->block_start == EXTENT_MAP_HOLE ||10155 if (em->block_start == EXTENT_MAP_HOLE ||
10116 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {10156 (em->flags & EXTENT_FLAG_PREALLOC)) {
10117 disk_bytenr = EXTENT_MAP_HOLE;10157 disk_bytenr = EXTENT_MAP_HOLE;
10118 count = min_t(u64, count, encoded->len);10158 count = min_t(u64, count, encoded->len);
10119 encoded->len = count;10159 encoded->len = count;
10120 encoded->unencoded_len = count;10160 encoded->unencoded_len = count;
10121 } else if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {10161 } else if (extent_map_is_compressed(em)) {
10122 disk_bytenr = em->block_start;10162 disk_bytenr = em->block_start;
10123 /*10163 /*
10124 * Bail if the buffer isn't large enough to return the whole10164 * Bail if the buffer isn't large enough to return the whole
@@ -10133,7 +10173,7 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, · 8 unmodified lines
10133 encoded->unencoded_len = em->ram_bytes;10173 encoded->unencoded_len = em->ram_bytes;
10134 encoded->unencoded_offset = iocb->ki_pos - em->orig_start;10174 encoded->unencoded_offset = iocb->ki_pos - em->orig_start;
10135 ret = btrfs_encoded_io_compression_from_extent(fs_info,10175 ret = btrfs_encoded_io_compression_from_extent(fs_info,
10136 em->compress_type);10176 extent_map_compression(em));
10137 if (ret < 0)10177 if (ret < 0)
10138 goto out_em;10178 goto out_em;
10139 encoded->compression = ret;10179 encoded->compression = ret;
@@ -10564,6 +10604,7 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 424 unmodified lines
10564 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;10604 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10565 struct extent_state *cached_state = NULL;10605 struct extent_state *cached_state = NULL;
10566 struct extent_map *em = NULL;10606 struct extent_map *em = NULL;
10607 struct btrfs_chunk_map *map = NULL;
10567 struct btrfs_device *device = NULL;10608 struct btrfs_device *device = NULL;
10568 struct btrfs_swap_info bsi = {10609 struct btrfs_swap_info bsi = {
10569 .lowest_ppage = (sector_t)-1ULL,10610 .lowest_ppage = (sector_t)-1ULL,
@@ -10680,7 +10721,7 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 110 unmodified lines
10680 ret = -EINVAL;10721 ret = -EINVAL;
10681 goto out;10722 goto out;
10682 }10723 }
10683 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {10724 if (extent_map_is_compressed(em)) {
10684 btrfs_warn(fs_info, "swapfile must not be compressed");10725 btrfs_warn(fs_info, "swapfile must not be compressed");
10685 ret = -EINVAL;10726 ret = -EINVAL;
10686 goto out;10727 goto out;
@@ -10703,13 +10744,13 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 16 unmodified lines
10703 goto out;10744 goto out;
10704 }10745 }
10705 10746
10706 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);10747 map = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10707 if (IS_ERR(em)) {10748 if (IS_ERR(map)) {
10708 ret = PTR_ERR(em);10749 ret = PTR_ERR(map);
10709 goto out;10750 goto out;
10710 }10751 }
10711 10752
10712 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {10753 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10713 btrfs_warn(fs_info,10754 btrfs_warn(fs_info,
10714 "swapfile must have single data profile");10755 "swapfile must have single data profile");
10715 ret = -EINVAL;10756 ret = -EINVAL;
@@ -10717,23 +10758,23 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 1 unmodified line
10717 }10758 }
10718 10759
10719 if (device == NULL) {10760 if (device == NULL) {
10720 device = em->map_lookup->stripes[0].dev;10761 device = map->stripes[0].dev;
10721 ret = btrfs_add_swapfile_pin(inode, device, false);10762 ret = btrfs_add_swapfile_pin(inode, device, false);
10722 if (ret == 1)10763 if (ret == 1)
10723 ret = 0;10764 ret = 0;
10724 else if (ret)10765 else if (ret)
10725 goto out;10766 goto out;
10726 } else if (device != em->map_lookup->stripes[0].dev) {10767 } else if (device != map->stripes[0].dev) {
10727 btrfs_warn(fs_info, "swapfile must be on one device");10768 btrfs_warn(fs_info, "swapfile must be on one device");
10728 ret = -EINVAL;10769 ret = -EINVAL;
10729 goto out;10770 goto out;
10730 }10771 }
10731 10772
10732 physical_block_start = (em->map_lookup->stripes[0].physical +10773 physical_block_start = (map->stripes[0].physical +
10733 (logical_block_start - em->start));10774 (logical_block_start - map->start));
10734 len = min(len, em->len - (logical_block_start - em->start));10775 len = min(len, map->chunk_len - (logical_block_start - map->start));
10735 free_extent_map(em);10776 btrfs_free_chunk_map(map);
10736 em = NULL;10777 map = NULL;
10737 10778
10738 bg = btrfs_lookup_block_group(fs_info, logical_block_start);10779 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10739 if (!bg) {10780 if (!bg) {
@@ -10786,6 +10827,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 46 unmodified lines
10786out:10827out:
10787 if (!IS_ERR_OR_NULL(em))10828 if (!IS_ERR_OR_NULL(em))
10788 free_extent_map(em);10829 free_extent_map(em);
10830 if (!IS_ERR_OR_NULL(map))
10831 btrfs_free_chunk_map(map);
10789 10832
10790 unlock_extent(io_tree, 0, isize - 1, &cached_state);10833 unlock_extent(io_tree, 0, isize - 1, &cached_state);
10791 10834
fs/btrfs/lru_cache.c+1−1
@@ -9,7 +9,7 @@ · 8 unmodified lines
9 *9 *
10 * @cache: The cache.10 * @cache: The cache.
11 * @max_size: Maximum size (number of entries) for the cache.11 * @max_size: Maximum size (number of entries) for the cache.
12 * Use 0 for unlimited size, it's the user's responsability to12 * Use 0 for unlimited size, it's the user's responsibility to
13 * trim the cache in that case.13 * trim the cache in that case.
14 */14 */
15void btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size)15void btrfs_lru_cache_init(struct btrfs_lru_cache *cache, unsigned int max_size)
fs/btrfs/lzo.c+2−2
@@ -152,7 +152,7 @@ static int copy_compressed_data_to_page(char *compressed_data, · 151 unmodified lines
152 cur_page = out_pages[*cur_out / PAGE_SIZE];152 cur_page = out_pages[*cur_out / PAGE_SIZE];
153 /* Allocate a new page */153 /* Allocate a new page */
154 if (!cur_page) {154 if (!cur_page) {
155 cur_page = alloc_page(GFP_NOFS);155 cur_page = btrfs_alloc_compr_page();
156 if (!cur_page)156 if (!cur_page)
157 return -ENOMEM;157 return -ENOMEM;
158 out_pages[*cur_out / PAGE_SIZE] = cur_page;158 out_pages[*cur_out / PAGE_SIZE] = cur_page;
@@ -178,7 +178,7 @@ static int copy_compressed_data_to_page(char *compressed_data, · 19 unmodified lines
178 cur_page = out_pages[*cur_out / PAGE_SIZE];178 cur_page = out_pages[*cur_out / PAGE_SIZE];
179 /* Allocate a new page */179 /* Allocate a new page */
180 if (!cur_page) {180 if (!cur_page) {
181 cur_page = alloc_page(GFP_NOFS);181 cur_page = btrfs_alloc_compr_page();
182 if (!cur_page)182 if (!cur_page)
183 return -ENOMEM;183 return -ENOMEM;
184 out_pages[*cur_out / PAGE_SIZE] = cur_page;184 out_pages[*cur_out / PAGE_SIZE] = cur_page;
fs/btrfs/messages.c+1−1
@@ -287,7 +287,7 @@ void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info) · 286 unmodified lines
287 * panic or BUGs, depending on mount options.287 * panic or BUGs, depending on mount options.
288 */288 */
289__cold289__cold
290void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,290void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
291 unsigned int line, int error, const char *fmt, ...)291 unsigned int line, int error, const char *fmt, ...)
292{292{
293 char *s_id = "<unknown>";293 char *s_id = "<unknown>";
fs/btrfs/messages.h+1−1
@@ -194,7 +194,7 @@ const char * __attribute_const__ btrfs_decode_error(int error); · 193 unmodified lines
194 194
195__printf(5, 6)195__printf(5, 6)
196__cold196__cold
197void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,197void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
198 unsigned int line, int error, const char *fmt, ...);198 unsigned int line, int error, const char *fmt, ...);
199/*199/*
200 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic200 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
fs/btrfs/ordered-data.c+3−2
@@ -323,9 +323,10 @@ static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered, · 322 unmodified lines
323 *323 *
324 * If there's no such bit, we need to skip to next range.324 * If there's no such bit, we need to skip to next range.
325 */325 */
326 if (!btrfs_page_test_ordered(fs_info, page, file_offset, len))326 if (!btrfs_folio_test_ordered(fs_info, page_folio(page),
327 file_offset, len))
327 return false;328 return false;
328 btrfs_page_clear_ordered(fs_info, page, file_offset, len);329 btrfs_folio_clear_ordered(fs_info, page_folio(page), file_offset, len);
329 }330 }
330 331
331 /* Now we're fine to update the accounting. */332 /* Now we're fine to update the accounting. */
fs/btrfs/ordered-data.h+0−7
@@ -96,13 +96,6 @@ struct btrfs_ordered_extent { · 95 unmodified lines
96 /* number of bytes that still need writing */96 /* number of bytes that still need writing */
97 u64 bytes_left;97 u64 bytes_left;
98 98
99 /*
100 * the end of the ordered extent which is behind it but
101 * didn't update disk_i_size. Please see the comment of
102 * btrfs_ordered_update_i_size();
103 */
104 u64 outstanding_isize;
105
106 /*99 /*
107 * If we get truncated we need to adjust the file extent we enter for100 * If we get truncated we need to adjust the file extent we enter for
108 * this ordered extent so that we do not expose stale data.101 * this ordered extent so that we do not expose stale data.
fs/btrfs/qgroup.c+1−1
@@ -194,7 +194,7 @@ static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info, · 193 unmodified lines
194 *194 *
195 * Must be called with qgroup_lock held and @prealloc preallocated.195 * Must be called with qgroup_lock held and @prealloc preallocated.
196 *196 *
197 * The control on the lifespan of @prealloc would be transfered to this197 * The control on the lifespan of @prealloc would be transferred to this
198 * function, thus caller should no longer touch @prealloc.198 * function, thus caller should no longer touch @prealloc.
199 */199 */
200static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,200static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
fs/btrfs/raid56.c+3−4
@@ -964,7 +964,7 @@ static int alloc_rbio_pages(struct btrfs_raid_bio *rbio) · 963 unmodified lines
964{964{
965 int ret;965 int ret;
966 966
967 ret = btrfs_alloc_page_array(rbio->nr_pages, rbio->stripe_pages);967 ret = btrfs_alloc_page_array(rbio->nr_pages, rbio->stripe_pages, 0);
968 if (ret < 0)968 if (ret < 0)
969 return ret;969 return ret;
970 /* Mapping all sectors */970 /* Mapping all sectors */
@@ -979,7 +979,7 @@ static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio) · 8 unmodified lines
979 int ret;979 int ret;
980 980
981 ret = btrfs_alloc_page_array(rbio->nr_pages - data_pages,981 ret = btrfs_alloc_page_array(rbio->nr_pages - data_pages,
982 rbio->stripe_pages + data_pages);982 rbio->stripe_pages + data_pages, 0);
983 if (ret < 0)983 if (ret < 0)
984 return ret;984 return ret;
985 985
@@ -1530,7 +1530,7 @@ static int alloc_rbio_data_pages(struct btrfs_raid_bio *rbio) · 544 unmodified lines
1530 const int data_pages = rbio->nr_data * rbio->stripe_npages;1530 const int data_pages = rbio->nr_data * rbio->stripe_npages;
1531 int ret;1531 int ret;
1532 1532
1533 ret = btrfs_alloc_page_array(data_pages, rbio->stripe_pages);1533 ret = btrfs_alloc_page_array(data_pages, rbio->stripe_pages, 0);
1534 if (ret < 0)1534 if (ret < 0)
1535 return ret;1535 return ret;
1536 1536
@@ -1549,7 +1549,6 @@ struct btrfs_plug_cb { · 12 unmodified lines
1549 struct blk_plug_cb cb;1549 struct blk_plug_cb cb;
1550 struct btrfs_fs_info *info;1550 struct btrfs_fs_info *info;
1551 struct list_head rbio_list;1551 struct list_head rbio_list;
1552 struct work_struct work;
1553};1552};
1554 1553
1555/*1554/*
fs/btrfs/raid56.h+1−1
@@ -164,7 +164,7 @@ struct raid56_bio_trace_info { · 163 unmodified lines
164 u8 stripe_nr;164 u8 stripe_nr;
165};165};
166 166
167static inline int nr_data_stripes(const struct map_lookup *map)167static inline int nr_data_stripes(const struct btrfs_chunk_map *map)
168{168{
169 return map->num_stripes - btrfs_nr_parity_stripes(map->type);169 return map->num_stripes - btrfs_nr_parity_stripes(map->type);
170}170}
fs/btrfs/reflink.c+3−3
@@ -141,9 +141,9 @@ static int copy_inline_to_page(struct btrfs_inode *inode, · 140 unmodified lines
141 if (datal < block_size)141 if (datal < block_size)
142 memzero_page(page, datal, block_size - datal);142 memzero_page(page, datal, block_size - datal);
143 143
144 btrfs_page_set_uptodate(fs_info, page, file_offset, block_size);144 btrfs_folio_set_uptodate(fs_info, page_folio(page), file_offset, block_size);
145 btrfs_page_clear_checked(fs_info, page, file_offset, block_size);145 btrfs_folio_clear_checked(fs_info, page_folio(page), file_offset, block_size);
146 btrfs_page_set_dirty(fs_info, page, file_offset, block_size);146 btrfs_folio_set_dirty(fs_info, page_folio(page), file_offset, block_size);
147out_unlock:147out_unlock:
148 if (page) {148 if (page) {
149 unlock_page(page);149 unlock_page(page);
fs/btrfs/relocation.c+4−3
@@ -2895,7 +2895,7 @@ static noinline_for_stack int prealloc_file_extent_cluster( · 2894 unmodified lines
2895 * will re-read the whole page anyway.2895 * will re-read the whole page anyway.
2896 */2896 */
2897 if (page) {2897 if (page) {
2898 btrfs_subpage_clear_uptodate(fs_info, page, i_size,2898 btrfs_subpage_clear_uptodate(fs_info, page_folio(page), i_size,
2899 round_up(i_size, PAGE_SIZE) - i_size);2899 round_up(i_size, PAGE_SIZE) - i_size);
2900 unlock_page(page);2900 unlock_page(page);
2901 put_page(page);2901 put_page(page);
@@ -2951,7 +2951,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod · 49 unmodified lines
2951 em->len = end + 1 - start;2951 em->len = end + 1 - start;
2952 em->block_len = em->len;2952 em->block_len = em->len;
2953 em->block_start = block_start;2953 em->block_start = block_start;
2954 set_bit(EXTENT_FLAG_PINNED, &em->flags);2954 em->flags |= EXTENT_FLAG_PINNED;
2955 2955
2956 lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);2956 lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
2957 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, false);2957 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, false);
@@ -3070,7 +3070,8 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra, · 112 unmodified lines
3070 clamped_len);3070 clamped_len);
3071 goto release_page;3071 goto release_page;
3072 }3072 }
3073 btrfs_page_set_dirty(fs_info, page, clamped_start, clamped_len);3073 btrfs_folio_set_dirty(fs_info, page_folio(page),
3074 clamped_start, clamped_len);
3074 3075
3075 /*3076 /*
3076 * Set the boundary if it's inside the page.3077 * Set the boundary if it's inside the page.
fs/btrfs/scrub.c+26−37
@@ -43,7 +43,7 @@ struct scrub_ctx; · 42 unmodified lines
43/*43/*
44 * The following value only influences the performance.44 * The following value only influences the performance.
45 *45 *
46 * This detemines how many stripes would be submitted in one go,46 * This determines how many stripes would be submitted in one go,
47 * which is 512KiB (BTRFS_STRIPE_LEN * SCRUB_STRIPES_PER_GROUP).47 * which is 512KiB (BTRFS_STRIPE_LEN * SCRUB_STRIPES_PER_GROUP).
48 */48 */
49#define SCRUB_STRIPES_PER_GROUP 849#define SCRUB_STRIPES_PER_GROUP 8
@@ -192,7 +192,6 @@ struct scrub_ctx { · 142 unmodified lines
192 int cur_stripe;192 int cur_stripe;
193 atomic_t cancel_req;193 atomic_t cancel_req;
194 int readonly;194 int readonly;
195 int sectors_per_bio;
196 195
197 /* State of IO submission throttling affecting the associated device */196 /* State of IO submission throttling affecting the associated device */
198 ktime_t throttle_deadline;197 ktime_t throttle_deadline;
@@ -262,7 +261,7 @@ static int init_scrub_stripe(struct btrfs_fs_info *fs_info, · 63 unmodified lines
262 atomic_set(&stripe->pending_io, 0);261 atomic_set(&stripe->pending_io, 0);
263 spin_lock_init(&stripe->write_error_lock);262 spin_lock_init(&stripe->write_error_lock);
264 263
265 ret = btrfs_alloc_page_array(SCRUB_STRIPE_PAGES, stripe->pages);264 ret = btrfs_alloc_page_array(SCRUB_STRIPE_PAGES, stripe->pages, 0);
266 if (ret < 0)265 if (ret < 0)
267 goto error;266 goto error;
268 267
@@ -710,7 +709,7 @@ static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr) · 441 unmodified lines
710 /* Metadata, verify the full tree block. */709 /* Metadata, verify the full tree block. */
711 if (sector->is_metadata) {710 if (sector->is_metadata) {
712 /*711 /*
713 * Check if the tree block crosses the stripe boudary. If712 * Check if the tree block crosses the stripe boundary. If
714 * crossed the boundary, we cannot verify it but only give a713 * crossed the boundary, we cannot verify it but only give a
715 * warning.714 * warning.
716 *715 *
@@ -884,7 +883,7 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx, · 167 unmodified lines
884 /*883 /*
885 * Init needed infos for error reporting.884 * Init needed infos for error reporting.
886 *885 *
887 * Although our scrub_stripe infrastucture is mostly based on btrfs_submit_bio()886 * Although our scrub_stripe infrastructure is mostly based on btrfs_submit_bio()
888 * thus no need for dev/physical, error reporting still needs dev and physical.887 * thus no need for dev/physical, error reporting still needs dev and physical.
889 */888 */
890 if (!bitmap_empty(&stripe->init_error_bitmap, stripe->nr_sectors)) {889 if (!bitmap_empty(&stripe->init_error_bitmap, stripe->nr_sectors)) {
@@ -1280,7 +1279,7 @@ static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *d · 389 unmodified lines
1280 * return 0 if it is a data stripe, 1 means parity stripe.1279 * return 0 if it is a data stripe, 1 means parity stripe.
1281 */1280 */
1282static int get_raid56_logic_offset(u64 physical, int num,1281static int get_raid56_logic_offset(u64 physical, int num,
1283 struct map_lookup *map, u64 *offset,1282 struct btrfs_chunk_map *map, u64 *offset,
1284 u64 *stripe_start)1283 u64 *stripe_start)
1285{1284{
1286 int i;1285 int i;
@@ -1409,14 +1408,11 @@ search_forward: · 122 unmodified lines
1409 if (ret > 0)1408 if (ret > 0)
1410 break;1409 break;
1411next:1410next:
1412 path->slots[0]++;1411 ret = btrfs_next_item(extent_root, path);
1413 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {1412 if (ret) {
1414 ret = btrfs_next_leaf(extent_root, path);1413 /* Either no more items or a fatal error. */
1415 if (ret) {1414 btrfs_release_path(path);
1416 /* Either no more item or fatal error */1415 return ret;
1417 btrfs_release_path(path);
1418 return ret;
1419 }
1420 }1416 }
1421 }1417 }
1422 btrfs_release_path(path);1418 btrfs_release_path(path);
@@ -1816,7 +1812,7 @@ static int flush_scrub_stripes(struct scrub_ctx *sctx) · 393 unmodified lines
1816 if (sctx->is_dev_replace) {1812 if (sctx->is_dev_replace) {
1817 /*1813 /*
1818 * For dev-replace, if we know there is something wrong with1814 * For dev-replace, if we know there is something wrong with
1819 * metadata, we should immedately abort.1815 * metadata, we should immediately abort.
1820 */1816 */
1821 for (int i = 0; i < nr_stripes; i++) {1817 for (int i = 0; i < nr_stripes; i++) {
1822 if (stripe_has_metadata_error(&sctx->stripes[i])) {1818 if (stripe_has_metadata_error(&sctx->stripes[i])) {
@@ -1898,7 +1894,7 @@ static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group * · 75 unmodified lines
1898static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,1894static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
1899 struct btrfs_device *scrub_dev,1895 struct btrfs_device *scrub_dev,
1900 struct btrfs_block_group *bg,1896 struct btrfs_block_group *bg,
1901 struct map_lookup *map,1897 struct btrfs_chunk_map *map,
1902 u64 full_stripe_start)1898 u64 full_stripe_start)
1903{1899{
1904 DECLARE_COMPLETION_ONSTACK(io_done);1900 DECLARE_COMPLETION_ONSTACK(io_done);
@@ -2067,7 +2063,7 @@ out: · 162 unmodified lines
2067 */2063 */
2068static int scrub_simple_mirror(struct scrub_ctx *sctx,2064static int scrub_simple_mirror(struct scrub_ctx *sctx,
2069 struct btrfs_block_group *bg,2065 struct btrfs_block_group *bg,
2070 struct map_lookup *map,2066 struct btrfs_chunk_map *map,
2071 u64 logical_start, u64 logical_length,2067 u64 logical_start, u64 logical_length,
2072 struct btrfs_device *device,2068 struct btrfs_device *device,
2073 u64 physical, int mirror_num)2069 u64 physical, int mirror_num)
@@ -2128,7 +2124,7 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, · 54 unmodified lines
2128}2124}
2129 2125
2130/* Calculate the full stripe length for simple stripe based profiles */2126/* Calculate the full stripe length for simple stripe based profiles */
2131static u64 simple_stripe_full_stripe_len(const struct map_lookup *map)2127static u64 simple_stripe_full_stripe_len(const struct btrfs_chunk_map *map)
2132{2128{
2133 ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |2129 ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2134 BTRFS_BLOCK_GROUP_RAID10));2130 BTRFS_BLOCK_GROUP_RAID10));
@@ -2137,7 +2133,7 @@ static u64 simple_stripe_full_stripe_len(const struct map_lookup *map) · 2 unmodified lines
2137}2133}
2138 2134
2139/* Get the logical bytenr for the stripe */2135/* Get the logical bytenr for the stripe */
2140static u64 simple_stripe_get_logical(struct map_lookup *map,2136static u64 simple_stripe_get_logical(struct btrfs_chunk_map *map,
2141 struct btrfs_block_group *bg,2137 struct btrfs_block_group *bg,
2142 int stripe_index)2138 int stripe_index)
2143{2139{
@@ -2154,7 +2150,7 @@ static u64 simple_stripe_get_logical(struct map_lookup *map, · 10 unmodified lines
2154}2150}
2155 2151
2156/* Get the mirror number for the stripe */2152/* Get the mirror number for the stripe */
2157static int simple_stripe_mirror_num(struct map_lookup *map, int stripe_index)2153static int simple_stripe_mirror_num(struct btrfs_chunk_map *map, int stripe_index)
2158{2154{
2159 ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |2155 ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2160 BTRFS_BLOCK_GROUP_RAID10));2156 BTRFS_BLOCK_GROUP_RAID10));
@@ -2166,7 +2162,7 @@ static int simple_stripe_mirror_num(struct map_lookup *map, int stripe_index) · 5 unmodified lines
2166 2162
2167static int scrub_simple_stripe(struct scrub_ctx *sctx,2163static int scrub_simple_stripe(struct scrub_ctx *sctx,
2168 struct btrfs_block_group *bg,2164 struct btrfs_block_group *bg,
2169 struct map_lookup *map,2165 struct btrfs_chunk_map *map,
2170 struct btrfs_device *device,2166 struct btrfs_device *device,
2171 int stripe_index)2167 int stripe_index)
2172{2168{
@@ -2199,18 +2195,17 @@ static int scrub_simple_stripe(struct scrub_ctx *sctx, · 26 unmodified lines
2199 2195
2200static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,2196static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
2201 struct btrfs_block_group *bg,2197 struct btrfs_block_group *bg,
2202 struct extent_map *em,2198 struct btrfs_chunk_map *map,
2203 struct btrfs_device *scrub_dev,2199 struct btrfs_device *scrub_dev,
2204 int stripe_index)2200 int stripe_index)
2205{2201{
2206 struct btrfs_fs_info *fs_info = sctx->fs_info;2202 struct btrfs_fs_info *fs_info = sctx->fs_info;
2207 struct map_lookup *map = em->map_lookup;
2208 const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;2203 const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
2209 const u64 chunk_logical = bg->start;2204 const u64 chunk_logical = bg->start;
2210 int ret;2205 int ret;
2211 int ret2;2206 int ret2;
2212 u64 physical = map->stripes[stripe_index].physical;2207 u64 physical = map->stripes[stripe_index].physical;
2213 const u64 dev_stripe_len = btrfs_calc_stripe_length(em);2208 const u64 dev_stripe_len = btrfs_calc_stripe_length(map);
2214 const u64 physical_end = physical + dev_stripe_len;2209 const u64 physical_end = physical + dev_stripe_len;
2215 u64 logical;2210 u64 logical;
2216 u64 logic_end;2211 u64 logic_end;
@@ -2373,17 +2368,12 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, · 156 unmodified lines
2373 u64 dev_extent_len)2368 u64 dev_extent_len)
2374{2369{
2375 struct btrfs_fs_info *fs_info = sctx->fs_info;2370 struct btrfs_fs_info *fs_info = sctx->fs_info;
2376 struct extent_map_tree *map_tree = &fs_info->mapping_tree;2371 struct btrfs_chunk_map *map;
2377 struct map_lookup *map;
2378 struct extent_map *em;
2379 int i;2372 int i;
2380 int ret = 0;2373 int ret = 0;
2381 2374
2382 read_lock(&map_tree->lock);2375 map = btrfs_find_chunk_map(fs_info, bg->start, bg->length);
2383 em = lookup_extent_mapping(map_tree, bg->start, bg->length);2376 if (!map) {
2384 read_unlock(&map_tree->lock);
2385
2386 if (!em) {
2387 /*2377 /*
2388 * Might have been an unused block group deleted by the cleaner2378 * Might have been an unused block group deleted by the cleaner
2389 * kthread or relocation.2379 * kthread or relocation.
@@ -2395,22 +2385,21 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, · 5 unmodified lines
2395 2385
2396 return ret;2386 return ret;
2397 }2387 }
2398 if (em->start != bg->start)2388 if (map->start != bg->start)
2399 goto out;2389 goto out;
2400 if (em->len < dev_extent_len)2390 if (map->chunk_len < dev_extent_len)
2401 goto out;2391 goto out;
2402 2392
2403 map = em->map_lookup;
2404 for (i = 0; i < map->num_stripes; ++i) {2393 for (i = 0; i < map->num_stripes; ++i) {
2405 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&2394 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
2406 map->stripes[i].physical == dev_offset) {2395 map->stripes[i].physical == dev_offset) {
2407 ret = scrub_stripe(sctx, bg, em, scrub_dev, i);2396 ret = scrub_stripe(sctx, bg, map, scrub_dev, i);
2408 if (ret)2397 if (ret)
2409 goto out;2398 goto out;
2410 }2399 }
2411 }2400 }
2412out:2401out:
2413 free_extent_map(em);2402 btrfs_free_chunk_map(map);
2414 2403
2415 return ret;2404 return ret;
2416}2405}
fs/btrfs/subpage.c+192−181
@@ -64,7 +64,7 @@ · 63 unmodified lines
64 * This means a slightly higher tree locking latency.64 * This means a slightly higher tree locking latency.
65 */65 */
66 66
67bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page)67bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping)
68{68{
69 if (fs_info->sectorsize >= PAGE_SIZE)69 if (fs_info->sectorsize >= PAGE_SIZE)
70 return false;70 return false;
@@ -74,8 +74,7 @@ bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page) · 3 unmodified lines
74 * mapping. And if page->mapping->host is data inode, it's subpage.74 * mapping. And if page->mapping->host is data inode, it's subpage.
75 * As we have ruled our sectorsize >= PAGE_SIZE case already.75 * As we have ruled our sectorsize >= PAGE_SIZE case already.
76 */76 */
77 if (!page->mapping || !page->mapping->host ||77 if (!mapping || !mapping->host || is_data_inode(mapping->host))
78 is_data_inode(page->mapping->host))
79 return true;78 return true;
80 79
81 /*80 /*
@@ -116,7 +115,7 @@ void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sector · 34 unmodified lines
116}115}
117 116
118int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,117int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
119 struct page *page, enum btrfs_subpage_type type)118 struct folio *folio, enum btrfs_subpage_type type)
120{119{
121 struct btrfs_subpage *subpage;120 struct btrfs_subpage *subpage;
122 121
@@ -124,31 +123,30 @@ int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info, · 1 unmodified line
124 * We have cases like a dummy extent buffer page, which is not mapped123 * We have cases like a dummy extent buffer page, which is not mapped
125 * and doesn't need to be locked.124 * and doesn't need to be locked.
126 */125 */
127 if (page->mapping)126 if (folio->mapping)
128 ASSERT(PageLocked(page));127 ASSERT(folio_test_locked(folio));
129 128
130 /* Either not subpage, or the page already has private attached */129 /* Either not subpage, or the folio already has private attached. */
131 if (!btrfs_is_subpage(fs_info, page) || PagePrivate(page))130 if (!btrfs_is_subpage(fs_info, folio->mapping) || folio_test_private(folio))
132 return 0;131 return 0;
133 132
134 subpage = btrfs_alloc_subpage(fs_info, type);133 subpage = btrfs_alloc_subpage(fs_info, type);
135 if (IS_ERR(subpage))134 if (IS_ERR(subpage))
136 return PTR_ERR(subpage);135 return PTR_ERR(subpage);
137 136
138 attach_page_private(page, subpage);137 folio_attach_private(folio, subpage);
139 return 0;138 return 0;
140}139}
141 140
142void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,141void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio)
143 struct page *page)
144{142{
145 struct btrfs_subpage *subpage;143 struct btrfs_subpage *subpage;
146 144
147 /* Either not subpage, or already detached */145 /* Either not subpage, or the folio already has private attached. */
148 if (!btrfs_is_subpage(fs_info, page) || !PagePrivate(page))146 if (!btrfs_is_subpage(fs_info, folio->mapping) || !folio_test_private(folio))
149 return;147 return;
150 148
151 subpage = detach_page_private(page);149 subpage = folio_detach_private(folio);
152 ASSERT(subpage);150 ASSERT(subpage);
153 btrfs_free_subpage(subpage);151 btrfs_free_subpage(subpage);
154}152}
@@ -188,77 +186,78 @@ void btrfs_free_subpage(struct btrfs_subpage *subpage) · 33 unmodified lines
188 * This is important for eb allocation, to prevent race with last eb freeing186 * This is important for eb allocation, to prevent race with last eb freeing
189 * of the same page.187 * of the same page.
190 * With the eb_refs increased before the eb inserted into radix tree,188 * With the eb_refs increased before the eb inserted into radix tree,
191 * detach_extent_buffer_page() won't detach the page private while we're still189 * detach_extent_buffer_page() won't detach the folio private while we're still
192 * allocating the extent buffer.190 * allocating the extent buffer.
193 */191 */
194void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,192void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio)
195 struct page *page)
196{193{
197 struct btrfs_subpage *subpage;194 struct btrfs_subpage *subpage;
198 195
199 if (!btrfs_is_subpage(fs_info, page))196 if (!btrfs_is_subpage(fs_info, folio->mapping))
200 return;197 return;
201 198
202 ASSERT(PagePrivate(page) && page->mapping);199 ASSERT(folio_test_private(folio) && folio->mapping);
203 lockdep_assert_held(&page->mapping->i_private_lock);200 lockdep_assert_held(&folio->mapping->i_private_lock);
204 201
205 subpage = (struct btrfs_subpage *)page->private;202 subpage = folio_get_private(folio);
206 atomic_inc(&subpage->eb_refs);203 atomic_inc(&subpage->eb_refs);
207}204}
208 205
209void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,206void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio)
210 struct page *page)
211{207{
212 struct btrfs_subpage *subpage;208 struct btrfs_subpage *subpage;
213 209
214 if (!btrfs_is_subpage(fs_info, page))210 if (!btrfs_is_subpage(fs_info, folio->mapping))
215 return;211 return;
216 212
217 ASSERT(PagePrivate(page) && page->mapping);213 ASSERT(folio_test_private(folio) && folio->mapping);
218 lockdep_assert_held(&page->mapping->i_private_lock);214 lockdep_assert_held(&folio->mapping->i_private_lock);
219 215
220 subpage = (struct btrfs_subpage *)page->private;216 subpage = folio_get_private(folio);
221 ASSERT(atomic_read(&subpage->eb_refs));217 ASSERT(atomic_read(&subpage->eb_refs));
222 atomic_dec(&subpage->eb_refs);218 atomic_dec(&subpage->eb_refs);
223}219}
224 220
225static void btrfs_subpage_assert(const struct btrfs_fs_info *fs_info,221static void btrfs_subpage_assert(const struct btrfs_fs_info *fs_info,
226 struct page *page, u64 start, u32 len)222 struct folio *folio, u64 start, u32 len)
227{223{
224 /* For subpage support, the folio must be single page. */
225 ASSERT(folio_order(folio) == 0);
226
228 /* Basic checks */227 /* Basic checks */
229 ASSERT(PagePrivate(page) && page->private);228 ASSERT(folio_test_private(folio) && folio_get_private(folio));
230 ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&229 ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
231 IS_ALIGNED(len, fs_info->sectorsize));230 IS_ALIGNED(len, fs_info->sectorsize));
232 /*231 /*
233 * The range check only works for mapped page, we can still have232 * The range check only works for mapped page, we can still have
234 * unmapped page like dummy extent buffer pages.233 * unmapped page like dummy extent buffer pages.
235 */234 */
236 if (page->mapping)235 if (folio->mapping)
237 ASSERT(page_offset(page) <= start &&236 ASSERT(folio_pos(folio) <= start &&
238 start + len <= page_offset(page) + PAGE_SIZE);237 start + len <= folio_pos(folio) + PAGE_SIZE);
239}238}
240 239
241void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,240void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
242 struct page *page, u64 start, u32 len)241 struct folio *folio, u64 start, u32 len)
243{242{
244 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;243 struct btrfs_subpage *subpage = folio_get_private(folio);
245 const int nbits = len >> fs_info->sectorsize_bits;244 const int nbits = len >> fs_info->sectorsize_bits;
246 245
247 btrfs_subpage_assert(fs_info, page, start, len);246 btrfs_subpage_assert(fs_info, folio, start, len);
248 247
249 atomic_add(nbits, &subpage->readers);248 atomic_add(nbits, &subpage->readers);
250}249}
251 250
252void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,251void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
253 struct page *page, u64 start, u32 len)252 struct folio *folio, u64 start, u32 len)
254{253{
255 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;254 struct btrfs_subpage *subpage = folio_get_private(folio);
256 const int nbits = len >> fs_info->sectorsize_bits;255 const int nbits = len >> fs_info->sectorsize_bits;
257 bool is_data;256 bool is_data;
258 bool last;257 bool last;
259 258
260 btrfs_subpage_assert(fs_info, page, start, len);259 btrfs_subpage_assert(fs_info, folio, start, len);
261 is_data = is_data_inode(page->mapping->host);260 is_data = is_data_inode(folio->mapping->host);
262 ASSERT(atomic_read(&subpage->readers) >= nbits);261 ASSERT(atomic_read(&subpage->readers) >= nbits);
263 last = atomic_sub_and_test(nbits, &subpage->readers);262 last = atomic_sub_and_test(nbits, &subpage->readers);
264 263
@@ -270,35 +269,35 @@ void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info, · 5 unmodified lines
270 * As we want the atomic_sub_and_test() to be always executed.269 * As we want the atomic_sub_and_test() to be always executed.
271 */270 */
272 if (is_data && last)271 if (is_data && last)
273 unlock_page(page);272 folio_unlock(folio);
274}273}
275 274
276static void btrfs_subpage_clamp_range(struct page *page, u64 *start, u32 *len)275static void btrfs_subpage_clamp_range(struct folio *folio, u64 *start, u32 *len)
277{276{
278 u64 orig_start = *start;277 u64 orig_start = *start;
279 u32 orig_len = *len;278 u32 orig_len = *len;
280 279
281 *start = max_t(u64, page_offset(page), orig_start);280 *start = max_t(u64, folio_pos(folio), orig_start);
282 /*281 /*
283 * For certain call sites like btrfs_drop_pages(), we may have pages282 * For certain call sites like btrfs_drop_pages(), we may have pages
284 * beyond the target range. In that case, just set @len to 0, subpage283 * beyond the target range. In that case, just set @len to 0, subpage
285 * helpers can handle @len == 0 without any problem.284 * helpers can handle @len == 0 without any problem.
286 */285 */
287 if (page_offset(page) >= orig_start + orig_len)286 if (folio_pos(folio) >= orig_start + orig_len)
288 *len = 0;287 *len = 0;
289 else288 else
290 *len = min_t(u64, page_offset(page) + PAGE_SIZE,289 *len = min_t(u64, folio_pos(folio) + PAGE_SIZE,
291 orig_start + orig_len) - *start;290 orig_start + orig_len) - *start;
292}291}
293 292
294void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,293void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
295 struct page *page, u64 start, u32 len)294 struct folio *folio, u64 start, u32 len)
296{295{
297 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;296 struct btrfs_subpage *subpage = folio_get_private(folio);
298 const int nbits = (len >> fs_info->sectorsize_bits);297 const int nbits = (len >> fs_info->sectorsize_bits);
299 int ret;298 int ret;
300 299
301 btrfs_subpage_assert(fs_info, page, start, len);300 btrfs_subpage_assert(fs_info, folio, start, len);
302 301
303 ASSERT(atomic_read(&subpage->readers) == 0);302 ASSERT(atomic_read(&subpage->readers) == 0);
304 ret = atomic_add_return(nbits, &subpage->writers);303 ret = atomic_add_return(nbits, &subpage->writers);
@@ -306,12 +305,12 @@ void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info, · 1 unmodified line
306}305}
307 306
308bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,307bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
309 struct page *page, u64 start, u32 len)308 struct folio *folio, u64 start, u32 len)
310{309{
311 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;310 struct btrfs_subpage *subpage = folio_get_private(folio);
312 const int nbits = (len >> fs_info->sectorsize_bits);311 const int nbits = (len >> fs_info->sectorsize_bits);
313 312
314 btrfs_subpage_assert(fs_info, page, start, len);313 btrfs_subpage_assert(fs_info, folio, start, len);
315 314
316 /*315 /*
317 * We have call sites passing @lock_page into316 * We have call sites passing @lock_page into
@@ -328,7 +327,7 @@ bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info, · 10 unmodified lines
328}327}
329 328
330/*329/*
331 * Lock a page for delalloc page writeback.330 * Lock a folio for delalloc page writeback.
332 *331 *
333 * Return -EAGAIN if the page is not properly initialized.332 * Return -EAGAIN if the page is not properly initialized.
334 * Return 0 with the page locked, and writer counter updated.333 * Return 0 with the page locked, and writer counter updated.
@@ -337,38 +336,40 @@ bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info, · 2 unmodified lines
337 * it's really the correct page, as the caller is using336 * it's really the correct page, as the caller is using
338 * filemap_get_folios_contig(), which can race with page invalidating.337 * filemap_get_folios_contig(), which can race with page invalidating.
339 */338 */
340int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,339int btrfs_folio_start_writer_lock(const struct btrfs_fs_info *fs_info,
341 struct page *page, u64 start, u32 len)340 struct folio *folio, u64 start, u32 len)
342{341{
343 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) {342 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping)) {
344 lock_page(page);343 folio_lock(folio);
345 return 0;344 return 0;
346 }345 }
347 lock_page(page);346 folio_lock(folio);
348 if (!PagePrivate(page) || !page->private) {347 if (!folio_test_private(folio) || !folio_get_private(folio)) {
349 unlock_page(page);348 folio_unlock(folio);
350 return -EAGAIN;349 return -EAGAIN;
351 }350 }
352 btrfs_subpage_clamp_range(page, &start, &len);351 btrfs_subpage_clamp_range(folio, &start, &len);
353 btrfs_subpage_start_writer(fs_info, page, start, len);352 btrfs_subpage_start_writer(fs_info, folio, start, len);
354 return 0;353 return 0;
355}354}
356 355
357void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,356void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
358 struct page *page, u64 start, u32 len)357 struct folio *folio, u64 start, u32 len)
359{358{
360 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page))359 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping)) {
361 return unlock_page(page);360 folio_unlock(folio);
362 btrfs_subpage_clamp_range(page, &start, &len);361 return;
363 if (btrfs_subpage_end_and_test_writer(fs_info, page, start, len))362 }
364 unlock_page(page);363 btrfs_subpage_clamp_range(folio, &start, &len);
364 if (btrfs_subpage_end_and_test_writer(fs_info, folio, start, len))
365 folio_unlock(folio);
365}366}
366 367
367#define subpage_calc_start_bit(fs_info, page, name, start, len) \368#define subpage_calc_start_bit(fs_info, folio, name, start, len) \
368({ \369({ \
369 unsigned int start_bit; \370 unsigned int start_bit; \
370 \371 \
371 btrfs_subpage_assert(fs_info, page, start, len); \372 btrfs_subpage_assert(fs_info, folio, start, len); \
372 start_bit = offset_in_page(start) >> fs_info->sectorsize_bits; \373 start_bit = offset_in_page(start) >> fs_info->sectorsize_bits; \
373 start_bit += fs_info->subpage_info->name##_offset; \374 start_bit += fs_info->subpage_info->name##_offset; \
374 start_bit; \375 start_bit; \
@@ -385,46 +386,46 @@ void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, · 10 unmodified lines
385 fs_info->subpage_info->bitmap_nr_bits)386 fs_info->subpage_info->bitmap_nr_bits)
386 387
387void btrfs_subpage_set_uptodate(const struct btrfs_fs_info *fs_info,388void btrfs_subpage_set_uptodate(const struct btrfs_fs_info *fs_info,
388 struct page *page, u64 start, u32 len)389 struct folio *folio, u64 start, u32 len)
389{390{
390 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;391 struct btrfs_subpage *subpage = folio_get_private(folio);
391 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,392 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
392 uptodate, start, len);393 uptodate, start, len);
393 unsigned long flags;394 unsigned long flags;
394 395
395 spin_lock_irqsave(&subpage->lock, flags);396 spin_lock_irqsave(&subpage->lock, flags);
396 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);397 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
397 if (subpage_test_bitmap_all_set(fs_info, subpage, uptodate))398 if (subpage_test_bitmap_all_set(fs_info, subpage, uptodate))
398 SetPageUptodate(page);399 folio_mark_uptodate(folio);
399 spin_unlock_irqrestore(&subpage->lock, flags);400 spin_unlock_irqrestore(&subpage->lock, flags);
400}401}
401 402
402void btrfs_subpage_clear_uptodate(const struct btrfs_fs_info *fs_info,403void btrfs_subpage_clear_uptodate(const struct btrfs_fs_info *fs_info,
403 struct page *page, u64 start, u32 len)404 struct folio *folio, u64 start, u32 len)
404{405{
405 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;406 struct btrfs_subpage *subpage = folio_get_private(folio);
406 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,407 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
407 uptodate, start, len);408 uptodate, start, len);
408 unsigned long flags;409 unsigned long flags;
409 410
410 spin_lock_irqsave(&subpage->lock, flags);411 spin_lock_irqsave(&subpage->lock, flags);
411 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);412 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
412 ClearPageUptodate(page);413 folio_clear_uptodate(folio);
413 spin_unlock_irqrestore(&subpage->lock, flags);414 spin_unlock_irqrestore(&subpage->lock, flags);
414}415}
415 416
416void btrfs_subpage_set_dirty(const struct btrfs_fs_info *fs_info,417void btrfs_subpage_set_dirty(const struct btrfs_fs_info *fs_info,
417 struct page *page, u64 start, u32 len)418 struct folio *folio, u64 start, u32 len)
418{419{
419 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;420 struct btrfs_subpage *subpage = folio_get_private(folio);
420 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,421 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
421 dirty, start, len);422 dirty, start, len);
422 unsigned long flags;423 unsigned long flags;
423 424
424 spin_lock_irqsave(&subpage->lock, flags);425 spin_lock_irqsave(&subpage->lock, flags);
425 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);426 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
426 spin_unlock_irqrestore(&subpage->lock, flags);427 spin_unlock_irqrestore(&subpage->lock, flags);
427 set_page_dirty(page);428 folio_mark_dirty(folio);
428}429}
429 430
430/*431/*
@@ -438,10 +439,10 @@ void btrfs_subpage_set_dirty(const struct btrfs_fs_info *fs_info, · 7 unmodified lines
438 * extra handling for tree blocks.439 * extra handling for tree blocks.
439 */440 */
440bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,441bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
441 struct page *page, u64 start, u32 len)442 struct folio *folio, u64 start, u32 len)
442{443{
443 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;444 struct btrfs_subpage *subpage = folio_get_private(folio);
444 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,445 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
445 dirty, start, len);446 dirty, start, len);
446 unsigned long flags;447 unsigned long flags;
447 bool last = false;448 bool last = false;
@@ -455,101 +456,101 @@ bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info, · 7 unmodified lines
455}456}
456 457
457void btrfs_subpage_clear_dirty(const struct btrfs_fs_info *fs_info,458void btrfs_subpage_clear_dirty(const struct btrfs_fs_info *fs_info,
458 struct page *page, u64 start, u32 len)459 struct folio *folio, u64 start, u32 len)
459{460{
460 bool last;461 bool last;
461 462
462 last = btrfs_subpage_clear_and_test_dirty(fs_info, page, start, len);463 last = btrfs_subpage_clear_and_test_dirty(fs_info, folio, start, len);
463 if (last)464 if (last)
464 clear_page_dirty_for_io(page);465 folio_clear_dirty_for_io(folio);
465}466}
466 467
467void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,468void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,
468 struct page *page, u64 start, u32 len)469 struct folio *folio, u64 start, u32 len)
469{470{
470 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;471 struct btrfs_subpage *subpage = folio_get_private(folio);
471 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,472 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
472 writeback, start, len);473 writeback, start, len);
473 unsigned long flags;474 unsigned long flags;
474 475
475 spin_lock_irqsave(&subpage->lock, flags);476 spin_lock_irqsave(&subpage->lock, flags);
476 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);477 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
477 set_page_writeback(page);478 folio_start_writeback(folio);
478 spin_unlock_irqrestore(&subpage->lock, flags);479 spin_unlock_irqrestore(&subpage->lock, flags);
479}480}
480 481
481void btrfs_subpage_clear_writeback(const struct btrfs_fs_info *fs_info,482void btrfs_subpage_clear_writeback(const struct btrfs_fs_info *fs_info,
482 struct page *page, u64 start, u32 len)483 struct folio *folio, u64 start, u32 len)
483{484{
484 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;485 struct btrfs_subpage *subpage = folio_get_private(folio);
485 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,486 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
486 writeback, start, len);487 writeback, start, len);
487 unsigned long flags;488 unsigned long flags;
488 489
489 spin_lock_irqsave(&subpage->lock, flags);490 spin_lock_irqsave(&subpage->lock, flags);
490 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);491 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
491 if (subpage_test_bitmap_all_zero(fs_info, subpage, writeback)) {492 if (subpage_test_bitmap_all_zero(fs_info, subpage, writeback)) {
492 ASSERT(PageWriteback(page));493 ASSERT(folio_test_writeback(folio));
493 end_page_writeback(page);494 folio_end_writeback(folio);
494 }495 }
495 spin_unlock_irqrestore(&subpage->lock, flags);496 spin_unlock_irqrestore(&subpage->lock, flags);
496}497}
497 498
498void btrfs_subpage_set_ordered(const struct btrfs_fs_info *fs_info,499void btrfs_subpage_set_ordered(const struct btrfs_fs_info *fs_info,
499 struct page *page, u64 start, u32 len)500 struct folio *folio, u64 start, u32 len)
500{501{
501 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;502 struct btrfs_subpage *subpage = folio_get_private(folio);
502 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,503 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
503 ordered, start, len);504 ordered, start, len);
504 unsigned long flags;505 unsigned long flags;
505 506
506 spin_lock_irqsave(&subpage->lock, flags);507 spin_lock_irqsave(&subpage->lock, flags);
507 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);508 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
508 SetPageOrdered(page);509 folio_set_ordered(folio);
509 spin_unlock_irqrestore(&subpage->lock, flags);510 spin_unlock_irqrestore(&subpage->lock, flags);
510}511}
511 512
512void btrfs_subpage_clear_ordered(const struct btrfs_fs_info *fs_info,513void btrfs_subpage_clear_ordered(const struct btrfs_fs_info *fs_info,
513 struct page *page, u64 start, u32 len)514 struct folio *folio, u64 start, u32 len)
514{515{
515 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;516 struct btrfs_subpage *subpage = folio_get_private(folio);
516 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,517 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
517 ordered, start, len);518 ordered, start, len);
518 unsigned long flags;519 unsigned long flags;
519 520
520 spin_lock_irqsave(&subpage->lock, flags);521 spin_lock_irqsave(&subpage->lock, flags);
521 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);522 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
522 if (subpage_test_bitmap_all_zero(fs_info, subpage, ordered))523 if (subpage_test_bitmap_all_zero(fs_info, subpage, ordered))
523 ClearPageOrdered(page);524 folio_clear_ordered(folio);
524 spin_unlock_irqrestore(&subpage->lock, flags);525 spin_unlock_irqrestore(&subpage->lock, flags);
525}526}
526 527
527void btrfs_subpage_set_checked(const struct btrfs_fs_info *fs_info,528void btrfs_subpage_set_checked(const struct btrfs_fs_info *fs_info,
528 struct page *page, u64 start, u32 len)529 struct folio *folio, u64 start, u32 len)
529{530{
530 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;531 struct btrfs_subpage *subpage = folio_get_private(folio);
531 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,532 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
532 checked, start, len);533 checked, start, len);
533 unsigned long flags;534 unsigned long flags;
534 535
535 spin_lock_irqsave(&subpage->lock, flags);536 spin_lock_irqsave(&subpage->lock, flags);
536 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);537 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
537 if (subpage_test_bitmap_all_set(fs_info, subpage, checked))538 if (subpage_test_bitmap_all_set(fs_info, subpage, checked))
538 SetPageChecked(page);539 folio_set_checked(folio);
539 spin_unlock_irqrestore(&subpage->lock, flags);540 spin_unlock_irqrestore(&subpage->lock, flags);
540}541}
541 542
542void btrfs_subpage_clear_checked(const struct btrfs_fs_info *fs_info,543void btrfs_subpage_clear_checked(const struct btrfs_fs_info *fs_info,
543 struct page *page, u64 start, u32 len)544 struct folio *folio, u64 start, u32 len)
544{545{
545 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;546 struct btrfs_subpage *subpage = folio_get_private(folio);
546 unsigned int start_bit = subpage_calc_start_bit(fs_info, page,547 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
547 checked, start, len);548 checked, start, len);
548 unsigned long flags;549 unsigned long flags;
549 550
550 spin_lock_irqsave(&subpage->lock, flags);551 spin_lock_irqsave(&subpage->lock, flags);
551 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);552 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
552 ClearPageChecked(page);553 folio_clear_checked(folio);
553 spin_unlock_irqrestore(&subpage->lock, flags);554 spin_unlock_irqrestore(&subpage->lock, flags);
554}555}
555 556
@@ -559,10 +560,10 @@ void btrfs_subpage_clear_checked(const struct btrfs_fs_info *fs_info, · 3 unmodified lines
559 */560 */
560#define IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(name) \561#define IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(name) \
561bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \562bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
562 struct page *page, u64 start, u32 len) \563 struct folio *folio, u64 start, u32 len) \
563{ \564{ \
564 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; \565 struct btrfs_subpage *subpage = folio_get_private(folio); \
565 unsigned int start_bit = subpage_calc_start_bit(fs_info, page, \566 unsigned int start_bit = subpage_calc_start_bit(fs_info, folio, \
566 name, start, len); \567 name, start, len); \
567 unsigned long flags; \568 unsigned long flags; \
568 bool ret; \569 bool ret; \
@@ -584,88 +585,94 @@ IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(checked); · 15 unmodified lines
584 * in. We only test sectorsize == PAGE_SIZE cases so far, thus we can fall585 * in. We only test sectorsize == PAGE_SIZE cases so far, thus we can fall
585 * back to regular sectorsize branch.586 * back to regular sectorsize branch.
586 */587 */
587#define IMPLEMENT_BTRFS_PAGE_OPS(name, set_page_func, clear_page_func, \588#define IMPLEMENT_BTRFS_PAGE_OPS(name, folio_set_func, \
588 test_page_func) \589 folio_clear_func, folio_test_func) \
589void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \590void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
590 struct page *page, u64 start, u32 len) \591 struct folio *folio, u64 start, u32 len) \
591{ \592{ \
592 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \593 if (unlikely(!fs_info) || \
593 set_page_func(page); \594 !btrfs_is_subpage(fs_info, folio->mapping)) { \
595 folio_set_func(folio); \
594 return; \596 return; \
595 } \597 } \
596 btrfs_subpage_set_##name(fs_info, page, start, len); \598 btrfs_subpage_set_##name(fs_info, folio, start, len); \
597} \599} \
598void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \600void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
599 struct page *page, u64 start, u32 len) \601 struct folio *folio, u64 start, u32 len) \
600{ \602{ \
601 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \603 if (unlikely(!fs_info) || \
602 clear_page_func(page); \604 !btrfs_is_subpage(fs_info, folio->mapping)) { \
605 folio_clear_func(folio); \
603 return; \606 return; \
604 } \607 } \
605 btrfs_subpage_clear_##name(fs_info, page, start, len); \608 btrfs_subpage_clear_##name(fs_info, folio, start, len); \
606} \609} \
607bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \610bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
608 struct page *page, u64 start, u32 len) \611 struct folio *folio, u64 start, u32 len) \
609{ \612{ \
610 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) \613 if (unlikely(!fs_info) || \
611 return test_page_func(page); \614 !btrfs_is_subpage(fs_info, folio->mapping)) \
612 return btrfs_subpage_test_##name(fs_info, page, start, len); \615 return folio_test_func(folio); \
616 return btrfs_subpage_test_##name(fs_info, folio, start, len); \
613} \617} \
614void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \618void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
615 struct page *page, u64 start, u32 len) \619 struct folio *folio, u64 start, u32 len) \
616{ \620{ \
617 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \621 if (unlikely(!fs_info) || \
618 set_page_func(page); \622 !btrfs_is_subpage(fs_info, folio->mapping)) { \
623 folio_set_func(folio); \
619 return; \624 return; \
620 } \625 } \
621 btrfs_subpage_clamp_range(page, &start, &len); \626 btrfs_subpage_clamp_range(folio, &start, &len); \
622 btrfs_subpage_set_##name(fs_info, page, start, len); \627 btrfs_subpage_set_##name(fs_info, folio, start, len); \
623} \628} \
624void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \629void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
625 struct page *page, u64 start, u32 len) \630 struct folio *folio, u64 start, u32 len) \
626{ \631{ \
627 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \632 if (unlikely(!fs_info) || \
628 clear_page_func(page); \633 !btrfs_is_subpage(fs_info, folio->mapping)) { \
634 folio_clear_func(folio); \
629 return; \635 return; \
630 } \636 } \
631 btrfs_subpage_clamp_range(page, &start, &len); \637 btrfs_subpage_clamp_range(folio, &start, &len); \
632 btrfs_subpage_clear_##name(fs_info, page, start, len); \638 btrfs_subpage_clear_##name(fs_info, folio, start, len); \
633} \639} \
634bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \640bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
635 struct page *page, u64 start, u32 len) \641 struct folio *folio, u64 start, u32 len) \
636{ \642{ \
637 if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) \643 if (unlikely(!fs_info) || \
638 return test_page_func(page); \644 !btrfs_is_subpage(fs_info, folio->mapping)) \
639 btrfs_subpage_clamp_range(page, &start, &len); \645 return folio_test_func(folio); \
640 return btrfs_subpage_test_##name(fs_info, page, start, len); \646 btrfs_subpage_clamp_range(folio, &start, &len); \
641}647 return btrfs_subpage_test_##name(fs_info, folio, start, len); \
642IMPLEMENT_BTRFS_PAGE_OPS(uptodate, SetPageUptodate, ClearPageUptodate,648}
643 PageUptodate);649IMPLEMENT_BTRFS_PAGE_OPS(uptodate, folio_mark_uptodate, folio_clear_uptodate,
644IMPLEMENT_BTRFS_PAGE_OPS(dirty, set_page_dirty, clear_page_dirty_for_io,650 folio_test_uptodate);
645 PageDirty);651IMPLEMENT_BTRFS_PAGE_OPS(dirty, folio_mark_dirty, folio_clear_dirty_for_io,
646IMPLEMENT_BTRFS_PAGE_OPS(writeback, set_page_writeback, end_page_writeback,652 folio_test_dirty);
647 PageWriteback);653IMPLEMENT_BTRFS_PAGE_OPS(writeback, folio_start_writeback, folio_end_writeback,
648IMPLEMENT_BTRFS_PAGE_OPS(ordered, SetPageOrdered, ClearPageOrdered,654 folio_test_writeback);
649 PageOrdered);655IMPLEMENT_BTRFS_PAGE_OPS(ordered, folio_set_ordered, folio_clear_ordered,
650IMPLEMENT_BTRFS_PAGE_OPS(checked, SetPageChecked, ClearPageChecked, PageChecked);656 folio_test_ordered);
657IMPLEMENT_BTRFS_PAGE_OPS(checked, folio_set_checked, folio_clear_checked,
658 folio_test_checked);
651 659
652/*660/*
653 * Make sure not only the page dirty bit is cleared, but also subpage dirty bit661 * Make sure not only the page dirty bit is cleared, but also subpage dirty bit
654 * is cleared.662 * is cleared.
655 */663 */
656void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,664void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info, struct folio *folio)
657 struct page *page)
658{665{
659 struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;666 struct btrfs_subpage *subpage = folio_get_private(folio);
660 667
661 if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))668 if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
662 return;669 return;
663 670
664 ASSERT(!PageDirty(page));671 ASSERT(!folio_test_dirty(folio));
665 if (!btrfs_is_subpage(fs_info, page))672 if (!btrfs_is_subpage(fs_info, folio->mapping))
666 return;673 return;
667 674
668 ASSERT(PagePrivate(page) && page->private);675 ASSERT(folio_test_private(folio) && folio_get_private(folio));
669 ASSERT(subpage_test_bitmap_all_zero(fs_info, subpage, dirty));676 ASSERT(subpage_test_bitmap_all_zero(fs_info, subpage, dirty));
670}677}
671 678
@@ -684,18 +691,20 @@ void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info, · 12 unmodified lines
684 * extent_write_locked_range().691 * extent_write_locked_range().
685 * In this case, we have to call subpage helper to handle the case.692 * In this case, we have to call subpage helper to handle the case.
686 */693 */
687void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page,694void btrfs_folio_unlock_writer(struct btrfs_fs_info *fs_info,
688 u64 start, u32 len)695 struct folio *folio, u64 start, u32 len)
689{696{
690 struct btrfs_subpage *subpage;697 struct btrfs_subpage *subpage;
691 698
692 ASSERT(PageLocked(page));699 ASSERT(folio_test_locked(folio));
693 /* For non-subpage case, we just unlock the page */700 /* For non-subpage case, we just unlock the page */
694 if (!btrfs_is_subpage(fs_info, page))701 if (!btrfs_is_subpage(fs_info, folio->mapping)) {
695 return unlock_page(page);702 folio_unlock(folio);
703 return;
704 }
696 705
697 ASSERT(PagePrivate(page) && page->private);706 ASSERT(folio_test_private(folio) && folio_get_private(folio));
698 subpage = (struct btrfs_subpage *)page->private;707 subpage = folio_get_private(folio);
699 708
700 /*709 /*
701 * For subpage case, there are two types of locked page. With or710 * For subpage case, there are two types of locked page. With or
@@ -704,12 +713,14 @@ void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page, · 2 unmodified lines
704 * Since we own the page lock, no one else could touch subpage::writers713 * Since we own the page lock, no one else could touch subpage::writers
705 * and we are safe to do several atomic operations without spinlock.714 * and we are safe to do several atomic operations without spinlock.
706 */715 */
707 if (atomic_read(&subpage->writers) == 0)716 if (atomic_read(&subpage->writers) == 0) {
708 /* No writers, locked by plain lock_page() */717 /* No writers, locked by plain lock_page() */
709 return unlock_page(page);718 folio_unlock(folio);
719 return;
720 }
710 721
711 /* Have writers, use proper subpage helper to end it */722 /* Have writers, use proper subpage helper to end it */
712 btrfs_page_end_writer_lock(fs_info, page, start, len);723 btrfs_folio_end_writer_lock(fs_info, folio, start, len);
713}724}
714 725
715#define GET_SUBPAGE_BITMAP(subpage, subpage_info, name, dst) \726#define GET_SUBPAGE_BITMAP(subpage, subpage_info, name, dst) \
@@ -717,7 +728,7 @@ void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page, · 1 unmodified line
717 subpage_info->name##_offset, subpage_info->bitmap_nr_bits)728 subpage_info->name##_offset, subpage_info->bitmap_nr_bits)
718 729
719void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,730void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
720 struct page *page, u64 start, u32 len)731 struct folio *folio, u64 start, u32 len)
721{732{
722 struct btrfs_subpage_info *subpage_info = fs_info->subpage_info;733 struct btrfs_subpage_info *subpage_info = fs_info->subpage_info;
723 struct btrfs_subpage *subpage;734 struct btrfs_subpage *subpage;
@@ -729,9 +740,9 @@ void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info, · 5 unmodified lines
729 unsigned long checked_bitmap;740 unsigned long checked_bitmap;
730 unsigned long flags;741 unsigned long flags;
731 742
732 ASSERT(PagePrivate(page) && page->private);743 ASSERT(folio_test_private(folio) && folio_get_private(folio));
733 ASSERT(subpage_info);744 ASSERT(subpage_info);
734 subpage = (struct btrfs_subpage *)page->private;745 subpage = folio_get_private(folio);
735 746
736 spin_lock_irqsave(&subpage->lock, flags);747 spin_lock_irqsave(&subpage->lock, flags);
737 GET_SUBPAGE_BITMAP(subpage, subpage_info, uptodate, &uptodate_bitmap);748 GET_SUBPAGE_BITMAP(subpage, subpage_info, uptodate, &uptodate_bitmap);
@@ -741,10 +752,10 @@ void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info, · 3 unmodified lines
741 GET_SUBPAGE_BITMAP(subpage, subpage_info, checked, &checked_bitmap);752 GET_SUBPAGE_BITMAP(subpage, subpage_info, checked, &checked_bitmap);
742 spin_unlock_irqrestore(&subpage->lock, flags);753 spin_unlock_irqrestore(&subpage->lock, flags);
743 754
744 dump_page(page, "btrfs subpage dump");755 dump_page(folio_page(folio, 0), "btrfs subpage dump");
745 btrfs_warn(fs_info,756 btrfs_warn(fs_info,
746"start=%llu len=%u page=%llu, bitmaps uptodate=%*pbl error=%*pbl dirty=%*pbl writeback=%*pbl ordered=%*pbl checked=%*pbl",757"start=%llu len=%u page=%llu, bitmaps uptodate=%*pbl error=%*pbl dirty=%*pbl writeback=%*pbl ordered=%*pbl checked=%*pbl",
747 start, len, page_offset(page),758 start, len, folio_pos(folio),
748 subpage_info->bitmap_nr_bits, &uptodate_bitmap,759 subpage_info->bitmap_nr_bits, &uptodate_bitmap,
749 subpage_info->bitmap_nr_bits, &error_bitmap,760 subpage_info->bitmap_nr_bits, &error_bitmap,
750 subpage_info->bitmap_nr_bits, &dirty_bitmap,761 subpage_info->bitmap_nr_bits, &dirty_bitmap,
fs/btrfs/subpage.h+39−43
@@ -73,71 +73,68 @@ enum btrfs_subpage_type { · 72 unmodified lines
73 BTRFS_SUBPAGE_DATA,73 BTRFS_SUBPAGE_DATA,
74};74};
75 75
76bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page);76bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);
77 77
78void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sectorsize);78void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sectorsize);
79int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,79int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
80 struct page *page, enum btrfs_subpage_type type);80 struct folio *folio, enum btrfs_subpage_type type);
81void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,81void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio);
82 struct page *page);
83 82
84/* Allocate additional data where page represents more than one sector */83/* Allocate additional data where page represents more than one sector */
85struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,84struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
86 enum btrfs_subpage_type type);85 enum btrfs_subpage_type type);
87void btrfs_free_subpage(struct btrfs_subpage *subpage);86void btrfs_free_subpage(struct btrfs_subpage *subpage);
88 87
89void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,88void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
90 struct page *page);89void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
91void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
92 struct page *page);
93 90
94void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,91void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
95 struct page *page, u64 start, u32 len);92 struct folio *folio, u64 start, u32 len);
96void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,93void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
97 struct page *page, u64 start, u32 len);94 struct folio *folio, u64 start, u32 len);
98 95
99void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,96void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
100 struct page *page, u64 start, u32 len);97 struct folio *folio, u64 start, u32 len);
101bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,98bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
102 struct page *page, u64 start, u32 len);99 struct folio *folio, u64 start, u32 len);
103int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,100int btrfs_folio_start_writer_lock(const struct btrfs_fs_info *fs_info,
104 struct page *page, u64 start, u32 len);101 struct folio *folio, u64 start, u32 len);
105void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,102void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
106 struct page *page, u64 start, u32 len);103 struct folio *folio, u64 start, u32 len);
107 104
108/*105/*
109 * Template for subpage related operations.106 * Template for subpage related operations.
110 *107 *
111 * btrfs_subpage_*() are for call sites where the page has subpage attached and108 * btrfs_subpage_*() are for call sites where the folio has subpage attached and
112 * the range is ensured to be inside the page.109 * the range is ensured to be inside the folio's single page.
113 *110 *
114 * btrfs_page_*() are for call sites where the page can either be subpage111 * btrfs_folio_*() are for call sites where the page can either be subpage
115 * specific or regular page. The function will handle both cases.112 * specific or regular folios. The function will handle both cases.
116 * But the range still needs to be inside the page.113 * But the range still needs to be inside one single page.
117 *114 *
118 * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't115 * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
119 * need to be inside the page. Those functions will truncate the range116 * need to be inside the page. Those functions will truncate the range
120 * automatically.117 * automatically.
121 */118 */
122#define DECLARE_BTRFS_SUBPAGE_OPS(name) \119#define DECLARE_BTRFS_SUBPAGE_OPS(name) \
123void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \120void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
124 struct page *page, u64 start, u32 len); \121 struct folio *folio, u64 start, u32 len); \
125void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \122void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
126 struct page *page, u64 start, u32 len); \123 struct folio *folio, u64 start, u32 len); \
127bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \124bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
128 struct page *page, u64 start, u32 len); \125 struct folio *folio, u64 start, u32 len); \
129void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \126void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
130 struct page *page, u64 start, u32 len); \127 struct folio *folio, u64 start, u32 len); \
131void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \128void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
132 struct page *page, u64 start, u32 len); \129 struct folio *folio, u64 start, u32 len); \
133bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \130bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
134 struct page *page, u64 start, u32 len); \131 struct folio *folio, u64 start, u32 len); \
135void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \132void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
136 struct page *page, u64 start, u32 len); \133 struct folio *folio, u64 start, u32 len); \
137void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \134void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
138 struct page *page, u64 start, u32 len); \135 struct folio *folio, u64 start, u32 len); \
139bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \136bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
140 struct page *page, u64 start, u32 len);137 struct folio *folio, u64 start, u32 len);
141 138
142DECLARE_BTRFS_SUBPAGE_OPS(uptodate);139DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
143DECLARE_BTRFS_SUBPAGE_OPS(dirty);140DECLARE_BTRFS_SUBPAGE_OPS(dirty);
@@ -146,13 +143,12 @@ DECLARE_BTRFS_SUBPAGE_OPS(ordered); · 2 unmodified lines
146DECLARE_BTRFS_SUBPAGE_OPS(checked);143DECLARE_BTRFS_SUBPAGE_OPS(checked);
147 144
148bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,145bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
149 struct page *page, u64 start, u32 len);146 struct folio *folio, u64 start, u32 len);
150 147
151void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,148void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info, struct folio *folio);
152 struct page *page);149void btrfs_folio_unlock_writer(struct btrfs_fs_info *fs_info,
153void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page,150 struct folio *folio, u64 start, u32 len);
154 u64 start, u32 len);
155void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,151void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
156 struct page *page, u64 start, u32 len);152 struct folio *folio, u64 start, u32 len);
157 153
158#endif154#endif
fs/btrfs/super.c+1195−1150
@@ -27,6 +27,7 @@ · 26 unmodified lines
27#include <linux/crc32c.h>27#include <linux/crc32c.h>
28#include <linux/btrfs.h>28#include <linux/btrfs.h>
29#include <linux/security.h>29#include <linux/security.h>
30#include <linux/fs_parser.h>
30#include "messages.h"31#include "messages.h"
31#include "delayed-inode.h"32#include "delayed-inode.h"
32#include "ctree.h"33#include "ctree.h"
@@ -64,19 +65,7 @@ · 31 unmodified lines
64#include <trace/events/btrfs.h>65#include <trace/events/btrfs.h>
65 66
66static const struct super_operations btrfs_super_ops;67static const struct super_operations btrfs_super_ops;
67
68/*
69 * Types for mounting the default subvolume and a subvolume explicitly
70 * requested by subvol=/path. That way the callchain is straightforward and we
71 * don't have to play tricks with the mount options and recursive calls to
72 * btrfs_mount.
73 *
74 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
75 */
76static struct file_system_type btrfs_fs_type;68static struct file_system_type btrfs_fs_type;
77static struct file_system_type btrfs_root_fs_type;
78
79static int btrfs_remount(struct super_block *sb, int *flags, char *data);
80 69
81static void btrfs_put_super(struct super_block *sb)70static void btrfs_put_super(struct super_block *sb)
82{71{
@@ -86,8 +75,22 @@ static void btrfs_put_super(struct super_block *sb) · 3 unmodified lines
86 close_ctree(fs_info);75 close_ctree(fs_info);
87}76}
88 77
78/* Store the mount options related information. */
79struct btrfs_fs_context {
80 char *subvol_name;
81 u64 subvol_objectid;
82 u64 max_inline;
83 u32 commit_interval;
84 u32 metadata_ratio;
85 u32 thread_pool_size;
86 unsigned long mount_opt;
87 unsigned long compress_type:4;
88 unsigned int compress_level;
89 refcount_t refs;
90};
91
89enum {92enum {
90 Opt_acl, Opt_noacl,93 Opt_acl,
91 Opt_clear_cache,94 Opt_clear_cache,
92 Opt_commit_interval,95 Opt_commit_interval,
93 Opt_compress,96 Opt_compress,
@@ -97,27 +100,26 @@ enum { · 3 unmodified lines
97 Opt_degraded,100 Opt_degraded,
98 Opt_device,101 Opt_device,
99 Opt_fatal_errors,102 Opt_fatal_errors,
100 Opt_flushoncommit, Opt_noflushoncommit,103 Opt_flushoncommit,
101 Opt_max_inline,104 Opt_max_inline,
102 Opt_barrier, Opt_nobarrier,105 Opt_barrier,
103 Opt_datacow, Opt_nodatacow,106 Opt_datacow,
104 Opt_datasum, Opt_nodatasum,107 Opt_datasum,
105 Opt_defrag, Opt_nodefrag,108 Opt_defrag,
106 Opt_discard, Opt_nodiscard,109 Opt_discard,
107 Opt_discard_mode,110 Opt_discard_mode,
108 Opt_norecovery,
109 Opt_ratio,111 Opt_ratio,
110 Opt_rescan_uuid_tree,112 Opt_rescan_uuid_tree,
111 Opt_skip_balance,113 Opt_skip_balance,
112 Opt_space_cache, Opt_no_space_cache,114 Opt_space_cache,
113 Opt_space_cache_version,115 Opt_space_cache_version,
114 Opt_ssd, Opt_nossd,116 Opt_ssd,
115 Opt_ssd_spread, Opt_nossd_spread,117 Opt_ssd_spread,
116 Opt_subvol,118 Opt_subvol,
117 Opt_subvol_empty,119 Opt_subvol_empty,
118 Opt_subvolid,120 Opt_subvolid,
119 Opt_thread_pool,121 Opt_thread_pool,
120 Opt_treelog, Opt_notreelog,122 Opt_treelog,
121 Opt_user_subvol_rm_allowed,123 Opt_user_subvol_rm_allowed,
122 124
123 /* Rescue options */125 /* Rescue options */
@@ -128,14 +130,10 @@ enum { · 4 unmodified lines
128 Opt_ignoredatacsums,130 Opt_ignoredatacsums,
129 Opt_rescue_all,131 Opt_rescue_all,
130 132
131 /* Deprecated options */
132 Opt_recovery,
133 Opt_inode_cache, Opt_noinode_cache,
134
135 /* Debugging options */133 /* Debugging options */
136 Opt_enospc_debug, Opt_noenospc_debug,134 Opt_enospc_debug,
137#ifdef CONFIG_BTRFS_DEBUG135#ifdef CONFIG_BTRFS_DEBUG
138 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,136 Opt_fragment, Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
139#endif137#endif
140#ifdef CONFIG_BTRFS_FS_REF_VERIFY138#ifdef CONFIG_BTRFS_FS_REF_VERIFY
141 Opt_ref_verify,139 Opt_ref_verify,
@@ -143,785 +141,611 @@ enum { · 1 unmodified line
143 Opt_err,141 Opt_err,
144};142};
145 143
146static const match_table_t tokens = {144enum {
147 {Opt_acl, "acl"},145 Opt_fatal_errors_panic,
148 {Opt_noacl, "noacl"},146 Opt_fatal_errors_bug,
149 {Opt_clear_cache, "clear_cache"},147};
150 {Opt_commit_interval, "commit=%u"},
151 {Opt_compress, "compress"},
152 {Opt_compress_type, "compress=%s"},
153 {Opt_compress_force, "compress-force"},
154 {Opt_compress_force_type, "compress-force=%s"},
155 {Opt_degraded, "degraded"},
156 {Opt_device, "device=%s"},
157 {Opt_fatal_errors, "fatal_errors=%s"},
158 {Opt_flushoncommit, "flushoncommit"},
159 {Opt_noflushoncommit, "noflushoncommit"},
160 {Opt_inode_cache, "inode_cache"},
161 {Opt_noinode_cache, "noinode_cache"},
162 {Opt_max_inline, "max_inline=%s"},
163 {Opt_barrier, "barrier"},
164 {Opt_nobarrier, "nobarrier"},
165 {Opt_datacow, "datacow"},
166 {Opt_nodatacow, "nodatacow"},
167 {Opt_datasum, "datasum"},
168 {Opt_nodatasum, "nodatasum"},
169 {Opt_defrag, "autodefrag"},
170 {Opt_nodefrag, "noautodefrag"},
171 {Opt_discard, "discard"},
172 {Opt_discard_mode, "discard=%s"},
173 {Opt_nodiscard, "nodiscard"},
174 {Opt_norecovery, "norecovery"},
175 {Opt_ratio, "metadata_ratio=%u"},
176 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
177 {Opt_skip_balance, "skip_balance"},
178 {Opt_space_cache, "space_cache"},
179 {Opt_no_space_cache, "nospace_cache"},
180 {Opt_space_cache_version, "space_cache=%s"},
181 {Opt_ssd, "ssd"},
182 {Opt_nossd, "nossd"},
183 {Opt_ssd_spread, "ssd_spread"},
184 {Opt_nossd_spread, "nossd_spread"},
185 {Opt_subvol, "subvol=%s"},
186 {Opt_subvol_empty, "subvol="},
187 {Opt_subvolid, "subvolid=%s"},
188 {Opt_thread_pool, "thread_pool=%u"},
189 {Opt_treelog, "treelog"},
190 {Opt_notreelog, "notreelog"},
191 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
192 148
193 /* Rescue options */149static const struct constant_table btrfs_parameter_fatal_errors[] = {
194 {Opt_rescue, "rescue=%s"},150 { "panic", Opt_fatal_errors_panic },
151 { "bug", Opt_fatal_errors_bug },
152 {}
153};
154
155enum {
156 Opt_discard_sync,
157 Opt_discard_async,
158};
159
160static const struct constant_table btrfs_parameter_discard[] = {
161 { "sync", Opt_discard_sync },
162 { "async", Opt_discard_async },
163 {}
164};
165
166enum {
167 Opt_space_cache_v1,
168 Opt_space_cache_v2,
169};
170
171static const struct constant_table btrfs_parameter_space_cache[] = {
172 { "v1", Opt_space_cache_v1 },
173 { "v2", Opt_space_cache_v2 },
174 {}
175};
176
177enum {
178 Opt_rescue_usebackuproot,
179 Opt_rescue_nologreplay,
180 Opt_rescue_ignorebadroots,
181 Opt_rescue_ignoredatacsums,
182 Opt_rescue_parameter_all,
183};
184
185static const struct constant_table btrfs_parameter_rescue[] = {
186 { "usebackuproot", Opt_rescue_usebackuproot },
187 { "nologreplay", Opt_rescue_nologreplay },
188 { "ignorebadroots", Opt_rescue_ignorebadroots },
189 { "ibadroots", Opt_rescue_ignorebadroots },
190 { "ignoredatacsums", Opt_rescue_ignoredatacsums },
191 { "idatacsums", Opt_rescue_ignoredatacsums },
192 { "all", Opt_rescue_parameter_all },
193 {}
194};
195
196#ifdef CONFIG_BTRFS_DEBUG
197enum {
198 Opt_fragment_parameter_data,
199 Opt_fragment_parameter_metadata,
200 Opt_fragment_parameter_all,
201};
202
203static const struct constant_table btrfs_parameter_fragment[] = {
204 { "data", Opt_fragment_parameter_data },
205 { "metadata", Opt_fragment_parameter_metadata },
206 { "all", Opt_fragment_parameter_all },
207 {}
208};
209#endif
210
211static const struct fs_parameter_spec btrfs_fs_parameters[] = {
212 fsparam_flag_no("acl", Opt_acl),
213 fsparam_flag_no("autodefrag", Opt_defrag),
214 fsparam_flag_no("barrier", Opt_barrier),
215 fsparam_flag("clear_cache", Opt_clear_cache),
216 fsparam_u32("commit", Opt_commit_interval),
217 fsparam_flag("compress", Opt_compress),
218 fsparam_string("compress", Opt_compress_type),
219 fsparam_flag("compress-force", Opt_compress_force),
220 fsparam_string("compress-force", Opt_compress_force_type),
221 fsparam_flag_no("datacow", Opt_datacow),
222 fsparam_flag_no("datasum", Opt_datasum),
223 fsparam_flag("degraded", Opt_degraded),
224 fsparam_string("device", Opt_device),
225 fsparam_flag_no("discard", Opt_discard),
226 fsparam_enum("discard", Opt_discard_mode, btrfs_parameter_discard),
227 fsparam_enum("fatal_errors", Opt_fatal_errors, btrfs_parameter_fatal_errors),
228 fsparam_flag_no("flushoncommit", Opt_flushoncommit),
229 fsparam_string("max_inline", Opt_max_inline),
230 fsparam_u32("metadata_ratio", Opt_ratio),
231 fsparam_flag("rescan_uuid_tree", Opt_rescan_uuid_tree),
232 fsparam_flag("skip_balance", Opt_skip_balance),
233 fsparam_flag_no("space_cache", Opt_space_cache),
234 fsparam_enum("space_cache", Opt_space_cache_version, btrfs_parameter_space_cache),
235 fsparam_flag_no("ssd", Opt_ssd),
236 fsparam_flag_no("ssd_spread", Opt_ssd_spread),
237 fsparam_string("subvol", Opt_subvol),
238 fsparam_flag("subvol=", Opt_subvol_empty),
239 fsparam_u64("subvolid", Opt_subvolid),
240 fsparam_u32("thread_pool", Opt_thread_pool),
241 fsparam_flag_no("treelog", Opt_treelog),
242 fsparam_flag("user_subvol_rm_allowed", Opt_user_subvol_rm_allowed),
243
244 /* Rescue options. */
245 fsparam_enum("rescue", Opt_rescue, btrfs_parameter_rescue),
195 /* Deprecated, with alias rescue=nologreplay */246 /* Deprecated, with alias rescue=nologreplay */
196 {Opt_nologreplay, "nologreplay"},247 __fsparam(NULL, "nologreplay", Opt_nologreplay, fs_param_deprecated, NULL),
197 /* Deprecated, with alias rescue=usebackuproot */248 /* Deprecated, with alias rescue=usebackuproot */
198 {Opt_usebackuproot, "usebackuproot"},249 __fsparam(NULL, "usebackuproot", Opt_usebackuproot, fs_param_deprecated, NULL),
199 250
200 /* Deprecated options */251 /* Debugging options. */
201 {Opt_recovery, "recovery"},252 fsparam_flag_no("enospc_debug", Opt_enospc_debug),
202
203 /* Debugging options */
204 {Opt_enospc_debug, "enospc_debug"},
205 {Opt_noenospc_debug, "noenospc_debug"},
206#ifdef CONFIG_BTRFS_DEBUG253#ifdef CONFIG_BTRFS_DEBUG
207 {Opt_fragment_data, "fragment=data"},254 fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
208 {Opt_fragment_metadata, "fragment=metadata"},
209 {Opt_fragment_all, "fragment=all"},
210#endif255#endif
211#ifdef CONFIG_BTRFS_FS_REF_VERIFY256#ifdef CONFIG_BTRFS_FS_REF_VERIFY
212 {Opt_ref_verify, "ref_verify"},257 fsparam_flag("ref_verify", Opt_ref_verify),
213#endif258#endif
214 {Opt_err, NULL},259 {}
215};260};
216 261
217static const match_table_t rescue_tokens = {262/* No support for restricting writes to btrfs devices yet... */
218 {Opt_usebackuproot, "usebackuproot"},263static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
219 {Opt_nologreplay, "nologreplay"},
220 {Opt_ignorebadroots, "ignorebadroots"},
221 {Opt_ignorebadroots, "ibadroots"},
222 {Opt_ignoredatacsums, "ignoredatacsums"},
223 {Opt_ignoredatacsums, "idatacsums"},
224 {Opt_rescue_all, "all"},
225 {Opt_err, NULL},
226};
227
228static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
229 const char *opt_name)
230{264{
231 if (fs_info->mount_opt & opt) {265 return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
232 btrfs_err(fs_info, "%s must be used with ro mount option",
233 opt_name);
234 return true;
235 }
236 return false;
237}266}
238 267
239static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)268static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
240{269{
241 char *opts;270 struct btrfs_fs_context *ctx = fc->fs_private;
242 char *orig;271 struct fs_parse_result result;
243 char *p;272 int opt;
244 substring_t args[MAX_OPT_ARGS];
245 int ret = 0;
246 273
247 opts = kstrdup(options, GFP_KERNEL);274 opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
248 if (!opts)275 if (opt < 0)
249 return -ENOMEM;276 return opt;
250 orig = opts;
251 277
252 while ((p = strsep(&opts, ":")) != NULL) {278 switch (opt) {
253 int token;279 case Opt_degraded:
280 btrfs_set_opt(ctx->mount_opt, DEGRADED);
281 break;
282 case Opt_subvol_empty:
283 /*
284 * This exists because we used to allow it on accident, so we're
285 * keeping it to maintain ABI. See 37becec95ac3 ("Btrfs: allow
286 * empty subvol= again").
287 */
288 break;
289 case Opt_subvol:
290 kfree(ctx->subvol_name);
291 ctx->subvol_name = kstrdup(param->string, GFP_KERNEL);
292 if (!ctx->subvol_name)
293 return -ENOMEM;
294 break;
295 case Opt_subvolid:
296 ctx->subvol_objectid = result.uint_64;
254 297
255 if (!*p)298 /* subvolid=0 means give me the original fs_tree. */
256 continue;299 if (!ctx->subvol_objectid)
257 token = match_token(p, rescue_tokens, args);300 ctx->subvol_objectid = BTRFS_FS_TREE_OBJECTID;
258 switch (token){301 break;
259 case Opt_usebackuproot:302 case Opt_device: {
260 btrfs_info(info,303 struct btrfs_device *device;
261 "trying to use backup root at mount time");304 blk_mode_t mode = btrfs_open_mode(fc);
262 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
263 break;
264 case Opt_nologreplay:
265 btrfs_set_and_info(info, NOLOGREPLAY,
266 "disabling log replay at mount time");
267 break;
268 case Opt_ignorebadroots:
269 btrfs_set_and_info(info, IGNOREBADROOTS,
270 "ignoring bad roots");
271 break;
272 case Opt_ignoredatacsums:
273 btrfs_set_and_info(info, IGNOREDATACSUMS,
274 "ignoring data csums");
275 break;
276 case Opt_rescue_all:
277 btrfs_info(info, "enabling all of the rescue options");
278 btrfs_set_and_info(info, IGNOREDATACSUMS,
279 "ignoring data csums");
280 btrfs_set_and_info(info, IGNOREBADROOTS,
281 "ignoring bad roots");
282 btrfs_set_and_info(info, NOLOGREPLAY,
283 "disabling log replay at mount time");
284 break;
285 case Opt_err:
286 btrfs_info(info, "unrecognized rescue option '%s'", p);
287 ret = -EINVAL;
288 goto out;
289 default:
290 break;
291 }
292 305
306 mutex_lock(&uuid_mutex);
307 device = btrfs_scan_one_device(param->string, mode, false);
308 mutex_unlock(&uuid_mutex);
309 if (IS_ERR(device))
310 return PTR_ERR(device);
311 break;
293 }312 }
294out:313 case Opt_datasum:
295 kfree(orig);314 if (result.negated) {
296 return ret;315 btrfs_set_opt(ctx->mount_opt, NODATASUM);
297}
298
299/*
300 * Regular mount options parser. Everything that is needed only when
301 * reading in a new superblock is parsed here.
302 * XXX JDM: This needs to be cleaned up for remount.
303 */
304int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
305 unsigned long new_flags)
306{
307 substring_t args[MAX_OPT_ARGS];
308 char *p, *num;
309 int intarg;
310 int ret = 0;
311 char *compress_type;
312 bool compress_force = false;
313 enum btrfs_compression_type saved_compress_type;
314 int saved_compress_level;
315 bool saved_compress_force;
316 int no_compress = 0;
317 const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
318
319 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
320 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
321 else if (btrfs_free_space_cache_v1_active(info)) {
322 if (btrfs_is_zoned(info)) {
323 btrfs_info(info,
324 "zoned: clearing existing space cache");
325 btrfs_set_super_cache_generation(info->super_copy, 0);
326 } else {316 } else {
327 btrfs_set_opt(info->mount_opt, SPACE_CACHE);317 btrfs_clear_opt(ctx->mount_opt, NODATACOW);
318 btrfs_clear_opt(ctx->mount_opt, NODATASUM);
328 }319 }
329 }320 break;
330 321 case Opt_datacow:
331 /*322 if (result.negated) {
332 * Even the options are empty, we still need to do extra check323 btrfs_clear_opt(ctx->mount_opt, COMPRESS);
333 * against new flags324 btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
334 */325 btrfs_set_opt(ctx->mount_opt, NODATACOW);
335 if (!options)326 btrfs_set_opt(ctx->mount_opt, NODATASUM);
336 goto check;327 } else {
337 328 btrfs_clear_opt(ctx->mount_opt, NODATACOW);
338 while ((p = strsep(&options, ",")) != NULL) {329 }
339 int token;330 break;
340 if (!*p)331 case Opt_compress_force:
341 continue;332 case Opt_compress_force_type:
342 333 btrfs_set_opt(ctx->mount_opt, FORCE_COMPRESS);
343 token = match_token(p, tokens, args);334 fallthrough;
344 switch (token) {335 case Opt_compress:
345 case Opt_degraded:336 case Opt_compress_type:
346 btrfs_info(info, "allowing degraded mounts");337 if (opt == Opt_compress || opt == Opt_compress_force) {
347 btrfs_set_opt(info->mount_opt, DEGRADED);338 ctx->compress_type = BTRFS_COMPRESS_ZLIB;
348 break;339 ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
349 case Opt_subvol:340 btrfs_set_opt(ctx->mount_opt, COMPRESS);
350 case Opt_subvol_empty:341 btrfs_clear_opt(ctx->mount_opt, NODATACOW);
351 case Opt_subvolid:342 btrfs_clear_opt(ctx->mount_opt, NODATASUM);
352 case Opt_device:343 } else if (strncmp(param->string, "zlib", 4) == 0) {
353 /*344 ctx->compress_type = BTRFS_COMPRESS_ZLIB;
354 * These are parsed by btrfs_parse_subvol_options or345 ctx->compress_level =
355 * btrfs_parse_device_options and can be ignored here.346 btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
356 */347 param->string + 4);
357 break;348 btrfs_set_opt(ctx->mount_opt, COMPRESS);
358 case Opt_nodatasum:349 btrfs_clear_opt(ctx->mount_opt, NODATACOW);
359 btrfs_set_and_info(info, NODATASUM,350 btrfs_clear_opt(ctx->mount_opt, NODATASUM);
360 "setting nodatasum");351 } else if (strncmp(param->string, "lzo", 3) == 0) {
361 break;352 ctx->compress_type = BTRFS_COMPRESS_LZO;
362 case Opt_datasum:353 ctx->compress_level = 0;
363 if (btrfs_test_opt(info, NODATASUM)) {354 btrfs_set_opt(ctx->mount_opt, COMPRESS);
364 if (btrfs_test_opt(info, NODATACOW))355 btrfs_clear_opt(ctx->mount_opt, NODATACOW);
365 btrfs_info(info,356 btrfs_clear_opt(ctx->mount_opt, NODATASUM);
366 "setting datasum, datacow enabled");357 } else if (strncmp(param->string, "zstd", 4) == 0) {
367 else358 ctx->compress_type = BTRFS_COMPRESS_ZSTD;
368 btrfs_info(info, "setting datasum");359 ctx->compress_level =
369 }360 btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
370 btrfs_clear_opt(info->mount_opt, NODATACOW);361 param->string + 4);
371 btrfs_clear_opt(info->mount_opt, NODATASUM);362 btrfs_set_opt(ctx->mount_opt, COMPRESS);
372 break;363 btrfs_clear_opt(ctx->mount_opt, NODATACOW);
373 case Opt_nodatacow:364 btrfs_clear_opt(ctx->mount_opt, NODATASUM);
374 if (!btrfs_test_opt(info, NODATACOW)) {365 } else if (strncmp(param->string, "no", 2) == 0) {
375 if (!btrfs_test_opt(info, COMPRESS) ||366 ctx->compress_level = 0;
376 !btrfs_test_opt(info, FORCE_COMPRESS)) {367 ctx->compress_type = 0;
377 btrfs_info(info,368 btrfs_clear_opt(ctx->mount_opt, COMPRESS);
378 "setting nodatacow, compression disabled");369 btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
379 } else {370 } else {
380 btrfs_info(info, "setting nodatacow");371 btrfs_err(NULL, "unrecognized compression value %s",
381 }372 param->string);
382 }373 return -EINVAL;
383 btrfs_clear_opt(info->mount_opt, COMPRESS);374 }
384 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);375 break;
385 btrfs_set_opt(info->mount_opt, NODATACOW);376 case Opt_ssd:
386 btrfs_set_opt(info->mount_opt, NODATASUM);377 if (result.negated) {
387 break;378 btrfs_set_opt(ctx->mount_opt, NOSSD);
388 case Opt_datacow:379 btrfs_clear_opt(ctx->mount_opt, SSD);
389 btrfs_clear_and_info(info, NODATACOW,380 btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
390 "setting datacow");381 } else {
391 break;382 btrfs_set_opt(ctx->mount_opt, SSD);
392 case Opt_compress_force:383 btrfs_clear_opt(ctx->mount_opt, NOSSD);
393 case Opt_compress_force_type:384 }
394 compress_force = true;385 break;
395 fallthrough;386 case Opt_ssd_spread:
396 case Opt_compress:387 if (result.negated) {
397 case Opt_compress_type:388 btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
398 saved_compress_type = btrfs_test_opt(info,389 } else {
399 COMPRESS) ?390 btrfs_set_opt(ctx->mount_opt, SSD);
400 info->compress_type : BTRFS_COMPRESS_NONE;391 btrfs_set_opt(ctx->mount_opt, SSD_SPREAD);
401 saved_compress_force =392 btrfs_clear_opt(ctx->mount_opt, NOSSD);
402 btrfs_test_opt(info, FORCE_COMPRESS);393 }
403 saved_compress_level = info->compress_level;394 break;
404 if (token == Opt_compress ||395 case Opt_barrier:
405 token == Opt_compress_force ||396 if (result.negated)
406 strncmp(args[0].from, "zlib", 4) == 0) {397 btrfs_set_opt(ctx->mount_opt, NOBARRIER);
407 compress_type = "zlib";398 else
408 399 btrfs_clear_opt(ctx->mount_opt, NOBARRIER);
409 info->compress_type = BTRFS_COMPRESS_ZLIB;400 break;
410 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;401 case Opt_thread_pool:
411 /*402 if (result.uint_32 == 0) {
412 * args[0] contains uninitialized data since403 btrfs_err(NULL, "invalid value 0 for thread_pool");
413 * for these tokens we don't expect any404 return -EINVAL;
414 * parameter.405 }
415 */406 ctx->thread_pool_size = result.uint_32;
416 if (token != Opt_compress &&407 break;
417 token != Opt_compress_force)408 case Opt_max_inline:
418 info->compress_level =409 ctx->max_inline = memparse(param->string, NULL);
419 btrfs_compress_str2level(410 break;
420 BTRFS_COMPRESS_ZLIB,411 case Opt_acl:
421 args[0].from + 4);412 if (result.negated) {
422 btrfs_set_opt(info->mount_opt, COMPRESS);413 fc->sb_flags &= ~SB_POSIXACL;
423 btrfs_clear_opt(info->mount_opt, NODATACOW);414 } else {
424 btrfs_clear_opt(info->mount_opt, NODATASUM);
425 no_compress = 0;
426 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
427 compress_type = "lzo";
428 info->compress_type = BTRFS_COMPRESS_LZO;
429 info->compress_level = 0;
430 btrfs_set_opt(info->mount_opt, COMPRESS);
431 btrfs_clear_opt(info->mount_opt, NODATACOW);
432 btrfs_clear_opt(info->mount_opt, NODATASUM);
433 btrfs_set_fs_incompat(info, COMPRESS_LZO);
434 no_compress = 0;
435 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
436 compress_type = "zstd";
437 info->compress_type = BTRFS_COMPRESS_ZSTD;
438 info->compress_level =
439 btrfs_compress_str2level(
440 BTRFS_COMPRESS_ZSTD,
441 args[0].from + 4);
442 btrfs_set_opt(info->mount_opt, COMPRESS);
443 btrfs_clear_opt(info->mount_opt, NODATACOW);
444 btrfs_clear_opt(info->mount_opt, NODATASUM);
445 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
446 no_compress = 0;
447 } else if (strncmp(args[0].from, "no", 2) == 0) {
448 compress_type = "no";
449 info->compress_level = 0;
450 info->compress_type = 0;
451 btrfs_clear_opt(info->mount_opt, COMPRESS);
452 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
453 compress_force = false;
454 no_compress++;
455 } else {
456 btrfs_err(info, "unrecognized compression value %s",
457 args[0].from);
458 ret = -EINVAL;
459 goto out;
460 }
461
462 if (compress_force) {
463 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
464 } else {
465 /*
466 * If we remount from compress-force=xxx to
467 * compress=xxx, we need clear FORCE_COMPRESS
468 * flag, otherwise, there is no way for users
469 * to disable forcible compression separately.
470 */
471 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
472 }
473 if (no_compress == 1) {
474 btrfs_info(info, "use no compression");
475 } else if ((info->compress_type != saved_compress_type) ||
476 (compress_force != saved_compress_force) ||
477 (info->compress_level != saved_compress_level)) {
478 btrfs_info(info, "%s %s compression, level %d",
479 (compress_force) ? "force" : "use",
480 compress_type, info->compress_level);
481 }
482 compress_force = false;
483 break;
484 case Opt_ssd:
485 btrfs_set_and_info(info, SSD,
486 "enabling ssd optimizations");
487 btrfs_clear_opt(info->mount_opt, NOSSD);
488 break;
489 case Opt_ssd_spread:
490 btrfs_set_and_info(info, SSD,
491 "enabling ssd optimizations");
492 btrfs_set_and_info(info, SSD_SPREAD,
493 "using spread ssd allocation scheme");
494 btrfs_clear_opt(info->mount_opt, NOSSD);
495 break;
496 case Opt_nossd:
497 btrfs_set_opt(info->mount_opt, NOSSD);
498 btrfs_clear_and_info(info, SSD,
499 "not using ssd optimizations");
500 fallthrough;
501 case Opt_nossd_spread:
502 btrfs_clear_and_info(info, SSD_SPREAD,
503 "not using spread ssd allocation scheme");
504 break;
505 case Opt_barrier:
506 btrfs_clear_and_info(info, NOBARRIER,
507 "turning on barriers");
508 break;
509 case Opt_nobarrier:
510 btrfs_set_and_info(info, NOBARRIER,
511 "turning off barriers");
512 break;
513 case Opt_thread_pool:
514 ret = match_int(&args[0], &intarg);
515 if (ret) {
516 btrfs_err(info, "unrecognized thread_pool value %s",
517 args[0].from);
518 goto out;
519 } else if (intarg == 0) {
520 btrfs_err(info, "invalid value 0 for thread_pool");
521 ret = -EINVAL;
522 goto out;
523 }
524 info->thread_pool_size = intarg;
525 break;
526 case Opt_max_inline:
527 num = match_strdup(&args[0]);
528 if (num) {
529 info->max_inline = memparse(num, NULL);
530 kfree(num);
531
532 if (info->max_inline) {
533 info->max_inline = min_t(u64,
534 info->max_inline,
535 info->sectorsize);
536 }
537 btrfs_info(info, "max_inline at %llu",
538 info->max_inline);
539 } else {
540 ret = -ENOMEM;
541 goto out;
542 }
543 break;
544 case Opt_acl:
545#ifdef CONFIG_BTRFS_FS_POSIX_ACL415#ifdef CONFIG_BTRFS_FS_POSIX_ACL
546 info->sb->s_flags |= SB_POSIXACL;416 fc->sb_flags |= SB_POSIXACL;
547 break;
548#else417#else
549 btrfs_err(info, "support for ACL not compiled in!");418 btrfs_err(NULL, "support for ACL not compiled in");
550 ret = -EINVAL;419 return -EINVAL;
551 goto out;
552#endif420#endif
553 case Opt_noacl:421 }
554 info->sb->s_flags &= ~SB_POSIXACL;422 /*
555 break;423 * VFS limits the ability to toggle ACL on and off via remount,
556 case Opt_notreelog:424 * despite every file system allowing this. This seems to be
557 btrfs_set_and_info(info, NOTREELOG,425 * an oversight since we all do, but it'll fail if we're
558 "disabling tree log");426 * remounting. So don't set the mask here, we'll check it in
559 break;427 * btrfs_reconfigure and do the toggling ourselves.
560 case Opt_treelog:428 */
561 btrfs_clear_and_info(info, NOTREELOG,429 if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
562 "enabling tree log");430 fc->sb_flags_mask |= SB_POSIXACL;
563 break;431 break;
564 case Opt_norecovery:432 case Opt_treelog:
565 case Opt_nologreplay:433 if (result.negated)
566 btrfs_warn(info,434 btrfs_set_opt(ctx->mount_opt, NOTREELOG);
435 else
436 btrfs_clear_opt(ctx->mount_opt, NOTREELOG);
437 break;
438 case Opt_nologreplay:
439 btrfs_warn(NULL,
567 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");440 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
568 btrfs_set_and_info(info, NOLOGREPLAY,441 btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
569 "disabling log replay at mount time");442 break;
570 break;443 case Opt_flushoncommit:
571 case Opt_flushoncommit:444 if (result.negated)
572 btrfs_set_and_info(info, FLUSHONCOMMIT,445 btrfs_clear_opt(ctx->mount_opt, FLUSHONCOMMIT);
573 "turning on flush-on-commit");446 else
574 break;447 btrfs_set_opt(ctx->mount_opt, FLUSHONCOMMIT);
575 case Opt_noflushoncommit:448 break;
576 btrfs_clear_and_info(info, FLUSHONCOMMIT,449 case Opt_ratio:
577 "turning off flush-on-commit");450 ctx->metadata_ratio = result.uint_32;
578 break;451 break;
579 case Opt_ratio:452 case Opt_discard:
580 ret = match_int(&args[0], &intarg);453 if (result.negated) {
581 if (ret) {454 btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
582 btrfs_err(info, "unrecognized metadata_ratio value %s",455 btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
583 args[0].from);456 btrfs_set_opt(ctx->mount_opt, NODISCARD);
584 goto out;457 } else {
585 }458 btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
586 info->metadata_ratio = intarg;459 btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
587 btrfs_info(info, "metadata ratio %u",460 }
588 info->metadata_ratio);461 break;
589 break;462 case Opt_discard_mode:
590 case Opt_discard:463 switch (result.uint_32) {
591 case Opt_discard_mode:464 case Opt_discard_sync:
592 if (token == Opt_discard ||465 btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
593 strcmp(args[0].from, "sync") == 0) {466 btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
594 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
595 btrfs_set_and_info(info, DISCARD_SYNC,
596 "turning on sync discard");
597 } else if (strcmp(args[0].from, "async") == 0) {
598 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
599 btrfs_set_and_info(info, DISCARD_ASYNC,
600 "turning on async discard");
601 } else {
602 btrfs_err(info, "unrecognized discard mode value %s",
603 args[0].from);
604 ret = -EINVAL;
605 goto out;
606 }
607 btrfs_clear_opt(info->mount_opt, NODISCARD);
608 break;
609 case Opt_nodiscard:
610 btrfs_clear_and_info(info, DISCARD_SYNC,
611 "turning off discard");
612 btrfs_clear_and_info(info, DISCARD_ASYNC,
613 "turning off async discard");
614 btrfs_set_opt(info->mount_opt, NODISCARD);
615 break;
616 case Opt_space_cache:
617 case Opt_space_cache_version:
618 /*
619 * We already set FREE_SPACE_TREE above because we have
620 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
621 * to allow v1 to be set for extent tree v2, simply
622 * ignore this setting if we're extent tree v2.
623 */
624 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
625 break;
626 if (token == Opt_space_cache ||
627 strcmp(args[0].from, "v1") == 0) {
628 btrfs_clear_opt(info->mount_opt,
629 FREE_SPACE_TREE);
630 btrfs_set_and_info(info, SPACE_CACHE,
631 "enabling disk space caching");
632 } else if (strcmp(args[0].from, "v2") == 0) {
633 btrfs_clear_opt(info->mount_opt,
634 SPACE_CACHE);
635 btrfs_set_and_info(info, FREE_SPACE_TREE,
636 "enabling free space tree");
637 } else {
638 btrfs_err(info, "unrecognized space_cache value %s",
639 args[0].from);
640 ret = -EINVAL;
641 goto out;
642 }
643 break;
644 case Opt_rescan_uuid_tree:
645 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
646 break;
647 case Opt_no_space_cache:
648 /*
649 * We cannot operate without the free space tree with
650 * extent tree v2, ignore this option.
651 */
652 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
653 break;
654 if (btrfs_test_opt(info, SPACE_CACHE)) {
655 btrfs_clear_and_info(info, SPACE_CACHE,
656 "disabling disk space caching");
657 }
658 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
659 btrfs_clear_and_info(info, FREE_SPACE_TREE,
660 "disabling free space tree");
661 }
662 break;
663 case Opt_inode_cache:
664 case Opt_noinode_cache:
665 btrfs_warn(info,
666 "the 'inode_cache' option is deprecated and has no effect since 5.11");
667 break;
668 case Opt_clear_cache:
669 /*
670 * We cannot clear the free space tree with extent tree
671 * v2, ignore this option.
672 */
673 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
674 break;
675 btrfs_set_and_info(info, CLEAR_CACHE,
676 "force clearing of disk cache");
677 break;467 break;
678 case Opt_user_subvol_rm_allowed:468 case Opt_discard_async:
679 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);469 btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
470 btrfs_set_opt(ctx->mount_opt, DISCARD_ASYNC);
680 break;471 break;
681 case Opt_enospc_debug:472 default:
682 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);473 btrfs_err(NULL, "unrecognized discard mode value %s",
474 param->key);
475 return -EINVAL;
476 }
477 btrfs_clear_opt(ctx->mount_opt, NODISCARD);
478 break;
479 case Opt_space_cache:
480 if (result.negated) {
481 btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
482 btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
483 btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
484 } else {
485 btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
486 btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
487 }
488 break;
489 case Opt_space_cache_version:
490 switch (result.uint_32) {
491 case Opt_space_cache_v1:
492 btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
493 btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
683 break;494 break;
684 case Opt_noenospc_debug:495 case Opt_space_cache_v2:
685 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);496 btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
497 btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
686 break;498 break;
687 case Opt_defrag:499 default:
688 btrfs_set_and_info(info, AUTO_DEFRAG,500 btrfs_err(NULL, "unrecognized space_cache value %s",
689 "enabling auto defrag");501 param->key);
502 return -EINVAL;
503 }
504 break;
505 case Opt_rescan_uuid_tree:
506 btrfs_set_opt(ctx->mount_opt, RESCAN_UUID_TREE);
507 break;
508 case Opt_clear_cache:
509 btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
510 break;
511 case Opt_user_subvol_rm_allowed:
512 btrfs_set_opt(ctx->mount_opt, USER_SUBVOL_RM_ALLOWED);
513 break;
514 case Opt_enospc_debug:
515 if (result.negated)
516 btrfs_clear_opt(ctx->mount_opt, ENOSPC_DEBUG);
517 else
518 btrfs_set_opt(ctx->mount_opt, ENOSPC_DEBUG);
519 break;
520 case Opt_defrag:
521 if (result.negated)
522 btrfs_clear_opt(ctx->mount_opt, AUTO_DEFRAG);
523 else
524 btrfs_set_opt(ctx->mount_opt, AUTO_DEFRAG);
525 break;
526 case Opt_usebackuproot:
527 btrfs_warn(NULL,
528 "'usebackuproot' is deprecated, use 'rescue=usebackuproot' instead");
529 btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
530
531 /* If we're loading the backup roots we can't trust the space cache. */
532 btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
533 break;
534 case Opt_skip_balance:
535 btrfs_set_opt(ctx->mount_opt, SKIP_BALANCE);
536 break;
537 case Opt_fatal_errors:
538 switch (result.uint_32) {
539 case Opt_fatal_errors_panic:
540 btrfs_set_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
690 break;541 break;
691 case Opt_nodefrag:542 case Opt_fatal_errors_bug:
692 btrfs_clear_and_info(info, AUTO_DEFRAG,543 btrfs_clear_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
693 "disabling auto defrag");
694 break;544 break;
695 case Opt_recovery:545 default:
696 case Opt_usebackuproot:546 btrfs_err(NULL, "unrecognized fatal_errors value %s",
697 btrfs_warn(info,547 param->key);
698 "'%s' is deprecated, use 'rescue=usebackuproot' instead",548 return -EINVAL;
699 token == Opt_recovery ? "recovery" :549 }
700 "usebackuproot");550 break;
701 btrfs_info(info,551 case Opt_commit_interval:
702 "trying to use backup root at mount time");552 ctx->commit_interval = result.uint_32;
703 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);553 if (ctx->commit_interval == 0)
554 ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
555 break;
556 case Opt_rescue:
557 switch (result.uint_32) {
558 case Opt_rescue_usebackuproot:
559 btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
704 break;560 break;
705 case Opt_skip_balance:561 case Opt_rescue_nologreplay:
706 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);562 btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
707 break;563 break;
708 case Opt_fatal_errors:564 case Opt_rescue_ignorebadroots:
709 if (strcmp(args[0].from, "panic") == 0) {565 btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
710 btrfs_set_opt(info->mount_opt,
711 PANIC_ON_FATAL_ERROR);
712 } else if (strcmp(args[0].from, "bug") == 0) {
713 btrfs_clear_opt(info->mount_opt,
714 PANIC_ON_FATAL_ERROR);
715 } else {
716 btrfs_err(info, "unrecognized fatal_errors value %s",
717 args[0].from);
718 ret = -EINVAL;
719 goto out;
720 }
721 break;566 break;
722 case Opt_commit_interval:567 case Opt_rescue_ignoredatacsums:
723 intarg = 0;568 btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
724 ret = match_int(&args[0], &intarg);
725 if (ret) {
726 btrfs_err(info, "unrecognized commit_interval value %s",
727 args[0].from);
728 ret = -EINVAL;
729 goto out;
730 }
731 if (intarg == 0) {
732 btrfs_info(info,
733 "using default commit interval %us",
734 BTRFS_DEFAULT_COMMIT_INTERVAL);
735 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
736 } else if (intarg > 300) {
737 btrfs_warn(info, "excessive commit interval %d",
738 intarg);
739 }
740 info->commit_interval = intarg;
741 break;569 break;
742 case Opt_rescue:570 case Opt_rescue_parameter_all:
743 ret = parse_rescue_options(info, args[0].from);571 btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
744 if (ret < 0) {572 btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
745 btrfs_err(info, "unrecognized rescue value %s",573 btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
746 args[0].from);
747 goto out;
748 }
749 break;574 break;
575 default:
576 btrfs_info(NULL, "unrecognized rescue option '%s'",
577 param->key);
578 return -EINVAL;
579 }
580 break;
750#ifdef CONFIG_BTRFS_DEBUG581#ifdef CONFIG_BTRFS_DEBUG
751 case Opt_fragment_all:582 case Opt_fragment:
752 btrfs_info(info, "fragmenting all space");583 switch (result.uint_32) {
753 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);584 case Opt_fragment_parameter_all:
754 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);585 btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
586 btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
755 break;587 break;
756 case Opt_fragment_metadata:588 case Opt_fragment_parameter_metadata:
757 btrfs_info(info, "fragmenting metadata");589 btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
758 btrfs_set_opt(info->mount_opt,
759 FRAGMENT_METADATA);
760 break;590 break;
761 case Opt_fragment_data:591 case Opt_fragment_parameter_data:
762 btrfs_info(info, "fragmenting data");592 btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
763 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
764 break;593 break;
594 default:
595 btrfs_info(NULL, "unrecognized fragment option '%s'",
596 param->key);
597 return -EINVAL;
598 }
599 break;
765#endif600#endif
766#ifdef CONFIG_BTRFS_FS_REF_VERIFY601#ifdef CONFIG_BTRFS_FS_REF_VERIFY
767 case Opt_ref_verify:602 case Opt_ref_verify:
768 btrfs_info(info, "doing ref verification");603 btrfs_set_opt(ctx->mount_opt, REF_VERIFY);
769 btrfs_set_opt(info->mount_opt, REF_VERIFY);604 break;
770 break;
771#endif605#endif
772 case Opt_err:606 default:
773 btrfs_err(info, "unrecognized mount option '%s'", p);607 btrfs_err(NULL, "unrecognized mount option '%s'", param->key);
774 ret = -EINVAL;608 return -EINVAL;
775 goto out;
776 default:
777 break;
778 }
779 }609 }
780check:
781 /* We're read-only, don't have to check. */
782 if (new_flags & SB_RDONLY)
783 goto out;
784 610
785 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||611 return 0;
786 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
787 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
788 ret = -EINVAL;
789out:
790 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
791 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
792 !btrfs_test_opt(info, CLEAR_CACHE)) {
793 btrfs_err(info, "cannot disable free space tree");
794 ret = -EINVAL;
795 }
796 if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
797 !btrfs_test_opt(info, FREE_SPACE_TREE)) {
798 btrfs_err(info, "cannot disable free space tree with block-group-tree feature");
799 ret = -EINVAL;
800 }
801 if (!ret)
802 ret = btrfs_check_mountopts_zoned(info);
803 if (!ret && !remounting) {
804 if (btrfs_test_opt(info, SPACE_CACHE))
805 btrfs_info(info, "disk space caching is enabled");
806 if (btrfs_test_opt(info, FREE_SPACE_TREE))
807 btrfs_info(info, "using free space tree");
808 }
809 return ret;
810}612}
811 613
812/*614/*
813 * Parse mount options that are required early in the mount process.615 * Some options only have meaning at mount time and shouldn't persist across
814 *616 * remounts, or be displayed. Clear these at the end of mount and remount code
815 * All other options will be parsed on much later in the mount process and617 * paths.
816 * only when we need to allocate a new super block.
817 */618 */
818static int btrfs_parse_device_options(const char *options, blk_mode_t flags)619static void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
819{620{
820 substring_t args[MAX_OPT_ARGS];621 btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
821 char *device_name, *opts, *orig, *p;622 btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
822 struct btrfs_device *device = NULL;623 btrfs_clear_opt(fs_info->mount_opt, NOSPACECACHE);
823 int error = 0;624}
824 625
825 lockdep_assert_held(&uuid_mutex);626static bool check_ro_option(struct btrfs_fs_info *fs_info,
627 unsigned long mount_opt, unsigned long opt,
628 const char *opt_name)
629{
630 if (mount_opt & opt) {
631 btrfs_err(fs_info, "%s must be used with ro mount option",
632 opt_name);
633 return true;
634 }
635 return false;
636}
826 637
827 if (!options)638bool btrfs_check_options(struct btrfs_fs_info *info, unsigned long *mount_opt,
828 return 0;639 unsigned long flags)
640{
641 bool ret = true;
829 642
830 /*643 if (!(flags & SB_RDONLY) &&
831 * strsep changes the string, duplicate it because btrfs_parse_options644 (check_ro_option(info, *mount_opt, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
832 * gets called later645 check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
833 */646 check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")))
834 opts = kstrdup(options, GFP_KERNEL);647 ret = false;
835 if (!opts)
836 return -ENOMEM;
837 orig = opts;
838 648
839 while ((p = strsep(&opts, ",")) != NULL) {649 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
840 int token;650 !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE) &&
651 !btrfs_raw_test_opt(*mount_opt, CLEAR_CACHE)) {
652 btrfs_err(info, "cannot disable free-space-tree");
653 ret = false;
654 }
655 if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
656 !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE)) {
657 btrfs_err(info, "cannot disable free-space-tree with block-group-tree feature");
658 ret = false;
659 }
841 660
842 if (!*p)661 if (btrfs_check_mountopts_zoned(info, mount_opt))
843 continue;662 ret = false;
844 663
845 token = match_token(p, tokens, args);664 if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
846 if (token == Opt_device) {665 if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE))
847 device_name = match_strdup(&args[0]);666 btrfs_info(info, "disk space caching is enabled");
848 if (!device_name) {667 if (btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE))
849 error = -ENOMEM;668 btrfs_info(info, "using free-space-tree");
850 goto out;
851 }
852 device = btrfs_scan_one_device(device_name, flags, false);
853 kfree(device_name);
854 if (IS_ERR(device)) {
855 error = PTR_ERR(device);
856 goto out;
857 }
858 }
859 }669 }
860 670
861out:671 return ret;
862 kfree(orig);
863 return error;
864}672}
865 673
866/*674/*
867 * Parse mount options that are related to subvolume id675 * This is subtle, we only call this during open_ctree(). We need to pre-load
676 * the mount options with the on-disk settings. Before the new mount API took
677 * effect we would do this on mount and remount. With the new mount API we'll
678 * only do this on the initial mount.
868 *679 *
869 * The value is later passed to mount_subvol()680 * This isn't a change in behavior, because we're using the current state of the
681 * file system to set the current mount options. If you mounted with special
682 * options to disable these features and then remounted we wouldn't revert the
683 * settings, because mounting without these features cleared the on-disk
684 * settings, so this being called on re-mount is not needed.
870 */685 */
871static int btrfs_parse_subvol_options(const char *options, char **subvol_name,686void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
872 u64 *subvol_objectid)
873{687{
874 substring_t args[MAX_OPT_ARGS];688 if (fs_info->sectorsize < PAGE_SIZE) {
875 char *opts, *orig, *p;689 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
876 int error = 0;690 if (!btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
877 u64 subvolid;691 btrfs_info(fs_info,
878 692 "forcing free space tree for sector size %u with page size %lu",
879 if (!options)693 fs_info->sectorsize, PAGE_SIZE);
880 return 0;694 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
695 }
696 }
881 697
882 /*698 /*
883 * strsep changes the string, duplicate it because699 * At this point our mount options are populated, so we only mess with
884 * btrfs_parse_device_options gets called later700 * these settings if we don't have any settings already.
885 */701 */
886 opts = kstrdup(options, GFP_KERNEL);702 if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
887 if (!opts)703 return;
888 return -ENOMEM;
889 orig = opts;
890 704
891 while ((p = strsep(&opts, ",")) != NULL) {705 if (btrfs_is_zoned(fs_info) &&
892 int token;706 btrfs_free_space_cache_v1_active(fs_info)) {
893 if (!*p)707 btrfs_info(fs_info, "zoned: clearing existing space cache");
894 continue;708 btrfs_set_super_cache_generation(fs_info->super_copy, 0);
709 return;
710 }
895 711
896 token = match_token(p, tokens, args);712 if (btrfs_test_opt(fs_info, SPACE_CACHE))
897 switch (token) {713 return;
898 case Opt_subvol:
899 kfree(*subvol_name);
900 *subvol_name = match_strdup(&args[0]);
901 if (!*subvol_name) {
902 error = -ENOMEM;
903 goto out;
904 }
905 break;
906 case Opt_subvolid:
907 error = match_u64(&args[0], &subvolid);
908 if (error)
909 goto out;
910 714
911 /* we want the original fs_tree */715 if (btrfs_test_opt(fs_info, NOSPACECACHE))
912 if (subvolid == 0)716 return;
913 subvolid = BTRFS_FS_TREE_OBJECTID;
914 717
915 *subvol_objectid = subvolid;718 /*
916 break;719 * At this point we don't have explicit options set by the user, set
917 default:720 * them ourselves based on the state of the file system.
918 break;721 */
919 }722 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
920 }723 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
724 else if (btrfs_free_space_cache_v1_active(fs_info))
725 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
726}
921 727
922out:728static void set_device_specific_options(struct btrfs_fs_info *fs_info)
923 kfree(orig);729{
924 return error;730 if (!btrfs_test_opt(fs_info, NOSSD) &&
731 !fs_info->fs_devices->rotating)
732 btrfs_set_opt(fs_info->mount_opt, SSD);
733
734 /*
735 * For devices supporting discard turn on discard=async automatically,
736 * unless it's already set or disabled. This could be turned off by
737 * nodiscard for the same mount.
738 *
739 * The zoned mode piggy backs on the discard functionality for
740 * resetting a zone. There is no reason to delay the zone reset as it is
741 * fast enough. So, do not enable async discard for zoned mode.
742 */
743 if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
744 btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
745 btrfs_test_opt(fs_info, NODISCARD)) &&
746 fs_info->fs_devices->discardable &&
747 !btrfs_is_zoned(fs_info))
748 btrfs_set_opt(fs_info->mount_opt, DISCARD_ASYNC);
925}749}
926 750
927char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,751char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
@@ -1105,10 +929,6 @@ static int btrfs_fill_super(struct super_block *sb, · 177 unmodified lines
1105#endif929#endif
1106 sb->s_xattr = btrfs_xattr_handlers;930 sb->s_xattr = btrfs_xattr_handlers;
1107 sb->s_time_gran = 1;931 sb->s_time_gran = 1;
1108#ifdef CONFIG_BTRFS_FS_POSIX_ACL
1109 sb->s_flags |= SB_POSIXACL;
1110#endif
1111 sb->s_flags |= SB_I_VERSION;
1112 sb->s_iflags |= SB_I_CGROUPWB;932 sb->s_iflags |= SB_I_CGROUPWB;
1113 933
1114 err = super_setup_bdi(sb);934 err = super_setup_bdi(sb);
@@ -1291,22 +1111,6 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) · 176 unmodified lines
1291 return 0;1111 return 0;
1292}1112}
1293 1113
1294static int btrfs_test_super(struct super_block *s, void *data)
1295{
1296 struct btrfs_fs_info *p = data;
1297 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1298
1299 return fs_info->fs_devices == p->fs_devices;
1300}
1301
1302static int btrfs_set_super(struct super_block *s, void *data)
1303{
1304 int err = set_anon_super(s, data);
1305 if (!err)
1306 s->s_fs_info = data;
1307 return err;
1308}
1309
1310/*1114/*
1311 * subvolumes are identified by ino 2561115 * subvolumes are identified by ino 256
1312 */1116 */
@@ -1382,307 +1186,299 @@ out: · 69 unmodified lines
1382 return root;1186 return root;
1383}1187}
1384 1188
1385/*1189static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1386 * Find a superblock for the given device / mount point.1190 u32 new_pool_size, u32 old_pool_size)
1387 *
1388 * Note: This is based on mount_bdev from fs/super.c with a few additions
1389 * for multiple device setup. Make sure to keep it in sync.
1390 */
1391static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1392 int flags, const char *device_name, void *data)
1393{1191{
1394 struct block_device *bdev = NULL;1192 if (new_pool_size == old_pool_size)
1395 struct super_block *s;1193 return;
1396 struct btrfs_device *device = NULL;
1397 struct btrfs_fs_devices *fs_devices = NULL;
1398 struct btrfs_fs_info *fs_info = NULL;
1399 void *new_sec_opts = NULL;
1400 blk_mode_t mode = sb_open_mode(flags);
1401 int error = 0;
1402
1403 if (data) {
1404 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1405 if (error)
1406 return ERR_PTR(error);
1407 }
1408 1194
1409 /* No support for restricting writes to btrfs devices yet... */1195 fs_info->thread_pool_size = new_pool_size;
1410 mode &= ~BLK_OPEN_RESTRICT_WRITES;
1411 /*
1412 * Setup a dummy root and fs_info for test/set super. This is because
1413 * we don't actually fill this stuff out until open_ctree, but we need
1414 * then open_ctree will properly initialize the file system specific
1415 * settings later. btrfs_init_fs_info initializes the static elements
1416 * of the fs_info (locks and such) to make cleanup easier if we find a
1417 * superblock with our given fs_devices later on at sget() time.
1418 */
1419 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1420 if (!fs_info) {
1421 error = -ENOMEM;
1422 goto error_sec_opts;
1423 }
1424 btrfs_init_fs_info(fs_info);
1425 1196
1426 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);1197 btrfs_info(fs_info, "resize thread pool %d -> %d",
1427 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);1198 old_pool_size, new_pool_size);
1428 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1429 error = -ENOMEM;
1430 goto error_fs_info;
1431 }
1432 1199
1433 mutex_lock(&uuid_mutex);1200 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1434 error = btrfs_parse_device_options(data, mode);1201 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1435 if (error) {1202 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1436 mutex_unlock(&uuid_mutex);1203 workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
1437 goto error_fs_info;1204 workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1205 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1206 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1207 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1208}
1209
1210static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1211 unsigned long old_opts, int flags)
1212{
1213 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1214 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1215 (flags & SB_RDONLY))) {
1216 /* wait for any defraggers to finish */
1217 wait_event(fs_info->transaction_wait,
1218 (atomic_read(&fs_info->defrag_running) == 0));
1219 if (flags & SB_RDONLY)
1220 sync_filesystem(fs_info->sb);
1438 }1221 }
1222}
1223
1224static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1225 unsigned long old_opts)
1226{
1227 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1439 1228
1440 /*1229 /*
1441 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect1230 * We need to cleanup all defragable inodes if the autodefragment is
1442 * either a valid device or an error.1231 * close or the filesystem is read only.
1443 */1232 */
1444 device = btrfs_scan_one_device(device_name, mode, true);1233 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1445 ASSERT(device != NULL);1234 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1446 if (IS_ERR(device)) {1235 btrfs_cleanup_defrag_inodes(fs_info);
1447 mutex_unlock(&uuid_mutex);
1448 error = PTR_ERR(device);
1449 goto error_fs_info;
1450 }1236 }
1451 1237
1452 fs_devices = device->fs_devices;1238 /* If we toggled discard async */
1453 fs_info->fs_devices = fs_devices;1239 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1240 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1241 btrfs_discard_resume(fs_info);
1242 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1243 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1244 btrfs_discard_cleanup(fs_info);
1454 1245
1455 error = btrfs_open_devices(fs_devices, mode, fs_type);1246 /* If we toggled space cache */
1456 mutex_unlock(&uuid_mutex);1247 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1457 if (error)1248 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1458 goto error_fs_info;1249}
1459 1250
1460 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {1251static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
1461 error = -EACCES;1252{
1462 goto error_close_devices;1253 int ret;
1463 }
1464 1254
1465 bdev = fs_devices->latest_dev->bdev;1255 if (BTRFS_FS_ERROR(fs_info)) {
1466 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,1256 btrfs_err(fs_info,
1467 fs_info);1257 "remounting read-write after error is not allowed");
1468 if (IS_ERR(s)) {1258 return -EINVAL;
1469 error = PTR_ERR(s);
1470 goto error_close_devices;
1471 }1259 }
1472 1260
1473 if (s->s_root) {1261 if (fs_info->fs_devices->rw_devices == 0)
1474 btrfs_close_devices(fs_devices);1262 return -EACCES;
1475 btrfs_free_fs_info(fs_info);1263
1476 if ((flags ^ s->s_flags) & SB_RDONLY)1264 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1477 error = -EBUSY;1265 btrfs_warn(fs_info,
1478 } else {1266 "too many missing devices, writable remount is not allowed");
1479 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);1267 return -EACCES;
1480 shrinker_debugfs_rename(s->s_shrink, "sb-%s:%s", fs_type->name,
1481 s->s_id);
1482 btrfs_sb(s)->bdev_holder = fs_type;
1483 error = btrfs_fill_super(s, fs_devices, data);
1484 }1268 }
1485 if (!error)1269
1486 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);1270 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1487 security_free_mnt_opts(&new_sec_opts);1271 btrfs_warn(fs_info,
1488 if (error) {1272 "mount required to replay tree-log, cannot remount read-write");
1489 deactivate_locked_super(s);1273 return -EINVAL;
1490 return ERR_PTR(error);
1491 }1274 }
1492 1275
1493 return dget(s->s_root);1276 /*
1277 * NOTE: when remounting with a change that does writes, don't put it
1278 * anywhere above this point, as we are not sure to be safe to write
1279 * until we pass the above checks.
1280 */
1281 ret = btrfs_start_pre_rw_mount(fs_info);
1282 if (ret)
1283 return ret;
1494 1284
1495error_close_devices:1285 btrfs_clear_sb_rdonly(fs_info->sb);
1496 btrfs_close_devices(fs_devices);1286
1497error_fs_info:1287 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1498 btrfs_free_fs_info(fs_info);1288
1499error_sec_opts:1289 /*
1500 security_free_mnt_opts(&new_sec_opts);1290 * If we've gone from readonly -> read-write, we need to get our
1501 return ERR_PTR(error);1291 * sync/async discard lists in the right state.
1292 */
1293 btrfs_discard_resume(fs_info);
1294
1295 return 0;
1502}1296}
1503 1297
1504/*1298static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
1505 * Mount function which is called by VFS layer.
1506 *
1507 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1508 * which needs vfsmount* of device's root (/). This means device's root has to
1509 * be mounted internally in any case.
1510 *
1511 * Operation flow:
1512 * 1. Parse subvol id related options for later use in mount_subvol().
1513 *
1514 * 2. Mount device's root (/) by calling vfs_kern_mount().
1515 *
1516 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1517 * first place. In order to avoid calling btrfs_mount() again, we use
1518 * different file_system_type which is not registered to VFS by
1519 * register_filesystem() (btrfs_root_fs_type). As a result,
1520 * btrfs_mount_root() is called. The return value will be used by
1521 * mount_subtree() in mount_subvol().
1522 *
1523 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1524 * "btrfs subvolume set-default", mount_subvol() is called always.
1525 */
1526static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1527 const char *device_name, void *data)
1528{1299{
1529 struct vfsmount *mnt_root;1300 /*
1530 struct dentry *root;1301 * This also happens on 'umount -rf' or on shutdown, when the
1531 char *subvol_name = NULL;1302 * filesystem is busy.
1532 u64 subvol_objectid = 0;1303 */
1533 int error = 0;1304 cancel_work_sync(&fs_info->async_reclaim_work);
1305 cancel_work_sync(&fs_info->async_data_reclaim_work);
1534 1306
1535 error = btrfs_parse_subvol_options(data, &subvol_name,1307 btrfs_discard_cleanup(fs_info);
1536 &subvol_objectid);
1537 if (error) {
1538 kfree(subvol_name);
1539 return ERR_PTR(error);
1540 }
1541 1308
1542 /* mount device's root (/) */1309 /* Wait for the uuid_scan task to finish */
1543 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);1310 down(&fs_info->uuid_tree_rescan_sem);
1544 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {1311 /* Avoid complains from lockdep et al. */
1545 if (flags & SB_RDONLY) {1312 up(&fs_info->uuid_tree_rescan_sem);
1546 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1547 flags & ~SB_RDONLY, device_name, data);
1548 } else {
1549 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1550 flags | SB_RDONLY, device_name, data);
1551 if (IS_ERR(mnt_root)) {
1552 root = ERR_CAST(mnt_root);
1553 kfree(subvol_name);
1554 goto out;
1555 }
1556 1313
1557 down_write(&mnt_root->mnt_sb->s_umount);1314 btrfs_set_sb_rdonly(fs_info->sb);
1558 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1559 up_write(&mnt_root->mnt_sb->s_umount);
1560 if (error < 0) {
1561 root = ERR_PTR(error);
1562 mntput(mnt_root);
1563 kfree(subvol_name);
1564 goto out;
1565 }
1566 }
1567 }
1568 if (IS_ERR(mnt_root)) {
1569 root = ERR_CAST(mnt_root);
1570 kfree(subvol_name);
1571 goto out;
1572 }
1573 1315
1574 /* mount_subvol() will free subvol_name and mnt_root */1316 /*
1575 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);1317 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
1318 * loop if it's already active. If it's already asleep, we'll leave
1319 * unused block groups on disk until we're mounted read-write again
1320 * unless we clean them up here.
1321 */
1322 btrfs_delete_unused_bgs(fs_info);
1576 1323
1577out:1324 /*
1578 return root;1325 * The cleaner task could be already running before we set the flag
1579}1326 * BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). We must make
1327 * sure that after we finish the remount, i.e. after we call
1328 * btrfs_commit_super(), the cleaner can no longer start a transaction
1329 * - either because it was dropping a dead root, running delayed iputs
1330 * or deleting an unused block group (the cleaner picked a block
1331 * group from the list of unused block groups before we were able to
1332 * in the previous call to btrfs_delete_unused_bgs()).
1333 */
1334 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
1580 1335
1581static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,1336 /*
1582 u32 new_pool_size, u32 old_pool_size)1337 * We've set the superblock to RO mode, so we might have made the
1583{1338 * cleaner task sleep without running all pending delayed iputs. Go
1584 if (new_pool_size == old_pool_size)1339 * through all the delayed iputs here, so that if an unmount happens
1585 return;1340 * without remounting RW we don't end up at finishing close_ctree()
1341 * with a non-empty list of delayed iputs.
1342 */
1343 btrfs_run_delayed_iputs(fs_info);
1586 1344
1587 fs_info->thread_pool_size = new_pool_size;1345 btrfs_dev_replace_suspend_for_unmount(fs_info);
1346 btrfs_scrub_cancel(fs_info);
1347 btrfs_pause_balance(fs_info);
1588 1348
1589 btrfs_info(fs_info, "resize thread pool %d -> %d",1349 /*
1590 old_pool_size, new_pool_size);1350 * Pause the qgroup rescan worker if it is running. We don't want it to
1351 * be still running after we are in RO mode, as after that, by the time
1352 * we unmount, it might have left a transaction open, so we would leak
1353 * the transaction and/or crash.
1354 */
1355 btrfs_qgroup_wait_for_completion(fs_info, false);
1591 1356
1592 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);1357 return btrfs_commit_super(fs_info);
1593 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);1358}
1594 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);1359
1595 workqueue_set_max_active(fs_info->endio_workers, new_pool_size);1360static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1596 workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);1361{
1597 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);1362 fs_info->max_inline = ctx->max_inline;
1598 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);1363 fs_info->commit_interval = ctx->commit_interval;
1599 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);1364 fs_info->metadata_ratio = ctx->metadata_ratio;
1365 fs_info->thread_pool_size = ctx->thread_pool_size;
1366 fs_info->mount_opt = ctx->mount_opt;
1367 fs_info->compress_type = ctx->compress_type;
1368 fs_info->compress_level = ctx->compress_level;
1600}1369}
1601 1370
1602static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,1371static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1603 unsigned long old_opts, int flags)
1604{1372{
1605 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&1373 ctx->max_inline = fs_info->max_inline;
1606 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||1374 ctx->commit_interval = fs_info->commit_interval;
1607 (flags & SB_RDONLY))) {1375 ctx->metadata_ratio = fs_info->metadata_ratio;
1608 /* wait for any defraggers to finish */1376 ctx->thread_pool_size = fs_info->thread_pool_size;
1609 wait_event(fs_info->transaction_wait,1377 ctx->mount_opt = fs_info->mount_opt;
1610 (atomic_read(&fs_info->defrag_running) == 0));1378 ctx->compress_type = fs_info->compress_type;
1611 if (flags & SB_RDONLY)1379 ctx->compress_level = fs_info->compress_level;
1612 sync_filesystem(fs_info->sb);
1613 }
1614}1380}
1615 1381
1616static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,1382#define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...) \
1617 unsigned long old_opts)1383do { \
1384 if ((!old_ctx || !btrfs_raw_test_opt(old_ctx->mount_opt, opt)) && \
1385 btrfs_raw_test_opt(fs_info->mount_opt, opt)) \
1386 btrfs_info(fs_info, fmt, ##args); \
1387} while (0)
1388
1389#define btrfs_info_if_unset(fs_info, old_ctx, opt, fmt, args...) \
1390do { \
1391 if ((old_ctx && btrfs_raw_test_opt(old_ctx->mount_opt, opt)) && \
1392 !btrfs_raw_test_opt(fs_info->mount_opt, opt)) \
1393 btrfs_info(fs_info, fmt, ##args); \
1394} while (0)
1395
1396static void btrfs_emit_options(struct btrfs_fs_info *info,
1397 struct btrfs_fs_context *old)
1618{1398{
1619 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);1399 btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1620 1400 btrfs_info_if_set(info, old, DEGRADED, "allowing degraded mounts");
1621 /*1401 btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1622 * We need to cleanup all defragable inodes if the autodefragment is1402 btrfs_info_if_set(info, old, SSD, "enabling ssd optimizations");
1623 * close or the filesystem is read only.1403 btrfs_info_if_set(info, old, SSD_SPREAD, "using spread ssd allocation scheme");
1624 */1404 btrfs_info_if_set(info, old, NOBARRIER, "turning off barriers");
1625 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&1405 btrfs_info_if_set(info, old, NOTREELOG, "disabling tree log");
1626 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {1406 btrfs_info_if_set(info, old, NOLOGREPLAY, "disabling log replay at mount time");
1627 btrfs_cleanup_defrag_inodes(fs_info);1407 btrfs_info_if_set(info, old, FLUSHONCOMMIT, "turning on flush-on-commit");
1408 btrfs_info_if_set(info, old, DISCARD_SYNC, "turning on sync discard");
1409 btrfs_info_if_set(info, old, DISCARD_ASYNC, "turning on async discard");
1410 btrfs_info_if_set(info, old, FREE_SPACE_TREE, "enabling free space tree");
1411 btrfs_info_if_set(info, old, SPACE_CACHE, "enabling disk space caching");
1412 btrfs_info_if_set(info, old, CLEAR_CACHE, "force clearing of disk cache");
1413 btrfs_info_if_set(info, old, AUTO_DEFRAG, "enabling auto defrag");
1414 btrfs_info_if_set(info, old, FRAGMENT_DATA, "fragmenting data");
1415 btrfs_info_if_set(info, old, FRAGMENT_METADATA, "fragmenting metadata");
1416 btrfs_info_if_set(info, old, REF_VERIFY, "doing ref verification");
1417 btrfs_info_if_set(info, old, USEBACKUPROOT, "trying to use backup root at mount time");
1418 btrfs_info_if_set(info, old, IGNOREBADROOTS, "ignoring bad roots");
1419 btrfs_info_if_set(info, old, IGNOREDATACSUMS, "ignoring data csums");
1420
1421 btrfs_info_if_unset(info, old, NODATACOW, "setting datacow");
1422 btrfs_info_if_unset(info, old, SSD, "not using ssd optimizations");
1423 btrfs_info_if_unset(info, old, SSD_SPREAD, "not using spread ssd allocation scheme");
1424 btrfs_info_if_unset(info, old, NOBARRIER, "turning off barriers");
1425 btrfs_info_if_unset(info, old, NOTREELOG, "enabling tree log");
1426 btrfs_info_if_unset(info, old, SPACE_CACHE, "disabling disk space caching");
1427 btrfs_info_if_unset(info, old, FREE_SPACE_TREE, "disabling free space tree");
1428 btrfs_info_if_unset(info, old, AUTO_DEFRAG, "disabling auto defrag");
1429 btrfs_info_if_unset(info, old, COMPRESS, "use no compression");
1430
1431 /* Did the compression settings change? */
1432 if (btrfs_test_opt(info, COMPRESS) &&
1433 (!old ||
1434 old->compress_type != info->compress_type ||
1435 old->compress_level != info->compress_level ||
1436 (!btrfs_raw_test_opt(old->mount_opt, FORCE_COMPRESS) &&
1437 btrfs_raw_test_opt(info->mount_opt, FORCE_COMPRESS)))) {
1438 const char *compress_type = btrfs_compress_type2str(info->compress_type);
1439
1440 btrfs_info(info, "%s %s compression, level %d",
1441 btrfs_test_opt(info, FORCE_COMPRESS) ? "force" : "use",
1442 compress_type, info->compress_level);
1628 }1443 }
1629 1444
1630 /* If we toggled discard async */1445 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1631 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&1446 btrfs_info(info, "max_inline set to %llu", info->max_inline);
1632 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1633 btrfs_discard_resume(fs_info);
1634 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1635 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1636 btrfs_discard_cleanup(fs_info);
1637
1638 /* If we toggled space cache */
1639 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1640 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1641}1447}
1642 1448
1643static int btrfs_remount(struct super_block *sb, int *flags, char *data)1449static int btrfs_reconfigure(struct fs_context *fc)
1644{1450{
1451 struct super_block *sb = fc->root->d_sb;
1645 struct btrfs_fs_info *fs_info = btrfs_sb(sb);1452 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1646 unsigned old_flags = sb->s_flags;1453 struct btrfs_fs_context *ctx = fc->fs_private;
1647 unsigned long old_opts = fs_info->mount_opt;1454 struct btrfs_fs_context old_ctx;
1648 unsigned long old_compress_type = fs_info->compress_type;1455 int ret = 0;
1649 u64 old_max_inline = fs_info->max_inline;1456 bool mount_reconfigure = (fc->s_fs_info != NULL);
1650 u32 old_thread_pool_size = fs_info->thread_pool_size;1457
1651 u32 old_metadata_ratio = fs_info->metadata_ratio;1458 btrfs_info_to_ctx(fs_info, &old_ctx);
1652 int ret;
1653 1459
1654 sync_filesystem(sb);1460 sync_filesystem(sb);
1655 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);1461 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1656 1462
1657 if (data) {1463 if (!mount_reconfigure &&
1658 void *new_sec_opts = NULL;1464 !btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
1659 1465 return -EINVAL;
1660 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1661 if (!ret)
1662 ret = security_sb_remount(sb, new_sec_opts);
1663 security_free_mnt_opts(&new_sec_opts);
1664 if (ret)
1665 goto restore;
1666 }
1667
1668 ret = btrfs_parse_options(fs_info, data, *flags);
1669 if (ret)
1670 goto restore;
1671 1466
1672 ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));1467 ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
1673 if (ret < 0)1468 if (ret < 0)
1674 goto restore;1469 return ret;
1675 1470
1676 btrfs_remount_begin(fs_info, old_opts, *flags);1471 btrfs_ctx_to_info(fs_info, ctx);
1677 btrfs_resize_thread_pool(fs_info,1472 btrfs_remount_begin(fs_info, old_ctx.mount_opt, fc->sb_flags);
1678 fs_info->thread_pool_size, old_thread_pool_size);1473 btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size,
1474 old_ctx.thread_pool_size);
1679 1475
1680 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=1476 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1681 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&1477 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1682 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {1478 (!sb_rdonly(sb) || (fc->sb_flags & SB_RDONLY))) {
1683 btrfs_warn(fs_info,1479 btrfs_warn(fs_info,
1684 "remount supports changing free space tree only from ro to rw");1480 "remount supports changing free space tree only from RO to RW");
1685 /* Make sure free space cache options match the state on disk */1481 /* Make sure free space cache options match the state on disk. */
1686 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {1482 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
1687 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);1483 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1688 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);1484 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
@@ -1693,149 +1489,33 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data) · 4 unmodified lines
1693 }1489 }
1694 }1490 }
1695 1491
1696 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))1492 ret = 0;
1697 goto out;1493 if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY))
1698 1494 ret = btrfs_remount_ro(fs_info);
1699 if (*flags & SB_RDONLY) {1495 else if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY))
1700 /*1496 ret = btrfs_remount_rw(fs_info);
1701 * this also happens on 'umount -rf' or on shutdown, when1497 if (ret)
1702 * the filesystem is busy.1498 goto restore;
1703 */
1704 cancel_work_sync(&fs_info->async_reclaim_work);
1705 cancel_work_sync(&fs_info->async_data_reclaim_work);
1706
1707 btrfs_discard_cleanup(fs_info);
1708
1709 /* wait for the uuid_scan task to finish */
1710 down(&fs_info->uuid_tree_rescan_sem);
1711 /* avoid complains from lockdep et al. */
1712 up(&fs_info->uuid_tree_rescan_sem);
1713
1714 btrfs_set_sb_rdonly(sb);
1715
1716 /*
1717 * Setting SB_RDONLY will put the cleaner thread to
1718 * sleep at the next loop if it's already active.
1719 * If it's already asleep, we'll leave unused block
1720 * groups on disk until we're mounted read-write again
1721 * unless we clean them up here.
1722 */
1723 btrfs_delete_unused_bgs(fs_info);
1724
1725 /*
1726 * The cleaner task could be already running before we set the
1727 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1728 * We must make sure that after we finish the remount, i.e. after
1729 * we call btrfs_commit_super(), the cleaner can no longer start
1730 * a transaction - either because it was dropping a dead root,
1731 * running delayed iputs or deleting an unused block group (the
1732 * cleaner picked a block group from the list of unused block
1733 * groups before we were able to in the previous call to
1734 * btrfs_delete_unused_bgs()).
1735 */
1736 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1737 TASK_UNINTERRUPTIBLE);
1738
1739 /*
1740 * We've set the superblock to RO mode, so we might have made
1741 * the cleaner task sleep without running all pending delayed
1742 * iputs. Go through all the delayed iputs here, so that if an
1743 * unmount happens without remounting RW we don't end up at
1744 * finishing close_ctree() with a non-empty list of delayed
1745 * iputs.
1746 */
1747 btrfs_run_delayed_iputs(fs_info);
1748
1749 btrfs_dev_replace_suspend_for_unmount(fs_info);
1750 btrfs_scrub_cancel(fs_info);
1751 btrfs_pause_balance(fs_info);
1752
1753 /*
1754 * Pause the qgroup rescan worker if it is running. We don't want
1755 * it to be still running after we are in RO mode, as after that,
1756 * by the time we unmount, it might have left a transaction open,
1757 * so we would leak the transaction and/or crash.
1758 */
1759 btrfs_qgroup_wait_for_completion(fs_info, false);
1760
1761 ret = btrfs_commit_super(fs_info);
1762 if (ret)
1763 goto restore;
1764 } else {
1765 if (BTRFS_FS_ERROR(fs_info)) {
1766 btrfs_err(fs_info,
1767 "Remounting read-write after error is not allowed");
1768 ret = -EINVAL;
1769 goto restore;
1770 }
1771 if (fs_info->fs_devices->rw_devices == 0) {
1772 ret = -EACCES;
1773 goto restore;
1774 }
1775
1776 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1777 btrfs_warn(fs_info,
1778 "too many missing devices, writable remount is not allowed");
1779 ret = -EACCES;
1780 goto restore;
1781 }
1782
1783 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1784 btrfs_warn(fs_info,
1785 "mount required to replay tree-log, cannot remount read-write");
1786 ret = -EINVAL;
1787 goto restore;
1788 }
1789
1790 /*
1791 * NOTE: when remounting with a change that does writes, don't
1792 * put it anywhere above this point, as we are not sure to be
1793 * safe to write until we pass the above checks.
1794 */
1795 ret = btrfs_start_pre_rw_mount(fs_info);
1796 if (ret)
1797 goto restore;
1798
1799 btrfs_clear_sb_rdonly(sb);
1800
1801 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1802 1499
1803 /*
1804 * If we've gone from readonly -> read/write, we need to get
1805 * our sync/async discard lists in the right state.
1806 */
1807 btrfs_discard_resume(fs_info);
1808 }
1809out:
1810 /*1500 /*
1811 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,1501 * If we set the mask during the parameter parsing VFS would reject the
1812 * since the absence of the flag means it can be toggled off by remount.1502 * remount. Here we can set the mask and the value will be updated
1503 * appropriately.
1813 */1504 */
1814 *flags |= SB_I_VERSION;1505 if ((fc->sb_flags & SB_POSIXACL) != (sb->s_flags & SB_POSIXACL))
1506 fc->sb_flags_mask |= SB_POSIXACL;
1815 1507
1508 btrfs_emit_options(fs_info, &old_ctx);
1816 wake_up_process(fs_info->transaction_kthread);1509 wake_up_process(fs_info->transaction_kthread);
1817 btrfs_remount_cleanup(fs_info, old_opts);1510 btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
1818 btrfs_clear_oneshot_options(fs_info);1511 btrfs_clear_oneshot_options(fs_info);
1819 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);1512 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1820 1513
1821 return 0;1514 return 0;
1822
1823restore:1515restore:
1824 /* We've hit an error - don't reset SB_RDONLY */1516 btrfs_ctx_to_info(fs_info, &old_ctx);
1825 if (sb_rdonly(sb))1517 btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
1826 old_flags |= SB_RDONLY;
1827 if (!(old_flags & SB_RDONLY))
1828 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
1829 sb->s_flags = old_flags;
1830 fs_info->mount_opt = old_opts;
1831 fs_info->compress_type = old_compress_type;
1832 fs_info->max_inline = old_max_inline;
1833 btrfs_resize_thread_pool(fs_info,
1834 old_thread_pool_size, fs_info->thread_pool_size);
1835 fs_info->metadata_ratio = old_metadata_ratio;
1836 btrfs_remount_cleanup(fs_info, old_opts);
1837 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);1518 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1838
1839 return ret;1519 return ret;
1840}1520}
1841 1521
@@ -2096,6 +1776,309 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) · 254 unmodified lines
2096 return 0;1776 return 0;
2097}1777}
2098 1778
1779static int btrfs_fc_test_super(struct super_block *sb, struct fs_context *fc)
1780{
1781 struct btrfs_fs_info *p = fc->s_fs_info;
1782 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1783
1784 return fs_info->fs_devices == p->fs_devices;
1785}
1786
1787static int btrfs_get_tree_super(struct fs_context *fc)
1788{
1789 struct btrfs_fs_info *fs_info = fc->s_fs_info;
1790 struct btrfs_fs_context *ctx = fc->fs_private;
1791 struct btrfs_fs_devices *fs_devices = NULL;
1792 struct block_device *bdev;
1793 struct btrfs_device *device;
1794 struct super_block *sb;
1795 blk_mode_t mode = btrfs_open_mode(fc);
1796 int ret;
1797
1798 btrfs_ctx_to_info(fs_info, ctx);
1799 mutex_lock(&uuid_mutex);
1800
1801 /*
1802 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
1803 * either a valid device or an error.
1804 */
1805 device = btrfs_scan_one_device(fc->source, mode, true);
1806 ASSERT(device != NULL);
1807 if (IS_ERR(device)) {
1808 mutex_unlock(&uuid_mutex);
1809 return PTR_ERR(device);
1810 }
1811
1812 fs_devices = device->fs_devices;
1813 fs_info->fs_devices = fs_devices;
1814
1815 ret = btrfs_open_devices(fs_devices, mode, &btrfs_fs_type);
1816 mutex_unlock(&uuid_mutex);
1817 if (ret)
1818 return ret;
1819
1820 if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1821 ret = -EACCES;
1822 goto error;
1823 }
1824
1825 bdev = fs_devices->latest_dev->bdev;
1826
1827 /*
1828 * From now on the error handling is not straightforward.
1829 *
1830 * If successful, this will transfer the fs_info into the super block,
1831 * and fc->s_fs_info will be NULL. However if there's an existing
1832 * super, we'll still have fc->s_fs_info populated. If we error
1833 * completely out it'll be cleaned up when we drop the fs_context,
1834 * otherwise it's tied to the lifetime of the super_block.
1835 */
1836 sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
1837 if (IS_ERR(sb)) {
1838 ret = PTR_ERR(sb);
1839 goto error;
1840 }
1841
1842 set_device_specific_options(fs_info);
1843
1844 if (sb->s_root) {
1845 btrfs_close_devices(fs_devices);
1846 if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY)
1847 ret = -EBUSY;
1848 } else {
1849 snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
1850 shrinker_debugfs_rename(sb->s_shrink, "sb-btrfs:%s", sb->s_id);
1851 btrfs_sb(sb)->bdev_holder = &btrfs_fs_type;
1852 ret = btrfs_fill_super(sb, fs_devices, NULL);
1853 }
1854
1855 if (ret) {
1856 deactivate_locked_super(sb);
1857 return ret;
1858 }
1859
1860 btrfs_clear_oneshot_options(fs_info);
1861
1862 fc->root = dget(sb->s_root);
1863 return 0;
1864
1865error:
1866 btrfs_close_devices(fs_devices);
1867 return ret;
1868}
1869
1870/*
1871 * Ever since commit 0723a0473fb4 ("btrfs: allow mounting btrfs subvolumes
1872 * with different ro/rw options") the following works:
1873 *
1874 * (i) mount /dev/sda3 -o subvol=foo,ro /mnt/foo
1875 * (ii) mount /dev/sda3 -o subvol=bar,rw /mnt/bar
1876 *
1877 * which looks nice and innocent but is actually pretty intricate and deserves
1878 * a long comment.
1879 *
1880 * On another filesystem a subvolume mount is close to something like:
1881 *
1882 * (iii) # create rw superblock + initial mount
1883 * mount -t xfs /dev/sdb /opt/
1884 *
1885 * # create ro bind mount
1886 * mount --bind -o ro /opt/foo /mnt/foo
1887 *
1888 * # unmount initial mount
1889 * umount /opt
1890 *
1891 * Of course, there's some special subvolume sauce and there's the fact that the
1892 * sb->s_root dentry is really swapped after mount_subtree(). But conceptually
1893 * it's very close and will help us understand the issue.
1894 *
1895 * The old mount API didn't cleanly distinguish between a mount being made ro
1896 * and a superblock being made ro. The only way to change the ro state of
1897 * either object was by passing ms_rdonly. If a new mount was created via
1898 * mount(2) such as:
1899 *
1900 * mount("/dev/sdb", "/mnt", "xfs", ms_rdonly, null);
1901 *
1902 * the MS_RDONLY flag being specified had two effects:
1903 *
1904 * (1) MNT_READONLY was raised -> the resulting mount got
1905 * @mnt->mnt_flags |= MNT_READONLY raised.
1906 *
1907 * (2) MS_RDONLY was passed to the filesystem's mount method and the filesystems
1908 * made the superblock ro. Note, how SB_RDONLY has the same value as
1909 * ms_rdonly and is raised whenever MS_RDONLY is passed through mount(2).
1910 *
1911 * Creating a subtree mount via (iii) ends up leaving a rw superblock with a
1912 * subtree mounted ro.
1913 *
1914 * But consider the effect on the old mount API on btrfs subvolume mounting
1915 * which combines the distinct step in (iii) into a single step.
1916 *
1917 * By issuing (i) both the mount and the superblock are turned ro. Now when (ii)
1918 * is issued the superblock is ro and thus even if the mount created for (ii) is
1919 * rw it wouldn't help. Hence, btrfs needed to transition the superblock from ro
1920 * to rw for (ii) which it did using an internal remount call.
1921 *
1922 * IOW, subvolume mounting was inherently complicated due to the ambiguity of
1923 * MS_RDONLY in mount(2). Note, this ambiguity has mount(8) always translate
1924 * "ro" to MS_RDONLY. IOW, in both (i) and (ii) "ro" becomes MS_RDONLY when
1925 * passed by mount(8) to mount(2).
1926 *
1927 * Enter the new mount API. The new mount API disambiguates making a mount ro
1928 * and making a superblock ro.
1929 *
1930 * (3) To turn a mount ro the MOUNT_ATTR_ONLY flag can be used with either
1931 * fsmount() or mount_setattr() this is a pure VFS level change for a
1932 * specific mount or mount tree that is never seen by the filesystem itself.
1933 *
1934 * (4) To turn a superblock ro the "ro" flag must be used with
1935 * fsconfig(FSCONFIG_SET_FLAG, "ro"). This option is seen by the filesystem
1936 * in fc->sb_flags.
1937 *
1938 * This disambiguation has rather positive consequences. Mounting a subvolume
1939 * ro will not also turn the superblock ro. Only the mount for the subvolume
1940 * will become ro.
1941 *
1942 * So, if the superblock creation request comes from the new mount API the
1943 * caller must have explicitly done:
1944 *
1945 * fsconfig(FSCONFIG_SET_FLAG, "ro")
1946 * fsmount/mount_setattr(MOUNT_ATTR_RDONLY)
1947 *
1948 * IOW, at some point the caller must have explicitly turned the whole
1949 * superblock ro and we shouldn't just undo it like we did for the old mount
1950 * API. In any case, it lets us avoid the hack in the new mount API.
1951 *
1952 * Consequently, the remounting hack must only be used for requests originating
1953 * from the old mount API and should be marked for full deprecation so it can be
1954 * turned off in a couple of years.
1955 *
1956 * The new mount API has no reason to support this hack.
1957 */
1958static struct vfsmount *btrfs_reconfigure_for_mount(struct fs_context *fc)
1959{
1960 struct vfsmount *mnt;
1961 int ret;
1962 const bool ro2rw = !(fc->sb_flags & SB_RDONLY);
1963
1964 /*
1965 * We got an EBUSY because our SB_RDONLY flag didn't match the existing
1966 * super block, so invert our setting here and retry the mount so we
1967 * can get our vfsmount.
1968 */
1969 if (ro2rw)
1970 fc->sb_flags |= SB_RDONLY;
1971 else
1972 fc->sb_flags &= ~SB_RDONLY;
1973
1974 mnt = fc_mount(fc);
1975 if (IS_ERR(mnt))
1976 return mnt;
1977
1978 if (!fc->oldapi || !ro2rw)
1979 return mnt;
1980
1981 /* We need to convert to rw, call reconfigure. */
1982 fc->sb_flags &= ~SB_RDONLY;
1983 down_write(&mnt->mnt_sb->s_umount);
1984 ret = btrfs_reconfigure(fc);
1985 up_write(&mnt->mnt_sb->s_umount);
1986 if (ret) {
1987 mntput(mnt);
1988 return ERR_PTR(ret);
1989 }
1990 return mnt;
1991}
1992
1993static int btrfs_get_tree_subvol(struct fs_context *fc)
1994{
1995 struct btrfs_fs_info *fs_info = NULL;
1996 struct btrfs_fs_context *ctx = fc->fs_private;
1997 struct fs_context *dup_fc;
1998 struct dentry *dentry;
1999 struct vfsmount *mnt;
2000
2001 /*
2002 * Setup a dummy root and fs_info for test/set super. This is because
2003 * we don't actually fill this stuff out until open_ctree, but we need
2004 * then open_ctree will properly initialize the file system specific
2005 * settings later. btrfs_init_fs_info initializes the static elements
2006 * of the fs_info (locks and such) to make cleanup easier if we find a
2007 * superblock with our given fs_devices later on at sget() time.
2008 */
2009 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
2010 if (!fs_info)
2011 return -ENOMEM;
2012
2013 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
2014 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
2015 if (!fs_info->super_copy || !fs_info->super_for_commit) {
2016 btrfs_free_fs_info(fs_info);
2017 return -ENOMEM;
2018 }
2019 btrfs_init_fs_info(fs_info);
2020
2021 dup_fc = vfs_dup_fs_context(fc);
2022 if (IS_ERR(dup_fc)) {
2023 btrfs_free_fs_info(fs_info);
2024 return PTR_ERR(dup_fc);
2025 }
2026
2027 /*
2028 * When we do the sget_fc this gets transferred to the sb, so we only
2029 * need to set it on the dup_fc as this is what creates the super block.
2030 */
2031 dup_fc->s_fs_info = fs_info;
2032
2033 /*
2034 * We'll do the security settings in our btrfs_get_tree_super() mount
2035 * loop, they were duplicated into dup_fc, we can drop the originals
2036 * here.
2037 */
2038 security_free_mnt_opts(&fc->security);
2039 fc->security = NULL;
2040
2041 mnt = fc_mount(dup_fc);
2042 if (PTR_ERR_OR_ZERO(mnt) == -EBUSY)
2043 mnt = btrfs_reconfigure_for_mount(dup_fc);
2044 put_fs_context(dup_fc);
2045 if (IS_ERR(mnt))
2046 return PTR_ERR(mnt);
2047
2048 /*
2049 * This free's ->subvol_name, because if it isn't set we have to
2050 * allocate a buffer to hold the subvol_name, so we just drop our
2051 * reference to it here.
2052 */
2053 dentry = mount_subvol(ctx->subvol_name, ctx->subvol_objectid, mnt);
2054 ctx->subvol_name = NULL;
2055 if (IS_ERR(dentry))
2056 return PTR_ERR(dentry);
2057
2058 fc->root = dentry;
2059 return 0;
2060}
2061
2062static int btrfs_get_tree(struct fs_context *fc)
2063{
2064 /*
2065 * Since we use mount_subtree to mount the default/specified subvol, we
2066 * have to do mounts in two steps.
2067 *
2068 * First pass through we call btrfs_get_tree_subvol(), this is just a
2069 * wrapper around fc_mount() to call back into here again, and this time
2070 * we'll call btrfs_get_tree_super(). This will do the open_ctree() and
2071 * everything to open the devices and file system. Then we return back
2072 * with a fully constructed vfsmount in btrfs_get_tree_subvol(), and
2073 * from there we can do our mount_subvol() call, which will lookup
2074 * whichever subvol we're mounting and setup this fc with the
2075 * appropriate dentry for the subvol.
2076 */
2077 if (fc->s_fs_info)
2078 return btrfs_get_tree_super(fc);
2079 return btrfs_get_tree_subvol(fc);
2080}
2081
2099static void btrfs_kill_super(struct super_block *sb)2082static void btrfs_kill_super(struct super_block *sb)
2100{2083{
2101 struct btrfs_fs_info *fs_info = btrfs_sb(sb);2084 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
@@ -2103,22 +2086,85 @@ static void btrfs_kill_super(struct super_block *sb) · 1 unmodified line
2103 btrfs_free_fs_info(fs_info);2086 btrfs_free_fs_info(fs_info);
2104}2087}
2105 2088
2106static struct file_system_type btrfs_fs_type = {2089static void btrfs_free_fs_context(struct fs_context *fc)
2107 .owner = THIS_MODULE,2090{
2108 .name = "btrfs",2091 struct btrfs_fs_context *ctx = fc->fs_private;
2109 .mount = btrfs_mount,2092 struct btrfs_fs_info *fs_info = fc->s_fs_info;
2110 .kill_sb = btrfs_kill_super,2093
2111 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,2094 if (fs_info)
2112};2095 btrfs_free_fs_info(fs_info);
2096
2097 if (ctx && refcount_dec_and_test(&ctx->refs)) {
2098 kfree(ctx->subvol_name);
2099 kfree(ctx);
2100 }
2101}
2102
2103static int btrfs_dup_fs_context(struct fs_context *fc, struct fs_context *src_fc)
2104{
2105 struct btrfs_fs_context *ctx = src_fc->fs_private;
2106
2107 /*
2108 * Give a ref to our ctx to this dup, as we want to keep it around for
2109 * our original fc so we can have the subvolume name or objectid.
2110 *
2111 * We unset ->source in the original fc because the dup needs it for
2112 * mounting, and then once we free the dup it'll free ->source, so we
2113 * need to make sure we're only pointing to it in one fc.
2114 */
2115 refcount_inc(&ctx->refs);
2116 fc->fs_private = ctx;
2117 fc->source = src_fc->source;
2118 src_fc->source = NULL;
2119 return 0;
2120}
2113 2121
2114static struct file_system_type btrfs_root_fs_type = {2122static const struct fs_context_operations btrfs_fs_context_ops = {
2115 .owner = THIS_MODULE,2123 .parse_param = btrfs_parse_param,
2116 .name = "btrfs",2124 .reconfigure = btrfs_reconfigure,
2117 .mount = btrfs_mount_root,2125 .get_tree = btrfs_get_tree,
2118 .kill_sb = btrfs_kill_super,2126 .dup = btrfs_dup_fs_context,
2119 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,2127 .free = btrfs_free_fs_context,
2120};2128};
2121 2129
2130static int btrfs_init_fs_context(struct fs_context *fc)
2131{
2132 struct btrfs_fs_context *ctx;
2133
2134 ctx = kzalloc(sizeof(struct btrfs_fs_context), GFP_KERNEL);
2135 if (!ctx)
2136 return -ENOMEM;
2137
2138 refcount_set(&ctx->refs, 1);
2139 fc->fs_private = ctx;
2140 fc->ops = &btrfs_fs_context_ops;
2141
2142 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2143 btrfs_info_to_ctx(btrfs_sb(fc->root->d_sb), ctx);
2144 } else {
2145 ctx->thread_pool_size =
2146 min_t(unsigned long, num_online_cpus() + 2, 8);
2147 ctx->max_inline = BTRFS_DEFAULT_MAX_INLINE;
2148 ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
2149 }
2150
2151#ifdef CONFIG_BTRFS_FS_POSIX_ACL
2152 fc->sb_flags |= SB_POSIXACL;
2153#endif
2154 fc->sb_flags |= SB_I_VERSION;
2155
2156 return 0;
2157}
2158
2159static struct file_system_type btrfs_fs_type = {
2160 .owner = THIS_MODULE,
2161 .name = "btrfs",
2162 .init_fs_context = btrfs_init_fs_context,
2163 .parameters = btrfs_fs_parameters,
2164 .kill_sb = btrfs_kill_super,
2165 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2166 };
2167
2122MODULE_ALIAS_FS("btrfs");2168MODULE_ALIAS_FS("btrfs");
2123 2169
2124static int btrfs_control_open(struct inode *inode, struct file *file)2170static int btrfs_control_open(struct inode *inode, struct file *file)
@@ -2330,7 +2376,6 @@ static const struct super_operations btrfs_super_ops = { · 205 unmodified lines
2330 .destroy_inode = btrfs_destroy_inode,2376 .destroy_inode = btrfs_destroy_inode,
2331 .free_inode = btrfs_free_inode,2377 .free_inode = btrfs_free_inode,
2332 .statfs = btrfs_statfs,2378 .statfs = btrfs_statfs,
2333 .remount_fs = btrfs_remount,
2334 .freeze_fs = btrfs_freeze,2379 .freeze_fs = btrfs_freeze,
2335 .unfreeze_fs = btrfs_unfreeze,2380 .unfreeze_fs = btrfs_unfreeze,
2336};2381};
fs/btrfs/super.h+3−2
@@ -3,11 +3,12 @@ · 2 unmodified lines
3#ifndef BTRFS_SUPER_H3#ifndef BTRFS_SUPER_H
4#define BTRFS_SUPER_H4#define BTRFS_SUPER_H
5 5
6int btrfs_parse_options(struct btrfs_fs_info *info, char *options,6bool btrfs_check_options(struct btrfs_fs_info *info, unsigned long *mount_opt,
7 unsigned long new_flags);7 unsigned long flags);
8int btrfs_sync_fs(struct super_block *sb, int wait);8int btrfs_sync_fs(struct super_block *sb, int wait);
9char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,9char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
10 u64 subvol_objectid);10 u64 subvol_objectid);
11void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info);
11 12
12static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)13static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
13{14{
fs/btrfs/sysfs.c+4−0
@@ -1783,6 +1783,10 @@ static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj, · 1782 unmodified lines
1783 unsigned long long limit;1783 unsigned long long limit;
1784 1784
1785 limit = memparse(buf, &endptr);1785 limit = memparse(buf, &endptr);
1786 /* There could be trailing '\n', also catch any typos after the value. */
1787 endptr = skip_spaces(endptr);
1788 if (*endptr != 0)
1789 return -EINVAL;
1786 WRITE_ONCE(device->scrub_speed_max, limit);1790 WRITE_ONCE(device->scrub_speed_max, limit);
1787 return len;1791 return len;
1788}1792}
fs/btrfs/tests/btrfs-tests.c+3−2
@@ -28,6 +28,7 @@ const char *test_error[] = { · 27 unmodified lines
28 [TEST_ALLOC_INODE] = "cannot allocate inode",28 [TEST_ALLOC_INODE] = "cannot allocate inode",
29 [TEST_ALLOC_BLOCK_GROUP] = "cannot allocate block group",29 [TEST_ALLOC_BLOCK_GROUP] = "cannot allocate block group",
30 [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map",30 [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map",
31 [TEST_ALLOC_CHUNK_MAP] = "cannot allocate chunk map",
31};32};
32 33
33static const struct super_operations btrfs_test_super_ops = {34static const struct super_operations btrfs_test_super_ops = {
@@ -102,7 +103,7 @@ struct btrfs_device *btrfs_alloc_dummy_device(struct btrfs_fs_info *fs_info) · 68 unmodified lines
102 if (!dev)103 if (!dev)
103 return ERR_PTR(-ENOMEM);104 return ERR_PTR(-ENOMEM);
104 105
105 extent_io_tree_init(NULL, &dev->alloc_state, 0);106 extent_io_tree_init(fs_info, &dev->alloc_state, 0);
106 INIT_LIST_HEAD(&dev->dev_list);107 INIT_LIST_HEAD(&dev->dev_list);
107 list_add(&dev->dev_list, &fs_info->fs_devices->devices);108 list_add(&dev->dev_list, &fs_info->fs_devices->devices);
108 109
@@ -185,7 +186,7 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) · 76 unmodified lines
185 }186 }
186 spin_unlock(&fs_info->buffer_lock);187 spin_unlock(&fs_info->buffer_lock);
187 188
188 btrfs_mapping_tree_free(&fs_info->mapping_tree);189 btrfs_mapping_tree_free(fs_info);
189 list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,190 list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
190 dev_list) {191 dev_list) {
191 btrfs_free_dummy_device(dev);192 btrfs_free_dummy_device(dev);
fs/btrfs/tests/btrfs-tests.h+1−0
@@ -23,6 +23,7 @@ enum { · 22 unmodified lines
23 TEST_ALLOC_INODE,23 TEST_ALLOC_INODE,
24 TEST_ALLOC_BLOCK_GROUP,24 TEST_ALLOC_BLOCK_GROUP,
25 TEST_ALLOC_EXTENT_MAP,25 TEST_ALLOC_EXTENT_MAP,
26 TEST_ALLOC_CHUNK_MAP,
26};27};
27 28
28extern const char *test_error[];29extern const char *test_error[];
fs/btrfs/tests/extent-io-tests.c+2−2
@@ -652,7 +652,7 @@ static void dump_eb_and_memory_contents(struct extent_buffer *eb, void *memory, · 651 unmodified lines
652 const char *test_name)652 const char *test_name)
653{653{
654 for (int i = 0; i < eb->len; i++) {654 for (int i = 0; i < eb->len; i++) {
655 struct page *page = eb->pages[i >> PAGE_SHIFT];655 struct page *page = folio_page(eb->folios[i >> PAGE_SHIFT], 0);
656 void *addr = page_address(page) + offset_in_page(i);656 void *addr = page_address(page) + offset_in_page(i);
657 657
658 if (memcmp(addr, memory + i, 1) != 0) {658 if (memcmp(addr, memory + i, 1) != 0) {
@@ -668,7 +668,7 @@ static int verify_eb_and_memory(struct extent_buffer *eb, void *memory, · 9 unmodified lines
668 const char *test_name)668 const char *test_name)
669{669{
670 for (int i = 0; i < (eb->len >> PAGE_SHIFT); i++) {670 for (int i = 0; i < (eb->len >> PAGE_SHIFT); i++) {
671 void *eb_addr = page_address(eb->pages[i]);671 void *eb_addr = folio_address(eb->folios[i]);
672 672
673 if (memcmp(memory + (i << PAGE_SHIFT), eb_addr, PAGE_SIZE) != 0) {673 if (memcmp(memory + (i << PAGE_SHIFT), eb_addr, PAGE_SIZE) != 0) {
674 dump_eb_and_memory_contents(eb, memory, test_name);674 dump_eb_and_memory_contents(eb, memory, test_name);
fs/btrfs/tests/extent-map-tests.c+72−71
@@ -25,7 +25,7 @@ static void free_extent_map_tree(struct extent_map_tree *em_tree) · 24 unmodified lines
25#ifdef CONFIG_BTRFS_DEBUG25#ifdef CONFIG_BTRFS_DEBUG
26 if (refcount_read(&em->refs) != 1) {26 if (refcount_read(&em->refs) != 1) {
27 test_err(27 test_err(
28"em leak: em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx) refs %d",28"em leak: em (start %llu len %llu block_start %llu block_len %llu) refs %d",
29 em->start, em->len, em->block_start,29 em->start, em->len, em->block_start,
30 em->block_len, refcount_read(&em->refs));30 em->block_len, refcount_read(&em->refs));
31 31
@@ -73,7 +73,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info, · 41 unmodified lines
73 em->block_start = 0;73 em->block_start = 0;
74 em->block_len = SZ_16K;74 em->block_len = SZ_16K;
75 write_lock(&em_tree->lock);75 write_lock(&em_tree->lock);
76 ret = add_extent_mapping(em_tree, em, 0);76 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
77 write_unlock(&em_tree->lock);77 write_unlock(&em_tree->lock);
78 if (ret < 0) {78 if (ret < 0) {
79 test_err("cannot add extent range [0, 16K)");79 test_err("cannot add extent range [0, 16K)");
@@ -94,7 +94,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info, · 14 unmodified lines
94 em->block_start = SZ_32K; /* avoid merging */94 em->block_start = SZ_32K; /* avoid merging */
95 em->block_len = SZ_4K;95 em->block_len = SZ_4K;
96 write_lock(&em_tree->lock);96 write_lock(&em_tree->lock);
97 ret = add_extent_mapping(em_tree, em, 0);97 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
98 write_unlock(&em_tree->lock);98 write_unlock(&em_tree->lock);
99 if (ret < 0) {99 if (ret < 0) {
100 test_err("cannot add extent range [16K, 20K)");100 test_err("cannot add extent range [16K, 20K)");
@@ -121,9 +121,14 @@ static int test_case_1(struct btrfs_fs_info *fs_info, · 20 unmodified lines
121 test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);121 test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
122 goto out;122 goto out;
123 }123 }
124 if (em &&124 if (!em) {
125 (em->start != 0 || extent_map_end(em) != SZ_16K ||125 test_err("case1 [%llu %llu]: no extent map returned",
126 em->block_start != 0 || em->block_len != SZ_16K)) {126 start, start + len);
127 ret = -ENOENT;
128 goto out;
129 }
130 if (em->start != 0 || extent_map_end(em) != SZ_16K ||
131 em->block_start != 0 || em->block_len != SZ_16K) {
127 test_err(132 test_err(
128"case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",133"case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
129 start, start + len, ret, em->start, em->len,134 start, start + len, ret, em->start, em->len,
@@ -161,7 +166,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info, · 31 unmodified lines
161 em->block_start = EXTENT_MAP_INLINE;166 em->block_start = EXTENT_MAP_INLINE;
162 em->block_len = (u64)-1;167 em->block_len = (u64)-1;
163 write_lock(&em_tree->lock);168 write_lock(&em_tree->lock);
164 ret = add_extent_mapping(em_tree, em, 0);169 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
165 write_unlock(&em_tree->lock);170 write_unlock(&em_tree->lock);
166 if (ret < 0) {171 if (ret < 0) {
167 test_err("cannot add extent range [0, 1K)");172 test_err("cannot add extent range [0, 1K)");
@@ -182,7 +187,7 @@ static int test_case_2(struct btrfs_fs_info *fs_info, · 14 unmodified lines
182 em->block_start = SZ_4K;187 em->block_start = SZ_4K;
183 em->block_len = SZ_4K;188 em->block_len = SZ_4K;
184 write_lock(&em_tree->lock);189 write_lock(&em_tree->lock);
185 ret = add_extent_mapping(em_tree, em, 0);190 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
186 write_unlock(&em_tree->lock);191 write_unlock(&em_tree->lock);
187 if (ret < 0) {192 if (ret < 0) {
188 test_err("cannot add extent range [4K, 8K)");193 test_err("cannot add extent range [4K, 8K)");
@@ -209,9 +214,13 @@ static int test_case_2(struct btrfs_fs_info *fs_info, · 20 unmodified lines
209 test_err("case2 [0 1K]: ret %d", ret);214 test_err("case2 [0 1K]: ret %d", ret);
210 goto out;215 goto out;
211 }216 }
212 if (em &&217 if (!em) {
213 (em->start != 0 || extent_map_end(em) != SZ_1K ||218 test_err("case2 [0 1K]: no extent map returned");
214 em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {219 ret = -ENOENT;
220 goto out;
221 }
222 if (em->start != 0 || extent_map_end(em) != SZ_1K ||
223 em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1) {
215 test_err(224 test_err(
216"case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",225"case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
217 ret, em->start, em->len, em->block_start,226 ret, em->start, em->len, em->block_start,
@@ -244,7 +253,7 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, · 26 unmodified lines
244 em->block_start = SZ_4K;253 em->block_start = SZ_4K;
245 em->block_len = SZ_4K;254 em->block_len = SZ_4K;
246 write_lock(&em_tree->lock);255 write_lock(&em_tree->lock);
247 ret = add_extent_mapping(em_tree, em, 0);256 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
248 write_unlock(&em_tree->lock);257 write_unlock(&em_tree->lock);
249 if (ret < 0) {258 if (ret < 0) {
250 test_err("cannot add extent range [4K, 8K)");259 test_err("cannot add extent range [4K, 8K)");
@@ -268,19 +277,24 @@ static int __test_case_3(struct btrfs_fs_info *fs_info, · 17 unmodified lines
268 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);277 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
269 write_unlock(&em_tree->lock);278 write_unlock(&em_tree->lock);
270 if (ret) {279 if (ret) {
271 test_err("case3 [0x%llx 0x%llx): ret %d",280 test_err("case3 [%llu %llu): ret %d",
272 start, start + len, ret);281 start, start + len, ret);
273 goto out;282 goto out;
274 }283 }
284 if (!em) {
285 test_err("case3 [%llu %llu): no extent map returned",
286 start, start + len);
287 ret = -ENOENT;
288 goto out;
289 }
275 /*290 /*
276 * Since bytes within em are contiguous, em->block_start is identical to291 * Since bytes within em are contiguous, em->block_start is identical to
277 * em->start.292 * em->start.
278 */293 */
279 if (em &&294 if (start < em->start || start + len > extent_map_end(em) ||
280 (start < em->start || start + len > extent_map_end(em) ||295 em->start != em->block_start || em->len != em->block_len) {
281 em->start != em->block_start || em->len != em->block_len)) {
282 test_err(296 test_err(
283"case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",297"case3 [%llu %llu): ret %d em (start %llu len %llu block_start %llu block_len %llu)",
284 start, start + len, ret, em->start, em->len,298 start, start + len, ret, em->start, em->len,
285 em->block_start, em->block_len);299 em->block_start, em->block_len);
286 ret = -EINVAL;300 ret = -EINVAL;
@@ -343,7 +357,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, · 56 unmodified lines
343 em->block_start = 0;357 em->block_start = 0;
344 em->block_len = SZ_8K;358 em->block_len = SZ_8K;
345 write_lock(&em_tree->lock);359 write_lock(&em_tree->lock);
346 ret = add_extent_mapping(em_tree, em, 0);360 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
347 write_unlock(&em_tree->lock);361 write_unlock(&em_tree->lock);
348 if (ret < 0) {362 if (ret < 0) {
349 test_err("cannot add extent range [0, 8K)");363 test_err("cannot add extent range [0, 8K)");
@@ -364,7 +378,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, · 14 unmodified lines
364 em->block_start = SZ_16K; /* avoid merging */378 em->block_start = SZ_16K; /* avoid merging */
365 em->block_len = 24 * SZ_1K;379 em->block_len = 24 * SZ_1K;
366 write_lock(&em_tree->lock);380 write_lock(&em_tree->lock);
367 ret = add_extent_mapping(em_tree, em, 0);381 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
368 write_unlock(&em_tree->lock);382 write_unlock(&em_tree->lock);
369 if (ret < 0) {383 if (ret < 0) {
370 test_err("cannot add extent range [8K, 32K)");384 test_err("cannot add extent range [8K, 32K)");
@@ -387,14 +401,20 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, · 16 unmodified lines
387 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);401 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
388 write_unlock(&em_tree->lock);402 write_unlock(&em_tree->lock);
389 if (ret) {403 if (ret) {
390 test_err("case4 [0x%llx 0x%llx): ret %d",404 test_err("case4 [%llu %llu): ret %d",
391 start, len, ret);405 start, start + len, ret);
392 goto out;406 goto out;
393 }407 }
394 if (em && (start < em->start || start + len > extent_map_end(em))) {408 if (!em) {
409 test_err("case4 [%llu %llu): no extent map returned",
410 start, start + len);
411 ret = -ENOENT;
412 goto out;
413 }
414 if (start < em->start || start + len > extent_map_end(em)) {
395 test_err(415 test_err(
396"case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",416"case4 [%llu %llu): ret %d, added wrong em (start %llu len %llu block_start %llu block_len %llu)",
397 start, len, ret, em->start, em->len, em->block_start,417 start, start + len, ret, em->start, em->len, em->block_start,
398 em->block_len);418 em->block_len);
399 ret = -EINVAL;419 ret = -EINVAL;
400 }420 }
@@ -443,7 +463,8 @@ static int test_case_4(struct btrfs_fs_info *fs_info, · 42 unmodified lines
443 return ret;463 return ret;
444}464}
445 465
446static int add_compressed_extent(struct extent_map_tree *em_tree,466static int add_compressed_extent(struct btrfs_fs_info *fs_info,
467 struct extent_map_tree *em_tree,
447 u64 start, u64 len, u64 block_start)468 u64 start, u64 len, u64 block_start)
448{469{
449 struct extent_map *em;470 struct extent_map *em;
@@ -459,9 +480,9 @@ static int add_compressed_extent(struct extent_map_tree *em_tree, · 9 unmodified lines
459 em->len = len;480 em->len = len;
460 em->block_start = block_start;481 em->block_start = block_start;
461 em->block_len = SZ_4K;482 em->block_len = SZ_4K;
462 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);483 em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
463 write_lock(&em_tree->lock);484 write_lock(&em_tree->lock);
464 ret = add_extent_mapping(em_tree, em, 0);485 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
465 write_unlock(&em_tree->lock);486 write_unlock(&em_tree->lock);
466 free_extent_map(em);487 free_extent_map(em);
467 if (ret < 0) {488 if (ret < 0) {
@@ -567,7 +588,7 @@ static int validate_range(struct extent_map_tree *em_tree, int index) · 99 unmodified lines
567 * They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from588 * They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from
568 * merging the em's.589 * merging the em's.
569 */590 */
570static int test_case_5(void)591static int test_case_5(struct btrfs_fs_info *fs_info)
571{592{
572 struct extent_map_tree *em_tree;593 struct extent_map_tree *em_tree;
573 struct inode *inode;594 struct inode *inode;
@@ -585,35 +606,35 @@ static int test_case_5(void) · 11 unmodified lines
585 em_tree = &BTRFS_I(inode)->extent_tree;606 em_tree = &BTRFS_I(inode)->extent_tree;
586 607
587 /* [0, 12k) */608 /* [0, 12k) */
588 ret = add_compressed_extent(em_tree, 0, SZ_4K * 3, 0);609 ret = add_compressed_extent(fs_info, em_tree, 0, SZ_4K * 3, 0);
589 if (ret) {610 if (ret) {
590 test_err("cannot add extent range [0, 12K)");611 test_err("cannot add extent range [0, 12K)");
591 goto out;612 goto out;
592 }613 }
593 614
594 /* [12k, 24k) */615 /* [12k, 24k) */
595 ret = add_compressed_extent(em_tree, SZ_4K * 3, SZ_4K * 3, SZ_4K);616 ret = add_compressed_extent(fs_info, em_tree, SZ_4K * 3, SZ_4K * 3, SZ_4K);
596 if (ret) {617 if (ret) {
597 test_err("cannot add extent range [12k, 24k)");618 test_err("cannot add extent range [12k, 24k)");
598 goto out;619 goto out;
599 }620 }
600 621
601 /* [24k, 36k) */622 /* [24k, 36k) */
602 ret = add_compressed_extent(em_tree, SZ_4K * 6, SZ_4K * 3, SZ_8K);623 ret = add_compressed_extent(fs_info, em_tree, SZ_4K * 6, SZ_4K * 3, SZ_8K);
603 if (ret) {624 if (ret) {
604 test_err("cannot add extent range [12k, 24k)");625 test_err("cannot add extent range [12k, 24k)");
605 goto out;626 goto out;
606 }627 }
607 628
608 /* [36k, 40k) */629 /* [36k, 40k) */
609 ret = add_compressed_extent(em_tree, SZ_32K + SZ_4K, SZ_4K, SZ_4K * 3);630 ret = add_compressed_extent(fs_info, em_tree, SZ_32K + SZ_4K, SZ_4K, SZ_4K * 3);
610 if (ret) {631 if (ret) {
611 test_err("cannot add extent range [12k, 24k)");632 test_err("cannot add extent range [12k, 24k)");
612 goto out;633 goto out;
613 }634 }
614 635
615 /* [40k, 64k) */636 /* [40k, 64k) */
616 ret = add_compressed_extent(em_tree, SZ_4K * 10, SZ_4K * 6, SZ_16K);637 ret = add_compressed_extent(fs_info, em_tree, SZ_4K * 10, SZ_4K * 6, SZ_16K);
617 if (ret) {638 if (ret) {
618 test_err("cannot add extent range [12k, 24k)");639 test_err("cannot add extent range [12k, 24k)");
619 goto out;640 goto out;
@@ -665,11 +686,11 @@ static int test_case_6(struct btrfs_fs_info *fs_info, struct extent_map_tree *em · 45 unmodified lines
665 struct extent_map *em = NULL;686 struct extent_map *em = NULL;
666 int ret;687 int ret;
667 688
668 ret = add_compressed_extent(em_tree, 0, SZ_4K, 0);689 ret = add_compressed_extent(fs_info, em_tree, 0, SZ_4K, 0);
669 if (ret)690 if (ret)
670 goto out;691 goto out;
671 692
672 ret = add_compressed_extent(em_tree, SZ_4K, SZ_4K, 0);693 ret = add_compressed_extent(fs_info, em_tree, SZ_4K, SZ_4K, 0);
673 if (ret)694 if (ret)
674 goto out;695 goto out;
675 696
@@ -713,7 +734,7 @@ out: · 37 unmodified lines
713 * true would mess up the start/end calculations and subsequent splits would be734 * true would mess up the start/end calculations and subsequent splits would be
714 * incorrect.735 * incorrect.
715 */736 */
716static int test_case_7(void)737static int test_case_7(struct btrfs_fs_info *fs_info)
717{738{
718 struct extent_map_tree *em_tree;739 struct extent_map_tree *em_tree;
719 struct extent_map *em;740 struct extent_map *em;
@@ -742,9 +763,9 @@ static int test_case_7(void) · 22 unmodified lines
742 em->len = SZ_16K;763 em->len = SZ_16K;
743 em->block_start = 0;764 em->block_start = 0;
744 em->block_len = SZ_4K;765 em->block_len = SZ_4K;
745 set_bit(EXTENT_FLAG_PINNED, &em->flags);766 em->flags |= EXTENT_FLAG_PINNED;
746 write_lock(&em_tree->lock);767 write_lock(&em_tree->lock);
747 ret = add_extent_mapping(em_tree, em, 0);768 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
748 write_unlock(&em_tree->lock);769 write_unlock(&em_tree->lock);
749 if (ret < 0) {770 if (ret < 0) {
750 test_err("couldn't add extent map");771 test_err("couldn't add extent map");
@@ -765,7 +786,7 @@ static int test_case_7(void) · 14 unmodified lines
765 em->block_start = SZ_32K;786 em->block_start = SZ_32K;
766 em->block_len = SZ_16K;787 em->block_len = SZ_16K;
767 write_lock(&em_tree->lock);788 write_lock(&em_tree->lock);
768 ret = add_extent_mapping(em_tree, em, 0);789 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
769 write_unlock(&em_tree->lock);790 write_unlock(&em_tree->lock);
770 if (ret < 0) {791 if (ret < 0) {
771 test_err("couldn't add extent map");792 test_err("couldn't add extent map");
@@ -859,33 +880,21 @@ struct rmap_test_vector { · 87 unmodified lines
859static int test_rmap_block(struct btrfs_fs_info *fs_info,880static int test_rmap_block(struct btrfs_fs_info *fs_info,
860 struct rmap_test_vector *test)881 struct rmap_test_vector *test)
861{882{
862 struct extent_map *em;883 struct btrfs_chunk_map *map;
863 struct map_lookup *map = NULL;
864 u64 *logical = NULL;884 u64 *logical = NULL;
865 int i, out_ndaddrs, out_stripe_len;885 int i, out_ndaddrs, out_stripe_len;
866 int ret;886 int ret;
867 887
868 em = alloc_extent_map();888 map = btrfs_alloc_chunk_map(test->num_stripes, GFP_KERNEL);
869 if (!em) {
870 test_std_err(TEST_ALLOC_EXTENT_MAP);
871 return -ENOMEM;
872 }
873
874 map = kmalloc(map_lookup_size(test->num_stripes), GFP_KERNEL);
875 if (!map) {889 if (!map) {
876 kfree(em);890 test_std_err(TEST_ALLOC_CHUNK_MAP);
877 test_std_err(TEST_ALLOC_EXTENT_MAP);
878 return -ENOMEM;891 return -ENOMEM;
879 }892 }
880 893
881 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
882 /* Start at 4GiB logical address */894 /* Start at 4GiB logical address */
883 em->start = SZ_4G;895 map->start = SZ_4G;
884 em->len = test->data_stripe_size * test->num_data_stripes;896 map->chunk_len = test->data_stripe_size * test->num_data_stripes;
885 em->block_len = em->len;897 map->stripe_size = test->data_stripe_size;
886 em->orig_block_len = test->data_stripe_size;
887 em->map_lookup = map;
888
889 map->num_stripes = test->num_stripes;898 map->num_stripes = test->num_stripes;
890 map->type = test->raid_type;899 map->type = test->raid_type;
891 900
@@ -901,15 +910,13 @@ static int test_rmap_block(struct btrfs_fs_info *fs_info, · 9 unmodified lines
901 map->stripes[i].physical = test->data_stripe_phys_start[i];910 map->stripes[i].physical = test->data_stripe_phys_start[i];
902 }911 }
903 912
904 write_lock(&fs_info->mapping_tree.lock);913 ret = btrfs_add_chunk_map(fs_info, map);
905 ret = add_extent_mapping(&fs_info->mapping_tree, em, 0);
906 write_unlock(&fs_info->mapping_tree.lock);
907 if (ret) {914 if (ret) {
908 test_err("error adding block group mapping to mapping tree");915 test_err("error adding chunk map to mapping tree");
909 goto out_free;916 goto out_free;
910 }917 }
911 918
912 ret = btrfs_rmap_block(fs_info, em->start, btrfs_sb_offset(1),919 ret = btrfs_rmap_block(fs_info, map->start, btrfs_sb_offset(1),
913 &logical, &out_ndaddrs, &out_stripe_len);920 &logical, &out_ndaddrs, &out_stripe_len);
914 if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {921 if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {
915 test_err("didn't rmap anything but expected %d",922 test_err("didn't rmap anything but expected %d",
@@ -938,14 +945,8 @@ static int test_rmap_block(struct btrfs_fs_info *fs_info, · 22 unmodified lines
938 945
939 ret = 0;946 ret = 0;
940out:947out:
941 write_lock(&fs_info->mapping_tree.lock);948 btrfs_remove_chunk_map(fs_info, map);
942 remove_extent_mapping(&fs_info->mapping_tree, em);
943 write_unlock(&fs_info->mapping_tree.lock);
944 /* For us */
945 free_extent_map(em);
946out_free:949out_free:
947 /* For the tree */
948 free_extent_map(em);
949 kfree(logical);950 kfree(logical);
950 return ret;951 return ret;
951}952}
@@ -1022,13 +1023,13 @@ int btrfs_test_extent_map(void) · 70 unmodified lines
1022 ret = test_case_4(fs_info, em_tree);1023 ret = test_case_4(fs_info, em_tree);
1023 if (ret)1024 if (ret)
1024 goto out;1025 goto out;
1025 ret = test_case_5();1026 ret = test_case_5(fs_info);
1026 if (ret)1027 if (ret)
1027 goto out;1028 goto out;
1028 ret = test_case_6(fs_info, em_tree);1029 ret = test_case_6(fs_info, em_tree);
1029 if (ret)1030 if (ret)
1030 goto out;1031 goto out;
1031 ret = test_case_7();1032 ret = test_case_7(fs_info);
1032 if (ret)1033 if (ret)
1033 goto out;1034 goto out;
1034 1035
fs/btrfs/tests/inode-tests.c+30−30
@@ -211,9 +211,9 @@ static void setup_file_extents(struct btrfs_root *root, u32 sectorsize) · 210 unmodified lines
211 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);211 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
212}212}
213 213
214static unsigned long prealloc_only = 0;214static u32 prealloc_only = 0;
215static unsigned long compressed_only = 0;215static u32 compressed_only = 0;
216static unsigned long vacancy_only = 0;216static u32 vacancy_only = 0;
217 217
218static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)218static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
219{219{
@@ -305,7 +305,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 85 unmodified lines
305 goto out;305 goto out;
306 }306 }
307 if (em->flags != 0) {307 if (em->flags != 0) {
308 test_err("unexpected flags set, want 0 have %lu", em->flags);308 test_err("unexpected flags set, want 0 have %u", em->flags);
309 goto out;309 goto out;
310 }310 }
311 /*311 /*
@@ -332,7 +332,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 20 unmodified lines
332 goto out;332 goto out;
333 }333 }
334 if (em->flags != 0) {334 if (em->flags != 0) {
335 test_err("unexpected flags set, want 0 have %lu", em->flags);335 test_err("unexpected flags set, want 0 have %u", em->flags);
336 goto out;336 goto out;
337 }337 }
338 offset = em->start + em->len;338 offset = em->start + em->len;
@@ -355,7 +355,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 16 unmodified lines
355 goto out;355 goto out;
356 }356 }
357 if (em->flags != 0) {357 if (em->flags != 0) {
358 test_err("unexpected flags set, want 0 have %lu", em->flags);358 test_err("unexpected flags set, want 0 have %u", em->flags);
359 goto out;359 goto out;
360 }360 }
361 if (em->orig_start != em->start) {361 if (em->orig_start != em->start) {
@@ -383,7 +383,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 21 unmodified lines
383 goto out;383 goto out;
384 }384 }
385 if (em->flags != 0) {385 if (em->flags != 0) {
386 test_err("unexpected flags set, want 0 have %lu", em->flags);386 test_err("unexpected flags set, want 0 have %u", em->flags);
387 goto out;387 goto out;
388 }388 }
389 if (em->orig_start != em->start) {389 if (em->orig_start != em->start) {
@@ -412,7 +412,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 22 unmodified lines
412 goto out;412 goto out;
413 }413 }
414 if (em->flags != 0) {414 if (em->flags != 0) {
415 test_err("unexpected flags set, want 0 have %lu", em->flags);415 test_err("unexpected flags set, want 0 have %u", em->flags);
416 goto out;416 goto out;
417 }417 }
418 offset = em->start + em->len;418 offset = em->start + em->len;
@@ -434,7 +434,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 15 unmodified lines
434 goto out;434 goto out;
435 }435 }
436 if (em->flags != 0) {436 if (em->flags != 0) {
437 test_err("unexpected flags set, want 0 have %lu", em->flags);437 test_err("unexpected flags set, want 0 have %u", em->flags);
438 goto out;438 goto out;
439 }439 }
440 if (em->orig_start != orig_start) {440 if (em->orig_start != orig_start) {
@@ -468,7 +468,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 27 unmodified lines
468 goto out;468 goto out;
469 }469 }
470 if (em->flags != prealloc_only) {470 if (em->flags != prealloc_only) {
471 test_err("unexpected flags set, want %lu have %lu",471 test_err("unexpected flags set, want %u have %u",
472 prealloc_only, em->flags);472 prealloc_only, em->flags);
473 goto out;473 goto out;
474 }474 }
@@ -497,7 +497,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 22 unmodified lines
497 goto out;497 goto out;
498 }498 }
499 if (em->flags != prealloc_only) {499 if (em->flags != prealloc_only) {
500 test_err("unexpected flags set, want %lu have %lu",500 test_err("unexpected flags set, want %u have %u",
501 prealloc_only, em->flags);501 prealloc_only, em->flags);
502 goto out;502 goto out;
503 }503 }
@@ -527,7 +527,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 23 unmodified lines
527 goto out;527 goto out;
528 }528 }
529 if (em->flags != 0) {529 if (em->flags != 0) {
530 test_err("unexpected flags set, want 0 have %lu", em->flags);530 test_err("unexpected flags set, want 0 have %u", em->flags);
531 goto out;531 goto out;
532 }532 }
533 if (em->orig_start != orig_start) {533 if (em->orig_start != orig_start) {
@@ -560,7 +560,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 26 unmodified lines
560 goto out;560 goto out;
561 }561 }
562 if (em->flags != prealloc_only) {562 if (em->flags != prealloc_only) {
563 test_err("unexpected flags set, want %lu have %lu",563 test_err("unexpected flags set, want %u have %u",
564 prealloc_only, em->flags);564 prealloc_only, em->flags);
565 goto out;565 goto out;
566 }566 }
@@ -595,7 +595,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 28 unmodified lines
595 goto out;595 goto out;
596 }596 }
597 if (em->flags != compressed_only) {597 if (em->flags != compressed_only) {
598 test_err("unexpected flags set, want %lu have %lu",598 test_err("unexpected flags set, want %u have %u",
599 compressed_only, em->flags);599 compressed_only, em->flags);
600 goto out;600 goto out;
601 }601 }
@@ -604,9 +604,9 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 2 unmodified lines
604 em->start, em->orig_start);604 em->start, em->orig_start);
605 goto out;605 goto out;
606 }606 }
607 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {607 if (extent_map_compression(em) != BTRFS_COMPRESS_ZLIB) {
608 test_err("unexpected compress type, wanted %d, got %d",608 test_err("unexpected compress type, wanted %d, got %d",
609 BTRFS_COMPRESS_ZLIB, em->compress_type);609 BTRFS_COMPRESS_ZLIB, extent_map_compression(em));
610 goto out;610 goto out;
611 }611 }
612 offset = em->start + em->len;612 offset = em->start + em->len;
@@ -629,7 +629,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 16 unmodified lines
629 goto out;629 goto out;
630 }630 }
631 if (em->flags != compressed_only) {631 if (em->flags != compressed_only) {
632 test_err("unexpected flags set, want %lu have %lu",632 test_err("unexpected flags set, want %u have %u",
633 compressed_only, em->flags);633 compressed_only, em->flags);
634 goto out;634 goto out;
635 }635 }
@@ -638,9 +638,9 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 2 unmodified lines
638 em->start, em->orig_start);638 em->start, em->orig_start);
639 goto out;639 goto out;
640 }640 }
641 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {641 if (extent_map_compression(em) != BTRFS_COMPRESS_ZLIB) {
642 test_err("unexpected compress type, wanted %d, got %d",642 test_err("unexpected compress type, wanted %d, got %d",
643 BTRFS_COMPRESS_ZLIB, em->compress_type);643 BTRFS_COMPRESS_ZLIB, extent_map_compression(em));
644 goto out;644 goto out;
645 }645 }
646 disk_bytenr = em->block_start;646 disk_bytenr = em->block_start;
@@ -664,7 +664,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 17 unmodified lines
664 goto out;664 goto out;
665 }665 }
666 if (em->flags != 0) {666 if (em->flags != 0) {
667 test_err("unexpected flags set, want 0 have %lu", em->flags);667 test_err("unexpected flags set, want 0 have %u", em->flags);
668 goto out;668 goto out;
669 }669 }
670 if (em->orig_start != em->start) {670 if (em->orig_start != em->start) {
@@ -692,7 +692,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 21 unmodified lines
692 goto out;692 goto out;
693 }693 }
694 if (em->flags != compressed_only) {694 if (em->flags != compressed_only) {
695 test_err("unexpected flags set, want %lu have %lu",695 test_err("unexpected flags set, want %u have %u",
696 compressed_only, em->flags);696 compressed_only, em->flags);
697 goto out;697 goto out;
698 }698 }
@@ -701,9 +701,9 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 2 unmodified lines
701 em->start, orig_start);701 em->start, orig_start);
702 goto out;702 goto out;
703 }703 }
704 if (em->compress_type != BTRFS_COMPRESS_ZLIB) {704 if (extent_map_compression(em) != BTRFS_COMPRESS_ZLIB) {
705 test_err("unexpected compress type, wanted %d, got %d",705 test_err("unexpected compress type, wanted %d, got %d",
706 BTRFS_COMPRESS_ZLIB, em->compress_type);706 BTRFS_COMPRESS_ZLIB, extent_map_compression(em));
707 goto out;707 goto out;
708 }708 }
709 offset = em->start + em->len;709 offset = em->start + em->len;
@@ -726,7 +726,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 16 unmodified lines
726 goto out;726 goto out;
727 }727 }
728 if (em->flags != 0) {728 if (em->flags != 0) {
729 test_err("unexpected flags set, want 0 have %lu", em->flags);729 test_err("unexpected flags set, want 0 have %u", em->flags);
730 goto out;730 goto out;
731 }731 }
732 if (em->orig_start != em->start) {732 if (em->orig_start != em->start) {
@@ -758,7 +758,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 25 unmodified lines
758 goto out;758 goto out;
759 }759 }
760 if (em->flags != vacancy_only) {760 if (em->flags != vacancy_only) {
761 test_err("unexpected flags set, want %lu have %lu",761 test_err("unexpected flags set, want %u have %u",
762 vacancy_only, em->flags);762 vacancy_only, em->flags);
763 goto out;763 goto out;
764 }764 }
@@ -786,7 +786,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 21 unmodified lines
786 goto out;786 goto out;
787 }787 }
788 if (em->flags != 0) {788 if (em->flags != 0) {
789 test_err("unexpected flags set, want 0 have %lu", em->flags);789 test_err("unexpected flags set, want 0 have %u", em->flags);
790 goto out;790 goto out;
791 }791 }
792 if (em->orig_start != em->start) {792 if (em->orig_start != em->start) {
@@ -866,7 +866,7 @@ static int test_hole_first(u32 sectorsize, u32 nodesize) · 73 unmodified lines
866 goto out;866 goto out;
867 }867 }
868 if (em->flags != vacancy_only) {868 if (em->flags != vacancy_only) {
869 test_err("wrong flags, wanted %lu, have %lu", vacancy_only,869 test_err("wrong flags, wanted %u, have %u", vacancy_only,
870 em->flags);870 em->flags);
871 goto out;871 goto out;
872 }872 }
@@ -888,7 +888,7 @@ static int test_hole_first(u32 sectorsize, u32 nodesize) · 15 unmodified lines
888 goto out;888 goto out;
889 }889 }
890 if (em->flags != 0) {890 if (em->flags != 0) {
891 test_err("unexpected flags set, wanted 0 got %lu",891 test_err("unexpected flags set, wanted 0 got %u",
892 em->flags);892 em->flags);
893 goto out;893 goto out;
894 }894 }
@@ -1095,8 +1095,8 @@ int btrfs_test_inodes(u32 sectorsize, u32 nodesize) · 200 unmodified lines
1095 1095
1096 test_msg("running inode tests");1096 test_msg("running inode tests");
1097 1097
1098 set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);1098 compressed_only |= EXTENT_FLAG_COMPRESS_ZLIB;
1099 set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);1099 prealloc_only |= EXTENT_FLAG_PREALLOC;
1100 1100
1101 ret = test_btrfs_get_extent(sectorsize, nodesize);1101 ret = test_btrfs_get_extent(sectorsize, nodesize);
1102 if (ret)1102 if (ret)
fs/btrfs/tree-checker.h+1−1
@@ -22,7 +22,7 @@ struct btrfs_tree_parent_check { · 21 unmodified lines
22 22
23 /*23 /*
24 * Expected transid, can be 0 to skip the check, but such skip24 * Expected transid, can be 0 to skip the check, but such skip
25 * should only be utlized for backref walk related code.25 * should only be utilized for backref walk related code.
26 */26 */
27 u64 transid;27 u64 transid;
28 28
fs/btrfs/tree-log.c+9−8
@@ -2575,7 +2575,6 @@ static int clean_log_buffer(struct btrfs_trans_handle *trans, · 2574 unmodified lines
2575 ret = btrfs_pin_reserved_extent(trans, eb);2575 ret = btrfs_pin_reserved_extent(trans, eb);
2576 if (ret)2576 if (ret)
2577 return ret;2577 return ret;
2578 btrfs_redirty_list_add(trans->transaction, eb);
2579 } else {2578 } else {
2580 unaccount_log_buffer(eb->fs_info, eb->start);2579 unaccount_log_buffer(eb->fs_info, eb->start);
2581 }2580 }
@@ -4520,7 +4519,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, · 1938 unmodified lines
4520 int ret = 0;4519 int ret = 0;
4521 4520
4522 if (inode->flags & BTRFS_INODE_NODATASUM ||4521 if (inode->flags & BTRFS_INODE_NODATASUM ||
4523 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||4522 (em->flags & EXTENT_FLAG_PREALLOC) ||
4524 em->block_start == EXTENT_MAP_HOLE)4523 em->block_start == EXTENT_MAP_HOLE)
4525 return 0;4524 return 0;
4526 4525
@@ -4583,7 +4582,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, · 56 unmodified lines
4583 return 0;4582 return 0;
4584 4583
4585 /* If we're compressed we have to save the entire range of csums. */4584 /* If we're compressed we have to save the entire range of csums. */
4586 if (em->compress_type) {4585 if (extent_map_is_compressed(em)) {
4587 csum_offset = 0;4586 csum_offset = 0;
4588 csum_len = max(em->block_len, em->orig_block_len);4587 csum_len = max(em->block_len, em->orig_block_len);
4589 } else {4588 } else {
@@ -4623,18 +4622,20 @@ static int log_one_extent(struct btrfs_trans_handle *trans, · 33 unmodified lines
4623 struct btrfs_file_extent_item fi = { 0 };4622 struct btrfs_file_extent_item fi = { 0 };
4624 struct extent_buffer *leaf;4623 struct extent_buffer *leaf;
4625 struct btrfs_key key;4624 struct btrfs_key key;
4625 enum btrfs_compression_type compress_type;
4626 u64 extent_offset = em->start - em->orig_start;4626 u64 extent_offset = em->start - em->orig_start;
4627 u64 block_len;4627 u64 block_len;
4628 int ret;4628 int ret;
4629 4629
4630 btrfs_set_stack_file_extent_generation(&fi, trans->transid);4630 btrfs_set_stack_file_extent_generation(&fi, trans->transid);
4631 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))4631 if (em->flags & EXTENT_FLAG_PREALLOC)
4632 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);4632 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
4633 else4633 else
4634 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);4634 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
4635 4635
4636 block_len = max(em->block_len, em->orig_block_len);4636 block_len = max(em->block_len, em->orig_block_len);
4637 if (em->compress_type != BTRFS_COMPRESS_NONE) {4637 compress_type = extent_map_compression(em);
4638 if (compress_type != BTRFS_COMPRESS_NONE) {
4638 btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start);4639 btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start);
4639 btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);4640 btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
4640 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {4641 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
@@ -4646,7 +4647,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans, · 5 unmodified lines
4646 btrfs_set_stack_file_extent_offset(&fi, extent_offset);4647 btrfs_set_stack_file_extent_offset(&fi, extent_offset);
4647 btrfs_set_stack_file_extent_num_bytes(&fi, em->len);4648 btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
4648 btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);4649 btrfs_set_stack_file_extent_ram_bytes(&fi, em->ram_bytes);
4649 btrfs_set_stack_file_extent_compression(&fi, em->compress_type);4650 btrfs_set_stack_file_extent_compression(&fi, compress_type);
4650 4651
4651 ret = log_extent_csums(trans, inode, log, em, ctx);4652 ret = log_extent_csums(trans, inode, log, em, ctx);
4652 if (ret)4653 if (ret)
@@ -4859,13 +4860,13 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, · 206 unmodified lines
4859 continue;4860 continue;
4860 4861
4861 /* We log prealloc extents beyond eof later. */4862 /* We log prealloc extents beyond eof later. */
4862 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&4863 if ((em->flags & EXTENT_FLAG_PREALLOC) &&
4863 em->start >= i_size_read(&inode->vfs_inode))4864 em->start >= i_size_read(&inode->vfs_inode))
4864 continue;4865 continue;
4865 4866
4866 /* Need a ref to keep it from getting evicted from cache */4867 /* Need a ref to keep it from getting evicted from cache */
4867 refcount_inc(&em->refs);4868 refcount_inc(&em->refs);
4868 set_bit(EXTENT_FLAG_LOGGING, &em->flags);4869 em->flags |= EXTENT_FLAG_LOGGING;
4869 list_add_tail(&em->list, &extents);4870 list_add_tail(&em->list, &extents);
4870 num++;4871 num++;
4871 }4872 }
fs/btrfs/volumes.c+564−370
@@ -41,6 +41,17 @@ · 40 unmodified lines
41 BTRFS_BLOCK_GROUP_RAID10 | \41 BTRFS_BLOCK_GROUP_RAID10 | \
42 BTRFS_BLOCK_GROUP_RAID56_MASK)42 BTRFS_BLOCK_GROUP_RAID56_MASK)
43 43
44struct btrfs_io_geometry {
45 u32 stripe_index;
46 u32 stripe_nr;
47 int mirror_num;
48 int num_stripes;
49 u64 stripe_offset;
50 u64 raid56_full_stripe_start;
51 int max_errors;
52 enum btrfs_map_op op;
53};
54
44const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {55const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
45 [BTRFS_RAID_RAID10] = {56 [BTRFS_RAID_RAID10] = {
46 .sub_stripes = 2,57 .sub_stripes = 2,
@@ -1742,19 +1753,18 @@ out: · 1695 unmodified lines
1742 1753
1743static u64 find_next_chunk(struct btrfs_fs_info *fs_info)1754static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1744{1755{
1745 struct extent_map_tree *em_tree;
1746 struct extent_map *em;
1747 struct rb_node *n;1756 struct rb_node *n;
1748 u64 ret = 0;1757 u64 ret = 0;
1749 1758
1750 em_tree = &fs_info->mapping_tree;1759 read_lock(&fs_info->mapping_tree_lock);
1751 read_lock(&em_tree->lock);1760 n = rb_last(&fs_info->mapping_tree.rb_root);
1752 n = rb_last(&em_tree->map.rb_root);
1753 if (n) {1761 if (n) {
1754 em = rb_entry(n, struct extent_map, rb_node);1762 struct btrfs_chunk_map *map;
1755 ret = em->start + em->len;1763
1764 map = rb_entry(n, struct btrfs_chunk_map, rb_node);
1765 ret = map->start + map->chunk_len;
1756 }1766 }
1757 read_unlock(&em_tree->lock);1767 read_unlock(&fs_info->mapping_tree_lock);
1758 1768
1759 return ret;1769 return ret;
1760}1770}
@@ -2986,6 +2996,81 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) · 1225 unmodified lines
2986 return ret;2996 return ret;
2987}2997}
2988 2998
2999struct btrfs_chunk_map *btrfs_find_chunk_map_nolock(struct btrfs_fs_info *fs_info,
3000 u64 logical, u64 length)
3001{
3002 struct rb_node *node = fs_info->mapping_tree.rb_root.rb_node;
3003 struct rb_node *prev = NULL;
3004 struct rb_node *orig_prev;
3005 struct btrfs_chunk_map *map;
3006 struct btrfs_chunk_map *prev_map = NULL;
3007
3008 while (node) {
3009 map = rb_entry(node, struct btrfs_chunk_map, rb_node);
3010 prev = node;
3011 prev_map = map;
3012
3013 if (logical < map->start) {
3014 node = node->rb_left;
3015 } else if (logical >= map->start + map->chunk_len) {
3016 node = node->rb_right;
3017 } else {
3018 refcount_inc(&map->refs);
3019 return map;
3020 }
3021 }
3022
3023 if (!prev)
3024 return NULL;
3025
3026 orig_prev = prev;
3027 while (prev && logical >= prev_map->start + prev_map->chunk_len) {
3028 prev = rb_next(prev);
3029 prev_map = rb_entry(prev, struct btrfs_chunk_map, rb_node);
3030 }
3031
3032 if (!prev) {
3033 prev = orig_prev;
3034 prev_map = rb_entry(prev, struct btrfs_chunk_map, rb_node);
3035 while (prev && logical < prev_map->start) {
3036 prev = rb_prev(prev);
3037 prev_map = rb_entry(prev, struct btrfs_chunk_map, rb_node);
3038 }
3039 }
3040
3041 if (prev) {
3042 u64 end = logical + length;
3043
3044 /*
3045 * Caller can pass a U64_MAX length when it wants to get any
3046 * chunk starting at an offset of 'logical' or higher, so deal
3047 * with underflow by resetting the end offset to U64_MAX.
3048 */
3049 if (end < logical)
3050 end = U64_MAX;
3051
3052 if (end > prev_map->start &&
3053 logical < prev_map->start + prev_map->chunk_len) {
3054 refcount_inc(&prev_map->refs);
3055 return prev_map;
3056 }
3057 }
3058
3059 return NULL;
3060}
3061
3062struct btrfs_chunk_map *btrfs_find_chunk_map(struct btrfs_fs_info *fs_info,
3063 u64 logical, u64 length)
3064{
3065 struct btrfs_chunk_map *map;
3066
3067 read_lock(&fs_info->mapping_tree_lock);
3068 map = btrfs_find_chunk_map_nolock(fs_info, logical, length);
3069 read_unlock(&fs_info->mapping_tree_lock);
3070
3071 return map;
3072}
3073
2989/*3074/*
2990 * Find the mapping containing the given logical extent.3075 * Find the mapping containing the given logical extent.
2991 *3076 *
@@ -2994,38 +3079,37 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) · 2 unmodified lines
2994 *3079 *
2995 * Return: Chunk mapping or ERR_PTR.3080 * Return: Chunk mapping or ERR_PTR.
2996 */3081 */
2997struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,3082struct btrfs_chunk_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2998 u64 logical, u64 length)3083 u64 logical, u64 length)
2999{3084{
3000 struct extent_map_tree *em_tree;3085 struct btrfs_chunk_map *map;
3001 struct extent_map *em;
3002 3086
3003 em_tree = &fs_info->mapping_tree;3087 map = btrfs_find_chunk_map(fs_info, logical, length);
3004 read_lock(&em_tree->lock);
3005 em = lookup_extent_mapping(em_tree, logical, length);
3006 read_unlock(&em_tree->lock);
3007 3088
3008 if (!em) {3089 if (unlikely(!map)) {
3090 read_unlock(&fs_info->mapping_tree_lock);
3009 btrfs_crit(fs_info,3091 btrfs_crit(fs_info,
3010 "unable to find chunk map for logical %llu length %llu",3092 "unable to find chunk map for logical %llu length %llu",
3011 logical, length);3093 logical, length);
3012 return ERR_PTR(-EINVAL);3094 return ERR_PTR(-EINVAL);
3013 }3095 }
3014 3096
3015 if (em->start > logical || em->start + em->len <= logical) {3097 if (unlikely(map->start > logical || map->start + map->chunk_len <= logical)) {
3098 read_unlock(&fs_info->mapping_tree_lock);
3016 btrfs_crit(fs_info,3099 btrfs_crit(fs_info,
3017 "found a bad chunk map, wanted %llu-%llu, found %llu-%llu",3100 "found a bad chunk map, wanted %llu-%llu, found %llu-%llu",
3018 logical, logical + length, em->start, em->start + em->len);3101 logical, logical + length, map->start,
3019 free_extent_map(em);3102 map->start + map->chunk_len);
3103 btrfs_free_chunk_map(map);
3020 return ERR_PTR(-EINVAL);3104 return ERR_PTR(-EINVAL);
3021 }3105 }
3022 3106
3023 /* callers are responsible for dropping em's ref. */3107 /* Callers are responsible for dropping the reference. */
3024 return em;3108 return map;
3025}3109}
3026 3110
3027static int remove_chunk_item(struct btrfs_trans_handle *trans,3111static int remove_chunk_item(struct btrfs_trans_handle *trans,
3028 struct map_lookup *map, u64 chunk_offset)3112 struct btrfs_chunk_map *map, u64 chunk_offset)
3029{3113{
3030 int i;3114 int i;
3031 3115
@@ -3050,23 +3134,21 @@ static int remove_chunk_item(struct btrfs_trans_handle *trans, · 18 unmodified lines
3050int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)3134int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
3051{3135{
3052 struct btrfs_fs_info *fs_info = trans->fs_info;3136 struct btrfs_fs_info *fs_info = trans->fs_info;
3053 struct extent_map *em;3137 struct btrfs_chunk_map *map;
3054 struct map_lookup *map;
3055 u64 dev_extent_len = 0;3138 u64 dev_extent_len = 0;
3056 int i, ret = 0;3139 int i, ret = 0;
3057 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;3140 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3058 3141
3059 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);3142 map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
3060 if (IS_ERR(em)) {3143 if (IS_ERR(map)) {
3061 /*3144 /*
3062 * This is a logic error, but we don't want to just rely on the3145 * This is a logic error, but we don't want to just rely on the
3063 * user having built with ASSERT enabled, so if ASSERT doesn't3146 * user having built with ASSERT enabled, so if ASSERT doesn't
3064 * do anything we still error out.3147 * do anything we still error out.
3065 */3148 */
3066 ASSERT(0);3149 ASSERT(0);
3067 return PTR_ERR(em);3150 return PTR_ERR(map);
3068 }3151 }
3069 map = em->map_lookup;
3070 3152
3071 /*3153 /*
3072 * First delete the device extent items from the devices btree.3154 * First delete the device extent items from the devices btree.
@@ -3169,7 +3251,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) · 96 unmodified lines
3169 goto out;3251 goto out;
3170 }3252 }
3171 3253
3172 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);3254 trace_btrfs_chunk_free(fs_info, map, chunk_offset, map->chunk_len);
3173 3255
3174 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {3256 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3175 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);3257 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
@@ -3188,7 +3270,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) · 12 unmodified lines
3188 */3270 */
3189 btrfs_trans_release_chunk_metadata(trans);3271 btrfs_trans_release_chunk_metadata(trans);
3190 3272
3191 ret = btrfs_remove_block_group(trans, chunk_offset, em);3273 ret = btrfs_remove_block_group(trans, map);
3192 if (ret) {3274 if (ret) {
3193 btrfs_abort_transaction(trans, ret);3275 btrfs_abort_transaction(trans, ret);
3194 goto out;3276 goto out;
@@ -3200,7 +3282,7 @@ out: · 5 unmodified lines
3200 trans->removing_chunk = false;3282 trans->removing_chunk = false;
3201 }3283 }
3202 /* once for us */3284 /* once for us */
3203 free_extent_map(em);3285 btrfs_free_chunk_map(map);
3204 return ret;3286 return ret;
3205}3287}
3206 3288
@@ -5347,24 +5429,131 @@ static int decide_stripe_size(struct btrfs_fs_devices *fs_devices, · 2140 unmodified lines
5347 }5429 }
5348}5430}
5349 5431
5432static void chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
5433{
5434 for (int i = 0; i < map->num_stripes; i++) {
5435 struct btrfs_io_stripe *stripe = &map->stripes[i];
5436 struct btrfs_device *device = stripe->dev;
5437
5438 set_extent_bit(&device->alloc_state, stripe->physical,
5439 stripe->physical + map->stripe_size - 1,
5440 bits | EXTENT_NOWAIT, NULL);
5441 }
5442}
5443
5444static void chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
5445{
5446 for (int i = 0; i < map->num_stripes; i++) {
5447 struct btrfs_io_stripe *stripe = &map->stripes[i];
5448 struct btrfs_device *device = stripe->dev;
5449
5450 __clear_extent_bit(&device->alloc_state, stripe->physical,
5451 stripe->physical + map->stripe_size - 1,
5452 bits | EXTENT_NOWAIT,
5453 NULL, NULL);
5454 }
5455}
5456
5457void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
5458{
5459 write_lock(&fs_info->mapping_tree_lock);
5460 rb_erase_cached(&map->rb_node, &fs_info->mapping_tree);
5461 RB_CLEAR_NODE(&map->rb_node);
5462 chunk_map_device_clear_bits(map, CHUNK_ALLOCATED);
5463 write_unlock(&fs_info->mapping_tree_lock);
5464
5465 /* Once for the tree reference. */
5466 btrfs_free_chunk_map(map);
5467}
5468
5469EXPORT_FOR_TESTS
5470int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
5471{
5472 struct rb_node **p;
5473 struct rb_node *parent = NULL;
5474 bool leftmost = true;
5475
5476 write_lock(&fs_info->mapping_tree_lock);
5477 p = &fs_info->mapping_tree.rb_root.rb_node;
5478 while (*p) {
5479 struct btrfs_chunk_map *entry;
5480
5481 parent = *p;
5482 entry = rb_entry(parent, struct btrfs_chunk_map, rb_node);
5483
5484 if (map->start < entry->start) {
5485 p = &(*p)->rb_left;
5486 } else if (map->start > entry->start) {
5487 p = &(*p)->rb_right;
5488 leftmost = false;
5489 } else {
5490 write_unlock(&fs_info->mapping_tree_lock);
5491 return -EEXIST;
5492 }
5493 }
5494 rb_link_node(&map->rb_node, parent, p);
5495 rb_insert_color_cached(&map->rb_node, &fs_info->mapping_tree, leftmost);
5496 chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
5497 chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
5498 write_unlock(&fs_info->mapping_tree_lock);
5499
5500 return 0;
5501}
5502
5503EXPORT_FOR_TESTS
5504struct btrfs_chunk_map *btrfs_alloc_chunk_map(int num_stripes, gfp_t gfp)
5505{
5506 struct btrfs_chunk_map *map;
5507
5508 map = kmalloc(btrfs_chunk_map_size(num_stripes), gfp);
5509 if (!map)
5510 return NULL;
5511
5512 refcount_set(&map->refs, 1);
5513 RB_CLEAR_NODE(&map->rb_node);
5514
5515 return map;
5516}
5517
5518struct btrfs_chunk_map *btrfs_clone_chunk_map(struct btrfs_chunk_map *map, gfp_t gfp)
5519{
5520 const int size = btrfs_chunk_map_size(map->num_stripes);
5521 struct btrfs_chunk_map *clone;
5522
5523 clone = kmemdup(map, size, gfp);
5524 if (!clone)
5525 return NULL;
5526
5527 refcount_set(&clone->refs, 1);
5528 RB_CLEAR_NODE(&clone->rb_node);
5529
5530 return clone;
5531}
5532
5350static struct btrfs_block_group *create_chunk(struct btrfs_trans_handle *trans,5533static struct btrfs_block_group *create_chunk(struct btrfs_trans_handle *trans,
5351 struct alloc_chunk_ctl *ctl,5534 struct alloc_chunk_ctl *ctl,
5352 struct btrfs_device_info *devices_info)5535 struct btrfs_device_info *devices_info)
5353{5536{
5354 struct btrfs_fs_info *info = trans->fs_info;5537 struct btrfs_fs_info *info = trans->fs_info;
5355 struct map_lookup *map = NULL;5538 struct btrfs_chunk_map *map;
5356 struct extent_map_tree *em_tree;
5357 struct btrfs_block_group *block_group;5539 struct btrfs_block_group *block_group;
5358 struct extent_map *em;
5359 u64 start = ctl->start;5540 u64 start = ctl->start;
5360 u64 type = ctl->type;5541 u64 type = ctl->type;
5361 int ret;5542 int ret;
5362 int i;5543 int i;
5363 int j;5544 int j;
5364 5545
5365 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);5546 map = btrfs_alloc_chunk_map(ctl->num_stripes, GFP_NOFS);
5366 if (!map)5547 if (!map)
5367 return ERR_PTR(-ENOMEM);5548 return ERR_PTR(-ENOMEM);
5549
5550 map->start = start;
5551 map->chunk_len = ctl->chunk_size;
5552 map->stripe_size = ctl->stripe_size;
5553 map->type = type;
5554 map->io_align = BTRFS_STRIPE_LEN;
5555 map->io_width = BTRFS_STRIPE_LEN;
5556 map->sub_stripes = ctl->sub_stripes;
5368 map->num_stripes = ctl->num_stripes;5557 map->num_stripes = ctl->num_stripes;
5369 5558
5370 for (i = 0; i < ctl->ndevs; ++i) {5559 for (i = 0; i < ctl->ndevs; ++i) {
@@ -5375,41 +5564,22 @@ static struct btrfs_block_group *create_chunk(struct btrfs_trans_handle *trans, · 4 unmodified lines
5375 j * ctl->stripe_size;5564 j * ctl->stripe_size;
5376 }5565 }
5377 }5566 }
5378 map->io_align = BTRFS_STRIPE_LEN;
5379 map->io_width = BTRFS_STRIPE_LEN;
5380 map->type = type;
5381 map->sub_stripes = ctl->sub_stripes;
5382 5567
5383 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);5568 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
5384 5569
5385 em = alloc_extent_map();5570 ret = btrfs_add_chunk_map(info, map);
5386 if (!em) {
5387 kfree(map);
5388 return ERR_PTR(-ENOMEM);
5389 }
5390 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5391 em->map_lookup = map;
5392 em->start = start;
5393 em->len = ctl->chunk_size;
5394 em->block_start = 0;
5395 em->block_len = em->len;
5396 em->orig_block_len = ctl->stripe_size;
5397
5398 em_tree = &info->mapping_tree;
5399 write_lock(&em_tree->lock);
5400 ret = add_extent_mapping(em_tree, em, 0);
5401 if (ret) {5571 if (ret) {
5402 write_unlock(&em_tree->lock);5572 btrfs_free_chunk_map(map);
5403 free_extent_map(em);
5404 return ERR_PTR(ret);5573 return ERR_PTR(ret);
5405 }5574 }
5406 write_unlock(&em_tree->lock);
5407 5575
5408 block_group = btrfs_make_block_group(trans, type, start, ctl->chunk_size);5576 block_group = btrfs_make_block_group(trans, type, start, ctl->chunk_size);
5409 if (IS_ERR(block_group))5577 if (IS_ERR(block_group)) {
5410 goto error_del_extent;5578 btrfs_remove_chunk_map(info, map);
5579 return block_group;
5580 }
5411 5581
5412 for (i = 0; i < map->num_stripes; i++) {5582 for (int i = 0; i < map->num_stripes; i++) {
5413 struct btrfs_device *dev = map->stripes[i].dev;5583 struct btrfs_device *dev = map->stripes[i].dev;
5414 5584
5415 btrfs_device_set_bytes_used(dev,5585 btrfs_device_set_bytes_used(dev,
@@ -5422,22 +5592,9 @@ static struct btrfs_block_group *create_chunk(struct btrfs_trans_handle *trans, · 6 unmodified lines
5422 atomic64_sub(ctl->stripe_size * map->num_stripes,5592 atomic64_sub(ctl->stripe_size * map->num_stripes,
5423 &info->free_chunk_space);5593 &info->free_chunk_space);
5424 5594
5425 free_extent_map(em);
5426 check_raid56_incompat_flag(info, type);5595 check_raid56_incompat_flag(info, type);
5427 check_raid1c34_incompat_flag(info, type);5596 check_raid1c34_incompat_flag(info, type);
5428 5597
5429 return block_group;
5430
5431error_del_extent:
5432 write_lock(&em_tree->lock);
5433 remove_extent_mapping(em_tree, em);
5434 write_unlock(&em_tree->lock);
5435
5436 /* One for our allocation */
5437 free_extent_map(em);
5438 /* One for the tree reference */
5439 free_extent_map(em);
5440
5441 return block_group;5598 return block_group;
5442}5599}
5443 5600
@@ -5514,8 +5671,7 @@ int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, · 70 unmodified lines
5514 struct btrfs_key key;5671 struct btrfs_key key;
5515 struct btrfs_chunk *chunk;5672 struct btrfs_chunk *chunk;
5516 struct btrfs_stripe *stripe;5673 struct btrfs_stripe *stripe;
5517 struct extent_map *em;5674 struct btrfs_chunk_map *map;
5518 struct map_lookup *map;
5519 size_t item_size;5675 size_t item_size;
5520 int i;5676 int i;
5521 int ret;5677 int ret;
@@ -5544,14 +5700,13 @@ int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, · 22 unmodified lines
5544 */5700 */
5545 lockdep_assert_held(&fs_info->chunk_mutex);5701 lockdep_assert_held(&fs_info->chunk_mutex);
5546 5702
5547 em = btrfs_get_chunk_map(fs_info, bg->start, bg->length);5703 map = btrfs_get_chunk_map(fs_info, bg->start, bg->length);
5548 if (IS_ERR(em)) {5704 if (IS_ERR(map)) {
5549 ret = PTR_ERR(em);5705 ret = PTR_ERR(map);
5550 btrfs_abort_transaction(trans, ret);5706 btrfs_abort_transaction(trans, ret);
5551 return ret;5707 return ret;
5552 }5708 }
5553 5709
5554 map = em->map_lookup;
5555 item_size = btrfs_chunk_item_size(map->num_stripes);5710 item_size = btrfs_chunk_item_size(map->num_stripes);
5556 5711
5557 chunk = kzalloc(item_size, GFP_NOFS);5712 chunk = kzalloc(item_size, GFP_NOFS);
@@ -5608,7 +5763,7 @@ int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, · 50 unmodified lines
5608 5763
5609out:5764out:
5610 kfree(chunk);5765 kfree(chunk);
5611 free_extent_map(em);5766 btrfs_free_chunk_map(map);
5612 return ret;5767 return ret;
5613}5768}
5614 5769
@@ -5653,7 +5808,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) · 38 unmodified lines
5653 return 0;5808 return 0;
5654}5809}
5655 5810
5656static inline int btrfs_chunk_max_errors(struct map_lookup *map)5811static inline int btrfs_chunk_max_errors(struct btrfs_chunk_map *map)
5657{5812{
5658 const int index = btrfs_bg_flags_to_raid_index(map->type);5813 const int index = btrfs_bg_flags_to_raid_index(map->type);
5659 5814
@@ -5662,17 +5817,15 @@ static inline int btrfs_chunk_max_errors(struct map_lookup *map) · 2 unmodified lines
5662 5817
5663bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset)5818bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset)
5664{5819{
5665 struct extent_map *em;5820 struct btrfs_chunk_map *map;
5666 struct map_lookup *map;
5667 int miss_ndevs = 0;5821 int miss_ndevs = 0;
5668 int i;5822 int i;
5669 bool ret = true;5823 bool ret = true;
5670 5824
5671 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);5825 map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
5672 if (IS_ERR(em))5826 if (IS_ERR(map))
5673 return false;5827 return false;
5674 5828
5675 map = em->map_lookup;
5676 for (i = 0; i < map->num_stripes; i++) {5829 for (i = 0; i < map->num_stripes; i++) {
5677 if (test_bit(BTRFS_DEV_STATE_MISSING,5830 if (test_bit(BTRFS_DEV_STATE_MISSING,
5678 &map->stripes[i].dev->dev_state)) {5831 &map->stripes[i].dev->dev_state)) {
@@ -5693,38 +5846,37 @@ bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset) · 14 unmodified lines
5693 if (miss_ndevs > btrfs_chunk_max_errors(map))5846 if (miss_ndevs > btrfs_chunk_max_errors(map))
5694 ret = false;5847 ret = false;
5695end:5848end:
5696 free_extent_map(em);5849 btrfs_free_chunk_map(map);
5697 return ret;5850 return ret;
5698}5851}
5699 5852
5700void btrfs_mapping_tree_free(struct extent_map_tree *tree)5853void btrfs_mapping_tree_free(struct btrfs_fs_info *fs_info)
5701{5854{
5702 struct extent_map *em;5855 write_lock(&fs_info->mapping_tree_lock);
5856 while (!RB_EMPTY_ROOT(&fs_info->mapping_tree.rb_root)) {
5857 struct btrfs_chunk_map *map;
5858 struct rb_node *node;
5703 5859
5704 while (1) {5860 node = rb_first_cached(&fs_info->mapping_tree);
5705 write_lock(&tree->lock);5861 map = rb_entry(node, struct btrfs_chunk_map, rb_node);
5706 em = lookup_extent_mapping(tree, 0, (u64)-1);5862 rb_erase_cached(&map->rb_node, &fs_info->mapping_tree);
5707 if (em)5863 RB_CLEAR_NODE(&map->rb_node);
5708 remove_extent_mapping(tree, em);5864 chunk_map_device_clear_bits(map, CHUNK_ALLOCATED);
5709 write_unlock(&tree->lock);5865 /* Once for the tree ref. */
5710 if (!em)5866 btrfs_free_chunk_map(map);
5711 break;5867 cond_resched_rwlock_write(&fs_info->mapping_tree_lock);
5712 /* once for us */
5713 free_extent_map(em);
5714 /* once for the tree */
5715 free_extent_map(em);
5716 }5868 }
5869 write_unlock(&fs_info->mapping_tree_lock);
5717}5870}
5718 5871
5719int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)5872int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5720{5873{
5721 struct extent_map *em;5874 struct btrfs_chunk_map *map;
5722 struct map_lookup *map;
5723 enum btrfs_raid_types index;5875 enum btrfs_raid_types index;
5724 int ret = 1;5876 int ret = 1;
5725 5877
5726 em = btrfs_get_chunk_map(fs_info, logical, len);5878 map = btrfs_get_chunk_map(fs_info, logical, len);
5727 if (IS_ERR(em))5879 if (IS_ERR(map))
5728 /*5880 /*
5729 * We could return errors for these cases, but that could get5881 * We could return errors for these cases, but that could get
5730 * ugly and we'd probably do the same thing which is just not do5882 * ugly and we'd probably do the same thing which is just not do
@@ -5733,7 +5885,6 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) · 2 unmodified lines
5733 */5885 */
5734 return 1;5886 return 1;
5735 5887
5736 map = em->map_lookup;
5737 index = btrfs_bg_flags_to_raid_index(map->type);5888 index = btrfs_bg_flags_to_raid_index(map->type);
5738 5889
5739 /* Non-RAID56, use their ncopies from btrfs_raid_array. */5890 /* Non-RAID56, use their ncopies from btrfs_raid_array. */
@@ -5750,53 +5901,49 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) · 10 unmodified lines
5750 * stripe under reconstruction.5901 * stripe under reconstruction.
5751 */5902 */
5752 ret = map->num_stripes;5903 ret = map->num_stripes;
5753 free_extent_map(em);5904 btrfs_free_chunk_map(map);
5754 return ret;5905 return ret;
5755}5906}
5756 5907
5757unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,5908unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
5758 u64 logical)5909 u64 logical)
5759{5910{
5760 struct extent_map *em;5911 struct btrfs_chunk_map *map;
5761 struct map_lookup *map;
5762 unsigned long len = fs_info->sectorsize;5912 unsigned long len = fs_info->sectorsize;
5763 5913
5764 if (!btrfs_fs_incompat(fs_info, RAID56))5914 if (!btrfs_fs_incompat(fs_info, RAID56))
5765 return len;5915 return len;
5766 5916
5767 em = btrfs_get_chunk_map(fs_info, logical, len);5917 map = btrfs_get_chunk_map(fs_info, logical, len);
5768 5918
5769 if (!WARN_ON(IS_ERR(em))) {5919 if (!WARN_ON(IS_ERR(map))) {
5770 map = em->map_lookup;
5771 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)5920 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5772 len = btrfs_stripe_nr_to_offset(nr_data_stripes(map));5921 len = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
5773 free_extent_map(em);5922 btrfs_free_chunk_map(map);
5774 }5923 }
5775 return len;5924 return len;
5776}5925}
5777 5926
5778int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)5927int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5779{5928{
5780 struct extent_map *em;5929 struct btrfs_chunk_map *map;
5781 struct map_lookup *map;
5782 int ret = 0;5930 int ret = 0;
5783 5931
5784 if (!btrfs_fs_incompat(fs_info, RAID56))5932 if (!btrfs_fs_incompat(fs_info, RAID56))
5785 return 0;5933 return 0;
5786 5934
5787 em = btrfs_get_chunk_map(fs_info, logical, len);5935 map = btrfs_get_chunk_map(fs_info, logical, len);
5788 5936
5789 if(!WARN_ON(IS_ERR(em))) {5937 if (!WARN_ON(IS_ERR(map))) {
5790 map = em->map_lookup;
5791 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)5938 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5792 ret = 1;5939 ret = 1;
5793 free_extent_map(em);5940 btrfs_free_chunk_map(map);
5794 }5941 }
5795 return ret;5942 return ret;
5796}5943}
5797 5944
5798static int find_live_mirror(struct btrfs_fs_info *fs_info,5945static int find_live_mirror(struct btrfs_fs_info *fs_info,
5799 struct map_lookup *map, int first,5946 struct btrfs_chunk_map *map, int first,
5800 int dev_replace_is_ongoing)5947 int dev_replace_is_ongoing)
5801{5948{
5802 int i;5949 int i;
@@ -5903,8 +6050,7 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 100 unmodified lines
5903 u64 logical, u64 *length_ret,6050 u64 logical, u64 *length_ret,
5904 u32 *num_stripes)6051 u32 *num_stripes)
5905{6052{
5906 struct extent_map *em;6053 struct btrfs_chunk_map *map;
5907 struct map_lookup *map;
5908 struct btrfs_discard_stripe *stripes;6054 struct btrfs_discard_stripe *stripes;
5909 u64 length = *length_ret;6055 u64 length = *length_ret;
5910 u64 offset;6056 u64 offset;
@@ -5922,11 +6068,9 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 11 unmodified lines
5922 int ret;6068 int ret;
5923 int i;6069 int i;
5924 6070
5925 em = btrfs_get_chunk_map(fs_info, logical, length);6071 map = btrfs_get_chunk_map(fs_info, logical, length);
5926 if (IS_ERR(em))6072 if (IS_ERR(map))
5927 return ERR_CAST(em);6073 return ERR_CAST(map);
5928
5929 map = em->map_lookup;
5930 6074
5931 /* we don't discard raid56 yet */6075 /* we don't discard raid56 yet */
5932 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {6076 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
@@ -5934,8 +6078,8 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 1 unmodified line
5934 goto out_free_map;6078 goto out_free_map;
5935 }6079 }
5936 6080
5937 offset = logical - em->start;6081 offset = logical - map->start;
5938 length = min_t(u64, em->start + em->len - logical, length);6082 length = min_t(u64, map->start + map->chunk_len - logical, length);
5939 *length_ret = length;6083 *length_ret = length;
5940 6084
5941 /*6085 /*
@@ -6032,10 +6176,10 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 90 unmodified lines
6032 }6176 }
6033 }6177 }
6034 6178
6035 free_extent_map(em);6179 btrfs_free_chunk_map(map);
6036 return stripes;6180 return stripes;
6037out_free_map:6181out_free_map:
6038 free_extent_map(em);6182 btrfs_free_chunk_map(map);
6039 return ERR_PTR(ret);6183 return ERR_PTR(ret);
6040}6184}
6041 6185
@@ -6133,17 +6277,16 @@ static void handle_ops_on_dev_replace(enum btrfs_map_op op, · 91 unmodified lines
6133 bioc->replace_nr_stripes = nr_extra_stripes;6277 bioc->replace_nr_stripes = nr_extra_stripes;
6134}6278}
6135 6279
6136static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op,6280static u64 btrfs_max_io_len(struct btrfs_chunk_map *map, u64 offset,
6137 u64 offset, u32 *stripe_nr, u64 *stripe_offset,6281 struct btrfs_io_geometry *io_geom)
6138 u64 *full_stripe_start)
6139{6282{
6140 /*6283 /*
6141 * Stripe_nr is the stripe where this block falls. stripe_offset is6284 * Stripe_nr is the stripe where this block falls. stripe_offset is
6142 * the offset of this block in its stripe.6285 * the offset of this block in its stripe.
6143 */6286 */
6144 *stripe_offset = offset & BTRFS_STRIPE_LEN_MASK;6287 io_geom->stripe_offset = offset & BTRFS_STRIPE_LEN_MASK;
6145 *stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT;6288 io_geom->stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT;
6146 ASSERT(*stripe_offset < U32_MAX);6289 ASSERT(io_geom->stripe_offset < U32_MAX);
6147 6290
6148 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {6291 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6149 unsigned long full_stripe_len =6292 unsigned long full_stripe_len =
@@ -6158,18 +6301,17 @@ static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, · 8 unmodified lines
6158 * to go rounddown(), not round_down(), as nr_data_stripes is6301 * to go rounddown(), not round_down(), as nr_data_stripes is
6159 * not ensured to be power of 2.6302 * not ensured to be power of 2.
6160 */6303 */
6161 *full_stripe_start =6304 io_geom->raid56_full_stripe_start = btrfs_stripe_nr_to_offset(
6162 btrfs_stripe_nr_to_offset(6305 rounddown(io_geom->stripe_nr, nr_data_stripes(map)));
6163 rounddown(*stripe_nr, nr_data_stripes(map)));
6164 6306
6165 ASSERT(*full_stripe_start + full_stripe_len > offset);6307 ASSERT(io_geom->raid56_full_stripe_start + full_stripe_len > offset);
6166 ASSERT(*full_stripe_start <= offset);6308 ASSERT(io_geom->raid56_full_stripe_start <= offset);
6167 /*6309 /*
6168 * For writes to RAID56, allow to write a full stripe set, but6310 * For writes to RAID56, allow to write a full stripe set, but
6169 * no straddling of stripe sets.6311 * no straddling of stripe sets.
6170 */6312 */
6171 if (op == BTRFS_MAP_WRITE)6313 if (io_geom->op == BTRFS_MAP_WRITE)
6172 return full_stripe_len - (offset - *full_stripe_start);6314 return full_stripe_len - (offset - io_geom->raid56_full_stripe_start);
6173 }6315 }
6174 6316
6175 /*6317 /*
@@ -6177,26 +6319,180 @@ static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, · 1 unmodified line
6177 * a single disk).6319 * a single disk).
6178 */6320 */
6179 if (map->type & BTRFS_BLOCK_GROUP_STRIPE_MASK)6321 if (map->type & BTRFS_BLOCK_GROUP_STRIPE_MASK)
6180 return BTRFS_STRIPE_LEN - *stripe_offset;6322 return BTRFS_STRIPE_LEN - io_geom->stripe_offset;
6181 return U64_MAX;6323 return U64_MAX;
6182}6324}
6183 6325
6184static int set_io_stripe(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,6326static int set_io_stripe(struct btrfs_fs_info *fs_info, u64 logical,
6185 u64 logical, u64 *length, struct btrfs_io_stripe *dst,6327 u64 *length, struct btrfs_io_stripe *dst,
6186 struct map_lookup *map, u32 stripe_index,6328 struct btrfs_chunk_map *map,
6187 u64 stripe_offset, u64 stripe_nr)6329 struct btrfs_io_geometry *io_geom)
6188{6330{
6189 dst->dev = map->stripes[stripe_index].dev;6331 dst->dev = map->stripes[io_geom->stripe_index].dev;
6190 6332
6191 if (op == BTRFS_MAP_READ && btrfs_need_stripe_tree_update(fs_info, map->type))6333 if (io_geom->op == BTRFS_MAP_READ &&
6334 btrfs_need_stripe_tree_update(fs_info, map->type))
6192 return btrfs_get_raid_extent_offset(fs_info, logical, length,6335 return btrfs_get_raid_extent_offset(fs_info, logical, length,
6193 map->type, stripe_index, dst);6336 map->type,
6337 io_geom->stripe_index, dst);
6194 6338
6195 dst->physical = map->stripes[stripe_index].physical +6339 dst->physical = map->stripes[io_geom->stripe_index].physical +
6196 stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr);6340 io_geom->stripe_offset +
6341 btrfs_stripe_nr_to_offset(io_geom->stripe_nr);
6197 return 0;6342 return 0;
6198}6343}
6199 6344
6345static bool is_single_device_io(struct btrfs_fs_info *fs_info,
6346 const struct btrfs_io_stripe *smap,
6347 const struct btrfs_chunk_map *map,
6348 int num_alloc_stripes,
6349 enum btrfs_map_op op, int mirror_num)
6350{
6351 if (!smap)
6352 return false;
6353
6354 if (num_alloc_stripes != 1)
6355 return false;
6356
6357 if (btrfs_need_stripe_tree_update(fs_info, map->type) && op != BTRFS_MAP_READ)
6358 return false;
6359
6360 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1)
6361 return false;
6362
6363 return true;
6364}
6365
6366static void map_blocks_raid0(const struct btrfs_chunk_map *map,
6367 struct btrfs_io_geometry *io_geom)
6368{
6369 io_geom->stripe_index = io_geom->stripe_nr % map->num_stripes;
6370 io_geom->stripe_nr /= map->num_stripes;
6371 if (io_geom->op == BTRFS_MAP_READ)
6372 io_geom->mirror_num = 1;
6373}
6374
6375static void map_blocks_raid1(struct btrfs_fs_info *fs_info,
6376 struct btrfs_chunk_map *map,
6377 struct btrfs_io_geometry *io_geom,
6378 bool dev_replace_is_ongoing)
6379{
6380 if (io_geom->op != BTRFS_MAP_READ) {
6381 io_geom->num_stripes = map->num_stripes;
6382 return;
6383 }
6384
6385 if (io_geom->mirror_num) {
6386 io_geom->stripe_index = io_geom->mirror_num - 1;
6387 return;
6388 }
6389
6390 io_geom->stripe_index = find_live_mirror(fs_info, map, 0,
6391 dev_replace_is_ongoing);
6392 io_geom->mirror_num = io_geom->stripe_index + 1;
6393}
6394
6395static void map_blocks_dup(const struct btrfs_chunk_map *map,
6396 struct btrfs_io_geometry *io_geom)
6397{
6398 if (io_geom->op != BTRFS_MAP_READ) {
6399 io_geom->num_stripes = map->num_stripes;
6400 return;
6401 }
6402
6403 if (io_geom->mirror_num) {
6404 io_geom->stripe_index = io_geom->mirror_num - 1;
6405 return;
6406 }
6407
6408 io_geom->mirror_num = 1;
6409}
6410
6411static void map_blocks_raid10(struct btrfs_fs_info *fs_info,
6412 struct btrfs_chunk_map *map,
6413 struct btrfs_io_geometry *io_geom,
6414 bool dev_replace_is_ongoing)
6415{
6416 u32 factor = map->num_stripes / map->sub_stripes;
6417 int old_stripe_index;
6418
6419 io_geom->stripe_index = (io_geom->stripe_nr % factor) * map->sub_stripes;
6420 io_geom->stripe_nr /= factor;
6421
6422 if (io_geom->op != BTRFS_MAP_READ) {
6423 io_geom->num_stripes = map->sub_stripes;
6424 return;
6425 }
6426
6427 if (io_geom->mirror_num) {
6428 io_geom->stripe_index += io_geom->mirror_num - 1;
6429 return;
6430 }
6431
6432 old_stripe_index = io_geom->stripe_index;
6433 io_geom->stripe_index = find_live_mirror(fs_info, map,
6434 io_geom->stripe_index,
6435 dev_replace_is_ongoing);
6436 io_geom->mirror_num = io_geom->stripe_index - old_stripe_index + 1;
6437}
6438
6439static void map_blocks_raid56_write(struct btrfs_chunk_map *map,
6440 struct btrfs_io_geometry *io_geom,
6441 u64 logical, u64 *length)
6442{
6443 int data_stripes = nr_data_stripes(map);
6444
6445 /*
6446 * Needs full stripe mapping.
6447 *
6448 * Push stripe_nr back to the start of the full stripe For those cases
6449 * needing a full stripe, @stripe_nr is the full stripe number.
6450 *
6451 * Originally we go raid56_full_stripe_start / full_stripe_len, but
6452 * that can be expensive. Here we just divide @stripe_nr with
6453 * @data_stripes.
6454 */
6455 io_geom->stripe_nr /= data_stripes;
6456
6457 /* RAID[56] write or recovery. Return all stripes */
6458 io_geom->num_stripes = map->num_stripes;
6459 io_geom->max_errors = btrfs_chunk_max_errors(map);
6460
6461 /* Return the length to the full stripe end. */
6462 *length = min(logical + *length,
6463 io_geom->raid56_full_stripe_start + map->start +
6464 btrfs_stripe_nr_to_offset(data_stripes)) -
6465 logical;
6466 io_geom->stripe_index = 0;
6467 io_geom->stripe_offset = 0;
6468}
6469
6470static void map_blocks_raid56_read(struct btrfs_chunk_map *map,
6471 struct btrfs_io_geometry *io_geom)
6472{
6473 int data_stripes = nr_data_stripes(map);
6474
6475 ASSERT(io_geom->mirror_num <= 1);
6476 /* Just grab the data stripe directly. */
6477 io_geom->stripe_index = io_geom->stripe_nr % data_stripes;
6478 io_geom->stripe_nr /= data_stripes;
6479
6480 /* We distribute the parity blocks across stripes. */
6481 io_geom->stripe_index =
6482 (io_geom->stripe_nr + io_geom->stripe_index) % map->num_stripes;
6483
6484 if (io_geom->op == BTRFS_MAP_READ && io_geom->mirror_num < 1)
6485 io_geom->mirror_num = 1;
6486}
6487
6488static void map_blocks_single(const struct btrfs_chunk_map *map,
6489 struct btrfs_io_geometry *io_geom)
6490{
6491 io_geom->stripe_index = io_geom->stripe_nr % map->num_stripes;
6492 io_geom->stripe_nr /= map->num_stripes;
6493 io_geom->mirror_num = io_geom->stripe_index + 1;
6494}
6495
6200/*6496/*
6201 * Map one logical range to one or more physical ranges.6497 * Map one logical range to one or more physical ranges.
6202 *6498 *
@@ -6237,43 +6533,37 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 34 unmodified lines
6237 struct btrfs_io_context **bioc_ret,6533 struct btrfs_io_context **bioc_ret,
6238 struct btrfs_io_stripe *smap, int *mirror_num_ret)6534 struct btrfs_io_stripe *smap, int *mirror_num_ret)
6239{6535{
6240 struct extent_map *em;6536 struct btrfs_chunk_map *map;
6241 struct map_lookup *map;6537 struct btrfs_io_geometry io_geom = { 0 };
6242 u64 map_offset;6538 u64 map_offset;
6243 u64 stripe_offset;
6244 u32 stripe_nr;
6245 u32 stripe_index;
6246 int data_stripes;
6247 int i;6539 int i;
6248 int ret = 0;6540 int ret = 0;
6249 int mirror_num = (mirror_num_ret ? *mirror_num_ret : 0);
6250 int num_stripes;
6251 int num_copies;6541 int num_copies;
6252 int max_errors = 0;
6253 struct btrfs_io_context *bioc = NULL;6542 struct btrfs_io_context *bioc = NULL;
6254 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;6543 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6255 int dev_replace_is_ongoing = 0;6544 int dev_replace_is_ongoing = 0;
6256 u16 num_alloc_stripes;6545 u16 num_alloc_stripes;
6257 u64 raid56_full_stripe_start = (u64)-1;
6258 u64 max_len;6546 u64 max_len;
6259 6547
6260 ASSERT(bioc_ret);6548 ASSERT(bioc_ret);
6261 6549
6550 io_geom.mirror_num = (mirror_num_ret ? *mirror_num_ret : 0);
6551 io_geom.num_stripes = 1;
6552 io_geom.stripe_index = 0;
6553 io_geom.op = op;
6554
6262 num_copies = btrfs_num_copies(fs_info, logical, fs_info->sectorsize);6555 num_copies = btrfs_num_copies(fs_info, logical, fs_info->sectorsize);
6263 if (mirror_num > num_copies)6556 if (io_geom.mirror_num > num_copies)
6264 return -EINVAL;6557 return -EINVAL;
6265 6558
6266 em = btrfs_get_chunk_map(fs_info, logical, *length);6559 map = btrfs_get_chunk_map(fs_info, logical, *length);
6267 if (IS_ERR(em))6560 if (IS_ERR(map))
6268 return PTR_ERR(em);6561 return PTR_ERR(map);
6269 6562
6270 map = em->map_lookup;6563 map_offset = logical - map->start;
6271 data_stripes = nr_data_stripes(map);6564 io_geom.raid56_full_stripe_start = (u64)-1;
6272 6565 max_len = btrfs_max_io_len(map, map_offset, &io_geom);
6273 map_offset = logical - em->start;6566 *length = min_t(u64, map->chunk_len - map_offset, max_len);
6274 max_len = btrfs_max_io_len(map, op, map_offset, &stripe_nr,
6275 &stripe_offset, &raid56_full_stripe_start);
6276 *length = min_t(u64, em->len - map_offset, max_len);
6277 6567
6278 down_read(&dev_replace->rwsem);6568 down_read(&dev_replace->rwsem);
6279 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);6569 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
@@ -6284,107 +6574,46 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 4 unmodified lines
6284 if (!dev_replace_is_ongoing)6574 if (!dev_replace_is_ongoing)
6285 up_read(&dev_replace->rwsem);6575 up_read(&dev_replace->rwsem);
6286 6576
6287 num_stripes = 1;6577 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6288 stripe_index = 0;6578 case BTRFS_BLOCK_GROUP_RAID0:
6289 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {6579 map_blocks_raid0(map, &io_geom);
6290 stripe_index = stripe_nr % map->num_stripes;6580 break;
6291 stripe_nr /= map->num_stripes;6581 case BTRFS_BLOCK_GROUP_RAID1:
6292 if (op == BTRFS_MAP_READ)6582 case BTRFS_BLOCK_GROUP_RAID1C3:
6293 mirror_num = 1;6583 case BTRFS_BLOCK_GROUP_RAID1C4:
6294 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {6584 map_blocks_raid1(fs_info, map, &io_geom, dev_replace_is_ongoing);
6295 if (op != BTRFS_MAP_READ) {6585 break;
6296 num_stripes = map->num_stripes;6586 case BTRFS_BLOCK_GROUP_DUP:
6297 } else if (mirror_num) {6587 map_blocks_dup(map, &io_geom);
6298 stripe_index = mirror_num - 1;6588 break;
6299 } else {6589 case BTRFS_BLOCK_GROUP_RAID10:
6300 stripe_index = find_live_mirror(fs_info, map, 0,6590 map_blocks_raid10(fs_info, map, &io_geom, dev_replace_is_ongoing);
6301 dev_replace_is_ongoing);6591 break;
6302 mirror_num = stripe_index + 1;6592 case BTRFS_BLOCK_GROUP_RAID5:
6303 }6593 case BTRFS_BLOCK_GROUP_RAID6:
6304 6594 if (op != BTRFS_MAP_READ || io_geom.mirror_num > 1)
6305 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {6595 map_blocks_raid56_write(map, &io_geom, logical, length);
6306 if (op != BTRFS_MAP_READ) {6596 else
6307 num_stripes = map->num_stripes;6597 map_blocks_raid56_read(map, &io_geom);
6308 } else if (mirror_num) {6598 break;
6309 stripe_index = mirror_num - 1;6599 default:
6310 } else {
6311 mirror_num = 1;
6312 }
6313
6314 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6315 u32 factor = map->num_stripes / map->sub_stripes;
6316
6317 stripe_index = (stripe_nr % factor) * map->sub_stripes;
6318 stripe_nr /= factor;
6319
6320 if (op != BTRFS_MAP_READ)
6321 num_stripes = map->sub_stripes;
6322 else if (mirror_num)
6323 stripe_index += mirror_num - 1;
6324 else {
6325 int old_stripe_index = stripe_index;
6326 stripe_index = find_live_mirror(fs_info, map,
6327 stripe_index,
6328 dev_replace_is_ongoing);
6329 mirror_num = stripe_index - old_stripe_index + 1;
6330 }
6331
6332 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6333 if (op != BTRFS_MAP_READ || mirror_num > 1) {
6334 /*
6335 * Needs full stripe mapping.
6336 *
6337 * Push stripe_nr back to the start of the full stripe
6338 * For those cases needing a full stripe, @stripe_nr
6339 * is the full stripe number.
6340 *
6341 * Originally we go raid56_full_stripe_start / full_stripe_len,
6342 * but that can be expensive. Here we just divide
6343 * @stripe_nr with @data_stripes.
6344 */
6345 stripe_nr /= data_stripes;
6346
6347 /* RAID[56] write or recovery. Return all stripes */
6348 num_stripes = map->num_stripes;
6349 max_errors = btrfs_chunk_max_errors(map);
6350
6351 /* Return the length to the full stripe end */
6352 *length = min(logical + *length,
6353 raid56_full_stripe_start + em->start +
6354 btrfs_stripe_nr_to_offset(data_stripes)) -
6355 logical;
6356 stripe_index = 0;
6357 stripe_offset = 0;
6358 } else {
6359 ASSERT(mirror_num <= 1);
6360 /* Just grab the data stripe directly. */
6361 stripe_index = stripe_nr % data_stripes;
6362 stripe_nr /= data_stripes;
6363
6364 /* We distribute the parity blocks across stripes */
6365 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
6366 if (op == BTRFS_MAP_READ && mirror_num < 1)
6367 mirror_num = 1;
6368 }
6369 } else {
6370 /*6600 /*
6371 * After this, stripe_nr is the number of stripes on this6601 * After this, stripe_nr is the number of stripes on this
6372 * device we have to walk to find the data, and stripe_index is6602 * device we have to walk to find the data, and stripe_index is
6373 * the number of our device in the stripe array6603 * the number of our device in the stripe array
6374 */6604 */
6375 stripe_index = stripe_nr % map->num_stripes;6605 map_blocks_single(map, &io_geom);
6376 stripe_nr /= map->num_stripes;6606 break;
6377 mirror_num = stripe_index + 1;
6378 }6607 }
6379 if (stripe_index >= map->num_stripes) {6608 if (io_geom.stripe_index >= map->num_stripes) {
6380 btrfs_crit(fs_info,6609 btrfs_crit(fs_info,
6381 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",6610 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
6382 stripe_index, map->num_stripes);6611 io_geom.stripe_index, map->num_stripes);
6383 ret = -EINVAL;6612 ret = -EINVAL;
6384 goto out;6613 goto out;
6385 }6614 }
6386 6615
6387 num_alloc_stripes = num_stripes;6616 num_alloc_stripes = io_geom.num_stripes;
6388 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&6617 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
6389 op != BTRFS_MAP_READ)6618 op != BTRFS_MAP_READ)
6390 /*6619 /*
@@ -6401,14 +6630,11 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 10 unmodified lines
6401 * physical block information on the stack instead of allocating an6630 * physical block information on the stack instead of allocating an
6402 * I/O context structure.6631 * I/O context structure.
6403 */6632 */
6404 if (smap && num_alloc_stripes == 1 &&6633 if (is_single_device_io(fs_info, smap, map, num_alloc_stripes, op,
6405 !(btrfs_need_stripe_tree_update(fs_info, map->type) &&6634 io_geom.mirror_num)) {
6406 op != BTRFS_MAP_READ) &&6635 ret = set_io_stripe(fs_info, logical, length, smap, map, &io_geom);
6407 !((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1)) {
6408 ret = set_io_stripe(fs_info, op, logical, length, smap, map,
6409 stripe_index, stripe_offset, stripe_nr);
6410 if (mirror_num_ret)6636 if (mirror_num_ret)
6411 *mirror_num_ret = mirror_num;6637 *mirror_num_ret = io_geom.mirror_num;
6412 *bioc_ret = NULL;6638 *bioc_ret = NULL;
6413 goto out;6639 goto out;
6414 }6640 }
@@ -6428,7 +6654,7 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 13 unmodified lines
6428 * It's still mostly the same as other profiles, just with extra rotation.6654 * It's still mostly the same as other profiles, just with extra rotation.
6429 */6655 */
6430 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&6656 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
6431 (op != BTRFS_MAP_READ || mirror_num > 1)) {6657 (op != BTRFS_MAP_READ || io_geom.mirror_num > 1)) {
6432 /*6658 /*
6433 * For RAID56 @stripe_nr is already the number of full stripes6659 * For RAID56 @stripe_nr is already the number of full stripes
6434 * before us, which is also the rotation value (needs to modulo6660 * before us, which is also the rotation value (needs to modulo
@@ -6437,28 +6663,31 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 2 unmodified lines
6437 * In this case, we just add @stripe_nr with @i, then do the6663 * In this case, we just add @stripe_nr with @i, then do the
6438 * modulo, to reduce one modulo call.6664 * modulo, to reduce one modulo call.
6439 */6665 */
6440 bioc->full_stripe_logical = em->start +6666 bioc->full_stripe_logical = map->start +
6441 btrfs_stripe_nr_to_offset(stripe_nr * data_stripes);6667 btrfs_stripe_nr_to_offset(io_geom.stripe_nr *
6442 for (int i = 0; i < num_stripes; i++) {6668 nr_data_stripes(map));
6443 ret = set_io_stripe(fs_info, op, logical, length,6669 for (int i = 0; i < io_geom.num_stripes; i++) {
6444 &bioc->stripes[i], map,6670 struct btrfs_io_stripe *dst = &bioc->stripes[i];
6445 (i + stripe_nr) % num_stripes,6671 u32 stripe_index;
6446 stripe_offset, stripe_nr);6672
6447 if (ret < 0)6673 stripe_index = (i + io_geom.stripe_nr) % io_geom.num_stripes;
6448 break;6674 dst->dev = map->stripes[stripe_index].dev;
6675 dst->physical =
6676 map->stripes[stripe_index].physical +
6677 io_geom.stripe_offset +
6678 btrfs_stripe_nr_to_offset(io_geom.stripe_nr);
6449 }6679 }
6450 } else {6680 } else {
6451 /*6681 /*
6452 * For all other non-RAID56 profiles, just copy the target6682 * For all other non-RAID56 profiles, just copy the target
6453 * stripe into the bioc.6683 * stripe into the bioc.
6454 */6684 */
6455 for (i = 0; i < num_stripes; i++) {6685 for (i = 0; i < io_geom.num_stripes; i++) {
6456 ret = set_io_stripe(fs_info, op, logical, length,6686 ret = set_io_stripe(fs_info, logical, length,
6457 &bioc->stripes[i], map, stripe_index,6687 &bioc->stripes[i], map, &io_geom);
6458 stripe_offset, stripe_nr);
6459 if (ret < 0)6688 if (ret < 0)
6460 break;6689 break;
6461 stripe_index++;6690 io_geom.stripe_index++;
6462 }6691 }
6463 }6692 }
6464 6693
@@ -6469,18 +6698,18 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 4 unmodified lines
6469 }6698 }
6470 6699
6471 if (op != BTRFS_MAP_READ)6700 if (op != BTRFS_MAP_READ)
6472 max_errors = btrfs_chunk_max_errors(map);6701 io_geom.max_errors = btrfs_chunk_max_errors(map);
6473 6702
6474 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&6703 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
6475 op != BTRFS_MAP_READ) {6704 op != BTRFS_MAP_READ) {
6476 handle_ops_on_dev_replace(op, bioc, dev_replace, logical,6705 handle_ops_on_dev_replace(op, bioc, dev_replace, logical,
6477 &num_stripes, &max_errors);6706 &io_geom.num_stripes, &io_geom.max_errors);
6478 }6707 }
6479 6708
6480 *bioc_ret = bioc;6709 *bioc_ret = bioc;
6481 bioc->num_stripes = num_stripes;6710 bioc->num_stripes = io_geom.num_stripes;
6482 bioc->max_errors = max_errors;6711 bioc->max_errors = io_geom.max_errors;
6483 bioc->mirror_num = mirror_num;6712 bioc->mirror_num = io_geom.mirror_num;
6484 6713
6485out:6714out:
6486 if (dev_replace_is_ongoing) {6715 if (dev_replace_is_ongoing) {
@@ -6488,7 +6717,7 @@ out: · 1 unmodified line
6488 /* Unlock and let waiting writers proceed */6717 /* Unlock and let waiting writers proceed */
6489 up_read(&dev_replace->rwsem);6718 up_read(&dev_replace->rwsem);
6490 }6719 }
6491 free_extent_map(em);6720 btrfs_free_chunk_map(map);
6492 return ret;6721 return ret;
6493}6722}
6494 6723
@@ -6660,12 +6889,11 @@ static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, · 165 unmodified lines
6660 devid, uuid);6889 devid, uuid);
6661}6890}
6662 6891
6663u64 btrfs_calc_stripe_length(const struct extent_map *em)6892u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map)
6664{6893{
6665 const struct map_lookup *map = em->map_lookup;
6666 const int data_stripes = calc_data_stripes(map->type, map->num_stripes);6894 const int data_stripes = calc_data_stripes(map->type, map->num_stripes);
6667 6895
6668 return div_u64(em->len, data_stripes);6896 return div_u64(map->chunk_len, data_stripes);
6669}6897}
6670 6898
6671#if BITS_PER_LONG == 326899#if BITS_PER_LONG == 32
@@ -6734,9 +6962,7 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 62 unmodified lines
6734{6962{
6735 BTRFS_DEV_LOOKUP_ARGS(args);6963 BTRFS_DEV_LOOKUP_ARGS(args);
6736 struct btrfs_fs_info *fs_info = leaf->fs_info;6964 struct btrfs_fs_info *fs_info = leaf->fs_info;
6737 struct extent_map_tree *map_tree = &fs_info->mapping_tree;6965 struct btrfs_chunk_map *map;
6738 struct map_lookup *map;
6739 struct extent_map *em;
6740 u64 logical;6966 u64 logical;
6741 u64 length;6967 u64 length;
6742 u64 devid;6968 u64 devid;
@@ -6770,35 +6996,22 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 27 unmodified lines
6770 return ret;6996 return ret;
6771 }6997 }
6772 6998
6773 read_lock(&map_tree->lock);6999 map = btrfs_find_chunk_map(fs_info, logical, 1);
6774 em = lookup_extent_mapping(map_tree, logical, 1);
6775 read_unlock(&map_tree->lock);
6776 7000
6777 /* already mapped? */7001 /* already mapped? */
6778 if (em && em->start <= logical && em->start + em->len > logical) {7002 if (map && map->start <= logical && map->start + map->chunk_len > logical) {
6779 free_extent_map(em);7003 btrfs_free_chunk_map(map);
6780 return 0;7004 return 0;
6781 } else if (em) {7005 } else if (map) {
6782 free_extent_map(em);7006 btrfs_free_chunk_map(map);
6783 }7007 }
6784 7008
6785 em = alloc_extent_map();7009 map = btrfs_alloc_chunk_map(num_stripes, GFP_NOFS);
6786 if (!em)7010 if (!map)
6787 return -ENOMEM;
6788 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6789 if (!map) {
6790 free_extent_map(em);
6791 return -ENOMEM;7011 return -ENOMEM;
6792 }
6793
6794 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6795 em->map_lookup = map;
6796 em->start = logical;
6797 em->len = length;
6798 em->orig_start = 0;
6799 em->block_start = 0;
6800 em->block_len = em->len;
6801 7012
7013 map->start = logical;
7014 map->chunk_len = length;
6802 map->num_stripes = num_stripes;7015 map->num_stripes = num_stripes;
6803 map->io_width = btrfs_chunk_io_width(leaf, chunk);7016 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6804 map->io_align = btrfs_chunk_io_align(leaf, chunk);7017 map->io_align = btrfs_chunk_io_align(leaf, chunk);
@@ -6813,7 +7026,7 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 8 unmodified lines
6813 */7026 */
6814 map->sub_stripes = btrfs_raid_array[index].sub_stripes;7027 map->sub_stripes = btrfs_raid_array[index].sub_stripes;
6815 map->verified_stripes = 0;7028 map->verified_stripes = 0;
6816 em->orig_block_len = btrfs_calc_stripe_length(em);7029 map->stripe_size = btrfs_calc_stripe_length(map);
6817 for (i = 0; i < num_stripes; i++) {7030 for (i = 0; i < num_stripes; i++) {
6818 map->stripes[i].physical =7031 map->stripes[i].physical =
6819 btrfs_stripe_offset_nr(leaf, chunk, i);7032 btrfs_stripe_offset_nr(leaf, chunk, i);
@@ -6829,7 +7042,7 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 9 unmodified lines
6829 devid, uuid);7042 devid, uuid);
6830 if (IS_ERR(map->stripes[i].dev)) {7043 if (IS_ERR(map->stripes[i].dev)) {
6831 ret = PTR_ERR(map->stripes[i].dev);7044 ret = PTR_ERR(map->stripes[i].dev);
6832 free_extent_map(em);7045 btrfs_free_chunk_map(map);
6833 return ret;7046 return ret;
6834 }7047 }
6835 }7048 }
@@ -6838,15 +7051,12 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 2 unmodified lines
6838 &(map->stripes[i].dev->dev_state));7051 &(map->stripes[i].dev->dev_state));
6839 }7052 }
6840 7053
6841 write_lock(&map_tree->lock);7054 ret = btrfs_add_chunk_map(fs_info, map);
6842 ret = add_extent_mapping(map_tree, em, 0);
6843 write_unlock(&map_tree->lock);
6844 if (ret < 0) {7055 if (ret < 0) {
6845 btrfs_err(fs_info,7056 btrfs_err(fs_info,
6846 "failed to add chunk map, start=%llu len=%llu: %d",7057 "failed to add chunk map, start=%llu len=%llu: %d",
6847 em->start, em->len, ret);7058 map->start, map->chunk_len, ret);
6848 }7059 }
6849 free_extent_map(em);
6850 7060
6851 return ret;7061 return ret;
6852}7062}
@@ -7156,26 +7366,21 @@ out_short_read: · 303 unmodified lines
7156bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,7366bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7157 struct btrfs_device *failing_dev)7367 struct btrfs_device *failing_dev)
7158{7368{
7159 struct extent_map_tree *map_tree = &fs_info->mapping_tree;7369 struct btrfs_chunk_map *map;
7160 struct extent_map *em;7370 u64 next_start;
7161 u64 next_start = 0;
7162 bool ret = true;7371 bool ret = true;
7163 7372
7164 read_lock(&map_tree->lock);7373 map = btrfs_find_chunk_map(fs_info, 0, U64_MAX);
7165 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7166 read_unlock(&map_tree->lock);
7167 /* No chunk at all? Return false anyway */7374 /* No chunk at all? Return false anyway */
7168 if (!em) {7375 if (!map) {
7169 ret = false;7376 ret = false;
7170 goto out;7377 goto out;
7171 }7378 }
7172 while (em) {7379 while (map) {
7173 struct map_lookup *map;
7174 int missing = 0;7380 int missing = 0;
7175 int max_tolerated;7381 int max_tolerated;
7176 int i;7382 int i;
7177 7383
7178 map = em->map_lookup;
7179 max_tolerated =7384 max_tolerated =
7180 btrfs_get_num_tolerated_disk_barrier_failures(7385 btrfs_get_num_tolerated_disk_barrier_failures(
7181 map->type);7386 map->type);
@@ -7193,18 +7398,15 @@ bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, · 11 unmodified lines
7193 if (!failing_dev)7398 if (!failing_dev)
7194 btrfs_warn(fs_info,7399 btrfs_warn(fs_info,
7195 "chunk %llu missing %d devices, max tolerance is %d for writable mount",7400 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
7196 em->start, missing, max_tolerated);7401 map->start, missing, max_tolerated);
7197 free_extent_map(em);7402 btrfs_free_chunk_map(map);
7198 ret = false;7403 ret = false;
7199 goto out;7404 goto out;
7200 }7405 }
7201 next_start = extent_map_end(em);7406 next_start = map->start + map->chunk_len;
7202 free_extent_map(em);7407 btrfs_free_chunk_map(map);
7203 7408
7204 read_lock(&map_tree->lock);7409 map = btrfs_find_chunk_map(fs_info, next_start, U64_MAX - next_start);
7205 em = lookup_extent_mapping(map_tree, next_start,
7206 (u64)(-1) - next_start);
7207 read_unlock(&map_tree->lock);
7208 }7410 }
7209out:7411out:
7210 return ret;7412 return ret;
@@ -7697,20 +7899,15 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 486 unmodified lines
7697 u64 physical_offset, u64 physical_len)7899 u64 physical_offset, u64 physical_len)
7698{7900{
7699 struct btrfs_dev_lookup_args args = { .devid = devid };7901 struct btrfs_dev_lookup_args args = { .devid = devid };
7700 struct extent_map_tree *em_tree = &fs_info->mapping_tree;7902 struct btrfs_chunk_map *map;
7701 struct extent_map *em;
7702 struct map_lookup *map;
7703 struct btrfs_device *dev;7903 struct btrfs_device *dev;
7704 u64 stripe_len;7904 u64 stripe_len;
7705 bool found = false;7905 bool found = false;
7706 int ret = 0;7906 int ret = 0;
7707 int i;7907 int i;
7708 7908
7709 read_lock(&em_tree->lock);7909 map = btrfs_find_chunk_map(fs_info, chunk_offset, 1);
7710 em = lookup_extent_mapping(em_tree, chunk_offset, 1);7910 if (!map) {
7711 read_unlock(&em_tree->lock);
7712
7713 if (!em) {
7714 btrfs_err(fs_info,7911 btrfs_err(fs_info,
7715"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",7912"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7716 physical_offset, devid);7913 physical_offset, devid);
@@ -7718,12 +7915,11 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 1 unmodified line
7718 goto out;7915 goto out;
7719 }7916 }
7720 7917
7721 map = em->map_lookup;7918 stripe_len = btrfs_calc_stripe_length(map);
7722 stripe_len = btrfs_calc_stripe_length(em);
7723 if (physical_len != stripe_len) {7919 if (physical_len != stripe_len) {
7724 btrfs_err(fs_info,7920 btrfs_err(fs_info,
7725"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",7921"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7726 physical_offset, devid, em->start, physical_len,7922 physical_offset, devid, map->start, physical_len,
7727 stripe_len);7923 stripe_len);
7728 ret = -EUCLEAN;7924 ret = -EUCLEAN;
7729 goto out;7925 goto out;
@@ -7746,7 +7942,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 16 unmodified lines
7746 if (map->verified_stripes >= map->num_stripes) {7942 if (map->verified_stripes >= map->num_stripes) {
7747 btrfs_err(fs_info,7943 btrfs_err(fs_info,
7748 "too many dev extents for chunk %llu found",7944 "too many dev extents for chunk %llu found",
7749 em->start);7945 map->start);
7750 ret = -EUCLEAN;7946 ret = -EUCLEAN;
7751 goto out;7947 goto out;
7752 }7948 }
@@ -7792,32 +7988,30 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 39 unmodified lines
7792 }7988 }
7793 7989
7794out:7990out:
7795 free_extent_map(em);7991 btrfs_free_chunk_map(map);
7796 return ret;7992 return ret;
7797}7993}
7798 7994
7799static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)7995static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7800{7996{
7801 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7802 struct extent_map *em;
7803 struct rb_node *node;7997 struct rb_node *node;
7804 int ret = 0;7998 int ret = 0;
7805 7999
7806 read_lock(&em_tree->lock);8000 read_lock(&fs_info->mapping_tree_lock);
7807 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {8001 for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) {
7808 em = rb_entry(node, struct extent_map, rb_node);8002 struct btrfs_chunk_map *map;
7809 if (em->map_lookup->num_stripes !=8003
7810 em->map_lookup->verified_stripes) {8004 map = rb_entry(node, struct btrfs_chunk_map, rb_node);
8005 if (map->num_stripes != map->verified_stripes) {
7811 btrfs_err(fs_info,8006 btrfs_err(fs_info,
7812 "chunk %llu has missing dev extent, have %d expect %d",8007 "chunk %llu has missing dev extent, have %d expect %d",
7813 em->start, em->map_lookup->verified_stripes,8008 map->start, map->verified_stripes, map->num_stripes);
7814 em->map_lookup->num_stripes);
7815 ret = -EUCLEAN;8009 ret = -EUCLEAN;
7816 goto out;8010 goto out;
7817 }8011 }
7818 }8012 }
7819out:8013out:
7820 read_unlock(&em_tree->lock);8014 read_unlock(&fs_info->mapping_tree_lock);
7821 return ret;8015 return ret;
7822}8016}
7823 8017
fs/btrfs/volumes.h+37−10
@@ -426,7 +426,8 @@ struct btrfs_discard_stripe { · 425 unmodified lines
426struct btrfs_io_context {426struct btrfs_io_context {
427 refcount_t refs;427 refcount_t refs;
428 struct btrfs_fs_info *fs_info;428 struct btrfs_fs_info *fs_info;
429 u64 map_type; /* get from map_lookup->type */429 /* Taken from struct btrfs_chunk_map::type. */
430 u64 map_type;
430 struct bio *orig_bio;431 struct bio *orig_bio;
431 atomic_t error;432 atomic_t error;
432 u16 max_errors;433 u16 max_errors;
@@ -529,18 +530,32 @@ struct btrfs_raid_attr { · 96 unmodified lines
529 530
530extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];531extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
531 532
532struct map_lookup {533struct btrfs_chunk_map {
534 struct rb_node rb_node;
535 /* For mount time dev extent verification. */
536 int verified_stripes;
537 refcount_t refs;
538 u64 start;
539 u64 chunk_len;
540 u64 stripe_size;
533 u64 type;541 u64 type;
534 int io_align;542 int io_align;
535 int io_width;543 int io_width;
536 int num_stripes;544 int num_stripes;
537 int sub_stripes;545 int sub_stripes;
538 int verified_stripes; /* For mount time dev extent verification */
539 struct btrfs_io_stripe stripes[];546 struct btrfs_io_stripe stripes[];
540};547};
541 548
542#define map_lookup_size(n) (sizeof(struct map_lookup) + \549#define btrfs_chunk_map_size(n) (sizeof(struct btrfs_chunk_map) + \
543 (sizeof(struct btrfs_io_stripe) * (n)))550 (sizeof(struct btrfs_io_stripe) * (n)))
551
552static inline void btrfs_free_chunk_map(struct btrfs_chunk_map *map)
553{
554 if (map && refcount_dec_and_test(&map->refs)) {
555 ASSERT(RB_EMPTY_NODE(&map->rb_node));
556 kfree(map);
557 }
558}
544 559
545struct btrfs_balance_args;560struct btrfs_balance_args;
546struct btrfs_balance_progress;561struct btrfs_balance_progress;
@@ -598,7 +613,7 @@ static inline unsigned long btrfs_chunk_item_size(int num_stripes) · 51 unmodified lines
598}613}
599 614
600/*615/*
601 * Do the type safe converstion from stripe_nr to offset inside the chunk.616 * Do the type safe conversion from stripe_nr to offset inside the chunk.
602 *617 *
603 * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger618 * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
604 * than 4G. This does the proper type cast to avoid overflow.619 * than 4G. This does the proper type cast to avoid overflow.
@@ -624,7 +639,7 @@ int btrfs_read_sys_array(struct btrfs_fs_info *fs_info); · 19 unmodified lines
624int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);639int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
625struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,640struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
626 u64 type);641 u64 type);
627void btrfs_mapping_tree_free(struct extent_map_tree *tree);642void btrfs_mapping_tree_free(struct btrfs_fs_info *fs_info);
628int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,643int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
629 blk_mode_t flags, void *holder);644 blk_mode_t flags, void *holder);
630struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,645struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
@@ -680,13 +695,25 @@ int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, · 49 unmodified lines
680 u64 logical, u64 len);695 u64 logical, u64 len);
681unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,696unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
682 u64 logical);697 u64 logical);
683u64 btrfs_calc_stripe_length(const struct extent_map *em);698u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map);
684int btrfs_nr_parity_stripes(u64 type);699int btrfs_nr_parity_stripes(u64 type);
685int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,700int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
686 struct btrfs_block_group *bg);701 struct btrfs_block_group *bg);
687int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);702int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
688struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,703
689 u64 logical, u64 length);704#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
705struct btrfs_chunk_map *btrfs_alloc_chunk_map(int num_stripes, gfp_t gfp);
706int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map);
707#endif
708
709struct btrfs_chunk_map *btrfs_clone_chunk_map(struct btrfs_chunk_map *map, gfp_t gfp);
710struct btrfs_chunk_map *btrfs_find_chunk_map(struct btrfs_fs_info *fs_info,
711 u64 logical, u64 length);
712struct btrfs_chunk_map *btrfs_find_chunk_map_nolock(struct btrfs_fs_info *fs_info,
713 u64 logical, u64 length);
714struct btrfs_chunk_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
715 u64 logical, u64 length);
716void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map);
690void btrfs_release_disk_super(struct btrfs_super_block *super);717void btrfs_release_disk_super(struct btrfs_super_block *super);
691 718
692static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,719static inline void btrfs_dev_stat_inc(struct btrfs_device *dev,
fs/btrfs/xattr.c+53−2
@@ -382,6 +382,53 @@ static int btrfs_xattr_handler_set(const struct xattr_handler *handler, · 381 unmodified lines
382 return btrfs_setxattr_trans(inode, name, buffer, size, flags);382 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
383}383}
384 384
385static int btrfs_xattr_handler_get_security(const struct xattr_handler *handler,
386 struct dentry *unused,
387 struct inode *inode,
388 const char *name, void *buffer,
389 size_t size)
390{
391 int ret;
392 bool is_cap = false;
393
394 name = xattr_full_name(handler, name);
395
396 /*
397 * security.capability doesn't cache the results, so calls into us
398 * constantly to see if there's a capability xattr. Cache the result
399 * here in order to avoid wasting time doing lookups for xattrs we know
400 * don't exist.
401 */
402 if (strcmp(name, XATTR_NAME_CAPS) == 0) {
403 is_cap = true;
404 if (test_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags))
405 return -ENODATA;
406 }
407
408 ret = btrfs_getxattr(inode, name, buffer, size);
409 if (ret == -ENODATA && is_cap)
410 set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
411 return ret;
412}
413
414static int btrfs_xattr_handler_set_security(const struct xattr_handler *handler,
415 struct mnt_idmap *idmap,
416 struct dentry *unused,
417 struct inode *inode,
418 const char *name,
419 const void *buffer,
420 size_t size, int flags)
421{
422 if (btrfs_root_readonly(BTRFS_I(inode)->root))
423 return -EROFS;
424
425 name = xattr_full_name(handler, name);
426 if (strcmp(name, XATTR_NAME_CAPS) == 0)
427 clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
428
429 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
430}
431
385static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,432static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
386 struct mnt_idmap *idmap,433 struct mnt_idmap *idmap,
387 struct dentry *unused, struct inode *inode,434 struct dentry *unused, struct inode *inode,
@@ -420,8 +467,8 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, · 32 unmodified lines
420 467
421static const struct xattr_handler btrfs_security_xattr_handler = {468static const struct xattr_handler btrfs_security_xattr_handler = {
422 .prefix = XATTR_SECURITY_PREFIX,469 .prefix = XATTR_SECURITY_PREFIX,
423 .get = btrfs_xattr_handler_get,470 .get = btrfs_xattr_handler_get_security,
424 .set = btrfs_xattr_handler_set,471 .set = btrfs_xattr_handler_set_security,
425};472};
426 473
427static const struct xattr_handler btrfs_trusted_xattr_handler = {474static const struct xattr_handler btrfs_trusted_xattr_handler = {
@@ -473,6 +520,10 @@ static int btrfs_initxattrs(struct inode *inode, · 45 unmodified lines
473 }520 }
474 strcpy(name, XATTR_SECURITY_PREFIX);521 strcpy(name, XATTR_SECURITY_PREFIX);
475 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);522 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
523
524 if (strcmp(name, XATTR_NAME_CAPS) == 0)
525 clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
526
476 err = btrfs_setxattr(trans, inode, name, xattr->value,527 err = btrfs_setxattr(trans, inode, name, xattr->value,
477 xattr->value_len, 0);528 xattr->value_len, 0);
478 kfree(name);529 kfree(name);
fs/btrfs/zlib.c+3−3
@@ -121,7 +121,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · 120 unmodified lines
121 workspace->strm.total_in = 0;121 workspace->strm.total_in = 0;
122 workspace->strm.total_out = 0;122 workspace->strm.total_out = 0;
123 123
124 out_page = alloc_page(GFP_NOFS);124 out_page = btrfs_alloc_compr_page();
125 if (out_page == NULL) {125 if (out_page == NULL) {
126 ret = -ENOMEM;126 ret = -ENOMEM;
127 goto out;127 goto out;
@@ -200,7 +200,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · 72 unmodified lines
200 ret = -E2BIG;200 ret = -E2BIG;
201 goto out;201 goto out;
202 }202 }
203 out_page = alloc_page(GFP_NOFS);203 out_page = btrfs_alloc_compr_page();
204 if (out_page == NULL) {204 if (out_page == NULL) {
205 ret = -ENOMEM;205 ret = -ENOMEM;
206 goto out;206 goto out;
@@ -236,7 +236,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · 29 unmodified lines
236 ret = -E2BIG;236 ret = -E2BIG;
237 goto out;237 goto out;
238 }238 }
239 out_page = alloc_page(GFP_NOFS);239 out_page = btrfs_alloc_compr_page();
240 if (out_page == NULL) {240 if (out_page == NULL) {
241 ret = -ENOMEM;241 ret = -ENOMEM;
242 goto out;242 goto out;
fs/btrfs/zoned.c+22−44
@@ -781,7 +781,7 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info) · 780 unmodified lines
781 * Check mount options here, because we might change fs_info->zoned781 * Check mount options here, because we might change fs_info->zoned
782 * from fs_info->zone_size.782 * from fs_info->zone_size.
783 */783 */
784 ret = btrfs_check_mountopts_zoned(fs_info);784 ret = btrfs_check_mountopts_zoned(fs_info, &fs_info->mount_opt);
785 if (ret)785 if (ret)
786 return ret;786 return ret;
787 787
@@ -789,7 +789,7 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info) · 1 unmodified line
789 return 0;789 return 0;
790}790}
791 791
792int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)792int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info, unsigned long *mount_opt)
793{793{
794 if (!btrfs_is_zoned(info))794 if (!btrfs_is_zoned(info))
795 return 0;795 return 0;
@@ -798,18 +798,21 @@ int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info) · 2 unmodified lines
798 * Space cache writing is not COWed. Disable that to avoid write errors798 * Space cache writing is not COWed. Disable that to avoid write errors
799 * in sequential zones.799 * in sequential zones.
800 */800 */
801 if (btrfs_test_opt(info, SPACE_CACHE)) {801 if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
802 btrfs_err(info, "zoned: space cache v1 is not supported");802 btrfs_err(info, "zoned: space cache v1 is not supported");
803 return -EINVAL;803 return -EINVAL;
804 }804 }
805 805
806 if (btrfs_test_opt(info, NODATACOW)) {806 if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) {
807 btrfs_err(info, "zoned: NODATACOW not supported");807 btrfs_err(info, "zoned: NODATACOW not supported");
808 return -EINVAL;808 return -EINVAL;
809 }809 }
810 810
811 btrfs_clear_and_info(info, DISCARD_ASYNC,811 if (btrfs_raw_test_opt(*mount_opt, DISCARD_ASYNC)) {
812 "zoned: async discard ignored and disabled for zoned mode");812 btrfs_info(info,
813 "zoned: async discard ignored and disabled for zoned mode");
814 btrfs_clear_opt(*mount_opt, DISCARD_ASYNC);
815 }
813 816
814 return 0;817 return 0;
815}818}
@@ -1290,7 +1293,7 @@ struct zone_info { · 474 unmodified lines
1290 1293
1291static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,1294static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
1292 struct zone_info *info, unsigned long *active,1295 struct zone_info *info, unsigned long *active,
1293 struct map_lookup *map)1296 struct btrfs_chunk_map *map)
1294{1297{
1295 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;1298 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1296 struct btrfs_device *device = map->stripes[zone_idx].dev;1299 struct btrfs_device *device = map->stripes[zone_idx].dev;
@@ -1393,7 +1396,7 @@ static int btrfs_load_block_group_single(struct btrfs_block_group *bg, · 96 unmodified lines
1393}1396}
1394 1397
1395static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,1398static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
1396 struct map_lookup *map,1399 struct btrfs_chunk_map *map,
1397 struct zone_info *zone_info,1400 struct zone_info *zone_info,
1398 unsigned long *active)1401 unsigned long *active)
1399{1402{
@@ -1435,7 +1438,7 @@ static int btrfs_load_block_group_dup(struct btrfs_block_group *bg, · 35 unmodified lines
1435}1438}
1436 1439
1437static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,1440static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
1438 struct map_lookup *map,1441 struct btrfs_chunk_map *map,
1439 struct zone_info *zone_info,1442 struct zone_info *zone_info,
1440 unsigned long *active)1443 unsigned long *active)
1441{1444{
@@ -1483,7 +1486,7 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg, · 41 unmodified lines
1483}1486}
1484 1487
1485static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,1488static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
1486 struct map_lookup *map,1489 struct btrfs_chunk_map *map,
1487 struct zone_info *zone_info,1490 struct zone_info *zone_info,
1488 unsigned long *active)1491 unsigned long *active)
1489{1492{
@@ -1515,7 +1518,7 @@ static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg, · 25 unmodified lines
1515}1518}
1516 1519
1517static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,1520static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
1518 struct map_lookup *map,1521 struct btrfs_chunk_map *map,
1519 struct zone_info *zone_info,1522 struct zone_info *zone_info,
1520 unsigned long *active)1523 unsigned long *active)
1521{1524{
@@ -1552,9 +1555,7 @@ static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg, · 30 unmodified lines
1552int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)1555int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1553{1556{
1554 struct btrfs_fs_info *fs_info = cache->fs_info;1557 struct btrfs_fs_info *fs_info = cache->fs_info;
1555 struct extent_map_tree *em_tree = &fs_info->mapping_tree;1558 struct btrfs_chunk_map *map;
1556 struct extent_map *em;
1557 struct map_lookup *map;
1558 u64 logical = cache->start;1559 u64 logical = cache->start;
1559 u64 length = cache->length;1560 u64 length = cache->length;
1560 struct zone_info *zone_info = NULL;1561 struct zone_info *zone_info = NULL;
@@ -1575,17 +1576,11 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · 14 unmodified lines
1575 return -EIO;1576 return -EIO;
1576 }1577 }
1577 1578
1578 /* Get the chunk mapping */1579 map = btrfs_find_chunk_map(fs_info, logical, length);
1579 read_lock(&em_tree->lock);1580 if (!map)
1580 em = lookup_extent_mapping(em_tree, logical, length);
1581 read_unlock(&em_tree->lock);
1582
1583 if (!em)
1584 return -EINVAL;1581 return -EINVAL;
1585 1582
1586 map = em->map_lookup;1583 cache->physical_map = btrfs_clone_chunk_map(map, GFP_NOFS);
1587
1588 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1589 if (!cache->physical_map) {1584 if (!cache->physical_map) {
1590 ret = -ENOMEM;1585 ret = -ENOMEM;
1591 goto out;1586 goto out;
@@ -1687,12 +1682,11 @@ out: · 95 unmodified lines
1687 spin_unlock(&fs_info->zone_active_bgs_lock);1682 spin_unlock(&fs_info->zone_active_bgs_lock);
1688 }1683 }
1689 } else {1684 } else {
1690 kfree(cache->physical_map);1685 btrfs_free_chunk_map(cache->physical_map);
1691 cache->physical_map = NULL;1686 cache->physical_map = NULL;
1692 }1687 }
1693 bitmap_free(active);1688 bitmap_free(active);
1694 kfree(zone_info);1689 kfree(zone_info);
1695 free_extent_map(em);
1696 1690
1697 return ret;1691 return ret;
1698}1692}
@@ -1715,22 +1709,6 @@ void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) · 16 unmodified lines
1715 cache->zone_unusable = unusable;1709 cache->zone_unusable = unusable;
1716}1710}
1717 1711
1718void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1719 struct extent_buffer *eb)
1720{
1721 if (!btrfs_is_zoned(eb->fs_info) ||
1722 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN))
1723 return;
1724
1725 ASSERT(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1726
1727 memzero_extent_buffer(eb, 0, eb->len);
1728 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1729 set_extent_buffer_dirty(eb);
1730 set_extent_bit(&trans->dirty_pages, eb->start, eb->start + eb->len - 1,
1731 EXTENT_DIRTY, NULL);
1732}
1733
1734bool btrfs_use_zone_append(struct btrfs_bio *bbio)1712bool btrfs_use_zone_append(struct btrfs_bio *bbio)
1735{1713{
1736 u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);1714 u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT);
@@ -2082,7 +2060,7 @@ int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical, · 345 unmodified lines
2082bool btrfs_zone_activate(struct btrfs_block_group *block_group)2060bool btrfs_zone_activate(struct btrfs_block_group *block_group)
2083{2061{
2084 struct btrfs_fs_info *fs_info = block_group->fs_info;2062 struct btrfs_fs_info *fs_info = block_group->fs_info;
2085 struct map_lookup *map;2063 struct btrfs_chunk_map *map;
2086 struct btrfs_device *device;2064 struct btrfs_device *device;
2087 u64 physical;2065 u64 physical;
2088 const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);2066 const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
@@ -2194,7 +2172,7 @@ static void wait_eb_writebacks(struct btrfs_block_group *block_group) · 105 unmodified lines
2194static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)2172static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
2195{2173{
2196 struct btrfs_fs_info *fs_info = block_group->fs_info;2174 struct btrfs_fs_info *fs_info = block_group->fs_info;
2197 struct map_lookup *map;2175 struct btrfs_chunk_map *map;
2198 const bool is_metadata = (block_group->flags &2176 const bool is_metadata = (block_group->flags &
2199 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));2177 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
2200 int ret = 0;2178 int ret = 0;
@@ -2643,7 +2621,7 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) · 442 unmodified lines
2643 /* Release reservation for currently active block groups. */2621 /* Release reservation for currently active block groups. */
2644 spin_lock(&fs_info->zone_active_bgs_lock);2622 spin_lock(&fs_info->zone_active_bgs_lock);
2645 list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {2623 list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) {
2646 struct map_lookup *map = block_group->physical_map;2624 struct btrfs_chunk_map *map = block_group->physical_map;
2647 2625
2648 if (!(block_group->flags &2626 if (!(block_group->flags &
2649 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))2627 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)))
fs/btrfs/zoned.h+4−8
@@ -45,7 +45,7 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache); · 44 unmodified lines
45void btrfs_destroy_dev_zone_info(struct btrfs_device *device);45void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
46struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev);46struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev);
47int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);47int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
48int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);48int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info, unsigned long *mount_opt);
49int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,49int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
50 u64 *bytenr_ret);50 u64 *bytenr_ret);
51int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,51int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
@@ -59,8 +59,6 @@ int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical, · 7 unmodified lines
59int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);59int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
60int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);60int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
61void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);61void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
62void btrfs_redirty_list_add(struct btrfs_transaction *trans,
63 struct extent_buffer *eb);
64bool btrfs_use_zone_append(struct btrfs_bio *bbio);62bool btrfs_use_zone_append(struct btrfs_bio *bbio);
65void btrfs_record_physical_zoned(struct btrfs_bio *bbio);63void btrfs_record_physical_zoned(struct btrfs_bio *bbio);
66int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,64int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
@@ -123,7 +121,8 @@ static inline int btrfs_check_zoned_mode(const struct btrfs_fs_info *fs_info) · 56 unmodified lines
123 return -EOPNOTSUPP;121 return -EOPNOTSUPP;
124}122}
125 123
126static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)124static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info,
125 unsigned long *mount_opt)
127{126{
128 return 0;127 return 0;
129}128}
@@ -180,9 +179,6 @@ static inline int btrfs_load_block_group_zone_info( · 50 unmodified lines
180 179
181static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }180static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
182 181
183static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
184 struct extent_buffer *eb) { }
185
186static inline bool btrfs_use_zone_append(struct btrfs_bio *bbio)182static inline bool btrfs_use_zone_append(struct btrfs_bio *bbio)
187{183{
188 return false;184 return false;
@@ -323,7 +319,7 @@ static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_i · 134 unmodified lines
323 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);319 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
324 }320 }
325 321
326 /* Do not allow Host Manged zoned device */322 /* Do not allow Host Managed zoned device. */
327 return bdev_zoned_model(bdev) != BLK_ZONED_HM;323 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
328}324}
329 325
fs/btrfs/zstd.c+3−4
@@ -410,9 +410,8 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · 409 unmodified lines
410 workspace->in_buf.pos = 0;410 workspace->in_buf.pos = 0;
411 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);411 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
412 412
413
414 /* Allocate and map in the output buffer */413 /* Allocate and map in the output buffer */
415 out_page = alloc_page(GFP_NOFS);414 out_page = btrfs_alloc_compr_page();
416 if (out_page == NULL) {415 if (out_page == NULL) {
417 ret = -ENOMEM;416 ret = -ENOMEM;
418 goto out;417 goto out;
@@ -457,7 +456,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · 38 unmodified lines
457 ret = -E2BIG;456 ret = -E2BIG;
458 goto out;457 goto out;
459 }458 }
460 out_page = alloc_page(GFP_NOFS);459 out_page = btrfs_alloc_compr_page();
461 if (out_page == NULL) {460 if (out_page == NULL) {
462 ret = -ENOMEM;461 ret = -ENOMEM;
463 goto out;462 goto out;
@@ -514,7 +513,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · 50 unmodified lines
514 ret = -E2BIG;513 ret = -E2BIG;
515 goto out;514 goto out;
516 }515 }
517 out_page = alloc_page(GFP_NOFS);516 out_page = btrfs_alloc_compr_page();
518 if (out_page == NULL) {517 if (out_page == NULL) {
519 ret = -ENOMEM;518 ret = -ENOMEM;
520 goto out;519 goto out;
fs/namespace.c+11−0
@@ -2878,7 +2878,12 @@ static int do_remount(struct path *path, int ms_flags, int sb_flags, · 2877 unmodified lines
2878 if (IS_ERR(fc))2878 if (IS_ERR(fc))
2879 return PTR_ERR(fc);2879 return PTR_ERR(fc);
2880 2880
2881 /*
2882 * Indicate to the filesystem that the remount request is coming
2883 * from the legacy mount system call.
2884 */
2881 fc->oldapi = true;2885 fc->oldapi = true;
2886
2882 err = parse_monolithic_mount_data(fc, data);2887 err = parse_monolithic_mount_data(fc, data);
2883 if (!err) {2888 if (!err) {
2884 down_write(&sb->s_umount);2889 down_write(&sb->s_umount);
@@ -3328,6 +3333,12 @@ static int do_new_mount(struct path *path, const char *fstype, int sb_flags, · 443 unmodified lines
3328 if (IS_ERR(fc))3333 if (IS_ERR(fc))
3329 return PTR_ERR(fc);3334 return PTR_ERR(fc);
3330 3335
3336 /*
3337 * Indicate to the filesystem that the mount request is coming
3338 * from the legacy mount system call.
3339 */
3340 fc->oldapi = true;
3341
3331 if (subtype)3342 if (subtype)
3332 err = vfs_parse_fs_string(fc, "subtype",3343 err = vfs_parse_fs_string(fc, "subtype",
3333 subtype, strlen(subtype));3344 subtype, strlen(subtype));
include/trace/events/btrfs.h+30−48
@@ -21,7 +21,7 @@ struct btrfs_delayed_data_ref; · 20 unmodified lines
21struct btrfs_delayed_ref_head;21struct btrfs_delayed_ref_head;
22struct btrfs_block_group;22struct btrfs_block_group;
23struct btrfs_free_cluster;23struct btrfs_free_cluster;
24struct map_lookup;24struct btrfs_chunk_map;
25struct extent_buffer;25struct extent_buffer;
26struct btrfs_work;26struct btrfs_work;
27struct btrfs_workqueue;27struct btrfs_workqueue;
@@ -265,20 +265,20 @@ DEFINE_EVENT(btrfs__inode, btrfs_inode_evict, · 237 unmodified lines
265 __print_symbolic_u64(type, \265 __print_symbolic_u64(type, \
266 { EXTENT_MAP_LAST_BYTE, "LAST_BYTE" }, \266 { EXTENT_MAP_LAST_BYTE, "LAST_BYTE" }, \
267 { EXTENT_MAP_HOLE, "HOLE" }, \267 { EXTENT_MAP_HOLE, "HOLE" }, \
268 { EXTENT_MAP_INLINE, "INLINE" }, \268 { EXTENT_MAP_INLINE, "INLINE" })
269 { EXTENT_MAP_DELALLOC, "DELALLOC" })
270 269
271#define show_map_type(type) \270#define show_map_type(type) \
272 type, (type >= EXTENT_MAP_LAST_BYTE) ? "-" : __show_map_type(type)271 type, (type >= EXTENT_MAP_LAST_BYTE) ? "-" : __show_map_type(type)
273 272
274#define show_map_flags(flag) \273#define show_map_flags(flag) \
275 __print_flags(flag, "|", \274 __print_flags(flag, "|", \
276 { (1 << EXTENT_FLAG_PINNED), "PINNED" },\275 { EXTENT_FLAG_PINNED, "PINNED" },\
277 { (1 << EXTENT_FLAG_COMPRESSED), "COMPRESSED" },\276 { EXTENT_FLAG_COMPRESS_ZLIB, "COMPRESS_ZLIB" },\
278 { (1 << EXTENT_FLAG_PREALLOC), "PREALLOC" },\277 { EXTENT_FLAG_COMPRESS_LZO, "COMPRESS_LZO" },\
279 { (1 << EXTENT_FLAG_LOGGING), "LOGGING" },\278 { EXTENT_FLAG_COMPRESS_ZSTD, "COMPRESS_ZSTD" },\
280 { (1 << EXTENT_FLAG_FILLING), "FILLING" },\279 { EXTENT_FLAG_PREALLOC, "PREALLOC" },\
281 { (1 << EXTENT_FLAG_FS_MAPPING), "FS_MAPPING" })280 { EXTENT_FLAG_LOGGING, "LOGGING" },\
281 { EXTENT_FLAG_FILLING, "FILLING" })
282 282
283TRACE_EVENT_CONDITION(btrfs_get_extent,283TRACE_EVENT_CONDITION(btrfs_get_extent,
284 284
@@ -297,9 +297,8 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, · 12 unmodified lines
297 __field( u64, orig_start )297 __field( u64, orig_start )
298 __field( u64, block_start )298 __field( u64, block_start )
299 __field( u64, block_len )299 __field( u64, block_len )
300 __field( unsigned long, flags )300 __field( u32, flags )
301 __field( int, refs )301 __field( int, refs )
302 __field( unsigned int, compress_type )
303 ),302 ),
304 303
305 TP_fast_assign_btrfs(root->fs_info,304 TP_fast_assign_btrfs(root->fs_info,
@@ -312,13 +311,11 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, · 6 unmodified lines
312 __entry->block_len = map->block_len;311 __entry->block_len = map->block_len;
313 __entry->flags = map->flags;312 __entry->flags = map->flags;
314 __entry->refs = refcount_read(&map->refs);313 __entry->refs = refcount_read(&map->refs);
315 __entry->compress_type = map->compress_type;
316 ),314 ),
317 315
318 TP_printk_btrfs("root=%llu(%s) ino=%llu start=%llu len=%llu "316 TP_printk_btrfs("root=%llu(%s) ino=%llu start=%llu len=%llu "
319 "orig_start=%llu block_start=%llu(%s) "317 "orig_start=%llu block_start=%llu(%s) "
320 "block_len=%llu flags=%s refs=%u "318 "block_len=%llu flags=%s refs=%u",
321 "compress_type=%u",
322 show_root_type(__entry->root_objectid),319 show_root_type(__entry->root_objectid),
323 __entry->ino,320 __entry->ino,
324 __entry->start,321 __entry->start,
@@ -327,7 +324,7 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, · 2 unmodified lines
327 show_map_type(__entry->block_start),324 show_map_type(__entry->block_start),
328 __entry->block_len,325 __entry->block_len,
329 show_map_flags(__entry->flags),326 show_map_flags(__entry->flags),
330 __entry->refs, __entry->compress_type)327 __entry->refs)
331);328);
332 329
333TRACE_EVENT(btrfs_handle_em_exist,330TRACE_EVENT(btrfs_handle_em_exist,
@@ -1061,7 +1058,7 @@ DEFINE_EVENT(btrfs_delayed_ref_head, run_delayed_ref_head, · 727 unmodified lines
1061DECLARE_EVENT_CLASS(btrfs__chunk,1058DECLARE_EVENT_CLASS(btrfs__chunk,
1062 1059
1063 TP_PROTO(const struct btrfs_fs_info *fs_info,1060 TP_PROTO(const struct btrfs_fs_info *fs_info,
1064 const struct map_lookup *map, u64 offset, u64 size),1061 const struct btrfs_chunk_map *map, u64 offset, u64 size),
1065 1062
1066 TP_ARGS(fs_info, map, offset, size),1063 TP_ARGS(fs_info, map, offset, size),
1067 1064
@@ -1095,7 +1092,7 @@ DECLARE_EVENT_CLASS(btrfs__chunk, · 27 unmodified lines
1095DEFINE_EVENT(btrfs__chunk, btrfs_chunk_alloc,1092DEFINE_EVENT(btrfs__chunk, btrfs_chunk_alloc,
1096 1093
1097 TP_PROTO(const struct btrfs_fs_info *fs_info,1094 TP_PROTO(const struct btrfs_fs_info *fs_info,
1098 const struct map_lookup *map, u64 offset, u64 size),1095 const struct btrfs_chunk_map *map, u64 offset, u64 size),
1099 1096
1100 TP_ARGS(fs_info, map, offset, size)1097 TP_ARGS(fs_info, map, offset, size)
1101);1098);
@@ -1103,7 +1100,7 @@ DEFINE_EVENT(btrfs__chunk, btrfs_chunk_alloc, · 1 unmodified line
1103DEFINE_EVENT(btrfs__chunk, btrfs_chunk_free,1100DEFINE_EVENT(btrfs__chunk, btrfs_chunk_free,
1104 1101
1105 TP_PROTO(const struct btrfs_fs_info *fs_info,1102 TP_PROTO(const struct btrfs_fs_info *fs_info,
1106 const struct map_lookup *map, u64 offset, u64 size),1103 const struct btrfs_chunk_map *map, u64 offset, u64 size),
1107 1104
1108 TP_ARGS(fs_info, map, offset, size)1105 TP_ARGS(fs_info, map, offset, size)
1109);1106);
@@ -2099,17 +2096,12 @@ TRACE_EVENT(btrfs_set_extent_bit, · 989 unmodified lines
2099 __field( unsigned, set_bits)2096 __field( unsigned, set_bits)
2100 ),2097 ),
2101 2098
2102 TP_fast_assign_btrfs(tree->fs_info,2099 TP_fast_assign_btrfs(extent_io_tree_to_fs_info(tree),
2103 __entry->owner = tree->owner;2100 const struct btrfs_inode *inode = extent_io_tree_to_inode_const(tree);
2104 if (tree->inode) {
2105 const struct btrfs_inode *inode = tree->inode;
2106 2101
2107 __entry->ino = btrfs_ino(inode);2102 __entry->owner = tree->owner;
2108 __entry->rootid = inode->root->root_key.objectid;2103 __entry->ino = inode ? btrfs_ino(inode) : 0;
2109 } else {2104 __entry->rootid = inode ? inode->root->root_key.objectid : 0;
2110 __entry->ino = 0;
2111 __entry->rootid = 0;
2112 }
2113 __entry->start = start;2105 __entry->start = start;
2114 __entry->len = len;2106 __entry->len = len;
2115 __entry->set_bits = set_bits;2107 __entry->set_bits = set_bits;
@@ -2137,17 +2129,12 @@ TRACE_EVENT(btrfs_clear_extent_bit, · 21 unmodified lines
2137 __field( unsigned, clear_bits)2129 __field( unsigned, clear_bits)
2138 ),2130 ),
2139 2131
2140 TP_fast_assign_btrfs(tree->fs_info,2132 TP_fast_assign_btrfs(extent_io_tree_to_fs_info(tree),
2141 __entry->owner = tree->owner;2133 const struct btrfs_inode *inode = extent_io_tree_to_inode_const(tree);
2142 if (tree->inode) {
2143 const struct btrfs_inode *inode = tree->inode;
2144 2134
2145 __entry->ino = btrfs_ino(inode);2135 __entry->owner = tree->owner;
2146 __entry->rootid = inode->root->root_key.objectid;2136 __entry->ino = inode ? btrfs_ino(inode) : 0;
2147 } else {2137 __entry->rootid = inode ? inode->root->root_key.objectid : 0;
2148 __entry->ino = 0;
2149 __entry->rootid = 0;
2150 }
2151 __entry->start = start;2138 __entry->start = start;
2152 __entry->len = len;2139 __entry->len = len;
2153 __entry->clear_bits = clear_bits;2140 __entry->clear_bits = clear_bits;
@@ -2176,17 +2163,12 @@ TRACE_EVENT(btrfs_convert_extent_bit, · 22 unmodified lines
2176 __field( unsigned, clear_bits)2163 __field( unsigned, clear_bits)
2177 ),2164 ),
2178 2165
2179 TP_fast_assign_btrfs(tree->fs_info,2166 TP_fast_assign_btrfs(extent_io_tree_to_fs_info(tree),
2180 __entry->owner = tree->owner;2167 const struct btrfs_inode *inode = extent_io_tree_to_inode_const(tree);
2181 if (tree->inode) {
2182 const struct btrfs_inode *inode = tree->inode;
2183 2168
2184 __entry->ino = btrfs_ino(inode);2169 __entry->owner = tree->owner;
2185 __entry->rootid = inode->root->root_key.objectid;2170 __entry->ino = inode ? btrfs_ino(inode) : 0;
2186 } else {2171 __entry->rootid = inode ? inode->root->root_key.objectid : 0;
2187 __entry->ino = 0;
2188 __entry->rootid = 0;
2189 }
2190 __entry->start = start;2172 __entry->start = start;
2191 __entry->len = len;2173 __entry->len = len;
2192 __entry->set_bits = set_bits;2174 __entry->set_bits = set_bits;