History

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

Pull btrfs updates from David Sterba: "New features: - raid-stripe-tree New tree for logical file extent mapping where the physical mapping may not match on multiple devices. This is now used in zoned mode to implement RAID0/RAID1* profiles, but can be used in non-zoned mode as well. The support for RAID56 is in development and will eventually fix the problems with the current implementation. This is a backward incompatible feature and has to be enabled at mkfs time. - simple quota accounting (squota) A simplified mode of qgroup that accounts all space on the initial extent owners (a subvolume), the snapshots are then cheap to create and delete. The deletion of snapshots in fully accounting qgroups is a known CPU/IO performance bottleneck. The squota is not suitable for the general use case but works well for containers where the original subvolume exists for the whole time. This is a backward incompatible feature as it needs extending some structures, but can be enabled on an existing filesystem. - temporary filesystem fsid (temp_fsid) The fsid identifies a filesystem and is hard coded in the structures, which disallows mounting the same fsid found on different devices. For a single device filesystem this is not strictly necessary, a new temporary fsid can be generated on mount e.g. after a device is cloned. This will be used by Steam Deck for root partition A/B testing, or can be used for VM root images. Other user visible changes: - filesystems with partially finished metadata_uuid conversion cannot be mounted anymore and the uuid fixup has to be done by btrfs-progs (btrfstune). Performance improvements: - reduce reservations for checksum deletions (with enabled free space tree by factor of 4), on a sample workload on file with many extents the deletion time decreased by 12% - make extent state merges more efficient during insertions, reduce rb-tree iterations (run time of critical functions reduced by 5%) Core changes: - the integrity check functionality has been removed, this was a debugging feature and removal does not affect other integrity checks like checksums or tree-checker - space reservation changes: - more efficient delayed ref reservations, this avoids building up too much work or overusing or exhausting the global block reserve in some situations - move delayed refs reservation to the transaction start time, this prevents some ENOSPC corner cases related to exhaustion of global reserve - improvements in reducing excessive reservations for block group items - adjust overcommit logic in near full situations, account for one more chunk to eventually allocate metadata chunk, this is mostly relevant for small filesystems (<10GiB) - single device filesystems are scanned but not registered (except seed devices), this allows temp_fsid to work - qgroup iterations do not need GFP_ATOMIC allocations anymore - cleanups, refactoring, reduced data structure size, function parameter simplifications, error handling fixes" * tag 'for-6.7-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: (156 commits) btrfs: open code timespec64 in struct btrfs_inode btrfs: remove redundant log root tree index assignment during log sync btrfs: remove redundant initialization of variable dirty in btrfs_update_time() btrfs: sysfs: show temp_fsid feature btrfs: disable the device add feature for temp-fsid btrfs: disable the seed feature for temp-fsid btrfs: update comment for temp-fsid, fsid, and metadata_uuid btrfs: remove pointless empty log context list check when syncing log btrfs: update comment for struct btrfs_inode::lock btrfs: remove pointless barrier from btrfs_sync_file() btrfs: add and use helpers for reading and writing last_trans_committed btrfs: add and use helpers for reading and writing fs_info->generation btrfs: add and use helpers for reading and writing log_transid btrfs: add and use helpers for reading and writing last_log_commit btrfs: support cloned-device mount capability btrfs: add helper function find_fsid_by_disk btrfs: stop reserving excessive space for block group item insertions btrfs: stop reserving excessive space for block group item updates btrfs: reorder btrfs_inode to fill gaps btrfs: open code btrfs_ordered_inode_tree in btrfs_inode ...

Linus Torvalds committed 3 years ago

Commit d5acbc62 parents 8829687 c6e8f8983 files · +4033 −5291Browse files

Unified · Split

83 files
  1. fs/btrfs/Kconfig +0 −21
  2. fs/btrfs/Makefile +1 −2
  3. fs/btrfs/accessors.h +16 −0
  4. fs/btrfs/async-thread.c +6 −6
  5. fs/btrfs/async-thread.h +3 −3
  6. fs/btrfs/backref.c +4 −1
  7. fs/btrfs/backref.h +5 −5
  8. fs/btrfs/bio.c +33 −14
  9. fs/btrfs/block-group.c +80 −94
  10. fs/btrfs/block-rsv.c +16 −8
  11. fs/btrfs/btrfs_inode.h +44 −36
  12. fs/btrfs/check-integrity.c +0 −2871
  13. fs/btrfs/check-integrity.h +0 −20
  14. fs/btrfs/compression.c +3 −3
  15. fs/btrfs/ctree.c +106 −234
  16. fs/btrfs/ctree.h +81 −61
  17. fs/btrfs/defrag.c +148 −4
  18. fs/btrfs/defrag.h +1 −1
  19. fs/btrfs/delalloc-space.c +2 −4
  20. fs/btrfs/delayed-inode.c +11 −16
  21. fs/btrfs/delayed-inode.h +0 −1
  22. fs/btrfs/delayed-ref.c +158 −41
  23. fs/btrfs/delayed-ref.h +53 −17
  24. fs/btrfs/dev-replace.c +1 −2
  25. fs/btrfs/dir-item.c +4 −4
  26. fs/btrfs/dir-item.h +9 −0
  27. fs/btrfs/disk-io.c +55 −87
  28. fs/btrfs/disk-io.h +2 −1
  29. fs/btrfs/extent-io-tree.c +180 −92
  30. fs/btrfs/extent-io-tree.h +3 −4
  31. fs/btrfs/extent-tree.c +360 −176
  32. fs/btrfs/extent-tree.h +10 −5
  33. fs/btrfs/extent_io.c +23 −16
  34. fs/btrfs/extent_io.h +2 −2
  35. fs/btrfs/file-item.c +9 −8
  36. fs/btrfs/file.c +32 −29
  37. fs/btrfs/free-space-cache.c +16 −12
  38. fs/btrfs/free-space-tree.c +9 −8
  39. fs/btrfs/fs.h +52 −17
  40. fs/btrfs/inode-item.c +11 −10
  41. fs/btrfs/inode-item.h +8 −0
  42. fs/btrfs/inode.c +101 −90
  43. fs/btrfs/ioctl.c +15 −8
  44. fs/btrfs/locking.c +16 −4
  45. fs/btrfs/messages.c +16 −16
  46. fs/btrfs/messages.h +7 −7
  47. fs/btrfs/ordered-data.c +57 −70
  48. fs/btrfs/ordered-data.h +2 −15
  49. fs/btrfs/print-tree.c +35 −0
  50. fs/btrfs/props.c +1 −0
  51. fs/btrfs/qgroup.c +560 −312
  52. fs/btrfs/qgroup.h +67 −82
  53. fs/btrfs/raid-stripe-tree.c +274 −0
  54. fs/btrfs/raid-stripe-tree.h +50 −0
  55. fs/btrfs/ref-verify.c +6 −3
  56. fs/btrfs/reflink.c +1 −2
  57. fs/btrfs/relocation.c +127 −81
  58. fs/btrfs/relocation.h +5 −4
  59. fs/btrfs/root-tree.c +7 −5
  60. fs/btrfs/root-tree.h +4 −4
  61. fs/btrfs/scrub.c +74 −4
  62. fs/btrfs/send.c +3 −3
  63. fs/btrfs/space-info.c +41 −23
  64. fs/btrfs/space-info.h +2 −1
  65. fs/btrfs/super.c +23 −64
  66. fs/btrfs/sysfs.c +52 −1
  67. fs/btrfs/tests/extent-buffer-tests.c +5 −1
  68. fs/btrfs/tests/inode-tests.c +10 −2
  69. fs/btrfs/transaction.c +140 −89
  70. fs/btrfs/transaction.h +11 −9
  71. fs/btrfs/tree-checker.c +47 −1
  72. fs/btrfs/tree-log.c +36 −45
  73. fs/btrfs/ulist.c +2 −1
  74. fs/btrfs/uuid-tree.c +3 −3
  75. fs/btrfs/verity.c +2 −2
  76. fs/btrfs/volumes.c +203 −214
  77. fs/btrfs/volumes.h +29 −10
  78. fs/btrfs/xattr.c +6 −6
  79. fs/btrfs/zoned.c +289 −163
  80. fs/btrfs/zstd.c +7 −4
  81. include/trace/events/btrfs.h +78 −5
  82. include/uapi/linux/btrfs.h +3 −0
  83. include/uapi/linux/btrfs_tree.h +59 −1
fs/btrfs/Kconfig+0−21
@@ -48,27 +48,6 @@ config BTRFS_FS_POSIX_ACL · 47 unmodified lines
4848
4949 If you don't know what Access Control Lists are, say N
5050
51−config BTRFS_FS_CHECK_INTEGRITY
52− bool "Btrfs with integrity check tool compiled in (DEPRECATED)"
53− depends on BTRFS_FS
54− help
55− This feature has been deprecated and will be removed in 6.7.
56−
57− Adds code that examines all block write requests (including
58− writes of the super block). The goal is to verify that the
59− state of the filesystem on disk is always consistent, i.e.,
60− after a power-loss or kernel panic event the filesystem is
61− in a consistent state.
62−
63− If the integrity check tool is included and activated in
64− the mount options, plenty of kernel memory is used, and
65− plenty of additional CPU cycles are spent. Enabling this
66− functionality is not intended for normal use.
67−
68− In most cases, unless you are a btrfs developer who needs
69− to verify the integrity of (super)-block write requests
70− during the run of a regression test, say N
71−
7251 config BTRFS_FS_RUN_SANITY_TESTS
7352 bool "Btrfs will run sanity tests upon loading"
7453 depends on BTRFS_FS
fs/btrfs/Makefile+1−2
@@ -33,10 +33,9 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ · 32 unmodified lines
3333 uuid-tree.o props.o free-space-tree.o tree-checker.o space-info.o \
3434 block-rsv.o delalloc-space.o block-group.o discard.o reflink.o \
3535 subpage.o tree-mod-log.o extent-io-tree.o fs.o messages.o bio.o \
36− lru_cache.o
36+ lru_cache.o raid-stripe-tree.o
3737
3838 btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o
39−btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
4039 btrfs-$(CONFIG_BTRFS_FS_REF_VERIFY) += ref-verify.o
4140 btrfs-$(CONFIG_BLK_DEV_ZONED) += zoned.o
4241 btrfs-$(CONFIG_FS_VERITY) += verity.o
fs/btrfs/accessors.h+16−0
@@ -4,6 +4,7 @@ · 3 unmodified lines
44 #define BTRFS_ACCESSORS_H
55
66 #include <linux/stddef.h>
7+#include <asm/unaligned.h>
78
89 struct btrfs_map_token {
910 struct extent_buffer *eb;
@@ -305,6 +306,14 @@ BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32); · 295 unmodified lines
305306 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
306307 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
307308
309+BTRFS_SETGET_FUNCS(stripe_extent_encoding, struct btrfs_stripe_extent, encoding, 8);
310+BTRFS_SETGET_FUNCS(raid_stride_devid, struct btrfs_raid_stride, devid, 64);
311+BTRFS_SETGET_FUNCS(raid_stride_physical, struct btrfs_raid_stride, physical, 64);
312+BTRFS_SETGET_STACK_FUNCS(stack_stripe_extent_encoding,
313+ struct btrfs_stripe_extent, encoding, 8);
314+BTRFS_SETGET_STACK_FUNCS(stack_raid_stride_devid, struct btrfs_raid_stride, devid, 64);
315+BTRFS_SETGET_STACK_FUNCS(stack_raid_stride_physical, struct btrfs_raid_stride, physical, 64);
316+
308317 /* struct btrfs_dev_extent */
309318 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent, chunk_tree, 64);
310319 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
@@ -349,6 +358,9 @@ BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref, count, 3 · 38 unmodified lines
349358
350359 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32);
351360
361+BTRFS_SETGET_FUNCS(extent_owner_ref_root_id, struct btrfs_extent_owner_ref,
362+ root_id, 64);
363+
352364 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
353365 type, 8);
354366 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
@@ -365,6 +377,8 @@ static inline u32 btrfs_extent_inline_ref_size(int type) · 10 unmodified lines
365377 if (type == BTRFS_EXTENT_DATA_REF_KEY)
366378 return sizeof(struct btrfs_extent_data_ref) +
367379 offsetof(struct btrfs_extent_inline_ref, offset);
380+ if (type == BTRFS_EXTENT_OWNER_REF_KEY)
381+ return sizeof(struct btrfs_extent_inline_ref);
368382 return 0;
369383 }
370384
@@ -966,6 +980,8 @@ BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item, · 595 unmodified lines
966980 flags, 64);
967981 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
968982 rescan, 64);
983+BTRFS_SETGET_FUNCS(qgroup_status_enable_gen, struct btrfs_qgroup_status_item,
984+ enable_gen, 64);
969985
970986 /* btrfs_qgroup_info_item */
971987 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
fs/btrfs/async-thread.c+6−6
@@ -9,6 +9,7 @@ · 8 unmodified lines
99 #include <linux/list.h>
1010 #include <linux/spinlock.h>
1111 #include <linux/freezer.h>
12+#include <trace/events/btrfs.h>
1213 #include "async-thread.h"
1314 #include "ctree.h"
1415
@@ -242,7 +243,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq, · 227 unmodified lines
242243 break;
243244 trace_btrfs_ordered_sched(work);
244245 spin_unlock_irqrestore(lock, flags);
245− work->ordered_func(work);
246+ work->ordered_func(work, false);
246247
247248 /* now take the lock again and drop our item from the list */
248249 spin_lock_irqsave(lock, flags);
@@ -277,7 +278,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq, · 28 unmodified lines
277278 * We don't want to call the ordered free functions with
278279 * the lock held.
279280 */
280− work->ordered_free(work);
281+ work->ordered_func(work, true);
281282 /* NB: work must not be dereferenced past this point. */
282283 trace_btrfs_all_work_done(wq->fs_info, work);
283284 }
@@ -285,7 +286,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq, · 1 unmodified line
285286 spin_unlock_irqrestore(lock, flags);
286287
287288 if (free_self) {
288− self->ordered_free(self);
289+ self->ordered_func(self, true);
289290 /* NB: self must not be dereferenced past this point. */
290291 trace_btrfs_all_work_done(wq->fs_info, self);
291292 }
@@ -300,7 +301,7 @@ static void btrfs_work_helper(struct work_struct *normal_work) · 8 unmodified lines
300301
301302 /*
302303 * We should not touch things inside work in the following cases:
303− * 1) after work->func() if it has no ordered_free
304+ * 1) after work->func() if it has no ordered_func(..., true) to free
304305 * Since the struct is freed in work->func().
305306 * 2) after setting WORK_DONE_BIT
306307 * The work may be freed in other threads almost instantly.
@@ -329,11 +330,10 @@ static void btrfs_work_helper(struct work_struct *normal_work) · 22 unmodified lines
329330 }
330331
331332 void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,
332− btrfs_func_t ordered_func, btrfs_func_t ordered_free)
333+ btrfs_ordered_func_t ordered_func)
333334 {
334335 work->func = func;
335336 work->ordered_func = ordered_func;
336− work->ordered_free = ordered_free;
337337 INIT_WORK(&work->normal_work, btrfs_work_helper);
338338 INIT_LIST_HEAD(&work->ordered_list);
339339 work->flags = 0;
fs/btrfs/async-thread.h+3−3
@@ -13,11 +13,11 @@ struct btrfs_fs_info; · 12 unmodified lines
1313 struct btrfs_workqueue;
1414 struct btrfs_work;
1515 typedef void (*btrfs_func_t)(struct btrfs_work *arg);
16+typedef void (*btrfs_ordered_func_t)(struct btrfs_work *arg, bool);
1617
1718 struct btrfs_work {
1819 btrfs_func_t func;
19− btrfs_func_t ordered_func;
20− btrfs_func_t ordered_free;
20+ btrfs_ordered_func_t ordered_func;
2121
2222 /* Don't touch things below */
2323 struct work_struct normal_work;
@@ -35,7 +35,7 @@ struct btrfs_workqueue *btrfs_alloc_ordered_workqueue( · 11 unmodified lines
3535 struct btrfs_fs_info *fs_info, const char *name,
3636 unsigned int flags);
3737 void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,
38− btrfs_func_t ordered_func, btrfs_func_t ordered_free);
38+ btrfs_ordered_func_t ordered_func);
3939 void btrfs_queue_work(struct btrfs_workqueue *wq,
4040 struct btrfs_work *work);
4141 void btrfs_destroy_workqueue(struct btrfs_workqueue *wq);
fs/btrfs/backref.c+4−1
@@ -1129,6 +1129,9 @@ static int add_inline_refs(struct btrfs_backref_walk_ctx *ctx, · 1128 unmodified lines
11291129 count, sc, GFP_NOFS);
11301130 break;
11311131 }
1132+ case BTRFS_EXTENT_OWNER_REF_KEY:
1133+ ASSERT(btrfs_fs_incompat(ctx->fs_info, SIMPLE_QUOTA));
1134+ break;
11321135 default:
11331136 WARN_ON(1);
11341137 }
@@ -2998,7 +3001,7 @@ int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) · 1863 unmodified lines
29983001 }
29993002
30003003 void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
3001− struct btrfs_backref_cache *cache, int is_reloc)
3004+ struct btrfs_backref_cache *cache, bool is_reloc)
30023005 {
30033006 int i;
30043007
fs/btrfs/backref.h+5−5
@@ -247,7 +247,7 @@ struct prelim_ref { · 246 unmodified lines
247247 struct rb_node rbnode;
248248 u64 root_id;
249249 struct btrfs_key key_for_search;
250− int level;
250+ u8 level;
251251 int count;
252252 struct extent_inode_elem *inode_list;
253253 u64 parent;
@@ -440,11 +440,11 @@ struct btrfs_backref_cache { · 186 unmodified lines
440440 * Reloction backref cache require more info for reloc root compared
441441 * to generic backref cache.
442442 */
443− unsigned int is_reloc;
443+ bool is_reloc;
444444 };
445445
446446 void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
447− struct btrfs_backref_cache *cache, int is_reloc);
447+ struct btrfs_backref_cache *cache, bool is_reloc);
448448 struct btrfs_backref_node *btrfs_backref_alloc_node(
449449 struct btrfs_backref_cache *cache, u64 bytenr, int level);
450450 struct btrfs_backref_edge *btrfs_backref_alloc_edge(
@@ -533,9 +533,9 @@ void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, · 82 unmodified lines
533533 void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
534534
535535 static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
536− u64 bytenr, int errno)
536+ u64 bytenr, int error)
537537 {
538− btrfs_panic(fs_info, errno,
538+ btrfs_panic(fs_info, error,
539539 "Inconsistency in backref cache found at offset %llu",
540540 bytenr);
541541 }
fs/btrfs/bio.c+33−14
@@ -10,11 +10,11 @@ · 9 unmodified lines
1010 #include "volumes.h"
1111 #include "raid56.h"
1212 #include "async-thread.h"
13−#include "check-integrity.h"
1413 #include "dev-replace.h"
1514 #include "rcu-string.h"
1615 #include "zoned.h"
1716 #include "file-item.h"
17+#include "raid-stripe-tree.h"
1818
1919 static struct bio_set btrfs_bioset;
2020 static struct bio_set btrfs_clone_bioset;
@@ -416,6 +416,9 @@ static void btrfs_orig_write_end_io(struct bio *bio) · 395 unmodified lines
416416 else
417417 bio->bi_status = BLK_STS_OK;
418418
419+ if (bio_op(bio) == REQ_OP_ZONE_APPEND && !bio->bi_status)
420+ stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
421+
419422 btrfs_orig_bbio_end_io(bbio);
420423 btrfs_put_bioc(bioc);
421424 }
@@ -427,6 +430,8 @@ static void btrfs_clone_write_end_io(struct bio *bio) · 5 unmodified lines
427430 if (bio->bi_status) {
428431 atomic_inc(&stripe->bioc->error);
429432 btrfs_log_dev_io_error(bio, stripe->dev);
433+ } else if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
434+ stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
430435 }
431436
432437 /* Pass on control to the original bio this one was cloned from */
@@ -463,8 +468,6 @@ static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio) · 30 unmodified lines
463468 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),
464469 dev->devid, bio->bi_iter.bi_size);
465470
466− btrfsic_check_bio(bio);
467−
468471 if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT)
469472 blkcg_punt_bio_submit(bio);
470473 else
@@ -490,6 +493,7 @@ static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr) · 19 unmodified lines
490493 bio->bi_private = &bioc->stripes[dev_nr];
491494 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;
492495 bioc->stripes[dev_nr].bioc = bioc;
496+ bioc->size = bio->bi_iter.bi_size;
493497 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
494498 }
495499
@@ -499,6 +503,8 @@ static void __btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc, · 3 unmodified lines
499503 if (!bioc) {
500504 /* Single mirror read/write fast path. */
501505 btrfs_bio(bio)->mirror_num = mirror_num;
506+ if (bio_op(bio) != REQ_OP_READ)
507+ btrfs_bio(bio)->orig_physical = smap->physical;
502508 bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;
503509 if (bio_op(bio) != REQ_OP_READ)
504510 btrfs_bio(bio)->orig_physical = smap->physical;
@@ -568,13 +574,20 @@ static void run_one_async_start(struct btrfs_work *work) · 63 unmodified lines
568574 *
569575 * At IO completion time the csums attached on the ordered extent record are
570576 * inserted into the tree.
577+ *
578+ * If called with @do_free == true, then it will free the work struct.
571579 */
572−static void run_one_async_done(struct btrfs_work *work)
580+static void run_one_async_done(struct btrfs_work *work, bool do_free)
573581 {
574582 struct async_submit_bio *async =
575583 container_of(work, struct async_submit_bio, work);
576584 struct bio *bio = &async->bbio->bio;
577585
586+ if (do_free) {
587+ kfree(container_of(work, struct async_submit_bio, work));
588+ return;
589+ }
590+
578591 /* If an error occurred we just want to clean up the bio and move on. */
579592 if (bio->bi_status) {
580593 btrfs_orig_bbio_end_io(async->bbio);
@@ -590,11 +603,6 @@ static void run_one_async_done(struct btrfs_work *work) · 9 unmodified lines
590603 __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);
591604 }
592605
593−static void run_one_async_free(struct btrfs_work *work)
594−{
595− kfree(container_of(work, struct async_submit_bio, work));
596−}
597−
598606 static bool should_async_write(struct btrfs_bio *bbio)
599607 {
600608 /* Submit synchronously if the checksum implementation is fast. */
@@ -636,8 +644,7 @@ static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio, · 35 unmodified lines
636644 async->smap = *smap;
637645 async->mirror_num = mirror_num;
638646
639− btrfs_init_work(&async->work, run_one_async_start, run_one_async_done,
640− run_one_async_free);
647+ btrfs_init_work(&async->work, run_one_async_start, run_one_async_done);
641648 btrfs_queue_work(fs_info->workers, &async->work);
642649 return true;
643650 }
@@ -657,9 +664,11 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) · 13 unmodified lines
657664 blk_status_t ret;
658665 int error;
659666
667+ smap.is_scrub = !bbio->inode;
668+
660669 btrfs_bio_counter_inc_blocked(fs_info);
661670 error = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
662− &bioc, &smap, &mirror_num, 1);
671+ &bioc, &smap, &mirror_num);
663672 if (error) {
664673 ret = errno_to_blk_status(error);
665674 goto fail;
@@ -691,6 +700,18 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) · 25 unmodified lines
691700 bio->bi_opf |= REQ_OP_ZONE_APPEND;
692701 }
693702
703+ if (is_data_bbio(bbio) && bioc &&
704+ btrfs_need_stripe_tree_update(bioc->fs_info, bioc->map_type)) {
705+ /*
706+ * No locking for the list update, as we only add to
707+ * the list in the I/O submission path, and list
708+ * iteration only happens in the completion path, which
709+ * can't happen until after the last submission.
710+ */
711+ btrfs_get_bioc(bioc);
712+ list_add_tail(&bioc->rst_ordered_entry, &bbio->ordered->bioc_list);
713+ }
714+
694715 /*
695716 * Csum items for reloc roots have already been cloned at this
696717 * point, so they are handled as part of the no-checksum case.
@@ -779,8 +800,6 @@ int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, · 82 unmodified lines
779800 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
780801 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
781802 __bio_add_page(&bio, page, length, pg_offset);
782−
783− btrfsic_check_bio(&bio);
784803 ret = submit_bio_wait(&bio);
785804 if (ret) {
786805 /* try to remap that extent elsewhere? */
fs/btrfs/block-group.c+80−94
@@ -935,7 +935,7 @@ int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait) · 934 unmodified lines
935935 caching_ctl->block_group = cache;
936936 refcount_set(&caching_ctl->count, 2);
937937 atomic_set(&caching_ctl->progress, 0);
938− btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
938+ btrfs_init_work(&caching_ctl->work, caching_thread, NULL);
939939
940940 spin_lock(&cache->lock);
941941 if (cache->cached != BTRFS_CACHE_NO) {
@@ -1286,7 +1286,7 @@ out: · 344 unmodified lines
12861286 /* Once for the lookup reference */
12871287 btrfs_put_block_group(block_group);
12881288 if (remove_rsv)
1289− btrfs_delayed_refs_rsv_release(fs_info, 1);
1289+ btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
12901290 btrfs_free_path(path);
12911291 return ret;
12921292 }
@@ -2601,7 +2601,7 @@ static int insert_dev_extent(struct btrfs_trans_handle *trans, · 1308 unmodified lines
26012601 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
26022602
26032603 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
2604− btrfs_mark_buffer_dirty(leaf);
2604+ btrfs_mark_buffer_dirty(trans, leaf);
26052605 out:
26062606 btrfs_free_path(path);
26072607 return ret;
@@ -2709,7 +2709,7 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) · 101 unmodified lines
27092709
27102710 /* Already aborted the transaction if it failed. */
27112711 next:
2712− btrfs_delayed_refs_rsv_release(fs_info, 1);
2712+ btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
27132713 list_del_init(&block_group->bg_list);
27142714 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags);
27152715 }
@@ -2819,8 +2819,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran · 103 unmodified lines
28192819 #endif
28202820
28212821 list_add_tail(&cache->bg_list, &trans->new_bgs);
2822− trans->delayed_ref_updates++;
2823− btrfs_update_delayed_refs_rsv(trans);
2822+ btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info);
28242823
28252824 set_avail_alloc_bits(fs_info, type);
28262825 return cache;
@@ -3025,7 +3024,7 @@ static int update_block_group_item(struct btrfs_trans_handle *trans, · 198 unmodified lines
30253024 cache->global_root_id);
30263025 btrfs_set_stack_block_group_flags(&bgi, cache->flags);
30273026 write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
3028− btrfs_mark_buffer_dirty(leaf);
3027+ btrfs_mark_buffer_dirty(trans, leaf);
30293028 fail:
30303029 btrfs_release_path(path);
30313030 /*
@@ -3051,7 +3050,6 @@ static int cache_save_setup(struct btrfs_block_group *block_group, · 19 unmodified lines
30513050 struct btrfs_path *path)
30523051 {
30533052 struct btrfs_fs_info *fs_info = block_group->fs_info;
3054− struct btrfs_root *root = fs_info->tree_root;
30553053 struct inode *inode = NULL;
30563054 struct extent_changeset *data_reserved = NULL;
30573055 u64 alloc_hint = 0;
@@ -3103,7 +3101,7 @@ again: · 45 unmodified lines
31033101 * time.
31043102 */
31053103 BTRFS_I(inode)->generation = 0;
3106− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
3104+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
31073105 if (ret) {
31083106 /*
31093107 * So theoretically we could recover from this, simply set the
@@ -3370,7 +3368,7 @@ again: · 260 unmodified lines
33703368 if (should_put)
33713369 btrfs_put_block_group(cache);
33723370 if (drop_reserve)
3373− btrfs_delayed_refs_rsv_release(fs_info, 1);
3371+ btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
33743372 /*
33753373 * Avoid blocking other tasks for too long. It might even save
33763374 * us from writing caches for block groups that are going to be
@@ -3474,8 +3472,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) · 97 unmodified lines
34743472 cache_save_setup(cache, trans, path);
34753473
34763474 if (!ret)
3477− ret = btrfs_run_delayed_refs(trans,
3478− (unsigned long) -1);
3475+ ret = btrfs_run_delayed_refs(trans, U64_MAX);
34793476
34803477 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
34813478 cache->io_ctl.inode = NULL;
@@ -3518,7 +3515,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) · 36 unmodified lines
35183515 /* If its not on the io list, we need to put the block group */
35193516 if (should_put)
35203517 btrfs_put_block_group(cache);
3521− btrfs_delayed_refs_rsv_release(fs_info, 1);
3518+ btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
35223519 spin_lock(&cur_trans->dirty_bgs_lock);
35233520 }
35243521 spin_unlock(&cur_trans->dirty_bgs_lock);
@@ -3543,12 +3540,12 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, · 18 unmodified lines
35433540 u64 bytenr, u64 num_bytes, bool alloc)
35443541 {
35453542 struct btrfs_fs_info *info = trans->fs_info;
3546− struct btrfs_block_group *cache = NULL;
3547− u64 total = num_bytes;
3543+ struct btrfs_space_info *space_info;
3544+ struct btrfs_block_group *cache;
35483545 u64 old_val;
3549− u64 byte_in_group;
3546+ bool reclaim = false;
3547+ bool bg_already_dirty = true;
35503548 int factor;
3551− int ret = 0;
35523549
35533550 /* Block accounting for super block */
35543551 spin_lock(&info->delalloc_root_lock);
@@ -3560,97 +3557,86 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, · 5 unmodified lines
35603557 btrfs_set_super_bytes_used(info->super_copy, old_val);
35613558 spin_unlock(&info->delalloc_root_lock);
35623559
3563− while (total) {
3564− struct btrfs_space_info *space_info;
3565− bool reclaim = false;
3566−
3567− cache = btrfs_lookup_block_group(info, bytenr);
3568− if (!cache) {
3569− ret = -ENOENT;
3570− break;
3571− }
3572− space_info = cache->space_info;
3573− factor = btrfs_bg_type_to_factor(cache->flags);
3560+ cache = btrfs_lookup_block_group(info, bytenr);
3561+ if (!cache)
3562+ return -ENOENT;
35743563
3575− /*
3576− * If this block group has free space cache written out, we
3577− * need to make sure to load it if we are removing space. This
3578− * is because we need the unpinning stage to actually add the
3579− * space back to the block group, otherwise we will leak space.
3580− */
3581− if (!alloc && !btrfs_block_group_done(cache))
3582− btrfs_cache_block_group(cache, true);
3564+ /* An extent can not span multiple block groups. */
3565+ ASSERT(bytenr + num_bytes <= cache->start + cache->length);
35833566
3584− byte_in_group = bytenr - cache->start;
3585− WARN_ON(byte_in_group > cache->length);
3567+ space_info = cache->space_info;
3568+ factor = btrfs_bg_type_to_factor(cache->flags);
35863569
3587− spin_lock(&space_info->lock);
3588− spin_lock(&cache->lock);
3570+ /*
3571+ * If this block group has free space cache written out, we need to make
3572+ * sure to load it if we are removing space. This is because we need
3573+ * the unpinning stage to actually add the space back to the block group,
3574+ * otherwise we will leak space.
3575+ */
3576+ if (!alloc && !btrfs_block_group_done(cache))
3577+ btrfs_cache_block_group(cache, true);
35893578
3590− if (btrfs_test_opt(info, SPACE_CACHE) &&
3591− cache->disk_cache_state < BTRFS_DC_CLEAR)
3592− cache->disk_cache_state = BTRFS_DC_CLEAR;
3579+ spin_lock(&space_info->lock);
3580+ spin_lock(&cache->lock);
35933581
3594− old_val = cache->used;
3595− num_bytes = min(total, cache->length - byte_in_group);
3596− if (alloc) {
3597− old_val += num_bytes;
3598− cache->used = old_val;
3599− cache->reserved -= num_bytes;
3600− space_info->bytes_reserved -= num_bytes;
3601− space_info->bytes_used += num_bytes;
3602− space_info->disk_used += num_bytes * factor;
3603− spin_unlock(&cache->lock);
3604− spin_unlock(&space_info->lock);
3605− } else {
3606− old_val -= num_bytes;
3607− cache->used = old_val;
3608− cache->pinned += num_bytes;
3609− btrfs_space_info_update_bytes_pinned(info, space_info,
3610− num_bytes);
3611− space_info->bytes_used -= num_bytes;
3612− space_info->disk_used -= num_bytes * factor;
3582+ if (btrfs_test_opt(info, SPACE_CACHE) &&
3583+ cache->disk_cache_state < BTRFS_DC_CLEAR)
3584+ cache->disk_cache_state = BTRFS_DC_CLEAR;
36133585
3614− reclaim = should_reclaim_block_group(cache, num_bytes);
3586+ old_val = cache->used;
3587+ if (alloc) {
3588+ old_val += num_bytes;
3589+ cache->used = old_val;
3590+ cache->reserved -= num_bytes;
3591+ space_info->bytes_reserved -= num_bytes;
3592+ space_info->bytes_used += num_bytes;
3593+ space_info->disk_used += num_bytes * factor;
3594+ spin_unlock(&cache->lock);
3595+ spin_unlock(&space_info->lock);
3596+ } else {
3597+ old_val -= num_bytes;
3598+ cache->used = old_val;
3599+ cache->pinned += num_bytes;
3600+ btrfs_space_info_update_bytes_pinned(info, space_info, num_bytes);
3601+ space_info->bytes_used -= num_bytes;
3602+ space_info->disk_used -= num_bytes * factor;
36153603
3616− spin_unlock(&cache->lock);
3617− spin_unlock(&space_info->lock);
3604+ reclaim = should_reclaim_block_group(cache, num_bytes);
36183605
3619− set_extent_bit(&trans->transaction->pinned_extents,
3620− bytenr, bytenr + num_bytes - 1,
3621− EXTENT_DIRTY, NULL);
3622− }
3606+ spin_unlock(&cache->lock);
3607+ spin_unlock(&space_info->lock);
36233608
3624− spin_lock(&trans->transaction->dirty_bgs_lock);
3625− if (list_empty(&cache->dirty_list)) {
3626− list_add_tail(&cache->dirty_list,
3627− &trans->transaction->dirty_bgs);
3628− trans->delayed_ref_updates++;
3629− btrfs_get_block_group(cache);
3630− }
3631− spin_unlock(&trans->transaction->dirty_bgs_lock);
3609+ set_extent_bit(&trans->transaction->pinned_extents, bytenr,
3610+ bytenr + num_bytes - 1, EXTENT_DIRTY, NULL);
3611+ }
36323612
3633− /*
3634− * No longer have used bytes in this block group, queue it for
3635− * deletion. We do this after adding the block group to the
3636− * dirty list to avoid races between cleaner kthread and space
3637− * cache writeout.
3638− */
3639− if (!alloc && old_val == 0) {
3640− if (!btrfs_test_opt(info, DISCARD_ASYNC))
3641− btrfs_mark_bg_unused(cache);
3642− } else if (!alloc && reclaim) {
3643− btrfs_mark_bg_to_reclaim(cache);
3644− }
3613+ spin_lock(&trans->transaction->dirty_bgs_lock);
3614+ if (list_empty(&cache->dirty_list)) {
3615+ list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs);
3616+ bg_already_dirty = false;
3617+ btrfs_get_block_group(cache);
3618+ }
3619+ spin_unlock(&trans->transaction->dirty_bgs_lock);
36453620
3646− btrfs_put_block_group(cache);
3647− total -= num_bytes;
3648− bytenr += num_bytes;
3621+ /*
3622+ * No longer have used bytes in this block group, queue it for deletion.
3623+ * We do this after adding the block group to the dirty list to avoid
3624+ * races between cleaner kthread and space cache writeout.
3625+ */
3626+ if (!alloc && old_val == 0) {
3627+ if (!btrfs_test_opt(info, DISCARD_ASYNC))
3628+ btrfs_mark_bg_unused(cache);
3629+ } else if (!alloc && reclaim) {
3630+ btrfs_mark_bg_to_reclaim(cache);
36493631 }
36503632
3633+ btrfs_put_block_group(cache);
3634+
36513635 /* Modified block groups are accounted for in the delayed_refs_rsv. */
3652− btrfs_update_delayed_refs_rsv(trans);
3653− return ret;
3636+ if (!bg_already_dirty)
3637+ btrfs_inc_delayed_refs_rsv_bg_updates(info);
3638+
3639+ return 0;
36543640 }
36553641
36563642 /*
fs/btrfs/block-rsv.c+16−8
@@ -221,7 +221,8 @@ int btrfs_block_rsv_add(struct btrfs_fs_info *fs_info, · 220 unmodified lines
221221 if (num_bytes == 0)
222222 return 0;
223223
224− ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
224+ ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
225+ num_bytes, flush);
225226 if (!ret)
226227 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
227228
@@ -261,7 +262,8 @@ int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info, · 33 unmodified lines
261262 if (!ret)
262263 return 0;
263264
264− ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
265+ ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
266+ num_bytes, flush);
265267 if (!ret) {
266268 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
267269 return 0;
@@ -279,10 +281,10 @@ u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, · 11 unmodified lines
279281 struct btrfs_block_rsv *target = NULL;
280282
281283 /*
282− * If we are the delayed_rsv then push to the global rsv, otherwise dump
283− * into the delayed rsv if it is not full.
284+ * If we are a delayed block reserve then push to the global rsv,
285+ * otherwise dump into the global delayed reserve if it is not full.
284286 */
285− if (block_rsv == delayed_rsv)
287+ if (block_rsv->type == BTRFS_BLOCK_RSV_DELOPS)
286288 target = global_rsv;
287289 else if (block_rsv != global_rsv && !btrfs_block_rsv_full(delayed_rsv))
288290 target = delayed_rsv;
@@ -354,6 +356,11 @@ void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info) · 65 unmodified lines
354356 min_items++;
355357 }
356358
359+ if (btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE)) {
360+ num_bytes += btrfs_root_used(&fs_info->stripe_root->root_item);
361+ min_items++;
362+ }
363+
357364 /*
358365 * But we also want to reserve enough space so we can do the fallback
359366 * global reserve for an unlink, which is an additional
@@ -405,6 +412,7 @@ void btrfs_init_root_block_rsv(struct btrfs_root *root) · 45 unmodified lines
405412 case BTRFS_EXTENT_TREE_OBJECTID:
406413 case BTRFS_FREE_SPACE_TREE_OBJECTID:
407414 case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
415+ case BTRFS_RAID_STRIPE_TREE_OBJECTID:
408416 root->block_rsv = &fs_info->delayed_refs_rsv;
409417 break;
410418 case BTRFS_ROOT_TREE_OBJECTID:
@@ -517,8 +525,8 @@ again: · 106 unmodified lines
517525 block_rsv->type, ret);
518526 }
519527 try_reserve:
520− ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
521− BTRFS_RESERVE_NO_FLUSH);
528+ ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
529+ blocksize, BTRFS_RESERVE_NO_FLUSH);
522530 if (!ret)
523531 return block_rsv;
524532 /*
@@ -539,7 +547,7 @@ try_reserve: · 14 unmodified lines
539547 * one last time to force a reservation if there's enough actual space
540548 * on disk to make the reservation.
541549 */
542− ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
550+ ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info, blocksize,
543551 BTRFS_RESERVE_FLUSH_EMERGENCY);
544552 if (!ret)
545553 return block_rsv;
fs/btrfs/btrfs_inode.h+44−36
@@ -8,6 +8,8 @@ · 7 unmodified lines
88
99 #include <linux/hash.h>
1010 #include <linux/refcount.h>
11+#include <linux/fscrypt.h>
12+#include <trace/events/btrfs.h>
1113 #include "extent_map.h"
1214 #include "extent_io.h"
1315 #include "ordered-data.h"
@@ -79,11 +81,21 @@ struct btrfs_inode { · 65 unmodified lines
7981 */
8082 struct btrfs_key location;
8183
84+ /* Cached value of inode property 'compression'. */
85+ u8 prop_compress;
86+
87+ /*
88+ * Force compression on the file using the defrag ioctl, could be
89+ * different from prop_compress and takes precedence if set.
90+ */
91+ u8 defrag_compress;
92+
8293 /*
8394 * Lock for counters and all fields used to determine if the inode is in
8495 * the log or not (last_trans, last_sub_trans, last_log_commit,
85− * logged_trans), to access/update new_delalloc_bytes and to update the
86− * VFS' inode number of bytes used.
96+ * logged_trans), to access/update delalloc_bytes, new_delalloc_bytes,
97+ * defrag_bytes, disk_i_size, outstanding_extents, csum_bytes and to
98+ * update the VFS' inode number of bytes used.
8799 */
88100 spinlock_t lock;
89101
@@ -102,8 +114,18 @@ struct btrfs_inode { · 12 unmodified lines
102114 /* held while logging the inode in tree-log.c */
103115 struct mutex log_mutex;
104116
117+ /*
118+ * Counters to keep track of the number of extent item's we may use due
119+ * to delalloc and such. outstanding_extents is the number of extent
120+ * items we think we'll end up using, and reserved_extents is the number
121+ * of extent items we've reserved metadata for. Protected by 'lock'.
122+ */
123+ unsigned outstanding_extents;
124+
105125 /* used to order data wrt metadata */
106− struct btrfs_ordered_inode_tree ordered_tree;
126+ spinlock_t ordered_tree_lock;
127+ struct rb_root ordered_tree;
128+ struct rb_node *ordered_tree_last;
107129
108130 /* list of all the delalloc inodes in the FS. There are times we need
109131 * to write all the delalloc pages to disk, and this list is used
@@ -122,28 +144,31 @@ struct btrfs_inode { · 12 unmodified lines
122144 u64 generation;
123145
124146 /*
125− * transid of the trans_handle that last modified this inode
147+ * ID of the transaction handle that last modified this inode.
148+ * Protected by 'lock'.
126149 */
127150 u64 last_trans;
128151
129152 /*
130− * transid that last logged this inode
153+ * ID of the transaction that last logged this inode.
154+ * Protected by 'lock'.
131155 */
132156 u64 logged_trans;
133157
134158 /*
135− * log transid when this inode was last modified
159+ * Log transaction ID when this inode was last modified.
160+ * Protected by 'lock'.
136161 */
137162 int last_sub_trans;
138163
139− /* a local copy of root's last_log_commit */
164+ /* A local copy of root's last_log_commit. Protected by 'lock'. */
140165 int last_log_commit;
141166
142167 union {
143168 /*
144169 * Total number of bytes pending delalloc, used by stat to
145170 * calculate the real block usage of the file. This is used
146− * only for files.
171+ * only for files. Protected by 'lock'.
147172 */
148173 u64 delalloc_bytes;
149174 /*
@@ -161,7 +186,7 @@ struct btrfs_inode { · 11 unmodified lines
161186 * Total number of bytes pending delalloc that fall within a file
162187 * range that is either a hole or beyond EOF (and no prealloc extent
163188 * exists in the range). This is always <= delalloc_bytes and this
164− * is used only for files.
189+ * is used only for files. Protected by 'lock'.
165190 */
166191 u64 new_delalloc_bytes;
167192 /*
@@ -172,15 +197,15 @@ struct btrfs_inode { · 4 unmodified lines
172197 };
173198
174199 /*
175− * total number of bytes pending defrag, used by stat to check whether
176− * it needs COW.
200+ * Total number of bytes pending defrag, used by stat to check whether
201+ * it needs COW. Protected by 'lock'.
177202 */
178203 u64 defrag_bytes;
179204
180205 /*
181− * the size of the file stored in the metadata on disk. data=ordered
206+ * The size of the file stored in the metadata on disk. data=ordered
182207 * means the in-memory i_size might be larger than the size on disk
183− * because not all the blocks are written yet.
208+ * because not all the blocks are written yet. Protected by 'lock'.
184209 */
185210 u64 disk_i_size;
186211
@@ -214,7 +239,7 @@ struct btrfs_inode { · 27 unmodified lines
214239
215240 /*
216241 * Number of bytes outstanding that are going to need csums. This is
217− * used in ENOSPC accounting.
242+ * used in ENOSPC accounting. Protected by 'lock'.
218243 */
219244 u64 csum_bytes;
220245
@@ -223,30 +248,13 @@ struct btrfs_inode { · 2 unmodified lines
223248 /* Read-only compatibility flags, upper half of inode_item::flags */
224249 u32 ro_flags;
225250
226− /*
227− * Counters to keep track of the number of extent item's we may use due
228− * to delalloc and such. outstanding_extents is the number of extent
229− * items we think we'll end up using, and reserved_extents is the number
230− * of extent items we've reserved metadata for.
231− */
232− unsigned outstanding_extents;
233−
234251 struct btrfs_block_rsv block_rsv;
235252
236− /*
237− * Cached values of inode properties
238− */
239− unsigned prop_compress; /* per-file compression algorithm */
240− /*
241− * Force compression on the file using the defrag ioctl, could be
242− * different from prop_compress and takes precedence if set
243− */
244− unsigned defrag_compress;
245−
246253 struct btrfs_delayed_node *delayed_node;
247254
248255 /* File creation time. */
249− struct timespec64 i_otime;
256+ u64 i_otime_sec;
257+ u32 i_otime_nsec;
250258
251259 /* Hook into fs_info->delayed_iputs */
252260 struct list_head delayed_iput;
@@ -387,7 +395,7 @@ static inline bool btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation) · 134 unmodified lines
387395 spin_lock(&inode->lock);
388396 if (inode->logged_trans == generation &&
389397 inode->last_sub_trans <= inode->last_log_commit &&
390− inode->last_sub_trans <= inode->root->last_log_commit)
398+ inode->last_sub_trans <= btrfs_get_root_last_log_commit(inode->root))
391399 ret = true;
392400 spin_unlock(&inode->lock);
393401 return ret;
@@ -481,9 +489,9 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, · 87 unmodified lines
481489 struct page *page, size_t pg_offset,
482490 u64 start, u64 end);
483491 int btrfs_update_inode(struct btrfs_trans_handle *trans,
484− struct btrfs_root *root, struct btrfs_inode *inode);
492+ struct btrfs_inode *inode);
485493 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
486− struct btrfs_root *root, struct btrfs_inode *inode);
494+ struct btrfs_inode *inode);
487495 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);
488496 int btrfs_orphan_cleanup(struct btrfs_root *root);
489497 int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
fs/btrfs/check-integrity.c+0−2871
@@ -1,2871 +0,0 @@
1−// SPDX-License-Identifier: GPL-2.0
2−/*
3− * Copyright (C) STRATO AG 2011. All rights reserved.
4− */
5−
6−/*
7− * This module can be used to catch cases when the btrfs kernel
8− * code executes write requests to the disk that bring the file
9− * system in an inconsistent state. In such a state, a power-loss
10− * or kernel panic event would cause that the data on disk is
11− * lost or at least damaged.
12− *
13− * Code is added that examines all block write requests during
14− * runtime (including writes of the super block). Three rules
15− * are verified and an error is printed on violation of the
16− * rules:
17− * 1. It is not allowed to write a disk block which is
18− * currently referenced by the super block (either directly
19− * or indirectly).
20− * 2. When a super block is written, it is verified that all
21− * referenced (directly or indirectly) blocks fulfill the
22− * following requirements:
23− * 2a. All referenced blocks have either been present when
24− * the file system was mounted, (i.e., they have been
25− * referenced by the super block) or they have been
26− * written since then and the write completion callback
27− * was called and no write error was indicated and a
28− * FLUSH request to the device where these blocks are
29− * located was received and completed.
30− * 2b. All referenced blocks need to have a generation
31− * number which is equal to the parent's number.
32− *
33− * One issue that was found using this module was that the log
34− * tree on disk became temporarily corrupted because disk blocks
35− * that had been in use for the log tree had been freed and
36− * reused too early, while being referenced by the written super
37− * block.
38− *
39− * The search term in the kernel log that can be used to filter
40− * on the existence of detected integrity issues is
41− * "btrfs: attempt".
42− *
43− * The integrity check is enabled via mount options. These
44− * mount options are only supported if the integrity check
45− * tool is compiled by defining BTRFS_FS_CHECK_INTEGRITY.
46− *
47− * Example #1, apply integrity checks to all metadata:
48− * mount /dev/sdb1 /mnt -o check_int
49− *
50− * Example #2, apply integrity checks to all metadata and
51− * to data extents:
52− * mount /dev/sdb1 /mnt -o check_int_data
53− *
54− * Example #3, apply integrity checks to all metadata and dump
55− * the tree that the super block references to kernel messages
56− * each time after a super block was written:
57− * mount /dev/sdb1 /mnt -o check_int,check_int_print_mask=263
58− *
59− * If the integrity check tool is included and activated in
60− * the mount options, plenty of kernel memory is used, and
61− * plenty of additional CPU cycles are spent. Enabling this
62− * functionality is not intended for normal use. In most
63− * cases, unless you are a btrfs developer who needs to verify
64− * the integrity of (super)-block write requests, do not
65− * enable the config option BTRFS_FS_CHECK_INTEGRITY to
66− * include and compile the integrity check tool.
67− *
68− * Expect millions of lines of information in the kernel log with an
69− * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the
70− * kernel config to at least 26 (which is 64MB). Usually the value is
71− * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be
72− * changed like this before LOG_BUF_SHIFT can be set to a high value:
73− * config LOG_BUF_SHIFT
74− * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)"
75− * range 12 30
76− */
77−
78−#include <linux/sched.h>
79−#include <linux/slab.h>
80−#include <linux/mutex.h>
81−#include <linux/blkdev.h>
82−#include <linux/mm.h>
83−#include <linux/string.h>
84−#include <crypto/hash.h>
85−#include "messages.h"
86−#include "ctree.h"
87−#include "disk-io.h"
88−#include "transaction.h"
89−#include "extent_io.h"
90−#include "volumes.h"
91−#include "print-tree.h"
92−#include "locking.h"
93−#include "check-integrity.h"
94−#include "rcu-string.h"
95−#include "compression.h"
96−#include "accessors.h"
97−
98−#define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
99−#define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
100−#define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
101−#define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
102−#define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
103−#define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
104−#define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
105−#define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
106− * excluding " [...]" */
107−#define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
108−
109−/*
110− * The definition of the bitmask fields for the print_mask.
111− * They are specified with the mount option check_integrity_print_mask.
112− */
113−#define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
114−#define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
115−#define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
116−#define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
117−#define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
118−#define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
119−#define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
120−#define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
121−#define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
122−#define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
123−#define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
124−#define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
125−#define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
126−#define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
127−
128−struct btrfsic_dev_state;
129−struct btrfsic_state;
130−
131−struct btrfsic_block {
132− u32 magic_num; /* only used for debug purposes */
133− unsigned int is_metadata:1; /* if it is meta-data, not data-data */
134− unsigned int is_superblock:1; /* if it is one of the superblocks */
135− unsigned int is_iodone:1; /* if is done by lower subsystem */
136− unsigned int iodone_w_error:1; /* error was indicated to endio */
137− unsigned int never_written:1; /* block was added because it was
138− * referenced, not because it was
139− * written */
140− unsigned int mirror_num; /* large enough to hold
141− * BTRFS_SUPER_MIRROR_MAX */
142− struct btrfsic_dev_state *dev_state;
143− u64 dev_bytenr; /* key, physical byte num on disk */
144− u64 logical_bytenr; /* logical byte num on disk */
145− u64 generation;
146− struct btrfs_disk_key disk_key; /* extra info to print in case of
147− * issues, will not always be correct */
148− struct list_head collision_resolving_node; /* list node */
149− struct list_head all_blocks_node; /* list node */
150−
151− /* the following two lists contain block_link items */
152− struct list_head ref_to_list; /* list */
153− struct list_head ref_from_list; /* list */
154− struct btrfsic_block *next_in_same_bio;
155− void *orig_bio_private;
156− bio_end_io_t *orig_bio_end_io;
157− blk_opf_t submit_bio_bh_rw;
158− u64 flush_gen; /* only valid if !never_written */
159−};
160−
161−/*
162− * Elements of this type are allocated dynamically and required because
163− * each block object can refer to and can be ref from multiple blocks.
164− * The key to lookup them in the hashtable is the dev_bytenr of
165− * the block ref to plus the one from the block referred from.
166− * The fact that they are searchable via a hashtable and that a
167− * ref_cnt is maintained is not required for the btrfs integrity
168− * check algorithm itself, it is only used to make the output more
169− * beautiful in case that an error is detected (an error is defined
170− * as a write operation to a block while that block is still referenced).
171− */
172−struct btrfsic_block_link {
173− u32 magic_num; /* only used for debug purposes */
174− u32 ref_cnt;
175− struct list_head node_ref_to; /* list node */
176− struct list_head node_ref_from; /* list node */
177− struct list_head collision_resolving_node; /* list node */
178− struct btrfsic_block *block_ref_to;
179− struct btrfsic_block *block_ref_from;
180− u64 parent_generation;
181−};
182−
183−struct btrfsic_dev_state {
184− u32 magic_num; /* only used for debug purposes */
185− struct block_device *bdev;
186− struct btrfsic_state *state;
187− struct list_head collision_resolving_node; /* list node */
188− struct btrfsic_block dummy_block_for_bio_bh_flush;
189− u64 last_flush_gen;
190−};
191−
192−struct btrfsic_block_hashtable {
193− struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
194−};
195−
196−struct btrfsic_block_link_hashtable {
197− struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
198−};
199−
200−struct btrfsic_dev_state_hashtable {
201− struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
202−};
203−
204−struct btrfsic_block_data_ctx {
205− u64 start; /* virtual bytenr */
206− u64 dev_bytenr; /* physical bytenr on device */
207− u32 len;
208− struct btrfsic_dev_state *dev;
209− char **datav;
210− struct page **pagev;
211− void *mem_to_free;
212−};
213−
214−/* This structure is used to implement recursion without occupying
215− * any stack space, refer to btrfsic_process_metablock() */
216−struct btrfsic_stack_frame {
217− u32 magic;
218− u32 nr;
219− int error;
220− int i;
221− int limit_nesting;
222− int num_copies;
223− int mirror_num;
224− struct btrfsic_block *block;
225− struct btrfsic_block_data_ctx *block_ctx;
226− struct btrfsic_block *next_block;
227− struct btrfsic_block_data_ctx next_block_ctx;
228− struct btrfs_header *hdr;
229− struct btrfsic_stack_frame *prev;
230−};
231−
232−/* Some state per mounted filesystem */
233−struct btrfsic_state {
234− u32 print_mask;
235− int include_extent_data;
236− struct list_head all_blocks_list;
237− struct btrfsic_block_hashtable block_hashtable;
238− struct btrfsic_block_link_hashtable block_link_hashtable;
239− struct btrfs_fs_info *fs_info;
240− u64 max_superblock_generation;
241− struct btrfsic_block *latest_superblock;
242− u32 metablock_size;
243− u32 datablock_size;
244−};
245−
246−static int btrfsic_process_metablock(struct btrfsic_state *state,
247− struct btrfsic_block *block,
248− struct btrfsic_block_data_ctx *block_ctx,
249− int limit_nesting, int force_iodone_flag);
250−static void btrfsic_read_from_block_data(
251− struct btrfsic_block_data_ctx *block_ctx,
252− void *dst, u32 offset, size_t len);
253−static int btrfsic_create_link_to_next_block(
254− struct btrfsic_state *state,
255− struct btrfsic_block *block,
256− struct btrfsic_block_data_ctx
257− *block_ctx, u64 next_bytenr,
258− int limit_nesting,
259− struct btrfsic_block_data_ctx *next_block_ctx,
260− struct btrfsic_block **next_blockp,
261− int force_iodone_flag,
262− int *num_copiesp, int *mirror_nump,
263− struct btrfs_disk_key *disk_key,
264− u64 parent_generation);
265−static int btrfsic_handle_extent_data(struct btrfsic_state *state,
266− struct btrfsic_block *block,
267− struct btrfsic_block_data_ctx *block_ctx,
268− u32 item_offset, int force_iodone_flag);
269−static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
270− struct btrfsic_block_data_ctx *block_ctx_out,
271− int mirror_num);
272−static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
273−static int btrfsic_read_block(struct btrfsic_state *state,
274− struct btrfsic_block_data_ctx *block_ctx);
275−static int btrfsic_process_written_superblock(
276− struct btrfsic_state *state,
277− struct btrfsic_block *const block,
278− struct btrfs_super_block *const super_hdr);
279−static void btrfsic_bio_end_io(struct bio *bp);
280−static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
281− const struct btrfsic_block *block,
282− int recursion_level);
283−static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
284− struct btrfsic_block *const block,
285− int recursion_level);
286−static void btrfsic_print_add_link(const struct btrfsic_state *state,
287− const struct btrfsic_block_link *l);
288−static void btrfsic_print_rem_link(const struct btrfsic_state *state,
289− const struct btrfsic_block_link *l);
290−static char btrfsic_get_block_type(const struct btrfsic_state *state,
291− const struct btrfsic_block *block);
292−static void btrfsic_dump_tree(const struct btrfsic_state *state);
293−static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
294− const struct btrfsic_block *block,
295− int indent_level);
296−static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
297− struct btrfsic_state *state,
298− struct btrfsic_block_data_ctx *next_block_ctx,
299− struct btrfsic_block *next_block,
300− struct btrfsic_block *from_block,
301− u64 parent_generation);
302−static struct btrfsic_block *btrfsic_block_lookup_or_add(
303− struct btrfsic_state *state,
304− struct btrfsic_block_data_ctx *block_ctx,
305− const char *additional_string,
306− int is_metadata,
307− int is_iodone,
308− int never_written,
309− int mirror_num,
310− int *was_created);
311−static int btrfsic_process_superblock_dev_mirror(
312− struct btrfsic_state *state,
313− struct btrfsic_dev_state *dev_state,
314− struct btrfs_device *device,
315− int superblock_mirror_num,
316− struct btrfsic_dev_state **selected_dev_state,
317− struct btrfs_super_block *selected_super);
318−static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev);
319−static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
320− u64 bytenr,
321− struct btrfsic_dev_state *dev_state,
322− u64 dev_bytenr);
323−
324−static struct mutex btrfsic_mutex;
325−static int btrfsic_is_initialized;
326−static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
327−
328−
329−static void btrfsic_block_init(struct btrfsic_block *b)
330−{
331− b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
332− b->dev_state = NULL;
333− b->dev_bytenr = 0;
334− b->logical_bytenr = 0;
335− b->generation = BTRFSIC_GENERATION_UNKNOWN;
336− b->disk_key.objectid = 0;
337− b->disk_key.type = 0;
338− b->disk_key.offset = 0;
339− b->is_metadata = 0;
340− b->is_superblock = 0;
341− b->is_iodone = 0;
342− b->iodone_w_error = 0;
343− b->never_written = 0;
344− b->mirror_num = 0;
345− b->next_in_same_bio = NULL;
346− b->orig_bio_private = NULL;
347− b->orig_bio_end_io = NULL;
348− INIT_LIST_HEAD(&b->collision_resolving_node);
349− INIT_LIST_HEAD(&b->all_blocks_node);
350− INIT_LIST_HEAD(&b->ref_to_list);
351− INIT_LIST_HEAD(&b->ref_from_list);
352− b->submit_bio_bh_rw = 0;
353− b->flush_gen = 0;
354−}
355−
356−static struct btrfsic_block *btrfsic_block_alloc(void)
357−{
358− struct btrfsic_block *b;
359−
360− b = kzalloc(sizeof(*b), GFP_NOFS);
361− if (NULL != b)
362− btrfsic_block_init(b);
363−
364− return b;
365−}
366−
367−static void btrfsic_block_free(struct btrfsic_block *b)
368−{
369− BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
370− kfree(b);
371−}
372−
373−static void btrfsic_block_link_init(struct btrfsic_block_link *l)
374−{
375− l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
376− l->ref_cnt = 1;
377− INIT_LIST_HEAD(&l->node_ref_to);
378− INIT_LIST_HEAD(&l->node_ref_from);
379− INIT_LIST_HEAD(&l->collision_resolving_node);
380− l->block_ref_to = NULL;
381− l->block_ref_from = NULL;
382−}
383−
384−static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
385−{
386− struct btrfsic_block_link *l;
387−
388− l = kzalloc(sizeof(*l), GFP_NOFS);
389− if (NULL != l)
390− btrfsic_block_link_init(l);
391−
392− return l;
393−}
394−
395−static void btrfsic_block_link_free(struct btrfsic_block_link *l)
396−{
397− BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
398− kfree(l);
399−}
400−
401−static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
402−{
403− ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
404− ds->bdev = NULL;
405− ds->state = NULL;
406− INIT_LIST_HEAD(&ds->collision_resolving_node);
407− ds->last_flush_gen = 0;
408− btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
409− ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
410− ds->dummy_block_for_bio_bh_flush.dev_state = ds;
411−}
412−
413−static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
414−{
415− struct btrfsic_dev_state *ds;
416−
417− ds = kzalloc(sizeof(*ds), GFP_NOFS);
418− if (NULL != ds)
419− btrfsic_dev_state_init(ds);
420−
421− return ds;
422−}
423−
424−static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
425−{
426− BUG_ON(!(NULL == ds ||
427− BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
428− kfree(ds);
429−}
430−
431−static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
432−{
433− int i;
434−
435− for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
436− INIT_LIST_HEAD(h->table + i);
437−}
438−
439−static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
440− struct btrfsic_block_hashtable *h)
441−{
442− const unsigned int hashval =
443− (((unsigned int)(b->dev_bytenr >> 16)) ^
444− ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
445− (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
446−
447− list_add(&b->collision_resolving_node, h->table + hashval);
448−}
449−
450−static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
451−{
452− list_del(&b->collision_resolving_node);
453−}
454−
455−static struct btrfsic_block *btrfsic_block_hashtable_lookup(
456− struct block_device *bdev,
457− u64 dev_bytenr,
458− struct btrfsic_block_hashtable *h)
459−{
460− const unsigned int hashval =
461− (((unsigned int)(dev_bytenr >> 16)) ^
462− ((unsigned int)((uintptr_t)bdev))) &
463− (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
464− struct btrfsic_block *b;
465−
466− list_for_each_entry(b, h->table + hashval, collision_resolving_node) {
467− if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
468− return b;
469− }
470−
471− return NULL;
472−}
473−
474−static void btrfsic_block_link_hashtable_init(
475− struct btrfsic_block_link_hashtable *h)
476−{
477− int i;
478−
479− for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
480− INIT_LIST_HEAD(h->table + i);
481−}
482−
483−static void btrfsic_block_link_hashtable_add(
484− struct btrfsic_block_link *l,
485− struct btrfsic_block_link_hashtable *h)
486−{
487− const unsigned int hashval =
488− (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
489− ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
490− ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
491− ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
492− & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
493−
494− BUG_ON(NULL == l->block_ref_to);
495− BUG_ON(NULL == l->block_ref_from);
496− list_add(&l->collision_resolving_node, h->table + hashval);
497−}
498−
499−static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
500−{
501− list_del(&l->collision_resolving_node);
502−}
503−
504−static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
505− struct block_device *bdev_ref_to,
506− u64 dev_bytenr_ref_to,
507− struct block_device *bdev_ref_from,
508− u64 dev_bytenr_ref_from,
509− struct btrfsic_block_link_hashtable *h)
510−{
511− const unsigned int hashval =
512− (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
513− ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
514− ((unsigned int)((uintptr_t)bdev_ref_to)) ^
515− ((unsigned int)((uintptr_t)bdev_ref_from))) &
516− (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
517− struct btrfsic_block_link *l;
518−
519− list_for_each_entry(l, h->table + hashval, collision_resolving_node) {
520− BUG_ON(NULL == l->block_ref_to);
521− BUG_ON(NULL == l->block_ref_from);
522− if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
523− l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
524− l->block_ref_from->dev_state->bdev == bdev_ref_from &&
525− l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
526− return l;
527− }
528−
529− return NULL;
530−}
531−
532−static void btrfsic_dev_state_hashtable_init(
533− struct btrfsic_dev_state_hashtable *h)
534−{
535− int i;
536−
537− for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
538− INIT_LIST_HEAD(h->table + i);
539−}
540−
541−static void btrfsic_dev_state_hashtable_add(
542− struct btrfsic_dev_state *ds,
543− struct btrfsic_dev_state_hashtable *h)
544−{
545− const unsigned int hashval =
546− (((unsigned int)((uintptr_t)ds->bdev->bd_dev)) &
547− (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
548−
549− list_add(&ds->collision_resolving_node, h->table + hashval);
550−}
551−
552−static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
553−{
554− list_del(&ds->collision_resolving_node);
555−}
556−
557−static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
558− struct btrfsic_dev_state_hashtable *h)
559−{
560− const unsigned int hashval =
561− dev & (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1);
562− struct btrfsic_dev_state *ds;
563−
564− list_for_each_entry(ds, h->table + hashval, collision_resolving_node) {
565− if (ds->bdev->bd_dev == dev)
566− return ds;
567− }
568−
569− return NULL;
570−}
571−
572−static int btrfsic_process_superblock(struct btrfsic_state *state,
573− struct btrfs_fs_devices *fs_devices)
574−{
575− struct btrfs_super_block *selected_super;
576− struct list_head *dev_head = &fs_devices->devices;
577− struct btrfs_device *device;
578− struct btrfsic_dev_state *selected_dev_state = NULL;
579− int ret = 0;
580− int pass;
581−
582− selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
583− if (!selected_super)
584− return -ENOMEM;
585−
586− list_for_each_entry(device, dev_head, dev_list) {
587− int i;
588− struct btrfsic_dev_state *dev_state;
589−
590− if (!device->bdev || !device->name)
591− continue;
592−
593− dev_state = btrfsic_dev_state_lookup(device->bdev->bd_dev);
594− BUG_ON(NULL == dev_state);
595− for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
596− ret = btrfsic_process_superblock_dev_mirror(
597− state, dev_state, device, i,
598− &selected_dev_state, selected_super);
599− if (0 != ret && 0 == i) {
600− kfree(selected_super);
601− return ret;
602− }
603− }
604− }
605−
606− if (NULL == state->latest_superblock) {
607− pr_info("btrfsic: no superblock found!\n");
608− kfree(selected_super);
609− return -1;
610− }
611−
612− for (pass = 0; pass < 3; pass++) {
613− int num_copies;
614− int mirror_num;
615− u64 next_bytenr;
616−
617− switch (pass) {
618− case 0:
619− next_bytenr = btrfs_super_root(selected_super);
620− if (state->print_mask &
621− BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
622− pr_info("root@%llu\n", next_bytenr);
623− break;
624− case 1:
625− next_bytenr = btrfs_super_chunk_root(selected_super);
626− if (state->print_mask &
627− BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
628− pr_info("chunk@%llu\n", next_bytenr);
629− break;
630− case 2:
631− next_bytenr = btrfs_super_log_root(selected_super);
632− if (0 == next_bytenr)
633− continue;
634− if (state->print_mask &
635− BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
636− pr_info("log@%llu\n", next_bytenr);
637− break;
638− }
639−
640− num_copies = btrfs_num_copies(state->fs_info, next_bytenr,
641− state->metablock_size);
642− if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
643− pr_info("num_copies(log_bytenr=%llu) = %d\n",
644− next_bytenr, num_copies);
645−
646− for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
647− struct btrfsic_block *next_block;
648− struct btrfsic_block_data_ctx tmp_next_block_ctx;
649− struct btrfsic_block_link *l;
650−
651− ret = btrfsic_map_block(state, next_bytenr,
652− state->metablock_size,
653− &tmp_next_block_ctx,
654− mirror_num);
655− if (ret) {
656− pr_info("btrfsic: btrfsic_map_block(root @%llu, mirror %d) failed!\n",
657− next_bytenr, mirror_num);
658− kfree(selected_super);
659− return -1;
660− }
661−
662− next_block = btrfsic_block_hashtable_lookup(
663− tmp_next_block_ctx.dev->bdev,
664− tmp_next_block_ctx.dev_bytenr,
665− &state->block_hashtable);
666− BUG_ON(NULL == next_block);
667−
668− l = btrfsic_block_link_hashtable_lookup(
669− tmp_next_block_ctx.dev->bdev,
670− tmp_next_block_ctx.dev_bytenr,
671− state->latest_superblock->dev_state->
672− bdev,
673− state->latest_superblock->dev_bytenr,
674− &state->block_link_hashtable);
675− BUG_ON(NULL == l);
676−
677− ret = btrfsic_read_block(state, &tmp_next_block_ctx);
678− if (ret < (int)PAGE_SIZE) {
679− pr_info("btrfsic: read @logical %llu failed!\n",
680− tmp_next_block_ctx.start);
681− btrfsic_release_block_ctx(&tmp_next_block_ctx);
682− kfree(selected_super);
683− return -1;
684− }
685−
686− ret = btrfsic_process_metablock(state,
687− next_block,
688− &tmp_next_block_ctx,
689− BTRFS_MAX_LEVEL + 3, 1);
690− btrfsic_release_block_ctx(&tmp_next_block_ctx);
691− }
692− }
693−
694− kfree(selected_super);
695− return ret;
696−}
697−
698−static int btrfsic_process_superblock_dev_mirror(
699− struct btrfsic_state *state,
700− struct btrfsic_dev_state *dev_state,
701− struct btrfs_device *device,
702− int superblock_mirror_num,
703− struct btrfsic_dev_state **selected_dev_state,
704− struct btrfs_super_block *selected_super)
705−{
706− struct btrfs_fs_info *fs_info = state->fs_info;
707− struct btrfs_super_block *super_tmp;
708− u64 dev_bytenr;
709− struct btrfsic_block *superblock_tmp;
710− int pass;
711− struct block_device *const superblock_bdev = device->bdev;
712− struct page *page;
713− struct address_space *mapping = superblock_bdev->bd_inode->i_mapping;
714− int ret = 0;
715−
716− /* super block bytenr is always the unmapped device bytenr */
717− dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
718− if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
719− return -1;
720−
721− page = read_cache_page_gfp(mapping, dev_bytenr >> PAGE_SHIFT, GFP_NOFS);
722− if (IS_ERR(page))
723− return -1;
724−
725− super_tmp = page_address(page);
726−
727− if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
728− btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
729− memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
730− btrfs_super_nodesize(super_tmp) != state->metablock_size ||
731− btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
732− ret = 0;
733− goto out;
734− }
735−
736− superblock_tmp =
737− btrfsic_block_hashtable_lookup(superblock_bdev,
738− dev_bytenr,
739− &state->block_hashtable);
740− if (NULL == superblock_tmp) {
741− superblock_tmp = btrfsic_block_alloc();
742− if (NULL == superblock_tmp) {
743− ret = -1;
744− goto out;
745− }
746− /* for superblock, only the dev_bytenr makes sense */
747− superblock_tmp->dev_bytenr = dev_bytenr;
748− superblock_tmp->dev_state = dev_state;
749− superblock_tmp->logical_bytenr = dev_bytenr;
750− superblock_tmp->generation = btrfs_super_generation(super_tmp);
751− superblock_tmp->is_metadata = 1;
752− superblock_tmp->is_superblock = 1;
753− superblock_tmp->is_iodone = 1;
754− superblock_tmp->never_written = 0;
755− superblock_tmp->mirror_num = 1 + superblock_mirror_num;
756− if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
757− btrfs_info_in_rcu(fs_info,
758− "new initial S-block (bdev %p, %s) @%llu (%pg/%llu/%d)",
759− superblock_bdev,
760− btrfs_dev_name(device), dev_bytenr,
761− dev_state->bdev, dev_bytenr,
762− superblock_mirror_num);
763− list_add(&superblock_tmp->all_blocks_node,
764− &state->all_blocks_list);
765− btrfsic_block_hashtable_add(superblock_tmp,
766− &state->block_hashtable);
767− }
768−
769− /* select the one with the highest generation field */
770− if (btrfs_super_generation(super_tmp) >
771− state->max_superblock_generation ||
772− 0 == state->max_superblock_generation) {
773− memcpy(selected_super, super_tmp, sizeof(*selected_super));
774− *selected_dev_state = dev_state;
775− state->max_superblock_generation =
776− btrfs_super_generation(super_tmp);
777− state->latest_superblock = superblock_tmp;
778− }
779−
780− for (pass = 0; pass < 3; pass++) {
781− u64 next_bytenr;
782− int num_copies;
783− int mirror_num;
784− const char *additional_string = NULL;
785− struct btrfs_disk_key tmp_disk_key;
786−
787− tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
788− tmp_disk_key.offset = 0;
789− switch (pass) {
790− case 0:
791− btrfs_set_disk_key_objectid(&tmp_disk_key,
792− BTRFS_ROOT_TREE_OBJECTID);
793− additional_string = "initial root ";
794− next_bytenr = btrfs_super_root(super_tmp);
795− break;
796− case 1:
797− btrfs_set_disk_key_objectid(&tmp_disk_key,
798− BTRFS_CHUNK_TREE_OBJECTID);
799− additional_string = "initial chunk ";
800− next_bytenr = btrfs_super_chunk_root(super_tmp);
801− break;
802− case 2:
803− btrfs_set_disk_key_objectid(&tmp_disk_key,
804− BTRFS_TREE_LOG_OBJECTID);
805− additional_string = "initial log ";
806− next_bytenr = btrfs_super_log_root(super_tmp);
807− if (0 == next_bytenr)
808− continue;
809− break;
810− }
811−
812− num_copies = btrfs_num_copies(fs_info, next_bytenr,
813− state->metablock_size);
814− if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
815− pr_info("num_copies(log_bytenr=%llu) = %d\n",
816− next_bytenr, num_copies);
817− for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
818− struct btrfsic_block *next_block;
819− struct btrfsic_block_data_ctx tmp_next_block_ctx;
820− struct btrfsic_block_link *l;
821−
822− if (btrfsic_map_block(state, next_bytenr,
823− state->metablock_size,
824− &tmp_next_block_ctx,
825− mirror_num)) {
826− pr_info("btrfsic: btrfsic_map_block(bytenr @%llu, mirror %d) failed!\n",
827− next_bytenr, mirror_num);
828− ret = -1;
829− goto out;
830− }
831−
832− next_block = btrfsic_block_lookup_or_add(
833− state, &tmp_next_block_ctx,
834− additional_string, 1, 1, 0,
835− mirror_num, NULL);
836− if (NULL == next_block) {
837− btrfsic_release_block_ctx(&tmp_next_block_ctx);
838− ret = -1;
839− goto out;
840− }
841−
842− next_block->disk_key = tmp_disk_key;
843− next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
844− l = btrfsic_block_link_lookup_or_add(
845− state, &tmp_next_block_ctx,
846− next_block, superblock_tmp,
847− BTRFSIC_GENERATION_UNKNOWN);
848− btrfsic_release_block_ctx(&tmp_next_block_ctx);
849− if (NULL == l) {
850− ret = -1;
851− goto out;
852− }
853− }
854− }
855− if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
856− btrfsic_dump_tree_sub(state, superblock_tmp, 0);
857−
858−out:
859− put_page(page);
860− return ret;
861−}
862−
863−static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
864−{
865− struct btrfsic_stack_frame *sf;
866−
867− sf = kzalloc(sizeof(*sf), GFP_NOFS);
868− if (sf)
869− sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
870− return sf;
871−}
872−
873−static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
874−{
875− BUG_ON(!(NULL == sf ||
876− BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
877− kfree(sf);
878−}
879−
880−static noinline_for_stack int btrfsic_process_metablock(
881− struct btrfsic_state *state,
882− struct btrfsic_block *const first_block,
883− struct btrfsic_block_data_ctx *const first_block_ctx,
884− int first_limit_nesting, int force_iodone_flag)
885−{
886− struct btrfsic_stack_frame initial_stack_frame = { 0 };
887− struct btrfsic_stack_frame *sf;
888− struct btrfsic_stack_frame *next_stack;
889− struct btrfs_header *const first_hdr =
890− (struct btrfs_header *)first_block_ctx->datav[0];
891−
892− BUG_ON(!first_hdr);
893− sf = &initial_stack_frame;
894− sf->error = 0;
895− sf->i = -1;
896− sf->limit_nesting = first_limit_nesting;
897− sf->block = first_block;
898− sf->block_ctx = first_block_ctx;
899− sf->next_block = NULL;
900− sf->hdr = first_hdr;
901− sf->prev = NULL;
902−
903−continue_with_new_stack_frame:
904− sf->block->generation = btrfs_stack_header_generation(sf->hdr);
905− if (0 == sf->hdr->level) {
906− struct btrfs_leaf *const leafhdr =
907− (struct btrfs_leaf *)sf->hdr;
908−
909− if (-1 == sf->i) {
910− sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
911−
912− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
913− pr_info("leaf %llu items %d generation %llu owner %llu\n",
914− sf->block_ctx->start, sf->nr,
915− btrfs_stack_header_generation(
916− &leafhdr->header),
917− btrfs_stack_header_owner(
918− &leafhdr->header));
919− }
920−
921−continue_with_current_leaf_stack_frame:
922− if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
923− sf->i++;
924− sf->num_copies = 0;
925− }
926−
927− if (sf->i < sf->nr) {
928− struct btrfs_item disk_item;
929− u32 disk_item_offset =
930− (uintptr_t)(leafhdr->items + sf->i) -
931− (uintptr_t)leafhdr;
932− struct btrfs_disk_key *disk_key;
933− u8 type;
934− u32 item_offset;
935− u32 item_size;
936−
937− if (disk_item_offset + sizeof(struct btrfs_item) >
938− sf->block_ctx->len) {
939−leaf_item_out_of_bounce_error:
940− pr_info(
941− "btrfsic: leaf item out of bounce at logical %llu, dev %pg\n",
942− sf->block_ctx->start,
943− sf->block_ctx->dev->bdev);
944− goto one_stack_frame_backwards;
945− }
946− btrfsic_read_from_block_data(sf->block_ctx,
947− &disk_item,
948− disk_item_offset,
949− sizeof(struct btrfs_item));
950− item_offset = btrfs_stack_item_offset(&disk_item);
951− item_size = btrfs_stack_item_size(&disk_item);
952− disk_key = &disk_item.key;
953− type = btrfs_disk_key_type(disk_key);
954−
955− if (BTRFS_ROOT_ITEM_KEY == type) {
956− struct btrfs_root_item root_item;
957− u32 root_item_offset;
958− u64 next_bytenr;
959−
960− root_item_offset = item_offset +
961− offsetof(struct btrfs_leaf, items);
962− if (root_item_offset + item_size >
963− sf->block_ctx->len)
964− goto leaf_item_out_of_bounce_error;
965− btrfsic_read_from_block_data(
966− sf->block_ctx, &root_item,
967− root_item_offset,
968− item_size);
969− next_bytenr = btrfs_root_bytenr(&root_item);
970−
971− sf->error =
972− btrfsic_create_link_to_next_block(
973− state,
974− sf->block,
975− sf->block_ctx,
976− next_bytenr,
977− sf->limit_nesting,
978− &sf->next_block_ctx,
979− &sf->next_block,
980− force_iodone_flag,
981− &sf->num_copies,
982− &sf->mirror_num,
983− disk_key,
984− btrfs_root_generation(
985− &root_item));
986− if (sf->error)
987− goto one_stack_frame_backwards;
988−
989− if (NULL != sf->next_block) {
990− struct btrfs_header *const next_hdr =
991− (struct btrfs_header *)
992− sf->next_block_ctx.datav[0];
993−
994− next_stack =
995− btrfsic_stack_frame_alloc();
996− if (NULL == next_stack) {
997− sf->error = -1;
998− btrfsic_release_block_ctx(
999− &sf->
1000− next_block_ctx);
1001− goto one_stack_frame_backwards;
1002− }
1003−
1004− next_stack->i = -1;
1005− next_stack->block = sf->next_block;
1006− next_stack->block_ctx =
1007− &sf->next_block_ctx;
1008− next_stack->next_block = NULL;
1009− next_stack->hdr = next_hdr;
1010− next_stack->limit_nesting =
1011− sf->limit_nesting - 1;
1012− next_stack->prev = sf;
1013− sf = next_stack;
1014− goto continue_with_new_stack_frame;
1015− }
1016− } else if (BTRFS_EXTENT_DATA_KEY == type &&
1017− state->include_extent_data) {
1018− sf->error = btrfsic_handle_extent_data(
1019− state,
1020− sf->block,
1021− sf->block_ctx,
1022− item_offset,
1023− force_iodone_flag);
1024− if (sf->error)
1025− goto one_stack_frame_backwards;
1026− }
1027−
1028− goto continue_with_current_leaf_stack_frame;
1029− }
1030− } else {
1031− struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1032−
1033− if (-1 == sf->i) {
1034− sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
1035−
1036− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1037− pr_info("node %llu level %d items %d generation %llu owner %llu\n",
1038− sf->block_ctx->start,
1039− nodehdr->header.level, sf->nr,
1040− btrfs_stack_header_generation(
1041− &nodehdr->header),
1042− btrfs_stack_header_owner(
1043− &nodehdr->header));
1044− }
1045−
1046−continue_with_current_node_stack_frame:
1047− if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1048− sf->i++;
1049− sf->num_copies = 0;
1050− }
1051−
1052− if (sf->i < sf->nr) {
1053− struct btrfs_key_ptr key_ptr;
1054− u32 key_ptr_offset;
1055− u64 next_bytenr;
1056−
1057− key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1058− (uintptr_t)nodehdr;
1059− if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1060− sf->block_ctx->len) {
1061− pr_info(
1062− "btrfsic: node item out of bounce at logical %llu, dev %pg\n",
1063− sf->block_ctx->start,
1064− sf->block_ctx->dev->bdev);
1065− goto one_stack_frame_backwards;
1066− }
1067− btrfsic_read_from_block_data(
1068− sf->block_ctx, &key_ptr, key_ptr_offset,
1069− sizeof(struct btrfs_key_ptr));
1070− next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
1071−
1072− sf->error = btrfsic_create_link_to_next_block(
1073− state,
1074− sf->block,
1075− sf->block_ctx,
1076− next_bytenr,
1077− sf->limit_nesting,
1078− &sf->next_block_ctx,
1079− &sf->next_block,
1080− force_iodone_flag,
1081− &sf->num_copies,
1082− &sf->mirror_num,
1083− &key_ptr.key,
1084− btrfs_stack_key_generation(&key_ptr));
1085− if (sf->error)
1086− goto one_stack_frame_backwards;
1087−
1088− if (NULL != sf->next_block) {
1089− struct btrfs_header *const next_hdr =
1090− (struct btrfs_header *)
1091− sf->next_block_ctx.datav[0];
1092−
1093− next_stack = btrfsic_stack_frame_alloc();
1094− if (NULL == next_stack) {
1095− sf->error = -1;
1096− goto one_stack_frame_backwards;
1097− }
1098−
1099− next_stack->i = -1;
1100− next_stack->block = sf->next_block;
1101− next_stack->block_ctx = &sf->next_block_ctx;
1102− next_stack->next_block = NULL;
1103− next_stack->hdr = next_hdr;
1104− next_stack->limit_nesting =
1105− sf->limit_nesting - 1;
1106− next_stack->prev = sf;
1107− sf = next_stack;
1108− goto continue_with_new_stack_frame;
1109− }
1110−
1111− goto continue_with_current_node_stack_frame;
1112− }
1113− }
1114−
1115−one_stack_frame_backwards:
1116− if (NULL != sf->prev) {
1117− struct btrfsic_stack_frame *const prev = sf->prev;
1118−
1119− /* the one for the initial block is freed in the caller */
1120− btrfsic_release_block_ctx(sf->block_ctx);
1121−
1122− if (sf->error) {
1123− prev->error = sf->error;
1124− btrfsic_stack_frame_free(sf);
1125− sf = prev;
1126− goto one_stack_frame_backwards;
1127− }
1128−
1129− btrfsic_stack_frame_free(sf);
1130− sf = prev;
1131− goto continue_with_new_stack_frame;
1132− } else {
1133− BUG_ON(&initial_stack_frame != sf);
1134− }
1135−
1136− return sf->error;
1137−}
1138−
1139−static void btrfsic_read_from_block_data(
1140− struct btrfsic_block_data_ctx *block_ctx,
1141− void *dstv, u32 offset, size_t len)
1142−{
1143− size_t cur;
1144− size_t pgoff;
1145− char *kaddr;
1146− char *dst = (char *)dstv;
1147− size_t start_offset = offset_in_page(block_ctx->start);
1148− unsigned long i = (start_offset + offset) >> PAGE_SHIFT;
1149−
1150− WARN_ON(offset + len > block_ctx->len);
1151− pgoff = offset_in_page(start_offset + offset);
1152−
1153− while (len > 0) {
1154− cur = min(len, ((size_t)PAGE_SIZE - pgoff));
1155− BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_SIZE));
1156− kaddr = block_ctx->datav[i];
1157− memcpy(dst, kaddr + pgoff, cur);
1158−
1159− dst += cur;
1160− len -= cur;
1161− pgoff = 0;
1162− i++;
1163− }
1164−}
1165−
1166−static int btrfsic_create_link_to_next_block(
1167− struct btrfsic_state *state,
1168− struct btrfsic_block *block,
1169− struct btrfsic_block_data_ctx *block_ctx,
1170− u64 next_bytenr,
1171− int limit_nesting,
1172− struct btrfsic_block_data_ctx *next_block_ctx,
1173− struct btrfsic_block **next_blockp,
1174− int force_iodone_flag,
1175− int *num_copiesp, int *mirror_nump,
1176− struct btrfs_disk_key *disk_key,
1177− u64 parent_generation)
1178−{
1179− struct btrfs_fs_info *fs_info = state->fs_info;
1180− struct btrfsic_block *next_block = NULL;
1181− int ret;
1182− struct btrfsic_block_link *l;
1183− int did_alloc_block_link;
1184− int block_was_created;
1185−
1186− *next_blockp = NULL;
1187− if (0 == *num_copiesp) {
1188− *num_copiesp = btrfs_num_copies(fs_info, next_bytenr,
1189− state->metablock_size);
1190− if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1191− pr_info("num_copies(log_bytenr=%llu) = %d\n",
1192− next_bytenr, *num_copiesp);
1193− *mirror_nump = 1;
1194− }
1195−
1196− if (*mirror_nump > *num_copiesp)
1197− return 0;
1198−
1199− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1200− pr_info("btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1201− *mirror_nump);
1202− ret = btrfsic_map_block(state, next_bytenr,
1203− state->metablock_size,
1204− next_block_ctx, *mirror_nump);
1205− if (ret) {
1206− pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1207− next_bytenr, *mirror_nump);
1208− btrfsic_release_block_ctx(next_block_ctx);
1209− *next_blockp = NULL;
1210− return -1;
1211− }
1212−
1213− next_block = btrfsic_block_lookup_or_add(state,
1214− next_block_ctx, "referenced ",
1215− 1, force_iodone_flag,
1216− !force_iodone_flag,
1217− *mirror_nump,
1218− &block_was_created);
1219− if (NULL == next_block) {
1220− btrfsic_release_block_ctx(next_block_ctx);
1221− *next_blockp = NULL;
1222− return -1;
1223− }
1224− if (block_was_created) {
1225− l = NULL;
1226− next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1227− } else {
1228− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1229− if (next_block->logical_bytenr != next_bytenr &&
1230− !(!next_block->is_metadata &&
1231− 0 == next_block->logical_bytenr))
1232− pr_info(
1233−"referenced block @%llu (%pg/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu)\n",
1234− next_bytenr, next_block_ctx->dev->bdev,
1235− next_block_ctx->dev_bytenr, *mirror_nump,
1236− btrfsic_get_block_type(state,
1237− next_block),
1238− next_block->logical_bytenr);
1239− else
1240− pr_info(
1241− "referenced block @%llu (%pg/%llu/%d) found in hash table, %c\n",
1242− next_bytenr, next_block_ctx->dev->bdev,
1243− next_block_ctx->dev_bytenr, *mirror_nump,
1244− btrfsic_get_block_type(state,
1245− next_block));
1246− }
1247− next_block->logical_bytenr = next_bytenr;
1248−
1249− next_block->mirror_num = *mirror_nump;
1250− l = btrfsic_block_link_hashtable_lookup(
1251− next_block_ctx->dev->bdev,
1252− next_block_ctx->dev_bytenr,
1253− block_ctx->dev->bdev,
1254− block_ctx->dev_bytenr,
1255− &state->block_link_hashtable);
1256− }
1257−
1258− next_block->disk_key = *disk_key;
1259− if (NULL == l) {
1260− l = btrfsic_block_link_alloc();
1261− if (NULL == l) {
1262− btrfsic_release_block_ctx(next_block_ctx);
1263− *next_blockp = NULL;
1264− return -1;
1265− }
1266−
1267− did_alloc_block_link = 1;
1268− l->block_ref_to = next_block;
1269− l->block_ref_from = block;
1270− l->ref_cnt = 1;
1271− l->parent_generation = parent_generation;
1272−
1273− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1274− btrfsic_print_add_link(state, l);
1275−
1276− list_add(&l->node_ref_to, &block->ref_to_list);
1277− list_add(&l->node_ref_from, &next_block->ref_from_list);
1278−
1279− btrfsic_block_link_hashtable_add(l,
1280− &state->block_link_hashtable);
1281− } else {
1282− did_alloc_block_link = 0;
1283− if (0 == limit_nesting) {
1284− l->ref_cnt++;
1285− l->parent_generation = parent_generation;
1286− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1287− btrfsic_print_add_link(state, l);
1288− }
1289− }
1290−
1291− if (limit_nesting > 0 && did_alloc_block_link) {
1292− ret = btrfsic_read_block(state, next_block_ctx);
1293− if (ret < (int)next_block_ctx->len) {
1294− pr_info("btrfsic: read block @logical %llu failed!\n",
1295− next_bytenr);
1296− btrfsic_release_block_ctx(next_block_ctx);
1297− *next_blockp = NULL;
1298− return -1;
1299− }
1300−
1301− *next_blockp = next_block;
1302− } else {
1303− *next_blockp = NULL;
1304− }
1305− (*mirror_nump)++;
1306−
1307− return 0;
1308−}
1309−
1310−static int btrfsic_handle_extent_data(
1311− struct btrfsic_state *state,
1312− struct btrfsic_block *block,
1313− struct btrfsic_block_data_ctx *block_ctx,
1314− u32 item_offset, int force_iodone_flag)
1315−{
1316− struct btrfs_fs_info *fs_info = state->fs_info;
1317− struct btrfs_file_extent_item file_extent_item;
1318− u64 file_extent_item_offset;
1319− u64 next_bytenr;
1320− u64 num_bytes;
1321− u64 generation;
1322− struct btrfsic_block_link *l;
1323− int ret;
1324−
1325− file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1326− item_offset;
1327− if (file_extent_item_offset +
1328− offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1329− block_ctx->len) {
1330− pr_info("btrfsic: file item out of bounce at logical %llu, dev %pg\n",
1331− block_ctx->start, block_ctx->dev->bdev);
1332− return -1;
1333− }
1334−
1335− btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1336− file_extent_item_offset,
1337− offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1338− if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
1339− btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
1340− if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1341− pr_info("extent_data: type %u, disk_bytenr = %llu\n",
1342− file_extent_item.type,
1343− btrfs_stack_file_extent_disk_bytenr(
1344− &file_extent_item));
1345− return 0;
1346− }
1347−
1348− if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1349− block_ctx->len) {
1350− pr_info("btrfsic: file item out of bounce at logical %llu, dev %pg\n",
1351− block_ctx->start, block_ctx->dev->bdev);
1352− return -1;
1353− }
1354− btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1355− file_extent_item_offset,
1356− sizeof(struct btrfs_file_extent_item));
1357− next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
1358− if (btrfs_stack_file_extent_compression(&file_extent_item) ==
1359− BTRFS_COMPRESS_NONE) {
1360− next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
1361− num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1362− } else {
1363− num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
1364− }
1365− generation = btrfs_stack_file_extent_generation(&file_extent_item);
1366−
1367− if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1368− pr_info("extent_data: type %u, disk_bytenr = %llu, offset = %llu, num_bytes = %llu\n",
1369− file_extent_item.type,
1370− btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
1371− btrfs_stack_file_extent_offset(&file_extent_item),
1372− num_bytes);
1373− while (num_bytes > 0) {
1374− u32 chunk_len;
1375− int num_copies;
1376− int mirror_num;
1377−
1378− if (num_bytes > state->datablock_size)
1379− chunk_len = state->datablock_size;
1380− else
1381− chunk_len = num_bytes;
1382−
1383− num_copies = btrfs_num_copies(fs_info, next_bytenr,
1384− state->datablock_size);
1385− if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1386− pr_info("num_copies(log_bytenr=%llu) = %d\n",
1387− next_bytenr, num_copies);
1388− for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1389− struct btrfsic_block_data_ctx next_block_ctx;
1390− struct btrfsic_block *next_block;
1391− int block_was_created;
1392−
1393− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1394− pr_info("btrfsic_handle_extent_data(mirror_num=%d)\n",
1395− mirror_num);
1396− if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1397− pr_info("\tdisk_bytenr = %llu, num_bytes %u\n",
1398− next_bytenr, chunk_len);
1399− ret = btrfsic_map_block(state, next_bytenr,
1400− chunk_len, &next_block_ctx,
1401− mirror_num);
1402− if (ret) {
1403− pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1404− next_bytenr, mirror_num);
1405− return -1;
1406− }
1407−
1408− next_block = btrfsic_block_lookup_or_add(
1409− state,
1410− &next_block_ctx,
1411− "referenced ",
1412− 0,
1413− force_iodone_flag,
1414− !force_iodone_flag,
1415− mirror_num,
1416− &block_was_created);
1417− if (NULL == next_block) {
1418− btrfsic_release_block_ctx(&next_block_ctx);
1419− return -1;
1420− }
1421− if (!block_was_created) {
1422− if ((state->print_mask &
1423− BTRFSIC_PRINT_MASK_VERBOSE) &&
1424− next_block->logical_bytenr != next_bytenr &&
1425− !(!next_block->is_metadata &&
1426− 0 == next_block->logical_bytenr)) {
1427− pr_info(
1428−"referenced block @%llu (%pg/%llu/%d) found in hash table, D, bytenr mismatch (!= stored %llu)\n",
1429− next_bytenr,
1430− next_block_ctx.dev->bdev,
1431− next_block_ctx.dev_bytenr,
1432− mirror_num,
1433− next_block->logical_bytenr);
1434− }
1435− next_block->logical_bytenr = next_bytenr;
1436− next_block->mirror_num = mirror_num;
1437− }
1438−
1439− l = btrfsic_block_link_lookup_or_add(state,
1440− &next_block_ctx,
1441− next_block, block,
1442− generation);
1443− btrfsic_release_block_ctx(&next_block_ctx);
1444− if (NULL == l)
1445− return -1;
1446− }
1447−
1448− next_bytenr += chunk_len;
1449− num_bytes -= chunk_len;
1450− }
1451−
1452− return 0;
1453−}
1454−
1455−static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1456− struct btrfsic_block_data_ctx *block_ctx_out,
1457− int mirror_num)
1458−{
1459− struct btrfs_fs_info *fs_info = state->fs_info;
1460− int ret;
1461− u64 length;
1462− struct btrfs_io_context *bioc = NULL;
1463− struct btrfs_io_stripe smap, *map;
1464− struct btrfs_device *device;
1465−
1466− length = len;
1467− ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, bytenr, &length, &bioc,
1468− NULL, &mirror_num, 0);
1469− if (ret) {
1470− block_ctx_out->start = 0;
1471− block_ctx_out->dev_bytenr = 0;
1472− block_ctx_out->len = 0;
1473− block_ctx_out->dev = NULL;
1474− block_ctx_out->datav = NULL;
1475− block_ctx_out->pagev = NULL;
1476− block_ctx_out->mem_to_free = NULL;
1477−
1478− return ret;
1479− }
1480−
1481− if (bioc)
1482− map = &bioc->stripes[0];
1483− else
1484− map = &smap;
1485−
1486− device = map->dev;
1487− if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
1488− !device->bdev || !device->name)
1489− block_ctx_out->dev = NULL;
1490− else
1491− block_ctx_out->dev = btrfsic_dev_state_lookup(
1492− device->bdev->bd_dev);
1493− block_ctx_out->dev_bytenr = map->physical;
1494− block_ctx_out->start = bytenr;
1495− block_ctx_out->len = len;
1496− block_ctx_out->datav = NULL;
1497− block_ctx_out->pagev = NULL;
1498− block_ctx_out->mem_to_free = NULL;
1499−
1500− kfree(bioc);
1501− if (NULL == block_ctx_out->dev) {
1502− ret = -ENXIO;
1503− pr_info("btrfsic: error, cannot lookup dev (#1)!\n");
1504− }
1505−
1506− return ret;
1507−}
1508−
1509−static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1510−{
1511− if (block_ctx->mem_to_free) {
1512− unsigned int num_pages;
1513−
1514− BUG_ON(!block_ctx->datav);
1515− BUG_ON(!block_ctx->pagev);
1516− num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
1517− PAGE_SHIFT;
1518− /* Pages must be unmapped in reverse order */
1519− while (num_pages > 0) {
1520− num_pages--;
1521− if (block_ctx->datav[num_pages])
1522− block_ctx->datav[num_pages] = NULL;
1523− if (block_ctx->pagev[num_pages]) {
1524− __free_page(block_ctx->pagev[num_pages]);
1525− block_ctx->pagev[num_pages] = NULL;
1526− }
1527− }
1528−
1529− kfree(block_ctx->mem_to_free);
1530− block_ctx->mem_to_free = NULL;
1531− block_ctx->pagev = NULL;
1532− block_ctx->datav = NULL;
1533− }
1534−}
1535−
1536−static int btrfsic_read_block(struct btrfsic_state *state,
1537− struct btrfsic_block_data_ctx *block_ctx)
1538−{
1539− unsigned int num_pages;
1540− unsigned int i;
1541− size_t size;
1542− u64 dev_bytenr;
1543− int ret;
1544−
1545− BUG_ON(block_ctx->datav);
1546− BUG_ON(block_ctx->pagev);
1547− BUG_ON(block_ctx->mem_to_free);
1548− if (!PAGE_ALIGNED(block_ctx->dev_bytenr)) {
1549− pr_info("btrfsic: read_block() with unaligned bytenr %llu\n",
1550− block_ctx->dev_bytenr);
1551− return -1;
1552− }
1553−
1554− num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
1555− PAGE_SHIFT;
1556− size = sizeof(*block_ctx->datav) + sizeof(*block_ctx->pagev);
1557− block_ctx->mem_to_free = kcalloc(num_pages, size, GFP_NOFS);
1558− if (!block_ctx->mem_to_free)
1559− return -ENOMEM;
1560− block_ctx->datav = block_ctx->mem_to_free;
1561− block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1562− ret = btrfs_alloc_page_array(num_pages, block_ctx->pagev);
1563− if (ret)
1564− return ret;
1565−
1566− dev_bytenr = block_ctx->dev_bytenr;
1567− for (i = 0; i < num_pages;) {
1568− struct bio *bio;
1569− unsigned int j;
1570−
1571− bio = bio_alloc(block_ctx->dev->bdev, num_pages - i,
1572− REQ_OP_READ, GFP_NOFS);
1573− bio->bi_iter.bi_sector = dev_bytenr >> SECTOR_SHIFT;
1574−
1575− for (j = i; j < num_pages; j++) {
1576− ret = bio_add_page(bio, block_ctx->pagev[j],
1577− PAGE_SIZE, 0);
1578− if (PAGE_SIZE != ret)
1579− break;
1580− }
1581− if (j == i) {
1582− pr_info("btrfsic: error, failed to add a single page!\n");
1583− return -1;
1584− }
1585− if (submit_bio_wait(bio)) {
1586− pr_info("btrfsic: read error at logical %llu dev %pg!\n",
1587− block_ctx->start, block_ctx->dev->bdev);
1588− bio_put(bio);
1589− return -1;
1590− }
1591− bio_put(bio);
1592− dev_bytenr += (j - i) * PAGE_SIZE;
1593− i = j;
1594− }
1595− for (i = 0; i < num_pages; i++)
1596− block_ctx->datav[i] = page_address(block_ctx->pagev[i]);
1597−
1598− return block_ctx->len;
1599−}
1600−
1601−static void btrfsic_dump_database(struct btrfsic_state *state)
1602−{
1603− const struct btrfsic_block *b_all;
1604−
1605− BUG_ON(NULL == state);
1606−
1607− pr_info("all_blocks_list:\n");
1608− list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) {
1609− const struct btrfsic_block_link *l;
1610−
1611− pr_info("%c-block @%llu (%pg/%llu/%d)\n",
1612− btrfsic_get_block_type(state, b_all),
1613− b_all->logical_bytenr, b_all->dev_state->bdev,
1614− b_all->dev_bytenr, b_all->mirror_num);
1615−
1616− list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) {
1617− pr_info(
1618− " %c @%llu (%pg/%llu/%d) refers %u* to %c @%llu (%pg/%llu/%d)\n",
1619− btrfsic_get_block_type(state, b_all),
1620− b_all->logical_bytenr, b_all->dev_state->bdev,
1621− b_all->dev_bytenr, b_all->mirror_num,
1622− l->ref_cnt,
1623− btrfsic_get_block_type(state, l->block_ref_to),
1624− l->block_ref_to->logical_bytenr,
1625− l->block_ref_to->dev_state->bdev,
1626− l->block_ref_to->dev_bytenr,
1627− l->block_ref_to->mirror_num);
1628− }
1629−
1630− list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) {
1631− pr_info(
1632− " %c @%llu (%pg/%llu/%d) is ref %u* from %c @%llu (%pg/%llu/%d)\n",
1633− btrfsic_get_block_type(state, b_all),
1634− b_all->logical_bytenr, b_all->dev_state->bdev,
1635− b_all->dev_bytenr, b_all->mirror_num,
1636− l->ref_cnt,
1637− btrfsic_get_block_type(state, l->block_ref_from),
1638− l->block_ref_from->logical_bytenr,
1639− l->block_ref_from->dev_state->bdev,
1640− l->block_ref_from->dev_bytenr,
1641− l->block_ref_from->mirror_num);
1642− }
1643−
1644− pr_info("\n");
1645− }
1646−}
1647−
1648−/*
1649− * Test whether the disk block contains a tree block (leaf or node)
1650− * (note that this test fails for the super block)
1651− */
1652−static noinline_for_stack int btrfsic_test_for_metadata(
1653− struct btrfsic_state *state,
1654− char **datav, unsigned int num_pages)
1655−{
1656− struct btrfs_fs_info *fs_info = state->fs_info;
1657− SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
1658− struct btrfs_header *h;
1659− u8 csum[BTRFS_CSUM_SIZE];
1660− unsigned int i;
1661−
1662− if (num_pages * PAGE_SIZE < state->metablock_size)
1663− return 1; /* not metadata */
1664− num_pages = state->metablock_size >> PAGE_SHIFT;
1665− h = (struct btrfs_header *)datav[0];
1666−
1667− if (memcmp(h->fsid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE))
1668− return 1;
1669−
1670− shash->tfm = fs_info->csum_shash;
1671− crypto_shash_init(shash);
1672−
1673− for (i = 0; i < num_pages; i++) {
1674− u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1675− size_t sublen = i ? PAGE_SIZE :
1676− (PAGE_SIZE - BTRFS_CSUM_SIZE);
1677−
1678− crypto_shash_update(shash, data, sublen);
1679− }
1680− crypto_shash_final(shash, csum);
1681− if (memcmp(csum, h->csum, fs_info->csum_size))
1682− return 1;
1683−
1684− return 0; /* is metadata */
1685−}
1686−
1687−static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
1688− u64 dev_bytenr, char **mapped_datav,
1689− unsigned int num_pages,
1690− struct bio *bio, int *bio_is_patched,
1691− blk_opf_t submit_bio_bh_rw)
1692−{
1693− int is_metadata;
1694− struct btrfsic_block *block;
1695− struct btrfsic_block_data_ctx block_ctx;
1696− int ret;
1697− struct btrfsic_state *state = dev_state->state;
1698− struct block_device *bdev = dev_state->bdev;
1699− unsigned int processed_len;
1700−
1701− if (NULL != bio_is_patched)
1702− *bio_is_patched = 0;
1703−
1704−again:
1705− if (num_pages == 0)
1706− return;
1707−
1708− processed_len = 0;
1709− is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1710− num_pages));
1711−
1712− block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1713− &state->block_hashtable);
1714− if (NULL != block) {
1715− u64 bytenr = 0;
1716− struct btrfsic_block_link *l, *tmp;
1717−
1718− if (block->is_superblock) {
1719− bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1720− mapped_datav[0]);
1721− if (num_pages * PAGE_SIZE <
1722− BTRFS_SUPER_INFO_SIZE) {
1723− pr_info("btrfsic: cannot work with too short bios!\n");
1724− return;
1725− }
1726− is_metadata = 1;
1727− BUG_ON(!PAGE_ALIGNED(BTRFS_SUPER_INFO_SIZE));
1728− processed_len = BTRFS_SUPER_INFO_SIZE;
1729− if (state->print_mask &
1730− BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1731− pr_info("[before new superblock is written]:\n");
1732− btrfsic_dump_tree_sub(state, block, 0);
1733− }
1734− }
1735− if (is_metadata) {
1736− if (!block->is_superblock) {
1737− if (num_pages * PAGE_SIZE <
1738− state->metablock_size) {
1739− pr_info("btrfsic: cannot work with too short bios!\n");
1740− return;
1741− }
1742− processed_len = state->metablock_size;
1743− bytenr = btrfs_stack_header_bytenr(
1744− (struct btrfs_header *)
1745− mapped_datav[0]);
1746− btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1747− dev_state,
1748− dev_bytenr);
1749− }
1750− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1751− if (block->logical_bytenr != bytenr &&
1752− !(!block->is_metadata &&
1753− block->logical_bytenr == 0))
1754− pr_info(
1755−"written block @%llu (%pg/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu)\n",
1756− bytenr, dev_state->bdev,
1757− dev_bytenr,
1758− block->mirror_num,
1759− btrfsic_get_block_type(state,
1760− block),
1761− block->logical_bytenr);
1762− else
1763− pr_info(
1764− "written block @%llu (%pg/%llu/%d) found in hash table, %c\n",
1765− bytenr, dev_state->bdev,
1766− dev_bytenr, block->mirror_num,
1767− btrfsic_get_block_type(state,
1768− block));
1769− }
1770− block->logical_bytenr = bytenr;
1771− } else {
1772− if (num_pages * PAGE_SIZE <
1773− state->datablock_size) {
1774− pr_info("btrfsic: cannot work with too short bios!\n");
1775− return;
1776− }
1777− processed_len = state->datablock_size;
1778− bytenr = block->logical_bytenr;
1779− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1780− pr_info(
1781− "written block @%llu (%pg/%llu/%d) found in hash table, %c\n",
1782− bytenr, dev_state->bdev, dev_bytenr,
1783− block->mirror_num,
1784− btrfsic_get_block_type(state, block));
1785− }
1786−
1787− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1788− pr_info("ref_to_list: %cE, ref_from_list: %cE\n",
1789− list_empty(&block->ref_to_list) ? ' ' : '!',
1790− list_empty(&block->ref_from_list) ? ' ' : '!');
1791− if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1792− pr_info(
1793−"btrfs: attempt to overwrite %c-block @%llu (%pg/%llu/%d), old(gen=%llu, objectid=%llu, type=%d, offset=%llu), new(gen=%llu), which is referenced by most recent superblock (superblockgen=%llu)!\n",
1794− btrfsic_get_block_type(state, block), bytenr,
1795− dev_state->bdev, dev_bytenr, block->mirror_num,
1796− block->generation,
1797− btrfs_disk_key_objectid(&block->disk_key),
1798− block->disk_key.type,
1799− btrfs_disk_key_offset(&block->disk_key),
1800− btrfs_stack_header_generation(
1801− (struct btrfs_header *) mapped_datav[0]),
1802− state->max_superblock_generation);
1803− btrfsic_dump_tree(state);
1804− }
1805−
1806− if (!block->is_iodone && !block->never_written) {
1807− pr_info(
1808−"btrfs: attempt to overwrite %c-block @%llu (%pg/%llu/%d), oldgen=%llu, newgen=%llu, which is not yet iodone!\n",
1809− btrfsic_get_block_type(state, block), bytenr,
1810− dev_state->bdev, dev_bytenr, block->mirror_num,
1811− block->generation,
1812− btrfs_stack_header_generation(
1813− (struct btrfs_header *)
1814− mapped_datav[0]));
1815− /* it would not be safe to go on */
1816− btrfsic_dump_tree(state);
1817− goto continue_loop;
1818− }
1819−
1820− /*
1821− * Clear all references of this block. Do not free
1822− * the block itself even if is not referenced anymore
1823− * because it still carries valuable information
1824− * like whether it was ever written and IO completed.
1825− */
1826− list_for_each_entry_safe(l, tmp, &block->ref_to_list,
1827− node_ref_to) {
1828− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1829− btrfsic_print_rem_link(state, l);
1830− l->ref_cnt--;
1831− if (0 == l->ref_cnt) {
1832− list_del(&l->node_ref_to);
1833− list_del(&l->node_ref_from);
1834− btrfsic_block_link_hashtable_remove(l);
1835− btrfsic_block_link_free(l);
1836− }
1837− }
1838−
1839− block_ctx.dev = dev_state;
1840− block_ctx.dev_bytenr = dev_bytenr;
1841− block_ctx.start = bytenr;
1842− block_ctx.len = processed_len;
1843− block_ctx.pagev = NULL;
1844− block_ctx.mem_to_free = NULL;
1845− block_ctx.datav = mapped_datav;
1846−
1847− if (is_metadata || state->include_extent_data) {
1848− block->never_written = 0;
1849− block->iodone_w_error = 0;
1850− if (NULL != bio) {
1851− block->is_iodone = 0;
1852− BUG_ON(NULL == bio_is_patched);
1853− if (!*bio_is_patched) {
1854− block->orig_bio_private =
1855− bio->bi_private;
1856− block->orig_bio_end_io =
1857− bio->bi_end_io;
1858− block->next_in_same_bio = NULL;
1859− bio->bi_private = block;
1860− bio->bi_end_io = btrfsic_bio_end_io;
1861− *bio_is_patched = 1;
1862− } else {
1863− struct btrfsic_block *chained_block =
1864− (struct btrfsic_block *)
1865− bio->bi_private;
1866−
1867− BUG_ON(NULL == chained_block);
1868− block->orig_bio_private =
1869− chained_block->orig_bio_private;
1870− block->orig_bio_end_io =
1871− chained_block->orig_bio_end_io;
1872− block->next_in_same_bio = chained_block;
1873− bio->bi_private = block;
1874− }
1875− } else {
1876− block->is_iodone = 1;
1877− block->orig_bio_private = NULL;
1878− block->orig_bio_end_io = NULL;
1879− block->next_in_same_bio = NULL;
1880− }
1881− }
1882−
1883− block->flush_gen = dev_state->last_flush_gen + 1;
1884− block->submit_bio_bh_rw = submit_bio_bh_rw;
1885− if (is_metadata) {
1886− block->logical_bytenr = bytenr;
1887− block->is_metadata = 1;
1888− if (block->is_superblock) {
1889− BUG_ON(PAGE_SIZE !=
1890− BTRFS_SUPER_INFO_SIZE);
1891− ret = btrfsic_process_written_superblock(
1892− state,
1893− block,
1894− (struct btrfs_super_block *)
1895− mapped_datav[0]);
1896− if (state->print_mask &
1897− BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
1898− pr_info("[after new superblock is written]:\n");
1899− btrfsic_dump_tree_sub(state, block, 0);
1900− }
1901− } else {
1902− block->mirror_num = 0; /* unknown */
1903− ret = btrfsic_process_metablock(
1904− state,
1905− block,
1906− &block_ctx,
1907− 0, 0);
1908− }
1909− if (ret)
1910− pr_info("btrfsic: btrfsic_process_metablock(root @%llu) failed!\n",
1911− dev_bytenr);
1912− } else {
1913− block->is_metadata = 0;
1914− block->mirror_num = 0; /* unknown */
1915− block->generation = BTRFSIC_GENERATION_UNKNOWN;
1916− if (!state->include_extent_data
1917− && list_empty(&block->ref_from_list)) {
1918− /*
1919− * disk block is overwritten with extent
1920− * data (not meta data) and we are configured
1921− * to not include extent data: take the
1922− * chance and free the block's memory
1923− */
1924− btrfsic_block_hashtable_remove(block);
1925− list_del(&block->all_blocks_node);
1926− btrfsic_block_free(block);
1927− }
1928− }
1929− btrfsic_release_block_ctx(&block_ctx);
1930− } else {
1931− /* block has not been found in hash table */
1932− u64 bytenr;
1933−
1934− if (!is_metadata) {
1935− processed_len = state->datablock_size;
1936− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1937− pr_info(
1938− "written block (%pg/%llu/?) !found in hash table, D\n",
1939− dev_state->bdev, dev_bytenr);
1940− if (!state->include_extent_data) {
1941− /* ignore that written D block */
1942− goto continue_loop;
1943− }
1944−
1945− /* this is getting ugly for the
1946− * include_extent_data case... */
1947− bytenr = 0; /* unknown */
1948− } else {
1949− processed_len = state->metablock_size;
1950− bytenr = btrfs_stack_header_bytenr(
1951− (struct btrfs_header *)
1952− mapped_datav[0]);
1953− btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
1954− dev_bytenr);
1955− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1956− pr_info(
1957− "written block @%llu (%pg/%llu/?) !found in hash table, M\n",
1958− bytenr, dev_state->bdev, dev_bytenr);
1959− }
1960−
1961− block_ctx.dev = dev_state;
1962− block_ctx.dev_bytenr = dev_bytenr;
1963− block_ctx.start = bytenr;
1964− block_ctx.len = processed_len;
1965− block_ctx.pagev = NULL;
1966− block_ctx.mem_to_free = NULL;
1967− block_ctx.datav = mapped_datav;
1968−
1969− block = btrfsic_block_alloc();
1970− if (NULL == block) {
1971− btrfsic_release_block_ctx(&block_ctx);
1972− goto continue_loop;
1973− }
1974− block->dev_state = dev_state;
1975− block->dev_bytenr = dev_bytenr;
1976− block->logical_bytenr = bytenr;
1977− block->is_metadata = is_metadata;
1978− block->never_written = 0;
1979− block->iodone_w_error = 0;
1980− block->mirror_num = 0; /* unknown */
1981− block->flush_gen = dev_state->last_flush_gen + 1;
1982− block->submit_bio_bh_rw = submit_bio_bh_rw;
1983− if (NULL != bio) {
1984− block->is_iodone = 0;
1985− BUG_ON(NULL == bio_is_patched);
1986− if (!*bio_is_patched) {
1987− block->orig_bio_private = bio->bi_private;
1988− block->orig_bio_end_io = bio->bi_end_io;
1989− block->next_in_same_bio = NULL;
1990− bio->bi_private = block;
1991− bio->bi_end_io = btrfsic_bio_end_io;
1992− *bio_is_patched = 1;
1993− } else {
1994− struct btrfsic_block *chained_block =
1995− (struct btrfsic_block *)
1996− bio->bi_private;
1997−
1998− BUG_ON(NULL == chained_block);
1999− block->orig_bio_private =
2000− chained_block->orig_bio_private;
2001− block->orig_bio_end_io =
2002− chained_block->orig_bio_end_io;
2003− block->next_in_same_bio = chained_block;
2004− bio->bi_private = block;
2005− }
2006− } else {
2007− block->is_iodone = 1;
2008− block->orig_bio_private = NULL;
2009− block->orig_bio_end_io = NULL;
2010− block->next_in_same_bio = NULL;
2011− }
2012− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2013− pr_info("new written %c-block @%llu (%pg/%llu/%d)\n",
2014− is_metadata ? 'M' : 'D',
2015− block->logical_bytenr, block->dev_state->bdev,
2016− block->dev_bytenr, block->mirror_num);
2017− list_add(&block->all_blocks_node, &state->all_blocks_list);
2018− btrfsic_block_hashtable_add(block, &state->block_hashtable);
2019−
2020− if (is_metadata) {
2021− ret = btrfsic_process_metablock(state, block,
2022− &block_ctx, 0, 0);
2023− if (ret)
2024− pr_info("btrfsic: process_metablock(root @%llu) failed!\n",
2025− dev_bytenr);
2026− }
2027− btrfsic_release_block_ctx(&block_ctx);
2028− }
2029−
2030−continue_loop:
2031− BUG_ON(!processed_len);
2032− dev_bytenr += processed_len;
2033− mapped_datav += processed_len >> PAGE_SHIFT;
2034− num_pages -= processed_len >> PAGE_SHIFT;
2035− goto again;
2036−}
2037−
2038−static void btrfsic_bio_end_io(struct bio *bp)
2039−{
2040− struct btrfsic_block *block = bp->bi_private;
2041− int iodone_w_error;
2042−
2043− /* mutex is not held! This is not save if IO is not yet completed
2044− * on umount */
2045− iodone_w_error = 0;
2046− if (bp->bi_status)
2047− iodone_w_error = 1;
2048−
2049− BUG_ON(NULL == block);
2050− bp->bi_private = block->orig_bio_private;
2051− bp->bi_end_io = block->orig_bio_end_io;
2052−
2053− do {
2054− struct btrfsic_block *next_block;
2055− struct btrfsic_dev_state *const dev_state = block->dev_state;
2056−
2057− if ((dev_state->state->print_mask &
2058− BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2059− pr_info("bio_end_io(err=%d) for %c @%llu (%pg/%llu/%d)\n",
2060− bp->bi_status,
2061− btrfsic_get_block_type(dev_state->state, block),
2062− block->logical_bytenr, dev_state->bdev,
2063− block->dev_bytenr, block->mirror_num);
2064− next_block = block->next_in_same_bio;
2065− block->iodone_w_error = iodone_w_error;
2066− if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
2067− dev_state->last_flush_gen++;
2068− if ((dev_state->state->print_mask &
2069− BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2070− pr_info("bio_end_io() new %pg flush_gen=%llu\n",
2071− dev_state->bdev,
2072− dev_state->last_flush_gen);
2073− }
2074− if (block->submit_bio_bh_rw & REQ_FUA)
2075− block->flush_gen = 0; /* FUA completed means block is
2076− * on disk */
2077− block->is_iodone = 1; /* for FLUSH, this releases the block */
2078− block = next_block;
2079− } while (NULL != block);
2080−
2081− bp->bi_end_io(bp);
2082−}
2083−
2084−static int btrfsic_process_written_superblock(
2085− struct btrfsic_state *state,
2086− struct btrfsic_block *const superblock,
2087− struct btrfs_super_block *const super_hdr)
2088−{
2089− struct btrfs_fs_info *fs_info = state->fs_info;
2090− int pass;
2091−
2092− superblock->generation = btrfs_super_generation(super_hdr);
2093− if (!(superblock->generation > state->max_superblock_generation ||
2094− 0 == state->max_superblock_generation)) {
2095− if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2096− pr_info(
2097− "btrfsic: superblock @%llu (%pg/%llu/%d) with old gen %llu <= %llu\n",
2098− superblock->logical_bytenr,
2099− superblock->dev_state->bdev,
2100− superblock->dev_bytenr, superblock->mirror_num,
2101− btrfs_super_generation(super_hdr),
2102− state->max_superblock_generation);
2103− } else {
2104− if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2105− pr_info(
2106− "btrfsic: got new superblock @%llu (%pg/%llu/%d) with new gen %llu > %llu\n",
2107− superblock->logical_bytenr,
2108− superblock->dev_state->bdev,
2109− superblock->dev_bytenr, superblock->mirror_num,
2110− btrfs_super_generation(super_hdr),
2111− state->max_superblock_generation);
2112−
2113− state->max_superblock_generation =
2114− btrfs_super_generation(super_hdr);
2115− state->latest_superblock = superblock;
2116− }
2117−
2118− for (pass = 0; pass < 3; pass++) {
2119− int ret;
2120− u64 next_bytenr;
2121− struct btrfsic_block *next_block;
2122− struct btrfsic_block_data_ctx tmp_next_block_ctx;
2123− struct btrfsic_block_link *l;
2124− int num_copies;
2125− int mirror_num;
2126− const char *additional_string = NULL;
2127− struct btrfs_disk_key tmp_disk_key = {0};
2128−
2129− btrfs_set_disk_key_objectid(&tmp_disk_key,
2130− BTRFS_ROOT_ITEM_KEY);
2131− btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
2132−
2133− switch (pass) {
2134− case 0:
2135− btrfs_set_disk_key_objectid(&tmp_disk_key,
2136− BTRFS_ROOT_TREE_OBJECTID);
2137− additional_string = "root ";
2138− next_bytenr = btrfs_super_root(super_hdr);
2139− if (state->print_mask &
2140− BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2141− pr_info("root@%llu\n", next_bytenr);
2142− break;
2143− case 1:
2144− btrfs_set_disk_key_objectid(&tmp_disk_key,
2145− BTRFS_CHUNK_TREE_OBJECTID);
2146− additional_string = "chunk ";
2147− next_bytenr = btrfs_super_chunk_root(super_hdr);
2148− if (state->print_mask &
2149− BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2150− pr_info("chunk@%llu\n", next_bytenr);
2151− break;
2152− case 2:
2153− btrfs_set_disk_key_objectid(&tmp_disk_key,
2154− BTRFS_TREE_LOG_OBJECTID);
2155− additional_string = "log ";
2156− next_bytenr = btrfs_super_log_root(super_hdr);
2157− if (0 == next_bytenr)
2158− continue;
2159− if (state->print_mask &
2160− BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2161− pr_info("log@%llu\n", next_bytenr);
2162− break;
2163− }
2164−
2165− num_copies = btrfs_num_copies(fs_info, next_bytenr,
2166− BTRFS_SUPER_INFO_SIZE);
2167− if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2168− pr_info("num_copies(log_bytenr=%llu) = %d\n",
2169− next_bytenr, num_copies);
2170− for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2171− int was_created;
2172−
2173− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2174− pr_info("btrfsic_process_written_superblock(mirror_num=%d)\n", mirror_num);
2175− ret = btrfsic_map_block(state, next_bytenr,
2176− BTRFS_SUPER_INFO_SIZE,
2177− &tmp_next_block_ctx,
2178− mirror_num);
2179− if (ret) {
2180− pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
2181− next_bytenr, mirror_num);
2182− return -1;
2183− }
2184−
2185− next_block = btrfsic_block_lookup_or_add(
2186− state,
2187− &tmp_next_block_ctx,
2188− additional_string,
2189− 1, 0, 1,
2190− mirror_num,
2191− &was_created);
2192− if (NULL == next_block) {
2193− btrfsic_release_block_ctx(&tmp_next_block_ctx);
2194− return -1;
2195− }
2196−
2197− next_block->disk_key = tmp_disk_key;
2198− if (was_created)
2199− next_block->generation =
2200− BTRFSIC_GENERATION_UNKNOWN;
2201− l = btrfsic_block_link_lookup_or_add(
2202− state,
2203− &tmp_next_block_ctx,
2204− next_block,
2205− superblock,
2206− BTRFSIC_GENERATION_UNKNOWN);
2207− btrfsic_release_block_ctx(&tmp_next_block_ctx);
2208− if (NULL == l)
2209− return -1;
2210− }
2211− }
2212−
2213− if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
2214− btrfsic_dump_tree(state);
2215−
2216− return 0;
2217−}
2218−
2219−static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2220− struct btrfsic_block *const block,
2221− int recursion_level)
2222−{
2223− const struct btrfsic_block_link *l;
2224− int ret = 0;
2225−
2226− if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2227− /*
2228− * Note that this situation can happen and does not
2229− * indicate an error in regular cases. It happens
2230− * when disk blocks are freed and later reused.
2231− * The check-integrity module is not aware of any
2232− * block free operations, it just recognizes block
2233− * write operations. Therefore it keeps the linkage
2234− * information for a block until a block is
2235− * rewritten. This can temporarily cause incorrect
2236− * and even circular linkage information. This
2237− * causes no harm unless such blocks are referenced
2238− * by the most recent super block.
2239− */
2240− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2241− pr_info("btrfsic: abort cyclic linkage (case 1).\n");
2242−
2243− return ret;
2244− }
2245−
2246− /*
2247− * This algorithm is recursive because the amount of used stack
2248− * space is very small and the max recursion depth is limited.
2249− */
2250− list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
2251− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2252− pr_info(
2253− "rl=%d, %c @%llu (%pg/%llu/%d) %u* refers to %c @%llu (%pg/%llu/%d)\n",
2254− recursion_level,
2255− btrfsic_get_block_type(state, block),
2256− block->logical_bytenr, block->dev_state->bdev,
2257− block->dev_bytenr, block->mirror_num,
2258− l->ref_cnt,
2259− btrfsic_get_block_type(state, l->block_ref_to),
2260− l->block_ref_to->logical_bytenr,
2261− l->block_ref_to->dev_state->bdev,
2262− l->block_ref_to->dev_bytenr,
2263− l->block_ref_to->mirror_num);
2264− if (l->block_ref_to->never_written) {
2265− pr_info(
2266−"btrfs: attempt to write superblock which references block %c @%llu (%pg/%llu/%d) which is never written!\n",
2267− btrfsic_get_block_type(state, l->block_ref_to),
2268− l->block_ref_to->logical_bytenr,
2269− l->block_ref_to->dev_state->bdev,
2270− l->block_ref_to->dev_bytenr,
2271− l->block_ref_to->mirror_num);
2272− ret = -1;
2273− } else if (!l->block_ref_to->is_iodone) {
2274− pr_info(
2275−"btrfs: attempt to write superblock which references block %c @%llu (%pg/%llu/%d) which is not yet iodone!\n",
2276− btrfsic_get_block_type(state, l->block_ref_to),
2277− l->block_ref_to->logical_bytenr,
2278− l->block_ref_to->dev_state->bdev,
2279− l->block_ref_to->dev_bytenr,
2280− l->block_ref_to->mirror_num);
2281− ret = -1;
2282− } else if (l->block_ref_to->iodone_w_error) {
2283− pr_info(
2284−"btrfs: attempt to write superblock which references block %c @%llu (%pg/%llu/%d) which has write error!\n",
2285− btrfsic_get_block_type(state, l->block_ref_to),
2286− l->block_ref_to->logical_bytenr,
2287− l->block_ref_to->dev_state->bdev,
2288− l->block_ref_to->dev_bytenr,
2289− l->block_ref_to->mirror_num);
2290− ret = -1;
2291− } else if (l->parent_generation !=
2292− l->block_ref_to->generation &&
2293− BTRFSIC_GENERATION_UNKNOWN !=
2294− l->parent_generation &&
2295− BTRFSIC_GENERATION_UNKNOWN !=
2296− l->block_ref_to->generation) {
2297− pr_info(
2298−"btrfs: attempt to write superblock which references block %c @%llu (%pg/%llu/%d) with generation %llu != parent generation %llu!\n",
2299− btrfsic_get_block_type(state, l->block_ref_to),
2300− l->block_ref_to->logical_bytenr,
2301− l->block_ref_to->dev_state->bdev,
2302− l->block_ref_to->dev_bytenr,
2303− l->block_ref_to->mirror_num,
2304− l->block_ref_to->generation,
2305− l->parent_generation);
2306− ret = -1;
2307− } else if (l->block_ref_to->flush_gen >
2308− l->block_ref_to->dev_state->last_flush_gen) {
2309− pr_info(
2310−"btrfs: attempt to write superblock which references block %c @%llu (%pg/%llu/%d) which is not flushed out of disk's write cache (block flush_gen=%llu, dev->flush_gen=%llu)!\n",
2311− btrfsic_get_block_type(state, l->block_ref_to),
2312− l->block_ref_to->logical_bytenr,
2313− l->block_ref_to->dev_state->bdev,
2314− l->block_ref_to->dev_bytenr,
2315− l->block_ref_to->mirror_num, block->flush_gen,
2316− l->block_ref_to->dev_state->last_flush_gen);
2317− ret = -1;
2318− } else if (-1 == btrfsic_check_all_ref_blocks(state,
2319− l->block_ref_to,
2320− recursion_level +
2321− 1)) {
2322− ret = -1;
2323− }
2324− }
2325−
2326− return ret;
2327−}
2328−
2329−static int btrfsic_is_block_ref_by_superblock(
2330− const struct btrfsic_state *state,
2331− const struct btrfsic_block *block,
2332− int recursion_level)
2333−{
2334− const struct btrfsic_block_link *l;
2335−
2336− if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2337− /* refer to comment at "abort cyclic linkage (case 1)" */
2338− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2339− pr_info("btrfsic: abort cyclic linkage (case 2).\n");
2340−
2341− return 0;
2342− }
2343−
2344− /*
2345− * This algorithm is recursive because the amount of used stack space
2346− * is very small and the max recursion depth is limited.
2347− */
2348− list_for_each_entry(l, &block->ref_from_list, node_ref_from) {
2349− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2350− pr_info(
2351− "rl=%d, %c @%llu (%pg/%llu/%d) is ref %u* from %c @%llu (%pg/%llu/%d)\n",
2352− recursion_level,
2353− btrfsic_get_block_type(state, block),
2354− block->logical_bytenr, block->dev_state->bdev,
2355− block->dev_bytenr, block->mirror_num,
2356− l->ref_cnt,
2357− btrfsic_get_block_type(state, l->block_ref_from),
2358− l->block_ref_from->logical_bytenr,
2359− l->block_ref_from->dev_state->bdev,
2360− l->block_ref_from->dev_bytenr,
2361− l->block_ref_from->mirror_num);
2362− if (l->block_ref_from->is_superblock &&
2363− state->latest_superblock->dev_bytenr ==
2364− l->block_ref_from->dev_bytenr &&
2365− state->latest_superblock->dev_state->bdev ==
2366− l->block_ref_from->dev_state->bdev)
2367− return 1;
2368− else if (btrfsic_is_block_ref_by_superblock(state,
2369− l->block_ref_from,
2370− recursion_level +
2371− 1))
2372− return 1;
2373− }
2374−
2375− return 0;
2376−}
2377−
2378−static void btrfsic_print_add_link(const struct btrfsic_state *state,
2379− const struct btrfsic_block_link *l)
2380−{
2381− pr_info("add %u* link from %c @%llu (%pg/%llu/%d) to %c @%llu (%pg/%llu/%d)\n",
2382− l->ref_cnt,
2383− btrfsic_get_block_type(state, l->block_ref_from),
2384− l->block_ref_from->logical_bytenr,
2385− l->block_ref_from->dev_state->bdev,
2386− l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2387− btrfsic_get_block_type(state, l->block_ref_to),
2388− l->block_ref_to->logical_bytenr,
2389− l->block_ref_to->dev_state->bdev, l->block_ref_to->dev_bytenr,
2390− l->block_ref_to->mirror_num);
2391−}
2392−
2393−static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2394− const struct btrfsic_block_link *l)
2395−{
2396− pr_info("rem %u* link from %c @%llu (%pg/%llu/%d) to %c @%llu (%pg/%llu/%d)\n",
2397− l->ref_cnt,
2398− btrfsic_get_block_type(state, l->block_ref_from),
2399− l->block_ref_from->logical_bytenr,
2400− l->block_ref_from->dev_state->bdev,
2401− l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2402− btrfsic_get_block_type(state, l->block_ref_to),
2403− l->block_ref_to->logical_bytenr,
2404− l->block_ref_to->dev_state->bdev, l->block_ref_to->dev_bytenr,
2405− l->block_ref_to->mirror_num);
2406−}
2407−
2408−static char btrfsic_get_block_type(const struct btrfsic_state *state,
2409− const struct btrfsic_block *block)
2410−{
2411− if (block->is_superblock &&
2412− state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2413− state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2414− return 'S';
2415− else if (block->is_superblock)
2416− return 's';
2417− else if (block->is_metadata)
2418− return 'M';
2419− else
2420− return 'D';
2421−}
2422−
2423−static void btrfsic_dump_tree(const struct btrfsic_state *state)
2424−{
2425− btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2426−}
2427−
2428−static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2429− const struct btrfsic_block *block,
2430− int indent_level)
2431−{
2432− const struct btrfsic_block_link *l;
2433− int indent_add;
2434− static char buf[80];
2435− int cursor_position;
2436−
2437− /*
2438− * Should better fill an on-stack buffer with a complete line and
2439− * dump it at once when it is time to print a newline character.
2440− */
2441−
2442− /*
2443− * This algorithm is recursive because the amount of used stack space
2444− * is very small and the max recursion depth is limited.
2445− */
2446− indent_add = sprintf(buf, "%c-%llu(%pg/%llu/%u)",
2447− btrfsic_get_block_type(state, block),
2448− block->logical_bytenr, block->dev_state->bdev,
2449− block->dev_bytenr, block->mirror_num);
2450− if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2451− printk("[...]\n");
2452− return;
2453− }
2454− printk(buf);
2455− indent_level += indent_add;
2456− if (list_empty(&block->ref_to_list)) {
2457− printk("\n");
2458− return;
2459− }
2460− if (block->mirror_num > 1 &&
2461− !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2462− printk(" [...]\n");
2463− return;
2464− }
2465−
2466− cursor_position = indent_level;
2467− list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
2468− while (cursor_position < indent_level) {
2469− printk(" ");
2470− cursor_position++;
2471− }
2472− if (l->ref_cnt > 1)
2473− indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2474− else
2475− indent_add = sprintf(buf, " --> ");
2476− if (indent_level + indent_add >
2477− BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2478− printk("[...]\n");
2479− cursor_position = 0;
2480− continue;
2481− }
2482−
2483− printk(buf);
2484−
2485− btrfsic_dump_tree_sub(state, l->block_ref_to,
2486− indent_level + indent_add);
2487− cursor_position = 0;
2488− }
2489−}
2490−
2491−static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2492− struct btrfsic_state *state,
2493− struct btrfsic_block_data_ctx *next_block_ctx,
2494− struct btrfsic_block *next_block,
2495− struct btrfsic_block *from_block,
2496− u64 parent_generation)
2497−{
2498− struct btrfsic_block_link *l;
2499−
2500− l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2501− next_block_ctx->dev_bytenr,
2502− from_block->dev_state->bdev,
2503− from_block->dev_bytenr,
2504− &state->block_link_hashtable);
2505− if (NULL == l) {
2506− l = btrfsic_block_link_alloc();
2507− if (!l)
2508− return NULL;
2509−
2510− l->block_ref_to = next_block;
2511− l->block_ref_from = from_block;
2512− l->ref_cnt = 1;
2513− l->parent_generation = parent_generation;
2514−
2515− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2516− btrfsic_print_add_link(state, l);
2517−
2518− list_add(&l->node_ref_to, &from_block->ref_to_list);
2519− list_add(&l->node_ref_from, &next_block->ref_from_list);
2520−
2521− btrfsic_block_link_hashtable_add(l,
2522− &state->block_link_hashtable);
2523− } else {
2524− l->ref_cnt++;
2525− l->parent_generation = parent_generation;
2526− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2527− btrfsic_print_add_link(state, l);
2528− }
2529−
2530− return l;
2531−}
2532−
2533−static struct btrfsic_block *btrfsic_block_lookup_or_add(
2534− struct btrfsic_state *state,
2535− struct btrfsic_block_data_ctx *block_ctx,
2536− const char *additional_string,
2537− int is_metadata,
2538− int is_iodone,
2539− int never_written,
2540− int mirror_num,
2541− int *was_created)
2542−{
2543− struct btrfsic_block *block;
2544−
2545− block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2546− block_ctx->dev_bytenr,
2547− &state->block_hashtable);
2548− if (NULL == block) {
2549− struct btrfsic_dev_state *dev_state;
2550−
2551− block = btrfsic_block_alloc();
2552− if (!block)
2553− return NULL;
2554−
2555− dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev->bd_dev);
2556− if (NULL == dev_state) {
2557− pr_info("btrfsic: error, lookup dev_state failed!\n");
2558− btrfsic_block_free(block);
2559− return NULL;
2560− }
2561− block->dev_state = dev_state;
2562− block->dev_bytenr = block_ctx->dev_bytenr;
2563− block->logical_bytenr = block_ctx->start;
2564− block->is_metadata = is_metadata;
2565− block->is_iodone = is_iodone;
2566− block->never_written = never_written;
2567− block->mirror_num = mirror_num;
2568− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2569− pr_info("New %s%c-block @%llu (%pg/%llu/%d)\n",
2570− additional_string,
2571− btrfsic_get_block_type(state, block),
2572− block->logical_bytenr, dev_state->bdev,
2573− block->dev_bytenr, mirror_num);
2574− list_add(&block->all_blocks_node, &state->all_blocks_list);
2575− btrfsic_block_hashtable_add(block, &state->block_hashtable);
2576− if (NULL != was_created)
2577− *was_created = 1;
2578− } else {
2579− if (NULL != was_created)
2580− *was_created = 0;
2581− }
2582−
2583− return block;
2584−}
2585−
2586−static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2587− u64 bytenr,
2588− struct btrfsic_dev_state *dev_state,
2589− u64 dev_bytenr)
2590−{
2591− struct btrfs_fs_info *fs_info = state->fs_info;
2592− struct btrfsic_block_data_ctx block_ctx;
2593− int num_copies;
2594− int mirror_num;
2595− int match = 0;
2596− int ret;
2597−
2598− num_copies = btrfs_num_copies(fs_info, bytenr, state->metablock_size);
2599−
2600− for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2601− ret = btrfsic_map_block(state, bytenr, state->metablock_size,
2602− &block_ctx, mirror_num);
2603− if (ret) {
2604− pr_info("btrfsic: btrfsic_map_block(logical @%llu, mirror %d) failed!\n",
2605− bytenr, mirror_num);
2606− continue;
2607− }
2608−
2609− if (dev_state->bdev == block_ctx.dev->bdev &&
2610− dev_bytenr == block_ctx.dev_bytenr) {
2611− match++;
2612− btrfsic_release_block_ctx(&block_ctx);
2613− break;
2614− }
2615− btrfsic_release_block_ctx(&block_ctx);
2616− }
2617−
2618− if (WARN_ON(!match)) {
2619− pr_info(
2620−"btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio, buffer->log_bytenr=%llu, submit_bio(bdev=%pg, phys_bytenr=%llu)!\n",
2621− bytenr, dev_state->bdev, dev_bytenr);
2622− for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2623− ret = btrfsic_map_block(state, bytenr,
2624− state->metablock_size,
2625− &block_ctx, mirror_num);
2626− if (ret)
2627− continue;
2628−
2629− pr_info("read logical bytenr @%llu maps to (%pg/%llu/%d)\n",
2630− bytenr, block_ctx.dev->bdev,
2631− block_ctx.dev_bytenr, mirror_num);
2632− }
2633− }
2634−}
2635−
2636−static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev)
2637−{
2638− return btrfsic_dev_state_hashtable_lookup(dev,
2639− &btrfsic_dev_state_hashtable);
2640−}
2641−
2642−static void btrfsic_check_write_bio(struct bio *bio, struct btrfsic_dev_state *dev_state)
2643−{
2644− unsigned int segs = bio_segments(bio);
2645− u64 dev_bytenr = 512 * bio->bi_iter.bi_sector;
2646− u64 cur_bytenr = dev_bytenr;
2647− struct bvec_iter iter;
2648− struct bio_vec bvec;
2649− char **mapped_datav;
2650− int bio_is_patched = 0;
2651− int i = 0;
2652−
2653− if (dev_state->state->print_mask & BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2654− pr_info(
2655−"submit_bio(rw=%d,0x%x, bi_vcnt=%u, bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
2656− bio_op(bio), bio->bi_opf, segs,
2657− bio->bi_iter.bi_sector, dev_bytenr, bio->bi_bdev);
2658−
2659− mapped_datav = kmalloc_array(segs, sizeof(*mapped_datav), GFP_NOFS);
2660− if (!mapped_datav)
2661− return;
2662−
2663− bio_for_each_segment(bvec, bio, iter) {
2664− BUG_ON(bvec.bv_len != PAGE_SIZE);
2665− mapped_datav[i] = page_address(bvec.bv_page);
2666− i++;
2667−
2668− if (dev_state->state->print_mask &
2669− BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
2670− pr_info("#%u: bytenr=%llu, len=%u, offset=%u\n",
2671− i, cur_bytenr, bvec.bv_len, bvec.bv_offset);
2672− cur_bytenr += bvec.bv_len;
2673− }
2674−
2675− btrfsic_process_written_block(dev_state, dev_bytenr, mapped_datav, segs,
2676− bio, &bio_is_patched, bio->bi_opf);
2677− kfree(mapped_datav);
2678−}
2679−
2680−static void btrfsic_check_flush_bio(struct bio *bio, struct btrfsic_dev_state *dev_state)
2681−{
2682− if (dev_state->state->print_mask & BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2683− pr_info("submit_bio(rw=%d,0x%x FLUSH, bdev=%p)\n",
2684− bio_op(bio), bio->bi_opf, bio->bi_bdev);
2685−
2686− if (dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2687− struct btrfsic_block *const block =
2688− &dev_state->dummy_block_for_bio_bh_flush;
2689−
2690− block->is_iodone = 0;
2691− block->never_written = 0;
2692− block->iodone_w_error = 0;
2693− block->flush_gen = dev_state->last_flush_gen + 1;
2694− block->submit_bio_bh_rw = bio->bi_opf;
2695− block->orig_bio_private = bio->bi_private;
2696− block->orig_bio_end_io = bio->bi_end_io;
2697− block->next_in_same_bio = NULL;
2698− bio->bi_private = block;
2699− bio->bi_end_io = btrfsic_bio_end_io;
2700− } else if ((dev_state->state->print_mask &
2701− (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2702− BTRFSIC_PRINT_MASK_VERBOSE))) {
2703− pr_info(
2704−"btrfsic_submit_bio(%pg) with FLUSH but dummy block already in use (ignored)!\n",
2705− dev_state->bdev);
2706− }
2707−}
2708−
2709−void btrfsic_check_bio(struct bio *bio)
2710−{
2711− struct btrfsic_dev_state *dev_state;
2712−
2713− if (!btrfsic_is_initialized)
2714− return;
2715−
2716− /*
2717− * We can be called before btrfsic_mount, so there might not be a
2718− * dev_state.
2719− */
2720− dev_state = btrfsic_dev_state_lookup(bio->bi_bdev->bd_dev);
2721− mutex_lock(&btrfsic_mutex);
2722− if (dev_state) {
2723− if (bio_op(bio) == REQ_OP_WRITE && bio_has_data(bio))
2724− btrfsic_check_write_bio(bio, dev_state);
2725− else if (bio->bi_opf & REQ_PREFLUSH)
2726− btrfsic_check_flush_bio(bio, dev_state);
2727− }
2728− mutex_unlock(&btrfsic_mutex);
2729−}
2730−
2731−int btrfsic_mount(struct btrfs_fs_info *fs_info,
2732− struct btrfs_fs_devices *fs_devices,
2733− int including_extent_data, u32 print_mask)
2734−{
2735− int ret;
2736− struct btrfsic_state *state;
2737− struct list_head *dev_head = &fs_devices->devices;
2738− struct btrfs_device *device;
2739−
2740− if (!PAGE_ALIGNED(fs_info->nodesize)) {
2741− pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
2742− fs_info->nodesize, PAGE_SIZE);
2743− return -1;
2744− }
2745− if (!PAGE_ALIGNED(fs_info->sectorsize)) {
2746− pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
2747− fs_info->sectorsize, PAGE_SIZE);
2748− return -1;
2749− }
2750− state = kvzalloc(sizeof(*state), GFP_KERNEL);
2751− if (!state)
2752− return -ENOMEM;
2753−
2754− if (!btrfsic_is_initialized) {
2755− mutex_init(&btrfsic_mutex);
2756− btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
2757− btrfsic_is_initialized = 1;
2758− }
2759− mutex_lock(&btrfsic_mutex);
2760− state->fs_info = fs_info;
2761− state->print_mask = print_mask;
2762− state->include_extent_data = including_extent_data;
2763− state->metablock_size = fs_info->nodesize;
2764− state->datablock_size = fs_info->sectorsize;
2765− INIT_LIST_HEAD(&state->all_blocks_list);
2766− btrfsic_block_hashtable_init(&state->block_hashtable);
2767− btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
2768− state->max_superblock_generation = 0;
2769− state->latest_superblock = NULL;
2770−
2771− list_for_each_entry(device, dev_head, dev_list) {
2772− struct btrfsic_dev_state *ds;
2773−
2774− if (!device->bdev || !device->name)
2775− continue;
2776−
2777− ds = btrfsic_dev_state_alloc();
2778− if (NULL == ds) {
2779− mutex_unlock(&btrfsic_mutex);
2780− return -ENOMEM;
2781− }
2782− ds->bdev = device->bdev;
2783− ds->state = state;
2784− btrfsic_dev_state_hashtable_add(ds,
2785− &btrfsic_dev_state_hashtable);
2786− }
2787−
2788− ret = btrfsic_process_superblock(state, fs_devices);
2789− if (0 != ret) {
2790− mutex_unlock(&btrfsic_mutex);
2791− btrfsic_unmount(fs_devices);
2792− return ret;
2793− }
2794−
2795− if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
2796− btrfsic_dump_database(state);
2797− if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
2798− btrfsic_dump_tree(state);
2799−
2800− mutex_unlock(&btrfsic_mutex);
2801− return 0;
2802−}
2803−
2804−void btrfsic_unmount(struct btrfs_fs_devices *fs_devices)
2805−{
2806− struct btrfsic_block *b_all, *tmp_all;
2807− struct btrfsic_state *state;
2808− struct list_head *dev_head = &fs_devices->devices;
2809− struct btrfs_device *device;
2810−
2811− if (!btrfsic_is_initialized)
2812− return;
2813−
2814− mutex_lock(&btrfsic_mutex);
2815−
2816− state = NULL;
2817− list_for_each_entry(device, dev_head, dev_list) {
2818− struct btrfsic_dev_state *ds;
2819−
2820− if (!device->bdev || !device->name)
2821− continue;
2822−
2823− ds = btrfsic_dev_state_hashtable_lookup(
2824− device->bdev->bd_dev,
2825− &btrfsic_dev_state_hashtable);
2826− if (NULL != ds) {
2827− state = ds->state;
2828− btrfsic_dev_state_hashtable_remove(ds);
2829− btrfsic_dev_state_free(ds);
2830− }
2831− }
2832−
2833− if (NULL == state) {
2834− pr_info("btrfsic: error, cannot find state information on umount!\n");
2835− mutex_unlock(&btrfsic_mutex);
2836− return;
2837− }
2838−
2839− /*
2840− * Don't care about keeping the lists' state up to date,
2841− * just free all memory that was allocated dynamically.
2842− * Free the blocks and the block_links.
2843− */
2844− list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
2845− all_blocks_node) {
2846− struct btrfsic_block_link *l, *tmp;
2847−
2848− list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
2849− node_ref_to) {
2850− if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2851− btrfsic_print_rem_link(state, l);
2852−
2853− l->ref_cnt--;
2854− if (0 == l->ref_cnt)
2855− btrfsic_block_link_free(l);
2856− }
2857−
2858− if (b_all->is_iodone || b_all->never_written)
2859− btrfsic_block_free(b_all);
2860− else
2861− pr_info(
2862−"btrfs: attempt to free %c-block @%llu (%pg/%llu/%d) on umount which is not yet iodone!\n",
2863− btrfsic_get_block_type(state, b_all),
2864− b_all->logical_bytenr, b_all->dev_state->bdev,
2865− b_all->dev_bytenr, b_all->mirror_num);
2866− }
2867−
2868− mutex_unlock(&btrfsic_mutex);
2869−
2870− kvfree(state);
2871−}
fs/btrfs/check-integrity.h+0−20
@@ -1,20 +0,0 @@
1−/* SPDX-License-Identifier: GPL-2.0 */
2−/*
3− * Copyright (C) STRATO AG 2011. All rights reserved.
4− */
5−
6−#ifndef BTRFS_CHECK_INTEGRITY_H
7−#define BTRFS_CHECK_INTEGRITY_H
8−
9−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
10−void btrfsic_check_bio(struct bio *bio);
11−#else
12−static inline void btrfsic_check_bio(struct bio *bio) { }
13−#endif
14−
15−int btrfsic_mount(struct btrfs_fs_info *fs_info,
16− struct btrfs_fs_devices *fs_devices,
17− int including_extent_data, u32 print_mask);
18−void btrfsic_unmount(struct btrfs_fs_devices *fs_devices);
19−
20−#endif
fs/btrfs/compression.c+3−3
@@ -193,12 +193,12 @@ static noinline void end_compressed_writeback(const struct compressed_bio *cb) · 192 unmodified lines
193193 unsigned long index = cb->start >> PAGE_SHIFT;
194194 unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
195195 struct folio_batch fbatch;
196− const int errno = blk_status_to_errno(cb->bbio.bio.bi_status);
196+ const int error = blk_status_to_errno(cb->bbio.bio.bi_status);
197197 int i;
198198 int ret;
199199
200− if (errno)
201− mapping_set_error(inode->i_mapping, errno);
200+ if (error)
201+ mapping_set_error(inode->i_mapping, error);
202202
203203 folio_batch_init(&fbatch);
204204 while (index <= end_index) {
fs/btrfs/ctree.c+106−234
@@ -230,9 +230,9 @@ noinline void btrfs_release_path(struct btrfs_path *p) · 229 unmodified lines
230230 * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
231231 * caused by external factors.
232232 */
233−bool __cold abort_should_print_stack(int errno)
233+bool __cold abort_should_print_stack(int error)
234234 {
235− switch (errno) {
235+ switch (error) {
236236 case -EIO:
237237 case -EROFS:
238238 case -ENOMEM:
@@ -316,6 +316,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 77 unmodified lines
316316 int ret = 0;
317317 int level;
318318 struct btrfs_disk_key disk_key;
319+ u64 reloc_src_root = 0;
319320
320321 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
321322 trans->transid != fs_info->running_transaction->transid);
@@ -328,9 +329,11 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 6 unmodified lines
328329 else
329330 btrfs_node_key(buf, &disk_key, 0);
330331
332+ if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
333+ reloc_src_root = btrfs_header_owner(buf);
331334 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
332335 &disk_key, level, buf->start, 0,
333− BTRFS_NESTING_NEW_ROOT);
336+ reloc_src_root, BTRFS_NESTING_NEW_ROOT);
334337 if (IS_ERR(cow))
335338 return PTR_ERR(cow);
336339
@@ -359,7 +362,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 22 unmodified lines
359362 return ret;
360363 }
361364
362− btrfs_mark_buffer_dirty(cow);
365+ btrfs_mark_buffer_dirty(trans, cow);
363366 *cow_ret = cow;
364367 return 0;
365368 }
@@ -518,13 +521,13 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, · 152 unmodified lines
518521 * bytes the allocator should try to find free next to the block it returns.
519522 * This is just a hint and may be ignored by the allocator.
520523 */
521−static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
522− struct btrfs_root *root,
523− struct extent_buffer *buf,
524− struct extent_buffer *parent, int parent_slot,
525− struct extent_buffer **cow_ret,
526− u64 search_start, u64 empty_size,
527− enum btrfs_lock_nesting nest)
524+int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
525+ struct btrfs_root *root,
526+ struct extent_buffer *buf,
527+ struct extent_buffer *parent, int parent_slot,
528+ struct extent_buffer **cow_ret,
529+ u64 search_start, u64 empty_size,
530+ enum btrfs_lock_nesting nest)
528531 {
529532 struct btrfs_fs_info *fs_info = root->fs_info;
530533 struct btrfs_disk_key disk_key;
@@ -533,6 +536,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 2 unmodified lines
533536 int last_ref = 0;
534537 int unlock_orig = 0;
535538 u64 parent_start = 0;
539+ u64 reloc_src_root = 0;
536540
537541 if (*cow_ret == buf)
538542 unlock_orig = 1;
@@ -551,12 +555,14 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 12 unmodified lines
551555 else
552556 btrfs_node_key(buf, &disk_key, 0);
553557
554− if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
555− parent_start = parent->start;
556−
558+ if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
559+ if (parent)
560+ parent_start = parent->start;
561+ reloc_src_root = btrfs_header_owner(buf);
562+ }
557563 cow = btrfs_alloc_tree_block(trans, root, parent_start,
558564 root->root_key.objectid, &disk_key, level,
559− search_start, empty_size, nest);
565+ search_start, empty_size, reloc_src_root, nest);
560566 if (IS_ERR(cow))
561567 return PTR_ERR(cow);
562568
@@ -627,7 +633,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 64 unmodified lines
627633 cow->start);
628634 btrfs_set_node_ptr_generation(parent, parent_slot,
629635 trans->transid);
630− btrfs_mark_buffer_dirty(parent);
636+ btrfs_mark_buffer_dirty(trans, parent);
631637 if (last_ref) {
632638 ret = btrfs_tree_mod_log_free_eb(buf);
633639 if (ret) {
@@ -643,7 +649,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 9 unmodified lines
643649 if (unlock_orig)
644650 btrfs_tree_unlock(buf);
645651 free_extent_buffer_stale(buf);
646− btrfs_mark_buffer_dirty(cow);
652+ btrfs_mark_buffer_dirty(trans, cow);
647653 *cow_ret = cow;
648654 return 0;
649655 }
@@ -679,11 +685,11 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans, · 29 unmodified lines
679685 }
680686
681687 /*
682− * cows a single block, see __btrfs_cow_block for the real work.
688+ * COWs a single block, see btrfs_force_cow_block() for the real work.
683689 * This version of it has extra checks so that a block isn't COWed more than
684690 * once per transaction, as long as it hasn't been written yet
685691 */
686−noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
692+int btrfs_cow_block(struct btrfs_trans_handle *trans,
687693 struct btrfs_root *root, struct extent_buffer *buf,
688694 struct extent_buffer *parent, int parent_slot,
689695 struct extent_buffer **cow_ret,
@@ -723,7 +729,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, · 33 unmodified lines
723729 return 0;
724730 }
725731
726− search_start = buf->start & ~((u64)SZ_1G - 1);
732+ search_start = round_down(buf->start, SZ_1G);
727733
728734 /*
729735 * Before CoWing this block for later modification, check if it's
@@ -732,8 +738,8 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, · 2 unmodified lines
732738 * Also We don't care about the error, as it's handled internally.
733739 */
734740 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
735− ret = __btrfs_cow_block(trans, root, buf, parent,
736− parent_slot, cow_ret, search_start, 0, nest);
741+ ret = btrfs_force_cow_block(trans, root, buf, parent, parent_slot,
742+ cow_ret, search_start, 0, nest);
737743
738744 trace_btrfs_cow_block(root, buf, *cow_ret);
739745
@@ -741,49 +747,6 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, · 1 unmodified line
741747 }
742748 ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
743749
744−/*
745− * helper function for defrag to decide if two blocks pointed to by a
746− * node are actually close by
747− */
748−static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
749−{
750− if (blocknr < other && other - (blocknr + blocksize) < 32768)
751− return 1;
752− if (blocknr > other && blocknr - (other + blocksize) < 32768)
753− return 1;
754− return 0;
755−}
756−
757−#ifdef __LITTLE_ENDIAN
758−
759−/*
760− * Compare two keys, on little-endian the disk order is same as CPU order and
761− * we can avoid the conversion.
762− */
763−static int comp_keys(const struct btrfs_disk_key *disk_key,
764− const struct btrfs_key *k2)
765−{
766− const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
767−
768− return btrfs_comp_cpu_keys(k1, k2);
769−}
770−
771−#else
772−
773−/*
774− * compare two keys in a memcmp fashion
775− */
776−static int comp_keys(const struct btrfs_disk_key *disk,
777− const struct btrfs_key *k2)
778−{
779− struct btrfs_key k1;
780−
781− btrfs_disk_key_to_cpu(&k1, disk);
782−
783− return btrfs_comp_cpu_keys(&k1, k2);
784−}
785−#endif
786−
787750 /*
788751 * same as comp_keys only with two btrfs_key's
789752 */
@@ -804,105 +767,6 @@ int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_ke · 14 unmodified lines
804767 return 0;
805768 }
806769
807−/*
808− * this is used by the defrag code to go through all the
809− * leaves pointed to by a node and reallocate them so that
810− * disk order is close to key order
811− */
812−int btrfs_realloc_node(struct btrfs_trans_handle *trans,
813− struct btrfs_root *root, struct extent_buffer *parent,
814− int start_slot, u64 *last_ret,
815− struct btrfs_key *progress)
816−{
817− struct btrfs_fs_info *fs_info = root->fs_info;
818− struct extent_buffer *cur;
819− u64 blocknr;
820− u64 search_start = *last_ret;
821− u64 last_block = 0;
822− u64 other;
823− u32 parent_nritems;
824− int end_slot;
825− int i;
826− int err = 0;
827− u32 blocksize;
828− int progress_passed = 0;
829− struct btrfs_disk_key disk_key;
830−
831− /*
832− * COWing must happen through a running transaction, which always
833− * matches the current fs generation (it's a transaction with a state
834− * less than TRANS_STATE_UNBLOCKED). If it doesn't, then turn the fs
835− * into error state to prevent the commit of any transaction.
836− */
837− if (unlikely(trans->transaction != fs_info->running_transaction ||
838− trans->transid != fs_info->generation)) {
839− btrfs_abort_transaction(trans, -EUCLEAN);
840− btrfs_crit(fs_info,
841−"unexpected transaction when attempting to reallocate parent %llu for root %llu, transaction %llu running transaction %llu fs generation %llu",
842− parent->start, btrfs_root_id(root), trans->transid,
843− fs_info->running_transaction->transid,
844− fs_info->generation);
845− return -EUCLEAN;
846− }
847−
848− parent_nritems = btrfs_header_nritems(parent);
849− blocksize = fs_info->nodesize;
850− end_slot = parent_nritems - 1;
851−
852− if (parent_nritems <= 1)
853− return 0;
854−
855− for (i = start_slot; i <= end_slot; i++) {
856− int close = 1;
857−
858− btrfs_node_key(parent, &disk_key, i);
859− if (!progress_passed && comp_keys(&disk_key, progress) < 0)
860− continue;
861−
862− progress_passed = 1;
863− blocknr = btrfs_node_blockptr(parent, i);
864− if (last_block == 0)
865− last_block = blocknr;
866−
867− if (i > 0) {
868− other = btrfs_node_blockptr(parent, i - 1);
869− close = close_blocks(blocknr, other, blocksize);
870− }
871− if (!close && i < end_slot) {
872− other = btrfs_node_blockptr(parent, i + 1);
873− close = close_blocks(blocknr, other, blocksize);
874− }
875− if (close) {
876− last_block = blocknr;
877− continue;
878− }
879−
880− cur = btrfs_read_node_slot(parent, i);
881− if (IS_ERR(cur))
882− return PTR_ERR(cur);
883− if (search_start == 0)
884− search_start = last_block;
885−
886− btrfs_tree_lock(cur);
887− err = __btrfs_cow_block(trans, root, cur, parent, i,
888− &cur, search_start,
889− min(16 * blocksize,
890− (end_slot - i) * blocksize),
891− BTRFS_NESTING_COW);
892− if (err) {
893− btrfs_tree_unlock(cur);
894− free_extent_buffer(cur);
895− break;
896− }
897− search_start = cur->start;
898− last_block = cur->start;
899− *last_ret = search_start;
900− btrfs_tree_unlock(cur);
901− free_extent_buffer(cur);
902− }
903− return err;
904−}
905−
906770 /*
907771 * Search for a key in the given extent_buffer.
908772 *
@@ -969,7 +833,7 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 60 unmodified lines
969833 tmp = &unaligned;
970834 }
971835
972− ret = comp_keys(tmp, key);
836+ ret = btrfs_comp_keys(tmp, key);
973837
974838 if (ret < 0)
975839 low = mid + 1;
@@ -984,19 +848,19 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 8 unmodified lines
984848 return 1;
985849 }
986850
987−static void root_add_used(struct btrfs_root *root, u32 size)
851+static void root_add_used_bytes(struct btrfs_root *root)
988852 {
989853 spin_lock(&root->accounting_lock);
990854 btrfs_set_root_used(&root->root_item,
991− btrfs_root_used(&root->root_item) + size);
855+ btrfs_root_used(&root->root_item) + root->fs_info->nodesize);
992856 spin_unlock(&root->accounting_lock);
993857 }
994858
995−static void root_sub_used(struct btrfs_root *root, u32 size)
859+static void root_sub_used_bytes(struct btrfs_root *root)
996860 {
997861 spin_lock(&root->accounting_lock);
998862 btrfs_set_root_used(&root->root_item,
999− btrfs_root_used(&root->root_item) - size);
863+ btrfs_root_used(&root->root_item) - root->fs_info->nodesize);
1000864 spin_unlock(&root->accounting_lock);
1001865 }
1002866
@@ -1112,7 +976,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 109 unmodified lines
1112976 /* once for the path */
1113977 free_extent_buffer(mid);
1114978
1115− root_sub_used(root, mid->len);
979+ root_sub_used_bytes(root);
1116980 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1117981 /* once for the root ptr */
1118982 free_extent_buffer_stale(mid);
@@ -1182,7 +1046,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 63 unmodified lines
11821046 right = NULL;
11831047 goto out;
11841048 }
1185− root_sub_used(root, right->len);
1049+ root_sub_used_bytes(root);
11861050 btrfs_free_tree_block(trans, btrfs_root_id(root), right,
11871051 0, 1);
11881052 free_extent_buffer_stale(right);
@@ -1197,7 +1061,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 8 unmodified lines
11971061 goto out;
11981062 }
11991063 btrfs_set_node_key(parent, &right_key, pslot + 1);
1200− btrfs_mark_buffer_dirty(parent);
1064+ btrfs_mark_buffer_dirty(trans, parent);
12011065 }
12021066 }
12031067 if (btrfs_header_nritems(mid) == 1) {
@@ -1240,7 +1104,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 36 unmodified lines
12401104 mid = NULL;
12411105 goto out;
12421106 }
1243− root_sub_used(root, mid->len);
1107+ root_sub_used_bytes(root);
12441108 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
12451109 free_extent_buffer_stale(mid);
12461110 mid = NULL;
@@ -1255,7 +1119,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 8 unmodified lines
12551119 goto out;
12561120 }
12571121 btrfs_set_node_key(parent, &mid_key, pslot);
1258− btrfs_mark_buffer_dirty(parent);
1122+ btrfs_mark_buffer_dirty(trans, parent);
12591123 }
12601124
12611125 /* update the path */
@@ -1362,7 +1226,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, · 100 unmodified lines
13621226 return ret;
13631227 }
13641228 btrfs_set_node_key(parent, &disk_key, pslot);
1365− btrfs_mark_buffer_dirty(parent);
1229+ btrfs_mark_buffer_dirty(trans, parent);
13661230 if (btrfs_header_nritems(left) > orig_slot) {
13671231 path->nodes[level] = left;
13681232 path->slots[level + 1] -= 1;
@@ -1422,7 +1286,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, · 53 unmodified lines
14221286 return ret;
14231287 }
14241288 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1425− btrfs_mark_buffer_dirty(parent);
1289+ btrfs_mark_buffer_dirty(trans, parent);
14261290
14271291 if (btrfs_header_nritems(mid) <= orig_slot) {
14281292 path->nodes[level] = right;
@@ -2006,7 +1870,7 @@ static int search_leaf(struct btrfs_trans_handle *trans, · 577 unmodified lines
20061870 * the extent buffer's header and we have recently accessed
20071871 * the header's level field.
20081872 */
2009− ret = comp_keys(&first_key, key);
1873+ ret = btrfs_comp_keys(&first_key, key);
20101874 if (ret < 0) {
20111875 /*
20121876 * The first key is smaller than the key we want
@@ -2091,8 +1955,8 @@ static int search_leaf(struct btrfs_trans_handle *trans, · 78 unmodified lines
20911955 }
20921956
20931957 /*
2094− * btrfs_search_slot - look for a key in a tree and perform necessary
2095− * modifications to preserve tree invariants.
1958+ * Look for a key in a tree and perform necessary modifications to preserve
1959+ * tree invariants.
20961960 *
20971961 * @trans: Handle of transaction, used when modifying the tree
20981962 * @p: Holds all btree nodes along the search path
@@ -2515,7 +2379,7 @@ static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) · 416 unmodified lines
25152379 */
25162380 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
25172381 btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);
2518− ret = comp_keys(&found_key, &orig_key);
2382+ ret = btrfs_comp_keys(&found_key, &orig_key);
25192383 if (ret == 0) {
25202384 if (path->slots[0] > 0) {
25212385 path->slots[0]--;
@@ -2530,7 +2394,7 @@ static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) · 8 unmodified lines
25302394 }
25312395
25322396 btrfs_item_key(path->nodes[0], &found_key, 0);
2533− ret = comp_keys(&found_key, &key);
2397+ ret = btrfs_comp_keys(&found_key, &key);
25342398 /*
25352399 * We might have had an item with the previous key in the tree right
25362400 * before we released our path. And after we released our path, that
@@ -2678,7 +2542,8 @@ int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key, · 141 unmodified lines
26782542 * higher levels
26792543 *
26802544 */
2681−static void fixup_low_keys(struct btrfs_path *path,
2545+static void fixup_low_keys(struct btrfs_trans_handle *trans,
2546+ struct btrfs_path *path,
26822547 struct btrfs_disk_key *key, int level)
26832548 {
26842549 int i;
@@ -2695,7 +2560,7 @@ static void fixup_low_keys(struct btrfs_path *path, · 10 unmodified lines
26952560 BTRFS_MOD_LOG_KEY_REPLACE);
26962561 BUG_ON(ret < 0);
26972562 btrfs_set_node_key(t, key, tslot);
2698− btrfs_mark_buffer_dirty(path->nodes[i]);
2563+ btrfs_mark_buffer_dirty(trans, path->nodes[i]);
26992564 if (tslot != 0)
27002565 break;
27012566 }
@@ -2707,10 +2572,11 @@ static void fixup_low_keys(struct btrfs_path *path, · 5 unmodified lines
27072572 * This function isn't completely safe. It's the caller's responsibility
27082573 * that the new key won't break the order
27092574 */
2710−void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2575+void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
27112576 struct btrfs_path *path,
27122577 const struct btrfs_key *new_key)
27132578 {
2579+ struct btrfs_fs_info *fs_info = trans->fs_info;
27142580 struct btrfs_disk_key disk_key;
27152581 struct extent_buffer *eb;
27162582 int slot;
@@ -2719,7 +2585,7 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, · 2 unmodified lines
27192585 slot = path->slots[0];
27202586 if (slot > 0) {
27212587 btrfs_item_key(eb, &disk_key, slot - 1);
2722− if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2588+ if (unlikely(btrfs_comp_keys(&disk_key, new_key) >= 0)) {
27232589 btrfs_print_leaf(eb);
27242590 btrfs_crit(fs_info,
27252591 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
@@ -2733,7 +2599,7 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, · 7 unmodified lines
27332599 }
27342600 if (slot < btrfs_header_nritems(eb) - 1) {
27352601 btrfs_item_key(eb, &disk_key, slot + 1);
2736− if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2602+ if (unlikely(btrfs_comp_keys(&disk_key, new_key) <= 0)) {
27372603 btrfs_print_leaf(eb);
27382604 btrfs_crit(fs_info,
27392605 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
@@ -2748,9 +2614,9 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, · 8 unmodified lines
27482614
27492615 btrfs_cpu_key_to_disk(&disk_key, new_key);
27502616 btrfs_set_item_key(eb, &disk_key, slot);
2751− btrfs_mark_buffer_dirty(eb);
2617+ btrfs_mark_buffer_dirty(trans, eb);
27522618 if (slot == 0)
2753− fixup_low_keys(path, &disk_key, 1);
2619+ fixup_low_keys(trans, path, &disk_key, 1);
27542620 }
27552621
27562622 /*
@@ -2881,8 +2747,8 @@ static int push_node_left(struct btrfs_trans_handle *trans, · 124 unmodified lines
28812747 }
28822748 btrfs_set_header_nritems(src, src_nritems - push_items);
28832749 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2884− btrfs_mark_buffer_dirty(src);
2885− btrfs_mark_buffer_dirty(dst);
2750+ btrfs_mark_buffer_dirty(trans, src);
2751+ btrfs_mark_buffer_dirty(trans, dst);
28862752
28872753 return ret;
28882754 }
@@ -2957,8 +2823,8 @@ static int balance_node_right(struct btrfs_trans_handle *trans, · 68 unmodified lines
29572823 btrfs_set_header_nritems(src, src_nritems - push_items);
29582824 btrfs_set_header_nritems(dst, dst_nritems + push_items);
29592825
2960− btrfs_mark_buffer_dirty(src);
2961− btrfs_mark_buffer_dirty(dst);
2826+ btrfs_mark_buffer_dirty(trans, src);
2827+ btrfs_mark_buffer_dirty(trans, dst);
29622828
29632829 return ret;
29642830 }
@@ -2974,7 +2840,6 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, · 9 unmodified lines
29742840 struct btrfs_root *root,
29752841 struct btrfs_path *path, int level)
29762842 {
2977− struct btrfs_fs_info *fs_info = root->fs_info;
29782843 u64 lower_gen;
29792844 struct extent_buffer *lower;
29802845 struct extent_buffer *c;
@@ -2993,11 +2858,11 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, · 12 unmodified lines
29932858
29942859 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
29952860 &lower_key, level, root->node->start, 0,
2996− BTRFS_NESTING_NEW_ROOT);
2861+ 0, BTRFS_NESTING_NEW_ROOT);
29972862 if (IS_ERR(c))
29982863 return PTR_ERR(c);
29992864
3000− root_add_used(root, fs_info->nodesize);
2865+ root_add_used_bytes(root);
30012866
30022867 btrfs_set_header_nritems(c, 1);
30032868 btrfs_set_node_key(c, &lower_key, 0);
@@ -3007,7 +2872,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, · 3 unmodified lines
30072872
30082873 btrfs_set_node_ptr_generation(c, 0, lower_gen);
30092874
3010− btrfs_mark_buffer_dirty(c);
2875+ btrfs_mark_buffer_dirty(trans, c);
30112876
30122877 old = root->node;
30132878 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
@@ -3079,7 +2944,7 @@ static int insert_ptr(struct btrfs_trans_handle *trans, · 65 unmodified lines
30792944 WARN_ON(trans->transid == 0);
30802945 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
30812946 btrfs_set_header_nritems(lower, nritems + 1);
3082− btrfs_mark_buffer_dirty(lower);
2947+ btrfs_mark_buffer_dirty(trans, lower);
30832948
30842949 return 0;
30852950 }
@@ -3137,11 +3002,11 @@ static noinline int split_node(struct btrfs_trans_handle *trans, · 51 unmodified lines
31373002
31383003 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
31393004 &disk_key, level, c->start, 0,
3140− BTRFS_NESTING_SPLIT);
3005+ 0, BTRFS_NESTING_SPLIT);
31413006 if (IS_ERR(split))
31423007 return PTR_ERR(split);
31433008
3144− root_add_used(root, fs_info->nodesize);
3009+ root_add_used_bytes(root);
31453010 ASSERT(btrfs_header_level(c) == level);
31463011
31473012 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
@@ -3158,8 +3023,8 @@ static noinline int split_node(struct btrfs_trans_handle *trans, · 10 unmodified lines
31583023 btrfs_set_header_nritems(split, c_nritems - mid);
31593024 btrfs_set_header_nritems(c, mid);
31603025
3161− btrfs_mark_buffer_dirty(c);
3162− btrfs_mark_buffer_dirty(split);
3026+ btrfs_mark_buffer_dirty(trans, c);
3027+ btrfs_mark_buffer_dirty(trans, split);
31633028
31643029 ret = insert_ptr(trans, path, &disk_key, split->start,
31653030 path->slots[level + 1] + 1, level + 1);
@@ -3325,15 +3190,15 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, · 159 unmodified lines
33253190 btrfs_set_header_nritems(left, left_nritems);
33263191
33273192 if (left_nritems)
3328− btrfs_mark_buffer_dirty(left);
3193+ btrfs_mark_buffer_dirty(trans, left);
33293194 else
33303195 btrfs_clear_buffer_dirty(trans, left);
33313196
3332− btrfs_mark_buffer_dirty(right);
3197+ btrfs_mark_buffer_dirty(trans, right);
33333198
33343199 btrfs_item_key(right, &disk_key, 0);
33353200 btrfs_set_node_key(upper, &disk_key, slot + 1);
3336− btrfs_mark_buffer_dirty(upper);
3201+ btrfs_mark_buffer_dirty(trans, upper);
33373202
33383203 /* then fixup the leaf pointer in the path */
33393204 if (path->slots[0] >= left_nritems) {
@@ -3545,14 +3410,14 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, · 205 unmodified lines
35453410 btrfs_set_token_item_offset(&token, i, push_space);
35463411 }
35473412
3548− btrfs_mark_buffer_dirty(left);
3413+ btrfs_mark_buffer_dirty(trans, left);
35493414 if (right_nritems)
3550− btrfs_mark_buffer_dirty(right);
3415+ btrfs_mark_buffer_dirty(trans, right);
35513416 else
35523417 btrfs_clear_buffer_dirty(trans, right);
35533418
35543419 btrfs_item_key(right, &disk_key, 0);
3555− fixup_low_keys(path, &disk_key, 1);
3420+ fixup_low_keys(trans, path, &disk_key, 1);
35563421
35573422 /* then fixup the leaf pointer in the path */
35583423 if (path->slots[0] < push_items) {
@@ -3683,8 +3548,8 @@ static noinline int copy_for_split(struct btrfs_trans_handle *trans, · 124 unmodified lines
36833548 if (ret < 0)
36843549 return ret;
36853550
3686− btrfs_mark_buffer_dirty(right);
3687− btrfs_mark_buffer_dirty(l);
3551+ btrfs_mark_buffer_dirty(trans, right);
3552+ btrfs_mark_buffer_dirty(trans, l);
36883553 BUG_ON(path->slots[0] != slot);
36893554
36903555 if (mid <= slot) {
@@ -3888,13 +3753,13 @@ again: · 197 unmodified lines
38883753 * use BTRFS_NESTING_NEW_ROOT.
38893754 */
38903755 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3891− &disk_key, 0, l->start, 0,
3756+ &disk_key, 0, l->start, 0, 0,
38923757 num_doubles ? BTRFS_NESTING_NEW_ROOT :
38933758 BTRFS_NESTING_SPLIT);
38943759 if (IS_ERR(right))
38953760 return PTR_ERR(right);
38963761
3897− root_add_used(root, fs_info->nodesize);
3762+ root_add_used_bytes(root);
38983763
38993764 if (split == 0) {
39003765 if (mid <= slot) {
@@ -3925,7 +3790,7 @@ again: · 24 unmodified lines
39253790 path->nodes[0] = right;
39263791 path->slots[0] = 0;
39273792 if (path->slots[1] == 0)
3928− fixup_low_keys(path, &disk_key, 1);
3793+ fixup_low_keys(trans, path, &disk_key, 1);
39293794 }
39303795 /*
39313796 * We create a new leaf 'right' for the required ins_len and
@@ -4024,7 +3889,8 @@ err: · 92 unmodified lines
40243889 return ret;
40253890 }
40263891
4027−static noinline int split_item(struct btrfs_path *path,
3892+static noinline int split_item(struct btrfs_trans_handle *trans,
3893+ struct btrfs_path *path,
40283894 const struct btrfs_key *new_key,
40293895 unsigned long split_offset)
40303896 {
@@ -4083,7 +3949,7 @@ static noinline int split_item(struct btrfs_path *path, · 52 unmodified lines
40833949 write_extent_buffer(leaf, buf + split_offset,
40843950 btrfs_item_ptr_offset(leaf, slot),
40853951 item_size - split_offset);
4086− btrfs_mark_buffer_dirty(leaf);
3952+ btrfs_mark_buffer_dirty(trans, leaf);
40873953
40883954 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
40893955 kfree(buf);
@@ -4117,7 +3983,7 @@ int btrfs_split_item(struct btrfs_trans_handle *trans, · 27 unmodified lines
41173983 if (ret)
41183984 return ret;
41193985
4120− ret = split_item(path, new_key, split_offset);
3986+ ret = split_item(trans, path, new_key, split_offset);
41213987 return ret;
41223988 }
41233989
@@ -4127,7 +3993,8 @@ int btrfs_split_item(struct btrfs_trans_handle *trans, · 3 unmodified lines
41273993 * off the end of the item or if we shift the item to chop bytes off
41283994 * the front.
41293995 */
4130−void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
3996+void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3997+ struct btrfs_path *path, u32 new_size, int from_end)
41313998 {
41323999 int slot;
41334000 struct extent_buffer *leaf;
@@ -4203,11 +4070,11 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) · 69 unmodified lines
42034070 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
42044071 btrfs_set_item_key(leaf, &disk_key, slot);
42054072 if (slot == 0)
4206− fixup_low_keys(path, &disk_key, 1);
4073+ fixup_low_keys(trans, path, &disk_key, 1);
42074074 }
42084075
42094076 btrfs_set_item_size(leaf, slot, new_size);
4210− btrfs_mark_buffer_dirty(leaf);
4077+ btrfs_mark_buffer_dirty(trans, leaf);
42114078
42124079 if (btrfs_leaf_free_space(leaf) < 0) {
42134080 btrfs_print_leaf(leaf);
@@ -4218,7 +4085,8 @@ void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) · 4 unmodified lines
42184085 /*
42194086 * make the item pointed to by the path bigger, data_size is the added size.
42204087 */
4221−void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
4088+void btrfs_extend_item(struct btrfs_trans_handle *trans,
4089+ struct btrfs_path *path, u32 data_size)
42224090 {
42234091 int slot;
42244092 struct extent_buffer *leaf;
@@ -4268,7 +4136,7 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) · 43 unmodified lines
42684136 data_end = old_data;
42694137 old_size = btrfs_item_size(leaf, slot);
42704138 btrfs_set_item_size(leaf, slot, old_size + data_size);
4271− btrfs_mark_buffer_dirty(leaf);
4139+ btrfs_mark_buffer_dirty(trans, leaf);
42724140
42734141 if (btrfs_leaf_free_space(leaf) < 0) {
42744142 btrfs_print_leaf(leaf);
@@ -4279,6 +4147,7 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) · 4 unmodified lines
42794147 /*
42804148 * Make space in the node before inserting one or more items.
42814149 *
4150+ * @trans: transaction handle
42824151 * @root: root we are inserting items to
42834152 * @path: points to the leaf/slot where we are going to insert new items
42844153 * @batch: information about the batch of items to insert
@@ -4286,7 +4155,8 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) · 1 unmodified line
42864155 * Main purpose is to save stack depth by doing the bulk of the work in a
42874156 * function that doesn't call btrfs_search_slot
42884157 */
4289−static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4158+static void setup_items_for_insert(struct btrfs_trans_handle *trans,
4159+ struct btrfs_root *root, struct btrfs_path *path,
42904160 const struct btrfs_item_batch *batch)
42914161 {
42924162 struct btrfs_fs_info *fs_info = root->fs_info;
@@ -4306,7 +4176,7 @@ static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *p · 13 unmodified lines
43064176 */
43074177 if (path->slots[0] == 0) {
43084178 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]);
4309− fixup_low_keys(path, &disk_key, 1);
4179+ fixup_low_keys(trans, path, &disk_key, 1);
43104180 }
43114181 btrfs_unlock_up_safe(path, 1);
43124182
@@ -4365,7 +4235,7 @@ static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *p · 52 unmodified lines
43654235 }
43664236
43674237 btrfs_set_header_nritems(leaf, nritems + batch->nr);
4368− btrfs_mark_buffer_dirty(leaf);
4238+ btrfs_mark_buffer_dirty(trans, leaf);
43694239
43704240 if (btrfs_leaf_free_space(leaf) < 0) {
43714241 btrfs_print_leaf(leaf);
@@ -4376,12 +4246,14 @@ static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *p · 4 unmodified lines
43764246 /*
43774247 * Insert a new item into a leaf.
43784248 *
4249+ * @trans: Transaction handle.
43794250 * @root: The root of the btree.
43804251 * @path: A path pointing to the target leaf and slot.
43814252 * @key: The key of the new item.
43824253 * @data_size: The size of the data associated with the new key.
43834254 */
4384−void btrfs_setup_item_for_insert(struct btrfs_root *root,
4255+void btrfs_setup_item_for_insert(struct btrfs_trans_handle *trans,
4256+ struct btrfs_root *root,
43854257 struct btrfs_path *path,
43864258 const struct btrfs_key *key,
43874259 u32 data_size)
@@ -4393,7 +4265,7 @@ void btrfs_setup_item_for_insert(struct btrfs_root *root, · 5 unmodified lines
43934265 batch.total_data_size = data_size;
43944266 batch.nr = 1;
43954267
4396− setup_items_for_insert(root, path, &batch);
4268+ setup_items_for_insert(trans, root, path, &batch);
43974269 }
43984270
43994271 /*
@@ -4419,7 +4291,7 @@ int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, · 19 unmodified lines
44194291 slot = path->slots[0];
44204292 BUG_ON(slot < 0);
44214293
4422− setup_items_for_insert(root, path, batch);
4294+ setup_items_for_insert(trans, root, path, batch);
44234295 return 0;
44244296 }
44254297
@@ -4444,7 +4316,7 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 18 unmodified lines
44444316 leaf = path->nodes[0];
44454317 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
44464318 write_extent_buffer(leaf, data, ptr, data_size);
4447− btrfs_mark_buffer_dirty(leaf);
4319+ btrfs_mark_buffer_dirty(trans, leaf);
44484320 }
44494321 btrfs_free_path(path);
44504322 return ret;
@@ -4475,7 +4347,7 @@ int btrfs_duplicate_item(struct btrfs_trans_handle *trans, · 24 unmodified lines
44754347 return ret;
44764348
44774349 path->slots[0]++;
4478− btrfs_setup_item_for_insert(root, path, new_key, item_size);
4350+ btrfs_setup_item_for_insert(trans, root, path, new_key, item_size);
44794351 leaf = path->nodes[0];
44804352 memcpy_extent_buffer(leaf,
44814353 btrfs_item_ptr_offset(leaf, path->slots[0]),
@@ -4533,9 +4405,9 @@ int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 51 unmodified lines
45334405 struct btrfs_disk_key disk_key;
45344406
45354407 btrfs_node_key(parent, &disk_key, 0);
4536− fixup_low_keys(path, &disk_key, level + 1);
4408+ fixup_low_keys(trans, path, &disk_key, level + 1);
45374409 }
4538− btrfs_mark_buffer_dirty(parent);
4410+ btrfs_mark_buffer_dirty(trans, parent);
45394411 return 0;
45404412 }
45414413
@@ -4567,7 +4439,7 @@ static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, · 25 unmodified lines
45674439 */
45684440 btrfs_unlock_up_safe(path, 0);
45694441
4570− root_sub_used(root, leaf->len);
4442+ root_sub_used_bytes(root);
45714443
45724444 atomic_inc(&leaf->refs);
45734445 btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1);
@@ -4632,7 +4504,7 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 58 unmodified lines
46324504 struct btrfs_disk_key disk_key;
46334505
46344506 btrfs_item_key(leaf, &disk_key, 0);
4635− fixup_low_keys(path, &disk_key, 1);
4507+ fixup_low_keys(trans, path, &disk_key, 1);
46364508 }
46374509
46384510 /*
@@ -4697,11 +4569,11 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 58 unmodified lines
46974569 * dirtied this buffer
46984570 */
46994571 if (path->nodes[0] == leaf)
4700− btrfs_mark_buffer_dirty(leaf);
4572+ btrfs_mark_buffer_dirty(trans, leaf);
47014573 free_extent_buffer(leaf);
47024574 }
47034575 } else {
4704− btrfs_mark_buffer_dirty(leaf);
4576+ btrfs_mark_buffer_dirty(trans, leaf);
47054577 }
47064578 }
47074579 return ret;
fs/btrfs/ctree.h+81−61
@@ -6,37 +6,10 @@ · 5 unmodified lines
66 #ifndef BTRFS_CTREE_H
77 #define BTRFS_CTREE_H
88
9−#include <linux/mm.h>
10−#include <linux/sched/signal.h>
11−#include <linux/highmem.h>
12−#include <linux/fs.h>
13−#include <linux/rwsem.h>
14−#include <linux/semaphore.h>
15−#include <linux/completion.h>
16−#include <linux/backing-dev.h>
17−#include <linux/wait.h>
18−#include <linux/slab.h>
19−#include <trace/events/btrfs.h>
20−#include <asm/unaligned.h>
219 #include <linux/pagemap.h>
22−#include <linux/btrfs.h>
23−#include <linux/btrfs_tree.h>
24−#include <linux/workqueue.h>
25−#include <linux/security.h>
26−#include <linux/sizes.h>
27−#include <linux/dynamic_debug.h>
28−#include <linux/refcount.h>
29−#include <linux/crc32c.h>
30−#include <linux/iomap.h>
31−#include <linux/fscrypt.h>
32−#include "extent-io-tree.h"
33−#include "extent_io.h"
34−#include "extent_map.h"
35−#include "async-thread.h"
36−#include "block-rsv.h"
3710 #include "locking.h"
38−#include "misc.h"
3911 #include "fs.h"
12+#include "accessors.h"
4013
4114 struct btrfs_trans_handle;
4215 struct btrfs_transaction;
@@ -218,10 +191,22 @@ struct btrfs_root { · 175 unmodified lines
218191 atomic_t log_commit[2];
219192 /* Used only for log trees of subvolumes, not for the log root tree */
220193 atomic_t log_batch;
194+ /*
195+ * Protected by the 'log_mutex' lock but can be read without holding
196+ * that lock to avoid unnecessary lock contention, in which case it
197+ * should be read using btrfs_get_root_log_transid() except if it's a
198+ * log tree in which case it can be directly accessed. Updates to this
199+ * field should always use btrfs_set_root_log_transid(), except for log
200+ * trees where the field can be updated directly.
201+ */
221202 int log_transid;
222203 /* No matter the commit succeeds or not*/
223204 int log_transid_committed;
224− /* Just be updated when the commit succeeds. */
205+ /*
206+ * Just be updated when the commit succeeds. Use
207+ * btrfs_get_root_last_log_commit() and btrfs_set_root_last_log_commit()
208+ * to access this field.
209+ */
225210 int last_log_commit;
226211 pid_t log_start_pid;
227212
@@ -326,6 +311,9 @@ struct btrfs_root { · 98 unmodified lines
326311 /* Used only by log trees, when logging csum items */
327312 struct extent_io_tree log_csum_range;
328313
314+ /* Used in simple quotas, track root during relocation. */
315+ u64 relocation_src_root;
316+
329317 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
330318 u64 alloc_bytenr;
331319 #endif
@@ -352,6 +340,26 @@ static inline u64 btrfs_root_id(const struct btrfs_root *root) · 20 unmodified lines
352340 return root->root_key.objectid;
353341 }
354342
343+static inline int btrfs_get_root_log_transid(const struct btrfs_root *root)
344+{
345+ return READ_ONCE(root->log_transid);
346+}
347+
348+static inline void btrfs_set_root_log_transid(struct btrfs_root *root, int log_transid)
349+{
350+ WRITE_ONCE(root->log_transid, log_transid);
351+}
352+
353+static inline int btrfs_get_root_last_log_commit(const struct btrfs_root *root)
354+{
355+ return READ_ONCE(root->last_log_commit);
356+}
357+
358+static inline void btrfs_set_root_last_log_commit(struct btrfs_root *root, int commit_id)
359+{
360+ WRITE_ONCE(root->last_log_commit, commit_id);
361+}
362+
355363 /*
356364 * Structure that conveys information about an extent that is going to replace
357365 * all the extents in a file range.
@@ -470,30 +478,6 @@ static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info) · 112 unmodified lines
470478 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
471479 ((bytes) >> (fs_info)->sectorsize_bits)
472480
473−static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
474−{
475− return crc32c(crc, address, length);
476−}
477−
478−static inline void btrfs_crc32c_final(u32 crc, u8 *result)
479−{
480− put_unaligned_le32(~crc, result);
481−}
482−
483−static inline u64 btrfs_name_hash(const char *name, int len)
484−{
485− return crc32c((u32)~1, name, len);
486−}
487−
488−/*
489− * Figure the key offset of an extended inode ref
490− */
491−static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
492− int len)
493−{
494− return (u64) crc32c(parent_objectid, name, len);
495−}
496−
497481 static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
498482 {
499483 return mapping_gfp_constraint(mapping, ~__GFP_FS);
@@ -513,12 +497,42 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 13 unmodified lines
513497 const struct btrfs_key *key, int *slot);
514498
515499 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
500+
501+#ifdef __LITTLE_ENDIAN
502+
503+/*
504+ * Compare two keys, on little-endian the disk order is same as CPU order and
505+ * we can avoid the conversion.
506+ */
507+static inline int btrfs_comp_keys(const struct btrfs_disk_key *disk_key,
508+ const struct btrfs_key *k2)
509+{
510+ const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
511+
512+ return btrfs_comp_cpu_keys(k1, k2);
513+}
514+
515+#else
516+
517+/* Compare two keys in a memcmp fashion. */
518+static inline int btrfs_comp_keys(const struct btrfs_disk_key *disk,
519+ const struct btrfs_key *k2)
520+{
521+ struct btrfs_key k1;
522+
523+ btrfs_disk_key_to_cpu(&k1, disk);
524+
525+ return btrfs_comp_cpu_keys(&k1, k2);
526+}
527+
528+#endif
529+
516530 int btrfs_previous_item(struct btrfs_root *root,
517531 struct btrfs_path *path, u64 min_objectid,
518532 int type);
519533 int btrfs_previous_extent_item(struct btrfs_root *root,
520534 struct btrfs_path *path, u64 min_objectid);
521−void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
535+void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
522536 struct btrfs_path *path,
523537 const struct btrfs_key *new_key);
524538 struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
@@ -536,6 +550,13 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans, · 11 unmodified lines
536550 struct extent_buffer *parent, int parent_slot,
537551 struct extent_buffer **cow_ret,
538552 enum btrfs_lock_nesting nest);
553+int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
554+ struct btrfs_root *root,
555+ struct extent_buffer *buf,
556+ struct extent_buffer *parent, int parent_slot,
557+ struct extent_buffer **cow_ret,
558+ u64 search_start, u64 empty_size,
559+ enum btrfs_lock_nesting nest);
539560 int btrfs_copy_root(struct btrfs_trans_handle *trans,
540561 struct btrfs_root *root,
541562 struct extent_buffer *buf,
@@ -545,8 +566,10 @@ int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans, · 3 unmodified lines
545566 struct extent_buffer *buf);
546567 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
547568 struct btrfs_path *path, int level, int slot);
548−void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
549−void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
569+void btrfs_extend_item(struct btrfs_trans_handle *trans,
570+ struct btrfs_path *path, u32 data_size);
571+void btrfs_truncate_item(struct btrfs_trans_handle *trans,
572+ struct btrfs_path *path, u32 new_size, int from_end);
550573 int btrfs_split_item(struct btrfs_trans_handle *trans,
551574 struct btrfs_root *root,
552575 struct btrfs_path *path,
@@ -567,10 +590,6 @@ int btrfs_search_slot_for_read(struct btrfs_root *root, · 14 unmodified lines
567590 const struct btrfs_key *key,
568591 struct btrfs_path *p, int find_higher,
569592 int return_any);
570−int btrfs_realloc_node(struct btrfs_trans_handle *trans,
571− struct btrfs_root *root, struct extent_buffer *parent,
572− int start_slot, u64 *last_ret,
573− struct btrfs_key *progress);
574593 void btrfs_release_path(struct btrfs_path *p);
575594 struct btrfs_path *btrfs_alloc_path(void);
576595 void btrfs_free_path(struct btrfs_path *p);
@@ -610,7 +629,8 @@ struct btrfs_item_batch { · 33 unmodified lines
610629 int nr;
611630 };
612631
613−void btrfs_setup_item_for_insert(struct btrfs_root *root,
632+void btrfs_setup_item_for_insert(struct btrfs_trans_handle *trans,
633+ struct btrfs_root *root,
614634 struct btrfs_path *path,
615635 const struct btrfs_key *key,
616636 u32 data_size);
fs/btrfs/defrag.c+148−4
@@ -337,14 +337,119 @@ int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info) · 336 unmodified lines
337337 return 0;
338338 }
339339
340+/*
341+ * Check if two blocks addresses are close, used by defrag.
342+ */
343+static bool close_blocks(u64 blocknr, u64 other, u32 blocksize)
344+{
345+ if (blocknr < other && other - (blocknr + blocksize) < SZ_32K)
346+ return true;
347+ if (blocknr > other && blocknr - (other + blocksize) < SZ_32K)
348+ return true;
349+ return false;
350+}
351+
352+/*
353+ * Go through all the leaves pointed to by a node and reallocate them so that
354+ * disk order is close to key order.
355+ */
356+static int btrfs_realloc_node(struct btrfs_trans_handle *trans,
357+ struct btrfs_root *root,
358+ struct extent_buffer *parent,
359+ int start_slot, u64 *last_ret,
360+ struct btrfs_key *progress)
361+{
362+ struct btrfs_fs_info *fs_info = root->fs_info;
363+ const u32 blocksize = fs_info->nodesize;
364+ const int end_slot = btrfs_header_nritems(parent) - 1;
365+ u64 search_start = *last_ret;
366+ u64 last_block = 0;
367+ int ret = 0;
368+ bool progress_passed = false;
369+
370+ /*
371+ * COWing must happen through a running transaction, which always
372+ * matches the current fs generation (it's a transaction with a state
373+ * less than TRANS_STATE_UNBLOCKED). If it doesn't, then turn the fs
374+ * into error state to prevent the commit of any transaction.
375+ */
376+ if (unlikely(trans->transaction != fs_info->running_transaction ||
377+ trans->transid != fs_info->generation)) {
378+ btrfs_abort_transaction(trans, -EUCLEAN);
379+ btrfs_crit(fs_info,
380+"unexpected transaction when attempting to reallocate parent %llu for root %llu, transaction %llu running transaction %llu fs generation %llu",
381+ parent->start, btrfs_root_id(root), trans->transid,
382+ fs_info->running_transaction->transid,
383+ fs_info->generation);
384+ return -EUCLEAN;
385+ }
386+
387+ if (btrfs_header_nritems(parent) <= 1)
388+ return 0;
389+
390+ for (int i = start_slot; i <= end_slot; i++) {
391+ struct extent_buffer *cur;
392+ struct btrfs_disk_key disk_key;
393+ u64 blocknr;
394+ u64 other;
395+ bool close = true;
396+
397+ btrfs_node_key(parent, &disk_key, i);
398+ if (!progress_passed && btrfs_comp_keys(&disk_key, progress) < 0)
399+ continue;
400+
401+ progress_passed = true;
402+ blocknr = btrfs_node_blockptr(parent, i);
403+ if (last_block == 0)
404+ last_block = blocknr;
405+
406+ if (i > 0) {
407+ other = btrfs_node_blockptr(parent, i - 1);
408+ close = close_blocks(blocknr, other, blocksize);
409+ }
410+ if (!close && i < end_slot) {
411+ other = btrfs_node_blockptr(parent, i + 1);
412+ close = close_blocks(blocknr, other, blocksize);
413+ }
414+ if (close) {
415+ last_block = blocknr;
416+ continue;
417+ }
418+
419+ cur = btrfs_read_node_slot(parent, i);
420+ if (IS_ERR(cur))
421+ return PTR_ERR(cur);
422+ if (search_start == 0)
423+ search_start = last_block;
424+
425+ btrfs_tree_lock(cur);
426+ ret = btrfs_force_cow_block(trans, root, cur, parent, i,
427+ &cur, search_start,
428+ min(16 * blocksize,
429+ (end_slot - i) * blocksize),
430+ BTRFS_NESTING_COW);
431+ if (ret) {
432+ btrfs_tree_unlock(cur);
433+ free_extent_buffer(cur);
434+ break;
435+ }
436+ search_start = cur->start;
437+ last_block = cur->start;
438+ *last_ret = search_start;
439+ btrfs_tree_unlock(cur);
440+ free_extent_buffer(cur);
441+ }
442+ return ret;
443+}
444+
340445 /*
341446 * Defrag all the leaves in a given btree.
342447 * Read all the leaves and try to get key order to
343448 * better reflect disk order
344449 */
345450
346−int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
347− struct btrfs_root *root)
451+static int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
452+ struct btrfs_root *root)
348453 {
349454 struct btrfs_path *path = NULL;
350455 struct btrfs_key key;
@@ -460,6 +565,45 @@ done: · 109 unmodified lines
460565 return ret;
461566 }
462567
568+/*
569+ * Defrag a given btree. Every leaf in the btree is read and defragmented.
570+ */
571+int btrfs_defrag_root(struct btrfs_root *root)
572+{
573+ struct btrfs_fs_info *fs_info = root->fs_info;
574+ int ret;
575+
576+ if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
577+ return 0;
578+
579+ while (1) {
580+ struct btrfs_trans_handle *trans;
581+
582+ trans = btrfs_start_transaction(root, 0);
583+ if (IS_ERR(trans)) {
584+ ret = PTR_ERR(trans);
585+ break;
586+ }
587+
588+ ret = btrfs_defrag_leaves(trans, root);
589+
590+ btrfs_end_transaction(trans);
591+ btrfs_btree_balance_dirty(fs_info);
592+ cond_resched();
593+
594+ if (btrfs_fs_closing(fs_info) || ret != -EAGAIN)
595+ break;
596+
597+ if (btrfs_defrag_cancelled(fs_info)) {
598+ btrfs_debug(fs_info, "defrag_root cancelled");
599+ ret = -EAGAIN;
600+ break;
601+ }
602+ }
603+ clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
604+ return ret;
605+}
606+
463607 /*
464608 * Defrag specific helper to get an extent map.
465609 *
@@ -891,8 +1035,8 @@ static int defrag_collect_targets(struct btrfs_inode *inode, · 425 unmodified lines
8911035 * very likely resulting in a larger extent after writeback is
8921036 * triggered (except in a case of free space fragmentation).
8931037 */
894− if (test_range_bit(&inode->io_tree, cur, cur + range_len - 1,
895− EXTENT_DELALLOC, 0, NULL))
1038+ if (test_range_bit_exists(&inode->io_tree, cur, cur + range_len - 1,
1039+ EXTENT_DELALLOC))
8961040 goto next;
8971041
8981042 /*
fs/btrfs/defrag.h+1−1
@@ -12,7 +12,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans, · 11 unmodified lines
1212 struct btrfs_inode *inode, u32 extent_thresh);
1313 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
1414 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
15−int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, struct btrfs_root *root);
15+int btrfs_defrag_root(struct btrfs_root *root);
1616
1717 static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
1818 {
fs/btrfs/delalloc-space.c+2−4
@@ -322,9 +322,6 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes, · 321 unmodified lines
322322 } else {
323323 if (current->journal_info)
324324 flush = BTRFS_RESERVE_FLUSH_LIMIT;
325−
326− if (btrfs_transaction_in_commit(fs_info))
327− schedule_timeout(1);
328325 }
329326
330327 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
@@ -346,7 +343,8 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes, · 15 unmodified lines
346343 noflush);
347344 if (ret)
348345 return ret;
349− ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, meta_reserve, flush);
346+ ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
347+ meta_reserve, flush);
350348 if (ret) {
351349 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
352350 return ret;
fs/btrfs/delayed-inode.c+11−16
@@ -328,7 +328,8 @@ static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u16 data_len, · 327 unmodified lines
328328 }
329329
330330 /*
331− * __btrfs_lookup_delayed_item - look up the delayed item by key
331+ * Look up the delayed item by key.
332+ *
332333 * @delayed_node: pointer to the delayed node
333334 * @index: the dir index value to lookup (offset of a dir index key)
334335 *
@@ -517,7 +518,7 @@ static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, · 182 unmodified lines
517518 /*
518519 * For insertions we track reserved metadata space by accounting
519520 * for the number of leaves that will be used, based on the delayed
520− * node's index_items_size field.
521+ * node's curr_index_batch_size and index_item_leaves fields.
521522 */
522523 if (item->type == BTRFS_DELAYED_DELETION_ITEM)
523524 item->bytes_reserved = num_bytes;
@@ -1030,7 +1031,7 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, · 506 unmodified lines
10301031 struct btrfs_inode_item);
10311032 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
10321033 sizeof(struct btrfs_inode_item));
1033− btrfs_mark_buffer_dirty(leaf);
1034+ btrfs_mark_buffer_dirty(trans, leaf);
10341035
10351036 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
10361037 goto out;
@@ -1378,8 +1379,7 @@ static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, · 341 unmodified lines
13781379 return -ENOMEM;
13791380
13801381 async_work->delayed_root = delayed_root;
1381− btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL,
1382− NULL);
1382+ btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL);
13831383 async_work->nr = nr;
13841384
13851385 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
@@ -1760,8 +1760,7 @@ int btrfs_should_delete_dir_index(struct list_head *del_list, · 374 unmodified lines
17601760 }
17611761
17621762 /*
1763− * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1764− *
1763+ * Read dir info stored in the delayed tree.
17651764 */
17661765 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
17671766 struct list_head *ins_list)
@@ -1848,10 +1847,8 @@ static void fill_stack_inode_item(struct btrfs_trans_handle *trans, · 80 unmodified lines
18481847 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
18491848 inode_get_ctime_nsec(inode));
18501849
1851− btrfs_set_stack_timespec_sec(&inode_item->otime,
1852− BTRFS_I(inode)->i_otime.tv_sec);
1853− btrfs_set_stack_timespec_nsec(&inode_item->otime,
1854− BTRFS_I(inode)->i_otime.tv_nsec);
1850+ btrfs_set_stack_timespec_sec(&inode_item->otime, BTRFS_I(inode)->i_otime_sec);
1851+ btrfs_set_stack_timespec_nsec(&inode_item->otime, BTRFS_I(inode)->i_otime_nsec);
18551852 }
18561853
18571854 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
@@ -1900,10 +1897,8 @@ int btrfs_fill_inode(struct inode *inode, u32 *rdev) · 42 unmodified lines
19001897 inode_set_ctime(inode, btrfs_stack_timespec_sec(&inode_item->ctime),
19011898 btrfs_stack_timespec_nsec(&inode_item->ctime));
19021899
1903− BTRFS_I(inode)->i_otime.tv_sec =
1904− btrfs_stack_timespec_sec(&inode_item->otime);
1905− BTRFS_I(inode)->i_otime.tv_nsec =
1906− btrfs_stack_timespec_nsec(&inode_item->otime);
1900+ BTRFS_I(inode)->i_otime_sec = btrfs_stack_timespec_sec(&inode_item->otime);
1901+ BTRFS_I(inode)->i_otime_nsec = btrfs_stack_timespec_nsec(&inode_item->otime);
19071902
19081903 inode->i_generation = BTRFS_I(inode)->generation;
19091904 BTRFS_I(inode)->index_cnt = (u64)-1;
@@ -1914,9 +1909,9 @@ int btrfs_fill_inode(struct inode *inode, u32 *rdev) · 4 unmodified lines
19141909 }
19151910
19161911 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1917− struct btrfs_root *root,
19181912 struct btrfs_inode *inode)
19191913 {
1914+ struct btrfs_root *root = inode->root;
19201915 struct btrfs_delayed_node *delayed_node;
19211916 int ret = 0;
19221917
fs/btrfs/delayed-inode.h+0−1
@@ -135,7 +135,6 @@ int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode); · 134 unmodified lines
135135
136136
137137 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
138− struct btrfs_root *root,
139138 struct btrfs_inode *inode);
140139 int btrfs_fill_inode(struct inode *inode, u32 *rdev);
141140 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);
fs/btrfs/delayed-ref.c+158−41
@@ -57,16 +57,20 @@ bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info) · 56 unmodified lines
5757 * Release a ref head's reservation.
5858 *
5959 * @fs_info: the filesystem
60− * @nr: number of items to drop
60+ * @nr_refs: number of delayed refs to drop
61+ * @nr_csums: number of csum items to drop
6162 *
6263 * Drops the delayed ref head's count from the delayed refs rsv and free any
6364 * excess reservation we had.
6465 */
65−void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr)
66+void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums)
6667 {
6768 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
68− const u64 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr);
69− u64 released = 0;
69+ u64 num_bytes;
70+ u64 released;
71+
72+ num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr_refs);
73+ num_bytes += btrfs_calc_delayed_ref_csum_bytes(fs_info, nr_csums);
7074
7175 released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
7276 if (released)
@@ -77,26 +81,118 @@ void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr) · 4 unmodified lines
7781 /*
7882 * Adjust the size of the delayed refs rsv.
7983 *
80− * This is to be called anytime we may have adjusted trans->delayed_ref_updates,
81− * it'll calculate the additional size and add it to the delayed_refs_rsv.
84+ * This is to be called anytime we may have adjusted trans->delayed_ref_updates
85+ * or trans->delayed_ref_csum_deletions, it'll calculate the additional size and
86+ * add it to the delayed_refs_rsv.
8287 */
8388 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
8489 {
8590 struct btrfs_fs_info *fs_info = trans->fs_info;
8691 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
92+ struct btrfs_block_rsv *local_rsv = &trans->delayed_rsv;
8793 u64 num_bytes;
94+ u64 reserved_bytes;
8895
89− if (!trans->delayed_ref_updates)
96+ num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, trans->delayed_ref_updates);
97+ num_bytes += btrfs_calc_delayed_ref_csum_bytes(fs_info,
98+ trans->delayed_ref_csum_deletions);
99+
100+ if (num_bytes == 0)
90101 return;
91102
92− num_bytes = btrfs_calc_delayed_ref_bytes(fs_info,
93− trans->delayed_ref_updates);
103+ /*
104+ * Try to take num_bytes from the transaction's local delayed reserve.
105+ * If not possible, try to take as much as it's available. If the local
106+ * reserve doesn't have enough reserved space, the delayed refs reserve
107+ * will be refilled next time btrfs_delayed_refs_rsv_refill() is called
108+ * by someone or if a transaction commit is triggered before that, the
109+ * global block reserve will be used. We want to minimize using the
110+ * global block reserve for cases we can account for in advance, to
111+ * avoid exhausting it and reach -ENOSPC during a transaction commit.
112+ */
113+ spin_lock(&local_rsv->lock);
114+ reserved_bytes = min(num_bytes, local_rsv->reserved);
115+ local_rsv->reserved -= reserved_bytes;
116+ local_rsv->full = (local_rsv->reserved >= local_rsv->size);
117+ spin_unlock(&local_rsv->lock);
94118
95119 spin_lock(&delayed_rsv->lock);
96120 delayed_rsv->size += num_bytes;
97− delayed_rsv->full = false;
121+ delayed_rsv->reserved += reserved_bytes;
122+ delayed_rsv->full = (delayed_rsv->reserved >= delayed_rsv->size);
98123 spin_unlock(&delayed_rsv->lock);
99124 trans->delayed_ref_updates = 0;
125+ trans->delayed_ref_csum_deletions = 0;
126+}
127+
128+/*
129+ * Adjust the size of the delayed refs block reserve for 1 block group item
130+ * insertion, used after allocating a block group.
131+ */
132+void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info)
133+{
134+ struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
135+
136+ spin_lock(&delayed_rsv->lock);
137+ /*
138+ * Inserting a block group item does not require changing the free space
139+ * tree, only the extent tree or the block group tree, so this is all we
140+ * need.
141+ */
142+ delayed_rsv->size += btrfs_calc_insert_metadata_size(fs_info, 1);
143+ delayed_rsv->full = false;
144+ spin_unlock(&delayed_rsv->lock);
145+}
146+
147+/*
148+ * Adjust the size of the delayed refs block reserve to release space for 1
149+ * block group item insertion.
150+ */
151+void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info)
152+{
153+ struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
154+ const u64 num_bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
155+ u64 released;
156+
157+ released = btrfs_block_rsv_release(fs_info, delayed_rsv, num_bytes, NULL);
158+ if (released > 0)
159+ trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
160+ 0, released, 0);
161+}
162+
163+/*
164+ * Adjust the size of the delayed refs block reserve for 1 block group item
165+ * update.
166+ */
167+void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info)
168+{
169+ struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
170+
171+ spin_lock(&delayed_rsv->lock);
172+ /*
173+ * Updating a block group item does not result in new nodes/leaves and
174+ * does not require changing the free space tree, only the extent tree
175+ * or the block group tree, so this is all we need.
176+ */
177+ delayed_rsv->size += btrfs_calc_metadata_size(fs_info, 1);
178+ delayed_rsv->full = false;
179+ spin_unlock(&delayed_rsv->lock);
180+}
181+
182+/*
183+ * Adjust the size of the delayed refs block reserve to release space for 1
184+ * block group item update.
185+ */
186+void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info)
187+{
188+ struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
189+ const u64 num_bytes = btrfs_calc_metadata_size(fs_info, 1);
190+ u64 released;
191+
192+ released = btrfs_block_rsv_release(fs_info, delayed_rsv, num_bytes, NULL);
193+ if (released > 0)
194+ trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
195+ 0, released, 0);
100196 }
101197
102198 /*
@@ -154,6 +250,7 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, · 51 unmodified lines
154250 enum btrfs_reserve_flush_enum flush)
155251 {
156252 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
253+ struct btrfs_space_info *space_info = block_rsv->space_info;
157254 u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);
158255 u64 num_bytes = 0;
159256 u64 refilled_bytes;
@@ -170,7 +267,7 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, · 10 unmodified lines
170267 if (!num_bytes)
171268 return 0;
172269
173− ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
270+ ret = btrfs_reserve_metadata_bytes(fs_info, space_info, num_bytes, flush);
174271 if (ret)
175272 return ret;
176273
@@ -199,8 +296,7 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, · 22 unmodified lines
199296 spin_unlock(&block_rsv->lock);
200297
201298 if (to_free > 0)
202− btrfs_space_info_free_bytes_may_use(fs_info, block_rsv->space_info,
203− to_free);
299+ btrfs_space_info_free_bytes_may_use(fs_info, space_info, to_free);
204300
205301 if (refilled_bytes > 0)
206302 trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", 0,
@@ -422,7 +518,8 @@ int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs, · 215 unmodified lines
422518 return 0;
423519 }
424520
425−static inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs,
521+static inline void drop_delayed_ref(struct btrfs_fs_info *fs_info,
522+ struct btrfs_delayed_ref_root *delayed_refs,
426523 struct btrfs_delayed_ref_head *head,
427524 struct btrfs_delayed_ref_node *ref)
428525 {
@@ -433,9 +530,11 @@ static inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs, · 4 unmodified lines
433530 list_del(&ref->add_list);
434531 btrfs_put_delayed_ref(ref);
435532 atomic_dec(&delayed_refs->num_entries);
533+ btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
436534 }
437535
438−static bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs,
536+static bool merge_ref(struct btrfs_fs_info *fs_info,
537+ struct btrfs_delayed_ref_root *delayed_refs,
439538 struct btrfs_delayed_ref_head *head,
440539 struct btrfs_delayed_ref_node *ref,
441540 u64 seq)
@@ -464,10 +563,10 @@ static bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs, · 22 unmodified lines
464563 mod = -next->ref_mod;
465564 }
466565
467− drop_delayed_ref(delayed_refs, head, next);
566+ drop_delayed_ref(fs_info, delayed_refs, head, next);
468567 ref->ref_mod += mod;
469568 if (ref->ref_mod == 0) {
470− drop_delayed_ref(delayed_refs, head, ref);
569+ drop_delayed_ref(fs_info, delayed_refs, head, ref);
471570 done = true;
472571 } else {
473572 /*
@@ -505,7 +604,7 @@ again: · 31 unmodified lines
505604 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
506605 if (seq && ref->seq >= seq)
507606 continue;
508− if (merge_ref(delayed_refs, head, ref, seq))
607+ if (merge_ref(fs_info, delayed_refs, head, ref, seq))
509608 goto again;
510609 }
511610 }
@@ -584,10 +683,11 @@ void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs, · 72 unmodified lines
584683 * Return true if the ref was merged into an existing one (and therefore can be
585684 * freed by the caller).
586685 */
587−static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root,
686+static bool insert_delayed_ref(struct btrfs_trans_handle *trans,
588687 struct btrfs_delayed_ref_head *href,
589688 struct btrfs_delayed_ref_node *ref)
590689 {
690+ struct btrfs_delayed_ref_root *root = &trans->transaction->delayed_refs;
591691 struct btrfs_delayed_ref_node *exist;
592692 int mod;
593693
@@ -598,6 +698,7 @@ static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root, · 4 unmodified lines
598698 list_add_tail(&ref->add_list, &href->ref_add_list);
599699 atomic_inc(&root->num_entries);
600700 spin_unlock(&href->lock);
701+ trans->delayed_ref_updates++;
601702 return false;
602703 }
603704
@@ -626,7 +727,7 @@ static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root, · 22 unmodified lines
626727
627728 /* remove existing tail if its ref_mod is zero */
628729 if (exist->ref_mod == 0)
629− drop_delayed_ref(root, href, exist);
730+ drop_delayed_ref(trans->fs_info, root, href, exist);
630731 spin_unlock(&href->lock);
631732 return true;
632733 }
@@ -647,6 +748,15 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 14 unmodified lines
647748 BUG_ON(existing->is_data != update->is_data);
648749
649750 spin_lock(&existing->lock);
751+
752+ /*
753+ * When freeing an extent, we may not know the owning root when we
754+ * first create the head_ref. However, some deref before the last deref
755+ * will know it, so we just need to update the head_ref accordingly.
756+ */
757+ if (!existing->owning_root)
758+ existing->owning_root = update->owning_root;
759+
650760 if (update->must_insert_reserved) {
651761 /* if the extent was freed and then
652762 * reallocated before the delayed ref
@@ -656,6 +766,7 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 3 unmodified lines
656766 * Set it again here
657767 */
658768 existing->must_insert_reserved = update->must_insert_reserved;
769+ existing->owning_root = update->owning_root;
659770
660771 /*
661772 * update the num_bytes so we make sure the accounting
@@ -695,6 +806,8 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 33 unmodified lines
695806 /*
696807 * If we are going to from a positive ref mod to a negative or vice
697808 * versa we need to make sure to adjust pending_csums accordingly.
809+ * We reserve bytes for csum deletion when adding or updating a ref head
810+ * see add_delayed_ref_head() for more details.
698811 */
699812 if (existing->is_data) {
700813 u64 csum_leaves =
@@ -703,11 +816,11 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 2 unmodified lines
703816
704817 if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
705818 delayed_refs->pending_csums -= existing->num_bytes;
706− btrfs_delayed_refs_rsv_release(fs_info, csum_leaves);
819+ btrfs_delayed_refs_rsv_release(fs_info, 0, csum_leaves);
707820 }
708821 if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
709822 delayed_refs->pending_csums += existing->num_bytes;
710− trans->delayed_ref_updates += csum_leaves;
823+ trans->delayed_ref_csum_deletions += csum_leaves;
711824 }
712825 }
713826
@@ -718,7 +831,7 @@ static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref, · 4 unmodified lines
718831 struct btrfs_qgroup_extent_record *qrecord,
719832 u64 bytenr, u64 num_bytes, u64 ref_root,
720833 u64 reserved, int action, bool is_data,
721− bool is_system)
834+ bool is_system, u64 owning_root)
722835 {
723836 int count_mod = 1;
724837 bool must_insert_reserved = false;
@@ -758,7 +871,9 @@ static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref, · 33 unmodified lines
758871 head_ref->bytenr = bytenr;
759872 head_ref->num_bytes = num_bytes;
760873 head_ref->ref_mod = count_mod;
874+ head_ref->reserved_bytes = reserved;
761875 head_ref->must_insert_reserved = must_insert_reserved;
876+ head_ref->owning_root = owning_root;
762877 head_ref->is_data = is_data;
763878 head_ref->is_system = is_system;
764879 head_ref->ref_tree = RB_ROOT_CACHED;
@@ -819,16 +934,21 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans, · 54 unmodified lines
819934 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
820935 head_ref = existing;
821936 } else {
937+ /*
938+ * We reserve the amount of bytes needed to delete csums when
939+ * adding the ref head and not when adding individual drop refs
940+ * since the csum items are deleted only after running the last
941+ * delayed drop ref (the data extent's ref count drops to 0).
942+ */
822943 if (head_ref->is_data && head_ref->ref_mod < 0) {
823944 delayed_refs->pending_csums += head_ref->num_bytes;
824− trans->delayed_ref_updates +=
945+ trans->delayed_ref_csum_deletions +=
825946 btrfs_csum_bytes_to_leaves(trans->fs_info,
826947 head_ref->num_bytes);
827948 }
828949 delayed_refs->num_heads++;
829950 delayed_refs->num_heads_ready++;
830951 atomic_inc(&delayed_refs->num_entries);
831− trans->delayed_ref_updates++;
832952 }
833953 if (qrecord_inserted_ret)
834954 *qrecord_inserted_ret = qrecord_inserted;
@@ -837,8 +957,7 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans, · 2 unmodified lines
837957 }
838958
839959 /*
840− * init_delayed_ref_common - Initialize the structure which represents a
841− * modification to a an extent.
960+ * Initialize the structure which represents a modification to a an extent.
842961 *
843962 * @fs_info: Internal to the mounted filesystem mount structure.
844963 *
@@ -909,7 +1028,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, · 64 unmodified lines
9091028 u64 parent = generic_ref->parent;
9101029 u8 ref_type;
9111030
912− is_system = (generic_ref->tree_ref.owning_root == BTRFS_CHUNK_TREE_OBJECTID);
1031+ is_system = (generic_ref->tree_ref.ref_root == BTRFS_CHUNK_TREE_OBJECTID);
9131032
9141033 ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
9151034 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
@@ -922,8 +1041,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
9221041 return -ENOMEM;
9231042 }
9241043
925− if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
926− !generic_ref->skip_qgroup) {
1044+ if (btrfs_qgroup_enabled(fs_info) && !generic_ref->skip_qgroup) {
9271045 record = kzalloc(sizeof(*record), GFP_NOFS);
9281046 if (!record) {
9291047 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
@@ -938,15 +1056,15 @@ int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, · 8 unmodified lines
9381056 ref_type = BTRFS_TREE_BLOCK_REF_KEY;
9391057
9401058 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
941− generic_ref->tree_ref.owning_root, action,
1059+ generic_ref->tree_ref.ref_root, action,
9421060 ref_type);
943− ref->root = generic_ref->tree_ref.owning_root;
1061+ ref->root = generic_ref->tree_ref.ref_root;
9441062 ref->parent = parent;
9451063 ref->level = level;
9461064
9471065 init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
948− generic_ref->tree_ref.owning_root, 0, action,
949− false, is_system);
1066+ generic_ref->tree_ref.ref_root, 0, action,
1067+ false, is_system, generic_ref->owning_root);
9501068 head_ref->extent_op = extent_op;
9511069
9521070 delayed_refs = &trans->transaction->delayed_refs;
@@ -959,7 +1077,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
9591077 head_ref = add_delayed_ref_head(trans, head_ref, record,
9601078 action, &qrecord_inserted);
9611079
962− merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);
1080+ merged = insert_delayed_ref(trans, head_ref, &ref->node);
9631081 spin_unlock(&delayed_refs->lock);
9641082
9651083 /*
@@ -998,7 +1116,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 32 unmodified lines
9981116 u64 bytenr = generic_ref->bytenr;
9991117 u64 num_bytes = generic_ref->len;
10001118 u64 parent = generic_ref->parent;
1001− u64 ref_root = generic_ref->data_ref.owning_root;
1119+ u64 ref_root = generic_ref->data_ref.ref_root;
10021120 u64 owner = generic_ref->data_ref.ino;
10031121 u64 offset = generic_ref->data_ref.offset;
10041122 u8 ref_type;
@@ -1026,8 +1144,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 21 unmodified lines
10261144 return -ENOMEM;
10271145 }
10281146
1029− if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
1030− !generic_ref->skip_qgroup) {
1147+ if (btrfs_qgroup_enabled(fs_info) && !generic_ref->skip_qgroup) {
10311148 record = kzalloc(sizeof(*record), GFP_NOFS);
10321149 if (!record) {
10331150 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
@@ -1038,7 +1155,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 4 unmodified lines
10381155 }
10391156
10401157 init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
1041− reserved, action, true, false);
1158+ reserved, action, true, false, generic_ref->owning_root);
10421159 head_ref->extent_op = NULL;
10431160
10441161 delayed_refs = &trans->transaction->delayed_refs;
@@ -1051,7 +1168,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
10511168 head_ref = add_delayed_ref_head(trans, head_ref, record,
10521169 action, &qrecord_inserted);
10531170
1054− merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);
1171+ merged = insert_delayed_ref(trans, head_ref, &ref->node);
10551172 spin_unlock(&delayed_refs->lock);
10561173
10571174 /*
@@ -1084,7 +1201,7 @@ int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans, · 26 unmodified lines
10841201 return -ENOMEM;
10851202
10861203 init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
1087− BTRFS_UPDATE_DELAYED_HEAD, false, false);
1204+ BTRFS_UPDATE_DELAYED_HEAD, false, false, 0);
10881205 head_ref->extent_op = extent_op;
10891206
10901207 delayed_refs = &trans->transaction->delayed_refs;
fs/btrfs/delayed-ref.h+53−17
@@ -9,10 +9,16 @@ · 8 unmodified lines
99 #include <linux/refcount.h>
1010
1111 /* these are the possible values of struct btrfs_delayed_ref_node->action */
12−#define BTRFS_ADD_DELAYED_REF 1 /* add one backref to the tree */
13−#define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */
14−#define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
15−#define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
12+enum btrfs_delayed_ref_action {
13+ /* Add one backref to the tree */
14+ BTRFS_ADD_DELAYED_REF = 1,
15+ /* Delete one backref from the tree */
16+ BTRFS_DROP_DELAYED_REF,
17+ /* Record a full extent allocation */
18+ BTRFS_ADD_DELAYED_EXTENT,
19+ /* Not changing ref count on head ref */
20+ BTRFS_UPDATE_DELAYED_HEAD,
21+} __packed;
1622
1723 struct btrfs_delayed_ref_node {
1824 struct rb_node ref_node;
@@ -104,6 +110,18 @@ struct btrfs_delayed_ref_head { · 85 unmodified lines
104110 */
105111 int ref_mod;
106112
113+ /*
114+ * The root that triggered the allocation when must_insert_reserved is
115+ * set to true.
116+ */
117+ u64 owning_root;
118+
119+ /*
120+ * Track reserved bytes when setting must_insert_reserved. On success
121+ * or cleanup, we will need to free the reservation.
122+ */
123+ u64 reserved_bytes;
124+
107125 /*
108126 * when a new extent is allocated, it is just reserved in memory
109127 * The actual extent isn't inserted into the extent allocation tree
@@ -117,6 +135,7 @@ struct btrfs_delayed_ref_head { · 7 unmodified lines
117135 * the free has happened.
118136 */
119137 bool must_insert_reserved;
138+
120139 bool is_data;
121140 bool is_system;
122141 bool processing;
@@ -183,13 +202,13 @@ enum btrfs_ref_type { · 60 unmodified lines
183202 BTRFS_REF_DATA,
184203 BTRFS_REF_METADATA,
185204 BTRFS_REF_LAST,
186−};
205+} __packed;
187206
188207 struct btrfs_data_ref {
189208 /* For EXTENT_DATA_REF */
190209
191− /* Original root this data extent belongs to */
192− u64 owning_root;
210+ /* Root which owns this data reference. */
211+ u64 ref_root;
193212
194213 /* Inode which refers to this data extent */
195214 u64 ino;
@@ -212,18 +231,18 @@ struct btrfs_tree_ref { · 16 unmodified lines
212231 int level;
213232
214233 /*
215− * Root which owns this tree block.
234+ * Root which owns this tree block reference.
216235 *
217236 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
218237 */
219− u64 owning_root;
238+ u64 ref_root;
220239
221240 /* For non-skinny metadata, no special member needed */
222241 };
223242
224243 struct btrfs_ref {
225244 enum btrfs_ref_type type;
226− int action;
245+ enum btrfs_delayed_ref_action action;
227246
228247 /*
229248 * Whether this extent should go through qgroup record.
@@ -239,6 +258,7 @@ struct btrfs_ref { · 9 unmodified lines
239258 #endif
240259 u64 bytenr;
241260 u64 len;
261+ u64 owning_root;
242262
243263 /* Bytenr of the parent tree block */
244264 u64 parent;
@@ -277,24 +297,37 @@ static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_in · 32 unmodified lines
277297 return num_bytes;
278298 }
279299
300+static inline u64 btrfs_calc_delayed_ref_csum_bytes(const struct btrfs_fs_info *fs_info,
301+ int num_csum_items)
302+{
303+ /*
304+ * Deleting csum items does not result in new nodes/leaves and does not
305+ * require changing the free space tree, only the csum tree, so this is
306+ * all we need.
307+ */
308+ return btrfs_calc_metadata_size(fs_info, num_csum_items);
309+}
310+
280311 static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
281− int action, u64 bytenr, u64 len, u64 parent)
312+ int action, u64 bytenr, u64 len,
313+ u64 parent, u64 owning_root)
282314 {
283315 generic_ref->action = action;
284316 generic_ref->bytenr = bytenr;
285317 generic_ref->len = len;
286318 generic_ref->parent = parent;
319+ generic_ref->owning_root = owning_root;
287320 }
288321
289−static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
290− int level, u64 root, u64 mod_root, bool skip_qgroup)
322+static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level,
323+ u64 root, u64 mod_root, bool skip_qgroup)
291324 {
292325 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
293326 /* If @real_root not set, use @root as fallback */
294327 generic_ref->real_root = mod_root ?: root;
295328 #endif
296329 generic_ref->tree_ref.level = level;
297− generic_ref->tree_ref.owning_root = root;
330+ generic_ref->tree_ref.ref_root = root;
298331 generic_ref->type = BTRFS_REF_METADATA;
299332 if (skip_qgroup || !(is_fstree(root) &&
300333 (!mod_root || is_fstree(mod_root))))
@@ -312,7 +345,7 @@ static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref, · 11 unmodified lines
312345 /* If @real_root not set, use @root as fallback */
313346 generic_ref->real_root = mod_root ?: ref_root;
314347 #endif
315− generic_ref->data_ref.owning_root = ref_root;
348+ generic_ref->data_ref.ref_root = ref_root;
316349 generic_ref->data_ref.ino = ino;
317350 generic_ref->data_ref.offset = offset;
318351 generic_ref->type = BTRFS_REF_DATA;
@@ -338,7 +371,6 @@ btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op) · 19 unmodified lines
338371
339372 static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
340373 {
341− WARN_ON(refcount_read(&ref->refs) == 0);
342374 if (refcount_dec_and_test(&ref->refs)) {
343375 WARN_ON(!RB_EMPTY_NODE(&ref->ref_node));
344376 switch (ref->type) {
@@ -402,8 +434,12 @@ struct btrfs_delayed_ref_head *btrfs_select_ref_head( · 57 unmodified lines
402434
403435 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
404436
405−void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
437+void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums);
406438 void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
439+void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
440+void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
441+void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
442+void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
407443 int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
408444 enum btrfs_reserve_flush_enum flush);
409445 void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
fs/btrfs/dev-replace.c+1−2
@@ -17,7 +17,6 @@ · 16 unmodified lines
1717 #include "print-tree.h"
1818 #include "volumes.h"
1919 #include "async-thread.h"
20−#include "check-integrity.h"
2120 #include "dev-replace.h"
2221 #include "sysfs.h"
2322 #include "zoned.h"
@@ -444,7 +443,7 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans) · 420 unmodified lines
444443 dev_replace->item_needs_writeback = 0;
445444 up_write(&dev_replace->rwsem);
446445
447− btrfs_mark_buffer_dirty(eb);
446+ btrfs_mark_buffer_dirty(trans, eb);
448447
449448 out:
450449 btrfs_free_path(path);
fs/btrfs/dir-item.c+4−4
@@ -38,7 +38,7 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle · 37 unmodified lines
3838 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
3939 if (di)
4040 return ERR_PTR(-EEXIST);
41− btrfs_extend_item(path, data_size);
41+ btrfs_extend_item(trans, path, data_size);
4242 } else if (ret < 0)
4343 return ERR_PTR(ret);
4444 WARN_ON(ret > 0);
@@ -93,7 +93,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, · 48 unmodified lines
9393
9494 write_extent_buffer(leaf, name, name_ptr, name_len);
9595 write_extent_buffer(leaf, data, data_ptr, data_len);
96− btrfs_mark_buffer_dirty(path->nodes[0]);
96+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
9797
9898 return ret;
9999 }
@@ -153,7 +153,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, · 53 unmodified lines
153153 name_ptr = (unsigned long)(dir_item + 1);
154154
155155 write_extent_buffer(leaf, name->name, name_ptr, name->len);
156− btrfs_mark_buffer_dirty(leaf);
156+ btrfs_mark_buffer_dirty(trans, leaf);
157157
158158 second_insert:
159159 /* FIXME, use some real flag for selecting the extra index */
@@ -439,7 +439,7 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, · 279 unmodified lines
439439 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
440440 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
441441 item_len - (ptr + sub_item_len - start));
442− btrfs_truncate_item(path, item_len - sub_item_len, 1);
442+ btrfs_truncate_item(trans, path, item_len - sub_item_len, 1);
443443 }
444444 return ret;
445445 }
fs/btrfs/dir-item.h+9−0
@@ -3,6 +3,10 @@ · 2 unmodified lines
33 #ifndef BTRFS_DIR_ITEM_H
44 #define BTRFS_DIR_ITEM_H
55
6+#include <linux/crc32c.h>
7+
8+struct fscrypt_str;
9+
610 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
711 const struct fscrypt_str *name);
812 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
@@ -39,4 +43,9 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info, · 30 unmodified lines
3943 const char *name,
4044 int name_len);
4145
46+static inline u64 btrfs_name_hash(const char *name, int len)
47+{
48+ return crc32c((u32)~1, name, len);
49+}
50+
4251 #endif
fs/btrfs/disk-io.c+55−87
@@ -29,7 +29,6 @@ · 28 unmodified lines
2929 #include "tree-log.h"
3030 #include "free-space-cache.h"
3131 #include "free-space-tree.h"
32−#include "check-integrity.h"
3332 #include "rcu-string.h"
3433 #include "dev-replace.h"
3534 #include "raid56.h"
@@ -245,6 +244,7 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio) · 209 unmodified lines
245244 struct extent_buffer *eb = bbio->private;
246245 struct btrfs_fs_info *fs_info = eb->fs_info;
247246 u64 found_start = btrfs_header_bytenr(eb);
247+ u64 last_trans;
248248 u8 result[BTRFS_CSUM_SIZE];
249249 int ret;
250250
@@ -282,12 +282,12 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio) · 31 unmodified lines
282282 * Also check the generation, the eb reached here must be newer than
283283 * last committed. Or something seriously wrong happened.
284284 */
285− if (unlikely(btrfs_header_generation(eb) <= fs_info->last_trans_committed)) {
285+ last_trans = btrfs_get_last_trans_committed(fs_info);
286+ if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
286287 ret = -EUCLEAN;
287288 btrfs_err(fs_info,
288289 "block=%llu bad generation, have %llu expect > %llu",
289− eb->start, btrfs_header_generation(eb),
290− fs_info->last_trans_committed);
290+ eb->start, btrfs_header_generation(eb), last_trans);
291291 goto error;
292292 }
293293 write_extent_buffer(eb, result, 0, fs_info->csum_size);
@@ -318,9 +318,10 @@ static bool check_tree_block_fsid(struct extent_buffer *eb) · 24 unmodified lines
318318 BTRFS_FSID_SIZE);
319319
320320 /*
321− * alloc_fs_devices() copies the fsid into metadata_uuid if the
322− * metadata_uuid is unset in the superblock, including for a seed device.
323− * So, we can use fs_devices->metadata_uuid.
321+ * alloc_fsid_devices() copies the fsid into fs_devices::metadata_uuid.
322+ * This is then overwritten by metadata_uuid if it is present in the
323+ * device_list_add(). The same true for a seed device as well. So use of
324+ * fs_devices::metadata_uuid is appropriate here.
324325 */
325326 if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
326327 return false;
@@ -675,9 +676,9 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, · 348 unmodified lines
675676 refcount_set(&root->refs, 1);
676677 atomic_set(&root->snapshot_force_cow, 0);
677678 atomic_set(&root->nr_swapfiles, 0);
678− root->log_transid = 0;
679+ btrfs_set_root_log_transid(root, 0);
679680 root->log_transid_committed = -1;
680− root->last_log_commit = 0;
681+ btrfs_set_root_last_log_commit(root, 0);
681682 root->anon_dev = 0;
682683 if (!dummy) {
683684 extent_io_tree_init(fs_info, &root->dirty_log_pages,
@@ -859,7 +860,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, · 175 unmodified lines
859860 root->root_key.offset = 0;
860861
861862 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
862− BTRFS_NESTING_NORMAL);
863+ 0, BTRFS_NESTING_NORMAL);
863864 if (IS_ERR(leaf)) {
864865 ret = PTR_ERR(leaf);
865866 leaf = NULL;
@@ -867,7 +868,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, · 1 unmodified line
867868 }
868869
869870 root->node = leaf;
870− btrfs_mark_buffer_dirty(leaf);
871+ btrfs_mark_buffer_dirty(trans, leaf);
871872
872873 root->commit_root = btrfs_root_node(root);
873874 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
@@ -936,13 +937,13 @@ int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, · 62 unmodified lines
936937 */
937938
938939 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
939− NULL, 0, 0, 0, BTRFS_NESTING_NORMAL);
940+ NULL, 0, 0, 0, 0, BTRFS_NESTING_NORMAL);
940941 if (IS_ERR(leaf))
941942 return PTR_ERR(leaf);
942943
943944 root->node = leaf;
944945
945− btrfs_mark_buffer_dirty(root->node);
946+ btrfs_mark_buffer_dirty(trans, root->node);
946947 btrfs_tree_unlock(root->node);
947948
948949 return 0;
@@ -1004,9 +1005,9 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans, · 55 unmodified lines
10041005
10051006 WARN_ON(root->log_root);
10061007 root->log_root = log_root;
1007− root->log_transid = 0;
1008+ btrfs_set_root_log_transid(root, 0);
10081009 root->log_transid_committed = -1;
1009− root->last_log_commit = 0;
1010+ btrfs_set_root_last_log_commit(root, 0);
10101011 return 0;
10111012 }
10121013
@@ -1179,6 +1180,8 @@ static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, · 166 unmodified lines
11791180 return btrfs_grab_root(fs_info->block_group_root);
11801181 case BTRFS_FREE_SPACE_TREE_OBJECTID:
11811182 return btrfs_grab_root(btrfs_global_root(fs_info, &key));
1183+ case BTRFS_RAID_STRIPE_TREE_OBJECTID:
1184+ return btrfs_grab_root(fs_info->stripe_root);
11821185 default:
11831186 return NULL;
11841187 }
@@ -1259,6 +1262,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) · 74 unmodified lines
12591262 btrfs_put_root(fs_info->fs_root);
12601263 btrfs_put_root(fs_info->data_reloc_root);
12611264 btrfs_put_root(fs_info->block_group_root);
1265+ btrfs_put_root(fs_info->stripe_root);
12621266 btrfs_check_leaked_roots(fs_info);
12631267 btrfs_extent_buffer_leak_debug_check(fs_info);
12641268 kfree(fs_info->super_copy);
@@ -1402,7 +1406,8 @@ struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, · 137 unmodified lines
14021406 }
14031407
14041408 /*
1405− * btrfs_get_fs_root_commit_root - return a root for the given objectid
1409+ * Return a root for the given objectid.
1410+ *
14061411 * @fs_info: the fs_info
14071412 * @objectid: the objectid we need to lookup
14081413 *
@@ -1699,11 +1704,11 @@ static void backup_super_roots(struct btrfs_fs_info *info) · 290 unmodified lines
16991704 }
17001705
17011706 /*
1702− * read_backup_root - Reads a backup root based on the passed priority. Prio 0
1703− * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
1707+ * Reads a backup root based on the passed priority. Prio 0 is the newest, prio
1708+ * 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
17041709 *
1705− * fs_info - filesystem whose backup roots need to be read
1706− * priority - priority of backup root required
1710+ * @fs_info: filesystem whose backup roots need to be read
1711+ * @priority: priority of backup root required
17071712 *
17081713 * Returns backup root index on success and -EINVAL otherwise.
17091714 */
@@ -1803,6 +1808,7 @@ static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) · 93 unmodified lines
18031808 free_root_extent_buffers(info->fs_root);
18041809 free_root_extent_buffers(info->data_reloc_root);
18051810 free_root_extent_buffers(info->block_group_root);
1811+ free_root_extent_buffers(info->stripe_root);
18061812 if (free_chunk_root)
18071813 free_root_extent_buffers(info->chunk_root);
18081814 }
@@ -2262,7 +2268,6 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info) · 453 unmodified lines
22622268 root = btrfs_read_tree_root(tree_root, &location);
22632269 if (!IS_ERR(root)) {
22642270 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2265− set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
22662271 fs_info->quota_root = root;
22672272 }
22682273
@@ -2279,6 +2284,20 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info) · 10 unmodified lines
22792284 fs_info->uuid_root = root;
22802285 }
22812286
2287+ if (btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE)) {
2288+ location.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
2289+ root = btrfs_read_tree_root(tree_root, &location);
2290+ if (IS_ERR(root)) {
2291+ if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
2292+ ret = PTR_ERR(root);
2293+ goto out;
2294+ }
2295+ } else {
2296+ set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2297+ fs_info->stripe_root = root;
2298+ }
2299+ }
2300+
22822301 return 0;
22832302 out:
22842303 btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
@@ -2381,7 +2400,8 @@ int btrfs_validate_super(struct btrfs_fs_info *fs_info, · 96 unmodified lines
23812400 ret = -EINVAL;
23822401 }
23832402
2384− if (memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
2403+ if (!fs_info->fs_devices->temp_fsid &&
2404+ memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
23852405 btrfs_err(fs_info,
23862406 "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
23872407 sb->fsid, fs_info->fs_devices->fsid);
@@ -2634,7 +2654,7 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) · 246 unmodified lines
26342654
26352655 /* All successful */
26362656 fs_info->generation = btrfs_header_generation(tree_root->node);
2637− fs_info->last_trans_committed = fs_info->generation;
2657+ btrfs_set_last_trans_committed(fs_info, fs_info->generation);
26382658 fs_info->last_reloc_trans = 0;
26392659
26402660 /* Always begin writing backup roots after the one being used */
@@ -2735,9 +2755,6 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) · 94 unmodified lines
27352755 spin_lock_init(&fs_info->ordered_root_lock);
27362756
27372757 btrfs_init_scrub(fs_info);
2738−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2739− fs_info->check_integrity_print_mask = 0;
2740−#endif
27412758 btrfs_init_balance(fs_info);
27422759 btrfs_init_async_reclaim_work(fs_info);
27432760
@@ -3157,7 +3174,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 413 unmodified lines
31573174 u32 nodesize;
31583175 u32 stripesize;
31593176 u64 generation;
3160− u64 features;
31613177 u16 csum_type;
31623178 struct btrfs_super_block *disk_super;
31633179 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
@@ -3239,15 +3255,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 75 unmodified lines
32393255
32403256 disk_super = fs_info->super_copy;
32413257
3242−
3243− features = btrfs_super_flags(disk_super);
3244− if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) {
3245− features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2;
3246− btrfs_set_super_flags(disk_super, features);
3247− btrfs_info(fs_info,
3248− "found metadata UUID change in progress flag, clearing");
3249− }
3250−
32513258 memcpy(fs_info->super_for_commit, fs_info->super_copy,
32523259 sizeof(*fs_info->super_for_commit));
32533260
@@ -3509,18 +3516,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 255 unmodified lines
35093516 "auto enabling async discard");
35103517 }
35113518
3512−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3513− if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
3514− ret = btrfsic_mount(fs_info, fs_devices,
3515− btrfs_test_opt(fs_info,
3516− CHECK_INTEGRITY_DATA) ? 1 : 0,
3517− fs_info->check_integrity_print_mask);
3518− if (ret)
3519− btrfs_warn(fs_info,
3520− "failed to initialize integrity check module: %d",
3521− ret);
3522− }
3523−#endif
35243519 ret = btrfs_read_qgroup_config(fs_info);
35253520 if (ret)
35263521 goto fail_trans_kthread;
@@ -3820,8 +3815,6 @@ static int write_dev_supers(struct btrfs_device *device, · 293 unmodified lines
38203815 */
38213816 if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
38223817 bio->bi_opf |= REQ_FUA;
3823−
3824− btrfsic_check_bio(bio);
38253818 submit_bio(bio);
38263819
38273820 if (btrfs_advance_sb_log(device, i))
@@ -3917,28 +3910,11 @@ static void write_dev_flush(struct btrfs_device *device) · 89 unmodified lines
39173910
39183911 device->last_flush_error = BLK_STS_OK;
39193912
3920−#ifndef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3921− /*
3922− * When a disk has write caching disabled, we skip submission of a bio
3923− * with flush and sync requests before writing the superblock, since
3924− * it's not needed. However when the integrity checker is enabled, this
3925− * results in reports that there are metadata blocks referred by a
3926− * superblock that were not properly flushed. So don't skip the bio
3927− * submission only when the integrity checker is enabled for the sake
3928− * of simplicity, since this is a debug tool and not meant for use in
3929− * non-debug builds.
3930− */
3931− if (!bdev_write_cache(device->bdev))
3932− return;
3933−#endif
3934−
39353913 bio_init(bio, device->bdev, NULL, 0,
39363914 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
39373915 bio->bi_end_io = btrfs_end_empty_barrier;
39383916 init_completion(&device->flush_wait);
39393917 bio->bi_private = &device->flush_wait;
3940−
3941− btrfsic_check_bio(bio);
39423918 submit_bio(bio);
39433919 set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
39443920 }
@@ -4414,16 +4390,12 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) · 469 unmodified lines
44144390
44154391 iput(fs_info->btree_inode);
44164392
4417−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4418− if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
4419− btrfsic_unmount(fs_info->fs_devices);
4420−#endif
4421−
44224393 btrfs_mapping_tree_free(&fs_info->mapping_tree);
44234394 btrfs_close_devices(fs_info->fs_devices);
44244395 }
44254396
4426−void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4397+void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
4398+ struct extent_buffer *buf)
44274399 {
44284400 struct btrfs_fs_info *fs_info = buf->fs_info;
44294401 u64 transid = btrfs_header_generation(buf);
@@ -4437,21 +4409,16 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf) · 7 unmodified lines
44374409 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
44384410 return;
44394411 #endif
4412+ /* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
4413+ ASSERT(trans->transid == fs_info->generation);
44404414 btrfs_assert_tree_write_locked(buf);
4441− if (transid != fs_info->generation)
4442− WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
4443− buf->start, transid, fs_info->generation);
4444− set_extent_buffer_dirty(buf);
4445−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4446− /*
4447− * btrfs_check_leaf() won't check item data if we don't have WRITTEN
4448− * set, so this will only validate the basic structure of the items.
4449− */
4450− if (btrfs_header_level(buf) == 0 && btrfs_check_leaf(buf)) {
4451− btrfs_print_leaf(buf);
4452− ASSERT(0);
4415+ if (unlikely(transid != fs_info->generation)) {
4416+ btrfs_abort_transaction(trans, -EUCLEAN);
4417+ btrfs_crit(fs_info,
4418+"dirty buffer transid mismatch, logical %llu found transid %llu running transid %llu",
4419+ buf->start, transid, fs_info->generation);
44534420 }
4454−#endif
4421+ set_extent_buffer_dirty(buf);
44554422 }
44564423
44574424 static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
@@ -4611,6 +4578,7 @@ static void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, · 153 unmodified lines
46114578 list_del(&ref->add_list);
46124579 atomic_dec(&delayed_refs->num_entries);
46134580 btrfs_put_delayed_ref(ref);
4581+ btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
46144582 }
46154583 if (head->must_insert_reserved)
46164584 pin_bytes = true;
@@ -4808,7 +4776,7 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, · 191 unmodified lines
48084776
48094777 spin_unlock(&cur_trans->dirty_bgs_lock);
48104778 btrfs_put_block_group(cache);
4811− btrfs_delayed_refs_rsv_release(fs_info, 1);
4779+ btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
48124780 spin_lock(&cur_trans->dirty_bgs_lock);
48134781 }
48144782 spin_unlock(&cur_trans->dirty_bgs_lock);
fs/btrfs/disk-io.h+2−1
@@ -104,7 +104,8 @@ static inline struct btrfs_root *btrfs_grab_root(struct btrfs_root *root) · 103 unmodified lines
104104 }
105105
106106 void btrfs_put_root(struct btrfs_root *root);
107−void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
107+void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
108+ struct extent_buffer *buf);
108109 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
109110 int atomic);
110111 int btrfs_read_extent_buffer(struct extent_buffer *buf,
fs/btrfs/extent-io-tree.c+180−92
@@ -105,32 +105,40 @@ void extent_io_tree_init(struct btrfs_fs_info *fs_info, · 104 unmodified lines
105105 lockdep_set_class(&tree->lock, &file_extent_tree_class);
106106 }
107107
108+/*
109+ * Empty an io tree, removing and freeing every extent state record from the
110+ * tree. This should be called once we are sure no other task can access the
111+ * tree anymore, so no tree updates happen after we empty the tree and there
112+ * aren't any waiters on any extent state record (EXTENT_LOCKED bit is never
113+ * set on any extent state when calling this function).
114+ */
108115 void extent_io_tree_release(struct extent_io_tree *tree)
109116 {
117+ struct rb_root root;
118+ struct extent_state *state;
119+ struct extent_state *tmp;
120+
110121 spin_lock(&tree->lock);
111− /*
112− * Do a single barrier for the waitqueue_active check here, the state
113− * of the waitqueue should not change once extent_io_tree_release is
114− * called.
115− */
116− smp_mb();
117− while (!RB_EMPTY_ROOT(&tree->state)) {
118− struct rb_node *node;
119− struct extent_state *state;
120−
121− node = rb_first(&tree->state);
122− state = rb_entry(node, struct extent_state, rb_node);
123− rb_erase(&state->rb_node, &tree->state);
122+ root = tree->state;
123+ tree->state = RB_ROOT;
124+ rbtree_postorder_for_each_entry_safe(state, tmp, &root, rb_node) {
125+ /* Clear node to keep free_extent_state() happy. */
124126 RB_CLEAR_NODE(&state->rb_node);
127+ ASSERT(!(state->state & EXTENT_LOCKED));
125128 /*
126− * btree io trees aren't supposed to have tasks waiting for
127− * changes in the flags of extent states ever.
129+ * No need for a memory barrier here, as we are holding the tree
130+ * lock and we only change the waitqueue while holding that lock
131+ * (see wait_extent_bit()).
128132 */
129133 ASSERT(!waitqueue_active(&state->wq));
130134 free_extent_state(state);
131−
132135 cond_resched_lock(&tree->lock);
133136 }
137+ /*
138+ * Should still be empty even after a reschedule, no other task should
139+ * be accessing the tree anymore.
140+ */
141+ ASSERT(RB_EMPTY_ROOT(&tree->state));
134142 spin_unlock(&tree->lock);
135143 }
136144
@@ -327,6 +335,36 @@ static void extent_io_tree_panic(struct extent_io_tree *tree, int err) · 190 unmodified lines
327335 "locking error: extent tree was modified by another thread while locked");
328336 }
329337
338+static void merge_prev_state(struct extent_io_tree *tree, struct extent_state *state)
339+{
340+ struct extent_state *prev;
341+
342+ prev = prev_state(state);
343+ if (prev && prev->end == state->start - 1 && prev->state == state->state) {
344+ if (tree->inode)
345+ btrfs_merge_delalloc_extent(tree->inode, state, prev);
346+ state->start = prev->start;
347+ rb_erase(&prev->rb_node, &tree->state);
348+ RB_CLEAR_NODE(&prev->rb_node);
349+ free_extent_state(prev);
350+ }
351+}
352+
353+static void merge_next_state(struct extent_io_tree *tree, struct extent_state *state)
354+{
355+ struct extent_state *next;
356+
357+ next = next_state(state);
358+ if (next && next->start == state->end + 1 && next->state == state->state) {
359+ if (tree->inode)
360+ btrfs_merge_delalloc_extent(tree->inode, state, next);
361+ state->end = next->end;
362+ rb_erase(&next->rb_node, &tree->state);
363+ RB_CLEAR_NODE(&next->rb_node);
364+ free_extent_state(next);
365+ }
366+}
367+
330368 /*
331369 * Utility function to look for merge candidates inside a given range. Any
332370 * extents with matching state are merged together into a single extent in the
@@ -338,31 +376,11 @@ static void extent_io_tree_panic(struct extent_io_tree *tree, int err) · 5 unmodified lines
338376 */
339377 static void merge_state(struct extent_io_tree *tree, struct extent_state *state)
340378 {
341− struct extent_state *other;
342−
343379 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
344380 return;
345381
346− other = prev_state(state);
347− if (other && other->end == state->start - 1 &&
348− other->state == state->state) {
349− if (tree->inode)
350− btrfs_merge_delalloc_extent(tree->inode, state, other);
351− state->start = other->start;
352− rb_erase(&other->rb_node, &tree->state);
353− RB_CLEAR_NODE(&other->rb_node);
354− free_extent_state(other);
355− }
356− other = next_state(state);
357− if (other && other->start == state->end + 1 &&
358− other->state == state->state) {
359− if (tree->inode)
360− btrfs_merge_delalloc_extent(tree->inode, state, other);
361− state->end = other->end;
362− rb_erase(&other->rb_node, &tree->state);
363− RB_CLEAR_NODE(&other->rb_node);
364− free_extent_state(other);
365− }
382+ merge_prev_state(tree, state);
383+ merge_next_state(tree, state);
366384 }
367385
368386 static void set_state_bits(struct extent_io_tree *tree,
@@ -384,19 +402,27 @@ static void set_state_bits(struct extent_io_tree *tree, · 15 unmodified lines
384402 * Insert an extent_state struct into the tree. 'bits' are set on the
385403 * struct before it is inserted.
386404 *
387− * This may return -EEXIST if the extent is already there, in which case the
388− * state struct is freed.
405+ * Returns a pointer to the struct extent_state record containing the range
406+ * requested for insertion, which may be the same as the given struct or it
407+ * may be an existing record in the tree that was expanded to accommodate the
408+ * requested range. In case of an extent_state different from the one that was
409+ * given, the later can be freed or reused by the caller.
410+ *
411+ * On error it returns an error pointer.
389412 *
390413 * The tree lock is not taken internally. This is a utility function and
391414 * probably isn't what you want to call (see set/clear_extent_bit).
392415 */
393−static int insert_state(struct extent_io_tree *tree,
394− struct extent_state *state,
395− u32 bits, struct extent_changeset *changeset)
416+static struct extent_state *insert_state(struct extent_io_tree *tree,
417+ struct extent_state *state,
418+ u32 bits,
419+ struct extent_changeset *changeset)
396420 {
397421 struct rb_node **node;
398422 struct rb_node *parent = NULL;
399− const u64 end = state->end;
423+ const u64 start = state->start - 1;
424+ const u64 end = state->end + 1;
425+ const bool try_merge = !(bits & (EXTENT_LOCKED | EXTENT_BOUNDARY));
400426
401427 set_state_bits(tree, state, bits, changeset);
402428
@@ -407,23 +433,42 @@ static int insert_state(struct extent_io_tree *tree, · 4 unmodified lines
407433 parent = *node;
408434 entry = rb_entry(parent, struct extent_state, rb_node);
409435
410− if (end < entry->start) {
436+ if (state->end < entry->start) {
437+ if (try_merge && end == entry->start &&
438+ state->state == entry->state) {
439+ if (tree->inode)
440+ btrfs_merge_delalloc_extent(tree->inode,
441+ state, entry);
442+ entry->start = state->start;
443+ merge_prev_state(tree, entry);
444+ state->state = 0;
445+ return entry;
446+ }
411447 node = &(*node)->rb_left;
412− } else if (end > entry->end) {
448+ } else if (state->end > entry->end) {
449+ if (try_merge && entry->end == start &&
450+ state->state == entry->state) {
451+ if (tree->inode)
452+ btrfs_merge_delalloc_extent(tree->inode,
453+ state, entry);
454+ entry->end = state->end;
455+ merge_next_state(tree, entry);
456+ state->state = 0;
457+ return entry;
458+ }
413459 node = &(*node)->rb_right;
414460 } else {
415461 btrfs_err(tree->fs_info,
416462 "found node %llu %llu on insert of %llu %llu",
417− entry->start, entry->end, state->start, end);
418− return -EEXIST;
463+ entry->start, entry->end, state->start, state->end);
464+ return ERR_PTR(-EEXIST);
419465 }
420466 }
421467
422468 rb_link_node(&state->rb_node, parent, node);
423469 rb_insert_color(&state->rb_node, &tree->state);
424470
425− merge_state(tree, state);
426− return 0;
471+ return state;
427472 }
428473
429474 /*
@@ -708,26 +753,13 @@ out: · 278 unmodified lines
708753
709754 }
710755
711−static void wait_on_state(struct extent_io_tree *tree,
712− struct extent_state *state)
713− __releases(tree->lock)
714− __acquires(tree->lock)
715−{
716− DEFINE_WAIT(wait);
717− prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
718− spin_unlock(&tree->lock);
719− schedule();
720− spin_lock(&tree->lock);
721− finish_wait(&state->wq, &wait);
722−}
723−
724756 /*
725757 * Wait for one or more bits to clear on a range in the state tree.
726758 * The range [start, end] is inclusive.
727759 * The tree lock is taken by this function
728760 */
729−void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
730− struct extent_state **cached_state)
761+static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
762+ u32 bits, struct extent_state **cached_state)
731763 {
732764 struct extent_state *state;
733765
@@ -758,9 +790,15 @@ process_node: · 24 unmodified lines
758790 goto out;
759791
760792 if (state->state & bits) {
793+ DEFINE_WAIT(wait);
794+
761795 start = state->start;
762796 refcount_inc(&state->refs);
763− wait_on_state(tree, state);
797+ prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
798+ spin_unlock(&tree->lock);
799+ schedule();
800+ spin_lock(&tree->lock);
801+ finish_wait(&state->wq, &wait);
764802 free_extent_state(state);
765803 goto again;
766804 }
@@ -847,10 +885,19 @@ bool find_first_extent_bit(struct extent_io_tree *tree, u64 start, · 80 unmodified lines
847885 if (state->end == start - 1 && extent_state_in_tree(state)) {
848886 while ((state = next_state(state)) != NULL) {
849887 if (state->state & bits)
850− goto got_it;
888+ break;
851889 }
890+ /*
891+ * If we found the next extent state, clear cached_state
892+ * so that we can cache the next extent state below and
893+ * avoid future calls going over the same extent state
894+ * again. If we haven't found any, clear as well since
895+ * it's now useless.
896+ */
852897 free_extent_state(*cached_state);
853898 *cached_state = NULL;
899+ if (state)
900+ goto got_it;
854901 goto out;
855902 }
856903 free_extent_state(*cached_state);
@@ -1133,6 +1180,8 @@ hit_next: · 276 unmodified lines
11331180 */
11341181 if (state->start > start) {
11351182 u64 this_end;
1183+ struct extent_state *inserted_state;
1184+
11361185 if (end < last_start)
11371186 this_end = end;
11381187 else
@@ -1148,12 +1197,15 @@ hit_next: · 9 unmodified lines
11481197 */
11491198 prealloc->start = start;
11501199 prealloc->end = this_end;
1151− err = insert_state(tree, prealloc, bits, changeset);
1152− if (err)
1200+ inserted_state = insert_state(tree, prealloc, bits, changeset);
1201+ if (IS_ERR(inserted_state)) {
1202+ err = PTR_ERR(inserted_state);
11531203 extent_io_tree_panic(tree, err);
1204+ }
11541205
1155− cache_state(prealloc, cached_state);
1156− prealloc = NULL;
1206+ cache_state(inserted_state, cached_state);
1207+ if (inserted_state == prealloc)
1208+ prealloc = NULL;
11571209 start = this_end + 1;
11581210 goto search_again;
11591211 }
@@ -1356,6 +1408,8 @@ hit_next: · 196 unmodified lines
13561408 */
13571409 if (state->start > start) {
13581410 u64 this_end;
1411+ struct extent_state *inserted_state;
1412+
13591413 if (end < last_start)
13601414 this_end = end;
13611415 else
@@ -1373,11 +1427,14 @@ hit_next: · 11 unmodified lines
13731427 */
13741428 prealloc->start = start;
13751429 prealloc->end = this_end;
1376− err = insert_state(tree, prealloc, bits, NULL);
1377− if (err)
1430+ inserted_state = insert_state(tree, prealloc, bits, NULL);
1431+ if (IS_ERR(inserted_state)) {
1432+ err = PTR_ERR(inserted_state);
13781433 extent_io_tree_panic(tree, err);
1379− cache_state(prealloc, cached_state);
1380− prealloc = NULL;
1434+ }
1435+ cache_state(inserted_state, cached_state);
1436+ if (inserted_state == prealloc)
1437+ prealloc = NULL;
13811438 start = this_end + 1;
13821439 goto search_again;
13831440 }
@@ -1640,15 +1697,46 @@ search: · 256 unmodified lines
16401697 }
16411698
16421699 /*
1643− * Search a range in the state tree for a given mask. If 'filled' == 1, this
1644− * returns 1 only if every extent in the tree has the bits set. Otherwise, 1
1645− * is returned if any bit in the range is found set.
1700+ * Check if the single @bit exists in the given range.
1701+ */
1702+bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit)
1703+{
1704+ struct extent_state *state = NULL;
1705+ bool bitset = false;
1706+
1707+ ASSERT(is_power_of_2(bit));
1708+
1709+ spin_lock(&tree->lock);
1710+ state = tree_search(tree, start);
1711+ while (state && start <= end) {
1712+ if (state->start > end)
1713+ break;
1714+
1715+ if (state->state & bit) {
1716+ bitset = true;
1717+ break;
1718+ }
1719+
1720+ /* If state->end is (u64)-1, start will overflow to 0 */
1721+ start = state->end + 1;
1722+ if (start > end || start == 0)
1723+ break;
1724+ state = next_state(state);
1725+ }
1726+ spin_unlock(&tree->lock);
1727+ return bitset;
1728+}
1729+
1730+/*
1731+ * Check if the whole range [@start,@end) contains the single @bit set.
16461732 */
1647−int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1648− u32 bits, int filled, struct extent_state *cached)
1733+bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
1734+ struct extent_state *cached)
16491735 {
16501736 struct extent_state *state = NULL;
1651− int bitset = 0;
1737+ bool bitset = true;
1738+
1739+ ASSERT(is_power_of_2(bit));
16521740
16531741 spin_lock(&tree->lock);
16541742 if (cached && extent_state_in_tree(cached) && cached->start <= start &&
@@ -1657,35 +1745,35 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, · 2 unmodified lines
16571745 else
16581746 state = tree_search(tree, start);
16591747 while (state && start <= end) {
1660− if (filled && state->start > start) {
1661− bitset = 0;
1748+ if (state->start > start) {
1749+ bitset = false;
16621750 break;
16631751 }
16641752
16651753 if (state->start > end)
16661754 break;
16671755
1668− if (state->state & bits) {
1669− bitset = 1;
1670− if (!filled)
1671− break;
1672− } else if (filled) {
1673− bitset = 0;
1756+ if ((state->state & bit) == 0) {
1757+ bitset = false;
16741758 break;
16751759 }
16761760
16771761 if (state->end == (u64)-1)
16781762 break;
16791763
1764+ /*
1765+ * Last entry (if state->end is (u64)-1 and overflow happens),
1766+ * or next entry starts after the range.
1767+ */
16801768 start = state->end + 1;
1681− if (start > end)
1769+ if (start > end || start == 0)
16821770 break;
16831771 state = next_state(state);
16841772 }
16851773
16861774 /* We ran out of states and were still inside of our range. */
1687− if (filled && !state)
1688− bitset = 0;
1775+ if (!state)
1776+ bitset = false;
16891777 spin_unlock(&tree->lock);
16901778 return bitset;
16911779 }
fs/btrfs/extent-io-tree.h+3−4
@@ -131,8 +131,9 @@ u64 count_range_bits(struct extent_io_tree *tree, · 130 unmodified lines
131131 struct extent_state **cached_state);
132132
133133 void free_extent_state(struct extent_state *state);
134−int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
135− u32 bits, int filled, struct extent_state *cached_state);
134+bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
135+ struct extent_state *cached_state);
136+bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
136137 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
137138 u32 bits, struct extent_changeset *changeset);
138139 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
@@ -192,7 +193,5 @@ int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, · 53 unmodified lines
192193 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
193194 u64 *end, u64 max_bytes,
194195 struct extent_state **cached_state);
195−void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
196− struct extent_state **cached_state);
197196
198197 #endif /* BTRFS_EXTENT_IO_TREE_H */
fs/btrfs/extent-tree.c+360−176
@@ -42,14 +42,16 @@ · 41 unmodified lines
4242 #include "file-item.h"
4343 #include "orphan.h"
4444 #include "tree-checker.h"
45+#include "raid-stripe-tree.h"
4546
4647 #undef SCRAMBLE_DELAYED_REFS
4748
4849
4950 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
51+ struct btrfs_delayed_ref_head *href,
5052 struct btrfs_delayed_ref_node *node, u64 parent,
5153 u64 root_objectid, u64 owner_objectid,
52− u64 owner_offset, int refs_to_drop,
54+ u64 owner_offset,
5355 struct btrfs_delayed_extent_op *extra_op);
5456 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
5557 struct extent_buffer *leaf,
@@ -57,7 +59,7 @@ static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, · 1 unmodified line
5759 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5860 u64 parent, u64 root_objectid,
5961 u64 flags, u64 owner, u64 offset,
60− struct btrfs_key *ins, int ref_mod);
62+ struct btrfs_key *ins, int ref_mod, u64 oref_root);
6163 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6264 struct btrfs_delayed_ref_node *node,
6365 struct btrfs_delayed_extent_op *extent_op);
@@ -344,9 +346,15 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, · 280 unmodified lines
344346 struct btrfs_extent_inline_ref *iref,
345347 enum btrfs_inline_ref_type is_data)
346348 {
349+ struct btrfs_fs_info *fs_info = eb->fs_info;
347350 int type = btrfs_extent_inline_ref_type(eb, iref);
348351 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
349352
353+ if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
354+ ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
355+ return type;
356+ }
357+
350358 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
351359 type == BTRFS_SHARED_BLOCK_REF_KEY ||
352360 type == BTRFS_SHARED_DATA_REF_KEY ||
@@ -355,26 +363,25 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, · 2 unmodified lines
355363 if (type == BTRFS_TREE_BLOCK_REF_KEY)
356364 return type;
357365 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
358− ASSERT(eb->fs_info);
366+ ASSERT(fs_info);
359367 /*
360368 * Every shared one has parent tree block,
361369 * which must be aligned to sector size.
362370 */
363− if (offset &&
364− IS_ALIGNED(offset, eb->fs_info->sectorsize))
371+ if (offset && IS_ALIGNED(offset, fs_info->sectorsize))
365372 return type;
366373 }
367374 } else if (is_data == BTRFS_REF_TYPE_DATA) {
368375 if (type == BTRFS_EXTENT_DATA_REF_KEY)
369376 return type;
370377 if (type == BTRFS_SHARED_DATA_REF_KEY) {
371− ASSERT(eb->fs_info);
378+ ASSERT(fs_info);
372379 /*
373380 * Every shared one has parent tree block,
374381 * which must be aligned to sector size.
375382 */
376383 if (offset &&
377− IS_ALIGNED(offset, eb->fs_info->sectorsize))
384+ IS_ALIGNED(offset, fs_info->sectorsize))
378385 return type;
379386 }
380387 } else {
@@ -385,7 +392,7 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, · 4 unmodified lines
385392
386393 WARN_ON(1);
387394 btrfs_print_leaf(eb);
388− btrfs_err(eb->fs_info,
395+ btrfs_err(fs_info,
389396 "eb %llu iref 0x%lx invalid extent inline ref type %d",
390397 eb->start, (unsigned long)iref, type);
391398
@@ -399,11 +406,11 @@ u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) · 7 unmodified lines
399406 __le64 lenum;
400407
401408 lenum = cpu_to_le64(root_objectid);
402− high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
409+ high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
403410 lenum = cpu_to_le64(owner);
404− low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
411+ low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
405412 lenum = cpu_to_le64(offset);
406− low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
413+ low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
407414
408415 return ((u64)high_crc << 31) ^ (u64)low_crc;
409416 }
@@ -575,7 +582,7 @@ static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, · 165 unmodified lines
575582 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
576583 }
577584 }
578− btrfs_mark_buffer_dirty(leaf);
585+ btrfs_mark_buffer_dirty(trans, leaf);
579586 ret = 0;
580587 fail:
581588 btrfs_release_path(path);
@@ -623,7 +630,7 @@ static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, · 41 unmodified lines
623630 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
624631 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
625632 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
626− btrfs_mark_buffer_dirty(leaf);
633+ btrfs_mark_buffer_dirty(trans, leaf);
627634 }
628635 return ret;
629636 }
@@ -789,7 +796,6 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, · 159 unmodified lines
789796 int type;
790797 int want;
791798 int ret;
792− int err = 0;
793799 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
794800 int needed;
795801
@@ -816,10 +822,8 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, · 20 unmodified lines
816822
817823 again:
818824 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
819− if (ret < 0) {
820− err = ret;
825+ if (ret < 0)
821826 goto out;
822− }
823827
824828 /*
825829 * We may be a newly converted file system which still has the old fat
@@ -846,7 +850,7 @@ again: · 20 unmodified lines
846850 }
847851
848852 if (ret && !insert) {
849− err = -ENOENT;
853+ ret = -ENOENT;
850854 goto out;
851855 } else if (WARN_ON(ret)) {
852856 btrfs_print_leaf(path->nodes[0]);
@@ -854,18 +858,18 @@ again: · 1 unmodified line
854858 "extent item not found for insert, bytenr %llu num_bytes %llu parent %llu root_objectid %llu owner %llu offset %llu",
855859 bytenr, num_bytes, parent, root_objectid, owner,
856860 offset);
857− err = -EIO;
861+ ret = -EUCLEAN;
858862 goto out;
859863 }
860864
861865 leaf = path->nodes[0];
862866 item_size = btrfs_item_size(leaf, path->slots[0]);
863867 if (unlikely(item_size < sizeof(*ei))) {
864− err = -EUCLEAN;
868+ ret = -EUCLEAN;
865869 btrfs_err(fs_info,
866870 "unexpected extent item size, has %llu expect >= %zu",
867871 item_size, sizeof(*ei));
868− btrfs_abort_transaction(trans, err);
872+ btrfs_abort_transaction(trans, ret);
869873 goto out;
870874 }
871875
@@ -885,22 +889,17 @@ again: · 13 unmodified lines
885889 else
886890 needed = BTRFS_REF_TYPE_BLOCK;
887891
888− err = -ENOENT;
889− while (1) {
890− if (ptr >= end) {
891− if (ptr > end) {
892− err = -EUCLEAN;
893− btrfs_print_leaf(path->nodes[0]);
894− btrfs_crit(fs_info,
895−"overrun extent record at slot %d while looking for inline extent for root %llu owner %llu offset %llu parent %llu",
896− path->slots[0], root_objectid, owner, offset, parent);
897− }
898− break;
899− }
892+ ret = -ENOENT;
893+ while (ptr < end) {
900894 iref = (struct btrfs_extent_inline_ref *)ptr;
901895 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
896+ if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
897+ ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
898+ ptr += btrfs_extent_inline_ref_size(type);
899+ continue;
900+ }
902901 if (type == BTRFS_REF_TYPE_INVALID) {
903− err = -EUCLEAN;
902+ ret = -EUCLEAN;
904903 goto out;
905904 }
906905
@@ -916,7 +915,7 @@ again: · 9 unmodified lines
916915 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
917916 if (match_extent_data_ref(leaf, dref, root_objectid,
918917 owner, offset)) {
919− err = 0;
918+ ret = 0;
920919 break;
921920 }
922921 if (hash_extent_data_ref_item(leaf, dref) <
@@ -927,14 +926,14 @@ again: · 4 unmodified lines
927926 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
928927 if (parent > 0) {
929928 if (parent == ref_offset) {
930− err = 0;
929+ ret = 0;
931930 break;
932931 }
933932 if (ref_offset < parent)
934933 break;
935934 } else {
936935 if (root_objectid == ref_offset) {
937− err = 0;
936+ ret = 0;
938937 break;
939938 }
940939 if (ref_offset < root_objectid)
@@ -943,10 +942,20 @@ again: · 2 unmodified lines
943942 }
944943 ptr += btrfs_extent_inline_ref_size(type);
945944 }
946− if (err == -ENOENT && insert) {
945+
946+ if (unlikely(ptr > end)) {
947+ ret = -EUCLEAN;
948+ btrfs_print_leaf(path->nodes[0]);
949+ btrfs_crit(fs_info,
950+"overrun extent record at slot %d while looking for inline extent for root %llu owner %llu offset %llu parent %llu",
951+ path->slots[0], root_objectid, owner, offset, parent);
952+ goto out;
953+ }
954+
955+ if (ret == -ENOENT && insert) {
947956 if (item_size + extra_size >=
948957 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
949− err = -EAGAIN;
958+ ret = -EAGAIN;
950959 goto out;
951960 }
952961 /*
@@ -958,7 +967,7 @@ again: · 5 unmodified lines
958967 if (find_next_key(path, 0, &key) == 0 &&
959968 key.objectid == bytenr &&
960969 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
961− err = -EAGAIN;
970+ ret = -EAGAIN;
962971 goto out;
963972 }
964973 }
@@ -969,14 +978,14 @@ out: · 4 unmodified lines
969978 path->search_for_extension = 0;
970979 btrfs_unlock_up_safe(path, 1);
971980 }
972− return err;
981+ return ret;
973982 }
974983
975984 /*
976985 * helper to add new inline back ref
977986 */
978987 static noinline_for_stack
979−void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
988+void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
980989 struct btrfs_path *path,
981990 struct btrfs_extent_inline_ref *iref,
982991 u64 parent, u64 root_objectid,
@@ -999,7 +1008,7 @@ void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, · 16 unmodified lines
9991008 type = extent_ref_type(parent, owner);
10001009 size = btrfs_extent_inline_ref_size(type);
10011010
1002− btrfs_extend_item(path, size);
1011+ btrfs_extend_item(trans, path, size);
10031012
10041013 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
10051014 refs = btrfs_extent_refs(leaf, ei);
@@ -1033,7 +1042,7 @@ void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, · 27 unmodified lines
10331042 } else {
10341043 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
10351044 }
1036− btrfs_mark_buffer_dirty(leaf);
1045+ btrfs_mark_buffer_dirty(trans, leaf);
10371046 }
10381047
10391048 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
@@ -1066,7 +1075,9 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans, · 26 unmodified lines
10661075 /*
10671076 * helper to update/remove inline back ref
10681077 */
1069−static noinline_for_stack int update_inline_extent_backref(struct btrfs_path *path,
1078+static noinline_for_stack int update_inline_extent_backref(
1079+ struct btrfs_trans_handle *trans,
1080+ struct btrfs_path *path,
10701081 struct btrfs_extent_inline_ref *iref,
10711082 int refs_to_mod,
10721083 struct btrfs_delayed_extent_op *extent_op)
@@ -1174,9 +1185,9 @@ static noinline_for_stack int update_inline_extent_backref(struct btrfs_path *pa · 101 unmodified lines
11741185 memmove_extent_buffer(leaf, ptr, ptr + size,
11751186 end - ptr - size);
11761187 item_size -= size;
1177− btrfs_truncate_item(path, item_size, 1);
1188+ btrfs_truncate_item(trans, path, item_size, 1);
11781189 }
1179− btrfs_mark_buffer_dirty(leaf);
1190+ btrfs_mark_buffer_dirty(trans, leaf);
11801191 return 0;
11811192 }
11821193
@@ -1206,9 +1217,10 @@ int insert_inline_extent_backref(struct btrfs_trans_handle *trans, · 23 unmodified lines
12061217 bytenr, num_bytes, root_objectid, path->slots[0]);
12071218 return -EUCLEAN;
12081219 }
1209− ret = update_inline_extent_backref(path, iref, refs_to_add, extent_op);
1220+ ret = update_inline_extent_backref(trans, path, iref,
1221+ refs_to_add, extent_op);
12101222 } else if (ret == -ENOENT) {
1211− setup_inline_extent_backref(trans->fs_info, path, iref, parent,
1223+ setup_inline_extent_backref(trans, path, iref, parent,
12121224 root_objectid, owner, offset,
12131225 refs_to_add, extent_op);
12141226 ret = 0;
@@ -1226,7 +1238,8 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans, · 11 unmodified lines
12261238
12271239 BUG_ON(!is_data && refs_to_drop != 1);
12281240 if (iref)
1229− ret = update_inline_extent_backref(path, iref, -refs_to_drop, NULL);
1241+ ret = update_inline_extent_backref(trans, path, iref,
1242+ -refs_to_drop, NULL);
12301243 else if (is_data)
12311244 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
12321245 else
@@ -1422,7 +1435,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 189 unmodified lines
14221435 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
14231436 generic_ref->action);
14241437 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1425− generic_ref->tree_ref.owning_root == BTRFS_TREE_LOG_OBJECTID);
1438+ generic_ref->tree_ref.ref_root == BTRFS_TREE_LOG_OBJECTID);
14261439
14271440 if (generic_ref->type == BTRFS_REF_METADATA)
14281441 ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL);
@@ -1435,7 +1448,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
14351448 }
14361449
14371450 /*
1438− * __btrfs_inc_extent_ref - insert backreference for a given extent
1451+ * Insert backreference for a given extent.
14391452 *
14401453 * The counterpart is in __btrfs_free_extent(), with examples and more details
14411454 * how it works.
@@ -1465,8 +1478,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 23 unmodified lines
14651478 * always passed as 0. For data extents it is the fileoffset
14661479 * this extent belongs to.
14671480 *
1468− * @refs_to_add Number of references to add
1469− *
14701481 * @extent_op Pointer to a structure, holding information necessary when
14711482 * updating a tree block's flags
14721483 *
@@ -1474,7 +1485,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 1 unmodified line
14741485 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
14751486 struct btrfs_delayed_ref_node *node,
14761487 u64 parent, u64 root_objectid,
1477− u64 owner, u64 offset, int refs_to_add,
1488+ u64 owner, u64 offset,
14781489 struct btrfs_delayed_extent_op *extent_op)
14791490 {
14801491 struct btrfs_path *path;
@@ -1484,6 +1495,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 3 unmodified lines
14841495 u64 bytenr = node->bytenr;
14851496 u64 num_bytes = node->num_bytes;
14861497 u64 refs;
1498+ int refs_to_add = node->ref_mod;
14871499 int ret;
14881500
14891501 path = btrfs_alloc_path();
@@ -1510,7 +1522,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 20 unmodified lines
15101522 if (extent_op)
15111523 __run_delayed_extent_op(extent_op, leaf, item);
15121524
1513− btrfs_mark_buffer_dirty(leaf);
1525+ btrfs_mark_buffer_dirty(trans, leaf);
15141526 btrfs_release_path(path);
15151527
15161528 /* now insert the actual backref */
@@ -1530,44 +1542,57 @@ out: · 13 unmodified lines
15301542 }
15311543
15321544 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1545+ struct btrfs_delayed_ref_head *href,
15331546 struct btrfs_delayed_ref_node *node,
15341547 struct btrfs_delayed_extent_op *extent_op,
15351548 bool insert_reserved)
15361549 {
15371550 int ret = 0;
15381551 struct btrfs_delayed_data_ref *ref;
1539− struct btrfs_key ins;
15401552 u64 parent = 0;
1541− u64 ref_root = 0;
15421553 u64 flags = 0;
15431554
1544− ins.objectid = node->bytenr;
1545− ins.offset = node->num_bytes;
1546− ins.type = BTRFS_EXTENT_ITEM_KEY;
1547−
15481555 ref = btrfs_delayed_node_to_data_ref(node);
15491556 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
15501557
15511558 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
15521559 parent = ref->parent;
1553− ref_root = ref->root;
15541560
15551561 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1562+ struct btrfs_key key;
1563+ struct btrfs_squota_delta delta = {
1564+ .root = href->owning_root,
1565+ .num_bytes = node->num_bytes,
1566+ .rsv_bytes = href->reserved_bytes,
1567+ .is_data = true,
1568+ .is_inc = true,
1569+ .generation = trans->transid,
1570+ };
1571+
15561572 if (extent_op)
15571573 flags |= extent_op->flags_to_set;
1558− ret = alloc_reserved_file_extent(trans, parent, ref_root,
1574+
1575+ key.objectid = node->bytenr;
1576+ key.type = BTRFS_EXTENT_ITEM_KEY;
1577+ key.offset = node->num_bytes;
1578+
1579+ ret = alloc_reserved_file_extent(trans, parent, ref->root,
15591580 flags, ref->objectid,
1560− ref->offset, &ins,
1561− node->ref_mod);
1581+ ref->offset, &key,
1582+ node->ref_mod, href->owning_root);
1583+ if (!ret)
1584+ ret = btrfs_record_squota_delta(trans->fs_info, &delta);
1585+ else
1586+ btrfs_qgroup_free_refroot(trans->fs_info, delta.root,
1587+ delta.rsv_bytes, BTRFS_QGROUP_RSV_DATA);
15621588 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1563− ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1589+ ret = __btrfs_inc_extent_ref(trans, node, parent, ref->root,
15641590 ref->objectid, ref->offset,
1565− node->ref_mod, extent_op);
1591+ extent_op);
15661592 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1567− ret = __btrfs_free_extent(trans, node, parent,
1568− ref_root, ref->objectid,
1569− ref->offset, node->ref_mod,
1570− extent_op);
1593+ ret = __btrfs_free_extent(trans, href, node, parent,
1594+ ref->root, ref->objectid,
1595+ ref->offset, extent_op);
15711596 } else {
15721597 BUG();
15731598 }
@@ -1604,7 +1629,6 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans, · 30 unmodified lines
16041629 struct extent_buffer *leaf;
16051630 u32 item_size;
16061631 int ret;
1607− int err = 0;
16081632 int metadata = 1;
16091633
16101634 if (TRANS_ABORTED(trans))
@@ -1631,10 +1655,8 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans, · 20 unmodified lines
16311655 again:
16321656 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
16331657 if (ret < 0) {
1634− err = ret;
16351658 goto out;
1636− }
1637− if (ret > 0) {
1659+ } else if (ret > 0) {
16381660 if (metadata) {
16391661 if (path->slots[0] > 0) {
16401662 path->slots[0]--;
@@ -1655,7 +1677,7 @@ again: · 14 unmodified lines
16551677 goto again;
16561678 }
16571679 } else {
1658− err = -EUCLEAN;
1680+ ret = -EUCLEAN;
16591681 btrfs_err(fs_info,
16601682 "missing extent item for extent %llu num_bytes %llu level %d",
16611683 head->bytenr, head->num_bytes, extent_op->level);
@@ -1667,29 +1689,31 @@ again: · 5 unmodified lines
16671689 item_size = btrfs_item_size(leaf, path->slots[0]);
16681690
16691691 if (unlikely(item_size < sizeof(*ei))) {
1670− err = -EUCLEAN;
1692+ ret = -EUCLEAN;
16711693 btrfs_err(fs_info,
16721694 "unexpected extent item size, has %u expect >= %zu",
16731695 item_size, sizeof(*ei));
1674− btrfs_abort_transaction(trans, err);
1696+ btrfs_abort_transaction(trans, ret);
16751697 goto out;
16761698 }
16771699
16781700 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
16791701 __run_delayed_extent_op(extent_op, leaf, ei);
16801702
1681− btrfs_mark_buffer_dirty(leaf);
1703+ btrfs_mark_buffer_dirty(trans, leaf);
16821704 out:
16831705 btrfs_free_path(path);
1684− return err;
1706+ return ret;
16851707 }
16861708
16871709 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1710+ struct btrfs_delayed_ref_head *href,
16881711 struct btrfs_delayed_ref_node *node,
16891712 struct btrfs_delayed_extent_op *extent_op,
16901713 bool insert_reserved)
16911714 {
16921715 int ret = 0;
1716+ struct btrfs_fs_info *fs_info = trans->fs_info;
16931717 struct btrfs_delayed_tree_ref *ref;
16941718 u64 parent = 0;
16951719 u64 ref_root = 0;
@@ -1709,14 +1733,25 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, · 13 unmodified lines
17091733 return -EUCLEAN;
17101734 }
17111735 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1736+ struct btrfs_squota_delta delta = {
1737+ .root = href->owning_root,
1738+ .num_bytes = fs_info->nodesize,
1739+ .rsv_bytes = 0,
1740+ .is_data = false,
1741+ .is_inc = true,
1742+ .generation = trans->transid,
1743+ };
1744+
17121745 BUG_ON(!extent_op || !extent_op->update_flags);
17131746 ret = alloc_reserved_tree_block(trans, node, extent_op);
1747+ if (!ret)
1748+ btrfs_record_squota_delta(fs_info, &delta);
17141749 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
17151750 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1716− ref->level, 0, 1, extent_op);
1751+ ref->level, 0, extent_op);
17171752 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1718− ret = __btrfs_free_extent(trans, node, parent, ref_root,
1719− ref->level, 0, 1, extent_op);
1753+ ret = __btrfs_free_extent(trans, href, node, parent, ref_root,
1754+ ref->level, 0, extent_op);
17201755 } else {
17211756 BUG();
17221757 }
@@ -1725,6 +1760,7 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, · 2 unmodified lines
17251760
17261761 /* helper function to actually process a single delayed ref entry */
17271762 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1763+ struct btrfs_delayed_ref_head *href,
17281764 struct btrfs_delayed_ref_node *node,
17291765 struct btrfs_delayed_extent_op *extent_op,
17301766 bool insert_reserved)
@@ -1739,12 +1775,14 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans, · 8 unmodified lines
17391775
17401776 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
17411777 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1742− ret = run_delayed_tree_ref(trans, node, extent_op,
1778+ ret = run_delayed_tree_ref(trans, href, node, extent_op,
17431779 insert_reserved);
17441780 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
17451781 node->type == BTRFS_SHARED_DATA_REF_KEY)
1746− ret = run_delayed_data_ref(trans, node, extent_op,
1782+ ret = run_delayed_data_ref(trans, href, node, extent_op,
17471783 insert_reserved);
1784+ else if (node->type == BTRFS_EXTENT_OWNER_REF_KEY)
1785+ ret = 0;
17481786 else
17491787 BUG();
17501788 if (ret && insert_reserved)
@@ -1823,28 +1861,37 @@ static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, · 72 unmodified lines
18231861 return ret ? ret : 1;
18241862 }
18251863
1826−void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1864+u64 btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
18271865 struct btrfs_delayed_ref_root *delayed_refs,
18281866 struct btrfs_delayed_ref_head *head)
18291867 {
1830− int nr_items = 1; /* Dropping this ref head update. */
1831−
18321868 /*
18331869 * We had csum deletions accounted for in our delayed refs rsv, we need
18341870 * to drop the csum leaves for this update from our delayed_refs_rsv.
18351871 */
18361872 if (head->total_ref_mod < 0 && head->is_data) {
1873+ int nr_csums;
1874+
18371875 spin_lock(&delayed_refs->lock);
18381876 delayed_refs->pending_csums -= head->num_bytes;
18391877 spin_unlock(&delayed_refs->lock);
1840− nr_items += btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes);
1878+ nr_csums = btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes);
1879+
1880+ btrfs_delayed_refs_rsv_release(fs_info, 0, nr_csums);
1881+
1882+ return btrfs_calc_delayed_ref_csum_bytes(fs_info, nr_csums);
18411883 }
1884+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE &&
1885+ head->must_insert_reserved && head->is_data)
1886+ btrfs_qgroup_free_refroot(fs_info, head->owning_root,
1887+ head->reserved_bytes, BTRFS_QGROUP_RSV_DATA);
18421888
1843− btrfs_delayed_refs_rsv_release(fs_info, nr_items);
1889+ return 0;
18441890 }
18451891
18461892 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
1847− struct btrfs_delayed_ref_head *head)
1893+ struct btrfs_delayed_ref_head *head,
1894+ u64 *bytes_released)
18481895 {
18491896
18501897 struct btrfs_fs_info *fs_info = trans->fs_info;
@@ -1889,7 +1936,7 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans, · 38 unmodified lines
18891936 }
18901937 }
18911938
1892− btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
1939+ *bytes_released += btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
18931940
18941941 trace_run_delayed_ref_head(fs_info, head, 0);
18951942 btrfs_delayed_ref_unlock(head);
@@ -1931,7 +1978,8 @@ static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( · 35 unmodified lines
19311978 }
19321979
19331980 static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1934− struct btrfs_delayed_ref_head *locked_ref)
1981+ struct btrfs_delayed_ref_head *locked_ref,
1982+ u64 *bytes_released)
19351983 {
19361984 struct btrfs_fs_info *fs_info = trans->fs_info;
19371985 struct btrfs_delayed_ref_root *delayed_refs;
@@ -1985,8 +2033,10 @@ static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, · 47 unmodified lines
19852033 locked_ref->extent_op = NULL;
19862034 spin_unlock(&locked_ref->lock);
19872035
1988− ret = run_one_delayed_ref(trans, ref, extent_op,
2036+ ret = run_one_delayed_ref(trans, locked_ref, ref, extent_op,
19892037 must_insert_reserved);
2038+ btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
2039+ *bytes_released += btrfs_calc_delayed_ref_bytes(fs_info, 1);
19902040
19912041 btrfs_free_delayed_extent_op(extent_op);
19922042 if (ret) {
@@ -2010,15 +2060,22 @@ static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, · 17 unmodified lines
20102060 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
20112061 */
20122062 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2013− unsigned long nr)
2063+ u64 min_bytes)
20142064 {
20152065 struct btrfs_fs_info *fs_info = trans->fs_info;
20162066 struct btrfs_delayed_ref_root *delayed_refs;
20172067 struct btrfs_delayed_ref_head *locked_ref = NULL;
20182068 int ret;
20192069 unsigned long count = 0;
2070+ unsigned long max_count = 0;
2071+ u64 bytes_processed = 0;
20202072
20212073 delayed_refs = &trans->transaction->delayed_refs;
2074+ if (min_bytes == 0) {
2075+ max_count = delayed_refs->num_heads_ready;
2076+ min_bytes = U64_MAX;
2077+ }
2078+
20222079 do {
20232080 if (!locked_ref) {
20242081 locked_ref = btrfs_obtain_ref_head(trans);
@@ -2046,7 +2103,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, · 21 unmodified lines
20462103 spin_lock(&locked_ref->lock);
20472104 btrfs_merge_delayed_refs(fs_info, delayed_refs, locked_ref);
20482105
2049− ret = btrfs_run_delayed_refs_for_head(trans, locked_ref);
2106+ ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, &bytes_processed);
20502107 if (ret < 0 && ret != -EAGAIN) {
20512108 /*
20522109 * Error, btrfs_run_delayed_refs_for_head already
@@ -2058,7 +2115,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, · 5 unmodified lines
20582115 * Success, perform the usual cleanup of a processed
20592116 * head
20602117 */
2061− ret = cleanup_ref_head(trans, locked_ref);
2118+ ret = cleanup_ref_head(trans, locked_ref, &bytes_processed);
20622119 if (ret > 0 ) {
20632120 /* We dropped our lock, we need to loop. */
20642121 ret = 0;
@@ -2075,7 +2132,9 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, · 10 unmodified lines
20752132
20762133 locked_ref = NULL;
20772134 cond_resched();
2078− } while ((nr != -1 && count < nr) || locked_ref);
2135+ } while ((min_bytes != U64_MAX && bytes_processed < min_bytes) ||
2136+ (max_count > 0 && count < max_count) ||
2137+ locked_ref);
20792138
20802139 return 0;
20812140 }
@@ -2124,24 +2183,25 @@ static u64 find_middle(struct rb_root *root) · 42 unmodified lines
21242183 #endif
21252184
21262185 /*
2127− * this starts processing the delayed reference count updates and
2128− * extent insertions we have queued up so far. count can be
2129− * 0, which means to process everything in the tree at the start
2130− * of the run (but not newly added entries), or it can be some target
2131− * number you'd like to process.
2186+ * Start processing the delayed reference count updates and extent insertions
2187+ * we have queued up so far.
2188+ *
2189+ * @trans: Transaction handle.
2190+ * @min_bytes: How many bytes of delayed references to process. After this
2191+ * many bytes we stop processing delayed references if there are
2192+ * any more. If 0 it means to run all existing delayed references,
2193+ * but not new ones added after running all existing ones.
2194+ * Use (u64)-1 (U64_MAX) to run all existing delayed references
2195+ * plus any new ones that are added.
21322196 *
21332197 * Returns 0 on success or if called with an aborted transaction
21342198 * Returns <0 on error and aborts the transaction
21352199 */
2136−int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2137− unsigned long count)
2200+int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, u64 min_bytes)
21382201 {
21392202 struct btrfs_fs_info *fs_info = trans->fs_info;
2140− struct rb_node *node;
21412203 struct btrfs_delayed_ref_root *delayed_refs;
2142− struct btrfs_delayed_ref_head *head;
21432204 int ret;
2144− int run_all = count == (unsigned long)-1;
21452205
21462206 /* We'll clean this up in btrfs_cleanup_transaction */
21472207 if (TRANS_ABORTED(trans))
@@ -2151,42 +2211,30 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, · 3 unmodified lines
21512211 return 0;
21522212
21532213 delayed_refs = &trans->transaction->delayed_refs;
2154− if (count == 0)
2155− count = delayed_refs->num_heads_ready;
2156−
21572214 again:
21582215 #ifdef SCRAMBLE_DELAYED_REFS
21592216 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
21602217 #endif
2161− ret = __btrfs_run_delayed_refs(trans, count);
2218+ ret = __btrfs_run_delayed_refs(trans, min_bytes);
21622219 if (ret < 0) {
21632220 btrfs_abort_transaction(trans, ret);
21642221 return ret;
21652222 }
21662223
2167− if (run_all) {
2224+ if (min_bytes == U64_MAX) {
21682225 btrfs_create_pending_block_groups(trans);
21692226
21702227 spin_lock(&delayed_refs->lock);
2171− node = rb_first_cached(&delayed_refs->href_root);
2172− if (!node) {
2228+ if (RB_EMPTY_ROOT(&delayed_refs->href_root.rb_root)) {
21732229 spin_unlock(&delayed_refs->lock);
2174− goto out;
2230+ return 0;
21752231 }
2176− head = rb_entry(node, struct btrfs_delayed_ref_head,
2177− href_node);
2178− refcount_inc(&head->refs);
21792232 spin_unlock(&delayed_refs->lock);
21802233
2181− /* Mutex was contended, block until it's released and retry. */
2182− mutex_lock(&head->mutex);
2183− mutex_unlock(&head->mutex);
2184−
2185− btrfs_put_delayed_ref_head(head);
21862234 cond_resched();
21872235 goto again;
21882236 }
2189−out:
2237+
21902238 return 0;
21912239 }
21922240
@@ -2311,6 +2359,7 @@ static noinline int check_committed_ref(struct btrfs_root *root, · 118 unmodified lines
23112359 struct btrfs_extent_item *ei;
23122360 struct btrfs_key key;
23132361 u32 item_size;
2362+ u32 expected_size;
23142363 int type;
23152364 int ret;
23162365
@@ -2337,10 +2386,22 @@ static noinline int check_committed_ref(struct btrfs_root *root, · 20 unmodified lines
23372386 ret = 1;
23382387 item_size = btrfs_item_size(leaf, path->slots[0]);
23392388 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2389+ expected_size = sizeof(*ei) + btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY);
2390+
2391+ /* No inline refs; we need to bail before checking for owner ref. */
2392+ if (item_size == sizeof(*ei))
2393+ goto out;
2394+
2395+ /* Check for an owner ref; skip over it to the real inline refs. */
2396+ iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2397+ type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2398+ if (btrfs_fs_incompat(fs_info, SIMPLE_QUOTA) && type == BTRFS_EXTENT_OWNER_REF_KEY) {
2399+ expected_size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY);
2400+ iref = (struct btrfs_extent_inline_ref *)(iref + 1);
2401+ }
23402402
23412403 /* If extent item has more than 1 inline ref then it's shared */
2342− if (item_size != sizeof(*ei) +
2343− btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2404+ if (item_size != expected_size)
23442405 goto out;
23452406
23462407 /*
@@ -2352,8 +2413,6 @@ static noinline int check_committed_ref(struct btrfs_root *root, · 5 unmodified lines
23522413 btrfs_root_last_snapshot(&root->root_item)))
23532414 goto out;
23542415
2355− iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2356−
23572416 /* If this extent has SHARED_DATA_REF then it's shared */
23582417 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
23592418 if (type != BTRFS_EXTENT_DATA_REF_KEY)
@@ -2450,7 +2509,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, · 90 unmodified lines
24502509 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
24512510 key.offset -= btrfs_file_extent_offset(buf, fi);
24522511 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2453− num_bytes, parent);
2512+ num_bytes, parent, ref_root);
24542513 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
24552514 key.offset, root->root_key.objectid,
24562515 for_reloc);
@@ -2463,8 +2522,9 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
24632522 } else {
24642523 bytenr = btrfs_node_blockptr(buf, i);
24652524 num_bytes = fs_info->nodesize;
2525+ /* We don't know the owning_root, use 0. */
24662526 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2467− num_bytes, parent);
2527+ num_bytes, parent, 0);
24682528 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root,
24692529 root->root_key.objectid, for_reloc);
24702530 if (inc)
@@ -2565,16 +2625,13 @@ int btrfs_pin_extent(struct btrfs_trans_handle *trans, · 94 unmodified lines
25652625 return 0;
25662626 }
25672627
2568−/*
2569− * this function must be called within transaction
2570− */
25712628 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
2572− u64 bytenr, u64 num_bytes)
2629+ const struct extent_buffer *eb)
25732630 {
25742631 struct btrfs_block_group *cache;
25752632 int ret;
25762633
2577− cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
2634+ cache = btrfs_lookup_block_group(trans->fs_info, eb->start);
25782635 if (!cache)
25792636 return -EINVAL;
25802637
@@ -2586,10 +2643,10 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, · 5 unmodified lines
25862643 if (ret)
25872644 goto out;
25882645
2589− pin_down_extent(trans, cache, bytenr, num_bytes, 0);
2646+ pin_down_extent(trans, cache, eb->start, eb->len, 0);
25902647
25912648 /* remove us from the free space cache (if we're there at all) */
2592− ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
2649+ ret = btrfs_remove_free_space(cache, eb->start, eb->len);
25932650 out:
25942651 btrfs_put_block_group(cache);
25952652 return ret;
@@ -2844,12 +2901,61 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) · 248 unmodified lines
28442901 return 0;
28452902 }
28462903
2904+/*
2905+ * Parse an extent item's inline extents looking for a simple quotas owner ref.
2906+ *
2907+ * @fs_info: the btrfs_fs_info for this mount
2908+ * @leaf: a leaf in the extent tree containing the extent item
2909+ * @slot: the slot in the leaf where the extent item is found
2910+ *
2911+ * Returns the objectid of the root that originally allocated the extent item
2912+ * if the inline owner ref is expected and present, otherwise 0.
2913+ *
2914+ * If an extent item has an owner ref item, it will be the first inline ref
2915+ * item. Therefore the logic is to check whether there are any inline ref
2916+ * items, then check the type of the first one.
2917+ */
2918+u64 btrfs_get_extent_owner_root(struct btrfs_fs_info *fs_info,
2919+ struct extent_buffer *leaf, int slot)
2920+{
2921+ struct btrfs_extent_item *ei;
2922+ struct btrfs_extent_inline_ref *iref;
2923+ struct btrfs_extent_owner_ref *oref;
2924+ unsigned long ptr;
2925+ unsigned long end;
2926+ int type;
2927+
2928+ if (!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA))
2929+ return 0;
2930+
2931+ ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
2932+ ptr = (unsigned long)(ei + 1);
2933+ end = (unsigned long)ei + btrfs_item_size(leaf, slot);
2934+
2935+ /* No inline ref items of any kind, can't check type. */
2936+ if (ptr == end)
2937+ return 0;
2938+
2939+ iref = (struct btrfs_extent_inline_ref *)ptr;
2940+ type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
2941+
2942+ /* We found an owner ref, get the root out of it. */
2943+ if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
2944+ oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
2945+ return btrfs_extent_owner_ref_root_id(leaf, oref);
2946+ }
2947+
2948+ /* We have inline refs, but not an owner ref. */
2949+ return 0;
2950+}
2951+
28472952 static int do_free_extent_accounting(struct btrfs_trans_handle *trans,
2848− u64 bytenr, u64 num_bytes, bool is_data)
2953+ u64 bytenr, struct btrfs_squota_delta *delta)
28492954 {
28502955 int ret;
2956+ u64 num_bytes = delta->num_bytes;
28512957
2852− if (is_data) {
2958+ if (delta->is_data) {
28532959 struct btrfs_root *csum_root;
28542960
28552961 csum_root = btrfs_csum_root(trans->fs_info, bytenr);
@@ -2858,6 +2964,18 @@ static int do_free_extent_accounting(struct btrfs_trans_handle *trans, · 2 unmodified lines
28582964 btrfs_abort_transaction(trans, ret);
28592965 return ret;
28602966 }
2967+
2968+ ret = btrfs_delete_raid_extent(trans, bytenr, num_bytes);
2969+ if (ret) {
2970+ btrfs_abort_transaction(trans, ret);
2971+ return ret;
2972+ }
2973+ }
2974+
2975+ ret = btrfs_record_squota_delta(trans->fs_info, delta);
2976+ if (ret) {
2977+ btrfs_abort_transaction(trans, ret);
2978+ return ret;
28612979 }
28622980
28632981 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
@@ -2940,9 +3058,10 @@ static int do_free_extent_accounting(struct btrfs_trans_handle *trans, · 76 unmodified lines
29403058 * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.
29413059 */
29423060 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3061+ struct btrfs_delayed_ref_head *href,
29433062 struct btrfs_delayed_ref_node *node, u64 parent,
29443063 u64 root_objectid, u64 owner_objectid,
2945− u64 owner_offset, int refs_to_drop,
3064+ u64 owner_offset,
29463065 struct btrfs_delayed_extent_op *extent_op)
29473066 {
29483067 struct btrfs_fs_info *info = trans->fs_info;
@@ -2957,11 +3076,13 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 8 unmodified lines
29573076 int extent_slot = 0;
29583077 int found_extent = 0;
29593078 int num_to_del = 1;
3079+ int refs_to_drop = node->ref_mod;
29603080 u32 item_size;
29613081 u64 refs;
29623082 u64 bytenr = node->bytenr;
29633083 u64 num_bytes = node->num_bytes;
29643084 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
3085+ u64 delayed_ref_root = href->owning_root;
29653086
29663087 extent_root = btrfs_extent_root(info, bytenr);
29673088 ASSERT(extent_root);
@@ -3151,7 +3272,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 183 unmodified lines
31513272 }
31523273 } else {
31533274 btrfs_set_extent_refs(leaf, ei, refs);
3154− btrfs_mark_buffer_dirty(leaf);
3275+ btrfs_mark_buffer_dirty(trans, leaf);
31553276 }
31563277 if (found_extent) {
31573278 ret = remove_extent_backref(trans, extent_root, path,
@@ -3162,6 +3283,15 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 4 unmodified lines
31623283 }
31633284 }
31643285 } else {
3286+ struct btrfs_squota_delta delta = {
3287+ .root = delayed_ref_root,
3288+ .num_bytes = num_bytes,
3289+ .rsv_bytes = 0,
3290+ .is_data = is_data,
3291+ .is_inc = false,
3292+ .generation = btrfs_extent_generation(leaf, ei),
3293+ };
3294+
31653295 /* In this branch refs == 1 */
31663296 if (found_extent) {
31673297 if (is_data && refs_to_drop !=
@@ -3200,6 +3330,16 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 32 unmodified lines
32003330 num_to_del = 2;
32013331 }
32023332 }
3333+ /*
3334+ * We can't infer the data owner from the delayed ref, so we need
3335+ * to try to get it from the owning ref item.
3336+ *
3337+ * If it is not present, then that extent was not written under
3338+ * simple quotas mode, so we don't need to account for its deletion.
3339+ */
3340+ if (is_data)
3341+ delta.root = btrfs_get_extent_owner_root(trans->fs_info,
3342+ leaf, extent_slot);
32033343
32043344 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
32053345 num_to_del);
@@ -3209,7 +3349,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 3 unmodified lines
32093349 }
32103350 btrfs_release_path(path);
32113351
3212− ret = do_free_extent_accounting(trans, bytenr, num_bytes, is_data);
3352+ ret = do_free_extent_accounting(trans, bytenr, &delta);
32133353 }
32143354 btrfs_release_path(path);
32153355
@@ -3283,7 +3423,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, · 67 unmodified lines
32833423 int ret;
32843424
32853425 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3286− buf->start, buf->len, parent);
3426+ buf->start, buf->len, parent, btrfs_header_owner(buf));
32873427 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
32883428 root_id, 0, false);
32893429
@@ -3370,10 +3510,9 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref) · 80 unmodified lines
33703510 * tree, just update pinning info and exit early.
33713511 */
33723512 if ((ref->type == BTRFS_REF_METADATA &&
3373− ref->tree_ref.owning_root == BTRFS_TREE_LOG_OBJECTID) ||
3513+ ref->tree_ref.ref_root == BTRFS_TREE_LOG_OBJECTID) ||
33743514 (ref->type == BTRFS_REF_DATA &&
3375− ref->data_ref.owning_root == BTRFS_TREE_LOG_OBJECTID)) {
3376− /* unlocks the pinned mutex */
3515+ ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
33773516 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
33783517 ret = 0;
33793518 } else if (ref->type == BTRFS_REF_METADATA) {
@@ -3383,9 +3522,9 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref) · 3 unmodified lines
33833522 }
33843523
33853524 if (!((ref->type == BTRFS_REF_METADATA &&
3386− ref->tree_ref.owning_root == BTRFS_TREE_LOG_OBJECTID) ||
3525+ ref->tree_ref.ref_root == BTRFS_TREE_LOG_OBJECTID) ||
33873526 (ref->type == BTRFS_REF_DATA &&
3388− ref->data_ref.owning_root == BTRFS_TREE_LOG_OBJECTID)))
3527+ ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
33893528 btrfs_ref_tree_mod(fs_info, ref);
33903529
33913530 return ret;
@@ -4442,8 +4581,8 @@ loop: · 1050 unmodified lines
44424581 }
44434582
44444583 /*
4445− * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4446− * hole that is at least as big as @num_bytes.
4584+ * Entry point to the extent allocator. Tries to find a hole that is at least
4585+ * as big as @num_bytes.
44474586 *
44484587 * @root - The root that will contain this extent
44494588 *
@@ -4562,20 +4701,20 @@ int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, · 112 unmodified lines
45624701 return 0;
45634702 }
45644703
4565−int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
4566− u64 len)
4704+int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans,
4705+ const struct extent_buffer *eb)
45674706 {
45684707 struct btrfs_block_group *cache;
45694708 int ret = 0;
45704709
4571− cache = btrfs_lookup_block_group(trans->fs_info, start);
4710+ cache = btrfs_lookup_block_group(trans->fs_info, eb->start);
45724711 if (!cache) {
45734712 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4574− start);
4713+ eb->start);
45754714 return -ENOSPC;
45764715 }
45774716
4578− ret = pin_down_extent(trans, cache, start, len, 1);
4717+ ret = pin_down_extent(trans, cache, eb->start, eb->len, 1);
45794718 btrfs_put_block_group(cache);
45804719 return ret;
45814720 }
@@ -4605,24 +4744,29 @@ static int alloc_reserved_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 23 unmodified lines
46054744 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
46064745 u64 parent, u64 root_objectid,
46074746 u64 flags, u64 owner, u64 offset,
4608− struct btrfs_key *ins, int ref_mod)
4747+ struct btrfs_key *ins, int ref_mod, u64 oref_root)
46094748 {
46104749 struct btrfs_fs_info *fs_info = trans->fs_info;
46114750 struct btrfs_root *extent_root;
46124751 int ret;
46134752 struct btrfs_extent_item *extent_item;
4753+ struct btrfs_extent_owner_ref *oref;
46144754 struct btrfs_extent_inline_ref *iref;
46154755 struct btrfs_path *path;
46164756 struct extent_buffer *leaf;
46174757 int type;
46184758 u32 size;
4759+ const bool simple_quota = (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE);
46194760
46204761 if (parent > 0)
46214762 type = BTRFS_SHARED_DATA_REF_KEY;
46224763 else
46234764 type = BTRFS_EXTENT_DATA_REF_KEY;
46244765
4625− size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4766+ size = sizeof(*extent_item);
4767+ if (simple_quota)
4768+ size += btrfs_extent_inline_ref_size(BTRFS_EXTENT_OWNER_REF_KEY);
4769+ size += btrfs_extent_inline_ref_size(type);
46264770
46274771 path = btrfs_alloc_path();
46284772 if (!path)
@@ -4644,7 +4788,14 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, · 15 unmodified lines
46444788 flags | BTRFS_EXTENT_FLAG_DATA);
46454789
46464790 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4791+ if (simple_quota) {
4792+ btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_EXTENT_OWNER_REF_KEY);
4793+ oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
4794+ btrfs_set_extent_owner_ref_root_id(leaf, oref, oref_root);
4795+ iref = (struct btrfs_extent_inline_ref *)(oref + 1);
4796+ }
46474797 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4798+
46484799 if (parent > 0) {
46494800 struct btrfs_shared_data_ref *ref;
46504801 ref = (struct btrfs_shared_data_ref *)(iref + 1);
@@ -4659,7 +4810,7 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, · 8 unmodified lines
46594810 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
46604811 }
46614812
4662− btrfs_mark_buffer_dirty(path->nodes[0]);
4813+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
46634814 btrfs_free_path(path);
46644815
46654816 return alloc_reserved_extent(trans, ins->objectid, ins->offset);
@@ -4734,7 +4885,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, · 68 unmodified lines
47344885 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
47354886 }
47364887
4737− btrfs_mark_buffer_dirty(leaf);
4888+ btrfs_mark_buffer_dirty(trans, leaf);
47384889 btrfs_free_path(path);
47394890
47404891 return alloc_reserved_extent(trans, node->bytenr, fs_info->nodesize);
@@ -4746,12 +4897,17 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, · 5 unmodified lines
47464897 struct btrfs_key *ins)
47474898 {
47484899 struct btrfs_ref generic_ref = { 0 };
4900+ u64 root_objectid = root->root_key.objectid;
4901+ u64 owning_root = root_objectid;
47494902
4750− BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
4903+ BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
4904+
4905+ if (btrfs_is_data_reloc_root(root) && is_fstree(root->relocation_src_root))
4906+ owning_root = root->relocation_src_root;
47514907
47524908 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4753− ins->objectid, ins->offset, 0);
4754− btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner,
4909+ ins->objectid, ins->offset, 0, owning_root);
4910+ btrfs_init_data_ref(&generic_ref, root_objectid, owner,
47554911 offset, 0, false);
47564912 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
47574913
@@ -4771,6 +4927,14 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, · 13 unmodified lines
47714927 int ret;
47724928 struct btrfs_block_group *block_group;
47734929 struct btrfs_space_info *space_info;
4930+ struct btrfs_squota_delta delta = {
4931+ .root = root_objectid,
4932+ .num_bytes = ins->offset,
4933+ .generation = trans->transid,
4934+ .rsv_bytes = 0,
4935+ .is_data = true,
4936+ .is_inc = true,
4937+ };
47744938
47754939 /*
47764940 * Mixed block groups will exclude before processing the log so we only
@@ -4796,13 +4960,36 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, · 19 unmodified lines
47964960 spin_unlock(&space_info->lock);
47974961
47984962 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4799− offset, ins, 1);
4963+ offset, ins, 1, root_objectid);
48004964 if (ret)
48014965 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
4966+ ret = btrfs_record_squota_delta(fs_info, &delta);
48024967 btrfs_put_block_group(block_group);
48034968 return ret;
48044969 }
48054970
4971+#ifdef CONFIG_BTRFS_DEBUG
4972+/*
4973+ * Extra safety check in case the extent tree is corrupted and extent allocator
4974+ * chooses to use a tree block which is already used and locked.
4975+ */
4976+static bool check_eb_lock_owner(const struct extent_buffer *eb)
4977+{
4978+ if (eb->lock_owner == current->pid) {
4979+ btrfs_err_rl(eb->fs_info,
4980+"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4981+ eb->start, btrfs_header_owner(eb), current->pid);
4982+ return true;
4983+ }
4984+ return false;
4985+}
4986+#else
4987+static bool check_eb_lock_owner(struct extent_buffer *eb)
4988+{
4989+ return false;
4990+}
4991+#endif
4992+
48064993 static struct extent_buffer *
48074994 btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
48084995 u64 bytenr, int level, u64 owner,
@@ -4816,15 +5003,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 7 unmodified lines
48165003 if (IS_ERR(buf))
48175004 return buf;
48185005
4819− /*
4820− * Extra safety check in case the extent tree is corrupted and extent
4821− * allocator chooses to use a tree block which is already used and
4822− * locked.
4823− */
4824− if (buf->lock_owner == current->pid) {
4825− btrfs_err_rl(fs_info,
4826−"tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4827− buf->start, btrfs_header_owner(buf), current->pid);
5006+ if (check_eb_lock_owner(buf)) {
48285007 free_extent_buffer(buf);
48295008 return ERR_PTR(-EUCLEAN);
48305009 }
@@ -4901,6 +5080,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 70 unmodified lines
49015080 const struct btrfs_disk_key *key,
49025081 int level, u64 hint,
49035082 u64 empty_size,
5083+ u64 reloc_src_root,
49045084 enum btrfs_lock_nesting nest)
49055085 {
49065086 struct btrfs_fs_info *fs_info = root->fs_info;
@@ -4913,6 +5093,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 6 unmodified lines
49135093 int ret;
49145094 u32 blocksize = fs_info->nodesize;
49155095 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
5096+ u64 owning_root;
49165097
49175098 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
49185099 if (btrfs_is_testing(fs_info)) {
@@ -4939,11 +5120,13 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 20 unmodified lines
49395120 ret = PTR_ERR(buf);
49405121 goto out_free_reserved;
49415122 }
5123+ owning_root = btrfs_header_owner(buf);
49425124
49435125 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
49445126 if (parent == 0)
49455127 parent = ins.objectid;
49465128 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5129+ owning_root = reloc_src_root;
49475130 } else
49485131 BUG_ON(parent > 0);
49495132
@@ -4963,7 +5146,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 13 unmodified lines
49635146 extent_op->level = level;
49645147
49655148 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4966− ins.objectid, ins.offset, parent);
5149+ ins.objectid, ins.offset, parent, owning_root);
49675150 btrfs_init_tree_ref(&generic_ref, level, root_objectid,
49685151 root->root_key.objectid, false);
49695152 btrfs_ref_tree_mod(fs_info, &generic_ref);
@@ -5384,7 +5567,8 @@ skip: · 414 unmodified lines
53845567 find_next_key(path, level, &wc->drop_progress);
53855568
53865569 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
5387− fs_info->nodesize, parent);
5570+ fs_info->nodesize, parent,
5571+ btrfs_header_owner(next));
53885572 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid,
53895573 0, false);
53905574 ret = btrfs_free_extent(trans, &ref);
fs/btrfs/extent-tree.h+10−5
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 #include "block-group.h"
88
99 struct btrfs_free_cluster;
10+struct btrfs_delayed_ref_head;
1011
1112 enum btrfs_extent_allocation_policy {
1213 BTRFS_EXTENT_ALLOC_CLUSTERED,
@@ -91,8 +92,8 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, · 78 unmodified lines
9192 enum btrfs_inline_ref_type is_data);
9293 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
9394
94−int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, unsigned long count);
95−void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
95+int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, u64 min_bytes);
96+u64 btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
9697 struct btrfs_delayed_ref_root *delayed_refs,
9798 struct btrfs_delayed_ref_head *head);
9899 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
@@ -102,7 +103,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, · 3 unmodified lines
102103 int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num,
103104 int reserved);
104105 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
105− u64 bytenr, u64 num_bytes);
106+ const struct extent_buffer *eb);
106107 int btrfs_exclude_logged_extents(struct extent_buffer *eb);
107108 int btrfs_cross_ref_exist(struct btrfs_root *root,
108109 u64 objectid, u64 offset, u64 bytenr, bool strict,
@@ -113,6 +114,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 4 unmodified lines
113114 const struct btrfs_disk_key *key,
114115 int level, u64 hint,
115116 u64 empty_size,
117+ u64 reloc_src_root,
116118 enum btrfs_lock_nesting nest);
117119 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
118120 u64 root_id,
@@ -136,12 +138,15 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, · 17 unmodified lines
136138 struct extent_buffer *eb, u64 flags);
137139 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
138140
141+u64 btrfs_get_extent_owner_root(struct btrfs_fs_info *fs_info,
142+ struct extent_buffer *leaf, int slot);
139143 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
140144 u64 start, u64 len, int delalloc);
141−int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start, u64 len);
145+int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans,
146+ const struct extent_buffer *eb);
142147 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
143148 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, struct btrfs_ref *generic_ref);
144−int __must_check btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,
149+int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,
145150 int for_reloc);
146151 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
147152 struct btrfs_root *root,
fs/btrfs/extent_io.c+23−16
@@ -21,7 +21,6 @@ · 20 unmodified lines
2121 #include "ctree.h"
2222 #include "btrfs_inode.h"
2323 #include "bio.h"
24−#include "check-integrity.h"
2524 #include "locking.h"
2625 #include "rcu-string.h"
2726 #include "backref.h"
@@ -395,7 +394,7 @@ again: · 367 unmodified lines
395394
396395 /* then test to make sure it is all still delalloc */
397396 ret = test_range_bit(tree, delalloc_start, delalloc_end,
398− EXTENT_DELALLOC, 1, cached_state);
397+ EXTENT_DELALLOC, cached_state);
399398 if (!ret) {
400399 unlock_extent(tree, delalloc_start, delalloc_end,
401400 &cached_state);
@@ -2294,7 +2293,7 @@ static int try_release_extent_state(struct extent_io_tree *tree, · 1892 unmodified lines
22942293 u64 end = start + PAGE_SIZE - 1;
22952294 int ret = 1;
22962295
2297− if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {
2296+ if (test_range_bit_exists(tree, start, end, EXTENT_LOCKED)) {
22982297 ret = 0;
22992298 } else {
23002299 u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM |
@@ -2353,9 +2352,9 @@ int try_release_extent_mapping(struct page *page, gfp_t mask) · 52 unmodified lines
23532352 free_extent_map(em);
23542353 break;
23552354 }
2356− if (test_range_bit(tree, em->start,
2357− extent_map_end(em) - 1,
2358− EXTENT_LOCKED, 0, NULL))
2355+ if (test_range_bit_exists(tree, em->start,
2356+ extent_map_end(em) - 1,
2357+ EXTENT_LOCKED))
23592358 goto next;
23602359 /*
23612360 * If it's not in the list of modified extents, used
@@ -3455,6 +3454,12 @@ static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) · 1093 unmodified lines
34553454 start, fs_info->nodesize);
34563455 return -EINVAL;
34573456 }
3457+ if (!IS_ALIGNED(start, fs_info->nodesize) &&
3458+ !test_and_set_bit(BTRFS_FS_UNALIGNED_TREE_BLOCK, &fs_info->flags)) {
3459+ btrfs_warn(fs_info,
3460+"tree block not nodesize aligned, start %llu nodesize %u, can be resolved by a full metadata balance",
3461+ start, fs_info->nodesize);
3462+ }
34583463 return 0;
34593464 }
34603465
@@ -4248,14 +4253,14 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 787 unmodified lines
42484253 }
42494254
42504255 /*
4251− * eb_bitmap_offset() - calculate the page and offset of the byte containing the
4252− * given bit number
4253− * @eb: the extent buffer
4254− * @start: offset of the bitmap item in the extent buffer
4255− * @nr: bit number
4256− * @page_index: return index of the page in the extent buffer that contains the
4257− * given bit number
4258− * @page_offset: return offset into the page given by page_index
4256+ * Calculate the page and offset of the byte containing the given bit number.
4257+ *
4258+ * @eb: the extent buffer
4259+ * @start: offset of the bitmap item in the extent buffer
4260+ * @nr: bit number
4261+ * @page_index: return index of the page in the extent buffer that contains
4262+ * the given bit number
4263+ * @page_offset: return offset into the page given by page_index
42594264 *
42604265 * This helper hides the ugliness of finding the byte in an extent buffer which
42614266 * contains a given bit.
@@ -4614,7 +4619,8 @@ int try_release_extent_buffer(struct page *page) · 352 unmodified lines
46144619 }
46154620
46164621 /*
4617− * btrfs_readahead_tree_block - attempt to readahead a child block
4622+ * Attempt to readahead a child block.
4623+ *
46184624 * @fs_info: the fs_info
46194625 * @bytenr: bytenr to read
46204626 * @owner_root: objectid of the root that owns this eb
@@ -4653,7 +4659,8 @@ void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, · 32 unmodified lines
46534659 }
46544660
46554661 /*
4656− * btrfs_readahead_node_child - readahead a node's child block
4662+ * Readahead a node's child block.
4663+ *
46574664 * @node: parent node we're reading from
46584665 * @slot: slot in the parent node for the child we want to read
46594666 *
fs/btrfs/extent_io.h+2−2
@@ -80,16 +80,16 @@ struct extent_buffer { · 79 unmodified lines
8080 spinlock_t refs_lock;
8181 atomic_t refs;
8282 int read_mirror;
83− struct rcu_head rcu_head;
84− pid_t lock_owner;
8583 /* >= 0 if eb belongs to a log tree, -1 otherwise */
8684 s8 log_index;
85+ struct rcu_head rcu_head;
8786
8887 struct rw_semaphore lock;
8988
9089 struct page *pages[INLINE_EXTENT_BUFFER_PAGES];
9190 #ifdef CONFIG_BTRFS_DEBUG
9291 struct list_head leak_list;
92+ pid_t lock_owner;
9393 #endif
9494 };
9595
fs/btrfs/file-item.c+9−8
@@ -194,7 +194,7 @@ int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans, · 193 unmodified lines
194194 btrfs_set_file_extent_encryption(leaf, item, 0);
195195 btrfs_set_file_extent_other_encoding(leaf, item, 0);
196196
197− btrfs_mark_buffer_dirty(leaf);
197+ btrfs_mark_buffer_dirty(trans, leaf);
198198 out:
199199 btrfs_free_path(path);
200200 return ret;
@@ -811,11 +811,12 @@ blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio) · 610 unmodified lines
811811 * This calls btrfs_truncate_item with the correct args based on the overlap,
812812 * and fixes up the key as required.
813813 */
814−static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
814+static noinline void truncate_one_csum(struct btrfs_trans_handle *trans,
815815 struct btrfs_path *path,
816816 struct btrfs_key *key,
817817 u64 bytenr, u64 len)
818818 {
819+ struct btrfs_fs_info *fs_info = trans->fs_info;
819820 struct extent_buffer *leaf;
820821 const u32 csum_size = fs_info->csum_size;
821822 u64 csum_end;
@@ -836,7 +837,7 @@ static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, · 14 unmodified lines
836837 */
837838 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
838839 new_size *= csum_size;
839− btrfs_truncate_item(path, new_size, 1);
840+ btrfs_truncate_item(trans, path, new_size, 1);
840841 } else if (key->offset >= bytenr && csum_end > end_byte &&
841842 end_byte > key->offset) {
842843 /*
@@ -848,10 +849,10 @@ static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, · 5 unmodified lines
848849 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
849850 new_size *= csum_size;
850851
851− btrfs_truncate_item(path, new_size, 0);
852+ btrfs_truncate_item(trans, path, new_size, 0);
852853
853854 key->offset = end_byte;
854− btrfs_set_item_key_safe(fs_info, path, key);
855+ btrfs_set_item_key_safe(trans, path, key);
855856 } else {
856857 BUG();
857858 }
@@ -994,7 +995,7 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, · 136 unmodified lines
994995
995996 key.offset = end_byte - 1;
996997 } else {
997− truncate_one_csum(fs_info, path, &key, bytenr, len);
998+ truncate_one_csum(trans, path, &key, bytenr, len);
998999 if (key.offset < bytenr)
9991000 break;
10001001 }
@@ -1202,7 +1203,7 @@ extend_csum: · 201 unmodified lines
12021203 diff /= csum_size;
12031204 diff *= csum_size;
12041205
1205− btrfs_extend_item(path, diff);
1206+ btrfs_extend_item(trans, path, diff);
12061207 ret = 0;
12071208 goto csum;
12081209 }
@@ -1249,7 +1250,7 @@ found: · 40 unmodified lines
12491250 ins_size /= csum_size;
12501251 total_bytes += ins_size * fs_info->sectorsize;
12511252
1252− btrfs_mark_buffer_dirty(path->nodes[0]);
1253+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
12531254 if (total_bytes < sums->len) {
12541255 btrfs_release_path(path);
12551256 cond_resched();
fs/btrfs/file.c+32−29
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 #include <linux/uio.h>
1818 #include <linux/iversion.h>
1919 #include <linux/fsverity.h>
20+#include <linux/iomap.h>
2021 #include "ctree.h"
2122 #include "disk-io.h"
2223 #include "transaction.h"
@@ -368,12 +369,13 @@ next_slot: · 345 unmodified lines
368369 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
369370 btrfs_set_file_extent_num_bytes(leaf, fi,
370371 extent_end - args->start);
371− btrfs_mark_buffer_dirty(leaf);
372+ btrfs_mark_buffer_dirty(trans, leaf);
372373
373374 if (update_refs && disk_bytenr > 0) {
374375 btrfs_init_generic_ref(&ref,
375376 BTRFS_ADD_DELAYED_REF,
376− disk_bytenr, num_bytes, 0);
377+ disk_bytenr, num_bytes, 0,
378+ root->root_key.objectid);
377379 btrfs_init_data_ref(&ref,
378380 root->root_key.objectid,
379381 new_key.objectid,
@@ -405,13 +407,13 @@ next_slot: · 25 unmodified lines
405407
406408 memcpy(&new_key, &key, sizeof(new_key));
407409 new_key.offset = args->end;
408− btrfs_set_item_key_safe(fs_info, path, &new_key);
410+ btrfs_set_item_key_safe(trans, path, &new_key);
409411
410412 extent_offset += args->end - key.offset;
411413 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
412414 btrfs_set_file_extent_num_bytes(leaf, fi,
413415 extent_end - args->end);
414− btrfs_mark_buffer_dirty(leaf);
416+ btrfs_mark_buffer_dirty(trans, leaf);
415417 if (update_refs && disk_bytenr > 0)
416418 args->bytes_found += args->end - key.offset;
417419 break;
@@ -431,7 +433,7 @@ next_slot: · 13 unmodified lines
431433
432434 btrfs_set_file_extent_num_bytes(leaf, fi,
433435 args->start - key.offset);
434− btrfs_mark_buffer_dirty(leaf);
436+ btrfs_mark_buffer_dirty(trans, leaf);
435437 if (update_refs && disk_bytenr > 0)
436438 args->bytes_found += extent_end - args->start;
437439 if (args->end == extent_end)
@@ -463,7 +465,8 @@ delete_extent_item: · 25 unmodified lines
463465 } else if (update_refs && disk_bytenr > 0) {
464466 btrfs_init_generic_ref(&ref,
465467 BTRFS_DROP_DELAYED_REF,
466− disk_bytenr, num_bytes, 0);
468+ disk_bytenr, num_bytes, 0,
469+ root->root_key.objectid);
467470 btrfs_init_data_ref(&ref,
468471 root->root_key.objectid,
469472 key.objectid,
@@ -536,7 +539,8 @@ delete_extent_item: · 66 unmodified lines
536539 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
537540 path->slots[0]++;
538541 }
539− btrfs_setup_item_for_insert(root, path, &key, args->extent_item_size);
542+ btrfs_setup_item_for_insert(trans, root, path, &key,
543+ args->extent_item_size);
540544 args->extent_inserted = true;
541545 }
542546
@@ -593,7 +597,6 @@ static int extent_mergeable(struct extent_buffer *leaf, int slot, · 50 unmodified lines
593597 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
594598 struct btrfs_inode *inode, u64 start, u64 end)
595599 {
596− struct btrfs_fs_info *fs_info = trans->fs_info;
597600 struct btrfs_root *root = inode->root;
598601 struct extent_buffer *leaf;
599602 struct btrfs_path *path;
@@ -664,7 +667,7 @@ again: · 64 unmodified lines
664667 ino, bytenr, orig_offset,
665668 &other_start, &other_end)) {
666669 new_key.offset = end;
667− btrfs_set_item_key_safe(fs_info, path, &new_key);
670+ btrfs_set_item_key_safe(trans, path, &new_key);
668671 fi = btrfs_item_ptr(leaf, path->slots[0],
669672 struct btrfs_file_extent_item);
670673 btrfs_set_file_extent_generation(leaf, fi,
@@ -679,7 +682,7 @@ again: · 8 unmodified lines
679682 trans->transid);
680683 btrfs_set_file_extent_num_bytes(leaf, fi,
681684 end - other_start);
682− btrfs_mark_buffer_dirty(leaf);
685+ btrfs_mark_buffer_dirty(trans, leaf);
683686 goto out;
684687 }
685688 }
@@ -698,7 +701,7 @@ again: · 12 unmodified lines
698701 trans->transid);
699702 path->slots[0]++;
700703 new_key.offset = start;
701− btrfs_set_item_key_safe(fs_info, path, &new_key);
704+ btrfs_set_item_key_safe(trans, path, &new_key);
702705
703706 fi = btrfs_item_ptr(leaf, path->slots[0],
704707 struct btrfs_file_extent_item);
@@ -708,7 +711,7 @@ again: · 3 unmodified lines
708711 other_end - start);
709712 btrfs_set_file_extent_offset(leaf, fi,
710713 start - orig_offset);
711− btrfs_mark_buffer_dirty(leaf);
714+ btrfs_mark_buffer_dirty(trans, leaf);
712715 goto out;
713716 }
714717 }
@@ -742,10 +745,10 @@ again: · 27 unmodified lines
742745 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
743746 btrfs_set_file_extent_num_bytes(leaf, fi,
744747 extent_end - split);
745− btrfs_mark_buffer_dirty(leaf);
748+ btrfs_mark_buffer_dirty(trans, leaf);
746749
747750 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,
748− num_bytes, 0);
751+ num_bytes, 0, root->root_key.objectid);
749752 btrfs_init_data_ref(&ref, root->root_key.objectid, ino,
750753 orig_offset, 0, false);
751754 ret = btrfs_inc_extent_ref(trans, &ref);
@@ -771,7 +774,7 @@ again: · 19 unmodified lines
771774 other_start = end;
772775 other_end = 0;
773776 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
774− num_bytes, 0);
777+ num_bytes, 0, root->root_key.objectid);
775778 btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset,
776779 0, false);
777780 if (extent_mergeable(leaf, path->slots[0] + 1,
@@ -814,7 +817,7 @@ again: · 36 unmodified lines
814817 btrfs_set_file_extent_type(leaf, fi,
815818 BTRFS_FILE_EXTENT_REG);
816819 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
817− btrfs_mark_buffer_dirty(leaf);
820+ btrfs_mark_buffer_dirty(trans, leaf);
818821 } else {
819822 fi = btrfs_item_ptr(leaf, del_slot - 1,
820823 struct btrfs_file_extent_item);
@@ -823,7 +826,7 @@ again: · 2 unmodified lines
823826 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
824827 btrfs_set_file_extent_num_bytes(leaf, fi,
825828 extent_end - key.offset);
826− btrfs_mark_buffer_dirty(leaf);
829+ btrfs_mark_buffer_dirty(trans, leaf);
827830
828831 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
829832 if (ret < 0) {
@@ -1747,7 +1750,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx) · 917 unmodified lines
17471750 struct btrfs_inode *inode = BTRFS_I(ctx->inode);
17481751 struct btrfs_fs_info *fs_info = inode->root->fs_info;
17491752
1750− if (btrfs_inode_in_log(inode, fs_info->generation) &&
1753+ if (btrfs_inode_in_log(inode, btrfs_get_fs_generation(fs_info)) &&
17511754 list_empty(&ctx->ordered_extents))
17521755 return true;
17531756
@@ -1758,7 +1761,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx) · 4 unmodified lines
17581761 * and for a fast fsync we don't wait for that, we only wait for the
17591762 * writeback to complete.
17601763 */
1761− if (inode->last_trans <= fs_info->last_trans_committed &&
1764+ if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
17621765 (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
17631766 list_empty(&ctx->ordered_extents)))
17641767 return true;
@@ -1887,7 +1890,6 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) · 122 unmodified lines
18871890
18881891 atomic_inc(&root->log_batch);
18891892
1890− smp_mb();
18911893 if (skip_inode_logging(&ctx)) {
18921894 /*
18931895 * We've had everything committed since the last time we were
@@ -2105,7 +2107,7 @@ static int fill_holes(struct btrfs_trans_handle *trans, · 211 unmodified lines
21052107 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
21062108 btrfs_set_file_extent_offset(leaf, fi, 0);
21072109 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2108− btrfs_mark_buffer_dirty(leaf);
2110+ btrfs_mark_buffer_dirty(trans, leaf);
21092111 goto out;
21102112 }
21112113
@@ -2113,7 +2115,7 @@ static int fill_holes(struct btrfs_trans_handle *trans, · 1 unmodified line
21132115 u64 num_bytes;
21142116
21152117 key.offset = offset;
2116− btrfs_set_item_key_safe(fs_info, path, &key);
2118+ btrfs_set_item_key_safe(trans, path, &key);
21172119 fi = btrfs_item_ptr(leaf, path->slots[0],
21182120 struct btrfs_file_extent_item);
21192121 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
@@ -2122,7 +2124,7 @@ static int fill_holes(struct btrfs_trans_handle *trans, · 2 unmodified lines
21222124 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
21232125 btrfs_set_file_extent_offset(leaf, fi, 0);
21242126 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2125− btrfs_mark_buffer_dirty(leaf);
2127+ btrfs_mark_buffer_dirty(trans, leaf);
21262128 goto out;
21272129 }
21282130 btrfs_release_path(path);
@@ -2274,7 +2276,7 @@ static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans, · 145 unmodified lines
22742276 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
22752277 if (extent_info->is_new_extent)
22762278 btrfs_set_file_extent_generation(leaf, extent, trans->transid);
2277− btrfs_mark_buffer_dirty(leaf);
2279+ btrfs_mark_buffer_dirty(trans, leaf);
22782280 btrfs_release_path(path);
22792281
22802282 ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,
@@ -2304,7 +2306,8 @@ static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans, · 23 unmodified lines
23042306
23052307 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
23062308 extent_info->disk_offset,
2307− extent_info->disk_len, 0);
2309+ extent_info->disk_len, 0,
2310+ root->root_key.objectid);
23082311 ref_offset = extent_info->file_offset - extent_info->data_offset;
23092312 btrfs_init_data_ref(&ref, root->root_key.objectid,
23102313 btrfs_ino(inode), ref_offset, 0, false);
@@ -2477,7 +2480,7 @@ int btrfs_replace_file_extents(struct btrfs_inode *inode, · 166 unmodified lines
24772480 inode_set_mtime_to_ts(&inode->vfs_inode,
24782481 inode_set_ctime_current(&inode->vfs_inode));
24792482
2480− ret = btrfs_update_inode(trans, root, inode);
2483+ ret = btrfs_update_inode(trans, inode);
24812484 if (ret)
24822485 break;
24832486
@@ -2717,7 +2720,7 @@ static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len) · 233 unmodified lines
27172720 ASSERT(trans != NULL);
27182721 inode_inc_iversion(inode);
27192722 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2720− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
2723+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
27212724 updated_inode = true;
27222725 btrfs_end_transaction(trans);
27232726 btrfs_btree_balance_dirty(fs_info);
@@ -2743,7 +2746,7 @@ out_only_mutex: · 19 unmodified lines
27432746 } else {
27442747 int ret2;
27452748
2746− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
2749+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
27472750 ret2 = btrfs_end_transaction(trans);
27482751 if (!ret)
27492752 ret = ret2;
@@ -2810,7 +2813,7 @@ static int btrfs_fallocate_update_isize(struct inode *inode, · 60 unmodified lines
28102813 inode_set_ctime_current(inode);
28112814 i_size_write(inode, end);
28122815 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
2813− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
2816+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
28142817 ret2 = btrfs_end_transaction(trans);
28152818
28162819 return ret ? ret : ret2;
fs/btrfs/free-space-cache.c+16−12
@@ -57,6 +57,11 @@ static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, · 56 unmodified lines
5757 struct btrfs_free_space *info, u64 offset,
5858 u64 bytes, bool update_stats);
5959
60+static void btrfs_crc32c_final(u32 crc, u8 *result)
61+{
62+ put_unaligned_le32(~crc, result);
63+}
64+
6065 static void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
6166 {
6267 struct btrfs_free_space *info;
@@ -195,7 +200,7 @@ static int __create_free_space_inode(struct btrfs_root *root, · 132 unmodified lines
195200 btrfs_set_inode_nlink(leaf, inode_item, 1);
196201 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
197202 btrfs_set_inode_block_group(leaf, inode_item, offset);
198− btrfs_mark_buffer_dirty(leaf);
203+ btrfs_mark_buffer_dirty(trans, leaf);
199204 btrfs_release_path(path);
200205
201206 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
@@ -213,7 +218,7 @@ static int __create_free_space_inode(struct btrfs_root *root, · 11 unmodified lines
213218 struct btrfs_free_space_header);
214219 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
215220 btrfs_set_free_space_key(leaf, header, &disk_key);
216− btrfs_mark_buffer_dirty(leaf);
221+ btrfs_mark_buffer_dirty(trans, leaf);
217222 btrfs_release_path(path);
218223
219224 return 0;
@@ -354,7 +359,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans, · 134 unmodified lines
354359 if (ret)
355360 goto fail;
356361
357− ret = btrfs_update_inode(trans, root, inode);
362+ ret = btrfs_update_inode(trans, inode);
358363
359364 fail:
360365 if (locked)
@@ -540,7 +545,7 @@ static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index) · 179 unmodified lines
540545 if (index == 0)
541546 offset = sizeof(u32) * io_ctl->num_pages;
542547
543− crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
548+ crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
544549 btrfs_crc32c_final(crc, (u8 *)&crc);
545550 io_ctl_unmap_page(io_ctl);
546551 tmp = page_address(io_ctl->pages[0]);
@@ -562,7 +567,7 @@ static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index) · 15 unmodified lines
562567 val = *tmp;
563568
564569 io_ctl_map_page(io_ctl, 0);
565− crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
570+ crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
566571 btrfs_crc32c_final(crc, (u8 *)&crc);
567572 if (val != crc) {
568573 btrfs_err_rl(io_ctl->fs_info,
@@ -1185,7 +1190,7 @@ update_cache_item(struct btrfs_trans_handle *trans, · 616 unmodified lines
11851190 btrfs_set_free_space_entries(leaf, header, entries);
11861191 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
11871192 btrfs_set_free_space_generation(leaf, header, trans->transid);
1188− btrfs_mark_buffer_dirty(leaf);
1193+ btrfs_mark_buffer_dirty(trans, leaf);
11891194 btrfs_release_path(path);
11901195
11911196 return 0;
@@ -1321,7 +1326,7 @@ out: · 129 unmodified lines
13211326 "failed to write free space cache for block group %llu error %d",
13221327 block_group->start, ret);
13231328 }
1324− btrfs_update_inode(trans, root, BTRFS_I(inode));
1329+ btrfs_update_inode(trans, BTRFS_I(inode));
13251330
13261331 if (block_group) {
13271332 /* the dirty list is protected by the dirty_bgs_lock */
@@ -1362,7 +1367,6 @@ int btrfs_wait_cache_io(struct btrfs_trans_handle *trans, · 34 unmodified lines
13621367 /*
13631368 * Write out cached info to an inode.
13641369 *
1365− * @root: root the inode belongs to
13661370 * @inode: freespace inode we are writing out
13671371 * @ctl: free space cache we are going to write out
13681372 * @block_group: block_group for this cache if it belongs to a block_group
@@ -1373,7 +1377,7 @@ int btrfs_wait_cache_io(struct btrfs_trans_handle *trans, · 4 unmodified lines
13731377 * on mount. This will return 0 if it was successful in writing the cache out,
13741378 * or an errno if it was not.
13751379 */
1376−static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1380+static int __btrfs_write_out_cache(struct inode *inode,
13771381 struct btrfs_free_space_ctl *ctl,
13781382 struct btrfs_block_group *block_group,
13791383 struct btrfs_io_ctl *io_ctl,
@@ -1506,7 +1510,7 @@ out: · 126 unmodified lines
15061510 invalidate_inode_pages2(inode->i_mapping);
15071511 BTRFS_I(inode)->generation = 0;
15081512 }
1509− btrfs_update_inode(trans, root, BTRFS_I(inode));
1513+ btrfs_update_inode(trans, BTRFS_I(inode));
15101514 if (must_iput)
15111515 iput(inode);
15121516 return ret;
@@ -1532,8 +1536,8 @@ int btrfs_write_out_cache(struct btrfs_trans_handle *trans, · 19 unmodified lines
15321536 if (IS_ERR(inode))
15331537 return 0;
15341538
1535− ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1536− block_group, &block_group->io_ctl, trans);
1539+ ret = __btrfs_write_out_cache(inode, ctl, block_group,
1540+ &block_group->io_ctl, trans);
15371541 if (ret) {
15381542 btrfs_debug(fs_info,
15391543 "failed to write free space cache for block group %llu error %d",
fs/btrfs/free-space-tree.c+9−8
@@ -89,7 +89,7 @@ static int add_new_free_space_info(struct btrfs_trans_handle *trans, · 88 unmodified lines
8989 struct btrfs_free_space_info);
9090 btrfs_set_free_space_extent_count(leaf, info, 0);
9191 btrfs_set_free_space_flags(leaf, info, 0);
92− btrfs_mark_buffer_dirty(leaf);
92+ btrfs_mark_buffer_dirty(trans, leaf);
9393
9494 ret = 0;
9595 out:
@@ -287,7 +287,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, · 191 unmodified lines
287287 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
288288 btrfs_set_free_space_flags(leaf, info, flags);
289289 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
290− btrfs_mark_buffer_dirty(leaf);
290+ btrfs_mark_buffer_dirty(trans, leaf);
291291 btrfs_release_path(path);
292292
293293 if (extent_count != expected_extent_count) {
@@ -324,7 +324,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, · 30 unmodified lines
324324 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
325325 write_extent_buffer(leaf, bitmap_cursor, ptr,
326326 data_size);
327− btrfs_mark_buffer_dirty(leaf);
327+ btrfs_mark_buffer_dirty(trans, leaf);
328328 btrfs_release_path(path);
329329
330330 i += extent_size;
@@ -430,7 +430,7 @@ int convert_free_space_to_extents(struct btrfs_trans_handle *trans, · 99 unmodified lines
430430 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
431431 btrfs_set_free_space_flags(leaf, info, flags);
432432 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
433− btrfs_mark_buffer_dirty(leaf);
433+ btrfs_mark_buffer_dirty(trans, leaf);
434434 btrfs_release_path(path);
435435
436436 nrbits = block_group->length >> block_group->fs_info->sectorsize_bits;
@@ -495,7 +495,7 @@ static int update_free_space_extent_count(struct btrfs_trans_handle *trans, · 58 unmodified lines
495495
496496 extent_count += new_extents;
497497 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
498− btrfs_mark_buffer_dirty(path->nodes[0]);
498+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
499499 btrfs_release_path(path);
500500
501501 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
@@ -533,7 +533,8 @@ int free_space_test_bit(struct btrfs_block_group *block_group, · 31 unmodified lines
533533 return !!extent_buffer_test_bit(leaf, ptr, i);
534534 }
535535
536−static void free_space_set_bits(struct btrfs_block_group *block_group,
536+static void free_space_set_bits(struct btrfs_trans_handle *trans,
537+ struct btrfs_block_group *block_group,
537538 struct btrfs_path *path, u64 *start, u64 *size,
538539 int bit)
539540 {
@@ -563,7 +564,7 @@ static void free_space_set_bits(struct btrfs_block_group *block_group, · 23 unmodified lines
563564 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
564565 else
565566 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
566− btrfs_mark_buffer_dirty(leaf);
567+ btrfs_mark_buffer_dirty(trans, leaf);
567568
568569 *size -= end - *start;
569570 *start = end;
@@ -656,7 +657,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, · 86 unmodified lines
656657 cur_start = start;
657658 cur_size = size;
658659 while (1) {
659− free_space_set_bits(block_group, path, &cur_start, &cur_size,
660+ free_space_set_bits(trans, block_group, path, &cur_start, &cur_size,
660661 !remove);
661662 if (cur_size == 0)
662663 break;
fs/btrfs/fs.h+52−17
@@ -139,6 +139,12 @@ enum { · 138 unmodified lines
139139 */
140140 BTRFS_FS_FEATURE_CHANGED,
141141
142+ /*
143+ * Indicate that we have found a tree block which is only aligned to
144+ * sectorsize, but not to nodesize. This should be rare nowadays.
145+ */
146+ BTRFS_FS_UNALIGNED_TREE_BLOCK,
147+
142148 #if BITS_PER_LONG == 32
143149 /* Indicate if we have error/warn message printed on 32bit systems */
144150 BTRFS_FS_32BIT_ERROR,
@@ -171,19 +177,17 @@ enum { · 26 unmodified lines
171177 BTRFS_MOUNT_AUTO_DEFRAG = (1UL << 16),
172178 BTRFS_MOUNT_USEBACKUPROOT = (1UL << 17),
173179 BTRFS_MOUNT_SKIP_BALANCE = (1UL << 18),
174− BTRFS_MOUNT_CHECK_INTEGRITY = (1UL << 19),
175− BTRFS_MOUNT_CHECK_INTEGRITY_DATA = (1UL << 20),
176− BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 21),
177− BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 22),
178− BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 23),
179− BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 24),
180− BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 25),
181− BTRFS_MOUNT_NOLOGREPLAY = (1UL << 26),
182− BTRFS_MOUNT_REF_VERIFY = (1UL << 27),
183− BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),
184− BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),
185− BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
186− BTRFS_MOUNT_NODISCARD = (1UL << 31),
180+ BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 19),
181+ BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 20),
182+ BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 21),
183+ BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 22),
184+ BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 23),
185+ BTRFS_MOUNT_NOLOGREPLAY = (1UL << 24),
186+ BTRFS_MOUNT_REF_VERIFY = (1UL << 25),
187+ BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 26),
188+ BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 27),
189+ BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 28),
190+ BTRFS_MOUNT_NODISCARD = (1UL << 29),
187191 };
188192
189193 /*
@@ -216,7 +220,8 @@ enum { · 26 unmodified lines
216220 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
217221 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
218222 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
219− BTRFS_FEATURE_INCOMPAT_ZONED)
223+ BTRFS_FEATURE_INCOMPAT_ZONED | \
224+ BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA)
220225
221226 #ifdef CONFIG_BTRFS_DEBUG
222227 /*
@@ -225,6 +230,7 @@ enum { · 2 unmodified lines
225230 */
226231 #define BTRFS_FEATURE_INCOMPAT_SUPP \
227232 (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE | \
233+ BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE | \
228234 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
229235
230236 #else
@@ -369,6 +375,7 @@ struct btrfs_fs_info { · 138 unmodified lines
369375 struct btrfs_root *uuid_root;
370376 struct btrfs_root *data_reloc_root;
371377 struct btrfs_root *block_group_root;
378+ struct btrfs_root *stripe_root;
372379
373380 /* The log root tree is a directory of all the other log roots */
374381 struct btrfs_root *log_root_tree;
@@ -409,7 +416,17 @@ struct btrfs_fs_info { · 34 unmodified lines
409416
410417 struct btrfs_block_rsv empty_block_rsv;
411418
419+ /*
420+ * Updated while holding the lock 'trans_lock'. Due to the life cycle of
421+ * a transaction, it can be directly read while holding a transaction
422+ * handle, everywhere else must be read with btrfs_get_fs_generation().
423+ * Should always be updated using btrfs_set_fs_generation().
424+ */
412425 u64 generation;
426+ /*
427+ * Always use btrfs_get_last_trans_committed() and
428+ * btrfs_set_last_trans_committed() to read and update this field.
429+ */
413430 u64 last_trans_committed;
414431 /*
415432 * Generation of the last transaction used for block group relocation
@@ -645,9 +662,6 @@ struct btrfs_fs_info { · 229 unmodified lines
645662
646663 struct btrfs_discard_ctl discard_ctl;
647664
648−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
649− u32 check_integrity_print_mask;
650−#endif
651665 /* Is qgroup tracking in a consistent state? */
652666 u64 qgroup_flags;
653667
@@ -683,6 +697,7 @@ struct btrfs_fs_info { · 29 unmodified lines
683697 /* Protected by qgroup_rescan_lock */
684698 bool qgroup_rescan_running;
685699 u8 qgroup_drop_subtree_thres;
700+ u64 qgroup_enable_gen;
686701
687702 /*
688703 * If this is not 0, then it indicates a serious filesystem error has
@@ -812,6 +827,26 @@ struct btrfs_fs_info { · 123 unmodified lines
812827 #endif
813828 };
814829
830+static inline u64 btrfs_get_fs_generation(const struct btrfs_fs_info *fs_info)
831+{
832+ return READ_ONCE(fs_info->generation);
833+}
834+
835+static inline void btrfs_set_fs_generation(struct btrfs_fs_info *fs_info, u64 gen)
836+{
837+ WRITE_ONCE(fs_info->generation, gen);
838+}
839+
840+static inline u64 btrfs_get_last_trans_committed(const struct btrfs_fs_info *fs_info)
841+{
842+ return READ_ONCE(fs_info->last_trans_committed);
843+}
844+
845+static inline void btrfs_set_last_trans_committed(struct btrfs_fs_info *fs_info, u64 gen)
846+{
847+ WRITE_ONCE(fs_info->last_trans_committed, gen);
848+}
849+
815850 static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
816851 u64 gen)
817852 {
fs/btrfs/inode-item.c+11−10
@@ -167,7 +167,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, · 166 unmodified lines
167167 memmove_extent_buffer(leaf, ptr, ptr + del_len,
168168 item_size - (ptr + del_len - item_start));
169169
170− btrfs_truncate_item(path, item_size - del_len, 1);
170+ btrfs_truncate_item(trans, path, item_size - del_len, 1);
171171
172172 out:
173173 btrfs_free_path(path);
@@ -229,7 +229,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, · 55 unmodified lines
229229 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
230230 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
231231 item_size - (ptr + sub_item_len - item_start));
232− btrfs_truncate_item(path, item_size - sub_item_len, 1);
232+ btrfs_truncate_item(trans, path, item_size - sub_item_len, 1);
233233 out:
234234 btrfs_free_path(path);
235235
@@ -247,7 +247,7 @@ out: · 11 unmodified lines
247247 }
248248
249249 /*
250− * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
250+ * Insert an extended inode ref into a tree.
251251 *
252252 * The caller must have checked against BTRFS_LINK_MAX already.
253253 */
@@ -282,7 +282,7 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, · 28 unmodified lines
282282 name))
283283 goto out;
284284
285− btrfs_extend_item(path, ins_len);
285+ btrfs_extend_item(trans, path, ins_len);
286286 ret = 0;
287287 }
288288 if (ret < 0)
@@ -299,7 +299,7 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, · 10 unmodified lines
299299
300300 ptr = (unsigned long)&extref->name;
301301 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
302− btrfs_mark_buffer_dirty(path->nodes[0]);
302+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
303303
304304 out:
305305 btrfs_free_path(path);
@@ -338,7 +338,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, · 32 unmodified lines
338338 goto out;
339339
340340 old_size = btrfs_item_size(path->nodes[0], path->slots[0]);
341− btrfs_extend_item(path, ins_len);
341+ btrfs_extend_item(trans, path, ins_len);
342342 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
343343 struct btrfs_inode_ref);
344344 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
@@ -364,7 +364,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, · 19 unmodified lines
364364 ptr = (unsigned long)(ref + 1);
365365 }
366366 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);
367− btrfs_mark_buffer_dirty(path->nodes[0]);
367+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
368368
369369 out:
370370 btrfs_free_path(path);
@@ -591,7 +591,7 @@ search_again: · 220 unmodified lines
591591 num_dec = (orig_num_bytes - extent_num_bytes);
592592 if (extent_start != 0)
593593 control->sub_bytes += num_dec;
594− btrfs_mark_buffer_dirty(leaf);
594+ btrfs_mark_buffer_dirty(trans, leaf);
595595 } else {
596596 extent_num_bytes =
597597 btrfs_file_extent_disk_num_bytes(leaf, fi);
@@ -617,7 +617,7 @@ search_again: · 19 unmodified lines
617617
618618 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
619619 size = btrfs_file_extent_calc_inline_size(size);
620− btrfs_truncate_item(path, size, 1);
620+ btrfs_truncate_item(trans, path, size, 1);
621621 } else if (!del_item) {
622622 /*
623623 * We have to bail so the last_size is set to
@@ -676,7 +676,8 @@ delete: · 52 unmodified lines
676676 bytes_deleted += extent_num_bytes;
677677
678678 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
679− extent_start, extent_num_bytes, 0);
679+ extent_start, extent_num_bytes, 0,
680+ root->root_key.objectid);
680681 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
681682 control->ino, extent_offset,
682683 root->root_key.objectid, false);
fs/btrfs/inode-item.h+8−0
@@ -4,6 +4,7 @@ · 3 unmodified lines
44 #define BTRFS_INODE_ITEM_H
55
66 #include <linux/types.h>
7+#include <linux/crc32c.h>
78
89 struct btrfs_trans_handle;
910 struct btrfs_root;
@@ -12,6 +13,7 @@ struct btrfs_key; · 2 unmodified lines
1213 struct btrfs_inode_extref;
1314 struct btrfs_inode;
1415 struct extent_buffer;
16+struct fscrypt_str;
1517
1618 /*
1719 * Return this if we need to call truncate_block for the last bit of the
@@ -76,6 +78,12 @@ static inline void btrfs_inode_split_flags(u64 inode_item_flags, · 58 unmodified lines
7678 *ro_flags = (u32)(inode_item_flags >> 32);
7779 }
7880
81+/* Figure the key offset of an extended inode ref. */
82+static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name, int len)
83+{
84+ return (u64)crc32c(parent_objectid, name, len);
85+}
86+
7987 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
8088 struct btrfs_root *root,
8189 struct btrfs_truncate_control *control);
fs/btrfs/inode.c+101−90
@@ -71,6 +71,7 @@ · 70 unmodified lines
7171 #include "super.h"
7272 #include "orphan.h"
7373 #include "backref.h"
74+#include "raid-stripe-tree.h"
7475
7576 struct btrfs_iget_args {
7677 u64 ino;
@@ -348,7 +349,7 @@ static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode, · 271 unmodified lines
348349 }
349350
350351 /*
351− * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
352+ * Lock inode i_rwsem based on arguments passed.
352353 *
353354 * ilock_flags can have the following bit set:
354355 *
@@ -382,7 +383,7 @@ int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags) · 27 unmodified lines
382383 }
383384
384385 /*
385− * btrfs_inode_unlock - unock inode i_rwsem
386+ * Unock inode i_rwsem.
386387 *
387388 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
388389 * to decide whether the lock acquired is shared or exclusive.
@@ -573,7 +574,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, · 184 unmodified lines
573574 kunmap_local(kaddr);
574575 put_page(page);
575576 }
576− btrfs_mark_buffer_dirty(leaf);
577+ btrfs_mark_buffer_dirty(trans, leaf);
577578 btrfs_release_path(path);
578579
579580 /*
@@ -670,7 +671,7 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, · 90 unmodified lines
670671 }
671672
672673 btrfs_update_inode_bytes(inode, size, drop_args.bytes_found);
673− ret = btrfs_update_inode(trans, root, inode);
674+ ret = btrfs_update_inode(trans, inode);
674675 if (ret && ret != -ENOSPC) {
675676 btrfs_abort_transaction(trans, ret);
676677 goto out;
@@ -1565,8 +1566,11 @@ out_unlock: · 888 unmodified lines
15651566 * Phase two of compressed writeback. This is the ordered portion of the code,
15661567 * which only gets called in the order the work was queued. We walk all the
15671568 * async extents created by compress_file_range and send them down to the disk.
1569+ *
1570+ * If called with @do_free == true then it'll try to finish the work and free
1571+ * the work struct eventually.
15681572 */
1569−static noinline void submit_compressed_extents(struct btrfs_work *work)
1573+static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_free)
15701574 {
15711575 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
15721576 work);
@@ -1575,6 +1579,21 @@ static noinline void submit_compressed_extents(struct btrfs_work *work) · 2 unmodified lines
15751579 unsigned long nr_pages;
15761580 u64 alloc_hint = 0;
15771581
1582+ if (do_free) {
1583+ struct async_chunk *async_chunk;
1584+ struct async_cow *async_cow;
1585+
1586+ async_chunk = container_of(work, struct async_chunk, work);
1587+ btrfs_add_delayed_iput(async_chunk->inode);
1588+ if (async_chunk->blkcg_css)
1589+ css_put(async_chunk->blkcg_css);
1590+
1591+ async_cow = async_chunk->async_cow;
1592+ if (atomic_dec_and_test(&async_cow->num_chunks))
1593+ kvfree(async_cow);
1594+ return;
1595+ }
1596+
15781597 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
15791598 PAGE_SHIFT;
15801599
@@ -1591,21 +1610,6 @@ static noinline void submit_compressed_extents(struct btrfs_work *work) · 10 unmodified lines
15911610 cond_wake_up_nomb(&fs_info->async_submit_wait);
15921611 }
15931612
1594−static noinline void async_cow_free(struct btrfs_work *work)
1595−{
1596− struct async_chunk *async_chunk;
1597− struct async_cow *async_cow;
1598−
1599− async_chunk = container_of(work, struct async_chunk, work);
1600− btrfs_add_delayed_iput(async_chunk->inode);
1601− if (async_chunk->blkcg_css)
1602− css_put(async_chunk->blkcg_css);
1603−
1604− async_cow = async_chunk->async_cow;
1605− if (atomic_dec_and_test(&async_cow->num_chunks))
1606− kvfree(async_cow);
1607−}
1608−
16091613 static bool run_delalloc_compressed(struct btrfs_inode *inode,
16101614 struct page *locked_page, u64 start,
16111615 u64 end, struct writeback_control *wbc)
@@ -1683,7 +1687,7 @@ static bool run_delalloc_compressed(struct btrfs_inode *inode, · 71 unmodified lines
16831687 }
16841688
16851689 btrfs_init_work(&async_chunk[i].work, compress_file_range,
1686− submit_compressed_extents, async_cow_free);
1690+ submit_compressed_extents);
16871691
16881692 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
16891693 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
@@ -2235,8 +2239,7 @@ static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) · 545 unmodified lines
22352239 {
22362240 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
22372241 if (inode->defrag_bytes &&
2238− test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
2239− 0, NULL))
2242+ test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG))
22402243 return false;
22412244 return true;
22422245 }
@@ -2847,7 +2850,7 @@ int btrfs_writepage_cow_fixup(struct page *page) · 604 unmodified lines
28472850 ihold(inode);
28482851 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);
28492852 get_page(page);
2850− btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
2853+ btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL);
28512854 fixup->page = page;
28522855 fixup->inode = BTRFS_I(inode);
28532856 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
@@ -2912,7 +2915,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, · 58 unmodified lines
29122915 btrfs_item_ptr_offset(leaf, path->slots[0]),
29132916 sizeof(struct btrfs_file_extent_item));
29142917
2915− btrfs_mark_buffer_dirty(leaf);
2918+ btrfs_mark_buffer_dirty(trans, leaf);
29162919 btrfs_release_path(path);
29172920
29182921 /*
@@ -3070,7 +3073,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 151 unmodified lines
30703073 goto out;
30713074 }
30723075 trans->block_rsv = &inode->block_rsv;
3073− ret = btrfs_update_inode_fallback(trans, root, inode);
3076+ ret = btrfs_update_inode_fallback(trans, inode);
30743077 if (ret) /* -ENOMEM or corruption */
30753078 btrfs_abort_transaction(trans, ret);
30763079 goto out;
@@ -3091,6 +3094,10 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 14 unmodified lines
30913094
30923095 trans->block_rsv = &inode->block_rsv;
30933096
3097+ ret = btrfs_insert_raid_extent(trans, ordered_extent);
3098+ if (ret)
3099+ goto out;
3100+
30943101 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
30953102 compress_type = ordered_extent->compress_type;
30963103 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
@@ -3136,7 +3143,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 39 unmodified lines
31363143 &cached_state);
31373144
31383145 btrfs_inode_safe_disk_i_size_write(inode, 0);
3139− ret = btrfs_update_inode_fallback(trans, root, inode);
3146+ ret = btrfs_update_inode_fallback(trans, inode);
31403147 if (ret) { /* -ENOMEM or corruption */
31413148 btrfs_abort_transaction(trans, ret);
31423149 goto out;
@@ -3224,7 +3231,8 @@ out: · 81 unmodified lines
32243231 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered)
32253232 {
32263233 if (btrfs_is_zoned(btrfs_sb(ordered->inode->i_sb)) &&
3227− !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
3234+ !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3235+ list_empty(&ordered->bioc_list))
32283236 btrfs_finish_ordered_zoned(ordered);
32293237 return btrfs_finish_one_ordered(ordered);
32303238 }
@@ -3282,7 +3290,7 @@ bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev, · 51 unmodified lines
32823290
32833291 if (btrfs_is_data_reloc_root(inode->root) &&
32843292 test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM,
3285− 1, NULL)) {
3293+ NULL)) {
32863294 /* Skip the range without csum for data reloc inode */
32873295 clear_extent_bits(&inode->io_tree, file_offset, end,
32883296 EXTENT_NODATASUM);
@@ -3306,7 +3314,7 @@ zeroit: · 17 unmodified lines
33063314 }
33073315
33083316 /*
3309− * btrfs_add_delayed_iput - perform a delayed iput on @inode
3317+ * Perform a delayed iput on @inode.
33103318 *
33113319 * @inode: The inode we want to perform iput on
33123320 *
@@ -3763,10 +3771,8 @@ static int btrfs_read_locked_inode(struct inode *inode, · 450 unmodified lines
37633771 inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime),
37643772 btrfs_timespec_nsec(leaf, &inode_item->ctime));
37653773
3766− BTRFS_I(inode)->i_otime.tv_sec =
3767− btrfs_timespec_sec(leaf, &inode_item->otime);
3768− BTRFS_I(inode)->i_otime.tv_nsec =
3769− btrfs_timespec_nsec(leaf, &inode_item->otime);
3774+ BTRFS_I(inode)->i_otime_sec = btrfs_timespec_sec(leaf, &inode_item->otime);
3775+ BTRFS_I(inode)->i_otime_nsec = btrfs_timespec_nsec(leaf, &inode_item->otime);
37703776
37713777 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
37723778 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
@@ -3792,7 +3798,7 @@ cache_index: · 19 unmodified lines
37923798 * This is required for both inode re-read from disk and delayed inode
37933799 * in delayed_nodes_tree.
37943800 */
3795− if (BTRFS_I(inode)->last_trans == fs_info->generation)
3801+ if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info))
37963802 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
37973803 &BTRFS_I(inode)->runtime_flags);
37983804
@@ -3936,10 +3942,8 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, · 137 unmodified lines
39363942 btrfs_set_token_timespec_nsec(&token, &item->ctime,
39373943 inode_get_ctime_nsec(inode));
39383944
3939− btrfs_set_token_timespec_sec(&token, &item->otime,
3940− BTRFS_I(inode)->i_otime.tv_sec);
3941− btrfs_set_token_timespec_nsec(&token, &item->otime,
3942− BTRFS_I(inode)->i_otime.tv_nsec);
3945+ btrfs_set_token_timespec_sec(&token, &item->otime, BTRFS_I(inode)->i_otime_sec);
3946+ btrfs_set_token_timespec_nsec(&token, &item->otime, BTRFS_I(inode)->i_otime_nsec);
39433947
39443948 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
39453949 btrfs_set_token_inode_generation(&token, item,
@@ -3957,8 +3961,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, · 11 unmodified lines
39573961 * copy everything in the in-memory inode into the btree.
39583962 */
39593963 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3960− struct btrfs_root *root,
3961− struct btrfs_inode *inode)
3964+ struct btrfs_inode *inode)
39623965 {
39633966 struct btrfs_inode_item *inode_item;
39643967 struct btrfs_path *path;
@@ -3969,7 +3972,7 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, · 4 unmodified lines
39693972 if (!path)
39703973 return -ENOMEM;
39713974
3972− ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
3975+ ret = btrfs_lookup_inode(trans, inode->root, path, &inode->location, 1);
39733976 if (ret) {
39743977 if (ret > 0)
39753978 ret = -ENOENT;
@@ -3981,7 +3984,7 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, · 5 unmodified lines
39813984 struct btrfs_inode_item);
39823985
39833986 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
3984− btrfs_mark_buffer_dirty(leaf);
3987+ btrfs_mark_buffer_dirty(trans, leaf);
39853988 btrfs_set_inode_last_trans(trans, inode);
39863989 ret = 0;
39873990 failed:
@@ -3992,10 +3995,10 @@ failed: · 4 unmodified lines
39923995 /*
39933996 * copy everything in the in-memory inode into the btree.
39943997 */
3995−noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3996− struct btrfs_root *root,
3997− struct btrfs_inode *inode)
3998+int btrfs_update_inode(struct btrfs_trans_handle *trans,
3999+ struct btrfs_inode *inode)
39984000 {
4001+ struct btrfs_root *root = inode->root;
39994002 struct btrfs_fs_info *fs_info = root->fs_info;
40004003 int ret;
40014004
@@ -4011,23 +4014,23 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, · 9 unmodified lines
40114014 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
40124015 btrfs_update_root_times(trans, root);
40134016
4014− ret = btrfs_delayed_update_inode(trans, root, inode);
4017+ ret = btrfs_delayed_update_inode(trans, inode);
40154018 if (!ret)
40164019 btrfs_set_inode_last_trans(trans, inode);
40174020 return ret;
40184021 }
40194022
4020− return btrfs_update_inode_item(trans, root, inode);
4023+ return btrfs_update_inode_item(trans, inode);
40214024 }
40224025
40234026 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4024− struct btrfs_root *root, struct btrfs_inode *inode)
4027+ struct btrfs_inode *inode)
40254028 {
40264029 int ret;
40274030
4028− ret = btrfs_update_inode(trans, root, inode);
4031+ ret = btrfs_update_inode(trans, inode);
40294032 if (ret == -ENOSPC)
4030− return btrfs_update_inode_item(trans, root, inode);
4033+ return btrfs_update_inode_item(trans, inode);
40314034 return ret;
40324035 }
40334036
@@ -4133,7 +4136,7 @@ err: · 99 unmodified lines
41334136 inode_inc_iversion(&inode->vfs_inode);
41344137 inode_inc_iversion(&dir->vfs_inode);
41354138 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode));
4136− ret = btrfs_update_inode(trans, root, dir);
4139+ ret = btrfs_update_inode(trans, dir);
41374140 out:
41384141 return ret;
41394142 }
@@ -4147,7 +4150,7 @@ int btrfs_unlink_inode(struct btrfs_trans_handle *trans, · 7 unmodified lines
41474150 ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL);
41484151 if (!ret) {
41494152 drop_nlink(&inode->vfs_inode);
4150− ret = btrfs_update_inode(trans, inode->root, inode);
4153+ ret = btrfs_update_inode(trans, inode);
41514154 }
41524155 return ret;
41534156 }
@@ -4306,7 +4309,7 @@ static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, · 152 unmodified lines
43064309 btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2);
43074310 inode_inc_iversion(&dir->vfs_inode);
43084311 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode));
4309− ret = btrfs_update_inode_fallback(trans, root, dir);
4312+ ret = btrfs_update_inode_fallback(trans, dir);
43104313 if (ret)
43114314 btrfs_abort_transaction(trans, ret);
43124315 out:
@@ -4640,7 +4643,8 @@ out_notrans: · 327 unmodified lines
46404643 }
46414644
46424645 /*
4643− * btrfs_truncate_block - read, zero a chunk and write a block
4646+ * Read, zero a chunk and write a block.
4647+ *
46444648 * @inode - inode that we're zeroing
46454649 * @from - the offset to start zeroing
46464650 * @len - the length to zero, 0 to zero the entire range respective to the
@@ -4790,9 +4794,9 @@ out: · 143 unmodified lines
47904794 return ret;
47914795 }
47924796
4793−static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
4794− u64 offset, u64 len)
4797+static int maybe_insert_hole(struct btrfs_inode *inode, u64 offset, u64 len)
47954798 {
4799+ struct btrfs_root *root = inode->root;
47964800 struct btrfs_fs_info *fs_info = root->fs_info;
47974801 struct btrfs_trans_handle *trans;
47984802 struct btrfs_drop_extents_args drop_args = { 0 };
@@ -4832,7 +4836,7 @@ static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode, · 33 unmodified lines
48324836 btrfs_abort_transaction(trans, ret);
48334837 } else {
48344838 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4835− btrfs_update_inode(trans, root, inode);
4839+ btrfs_update_inode(trans, inode);
48364840 }
48374841 btrfs_end_transaction(trans);
48384842 return ret;
@@ -4888,8 +4892,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) · 49 unmodified lines
48884892 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
48894893 struct extent_map *hole_em;
48904894
4891− err = maybe_insert_hole(root, inode, cur_offset,
4892− hole_size);
4895+ err = maybe_insert_hole(inode, cur_offset, hole_size);
48934896 if (err)
48944897 break;
48954898
@@ -4915,7 +4918,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) · 19 unmodified lines
49154918 hole_em->orig_block_len = 0;
49164919 hole_em->ram_bytes = hole_size;
49174920 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4918− hole_em->generation = fs_info->generation;
4921+ hole_em->generation = btrfs_get_fs_generation(fs_info);
49194922
49204923 err = btrfs_replace_extent_map_range(inode, hole_em, true);
49214924 free_extent_map(hole_em);
@@ -4984,7 +4987,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr) · 62 unmodified lines
49844987 i_size_write(inode, newsize);
49854988 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
49864989 pagecache_isize_extended(inode, oldsize, newsize);
4987− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
4990+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
49884991 btrfs_drew_write_unlock(&root->snapshot_lock);
49894992 btrfs_end_transaction(trans);
49904993 } else {
@@ -5582,6 +5585,7 @@ static struct inode *new_simple_dir(struct inode *dir, · 591 unmodified lines
55825585 struct btrfs_key *key,
55835586 struct btrfs_root *root)
55845587 {
5588+ struct timespec64 ts;
55855589 struct inode *inode = new_inode(dir->i_sb);
55865590
55875591 if (!inode)
@@ -5600,9 +5604,13 @@ static struct inode *new_simple_dir(struct inode *dir, · 12 unmodified lines
56005604 inode->i_opflags &= ~IOP_XATTR;
56015605 inode->i_fop = &simple_dir_operations;
56025606 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5603− inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
5607+
5608+ ts = inode_set_ctime_current(inode);
5609+ inode_set_mtime_to_ts(inode, ts);
56045610 inode_set_atime_to_ts(inode, inode_get_atime(dir));
5605− BTRFS_I(inode)->i_otime = inode_get_mtime(inode);
5611+ BTRFS_I(inode)->i_otime_sec = ts.tv_sec;
5612+ BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec;
5613+
56065614 inode->i_uid = dir->i_uid;
56075615 inode->i_gid = dir->i_gid;
56085616
@@ -6000,15 +6008,15 @@ static int btrfs_dirty_inode(struct btrfs_inode *inode) · 391 unmodified lines
60006008 if (IS_ERR(trans))
60016009 return PTR_ERR(trans);
60026010
6003− ret = btrfs_update_inode(trans, root, inode);
6004− if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
6011+ ret = btrfs_update_inode(trans, inode);
6012+ if (ret == -ENOSPC || ret == -EDQUOT) {
60056013 /* whoops, lets try again with the full transaction */
60066014 btrfs_end_transaction(trans);
60076015 trans = btrfs_start_transaction(root, 1);
60086016 if (IS_ERR(trans))
60096017 return PTR_ERR(trans);
60106018
6011− ret = btrfs_update_inode(trans, root, inode);
6019+ ret = btrfs_update_inode(trans, inode);
60126020 }
60136021 btrfs_end_transaction(trans);
60146022 if (inode->delayed_node)
@@ -6024,7 +6032,7 @@ static int btrfs_dirty_inode(struct btrfs_inode *inode) · 9 unmodified lines
60246032 static int btrfs_update_time(struct inode *inode, int flags)
60256033 {
60266034 struct btrfs_root *root = BTRFS_I(inode)->root;
6027− bool dirty = flags & ~S_VERSION;
6035+ bool dirty;
60286036
60296037 if (btrfs_root_readonly(root))
60306038 return -EROFS;
@@ -6160,6 +6168,7 @@ static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode * · 129 unmodified lines
61606168 int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
61616169 struct btrfs_new_inode_args *args)
61626170 {
6171+ struct timespec64 ts;
61636172 struct inode *dir = args->dir;
61646173 struct inode *inode = args->inode;
61656174 const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name;
@@ -6277,8 +6286,9 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans, · 111 unmodified lines
62776286 goto discard;
62786287 }
62796288
6280− simple_inode_init_ts(inode);
6281− BTRFS_I(inode)->i_otime = inode_get_mtime(inode);
6289+ ts = simple_inode_init_ts(inode);
6290+ BTRFS_I(inode)->i_otime_sec = ts.tv_sec;
6291+ BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec;
62826292
62836293 /*
62846294 * We're going to fill the inode item now, so at this point the inode
@@ -6309,7 +6319,7 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans, · 24 unmodified lines
63096319 }
63106320 }
63116321
6312− btrfs_mark_buffer_dirty(path->nodes[0]);
6322+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
63136323 /*
63146324 * We don't need the path anymore, plus inheriting properties, adding
63156325 * ACLs, security xattrs, orphan item or adding the link, will result in
@@ -6446,7 +6456,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans, · 130 unmodified lines
64466456 inode_set_mtime_to_ts(&parent_inode->vfs_inode,
64476457 inode_set_ctime_current(&parent_inode->vfs_inode));
64486458
6449− ret = btrfs_update_inode(trans, root, parent_inode);
6459+ ret = btrfs_update_inode(trans, parent_inode);
64506460 if (ret)
64516461 btrfs_abort_transaction(trans, ret);
64526462 return ret;
@@ -6597,7 +6607,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, · 144 unmodified lines
65976607 } else {
65986608 struct dentry *parent = dentry->d_parent;
65996609
6600− err = btrfs_update_inode(trans, root, BTRFS_I(inode));
6610+ err = btrfs_update_inode(trans, BTRFS_I(inode));
66016611 if (err)
66026612 goto fail;
66036613 if (inode->i_nlink == 1) {
@@ -7102,8 +7112,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, · 498 unmodified lines
71027112
71037113 range_end = round_up(offset + nocow_args.num_bytes,
71047114 root->fs_info->sectorsize) - 1;
7105− ret = test_range_bit(io_tree, offset, range_end,
7106− EXTENT_DELALLOC, 0, NULL);
7115+ ret = test_range_bit_exists(io_tree, offset, range_end, EXTENT_DELALLOC);
71077116 if (ret) {
71087117 ret = -EAGAIN;
71097118 goto out;
@@ -8004,11 +8013,11 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset, · 894 unmodified lines
80048013 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
80058014 EXTENT_DEFRAG, &cached_state);
80068015
8007− spin_lock_irq(&inode->ordered_tree.lock);
8016+ spin_lock_irq(&inode->ordered_tree_lock);
80088017 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
80098018 ordered->truncated_len = min(ordered->truncated_len,
80108019 cur - ordered->file_offset);
8011− spin_unlock_irq(&inode->ordered_tree.lock);
8020+ spin_unlock_irq(&inode->ordered_tree_lock);
80128021
80138022 /*
80148023 * If the ordered extent has finished, we're safe to delete all
@@ -8338,7 +8347,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) · 323 unmodified lines
83388347 if (ret != -ENOSPC && ret != -EAGAIN)
83398348 break;
83408349
8341− ret = btrfs_update_inode(trans, root, inode);
8350+ ret = btrfs_update_inode(trans, inode);
83428351 if (ret)
83438352 break;
83448353
@@ -8391,7 +8400,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) · 46 unmodified lines
83918400 int ret2;
83928401
83938402 trans->block_rsv = &fs_info->trans_block_rsv;
8394− ret2 = btrfs_update_inode(trans, root, inode);
8403+ ret2 = btrfs_update_inode(trans, inode);
83958404 if (ret2 && !ret)
83968405 ret = ret2;
83978406
@@ -8480,8 +8489,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 82 unmodified lines
84808489
84818490 ei->delayed_node = NULL;
84828491
8483− ei->i_otime.tv_sec = 0;
8484− ei->i_otime.tv_nsec = 0;
8492+ ei->i_otime_sec = 0;
8493+ ei->i_otime_nsec = 0;
84858494
84868495 inode = &ei->vfs_inode;
84878496 extent_map_tree_init(&ei->extent_tree);
@@ -8490,7 +8499,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 2 unmodified lines
84908499 extent_io_tree_init(fs_info, &ei->file_extent_tree,
84918500 IO_TREE_INODE_FILE_EXTENT);
84928501 mutex_init(&ei->log_mutex);
8493− btrfs_ordered_inode_tree_init(&ei->ordered_tree);
8502+ spin_lock_init(&ei->ordered_tree_lock);
8503+ ei->ordered_tree = RB_ROOT;
8504+ ei->ordered_tree_last = NULL;
84948505 INIT_LIST_HEAD(&ei->delalloc_inodes);
84958506 INIT_LIST_HEAD(&ei->delayed_iput);
84968507 RB_CLEAR_NODE(&ei->rb_node);
@@ -8633,8 +8644,8 @@ static int btrfs_getattr(struct mnt_idmap *idmap, · 136 unmodified lines
86338644 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
86348645
86358646 stat->result_mask |= STATX_BTIME;
8636− stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8637− stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8647+ stat->btime.tv_sec = BTRFS_I(inode)->i_otime_sec;
8648+ stat->btime.tv_nsec = BTRFS_I(inode)->i_otime_nsec;
86388649 if (bi_flags & BTRFS_INODE_APPEND)
86398650 stat->attributes |= STATX_ATTR_APPEND;
86408651 if (bi_flags & BTRFS_INODE_COMPRESS)
@@ -8822,7 +8833,7 @@ static int btrfs_rename_exchange(struct inode *old_dir, · 181 unmodified lines
88228833 BTRFS_I(old_dentry->d_inode),
88238834 old_name, &old_rename_ctx);
88248835 if (!ret)
8825− ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
8836+ ret = btrfs_update_inode(trans, BTRFS_I(old_inode));
88268837 }
88278838 if (ret) {
88288839 btrfs_abort_transaction(trans, ret);
@@ -8837,7 +8848,7 @@ static int btrfs_rename_exchange(struct inode *old_dir, · 8 unmodified lines
88378848 BTRFS_I(new_dentry->d_inode),
88388849 new_name, &new_rename_ctx);
88398850 if (!ret)
8840− ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
8851+ ret = btrfs_update_inode(trans, BTRFS_I(new_inode));
88418852 }
88428853 if (ret) {
88438854 btrfs_abort_transaction(trans, ret);
@@ -9082,7 +9093,7 @@ static int btrfs_rename(struct mnt_idmap *idmap, · 238 unmodified lines
90829093 BTRFS_I(d_inode(old_dentry)),
90839094 &old_fname.disk_name, &rename_ctx);
90849095 if (!ret)
9085− ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
9096+ ret = btrfs_update_inode(trans, BTRFS_I(old_inode));
90869097 }
90879098 if (ret) {
90889099 btrfs_abort_transaction(trans, ret);
@@ -9207,7 +9218,7 @@ static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode · 118 unmodified lines
92079218 init_completion(&work->completion);
92089219 INIT_LIST_HEAD(&work->list);
92099220 work->inode = inode;
9210− btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
9221+ btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL);
92119222
92129223 return work;
92139224 }
@@ -9445,7 +9456,7 @@ static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir, · 231 unmodified lines
94459456
94469457 ptr = btrfs_file_extent_inline_start(ei);
94479458 write_extent_buffer(leaf, symname, ptr, name_len);
9448− btrfs_mark_buffer_dirty(leaf);
9459+ btrfs_mark_buffer_dirty(trans, leaf);
94499460 btrfs_free_path(path);
94509461
94519462 d_instantiate_new(dentry, inode);
@@ -9638,7 +9649,7 @@ next: · 186 unmodified lines
96389649 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
96399650 }
96409651
9641− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
9652+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
96429653
96439654 if (ret) {
96449655 btrfs_abort_transaction(trans, ret);
fs/btrfs/ioctl.c+15−8
@@ -385,7 +385,7 @@ update_flags: · 384 unmodified lines
385385 btrfs_sync_inode_flags_to_i_flags(inode);
386386 inode_inc_iversion(inode);
387387 inode_set_ctime_current(inode);
388− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
388+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
389389
390390 out_end_trans:
391391 btrfs_end_transaction(trans);
@@ -652,18 +652,18 @@ static noinline int create_subvol(struct mnt_idmap *idmap, · 260 unmodified lines
652652 /* Tree log can't currently deal with an inode which is a new root. */
653653 btrfs_set_log_full_commit(trans);
654654
655− ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
655+ ret = btrfs_qgroup_inherit(trans, 0, objectid, root->root_key.objectid, inherit);
656656 if (ret)
657657 goto out;
658658
659659 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
660− BTRFS_NESTING_NORMAL);
660+ 0, BTRFS_NESTING_NORMAL);
661661 if (IS_ERR(leaf)) {
662662 ret = PTR_ERR(leaf);
663663 goto out;
664664 }
665665
666− btrfs_mark_buffer_dirty(leaf);
666+ btrfs_mark_buffer_dirty(trans, leaf);
667667
668668 inode_item = &root_item->inode;
669669 btrfs_set_stack_inode_generation(inode_item, 1);
@@ -2635,6 +2635,12 @@ static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) · 1965 unmodified lines
26352635 return -EINVAL;
26362636 }
26372637
2638+ if (fs_info->fs_devices->temp_fsid) {
2639+ btrfs_err(fs_info,
2640+ "device add not supported on cloned temp-fsid mount");
2641+ return -EINVAL;
2642+ }
2643+
26382644 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {
26392645 if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))
26402646 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
@@ -2820,7 +2826,7 @@ static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, · 179 unmodified lines
28202826 }
28212827
28222828 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
2823− fi_args->generation = fs_info->generation;
2829+ fi_args->generation = btrfs_get_fs_generation(fs_info);
28242830 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
28252831 }
28262832
@@ -2945,7 +2951,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) · 118 unmodified lines
29452951
29462952 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
29472953 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2948− btrfs_mark_buffer_dirty(path->nodes[0]);
2954+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
29492955 btrfs_release_path(path);
29502956
29512957 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
@@ -3129,7 +3135,7 @@ static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, · 177 unmodified lines
31293135 return PTR_ERR(trans);
31303136
31313137 /* No running transaction, don't bother */
3132− transid = root->fs_info->last_trans_committed;
3138+ transid = btrfs_get_last_trans_committed(root->fs_info);
31333139 goto out;
31343140 }
31353141 transid = trans->transid;
@@ -3695,7 +3701,8 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) · 559 unmodified lines
36953701
36963702 switch (sa->cmd) {
36973703 case BTRFS_QUOTA_CTL_ENABLE:
3698− ret = btrfs_quota_enable(fs_info);
3704+ case BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA:
3705+ ret = btrfs_quota_enable(fs_info, sa);
36993706 break;
37003707 case BTRFS_QUOTA_CTL_DISABLE:
37013708 ret = btrfs_quota_disable(fs_info);
fs/btrfs/locking.c+16−4
@@ -8,6 +8,7 @@ · 7 unmodified lines
88 #include <linux/spinlock.h>
99 #include <linux/page-flags.h>
1010 #include <asm/bug.h>
11+#include <trace/events/btrfs.h>
1112 #include "misc.h"
1213 #include "ctree.h"
1314 #include "extent_io.h"
@@ -73,6 +74,7 @@ static struct btrfs_lockdep_keyset { · 59 unmodified lines
7374 { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") },
7475 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") },
7576 { .id = BTRFS_BLOCK_GROUP_TREE_OBJECTID, DEFINE_NAME("block-group") },
77+ { .id = BTRFS_RAID_STRIPE_TREE_OBJECTID, DEFINE_NAME("raid-stripe") },
7678 { .id = 0, DEFINE_NAME("tree") },
7779 };
7880
@@ -102,6 +104,15 @@ void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buff · 23 unmodified lines
102104
103105 #endif
104106
107+#ifdef CONFIG_BTRFS_DEBUG
108+static void btrfs_set_eb_lock_owner(struct extent_buffer *eb, pid_t owner)
109+{
110+ eb->lock_owner = owner;
111+}
112+#else
113+static void btrfs_set_eb_lock_owner(struct extent_buffer *eb, pid_t owner) { }
114+#endif
115+
105116 /*
106117 * Extent buffer locking
107118 * =====================
@@ -164,7 +175,7 @@ int btrfs_try_tree_read_lock(struct extent_buffer *eb) · 56 unmodified lines
164175 int btrfs_try_tree_write_lock(struct extent_buffer *eb)
165176 {
166177 if (down_write_trylock(&eb->lock)) {
167− eb->lock_owner = current->pid;
178+ btrfs_set_eb_lock_owner(eb, current->pid);
168179 trace_btrfs_try_tree_write_lock(eb);
169180 return 1;
170181 }
@@ -181,7 +192,8 @@ void btrfs_tree_read_unlock(struct extent_buffer *eb) · 10 unmodified lines
181192 }
182193
183194 /*
184− * __btrfs_tree_lock - lock eb for write
195+ * Lock eb for write.
196+ *
185197 * @eb: the eb to lock
186198 * @nest: the nesting to use for the lock
187199 *
@@ -196,7 +208,7 @@ void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest) · 8 unmodified lines
196208 start_ns = ktime_get_ns();
197209
198210 down_write_nested(&eb->lock, nest);
199− eb->lock_owner = current->pid;
211+ btrfs_set_eb_lock_owner(eb, current->pid);
200212 trace_btrfs_tree_lock(eb, start_ns);
201213 }
202214
@@ -211,7 +223,7 @@ void btrfs_tree_lock(struct extent_buffer *eb) · 8 unmodified lines
211223 void btrfs_tree_unlock(struct extent_buffer *eb)
212224 {
213225 trace_btrfs_tree_unlock(eb);
214− eb->lock_owner = 0;
226+ btrfs_set_eb_lock_owner(eb, 0);
215227 up_write(&eb->lock);
216228 }
217229
fs/btrfs/messages.c+16−16
@@ -72,11 +72,11 @@ static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf) · 71 unmodified lines
7272 * over the error. Each subsequent error that doesn't have any context
7373 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
7474 */
75−const char * __attribute_const__ btrfs_decode_error(int errno)
75+const char * __attribute_const__ btrfs_decode_error(int error)
7676 {
7777 char *errstr = "unknown";
7878
79− switch (errno) {
79+ switch (error) {
8080 case -ENOENT: /* -2 */
8181 errstr = "No such entry";
8282 break;
@@ -110,12 +110,12 @@ const char * __attribute_const__ btrfs_decode_error(int errno) · 27 unmodified lines
110110 }
111111
112112 /*
113− * __btrfs_handle_fs_error decodes expected errors from the caller and
114− * invokes the appropriate error response.
113+ * Decodes expected errors from the caller and invokes the appropriate error
114+ * response.
115115 */
116116 __cold
117117 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
118− unsigned int line, int errno, const char *fmt, ...)
118+ unsigned int line, int error, const char *fmt, ...)
119119 {
120120 struct super_block *sb = fs_info->sb;
121121 #ifdef CONFIG_PRINTK
@@ -132,11 +132,11 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function · 10 unmodified lines
132132 * Special case: if the error is EROFS, and we're already under
133133 * SB_RDONLY, then it is safe here.
134134 */
135− if (errno == -EROFS && sb_rdonly(sb))
135+ if (error == -EROFS && sb_rdonly(sb))
136136 return;
137137
138138 #ifdef CONFIG_PRINTK
139− errstr = btrfs_decode_error(errno);
139+ errstr = btrfs_decode_error(error);
140140 btrfs_state_to_string(fs_info, statestr);
141141 if (fmt) {
142142 struct va_format vaf;
@@ -147,11 +147,11 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function · 4 unmodified lines
147147 vaf.va = &args;
148148
149149 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
150− sb->s_id, statestr, function, line, errno, errstr, &vaf);
150+ sb->s_id, statestr, function, line, error, errstr, &vaf);
151151 va_end(args);
152152 } else {
153153 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
154− sb->s_id, statestr, function, line, errno, errstr);
154+ sb->s_id, statestr, function, line, error, errstr);
155155 }
156156 #endif
157157
@@ -159,7 +159,7 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function · 1 unmodified line
159159 * Today we only save the error info to memory. Long term we'll also
160160 * send it down to the disk.
161161 */
162− WRITE_ONCE(fs_info->fs_error, errno);
162+ WRITE_ONCE(fs_info->fs_error, error);
163163
164164 /* Don't go through full error handling during mount. */
165165 if (!(sb->s_flags & SB_BORN))
@@ -283,12 +283,12 @@ void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info) · 117 unmodified lines
283283 #endif
284284
285285 /*
286− * __btrfs_panic decodes unexpected, fatal errors from the caller, issues an
287− * alert, and either panics or BUGs, depending on mount options.
286+ * Decode unexpected, fatal errors from the caller, issue an alert, and either
287+ * panic or BUGs, depending on mount options.
288288 */
289289 __cold
290290 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
291− unsigned int line, int errno, const char *fmt, ...)
291+ unsigned int line, int error, const char *fmt, ...)
292292 {
293293 char *s_id = "<unknown>";
294294 const char *errstr;
@@ -301,13 +301,13 @@ void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, · 6 unmodified lines
301301 va_start(args, fmt);
302302 vaf.va = &args;
303303
304− errstr = btrfs_decode_error(errno);
304+ errstr = btrfs_decode_error(error);
305305 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
306306 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
307− s_id, function, line, &vaf, errno, errstr);
307+ s_id, function, line, &vaf, error, errstr);
308308
309309 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
310− function, line, &vaf, errno, errstr);
310+ function, line, &vaf, error, errstr);
311311 va_end(args);
312312 /* Caller calls BUG() */
313313 }
fs/btrfs/messages.h+7−7
@@ -184,25 +184,25 @@ do { \ · 183 unmodified lines
184184 __printf(5, 6)
185185 __cold
186186 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
187− unsigned int line, int errno, const char *fmt, ...);
187+ unsigned int line, int error, const char *fmt, ...);
188188
189−const char * __attribute_const__ btrfs_decode_error(int errno);
189+const char * __attribute_const__ btrfs_decode_error(int error);
190190
191−#define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \
191+#define btrfs_handle_fs_error(fs_info, error, fmt, args...) \
192192 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
193− (errno), fmt, ##args)
193+ (error), fmt, ##args)
194194
195195 __printf(5, 6)
196196 __cold
197197 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
198− unsigned int line, int errno, const char *fmt, ...);
198+ unsigned int line, int error, const char *fmt, ...);
199199 /*
200200 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
201201 * will panic(). Otherwise we BUG() here.
202202 */
203−#define btrfs_panic(fs_info, errno, fmt, args...) \
203+#define btrfs_panic(fs_info, error, fmt, args...) \
204204 do { \
205− __btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \
205+ __btrfs_panic(fs_info, __func__, __LINE__, error, fmt, ##args); \
206206 BUG(); \
207207 } while (0)
208208
fs/btrfs/ordered-data.c+57−70
@@ -124,25 +124,24 @@ static int range_overlaps(struct btrfs_ordered_extent *entry, u64 file_offset, · 123 unmodified lines
124124 * look find the first ordered struct that has this offset, otherwise
125125 * the first one less than this offset
126126 */
127−static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
128− u64 file_offset)
127+static inline struct rb_node *ordered_tree_search(struct btrfs_inode *inode,
128+ u64 file_offset)
129129 {
130− struct rb_root *root = &tree->tree;
131130 struct rb_node *prev = NULL;
132131 struct rb_node *ret;
133132 struct btrfs_ordered_extent *entry;
134133
135− if (tree->last) {
136− entry = rb_entry(tree->last, struct btrfs_ordered_extent,
134+ if (inode->ordered_tree_last) {
135+ entry = rb_entry(inode->ordered_tree_last, struct btrfs_ordered_extent,
137136 rb_node);
138137 if (in_range(file_offset, entry->file_offset, entry->num_bytes))
139− return tree->last;
138+ return inode->ordered_tree_last;
140139 }
141− ret = __tree_search(root, file_offset, &prev);
140+ ret = __tree_search(&inode->ordered_tree, file_offset, &prev);
142141 if (!ret)
143142 ret = prev;
144143 if (ret)
145− tree->last = ret;
144+ inode->ordered_tree_last = ret;
146145 return ret;
147146 }
148147
@@ -191,6 +190,7 @@ static struct btrfs_ordered_extent *alloc_ordered_extent( · 42 unmodified lines
191190 INIT_LIST_HEAD(&entry->log_list);
192191 INIT_LIST_HEAD(&entry->root_extent_list);
193192 INIT_LIST_HEAD(&entry->work_list);
193+ INIT_LIST_HEAD(&entry->bioc_list);
194194 init_completion(&entry->completion);
195195
196196 /*
@@ -208,7 +208,6 @@ static struct btrfs_ordered_extent *alloc_ordered_extent( · 11 unmodified lines
208208 static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
209209 {
210210 struct btrfs_inode *inode = BTRFS_I(entry->inode);
211− struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
212211 struct btrfs_root *root = inode->root;
213212 struct btrfs_fs_info *fs_info = root->fs_info;
214213 struct rb_node *node;
@@ -221,13 +220,14 @@ static void insert_ordered_extent(struct btrfs_ordered_extent *entry) · 6 unmodified lines
221220 /* One ref for the tree. */
222221 refcount_inc(&entry->refs);
223222
224− spin_lock_irq(&tree->lock);
225− node = tree_insert(&tree->tree, entry->file_offset, &entry->rb_node);
223+ spin_lock_irq(&inode->ordered_tree_lock);
224+ node = tree_insert(&inode->ordered_tree, entry->file_offset,
225+ &entry->rb_node);
226226 if (node)
227227 btrfs_panic(fs_info, -EEXIST,
228228 "inconsistency in ordered tree at offset %llu",
229229 entry->file_offset);
230− spin_unlock_irq(&tree->lock);
230+ spin_unlock_irq(&inode->ordered_tree_lock);
231231
232232 spin_lock(&root->ordered_extent_lock);
233233 list_add_tail(&entry->root_extent_list,
@@ -287,12 +287,11 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( · 53 unmodified lines
287287 void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
288288 struct btrfs_ordered_sum *sum)
289289 {
290− struct btrfs_ordered_inode_tree *tree;
290+ struct btrfs_inode *inode = BTRFS_I(entry->inode);
291291
292− tree = &BTRFS_I(entry->inode)->ordered_tree;
293− spin_lock_irq(&tree->lock);
292+ spin_lock_irq(&inode->ordered_tree_lock);
294293 list_add_tail(&sum->list, &entry->list);
295− spin_unlock_irq(&tree->lock);
294+ spin_unlock_irq(&inode->ordered_tree_lock);
296295 }
297296
298297 static void finish_ordered_fn(struct btrfs_work *work)
@@ -310,7 +309,7 @@ static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered, · 11 unmodified lines
310309 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
311310 struct btrfs_fs_info *fs_info = inode->root->fs_info;
312311
313− lockdep_assert_held(&inode->ordered_tree.lock);
312+ lockdep_assert_held(&inode->ordered_tree_lock);
314313
315314 if (page) {
316315 ASSERT(page->mapping);
@@ -364,7 +363,7 @@ static void btrfs_queue_ordered_fn(struct btrfs_ordered_extent *ordered) · 47 unmodified lines
364363 struct btrfs_workqueue *wq = btrfs_is_free_space_inode(inode) ?
365364 fs_info->endio_freespace_worker : fs_info->endio_write_workers;
366365
367− btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL);
366+ btrfs_init_work(&ordered->work, finish_ordered_fn, NULL);
368367 btrfs_queue_work(wq, &ordered->work);
369368 }
370369
@@ -378,9 +377,9 @@ bool btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered, · 7 unmodified lines
378377
379378 trace_btrfs_finish_ordered_extent(inode, file_offset, len, uptodate);
380379
381− spin_lock_irqsave(&inode->ordered_tree.lock, flags);
380+ spin_lock_irqsave(&inode->ordered_tree_lock, flags);
382381 ret = can_finish_ordered_extent(ordered, page, file_offset, len, uptodate);
383− spin_unlock_irqrestore(&inode->ordered_tree.lock, flags);
382+ spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
384383
385384 if (ret)
386385 btrfs_queue_ordered_fn(ordered);
@@ -404,7 +403,6 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, · 17 unmodified lines
404403 struct page *page, u64 file_offset,
405404 u64 num_bytes, bool uptodate)
406405 {
407− struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
408406 struct rb_node *node;
409407 struct btrfs_ordered_extent *entry = NULL;
410408 unsigned long flags;
@@ -414,13 +412,13 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, · 3 unmodified lines
414412 file_offset + num_bytes - 1,
415413 uptodate);
416414
417− spin_lock_irqsave(&tree->lock, flags);
415+ spin_lock_irqsave(&inode->ordered_tree_lock, flags);
418416 while (cur < file_offset + num_bytes) {
419417 u64 entry_end;
420418 u64 end;
421419 u32 len;
422420
423− node = tree_search(tree, cur);
421+ node = ordered_tree_search(inode, cur);
424422 /* No ordered extents at all */
425423 if (!node)
426424 break;
@@ -467,13 +465,13 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, · 40 unmodified lines
467465 len = end + 1 - cur;
468466
469467 if (can_finish_ordered_extent(entry, page, cur, len, uptodate)) {
470− spin_unlock_irqrestore(&tree->lock, flags);
468+ spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
471469 btrfs_queue_ordered_fn(entry);
472− spin_lock_irqsave(&tree->lock, flags);
470+ spin_lock_irqsave(&inode->ordered_tree_lock, flags);
473471 }
474472 cur += len;
475473 }
476− spin_unlock_irqrestore(&tree->lock, flags);
474+ spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
477475 }
478476
479477 /*
@@ -497,19 +495,18 @@ bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode, · 17 unmodified lines
497495 struct btrfs_ordered_extent **cached,
498496 u64 file_offset, u64 io_size)
499497 {
500− struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
501498 struct rb_node *node;
502499 struct btrfs_ordered_extent *entry = NULL;
503500 unsigned long flags;
504501 bool finished = false;
505502
506− spin_lock_irqsave(&tree->lock, flags);
503+ spin_lock_irqsave(&inode->ordered_tree_lock, flags);
507504 if (cached && *cached) {
508505 entry = *cached;
509506 goto have_entry;
510507 }
511508
512− node = tree_search(tree, file_offset);
509+ node = ordered_tree_search(inode, file_offset);
513510 if (!node)
514511 goto out;
515512
@@ -540,7 +537,7 @@ out: · 24 unmodified lines
540537 refcount_inc(&entry->refs);
541538 trace_btrfs_ordered_extent_dec_test_pending(inode, entry);
542539 }
543− spin_unlock_irqrestore(&tree->lock, flags);
540+ spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
544541 return finished;
545542 }
546543
@@ -578,7 +575,6 @@ void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry) · 31 unmodified lines
578575 void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
579576 struct btrfs_ordered_extent *entry)
580577 {
581− struct btrfs_ordered_inode_tree *tree;
582578 struct btrfs_root *root = btrfs_inode->root;
583579 struct btrfs_fs_info *fs_info = root->fs_info;
584580 struct rb_node *node;
@@ -609,16 +605,15 @@ void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode, · 24 unmodified lines
609605 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,
610606 fs_info->delalloc_batch);
611607
612− tree = &btrfs_inode->ordered_tree;
613− spin_lock_irq(&tree->lock);
608+ spin_lock_irq(&btrfs_inode->ordered_tree_lock);
614609 node = &entry->rb_node;
615− rb_erase(node, &tree->tree);
610+ rb_erase(node, &btrfs_inode->ordered_tree);
616611 RB_CLEAR_NODE(node);
617− if (tree->last == node)
618− tree->last = NULL;
612+ if (btrfs_inode->ordered_tree_last == node)
613+ btrfs_inode->ordered_tree_last = NULL;
619614 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
620615 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);
621− spin_unlock_irq(&tree->lock);
616+ spin_unlock_irq(&btrfs_inode->ordered_tree_lock);
622617
623618 /*
624619 * The current running transaction is waiting on us, we need to let it
@@ -711,7 +706,7 @@ u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, · 86 unmodified lines
711706 spin_unlock(&root->ordered_extent_lock);
712707
713708 btrfs_init_work(&ordered->flush_work,
714− btrfs_run_ordered_extent_work, NULL, NULL);
709+ btrfs_run_ordered_extent_work, NULL);
715710 list_add_tail(&ordered->work_list, &works);
716711 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
717712
@@ -875,14 +870,12 @@ int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len) · 157 unmodified lines
875870 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
876871 u64 file_offset)
877872 {
878− struct btrfs_ordered_inode_tree *tree;
879873 struct rb_node *node;
880874 struct btrfs_ordered_extent *entry = NULL;
881875 unsigned long flags;
882876
883− tree = &inode->ordered_tree;
884− spin_lock_irqsave(&tree->lock, flags);
885− node = tree_search(tree, file_offset);
877+ spin_lock_irqsave(&inode->ordered_tree_lock, flags);
878+ node = ordered_tree_search(inode, file_offset);
886879 if (!node)
887880 goto out;
888881
@@ -894,7 +887,7 @@ struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *ino · 5 unmodified lines
894887 trace_btrfs_ordered_extent_lookup(inode, entry);
895888 }
896889 out:
897− spin_unlock_irqrestore(&tree->lock, flags);
890+ spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
898891 return entry;
899892 }
900893
@@ -904,15 +897,13 @@ out: · 3 unmodified lines
904897 struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
905898 struct btrfs_inode *inode, u64 file_offset, u64 len)
906899 {
907− struct btrfs_ordered_inode_tree *tree;
908900 struct rb_node *node;
909901 struct btrfs_ordered_extent *entry = NULL;
910902
911− tree = &inode->ordered_tree;
912− spin_lock_irq(&tree->lock);
913− node = tree_search(tree, file_offset);
903+ spin_lock_irq(&inode->ordered_tree_lock);
904+ node = ordered_tree_search(inode, file_offset);
914905 if (!node) {
915− node = tree_search(tree, file_offset + len);
906+ node = ordered_tree_search(inode, file_offset + len);
916907 if (!node)
917908 goto out;
918909 }
@@ -936,7 +927,7 @@ out: · 17 unmodified lines
936927 refcount_inc(&entry->refs);
937928 trace_btrfs_ordered_extent_lookup_range(inode, entry);
938929 }
939− spin_unlock_irq(&tree->lock);
930+ spin_unlock_irq(&inode->ordered_tree_lock);
940931 return entry;
941932 }
942933
@@ -947,13 +938,12 @@ out: · 4 unmodified lines
947938 void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
948939 struct list_head *list)
949940 {
950− struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
951941 struct rb_node *n;
952942
953943 ASSERT(inode_is_locked(&inode->vfs_inode));
954944
955− spin_lock_irq(&tree->lock);
956− for (n = rb_first(&tree->tree); n; n = rb_next(n)) {
945+ spin_lock_irq(&inode->ordered_tree_lock);
946+ for (n = rb_first(&inode->ordered_tree); n; n = rb_next(n)) {
957947 struct btrfs_ordered_extent *ordered;
958948
959949 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);
@@ -966,7 +956,7 @@ void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, · 6 unmodified lines
966956 refcount_inc(&ordered->refs);
967957 trace_btrfs_ordered_extent_lookup_for_logging(inode, ordered);
968958 }
969− spin_unlock_irq(&tree->lock);
959+ spin_unlock_irq(&inode->ordered_tree_lock);
970960 }
971961
972962 /*
@@ -976,13 +966,11 @@ void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, · 3 unmodified lines
976966 struct btrfs_ordered_extent *
977967 btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
978968 {
979− struct btrfs_ordered_inode_tree *tree;
980969 struct rb_node *node;
981970 struct btrfs_ordered_extent *entry = NULL;
982971
983− tree = &inode->ordered_tree;
984− spin_lock_irq(&tree->lock);
985− node = tree_search(tree, file_offset);
972+ spin_lock_irq(&inode->ordered_tree_lock);
973+ node = ordered_tree_search(inode, file_offset);
986974 if (!node)
987975 goto out;
988976
@@ -990,7 +978,7 @@ btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset) · 1 unmodified line
990978 refcount_inc(&entry->refs);
991979 trace_btrfs_ordered_extent_lookup_first(inode, entry);
992980 out:
993− spin_unlock_irq(&tree->lock);
981+ spin_unlock_irq(&inode->ordered_tree_lock);
994982 return entry;
995983 }
996984
@@ -1006,15 +994,14 @@ out: · 9 unmodified lines
1006994 struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
1007995 struct btrfs_inode *inode, u64 file_offset, u64 len)
1008996 {
1009− struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
1010997 struct rb_node *node;
1011998 struct rb_node *cur;
1012999 struct rb_node *prev;
10131000 struct rb_node *next;
10141001 struct btrfs_ordered_extent *entry = NULL;
10151002
1016− spin_lock_irq(&tree->lock);
1017− node = tree->tree.rb_node;
1003+ spin_lock_irq(&inode->ordered_tree_lock);
1004+ node = inode->ordered_tree.rb_node;
10181005 /*
10191006 * Here we don't want to use tree_search() which will use tree->last
10201007 * and screw up the search order.
@@ -1068,7 +1055,7 @@ out: · 47 unmodified lines
10681055 trace_btrfs_ordered_extent_lookup_first_range(inode, entry);
10691056 }
10701057
1071− spin_unlock_irq(&tree->lock);
1058+ spin_unlock_irq(&inode->ordered_tree_lock);
10721059 return entry;
10731060 }
10741061
@@ -1147,7 +1134,6 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( · 72 unmodified lines
11471134 struct btrfs_ordered_extent *ordered, u64 len)
11481135 {
11491136 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1150− struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
11511137 struct btrfs_root *root = inode->root;
11521138 struct btrfs_fs_info *fs_info = root->fs_info;
11531139 u64 file_offset = ordered->file_offset;
@@ -1187,13 +1173,13 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( · 33 unmodified lines
11871173 refcount_inc(&new->refs);
11881174
11891175 spin_lock_irq(&root->ordered_extent_lock);
1190− spin_lock(&tree->lock);
1176+ spin_lock(&inode->ordered_tree_lock);
11911177 /* Remove from tree once */
11921178 node = &ordered->rb_node;
1193− rb_erase(node, &tree->tree);
1179+ rb_erase(node, &inode->ordered_tree);
11941180 RB_CLEAR_NODE(node);
1195− if (tree->last == node)
1196− tree->last = NULL;
1181+ if (inode->ordered_tree_last == node)
1182+ inode->ordered_tree_last = NULL;
11971183
11981184 ordered->file_offset += len;
11991185 ordered->disk_bytenr += len;
@@ -1224,18 +1210,19 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( · 24 unmodified lines
12241210 }
12251211
12261212 /* Re-insert the node */
1227− node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node);
1213+ node = tree_insert(&inode->ordered_tree, ordered->file_offset,
1214+ &ordered->rb_node);
12281215 if (node)
12291216 btrfs_panic(fs_info, -EEXIST,
12301217 "zoned: inconsistency in ordered tree at offset %llu",
12311218 ordered->file_offset);
12321219
1233− node = tree_insert(&tree->tree, new->file_offset, &new->rb_node);
1220+ node = tree_insert(&inode->ordered_tree, new->file_offset, &new->rb_node);
12341221 if (node)
12351222 btrfs_panic(fs_info, -EEXIST,
12361223 "zoned: inconsistency in ordered tree at offset %llu",
12371224 new->file_offset);
1238− spin_unlock(&tree->lock);
1225+ spin_unlock(&inode->ordered_tree_lock);
12391226
12401227 list_add_tail(&new->root_extent_list, &root->ordered_extents);
12411228 root->nr_ordered_extents++;
fs/btrfs/ordered-data.h+2−15
@@ -6,13 +6,6 @@ · 5 unmodified lines
66 #ifndef BTRFS_ORDERED_DATA_H
77 #define BTRFS_ORDERED_DATA_H
88
9−/* one of these per inode */
10−struct btrfs_ordered_inode_tree {
11− spinlock_t lock;
12− struct rb_root tree;
13− struct rb_node *last;
14−};
15−
169 struct btrfs_ordered_sum {
1710 /*
1811 * Logical start address and length for of the blocks covered by
@@ -151,15 +144,9 @@ struct btrfs_ordered_extent { · 132 unmodified lines
151144 struct completion completion;
152145 struct btrfs_work flush_work;
153146 struct list_head work_list;
154−};
155147
156−static inline void
157−btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
158−{
159− spin_lock_init(&t->lock);
160− t->tree = RB_ROOT;
161− t->last = NULL;
162−}
148+ struct list_head bioc_list;
149+};
163150
164151 int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);
165152 int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
fs/btrfs/print-tree.c+35−0
@@ -9,6 +9,8 @@ · 8 unmodified lines
99 #include "print-tree.h"
1010 #include "accessors.h"
1111 #include "tree-checker.h"
12+#include "volumes.h"
13+#include "raid-stripe-tree.h"
1214
1315 struct root_name_map {
1416 u64 id;
@@ -28,6 +30,7 @@ static const struct root_name_map root_map[] = { · 13 unmodified lines
2830 { BTRFS_FREE_SPACE_TREE_OBJECTID, "FREE_SPACE_TREE" },
2931 { BTRFS_BLOCK_GROUP_TREE_OBJECTID, "BLOCK_GROUP_TREE" },
3032 { BTRFS_DATA_RELOC_TREE_OBJECTID, "DATA_RELOC_TREE" },
33+ { BTRFS_RAID_STRIPE_TREE_OBJECTID, "RAID_STRIPE_TREE" },
3134 };
3235
3336 const char *btrfs_root_name(const struct btrfs_key *key, char *buf)
@@ -80,12 +83,20 @@ static void print_extent_data_ref(const struct extent_buffer *eb, · 46 unmodified lines
8083 btrfs_extent_data_ref_count(eb, ref));
8184 }
8285
86+static void print_extent_owner_ref(const struct extent_buffer *eb,
87+ const struct btrfs_extent_owner_ref *ref)
88+{
89+ ASSERT(btrfs_fs_incompat(eb->fs_info, SIMPLE_QUOTA));
90+ pr_cont("extent data owner root %llu\n", btrfs_extent_owner_ref_root_id(eb, ref));
91+}
92+
8393 static void print_extent_item(const struct extent_buffer *eb, int slot, int type)
8494 {
8595 struct btrfs_extent_item *ei;
8696 struct btrfs_extent_inline_ref *iref;
8797 struct btrfs_extent_data_ref *dref;
8898 struct btrfs_shared_data_ref *sref;
99+ struct btrfs_extent_owner_ref *oref;
89100 struct btrfs_disk_key key;
90101 unsigned long end;
91102 unsigned long ptr;
@@ -161,6 +172,10 @@ static void print_extent_item(const struct extent_buffer *eb, int slot, int type · 69 unmodified lines
161172 "\t\t\t(parent %llu not aligned to sectorsize %u)\n",
162173 offset, eb->fs_info->sectorsize);
163174 break;
175+ case BTRFS_EXTENT_OWNER_REF_KEY:
176+ oref = (struct btrfs_extent_owner_ref *)(&iref->offset);
177+ print_extent_owner_ref(eb, oref);
178+ break;
164179 default:
165180 pr_cont("(extent %llu has INVALID ref type %d)\n",
166181 eb->start, type);
@@ -189,6 +204,22 @@ static void print_uuid_item(const struct extent_buffer *l, unsigned long offset, · 22 unmodified lines
189204 }
190205 }
191206
207+static void print_raid_stripe_key(const struct extent_buffer *eb, u32 item_size,
208+ struct btrfs_stripe_extent *stripe)
209+{
210+ const int num_stripes = btrfs_num_raid_stripes(item_size);
211+ const u8 encoding = btrfs_stripe_extent_encoding(eb, stripe);
212+
213+ pr_info("\t\t\tencoding: %s\n",
214+ (encoding && encoding < BTRFS_NR_RAID_TYPES) ?
215+ btrfs_raid_array[encoding].raid_name : "unknown");
216+
217+ for (int i = 0; i < num_stripes; i++)
218+ pr_info("\t\t\tstride %d devid %llu physical %llu\n",
219+ i, btrfs_raid_stride_devid(eb, &stripe->strides[i]),
220+ btrfs_raid_stride_physical(eb, &stripe->strides[i]));
221+}
222+
192223 /*
193224 * Helper to output refs and locking status of extent buffer. Useful to debug
194225 * race condition related problems.
@@ -349,6 +380,10 @@ void btrfs_print_leaf(const struct extent_buffer *l) · 154 unmodified lines
349380 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
350381 btrfs_item_size(l, i));
351382 break;
383+ case BTRFS_RAID_STRIPE_KEY:
384+ print_raid_stripe_key(l, btrfs_item_size(l, i),
385+ btrfs_item_ptr(l, i, struct btrfs_stripe_extent));
386+ break;
352387 }
353388 }
354389 }
fs/btrfs/props.c+1−0
@@ -15,6 +15,7 @@ · 14 unmodified lines
1515 #include "fs.h"
1616 #include "accessors.h"
1717 #include "super.h"
18+#include "dir-item.h"
1819
1920 #define BTRFS_PROP_HANDLERS_HT_BITS 8
2021 static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
fs/btrfs/qgroup.c+560−312
@@ -30,6 +30,25 @@ · 29 unmodified lines
3030 #include "root-tree.h"
3131 #include "tree-checker.h"
3232
33+enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info)
34+{
35+ if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
36+ return BTRFS_QGROUP_MODE_DISABLED;
37+ if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE)
38+ return BTRFS_QGROUP_MODE_SIMPLE;
39+ return BTRFS_QGROUP_MODE_FULL;
40+}
41+
42+bool btrfs_qgroup_enabled(struct btrfs_fs_info *fs_info)
43+{
44+ return btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_DISABLED;
45+}
46+
47+bool btrfs_qgroup_full_accounting(struct btrfs_fs_info *fs_info)
48+{
49+ return btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL;
50+}
51+
3352 /*
3453 * Helpers to access qgroup reservation
3554 *
@@ -146,16 +165,6 @@ struct btrfs_qgroup_list { · 110 unmodified lines
146165 struct btrfs_qgroup *member;
147166 };
148167
149−static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
150−{
151− return (u64)(uintptr_t)qg;
152−}
153−
154−static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
155−{
156− return (struct btrfs_qgroup *)(uintptr_t)n->aux;
157−}
158−
159168 static int
160169 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
161170 int init_flags);
@@ -180,34 +189,46 @@ static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info, · 18 unmodified lines
180189 return NULL;
181190 }
182191
183−/* must be called with qgroup_lock held */
192+/*
193+ * Add qgroup to the filesystem's qgroup tree.
194+ *
195+ * Must be called with qgroup_lock held and @prealloc preallocated.
196+ *
197+ * The control on the lifespan of @prealloc would be transfered to this
198+ * function, thus caller should no longer touch @prealloc.
199+ */
184200 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
201+ struct btrfs_qgroup *prealloc,
185202 u64 qgroupid)
186203 {
187204 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
188205 struct rb_node *parent = NULL;
189206 struct btrfs_qgroup *qgroup;
190207
208+ /* Caller must have pre-allocated @prealloc. */
209+ ASSERT(prealloc);
210+
191211 while (*p) {
192212 parent = *p;
193213 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
194214
195− if (qgroup->qgroupid < qgroupid)
215+ if (qgroup->qgroupid < qgroupid) {
196216 p = &(*p)->rb_left;
197− else if (qgroup->qgroupid > qgroupid)
217+ } else if (qgroup->qgroupid > qgroupid) {
198218 p = &(*p)->rb_right;
199− else
219+ } else {
220+ kfree(prealloc);
200221 return qgroup;
222+ }
201223 }
202224
203− qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
204− if (!qgroup)
205− return ERR_PTR(-ENOMEM);
206−
225+ qgroup = prealloc;
207226 qgroup->qgroupid = qgroupid;
208227 INIT_LIST_HEAD(&qgroup->groups);
209228 INIT_LIST_HEAD(&qgroup->members);
210229 INIT_LIST_HEAD(&qgroup->dirty);
230+ INIT_LIST_HEAD(&qgroup->iterator);
231+ INIT_LIST_HEAD(&qgroup->nested_iterator);
211232
212233 rb_link_node(&qgroup->node, parent, p);
213234 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
@@ -254,27 +275,26 @@ static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid) · 40 unmodified lines
254275 /*
255276 * Add relation specified by two qgroups.
256277 *
257− * Must be called with qgroup_lock held.
278+ * Must be called with qgroup_lock held, the ownership of @prealloc is
279+ * transferred to this function and caller should not touch it anymore.
258280 *
259281 * Return: 0 on success
260282 * -ENOENT if one of the qgroups is NULL
261283 * <0 other errors
262284 */
263−static int __add_relation_rb(struct btrfs_qgroup *member, struct btrfs_qgroup *parent)
285+static int __add_relation_rb(struct btrfs_qgroup_list *prealloc,
286+ struct btrfs_qgroup *member,
287+ struct btrfs_qgroup *parent)
264288 {
265− struct btrfs_qgroup_list *list;
266−
267− if (!member || !parent)
289+ if (!member || !parent) {
290+ kfree(prealloc);
268291 return -ENOENT;
292+ }
269293
270− list = kzalloc(sizeof(*list), GFP_ATOMIC);
271− if (!list)
272− return -ENOMEM;
273−
274− list->group = parent;
275− list->member = member;
276− list_add_tail(&list->next_group, &member->groups);
277− list_add_tail(&list->next_member, &parent->members);
294+ prealloc->group = parent;
295+ prealloc->member = member;
296+ list_add_tail(&prealloc->next_group, &member->groups);
297+ list_add_tail(&prealloc->next_member, &parent->members);
278298
279299 return 0;
280300 }
@@ -288,7 +308,9 @@ static int __add_relation_rb(struct btrfs_qgroup *member, struct btrfs_qgroup *p · 7 unmodified lines
288308 * -ENOENT if one of the ids does not exist
289309 * <0 other errors
290310 */
291−static int add_relation_rb(struct btrfs_fs_info *fs_info, u64 memberid, u64 parentid)
311+static int add_relation_rb(struct btrfs_fs_info *fs_info,
312+ struct btrfs_qgroup_list *prealloc,
313+ u64 memberid, u64 parentid)
292314 {
293315 struct btrfs_qgroup *member;
294316 struct btrfs_qgroup *parent;
@@ -296,7 +318,7 @@ static int add_relation_rb(struct btrfs_fs_info *fs_info, u64 memberid, u64 pare · 1 unmodified line
296318 member = find_qgroup_rb(fs_info, memberid);
297319 parent = find_qgroup_rb(fs_info, parentid);
298320
299− return __add_relation_rb(member, parent);
321+ return __add_relation_rb(prealloc, member, parent);
300322 }
301323
302324 /* Must be called with qgroup_lock held */
@@ -340,11 +362,22 @@ int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid, · 37 unmodified lines
340362
341363 static void qgroup_mark_inconsistent(struct btrfs_fs_info *fs_info)
342364 {
365+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
366+ return;
343367 fs_info->qgroup_flags |= (BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT |
344368 BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |
345369 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
346370 }
347371
372+static void qgroup_read_enable_gen(struct btrfs_fs_info *fs_info,
373+ struct extent_buffer *leaf, int slot,
374+ struct btrfs_qgroup_status_item *ptr)
375+{
376+ ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
377+ ASSERT(btrfs_item_size(leaf, slot) >= sizeof(*ptr));
378+ fs_info->qgroup_enable_gen = btrfs_qgroup_status_enable_gen(leaf, ptr);
379+}
380+
348381 /*
349382 * The full config is read in one go, only called from open_ctree()
350383 * It doesn't use any locking, as at this point we're still single-threaded
@@ -361,7 +394,7 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) · 10 unmodified lines
361394 u64 flags = 0;
362395 u64 rescan_progress = 0;
363396
364− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
397+ if (!fs_info->quota_root)
365398 return 0;
366399
367400 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
@@ -411,14 +444,14 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) · 43 unmodified lines
411444 "old qgroup version, quota disabled");
412445 goto out;
413446 }
414− if (btrfs_qgroup_status_generation(l, ptr) !=
415− fs_info->generation) {
447+ fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, ptr);
448+ if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE) {
449+ qgroup_read_enable_gen(fs_info, l, slot, ptr);
450+ } else if (btrfs_qgroup_status_generation(l, ptr) != fs_info->generation) {
416451 qgroup_mark_inconsistent(fs_info);
417452 btrfs_err(fs_info,
418453 "qgroup generation mismatch, marked as inconsistent");
419454 }
420− fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
421− ptr);
422455 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
423456 goto next1;
424457 }
@@ -434,11 +467,14 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) · 9 unmodified lines
434467 qgroup_mark_inconsistent(fs_info);
435468 }
436469 if (!qgroup) {
437− qgroup = add_qgroup_rb(fs_info, found_key.offset);
438− if (IS_ERR(qgroup)) {
439− ret = PTR_ERR(qgroup);
470+ struct btrfs_qgroup *prealloc;
471+
472+ prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
473+ if (!prealloc) {
474+ ret = -ENOMEM;
440475 goto out;
441476 }
477+ qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
442478 }
443479 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
444480 if (ret < 0)
@@ -489,6 +525,8 @@ next1: · 44 unmodified lines
489525 if (ret)
490526 goto out;
491527 while (1) {
528+ struct btrfs_qgroup_list *list = NULL;
529+
492530 slot = path->slots[0];
493531 l = path->nodes[0];
494532 btrfs_item_key_to_cpu(l, &found_key, slot);
@@ -502,8 +540,14 @@ next1: · 7 unmodified lines
502540 goto next2;
503541 }
504542
505− ret = add_relation_rb(fs_info, found_key.objectid,
543+ list = kzalloc(sizeof(*list), GFP_KERNEL);
544+ if (!list) {
545+ ret = -ENOMEM;
546+ goto out;
547+ }
548+ ret = add_relation_rb(fs_info, list, found_key.objectid,
506549 found_key.offset);
550+ list = NULL;
507551 if (ret == -ENOENT) {
508552 btrfs_warn(fs_info,
509553 "orphan qgroup relation 0x%llx->0x%llx",
@@ -522,13 +566,12 @@ next2: · 12 unmodified lines
522566 out:
523567 btrfs_free_path(path);
524568 fs_info->qgroup_flags |= flags;
525− if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
526− clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
527− else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
528− ret >= 0)
529− ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
530−
531− if (ret < 0) {
569+ if (ret >= 0) {
570+ if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)
571+ set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
572+ if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
573+ ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
574+ } else {
532575 ulist_free(fs_info->qgroup_ulist);
533576 fs_info->qgroup_ulist = NULL;
534577 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
@@ -550,7 +593,7 @@ bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info) · 15 unmodified lines
550593 struct rb_node *node;
551594 bool ret = false;
552595
553− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
596+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
554597 return ret;
555598 /*
556599 * Since we're unmounting, there is no race and no need to grab qgroup
@@ -622,7 +665,7 @@ static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src, · 65 unmodified lines
622665
623666 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
624667
625− btrfs_mark_buffer_dirty(path->nodes[0]);
668+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
626669
627670 btrfs_free_path(path);
628671 return ret;
@@ -700,7 +743,7 @@ static int add_qgroup_item(struct btrfs_trans_handle *trans, · 71 unmodified lines
700743 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
701744 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
702745
703− btrfs_mark_buffer_dirty(leaf);
746+ btrfs_mark_buffer_dirty(trans, leaf);
704747
705748 btrfs_release_path(path);
706749
@@ -719,7 +762,7 @@ static int add_qgroup_item(struct btrfs_trans_handle *trans, · 12 unmodified lines
719762 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
720763 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
721764
722− btrfs_mark_buffer_dirty(leaf);
765+ btrfs_mark_buffer_dirty(trans, leaf);
723766
724767 ret = 0;
725768 out:
@@ -808,7 +851,7 @@ static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, · 82 unmodified lines
808851 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
809852 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
810853
811− btrfs_mark_buffer_dirty(l);
854+ btrfs_mark_buffer_dirty(trans, l);
812855
813856 out:
814857 btrfs_free_path(path);
@@ -854,7 +897,7 @@ static int update_qgroup_info_item(struct btrfs_trans_handle *trans, · 39 unmodified lines
854897 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
855898 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
856899
857− btrfs_mark_buffer_dirty(l);
900+ btrfs_mark_buffer_dirty(trans, l);
858901
859902 out:
860903 btrfs_free_path(path);
@@ -896,7 +939,7 @@ static int update_qgroup_status_item(struct btrfs_trans_handle *trans) · 35 unmodified lines
896939 btrfs_set_qgroup_status_rescan(l, ptr,
897940 fs_info->qgroup_rescan_progress.objectid);
898941
899− btrfs_mark_buffer_dirty(l);
942+ btrfs_mark_buffer_dirty(trans, l);
900943
901944 out:
902945 btrfs_free_path(path);
@@ -949,7 +992,8 @@ out: · 46 unmodified lines
949992 return ret;
950993 }
951994
952−int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
995+int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
996+ struct btrfs_ioctl_quota_ctl_args *quota_ctl_args)
953997 {
954998 struct btrfs_root *quota_root;
955999 struct btrfs_root *tree_root = fs_info->tree_root;
@@ -959,8 +1003,10 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 3 unmodified lines
9591003 struct btrfs_key key;
9601004 struct btrfs_key found_key;
9611005 struct btrfs_qgroup *qgroup = NULL;
1006+ struct btrfs_qgroup *prealloc = NULL;
9621007 struct btrfs_trans_handle *trans = NULL;
9631008 struct ulist *ulist = NULL;
1009+ const bool simple = (quota_ctl_args->cmd == BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA);
9641010 int ret = 0;
9651011 int slot;
9661012
@@ -1063,13 +1109,18 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 96 unmodified lines
10631109 struct btrfs_qgroup_status_item);
10641110 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
10651111 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
1066− fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
1067− BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1112+ fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON;
1113+ if (simple) {
1114+ fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
1115+ btrfs_set_qgroup_status_enable_gen(leaf, ptr, trans->transid);
1116+ } else {
1117+ fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1118+ }
10681119 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags &
10691120 BTRFS_QGROUP_STATUS_FLAGS_MASK);
10701121 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
10711122
1072− btrfs_mark_buffer_dirty(leaf);
1123+ btrfs_mark_buffer_dirty(trans, leaf);
10731124
10741125 key.objectid = 0;
10751126 key.type = BTRFS_ROOT_REF_KEY;
@@ -1094,6 +1145,15 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 18 unmodified lines
10941145 /* Release locks on tree_root before we access quota_root */
10951146 btrfs_release_path(path);
10961147
1148+ /* We should not have a stray @prealloc pointer. */
1149+ ASSERT(prealloc == NULL);
1150+ prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1151+ if (!prealloc) {
1152+ ret = -ENOMEM;
1153+ btrfs_abort_transaction(trans, ret);
1154+ goto out_free_path;
1155+ }
1156+
10971157 ret = add_qgroup_item(trans, quota_root,
10981158 found_key.offset);
10991159 if (ret) {
@@ -1101,7 +1161,8 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 1 unmodified line
11011161 goto out_free_path;
11021162 }
11031163
1104− qgroup = add_qgroup_rb(fs_info, found_key.offset);
1164+ qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
1165+ prealloc = NULL;
11051166 if (IS_ERR(qgroup)) {
11061167 ret = PTR_ERR(qgroup);
11071168 btrfs_abort_transaction(trans, ret);
@@ -1144,18 +1205,22 @@ out_add_root: · 36 unmodified lines
11441205 goto out_free_path;
11451206 }
11461207
1147− qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
1148− if (IS_ERR(qgroup)) {
1149− ret = PTR_ERR(qgroup);
1150− btrfs_abort_transaction(trans, ret);
1208+ ASSERT(prealloc == NULL);
1209+ prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1210+ if (!prealloc) {
1211+ ret = -ENOMEM;
11511212 goto out_free_path;
11521213 }
1214+ qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);
1215+ prealloc = NULL;
11531216 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
11541217 if (ret < 0) {
11551218 btrfs_abort_transaction(trans, ret);
11561219 goto out_free_path;
11571220 }
11581221
1222+ fs_info->qgroup_enable_gen = trans->transid;
1223+
11591224 mutex_unlock(&fs_info->qgroup_ioctl_lock);
11601225 /*
11611226 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
@@ -1180,8 +1245,14 @@ out_add_root: · 18 unmodified lines
11801245 spin_lock(&fs_info->qgroup_lock);
11811246 fs_info->quota_root = quota_root;
11821247 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1248+ if (simple)
1249+ btrfs_set_fs_incompat(fs_info, SIMPLE_QUOTA);
11831250 spin_unlock(&fs_info->qgroup_lock);
11841251
1252+ /* Skip rescan for simple qgroups. */
1253+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1254+ goto out_free_path;
1255+
11851256 ret = qgroup_rescan_init(fs_info, 0, 1);
11861257 if (!ret) {
11871258 qgroup_rescan_zero_tracking(fs_info);
@@ -1222,6 +1293,39 @@ out: · 34 unmodified lines
12221293 else if (trans)
12231294 ret = btrfs_end_transaction(trans);
12241295 ulist_free(ulist);
1296+ kfree(prealloc);
1297+ return ret;
1298+}
1299+
1300+/*
1301+ * It is possible to have outstanding ordered extents which reserved bytes
1302+ * before we disabled. We need to fully flush delalloc, ordered extents, and a
1303+ * commit to ensure that we don't leak such reservations, only to have them
1304+ * come back if we re-enable.
1305+ *
1306+ * - enable simple quotas
1307+ * - reserve space
1308+ * - release it, store rsv_bytes in OE
1309+ * - disable quotas
1310+ * - enable simple quotas (qgroup rsv are all 0)
1311+ * - OE finishes
1312+ * - run delayed refs
1313+ * - free rsv_bytes, resulting in miscounting or even underflow
1314+ */
1315+static int flush_reservations(struct btrfs_fs_info *fs_info)
1316+{
1317+ struct btrfs_trans_handle *trans;
1318+ int ret;
1319+
1320+ ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
1321+ if (ret)
1322+ return ret;
1323+ btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1324+ trans = btrfs_join_transaction(fs_info->tree_root);
1325+ if (IS_ERR(trans))
1326+ return PTR_ERR(trans);
1327+ btrfs_commit_transaction(trans);
1328+
12251329 return ret;
12261330 }
12271331
@@ -1269,6 +1373,10 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info) · 41 unmodified lines
12691373 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
12701374 btrfs_qgroup_wait_for_completion(fs_info, false);
12711375
1376+ ret = flush_reservations(fs_info);
1377+ if (ret)
1378+ goto out_unlock_cleaner;
1379+
12721380 /*
12731381 * 1 For the root item
12741382 *
@@ -1295,6 +1403,7 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info) · 20 unmodified lines
12951403 quota_root = fs_info->quota_root;
12961404 fs_info->quota_root = NULL;
12971405 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1406+ fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
12981407 fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
12991408 spin_unlock(&fs_info->qgroup_lock);
13001409
@@ -1329,7 +1438,8 @@ out: · 28 unmodified lines
13291438 if (ret && trans)
13301439 btrfs_end_transaction(trans);
13311440 else if (trans)
1332− ret = btrfs_end_transaction(trans);
1441+ ret = btrfs_commit_transaction(trans);
1442+out_unlock_cleaner:
13331443 mutex_unlock(&fs_info->cleaner_mutex);
13341444
13351445 return ret;
@@ -1342,6 +1452,24 @@ static void qgroup_dirty(struct btrfs_fs_info *fs_info, · 6 unmodified lines
13421452 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
13431453 }
13441454
1455+static void qgroup_iterator_add(struct list_head *head, struct btrfs_qgroup *qgroup)
1456+{
1457+ if (!list_empty(&qgroup->iterator))
1458+ return;
1459+
1460+ list_add_tail(&qgroup->iterator, head);
1461+}
1462+
1463+static void qgroup_iterator_clean(struct list_head *head)
1464+{
1465+ while (!list_empty(head)) {
1466+ struct btrfs_qgroup *qgroup;
1467+
1468+ qgroup = list_first_entry(head, struct btrfs_qgroup, iterator);
1469+ list_del_init(&qgroup->iterator);
1470+ }
1471+}
1472+
13451473 /*
13461474 * The easy accounting, we're updating qgroup relationship whose child qgroup
13471475 * only has exclusive extents.
@@ -1356,14 +1484,12 @@ static void qgroup_dirty(struct btrfs_fs_info *fs_info, · 8 unmodified lines
13561484 *
13571485 * Caller should hold fs_info->qgroup_lock.
13581486 */
1359−static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1360− struct ulist *tmp, u64 ref_root,
1487+static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, u64 ref_root,
13611488 struct btrfs_qgroup *src, int sign)
13621489 {
13631490 struct btrfs_qgroup *qgroup;
1364− struct btrfs_qgroup_list *glist;
1365− struct ulist_node *unode;
1366− struct ulist_iterator uiter;
1491+ struct btrfs_qgroup *cur;
1492+ LIST_HEAD(qgroup_list);
13671493 u64 num_bytes = src->excl;
13681494 int ret = 0;
13691495
@@ -1371,53 +1497,30 @@ static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, · 1 unmodified line
13711497 if (!qgroup)
13721498 goto out;
13731499
1374− qgroup->rfer += sign * num_bytes;
1375− qgroup->rfer_cmpr += sign * num_bytes;
1376−
1377− WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1378− qgroup->excl += sign * num_bytes;
1379− qgroup->excl_cmpr += sign * num_bytes;
1380−
1381− if (sign > 0)
1382− qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1383− else
1384− qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1385−
1386− qgroup_dirty(fs_info, qgroup);
1387−
1388− /* Get all of the parent groups that contain this qgroup */
1389− list_for_each_entry(glist, &qgroup->groups, next_group) {
1390− ret = ulist_add(tmp, glist->group->qgroupid,
1391− qgroup_to_aux(glist->group), GFP_ATOMIC);
1392− if (ret < 0)
1393− goto out;
1394− }
1500+ qgroup_iterator_add(&qgroup_list, qgroup);
1501+ list_for_each_entry(cur, &qgroup_list, iterator) {
1502+ struct btrfs_qgroup_list *glist;
13951503
1396− /* Iterate all of the parents and adjust their reference counts */
1397− ULIST_ITER_INIT(&uiter);
1398− while ((unode = ulist_next(tmp, &uiter))) {
1399− qgroup = unode_aux_to_qgroup(unode);
14001504 qgroup->rfer += sign * num_bytes;
14011505 qgroup->rfer_cmpr += sign * num_bytes;
1506+
14021507 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
14031508 qgroup->excl += sign * num_bytes;
1509+ qgroup->excl_cmpr += sign * num_bytes;
1510+
14041511 if (sign > 0)
14051512 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
14061513 else
14071514 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1408− qgroup->excl_cmpr += sign * num_bytes;
14091515 qgroup_dirty(fs_info, qgroup);
14101516
1411− /* Add any parents of the parents */
1412− list_for_each_entry(glist, &qgroup->groups, next_group) {
1413− ret = ulist_add(tmp, glist->group->qgroupid,
1414− qgroup_to_aux(glist->group), GFP_ATOMIC);
1415− if (ret < 0)
1416− goto out;
1417− }
1517+ /* Append parent qgroups to @qgroup_list. */
1518+ list_for_each_entry(glist, &qgroup->groups, next_group)
1519+ qgroup_iterator_add(&qgroup_list, glist->group);
14181520 }
14191521 ret = 0;
14201522 out:
1523+ qgroup_iterator_clean(&qgroup_list);
14211524 return ret;
14221525 }
14231526
@@ -1434,8 +1537,7 @@ out: · 10 unmodified lines
14341537 * Return < 0 for other error.
14351538 */
14361539 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1437− struct ulist *tmp, u64 src, u64 dst,
1438− int sign)
1540+ u64 src, u64 dst, int sign)
14391541 {
14401542 struct btrfs_qgroup *qgroup;
14411543 int ret = 1;
@@ -1446,8 +1548,7 @@ static int quick_update_accounting(struct btrfs_fs_info *fs_info, · 4 unmodified lines
14461548 goto out;
14471549 if (qgroup->excl == qgroup->rfer) {
14481550 ret = 0;
1449− err = __qgroup_excl_accounting(fs_info, tmp, dst,
1450− qgroup, sign);
1551+ err = __qgroup_excl_accounting(fs_info, dst, qgroup, sign);
14511552 if (err < 0) {
14521553 ret = err;
14531554 goto out;
@@ -1459,28 +1560,19 @@ out: · 5 unmodified lines
14591560 return ret;
14601561 }
14611562
1462−int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1463− u64 dst)
1563+int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst)
14641564 {
14651565 struct btrfs_fs_info *fs_info = trans->fs_info;
14661566 struct btrfs_qgroup *parent;
14671567 struct btrfs_qgroup *member;
14681568 struct btrfs_qgroup_list *list;
1469− struct ulist *tmp;
1470− unsigned int nofs_flag;
1569+ struct btrfs_qgroup_list *prealloc = NULL;
14711570 int ret = 0;
14721571
14731572 /* Check the level of src and dst first */
14741573 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
14751574 return -EINVAL;
14761575
1477− /* We hold a transaction handle open, must do a NOFS allocation. */
1478− nofs_flag = memalloc_nofs_save();
1479− tmp = ulist_alloc(GFP_KERNEL);
1480− memalloc_nofs_restore(nofs_flag);
1481− if (!tmp)
1482− return -ENOMEM;
1483−
14841576 mutex_lock(&fs_info->qgroup_ioctl_lock);
14851577 if (!fs_info->quota_root) {
14861578 ret = -ENOTCONN;
@@ -1501,6 +1593,11 @@ int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, · 14 unmodified lines
15011593 }
15021594 }
15031595
1596+ prealloc = kzalloc(sizeof(*list), GFP_NOFS);
1597+ if (!prealloc) {
1598+ ret = -ENOMEM;
1599+ goto out;
1600+ }
15041601 ret = add_qgroup_relation_item(trans, src, dst);
15051602 if (ret)
15061603 goto out;
@@ -1512,16 +1609,17 @@ int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, · 5 unmodified lines
15121609 }
15131610
15141611 spin_lock(&fs_info->qgroup_lock);
1515− ret = __add_relation_rb(member, parent);
1612+ ret = __add_relation_rb(prealloc, member, parent);
1613+ prealloc = NULL;
15161614 if (ret < 0) {
15171615 spin_unlock(&fs_info->qgroup_lock);
15181616 goto out;
15191617 }
1520− ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1618+ ret = quick_update_accounting(fs_info, src, dst, 1);
15211619 spin_unlock(&fs_info->qgroup_lock);
15221620 out:
1621+ kfree(prealloc);
15231622 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1524− ulist_free(tmp);
15251623 return ret;
15261624 }
15271625
@@ -1532,19 +1630,10 @@ static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, · 4 unmodified lines
15321630 struct btrfs_qgroup *parent;
15331631 struct btrfs_qgroup *member;
15341632 struct btrfs_qgroup_list *list;
1535− struct ulist *tmp;
15361633 bool found = false;
1537− unsigned int nofs_flag;
15381634 int ret = 0;
15391635 int ret2;
15401636
1541− /* We hold a transaction handle open, must do a NOFS allocation. */
1542− nofs_flag = memalloc_nofs_save();
1543− tmp = ulist_alloc(GFP_KERNEL);
1544− memalloc_nofs_restore(nofs_flag);
1545− if (!tmp)
1546− return -ENOMEM;
1547−
15481637 if (!fs_info->quota_root) {
15491638 ret = -ENOTCONN;
15501639 goto out;
@@ -1582,11 +1671,10 @@ delete_item: · 31 unmodified lines
15821671 if (found) {
15831672 spin_lock(&fs_info->qgroup_lock);
15841673 del_relation_rb(fs_info, src, dst);
1585− ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1674+ ret = quick_update_accounting(fs_info, src, dst, -1);
15861675 spin_unlock(&fs_info->qgroup_lock);
15871676 }
15881677 out:
1589− ulist_free(tmp);
15901678 return ret;
15911679 }
15921680
@@ -1608,8 +1696,12 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) · 15 unmodified lines
16081696 struct btrfs_fs_info *fs_info = trans->fs_info;
16091697 struct btrfs_root *quota_root;
16101698 struct btrfs_qgroup *qgroup;
1699+ struct btrfs_qgroup *prealloc = NULL;
16111700 int ret = 0;
16121701
1702+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
1703+ return 0;
1704+
16131705 mutex_lock(&fs_info->qgroup_ioctl_lock);
16141706 if (!fs_info->quota_root) {
16151707 ret = -ENOTCONN;
@@ -1622,21 +1714,25 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) · 6 unmodified lines
16221714 goto out;
16231715 }
16241716
1717+ prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1718+ if (!prealloc) {
1719+ ret = -ENOMEM;
1720+ goto out;
1721+ }
1722+
16251723 ret = add_qgroup_item(trans, quota_root, qgroupid);
16261724 if (ret)
16271725 goto out;
16281726
16291727 spin_lock(&fs_info->qgroup_lock);
1630− qgroup = add_qgroup_rb(fs_info, qgroupid);
1728+ qgroup = add_qgroup_rb(fs_info, prealloc, qgroupid);
16311729 spin_unlock(&fs_info->qgroup_lock);
1730+ prealloc = NULL;
16321731
1633− if (IS_ERR(qgroup)) {
1634− ret = PTR_ERR(qgroup);
1635− goto out;
1636− }
16371732 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
16381733 out:
16391734 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1735+ kfree(prealloc);
16401736 return ret;
16411737 }
16421738
@@ -1771,6 +1867,17 @@ out: · 128 unmodified lines
17711867 return ret;
17721868 }
17731869
1870+/*
1871+ * Inform qgroup to trace one dirty extent, its info is recorded in @record.
1872+ * So qgroup can account it at transaction committing time.
1873+ *
1874+ * No lock version, caller must acquire delayed ref lock and allocated memory,
1875+ * then call btrfs_qgroup_trace_extent_post() after exiting lock context.
1876+ *
1877+ * Return 0 for success insert
1878+ * Return >0 for existing record, caller can free @record safely.
1879+ * Error is not possible
1880+ */
17741881 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
17751882 struct btrfs_delayed_ref_root *delayed_refs,
17761883 struct btrfs_qgroup_extent_record *record)
@@ -1780,6 +1887,9 @@ int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info, · 3 unmodified lines
17801887 struct btrfs_qgroup_extent_record *entry;
17811888 u64 bytenr = record->bytenr;
17821889
1890+ if (!btrfs_qgroup_full_accounting(fs_info))
1891+ return 0;
1892+
17831893 lockdep_assert_held(&delayed_refs->lock);
17841894 trace_btrfs_qgroup_trace_extent(fs_info, record);
17851895
@@ -1806,12 +1916,35 @@ int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info, · 20 unmodified lines
18061916 return 0;
18071917 }
18081918
1919+/*
1920+ * Post handler after qgroup_trace_extent_nolock().
1921+ *
1922+ * NOTE: Current qgroup does the expensive backref walk at transaction
1923+ * committing time with TRANS_STATE_COMMIT_DOING, this blocks incoming
1924+ * new transaction.
1925+ * This is designed to allow btrfs_find_all_roots() to get correct new_roots
1926+ * result.
1927+ *
1928+ * However for old_roots there is no need to do backref walk at that time,
1929+ * since we search commit roots to walk backref and result will always be
1930+ * correct.
1931+ *
1932+ * Due to the nature of no lock version, we can't do backref there.
1933+ * So we must call btrfs_qgroup_trace_extent_post() after exiting
1934+ * spinlock context.
1935+ *
1936+ * TODO: If we can fix and prove btrfs_find_all_roots() can get correct result
1937+ * using current root, then we can move all expensive backref walk out of
1938+ * transaction committing, but not now as qgroup accounting will be wrong again.
1939+ */
18091940 int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
18101941 struct btrfs_qgroup_extent_record *qrecord)
18111942 {
18121943 struct btrfs_backref_walk_ctx ctx = { 0 };
18131944 int ret;
18141945
1946+ if (!btrfs_qgroup_full_accounting(trans->fs_info))
1947+ return 0;
18151948 /*
18161949 * We are always called in a context where we are already holding a
18171950 * transaction handle. Often we are called when adding a data delayed
@@ -1859,6 +1992,19 @@ int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans, · 41 unmodified lines
18591992 return 0;
18601993 }
18611994
1995+/*
1996+ * Inform qgroup to trace one dirty extent, specified by @bytenr and
1997+ * @num_bytes.
1998+ * So qgroup can account it at commit trans time.
1999+ *
2000+ * Better encapsulated version, with memory allocation and backref walk for
2001+ * commit roots.
2002+ * So this can sleep.
2003+ *
2004+ * Return 0 if the operation is done.
2005+ * Return <0 for error, like memory allocation failure or invalid parameter
2006+ * (NULL trans)
2007+ */
18622008 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
18632009 u64 num_bytes)
18642010 {
@@ -1867,8 +2013,7 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 2 unmodified lines
18672013 struct btrfs_delayed_ref_root *delayed_refs;
18682014 int ret;
18692015
1870− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1871− || bytenr == 0 || num_bytes == 0)
2016+ if (!btrfs_qgroup_full_accounting(fs_info) || bytenr == 0 || num_bytes == 0)
18722017 return 0;
18732018 record = kzalloc(sizeof(*record), GFP_NOFS);
18742019 if (!record)
@@ -1889,6 +2034,12 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 14 unmodified lines
18892034 return btrfs_qgroup_trace_extent_post(trans, record);
18902035 }
18912036
2037+/*
2038+ * Inform qgroup to trace all leaf items of data
2039+ *
2040+ * Return 0 for success
2041+ * Return <0 for error(ENOMEM)
2042+ */
18922043 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
18932044 struct extent_buffer *eb)
18942045 {
@@ -1900,7 +2051,7 @@ int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans, · 5 unmodified lines
19002051 u64 bytenr, num_bytes;
19012052
19022053 /* We can be called directly from walk_up_proc() */
1903− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2054+ if (!btrfs_qgroup_full_accounting(fs_info))
19042055 return 0;
19052056
19062057 for (i = 0; i < nr; i++) {
@@ -2276,7 +2427,7 @@ static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans, · 369 unmodified lines
22762427 int level;
22772428 int ret;
22782429
2279− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2430+ if (!btrfs_qgroup_full_accounting(fs_info))
22802431 return 0;
22812432
22822433 /* Wrong parameter order */
@@ -2319,6 +2470,16 @@ out: · 36 unmodified lines
23192470 return ret;
23202471 }
23212472
2473+/*
2474+ * Inform qgroup to trace a whole subtree, including all its child tree
2475+ * blocks and data.
2476+ * The root tree block is specified by @root_eb.
2477+ *
2478+ * Normally used by relocation(tree block swap) and subvolume deletion.
2479+ *
2480+ * Return 0 for success
2481+ * Return <0 for error(ENOMEM or tree search error)
2482+ */
23222483 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
23232484 struct extent_buffer *root_eb,
23242485 u64 root_gen, int root_level)
@@ -2333,7 +2494,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans, · 8 unmodified lines
23332494 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
23342495 BUG_ON(root_eb == NULL);
23352496
2336− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2497+ if (!btrfs_qgroup_full_accounting(fs_info))
23372498 return 0;
23382499
23392500 spin_lock(&fs_info->qgroup_lock);
@@ -2445,62 +2606,64 @@ out: · 105 unmodified lines
24452606 return ret;
24462607 }
24472608
2609+static void qgroup_iterator_nested_add(struct list_head *head, struct btrfs_qgroup *qgroup)
2610+{
2611+ if (!list_empty(&qgroup->nested_iterator))
2612+ return;
2613+
2614+ list_add_tail(&qgroup->nested_iterator, head);
2615+}
2616+
2617+static void qgroup_iterator_nested_clean(struct list_head *head)
2618+{
2619+ while (!list_empty(head)) {
2620+ struct btrfs_qgroup *qgroup;
2621+
2622+ qgroup = list_first_entry(head, struct btrfs_qgroup, nested_iterator);
2623+ list_del_init(&qgroup->nested_iterator);
2624+ }
2625+}
2626+
24482627 #define UPDATE_NEW 0
24492628 #define UPDATE_OLD 1
24502629 /*
24512630 * Walk all of the roots that points to the bytenr and adjust their refcnts.
24522631 */
2453−static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2454− struct ulist *roots, struct ulist *tmp,
2455− struct ulist *qgroups, u64 seq, int update_old)
2632+static void qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2633+ struct ulist *roots, struct list_head *qgroups,
2634+ u64 seq, int update_old)
24562635 {
24572636 struct ulist_node *unode;
24582637 struct ulist_iterator uiter;
2459− struct ulist_node *tmp_unode;
2460− struct ulist_iterator tmp_uiter;
24612638 struct btrfs_qgroup *qg;
2462− int ret = 0;
24632639
24642640 if (!roots)
2465− return 0;
2641+ return;
24662642 ULIST_ITER_INIT(&uiter);
24672643 while ((unode = ulist_next(roots, &uiter))) {
2644+ LIST_HEAD(tmp);
2645+
24682646 qg = find_qgroup_rb(fs_info, unode->val);
24692647 if (!qg)
24702648 continue;
24712649
2472− ulist_reinit(tmp);
2473− ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
2474− GFP_ATOMIC);
2475− if (ret < 0)
2476− return ret;
2477− ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
2478− if (ret < 0)
2479− return ret;
2480− ULIST_ITER_INIT(&tmp_uiter);
2481− while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
2650+ qgroup_iterator_nested_add(qgroups, qg);
2651+ qgroup_iterator_add(&tmp, qg);
2652+ list_for_each_entry(qg, &tmp, iterator) {
24822653 struct btrfs_qgroup_list *glist;
24832654
2484− qg = unode_aux_to_qgroup(tmp_unode);
24852655 if (update_old)
24862656 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
24872657 else
24882658 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2659+
24892660 list_for_each_entry(glist, &qg->groups, next_group) {
2490− ret = ulist_add(qgroups, glist->group->qgroupid,
2491− qgroup_to_aux(glist->group),
2492− GFP_ATOMIC);
2493− if (ret < 0)
2494− return ret;
2495− ret = ulist_add(tmp, glist->group->qgroupid,
2496− qgroup_to_aux(glist->group),
2497− GFP_ATOMIC);
2498− if (ret < 0)
2499− return ret;
2661+ qgroup_iterator_nested_add(qgroups, glist->group);
2662+ qgroup_iterator_add(&tmp, glist->group);
25002663 }
25012664 }
2665+ qgroup_iterator_clean(&tmp);
25022666 }
2503− return 0;
25042667 }
25052668
25062669 /*
@@ -2539,22 +2702,16 @@ static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info, · 32 unmodified lines
25392702 * But this time we don't need to consider other things, the codes and logic
25402703 * is easy to understand now.
25412704 */
2542−static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
2543− struct ulist *qgroups,
2544− u64 nr_old_roots,
2545− u64 nr_new_roots,
2546− u64 num_bytes, u64 seq)
2705+static void qgroup_update_counters(struct btrfs_fs_info *fs_info,
2706+ struct list_head *qgroups, u64 nr_old_roots,
2707+ u64 nr_new_roots, u64 num_bytes, u64 seq)
25472708 {
2548− struct ulist_node *unode;
2549− struct ulist_iterator uiter;
25502709 struct btrfs_qgroup *qg;
2551− u64 cur_new_count, cur_old_count;
25522710
2553− ULIST_ITER_INIT(&uiter);
2554− while ((unode = ulist_next(qgroups, &uiter))) {
2711+ list_for_each_entry(qg, qgroups, nested_iterator) {
2712+ u64 cur_new_count, cur_old_count;
25552713 bool dirty = false;
25562714
2557− qg = unode_aux_to_qgroup(unode);
25582715 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
25592716 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
25602717
@@ -2625,7 +2782,6 @@ static int qgroup_update_counters(struct btrfs_fs_info *fs_info, · 64 unmodified lines
26252782 if (dirty)
26262783 qgroup_dirty(fs_info, qg);
26272784 }
2628− return 0;
26292785 }
26302786
26312787 /*
@@ -2662,8 +2818,7 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 30 unmodified lines
26622818 struct ulist *new_roots)
26632819 {
26642820 struct btrfs_fs_info *fs_info = trans->fs_info;
2665− struct ulist *qgroups = NULL;
2666− struct ulist *tmp = NULL;
2821+ LIST_HEAD(qgroups);
26672822 u64 seq;
26682823 u64 nr_new_roots = 0;
26692824 u64 nr_old_roots = 0;
@@ -2673,7 +2828,7 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 3 unmodified lines
26732828 * If quotas get disabled meanwhile, the resources need to be freed and
26742829 * we can't just exit here.
26752830 */
2676− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
2831+ if (!btrfs_qgroup_full_accounting(fs_info) ||
26772832 fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)
26782833 goto out_free;
26792834
@@ -2697,17 +2852,6 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 17 unmodified lines
26972852 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
26982853 num_bytes, nr_old_roots, nr_new_roots);
26992854
2700− qgroups = ulist_alloc(GFP_NOFS);
2701− if (!qgroups) {
2702− ret = -ENOMEM;
2703− goto out_free;
2704− }
2705− tmp = ulist_alloc(GFP_NOFS);
2706− if (!tmp) {
2707− ret = -ENOMEM;
2708− goto out_free;
2709− }
2710−
27112855 mutex_lock(&fs_info->qgroup_rescan_lock);
27122856 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
27132857 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
@@ -2722,29 +2866,21 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 8 unmodified lines
27222866 seq = fs_info->qgroup_seq;
27232867
27242868 /* Update old refcnts using old_roots */
2725− ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2726− UPDATE_OLD);
2727− if (ret < 0)
2728− goto out;
2869+ qgroup_update_refcnt(fs_info, old_roots, &qgroups, seq, UPDATE_OLD);
27292870
27302871 /* Update new refcnts using new_roots */
2731− ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2732− UPDATE_NEW);
2733− if (ret < 0)
2734− goto out;
2872+ qgroup_update_refcnt(fs_info, new_roots, &qgroups, seq, UPDATE_NEW);
27352873
2736− qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2874+ qgroup_update_counters(fs_info, &qgroups, nr_old_roots, nr_new_roots,
27372875 num_bytes, seq);
27382876
27392877 /*
27402878 * Bump qgroup_seq to avoid seq overlap
27412879 */
27422880 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2743−out:
27442881 spin_unlock(&fs_info->qgroup_lock);
27452882 out_free:
2746− ulist_free(tmp);
2747− ulist_free(qgroups);
2883+ qgroup_iterator_nested_clean(&qgroups);
27482884 ulist_free(old_roots);
27492885 ulist_free(new_roots);
27502886 return ret;
@@ -2761,6 +2897,9 @@ int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans) · 10 unmodified lines
27612897 u64 qgroup_to_skip;
27622898 int ret = 0;
27632899
2900+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
2901+ return 0;
2902+
27642903 delayed_refs = &trans->transaction->delayed_refs;
27652904 qgroup_to_skip = delayed_refs->qgroup_to_skip;
27662905 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
@@ -2876,7 +3015,7 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans) · 109 unmodified lines
28763015 qgroup_mark_inconsistent(fs_info);
28773016 spin_lock(&fs_info->qgroup_lock);
28783017 }
2879− if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
3018+ if (btrfs_qgroup_enabled(fs_info))
28803019 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
28813020 else
28823021 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
@@ -2889,6 +3028,47 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans) · 6 unmodified lines
28893028 return ret;
28903029 }
28913030
3031+static int qgroup_auto_inherit(struct btrfs_fs_info *fs_info,
3032+ u64 inode_rootid,
3033+ struct btrfs_qgroup_inherit **inherit)
3034+{
3035+ int i = 0;
3036+ u64 num_qgroups = 0;
3037+ struct btrfs_qgroup *inode_qg;
3038+ struct btrfs_qgroup_list *qg_list;
3039+ struct btrfs_qgroup_inherit *res;
3040+ size_t struct_sz;
3041+ u64 *qgids;
3042+
3043+ if (*inherit)
3044+ return -EEXIST;
3045+
3046+ inode_qg = find_qgroup_rb(fs_info, inode_rootid);
3047+ if (!inode_qg)
3048+ return -ENOENT;
3049+
3050+ num_qgroups = list_count_nodes(&inode_qg->groups);
3051+
3052+ if (!num_qgroups)
3053+ return 0;
3054+
3055+ struct_sz = struct_size(res, qgroups, num_qgroups);
3056+ if (struct_sz == SIZE_MAX)
3057+ return -ERANGE;
3058+
3059+ res = kzalloc(struct_sz, GFP_NOFS);
3060+ if (!res)
3061+ return -ENOMEM;
3062+ res->num_qgroups = num_qgroups;
3063+ qgids = res->qgroups;
3064+
3065+ list_for_each_entry(qg_list, &inode_qg->groups, next_group)
3066+ qgids[i] = qg_list->group->qgroupid;
3067+
3068+ *inherit = res;
3069+ return 0;
3070+}
3071+
28923072 /*
28933073 * Copy the accounting information between qgroups. This is necessary
28943074 * when a snapshot or a subvolume is created. Throwing an error will
@@ -2896,7 +3076,8 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans) · 1 unmodified line
28963076 * when a readonly fs is a reasonable outcome.
28973077 */
28983078 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
2899− u64 objectid, struct btrfs_qgroup_inherit *inherit)
3079+ u64 objectid, u64 inode_rootid,
3080+ struct btrfs_qgroup_inherit *inherit)
29003081 {
29013082 int ret = 0;
29023083 int i;
@@ -2906,10 +3087,17 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 3 unmodified lines
29063087 struct btrfs_root *quota_root;
29073088 struct btrfs_qgroup *srcgroup;
29083089 struct btrfs_qgroup *dstgroup;
3090+ struct btrfs_qgroup *prealloc;
3091+ struct btrfs_qgroup_list **qlist_prealloc = NULL;
3092+ bool free_inherit = false;
29093093 bool need_rescan = false;
29103094 u32 level_size = 0;
29113095 u64 nums;
29123096
3097+ prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
3098+ if (!prealloc)
3099+ return -ENOMEM;
3100+
29133101 /*
29143102 * There are only two callers of this function.
29153103 *
@@ -2929,7 +3117,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 13 unmodified lines
29293117
29303118 if (!committing)
29313119 mutex_lock(&fs_info->qgroup_ioctl_lock);
2932− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
3120+ if (!btrfs_qgroup_enabled(fs_info))
29333121 goto out;
29343122
29353123 quota_root = fs_info->quota_root;
@@ -2938,6 +3126,13 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 2 unmodified lines
29383126 goto out;
29393127 }
29403128
3129+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE && !inherit) {
3130+ ret = qgroup_auto_inherit(fs_info, inode_rootid, &inherit);
3131+ if (ret)
3132+ goto out;
3133+ free_inherit = true;
3134+ }
3135+
29413136 if (inherit) {
29423137 i_qgroups = (u64 *)(inherit + 1);
29433138 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
@@ -2982,16 +3177,28 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 38 unmodified lines
29823177 goto out;
29833178 }
29843179 ret = 0;
2985− }
29863180
3181+ qlist_prealloc = kcalloc(inherit->num_qgroups,
3182+ sizeof(struct btrfs_qgroup_list *),
3183+ GFP_NOFS);
3184+ if (!qlist_prealloc) {
3185+ ret = -ENOMEM;
3186+ goto out;
3187+ }
3188+ for (int i = 0; i < inherit->num_qgroups; i++) {
3189+ qlist_prealloc[i] = kzalloc(sizeof(struct btrfs_qgroup_list),
3190+ GFP_NOFS);
3191+ if (!qlist_prealloc[i]) {
3192+ ret = -ENOMEM;
3193+ goto out;
3194+ }
3195+ }
3196+ }
29873197
29883198 spin_lock(&fs_info->qgroup_lock);
29893199
2990− dstgroup = add_qgroup_rb(fs_info, objectid);
2991− if (IS_ERR(dstgroup)) {
2992− ret = PTR_ERR(dstgroup);
2993− goto unlock;
2994− }
3200+ dstgroup = add_qgroup_rb(fs_info, prealloc, objectid);
3201+ prealloc = NULL;
29953202
29963203 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
29973204 dstgroup->lim_flags = inherit->lim.flags;
@@ -3003,7 +3210,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 5 unmodified lines
30033210 qgroup_dirty(fs_info, dstgroup);
30043211 }
30053212
3006− if (srcid) {
3213+ if (srcid && btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL) {
30073214 srcgroup = find_qgroup_rb(fs_info, srcid);
30083215 if (!srcgroup)
30093216 goto unlock;
@@ -3038,7 +3245,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 28 unmodified lines
30383245 i_qgroups = (u64 *)(inherit + 1);
30393246 for (i = 0; i < inherit->num_qgroups; ++i) {
30403247 if (*i_qgroups) {
3041− ret = add_relation_rb(fs_info, objectid, *i_qgroups);
3248+ ret = add_relation_rb(fs_info, qlist_prealloc[i], objectid,
3249+ *i_qgroups);
3250+ qlist_prealloc[i] = NULL;
30423251 if (ret)
30433252 goto unlock;
30443253 }
@@ -3102,6 +3311,14 @@ out: · 57 unmodified lines
31023311 mutex_unlock(&fs_info->qgroup_ioctl_lock);
31033312 if (need_rescan)
31043313 qgroup_mark_inconsistent(fs_info);
3314+ if (qlist_prealloc) {
3315+ for (int i = 0; i < inherit->num_qgroups; i++)
3316+ kfree(qlist_prealloc[i]);
3317+ kfree(qlist_prealloc);
3318+ }
3319+ if (free_inherit)
3320+ kfree(inherit);
3321+ kfree(prealloc);
31053322 return ret;
31063323 }
31073324
@@ -3125,8 +3342,7 @@ static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce, · 17 unmodified lines
31253342 struct btrfs_fs_info *fs_info = root->fs_info;
31263343 u64 ref_root = root->root_key.objectid;
31273344 int ret = 0;
3128− struct ulist_node *unode;
3129− struct ulist_iterator uiter;
3345+ LIST_HEAD(qgroup_list);
31303346
31313347 if (!is_fstree(ref_root))
31323348 return 0;
@@ -3146,49 +3362,28 @@ static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce, · 13 unmodified lines
31463362 if (!qgroup)
31473363 goto out;
31483364
3149− /*
3150− * in a first step, we check all affected qgroups if any limits would
3151− * be exceeded
3152− */
3153− ulist_reinit(fs_info->qgroup_ulist);
3154− ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3155− qgroup_to_aux(qgroup), GFP_ATOMIC);
3156− if (ret < 0)
3157− goto out;
3158− ULIST_ITER_INIT(&uiter);
3159− while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3160− struct btrfs_qgroup *qg;
3365+ qgroup_iterator_add(&qgroup_list, qgroup);
3366+ list_for_each_entry(qgroup, &qgroup_list, iterator) {
31613367 struct btrfs_qgroup_list *glist;
31623368
3163− qg = unode_aux_to_qgroup(unode);
3164−
3165− if (enforce && !qgroup_check_limits(qg, num_bytes)) {
3369+ if (enforce && !qgroup_check_limits(qgroup, num_bytes)) {
31663370 ret = -EDQUOT;
31673371 goto out;
31683372 }
31693373
3170− list_for_each_entry(glist, &qg->groups, next_group) {
3171− ret = ulist_add(fs_info->qgroup_ulist,
3172− glist->group->qgroupid,
3173− qgroup_to_aux(glist->group), GFP_ATOMIC);
3174− if (ret < 0)
3175− goto out;
3176− }
3374+ list_for_each_entry(glist, &qgroup->groups, next_group)
3375+ qgroup_iterator_add(&qgroup_list, glist->group);
31773376 }
3377+
31783378 ret = 0;
31793379 /*
31803380 * no limits exceeded, now record the reservation into all qgroups
31813381 */
3182− ULIST_ITER_INIT(&uiter);
3183− while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3184− struct btrfs_qgroup *qg;
3185−
3186− qg = unode_aux_to_qgroup(unode);
3187−
3188− qgroup_rsv_add(fs_info, qg, num_bytes, type);
3189− }
3382+ list_for_each_entry(qgroup, &qgroup_list, iterator)
3383+ qgroup_rsv_add(fs_info, qgroup, num_bytes, type);
31903384
31913385 out:
3386+ qgroup_iterator_clean(&qgroup_list);
31923387 spin_unlock(&fs_info->qgroup_lock);
31933388 return ret;
31943389 }
@@ -3207,9 +3402,7 @@ void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info, · 12 unmodified lines
32073402 enum btrfs_qgroup_rsv_type type)
32083403 {
32093404 struct btrfs_qgroup *qgroup;
3210− struct ulist_node *unode;
3211− struct ulist_iterator uiter;
3212− int ret = 0;
3405+ LIST_HEAD(qgroup_list);
32133406
32143407 if (!is_fstree(ref_root))
32153408 return;
@@ -3237,30 +3430,17 @@ void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info, · 21 unmodified lines
32373430 */
32383431 num_bytes = qgroup->rsv.values[type];
32393432
3240− ulist_reinit(fs_info->qgroup_ulist);
3241− ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3242− qgroup_to_aux(qgroup), GFP_ATOMIC);
3243− if (ret < 0)
3244− goto out;
3245− ULIST_ITER_INIT(&uiter);
3246− while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3247− struct btrfs_qgroup *qg;
3433+ qgroup_iterator_add(&qgroup_list, qgroup);
3434+ list_for_each_entry(qgroup, &qgroup_list, iterator) {
32483435 struct btrfs_qgroup_list *glist;
32493436
3250− qg = unode_aux_to_qgroup(unode);
3251−
3252− qgroup_rsv_release(fs_info, qg, num_bytes, type);
3253−
3254− list_for_each_entry(glist, &qg->groups, next_group) {
3255− ret = ulist_add(fs_info->qgroup_ulist,
3256− glist->group->qgroupid,
3257− qgroup_to_aux(glist->group), GFP_ATOMIC);
3258− if (ret < 0)
3259− goto out;
3437+ qgroup_rsv_release(fs_info, qgroup, num_bytes, type);
3438+ list_for_each_entry(glist, &qgroup->groups, next_group) {
3439+ qgroup_iterator_add(&qgroup_list, glist->group);
32603440 }
32613441 }
3262−
32633442 out:
3443+ qgroup_iterator_clean(&qgroup_list);
32643444 spin_unlock(&fs_info->qgroup_lock);
32653445 }
32663446
@@ -3295,6 +3475,9 @@ static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans, · 28 unmodified lines
32953475 int slot;
32963476 int ret;
32973477
3478+ if (!btrfs_qgroup_full_accounting(fs_info))
3479+ return 1;
3480+
32983481 mutex_lock(&fs_info->qgroup_rescan_lock);
32993482 extent_root = btrfs_extent_root(fs_info,
33003483 fs_info->qgroup_rescan_progress.objectid);
@@ -3375,10 +3558,15 @@ out: · 74 unmodified lines
33753558
33763559 static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
33773560 {
3378− return btrfs_fs_closing(fs_info) ||
3379− test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state) ||
3380− !test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3381− fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN;
3561+ if (btrfs_fs_closing(fs_info))
3562+ return true;
3563+ if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
3564+ return true;
3565+ if (!btrfs_qgroup_enabled(fs_info))
3566+ return true;
3567+ if (fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN)
3568+ return true;
3569+ return false;
33823570 }
33833571
33843572 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
@@ -3392,6 +3580,9 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) · 7 unmodified lines
33923580 bool stopped = false;
33933581 bool did_leaf_rescans = false;
33943582
3583+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
3584+ return;
3585+
33953586 path = btrfs_alloc_path();
33963587 if (!path)
33973588 goto out;
@@ -3495,6 +3686,11 @@ qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, · 97 unmodified lines
34953686 {
34963687 int ret = 0;
34973688
3689+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE) {
3690+ btrfs_warn(fs_info, "qgroup rescan init failed, running in simple mode");
3691+ return -EINVAL;
3692+ }
3693+
34983694 if (!init_flags) {
34993695 /* we're resuming qgroup rescan at mount time */
35003696 if (!(fs_info->qgroup_flags &
@@ -3525,7 +3721,7 @@ qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, · 24 unmodified lines
35253721 btrfs_warn(fs_info,
35263722 "qgroup rescan init failed, qgroup is not enabled");
35273723 ret = -EINVAL;
3528− } else if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
3724+ } else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
35293725 /* Quota disable is in progress */
35303726 ret = -EBUSY;
35313727 }
@@ -3546,7 +3742,7 @@ qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, · 14 unmodified lines
35463742 mutex_unlock(&fs_info->qgroup_rescan_lock);
35473743
35483744 btrfs_init_work(&fs_info->qgroup_rescan_work,
3549− btrfs_qgroup_rescan_worker, NULL, NULL);
3745+ btrfs_qgroup_rescan_worker, NULL);
35503746 return 0;
35513747 }
35523748
@@ -3784,7 +3980,7 @@ static int qgroup_reserve_data(struct btrfs_inode *inode, · 231 unmodified lines
37843980 u64 to_reserve;
37853981 int ret;
37863982
3787− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
3983+ if (btrfs_qgroup_mode(root->fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
37883984 !is_fstree(root->root_key.objectid) || len == 0)
37893985 return 0;
37903986
@@ -3916,8 +4112,12 @@ static int __btrfs_qgroup_release_data(struct btrfs_inode *inode, · 125 unmodified lines
39164112 int trace_op = QGROUP_RELEASE;
39174113 int ret;
39184114
3919− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))
3920− return 0;
4115+ if (btrfs_qgroup_mode(inode->root->fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
4116+ extent_changeset_init(&changeset);
4117+ return clear_record_extent_bits(&inode->io_tree, start,
4118+ start + len - 1,
4119+ EXTENT_QGROUP_RESERVED, &changeset);
4120+ }
39214121
39224122 /* In release case, we shouldn't have @reserved */
39234123 WARN_ON(!free && reserved);
@@ -4027,7 +4227,7 @@ int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, · 103 unmodified lines
40274227 struct btrfs_fs_info *fs_info = root->fs_info;
40284228 int ret;
40294229
4030− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
4230+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
40314231 !is_fstree(root->root_key.objectid) || num_bytes == 0)
40324232 return 0;
40334233
@@ -4064,11 +4264,15 @@ int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, · 30 unmodified lines
40644264 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
40654265 }
40664266
4267+/*
4268+ * Per-transaction meta reservation should be all freed at transaction commit
4269+ * time
4270+ */
40674271 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
40684272 {
40694273 struct btrfs_fs_info *fs_info = root->fs_info;
40704274
4071− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
4275+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
40724276 !is_fstree(root->root_key.objectid))
40734277 return;
40744278
@@ -4084,7 +4288,7 @@ void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes, · 9 unmodified lines
40844288 {
40854289 struct btrfs_fs_info *fs_info = root->fs_info;
40864290
4087− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
4291+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
40884292 !is_fstree(root->root_key.objectid))
40894293 return;
40904294
@@ -4104,9 +4308,7 @@ static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root, · 13 unmodified lines
41044308 int num_bytes)
41054309 {
41064310 struct btrfs_qgroup *qgroup;
4107− struct ulist_node *unode;
4108− struct ulist_iterator uiter;
4109− int ret = 0;
4311+ LIST_HEAD(qgroup_list);
41104312
41114313 if (num_bytes == 0)
41124314 return;
@@ -4117,39 +4319,35 @@ static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root, · 4 unmodified lines
41174319 qgroup = find_qgroup_rb(fs_info, ref_root);
41184320 if (!qgroup)
41194321 goto out;
4120− ulist_reinit(fs_info->qgroup_ulist);
4121− ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
4122− qgroup_to_aux(qgroup), GFP_ATOMIC);
4123− if (ret < 0)
4124− goto out;
4125− ULIST_ITER_INIT(&uiter);
4126− while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
4127− struct btrfs_qgroup *qg;
4128− struct btrfs_qgroup_list *glist;
41294322
4130− qg = unode_aux_to_qgroup(unode);
4323+ qgroup_iterator_add(&qgroup_list, qgroup);
4324+ list_for_each_entry(qgroup, &qgroup_list, iterator) {
4325+ struct btrfs_qgroup_list *glist;
41314326
4132− qgroup_rsv_release(fs_info, qg, num_bytes,
4327+ qgroup_rsv_release(fs_info, qgroup, num_bytes,
41334328 BTRFS_QGROUP_RSV_META_PREALLOC);
4134− qgroup_rsv_add(fs_info, qg, num_bytes,
4329+ qgroup_rsv_add(fs_info, qgroup, num_bytes,
41354330 BTRFS_QGROUP_RSV_META_PERTRANS);
4136− list_for_each_entry(glist, &qg->groups, next_group) {
4137− ret = ulist_add(fs_info->qgroup_ulist,
4138− glist->group->qgroupid,
4139− qgroup_to_aux(glist->group), GFP_ATOMIC);
4140− if (ret < 0)
4141− goto out;
4142− }
4331+
4332+ list_for_each_entry(glist, &qgroup->groups, next_group)
4333+ qgroup_iterator_add(&qgroup_list, glist->group);
41434334 }
41444335 out:
4336+ qgroup_iterator_clean(&qgroup_list);
41454337 spin_unlock(&fs_info->qgroup_lock);
41464338 }
41474339
4340+/*
4341+ * Convert @num_bytes of META_PREALLOCATED reservation to META_PERTRANS.
4342+ *
4343+ * This is called when preallocated meta reservation needs to be used.
4344+ * Normally after btrfs_join_transaction() call.
4345+ */
41484346 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
41494347 {
41504348 struct btrfs_fs_info *fs_info = root->fs_info;
41514349
4152− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
4350+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
41534351 !is_fstree(root->root_key.objectid))
41544352 return;
41554353 /* Same as btrfs_qgroup_free_meta_prealloc() */
@@ -4257,7 +4455,7 @@ int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans, · 101 unmodified lines
42574455 int level = btrfs_header_level(subvol_parent) - 1;
42584456 int ret = 0;
42594457
4260− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4458+ if (!btrfs_qgroup_full_accounting(fs_info))
42614459 return 0;
42624460
42634461 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
@@ -4367,7 +4565,7 @@ int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans, · 103 unmodified lines
43674565 int ret = 0;
43684566 int i;
43694567
4370− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4568+ if (!btrfs_qgroup_full_accounting(fs_info))
43714569 return 0;
43724570 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
43734571 return 0;
@@ -4450,3 +4648,53 @@ void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans) · 76 unmodified lines
44504648 }
44514649 *root = RB_ROOT;
44524650 }
4651+
4652+int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
4653+ struct btrfs_squota_delta *delta)
4654+{
4655+ int ret;
4656+ struct btrfs_qgroup *qgroup;
4657+ struct btrfs_qgroup *qg;
4658+ LIST_HEAD(qgroup_list);
4659+ u64 root = delta->root;
4660+ u64 num_bytes = delta->num_bytes;
4661+ const int sign = (delta->is_inc ? 1 : -1);
4662+
4663+ if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE)
4664+ return 0;
4665+
4666+ if (!is_fstree(root))
4667+ return 0;
4668+
4669+ /* If the extent predates enabling quotas, don't count it. */
4670+ if (delta->generation < fs_info->qgroup_enable_gen)
4671+ return 0;
4672+
4673+ spin_lock(&fs_info->qgroup_lock);
4674+ qgroup = find_qgroup_rb(fs_info, root);
4675+ if (!qgroup) {
4676+ ret = -ENOENT;
4677+ goto out;
4678+ }
4679+
4680+ ret = 0;
4681+ qgroup_iterator_add(&qgroup_list, qgroup);
4682+ list_for_each_entry(qg, &qgroup_list, iterator) {
4683+ struct btrfs_qgroup_list *glist;
4684+
4685+ qg->excl += num_bytes * sign;
4686+ qg->rfer += num_bytes * sign;
4687+ qgroup_dirty(fs_info, qg);
4688+
4689+ list_for_each_entry(glist, &qg->groups, next_group)
4690+ qgroup_iterator_add(&qgroup_list, glist->group);
4691+ }
4692+ qgroup_iterator_clean(&qgroup_list);
4693+
4694+out:
4695+ spin_unlock(&fs_info->qgroup_lock);
4696+ if (!ret && delta->rsv_bytes)
4697+ btrfs_qgroup_free_refroot(fs_info, root, delta->rsv_bytes,
4698+ BTRFS_QGROUP_RSV_DATA);
4699+ return ret;
4700+}
fs/btrfs/qgroup.h+67−82
@@ -101,8 +101,15 @@ · 100 unmodified lines
101101 * subtree rescan for them.
102102 */
103103
104−#define BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN (1UL << 3)
105−#define BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING (1UL << 4)
104+/*
105+ * These flags share the flags field of the btrfs_qgroup_status_item with the
106+ * persisted flags defined in btrfs_tree.h.
107+ *
108+ * To minimize the chance of collision with new persisted status flags, these
109+ * count backwards from the MSB.
110+ */
111+#define BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN (1ULL << 63)
112+#define BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING (1ULL << 62)
106113
107114 /*
108115 * Record a dirty extent, and info qgroup to update quota on it
@@ -220,6 +227,33 @@ struct btrfs_qgroup { · 111 unmodified lines
220227 struct list_head groups; /* groups this group is member of */
221228 struct list_head members; /* groups that are members of this group */
222229 struct list_head dirty; /* dirty groups */
230+
231+ /*
232+ * For qgroup iteration usage.
233+ *
234+ * The iteration list should always be empty until qgroup_iterator_add()
235+ * is called. And should be reset to empty after the iteration is
236+ * finished.
237+ */
238+ struct list_head iterator;
239+
240+ /*
241+ * For nested iterator usage.
242+ *
243+ * Here we support at most one level of nested iterator calls like:
244+ *
245+ * LIST_HEAD(all_qgroups);
246+ * {
247+ * LIST_HEAD(local_qgroups);
248+ * qgroup_iterator_add(local_qgroups, qg);
249+ * qgroup_iterator_nested_add(all_qgroups, qg);
250+ * do_some_work(local_qgroups);
251+ * qgroup_iterator_clean(local_qgroups);
252+ * }
253+ * do_some_work(all_qgroups);
254+ * qgroup_iterator_nested_clean(all_qgroups);
255+ */
256+ struct list_head nested_iterator;
223257 struct rb_node node; /* tree of qgroups */
224258
225259 /*
@@ -235,6 +269,21 @@ struct btrfs_qgroup { · 9 unmodified lines
235269 struct kobject kobj;
236270 };
237271
272+struct btrfs_squota_delta {
273+ /* The fstree root this delta counts against. */
274+ u64 root;
275+ /* The number of bytes in the extent being counted. */
276+ u64 num_bytes;
277+ /* The number of bytes reserved for this extent. */
278+ u64 rsv_bytes;
279+ /* The generation the extent was created in. */
280+ u64 generation;
281+ /* Whether we are using or freeing the extent. */
282+ bool is_inc;
283+ /* Whether the extent is data or metadata. */
284+ bool is_data;
285+};
286+
238287 static inline u64 btrfs_qgroup_subvolid(u64 qgroupid)
239288 {
240289 return (qgroupid & ((1ULL << BTRFS_QGROUP_LEVEL_SHIFT) - 1));
@@ -249,14 +298,23 @@ enum { · 8 unmodified lines
249298 ENUM_BIT(QGROUP_FREE),
250299 };
251300
252−int btrfs_quota_enable(struct btrfs_fs_info *fs_info);
301+enum btrfs_qgroup_mode {
302+ BTRFS_QGROUP_MODE_DISABLED,
303+ BTRFS_QGROUP_MODE_FULL,
304+ BTRFS_QGROUP_MODE_SIMPLE
305+};
306+
307+enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info);
308+bool btrfs_qgroup_enabled(struct btrfs_fs_info *fs_info);
309+bool btrfs_qgroup_full_accounting(struct btrfs_fs_info *fs_info);
310+int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
311+ struct btrfs_ioctl_quota_ctl_args *quota_ctl_args);
253312 int btrfs_quota_disable(struct btrfs_fs_info *fs_info);
254313 int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
255314 void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
256315 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
257316 bool interruptible);
258−int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
259− u64 dst);
317+int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst);
260318 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
261319 u64 dst);
262320 int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);
@@ -267,80 +325,16 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info); · 4 unmodified lines
267325 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
268326 struct btrfs_delayed_extent_op;
269327
270−/*
271− * Inform qgroup to trace one dirty extent, its info is recorded in @record.
272− * So qgroup can account it at transaction committing time.
273− *
274− * No lock version, caller must acquire delayed ref lock and allocated memory,
275− * then call btrfs_qgroup_trace_extent_post() after exiting lock context.
276− *
277− * Return 0 for success insert
278− * Return >0 for existing record, caller can free @record safely.
279− * Error is not possible
280− */
281328 int btrfs_qgroup_trace_extent_nolock(
282329 struct btrfs_fs_info *fs_info,
283330 struct btrfs_delayed_ref_root *delayed_refs,
284331 struct btrfs_qgroup_extent_record *record);
285−
286−/*
287− * Post handler after qgroup_trace_extent_nolock().
288− *
289− * NOTE: Current qgroup does the expensive backref walk at transaction
290− * committing time with TRANS_STATE_COMMIT_DOING, this blocks incoming
291− * new transaction.
292− * This is designed to allow btrfs_find_all_roots() to get correct new_roots
293− * result.
294− *
295− * However for old_roots there is no need to do backref walk at that time,
296− * since we search commit roots to walk backref and result will always be
297− * correct.
298− *
299− * Due to the nature of no lock version, we can't do backref there.
300− * So we must call btrfs_qgroup_trace_extent_post() after exiting
301− * spinlock context.
302− *
303− * TODO: If we can fix and prove btrfs_find_all_roots() can get correct result
304− * using current root, then we can move all expensive backref walk out of
305− * transaction committing, but not now as qgroup accounting will be wrong again.
306− */
307332 int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
308333 struct btrfs_qgroup_extent_record *qrecord);
309−
310−/*
311− * Inform qgroup to trace one dirty extent, specified by @bytenr and
312− * @num_bytes.
313− * So qgroup can account it at commit trans time.
314− *
315− * Better encapsulated version, with memory allocation and backref walk for
316− * commit roots.
317− * So this can sleep.
318− *
319− * Return 0 if the operation is done.
320− * Return <0 for error, like memory allocation failure or invalid parameter
321− * (NULL trans)
322− */
323334 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
324335 u64 num_bytes);
325−
326−/*
327− * Inform qgroup to trace all leaf items of data
328− *
329− * Return 0 for success
330− * Return <0 for error(ENOMEM)
331− */
332336 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
333337 struct extent_buffer *eb);
334−/*
335− * Inform qgroup to trace a whole subtree, including all its child tree
336− * blocks and data.
337− * The root tree block is specified by @root_eb.
338− *
339− * Normally used by relocation(tree block swap) and subvolume deletion.
340− *
341− * Return 0 for success
342− * Return <0 for error(ENOMEM or tree search error)
343− */
344338 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
345339 struct extent_buffer *root_eb,
346340 u64 root_gen, int root_level);
@@ -350,7 +344,8 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 3 unmodified lines
350344 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans);
351345 int btrfs_run_qgroups(struct btrfs_trans_handle *trans);
352346 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
353− u64 objectid, struct btrfs_qgroup_inherit *inherit);
347+ u64 objectid, u64 inode_rootid,
348+ struct btrfs_qgroup_inherit *inherit);
354349 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
355350 u64 ref_root, u64 num_bytes,
356351 enum btrfs_qgroup_rsv_type type);
@@ -408,20 +403,8 @@ static inline void btrfs_qgroup_free_meta_prealloc(struct btrfs_root *root, · 51 unmodified lines
408403 BTRFS_QGROUP_RSV_META_PREALLOC);
409404 }
410405
411−/*
412− * Per-transaction meta reservation should be all freed at transaction commit
413− * time
414− */
415406 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root);
416−
417−/*
418− * Convert @num_bytes of META_PREALLOCATED reservation to META_PERTRANS.
419− *
420− * This is called when preallocated meta reservation needs to be used.
421− * Normally after btrfs_join_transaction() call.
422− */
423407 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes);
424−
425408 void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode);
426409
427410 /* btrfs_qgroup_swapped_blocks related functions */
@@ -439,5 +422,7 @@ int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans, · 11 unmodified lines
439422 struct btrfs_root *root, struct extent_buffer *eb);
440423 void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans);
441424 bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info);
425+int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
426+ struct btrfs_squota_delta *delta);
442427
443428 #endif
fs/btrfs/raid-stripe-tree.c+274−0
@@ -0,0 +1,274 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * Copyright (C) 2023 Western Digital Corporation or its affiliates.
4+ */
5+
6+#include <linux/btrfs_tree.h>
7+#include "ctree.h"
8+#include "fs.h"
9+#include "accessors.h"
10+#include "transaction.h"
11+#include "disk-io.h"
12+#include "raid-stripe-tree.h"
13+#include "volumes.h"
14+#include "misc.h"
15+#include "print-tree.h"
16+
17+int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length)
18+{
19+ struct btrfs_fs_info *fs_info = trans->fs_info;
20+ struct btrfs_root *stripe_root = fs_info->stripe_root;
21+ struct btrfs_path *path;
22+ struct btrfs_key key;
23+ struct extent_buffer *leaf;
24+ u64 found_start;
25+ u64 found_end;
26+ u64 end = start + length;
27+ int slot;
28+ int ret;
29+
30+ if (!stripe_root)
31+ return 0;
32+
33+ path = btrfs_alloc_path();
34+ if (!path)
35+ return -ENOMEM;
36+
37+ while (1) {
38+ key.objectid = start;
39+ key.type = BTRFS_RAID_STRIPE_KEY;
40+ key.offset = length;
41+
42+ ret = btrfs_search_slot(trans, stripe_root, &key, path, -1, 1);
43+ if (ret < 0)
44+ break;
45+ if (ret > 0) {
46+ ret = 0;
47+ if (path->slots[0] == 0)
48+ break;
49+ path->slots[0]--;
50+ }
51+
52+ leaf = path->nodes[0];
53+ slot = path->slots[0];
54+ btrfs_item_key_to_cpu(leaf, &key, slot);
55+ found_start = key.objectid;
56+ found_end = found_start + key.offset;
57+
58+ /* That stripe ends before we start, we're done. */
59+ if (found_end <= start)
60+ break;
61+
62+ trace_btrfs_raid_extent_delete(fs_info, start, end,
63+ found_start, found_end);
64+
65+ ASSERT(found_start >= start && found_end <= end);
66+ ret = btrfs_del_item(trans, stripe_root, path);
67+ if (ret)
68+ break;
69+
70+ btrfs_release_path(path);
71+ }
72+
73+ btrfs_free_path(path);
74+ return ret;
75+}
76+
77+static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
78+ struct btrfs_io_context *bioc)
79+{
80+ struct btrfs_fs_info *fs_info = trans->fs_info;
81+ struct btrfs_key stripe_key;
82+ struct btrfs_root *stripe_root = fs_info->stripe_root;
83+ const int num_stripes = btrfs_bg_type_to_factor(bioc->map_type);
84+ u8 encoding = btrfs_bg_flags_to_raid_index(bioc->map_type);
85+ struct btrfs_stripe_extent *stripe_extent;
86+ const size_t item_size = struct_size(stripe_extent, strides, num_stripes);
87+ int ret;
88+
89+ stripe_extent = kzalloc(item_size, GFP_NOFS);
90+ if (!stripe_extent) {
91+ btrfs_abort_transaction(trans, -ENOMEM);
92+ btrfs_end_transaction(trans);
93+ return -ENOMEM;
94+ }
95+
96+ trace_btrfs_insert_one_raid_extent(fs_info, bioc->logical, bioc->size,
97+ num_stripes);
98+ btrfs_set_stack_stripe_extent_encoding(stripe_extent, encoding);
99+ for (int i = 0; i < num_stripes; i++) {
100+ u64 devid = bioc->stripes[i].dev->devid;
101+ u64 physical = bioc->stripes[i].physical;
102+ u64 length = bioc->stripes[i].length;
103+ struct btrfs_raid_stride *raid_stride = &stripe_extent->strides[i];
104+
105+ if (length == 0)
106+ length = bioc->size;
107+
108+ btrfs_set_stack_raid_stride_devid(raid_stride, devid);
109+ btrfs_set_stack_raid_stride_physical(raid_stride, physical);
110+ }
111+
112+ stripe_key.objectid = bioc->logical;
113+ stripe_key.type = BTRFS_RAID_STRIPE_KEY;
114+ stripe_key.offset = bioc->size;
115+
116+ ret = btrfs_insert_item(trans, stripe_root, &stripe_key, stripe_extent,
117+ item_size);
118+ if (ret)
119+ btrfs_abort_transaction(trans, ret);
120+
121+ kfree(stripe_extent);
122+
123+ return ret;
124+}
125+
126+int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
127+ struct btrfs_ordered_extent *ordered_extent)
128+{
129+ struct btrfs_io_context *bioc;
130+ int ret;
131+
132+ if (!btrfs_fs_incompat(trans->fs_info, RAID_STRIPE_TREE))
133+ return 0;
134+
135+ list_for_each_entry(bioc, &ordered_extent->bioc_list, rst_ordered_entry) {
136+ ret = btrfs_insert_one_raid_extent(trans, bioc);
137+ if (ret)
138+ return ret;
139+ }
140+
141+ while (!list_empty(&ordered_extent->bioc_list)) {
142+ bioc = list_first_entry(&ordered_extent->bioc_list,
143+ typeof(*bioc), rst_ordered_entry);
144+ list_del(&bioc->rst_ordered_entry);
145+ btrfs_put_bioc(bioc);
146+ }
147+
148+ return ret;
149+}
150+
151+int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
152+ u64 logical, u64 *length, u64 map_type,
153+ u32 stripe_index, struct btrfs_io_stripe *stripe)
154+{
155+ struct btrfs_root *stripe_root = fs_info->stripe_root;
156+ struct btrfs_stripe_extent *stripe_extent;
157+ struct btrfs_key stripe_key;
158+ struct btrfs_key found_key;
159+ struct btrfs_path *path;
160+ struct extent_buffer *leaf;
161+ const u64 end = logical + *length;
162+ int num_stripes;
163+ u8 encoding;
164+ u64 offset;
165+ u64 found_logical;
166+ u64 found_length;
167+ u64 found_end;
168+ int slot;
169+ int ret;
170+
171+ stripe_key.objectid = logical;
172+ stripe_key.type = BTRFS_RAID_STRIPE_KEY;
173+ stripe_key.offset = 0;
174+
175+ path = btrfs_alloc_path();
176+ if (!path)
177+ return -ENOMEM;
178+
179+ if (stripe->is_scrub) {
180+ path->skip_locking = 1;
181+ path->search_commit_root = 1;
182+ }
183+
184+ ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
185+ if (ret < 0)
186+ goto free_path;
187+ if (ret) {
188+ if (path->slots[0] != 0)
189+ path->slots[0]--;
190+ }
191+
192+ while (1) {
193+ leaf = path->nodes[0];
194+ slot = path->slots[0];
195+
196+ btrfs_item_key_to_cpu(leaf, &found_key, slot);
197+ found_logical = found_key.objectid;
198+ found_length = found_key.offset;
199+ found_end = found_logical + found_length;
200+
201+ if (found_logical > end) {
202+ ret = -ENOENT;
203+ goto out;
204+ }
205+
206+ if (in_range(logical, found_logical, found_length))
207+ break;
208+
209+ ret = btrfs_next_item(stripe_root, path);
210+ if (ret)
211+ goto out;
212+ }
213+
214+ offset = logical - found_logical;
215+
216+ /*
217+ * If we have a logically contiguous, but physically non-continuous
218+ * range, we need to split the bio. Record the length after which we
219+ * must split the bio.
220+ */
221+ if (end > found_end)
222+ *length -= end - found_end;
223+
224+ num_stripes = btrfs_num_raid_stripes(btrfs_item_size(leaf, slot));
225+ stripe_extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
226+ encoding = btrfs_stripe_extent_encoding(leaf, stripe_extent);
227+
228+ if (encoding != btrfs_bg_flags_to_raid_index(map_type)) {
229+ ret = -EUCLEAN;
230+ btrfs_handle_fs_error(fs_info, ret,
231+ "on-disk stripe encoding %d doesn't match RAID index %d",
232+ encoding,
233+ btrfs_bg_flags_to_raid_index(map_type));
234+ goto out;
235+ }
236+
237+ for (int i = 0; i < num_stripes; i++) {
238+ struct btrfs_raid_stride *stride = &stripe_extent->strides[i];
239+ u64 devid = btrfs_raid_stride_devid(leaf, stride);
240+ u64 physical = btrfs_raid_stride_physical(leaf, stride);
241+
242+ if (devid != stripe->dev->devid)
243+ continue;
244+
245+ if ((map_type & BTRFS_BLOCK_GROUP_DUP) && stripe_index != i)
246+ continue;
247+
248+ stripe->physical = physical + offset;
249+
250+ trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
251+ stripe->physical, devid);
252+
253+ ret = 0;
254+ goto free_path;
255+ }
256+
257+ /* If we're here, we haven't found the requested devid in the stripe. */
258+ ret = -ENOENT;
259+out:
260+ if (ret > 0)
261+ ret = -ENOENT;
262+ if (ret && ret != -EIO && !stripe->is_scrub) {
263+ if (IS_ENABLED(CONFIG_BTRFS_DEBUG))
264+ btrfs_print_tree(leaf, 1);
265+ btrfs_err(fs_info,
266+ "cannot find raid-stripe for logical [%llu, %llu] devid %llu, profile %s",
267+ logical, logical + *length, stripe->dev->devid,
268+ btrfs_bg_type_to_raid_name(map_type));
269+ }
270+free_path:
271+ btrfs_free_path(path);
272+
273+ return ret;
274+}
fs/btrfs/raid-stripe-tree.h+50−0
@@ -0,0 +1,50 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2023 Western Digital Corporation or its affiliates.
4+ */
5+
6+#ifndef BTRFS_RAID_STRIPE_TREE_H
7+#define BTRFS_RAID_STRIPE_TREE_H
8+
9+#define BTRFS_RST_SUPP_BLOCK_GROUP_MASK (BTRFS_BLOCK_GROUP_DUP | \
10+ BTRFS_BLOCK_GROUP_RAID1_MASK | \
11+ BTRFS_BLOCK_GROUP_RAID0 | \
12+ BTRFS_BLOCK_GROUP_RAID10)
13+
14+struct btrfs_io_context;
15+struct btrfs_io_stripe;
16+struct btrfs_ordered_extent;
17+struct btrfs_trans_handle;
18+
19+int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length);
20+int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
21+ u64 logical, u64 *length, u64 map_type,
22+ u32 stripe_index, struct btrfs_io_stripe *stripe);
23+int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
24+ struct btrfs_ordered_extent *ordered_extent);
25+
26+static inline bool btrfs_need_stripe_tree_update(struct btrfs_fs_info *fs_info,
27+ u64 map_type)
28+{
29+ u64 type = map_type & BTRFS_BLOCK_GROUP_TYPE_MASK;
30+ u64 profile = map_type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
31+
32+ if (!btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE))
33+ return false;
34+
35+ if (type != BTRFS_BLOCK_GROUP_DATA)
36+ return false;
37+
38+ if (profile & BTRFS_RST_SUPP_BLOCK_GROUP_MASK)
39+ return true;
40+
41+ return false;
42+}
43+
44+static inline int btrfs_num_raid_stripes(u32 item_size)
45+{
46+ return (item_size - offsetof(struct btrfs_stripe_extent, strides)) /
47+ sizeof(struct btrfs_raid_stride);
48+}
49+
50+#endif
fs/btrfs/ref-verify.c+6−3
@@ -485,6 +485,9 @@ static int process_extent_item(struct btrfs_fs_info *fs_info, · 484 unmodified lines
485485 ret = add_shared_data_ref(fs_info, offset, count,
486486 key->objectid, key->offset);
487487 break;
488+ case BTRFS_EXTENT_OWNER_REF_KEY:
489+ WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
490+ break;
488491 default:
489492 btrfs_err(fs_info, "invalid key type in iref");
490493 ret = -EINVAL;
@@ -652,7 +655,7 @@ static void dump_block_entry(struct btrfs_fs_info *fs_info, · 161 unmodified lines
652655 }
653656
654657 /*
655− * btrfs_ref_tree_mod: called when we modify a ref for a bytenr
658+ * Called when we modify a ref for a bytenr.
656659 *
657660 * This will add an action item to the given bytenr and do sanity checks to make
658661 * sure we haven't messed something up. If we are making a new allocation and
@@ -681,10 +684,10 @@ int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info, · 22 unmodified lines
681684
682685 if (generic_ref->type == BTRFS_REF_METADATA) {
683686 if (!parent)
684− ref_root = generic_ref->tree_ref.owning_root;
687+ ref_root = generic_ref->tree_ref.ref_root;
685688 owner = generic_ref->tree_ref.level;
686689 } else if (!parent) {
687− ref_root = generic_ref->data_ref.owning_root;
690+ ref_root = generic_ref->data_ref.ref_root;
688691 owner = generic_ref->data_ref.ino;
689692 offset = generic_ref->data_ref.offset;
690693 }
fs/btrfs/reflink.c+1−2
@@ -25,7 +25,6 @@ static int clone_finish_inode_update(struct btrfs_trans_handle *trans, · 24 unmodified lines
2525 const u64 olen,
2626 int no_time_update)
2727 {
28− struct btrfs_root *root = BTRFS_I(inode)->root;
2928 int ret;
3029
3130 inode_inc_iversion(inode);
@@ -43,7 +42,7 @@ static int clone_finish_inode_update(struct btrfs_trans_handle *trans, · 11 unmodified lines
4342 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
4443 }
4544
46− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
45+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
4746 if (ret) {
4847 btrfs_abort_transaction(trans, ret);
4948 btrfs_end_transaction(trans);
fs/btrfs/relocation.c+127−81
@@ -111,8 +111,8 @@ struct tree_block { · 110 unmodified lines
111111 }; /* Use rb_simple_node for search/insert */
112112 u64 owner;
113113 struct btrfs_key key;
114− unsigned int level:8;
115− unsigned int key_ready:1;
114+ u8 level;
115+ bool key_ready;
116116 };
117117
118118 #define MAX_EXTENTS 128
@@ -122,6 +122,13 @@ struct file_extent_cluster { · 3 unmodified lines
122122 u64 end;
123123 u64 boundary[MAX_EXTENTS];
124124 unsigned int nr;
125+ u64 owning_root;
126+};
127+
128+/* Stages of data relocation. */
129+enum reloc_stage {
130+ MOVE_DATA_EXTENTS,
131+ UPDATE_DATA_PTRS
125132 };
126133
127134 struct reloc_control {
@@ -155,16 +162,12 @@ struct reloc_control { · 27 unmodified lines
155162 u64 search_start;
156163 u64 extents_found;
157164
158− unsigned int stage:8;
159− unsigned int create_reloc_tree:1;
160− unsigned int merge_reloc_tree:1;
161− unsigned int found_file_extent:1;
165+ enum reloc_stage stage;
166+ bool create_reloc_tree;
167+ bool merge_reloc_tree;
168+ bool found_file_extent;
162169 };
163170
164−/* stages of data relocation */
165−#define MOVE_DATA_EXTENTS 0
166−#define UPDATE_DATA_PTRS 1
167−
168171 static void mark_block_processed(struct reloc_control *rc,
169172 struct btrfs_backref_node *node)
170173 {
@@ -180,13 +183,6 @@ static void mark_block_processed(struct reloc_control *rc, · 9 unmodified lines
180183 node->processed = 1;
181184 }
182185
183−
184−static void mapping_tree_init(struct mapping_tree *tree)
185−{
186− tree->rb_root = RB_ROOT;
187− spin_lock_init(&tree->lock);
188−}
189−
190186 /*
191187 * walk up backref nodes until reach node presents tree root
192188 */
@@ -299,7 +295,7 @@ static int update_backref_cache(struct btrfs_trans_handle *trans, · 106 unmodified lines
299295 return 1;
300296 }
301297
302−static bool reloc_root_is_dead(struct btrfs_root *root)
298+static bool reloc_root_is_dead(const struct btrfs_root *root)
303299 {
304300 /*
305301 * Pair with set_bit/clear_bit in clean_dirty_subvols and
@@ -320,7 +316,7 @@ static bool reloc_root_is_dead(struct btrfs_root *root) · 14 unmodified lines
320316 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
321317 * special case.
322318 */
323−static bool have_reloc_root(struct btrfs_root *root)
319+static bool have_reloc_root(const struct btrfs_root *root)
324320 {
325321 if (reloc_root_is_dead(root))
326322 return false;
@@ -329,31 +325,30 @@ static bool have_reloc_root(struct btrfs_root *root) · 2 unmodified lines
329325 return true;
330326 }
331327
332−int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
328+bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root)
333329 {
334330 struct btrfs_root *reloc_root;
335331
336332 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
337− return 0;
333+ return false;
338334
339335 /* This root has been merged with its reloc tree, we can ignore it */
340336 if (reloc_root_is_dead(root))
341− return 1;
337+ return true;
342338
343339 reloc_root = root->reloc_root;
344340 if (!reloc_root)
345− return 0;
341+ return false;
346342
347343 if (btrfs_header_generation(reloc_root->commit_root) ==
348344 root->fs_info->running_transaction->transid)
349− return 0;
345+ return false;
350346 /*
351− * if there is reloc tree and it was created in previous
352− * transaction backref lookup can find the reloc tree,
353− * so backref node for the fs tree root is useless for
354− * relocation.
347+ * If there is reloc tree and it was created in previous transaction
348+ * backref lookup can find the reloc tree, so backref node for the fs
349+ * tree root is useless for relocation.
355350 */
356− return 1;
351+ return true;
357352 }
358353
359354 /*
@@ -547,7 +542,7 @@ out: · 187 unmodified lines
547542 */
548543 static int clone_backref_node(struct btrfs_trans_handle *trans,
549544 struct reloc_control *rc,
550− struct btrfs_root *src,
545+ const struct btrfs_root *src,
551546 struct btrfs_root *dest)
552547 {
553548 struct btrfs_root *reloc_root = src->reloc_root;
@@ -632,7 +627,7 @@ fail: · 78 unmodified lines
632627 /*
633628 * helper to add 'address of tree root -> reloc tree' mapping
634629 */
635−static int __must_check __add_reloc_root(struct btrfs_root *root)
630+static int __add_reloc_root(struct btrfs_root *root)
636631 {
637632 struct btrfs_fs_info *fs_info = root->fs_info;
638633 struct rb_node *rb_node;
@@ -1159,7 +1154,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 520 unmodified lines
11591154
11601155 key.offset -= btrfs_file_extent_offset(leaf, fi);
11611156 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1162− num_bytes, parent);
1157+ num_bytes, parent, root->root_key.objectid);
11631158 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
11641159 key.objectid, key.offset,
11651160 root->root_key.objectid, false);
@@ -1170,7 +1165,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 4 unmodified lines
11701165 }
11711166
11721167 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1173− num_bytes, parent);
1168+ num_bytes, parent, root->root_key.objectid);
11741169 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
11751170 key.objectid, key.offset,
11761171 root->root_key.objectid, false);
@@ -1181,15 +1176,15 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 4 unmodified lines
11811176 }
11821177 }
11831178 if (dirty)
1184− btrfs_mark_buffer_dirty(leaf);
1179+ btrfs_mark_buffer_dirty(trans, leaf);
11851180 if (inode)
11861181 btrfs_add_delayed_iput(BTRFS_I(inode));
11871182 return ret;
11881183 }
11891184
1190−static noinline_for_stack
1191−int memcmp_node_keys(struct extent_buffer *eb, int slot,
1192− struct btrfs_path *path, int level)
1185+static noinline_for_stack int memcmp_node_keys(const struct extent_buffer *eb,
1186+ int slot, const struct btrfs_path *path,
1187+ int level)
11931188 {
11941189 struct btrfs_disk_key key1;
11951190 struct btrfs_disk_key key2;
@@ -1374,16 +1369,17 @@ again: · 178 unmodified lines
13741369 */
13751370 btrfs_set_node_blockptr(parent, slot, new_bytenr);
13761371 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1377− btrfs_mark_buffer_dirty(parent);
1372+ btrfs_mark_buffer_dirty(trans, parent);
13781373
13791374 btrfs_set_node_blockptr(path->nodes[level],
13801375 path->slots[level], old_bytenr);
13811376 btrfs_set_node_ptr_generation(path->nodes[level],
13821377 path->slots[level], old_ptr_gen);
1383− btrfs_mark_buffer_dirty(path->nodes[level]);
1378+ btrfs_mark_buffer_dirty(trans, path->nodes[level]);
13841379
13851380 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
1386− blocksize, path->nodes[level]->start);
1381+ blocksize, path->nodes[level]->start,
1382+ src->root_key.objectid);
13871383 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
13881384 0, true);
13891385 ret = btrfs_inc_extent_ref(trans, &ref);
@@ -1392,7 +1388,7 @@ again: · 2 unmodified lines
13921388 break;
13931389 }
13941390 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1395− blocksize, 0);
1391+ blocksize, 0, dest->root_key.objectid);
13961392 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid, 0,
13971393 true);
13981394 ret = btrfs_inc_extent_ref(trans, &ref);
@@ -1401,8 +1397,9 @@ again: · 2 unmodified lines
14011397 break;
14021398 }
14031399
1400+ /* We don't know the real owning_root, use 0. */
14041401 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1405− blocksize, path->nodes[level]->start);
1402+ blocksize, path->nodes[level]->start, 0);
14061403 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
14071404 0, true);
14081405 ret = btrfs_free_extent(trans, &ref);
@@ -1411,8 +1408,9 @@ again: · 2 unmodified lines
14111408 break;
14121409 }
14131410
1411+ /* We don't know the real owning_root, use 0. */
14141412 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1415− blocksize, 0);
1413+ blocksize, 0, 0);
14161414 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid,
14171415 0, true);
14181416 ret = btrfs_free_extent(trans, &ref);
@@ -1518,8 +1516,8 @@ int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, · 99 unmodified lines
15181516 * [min_key, max_key)
15191517 */
15201518 static int invalidate_extent_cache(struct btrfs_root *root,
1521− struct btrfs_key *min_key,
1522− struct btrfs_key *max_key)
1519+ const struct btrfs_key *min_key,
1520+ const struct btrfs_key *max_key)
15231521 {
15241522 struct btrfs_fs_info *fs_info = root->fs_info;
15251523 struct inode *inode = NULL;
@@ -1897,7 +1895,7 @@ again: · 371 unmodified lines
18971895 }
18981896 }
18991897
1900− rc->merge_reloc_tree = 1;
1898+ rc->merge_reloc_tree = true;
19011899
19021900 while (!list_empty(&rc->reloc_roots)) {
19031901 reloc_root = list_entry(rc->reloc_roots.next,
@@ -2517,11 +2515,12 @@ static int do_relocation(struct btrfs_trans_handle *trans, · 613 unmodified lines
25172515 node->eb->start);
25182516 btrfs_set_node_ptr_generation(upper->eb, slot,
25192517 trans->transid);
2520− btrfs_mark_buffer_dirty(upper->eb);
2518+ btrfs_mark_buffer_dirty(trans, upper->eb);
25212519
25222520 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
25232521 node->eb->start, blocksize,
2524− upper->eb->start);
2522+ upper->eb->start,
2523+ btrfs_header_owner(upper->eb));
25252524 btrfs_init_tree_ref(&ref, node->level,
25262525 btrfs_header_owner(upper->eb),
25272526 root->root_key.objectid, false);
@@ -2633,7 +2632,7 @@ static int tree_block_processed(u64 bytenr, struct reloc_control *rc) · 105 unmodified lines
26332632 u32 blocksize = rc->extent_root->fs_info->nodesize;
26342633
26352634 if (test_range_bit(&rc->processed_blocks, bytenr,
2636− bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
2635+ bytenr + blocksize - 1, EXTENT_DIRTY, NULL))
26372636 return 1;
26382637 return 0;
26392638 }
@@ -2660,7 +2659,7 @@ static int get_tree_block_key(struct btrfs_fs_info *fs_info, · 20 unmodified lines
26602659 else
26612660 btrfs_node_key_to_cpu(eb, &block->key, 0);
26622661 free_extent_buffer(eb);
2663− block->key_ready = 1;
2662+ block->key_ready = true;
26642663 return 0;
26652664 }
26662665
@@ -2830,7 +2829,7 @@ out_free_blocks: · 163 unmodified lines
28302829
28312830 static noinline_for_stack int prealloc_file_extent_cluster(
28322831 struct btrfs_inode *inode,
2833− struct file_extent_cluster *cluster)
2832+ const struct file_extent_cluster *cluster)
28342833 {
28352834 u64 alloc_hint = 0;
28362835 u64 start;
@@ -2965,7 +2964,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod · 128 unmodified lines
29652964 /*
29662965 * Allow error injection to test balance/relocation cancellation
29672966 */
2968−noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
2967+noinline int btrfs_should_cancel_balance(const struct btrfs_fs_info *fs_info)
29692968 {
29702969 return atomic_read(&fs_info->balance_cancel_req) ||
29712970 atomic_read(&fs_info->reloc_cancel_req) ||
@@ -2973,7 +2972,7 @@ noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info) · 1 unmodified line
29732972 }
29742973 ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
29752974
2976−static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster,
2975+static u64 get_cluster_boundary_end(const struct file_extent_cluster *cluster,
29772976 int cluster_nr)
29782977 {
29792978 /* Last extent, use cluster end directly */
@@ -2985,7 +2984,7 @@ static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster, · 5 unmodified lines
29852984 }
29862985
29872986 static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
2988− struct file_extent_cluster *cluster,
2987+ const struct file_extent_cluster *cluster,
29892988 int *cluster_nr, unsigned long page_index)
29902989 {
29912990 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
@@ -3120,7 +3119,7 @@ release_page: · 128 unmodified lines
31203119 }
31213120
31223121 static int relocate_file_extent_cluster(struct inode *inode,
3123− struct file_extent_cluster *cluster)
3122+ const struct file_extent_cluster *cluster)
31243123 {
31253124 u64 offset = BTRFS_I(inode)->index_cnt;
31263125 unsigned long index;
@@ -3158,11 +3157,12 @@ out: · 31 unmodified lines
31583157 return ret;
31593158 }
31603159
3161−static noinline_for_stack
3162−int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3163− struct file_extent_cluster *cluster)
3160+static noinline_for_stack int relocate_data_extent(struct inode *inode,
3161+ const struct btrfs_key *extent_key,
3162+ struct file_extent_cluster *cluster)
31643163 {
31653164 int ret;
3165+ struct btrfs_root *root = BTRFS_I(inode)->root;
31663166
31673167 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
31683168 ret = relocate_file_extent_cluster(inode, cluster);
@@ -3171,8 +3171,38 @@ int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key, · 2 unmodified lines
31713171 cluster->nr = 0;
31723172 }
31733173
3174− if (!cluster->nr)
3174+ /*
3175+ * Under simple quotas, we set root->relocation_src_root when we find
3176+ * the extent. If adjacent extents have different owners, we can't merge
3177+ * them while relocating. Handle this by storing the owning root that
3178+ * started a cluster and if we see an extent from a different root break
3179+ * cluster formation (just like the above case of non-adjacent extents).
3180+ *
3181+ * Without simple quotas, relocation_src_root is always 0, so we should
3182+ * never see a mismatch, and it should have no effect on relocation
3183+ * clusters.
3184+ */
3185+ if (cluster->nr > 0 && cluster->owning_root != root->relocation_src_root) {
3186+ u64 tmp = root->relocation_src_root;
3187+
3188+ /*
3189+ * root->relocation_src_root is the state that actually affects
3190+ * the preallocation we do here, so set it to the root owning
3191+ * the cluster we need to relocate.
3192+ */
3193+ root->relocation_src_root = cluster->owning_root;
3194+ ret = relocate_file_extent_cluster(inode, cluster);
3195+ if (ret)
3196+ return ret;
3197+ cluster->nr = 0;
3198+ /* And reset it back for the current extent's owning root. */
3199+ root->relocation_src_root = tmp;
3200+ }
3201+
3202+ if (!cluster->nr) {
31753203 cluster->start = extent_key->objectid;
3204+ cluster->owning_root = root->relocation_src_root;
3205+ }
31763206 else
31773207 BUG_ON(cluster->nr >= MAX_EXTENTS);
31783208 cluster->end = extent_key->objectid + extent_key->offset - 1;
@@ -3193,7 +3223,7 @@ int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key, · 14 unmodified lines
31933223 * the major work is getting the generation and level of the block
31943224 */
31953225 static int add_tree_block(struct reloc_control *rc,
3196− struct btrfs_key *extent_key,
3226+ const struct btrfs_key *extent_key,
31973227 struct btrfs_path *path,
31983228 struct rb_root *blocks)
31993229 {
@@ -3278,7 +3308,7 @@ static int add_tree_block(struct reloc_control *rc, · 78 unmodified lines
32783308 block->key.objectid = rc->extent_root->fs_info->nodesize;
32793309 block->key.offset = generation;
32803310 block->level = level;
3281− block->key_ready = 0;
3311+ block->key_ready = false;
32823312 block->owner = owner;
32833313
32843314 rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
@@ -3444,11 +3474,10 @@ static int delete_v1_space_cache(struct extent_buffer *leaf, · 159 unmodified lines
34443474 /*
34453475 * helper to find all tree blocks that reference a given data extent
34463476 */
3447−static noinline_for_stack
3448−int add_data_references(struct reloc_control *rc,
3449− struct btrfs_key *extent_key,
3450− struct btrfs_path *path,
3451− struct rb_root *blocks)
3477+static noinline_for_stack int add_data_references(struct reloc_control *rc,
3478+ const struct btrfs_key *extent_key,
3479+ struct btrfs_path *path,
3480+ struct rb_root *blocks)
34523481 {
34533482 struct btrfs_backref_walk_ctx ctx = { 0 };
34543483 struct ulist_iterator leaf_uiter;
@@ -3622,7 +3651,7 @@ int prepare_to_relocate(struct reloc_control *rc) · 167 unmodified lines
36223651 if (ret)
36233652 return ret;
36243653
3625− rc->create_reloc_tree = 1;
3654+ rc->create_reloc_tree = true;
36263655 set_reloc_control(rc);
36273656
36283657 trans = btrfs_join_transaction(rc->extent_root);
@@ -3702,6 +3731,21 @@ restart: · 73 unmodified lines
37023731 struct btrfs_extent_item);
37033732 flags = btrfs_extent_flags(path->nodes[0], ei);
37043733
3734+ /*
3735+ * If we are relocating a simple quota owned extent item, we
3736+ * need to note the owner on the reloc data root so that when
3737+ * we allocate the replacement item, we can attribute it to the
3738+ * correct eventual owner (rather than the reloc data root).
3739+ */
3740+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE) {
3741+ struct btrfs_root *root = BTRFS_I(rc->data_inode)->root;
3742+ u64 owning_root_id = btrfs_get_extent_owner_root(fs_info,
3743+ path->nodes[0],
3744+ path->slots[0]);
3745+
3746+ root->relocation_src_root = owning_root_id;
3747+ }
3748+
37053749 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
37063750 ret = add_tree_block(rc, &key, path, &blocks);
37073751 } else if (rc->stage == UPDATE_DATA_PTRS &&
@@ -3734,7 +3778,7 @@ restart: · 26 unmodified lines
37343778
37353779 if (rc->stage == MOVE_DATA_EXTENTS &&
37363780 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3737− rc->found_file_extent = 1;
3781+ rc->found_file_extent = true;
37383782 ret = relocate_data_extent(rc->data_inode,
37393783 &key, &rc->cluster);
37403784 if (ret < 0) {
@@ -3771,7 +3815,7 @@ restart: · 30 unmodified lines
37713815 err = ret;
37723816 }
37733817
3774− rc->create_reloc_tree = 0;
3818+ rc->create_reloc_tree = false;
37753819 set_reloc_control(rc);
37763820
37773821 btrfs_backref_release_cache(&rc->backref_cache);
@@ -3789,7 +3833,7 @@ restart: · 11 unmodified lines
37893833
37903834 merge_reloc_roots(rc);
37913835
3792− rc->merge_reloc_tree = 0;
3836+ rc->merge_reloc_tree = false;
37933837 unset_reloc_control(rc);
37943838 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
37953839
@@ -3835,7 +3879,7 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans, · 39 unmodified lines
38353879 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
38363880 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
38373881 BTRFS_INODE_PREALLOC);
3838− btrfs_mark_buffer_dirty(leaf);
3882+ btrfs_mark_buffer_dirty(trans, leaf);
38393883 out:
38403884 btrfs_free_path(path);
38413885 return ret;
@@ -3874,9 +3918,9 @@ out: · 32 unmodified lines
38743918 * helper to create inode for data relocation.
38753919 * the inode is in data relocation tree and its link count is 0
38763920 */
3877−static noinline_for_stack
3878−struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
3879− struct btrfs_block_group *group)
3921+static noinline_for_stack struct inode *create_reloc_inode(
3922+ struct btrfs_fs_info *fs_info,
3923+ const struct btrfs_block_group *group)
38803924 {
38813925 struct inode *inode = NULL;
38823926 struct btrfs_trans_handle *trans;
@@ -3971,8 +4015,9 @@ static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) · 88 unmodified lines
39714015
39724016 INIT_LIST_HEAD(&rc->reloc_roots);
39734017 INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3974− btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
3975− mapping_tree_init(&rc->reloc_root_tree);
4018+ btrfs_backref_init_cache(fs_info, &rc->backref_cache, true);
4019+ rc->reloc_root_tree.rb_root = RB_ROOT;
4020+ spin_lock_init(&rc->reloc_root_tree.lock);
39764021 extent_io_tree_init(fs_info, &rc->processed_blocks, IO_TREE_RELOC_BLOCKS);
39774022 return rc;
39784023 }
@@ -4004,7 +4049,7 @@ static void describe_relocation(struct btrfs_fs_info *fs_info, · 25 unmodified lines
40044049 block_group->start, buf);
40054050 }
40064051
4007−static const char *stage_to_string(int stage)
4052+static const char *stage_to_string(enum reloc_stage stage)
40084053 {
40094054 if (stage == MOVE_DATA_EXTENTS)
40104055 return "move data extents";
@@ -4120,7 +4165,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) · 109 unmodified lines
41204165 WARN_ON(ret && ret != -EAGAIN);
41214166
41224167 while (1) {
4123− int finishes_stage;
4168+ enum reloc_stage finishes_stage;
41244169
41254170 mutex_lock(&fs_info->cleaner_mutex);
41264171 ret = relocate_block_group(rc);
@@ -4303,7 +4348,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) · 176 unmodified lines
43034348 goto out_unset;
43044349 }
43054350
4306− rc->merge_reloc_tree = 1;
4351+ rc->merge_reloc_tree = true;
43074352
43084353 while (!list_empty(&reloc_roots)) {
43094354 reloc_root = list_entry(reloc_roots.next,
@@ -4422,7 +4467,8 @@ int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered) · 112 unmodified lines
44224467 }
44234468
44244469 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4425− struct btrfs_root *root, struct extent_buffer *buf,
4470+ struct btrfs_root *root,
4471+ const struct extent_buffer *buf,
44264472 struct extent_buffer *cow)
44274473 {
44284474 struct btrfs_fs_info *fs_info = root->fs_info;
@@ -4561,7 +4607,7 @@ int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, · 132 unmodified lines
45614607 *
45624608 * Return U64_MAX if no running relocation.
45634609 */
4564−u64 btrfs_get_reloc_bg_bytenr(struct btrfs_fs_info *fs_info)
4610+u64 btrfs_get_reloc_bg_bytenr(const struct btrfs_fs_info *fs_info)
45654611 {
45664612 u64 logical = U64_MAX;
45674613
fs/btrfs/relocation.h+5−4
@@ -10,15 +10,16 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, · 9 unmodified lines
1010 int btrfs_recover_relocation(struct btrfs_fs_info *fs_info);
1111 int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered);
1212 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
13− struct btrfs_root *root, struct extent_buffer *buf,
13+ struct btrfs_root *root,
14+ const struct extent_buffer *buf,
1415 struct extent_buffer *cow);
1516 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
1617 u64 *bytes_to_reserve);
1718 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
1819 struct btrfs_pending_snapshot *pending);
19−int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info);
20+int btrfs_should_cancel_balance(const struct btrfs_fs_info *fs_info);
2021 struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr);
21−int btrfs_should_ignore_reloc_root(struct btrfs_root *root);
22−u64 btrfs_get_reloc_bg_bytenr(struct btrfs_fs_info *fs_info);
22+bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root);
23+u64 btrfs_get_reloc_bg_bytenr(const struct btrfs_fs_info *fs_info);
2324
2425 #endif
fs/btrfs/root-tree.c+7−5
@@ -51,7 +51,8 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot, · 50 unmodified lines
5151 }
5252
5353 /*
54− * btrfs_find_root - lookup the root by the key.
54+ * Lookup the root by the key.
55+ *
5556 * root: the root of the root tree
5657 * search_key: the key to search
5758 * path: the path we search
@@ -191,7 +192,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root · 133 unmodified lines
191192 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
192193
193194 write_extent_buffer(l, item, ptr, sizeof(*item));
194− btrfs_mark_buffer_dirty(path->nodes[0]);
195+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
195196 out:
196197 btrfs_free_path(path);
197198 return ret;
@@ -438,7 +439,7 @@ again: · 240 unmodified lines
438439 btrfs_set_root_ref_name_len(leaf, ref, name->len);
439440 ptr = (unsigned long)(ref + 1);
440441 write_extent_buffer(leaf, name->name, ptr, name->len);
441− btrfs_mark_buffer_dirty(leaf);
442+ btrfs_mark_buffer_dirty(trans, leaf);
442443
443444 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
444445 btrfs_release_path(path);
@@ -485,7 +486,8 @@ void btrfs_update_root_times(struct btrfs_trans_handle *trans, · 40 unmodified lines
485486 }
486487
487488 /*
488− * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
489+ * Reserve space for subvolume operation.
490+ *
489491 * root: the root of the parent directory
490492 * rsv: block reservation
491493 * items: the number of items that we need do reservation
@@ -508,7 +510,7 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, · 16 unmodified lines
508510 struct btrfs_fs_info *fs_info = root->fs_info;
509511 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
510512
511− if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
513+ if (btrfs_qgroup_enabled(fs_info)) {
512514 /* One for parent inode, two for dir entries */
513515 qgroup_num_bytes = 3 * fs_info->nodesize;
514516 ret = btrfs_qgroup_reserve_meta_prealloc(root,
fs/btrfs/root-tree.h+4−4
@@ -3,6 +3,8 @@ · 2 unmodified lines
33 #ifndef BTRFS_ROOT_TREE_H
44 #define BTRFS_ROOT_TREE_H
55
6+struct fscrypt_str;
7+
68 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
79 struct btrfs_block_rsv *rsv,
810 int nitems, bool use_global_rsv);
@@ -18,10 +20,8 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, const struct btrfs_key *key · 9 unmodified lines
1820 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1921 const struct btrfs_key *key,
2022 struct btrfs_root_item *item);
21−int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
22− struct btrfs_root *root,
23− struct btrfs_key *key,
24− struct btrfs_root_item *item);
23+int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
24+ struct btrfs_key *key, struct btrfs_root_item *item);
2525 int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
2626 struct btrfs_path *path, struct btrfs_root_item *root_item,
2727 struct btrfs_key *root_key);
fs/btrfs/scrub.c+74−4
@@ -16,7 +16,6 @@ · 15 unmodified lines
1616 #include "backref.h"
1717 #include "extent_io.h"
1818 #include "dev-replace.h"
19−#include "check-integrity.h"
2019 #include "raid56.h"
2120 #include "block-group.h"
2221 #include "zoned.h"
@@ -24,6 +23,7 @@ · 1 unmodified line
2423 #include "accessors.h"
2524 #include "file-item.h"
2625 #include "scrub.h"
26+#include "raid-stripe-tree.h"
2727
2828 /*
2929 * This is only the first step towards a full-features scrub. It reads all
@@ -897,7 +897,7 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx, · 867 unmodified lines
897897 ASSERT(stripe->mirror_num >= 1);
898898 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
899899 stripe->logical, &mapped_len, &bioc,
900− NULL, NULL, 1);
900+ NULL, NULL);
901901 /*
902902 * If we failed, dev will be NULL, and later detailed reports
903903 * will just be skipped.
@@ -1635,6 +1635,71 @@ static void scrub_reset_stripe(struct scrub_stripe *stripe) · 731 unmodified lines
16351635 }
16361636 }
16371637
1638+static void scrub_submit_extent_sector_read(struct scrub_ctx *sctx,
1639+ struct scrub_stripe *stripe)
1640+{
1641+ struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1642+ struct btrfs_bio *bbio = NULL;
1643+ u64 stripe_len = BTRFS_STRIPE_LEN;
1644+ int mirror = stripe->mirror_num;
1645+ int i;
1646+
1647+ atomic_inc(&stripe->pending_io);
1648+
1649+ for_each_set_bit(i, &stripe->extent_sector_bitmap, stripe->nr_sectors) {
1650+ struct page *page = scrub_stripe_get_page(stripe, i);
1651+ unsigned int pgoff = scrub_stripe_get_page_offset(stripe, i);
1652+
1653+ /* The current sector cannot be merged, submit the bio. */
1654+ if (bbio &&
1655+ ((i > 0 &&
1656+ !test_bit(i - 1, &stripe->extent_sector_bitmap)) ||
1657+ bbio->bio.bi_iter.bi_size >= stripe_len)) {
1658+ ASSERT(bbio->bio.bi_iter.bi_size);
1659+ atomic_inc(&stripe->pending_io);
1660+ btrfs_submit_bio(bbio, mirror);
1661+ bbio = NULL;
1662+ }
1663+
1664+ if (!bbio) {
1665+ struct btrfs_io_stripe io_stripe = {};
1666+ struct btrfs_io_context *bioc = NULL;
1667+ const u64 logical = stripe->logical +
1668+ (i << fs_info->sectorsize_bits);
1669+ int err;
1670+
1671+ bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_READ,
1672+ fs_info, scrub_read_endio, stripe);
1673+ bbio->bio.bi_iter.bi_sector = logical >> SECTOR_SHIFT;
1674+
1675+ io_stripe.is_scrub = true;
1676+ err = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
1677+ &stripe_len, &bioc, &io_stripe,
1678+ &mirror);
1679+ btrfs_put_bioc(bioc);
1680+ if (err) {
1681+ btrfs_bio_end_io(bbio,
1682+ errno_to_blk_status(err));
1683+ return;
1684+ }
1685+ }
1686+
1687+ __bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
1688+ }
1689+
1690+ if (bbio) {
1691+ ASSERT(bbio->bio.bi_iter.bi_size);
1692+ atomic_inc(&stripe->pending_io);
1693+ btrfs_submit_bio(bbio, mirror);
1694+ }
1695+
1696+ if (atomic_dec_and_test(&stripe->pending_io)) {
1697+ wake_up(&stripe->io_wait);
1698+ INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker);
1699+ queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work);
1700+ }
1701+}
1702+
16381703 static void scrub_submit_initial_read(struct scrub_ctx *sctx,
16391704 struct scrub_stripe *stripe)
16401705 {
@@ -1646,6 +1711,11 @@ static void scrub_submit_initial_read(struct scrub_ctx *sctx, · 5 unmodified lines
16461711 ASSERT(stripe->mirror_num > 0);
16471712 ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
16481713
1714+ if (btrfs_need_stripe_tree_update(fs_info, stripe->bg->flags)) {
1715+ scrub_submit_extent_sector_read(sctx, stripe);
1716+ return;
1717+ }
1718+
16491719 bbio = btrfs_bio_alloc(SCRUB_STRIPE_PAGES, REQ_OP_READ, fs_info,
16501720 scrub_read_endio, stripe);
16511721
@@ -1952,7 +2022,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx, · 300 unmodified lines
19522022
19532023 btrfs_bio_counter_inc_blocked(fs_info);
19542024 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
1955− &length, &bioc, NULL, NULL, 1);
2025+ &length, &bioc, NULL, NULL);
19562026 if (ret < 0) {
19572027 btrfs_put_bioc(bioc);
19582028 btrfs_bio_counter_dec(fs_info);
@@ -2717,7 +2787,7 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx, · 758 unmodified lines
27172787 if (scrub_dev->fs_devices != fs_info->fs_devices)
27182788 gen = scrub_dev->generation;
27192789 else
2720− gen = fs_info->last_trans_committed;
2790+ gen = btrfs_get_last_trans_committed(fs_info);
27212791
27222792 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
27232793 bytenr = btrfs_sb_offset(i);
fs/btrfs/send.c+3−3
@@ -796,7 +796,7 @@ static int send_cmd(struct send_ctx *sctx) · 795 unmodified lines
796796 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
797797 put_unaligned_le32(0, &hdr->crc);
798798
799− crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
799+ crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
800800 put_unaligned_le32(crc, &hdr->crc);
801801
802802 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
@@ -5669,8 +5669,8 @@ static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path, · 4866 unmodified lines
56695669 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
56705670 hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));
56715671 hdr->crc = 0;
5672− crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size);
5673− crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
5672+ crc = crc32c(0, sctx->send_buf, sctx->send_size);
5673+ crc = crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
56745674 hdr->crc = cpu_to_le32(crc);
56755675
56765676 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
fs/btrfs/space-info.c+41−23
@@ -345,8 +345,10 @@ static u64 calc_available_free_space(struct btrfs_fs_info *fs_info, · 344 unmodified lines
345345 struct btrfs_space_info *space_info,
346346 enum btrfs_reserve_flush_enum flush)
347347 {
348+ struct btrfs_space_info *data_sinfo;
348349 u64 profile;
349350 u64 avail;
351+ u64 data_chunk_size;
350352 int factor;
351353
352354 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
@@ -364,6 +366,36 @@ static u64 calc_available_free_space(struct btrfs_fs_info *fs_info, · 11 unmodified lines
364366 */
365367 factor = btrfs_bg_type_to_factor(profile);
366368 avail = div_u64(avail, factor);
369+ if (avail == 0)
370+ return 0;
371+
372+ /*
373+ * Calculate the data_chunk_size, space_info->chunk_size is the
374+ * "optimal" chunk size based on the fs size. However when we actually
375+ * allocate the chunk we will strip this down further, making it no more
376+ * than 10% of the disk or 1G, whichever is smaller.
377+ */
378+ data_sinfo = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
379+ data_chunk_size = min(data_sinfo->chunk_size,
380+ mult_perc(fs_info->fs_devices->total_rw_bytes, 10));
381+ data_chunk_size = min_t(u64, data_chunk_size, SZ_1G);
382+
383+ /*
384+ * Since data allocations immediately use block groups as part of the
385+ * reservation, because we assume that data reservations will == actual
386+ * usage, we could potentially overcommit and then immediately have that
387+ * available space used by a data allocation, which could put us in a
388+ * bind when we get close to filling the file system.
389+ *
390+ * To handle this simply remove the data_chunk_size from the available
391+ * space. If we are relatively empty this won't affect our ability to
392+ * overcommit much, and if we're very close to full it'll keep us from
393+ * getting into a position where we've given ourselves very little
394+ * metadata wiggle room.
395+ */
396+ if (avail <= data_chunk_size)
397+ return 0;
398+ avail -= data_chunk_size;
367399
368400 /*
369401 * If we aren't flushing all things, let us overcommit up to
@@ -556,18 +588,6 @@ static inline u64 calc_reclaim_items_nr(const struct btrfs_fs_info *fs_info, · 186 unmodified lines
556588 return nr;
557589 }
558590
559−static inline u64 calc_delayed_refs_nr(const struct btrfs_fs_info *fs_info,
560− u64 to_reclaim)
561−{
562− const u64 bytes = btrfs_calc_delayed_ref_bytes(fs_info, 1);
563− u64 nr;
564−
565− nr = div64_u64(to_reclaim, bytes);
566− if (!nr)
567− nr = 1;
568− return nr;
569−}
570−
571591 #define EXTENT_SIZE_PER_ITEM SZ_256K
572592
573593 /*
@@ -749,10 +769,9 @@ static void flush_space(struct btrfs_fs_info *fs_info, · 175 unmodified lines
749769 break;
750770 }
751771 if (state == FLUSH_DELAYED_REFS_NR)
752− nr = calc_delayed_refs_nr(fs_info, num_bytes);
772+ btrfs_run_delayed_refs(trans, num_bytes);
753773 else
754− nr = 0;
755− btrfs_run_delayed_refs(trans, nr);
774+ btrfs_run_delayed_refs(trans, 0);
756775 btrfs_end_transaction(trans);
757776 break;
758777 case ALLOC_CHUNK:
@@ -978,7 +997,8 @@ static bool steal_from_global_rsv(struct btrfs_fs_info *fs_info, · 219 unmodified lines
978997 }
979998
980999 /*
981− * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets
1000+ * We've exhausted our flushing, start failing tickets.
1001+ *
9821002 * @fs_info - fs_info for this fs
9831003 * @space_info - the space info we were flushing
9841004 *
@@ -1742,7 +1762,7 @@ static int __reserve_bytes(struct btrfs_fs_info *fs_info, · 757 unmodified lines
17421762 * Try to reserve metadata bytes from the block_rsv's space.
17431763 *
17441764 * @fs_info: the filesystem
1745− * @block_rsv: block_rsv we're allocating for
1765+ * @space_info: the space_info we're allocating for
17461766 * @orig_bytes: number of bytes we want
17471767 * @flush: whether or not we can flush to make our reservation
17481768 *
@@ -1754,21 +1774,19 @@ static int __reserve_bytes(struct btrfs_fs_info *fs_info, · 5 unmodified lines
17541774 * space already.
17551775 */
17561776 int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
1757− struct btrfs_block_rsv *block_rsv,
1777+ struct btrfs_space_info *space_info,
17581778 u64 orig_bytes,
17591779 enum btrfs_reserve_flush_enum flush)
17601780 {
17611781 int ret;
17621782
1763− ret = __reserve_bytes(fs_info, block_rsv->space_info, orig_bytes, flush);
1783+ ret = __reserve_bytes(fs_info, space_info, orig_bytes, flush);
17641784 if (ret == -ENOSPC) {
17651785 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
1766− block_rsv->space_info->flags,
1767− orig_bytes, 1);
1786+ space_info->flags, orig_bytes, 1);
17681787
17691788 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
1770− btrfs_dump_space_info(fs_info, block_rsv->space_info,
1771− orig_bytes, 0);
1789+ btrfs_dump_space_info(fs_info, space_info, orig_bytes, 0);
17721790 }
17731791 return ret;
17741792 }
fs/btrfs/space-info.h+2−1
@@ -3,6 +3,7 @@ · 2 unmodified lines
33 #ifndef BTRFS_SPACE_INFO_H
44 #define BTRFS_SPACE_INFO_H
55
6+#include <trace/events/btrfs.h>
67 #include "volumes.h"
78
89 /*
@@ -212,7 +213,7 @@ void btrfs_dump_space_info(struct btrfs_fs_info *fs_info, · 203 unmodified lines
212213 struct btrfs_space_info *info, u64 bytes,
213214 int dump_block_groups);
214215 int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
215− struct btrfs_block_rsv *block_rsv,
216+ struct btrfs_space_info *space_info,
216217 u64 orig_bytes,
217218 enum btrfs_reserve_flush_enum flush);
218219 void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
fs/btrfs/super.c+23−64
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #include <linux/ratelimit.h>
2727 #include <linux/crc32c.h>
2828 #include <linux/btrfs.h>
29+#include <linux/security.h>
2930 #include "messages.h"
3031 #include "delayed-inode.h"
3132 #include "ctree.h"
@@ -129,9 +130,6 @@ enum { · 97 unmodified lines
129130 Opt_inode_cache, Opt_noinode_cache,
130131
131132 /* Debugging options */
132− Opt_check_integrity,
133− Opt_check_integrity_including_extent_data,
134− Opt_check_integrity_print_mask,
135133 Opt_enospc_debug, Opt_noenospc_debug,
136134 #ifdef CONFIG_BTRFS_DEBUG
137135 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
@@ -200,9 +198,6 @@ static const match_table_t tokens = { · 62 unmodified lines
200198 {Opt_recovery, "recovery"},
201199
202200 /* Debugging options */
203− {Opt_check_integrity, "check_int"},
204− {Opt_check_integrity_including_extent_data, "check_int_data"},
205− {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
206201 {Opt_enospc_debug, "enospc_debug"},
207202 {Opt_noenospc_debug, "noenospc_debug"},
208203 #ifdef CONFIG_BTRFS_DEBUG
@@ -707,44 +702,6 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, · 498 unmodified lines
707702 case Opt_skip_balance:
708703 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
709704 break;
710−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
711− case Opt_check_integrity_including_extent_data:
712− btrfs_warn(info,
713− "integrity checker is deprecated and will be removed in 6.7");
714− btrfs_info(info,
715− "enabling check integrity including extent data");
716− btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
717− btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
718− break;
719− case Opt_check_integrity:
720− btrfs_warn(info,
721− "integrity checker is deprecated and will be removed in 6.7");
722− btrfs_info(info, "enabling check integrity");
723− btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
724− break;
725− case Opt_check_integrity_print_mask:
726− ret = match_int(&args[0], &intarg);
727− if (ret) {
728− btrfs_err(info,
729− "unrecognized check_integrity_print_mask value %s",
730− args[0].from);
731− goto out;
732− }
733− info->check_integrity_print_mask = intarg;
734− btrfs_warn(info,
735− "integrity checker is deprecated and will be removed in 6.7");
736− btrfs_info(info, "check_integrity_print_mask 0x%x",
737− info->check_integrity_print_mask);
738− break;
739−#else
740− case Opt_check_integrity_including_extent_data:
741− case Opt_check_integrity:
742− case Opt_check_integrity_print_mask:
743− btrfs_err(info,
744− "support for check_integrity* not compiled in!");
745− ret = -EINVAL;
746− goto out;
747−#endif
748705 case Opt_fatal_errors:
749706 if (strcmp(args[0].from, "panic") == 0) {
750707 btrfs_set_opt(info->mount_opt,
@@ -889,7 +846,7 @@ static int btrfs_parse_device_options(const char *options, blk_mode_t flags) · 138 unmodified lines
889846 error = -ENOMEM;
890847 goto out;
891848 }
892− device = btrfs_scan_one_device(device_name, flags);
849+ device = btrfs_scan_one_device(device_name, flags, false);
893850 kfree(device_name);
894851 if (IS_ERR(device)) {
895852 error = PTR_ERR(device);
@@ -1305,15 +1262,6 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) · 409 unmodified lines
13051262 seq_puts(seq, ",autodefrag");
13061263 if (btrfs_test_opt(info, SKIP_BALANCE))
13071264 seq_puts(seq, ",skip_balance");
1308−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1309− if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
1310− seq_puts(seq, ",check_int_data");
1311− else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1312− seq_puts(seq, ",check_int");
1313− if (info->check_integrity_print_mask)
1314− seq_printf(seq, ",check_int_print_mask=%d",
1315− info->check_integrity_print_mask);
1316−#endif
13171265 if (info->metadata_ratio)
13181266 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
13191267 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
@@ -1484,7 +1432,12 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, · 164 unmodified lines
14841432 goto error_fs_info;
14851433 }
14861434
1487− device = btrfs_scan_one_device(device_name, mode);
1435+ /*
1436+ * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
1437+ * either a valid device or an error.
1438+ */
1439+ device = btrfs_scan_one_device(device_name, mode, true);
1440+ ASSERT(device != NULL);
14881441 if (IS_ERR(device)) {
14891442 mutex_unlock(&uuid_mutex);
14901443 error = PTR_ERR(device);
@@ -2196,7 +2149,11 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd, · 705 unmodified lines
21962149 switch (cmd) {
21972150 case BTRFS_IOC_SCAN_DEV:
21982151 mutex_lock(&uuid_mutex);
2199− device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
2152+ /*
2153+ * Scanning outside of mount can return NULL which would turn
2154+ * into 0 error code.
2155+ */
2156+ device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
22002157 ret = PTR_ERR_OR_ZERO(device);
22012158 mutex_unlock(&uuid_mutex);
22022159 break;
@@ -2210,8 +2167,12 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd, · 7 unmodified lines
22102167 break;
22112168 case BTRFS_IOC_DEVICES_READY:
22122169 mutex_lock(&uuid_mutex);
2213− device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
2214− if (IS_ERR(device)) {
2170+ /*
2171+ * Scanning outside of mount can return NULL which would turn
2172+ * into 0 error code.
2173+ */
2174+ device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
2175+ if (IS_ERR_OR_NULL(device)) {
22152176 mutex_unlock(&uuid_mutex);
22162177 ret = PTR_ERR(device);
22172178 break;
@@ -2256,6 +2217,7 @@ static int check_dev_super(struct btrfs_device *dev) · 38 unmodified lines
22562217 {
22572218 struct btrfs_fs_info *fs_info = dev->fs_info;
22582219 struct btrfs_super_block *sb;
2220+ u64 last_trans;
22592221 u16 csum_type;
22602222 int ret = 0;
22612223
@@ -2291,10 +2253,10 @@ static int check_dev_super(struct btrfs_device *dev) · 29 unmodified lines
22912253 if (ret < 0)
22922254 goto out;
22932255
2294− if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2256+ last_trans = btrfs_get_last_trans_committed(fs_info);
2257+ if (btrfs_super_generation(sb) != last_trans) {
22952258 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2296− btrfs_super_generation(sb),
2297− fs_info->last_trans_committed);
2259+ btrfs_super_generation(sb), last_trans);
22982260 ret = -EUCLEAN;
22992261 goto out;
23002262 }
@@ -2404,9 +2366,6 @@ static int __init btrfs_print_mod_info(void) · 103 unmodified lines
24042366 #ifdef CONFIG_BTRFS_ASSERT
24052367 ", assert=on"
24062368 #endif
2407−#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2408− ", integrity-checker=on"
2409−#endif
24102369 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
24112370 ", ref-verify=on"
24122371 #endif
fs/btrfs/sysfs.c+52−1
@@ -291,12 +291,15 @@ BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); · 290 unmodified lines
291291 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
292292 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
293293 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
294+BTRFS_FEAT_ATTR_INCOMPAT(simple_quota, SIMPLE_QUOTA);
294295 #ifdef CONFIG_BLK_DEV_ZONED
295296 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
296297 #endif
297298 #ifdef CONFIG_BTRFS_DEBUG
298299 /* Remove once support for extent tree v2 is feature complete */
299300 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
301+/* Remove once support for raid stripe tree is feature complete. */
302+BTRFS_FEAT_ATTR_INCOMPAT(raid_stripe_tree, RAID_STRIPE_TREE);
300303 #endif
301304 #ifdef CONFIG_FS_VERITY
302305 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
@@ -322,11 +325,13 @@ static struct attribute *btrfs_supported_feature_attrs[] = { · 19 unmodified lines
322325 BTRFS_FEAT_ATTR_PTR(free_space_tree),
323326 BTRFS_FEAT_ATTR_PTR(raid1c34),
324327 BTRFS_FEAT_ATTR_PTR(block_group_tree),
328+ BTRFS_FEAT_ATTR_PTR(simple_quota),
325329 #ifdef CONFIG_BLK_DEV_ZONED
326330 BTRFS_FEAT_ATTR_PTR(zoned),
327331 #endif
328332 #ifdef CONFIG_BTRFS_DEBUG
329333 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
334+ BTRFS_FEAT_ATTR_PTR(raid_stripe_tree),
330335 #endif
331336 #ifdef CONFIG_FS_VERITY
332337 BTRFS_FEAT_ATTR_PTR(verity),
@@ -420,6 +425,13 @@ static ssize_t acl_show(struct kobject *kobj, struct kobj_attribute *a, char *bu · 87 unmodified lines
420425 }
421426 BTRFS_ATTR(static_feature, acl, acl_show);
422427
428+static ssize_t temp_fsid_supported_show(struct kobject *kobj,
429+ struct kobj_attribute *a, char *buf)
430+{
431+ return sysfs_emit(buf, "0\n");
432+}
433+BTRFS_ATTR(static_feature, temp_fsid, temp_fsid_supported_show);
434+
423435 /*
424436 * Features which only depend on kernel version.
425437 *
@@ -433,6 +445,7 @@ static struct attribute *btrfs_supported_static_feature_attrs[] = { · 7 unmodified lines
433445 BTRFS_ATTR_PTR(static_feature, send_stream_version),
434446 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
435447 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
448+ BTRFS_ATTR_PTR(static_feature, temp_fsid),
436449 NULL
437450 };
438451
@@ -1196,10 +1209,19 @@ static ssize_t btrfs_generation_show(struct kobject *kobj, · 757 unmodified lines
11961209 {
11971210 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
11981211
1199− return sysfs_emit(buf, "%llu\n", fs_info->generation);
1212+ return sysfs_emit(buf, "%llu\n", btrfs_get_fs_generation(fs_info));
12001213 }
12011214 BTRFS_ATTR(, generation, btrfs_generation_show);
12021215
1216+static ssize_t btrfs_temp_fsid_show(struct kobject *kobj,
1217+ struct kobj_attribute *a, char *buf)
1218+{
1219+ struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1220+
1221+ return sysfs_emit(buf, "%d\n", fs_info->fs_devices->temp_fsid);
1222+}
1223+BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show);
1224+
12031225 static const char * const btrfs_read_policy_name[] = { "pid" };
12041226
12051227 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
@@ -1302,6 +1324,7 @@ static const struct attribute *btrfs_attrs[] = { · 96 unmodified lines
13021324 BTRFS_ATTR_PTR(, read_policy),
13031325 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
13041326 BTRFS_ATTR_PTR(, commit_stats),
1327+ BTRFS_ATTR_PTR(, temp_fsid),
13051328 NULL,
13061329 };
13071330
@@ -2086,6 +2109,33 @@ static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj, · 778 unmodified lines
20862109 }
20872110 BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
20882111
2112+static ssize_t qgroup_mode_show(struct kobject *qgroups_kobj,
2113+ struct kobj_attribute *a,
2114+ char *buf)
2115+{
2116+ struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2117+ ssize_t ret = 0;
2118+
2119+ spin_lock(&fs_info->qgroup_lock);
2120+ ASSERT(btrfs_qgroup_enabled(fs_info));
2121+ switch (btrfs_qgroup_mode(fs_info)) {
2122+ case BTRFS_QGROUP_MODE_FULL:
2123+ ret = sysfs_emit(buf, "qgroup\n");
2124+ break;
2125+ case BTRFS_QGROUP_MODE_SIMPLE:
2126+ ret = sysfs_emit(buf, "squota\n");
2127+ break;
2128+ default:
2129+ btrfs_warn(fs_info, "unexpected qgroup mode %d\n",
2130+ btrfs_qgroup_mode(fs_info));
2131+ break;
2132+ }
2133+ spin_unlock(&fs_info->qgroup_lock);
2134+
2135+ return ret;
2136+}
2137+BTRFS_ATTR(qgroups, mode, qgroup_mode_show);
2138+
20892139 static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
20902140 struct kobj_attribute *a,
20912141 char *buf)
@@ -2148,6 +2198,7 @@ static struct attribute *qgroups_attrs[] = { · 56 unmodified lines
21482198 BTRFS_ATTR_PTR(qgroups, enabled),
21492199 BTRFS_ATTR_PTR(qgroups, inconsistent),
21502200 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2201+ BTRFS_ATTR_PTR(qgroups, mode),
21512202 NULL
21522203 };
21532204 ATTRIBUTE_GROUPS(qgroups);
fs/btrfs/tests/extent-buffer-tests.c+5−1
@@ -61,7 +61,11 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) · 60 unmodified lines
6161 key.type = BTRFS_EXTENT_CSUM_KEY;
6262 key.offset = 0;
6363
64− btrfs_setup_item_for_insert(root, path, &key, value_len);
64+ /*
65+ * Passing a NULL trans handle is fine here, we have a dummy root eb
66+ * and the tree is a single node (level 0).
67+ */
68+ btrfs_setup_item_for_insert(NULL, root, path, &key, value_len);
6569 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
6670 value_len);
6771
fs/btrfs/tests/inode-tests.c+10−2
@@ -34,7 +34,11 @@ static void insert_extent(struct btrfs_root *root, u64 start, u64 len, · 33 unmodified lines
3434 key.type = BTRFS_EXTENT_DATA_KEY;
3535 key.offset = start;
3636
37− btrfs_setup_item_for_insert(root, &path, &key, value_len);
37+ /*
38+ * Passing a NULL trans handle is fine here, we have a dummy root eb
39+ * and the tree is a single node (level 0).
40+ */
41+ btrfs_setup_item_for_insert(NULL, root, &path, &key, value_len);
3842 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3943 btrfs_set_file_extent_generation(leaf, fi, 1);
4044 btrfs_set_file_extent_type(leaf, fi, type);
@@ -64,7 +68,11 @@ static void insert_inode_item_key(struct btrfs_root *root) · 23 unmodified lines
6468 key.type = BTRFS_INODE_ITEM_KEY;
6569 key.offset = 0;
6670
67− btrfs_setup_item_for_insert(root, &path, &key, value_len);
71+ /*
72+ * Passing a NULL trans handle is fine here, we have a dummy root eb
73+ * and the tree is a single node (level 0).
74+ */
75+ btrfs_setup_item_for_insert(NULL, root, &path, &key, value_len);
6876 }
6977
7078 /*
fs/btrfs/transaction.c+140−89
@@ -386,7 +386,7 @@ loop: · 385 unmodified lines
386386 IO_TREE_TRANS_DIRTY_PAGES);
387387 extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
388388 IO_TREE_FS_PINNED_EXTENTS);
389− fs_info->generation++;
389+ btrfs_set_fs_generation(fs_info, fs_info->generation + 1);
390390 cur_trans->transid = fs_info->generation;
391391 fs_info->running_transaction = cur_trans;
392392 cur_trans->aborted = 0;
@@ -561,6 +561,69 @@ static inline bool need_reserve_reloc_root(struct btrfs_root *root) · 168 unmodified lines
561561 return true;
562562 }
563563
564+static int btrfs_reserve_trans_metadata(struct btrfs_fs_info *fs_info,
565+ enum btrfs_reserve_flush_enum flush,
566+ u64 num_bytes,
567+ u64 *delayed_refs_bytes)
568+{
569+ struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
570+ struct btrfs_space_info *si = fs_info->trans_block_rsv.space_info;
571+ u64 extra_delayed_refs_bytes = 0;
572+ u64 bytes;
573+ int ret;
574+
575+ /*
576+ * If there's a gap between the size of the delayed refs reserve and
577+ * its reserved space, than some tasks have added delayed refs or bumped
578+ * its size otherwise (due to block group creation or removal, or block
579+ * group item update). Also try to allocate that gap in order to prevent
580+ * using (and possibly abusing) the global reserve when committing the
581+ * transaction.
582+ */
583+ if (flush == BTRFS_RESERVE_FLUSH_ALL &&
584+ !btrfs_block_rsv_full(delayed_refs_rsv)) {
585+ spin_lock(&delayed_refs_rsv->lock);
586+ if (delayed_refs_rsv->size > delayed_refs_rsv->reserved)
587+ extra_delayed_refs_bytes = delayed_refs_rsv->size -
588+ delayed_refs_rsv->reserved;
589+ spin_unlock(&delayed_refs_rsv->lock);
590+ }
591+
592+ bytes = num_bytes + *delayed_refs_bytes + extra_delayed_refs_bytes;
593+
594+ /*
595+ * We want to reserve all the bytes we may need all at once, so we only
596+ * do 1 enospc flushing cycle per transaction start.
597+ */
598+ ret = btrfs_reserve_metadata_bytes(fs_info, si, bytes, flush);
599+ if (ret == 0) {
600+ if (extra_delayed_refs_bytes > 0)
601+ btrfs_migrate_to_delayed_refs_rsv(fs_info,
602+ extra_delayed_refs_bytes);
603+ return 0;
604+ }
605+
606+ if (extra_delayed_refs_bytes > 0) {
607+ bytes -= extra_delayed_refs_bytes;
608+ ret = btrfs_reserve_metadata_bytes(fs_info, si, bytes, flush);
609+ if (ret == 0)
610+ return 0;
611+ }
612+
613+ /*
614+ * If we are an emergency flush, which can steal from the global block
615+ * reserve, then attempt to not reserve space for the delayed refs, as
616+ * we will consume space for them from the global block reserve.
617+ */
618+ if (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL) {
619+ bytes -= *delayed_refs_bytes;
620+ *delayed_refs_bytes = 0;
621+ ret = btrfs_reserve_metadata_bytes(fs_info, si, bytes, flush);
622+ }
623+
624+ return ret;
625+}
626+
564627 static struct btrfs_trans_handle *
565628 start_transaction(struct btrfs_root *root, unsigned int num_items,
566629 unsigned int type, enum btrfs_reserve_flush_enum flush,
@@ -568,10 +631,12 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 1 unmodified line
568631 {
569632 struct btrfs_fs_info *fs_info = root->fs_info;
570633 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
634+ struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;
571635 struct btrfs_trans_handle *h;
572636 struct btrfs_transaction *cur_trans;
573637 u64 num_bytes = 0;
574638 u64 qgroup_reserved = 0;
639+ u64 delayed_refs_bytes = 0;
575640 bool reloc_reserved = false;
576641 bool do_chunk_alloc = false;
577642 int ret;
@@ -594,9 +659,6 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 16 unmodified lines
594659 * the appropriate flushing if need be.
595660 */
596661 if (num_items && root != fs_info->chunk_root) {
597− struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
598− u64 delayed_refs_bytes = 0;
599−
600662 qgroup_reserved = num_items * fs_info->nodesize;
601663 /*
602664 * Use prealloc for now, as there might be a currently running
@@ -608,20 +670,16 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 5 unmodified lines
608670 if (ret)
609671 return ERR_PTR(ret);
610672
673+ num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
611674 /*
612− * We want to reserve all the bytes we may need all at once, so
613− * we only do 1 enospc flushing cycle per transaction start. We
614− * accomplish this by simply assuming we'll do num_items worth
615− * of delayed refs updates in this trans handle, and refill that
616− * amount for whatever is missing in the reserve.
675+ * If we plan to insert/update/delete "num_items" from a btree,
676+ * we will also generate delayed refs for extent buffers in the
677+ * respective btree paths, so reserve space for the delayed refs
678+ * that will be generated by the caller as it modifies btrees.
679+ * Try to reserve them to avoid excessive use of the global
680+ * block reserve.
617681 */
618− num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
619− if (flush == BTRFS_RESERVE_FLUSH_ALL &&
620− !btrfs_block_rsv_full(delayed_refs_rsv)) {
621− delayed_refs_bytes = btrfs_calc_delayed_ref_bytes(fs_info,
622− num_items);
623− num_bytes += delayed_refs_bytes;
624− }
682+ delayed_refs_bytes = btrfs_calc_delayed_ref_bytes(fs_info, num_items);
625683
626684 /*
627685 * Do the reservation for the relocation root creation
@@ -631,16 +689,14 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 3 unmodified lines
631689 reloc_reserved = true;
632690 }
633691
634− ret = btrfs_reserve_metadata_bytes(fs_info, rsv, num_bytes, flush);
692+ ret = btrfs_reserve_trans_metadata(fs_info, flush, num_bytes,
693+ &delayed_refs_bytes);
635694 if (ret)
636695 goto reserve_fail;
637− if (delayed_refs_bytes) {
638− btrfs_migrate_to_delayed_refs_rsv(fs_info, delayed_refs_bytes);
639− num_bytes -= delayed_refs_bytes;
640− }
641− btrfs_block_rsv_add_bytes(rsv, num_bytes, true);
642696
643− if (rsv->space_info->force_alloc)
697+ btrfs_block_rsv_add_bytes(trans_rsv, num_bytes, true);
698+
699+ if (trans_rsv->space_info->force_alloc)
644700 do_chunk_alloc = true;
645701 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
646702 !btrfs_block_rsv_full(delayed_refs_rsv)) {
@@ -700,6 +756,7 @@ again: · 53 unmodified lines
700756
701757 h->type = type;
702758 INIT_LIST_HEAD(&h->new_bgs);
759+ btrfs_init_metadata_block_rsv(fs_info, &h->delayed_rsv, BTRFS_BLOCK_RSV_DELOPS);
703760
704761 smp_mb();
705762 if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
@@ -712,8 +769,17 @@ again: · 6 unmodified lines
712769 if (num_bytes) {
713770 trace_btrfs_space_reservation(fs_info, "transaction",
714771 h->transid, num_bytes, 1);
715− h->block_rsv = &fs_info->trans_block_rsv;
772+ h->block_rsv = trans_rsv;
716773 h->bytes_reserved = num_bytes;
774+ if (delayed_refs_bytes > 0) {
775+ trace_btrfs_space_reservation(fs_info,
776+ "local_delayed_refs_rsv",
777+ h->transid,
778+ delayed_refs_bytes, 1);
779+ h->delayed_refs_bytes_reserved = delayed_refs_bytes;
780+ btrfs_block_rsv_add_bytes(&h->delayed_rsv, delayed_refs_bytes, true);
781+ delayed_refs_bytes = 0;
782+ }
717783 h->reloc_reserved = reloc_reserved;
718784 }
719785
@@ -769,8 +835,10 @@ join_fail: · 49 unmodified lines
769835 kmem_cache_free(btrfs_trans_handle_cachep, h);
770836 alloc_fail:
771837 if (num_bytes)
772− btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
773− num_bytes, NULL);
838+ btrfs_block_rsv_release(fs_info, trans_rsv, num_bytes, NULL);
839+ if (delayed_refs_bytes)
840+ btrfs_space_info_free_bytes_may_use(fs_info, trans_rsv->space_info,
841+ delayed_refs_bytes);
774842 reserve_fail:
775843 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
776844 return ERR_PTR(ret);
@@ -817,7 +885,7 @@ struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *roo · 40 unmodified lines
817885 }
818886
819887 /*
820− * btrfs_attach_transaction() - catch the running transaction
888+ * Catch the running transaction.
821889 *
822890 * It is used when we want to commit the current the transaction, but
823891 * don't want to start a new one.
@@ -836,7 +904,7 @@ struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) · 12 unmodified lines
836904 }
837905
838906 /*
839− * btrfs_attach_transaction_barrier() - catch the running transaction
907+ * Catch the running transaction.
840908 *
841909 * It is similar to the above function, the difference is this one
842910 * will wait for all the inactive transactions until they fully
@@ -912,7 +980,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) · 69 unmodified lines
912980 int ret = 0;
913981
914982 if (transid) {
915− if (transid <= fs_info->last_trans_committed)
983+ if (transid <= btrfs_get_last_trans_committed(fs_info))
916984 goto out;
917985
918986 /* find specified transaction */
@@ -936,7 +1004,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) · 17 unmodified lines
9361004 * raced with btrfs_commit_transaction
9371005 */
9381006 if (!cur_trans) {
939− if (transid > fs_info->last_trans_committed)
1007+ if (transid > btrfs_get_last_trans_committed(fs_info))
9401008 ret = -EINVAL;
9411009 goto out;
9421010 }
@@ -991,11 +1059,14 @@ static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) · 48 unmodified lines
9911059
9921060 if (!trans->block_rsv) {
9931061 ASSERT(!trans->bytes_reserved);
1062+ ASSERT(!trans->delayed_refs_bytes_reserved);
9941063 return;
9951064 }
9961065
997− if (!trans->bytes_reserved)
1066+ if (!trans->bytes_reserved) {
1067+ ASSERT(!trans->delayed_refs_bytes_reserved);
9981068 return;
1069+ }
9991070
10001071 ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
10011072 trace_btrfs_space_reservation(fs_info, "transaction",
@@ -1003,6 +1074,16 @@ static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) · 1 unmodified line
10031074 btrfs_block_rsv_release(fs_info, trans->block_rsv,
10041075 trans->bytes_reserved, NULL);
10051076 trans->bytes_reserved = 0;
1077+
1078+ if (!trans->delayed_refs_bytes_reserved)
1079+ return;
1080+
1081+ trace_btrfs_space_reservation(fs_info, "local_delayed_refs_rsv",
1082+ trans->transid,
1083+ trans->delayed_refs_bytes_reserved, 0);
1084+ btrfs_block_rsv_release(fs_info, &trans->delayed_rsv,
1085+ trans->delayed_refs_bytes_reserved, NULL);
1086+ trans->delayed_refs_bytes_reserved = 0;
10061087 }
10071088
10081089 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
@@ -1334,7 +1415,7 @@ again: · 325 unmodified lines
13341415 }
13351416
13361417 /* Now flush any delayed refs generated by updating all of the roots */
1337− ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1418+ ret = btrfs_run_delayed_refs(trans, U64_MAX);
13381419 if (ret)
13391420 return ret;
13401421
@@ -1349,7 +1430,7 @@ again: · 8 unmodified lines
13491430 * so we want to keep this flushing in this loop to make sure
13501431 * everything gets run.
13511432 */
1352− ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1433+ ret = btrfs_run_delayed_refs(trans, U64_MAX);
13531434 if (ret)
13541435 return ret;
13551436 }
@@ -1483,45 +1564,6 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) · 127 unmodified lines
14831564 return 0;
14841565 }
14851566
1486−/*
1487− * defrag a given btree.
1488− * Every leaf in the btree is read and defragged.
1489− */
1490−int btrfs_defrag_root(struct btrfs_root *root)
1491−{
1492− struct btrfs_fs_info *info = root->fs_info;
1493− struct btrfs_trans_handle *trans;
1494− int ret;
1495−
1496− if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1497− return 0;
1498−
1499− while (1) {
1500− trans = btrfs_start_transaction(root, 0);
1501− if (IS_ERR(trans)) {
1502− ret = PTR_ERR(trans);
1503− break;
1504− }
1505−
1506− ret = btrfs_defrag_leaves(trans, root);
1507−
1508− btrfs_end_transaction(trans);
1509− btrfs_btree_balance_dirty(info);
1510− cond_resched();
1511−
1512− if (btrfs_fs_closing(info) || ret != -EAGAIN)
1513− break;
1514−
1515− if (btrfs_defrag_cancelled(info)) {
1516− btrfs_debug(info, "defrag_root cancelled");
1517− ret = -EAGAIN;
1518− break;
1519− }
1520− }
1521− clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
1522− return ret;
1523−}
1524−
15251567 /*
15261568 * Do all special snapshot related qgroup dirty hack.
15271569 *
@@ -1539,11 +1581,10 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, · 11 unmodified lines
15391581 int ret;
15401582
15411583 /*
1542− * Save some performance in the case that qgroups are not
1543− * enabled. If this check races with the ioctl, rescan will
1544− * kick in anyway.
1584+ * Save some performance in the case that qgroups are not enabled. If
1585+ * this check races with the ioctl, rescan will kick in anyway.
15451586 */
1546− if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1587+ if (!btrfs_qgroup_full_accounting(fs_info))
15471588 return 0;
15481589
15491590 /*
@@ -1567,7 +1608,7 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, · 17 unmodified lines
15671608 * for now flush the delayed refs to narrow the race window where the
15681609 * qgroup counters could end up wrong.
15691610 */
1570− ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1611+ ret = btrfs_run_delayed_refs(trans, U64_MAX);
15711612 if (ret) {
15721613 btrfs_abort_transaction(trans, ret);
15731614 return ret;
@@ -1582,7 +1623,7 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, · 8 unmodified lines
15821623
15831624 /* Now qgroup are all updated, we can inherit it to new qgroups */
15841625 ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1585− inherit);
1626+ parent->root_key.objectid, inherit);
15861627 if (ret < 0)
15871628 goto out;
15881629
@@ -1732,6 +1773,12 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 143 unmodified lines
17321773 }
17331774 btrfs_release_path(path);
17341775
1776+ ret = btrfs_create_qgroup(trans, objectid);
1777+ if (ret) {
1778+ btrfs_abort_transaction(trans, ret);
1779+ goto fail;
1780+ }
1781+
17351782 /*
17361783 * pull in the delayed directory update
17371784 * and the delayed inode item
@@ -1843,8 +1890,12 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 105 unmodified lines
18431890 * To co-operate with that hack, we do hack again.
18441891 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
18451892 */
1846− ret = qgroup_account_snapshot(trans, root, parent_root,
1847− pending->inherit, objectid);
1893+ if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL)
1894+ ret = qgroup_account_snapshot(trans, root, parent_root,
1895+ pending->inherit, objectid);
1896+ else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1897+ ret = btrfs_qgroup_inherit(trans, root->root_key.objectid, objectid,
1898+ parent_root->root_key.objectid, pending->inherit);
18481899 if (ret < 0)
18491900 goto fail;
18501901
@@ -1862,7 +1913,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 11 unmodified lines
18621913 fname.disk_name.len * 2);
18631914 inode_set_mtime_to_ts(parent_inode,
18641915 inode_set_ctime_current(parent_inode));
1865− ret = btrfs_update_inode_fallback(trans, parent_root, BTRFS_I(parent_inode));
1916+ ret = btrfs_update_inode_fallback(trans, BTRFS_I(parent_inode));
18661917 if (ret) {
18671918 btrfs_abort_transaction(trans, ret);
18681919 goto fail;
@@ -2085,7 +2136,7 @@ static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) · 216 unmodified lines
20852136 struct btrfs_block_group *block_group, *tmp;
20862137
20872138 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
2088− btrfs_delayed_refs_rsv_release(fs_info, 1);
2139+ btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
20892140 list_del_init(&block_group->bg_list);
20902141 }
20912142 }
@@ -2404,7 +2455,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) · 312 unmodified lines
24042455 if (ret)
24052456 goto unlock_reloc;
24062457
2407− ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2458+ ret = btrfs_run_delayed_refs(trans, U64_MAX);
24082459 if (ret)
24092460 goto unlock_reloc;
24102461
@@ -2537,7 +2588,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) · 126 unmodified lines
25372588 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
25382589 btrfs_clear_space_info_full(fs_info);
25392590
2540− fs_info->last_trans_committed = cur_trans->transid;
2591+ btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
25412592 /*
25422593 * We needn't acquire the lock here because there is no other task
25432594 * which can change it.
@@ -2655,18 +2706,18 @@ int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info) · 111 unmodified lines
26552706 */
26562707 void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
26572708 const char *function,
2658− unsigned int line, int errno, bool first_hit)
2709+ unsigned int line, int error, bool first_hit)
26592710 {
26602711 struct btrfs_fs_info *fs_info = trans->fs_info;
26612712
2662− WRITE_ONCE(trans->aborted, errno);
2663− WRITE_ONCE(trans->transaction->aborted, errno);
2664− if (first_hit && errno == -ENOSPC)
2713+ WRITE_ONCE(trans->aborted, error);
2714+ WRITE_ONCE(trans->transaction->aborted, error);
2715+ if (first_hit && error == -ENOSPC)
26652716 btrfs_dump_space_info_for_trans_abort(fs_info);
26662717 /* Wake up anybody who may be waiting on this transaction */
26672718 wake_up(&fs_info->transaction_wait);
26682719 wake_up(&fs_info->transaction_blocked_wait);
2669− __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
2720+ __btrfs_handle_fs_error(fs_info, function, line, error, NULL);
26702721 }
26712722
26722723 int __init btrfs_transaction_init(void)
fs/btrfs/transaction.h+11−9
@@ -118,8 +118,10 @@ enum { · 117 unmodified lines
118118 struct btrfs_trans_handle {
119119 u64 transid;
120120 u64 bytes_reserved;
121+ u64 delayed_refs_bytes_reserved;
121122 u64 chunk_bytes_reserved;
122123 unsigned long delayed_ref_updates;
124+ unsigned long delayed_ref_csum_deletions;
123125 struct btrfs_transaction *transaction;
124126 struct btrfs_block_rsv *block_rsv;
125127 struct btrfs_block_rsv *orig_rsv;
@@ -139,6 +141,7 @@ struct btrfs_trans_handle { · 13 unmodified lines
139141 bool in_fsync;
140142 struct btrfs_fs_info *fs_info;
141143 struct list_head new_bgs;
144+ struct btrfs_block_rsv delayed_rsv;
142145 };
143146
144147 /*
@@ -172,7 +175,7 @@ static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans, · 27 unmodified lines
172175 {
173176 spin_lock(&inode->lock);
174177 inode->last_trans = trans->transaction->transid;
175− inode->last_sub_trans = inode->root->log_transid;
178+ inode->last_sub_trans = btrfs_get_root_log_transid(inode->root);
176179 inode->last_log_commit = inode->last_sub_trans - 1;
177180 spin_unlock(&inode->lock);
178181 }
@@ -200,32 +203,32 @@ static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans) · 21 unmodified lines
200203 delayed_refs->qgroup_to_skip = 0;
201204 }
202205
203−bool __cold abort_should_print_stack(int errno);
206+bool __cold abort_should_print_stack(int error);
204207
205208 /*
206209 * Call btrfs_abort_transaction as early as possible when an error condition is
207210 * detected, that way the exact stack trace is reported for some errors.
208211 */
209−#define btrfs_abort_transaction(trans, errno) \
212+#define btrfs_abort_transaction(trans, error) \
210213 do { \
211214 bool first = false; \
212215 /* Report first abort since mount */ \
213216 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
214217 &((trans)->fs_info->fs_state))) { \
215218 first = true; \
216− if (WARN(abort_should_print_stack(errno), \
219+ if (WARN(abort_should_print_stack(error), \
217220 KERN_ERR \
218221 "BTRFS: Transaction aborted (error %d)\n", \
219− (errno))) { \
222+ (error))) { \
220223 /* Stack trace printed. */ \
221224 } else { \
222225 btrfs_err((trans)->fs_info, \
223226 "Transaction aborted (error %d)", \
224− (errno)); \
227+ (error)); \
225228 } \
226229 } \
227230 __btrfs_abort_transaction((trans), __func__, \
228− __LINE__, (errno), first); \
231+ __LINE__, (error), first); \
229232 } while (0)
230233
231234 int btrfs_end_transaction(struct btrfs_trans_handle *trans);
@@ -243,7 +246,6 @@ struct btrfs_trans_handle *btrfs_attach_transaction_barrier( · 11 unmodified lines
243246 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
244247
245248 void btrfs_add_dead_root(struct btrfs_root *root);
246−int btrfs_defrag_root(struct btrfs_root *root);
247249 void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
248250 int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
249251 int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
@@ -264,7 +266,7 @@ void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, · 14 unmodified lines
264266 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
265267 void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
266268 const char *function,
267− unsigned int line, int errno, bool first_hit);
269+ unsigned int line, int error, bool first_hit);
268270
269271 int __init btrfs_transaction_init(void);
270272 void __cold btrfs_transaction_exit(void);
fs/btrfs/tree-checker.c+47−1
@@ -29,6 +29,8 @@ · 28 unmodified lines
2929 #include "accessors.h"
3030 #include "file-item.h"
3131 #include "inode-item.h"
32+#include "dir-item.h"
33+#include "raid-stripe-tree.h"
3234
3335 /*
3436 * Error message should follow the following format:
@@ -1465,6 +1467,9 @@ static int check_extent_item(struct extent_buffer *leaf, · 1430 unmodified lines
14651467 }
14661468 inline_refs += btrfs_shared_data_ref_count(leaf, sref);
14671469 break;
1470+ case BTRFS_EXTENT_OWNER_REF_KEY:
1471+ WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
1472+ break;
14681473 default:
14691474 extent_err(leaf, slot, "unknown inline ref type: %u",
14701475 inline_type);
@@ -1631,6 +1636,44 @@ static int check_inode_ref(struct extent_buffer *leaf, · 160 unmodified lines
16311636 return 0;
16321637 }
16331638
1639+static int check_raid_stripe_extent(const struct extent_buffer *leaf,
1640+ const struct btrfs_key *key, int slot)
1641+{
1642+ struct btrfs_stripe_extent *stripe_extent =
1643+ btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
1644+
1645+ if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1646+ generic_err(leaf, slot,
1647+"invalid key objectid for raid stripe extent, have %llu expect aligned to %u",
1648+ key->objectid, leaf->fs_info->sectorsize);
1649+ return -EUCLEAN;
1650+ }
1651+
1652+ if (unlikely(!btrfs_fs_incompat(leaf->fs_info, RAID_STRIPE_TREE))) {
1653+ generic_err(leaf, slot,
1654+ "RAID_STRIPE_EXTENT present but RAID_STRIPE_TREE incompat bit unset");
1655+ return -EUCLEAN;
1656+ }
1657+
1658+ switch (btrfs_stripe_extent_encoding(leaf, stripe_extent)) {
1659+ case BTRFS_STRIPE_RAID0:
1660+ case BTRFS_STRIPE_RAID1:
1661+ case BTRFS_STRIPE_DUP:
1662+ case BTRFS_STRIPE_RAID10:
1663+ case BTRFS_STRIPE_RAID5:
1664+ case BTRFS_STRIPE_RAID6:
1665+ case BTRFS_STRIPE_RAID1C3:
1666+ case BTRFS_STRIPE_RAID1C4:
1667+ break;
1668+ default:
1669+ generic_err(leaf, slot, "invalid raid stripe encoding %u",
1670+ btrfs_stripe_extent_encoding(leaf, stripe_extent));
1671+ return -EUCLEAN;
1672+ }
1673+
1674+ return 0;
1675+}
1676+
16341677 /*
16351678 * Common point to switch the item-specific validation.
16361679 */
@@ -1685,6 +1728,9 @@ static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf, · 48 unmodified lines
16851728 case BTRFS_EXTENT_DATA_REF_KEY:
16861729 ret = check_extent_data_ref(leaf, key, slot);
16871730 break;
1731+ case BTRFS_RAID_STRIPE_KEY:
1732+ ret = check_raid_stripe_extent(leaf, key, slot);
1733+ break;
16881734 }
16891735
16901736 if (ret)
@@ -2005,7 +2051,7 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level, · 314 unmodified lines
20052051 * So we only checks tree blocks which is read from disk, whose
20062052 * generation <= fs_info->last_trans_committed.
20072053 */
2008− if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
2054+ if (btrfs_header_generation(eb) > btrfs_get_last_trans_committed(fs_info))
20092055 return 0;
20102056
20112057 /* We have @first_key, so this @eb must have at least one item */
fs/btrfs/tree-log.c+36−45
@@ -347,8 +347,7 @@ static int process_one_buffer(struct btrfs_root *log, · 346 unmodified lines
347347 }
348348
349349 if (wc->pin) {
350− ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
351− eb->len);
350+ ret = btrfs_pin_extent_for_log_replay(wc->trans, eb);
352351 if (ret)
353352 return ret;
354353
@@ -504,9 +503,9 @@ insert: · 149 unmodified lines
504503 found_size = btrfs_item_size(path->nodes[0],
505504 path->slots[0]);
506505 if (found_size > item_size)
507− btrfs_truncate_item(path, item_size, 1);
506+ btrfs_truncate_item(trans, path, item_size, 1);
508507 else if (found_size < item_size)
509− btrfs_extend_item(path, item_size - found_size);
508+ btrfs_extend_item(trans, path, item_size - found_size);
510509 } else if (ret) {
511510 return ret;
512511 }
@@ -574,7 +573,7 @@ insert: · 61 unmodified lines
574573 }
575574 }
576575 no_copy:
577− btrfs_mark_buffer_dirty(path->nodes[0]);
576+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
578577 btrfs_release_path(path);
579578 return 0;
580579 }
@@ -767,7 +766,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, · 186 unmodified lines
767766 } else if (ret == 0) {
768767 btrfs_init_generic_ref(&ref,
769768 BTRFS_ADD_DELAYED_REF,
770− ins.objectid, ins.offset, 0);
769+ ins.objectid, ins.offset, 0,
770+ root->root_key.objectid);
771771 btrfs_init_data_ref(&ref,
772772 root->root_key.objectid,
773773 key->objectid, offset, 0, false);
@@ -890,7 +890,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, · 116 unmodified lines
890890
891891 update_inode:
892892 btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found);
893− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
893+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
894894 out:
895895 iput(inode);
896896 return ret;
@@ -1445,7 +1445,7 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans, · 548 unmodified lines
14451445 if (ret)
14461446 goto out;
14471447
1448− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1448+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
14491449 if (ret)
14501450 goto out;
14511451 }
@@ -1483,8 +1483,7 @@ out: · 31 unmodified lines
14831483 return ret;
14841484 }
14851485
1486−static int count_inode_extrefs(struct btrfs_root *root,
1487− struct btrfs_inode *inode, struct btrfs_path *path)
1486+static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path)
14881487 {
14891488 int ret = 0;
14901489 int name_len;
@@ -1498,8 +1497,8 @@ static int count_inode_extrefs(struct btrfs_root *root, · 7 unmodified lines
14981497 struct extent_buffer *leaf;
14991498
15001499 while (1) {
1501− ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1502− &extref, &offset);
1500+ ret = btrfs_find_one_extref(inode->root, inode_objectid, offset,
1501+ path, &extref, &offset);
15031502 if (ret)
15041503 break;
15051504
@@ -1527,8 +1526,7 @@ static int count_inode_extrefs(struct btrfs_root *root, · 21 unmodified lines
15271526 return nlink;
15281527 }
15291528
1530−static int count_inode_refs(struct btrfs_root *root,
1531− struct btrfs_inode *inode, struct btrfs_path *path)
1529+static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path)
15321530 {
15331531 int ret;
15341532 struct btrfs_key key;
@@ -1543,7 +1541,7 @@ static int count_inode_refs(struct btrfs_root *root, · 8 unmodified lines
15431541 key.offset = (u64)-1;
15441542
15451543 while (1) {
1546− ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1544+ ret = btrfs_search_slot(NULL, inode->root, &key, path, 0, 0);
15471545 if (ret < 0)
15481546 break;
15491547 if (ret > 0) {
@@ -1595,9 +1593,9 @@ process_slot: · 45 unmodified lines
15951593 * will free the inode.
15961594 */
15971595 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1598− struct btrfs_root *root,
15991596 struct inode *inode)
16001597 {
1598+ struct btrfs_root *root = BTRFS_I(inode)->root;
16011599 struct btrfs_path *path;
16021600 int ret;
16031601 u64 nlink = 0;
@@ -1607,13 +1605,13 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, · 3 unmodified lines
16071605 if (!path)
16081606 return -ENOMEM;
16091607
1610− ret = count_inode_refs(root, BTRFS_I(inode), path);
1608+ ret = count_inode_refs(BTRFS_I(inode), path);
16111609 if (ret < 0)
16121610 goto out;
16131611
16141612 nlink = ret;
16151613
1616− ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1614+ ret = count_inode_extrefs(BTRFS_I(inode), path);
16171615 if (ret < 0)
16181616 goto out;
16191617
@@ -1623,7 +1621,7 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, · 3 unmodified lines
16231621
16241622 if (nlink != inode->i_nlink) {
16251623 set_nlink(inode, nlink);
1626− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1624+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
16271625 if (ret)
16281626 goto out;
16291627 }
@@ -1685,7 +1683,7 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, · 55 unmodified lines
16851683 break;
16861684 }
16871685
1688− ret = fixup_inode_link_count(trans, root, inode);
1686+ ret = fixup_inode_link_count(trans, inode);
16891687 iput(inode);
16901688 if (ret)
16911689 break;
@@ -1732,7 +1730,7 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, · 40 unmodified lines
17321730 set_nlink(inode, 1);
17331731 else
17341732 inc_nlink(inode);
1735− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1733+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
17361734 } else if (ret == -EEXIST) {
17371735 ret = 0;
17381736 }
@@ -1939,7 +1937,7 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans, · 200 unmodified lines
19391937 out:
19401938 if (!ret && update_size) {
19411939 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name.len * 2);
1942− ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
1940+ ret = btrfs_update_inode(trans, BTRFS_I(dir));
19431941 }
19441942 kfree(name.name);
19451943 iput(dir);
@@ -2483,7 +2481,7 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, · 537 unmodified lines
24832481 drop_args.bytes_found);
24842482 /* Update the inode's nbytes. */
24852483 ret = btrfs_update_inode(wc->trans,
2486− root, BTRFS_I(inode));
2484+ BTRFS_I(inode));
24872485 }
24882486 iput(inode);
24892487 if (ret)
@@ -2574,7 +2572,7 @@ static int clean_log_buffer(struct btrfs_trans_handle *trans, · 84 unmodified lines
25742572 btrfs_tree_unlock(eb);
25752573
25762574 if (trans) {
2577− ret = btrfs_pin_reserved_extent(trans, eb->start, eb->len);
2575+ ret = btrfs_pin_reserved_extent(trans, eb);
25782576 if (ret)
25792577 return ret;
25802578 btrfs_redirty_list_add(trans->transaction, eb);
@@ -2848,10 +2846,9 @@ static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root, · 267 unmodified lines
28482846 }
28492847
28502848 /*
2851− * btrfs_sync_log does sends a given tree log down to the disk and
2852− * updates the super blocks to record it. When this call is done,
2853− * you know that any inodes previously logged are safely on disk only
2854− * if it returns 0.
2849+ * Sends a given tree log down to the disk and updates the super blocks to
2850+ * record it. When this call is done, you know that any inodes previously
2851+ * logged are safely on disk only if it returns 0.
28552852 *
28562853 * Any other return value means you need to call btrfs_commit_transaction.
28572854 * Some of the edge cases for fsyncing directories that have had unlinks
@@ -2961,7 +2958,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 103 unmodified lines
29612958 btrfs_set_root_node(&log->root_item, log->node);
29622959 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
29632960
2964− root->log_transid++;
2961+ btrfs_set_root_log_transid(root, root->log_transid + 1);
29652962 log->log_transid = root->log_transid;
29662963 root->log_start_pid = 0;
29672964 /*
@@ -2999,9 +2996,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 31 unmodified lines
29992996 */
30002997 ret = update_log_root(trans, log, &new_root_item);
30012998 if (ret) {
3002− if (!list_empty(&root_log_ctx.list))
3003− list_del_init(&root_log_ctx.list);
3004−
2999+ list_del_init(&root_log_ctx.list);
30053000 blk_finish_plug(&plug);
30063001 btrfs_set_log_full_commit(trans);
30073002 if (ret != -ENOSPC)
@@ -3021,7 +3016,6 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 13 unmodified lines
30213016 goto out;
30223017 }
30233018
3024− index2 = root_log_ctx.log_transid % 2;
30253019 if (atomic_read(&log_root_tree->log_commit[index2])) {
30263020 blk_finish_plug(&plug);
30273021 ret = btrfs_wait_tree_log_extents(log, mark);
@@ -3136,8 +3130,8 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 108 unmodified lines
31363130 * someone else already started it. We use <= and not < because the
31373131 * first log transaction has an ID of 0.
31383132 */
3139− ASSERT(root->last_log_commit <= log_transid);
3140− root->last_log_commit = log_transid;
3133+ ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid);
3134+ btrfs_set_root_last_log_commit(root, log_transid);
31413135
31423136 out_wake_log_root:
31433137 mutex_lock(&log_root_tree->log_mutex);
@@ -3211,8 +3205,7 @@ static void free_log_tree(struct btrfs_trans_handle *trans, · 67 unmodified lines
32113205 }
32123206 }
32133207
3214− clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3215− EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3208+ extent_io_tree_release(&log->dirty_log_pages);
32163209 extent_io_tree_release(&log->log_csum_range);
32173210
32183211 btrfs_put_root(log);
@@ -3530,7 +3523,7 @@ static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, · 311 unmodified lines
35303523 last_offset = max(last_offset, curr_end);
35313524 }
35323525 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3533− btrfs_mark_buffer_dirty(path->nodes[0]);
3526+ btrfs_mark_buffer_dirty(trans, path->nodes[0]);
35343527 btrfs_release_path(path);
35353528 return 0;
35363529 }
@@ -4488,7 +4481,7 @@ copy_item: · 951 unmodified lines
44884481 dst_index++;
44894482 }
44904483
4491− btrfs_mark_buffer_dirty(dst_path->nodes[0]);
4484+ btrfs_mark_buffer_dirty(trans, dst_path->nodes[0]);
44924485 btrfs_release_path(dst_path);
44934486 out:
44944487 kfree(ins_data);
@@ -4693,7 +4686,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans, · 198 unmodified lines
46934686 write_extent_buffer(leaf, &fi,
46944687 btrfs_item_ptr_offset(leaf, path->slots[0]),
46954688 sizeof(fi));
4696− btrfs_mark_buffer_dirty(leaf);
4689+ btrfs_mark_buffer_dirty(trans, leaf);
46974690
46984691 btrfs_release_path(path);
46994692
@@ -4921,12 +4914,12 @@ process: · 221 unmodified lines
49214914 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
49224915
49234916 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4924− spin_lock_irq(&inode->ordered_tree.lock);
4917+ spin_lock_irq(&inode->ordered_tree_lock);
49254918 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
49264919 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
49274920 atomic_inc(&trans->transaction->pending_ordered);
49284921 }
4929− spin_unlock_irq(&inode->ordered_tree.lock);
4922+ spin_unlock_irq(&inode->ordered_tree_lock);
49304923 }
49314924 btrfs_put_ordered_extent(ordered);
49324925 }
@@ -7204,9 +7197,7 @@ again: · 2271 unmodified lines
72047197 * each subsequent pass.
72057198 */
72067199 if (ret == -ENOENT)
7207− ret = btrfs_pin_extent_for_log_replay(trans,
7208− log->node->start,
7209− log->node->len);
7200+ ret = btrfs_pin_extent_for_log_replay(trans, log->node);
72107201 btrfs_put_root(log);
72117202
72127203 if (!ret)
fs/btrfs/ulist.c+2−1
@@ -223,7 +223,8 @@ int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux, · 222 unmodified lines
223223 }
224224
225225 /*
226− * ulist_del - delete one node from ulist
226+ * Delete one node from ulist.
227+ *
227228 * @ulist: ulist to remove node from
228229 * @val: value to delete
229230 * @aux: aux to delete
fs/btrfs/uuid-tree.c+3−3
@@ -124,7 +124,7 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, · 123 unmodified lines
124124 * An item with that type already exists.
125125 * Extend the item and store the new subid at the end.
126126 */
127− btrfs_extend_item(path, sizeof(subid_le));
127+ btrfs_extend_item(trans, path, sizeof(subid_le));
128128 eb = path->nodes[0];
129129 slot = path->slots[0];
130130 offset = btrfs_item_ptr_offset(eb, slot);
@@ -139,7 +139,7 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, · 8 unmodified lines
139139 ret = 0;
140140 subid_le = cpu_to_le64(subid_cpu);
141141 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
142− btrfs_mark_buffer_dirty(eb);
142+ btrfs_mark_buffer_dirty(trans, eb);
143143
144144 out:
145145 btrfs_free_path(path);
@@ -221,7 +221,7 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type, · 75 unmodified lines
221221 move_src = offset + sizeof(subid);
222222 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
223223 memmove_extent_buffer(eb, move_dst, move_src, move_len);
224− btrfs_truncate_item(path, item_size - sizeof(subid), 1);
224+ btrfs_truncate_item(trans, path, item_size - sizeof(subid), 1);
225225
226226 out:
227227 btrfs_free_path(path);
fs/btrfs/verity.c+2−2
@@ -487,7 +487,7 @@ static int rollback_verity(struct btrfs_inode *inode) · 486 unmodified lines
487487 }
488488 inode->ro_flags &= ~BTRFS_INODE_RO_VERITY;
489489 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
490− ret = btrfs_update_inode(trans, root, inode);
490+ ret = btrfs_update_inode(trans, inode);
491491 if (ret) {
492492 btrfs_abort_transaction(trans, ret);
493493 goto out;
@@ -554,7 +554,7 @@ static int finish_verity(struct btrfs_inode *inode, const void *desc, · 60 unmodified lines
554554 }
555555 inode->ro_flags |= BTRFS_INODE_RO_VERITY;
556556 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
557− ret = btrfs_update_inode(trans, root, inode);
557+ ret = btrfs_update_inode(trans, inode);
558558 if (ret)
559559 goto end_trans;
560560 ret = del_orphan(trans, inode);
fs/btrfs/volumes.c+203−214
@@ -35,6 +35,7 @@ · 34 unmodified lines
3535 #include "relocation.h"
3636 #include "scrub.h"
3737 #include "super.h"
38+#include "raid-stripe-tree.h"
3839
3940 #define BTRFS_BLOCK_GROUP_STRIPE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \
4041 BTRFS_BLOCK_GROUP_RAID10 | \
@@ -357,21 +358,19 @@ struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) · 316 unmodified lines
357358 }
358359
359360 /*
360− * alloc_fs_devices - allocate struct btrfs_fs_devices
361− * @fsid: if not NULL, copy the UUID to fs_devices::fsid
362− * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
361+ * Allocate new btrfs_fs_devices structure identified by a fsid.
362+ *
363+ * @fsid: if not NULL, copy the UUID to fs_devices::fsid and to
364+ * fs_devices::metadata_fsid
363365 *
364366 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
365367 * The returned struct is not linked onto any lists and can be destroyed with
366368 * kfree() right away.
367369 */
368−static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
369− const u8 *metadata_fsid)
370+static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
370371 {
371372 struct btrfs_fs_devices *fs_devs;
372373
373− ASSERT(fsid || !metadata_fsid);
374−
375374 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
376375 if (!fs_devs)
377376 return ERR_PTR(-ENOMEM);
@@ -385,8 +384,7 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, · 7 unmodified lines
385384
386385 if (fsid) {
387386 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
388− memcpy(fs_devs->metadata_uuid,
389− metadata_fsid ?: fsid, BTRFS_FSID_SIZE);
387+ memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
390388 }
391389
392390 return fs_devs;
@@ -457,58 +455,6 @@ static noinline struct btrfs_fs_devices *find_fsid( · 64 unmodified lines
457455 return NULL;
458456 }
459457
460−/*
461− * First check if the metadata_uuid is different from the fsid in the given
462− * fs_devices. Then check if the given fsid is the same as the metadata_uuid
463− * in the fs_devices. If it is, return true; otherwise, return false.
464− */
465−static inline bool check_fsid_changed(const struct btrfs_fs_devices *fs_devices,
466− const u8 *fsid)
467−{
468− return memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
469− BTRFS_FSID_SIZE) != 0 &&
470− memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE) == 0;
471−}
472−
473−static struct btrfs_fs_devices *find_fsid_with_metadata_uuid(
474− struct btrfs_super_block *disk_super)
475−{
476−
477− struct btrfs_fs_devices *fs_devices;
478−
479− /*
480− * Handle scanned device having completed its fsid change but
481− * belonging to a fs_devices that was created by first scanning
482− * a device which didn't have its fsid/metadata_uuid changed
483− * at all and the CHANGING_FSID_V2 flag set.
484− */
485− list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
486− if (!fs_devices->fsid_change)
487− continue;
488−
489− if (match_fsid_fs_devices(fs_devices, disk_super->metadata_uuid,
490− fs_devices->fsid))
491− return fs_devices;
492− }
493−
494− /*
495− * Handle scanned device having completed its fsid change but
496− * belonging to a fs_devices that was created by a device that
497− * has an outdated pair of fsid/metadata_uuid and
498− * CHANGING_FSID_V2 flag set.
499− */
500− list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
501− if (!fs_devices->fsid_change)
502− continue;
503−
504− if (check_fsid_changed(fs_devices, disk_super->metadata_uuid))
505− return fs_devices;
506− }
507−
508− return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
509−}
510−
511−
512458 static int
513459 btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,
514460 int flush, struct bdev_handle **bdev_handle,
@@ -564,13 +510,13 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device · 49 unmodified lines
564510 {
565511 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
566512 struct btrfs_device *device, *tmp_device;
567− int ret = 0;
513+ int ret;
514+ bool freed = false;
568515
569516 lockdep_assert_held(&uuid_mutex);
570517
571− if (devt)
572− ret = -ENOENT;
573−
518+ /* Return good status if there is no instance of devt. */
519+ ret = 0;
574520 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
575521
576522 mutex_lock(&fs_devices->device_list_mutex);
@@ -581,8 +527,7 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device · 4 unmodified lines
581527 if (devt && devt != device->devt)
582528 continue;
583529 if (fs_devices->opened) {
584− /* for an already deleted device return 0 */
585− if (devt && ret != 0)
530+ if (devt)
586531 ret = -EBUSY;
587532 break;
588533 }
@@ -592,7 +537,7 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device · 3 unmodified lines
592537 list_del(&device->dev_list);
593538 btrfs_free_device(device);
594539
595− ret = 0;
540+ freed = true;
596541 }
597542 mutex_unlock(&fs_devices->device_list_mutex);
598543
@@ -603,9 +548,81 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device · 4 unmodified lines
603548 }
604549 }
605550
551+ /* If there is at least one freed device return 0. */
552+ if (freed)
553+ return 0;
554+
606555 return ret;
607556 }
608557
558+static struct btrfs_fs_devices *find_fsid_by_device(
559+ struct btrfs_super_block *disk_super,
560+ dev_t devt, bool *same_fsid_diff_dev)
561+{
562+ struct btrfs_fs_devices *fsid_fs_devices;
563+ struct btrfs_fs_devices *devt_fs_devices;
564+ const bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
565+ BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
566+ bool found_by_devt = false;
567+
568+ /* Find the fs_device by the usual method, if found use it. */
569+ fsid_fs_devices = find_fsid(disk_super->fsid,
570+ has_metadata_uuid ? disk_super->metadata_uuid : NULL);
571+
572+ /* The temp_fsid feature is supported only with single device filesystem. */
573+ if (btrfs_super_num_devices(disk_super) != 1)
574+ return fsid_fs_devices;
575+
576+ /*
577+ * A seed device is an integral component of the sprout device, which
578+ * functions as a multi-device filesystem. So, temp-fsid feature is
579+ * not supported.
580+ */
581+ if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING)
582+ return fsid_fs_devices;
583+
584+ /* Try to find a fs_devices by matching devt. */
585+ list_for_each_entry(devt_fs_devices, &fs_uuids, fs_list) {
586+ struct btrfs_device *device;
587+
588+ list_for_each_entry(device, &devt_fs_devices->devices, dev_list) {
589+ if (device->devt == devt) {
590+ found_by_devt = true;
591+ break;
592+ }
593+ }
594+ if (found_by_devt)
595+ break;
596+ }
597+
598+ if (found_by_devt) {
599+ /* Existing device. */
600+ if (fsid_fs_devices == NULL) {
601+ if (devt_fs_devices->opened == 0) {
602+ /* Stale device. */
603+ return NULL;
604+ } else {
605+ /* temp_fsid is mounting a subvol. */
606+ return devt_fs_devices;
607+ }
608+ } else {
609+ /* Regular or temp_fsid device mounting a subvol. */
610+ return devt_fs_devices;
611+ }
612+ } else {
613+ /* New device. */
614+ if (fsid_fs_devices == NULL) {
615+ return NULL;
616+ } else {
617+ /* sb::fsid is already used create a new temp_fsid. */
618+ *same_fsid_diff_dev = true;
619+ return NULL;
620+ }
621+ }
622+
623+ /* Not reached. */
624+}
625+
609626 /*
610627 * This is only used on mount, and we are protected from competing things
611628 * messing with our fs_devices by the uuid_mutex, thus we do not need the
@@ -691,84 +708,6 @@ u8 *btrfs_sb_fsid_ptr(struct btrfs_super_block *sb) · 79 unmodified lines
691708 return has_metadata_uuid ? sb->metadata_uuid : sb->fsid;
692709 }
693710
694−/*
695− * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
696− * being created with a disk that has already completed its fsid change. Such
697− * disk can belong to an fs which has its FSID changed or to one which doesn't.
698− * Handle both cases here.
699− */
700−static struct btrfs_fs_devices *find_fsid_inprogress(
701− struct btrfs_super_block *disk_super)
702−{
703− struct btrfs_fs_devices *fs_devices;
704−
705− list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
706− if (fs_devices->fsid_change)
707− continue;
708−
709− if (check_fsid_changed(fs_devices, disk_super->fsid))
710− return fs_devices;
711− }
712−
713− return find_fsid(disk_super->fsid, NULL);
714−}
715−
716−static struct btrfs_fs_devices *find_fsid_changed(
717− struct btrfs_super_block *disk_super)
718−{
719− struct btrfs_fs_devices *fs_devices;
720−
721− /*
722− * Handles the case where scanned device is part of an fs that had
723− * multiple successful changes of FSID but currently device didn't
724− * observe it. Meaning our fsid will be different than theirs. We need
725− * to handle two subcases :
726− * 1 - The fs still continues to have different METADATA/FSID uuids.
727− * 2 - The fs is switched back to its original FSID (METADATA/FSID
728− * are equal).
729− */
730− list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
731− /* Changed UUIDs */
732− if (check_fsid_changed(fs_devices, disk_super->metadata_uuid) &&
733− memcmp(fs_devices->fsid, disk_super->fsid,
734− BTRFS_FSID_SIZE) != 0)
735− return fs_devices;
736−
737− /* Unchanged UUIDs */
738− if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
739− BTRFS_FSID_SIZE) == 0 &&
740− memcmp(fs_devices->fsid, disk_super->metadata_uuid,
741− BTRFS_FSID_SIZE) == 0)
742− return fs_devices;
743− }
744−
745− return NULL;
746−}
747−
748−static struct btrfs_fs_devices *find_fsid_reverted_metadata(
749− struct btrfs_super_block *disk_super)
750−{
751− struct btrfs_fs_devices *fs_devices;
752−
753− /*
754− * Handle the case where the scanned device is part of an fs whose last
755− * metadata UUID change reverted it to the original FSID. At the same
756− * time fs_devices was first created by another constituent device
757− * which didn't fully observe the operation. This results in an
758− * btrfs_fs_devices created with metadata/fsid different AND
759− * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
760− * fs_devices equal to the FSID of the disk.
761− */
762− list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
763− if (!fs_devices->fsid_change)
764− continue;
765−
766− if (check_fsid_changed(fs_devices, disk_super->fsid))
767− return fs_devices;
768− }
769−
770− return NULL;
771−}
772711 /*
773712 * Add new device to list of registered devices
774713 *
@@ -787,10 +726,16 @@ static noinline struct btrfs_device *device_list_add(const char *path, · 12 unmodified lines
787726 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
788727 dev_t path_devt;
789728 int error;
729+ bool same_fsid_diff_dev = false;
790730 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
791731 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
792− bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
793− BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
732+
733+ if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) {
734+ btrfs_err(NULL,
735+"device %s has incomplete metadata_uuid change, please use btrfstune to complete",
736+ path);
737+ return ERR_PTR(-EAGAIN);
738+ }
794739
795740 error = lookup_bdev(path, &path_devt);
796741 if (error) {
@@ -799,27 +744,23 @@ static noinline struct btrfs_device *device_list_add(const char *path, · 2 unmodified lines
799744 return ERR_PTR(error);
800745 }
801746
802− if (fsid_change_in_progress) {
803− if (!has_metadata_uuid)
804− fs_devices = find_fsid_inprogress(disk_super);
805− else
806− fs_devices = find_fsid_changed(disk_super);
807− } else if (has_metadata_uuid) {
808− fs_devices = find_fsid_with_metadata_uuid(disk_super);
809− } else {
810− fs_devices = find_fsid_reverted_metadata(disk_super);
811− if (!fs_devices)
812− fs_devices = find_fsid(disk_super->fsid, NULL);
813− }
814−
747+ fs_devices = find_fsid_by_device(disk_super, path_devt, &same_fsid_diff_dev);
815748
816749 if (!fs_devices) {
817− fs_devices = alloc_fs_devices(disk_super->fsid,
818− has_metadata_uuid ? disk_super->metadata_uuid : NULL);
750+ fs_devices = alloc_fs_devices(disk_super->fsid);
751+ if (has_metadata_uuid)
752+ memcpy(fs_devices->metadata_uuid,
753+ disk_super->metadata_uuid, BTRFS_FSID_SIZE);
754+
819755 if (IS_ERR(fs_devices))
820756 return ERR_CAST(fs_devices);
821757
822− fs_devices->fsid_change = fsid_change_in_progress;
758+ if (same_fsid_diff_dev) {
759+ generate_random_uuid(fs_devices->fsid);
760+ fs_devices->temp_fsid = true;
761+ pr_info("BTRFS: device %s using temp-fsid %pU\n",
762+ path, fs_devices->fsid);
763+ }
823764
824765 mutex_lock(&fs_devices->device_list_mutex);
825766 list_add(&fs_devices->fs_list, &fs_uuids);
@@ -834,18 +775,11 @@ static noinline struct btrfs_device *device_list_add(const char *path, · 8 unmodified lines
834775 mutex_lock(&fs_devices->device_list_mutex);
835776 device = btrfs_find_device(fs_devices, &args);
836777
837− /*
838− * If this disk has been pulled into an fs devices created by
839− * a device which had the CHANGING_FSID_V2 flag then replace the
840− * metadata_uuid/fsid values of the fs_devices.
841− */
842− if (fs_devices->fsid_change &&
843− found_transid > fs_devices->latest_generation) {
778+ if (found_transid > fs_devices->latest_generation) {
844779 memcpy(fs_devices->fsid, disk_super->fsid,
845780 BTRFS_FSID_SIZE);
846781 memcpy(fs_devices->metadata_uuid,
847782 btrfs_sb_fsid_ptr(disk_super), BTRFS_FSID_SIZE);
848− fs_devices->fsid_change = false;
849783 }
850784 }
851785
@@ -999,7 +933,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) · 147 unmodified lines
999933
1000934 lockdep_assert_held(&uuid_mutex);
1001935
1002− fs_devices = alloc_fs_devices(orig->fsid, NULL);
936+ fs_devices = alloc_fs_devices(orig->fsid);
1003937 if (IS_ERR(fs_devices))
1004938 return fs_devices;
1005939
@@ -1359,9 +1293,14 @@ int btrfs_forget_devices(dev_t devt) · 353 unmodified lines
13591293 /*
13601294 * Look for a btrfs signature on a device. This may be called out of the mount path
13611295 * and we are not allowed to call set_blocksize during the scan. The superblock
1362− * is read via pagecache
1296+ * is read via pagecache.
1297+ *
1298+ * With @mount_arg_dev it's a scan during mount time that will always register
1299+ * the device or return an error. Multi-device and seeding devices are registered
1300+ * in both cases.
13631301 */
1364−struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags)
1302+struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
1303+ bool mount_arg_dev)
13651304 {
13661305 struct btrfs_super_block *disk_super;
13671306 bool new_device_added = false;
@@ -1407,10 +1346,27 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags) · 39 unmodified lines
14071346 goto error_bdev_put;
14081347 }
14091348
1349+ if (!mount_arg_dev && btrfs_super_num_devices(disk_super) == 1 &&
1350+ !(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING)) {
1351+ dev_t devt;
1352+
1353+ ret = lookup_bdev(path, &devt);
1354+ if (ret)
1355+ btrfs_warn(NULL, "lookup bdev failed for path %s: %d",
1356+ path, ret);
1357+ else
1358+ btrfs_free_stale_devices(devt, NULL);
1359+
1360+ pr_debug("BTRFS: skip registering single non-seed device %s\n", path);
1361+ device = NULL;
1362+ goto free_disk_super;
1363+ }
1364+
14101365 device = device_list_add(path, disk_super, &new_device_added);
14111366 if (!IS_ERR(device) && new_device_added)
14121367 btrfs_free_stale_devices(device->devt, device);
14131368
1369+free_disk_super:
14141370 btrfs_release_disk_super(disk_super);
14151371
14161372 error_bdev_put:
@@ -1898,7 +1854,7 @@ static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, · 481 unmodified lines
18981854 ptr = btrfs_device_fsid(dev_item);
18991855 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
19001856 ptr, BTRFS_FSID_SIZE);
1901− btrfs_mark_buffer_dirty(leaf);
1857+ btrfs_mark_buffer_dirty(trans, leaf);
19021858
19031859 ret = 0;
19041860 out:
@@ -2454,7 +2410,7 @@ static struct btrfs_fs_devices *btrfs_init_sprout(struct btrfs_fs_info *fs_info) · 549 unmodified lines
24542410 * Private copy of the seed devices, anchored at
24552411 * fs_info->fs_devices->seed_list
24562412 */
2457− seed_devices = alloc_fs_devices(NULL, NULL);
2413+ seed_devices = alloc_fs_devices(NULL);
24582414 if (IS_ERR(seed_devices))
24592415 return seed_devices;
24602416
@@ -2600,7 +2556,7 @@ next_slot: · 139 unmodified lines
26002556 if (device->fs_devices->seeding) {
26012557 btrfs_set_device_generation(leaf, dev_item,
26022558 device->generation);
2603− btrfs_mark_buffer_dirty(leaf);
2559+ btrfs_mark_buffer_dirty(trans, leaf);
26042560 }
26052561
26062562 path->slots[0]++;
@@ -2898,7 +2854,7 @@ static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, · 291 unmodified lines
28982854 btrfs_device_get_disk_total_bytes(device));
28992855 btrfs_set_device_bytes_used(leaf, dev_item,
29002856 btrfs_device_get_bytes_used(device));
2901− btrfs_mark_buffer_dirty(leaf);
2857+ btrfs_mark_buffer_dirty(trans, leaf);
29022858
29032859 out:
29042860 btrfs_free_path(path);
@@ -2932,6 +2888,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans, · 27 unmodified lines
29322888 btrfs_set_super_total_bytes(super_copy,
29332889 round_down(old_total + diff, fs_info->sectorsize));
29342890 device->fs_devices->total_rw_bytes += diff;
2891+ atomic64_add(diff, &fs_info->free_chunk_space);
29352892
29362893 btrfs_device_set_total_bytes(device, new_size);
29372894 btrfs_device_set_disk_total_bytes(device, new_size);
@@ -3030,7 +2987,8 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) · 92 unmodified lines
30302987 }
30312988
30322989 /*
3033− * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2990+ * Find the mapping containing the given logical extent.
2991+ *
30342992 * @logical: Logical block offset in bytes.
30352993 * @length: Length of extent in bytes.
30362994 *
@@ -3486,7 +3444,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info, · 449 unmodified lines
34863444
34873445 btrfs_set_balance_flags(leaf, item, bctl->flags);
34883446
3489− btrfs_mark_buffer_dirty(leaf);
3447+ btrfs_mark_buffer_dirty(trans, leaf);
34903448 out:
34913449 btrfs_free_path(path);
34923450 err = btrfs_commit_transaction(trans);
@@ -4841,6 +4799,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) · 1348 unmodified lines
48414799 u64 old_size = btrfs_device_get_total_bytes(device);
48424800 u64 diff;
48434801 u64 start;
4802+ u64 free_diff = 0;
48444803
48454804 new_size = round_down(new_size, fs_info->sectorsize);
48464805 start = new_size;
@@ -4866,7 +4825,19 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) · 19 unmodified lines
48664825 btrfs_device_set_total_bytes(device, new_size);
48674826 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
48684827 device->fs_devices->total_rw_bytes -= diff;
4869− atomic64_sub(diff, &fs_info->free_chunk_space);
4828+
4829+ /*
4830+ * The new free_chunk_space is new_size - used, so we have to
4831+ * subtract the delta of the old free_chunk_space which included
4832+ * old_size - used. If used > new_size then just subtract this
4833+ * entire device's free space.
4834+ */
4835+ if (device->bytes_used < new_size)
4836+ free_diff = (old_size - device->bytes_used) -
4837+ (new_size - device->bytes_used);
4838+ else
4839+ free_diff = old_size - device->bytes_used;
4840+ atomic64_sub(free_diff, &fs_info->free_chunk_space);
48704841 }
48714842
48724843 /*
@@ -5001,9 +4972,10 @@ done: · 128 unmodified lines
50014972 if (ret) {
50024973 mutex_lock(&fs_info->chunk_mutex);
50034974 btrfs_device_set_total_bytes(device, old_size);
5004− if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
4975+ if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
50054976 device->fs_devices->total_rw_bytes += diff;
5006− atomic64_add(diff, &fs_info->free_chunk_space);
4977+ atomic64_add(free_diff, &fs_info->free_chunk_space);
4978+ }
50074979 mutex_unlock(&fs_info->chunk_mutex);
50084980 }
50094981 return ret;
@@ -5883,6 +5855,7 @@ static int find_live_mirror(struct btrfs_fs_info *fs_info, · 873 unmodified lines
58835855 }
58845856
58855857 static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
5858+ u64 logical,
58865859 u16 total_stripes)
58875860 {
58885861 struct btrfs_io_context *bioc;
@@ -5902,6 +5875,7 @@ static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_ · 13 unmodified lines
59025875 bioc->fs_info = fs_info;
59035876 bioc->replace_stripe_src = -1;
59045877 bioc->full_stripe_logical = (u64)-1;
5878+ bioc->logical = logical;
59055879
59065880 return bioc;
59075881 }
@@ -6206,12 +6180,20 @@ static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, · 298 unmodified lines
62066180 return U64_MAX;
62076181 }
62086182
6209−static void set_io_stripe(struct btrfs_io_stripe *dst, const struct map_lookup *map,
6210− u32 stripe_index, u64 stripe_offset, u32 stripe_nr)
6183+static int set_io_stripe(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6184+ u64 logical, u64 *length, struct btrfs_io_stripe *dst,
6185+ struct map_lookup *map, u32 stripe_index,
6186+ u64 stripe_offset, u64 stripe_nr)
62116187 {
62126188 dst->dev = map->stripes[stripe_index].dev;
6189+
6190+ if (op == BTRFS_MAP_READ && btrfs_need_stripe_tree_update(fs_info, map->type))
6191+ return btrfs_get_raid_extent_offset(fs_info, logical, length,
6192+ map->type, stripe_index, dst);
6193+
62136194 dst->physical = map->stripes[stripe_index].physical +
62146195 stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr);
6196+ return 0;
62156197 }
62166198
62176199 /*
@@ -6248,16 +6230,11 @@ static void set_io_stripe(struct btrfs_io_stripe *dst, const struct map_lookup * · 30 unmodified lines
62486230 * For RAID6 profile, mirror > 2 means mark another
62496231 * data/P stripe error and rebuild from the remaining
62506232 * stripes..
6251− *
6252− * @need_raid_map: (Used only for integrity checker) whether the map wants
6253− * a full stripe map (including all data and P/Q stripes)
6254− * for RAID56. Should always be 1 except integrity checker.
62556233 */
62566234 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
62576235 u64 logical, u64 *length,
62586236 struct btrfs_io_context **bioc_ret,
6259− struct btrfs_io_stripe *smap, int *mirror_num_ret,
6260− int need_raid_map)
6237+ struct btrfs_io_stripe *smap, int *mirror_num_ret)
62616238 {
62626239 struct extent_map *em;
62636240 struct map_lookup *map;
@@ -6352,8 +6329,10 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 88 unmodified lines
63526329 }
63536330
63546331 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6355− if (need_raid_map && (op != BTRFS_MAP_READ || mirror_num > 1)) {
6332+ if (op != BTRFS_MAP_READ || mirror_num > 1) {
63566333 /*
6334+ * Needs full stripe mapping.
6335+ *
63576336 * Push stripe_nr back to the start of the full stripe
63586337 * For those cases needing a full stripe, @stripe_nr
63596338 * is the full stripe number.
@@ -6376,19 +6355,14 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 16 unmodified lines
63766355 stripe_index = 0;
63776356 stripe_offset = 0;
63786357 } else {
6379− /*
6380− * Mirror #0 or #1 means the original data block.
6381− * Mirror #2 is RAID5 parity block.
6382− * Mirror #3 is RAID6 Q block.
6383− */
6358+ ASSERT(mirror_num <= 1);
6359+ /* Just grab the data stripe directly. */
63846360 stripe_index = stripe_nr % data_stripes;
63856361 stripe_nr /= data_stripes;
6386− if (mirror_num > 1)
6387− stripe_index = data_stripes + mirror_num - 2;
63886362
63896363 /* We distribute the parity blocks across stripes */
63906364 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
6391− if (op == BTRFS_MAP_READ && mirror_num <= 1)
6365+ if (op == BTRFS_MAP_READ && mirror_num < 1)
63926366 mirror_num = 1;
63936367 }
63946368 } else {
@@ -6427,16 +6401,18 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 32 unmodified lines
64276401 * I/O context structure.
64286402 */
64296403 if (smap && num_alloc_stripes == 1 &&
6404+ !(btrfs_need_stripe_tree_update(fs_info, map->type) &&
6405+ op != BTRFS_MAP_READ) &&
64306406 !((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1)) {
6431− set_io_stripe(smap, map, stripe_index, stripe_offset, stripe_nr);
6407+ ret = set_io_stripe(fs_info, op, logical, length, smap, map,
6408+ stripe_index, stripe_offset, stripe_nr);
64326409 if (mirror_num_ret)
64336410 *mirror_num_ret = mirror_num;
64346411 *bioc_ret = NULL;
6435− ret = 0;
64366412 goto out;
64376413 }
64386414
6439− bioc = alloc_btrfs_io_context(fs_info, num_alloc_stripes);
6415+ bioc = alloc_btrfs_io_context(fs_info, logical, num_alloc_stripes);
64406416 if (!bioc) {
64416417 ret = -ENOMEM;
64426418 goto out;
@@ -6450,7 +6426,7 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 7 unmodified lines
64506426 *
64516427 * It's still mostly the same as other profiles, just with extra rotation.
64526428 */
6453− if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6429+ if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
64546430 (op != BTRFS_MAP_READ || mirror_num > 1)) {
64556431 /*
64566432 * For RAID56 @stripe_nr is already the number of full stripes
@@ -6462,22 +6438,35 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 5 unmodified lines
64626438 */
64636439 bioc->full_stripe_logical = em->start +
64646440 btrfs_stripe_nr_to_offset(stripe_nr * data_stripes);
6465− for (i = 0; i < num_stripes; i++)
6466− set_io_stripe(&bioc->stripes[i], map,
6467− (i + stripe_nr) % num_stripes,
6468− stripe_offset, stripe_nr);
6441+ for (int i = 0; i < num_stripes; i++) {
6442+ ret = set_io_stripe(fs_info, op, logical, length,
6443+ &bioc->stripes[i], map,
6444+ (i + stripe_nr) % num_stripes,
6445+ stripe_offset, stripe_nr);
6446+ if (ret < 0)
6447+ break;
6448+ }
64696449 } else {
64706450 /*
64716451 * For all other non-RAID56 profiles, just copy the target
64726452 * stripe into the bioc.
64736453 */
64746454 for (i = 0; i < num_stripes; i++) {
6475− set_io_stripe(&bioc->stripes[i], map, stripe_index,
6476− stripe_offset, stripe_nr);
6455+ ret = set_io_stripe(fs_info, op, logical, length,
6456+ &bioc->stripes[i], map, stripe_index,
6457+ stripe_offset, stripe_nr);
6458+ if (ret < 0)
6459+ break;
64776460 stripe_index++;
64786461 }
64796462 }
64806463
6464+ if (ret) {
6465+ *bioc_ret = NULL;
6466+ btrfs_put_bioc(bioc);
6467+ goto out;
6468+ }
6469+
64816470 if (op != BTRFS_MAP_READ)
64826471 max_errors = btrfs_chunk_max_errors(map);
64836472
@@ -6904,7 +6893,7 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, · 420 unmodified lines
69046893 if (!btrfs_test_opt(fs_info, DEGRADED))
69056894 return ERR_PTR(-ENOENT);
69066895
6907− fs_devices = alloc_fs_devices(fsid, NULL);
6896+ fs_devices = alloc_fs_devices(fsid);
69086897 if (IS_ERR(fs_devices))
69096898 return fs_devices;
69106899
@@ -7537,7 +7526,7 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans, · 626 unmodified lines
75377526 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
75387527 btrfs_set_dev_stats_value(eb, ptr, i,
75397528 btrfs_dev_stat_read(device, i));
7540− btrfs_mark_buffer_dirty(eb);
7529+ btrfs_mark_buffer_dirty(trans, eb);
75417530
75427531 out:
75437532 btrfs_free_path(path);
@@ -8079,7 +8068,7 @@ int btrfs_map_repair_block(struct btrfs_fs_info *fs_info, · 535 unmodified lines
80798068 ASSERT(mirror_num > 0);
80808069
80818070 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, &map_length,
8082− &bioc, smap, &mirror_ret, true);
8071+ &bioc, smap, &mirror_ret);
80838072 if (ret < 0)
80848073 return ret;
80858074
fs/btrfs/volumes.h+29−10
@@ -288,6 +288,19 @@ struct btrfs_fs_devices { · 287 unmodified lines
288288 * - Following shall be true at all times:
289289 * - metadata_uuid == btrfs_header::fsid
290290 * - metadata_uuid == btrfs_dev_item::fsid
291+ *
292+ * - Relations between fsid and metadata_uuid in sb and fs_devices:
293+ * - Normal:
294+ * fs_devices->fsid == fs_devices->metadata_uuid == sb->fsid
295+ * sb->metadata_uuid == 0
296+ *
297+ * - When the BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag is set:
298+ * fs_devices->fsid == sb->fsid
299+ * fs_devices->metadata_uuid == sb->metadata_uuid
300+ *
301+ * - When in-memory fs_devices->temp_fsid is true
302+ * fs_devices->fsid = random
303+ * fs_devices->metadata_uuid == sb->fsid
291304 */
292305 u8 metadata_uuid[BTRFS_FSID_SIZE];
293306
@@ -351,9 +364,10 @@ struct btrfs_fs_devices { · 57 unmodified lines
351364 bool rotating;
352365 /* Devices support TRIM/discard commands. */
353366 bool discardable;
354− bool fsid_change;
355367 /* The filesystem is a seed filesystem. */
356368 bool seeding;
369+ /* The mount needs to use a randomly generated fsid. */
370+ bool temp_fsid;
357371
358372 struct btrfs_fs_info *fs_info;
359373 /* sysfs kobjects */
@@ -379,12 +393,12 @@ struct btrfs_fs_devices { · 19 unmodified lines
379393
380394 struct btrfs_io_stripe {
381395 struct btrfs_device *dev;
382− union {
383− /* Block mapping */
384− u64 physical;
385− /* For the endio handler */
386− struct btrfs_io_context *bioc;
387− };
396+ /* Block mapping. */
397+ u64 physical;
398+ u64 length;
399+ bool is_scrub;
400+ /* For the endio handler. */
401+ struct btrfs_io_context *bioc;
388402 };
389403
390404 struct btrfs_discard_stripe {
@@ -417,6 +431,11 @@ struct btrfs_io_context { · 26 unmodified lines
417431 atomic_t error;
418432 u16 max_errors;
419433
434+ u64 logical;
435+ u64 size;
436+ /* Raid stripe tree ordered entry. */
437+ struct list_head rst_ordered_entry;
438+
420439 /*
421440 * The total number of stripes, including the extra duplicated
422441 * stripe for replace.
@@ -594,8 +613,7 @@ void btrfs_put_bioc(struct btrfs_io_context *bioc); · 171 unmodified lines
594613 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
595614 u64 logical, u64 *length,
596615 struct btrfs_io_context **bioc_ret,
597− struct btrfs_io_stripe *smap, int *mirror_num_ret,
598− int need_raid_map);
616+ struct btrfs_io_stripe *smap, int *mirror_num_ret);
599617 int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,
600618 struct btrfs_io_stripe *smap, u64 logical,
601619 u32 length, int mirror_num);
@@ -609,7 +627,8 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans, · 7 unmodified lines
609627 void btrfs_mapping_tree_free(struct extent_map_tree *tree);
610628 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
611629 blk_mode_t flags, void *holder);
612−struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags);
630+struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
631+ bool mount_arg_dev);
613632 int btrfs_forget_devices(dev_t devt);
614633 void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
615634 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);
fs/btrfs/xattr.c+6−6
@@ -188,15 +188,15 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, · 187 unmodified lines
188188 if (old_data_len + name_len + sizeof(*di) == item_size) {
189189 /* No other xattrs packed in the same leaf item. */
190190 if (size > old_data_len)
191− btrfs_extend_item(path, size - old_data_len);
191+ btrfs_extend_item(trans, path, size - old_data_len);
192192 else if (size < old_data_len)
193− btrfs_truncate_item(path, data_size, 1);
193+ btrfs_truncate_item(trans, path, data_size, 1);
194194 } else {
195195 /* There are other xattrs packed in the same item. */
196196 ret = btrfs_delete_one_dir_name(trans, root, path, di);
197197 if (ret)
198198 goto out;
199− btrfs_extend_item(path, data_size);
199+ btrfs_extend_item(trans, path, data_size);
200200 }
201201
202202 ptr = btrfs_item_ptr(leaf, slot, char);
@@ -205,7 +205,7 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, · 2 unmodified lines
205205 btrfs_set_dir_data_len(leaf, di, size);
206206 data_ptr = ((unsigned long)(di + 1)) + name_len;
207207 write_extent_buffer(leaf, value, data_ptr, size);
208− btrfs_mark_buffer_dirty(leaf);
208+ btrfs_mark_buffer_dirty(trans, leaf);
209209 } else {
210210 /*
211211 * Insert, and we had space for the xattr, so path->slots[0] is
@@ -265,7 +265,7 @@ int btrfs_setxattr_trans(struct inode *inode, const char *name, · 53 unmodified lines
265265
266266 inode_inc_iversion(inode);
267267 inode_set_ctime_current(inode);
268− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
268+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
269269 if (ret)
270270 btrfs_abort_transaction(trans, ret);
271271 out:
@@ -408,7 +408,7 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, · 136 unmodified lines
408408 if (!ret) {
409409 inode_inc_iversion(inode);
410410 inode_set_ctime_current(inode);
411− ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
411+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
412412 if (ret)
413413 btrfs_abort_transaction(trans, ret);
414414 }
fs/btrfs/zoned.c+289−163
@@ -1282,21 +1282,284 @@ out: · 1281 unmodified lines
12821282 return ret;
12831283 }
12841284
1285+struct zone_info {
1286+ u64 physical;
1287+ u64 capacity;
1288+ u64 alloc_offset;
1289+};
1290+
1291+static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
1292+ struct zone_info *info, unsigned long *active,
1293+ struct map_lookup *map)
1294+{
1295+ struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1296+ struct btrfs_device *device = map->stripes[zone_idx].dev;
1297+ int dev_replace_is_ongoing = 0;
1298+ unsigned int nofs_flag;
1299+ struct blk_zone zone;
1300+ int ret;
1301+
1302+ info->physical = map->stripes[zone_idx].physical;
1303+
1304+ if (!device->bdev) {
1305+ info->alloc_offset = WP_MISSING_DEV;
1306+ return 0;
1307+ }
1308+
1309+ /* Consider a zone as active if we can allow any number of active zones. */
1310+ if (!device->zone_info->max_active_zones)
1311+ __set_bit(zone_idx, active);
1312+
1313+ if (!btrfs_dev_is_sequential(device, info->physical)) {
1314+ info->alloc_offset = WP_CONVENTIONAL;
1315+ return 0;
1316+ }
1317+
1318+ /* This zone will be used for allocation, so mark this zone non-empty. */
1319+ btrfs_dev_clear_zone_empty(device, info->physical);
1320+
1321+ down_read(&dev_replace->rwsem);
1322+ dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1323+ if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1324+ btrfs_dev_clear_zone_empty(dev_replace->tgtdev, info->physical);
1325+ up_read(&dev_replace->rwsem);
1326+
1327+ /*
1328+ * The group is mapped to a sequential zone. Get the zone write pointer
1329+ * to determine the allocation offset within the zone.
1330+ */
1331+ WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size));
1332+ nofs_flag = memalloc_nofs_save();
1333+ ret = btrfs_get_dev_zone(device, info->physical, &zone);
1334+ memalloc_nofs_restore(nofs_flag);
1335+ if (ret) {
1336+ if (ret != -EIO && ret != -EOPNOTSUPP)
1337+ return ret;
1338+ info->alloc_offset = WP_MISSING_DEV;
1339+ return 0;
1340+ }
1341+
1342+ if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1343+ btrfs_err_in_rcu(fs_info,
1344+ "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1345+ zone.start << SECTOR_SHIFT, rcu_str_deref(device->name),
1346+ device->devid);
1347+ return -EIO;
1348+ }
1349+
1350+ info->capacity = (zone.capacity << SECTOR_SHIFT);
1351+
1352+ switch (zone.cond) {
1353+ case BLK_ZONE_COND_OFFLINE:
1354+ case BLK_ZONE_COND_READONLY:
1355+ btrfs_err(fs_info,
1356+ "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1357+ (info->physical >> device->zone_info->zone_size_shift),
1358+ rcu_str_deref(device->name), device->devid);
1359+ info->alloc_offset = WP_MISSING_DEV;
1360+ break;
1361+ case BLK_ZONE_COND_EMPTY:
1362+ info->alloc_offset = 0;
1363+ break;
1364+ case BLK_ZONE_COND_FULL:
1365+ info->alloc_offset = info->capacity;
1366+ break;
1367+ default:
1368+ /* Partially used zone. */
1369+ info->alloc_offset = ((zone.wp - zone.start) << SECTOR_SHIFT);
1370+ __set_bit(zone_idx, active);
1371+ break;
1372+ }
1373+
1374+ return 0;
1375+}
1376+
1377+static int btrfs_load_block_group_single(struct btrfs_block_group *bg,
1378+ struct zone_info *info,
1379+ unsigned long *active)
1380+{
1381+ if (info->alloc_offset == WP_MISSING_DEV) {
1382+ btrfs_err(bg->fs_info,
1383+ "zoned: cannot recover write pointer for zone %llu",
1384+ info->physical);
1385+ return -EIO;
1386+ }
1387+
1388+ bg->alloc_offset = info->alloc_offset;
1389+ bg->zone_capacity = info->capacity;
1390+ if (test_bit(0, active))
1391+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1392+ return 0;
1393+}
1394+
1395+static int btrfs_load_block_group_dup(struct btrfs_block_group *bg,
1396+ struct map_lookup *map,
1397+ struct zone_info *zone_info,
1398+ unsigned long *active)
1399+{
1400+ struct btrfs_fs_info *fs_info = bg->fs_info;
1401+
1402+ if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1403+ btrfs_err(fs_info, "zoned: data DUP profile needs raid-stripe-tree");
1404+ return -EINVAL;
1405+ }
1406+
1407+ if (zone_info[0].alloc_offset == WP_MISSING_DEV) {
1408+ btrfs_err(bg->fs_info,
1409+ "zoned: cannot recover write pointer for zone %llu",
1410+ zone_info[0].physical);
1411+ return -EIO;
1412+ }
1413+ if (zone_info[1].alloc_offset == WP_MISSING_DEV) {
1414+ btrfs_err(bg->fs_info,
1415+ "zoned: cannot recover write pointer for zone %llu",
1416+ zone_info[1].physical);
1417+ return -EIO;
1418+ }
1419+ if (zone_info[0].alloc_offset != zone_info[1].alloc_offset) {
1420+ btrfs_err(bg->fs_info,
1421+ "zoned: write pointer offset mismatch of zones in DUP profile");
1422+ return -EIO;
1423+ }
1424+
1425+ if (test_bit(0, active) != test_bit(1, active)) {
1426+ if (!btrfs_zone_activate(bg))
1427+ return -EIO;
1428+ } else if (test_bit(0, active)) {
1429+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1430+ }
1431+
1432+ bg->alloc_offset = zone_info[0].alloc_offset;
1433+ bg->zone_capacity = min(zone_info[0].capacity, zone_info[1].capacity);
1434+ return 0;
1435+}
1436+
1437+static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg,
1438+ struct map_lookup *map,
1439+ struct zone_info *zone_info,
1440+ unsigned long *active)
1441+{
1442+ struct btrfs_fs_info *fs_info = bg->fs_info;
1443+ int i;
1444+
1445+ if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1446+ btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1447+ btrfs_bg_type_to_raid_name(map->type));
1448+ return -EINVAL;
1449+ }
1450+
1451+ for (i = 0; i < map->num_stripes; i++) {
1452+ if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1453+ zone_info[i].alloc_offset == WP_CONVENTIONAL)
1454+ continue;
1455+
1456+ if ((zone_info[0].alloc_offset != zone_info[i].alloc_offset) &&
1457+ !btrfs_test_opt(fs_info, DEGRADED)) {
1458+ btrfs_err(fs_info,
1459+ "zoned: write pointer offset mismatch of zones in %s profile",
1460+ btrfs_bg_type_to_raid_name(map->type));
1461+ return -EIO;
1462+ }
1463+ if (test_bit(0, active) != test_bit(i, active)) {
1464+ if (!btrfs_test_opt(fs_info, DEGRADED) &&
1465+ !btrfs_zone_activate(bg)) {
1466+ return -EIO;
1467+ }
1468+ } else {
1469+ if (test_bit(0, active))
1470+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1471+ }
1472+ /* In case a device is missing we have a cap of 0, so don't use it. */
1473+ bg->zone_capacity = min_not_zero(zone_info[0].capacity,
1474+ zone_info[1].capacity);
1475+ }
1476+
1477+ if (zone_info[0].alloc_offset != WP_MISSING_DEV)
1478+ bg->alloc_offset = zone_info[0].alloc_offset;
1479+ else
1480+ bg->alloc_offset = zone_info[i - 1].alloc_offset;
1481+
1482+ return 0;
1483+}
1484+
1485+static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
1486+ struct map_lookup *map,
1487+ struct zone_info *zone_info,
1488+ unsigned long *active)
1489+{
1490+ struct btrfs_fs_info *fs_info = bg->fs_info;
1491+
1492+ if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1493+ btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1494+ btrfs_bg_type_to_raid_name(map->type));
1495+ return -EINVAL;
1496+ }
1497+
1498+ for (int i = 0; i < map->num_stripes; i++) {
1499+ if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1500+ zone_info[i].alloc_offset == WP_CONVENTIONAL)
1501+ continue;
1502+
1503+ if (test_bit(0, active) != test_bit(i, active)) {
1504+ if (!btrfs_zone_activate(bg))
1505+ return -EIO;
1506+ } else {
1507+ if (test_bit(0, active))
1508+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1509+ }
1510+ bg->zone_capacity += zone_info[i].capacity;
1511+ bg->alloc_offset += zone_info[i].alloc_offset;
1512+ }
1513+
1514+ return 0;
1515+}
1516+
1517+static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
1518+ struct map_lookup *map,
1519+ struct zone_info *zone_info,
1520+ unsigned long *active)
1521+{
1522+ struct btrfs_fs_info *fs_info = bg->fs_info;
1523+
1524+ if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) {
1525+ btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree",
1526+ btrfs_bg_type_to_raid_name(map->type));
1527+ return -EINVAL;
1528+ }
1529+
1530+ for (int i = 0; i < map->num_stripes; i++) {
1531+ if (zone_info[i].alloc_offset == WP_MISSING_DEV ||
1532+ zone_info[i].alloc_offset == WP_CONVENTIONAL)
1533+ continue;
1534+
1535+ if (test_bit(0, active) != test_bit(i, active)) {
1536+ if (!btrfs_zone_activate(bg))
1537+ return -EIO;
1538+ } else {
1539+ if (test_bit(0, active))
1540+ set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags);
1541+ }
1542+
1543+ if ((i % map->sub_stripes) == 0) {
1544+ bg->zone_capacity += zone_info[i].capacity;
1545+ bg->alloc_offset += zone_info[i].alloc_offset;
1546+ }
1547+ }
1548+
1549+ return 0;
1550+}
1551+
12851552 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
12861553 {
12871554 struct btrfs_fs_info *fs_info = cache->fs_info;
12881555 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
12891556 struct extent_map *em;
12901557 struct map_lookup *map;
1291− struct btrfs_device *device;
12921558 u64 logical = cache->start;
12931559 u64 length = cache->length;
1560+ struct zone_info *zone_info = NULL;
12941561 int ret;
12951562 int i;
1296− unsigned int nofs_flag;
1297− u64 *alloc_offsets = NULL;
1298− u64 *caps = NULL;
1299− u64 *physical = NULL;
13001563 unsigned long *active = NULL;
13011564 u64 last_alloc = 0;
13021565 u32 num_sequential = 0, num_conventional = 0;
@@ -1328,20 +1591,8 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · 25 unmodified lines
13281591 goto out;
13291592 }
13301593
1331− alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1332− if (!alloc_offsets) {
1333− ret = -ENOMEM;
1334− goto out;
1335− }
1336−
1337− caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1338− if (!caps) {
1339− ret = -ENOMEM;
1340− goto out;
1341− }
1342−
1343− physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1344− if (!physical) {
1594+ zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS);
1595+ if (!zone_info) {
13451596 ret = -ENOMEM;
13461597 goto out;
13471598 }
@@ -1353,98 +1604,14 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · 5 unmodified lines
13531604 }
13541605
13551606 for (i = 0; i < map->num_stripes; i++) {
1356− bool is_sequential;
1357− struct blk_zone zone;
1358− struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1359− int dev_replace_is_ongoing = 0;
1360−
1361− device = map->stripes[i].dev;
1362− physical[i] = map->stripes[i].physical;
1363−
1364− if (device->bdev == NULL) {
1365− alloc_offsets[i] = WP_MISSING_DEV;
1366− continue;
1367− }
1368−
1369− is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1370− if (is_sequential)
1371− num_sequential++;
1372− else
1373− num_conventional++;
1374−
1375− /*
1376− * Consider a zone as active if we can allow any number of
1377− * active zones.
1378− */
1379− if (!device->zone_info->max_active_zones)
1380− __set_bit(i, active);
1381−
1382− if (!is_sequential) {
1383− alloc_offsets[i] = WP_CONVENTIONAL;
1384− continue;
1385− }
1386−
1387− /*
1388− * This zone will be used for allocation, so mark this zone
1389− * non-empty.
1390− */
1391− btrfs_dev_clear_zone_empty(device, physical[i]);
1392−
1393− down_read(&dev_replace->rwsem);
1394− dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1395− if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1396− btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1397− up_read(&dev_replace->rwsem);
1398−
1399− /*
1400− * The group is mapped to a sequential zone. Get the zone write
1401− * pointer to determine the allocation offset within the zone.
1402− */
1403− WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1404− nofs_flag = memalloc_nofs_save();
1405− ret = btrfs_get_dev_zone(device, physical[i], &zone);
1406− memalloc_nofs_restore(nofs_flag);
1407− if (ret == -EIO || ret == -EOPNOTSUPP) {
1408− ret = 0;
1409− alloc_offsets[i] = WP_MISSING_DEV;
1410− continue;
1411− } else if (ret) {
1412− goto out;
1413− }
1414−
1415− if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1416− btrfs_err_in_rcu(fs_info,
1417− "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1418− zone.start << SECTOR_SHIFT,
1419− rcu_str_deref(device->name), device->devid);
1420− ret = -EIO;
1607+ ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map);
1608+ if (ret)
14211609 goto out;
1422− }
14231610
1424− caps[i] = (zone.capacity << SECTOR_SHIFT);
1425−
1426− switch (zone.cond) {
1427− case BLK_ZONE_COND_OFFLINE:
1428− case BLK_ZONE_COND_READONLY:
1429− btrfs_err(fs_info,
1430− "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1431− physical[i] >> device->zone_info->zone_size_shift,
1432− rcu_str_deref(device->name), device->devid);
1433− alloc_offsets[i] = WP_MISSING_DEV;
1434− break;
1435− case BLK_ZONE_COND_EMPTY:
1436− alloc_offsets[i] = 0;
1437− break;
1438− case BLK_ZONE_COND_FULL:
1439− alloc_offsets[i] = caps[i];
1440− break;
1441− default:
1442− /* Partially used zone */
1443− alloc_offsets[i] =
1444− ((zone.wp - zone.start) << SECTOR_SHIFT);
1445− __set_bit(i, active);
1446− break;
1447− }
1611+ if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1612+ num_conventional++;
1613+ else
1614+ num_sequential++;
14481615 }
14491616
14501617 if (num_sequential > 0)
@@ -1468,63 +1635,24 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · 17 unmodified lines
14681635
14691636 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
14701637 case 0: /* single */
1471− if (alloc_offsets[0] == WP_MISSING_DEV) {
1472− btrfs_err(fs_info,
1473− "zoned: cannot recover write pointer for zone %llu",
1474− physical[0]);
1475− ret = -EIO;
1476− goto out;
1477− }
1478− cache->alloc_offset = alloc_offsets[0];
1479− cache->zone_capacity = caps[0];
1480− if (test_bit(0, active))
1481− set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags);
1638+ ret = btrfs_load_block_group_single(cache, &zone_info[0], active);
14821639 break;
14831640 case BTRFS_BLOCK_GROUP_DUP:
1484− if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1485− btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1486− ret = -EINVAL;
1487− goto out;
1488− }
1489− if (alloc_offsets[0] == WP_MISSING_DEV) {
1490− btrfs_err(fs_info,
1491− "zoned: cannot recover write pointer for zone %llu",
1492− physical[0]);
1493− ret = -EIO;
1494− goto out;
1495− }
1496− if (alloc_offsets[1] == WP_MISSING_DEV) {
1497− btrfs_err(fs_info,
1498− "zoned: cannot recover write pointer for zone %llu",
1499− physical[1]);
1500− ret = -EIO;
1501− goto out;
1502− }
1503− if (alloc_offsets[0] != alloc_offsets[1]) {
1504− btrfs_err(fs_info,
1505− "zoned: write pointer offset mismatch of zones in DUP profile");
1506− ret = -EIO;
1507− goto out;
1508− }
1509− if (test_bit(0, active) != test_bit(1, active)) {
1510− if (!btrfs_zone_activate(cache)) {
1511− ret = -EIO;
1512− goto out;
1513− }
1514− } else {
1515− if (test_bit(0, active))
1516− set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1517− &cache->runtime_flags);
1518− }
1519− cache->alloc_offset = alloc_offsets[0];
1520− cache->zone_capacity = min(caps[0], caps[1]);
1641+ ret = btrfs_load_block_group_dup(cache, map, zone_info, active);
15211642 break;
15221643 case BTRFS_BLOCK_GROUP_RAID1:
1644+ case BTRFS_BLOCK_GROUP_RAID1C3:
1645+ case BTRFS_BLOCK_GROUP_RAID1C4:
1646+ ret = btrfs_load_block_group_raid1(cache, map, zone_info, active);
1647+ break;
15231648 case BTRFS_BLOCK_GROUP_RAID0:
1649+ ret = btrfs_load_block_group_raid0(cache, map, zone_info, active);
1650+ break;
15241651 case BTRFS_BLOCK_GROUP_RAID10:
1652+ ret = btrfs_load_block_group_raid10(cache, map, zone_info, active);
1653+ break;
15251654 case BTRFS_BLOCK_GROUP_RAID5:
15261655 case BTRFS_BLOCK_GROUP_RAID6:
1527− /* non-single profiles are not supported yet */
15281656 default:
15291657 btrfs_err(fs_info, "zoned: profile %s not yet supported",
15301658 btrfs_bg_type_to_raid_name(map->type));
@@ -1570,9 +1698,7 @@ out: · 39 unmodified lines
15701698 cache->physical_map = NULL;
15711699 }
15721700 bitmap_free(active);
1573− kfree(physical);
1574− kfree(caps);
1575− kfree(alloc_offsets);
1701+ kfree(zone_info);
15761702 free_extent_map(em);
15771703
15781704 return ret;
@@ -1609,7 +1735,7 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans, · 30 unmodified lines
16091735 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
16101736 set_extent_buffer_dirty(eb);
16111737 set_extent_bit(&trans->dirty_pages, eb->start, eb->start + eb->len - 1,
1612− EXTENT_DIRTY | EXTENT_NOWAIT, NULL);
1738+ EXTENT_DIRTY, NULL);
16131739 }
16141740
16151741 bool btrfs_use_zone_append(struct btrfs_bio *bbio)
@@ -1887,7 +2013,7 @@ static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical, · 271 unmodified lines
18872013 int i, ret;
18882014
18892015 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1890− &mapped_length, &bioc, NULL, NULL, 1);
2016+ &mapped_length, &bioc, NULL, NULL);
18912017 if (ret || !bioc || mapped_length < PAGE_SIZE) {
18922018 ret = -EIO;
18932019 goto out_put_bioc;
fs/btrfs/zstd.c+7−4
@@ -145,7 +145,7 @@ static void zstd_reclaim_timer_fn(struct timer_list *timer) · 144 unmodified lines
145145 }
146146
147147 /*
148− * zstd_calc_ws_mem_sizes - calculate monotonic memory bounds
148+ * Calculate monotonic memory bounds.
149149 *
150150 * It is possible based on the level configurations that a higher level
151151 * workspace uses less memory than a lower level workspace. In order to reuse
@@ -218,7 +218,8 @@ void zstd_cleanup_workspace_manager(void) · 66 unmodified lines
218218 }
219219
220220 /*
221− * zstd_find_workspace - find workspace
221+ * Find workspace for given level.
222+ *
222223 * @level: compression level
223224 *
224225 * This iterates over the set bits in the active_map beginning at the requested
@@ -256,7 +257,8 @@ static struct list_head *zstd_find_workspace(unsigned int level) · 31 unmodified lines
256257 }
257258
258259 /*
259− * zstd_get_workspace - zstd's get_workspace
260+ * Zstd get_workspace for level.
261+ *
260262 * @level: compression level
261263 *
262264 * If @level is 0, then any compression level can be used. Therefore, we begin
@@ -296,7 +298,8 @@ again: · 33 unmodified lines
296298 }
297299
298300 /*
299− * zstd_put_workspace - zstd put_workspace
301+ * Zstd put_workspace.
302+ *
300303 * @ws: list_head for the workspace
301304 *
302305 * When putting back a workspace, we only need to update the LRU if we are of
include/trace/events/btrfs.h+78−5
@@ -1561,7 +1561,6 @@ DECLARE_EVENT_CLASS(btrfs__work, · 1560 unmodified lines
15611561 __field( const void *, wq )
15621562 __field( const void *, func )
15631563 __field( const void *, ordered_func )
1564− __field( const void *, ordered_free )
15651564 __field( const void *, normal_work )
15661565 ),
15671566
@@ -1570,14 +1569,12 @@ DECLARE_EVENT_CLASS(btrfs__work, · 2 unmodified lines
15701569 __entry->wq = work->wq;
15711570 __entry->func = work->func;
15721571 __entry->ordered_func = work->ordered_func;
1573− __entry->ordered_free = work->ordered_free;
15741572 __entry->normal_work = &work->normal_work;
15751573 ),
15761574
1577− TP_printk_btrfs("work=%p (normal_work=%p) wq=%p func=%ps ordered_func=%p "
1578− "ordered_free=%p",
1575+ TP_printk_btrfs("work=%p (normal_work=%p) wq=%p func=%ps ordered_func=%p",
15791576 __entry->work, __entry->normal_work, __entry->wq,
1580− __entry->func, __entry->ordered_func, __entry->ordered_free)
1577+ __entry->func, __entry->ordered_func)
15811578 );
15821579
15831580 /*
@@ -2497,6 +2494,82 @@ DEFINE_EVENT(btrfs_raid56_bio, raid56_write, · 913 unmodified lines
24972494 TP_ARGS(rbio, bio, trace_info)
24982495 );
24992496
2497+TRACE_EVENT(btrfs_insert_one_raid_extent,
2498+
2499+ TP_PROTO(const struct btrfs_fs_info *fs_info, u64 logical, u64 length,
2500+ int num_stripes),
2501+
2502+ TP_ARGS(fs_info, logical, length, num_stripes),
2503+
2504+ TP_STRUCT__entry_btrfs(
2505+ __field( u64, logical )
2506+ __field( u64, length )
2507+ __field( int, num_stripes )
2508+ ),
2509+
2510+ TP_fast_assign_btrfs(fs_info,
2511+ __entry->logical = logical;
2512+ __entry->length = length;
2513+ __entry->num_stripes = num_stripes;
2514+ ),
2515+
2516+ TP_printk_btrfs("logical=%llu length=%llu num_stripes=%d",
2517+ __entry->logical, __entry->length,
2518+ __entry->num_stripes)
2519+);
2520+
2521+TRACE_EVENT(btrfs_raid_extent_delete,
2522+
2523+ TP_PROTO(const struct btrfs_fs_info *fs_info, u64 start, u64 end,
2524+ u64 found_start, u64 found_end),
2525+
2526+ TP_ARGS(fs_info, start, end, found_start, found_end),
2527+
2528+ TP_STRUCT__entry_btrfs(
2529+ __field( u64, start )
2530+ __field( u64, end )
2531+ __field( u64, found_start )
2532+ __field( u64, found_end )
2533+ ),
2534+
2535+ TP_fast_assign_btrfs(fs_info,
2536+ __entry->start = start;
2537+ __entry->end = end;
2538+ __entry->found_start = found_start;
2539+ __entry->found_end = found_end;
2540+ ),
2541+
2542+ TP_printk_btrfs("start=%llu end=%llu found_start=%llu found_end=%llu",
2543+ __entry->start, __entry->end, __entry->found_start,
2544+ __entry->found_end)
2545+);
2546+
2547+TRACE_EVENT(btrfs_get_raid_extent_offset,
2548+
2549+ TP_PROTO(const struct btrfs_fs_info *fs_info, u64 logical, u64 length,
2550+ u64 physical, u64 devid),
2551+
2552+ TP_ARGS(fs_info, logical, length, physical, devid),
2553+
2554+ TP_STRUCT__entry_btrfs(
2555+ __field( u64, logical )
2556+ __field( u64, length )
2557+ __field( u64, physical )
2558+ __field( u64, devid )
2559+ ),
2560+
2561+ TP_fast_assign_btrfs(fs_info,
2562+ __entry->logical = logical;
2563+ __entry->length = length;
2564+ __entry->physical = physical;
2565+ __entry->devid = devid;
2566+ ),
2567+
2568+ TP_printk_btrfs("logical=%llu length=%llu physical=%llu devid=%llu",
2569+ __entry->logical, __entry->length, __entry->physical,
2570+ __entry->devid)
2571+);
2572+
25002573 #endif /* _TRACE_BTRFS_H */
25012574
25022575 /* This part must be outside protection */
include/uapi/linux/btrfs.h+3−0
@@ -333,6 +333,8 @@ struct btrfs_ioctl_fs_info_args { · 332 unmodified lines
333333 #define BTRFS_FEATURE_INCOMPAT_RAID1C34 (1ULL << 11)
334334 #define BTRFS_FEATURE_INCOMPAT_ZONED (1ULL << 12)
335335 #define BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2 (1ULL << 13)
336+#define BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE (1ULL << 14)
337+#define BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA (1ULL << 16)
336338
337339 struct btrfs_ioctl_feature_flags {
338340 __u64 compat_flags;
@@ -753,6 +755,7 @@ struct btrfs_ioctl_get_dev_stats { · 414 unmodified lines
753755 #define BTRFS_QUOTA_CTL_ENABLE 1
754756 #define BTRFS_QUOTA_CTL_DISABLE 2
755757 #define BTRFS_QUOTA_CTL_RESCAN__NOTUSED 3
758+#define BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA 4
756759 struct btrfs_ioctl_quota_ctl_args {
757760 __u64 cmd;
758761 __u64 status;
include/uapi/linux/btrfs_tree.h+59−1
@@ -73,6 +73,9 @@ · 72 unmodified lines
7373 /* Holds the block group items for extent tree v2. */
7474 #define BTRFS_BLOCK_GROUP_TREE_OBJECTID 11ULL
7575
76+/* Tracks RAID stripes in block groups. */
77+#define BTRFS_RAID_STRIPE_TREE_OBJECTID 12ULL
78+
7679 /* device stats in the device tree */
7780 #define BTRFS_DEV_STATS_OBJECTID 0ULL
7881
@@ -230,6 +233,14 @@ · 151 unmodified lines
230233
231234 #define BTRFS_SHARED_DATA_REF_KEY 184
232235
236+/*
237+ * Special inline ref key which stores the id of the subvolume which originally
238+ * created the extent. This subvolume owns the extent permanently from the
239+ * perspective of simple quotas. Needed to know which subvolume to free quota
240+ * usage from when the extent is deleted.
241+ */
242+#define BTRFS_EXTENT_OWNER_REF_KEY 188
243+
233244 /*
234245 * block groups give us hints into the extent allocation trees. Which
235246 * blocks are free etc etc
@@ -261,6 +272,8 @@ · 25 unmodified lines
261272 #define BTRFS_DEV_ITEM_KEY 216
262273 #define BTRFS_CHUNK_ITEM_KEY 228
263274
275+#define BTRFS_RAID_STRIPE_KEY 230
276+
264277 /*
265278 * Records the overall state of the qgroups.
266279 * There's only one instance of this key present,
@@ -719,6 +732,30 @@ struct btrfs_free_space_header { · 452 unmodified lines
719732 __le64 num_bitmaps;
720733 } __attribute__ ((__packed__));
721734
735+struct btrfs_raid_stride {
736+ /* The id of device this raid extent lives on. */
737+ __le64 devid;
738+ /* The physical location on disk. */
739+ __le64 physical;
740+} __attribute__ ((__packed__));
741+
742+/* The stripe_extent::encoding, 1:1 mapping of enum btrfs_raid_types. */
743+#define BTRFS_STRIPE_RAID0 1
744+#define BTRFS_STRIPE_RAID1 2
745+#define BTRFS_STRIPE_DUP 3
746+#define BTRFS_STRIPE_RAID10 4
747+#define BTRFS_STRIPE_RAID5 5
748+#define BTRFS_STRIPE_RAID6 6
749+#define BTRFS_STRIPE_RAID1C3 7
750+#define BTRFS_STRIPE_RAID1C4 8
751+
752+struct btrfs_stripe_extent {
753+ __u8 encoding;
754+ __u8 reserved[7];
755+ /* An array of raid strides this stripe is composed of. */
756+ struct btrfs_raid_stride strides[];
757+} __attribute__ ((__packed__));
758+
722759 #define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0)
723760 #define BTRFS_HEADER_FLAG_RELOC (1ULL << 1)
724761
@@ -787,6 +824,10 @@ struct btrfs_shared_data_ref { · 62 unmodified lines
787824 __le32 count;
788825 } __attribute__ ((__packed__));
789826
827+struct btrfs_extent_owner_ref {
828+ __le64 root_id;
829+} __attribute__ ((__packed__));
830+
790831 struct btrfs_extent_inline_ref {
791832 __u8 type;
792833 __le64 offset;
@@ -1204,9 +1245,17 @@ static inline __u16 btrfs_qgroup_level(__u64 qgroupid) · 411 unmodified lines
12041245 */
12051246 #define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2)
12061247
1248+/*
1249+ * Whether or not this filesystem is using simple quotas. Not exactly the
1250+ * incompat bit, because we support using simple quotas, disabling it, then
1251+ * going back to full qgroup quotas.
1252+ */
1253+#define BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE (1ULL << 3)
1254+
12071255 #define BTRFS_QGROUP_STATUS_FLAGS_MASK (BTRFS_QGROUP_STATUS_FLAG_ON | \
12081256 BTRFS_QGROUP_STATUS_FLAG_RESCAN | \
1209− BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
1257+ BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT | \
1258+ BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE)
12101259
12111260 #define BTRFS_QGROUP_STATUS_VERSION 1
12121261
@@ -1228,6 +1277,15 @@ struct btrfs_qgroup_status_item { · 15 unmodified lines
12281277 * of the scan. It contains a logical address
12291278 */
12301279 __le64 rescan;
1280+
1281+ /*
1282+ * The generation when quotas were last enabled. Used by simple quotas to
1283+ * avoid decrementing when freeing an extent that was written before
1284+ * enable.
1285+ *
1286+ * Set only if flags contain BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE.
1287+ */
1288+ __le64 enable_gen;
12311289 } __attribute__ ((__packed__));
12321290
12331291 struct btrfs_qgroup_info_item {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t