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
2727 void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)
2828 {
2929 token->eb = eb;
30− token->kaddr = page_address(eb->pages[0]);
30+ token->kaddr = folio_address(eb->folios[0]);
3131 token->offset = 0;
3232 }
3333
@@ -50,7 +50,7 @@ void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *e · 16 unmodified lines
5050 * an offset into the extent buffer page array, cast to a specific type. This
5151 * gives us all the type checking.
5252 *
53− * The extent buffer pages stored in the array pages do not form a contiguous
53+ * The extent buffer pages stored in the array folios may not form a contiguous
5454 * phyusical range, but the API functions assume the linear offset to the range
5555 * from 0 to metadata node size.
5656 */
@@ -60,28 +60,30 @@ u##bits btrfs_get_token_##bits(struct btrfs_map_token *token, \ · 3 unmodified lines
6060 const void *ptr, unsigned long off) \
6161 { \
6262 const unsigned long member_offset = (unsigned long)ptr + off; \
63− const unsigned long idx = get_eb_page_index(member_offset); \
64− const unsigned long oip = get_eb_offset_in_page(token->eb, \
65− member_offset); \
63+ const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
64+ const unsigned long oil = get_eb_offset_in_folio(token->eb, \
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]); \
6668 const int size = sizeof(u##bits); \
6769 u8 lebytes[sizeof(u##bits)]; \
68− const int part = PAGE_SIZE - oip; \
70+ const int part = unit_size - oil; \
6971 \
7072 ASSERT(token); \
7173 ASSERT(token->kaddr); \
7274 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
7375 if (token->offset <= member_offset && \
74− member_offset + size <= token->offset + PAGE_SIZE) { \
75− return get_unaligned_le##bits(token->kaddr + oip); \
76+ member_offset + size <= token->offset + unit_size) { \
77+ return get_unaligned_le##bits(token->kaddr + oil); \
7678 } \
77− token->kaddr = page_address(token->eb->pages[idx]); \
78− token->offset = idx << PAGE_SHIFT; \
79− if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE ) \
80− return get_unaligned_le##bits(token->kaddr + oip); \
79+ token->kaddr = folio_address(token->eb->folios[idx]); \
80+ token->offset = idx << unit_shift; \
81+ if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \
82+ return get_unaligned_le##bits(token->kaddr + oil); \
8183 \
82− memcpy(lebytes, token->kaddr + oip, part); \
83− token->kaddr = page_address(token->eb->pages[idx + 1]); \
84− token->offset = (idx + 1) << PAGE_SHIFT; \
84+ memcpy(lebytes, token->kaddr + oil, part); \
85+ token->kaddr = folio_address(token->eb->folios[idx + 1]); \
86+ token->offset = (idx + 1) << unit_shift; \
8587 memcpy(lebytes + part, token->kaddr, size - part); \
8688 return get_unaligned_le##bits(lebytes); \
8789 } \
@@ -89,19 +91,21 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \ · 1 unmodified line
8991 const void *ptr, unsigned long off) \
9092 { \
9193 const unsigned long member_offset = (unsigned long)ptr + off; \
92− const unsigned long oip = get_eb_offset_in_page(eb, member_offset); \
93− const unsigned long idx = get_eb_page_index(member_offset); \
94− char *kaddr = page_address(eb->pages[idx]); \
94+ const unsigned long idx = get_eb_folio_index(eb, member_offset);\
95+ const unsigned long oil = get_eb_offset_in_folio(eb, \
96+ member_offset);\
97+ const int unit_size = folio_size(eb->folios[0]); \
98+ char *kaddr = folio_address(eb->folios[idx]); \
9599 const int size = sizeof(u##bits); \
96− const int part = PAGE_SIZE - oip; \
100+ const int part = unit_size - oil; \
97101 u8 lebytes[sizeof(u##bits)]; \
98102 \
99103 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
100− if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) \
101− return get_unaligned_le##bits(kaddr + oip); \
104+ if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \
105+ return get_unaligned_le##bits(kaddr + oil); \
102106 \
103− memcpy(lebytes, kaddr + oip, part); \
104− kaddr = page_address(eb->pages[idx + 1]); \
107+ memcpy(lebytes, kaddr + oil, part); \
108+ kaddr = folio_address(eb->folios[idx + 1]); \
105109 memcpy(lebytes + part, kaddr, size - part); \
106110 return get_unaligned_le##bits(lebytes); \
107111 } \
@@ -110,53 +114,59 @@ void btrfs_set_token_##bits(struct btrfs_map_token *token, \ · 2 unmodified lines
110114 u##bits val) \
111115 { \
112116 const unsigned long member_offset = (unsigned long)ptr + off; \
113− const unsigned long idx = get_eb_page_index(member_offset); \
114− const unsigned long oip = get_eb_offset_in_page(token->eb, \
115− member_offset); \
117+ const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
118+ const unsigned long oil = get_eb_offset_in_folio(token->eb, \
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]); \
116122 const int size = sizeof(u##bits); \
117123 u8 lebytes[sizeof(u##bits)]; \
118− const int part = PAGE_SIZE - oip; \
124+ const int part = unit_size - oil; \
119125 \
120126 ASSERT(token); \
121127 ASSERT(token->kaddr); \
122128 ASSERT(check_setget_bounds(token->eb, ptr, off, size)); \
123129 if (token->offset <= member_offset && \
124− member_offset + size <= token->offset + PAGE_SIZE) { \
125− put_unaligned_le##bits(val, token->kaddr + oip); \
130+ member_offset + size <= token->offset + unit_size) { \
131+ put_unaligned_le##bits(val, token->kaddr + oil); \
126132 return; \
127133 } \
128− token->kaddr = page_address(token->eb->pages[idx]); \
129− token->offset = idx << PAGE_SHIFT; \
130− if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) { \
131− put_unaligned_le##bits(val, token->kaddr + oip); \
134+ token->kaddr = folio_address(token->eb->folios[idx]); \
135+ token->offset = idx << unit_shift; \
136+ if (INLINE_EXTENT_BUFFER_PAGES == 1 || \
137+ oil + size <= unit_size) { \
138+ put_unaligned_le##bits(val, token->kaddr + oil); \
132139 return; \
133140 } \
134141 put_unaligned_le##bits(val, lebytes); \
135− memcpy(token->kaddr + oip, lebytes, part); \
136− token->kaddr = page_address(token->eb->pages[idx + 1]); \
137− token->offset = (idx + 1) << PAGE_SHIFT; \
142+ memcpy(token->kaddr + oil, lebytes, part); \
143+ token->kaddr = folio_address(token->eb->folios[idx + 1]); \
144+ token->offset = (idx + 1) << unit_shift; \
138145 memcpy(token->kaddr, lebytes + part, size - part); \
139146 } \
140147 void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
141148 unsigned long off, u##bits val) \
142149 { \
143150 const unsigned long member_offset = (unsigned long)ptr + off; \
144− const unsigned long oip = get_eb_offset_in_page(eb, member_offset); \
145− const unsigned long idx = get_eb_page_index(member_offset); \
146− char *kaddr = page_address(eb->pages[idx]); \
151+ const unsigned long idx = get_eb_folio_index(eb, member_offset);\
152+ const unsigned long oil = get_eb_offset_in_folio(eb, \
153+ member_offset);\
154+ const int unit_size = folio_size(eb->folios[0]); \
155+ char *kaddr = folio_address(eb->folios[idx]); \
147156 const int size = sizeof(u##bits); \
148− const int part = PAGE_SIZE - oip; \
157+ const int part = unit_size - oil; \
149158 u8 lebytes[sizeof(u##bits)]; \
150159 \
151160 ASSERT(check_setget_bounds(eb, ptr, off, size)); \
152− if (INLINE_EXTENT_BUFFER_PAGES == 1 || oip + size <= PAGE_SIZE) { \
153− put_unaligned_le##bits(val, kaddr + oip); \
161+ if (INLINE_EXTENT_BUFFER_PAGES == 1 || \
162+ oil + size <= unit_size) { \
163+ put_unaligned_le##bits(val, kaddr + oil); \
154164 return; \
155165 } \
156166 \
157167 put_unaligned_le##bits(val, lebytes); \
158− memcpy(kaddr + oip, lebytes, part); \
159− kaddr = page_address(eb->pages[idx + 1]); \
168+ memcpy(kaddr + oil, lebytes, part); \
169+ kaddr = folio_address(eb->folios[idx + 1]); \
160170 memcpy(kaddr, lebytes + part, size - part); \
161171 }
162172
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
9090 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
9191 static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
9292 { \
93− const type *p = page_address(eb->pages[0]) + \
93+ const type *p = folio_address(eb->folios[0]) + \
9494 offset_in_page(eb->start); \
9595 return get_unaligned_le##bits(&p->member); \
9696 } \
9797 static inline void btrfs_set_##name(const struct extent_buffer *eb, \
9898 u##bits val) \
9999 { \
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); \
101101 put_unaligned_le##bits(val, &p->member); \
102102 }
103103
fs/btrfs/bio.c+12−5
@@ -194,6 +194,12 @@ static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio, · 193 unmodified lines
194194 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);
195195 int mirror = repair_bbio->mirror_num;
196196
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+
197203 if (repair_bbio->bio.bi_status ||
198204 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {
199205 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
215221 btrfs_repair_io_failure(fs_info, btrfs_ino(inode),
216222 repair_bbio->file_offset, fs_info->sectorsize,
217223 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);
219225 } while (mirror != fbio->bbio->mirror_num);
220226
221227 done:
@@ -626,7 +632,7 @@ static bool should_async_write(struct btrfs_bio *bbio) · 404 unmodified lines
626632 /*
627633 * Submit bio to an async queue.
628634 *
629− * Return true if the work has been succesfuly submitted, else false.
635+ * Return true if the work has been successfully submitted, else false.
630636 */
631637 static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio,
632638 struct btrfs_io_context *bioc,
@@ -767,8 +773,8 @@ void btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num) · 134 unmodified lines
767773 * freeing the bio.
768774 */
769775 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
770− u64 length, u64 logical, struct page *page,
771− unsigned int pg_offset, int mirror_num)
776+ u64 length, u64 logical, struct folio *folio,
777+ unsigned int folio_offset, int mirror_num)
772778 {
773779 struct btrfs_io_stripe smap = { 0 };
774780 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
799805
800806 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
801807 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);
803810 ret = submit_bio_wait(&bio);
804811 if (ret) {
805812 /* 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
105105 void btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num);
106106 void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace);
107107 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
108− u64 length, u64 logical, struct page *page,
109− unsigned int pg_offset, int mirror_num);
108+ u64 length, u64 logical, struct folio *folio,
109+ unsigned int folio_offset, int mirror_num);
110110
111111 #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
168168 cache);
169169
170170 kfree(cache->free_space_ctl);
171− kfree(cache->physical_map);
171+ btrfs_free_chunk_map(cache->physical_map);
172172 kfree(cache);
173173 }
174174 }
@@ -1047,7 +1047,7 @@ static int remove_block_group_item(struct btrfs_trans_handle *trans, · 872 unmodified lines
10471047 }
10481048
10491049 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1050− u64 group_start, struct extent_map *em)
1050+ struct btrfs_chunk_map *map)
10511051 {
10521052 struct btrfs_fs_info *fs_info = trans->fs_info;
10531053 struct btrfs_path *path;
@@ -1059,10 +1059,10 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 5 unmodified lines
10591059 int index;
10601060 int factor;
10611061 struct btrfs_caching_control *caching_ctl = NULL;
1062− bool remove_em;
1062+ bool remove_map;
10631063 bool remove_rsv = false;
10641064
1065− block_group = btrfs_lookup_block_group(fs_info, group_start);
1065+ block_group = btrfs_lookup_block_group(fs_info, map->start);
10661066 BUG_ON(!block_group);
10671067 BUG_ON(!block_group->ro);
10681068
@@ -1252,7 +1252,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 183 unmodified lines
12521252 * entries because we already removed them all when we called
12531253 * btrfs_remove_free_space_cache().
12541254 *
1255− * And we must not remove the extent map from the fs_info->mapping_tree
1255+ * And we must not remove the chunk map from the fs_info->mapping_tree
12561256 * to prevent the same logical address range and physical device space
12571257 * ranges from being reused for a new block group. This is needed to
12581258 * avoid races with trimming and scrub.
@@ -1268,19 +1268,11 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · 9 unmodified lines
12681268 * in place until the extents have been discarded completely when
12691269 * the transaction commit has completed.
12701270 */
1271− remove_em = (atomic_read(&block_group->frozen) == 0);
1271+ remove_map = (atomic_read(&block_group->frozen) == 0);
12721272 spin_unlock(&block_group->lock);
12731273
1274− if (remove_em) {
1275− struct extent_map_tree *em_tree;
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− }
1274+ if (remove_map)
1275+ btrfs_remove_chunk_map(fs_info, map);
12841276
12851277 out:
12861278 /* Once for the lookup reference */
@@ -1295,15 +1287,12 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( · 8 unmodified lines
12951287 struct btrfs_fs_info *fs_info, const u64 chunk_offset)
12961288 {
12971289 struct btrfs_root *root = btrfs_block_group_root(fs_info);
1298− struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1299− struct extent_map *em;
1300− struct map_lookup *map;
1290+ struct btrfs_chunk_map *map;
13011291 unsigned int num_items;
13021292
1303− read_lock(&em_tree->lock);
1304− em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1305− read_unlock(&em_tree->lock);
1306− ASSERT(em && em->start == chunk_offset);
1293+ map = btrfs_find_chunk_map(fs_info, chunk_offset, 1);
1294+ ASSERT(map != NULL);
1295+ ASSERT(map->start == chunk_offset);
13071296
13081297 /*
13091298 * 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
13241313 * more device items and remove one chunk item), but this is done at
13251314 * btrfs_remove_chunk() through a call to check_system_chunk().
13261315 */
1327− map = em->map_lookup;
13281316 num_items = 3 + map->num_stripes;
1329− free_extent_map(em);
1317+ btrfs_free_chunk_map(map);
13301318
13311319 return btrfs_start_transaction_fallback_global_rsv(root, num_items);
13321320 }
@@ -1927,8 +1915,7 @@ void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg) · 594 unmodified lines
19271915 static int read_bg_from_eb(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
19281916 struct btrfs_path *path)
19291917 {
1930− struct extent_map_tree *em_tree;
1931− struct extent_map *em;
1918+ struct btrfs_chunk_map *map;
19321919 struct btrfs_block_group_item bg;
19331920 struct extent_buffer *leaf;
19341921 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
19381925 slot = path->slots[0];
19391926 leaf = path->nodes[0];
19401927
1941− em_tree = &fs_info->mapping_tree;
1942− read_lock(&em_tree->lock);
1943− em = lookup_extent_mapping(em_tree, key->objectid, key->offset);
1944− read_unlock(&em_tree->lock);
1945− if (!em) {
1928+ map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset);
1929+ if (!map) {
19461930 btrfs_err(fs_info,
19471931 "logical %llu len %llu found bg but no related chunk",
19481932 key->objectid, key->offset);
19491933 return -ENOENT;
19501934 }
19511935
1952− if (em->start != key->objectid || em->len != key->offset) {
1936+ if (map->start != key->objectid || map->chunk_len != key->offset) {
19531937 btrfs_err(fs_info,
19541938 "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);
19561940 ret = -EUCLEAN;
1957− goto out_free_em;
1941+ goto out_free_map;
19581942 }
19591943
19601944 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
19621946 flags = btrfs_stack_block_group_flags(&bg) &
19631947 BTRFS_BLOCK_GROUP_TYPE_MASK;
19641948
1965− if (flags != (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
1949+ if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
19661950 btrfs_err(fs_info,
19671951 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
19681952 key->objectid, key->offset, flags,
1969− (BTRFS_BLOCK_GROUP_TYPE_MASK & em->map_lookup->type));
1953+ (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type));
19701954 ret = -EUCLEAN;
19711955 }
19721956
1973−out_free_em:
1974− free_extent_map(em);
1957+out_free_map:
1958+ btrfs_free_chunk_map(map);
19751959 return ret;
19761960 }
19771961
@@ -2024,8 +2008,7 @@ static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) · 46 unmodified lines
20242008 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
20252009 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
20262010 {
2027− struct extent_map *em;
2028− struct map_lookup *map;
2011+ struct btrfs_chunk_map *map;
20292012 u64 *buf;
20302013 u64 bytenr;
20312014 u64 data_stripe_length;
@@ -2033,14 +2016,13 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, · 1 unmodified line
20332016 int i, nr = 0;
20342017 int ret = 0;
20352018
2036− em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
2037− if (IS_ERR(em))
2019+ map = btrfs_get_chunk_map(fs_info, chunk_start, 1);
2020+ if (IS_ERR(map))
20382021 return -EIO;
20392022
2040− map = em->map_lookup;
2041− data_stripe_length = em->orig_block_len;
2023+ data_stripe_length = map->stripe_size;
20422024 io_stripe_size = BTRFS_STRIPE_LEN;
2043− chunk_start = em->start;
2025+ chunk_start = map->start;
20442026
20452027 /* For RAID5/6 adjust to a full IO stripe length */
20462028 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
20942076 *naddrs = nr;
20952077 *stripe_len = io_stripe_size;
20962078 out:
2097− free_extent_map(em);
2079+ btrfs_free_chunk_map(map);
20982080 return ret;
20992081 }
21002082
@@ -2199,49 +2181,47 @@ static struct btrfs_block_group *btrfs_create_block_group_cache( · 98 unmodified lines
21992181 */
22002182 static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
22012183 {
2202− struct extent_map_tree *map_tree = &fs_info->mapping_tree;
2203− struct extent_map *em;
2204− struct btrfs_block_group *bg;
22052184 u64 start = 0;
22062185 int ret = 0;
22072186
22082187 while (1) {
2209− read_lock(&map_tree->lock);
2188+ struct btrfs_chunk_map *map;
2189+ struct btrfs_block_group *bg;
2190+
22102191 /*
2211− * lookup_extent_mapping will return the first extent map
2212− * intersecting the range, so setting @len to 1 is enough to
2192+ * btrfs_find_chunk_map() will return the first chunk map
2193+ * intersecting the range, so setting @length to 1 is enough to
22132194 * get the first chunk.
22142195 */
2215− em = lookup_extent_mapping(map_tree, start, 1);
2216− read_unlock(&map_tree->lock);
2217− if (!em)
2196+ map = btrfs_find_chunk_map(fs_info, start, 1);
2197+ if (!map)
22182198 break;
22192199
2220− bg = btrfs_lookup_block_group(fs_info, em->start);
2200+ bg = btrfs_lookup_block_group(fs_info, map->start);
22212201 if (!bg) {
22222202 btrfs_err(fs_info,
22232203 "chunk start=%llu len=%llu doesn't have corresponding block group",
2224− em->start, em->len);
2204+ map->start, map->chunk_len);
22252205 ret = -EUCLEAN;
2226− free_extent_map(em);
2206+ btrfs_free_chunk_map(map);
22272207 break;
22282208 }
2229− if (bg->start != em->start || bg->length != em->len ||
2209+ if (bg->start != map->start || bg->length != map->chunk_len ||
22302210 (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)) {
22322212 btrfs_err(fs_info,
22332213 "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,
2235− em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
2214+ map->start, map->chunk_len,
2215+ map->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
22362216 bg->start, bg->length,
22372217 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
22382218 ret = -EUCLEAN;
2239− free_extent_map(em);
2219+ btrfs_free_chunk_map(map);
22402220 btrfs_put_block_group(bg);
22412221 break;
22422222 }
2243− start = em->start + em->len;
2244− free_extent_map(em);
2223+ start = map->start + map->chunk_len;
2224+ btrfs_free_chunk_map(map);
22452225 btrfs_put_block_group(bg);
22462226 }
22472227 return ret;
@@ -2369,28 +2349,25 @@ error: · 121 unmodified lines
23692349
23702350 static int fill_dummy_bgs(struct btrfs_fs_info *fs_info)
23712351 {
2372− struct extent_map_tree *em_tree = &fs_info->mapping_tree;
23732352 struct rb_node *node;
23742353 int ret = 0;
23752354
2376− for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
2377− struct extent_map *em;
2378− struct map_lookup *map;
2355+ for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) {
2356+ struct btrfs_chunk_map *map;
23792357 struct btrfs_block_group *bg;
23802358
2381− em = rb_entry(node, struct extent_map, rb_node);
2382− map = em->map_lookup;
2383− bg = btrfs_create_block_group_cache(fs_info, em->start);
2359+ map = rb_entry(node, struct btrfs_chunk_map, rb_node);
2360+ bg = btrfs_create_block_group_cache(fs_info, map->start);
23842361 if (!bg) {
23852362 ret = -ENOMEM;
23862363 break;
23872364 }
23882365
23892366 /* Fill dummy cache as FULL */
2390− bg->length = em->len;
2367+ bg->length = map->chunk_len;
23912368 bg->flags = map->type;
23922369 bg->cached = BTRFS_CACHE_FINISHED;
2393− bg->used = em->len;
2370+ bg->used = map->chunk_len;
23942371 bg->flags = map->type;
23952372 ret = btrfs_add_block_group_cache(fs_info, bg);
23962373 /*
@@ -2618,19 +2595,14 @@ static int insert_dev_extents(struct btrfs_trans_handle *trans, · 221 unmodified lines
26182595 {
26192596 struct btrfs_fs_info *fs_info = trans->fs_info;
26202597 struct btrfs_device *device;
2621− struct extent_map *em;
2622− struct map_lookup *map;
2598+ struct btrfs_chunk_map *map;
26232599 u64 dev_offset;
2624− u64 stripe_size;
26252600 int i;
26262601 int ret = 0;
26272602
2628− em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
2629− if (IS_ERR(em))
2630− return PTR_ERR(em);
2631−
2632− map = em->map_lookup;
2633− stripe_size = em->orig_block_len;
2603+ map = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
2604+ if (IS_ERR(map))
2605+ return PTR_ERR(map);
26342606
26352607 /*
26362608 * 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
26472619 dev_offset = map->stripes[i].physical;
26482620
26492621 ret = insert_dev_extent(trans, device, chunk_offset, dev_offset,
2650− stripe_size);
2622+ map->stripe_size);
26512623 if (ret)
26522624 break;
26532625 }
26542626 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
26552627
2656− free_extent_map(em);
2628+ btrfs_free_chunk_map(map);
26572629 return ret;
26582630 }
26592631
@@ -2910,7 +2882,7 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, · 250 unmodified lines
29102882 goto unlock_out;
29112883
29122884 /*
2913− * Skip chunk alloction if the bg is SYSTEM, this is to avoid system
2885+ * Skip chunk allocation if the bg is SYSTEM, this is to avoid system
29142886 * chunk allocation storm to exhaust the system chunk array. Otherwise
29152887 * we still want to try our best to mark the block group read-only.
29162888 */
@@ -4406,8 +4378,6 @@ void btrfs_freeze_block_group(struct btrfs_block_group *cache) · 1489 unmodified lines
44064378 void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
44074379 {
44084380 struct btrfs_fs_info *fs_info = block_group->fs_info;
4409− struct extent_map_tree *em_tree;
4410− struct extent_map *em;
44114381 bool cleanup;
44124382
44134383 spin_lock(&block_group->lock);
@@ -4416,17 +4386,16 @@ void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group) · 2 unmodified lines
44164386 spin_unlock(&block_group->lock);
44174387
44184388 if (cleanup) {
4419− em_tree = &fs_info->mapping_tree;
4420− write_lock(&em_tree->lock);
4421− em = lookup_extent_mapping(em_tree, block_group->start,
4422− 1);
4423− BUG_ON(!em); /* logic error, can't happen */
4424− remove_extent_mapping(em_tree, em);
4425− write_unlock(&em_tree->lock);
4426−
4427− /* once for us and once for the tree */
4428− free_extent_map(em);
4429− free_extent_map(em);
4389+ struct btrfs_chunk_map *map;
4390+
4391+ map = btrfs_find_chunk_map(fs_info, block_group->start, 1);
4392+ /* Logic error, can't happen. */
4393+ ASSERT(map);
4394+
4395+ btrfs_remove_chunk_map(fs_info, map);
4396+
4397+ /* Once for our lookup reference. */
4398+ btrfs_free_chunk_map(map);
44304399
44314400 /*
44324401 * 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
55
66 #include "free-space-cache.h"
77
8+struct btrfs_chunk_map;
9+
810 enum btrfs_disk_cache_state {
911 BTRFS_DC_WRITTEN,
1012 BTRFS_DC_ERROR,
@@ -243,7 +245,7 @@ struct btrfs_block_group { · 232 unmodified lines
243245 u64 zone_unusable;
244246 u64 zone_capacity;
245247 u64 meta_write_pointer;
246− struct map_lookup *physical_map;
248+ struct btrfs_chunk_map *physical_map;
247249 struct list_head active_bg_list;
248250 struct work_struct zone_finish_work;
249251 struct extent_buffer *last_eb;
@@ -297,7 +299,7 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( · 47 unmodified lines
297299 struct btrfs_fs_info *fs_info,
298300 const u64 chunk_offset);
299301 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
300− u64 group_start, struct extent_map *em);
302+ struct btrfs_chunk_map *map);
301303 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
302304 void btrfs_mark_bg_unused(struct btrfs_block_group *bg);
303305 void btrfs_reclaim_bgs_work(struct work_struct *work);
fs/btrfs/btrfs_inode.h+7−3
@@ -69,6 +69,8 @@ enum { · 68 unmodified lines
6969 BTRFS_INODE_VERITY_IN_PROGRESS,
7070 /* Set when this inode is a free space inode. */
7171 BTRFS_INODE_FREE_SPACE_INODE,
72+ /* Set when there are no capabilities in XATTs for the inode. */
73+ BTRFS_INODE_NO_CAP_XATTR,
7274 };
7375
7476 /* in memory btrfs inode */
@@ -107,9 +109,11 @@ struct btrfs_inode { · 32 unmodified lines
107109
108110 /*
109111 * Keep track of where the inode has extent items mapped in order to
110− * make sure the i_size adjustments are accurate
112+ * 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.
111115 */
112− struct extent_io_tree file_extent_tree;
116+ struct extent_io_tree *file_extent_tree;
113117
114118 /* held while logging the inode in tree-log.c */
115119 struct mutex log_mutex;
@@ -487,7 +491,7 @@ struct inode *btrfs_iget_path(struct super_block *s, u64 ino, · 371 unmodified lines
487491 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
488492 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
489493 struct page *page, size_t pg_offset,
490− u64 start, u64 end);
494+ u64 start, u64 len);
491495 int btrfs_update_inode(struct btrfs_trans_handle *trans,
492496 struct btrfs_inode *inode);
493497 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
fs/btrfs/compression.c+128−11
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include <linux/slab.h>
2121 #include <linux/sched/mm.h>
2222 #include <linux/log2.h>
23+#include <linux/shrinker.h>
2324 #include <crypto/hash.h>
2425 #include "misc.h"
2526 #include "ctree.h"
@@ -163,13 +164,107 @@ static int compression_decompress(int type, struct list_head *ws, · 137 unmodified lines
163164 static void btrfs_free_compressed_pages(struct compressed_bio *cb)
164165 {
165166 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]);
167168 kfree(cb->compressed_pages);
168169 }
169170
170171 static int btrfs_decompress_bio(struct compressed_bio *cb);
171172
172−static void end_compressed_bio_read(struct btrfs_bio *bbio)
173+/*
174+ * Global cache of last unused pages for compression/decompression.
175+ */
176+static 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+
184+static 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+
198+static 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+ */
229+struct 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+
247+void 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+
267+static void end_bbio_comprssed_read(struct btrfs_bio *bbio)
173268 {
174269 struct compressed_bio *cb = to_compressed_bio(bbio);
175270 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
211306 for (i = 0; i < ret; i++) {
212307 struct folio *folio = fbatch.folios[i];
213308
214− btrfs_page_clamp_clear_writeback(fs_info, &folio->page,
215− cb->start, cb->len);
309+ btrfs_folio_clamp_clear_writeback(fs_info, folio,
310+ cb->start, cb->len);
216311 }
217312 folio_batch_release(&fbatch);
218313 }
@@ -242,7 +337,7 @@ static void btrfs_finish_compressed_write_work(struct work_struct *work) · 23 unmodified lines
242337 * This also calls the writeback end hooks for the file pages so that metadata
243338 * and checksums can be updated in the file.
244339 */
245−static void end_compressed_bio_write(struct btrfs_bio *bbio)
340+static void end_bbio_comprssed_write(struct btrfs_bio *bbio)
246341 {
247342 struct compressed_bio *cb = to_compressed_bio(bbio);
248343 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
289384
290385 cb = alloc_compressed_bio(inode, ordered->file_offset,
291386 REQ_OP_WRITE | write_flags,
292− end_compressed_bio_write);
387+ end_bbio_comprssed_write);
293388 cb->start = ordered->file_offset;
294389 cb->len = ordered->num_bytes;
295390 cb->compressed_pages = compressed_pages;
@@ -446,7 +541,8 @@ static noinline int add_ra_bio_pages(struct inode *inode, · 150 unmodified lines
446541 * subpage::readers and to unlock the page.
447542 */
448543 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);
450546 put_page(page);
451547 cur += add_size;
452548 }
@@ -489,11 +585,11 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) · 36 unmodified lines
489585 goto out;
490586 }
491587
492− ASSERT(em->compress_type != BTRFS_COMPRESS_NONE);
588+ ASSERT(extent_map_is_compressed(em));
493589 compressed_len = em->block_len;
494590
495591 cb = alloc_compressed_bio(inode, file_offset, REQ_OP_READ,
496− end_compressed_bio_read);
592+ end_bbio_comprssed_read);
497593
498594 cb->start = em->orig_start;
499595 em_len = em->len;
@@ -501,7 +597,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) · 1 unmodified line
501597
502598 cb->len = bbio->bio.bi_iter.bi_size;
503599 cb->compressed_len = compressed_len;
504− cb->compress_type = em->compress_type;
600+ cb->compress_type = extent_map_compression(em);
505601 cb->orig_bbio = bbio;
506602
507603 free_extent_map(em);
@@ -513,7 +609,7 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio) · 5 unmodified lines
513609 goto out_free_bio;
514610 }
515611
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);
517613 if (ret2) {
518614 ret = BLK_STS_RESOURCE;
519615 goto out_free_compressed_pages;
@@ -960,15 +1056,36 @@ int __init btrfs_init_compress(void) · 440 unmodified lines
9601056 offsetof(struct compressed_bio, bbio.bio),
9611057 BIOSET_NEED_BVECS))
9621058 return -ENOMEM;
1059+
1060+ compr_pool.shrinker = shrinker_alloc(SHRINKER_NONSLAB, "btrfs-compr-pages");
1061+ if (!compr_pool.shrinker)
1062+ return -ENOMEM;
1063+
9631064 btrfs_init_workspace_manager(BTRFS_COMPRESS_NONE);
9641065 btrfs_init_workspace_manager(BTRFS_COMPRESS_ZLIB);
9651066 btrfs_init_workspace_manager(BTRFS_COMPRESS_LZO);
9661067 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+
9671080 return 0;
9681081 }
9691082
9701083 void __cold btrfs_exit_compress(void)
9711084 {
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+
9721089 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_NONE);
9731090 btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_ZLIB);
9741091 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
3232
3333 #define BTRFS_ZLIB_DEFAULT_LEVEL 3
3434
35+struct page;
36+
3537 struct compressed_bio {
3638 /* Number of compressed pages in the array */
3739 unsigned int nr_pages;
@@ -96,6 +98,9 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio); · 58 unmodified lines
9698
9799 unsigned int btrfs_compress_str2level(unsigned int type, const char *str);
98100
101+struct page *btrfs_alloc_compr_page(void);
102+void btrfs_free_compr_page(struct page *page);
103+
99104 enum btrfs_compression_type {
100105 BTRFS_COMPRESS_NONE = 0,
101106 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
370370 /*
371371 * check if the tree block can be shared by multiple trees
372372 */
373−int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
374− struct btrfs_root *root,
375− struct extent_buffer *buf)
373+bool btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
374+ struct btrfs_root *root,
375+ struct extent_buffer *buf)
376376 {
377+ const u64 buf_gen = btrfs_header_generation(buf);
378+
377379 /*
378380 * Tree blocks not in shareable trees and tree roots are never shared.
379381 * If a block was allocated after the last snapshot and the block was
380382 * not allocated by tree relocation, we know the block is not shared.
381383 */
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− }
398384
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;
400408 }
401409
402410 static 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
812820 }
813821
814822 while (low < high) {
815− unsigned long oip;
823+ const int unit_size = folio_size(eb->folios[0]);
824+ unsigned long oil;
816825 unsigned long offset;
817826 struct btrfs_disk_key *tmp;
818827 struct btrfs_disk_key unaligned;
@@ -820,14 +829,14 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 1 unmodified line
820829
821830 mid = (low + high) / 2;
822831 offset = p + mid * item_size;
823− oip = offset_in_page(offset);
832+ oil = get_eb_offset_in_folio(eb, offset);
824833
825− if (oip + key_size <= PAGE_SIZE) {
826− const unsigned long idx = get_eb_page_index(offset);
827− char *kaddr = page_address(eb->pages[idx]);
834+ if (oil + key_size <= unit_size) {
835+ const unsigned long idx = get_eb_folio_index(eb, offset);
836+ char *kaddr = folio_address(eb->folios[idx]);
828837
829− oip = get_eb_offset_in_page(eb, offset);
830− tmp = (struct btrfs_disk_key *)(kaddr + oip);
838+ oil = get_eb_offset_in_folio(eb, offset);
839+ tmp = (struct btrfs_disk_key *)(kaddr + oil);
831840 } else {
832841 read_extent_buffer(eb, &unaligned, offset, key_size);
833842 tmp = &unaligned;
fs/btrfs/ctree.h+6−11
@@ -212,8 +212,6 @@ struct btrfs_root { · 211 unmodified lines
212212
213213 u64 last_trans;
214214
215− u32 type;
216−
217215 u64 free_objectid;
218216
219217 struct btrfs_key defrag_progress;
@@ -224,18 +222,15 @@ struct btrfs_root { · 4 unmodified lines
224222
225223 struct list_head root_list;
226224
227− spinlock_t log_extents_lock[2];
228− struct list_head logged_list[2];
229−
230225 spinlock_t inode_lock;
231226 /* red-black tree that keeps track of in-memory inodes */
232227 struct rb_root inode_tree;
233228
234229 /*
235− * radix tree that keeps track of delayed nodes of every inode,
236− * protected by inode_lock
230+ * Xarray that keeps track of delayed nodes of every inode, protected
231+ * by @inode_lock.
237232 */
238− struct radix_tree_root delayed_nodes_tree;
233+ struct xarray delayed_nodes;
239234 /*
240235 * right now this just gets used so that a root has its own devid
241236 * 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
561556 struct btrfs_root *root,
562557 struct extent_buffer *buf,
563558 struct extent_buffer **cow_ret, u64 new_root_objectid);
564−int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
565− struct btrfs_root *root,
566− struct extent_buffer *buf);
559+bool btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
560+ struct btrfs_root *root,
561+ struct extent_buffer *buf);
567562 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
568563 struct btrfs_path *path, int level, int slot);
569564 void 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
775775 * this em, as either we don't care about the generation, or the
776776 * merged extent map will be rejected anyway.
777777 */
778− if (em && test_bit(EXTENT_FLAG_MERGED, &em->flags) &&
778+ if (em && (em->flags & EXTENT_FLAG_MERGED) &&
779779 newer_than && em->generation >= newer_than) {
780780 free_extent_map(em);
781781 em = NULL;
@@ -802,7 +802,7 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, · 20 unmodified lines
802802 static u32 get_extent_max_capacity(const struct btrfs_fs_info *fs_info,
803803 const struct extent_map *em)
804804 {
805− if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
805+ if (extent_map_is_compressed(em))
806806 return BTRFS_MAX_COMPRESSED;
807807 return fs_info->max_extent_size;
808808 }
@@ -828,7 +828,7 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em, · 19 unmodified lines
828828 /* No more em or hole */
829829 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
830830 goto out;
831− if (test_bit(EXTENT_FLAG_PREALLOC, &next->flags))
831+ if (next->flags & EXTENT_FLAG_PREALLOC)
832832 goto out;
833833 /*
834834 * 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
996996 em->len <= inode->root->fs_info->max_inline)
997997 goto next;
998998
999− /* Skip hole/delalloc/preallocated extents */
999+ /* Skip holes and preallocated extents. */
10001000 if (em->block_start == EXTENT_MAP_HOLE ||
1001− em->block_start == EXTENT_MAP_DELALLOC ||
1002− test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1001+ (em->flags & EXTENT_FLAG_PREALLOC))
10031002 goto next;
10041003
10051004 /* Skip older extent */
@@ -1190,7 +1189,7 @@ static int defrag_one_locked_target(struct btrfs_inode *inode, · 184 unmodified lines
11901189 /* Update the page status */
11911190 for (i = start_index - first_index; i <= last_index - first_index; i++) {
11921191 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);
11941193 }
11951194 btrfs_delalloc_release_extents(inode, len);
11961195 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
7171 }
7272
7373 spin_lock(&root->inode_lock);
74− node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
74+ node = xa_load(&root->delayed_nodes, ino);
7575
7676 if (node) {
7777 if (btrfs_inode->delayed_node) {
@@ -83,9 +83,9 @@ static struct btrfs_delayed_node *btrfs_get_delayed_node( · 5 unmodified lines
8383
8484 /*
8585 * It's possible that we're racing into the middle of removing
86− * this node from the radix tree. In this case, the refcount
86+ * this node from the xarray. In this case, the refcount
8787 * was zero and it should never go back to one. Just return
88− * NULL like it was never in the radix at all; our release
88+ * NULL like it was never in the xarray at all; our release
8989 * function is in the process of removing it.
9090 *
9191 * 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
9393 * here, refcount_inc() may decide to just WARN_ONCE() instead
9494 * of actually bumping the refcount.
9595 *
96− * If this node is properly in the radix, we want to bump the
96+ * If this node is properly in the xarray, we want to bump the
9797 * refcount twice, once for the inode and once for this get
9898 * operation.
9999 */
@@ -120,6 +120,7 @@ static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( · 20 unmodified lines
120120 struct btrfs_root *root = btrfs_inode->root;
121121 u64 ino = btrfs_ino(btrfs_inode);
122122 int ret;
123+ void *ptr;
123124
124125 again:
125126 node = btrfs_get_delayed_node(btrfs_inode);
@@ -131,26 +132,30 @@ again: · 5 unmodified lines
131132 return ERR_PTR(-ENOMEM);
132133 btrfs_init_delayed_node(node, root, ino);
133134
134− /* cached in the btrfs inode and can be accessed */
135+ /* Cached in the inode and can be accessed. */
135136 refcount_set(&node->refs, 2);
136137
137− ret = radix_tree_preload(GFP_NOFS);
138− if (ret) {
138+ /* Allocate and reserve the slot, from now it can return a NULL from xa_load(). */
139+ ret = xa_reserve(&root->delayed_nodes, ino, GFP_NOFS);
140+ if (ret == -ENOMEM) {
139141 kmem_cache_free(delayed_node_cache, node);
140− return ERR_PTR(ret);
142+ return ERR_PTR(-ENOMEM);
141143 }
142−
143144 spin_lock(&root->inode_lock);
144− ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
145− if (ret == -EEXIST) {
145+ ptr = xa_load(&root->delayed_nodes, ino);
146+ if (ptr) {
147+ /* Somebody inserted it, go back and read it. */
146148 spin_unlock(&root->inode_lock);
147149 kmem_cache_free(delayed_node_cache, node);
148− radix_tree_preload_end();
150+ node = NULL;
149151 goto again;
150152 }
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);
151157 btrfs_inode->delayed_node = node;
152158 spin_unlock(&root->inode_lock);
153− radix_tree_preload_end();
154159
155160 return node;
156161 }
@@ -269,8 +274,7 @@ static void __btrfs_release_delayed_node( · 112 unmodified lines
269274 * back up. We can delete it now.
270275 */
271276 ASSERT(refcount_read(&delayed_node->refs) == 0);
272− radix_tree_delete(&root->delayed_nodes_tree,
273− delayed_node->inode_id);
277+ xa_erase(&root->delayed_nodes, delayed_node->inode_id);
274278 spin_unlock(&root->inode_lock);
275279 kmem_cache_free(delayed_node_cache, delayed_node);
276280 }
@@ -1036,14 +1040,33 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, · 759 unmodified lines
10361040 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
10371041 goto out;
10381042
1039− path->slots[0]++;
1040− if (path->slots[0] >= btrfs_header_nritems(leaf))
1041− goto search;
1042−again:
1043+ /*
1044+ * Now we're going to delete the INODE_REF/EXTREF, which should be the
1045+ * only one ref left. Check if the next item is an INODE_REF/EXTREF.
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+ }
10431067 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
10441068 if (key.objectid != node->inode_id)
10451069 goto out;
1046−
10471070 if (key.type != BTRFS_INODE_REF_KEY &&
10481071 key.type != BTRFS_INODE_EXTREF_KEY)
10491072 goto out;
@@ -1070,22 +1093,6 @@ err_out: · 20 unmodified lines
10701093 btrfs_abort_transaction(trans, ret);
10711094
10721095 return ret;
1073−
1074−search:
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;
10891096 }
10901097
10911098 static 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
20352042
20362043 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
20372044 {
2038− u64 inode_id = 0;
2045+ unsigned long index = 0;
20392046 struct btrfs_delayed_node *delayed_nodes[8];
2040− int i, n;
20412047
20422048 while (1) {
2049+ struct btrfs_delayed_node *node;
2050+ int count;
2051+
20432052 spin_lock(&root->inode_lock);
2044− n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
2045− (void **)delayed_nodes, inode_id,
2046− ARRAY_SIZE(delayed_nodes));
2047− if (!n) {
2053+ if (xa_empty(&root->delayed_nodes)) {
20482054 spin_unlock(&root->inode_lock);
2049− break;
2055+ return;
20502056 }
20512057
2052− inode_id = delayed_nodes[n - 1]->inode_id + 1;
2053− for (i = 0; i < n; i++) {
2058+ count = 0;
2059+ xa_for_each_start(&root->delayed_nodes, index, node, index) {
20542060 /*
20552061 * Don't increase refs in case the node is dead and
20562062 * about to be removed from the tree in the loop below
20572063 */
2058− if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
2059− delayed_nodes[i] = NULL;
2064+ if (refcount_inc_not_zero(&node->refs)) {
2065+ delayed_nodes[count] = node;
2066+ count++;
2067+ }
2068+ if (count >= ARRAY_SIZE(delayed_nodes))
2069+ break;
20602070 }
20612071 spin_unlock(&root->inode_lock);
2072+ index++;
20622073
2063− for (i = 0; i < n; i++) {
2064− if (!delayed_nodes[i])
2065− continue;
2074+ for (int i = 0; i < count; i++) {
20662075 __btrfs_kill_delayed_node(delayed_nodes[i]);
20672076 btrfs_release_delayed_node(delayed_nodes[i]);
20682077 }
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
550550 u64 physical)
551551 {
552552 struct btrfs_fs_info *fs_info = cache->fs_info;
553− struct extent_map *em;
554− struct map_lookup *map;
553+ struct btrfs_chunk_map *map;
555554 u64 chunk_offset = cache->start;
556555 int num_extents, cur_extent;
557556 int i;
@@ -567,9 +566,8 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev, · 9 unmodified lines
567566 }
568567 spin_unlock(&cache->lock);
569568
570− em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
571− ASSERT(!IS_ERR(em));
572− map = em->map_lookup;
569+ map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
570+ ASSERT(!IS_ERR(map));
573571
574572 num_extents = 0;
575573 cur_extent = 0;
@@ -583,7 +581,7 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev, · 7 unmodified lines
583581 cur_extent = i;
584582 }
585583
586− free_extent_map(em);
584+ btrfs_free_chunk_map(map);
587585
588586 if (num_extents > 1 && cur_extent < num_extents - 1) {
589587 /*
@@ -812,25 +810,23 @@ static void btrfs_dev_replace_update_device_in_mapping_tree( · 222 unmodified lines
812810 struct btrfs_device *srcdev,
813811 struct btrfs_device *tgtdev)
814812 {
815− struct extent_map_tree *em_tree = &fs_info->mapping_tree;
816− struct extent_map *em;
817− struct map_lookup *map;
818813 u64 start = 0;
819814 int i;
820815
821− write_lock(&em_tree->lock);
816+ write_lock(&fs_info->mapping_tree_lock);
822817 do {
823− em = lookup_extent_mapping(em_tree, start, (u64)-1);
824− if (!em)
818+ struct btrfs_chunk_map *map;
819+
820+ map = btrfs_find_chunk_map_nolock(fs_info, start, U64_MAX);
821+ if (!map)
825822 break;
826− map = em->map_lookup;
827823 for (i = 0; i < map->num_stripes; i++)
828824 if (srcdev == map->stripes[i].dev)
829825 map->stripes[i].dev = tgtdev;
830− start = em->start + em->len;
831− free_extent_map(em);
826+ start = map->start + map->chunk_len;
827+ btrfs_free_chunk_map(map);
832828 } while (start);
833− write_unlock(&em_tree->lock);
829+ write_unlock(&fs_info->mapping_tree_lock);
834830 }
835831
836832 static 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
7474 static void csum_tree_block(struct extent_buffer *buf, u8 *result)
7575 {
7676 struct btrfs_fs_info *fs_info = buf->fs_info;
77− const int num_pages = num_extent_pages(buf);
78− const int first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
77+ int num_pages;
78+ u32 first_page_part;
7979 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
8080 char *kaddr;
8181 int i;
8282
8383 shash->tfm = fs_info->csum_shash;
8484 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+
8697 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
8798 first_page_part - BTRFS_CSUM_SIZE);
8899
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+ */
89106 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]);
91108 crypto_shash_update(shash, kaddr, PAGE_SIZE);
92109 }
93110 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
166183 int mirror_num)
167184 {
168185 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);
170187 int ret = 0;
171188
172189 if (sb_rdonly(fs_info->sb))
173190 return -EROFS;
174191
175− for (i = 0; i < num_pages; i++) {
176− struct page *p = eb->pages[i];
177− u64 start = max_t(u64, eb->start, page_offset(p));
178− u64 end = min_t(u64, eb->start + eb->len, page_offset(p) + PAGE_SIZE);
192+ for (int i = 0; i < num_folios; i++) {
193+ struct folio *folio = eb->folios[i];
194+ u64 start = max_t(u64, eb->start, folio_pos(folio));
195+ u64 end = min_t(u64, eb->start + eb->len,
196+ folio_pos(folio) + folio_size(folio));
179197 u32 len = end - start;
180198
181199 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);
183202 if (ret)
184203 break;
185204 }
@@ -254,15 +273,20 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio) · 68 unmodified lines
254273 if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
255274 return BLK_STS_IOERR;
256275
257− if (test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)) {
258− WARN_ON_ONCE(found_start != 0);
276+ /*
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);
259283 return BLK_STS_OK;
260284 }
261285
262286 if (WARN_ON_ONCE(found_start != eb->start))
263287 return BLK_STS_IOERR;
264− if (WARN_ON(!btrfs_page_test_uptodate(fs_info, eb->pages[0], eb->start,
265− eb->len)))
288+ if (WARN_ON(!btrfs_folio_test_uptodate(fs_info, eb->folios[0],
289+ eb->start, eb->len)))
266290 return BLK_STS_IOERR;
267291
268292 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
371395 }
372396
373397 csum_tree_block(eb, result);
374− header_csum = page_address(eb->pages[0]) +
375− get_eb_offset_in_page(eb, offsetof(struct btrfs_header, csum));
398+ header_csum = folio_address(eb->folios[0]) +
399+ get_eb_offset_in_folio(eb, offsetof(struct btrfs_header, csum));
376400
377401 if (memcmp(result, header_csum, csum_size) != 0) {
378402 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
639663 root->nr_delalloc_inodes = 0;
640664 root->nr_ordered_extents = 0;
641665 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);
643668
644669 btrfs_init_root_block_rsv(root);
645670
@@ -650,14 +675,10 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, · 4 unmodified lines
650675 INIT_LIST_HEAD(&root->ordered_extents);
651676 INIT_LIST_HEAD(&root->ordered_root);
652677 INIT_LIST_HEAD(&root->reloc_dirty_list);
653− INIT_LIST_HEAD(&root->logged_list[0]);
654− INIT_LIST_HEAD(&root->logged_list[1]);
655678 spin_lock_init(&root->inode_lock);
656679 spin_lock_init(&root->delalloc_lock);
657680 spin_lock_init(&root->ordered_extent_lock);
658681 spin_lock_init(&root->accounting_lock);
659− spin_lock_init(&root->log_extents_lock[0]);
660− spin_lock_init(&root->log_extents_lock[1]);
661682 spin_lock_init(&root->qgroup_meta_rsv_lock);
662683 mutex_init(&root->objectid_mutex);
663684 mutex_init(&root->log_mutex);
@@ -2618,9 +2639,6 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) · 1954 unmodified lines
26182639 */
26192640 btrfs_set_super_log_root(sb, 0);
26202641
2621− /* We can't trust the free space cache either */
2622− btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
2623−
26242642 btrfs_warn(fs_info, "try to load backup roots slot %d", i);
26252643 ret = read_backup_root(fs_info, i);
26262644 backup_index = ret;
@@ -2724,7 +2742,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) · 97 unmodified lines
27242742 INIT_LIST_HEAD(&fs_info->allocated_ebs);
27252743 spin_lock_init(&fs_info->eb_leak_lock);
27262744 #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);
27282747 btrfs_init_block_rsv(&fs_info->global_block_rsv,
27292748 BTRFS_BLOCK_RSV_GLOBAL);
27302749 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
27942813 fs_info->sectorsize_bits = ilog2(4096);
27952814 fs_info->stripesize = 4096;
27962815
2816+ /* Default compress algorithm when user does -o compress */
2817+ fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2818+
27972819 fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
27982820
27992821 spin_lock_init(&fs_info->swapfile_pins_lock);
@@ -2930,17 +2952,6 @@ out: · 130 unmodified lines
29302952 return err;
29312953 }
29322954
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− */
2938−void 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−
29442955 /*
29452956 * Mounting logic specific to read-write file systems. Shared by open_ctree
29462957 * 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
29532964
29542965 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
29552966 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;
29572972 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
29582973 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
29592974 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
32763291 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
32773292 WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
32783293
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−
32863294 /* Set up fs_info before parsing mount options */
32873295 nodesize = btrfs_super_nodesize(disk_super);
32883296 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
32963304 fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
32973305 fs_info->stripesize = stripesize;
32983306
3299− ret = btrfs_parse_options(fs_info, options, sb->s_flags);
3300− if (ret)
3307+ /*
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;
33013315 goto fail_alloc;
3316+ }
33023317
33033318 ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
33043319 if (ret < 0)
33053320 goto fail_alloc;
33063321
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+
33073328 if (sectorsize < PAGE_SIZE) {
33083329 struct btrfs_subpage_info *subpage_info;
33093330
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−
33213331 btrfs_warn(fs_info,
33223332 "read-write for sector size %u with page size %lu is experimental",
33233333 sectorsize, PAGE_SIZE);
@@ -3494,29 +3504,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 170 unmodified lines
34943504 goto fail_cleaner;
34953505 }
34963506
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−
35203507 ret = btrfs_read_qgroup_config(fs_info);
35213508 if (ret)
35223509 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
35423529 }
35433530
35443531 if (sb_rdonly(sb))
3545− goto clear_oneshot;
3532+ return 0;
35463533
35473534 ret = btrfs_start_pre_rw_mount(fs_info);
35483535 if (ret) {
@@ -3570,8 +3557,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 21 unmodified lines
35703557 if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
35713558 wake_up_process(fs_info->cleaner_kthread);
35723559
3573−clear_oneshot:
3574− btrfs_clear_oneshot_options(fs_info);
35753560 return 0;
35763561
35773562 fail_qgroup:
@@ -3608,7 +3593,7 @@ fail_sb_buffer: · 30 unmodified lines
36083593 btrfs_stop_all_workers(fs_info);
36093594 btrfs_free_block_groups(fs_info);
36103595 fail_alloc:
3611− btrfs_mapping_tree_free(&fs_info->mapping_tree);
3596+ btrfs_mapping_tree_free(fs_info);
36123597
36133598 iput(fs_info->btree_inode);
36143599 fail:
@@ -4391,7 +4376,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) · 776 unmodified lines
43914376
43924377 iput(fs_info->btree_inode);
43934378
4394− btrfs_mapping_tree_free(&fs_info->mapping_tree);
4379+ btrfs_mapping_tree_free(fs_info);
43954380 btrfs_close_devices(fs_info->fs_devices);
43964381 }
43974382
fs/btrfs/disk-io.h+0−3
@@ -37,9 +37,6 @@ struct extent_buffer *btrfs_find_create_tree_block( · 36 unmodified lines
3737 struct btrfs_fs_info *fs_info,
3838 u64 bytenr, u64 owner_root,
3939 int level);
40−void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
41− struct extent_buffer *buf);
42−void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info);
4340 int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info);
4441 int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
4542 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
5858 struct extent_io_tree *tree,
5959 u64 start, u64 end)
6060 {
61− struct btrfs_inode *inode = tree->inode;
61+ const struct btrfs_inode *inode;
6262 u64 isize;
6363
64− if (!inode)
64+ if (tree->owner != IO_TREE_INODE_IO)
6565 return;
6666
67+ inode = extent_io_tree_to_inode_const(tree);
6768 isize = i_size_read(&inode->vfs_inode);
6869 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
6970 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
7879 #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
7980 #endif
8081
82+
8183 /*
82− * For the file_extent_tree, we want to hold the inode lock when we lookup and
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.
84+ * The only tree allowed to set the inode is IO_TREE_INODE_IO.
8785 */
88−static struct lock_class_key file_extent_tree_class;
86+static 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. */
92+struct 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. */
100+const 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+}
89106
90−struct tree_entry {
91− u64 start;
92− u64 end;
93− struct rb_node rb_node;
94−};
107+/* For read-only access to fs_info. */
108+const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree)
109+{
110+ if (tree->owner == IO_TREE_INODE_IO)
111+ return tree->inode->root->fs_info;
112+ return tree->fs_info;
113+}
95114
96115 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
97116 struct extent_io_tree *tree, unsigned int owner)
98117 {
99− tree->fs_info = fs_info;
100118 tree->state = RB_ROOT;
101119 spin_lock_init(&tree->lock);
102− tree->inode = NULL;
120+ tree->fs_info = fs_info;
103121 tree->owner = owner;
104− if (owner == IO_TREE_INODE_FILE_EXTENT)
105− lockdep_set_class(&tree->lock, &file_extent_tree_class);
106122 }
107123
108124 /*
@@ -329,10 +345,14 @@ static inline struct extent_state *tree_search(struct extent_io_tree *tree, u64 · 220 unmodified lines
329345 return tree_search_for_insert(tree, offset, NULL, NULL);
330346 }
331347
332−static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
348+static void extent_io_tree_panic(const struct extent_io_tree *tree,
349+ const struct extent_state *state,
350+ const char *opname,
351+ int err)
333352 {
334− btrfs_panic(tree->fs_info, err,
335− "locking error: extent tree was modified by another thread while locked");
353+ btrfs_panic(extent_io_tree_to_fs_info(tree), err,
354+ "extent io tree error on %s state start %llu end %llu",
355+ opname, state->start, state->end);
336356 }
337357
338358 static 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
341361
342362 prev = prev_state(state);
343363 if (prev && prev->end == state->start - 1 && prev->state == state->state) {
344− if (tree->inode)
345− btrfs_merge_delalloc_extent(tree->inode, state, prev);
364+ if (is_inode_io_tree(tree))
365+ btrfs_merge_delalloc_extent(extent_io_tree_to_inode(tree),
366+ state, prev);
346367 state->start = prev->start;
347368 rb_erase(&prev->rb_node, &tree->state);
348369 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
356377
357378 next = next_state(state);
358379 if (next && next->start == state->end + 1 && next->state == state->state) {
359− if (tree->inode)
360− btrfs_merge_delalloc_extent(tree->inode, state, next);
380+ if (is_inode_io_tree(tree))
381+ btrfs_merge_delalloc_extent(extent_io_tree_to_inode(tree),
382+ state, next);
361383 state->end = next->end;
362384 rb_erase(&next->rb_node, &tree->state);
363385 RB_CLEAR_NODE(&next->rb_node);
@@ -390,8 +412,8 @@ static void set_state_bits(struct extent_io_tree *tree, · 26 unmodified lines
390412 u32 bits_to_set = bits & ~EXTENT_CTLBITS;
391413 int ret;
392414
393− if (tree->inode)
394− btrfs_set_delalloc_extent(tree->inode, state, bits);
415+ if (is_inode_io_tree(tree))
416+ btrfs_set_delalloc_extent(extent_io_tree_to_inode(tree), state, bits);
395417
396418 ret = add_extent_changeset(state, bits_to_set, changeset, 1);
397419 BUG_ON(ret < 0);
@@ -436,9 +458,10 @@ static struct extent_state *insert_state(struct extent_io_tree *tree, · 38 unmodified lines
436458 if (state->end < entry->start) {
437459 if (try_merge && end == entry->start &&
438460 state->state == entry->state) {
439− if (tree->inode)
440− btrfs_merge_delalloc_extent(tree->inode,
441− state, entry);
461+ if (is_inode_io_tree(tree))
462+ btrfs_merge_delalloc_extent(
463+ extent_io_tree_to_inode(tree),
464+ state, entry);
442465 entry->start = state->start;
443466 merge_prev_state(tree, entry);
444467 state->state = 0;
@@ -448,9 +471,10 @@ static struct extent_state *insert_state(struct extent_io_tree *tree, · 3 unmodified lines
448471 } else if (state->end > entry->end) {
449472 if (try_merge && entry->end == start &&
450473 state->state == entry->state) {
451− if (tree->inode)
452− btrfs_merge_delalloc_extent(tree->inode,
453− state, entry);
474+ if (is_inode_io_tree(tree))
475+ btrfs_merge_delalloc_extent(
476+ extent_io_tree_to_inode(tree),
477+ state, entry);
454478 entry->end = state->end;
455479 merge_next_state(tree, entry);
456480 state->state = 0;
@@ -458,9 +482,6 @@ static struct extent_state *insert_state(struct extent_io_tree *tree, · 1 unmodified line
458482 }
459483 node = &(*node)->rb_right;
460484 } 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);
464485 return ERR_PTR(-EEXIST);
465486 }
466487 }
@@ -505,8 +526,9 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig, · 38 unmodified lines
505526 struct rb_node *parent = NULL;
506527 struct rb_node **node;
507528
508− if (tree->inode)
509− btrfs_split_delalloc_extent(tree->inode, orig, split);
529+ if (is_inode_io_tree(tree))
530+ btrfs_split_delalloc_extent(extent_io_tree_to_inode(tree), orig,
531+ split);
510532
511533 prealloc->start = orig->start;
512534 prealloc->end = split - 1;
@@ -553,8 +575,9 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree, · 40 unmodified lines
553575 u32 bits_to_clear = bits & ~EXTENT_CTLBITS;
554576 int ret;
555577
556− if (tree->inode)
557− btrfs_clear_delalloc_extent(tree->inode, state, bits);
578+ if (is_inode_io_tree(tree))
579+ btrfs_clear_delalloc_extent(extent_io_tree_to_inode(tree), state,
580+ bits);
558581
559582 ret = add_extent_changeset(state, bits_to_clear, changeset, 0);
560583 BUG_ON(ret < 0);
@@ -695,7 +718,7 @@ hit_next: · 134 unmodified lines
695718 goto search_again;
696719 err = split_state(tree, state, prealloc, start);
697720 if (err)
698− extent_io_tree_panic(tree, err);
721+ extent_io_tree_panic(tree, state, "split", err);
699722
700723 prealloc = NULL;
701724 if (err)
@@ -717,7 +740,7 @@ hit_next: · 15 unmodified lines
717740 goto search_again;
718741 err = split_state(tree, state, prealloc, end + 1);
719742 if (err)
720− extent_io_tree_panic(tree, err);
743+ extent_io_tree_panic(tree, state, "split", err);
721744
722745 if (wake)
723746 wake_up(&state->wq);
@@ -939,6 +962,8 @@ int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, · 215 unmodified lines
939962 struct extent_state *state;
940963 int ret = 1;
941964
965+ ASSERT(!btrfs_fs_incompat(extent_io_tree_to_fs_info(tree), NO_HOLES));
966+
942967 spin_lock(&tree->lock);
943968 state = find_first_extent_bit_state(tree, start, bits);
944969 if (state) {
@@ -1152,7 +1177,7 @@ hit_next: · 207 unmodified lines
11521177 goto search_again;
11531178 err = split_state(tree, state, prealloc, start);
11541179 if (err)
1155− extent_io_tree_panic(tree, err);
1180+ extent_io_tree_panic(tree, state, "split", err);
11561181
11571182 prealloc = NULL;
11581183 if (err)
@@ -1200,7 +1225,7 @@ hit_next: · 41 unmodified lines
12001225 inserted_state = insert_state(tree, prealloc, bits, changeset);
12011226 if (IS_ERR(inserted_state)) {
12021227 err = PTR_ERR(inserted_state);
1203− extent_io_tree_panic(tree, err);
1228+ extent_io_tree_panic(tree, prealloc, "insert", err);
12041229 }
12051230
12061231 cache_state(inserted_state, cached_state);
@@ -1228,7 +1253,7 @@ hit_next: · 21 unmodified lines
12281253 goto search_again;
12291254 err = split_state(tree, state, prealloc, end + 1);
12301255 if (err)
1231− extent_io_tree_panic(tree, err);
1256+ extent_io_tree_panic(tree, state, "split", err);
12321257
12331258 set_state_bits(tree, prealloc, bits, changeset);
12341259 cache_state(prealloc, cached_state);
@@ -1382,7 +1407,7 @@ hit_next: · 147 unmodified lines
13821407 }
13831408 err = split_state(tree, state, prealloc, start);
13841409 if (err)
1385− extent_io_tree_panic(tree, err);
1410+ extent_io_tree_panic(tree, state, "split", err);
13861411 prealloc = NULL;
13871412 if (err)
13881413 goto out;
@@ -1430,7 +1455,7 @@ hit_next: · 41 unmodified lines
14301455 inserted_state = insert_state(tree, prealloc, bits, NULL);
14311456 if (IS_ERR(inserted_state)) {
14321457 err = PTR_ERR(inserted_state);
1433− extent_io_tree_panic(tree, err);
1458+ extent_io_tree_panic(tree, prealloc, "insert", err);
14341459 }
14351460 cache_state(inserted_state, cached_state);
14361461 if (inserted_state == prealloc)
@@ -1453,7 +1478,7 @@ hit_next: · 16 unmodified lines
14531478
14541479 err = split_state(tree, state, prealloc, end + 1);
14551480 if (err)
1456− extent_io_tree_panic(tree, err);
1481+ extent_io_tree_panic(tree, state, "split", err);
14571482
14581483 set_state_bits(tree, prealloc, bits, NULL);
14591484 cache_state(prealloc, cached_state);
fs/btrfs/extent-io-tree.h+15−3
@@ -87,9 +87,17 @@ enum { · 86 unmodified lines
8787
8888 struct extent_io_tree {
8989 struct rb_root state;
90− struct btrfs_fs_info *fs_info;
91− /* Inode associated with this tree, or NULL. */
92− struct btrfs_inode *inode;
90+ /*
91+ * The fs_info is needed for trace points, a tree attached to an 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+ };
93101
94102 /* Who owns this io tree, should be one of IO_TREE_* */
95103 u8 owner;
@@ -112,6 +120,10 @@ struct extent_state { · 16 unmodified lines
112120 #endif
113121 };
114122
123+struct btrfs_inode *extent_io_tree_to_inode(struct extent_io_tree *tree);
124+const struct btrfs_inode *extent_io_tree_to_inode_const(const struct extent_io_tree *tree);
125+const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
126+
115127 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
116128 struct extent_io_tree *tree, unsigned int owner);
117129 void 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
34473447 {
34483448 struct btrfs_fs_info *fs_info = trans->fs_info;
34493449 struct btrfs_ref generic_ref = { 0 };
3450+ struct btrfs_block_group *bg;
34503451 int ret;
34513452
34523453 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
34603461 BUG_ON(ret); /* -ENOMEM */
34613462 }
34623463
3463− if (last_ref && btrfs_header_generation(buf) == trans->transid) {
3464− struct btrfs_block_group *cache;
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− }
3464+ if (!last_ref)
3465+ return;
34743466
3475− cache = btrfs_lookup_block_group(fs_info, buf->start);
3467+ if (btrfs_header_generation(buf) != trans->transid)
3468+ goto out;
34763469
3477− if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3478− pin_down_extent(trans, cache, buf->start, buf->len, 1);
3479− btrfs_put_block_group(cache);
3470+ if (root_id != BTRFS_TREE_LOG_OBJECTID) {
3471+ ret = check_ref_cleanup(trans, buf->start);
3472+ if (!ret)
34803473 goto out;
3481− }
3474+ }
34823475
3483− /*
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;
3476+ bg = btrfs_lookup_block_group(fs_info, buf->start);
35013477
3502− if (must_pin || btrfs_is_zoned(fs_info)) {
3503− btrfs_redirty_list_add(trans->transaction, buf);
3504− pin_down_extent(trans, cache, buf->start, buf->len, 1);
3505− btrfs_put_block_group(cache);
3506− goto out;
3507− }
3478+ if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3479+ pin_down_extent(trans, bg, buf->start, buf->len, 1);
3480+ btrfs_put_block_group(bg);
3481+ goto out;
3482+ }
35083483
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+ */
35103500
3511− btrfs_add_free_space(cache, buf->start, buf->len);
3512− btrfs_free_reserved_bytes(cache, buf->len, 0);
3513− btrfs_put_block_group(cache);
3514− trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
3501+ if (test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags)
3502+ || btrfs_is_zoned(fs_info)) {
3503+ pin_down_extent(trans, bg, buf->start, buf->len, 1);
3504+ btrfs_put_block_group(bg);
3505+ goto out;
35153506 }
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+
35163515 out:
3517− if (last_ref) {
3518− /*
3519− * Deleting the buffer, clear the corrupt flag since it doesn't
3520− * matter anymore.
3521− */
3522− clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3523− }
3516+
3517+ /*
3518+ * Deleting the buffer, clear the corrupt flag since it doesn't
3519+ * matter anymore.
3520+ */
3521+ clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
35243522 }
35253523
35263524 /* Can return -ENOMEM */
@@ -5061,7 +5059,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 1534 unmodified lines
50615059 __btrfs_tree_lock(buf, nest);
50625060 btrfs_clear_buffer_dirty(trans, buf);
50635061 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);
50655063
50665064 set_extent_buffer_uptodate(buf);
50675065
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
184184 struct page *page, struct page *locked_page,
185185 unsigned long page_ops, u64 start, u64 end)
186186 {
187+ struct folio *folio = page_folio(page);
187188 u32 len;
188189
189190 ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
190191 len = end + 1 - start;
191192
192193 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);
194195 if (page_ops & PAGE_START_WRITEBACK) {
195− btrfs_page_clamp_clear_dirty(fs_info, page, start, len);
196− btrfs_page_clamp_set_writeback(fs_info, page, start, len);
196+ btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
197+ btrfs_folio_clamp_set_writeback(fs_info, folio, start, len);
197198 }
198199 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);
200201
201202 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);
203204 }
204205
205206 static void __process_pages_contig(struct address_space *mapping,
@@ -271,19 +272,20 @@ static noinline int lock_delalloc_pages(struct inode *inode, · 65 unmodified lines
271272 goto out;
272273
273274 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);
275277 u32 len = end + 1 - start;
276278
277279 if (page == locked_page)
278280 continue;
279281
280− if (btrfs_page_start_writer_lock(fs_info, page, start,
281− len))
282+ if (btrfs_folio_start_writer_lock(fs_info, folio, start,
283+ len))
282284 goto out;
283285
284286 if (!PageDirty(page) || page->mapping != mapping) {
285− btrfs_page_end_writer_lock(fs_info, page, start,
286− len);
287+ btrfs_folio_end_writer_lock(fs_info, folio, start,
288+ len);
287289 goto out;
288290 }
289291
@@ -432,60 +434,65 @@ static bool btrfs_verify_page(struct page *page, u64 start) · 142 unmodified lines
432434 static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len)
433435 {
434436 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
437+ struct folio *folio = page_folio(page);
435438
436439 ASSERT(page_offset(page) <= start &&
437440 start + len <= page_offset(page) + PAGE_SIZE);
438441
439442 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);
441444 else
442− btrfs_page_clear_uptodate(fs_info, page, start, len);
445+ btrfs_folio_clear_uptodate(fs_info, folio, start, len);
443446
444− if (!btrfs_is_subpage(fs_info, page))
447+ if (!btrfs_is_subpage(fs_info, page->mapping))
445448 unlock_page(page);
446449 else
447− btrfs_subpage_end_reader(fs_info, page, start, len);
450+ btrfs_subpage_end_reader(fs_info, folio, start, len);
448451 }
449452
450453 /*
451− * after a writepage IO is done, we need to:
452− * clear the uptodate bits on error
453− * clear the writeback bits in the extent tree for this IO
454− * end_page_writeback if the page has no more pending IO
454+ * After a write IO is done, we need to:
455+ *
456+ * - clear the uptodate bits on error
457+ * - 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
455459 *
456460 * Scheduling is not allowed, so the extent state tree is expected
457461 * to have one and only one object corresponding to this IO.
458462 */
459−static void end_bio_extent_writepage(struct btrfs_bio *bbio)
463+static void end_bbio_data_write(struct btrfs_bio *bbio)
460464 {
461465 struct bio *bio = &bbio->bio;
462466 int error = blk_status_to_errno(bio->bi_status);
463− struct bio_vec *bvec;
464− struct bvec_iter_all iter_all;
467+ struct folio_iter fi;
465468
466469 ASSERT(!bio_flagged(bio, BIO_CLONED));
467− bio_for_each_segment_all(bvec, bio, iter_all) {
468− struct page *page = bvec->bv_page;
469− struct inode *inode = page->mapping->host;
470+ bio_for_each_folio_all(fi, bio) {
471+ struct folio *folio = fi.folio;
472+ struct inode *inode = folio->mapping->host;
470473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
471474 const u32 sectorsize = fs_info->sectorsize;
472− u64 start = page_offset(page) + bvec->bv_offset;
473− u32 len = bvec->bv_len;
475+ u64 start = folio_pos(folio) + fi.offset;
476+ u32 len = fi.length;
477+
478+ /* Only order 0 (single page) folios are allowed for data. */
479+ ASSERT(folio_order(folio) == 0);
474480
475481 /* Our read/write should always be sector aligned. */
476− if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
482+ if (!IS_ALIGNED(fi.offset, sectorsize))
477483 btrfs_err(fs_info,
478− "partial page write in btrfs with offset %u and length %u",
479− bvec->bv_offset, bvec->bv_len);
480− else if (!IS_ALIGNED(bvec->bv_len, sectorsize))
484+ "partial page write in btrfs with offset %zu and length %zu",
485+ fi.offset, fi.length);
486+ else if (!IS_ALIGNED(fi.length, sectorsize))
481487 btrfs_info(fs_info,
482− "incomplete page write with offset %u and length %u",
483− bvec->bv_offset, bvec->bv_len);
488+ "incomplete page write with offset %zu and length %zu",
489+ fi.offset, fi.length);
484490
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);
486493 if (error)
487− mapping_set_error(page->mapping, error);
488− btrfs_page_clear_writeback(fs_info, page, start, len);
494+ mapping_set_error(folio->mapping, error);
495+ btrfs_folio_clear_writeback(fs_info, folio, start, len);
489496 }
490497
491498 bio_put(bio);
@@ -562,98 +569,102 @@ update: · 70 unmodified lines
562569
563570 static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
564571 {
565− ASSERT(PageLocked(page));
566− if (!btrfs_is_subpage(fs_info, page))
572+ struct folio *folio = page_folio(page);
573+
574+ ASSERT(folio_test_locked(folio));
575+ if (!btrfs_is_subpage(fs_info, folio->mapping))
567576 return;
568577
569− ASSERT(PagePrivate(page));
570− btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE);
578+ ASSERT(folio_test_private(folio));
579+ btrfs_subpage_start_reader(fs_info, folio, page_offset(page), PAGE_SIZE);
571580 }
572581
573582 /*
574− * after a readpage IO is done, we need to:
575− * clear the uptodate bits on error
576− * set the uptodate bits if things worked
577− * set the page up to date if all extents in the tree are uptodate
578− * clear the lock bit in the extent tree
579− * unlock the page if there are no other extents locked for it
583+ * After a data read IO is done, we need to:
584+ *
585+ * - clear the uptodate bits on error
586+ * - set the uptodate bits if things worked
587+ * - set the folio up to date if all extents in the tree are uptodate
588+ * - clear the lock bit in the extent tree
589+ * - unlock the folio if there are no other extents locked for it
580590 *
581591 * Scheduling is not allowed, so the extent state tree is expected
582592 * to have one and only one object corresponding to this IO.
583593 */
584−static void end_bio_extent_readpage(struct btrfs_bio *bbio)
594+static void end_bbio_data_read(struct btrfs_bio *bbio)
585595 {
586596 struct bio *bio = &bbio->bio;
587− struct bio_vec *bvec;
588597 struct processed_extent processed = { 0 };
598+ struct folio_iter fi;
589599 /*
590600 * The offset to the beginning of a bio, since one bio can never be
591601 * larger than UINT_MAX, u32 here is enough.
592602 */
593603 u32 bio_offset = 0;
594− struct bvec_iter_all iter_all;
595604
596605 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) {
598607 bool uptodate = !bio->bi_status;
599− struct page *page = bvec->bv_page;
600− struct inode *inode = page->mapping->host;
608+ struct folio *folio = fi.folio;
609+ struct inode *inode = folio->mapping->host;
601610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
602611 const u32 sectorsize = fs_info->sectorsize;
603612 u64 start;
604613 u64 end;
605614 u32 len;
606615
616+ /* For now only order 0 folios are supported for data. */
617+ ASSERT(folio_order(folio) == 0);
607618 btrfs_debug(fs_info,
608− "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
609− bio->bi_iter.bi_sector, bio->bi_status,
619+ "%s: bi_sector=%llu, err=%d, mirror=%u",
620+ __func__, bio->bi_iter.bi_sector, bio->bi_status,
610621 bbio->mirror_num);
611622
612623 /*
613624 * We always issue full-sector reads, but if some block in a
614− * page fails to read, blk_update_request() will advance
625+ * folio fails to read, blk_update_request() will advance
615626 * bv_offset and adjust bv_len to compensate. Print a warning
616627 * for unaligned offsets, and an error if they don't add up to
617628 * a full sector.
618629 */
619− if (!IS_ALIGNED(bvec->bv_offset, sectorsize))
630+ if (!IS_ALIGNED(fi.offset, sectorsize))
620631 btrfs_err(fs_info,
621− "partial page read in btrfs with offset %u and length %u",
622− bvec->bv_offset, bvec->bv_len);
623− else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len,
624− sectorsize))
632+ "partial page read in btrfs with offset %zu and length %zu",
633+ fi.offset, fi.length);
634+ else if (!IS_ALIGNED(fi.offset + fi.length, sectorsize))
625635 btrfs_info(fs_info,
626− "incomplete page read with offset %u and length %u",
627− bvec->bv_offset, bvec->bv_len);
636+ "incomplete page read with offset %zu and length %zu",
637+ fi.offset, fi.length);
628638
629− start = page_offset(page) + bvec->bv_offset;
630− end = start + bvec->bv_len - 1;
631− len = bvec->bv_len;
639+ start = folio_pos(folio) + fi.offset;
640+ end = start + fi.length - 1;
641+ len = fi.length;
632642
633643 if (likely(uptodate)) {
634644 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);
636646
637647 /*
638648 * Zero out the remaining part if this range straddles
639649 * i_size.
640650 *
641− * Here we should only zero the range inside the bvec,
651+ * Here we should only zero the range inside the folio,
642652 * not touch anything else.
643653 *
644654 * NOTE: i_size is exclusive while end is inclusive.
645655 */
646− if (page->index == end_index && i_size <= end) {
647− u32 zero_start = max(offset_in_page(i_size),
648− offset_in_page(start));
656+ if (folio_index(folio) == end_index && i_size <= end) {
657+ u32 zero_start = max(offset_in_folio(folio, i_size),
658+ offset_in_folio(folio, start));
659+ u32 zero_len = offset_in_folio(folio, end) + 1 -
660+ zero_start;
649661
650− zero_user_segment(page, zero_start,
651− offset_in_page(end) + 1);
662+ folio_zero_range(folio, zero_start, zero_len);
652663 }
653664 }
654665
655666 /* Update page status and unlock. */
656− end_page_read(page, uptodate, start, len);
667+ end_page_read(folio_page(folio, 0), uptodate, start, len);
657668 endio_readpage_release_extent(&processed, BTRFS_I(inode),
658669 start, end, uptodate);
659670
@@ -672,19 +683,22 @@ static void end_bio_extent_readpage(struct btrfs_bio *bbio) · 12 unmodified lines
672683 * @nr_pages: number of pages to allocate
673684 * @page_array: the array to fill with pages; any existing non-null entries in
674685 * the array will be skipped
686+ * @extra_gfp: the extra GFP flags for the allocation.
675687 *
676688 * Return: 0 if all pages were able to be allocated;
677689 * -ENOMEM otherwise, the partially allocated pages would be freed and
678690 * the array slots zeroed
679691 */
680−int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array)
692+int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
693+ gfp_t extra_gfp)
681694 {
682695 unsigned int allocated;
683696
684697 for (allocated = 0; allocated < nr_pages;) {
685698 unsigned int last = allocated;
686699
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);
688702
689703 if (allocated == nr_pages)
690704 return 0;
@@ -707,6 +721,26 @@ int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array) · 16 unmodified lines
707721 return 0;
708722 }
709723
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+ */
729+static 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+
710744 static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,
711745 struct page *page, u64 disk_bytenr,
712746 unsigned int pg_offset)
@@ -861,9 +895,9 @@ static void submit_extent_page(struct btrfs_bio_ctrl *bio_ctrl, · 148 unmodified lines
861895 } while (size);
862896 }
863897
864−static int attach_extent_buffer_page(struct extent_buffer *eb,
865− struct page *page,
866− struct btrfs_subpage *prealloc)
898+static int attach_extent_buffer_folio(struct extent_buffer *eb,
899+ struct folio *folio,
900+ struct btrfs_subpage *prealloc)
867901 {
868902 struct btrfs_fs_info *fs_info = eb->fs_info;
869903 int ret = 0;
@@ -874,65 +908,66 @@ static int attach_extent_buffer_page(struct extent_buffer *eb, · 4 unmodified lines
874908 * For cloned or dummy extent buffers, their pages are not mapped and
875909 * will not race with any other ebs.
876910 */
877− if (page->mapping)
878− lockdep_assert_held(&page->mapping->i_private_lock);
911+ if (folio->mapping)
912+ lockdep_assert_held(&folio->mapping->i_private_lock);
879913
880914 if (fs_info->nodesize >= PAGE_SIZE) {
881− if (!PagePrivate(page))
882− attach_page_private(page, eb);
915+ if (!folio_test_private(folio))
916+ folio_attach_private(folio, eb);
883917 else
884− WARN_ON(page->private != (unsigned long)eb);
918+ WARN_ON(folio_get_private(folio) != eb);
885919 return 0;
886920 }
887921
888922 /* Already mapped, just free prealloc */
889− if (PagePrivate(page)) {
923+ if (folio_test_private(folio)) {
890924 btrfs_free_subpage(prealloc);
891925 return 0;
892926 }
893927
894928 if (prealloc)
895929 /* Has preallocated memory for subpage */
896− attach_page_private(page, prealloc);
930+ folio_attach_private(folio, prealloc);
897931 else
898932 /* Do new allocation to attach subpage */
899− ret = btrfs_attach_subpage(fs_info, page,
900− BTRFS_SUBPAGE_METADATA);
933+ ret = btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_METADATA);
901934 return ret;
902935 }
903936
904937 int set_page_extent_mapped(struct page *page)
905938 {
939+ struct folio *folio = page_folio(page);
906940 struct btrfs_fs_info *fs_info;
907941
908942 ASSERT(page->mapping);
909943
910− if (PagePrivate(page))
944+ if (folio_test_private(folio))
911945 return 0;
912946
913947 fs_info = btrfs_sb(page->mapping->host->i_sb);
914948
915− if (btrfs_is_subpage(fs_info, page))
916− return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA);
949+ if (btrfs_is_subpage(fs_info, page->mapping))
950+ return btrfs_attach_subpage(fs_info, folio, BTRFS_SUBPAGE_DATA);
917951
918− attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE);
952+ folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
919953 return 0;
920954 }
921955
922956 void clear_page_extent_mapped(struct page *page)
923957 {
958+ struct folio *folio = page_folio(page);
924959 struct btrfs_fs_info *fs_info;
925960
926961 ASSERT(page->mapping);
927962
928− if (!PagePrivate(page))
963+ if (!folio_test_private(folio))
929964 return;
930965
931966 fs_info = btrfs_sb(page->mapping->host->i_sb);
932− if (btrfs_is_subpage(fs_info, page))
933− return btrfs_detach_subpage(fs_info, page);
967+ if (btrfs_is_subpage(fs_info, page->mapping))
968+ return btrfs_detach_subpage(fs_info, folio);
934969
935− detach_page_private(page);
970+ folio_detach_private(folio);
936971 }
937972
938973 static struct extent_map *
@@ -1001,7 +1036,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 62 unmodified lines
10011036 memzero_page(page, zero_offset, iosize);
10021037 }
10031038 }
1004− bio_ctrl->end_io_func = end_bio_extent_readpage;
1039+ bio_ctrl->end_io_func = end_bbio_data_read;
10051040 begin_page_read(fs_info, page);
10061041 while (cur <= end) {
10071042 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
10271062 BUG_ON(extent_map_end(em) <= cur);
10281063 BUG_ON(end < cur);
10291064
1030− if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1031− compress_type = em->compress_type;
1065+ compress_type = extent_map_compression(em);
10321066
10331067 iosize = min(extent_map_end(em) - cur, end - cur + 1);
10341068 iosize = ALIGN(iosize, blocksize);
@@ -1037,7 +1071,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 2 unmodified lines
10371071 else
10381072 disk_bytenr = em->block_start + extent_offset;
10391073 block_start = em->block_start;
1040− if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1074+ if (em->flags & EXTENT_FLAG_PREALLOC)
10411075 block_start = EXTENT_MAP_HOLE;
10421076
10431077 /*
@@ -1074,7 +1108,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, · 30 unmodified lines
10741108 * is a corner case so we prioritize correctness over
10751109 * non-optimal behavior (submitting 2 bios for the same extent).
10761110 */
1077− if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
1111+ if (compress_type != BTRFS_COMPRESS_NONE &&
10781112 prev_em_start && *prev_em_start != (u64)-1 &&
10791113 *prev_em_start != em->start)
10801114 force_bio_submit = true;
@@ -1240,7 +1274,8 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, · 159 unmodified lines
12401274 static void find_next_dirty_byte(struct btrfs_fs_info *fs_info,
12411275 struct page *page, u64 *start, u64 *end)
12421276 {
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);
12441279 struct btrfs_subpage_info *spi = fs_info->subpage_info;
12451280 u64 orig_start = *start;
12461281 /* 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
12521287 * For regular sector size == page size case, since one page only
12531288 * contains one sector, we return the page offset directly.
12541289 */
1255− if (!btrfs_is_subpage(fs_info, page)) {
1290+ if (!btrfs_is_subpage(fs_info, page->mapping)) {
12561291 *start = page_offset(page);
12571292 *end = page_offset(page) + PAGE_SIZE;
12581293 return;
@@ -1305,7 +1340,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 46 unmodified lines
13051340 return 1;
13061341 }
13071342
1308− bio_ctrl->end_io_func = end_bio_extent_writepage;
1343+ bio_ctrl->end_io_func = end_bbio_data_write;
13091344 while (cur <= end) {
13101345 u32 len = end - cur + 1;
13111346 u64 disk_bytenr;
@@ -1325,7 +1360,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 13 unmodified lines
13251360 * writeback the sectors with subpage dirty bits,
13261361 * causing writeback without ordered extent.
13271362 */
1328− btrfs_page_clear_dirty(fs_info, page, cur, len);
1363+ btrfs_folio_clear_dirty(fs_info, page_folio(page), cur, len);
13291364 break;
13301365 }
13311366
@@ -1352,7 +1387,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 20 unmodified lines
13521387 block_start = em->block_start;
13531388 disk_bytenr = em->block_start + extent_offset;
13541389
1355− ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
1390+ ASSERT(!extent_map_is_compressed(em));
13561391 ASSERT(block_start != EXTENT_MAP_HOLE);
13571392 ASSERT(block_start != EXTENT_MAP_INLINE);
13581393
@@ -1377,7 +1412,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 18 unmodified lines
13771412 * So clear subpage dirty bit here so next time we won't submit
13781413 * page for range already written to disk.
13791414 */
1380− btrfs_page_clear_dirty(fs_info, page, cur, iosize);
1415+ btrfs_folio_clear_dirty(fs_info, page_folio(page), cur, iosize);
13811416
13821417 submit_extent_page(bio_ctrl, disk_bytenr, page, iosize,
13831418 cur - page_offset(page));
@@ -1385,7 +1420,7 @@ static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, · 1 unmodified line
13851420 nr++;
13861421 }
13871422
1388− btrfs_page_assert_not_dirty(fs_info, page);
1423+ btrfs_folio_assert_not_dirty(fs_info, page_folio(page));
13891424 *nr_ret = nr;
13901425 return 0;
13911426
@@ -1607,24 +1642,23 @@ static struct extent_buffer *find_extent_buffer_nolock( · 215 unmodified lines
16071642 return NULL;
16081643 }
16091644
1610−static void extent_buffer_write_end_io(struct btrfs_bio *bbio)
1645+static void end_bbio_meta_write(struct btrfs_bio *bbio)
16111646 {
16121647 struct extent_buffer *eb = bbio->private;
16131648 struct btrfs_fs_info *fs_info = eb->fs_info;
16141649 bool uptodate = !bbio->bio.bi_status;
1615− struct bvec_iter_all iter_all;
1616− struct bio_vec *bvec;
1650+ struct folio_iter fi;
16171651 u32 bio_offset = 0;
16181652
16191653 if (!uptodate)
16201654 set_btree_ioerr(eb);
16211655
1622− bio_for_each_segment_all(bvec, &bbio->bio, iter_all) {
1656+ bio_for_each_folio_all(fi, &bbio->bio) {
16231657 u64 start = eb->start + bio_offset;
1624− struct page *page = bvec->bv_page;
1625− u32 len = bvec->bv_len;
1658+ struct folio *folio = fi.folio;
1659+ u32 len = fi.length;
16261660
1627− btrfs_page_clear_writeback(fs_info, page, start, len);
1661+ btrfs_folio_clear_writeback(fs_info, folio, start, len);
16281662 bio_offset += len;
16291663 }
16301664
@@ -1673,36 +1707,44 @@ static noinline_for_stack void write_one_eb(struct extent_buffer *eb, · 42 unmodified lines
16731707
16741708 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
16751709 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);
16771711 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
16781712 bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
16791713 wbc_init_bio(wbc, &bbio->bio);
16801714 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);
16811715 bbio->file_offset = eb->start;
16821716 if (fs_info->nodesize < PAGE_SIZE) {
1683− struct page *p = eb->pages[0];
1717+ struct folio *folio = eb->folios[0];
1718+ bool ret;
16841719
1685− lock_page(p);
1686− btrfs_subpage_set_writeback(fs_info, p, eb->start, eb->len);
1687− if (btrfs_subpage_clear_and_test_dirty(fs_info, p, eb->start,
1720+ folio_lock(folio);
1721+ btrfs_subpage_set_writeback(fs_info, folio, eb->start, eb->len);
1722+ if (btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start,
16881723 eb->len)) {
1689− clear_page_dirty_for_io(p);
1724+ folio_clear_dirty_for_io(folio);
16901725 wbc->nr_to_write--;
16911726 }
1692− __bio_add_page(&bbio->bio, p, eb->len, eb->start - page_offset(p));
1693− wbc_account_cgroup_owner(wbc, p, eb->len);
1694− unlock_page(p);
1727+ ret = bio_add_folio(&bbio->bio, folio, eb->len,
1728+ eb->start - folio_pos(folio));
1729+ ASSERT(ret);
1730+ wbc_account_cgroup_owner(wbc, folio_page(folio, 0), eb->len);
1731+ folio_unlock(folio);
16951732 } else {
1696− for (int i = 0; i < num_extent_pages(eb); i++) {
1697− struct page *p = eb->pages[i];
1698−
1699− lock_page(p);
1700− clear_page_dirty_for_io(p);
1701− set_page_writeback(p);
1702− __bio_add_page(&bbio->bio, p, PAGE_SIZE, 0);
1703− wbc_account_cgroup_owner(wbc, p, PAGE_SIZE);
1704− wbc->nr_to_write--;
1705− unlock_page(p);
1733+ int num_folios = num_extent_folios(eb);
1734+
1735+ for (int i = 0; i < num_folios; i++) {
1736+ struct folio *folio = eb->folios[i];
1737+ bool ret;
1738+
1739+ folio_lock(folio);
1740+ folio_clear_dirty_for_io(folio);
1741+ folio_start_writeback(folio);
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);
17061748 }
17071749 }
17081750 btrfs_submit_bio(bbio, 0);
@@ -1725,6 +1767,7 @@ static noinline_for_stack void write_one_eb(struct extent_buffer *eb, · 16 unmodified lines
17251767 static int submit_eb_subpage(struct page *page, struct writeback_control *wbc)
17261768 {
17271769 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
1770+ struct folio *folio = page_folio(page);
17281771 int submitted = 0;
17291772 u64 page_start = page_offset(page);
17301773 int bit_start = 0;
@@ -1732,7 +1775,7 @@ static int submit_eb_subpage(struct page *page, struct writeback_control *wbc) · 1 unmodified line
17321775
17331776 /* Lock and write each dirty extent buffers in the range */
17341777 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);
17361779 struct extent_buffer *eb;
17371780 unsigned long flags;
17381781 u64 start;
@@ -1742,7 +1785,7 @@ static int submit_eb_subpage(struct page *page, struct writeback_control *wbc) · 3 unmodified lines
17421785 * in the meantime.
17431786 */
17441787 spin_lock(&page->mapping->i_private_lock);
1745− if (!PagePrivate(page)) {
1788+ if (!folio_test_private(folio)) {
17461789 spin_unlock(&page->mapping->i_private_lock);
17471790 break;
17481791 }
@@ -1807,22 +1850,23 @@ static int submit_eb_page(struct page *page, struct btrfs_eb_write_context *ctx) · 58 unmodified lines
18071850 {
18081851 struct writeback_control *wbc = ctx->wbc;
18091852 struct address_space *mapping = page->mapping;
1853+ struct folio *folio = page_folio(page);
18101854 struct extent_buffer *eb;
18111855 int ret;
18121856
1813− if (!PagePrivate(page))
1857+ if (!folio_test_private(folio))
18141858 return 0;
18151859
18161860 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
18171861 return submit_eb_subpage(page, wbc);
18181862
18191863 spin_lock(&mapping->i_private_lock);
1820− if (!PagePrivate(page)) {
1864+ if (!folio_test_private(folio)) {
18211865 spin_unlock(&mapping->i_private_lock);
18221866 return 0;
18231867 }
18241868
1825− eb = (struct extent_buffer *)page->private;
1869+ eb = folio_get_private(folio);
18261870
18271871 /*
18281872 * 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
22012245 cur, cur_len, !ret);
22022246 mapping_set_error(page->mapping, ret);
22032247 }
2204− btrfs_page_unlock_writer(fs_info, page, cur, cur_len);
2248+ btrfs_folio_unlock_writer(fs_info, page_folio(page), cur, cur_len);
22052249 if (ret < 0)
22062250 found_error = true;
22072251 next_page:
@@ -2352,7 +2396,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask) · 144 unmodified lines
23522396 write_unlock(&map->lock);
23532397 break;
23542398 }
2355− if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2399+ if ((em->flags & EXTENT_FLAG_PINNED) ||
23562400 em->start != start) {
23572401 write_unlock(&map->lock);
23582402 free_extent_map(em);
@@ -2369,7 +2413,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask) · 10 unmodified lines
23692413 * extra reference on the em.
23702414 */
23712415 if (list_empty(&em->list) ||
2372− test_bit(EXTENT_FLAG_LOGGING, &em->flags))
2416+ (em->flags & EXTENT_FLAG_LOGGING))
23732417 goto remove_em;
23742418 /*
23752419 * 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
30583102 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
30593103 }
30603104
3061−static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page)
3105+static bool folio_range_has_eb(struct btrfs_fs_info *fs_info, struct folio *folio)
30623106 {
30633107 struct btrfs_subpage *subpage;
30643108
3065− lockdep_assert_held(&page->mapping->i_private_lock);
3109+ lockdep_assert_held(&folio->mapping->i_private_lock);
30663110
3067− if (PagePrivate(page)) {
3068− subpage = (struct btrfs_subpage *)page->private;
3111+ if (folio_test_private(folio)) {
3112+ subpage = folio_get_private(folio);
30693113 if (atomic_read(&subpage->eb_refs))
30703114 return true;
30713115 /*
@@ -3078,21 +3122,21 @@ static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page) · 6 unmodified lines
30783122 return false;
30793123 }
30803124
3081−static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
3125+static void detach_extent_buffer_folio(struct extent_buffer *eb, struct folio *folio)
30823126 {
30833127 struct btrfs_fs_info *fs_info = eb->fs_info;
30843128 const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
30853129
30863130 /*
3087− * For mapped eb, we're going to change the page private, which should
3131+ * For mapped eb, we're going to change the folio private, which should
30883132 * be done under the i_private_lock.
30893133 */
30903134 if (mapped)
3091− spin_lock(&page->mapping->i_private_lock);
3135+ spin_lock(&folio->mapping->i_private_lock);
30923136
3093− if (!PagePrivate(page)) {
3137+ if (!folio_test_private(folio)) {
30943138 if (mapped)
3095− spin_unlock(&page->mapping->i_private_lock);
3139+ spin_unlock(&folio->mapping->i_private_lock);
30963140 return;
30973141 }
30983142
@@ -3101,66 +3145,58 @@ static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *pag · 2 unmodified lines
31013145 * We do this since we'll remove the pages after we've
31023146 * removed the eb from the radix tree, so we could race
31033147 * and have this page now attached to the new eb. So
3104− * only clear page_private if it's still connected to
3148+ * only clear folio if it's still connected to
31053149 * this eb.
31063150 */
3107− if (PagePrivate(page) &&
3108− page->private == (unsigned long)eb) {
3151+ if (folio_test_private(folio) && folio_get_private(folio) == eb) {
31093152 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3110− BUG_ON(PageDirty(page));
3111− BUG_ON(PageWriteback(page));
3112− /*
3113− * We need to make sure we haven't be attached
3114− * to a new eb.
3115− */
3116− detach_page_private(page);
3153+ BUG_ON(folio_test_dirty(folio));
3154+ BUG_ON(folio_test_writeback(folio));
3155+ /* We need to make sure we haven't be attached to a new eb. */
3156+ folio_detach_private(folio);
31173157 }
31183158 if (mapped)
3119− spin_unlock(&page->mapping->i_private_lock);
3159+ spin_unlock(&folio->mapping->i_private_lock);
31203160 return;
31213161 }
31223162
31233163 /*
3124− * For subpage, we can have dummy eb with page private. In this case,
3125− * we can directly detach the private as such page is only attached to
3126− * one dummy eb, no sharing.
3164+ * For subpage, we can have dummy eb with folio private attached. In
3165+ * this case, we can directly detach the private as such folio is only
3166+ * attached to one dummy eb, no sharing.
31273167 */
31283168 if (!mapped) {
3129− btrfs_detach_subpage(fs_info, page);
3169+ btrfs_detach_subpage(fs_info, folio);
31303170 return;
31313171 }
31323172
3133− btrfs_page_dec_eb_refs(fs_info, page);
3173+ btrfs_folio_dec_eb_refs(fs_info, folio);
31343174
31353175 /*
3136− * We can only detach the page private if there are no other ebs in the
3176+ * We can only detach the folio private if there are no other ebs in the
31373177 * page range and no unfinished IO.
31383178 */
3139− if (!page_range_has_eb(fs_info, page))
3140− btrfs_detach_subpage(fs_info, page);
3179+ if (!folio_range_has_eb(fs_info, folio))
3180+ btrfs_detach_subpage(fs_info, folio);
31413181
3142− spin_unlock(&page->mapping->i_private_lock);
3182+ spin_unlock(&folio->mapping->i_private_lock);
31433183 }
31443184
31453185 /* Release all pages attached to the extent buffer */
31463186 static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)
31473187 {
3148− int i;
3149− int num_pages;
3150−
31513188 ASSERT(!extent_buffer_under_io(eb));
31523189
3153− num_pages = num_extent_pages(eb);
3154− for (i = 0; i < num_pages; i++) {
3155− struct page *page = eb->pages[i];
3190+ for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) {
3191+ struct folio *folio = eb->folios[i];
31563192
3157− if (!page)
3193+ if (!folio)
31583194 continue;
31593195
3160− detach_extent_buffer_page(eb, page);
3196+ detach_extent_buffer_folio(eb, folio);
31613197
3162− /* One for when we allocated the page */
3163− put_page(page);
3198+ /* One for when we allocated the folio. */
3199+ folio_put(folio);
31643200 }
31653201 }
31663202
@@ -3198,9 +3234,8 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, · 31 unmodified lines
31983234
31993235 struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
32003236 {
3201− int i;
32023237 struct extent_buffer *new;
3203− int num_pages = num_extent_pages(src);
3238+ int num_folios = num_extent_folios(src);
32043239 int ret;
32053240
32063241 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
32143249 */
32153250 set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
32163251
3217− ret = btrfs_alloc_page_array(num_pages, new->pages);
3252+ ret = alloc_eb_folio_array(new, 0);
32183253 if (ret) {
32193254 btrfs_release_extent_buffer(new);
32203255 return NULL;
32213256 }
32223257
3223− for (i = 0; i < num_pages; i++) {
3258+ for (int i = 0; i < num_folios; i++) {
3259+ struct folio *folio = new->folios[i];
32243260 int ret;
3225− struct page *p = new->pages[i];
32263261
3227− ret = attach_extent_buffer_page(new, p, NULL);
3262+ ret = attach_extent_buffer_folio(new, folio, NULL);
32283263 if (ret < 0) {
32293264 btrfs_release_extent_buffer(new);
32303265 return NULL;
32313266 }
3232− WARN_ON(PageDirty(p));
3267+ WARN_ON(folio_test_dirty(folio));
32333268 }
32343269 copy_extent_buffer_full(new, src);
32353270 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
32413276 u64 start, unsigned long len)
32423277 {
32433278 struct extent_buffer *eb;
3244− int num_pages;
3245− int i;
3279+ int num_folios = 0;
32463280 int ret;
32473281
32483282 eb = __alloc_extent_buffer(fs_info, start, len);
32493283 if (!eb)
32503284 return NULL;
32513285
3252− num_pages = num_extent_pages(eb);
3253− ret = btrfs_alloc_page_array(num_pages, eb->pages);
3286+ ret = alloc_eb_folio_array(eb, 0);
32543287 if (ret)
32553288 goto err;
32563289
3257− for (i = 0; i < num_pages; i++) {
3258− struct page *p = eb->pages[i];
3259−
3260− ret = attach_extent_buffer_page(eb, p, NULL);
3290+ num_folios = num_extent_folios(eb);
3291+ for (int i = 0; i < num_folios; i++) {
3292+ ret = attach_extent_buffer_folio(eb, eb->folios[i], NULL);
32613293 if (ret < 0)
32623294 goto err;
32633295 }
@@ -3268,10 +3300,10 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, · 4 unmodified lines
32683300
32693301 return eb;
32703302 err:
3271− for (i = 0; i < num_pages; i++) {
3272− if (eb->pages[i]) {
3273− detach_extent_buffer_page(eb, eb->pages[i]);
3274− __free_page(eb->pages[i]);
3303+ for (int i = 0; i < num_folios; i++) {
3304+ if (eb->folios[i]) {
3305+ detach_extent_buffer_folio(eb, eb->folios[i]);
3306+ __folio_put(eb->folios[i]);
32753307 }
32763308 }
32773309 __free_extent_buffer(eb);
@@ -3320,20 +3352,14 @@ static void check_buffer_tree_ref(struct extent_buffer *eb) · 42 unmodified lines
33203352 spin_unlock(&eb->refs_lock);
33213353 }
33223354
3323−static void mark_extent_buffer_accessed(struct extent_buffer *eb,
3324− struct page *accessed)
3355+static void mark_extent_buffer_accessed(struct extent_buffer *eb)
33253356 {
3326− int num_pages, i;
3357+ int num_folios= num_extent_folios(eb);
33273358
33283359 check_buffer_tree_ref(eb);
33293360
3330− num_pages = num_extent_pages(eb);
3331− for (i = 0; i < num_pages; i++) {
3332− struct page *p = eb->pages[i];
3333−
3334− if (p != accessed)
3335− mark_page_accessed(p);
3336− }
3361+ for (int i = 0; i < num_folios; i++)
3362+ folio_mark_accessed(eb->folios[i]);
33373363 }
33383364
33393365 struct 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
33613387 spin_lock(&eb->refs_lock);
33623388 spin_unlock(&eb->refs_lock);
33633389 }
3364− mark_extent_buffer_accessed(eb, NULL);
3390+ mark_extent_buffer_accessed(eb);
33653391 return eb;
33663392 }
33673393
@@ -3410,6 +3436,7 @@ free_eb: · 42 unmodified lines
34103436 static struct extent_buffer *grab_extent_buffer(
34113437 struct btrfs_fs_info *fs_info, struct page *page)
34123438 {
3439+ struct folio *folio = page_folio(page);
34133440 struct extent_buffer *exists;
34143441
34153442 /*
@@ -3421,21 +3448,21 @@ static struct extent_buffer *grab_extent_buffer( · 5 unmodified lines
34213448 return NULL;
34223449
34233450 /* Page not yet attached to an extent buffer */
3424− if (!PagePrivate(page))
3451+ if (!folio_test_private(folio))
34253452 return NULL;
34263453
34273454 /*
34283455 * We could have already allocated an eb for this page and attached one
34293456 * so lets see if we can get a ref on the existing eb, and if we can we
34303457 * 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.
34323459 */
3433− exists = (struct extent_buffer *)page->private;
3460+ exists = folio_get_private(folio);
34343461 if (atomic_inc_not_zero(&exists->refs))
34353462 return exists;
34363463
34373464 WARN_ON(PageDirty(page));
3438− detach_page_private(page);
3465+ folio_detach_private(folio);
34393466 return NULL;
34403467 }
34413468
@@ -3469,19 +3496,88 @@ static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) · 27 unmodified lines
34693496 return 0;
34703497 }
34713498
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+ */
3508+static 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+
3523+retry:
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+
34723569 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
34733570 u64 start, u64 owner_root, int level)
34743571 {
34753572 unsigned long len = fs_info->nodesize;
3476− int num_pages;
3477− int i;
3478− unsigned long index = start >> PAGE_SHIFT;
3573+ int num_folios;
3574+ int attached = 0;
34793575 struct extent_buffer *eb;
3480− struct extent_buffer *exists = NULL;
3481− struct page *p;
3576+ struct extent_buffer *existing_eb = NULL;
34823577 struct address_space *mapping = fs_info->btree_inode->i_mapping;
34833578 struct btrfs_subpage *prealloc = NULL;
34843579 u64 lockdep_owner = owner_root;
3580+ bool page_contig = true;
34853581 int uptodate = 1;
34863582 int ret;
34873583
@@ -3516,10 +3612,8 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, · 28 unmodified lines
35163612
35173613 btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level);
35183614
3519− num_pages = num_extent_pages(eb);
3520−
35213615 /*
3522− * Preallocate page->private for subpage case, so that we won't
3616+ * Preallocate folio private for subpage case, so that we won't
35233617 * allocate memory with i_private_lock nor page lock hold.
35243618 *
35253619 * 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
35283622 if (fs_info->nodesize < PAGE_SIZE) {
35293623 prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA);
35303624 if (IS_ERR(prealloc)) {
3531− exists = ERR_CAST(prealloc);
3532− goto free_eb;
3625+ ret = PTR_ERR(prealloc);
3626+ goto out;
35333627 }
35343628 }
35353629
3536− for (i = 0; i < num_pages; i++, index++) {
3537− p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
3538− if (!p) {
3539− exists = ERR_PTR(-ENOMEM);
3540− btrfs_free_subpage(prealloc);
3541− goto free_eb;
3630+reallocate:
3631+ /* Allocate all pages first. */
3632+ ret = alloc_eb_folio_array(eb, __GFP_NOFAIL);
3633+ if (ret < 0) {
3634+ btrfs_free_subpage(prealloc);
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;
35423647 }
35433648
3544− spin_lock(&mapping->i_private_lock);
3545− exists = grab_extent_buffer(fs_info, p);
3546− if (exists) {
3547− spin_unlock(&mapping->i_private_lock);
3548− unlock_page(p);
3549− put_page(p);
3550− mark_extent_buffer_accessed(exists, p);
3551− btrfs_free_subpage(prealloc);
3552− goto free_eb;
3649+ /*
3650+ * TODO: Special handling for a corner case where the order of
3651+ * folios mismatch between the new eb and filemap.
3652+ *
3653+ * This happens when:
3654+ *
3655+ * - the new eb is using higher order folio
3656+ *
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;
35533671 }
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);
35543681 /* 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);
35563683 ASSERT(!ret);
35573684 /*
35583685 * To inform we have extra eb under allocation, so that
3559− * detach_extent_buffer_page() won't release the page private
3686+ * detach_extent_buffer_page() won't release the folio private
35603687 * when the eb hasn't yet been inserted into radix tree.
35613688 *
35623689 * The ref will be decreased when the eb released the page, in
35633690 * detach_extent_buffer_page().
35643691 * Thus needs no special handling in error path.
35653692 */
3566− btrfs_page_inc_eb_refs(fs_info, p);
3693+ btrfs_folio_inc_eb_refs(fs_info, folio);
35673694 spin_unlock(&mapping->i_private_lock);
35683695
3569− WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len));
3570− eb->pages[i] = p;
3571− if (!btrfs_page_test_uptodate(fs_info, p, eb->start, eb->len))
3696+ WARN_ON(btrfs_folio_test_dirty(fs_info, folio, eb->start, eb->len));
3697+
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))
35723708 uptodate = 0;
35733709
35743710 /*
@@ -3581,12 +3717,13 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, · 6 unmodified lines
35813717 }
35823718 if (uptodate)
35833719 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);
35843723 again:
35853724 ret = radix_tree_preload(GFP_NOFS);
3586− if (ret) {
3587− exists = ERR_PTR(ret);
3588− goto free_eb;
3589− }
3725+ if (ret)
3726+ goto out;
35903727
35913728 spin_lock(&fs_info->buffer_lock);
35923729 ret = radix_tree_insert(&fs_info->buffer_radix,
@@ -3594,9 +3731,10 @@ again: · 1 unmodified line
35943731 spin_unlock(&fs_info->buffer_lock);
35953732 radix_tree_preload_end();
35963733 if (ret == -EEXIST) {
3597− exists = find_extent_buffer(fs_info, start);
3598− if (exists)
3599− goto free_eb;
3734+ ret = 0;
3735+ existing_eb = find_extent_buffer(fs_info, start);
3736+ if (existing_eb)
3737+ goto out;
36003738 else
36013739 goto again;
36023740 }
@@ -3609,19 +3747,46 @@ again: · 6 unmodified lines
36093747 * btree_release_folio will correctly detect that a page belongs to a
36103748 * live buffer and won't free them prematurely.
36113749 */
3612− for (i = 0; i < num_pages; i++)
3613− unlock_page(eb->pages[i]);
3750+ for (int i = 0; i < num_folios; i++)
3751+ unlock_page(folio_page(eb->folios[i], 0));
36143752 return eb;
36153753
3616−free_eb:
3754+out:
36173755 WARN_ON(!atomic_dec_and_test(&eb->refs));
3618− for (i = 0; i < num_pages; i++) {
3619− if (eb->pages[i])
3620− unlock_page(eb->pages[i]);
3756+
3757+ /*
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;
36213778 }
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);
36223784
36233785 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;
36253790 }
36263791
36273792 static 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
37133878 release_extent_buffer(eb);
37143879 }
37153880
3716−static void btree_clear_page_dirty(struct page *page)
3881+static void btree_clear_folio_dirty(struct folio *folio)
37173882 {
3718− ASSERT(PageDirty(page));
3719− ASSERT(PageLocked(page));
3720− clear_page_dirty_for_io(page);
3721− xa_lock_irq(&page->mapping->i_pages);
3722− if (!PageDirty(page))
3723− __xa_clear_mark(&page->mapping->i_pages,
3724− page_index(page), PAGECACHE_TAG_DIRTY);
3725− xa_unlock_irq(&page->mapping->i_pages);
3883+ ASSERT(folio_test_dirty(folio));
3884+ ASSERT(folio_test_locked(folio));
3885+ folio_clear_dirty_for_io(folio);
3886+ xa_lock_irq(&folio->mapping->i_pages);
3887+ if (!folio_test_dirty(folio))
3888+ __xa_clear_mark(&folio->mapping->i_pages,
3889+ folio_index(folio), PAGECACHE_TAG_DIRTY);
3890+ xa_unlock_irq(&folio->mapping->i_pages);
37263891 }
37273892
37283893 static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb)
37293894 {
37303895 struct btrfs_fs_info *fs_info = eb->fs_info;
3731− struct page *page = eb->pages[0];
3896+ struct folio *folio = eb->folios[0];
37323897 bool last;
37333898
3734− /* btree_clear_page_dirty() needs page locked */
3735− lock_page(page);
3736− last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start,
3737− eb->len);
3899+ /* btree_clear_folio_dirty() needs page locked. */
3900+ folio_lock(folio);
3901+ last = btrfs_subpage_clear_and_test_dirty(fs_info, folio, eb->start, eb->len);
37383902 if (last)
3739− btree_clear_page_dirty(page);
3740− unlock_page(page);
3903+ btree_clear_folio_dirty(folio);
3904+ folio_unlock(folio);
37413905 WARN_ON(atomic_read(&eb->refs) == 0);
37423906 }
37433907
@@ -3745,15 +3909,27 @@ void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, · 1 unmodified line
37453909 struct extent_buffer *eb)
37463910 {
37473911 struct btrfs_fs_info *fs_info = eb->fs_info;
3748− int i;
3749− int num_pages;
3750− struct page *page;
3912+ int num_folios;
37513913
37523914 btrfs_assert_tree_write_locked(eb);
37533915
37543916 if (trans && btrfs_header_generation(eb) != trans->transid)
37553917 return;
37563918
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+
37573933 if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags))
37583934 return;
37593935
@@ -3763,30 +3939,29 @@ void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans, · 3 unmodified lines
37633939 if (eb->fs_info->nodesize < PAGE_SIZE)
37643940 return clear_subpage_extent_buffer_dirty(eb);
37653941
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];
37673945
3768− for (i = 0; i < num_pages; i++) {
3769− page = eb->pages[i];
3770− if (!PageDirty(page))
3946+ if (!folio_test_dirty(folio))
37713947 continue;
3772− lock_page(page);
3773− btree_clear_page_dirty(page);
3774− unlock_page(page);
3948+ folio_lock(folio);
3949+ btree_clear_folio_dirty(folio);
3950+ folio_unlock(folio);
37753951 }
37763952 WARN_ON(atomic_read(&eb->refs) == 0);
37773953 }
37783954
37793955 void set_extent_buffer_dirty(struct extent_buffer *eb)
37803956 {
3781− int i;
3782− int num_pages;
3957+ int num_folios;
37833958 bool was_dirty;
37843959
37853960 check_buffer_tree_ref(eb);
37863961
37873962 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
37883963
3789− num_pages = num_extent_pages(eb);
3964+ num_folios = num_extent_folios(eb);
37903965 WARN_ON(atomic_read(&eb->refs) == 0);
37913966 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
37923967
@@ -3805,34 +3980,32 @@ void set_extent_buffer_dirty(struct extent_buffer *eb) · 12 unmodified lines
38053980 * the above race.
38063981 */
38073982 if (subpage)
3808− lock_page(eb->pages[0]);
3809− for (i = 0; i < num_pages; i++)
3810− btrfs_page_set_dirty(eb->fs_info, eb->pages[i],
3811− eb->start, eb->len);
3983+ lock_page(folio_page(eb->folios[0], 0));
3984+ for (int i = 0; i < num_folios; i++)
3985+ btrfs_folio_set_dirty(eb->fs_info, eb->folios[i],
3986+ eb->start, eb->len);
38123987 if (subpage)
3813− unlock_page(eb->pages[0]);
3988+ unlock_page(folio_page(eb->folios[0], 0));
38143989 percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes,
38153990 eb->len,
38163991 eb->fs_info->dirty_metadata_batch);
38173992 }
38183993 #ifdef CONFIG_BTRFS_DEBUG
3819− for (i = 0; i < num_pages; i++)
3820− ASSERT(PageDirty(eb->pages[i]));
3994+ for (int i = 0; i < num_folios; i++)
3995+ ASSERT(folio_test_dirty(eb->folios[i]));
38213996 #endif
38223997 }
38233998
38243999 void clear_extent_buffer_uptodate(struct extent_buffer *eb)
38254000 {
38264001 struct btrfs_fs_info *fs_info = eb->fs_info;
3827− struct page *page;
3828− int num_pages;
3829− int i;
4002+ int num_folios = num_extent_folios(eb);
38304003
38314004 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3832− num_pages = num_extent_pages(eb);
3833− for (i = 0; i < num_pages; i++) {
3834− page = eb->pages[i];
3835− if (!page)
4005+ for (int i = 0; i < num_folios; i++) {
4006+ struct folio *folio = eb->folios[i];
4007+
4008+ if (!folio)
38364009 continue;
38374010
38384011 /*
@@ -3840,44 +4013,40 @@ void clear_extent_buffer_uptodate(struct extent_buffer *eb) · 1 unmodified line
38404013 * btrfs_is_subpage() can not handle cloned/dummy metadata.
38414014 */
38424015 if (fs_info->nodesize >= PAGE_SIZE)
3843− ClearPageUptodate(page);
4016+ folio_clear_uptodate(folio);
38444017 else
3845− btrfs_subpage_clear_uptodate(fs_info, page, eb->start,
3846− eb->len);
4018+ btrfs_subpage_clear_uptodate(fs_info, folio,
4019+ eb->start, eb->len);
38474020 }
38484021 }
38494022
38504023 void set_extent_buffer_uptodate(struct extent_buffer *eb)
38514024 {
38524025 struct btrfs_fs_info *fs_info = eb->fs_info;
3853− struct page *page;
3854− int num_pages;
3855− int i;
4026+ int num_folios = num_extent_folios(eb);
38564027
38574028 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3858− num_pages = num_extent_pages(eb);
3859− for (i = 0; i < num_pages; i++) {
3860− page = eb->pages[i];
4029+ for (int i = 0; i < num_folios; i++) {
4030+ struct folio *folio = eb->folios[i];
38614031
38624032 /*
38634033 * This is special handling for metadata subpage, as regular
38644034 * btrfs_is_subpage() can not handle cloned/dummy metadata.
38654035 */
38664036 if (fs_info->nodesize >= PAGE_SIZE)
3867− SetPageUptodate(page);
4037+ folio_mark_uptodate(folio);
38684038 else
3869− btrfs_subpage_set_uptodate(fs_info, page, eb->start,
3870− eb->len);
4039+ btrfs_subpage_set_uptodate(fs_info, folio,
4040+ eb->start, eb->len);
38714041 }
38724042 }
38734043
3874−static void extent_buffer_read_end_io(struct btrfs_bio *bbio)
4044+static void end_bbio_meta_read(struct btrfs_bio *bbio)
38754045 {
38764046 struct extent_buffer *eb = bbio->private;
38774047 struct btrfs_fs_info *fs_info = eb->fs_info;
38784048 bool uptodate = !bbio->bio.bi_status;
3879− struct bvec_iter_all iter_all;
3880− struct bio_vec *bvec;
4049+ struct folio_iter fi;
38814050 u32 bio_offset = 0;
38824051
38834052 eb->read_mirror = bbio->mirror_num;
@@ -3893,15 +4062,15 @@ static void extent_buffer_read_end_io(struct btrfs_bio *bbio) · 9 unmodified lines
38934062 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
38944063 }
38954064
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;
38974067 u64 start = eb->start + bio_offset;
3898− struct page *page = bvec->bv_page;
3899− u32 len = bvec->bv_len;
4068+ u32 len = fi.length;
39004069
39014070 if (uptodate)
3902− btrfs_page_set_uptodate(fs_info, page, start, len);
4071+ btrfs_folio_set_uptodate(fs_info, folio, start, len);
39034072 else
3904− btrfs_page_clear_uptodate(fs_info, page, start, len);
4073+ btrfs_folio_clear_uptodate(fs_info, folio, start, len);
39054074
39064075 bio_offset += len;
39074076 }
@@ -3917,8 +4086,8 @@ static void extent_buffer_read_end_io(struct btrfs_bio *bbio) · 9 unmodified lines
39174086 int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num,
39184087 struct btrfs_tree_parent_check *check)
39194088 {
3920− int num_pages = num_extent_pages(eb), i;
39214089 struct btrfs_bio *bbio;
4090+ bool ret;
39224091
39234092 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
39244093 return 0;
@@ -3942,17 +4111,24 @@ int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num, · 17 unmodified lines
39424111
39434112 bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
39444113 REQ_OP_READ | REQ_META, eb->fs_info,
3945− extent_buffer_read_end_io, eb);
4114+ end_bbio_meta_read, eb);
39464115 bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
39474116 bbio->inode = BTRFS_I(eb->fs_info->btree_inode);
39484117 bbio->file_offset = eb->start;
39494118 memcpy(&bbio->parent_check, check, sizeof(*check));
39504119 if (eb->fs_info->nodesize < PAGE_SIZE) {
3951− __bio_add_page(&bbio->bio, eb->pages[0], eb->len,
3952− eb->start - page_offset(eb->pages[0]));
4120+ ret = bio_add_folio(&bbio->bio, eb->folios[0], eb->len,
4121+ eb->start - folio_pos(eb->folios[0]));
4122+ ASSERT(ret);
39534123 } else {
3954− for (i = 0; i < num_pages; i++)
3955− __bio_add_page(&bbio->bio, eb->pages[i], PAGE_SIZE, 0);
4124+ int num_folios = num_extent_folios(eb);
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+ }
39564132 }
39574133 btrfs_submit_bio(bbio, mirror_num);
39584134
@@ -3999,29 +4175,33 @@ static inline int check_eb_range(const struct extent_buffer *eb, · 40 unmodified lines
39994175 void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
40004176 unsigned long start, unsigned long len)
40014177 {
4178+ const int unit_size = folio_size(eb->folios[0]);
40024179 size_t cur;
40034180 size_t offset;
4004− struct page *page;
4005− char *kaddr;
40064181 char *dst = (char *)dstv;
4007− unsigned long i = get_eb_page_index(start);
4182+ unsigned long i = get_eb_folio_index(eb, start);
40084183
40094184 if (check_eb_range(eb, start, len)) {
40104185 /*
40114186 * 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.
40134188 */
40144189 memset(dstv, 0, len);
40154190 return;
40164191 }
40174192
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);
40194199
40204200 while (len > 0) {
4021− page = eb->pages[i];
4201+ char *kaddr;
40224202
4023− cur = min(len, (PAGE_SIZE - offset));
4024− kaddr = page_address(page);
4203+ cur = min(len, unit_size - offset);
4204+ kaddr = folio_address(eb->folios[i]);
40254205 memcpy(dst, kaddr + offset, cur);
40264206
40274207 dst += cur;
@@ -4035,24 +4215,29 @@ int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, · 7 unmodified lines
40354215 void __user *dstv,
40364216 unsigned long start, unsigned long len)
40374217 {
4218+ const int unit_size = folio_size(eb->folios[0]);
40384219 size_t cur;
40394220 size_t offset;
4040− struct page *page;
4041− char *kaddr;
40424221 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);
40444223 int ret = 0;
40454224
40464225 WARN_ON(start > eb->len);
40474226 WARN_ON(start + len > eb->start + eb->len);
40484227
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);
40504235
40514236 while (len > 0) {
4052− page = eb->pages[i];
4237+ char *kaddr;
40534238
4054− cur = min(len, (PAGE_SIZE - offset));
4055− kaddr = page_address(page);
4239+ cur = min(len, unit_size - offset);
4240+ kaddr = folio_address(eb->folios[i]);
40564241 if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
40574242 ret = -EFAULT;
40584243 break;
@@ -4070,25 +4255,25 @@ int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, · 11 unmodified lines
40704255 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
40714256 unsigned long start, unsigned long len)
40724257 {
4258+ const int unit_size = folio_size(eb->folios[0]);
40734259 size_t cur;
40744260 size_t offset;
4075− struct page *page;
40764261 char *kaddr;
40774262 char *ptr = (char *)ptrv;
4078− unsigned long i = get_eb_page_index(start);
4263+ unsigned long i = get_eb_folio_index(eb, start);
40794264 int ret = 0;
40804265
40814266 if (check_eb_range(eb, start, len))
40824267 return -EINVAL;
40834268
4084− offset = get_eb_offset_in_page(eb, start);
4269+ if (eb->addr)
4270+ return memcmp(ptrv, eb->addr + start, len);
40854271
4086− while (len > 0) {
4087− page = eb->pages[i];
4272+ offset = get_eb_offset_in_folio(eb, start);
40884273
4089− cur = min(len, (PAGE_SIZE - offset));
4090−
4091− kaddr = page_address(page);
4274+ while (len > 0) {
4275+ cur = min(len, unit_size - offset);
4276+ kaddr = folio_address(eb->folios[i]);
40924277 ret = memcmp(ptr, kaddr + offset, cur);
40934278 if (ret)
40944279 break;
@@ -4107,10 +4292,12 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, · 12 unmodified lines
41074292 * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
41084293 * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
41094294 */
4110−static void assert_eb_page_uptodate(const struct extent_buffer *eb,
4111− struct page *page)
4295+static void assert_eb_folio_uptodate(const struct extent_buffer *eb, int i)
41124296 {
41134297 struct btrfs_fs_info *fs_info = eb->fs_info;
4298+ struct folio *folio = eb->folios[i];
4299+
4300+ ASSERT(folio);
41144301
41154302 /*
41164303 * 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
41244311 return;
41254312
41264313 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,
41284318 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);
41304320 } else {
4131− WARN_ON(!PageUptodate(page));
4321+ WARN_ON(!folio_test_uptodate(folio));
41324322 }
41334323 }
41344324
@@ -4136,29 +4326,34 @@ static void __write_extent_buffer(const struct extent_buffer *eb, · 1 unmodified line
41364326 const void *srcv, unsigned long start,
41374327 unsigned long len, bool use_memmove)
41384328 {
4329+ const int unit_size = folio_size(eb->folios[0]);
41394330 size_t cur;
41404331 size_t offset;
4141− struct page *page;
41424332 char *kaddr;
41434333 char *src = (char *)srcv;
4144− unsigned long i = get_eb_page_index(start);
4334+ unsigned long i = get_eb_folio_index(eb, start);
41454335 /* For unmapped (dummy) ebs, no need to check their uptodate status. */
41464336 const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
41474337
4148− WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags));
4149−
41504338 if (check_eb_range(eb, start, len))
41514339 return;
41524340
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);
41544350
41554351 while (len > 0) {
4156− page = eb->pages[i];
41574352 if (check_uptodate)
4158− assert_eb_page_uptodate(eb, page);
4353+ assert_eb_folio_uptodate(eb, i);
41594354
4160− cur = min(len, PAGE_SIZE - offset);
4161− kaddr = page_address(page);
4355+ cur = min(len, unit_size - offset);
4356+ kaddr = folio_address(eb->folios[i]);
41624357 if (use_memmove)
41634358 memmove(kaddr + offset, src, cur);
41644359 else
@@ -4180,16 +4375,21 @@ void write_extent_buffer(const struct extent_buffer *eb, const void *srcv, · 15 unmodified lines
41804375 static void memset_extent_buffer(const struct extent_buffer *eb, int c,
41814376 unsigned long start, unsigned long len)
41824377 {
4378+ const int unit_size = folio_size(eb->folios[0]);
41834379 unsigned long cur = start;
41844380
4381+ if (eb->addr) {
4382+ memset(eb->addr + start, c, len);
4383+ return;
4384+ }
4385+
41854386 while (cur < start + len) {
4186− unsigned long index = get_eb_page_index(cur);
4187− unsigned int offset = get_eb_offset_in_page(eb, cur);
4188− unsigned int cur_len = min(start + len - cur, PAGE_SIZE - offset);
4189− struct page *page = eb->pages[index];
4387+ unsigned long index = get_eb_folio_index(eb, cur);
4388+ unsigned int offset = get_eb_offset_in_folio(eb, cur);
4389+ unsigned int cur_len = min(start + len - cur, unit_size - offset);
41904390
4191− assert_eb_page_uptodate(eb, page);
4192− memset(page_address(page) + offset, c, cur_len);
4391+ assert_eb_folio_uptodate(eb, index);
4392+ memset(folio_address(eb->folios[index]) + offset, c, cur_len);
41934393
41944394 cur += cur_len;
41954395 }
@@ -4206,15 +4406,16 @@ void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, · 10 unmodified lines
42064406 void copy_extent_buffer_full(const struct extent_buffer *dst,
42074407 const struct extent_buffer *src)
42084408 {
4409+ const int unit_size = folio_size(src->folios[0]);
42094410 unsigned long cur = 0;
42104411
42114412 ASSERT(dst->len == src->len);
42124413
42134414 while (cur < src->len) {
4214− unsigned long index = get_eb_page_index(cur);
4215− unsigned long offset = get_eb_offset_in_page(src, cur);
4216− unsigned long cur_len = min(src->len, PAGE_SIZE - offset);
4217− void *addr = page_address(src->pages[index]) + offset;
4415+ unsigned long index = get_eb_folio_index(src, cur);
4416+ unsigned long offset = get_eb_offset_in_folio(src, cur);
4417+ unsigned long cur_len = min(src->len, unit_size - offset);
4418+ void *addr = folio_address(src->folios[index]) + offset;
42184419
42194420 write_extent_buffer(dst, addr, cur, cur_len);
42204421
@@ -4227,12 +4428,12 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 6 unmodified lines
42274428 unsigned long dst_offset, unsigned long src_offset,
42284429 unsigned long len)
42294430 {
4431+ const int unit_size = folio_size(dst->folios[0]);
42304432 u64 dst_len = dst->len;
42314433 size_t cur;
42324434 size_t offset;
4233− struct page *page;
42344435 char *kaddr;
4235− unsigned long i = get_eb_page_index(dst_offset);
4436+ unsigned long i = get_eb_folio_index(dst, dst_offset);
42364437
42374438 if (check_eb_range(dst, dst_offset, len) ||
42384439 check_eb_range(src, src_offset, len))
@@ -4240,15 +4441,14 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 1 unmodified line
42404441
42414442 WARN_ON(src->len != dst_len);
42424443
4243− offset = get_eb_offset_in_page(dst, dst_offset);
4444+ offset = get_eb_offset_in_folio(dst, dst_offset);
42444445
42454446 while (len > 0) {
4246− page = dst->pages[i];
4247− assert_eb_page_uptodate(dst, page);
4447+ assert_eb_folio_uptodate(dst, i);
42484448
4249− cur = min(len, (unsigned long)(PAGE_SIZE - offset));
4449+ cur = min(len, (unsigned long)(unit_size - offset));
42504450
4251− kaddr = page_address(page);
4451+ kaddr = folio_address(dst->folios[i]);
42524452 read_extent_buffer(src, kaddr + offset, src_offset, cur);
42534453
42544454 src_offset += cur;
@@ -4259,22 +4459,22 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 4 unmodified lines
42594459 }
42604460
42614461 /*
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.
42634463 *
42644464 * @eb: the extent buffer
42654465 * @start: offset of the bitmap item in the extent buffer
42664466 * @nr: bit number
4267− * @page_index: return index of the page in the extent buffer that contains
4467+ * @folio_index: return index of the folio in the extent buffer that contains
42684468 * the given bit number
4269− * @page_offset: return offset into the page given by page_index
4469+ * @folio_offset: return offset into the folio given by folio_index
42704470 *
42714471 * This helper hides the ugliness of finding the byte in an extent buffer which
42724472 * contains a given bit.
42734473 */
42744474 static inline void eb_bitmap_offset(const struct extent_buffer *eb,
42754475 unsigned long start, unsigned long nr,
4276− unsigned long *page_index,
4277− size_t *page_offset)
4476+ unsigned long *folio_index,
4477+ size_t *folio_offset)
42784478 {
42794479 size_t byte_offset = BIT_BYTE(nr);
42804480 size_t offset;
@@ -4284,10 +4484,10 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb, · 3 unmodified lines
42844484 * the bitmap item in the extent buffer + the offset of the byte in the
42854485 * bitmap item.
42864486 */
4287− offset = start + offset_in_page(eb->start) + byte_offset;
4487+ offset = start + offset_in_folio(eb->folios[0], eb->start) + byte_offset;
42884488
4289− *page_index = offset >> PAGE_SHIFT;
4290− *page_offset = offset_in_page(offset);
4489+ *folio_index = offset >> folio_shift(eb->folios[0]);
4490+ *folio_offset = offset_in_folio(eb->folios[0], offset);
42914491 }
42924492
42934493 /*
@@ -4300,25 +4500,23 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb, · 6 unmodified lines
43004500 int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
43014501 unsigned long nr)
43024502 {
4303− u8 *kaddr;
4304− struct page *page;
43054503 unsigned long i;
43064504 size_t offset;
4505+ u8 *kaddr;
43074506
43084507 eb_bitmap_offset(eb, start, nr, &i, &offset);
4309− page = eb->pages[i];
4310− assert_eb_page_uptodate(eb, page);
4311− kaddr = page_address(page);
4508+ assert_eb_folio_uptodate(eb, i);
4509+ kaddr = folio_address(eb->folios[i]);
43124510 return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
43134511 }
43144512
43154513 static u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr)
43164514 {
4317− unsigned long index = get_eb_page_index(bytenr);
4515+ unsigned long index = get_eb_folio_index(eb, bytenr);
43184516
43194517 if (check_eb_range(eb, bytenr, 1))
43204518 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);
43224520 }
43234521
43244522 /*
@@ -4403,19 +4601,30 @@ void memcpy_extent_buffer(const struct extent_buffer *dst, · 78 unmodified lines
44034601 unsigned long dst_offset, unsigned long src_offset,
44044602 unsigned long len)
44054603 {
4604+ const int unit_size = folio_size(dst->folios[0]);
44064605 unsigned long cur_off = 0;
44074606
44084607 if (check_eb_range(dst, dst_offset, len) ||
44094608 check_eb_range(dst, src_offset, len))
44104609 return;
44114610
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+
44124621 while (cur_off < len) {
44134622 unsigned long cur_src = cur_off + src_offset;
4414− unsigned long pg_index = get_eb_page_index(cur_src);
4415− unsigned long pg_off = get_eb_offset_in_page(dst, cur_src);
4623+ unsigned long folio_index = get_eb_folio_index(dst, cur_src);
4624+ unsigned long folio_off = get_eb_offset_in_folio(dst, cur_src);
44164625 unsigned long cur_len = min(src_offset + len - cur_src,
4417− PAGE_SIZE - pg_off);
4418− void *src_addr = page_address(dst->pages[pg_index]) + pg_off;
4626+ unit_size - folio_off);
4627+ void *src_addr = folio_address(dst->folios[folio_index]) + folio_off;
44194628 const bool use_memmove = areas_overlap(src_offset + cur_off,
44204629 dst_offset + cur_off, cur_len);
44214630
@@ -4441,24 +4650,29 @@ void memmove_extent_buffer(const struct extent_buffer *dst, · 19 unmodified lines
44414650 return;
44424651 }
44434652
4653+ if (dst->addr) {
4654+ memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
4655+ return;
4656+ }
4657+
44444658 while (len > 0) {
44454659 unsigned long src_i;
44464660 size_t cur;
4447− size_t dst_off_in_page;
4448− size_t src_off_in_page;
4661+ size_t dst_off_in_folio;
4662+ size_t src_off_in_folio;
44494663 void *src_addr;
44504664 bool use_memmove;
44514665
4452− src_i = get_eb_page_index(src_end);
4666+ src_i = get_eb_folio_index(dst, src_end);
44534667
4454− dst_off_in_page = get_eb_offset_in_page(dst, dst_end);
4455− src_off_in_page = get_eb_offset_in_page(dst, src_end);
4668+ dst_off_in_folio = get_eb_offset_in_folio(dst, dst_end);
4669+ src_off_in_folio = get_eb_offset_in_folio(dst, src_end);
44564670
4457− cur = min_t(unsigned long, len, src_off_in_page + 1);
4458− cur = min(cur, dst_off_in_page + 1);
4671+ cur = min_t(unsigned long, len, src_off_in_folio + 1);
4672+ cur = min(cur, dst_off_in_folio + 1);
44594673
4460− src_addr = page_address(dst->pages[src_i]) + src_off_in_page -
4461− cur + 1;
4674+ src_addr = folio_address(dst->folios[src_i]) + src_off_in_folio -
4675+ cur + 1;
44624676 use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1,
44634677 cur);
44644678
@@ -4520,7 +4734,7 @@ static int try_release_subpage_extent_buffer(struct page *page) · 55 unmodified lines
45204734 struct extent_buffer *eb = NULL;
45214735
45224736 /*
4523− * Unlike try_release_extent_buffer() which uses page->private
4737+ * Unlike try_release_extent_buffer() which uses folio private
45244738 * to grab buffer, for subpage case we rely on radix tree, thus
45254739 * we need to ensure radix tree consistency.
45264740 *
@@ -4560,17 +4774,17 @@ static int try_release_subpage_extent_buffer(struct page *page) · 33 unmodified lines
45604774
45614775 /*
45624776 * Here we don't care about the return value, we will always
4563− * check the page private at the end. And
4777+ * check the folio private at the end. And
45644778 * release_extent_buffer() will release the refs_lock.
45654779 */
45664780 release_extent_buffer(eb);
45674781 }
45684782 /*
4569− * Finally to check if we have cleared page private, as if we have
4570− * released all ebs in the page, the page private should be cleared now.
4783+ * Finally to check if we have cleared folio private, as if we have
4784+ * released all ebs in the page, the folio private should be cleared now.
45714785 */
45724786 spin_lock(&page->mapping->i_private_lock);
4573− if (!PagePrivate(page))
4787+ if (!folio_test_private(page_folio(page)))
45744788 ret = 1;
45754789 else
45764790 ret = 0;
@@ -4581,22 +4795,23 @@ static int try_release_subpage_extent_buffer(struct page *page) · 4 unmodified lines
45814795
45824796 int try_release_extent_buffer(struct page *page)
45834797 {
4798+ struct folio *folio = page_folio(page);
45844799 struct extent_buffer *eb;
45854800
45864801 if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE)
45874802 return try_release_subpage_extent_buffer(page);
45884803
45894804 /*
4590− * We need to make sure nobody is changing page->private, as we rely on
4591− * page->private as the pointer to extent buffer.
4805+ * We need to make sure nobody is changing folio private, as we rely on
4806+ * folio private as the pointer to extent buffer.
45924807 */
45934808 spin_lock(&page->mapping->i_private_lock);
4594− if (!PagePrivate(page)) {
4809+ if (!folio_test_private(folio)) {
45954810 spin_unlock(&page->mapping->i_private_lock);
45964811 return 1;
45974812 }
45984813
4599− eb = (struct extent_buffer *)page->private;
4814+ eb = folio_get_private(folio);
46004815 BUG_ON(!eb);
46014816
46024817 /*
fs/btrfs/extent_io.h+61−19
@@ -28,7 +28,8 @@ enum { · 27 unmodified lines
2828 EXTENT_BUFFER_IN_TREE,
2929 /* write IO error */
3030 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,
3233 /* Indicate that extent buffer pages a being read */
3334 EXTENT_BUFFER_READING,
3435 };
@@ -43,10 +44,10 @@ enum { · 8 unmodified lines
4344 };
4445
4546 /*
46− * page->private values. Every page that is controlled by the extent
47− * map has page->private set to one.
47+ * Folio private values. Every page that is controlled by the extent map has
48+ * folio private set to this value.
4849 */
49−#define EXTENT_PAGE_PRIVATE 1
50+#define EXTENT_FOLIO_PRIVATE 1
5051
5152 /*
5253 * The extent buffer bitmap operations are done with byte granularity instead of
@@ -77,6 +78,13 @@ struct extent_buffer { · 24 unmodified lines
7778 unsigned long len;
7879 unsigned long bflags;
7980 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+
8088 spinlock_t refs_lock;
8189 atomic_t refs;
8290 int read_mirror;
@@ -86,7 +94,12 @@ struct extent_buffer { · 3 unmodified lines
8694
8795 struct rw_semaphore lock;
8896
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];
90103 #ifdef CONFIG_BTRFS_DEBUG
91104 struct list_head leak_list;
92105 pid_t lock_owner;
@@ -108,29 +121,43 @@ struct btrfs_eb_write_context { · 15 unmodified lines
108121 *
109122 * Will handle both sectorsize == PAGE_SIZE and sectorsize < PAGE_SIZE cases.
110123 */
111−static inline size_t get_eb_offset_in_page(const struct extent_buffer *eb,
112− unsigned long offset)
124+static inline size_t get_eb_offset_in_folio(const struct extent_buffer *eb,
125+ unsigned long offset)
113126 {
114127 /*
115− * For sectorsize == PAGE_SIZE case, eb->start will always be aligned
116− * to PAGE_SIZE, thus adding it won't cause any difference.
128+ * 1) sectorsize == PAGE_SIZE and nodesize >= PAGE_SIZE case
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.
117135 *
118− * For sectorsize < PAGE_SIZE, we must only read the data that belongs
119− * to the eb, thus we have to take the eb->start into consideration.
136+ * 2) sectorsize < PAGE_SIZE and nodesize < PAGE_SIZE case
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.
120141 */
121− return offset_in_page(offset + eb->start);
142+ return offset_in_folio(eb->folios[0], offset + eb->start);
122143 }
123144
124−static inline unsigned long get_eb_page_index(unsigned long offset)
145+static inline unsigned long get_eb_folio_index(const struct extent_buffer *eb,
146+ unsigned long offset)
125147 {
126148 /*
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.
128156 *
129− * For sectorsize < PAGE_SIZE case, we only support 64K PAGE_SIZE,
130− * and have ensured that all tree blocks are contained in one page,
131− * thus we always get index == 0.
157+ * 2) sectorsize < PAGE_SIZE and nodesize < PAGE_SIZE case
158+ * The folio would only be page sized, and always give us 0 as index.
132159 */
133− return offset >> PAGE_SHIFT;
160+ return offset >> folio_shift(eb->folios[0]);
134161 }
135162
136163 /*
@@ -230,6 +257,20 @@ static inline int num_extent_pages(const struct extent_buffer *eb) · 93 unmodified lines
230257 return (eb->len >> PAGE_SHIFT) ?: 1;
231258 }
232259
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+ */
267+static 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+
233274 static inline int extent_buffer_uptodate(const struct extent_buffer *eb)
234275 {
235276 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
@@ -294,7 +335,8 @@ int extent_invalidate_folio(struct extent_io_tree *tree, · 58 unmodified lines
294335 void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
295336 struct extent_buffer *buf);
296337
297−int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array);
338+int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
339+ gfp_t extra_gfp);
298340
299341 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
300342 bool 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
5050 if (!em)
5151 return NULL;
5252 RB_CLEAR_NODE(&em->rb_node);
53− em->compress_type = BTRFS_COMPRESS_NONE;
5453 refcount_set(&em->refs, 1);
5554 INIT_LIST_HEAD(&em->list);
5655 return em;
@@ -67,8 +66,6 @@ void free_extent_map(struct extent_map *em) · 10 unmodified lines
6766 if (refcount_dec_and_test(&em->refs)) {
6867 WARN_ON(extent_map_in_tree(em));
6968 WARN_ON(!list_empty(&em->list));
70− if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
71− kfree(em->map_lookup);
7269 kmem_cache_free(extent_map_cache, em);
7370 }
7471 }
@@ -182,50 +179,50 @@ static struct rb_node *__tree_search(struct rb_root *root, u64 offset, · 107 unmodified lines
182179 return NULL;
183180 }
184181
185−/* Check to see if two extent_map structs are adjacent and safe to merge. */
186−static int mergable_maps(struct extent_map *prev, struct extent_map *next)
182+static inline u64 extent_map_block_end(const struct extent_map *em)
187183 {
188− if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
189− return 0;
184+ if (em->block_start + em->block_len < em->block_start)
185+ return (u64)-1;
186+ return em->block_start + em->block_len;
187+}
190188
191− /*
192− * don't merge compressed extents, we need to know their
193− * actual size
194− */
195− if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
196− return 0;
189+static bool can_merge_extent_map(const struct extent_map *em)
190+{
191+ if (em->flags & EXTENT_FLAG_PINNED)
192+ return false;
193+
194+ /* Don't merge compressed extents, we need to know their actual size. */
195+ if (extent_map_is_compressed(em))
196+ return false;
197197
198− if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
199− test_bit(EXTENT_FLAG_LOGGING, &next->flags))
200− return 0;
198+ if (em->flags & EXTENT_FLAG_LOGGING)
199+ return false;
201200
202201 /*
203202 * We don't want to merge stuff that hasn't been written to the log yet
204203 * since it may not reflect exactly what is on disk, and that would be
205204 * bad.
206205 */
207− if (!list_empty(&prev->list) || !list_empty(&next->list))
208− return 0;
209−
210− ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
211− prev->block_start != EXTENT_MAP_DELALLOC);
212−
213− if (prev->map_lookup || next->map_lookup)
214− ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
215− test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
216−
217− if (extent_map_end(prev) == next->start &&
218− prev->flags == next->flags &&
219− prev->map_lookup == next->map_lookup &&
220− ((next->block_start == EXTENT_MAP_HOLE &&
221− prev->block_start == EXTENT_MAP_HOLE) ||
222− (next->block_start == EXTENT_MAP_INLINE &&
223− prev->block_start == EXTENT_MAP_INLINE) ||
224− (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
225− next->block_start == extent_map_block_end(prev)))) {
226− return 1;
227− }
228− return 0;
206+ if (!list_empty(&em->list))
207+ return false;
208+
209+ return true;
210+}
211+
212+/* Check to see if two extent_map structs are adjacent and safe to merge. */
213+static bool mergeable_maps(const struct extent_map *prev, const struct extent_map *next)
214+{
215+ if (extent_map_end(prev) != next->start)
216+ return false;
217+
218+ if (prev->flags != next->flags)
219+ return false;
220+
221+ if (next->block_start < EXTENT_MAP_LAST_BYTE - 1)
222+ return next->block_start == extent_map_block_end(prev);
223+
224+ /* HOLES and INLINE extents. */
225+ return next->block_start == prev->block_start;
229226 }
230227
231228 static 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
244241 if (refcount_read(&em->refs) > 2)
245242 return;
246243
244+ if (!can_merge_extent_map(em))
245+ return;
246+
247247 if (em->start != 0) {
248248 rb = rb_prev(&em->rb_node);
249249 if (rb)
250250 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)) {
252252 em->start = merge->start;
253253 em->orig_start = merge->orig_start;
254254 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
257257 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
258258 em->mod_start = merge->mod_start;
259259 em->generation = max(em->generation, merge->generation);
260− set_bit(EXTENT_FLAG_MERGED, &em->flags);
260+ em->flags |= EXTENT_FLAG_MERGED;
261261
262262 rb_erase_cached(&merge->rb_node, &tree->map);
263263 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
268268 rb = rb_next(&em->rb_node);
269269 if (rb)
270270 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)) {
272272 em->len += merge->len;
273273 em->block_len += merge->block_len;
274274 rb_erase_cached(&merge->rb_node, &tree->map);
275275 RB_CLEAR_NODE(&merge->rb_node);
276276 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
277277 em->generation = max(em->generation, merge->generation);
278− set_bit(EXTENT_FLAG_MERGED, &em->flags);
278+ em->flags |= EXTENT_FLAG_MERGED;
279279 free_extent_map(merge);
280280 }
281281 }
@@ -283,7 +283,7 @@ static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) · 1 unmodified line
283283 /*
284284 * Unpin an extent from the cache.
285285 *
286− * @tree: tree to unpin the extent in
286+ * @inode: the inode from which we are unpinning an extent range
287287 * @start: logical offset in the file
288288 * @len: length of the extent
289289 * @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
292292 * to the generation that actually added the file item to the inode so we know
293293 * we need to sync this extent when we call fsync().
294294 */
295−int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
296− u64 gen)
295+int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
297296 {
297+ struct btrfs_fs_info *fs_info = inode->root->fs_info;
298+ struct extent_map_tree *tree = &inode->extent_tree;
298299 int ret = 0;
299300 struct extent_map *em;
300301 bool prealloc = false;
@@ -302,19 +303,28 @@ int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, · 1 unmodified line
302303 write_lock(&tree->lock);
303304 em = lookup_extent_mapping(tree, start, len);
304305
305− WARN_ON(!em || em->start != start);
306−
307− if (!em)
306+ if (WARN_ON(!em)) {
307+ btrfs_warn(fs_info,
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);
308311 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);
309319
310320 em->generation = gen;
311− clear_bit(EXTENT_FLAG_PINNED, &em->flags);
321+ em->flags &= ~EXTENT_FLAG_PINNED;
312322 em->mod_start = em->start;
313323 em->mod_len = em->len;
314324
315− if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
325+ if (em->flags & EXTENT_FLAG_FILLING) {
316326 prealloc = true;
317− clear_bit(EXTENT_FLAG_FILLING, &em->flags);
327+ em->flags &= ~EXTENT_FLAG_FILLING;
318328 }
319329
320330 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
335345 {
336346 lockdep_assert_held_write(&tree->lock);
337347
338− clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
348+ em->flags &= ~EXTENT_FLAG_LOGGING;
339349 if (extent_map_in_tree(em))
340350 try_merge_map(tree, em);
341351 }
@@ -348,45 +358,14 @@ static inline void setup_extent_mapping(struct extent_map_tree *tree, · 6 unmodified lines
348358 em->mod_start = em->start;
349359 em->mod_len = em->len;
350360
361+ ASSERT(list_empty(&em->list));
362+
351363 if (modified)
352− list_move(&em->list, &tree->modified_extents);
364+ list_add(&em->list, &tree->modified_extents);
353365 else
354366 try_merge_map(tree, em);
355367 }
356368
357−static 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−
373−static 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−
390369 /*
391370 * Add new extent map to the extent tree
392371 *
@@ -400,8 +379,8 @@ static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits) · 7 unmodified lines
400379 * into the tree directly, with an additional reference taken, or a
401380 * reference dropped if the merge attempt was successful.
402381 */
403−int add_extent_mapping(struct extent_map_tree *tree,
404− struct extent_map *em, int modified)
382+static int add_extent_mapping(struct extent_map_tree *tree,
383+ struct extent_map *em, int modified)
405384 {
406385 int ret = 0;
407386
@@ -412,10 +391,6 @@ int add_extent_mapping(struct extent_map_tree *tree, · 4 unmodified lines
412391 goto out;
413392
414393 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− }
419394 out:
420395 return ret;
421396 }
@@ -495,12 +470,10 @@ void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) · 73 unmodified lines
495470 {
496471 lockdep_assert_held_write(&tree->lock);
497472
498− WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
473+ WARN_ON(em->flags & EXTENT_FLAG_PINNED);
499474 rb_erase_cached(&em->rb_node, &tree->map);
500− if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
475+ if (!(em->flags & EXTENT_FLAG_LOGGING))
501476 list_del_init(&em->list);
502− if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
503− extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
504477 RB_CLEAR_NODE(&em->rb_node);
505478 }
506479
@@ -511,9 +484,9 @@ static void replace_extent_mapping(struct extent_map_tree *tree, · 4 unmodified lines
511484 {
512485 lockdep_assert_held_write(&tree->lock);
513486
514− WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
487+ WARN_ON(cur->flags & EXTENT_FLAG_PINNED);
515488 ASSERT(extent_map_in_tree(cur));
516− if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
489+ if (!(cur->flags & EXTENT_FLAG_LOGGING))
517490 list_del_init(&cur->list);
518491 rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
519492 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
576549 em->start = start;
577550 em->len = end - start;
578551 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
579− !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
552+ !extent_map_is_compressed(em)) {
580553 em->block_start += start_diff;
581554 em->block_len = em->len;
582555 }
@@ -626,8 +599,6 @@ int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, · 43 unmodified lines
626599 if (ret == -EEXIST) {
627600 struct extent_map *existing;
628601
629− ret = 0;
630−
631602 existing = search_extent_mapping(em_tree, start, len);
632603
633604 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
681652
682653 node = rb_first_cached(&tree->map);
683654 em = rb_entry(node, struct extent_map, rb_node);
684− clear_bit(EXTENT_FLAG_PINNED, &em->flags);
685− clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
655+ em->flags &= ~(EXTENT_FLAG_PINNED | EXTENT_FLAG_LOGGING);
686656 remove_extent_mapping(tree, em);
687657 free_extent_map(em);
688658 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
758728 }
759729 }
760730
761− if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
731+ if (skip_pinned && (em->flags & EXTENT_FLAG_PINNED)) {
762732 start = em_end;
763733 goto next;
764734 }
765735
766736 flags = em->flags;
767− clear_bit(EXTENT_FLAG_PINNED, &em->flags);
768737 /*
769738 * In case we split the extent map, we want to preserve the
770739 * EXTENT_FLAG_LOGGING flag on our extent map, but we don't want
771740 * it on the new extent maps.
772741 */
773− clear_bit(EXTENT_FLAG_LOGGING, &flags);
742+ em->flags &= ~(EXTENT_FLAG_PINNED | EXTENT_FLAG_LOGGING);
774743 modified = !list_empty(&em->list);
775744
776745 /*
@@ -781,7 +750,7 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 4 unmodified lines
781750 goto remove_em;
782751
783752 gen = em->generation;
784− compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
753+ compressed = extent_map_is_compressed(em);
785754
786755 if (em->start < start) {
787756 if (!split) {
@@ -814,7 +783,6 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 26 unmodified lines
814783
815784 split->generation = gen;
816785 split->flags = flags;
817− split->compress_type = em->compress_type;
818786 replace_extent_mapping(em_tree, em, split, modified);
819787 free_extent_map(split);
820788 split = split2;
@@ -831,7 +799,6 @@ void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end, · 10 unmodified lines
831799 split->len = em_end - end;
832800 split->block_start = em->block_start;
833801 split->flags = flags;
834− split->compress_type = em->compress_type;
835802 split->generation = gen;
836803
837804 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
997964 }
998965
999966 ASSERT(em->len == len);
1000− ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
967+ ASSERT(!extent_map_is_compressed(em));
1001968 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
1002− ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
1003− ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
969+ ASSERT(em->flags & EXTENT_FLAG_PINNED);
970+ ASSERT(!(em->flags & EXTENT_FLAG_LOGGING));
1004971 ASSERT(!list_empty(&em->list));
1005972
1006973 flags = em->flags;
1007− clear_bit(EXTENT_FLAG_PINNED, &em->flags);
974+ em->flags &= ~EXTENT_FLAG_PINNED;
1008975
1009976 /* First, replace the em with a new extent_map starting from * em->start */
1010977 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
1015982 split_pre->orig_block_len = split_pre->block_len;
1016983 split_pre->ram_bytes = split_pre->len;
1017984 split_pre->flags = flags;
1018− split_pre->compress_type = em->compress_type;
1019985 split_pre->generation = em->generation;
1020986
1021987 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
10341000 split_mid->orig_block_len = split_mid->block_len;
10351001 split_mid->ram_bytes = split_mid->len;
10361002 split_mid->flags = flags;
1037− split_mid->compress_type = em->compress_type;
10381003 split_mid->generation = em->generation;
10391004 add_extent_mapping(em_tree, split_mid, 1);
10401005
fs/btrfs/extent_map.h+52−25
@@ -5,30 +5,33 @@ · 4 unmodified lines
55
66 #include <linux/rbtree.h>
77 #include <linux/refcount.h>
8+#include "compression.h"
89
910 #define EXTENT_MAP_LAST_BYTE ((u64)-4)
1011 #define EXTENT_MAP_HOLE ((u64)-3)
1112 #define EXTENT_MAP_INLINE ((u64)-2)
12−/* used only during fiemap calls */
13−#define EXTENT_MAP_DELALLOC ((u64)-1)
1413
1514 /* bits for the extent_map::flags field */
1615 enum {
1716 /* this entry not yet on disk, don't free it */
18− EXTENT_FLAG_PINNED,
19− EXTENT_FLAG_COMPRESSED,
17+ ENUM_BIT(EXTENT_FLAG_PINNED),
18+ ENUM_BIT(EXTENT_FLAG_COMPRESS_ZLIB),
19+ ENUM_BIT(EXTENT_FLAG_COMPRESS_LZO),
20+ ENUM_BIT(EXTENT_FLAG_COMPRESS_ZSTD),
2021 /* pre-allocated extent */
21− EXTENT_FLAG_PREALLOC,
22+ ENUM_BIT(EXTENT_FLAG_PREALLOC),
2223 /* Logging this extent */
23− EXTENT_FLAG_LOGGING,
24+ ENUM_BIT(EXTENT_FLAG_LOGGING),
2425 /* Filling in a preallocated extent */
25− EXTENT_FLAG_FILLING,
26− /* filesystem extent mapping type */
27− EXTENT_FLAG_FS_MAPPING,
26+ ENUM_BIT(EXTENT_FLAG_FILLING),
2827 /* This em is merged from two or more physically adjacent ems */
29− EXTENT_FLAG_MERGED,
28+ ENUM_BIT(EXTENT_FLAG_MERGED),
3029 };
3130
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+ */
3235 struct extent_map {
3336 struct rb_node rb_node;
3437
@@ -49,11 +52,8 @@ struct extent_map { · 14 unmodified lines
4952 * For non-merged extents, it's from btrfs_file_extent_item::generation.
5053 */
5154 u64 generation;
52− unsigned long flags;
53− /* Used for chunk mappings, flag EXTENT_FLAG_FS_MAPPING must be set */
54− struct map_lookup *map_lookup;
55+ u32 flags;
5556 refcount_t refs;
56− unsigned int compress_type;
5757 struct list_head list;
5858 };
5959
@@ -65,30 +65,57 @@ struct extent_map_tree { · 5 unmodified lines
6565
6666 struct btrfs_inode;
6767
68+static 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+
79+static 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+ */
97+static 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+
68104 static inline int extent_map_in_tree(const struct extent_map *em)
69105 {
70106 return !RB_EMPTY_NODE(&em->rb_node);
71107 }
72108
73−static inline u64 extent_map_end(struct extent_map *em)
109+static inline u64 extent_map_end(const struct extent_map *em)
74110 {
75111 if (em->start + em->len < em->start)
76112 return (u64)-1;
77113 return em->start + em->len;
78114 }
79115
80−static 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−
87116 void extent_map_tree_init(struct extent_map_tree *tree);
88117 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
89118 u64 start, u64 len);
90−int add_extent_mapping(struct extent_map_tree *tree,
91− struct extent_map *em, int modified);
92119 void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);
93120 int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,
94121 u64 new_logical);
@@ -97,7 +124,7 @@ struct extent_map *alloc_extent_map(void); · 2 unmodified lines
97124 void free_extent_map(struct extent_map *em);
98125 int __init extent_map_init(void);
99126 void __cold extent_map_exit(void);
100−int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
127+int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
101128 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
102129 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
103130 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
5959 goto out_unlock;
6060 }
6161
62− ret = find_contiguous_extent_bit(&inode->file_extent_tree, 0, &start,
62+ ret = find_contiguous_extent_bit(inode->file_extent_tree, 0, &start,
6363 &end, EXTENT_DIRTY);
6464 if (!ret && start == 0)
6565 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
9494
9595 if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
9696 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,
9898 EXTENT_DIRTY, NULL);
9999 }
100100
@@ -123,7 +123,7 @@ int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, · 22 unmodified lines
123123
124124 if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
125125 return 0;
126− return clear_extent_bit(&inode->file_extent_tree, start,
126+ return clear_extent_bit(inode->file_extent_tree, start,
127127 start + len - 1, EXTENT_DIRTY, NULL);
128128 }
129129
@@ -1294,8 +1294,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · 1164 unmodified lines
12941294 return;
12951295 }
12961296 if (compress_type != BTRFS_COMPRESS_NONE) {
1297− set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1298− em->compress_type = compress_type;
1297+ extent_map_set_compression(em, compress_type);
12991298 em->block_start = bytenr;
13001299 em->block_len = em->orig_block_len;
13011300 } else {
@@ -1303,7 +1302,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · 1 unmodified line
13031302 em->block_start = bytenr;
13041303 em->block_len = em->len;
13051304 if (type == BTRFS_FILE_EXTENT_PREALLOC)
1306− set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
1305+ em->flags |= EXTENT_FLAG_PREALLOC;
13071306 }
13081307 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
13091308 em->block_start = EXTENT_MAP_INLINE;
@@ -1315,9 +1314,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, · 5 unmodified lines
13151314 */
13161315 em->orig_start = EXTENT_MAP_HOLE;
13171316 em->block_len = (u64)-1;
1318− em->compress_type = compress_type;
1319− if (compress_type != BTRFS_COMPRESS_NONE)
1320− set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
1317+ extent_map_set_compression(em, compress_type);
13211318 } else {
13221319 btrfs_err(fs_info,
13231320 "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
111111 * accessed as prepare_pages should have marked them accessed
112112 * in prepare_pages via find_or_create_page()
113113 */
114− btrfs_page_clamp_clear_checked(fs_info, pages[i], block_start,
115− block_len);
114+ btrfs_folio_clamp_clear_checked(fs_info, page_folio(pages[i]),
115+ block_start, block_len);
116116 unlock_page(pages[i]);
117117 put_page(pages[i]);
118118 }
@@ -168,9 +168,12 @@ int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages, · 49 unmodified lines
168168 for (i = 0; i < num_pages; i++) {
169169 struct page *p = pages[i];
170170
171− btrfs_page_clamp_set_uptodate(fs_info, p, start_pos, num_bytes);
172− btrfs_page_clamp_clear_checked(fs_info, p, start_pos, num_bytes);
173− btrfs_page_clamp_set_dirty(fs_info, p, start_pos, num_bytes);
171+ btrfs_folio_clamp_set_uptodate(fs_info, page_folio(p),
172+ 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);
174177 }
175178
176179 /*
@@ -869,9 +872,9 @@ static int prepare_uptodate_page(struct inode *inode, · 692 unmodified lines
869872 * released.
870873 *
871874 * 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.
873876 */
874− if (page->mapping != inode->i_mapping || !PagePrivate(page)) {
877+ if (page->mapping != inode->i_mapping || !folio_test_private(folio)) {
875878 unlock_page(page);
876879 return -EAGAIN;
877880 }
@@ -2150,7 +2153,6 @@ out: · 1272 unmodified lines
21502153 hole_em->block_start = EXTENT_MAP_HOLE;
21512154 hole_em->block_len = 0;
21522155 hole_em->orig_block_len = 0;
2153− hole_em->compress_type = BTRFS_COMPRESS_NONE;
21542156 hole_em->generation = trans->transid;
21552157
21562158 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
28392841
28402842 if (em->block_start == EXTENT_MAP_HOLE)
28412843 ret = RANGE_BOUNDARY_HOLE;
2842− else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2844+ else if (em->flags & EXTENT_FLAG_PREALLOC)
28432845 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
28442846 else
28452847 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
@@ -2879,8 +2881,7 @@ static int btrfs_zero_range(struct inode *inode, · 33 unmodified lines
28792881 * extents and holes, we drop all the existing extents and allocate a
28802882 * new prealloc extent, so that we get a larger contiguous disk extent.
28812883 */
2882− if (em->start <= alloc_start &&
2883− test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
2884+ if (em->start <= alloc_start && (em->flags & EXTENT_FLAG_PREALLOC)) {
28842885 const u64 em_end = em->start + em->len;
28852886
28862887 if (em_end >= offset + len) {
@@ -2915,7 +2916,7 @@ static int btrfs_zero_range(struct inode *inode, · 28 unmodified lines
29152916 goto out;
29162917 }
29172918
2918− if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
2919+ if (em->flags & EXTENT_FLAG_PREALLOC) {
29192920 free_extent_map(em);
29202921 ret = btrfs_fallocate_update_isize(inode, offset + len,
29212922 mode);
@@ -3136,7 +3137,7 @@ static long btrfs_fallocate(struct file *file, int mode, · 214 unmodified lines
31363137 last_byte = ALIGN(last_byte, blocksize);
31373138 if (em->block_start == EXTENT_MAP_HOLE ||
31383139 (cur_offset >= inode->i_size &&
3139− !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
3140+ !(em->flags & EXTENT_FLAG_PREALLOC))) {
31403141 const u64 range_len = last_byte - cur_offset;
31413142
31423143 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
439439
440440 for (i = 0; i < io_ctl->num_pages; i++) {
441441 if (io_ctl->pages[i]) {
442− btrfs_page_clear_checked(io_ctl->fs_info,
443− io_ctl->pages[i],
442+ btrfs_folio_clear_checked(io_ctl->fs_info,
443+ page_folio(io_ctl->pages[i]),
444444 page_offset(io_ctl->pages[i]),
445445 PAGE_SIZE);
446446 unlock_page(io_ctl->pages[i]);
fs/btrfs/fs.h+3−15
@@ -188,6 +188,7 @@ enum { · 187 unmodified lines
188188 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 27),
189189 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 28),
190190 BTRFS_MOUNT_NODISCARD = (1UL << 29),
191+ BTRFS_MOUNT_NOSPACECACHE = (1UL << 30),
191192 };
192193
193194 /*
@@ -398,7 +399,8 @@ struct btrfs_fs_info { · 204 unmodified lines
398399 struct extent_io_tree excluded_extents;
399400
400401 /* logical->physical extent mapping */
401− struct extent_map_tree mapping_tree;
402+ struct rb_root_cached mapping_tree;
403+ rwlock_t mapping_tree_lock;
402404
403405 /*
404406 * 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
960962 #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
961963 BTRFS_MOUNT_##opt)
962964
963−#define btrfs_set_and_info(fs_info, opt, fmt, args...) \
964−do { \
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...) \
971−do { \
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−
977965 static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
978966 {
979967 /* 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
114114 int mirror_num;
115115 };
116116
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+ */
124+static struct lock_class_key file_extent_tree_class;
125+
117126 static const struct inode_operations btrfs_dir_inode_operations;
118127 static const struct inode_operations btrfs_symlink_inode_operations;
119128 static 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
447456 * range, then btrfs_mark_ordered_io_finished() will handle
448457 * the ordered extent accounting for the range.
449458 */
450− btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
451− offset, bytes);
459+ btrfs_folio_clamp_clear_ordered(inode->root->fs_info,
460+ page_folio(page), offset, bytes);
452461 put_page(page);
453462 }
454463
@@ -1037,7 +1046,7 @@ free_pages: · 582 unmodified lines
10371046 if (pages) {
10381047 for (i = 0; i < nr_pages; i++) {
10391048 WARN_ON(pages[i]->mapping);
1040− put_page(pages[i]);
1049+ btrfs_free_compr_page(pages[i]);
10411050 }
10421051 kfree(pages);
10431052 }
@@ -1052,7 +1061,7 @@ static void free_async_extent_pages(struct async_extent *async_extent) · 8 unmodified lines
10521061
10531062 for (i = 0; i < async_extent->nr_pages; i++) {
10541063 WARN_ON(async_extent->pages[i]->mapping);
1055− put_page(async_extent->pages[i]);
1064+ btrfs_free_compr_page(async_extent->pages[i]);
10561065 }
10571066 kfree(async_extent->pages);
10581067 async_extent->nr_pages = 0;
@@ -2793,7 +2802,7 @@ out_page: · 1734 unmodified lines
27932802 PAGE_SIZE, !ret);
27942803 clear_page_dirty_for_io(page);
27952804 }
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);
27972806 unlock_page(page);
27982807 put_page(page);
27992808 kfree(fixup);
@@ -2848,7 +2857,7 @@ int btrfs_writepage_cow_fixup(struct page *page) · 48 unmodified lines
28482857 * page->mapping outside of the page lock.
28492858 */
28502859 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);
28522861 get_page(page);
28532862 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL);
28542863 fixup->page = page;
@@ -3118,7 +3127,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 263 unmodified lines
31183127 ordered_extent->disk_num_bytes);
31193128 }
31203129 }
3121− unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
3130+ unpin_extent_cache(inode, ordered_extent->file_offset,
31223131 ordered_extent->num_bytes, trans->transid);
31233132 if (ret < 0) {
31243133 btrfs_abort_transaction(trans, ret);
@@ -3796,7 +3805,7 @@ cache_index: · 671 unmodified lines
37963805 * cache.
37973806 *
37983807 * This is required for both inode re-read from disk and delayed inode
3799− * in delayed_nodes_tree.
3808+ * in the delayed_nodes xarray.
38003809 */
38013810 if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info))
38023811 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
@@ -4725,7 +4734,7 @@ again: · 922 unmodified lines
47254734 /*
47264735 * We unlock the page after the io is completed and then re-lock it
47274736 * above. release_folio() could have come in between that and cleared
4728− * PagePrivate(), but left the page in the mapping. Set the page mapped
4737+ * folio private, but left the page in the mapping. Set the page mapped
47294738 * here to make sure it's properly set for the subpage stuff.
47304739 */
47314740 ret = set_page_extent_mapped(page);
@@ -4767,9 +4776,10 @@ again: · 35 unmodified lines
47674776 memzero_page(page, (block_start - page_offset(page)) + offset,
47684777 len);
47694778 }
4770− btrfs_page_clear_checked(fs_info, page, block_start,
4771− block_end + 1 - block_start);
4772− btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
4779+ btrfs_folio_clear_checked(fs_info, page_folio(page), block_start,
4780+ block_end + 1 - block_start);
4781+ btrfs_folio_set_dirty(fs_info, page_folio(page), block_start,
4782+ block_end + 1 - block_start);
47734783 unlock_extent(io_tree, block_start, block_end, &cached_state);
47744784
47754785 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
48894899 last_byte = ALIGN(last_byte, fs_info->sectorsize);
48904900 hole_size = last_byte - cur_offset;
48914901
4892− if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4902+ if (!(em->flags & EXTENT_FLAG_PREALLOC)) {
48934903 struct extent_map *hole_em;
48944904
48954905 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
49174927 hole_em->block_len = 0;
49184928 hole_em->orig_block_len = 0;
49194929 hole_em->ram_bytes = hole_size;
4920− hole_em->compress_type = BTRFS_COMPRESS_NONE;
49214930 hole_em->generation = btrfs_get_fs_generation(fs_info);
49224931
49234932 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
62166225 BTRFS_I(inode)->generation = trans->transid;
62176226 inode->i_generation = BTRFS_I(inode)->generation;
62186227
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+
62196235 /*
62206236 * Subvolumes don't inherit flags from their parent directory.
62216237 * 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
72587274 em->orig_block_len = orig_block_len;
72597275 em->ram_bytes = ram_bytes;
72607276 em->generation = -1;
7261− set_bit(EXTENT_FLAG_PINNED, &em->flags);
7262− if (type == BTRFS_ORDERED_PREALLOC) {
7263− set_bit(EXTENT_FLAG_FILLING, &em->flags);
7264− } else if (type == BTRFS_ORDERED_COMPRESSED) {
7265− set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7266− em->compress_type = compress_type;
7267− }
7277+ em->flags |= EXTENT_FLAG_PINNED;
7278+ if (type == BTRFS_ORDERED_PREALLOC)
7279+ em->flags |= EXTENT_FLAG_FILLING;
7280+ else if (type == BTRFS_ORDERED_COMPRESSED)
7281+ extent_map_set_compression(em, compress_type);
72687282
72697283 ret = btrfs_replace_extent_map_range(inode, em, true);
72707284 if (ret) {
@@ -7304,10 +7318,10 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map, · 33 unmodified lines
73047318 * just use the extent.
73057319 *
73067320 */
7307− if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7321+ if ((em->flags & EXTENT_FLAG_PREALLOC) ||
73087322 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
73097323 em->block_start != EXTENT_MAP_HOLE)) {
7310− if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7324+ if (em->flags & EXTENT_FLAG_PREALLOC)
73117325 type = BTRFS_ORDERED_PREALLOC;
73127326 else
73137327 type = BTRFS_ORDERED_NOCOW;
@@ -7542,7 +7556,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, · 228 unmodified lines
75427556 * to buffered IO. Don't blame me, this is the price we pay for using
75437557 * the generic code.
75447558 */
7545− if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7559+ if (extent_map_is_compressed(em) ||
75467560 em->block_start == EXTENT_MAP_INLINE) {
75477561 free_extent_map(em);
75487562 /*
@@ -7638,7 +7652,7 @@ static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start, · 89 unmodified lines
76387652 * that, since we have locked only the parts we are performing I/O in.
76397653 */
76407654 if ((em->block_start == EXTENT_MAP_HOLE) ||
7641− (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7655+ ((em->flags & EXTENT_FLAG_PREALLOC) && !write)) {
76427656 iomap->addr = IOMAP_NULL_ADDR;
76437657 iomap->type = IOMAP_HOLE;
76447658 } else {
@@ -7851,13 +7865,14 @@ static void btrfs_readahead(struct readahead_control *rac) · 206 unmodified lines
78517865 static void wait_subpage_spinlock(struct page *page)
78527866 {
78537867 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
7868+ struct folio *folio = page_folio(page);
78547869 struct btrfs_subpage *subpage;
78557870
7856− if (!btrfs_is_subpage(fs_info, page))
7871+ if (!btrfs_is_subpage(fs_info, page->mapping))
78577872 return;
78587873
7859− ASSERT(PagePrivate(page) && page->private);
7860− subpage = (struct btrfs_subpage *)page->private;
7874+ ASSERT(folio_test_private(folio) && folio_get_private(folio));
7875+ subpage = folio_get_private(folio);
78617876
78627877 /*
78637878 * 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
79958010 page_end);
79968011 ASSERT(range_end + 1 - cur < U32_MAX);
79978012 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)) {
79998014 /*
80008015 * If Ordered (Private2) is cleared, it means endio has
80018016 * already been executed for the range.
@@ -8004,7 +8019,7 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset, · 2 unmodified lines
80048019 */
80058020 goto next;
80068021 }
8007− btrfs_page_clear_ordered(fs_info, &folio->page, cur, range_len);
8022+ btrfs_folio_clear_ordered(fs_info, folio, cur, range_len);
80088023
80098024 /*
80108025 * IO on this page will never be started, so we need to account
@@ -8074,7 +8089,7 @@ next: · 63 unmodified lines
80748089 * did something wrong.
80758090 */
80768091 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));
80788093 if (!inode_evicting)
80798094 __btrfs_release_folio(folio, GFP_NOFS);
80808095 clear_page_extent_mapped(&folio->page);
@@ -8098,6 +8113,7 @@ next: · 17 unmodified lines
80988113 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
80998114 {
81008115 struct page *page = vmf->page;
8116+ struct folio *folio = page_folio(page);
81018117 struct inode *inode = file_inode(vmf->vma->vm_file);
81028118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
81038119 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
81148130 u64 page_end;
81158131 u64 end;
81168132
8133+ ASSERT(folio_order(folio) == 0);
8134+
81178135 reserved_space = PAGE_SIZE;
81188136
81198137 sb_start_pagefault(inode->i_sb);
@@ -8217,9 +8235,9 @@ again: · 97 unmodified lines
82178235 if (zero_start != PAGE_SIZE)
82188236 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
82198237
8220− btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);
8221− btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8222− btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
8238+ btrfs_folio_clear_checked(fs_info, folio, page_start, PAGE_SIZE);
8239+ btrfs_folio_set_dirty(fs_info, folio, page_start, end + 1 - page_start);
8240+ btrfs_folio_set_uptodate(fs_info, folio, page_start, end + 1 - page_start);
82238241
82248242 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
82258243
@@ -8462,10 +8480,20 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 236 unmodified lines
84628480 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
84638481 struct btrfs_inode *ei;
84648482 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+ }
84658491
84668492 ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);
8467− if (!ei)
8493+ if (!ei) {
8494+ kfree(file_extent_tree);
84688495 return NULL;
8496+ }
84698497
84708498 ei->root = NULL;
84718499 ei->generation = 0;
@@ -8501,10 +8529,18 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 29 unmodified lines
85018529
85028530 inode = &ei->vfs_inode;
85038531 extent_map_tree_init(&ei->extent_tree);
8532+
8533+ /* This io tree sets the valid inode. */
85048534 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO);
85058535 ei->io_tree.inode = ei;
8506− extent_io_tree_init(fs_info, &ei->file_extent_tree,
8507− IO_TREE_INODE_FILE_EXTENT);
8536+
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+ }
85088544 mutex_init(&ei->log_mutex);
85098545 spin_lock_init(&ei->ordered_tree_lock);
85108546 ei->ordered_tree = RB_ROOT;
@@ -8521,12 +8557,14 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 10 unmodified lines
85218557 void btrfs_test_destroy_inode(struct inode *inode)
85228558 {
85238559 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
8560+ kfree(BTRFS_I(inode)->file_extent_tree);
85248561 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
85258562 }
85268563 #endif
85278564
85288565 void btrfs_free_inode(struct inode *inode)
85298566 {
8567+ kfree(BTRFS_I(inode)->file_extent_tree);
85308568 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
85318569 }
85328570
@@ -9632,7 +9670,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, · 1099 unmodified lines
96329670 em->block_len = ins.offset;
96339671 em->orig_block_len = ins.offset;
96349672 em->ram_bytes = ins.offset;
9635− set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9673+ em->flags |= EXTENT_FLAG_PREALLOC;
96369674 em->generation = trans->transid;
96379675
96389676 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
97859823 page = find_get_page(inode->vfs_inode.i_mapping, index);
97869824 ASSERT(page); /* Pages should be in the extent_io_tree */
97879825
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);
97899829 put_page(page);
97909830 index++;
97919831 }
@@ -9994,7 +10034,7 @@ static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb, · 202 unmodified lines
999410034 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
999510035 if (!pages)
999610036 return -ENOMEM;
9997− ret = btrfs_alloc_page_array(nr_pages, pages);
10037+ ret = btrfs_alloc_page_array(nr_pages, pages, 0);
999810038 if (ret) {
999910039 ret = -ENOMEM;
1000010040 goto out;
@@ -10113,12 +10153,12 @@ ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter, · 112 unmodified lines
1011310153 encoded->len = min_t(u64, extent_map_end(em),
1011410154 inode->vfs_inode.i_size) - iocb->ki_pos;
1011510155 if (em->block_start == EXTENT_MAP_HOLE ||
10116− test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
10156+ (em->flags & EXTENT_FLAG_PREALLOC)) {
1011710157 disk_bytenr = EXTENT_MAP_HOLE;
1011810158 count = min_t(u64, count, encoded->len);
1011910159 encoded->len = count;
1012010160 encoded->unencoded_len = count;
10121− } else if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10161+ } else if (extent_map_is_compressed(em)) {
1012210162 disk_bytenr = em->block_start;
1012310163 /*
1012410164 * 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
1013310173 encoded->unencoded_len = em->ram_bytes;
1013410174 encoded->unencoded_offset = iocb->ki_pos - em->orig_start;
1013510175 ret = btrfs_encoded_io_compression_from_extent(fs_info,
10136− em->compress_type);
10176+ extent_map_compression(em));
1013710177 if (ret < 0)
1013810178 goto out_em;
1013910179 encoded->compression = ret;
@@ -10564,6 +10604,7 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 424 unmodified lines
1056410604 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1056510605 struct extent_state *cached_state = NULL;
1056610606 struct extent_map *em = NULL;
10607+ struct btrfs_chunk_map *map = NULL;
1056710608 struct btrfs_device *device = NULL;
1056810609 struct btrfs_swap_info bsi = {
1056910610 .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
1068010721 ret = -EINVAL;
1068110722 goto out;
1068210723 }
10683− if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10724+ if (extent_map_is_compressed(em)) {
1068410725 btrfs_warn(fs_info, "swapfile must not be compressed");
1068510726 ret = -EINVAL;
1068610727 goto out;
@@ -10703,13 +10744,13 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 16 unmodified lines
1070310744 goto out;
1070410745 }
1070510746
10706− em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10707− if (IS_ERR(em)) {
10708− ret = PTR_ERR(em);
10747+ map = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10748+ if (IS_ERR(map)) {
10749+ ret = PTR_ERR(map);
1070910750 goto out;
1071010751 }
1071110752
10712− if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10753+ if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1071310754 btrfs_warn(fs_info,
1071410755 "swapfile must have single data profile");
1071510756 ret = -EINVAL;
@@ -10717,23 +10758,23 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 1 unmodified line
1071710758 }
1071810759
1071910760 if (device == NULL) {
10720− device = em->map_lookup->stripes[0].dev;
10761+ device = map->stripes[0].dev;
1072110762 ret = btrfs_add_swapfile_pin(inode, device, false);
1072210763 if (ret == 1)
1072310764 ret = 0;
1072410765 else if (ret)
1072510766 goto out;
10726− } else if (device != em->map_lookup->stripes[0].dev) {
10767+ } else if (device != map->stripes[0].dev) {
1072710768 btrfs_warn(fs_info, "swapfile must be on one device");
1072810769 ret = -EINVAL;
1072910770 goto out;
1073010771 }
1073110772
10732− physical_block_start = (em->map_lookup->stripes[0].physical +
10733− (logical_block_start - em->start));
10734− len = min(len, em->len - (logical_block_start - em->start));
10735− free_extent_map(em);
10736− em = NULL;
10773+ physical_block_start = (map->stripes[0].physical +
10774+ (logical_block_start - map->start));
10775+ len = min(len, map->chunk_len - (logical_block_start - map->start));
10776+ btrfs_free_chunk_map(map);
10777+ map = NULL;
1073710778
1073810779 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
1073910780 if (!bg) {
@@ -10786,6 +10827,8 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file, · 46 unmodified lines
1078610827 out:
1078710828 if (!IS_ERR_OR_NULL(em))
1078810829 free_extent_map(em);
10830+ if (!IS_ERR_OR_NULL(map))
10831+ btrfs_free_chunk_map(map);
1078910832
1079010833 unlock_extent(io_tree, 0, isize - 1, &cached_state);
1079110834
fs/btrfs/lru_cache.c+1−1
@@ -9,7 +9,7 @@ · 8 unmodified lines
99 *
1010 * @cache: The cache.
1111 * @max_size: Maximum size (number of entries) for the cache.
12− * Use 0 for unlimited size, it's the user's responsability to
12+ * Use 0 for unlimited size, it's the user's responsibility to
1313 * trim the cache in that case.
1414 */
1515 void 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
152152 cur_page = out_pages[*cur_out / PAGE_SIZE];
153153 /* Allocate a new page */
154154 if (!cur_page) {
155− cur_page = alloc_page(GFP_NOFS);
155+ cur_page = btrfs_alloc_compr_page();
156156 if (!cur_page)
157157 return -ENOMEM;
158158 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
178178 cur_page = out_pages[*cur_out / PAGE_SIZE];
179179 /* Allocate a new page */
180180 if (!cur_page) {
181− cur_page = alloc_page(GFP_NOFS);
181+ cur_page = btrfs_alloc_compr_page();
182182 if (!cur_page)
183183 return -ENOMEM;
184184 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
287287 * panic or BUGs, depending on mount options.
288288 */
289289 __cold
290−void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
290+void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
291291 unsigned int line, int error, const char *fmt, ...)
292292 {
293293 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
194194
195195 __printf(5, 6)
196196 __cold
197−void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
197+void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function,
198198 unsigned int line, int error, const char *fmt, ...);
199199 /*
200200 * 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
323323 *
324324 * If there's no such bit, we need to skip to next range.
325325 */
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))
327328 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);
329330 }
330331
331332 /* 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
9696 /* number of bytes that still need writing */
9797 u64 bytes_left;
9898
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−
10699 /*
107100 * If we get truncated we need to adjust the file extent we enter for
108101 * 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
194194 *
195195 * Must be called with qgroup_lock held and @prealloc preallocated.
196196 *
197− * The control on the lifespan of @prealloc would be transfered to this
197+ * The control on the lifespan of @prealloc would be transferred to this
198198 * function, thus caller should no longer touch @prealloc.
199199 */
200200 static 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
964964 {
965965 int ret;
966966
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);
968968 if (ret < 0)
969969 return ret;
970970 /* Mapping all sectors */
@@ -979,7 +979,7 @@ static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio) · 8 unmodified lines
979979 int ret;
980980
981981 ret = btrfs_alloc_page_array(rbio->nr_pages - data_pages,
982− rbio->stripe_pages + data_pages);
982+ rbio->stripe_pages + data_pages, 0);
983983 if (ret < 0)
984984 return ret;
985985
@@ -1530,7 +1530,7 @@ static int alloc_rbio_data_pages(struct btrfs_raid_bio *rbio) · 544 unmodified lines
15301530 const int data_pages = rbio->nr_data * rbio->stripe_npages;
15311531 int ret;
15321532
1533− ret = btrfs_alloc_page_array(data_pages, rbio->stripe_pages);
1533+ ret = btrfs_alloc_page_array(data_pages, rbio->stripe_pages, 0);
15341534 if (ret < 0)
15351535 return ret;
15361536
@@ -1549,7 +1549,6 @@ struct btrfs_plug_cb { · 12 unmodified lines
15491549 struct blk_plug_cb cb;
15501550 struct btrfs_fs_info *info;
15511551 struct list_head rbio_list;
1552− struct work_struct work;
15531552 };
15541553
15551554 /*
fs/btrfs/raid56.h+1−1
@@ -164,7 +164,7 @@ struct raid56_bio_trace_info { · 163 unmodified lines
164164 u8 stripe_nr;
165165 };
166166
167−static inline int nr_data_stripes(const struct map_lookup *map)
167+static inline int nr_data_stripes(const struct btrfs_chunk_map *map)
168168 {
169169 return map->num_stripes - btrfs_nr_parity_stripes(map->type);
170170 }
fs/btrfs/reflink.c+3−3
@@ -141,9 +141,9 @@ static int copy_inline_to_page(struct btrfs_inode *inode, · 140 unmodified lines
141141 if (datal < block_size)
142142 memzero_page(page, datal, block_size - datal);
143143
144− btrfs_page_set_uptodate(fs_info, page, file_offset, block_size);
145− btrfs_page_clear_checked(fs_info, page, file_offset, block_size);
146− btrfs_page_set_dirty(fs_info, page, file_offset, block_size);
144+ btrfs_folio_set_uptodate(fs_info, page_folio(page), file_offset, block_size);
145+ btrfs_folio_clear_checked(fs_info, page_folio(page), file_offset, block_size);
146+ btrfs_folio_set_dirty(fs_info, page_folio(page), file_offset, block_size);
147147 out_unlock:
148148 if (page) {
149149 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
28952895 * will re-read the whole page anyway.
28962896 */
28972897 if (page) {
2898− btrfs_subpage_clear_uptodate(fs_info, page, i_size,
2898+ btrfs_subpage_clear_uptodate(fs_info, page_folio(page), i_size,
28992899 round_up(i_size, PAGE_SIZE) - i_size);
29002900 unlock_page(page);
29012901 put_page(page);
@@ -2951,7 +2951,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod · 49 unmodified lines
29512951 em->len = end + 1 - start;
29522952 em->block_len = em->len;
29532953 em->block_start = block_start;
2954− set_bit(EXTENT_FLAG_PINNED, &em->flags);
2954+ em->flags |= EXTENT_FLAG_PINNED;
29552955
29562956 lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
29572957 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
30703070 clamped_len);
30713071 goto release_page;
30723072 }
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);
30743075
30753076 /*
30763077 * 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
4343 /*
4444 * The following value only influences the performance.
4545 *
46− * This detemines how many stripes would be submitted in one go,
46+ * This determines how many stripes would be submitted in one go,
4747 * which is 512KiB (BTRFS_STRIPE_LEN * SCRUB_STRIPES_PER_GROUP).
4848 */
4949 #define SCRUB_STRIPES_PER_GROUP 8
@@ -192,7 +192,6 @@ struct scrub_ctx { · 142 unmodified lines
192192 int cur_stripe;
193193 atomic_t cancel_req;
194194 int readonly;
195− int sectors_per_bio;
196195
197196 /* State of IO submission throttling affecting the associated device */
198197 ktime_t throttle_deadline;
@@ -262,7 +261,7 @@ static int init_scrub_stripe(struct btrfs_fs_info *fs_info, · 63 unmodified lines
262261 atomic_set(&stripe->pending_io, 0);
263262 spin_lock_init(&stripe->write_error_lock);
264263
265− ret = btrfs_alloc_page_array(SCRUB_STRIPE_PAGES, stripe->pages);
264+ ret = btrfs_alloc_page_array(SCRUB_STRIPE_PAGES, stripe->pages, 0);
266265 if (ret < 0)
267266 goto error;
268267
@@ -710,7 +709,7 @@ static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr) · 441 unmodified lines
710709 /* Metadata, verify the full tree block. */
711710 if (sector->is_metadata) {
712711 /*
713− * Check if the tree block crosses the stripe boudary. If
712+ * Check if the tree block crosses the stripe boundary. If
714713 * crossed the boundary, we cannot verify it but only give a
715714 * warning.
716715 *
@@ -884,7 +883,7 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx, · 167 unmodified lines
884883 /*
885884 * Init needed infos for error reporting.
886885 *
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()
888887 * thus no need for dev/physical, error reporting still needs dev and physical.
889888 */
890889 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
12801279 * return 0 if it is a data stripe, 1 means parity stripe.
12811280 */
12821281 static int get_raid56_logic_offset(u64 physical, int num,
1283− struct map_lookup *map, u64 *offset,
1282+ struct btrfs_chunk_map *map, u64 *offset,
12841283 u64 *stripe_start)
12851284 {
12861285 int i;
@@ -1409,14 +1408,11 @@ search_forward: · 122 unmodified lines
14091408 if (ret > 0)
14101409 break;
14111410 next:
1412− path->slots[0]++;
1413− if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
1414− ret = btrfs_next_leaf(extent_root, path);
1415− if (ret) {
1416− /* Either no more item or fatal error */
1417− btrfs_release_path(path);
1418− return ret;
1419− }
1411+ ret = btrfs_next_item(extent_root, path);
1412+ if (ret) {
1413+ /* Either no more items or a fatal error. */
1414+ btrfs_release_path(path);
1415+ return ret;
14201416 }
14211417 }
14221418 btrfs_release_path(path);
@@ -1816,7 +1812,7 @@ static int flush_scrub_stripes(struct scrub_ctx *sctx) · 393 unmodified lines
18161812 if (sctx->is_dev_replace) {
18171813 /*
18181814 * For dev-replace, if we know there is something wrong with
1819− * metadata, we should immedately abort.
1815+ * metadata, we should immediately abort.
18201816 */
18211817 for (int i = 0; i < nr_stripes; i++) {
18221818 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
18981894 static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
18991895 struct btrfs_device *scrub_dev,
19001896 struct btrfs_block_group *bg,
1901− struct map_lookup *map,
1897+ struct btrfs_chunk_map *map,
19021898 u64 full_stripe_start)
19031899 {
19041900 DECLARE_COMPLETION_ONSTACK(io_done);
@@ -2067,7 +2063,7 @@ out: · 162 unmodified lines
20672063 */
20682064 static int scrub_simple_mirror(struct scrub_ctx *sctx,
20692065 struct btrfs_block_group *bg,
2070− struct map_lookup *map,
2066+ struct btrfs_chunk_map *map,
20712067 u64 logical_start, u64 logical_length,
20722068 struct btrfs_device *device,
20732069 u64 physical, int mirror_num)
@@ -2128,7 +2124,7 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, · 54 unmodified lines
21282124 }
21292125
21302126 /* Calculate the full stripe length for simple stripe based profiles */
2131−static u64 simple_stripe_full_stripe_len(const struct map_lookup *map)
2127+static u64 simple_stripe_full_stripe_len(const struct btrfs_chunk_map *map)
21322128 {
21332129 ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
21342130 BTRFS_BLOCK_GROUP_RAID10));
@@ -2137,7 +2133,7 @@ static u64 simple_stripe_full_stripe_len(const struct map_lookup *map) · 2 unmodified lines
21372133 }
21382134
21392135 /* Get the logical bytenr for the stripe */
2140−static u64 simple_stripe_get_logical(struct map_lookup *map,
2136+static u64 simple_stripe_get_logical(struct btrfs_chunk_map *map,
21412137 struct btrfs_block_group *bg,
21422138 int stripe_index)
21432139 {
@@ -2154,7 +2150,7 @@ static u64 simple_stripe_get_logical(struct map_lookup *map, · 10 unmodified lines
21542150 }
21552151
21562152 /* Get the mirror number for the stripe */
2157−static int simple_stripe_mirror_num(struct map_lookup *map, int stripe_index)
2153+static int simple_stripe_mirror_num(struct btrfs_chunk_map *map, int stripe_index)
21582154 {
21592155 ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
21602156 BTRFS_BLOCK_GROUP_RAID10));
@@ -2166,7 +2162,7 @@ static int simple_stripe_mirror_num(struct map_lookup *map, int stripe_index) · 5 unmodified lines
21662162
21672163 static int scrub_simple_stripe(struct scrub_ctx *sctx,
21682164 struct btrfs_block_group *bg,
2169− struct map_lookup *map,
2165+ struct btrfs_chunk_map *map,
21702166 struct btrfs_device *device,
21712167 int stripe_index)
21722168 {
@@ -2199,18 +2195,17 @@ static int scrub_simple_stripe(struct scrub_ctx *sctx, · 26 unmodified lines
21992195
22002196 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
22012197 struct btrfs_block_group *bg,
2202− struct extent_map *em,
2198+ struct btrfs_chunk_map *map,
22032199 struct btrfs_device *scrub_dev,
22042200 int stripe_index)
22052201 {
22062202 struct btrfs_fs_info *fs_info = sctx->fs_info;
2207− struct map_lookup *map = em->map_lookup;
22082203 const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
22092204 const u64 chunk_logical = bg->start;
22102205 int ret;
22112206 int ret2;
22122207 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);
22142209 const u64 physical_end = physical + dev_stripe_len;
22152210 u64 logical;
22162211 u64 logic_end;
@@ -2373,17 +2368,12 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, · 156 unmodified lines
23732368 u64 dev_extent_len)
23742369 {
23752370 struct btrfs_fs_info *fs_info = sctx->fs_info;
2376− struct extent_map_tree *map_tree = &fs_info->mapping_tree;
2377− struct map_lookup *map;
2378− struct extent_map *em;
2371+ struct btrfs_chunk_map *map;
23792372 int i;
23802373 int ret = 0;
23812374
2382− read_lock(&map_tree->lock);
2383− em = lookup_extent_mapping(map_tree, bg->start, bg->length);
2384− read_unlock(&map_tree->lock);
2385−
2386− if (!em) {
2375+ map = btrfs_find_chunk_map(fs_info, bg->start, bg->length);
2376+ if (!map) {
23872377 /*
23882378 * Might have been an unused block group deleted by the cleaner
23892379 * kthread or relocation.
@@ -2395,22 +2385,21 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, · 5 unmodified lines
23952385
23962386 return ret;
23972387 }
2398− if (em->start != bg->start)
2388+ if (map->start != bg->start)
23992389 goto out;
2400− if (em->len < dev_extent_len)
2390+ if (map->chunk_len < dev_extent_len)
24012391 goto out;
24022392
2403− map = em->map_lookup;
24042393 for (i = 0; i < map->num_stripes; ++i) {
24052394 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
24062395 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);
24082397 if (ret)
24092398 goto out;
24102399 }
24112400 }
24122401 out:
2413− free_extent_map(em);
2402+ btrfs_free_chunk_map(map);
24142403
24152404 return ret;
24162405 }
fs/btrfs/subpage.c+192−181
@@ -64,7 +64,7 @@ · 63 unmodified lines
6464 * This means a slightly higher tree locking latency.
6565 */
6666
67−bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page)
67+bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping)
6868 {
6969 if (fs_info->sectorsize >= PAGE_SIZE)
7070 return false;
@@ -74,8 +74,7 @@ bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page) · 3 unmodified lines
7474 * mapping. And if page->mapping->host is data inode, it's subpage.
7575 * As we have ruled our sectorsize >= PAGE_SIZE case already.
7676 */
77− if (!page->mapping || !page->mapping->host ||
78− is_data_inode(page->mapping->host))
77+ if (!mapping || !mapping->host || is_data_inode(mapping->host))
7978 return true;
8079
8180 /*
@@ -116,7 +115,7 @@ void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sector · 34 unmodified lines
116115 }
117116
118117 int 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)
120119 {
121120 struct btrfs_subpage *subpage;
122121
@@ -124,31 +123,30 @@ int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info, · 1 unmodified line
124123 * We have cases like a dummy extent buffer page, which is not mapped
125124 * and doesn't need to be locked.
126125 */
127− if (page->mapping)
128− ASSERT(PageLocked(page));
126+ if (folio->mapping)
127+ ASSERT(folio_test_locked(folio));
129128
130− /* Either not subpage, or the page already has private attached */
131− if (!btrfs_is_subpage(fs_info, page) || PagePrivate(page))
129+ /* Either not subpage, or the folio already has private attached. */
130+ if (!btrfs_is_subpage(fs_info, folio->mapping) || folio_test_private(folio))
132131 return 0;
133132
134133 subpage = btrfs_alloc_subpage(fs_info, type);
135134 if (IS_ERR(subpage))
136135 return PTR_ERR(subpage);
137136
138− attach_page_private(page, subpage);
137+ folio_attach_private(folio, subpage);
139138 return 0;
140139 }
141140
142−void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
143− struct page *page)
141+void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio)
144142 {
145143 struct btrfs_subpage *subpage;
146144
147− /* Either not subpage, or already detached */
148− if (!btrfs_is_subpage(fs_info, page) || !PagePrivate(page))
145+ /* Either not subpage, or the folio already has private attached. */
146+ if (!btrfs_is_subpage(fs_info, folio->mapping) || !folio_test_private(folio))
149147 return;
150148
151− subpage = detach_page_private(page);
149+ subpage = folio_detach_private(folio);
152150 ASSERT(subpage);
153151 btrfs_free_subpage(subpage);
154152 }
@@ -188,77 +186,78 @@ void btrfs_free_subpage(struct btrfs_subpage *subpage) · 33 unmodified lines
188186 * This is important for eb allocation, to prevent race with last eb freeing
189187 * of the same page.
190188 * 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 still
189+ * detach_extent_buffer_page() won't detach the folio private while we're still
192190 * allocating the extent buffer.
193191 */
194−void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
195− struct page *page)
192+void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio)
196193 {
197194 struct btrfs_subpage *subpage;
198195
199− if (!btrfs_is_subpage(fs_info, page))
196+ if (!btrfs_is_subpage(fs_info, folio->mapping))
200197 return;
201198
202− ASSERT(PagePrivate(page) && page->mapping);
203− lockdep_assert_held(&page->mapping->i_private_lock);
199+ ASSERT(folio_test_private(folio) && folio->mapping);
200+ lockdep_assert_held(&folio->mapping->i_private_lock);
204201
205− subpage = (struct btrfs_subpage *)page->private;
202+ subpage = folio_get_private(folio);
206203 atomic_inc(&subpage->eb_refs);
207204 }
208205
209−void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
210− struct page *page)
206+void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio)
211207 {
212208 struct btrfs_subpage *subpage;
213209
214− if (!btrfs_is_subpage(fs_info, page))
210+ if (!btrfs_is_subpage(fs_info, folio->mapping))
215211 return;
216212
217− ASSERT(PagePrivate(page) && page->mapping);
218− lockdep_assert_held(&page->mapping->i_private_lock);
213+ ASSERT(folio_test_private(folio) && folio->mapping);
214+ lockdep_assert_held(&folio->mapping->i_private_lock);
219215
220− subpage = (struct btrfs_subpage *)page->private;
216+ subpage = folio_get_private(folio);
221217 ASSERT(atomic_read(&subpage->eb_refs));
222218 atomic_dec(&subpage->eb_refs);
223219 }
224220
225221 static 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)
227223 {
224+ /* For subpage support, the folio must be single page. */
225+ ASSERT(folio_order(folio) == 0);
226+
228227 /* Basic checks */
229− ASSERT(PagePrivate(page) && page->private);
228+ ASSERT(folio_test_private(folio) && folio_get_private(folio));
230229 ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
231230 IS_ALIGNED(len, fs_info->sectorsize));
232231 /*
233232 * The range check only works for mapped page, we can still have
234233 * unmapped page like dummy extent buffer pages.
235234 */
236− if (page->mapping)
237− ASSERT(page_offset(page) <= start &&
238− start + len <= page_offset(page) + PAGE_SIZE);
235+ if (folio->mapping)
236+ ASSERT(folio_pos(folio) <= start &&
237+ start + len <= folio_pos(folio) + PAGE_SIZE);
239238 }
240239
241240 void 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)
243242 {
244− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
243+ struct btrfs_subpage *subpage = folio_get_private(folio);
245244 const int nbits = len >> fs_info->sectorsize_bits;
246245
247− btrfs_subpage_assert(fs_info, page, start, len);
246+ btrfs_subpage_assert(fs_info, folio, start, len);
248247
249248 atomic_add(nbits, &subpage->readers);
250249 }
251250
252251 void 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)
254253 {
255− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
254+ struct btrfs_subpage *subpage = folio_get_private(folio);
256255 const int nbits = len >> fs_info->sectorsize_bits;
257256 bool is_data;
258257 bool last;
259258
260− btrfs_subpage_assert(fs_info, page, start, len);
261− is_data = is_data_inode(page->mapping->host);
259+ btrfs_subpage_assert(fs_info, folio, start, len);
260+ is_data = is_data_inode(folio->mapping->host);
262261 ASSERT(atomic_read(&subpage->readers) >= nbits);
263262 last = atomic_sub_and_test(nbits, &subpage->readers);
264263
@@ -270,35 +269,35 @@ void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info, · 5 unmodified lines
270269 * As we want the atomic_sub_and_test() to be always executed.
271270 */
272271 if (is_data && last)
273− unlock_page(page);
272+ folio_unlock(folio);
274273 }
275274
276−static void btrfs_subpage_clamp_range(struct page *page, u64 *start, u32 *len)
275+static void btrfs_subpage_clamp_range(struct folio *folio, u64 *start, u32 *len)
277276 {
278277 u64 orig_start = *start;
279278 u32 orig_len = *len;
280279
281− *start = max_t(u64, page_offset(page), orig_start);
280+ *start = max_t(u64, folio_pos(folio), orig_start);
282281 /*
283282 * For certain call sites like btrfs_drop_pages(), we may have pages
284283 * beyond the target range. In that case, just set @len to 0, subpage
285284 * helpers can handle @len == 0 without any problem.
286285 */
287− if (page_offset(page) >= orig_start + orig_len)
286+ if (folio_pos(folio) >= orig_start + orig_len)
288287 *len = 0;
289288 else
290− *len = min_t(u64, page_offset(page) + PAGE_SIZE,
289+ *len = min_t(u64, folio_pos(folio) + PAGE_SIZE,
291290 orig_start + orig_len) - *start;
292291 }
293292
294293 void 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)
296295 {
297− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
296+ struct btrfs_subpage *subpage = folio_get_private(folio);
298297 const int nbits = (len >> fs_info->sectorsize_bits);
299298 int ret;
300299
301− btrfs_subpage_assert(fs_info, page, start, len);
300+ btrfs_subpage_assert(fs_info, folio, start, len);
302301
303302 ASSERT(atomic_read(&subpage->readers) == 0);
304303 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
306305 }
307306
308307 bool 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)
310309 {
311− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
310+ struct btrfs_subpage *subpage = folio_get_private(folio);
312311 const int nbits = (len >> fs_info->sectorsize_bits);
313312
314− btrfs_subpage_assert(fs_info, page, start, len);
313+ btrfs_subpage_assert(fs_info, folio, start, len);
315314
316315 /*
317316 * 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
328327 }
329328
330329 /*
331− * Lock a page for delalloc page writeback.
330+ * Lock a folio for delalloc page writeback.
332331 *
333332 * Return -EAGAIN if the page is not properly initialized.
334333 * 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
337336 * it's really the correct page, as the caller is using
338337 * filemap_get_folios_contig(), which can race with page invalidating.
339338 */
340−int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
341− struct page *page, u64 start, u32 len)
339+int btrfs_folio_start_writer_lock(const struct btrfs_fs_info *fs_info,
340+ struct folio *folio, u64 start, u32 len)
342341 {
343− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) {
344− lock_page(page);
342+ if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping)) {
343+ folio_lock(folio);
345344 return 0;
346345 }
347− lock_page(page);
348− if (!PagePrivate(page) || !page->private) {
349− unlock_page(page);
346+ folio_lock(folio);
347+ if (!folio_test_private(folio) || !folio_get_private(folio)) {
348+ folio_unlock(folio);
350349 return -EAGAIN;
351350 }
352− btrfs_subpage_clamp_range(page, &start, &len);
353− btrfs_subpage_start_writer(fs_info, page, start, len);
351+ btrfs_subpage_clamp_range(folio, &start, &len);
352+ btrfs_subpage_start_writer(fs_info, folio, start, len);
354353 return 0;
355354 }
356355
357−void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
358− struct page *page, u64 start, u32 len)
356+void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
357+ struct folio *folio, u64 start, u32 len)
359358 {
360− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page))
361− return unlock_page(page);
362− btrfs_subpage_clamp_range(page, &start, &len);
363− if (btrfs_subpage_end_and_test_writer(fs_info, page, start, len))
364− unlock_page(page);
359+ if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping)) {
360+ folio_unlock(folio);
361+ return;
362+ }
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);
365366 }
366367
367−#define subpage_calc_start_bit(fs_info, page, name, start, len) \
368+#define subpage_calc_start_bit(fs_info, folio, name, start, len) \
368369 ({ \
369370 unsigned int start_bit; \
370371 \
371− btrfs_subpage_assert(fs_info, page, start, len); \
372+ btrfs_subpage_assert(fs_info, folio, start, len); \
372373 start_bit = offset_in_page(start) >> fs_info->sectorsize_bits; \
373374 start_bit += fs_info->subpage_info->name##_offset; \
374375 start_bit; \
@@ -385,46 +386,46 @@ void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, · 10 unmodified lines
385386 fs_info->subpage_info->bitmap_nr_bits)
386387
387388 void 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)
389390 {
390− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
391− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
391+ struct btrfs_subpage *subpage = folio_get_private(folio);
392+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
392393 uptodate, start, len);
393394 unsigned long flags;
394395
395396 spin_lock_irqsave(&subpage->lock, flags);
396397 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
397398 if (subpage_test_bitmap_all_set(fs_info, subpage, uptodate))
398− SetPageUptodate(page);
399+ folio_mark_uptodate(folio);
399400 spin_unlock_irqrestore(&subpage->lock, flags);
400401 }
401402
402403 void 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)
404405 {
405− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
406− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
406+ struct btrfs_subpage *subpage = folio_get_private(folio);
407+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
407408 uptodate, start, len);
408409 unsigned long flags;
409410
410411 spin_lock_irqsave(&subpage->lock, flags);
411412 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
412− ClearPageUptodate(page);
413+ folio_clear_uptodate(folio);
413414 spin_unlock_irqrestore(&subpage->lock, flags);
414415 }
415416
416417 void 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)
418419 {
419− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
420− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
420+ struct btrfs_subpage *subpage = folio_get_private(folio);
421+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
421422 dirty, start, len);
422423 unsigned long flags;
423424
424425 spin_lock_irqsave(&subpage->lock, flags);
425426 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
426427 spin_unlock_irqrestore(&subpage->lock, flags);
427− set_page_dirty(page);
428+ folio_mark_dirty(folio);
428429 }
429430
430431 /*
@@ -438,10 +439,10 @@ void btrfs_subpage_set_dirty(const struct btrfs_fs_info *fs_info, · 7 unmodified lines
438439 * extra handling for tree blocks.
439440 */
440441 bool 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)
442443 {
443− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
444− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
444+ struct btrfs_subpage *subpage = folio_get_private(folio);
445+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
445446 dirty, start, len);
446447 unsigned long flags;
447448 bool last = false;
@@ -455,101 +456,101 @@ bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info, · 7 unmodified lines
455456 }
456457
457458 void 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)
459460 {
460461 bool last;
461462
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);
463464 if (last)
464− clear_page_dirty_for_io(page);
465+ folio_clear_dirty_for_io(folio);
465466 }
466467
467468 void 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)
469470 {
470− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
471− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
471+ struct btrfs_subpage *subpage = folio_get_private(folio);
472+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
472473 writeback, start, len);
473474 unsigned long flags;
474475
475476 spin_lock_irqsave(&subpage->lock, flags);
476477 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
477− set_page_writeback(page);
478+ folio_start_writeback(folio);
478479 spin_unlock_irqrestore(&subpage->lock, flags);
479480 }
480481
481482 void 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)
483484 {
484− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
485− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
485+ struct btrfs_subpage *subpage = folio_get_private(folio);
486+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
486487 writeback, start, len);
487488 unsigned long flags;
488489
489490 spin_lock_irqsave(&subpage->lock, flags);
490491 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
491492 if (subpage_test_bitmap_all_zero(fs_info, subpage, writeback)) {
492− ASSERT(PageWriteback(page));
493− end_page_writeback(page);
493+ ASSERT(folio_test_writeback(folio));
494+ folio_end_writeback(folio);
494495 }
495496 spin_unlock_irqrestore(&subpage->lock, flags);
496497 }
497498
498499 void 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)
500501 {
501− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
502− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
502+ struct btrfs_subpage *subpage = folio_get_private(folio);
503+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
503504 ordered, start, len);
504505 unsigned long flags;
505506
506507 spin_lock_irqsave(&subpage->lock, flags);
507508 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
508− SetPageOrdered(page);
509+ folio_set_ordered(folio);
509510 spin_unlock_irqrestore(&subpage->lock, flags);
510511 }
511512
512513 void 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)
514515 {
515− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
516− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
516+ struct btrfs_subpage *subpage = folio_get_private(folio);
517+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
517518 ordered, start, len);
518519 unsigned long flags;
519520
520521 spin_lock_irqsave(&subpage->lock, flags);
521522 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
522523 if (subpage_test_bitmap_all_zero(fs_info, subpage, ordered))
523− ClearPageOrdered(page);
524+ folio_clear_ordered(folio);
524525 spin_unlock_irqrestore(&subpage->lock, flags);
525526 }
526527
527528 void 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)
529530 {
530− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
531− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
531+ struct btrfs_subpage *subpage = folio_get_private(folio);
532+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
532533 checked, start, len);
533534 unsigned long flags;
534535
535536 spin_lock_irqsave(&subpage->lock, flags);
536537 bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
537538 if (subpage_test_bitmap_all_set(fs_info, subpage, checked))
538− SetPageChecked(page);
539+ folio_set_checked(folio);
539540 spin_unlock_irqrestore(&subpage->lock, flags);
540541 }
541542
542543 void 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)
544545 {
545− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
546− unsigned int start_bit = subpage_calc_start_bit(fs_info, page,
546+ struct btrfs_subpage *subpage = folio_get_private(folio);
547+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
547548 checked, start, len);
548549 unsigned long flags;
549550
550551 spin_lock_irqsave(&subpage->lock, flags);
551552 bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
552− ClearPageChecked(page);
553+ folio_clear_checked(folio);
553554 spin_unlock_irqrestore(&subpage->lock, flags);
554555 }
555556
@@ -559,10 +560,10 @@ void btrfs_subpage_clear_checked(const struct btrfs_fs_info *fs_info, · 3 unmodified lines
559560 */
560561 #define IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(name) \
561562 bool 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) \
563564 { \
564− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; \
565− unsigned int start_bit = subpage_calc_start_bit(fs_info, page, \
565+ struct btrfs_subpage *subpage = folio_get_private(folio); \
566+ unsigned int start_bit = subpage_calc_start_bit(fs_info, folio, \
566567 name, start, len); \
567568 unsigned long flags; \
568569 bool ret; \
@@ -584,88 +585,94 @@ IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(checked); · 15 unmodified lines
584585 * in. We only test sectorsize == PAGE_SIZE cases so far, thus we can fall
585586 * back to regular sectorsize branch.
586587 */
587−#define IMPLEMENT_BTRFS_PAGE_OPS(name, set_page_func, clear_page_func, \
588− test_page_func) \
589−void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \
590− struct page *page, u64 start, u32 len) \
588+#define IMPLEMENT_BTRFS_PAGE_OPS(name, folio_set_func, \
589+ folio_clear_func, folio_test_func) \
590+void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
591+ struct folio *folio, u64 start, u32 len) \
591592 { \
592− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \
593− set_page_func(page); \
593+ if (unlikely(!fs_info) || \
594+ !btrfs_is_subpage(fs_info, folio->mapping)) { \
595+ folio_set_func(folio); \
594596 return; \
595597 } \
596− btrfs_subpage_set_##name(fs_info, page, start, len); \
598+ btrfs_subpage_set_##name(fs_info, folio, start, len); \
597599 } \
598−void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \
599− struct page *page, u64 start, u32 len) \
600+void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
601+ struct folio *folio, u64 start, u32 len) \
600602 { \
601− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \
602− clear_page_func(page); \
603+ if (unlikely(!fs_info) || \
604+ !btrfs_is_subpage(fs_info, folio->mapping)) { \
605+ folio_clear_func(folio); \
603606 return; \
604607 } \
605− btrfs_subpage_clear_##name(fs_info, page, start, len); \
608+ btrfs_subpage_clear_##name(fs_info, folio, start, len); \
606609 } \
607−bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \
608− struct page *page, u64 start, u32 len) \
610+bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
611+ struct folio *folio, u64 start, u32 len) \
609612 { \
610− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) \
611− return test_page_func(page); \
612− return btrfs_subpage_test_##name(fs_info, page, start, len); \
613+ if (unlikely(!fs_info) || \
614+ !btrfs_is_subpage(fs_info, folio->mapping)) \
615+ return folio_test_func(folio); \
616+ return btrfs_subpage_test_##name(fs_info, folio, start, len); \
613617 } \
614−void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
615− struct page *page, u64 start, u32 len) \
618+void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
619+ struct folio *folio, u64 start, u32 len) \
616620 { \
617− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \
618− set_page_func(page); \
621+ if (unlikely(!fs_info) || \
622+ !btrfs_is_subpage(fs_info, folio->mapping)) { \
623+ folio_set_func(folio); \
619624 return; \
620625 } \
621− btrfs_subpage_clamp_range(page, &start, &len); \
622− btrfs_subpage_set_##name(fs_info, page, start, len); \
626+ btrfs_subpage_clamp_range(folio, &start, &len); \
627+ btrfs_subpage_set_##name(fs_info, folio, start, len); \
623628 } \
624−void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
625− struct page *page, u64 start, u32 len) \
629+void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
630+ struct folio *folio, u64 start, u32 len) \
626631 { \
627− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) { \
628− clear_page_func(page); \
632+ if (unlikely(!fs_info) || \
633+ !btrfs_is_subpage(fs_info, folio->mapping)) { \
634+ folio_clear_func(folio); \
629635 return; \
630636 } \
631− btrfs_subpage_clamp_range(page, &start, &len); \
632− btrfs_subpage_clear_##name(fs_info, page, start, len); \
637+ btrfs_subpage_clamp_range(folio, &start, &len); \
638+ btrfs_subpage_clear_##name(fs_info, folio, start, len); \
633639 } \
634−bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
635− struct page *page, u64 start, u32 len) \
640+bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
641+ struct folio *folio, u64 start, u32 len) \
636642 { \
637− if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, page)) \
638− return test_page_func(page); \
639− btrfs_subpage_clamp_range(page, &start, &len); \
640− return btrfs_subpage_test_##name(fs_info, page, start, len); \
641−}
642−IMPLEMENT_BTRFS_PAGE_OPS(uptodate, SetPageUptodate, ClearPageUptodate,
643− PageUptodate);
644−IMPLEMENT_BTRFS_PAGE_OPS(dirty, set_page_dirty, clear_page_dirty_for_io,
645− PageDirty);
646−IMPLEMENT_BTRFS_PAGE_OPS(writeback, set_page_writeback, end_page_writeback,
647− PageWriteback);
648−IMPLEMENT_BTRFS_PAGE_OPS(ordered, SetPageOrdered, ClearPageOrdered,
649− PageOrdered);
650−IMPLEMENT_BTRFS_PAGE_OPS(checked, SetPageChecked, ClearPageChecked, PageChecked);
643+ if (unlikely(!fs_info) || \
644+ !btrfs_is_subpage(fs_info, folio->mapping)) \
645+ return folio_test_func(folio); \
646+ btrfs_subpage_clamp_range(folio, &start, &len); \
647+ return btrfs_subpage_test_##name(fs_info, folio, start, len); \
648+}
649+IMPLEMENT_BTRFS_PAGE_OPS(uptodate, folio_mark_uptodate, folio_clear_uptodate,
650+ folio_test_uptodate);
651+IMPLEMENT_BTRFS_PAGE_OPS(dirty, folio_mark_dirty, folio_clear_dirty_for_io,
652+ folio_test_dirty);
653+IMPLEMENT_BTRFS_PAGE_OPS(writeback, folio_start_writeback, folio_end_writeback,
654+ folio_test_writeback);
655+IMPLEMENT_BTRFS_PAGE_OPS(ordered, folio_set_ordered, folio_clear_ordered,
656+ folio_test_ordered);
657+IMPLEMENT_BTRFS_PAGE_OPS(checked, folio_set_checked, folio_clear_checked,
658+ folio_test_checked);
651659
652660 /*
653661 * Make sure not only the page dirty bit is cleared, but also subpage dirty bit
654662 * is cleared.
655663 */
656−void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,
657− struct page *page)
664+void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info, struct folio *folio)
658665 {
659− struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private;
666+ struct btrfs_subpage *subpage = folio_get_private(folio);
660667
661668 if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
662669 return;
663670
664− ASSERT(!PageDirty(page));
665− if (!btrfs_is_subpage(fs_info, page))
671+ ASSERT(!folio_test_dirty(folio));
672+ if (!btrfs_is_subpage(fs_info, folio->mapping))
666673 return;
667674
668− ASSERT(PagePrivate(page) && page->private);
675+ ASSERT(folio_test_private(folio) && folio_get_private(folio));
669676 ASSERT(subpage_test_bitmap_all_zero(fs_info, subpage, dirty));
670677 }
671678
@@ -684,18 +691,20 @@ void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info, · 12 unmodified lines
684691 * extent_write_locked_range().
685692 * In this case, we have to call subpage helper to handle the case.
686693 */
687−void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page,
688− u64 start, u32 len)
694+void btrfs_folio_unlock_writer(struct btrfs_fs_info *fs_info,
695+ struct folio *folio, u64 start, u32 len)
689696 {
690697 struct btrfs_subpage *subpage;
691698
692− ASSERT(PageLocked(page));
699+ ASSERT(folio_test_locked(folio));
693700 /* For non-subpage case, we just unlock the page */
694− if (!btrfs_is_subpage(fs_info, page))
695− return unlock_page(page);
701+ if (!btrfs_is_subpage(fs_info, folio->mapping)) {
702+ folio_unlock(folio);
703+ return;
704+ }
696705
697− ASSERT(PagePrivate(page) && page->private);
698− subpage = (struct btrfs_subpage *)page->private;
706+ ASSERT(folio_test_private(folio) && folio_get_private(folio));
707+ subpage = folio_get_private(folio);
699708
700709 /*
701710 * 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
704713 * Since we own the page lock, no one else could touch subpage::writers
705714 * and we are safe to do several atomic operations without spinlock.
706715 */
707− if (atomic_read(&subpage->writers) == 0)
716+ if (atomic_read(&subpage->writers) == 0) {
708717 /* No writers, locked by plain lock_page() */
709− return unlock_page(page);
718+ folio_unlock(folio);
719+ return;
720+ }
710721
711722 /* 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);
713724 }
714725
715726 #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
717728 subpage_info->name##_offset, subpage_info->bitmap_nr_bits)
718729
719730 void __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)
721732 {
722733 struct btrfs_subpage_info *subpage_info = fs_info->subpage_info;
723734 struct btrfs_subpage *subpage;
@@ -729,9 +740,9 @@ void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info, · 5 unmodified lines
729740 unsigned long checked_bitmap;
730741 unsigned long flags;
731742
732− ASSERT(PagePrivate(page) && page->private);
743+ ASSERT(folio_test_private(folio) && folio_get_private(folio));
733744 ASSERT(subpage_info);
734− subpage = (struct btrfs_subpage *)page->private;
745+ subpage = folio_get_private(folio);
735746
736747 spin_lock_irqsave(&subpage->lock, flags);
737748 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
741752 GET_SUBPAGE_BITMAP(subpage, subpage_info, checked, &checked_bitmap);
742753 spin_unlock_irqrestore(&subpage->lock, flags);
743754
744− dump_page(page, "btrfs subpage dump");
755+ dump_page(folio_page(folio, 0), "btrfs subpage dump");
745756 btrfs_warn(fs_info,
746757 "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),
748759 subpage_info->bitmap_nr_bits, &uptodate_bitmap,
749760 subpage_info->bitmap_nr_bits, &error_bitmap,
750761 subpage_info->bitmap_nr_bits, &dirty_bitmap,
fs/btrfs/subpage.h+39−43
@@ -73,71 +73,68 @@ enum btrfs_subpage_type { · 72 unmodified lines
7373 BTRFS_SUBPAGE_DATA,
7474 };
7575
76−bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page);
76+bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);
7777
7878 void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sectorsize);
7979 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
80− struct page *page, enum btrfs_subpage_type type);
81−void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
82− struct page *page);
80+ struct folio *folio, enum btrfs_subpage_type type);
81+void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio);
8382
8483 /* Allocate additional data where page represents more than one sector */
8584 struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
8685 enum btrfs_subpage_type type);
8786 void btrfs_free_subpage(struct btrfs_subpage *subpage);
8887
89−void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
90− struct page *page);
91−void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
92− struct page *page);
88+void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
89+void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
9390
9491 void 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);
9693 void 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);
9895
9996 void 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);
10198 bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
102− struct page *page, u64 start, u32 len);
103−int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
104− struct page *page, u64 start, u32 len);
105−void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
106− struct page *page, u64 start, u32 len);
99+ struct folio *folio, u64 start, u32 len);
100+int btrfs_folio_start_writer_lock(const struct btrfs_fs_info *fs_info,
101+ struct folio *folio, u64 start, u32 len);
102+void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
103+ struct folio *folio, u64 start, u32 len);
107104
108105 /*
109106 * Template for subpage related operations.
110107 *
111− * btrfs_subpage_*() are for call sites where the page has subpage attached and
112− * the range is ensured to be inside the page.
108+ * btrfs_subpage_*() are for call sites where the folio has subpage attached and
109+ * the range is ensured to be inside the folio's single page.
113110 *
114− * btrfs_page_*() are for call sites where the page can either be subpage
115− * specific or regular page. The function will handle both cases.
116− * But the range still needs to be inside the page.
111+ * btrfs_folio_*() are for call sites where the page can either be subpage
112+ * specific or regular folios. The function will handle both cases.
113+ * But the range still needs to be inside one single page.
117114 *
118− * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't
115+ * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
119116 * need to be inside the page. Those functions will truncate the range
120117 * automatically.
121118 */
122119 #define DECLARE_BTRFS_SUBPAGE_OPS(name) \
123120 void 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); \
125122 void 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); \
127124 bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
128− struct page *page, u64 start, u32 len); \
129−void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \
130− struct page *page, u64 start, u32 len); \
131−void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \
132− struct page *page, u64 start, u32 len); \
133−bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \
134− struct page *page, u64 start, u32 len); \
135−void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
136− struct page *page, u64 start, u32 len); \
137−void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
138− struct page *page, u64 start, u32 len); \
139−bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
140− struct page *page, u64 start, u32 len);
125+ struct folio *folio, u64 start, u32 len); \
126+void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
127+ struct folio *folio, u64 start, u32 len); \
128+void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
129+ struct folio *folio, u64 start, u32 len); \
130+bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
131+ struct folio *folio, u64 start, u32 len); \
132+void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
133+ struct folio *folio, u64 start, u32 len); \
134+void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
135+ struct folio *folio, u64 start, u32 len); \
136+bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
137+ struct folio *folio, u64 start, u32 len);
141138
142139 DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
143140 DECLARE_BTRFS_SUBPAGE_OPS(dirty);
@@ -146,13 +143,12 @@ DECLARE_BTRFS_SUBPAGE_OPS(ordered); · 2 unmodified lines
146143 DECLARE_BTRFS_SUBPAGE_OPS(checked);
147144
148145 bool 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);
150147
151−void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,
152− struct page *page);
153−void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page,
154− u64 start, u32 len);
148+void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info, struct folio *folio);
149+void btrfs_folio_unlock_writer(struct btrfs_fs_info *fs_info,
150+ struct folio *folio, u64 start, u32 len);
155151 void __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);
157153
158154 #endif
fs/btrfs/super.c+1195−1150
@@ -27,6 +27,7 @@ · 26 unmodified lines
2727 #include <linux/crc32c.h>
2828 #include <linux/btrfs.h>
2929 #include <linux/security.h>
30+#include <linux/fs_parser.h>
3031 #include "messages.h"
3132 #include "delayed-inode.h"
3233 #include "ctree.h"
@@ -64,19 +65,7 @@ · 31 unmodified lines
6465 #include <trace/events/btrfs.h>
6566
6667 static 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− */
7668 static struct file_system_type btrfs_fs_type;
77−static struct file_system_type btrfs_root_fs_type;
78−
79−static int btrfs_remount(struct super_block *sb, int *flags, char *data);
8069
8170 static void btrfs_put_super(struct super_block *sb)
8271 {
@@ -86,8 +75,22 @@ static void btrfs_put_super(struct super_block *sb) · 3 unmodified lines
8675 close_ctree(fs_info);
8776 }
8877
78+/* Store the mount options related information. */
79+struct 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+
8992 enum {
90− Opt_acl, Opt_noacl,
93+ Opt_acl,
9194 Opt_clear_cache,
9295 Opt_commit_interval,
9396 Opt_compress,
@@ -97,27 +100,26 @@ enum { · 3 unmodified lines
97100 Opt_degraded,
98101 Opt_device,
99102 Opt_fatal_errors,
100− Opt_flushoncommit, Opt_noflushoncommit,
103+ Opt_flushoncommit,
101104 Opt_max_inline,
102− Opt_barrier, Opt_nobarrier,
103− Opt_datacow, Opt_nodatacow,
104− Opt_datasum, Opt_nodatasum,
105− Opt_defrag, Opt_nodefrag,
106− Opt_discard, Opt_nodiscard,
105+ Opt_barrier,
106+ Opt_datacow,
107+ Opt_datasum,
108+ Opt_defrag,
109+ Opt_discard,
107110 Opt_discard_mode,
108− Opt_norecovery,
109111 Opt_ratio,
110112 Opt_rescan_uuid_tree,
111113 Opt_skip_balance,
112− Opt_space_cache, Opt_no_space_cache,
114+ Opt_space_cache,
113115 Opt_space_cache_version,
114− Opt_ssd, Opt_nossd,
115− Opt_ssd_spread, Opt_nossd_spread,
116+ Opt_ssd,
117+ Opt_ssd_spread,
116118 Opt_subvol,
117119 Opt_subvol_empty,
118120 Opt_subvolid,
119121 Opt_thread_pool,
120− Opt_treelog, Opt_notreelog,
122+ Opt_treelog,
121123 Opt_user_subvol_rm_allowed,
122124
123125 /* Rescue options */
@@ -128,14 +130,10 @@ enum { · 4 unmodified lines
128130 Opt_ignoredatacsums,
129131 Opt_rescue_all,
130132
131− /* Deprecated options */
132− Opt_recovery,
133− Opt_inode_cache, Opt_noinode_cache,
134−
135133 /* Debugging options */
136− Opt_enospc_debug, Opt_noenospc_debug,
134+ Opt_enospc_debug,
137135 #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,
139137 #endif
140138 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
141139 Opt_ref_verify,
@@ -143,785 +141,611 @@ enum { · 1 unmodified line
143141 Opt_err,
144142 };
145143
146−static const match_table_t tokens = {
147− {Opt_acl, "acl"},
148− {Opt_noacl, "noacl"},
149− {Opt_clear_cache, "clear_cache"},
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"},
144+enum {
145+ Opt_fatal_errors_panic,
146+ Opt_fatal_errors_bug,
147+};
192148
193− /* Rescue options */
194− {Opt_rescue, "rescue=%s"},
149+static const struct constant_table btrfs_parameter_fatal_errors[] = {
150+ { "panic", Opt_fatal_errors_panic },
151+ { "bug", Opt_fatal_errors_bug },
152+ {}
153+};
154+
155+enum {
156+ Opt_discard_sync,
157+ Opt_discard_async,
158+};
159+
160+static const struct constant_table btrfs_parameter_discard[] = {
161+ { "sync", Opt_discard_sync },
162+ { "async", Opt_discard_async },
163+ {}
164+};
165+
166+enum {
167+ Opt_space_cache_v1,
168+ Opt_space_cache_v2,
169+};
170+
171+static const struct constant_table btrfs_parameter_space_cache[] = {
172+ { "v1", Opt_space_cache_v1 },
173+ { "v2", Opt_space_cache_v2 },
174+ {}
175+};
176+
177+enum {
178+ Opt_rescue_usebackuproot,
179+ Opt_rescue_nologreplay,
180+ Opt_rescue_ignorebadroots,
181+ Opt_rescue_ignoredatacsums,
182+ Opt_rescue_parameter_all,
183+};
184+
185+static 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
197+enum {
198+ Opt_fragment_parameter_data,
199+ Opt_fragment_parameter_metadata,
200+ Opt_fragment_parameter_all,
201+};
202+
203+static 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+
211+static 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),
195246 /* Deprecated, with alias rescue=nologreplay */
196− {Opt_nologreplay, "nologreplay"},
247+ __fsparam(NULL, "nologreplay", Opt_nologreplay, fs_param_deprecated, NULL),
197248 /* Deprecated, with alias rescue=usebackuproot */
198− {Opt_usebackuproot, "usebackuproot"},
249+ __fsparam(NULL, "usebackuproot", Opt_usebackuproot, fs_param_deprecated, NULL),
199250
200− /* Deprecated options */
201− {Opt_recovery, "recovery"},
202−
203− /* Debugging options */
204− {Opt_enospc_debug, "enospc_debug"},
205− {Opt_noenospc_debug, "noenospc_debug"},
251+ /* Debugging options. */
252+ fsparam_flag_no("enospc_debug", Opt_enospc_debug),
206253 #ifdef CONFIG_BTRFS_DEBUG
207− {Opt_fragment_data, "fragment=data"},
208− {Opt_fragment_metadata, "fragment=metadata"},
209− {Opt_fragment_all, "fragment=all"},
254+ fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
210255 #endif
211256 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
212− {Opt_ref_verify, "ref_verify"},
257+ fsparam_flag("ref_verify", Opt_ref_verify),
213258 #endif
214− {Opt_err, NULL},
259+ {}
215260 };
216261
217−static const match_table_t rescue_tokens = {
218− {Opt_usebackuproot, "usebackuproot"},
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−
228−static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
229− const char *opt_name)
262+/* No support for restricting writes to btrfs devices yet... */
263+static inline blk_mode_t btrfs_open_mode(struct fs_context *fc)
230264 {
231− if (fs_info->mount_opt & opt) {
232− btrfs_err(fs_info, "%s must be used with ro mount option",
233− opt_name);
234− return true;
235− }
236− return false;
265+ return sb_open_mode(fc->sb_flags) & ~BLK_OPEN_RESTRICT_WRITES;
237266 }
238267
239−static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
268+static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
240269 {
241− char *opts;
242− char *orig;
243− char *p;
244− substring_t args[MAX_OPT_ARGS];
245− int ret = 0;
270+ struct btrfs_fs_context *ctx = fc->fs_private;
271+ struct fs_parse_result result;
272+ int opt;
246273
247− opts = kstrdup(options, GFP_KERNEL);
248− if (!opts)
249− return -ENOMEM;
250− orig = opts;
274+ opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
275+ if (opt < 0)
276+ return opt;
251277
252− while ((p = strsep(&opts, ":")) != NULL) {
253− int token;
278+ switch (opt) {
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;
254297
255− if (!*p)
256− continue;
257− token = match_token(p, rescue_tokens, args);
258− switch (token){
259− case Opt_usebackuproot:
260− btrfs_info(info,
261− "trying to use backup root at mount time");
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− }
298+ /* subvolid=0 means give me the original fs_tree. */
299+ if (!ctx->subvol_objectid)
300+ ctx->subvol_objectid = BTRFS_FS_TREE_OBJECTID;
301+ break;
302+ case Opt_device: {
303+ struct btrfs_device *device;
304+ blk_mode_t mode = btrfs_open_mode(fc);
292305
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;
293312 }
294−out:
295− kfree(orig);
296− return ret;
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− */
304−int 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);
313+ case Opt_datasum:
314+ if (result.negated) {
315+ btrfs_set_opt(ctx->mount_opt, NODATASUM);
326316 } 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);
328319 }
329− }
330−
331− /*
332− * Even the options are empty, we still need to do extra check
333− * against new flags
334− */
335− if (!options)
336− goto check;
337−
338− while ((p = strsep(&options, ",")) != NULL) {
339− int token;
340− if (!*p)
341− continue;
342−
343− token = match_token(p, tokens, args);
344− switch (token) {
345− case Opt_degraded:
346− btrfs_info(info, "allowing degraded mounts");
347− btrfs_set_opt(info->mount_opt, DEGRADED);
348− break;
349− case Opt_subvol:
350− case Opt_subvol_empty:
351− case Opt_subvolid:
352− case Opt_device:
353− /*
354− * These are parsed by btrfs_parse_subvol_options or
355− * btrfs_parse_device_options and can be ignored here.
356− */
357− break;
358− case Opt_nodatasum:
359− btrfs_set_and_info(info, NODATASUM,
360− "setting nodatasum");
361− break;
362− case Opt_datasum:
363− if (btrfs_test_opt(info, NODATASUM)) {
364− if (btrfs_test_opt(info, NODATACOW))
365− btrfs_info(info,
366− "setting datasum, datacow enabled");
367− else
368− btrfs_info(info, "setting datasum");
369− }
370− btrfs_clear_opt(info->mount_opt, NODATACOW);
371− btrfs_clear_opt(info->mount_opt, NODATASUM);
372− break;
373− case Opt_nodatacow:
374− if (!btrfs_test_opt(info, NODATACOW)) {
375− if (!btrfs_test_opt(info, COMPRESS) ||
376− !btrfs_test_opt(info, FORCE_COMPRESS)) {
377− btrfs_info(info,
378− "setting nodatacow, compression disabled");
379− } else {
380− btrfs_info(info, "setting nodatacow");
381− }
382− }
383− btrfs_clear_opt(info->mount_opt, COMPRESS);
384− btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
385− btrfs_set_opt(info->mount_opt, NODATACOW);
386− btrfs_set_opt(info->mount_opt, NODATASUM);
387− break;
388− case Opt_datacow:
389− btrfs_clear_and_info(info, NODATACOW,
390− "setting datacow");
391− break;
392− case Opt_compress_force:
393− case Opt_compress_force_type:
394− compress_force = true;
395− fallthrough;
396− case Opt_compress:
397− case Opt_compress_type:
398− saved_compress_type = btrfs_test_opt(info,
399− COMPRESS) ?
400− info->compress_type : BTRFS_COMPRESS_NONE;
401− saved_compress_force =
402− btrfs_test_opt(info, FORCE_COMPRESS);
403− saved_compress_level = info->compress_level;
404− if (token == Opt_compress ||
405− token == Opt_compress_force ||
406− strncmp(args[0].from, "zlib", 4) == 0) {
407− compress_type = "zlib";
408−
409− info->compress_type = BTRFS_COMPRESS_ZLIB;
410− info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
411− /*
412− * args[0] contains uninitialized data since
413− * for these tokens we don't expect any
414− * parameter.
415− */
416− if (token != Opt_compress &&
417− token != Opt_compress_force)
418− info->compress_level =
419− btrfs_compress_str2level(
420− BTRFS_COMPRESS_ZLIB,
421− args[0].from + 4);
422− btrfs_set_opt(info->mount_opt, COMPRESS);
423− btrfs_clear_opt(info->mount_opt, NODATACOW);
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:
320+ break;
321+ case Opt_datacow:
322+ if (result.negated) {
323+ btrfs_clear_opt(ctx->mount_opt, COMPRESS);
324+ btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
325+ btrfs_set_opt(ctx->mount_opt, NODATACOW);
326+ btrfs_set_opt(ctx->mount_opt, NODATASUM);
327+ } else {
328+ btrfs_clear_opt(ctx->mount_opt, NODATACOW);
329+ }
330+ break;
331+ case Opt_compress_force:
332+ case Opt_compress_force_type:
333+ btrfs_set_opt(ctx->mount_opt, FORCE_COMPRESS);
334+ fallthrough;
335+ case Opt_compress:
336+ case Opt_compress_type:
337+ if (opt == Opt_compress || opt == Opt_compress_force) {
338+ ctx->compress_type = BTRFS_COMPRESS_ZLIB;
339+ ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
340+ btrfs_set_opt(ctx->mount_opt, COMPRESS);
341+ btrfs_clear_opt(ctx->mount_opt, NODATACOW);
342+ btrfs_clear_opt(ctx->mount_opt, NODATASUM);
343+ } else if (strncmp(param->string, "zlib", 4) == 0) {
344+ ctx->compress_type = BTRFS_COMPRESS_ZLIB;
345+ ctx->compress_level =
346+ btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB,
347+ param->string + 4);
348+ btrfs_set_opt(ctx->mount_opt, COMPRESS);
349+ btrfs_clear_opt(ctx->mount_opt, NODATACOW);
350+ btrfs_clear_opt(ctx->mount_opt, NODATASUM);
351+ } else if (strncmp(param->string, "lzo", 3) == 0) {
352+ ctx->compress_type = BTRFS_COMPRESS_LZO;
353+ ctx->compress_level = 0;
354+ btrfs_set_opt(ctx->mount_opt, COMPRESS);
355+ btrfs_clear_opt(ctx->mount_opt, NODATACOW);
356+ btrfs_clear_opt(ctx->mount_opt, NODATASUM);
357+ } else if (strncmp(param->string, "zstd", 4) == 0) {
358+ ctx->compress_type = BTRFS_COMPRESS_ZSTD;
359+ ctx->compress_level =
360+ btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD,
361+ param->string + 4);
362+ btrfs_set_opt(ctx->mount_opt, COMPRESS);
363+ btrfs_clear_opt(ctx->mount_opt, NODATACOW);
364+ btrfs_clear_opt(ctx->mount_opt, NODATASUM);
365+ } else if (strncmp(param->string, "no", 2) == 0) {
366+ ctx->compress_level = 0;
367+ ctx->compress_type = 0;
368+ btrfs_clear_opt(ctx->mount_opt, COMPRESS);
369+ btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
370+ } else {
371+ btrfs_err(NULL, "unrecognized compression value %s",
372+ param->string);
373+ return -EINVAL;
374+ }
375+ break;
376+ case Opt_ssd:
377+ if (result.negated) {
378+ btrfs_set_opt(ctx->mount_opt, NOSSD);
379+ btrfs_clear_opt(ctx->mount_opt, SSD);
380+ btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
381+ } else {
382+ btrfs_set_opt(ctx->mount_opt, SSD);
383+ btrfs_clear_opt(ctx->mount_opt, NOSSD);
384+ }
385+ break;
386+ case Opt_ssd_spread:
387+ if (result.negated) {
388+ btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
389+ } else {
390+ btrfs_set_opt(ctx->mount_opt, SSD);
391+ btrfs_set_opt(ctx->mount_opt, SSD_SPREAD);
392+ btrfs_clear_opt(ctx->mount_opt, NOSSD);
393+ }
394+ break;
395+ case Opt_barrier:
396+ if (result.negated)
397+ btrfs_set_opt(ctx->mount_opt, NOBARRIER);
398+ else
399+ btrfs_clear_opt(ctx->mount_opt, NOBARRIER);
400+ break;
401+ case Opt_thread_pool:
402+ if (result.uint_32 == 0) {
403+ btrfs_err(NULL, "invalid value 0 for thread_pool");
404+ return -EINVAL;
405+ }
406+ ctx->thread_pool_size = result.uint_32;
407+ break;
408+ case Opt_max_inline:
409+ ctx->max_inline = memparse(param->string, NULL);
410+ break;
411+ case Opt_acl:
412+ if (result.negated) {
413+ fc->sb_flags &= ~SB_POSIXACL;
414+ } else {
545415 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
546− info->sb->s_flags |= SB_POSIXACL;
547− break;
416+ fc->sb_flags |= SB_POSIXACL;
548417 #else
549− btrfs_err(info, "support for ACL not compiled in!");
550− ret = -EINVAL;
551− goto out;
418+ btrfs_err(NULL, "support for ACL not compiled in");
419+ return -EINVAL;
552420 #endif
553− case Opt_noacl:
554− info->sb->s_flags &= ~SB_POSIXACL;
555− break;
556− case Opt_notreelog:
557− btrfs_set_and_info(info, NOTREELOG,
558− "disabling tree log");
559− break;
560− case Opt_treelog:
561− btrfs_clear_and_info(info, NOTREELOG,
562− "enabling tree log");
563− break;
564− case Opt_norecovery:
565− case Opt_nologreplay:
566− btrfs_warn(info,
421+ }
422+ /*
423+ * VFS limits the ability to toggle ACL on and off via remount,
424+ * despite every file system allowing this. This seems to be
425+ * an oversight since we all do, but it'll fail if we're
426+ * remounting. So don't set the mask here, we'll check it in
427+ * btrfs_reconfigure and do the toggling ourselves.
428+ */
429+ if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
430+ fc->sb_flags_mask |= SB_POSIXACL;
431+ break;
432+ case Opt_treelog:
433+ if (result.negated)
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,
567440 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
568− btrfs_set_and_info(info, NOLOGREPLAY,
569− "disabling log replay at mount time");
570− break;
571− case Opt_flushoncommit:
572− btrfs_set_and_info(info, FLUSHONCOMMIT,
573− "turning on flush-on-commit");
574− break;
575− case Opt_noflushoncommit:
576− btrfs_clear_and_info(info, FLUSHONCOMMIT,
577− "turning off flush-on-commit");
578− break;
579− case Opt_ratio:
580− ret = match_int(&args[0], &intarg);
581− if (ret) {
582− btrfs_err(info, "unrecognized metadata_ratio value %s",
583− args[0].from);
584− goto out;
585− }
586− info->metadata_ratio = intarg;
587− btrfs_info(info, "metadata ratio %u",
588− info->metadata_ratio);
589− break;
590− case Opt_discard:
591− case Opt_discard_mode:
592− if (token == Opt_discard ||
593− strcmp(args[0].from, "sync") == 0) {
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");
441+ btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
442+ break;
443+ case Opt_flushoncommit:
444+ if (result.negated)
445+ btrfs_clear_opt(ctx->mount_opt, FLUSHONCOMMIT);
446+ else
447+ btrfs_set_opt(ctx->mount_opt, FLUSHONCOMMIT);
448+ break;
449+ case Opt_ratio:
450+ ctx->metadata_ratio = result.uint_32;
451+ break;
452+ case Opt_discard:
453+ if (result.negated) {
454+ btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
455+ btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
456+ btrfs_set_opt(ctx->mount_opt, NODISCARD);
457+ } else {
458+ btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
459+ btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
460+ }
461+ break;
462+ case Opt_discard_mode:
463+ switch (result.uint_32) {
464+ case Opt_discard_sync:
465+ btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
466+ btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
677467 break;
678− case Opt_user_subvol_rm_allowed:
679− btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
468+ case Opt_discard_async:
469+ btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
470+ btrfs_set_opt(ctx->mount_opt, DISCARD_ASYNC);
680471 break;
681− case Opt_enospc_debug:
682− btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
472+ default:
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);
683494 break;
684− case Opt_noenospc_debug:
685− btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
495+ case Opt_space_cache_v2:
496+ btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
497+ btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
686498 break;
687− case Opt_defrag:
688− btrfs_set_and_info(info, AUTO_DEFRAG,
689− "enabling auto defrag");
499+ default:
500+ btrfs_err(NULL, "unrecognized space_cache value %s",
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);
690541 break;
691− case Opt_nodefrag:
692− btrfs_clear_and_info(info, AUTO_DEFRAG,
693− "disabling auto defrag");
542+ case Opt_fatal_errors_bug:
543+ btrfs_clear_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
694544 break;
695− case Opt_recovery:
696− case Opt_usebackuproot:
697− btrfs_warn(info,
698− "'%s' is deprecated, use 'rescue=usebackuproot' instead",
699− token == Opt_recovery ? "recovery" :
700− "usebackuproot");
701− btrfs_info(info,
702− "trying to use backup root at mount time");
703− btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
545+ default:
546+ btrfs_err(NULL, "unrecognized fatal_errors value %s",
547+ param->key);
548+ return -EINVAL;
549+ }
550+ break;
551+ case Opt_commit_interval:
552+ ctx->commit_interval = result.uint_32;
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);
704560 break;
705− case Opt_skip_balance:
706− btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
561+ case Opt_rescue_nologreplay:
562+ btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
707563 break;
708− case Opt_fatal_errors:
709− if (strcmp(args[0].from, "panic") == 0) {
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− }
564+ case Opt_rescue_ignorebadroots:
565+ btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
721566 break;
722− case Opt_commit_interval:
723− intarg = 0;
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;
567+ case Opt_rescue_ignoredatacsums:
568+ btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
741569 break;
742− case Opt_rescue:
743− ret = parse_rescue_options(info, args[0].from);
744− if (ret < 0) {
745− btrfs_err(info, "unrecognized rescue value %s",
746− args[0].from);
747− goto out;
748− }
570+ case Opt_rescue_parameter_all:
571+ btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
572+ btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
573+ btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
749574 break;
575+ default:
576+ btrfs_info(NULL, "unrecognized rescue option '%s'",
577+ param->key);
578+ return -EINVAL;
579+ }
580+ break;
750581 #ifdef CONFIG_BTRFS_DEBUG
751− case Opt_fragment_all:
752− btrfs_info(info, "fragmenting all space");
753− btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
754− btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
582+ case Opt_fragment:
583+ switch (result.uint_32) {
584+ case Opt_fragment_parameter_all:
585+ btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
586+ btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
755587 break;
756− case Opt_fragment_metadata:
757− btrfs_info(info, "fragmenting metadata");
758− btrfs_set_opt(info->mount_opt,
759− FRAGMENT_METADATA);
588+ case Opt_fragment_parameter_metadata:
589+ btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
760590 break;
761− case Opt_fragment_data:
762− btrfs_info(info, "fragmenting data");
763− btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
591+ case Opt_fragment_parameter_data:
592+ btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
764593 break;
594+ default:
595+ btrfs_info(NULL, "unrecognized fragment option '%s'",
596+ param->key);
597+ return -EINVAL;
598+ }
599+ break;
765600 #endif
766601 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
767− case Opt_ref_verify:
768− btrfs_info(info, "doing ref verification");
769− btrfs_set_opt(info->mount_opt, REF_VERIFY);
770− break;
602+ case Opt_ref_verify:
603+ btrfs_set_opt(ctx->mount_opt, REF_VERIFY);
604+ break;
771605 #endif
772− case Opt_err:
773− btrfs_err(info, "unrecognized mount option '%s'", p);
774− ret = -EINVAL;
775− goto out;
776− default:
777− break;
778− }
606+ default:
607+ btrfs_err(NULL, "unrecognized mount option '%s'", param->key);
608+ return -EINVAL;
779609 }
780−check:
781− /* We're read-only, don't have to check. */
782− if (new_flags & SB_RDONLY)
783− goto out;
784610
785− if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
786− check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
787− check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
788− ret = -EINVAL;
789−out:
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;
611+ return 0;
810612 }
811613
812614 /*
813− * Parse mount options that are required early in the mount process.
814− *
815− * All other options will be parsed on much later in the mount process and
816− * only when we need to allocate a new super block.
615+ * Some options only have meaning at mount time and shouldn't persist across
616+ * remounts, or be displayed. Clear these at the end of mount and remount code
617+ * paths.
817618 */
818−static int btrfs_parse_device_options(const char *options, blk_mode_t flags)
619+static void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
819620 {
820− substring_t args[MAX_OPT_ARGS];
821− char *device_name, *opts, *orig, *p;
822− struct btrfs_device *device = NULL;
823− int error = 0;
621+ btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
622+ btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
623+ btrfs_clear_opt(fs_info->mount_opt, NOSPACECACHE);
624+}
824625
825− lockdep_assert_held(&uuid_mutex);
626+static 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+}
826637
827− if (!options)
828− return 0;
638+bool btrfs_check_options(struct btrfs_fs_info *info, unsigned long *mount_opt,
639+ unsigned long flags)
640+{
641+ bool ret = true;
829642
830− /*
831− * strsep changes the string, duplicate it because btrfs_parse_options
832− * gets called later
833− */
834− opts = kstrdup(options, GFP_KERNEL);
835− if (!opts)
836− return -ENOMEM;
837− orig = opts;
643+ if (!(flags & SB_RDONLY) &&
644+ (check_ro_option(info, *mount_opt, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
645+ check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
646+ check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")))
647+ ret = false;
838648
839− while ((p = strsep(&opts, ",")) != NULL) {
840− int token;
649+ if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
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+ }
841660
842− if (!*p)
843− continue;
661+ if (btrfs_check_mountopts_zoned(info, mount_opt))
662+ ret = false;
844663
845− token = match_token(p, tokens, args);
846− if (token == Opt_device) {
847− device_name = match_strdup(&args[0]);
848− if (!device_name) {
849− error = -ENOMEM;
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− }
664+ if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
665+ if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE))
666+ btrfs_info(info, "disk space caching is enabled");
667+ if (btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE))
668+ btrfs_info(info, "using free-space-tree");
859669 }
860670
861−out:
862− kfree(orig);
863− return error;
671+ return ret;
864672 }
865673
866674 /*
867− * Parse mount options that are related to subvolume id
675+ * 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.
868679 *
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.
870685 */
871−static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
872− u64 *subvol_objectid)
686+void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
873687 {
874− substring_t args[MAX_OPT_ARGS];
875− char *opts, *orig, *p;
876− int error = 0;
877− u64 subvolid;
878−
879− if (!options)
880− return 0;
688+ if (fs_info->sectorsize < PAGE_SIZE) {
689+ btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
690+ if (!btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
691+ btrfs_info(fs_info,
692+ "forcing free space tree for sector size %u with page size %lu",
693+ fs_info->sectorsize, PAGE_SIZE);
694+ btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
695+ }
696+ }
881697
882698 /*
883− * strsep changes the string, duplicate it because
884− * btrfs_parse_device_options gets called later
699+ * At this point our mount options are populated, so we only mess with
700+ * these settings if we don't have any settings already.
885701 */
886− opts = kstrdup(options, GFP_KERNEL);
887− if (!opts)
888− return -ENOMEM;
889− orig = opts;
702+ if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
703+ return;
890704
891− while ((p = strsep(&opts, ",")) != NULL) {
892− int token;
893− if (!*p)
894− continue;
705+ if (btrfs_is_zoned(fs_info) &&
706+ btrfs_free_space_cache_v1_active(fs_info)) {
707+ btrfs_info(fs_info, "zoned: clearing existing space cache");
708+ btrfs_set_super_cache_generation(fs_info->super_copy, 0);
709+ return;
710+ }
895711
896− token = match_token(p, tokens, args);
897− switch (token) {
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;
712+ if (btrfs_test_opt(fs_info, SPACE_CACHE))
713+ return;
910714
911− /* we want the original fs_tree */
912− if (subvolid == 0)
913− subvolid = BTRFS_FS_TREE_OBJECTID;
715+ if (btrfs_test_opt(fs_info, NOSPACECACHE))
716+ return;
914717
915− *subvol_objectid = subvolid;
916− break;
917− default:
918− break;
919− }
920− }
718+ /*
719+ * At this point we don't have explicit options set by the user, set
720+ * them ourselves based on the state of the file system.
721+ */
722+ if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
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+}
921727
922−out:
923− kfree(orig);
924− return error;
728+static void set_device_specific_options(struct btrfs_fs_info *fs_info)
729+{
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);
925749 }
926750
927751 char *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
1105929 #endif
1106930 sb->s_xattr = btrfs_xattr_handlers;
1107931 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;
1112932 sb->s_iflags |= SB_I_CGROUPWB;
1113933
1114934 err = super_setup_bdi(sb);
@@ -1291,22 +1111,6 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) · 176 unmodified lines
12911111 return 0;
12921112 }
12931113
1294−static 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−
1302−static 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−
13101114 /*
13111115 * subvolumes are identified by ino 256
13121116 */
@@ -1382,307 +1186,299 @@ out: · 69 unmodified lines
13821186 return root;
13831187 }
13841188
1385−/*
1386− * Find a superblock for the given device / mount point.
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− */
1391−static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1392− int flags, const char *device_name, void *data)
1189+static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1190+ u32 new_pool_size, u32 old_pool_size)
13931191 {
1394− struct block_device *bdev = NULL;
1395− struct super_block *s;
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− }
1192+ if (new_pool_size == old_pool_size)
1193+ return;
14081194
1409− /* No support for restricting writes to btrfs devices yet... */
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);
1195+ fs_info->thread_pool_size = new_pool_size;
14251196
1426− fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1427− fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1428− if (!fs_info->super_copy || !fs_info->super_for_commit) {
1429− error = -ENOMEM;
1430− goto error_fs_info;
1431− }
1197+ btrfs_info(fs_info, "resize thread pool %d -> %d",
1198+ old_pool_size, new_pool_size);
14321199
1433− mutex_lock(&uuid_mutex);
1434− error = btrfs_parse_device_options(data, mode);
1435− if (error) {
1436− mutex_unlock(&uuid_mutex);
1437− goto error_fs_info;
1200+ btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1201+ btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1202+ btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1203+ workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
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+
1210+static 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);
14381221 }
1222+}
1223+
1224+static 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);
14391228
14401229 /*
1441− * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
1442− * either a valid device or an error.
1230+ * We need to cleanup all defragable inodes if the autodefragment is
1231+ * close or the filesystem is read only.
14431232 */
1444− device = btrfs_scan_one_device(device_name, mode, true);
1445− ASSERT(device != NULL);
1446− if (IS_ERR(device)) {
1447− mutex_unlock(&uuid_mutex);
1448− error = PTR_ERR(device);
1449− goto error_fs_info;
1233+ if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1234+ (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1235+ btrfs_cleanup_defrag_inodes(fs_info);
14501236 }
14511237
1452− fs_devices = device->fs_devices;
1453− fs_info->fs_devices = fs_devices;
1238+ /* If we toggled discard async */
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);
14541245
1455− error = btrfs_open_devices(fs_devices, mode, fs_type);
1456− mutex_unlock(&uuid_mutex);
1457− if (error)
1458− goto error_fs_info;
1246+ /* If we toggled space cache */
1247+ if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1248+ btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1249+}
14591250
1460− if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1461− error = -EACCES;
1462− goto error_close_devices;
1463− }
1251+static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
1252+{
1253+ int ret;
14641254
1465− bdev = fs_devices->latest_dev->bdev;
1466− s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1467− fs_info);
1468− if (IS_ERR(s)) {
1469− error = PTR_ERR(s);
1470− goto error_close_devices;
1255+ if (BTRFS_FS_ERROR(fs_info)) {
1256+ btrfs_err(fs_info,
1257+ "remounting read-write after error is not allowed");
1258+ return -EINVAL;
14711259 }
14721260
1473− if (s->s_root) {
1474− btrfs_close_devices(fs_devices);
1475− btrfs_free_fs_info(fs_info);
1476− if ((flags ^ s->s_flags) & SB_RDONLY)
1477− error = -EBUSY;
1478− } else {
1479− snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
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);
1261+ if (fs_info->fs_devices->rw_devices == 0)
1262+ return -EACCES;
1263+
1264+ if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1265+ btrfs_warn(fs_info,
1266+ "too many missing devices, writable remount is not allowed");
1267+ return -EACCES;
14841268 }
1485− if (!error)
1486− error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1487− security_free_mnt_opts(&new_sec_opts);
1488− if (error) {
1489− deactivate_locked_super(s);
1490− return ERR_PTR(error);
1269+
1270+ if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1271+ btrfs_warn(fs_info,
1272+ "mount required to replay tree-log, cannot remount read-write");
1273+ return -EINVAL;
14911274 }
14921275
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;
14941284
1495−error_close_devices:
1496− btrfs_close_devices(fs_devices);
1497−error_fs_info:
1498− btrfs_free_fs_info(fs_info);
1499−error_sec_opts:
1500− security_free_mnt_opts(&new_sec_opts);
1501− return ERR_PTR(error);
1285+ btrfs_clear_sb_rdonly(fs_info->sb);
1286+
1287+ set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1288+
1289+ /*
1290+ * If we've gone from readonly -> read-write, we need to get our
1291+ * sync/async discard lists in the right state.
1292+ */
1293+ btrfs_discard_resume(fs_info);
1294+
1295+ return 0;
15021296 }
15031297
1504−/*
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− */
1526−static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1527− const char *device_name, void *data)
1298+static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
15281299 {
1529− struct vfsmount *mnt_root;
1530− struct dentry *root;
1531− char *subvol_name = NULL;
1532− u64 subvol_objectid = 0;
1533− int error = 0;
1300+ /*
1301+ * This also happens on 'umount -rf' or on shutdown, when the
1302+ * filesystem is busy.
1303+ */
1304+ cancel_work_sync(&fs_info->async_reclaim_work);
1305+ cancel_work_sync(&fs_info->async_data_reclaim_work);
15341306
1535− error = btrfs_parse_subvol_options(data, &subvol_name,
1536− &subvol_objectid);
1537− if (error) {
1538− kfree(subvol_name);
1539− return ERR_PTR(error);
1540− }
1307+ btrfs_discard_cleanup(fs_info);
15411308
1542− /* mount device's root (/) */
1543− mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1544− if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1545− if (flags & SB_RDONLY) {
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− }
1309+ /* Wait for the uuid_scan task to finish */
1310+ down(&fs_info->uuid_tree_rescan_sem);
1311+ /* Avoid complains from lockdep et al. */
1312+ up(&fs_info->uuid_tree_rescan_sem);
15561313
1557− down_write(&mnt_root->mnt_sb->s_umount);
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− }
1314+ btrfs_set_sb_rdonly(fs_info->sb);
15731315
1574− /* mount_subvol() will free subvol_name and mnt_root */
1575− root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1316+ /*
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);
15761323
1577−out:
1578− return root;
1579−}
1324+ /*
1325+ * The cleaner task could be already running before we set the flag
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);
15801335
1581−static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1582− u32 new_pool_size, u32 old_pool_size)
1583−{
1584− if (new_pool_size == old_pool_size)
1585− return;
1336+ /*
1337+ * We've set the superblock to RO mode, so we might have made the
1338+ * cleaner task sleep without running all pending delayed iputs. Go
1339+ * through all the delayed iputs here, so that if an unmount happens
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);
15861344
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);
15881348
1589− btrfs_info(fs_info, "resize thread pool %d -> %d",
1590− old_pool_size, new_pool_size);
1349+ /*
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);
15911356
1592− btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1593− btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1594− btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1595− workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
1596− workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1597− btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1598− btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1599− btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1357+ return btrfs_commit_super(fs_info);
1358+}
1359+
1360+static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
1361+{
1362+ fs_info->max_inline = ctx->max_inline;
1363+ fs_info->commit_interval = ctx->commit_interval;
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;
16001369 }
16011370
1602−static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1603− unsigned long old_opts, int flags)
1371+static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
16041372 {
1605− if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1606− (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1607− (flags & SB_RDONLY))) {
1608− /* wait for any defraggers to finish */
1609− wait_event(fs_info->transaction_wait,
1610− (atomic_read(&fs_info->defrag_running) == 0));
1611− if (flags & SB_RDONLY)
1612− sync_filesystem(fs_info->sb);
1613− }
1373+ ctx->max_inline = fs_info->max_inline;
1374+ ctx->commit_interval = fs_info->commit_interval;
1375+ ctx->metadata_ratio = fs_info->metadata_ratio;
1376+ ctx->thread_pool_size = fs_info->thread_pool_size;
1377+ ctx->mount_opt = fs_info->mount_opt;
1378+ ctx->compress_type = fs_info->compress_type;
1379+ ctx->compress_level = fs_info->compress_level;
16141380 }
16151381
1616−static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1617− unsigned long old_opts)
1382+#define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...) \
1383+do { \
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...) \
1390+do { \
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+
1396+static void btrfs_emit_options(struct btrfs_fs_info *info,
1397+ struct btrfs_fs_context *old)
16181398 {
1619− const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1620−
1621− /*
1622− * We need to cleanup all defragable inodes if the autodefragment is
1623− * close or the filesystem is read only.
1624− */
1625− if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1626− (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1627− btrfs_cleanup_defrag_inodes(fs_info);
1399+ btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1400+ btrfs_info_if_set(info, old, DEGRADED, "allowing degraded mounts");
1401+ btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
1402+ btrfs_info_if_set(info, old, SSD, "enabling ssd optimizations");
1403+ btrfs_info_if_set(info, old, SSD_SPREAD, "using spread ssd allocation scheme");
1404+ btrfs_info_if_set(info, old, NOBARRIER, "turning off barriers");
1405+ btrfs_info_if_set(info, old, NOTREELOG, "disabling tree log");
1406+ btrfs_info_if_set(info, old, NOLOGREPLAY, "disabling log replay at mount time");
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);
16281443 }
16291444
1630− /* If we toggled discard async */
1631− if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
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);
1445+ if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1446+ btrfs_info(info, "max_inline set to %llu", info->max_inline);
16411447 }
16421448
1643−static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1449+static int btrfs_reconfigure(struct fs_context *fc)
16441450 {
1451+ struct super_block *sb = fc->root->d_sb;
16451452 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1646− unsigned old_flags = sb->s_flags;
1647− unsigned long old_opts = fs_info->mount_opt;
1648− unsigned long old_compress_type = fs_info->compress_type;
1649− u64 old_max_inline = fs_info->max_inline;
1650− u32 old_thread_pool_size = fs_info->thread_pool_size;
1651− u32 old_metadata_ratio = fs_info->metadata_ratio;
1652− int ret;
1453+ struct btrfs_fs_context *ctx = fc->fs_private;
1454+ struct btrfs_fs_context old_ctx;
1455+ int ret = 0;
1456+ bool mount_reconfigure = (fc->s_fs_info != NULL);
1457+
1458+ btrfs_info_to_ctx(fs_info, &old_ctx);
16531459
16541460 sync_filesystem(sb);
16551461 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
16561462
1657− if (data) {
1658− void *new_sec_opts = NULL;
1659−
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;
1463+ if (!mount_reconfigure &&
1464+ !btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
1465+ return -EINVAL;
16711466
1672− ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));
1467+ ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
16731468 if (ret < 0)
1674− goto restore;
1469+ return ret;
16751470
1676− btrfs_remount_begin(fs_info, old_opts, *flags);
1677− btrfs_resize_thread_pool(fs_info,
1678− fs_info->thread_pool_size, old_thread_pool_size);
1471+ btrfs_ctx_to_info(fs_info, ctx);
1472+ btrfs_remount_begin(fs_info, old_ctx.mount_opt, fc->sb_flags);
1473+ btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size,
1474+ old_ctx.thread_pool_size);
16791475
16801476 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
16811477 (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))) {
16831479 btrfs_warn(fs_info,
1684− "remount supports changing free space tree only from ro to rw");
1685− /* Make sure free space cache options match the state on disk */
1480+ "remount supports changing free space tree only from RO to RW");
1481+ /* Make sure free space cache options match the state on disk. */
16861482 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
16871483 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
16881484 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
16931489 }
16941490 }
16951491
1696− if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1697− goto out;
1698−
1699− if (*flags & SB_RDONLY) {
1700− /*
1701− * this also happens on 'umount -rf' or on shutdown, when
1702− * the filesystem is busy.
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);
1492+ ret = 0;
1493+ if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY))
1494+ ret = btrfs_remount_ro(fs_info);
1495+ else if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY))
1496+ ret = btrfs_remount_rw(fs_info);
1497+ if (ret)
1498+ goto restore;
18021499
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− }
1809−out:
18101500 /*
1811− * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1812− * since the absence of the flag means it can be toggled off by remount.
1501+ * If we set the mask during the parameter parsing VFS would reject the
1502+ * remount. Here we can set the mask and the value will be updated
1503+ * appropriately.
18131504 */
1814− *flags |= SB_I_VERSION;
1505+ if ((fc->sb_flags & SB_POSIXACL) != (sb->s_flags & SB_POSIXACL))
1506+ fc->sb_flags_mask |= SB_POSIXACL;
18151507
1508+ btrfs_emit_options(fs_info, &old_ctx);
18161509 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);
18181511 btrfs_clear_oneshot_options(fs_info);
18191512 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
18201513
18211514 return 0;
1822−
18231515 restore:
1824− /* We've hit an error - don't reset SB_RDONLY */
1825− if (sb_rdonly(sb))
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);
1516+ btrfs_ctx_to_info(fs_info, &old_ctx);
1517+ btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
18371518 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1838−
18391519 return ret;
18401520 }
18411521
@@ -2096,6 +1776,309 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) · 254 unmodified lines
20961776 return 0;
20971777 }
20981778
1779+static 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+
1787+static 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+
1865+error:
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+ */
1958+static 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+
1993+static 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+
2062+static 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+
20992082 static void btrfs_kill_super(struct super_block *sb)
21002083 {
21012084 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
21032086 btrfs_free_fs_info(fs_info);
21042087 }
21052088
2106−static struct file_system_type btrfs_fs_type = {
2107− .owner = THIS_MODULE,
2108− .name = "btrfs",
2109− .mount = btrfs_mount,
2110− .kill_sb = btrfs_kill_super,
2111− .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2112−};
2089+static void btrfs_free_fs_context(struct fs_context *fc)
2090+{
2091+ struct btrfs_fs_context *ctx = fc->fs_private;
2092+ struct btrfs_fs_info *fs_info = fc->s_fs_info;
2093+
2094+ if (fs_info)
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+
2103+static 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+}
21132121
2114−static struct file_system_type btrfs_root_fs_type = {
2115− .owner = THIS_MODULE,
2116− .name = "btrfs",
2117− .mount = btrfs_mount_root,
2118− .kill_sb = btrfs_kill_super,
2119− .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2122+static const struct fs_context_operations btrfs_fs_context_ops = {
2123+ .parse_param = btrfs_parse_param,
2124+ .reconfigure = btrfs_reconfigure,
2125+ .get_tree = btrfs_get_tree,
2126+ .dup = btrfs_dup_fs_context,
2127+ .free = btrfs_free_fs_context,
21202128 };
21212129
2130+static 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+
2159+static 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+
21222168 MODULE_ALIAS_FS("btrfs");
21232169
21242170 static 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
23302376 .destroy_inode = btrfs_destroy_inode,
23312377 .free_inode = btrfs_free_inode,
23322378 .statfs = btrfs_statfs,
2333− .remount_fs = btrfs_remount,
23342379 .freeze_fs = btrfs_freeze,
23352380 .unfreeze_fs = btrfs_unfreeze,
23362381 };
fs/btrfs/super.h+3−2
@@ -3,11 +3,12 @@ · 2 unmodified lines
33 #ifndef BTRFS_SUPER_H
44 #define BTRFS_SUPER_H
55
6−int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
7− unsigned long new_flags);
6+bool btrfs_check_options(struct btrfs_fs_info *info, unsigned long *mount_opt,
7+ unsigned long flags);
88 int btrfs_sync_fs(struct super_block *sb, int wait);
99 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1010 u64 subvol_objectid);
11+void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info);
1112
1213 static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
1314 {
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
17831783 unsigned long long limit;
17841784
17851785 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;
17861790 WRITE_ONCE(device->scrub_speed_max, limit);
17871791 return len;
17881792 }
fs/btrfs/tests/btrfs-tests.c+3−2
@@ -28,6 +28,7 @@ const char *test_error[] = { · 27 unmodified lines
2828 [TEST_ALLOC_INODE] = "cannot allocate inode",
2929 [TEST_ALLOC_BLOCK_GROUP] = "cannot allocate block group",
3030 [TEST_ALLOC_EXTENT_MAP] = "cannot allocate extent map",
31+ [TEST_ALLOC_CHUNK_MAP] = "cannot allocate chunk map",
3132 };
3233
3334 static 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
102103 if (!dev)
103104 return ERR_PTR(-ENOMEM);
104105
105− extent_io_tree_init(NULL, &dev->alloc_state, 0);
106+ extent_io_tree_init(fs_info, &dev->alloc_state, 0);
106107 INIT_LIST_HEAD(&dev->dev_list);
107108 list_add(&dev->dev_list, &fs_info->fs_devices->devices);
108109
@@ -185,7 +186,7 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) · 76 unmodified lines
185186 }
186187 spin_unlock(&fs_info->buffer_lock);
187188
188− btrfs_mapping_tree_free(&fs_info->mapping_tree);
189+ btrfs_mapping_tree_free(fs_info);
189190 list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
190191 dev_list) {
191192 btrfs_free_dummy_device(dev);
fs/btrfs/tests/btrfs-tests.h+1−0
@@ -23,6 +23,7 @@ enum { · 22 unmodified lines
2323 TEST_ALLOC_INODE,
2424 TEST_ALLOC_BLOCK_GROUP,
2525 TEST_ALLOC_EXTENT_MAP,
26+ TEST_ALLOC_CHUNK_MAP,
2627 };
2728
2829 extern 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
652652 const char *test_name)
653653 {
654654 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);
656656 void *addr = page_address(page) + offset_in_page(i);
657657
658658 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
668668 const char *test_name)
669669 {
670670 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]);
672672
673673 if (memcmp(memory + (i << PAGE_SHIFT), eb_addr, PAGE_SIZE) != 0) {
674674 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
2525 #ifdef CONFIG_BTRFS_DEBUG
2626 if (refcount_read(&em->refs) != 1) {
2727 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",
2929 em->start, em->len, em->block_start,
3030 em->block_len, refcount_read(&em->refs));
3131
@@ -73,7 +73,7 @@ static int test_case_1(struct btrfs_fs_info *fs_info, · 41 unmodified lines
7373 em->block_start = 0;
7474 em->block_len = SZ_16K;
7575 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);
7777 write_unlock(&em_tree->lock);
7878 if (ret < 0) {
7979 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
9494 em->block_start = SZ_32K; /* avoid merging */
9595 em->block_len = SZ_4K;
9696 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);
9898 write_unlock(&em_tree->lock);
9999 if (ret < 0) {
100100 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
121121 test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
122122 goto out;
123123 }
124− if (em &&
125− (em->start != 0 || extent_map_end(em) != SZ_16K ||
126− em->block_start != 0 || em->block_len != SZ_16K)) {
124+ if (!em) {
125+ test_err("case1 [%llu %llu]: no extent map returned",
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) {
127132 test_err(
128133 "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
129134 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
161166 em->block_start = EXTENT_MAP_INLINE;
162167 em->block_len = (u64)-1;
163168 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);
165170 write_unlock(&em_tree->lock);
166171 if (ret < 0) {
167172 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
182187 em->block_start = SZ_4K;
183188 em->block_len = SZ_4K;
184189 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);
186191 write_unlock(&em_tree->lock);
187192 if (ret < 0) {
188193 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
209214 test_err("case2 [0 1K]: ret %d", ret);
210215 goto out;
211216 }
212− if (em &&
213− (em->start != 0 || extent_map_end(em) != SZ_1K ||
214− em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {
217+ if (!em) {
218+ test_err("case2 [0 1K]: no extent map returned");
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) {
215224 test_err(
216225 "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
217226 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
244253 em->block_start = SZ_4K;
245254 em->block_len = SZ_4K;
246255 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);
248257 write_unlock(&em_tree->lock);
249258 if (ret < 0) {
250259 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
268277 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
269278 write_unlock(&em_tree->lock);
270279 if (ret) {
271− test_err("case3 [0x%llx 0x%llx): ret %d",
280+ test_err("case3 [%llu %llu): ret %d",
272281 start, start + len, ret);
273282 goto out;
274283 }
284+ if (!em) {
285+ test_err("case3 [%llu %llu): no extent map returned",
286+ start, start + len);
287+ ret = -ENOENT;
288+ goto out;
289+ }
275290 /*
276291 * Since bytes within em are contiguous, em->block_start is identical to
277292 * em->start.
278293 */
279− if (em &&
280− (start < em->start || start + len > extent_map_end(em) ||
281− em->start != em->block_start || em->len != em->block_len)) {
294+ if (start < em->start || start + len > extent_map_end(em) ||
295+ em->start != em->block_start || em->len != em->block_len) {
282296 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)",
284298 start, start + len, ret, em->start, em->len,
285299 em->block_start, em->block_len);
286300 ret = -EINVAL;
@@ -343,7 +357,7 @@ static int __test_case_4(struct btrfs_fs_info *fs_info, · 56 unmodified lines
343357 em->block_start = 0;
344358 em->block_len = SZ_8K;
345359 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);
347361 write_unlock(&em_tree->lock);
348362 if (ret < 0) {
349363 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
364378 em->block_start = SZ_16K; /* avoid merging */
365379 em->block_len = 24 * SZ_1K;
366380 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);
368382 write_unlock(&em_tree->lock);
369383 if (ret < 0) {
370384 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
387401 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
388402 write_unlock(&em_tree->lock);
389403 if (ret) {
390− test_err("case4 [0x%llx 0x%llx): ret %d",
391− start, len, ret);
404+ test_err("case4 [%llu %llu): ret %d",
405+ start, start + len, ret);
392406 goto out;
393407 }
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)) {
395415 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)",
397− start, len, ret, em->start, em->len, em->block_start,
416+"case4 [%llu %llu): ret %d, added wrong em (start %llu len %llu block_start %llu block_len %llu)",
417+ start, start + len, ret, em->start, em->len, em->block_start,
398418 em->block_len);
399419 ret = -EINVAL;
400420 }
@@ -443,7 +463,8 @@ static int test_case_4(struct btrfs_fs_info *fs_info, · 42 unmodified lines
443463 return ret;
444464 }
445465
446−static int add_compressed_extent(struct extent_map_tree *em_tree,
466+static int add_compressed_extent(struct btrfs_fs_info *fs_info,
467+ struct extent_map_tree *em_tree,
447468 u64 start, u64 len, u64 block_start)
448469 {
449470 struct extent_map *em;
@@ -459,9 +480,9 @@ static int add_compressed_extent(struct extent_map_tree *em_tree, · 9 unmodified lines
459480 em->len = len;
460481 em->block_start = block_start;
461482 em->block_len = SZ_4K;
462− set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
483+ em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
463484 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);
465486 write_unlock(&em_tree->lock);
466487 free_extent_map(em);
467488 if (ret < 0) {
@@ -567,7 +588,7 @@ static int validate_range(struct extent_map_tree *em_tree, int index) · 99 unmodified lines
567588 * They'll have the EXTENT_FLAG_COMPRESSED flag set to keep the em tree from
568589 * merging the em's.
569590 */
570−static int test_case_5(void)
591+static int test_case_5(struct btrfs_fs_info *fs_info)
571592 {
572593 struct extent_map_tree *em_tree;
573594 struct inode *inode;
@@ -585,35 +606,35 @@ static int test_case_5(void) · 11 unmodified lines
585606 em_tree = &BTRFS_I(inode)->extent_tree;
586607
587608 /* [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);
589610 if (ret) {
590611 test_err("cannot add extent range [0, 12K)");
591612 goto out;
592613 }
593614
594615 /* [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);
596617 if (ret) {
597618 test_err("cannot add extent range [12k, 24k)");
598619 goto out;
599620 }
600621
601622 /* [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);
603624 if (ret) {
604625 test_err("cannot add extent range [12k, 24k)");
605626 goto out;
606627 }
607628
608629 /* [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);
610631 if (ret) {
611632 test_err("cannot add extent range [12k, 24k)");
612633 goto out;
613634 }
614635
615636 /* [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);
617638 if (ret) {
618639 test_err("cannot add extent range [12k, 24k)");
619640 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
665686 struct extent_map *em = NULL;
666687 int ret;
667688
668− ret = add_compressed_extent(em_tree, 0, SZ_4K, 0);
689+ ret = add_compressed_extent(fs_info, em_tree, 0, SZ_4K, 0);
669690 if (ret)
670691 goto out;
671692
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);
673694 if (ret)
674695 goto out;
675696
@@ -713,7 +734,7 @@ out: · 37 unmodified lines
713734 * true would mess up the start/end calculations and subsequent splits would be
714735 * incorrect.
715736 */
716−static int test_case_7(void)
737+static int test_case_7(struct btrfs_fs_info *fs_info)
717738 {
718739 struct extent_map_tree *em_tree;
719740 struct extent_map *em;
@@ -742,9 +763,9 @@ static int test_case_7(void) · 22 unmodified lines
742763 em->len = SZ_16K;
743764 em->block_start = 0;
744765 em->block_len = SZ_4K;
745− set_bit(EXTENT_FLAG_PINNED, &em->flags);
766+ em->flags |= EXTENT_FLAG_PINNED;
746767 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);
748769 write_unlock(&em_tree->lock);
749770 if (ret < 0) {
750771 test_err("couldn't add extent map");
@@ -765,7 +786,7 @@ static int test_case_7(void) · 14 unmodified lines
765786 em->block_start = SZ_32K;
766787 em->block_len = SZ_16K;
767788 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);
769790 write_unlock(&em_tree->lock);
770791 if (ret < 0) {
771792 test_err("couldn't add extent map");
@@ -859,33 +880,21 @@ struct rmap_test_vector { · 87 unmodified lines
859880 static int test_rmap_block(struct btrfs_fs_info *fs_info,
860881 struct rmap_test_vector *test)
861882 {
862− struct extent_map *em;
863− struct map_lookup *map = NULL;
883+ struct btrfs_chunk_map *map;
864884 u64 *logical = NULL;
865885 int i, out_ndaddrs, out_stripe_len;
866886 int ret;
867887
868− em = alloc_extent_map();
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);
888+ map = btrfs_alloc_chunk_map(test->num_stripes, GFP_KERNEL);
875889 if (!map) {
876− kfree(em);
877− test_std_err(TEST_ALLOC_EXTENT_MAP);
890+ test_std_err(TEST_ALLOC_CHUNK_MAP);
878891 return -ENOMEM;
879892 }
880893
881− set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
882894 /* Start at 4GiB logical address */
883− em->start = SZ_4G;
884− em->len = test->data_stripe_size * test->num_data_stripes;
885− em->block_len = em->len;
886− em->orig_block_len = test->data_stripe_size;
887− em->map_lookup = map;
888−
895+ map->start = SZ_4G;
896+ map->chunk_len = test->data_stripe_size * test->num_data_stripes;
897+ map->stripe_size = test->data_stripe_size;
889898 map->num_stripes = test->num_stripes;
890899 map->type = test->raid_type;
891900
@@ -901,15 +910,13 @@ static int test_rmap_block(struct btrfs_fs_info *fs_info, · 9 unmodified lines
901910 map->stripes[i].physical = test->data_stripe_phys_start[i];
902911 }
903912
904− write_lock(&fs_info->mapping_tree.lock);
905− ret = add_extent_mapping(&fs_info->mapping_tree, em, 0);
906− write_unlock(&fs_info->mapping_tree.lock);
913+ ret = btrfs_add_chunk_map(fs_info, map);
907914 if (ret) {
908− test_err("error adding block group mapping to mapping tree");
915+ test_err("error adding chunk map to mapping tree");
909916 goto out_free;
910917 }
911918
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),
913920 &logical, &out_ndaddrs, &out_stripe_len);
914921 if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {
915922 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
938945
939946 ret = 0;
940947 out:
941− write_lock(&fs_info->mapping_tree.lock);
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);
948+ btrfs_remove_chunk_map(fs_info, map);
946949 out_free:
947− /* For the tree */
948− free_extent_map(em);
949950 kfree(logical);
950951 return ret;
951952 }
@@ -1022,13 +1023,13 @@ int btrfs_test_extent_map(void) · 70 unmodified lines
10221023 ret = test_case_4(fs_info, em_tree);
10231024 if (ret)
10241025 goto out;
1025− ret = test_case_5();
1026+ ret = test_case_5(fs_info);
10261027 if (ret)
10271028 goto out;
10281029 ret = test_case_6(fs_info, em_tree);
10291030 if (ret)
10301031 goto out;
1031− ret = test_case_7();
1032+ ret = test_case_7(fs_info);
10321033 if (ret)
10331034 goto out;
10341035
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
211211 sectorsize, BTRFS_FILE_EXTENT_REG, 0, slot);
212212 }
213213
214−static unsigned long prealloc_only = 0;
215−static unsigned long compressed_only = 0;
216−static unsigned long vacancy_only = 0;
214+static u32 prealloc_only = 0;
215+static u32 compressed_only = 0;
216+static u32 vacancy_only = 0;
217217
218218 static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
219219 {
@@ -305,7 +305,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 85 unmodified lines
305305 goto out;
306306 }
307307 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);
309309 goto out;
310310 }
311311 /*
@@ -332,7 +332,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 20 unmodified lines
332332 goto out;
333333 }
334334 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);
336336 goto out;
337337 }
338338 offset = em->start + em->len;
@@ -355,7 +355,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 16 unmodified lines
355355 goto out;
356356 }
357357 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);
359359 goto out;
360360 }
361361 if (em->orig_start != em->start) {
@@ -383,7 +383,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 21 unmodified lines
383383 goto out;
384384 }
385385 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);
387387 goto out;
388388 }
389389 if (em->orig_start != em->start) {
@@ -412,7 +412,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 22 unmodified lines
412412 goto out;
413413 }
414414 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);
416416 goto out;
417417 }
418418 offset = em->start + em->len;
@@ -434,7 +434,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 15 unmodified lines
434434 goto out;
435435 }
436436 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);
438438 goto out;
439439 }
440440 if (em->orig_start != orig_start) {
@@ -468,7 +468,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 27 unmodified lines
468468 goto out;
469469 }
470470 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",
472472 prealloc_only, em->flags);
473473 goto out;
474474 }
@@ -497,7 +497,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 22 unmodified lines
497497 goto out;
498498 }
499499 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",
501501 prealloc_only, em->flags);
502502 goto out;
503503 }
@@ -527,7 +527,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 23 unmodified lines
527527 goto out;
528528 }
529529 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);
531531 goto out;
532532 }
533533 if (em->orig_start != orig_start) {
@@ -560,7 +560,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 26 unmodified lines
560560 goto out;
561561 }
562562 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",
564564 prealloc_only, em->flags);
565565 goto out;
566566 }
@@ -595,7 +595,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 28 unmodified lines
595595 goto out;
596596 }
597597 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",
599599 compressed_only, em->flags);
600600 goto out;
601601 }
@@ -604,9 +604,9 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 2 unmodified lines
604604 em->start, em->orig_start);
605605 goto out;
606606 }
607− if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
607+ if (extent_map_compression(em) != BTRFS_COMPRESS_ZLIB) {
608608 test_err("unexpected compress type, wanted %d, got %d",
609− BTRFS_COMPRESS_ZLIB, em->compress_type);
609+ BTRFS_COMPRESS_ZLIB, extent_map_compression(em));
610610 goto out;
611611 }
612612 offset = em->start + em->len;
@@ -629,7 +629,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 16 unmodified lines
629629 goto out;
630630 }
631631 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",
633633 compressed_only, em->flags);
634634 goto out;
635635 }
@@ -638,9 +638,9 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 2 unmodified lines
638638 em->start, em->orig_start);
639639 goto out;
640640 }
641− if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
641+ if (extent_map_compression(em) != BTRFS_COMPRESS_ZLIB) {
642642 test_err("unexpected compress type, wanted %d, got %d",
643− BTRFS_COMPRESS_ZLIB, em->compress_type);
643+ BTRFS_COMPRESS_ZLIB, extent_map_compression(em));
644644 goto out;
645645 }
646646 disk_bytenr = em->block_start;
@@ -664,7 +664,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 17 unmodified lines
664664 goto out;
665665 }
666666 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);
668668 goto out;
669669 }
670670 if (em->orig_start != em->start) {
@@ -692,7 +692,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 21 unmodified lines
692692 goto out;
693693 }
694694 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",
696696 compressed_only, em->flags);
697697 goto out;
698698 }
@@ -701,9 +701,9 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 2 unmodified lines
701701 em->start, orig_start);
702702 goto out;
703703 }
704− if (em->compress_type != BTRFS_COMPRESS_ZLIB) {
704+ if (extent_map_compression(em) != BTRFS_COMPRESS_ZLIB) {
705705 test_err("unexpected compress type, wanted %d, got %d",
706− BTRFS_COMPRESS_ZLIB, em->compress_type);
706+ BTRFS_COMPRESS_ZLIB, extent_map_compression(em));
707707 goto out;
708708 }
709709 offset = em->start + em->len;
@@ -726,7 +726,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 16 unmodified lines
726726 goto out;
727727 }
728728 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);
730730 goto out;
731731 }
732732 if (em->orig_start != em->start) {
@@ -758,7 +758,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 25 unmodified lines
758758 goto out;
759759 }
760760 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",
762762 vacancy_only, em->flags);
763763 goto out;
764764 }
@@ -786,7 +786,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize) · 21 unmodified lines
786786 goto out;
787787 }
788788 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);
790790 goto out;
791791 }
792792 if (em->orig_start != em->start) {
@@ -866,7 +866,7 @@ static int test_hole_first(u32 sectorsize, u32 nodesize) · 73 unmodified lines
866866 goto out;
867867 }
868868 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,
870870 em->flags);
871871 goto out;
872872 }
@@ -888,7 +888,7 @@ static int test_hole_first(u32 sectorsize, u32 nodesize) · 15 unmodified lines
888888 goto out;
889889 }
890890 if (em->flags != 0) {
891− test_err("unexpected flags set, wanted 0 got %lu",
891+ test_err("unexpected flags set, wanted 0 got %u",
892892 em->flags);
893893 goto out;
894894 }
@@ -1095,8 +1095,8 @@ int btrfs_test_inodes(u32 sectorsize, u32 nodesize) · 200 unmodified lines
10951095
10961096 test_msg("running inode tests");
10971097
1098− set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
1099− set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
1098+ compressed_only |= EXTENT_FLAG_COMPRESS_ZLIB;
1099+ prealloc_only |= EXTENT_FLAG_PREALLOC;
11001100
11011101 ret = test_btrfs_get_extent(sectorsize, nodesize);
11021102 if (ret)
fs/btrfs/tree-checker.h+1−1
@@ -22,7 +22,7 @@ struct btrfs_tree_parent_check { · 21 unmodified lines
2222
2323 /*
2424 * 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.
2626 */
2727 u64 transid;
2828
fs/btrfs/tree-log.c+9−8
@@ -2575,7 +2575,6 @@ static int clean_log_buffer(struct btrfs_trans_handle *trans, · 2574 unmodified lines
25752575 ret = btrfs_pin_reserved_extent(trans, eb);
25762576 if (ret)
25772577 return ret;
2578− btrfs_redirty_list_add(trans->transaction, eb);
25792578 } else {
25802579 unaccount_log_buffer(eb->fs_info, eb->start);
25812580 }
@@ -4520,7 +4519,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, · 1938 unmodified lines
45204519 int ret = 0;
45214520
45224521 if (inode->flags & BTRFS_INODE_NODATASUM ||
4523− test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4522+ (em->flags & EXTENT_FLAG_PREALLOC) ||
45244523 em->block_start == EXTENT_MAP_HOLE)
45254524 return 0;
45264525
@@ -4583,7 +4582,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, · 56 unmodified lines
45834582 return 0;
45844583
45854584 /* 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)) {
45874586 csum_offset = 0;
45884587 csum_len = max(em->block_len, em->orig_block_len);
45894588 } else {
@@ -4623,18 +4622,20 @@ static int log_one_extent(struct btrfs_trans_handle *trans, · 33 unmodified lines
46234622 struct btrfs_file_extent_item fi = { 0 };
46244623 struct extent_buffer *leaf;
46254624 struct btrfs_key key;
4625+ enum btrfs_compression_type compress_type;
46264626 u64 extent_offset = em->start - em->orig_start;
46274627 u64 block_len;
46284628 int ret;
46294629
46304630 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)
46324632 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_PREALLOC);
46334633 else
46344634 btrfs_set_stack_file_extent_type(&fi, BTRFS_FILE_EXTENT_REG);
46354635
46364636 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) {
46384639 btrfs_set_stack_file_extent_disk_bytenr(&fi, em->block_start);
46394640 btrfs_set_stack_file_extent_disk_num_bytes(&fi, block_len);
46404641 } 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
46464647 btrfs_set_stack_file_extent_offset(&fi, extent_offset);
46474648 btrfs_set_stack_file_extent_num_bytes(&fi, em->len);
46484649 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);
46504651
46514652 ret = log_extent_csums(trans, inode, log, em, ctx);
46524653 if (ret)
@@ -4859,13 +4860,13 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans, · 206 unmodified lines
48594860 continue;
48604861
48614862 /* We log prealloc extents beyond eof later. */
4862− if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4863+ if ((em->flags & EXTENT_FLAG_PREALLOC) &&
48634864 em->start >= i_size_read(&inode->vfs_inode))
48644865 continue;
48654866
48664867 /* Need a ref to keep it from getting evicted from cache */
48674868 refcount_inc(&em->refs);
4868− set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4869+ em->flags |= EXTENT_FLAG_LOGGING;
48694870 list_add_tail(&em->list, &extents);
48704871 num++;
48714872 }
fs/btrfs/volumes.c+564−370
@@ -41,6 +41,17 @@ · 40 unmodified lines
4141 BTRFS_BLOCK_GROUP_RAID10 | \
4242 BTRFS_BLOCK_GROUP_RAID56_MASK)
4343
44+struct 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+
4455 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
4556 [BTRFS_RAID_RAID10] = {
4657 .sub_stripes = 2,
@@ -1742,19 +1753,18 @@ out: · 1695 unmodified lines
17421753
17431754 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
17441755 {
1745− struct extent_map_tree *em_tree;
1746− struct extent_map *em;
17471756 struct rb_node *n;
17481757 u64 ret = 0;
17491758
1750− em_tree = &fs_info->mapping_tree;
1751− read_lock(&em_tree->lock);
1752− n = rb_last(&em_tree->map.rb_root);
1759+ read_lock(&fs_info->mapping_tree_lock);
1760+ n = rb_last(&fs_info->mapping_tree.rb_root);
17531761 if (n) {
1754− em = rb_entry(n, struct extent_map, rb_node);
1755− ret = em->start + em->len;
1762+ struct btrfs_chunk_map *map;
1763+
1764+ map = rb_entry(n, struct btrfs_chunk_map, rb_node);
1765+ ret = map->start + map->chunk_len;
17561766 }
1757− read_unlock(&em_tree->lock);
1767+ read_unlock(&fs_info->mapping_tree_lock);
17581768
17591769 return ret;
17601770 }
@@ -2986,6 +2996,81 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) · 1225 unmodified lines
29862996 return ret;
29872997 }
29882998
2999+struct 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+
3062+struct 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+
29893074 /*
29903075 * Find the mapping containing the given logical extent.
29913076 *
@@ -2994,38 +3079,37 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) · 2 unmodified lines
29943079 *
29953080 * Return: Chunk mapping or ERR_PTR.
29963081 */
2997−struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2998− u64 logical, u64 length)
3082+struct btrfs_chunk_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
3083+ u64 logical, u64 length)
29993084 {
3000− struct extent_map_tree *em_tree;
3001− struct extent_map *em;
3085+ struct btrfs_chunk_map *map;
30023086
3003− em_tree = &fs_info->mapping_tree;
3004− read_lock(&em_tree->lock);
3005− em = lookup_extent_mapping(em_tree, logical, length);
3006− read_unlock(&em_tree->lock);
3087+ map = btrfs_find_chunk_map(fs_info, logical, length);
30073088
3008− if (!em) {
3089+ if (unlikely(!map)) {
3090+ read_unlock(&fs_info->mapping_tree_lock);
30093091 btrfs_crit(fs_info,
30103092 "unable to find chunk map for logical %llu length %llu",
30113093 logical, length);
30123094 return ERR_PTR(-EINVAL);
30133095 }
30143096
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);
30163099 btrfs_crit(fs_info,
30173100 "found a bad chunk map, wanted %llu-%llu, found %llu-%llu",
3018− logical, logical + length, em->start, em->start + em->len);
3019− free_extent_map(em);
3101+ logical, logical + length, map->start,
3102+ map->start + map->chunk_len);
3103+ btrfs_free_chunk_map(map);
30203104 return ERR_PTR(-EINVAL);
30213105 }
30223106
3023− /* callers are responsible for dropping em's ref. */
3024− return em;
3107+ /* Callers are responsible for dropping the reference. */
3108+ return map;
30253109 }
30263110
30273111 static 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)
30293113 {
30303114 int i;
30313115
@@ -3050,23 +3134,21 @@ static int remove_chunk_item(struct btrfs_trans_handle *trans, · 18 unmodified lines
30503134 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
30513135 {
30523136 struct btrfs_fs_info *fs_info = trans->fs_info;
3053− struct extent_map *em;
3054− struct map_lookup *map;
3137+ struct btrfs_chunk_map *map;
30553138 u64 dev_extent_len = 0;
30563139 int i, ret = 0;
30573140 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
30583141
3059− em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
3060− if (IS_ERR(em)) {
3142+ map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
3143+ if (IS_ERR(map)) {
30613144 /*
30623145 * This is a logic error, but we don't want to just rely on the
30633146 * user having built with ASSERT enabled, so if ASSERT doesn't
30643147 * do anything we still error out.
30653148 */
30663149 ASSERT(0);
3067− return PTR_ERR(em);
3150+ return PTR_ERR(map);
30683151 }
3069− map = em->map_lookup;
30703152
30713153 /*
30723154 * 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
31693251 goto out;
31703252 }
31713253
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);
31733255
31743256 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
31753257 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
31883270 */
31893271 btrfs_trans_release_chunk_metadata(trans);
31903272
3191− ret = btrfs_remove_block_group(trans, chunk_offset, em);
3273+ ret = btrfs_remove_block_group(trans, map);
31923274 if (ret) {
31933275 btrfs_abort_transaction(trans, ret);
31943276 goto out;
@@ -3200,7 +3282,7 @@ out: · 5 unmodified lines
32003282 trans->removing_chunk = false;
32013283 }
32023284 /* once for us */
3203− free_extent_map(em);
3285+ btrfs_free_chunk_map(map);
32043286 return ret;
32053287 }
32063288
@@ -5347,24 +5429,131 @@ static int decide_stripe_size(struct btrfs_fs_devices *fs_devices, · 2140 unmodified lines
53475429 }
53485430 }
53495431
5432+static 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+
5444+static 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+
5457+void 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+
5469+EXPORT_FOR_TESTS
5470+int 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+
5503+EXPORT_FOR_TESTS
5504+struct 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+
5518+struct 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+
53505533 static struct btrfs_block_group *create_chunk(struct btrfs_trans_handle *trans,
53515534 struct alloc_chunk_ctl *ctl,
53525535 struct btrfs_device_info *devices_info)
53535536 {
53545537 struct btrfs_fs_info *info = trans->fs_info;
5355− struct map_lookup *map = NULL;
5356− struct extent_map_tree *em_tree;
5538+ struct btrfs_chunk_map *map;
53575539 struct btrfs_block_group *block_group;
5358− struct extent_map *em;
53595540 u64 start = ctl->start;
53605541 u64 type = ctl->type;
53615542 int ret;
53625543 int i;
53635544 int j;
53645545
5365− map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);
5546+ map = btrfs_alloc_chunk_map(ctl->num_stripes, GFP_NOFS);
53665547 if (!map)
53675548 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;
53685557 map->num_stripes = ctl->num_stripes;
53695558
53705559 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
53755564 j * ctl->stripe_size;
53765565 }
53775566 }
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;
53825567
53835568 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
53845569
5385− em = alloc_extent_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);
5570+ ret = btrfs_add_chunk_map(info, map);
54015571 if (ret) {
5402− write_unlock(&em_tree->lock);
5403− free_extent_map(em);
5572+ btrfs_free_chunk_map(map);
54045573 return ERR_PTR(ret);
54055574 }
5406− write_unlock(&em_tree->lock);
54075575
54085576 block_group = btrfs_make_block_group(trans, type, start, ctl->chunk_size);
5409− if (IS_ERR(block_group))
5410− goto error_del_extent;
5577+ if (IS_ERR(block_group)) {
5578+ btrfs_remove_chunk_map(info, map);
5579+ return block_group;
5580+ }
54115581
5412− for (i = 0; i < map->num_stripes; i++) {
5582+ for (int i = 0; i < map->num_stripes; i++) {
54135583 struct btrfs_device *dev = map->stripes[i].dev;
54145584
54155585 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
54225592 atomic64_sub(ctl->stripe_size * map->num_stripes,
54235593 &info->free_chunk_space);
54245594
5425− free_extent_map(em);
54265595 check_raid56_incompat_flag(info, type);
54275596 check_raid1c34_incompat_flag(info, type);
54285597
5429− return block_group;
5430−
5431−error_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−
54415598 return block_group;
54425599 }
54435600
@@ -5514,8 +5671,7 @@ int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, · 70 unmodified lines
55145671 struct btrfs_key key;
55155672 struct btrfs_chunk *chunk;
55165673 struct btrfs_stripe *stripe;
5517− struct extent_map *em;
5518− struct map_lookup *map;
5674+ struct btrfs_chunk_map *map;
55195675 size_t item_size;
55205676 int i;
55215677 int ret;
@@ -5544,14 +5700,13 @@ int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans, · 22 unmodified lines
55445700 */
55455701 lockdep_assert_held(&fs_info->chunk_mutex);
55465702
5547− em = btrfs_get_chunk_map(fs_info, bg->start, bg->length);
5548− if (IS_ERR(em)) {
5549− ret = PTR_ERR(em);
5703+ map = btrfs_get_chunk_map(fs_info, bg->start, bg->length);
5704+ if (IS_ERR(map)) {
5705+ ret = PTR_ERR(map);
55505706 btrfs_abort_transaction(trans, ret);
55515707 return ret;
55525708 }
55535709
5554− map = em->map_lookup;
55555710 item_size = btrfs_chunk_item_size(map->num_stripes);
55565711
55575712 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
56085763
56095764 out:
56105765 kfree(chunk);
5611− free_extent_map(em);
5766+ btrfs_free_chunk_map(map);
56125767 return ret;
56135768 }
56145769
@@ -5653,7 +5808,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) · 38 unmodified lines
56535808 return 0;
56545809 }
56555810
5656−static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5811+static inline int btrfs_chunk_max_errors(struct btrfs_chunk_map *map)
56575812 {
56585813 const int index = btrfs_bg_flags_to_raid_index(map->type);
56595814
@@ -5662,17 +5817,15 @@ static inline int btrfs_chunk_max_errors(struct map_lookup *map) · 2 unmodified lines
56625817
56635818 bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset)
56645819 {
5665− struct extent_map *em;
5666− struct map_lookup *map;
5820+ struct btrfs_chunk_map *map;
56675821 int miss_ndevs = 0;
56685822 int i;
56695823 bool ret = true;
56705824
5671− em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
5672− if (IS_ERR(em))
5825+ map = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
5826+ if (IS_ERR(map))
56735827 return false;
56745828
5675− map = em->map_lookup;
56765829 for (i = 0; i < map->num_stripes; i++) {
56775830 if (test_bit(BTRFS_DEV_STATE_MISSING,
56785831 &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
56935846 if (miss_ndevs > btrfs_chunk_max_errors(map))
56945847 ret = false;
56955848 end:
5696− free_extent_map(em);
5849+ btrfs_free_chunk_map(map);
56975850 return ret;
56985851 }
56995852
5700−void btrfs_mapping_tree_free(struct extent_map_tree *tree)
5853+void btrfs_mapping_tree_free(struct btrfs_fs_info *fs_info)
57015854 {
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;
57035859
5704− while (1) {
5705− write_lock(&tree->lock);
5706− em = lookup_extent_mapping(tree, 0, (u64)-1);
5707− if (em)
5708− remove_extent_mapping(tree, em);
5709− write_unlock(&tree->lock);
5710− if (!em)
5711− break;
5712− /* once for us */
5713− free_extent_map(em);
5714− /* once for the tree */
5715− free_extent_map(em);
5860+ node = rb_first_cached(&fs_info->mapping_tree);
5861+ map = rb_entry(node, struct btrfs_chunk_map, rb_node);
5862+ rb_erase_cached(&map->rb_node, &fs_info->mapping_tree);
5863+ RB_CLEAR_NODE(&map->rb_node);
5864+ chunk_map_device_clear_bits(map, CHUNK_ALLOCATED);
5865+ /* Once for the tree ref. */
5866+ btrfs_free_chunk_map(map);
5867+ cond_resched_rwlock_write(&fs_info->mapping_tree_lock);
57165868 }
5869+ write_unlock(&fs_info->mapping_tree_lock);
57175870 }
57185871
57195872 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
57205873 {
5721− struct extent_map *em;
5722− struct map_lookup *map;
5874+ struct btrfs_chunk_map *map;
57235875 enum btrfs_raid_types index;
57245876 int ret = 1;
57255877
5726− em = btrfs_get_chunk_map(fs_info, logical, len);
5727− if (IS_ERR(em))
5878+ map = btrfs_get_chunk_map(fs_info, logical, len);
5879+ if (IS_ERR(map))
57285880 /*
57295881 * We could return errors for these cases, but that could get
57305882 * 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
57335885 */
57345886 return 1;
57355887
5736− map = em->map_lookup;
57375888 index = btrfs_bg_flags_to_raid_index(map->type);
57385889
57395890 /* 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
57505901 * stripe under reconstruction.
57515902 */
57525903 ret = map->num_stripes;
5753− free_extent_map(em);
5904+ btrfs_free_chunk_map(map);
57545905 return ret;
57555906 }
57565907
57575908 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
57585909 u64 logical)
57595910 {
5760− struct extent_map *em;
5761− struct map_lookup *map;
5911+ struct btrfs_chunk_map *map;
57625912 unsigned long len = fs_info->sectorsize;
57635913
57645914 if (!btrfs_fs_incompat(fs_info, RAID56))
57655915 return len;
57665916
5767− em = btrfs_get_chunk_map(fs_info, logical, len);
5917+ map = btrfs_get_chunk_map(fs_info, logical, len);
57685918
5769− if (!WARN_ON(IS_ERR(em))) {
5770− map = em->map_lookup;
5919+ if (!WARN_ON(IS_ERR(map))) {
57715920 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
57725921 len = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
5773− free_extent_map(em);
5922+ btrfs_free_chunk_map(map);
57745923 }
57755924 return len;
57765925 }
57775926
57785927 int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
57795928 {
5780− struct extent_map *em;
5781− struct map_lookup *map;
5929+ struct btrfs_chunk_map *map;
57825930 int ret = 0;
57835931
57845932 if (!btrfs_fs_incompat(fs_info, RAID56))
57855933 return 0;
57865934
5787− em = btrfs_get_chunk_map(fs_info, logical, len);
5935+ map = btrfs_get_chunk_map(fs_info, logical, len);
57885936
5789− if(!WARN_ON(IS_ERR(em))) {
5790− map = em->map_lookup;
5937+ if (!WARN_ON(IS_ERR(map))) {
57915938 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
57925939 ret = 1;
5793− free_extent_map(em);
5940+ btrfs_free_chunk_map(map);
57945941 }
57955942 return ret;
57965943 }
57975944
57985945 static 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,
58005947 int dev_replace_is_ongoing)
58015948 {
58025949 int i;
@@ -5903,8 +6050,7 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 100 unmodified lines
59036050 u64 logical, u64 *length_ret,
59046051 u32 *num_stripes)
59056052 {
5906− struct extent_map *em;
5907− struct map_lookup *map;
6053+ struct btrfs_chunk_map *map;
59086054 struct btrfs_discard_stripe *stripes;
59096055 u64 length = *length_ret;
59106056 u64 offset;
@@ -5922,11 +6068,9 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 11 unmodified lines
59226068 int ret;
59236069 int i;
59246070
5925− em = btrfs_get_chunk_map(fs_info, logical, length);
5926− if (IS_ERR(em))
5927− return ERR_CAST(em);
5928−
5929− map = em->map_lookup;
6071+ map = btrfs_get_chunk_map(fs_info, logical, length);
6072+ if (IS_ERR(map))
6073+ return ERR_CAST(map);
59306074
59316075 /* we don't discard raid56 yet */
59326076 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
59346078 goto out_free_map;
59356079 }
59366080
5937− offset = logical - em->start;
5938− length = min_t(u64, em->start + em->len - logical, length);
6081+ offset = logical - map->start;
6082+ length = min_t(u64, map->start + map->chunk_len - logical, length);
59396083 *length_ret = length;
59406084
59416085 /*
@@ -6032,10 +6176,10 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, · 90 unmodified lines
60326176 }
60336177 }
60346178
6035− free_extent_map(em);
6179+ btrfs_free_chunk_map(map);
60366180 return stripes;
60376181 out_free_map:
6038− free_extent_map(em);
6182+ btrfs_free_chunk_map(map);
60396183 return ERR_PTR(ret);
60406184 }
60416185
@@ -6133,17 +6277,16 @@ static void handle_ops_on_dev_replace(enum btrfs_map_op op, · 91 unmodified lines
61336277 bioc->replace_nr_stripes = nr_extra_stripes;
61346278 }
61356279
6136−static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op,
6137− u64 offset, u32 *stripe_nr, u64 *stripe_offset,
6138− u64 *full_stripe_start)
6280+static u64 btrfs_max_io_len(struct btrfs_chunk_map *map, u64 offset,
6281+ struct btrfs_io_geometry *io_geom)
61396282 {
61406283 /*
61416284 * Stripe_nr is the stripe where this block falls. stripe_offset is
61426285 * the offset of this block in its stripe.
61436286 */
6144− *stripe_offset = offset & BTRFS_STRIPE_LEN_MASK;
6145− *stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT;
6146− ASSERT(*stripe_offset < U32_MAX);
6287+ io_geom->stripe_offset = offset & BTRFS_STRIPE_LEN_MASK;
6288+ io_geom->stripe_nr = offset >> BTRFS_STRIPE_LEN_SHIFT;
6289+ ASSERT(io_geom->stripe_offset < U32_MAX);
61476290
61486291 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
61496292 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
61586301 * to go rounddown(), not round_down(), as nr_data_stripes is
61596302 * not ensured to be power of 2.
61606303 */
6161− *full_stripe_start =
6162− btrfs_stripe_nr_to_offset(
6163− rounddown(*stripe_nr, nr_data_stripes(map)));
6304+ io_geom->raid56_full_stripe_start = btrfs_stripe_nr_to_offset(
6305+ rounddown(io_geom->stripe_nr, nr_data_stripes(map)));
61646306
6165− ASSERT(*full_stripe_start + full_stripe_len > offset);
6166− ASSERT(*full_stripe_start <= offset);
6307+ ASSERT(io_geom->raid56_full_stripe_start + full_stripe_len > offset);
6308+ ASSERT(io_geom->raid56_full_stripe_start <= offset);
61676309 /*
61686310 * For writes to RAID56, allow to write a full stripe set, but
61696311 * no straddling of stripe sets.
61706312 */
6171− if (op == BTRFS_MAP_WRITE)
6172− return full_stripe_len - (offset - *full_stripe_start);
6313+ if (io_geom->op == BTRFS_MAP_WRITE)
6314+ return full_stripe_len - (offset - io_geom->raid56_full_stripe_start);
61736315 }
61746316
61756317 /*
@@ -6177,26 +6319,180 @@ static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, · 1 unmodified line
61776319 * a single disk).
61786320 */
61796321 if (map->type & BTRFS_BLOCK_GROUP_STRIPE_MASK)
6180− return BTRFS_STRIPE_LEN - *stripe_offset;
6322+ return BTRFS_STRIPE_LEN - io_geom->stripe_offset;
61816323 return U64_MAX;
61826324 }
61836325
6184−static int set_io_stripe(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6185− u64 logical, u64 *length, struct btrfs_io_stripe *dst,
6186− struct map_lookup *map, u32 stripe_index,
6187− u64 stripe_offset, u64 stripe_nr)
6326+static int set_io_stripe(struct btrfs_fs_info *fs_info, u64 logical,
6327+ u64 *length, struct btrfs_io_stripe *dst,
6328+ struct btrfs_chunk_map *map,
6329+ struct btrfs_io_geometry *io_geom)
61886330 {
6189− dst->dev = map->stripes[stripe_index].dev;
6331+ dst->dev = map->stripes[io_geom->stripe_index].dev;
61906332
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))
61926335 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);
61946338
6195− dst->physical = map->stripes[stripe_index].physical +
6196− stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr);
6339+ dst->physical = map->stripes[io_geom->stripe_index].physical +
6340+ io_geom->stripe_offset +
6341+ btrfs_stripe_nr_to_offset(io_geom->stripe_nr);
61976342 return 0;
61986343 }
61996344
6345+static 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+
6366+static 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+
6375+static 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+
6395+static 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+
6411+static 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+
6439+static 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+
6470+static 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+
6488+static 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+
62006496 /*
62016497 * Map one logical range to one or more physical ranges.
62026498 *
@@ -6237,43 +6533,37 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 34 unmodified lines
62376533 struct btrfs_io_context **bioc_ret,
62386534 struct btrfs_io_stripe *smap, int *mirror_num_ret)
62396535 {
6240− struct extent_map *em;
6241− struct map_lookup *map;
6536+ struct btrfs_chunk_map *map;
6537+ struct btrfs_io_geometry io_geom = { 0 };
62426538 u64 map_offset;
6243− u64 stripe_offset;
6244− u32 stripe_nr;
6245− u32 stripe_index;
6246− int data_stripes;
62476539 int i;
62486540 int ret = 0;
6249− int mirror_num = (mirror_num_ret ? *mirror_num_ret : 0);
6250− int num_stripes;
62516541 int num_copies;
6252− int max_errors = 0;
62536542 struct btrfs_io_context *bioc = NULL;
62546543 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
62556544 int dev_replace_is_ongoing = 0;
62566545 u16 num_alloc_stripes;
6257− u64 raid56_full_stripe_start = (u64)-1;
62586546 u64 max_len;
62596547
62606548 ASSERT(bioc_ret);
62616549
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+
62626555 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)
62646557 return -EINVAL;
62656558
6266− em = btrfs_get_chunk_map(fs_info, logical, *length);
6267− if (IS_ERR(em))
6268− return PTR_ERR(em);
6559+ map = btrfs_get_chunk_map(fs_info, logical, *length);
6560+ if (IS_ERR(map))
6561+ return PTR_ERR(map);
62696562
6270− map = em->map_lookup;
6271− data_stripes = nr_data_stripes(map);
6272−
6273− map_offset = logical - em->start;
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);
6563+ map_offset = logical - map->start;
6564+ io_geom.raid56_full_stripe_start = (u64)-1;
6565+ max_len = btrfs_max_io_len(map, map_offset, &io_geom);
6566+ *length = min_t(u64, map->chunk_len - map_offset, max_len);
62776567
62786568 down_read(&dev_replace->rwsem);
62796569 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
62846574 if (!dev_replace_is_ongoing)
62856575 up_read(&dev_replace->rwsem);
62866576
6287− num_stripes = 1;
6288− stripe_index = 0;
6289− if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6290− stripe_index = stripe_nr % map->num_stripes;
6291− stripe_nr /= map->num_stripes;
6292− if (op == BTRFS_MAP_READ)
6293− mirror_num = 1;
6294− } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
6295− if (op != BTRFS_MAP_READ) {
6296− num_stripes = map->num_stripes;
6297− } else if (mirror_num) {
6298− stripe_index = mirror_num - 1;
6299− } else {
6300− stripe_index = find_live_mirror(fs_info, map, 0,
6301− dev_replace_is_ongoing);
6302− mirror_num = stripe_index + 1;
6303− }
6304−
6305− } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
6306− if (op != BTRFS_MAP_READ) {
6307− num_stripes = map->num_stripes;
6308− } else if (mirror_num) {
6309− stripe_index = mirror_num - 1;
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 {
6577+ switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6578+ case BTRFS_BLOCK_GROUP_RAID0:
6579+ map_blocks_raid0(map, &io_geom);
6580+ break;
6581+ case BTRFS_BLOCK_GROUP_RAID1:
6582+ case BTRFS_BLOCK_GROUP_RAID1C3:
6583+ case BTRFS_BLOCK_GROUP_RAID1C4:
6584+ map_blocks_raid1(fs_info, map, &io_geom, dev_replace_is_ongoing);
6585+ break;
6586+ case BTRFS_BLOCK_GROUP_DUP:
6587+ map_blocks_dup(map, &io_geom);
6588+ break;
6589+ case BTRFS_BLOCK_GROUP_RAID10:
6590+ map_blocks_raid10(fs_info, map, &io_geom, dev_replace_is_ongoing);
6591+ break;
6592+ case BTRFS_BLOCK_GROUP_RAID5:
6593+ case BTRFS_BLOCK_GROUP_RAID6:
6594+ if (op != BTRFS_MAP_READ || io_geom.mirror_num > 1)
6595+ map_blocks_raid56_write(map, &io_geom, logical, length);
6596+ else
6597+ map_blocks_raid56_read(map, &io_geom);
6598+ break;
6599+ default:
63706600 /*
63716601 * After this, stripe_nr is the number of stripes on this
63726602 * device we have to walk to find the data, and stripe_index is
63736603 * the number of our device in the stripe array
63746604 */
6375− stripe_index = stripe_nr % map->num_stripes;
6376− stripe_nr /= map->num_stripes;
6377− mirror_num = stripe_index + 1;
6605+ map_blocks_single(map, &io_geom);
6606+ break;
63786607 }
6379− if (stripe_index >= map->num_stripes) {
6608+ if (io_geom.stripe_index >= map->num_stripes) {
63806609 btrfs_crit(fs_info,
63816610 "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);
63836612 ret = -EINVAL;
63846613 goto out;
63856614 }
63866615
6387− num_alloc_stripes = num_stripes;
6616+ num_alloc_stripes = io_geom.num_stripes;
63886617 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
63896618 op != BTRFS_MAP_READ)
63906619 /*
@@ -6401,14 +6630,11 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 10 unmodified lines
64016630 * physical block information on the stack instead of allocating an
64026631 * I/O context structure.
64036632 */
6404− if (smap && num_alloc_stripes == 1 &&
6405− !(btrfs_need_stripe_tree_update(fs_info, map->type) &&
6406− op != BTRFS_MAP_READ) &&
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);
6633+ if (is_single_device_io(fs_info, smap, map, num_alloc_stripes, op,
6634+ io_geom.mirror_num)) {
6635+ ret = set_io_stripe(fs_info, logical, length, smap, map, &io_geom);
64106636 if (mirror_num_ret)
6411− *mirror_num_ret = mirror_num;
6637+ *mirror_num_ret = io_geom.mirror_num;
64126638 *bioc_ret = NULL;
64136639 goto out;
64146640 }
@@ -6428,7 +6654,7 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 13 unmodified lines
64286654 * It's still mostly the same as other profiles, just with extra rotation.
64296655 */
64306656 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)) {
64326658 /*
64336659 * For RAID56 @stripe_nr is already the number of full stripes
64346660 * 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
64376663 * In this case, we just add @stripe_nr with @i, then do the
64386664 * modulo, to reduce one modulo call.
64396665 */
6440− bioc->full_stripe_logical = em->start +
6441− btrfs_stripe_nr_to_offset(stripe_nr * data_stripes);
6442− for (int i = 0; i < num_stripes; i++) {
6443− ret = set_io_stripe(fs_info, op, logical, length,
6444− &bioc->stripes[i], map,
6445− (i + stripe_nr) % num_stripes,
6446− stripe_offset, stripe_nr);
6447− if (ret < 0)
6448− break;
6666+ bioc->full_stripe_logical = map->start +
6667+ btrfs_stripe_nr_to_offset(io_geom.stripe_nr *
6668+ nr_data_stripes(map));
6669+ for (int i = 0; i < io_geom.num_stripes; i++) {
6670+ struct btrfs_io_stripe *dst = &bioc->stripes[i];
6671+ u32 stripe_index;
6672+
6673+ stripe_index = (i + io_geom.stripe_nr) % io_geom.num_stripes;
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);
64496679 }
64506680 } else {
64516681 /*
64526682 * For all other non-RAID56 profiles, just copy the target
64536683 * stripe into the bioc.
64546684 */
6455− for (i = 0; i < num_stripes; i++) {
6456− ret = set_io_stripe(fs_info, op, logical, length,
6457− &bioc->stripes[i], map, stripe_index,
6458− stripe_offset, stripe_nr);
6685+ for (i = 0; i < io_geom.num_stripes; i++) {
6686+ ret = set_io_stripe(fs_info, logical, length,
6687+ &bioc->stripes[i], map, &io_geom);
64596688 if (ret < 0)
64606689 break;
6461− stripe_index++;
6690+ io_geom.stripe_index++;
64626691 }
64636692 }
64646693
@@ -6469,18 +6698,18 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 4 unmodified lines
64696698 }
64706699
64716700 if (op != BTRFS_MAP_READ)
6472− max_errors = btrfs_chunk_max_errors(map);
6701+ io_geom.max_errors = btrfs_chunk_max_errors(map);
64736702
64746703 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
64756704 op != BTRFS_MAP_READ) {
64766705 handle_ops_on_dev_replace(op, bioc, dev_replace, logical,
6477− &num_stripes, &max_errors);
6706+ &io_geom.num_stripes, &io_geom.max_errors);
64786707 }
64796708
64806709 *bioc_ret = bioc;
6481− bioc->num_stripes = num_stripes;
6482− bioc->max_errors = max_errors;
6483− bioc->mirror_num = mirror_num;
6710+ bioc->num_stripes = io_geom.num_stripes;
6711+ bioc->max_errors = io_geom.max_errors;
6712+ bioc->mirror_num = io_geom.mirror_num;
64846713
64856714 out:
64866715 if (dev_replace_is_ongoing) {
@@ -6488,7 +6717,7 @@ out: · 1 unmodified line
64886717 /* Unlock and let waiting writers proceed */
64896718 up_read(&dev_replace->rwsem);
64906719 }
6491− free_extent_map(em);
6720+ btrfs_free_chunk_map(map);
64926721 return ret;
64936722 }
64946723
@@ -6660,12 +6889,11 @@ static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, · 165 unmodified lines
66606889 devid, uuid);
66616890 }
66626891
6663−u64 btrfs_calc_stripe_length(const struct extent_map *em)
6892+u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map)
66646893 {
6665− const struct map_lookup *map = em->map_lookup;
66666894 const int data_stripes = calc_data_stripes(map->type, map->num_stripes);
66676895
6668− return div_u64(em->len, data_stripes);
6896+ return div_u64(map->chunk_len, data_stripes);
66696897 }
66706898
66716899 #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
67346962 {
67356963 BTRFS_DEV_LOOKUP_ARGS(args);
67366964 struct btrfs_fs_info *fs_info = leaf->fs_info;
6737− struct extent_map_tree *map_tree = &fs_info->mapping_tree;
6738− struct map_lookup *map;
6739− struct extent_map *em;
6965+ struct btrfs_chunk_map *map;
67406966 u64 logical;
67416967 u64 length;
67426968 u64 devid;
@@ -6770,35 +6996,22 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 27 unmodified lines
67706996 return ret;
67716997 }
67726998
6773− read_lock(&map_tree->lock);
6774− em = lookup_extent_mapping(map_tree, logical, 1);
6775− read_unlock(&map_tree->lock);
6999+ map = btrfs_find_chunk_map(fs_info, logical, 1);
67767000
67777001 /* already mapped? */
6778− if (em && em->start <= logical && em->start + em->len > logical) {
6779− free_extent_map(em);
7002+ if (map && map->start <= logical && map->start + map->chunk_len > logical) {
7003+ btrfs_free_chunk_map(map);
67807004 return 0;
6781− } else if (em) {
6782− free_extent_map(em);
7005+ } else if (map) {
7006+ btrfs_free_chunk_map(map);
67837007 }
67847008
6785− em = alloc_extent_map();
6786− if (!em)
6787− return -ENOMEM;
6788− map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6789− if (!map) {
6790− free_extent_map(em);
7009+ map = btrfs_alloc_chunk_map(num_stripes, GFP_NOFS);
7010+ if (!map)
67917011 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;
68017012
7013+ map->start = logical;
7014+ map->chunk_len = length;
68027015 map->num_stripes = num_stripes;
68037016 map->io_width = btrfs_chunk_io_width(leaf, chunk);
68047017 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
68137026 */
68147027 map->sub_stripes = btrfs_raid_array[index].sub_stripes;
68157028 map->verified_stripes = 0;
6816− em->orig_block_len = btrfs_calc_stripe_length(em);
7029+ map->stripe_size = btrfs_calc_stripe_length(map);
68177030 for (i = 0; i < num_stripes; i++) {
68187031 map->stripes[i].physical =
68197032 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
68297042 devid, uuid);
68307043 if (IS_ERR(map->stripes[i].dev)) {
68317044 ret = PTR_ERR(map->stripes[i].dev);
6832− free_extent_map(em);
7045+ btrfs_free_chunk_map(map);
68337046 return ret;
68347047 }
68357048 }
@@ -6838,15 +7051,12 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, · 2 unmodified lines
68387051 &(map->stripes[i].dev->dev_state));
68397052 }
68407053
6841− write_lock(&map_tree->lock);
6842− ret = add_extent_mapping(map_tree, em, 0);
6843− write_unlock(&map_tree->lock);
7054+ ret = btrfs_add_chunk_map(fs_info, map);
68447055 if (ret < 0) {
68457056 btrfs_err(fs_info,
68467057 "failed to add chunk map, start=%llu len=%llu: %d",
6847− em->start, em->len, ret);
7058+ map->start, map->chunk_len, ret);
68487059 }
6849− free_extent_map(em);
68507060
68517061 return ret;
68527062 }
@@ -7156,26 +7366,21 @@ out_short_read: · 303 unmodified lines
71567366 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
71577367 struct btrfs_device *failing_dev)
71587368 {
7159− struct extent_map_tree *map_tree = &fs_info->mapping_tree;
7160− struct extent_map *em;
7161− u64 next_start = 0;
7369+ struct btrfs_chunk_map *map;
7370+ u64 next_start;
71627371 bool ret = true;
71637372
7164− read_lock(&map_tree->lock);
7165− em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7166− read_unlock(&map_tree->lock);
7373+ map = btrfs_find_chunk_map(fs_info, 0, U64_MAX);
71677374 /* No chunk at all? Return false anyway */
7168− if (!em) {
7375+ if (!map) {
71697376 ret = false;
71707377 goto out;
71717378 }
7172− while (em) {
7173− struct map_lookup *map;
7379+ while (map) {
71747380 int missing = 0;
71757381 int max_tolerated;
71767382 int i;
71777383
7178− map = em->map_lookup;
71797384 max_tolerated =
71807385 btrfs_get_num_tolerated_disk_barrier_failures(
71817386 map->type);
@@ -7193,18 +7398,15 @@ bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, · 11 unmodified lines
71937398 if (!failing_dev)
71947399 btrfs_warn(fs_info,
71957400 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
7196− em->start, missing, max_tolerated);
7197− free_extent_map(em);
7401+ map->start, missing, max_tolerated);
7402+ btrfs_free_chunk_map(map);
71987403 ret = false;
71997404 goto out;
72007405 }
7201− next_start = extent_map_end(em);
7202− free_extent_map(em);
7406+ next_start = map->start + map->chunk_len;
7407+ btrfs_free_chunk_map(map);
72037408
7204− read_lock(&map_tree->lock);
7205− em = lookup_extent_mapping(map_tree, next_start,
7206− (u64)(-1) - next_start);
7207− read_unlock(&map_tree->lock);
7409+ map = btrfs_find_chunk_map(fs_info, next_start, U64_MAX - next_start);
72087410 }
72097411 out:
72107412 return ret;
@@ -7697,20 +7899,15 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 486 unmodified lines
76977899 u64 physical_offset, u64 physical_len)
76987900 {
76997901 struct btrfs_dev_lookup_args args = { .devid = devid };
7700− struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7701− struct extent_map *em;
7702− struct map_lookup *map;
7902+ struct btrfs_chunk_map *map;
77037903 struct btrfs_device *dev;
77047904 u64 stripe_len;
77057905 bool found = false;
77067906 int ret = 0;
77077907 int i;
77087908
7709− read_lock(&em_tree->lock);
7710− em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7711− read_unlock(&em_tree->lock);
7712−
7713− if (!em) {
7909+ map = btrfs_find_chunk_map(fs_info, chunk_offset, 1);
7910+ if (!map) {
77147911 btrfs_err(fs_info,
77157912 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
77167913 physical_offset, devid);
@@ -7718,12 +7915,11 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 1 unmodified line
77187915 goto out;
77197916 }
77207917
7721− map = em->map_lookup;
7722− stripe_len = btrfs_calc_stripe_length(em);
7918+ stripe_len = btrfs_calc_stripe_length(map);
77237919 if (physical_len != stripe_len) {
77247920 btrfs_err(fs_info,
77257921 "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,
77277923 stripe_len);
77287924 ret = -EUCLEAN;
77297925 goto out;
@@ -7746,7 +7942,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 16 unmodified lines
77467942 if (map->verified_stripes >= map->num_stripes) {
77477943 btrfs_err(fs_info,
77487944 "too many dev extents for chunk %llu found",
7749− em->start);
7945+ map->start);
77507946 ret = -EUCLEAN;
77517947 goto out;
77527948 }
@@ -7792,32 +7988,30 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, · 39 unmodified lines
77927988 }
77937989
77947990 out:
7795− free_extent_map(em);
7991+ btrfs_free_chunk_map(map);
77967992 return ret;
77977993 }
77987994
77997995 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
78007996 {
7801− struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7802− struct extent_map *em;
78037997 struct rb_node *node;
78047998 int ret = 0;
78057999
7806− read_lock(&em_tree->lock);
7807− for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
7808− em = rb_entry(node, struct extent_map, rb_node);
7809− if (em->map_lookup->num_stripes !=
7810− em->map_lookup->verified_stripes) {
8000+ read_lock(&fs_info->mapping_tree_lock);
8001+ for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) {
8002+ struct btrfs_chunk_map *map;
8003+
8004+ map = rb_entry(node, struct btrfs_chunk_map, rb_node);
8005+ if (map->num_stripes != map->verified_stripes) {
78118006 btrfs_err(fs_info,
78128007 "chunk %llu has missing dev extent, have %d expect %d",
7813− em->start, em->map_lookup->verified_stripes,
7814− em->map_lookup->num_stripes);
8008+ map->start, map->verified_stripes, map->num_stripes);
78158009 ret = -EUCLEAN;
78168010 goto out;
78178011 }
78188012 }
78198013 out:
7820− read_unlock(&em_tree->lock);
8014+ read_unlock(&fs_info->mapping_tree_lock);
78218015 return ret;
78228016 }
78238017
fs/btrfs/volumes.h+37−10
@@ -426,7 +426,8 @@ struct btrfs_discard_stripe { · 425 unmodified lines
426426 struct btrfs_io_context {
427427 refcount_t refs;
428428 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;
430431 struct bio *orig_bio;
431432 atomic_t error;
432433 u16 max_errors;
@@ -529,18 +530,32 @@ struct btrfs_raid_attr { · 96 unmodified lines
529530
530531 extern const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES];
531532
532−struct map_lookup {
533+struct 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;
533541 u64 type;
534542 int io_align;
535543 int io_width;
536544 int num_stripes;
537545 int sub_stripes;
538− int verified_stripes; /* For mount time dev extent verification */
539546 struct btrfs_io_stripe stripes[];
540547 };
541548
542−#define map_lookup_size(n) (sizeof(struct map_lookup) + \
543− (sizeof(struct btrfs_io_stripe) * (n)))
549+#define btrfs_chunk_map_size(n) (sizeof(struct btrfs_chunk_map) + \
550+ (sizeof(struct btrfs_io_stripe) * (n)))
551+
552+static 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+}
544559
545560 struct btrfs_balance_args;
546561 struct btrfs_balance_progress;
@@ -598,7 +613,7 @@ static inline unsigned long btrfs_chunk_item_size(int num_stripes) · 51 unmodified lines
598613 }
599614
600615 /*
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.
602617 *
603618 * @stripe_nr is u32, with left shift it can overflow u32 for chunks larger
604619 * 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
624639 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
625640 struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
626641 u64 type);
627−void btrfs_mapping_tree_free(struct extent_map_tree *tree);
642+void btrfs_mapping_tree_free(struct btrfs_fs_info *fs_info);
628643 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
629644 blk_mode_t flags, void *holder);
630645 struct 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
680695 u64 logical, u64 len);
681696 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
682697 u64 logical);
683−u64 btrfs_calc_stripe_length(const struct extent_map *em);
698+u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map);
684699 int btrfs_nr_parity_stripes(u64 type);
685700 int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
686701 struct btrfs_block_group *bg);
687702 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
688−struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
689− u64 logical, u64 length);
703+
704+#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
705+struct btrfs_chunk_map *btrfs_alloc_chunk_map(int num_stripes, gfp_t gfp);
706+int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map);
707+#endif
708+
709+struct btrfs_chunk_map *btrfs_clone_chunk_map(struct btrfs_chunk_map *map, gfp_t gfp);
710+struct btrfs_chunk_map *btrfs_find_chunk_map(struct btrfs_fs_info *fs_info,
711+ u64 logical, u64 length);
712+struct btrfs_chunk_map *btrfs_find_chunk_map_nolock(struct btrfs_fs_info *fs_info,
713+ u64 logical, u64 length);
714+struct btrfs_chunk_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
715+ u64 logical, u64 length);
716+void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map);
690717 void btrfs_release_disk_super(struct btrfs_super_block *super);
691718
692719 static 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
382382 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
383383 }
384384
385+static 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+
414+static 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+
385432 static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
386433 struct mnt_idmap *idmap,
387434 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
420467
421468 static const struct xattr_handler btrfs_security_xattr_handler = {
422469 .prefix = XATTR_SECURITY_PREFIX,
423− .get = btrfs_xattr_handler_get,
424− .set = btrfs_xattr_handler_set,
470+ .get = btrfs_xattr_handler_get_security,
471+ .set = btrfs_xattr_handler_set_security,
425472 };
426473
427474 static const struct xattr_handler btrfs_trusted_xattr_handler = {
@@ -473,6 +520,10 @@ static int btrfs_initxattrs(struct inode *inode, · 45 unmodified lines
473520 }
474521 strcpy(name, XATTR_SECURITY_PREFIX);
475522 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+
476527 err = btrfs_setxattr(trans, inode, name, xattr->value,
477528 xattr->value_len, 0);
478529 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
121121 workspace->strm.total_in = 0;
122122 workspace->strm.total_out = 0;
123123
124− out_page = alloc_page(GFP_NOFS);
124+ out_page = btrfs_alloc_compr_page();
125125 if (out_page == NULL) {
126126 ret = -ENOMEM;
127127 goto out;
@@ -200,7 +200,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · 72 unmodified lines
200200 ret = -E2BIG;
201201 goto out;
202202 }
203− out_page = alloc_page(GFP_NOFS);
203+ out_page = btrfs_alloc_compr_page();
204204 if (out_page == NULL) {
205205 ret = -ENOMEM;
206206 goto out;
@@ -236,7 +236,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping, · 29 unmodified lines
236236 ret = -E2BIG;
237237 goto out;
238238 }
239− out_page = alloc_page(GFP_NOFS);
239+ out_page = btrfs_alloc_compr_page();
240240 if (out_page == NULL) {
241241 ret = -ENOMEM;
242242 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
781781 * Check mount options here, because we might change fs_info->zoned
782782 * from fs_info->zone_size.
783783 */
784− ret = btrfs_check_mountopts_zoned(fs_info);
784+ ret = btrfs_check_mountopts_zoned(fs_info, &fs_info->mount_opt);
785785 if (ret)
786786 return ret;
787787
@@ -789,7 +789,7 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info) · 1 unmodified line
789789 return 0;
790790 }
791791
792−int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
792+int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info, unsigned long *mount_opt)
793793 {
794794 if (!btrfs_is_zoned(info))
795795 return 0;
@@ -798,18 +798,21 @@ int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info) · 2 unmodified lines
798798 * Space cache writing is not COWed. Disable that to avoid write errors
799799 * in sequential zones.
800800 */
801− if (btrfs_test_opt(info, SPACE_CACHE)) {
801+ if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
802802 btrfs_err(info, "zoned: space cache v1 is not supported");
803803 return -EINVAL;
804804 }
805805
806− if (btrfs_test_opt(info, NODATACOW)) {
806+ if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) {
807807 btrfs_err(info, "zoned: NODATACOW not supported");
808808 return -EINVAL;
809809 }
810810
811− btrfs_clear_and_info(info, DISCARD_ASYNC,
812− "zoned: async discard ignored and disabled for zoned mode");
811+ if (btrfs_raw_test_opt(*mount_opt, DISCARD_ASYNC)) {
812+ btrfs_info(info,
813+ "zoned: async discard ignored and disabled for zoned mode");
814+ btrfs_clear_opt(*mount_opt, DISCARD_ASYNC);
815+ }
813816
814817 return 0;
815818 }
@@ -1290,7 +1293,7 @@ struct zone_info { · 474 unmodified lines
12901293
12911294 static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
12921295 struct zone_info *info, unsigned long *active,
1293− struct map_lookup *map)
1296+ struct btrfs_chunk_map *map)
12941297 {
12951298 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
12961299 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
13931396 }
13941397
13951398 static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
1396− struct map_lookup *map,
1399+ struct btrfs_chunk_map *map,
13971400 struct zone_info *zone_info,
13981401 unsigned long *active)
13991402 {
@@ -1435,7 +1438,7 @@ static int btrfs_load_block_group_dup(struct btrfs_block_group *bg, · 35 unmodified lines
14351438 }
14361439
14371440 static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
1438− struct map_lookup *map,
1441+ struct btrfs_chunk_map *map,
14391442 struct zone_info *zone_info,
14401443 unsigned long *active)
14411444 {
@@ -1483,7 +1486,7 @@ static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg, · 41 unmodified lines
14831486 }
14841487
14851488 static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
1486− struct map_lookup *map,
1489+ struct btrfs_chunk_map *map,
14871490 struct zone_info *zone_info,
14881491 unsigned long *active)
14891492 {
@@ -1515,7 +1518,7 @@ static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg, · 25 unmodified lines
15151518 }
15161519
15171520 static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
1518− struct map_lookup *map,
1521+ struct btrfs_chunk_map *map,
15191522 struct zone_info *zone_info,
15201523 unsigned long *active)
15211524 {
@@ -1552,9 +1555,7 @@ static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg, · 30 unmodified lines
15521555 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
15531556 {
15541557 struct btrfs_fs_info *fs_info = cache->fs_info;
1555− struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1556− struct extent_map *em;
1557− struct map_lookup *map;
1558+ struct btrfs_chunk_map *map;
15581559 u64 logical = cache->start;
15591560 u64 length = cache->length;
15601561 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
15751576 return -EIO;
15761577 }
15771578
1578− /* Get the chunk mapping */
1579− read_lock(&em_tree->lock);
1580− em = lookup_extent_mapping(em_tree, logical, length);
1581− read_unlock(&em_tree->lock);
1582−
1583− if (!em)
1579+ map = btrfs_find_chunk_map(fs_info, logical, length);
1580+ if (!map)
15841581 return -EINVAL;
15851582
1586− map = em->map_lookup;
1587−
1588− cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1583+ cache->physical_map = btrfs_clone_chunk_map(map, GFP_NOFS);
15891584 if (!cache->physical_map) {
15901585 ret = -ENOMEM;
15911586 goto out;
@@ -1687,12 +1682,11 @@ out: · 95 unmodified lines
16871682 spin_unlock(&fs_info->zone_active_bgs_lock);
16881683 }
16891684 } else {
1690− kfree(cache->physical_map);
1685+ btrfs_free_chunk_map(cache->physical_map);
16911686 cache->physical_map = NULL;
16921687 }
16931688 bitmap_free(active);
16941689 kfree(zone_info);
1695− free_extent_map(em);
16961690
16971691 return ret;
16981692 }
@@ -1715,22 +1709,6 @@ void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) · 16 unmodified lines
17151709 cache->zone_unusable = unusable;
17161710 }
17171711
1718−void 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−
17341712 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
17351713 {
17361714 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
20822060 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
20832061 {
20842062 struct btrfs_fs_info *fs_info = block_group->fs_info;
2085− struct map_lookup *map;
2063+ struct btrfs_chunk_map *map;
20862064 struct btrfs_device *device;
20872065 u64 physical;
20882066 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
21942172 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written)
21952173 {
21962174 struct btrfs_fs_info *fs_info = block_group->fs_info;
2197− struct map_lookup *map;
2175+ struct btrfs_chunk_map *map;
21982176 const bool is_metadata = (block_group->flags &
21992177 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
22002178 int ret = 0;
@@ -2643,7 +2621,7 @@ void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) · 442 unmodified lines
26432621 /* Release reservation for currently active block groups. */
26442622 spin_lock(&fs_info->zone_active_bgs_lock);
26452623 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;
26472625
26482626 if (!(block_group->flags &
26492627 (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
4545 void btrfs_destroy_dev_zone_info(struct btrfs_device *device);
4646 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev);
4747 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
48−int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info);
48+int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info, unsigned long *mount_opt);
4949 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
5050 u64 *bytenr_ret);
5151 int 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
5959 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size);
6060 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new);
6161 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache);
62−void btrfs_redirty_list_add(struct btrfs_transaction *trans,
63− struct extent_buffer *eb);
6462 bool btrfs_use_zone_append(struct btrfs_bio *bbio);
6563 void btrfs_record_physical_zoned(struct btrfs_bio *bbio);
6664 int 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
123121 return -EOPNOTSUPP;
124122 }
125123
126−static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
124+static inline int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info,
125+ unsigned long *mount_opt)
127126 {
128127 return 0;
129128 }
@@ -180,9 +179,6 @@ static inline int btrfs_load_block_group_zone_info( · 50 unmodified lines
180179
181180 static inline void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) { }
182181
183−static inline void btrfs_redirty_list_add(struct btrfs_transaction *trans,
184− struct extent_buffer *eb) { }
185−
186182 static inline bool btrfs_use_zone_append(struct btrfs_bio *bbio)
187183 {
188184 return false;
@@ -323,7 +319,7 @@ static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_i · 134 unmodified lines
323319 (bdev_zone_sectors(bdev) << SECTOR_SHIFT);
324320 }
325321
326− /* Do not allow Host Manged zoned device */
322+ /* Do not allow Host Managed zoned device. */
327323 return bdev_zoned_model(bdev) != BLK_ZONED_HM;
328324 }
329325
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
410410 workspace->in_buf.pos = 0;
411411 workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
412412
413−
414413 /* Allocate and map in the output buffer */
415− out_page = alloc_page(GFP_NOFS);
414+ out_page = btrfs_alloc_compr_page();
416415 if (out_page == NULL) {
417416 ret = -ENOMEM;
418417 goto out;
@@ -457,7 +456,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · 38 unmodified lines
457456 ret = -E2BIG;
458457 goto out;
459458 }
460− out_page = alloc_page(GFP_NOFS);
459+ out_page = btrfs_alloc_compr_page();
461460 if (out_page == NULL) {
462461 ret = -ENOMEM;
463462 goto out;
@@ -514,7 +513,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · 50 unmodified lines
514513 ret = -E2BIG;
515514 goto out;
516515 }
517− out_page = alloc_page(GFP_NOFS);
516+ out_page = btrfs_alloc_compr_page();
518517 if (out_page == NULL) {
519518 ret = -ENOMEM;
520519 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
28782878 if (IS_ERR(fc))
28792879 return PTR_ERR(fc);
28802880
2881+ /*
2882+ * Indicate to the filesystem that the remount request is coming
2883+ * from the legacy mount system call.
2884+ */
28812885 fc->oldapi = true;
2886+
28822887 err = parse_monolithic_mount_data(fc, data);
28832888 if (!err) {
28842889 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
33283333 if (IS_ERR(fc))
33293334 return PTR_ERR(fc);
33303335
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+
33313342 if (subtype)
33323343 err = vfs_parse_fs_string(fc, "subtype",
33333344 subtype, strlen(subtype));
include/trace/events/btrfs.h+30−48
@@ -21,7 +21,7 @@ struct btrfs_delayed_data_ref; · 20 unmodified lines
2121 struct btrfs_delayed_ref_head;
2222 struct btrfs_block_group;
2323 struct btrfs_free_cluster;
24−struct map_lookup;
24+struct btrfs_chunk_map;
2525 struct extent_buffer;
2626 struct btrfs_work;
2727 struct btrfs_workqueue;
@@ -265,20 +265,20 @@ DEFINE_EVENT(btrfs__inode, btrfs_inode_evict, · 237 unmodified lines
265265 __print_symbolic_u64(type, \
266266 { EXTENT_MAP_LAST_BYTE, "LAST_BYTE" }, \
267267 { EXTENT_MAP_HOLE, "HOLE" }, \
268− { EXTENT_MAP_INLINE, "INLINE" }, \
269− { EXTENT_MAP_DELALLOC, "DELALLOC" })
268+ { EXTENT_MAP_INLINE, "INLINE" })
270269
271270 #define show_map_type(type) \
272271 type, (type >= EXTENT_MAP_LAST_BYTE) ? "-" : __show_map_type(type)
273272
274273 #define show_map_flags(flag) \
275274 __print_flags(flag, "|", \
276− { (1 << EXTENT_FLAG_PINNED), "PINNED" },\
277− { (1 << EXTENT_FLAG_COMPRESSED), "COMPRESSED" },\
278− { (1 << EXTENT_FLAG_PREALLOC), "PREALLOC" },\
279− { (1 << EXTENT_FLAG_LOGGING), "LOGGING" },\
280− { (1 << EXTENT_FLAG_FILLING), "FILLING" },\
281− { (1 << EXTENT_FLAG_FS_MAPPING), "FS_MAPPING" })
275+ { EXTENT_FLAG_PINNED, "PINNED" },\
276+ { EXTENT_FLAG_COMPRESS_ZLIB, "COMPRESS_ZLIB" },\
277+ { EXTENT_FLAG_COMPRESS_LZO, "COMPRESS_LZO" },\
278+ { EXTENT_FLAG_COMPRESS_ZSTD, "COMPRESS_ZSTD" },\
279+ { EXTENT_FLAG_PREALLOC, "PREALLOC" },\
280+ { EXTENT_FLAG_LOGGING, "LOGGING" },\
281+ { EXTENT_FLAG_FILLING, "FILLING" })
282282
283283 TRACE_EVENT_CONDITION(btrfs_get_extent,
284284
@@ -297,9 +297,8 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, · 12 unmodified lines
297297 __field( u64, orig_start )
298298 __field( u64, block_start )
299299 __field( u64, block_len )
300− __field( unsigned long, flags )
300+ __field( u32, flags )
301301 __field( int, refs )
302− __field( unsigned int, compress_type )
303302 ),
304303
305304 TP_fast_assign_btrfs(root->fs_info,
@@ -312,13 +311,11 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, · 6 unmodified lines
312311 __entry->block_len = map->block_len;
313312 __entry->flags = map->flags;
314313 __entry->refs = refcount_read(&map->refs);
315− __entry->compress_type = map->compress_type;
316314 ),
317315
318316 TP_printk_btrfs("root=%llu(%s) ino=%llu start=%llu len=%llu "
319317 "orig_start=%llu block_start=%llu(%s) "
320− "block_len=%llu flags=%s refs=%u "
321− "compress_type=%u",
318+ "block_len=%llu flags=%s refs=%u",
322319 show_root_type(__entry->root_objectid),
323320 __entry->ino,
324321 __entry->start,
@@ -327,7 +324,7 @@ TRACE_EVENT_CONDITION(btrfs_get_extent, · 2 unmodified lines
327324 show_map_type(__entry->block_start),
328325 __entry->block_len,
329326 show_map_flags(__entry->flags),
330− __entry->refs, __entry->compress_type)
327+ __entry->refs)
331328 );
332329
333330 TRACE_EVENT(btrfs_handle_em_exist,
@@ -1061,7 +1058,7 @@ DEFINE_EVENT(btrfs_delayed_ref_head, run_delayed_ref_head, · 727 unmodified lines
10611058 DECLARE_EVENT_CLASS(btrfs__chunk,
10621059
10631060 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),
10651062
10661063 TP_ARGS(fs_info, map, offset, size),
10671064
@@ -1095,7 +1092,7 @@ DECLARE_EVENT_CLASS(btrfs__chunk, · 27 unmodified lines
10951092 DEFINE_EVENT(btrfs__chunk, btrfs_chunk_alloc,
10961093
10971094 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),
10991096
11001097 TP_ARGS(fs_info, map, offset, size)
11011098 );
@@ -1103,7 +1100,7 @@ DEFINE_EVENT(btrfs__chunk, btrfs_chunk_alloc, · 1 unmodified line
11031100 DEFINE_EVENT(btrfs__chunk, btrfs_chunk_free,
11041101
11051102 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),
11071104
11081105 TP_ARGS(fs_info, map, offset, size)
11091106 );
@@ -2099,17 +2096,12 @@ TRACE_EVENT(btrfs_set_extent_bit, · 989 unmodified lines
20992096 __field( unsigned, set_bits)
21002097 ),
21012098
2102− TP_fast_assign_btrfs(tree->fs_info,
2103− __entry->owner = tree->owner;
2104− if (tree->inode) {
2105− const struct btrfs_inode *inode = tree->inode;
2099+ TP_fast_assign_btrfs(extent_io_tree_to_fs_info(tree),
2100+ const struct btrfs_inode *inode = extent_io_tree_to_inode_const(tree);
21062101
2107− __entry->ino = btrfs_ino(inode);
2108− __entry->rootid = inode->root->root_key.objectid;
2109− } else {
2110− __entry->ino = 0;
2111− __entry->rootid = 0;
2112− }
2102+ __entry->owner = tree->owner;
2103+ __entry->ino = inode ? btrfs_ino(inode) : 0;
2104+ __entry->rootid = inode ? inode->root->root_key.objectid : 0;
21132105 __entry->start = start;
21142106 __entry->len = len;
21152107 __entry->set_bits = set_bits;
@@ -2137,17 +2129,12 @@ TRACE_EVENT(btrfs_clear_extent_bit, · 21 unmodified lines
21372129 __field( unsigned, clear_bits)
21382130 ),
21392131
2140− TP_fast_assign_btrfs(tree->fs_info,
2141− __entry->owner = tree->owner;
2142− if (tree->inode) {
2143− const struct btrfs_inode *inode = tree->inode;
2132+ TP_fast_assign_btrfs(extent_io_tree_to_fs_info(tree),
2133+ const struct btrfs_inode *inode = extent_io_tree_to_inode_const(tree);
21442134
2145− __entry->ino = btrfs_ino(inode);
2146− __entry->rootid = inode->root->root_key.objectid;
2147− } else {
2148− __entry->ino = 0;
2149− __entry->rootid = 0;
2150− }
2135+ __entry->owner = tree->owner;
2136+ __entry->ino = inode ? btrfs_ino(inode) : 0;
2137+ __entry->rootid = inode ? inode->root->root_key.objectid : 0;
21512138 __entry->start = start;
21522139 __entry->len = len;
21532140 __entry->clear_bits = clear_bits;
@@ -2176,17 +2163,12 @@ TRACE_EVENT(btrfs_convert_extent_bit, · 22 unmodified lines
21762163 __field( unsigned, clear_bits)
21772164 ),
21782165
2179− TP_fast_assign_btrfs(tree->fs_info,
2180− __entry->owner = tree->owner;
2181− if (tree->inode) {
2182− const struct btrfs_inode *inode = tree->inode;
2166+ TP_fast_assign_btrfs(extent_io_tree_to_fs_info(tree),
2167+ const struct btrfs_inode *inode = extent_io_tree_to_inode_const(tree);
21832168
2184− __entry->ino = btrfs_ino(inode);
2185− __entry->rootid = inode->root->root_key.objectid;
2186− } else {
2187− __entry->ino = 0;
2188− __entry->rootid = 0;
2189− }
2169+ __entry->owner = tree->owner;
2170+ __entry->ino = inode ? btrfs_ino(inode) : 0;
2171+ __entry->rootid = inode ? inode->root->root_key.objectid : 0;
21902172 __entry->start = start;
21912173 __entry->len = len;
21922174 __entry->set_bits = set_bits;